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/regulator/consumer.h>
22 #include <linux/slab.h>
23 #include <linux/of_device.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 #include <sound/initval.h>
29 #include <sound/tlv.h>
33 #define WM8741_NUM_SUPPLIES 2
34 static const char *wm8741_supply_names
[WM8741_NUM_SUPPLIES
] = {
39 #define WM8741_NUM_RATES 6
41 /* codec private data */
43 enum snd_soc_control_type control_type
;
44 struct regulator_bulk_data supplies
[WM8741_NUM_SUPPLIES
];
46 struct snd_pcm_hw_constraint_list
*sysclk_constraints
;
49 static const u16 wm8741_reg_defaults
[WM8741_REGISTER_COUNT
] = {
50 0x0000, /* R0 - DACLLSB Attenuation */
51 0x0000, /* R1 - DACLMSB Attenuation */
52 0x0000, /* R2 - DACRLSB Attenuation */
53 0x0000, /* R3 - DACRMSB Attenuation */
54 0x0000, /* R4 - Volume Control */
55 0x000A, /* R5 - Format Control */
56 0x0000, /* R6 - Filter Control */
57 0x0000, /* R7 - Mode Control 1 */
58 0x0002, /* R8 - Mode Control 2 */
59 0x0000, /* R9 - Reset */
60 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
[] = {
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_soc_dapm_widget wm8741_dapm_widgets
[] = {
80 SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM
, 0, 0),
81 SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM
, 0, 0),
82 SND_SOC_DAPM_OUTPUT("VOUTLP"),
83 SND_SOC_DAPM_OUTPUT("VOUTLN"),
84 SND_SOC_DAPM_OUTPUT("VOUTRP"),
85 SND_SOC_DAPM_OUTPUT("VOUTRN"),
88 static const struct snd_soc_dapm_route wm8741_dapm_routes
[] = {
89 { "VOUTLP", NULL
, "DACL" },
90 { "VOUTLN", NULL
, "DACL" },
91 { "VOUTRP", NULL
, "DACR" },
92 { "VOUTRN", NULL
, "DACR" },
98 } lrclk_ratios
[WM8741_NUM_RATES
] = {
107 static unsigned int rates_11289
[] = {
111 static struct snd_pcm_hw_constraint_list constraints_11289
= {
112 .count
= ARRAY_SIZE(rates_11289
),
116 static unsigned int rates_12288
[] = {
120 static struct snd_pcm_hw_constraint_list constraints_12288
= {
121 .count
= ARRAY_SIZE(rates_12288
),
125 static unsigned int rates_16384
[] = {
129 static struct snd_pcm_hw_constraint_list constraints_16384
= {
130 .count
= ARRAY_SIZE(rates_16384
),
134 static unsigned int rates_16934
[] = {
138 static struct snd_pcm_hw_constraint_list constraints_16934
= {
139 .count
= ARRAY_SIZE(rates_16934
),
143 static unsigned int rates_18432
[] = {
147 static struct snd_pcm_hw_constraint_list constraints_18432
= {
148 .count
= ARRAY_SIZE(rates_18432
),
152 static unsigned int rates_22579
[] = {
153 44100, 88235, 1764000
156 static struct snd_pcm_hw_constraint_list constraints_22579
= {
157 .count
= ARRAY_SIZE(rates_22579
),
161 static unsigned int rates_24576
[] = {
162 32000, 48000, 96000, 192000
165 static struct snd_pcm_hw_constraint_list constraints_24576
= {
166 .count
= ARRAY_SIZE(rates_24576
),
170 static unsigned int rates_36864
[] = {
174 static struct snd_pcm_hw_constraint_list constraints_36864
= {
175 .count
= ARRAY_SIZE(rates_36864
),
180 static int wm8741_startup(struct snd_pcm_substream
*substream
,
181 struct snd_soc_dai
*dai
)
183 struct snd_soc_codec
*codec
= dai
->codec
;
184 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
186 /* The set of sample rates that can be supported depends on the
187 * MCLK supplied to the CODEC - enforce this.
189 if (!wm8741
->sysclk
) {
191 "No MCLK configured, call set_sysclk() on init\n");
195 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
196 SNDRV_PCM_HW_PARAM_RATE
,
197 wm8741
->sysclk_constraints
);
202 static int wm8741_hw_params(struct snd_pcm_substream
*substream
,
203 struct snd_pcm_hw_params
*params
,
204 struct snd_soc_dai
*dai
)
206 struct snd_soc_codec
*codec
= dai
->codec
;
207 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
208 u16 iface
= snd_soc_read(codec
, WM8741_FORMAT_CONTROL
) & 0x1FC;
211 /* Find a supported LRCLK ratio */
212 for (i
= 0; i
< ARRAY_SIZE(lrclk_ratios
); i
++) {
213 if (wm8741
->sysclk
/ params_rate(params
) ==
214 lrclk_ratios
[i
].ratio
)
218 /* Should never happen, should be handled by constraints */
219 if (i
== ARRAY_SIZE(lrclk_ratios
)) {
220 dev_err(codec
->dev
, "MCLK/fs ratio %d unsupported\n",
221 wm8741
->sysclk
/ params_rate(params
));
226 switch (params_format(params
)) {
227 case SNDRV_PCM_FORMAT_S16_LE
:
229 case SNDRV_PCM_FORMAT_S20_3LE
:
232 case SNDRV_PCM_FORMAT_S24_LE
:
235 case SNDRV_PCM_FORMAT_S32_LE
:
239 dev_dbg(codec
->dev
, "wm8741_hw_params: Unsupported bit size param = %d",
240 params_format(params
));
244 dev_dbg(codec
->dev
, "wm8741_hw_params: bit size param = %d",
245 params_format(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
= &constraints_11289
;
262 wm8741
->sysclk
= freq
;
266 wm8741
->sysclk_constraints
= &constraints_12288
;
267 wm8741
->sysclk
= freq
;
271 wm8741
->sysclk_constraints
= &constraints_16384
;
272 wm8741
->sysclk
= freq
;
276 wm8741
->sysclk_constraints
= &constraints_16934
;
277 wm8741
->sysclk
= freq
;
281 wm8741
->sysclk_constraints
= &constraints_18432
;
282 wm8741
->sysclk
= freq
;
287 wm8741
->sysclk_constraints
= &constraints_22579
;
288 wm8741
->sysclk
= freq
;
292 wm8741
->sysclk_constraints
= &constraints_24576
;
293 wm8741
->sysclk
= freq
;
297 wm8741
->sysclk_constraints
= &constraints_36864
;
298 wm8741
->sysclk
= freq
;
304 static int wm8741_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
307 struct snd_soc_codec
*codec
= codec_dai
->codec
;
308 u16 iface
= snd_soc_read(codec
, WM8741_FORMAT_CONTROL
) & 0x1C3;
310 /* check master/slave audio interface */
311 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
312 case SND_SOC_DAIFMT_CBS_CFS
:
318 /* interface format */
319 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
320 case SND_SOC_DAIFMT_I2S
:
323 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_IB_IF
:
345 case SND_SOC_DAIFMT_IB_NF
:
348 case SND_SOC_DAIFMT_NB_IF
:
356 dev_dbg(codec
->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_write(codec
, WM8741_FORMAT_CONTROL
, iface
);
364 #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
365 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
366 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
367 SNDRV_PCM_RATE_192000)
369 #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
370 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
372 static const struct snd_soc_dai_ops wm8741_dai_ops
= {
373 .startup
= wm8741_startup
,
374 .hw_params
= wm8741_hw_params
,
375 .set_sysclk
= wm8741_set_dai_sysclk
,
376 .set_fmt
= wm8741_set_dai_fmt
,
379 static struct snd_soc_dai_driver wm8741_dai
= {
382 .stream_name
= "Playback",
383 .channels_min
= 2, /* Mono modes not yet supported */
385 .rates
= WM8741_RATES
,
386 .formats
= WM8741_FORMATS
,
388 .ops
= &wm8741_dai_ops
,
392 static int wm8741_resume(struct snd_soc_codec
*codec
)
394 snd_soc_cache_sync(codec
);
398 #define wm8741_suspend NULL
399 #define wm8741_resume NULL
402 static int wm8741_probe(struct snd_soc_codec
*codec
)
404 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
408 for (i
= 0; i
< ARRAY_SIZE(wm8741
->supplies
); i
++)
409 wm8741
->supplies
[i
].supply
= wm8741_supply_names
[i
];
411 ret
= regulator_bulk_get(codec
->dev
, ARRAY_SIZE(wm8741
->supplies
),
414 dev_err(codec
->dev
, "Failed to request supplies: %d\n", ret
);
418 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8741
->supplies
),
421 dev_err(codec
->dev
, "Failed to enable supplies: %d\n", ret
);
425 ret
= snd_soc_codec_set_cache_io(codec
, 7, 9, wm8741
->control_type
);
427 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
431 ret
= wm8741_reset(codec
);
433 dev_err(codec
->dev
, "Failed to issue reset\n");
437 /* Change some default settings - latch VU */
438 snd_soc_update_bits(codec
, WM8741_DACLLSB_ATTENUATION
,
439 WM8741_UPDATELL
, WM8741_UPDATELL
);
440 snd_soc_update_bits(codec
, WM8741_DACLMSB_ATTENUATION
,
441 WM8741_UPDATELM
, WM8741_UPDATELM
);
442 snd_soc_update_bits(codec
, WM8741_DACRLSB_ATTENUATION
,
443 WM8741_UPDATERL
, WM8741_UPDATERL
);
444 snd_soc_update_bits(codec
, WM8741_DACRMSB_ATTENUATION
,
445 WM8741_UPDATERM
, WM8741_UPDATERM
);
447 dev_dbg(codec
->dev
, "Successful registration\n");
451 regulator_bulk_disable(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
453 regulator_bulk_free(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
458 static int wm8741_remove(struct snd_soc_codec
*codec
)
460 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
462 regulator_bulk_disable(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
463 regulator_bulk_free(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
468 static struct snd_soc_codec_driver soc_codec_dev_wm8741
= {
469 .probe
= wm8741_probe
,
470 .remove
= wm8741_remove
,
471 .resume
= wm8741_resume
,
472 .reg_cache_size
= ARRAY_SIZE(wm8741_reg_defaults
),
473 .reg_word_size
= sizeof(u16
),
474 .reg_cache_default
= wm8741_reg_defaults
,
476 .controls
= wm8741_snd_controls
,
477 .num_controls
= ARRAY_SIZE(wm8741_snd_controls
),
478 .dapm_widgets
= wm8741_dapm_widgets
,
479 .num_dapm_widgets
= ARRAY_SIZE(wm8741_dapm_widgets
),
480 .dapm_routes
= wm8741_dapm_routes
,
481 .num_dapm_routes
= ARRAY_SIZE(wm8741_dapm_routes
),
484 static const struct of_device_id wm8741_of_match
[] = {
485 { .compatible
= "wlf,wm8741", },
488 MODULE_DEVICE_TABLE(of
, wm8741_of_match
);
490 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
491 static int wm8741_i2c_probe(struct i2c_client
*i2c
,
492 const struct i2c_device_id
*id
)
494 struct wm8741_priv
*wm8741
;
497 wm8741
= devm_kzalloc(&i2c
->dev
, sizeof(struct wm8741_priv
),
502 i2c_set_clientdata(i2c
, wm8741
);
503 wm8741
->control_type
= SND_SOC_I2C
;
505 ret
= snd_soc_register_codec(&i2c
->dev
,
506 &soc_codec_dev_wm8741
, &wm8741_dai
, 1);
511 static int wm8741_i2c_remove(struct i2c_client
*client
)
513 snd_soc_unregister_codec(&client
->dev
);
517 static const struct i2c_device_id wm8741_i2c_id
[] = {
521 MODULE_DEVICE_TABLE(i2c
, wm8741_i2c_id
);
523 static struct i2c_driver wm8741_i2c_driver
= {
526 .owner
= THIS_MODULE
,
527 .of_match_table
= wm8741_of_match
,
529 .probe
= wm8741_i2c_probe
,
530 .remove
= wm8741_i2c_remove
,
531 .id_table
= wm8741_i2c_id
,
535 #if defined(CONFIG_SPI_MASTER)
536 static int __devinit
wm8741_spi_probe(struct spi_device
*spi
)
538 struct wm8741_priv
*wm8741
;
541 wm8741
= devm_kzalloc(&spi
->dev
, sizeof(struct wm8741_priv
),
546 wm8741
->control_type
= SND_SOC_SPI
;
547 spi_set_drvdata(spi
, wm8741
);
549 ret
= snd_soc_register_codec(&spi
->dev
,
550 &soc_codec_dev_wm8741
, &wm8741_dai
, 1);
554 static int __devexit
wm8741_spi_remove(struct spi_device
*spi
)
556 snd_soc_unregister_codec(&spi
->dev
);
560 static struct spi_driver wm8741_spi_driver
= {
563 .owner
= THIS_MODULE
,
564 .of_match_table
= wm8741_of_match
,
566 .probe
= wm8741_spi_probe
,
567 .remove
= __devexit_p(wm8741_spi_remove
),
569 #endif /* CONFIG_SPI_MASTER */
571 static int __init
wm8741_modinit(void)
575 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
576 ret
= i2c_add_driver(&wm8741_i2c_driver
);
578 pr_err("Failed to register WM8741 I2C driver: %d\n", ret
);
580 #if defined(CONFIG_SPI_MASTER)
581 ret
= spi_register_driver(&wm8741_spi_driver
);
583 printk(KERN_ERR
"Failed to register wm8741 SPI driver: %d\n",
590 module_init(wm8741_modinit
);
592 static void __exit
wm8741_exit(void)
594 #if defined(CONFIG_SPI_MASTER)
595 spi_unregister_driver(&wm8741_spi_driver
);
597 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
598 i2c_del_driver(&wm8741_i2c_driver
);
601 module_exit(wm8741_exit
);
603 MODULE_DESCRIPTION("ASoC WM8741 driver");
604 MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
605 MODULE_LICENSE("GPL");