2 * wm8741.c -- WM8741 ALSA SoC Audio driver
4 * Copyright 2010-1 Wolfson Microelectronics plc
6 * Author: Ian Lartey <ian@opensource.wolfsonmicro.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
19 #include <linux/i2c.h>
20 #include <linux/spi/spi.h>
21 #include <linux/regmap.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/slab.h>
24 #include <linux/of_device.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 #define WM8741_NUM_SUPPLIES 2
35 static const char *wm8741_supply_names[WM8741_NUM_SUPPLIES] = {
40 #define WM8741_NUM_RATES 6
42 /* codec private data */
44 struct wm8741_platform_data pdata;
45 struct regmap *regmap;
46 struct regulator_bulk_data supplies[WM8741_NUM_SUPPLIES];
48 const struct snd_pcm_hw_constraint_list *sysclk_constraints;
51 static const struct reg_default wm8741_reg_defaults[] = {
52 { 0, 0x0000 }, /* R0 - DACLLSB Attenuation */
53 { 1, 0x0000 }, /* R1 - DACLMSB Attenuation */
54 { 2, 0x0000 }, /* R2 - DACRLSB Attenuation */
55 { 3, 0x0000 }, /* R3 - DACRMSB Attenuation */
56 { 4, 0x0000 }, /* R4 - Volume Control */
57 { 5, 0x000A }, /* R5 - Format Control */
58 { 6, 0x0000 }, /* R6 - Filter Control */
59 { 7, 0x0000 }, /* R7 - Mode Control 1 */
60 { 8, 0x0002 }, /* R8 - Mode Control 2 */
61 { 32, 0x0002 }, /* R32 - ADDITONAL_CONTROL_1 */
64 static bool wm8741_readable(struct device *dev, unsigned int reg)
67 case WM8741_DACLLSB_ATTENUATION:
68 case WM8741_DACLMSB_ATTENUATION:
69 case WM8741_DACRLSB_ATTENUATION:
70 case WM8741_DACRMSB_ATTENUATION:
71 case WM8741_VOLUME_CONTROL:
72 case WM8741_FORMAT_CONTROL:
73 case WM8741_FILTER_CONTROL:
74 case WM8741_MODE_CONTROL_1:
75 case WM8741_MODE_CONTROL_2:
76 case WM8741_ADDITIONAL_CONTROL_1:
83 static int wm8741_reset(struct snd_soc_codec *codec)
85 return snd_soc_write(codec, WM8741_RESET, 0);
88 static const DECLARE_TLV_DB_SCALE(dac_tlv_fine, -12700, 13, 0);
89 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 400, 0);
91 static const struct snd_kcontrol_new wm8741_snd_controls_stereo[] = {
92 SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
93 WM8741_DACRLSB_ATTENUATION, 1, 255, 1, dac_tlv_fine),
94 SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION,
95 WM8741_DACRMSB_ATTENUATION, 0, 511, 1, dac_tlv),
98 static const struct snd_kcontrol_new wm8741_snd_controls_mono_left[] = {
99 SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
100 1, 255, 1, dac_tlv_fine),
101 SOC_SINGLE_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION,
105 static const struct snd_kcontrol_new wm8741_snd_controls_mono_right[] = {
106 SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACRLSB_ATTENUATION,
107 1, 255, 1, dac_tlv_fine),
108 SOC_SINGLE_TLV("Playback Volume", WM8741_DACRMSB_ATTENUATION,
112 static const struct snd_soc_dapm_widget wm8741_dapm_widgets[] = {
113 SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM, 0, 0),
114 SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM, 0, 0),
115 SND_SOC_DAPM_OUTPUT("VOUTLP"),
116 SND_SOC_DAPM_OUTPUT("VOUTLN"),
117 SND_SOC_DAPM_OUTPUT("VOUTRP"),
118 SND_SOC_DAPM_OUTPUT("VOUTRN"),
121 static const struct snd_soc_dapm_route wm8741_dapm_routes[] = {
122 { "VOUTLP", NULL, "DACL" },
123 { "VOUTLN", NULL, "DACL" },
124 { "VOUTRP", NULL, "DACR" },
125 { "VOUTRN", NULL, "DACR" },
131 } lrclk_ratios[WM8741_NUM_RATES] = {
140 static const unsigned int rates_11289[] = {
144 static const struct snd_pcm_hw_constraint_list constraints_11289 = {
145 .count = ARRAY_SIZE(rates_11289),
149 static const unsigned int rates_12288[] = {
153 static const struct snd_pcm_hw_constraint_list constraints_12288 = {
154 .count = ARRAY_SIZE(rates_12288),
158 static const unsigned int rates_16384[] = {
162 static const struct snd_pcm_hw_constraint_list constraints_16384 = {
163 .count = ARRAY_SIZE(rates_16384),
167 static const unsigned int rates_16934[] = {
171 static const struct snd_pcm_hw_constraint_list constraints_16934 = {
172 .count = ARRAY_SIZE(rates_16934),
176 static const unsigned int rates_18432[] = {
180 static const struct snd_pcm_hw_constraint_list constraints_18432 = {
181 .count = ARRAY_SIZE(rates_18432),
185 static const unsigned int rates_22579[] = {
189 static const struct snd_pcm_hw_constraint_list constraints_22579 = {
190 .count = ARRAY_SIZE(rates_22579),
194 static const unsigned int rates_24576[] = {
195 32000, 48000, 96000, 192000
198 static const struct snd_pcm_hw_constraint_list constraints_24576 = {
199 .count = ARRAY_SIZE(rates_24576),
203 static const unsigned int rates_36864[] = {
207 static const struct snd_pcm_hw_constraint_list constraints_36864 = {
208 .count = ARRAY_SIZE(rates_36864),
213 static int wm8741_startup(struct snd_pcm_substream *substream,
214 struct snd_soc_dai *dai)
216 struct snd_soc_codec *codec = dai->codec;
217 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
219 /* The set of sample rates that can be supported depends on the
220 * MCLK supplied to the CODEC - enforce this.
222 if (!wm8741->sysclk) {
224 "No MCLK configured, call set_sysclk() on init\n");
228 snd_pcm_hw_constraint_list(substream->runtime, 0,
229 SNDRV_PCM_HW_PARAM_RATE,
230 wm8741->sysclk_constraints);
235 static int wm8741_hw_params(struct snd_pcm_substream *substream,
236 struct snd_pcm_hw_params *params,
237 struct snd_soc_dai *dai)
239 struct snd_soc_codec *codec = dai->codec;
240 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
241 u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1FC;
244 /* Find a supported LRCLK ratio */
245 for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
246 if (wm8741->sysclk / params_rate(params) ==
247 lrclk_ratios[i].ratio)
251 /* Should never happen, should be handled by constraints */
252 if (i == ARRAY_SIZE(lrclk_ratios)) {
253 dev_err(codec->dev, "MCLK/fs ratio %d unsupported\n",
254 wm8741->sysclk / params_rate(params));
259 switch (params_width(params)) {
272 dev_dbg(codec->dev, "wm8741_hw_params: Unsupported bit size param = %d",
273 params_width(params));
277 dev_dbg(codec->dev, "wm8741_hw_params: bit size param = %d",
278 params_width(params));
280 snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface);
284 static int wm8741_set_dai_sysclk(struct snd_soc_dai *codec_dai,
285 int clk_id, unsigned int freq, int dir)
287 struct snd_soc_codec *codec = codec_dai->codec;
288 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
290 dev_dbg(codec->dev, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq);
294 wm8741->sysclk_constraints = &constraints_11289;
295 wm8741->sysclk = freq;
299 wm8741->sysclk_constraints = &constraints_12288;
300 wm8741->sysclk = freq;
304 wm8741->sysclk_constraints = &constraints_16384;
305 wm8741->sysclk = freq;
309 wm8741->sysclk_constraints = &constraints_16934;
310 wm8741->sysclk = freq;
314 wm8741->sysclk_constraints = &constraints_18432;
315 wm8741->sysclk = freq;
320 wm8741->sysclk_constraints = &constraints_22579;
321 wm8741->sysclk = freq;
325 wm8741->sysclk_constraints = &constraints_24576;
326 wm8741->sysclk = freq;
330 wm8741->sysclk_constraints = &constraints_36864;
331 wm8741->sysclk = freq;
337 static int wm8741_set_dai_fmt(struct snd_soc_dai *codec_dai,
340 struct snd_soc_codec *codec = codec_dai->codec;
341 u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1C3;
343 /* check master/slave audio interface */
344 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
345 case SND_SOC_DAIFMT_CBS_CFS:
351 /* interface format */
352 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
353 case SND_SOC_DAIFMT_I2S:
356 case SND_SOC_DAIFMT_RIGHT_J:
358 case SND_SOC_DAIFMT_LEFT_J:
361 case SND_SOC_DAIFMT_DSP_A:
364 case SND_SOC_DAIFMT_DSP_B:
371 /* clock inversion */
372 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
373 case SND_SOC_DAIFMT_NB_NF:
375 case SND_SOC_DAIFMT_IB_IF:
378 case SND_SOC_DAIFMT_IB_NF:
381 case SND_SOC_DAIFMT_NB_IF:
389 dev_dbg(codec->dev, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
390 fmt & SND_SOC_DAIFMT_FORMAT_MASK,
391 ((fmt & SND_SOC_DAIFMT_INV_MASK)));
393 snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface);
397 #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
398 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
399 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
400 SNDRV_PCM_RATE_192000)
402 #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
403 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
405 static const struct snd_soc_dai_ops wm8741_dai_ops = {
406 .startup = wm8741_startup,
407 .hw_params = wm8741_hw_params,
408 .set_sysclk = wm8741_set_dai_sysclk,
409 .set_fmt = wm8741_set_dai_fmt,
412 static struct snd_soc_dai_driver wm8741_dai = {
415 .stream_name = "Playback",
418 .rates = WM8741_RATES,
419 .formats = WM8741_FORMATS,
421 .ops = &wm8741_dai_ops,
425 static int wm8741_resume(struct snd_soc_codec *codec)
427 snd_soc_cache_sync(codec);
431 #define wm8741_resume NULL
434 static int wm8741_configure(struct snd_soc_codec *codec)
436 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
438 /* Configure differential mode */
439 switch (wm8741->pdata.diff_mode) {
440 case WM8741_DIFF_MODE_STEREO:
441 case WM8741_DIFF_MODE_STEREO_REVERSED:
442 case WM8741_DIFF_MODE_MONO_LEFT:
443 case WM8741_DIFF_MODE_MONO_RIGHT:
444 snd_soc_update_bits(codec, WM8741_MODE_CONTROL_2,
446 wm8741->pdata.diff_mode << WM8741_DIFF_SHIFT);
452 /* Change some default settings - latch VU */
453 snd_soc_update_bits(codec, WM8741_DACLLSB_ATTENUATION,
454 WM8741_UPDATELL, WM8741_UPDATELL);
455 snd_soc_update_bits(codec, WM8741_DACLMSB_ATTENUATION,
456 WM8741_UPDATELM, WM8741_UPDATELM);
457 snd_soc_update_bits(codec, WM8741_DACRLSB_ATTENUATION,
458 WM8741_UPDATERL, WM8741_UPDATERL);
459 snd_soc_update_bits(codec, WM8741_DACRMSB_ATTENUATION,
460 WM8741_UPDATERM, WM8741_UPDATERM);
465 static int wm8741_add_controls(struct snd_soc_codec *codec)
467 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
469 switch (wm8741->pdata.diff_mode) {
470 case WM8741_DIFF_MODE_STEREO:
471 case WM8741_DIFF_MODE_STEREO_REVERSED:
472 snd_soc_add_codec_controls(codec,
473 wm8741_snd_controls_stereo,
474 ARRAY_SIZE(wm8741_snd_controls_stereo));
476 case WM8741_DIFF_MODE_MONO_LEFT:
477 snd_soc_add_codec_controls(codec,
478 wm8741_snd_controls_mono_left,
479 ARRAY_SIZE(wm8741_snd_controls_mono_left));
481 case WM8741_DIFF_MODE_MONO_RIGHT:
482 snd_soc_add_codec_controls(codec,
483 wm8741_snd_controls_mono_right,
484 ARRAY_SIZE(wm8741_snd_controls_mono_right));
493 static int wm8741_probe(struct snd_soc_codec *codec)
495 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
498 ret = regulator_bulk_enable(ARRAY_SIZE(wm8741->supplies),
501 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
505 ret = wm8741_reset(codec);
507 dev_err(codec->dev, "Failed to issue reset\n");
511 ret = wm8741_configure(codec);
513 dev_err(codec->dev, "Failed to change default settings\n");
517 ret = wm8741_add_controls(codec);
519 dev_err(codec->dev, "Failed to add controls\n");
523 dev_dbg(codec->dev, "Successful registration\n");
527 regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
532 static int wm8741_remove(struct snd_soc_codec *codec)
534 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
536 regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
541 static struct snd_soc_codec_driver soc_codec_dev_wm8741 = {
542 .probe = wm8741_probe,
543 .remove = wm8741_remove,
544 .resume = wm8741_resume,
546 .dapm_widgets = wm8741_dapm_widgets,
547 .num_dapm_widgets = ARRAY_SIZE(wm8741_dapm_widgets),
548 .dapm_routes = wm8741_dapm_routes,
549 .num_dapm_routes = ARRAY_SIZE(wm8741_dapm_routes),
552 static const struct of_device_id wm8741_of_match[] = {
553 { .compatible = "wlf,wm8741", },
556 MODULE_DEVICE_TABLE(of, wm8741_of_match);
558 static const struct regmap_config wm8741_regmap = {
561 .max_register = WM8741_MAX_REGISTER,
563 .reg_defaults = wm8741_reg_defaults,
564 .num_reg_defaults = ARRAY_SIZE(wm8741_reg_defaults),
565 .cache_type = REGCACHE_RBTREE,
567 .readable_reg = wm8741_readable,
570 static int wm8741_set_pdata(struct device *dev, struct wm8741_priv *wm8741)
572 const struct wm8741_platform_data *pdata = dev_get_platdata(dev);
576 if (of_property_read_u32(dev->of_node, "diff-mode", &diff_mode)
578 wm8741->pdata.diff_mode = diff_mode;
581 memcpy(&wm8741->pdata, pdata, sizeof(wm8741->pdata));
587 #if IS_ENABLED(CONFIG_I2C)
588 static int wm8741_i2c_probe(struct i2c_client *i2c,
589 const struct i2c_device_id *id)
591 struct wm8741_priv *wm8741;
594 wm8741 = devm_kzalloc(&i2c->dev, sizeof(struct wm8741_priv),
599 for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++)
600 wm8741->supplies[i].supply = wm8741_supply_names[i];
602 ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8741->supplies),
605 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
609 wm8741->regmap = devm_regmap_init_i2c(i2c, &wm8741_regmap);
610 if (IS_ERR(wm8741->regmap)) {
611 ret = PTR_ERR(wm8741->regmap);
612 dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
616 wm8741_set_pdata(&i2c->dev, wm8741);
618 dev_err(&i2c->dev, "Failed to set pdata: %d\n", ret);
622 i2c_set_clientdata(i2c, wm8741);
624 ret = snd_soc_register_codec(&i2c->dev,
625 &soc_codec_dev_wm8741, &wm8741_dai, 1);
630 static int wm8741_i2c_remove(struct i2c_client *client)
632 snd_soc_unregister_codec(&client->dev);
636 static const struct i2c_device_id wm8741_i2c_id[] = {
640 MODULE_DEVICE_TABLE(i2c, wm8741_i2c_id);
642 static struct i2c_driver wm8741_i2c_driver = {
645 .owner = THIS_MODULE,
646 .of_match_table = wm8741_of_match,
648 .probe = wm8741_i2c_probe,
649 .remove = wm8741_i2c_remove,
650 .id_table = wm8741_i2c_id,
654 #if defined(CONFIG_SPI_MASTER)
655 static int wm8741_spi_probe(struct spi_device *spi)
657 struct wm8741_priv *wm8741;
660 wm8741 = devm_kzalloc(&spi->dev, sizeof(struct wm8741_priv),
665 for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++)
666 wm8741->supplies[i].supply = wm8741_supply_names[i];
668 ret = devm_regulator_bulk_get(&spi->dev, ARRAY_SIZE(wm8741->supplies),
671 dev_err(&spi->dev, "Failed to request supplies: %d\n", ret);
675 wm8741->regmap = devm_regmap_init_spi(spi, &wm8741_regmap);
676 if (IS_ERR(wm8741->regmap)) {
677 ret = PTR_ERR(wm8741->regmap);
678 dev_err(&spi->dev, "Failed to init regmap: %d\n", ret);
682 wm8741_set_pdata(&spi->dev, wm8741);
684 dev_err(&spi->dev, "Failed to set pdata: %d\n", ret);
688 spi_set_drvdata(spi, wm8741);
690 ret = snd_soc_register_codec(&spi->dev,
691 &soc_codec_dev_wm8741, &wm8741_dai, 1);
695 static int wm8741_spi_remove(struct spi_device *spi)
697 snd_soc_unregister_codec(&spi->dev);
701 static struct spi_driver wm8741_spi_driver = {
704 .owner = THIS_MODULE,
705 .of_match_table = wm8741_of_match,
707 .probe = wm8741_spi_probe,
708 .remove = wm8741_spi_remove,
710 #endif /* CONFIG_SPI_MASTER */
712 static int __init wm8741_modinit(void)
716 #if IS_ENABLED(CONFIG_I2C)
717 ret = i2c_add_driver(&wm8741_i2c_driver);
719 pr_err("Failed to register WM8741 I2C driver: %d\n", ret);
721 #if defined(CONFIG_SPI_MASTER)
722 ret = spi_register_driver(&wm8741_spi_driver);
724 printk(KERN_ERR "Failed to register wm8741 SPI driver: %d\n",
731 module_init(wm8741_modinit);
733 static void __exit wm8741_exit(void)
735 #if defined(CONFIG_SPI_MASTER)
736 spi_unregister_driver(&wm8741_spi_driver);
738 #if IS_ENABLED(CONFIG_I2C)
739 i2c_del_driver(&wm8741_i2c_driver);
742 module_exit(wm8741_exit);
744 MODULE_DESCRIPTION("ASoC WM8741 driver");
745 MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
746 MODULE_LICENSE("GPL");