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>
20 #include <linux/i2c.h>
21 #include <linux/of_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/initval.h>
29 #include <sound/tlv.h>
33 /* codec private data */
35 enum snd_soc_control_type control_type;
39 static const u16 wm8776_reg[WM8776_CACHEREGNUM] = {
40 0x79, 0x79, 0x79, 0xff, 0xff, /* 4 */
41 0xff, 0x00, 0x90, 0x00, 0x00, /* 9 */
42 0x22, 0x22, 0x22, 0x08, 0xcf, /* 14 */
43 0xcf, 0x7b, 0x00, 0x32, 0x00, /* 19 */
47 static int wm8776_reset(struct snd_soc_codec *codec)
49 return snd_soc_write(codec, WM8776_RESET, 0);
52 static const DECLARE_TLV_DB_SCALE(hp_tlv, -12100, 100, 1);
53 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
54 static const DECLARE_TLV_DB_SCALE(adc_tlv, -10350, 50, 1);
56 static const struct snd_kcontrol_new wm8776_snd_controls[] = {
57 SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8776_HPLVOL, WM8776_HPRVOL,
59 SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8776_DACLVOL, WM8776_DACRVOL,
61 SOC_SINGLE("Digital Playback ZC Switch", WM8776_DACCTRL1, 0, 1, 0),
63 SOC_SINGLE("Deemphasis Switch", WM8776_DACCTRL2, 0, 1, 0),
65 SOC_DOUBLE_R_TLV("Capture Volume", WM8776_ADCLVOL, WM8776_ADCRVOL,
67 SOC_DOUBLE("Capture Switch", WM8776_ADCMUX, 7, 6, 1, 1),
68 SOC_DOUBLE_R("Capture ZC Switch", WM8776_ADCLVOL, WM8776_ADCRVOL, 8, 1, 0),
69 SOC_SINGLE("Capture HPF Switch", WM8776_ADCIFCTRL, 8, 1, 1),
72 static const struct snd_kcontrol_new inmix_controls[] = {
73 SOC_DAPM_SINGLE("AIN1 Switch", WM8776_ADCMUX, 0, 1, 0),
74 SOC_DAPM_SINGLE("AIN2 Switch", WM8776_ADCMUX, 1, 1, 0),
75 SOC_DAPM_SINGLE("AIN3 Switch", WM8776_ADCMUX, 2, 1, 0),
76 SOC_DAPM_SINGLE("AIN4 Switch", WM8776_ADCMUX, 3, 1, 0),
77 SOC_DAPM_SINGLE("AIN5 Switch", WM8776_ADCMUX, 4, 1, 0),
80 static const struct snd_kcontrol_new outmix_controls[] = {
81 SOC_DAPM_SINGLE("DAC Switch", WM8776_OUTMUX, 0, 1, 0),
82 SOC_DAPM_SINGLE("AUX Switch", WM8776_OUTMUX, 1, 1, 0),
83 SOC_DAPM_SINGLE("Bypass Switch", WM8776_OUTMUX, 2, 1, 0),
86 static const struct snd_soc_dapm_widget wm8776_dapm_widgets[] = {
87 SND_SOC_DAPM_INPUT("AUX"),
89 SND_SOC_DAPM_INPUT("AIN1"),
90 SND_SOC_DAPM_INPUT("AIN2"),
91 SND_SOC_DAPM_INPUT("AIN3"),
92 SND_SOC_DAPM_INPUT("AIN4"),
93 SND_SOC_DAPM_INPUT("AIN5"),
95 SND_SOC_DAPM_MIXER("Input Mixer", WM8776_PWRDOWN, 6, 1,
96 inmix_controls, ARRAY_SIZE(inmix_controls)),
98 SND_SOC_DAPM_ADC("ADC", "Capture", WM8776_PWRDOWN, 1, 1),
99 SND_SOC_DAPM_DAC("DAC", "Playback", WM8776_PWRDOWN, 2, 1),
101 SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
102 outmix_controls, ARRAY_SIZE(outmix_controls)),
104 SND_SOC_DAPM_PGA("Headphone PGA", WM8776_PWRDOWN, 3, 1, NULL, 0),
106 SND_SOC_DAPM_OUTPUT("VOUT"),
108 SND_SOC_DAPM_OUTPUT("HPOUTL"),
109 SND_SOC_DAPM_OUTPUT("HPOUTR"),
112 static const struct snd_soc_dapm_route routes[] = {
113 { "Input Mixer", "AIN1 Switch", "AIN1" },
114 { "Input Mixer", "AIN2 Switch", "AIN2" },
115 { "Input Mixer", "AIN3 Switch", "AIN3" },
116 { "Input Mixer", "AIN4 Switch", "AIN4" },
117 { "Input Mixer", "AIN5 Switch", "AIN5" },
119 { "ADC", NULL, "Input Mixer" },
121 { "Output Mixer", "DAC Switch", "DAC" },
122 { "Output Mixer", "AUX Switch", "AUX" },
123 { "Output Mixer", "Bypass Switch", "Input Mixer" },
125 { "VOUT", NULL, "Output Mixer" },
127 { "Headphone PGA", NULL, "Output Mixer" },
129 { "HPOUTL", NULL, "Headphone PGA" },
130 { "HPOUTR", NULL, "Headphone PGA" },
133 static int wm8776_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
135 struct snd_soc_codec *codec = dai->codec;
136 int reg, iface, master;
138 switch (dai->driver->id) {
140 reg = WM8776_DACIFCTRL;
144 reg = WM8776_ADCIFCTRL;
153 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
154 case SND_SOC_DAIFMT_CBM_CFM:
156 case SND_SOC_DAIFMT_CBS_CFS:
163 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
164 case SND_SOC_DAIFMT_I2S:
167 case SND_SOC_DAIFMT_RIGHT_J:
169 case SND_SOC_DAIFMT_LEFT_J:
176 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
177 case SND_SOC_DAIFMT_NB_NF:
179 case SND_SOC_DAIFMT_IB_IF:
182 case SND_SOC_DAIFMT_IB_NF:
185 case SND_SOC_DAIFMT_NB_IF:
192 /* Finally, write out the values */
193 snd_soc_update_bits(codec, reg, 0xf, iface);
194 snd_soc_update_bits(codec, WM8776_MSTRCTRL, 0x180, master);
199 static int mclk_ratios[] = {
208 static int wm8776_hw_params(struct snd_pcm_substream *substream,
209 struct snd_pcm_hw_params *params,
210 struct snd_soc_dai *dai)
212 struct snd_soc_codec *codec = dai->codec;
213 struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
214 int iface_reg, iface;
215 int ratio_shift, master;
218 switch (dai->driver->id) {
220 iface_reg = WM8776_DACIFCTRL;
225 iface_reg = WM8776_ADCIFCTRL;
233 /* Set word length */
234 switch (snd_pcm_format_width(params_format(params))) {
247 dev_err(codec->dev, "Unsupported sample size: %i\n",
248 snd_pcm_format_width(params_format(params)));
252 /* Only need to set MCLK/LRCLK ratio if we're master */
253 if (snd_soc_read(codec, WM8776_MSTRCTRL) & master) {
254 for (i = 0; i < ARRAY_SIZE(mclk_ratios); i++) {
255 if (wm8776->sysclk[dai->driver->id] / params_rate(params)
260 if (i == ARRAY_SIZE(mclk_ratios)) {
262 "Unable to configure MCLK ratio %d/%d\n",
263 wm8776->sysclk[dai->driver->id], params_rate(params));
267 dev_dbg(codec->dev, "MCLK is %dfs\n", mclk_ratios[i]);
269 snd_soc_update_bits(codec, WM8776_MSTRCTRL,
270 0x7 << ratio_shift, i << ratio_shift);
272 dev_dbg(codec->dev, "DAI in slave mode\n");
275 snd_soc_update_bits(codec, iface_reg, 0x30, iface);
280 static int wm8776_mute(struct snd_soc_dai *dai, int mute)
282 struct snd_soc_codec *codec = dai->codec;
284 return snd_soc_write(codec, WM8776_DACMUTE, !!mute);
287 static int wm8776_set_sysclk(struct snd_soc_dai *dai,
288 int clk_id, unsigned int freq, int dir)
290 struct snd_soc_codec *codec = dai->codec;
291 struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
293 BUG_ON(dai->driver->id >= ARRAY_SIZE(wm8776->sysclk));
295 wm8776->sysclk[dai->driver->id] = freq;
300 static int wm8776_set_bias_level(struct snd_soc_codec *codec,
301 enum snd_soc_bias_level level)
304 case SND_SOC_BIAS_ON:
306 case SND_SOC_BIAS_PREPARE:
308 case SND_SOC_BIAS_STANDBY:
309 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
310 snd_soc_cache_sync(codec);
312 /* Disable the global powerdown; DAPM does the rest */
313 snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 0);
317 case SND_SOC_BIAS_OFF:
318 snd_soc_update_bits(codec, WM8776_PWRDOWN, 1, 1);
322 codec->dapm.bias_level = level;
326 #define WM8776_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
327 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
329 static const struct snd_soc_dai_ops wm8776_dac_ops = {
330 .digital_mute = wm8776_mute,
331 .hw_params = wm8776_hw_params,
332 .set_fmt = wm8776_set_fmt,
333 .set_sysclk = wm8776_set_sysclk,
336 static const struct snd_soc_dai_ops wm8776_adc_ops = {
337 .hw_params = wm8776_hw_params,
338 .set_fmt = wm8776_set_fmt,
339 .set_sysclk = wm8776_set_sysclk,
342 static struct snd_soc_dai_driver wm8776_dai[] = {
344 .name = "wm8776-hifi-playback",
345 .id = WM8776_DAI_DAC,
347 .stream_name = "Playback",
350 .rates = SNDRV_PCM_RATE_CONTINUOUS,
353 .formats = WM8776_FORMATS,
355 .ops = &wm8776_dac_ops,
358 .name = "wm8776-hifi-capture",
359 .id = WM8776_DAI_ADC,
361 .stream_name = "Capture",
364 .rates = SNDRV_PCM_RATE_CONTINUOUS,
367 .formats = WM8776_FORMATS,
369 .ops = &wm8776_adc_ops,
374 static int wm8776_suspend(struct snd_soc_codec *codec)
376 wm8776_set_bias_level(codec, SND_SOC_BIAS_OFF);
381 static int wm8776_resume(struct snd_soc_codec *codec)
383 wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
387 #define wm8776_suspend NULL
388 #define wm8776_resume NULL
391 static int wm8776_probe(struct snd_soc_codec *codec)
393 struct wm8776_priv *wm8776 = snd_soc_codec_get_drvdata(codec);
396 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8776->control_type);
398 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
402 ret = wm8776_reset(codec);
404 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
408 wm8776_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
410 /* Latch the update bits; right channel only since we always
412 snd_soc_update_bits(codec, WM8776_HPRVOL, 0x100, 0x100);
413 snd_soc_update_bits(codec, WM8776_DACRVOL, 0x100, 0x100);
418 /* power down chip */
419 static int wm8776_remove(struct snd_soc_codec *codec)
421 wm8776_set_bias_level(codec, SND_SOC_BIAS_OFF);
425 static struct snd_soc_codec_driver soc_codec_dev_wm8776 = {
426 .probe = wm8776_probe,
427 .remove = wm8776_remove,
428 .suspend = wm8776_suspend,
429 .resume = wm8776_resume,
430 .set_bias_level = wm8776_set_bias_level,
431 .reg_cache_size = ARRAY_SIZE(wm8776_reg),
432 .reg_word_size = sizeof(u16),
433 .reg_cache_default = wm8776_reg,
435 .controls = wm8776_snd_controls,
436 .num_controls = ARRAY_SIZE(wm8776_snd_controls),
437 .dapm_widgets = wm8776_dapm_widgets,
438 .num_dapm_widgets = ARRAY_SIZE(wm8776_dapm_widgets),
439 .dapm_routes = routes,
440 .num_dapm_routes = ARRAY_SIZE(routes),
443 static const struct of_device_id wm8776_of_match[] = {
444 { .compatible = "wlf,wm8776", },
447 MODULE_DEVICE_TABLE(of, wm8776_of_match);
449 #if defined(CONFIG_SPI_MASTER)
450 static int __devinit wm8776_spi_probe(struct spi_device *spi)
452 struct wm8776_priv *wm8776;
455 wm8776 = devm_kzalloc(&spi->dev, sizeof(struct wm8776_priv),
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));
469 static int __devexit wm8776_spi_remove(struct spi_device *spi)
471 snd_soc_unregister_codec(&spi->dev);
475 static struct spi_driver wm8776_spi_driver = {
478 .owner = THIS_MODULE,
479 .of_match_table = wm8776_of_match,
481 .probe = wm8776_spi_probe,
482 .remove = __devexit_p(wm8776_spi_remove),
484 #endif /* CONFIG_SPI_MASTER */
486 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
487 static __devinit int wm8776_i2c_probe(struct i2c_client *i2c,
488 const struct i2c_device_id *id)
490 struct wm8776_priv *wm8776;
493 wm8776 = devm_kzalloc(&i2c->dev, sizeof(struct wm8776_priv),
498 i2c_set_clientdata(i2c, wm8776);
499 wm8776->control_type = SND_SOC_I2C;
501 ret = snd_soc_register_codec(&i2c->dev,
502 &soc_codec_dev_wm8776, wm8776_dai, ARRAY_SIZE(wm8776_dai));
507 static __devexit int wm8776_i2c_remove(struct i2c_client *client)
509 snd_soc_unregister_codec(&client->dev);
513 static const struct i2c_device_id wm8776_i2c_id[] = {
517 MODULE_DEVICE_TABLE(i2c, wm8776_i2c_id);
519 static struct i2c_driver wm8776_i2c_driver = {
522 .owner = THIS_MODULE,
523 .of_match_table = wm8776_of_match,
525 .probe = wm8776_i2c_probe,
526 .remove = __devexit_p(wm8776_i2c_remove),
527 .id_table = wm8776_i2c_id,
531 static int __init wm8776_modinit(void)
534 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
535 ret = i2c_add_driver(&wm8776_i2c_driver);
537 printk(KERN_ERR "Failed to register wm8776 I2C driver: %d\n",
541 #if defined(CONFIG_SPI_MASTER)
542 ret = spi_register_driver(&wm8776_spi_driver);
544 printk(KERN_ERR "Failed to register wm8776 SPI driver: %d\n",
550 module_init(wm8776_modinit);
552 static void __exit wm8776_exit(void)
554 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
555 i2c_del_driver(&wm8776_i2c_driver);
557 #if defined(CONFIG_SPI_MASTER)
558 spi_unregister_driver(&wm8776_spi_driver);
561 module_exit(wm8776_exit);
563 MODULE_DESCRIPTION("ASoC WM8776 driver");
564 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
565 MODULE_LICENSE("GPL");