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 int wm8741_reset(struct snd_soc_codec
*codec
)
66 return snd_soc_write(codec
, WM8741_RESET
, 0);
69 static const DECLARE_TLV_DB_SCALE(dac_tlv_fine
, -12700, 13, 0);
70 static const DECLARE_TLV_DB_SCALE(dac_tlv
, -12700, 400, 0);
72 static const struct snd_kcontrol_new wm8741_snd_controls_stereo
[] = {
73 SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION
,
74 WM8741_DACRLSB_ATTENUATION
, 1, 255, 1, dac_tlv_fine
),
75 SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION
,
76 WM8741_DACRMSB_ATTENUATION
, 0, 511, 1, dac_tlv
),
79 static const struct snd_kcontrol_new wm8741_snd_controls_mono_left
[] = {
80 SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION
,
81 1, 255, 1, dac_tlv_fine
),
82 SOC_SINGLE_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION
,
86 static const struct snd_kcontrol_new wm8741_snd_controls_mono_right
[] = {
87 SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACRLSB_ATTENUATION
,
88 1, 255, 1, dac_tlv_fine
),
89 SOC_SINGLE_TLV("Playback Volume", WM8741_DACRMSB_ATTENUATION
,
93 static const struct snd_soc_dapm_widget wm8741_dapm_widgets
[] = {
94 SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM
, 0, 0),
95 SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM
, 0, 0),
96 SND_SOC_DAPM_OUTPUT("VOUTLP"),
97 SND_SOC_DAPM_OUTPUT("VOUTLN"),
98 SND_SOC_DAPM_OUTPUT("VOUTRP"),
99 SND_SOC_DAPM_OUTPUT("VOUTRN"),
102 static const struct snd_soc_dapm_route wm8741_dapm_routes
[] = {
103 { "VOUTLP", NULL
, "DACL" },
104 { "VOUTLN", NULL
, "DACL" },
105 { "VOUTRP", NULL
, "DACR" },
106 { "VOUTRN", NULL
, "DACR" },
109 static const unsigned int rates_11289
[] = {
113 static const struct snd_pcm_hw_constraint_list constraints_11289
= {
114 .count
= ARRAY_SIZE(rates_11289
),
118 static const unsigned int rates_12288
[] = {
122 static const struct snd_pcm_hw_constraint_list constraints_12288
= {
123 .count
= ARRAY_SIZE(rates_12288
),
127 static const unsigned int rates_16384
[] = {
131 static const struct snd_pcm_hw_constraint_list constraints_16384
= {
132 .count
= ARRAY_SIZE(rates_16384
),
136 static const unsigned int rates_16934
[] = {
140 static const struct snd_pcm_hw_constraint_list constraints_16934
= {
141 .count
= ARRAY_SIZE(rates_16934
),
145 static const unsigned int rates_18432
[] = {
149 static const struct snd_pcm_hw_constraint_list constraints_18432
= {
150 .count
= ARRAY_SIZE(rates_18432
),
154 static const unsigned int rates_22579
[] = {
158 static const struct snd_pcm_hw_constraint_list constraints_22579
= {
159 .count
= ARRAY_SIZE(rates_22579
),
163 static const unsigned int rates_24576
[] = {
164 32000, 48000, 96000, 192000
167 static const struct snd_pcm_hw_constraint_list constraints_24576
= {
168 .count
= ARRAY_SIZE(rates_24576
),
172 static const unsigned int rates_36864
[] = {
176 static const struct snd_pcm_hw_constraint_list constraints_36864
= {
177 .count
= ARRAY_SIZE(rates_36864
),
181 static int wm8741_startup(struct snd_pcm_substream
*substream
,
182 struct snd_soc_dai
*dai
)
184 struct snd_soc_codec
*codec
= dai
->codec
;
185 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
188 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
189 SNDRV_PCM_HW_PARAM_RATE
,
190 wm8741
->sysclk_constraints
);
195 static int wm8741_hw_params(struct snd_pcm_substream
*substream
,
196 struct snd_pcm_hw_params
*params
,
197 struct snd_soc_dai
*dai
)
199 struct snd_soc_codec
*codec
= dai
->codec
;
200 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
201 u16 iface
= snd_soc_read(codec
, WM8741_FORMAT_CONTROL
) & 0x1FC;
204 /* The set of sample rates that can be supported depends on the
205 * MCLK supplied to the CODEC - enforce this.
207 if (!wm8741
->sysclk
) {
209 "No MCLK configured, call set_sysclk() on init or in hw_params\n");
213 /* Find a supported LRCLK rate */
214 for (i
= 0; i
< wm8741
->sysclk_constraints
->count
; i
++) {
215 if (wm8741
->sysclk_constraints
->list
[i
] == params_rate(params
))
219 if (i
== wm8741
->sysclk_constraints
->count
) {
220 dev_err(codec
->dev
, "LRCLK %d unsupported with MCLK %d\n",
221 params_rate(params
), wm8741
->sysclk
);
226 switch (params_width(params
)) {
239 dev_dbg(codec
->dev
, "wm8741_hw_params: Unsupported bit size param = %d",
240 params_width(params
));
244 dev_dbg(codec
->dev
, "wm8741_hw_params: bit size param = %d, rate param = %d",
245 params_width(params
), params_rate(params
));
247 snd_soc_write(codec
, WM8741_FORMAT_CONTROL
, iface
);
251 static int wm8741_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
252 int clk_id
, unsigned int freq
, int dir
)
254 struct snd_soc_codec
*codec
= codec_dai
->codec
;
255 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
257 dev_dbg(codec
->dev
, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq
);
261 wm8741
->sysclk_constraints
= NULL
;
264 wm8741
->sysclk_constraints
= &constraints_11289
;
267 wm8741
->sysclk_constraints
= &constraints_12288
;
270 wm8741
->sysclk_constraints
= &constraints_16384
;
273 wm8741
->sysclk_constraints
= &constraints_16934
;
276 wm8741
->sysclk_constraints
= &constraints_18432
;
280 wm8741
->sysclk_constraints
= &constraints_22579
;
283 wm8741
->sysclk_constraints
= &constraints_24576
;
286 wm8741
->sysclk_constraints
= &constraints_36864
;
292 wm8741
->sysclk
= freq
;
296 static int wm8741_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
299 struct snd_soc_codec
*codec
= codec_dai
->codec
;
300 u16 iface
= snd_soc_read(codec
, WM8741_FORMAT_CONTROL
) & 0x1C3;
302 /* check master/slave audio interface */
303 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
304 case SND_SOC_DAIFMT_CBS_CFS
:
310 /* interface format */
311 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
312 case SND_SOC_DAIFMT_I2S
:
315 case SND_SOC_DAIFMT_RIGHT_J
:
317 case SND_SOC_DAIFMT_LEFT_J
:
320 case SND_SOC_DAIFMT_DSP_A
:
323 case SND_SOC_DAIFMT_DSP_B
:
330 /* clock inversion */
331 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
332 case SND_SOC_DAIFMT_NB_NF
:
334 case SND_SOC_DAIFMT_IB_IF
:
337 case SND_SOC_DAIFMT_IB_NF
:
340 case SND_SOC_DAIFMT_NB_IF
:
348 dev_dbg(codec
->dev
, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
349 fmt
& SND_SOC_DAIFMT_FORMAT_MASK
,
350 ((fmt
& SND_SOC_DAIFMT_INV_MASK
)));
352 snd_soc_write(codec
, WM8741_FORMAT_CONTROL
, iface
);
356 #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
357 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
358 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
359 SNDRV_PCM_RATE_192000)
361 #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
362 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
364 static const struct snd_soc_dai_ops wm8741_dai_ops
= {
365 .startup
= wm8741_startup
,
366 .hw_params
= wm8741_hw_params
,
367 .set_sysclk
= wm8741_set_dai_sysclk
,
368 .set_fmt
= wm8741_set_dai_fmt
,
371 static struct snd_soc_dai_driver wm8741_dai
= {
374 .stream_name
= "Playback",
377 .rates
= WM8741_RATES
,
378 .formats
= WM8741_FORMATS
,
380 .ops
= &wm8741_dai_ops
,
384 static int wm8741_resume(struct snd_soc_codec
*codec
)
386 snd_soc_cache_sync(codec
);
390 #define wm8741_resume NULL
393 static int wm8741_configure(struct snd_soc_codec
*codec
)
395 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
397 /* Configure differential mode */
398 switch (wm8741
->pdata
.diff_mode
) {
399 case WM8741_DIFF_MODE_STEREO
:
400 case WM8741_DIFF_MODE_STEREO_REVERSED
:
401 case WM8741_DIFF_MODE_MONO_LEFT
:
402 case WM8741_DIFF_MODE_MONO_RIGHT
:
403 snd_soc_update_bits(codec
, WM8741_MODE_CONTROL_2
,
405 wm8741
->pdata
.diff_mode
<< WM8741_DIFF_SHIFT
);
411 /* Change some default settings - latch VU */
412 snd_soc_update_bits(codec
, WM8741_DACLLSB_ATTENUATION
,
413 WM8741_UPDATELL
, WM8741_UPDATELL
);
414 snd_soc_update_bits(codec
, WM8741_DACLMSB_ATTENUATION
,
415 WM8741_UPDATELM
, WM8741_UPDATELM
);
416 snd_soc_update_bits(codec
, WM8741_DACRLSB_ATTENUATION
,
417 WM8741_UPDATERL
, WM8741_UPDATERL
);
418 snd_soc_update_bits(codec
, WM8741_DACRMSB_ATTENUATION
,
419 WM8741_UPDATERM
, WM8741_UPDATERM
);
424 static int wm8741_add_controls(struct snd_soc_codec
*codec
)
426 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
428 switch (wm8741
->pdata
.diff_mode
) {
429 case WM8741_DIFF_MODE_STEREO
:
430 case WM8741_DIFF_MODE_STEREO_REVERSED
:
431 snd_soc_add_codec_controls(codec
,
432 wm8741_snd_controls_stereo
,
433 ARRAY_SIZE(wm8741_snd_controls_stereo
));
435 case WM8741_DIFF_MODE_MONO_LEFT
:
436 snd_soc_add_codec_controls(codec
,
437 wm8741_snd_controls_mono_left
,
438 ARRAY_SIZE(wm8741_snd_controls_mono_left
));
440 case WM8741_DIFF_MODE_MONO_RIGHT
:
441 snd_soc_add_codec_controls(codec
,
442 wm8741_snd_controls_mono_right
,
443 ARRAY_SIZE(wm8741_snd_controls_mono_right
));
452 static int wm8741_probe(struct snd_soc_codec
*codec
)
454 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
457 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8741
->supplies
),
460 dev_err(codec
->dev
, "Failed to enable supplies: %d\n", ret
);
464 ret
= wm8741_reset(codec
);
466 dev_err(codec
->dev
, "Failed to issue reset\n");
470 ret
= wm8741_configure(codec
);
472 dev_err(codec
->dev
, "Failed to change default settings\n");
476 ret
= wm8741_add_controls(codec
);
478 dev_err(codec
->dev
, "Failed to add controls\n");
482 dev_dbg(codec
->dev
, "Successful registration\n");
486 regulator_bulk_disable(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
491 static int wm8741_remove(struct snd_soc_codec
*codec
)
493 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
495 regulator_bulk_disable(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
500 static struct snd_soc_codec_driver soc_codec_dev_wm8741
= {
501 .probe
= wm8741_probe
,
502 .remove
= wm8741_remove
,
503 .resume
= wm8741_resume
,
505 .dapm_widgets
= wm8741_dapm_widgets
,
506 .num_dapm_widgets
= ARRAY_SIZE(wm8741_dapm_widgets
),
507 .dapm_routes
= wm8741_dapm_routes
,
508 .num_dapm_routes
= ARRAY_SIZE(wm8741_dapm_routes
),
511 static const struct of_device_id wm8741_of_match
[] = {
512 { .compatible
= "wlf,wm8741", },
515 MODULE_DEVICE_TABLE(of
, wm8741_of_match
);
517 static const struct regmap_config wm8741_regmap
= {
520 .max_register
= WM8741_MAX_REGISTER
,
522 .reg_defaults
= wm8741_reg_defaults
,
523 .num_reg_defaults
= ARRAY_SIZE(wm8741_reg_defaults
),
524 .cache_type
= REGCACHE_RBTREE
,
527 static int wm8741_set_pdata(struct device
*dev
, struct wm8741_priv
*wm8741
)
529 const struct wm8741_platform_data
*pdata
= dev_get_platdata(dev
);
533 if (of_property_read_u32(dev
->of_node
, "diff-mode", &diff_mode
)
535 wm8741
->pdata
.diff_mode
= diff_mode
;
538 memcpy(&wm8741
->pdata
, pdata
, sizeof(wm8741
->pdata
));
544 #if IS_ENABLED(CONFIG_I2C)
545 static int wm8741_i2c_probe(struct i2c_client
*i2c
,
546 const struct i2c_device_id
*id
)
548 struct wm8741_priv
*wm8741
;
551 wm8741
= devm_kzalloc(&i2c
->dev
, sizeof(struct wm8741_priv
),
556 for (i
= 0; i
< ARRAY_SIZE(wm8741
->supplies
); i
++)
557 wm8741
->supplies
[i
].supply
= wm8741_supply_names
[i
];
559 ret
= devm_regulator_bulk_get(&i2c
->dev
, ARRAY_SIZE(wm8741
->supplies
),
562 dev_err(&i2c
->dev
, "Failed to request supplies: %d\n", ret
);
566 wm8741
->regmap
= devm_regmap_init_i2c(i2c
, &wm8741_regmap
);
567 if (IS_ERR(wm8741
->regmap
)) {
568 ret
= PTR_ERR(wm8741
->regmap
);
569 dev_err(&i2c
->dev
, "Failed to init regmap: %d\n", ret
);
573 ret
= wm8741_set_pdata(&i2c
->dev
, wm8741
);
575 dev_err(&i2c
->dev
, "Failed to set pdata: %d\n", ret
);
579 i2c_set_clientdata(i2c
, wm8741
);
581 ret
= snd_soc_register_codec(&i2c
->dev
,
582 &soc_codec_dev_wm8741
, &wm8741_dai
, 1);
587 static int wm8741_i2c_remove(struct i2c_client
*client
)
589 snd_soc_unregister_codec(&client
->dev
);
593 static const struct i2c_device_id wm8741_i2c_id
[] = {
597 MODULE_DEVICE_TABLE(i2c
, wm8741_i2c_id
);
599 static struct i2c_driver wm8741_i2c_driver
= {
602 .of_match_table
= wm8741_of_match
,
604 .probe
= wm8741_i2c_probe
,
605 .remove
= wm8741_i2c_remove
,
606 .id_table
= wm8741_i2c_id
,
610 #if defined(CONFIG_SPI_MASTER)
611 static int wm8741_spi_probe(struct spi_device
*spi
)
613 struct wm8741_priv
*wm8741
;
616 wm8741
= devm_kzalloc(&spi
->dev
, sizeof(struct wm8741_priv
),
621 for (i
= 0; i
< ARRAY_SIZE(wm8741
->supplies
); i
++)
622 wm8741
->supplies
[i
].supply
= wm8741_supply_names
[i
];
624 ret
= devm_regulator_bulk_get(&spi
->dev
, ARRAY_SIZE(wm8741
->supplies
),
627 dev_err(&spi
->dev
, "Failed to request supplies: %d\n", ret
);
631 wm8741
->regmap
= devm_regmap_init_spi(spi
, &wm8741_regmap
);
632 if (IS_ERR(wm8741
->regmap
)) {
633 ret
= PTR_ERR(wm8741
->regmap
);
634 dev_err(&spi
->dev
, "Failed to init regmap: %d\n", ret
);
638 ret
= wm8741_set_pdata(&spi
->dev
, wm8741
);
640 dev_err(&spi
->dev
, "Failed to set pdata: %d\n", ret
);
644 spi_set_drvdata(spi
, wm8741
);
646 ret
= snd_soc_register_codec(&spi
->dev
,
647 &soc_codec_dev_wm8741
, &wm8741_dai
, 1);
651 static int wm8741_spi_remove(struct spi_device
*spi
)
653 snd_soc_unregister_codec(&spi
->dev
);
657 static struct spi_driver wm8741_spi_driver
= {
660 .of_match_table
= wm8741_of_match
,
662 .probe
= wm8741_spi_probe
,
663 .remove
= wm8741_spi_remove
,
665 #endif /* CONFIG_SPI_MASTER */
667 static int __init
wm8741_modinit(void)
671 #if IS_ENABLED(CONFIG_I2C)
672 ret
= i2c_add_driver(&wm8741_i2c_driver
);
674 pr_err("Failed to register WM8741 I2C driver: %d\n", ret
);
676 #if defined(CONFIG_SPI_MASTER)
677 ret
= spi_register_driver(&wm8741_spi_driver
);
679 printk(KERN_ERR
"Failed to register wm8741 SPI driver: %d\n",
686 module_init(wm8741_modinit
);
688 static void __exit
wm8741_exit(void)
690 #if defined(CONFIG_SPI_MASTER)
691 spi_unregister_driver(&wm8741_spi_driver
);
693 #if IS_ENABLED(CONFIG_I2C)
694 i2c_del_driver(&wm8741_i2c_driver
);
697 module_exit(wm8741_exit
);
699 MODULE_DESCRIPTION("ASoC WM8741 driver");
700 MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
701 MODULE_LICENSE("GPL");