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 regmap
*regmap
;
45 struct regulator_bulk_data supplies
[WM8741_NUM_SUPPLIES
];
47 struct snd_pcm_hw_constraint_list
*sysclk_constraints
;
50 static const struct reg_default wm8741_reg_defaults
[] = {
51 { 0, 0x0000 }, /* R0 - DACLLSB Attenuation */
52 { 1, 0x0000 }, /* R1 - DACLMSB Attenuation */
53 { 2, 0x0000 }, /* R2 - DACRLSB Attenuation */
54 { 3, 0x0000 }, /* R3 - DACRMSB Attenuation */
55 { 4, 0x0000 }, /* R4 - Volume Control */
56 { 5, 0x000A }, /* R5 - Format Control */
57 { 6, 0x0000 }, /* R6 - Filter Control */
58 { 7, 0x0000 }, /* R7 - Mode Control 1 */
59 { 8, 0x0002 }, /* R8 - Mode Control 2 */
60 { 32, 0x0002 }, /* R32 - ADDITONAL_CONTROL_1 */
63 static bool wm8741_readable(struct device
*dev
, unsigned int reg
)
66 case WM8741_DACLLSB_ATTENUATION
:
67 case WM8741_DACLMSB_ATTENUATION
:
68 case WM8741_DACRLSB_ATTENUATION
:
69 case WM8741_DACRMSB_ATTENUATION
:
70 case WM8741_VOLUME_CONTROL
:
71 case WM8741_FORMAT_CONTROL
:
72 case WM8741_FILTER_CONTROL
:
73 case WM8741_MODE_CONTROL_1
:
74 case WM8741_MODE_CONTROL_2
:
75 case WM8741_ADDITIONAL_CONTROL_1
:
82 static int wm8741_reset(struct snd_soc_codec
*codec
)
84 return snd_soc_write(codec
, WM8741_RESET
, 0);
87 static const DECLARE_TLV_DB_SCALE(dac_tlv_fine
, -12700, 13, 0);
88 static const DECLARE_TLV_DB_SCALE(dac_tlv
, -12700, 400, 0);
90 static const struct snd_kcontrol_new wm8741_snd_controls
[] = {
91 SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION
,
92 WM8741_DACRLSB_ATTENUATION
, 1, 255, 1, dac_tlv_fine
),
93 SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION
,
94 WM8741_DACRMSB_ATTENUATION
, 0, 511, 1, dac_tlv
),
97 static const struct snd_soc_dapm_widget wm8741_dapm_widgets
[] = {
98 SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM
, 0, 0),
99 SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM
, 0, 0),
100 SND_SOC_DAPM_OUTPUT("VOUTLP"),
101 SND_SOC_DAPM_OUTPUT("VOUTLN"),
102 SND_SOC_DAPM_OUTPUT("VOUTRP"),
103 SND_SOC_DAPM_OUTPUT("VOUTRN"),
106 static const struct snd_soc_dapm_route wm8741_dapm_routes
[] = {
107 { "VOUTLP", NULL
, "DACL" },
108 { "VOUTLN", NULL
, "DACL" },
109 { "VOUTRP", NULL
, "DACR" },
110 { "VOUTRN", NULL
, "DACR" },
116 } lrclk_ratios
[WM8741_NUM_RATES
] = {
125 static unsigned int rates_11289
[] = {
129 static struct snd_pcm_hw_constraint_list constraints_11289
= {
130 .count
= ARRAY_SIZE(rates_11289
),
134 static unsigned int rates_12288
[] = {
138 static struct snd_pcm_hw_constraint_list constraints_12288
= {
139 .count
= ARRAY_SIZE(rates_12288
),
143 static unsigned int rates_16384
[] = {
147 static struct snd_pcm_hw_constraint_list constraints_16384
= {
148 .count
= ARRAY_SIZE(rates_16384
),
152 static unsigned int rates_16934
[] = {
156 static struct snd_pcm_hw_constraint_list constraints_16934
= {
157 .count
= ARRAY_SIZE(rates_16934
),
161 static unsigned int rates_18432
[] = {
165 static struct snd_pcm_hw_constraint_list constraints_18432
= {
166 .count
= ARRAY_SIZE(rates_18432
),
170 static unsigned int rates_22579
[] = {
171 44100, 88235, 1764000
174 static struct snd_pcm_hw_constraint_list constraints_22579
= {
175 .count
= ARRAY_SIZE(rates_22579
),
179 static unsigned int rates_24576
[] = {
180 32000, 48000, 96000, 192000
183 static struct snd_pcm_hw_constraint_list constraints_24576
= {
184 .count
= ARRAY_SIZE(rates_24576
),
188 static unsigned int rates_36864
[] = {
192 static struct snd_pcm_hw_constraint_list constraints_36864
= {
193 .count
= ARRAY_SIZE(rates_36864
),
198 static int wm8741_startup(struct snd_pcm_substream
*substream
,
199 struct snd_soc_dai
*dai
)
201 struct snd_soc_codec
*codec
= dai
->codec
;
202 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
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\n");
213 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
214 SNDRV_PCM_HW_PARAM_RATE
,
215 wm8741
->sysclk_constraints
);
220 static int wm8741_hw_params(struct snd_pcm_substream
*substream
,
221 struct snd_pcm_hw_params
*params
,
222 struct snd_soc_dai
*dai
)
224 struct snd_soc_codec
*codec
= dai
->codec
;
225 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
226 u16 iface
= snd_soc_read(codec
, WM8741_FORMAT_CONTROL
) & 0x1FC;
229 /* Find a supported LRCLK ratio */
230 for (i
= 0; i
< ARRAY_SIZE(lrclk_ratios
); i
++) {
231 if (wm8741
->sysclk
/ params_rate(params
) ==
232 lrclk_ratios
[i
].ratio
)
236 /* Should never happen, should be handled by constraints */
237 if (i
== ARRAY_SIZE(lrclk_ratios
)) {
238 dev_err(codec
->dev
, "MCLK/fs ratio %d unsupported\n",
239 wm8741
->sysclk
/ params_rate(params
));
244 switch (params_format(params
)) {
245 case SNDRV_PCM_FORMAT_S16_LE
:
247 case SNDRV_PCM_FORMAT_S20_3LE
:
250 case SNDRV_PCM_FORMAT_S24_LE
:
253 case SNDRV_PCM_FORMAT_S32_LE
:
257 dev_dbg(codec
->dev
, "wm8741_hw_params: Unsupported bit size param = %d",
258 params_format(params
));
262 dev_dbg(codec
->dev
, "wm8741_hw_params: bit size param = %d",
263 params_format(params
));
265 snd_soc_write(codec
, WM8741_FORMAT_CONTROL
, iface
);
269 static int wm8741_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
270 int clk_id
, unsigned int freq
, int dir
)
272 struct snd_soc_codec
*codec
= codec_dai
->codec
;
273 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
275 dev_dbg(codec
->dev
, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq
);
279 wm8741
->sysclk_constraints
= &constraints_11289
;
280 wm8741
->sysclk
= freq
;
284 wm8741
->sysclk_constraints
= &constraints_12288
;
285 wm8741
->sysclk
= freq
;
289 wm8741
->sysclk_constraints
= &constraints_16384
;
290 wm8741
->sysclk
= freq
;
294 wm8741
->sysclk_constraints
= &constraints_16934
;
295 wm8741
->sysclk
= freq
;
299 wm8741
->sysclk_constraints
= &constraints_18432
;
300 wm8741
->sysclk
= freq
;
305 wm8741
->sysclk_constraints
= &constraints_22579
;
306 wm8741
->sysclk
= freq
;
310 wm8741
->sysclk_constraints
= &constraints_24576
;
311 wm8741
->sysclk
= freq
;
315 wm8741
->sysclk_constraints
= &constraints_36864
;
316 wm8741
->sysclk
= freq
;
322 static int wm8741_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
325 struct snd_soc_codec
*codec
= codec_dai
->codec
;
326 u16 iface
= snd_soc_read(codec
, WM8741_FORMAT_CONTROL
) & 0x1C3;
328 /* check master/slave audio interface */
329 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
330 case SND_SOC_DAIFMT_CBS_CFS
:
336 /* interface format */
337 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
338 case SND_SOC_DAIFMT_I2S
:
341 case SND_SOC_DAIFMT_RIGHT_J
:
343 case SND_SOC_DAIFMT_LEFT_J
:
346 case SND_SOC_DAIFMT_DSP_A
:
349 case SND_SOC_DAIFMT_DSP_B
:
356 /* clock inversion */
357 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
358 case SND_SOC_DAIFMT_NB_NF
:
360 case SND_SOC_DAIFMT_IB_IF
:
363 case SND_SOC_DAIFMT_IB_NF
:
366 case SND_SOC_DAIFMT_NB_IF
:
374 dev_dbg(codec
->dev
, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
375 fmt
& SND_SOC_DAIFMT_FORMAT_MASK
,
376 ((fmt
& SND_SOC_DAIFMT_INV_MASK
)));
378 snd_soc_write(codec
, WM8741_FORMAT_CONTROL
, iface
);
382 #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
383 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
384 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
385 SNDRV_PCM_RATE_192000)
387 #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
388 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
390 static const struct snd_soc_dai_ops wm8741_dai_ops
= {
391 .startup
= wm8741_startup
,
392 .hw_params
= wm8741_hw_params
,
393 .set_sysclk
= wm8741_set_dai_sysclk
,
394 .set_fmt
= wm8741_set_dai_fmt
,
397 static struct snd_soc_dai_driver wm8741_dai
= {
400 .stream_name
= "Playback",
401 .channels_min
= 2, /* Mono modes not yet supported */
403 .rates
= WM8741_RATES
,
404 .formats
= WM8741_FORMATS
,
406 .ops
= &wm8741_dai_ops
,
410 static int wm8741_resume(struct snd_soc_codec
*codec
)
412 snd_soc_cache_sync(codec
);
416 #define wm8741_suspend NULL
417 #define wm8741_resume NULL
420 static int wm8741_probe(struct snd_soc_codec
*codec
)
422 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
425 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8741
->supplies
),
428 dev_err(codec
->dev
, "Failed to enable supplies: %d\n", ret
);
432 ret
= snd_soc_codec_set_cache_io(codec
, 7, 9, SND_SOC_REGMAP
);
434 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
438 ret
= wm8741_reset(codec
);
440 dev_err(codec
->dev
, "Failed to issue reset\n");
444 /* Change some default settings - latch VU */
445 snd_soc_update_bits(codec
, WM8741_DACLLSB_ATTENUATION
,
446 WM8741_UPDATELL
, WM8741_UPDATELL
);
447 snd_soc_update_bits(codec
, WM8741_DACLMSB_ATTENUATION
,
448 WM8741_UPDATELM
, WM8741_UPDATELM
);
449 snd_soc_update_bits(codec
, WM8741_DACRLSB_ATTENUATION
,
450 WM8741_UPDATERL
, WM8741_UPDATERL
);
451 snd_soc_update_bits(codec
, WM8741_DACRMSB_ATTENUATION
,
452 WM8741_UPDATERM
, WM8741_UPDATERM
);
454 dev_dbg(codec
->dev
, "Successful registration\n");
458 regulator_bulk_disable(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
463 static int wm8741_remove(struct snd_soc_codec
*codec
)
465 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
467 regulator_bulk_disable(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
472 static struct snd_soc_codec_driver soc_codec_dev_wm8741
= {
473 .probe
= wm8741_probe
,
474 .remove
= wm8741_remove
,
475 .resume
= wm8741_resume
,
477 .controls
= wm8741_snd_controls
,
478 .num_controls
= ARRAY_SIZE(wm8741_snd_controls
),
479 .dapm_widgets
= wm8741_dapm_widgets
,
480 .num_dapm_widgets
= ARRAY_SIZE(wm8741_dapm_widgets
),
481 .dapm_routes
= wm8741_dapm_routes
,
482 .num_dapm_routes
= ARRAY_SIZE(wm8741_dapm_routes
),
485 static const struct of_device_id wm8741_of_match
[] = {
486 { .compatible
= "wlf,wm8741", },
489 MODULE_DEVICE_TABLE(of
, wm8741_of_match
);
491 static const struct regmap_config wm8741_regmap
= {
494 .max_register
= WM8741_MAX_REGISTER
,
496 .reg_defaults
= wm8741_reg_defaults
,
497 .num_reg_defaults
= ARRAY_SIZE(wm8741_reg_defaults
),
498 .cache_type
= REGCACHE_RBTREE
,
500 .readable_reg
= wm8741_readable
,
503 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
504 static int wm8741_i2c_probe(struct i2c_client
*i2c
,
505 const struct i2c_device_id
*id
)
507 struct wm8741_priv
*wm8741
;
510 wm8741
= devm_kzalloc(&i2c
->dev
, sizeof(struct wm8741_priv
),
515 for (i
= 0; i
< ARRAY_SIZE(wm8741
->supplies
); i
++)
516 wm8741
->supplies
[i
].supply
= wm8741_supply_names
[i
];
518 ret
= devm_regulator_bulk_get(&i2c
->dev
, ARRAY_SIZE(wm8741
->supplies
),
521 dev_err(&i2c
->dev
, "Failed to request supplies: %d\n", ret
);
525 wm8741
->regmap
= devm_regmap_init_i2c(i2c
, &wm8741_regmap
);
526 if (IS_ERR(wm8741
->regmap
)) {
527 ret
= PTR_ERR(wm8741
->regmap
);
528 dev_err(&i2c
->dev
, "Failed to init regmap: %d\n", ret
);
532 i2c_set_clientdata(i2c
, wm8741
);
534 ret
= snd_soc_register_codec(&i2c
->dev
,
535 &soc_codec_dev_wm8741
, &wm8741_dai
, 1);
540 static int wm8741_i2c_remove(struct i2c_client
*client
)
542 snd_soc_unregister_codec(&client
->dev
);
546 static const struct i2c_device_id wm8741_i2c_id
[] = {
550 MODULE_DEVICE_TABLE(i2c
, wm8741_i2c_id
);
552 static struct i2c_driver wm8741_i2c_driver
= {
555 .owner
= THIS_MODULE
,
556 .of_match_table
= wm8741_of_match
,
558 .probe
= wm8741_i2c_probe
,
559 .remove
= wm8741_i2c_remove
,
560 .id_table
= wm8741_i2c_id
,
564 #if defined(CONFIG_SPI_MASTER)
565 static int wm8741_spi_probe(struct spi_device
*spi
)
567 struct wm8741_priv
*wm8741
;
570 wm8741
= devm_kzalloc(&spi
->dev
, sizeof(struct wm8741_priv
),
575 for (i
= 0; i
< ARRAY_SIZE(wm8741
->supplies
); i
++)
576 wm8741
->supplies
[i
].supply
= wm8741_supply_names
[i
];
578 ret
= devm_regulator_bulk_get(&spi
->dev
, ARRAY_SIZE(wm8741
->supplies
),
581 dev_err(&spi
->dev
, "Failed to request supplies: %d\n", ret
);
585 wm8741
->regmap
= devm_regmap_init_spi(spi
, &wm8741_regmap
);
586 if (IS_ERR(wm8741
->regmap
)) {
587 ret
= PTR_ERR(wm8741
->regmap
);
588 dev_err(&spi
->dev
, "Failed to init regmap: %d\n", ret
);
592 spi_set_drvdata(spi
, wm8741
);
594 ret
= snd_soc_register_codec(&spi
->dev
,
595 &soc_codec_dev_wm8741
, &wm8741_dai
, 1);
599 static int wm8741_spi_remove(struct spi_device
*spi
)
601 snd_soc_unregister_codec(&spi
->dev
);
605 static struct spi_driver wm8741_spi_driver
= {
608 .owner
= THIS_MODULE
,
609 .of_match_table
= wm8741_of_match
,
611 .probe
= wm8741_spi_probe
,
612 .remove
= wm8741_spi_remove
,
614 #endif /* CONFIG_SPI_MASTER */
616 static int __init
wm8741_modinit(void)
620 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
621 ret
= i2c_add_driver(&wm8741_i2c_driver
);
623 pr_err("Failed to register WM8741 I2C driver: %d\n", ret
);
625 #if defined(CONFIG_SPI_MASTER)
626 ret
= spi_register_driver(&wm8741_spi_driver
);
628 printk(KERN_ERR
"Failed to register wm8741 SPI driver: %d\n",
635 module_init(wm8741_modinit
);
637 static void __exit
wm8741_exit(void)
639 #if defined(CONFIG_SPI_MASTER)
640 spi_unregister_driver(&wm8741_spi_driver
);
642 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
643 i2c_del_driver(&wm8741_i2c_driver
);
646 module_exit(wm8741_exit
);
648 MODULE_DESCRIPTION("ASoC WM8741 driver");
649 MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
650 MODULE_LICENSE("GPL");