1 // SPDX-License-Identifier: GPL-2.0-only
3 * wm8776.c -- WM8776 ALSA SoC Audio driver
5 * Copyright 2009-12 Wolfson Microelectronics plc
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
9 * TODO: Input ALC/limiter support
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/init.h>
15 #include <linux/delay.h>
17 #include <linux/i2c.h>
18 #include <linux/of_device.h>
19 #include <linux/regmap.h>
20 #include <linux/spi/spi.h>
21 #include <linux/slab.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/initval.h>
27 #include <sound/tlv.h>
31 enum wm8776_chip_type
{
36 /* codec private data */
38 struct regmap
*regmap
;
42 static const struct reg_default wm8776_reg_defaults
[] = {
68 static bool wm8776_volatile(struct device
*dev
, unsigned int reg
)
78 static int wm8776_reset(struct snd_soc_component
*component
)
80 return snd_soc_component_write(component
, WM8776_RESET
, 0);
83 static const DECLARE_TLV_DB_SCALE(hp_tlv
, -12100, 100, 1);
84 static const DECLARE_TLV_DB_SCALE(dac_tlv
, -12750, 50, 1);
85 static const DECLARE_TLV_DB_SCALE(adc_tlv
, -10350, 50, 1);
87 static const struct snd_kcontrol_new wm8776_snd_controls
[] = {
88 SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8776_HPLVOL
, WM8776_HPRVOL
,
90 SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8776_DACLVOL
, WM8776_DACRVOL
,
92 SOC_SINGLE("Digital Playback ZC Switch", WM8776_DACCTRL1
, 0, 1, 0),
94 SOC_SINGLE("Deemphasis Switch", WM8776_DACCTRL2
, 0, 1, 0),
96 SOC_DOUBLE_R_TLV("Capture Volume", WM8776_ADCLVOL
, WM8776_ADCRVOL
,
98 SOC_DOUBLE("Capture Switch", WM8776_ADCMUX
, 7, 6, 1, 1),
99 SOC_DOUBLE_R("Capture ZC Switch", WM8776_ADCLVOL
, WM8776_ADCRVOL
, 8, 1, 0),
100 SOC_SINGLE("Capture HPF Switch", WM8776_ADCIFCTRL
, 8, 1, 1),
103 static const struct snd_kcontrol_new inmix_controls
[] = {
104 SOC_DAPM_SINGLE("AIN1 Switch", WM8776_ADCMUX
, 0, 1, 0),
105 SOC_DAPM_SINGLE("AIN2 Switch", WM8776_ADCMUX
, 1, 1, 0),
106 SOC_DAPM_SINGLE("AIN3 Switch", WM8776_ADCMUX
, 2, 1, 0),
107 SOC_DAPM_SINGLE("AIN4 Switch", WM8776_ADCMUX
, 3, 1, 0),
108 SOC_DAPM_SINGLE("AIN5 Switch", WM8776_ADCMUX
, 4, 1, 0),
111 static const struct snd_kcontrol_new outmix_controls
[] = {
112 SOC_DAPM_SINGLE("DAC Switch", WM8776_OUTMUX
, 0, 1, 0),
113 SOC_DAPM_SINGLE("AUX Switch", WM8776_OUTMUX
, 1, 1, 0),
114 SOC_DAPM_SINGLE("Bypass Switch", WM8776_OUTMUX
, 2, 1, 0),
117 static const struct snd_soc_dapm_widget wm8776_dapm_widgets
[] = {
118 SND_SOC_DAPM_INPUT("AUX"),
120 SND_SOC_DAPM_INPUT("AIN1"),
121 SND_SOC_DAPM_INPUT("AIN2"),
122 SND_SOC_DAPM_INPUT("AIN3"),
123 SND_SOC_DAPM_INPUT("AIN4"),
124 SND_SOC_DAPM_INPUT("AIN5"),
126 SND_SOC_DAPM_MIXER("Input Mixer", WM8776_PWRDOWN
, 6, 1,
127 inmix_controls
, ARRAY_SIZE(inmix_controls
)),
129 SND_SOC_DAPM_ADC("ADC", "Capture", WM8776_PWRDOWN
, 1, 1),
130 SND_SOC_DAPM_DAC("DAC", "Playback", WM8776_PWRDOWN
, 2, 1),
132 SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM
, 0, 0,
133 outmix_controls
, ARRAY_SIZE(outmix_controls
)),
135 SND_SOC_DAPM_PGA("Headphone PGA", WM8776_PWRDOWN
, 3, 1, NULL
, 0),
137 SND_SOC_DAPM_OUTPUT("VOUT"),
139 SND_SOC_DAPM_OUTPUT("HPOUTL"),
140 SND_SOC_DAPM_OUTPUT("HPOUTR"),
143 static const struct snd_soc_dapm_route routes
[] = {
144 { "Input Mixer", "AIN1 Switch", "AIN1" },
145 { "Input Mixer", "AIN2 Switch", "AIN2" },
146 { "Input Mixer", "AIN3 Switch", "AIN3" },
147 { "Input Mixer", "AIN4 Switch", "AIN4" },
148 { "Input Mixer", "AIN5 Switch", "AIN5" },
150 { "ADC", NULL
, "Input Mixer" },
152 { "Output Mixer", "DAC Switch", "DAC" },
153 { "Output Mixer", "AUX Switch", "AUX" },
154 { "Output Mixer", "Bypass Switch", "Input Mixer" },
156 { "VOUT", NULL
, "Output Mixer" },
158 { "Headphone PGA", NULL
, "Output Mixer" },
160 { "HPOUTL", NULL
, "Headphone PGA" },
161 { "HPOUTR", NULL
, "Headphone PGA" },
164 static int wm8776_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
166 struct snd_soc_component
*component
= dai
->component
;
167 int reg
, iface
, master
;
169 switch (dai
->driver
->id
) {
171 reg
= WM8776_DACIFCTRL
;
175 reg
= WM8776_ADCIFCTRL
;
184 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
185 case SND_SOC_DAIFMT_CBM_CFM
:
187 case SND_SOC_DAIFMT_CBS_CFS
:
194 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
195 case SND_SOC_DAIFMT_I2S
:
198 case SND_SOC_DAIFMT_RIGHT_J
:
200 case SND_SOC_DAIFMT_LEFT_J
:
207 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
208 case SND_SOC_DAIFMT_NB_NF
:
210 case SND_SOC_DAIFMT_IB_IF
:
213 case SND_SOC_DAIFMT_IB_NF
:
216 case SND_SOC_DAIFMT_NB_IF
:
223 /* Finally, write out the values */
224 snd_soc_component_update_bits(component
, reg
, 0xf, iface
);
225 snd_soc_component_update_bits(component
, WM8776_MSTRCTRL
, 0x180, master
);
230 static int mclk_ratios
[] = {
239 static int wm8776_hw_params(struct snd_pcm_substream
*substream
,
240 struct snd_pcm_hw_params
*params
,
241 struct snd_soc_dai
*dai
)
243 struct snd_soc_component
*component
= dai
->component
;
244 struct wm8776_priv
*wm8776
= snd_soc_component_get_drvdata(component
);
245 int iface_reg
, iface
;
246 int ratio_shift
, master
;
249 switch (dai
->driver
->id
) {
251 iface_reg
= WM8776_DACIFCTRL
;
256 iface_reg
= WM8776_ADCIFCTRL
;
264 /* Set word length */
265 switch (params_width(params
)) {
279 dev_err(component
->dev
, "Unsupported sample size: %i\n",
280 params_width(params
));
284 /* Only need to set MCLK/LRCLK ratio if we're master */
285 if (snd_soc_component_read32(component
, WM8776_MSTRCTRL
) & master
) {
286 for (i
= 0; i
< ARRAY_SIZE(mclk_ratios
); i
++) {
287 if (wm8776
->sysclk
[dai
->driver
->id
] / params_rate(params
)
292 if (i
== ARRAY_SIZE(mclk_ratios
)) {
293 dev_err(component
->dev
,
294 "Unable to configure MCLK ratio %d/%d\n",
295 wm8776
->sysclk
[dai
->driver
->id
], params_rate(params
));
299 dev_dbg(component
->dev
, "MCLK is %dfs\n", mclk_ratios
[i
]);
301 snd_soc_component_update_bits(component
, WM8776_MSTRCTRL
,
302 0x7 << ratio_shift
, i
<< ratio_shift
);
304 dev_dbg(component
->dev
, "DAI in slave mode\n");
307 snd_soc_component_update_bits(component
, iface_reg
, 0x30, iface
);
312 static int wm8776_mute(struct snd_soc_dai
*dai
, int mute
)
314 struct snd_soc_component
*component
= dai
->component
;
316 return snd_soc_component_write(component
, WM8776_DACMUTE
, !!mute
);
319 static int wm8776_set_sysclk(struct snd_soc_dai
*dai
,
320 int clk_id
, unsigned int freq
, int dir
)
322 struct snd_soc_component
*component
= dai
->component
;
323 struct wm8776_priv
*wm8776
= snd_soc_component_get_drvdata(component
);
325 if (WARN_ON(dai
->driver
->id
>= ARRAY_SIZE(wm8776
->sysclk
)))
328 wm8776
->sysclk
[dai
->driver
->id
] = freq
;
333 static int wm8776_set_bias_level(struct snd_soc_component
*component
,
334 enum snd_soc_bias_level level
)
336 struct wm8776_priv
*wm8776
= snd_soc_component_get_drvdata(component
);
339 case SND_SOC_BIAS_ON
:
341 case SND_SOC_BIAS_PREPARE
:
343 case SND_SOC_BIAS_STANDBY
:
344 if (snd_soc_component_get_bias_level(component
) == SND_SOC_BIAS_OFF
) {
345 regcache_sync(wm8776
->regmap
);
347 /* Disable the global powerdown; DAPM does the rest */
348 snd_soc_component_update_bits(component
, WM8776_PWRDOWN
, 1, 0);
352 case SND_SOC_BIAS_OFF
:
353 snd_soc_component_update_bits(component
, WM8776_PWRDOWN
, 1, 1);
360 #define WM8776_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
361 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
363 static const struct snd_soc_dai_ops wm8776_dac_ops
= {
364 .digital_mute
= wm8776_mute
,
365 .hw_params
= wm8776_hw_params
,
366 .set_fmt
= wm8776_set_fmt
,
367 .set_sysclk
= wm8776_set_sysclk
,
370 static const struct snd_soc_dai_ops wm8776_adc_ops
= {
371 .hw_params
= wm8776_hw_params
,
372 .set_fmt
= wm8776_set_fmt
,
373 .set_sysclk
= wm8776_set_sysclk
,
376 static struct snd_soc_dai_driver wm8776_dai
[] = {
378 .name
= "wm8776-hifi-playback",
379 .id
= WM8776_DAI_DAC
,
381 .stream_name
= "Playback",
384 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
387 .formats
= WM8776_FORMATS
,
389 .ops
= &wm8776_dac_ops
,
392 .name
= "wm8776-hifi-capture",
393 .id
= WM8776_DAI_ADC
,
395 .stream_name
= "Capture",
398 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
401 .formats
= WM8776_FORMATS
,
403 .ops
= &wm8776_adc_ops
,
407 static int wm8776_probe(struct snd_soc_component
*component
)
411 ret
= wm8776_reset(component
);
413 dev_err(component
->dev
, "Failed to issue reset: %d\n", ret
);
417 /* Latch the update bits; right channel only since we always
419 snd_soc_component_update_bits(component
, WM8776_HPRVOL
, 0x100, 0x100);
420 snd_soc_component_update_bits(component
, WM8776_DACRVOL
, 0x100, 0x100);
425 static const struct snd_soc_component_driver soc_component_dev_wm8776
= {
426 .probe
= wm8776_probe
,
427 .set_bias_level
= wm8776_set_bias_level
,
428 .controls
= wm8776_snd_controls
,
429 .num_controls
= ARRAY_SIZE(wm8776_snd_controls
),
430 .dapm_widgets
= wm8776_dapm_widgets
,
431 .num_dapm_widgets
= ARRAY_SIZE(wm8776_dapm_widgets
),
432 .dapm_routes
= routes
,
433 .num_dapm_routes
= ARRAY_SIZE(routes
),
434 .suspend_bias_off
= 1,
436 .use_pmdown_time
= 1,
438 .non_legacy_dai_naming
= 1,
441 static const struct of_device_id wm8776_of_match
[] = {
442 { .compatible
= "wlf,wm8776", },
445 MODULE_DEVICE_TABLE(of
, wm8776_of_match
);
447 static const struct regmap_config wm8776_regmap
= {
450 .max_register
= WM8776_RESET
,
452 .reg_defaults
= wm8776_reg_defaults
,
453 .num_reg_defaults
= ARRAY_SIZE(wm8776_reg_defaults
),
454 .cache_type
= REGCACHE_RBTREE
,
456 .volatile_reg
= wm8776_volatile
,
459 #if defined(CONFIG_SPI_MASTER)
460 static int wm8776_spi_probe(struct spi_device
*spi
)
462 struct wm8776_priv
*wm8776
;
465 wm8776
= devm_kzalloc(&spi
->dev
, sizeof(struct wm8776_priv
),
470 wm8776
->regmap
= devm_regmap_init_spi(spi
, &wm8776_regmap
);
471 if (IS_ERR(wm8776
->regmap
))
472 return PTR_ERR(wm8776
->regmap
);
474 spi_set_drvdata(spi
, wm8776
);
476 ret
= devm_snd_soc_register_component(&spi
->dev
,
477 &soc_component_dev_wm8776
, wm8776_dai
, ARRAY_SIZE(wm8776_dai
));
482 static struct spi_driver wm8776_spi_driver
= {
485 .of_match_table
= wm8776_of_match
,
487 .probe
= wm8776_spi_probe
,
489 #endif /* CONFIG_SPI_MASTER */
491 #if IS_ENABLED(CONFIG_I2C)
492 static int wm8776_i2c_probe(struct i2c_client
*i2c
,
493 const struct i2c_device_id
*id
)
495 struct wm8776_priv
*wm8776
;
498 wm8776
= devm_kzalloc(&i2c
->dev
, sizeof(struct wm8776_priv
),
503 wm8776
->regmap
= devm_regmap_init_i2c(i2c
, &wm8776_regmap
);
504 if (IS_ERR(wm8776
->regmap
))
505 return PTR_ERR(wm8776
->regmap
);
507 i2c_set_clientdata(i2c
, wm8776
);
509 ret
= devm_snd_soc_register_component(&i2c
->dev
,
510 &soc_component_dev_wm8776
, wm8776_dai
, ARRAY_SIZE(wm8776_dai
));
515 static const struct i2c_device_id wm8776_i2c_id
[] = {
516 { "wm8775", WM8775
},
517 { "wm8776", WM8776
},
520 MODULE_DEVICE_TABLE(i2c
, wm8776_i2c_id
);
522 static struct i2c_driver wm8776_i2c_driver
= {
525 .of_match_table
= wm8776_of_match
,
527 .probe
= wm8776_i2c_probe
,
528 .id_table
= wm8776_i2c_id
,
532 static int __init
wm8776_modinit(void)
535 #if IS_ENABLED(CONFIG_I2C)
536 ret
= i2c_add_driver(&wm8776_i2c_driver
);
538 printk(KERN_ERR
"Failed to register wm8776 I2C driver: %d\n",
542 #if defined(CONFIG_SPI_MASTER)
543 ret
= spi_register_driver(&wm8776_spi_driver
);
545 printk(KERN_ERR
"Failed to register wm8776 SPI driver: %d\n",
551 module_init(wm8776_modinit
);
553 static void __exit
wm8776_exit(void)
555 #if IS_ENABLED(CONFIG_I2C)
556 i2c_del_driver(&wm8776_i2c_driver
);
558 #if defined(CONFIG_SPI_MASTER)
559 spi_unregister_driver(&wm8776_spi_driver
);
562 module_exit(wm8776_exit
);
564 MODULE_DESCRIPTION("ASoC WM8776 driver");
565 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
566 MODULE_LICENSE("GPL");