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 #define AUDIO_NAME "wm8731"
33 #define WM8731_VERSION "0.13"
35 struct snd_soc_codec_device soc_codec_dev_wm8731;
37 /* codec private data */
43 * wm8731 register cache
44 * We can't read the WM8731 register space when we are
45 * using 2 wire for device control, so we cache them instead.
46 * There is no point in caching the reset register
48 static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
49 0x0097, 0x0097, 0x0079, 0x0079,
50 0x000a, 0x0008, 0x009f, 0x000a,
55 * read wm8731 register cache
57 static inline unsigned int wm8731_read_reg_cache(struct snd_soc_codec *codec,
60 u16 *cache = codec->reg_cache;
61 if (reg == WM8731_RESET)
63 if (reg >= WM8731_CACHEREGNUM)
69 * write wm8731 register cache
71 static inline void wm8731_write_reg_cache(struct snd_soc_codec *codec,
72 u16 reg, unsigned int value)
74 u16 *cache = codec->reg_cache;
75 if (reg >= WM8731_CACHEREGNUM)
81 * write to the WM8731 register space
83 static int wm8731_write(struct snd_soc_codec *codec, unsigned int reg,
89 * D15..D9 WM8731 register offset
90 * D8...D0 register data
92 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
93 data[1] = value & 0x00ff;
95 wm8731_write_reg_cache(codec, reg, value);
96 if (codec->hw_write(codec->control_data, data, 2) == 2)
102 #define wm8731_reset(c) wm8731_write(c, WM8731_RESET, 0)
104 static const char *wm8731_input_select[] = {"Line In", "Mic"};
105 static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
107 static const struct soc_enum wm8731_enum[] = {
108 SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
109 SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
112 static const struct snd_kcontrol_new wm8731_snd_controls[] = {
114 SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
116 SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
119 SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0),
120 SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
122 SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
123 SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1),
125 SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1),
127 SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
128 SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
130 SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
133 /* add non dapm controls */
134 static int wm8731_add_controls(struct snd_soc_codec *codec)
138 for (i = 0; i < ARRAY_SIZE(wm8731_snd_controls); i++) {
139 err = snd_ctl_add(codec->card,
140 snd_soc_cnew(&wm8731_snd_controls[i],
150 static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
151 SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
152 SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
153 SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
157 static const struct snd_kcontrol_new wm8731_input_mux_controls =
158 SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
160 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
161 SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
162 &wm8731_output_mixer_controls[0],
163 ARRAY_SIZE(wm8731_output_mixer_controls)),
164 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
165 SND_SOC_DAPM_OUTPUT("LOUT"),
166 SND_SOC_DAPM_OUTPUT("LHPOUT"),
167 SND_SOC_DAPM_OUTPUT("ROUT"),
168 SND_SOC_DAPM_OUTPUT("RHPOUT"),
169 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
170 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
171 SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
172 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
173 SND_SOC_DAPM_INPUT("MICIN"),
174 SND_SOC_DAPM_INPUT("RLINEIN"),
175 SND_SOC_DAPM_INPUT("LLINEIN"),
178 static const struct snd_soc_dapm_route intercon[] = {
180 {"Output Mixer", "Line Bypass Switch", "Line Input"},
181 {"Output Mixer", "HiFi Playback Switch", "DAC"},
182 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
185 {"RHPOUT", NULL, "Output Mixer"},
186 {"ROUT", NULL, "Output Mixer"},
187 {"LHPOUT", NULL, "Output Mixer"},
188 {"LOUT", NULL, "Output Mixer"},
191 {"Input Mux", "Line In", "Line Input"},
192 {"Input Mux", "Mic", "Mic Bias"},
193 {"ADC", NULL, "Input Mux"},
196 {"Line Input", NULL, "LLINEIN"},
197 {"Line Input", NULL, "RLINEIN"},
198 {"Mic Bias", NULL, "MICIN"},
201 static int wm8731_add_widgets(struct snd_soc_codec *codec)
203 snd_soc_dapm_new_controls(codec, wm8731_dapm_widgets,
204 ARRAY_SIZE(wm8731_dapm_widgets));
206 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
208 snd_soc_dapm_new_widgets(codec);
221 /* codec mclk clock divider coefficients */
222 static const struct _coeff_div coeff_div[] = {
224 {12288000, 48000, 256, 0x0, 0x0, 0x0},
225 {18432000, 48000, 384, 0x0, 0x1, 0x0},
226 {12000000, 48000, 250, 0x0, 0x0, 0x1},
229 {12288000, 32000, 384, 0x6, 0x0, 0x0},
230 {18432000, 32000, 576, 0x6, 0x1, 0x0},
231 {12000000, 32000, 375, 0x6, 0x0, 0x1},
234 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
235 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
236 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
237 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
238 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
241 {12288000, 96000, 128, 0x7, 0x0, 0x0},
242 {18432000, 96000, 192, 0x7, 0x1, 0x0},
243 {12000000, 96000, 125, 0x7, 0x0, 0x1},
246 {11289600, 44100, 256, 0x8, 0x0, 0x0},
247 {16934400, 44100, 384, 0x8, 0x1, 0x0},
248 {12000000, 44100, 272, 0x8, 0x1, 0x1},
251 {11289600, 88200, 128, 0xf, 0x0, 0x0},
252 {16934400, 88200, 192, 0xf, 0x1, 0x0},
253 {12000000, 88200, 136, 0xf, 0x1, 0x1},
256 static inline int get_coeff(int mclk, int rate)
260 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
261 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
267 static int wm8731_hw_params(struct snd_pcm_substream *substream,
268 struct snd_pcm_hw_params *params)
270 struct snd_soc_pcm_runtime *rtd = substream->private_data;
271 struct snd_soc_device *socdev = rtd->socdev;
272 struct snd_soc_codec *codec = socdev->codec;
273 struct wm8731_priv *wm8731 = codec->private_data;
274 u16 iface = wm8731_read_reg_cache(codec, WM8731_IFACE) & 0xfff3;
275 int i = get_coeff(wm8731->sysclk, params_rate(params));
276 u16 srate = (coeff_div[i].sr << 2) |
277 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
279 wm8731_write(codec, WM8731_SRATE, srate);
282 switch (params_format(params)) {
283 case SNDRV_PCM_FORMAT_S16_LE:
285 case SNDRV_PCM_FORMAT_S20_3LE:
288 case SNDRV_PCM_FORMAT_S24_LE:
293 wm8731_write(codec, WM8731_IFACE, iface);
297 static int wm8731_pcm_prepare(struct snd_pcm_substream *substream)
299 struct snd_soc_pcm_runtime *rtd = substream->private_data;
300 struct snd_soc_device *socdev = rtd->socdev;
301 struct snd_soc_codec *codec = socdev->codec;
304 wm8731_write(codec, WM8731_ACTIVE, 0x0001);
309 static void wm8731_shutdown(struct snd_pcm_substream *substream)
311 struct snd_soc_pcm_runtime *rtd = substream->private_data;
312 struct snd_soc_device *socdev = rtd->socdev;
313 struct snd_soc_codec *codec = socdev->codec;
316 if (!codec->active) {
318 wm8731_write(codec, WM8731_ACTIVE, 0x0);
322 static int wm8731_mute(struct snd_soc_dai *dai, int mute)
324 struct snd_soc_codec *codec = dai->codec;
325 u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7;
328 wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8);
330 wm8731_write(codec, WM8731_APDIGI, mute_reg);
334 static int wm8731_set_dai_sysclk(struct snd_soc_dai *codec_dai,
335 int clk_id, unsigned int freq, int dir)
337 struct snd_soc_codec *codec = codec_dai->codec;
338 struct wm8731_priv *wm8731 = codec->private_data;
346 wm8731->sysclk = freq;
353 static int wm8731_set_dai_fmt(struct snd_soc_dai *codec_dai,
356 struct snd_soc_codec *codec = codec_dai->codec;
359 /* set master/slave audio interface */
360 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
361 case SND_SOC_DAIFMT_CBM_CFM:
364 case SND_SOC_DAIFMT_CBS_CFS:
370 /* interface format */
371 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
372 case SND_SOC_DAIFMT_I2S:
375 case SND_SOC_DAIFMT_RIGHT_J:
377 case SND_SOC_DAIFMT_LEFT_J:
380 case SND_SOC_DAIFMT_DSP_A:
383 case SND_SOC_DAIFMT_DSP_B:
390 /* clock inversion */
391 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
392 case SND_SOC_DAIFMT_NB_NF:
394 case SND_SOC_DAIFMT_IB_IF:
397 case SND_SOC_DAIFMT_IB_NF:
400 case SND_SOC_DAIFMT_NB_IF:
408 wm8731_write(codec, WM8731_IFACE, iface);
412 static int wm8731_set_bias_level(struct snd_soc_codec *codec,
413 enum snd_soc_bias_level level)
415 u16 reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f;
418 case SND_SOC_BIAS_ON:
419 /* vref/mid, osc on, dac unmute */
420 wm8731_write(codec, WM8731_PWR, reg);
422 case SND_SOC_BIAS_PREPARE:
424 case SND_SOC_BIAS_STANDBY:
425 /* everything off except vref/vmid, */
426 wm8731_write(codec, WM8731_PWR, reg | 0x0040);
428 case SND_SOC_BIAS_OFF:
429 /* everything off, dac mute, inactive */
430 wm8731_write(codec, WM8731_ACTIVE, 0x0);
431 wm8731_write(codec, WM8731_PWR, 0xffff);
434 codec->bias_level = level;
438 #define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
439 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
440 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
441 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
442 SNDRV_PCM_RATE_96000)
444 #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
445 SNDRV_PCM_FMTBIT_S24_LE)
447 struct snd_soc_dai wm8731_dai = {
450 .stream_name = "Playback",
453 .rates = WM8731_RATES,
454 .formats = WM8731_FORMATS,},
456 .stream_name = "Capture",
459 .rates = WM8731_RATES,
460 .formats = WM8731_FORMATS,},
462 .prepare = wm8731_pcm_prepare,
463 .hw_params = wm8731_hw_params,
464 .shutdown = wm8731_shutdown,
467 .digital_mute = wm8731_mute,
468 .set_sysclk = wm8731_set_dai_sysclk,
469 .set_fmt = wm8731_set_dai_fmt,
472 EXPORT_SYMBOL_GPL(wm8731_dai);
474 static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
476 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
477 struct snd_soc_codec *codec = socdev->codec;
479 wm8731_write(codec, WM8731_ACTIVE, 0x0);
480 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
484 static int wm8731_resume(struct platform_device *pdev)
486 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
487 struct snd_soc_codec *codec = socdev->codec;
490 u16 *cache = codec->reg_cache;
492 /* Sync reg_cache with the hardware */
493 for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
494 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
495 data[1] = cache[i] & 0x00ff;
496 codec->hw_write(codec->control_data, data, 2);
498 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
499 wm8731_set_bias_level(codec, codec->suspend_bias_level);
504 * initialise the WM8731 driver
505 * register the mixer and dsp interfaces with the kernel
507 static int wm8731_init(struct snd_soc_device *socdev)
509 struct snd_soc_codec *codec = socdev->codec;
512 codec->name = "WM8731";
513 codec->owner = THIS_MODULE;
514 codec->read = wm8731_read_reg_cache;
515 codec->write = wm8731_write;
516 codec->set_bias_level = wm8731_set_bias_level;
517 codec->dai = &wm8731_dai;
519 codec->reg_cache_size = ARRAY_SIZE(wm8731_reg);
520 codec->reg_cache = kmemdup(wm8731_reg, sizeof(wm8731_reg), GFP_KERNEL);
521 if (codec->reg_cache == NULL)
527 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
529 printk(KERN_ERR "wm8731: failed to create pcms\n");
533 /* power on device */
534 wm8731_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
536 /* set the update bits */
537 reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
538 wm8731_write(codec, WM8731_LOUT1V, reg & ~0x0100);
539 reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
540 wm8731_write(codec, WM8731_ROUT1V, reg & ~0x0100);
541 reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
542 wm8731_write(codec, WM8731_LINVOL, reg & ~0x0100);
543 reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
544 wm8731_write(codec, WM8731_RINVOL, reg & ~0x0100);
546 wm8731_add_controls(codec);
547 wm8731_add_widgets(codec);
548 ret = snd_soc_register_card(socdev);
550 printk(KERN_ERR "wm8731: failed to register card\n");
557 snd_soc_free_pcms(socdev);
558 snd_soc_dapm_free(socdev);
560 kfree(codec->reg_cache);
564 static struct snd_soc_device *wm8731_socdev;
566 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
569 * WM8731 2 wire address is determined by GPIO5
570 * state during powerup.
575 static int wm8731_i2c_probe(struct i2c_client *i2c,
576 const struct i2c_device_id *id)
578 struct snd_soc_device *socdev = wm8731_socdev;
579 struct snd_soc_codec *codec = socdev->codec;
582 i2c_set_clientdata(i2c, codec);
583 codec->control_data = i2c;
585 ret = wm8731_init(socdev);
587 pr_err("failed to initialise WM8731\n");
592 static int wm8731_i2c_remove(struct i2c_client *client)
594 struct snd_soc_codec *codec = i2c_get_clientdata(client);
595 kfree(codec->reg_cache);
599 static const struct i2c_device_id wm8731_i2c_id[] = {
603 MODULE_DEVICE_TABLE(i2c, wm8731_i2c_id);
605 static struct i2c_driver wm8731_i2c_driver = {
607 .name = "WM8731 I2C Codec",
608 .owner = THIS_MODULE,
610 .probe = wm8731_i2c_probe,
611 .remove = wm8731_i2c_remove,
612 .id_table = wm8731_i2c_id,
615 static int wm8731_add_i2c_device(struct platform_device *pdev,
616 const struct wm8731_setup_data *setup)
618 struct i2c_board_info info;
619 struct i2c_adapter *adapter;
620 struct i2c_client *client;
623 ret = i2c_add_driver(&wm8731_i2c_driver);
625 dev_err(&pdev->dev, "can't add i2c driver\n");
629 memset(&info, 0, sizeof(struct i2c_board_info));
630 info.addr = setup->i2c_address;
631 strlcpy(info.type, "wm8731", I2C_NAME_SIZE);
633 adapter = i2c_get_adapter(setup->i2c_bus);
635 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
640 client = i2c_new_device(adapter, &info);
641 i2c_put_adapter(adapter);
643 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
644 (unsigned int)info.addr);
651 i2c_del_driver(&wm8731_i2c_driver);
656 #if defined(CONFIG_SPI_MASTER)
657 static int __devinit wm8731_spi_probe(struct spi_device *spi)
659 struct snd_soc_device *socdev = wm8731_socdev;
660 struct snd_soc_codec *codec = socdev->codec;
663 codec->control_data = spi;
665 ret = wm8731_init(socdev);
667 dev_err(&spi->dev, "failed to initialise WM8731\n");
672 static int __devexit wm8731_spi_remove(struct spi_device *spi)
677 static struct spi_driver wm8731_spi_driver = {
680 .bus = &spi_bus_type,
681 .owner = THIS_MODULE,
683 .probe = wm8731_spi_probe,
684 .remove = __devexit_p(wm8731_spi_remove),
687 static int wm8731_spi_write(struct spi_device *spi, const char *data, int len)
689 struct spi_transfer t;
690 struct spi_message m;
699 spi_message_init(&m);
700 memset(&t, 0, (sizeof t));
705 spi_message_add_tail(&t, &m);
710 #endif /* CONFIG_SPI_MASTER */
712 static int wm8731_probe(struct platform_device *pdev)
714 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
715 struct wm8731_setup_data *setup;
716 struct snd_soc_codec *codec;
717 struct wm8731_priv *wm8731;
720 pr_info("WM8731 Audio Codec %s", WM8731_VERSION);
722 setup = socdev->codec_data;
723 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
727 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
728 if (wm8731 == NULL) {
733 codec->private_data = wm8731;
734 socdev->codec = codec;
735 mutex_init(&codec->mutex);
736 INIT_LIST_HEAD(&codec->dapm_widgets);
737 INIT_LIST_HEAD(&codec->dapm_paths);
739 wm8731_socdev = socdev;
742 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
743 if (setup->i2c_address) {
744 codec->hw_write = (hw_write_t)i2c_master_send;
745 ret = wm8731_add_i2c_device(pdev, setup);
748 #if defined(CONFIG_SPI_MASTER)
750 codec->hw_write = (hw_write_t)wm8731_spi_write;
751 ret = spi_register_driver(&wm8731_spi_driver);
753 printk(KERN_ERR "can't add spi driver");
758 kfree(codec->private_data);
764 /* power down chip */
765 static int wm8731_remove(struct platform_device *pdev)
767 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
768 struct snd_soc_codec *codec = socdev->codec;
770 if (codec->control_data)
771 wm8731_set_bias_level(codec, SND_SOC_BIAS_OFF);
773 snd_soc_free_pcms(socdev);
774 snd_soc_dapm_free(socdev);
775 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
776 i2c_unregister_device(codec->control_data);
777 i2c_del_driver(&wm8731_i2c_driver);
779 #if defined(CONFIG_SPI_MASTER)
780 spi_unregister_driver(&wm8731_spi_driver);
782 kfree(codec->private_data);
788 struct snd_soc_codec_device soc_codec_dev_wm8731 = {
789 .probe = wm8731_probe,
790 .remove = wm8731_remove,
791 .suspend = wm8731_suspend,
792 .resume = wm8731_resume,
794 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
796 MODULE_DESCRIPTION("ASoC WM8731 driver");
797 MODULE_AUTHOR("Richard Purdie");
798 MODULE_LICENSE("GPL");