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/platform_device.h>
22 #include <linux/spi/spi.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>
32 struct snd_soc_codec_device soc_codec_dev_wm8731;
34 /* codec private data */
39 #ifdef CONFIG_SPI_MASTER
40 static int wm8731_spi_write(struct spi_device *spi, const char *data, int len);
41 static struct spi_driver wm8731_spi_driver;
45 * wm8731 register cache
46 * We can't read the WM8731 register space when we are
47 * using 2 wire for device control, so we cache them instead.
48 * There is no point in caching the reset register
50 static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
51 0x0097, 0x0097, 0x0079, 0x0079,
52 0x000a, 0x0008, 0x009f, 0x000a,
57 * read wm8731 register cache
59 static inline unsigned int wm8731_read_reg_cache(struct snd_soc_codec *codec,
62 u16 *cache = codec->reg_cache;
63 if (reg == WM8731_RESET)
65 if (reg >= WM8731_CACHEREGNUM)
71 * write wm8731 register cache
73 static inline void wm8731_write_reg_cache(struct snd_soc_codec *codec,
74 u16 reg, unsigned int value)
76 u16 *cache = codec->reg_cache;
77 if (reg >= WM8731_CACHEREGNUM)
83 * write to the WM8731 register space
85 static int wm8731_write(struct snd_soc_codec *codec, unsigned int reg,
91 * D15..D9 WM8731 register offset
92 * D8...D0 register data
94 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
95 data[1] = value & 0x00ff;
97 wm8731_write_reg_cache(codec, reg, value);
98 if (codec->hw_write(codec->control_data, data, 2) == 2)
104 #define wm8731_reset(c) wm8731_write(c, WM8731_RESET, 0)
106 static const char *wm8731_input_select[] = {"Line In", "Mic"};
107 static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
109 static const struct soc_enum wm8731_enum[] = {
110 SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
111 SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
114 static const struct snd_kcontrol_new wm8731_snd_controls[] = {
116 SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
118 SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
121 SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0),
122 SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
124 SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
125 SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1),
127 SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1),
129 SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
130 SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
132 SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
136 static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
137 SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
138 SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
139 SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
143 static const struct snd_kcontrol_new wm8731_input_mux_controls =
144 SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
146 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
147 SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
148 &wm8731_output_mixer_controls[0],
149 ARRAY_SIZE(wm8731_output_mixer_controls)),
150 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
151 SND_SOC_DAPM_OUTPUT("LOUT"),
152 SND_SOC_DAPM_OUTPUT("LHPOUT"),
153 SND_SOC_DAPM_OUTPUT("ROUT"),
154 SND_SOC_DAPM_OUTPUT("RHPOUT"),
155 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
156 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
157 SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
158 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
159 SND_SOC_DAPM_INPUT("MICIN"),
160 SND_SOC_DAPM_INPUT("RLINEIN"),
161 SND_SOC_DAPM_INPUT("LLINEIN"),
164 static const struct snd_soc_dapm_route intercon[] = {
166 {"Output Mixer", "Line Bypass Switch", "Line Input"},
167 {"Output Mixer", "HiFi Playback Switch", "DAC"},
168 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
171 {"RHPOUT", NULL, "Output Mixer"},
172 {"ROUT", NULL, "Output Mixer"},
173 {"LHPOUT", NULL, "Output Mixer"},
174 {"LOUT", NULL, "Output Mixer"},
177 {"Input Mux", "Line In", "Line Input"},
178 {"Input Mux", "Mic", "Mic Bias"},
179 {"ADC", NULL, "Input Mux"},
182 {"Line Input", NULL, "LLINEIN"},
183 {"Line Input", NULL, "RLINEIN"},
184 {"Mic Bias", NULL, "MICIN"},
187 static int wm8731_add_widgets(struct snd_soc_codec *codec)
189 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
190 ARRAY_SIZE(wm8731_dapm_widgets));
192 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
194 snd_soc_dapm_new_widgets(codec);
207 /* codec mclk clock divider coefficients */
208 static const struct _coeff_div coeff_div[] = {
210 {12288000, 48000, 256, 0x0, 0x0, 0x0},
211 {18432000, 48000, 384, 0x0, 0x1, 0x0},
212 {12000000, 48000, 250, 0x0, 0x0, 0x1},
215 {12288000, 32000, 384, 0x6, 0x0, 0x0},
216 {18432000, 32000, 576, 0x6, 0x1, 0x0},
217 {12000000, 32000, 375, 0x6, 0x0, 0x1},
220 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
221 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
222 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
223 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
224 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
227 {12288000, 96000, 128, 0x7, 0x0, 0x0},
228 {18432000, 96000, 192, 0x7, 0x1, 0x0},
229 {12000000, 96000, 125, 0x7, 0x0, 0x1},
232 {11289600, 44100, 256, 0x8, 0x0, 0x0},
233 {16934400, 44100, 384, 0x8, 0x1, 0x0},
234 {12000000, 44100, 272, 0x8, 0x1, 0x1},
237 {11289600, 88200, 128, 0xf, 0x0, 0x0},
238 {16934400, 88200, 192, 0xf, 0x1, 0x0},
239 {12000000, 88200, 136, 0xf, 0x1, 0x1},
242 static inline int get_coeff(int mclk, int rate)
246 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
247 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
253 static int wm8731_hw_params(struct snd_pcm_substream *substream,
254 struct snd_pcm_hw_params *params,
255 struct snd_soc_dai *dai)
257 struct snd_soc_pcm_runtime *rtd = substream->private_data;
258 struct snd_soc_device *socdev = rtd->socdev;
259 struct snd_soc_codec *codec = socdev->card->codec;
260 struct wm8731_priv *wm8731 = codec->private_data;
261 u16 iface = wm8731_read_reg_cache(codec, WM8731_IFACE) & 0xfff3;
262 int i = get_coeff(wm8731->sysclk, params_rate(params));
263 u16 srate = (coeff_div[i].sr << 2) |
264 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
266 wm8731_write(codec, WM8731_SRATE, srate);
269 switch (params_format(params)) {
270 case SNDRV_PCM_FORMAT_S16_LE:
272 case SNDRV_PCM_FORMAT_S20_3LE:
275 case SNDRV_PCM_FORMAT_S24_LE:
280 wm8731_write(codec, WM8731_IFACE, iface);
284 static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
285 struct snd_soc_dai *dai)
287 struct snd_soc_pcm_runtime *rtd = substream->private_data;
288 struct snd_soc_device *socdev = rtd->socdev;
289 struct snd_soc_codec *codec = socdev->card->codec;
292 wm8731_write(codec, WM8731_ACTIVE, 0x0001);
297 static void wm8731_shutdown(struct snd_pcm_substream *substream,
298 struct snd_soc_dai *dai)
300 struct snd_soc_pcm_runtime *rtd = substream->private_data;
301 struct snd_soc_device *socdev = rtd->socdev;
302 struct snd_soc_codec *codec = socdev->card->codec;
305 if (!codec->active) {
307 wm8731_write(codec, WM8731_ACTIVE, 0x0);
311 static int wm8731_mute(struct snd_soc_dai *dai, int mute)
313 struct snd_soc_codec *codec = dai->codec;
314 u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7;
317 wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8);
319 wm8731_write(codec, WM8731_APDIGI, mute_reg);
323 static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
324 int clk_id, unsigned int freq, int dir)
326 struct snd_soc_codec *codec = codec_dai->codec;
327 struct wm8731_priv *wm8731 = codec->private_data;
335 wm8731->sysclk = freq;
342 static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
345 struct snd_soc_codec *codec = codec_dai->codec;
348 /* set master/slave audio interface */
349 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
350 case SND_SOC_DAIFMT_CBM_CFM:
353 case SND_SOC_DAIFMT_CBS_CFS:
359 /* interface format */
360 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
361 case SND_SOC_DAIFMT_I2S:
364 case SND_SOC_DAIFMT_RIGHT_J:
366 case SND_SOC_DAIFMT_LEFT_J:
369 case SND_SOC_DAIFMT_DSP_A:
372 case SND_SOC_DAIFMT_DSP_B:
379 /* clock inversion */
380 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
381 case SND_SOC_DAIFMT_NB_NF:
383 case SND_SOC_DAIFMT_IB_IF:
386 case SND_SOC_DAIFMT_IB_NF:
389 case SND_SOC_DAIFMT_NB_IF:
397 wm8731_write(codec, WM8731_IFACE, iface);
401 static int wm8731_set_bias_level(struct snd_soc_codec *codec,
402 enum snd_soc_bias_level level)
407 case SND_SOC_BIAS_ON:
409 case SND_SOC_BIAS_PREPARE:
411 case SND_SOC_BIAS_STANDBY:
412 /* Clear PWROFF, gate CLKOUT, everything else as-is */
413 reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f;
414 wm8731_write(codec, WM8731_PWR, reg | 0x0040);
416 case SND_SOC_BIAS_OFF:
417 wm8731_write(codec, WM8731_ACTIVE, 0x0);
418 wm8731_write(codec, WM8731_PWR, 0xffff);
421 codec->bias_level = level;
425 #define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
426 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
427 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
428 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
429 SNDRV_PCM_RATE_96000)
431 #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
432 SNDRV_PCM_FMTBIT_S24_LE)
434 struct snd_soc_dai wm8731_dai = {
437 .stream_name = "Playback",
440 .rates = WM8731_RATES,
441 .formats = WM8731_FORMATS,},
443 .stream_name = "Capture",
446 .rates = WM8731_RATES,
447 .formats = WM8731_FORMATS,},
449 .prepare = wm8731_pcm_prepare,
450 .hw_params = wm8731_hw_params,
451 .shutdown = wm8731_shutdown,
452 .digital_mute = wm8731_mute,
453 .set_sysclk = wm8731_set_dai_sysclk,
454 .set_fmt = wm8731_set_dai_fmt,
457 EXPORT_SYMBOL_GPL(wm8731_dai);
459 static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
461 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
462 struct snd_soc_codec *codec = socdev->card->codec;
464 wm8731_write(codec, WM8731_ACTIVE, 0x0);
465 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
469 static int wm8731_resume(struct platform_device *pdev)
471 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
472 struct snd_soc_codec *codec = socdev->card->codec;
475 u16 *cache = codec->reg_cache;
477 /* Sync reg_cache with the hardware */
478 for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
479 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
480 data[1] = cache[i] & 0x00ff;
481 codec->hw_write(codec->control_data, data, 2);
483 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
484 wm8731_set_bias_level(codec, codec->suspend_bias_level);
489 * initialise the WM8731 driver
490 * register the mixer and dsp interfaces with the kernel
492 static int wm8731_init(struct snd_soc_device *socdev)
494 struct snd_soc_codec *codec = socdev->card->codec;
497 codec->name = "WM8731";
498 codec->owner = THIS_MODULE;
499 codec->read = wm8731_read_reg_cache;
500 codec->write = wm8731_write;
501 codec->set_bias_level = wm8731_set_bias_level;
502 codec->dai = &wm8731_dai;
504 codec->reg_cache_size = ARRAY_SIZE(wm8731_reg);
505 codec->reg_cache = kmemdup(wm8731_reg, sizeof(wm8731_reg), GFP_KERNEL);
506 if (codec->reg_cache == NULL)
512 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
514 printk(KERN_ERR "wm8731: failed to create pcms\n");
518 /* power on device */
519 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
521 /* set the update bits */
522 reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
523 wm8731_write(codec, WM8731_LOUT1V, reg & ~0x0100);
524 reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
525 wm8731_write(codec, WM8731_ROUT1V, reg & ~0x0100);
526 reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
527 wm8731_write(codec, WM8731_LINVOL, reg & ~0x0100);
528 reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
529 wm8731_write(codec, WM8731_RINVOL, reg & ~0x0100);
531 snd_soc_add_controls(codec, wm8731_snd_controls,
532 ARRAY_SIZE(wm8731_snd_controls));
533 wm8731_add_widgets(codec);
534 ret = snd_soc_init_card(socdev);
536 printk(KERN_ERR "wm8731: failed to register card\n");
543 snd_soc_free_pcms(socdev);
544 snd_soc_dapm_free(socdev);
546 kfree(codec->reg_cache);
550 static struct snd_soc_device *wm8731_socdev;
553 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
554 static struct i2c_driver wm8731_i2c_driver;
556 static int wm8731_add_i2c_device(struct platform_device *pdev,
557 const struct wm8731_setup_data *setup)
559 struct i2c_board_info info;
560 struct i2c_adapter *adapter;
561 struct i2c_client *client;
564 ret = i2c_add_driver(&wm8731_i2c_driver);
566 dev_err(&pdev->dev, "can't add i2c driver\n");
570 memset(&info, 0, sizeof(struct i2c_board_info));
571 info.addr = setup->i2c_address;
572 strlcpy(info.type, "wm8731", I2C_NAME_SIZE);
574 adapter = i2c_get_adapter(setup->i2c_bus);
576 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
581 client = i2c_new_device(adapter, &info);
582 i2c_put_adapter(adapter);
584 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
585 (unsigned int)info.addr);
592 i2c_del_driver(&wm8731_i2c_driver);
597 static int wm8731_probe(struct platform_device *pdev)
599 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
600 struct wm8731_setup_data *setup;
601 struct snd_soc_codec *codec;
602 struct wm8731_priv *wm8731;
605 setup = socdev->codec_data;
606 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
610 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
611 if (wm8731 == NULL) {
616 codec->private_data = wm8731;
617 socdev->card->codec = codec;
618 mutex_init(&codec->mutex);
619 INIT_LIST_HEAD(&codec->dapm_widgets);
620 INIT_LIST_HEAD(&codec->dapm_paths);
622 wm8731_socdev = socdev;
625 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
626 if (setup->i2c_address) {
627 codec->hw_write = (hw_write_t)i2c_master_send;
628 ret = wm8731_add_i2c_device(pdev, setup);
631 #if defined(CONFIG_SPI_MASTER)
633 codec->hw_write = (hw_write_t)wm8731_spi_write;
634 ret = spi_register_driver(&wm8731_spi_driver);
636 printk(KERN_ERR "can't add spi driver");
641 kfree(codec->private_data);
647 /* power down chip */
648 static int wm8731_remove(struct platform_device *pdev)
650 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
651 struct snd_soc_codec *codec = socdev->card->codec;
653 if (codec->control_data)
654 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
656 snd_soc_free_pcms(socdev);
657 snd_soc_dapm_free(socdev);
658 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
659 i2c_unregister_device(codec->control_data);
660 i2c_del_driver(&wm8731_i2c_driver);
662 #if defined(CONFIG_SPI_MASTER)
663 spi_unregister_driver(&wm8731_spi_driver);
665 kfree(codec->private_data);
671 struct snd_soc_codec_device soc_codec_dev_wm8731 = {
672 .probe = wm8731_probe,
673 .remove = wm8731_remove,
674 .suspend = wm8731_suspend,
675 .resume = wm8731_resume,
677 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
679 #if defined(CONFIG_SPI_MASTER)
680 static int __devinit wm8731_spi_probe(struct spi_device *spi)
682 struct snd_soc_device *socdev = wm8731_socdev;
683 struct snd_soc_codec *codec = socdev->card->codec;
686 codec->control_data = spi;
688 ret = wm8731_init(socdev);
690 dev_err(&spi->dev, "failed to initialise WM8731\n");
695 static int __devexit wm8731_spi_remove(struct spi_device *spi)
700 static struct spi_driver wm8731_spi_driver = {
703 .bus = &spi_bus_type,
704 .owner = THIS_MODULE,
706 .probe = wm8731_spi_probe,
707 .remove = __devexit_p(wm8731_spi_remove),
710 static int wm8731_spi_write(struct spi_device *spi, const char *data, int len)
712 struct spi_transfer t;
713 struct spi_message m;
722 spi_message_init(&m);
723 memset(&t, 0, (sizeof t));
728 spi_message_add_tail(&t, &m);
733 #endif /* CONFIG_SPI_MASTER */
735 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
737 * WM8731 2 wire address is determined by GPIO5
738 * state during powerup.
743 static int wm8731_i2c_probe(struct i2c_client *i2c,
744 const struct i2c_device_id *id)
746 struct snd_soc_device *socdev = wm8731_socdev;
747 struct snd_soc_codec *codec = socdev->card->codec;
750 i2c_set_clientdata(i2c, codec);
751 codec->control_data = i2c;
753 ret = wm8731_init(socdev);
755 pr_err("failed to initialise WM8731\n");
760 static int wm8731_i2c_remove(struct i2c_client *client)
762 struct snd_soc_codec *codec = i2c_get_clientdata(client);
763 kfree(codec->reg_cache);
767 static const struct i2c_device_id wm8731_i2c_id[] = {
771 MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
773 static struct i2c_driver wm8731_i2c_driver = {
775 .name = "WM8731 I2C Codec",
776 .owner = THIS_MODULE,
778 .probe = wm8731_i2c_probe,
779 .remove = wm8731_i2c_remove,
780 .id_table = wm8731_i2c_id,
784 static int __init wm8731_modinit(void)
786 return snd_soc_register_dai(&wm8731_dai);
788 module_init(wm8731_modinit);
790 static void __exit wm8731_exit(void)
792 snd_soc_unregister_dai(&wm8731_dai);
794 module_exit(wm8731_exit);
796 MODULE_DESCRIPTION("ASoC WM8731 driver");
797 MODULE_AUTHOR("Richard Purdie");
798 MODULE_LICENSE("GPL");