1 // SPDX-License-Identifier: GPL-2.0-only
3 * wm8510.c -- WM8510 ALSA Soc Audio driver
5 * Copyright 2006 Wolfson Microelectronics PLC.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
16 #include <linux/i2c.h>
17 #include <linux/spi/spi.h>
18 #include <linux/slab.h>
19 #include <linux/of_device.h>
20 #include <linux/regmap.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/initval.h>
30 * wm8510 register cache
31 * We can't read the WM8510 register space when we are
32 * using 2 wire for device control, so we cache them instead.
34 static const struct reg_default wm8510_reg_defaults[] = {
93 static bool wm8510_volatile(struct device *dev, unsigned int reg)
103 #define WM8510_POWER1_BIASEN 0x08
104 #define WM8510_POWER1_BUFIOEN 0x10
106 #define wm8510_reset(c) snd_soc_component_write(c, WM8510_RESET, 0)
108 /* codec private data */
110 struct regmap *regmap;
113 static const char *wm8510_companding[] = { "Off", "NC", "u-law", "A-law" };
114 static const char *wm8510_deemp[] = { "None", "32kHz", "44.1kHz", "48kHz" };
115 static const char *wm8510_alc[] = { "ALC", "Limiter" };
117 static const struct soc_enum wm8510_enum[] = {
118 SOC_ENUM_SINGLE(WM8510_COMP, 1, 4, wm8510_companding), /* adc */
119 SOC_ENUM_SINGLE(WM8510_COMP, 3, 4, wm8510_companding), /* dac */
120 SOC_ENUM_SINGLE(WM8510_DAC, 4, 4, wm8510_deemp),
121 SOC_ENUM_SINGLE(WM8510_ALC3, 8, 2, wm8510_alc),
124 static const struct snd_kcontrol_new wm8510_snd_controls[] = {
126 SOC_SINGLE("Digital Loopback Switch", WM8510_COMP, 0, 1, 0),
128 SOC_ENUM("DAC Companding", wm8510_enum[1]),
129 SOC_ENUM("ADC Companding", wm8510_enum[0]),
131 SOC_ENUM("Playback De-emphasis", wm8510_enum[2]),
132 SOC_SINGLE("DAC Inversion Switch", WM8510_DAC, 0, 1, 0),
134 SOC_SINGLE("Master Playback Volume", WM8510_DACVOL, 0, 127, 0),
136 SOC_SINGLE("High Pass Filter Switch", WM8510_ADC, 8, 1, 0),
137 SOC_SINGLE("High Pass Cut Off", WM8510_ADC, 4, 7, 0),
138 SOC_SINGLE("ADC Inversion Switch", WM8510_COMP, 0, 1, 0),
140 SOC_SINGLE("Capture Volume", WM8510_ADCVOL, 0, 127, 0),
142 SOC_SINGLE("DAC Playback Limiter Switch", WM8510_DACLIM1, 8, 1, 0),
143 SOC_SINGLE("DAC Playback Limiter Decay", WM8510_DACLIM1, 4, 15, 0),
144 SOC_SINGLE("DAC Playback Limiter Attack", WM8510_DACLIM1, 0, 15, 0),
146 SOC_SINGLE("DAC Playback Limiter Threshold", WM8510_DACLIM2, 4, 7, 0),
147 SOC_SINGLE("DAC Playback Limiter Boost", WM8510_DACLIM2, 0, 15, 0),
149 SOC_SINGLE("ALC Enable Switch", WM8510_ALC1, 8, 1, 0),
150 SOC_SINGLE("ALC Capture Max Gain", WM8510_ALC1, 3, 7, 0),
151 SOC_SINGLE("ALC Capture Min Gain", WM8510_ALC1, 0, 7, 0),
153 SOC_SINGLE("ALC Capture ZC Switch", WM8510_ALC2, 8, 1, 0),
154 SOC_SINGLE("ALC Capture Hold", WM8510_ALC2, 4, 7, 0),
155 SOC_SINGLE("ALC Capture Target", WM8510_ALC2, 0, 15, 0),
157 SOC_ENUM("ALC Capture Mode", wm8510_enum[3]),
158 SOC_SINGLE("ALC Capture Decay", WM8510_ALC3, 4, 15, 0),
159 SOC_SINGLE("ALC Capture Attack", WM8510_ALC3, 0, 15, 0),
161 SOC_SINGLE("ALC Capture Noise Gate Switch", WM8510_NGATE, 3, 1, 0),
162 SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8510_NGATE, 0, 7, 0),
164 SOC_SINGLE("Capture PGA ZC Switch", WM8510_INPPGA, 7, 1, 0),
165 SOC_SINGLE("Capture PGA Volume", WM8510_INPPGA, 0, 63, 0),
167 SOC_SINGLE("Speaker Playback ZC Switch", WM8510_SPKVOL, 7, 1, 0),
168 SOC_SINGLE("Speaker Playback Switch", WM8510_SPKVOL, 6, 1, 1),
169 SOC_SINGLE("Speaker Playback Volume", WM8510_SPKVOL, 0, 63, 0),
170 SOC_SINGLE("Speaker Boost", WM8510_OUTPUT, 2, 1, 0),
172 SOC_SINGLE("Capture Boost(+20dB)", WM8510_ADCBOOST, 8, 1, 0),
173 SOC_SINGLE("Mono Playback Switch", WM8510_MONOMIX, 6, 1, 1),
176 /* Speaker Output Mixer */
177 static const struct snd_kcontrol_new wm8510_speaker_mixer_controls[] = {
178 SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_SPKMIX, 1, 1, 0),
179 SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_SPKMIX, 5, 1, 0),
180 SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_SPKMIX, 0, 1, 0),
183 /* Mono Output Mixer */
184 static const struct snd_kcontrol_new wm8510_mono_mixer_controls[] = {
185 SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_MONOMIX, 1, 1, 0),
186 SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_MONOMIX, 2, 1, 0),
187 SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_MONOMIX, 0, 1, 0),
190 static const struct snd_kcontrol_new wm8510_boost_controls[] = {
191 SOC_DAPM_SINGLE("Mic PGA Switch", WM8510_INPPGA, 6, 1, 1),
192 SOC_DAPM_SINGLE("Aux Volume", WM8510_ADCBOOST, 0, 7, 0),
193 SOC_DAPM_SINGLE("Mic Volume", WM8510_ADCBOOST, 4, 7, 0),
196 static const struct snd_kcontrol_new wm8510_micpga_controls[] = {
197 SOC_DAPM_SINGLE("MICP Switch", WM8510_INPUT, 0, 1, 0),
198 SOC_DAPM_SINGLE("MICN Switch", WM8510_INPUT, 1, 1, 0),
199 SOC_DAPM_SINGLE("AUX Switch", WM8510_INPUT, 2, 1, 0),
202 static const struct snd_soc_dapm_widget wm8510_dapm_widgets[] = {
203 SND_SOC_DAPM_MIXER("Speaker Mixer", WM8510_POWER3, 2, 0,
204 &wm8510_speaker_mixer_controls[0],
205 ARRAY_SIZE(wm8510_speaker_mixer_controls)),
206 SND_SOC_DAPM_MIXER("Mono Mixer", WM8510_POWER3, 3, 0,
207 &wm8510_mono_mixer_controls[0],
208 ARRAY_SIZE(wm8510_mono_mixer_controls)),
209 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8510_POWER3, 0, 0),
210 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8510_POWER2, 0, 0),
211 SND_SOC_DAPM_PGA("Aux Input", WM8510_POWER1, 6, 0, NULL, 0),
212 SND_SOC_DAPM_PGA("SpkN Out", WM8510_POWER3, 5, 0, NULL, 0),
213 SND_SOC_DAPM_PGA("SpkP Out", WM8510_POWER3, 6, 0, NULL, 0),
214 SND_SOC_DAPM_PGA("Mono Out", WM8510_POWER3, 7, 0, NULL, 0),
216 SND_SOC_DAPM_MIXER("Mic PGA", WM8510_POWER2, 2, 0,
217 &wm8510_micpga_controls[0],
218 ARRAY_SIZE(wm8510_micpga_controls)),
219 SND_SOC_DAPM_MIXER("Boost Mixer", WM8510_POWER2, 4, 0,
220 &wm8510_boost_controls[0],
221 ARRAY_SIZE(wm8510_boost_controls)),
223 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8510_POWER1, 4, 0),
225 SND_SOC_DAPM_INPUT("MICN"),
226 SND_SOC_DAPM_INPUT("MICP"),
227 SND_SOC_DAPM_INPUT("AUX"),
228 SND_SOC_DAPM_OUTPUT("MONOOUT"),
229 SND_SOC_DAPM_OUTPUT("SPKOUTP"),
230 SND_SOC_DAPM_OUTPUT("SPKOUTN"),
233 static const struct snd_soc_dapm_route wm8510_dapm_routes[] = {
234 /* Mono output mixer */
235 {"Mono Mixer", "PCM Playback Switch", "DAC"},
236 {"Mono Mixer", "Aux Playback Switch", "Aux Input"},
237 {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"},
239 /* Speaker output mixer */
240 {"Speaker Mixer", "PCM Playback Switch", "DAC"},
241 {"Speaker Mixer", "Aux Playback Switch", "Aux Input"},
242 {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"},
245 {"Mono Out", NULL, "Mono Mixer"},
246 {"MONOOUT", NULL, "Mono Out"},
247 {"SpkN Out", NULL, "Speaker Mixer"},
248 {"SpkP Out", NULL, "Speaker Mixer"},
249 {"SPKOUTN", NULL, "SpkN Out"},
250 {"SPKOUTP", NULL, "SpkP Out"},
253 {"Mic PGA", "MICN Switch", "MICN"},
254 {"Mic PGA", "MICP Switch", "MICP"},
255 { "Mic PGA", "AUX Switch", "Aux Input" },
258 {"Boost Mixer", "Mic PGA Switch", "Mic PGA"},
259 {"Boost Mixer", "Mic Volume", "MICP"},
260 {"Boost Mixer", "Aux Volume", "Aux Input"},
262 {"ADC", NULL, "Boost Mixer"},
266 unsigned int pre_div:4; /* prescale - 1 */
271 static struct pll_ pll_div;
273 /* The size in bits of the pll divide multiplied by 10
274 * to allow rounding later */
275 #define FIXED_PLL_SIZE ((1 << 24) * 10)
277 static void pll_factors(unsigned int target, unsigned int source)
279 unsigned long long Kpart;
280 unsigned int K, Ndiv, Nmod;
282 Ndiv = target / source;
286 Ndiv = target / source;
290 if ((Ndiv < 6) || (Ndiv > 12))
292 "WM8510 N value %u outwith recommended range!d\n",
296 Nmod = target % source;
297 Kpart = FIXED_PLL_SIZE * (long long)Nmod;
299 do_div(Kpart, source);
301 K = Kpart & 0xFFFFFFFF;
303 /* Check if we need to round */
307 /* Move down to proper range now rounding is done */
313 static int wm8510_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
314 int source, unsigned int freq_in, unsigned int freq_out)
316 struct snd_soc_component *component = codec_dai->component;
319 if (freq_in == 0 || freq_out == 0) {
320 /* Clock CODEC directly from MCLK */
321 reg = snd_soc_component_read(component, WM8510_CLOCK);
322 snd_soc_component_write(component, WM8510_CLOCK, reg & 0x0ff);
325 reg = snd_soc_component_read(component, WM8510_POWER1);
326 snd_soc_component_write(component, WM8510_POWER1, reg & 0x1df);
330 pll_factors(freq_out*4, freq_in);
332 snd_soc_component_write(component, WM8510_PLLN, (pll_div.pre_div << 4) | pll_div.n);
333 snd_soc_component_write(component, WM8510_PLLK1, pll_div.k >> 18);
334 snd_soc_component_write(component, WM8510_PLLK2, (pll_div.k >> 9) & 0x1ff);
335 snd_soc_component_write(component, WM8510_PLLK3, pll_div.k & 0x1ff);
336 reg = snd_soc_component_read(component, WM8510_POWER1);
337 snd_soc_component_write(component, WM8510_POWER1, reg | 0x020);
339 /* Run CODEC from PLL instead of MCLK */
340 reg = snd_soc_component_read(component, WM8510_CLOCK);
341 snd_soc_component_write(component, WM8510_CLOCK, reg | 0x100);
347 * Configure WM8510 clock dividers.
349 static int wm8510_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
352 struct snd_soc_component *component = codec_dai->component;
356 case WM8510_OPCLKDIV:
357 reg = snd_soc_component_read(component, WM8510_GPIO) & 0x1cf;
358 snd_soc_component_write(component, WM8510_GPIO, reg | div);
361 reg = snd_soc_component_read(component, WM8510_CLOCK) & 0x11f;
362 snd_soc_component_write(component, WM8510_CLOCK, reg | div);
365 reg = snd_soc_component_read(component, WM8510_ADC) & 0x1f7;
366 snd_soc_component_write(component, WM8510_ADC, reg | div);
369 reg = snd_soc_component_read(component, WM8510_DAC) & 0x1f7;
370 snd_soc_component_write(component, WM8510_DAC, reg | div);
373 reg = snd_soc_component_read(component, WM8510_CLOCK) & 0x1e3;
374 snd_soc_component_write(component, WM8510_CLOCK, reg | div);
383 static int wm8510_set_dai_fmt(struct snd_soc_dai *codec_dai,
386 struct snd_soc_component *component = codec_dai->component;
388 u16 clk = snd_soc_component_read(component, WM8510_CLOCK) & 0x1fe;
390 /* set master/slave audio interface */
391 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
392 case SND_SOC_DAIFMT_CBM_CFM:
395 case SND_SOC_DAIFMT_CBS_CFS:
401 /* interface format */
402 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
403 case SND_SOC_DAIFMT_I2S:
406 case SND_SOC_DAIFMT_RIGHT_J:
408 case SND_SOC_DAIFMT_LEFT_J:
411 case SND_SOC_DAIFMT_DSP_A:
418 /* clock inversion */
419 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
420 case SND_SOC_DAIFMT_NB_NF:
422 case SND_SOC_DAIFMT_IB_IF:
425 case SND_SOC_DAIFMT_IB_NF:
428 case SND_SOC_DAIFMT_NB_IF:
435 snd_soc_component_write(component, WM8510_IFACE, iface);
436 snd_soc_component_write(component, WM8510_CLOCK, clk);
440 static int wm8510_pcm_hw_params(struct snd_pcm_substream *substream,
441 struct snd_pcm_hw_params *params,
442 struct snd_soc_dai *dai)
444 struct snd_soc_component *component = dai->component;
445 u16 iface = snd_soc_component_read(component, WM8510_IFACE) & 0x19f;
446 u16 adn = snd_soc_component_read(component, WM8510_ADD) & 0x1f1;
449 switch (params_width(params)) {
463 /* filter coefficient */
464 switch (params_rate(params)) {
485 snd_soc_component_write(component, WM8510_IFACE, iface);
486 snd_soc_component_write(component, WM8510_ADD, adn);
490 static int wm8510_mute(struct snd_soc_dai *dai, int mute, int direction)
492 struct snd_soc_component *component = dai->component;
493 u16 mute_reg = snd_soc_component_read(component, WM8510_DAC) & 0xffbf;
496 snd_soc_component_write(component, WM8510_DAC, mute_reg | 0x40);
498 snd_soc_component_write(component, WM8510_DAC, mute_reg);
502 /* liam need to make this lower power with dapm */
503 static int wm8510_set_bias_level(struct snd_soc_component *component,
504 enum snd_soc_bias_level level)
506 struct wm8510_priv *wm8510 = snd_soc_component_get_drvdata(component);
507 u16 power1 = snd_soc_component_read(component, WM8510_POWER1) & ~0x3;
510 case SND_SOC_BIAS_ON:
511 case SND_SOC_BIAS_PREPARE:
512 power1 |= 0x1; /* VMID 50k */
513 snd_soc_component_write(component, WM8510_POWER1, power1);
516 case SND_SOC_BIAS_STANDBY:
517 power1 |= WM8510_POWER1_BIASEN | WM8510_POWER1_BUFIOEN;
519 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
520 regcache_sync(wm8510->regmap);
522 /* Initial cap charge at VMID 5k */
523 snd_soc_component_write(component, WM8510_POWER1, power1 | 0x3);
527 power1 |= 0x2; /* VMID 500k */
528 snd_soc_component_write(component, WM8510_POWER1, power1);
531 case SND_SOC_BIAS_OFF:
532 snd_soc_component_write(component, WM8510_POWER1, 0);
533 snd_soc_component_write(component, WM8510_POWER2, 0);
534 snd_soc_component_write(component, WM8510_POWER3, 0);
541 #define WM8510_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
542 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
543 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
545 #define WM8510_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
546 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
548 static const struct snd_soc_dai_ops wm8510_dai_ops = {
549 .hw_params = wm8510_pcm_hw_params,
550 .mute_stream = wm8510_mute,
551 .set_fmt = wm8510_set_dai_fmt,
552 .set_clkdiv = wm8510_set_dai_clkdiv,
553 .set_pll = wm8510_set_dai_pll,
554 .no_capture_mute = 1,
557 static struct snd_soc_dai_driver wm8510_dai = {
558 .name = "wm8510-hifi",
560 .stream_name = "Playback",
563 .rates = WM8510_RATES,
564 .formats = WM8510_FORMATS,},
566 .stream_name = "Capture",
569 .rates = WM8510_RATES,
570 .formats = WM8510_FORMATS,},
571 .ops = &wm8510_dai_ops,
572 .symmetric_rates = 1,
575 static int wm8510_probe(struct snd_soc_component *component)
577 wm8510_reset(component);
582 static const struct snd_soc_component_driver soc_component_dev_wm8510 = {
583 .probe = wm8510_probe,
584 .set_bias_level = wm8510_set_bias_level,
585 .controls = wm8510_snd_controls,
586 .num_controls = ARRAY_SIZE(wm8510_snd_controls),
587 .dapm_widgets = wm8510_dapm_widgets,
588 .num_dapm_widgets = ARRAY_SIZE(wm8510_dapm_widgets),
589 .dapm_routes = wm8510_dapm_routes,
590 .num_dapm_routes = ARRAY_SIZE(wm8510_dapm_routes),
591 .suspend_bias_off = 1,
593 .use_pmdown_time = 1,
595 .non_legacy_dai_naming = 1,
598 static const struct of_device_id wm8510_of_match[] = {
599 { .compatible = "wlf,wm8510" },
602 MODULE_DEVICE_TABLE(of, wm8510_of_match);
604 static const struct regmap_config wm8510_regmap = {
607 .max_register = WM8510_MONOMIX,
609 .reg_defaults = wm8510_reg_defaults,
610 .num_reg_defaults = ARRAY_SIZE(wm8510_reg_defaults),
611 .cache_type = REGCACHE_RBTREE,
613 .volatile_reg = wm8510_volatile,
616 #if defined(CONFIG_SPI_MASTER)
617 static int wm8510_spi_probe(struct spi_device *spi)
619 struct wm8510_priv *wm8510;
622 wm8510 = devm_kzalloc(&spi->dev, sizeof(struct wm8510_priv),
627 wm8510->regmap = devm_regmap_init_spi(spi, &wm8510_regmap);
628 if (IS_ERR(wm8510->regmap))
629 return PTR_ERR(wm8510->regmap);
631 spi_set_drvdata(spi, wm8510);
633 ret = devm_snd_soc_register_component(&spi->dev,
634 &soc_component_dev_wm8510, &wm8510_dai, 1);
639 static struct spi_driver wm8510_spi_driver = {
642 .of_match_table = wm8510_of_match,
644 .probe = wm8510_spi_probe,
646 #endif /* CONFIG_SPI_MASTER */
648 #if IS_ENABLED(CONFIG_I2C)
649 static int wm8510_i2c_probe(struct i2c_client *i2c,
650 const struct i2c_device_id *id)
652 struct wm8510_priv *wm8510;
655 wm8510 = devm_kzalloc(&i2c->dev, sizeof(struct wm8510_priv),
660 wm8510->regmap = devm_regmap_init_i2c(i2c, &wm8510_regmap);
661 if (IS_ERR(wm8510->regmap))
662 return PTR_ERR(wm8510->regmap);
664 i2c_set_clientdata(i2c, wm8510);
666 ret = devm_snd_soc_register_component(&i2c->dev,
667 &soc_component_dev_wm8510, &wm8510_dai, 1);
672 static const struct i2c_device_id wm8510_i2c_id[] = {
676 MODULE_DEVICE_TABLE(i2c, wm8510_i2c_id);
678 static struct i2c_driver wm8510_i2c_driver = {
681 .of_match_table = wm8510_of_match,
683 .probe = wm8510_i2c_probe,
684 .id_table = wm8510_i2c_id,
688 static int __init wm8510_modinit(void)
691 #if IS_ENABLED(CONFIG_I2C)
692 ret = i2c_add_driver(&wm8510_i2c_driver);
694 printk(KERN_ERR "Failed to register WM8510 I2C driver: %d\n",
698 #if defined(CONFIG_SPI_MASTER)
699 ret = spi_register_driver(&wm8510_spi_driver);
701 printk(KERN_ERR "Failed to register WM8510 SPI driver: %d\n",
707 module_init(wm8510_modinit);
709 static void __exit wm8510_exit(void)
711 #if IS_ENABLED(CONFIG_I2C)
712 i2c_del_driver(&wm8510_i2c_driver);
714 #if defined(CONFIG_SPI_MASTER)
715 spi_unregister_driver(&wm8510_spi_driver);
718 module_exit(wm8510_exit);
720 MODULE_DESCRIPTION("ASoC WM8510 driver");
721 MODULE_AUTHOR("Liam Girdwood");
722 MODULE_LICENSE("GPL");