2 * wm8776.c -- WM8776 ALSA SoC Audio driver
4 * Copyright 2009-12 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/regmap.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>
34 enum wm8776_chip_type
{
39 /* codec private data */
41 struct regmap
*regmap
;
45 static const struct reg_default wm8776_reg_defaults
[] = {
71 static bool wm8776_volatile(struct device
*dev
, unsigned int reg
)
81 static int wm8776_reset(struct snd_soc_codec
*codec
)
83 return snd_soc_write(codec
, WM8776_RESET
, 0);
86 static const DECLARE_TLV_DB_SCALE(hp_tlv
, -12100, 100, 1);
87 static const DECLARE_TLV_DB_SCALE(dac_tlv
, -12750, 50, 1);
88 static const DECLARE_TLV_DB_SCALE(adc_tlv
, -10350, 50, 1);
90 static const struct snd_kcontrol_new wm8776_snd_controls
[] = {
91 SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8776_HPLVOL
, WM8776_HPRVOL
,
93 SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8776_DACLVOL
, WM8776_DACRVOL
,
95 SOC_SINGLE("Digital Playback ZC Switch", WM8776_DACCTRL1
, 0, 1, 0),
97 SOC_SINGLE("Deemphasis Switch", WM8776_DACCTRL2
, 0, 1, 0),
99 SOC_DOUBLE_R_TLV("Capture Volume", WM8776_ADCLVOL
, WM8776_ADCRVOL
,
101 SOC_DOUBLE("Capture Switch", WM8776_ADCMUX
, 7, 6, 1, 1),
102 SOC_DOUBLE_R("Capture ZC Switch", WM8776_ADCLVOL
, WM8776_ADCRVOL
, 8, 1, 0),
103 SOC_SINGLE("Capture HPF Switch", WM8776_ADCIFCTRL
, 8, 1, 1),
106 static const struct snd_kcontrol_new inmix_controls
[] = {
107 SOC_DAPM_SINGLE("AIN1 Switch", WM8776_ADCMUX
, 0, 1, 0),
108 SOC_DAPM_SINGLE("AIN2 Switch", WM8776_ADCMUX
, 1, 1, 0),
109 SOC_DAPM_SINGLE("AIN3 Switch", WM8776_ADCMUX
, 2, 1, 0),
110 SOC_DAPM_SINGLE("AIN4 Switch", WM8776_ADCMUX
, 3, 1, 0),
111 SOC_DAPM_SINGLE("AIN5 Switch", WM8776_ADCMUX
, 4, 1, 0),
114 static const struct snd_kcontrol_new outmix_controls
[] = {
115 SOC_DAPM_SINGLE("DAC Switch", WM8776_OUTMUX
, 0, 1, 0),
116 SOC_DAPM_SINGLE("AUX Switch", WM8776_OUTMUX
, 1, 1, 0),
117 SOC_DAPM_SINGLE("Bypass Switch", WM8776_OUTMUX
, 2, 1, 0),
120 static const struct snd_soc_dapm_widget wm8776_dapm_widgets
[] = {
121 SND_SOC_DAPM_INPUT("AUX"),
123 SND_SOC_DAPM_INPUT("AIN1"),
124 SND_SOC_DAPM_INPUT("AIN2"),
125 SND_SOC_DAPM_INPUT("AIN3"),
126 SND_SOC_DAPM_INPUT("AIN4"),
127 SND_SOC_DAPM_INPUT("AIN5"),
129 SND_SOC_DAPM_MIXER("Input Mixer", WM8776_PWRDOWN
, 6, 1,
130 inmix_controls
, ARRAY_SIZE(inmix_controls
)),
132 SND_SOC_DAPM_ADC("ADC", "Capture", WM8776_PWRDOWN
, 1, 1),
133 SND_SOC_DAPM_DAC("DAC", "Playback", WM8776_PWRDOWN
, 2, 1),
135 SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM
, 0, 0,
136 outmix_controls
, ARRAY_SIZE(outmix_controls
)),
138 SND_SOC_DAPM_PGA("Headphone PGA", WM8776_PWRDOWN
, 3, 1, NULL
, 0),
140 SND_SOC_DAPM_OUTPUT("VOUT"),
142 SND_SOC_DAPM_OUTPUT("HPOUTL"),
143 SND_SOC_DAPM_OUTPUT("HPOUTR"),
146 static const struct snd_soc_dapm_route routes
[] = {
147 { "Input Mixer", "AIN1 Switch", "AIN1" },
148 { "Input Mixer", "AIN2 Switch", "AIN2" },
149 { "Input Mixer", "AIN3 Switch", "AIN3" },
150 { "Input Mixer", "AIN4 Switch", "AIN4" },
151 { "Input Mixer", "AIN5 Switch", "AIN5" },
153 { "ADC", NULL
, "Input Mixer" },
155 { "Output Mixer", "DAC Switch", "DAC" },
156 { "Output Mixer", "AUX Switch", "AUX" },
157 { "Output Mixer", "Bypass Switch", "Input Mixer" },
159 { "VOUT", NULL
, "Output Mixer" },
161 { "Headphone PGA", NULL
, "Output Mixer" },
163 { "HPOUTL", NULL
, "Headphone PGA" },
164 { "HPOUTR", NULL
, "Headphone PGA" },
167 static int wm8776_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
169 struct snd_soc_codec
*codec
= dai
->codec
;
170 int reg
, iface
, master
;
172 switch (dai
->driver
->id
) {
174 reg
= WM8776_DACIFCTRL
;
178 reg
= WM8776_ADCIFCTRL
;
187 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
188 case SND_SOC_DAIFMT_CBM_CFM
:
190 case SND_SOC_DAIFMT_CBS_CFS
:
197 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
198 case SND_SOC_DAIFMT_I2S
:
201 case SND_SOC_DAIFMT_RIGHT_J
:
203 case SND_SOC_DAIFMT_LEFT_J
:
210 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
211 case SND_SOC_DAIFMT_NB_NF
:
213 case SND_SOC_DAIFMT_IB_IF
:
216 case SND_SOC_DAIFMT_IB_NF
:
219 case SND_SOC_DAIFMT_NB_IF
:
226 /* Finally, write out the values */
227 snd_soc_update_bits(codec
, reg
, 0xf, iface
);
228 snd_soc_update_bits(codec
, WM8776_MSTRCTRL
, 0x180, master
);
233 static int mclk_ratios
[] = {
242 static int wm8776_hw_params(struct snd_pcm_substream
*substream
,
243 struct snd_pcm_hw_params
*params
,
244 struct snd_soc_dai
*dai
)
246 struct snd_soc_codec
*codec
= dai
->codec
;
247 struct wm8776_priv
*wm8776
= snd_soc_codec_get_drvdata(codec
);
248 int iface_reg
, iface
;
249 int ratio_shift
, master
;
252 switch (dai
->driver
->id
) {
254 iface_reg
= WM8776_DACIFCTRL
;
259 iface_reg
= WM8776_ADCIFCTRL
;
267 /* Set word length */
268 switch (snd_pcm_format_width(params_format(params
))) {
282 dev_err(codec
->dev
, "Unsupported sample size: %i\n",
283 snd_pcm_format_width(params_format(params
)));
287 /* Only need to set MCLK/LRCLK ratio if we're master */
288 if (snd_soc_read(codec
, WM8776_MSTRCTRL
) & master
) {
289 for (i
= 0; i
< ARRAY_SIZE(mclk_ratios
); i
++) {
290 if (wm8776
->sysclk
[dai
->driver
->id
] / params_rate(params
)
295 if (i
== ARRAY_SIZE(mclk_ratios
)) {
297 "Unable to configure MCLK ratio %d/%d\n",
298 wm8776
->sysclk
[dai
->driver
->id
], params_rate(params
));
302 dev_dbg(codec
->dev
, "MCLK is %dfs\n", mclk_ratios
[i
]);
304 snd_soc_update_bits(codec
, WM8776_MSTRCTRL
,
305 0x7 << ratio_shift
, i
<< ratio_shift
);
307 dev_dbg(codec
->dev
, "DAI in slave mode\n");
310 snd_soc_update_bits(codec
, iface_reg
, 0x30, iface
);
315 static int wm8776_mute(struct snd_soc_dai
*dai
, int mute
)
317 struct snd_soc_codec
*codec
= dai
->codec
;
319 return snd_soc_write(codec
, WM8776_DACMUTE
, !!mute
);
322 static int wm8776_set_sysclk(struct snd_soc_dai
*dai
,
323 int clk_id
, unsigned int freq
, int dir
)
325 struct snd_soc_codec
*codec
= dai
->codec
;
326 struct wm8776_priv
*wm8776
= snd_soc_codec_get_drvdata(codec
);
328 if (WARN_ON(dai
->driver
->id
>= ARRAY_SIZE(wm8776
->sysclk
)))
331 wm8776
->sysclk
[dai
->driver
->id
] = freq
;
336 static int wm8776_set_bias_level(struct snd_soc_codec
*codec
,
337 enum snd_soc_bias_level level
)
339 struct wm8776_priv
*wm8776
= snd_soc_codec_get_drvdata(codec
);
342 case SND_SOC_BIAS_ON
:
344 case SND_SOC_BIAS_PREPARE
:
346 case SND_SOC_BIAS_STANDBY
:
347 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_OFF
) {
348 regcache_sync(wm8776
->regmap
);
350 /* Disable the global powerdown; DAPM does the rest */
351 snd_soc_update_bits(codec
, WM8776_PWRDOWN
, 1, 0);
355 case SND_SOC_BIAS_OFF
:
356 snd_soc_update_bits(codec
, WM8776_PWRDOWN
, 1, 1);
360 codec
->dapm
.bias_level
= level
;
364 #define WM8776_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
365 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
367 static const struct snd_soc_dai_ops wm8776_dac_ops
= {
368 .digital_mute
= wm8776_mute
,
369 .hw_params
= wm8776_hw_params
,
370 .set_fmt
= wm8776_set_fmt
,
371 .set_sysclk
= wm8776_set_sysclk
,
374 static const struct snd_soc_dai_ops wm8776_adc_ops
= {
375 .hw_params
= wm8776_hw_params
,
376 .set_fmt
= wm8776_set_fmt
,
377 .set_sysclk
= wm8776_set_sysclk
,
380 static struct snd_soc_dai_driver wm8776_dai
[] = {
382 .name
= "wm8776-hifi-playback",
383 .id
= WM8776_DAI_DAC
,
385 .stream_name
= "Playback",
388 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
391 .formats
= WM8776_FORMATS
,
393 .ops
= &wm8776_dac_ops
,
396 .name
= "wm8776-hifi-capture",
397 .id
= WM8776_DAI_ADC
,
399 .stream_name
= "Capture",
402 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
405 .formats
= WM8776_FORMATS
,
407 .ops
= &wm8776_adc_ops
,
412 static int wm8776_suspend(struct snd_soc_codec
*codec
)
414 wm8776_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
419 static int wm8776_resume(struct snd_soc_codec
*codec
)
421 wm8776_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
425 #define wm8776_suspend NULL
426 #define wm8776_resume NULL
429 static int wm8776_probe(struct snd_soc_codec
*codec
)
433 ret
= wm8776_reset(codec
);
435 dev_err(codec
->dev
, "Failed to issue reset: %d\n", ret
);
439 wm8776_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
441 /* Latch the update bits; right channel only since we always
443 snd_soc_update_bits(codec
, WM8776_HPRVOL
, 0x100, 0x100);
444 snd_soc_update_bits(codec
, WM8776_DACRVOL
, 0x100, 0x100);
449 /* power down chip */
450 static int wm8776_remove(struct snd_soc_codec
*codec
)
452 wm8776_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
456 static struct snd_soc_codec_driver soc_codec_dev_wm8776
= {
457 .probe
= wm8776_probe
,
458 .remove
= wm8776_remove
,
459 .suspend
= wm8776_suspend
,
460 .resume
= wm8776_resume
,
461 .set_bias_level
= wm8776_set_bias_level
,
463 .controls
= wm8776_snd_controls
,
464 .num_controls
= ARRAY_SIZE(wm8776_snd_controls
),
465 .dapm_widgets
= wm8776_dapm_widgets
,
466 .num_dapm_widgets
= ARRAY_SIZE(wm8776_dapm_widgets
),
467 .dapm_routes
= routes
,
468 .num_dapm_routes
= ARRAY_SIZE(routes
),
471 static const struct of_device_id wm8776_of_match
[] = {
472 { .compatible
= "wlf,wm8776", },
475 MODULE_DEVICE_TABLE(of
, wm8776_of_match
);
477 static const struct regmap_config wm8776_regmap
= {
480 .max_register
= WM8776_RESET
,
482 .reg_defaults
= wm8776_reg_defaults
,
483 .num_reg_defaults
= ARRAY_SIZE(wm8776_reg_defaults
),
484 .cache_type
= REGCACHE_RBTREE
,
486 .volatile_reg
= wm8776_volatile
,
489 #if defined(CONFIG_SPI_MASTER)
490 static int wm8776_spi_probe(struct spi_device
*spi
)
492 struct wm8776_priv
*wm8776
;
495 wm8776
= devm_kzalloc(&spi
->dev
, sizeof(struct wm8776_priv
),
500 wm8776
->regmap
= devm_regmap_init_spi(spi
, &wm8776_regmap
);
501 if (IS_ERR(wm8776
->regmap
))
502 return PTR_ERR(wm8776
->regmap
);
504 spi_set_drvdata(spi
, wm8776
);
506 ret
= snd_soc_register_codec(&spi
->dev
,
507 &soc_codec_dev_wm8776
, wm8776_dai
, ARRAY_SIZE(wm8776_dai
));
512 static int wm8776_spi_remove(struct spi_device
*spi
)
514 snd_soc_unregister_codec(&spi
->dev
);
518 static struct spi_driver wm8776_spi_driver
= {
521 .owner
= THIS_MODULE
,
522 .of_match_table
= wm8776_of_match
,
524 .probe
= wm8776_spi_probe
,
525 .remove
= wm8776_spi_remove
,
527 #endif /* CONFIG_SPI_MASTER */
529 #if IS_ENABLED(CONFIG_I2C)
530 static int wm8776_i2c_probe(struct i2c_client
*i2c
,
531 const struct i2c_device_id
*id
)
533 struct wm8776_priv
*wm8776
;
536 wm8776
= devm_kzalloc(&i2c
->dev
, sizeof(struct wm8776_priv
),
541 wm8776
->regmap
= devm_regmap_init_i2c(i2c
, &wm8776_regmap
);
542 if (IS_ERR(wm8776
->regmap
))
543 return PTR_ERR(wm8776
->regmap
);
545 i2c_set_clientdata(i2c
, wm8776
);
547 ret
= snd_soc_register_codec(&i2c
->dev
,
548 &soc_codec_dev_wm8776
, wm8776_dai
, ARRAY_SIZE(wm8776_dai
));
553 static int wm8776_i2c_remove(struct i2c_client
*client
)
555 snd_soc_unregister_codec(&client
->dev
);
559 static const struct i2c_device_id wm8776_i2c_id
[] = {
560 { "wm8775", WM8775
},
561 { "wm8776", WM8776
},
564 MODULE_DEVICE_TABLE(i2c
, wm8776_i2c_id
);
566 static struct i2c_driver wm8776_i2c_driver
= {
569 .owner
= THIS_MODULE
,
570 .of_match_table
= wm8776_of_match
,
572 .probe
= wm8776_i2c_probe
,
573 .remove
= wm8776_i2c_remove
,
574 .id_table
= wm8776_i2c_id
,
578 static int __init
wm8776_modinit(void)
581 #if IS_ENABLED(CONFIG_I2C)
582 ret
= i2c_add_driver(&wm8776_i2c_driver
);
584 printk(KERN_ERR
"Failed to register wm8776 I2C driver: %d\n",
588 #if defined(CONFIG_SPI_MASTER)
589 ret
= spi_register_driver(&wm8776_spi_driver
);
591 printk(KERN_ERR
"Failed to register wm8776 SPI driver: %d\n",
597 module_init(wm8776_modinit
);
599 static void __exit
wm8776_exit(void)
601 #if IS_ENABLED(CONFIG_I2C)
602 i2c_del_driver(&wm8776_i2c_driver
);
604 #if defined(CONFIG_SPI_MASTER)
605 spi_unregister_driver(&wm8776_spi_driver
);
608 module_exit(wm8776_exit
);
610 MODULE_DESCRIPTION("ASoC WM8776 driver");
611 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
612 MODULE_LICENSE("GPL");