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 4
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 unsigned int rate_constraint_list[WM8741_NUM_RATES];
48 struct snd_pcm_hw_constraint_list rate_constraint;
51 static const u16 wm8741_reg_defaults[WM8741_REGISTER_COUNT] = {
52 0x0000, /* R0 - DACLLSB Attenuation */
53 0x0000, /* R1 - DACLMSB Attenuation */
54 0x0000, /* R2 - DACRLSB Attenuation */
55 0x0000, /* R3 - DACRMSB Attenuation */
56 0x0000, /* R4 - Volume Control */
57 0x000A, /* R5 - Format Control */
58 0x0000, /* R6 - Filter Control */
59 0x0000, /* R7 - Mode Control 1 */
60 0x0002, /* R8 - Mode Control 2 */
61 0x0000, /* R9 - Reset */
62 0x0002, /* R32 - ADDITONAL_CONTROL_1 */
66 static int wm8741_reset(struct snd_soc_codec *codec)
68 return snd_soc_write(codec, WM8741_RESET, 0);
71 static const DECLARE_TLV_DB_SCALE(dac_tlv_fine, -12700, 13, 0);
72 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 400, 0);
74 static const struct snd_kcontrol_new wm8741_snd_controls[] = {
75 SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
76 WM8741_DACRLSB_ATTENUATION, 1, 255, 1, dac_tlv_fine),
77 SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION,
78 WM8741_DACRMSB_ATTENUATION, 0, 511, 1, dac_tlv),
81 static const struct snd_soc_dapm_widget wm8741_dapm_widgets[] = {
82 SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM, 0, 0),
83 SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM, 0, 0),
84 SND_SOC_DAPM_OUTPUT("VOUTLP"),
85 SND_SOC_DAPM_OUTPUT("VOUTLN"),
86 SND_SOC_DAPM_OUTPUT("VOUTRP"),
87 SND_SOC_DAPM_OUTPUT("VOUTRN"),
90 static const struct snd_soc_dapm_route intercon[] = {
91 { "VOUTLP", NULL, "DACL" },
92 { "VOUTLN", NULL, "DACL" },
93 { "VOUTRP", NULL, "DACR" },
94 { "VOUTRN", NULL, "DACR" },
97 static int wm8741_add_widgets(struct snd_soc_codec *codec)
99 snd_soc_dapm_new_controls(codec, wm8741_dapm_widgets,
100 ARRAY_SIZE(wm8741_dapm_widgets));
102 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
110 } lrclk_ratios[WM8741_NUM_RATES] = {
118 static int wm8741_startup(struct snd_pcm_substream *substream,
119 struct snd_soc_dai *dai)
121 struct snd_soc_codec *codec = dai->codec;
122 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
124 /* The set of sample rates that can be supported depends on the
125 * MCLK supplied to the CODEC - enforce this.
127 if (!wm8741->sysclk) {
129 "No MCLK configured, call set_sysclk() on init\n");
133 snd_pcm_hw_constraint_list(substream->runtime, 0,
134 SNDRV_PCM_HW_PARAM_RATE,
135 &wm8741->rate_constraint);
140 static int wm8741_hw_params(struct snd_pcm_substream *substream,
141 struct snd_pcm_hw_params *params,
142 struct snd_soc_dai *dai)
144 struct snd_soc_pcm_runtime *rtd = substream->private_data;
145 struct snd_soc_codec *codec = rtd->codec;
146 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
147 u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1FC;
150 /* Find a supported LRCLK ratio */
151 for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
152 if (wm8741->sysclk / params_rate(params) ==
153 lrclk_ratios[i].ratio)
157 /* Should never happen, should be handled by constraints */
158 if (i == ARRAY_SIZE(lrclk_ratios)) {
159 dev_err(codec->dev, "MCLK/fs ratio %d unsupported\n",
160 wm8741->sysclk / params_rate(params));
165 switch (params_format(params)) {
166 case SNDRV_PCM_FORMAT_S16_LE:
168 case SNDRV_PCM_FORMAT_S20_3LE:
171 case SNDRV_PCM_FORMAT_S24_LE:
174 case SNDRV_PCM_FORMAT_S32_LE:
178 dev_dbg(codec->dev, "wm8741_hw_params: Unsupported bit size param = %d",
179 params_format(params));
183 dev_dbg(codec->dev, "wm8741_hw_params: bit size param = %d",
184 params_format(params));
186 snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface);
190 static int wm8741_set_dai_sysclk(struct snd_soc_dai *codec_dai,
191 int clk_id, unsigned int freq, int dir)
193 struct snd_soc_codec *codec = codec_dai->codec;
194 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
198 dev_dbg(codec->dev, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq);
200 wm8741->sysclk = freq;
202 wm8741->rate_constraint.count = 0;
204 for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
205 dev_dbg(codec->dev, "index = %d, ratio = %d, freq = %d",
206 i, lrclk_ratios[i].ratio, freq);
208 val = freq / lrclk_ratios[i].ratio;
209 /* Check that it's a standard rate since core can't
210 * cope with others and having the odd rates confuses
211 * constraint matching.
220 dev_dbg(codec->dev, "Supported sample rate: %dHz\n",
222 wm8741->rate_constraint_list[i] = val;
223 wm8741->rate_constraint.count++;
226 dev_dbg(codec->dev, "Skipping sample rate: %dHz\n",
231 /* Need at least one supported rate... */
232 if (wm8741->rate_constraint.count == 0)
238 static int wm8741_set_dai_fmt(struct snd_soc_dai *codec_dai,
241 struct snd_soc_codec *codec = codec_dai->codec;
242 u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1C3;
244 /* check master/slave audio interface */
245 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
246 case SND_SOC_DAIFMT_CBS_CFS:
252 /* interface format */
253 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
254 case SND_SOC_DAIFMT_I2S:
257 case SND_SOC_DAIFMT_RIGHT_J:
259 case SND_SOC_DAIFMT_LEFT_J:
262 case SND_SOC_DAIFMT_DSP_A:
265 case SND_SOC_DAIFMT_DSP_B:
272 /* clock inversion */
273 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
274 case SND_SOC_DAIFMT_NB_NF:
276 case SND_SOC_DAIFMT_IB_IF:
279 case SND_SOC_DAIFMT_IB_NF:
282 case SND_SOC_DAIFMT_NB_IF:
290 dev_dbg(codec->dev, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
291 fmt & SND_SOC_DAIFMT_FORMAT_MASK,
292 ((fmt & SND_SOC_DAIFMT_INV_MASK)));
294 snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface);
298 #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
299 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
300 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
301 SNDRV_PCM_RATE_192000)
303 #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
304 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
306 static struct snd_soc_dai_ops wm8741_dai_ops = {
307 .startup = wm8741_startup,
308 .hw_params = wm8741_hw_params,
309 .set_sysclk = wm8741_set_dai_sysclk,
310 .set_fmt = wm8741_set_dai_fmt,
313 static struct snd_soc_dai_driver wm8741_dai = {
316 .stream_name = "Playback",
317 .channels_min = 2, /* Mono modes not yet supported */
319 .rates = WM8741_RATES,
320 .formats = WM8741_FORMATS,
322 .ops = &wm8741_dai_ops,
326 static int wm8741_resume(struct snd_soc_codec *codec)
328 u16 *cache = codec->reg_cache;
331 /* RESTORE REG Cache */
332 for (i = 0; i < WM8741_REGISTER_COUNT; i++) {
333 if (cache[i] == wm8741_reg_defaults[i] || WM8741_RESET == i)
335 snd_soc_write(codec, i, cache[i]);
340 #define wm8741_suspend NULL
341 #define wm8741_resume NULL
344 static int wm8741_probe(struct snd_soc_codec *codec)
346 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
349 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8741->control_type);
351 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
355 ret = wm8741_reset(codec);
357 dev_err(codec->dev, "Failed to issue reset\n");
361 /* Change some default settings - latch VU */
362 wm8741->reg_cache[WM8741_DACLLSB_ATTENUATION] |= WM8741_UPDATELL;
363 wm8741->reg_cache[WM8741_DACLMSB_ATTENUATION] |= WM8741_UPDATELM;
364 wm8741->reg_cache[WM8741_DACRLSB_ATTENUATION] |= WM8741_UPDATERL;
365 wm8741->reg_cache[WM8741_DACRLSB_ATTENUATION] |= WM8741_UPDATERM;
367 snd_soc_add_controls(codec, wm8741_snd_controls,
368 ARRAY_SIZE(wm8741_snd_controls));
369 wm8741_add_widgets(codec);
371 dev_dbg(codec->dev, "Successful registration\n");
375 static struct snd_soc_codec_driver soc_codec_dev_wm8741 = {
376 .probe = wm8741_probe,
377 .resume = wm8741_resume,
378 .reg_cache_size = sizeof(wm8741_reg_defaults),
379 .reg_word_size = sizeof(u16),
380 .reg_cache_default = &wm8741_reg_defaults,
383 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
384 static int wm8741_i2c_probe(struct i2c_client *i2c,
385 const struct i2c_device_id *id)
387 struct wm8741_priv *wm8741;
390 wm8741 = kzalloc(sizeof(struct wm8741_priv), GFP_KERNEL);
394 wm8741->rate_constraint.list = &wm8741->rate_constraint_list[0];
395 wm8741->rate_constraint.count =
396 ARRAY_SIZE(wm8741->rate_constraint_list);
398 for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++)
399 wm8741->supplies[i].supply = wm8741_supply_names[i];
401 ret = regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8741->supplies),
404 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
408 ret = regulator_bulk_enable(ARRAY_SIZE(wm8741->supplies),
411 dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
415 i2c_set_clientdata(i2c, wm8741);
416 wm8741->control_type = SND_SOC_I2C;
418 ret = snd_soc_register_codec(&i2c->dev,
419 &soc_codec_dev_wm8741, &wm8741_dai, 1);
425 regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
428 regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
434 static int wm8741_i2c_remove(struct i2c_client *client)
436 struct wm8741_priv *wm8741 = i2c_get_clientdata(client);
438 snd_soc_unregister_codec(&client->dev);
439 regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
440 kfree(i2c_get_clientdata(client));
444 static const struct i2c_device_id wm8741_i2c_id[] = {
448 MODULE_DEVICE_TABLE(i2c, wm8741_i2c_id);
450 static struct i2c_driver wm8741_i2c_driver = {
452 .name = "wm8741-codec",
453 .owner = THIS_MODULE,
455 .probe = wm8741_i2c_probe,
456 .remove = wm8741_i2c_remove,
457 .id_table = wm8741_i2c_id,
461 static int __init wm8741_modinit(void)
465 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
466 ret = i2c_add_driver(&wm8741_i2c_driver);
468 pr_err("Failed to register WM8741 I2C driver: %d\n", ret);
474 module_init(wm8741_modinit);
476 static void __exit wm8741_exit(void)
478 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
479 i2c_del_driver(&wm8741_i2c_driver);
482 module_exit(wm8741_exit);
484 MODULE_DESCRIPTION("ASoC WM8741 driver");
485 MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
486 MODULE_LICENSE("GPL");