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 */
40 * wm8731 register cache
41 * We can't read the WM8731 register space when we are
42 * using 2 wire for device control, so we cache them instead.
43 * There is no point in caching the reset register
45 static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
46 0x0097, 0x0097, 0x0079, 0x0079,
47 0x000a, 0x0008, 0x009f, 0x000a,
52 * read wm8731 register cache
54 static inline unsigned int wm8731_read_reg_cache(struct snd_soc_codec *codec,
57 u16 *cache = codec->reg_cache;
58 if (reg == WM8731_RESET)
60 if (reg >= WM8731_CACHEREGNUM)
66 * write wm8731 register cache
68 static inline void wm8731_write_reg_cache(struct snd_soc_codec *codec,
69 u16 reg, unsigned int value)
71 u16 *cache = codec->reg_cache;
72 if (reg >= WM8731_CACHEREGNUM)
78 * write to the WM8731 register space
80 static int wm8731_write(struct snd_soc_codec *codec, unsigned int reg,
86 * D15..D9 WM8731 register offset
87 * D8...D0 register data
89 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
90 data[1] = value & 0x00ff;
92 wm8731_write_reg_cache(codec, reg, value);
93 if (codec->hw_write(codec->control_data, data, 2) == 2)
99 #define wm8731_reset(c) wm8731_write(c, WM8731_RESET, 0)
101 static const char *wm8731_input_select[] = {"Line In", "Mic"};
102 static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
104 static const struct soc_enum wm8731_enum[] = {
105 SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
106 SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
109 static const struct snd_kcontrol_new wm8731_snd_controls[] = {
111 SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
113 SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
116 SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0),
117 SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
119 SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
120 SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1),
122 SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1),
124 SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
125 SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
127 SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
131 static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
132 SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
133 SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
134 SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
138 static const struct snd_kcontrol_new wm8731_input_mux_controls =
139 SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
141 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
142 SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
143 &wm8731_output_mixer_controls[0],
144 ARRAY_SIZE(wm8731_output_mixer_controls)),
145 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
146 SND_SOC_DAPM_OUTPUT("LOUT"),
147 SND_SOC_DAPM_OUTPUT("LHPOUT"),
148 SND_SOC_DAPM_OUTPUT("ROUT"),
149 SND_SOC_DAPM_OUTPUT("RHPOUT"),
150 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
151 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
152 SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
153 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
154 SND_SOC_DAPM_INPUT("MICIN"),
155 SND_SOC_DAPM_INPUT("RLINEIN"),
156 SND_SOC_DAPM_INPUT("LLINEIN"),
159 static const struct snd_soc_dapm_route intercon[] = {
161 {"Output Mixer", "Line Bypass Switch", "Line Input"},
162 {"Output Mixer", "HiFi Playback Switch", "DAC"},
163 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
166 {"RHPOUT", NULL, "Output Mixer"},
167 {"ROUT", NULL, "Output Mixer"},
168 {"LHPOUT", NULL, "Output Mixer"},
169 {"LOUT", NULL, "Output Mixer"},
172 {"Input Mux", "Line In", "Line Input"},
173 {"Input Mux", "Mic", "Mic Bias"},
174 {"ADC", NULL, "Input Mux"},
177 {"Line Input", NULL, "LLINEIN"},
178 {"Line Input", NULL, "RLINEIN"},
179 {"Mic Bias", NULL, "MICIN"},
182 static int wm8731_add_widgets(struct snd_soc_codec *codec)
184 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
185 ARRAY_SIZE(wm8731_dapm_widgets));
187 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
189 snd_soc_dapm_new_widgets(codec);
202 /* codec mclk clock divider coefficients */
203 static const struct _coeff_div coeff_div[] = {
205 {12288000, 48000, 256, 0x0, 0x0, 0x0},
206 {18432000, 48000, 384, 0x0, 0x1, 0x0},
207 {12000000, 48000, 250, 0x0, 0x0, 0x1},
210 {12288000, 32000, 384, 0x6, 0x0, 0x0},
211 {18432000, 32000, 576, 0x6, 0x1, 0x0},
212 {12000000, 32000, 375, 0x6, 0x0, 0x1},
215 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
216 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
217 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
218 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
219 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
222 {12288000, 96000, 128, 0x7, 0x0, 0x0},
223 {18432000, 96000, 192, 0x7, 0x1, 0x0},
224 {12000000, 96000, 125, 0x7, 0x0, 0x1},
227 {11289600, 44100, 256, 0x8, 0x0, 0x0},
228 {16934400, 44100, 384, 0x8, 0x1, 0x0},
229 {12000000, 44100, 272, 0x8, 0x1, 0x1},
232 {11289600, 88200, 128, 0xf, 0x0, 0x0},
233 {16934400, 88200, 192, 0xf, 0x1, 0x0},
234 {12000000, 88200, 136, 0xf, 0x1, 0x1},
237 static inline int get_coeff(int mclk, int rate)
241 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
242 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
248 static int wm8731_hw_params(struct snd_pcm_substream *substream,
249 struct snd_pcm_hw_params *params,
250 struct snd_soc_dai *dai)
252 struct snd_soc_pcm_runtime *rtd = substream->private_data;
253 struct snd_soc_device *socdev = rtd->socdev;
254 struct snd_soc_codec *codec = socdev->card->codec;
255 struct wm8731_priv *wm8731 = codec->private_data;
256 u16 iface = wm8731_read_reg_cache(codec, WM8731_IFACE) & 0xfff3;
257 int i = get_coeff(wm8731->sysclk, params_rate(params));
258 u16 srate = (coeff_div[i].sr << 2) |
259 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
261 wm8731_write(codec, WM8731_SRATE, srate);
264 switch (params_format(params)) {
265 case SNDRV_PCM_FORMAT_S16_LE:
267 case SNDRV_PCM_FORMAT_S20_3LE:
270 case SNDRV_PCM_FORMAT_S24_LE:
275 wm8731_write(codec, WM8731_IFACE, iface);
279 static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
280 struct snd_soc_dai *dai)
282 struct snd_soc_pcm_runtime *rtd = substream->private_data;
283 struct snd_soc_device *socdev = rtd->socdev;
284 struct snd_soc_codec *codec = socdev->card->codec;
287 wm8731_write(codec, WM8731_ACTIVE, 0x0001);
292 static void wm8731_shutdown(struct snd_pcm_substream *substream,
293 struct snd_soc_dai *dai)
295 struct snd_soc_pcm_runtime *rtd = substream->private_data;
296 struct snd_soc_device *socdev = rtd->socdev;
297 struct snd_soc_codec *codec = socdev->card->codec;
300 if (!codec->active) {
302 wm8731_write(codec, WM8731_ACTIVE, 0x0);
306 static int wm8731_mute(struct snd_soc_dai *dai, int mute)
308 struct snd_soc_codec *codec = dai->codec;
309 u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7;
312 wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8);
314 wm8731_write(codec, WM8731_APDIGI, mute_reg);
318 static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
319 int clk_id, unsigned int freq, int dir)
321 struct snd_soc_codec *codec = codec_dai->codec;
322 struct wm8731_priv *wm8731 = codec->private_data;
330 wm8731->sysclk = freq;
337 static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
340 struct snd_soc_codec *codec = codec_dai->codec;
343 /* set master/slave audio interface */
344 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
345 case SND_SOC_DAIFMT_CBM_CFM:
348 case SND_SOC_DAIFMT_CBS_CFS:
354 /* interface format */
355 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
356 case SND_SOC_DAIFMT_I2S:
359 case SND_SOC_DAIFMT_RIGHT_J:
361 case SND_SOC_DAIFMT_LEFT_J:
364 case SND_SOC_DAIFMT_DSP_A:
367 case SND_SOC_DAIFMT_DSP_B:
374 /* clock inversion */
375 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
376 case SND_SOC_DAIFMT_NB_NF:
378 case SND_SOC_DAIFMT_IB_IF:
381 case SND_SOC_DAIFMT_IB_NF:
384 case SND_SOC_DAIFMT_NB_IF:
392 wm8731_write(codec, WM8731_IFACE, iface);
396 static int wm8731_set_bias_level(struct snd_soc_codec *codec,
397 enum snd_soc_bias_level level)
402 case SND_SOC_BIAS_ON:
404 case SND_SOC_BIAS_PREPARE:
406 case SND_SOC_BIAS_STANDBY:
407 /* Clear PWROFF, gate CLKOUT, everything else as-is */
408 reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f;
409 wm8731_write(codec, WM8731_PWR, reg | 0x0040);
411 case SND_SOC_BIAS_OFF:
412 wm8731_write(codec, WM8731_ACTIVE, 0x0);
413 wm8731_write(codec, WM8731_PWR, 0xffff);
416 codec->bias_level = level;
420 #define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
421 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
422 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
423 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
424 SNDRV_PCM_RATE_96000)
426 #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
427 SNDRV_PCM_FMTBIT_S24_LE)
429 struct snd_soc_dai wm8731_dai = {
432 .stream_name = "Playback",
435 .rates = WM8731_RATES,
436 .formats = WM8731_FORMATS,},
438 .stream_name = "Capture",
441 .rates = WM8731_RATES,
442 .formats = WM8731_FORMATS,},
444 .prepare = wm8731_pcm_prepare,
445 .hw_params = wm8731_hw_params,
446 .shutdown = wm8731_shutdown,
447 .digital_mute = wm8731_mute,
448 .set_sysclk = wm8731_set_dai_sysclk,
449 .set_fmt = wm8731_set_dai_fmt,
452 EXPORT_SYMBOL_GPL(wm8731_dai);
454 static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
456 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
457 struct snd_soc_codec *codec = socdev->card->codec;
459 wm8731_write(codec, WM8731_ACTIVE, 0x0);
460 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
464 static int wm8731_resume(struct platform_device *pdev)
466 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
467 struct snd_soc_codec *codec = socdev->card->codec;
470 u16 *cache = codec->reg_cache;
472 /* Sync reg_cache with the hardware */
473 for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
474 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
475 data[1] = cache[i] & 0x00ff;
476 codec->hw_write(codec->control_data, data, 2);
478 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
479 wm8731_set_bias_level(codec, codec->suspend_bias_level);
484 * initialise the WM8731 driver
485 * register the mixer and dsp interfaces with the kernel
487 static int wm8731_init(struct snd_soc_device *socdev)
489 struct snd_soc_codec *codec = socdev->card->codec;
492 codec->name = "WM8731";
493 codec->owner = THIS_MODULE;
494 codec->read = wm8731_read_reg_cache;
495 codec->write = wm8731_write;
496 codec->set_bias_level = wm8731_set_bias_level;
497 codec->dai = &wm8731_dai;
499 codec->reg_cache_size = ARRAY_SIZE(wm8731_reg);
500 codec->reg_cache = kmemdup(wm8731_reg, sizeof(wm8731_reg), GFP_KERNEL);
501 if (codec->reg_cache == NULL)
507 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
509 printk(KERN_ERR "wm8731: failed to create pcms\n");
513 /* power on device */
514 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
516 /* set the update bits */
517 reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
518 wm8731_write(codec, WM8731_LOUT1V, reg & ~0x0100);
519 reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
520 wm8731_write(codec, WM8731_ROUT1V, reg & ~0x0100);
521 reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
522 wm8731_write(codec, WM8731_LINVOL, reg & ~0x0100);
523 reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
524 wm8731_write(codec, WM8731_RINVOL, reg & ~0x0100);
526 snd_soc_add_controls(codec, wm8731_snd_controls,
527 ARRAY_SIZE(wm8731_snd_controls));
528 wm8731_add_widgets(codec);
529 ret = snd_soc_init_card(socdev);
531 printk(KERN_ERR "wm8731: failed to register card\n");
538 snd_soc_free_pcms(socdev);
539 snd_soc_dapm_free(socdev);
541 kfree(codec->reg_cache);
545 static struct snd_soc_device *wm8731_socdev;
547 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
550 * WM8731 2 wire address is determined by GPIO5
551 * state during powerup.
556 static int wm8731_i2c_probe(struct i2c_client *i2c,
557 const struct i2c_device_id *id)
559 struct snd_soc_device *socdev = wm8731_socdev;
560 struct snd_soc_codec *codec = socdev->card->codec;
563 i2c_set_clientdata(i2c, codec);
564 codec->control_data = i2c;
566 ret = wm8731_init(socdev);
568 pr_err("failed to initialise WM8731\n");
573 static int wm8731_i2c_remove(struct i2c_client *client)
575 struct snd_soc_codec *codec = i2c_get_clientdata(client);
576 kfree(codec->reg_cache);
580 static const struct i2c_device_id wm8731_i2c_id[] = {
584 MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
586 static struct i2c_driver wm8731_i2c_driver = {
588 .name = "WM8731 I2C Codec",
589 .owner = THIS_MODULE,
591 .probe = wm8731_i2c_probe,
592 .remove = wm8731_i2c_remove,
593 .id_table = wm8731_i2c_id,
596 static int wm8731_add_i2c_device(struct platform_device *pdev,
597 const struct wm8731_setup_data *setup)
599 struct i2c_board_info info;
600 struct i2c_adapter *adapter;
601 struct i2c_client *client;
604 ret = i2c_add_driver(&wm8731_i2c_driver);
606 dev_err(&pdev->dev, "can't add i2c driver\n");
610 memset(&info, 0, sizeof(struct i2c_board_info));
611 info.addr = setup->i2c_address;
612 strlcpy(info.type, "wm8731", I2C_NAME_SIZE);
614 adapter = i2c_get_adapter(setup->i2c_bus);
616 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
621 client = i2c_new_device(adapter, &info);
622 i2c_put_adapter(adapter);
624 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
625 (unsigned int)info.addr);
632 i2c_del_driver(&wm8731_i2c_driver);
637 #if defined(CONFIG_SPI_MASTER)
638 static int __devinit wm8731_spi_probe(struct spi_device *spi)
640 struct snd_soc_device *socdev = wm8731_socdev;
641 struct snd_soc_codec *codec = socdev->card->codec;
644 codec->control_data = spi;
646 ret = wm8731_init(socdev);
648 dev_err(&spi->dev, "failed to initialise WM8731\n");
653 static int __devexit wm8731_spi_remove(struct spi_device *spi)
658 static struct spi_driver wm8731_spi_driver = {
661 .bus = &spi_bus_type,
662 .owner = THIS_MODULE,
664 .probe = wm8731_spi_probe,
665 .remove = __devexit_p(wm8731_spi_remove),
668 static int wm8731_spi_write(struct spi_device *spi, const char *data, int len)
670 struct spi_transfer t;
671 struct spi_message m;
680 spi_message_init(&m);
681 memset(&t, 0, (sizeof t));
686 spi_message_add_tail(&t, &m);
691 #endif /* CONFIG_SPI_MASTER */
693 static int wm8731_probe(struct platform_device *pdev)
695 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
696 struct wm8731_setup_data *setup;
697 struct snd_soc_codec *codec;
698 struct wm8731_priv *wm8731;
701 setup = socdev->codec_data;
702 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
706 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
707 if (wm8731 == NULL) {
712 codec->private_data = wm8731;
713 socdev->card->codec = codec;
714 mutex_init(&codec->mutex);
715 INIT_LIST_HEAD(&codec->dapm_widgets);
716 INIT_LIST_HEAD(&codec->dapm_paths);
718 wm8731_socdev = socdev;
721 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
722 if (setup->i2c_address) {
723 codec->hw_write = (hw_write_t)i2c_master_send;
724 ret = wm8731_add_i2c_device(pdev, setup);
727 #if defined(CONFIG_SPI_MASTER)
729 codec->hw_write = (hw_write_t)wm8731_spi_write;
730 ret = spi_register_driver(&wm8731_spi_driver);
732 printk(KERN_ERR "can't add spi driver");
737 kfree(codec->private_data);
743 /* power down chip */
744 static int wm8731_remove(struct platform_device *pdev)
746 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
747 struct snd_soc_codec *codec = socdev->card->codec;
749 if (codec->control_data)
750 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
752 snd_soc_free_pcms(socdev);
753 snd_soc_dapm_free(socdev);
754 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
755 i2c_unregister_device(codec->control_data);
756 i2c_del_driver(&wm8731_i2c_driver);
758 #if defined(CONFIG_SPI_MASTER)
759 spi_unregister_driver(&wm8731_spi_driver);
761 kfree(codec->private_data);
767 struct snd_soc_codec_device soc_codec_dev_wm8731 = {
768 .probe = wm8731_probe,
769 .remove = wm8731_remove,
770 .suspend = wm8731_suspend,
771 .resume = wm8731_resume,
773 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
775 static int __init wm8731_modinit(void)
777 return snd_soc_register_dai(&wm8731_dai);
779 module_init(wm8731_modinit);
781 static void __exit wm8731_exit(void)
783 snd_soc_unregister_dai(&wm8731_dai);
785 module_exit(wm8731_exit);
787 MODULE_DESCRIPTION("ASoC WM8731 driver");
788 MODULE_AUTHOR("Richard Purdie");
789 MODULE_LICENSE("GPL");