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 /* codec private data */
42 struct wm8741_platform_data pdata
;
43 struct regmap
*regmap
;
44 struct regulator_bulk_data supplies
[WM8741_NUM_SUPPLIES
];
46 const struct snd_pcm_hw_constraint_list
*sysclk_constraints
;
49 static const struct reg_default wm8741_reg_defaults
[] = {
50 { 0, 0x0000 }, /* R0 - DACLLSB Attenuation */
51 { 1, 0x0000 }, /* R1 - DACLMSB Attenuation */
52 { 2, 0x0000 }, /* R2 - DACRLSB Attenuation */
53 { 3, 0x0000 }, /* R3 - DACRMSB Attenuation */
54 { 4, 0x0000 }, /* R4 - Volume Control */
55 { 5, 0x000A }, /* R5 - Format Control */
56 { 6, 0x0000 }, /* R6 - Filter Control */
57 { 7, 0x0000 }, /* R7 - Mode Control 1 */
58 { 8, 0x0002 }, /* R8 - Mode Control 2 */
59 { 32, 0x0002 }, /* R32 - ADDITONAL_CONTROL_1 */
62 static int wm8741_reset(struct snd_soc_codec
*codec
)
64 return snd_soc_write(codec
, WM8741_RESET
, 0);
67 static const DECLARE_TLV_DB_SCALE(dac_tlv_fine
, -12700, 13, 0);
68 static const DECLARE_TLV_DB_SCALE(dac_tlv
, -12700, 400, 0);
70 static const struct snd_kcontrol_new wm8741_snd_controls_stereo
[] = {
71 SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION
,
72 WM8741_DACRLSB_ATTENUATION
, 1, 255, 1, dac_tlv_fine
),
73 SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION
,
74 WM8741_DACRMSB_ATTENUATION
, 0, 511, 1, dac_tlv
),
77 static const struct snd_kcontrol_new wm8741_snd_controls_mono_left
[] = {
78 SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION
,
79 1, 255, 1, dac_tlv_fine
),
80 SOC_SINGLE_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION
,
84 static const struct snd_kcontrol_new wm8741_snd_controls_mono_right
[] = {
85 SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACRLSB_ATTENUATION
,
86 1, 255, 1, dac_tlv_fine
),
87 SOC_SINGLE_TLV("Playback Volume", WM8741_DACRMSB_ATTENUATION
,
91 static const struct snd_soc_dapm_widget wm8741_dapm_widgets
[] = {
92 SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM
, 0, 0),
93 SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM
, 0, 0),
94 SND_SOC_DAPM_OUTPUT("VOUTLP"),
95 SND_SOC_DAPM_OUTPUT("VOUTLN"),
96 SND_SOC_DAPM_OUTPUT("VOUTRP"),
97 SND_SOC_DAPM_OUTPUT("VOUTRN"),
100 static const struct snd_soc_dapm_route wm8741_dapm_routes
[] = {
101 { "VOUTLP", NULL
, "DACL" },
102 { "VOUTLN", NULL
, "DACL" },
103 { "VOUTRP", NULL
, "DACR" },
104 { "VOUTRN", NULL
, "DACR" },
107 static const unsigned int rates_11289
[] = {
111 static const struct snd_pcm_hw_constraint_list constraints_11289
= {
112 .count
= ARRAY_SIZE(rates_11289
),
116 static const unsigned int rates_12288
[] = {
120 static const struct snd_pcm_hw_constraint_list constraints_12288
= {
121 .count
= ARRAY_SIZE(rates_12288
),
125 static const unsigned int rates_16384
[] = {
129 static const struct snd_pcm_hw_constraint_list constraints_16384
= {
130 .count
= ARRAY_SIZE(rates_16384
),
134 static const unsigned int rates_16934
[] = {
138 static const struct snd_pcm_hw_constraint_list constraints_16934
= {
139 .count
= ARRAY_SIZE(rates_16934
),
143 static const unsigned int rates_18432
[] = {
147 static const struct snd_pcm_hw_constraint_list constraints_18432
= {
148 .count
= ARRAY_SIZE(rates_18432
),
152 static const unsigned int rates_22579
[] = {
156 static const struct snd_pcm_hw_constraint_list constraints_22579
= {
157 .count
= ARRAY_SIZE(rates_22579
),
161 static const unsigned int rates_24576
[] = {
162 32000, 48000, 96000, 192000
165 static const struct snd_pcm_hw_constraint_list constraints_24576
= {
166 .count
= ARRAY_SIZE(rates_24576
),
170 static const unsigned int rates_36864
[] = {
174 static const struct snd_pcm_hw_constraint_list constraints_36864
= {
175 .count
= ARRAY_SIZE(rates_36864
),
179 static int wm8741_startup(struct snd_pcm_substream
*substream
,
180 struct snd_soc_dai
*dai
)
182 struct snd_soc_codec
*codec
= dai
->codec
;
183 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
186 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
187 SNDRV_PCM_HW_PARAM_RATE
,
188 wm8741
->sysclk_constraints
);
193 static int wm8741_hw_params(struct snd_pcm_substream
*substream
,
194 struct snd_pcm_hw_params
*params
,
195 struct snd_soc_dai
*dai
)
197 struct snd_soc_codec
*codec
= dai
->codec
;
198 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
202 /* The set of sample rates that can be supported depends on the
203 * MCLK supplied to the CODEC - enforce this.
205 if (!wm8741
->sysclk
) {
207 "No MCLK configured, call set_sysclk() on init or in hw_params\n");
211 /* Find a supported LRCLK rate */
212 for (i
= 0; i
< wm8741
->sysclk_constraints
->count
; i
++) {
213 if (wm8741
->sysclk_constraints
->list
[i
] == params_rate(params
))
217 if (i
== wm8741
->sysclk_constraints
->count
) {
218 dev_err(codec
->dev
, "LRCLK %d unsupported with MCLK %d\n",
219 params_rate(params
), wm8741
->sysclk
);
224 switch (params_width(params
)) {
238 dev_dbg(codec
->dev
, "wm8741_hw_params: Unsupported bit size param = %d",
239 params_width(params
));
243 dev_dbg(codec
->dev
, "wm8741_hw_params: bit size param = %d, rate param = %d",
244 params_width(params
), params_rate(params
));
246 snd_soc_update_bits(codec
, WM8741_FORMAT_CONTROL
, WM8741_IWL_MASK
,
252 static int wm8741_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
253 int clk_id
, unsigned int freq
, int dir
)
255 struct snd_soc_codec
*codec
= codec_dai
->codec
;
256 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
258 dev_dbg(codec
->dev
, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq
);
262 wm8741
->sysclk_constraints
= NULL
;
265 wm8741
->sysclk_constraints
= &constraints_11289
;
268 wm8741
->sysclk_constraints
= &constraints_12288
;
271 wm8741
->sysclk_constraints
= &constraints_16384
;
274 wm8741
->sysclk_constraints
= &constraints_16934
;
277 wm8741
->sysclk_constraints
= &constraints_18432
;
281 wm8741
->sysclk_constraints
= &constraints_22579
;
284 wm8741
->sysclk_constraints
= &constraints_24576
;
287 wm8741
->sysclk_constraints
= &constraints_36864
;
293 wm8741
->sysclk
= freq
;
297 static int wm8741_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
300 struct snd_soc_codec
*codec
= codec_dai
->codec
;
303 /* check master/slave audio interface */
304 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
305 case SND_SOC_DAIFMT_CBS_CFS
:
311 /* interface format */
312 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
313 case SND_SOC_DAIFMT_I2S
:
316 case SND_SOC_DAIFMT_RIGHT_J
:
319 case SND_SOC_DAIFMT_LEFT_J
:
322 case SND_SOC_DAIFMT_DSP_A
:
325 case SND_SOC_DAIFMT_DSP_B
:
332 /* clock inversion */
333 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
334 case SND_SOC_DAIFMT_NB_NF
:
336 case SND_SOC_DAIFMT_NB_IF
:
339 case SND_SOC_DAIFMT_IB_NF
:
342 case SND_SOC_DAIFMT_IB_IF
:
350 dev_dbg(codec
->dev
, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
351 fmt
& SND_SOC_DAIFMT_FORMAT_MASK
,
352 ((fmt
& SND_SOC_DAIFMT_INV_MASK
)));
354 snd_soc_update_bits(codec
, WM8741_FORMAT_CONTROL
,
355 WM8741_BCP_MASK
| WM8741_LRP_MASK
| WM8741_FMT_MASK
,
361 #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
362 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
363 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
364 SNDRV_PCM_RATE_192000)
366 #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
367 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
369 static const struct snd_soc_dai_ops wm8741_dai_ops
= {
370 .startup
= wm8741_startup
,
371 .hw_params
= wm8741_hw_params
,
372 .set_sysclk
= wm8741_set_dai_sysclk
,
373 .set_fmt
= wm8741_set_dai_fmt
,
376 static struct snd_soc_dai_driver wm8741_dai
= {
379 .stream_name
= "Playback",
382 .rates
= WM8741_RATES
,
383 .formats
= WM8741_FORMATS
,
385 .ops
= &wm8741_dai_ops
,
389 static int wm8741_resume(struct snd_soc_codec
*codec
)
391 snd_soc_cache_sync(codec
);
395 #define wm8741_resume NULL
398 static int wm8741_configure(struct snd_soc_codec
*codec
)
400 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
402 /* Configure differential mode */
403 switch (wm8741
->pdata
.diff_mode
) {
404 case WM8741_DIFF_MODE_STEREO
:
405 case WM8741_DIFF_MODE_STEREO_REVERSED
:
406 case WM8741_DIFF_MODE_MONO_LEFT
:
407 case WM8741_DIFF_MODE_MONO_RIGHT
:
408 snd_soc_update_bits(codec
, WM8741_MODE_CONTROL_2
,
410 wm8741
->pdata
.diff_mode
<< WM8741_DIFF_SHIFT
);
416 /* Change some default settings - latch VU */
417 snd_soc_update_bits(codec
, WM8741_DACLLSB_ATTENUATION
,
418 WM8741_UPDATELL
, WM8741_UPDATELL
);
419 snd_soc_update_bits(codec
, WM8741_DACLMSB_ATTENUATION
,
420 WM8741_UPDATELM
, WM8741_UPDATELM
);
421 snd_soc_update_bits(codec
, WM8741_DACRLSB_ATTENUATION
,
422 WM8741_UPDATERL
, WM8741_UPDATERL
);
423 snd_soc_update_bits(codec
, WM8741_DACRMSB_ATTENUATION
,
424 WM8741_UPDATERM
, WM8741_UPDATERM
);
429 static int wm8741_add_controls(struct snd_soc_codec
*codec
)
431 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
433 switch (wm8741
->pdata
.diff_mode
) {
434 case WM8741_DIFF_MODE_STEREO
:
435 case WM8741_DIFF_MODE_STEREO_REVERSED
:
436 snd_soc_add_codec_controls(codec
,
437 wm8741_snd_controls_stereo
,
438 ARRAY_SIZE(wm8741_snd_controls_stereo
));
440 case WM8741_DIFF_MODE_MONO_LEFT
:
441 snd_soc_add_codec_controls(codec
,
442 wm8741_snd_controls_mono_left
,
443 ARRAY_SIZE(wm8741_snd_controls_mono_left
));
445 case WM8741_DIFF_MODE_MONO_RIGHT
:
446 snd_soc_add_codec_controls(codec
,
447 wm8741_snd_controls_mono_right
,
448 ARRAY_SIZE(wm8741_snd_controls_mono_right
));
457 static int wm8741_probe(struct snd_soc_codec
*codec
)
459 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
462 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8741
->supplies
),
465 dev_err(codec
->dev
, "Failed to enable supplies: %d\n", ret
);
469 ret
= wm8741_reset(codec
);
471 dev_err(codec
->dev
, "Failed to issue reset\n");
475 ret
= wm8741_configure(codec
);
477 dev_err(codec
->dev
, "Failed to change default settings\n");
481 ret
= wm8741_add_controls(codec
);
483 dev_err(codec
->dev
, "Failed to add controls\n");
487 dev_dbg(codec
->dev
, "Successful registration\n");
491 regulator_bulk_disable(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
496 static int wm8741_remove(struct snd_soc_codec
*codec
)
498 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
500 regulator_bulk_disable(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
505 static const struct snd_soc_codec_driver soc_codec_dev_wm8741
= {
506 .probe
= wm8741_probe
,
507 .remove
= wm8741_remove
,
508 .resume
= wm8741_resume
,
510 .component_driver
= {
511 .dapm_widgets
= wm8741_dapm_widgets
,
512 .num_dapm_widgets
= ARRAY_SIZE(wm8741_dapm_widgets
),
513 .dapm_routes
= wm8741_dapm_routes
,
514 .num_dapm_routes
= ARRAY_SIZE(wm8741_dapm_routes
),
518 static const struct of_device_id wm8741_of_match
[] = {
519 { .compatible
= "wlf,wm8741", },
522 MODULE_DEVICE_TABLE(of
, wm8741_of_match
);
524 static const struct regmap_config wm8741_regmap
= {
527 .max_register
= WM8741_MAX_REGISTER
,
529 .reg_defaults
= wm8741_reg_defaults
,
530 .num_reg_defaults
= ARRAY_SIZE(wm8741_reg_defaults
),
531 .cache_type
= REGCACHE_RBTREE
,
534 static int wm8741_set_pdata(struct device
*dev
, struct wm8741_priv
*wm8741
)
536 const struct wm8741_platform_data
*pdata
= dev_get_platdata(dev
);
540 if (of_property_read_u32(dev
->of_node
, "diff-mode", &diff_mode
)
542 wm8741
->pdata
.diff_mode
= diff_mode
;
545 memcpy(&wm8741
->pdata
, pdata
, sizeof(wm8741
->pdata
));
551 #if IS_ENABLED(CONFIG_I2C)
552 static int wm8741_i2c_probe(struct i2c_client
*i2c
,
553 const struct i2c_device_id
*id
)
555 struct wm8741_priv
*wm8741
;
558 wm8741
= devm_kzalloc(&i2c
->dev
, sizeof(struct wm8741_priv
),
563 for (i
= 0; i
< ARRAY_SIZE(wm8741
->supplies
); i
++)
564 wm8741
->supplies
[i
].supply
= wm8741_supply_names
[i
];
566 ret
= devm_regulator_bulk_get(&i2c
->dev
, ARRAY_SIZE(wm8741
->supplies
),
569 dev_err(&i2c
->dev
, "Failed to request supplies: %d\n", ret
);
573 wm8741
->regmap
= devm_regmap_init_i2c(i2c
, &wm8741_regmap
);
574 if (IS_ERR(wm8741
->regmap
)) {
575 ret
= PTR_ERR(wm8741
->regmap
);
576 dev_err(&i2c
->dev
, "Failed to init regmap: %d\n", ret
);
580 ret
= wm8741_set_pdata(&i2c
->dev
, wm8741
);
582 dev_err(&i2c
->dev
, "Failed to set pdata: %d\n", ret
);
586 i2c_set_clientdata(i2c
, wm8741
);
588 ret
= snd_soc_register_codec(&i2c
->dev
,
589 &soc_codec_dev_wm8741
, &wm8741_dai
, 1);
594 static int wm8741_i2c_remove(struct i2c_client
*client
)
596 snd_soc_unregister_codec(&client
->dev
);
600 static const struct i2c_device_id wm8741_i2c_id
[] = {
604 MODULE_DEVICE_TABLE(i2c
, wm8741_i2c_id
);
606 static struct i2c_driver wm8741_i2c_driver
= {
609 .of_match_table
= wm8741_of_match
,
611 .probe
= wm8741_i2c_probe
,
612 .remove
= wm8741_i2c_remove
,
613 .id_table
= wm8741_i2c_id
,
617 #if defined(CONFIG_SPI_MASTER)
618 static int wm8741_spi_probe(struct spi_device
*spi
)
620 struct wm8741_priv
*wm8741
;
623 wm8741
= devm_kzalloc(&spi
->dev
, sizeof(struct wm8741_priv
),
628 for (i
= 0; i
< ARRAY_SIZE(wm8741
->supplies
); i
++)
629 wm8741
->supplies
[i
].supply
= wm8741_supply_names
[i
];
631 ret
= devm_regulator_bulk_get(&spi
->dev
, ARRAY_SIZE(wm8741
->supplies
),
634 dev_err(&spi
->dev
, "Failed to request supplies: %d\n", ret
);
638 wm8741
->regmap
= devm_regmap_init_spi(spi
, &wm8741_regmap
);
639 if (IS_ERR(wm8741
->regmap
)) {
640 ret
= PTR_ERR(wm8741
->regmap
);
641 dev_err(&spi
->dev
, "Failed to init regmap: %d\n", ret
);
645 ret
= wm8741_set_pdata(&spi
->dev
, wm8741
);
647 dev_err(&spi
->dev
, "Failed to set pdata: %d\n", ret
);
651 spi_set_drvdata(spi
, wm8741
);
653 ret
= snd_soc_register_codec(&spi
->dev
,
654 &soc_codec_dev_wm8741
, &wm8741_dai
, 1);
658 static int wm8741_spi_remove(struct spi_device
*spi
)
660 snd_soc_unregister_codec(&spi
->dev
);
664 static struct spi_driver wm8741_spi_driver
= {
667 .of_match_table
= wm8741_of_match
,
669 .probe
= wm8741_spi_probe
,
670 .remove
= wm8741_spi_remove
,
672 #endif /* CONFIG_SPI_MASTER */
674 static int __init
wm8741_modinit(void)
678 #if IS_ENABLED(CONFIG_I2C)
679 ret
= i2c_add_driver(&wm8741_i2c_driver
);
681 pr_err("Failed to register WM8741 I2C driver: %d\n", ret
);
683 #if defined(CONFIG_SPI_MASTER)
684 ret
= spi_register_driver(&wm8741_spi_driver
);
686 printk(KERN_ERR
"Failed to register wm8741 SPI driver: %d\n",
693 module_init(wm8741_modinit
);
695 static void __exit
wm8741_exit(void)
697 #if defined(CONFIG_SPI_MASTER)
698 spi_unregister_driver(&wm8741_spi_driver
);
700 #if IS_ENABLED(CONFIG_I2C)
701 i2c_del_driver(&wm8741_i2c_driver
);
704 module_exit(wm8741_exit
);
706 MODULE_DESCRIPTION("ASoC WM8741 driver");
707 MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
708 MODULE_LICENSE("GPL");