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 <linux/of_device.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.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;
44 struct regulator_bulk_data supplies[WM8523_NUM_SUPPLIES];
46 unsigned int rate_constraint_list[WM8523_NUM_RATES];
47 struct snd_pcm_hw_constraint_list rate_constraint;
50 static const u16 wm8523_reg[WM8523_REGISTER_COUNT] = {
51 0x8523, /* R0 - DEVICE_ID */
52 0x0001, /* R1 - REVISION */
53 0x0000, /* R2 - PSCTRL1 */
54 0x1812, /* R3 - AIF_CTRL1 */
55 0x0000, /* R4 - AIF_CTRL2 */
56 0x0001, /* R5 - DAC_CTRL3 */
57 0x0190, /* R6 - DAC_GAINL */
58 0x0190, /* R7 - DAC_GAINR */
59 0x0000, /* R8 - ZERO_DETECT */
62 static int wm8523_volatile_register(struct snd_soc_codec *codec, unsigned int reg)
65 case WM8523_DEVICE_ID:
73 static int wm8523_reset(struct snd_soc_codec *codec)
75 return snd_soc_write(codec, WM8523_DEVICE_ID, 0);
78 static const DECLARE_TLV_DB_SCALE(dac_tlv, -10000, 25, 0);
80 static const char *wm8523_zd_count_text[] = {
85 static const struct soc_enum wm8523_zc_count =
86 SOC_ENUM_SINGLE(WM8523_ZERO_DETECT, 0, 2, wm8523_zd_count_text);
88 static const struct snd_kcontrol_new wm8523_controls[] = {
89 SOC_DOUBLE_R_TLV("Playback Volume", WM8523_DAC_GAINL, WM8523_DAC_GAINR,
91 SOC_SINGLE("ZC Switch", WM8523_DAC_CTRL3, 4, 1, 0),
92 SOC_SINGLE("Playback Deemphasis Switch", WM8523_AIF_CTRL1, 8, 1, 0),
93 SOC_DOUBLE("Playback Switch", WM8523_DAC_CTRL3, 2, 3, 1, 1),
94 SOC_SINGLE("Volume Ramp Up Switch", WM8523_DAC_CTRL3, 1, 1, 0),
95 SOC_SINGLE("Volume Ramp Down Switch", WM8523_DAC_CTRL3, 0, 1, 0),
96 SOC_ENUM("Zero Detect Count", wm8523_zc_count),
99 static const struct snd_soc_dapm_widget wm8523_dapm_widgets[] = {
100 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
101 SND_SOC_DAPM_OUTPUT("LINEVOUTL"),
102 SND_SOC_DAPM_OUTPUT("LINEVOUTR"),
105 static const struct snd_soc_dapm_route wm8523_dapm_routes[] = {
106 { "LINEVOUTL", NULL, "DAC" },
107 { "LINEVOUTR", NULL, "DAC" },
113 } lrclk_ratios[WM8523_NUM_RATES] = {
123 static int wm8523_startup(struct snd_pcm_substream *substream,
124 struct snd_soc_dai *dai)
126 struct snd_soc_codec *codec = dai->codec;
127 struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
129 /* The set of sample rates that can be supported depends on the
130 * MCLK supplied to the CODEC - enforce this.
132 if (!wm8523->sysclk) {
134 "No MCLK configured, call set_sysclk() on init\n");
138 snd_pcm_hw_constraint_list(substream->runtime, 0,
139 SNDRV_PCM_HW_PARAM_RATE,
140 &wm8523->rate_constraint);
145 static int wm8523_hw_params(struct snd_pcm_substream *substream,
146 struct snd_pcm_hw_params *params,
147 struct snd_soc_dai *dai)
149 struct snd_soc_pcm_runtime *rtd = substream->private_data;
150 struct snd_soc_codec *codec = rtd->codec;
151 struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
153 u16 aifctrl1 = snd_soc_read(codec, WM8523_AIF_CTRL1);
154 u16 aifctrl2 = snd_soc_read(codec, WM8523_AIF_CTRL2);
156 /* Find a supported LRCLK ratio */
157 for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
158 if (wm8523->sysclk / params_rate(params) ==
159 lrclk_ratios[i].ratio)
163 /* Should never happen, should be handled by constraints */
164 if (i == ARRAY_SIZE(lrclk_ratios)) {
165 dev_err(codec->dev, "MCLK/fs ratio %d unsupported\n",
166 wm8523->sysclk / params_rate(params));
170 aifctrl2 &= ~WM8523_SR_MASK;
171 aifctrl2 |= lrclk_ratios[i].value;
173 aifctrl1 &= ~WM8523_WL_MASK;
174 switch (params_format(params)) {
175 case SNDRV_PCM_FORMAT_S16_LE:
177 case SNDRV_PCM_FORMAT_S20_3LE:
180 case SNDRV_PCM_FORMAT_S24_LE:
183 case SNDRV_PCM_FORMAT_S32_LE:
188 snd_soc_write(codec, WM8523_AIF_CTRL1, aifctrl1);
189 snd_soc_write(codec, WM8523_AIF_CTRL2, aifctrl2);
194 static int wm8523_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 wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
202 wm8523->sysclk = freq;
204 wm8523->rate_constraint.count = 0;
205 for (i = 0; i < ARRAY_SIZE(lrclk_ratios); i++) {
206 val = freq / lrclk_ratios[i].ratio;
207 /* Check that it's a standard rate since core can't
208 * cope with others and having the odd rates confuses
209 * constraint matching.
224 dev_dbg(codec->dev, "Supported sample rate: %dHz\n",
226 wm8523->rate_constraint_list[i] = val;
227 wm8523->rate_constraint.count++;
230 dev_dbg(codec->dev, "Skipping sample rate: %dHz\n",
235 /* Need at least one supported rate... */
236 if (wm8523->rate_constraint.count == 0)
243 static int wm8523_set_dai_fmt(struct snd_soc_dai *codec_dai,
246 struct snd_soc_codec *codec = codec_dai->codec;
247 u16 aifctrl1 = snd_soc_read(codec, WM8523_AIF_CTRL1);
249 aifctrl1 &= ~(WM8523_BCLK_INV_MASK | WM8523_LRCLK_INV_MASK |
250 WM8523_FMT_MASK | WM8523_AIF_MSTR_MASK);
252 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
253 case SND_SOC_DAIFMT_CBM_CFM:
254 aifctrl1 |= WM8523_AIF_MSTR;
256 case SND_SOC_DAIFMT_CBS_CFS:
262 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
263 case SND_SOC_DAIFMT_I2S:
266 case SND_SOC_DAIFMT_RIGHT_J:
268 case SND_SOC_DAIFMT_LEFT_J:
271 case SND_SOC_DAIFMT_DSP_A:
274 case SND_SOC_DAIFMT_DSP_B:
281 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
282 case SND_SOC_DAIFMT_NB_NF:
284 case SND_SOC_DAIFMT_IB_IF:
285 aifctrl1 |= WM8523_BCLK_INV | WM8523_LRCLK_INV;
287 case SND_SOC_DAIFMT_IB_NF:
288 aifctrl1 |= WM8523_BCLK_INV;
290 case SND_SOC_DAIFMT_NB_IF:
291 aifctrl1 |= WM8523_LRCLK_INV;
297 snd_soc_write(codec, WM8523_AIF_CTRL1, aifctrl1);
302 static int wm8523_set_bias_level(struct snd_soc_codec *codec,
303 enum snd_soc_bias_level level)
305 struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
306 u16 *reg_cache = codec->reg_cache;
310 case SND_SOC_BIAS_ON:
313 case SND_SOC_BIAS_PREPARE:
315 snd_soc_update_bits(codec, WM8523_PSCTRL1,
316 WM8523_SYS_ENA_MASK, 3);
319 case SND_SOC_BIAS_STANDBY:
320 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
321 ret = regulator_bulk_enable(ARRAY_SIZE(wm8523->supplies),
325 "Failed to enable supplies: %d\n",
330 /* Initial power up */
331 snd_soc_update_bits(codec, WM8523_PSCTRL1,
332 WM8523_SYS_ENA_MASK, 1);
334 /* Sync back default/cached values */
335 for (i = WM8523_AIF_CTRL1;
336 i < WM8523_MAX_REGISTER; i++)
337 snd_soc_write(codec, i, reg_cache[i]);
343 /* Power up to mute */
344 snd_soc_update_bits(codec, WM8523_PSCTRL1,
345 WM8523_SYS_ENA_MASK, 2);
349 case SND_SOC_BIAS_OFF:
350 /* The chip runs through the power down sequence for us. */
351 snd_soc_update_bits(codec, WM8523_PSCTRL1,
352 WM8523_SYS_ENA_MASK, 0);
355 regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies),
359 codec->dapm.bias_level = level;
363 #define WM8523_RATES SNDRV_PCM_RATE_8000_192000
365 #define WM8523_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
366 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
368 static struct snd_soc_dai_ops wm8523_dai_ops = {
369 .startup = wm8523_startup,
370 .hw_params = wm8523_hw_params,
371 .set_sysclk = wm8523_set_dai_sysclk,
372 .set_fmt = wm8523_set_dai_fmt,
375 static struct snd_soc_dai_driver wm8523_dai = {
376 .name = "wm8523-hifi",
378 .stream_name = "Playback",
379 .channels_min = 2, /* Mono modes not yet supported */
381 .rates = WM8523_RATES,
382 .formats = WM8523_FORMATS,
384 .ops = &wm8523_dai_ops,
388 static int wm8523_suspend(struct snd_soc_codec *codec, pm_message_t state)
390 wm8523_set_bias_level(codec, SND_SOC_BIAS_OFF);
394 static int wm8523_resume(struct snd_soc_codec *codec)
396 wm8523_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
400 #define wm8523_suspend NULL
401 #define wm8523_resume NULL
404 static int wm8523_probe(struct snd_soc_codec *codec)
406 struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
409 wm8523->rate_constraint.list = &wm8523->rate_constraint_list[0];
410 wm8523->rate_constraint.count =
411 ARRAY_SIZE(wm8523->rate_constraint_list);
413 ret = snd_soc_codec_set_cache_io(codec, 8, 16, wm8523->control_type);
415 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
419 for (i = 0; i < ARRAY_SIZE(wm8523->supplies); i++)
420 wm8523->supplies[i].supply = wm8523_supply_names[i];
422 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8523->supplies),
425 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
429 ret = regulator_bulk_enable(ARRAY_SIZE(wm8523->supplies),
432 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
436 ret = snd_soc_read(codec, WM8523_DEVICE_ID);
438 dev_err(codec->dev, "Failed to read ID register\n");
441 if (ret != wm8523_reg[WM8523_DEVICE_ID]) {
442 dev_err(codec->dev, "Device is not a WM8523, ID is %x\n", ret);
447 ret = snd_soc_read(codec, WM8523_REVISION);
449 dev_err(codec->dev, "Failed to read revision register\n");
452 dev_info(codec->dev, "revision %c\n",
453 (ret & WM8523_CHIP_REV_MASK) + 'A');
455 ret = wm8523_reset(codec);
457 dev_err(codec->dev, "Failed to issue reset\n");
461 /* Change some default settings - latch VU and enable ZC */
462 snd_soc_update_bits(codec, WM8523_DAC_GAINR,
463 WM8523_DACR_VU, WM8523_DACR_VU);
464 snd_soc_update_bits(codec, WM8523_DAC_CTRL3, WM8523_ZC, WM8523_ZC);
466 wm8523_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
468 /* Bias level configuration will have done an extra enable */
469 regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
474 regulator_bulk_disable(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
476 regulator_bulk_free(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
481 static int wm8523_remove(struct snd_soc_codec *codec)
483 struct wm8523_priv *wm8523 = snd_soc_codec_get_drvdata(codec);
485 wm8523_set_bias_level(codec, SND_SOC_BIAS_OFF);
486 regulator_bulk_free(ARRAY_SIZE(wm8523->supplies), wm8523->supplies);
490 static struct snd_soc_codec_driver soc_codec_dev_wm8523 = {
491 .probe = wm8523_probe,
492 .remove = wm8523_remove,
493 .suspend = wm8523_suspend,
494 .resume = wm8523_resume,
495 .set_bias_level = wm8523_set_bias_level,
496 .reg_cache_size = WM8523_REGISTER_COUNT,
497 .reg_word_size = sizeof(u16),
498 .reg_cache_default = wm8523_reg,
499 .volatile_register = wm8523_volatile_register,
501 .controls = wm8523_controls,
502 .num_controls = ARRAY_SIZE(wm8523_controls),
503 .dapm_widgets = wm8523_dapm_widgets,
504 .num_dapm_widgets = ARRAY_SIZE(wm8523_dapm_widgets),
505 .dapm_routes = wm8523_dapm_routes,
506 .num_dapm_routes = ARRAY_SIZE(wm8523_dapm_routes),
509 static const struct of_device_id wm8523_of_match[] = {
510 { .compatible = "wlf,wm8523" },
514 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
515 static __devinit int wm8523_i2c_probe(struct i2c_client *i2c,
516 const struct i2c_device_id *id)
518 struct wm8523_priv *wm8523;
521 wm8523 = kzalloc(sizeof(struct wm8523_priv), GFP_KERNEL);
525 i2c_set_clientdata(i2c, wm8523);
526 wm8523->control_type = SND_SOC_I2C;
528 ret = snd_soc_register_codec(&i2c->dev,
529 &soc_codec_dev_wm8523, &wm8523_dai, 1);
536 static __devexit int wm8523_i2c_remove(struct i2c_client *client)
538 snd_soc_unregister_codec(&client->dev);
539 kfree(i2c_get_clientdata(client));
543 static const struct i2c_device_id wm8523_i2c_id[] = {
547 MODULE_DEVICE_TABLE(i2c, wm8523_i2c_id);
549 static struct i2c_driver wm8523_i2c_driver = {
552 .owner = THIS_MODULE,
553 .of_match_table = wm8523_of_match,
555 .probe = wm8523_i2c_probe,
556 .remove = __devexit_p(wm8523_i2c_remove),
557 .id_table = wm8523_i2c_id,
561 static int __init wm8523_modinit(void)
564 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
565 ret = i2c_add_driver(&wm8523_i2c_driver);
567 printk(KERN_ERR "Failed to register WM8523 I2C driver: %d\n",
573 module_init(wm8523_modinit);
575 static void __exit wm8523_exit(void)
577 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
578 i2c_del_driver(&wm8523_i2c_driver);
581 module_exit(wm8523_exit);
583 MODULE_DESCRIPTION("ASoC WM8523 driver");
584 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
585 MODULE_LICENSE("GPL");