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;
45 u16 reg_cache[WM8741_REGISTER_COUNT];
46 struct regulator_bulk_data supplies[WM8741_NUM_SUPPLIES];
48 unsigned int rate_constraint_list[WM8741_NUM_RATES];
49 struct snd_pcm_hw_constraint_list rate_constraint;
52 static const u16 wm8741_reg_defaults[WM8741_REGISTER_COUNT] = {
53 0x0000, /* R0 - DACLLSB Attenuation */
54 0x0000, /* R1 - DACLMSB Attenuation */
55 0x0000, /* R2 - DACRLSB Attenuation */
56 0x0000, /* R3 - DACRMSB Attenuation */
57 0x0000, /* R4 - Volume Control */
58 0x000A, /* R5 - Format Control */
59 0x0000, /* R6 - Filter Control */
60 0x0000, /* R7 - Mode Control 1 */
61 0x0002, /* R8 - Mode Control 2 */
62 0x0000, /* R9 - Reset */
63 0x0002, /* R32 - ADDITONAL_CONTROL_1 */
67 static int wm8741_reset(struct snd_soc_codec *codec)
69 return snd_soc_write(codec, WM8741_RESET, 0);
72 static const DECLARE_TLV_DB_SCALE(dac_tlv_fine, -12700, 13, 0);
73 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 400, 0);
75 static const struct snd_kcontrol_new wm8741_snd_controls[] = {
76 SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
77 WM8741_DACRLSB_ATTENUATION, 1, 255, 1, dac_tlv_fine),
78 SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION,
79 WM8741_DACRMSB_ATTENUATION, 0, 511, 1, dac_tlv),
82 static const struct snd_soc_dapm_widget wm8741_dapm_widgets[] = {
83 SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM, 0, 0),
84 SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM, 0, 0),
85 SND_SOC_DAPM_OUTPUT("VOUTLP"),
86 SND_SOC_DAPM_OUTPUT("VOUTLN"),
87 SND_SOC_DAPM_OUTPUT("VOUTRP"),
88 SND_SOC_DAPM_OUTPUT("VOUTRN"),
91 static const struct snd_soc_dapm_route intercon[] = {
92 { "VOUTLP", NULL, "DACL" },
93 { "VOUTLN", NULL, "DACL" },
94 { "VOUTRP", NULL, "DACR" },
95 { "VOUTRN", NULL, "DACR" },
98 static int wm8741_add_widgets(struct snd_soc_codec *codec)
100 snd_soc_dapm_new_controls(codec, wm8741_dapm_widgets,
101 ARRAY_SIZE(wm8741_dapm_widgets));
103 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
111 } lrclk_ratios[WM8741_NUM_RATES] = {
119 static int wm8741_startup(struct snd_pcm_substream *substream,
120 struct snd_soc_dai *dai)
122 struct snd_soc_codec *codec = dai->codec;
123 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
125 /* The set of sample rates that can be supported depends on the
126 * MCLK supplied to the CODEC - enforce this.
128 if (!wm8741->sysclk) {
130 "No MCLK configured, call set_sysclk() on init\n");
134 snd_pcm_hw_constraint_list(substream->runtime, 0,
135 SNDRV_PCM_HW_PARAM_RATE,
136 &wm8741->rate_constraint);
141 static int wm8741_hw_params(struct snd_pcm_substream *substream,
142 struct snd_pcm_hw_params *params,
143 struct snd_soc_dai *dai)
145 struct snd_soc_pcm_runtime *rtd = substream->private_data;
146 struct snd_soc_codec *codec = rtd->codec;
147 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
148 u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1FC;
151 /* Find a supported LRCLK ratio */
152 for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
153 if (wm8741->sysclk / params_rate(params) ==
154 lrclk_ratios[i].ratio)
158 /* Should never happen, should be handled by constraints */
159 if (i == ARRAY_SIZE(lrclk_ratios)) {
160 dev_err(codec->dev, "MCLK/fs ratio %d unsupported\n",
161 wm8741->sysclk / params_rate(params));
166 switch (params_format(params)) {
167 case SNDRV_PCM_FORMAT_S16_LE:
169 case SNDRV_PCM_FORMAT_S20_3LE:
172 case SNDRV_PCM_FORMAT_S24_LE:
175 case SNDRV_PCM_FORMAT_S32_LE:
179 dev_dbg(codec->dev, "wm8741_hw_params: Unsupported bit size param = %d",
180 params_format(params));
184 dev_dbg(codec->dev, "wm8741_hw_params: bit size param = %d",
185 params_format(params));
187 snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface);
191 static int wm8741_set_dai_sysclk(struct snd_soc_dai *codec_dai,
192 int clk_id, unsigned int freq, int dir)
194 struct snd_soc_codec *codec = codec_dai->codec;
195 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
199 dev_dbg(codec->dev, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq);
201 wm8741->sysclk = freq;
203 wm8741->rate_constraint.count = 0;
205 for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
206 dev_dbg(codec->dev, "index = %d, ratio = %d, freq = %d",
207 i, lrclk_ratios[i].ratio, freq);
209 val = freq / lrclk_ratios[i].ratio;
210 /* Check that it's a standard rate since core can't
211 * cope with others and having the odd rates confuses
212 * constraint matching.
221 dev_dbg(codec->dev, "Supported sample rate: %dHz\n",
223 wm8741->rate_constraint_list[i] = val;
224 wm8741->rate_constraint.count++;
227 dev_dbg(codec->dev, "Skipping sample rate: %dHz\n",
232 /* Need at least one supported rate... */
233 if (wm8741->rate_constraint.count == 0)
239 static int wm8741_set_dai_fmt(struct snd_soc_dai *codec_dai,
242 struct snd_soc_codec *codec = codec_dai->codec;
243 u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1C3;
245 /* check master/slave audio interface */
246 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
247 case SND_SOC_DAIFMT_CBS_CFS:
253 /* interface format */
254 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
255 case SND_SOC_DAIFMT_I2S:
258 case SND_SOC_DAIFMT_RIGHT_J:
260 case SND_SOC_DAIFMT_LEFT_J:
263 case SND_SOC_DAIFMT_DSP_A:
266 case SND_SOC_DAIFMT_DSP_B:
273 /* clock inversion */
274 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
275 case SND_SOC_DAIFMT_NB_NF:
277 case SND_SOC_DAIFMT_IB_IF:
280 case SND_SOC_DAIFMT_IB_NF:
283 case SND_SOC_DAIFMT_NB_IF:
291 dev_dbg(codec->dev, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n",
292 fmt & SND_SOC_DAIFMT_FORMAT_MASK,
293 ((fmt & SND_SOC_DAIFMT_INV_MASK)));
295 snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface);
299 #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
300 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \
301 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
302 SNDRV_PCM_RATE_192000)
304 #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
305 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
307 static struct snd_soc_dai_ops wm8741_dai_ops = {
308 .startup = wm8741_startup,
309 .hw_params = wm8741_hw_params,
310 .set_sysclk = wm8741_set_dai_sysclk,
311 .set_fmt = wm8741_set_dai_fmt,
314 static struct snd_soc_dai_driver wm8741_dai = {
317 .stream_name = "Playback",
318 .channels_min = 2, /* Mono modes not yet supported */
320 .rates = WM8741_RATES,
321 .formats = WM8741_FORMATS,
323 .ops = &wm8741_dai_ops,
327 static int wm8741_resume(struct snd_soc_codec *codec)
329 u16 *cache = codec->reg_cache;
332 /* RESTORE REG Cache */
333 for (i = 0; i < WM8741_REGISTER_COUNT; i++) {
334 if (cache[i] == wm8741_reg_defaults[i] || WM8741_RESET == i)
336 snd_soc_write(codec, i, cache[i]);
341 #define wm8741_suspend NULL
342 #define wm8741_resume NULL
345 static int wm8741_probe(struct snd_soc_codec *codec)
347 struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
350 codec->control_data = wm8741->control_data;
351 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8741->control_type);
353 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
357 ret = wm8741_reset(codec);
359 dev_err(codec->dev, "Failed to issue reset\n");
363 /* Change some default settings - latch VU */
364 wm8741->reg_cache[WM8741_DACLLSB_ATTENUATION] |= WM8741_UPDATELL;
365 wm8741->reg_cache[WM8741_DACLMSB_ATTENUATION] |= WM8741_UPDATELM;
366 wm8741->reg_cache[WM8741_DACRLSB_ATTENUATION] |= WM8741_UPDATERL;
367 wm8741->reg_cache[WM8741_DACRLSB_ATTENUATION] |= WM8741_UPDATERM;
369 snd_soc_add_controls(codec, wm8741_snd_controls,
370 ARRAY_SIZE(wm8741_snd_controls));
371 wm8741_add_widgets(codec);
373 dev_dbg(codec->dev, "Successful registration\n");
377 static struct snd_soc_codec_driver soc_codec_dev_wm8741 = {
378 .probe = wm8741_probe,
379 .resume = wm8741_resume,
380 .reg_cache_size = sizeof(wm8741_reg_defaults),
381 .reg_word_size = sizeof(u16),
382 .reg_cache_default = &wm8741_reg_defaults,
385 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
386 static int wm8741_i2c_probe(struct i2c_client *i2c,
387 const struct i2c_device_id *id)
389 struct wm8741_priv *wm8741;
392 wm8741 = kzalloc(sizeof(struct wm8741_priv), GFP_KERNEL);
396 wm8741->rate_constraint.list = &wm8741->rate_constraint_list[0];
397 wm8741->rate_constraint.count =
398 ARRAY_SIZE(wm8741->rate_constraint_list);
400 for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++)
401 wm8741->supplies[i].supply = wm8741_supply_names[i];
403 ret = regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8741->supplies),
406 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
410 ret = regulator_bulk_enable(ARRAY_SIZE(wm8741->supplies),
413 dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
417 i2c_set_clientdata(i2c, wm8741);
418 wm8741->control_data = i2c;
419 wm8741->control_type = SND_SOC_I2C;
421 ret = snd_soc_register_codec(&i2c->dev,
422 &soc_codec_dev_wm8741, &wm8741_dai, 1);
428 regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
431 regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
437 static int wm8741_i2c_remove(struct i2c_client *client)
439 struct wm8741_priv *wm8741 = i2c_get_clientdata(client);
441 snd_soc_unregister_codec(&client->dev);
442 regulator_bulk_free(ARRAY_SIZE(wm8741->supplies), wm8741->supplies);
443 kfree(i2c_get_clientdata(client));
447 static const struct i2c_device_id wm8741_i2c_id[] = {
451 MODULE_DEVICE_TABLE(i2c, wm8741_i2c_id);
453 static struct i2c_driver wm8741_i2c_driver = {
455 .name = "wm8741-codec",
456 .owner = THIS_MODULE,
458 .probe = wm8741_i2c_probe,
459 .remove = wm8741_i2c_remove,
460 .id_table = wm8741_i2c_id,
464 static int __init wm8741_modinit(void)
468 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
469 ret = i2c_add_driver(&wm8741_i2c_driver);
471 pr_err("Failed to register WM8741 I2C driver: %d\n", ret);
477 module_init(wm8741_modinit);
479 static void __exit wm8741_exit(void)
481 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
482 i2c_del_driver(&wm8741_i2c_driver);
485 module_exit(wm8741_exit);
487 MODULE_DESCRIPTION("ASoC WM8741 driver");
488 MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>");
489 MODULE_LICENSE("GPL");