2 * wm8731.c -- WM8731 ALSA SoC Audio driver
4 * Copyright 2005 Openedhand Ltd.
5 * Copyright 2006-12 Wolfson Microelectronics, plc
7 * Author: Richard Purdie <richard@openedhand.com>
9 * Based on wm8753.c by Liam Girdwood
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/init.h>
19 #include <linux/delay.h>
21 #include <linux/i2c.h>
22 #include <linux/slab.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/spi/spi.h>
26 #include <linux/of_device.h>
27 #include <linux/mutex.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/initval.h>
33 #include <sound/tlv.h>
37 #define WM8731_NUM_SUPPLIES 4
38 static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = {
45 /* codec private data */
47 struct regmap *regmap;
48 struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES];
49 const struct snd_pcm_hw_constraint_list *constraints;
60 * wm8731 register cache
62 static const struct reg_default wm8731_reg_defaults[] = {
75 static bool wm8731_volatile(struct device *dev, unsigned int reg)
77 return reg == WM8731_RESET;
80 static bool wm8731_writeable(struct device *dev, unsigned int reg)
82 return reg <= WM8731_RESET;
85 #define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0)
87 static const char *wm8731_input_select[] = {"Line In", "Mic"};
89 static SOC_ENUM_SINGLE_DECL(wm8731_insel_enum,
90 WM8731_APANA, 2, wm8731_input_select);
92 static int wm8731_deemph[] = { 0, 32000, 44100, 48000 };
94 static int wm8731_set_deemph(struct snd_soc_codec *codec)
96 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
99 /* If we're using deemphasis select the nearest available sample
102 if (wm8731->deemph) {
104 for (i = 2; i < ARRAY_SIZE(wm8731_deemph); i++) {
105 if (abs(wm8731_deemph[i] - wm8731->playback_fs) <
106 abs(wm8731_deemph[best] - wm8731->playback_fs))
116 dev_dbg(codec->dev, "Set deemphasis %d (%dHz)\n",
117 best, wm8731_deemph[best]);
119 return snd_soc_update_bits(codec, WM8731_APDIGI, 0x6, val);
122 static int wm8731_get_deemph(struct snd_kcontrol *kcontrol,
123 struct snd_ctl_elem_value *ucontrol)
125 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
126 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
128 ucontrol->value.enumerated.item[0] = wm8731->deemph;
133 static int wm8731_put_deemph(struct snd_kcontrol *kcontrol,
134 struct snd_ctl_elem_value *ucontrol)
136 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
137 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
138 int deemph = ucontrol->value.enumerated.item[0];
144 mutex_lock(&wm8731->lock);
145 if (wm8731->deemph != deemph) {
146 wm8731->deemph = deemph;
148 wm8731_set_deemph(codec);
152 mutex_unlock(&wm8731->lock);
157 static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0);
158 static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0);
159 static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
160 static const DECLARE_TLV_DB_SCALE(mic_tlv, 0, 2000, 0);
162 static const struct snd_kcontrol_new wm8731_snd_controls[] = {
164 SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
166 SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
169 SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0,
171 SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
173 SOC_SINGLE_TLV("Mic Boost Volume", WM8731_APANA, 0, 1, 0, mic_tlv),
174 SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1),
176 SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1,
179 SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
180 SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
182 SOC_SINGLE_BOOL_EXT("Playback Deemphasis Switch", 0,
183 wm8731_get_deemph, wm8731_put_deemph),
187 static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
188 SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
189 SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
190 SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
194 static const struct snd_kcontrol_new wm8731_input_mux_controls =
195 SOC_DAPM_ENUM("Input Select", wm8731_insel_enum);
197 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
198 SND_SOC_DAPM_SUPPLY("ACTIVE",WM8731_ACTIVE, 0, 0, NULL, 0),
199 SND_SOC_DAPM_SUPPLY("OSC", WM8731_PWR, 5, 1, NULL, 0),
200 SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
201 &wm8731_output_mixer_controls[0],
202 ARRAY_SIZE(wm8731_output_mixer_controls)),
203 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
204 SND_SOC_DAPM_OUTPUT("LOUT"),
205 SND_SOC_DAPM_OUTPUT("LHPOUT"),
206 SND_SOC_DAPM_OUTPUT("ROUT"),
207 SND_SOC_DAPM_OUTPUT("RHPOUT"),
208 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
209 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
210 SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
211 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
212 SND_SOC_DAPM_INPUT("MICIN"),
213 SND_SOC_DAPM_INPUT("RLINEIN"),
214 SND_SOC_DAPM_INPUT("LLINEIN"),
217 static int wm8731_check_osc(struct snd_soc_dapm_widget *source,
218 struct snd_soc_dapm_widget *sink)
220 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
221 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
223 return wm8731->sysclk_type == WM8731_SYSCLK_XTAL;
226 static const struct snd_soc_dapm_route wm8731_intercon[] = {
227 {"DAC", NULL, "OSC", wm8731_check_osc},
228 {"ADC", NULL, "OSC", wm8731_check_osc},
229 {"DAC", NULL, "ACTIVE"},
230 {"ADC", NULL, "ACTIVE"},
233 {"Output Mixer", "Line Bypass Switch", "Line Input"},
234 {"Output Mixer", "HiFi Playback Switch", "DAC"},
235 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
238 {"RHPOUT", NULL, "Output Mixer"},
239 {"ROUT", NULL, "Output Mixer"},
240 {"LHPOUT", NULL, "Output Mixer"},
241 {"LOUT", NULL, "Output Mixer"},
244 {"Input Mux", "Line In", "Line Input"},
245 {"Input Mux", "Mic", "Mic Bias"},
246 {"ADC", NULL, "Input Mux"},
249 {"Line Input", NULL, "LLINEIN"},
250 {"Line Input", NULL, "RLINEIN"},
251 {"Mic Bias", NULL, "MICIN"},
263 /* codec mclk clock divider coefficients */
264 static const struct _coeff_div coeff_div[] = {
266 {12288000, 48000, 256, 0x0, 0x0, 0x0},
267 {18432000, 48000, 384, 0x0, 0x1, 0x0},
268 {12000000, 48000, 250, 0x0, 0x0, 0x1},
271 {12288000, 32000, 384, 0x6, 0x0, 0x0},
272 {18432000, 32000, 576, 0x6, 0x1, 0x0},
273 {12000000, 32000, 375, 0x6, 0x0, 0x1},
276 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
277 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
278 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
279 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
280 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
283 {12288000, 96000, 128, 0x7, 0x0, 0x0},
284 {18432000, 96000, 192, 0x7, 0x1, 0x0},
285 {12000000, 96000, 125, 0x7, 0x0, 0x1},
288 {11289600, 44100, 256, 0x8, 0x0, 0x0},
289 {16934400, 44100, 384, 0x8, 0x1, 0x0},
290 {12000000, 44100, 272, 0x8, 0x1, 0x1},
293 {11289600, 88200, 128, 0xf, 0x0, 0x0},
294 {16934400, 88200, 192, 0xf, 0x1, 0x0},
295 {12000000, 88200, 136, 0xf, 0x1, 0x1},
298 /* rates constraints */
299 static const unsigned int wm8731_rates_12000000[] = {
300 8000, 32000, 44100, 48000, 96000, 88200,
303 static const unsigned int wm8731_rates_12288000_18432000[] = {
304 8000, 32000, 48000, 96000,
307 static const unsigned int wm8731_rates_11289600_16934400[] = {
311 static const struct snd_pcm_hw_constraint_list wm8731_constraints_12000000 = {
312 .list = wm8731_rates_12000000,
313 .count = ARRAY_SIZE(wm8731_rates_12000000),
317 struct snd_pcm_hw_constraint_list wm8731_constraints_12288000_18432000 = {
318 .list = wm8731_rates_12288000_18432000,
319 .count = ARRAY_SIZE(wm8731_rates_12288000_18432000),
323 struct snd_pcm_hw_constraint_list wm8731_constraints_11289600_16934400 = {
324 .list = wm8731_rates_11289600_16934400,
325 .count = ARRAY_SIZE(wm8731_rates_11289600_16934400),
328 static inline int get_coeff(int mclk, int rate)
332 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
333 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
339 static int wm8731_hw_params(struct snd_pcm_substream *substream,
340 struct snd_pcm_hw_params *params,
341 struct snd_soc_dai *dai)
343 struct snd_soc_codec *codec = dai->codec;
344 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
345 u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3;
346 int i = get_coeff(wm8731->sysclk, params_rate(params));
347 u16 srate = (coeff_div[i].sr << 2) |
348 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
350 wm8731->playback_fs = params_rate(params);
352 snd_soc_write(codec, WM8731_SRATE, srate);
355 switch (params_width(params)) {
366 wm8731_set_deemph(codec);
368 snd_soc_write(codec, WM8731_IFACE, iface);
372 static int wm8731_mute(struct snd_soc_dai *dai, int mute)
374 struct snd_soc_codec *codec = dai->codec;
375 u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7;
378 snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8);
380 snd_soc_write(codec, WM8731_APDIGI, mute_reg);
384 static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
385 int clk_id, unsigned int freq, int dir)
387 struct snd_soc_codec *codec = codec_dai->codec;
388 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
391 case WM8731_SYSCLK_XTAL:
392 case WM8731_SYSCLK_MCLK:
393 wm8731->sysclk_type = clk_id;
401 wm8731->constraints = NULL;
404 wm8731->constraints = &wm8731_constraints_12000000;
408 wm8731->constraints = &wm8731_constraints_12288000_18432000;
412 wm8731->constraints = &wm8731_constraints_11289600_16934400;
418 wm8731->sysclk = freq;
420 snd_soc_dapm_sync(&codec->dapm);
426 static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
429 struct snd_soc_codec *codec = codec_dai->codec;
432 /* set master/slave audio interface */
433 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
434 case SND_SOC_DAIFMT_CBM_CFM:
437 case SND_SOC_DAIFMT_CBS_CFS:
443 /* interface format */
444 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
445 case SND_SOC_DAIFMT_I2S:
448 case SND_SOC_DAIFMT_RIGHT_J:
450 case SND_SOC_DAIFMT_LEFT_J:
453 case SND_SOC_DAIFMT_DSP_A:
456 case SND_SOC_DAIFMT_DSP_B:
463 /* clock inversion */
464 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
465 case SND_SOC_DAIFMT_NB_NF:
467 case SND_SOC_DAIFMT_IB_IF:
470 case SND_SOC_DAIFMT_IB_NF:
473 case SND_SOC_DAIFMT_NB_IF:
481 snd_soc_write(codec, WM8731_IFACE, iface);
485 static int wm8731_set_bias_level(struct snd_soc_codec *codec,
486 enum snd_soc_bias_level level)
488 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
493 case SND_SOC_BIAS_ON:
495 case SND_SOC_BIAS_PREPARE:
497 case SND_SOC_BIAS_STANDBY:
498 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
499 ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
504 regcache_sync(wm8731->regmap);
507 /* Clear PWROFF, gate CLKOUT, everything else as-is */
508 reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f;
509 snd_soc_write(codec, WM8731_PWR, reg | 0x0040);
511 case SND_SOC_BIAS_OFF:
512 snd_soc_write(codec, WM8731_PWR, 0xffff);
513 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies),
515 regcache_mark_dirty(wm8731->regmap);
518 codec->dapm.bias_level = level;
522 static int wm8731_startup(struct snd_pcm_substream *substream,
523 struct snd_soc_dai *dai)
525 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(dai->codec);
527 if (wm8731->constraints)
528 snd_pcm_hw_constraint_list(substream->runtime, 0,
529 SNDRV_PCM_HW_PARAM_RATE,
530 wm8731->constraints);
535 #define WM8731_RATES SNDRV_PCM_RATE_8000_96000
537 #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
538 SNDRV_PCM_FMTBIT_S24_LE)
540 static const struct snd_soc_dai_ops wm8731_dai_ops = {
541 .startup = wm8731_startup,
542 .hw_params = wm8731_hw_params,
543 .digital_mute = wm8731_mute,
544 .set_sysclk = wm8731_set_dai_sysclk,
545 .set_fmt = wm8731_set_dai_fmt,
548 static struct snd_soc_dai_driver wm8731_dai = {
549 .name = "wm8731-hifi",
551 .stream_name = "Playback",
554 .rates = WM8731_RATES,
555 .formats = WM8731_FORMATS,},
557 .stream_name = "Capture",
560 .rates = WM8731_RATES,
561 .formats = WM8731_FORMATS,},
562 .ops = &wm8731_dai_ops,
563 .symmetric_rates = 1,
566 static int wm8731_probe(struct snd_soc_codec *codec)
568 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
571 for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++)
572 wm8731->supplies[i].supply = wm8731_supply_names[i];
574 ret = devm_regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies),
577 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
581 ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
584 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
588 ret = wm8731_reset(codec);
590 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
591 goto err_regulator_enable;
594 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
596 /* Latch the update bits */
597 snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0);
598 snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0);
599 snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0);
600 snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0);
602 /* Disable bypass path by default */
603 snd_soc_update_bits(codec, WM8731_APANA, 0x8, 0);
605 /* Regulators will have been enabled by bias management */
606 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
610 err_regulator_enable:
611 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
616 /* power down chip */
617 static int wm8731_remove(struct snd_soc_codec *codec)
619 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
621 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
626 static struct snd_soc_codec_driver soc_codec_dev_wm8731 = {
627 .probe = wm8731_probe,
628 .remove = wm8731_remove,
629 .set_bias_level = wm8731_set_bias_level,
630 .suspend_bias_off = true,
632 .dapm_widgets = wm8731_dapm_widgets,
633 .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
634 .dapm_routes = wm8731_intercon,
635 .num_dapm_routes = ARRAY_SIZE(wm8731_intercon),
636 .controls = wm8731_snd_controls,
637 .num_controls = ARRAY_SIZE(wm8731_snd_controls),
640 static const struct of_device_id wm8731_of_match[] = {
641 { .compatible = "wlf,wm8731", },
645 MODULE_DEVICE_TABLE(of, wm8731_of_match);
647 static const struct regmap_config wm8731_regmap = {
651 .max_register = WM8731_RESET,
652 .volatile_reg = wm8731_volatile,
653 .writeable_reg = wm8731_writeable,
655 .cache_type = REGCACHE_RBTREE,
656 .reg_defaults = wm8731_reg_defaults,
657 .num_reg_defaults = ARRAY_SIZE(wm8731_reg_defaults),
660 #if defined(CONFIG_SPI_MASTER)
661 static int wm8731_spi_probe(struct spi_device *spi)
663 struct wm8731_priv *wm8731;
666 wm8731 = devm_kzalloc(&spi->dev, sizeof(*wm8731), GFP_KERNEL);
670 mutex_init(&wm8731->lock);
672 wm8731->regmap = devm_regmap_init_spi(spi, &wm8731_regmap);
673 if (IS_ERR(wm8731->regmap)) {
674 ret = PTR_ERR(wm8731->regmap);
675 dev_err(&spi->dev, "Failed to allocate register map: %d\n",
680 spi_set_drvdata(spi, wm8731);
682 ret = snd_soc_register_codec(&spi->dev,
683 &soc_codec_dev_wm8731, &wm8731_dai, 1);
685 dev_err(&spi->dev, "Failed to register CODEC: %d\n", ret);
692 static int wm8731_spi_remove(struct spi_device *spi)
694 snd_soc_unregister_codec(&spi->dev);
698 static struct spi_driver wm8731_spi_driver = {
701 .owner = THIS_MODULE,
702 .of_match_table = wm8731_of_match,
704 .probe = wm8731_spi_probe,
705 .remove = wm8731_spi_remove,
707 #endif /* CONFIG_SPI_MASTER */
709 #if IS_ENABLED(CONFIG_I2C)
710 static int wm8731_i2c_probe(struct i2c_client *i2c,
711 const struct i2c_device_id *id)
713 struct wm8731_priv *wm8731;
716 wm8731 = devm_kzalloc(&i2c->dev, sizeof(struct wm8731_priv),
721 mutex_init(&wm8731->lock);
723 wm8731->regmap = devm_regmap_init_i2c(i2c, &wm8731_regmap);
724 if (IS_ERR(wm8731->regmap)) {
725 ret = PTR_ERR(wm8731->regmap);
726 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
731 i2c_set_clientdata(i2c, wm8731);
733 ret = snd_soc_register_codec(&i2c->dev,
734 &soc_codec_dev_wm8731, &wm8731_dai, 1);
736 dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
743 static int wm8731_i2c_remove(struct i2c_client *client)
745 snd_soc_unregister_codec(&client->dev);
749 static const struct i2c_device_id wm8731_i2c_id[] = {
753 MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
755 static struct i2c_driver wm8731_i2c_driver = {
758 .owner = THIS_MODULE,
759 .of_match_table = wm8731_of_match,
761 .probe = wm8731_i2c_probe,
762 .remove = wm8731_i2c_remove,
763 .id_table = wm8731_i2c_id,
767 static int __init wm8731_modinit(void)
770 #if IS_ENABLED(CONFIG_I2C)
771 ret = i2c_add_driver(&wm8731_i2c_driver);
773 printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
777 #if defined(CONFIG_SPI_MASTER)
778 ret = spi_register_driver(&wm8731_spi_driver);
780 printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
786 module_init(wm8731_modinit);
788 static void __exit wm8731_exit(void)
790 #if IS_ENABLED(CONFIG_I2C)
791 i2c_del_driver(&wm8731_i2c_driver);
793 #if defined(CONFIG_SPI_MASTER)
794 spi_unregister_driver(&wm8731_spi_driver);
797 module_exit(wm8731_exit);
799 MODULE_DESCRIPTION("ASoC WM8731 driver");
800 MODULE_AUTHOR("Richard Purdie");
801 MODULE_LICENSE("GPL");