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 static struct snd_soc_codec
*wm8741_codec
;
34 struct snd_soc_codec_device soc_codec_dev_wm8741
;
36 #define WM8741_NUM_SUPPLIES 2
37 static const char *wm8741_supply_names
[WM8741_NUM_SUPPLIES
] = {
42 #define WM8741_NUM_RATES 4
44 /* codec private data */
46 struct snd_soc_codec codec
;
47 u16 reg_cache
[WM8741_REGISTER_COUNT
];
48 struct regulator_bulk_data supplies
[WM8741_NUM_SUPPLIES
];
50 unsigned int rate_constraint_list
[WM8741_NUM_RATES
];
51 struct snd_pcm_hw_constraint_list rate_constraint
;
54 static const u16 wm8741_reg_defaults
[WM8741_REGISTER_COUNT
] = {
55 0x0000, /* R0 - DACLLSB Attenuation */
56 0x0000, /* R1 - DACLMSB Attenuation */
57 0x0000, /* R2 - DACRLSB Attenuation */
58 0x0000, /* R3 - DACRMSB Attenuation */
59 0x0000, /* R4 - Volume Control */
60 0x000A, /* R5 - Format Control */
61 0x0000, /* R6 - Filter Control */
62 0x0000, /* R7 - Mode Control 1 */
63 0x0002, /* R8 - Mode Control 2 */
64 0x0000, /* R9 - Reset */
65 0x0002, /* R32 - ADDITONAL_CONTROL_1 */
69 static int wm8741_reset(struct snd_soc_codec
*codec
)
71 return snd_soc_write(codec
, WM8741_RESET
, 0);
74 static const DECLARE_TLV_DB_SCALE(dac_tlv_fine
, -12700, 13, 0);
75 static const DECLARE_TLV_DB_SCALE(dac_tlv
, -12700, 400, 0);
77 static const struct snd_kcontrol_new wm8741_snd_controls
[] = {
78 SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION
,
79 WM8741_DACRLSB_ATTENUATION
, 1, 255, 1, dac_tlv_fine
),
80 SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION
,
81 WM8741_DACRMSB_ATTENUATION
, 0, 511, 1, dac_tlv
),
84 static const struct snd_soc_dapm_widget wm8741_dapm_widgets
[] = {
85 SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM
, 0, 0),
86 SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM
, 0, 0),
87 SND_SOC_DAPM_OUTPUT("VOUTLP"),
88 SND_SOC_DAPM_OUTPUT("VOUTLN"),
89 SND_SOC_DAPM_OUTPUT("VOUTRP"),
90 SND_SOC_DAPM_OUTPUT("VOUTRN"),
93 static const struct snd_soc_dapm_route intercon
[] = {
94 { "VOUTLP", NULL
, "DACL" },
95 { "VOUTLN", NULL
, "DACL" },
96 { "VOUTRP", NULL
, "DACR" },
97 { "VOUTRN", NULL
, "DACR" },
100 static int wm8741_add_widgets(struct snd_soc_codec
*codec
)
102 snd_soc_dapm_new_controls(codec
, wm8741_dapm_widgets
,
103 ARRAY_SIZE(wm8741_dapm_widgets
));
105 snd_soc_dapm_add_routes(codec
, intercon
, ARRAY_SIZE(intercon
));
113 } lrclk_ratios
[WM8741_NUM_RATES
] = {
121 static int wm8741_startup(struct snd_pcm_substream
*substream
,
122 struct snd_soc_dai
*dai
)
124 struct snd_soc_codec
*codec
= dai
->codec
;
125 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
127 /* The set of sample rates that can be supported depends on the
128 * MCLK supplied to the CODEC - enforce this.
130 if (!wm8741
->sysclk
) {
132 "No MCLK configured, call set_sysclk() on init\n");
136 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
137 SNDRV_PCM_HW_PARAM_RATE
,
138 &wm8741
->rate_constraint
);
143 static int wm8741_hw_params(struct snd_pcm_substream
*substream
,
144 struct snd_pcm_hw_params
*params
,
145 struct snd_soc_dai
*dai
)
147 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
148 struct snd_soc_device
*socdev
= rtd
->socdev
;
149 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
150 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
151 u16 iface
= snd_soc_read(codec
, WM8741_FORMAT_CONTROL
) & 0x1FC;
154 /* Find a supported LRCLK ratio */
155 for (i
= 0; i
< ARRAY_SIZE(lrclk_ratios
); i
++) {
156 if (wm8741
->sysclk
/ params_rate(params
) ==
157 lrclk_ratios
[i
].ratio
)
161 /* Should never happen, should be handled by constraints */
162 if (i
== ARRAY_SIZE(lrclk_ratios
)) {
163 dev_err(codec
->dev
, "MCLK/fs ratio %d unsupported\n",
164 wm8741
->sysclk
/ params_rate(params
));
169 switch (params_format(params
)) {
170 case SNDRV_PCM_FORMAT_S16_LE
:
172 case SNDRV_PCM_FORMAT_S20_3LE
:
175 case SNDRV_PCM_FORMAT_S24_LE
:
178 case SNDRV_PCM_FORMAT_S32_LE
:
182 dev_dbg(codec
->dev
, "wm8741_hw_params: Unsupported bit size param = %d",
183 params_format(params
));
187 dev_dbg(codec
->dev
, "wm8741_hw_params: bit size param = %d",
188 params_format(params
));
190 snd_soc_write(codec
, WM8741_FORMAT_CONTROL
, iface
);
194 static int wm8741_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
195 int clk_id
, unsigned int freq
, int dir
)
197 struct snd_soc_codec
*codec
= codec_dai
->codec
;
198 struct wm8741_priv
*wm8741
= snd_soc_codec_get_drvdata(codec
);
202 dev_dbg(codec
->dev
, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq
);
204 wm8741
->sysclk
= freq
;
206 wm8741
->rate_constraint
.count
= 0;
208 for (i
= 0; i
< ARRAY_SIZE(lrclk_ratios
); i
++) {
209 dev_dbg(codec
->dev
, "index = %d, ratio = %d, freq = %d",
210 i
, lrclk_ratios
[i
].ratio
, freq
);
212 val
= freq
/ lrclk_ratios
[i
].ratio
;
213 /* Check that it's a standard rate since core can't
214 * cope with others and having the odd rates confuses
215 * constraint matching.
224 dev_dbg(codec
->dev
, "Supported sample rate: %dHz\n",
226 wm8741
->rate_constraint_list
[i
] = val
;
227 wm8741
->rate_constraint
.count
++;
230 dev_dbg(codec
->dev
, "Skipping sample rate: %dHz\n",
235 /* Need at least one supported rate... */
236 if (wm8741
->rate_constraint
.count
== 0)
242 static int wm8741_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
245 struct snd_soc_codec
*codec
= codec_dai
->codec
;
246 u16 iface
= snd_soc_read(codec
, WM8741_FORMAT_CONTROL
) & 0x1C3;
248 /* check master/slave audio interface */
249 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
250 case SND_SOC_DAIFMT_CBS_CFS
:
256 /* interface format */
257 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
258 case SND_SOC_DAIFMT_I2S
:
261 case SND_SOC_DAIFMT_RIGHT_J
:
263 case SND_SOC_DAIFMT_LEFT_J
:
266 case SND_SOC_DAIFMT_DSP_A
:
269 case SND_SOC_DAIFMT_DSP_B
:
276 /* clock inversion */
277 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
278 case SND_SOC_DAIFMT_NB_NF
:
280 case SND_SOC_DAIFMT_IB_IF
:
283 case SND_SOC_DAIFMT_IB_NF
:
286 case SND_SOC_DAIFMT_NB_IF
:
294 dev_dbg(codec
->dev
, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
295 fmt
& SND_SOC_DAIFMT_FORMAT_MASK
,
296 ((fmt
& SND_SOC_DAIFMT_INV_MASK
)));
298 snd_soc_write(codec
, WM8741_FORMAT_CONTROL
, iface
);
302 #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
303 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
304 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
305 SNDRV_PCM_RATE_192000)
307 #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
308 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
310 static struct snd_soc_dai_ops wm8741_dai_ops
= {
311 .startup
= wm8741_startup
,
312 .hw_params
= wm8741_hw_params
,
313 .set_sysclk
= wm8741_set_dai_sysclk
,
314 .set_fmt
= wm8741_set_dai_fmt
,
317 struct snd_soc_dai wm8741_dai
= {
320 .stream_name
= "Playback",
321 .channels_min
= 2, /* Mono modes not yet supported */
323 .rates
= WM8741_RATES
,
324 .formats
= WM8741_FORMATS
,
326 .ops
= &wm8741_dai_ops
,
328 EXPORT_SYMBOL_GPL(wm8741_dai
);
331 static int wm8741_resume(struct platform_device
*pdev
)
333 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
334 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
335 u16
*cache
= codec
->reg_cache
;
338 /* RESTORE REG Cache */
339 for (i
= 0; i
< WM8741_REGISTER_COUNT
; i
++) {
340 if (cache
[i
] == wm8741_reg_defaults
[i
] || WM8741_RESET
== i
)
342 snd_soc_write(codec
, i
, cache
[i
]);
347 #define wm8741_suspend NULL
348 #define wm8741_resume NULL
351 static int wm8741_probe(struct platform_device
*pdev
)
353 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
354 struct snd_soc_codec
*codec
;
357 if (wm8741_codec
== NULL
) {
358 dev_err(&pdev
->dev
, "Codec device not registered\n");
362 socdev
->card
->codec
= wm8741_codec
;
363 codec
= wm8741_codec
;
366 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
368 dev_err(codec
->dev
, "failed to create pcms: %d\n", ret
);
372 snd_soc_add_controls(codec
, wm8741_snd_controls
,
373 ARRAY_SIZE(wm8741_snd_controls
));
374 wm8741_add_widgets(codec
);
382 static int wm8741_remove(struct platform_device
*pdev
)
384 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
386 snd_soc_free_pcms(socdev
);
387 snd_soc_dapm_free(socdev
);
392 struct snd_soc_codec_device soc_codec_dev_wm8741
= {
393 .probe
= wm8741_probe
,
394 .remove
= wm8741_remove
,
395 .resume
= wm8741_resume
,
397 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8741
);
399 static int wm8741_register(struct wm8741_priv
*wm8741
,
400 enum snd_soc_control_type control
)
403 struct snd_soc_codec
*codec
= &wm8741
->codec
;
407 dev_err(codec
->dev
, "Another WM8741 is registered\n");
411 mutex_init(&codec
->mutex
);
412 INIT_LIST_HEAD(&codec
->dapm_widgets
);
413 INIT_LIST_HEAD(&codec
->dapm_paths
);
415 snd_soc_codec_set_drvdata(codec
, wm8741
);
416 codec
->name
= "WM8741";
417 codec
->owner
= THIS_MODULE
;
418 codec
->bias_level
= SND_SOC_BIAS_OFF
;
419 codec
->set_bias_level
= NULL
;
420 codec
->dai
= &wm8741_dai
;
422 codec
->reg_cache_size
= WM8741_REGISTER_COUNT
;
423 codec
->reg_cache
= &wm8741
->reg_cache
;
425 wm8741
->rate_constraint
.list
= &wm8741
->rate_constraint_list
[0];
426 wm8741
->rate_constraint
.count
=
427 ARRAY_SIZE(wm8741
->rate_constraint_list
);
429 memcpy(codec
->reg_cache
, wm8741_reg_defaults
,
430 sizeof(wm8741
->reg_cache
));
432 ret
= snd_soc_codec_set_cache_io(codec
, 7, 9, control
);
434 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
438 for (i
= 0; i
< ARRAY_SIZE(wm8741
->supplies
); i
++)
439 wm8741
->supplies
[i
].supply
= wm8741_supply_names
[i
];
441 ret
= regulator_bulk_get(codec
->dev
, ARRAY_SIZE(wm8741
->supplies
),
444 dev_err(codec
->dev
, "Failed to request supplies: %d\n", ret
);
448 ret
= regulator_bulk_enable(ARRAY_SIZE(wm8741
->supplies
),
451 dev_err(codec
->dev
, "Failed to enable supplies: %d\n", ret
);
455 ret
= wm8741_reset(codec
);
457 dev_err(codec
->dev
, "Failed to issue reset\n");
461 wm8741_dai
.dev
= codec
->dev
;
463 /* Change some default settings - latch VU */
464 wm8741
->reg_cache
[WM8741_DACLLSB_ATTENUATION
] |= WM8741_UPDATELL
;
465 wm8741
->reg_cache
[WM8741_DACLMSB_ATTENUATION
] |= WM8741_UPDATELM
;
466 wm8741
->reg_cache
[WM8741_DACRLSB_ATTENUATION
] |= WM8741_UPDATERL
;
467 wm8741
->reg_cache
[WM8741_DACRLSB_ATTENUATION
] |= WM8741_UPDATERM
;
469 wm8741_codec
= codec
;
471 ret
= snd_soc_register_codec(codec
);
473 dev_err(codec
->dev
, "Failed to register codec: %d\n", ret
);
477 ret
= snd_soc_register_dai(&wm8741_dai
);
479 dev_err(codec
->dev
, "Failed to register DAI: %d\n", ret
);
480 snd_soc_unregister_codec(codec
);
484 dev_dbg(codec
->dev
, "Successful registration\n");
488 regulator_bulk_disable(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
491 regulator_bulk_free(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
498 static void wm8741_unregister(struct wm8741_priv
*wm8741
)
500 regulator_bulk_free(ARRAY_SIZE(wm8741
->supplies
), wm8741
->supplies
);
502 snd_soc_unregister_dai(&wm8741_dai
);
503 snd_soc_unregister_codec(&wm8741
->codec
);
508 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
509 static __devinit
int wm8741_i2c_probe(struct i2c_client
*i2c
,
510 const struct i2c_device_id
*id
)
512 struct wm8741_priv
*wm8741
;
513 struct snd_soc_codec
*codec
;
515 wm8741
= kzalloc(sizeof(struct wm8741_priv
), GFP_KERNEL
);
519 codec
= &wm8741
->codec
;
520 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
522 i2c_set_clientdata(i2c
, wm8741
);
523 codec
->control_data
= i2c
;
525 codec
->dev
= &i2c
->dev
;
527 return wm8741_register(wm8741
, SND_SOC_I2C
);
530 static __devexit
int wm8741_i2c_remove(struct i2c_client
*client
)
532 struct wm8741_priv
*wm8741
= i2c_get_clientdata(client
);
533 wm8741_unregister(wm8741
);
537 static const struct i2c_device_id wm8741_i2c_id
[] = {
541 MODULE_DEVICE_TABLE(i2c
, wm8741_i2c_id
);
544 static struct i2c_driver wm8741_i2c_driver
= {
547 .owner
= THIS_MODULE
,
549 .probe
= wm8741_i2c_probe
,
550 .remove
= __devexit_p(wm8741_i2c_remove
),
551 .id_table
= wm8741_i2c_id
,
555 static int __init
wm8741_modinit(void)
558 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
559 ret
= i2c_add_driver(&wm8741_i2c_driver
);
561 printk(KERN_ERR
"Failed to register WM8741 I2C driver: %d\n",
567 module_init(wm8741_modinit
);
569 static void __exit
wm8741_exit(void)
571 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
572 i2c_del_driver(&wm8741_i2c_driver
);
575 module_exit(wm8741_exit
);
577 MODULE_DESCRIPTION("ASoC WM8741 driver");
578 MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
579 MODULE_LICENSE("GPL");