04182c464e35ffeffb069ad06242dca74da52248
[linux-2.6.git] / sound / soc / codecs / wm8776.c
1 /*
2  * wm8776.c  --  WM8776 ALSA SoC Audio driver
3  *
4  * Copyright 2009 Wolfson Microelectronics plc
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * TODO: Input ALC/limiter support
13  */
14
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
19 #include <linux/pm.h>
20 #include <linux/i2c.h>
21 #include <linux/platform_device.h>
22 #include <linux/spi/spi.h>
23 #include <linux/slab.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 #include <sound/soc-dapm.h>
29 #include <sound/initval.h>
30 #include <sound/tlv.h>
31
32 #include "wm8776.h"
33
34 /* codec private data */
35 struct wm8776_priv {
36         enum snd_soc_control_type control_type;
37         u16 reg_cache[WM8776_CACHEREGNUM];
38         int sysclk[2];
39 };
40
41 static const u16 wm8776_reg[WM8776_CACHEREGNUM] = {
42         0x79, 0x79, 0x79, 0xff, 0xff,  /* 4 */
43         0xff, 0x00, 0x90, 0x00, 0x00,  /* 9 */
44         0x22, 0x22, 0x22, 0x08, 0xcf,  /* 14 */
45         0xcf, 0x7b, 0x00, 0x32, 0x00,  /* 19 */
46         0xa6, 0x01, 0x01
47 };
48
49 static int wm8776_reset(struct snd_soc_codec *codec)
50 {
51         return snd_soc_write(codec, WM8776_RESET, 0);
52 }
53
54 static const DECLARE_TLV_DB_SCALE(hp_tlv, -12100, 100, 1);
55 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
56 static const DECLARE_TLV_DB_SCALE(adc_tlv, -10350, 50, 1);
57
58 static const struct snd_kcontrol_new wm8776_snd_controls[] = {
59 SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8776_HPLVOL, WM8776_HPRVOL,
60                  0, 127, 0, hp_tlv),
61 SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8776_DACLVOL, WM8776_DACRVOL,
62                  0, 255, 0, dac_tlv),
63 SOC_SINGLE("Digital Playback ZC Switch", WM8776_DACCTRL1, 0, 1, 0),
64
65 SOC_SINGLE("Deemphasis Switch", WM8776_DACCTRL2, 0, 1, 0),
66
67 SOC_DOUBLE_R_TLV("Capture Volume", WM8776_ADCLVOL, WM8776_ADCRVOL,
68                  0, 255, 0, adc_tlv),
69 SOC_DOUBLE("Capture Switch", WM8776_ADCMUX, 7, 6, 1, 1),
70 SOC_DOUBLE_R("Capture ZC Switch", WM8776_ADCLVOL, WM8776_ADCRVOL, 8, 1, 0),
71 SOC_SINGLE("Capture HPF Switch", WM8776_ADCIFCTRL, 8, 1, 1),
72 };
73
74 static const struct snd_kcontrol_new inmix_controls[] = {
75 SOC_DAPM_SINGLE("AIN1 Switch", WM8776_ADCMUX, 0, 1, 0),
76 SOC_DAPM_SINGLE("AIN2 Switch", WM8776_ADCMUX, 1, 1, 0),
77 SOC_DAPM_SINGLE("AIN3 Switch", WM8776_ADCMUX, 2, 1, 0),
78 SOC_DAPM_SINGLE("AIN4 Switch", WM8776_ADCMUX, 3, 1, 0),
79 SOC_DAPM_SINGLE("AIN5 Switch", WM8776_ADCMUX, 4, 1, 0),
80 };
81
82 static const struct snd_kcontrol_new outmix_controls[] = {
83 SOC_DAPM_SINGLE("DAC Switch", WM8776_OUTMUX, 0, 1, 0),
84 SOC_DAPM_SINGLE("AUX Switch", WM8776_OUTMUX, 1, 1, 0),
85 SOC_DAPM_SINGLE("Bypass Switch", WM8776_OUTMUX, 2, 1, 0),
86 };
87
88 static const struct snd_soc_dapm_widget wm8776_dapm_widgets[] = {
89 SND_SOC_DAPM_INPUT("AUX"),
90
91 SND_SOC_DAPM_INPUT("AIN1"),
92 SND_SOC_DAPM_INPUT("AIN2"),
93 SND_SOC_DAPM_INPUT("AIN3"),
94 SND_SOC_DAPM_INPUT("AIN4"),
95 SND_SOC_DAPM_INPUT("AIN5"),
96
97 SND_SOC_DAPM_MIXER("Input Mixer", WM8776_PWRDOWN, 6, 1,
98                    inmix_controls, ARRAY_SIZE(inmix_controls)),
99
100 SND_SOC_DAPM_ADC("ADC", "Capture", WM8776_PWRDOWN, 1, 1),
101 SND_SOC_DAPM_DAC("DAC", "Playback", WM8776_PWRDOWN, 2, 1),
102
103 SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
104                    outmix_controls, ARRAY_SIZE(outmix_controls)),
105
106 SND_SOC_DAPM_PGA("Headphone PGA", WM8776_PWRDOWN, 3, 1, NULL, 0),
107
108 SND_SOC_DAPM_OUTPUT("VOUT"),
109
110 SND_SOC_DAPM_OUTPUT("HPOUTL"),
111 SND_SOC_DAPM_OUTPUT("HPOUTR"),
112 };
113
114 static const struct snd_soc_dapm_route routes[] = {
115         { "Input Mixer", "AIN1 Switch", "AIN1" },
116         { "Input Mixer", "AIN2 Switch", "AIN2" },
117         { "Input Mixer", "AIN3 Switch", "AIN3" },
118         { "Input Mixer", "AIN4 Switch", "AIN4" },
119         { "Input Mixer", "AIN5 Switch", "AIN5" },
120
121         { "ADC", NULL, "Input Mixer" },
122
123         { "Output Mixer", "DAC Switch", "DAC" },
124         { "Output Mixer", "AUX Switch", "AUX" },
125         { "Output Mixer", "Bypass Switch", "Input Mixer" },
126
127         { "VOUT", NULL, "Output Mixer" },
128
129         { "Headphone PGA", NULL, "Output Mixer" },
130
131         { "HPOUTL", NULL, "Headphone PGA" },
132         { "HPOUTR", NULL, "Headphone PGA" },
133 };
134
135 static int wm8776_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
136 {
137         struct snd_soc_codec *codec = dai->codec;
138         int reg, iface, master;
139
140         switch (dai->driver->id) {
141         case WM8776_DAI_DAC:
142                 reg = WM8776_DACIFCTRL;
143                 master = 0x80;
144                 break;
145         case WM8776_DAI_ADC:
146                 reg = WM8776_ADCIFCTRL;
147                 master = 0x100;
148                 break;
149         default:
150                 return -EINVAL;
151         }
152
153         iface = 0;
154
155         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
156         case SND_SOC_DAIFMT_CBM_CFM:
157                 break;
158         case SND_SOC_DAIFMT_CBS_CFS:
159                 master = 0;
160                 break;
161         default:
162                 return -EINVAL;
163         }
164
165         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
166         case SND_SOC_DAIFMT_I2S:
167                 iface |= 0x0002;
168                 break;
169         case SND_SOC_DAIFMT_RIGHT_J:
170                 break;
171         case SND_SOC_DAIFMT_LEFT_J:
172                 iface |= 0x0001;
173                 break;
174         default:
175                 return -EINVAL;
176         }
177
178         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
179         case SND_SOC_DAIFMT_NB_NF:
180                 break;
181         case SND_SOC_DAIFMT_IB_IF:
182                 iface |= 0x00c;
183                 break;
184         case SND_SOC_DAIFMT_IB_NF:
185                 iface |= 0x008;
186                 break;
187         case SND_SOC_DAIFMT_NB_IF:
188                 iface |= 0x004;
189                 break;
190         default:
191                 return -EINVAL;
192         }
193
194         /* Finally, write out the values */
195         snd_soc_update_bits(codec, reg, 0xf, iface);
196         snd_soc_update_bits(codec, WM8776_MSTRCTRL, 0x180, master);
197
198         return 0;
199 }
200
201 static int mclk_ratios[] = {
202         128,
203         192,
204         256,
205         384,
206         512,
207         768,
208 };
209
210 static int wm8776_hw_params(struct snd_pcm_substream *substream,
211                             struct snd_pcm_hw_params *params,
212                             struct snd_soc_dai *dai)
213 {
214         struct snd_soc_codec *codec = dai->codec;
215         struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
216         int iface_reg, iface;
217         int ratio_shift, master;
218         int i;
219
220         iface = 0;
221
222         switch (dai->driver->id) {
223         case WM8776_DAI_DAC:
224                 iface_reg = WM8776_DACIFCTRL;
225                 master = 0x80;
226                 ratio_shift = 4;
227                 break;
228         case WM8776_DAI_ADC:
229                 iface_reg = WM8776_ADCIFCTRL;
230                 master = 0x100;
231                 ratio_shift = 0;
232                 break;
233         default:
234                 return -EINVAL;
235         }
236
237
238         /* Set word length */
239         switch (params_format(params)) {
240         case SNDRV_PCM_FORMAT_S16_LE:
241                 break;
242         case SNDRV_PCM_FORMAT_S20_3LE:
243                 iface |= 0x10;
244                 break;
245         case SNDRV_PCM_FORMAT_S24_LE:
246                 iface |= 0x20;
247                 break;
248         case SNDRV_PCM_FORMAT_S32_LE:
249                 iface |= 0x30;
250                 break;
251         }
252
253         /* Only need to set MCLK/LRCLK ratio if we're master */
254         if (snd_soc_read(codec, WM8776_MSTRCTRL) & master) {
255                 for (i = 0; i < ARRAY_SIZE(mclk_ratios); i++) {
256                         if (wm8776->sysclk[dai->driver->id] / params_rate(params)
257                             == mclk_ratios[i])
258                                 break;
259                 }
260
261                 if (i == ARRAY_SIZE(mclk_ratios)) {
262                         dev_err(codec->dev,
263                                 "Unable to configure MCLK ratio %d/%d\n",
264                                 wm8776->sysclk[dai->driver->id], params_rate(params));
265                         return -EINVAL;
266                 }
267
268                 dev_dbg(codec->dev, "MCLK is %dfs\n", mclk_ratios[i]);
269
270                 snd_soc_update_bits(codec, WM8776_MSTRCTRL,
271                                     0x7 << ratio_shift, i << ratio_shift);
272         } else {
273                 dev_dbg(codec->dev, "DAI in slave mode\n");
274         }
275
276         snd_soc_update_bits(codec, iface_reg, 0x30, iface);
277
278         return 0;
279 }
280
281 static int wm8776_mute(struct snd_soc_dai *dai, int mute)
282 {
283         struct snd_soc_codec *codec = dai->codec;
284
285         return snd_soc_write(codec, WM8776_DACMUTE, !!mute);
286 }
287
288 static int wm8776_set_sysclk(struct snd_soc_dai *dai,
289                              int clk_id, unsigned int freq, int dir)
290 {
291         struct snd_soc_codec *codec = dai->codec;
292         struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
293
294         BUG_ON(dai->driver->id >= ARRAY_SIZE(wm8776->sysclk));
295
296         wm8776->sysclk[dai->driver->id] = freq;
297
298         return 0;
299 }
300
301 static int wm8776_set_bias_level(struct snd_soc_codec *codec,
302                                  enum snd_soc_bias_level level)
303 {
304         switch (level) {
305         case SND_SOC_BIAS_ON:
306                 break;
307         case SND_SOC_BIAS_PREPARE:
308                 break;
309         case SND_SOC_BIAS_STANDBY:
310                 if (codec->bias_level == SND_SOC_BIAS_OFF) {
311                         /* Disable the global powerdown; DAPM does the rest */
312                         snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 0);
313                 }
314
315                 break;
316         case SND_SOC_BIAS_OFF:
317                 snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 1);
318                 break;
319         }
320
321         codec->bias_level = level;
322         return 0;
323 }
324
325 #define WM8776_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
326                       SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
327                       SNDRV_PCM_RATE_96000)
328
329
330 #define WM8776_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
331                         SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
332
333 static struct snd_soc_dai_ops wm8776_dac_ops = {
334         .digital_mute   = wm8776_mute,
335         .hw_params      = wm8776_hw_params,
336         .set_fmt        = wm8776_set_fmt,
337         .set_sysclk     = wm8776_set_sysclk,
338 };
339
340 static struct snd_soc_dai_ops wm8776_adc_ops = {
341         .hw_params      = wm8776_hw_params,
342         .set_fmt        = wm8776_set_fmt,
343         .set_sysclk     = wm8776_set_sysclk,
344 };
345
346 static struct snd_soc_dai_driver wm8776_dai[] = {
347         {
348                 .name = "wm8776-hifi-playback",
349                 .id     = WM8776_DAI_DAC,
350                 .playback = {
351                         .stream_name = "Playback",
352                         .channels_min = 2,
353                         .channels_max = 2,
354                         .rates = WM8776_RATES,
355                         .formats = WM8776_FORMATS,
356                 },
357                 .ops = &wm8776_dac_ops,
358         },
359         {
360                 .name = "wm8776-hifi-capture",
361                 .id     = WM8776_DAI_ADC,
362                 .capture = {
363                         .stream_name = "Capture",
364                         .channels_min = 2,
365                         .channels_max = 2,
366                         .rates = WM8776_RATES,
367                         .formats = WM8776_FORMATS,
368                 },
369                 .ops = &wm8776_adc_ops,
370         },
371 };
372
373 #ifdef CONFIG_PM
374 static int wm8776_suspend(struct snd_soc_codec *codec, pm_message_t state)
375 {
376         wm8776_set_bias_level(codec, SND_SOC_BIAS_OFF);
377
378         return 0;
379 }
380
381 static int wm8776_resume(struct snd_soc_codec *codec)
382 {
383         int i;
384         u8 data[2];
385         u16 *cache = codec->reg_cache;
386
387         /* Sync reg_cache with the hardware */
388         for (i = 0; i < ARRAY_SIZE(wm8776_reg); i++) {
389                 if (cache[i] == wm8776_reg[i])
390                         continue;
391                 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
392                 data[1] = cache[i] & 0x00ff;
393                 codec->hw_write(codec->control_data, data, 2);
394         }
395
396         wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
397
398         return 0;
399 }
400 #else
401 #define wm8776_suspend NULL
402 #define wm8776_resume NULL
403 #endif
404
405 static int wm8776_probe(struct snd_soc_codec *codec)
406 {
407         struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
408         int ret = 0;
409
410         ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8776->control_type);
411         if (ret < 0) {
412                 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
413                 return ret;
414         }
415
416         ret = wm8776_reset(codec);
417         if (ret < 0) {
418                 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
419                 return ret;
420         }
421
422         wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
423
424         /* Latch the update bits; right channel only since we always
425          * update both. */
426         snd_soc_update_bits(codec, WM8776_HPRVOL, 0x100, 0x100);
427         snd_soc_update_bits(codec, WM8776_DACRVOL, 0x100, 0x100);
428
429         snd_soc_add_controls(codec, wm8776_snd_controls,
430                              ARRAY_SIZE(wm8776_snd_controls));
431         snd_soc_dapm_new_controls(codec, wm8776_dapm_widgets,
432                                   ARRAY_SIZE(wm8776_dapm_widgets));
433         snd_soc_dapm_add_routes(codec, routes, ARRAY_SIZE(routes));
434
435         return ret;
436 }
437
438 /* power down chip */
439 static int wm8776_remove(struct snd_soc_codec *codec)
440 {
441         wm8776_set_bias_level(codec, SND_SOC_BIAS_OFF);
442         return 0;
443 }
444
445 static struct snd_soc_codec_driver soc_codec_dev_wm8776 = {
446         .probe =        wm8776_probe,
447         .remove =       wm8776_remove,
448         .suspend =      wm8776_suspend,
449         .resume =       wm8776_resume,
450         .set_bias_level = wm8776_set_bias_level,
451         .reg_cache_size = ARRAY_SIZE(wm8776_reg),
452         .reg_word_size = sizeof(u16),
453         .reg_cache_default = wm8776_reg,
454 };
455
456 #if defined(CONFIG_SPI_MASTER)
457 static int __devinit wm8776_spi_probe(struct spi_device *spi)
458 {
459         struct wm8776_priv *wm8776;
460         int ret;
461
462         wm8776 = kzalloc(sizeof(struct wm8776_priv), GFP_KERNEL);
463         if (wm8776 == NULL)
464                 return -ENOMEM;
465
466         wm8776->control_type = SND_SOC_SPI;
467         spi_set_drvdata(spi, wm8776);
468
469         ret = snd_soc_register_codec(&spi->dev,
470                         &soc_codec_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
471         if (ret < 0)
472                 kfree(wm8776);
473         return ret;
474 }
475
476 static int __devexit wm8776_spi_remove(struct spi_device *spi)
477 {
478         snd_soc_unregister_codec(&spi->dev);
479         kfree(spi_get_drvdata(spi));
480         return 0;
481 }
482
483 static struct spi_driver wm8776_spi_driver = {
484         .driver = {
485                 .name   = "wm8776-codec",
486                 .owner  = THIS_MODULE,
487         },
488         .probe          = wm8776_spi_probe,
489         .remove         = __devexit_p(wm8776_spi_remove),
490 };
491 #endif /* CONFIG_SPI_MASTER */
492
493 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
494 static __devinit int wm8776_i2c_probe(struct i2c_client *i2c,
495                                       const struct i2c_device_id *id)
496 {
497         struct wm8776_priv *wm8776;
498         int ret;
499
500         wm8776 = kzalloc(sizeof(struct wm8776_priv), GFP_KERNEL);
501         if (wm8776 == NULL)
502                 return -ENOMEM;
503
504         i2c_set_clientdata(i2c, wm8776);
505         wm8776->control_type = SND_SOC_I2C;
506
507         ret =  snd_soc_register_codec(&i2c->dev,
508                         &soc_codec_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
509         if (ret < 0)
510                 kfree(wm8776);
511         return ret;
512 }
513
514 static __devexit int wm8776_i2c_remove(struct i2c_client *client)
515 {
516         snd_soc_unregister_codec(&client->dev);
517         kfree(i2c_get_clientdata(client));
518         return 0;
519 }
520
521 static const struct i2c_device_id wm8776_i2c_id[] = {
522         { "wm8776", 0 },
523         { }
524 };
525 MODULE_DEVICE_TABLE(i2c, wm8776_i2c_id);
526
527 static struct i2c_driver wm8776_i2c_driver = {
528         .driver = {
529                 .name = "wm8776-codec",
530                 .owner = THIS_MODULE,
531         },
532         .probe =    wm8776_i2c_probe,
533         .remove =   __devexit_p(wm8776_i2c_remove),
534         .id_table = wm8776_i2c_id,
535 };
536 #endif
537
538 static int __init wm8776_modinit(void)
539 {
540         int ret = 0;
541 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
542         ret = i2c_add_driver(&wm8776_i2c_driver);
543         if (ret != 0) {
544                 printk(KERN_ERR "Failed to register wm8776 I2C driver: %d\n",
545                        ret);
546         }
547 #endif
548 #if defined(CONFIG_SPI_MASTER)
549         ret = spi_register_driver(&wm8776_spi_driver);
550         if (ret != 0) {
551                 printk(KERN_ERR "Failed to register wm8776 SPI driver: %d\n",
552                        ret);
553         }
554 #endif
555         return ret;
556 }
557 module_init(wm8776_modinit);
558
559 static void __exit wm8776_exit(void)
560 {
561 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
562         i2c_del_driver(&wm8776_i2c_driver);
563 #endif
564 #if defined(CONFIG_SPI_MASTER)
565         spi_unregister_driver(&wm8776_spi_driver);
566 #endif
567 }
568 module_exit(wm8776_exit);
569
570 MODULE_DESCRIPTION("ASoC WM8776 driver");
571 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
572 MODULE_LICENSE("GPL");