1 // SPDX-License-Identifier: GPL-2.0-only
3 * wm8741.c -- WM8741 ALSA SoC Audio driver
5 * Copyright 2010-1 Wolfson Microelectronics plc
7 * Author: Ian Lartey <ian@opensource.wolfsonmicro.com>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
15 #include <linux/i2c.h>
16 #include <linux/spi/spi.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/slab.h>
20 #include <linux/of_device.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>
26 #include <sound/tlv.h>
30 #define WM8741_NUM_SUPPLIES 2
31 static const char *wm8741_supply_names
[WM8741_NUM_SUPPLIES
] = {
36 /* codec private data */
38 struct wm8741_platform_data pdata
;
39 struct regmap
*regmap
;
40 struct regulator_bulk_data supplies
[WM8741_NUM_SUPPLIES
];
42 const struct snd_pcm_hw_constraint_list
*sysclk_constraints
;
45 static const struct reg_default wm8741_reg_defaults
[] = {
46 { 0, 0x0000 }, /* R0 - DACLLSB Attenuation */
47 { 1, 0x0000 }, /* R1 - DACLMSB Attenuation */
48 { 2, 0x0000 }, /* R2 - DACRLSB Attenuation */
49 { 3, 0x0000 }, /* R3 - DACRMSB Attenuation */
50 { 4, 0x0000 }, /* R4 - Volume Control */
51 { 5, 0x000A }, /* R5 - Format Control */
52 { 6, 0x0000 }, /* R6 - Filter Control */
53 { 7, 0x0000 }, /* R7 - Mode Control 1 */
54 { 8, 0x0002 }, /* R8 - Mode Control 2 */
55 { 32, 0x0002 }, /* R32 - ADDITONAL_CONTROL_1 */
58 static int wm8741_reset(struct snd_soc_component
*component
)
60 return snd_soc_component_write(component
, WM8741_RESET
, 0);
63 static const DECLARE_TLV_DB_SCALE(dac_tlv_fine
, -12700, 13, 0);
64 static const DECLARE_TLV_DB_SCALE(dac_tlv
, -12700, 400, 0);
66 static const struct snd_kcontrol_new wm8741_snd_controls_stereo
[] = {
67 SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION
,
68 WM8741_DACRLSB_ATTENUATION
, 1, 255, 1, dac_tlv_fine
),
69 SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION
,
70 WM8741_DACRMSB_ATTENUATION
, 0, 511, 1, dac_tlv
),
73 static const struct snd_kcontrol_new wm8741_snd_controls_mono_left
[] = {
74 SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION
,
75 1, 255, 1, dac_tlv_fine
),
76 SOC_SINGLE_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION
,
80 static const struct snd_kcontrol_new wm8741_snd_controls_mono_right
[] = {
81 SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACRLSB_ATTENUATION
,
82 1, 255, 1, dac_tlv_fine
),
83 SOC_SINGLE_TLV("Playback Volume", WM8741_DACRMSB_ATTENUATION
,
87 static const struct snd_soc_dapm_widget wm8741_dapm_widgets
[] = {
88 SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM
, 0, 0),
89 SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM
, 0, 0),
90 SND_SOC_DAPM_OUTPUT("VOUTLP"),
91 SND_SOC_DAPM_OUTPUT("VOUTLN"),
92 SND_SOC_DAPM_OUTPUT("VOUTRP"),
93 SND_SOC_DAPM_OUTPUT("VOUTRN"),
96 static const struct snd_soc_dapm_route wm8741_dapm_routes
[] = {
97 { "VOUTLP", NULL
, "DACL" },
98 { "VOUTLN", NULL
, "DACL" },
99 { "VOUTRP", NULL
, "DACR" },
100 { "VOUTRN", NULL
, "DACR" },
103 static const unsigned int rates_11289
[] = {
107 static const struct snd_pcm_hw_constraint_list constraints_11289
= {
108 .count
= ARRAY_SIZE(rates_11289
),
112 static const unsigned int rates_12288
[] = {
116 static const struct snd_pcm_hw_constraint_list constraints_12288
= {
117 .count
= ARRAY_SIZE(rates_12288
),
121 static const unsigned int rates_16384
[] = {
125 static const struct snd_pcm_hw_constraint_list constraints_16384
= {
126 .count
= ARRAY_SIZE(rates_16384
),
130 static const unsigned int rates_16934
[] = {
134 static const struct snd_pcm_hw_constraint_list constraints_16934
= {
135 .count
= ARRAY_SIZE(rates_16934
),
139 static const unsigned int rates_18432
[] = {
143 static const struct snd_pcm_hw_constraint_list constraints_18432
= {
144 .count
= ARRAY_SIZE(rates_18432
),
148 static const unsigned int rates_22579
[] = {
152 static const struct snd_pcm_hw_constraint_list constraints_22579
= {
153 .count
= ARRAY_SIZE(rates_22579
),
157 static const unsigned int rates_24576
[] = {
158 32000, 48000, 96000, 192000
161 static const struct snd_pcm_hw_constraint_list constraints_24576
= {
162 .count
= ARRAY_SIZE(rates_24576
),
166 static const unsigned int rates_36864
[] = {
170 static const struct snd_pcm_hw_constraint_list constraints_36864
= {
171 .count
= ARRAY_SIZE(rates_36864
),
175 static int wm8741_startup(struct snd_pcm_substream
*substream
,
176 struct snd_soc_dai
*dai
)
178 struct snd_soc_component
*component
= dai
->component
;
179 struct wm8741_priv
*wm8741
= snd_soc_component_get_drvdata(component
);
182 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
183 SNDRV_PCM_HW_PARAM_RATE
,
184 wm8741
->sysclk_constraints
);
189 static int wm8741_hw_params(struct snd_pcm_substream
*substream
,
190 struct snd_pcm_hw_params
*params
,
191 struct snd_soc_dai
*dai
)
193 struct snd_soc_component
*component
= dai
->component
;
194 struct wm8741_priv
*wm8741
= snd_soc_component_get_drvdata(component
);
195 unsigned int iface
, mode
;
198 /* The set of sample rates that can be supported depends on the
199 * MCLK supplied to the CODEC - enforce this.
201 if (!wm8741
->sysclk
) {
202 dev_err(component
->dev
,
203 "No MCLK configured, call set_sysclk() on init or in hw_params\n");
207 /* Find a supported LRCLK rate */
208 for (i
= 0; i
< wm8741
->sysclk_constraints
->count
; i
++) {
209 if (wm8741
->sysclk_constraints
->list
[i
] == params_rate(params
))
213 if (i
== wm8741
->sysclk_constraints
->count
) {
214 dev_err(component
->dev
, "LRCLK %d unsupported with MCLK %d\n",
215 params_rate(params
), wm8741
->sysclk
);
220 switch (params_width(params
)) {
234 dev_dbg(component
->dev
, "wm8741_hw_params: Unsupported bit size param = %d",
235 params_width(params
));
239 /* oversampling rate */
240 if (params_rate(params
) > 96000)
242 else if (params_rate(params
) > 48000)
247 dev_dbg(component
->dev
, "wm8741_hw_params: bit size param = %d, rate param = %d",
248 params_width(params
), params_rate(params
));
250 snd_soc_component_update_bits(component
, WM8741_FORMAT_CONTROL
, WM8741_IWL_MASK
,
252 snd_soc_component_update_bits(component
, WM8741_MODE_CONTROL_1
, WM8741_OSR_MASK
,
258 static int wm8741_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
259 int clk_id
, unsigned int freq
, int dir
)
261 struct snd_soc_component
*component
= codec_dai
->component
;
262 struct wm8741_priv
*wm8741
= snd_soc_component_get_drvdata(component
);
264 dev_dbg(component
->dev
, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq
);
268 wm8741
->sysclk_constraints
= NULL
;
271 wm8741
->sysclk_constraints
= &constraints_11289
;
274 wm8741
->sysclk_constraints
= &constraints_12288
;
277 wm8741
->sysclk_constraints
= &constraints_16384
;
280 wm8741
->sysclk_constraints
= &constraints_16934
;
283 wm8741
->sysclk_constraints
= &constraints_18432
;
287 wm8741
->sysclk_constraints
= &constraints_22579
;
290 wm8741
->sysclk_constraints
= &constraints_24576
;
293 wm8741
->sysclk_constraints
= &constraints_36864
;
299 wm8741
->sysclk
= freq
;
303 static int wm8741_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
306 struct snd_soc_component
*component
= codec_dai
->component
;
309 /* check master/slave audio interface */
310 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
311 case SND_SOC_DAIFMT_CBS_CFS
:
317 /* interface format */
318 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
319 case SND_SOC_DAIFMT_I2S
:
322 case SND_SOC_DAIFMT_RIGHT_J
:
325 case SND_SOC_DAIFMT_LEFT_J
:
328 case SND_SOC_DAIFMT_DSP_A
:
331 case SND_SOC_DAIFMT_DSP_B
:
338 /* clock inversion */
339 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
340 case SND_SOC_DAIFMT_NB_NF
:
342 case SND_SOC_DAIFMT_NB_IF
:
345 case SND_SOC_DAIFMT_IB_NF
:
348 case SND_SOC_DAIFMT_IB_IF
:
356 dev_dbg(component
->dev
, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
357 fmt
& SND_SOC_DAIFMT_FORMAT_MASK
,
358 ((fmt
& SND_SOC_DAIFMT_INV_MASK
)));
360 snd_soc_component_update_bits(component
, WM8741_FORMAT_CONTROL
,
361 WM8741_BCP_MASK
| WM8741_LRP_MASK
| WM8741_FMT_MASK
,
367 static int wm8741_mute(struct snd_soc_dai
*codec_dai
, int mute
)
369 struct snd_soc_component
*component
= codec_dai
->component
;
371 snd_soc_component_update_bits(component
, WM8741_VOLUME_CONTROL
,
372 WM8741_SOFT_MASK
, !!mute
<< WM8741_SOFT_SHIFT
);
376 #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
377 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
378 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
379 SNDRV_PCM_RATE_192000)
381 #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
382 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
384 static const struct snd_soc_dai_ops wm8741_dai_ops
= {
385 .startup
= wm8741_startup
,
386 .hw_params
= wm8741_hw_params
,
387 .set_sysclk
= wm8741_set_dai_sysclk
,
388 .set_fmt
= wm8741_set_dai_fmt
,
389 .digital_mute
= wm8741_mute
,
392 static struct snd_soc_dai_driver wm8741_dai
= {
395 .stream_name
= "Playback",
398 .rates
= WM8741_RATES
,
399 .formats
= WM8741_FORMATS
,
401 .ops
= &wm8741_dai_ops
,
405 static int wm8741_resume(struct snd_soc_component
*component
)
407 snd_soc_component_cache_sync(component
);
411 #define wm8741_resume NULL
414 static int wm8741_configure(struct snd_soc_component
*component
)
416 struct wm8741_priv
*wm8741
= snd_soc_component_get_drvdata(component
);
418 /* Configure differential mode */
419 switch (wm8741
->pdata
.diff_mode
) {
420 case WM8741_DIFF_MODE_STEREO
:
421 case WM8741_DIFF_MODE_STEREO_REVERSED
:
422 case WM8741_DIFF_MODE_MONO_LEFT
:
423 case WM8741_DIFF_MODE_MONO_RIGHT
:
424 snd_soc_component_update_bits(component
, WM8741_MODE_CONTROL_2
,
426 wm8741
->pdata
.diff_mode
<< WM8741_DIFF_SHIFT
);
432 /* Change some default settings - latch VU */
433 snd_soc_component_update_bits(component
, WM8741_DACLLSB_ATTENUATION
,
434 WM8741_UPDATELL
, WM8741_UPDATELL
);
435 snd_soc_component_update_bits(component
, WM8741_DACLMSB_ATTENUATION
,
436 WM8741_UPDATELM
, WM8741_UPDATELM
);
437 snd_soc_component_update_bits(component
, WM8741_DACRLSB_ATTENUATION
,
438 WM8741_UPDATERL
, WM8741_UPDATERL
);
439 snd_soc_component_update_bits(component
, WM8741_DACRMSB_ATTENUATION
,
440 WM8741_UPDATERM
, WM8741_UPDATERM
);
445 static int wm8741_add_controls(struct snd_soc_component
*component
)
447 struct wm8741_priv
*wm8741
= snd_soc_component_get_drvdata(component
);
449 switch (wm8741
->pdata
.diff_mode
) {
450 case WM8741_DIFF_MODE_STEREO
:
451 case WM8741_DIFF_MODE_STEREO_REVERSED
:
452 snd_soc_add_component_controls(component
,
453 wm8741_snd_controls_stereo
,
454 ARRAY_SIZE(wm8741_snd_controls_stereo
));
456 case WM8741_DIFF_MODE_MONO_LEFT
:
457 snd_soc_add_component_controls(component
,
458 wm8741_snd_controls_mono_left
,
459 ARRAY_SIZE(wm8741_snd_controls_mono_left
));
461 case WM8741_DIFF_MODE_MONO_RIGHT
:
462 snd_soc_add_component_controls(component
,
463 wm8741_snd_controls_mono_right
,
464 ARRAY_SIZE(wm8741_snd_controls_mono_right
));
473 static int wm8741_probe(struct snd_soc_component
*component
)
475 struct wm8741_priv
*wm8741
= snd_soc_component_get_drvdata(component
);
478 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8741
->supplies
),
481 dev_err(component
->dev
, "Failed to enable supplies: %d\n", ret
);
485 ret
= wm8741_reset(component
);
487 dev_err(component
->dev
, "Failed to issue reset\n");
491 ret
= wm8741_configure(component
);
493 dev_err(component
->dev
, "Failed to change default settings\n");
497 ret
= wm8741_add_controls(component
);
499 dev_err(component
->dev
, "Failed to add controls\n");
503 dev_dbg(component
->dev
, "Successful registration\n");
507 regulator_bulk_disable(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
512 static void wm8741_remove(struct snd_soc_component
*component
)
514 struct wm8741_priv
*wm8741
= snd_soc_component_get_drvdata(component
);
516 regulator_bulk_disable(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
519 static const struct snd_soc_component_driver soc_component_dev_wm8741
= {
520 .probe
= wm8741_probe
,
521 .remove
= wm8741_remove
,
522 .resume
= wm8741_resume
,
523 .dapm_widgets
= wm8741_dapm_widgets
,
524 .num_dapm_widgets
= ARRAY_SIZE(wm8741_dapm_widgets
),
525 .dapm_routes
= wm8741_dapm_routes
,
526 .num_dapm_routes
= ARRAY_SIZE(wm8741_dapm_routes
),
528 .use_pmdown_time
= 1,
530 .non_legacy_dai_naming
= 1,
533 static const struct of_device_id wm8741_of_match
[] = {
534 { .compatible
= "wlf,wm8741", },
537 MODULE_DEVICE_TABLE(of
, wm8741_of_match
);
539 static const struct regmap_config wm8741_regmap
= {
542 .max_register
= WM8741_MAX_REGISTER
,
544 .reg_defaults
= wm8741_reg_defaults
,
545 .num_reg_defaults
= ARRAY_SIZE(wm8741_reg_defaults
),
546 .cache_type
= REGCACHE_RBTREE
,
549 static int wm8741_set_pdata(struct device
*dev
, struct wm8741_priv
*wm8741
)
551 const struct wm8741_platform_data
*pdata
= dev_get_platdata(dev
);
555 if (of_property_read_u32(dev
->of_node
, "diff-mode", &diff_mode
)
557 wm8741
->pdata
.diff_mode
= diff_mode
;
560 memcpy(&wm8741
->pdata
, pdata
, sizeof(wm8741
->pdata
));
566 #if IS_ENABLED(CONFIG_I2C)
567 static int wm8741_i2c_probe(struct i2c_client
*i2c
,
568 const struct i2c_device_id
*id
)
570 struct wm8741_priv
*wm8741
;
573 wm8741
= devm_kzalloc(&i2c
->dev
, sizeof(struct wm8741_priv
),
578 for (i
= 0; i
< ARRAY_SIZE(wm8741
->supplies
); i
++)
579 wm8741
->supplies
[i
].supply
= wm8741_supply_names
[i
];
581 ret
= devm_regulator_bulk_get(&i2c
->dev
, ARRAY_SIZE(wm8741
->supplies
),
584 dev_err(&i2c
->dev
, "Failed to request supplies: %d\n", ret
);
588 wm8741
->regmap
= devm_regmap_init_i2c(i2c
, &wm8741_regmap
);
589 if (IS_ERR(wm8741
->regmap
)) {
590 ret
= PTR_ERR(wm8741
->regmap
);
591 dev_err(&i2c
->dev
, "Failed to init regmap: %d\n", ret
);
595 ret
= wm8741_set_pdata(&i2c
->dev
, wm8741
);
597 dev_err(&i2c
->dev
, "Failed to set pdata: %d\n", ret
);
601 i2c_set_clientdata(i2c
, wm8741
);
603 ret
= devm_snd_soc_register_component(&i2c
->dev
,
604 &soc_component_dev_wm8741
, &wm8741_dai
, 1);
609 static const struct i2c_device_id wm8741_i2c_id
[] = {
613 MODULE_DEVICE_TABLE(i2c
, wm8741_i2c_id
);
615 static struct i2c_driver wm8741_i2c_driver
= {
618 .of_match_table
= wm8741_of_match
,
620 .probe
= wm8741_i2c_probe
,
621 .id_table
= wm8741_i2c_id
,
625 #if defined(CONFIG_SPI_MASTER)
626 static int wm8741_spi_probe(struct spi_device
*spi
)
628 struct wm8741_priv
*wm8741
;
631 wm8741
= devm_kzalloc(&spi
->dev
, sizeof(struct wm8741_priv
),
636 for (i
= 0; i
< ARRAY_SIZE(wm8741
->supplies
); i
++)
637 wm8741
->supplies
[i
].supply
= wm8741_supply_names
[i
];
639 ret
= devm_regulator_bulk_get(&spi
->dev
, ARRAY_SIZE(wm8741
->supplies
),
642 dev_err(&spi
->dev
, "Failed to request supplies: %d\n", ret
);
646 wm8741
->regmap
= devm_regmap_init_spi(spi
, &wm8741_regmap
);
647 if (IS_ERR(wm8741
->regmap
)) {
648 ret
= PTR_ERR(wm8741
->regmap
);
649 dev_err(&spi
->dev
, "Failed to init regmap: %d\n", ret
);
653 ret
= wm8741_set_pdata(&spi
->dev
, wm8741
);
655 dev_err(&spi
->dev
, "Failed to set pdata: %d\n", ret
);
659 spi_set_drvdata(spi
, wm8741
);
661 ret
= devm_snd_soc_register_component(&spi
->dev
,
662 &soc_component_dev_wm8741
, &wm8741_dai
, 1);
666 static struct spi_driver wm8741_spi_driver
= {
669 .of_match_table
= wm8741_of_match
,
671 .probe
= wm8741_spi_probe
,
673 #endif /* CONFIG_SPI_MASTER */
675 static int __init
wm8741_modinit(void)
679 #if IS_ENABLED(CONFIG_I2C)
680 ret
= i2c_add_driver(&wm8741_i2c_driver
);
682 pr_err("Failed to register WM8741 I2C driver: %d\n", ret
);
684 #if defined(CONFIG_SPI_MASTER)
685 ret
= spi_register_driver(&wm8741_spi_driver
);
687 printk(KERN_ERR
"Failed to register wm8741 SPI driver: %d\n",
694 module_init(wm8741_modinit
);
696 static void __exit
wm8741_exit(void)
698 #if defined(CONFIG_SPI_MASTER)
699 spi_unregister_driver(&wm8741_spi_driver
);
701 #if IS_ENABLED(CONFIG_I2C)
702 i2c_del_driver(&wm8741_i2c_driver
);
705 module_exit(wm8741_exit
);
707 MODULE_DESCRIPTION("ASoC WM8741 driver");
708 MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
709 MODULE_LICENSE("GPL");