1 // SPDX-License-Identifier: GPL-2.0-only
3 * wm8731.c -- WM8731 ALSA SoC Audio driver
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright 2006-12 Wolfson Microelectronics, plc
10 * Based on wm8753.c by Liam Girdwood
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/regmap.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/clk.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 static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = {
39 * wm8731 register cache
41 static const struct reg_default wm8731_reg_defaults[] = {
54 static bool wm8731_volatile(struct device *dev, unsigned int reg)
56 return reg == WM8731_RESET;
59 #define wm8731_reset(m) regmap_write(m, WM8731_RESET, 0)
61 static const char *wm8731_input_select[] = {"Line In", "Mic"};
63 static SOC_ENUM_SINGLE_DECL(wm8731_insel_enum,
64 WM8731_APANA, 2, wm8731_input_select);
66 static int wm8731_deemph[] = { 0, 32000, 44100, 48000 };
68 static int wm8731_set_deemph(struct snd_soc_component *component)
70 struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
73 /* If we're using deemphasis select the nearest available sample
78 for (i = 2; i < ARRAY_SIZE(wm8731_deemph); i++) {
79 if (abs(wm8731_deemph[i] - wm8731->playback_fs) <
80 abs(wm8731_deemph[best] - wm8731->playback_fs))
90 dev_dbg(component->dev, "Set deemphasis %d (%dHz)\n",
91 best, wm8731_deemph[best]);
93 return snd_soc_component_update_bits(component, WM8731_APDIGI, 0x6, val);
96 static int wm8731_get_deemph(struct snd_kcontrol *kcontrol,
97 struct snd_ctl_elem_value *ucontrol)
99 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
100 struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
102 ucontrol->value.integer.value[0] = wm8731->deemph;
107 static int wm8731_put_deemph(struct snd_kcontrol *kcontrol,
108 struct snd_ctl_elem_value *ucontrol)
110 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
111 struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
112 unsigned int deemph = ucontrol->value.integer.value[0];
118 mutex_lock(&wm8731->lock);
119 if (wm8731->deemph != deemph) {
120 wm8731->deemph = deemph;
122 wm8731_set_deemph(component);
126 mutex_unlock(&wm8731->lock);
131 static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0);
132 static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0);
133 static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
134 static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 2000, 0);
136 static const struct snd_kcontrol_new wm8731_snd_controls[] = {
138 SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
140 SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
143 SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0,
145 SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
147 SOC_SINGLE_TLV("Mic Boost Volume", WM8731_APANA, 0, 1, 0, mic_tlv),
148 SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1),
150 SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1,
153 SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
154 SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
156 SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0,
157 wm8731_get_deemph, wm8731_put_deemph),
161 static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
162 SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
163 SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
164 SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
168 static const struct snd_kcontrol_new wm8731_input_mux_controls =
169 SOC_DAPM_ENUM("Input Select", wm8731_insel_enum);
171 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
172 SND_SOC_DAPM_SUPPLY("ACTIVE",WM8731_ACTIVE, 0, 0, NULL, 0),
173 SND_SOC_DAPM_SUPPLY("OSC", WM8731_PWR, 5, 1, NULL, 0),
174 SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
175 &wm8731_output_mixer_controls[0],
176 ARRAY_SIZE(wm8731_output_mixer_controls)),
177 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
178 SND_SOC_DAPM_OUTPUT("LOUT"),
179 SND_SOC_DAPM_OUTPUT("LHPOUT"),
180 SND_SOC_DAPM_OUTPUT("ROUT"),
181 SND_SOC_DAPM_OUTPUT("RHPOUT"),
182 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
183 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
184 SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
185 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
186 SND_SOC_DAPM_INPUT("MICIN"),
187 SND_SOC_DAPM_INPUT("RLINEIN"),
188 SND_SOC_DAPM_INPUT("LLINEIN"),
191 static int wm8731_check_osc(struct snd_soc_dapm_widget *source,
192 struct snd_soc_dapm_widget *sink)
194 struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm);
195 struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
197 return wm8731->sysclk_type == WM8731_SYSCLK_XTAL;
200 static const struct snd_soc_dapm_route wm8731_intercon[] = {
201 {"DAC", NULL, "OSC", wm8731_check_osc},
202 {"ADC", NULL, "OSC", wm8731_check_osc},
203 {"DAC", NULL, "ACTIVE"},
204 {"ADC", NULL, "ACTIVE"},
207 {"Output Mixer", "Line Bypass Switch", "Line Input"},
208 {"Output Mixer", "HiFi Playback Switch", "DAC"},
209 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
212 {"RHPOUT", NULL, "Output Mixer"},
213 {"ROUT", NULL, "Output Mixer"},
214 {"LHPOUT", NULL, "Output Mixer"},
215 {"LOUT", NULL, "Output Mixer"},
218 {"Input Mux", "Line In", "Line Input"},
219 {"Input Mux", "Mic", "Mic Bias"},
220 {"ADC", NULL, "Input Mux"},
223 {"Line Input", NULL, "LLINEIN"},
224 {"Line Input", NULL, "RLINEIN"},
225 {"Mic Bias", NULL, "MICIN"},
237 /* codec mclk clock divider coefficients */
238 static const struct _coeff_div coeff_div[] = {
240 {12288000, 48000, 256, 0x0, 0x0, 0x0},
241 {18432000, 48000, 384, 0x0, 0x1, 0x0},
242 {12000000, 48000, 250, 0x0, 0x0, 0x1},
245 {12288000, 32000, 384, 0x6, 0x0, 0x0},
246 {18432000, 32000, 576, 0x6, 0x1, 0x0},
247 {12000000, 32000, 375, 0x6, 0x0, 0x1},
250 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
251 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
252 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
253 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
254 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
257 {12288000, 96000, 128, 0x7, 0x0, 0x0},
258 {18432000, 96000, 192, 0x7, 0x1, 0x0},
259 {12000000, 96000, 125, 0x7, 0x0, 0x1},
262 {11289600, 44100, 256, 0x8, 0x0, 0x0},
263 {16934400, 44100, 384, 0x8, 0x1, 0x0},
264 {12000000, 44100, 272, 0x8, 0x1, 0x1},
267 {11289600, 88200, 128, 0xf, 0x0, 0x0},
268 {16934400, 88200, 192, 0xf, 0x1, 0x0},
269 {12000000, 88200, 136, 0xf, 0x1, 0x1},
272 /* rates constraints */
273 static const unsigned int wm8731_rates_12000000[] = {
274 8000, 32000, 44100, 48000, 96000, 88200,
277 static const unsigned int wm8731_rates_12288000_18432000[] = {
278 8000, 32000, 48000, 96000,
281 static const unsigned int wm8731_rates_11289600_16934400[] = {
285 static const struct snd_pcm_hw_constraint_list wm8731_constraints_12000000 = {
286 .list = wm8731_rates_12000000,
287 .count = ARRAY_SIZE(wm8731_rates_12000000),
291 struct snd_pcm_hw_constraint_list wm8731_constraints_12288000_18432000 = {
292 .list = wm8731_rates_12288000_18432000,
293 .count = ARRAY_SIZE(wm8731_rates_12288000_18432000),
297 struct snd_pcm_hw_constraint_list wm8731_constraints_11289600_16934400 = {
298 .list = wm8731_rates_11289600_16934400,
299 .count = ARRAY_SIZE(wm8731_rates_11289600_16934400),
302 static inline int get_coeff(int mclk, int rate)
306 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
307 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
313 static int wm8731_hw_params(struct snd_pcm_substream *substream,
314 struct snd_pcm_hw_params *params,
315 struct snd_soc_dai *dai)
317 struct snd_soc_component *component = dai->component;
318 struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
319 u16 iface = snd_soc_component_read(component, WM8731_IFACE) & 0xfff3;
320 int i = get_coeff(wm8731->sysclk, params_rate(params));
321 u16 srate = (coeff_div[i].sr << 2) |
322 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
324 wm8731->playback_fs = params_rate(params);
326 snd_soc_component_write(component, WM8731_SRATE, srate);
329 switch (params_width(params)) {
343 wm8731_set_deemph(component);
345 snd_soc_component_write(component, WM8731_IFACE, iface);
349 static int wm8731_mute(struct snd_soc_dai *dai, int mute, int direction)
351 struct snd_soc_component *component = dai->component;
352 u16 mute_reg = snd_soc_component_read(component, WM8731_APDIGI) & 0xfff7;
355 snd_soc_component_write(component, WM8731_APDIGI, mute_reg | 0x8);
357 snd_soc_component_write(component, WM8731_APDIGI, mute_reg);
361 static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
362 int clk_id, unsigned int freq, int dir)
364 struct snd_soc_component *component = codec_dai->component;
365 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
366 struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
369 case WM8731_SYSCLK_XTAL:
370 case WM8731_SYSCLK_MCLK:
371 if (wm8731->mclk && clk_set_rate(wm8731->mclk, freq))
373 wm8731->sysclk_type = clk_id;
381 wm8731->constraints = NULL;
384 wm8731->constraints = &wm8731_constraints_12000000;
388 wm8731->constraints = &wm8731_constraints_12288000_18432000;
392 wm8731->constraints = &wm8731_constraints_11289600_16934400;
398 wm8731->sysclk = freq;
400 snd_soc_dapm_sync(dapm);
406 static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
409 struct snd_soc_component *component = codec_dai->component;
412 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
413 case SND_SOC_DAIFMT_CBP_CFP:
416 case SND_SOC_DAIFMT_CBC_CFC:
422 /* interface format */
423 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
424 case SND_SOC_DAIFMT_I2S:
427 case SND_SOC_DAIFMT_RIGHT_J:
429 case SND_SOC_DAIFMT_LEFT_J:
432 case SND_SOC_DAIFMT_DSP_A:
435 case SND_SOC_DAIFMT_DSP_B:
442 /* clock inversion */
443 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
444 case SND_SOC_DAIFMT_NB_NF:
446 case SND_SOC_DAIFMT_IB_IF:
449 case SND_SOC_DAIFMT_IB_NF:
452 case SND_SOC_DAIFMT_NB_IF:
460 snd_soc_component_write(component, WM8731_IFACE, iface);
464 static int wm8731_set_bias_level(struct snd_soc_component *component,
465 enum snd_soc_bias_level level)
467 struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(component);
472 case SND_SOC_BIAS_ON:
474 ret = clk_prepare_enable(wm8731->mclk);
479 case SND_SOC_BIAS_PREPARE:
481 case SND_SOC_BIAS_STANDBY:
482 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) {
483 ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
488 regcache_sync(wm8731->regmap);
491 /* Clear PWROFF, gate CLKOUT, everything else as-is */
492 reg = snd_soc_component_read(component, WM8731_PWR) & 0xff7f;
493 snd_soc_component_write(component, WM8731_PWR, reg | 0x0040);
495 case SND_SOC_BIAS_OFF:
497 clk_disable_unprepare(wm8731->mclk);
498 snd_soc_component_write(component, WM8731_PWR, 0xffff);
499 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies),
501 regcache_mark_dirty(wm8731->regmap);
507 static int wm8731_startup(struct snd_pcm_substream *substream,
508 struct snd_soc_dai *dai)
510 struct wm8731_priv *wm8731 = snd_soc_component_get_drvdata(dai->component);
512 if (wm8731->constraints)
513 snd_pcm_hw_constraint_list(substream->runtime, 0,
514 SNDRV_PCM_HW_PARAM_RATE,
515 wm8731->constraints);
520 #define WM8731_RATES SNDRV_PCM_RATE_8000_96000
522 #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
523 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
525 static const struct snd_soc_dai_ops wm8731_dai_ops = {
526 .startup = wm8731_startup,
527 .hw_params = wm8731_hw_params,
528 .mute_stream = wm8731_mute,
529 .set_sysclk = wm8731_set_dai_sysclk,
530 .set_fmt = wm8731_set_dai_fmt,
531 .no_capture_mute = 1,
534 static struct snd_soc_dai_driver wm8731_dai = {
535 .name = "wm8731-hifi",
537 .stream_name = "Playback",
540 .rates = WM8731_RATES,
541 .formats = WM8731_FORMATS,},
543 .stream_name = "Capture",
546 .rates = WM8731_RATES,
547 .formats = WM8731_FORMATS,},
548 .ops = &wm8731_dai_ops,
552 static const struct snd_soc_component_driver soc_component_dev_wm8731 = {
553 .set_bias_level = wm8731_set_bias_level,
554 .controls = wm8731_snd_controls,
555 .num_controls = ARRAY_SIZE(wm8731_snd_controls),
556 .dapm_widgets = wm8731_dapm_widgets,
557 .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
558 .dapm_routes = wm8731_intercon,
559 .num_dapm_routes = ARRAY_SIZE(wm8731_intercon),
560 .suspend_bias_off = 1,
562 .use_pmdown_time = 1,
566 int wm8731_init(struct device *dev, struct wm8731_priv *wm8731)
570 wm8731->mclk = devm_clk_get(dev, "mclk");
571 if (IS_ERR(wm8731->mclk)) {
572 ret = PTR_ERR(wm8731->mclk);
573 if (ret == -ENOENT) {
575 dev_warn(dev, "Assuming static MCLK\n");
577 dev_err(dev, "Failed to get MCLK: %d\n", ret);
582 mutex_init(&wm8731->lock);
584 for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++)
585 wm8731->supplies[i].supply = wm8731_supply_names[i];
587 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(wm8731->supplies),
590 dev_err(dev, "Failed to request supplies: %d\n", ret);
594 ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
597 dev_err(dev, "Failed to enable supplies: %d\n", ret);
601 ret = wm8731_reset(wm8731->regmap);
603 dev_err(dev, "Failed to issue reset: %d\n", ret);
604 goto err_regulator_enable;
607 /* Clear POWEROFF, keep everything else disabled */
608 regmap_write(wm8731->regmap, WM8731_PWR, 0x7f);
610 /* Latch the update bits */
611 regmap_update_bits(wm8731->regmap, WM8731_LOUT1V, 0x100, 0);
612 regmap_update_bits(wm8731->regmap, WM8731_ROUT1V, 0x100, 0);
613 regmap_update_bits(wm8731->regmap, WM8731_LINVOL, 0x100, 0);
614 regmap_update_bits(wm8731->regmap, WM8731_RINVOL, 0x100, 0);
616 /* Disable bypass path by default */
617 regmap_update_bits(wm8731->regmap, WM8731_APANA, 0x8, 0);
619 regcache_mark_dirty(wm8731->regmap);
621 ret = devm_snd_soc_register_component(dev,
622 &soc_component_dev_wm8731, &wm8731_dai, 1);
624 dev_err(dev, "Failed to register CODEC: %d\n", ret);
625 goto err_regulator_enable;
630 err_regulator_enable:
631 /* Regulators will be enabled by bias management */
632 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
636 EXPORT_SYMBOL_GPL(wm8731_init);
638 const struct regmap_config wm8731_regmap = {
642 .max_register = WM8731_RESET,
643 .volatile_reg = wm8731_volatile,
645 .cache_type = REGCACHE_MAPLE,
646 .reg_defaults = wm8731_reg_defaults,
647 .num_reg_defaults = ARRAY_SIZE(wm8731_reg_defaults),
649 EXPORT_SYMBOL_GPL(wm8731_regmap);
651 MODULE_DESCRIPTION("ASoC WM8731 driver");
652 MODULE_AUTHOR("Richard Purdie");
653 MODULE_LICENSE("GPL");