]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - sound/soc/codecs/wm8523.c
712ef7c76f90bbde579229ea85716dd14f7acb10
[~shefty/rdma-dev.git] / sound / soc / codecs / wm8523.c
1 /*
2  * wm8523.c  --  WM8523 ALSA SoC Audio driver
3  *
4  * Copyright 2009 Wolfson Microelectronics plc
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/pm.h>
19 #include <linux/i2c.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/soc-dapm.h>
28 #include <sound/initval.h>
29 #include <sound/tlv.h>
30
31 #include "wm8523.h"
32
33 #define WM8523_NUM_SUPPLIES 2
34 static const char *wm8523_supply_names[WM8523_NUM_SUPPLIES] = {
35         "AVDD",
36         "LINEVDD",
37 };
38
39 #define WM8523_NUM_RATES 7
40
41 /* codec private data */
42 struct wm8523_priv {
43         enum snd_soc_control_type control_type;
44         u16 reg_cache[WM8523_REGISTER_COUNT];
45         struct regulator_bulk_data supplies[WM8523_NUM_SUPPLIES];
46         unsigned int sysclk;
47         unsigned int rate_constraint_list[WM8523_NUM_RATES];
48         struct snd_pcm_hw_constraint_list rate_constraint;
49 };
50
51 static const u16 wm8523_reg[WM8523_REGISTER_COUNT] = {
52         0x8523,     /* R0 - DEVICE_ID */
53         0x0001,     /* R1 - REVISION */
54         0x0000,     /* R2 - PSCTRL1 */
55         0x1812,     /* R3 - AIF_CTRL1 */
56         0x0000,     /* R4 - AIF_CTRL2 */
57         0x0001,     /* R5 - DAC_CTRL3 */
58         0x0190,     /* R6 - DAC_GAINL */
59         0x0190,     /* R7 - DAC_GAINR */
60         0x0000,     /* R8 - ZERO_DETECT */
61 };
62
63 static int wm8523_volatile_register(unsigned int reg)
64 {
65         switch (reg) {
66         case WM8523_DEVICE_ID:
67         case WM8523_REVISION:
68                 return 1;
69         default:
70                 return 0;
71         }
72 }
73
74 static int wm8523_reset(struct snd_soc_codec *codec)
75 {
76         return snd_soc_write(codec, WM8523_DEVICE_ID, 0);
77 }
78
79 static const DECLARE_TLV_DB_SCALE(dac_tlv, -10000, 25, 0);
80
81 static const char *wm8523_zd_count_text[] = {
82         "1024",
83         "2048",
84 };
85
86 static const struct soc_enum wm8523_zc_count =
87         SOC_ENUM_SINGLE(WM8523_ZERO_DETECT, 0, 2, wm8523_zd_count_text);
88
89 static const struct snd_kcontrol_new wm8523_snd_controls[] = {
90 SOC_DOUBLE_R_TLV("Playback Volume", WM8523_DAC_GAINL, WM8523_DAC_GAINR,
91                  0, 448, 0, dac_tlv),
92 SOC_SINGLE("ZC Switch", WM8523_DAC_CTRL3, 4, 1, 0),
93 SOC_SINGLE("Playback Deemphasis Switch", WM8523_AIF_CTRL1, 8, 1, 0),
94 SOC_DOUBLE("Playback Switch", WM8523_DAC_CTRL3, 2, 3, 1, 1),
95 SOC_SINGLE("Volume Ramp Up Switch", WM8523_DAC_CTRL3, 1, 1, 0),
96 SOC_SINGLE("Volume Ramp Down Switch", WM8523_DAC_CTRL3, 0, 1, 0),
97 SOC_ENUM("Zero Detect Count", wm8523_zc_count),
98 };
99
100 static const struct snd_soc_dapm_widget wm8523_dapm_widgets[] = {
101 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
102 SND_SOC_DAPM_OUTPUT("LINEVOUTL"),
103 SND_SOC_DAPM_OUTPUT("LINEVOUTR"),
104 };
105
106 static const struct snd_soc_dapm_route intercon[] = {
107         { "LINEVOUTL", NULL, "DAC" },
108         { "LINEVOUTR", NULL, "DAC" },
109 };
110
111 static int wm8523_add_widgets(struct snd_soc_codec *codec)
112 {
113         snd_soc_dapm_new_controls(codec, wm8523_dapm_widgets,
114                                   ARRAY_SIZE(wm8523_dapm_widgets));
115
116         snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
117
118         return 0;
119 }
120
121 static struct {
122         int value;
123         int ratio;
124 } lrclk_ratios[WM8523_NUM_RATES] = {
125         { 1, 128 },
126         { 2, 192 },
127         { 3, 256 },
128         { 4, 384 },
129         { 5, 512 },
130         { 6, 768 },
131         { 7, 1152 },
132 };
133
134 static int wm8523_startup(struct snd_pcm_substream *substream,
135                           struct snd_soc_dai *dai)
136 {
137         struct snd_soc_codec *codec = dai->codec;
138         struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
139
140         /* The set of sample rates that can be supported depends on the
141          * MCLK supplied to the CODEC - enforce this.
142          */
143         if (!wm8523->sysclk) {
144                 dev_err(codec->dev,
145                         "No MCLK configured, call set_sysclk() on init\n");
146                 return -EINVAL;
147         }
148
149         return 0;
150         snd_pcm_hw_constraint_list(substream->runtime, 0,
151                                    SNDRV_PCM_HW_PARAM_RATE,
152                                    &wm8523->rate_constraint);
153
154         return 0;
155 }
156
157 static int wm8523_hw_params(struct snd_pcm_substream *substream,
158                             struct snd_pcm_hw_params *params,
159                             struct snd_soc_dai *dai)
160 {
161         struct snd_soc_pcm_runtime *rtd = substream->private_data;
162         struct snd_soc_codec *codec = rtd->codec;
163         struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
164         int i;
165         u16 aifctrl1 = snd_soc_read(codec, WM8523_AIF_CTRL1);
166         u16 aifctrl2 = snd_soc_read(codec, WM8523_AIF_CTRL2);
167
168         /* Find a supported LRCLK ratio */
169         for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
170                 if (wm8523->sysclk / params_rate(params) ==
171                     lrclk_ratios[i].ratio)
172                         break;
173         }
174
175         /* Should never happen, should be handled by constraints */
176         if (i == ARRAY_SIZE(lrclk_ratios)) {
177                 dev_err(codec->dev, "MCLK/fs ratio %d unsupported\n",
178                         wm8523->sysclk / params_rate(params));
179                 return -EINVAL;
180         }
181
182         aifctrl2 &= ~WM8523_SR_MASK;
183         aifctrl2 |= lrclk_ratios[i].value;
184
185         aifctrl1 &= ~WM8523_WL_MASK;
186         switch (params_format(params)) {
187         case SNDRV_PCM_FORMAT_S16_LE:
188                 break;
189         case SNDRV_PCM_FORMAT_S20_3LE:
190                 aifctrl1 |= 0x8;
191                 break;
192         case SNDRV_PCM_FORMAT_S24_LE:
193                 aifctrl1 |= 0x10;
194                 break;
195         case SNDRV_PCM_FORMAT_S32_LE:
196                 aifctrl1 |= 0x18;
197                 break;
198         }
199
200         snd_soc_write(codec, WM8523_AIF_CTRL1, aifctrl1);
201         snd_soc_write(codec, WM8523_AIF_CTRL2, aifctrl2);
202
203         return 0;
204 }
205
206 static int wm8523_set_dai_sysclk(struct snd_soc_dai *codec_dai,
207                 int clk_id, unsigned int freq, int dir)
208 {
209         struct snd_soc_codec *codec = codec_dai->codec;
210         struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
211         unsigned int val;
212         int i;
213
214         wm8523->sysclk = freq;
215
216         wm8523->rate_constraint.count = 0;
217         for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
218                 val = freq / lrclk_ratios[i].ratio;
219                 /* Check that it's a standard rate since core can't
220                  * cope with others and having the odd rates confuses
221                  * constraint matching.
222                  */
223                 switch (val) {
224                 case 8000:
225                 case 11025:
226                 case 16000:
227                 case 22050:
228                 case 32000:
229                 case 44100:
230                 case 48000:
231                 case 64000:
232                 case 88200:
233                 case 96000:
234                 case 176400:
235                 case 192000:
236                         dev_dbg(codec->dev, "Supported sample rate: %dHz\n",
237                                 val);
238                         wm8523->rate_constraint_list[i] = val;
239                         wm8523->rate_constraint.count++;
240                         break;
241                 default:
242                         dev_dbg(codec->dev, "Skipping sample rate: %dHz\n",
243                                 val);
244                 }
245         }
246
247         /* Need at least one supported rate... */
248         if (wm8523->rate_constraint.count == 0)
249                 return -EINVAL;
250
251         return 0;
252 }
253
254
255 static int wm8523_set_dai_fmt(struct snd_soc_dai *codec_dai,
256                 unsigned int fmt)
257 {
258         struct snd_soc_codec *codec = codec_dai->codec;
259         u16 aifctrl1 = snd_soc_read(codec, WM8523_AIF_CTRL1);
260
261         aifctrl1 &= ~(WM8523_BCLK_INV_MASK | WM8523_LRCLK_INV_MASK |
262                       WM8523_FMT_MASK | WM8523_AIF_MSTR_MASK);
263
264         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
265         case SND_SOC_DAIFMT_CBM_CFM:
266                 aifctrl1 |= WM8523_AIF_MSTR;
267                 break;
268         case SND_SOC_DAIFMT_CBS_CFS:
269                 break;
270         default:
271                 return -EINVAL;
272         }
273
274         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
275         case SND_SOC_DAIFMT_I2S:
276                 aifctrl1 |= 0x0002;
277                 break;
278         case SND_SOC_DAIFMT_RIGHT_J:
279                 break;
280         case SND_SOC_DAIFMT_LEFT_J:
281                 aifctrl1 |= 0x0001;
282                 break;
283         case SND_SOC_DAIFMT_DSP_A:
284                 aifctrl1 |= 0x0003;
285                 break;
286         case SND_SOC_DAIFMT_DSP_B:
287                 aifctrl1 |= 0x0023;
288                 break;
289         default:
290                 return -EINVAL;
291         }
292
293         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
294         case SND_SOC_DAIFMT_NB_NF:
295                 break;
296         case SND_SOC_DAIFMT_IB_IF:
297                 aifctrl1 |= WM8523_BCLK_INV | WM8523_LRCLK_INV;
298                 break;
299         case SND_SOC_DAIFMT_IB_NF:
300                 aifctrl1 |= WM8523_BCLK_INV;
301                 break;
302         case SND_SOC_DAIFMT_NB_IF:
303                 aifctrl1 |= WM8523_LRCLK_INV;
304                 break;
305         default:
306                 return -EINVAL;
307         }
308
309         snd_soc_write(codec, WM8523_AIF_CTRL1, aifctrl1);
310
311         return 0;
312 }
313
314 static int wm8523_set_bias_level(struct snd_soc_codec *codec,
315                                  enum snd_soc_bias_level level)
316 {
317         struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
318         int ret, i;
319
320         switch (level) {
321         case SND_SOC_BIAS_ON:
322                 break;
323
324         case SND_SOC_BIAS_PREPARE:
325                 /* Full power on */
326                 snd_soc_update_bits(codec, WM8523_PSCTRL1,
327                                     WM8523_SYS_ENA_MASK, 3);
328                 break;
329
330         case SND_SOC_BIAS_STANDBY:
331                 if (codec->bias_level == SND_SOC_BIAS_OFF) {
332                         ret = regulator_bulk_enable(ARRAY_SIZE(wm8523->supplies),
333                                                     wm8523->supplies);
334                         if (ret != 0) {
335                                 dev_err(codec->dev,
336                                         "Failed to enable supplies: %d\n",
337                                         ret);
338                                 return ret;
339                         }
340
341                         /* Initial power up */
342                         snd_soc_update_bits(codec, WM8523_PSCTRL1,
343                                             WM8523_SYS_ENA_MASK, 1);
344
345                         /* Sync back default/cached values */
346                         for (i = WM8523_AIF_CTRL1;
347                              i < WM8523_MAX_REGISTER; i++)
348                                 snd_soc_write(codec, i, wm8523->reg_cache[i]);
349
350
351                         msleep(100);
352                 }
353
354                 /* Power up to mute */
355                 snd_soc_update_bits(codec, WM8523_PSCTRL1,
356                                     WM8523_SYS_ENA_MASK, 2);
357
358                 break;
359
360         case SND_SOC_BIAS_OFF:
361                 /* The chip runs through the power down sequence for us. */
362                 snd_soc_update_bits(codec, WM8523_PSCTRL1,
363                                     WM8523_SYS_ENA_MASK, 0);
364                 msleep(100);
365
366                 regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies),
367                                        wm8523->supplies);
368                 break;
369         }
370         codec->bias_level = level;
371         return 0;
372 }
373
374 #define WM8523_RATES SNDRV_PCM_RATE_8000_192000
375
376 #define WM8523_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
377                         SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
378
379 static struct snd_soc_dai_ops wm8523_dai_ops = {
380         .startup        = wm8523_startup,
381         .hw_params      = wm8523_hw_params,
382         .set_sysclk     = wm8523_set_dai_sysclk,
383         .set_fmt        = wm8523_set_dai_fmt,
384 };
385
386 static struct snd_soc_dai_driver wm8523_dai = {
387         .name = "wm8523-hifi",
388         .playback = {
389                 .stream_name = "Playback",
390                 .channels_min = 2,  /* Mono modes not yet supported */
391                 .channels_max = 2,
392                 .rates = WM8523_RATES,
393                 .formats = WM8523_FORMATS,
394         },
395         .ops = &wm8523_dai_ops,
396 };
397
398 #ifdef CONFIG_PM
399 static int wm8523_suspend(struct snd_soc_codec *codec, pm_message_t state)
400 {
401         wm8523_set_bias_level(codec, SND_SOC_BIAS_OFF);
402         return 0;
403 }
404
405 static int wm8523_resume(struct snd_soc_codec *codec)
406 {
407         wm8523_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
408         return 0;
409 }
410 #else
411 #define wm8523_suspend NULL
412 #define wm8523_resume NULL
413 #endif
414
415 static int wm8523_probe(struct snd_soc_codec *codec)
416 {
417         struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
418         int ret, i;
419
420         codec->hw_write = (hw_write_t)i2c_master_send;
421         wm8523->rate_constraint.list = &wm8523->rate_constraint_list[0];
422         wm8523->rate_constraint.count =
423                 ARRAY_SIZE(wm8523->rate_constraint_list);
424
425         ret = snd_soc_codec_set_cache_io(codec, 8, 16, wm8523->control_type);
426         if (ret != 0) {
427                 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
428                 return ret;
429         }
430
431         for (i = 0; i < ARRAY_SIZE(wm8523->supplies); i++)
432                 wm8523->supplies[i].supply = wm8523_supply_names[i];
433
434         ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8523->supplies),
435                                  wm8523->supplies);
436         if (ret != 0) {
437                 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
438                 return ret;
439         }
440
441         ret = regulator_bulk_enable(ARRAY_SIZE(wm8523->supplies),
442                                     wm8523->supplies);
443         if (ret != 0) {
444                 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
445                 goto err_get;
446         }
447
448         ret = snd_soc_read(codec, WM8523_DEVICE_ID);
449         if (ret < 0) {
450                 dev_err(codec->dev, "Failed to read ID register\n");
451                 goto err_enable;
452         }
453         if (ret != wm8523_reg[WM8523_DEVICE_ID]) {
454                 dev_err(codec->dev, "Device is not a WM8523, ID is %x\n", ret);
455                 ret = -EINVAL;
456                 goto err_enable;
457         }
458
459         ret = snd_soc_read(codec, WM8523_REVISION);
460         if (ret < 0) {
461                 dev_err(codec->dev, "Failed to read revision register\n");
462                 goto err_enable;
463         }
464         dev_info(codec->dev, "revision %c\n",
465                  (ret & WM8523_CHIP_REV_MASK) + 'A');
466
467         ret = wm8523_reset(codec);
468         if (ret < 0) {
469                 dev_err(codec->dev, "Failed to issue reset\n");
470                 goto err_enable;
471         }
472
473         /* Change some default settings - latch VU and enable ZC */
474         wm8523->reg_cache[WM8523_DAC_GAINR] |= WM8523_DACR_VU;
475         wm8523->reg_cache[WM8523_DAC_CTRL3] |= WM8523_ZC;
476
477         wm8523_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
478
479         /* Bias level configuration will have done an extra enable */
480         regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
481
482         snd_soc_add_controls(codec, wm8523_snd_controls,
483                              ARRAY_SIZE(wm8523_snd_controls));
484         wm8523_add_widgets(codec);
485
486         return 0;
487
488 err_enable:
489         regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
490 err_get:
491         regulator_bulk_free(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
492
493         return ret;
494 }
495
496 static int wm8523_remove(struct snd_soc_codec *codec)
497 {
498         struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
499
500         wm8523_set_bias_level(codec, SND_SOC_BIAS_OFF);
501         regulator_bulk_free(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
502         return 0;
503 }
504
505 static struct snd_soc_codec_driver soc_codec_dev_wm8523 = {
506         .probe =        wm8523_probe,
507         .remove =       wm8523_remove,
508         .suspend =      wm8523_suspend,
509         .resume =       wm8523_resume,
510         .set_bias_level = wm8523_set_bias_level,
511         .reg_cache_size = WM8523_REGISTER_COUNT,
512         .reg_word_size = sizeof(u16),
513         .reg_cache_default = wm8523_reg,
514         .volatile_register = wm8523_volatile_register,
515 };
516
517 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
518 static __devinit int wm8523_i2c_probe(struct i2c_client *i2c,
519                                       const struct i2c_device_id *id)
520 {
521         struct wm8523_priv *wm8523;
522         int ret;
523
524         wm8523 = kzalloc(sizeof(struct wm8523_priv), GFP_KERNEL);
525         if (wm8523 == NULL)
526                 return -ENOMEM;
527
528         i2c_set_clientdata(i2c, wm8523);
529         wm8523->control_type = SND_SOC_I2C;
530
531         ret =  snd_soc_register_codec(&i2c->dev,
532                         &soc_codec_dev_wm8523, &wm8523_dai, 1);
533         if (ret < 0)
534                 kfree(wm8523);
535         return ret;
536
537 }
538
539 static __devexit int wm8523_i2c_remove(struct i2c_client *client)
540 {
541         snd_soc_unregister_codec(&client->dev);
542         kfree(i2c_get_clientdata(client));
543         return 0;
544 }
545
546 static const struct i2c_device_id wm8523_i2c_id[] = {
547         { "wm8523", 0 },
548         { }
549 };
550 MODULE_DEVICE_TABLE(i2c, wm8523_i2c_id);
551
552 static struct i2c_driver wm8523_i2c_driver = {
553         .driver = {
554                 .name = "wm8523-codec",
555                 .owner = THIS_MODULE,
556         },
557         .probe =    wm8523_i2c_probe,
558         .remove =   __devexit_p(wm8523_i2c_remove),
559         .id_table = wm8523_i2c_id,
560 };
561 #endif
562
563 static int __init wm8523_modinit(void)
564 {
565         int ret;
566 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
567         ret = i2c_add_driver(&wm8523_i2c_driver);
568         if (ret != 0) {
569                 printk(KERN_ERR "Failed to register WM8523 I2C driver: %d\n",
570                        ret);
571         }
572 #endif
573         return 0;
574 }
575 module_init(wm8523_modinit);
576
577 static void __exit wm8523_exit(void)
578 {
579 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
580         i2c_del_driver(&wm8523_i2c_driver);
581 #endif
582 }
583 module_exit(wm8523_exit);
584
585 MODULE_DESCRIPTION("ASoC WM8523 driver");
586 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
587 MODULE_LICENSE("GPL");