d3318886f43e0f51faba11cacd836f0869b1e5d4
[linux-2.6-microblaze.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         struct snd_soc_dapm_context *dapm = &codec->dapm;
114
115         snd_soc_dapm_new_controls(dapm, wm8523_dapm_widgets,
116                                   ARRAY_SIZE(wm8523_dapm_widgets));
117         snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
118
119         return 0;
120 }
121
122 static struct {
123         int value;
124         int ratio;
125 } lrclk_ratios[WM8523_NUM_RATES] = {
126         { 1, 128 },
127         { 2, 192 },
128         { 3, 256 },
129         { 4, 384 },
130         { 5, 512 },
131         { 6, 768 },
132         { 7, 1152 },
133 };
134
135 static int wm8523_startup(struct snd_pcm_substream *substream,
136                           struct snd_soc_dai *dai)
137 {
138         struct snd_soc_codec *codec = dai->codec;
139         struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
140
141         /* The set of sample rates that can be supported depends on the
142          * MCLK supplied to the CODEC - enforce this.
143          */
144         if (!wm8523->sysclk) {
145                 dev_err(codec->dev,
146                         "No MCLK configured, call set_sysclk() on init\n");
147                 return -EINVAL;
148         }
149
150         return 0;
151         snd_pcm_hw_constraint_list(substream->runtime, 0,
152                                    SNDRV_PCM_HW_PARAM_RATE,
153                                    &wm8523->rate_constraint);
154
155         return 0;
156 }
157
158 static int wm8523_hw_params(struct snd_pcm_substream *substream,
159                             struct snd_pcm_hw_params *params,
160                             struct snd_soc_dai *dai)
161 {
162         struct snd_soc_pcm_runtime *rtd = substream->private_data;
163         struct snd_soc_codec *codec = rtd->codec;
164         struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
165         int i;
166         u16 aifctrl1 = snd_soc_read(codec, WM8523_AIF_CTRL1);
167         u16 aifctrl2 = snd_soc_read(codec, WM8523_AIF_CTRL2);
168
169         /* Find a supported LRCLK ratio */
170         for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
171                 if (wm8523->sysclk / params_rate(params) ==
172                     lrclk_ratios[i].ratio)
173                         break;
174         }
175
176         /* Should never happen, should be handled by constraints */
177         if (i == ARRAY_SIZE(lrclk_ratios)) {
178                 dev_err(codec->dev, "MCLK/fs ratio %d unsupported\n",
179                         wm8523->sysclk / params_rate(params));
180                 return -EINVAL;
181         }
182
183         aifctrl2 &= ~WM8523_SR_MASK;
184         aifctrl2 |= lrclk_ratios[i].value;
185
186         aifctrl1 &= ~WM8523_WL_MASK;
187         switch (params_format(params)) {
188         case SNDRV_PCM_FORMAT_S16_LE:
189                 break;
190         case SNDRV_PCM_FORMAT_S20_3LE:
191                 aifctrl1 |= 0x8;
192                 break;
193         case SNDRV_PCM_FORMAT_S24_LE:
194                 aifctrl1 |= 0x10;
195                 break;
196         case SNDRV_PCM_FORMAT_S32_LE:
197                 aifctrl1 |= 0x18;
198                 break;
199         }
200
201         snd_soc_write(codec, WM8523_AIF_CTRL1, aifctrl1);
202         snd_soc_write(codec, WM8523_AIF_CTRL2, aifctrl2);
203
204         return 0;
205 }
206
207 static int wm8523_set_dai_sysclk(struct snd_soc_dai *codec_dai,
208                 int clk_id, unsigned int freq, int dir)
209 {
210         struct snd_soc_codec *codec = codec_dai->codec;
211         struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
212         unsigned int val;
213         int i;
214
215         wm8523->sysclk = freq;
216
217         wm8523->rate_constraint.count = 0;
218         for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
219                 val = freq / lrclk_ratios[i].ratio;
220                 /* Check that it's a standard rate since core can't
221                  * cope with others and having the odd rates confuses
222                  * constraint matching.
223                  */
224                 switch (val) {
225                 case 8000:
226                 case 11025:
227                 case 16000:
228                 case 22050:
229                 case 32000:
230                 case 44100:
231                 case 48000:
232                 case 64000:
233                 case 88200:
234                 case 96000:
235                 case 176400:
236                 case 192000:
237                         dev_dbg(codec->dev, "Supported sample rate: %dHz\n",
238                                 val);
239                         wm8523->rate_constraint_list[i] = val;
240                         wm8523->rate_constraint.count++;
241                         break;
242                 default:
243                         dev_dbg(codec->dev, "Skipping sample rate: %dHz\n",
244                                 val);
245                 }
246         }
247
248         /* Need at least one supported rate... */
249         if (wm8523->rate_constraint.count == 0)
250                 return -EINVAL;
251
252         return 0;
253 }
254
255
256 static int wm8523_set_dai_fmt(struct snd_soc_dai *codec_dai,
257                 unsigned int fmt)
258 {
259         struct snd_soc_codec *codec = codec_dai->codec;
260         u16 aifctrl1 = snd_soc_read(codec, WM8523_AIF_CTRL1);
261
262         aifctrl1 &= ~(WM8523_BCLK_INV_MASK | WM8523_LRCLK_INV_MASK |
263                       WM8523_FMT_MASK | WM8523_AIF_MSTR_MASK);
264
265         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
266         case SND_SOC_DAIFMT_CBM_CFM:
267                 aifctrl1 |= WM8523_AIF_MSTR;
268                 break;
269         case SND_SOC_DAIFMT_CBS_CFS:
270                 break;
271         default:
272                 return -EINVAL;
273         }
274
275         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
276         case SND_SOC_DAIFMT_I2S:
277                 aifctrl1 |= 0x0002;
278                 break;
279         case SND_SOC_DAIFMT_RIGHT_J:
280                 break;
281         case SND_SOC_DAIFMT_LEFT_J:
282                 aifctrl1 |= 0x0001;
283                 break;
284         case SND_SOC_DAIFMT_DSP_A:
285                 aifctrl1 |= 0x0003;
286                 break;
287         case SND_SOC_DAIFMT_DSP_B:
288                 aifctrl1 |= 0x0023;
289                 break;
290         default:
291                 return -EINVAL;
292         }
293
294         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
295         case SND_SOC_DAIFMT_NB_NF:
296                 break;
297         case SND_SOC_DAIFMT_IB_IF:
298                 aifctrl1 |= WM8523_BCLK_INV | WM8523_LRCLK_INV;
299                 break;
300         case SND_SOC_DAIFMT_IB_NF:
301                 aifctrl1 |= WM8523_BCLK_INV;
302                 break;
303         case SND_SOC_DAIFMT_NB_IF:
304                 aifctrl1 |= WM8523_LRCLK_INV;
305                 break;
306         default:
307                 return -EINVAL;
308         }
309
310         snd_soc_write(codec, WM8523_AIF_CTRL1, aifctrl1);
311
312         return 0;
313 }
314
315 static int wm8523_set_bias_level(struct snd_soc_codec *codec,
316                                  enum snd_soc_bias_level level)
317 {
318         struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
319         int ret, i;
320
321         switch (level) {
322         case SND_SOC_BIAS_ON:
323                 break;
324
325         case SND_SOC_BIAS_PREPARE:
326                 /* Full power on */
327                 snd_soc_update_bits(codec, WM8523_PSCTRL1,
328                                     WM8523_SYS_ENA_MASK, 3);
329                 break;
330
331         case SND_SOC_BIAS_STANDBY:
332                 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
333                         ret = regulator_bulk_enable(ARRAY_SIZE(wm8523->supplies),
334                                                     wm8523->supplies);
335                         if (ret != 0) {
336                                 dev_err(codec->dev,
337                                         "Failed to enable supplies: %d\n",
338                                         ret);
339                                 return ret;
340                         }
341
342                         /* Initial power up */
343                         snd_soc_update_bits(codec, WM8523_PSCTRL1,
344                                             WM8523_SYS_ENA_MASK, 1);
345
346                         /* Sync back default/cached values */
347                         for (i = WM8523_AIF_CTRL1;
348                              i < WM8523_MAX_REGISTER; i++)
349                                 snd_soc_write(codec, i, wm8523->reg_cache[i]);
350
351
352                         msleep(100);
353                 }
354
355                 /* Power up to mute */
356                 snd_soc_update_bits(codec, WM8523_PSCTRL1,
357                                     WM8523_SYS_ENA_MASK, 2);
358
359                 break;
360
361         case SND_SOC_BIAS_OFF:
362                 /* The chip runs through the power down sequence for us. */
363                 snd_soc_update_bits(codec, WM8523_PSCTRL1,
364                                     WM8523_SYS_ENA_MASK, 0);
365                 msleep(100);
366
367                 regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies),
368                                        wm8523->supplies);
369                 break;
370         }
371         codec->dapm.bias_level = level;
372         return 0;
373 }
374
375 #define WM8523_RATES SNDRV_PCM_RATE_8000_192000
376
377 #define WM8523_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
378                         SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
379
380 static struct snd_soc_dai_ops wm8523_dai_ops = {
381         .startup        = wm8523_startup,
382         .hw_params      = wm8523_hw_params,
383         .set_sysclk     = wm8523_set_dai_sysclk,
384         .set_fmt        = wm8523_set_dai_fmt,
385 };
386
387 static struct snd_soc_dai_driver wm8523_dai = {
388         .name = "wm8523-hifi",
389         .playback = {
390                 .stream_name = "Playback",
391                 .channels_min = 2,  /* Mono modes not yet supported */
392                 .channels_max = 2,
393                 .rates = WM8523_RATES,
394                 .formats = WM8523_FORMATS,
395         },
396         .ops = &wm8523_dai_ops,
397 };
398
399 #ifdef CONFIG_PM
400 static int wm8523_suspend(struct snd_soc_codec *codec, pm_message_t state)
401 {
402         wm8523_set_bias_level(codec, SND_SOC_BIAS_OFF);
403         return 0;
404 }
405
406 static int wm8523_resume(struct snd_soc_codec *codec)
407 {
408         wm8523_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
409         return 0;
410 }
411 #else
412 #define wm8523_suspend NULL
413 #define wm8523_resume NULL
414 #endif
415
416 static int wm8523_probe(struct snd_soc_codec *codec)
417 {
418         struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
419         int ret, i;
420
421         codec->hw_write = (hw_write_t)i2c_master_send;
422         wm8523->rate_constraint.list = &wm8523->rate_constraint_list[0];
423         wm8523->rate_constraint.count =
424                 ARRAY_SIZE(wm8523->rate_constraint_list);
425
426         ret = snd_soc_codec_set_cache_io(codec, 8, 16, wm8523->control_type);
427         if (ret != 0) {
428                 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
429                 return ret;
430         }
431
432         for (i = 0; i < ARRAY_SIZE(wm8523->supplies); i++)
433                 wm8523->supplies[i].supply = wm8523_supply_names[i];
434
435         ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8523->supplies),
436                                  wm8523->supplies);
437         if (ret != 0) {
438                 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
439                 return ret;
440         }
441
442         ret = regulator_bulk_enable(ARRAY_SIZE(wm8523->supplies),
443                                     wm8523->supplies);
444         if (ret != 0) {
445                 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
446                 goto err_get;
447         }
448
449         ret = snd_soc_read(codec, WM8523_DEVICE_ID);
450         if (ret < 0) {
451                 dev_err(codec->dev, "Failed to read ID register\n");
452                 goto err_enable;
453         }
454         if (ret != wm8523_reg[WM8523_DEVICE_ID]) {
455                 dev_err(codec->dev, "Device is not a WM8523, ID is %x\n", ret);
456                 ret = -EINVAL;
457                 goto err_enable;
458         }
459
460         ret = snd_soc_read(codec, WM8523_REVISION);
461         if (ret < 0) {
462                 dev_err(codec->dev, "Failed to read revision register\n");
463                 goto err_enable;
464         }
465         dev_info(codec->dev, "revision %c\n",
466                  (ret & WM8523_CHIP_REV_MASK) + 'A');
467
468         ret = wm8523_reset(codec);
469         if (ret < 0) {
470                 dev_err(codec->dev, "Failed to issue reset\n");
471                 goto err_enable;
472         }
473
474         /* Change some default settings - latch VU and enable ZC */
475         wm8523->reg_cache[WM8523_DAC_GAINR] |= WM8523_DACR_VU;
476         wm8523->reg_cache[WM8523_DAC_CTRL3] |= WM8523_ZC;
477
478         wm8523_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
479
480         /* Bias level configuration will have done an extra enable */
481         regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
482
483         snd_soc_add_controls(codec, wm8523_snd_controls,
484                              ARRAY_SIZE(wm8523_snd_controls));
485         wm8523_add_widgets(codec);
486
487         return 0;
488
489 err_enable:
490         regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
491 err_get:
492         regulator_bulk_free(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
493
494         return ret;
495 }
496
497 static int wm8523_remove(struct snd_soc_codec *codec)
498 {
499         struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
500
501         wm8523_set_bias_level(codec, SND_SOC_BIAS_OFF);
502         regulator_bulk_free(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
503         return 0;
504 }
505
506 static struct snd_soc_codec_driver soc_codec_dev_wm8523 = {
507         .probe =        wm8523_probe,
508         .remove =       wm8523_remove,
509         .suspend =      wm8523_suspend,
510         .resume =       wm8523_resume,
511         .set_bias_level = wm8523_set_bias_level,
512         .reg_cache_size = WM8523_REGISTER_COUNT,
513         .reg_word_size = sizeof(u16),
514         .reg_cache_default = wm8523_reg,
515         .volatile_register = wm8523_volatile_register,
516 };
517
518 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
519 static __devinit int wm8523_i2c_probe(struct i2c_client *i2c,
520                                       const struct i2c_device_id *id)
521 {
522         struct wm8523_priv *wm8523;
523         int ret;
524
525         wm8523 = kzalloc(sizeof(struct wm8523_priv), GFP_KERNEL);
526         if (wm8523 == NULL)
527                 return -ENOMEM;
528
529         i2c_set_clientdata(i2c, wm8523);
530         wm8523->control_type = SND_SOC_I2C;
531
532         ret =  snd_soc_register_codec(&i2c->dev,
533                         &soc_codec_dev_wm8523, &wm8523_dai, 1);
534         if (ret < 0)
535                 kfree(wm8523);
536         return ret;
537
538 }
539
540 static __devexit int wm8523_i2c_remove(struct i2c_client *client)
541 {
542         snd_soc_unregister_codec(&client->dev);
543         kfree(i2c_get_clientdata(client));
544         return 0;
545 }
546
547 static const struct i2c_device_id wm8523_i2c_id[] = {
548         { "wm8523", 0 },
549         { }
550 };
551 MODULE_DEVICE_TABLE(i2c, wm8523_i2c_id);
552
553 static struct i2c_driver wm8523_i2c_driver = {
554         .driver = {
555                 .name = "wm8523-codec",
556                 .owner = THIS_MODULE,
557         },
558         .probe =    wm8523_i2c_probe,
559         .remove =   __devexit_p(wm8523_i2c_remove),
560         .id_table = wm8523_i2c_id,
561 };
562 #endif
563
564 static int __init wm8523_modinit(void)
565 {
566         int ret;
567 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
568         ret = i2c_add_driver(&wm8523_i2c_driver);
569         if (ret != 0) {
570                 printk(KERN_ERR "Failed to register WM8523 I2C driver: %d\n",
571                        ret);
572         }
573 #endif
574         return 0;
575 }
576 module_init(wm8523_modinit);
577
578 static void __exit wm8523_exit(void)
579 {
580 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
581         i2c_del_driver(&wm8523_i2c_driver);
582 #endif
583 }
584 module_exit(wm8523_exit);
585
586 MODULE_DESCRIPTION("ASoC WM8523 driver");
587 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
588 MODULE_LICENSE("GPL");