2 * wm8523.c -- WM8523 ALSA SoC Audio driver
4 * Copyright 2009 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@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 WM8523_NUM_SUPPLIES 2
34 static const char *wm8523_supply_names[WM8523_NUM_SUPPLIES] = {
39 #define WM8523_NUM_RATES 7
41 /* codec private data */
43 enum snd_soc_control_type control_type;
45 u16 reg_cache[WM8523_REGISTER_COUNT];
46 struct regulator_bulk_data supplies[WM8523_NUM_SUPPLIES];
48 unsigned int rate_constraint_list[WM8523_NUM_RATES];
49 struct snd_pcm_hw_constraint_list rate_constraint;
52 static const u16 wm8523_reg[WM8523_REGISTER_COUNT] = {
53 0x8523, /* R0 - DEVICE_ID */
54 0x0001, /* R1 - REVISION */
55 0x0000, /* R2 - PSCTRL1 */
56 0x1812, /* R3 - AIF_CTRL1 */
57 0x0000, /* R4 - AIF_CTRL2 */
58 0x0001, /* R5 - DAC_CTRL3 */
59 0x0190, /* R6 - DAC_GAINL */
60 0x0190, /* R7 - DAC_GAINR */
61 0x0000, /* R8 - ZERO_DETECT */
64 static int wm8523_volatile_register(unsigned int reg)
67 case WM8523_DEVICE_ID:
75 static int wm8523_reset(struct snd_soc_codec *codec)
77 return snd_soc_write(codec, WM8523_DEVICE_ID, 0);
80 static const DECLARE_TLV_DB_SCALE(dac_tlv, -10000, 25, 0);
82 static const char *wm8523_zd_count_text[] = {
87 static const struct soc_enum wm8523_zc_count =
88 SOC_ENUM_SINGLE(WM8523_ZERO_DETECT, 0, 2, wm8523_zd_count_text);
90 static const struct snd_kcontrol_new wm8523_snd_controls[] = {
91 SOC_DOUBLE_R_TLV("Playback Volume", WM8523_DAC_GAINL, WM8523_DAC_GAINR,
93 SOC_SINGLE("ZC Switch", WM8523_DAC_CTRL3, 4, 1, 0),
94 SOC_SINGLE("Playback Deemphasis Switch", WM8523_AIF_CTRL1, 8, 1, 0),
95 SOC_DOUBLE("Playback Switch", WM8523_DAC_CTRL3, 2, 3, 1, 1),
96 SOC_SINGLE("Volume Ramp Up Switch", WM8523_DAC_CTRL3, 1, 1, 0),
97 SOC_SINGLE("Volume Ramp Down Switch", WM8523_DAC_CTRL3, 0, 1, 0),
98 SOC_ENUM("Zero Detect Count", wm8523_zc_count),
101 static const struct snd_soc_dapm_widget wm8523_dapm_widgets[] = {
102 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
103 SND_SOC_DAPM_OUTPUT("LINEVOUTL"),
104 SND_SOC_DAPM_OUTPUT("LINEVOUTR"),
107 static const struct snd_soc_dapm_route intercon[] = {
108 { "LINEVOUTL", NULL, "DAC" },
109 { "LINEVOUTR", NULL, "DAC" },
112 static int wm8523_add_widgets(struct snd_soc_codec *codec)
114 snd_soc_dapm_new_controls(codec, wm8523_dapm_widgets,
115 ARRAY_SIZE(wm8523_dapm_widgets));
117 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
125 } lrclk_ratios[WM8523_NUM_RATES] = {
135 static int wm8523_startup(struct snd_pcm_substream *substream,
136 struct snd_soc_dai *dai)
138 struct snd_soc_codec *codec = dai->codec;
139 struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
141 /* The set of sample rates that can be supported depends on the
142 * MCLK supplied to the CODEC - enforce this.
144 if (!wm8523->sysclk) {
146 "No MCLK configured, call set_sysclk() on init\n");
151 snd_pcm_hw_constraint_list(substream->runtime, 0,
152 SNDRV_PCM_HW_PARAM_RATE,
153 &wm8523->rate_constraint);
158 static int wm8523_hw_params(struct snd_pcm_substream *substream,
159 struct snd_pcm_hw_params *params,
160 struct snd_soc_dai *dai)
162 struct snd_soc_pcm_runtime *rtd = substream->private_data;
163 struct snd_soc_codec *codec = rtd->codec;
164 struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
166 u16 aifctrl1 = snd_soc_read(codec, WM8523_AIF_CTRL1);
167 u16 aifctrl2 = snd_soc_read(codec, WM8523_AIF_CTRL2);
169 /* Find a supported LRCLK ratio */
170 for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
171 if (wm8523->sysclk / params_rate(params) ==
172 lrclk_ratios[i].ratio)
176 /* Should never happen, should be handled by constraints */
177 if (i == ARRAY_SIZE(lrclk_ratios)) {
178 dev_err(codec->dev, "MCLK/fs ratio %d unsupported\n",
179 wm8523->sysclk / params_rate(params));
183 aifctrl2 &= ~WM8523_SR_MASK;
184 aifctrl2 |= lrclk_ratios[i].value;
186 aifctrl1 &= ~WM8523_WL_MASK;
187 switch (params_format(params)) {
188 case SNDRV_PCM_FORMAT_S16_LE:
190 case SNDRV_PCM_FORMAT_S20_3LE:
193 case SNDRV_PCM_FORMAT_S24_LE:
196 case SNDRV_PCM_FORMAT_S32_LE:
201 snd_soc_write(codec, WM8523_AIF_CTRL1, aifctrl1);
202 snd_soc_write(codec, WM8523_AIF_CTRL2, aifctrl2);
207 static int wm8523_set_dai_sysclk(struct snd_soc_dai *codec_dai,
208 int clk_id, unsigned int freq, int dir)
210 struct snd_soc_codec *codec = codec_dai->codec;
211 struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
215 wm8523->sysclk = freq;
217 wm8523->rate_constraint.count = 0;
218 for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
219 val = freq / lrclk_ratios[i].ratio;
220 /* Check that it's a standard rate since core can't
221 * cope with others and having the odd rates confuses
222 * constraint matching.
237 dev_dbg(codec->dev, "Supported sample rate: %dHz\n",
239 wm8523->rate_constraint_list[i] = val;
240 wm8523->rate_constraint.count++;
243 dev_dbg(codec->dev, "Skipping sample rate: %dHz\n",
248 /* Need at least one supported rate... */
249 if (wm8523->rate_constraint.count == 0)
256 static int wm8523_set_dai_fmt(struct snd_soc_dai *codec_dai,
259 struct snd_soc_codec *codec = codec_dai->codec;
260 u16 aifctrl1 = snd_soc_read(codec, WM8523_AIF_CTRL1);
262 aifctrl1 &= ~(WM8523_BCLK_INV_MASK | WM8523_LRCLK_INV_MASK |
263 WM8523_FMT_MASK | WM8523_AIF_MSTR_MASK);
265 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
266 case SND_SOC_DAIFMT_CBM_CFM:
267 aifctrl1 |= WM8523_AIF_MSTR;
269 case SND_SOC_DAIFMT_CBS_CFS:
275 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
276 case SND_SOC_DAIFMT_I2S:
279 case SND_SOC_DAIFMT_RIGHT_J:
281 case SND_SOC_DAIFMT_LEFT_J:
284 case SND_SOC_DAIFMT_DSP_A:
287 case SND_SOC_DAIFMT_DSP_B:
294 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
295 case SND_SOC_DAIFMT_NB_NF:
297 case SND_SOC_DAIFMT_IB_IF:
298 aifctrl1 |= WM8523_BCLK_INV | WM8523_LRCLK_INV;
300 case SND_SOC_DAIFMT_IB_NF:
301 aifctrl1 |= WM8523_BCLK_INV;
303 case SND_SOC_DAIFMT_NB_IF:
304 aifctrl1 |= WM8523_LRCLK_INV;
310 snd_soc_write(codec, WM8523_AIF_CTRL1, aifctrl1);
315 static int wm8523_set_bias_level(struct snd_soc_codec *codec,
316 enum snd_soc_bias_level level)
318 struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
322 case SND_SOC_BIAS_ON:
325 case SND_SOC_BIAS_PREPARE:
327 snd_soc_update_bits(codec, WM8523_PSCTRL1,
328 WM8523_SYS_ENA_MASK, 3);
331 case SND_SOC_BIAS_STANDBY:
332 if (codec->bias_level == SND_SOC_BIAS_OFF) {
333 ret = regulator_bulk_enable(ARRAY_SIZE(wm8523->supplies),
337 "Failed to enable supplies: %d\n",
342 /* Initial power up */
343 snd_soc_update_bits(codec, WM8523_PSCTRL1,
344 WM8523_SYS_ENA_MASK, 1);
346 /* Sync back default/cached values */
347 for (i = WM8523_AIF_CTRL1;
348 i < WM8523_MAX_REGISTER; i++)
349 snd_soc_write(codec, i, wm8523->reg_cache[i]);
355 /* Power up to mute */
356 snd_soc_update_bits(codec, WM8523_PSCTRL1,
357 WM8523_SYS_ENA_MASK, 2);
361 case SND_SOC_BIAS_OFF:
362 /* The chip runs through the power down sequence for us. */
363 snd_soc_update_bits(codec, WM8523_PSCTRL1,
364 WM8523_SYS_ENA_MASK, 0);
367 regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies),
371 codec->bias_level = level;
375 #define WM8523_RATES SNDRV_PCM_RATE_8000_192000
377 #define WM8523_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
378 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
380 static struct snd_soc_dai_ops wm8523_dai_ops = {
381 .startup = wm8523_startup,
382 .hw_params = wm8523_hw_params,
383 .set_sysclk = wm8523_set_dai_sysclk,
384 .set_fmt = wm8523_set_dai_fmt,
387 static struct snd_soc_dai_driver wm8523_dai = {
388 .name = "wm8523-hifi",
390 .stream_name = "Playback",
391 .channels_min = 2, /* Mono modes not yet supported */
393 .rates = WM8523_RATES,
394 .formats = WM8523_FORMATS,
396 .ops = &wm8523_dai_ops,
400 static int wm8523_suspend(struct snd_soc_codec *codec, pm_message_t state)
402 wm8523_set_bias_level(codec, SND_SOC_BIAS_OFF);
406 static int wm8523_resume(struct snd_soc_codec *codec)
408 wm8523_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
412 #define wm8523_suspend NULL
413 #define wm8523_resume NULL
416 static int wm8523_probe(struct snd_soc_codec *codec)
418 struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
421 codec->hw_write = (hw_write_t)i2c_master_send;
422 codec->control_data = wm8523->control_data;
423 wm8523->rate_constraint.list = &wm8523->rate_constraint_list[0];
424 wm8523->rate_constraint.count =
425 ARRAY_SIZE(wm8523->rate_constraint_list);
427 ret = snd_soc_codec_set_cache_io(codec, 8, 16, wm8523->control_type);
429 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
433 for (i = 0; i < ARRAY_SIZE(wm8523->supplies); i++)
434 wm8523->supplies[i].supply = wm8523_supply_names[i];
436 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8523->supplies),
439 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
443 ret = regulator_bulk_enable(ARRAY_SIZE(wm8523->supplies),
446 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
450 ret = snd_soc_read(codec, WM8523_DEVICE_ID);
452 dev_err(codec->dev, "Failed to read ID register\n");
455 if (ret != wm8523_reg[WM8523_DEVICE_ID]) {
456 dev_err(codec->dev, "Device is not a WM8523, ID is %x\n", ret);
461 ret = snd_soc_read(codec, WM8523_REVISION);
463 dev_err(codec->dev, "Failed to read revision register\n");
466 dev_info(codec->dev, "revision %c\n",
467 (ret & WM8523_CHIP_REV_MASK) + 'A');
469 ret = wm8523_reset(codec);
471 dev_err(codec->dev, "Failed to issue reset\n");
475 /* Change some default settings - latch VU and enable ZC */
476 wm8523->reg_cache[WM8523_DAC_GAINR] |= WM8523_DACR_VU;
477 wm8523->reg_cache[WM8523_DAC_CTRL3] |= WM8523_ZC;
479 wm8523_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
481 /* Bias level configuration will have done an extra enable */
482 regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
484 snd_soc_add_controls(codec, wm8523_snd_controls,
485 ARRAY_SIZE(wm8523_snd_controls));
486 wm8523_add_widgets(codec);
491 regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
493 regulator_bulk_free(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
498 static int wm8523_remove(struct snd_soc_codec *codec)
500 struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
502 wm8523_set_bias_level(codec, SND_SOC_BIAS_OFF);
503 regulator_bulk_free(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
507 static struct snd_soc_codec_driver soc_codec_dev_wm8523 = {
508 .probe = wm8523_probe,
509 .remove = wm8523_remove,
510 .suspend = wm8523_suspend,
511 .resume = wm8523_resume,
512 .set_bias_level = wm8523_set_bias_level,
513 .reg_cache_size = WM8523_REGISTER_COUNT,
514 .reg_word_size = sizeof(u16),
515 .reg_cache_default = wm8523_reg,
516 .volatile_register = wm8523_volatile_register,
519 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
520 static __devinit int wm8523_i2c_probe(struct i2c_client *i2c,
521 const struct i2c_device_id *id)
523 struct wm8523_priv *wm8523;
526 wm8523 = kzalloc(sizeof(struct wm8523_priv), GFP_KERNEL);
530 i2c_set_clientdata(i2c, wm8523);
531 wm8523->control_data = i2c;
532 wm8523->control_type = SND_SOC_I2C;
534 ret = snd_soc_register_codec(&i2c->dev,
535 &soc_codec_dev_wm8523, &wm8523_dai, 1);
542 static __devexit int wm8523_i2c_remove(struct i2c_client *client)
544 snd_soc_unregister_codec(&client->dev);
545 kfree(i2c_get_clientdata(client));
549 static const struct i2c_device_id wm8523_i2c_id[] = {
553 MODULE_DEVICE_TABLE(i2c, wm8523_i2c_id);
555 static struct i2c_driver wm8523_i2c_driver = {
557 .name = "wm8523-codec",
558 .owner = THIS_MODULE,
560 .probe = wm8523_i2c_probe,
561 .remove = __devexit_p(wm8523_i2c_remove),
562 .id_table = wm8523_i2c_id,
566 static int __init wm8523_modinit(void)
569 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
570 ret = i2c_add_driver(&wm8523_i2c_driver);
572 printk(KERN_ERR "Failed to register WM8523 I2C driver: %d\n",
578 module_init(wm8523_modinit);
580 static void __exit wm8523_exit(void)
582 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
583 i2c_del_driver(&wm8523_i2c_driver);
586 module_exit(wm8523_exit);
588 MODULE_DESCRIPTION("ASoC WM8523 driver");
589 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
590 MODULE_LICENSE("GPL");