2 * wm8731.c -- WM8731 ALSA SoC Audio driver
4 * Copyright 2005 Openedhand Ltd.
6 * Author: Richard Purdie <richard@openedhand.com>
8 * Based on wm8753.c by Liam Girdwood
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
20 #include <linux/i2c.h>
21 #include <linux/slab.h>
22 #include <linux/platform_device.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/spi/spi.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/soc-dapm.h>
30 #include <sound/initval.h>
31 #include <sound/tlv.h>
35 #define WM8731_NUM_SUPPLIES 4
36 static const char *wm8731_supply_names[WM8731_NUM_SUPPLIES] = {
43 /* codec private data */
45 enum snd_soc_control_type control_type;
47 struct regulator_bulk_data supplies[WM8731_NUM_SUPPLIES];
48 u16 reg_cache[WM8731_CACHEREGNUM];
54 * wm8731 register cache
55 * We can't read the WM8731 register space when we are
56 * using 2 wire for device control, so we cache them instead.
57 * There is no point in caching the reset register
59 static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
60 0x0097, 0x0097, 0x0079, 0x0079,
61 0x000a, 0x0008, 0x009f, 0x000a,
65 #define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0)
67 static const char *wm8731_input_select[] = {"Line In", "Mic"};
68 static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
70 static const struct soc_enum wm8731_enum[] = {
71 SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
72 SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
75 static const DECLARE_TLV_DB_SCALE(in_tlv, -3450, 150, 0);
76 static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -1500, 300, 0);
77 static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
79 static const struct snd_kcontrol_new wm8731_snd_controls[] = {
81 SOC_DOUBLE_R_TLV("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
83 SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
86 SOC_DOUBLE_R_TLV("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0,
88 SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
90 SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
91 SOC_SINGLE("Mic Capture Switch", WM8731_APANA, 1, 1, 1),
93 SOC_SINGLE_TLV("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1,
96 SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
97 SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
99 SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
103 static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
104 SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
105 SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
106 SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
110 static const struct snd_kcontrol_new wm8731_input_mux_controls =
111 SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
113 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
114 SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
115 &wm8731_output_mixer_controls[0],
116 ARRAY_SIZE(wm8731_output_mixer_controls)),
117 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
118 SND_SOC_DAPM_OUTPUT("LOUT"),
119 SND_SOC_DAPM_OUTPUT("LHPOUT"),
120 SND_SOC_DAPM_OUTPUT("ROUT"),
121 SND_SOC_DAPM_OUTPUT("RHPOUT"),
122 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
123 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
124 SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
125 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
126 SND_SOC_DAPM_INPUT("MICIN"),
127 SND_SOC_DAPM_INPUT("RLINEIN"),
128 SND_SOC_DAPM_INPUT("LLINEIN"),
131 static const struct snd_soc_dapm_route intercon[] = {
133 {"Output Mixer", "Line Bypass Switch", "Line Input"},
134 {"Output Mixer", "HiFi Playback Switch", "DAC"},
135 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
138 {"RHPOUT", NULL, "Output Mixer"},
139 {"ROUT", NULL, "Output Mixer"},
140 {"LHPOUT", NULL, "Output Mixer"},
141 {"LOUT", NULL, "Output Mixer"},
144 {"Input Mux", "Line In", "Line Input"},
145 {"Input Mux", "Mic", "Mic Bias"},
146 {"ADC", NULL, "Input Mux"},
149 {"Line Input", NULL, "LLINEIN"},
150 {"Line Input", NULL, "RLINEIN"},
151 {"Mic Bias", NULL, "MICIN"},
154 static int wm8731_add_widgets(struct snd_soc_codec *codec)
156 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
157 ARRAY_SIZE(wm8731_dapm_widgets));
159 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
173 /* codec mclk clock divider coefficients */
174 static const struct _coeff_div coeff_div[] = {
176 {12288000, 48000, 256, 0x0, 0x0, 0x0},
177 {18432000, 48000, 384, 0x0, 0x1, 0x0},
178 {12000000, 48000, 250, 0x0, 0x0, 0x1},
181 {12288000, 32000, 384, 0x6, 0x0, 0x0},
182 {18432000, 32000, 576, 0x6, 0x1, 0x0},
183 {12000000, 32000, 375, 0x6, 0x0, 0x1},
186 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
187 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
188 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
189 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
190 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
193 {12288000, 96000, 128, 0x7, 0x0, 0x0},
194 {18432000, 96000, 192, 0x7, 0x1, 0x0},
195 {12000000, 96000, 125, 0x7, 0x0, 0x1},
198 {11289600, 44100, 256, 0x8, 0x0, 0x0},
199 {16934400, 44100, 384, 0x8, 0x1, 0x0},
200 {12000000, 44100, 272, 0x8, 0x1, 0x1},
203 {11289600, 88200, 128, 0xf, 0x0, 0x0},
204 {16934400, 88200, 192, 0xf, 0x1, 0x0},
205 {12000000, 88200, 136, 0xf, 0x1, 0x1},
208 static inline int get_coeff(int mclk, int rate)
212 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
213 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
219 static int wm8731_hw_params(struct snd_pcm_substream *substream,
220 struct snd_pcm_hw_params *params,
221 struct snd_soc_dai *dai)
223 struct snd_soc_codec *codec = dai->codec;
224 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
225 u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3;
226 int i = get_coeff(wm8731->sysclk, params_rate(params));
227 u16 srate = (coeff_div[i].sr << 2) |
228 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
230 snd_soc_write(codec, WM8731_SRATE, srate);
233 switch (params_format(params)) {
234 case SNDRV_PCM_FORMAT_S16_LE:
236 case SNDRV_PCM_FORMAT_S20_3LE:
239 case SNDRV_PCM_FORMAT_S24_LE:
244 snd_soc_write(codec, WM8731_IFACE, iface);
248 static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
249 struct snd_soc_dai *dai)
251 struct snd_soc_codec *codec = dai->codec;
254 snd_soc_write(codec, WM8731_ACTIVE, 0x0001);
259 static void wm8731_shutdown(struct snd_pcm_substream *substream,
260 struct snd_soc_dai *dai)
262 struct snd_soc_codec *codec = dai->codec;
265 if (!codec->active) {
267 snd_soc_write(codec, WM8731_ACTIVE, 0x0);
271 static int wm8731_mute(struct snd_soc_dai *dai, int mute)
273 struct snd_soc_codec *codec = dai->codec;
274 u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7;
277 snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8);
279 snd_soc_write(codec, WM8731_APDIGI, mute_reg);
283 static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
284 int clk_id, unsigned int freq, int dir)
286 struct snd_soc_codec *codec = codec_dai->codec;
287 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
295 wm8731->sysclk = freq;
302 static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
305 struct snd_soc_codec *codec = codec_dai->codec;
308 /* set master/slave audio interface */
309 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
310 case SND_SOC_DAIFMT_CBM_CFM:
313 case SND_SOC_DAIFMT_CBS_CFS:
319 /* interface format */
320 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
321 case SND_SOC_DAIFMT_I2S:
324 case SND_SOC_DAIFMT_RIGHT_J:
326 case SND_SOC_DAIFMT_LEFT_J:
329 case SND_SOC_DAIFMT_DSP_A:
332 case SND_SOC_DAIFMT_DSP_B:
339 /* clock inversion */
340 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
341 case SND_SOC_DAIFMT_NB_NF:
343 case SND_SOC_DAIFMT_IB_IF:
346 case SND_SOC_DAIFMT_IB_NF:
349 case SND_SOC_DAIFMT_NB_IF:
357 snd_soc_write(codec, WM8731_IFACE, iface);
361 static int wm8731_set_bias_level(struct snd_soc_codec *codec,
362 enum snd_soc_bias_level level)
364 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
367 u16 *cache = codec->reg_cache;
371 case SND_SOC_BIAS_ON:
373 case SND_SOC_BIAS_PREPARE:
375 case SND_SOC_BIAS_STANDBY:
376 if (codec->bias_level == SND_SOC_BIAS_OFF) {
377 ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
382 /* Sync reg_cache with the hardware */
383 for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
384 if (cache[i] == wm8731_reg[i])
387 data[0] = (i << 1) | ((cache[i] >> 8)
389 data[1] = cache[i] & 0x00ff;
390 codec->hw_write(codec->control_data, data, 2);
394 /* Clear PWROFF, gate CLKOUT, everything else as-is */
395 reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f;
396 snd_soc_write(codec, WM8731_PWR, reg | 0x0040);
398 case SND_SOC_BIAS_OFF:
399 snd_soc_write(codec, WM8731_ACTIVE, 0x0);
400 snd_soc_write(codec, WM8731_PWR, 0xffff);
401 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies),
405 codec->bias_level = level;
409 #define WM8731_RATES SNDRV_PCM_RATE_8000_96000
411 #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
412 SNDRV_PCM_FMTBIT_S24_LE)
414 static struct snd_soc_dai_ops wm8731_dai_ops = {
415 .prepare = wm8731_pcm_prepare,
416 .hw_params = wm8731_hw_params,
417 .shutdown = wm8731_shutdown,
418 .digital_mute = wm8731_mute,
419 .set_sysclk = wm8731_set_dai_sysclk,
420 .set_fmt = wm8731_set_dai_fmt,
423 static struct snd_soc_dai_driver wm8731_dai = {
424 .name = "wm8731-hifi",
426 .stream_name = "Playback",
429 .rates = WM8731_RATES,
430 .formats = WM8731_FORMATS,},
432 .stream_name = "Capture",
435 .rates = WM8731_RATES,
436 .formats = WM8731_FORMATS,},
437 .ops = &wm8731_dai_ops,
438 .symmetric_rates = 1,
442 static int wm8731_suspend(struct snd_soc_codec *codec, pm_message_t state)
444 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
449 static int wm8731_resume(struct snd_soc_codec *codec)
451 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
456 #define wm8731_suspend NULL
457 #define wm8731_resume NULL
460 static int wm8731_probe(struct snd_soc_codec *codec)
462 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
465 codec->bias_level = SND_SOC_BIAS_OFF,
466 codec->control_data = wm8731->control_data;
468 ret = snd_soc_codec_set_cache_io(codec, 7, 9, wm8731->control_type);
470 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
474 for (i = 0; i < ARRAY_SIZE(wm8731->supplies); i++)
475 wm8731->supplies[i].supply = wm8731_supply_names[i];
477 ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8731->supplies),
480 dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
484 ret = regulator_bulk_enable(ARRAY_SIZE(wm8731->supplies),
487 dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
488 goto err_regulator_get;
491 ret = wm8731_reset(codec);
493 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
494 goto err_regulator_enable;
497 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
499 /* Latch the update bits */
500 snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0);
501 snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0);
502 snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0);
503 snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0);
505 /* Disable bypass path by default */
506 snd_soc_update_bits(codec, WM8731_APANA, 0x4, 0);
508 snd_soc_add_controls(codec, wm8731_snd_controls,
509 ARRAY_SIZE(wm8731_snd_controls));
510 wm8731_add_widgets(codec);
512 /* Regulators will have been enabled by bias management */
513 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
517 err_regulator_enable:
518 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
520 regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
526 /* power down chip */
527 static int wm8731_remove(struct snd_soc_codec *codec)
529 struct wm8731_priv *wm8731 = snd_soc_codec_get_drvdata(codec);
531 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
533 regulator_bulk_disable(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
534 regulator_bulk_free(ARRAY_SIZE(wm8731->supplies), wm8731->supplies);
539 static struct snd_soc_codec_driver soc_codec_dev_wm8731 = {
540 .probe = wm8731_probe,
541 .remove = wm8731_remove,
542 .suspend = wm8731_suspend,
543 .resume = wm8731_resume,
544 .set_bias_level = wm8731_set_bias_level,
545 .reg_cache_size = sizeof(wm8731_reg),
546 .reg_word_size = sizeof(u16),
547 .reg_cache_default = wm8731_reg,
550 #if defined(CONFIG_SPI_MASTER)
551 static int __devinit wm8731_spi_probe(struct spi_device *spi)
553 struct wm8731_priv *wm8731;
556 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
560 wm8731->control_data = spi;
561 wm8731->control_type = SND_SOC_SPI;
562 spi_set_drvdata(spi, wm8731);
564 ret = snd_soc_register_codec(&spi->dev,
565 &soc_codec_dev_wm8731, &wm8731_dai, 1);
571 static int __devexit wm8731_spi_remove(struct spi_device *spi)
573 snd_soc_unregister_codec(&spi->dev);
574 kfree(spi_get_drvdata(spi));
578 static struct spi_driver wm8731_spi_driver = {
580 .name = "wm8731-codec",
581 .bus = &spi_bus_type,
582 .owner = THIS_MODULE,
584 .probe = wm8731_spi_probe,
585 .remove = __devexit_p(wm8731_spi_remove),
587 #endif /* CONFIG_SPI_MASTER */
589 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
590 static __devinit int wm8731_i2c_probe(struct i2c_client *i2c,
591 const struct i2c_device_id *id)
593 struct wm8731_priv *wm8731;
596 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
600 i2c_set_clientdata(i2c, wm8731);
601 wm8731->control_data = i2c;
602 wm8731->control_type = SND_SOC_I2C;
604 ret = snd_soc_register_codec(&i2c->dev,
605 &soc_codec_dev_wm8731, &wm8731_dai, 1);
611 static __devexit int wm8731_i2c_remove(struct i2c_client *client)
613 snd_soc_unregister_codec(&client->dev);
614 kfree(i2c_get_clientdata(client));
618 static const struct i2c_device_id wm8731_i2c_id[] = {
622 MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
624 static struct i2c_driver wm8731_i2c_driver = {
626 .name = "wm8731-codec",
627 .owner = THIS_MODULE,
629 .probe = wm8731_i2c_probe,
630 .remove = __devexit_p(wm8731_i2c_remove),
631 .id_table = wm8731_i2c_id,
635 static int __init wm8731_modinit(void)
638 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
639 ret = i2c_add_driver(&wm8731_i2c_driver);
641 printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
645 #if defined(CONFIG_SPI_MASTER)
646 ret = spi_register_driver(&wm8731_spi_driver);
648 printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
654 module_init(wm8731_modinit);
656 static void __exit wm8731_exit(void)
658 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
659 i2c_del_driver(&wm8731_i2c_driver);
661 #if defined(CONFIG_SPI_MASTER)
662 spi_unregister_driver(&wm8731_spi_driver);
665 module_exit(wm8731_exit);
667 MODULE_DESCRIPTION("ASoC WM8731 driver");
668 MODULE_AUTHOR("Richard Purdie");
669 MODULE_LICENSE("GPL");