I2C: Add helper macro for i2c_driver boilerplate
[zen-stable.git] / sound / soc / codecs / wm8776.c
blobbfdc52370ad02de96bd9cf1859614db91e682949
1 /*
2 * wm8776.c -- WM8776 ALSA SoC Audio driver
4 * Copyright 2009 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
12 * TODO: Input ALC/limiter support
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/of_device.h>
22 #include <linux/platform_device.h>
23 #include <linux/spi/spi.h>
24 #include <linux/slab.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/initval.h>
30 #include <sound/tlv.h>
32 #include "wm8776.h"
34 /* codec private data */
35 struct wm8776_priv {
36 enum snd_soc_control_type control_type;
37 int sysclk[2];
40 static const u16 wm8776_reg[WM8776_CACHEREGNUM] = {
41 0x79, 0x79, 0x79, 0xff, 0xff, /* 4 */
42 0xff, 0x00, 0x90, 0x00, 0x00, /* 9 */
43 0x22, 0x22, 0x22, 0x08, 0xcf, /* 14 */
44 0xcf, 0x7b, 0x00, 0x32, 0x00, /* 19 */
45 0xa6, 0x01, 0x01
48 static int wm8776_reset(struct snd_soc_codec *codec)
50 return snd_soc_write(codec, WM8776_RESET, 0);
53 static const DECLARE_TLV_DB_SCALE(hp_tlv, -12100, 100, 1);
54 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
55 static const DECLARE_TLV_DB_SCALE(adc_tlv, -10350, 50, 1);
57 static const struct snd_kcontrol_new wm8776_snd_controls[] = {
58 SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8776_HPLVOL, WM8776_HPRVOL,
59 0, 127, 0, hp_tlv),
60 SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8776_DACLVOL, WM8776_DACRVOL,
61 0, 255, 0, dac_tlv),
62 SOC_SINGLE("Digital Playback ZC Switch", WM8776_DACCTRL1, 0, 1, 0),
64 SOC_SINGLE("Deemphasis Switch", WM8776_DACCTRL2, 0, 1, 0),
66 SOC_DOUBLE_R_TLV("Capture Volume", WM8776_ADCLVOL, WM8776_ADCRVOL,
67 0, 255, 0, adc_tlv),
68 SOC_DOUBLE("Capture Switch", WM8776_ADCMUX, 7, 6, 1, 1),
69 SOC_DOUBLE_R("Capture ZC Switch", WM8776_ADCLVOL, WM8776_ADCRVOL, 8, 1, 0),
70 SOC_SINGLE("Capture HPF Switch", WM8776_ADCIFCTRL, 8, 1, 1),
73 static const struct snd_kcontrol_new inmix_controls[] = {
74 SOC_DAPM_SINGLE("AIN1 Switch", WM8776_ADCMUX, 0, 1, 0),
75 SOC_DAPM_SINGLE("AIN2 Switch", WM8776_ADCMUX, 1, 1, 0),
76 SOC_DAPM_SINGLE("AIN3 Switch", WM8776_ADCMUX, 2, 1, 0),
77 SOC_DAPM_SINGLE("AIN4 Switch", WM8776_ADCMUX, 3, 1, 0),
78 SOC_DAPM_SINGLE("AIN5 Switch", WM8776_ADCMUX, 4, 1, 0),
81 static const struct snd_kcontrol_new outmix_controls[] = {
82 SOC_DAPM_SINGLE("DAC Switch", WM8776_OUTMUX, 0, 1, 0),
83 SOC_DAPM_SINGLE("AUX Switch", WM8776_OUTMUX, 1, 1, 0),
84 SOC_DAPM_SINGLE("Bypass Switch", WM8776_OUTMUX, 2, 1, 0),
87 static const struct snd_soc_dapm_widget wm8776_dapm_widgets[] = {
88 SND_SOC_DAPM_INPUT("AUX"),
90 SND_SOC_DAPM_INPUT("AIN1"),
91 SND_SOC_DAPM_INPUT("AIN2"),
92 SND_SOC_DAPM_INPUT("AIN3"),
93 SND_SOC_DAPM_INPUT("AIN4"),
94 SND_SOC_DAPM_INPUT("AIN5"),
96 SND_SOC_DAPM_MIXER("Input Mixer", WM8776_PWRDOWN, 6, 1,
97 inmix_controls, ARRAY_SIZE(inmix_controls)),
99 SND_SOC_DAPM_ADC("ADC", "Capture", WM8776_PWRDOWN, 1, 1),
100 SND_SOC_DAPM_DAC("DAC", "Playback", WM8776_PWRDOWN, 2, 1),
102 SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
103 outmix_controls, ARRAY_SIZE(outmix_controls)),
105 SND_SOC_DAPM_PGA("Headphone PGA", WM8776_PWRDOWN, 3, 1, NULL, 0),
107 SND_SOC_DAPM_OUTPUT("VOUT"),
109 SND_SOC_DAPM_OUTPUT("HPOUTL"),
110 SND_SOC_DAPM_OUTPUT("HPOUTR"),
113 static const struct snd_soc_dapm_route routes[] = {
114 { "Input Mixer", "AIN1 Switch", "AIN1" },
115 { "Input Mixer", "AIN2 Switch", "AIN2" },
116 { "Input Mixer", "AIN3 Switch", "AIN3" },
117 { "Input Mixer", "AIN4 Switch", "AIN4" },
118 { "Input Mixer", "AIN5 Switch", "AIN5" },
120 { "ADC", NULL, "Input Mixer" },
122 { "Output Mixer", "DAC Switch", "DAC" },
123 { "Output Mixer", "AUX Switch", "AUX" },
124 { "Output Mixer", "Bypass Switch", "Input Mixer" },
126 { "VOUT", NULL, "Output Mixer" },
128 { "Headphone PGA", NULL, "Output Mixer" },
130 { "HPOUTL", NULL, "Headphone PGA" },
131 { "HPOUTR", NULL, "Headphone PGA" },
134 static int wm8776_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
136 struct snd_soc_codec *codec = dai->codec;
137 int reg, iface, master;
139 switch (dai->driver->id) {
140 case WM8776_DAI_DAC:
141 reg = WM8776_DACIFCTRL;
142 master = 0x80;
143 break;
144 case WM8776_DAI_ADC:
145 reg = WM8776_ADCIFCTRL;
146 master = 0x100;
147 break;
148 default:
149 return -EINVAL;
152 iface = 0;
154 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
155 case SND_SOC_DAIFMT_CBM_CFM:
156 break;
157 case SND_SOC_DAIFMT_CBS_CFS:
158 master = 0;
159 break;
160 default:
161 return -EINVAL;
164 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
165 case SND_SOC_DAIFMT_I2S:
166 iface |= 0x0002;
167 break;
168 case SND_SOC_DAIFMT_RIGHT_J:
169 break;
170 case SND_SOC_DAIFMT_LEFT_J:
171 iface |= 0x0001;
172 break;
173 default:
174 return -EINVAL;
177 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
178 case SND_SOC_DAIFMT_NB_NF:
179 break;
180 case SND_SOC_DAIFMT_IB_IF:
181 iface |= 0x00c;
182 break;
183 case SND_SOC_DAIFMT_IB_NF:
184 iface |= 0x008;
185 break;
186 case SND_SOC_DAIFMT_NB_IF:
187 iface |= 0x004;
188 break;
189 default:
190 return -EINVAL;
193 /* Finally, write out the values */
194 snd_soc_update_bits(codec, reg, 0xf, iface);
195 snd_soc_update_bits(codec, WM8776_MSTRCTRL, 0x180, master);
197 return 0;
200 static int mclk_ratios[] = {
201 128,
202 192,
203 256,
204 384,
205 512,
206 768,
209 static int wm8776_hw_params(struct snd_pcm_substream *substream,
210 struct snd_pcm_hw_params *params,
211 struct snd_soc_dai *dai)
213 struct snd_soc_codec *codec = dai->codec;
214 struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
215 int iface_reg, iface;
216 int ratio_shift, master;
217 int i;
219 switch (dai->driver->id) {
220 case WM8776_DAI_DAC:
221 iface_reg = WM8776_DACIFCTRL;
222 master = 0x80;
223 ratio_shift = 4;
224 break;
225 case WM8776_DAI_ADC:
226 iface_reg = WM8776_ADCIFCTRL;
227 master = 0x100;
228 ratio_shift = 0;
229 break;
230 default:
231 return -EINVAL;
234 /* Set word length */
235 switch (snd_pcm_format_width(params_format(params))) {
236 case 16:
237 iface = 0;
238 case 20:
239 iface = 0x10;
240 break;
241 case 24:
242 iface = 0x20;
243 break;
244 case 32:
245 iface = 0x30;
246 break;
247 default:
248 dev_err(codec->dev, "Unsupported sample size: %i\n",
249 snd_pcm_format_width(params_format(params)));
250 return -EINVAL;
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;
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;
268 dev_dbg(codec->dev, "MCLK is %dfs\n", mclk_ratios[i]);
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");
276 snd_soc_update_bits(codec, iface_reg, 0x30, iface);
278 return 0;
281 static int wm8776_mute(struct snd_soc_dai *dai, int mute)
283 struct snd_soc_codec *codec = dai->codec;
285 return snd_soc_write(codec, WM8776_DACMUTE, !!mute);
288 static int wm8776_set_sysclk(struct snd_soc_dai *dai,
289 int clk_id, unsigned int freq, int dir)
291 struct snd_soc_codec *codec = dai->codec;
292 struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
294 BUG_ON(dai->driver->id >= ARRAY_SIZE(wm8776->sysclk));
296 wm8776->sysclk[dai->driver->id] = freq;
298 return 0;
301 static int wm8776_set_bias_level(struct snd_soc_codec *codec,
302 enum snd_soc_bias_level level)
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->dapm.bias_level == SND_SOC_BIAS_OFF) {
311 snd_soc_cache_sync(codec);
313 /* Disable the global powerdown; DAPM does the rest */
314 snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 0);
317 break;
318 case SND_SOC_BIAS_OFF:
319 snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 1);
320 break;
323 codec->dapm.bias_level = level;
324 return 0;
327 #define WM8776_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
328 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
330 static struct snd_soc_dai_ops wm8776_dac_ops = {
331 .digital_mute = wm8776_mute,
332 .hw_params = wm8776_hw_params,
333 .set_fmt = wm8776_set_fmt,
334 .set_sysclk = wm8776_set_sysclk,
337 static struct snd_soc_dai_ops wm8776_adc_ops = {
338 .hw_params = wm8776_hw_params,
339 .set_fmt = wm8776_set_fmt,
340 .set_sysclk = wm8776_set_sysclk,
343 static struct snd_soc_dai_driver wm8776_dai[] = {
345 .name = "wm8776-hifi-playback",
346 .id = WM8776_DAI_DAC,
347 .playback = {
348 .stream_name = "Playback",
349 .channels_min = 2,
350 .channels_max = 2,
351 .rates = SNDRV_PCM_RATE_CONTINUOUS,
352 .rate_min = 32000,
353 .rate_max = 192000,
354 .formats = WM8776_FORMATS,
356 .ops = &wm8776_dac_ops,
359 .name = "wm8776-hifi-capture",
360 .id = WM8776_DAI_ADC,
361 .capture = {
362 .stream_name = "Capture",
363 .channels_min = 2,
364 .channels_max = 2,
365 .rates = SNDRV_PCM_RATE_CONTINUOUS,
366 .rate_min = 32000,
367 .rate_max = 96000,
368 .formats = WM8776_FORMATS,
370 .ops = &wm8776_adc_ops,
374 #ifdef CONFIG_PM
375 static int wm8776_suspend(struct snd_soc_codec *codec, pm_message_t state)
377 wm8776_set_bias_level(codec, SND_SOC_BIAS_OFF);
379 return 0;
382 static int wm8776_resume(struct snd_soc_codec *codec)
384 wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
385 return 0;
387 #else
388 #define wm8776_suspend NULL
389 #define wm8776_resume NULL
390 #endif
392 static int wm8776_probe(struct snd_soc_codec *codec)
394 struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
395 struct snd_soc_dapm_context *dapm = &codec->dapm;
396 int ret = 0;
398 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8776->control_type);
399 if (ret < 0) {
400 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
401 return ret;
404 ret = wm8776_reset(codec);
405 if (ret < 0) {
406 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
407 return ret;
410 wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
412 /* Latch the update bits; right channel only since we always
413 * update both. */
414 snd_soc_update_bits(codec, WM8776_HPRVOL, 0x100, 0x100);
415 snd_soc_update_bits(codec, WM8776_DACRVOL, 0x100, 0x100);
417 snd_soc_add_controls(codec, wm8776_snd_controls,
418 ARRAY_SIZE(wm8776_snd_controls));
419 snd_soc_dapm_new_controls(dapm, wm8776_dapm_widgets,
420 ARRAY_SIZE(wm8776_dapm_widgets));
421 snd_soc_dapm_add_routes(dapm, routes, ARRAY_SIZE(routes));
423 return ret;
426 /* power down chip */
427 static int wm8776_remove(struct snd_soc_codec *codec)
429 wm8776_set_bias_level(codec, SND_SOC_BIAS_OFF);
430 return 0;
433 static struct snd_soc_codec_driver soc_codec_dev_wm8776 = {
434 .probe = wm8776_probe,
435 .remove = wm8776_remove,
436 .suspend = wm8776_suspend,
437 .resume = wm8776_resume,
438 .set_bias_level = wm8776_set_bias_level,
439 .reg_cache_size = ARRAY_SIZE(wm8776_reg),
440 .reg_word_size = sizeof(u16),
441 .reg_cache_default = wm8776_reg,
444 static const struct of_device_id wm8776_of_match[] = {
445 { .compatible = "wlf,wm8776", },
448 MODULE_DEVICE_TABLE(of, wm8776_of_match);
450 #if defined(CONFIG_SPI_MASTER)
451 static int __devinit wm8776_spi_probe(struct spi_device *spi)
453 struct wm8776_priv *wm8776;
454 int ret;
456 wm8776 = kzalloc(sizeof(struct wm8776_priv), GFP_KERNEL);
457 if (wm8776 == NULL)
458 return -ENOMEM;
460 wm8776->control_type = SND_SOC_SPI;
461 spi_set_drvdata(spi, wm8776);
463 ret = snd_soc_register_codec(&spi->dev,
464 &soc_codec_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
465 if (ret < 0)
466 kfree(wm8776);
467 return ret;
470 static int __devexit wm8776_spi_remove(struct spi_device *spi)
472 snd_soc_unregister_codec(&spi->dev);
473 kfree(spi_get_drvdata(spi));
474 return 0;
477 static struct spi_driver wm8776_spi_driver = {
478 .driver = {
479 .name = "wm8776",
480 .owner = THIS_MODULE,
481 .of_match_table = wm8776_of_match,
483 .probe = wm8776_spi_probe,
484 .remove = __devexit_p(wm8776_spi_remove),
486 #endif /* CONFIG_SPI_MASTER */
488 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
489 static __devinit int wm8776_i2c_probe(struct i2c_client *i2c,
490 const struct i2c_device_id *id)
492 struct wm8776_priv *wm8776;
493 int ret;
495 wm8776 = kzalloc(sizeof(struct wm8776_priv), GFP_KERNEL);
496 if (wm8776 == NULL)
497 return -ENOMEM;
499 i2c_set_clientdata(i2c, wm8776);
500 wm8776->control_type = SND_SOC_I2C;
502 ret = snd_soc_register_codec(&i2c->dev,
503 &soc_codec_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
504 if (ret < 0)
505 kfree(wm8776);
506 return ret;
509 static __devexit int wm8776_i2c_remove(struct i2c_client *client)
511 snd_soc_unregister_codec(&client->dev);
512 kfree(i2c_get_clientdata(client));
513 return 0;
516 static const struct i2c_device_id wm8776_i2c_id[] = {
517 { "wm8776", 0 },
520 MODULE_DEVICE_TABLE(i2c, wm8776_i2c_id);
522 static struct i2c_driver wm8776_i2c_driver = {
523 .driver = {
524 .name = "wm8776",
525 .owner = THIS_MODULE,
526 .of_match_table = wm8776_of_match,
528 .probe = wm8776_i2c_probe,
529 .remove = __devexit_p(wm8776_i2c_remove),
530 .id_table = wm8776_i2c_id,
532 #endif
534 static int __init wm8776_modinit(void)
536 int ret = 0;
537 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
538 ret = i2c_add_driver(&wm8776_i2c_driver);
539 if (ret != 0) {
540 printk(KERN_ERR "Failed to register wm8776 I2C driver: %d\n",
541 ret);
543 #endif
544 #if defined(CONFIG_SPI_MASTER)
545 ret = spi_register_driver(&wm8776_spi_driver);
546 if (ret != 0) {
547 printk(KERN_ERR "Failed to register wm8776 SPI driver: %d\n",
548 ret);
550 #endif
551 return ret;
553 module_init(wm8776_modinit);
555 static void __exit wm8776_exit(void)
557 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
558 i2c_del_driver(&wm8776_i2c_driver);
559 #endif
560 #if defined(CONFIG_SPI_MASTER)
561 spi_unregister_driver(&wm8776_spi_driver);
562 #endif
564 module_exit(wm8776_exit);
566 MODULE_DESCRIPTION("ASoC WM8776 driver");
567 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
568 MODULE_LICENSE("GPL");