2 * wm8741.c -- WM8741 ALSA SoC Audio driver
4 * Copyright 2010 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/platform_device.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 enum snd_soc_control_type control_type
;
45 struct regulator_bulk_data supplies
[WM8741_NUM_SUPPLIES
];
47 struct snd_pcm_hw_constraint_list
*sysclk_constraints
;
50 static const u16 wm8741_reg_defaults
[WM8741_REGISTER_COUNT
] = {
51 0x0000, /* R0 - DACLLSB Attenuation */
52 0x0000, /* R1 - DACLMSB Attenuation */
53 0x0000, /* R2 - DACRLSB Attenuation */
54 0x0000, /* R3 - DACRMSB Attenuation */
55 0x0000, /* R4 - Volume Control */
56 0x000A, /* R5 - Format Control */
57 0x0000, /* R6 - Filter Control */
58 0x0000, /* R7 - Mode Control 1 */
59 0x0002, /* R8 - Mode Control 2 */
60 0x0000, /* R9 - Reset */
61 0x0002, /* R32 - ADDITONAL_CONTROL_1 */
65 static int wm8741_reset(struct snd_soc_codec
*codec
)
67 return snd_soc_write(codec
, WM8741_RESET
, 0);
70 static const DECLARE_TLV_DB_SCALE(dac_tlv_fine
, -12700, 13, 0);
71 static const DECLARE_TLV_DB_SCALE(dac_tlv
, -12700, 400, 0);
73 static const struct snd_kcontrol_new wm8741_snd_controls
[] = {
74 SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION
,
75 WM8741_DACRLSB_ATTENUATION
, 1, 255, 1, dac_tlv_fine
),
76 SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION
,
77 WM8741_DACRMSB_ATTENUATION
, 0, 511, 1, dac_tlv
),
80 static const struct snd_soc_dapm_widget wm8741_dapm_widgets
[] = {
81 SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM
, 0, 0),
82 SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM
, 0, 0),
83 SND_SOC_DAPM_OUTPUT("VOUTLP"),
84 SND_SOC_DAPM_OUTPUT("VOUTLN"),
85 SND_SOC_DAPM_OUTPUT("VOUTRP"),
86 SND_SOC_DAPM_OUTPUT("VOUTRN"),
89 static const struct snd_soc_dapm_route intercon
[] = {
90 { "VOUTLP", NULL
, "DACL" },
91 { "VOUTLN", NULL
, "DACL" },
92 { "VOUTRP", NULL
, "DACR" },
93 { "VOUTRN", NULL
, "DACR" },
96 static int wm8741_add_widgets(struct snd_soc_codec
*codec
)
98 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
100 snd_soc_dapm_new_controls(dapm
, wm8741_dapm_widgets
,
101 ARRAY_SIZE(wm8741_dapm_widgets
));
102 snd_soc_dapm_add_routes(dapm
, intercon
, ARRAY_SIZE(intercon
));
110 } lrclk_ratios
[WM8741_NUM_RATES
] = {
119 static unsigned int rates_11289
[] = {
123 static struct snd_pcm_hw_constraint_list constraints_11289
= {
124 .count
= ARRAY_SIZE(rates_11289
),
128 static unsigned int rates_12288
[] = {
132 static struct snd_pcm_hw_constraint_list constraints_12288
= {
133 .count
= ARRAY_SIZE(rates_12288
),
137 static unsigned int rates_16384
[] = {
141 static struct snd_pcm_hw_constraint_list constraints_16384
= {
142 .count
= ARRAY_SIZE(rates_16384
),
146 static unsigned int rates_16934
[] = {
150 static struct snd_pcm_hw_constraint_list constraints_16934
= {
151 .count
= ARRAY_SIZE(rates_16934
),
155 static unsigned int rates_18432
[] = {
159 static struct snd_pcm_hw_constraint_list constraints_18432
= {
160 .count
= ARRAY_SIZE(rates_18432
),
164 static unsigned int rates_22579
[] = {
165 44100, 88235, 1764000
168 static struct snd_pcm_hw_constraint_list constraints_22579
= {
169 .count
= ARRAY_SIZE(rates_22579
),
173 static unsigned int rates_24576
[] = {
174 32000, 48000, 96000, 192000
177 static struct snd_pcm_hw_constraint_list constraints_24576
= {
178 .count
= ARRAY_SIZE(rates_24576
),
182 static unsigned int rates_36864
[] = {
186 static struct snd_pcm_hw_constraint_list constraints_36864
= {
187 .count
= ARRAY_SIZE(rates_36864
),
192 static int wm8741_startup(struct snd_pcm_substream
*substream
,
193 struct snd_soc_dai
*dai
)
195 struct snd_soc_codec
*codec
= dai
->codec
;
196 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
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
) {
203 "No MCLK configured, call set_sysclk() on init\n");
207 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
208 SNDRV_PCM_HW_PARAM_RATE
,
209 wm8741
->sysclk_constraints
);
214 static int wm8741_hw_params(struct snd_pcm_substream
*substream
,
215 struct snd_pcm_hw_params
*params
,
216 struct snd_soc_dai
*dai
)
218 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
219 struct snd_soc_codec
*codec
= rtd
->codec
;
220 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
221 u16 iface
= snd_soc_read(codec
, WM8741_FORMAT_CONTROL
) & 0x1FC;
224 /* Find a supported LRCLK ratio */
225 for (i
= 0; i
< ARRAY_SIZE(lrclk_ratios
); i
++) {
226 if (wm8741
->sysclk
/ params_rate(params
) ==
227 lrclk_ratios
[i
].ratio
)
231 /* Should never happen, should be handled by constraints */
232 if (i
== ARRAY_SIZE(lrclk_ratios
)) {
233 dev_err(codec
->dev
, "MCLK/fs ratio %d unsupported\n",
234 wm8741
->sysclk
/ params_rate(params
));
239 switch (params_format(params
)) {
240 case SNDRV_PCM_FORMAT_S16_LE
:
242 case SNDRV_PCM_FORMAT_S20_3LE
:
245 case SNDRV_PCM_FORMAT_S24_LE
:
248 case SNDRV_PCM_FORMAT_S32_LE
:
252 dev_dbg(codec
->dev
, "wm8741_hw_params: Unsupported bit size param = %d",
253 params_format(params
));
257 dev_dbg(codec
->dev
, "wm8741_hw_params: bit size param = %d",
258 params_format(params
));
260 snd_soc_write(codec
, WM8741_FORMAT_CONTROL
, iface
);
264 static int wm8741_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
265 int clk_id
, unsigned int freq
, int dir
)
267 struct snd_soc_codec
*codec
= codec_dai
->codec
;
268 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
270 dev_dbg(codec
->dev
, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq
);
274 wm8741
->sysclk_constraints
= &constraints_11289
;
275 wm8741
->sysclk
= freq
;
279 wm8741
->sysclk_constraints
= &constraints_12288
;
280 wm8741
->sysclk
= freq
;
284 wm8741
->sysclk_constraints
= &constraints_16384
;
285 wm8741
->sysclk
= freq
;
289 wm8741
->sysclk_constraints
= &constraints_16934
;
290 wm8741
->sysclk
= freq
;
294 wm8741
->sysclk_constraints
= &constraints_18432
;
295 wm8741
->sysclk
= freq
;
300 wm8741
->sysclk_constraints
= &constraints_22579
;
301 wm8741
->sysclk
= freq
;
305 wm8741
->sysclk_constraints
= &constraints_24576
;
306 wm8741
->sysclk
= freq
;
310 wm8741
->sysclk_constraints
= &constraints_36864
;
311 wm8741
->sysclk
= freq
;
317 static int wm8741_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
320 struct snd_soc_codec
*codec
= codec_dai
->codec
;
321 u16 iface
= snd_soc_read(codec
, WM8741_FORMAT_CONTROL
) & 0x1C3;
323 /* check master/slave audio interface */
324 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
325 case SND_SOC_DAIFMT_CBS_CFS
:
331 /* interface format */
332 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
333 case SND_SOC_DAIFMT_I2S
:
336 case SND_SOC_DAIFMT_RIGHT_J
:
338 case SND_SOC_DAIFMT_LEFT_J
:
341 case SND_SOC_DAIFMT_DSP_A
:
344 case SND_SOC_DAIFMT_DSP_B
:
351 /* clock inversion */
352 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
353 case SND_SOC_DAIFMT_NB_NF
:
355 case SND_SOC_DAIFMT_IB_IF
:
358 case SND_SOC_DAIFMT_IB_NF
:
361 case SND_SOC_DAIFMT_NB_IF
:
369 dev_dbg(codec
->dev
, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
370 fmt
& SND_SOC_DAIFMT_FORMAT_MASK
,
371 ((fmt
& SND_SOC_DAIFMT_INV_MASK
)));
373 snd_soc_write(codec
, WM8741_FORMAT_CONTROL
, iface
);
377 #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
378 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
379 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
380 SNDRV_PCM_RATE_192000)
382 #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
383 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
385 static struct snd_soc_dai_ops wm8741_dai_ops
= {
386 .startup
= wm8741_startup
,
387 .hw_params
= wm8741_hw_params
,
388 .set_sysclk
= wm8741_set_dai_sysclk
,
389 .set_fmt
= wm8741_set_dai_fmt
,
392 static struct snd_soc_dai_driver wm8741_dai
= {
395 .stream_name
= "Playback",
396 .channels_min
= 2, /* Mono modes not yet supported */
398 .rates
= WM8741_RATES
,
399 .formats
= WM8741_FORMATS
,
401 .ops
= &wm8741_dai_ops
,
405 static int wm8741_resume(struct snd_soc_codec
*codec
)
407 snd_soc_cache_sync(codec
);
411 #define wm8741_suspend NULL
412 #define wm8741_resume NULL
415 static int wm8741_probe(struct snd_soc_codec
*codec
)
417 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
421 for (i
= 0; i
< ARRAY_SIZE(wm8741
->supplies
); i
++)
422 wm8741
->supplies
[i
].supply
= wm8741_supply_names
[i
];
424 ret
= regulator_bulk_get(codec
->dev
, ARRAY_SIZE(wm8741
->supplies
),
427 dev_err(codec
->dev
, "Failed to request supplies: %d\n", ret
);
431 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8741
->supplies
),
434 dev_err(codec
->dev
, "Failed to enable supplies: %d\n", ret
);
438 ret
= snd_soc_codec_set_cache_io(codec
, 7, 9, wm8741
->control_type
);
440 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
444 ret
= wm8741_reset(codec
);
446 dev_err(codec
->dev
, "Failed to issue reset\n");
450 /* Change some default settings - latch VU */
451 snd_soc_update_bits(codec
, WM8741_DACLLSB_ATTENUATION
,
452 WM8741_UPDATELL
, WM8741_UPDATELL
);
453 snd_soc_update_bits(codec
, WM8741_DACLMSB_ATTENUATION
,
454 WM8741_UPDATELM
, WM8741_UPDATELM
);
455 snd_soc_update_bits(codec
, WM8741_DACRLSB_ATTENUATION
,
456 WM8741_UPDATERL
, WM8741_UPDATERL
);
457 snd_soc_update_bits(codec
, WM8741_DACRMSB_ATTENUATION
,
458 WM8741_UPDATERM
, WM8741_UPDATERM
);
460 snd_soc_add_controls(codec
, wm8741_snd_controls
,
461 ARRAY_SIZE(wm8741_snd_controls
));
462 wm8741_add_widgets(codec
);
464 dev_dbg(codec
->dev
, "Successful registration\n");
468 regulator_bulk_disable(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
470 regulator_bulk_free(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
475 static int wm8741_remove(struct snd_soc_codec
*codec
)
477 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
479 regulator_bulk_disable(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
480 regulator_bulk_free(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
485 static struct snd_soc_codec_driver soc_codec_dev_wm8741
= {
486 .probe
= wm8741_probe
,
487 .remove
= wm8741_remove
,
488 .resume
= wm8741_resume
,
489 .reg_cache_size
= ARRAY_SIZE(wm8741_reg_defaults
),
490 .reg_word_size
= sizeof(u16
),
491 .reg_cache_default
= wm8741_reg_defaults
,
494 static const struct of_device_id wm8741_of_match
[] = {
495 { .compatible
= "wlf,wm8741", },
498 MODULE_DEVICE_TABLE(of
, wm8741_of_match
);
500 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
501 static int wm8741_i2c_probe(struct i2c_client
*i2c
,
502 const struct i2c_device_id
*id
)
504 struct wm8741_priv
*wm8741
;
507 wm8741
= kzalloc(sizeof(struct wm8741_priv
), GFP_KERNEL
);
511 i2c_set_clientdata(i2c
, wm8741
);
512 wm8741
->control_type
= SND_SOC_I2C
;
514 ret
= snd_soc_register_codec(&i2c
->dev
,
515 &soc_codec_dev_wm8741
, &wm8741_dai
, 1);
526 static int wm8741_i2c_remove(struct i2c_client
*client
)
528 snd_soc_unregister_codec(&client
->dev
);
529 kfree(i2c_get_clientdata(client
));
533 static const struct i2c_device_id wm8741_i2c_id
[] = {
537 MODULE_DEVICE_TABLE(i2c
, wm8741_i2c_id
);
539 static struct i2c_driver wm8741_i2c_driver
= {
542 .owner
= THIS_MODULE
,
543 .of_match_table
= wm8741_of_match
,
545 .probe
= wm8741_i2c_probe
,
546 .remove
= wm8741_i2c_remove
,
547 .id_table
= wm8741_i2c_id
,
551 #if defined(CONFIG_SPI_MASTER)
552 static int __devinit
wm8741_spi_probe(struct spi_device
*spi
)
554 struct wm8741_priv
*wm8741
;
557 wm8741
= kzalloc(sizeof(struct wm8741_priv
), GFP_KERNEL
);
561 wm8741
->control_type
= SND_SOC_SPI
;
562 spi_set_drvdata(spi
, wm8741
);
564 ret
= snd_soc_register_codec(&spi
->dev
,
565 &soc_codec_dev_wm8741
, &wm8741_dai
, 1);
571 static int __devexit
wm8741_spi_remove(struct spi_device
*spi
)
573 snd_soc_unregister_codec(&spi
->dev
);
574 kfree(spi_get_drvdata(spi
));
578 static struct spi_driver wm8741_spi_driver
= {
581 .owner
= THIS_MODULE
,
582 .of_match_table
= wm8741_of_match
,
584 .probe
= wm8741_spi_probe
,
585 .remove
= __devexit_p(wm8741_spi_remove
),
587 #endif /* CONFIG_SPI_MASTER */
589 static int __init
wm8741_modinit(void)
593 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
594 ret
= i2c_add_driver(&wm8741_i2c_driver
);
596 pr_err("Failed to register WM8741 I2C driver: %d\n", ret
);
598 #if defined(CONFIG_SPI_MASTER)
599 ret
= spi_register_driver(&wm8741_spi_driver
);
601 printk(KERN_ERR
"Failed to register wm8741 SPI driver: %d\n",
608 module_init(wm8741_modinit
);
610 static void __exit
wm8741_exit(void)
612 #if defined(CONFIG_SPI_MASTER)
613 spi_unregister_driver(&wm8741_spi_driver
);
615 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
616 i2c_del_driver(&wm8741_i2c_driver
);
619 module_exit(wm8741_exit
);
621 MODULE_DESCRIPTION("ASoC WM8741 driver");
622 MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
623 MODULE_LICENSE("GPL");