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/soc-dapm.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 u16 reg_cache
[WM8741_REGISTER_COUNT
];
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 snd_soc_dapm_new_controls(codec
, wm8741_dapm_widgets
,
99 ARRAY_SIZE(wm8741_dapm_widgets
));
101 snd_soc_dapm_add_routes(codec
, intercon
, ARRAY_SIZE(intercon
));
109 } lrclk_ratios
[WM8741_NUM_RATES
] = {
118 static unsigned int rates_11289
[] = {
122 static struct snd_pcm_hw_constraint_list constraints_11289
= {
123 .count
= ARRAY_SIZE(rates_11289
),
127 static unsigned int rates_12288
[] = {
131 static struct snd_pcm_hw_constraint_list constraints_12288
= {
132 .count
= ARRAY_SIZE(rates_12288
),
136 static unsigned int rates_16384
[] = {
140 static struct snd_pcm_hw_constraint_list constraints_16384
= {
141 .count
= ARRAY_SIZE(rates_16384
),
145 static unsigned int rates_16934
[] = {
149 static struct snd_pcm_hw_constraint_list constraints_16934
= {
150 .count
= ARRAY_SIZE(rates_16934
),
154 static unsigned int rates_18432
[] = {
158 static struct snd_pcm_hw_constraint_list constraints_18432
= {
159 .count
= ARRAY_SIZE(rates_18432
),
163 static unsigned int rates_22579
[] = {
164 44100, 88235, 1764000
167 static struct snd_pcm_hw_constraint_list constraints_22579
= {
168 .count
= ARRAY_SIZE(rates_22579
),
172 static unsigned int rates_24576
[] = {
173 32000, 48000, 96000, 192000
176 static struct snd_pcm_hw_constraint_list constraints_24576
= {
177 .count
= ARRAY_SIZE(rates_24576
),
181 static unsigned int rates_36864
[] = {
185 static struct snd_pcm_hw_constraint_list constraints_36864
= {
186 .count
= ARRAY_SIZE(rates_36864
),
191 static int wm8741_startup(struct snd_pcm_substream
*substream
,
192 struct snd_soc_dai
*dai
)
194 struct snd_soc_codec
*codec
= dai
->codec
;
195 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
197 /* The set of sample rates that can be supported depends on the
198 * MCLK supplied to the CODEC - enforce this.
200 if (!wm8741
->sysclk
) {
202 "No MCLK configured, call set_sysclk() on init\n");
206 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
207 SNDRV_PCM_HW_PARAM_RATE
,
208 wm8741
->sysclk_constraints
);
213 static int wm8741_hw_params(struct snd_pcm_substream
*substream
,
214 struct snd_pcm_hw_params
*params
,
215 struct snd_soc_dai
*dai
)
217 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
218 struct snd_soc_codec
*codec
= rtd
->codec
;
219 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
220 u16 iface
= snd_soc_read(codec
, WM8741_FORMAT_CONTROL
) & 0x1FC;
223 /* Find a supported LRCLK ratio */
224 for (i
= 0; i
< ARRAY_SIZE(lrclk_ratios
); i
++) {
225 if (wm8741
->sysclk
/ params_rate(params
) ==
226 lrclk_ratios
[i
].ratio
)
230 /* Should never happen, should be handled by constraints */
231 if (i
== ARRAY_SIZE(lrclk_ratios
)) {
232 dev_err(codec
->dev
, "MCLK/fs ratio %d unsupported\n",
233 wm8741
->sysclk
/ params_rate(params
));
238 switch (params_format(params
)) {
239 case SNDRV_PCM_FORMAT_S16_LE
:
241 case SNDRV_PCM_FORMAT_S20_3LE
:
244 case SNDRV_PCM_FORMAT_S24_LE
:
247 case SNDRV_PCM_FORMAT_S32_LE
:
251 dev_dbg(codec
->dev
, "wm8741_hw_params: Unsupported bit size param = %d",
252 params_format(params
));
256 dev_dbg(codec
->dev
, "wm8741_hw_params: bit size param = %d",
257 params_format(params
));
259 snd_soc_write(codec
, WM8741_FORMAT_CONTROL
, iface
);
263 static int wm8741_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
264 int clk_id
, unsigned int freq
, int dir
)
266 struct snd_soc_codec
*codec
= codec_dai
->codec
;
267 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
269 dev_dbg(codec
->dev
, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq
);
273 wm8741
->sysclk_constraints
= &constraints_11289
;
274 wm8741
->sysclk
= freq
;
278 wm8741
->sysclk_constraints
= &constraints_12288
;
279 wm8741
->sysclk
= freq
;
283 wm8741
->sysclk_constraints
= &constraints_16384
;
284 wm8741
->sysclk
= freq
;
288 wm8741
->sysclk_constraints
= &constraints_16934
;
289 wm8741
->sysclk
= freq
;
293 wm8741
->sysclk_constraints
= &constraints_18432
;
294 wm8741
->sysclk
= freq
;
299 wm8741
->sysclk_constraints
= &constraints_22579
;
300 wm8741
->sysclk
= freq
;
304 wm8741
->sysclk_constraints
= &constraints_24576
;
305 wm8741
->sysclk
= freq
;
309 wm8741
->sysclk_constraints
= &constraints_36864
;
310 wm8741
->sysclk
= freq
;
316 static int wm8741_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
319 struct snd_soc_codec
*codec
= codec_dai
->codec
;
320 u16 iface
= snd_soc_read(codec
, WM8741_FORMAT_CONTROL
) & 0x1C3;
322 /* check master/slave audio interface */
323 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
324 case SND_SOC_DAIFMT_CBS_CFS
:
330 /* interface format */
331 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
332 case SND_SOC_DAIFMT_I2S
:
335 case SND_SOC_DAIFMT_RIGHT_J
:
337 case SND_SOC_DAIFMT_LEFT_J
:
340 case SND_SOC_DAIFMT_DSP_A
:
343 case SND_SOC_DAIFMT_DSP_B
:
350 /* clock inversion */
351 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
352 case SND_SOC_DAIFMT_NB_NF
:
354 case SND_SOC_DAIFMT_IB_IF
:
357 case SND_SOC_DAIFMT_IB_NF
:
360 case SND_SOC_DAIFMT_NB_IF
:
368 dev_dbg(codec
->dev
, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
369 fmt
& SND_SOC_DAIFMT_FORMAT_MASK
,
370 ((fmt
& SND_SOC_DAIFMT_INV_MASK
)));
372 snd_soc_write(codec
, WM8741_FORMAT_CONTROL
, iface
);
376 #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
377 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
378 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
379 SNDRV_PCM_RATE_192000)
381 #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
382 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
384 static struct snd_soc_dai_ops wm8741_dai_ops
= {
385 .startup
= wm8741_startup
,
386 .hw_params
= wm8741_hw_params
,
387 .set_sysclk
= wm8741_set_dai_sysclk
,
388 .set_fmt
= wm8741_set_dai_fmt
,
391 static struct snd_soc_dai_driver wm8741_dai
= {
394 .stream_name
= "Playback",
395 .channels_min
= 2, /* Mono modes not yet supported */
397 .rates
= WM8741_RATES
,
398 .formats
= WM8741_FORMATS
,
400 .ops
= &wm8741_dai_ops
,
404 static int wm8741_resume(struct snd_soc_codec
*codec
)
406 u16
*cache
= codec
->reg_cache
;
409 /* RESTORE REG Cache */
410 for (i
= 0; i
< WM8741_REGISTER_COUNT
; i
++) {
411 if (cache
[i
] == wm8741_reg_defaults
[i
] || WM8741_RESET
== i
)
413 snd_soc_write(codec
, i
, cache
[i
]);
418 #define wm8741_suspend NULL
419 #define wm8741_resume NULL
422 static int wm8741_probe(struct snd_soc_codec
*codec
)
424 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
427 ret
= snd_soc_codec_set_cache_io(codec
, 7, 9, wm8741
->control_type
);
429 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
433 ret
= wm8741_reset(codec
);
435 dev_err(codec
->dev
, "Failed to issue reset\n");
439 /* Change some default settings - latch VU */
440 wm8741
->reg_cache
[WM8741_DACLLSB_ATTENUATION
] |= WM8741_UPDATELL
;
441 wm8741
->reg_cache
[WM8741_DACLMSB_ATTENUATION
] |= WM8741_UPDATELM
;
442 wm8741
->reg_cache
[WM8741_DACRLSB_ATTENUATION
] |= WM8741_UPDATERL
;
443 wm8741
->reg_cache
[WM8741_DACRLSB_ATTENUATION
] |= WM8741_UPDATERM
;
445 snd_soc_add_controls(codec
, wm8741_snd_controls
,
446 ARRAY_SIZE(wm8741_snd_controls
));
447 wm8741_add_widgets(codec
);
449 dev_dbg(codec
->dev
, "Successful registration\n");
453 static struct snd_soc_codec_driver soc_codec_dev_wm8741
= {
454 .probe
= wm8741_probe
,
455 .resume
= wm8741_resume
,
456 .reg_cache_size
= ARRAY_SIZE(wm8741_reg_defaults
),
457 .reg_word_size
= sizeof(u16
),
458 .reg_cache_default
= &wm8741_reg_defaults
,
461 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
462 static int wm8741_i2c_probe(struct i2c_client
*i2c
,
463 const struct i2c_device_id
*id
)
465 struct wm8741_priv
*wm8741
;
468 wm8741
= kzalloc(sizeof(struct wm8741_priv
), GFP_KERNEL
);
472 for (i
= 0; i
< ARRAY_SIZE(wm8741
->supplies
); i
++)
473 wm8741
->supplies
[i
].supply
= wm8741_supply_names
[i
];
475 ret
= regulator_bulk_get(&i2c
->dev
, ARRAY_SIZE(wm8741
->supplies
),
478 dev_err(&i2c
->dev
, "Failed to request supplies: %d\n", ret
);
482 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8741
->supplies
),
485 dev_err(&i2c
->dev
, "Failed to enable supplies: %d\n", ret
);
489 i2c_set_clientdata(i2c
, wm8741
);
490 wm8741
->control_type
= SND_SOC_I2C
;
492 ret
= snd_soc_register_codec(&i2c
->dev
,
493 &soc_codec_dev_wm8741
, &wm8741_dai
, 1);
499 regulator_bulk_disable(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
502 regulator_bulk_free(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
508 static int wm8741_i2c_remove(struct i2c_client
*client
)
510 struct wm8741_priv
*wm8741
= i2c_get_clientdata(client
);
512 snd_soc_unregister_codec(&client
->dev
);
513 regulator_bulk_free(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
514 kfree(i2c_get_clientdata(client
));
518 static const struct i2c_device_id wm8741_i2c_id
[] = {
522 MODULE_DEVICE_TABLE(i2c
, wm8741_i2c_id
);
524 static struct i2c_driver wm8741_i2c_driver
= {
526 .name
= "wm8741-codec",
527 .owner
= THIS_MODULE
,
529 .probe
= wm8741_i2c_probe
,
530 .remove
= wm8741_i2c_remove
,
531 .id_table
= wm8741_i2c_id
,
535 static int __init
wm8741_modinit(void)
539 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
540 ret
= i2c_add_driver(&wm8741_i2c_driver
);
542 pr_err("Failed to register WM8741 I2C driver: %d\n", ret
);
547 module_init(wm8741_modinit
);
549 static void __exit
wm8741_exit(void)
551 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
552 i2c_del_driver(&wm8741_i2c_driver
);
555 module_exit(wm8741_exit
);
557 MODULE_DESCRIPTION("ASoC WM8741 driver");
558 MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
559 MODULE_LICENSE("GPL");