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/platform_device.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/initval.h>
28 #include <sound/tlv.h>
32 #define WM8741_NUM_SUPPLIES 2
33 static const char *wm8741_supply_names
[WM8741_NUM_SUPPLIES
] = {
38 #define WM8741_NUM_RATES 6
40 /* codec private data */
42 enum snd_soc_control_type control_type
;
43 struct regulator_bulk_data supplies
[WM8741_NUM_SUPPLIES
];
45 struct snd_pcm_hw_constraint_list
*sysclk_constraints
;
48 static const u16 wm8741_reg_defaults
[WM8741_REGISTER_COUNT
] = {
49 0x0000, /* R0 - DACLLSB Attenuation */
50 0x0000, /* R1 - DACLMSB Attenuation */
51 0x0000, /* R2 - DACRLSB Attenuation */
52 0x0000, /* R3 - DACRMSB Attenuation */
53 0x0000, /* R4 - Volume Control */
54 0x000A, /* R5 - Format Control */
55 0x0000, /* R6 - Filter Control */
56 0x0000, /* R7 - Mode Control 1 */
57 0x0002, /* R8 - Mode Control 2 */
58 0x0000, /* R9 - Reset */
59 0x0002, /* R32 - ADDITONAL_CONTROL_1 */
63 static int wm8741_reset(struct snd_soc_codec
*codec
)
65 return snd_soc_write(codec
, WM8741_RESET
, 0);
68 static const DECLARE_TLV_DB_SCALE(dac_tlv_fine
, -12700, 13, 0);
69 static const DECLARE_TLV_DB_SCALE(dac_tlv
, -12700, 400, 0);
71 static const struct snd_kcontrol_new wm8741_snd_controls
[] = {
72 SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION
,
73 WM8741_DACRLSB_ATTENUATION
, 1, 255, 1, dac_tlv_fine
),
74 SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION
,
75 WM8741_DACRMSB_ATTENUATION
, 0, 511, 1, dac_tlv
),
78 static const struct snd_soc_dapm_widget wm8741_dapm_widgets
[] = {
79 SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM
, 0, 0),
80 SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM
, 0, 0),
81 SND_SOC_DAPM_OUTPUT("VOUTLP"),
82 SND_SOC_DAPM_OUTPUT("VOUTLN"),
83 SND_SOC_DAPM_OUTPUT("VOUTRP"),
84 SND_SOC_DAPM_OUTPUT("VOUTRN"),
87 static const struct snd_soc_dapm_route intercon
[] = {
88 { "VOUTLP", NULL
, "DACL" },
89 { "VOUTLN", NULL
, "DACL" },
90 { "VOUTRP", NULL
, "DACR" },
91 { "VOUTRN", NULL
, "DACR" },
94 static int wm8741_add_widgets(struct snd_soc_codec
*codec
)
96 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
98 snd_soc_dapm_new_controls(dapm
, wm8741_dapm_widgets
,
99 ARRAY_SIZE(wm8741_dapm_widgets
));
100 snd_soc_dapm_add_routes(dapm
, intercon
, ARRAY_SIZE(intercon
));
108 } lrclk_ratios
[WM8741_NUM_RATES
] = {
117 static unsigned int rates_11289
[] = {
121 static struct snd_pcm_hw_constraint_list constraints_11289
= {
122 .count
= ARRAY_SIZE(rates_11289
),
126 static unsigned int rates_12288
[] = {
130 static struct snd_pcm_hw_constraint_list constraints_12288
= {
131 .count
= ARRAY_SIZE(rates_12288
),
135 static unsigned int rates_16384
[] = {
139 static struct snd_pcm_hw_constraint_list constraints_16384
= {
140 .count
= ARRAY_SIZE(rates_16384
),
144 static unsigned int rates_16934
[] = {
148 static struct snd_pcm_hw_constraint_list constraints_16934
= {
149 .count
= ARRAY_SIZE(rates_16934
),
153 static unsigned int rates_18432
[] = {
157 static struct snd_pcm_hw_constraint_list constraints_18432
= {
158 .count
= ARRAY_SIZE(rates_18432
),
162 static unsigned int rates_22579
[] = {
163 44100, 88235, 1764000
166 static struct snd_pcm_hw_constraint_list constraints_22579
= {
167 .count
= ARRAY_SIZE(rates_22579
),
171 static unsigned int rates_24576
[] = {
172 32000, 48000, 96000, 192000
175 static struct snd_pcm_hw_constraint_list constraints_24576
= {
176 .count
= ARRAY_SIZE(rates_24576
),
180 static unsigned int rates_36864
[] = {
184 static struct snd_pcm_hw_constraint_list constraints_36864
= {
185 .count
= ARRAY_SIZE(rates_36864
),
190 static int wm8741_startup(struct snd_pcm_substream
*substream
,
191 struct snd_soc_dai
*dai
)
193 struct snd_soc_codec
*codec
= dai
->codec
;
194 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
196 /* The set of sample rates that can be supported depends on the
197 * MCLK supplied to the CODEC - enforce this.
199 if (!wm8741
->sysclk
) {
201 "No MCLK configured, call set_sysclk() on init\n");
205 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
206 SNDRV_PCM_HW_PARAM_RATE
,
207 wm8741
->sysclk_constraints
);
212 static int wm8741_hw_params(struct snd_pcm_substream
*substream
,
213 struct snd_pcm_hw_params
*params
,
214 struct snd_soc_dai
*dai
)
216 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
217 struct snd_soc_codec
*codec
= rtd
->codec
;
218 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
219 u16 iface
= snd_soc_read(codec
, WM8741_FORMAT_CONTROL
) & 0x1FC;
222 /* Find a supported LRCLK ratio */
223 for (i
= 0; i
< ARRAY_SIZE(lrclk_ratios
); i
++) {
224 if (wm8741
->sysclk
/ params_rate(params
) ==
225 lrclk_ratios
[i
].ratio
)
229 /* Should never happen, should be handled by constraints */
230 if (i
== ARRAY_SIZE(lrclk_ratios
)) {
231 dev_err(codec
->dev
, "MCLK/fs ratio %d unsupported\n",
232 wm8741
->sysclk
/ params_rate(params
));
237 switch (params_format(params
)) {
238 case SNDRV_PCM_FORMAT_S16_LE
:
240 case SNDRV_PCM_FORMAT_S20_3LE
:
243 case SNDRV_PCM_FORMAT_S24_LE
:
246 case SNDRV_PCM_FORMAT_S32_LE
:
250 dev_dbg(codec
->dev
, "wm8741_hw_params: Unsupported bit size param = %d",
251 params_format(params
));
255 dev_dbg(codec
->dev
, "wm8741_hw_params: bit size param = %d",
256 params_format(params
));
258 snd_soc_write(codec
, WM8741_FORMAT_CONTROL
, iface
);
262 static int wm8741_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
263 int clk_id
, unsigned int freq
, int dir
)
265 struct snd_soc_codec
*codec
= codec_dai
->codec
;
266 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
268 dev_dbg(codec
->dev
, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq
);
272 wm8741
->sysclk_constraints
= &constraints_11289
;
273 wm8741
->sysclk
= freq
;
277 wm8741
->sysclk_constraints
= &constraints_12288
;
278 wm8741
->sysclk
= freq
;
282 wm8741
->sysclk_constraints
= &constraints_16384
;
283 wm8741
->sysclk
= freq
;
287 wm8741
->sysclk_constraints
= &constraints_16934
;
288 wm8741
->sysclk
= freq
;
292 wm8741
->sysclk_constraints
= &constraints_18432
;
293 wm8741
->sysclk
= freq
;
298 wm8741
->sysclk_constraints
= &constraints_22579
;
299 wm8741
->sysclk
= freq
;
303 wm8741
->sysclk_constraints
= &constraints_24576
;
304 wm8741
->sysclk
= freq
;
308 wm8741
->sysclk_constraints
= &constraints_36864
;
309 wm8741
->sysclk
= freq
;
315 static int wm8741_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
318 struct snd_soc_codec
*codec
= codec_dai
->codec
;
319 u16 iface
= snd_soc_read(codec
, WM8741_FORMAT_CONTROL
) & 0x1C3;
321 /* check master/slave audio interface */
322 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
323 case SND_SOC_DAIFMT_CBS_CFS
:
329 /* interface format */
330 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
331 case SND_SOC_DAIFMT_I2S
:
334 case SND_SOC_DAIFMT_RIGHT_J
:
336 case SND_SOC_DAIFMT_LEFT_J
:
339 case SND_SOC_DAIFMT_DSP_A
:
342 case SND_SOC_DAIFMT_DSP_B
:
349 /* clock inversion */
350 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
351 case SND_SOC_DAIFMT_NB_NF
:
353 case SND_SOC_DAIFMT_IB_IF
:
356 case SND_SOC_DAIFMT_IB_NF
:
359 case SND_SOC_DAIFMT_NB_IF
:
367 dev_dbg(codec
->dev
, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
368 fmt
& SND_SOC_DAIFMT_FORMAT_MASK
,
369 ((fmt
& SND_SOC_DAIFMT_INV_MASK
)));
371 snd_soc_write(codec
, WM8741_FORMAT_CONTROL
, iface
);
375 #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
376 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
377 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
378 SNDRV_PCM_RATE_192000)
380 #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
381 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
383 static struct snd_soc_dai_ops wm8741_dai_ops
= {
384 .startup
= wm8741_startup
,
385 .hw_params
= wm8741_hw_params
,
386 .set_sysclk
= wm8741_set_dai_sysclk
,
387 .set_fmt
= wm8741_set_dai_fmt
,
390 static struct snd_soc_dai_driver wm8741_dai
= {
393 .stream_name
= "Playback",
394 .channels_min
= 2, /* Mono modes not yet supported */
396 .rates
= WM8741_RATES
,
397 .formats
= WM8741_FORMATS
,
399 .ops
= &wm8741_dai_ops
,
403 static int wm8741_resume(struct snd_soc_codec
*codec
)
405 u16
*cache
= codec
->reg_cache
;
408 /* RESTORE REG Cache */
409 for (i
= 0; i
< WM8741_REGISTER_COUNT
; i
++) {
410 if (cache
[i
] == wm8741_reg_defaults
[i
] || WM8741_RESET
== i
)
412 snd_soc_write(codec
, i
, cache
[i
]);
417 #define wm8741_suspend NULL
418 #define wm8741_resume NULL
421 static int wm8741_probe(struct snd_soc_codec
*codec
)
423 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
426 ret
= snd_soc_codec_set_cache_io(codec
, 7, 9, wm8741
->control_type
);
428 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
432 ret
= wm8741_reset(codec
);
434 dev_err(codec
->dev
, "Failed to issue reset\n");
438 /* Change some default settings - latch VU */
439 snd_soc_update_bits(codec
, WM8741_DACLLSB_ATTENUATION
,
440 WM8741_UPDATELL
, WM8741_UPDATELL
);
441 snd_soc_update_bits(codec
, WM8741_DACLMSB_ATTENUATION
,
442 WM8741_UPDATELM
, WM8741_UPDATELM
);
443 snd_soc_update_bits(codec
, WM8741_DACRLSB_ATTENUATION
,
444 WM8741_UPDATERL
, WM8741_UPDATERL
);
445 snd_soc_update_bits(codec
, WM8741_DACRLSB_ATTENUATION
,
446 WM8741_UPDATERM
, WM8741_UPDATERM
);
448 snd_soc_add_controls(codec
, wm8741_snd_controls
,
449 ARRAY_SIZE(wm8741_snd_controls
));
450 wm8741_add_widgets(codec
);
452 dev_dbg(codec
->dev
, "Successful registration\n");
456 static struct snd_soc_codec_driver soc_codec_dev_wm8741
= {
457 .probe
= wm8741_probe
,
458 .resume
= wm8741_resume
,
459 .reg_cache_size
= ARRAY_SIZE(wm8741_reg_defaults
),
460 .reg_word_size
= sizeof(u16
),
461 .reg_cache_default
= wm8741_reg_defaults
,
464 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
465 static int wm8741_i2c_probe(struct i2c_client
*i2c
,
466 const struct i2c_device_id
*id
)
468 struct wm8741_priv
*wm8741
;
471 wm8741
= kzalloc(sizeof(struct wm8741_priv
), GFP_KERNEL
);
475 for (i
= 0; i
< ARRAY_SIZE(wm8741
->supplies
); i
++)
476 wm8741
->supplies
[i
].supply
= wm8741_supply_names
[i
];
478 ret
= regulator_bulk_get(&i2c
->dev
, ARRAY_SIZE(wm8741
->supplies
),
481 dev_err(&i2c
->dev
, "Failed to request supplies: %d\n", ret
);
485 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8741
->supplies
),
488 dev_err(&i2c
->dev
, "Failed to enable supplies: %d\n", ret
);
492 i2c_set_clientdata(i2c
, wm8741
);
493 wm8741
->control_type
= SND_SOC_I2C
;
495 ret
= snd_soc_register_codec(&i2c
->dev
,
496 &soc_codec_dev_wm8741
, &wm8741_dai
, 1);
502 regulator_bulk_disable(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
505 regulator_bulk_free(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
511 static int wm8741_i2c_remove(struct i2c_client
*client
)
513 struct wm8741_priv
*wm8741
= i2c_get_clientdata(client
);
515 snd_soc_unregister_codec(&client
->dev
);
516 regulator_bulk_free(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
517 kfree(i2c_get_clientdata(client
));
521 static const struct i2c_device_id wm8741_i2c_id
[] = {
525 MODULE_DEVICE_TABLE(i2c
, wm8741_i2c_id
);
527 static struct i2c_driver wm8741_i2c_driver
= {
529 .name
= "wm8741-codec",
530 .owner
= THIS_MODULE
,
532 .probe
= wm8741_i2c_probe
,
533 .remove
= wm8741_i2c_remove
,
534 .id_table
= wm8741_i2c_id
,
538 static int __init
wm8741_modinit(void)
542 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
543 ret
= i2c_add_driver(&wm8741_i2c_driver
);
545 pr_err("Failed to register WM8741 I2C driver: %d\n", ret
);
550 module_init(wm8741_modinit
);
552 static void __exit
wm8741_exit(void)
554 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
555 i2c_del_driver(&wm8741_i2c_driver
);
558 module_exit(wm8741_exit
);
560 MODULE_DESCRIPTION("ASoC WM8741 driver");
561 MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
562 MODULE_LICENSE("GPL");