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 static struct snd_soc_codec *wm8731_codec;
33 struct snd_soc_codec_device soc_codec_dev_wm8731;
35 /* codec private data */
37 struct snd_soc_codec codec;
38 u16 reg_cache[WM8731_CACHEREGNUM];
42 #ifdef CONFIG_SPI_MASTER
43 static int wm8731_spi_write(struct spi_device *spi, const char *data, int len);
47 * wm8731 register cache
48 * We can't read the WM8731 register space when we are
49 * using 2 wire for device control, so we cache them instead.
50 * There is no point in caching the reset register
52 static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
53 0x0097, 0x0097, 0x0079, 0x0079,
54 0x000a, 0x0008, 0x009f, 0x000a,
58 #define wm8731_reset(c) snd_soc_write(c, WM8731_RESET, 0)
60 static const char *wm8731_input_select[] = {"Line In", "Mic"};
61 static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
63 static const struct soc_enum wm8731_enum[] = {
64 SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
65 SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
68 static const struct snd_kcontrol_new wm8731_snd_controls[] = {
70 SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
72 SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
75 SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0),
76 SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
78 SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
79 SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1),
81 SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1),
83 SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
84 SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
86 SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
90 static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
91 SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
92 SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
93 SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
97 static const struct snd_kcontrol_new wm8731_input_mux_controls =
98 SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
100 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
101 SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
102 &wm8731_output_mixer_controls[0],
103 ARRAY_SIZE(wm8731_output_mixer_controls)),
104 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
105 SND_SOC_DAPM_OUTPUT("LOUT"),
106 SND_SOC_DAPM_OUTPUT("LHPOUT"),
107 SND_SOC_DAPM_OUTPUT("ROUT"),
108 SND_SOC_DAPM_OUTPUT("RHPOUT"),
109 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
110 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
111 SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
112 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
113 SND_SOC_DAPM_INPUT("MICIN"),
114 SND_SOC_DAPM_INPUT("RLINEIN"),
115 SND_SOC_DAPM_INPUT("LLINEIN"),
118 static const struct snd_soc_dapm_route intercon[] = {
120 {"Output Mixer", "Line Bypass Switch", "Line Input"},
121 {"Output Mixer", "HiFi Playback Switch", "DAC"},
122 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
125 {"RHPOUT", NULL, "Output Mixer"},
126 {"ROUT", NULL, "Output Mixer"},
127 {"LHPOUT", NULL, "Output Mixer"},
128 {"LOUT", NULL, "Output Mixer"},
131 {"Input Mux", "Line In", "Line Input"},
132 {"Input Mux", "Mic", "Mic Bias"},
133 {"ADC", NULL, "Input Mux"},
136 {"Line Input", NULL, "LLINEIN"},
137 {"Line Input", NULL, "RLINEIN"},
138 {"Mic Bias", NULL, "MICIN"},
141 static int wm8731_add_widgets(struct snd_soc_codec *codec)
143 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
144 ARRAY_SIZE(wm8731_dapm_widgets));
146 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
148 snd_soc_dapm_new_widgets(codec);
161 /* codec mclk clock divider coefficients */
162 static const struct _coeff_div coeff_div[] = {
164 {12288000, 48000, 256, 0x0, 0x0, 0x0},
165 {18432000, 48000, 384, 0x0, 0x1, 0x0},
166 {12000000, 48000, 250, 0x0, 0x0, 0x1},
169 {12288000, 32000, 384, 0x6, 0x0, 0x0},
170 {18432000, 32000, 576, 0x6, 0x1, 0x0},
171 {12000000, 32000, 375, 0x6, 0x0, 0x1},
174 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
175 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
176 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
177 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
178 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
181 {12288000, 96000, 128, 0x7, 0x0, 0x0},
182 {18432000, 96000, 192, 0x7, 0x1, 0x0},
183 {12000000, 96000, 125, 0x7, 0x0, 0x1},
186 {11289600, 44100, 256, 0x8, 0x0, 0x0},
187 {16934400, 44100, 384, 0x8, 0x1, 0x0},
188 {12000000, 44100, 272, 0x8, 0x1, 0x1},
191 {11289600, 88200, 128, 0xf, 0x0, 0x0},
192 {16934400, 88200, 192, 0xf, 0x1, 0x0},
193 {12000000, 88200, 136, 0xf, 0x1, 0x1},
196 static inline int get_coeff(int mclk, int rate)
200 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
201 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
207 static int wm8731_hw_params(struct snd_pcm_substream *substream,
208 struct snd_pcm_hw_params *params,
209 struct snd_soc_dai *dai)
211 struct snd_soc_pcm_runtime *rtd = substream->private_data;
212 struct snd_soc_device *socdev = rtd->socdev;
213 struct snd_soc_codec *codec = socdev->card->codec;
214 struct wm8731_priv *wm8731 = codec->private_data;
215 u16 iface = snd_soc_read(codec, WM8731_IFACE) & 0xfff3;
216 int i = get_coeff(wm8731->sysclk, params_rate(params));
217 u16 srate = (coeff_div[i].sr << 2) |
218 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
220 snd_soc_write(codec, WM8731_SRATE, srate);
223 switch (params_format(params)) {
224 case SNDRV_PCM_FORMAT_S16_LE:
226 case SNDRV_PCM_FORMAT_S20_3LE:
229 case SNDRV_PCM_FORMAT_S24_LE:
234 snd_soc_write(codec, WM8731_IFACE, iface);
238 static int wm8731_pcm_prepare(struct snd_pcm_substream *substream,
239 struct snd_soc_dai *dai)
241 struct snd_soc_pcm_runtime *rtd = substream->private_data;
242 struct snd_soc_device *socdev = rtd->socdev;
243 struct snd_soc_codec *codec = socdev->card->codec;
246 snd_soc_write(codec, WM8731_ACTIVE, 0x0001);
251 static void wm8731_shutdown(struct snd_pcm_substream *substream,
252 struct snd_soc_dai *dai)
254 struct snd_soc_pcm_runtime *rtd = substream->private_data;
255 struct snd_soc_device *socdev = rtd->socdev;
256 struct snd_soc_codec *codec = socdev->card->codec;
259 if (!codec->active) {
261 snd_soc_write(codec, WM8731_ACTIVE, 0x0);
265 static int wm8731_mute(struct snd_soc_dai *dai, int mute)
267 struct snd_soc_codec *codec = dai->codec;
268 u16 mute_reg = snd_soc_read(codec, WM8731_APDIGI) & 0xfff7;
271 snd_soc_write(codec, WM8731_APDIGI, mute_reg | 0x8);
273 snd_soc_write(codec, WM8731_APDIGI, mute_reg);
277 static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
278 int clk_id, unsigned int freq, int dir)
280 struct snd_soc_codec *codec = codec_dai->codec;
281 struct wm8731_priv *wm8731 = codec->private_data;
289 wm8731->sysclk = freq;
296 static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
299 struct snd_soc_codec *codec = codec_dai->codec;
302 /* set master/slave audio interface */
303 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
304 case SND_SOC_DAIFMT_CBM_CFM:
307 case SND_SOC_DAIFMT_CBS_CFS:
313 /* interface format */
314 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
315 case SND_SOC_DAIFMT_I2S:
318 case SND_SOC_DAIFMT_RIGHT_J:
320 case SND_SOC_DAIFMT_LEFT_J:
323 case SND_SOC_DAIFMT_DSP_A:
326 case SND_SOC_DAIFMT_DSP_B:
333 /* clock inversion */
334 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
335 case SND_SOC_DAIFMT_NB_NF:
337 case SND_SOC_DAIFMT_IB_IF:
340 case SND_SOC_DAIFMT_IB_NF:
343 case SND_SOC_DAIFMT_NB_IF:
351 snd_soc_write(codec, WM8731_IFACE, iface);
355 static int wm8731_set_bias_level(struct snd_soc_codec *codec,
356 enum snd_soc_bias_level level)
361 case SND_SOC_BIAS_ON:
363 case SND_SOC_BIAS_PREPARE:
365 case SND_SOC_BIAS_STANDBY:
366 /* Clear PWROFF, gate CLKOUT, everything else as-is */
367 reg = snd_soc_read(codec, WM8731_PWR) & 0xff7f;
368 snd_soc_write(codec, WM8731_PWR, reg | 0x0040);
370 case SND_SOC_BIAS_OFF:
371 snd_soc_write(codec, WM8731_ACTIVE, 0x0);
372 snd_soc_write(codec, WM8731_PWR, 0xffff);
375 codec->bias_level = level;
379 #define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
380 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
381 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
382 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
383 SNDRV_PCM_RATE_96000)
385 #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
386 SNDRV_PCM_FMTBIT_S24_LE)
388 static struct snd_soc_dai_ops wm8731_dai_ops = {
389 .prepare = wm8731_pcm_prepare,
390 .hw_params = wm8731_hw_params,
391 .shutdown = wm8731_shutdown,
392 .digital_mute = wm8731_mute,
393 .set_sysclk = wm8731_set_dai_sysclk,
394 .set_fmt = wm8731_set_dai_fmt,
397 struct snd_soc_dai wm8731_dai = {
400 .stream_name = "Playback",
403 .rates = WM8731_RATES,
404 .formats = WM8731_FORMATS,},
406 .stream_name = "Capture",
409 .rates = WM8731_RATES,
410 .formats = WM8731_FORMATS,},
411 .ops = &wm8731_dai_ops,
413 EXPORT_SYMBOL_GPL(wm8731_dai);
416 static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
418 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
419 struct snd_soc_codec *codec = socdev->card->codec;
421 snd_soc_write(codec, WM8731_ACTIVE, 0x0);
422 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
426 static int wm8731_resume(struct platform_device *pdev)
428 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
429 struct snd_soc_codec *codec = socdev->card->codec;
432 u16 *cache = codec->reg_cache;
434 /* Sync reg_cache with the hardware */
435 for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
436 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
437 data[1] = cache[i] & 0x00ff;
438 codec->hw_write(codec->control_data, data, 2);
440 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
441 wm8731_set_bias_level(codec, codec->suspend_bias_level);
445 #define wm8731_suspend NULL
446 #define wm8731_resume NULL
449 static int wm8731_probe(struct platform_device *pdev)
451 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
452 struct snd_soc_codec *codec;
455 if (wm8731_codec == NULL) {
456 dev_err(&pdev->dev, "Codec device not registered\n");
460 socdev->card->codec = wm8731_codec;
461 codec = wm8731_codec;
464 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
466 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
470 snd_soc_add_controls(codec, wm8731_snd_controls,
471 ARRAY_SIZE(wm8731_snd_controls));
472 wm8731_add_widgets(codec);
473 ret = snd_soc_init_card(socdev);
475 dev_err(codec->dev, "failed to register card: %d\n", ret);
482 snd_soc_free_pcms(socdev);
483 snd_soc_dapm_free(socdev);
488 /* power down chip */
489 static int wm8731_remove(struct platform_device *pdev)
491 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
493 snd_soc_free_pcms(socdev);
494 snd_soc_dapm_free(socdev);
499 struct snd_soc_codec_device soc_codec_dev_wm8731 = {
500 .probe = wm8731_probe,
501 .remove = wm8731_remove,
502 .suspend = wm8731_suspend,
503 .resume = wm8731_resume,
505 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
507 static int wm8731_register(struct wm8731_priv *wm8731,
508 enum snd_soc_control_type control)
511 struct snd_soc_codec *codec = &wm8731->codec;
514 dev_err(codec->dev, "Another WM8731 is registered\n");
519 mutex_init(&codec->mutex);
520 INIT_LIST_HEAD(&codec->dapm_widgets);
521 INIT_LIST_HEAD(&codec->dapm_paths);
523 codec->private_data = wm8731;
524 codec->name = "WM8731";
525 codec->owner = THIS_MODULE;
526 codec->bias_level = SND_SOC_BIAS_OFF;
527 codec->set_bias_level = wm8731_set_bias_level;
528 codec->dai = &wm8731_dai;
530 codec->reg_cache_size = WM8731_CACHEREGNUM;
531 codec->reg_cache = &wm8731->reg_cache;
533 memcpy(codec->reg_cache, wm8731_reg, sizeof(wm8731_reg));
535 ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
537 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
541 ret = wm8731_reset(codec);
543 dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
547 wm8731_dai.dev = codec->dev;
549 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
551 /* Latch the update bits */
552 snd_soc_update_bits(codec, WM8731_LOUT1V, 0x100, 0);
553 snd_soc_update_bits(codec, WM8731_ROUT1V, 0x100, 0);
554 snd_soc_update_bits(codec, WM8731_LINVOL, 0x100, 0);
555 snd_soc_update_bits(codec, WM8731_RINVOL, 0x100, 0);
557 /* Disable bypass path by default */
558 snd_soc_update_bits(codec, WM8731_APANA, 0x4, 0);
560 wm8731_codec = codec;
562 ret = snd_soc_register_codec(codec);
564 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
568 ret = snd_soc_register_dai(&wm8731_dai);
570 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
571 snd_soc_unregister_codec(codec);
578 snd_soc_unregister_codec(codec);
584 static void wm8731_unregister(struct wm8731_priv *wm8731)
586 wm8731_set_bias_level(&wm8731->codec, SND_SOC_BIAS_OFF);
587 snd_soc_unregister_dai(&wm8731_dai);
588 snd_soc_unregister_codec(&wm8731->codec);
593 #if defined(CONFIG_SPI_MASTER)
594 static int __devinit wm8731_spi_probe(struct spi_device *spi)
596 struct snd_soc_codec *codec;
597 struct wm8731_priv *wm8731;
599 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
603 codec = &wm8731->codec;
604 codec->control_data = spi;
605 codec->dev = &spi->dev;
607 dev_set_drvdata(&spi->dev, wm8731);
609 return wm8731_register(wm8731, SND_SOC_SPI);
612 static int __devexit wm8731_spi_remove(struct spi_device *spi)
614 struct wm8731_priv *wm8731 = dev_get_drvdata(&spi->dev);
616 wm8731_unregister(wm8731);
622 static int wm8731_spi_suspend(struct spi_device *spi, pm_message_t msg)
624 return snd_soc_suspend_device(&spi->dev);
627 static int wm8731_spi_resume(struct spi_device *spi)
629 return snd_soc_resume_device(&spi->dev);
632 #define wm8731_spi_suspend NULL
633 #define wm8731_spi_resume NULL
636 static struct spi_driver wm8731_spi_driver = {
639 .bus = &spi_bus_type,
640 .owner = THIS_MODULE,
642 .probe = wm8731_spi_probe,
643 .suspend = wm8731_spi_suspend,
644 .resume = wm8731_spi_resume,
645 .remove = __devexit_p(wm8731_spi_remove),
647 #endif /* CONFIG_SPI_MASTER */
649 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
650 static __devinit int wm8731_i2c_probe(struct i2c_client *i2c,
651 const struct i2c_device_id *id)
653 struct wm8731_priv *wm8731;
654 struct snd_soc_codec *codec;
656 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
660 codec = &wm8731->codec;
662 i2c_set_clientdata(i2c, wm8731);
663 codec->control_data = i2c;
665 codec->dev = &i2c->dev;
667 return wm8731_register(wm8731, SND_SOC_I2C);
670 static __devexit int wm8731_i2c_remove(struct i2c_client *client)
672 struct wm8731_priv *wm8731 = i2c_get_clientdata(client);
673 wm8731_unregister(wm8731);
678 static int wm8731_i2c_suspend(struct i2c_client *i2c, pm_message_t msg)
680 return snd_soc_suspend_device(&i2c->dev);
683 static int wm8731_i2c_resume(struct i2c_client *i2c)
685 return snd_soc_resume_device(&i2c->dev);
688 #define wm8731_i2c_suspend NULL
689 #define wm8731_i2c_resume NULL
692 static const struct i2c_device_id wm8731_i2c_id[] = {
696 MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
698 static struct i2c_driver wm8731_i2c_driver = {
700 .name = "WM8731 I2C Codec",
701 .owner = THIS_MODULE,
703 .probe = wm8731_i2c_probe,
704 .remove = __devexit_p(wm8731_i2c_remove),
705 .suspend = wm8731_i2c_suspend,
706 .resume = wm8731_i2c_resume,
707 .id_table = wm8731_i2c_id,
711 static int __init wm8731_modinit(void)
714 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
715 ret = i2c_add_driver(&wm8731_i2c_driver);
717 printk(KERN_ERR "Failed to register WM8731 I2C driver: %d\n",
721 #if defined(CONFIG_SPI_MASTER)
722 ret = spi_register_driver(&wm8731_spi_driver);
724 printk(KERN_ERR "Failed to register WM8731 SPI driver: %d\n",
730 module_init(wm8731_modinit);
732 static void __exit wm8731_exit(void)
734 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
735 i2c_del_driver(&wm8731_i2c_driver);
737 #if defined(CONFIG_SPI_MASTER)
738 spi_unregister_driver(&wm8731_spi_driver);
741 module_exit(wm8731_exit);
743 MODULE_DESCRIPTION("ASoC WM8731 driver");
744 MODULE_AUTHOR("Richard Purdie");
745 MODULE_LICENSE("GPL");