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 <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dapm.h>
27 #include <sound/initval.h>
31 #define AUDIO_NAME "wm8731"
32 #define WM8731_VERSION "0.13"
38 #define WM8731_DEBUG 0
41 #define dbg(format, arg...) \
42 printk(KERN_DEBUG AUDIO_NAME ": " format "\n" , ## arg)
44 #define dbg(format, arg...) do {} while (0)
46 #define err(format, arg...) \
47 printk(KERN_ERR AUDIO_NAME ": " format "\n" , ## arg)
48 #define info(format, arg...) \
49 printk(KERN_INFO AUDIO_NAME ": " format "\n" , ## arg)
50 #define warn(format, arg...) \
51 printk(KERN_WARNING AUDIO_NAME ": " format "\n" , ## arg)
53 struct snd_soc_codec_device soc_codec_dev_wm8731;
55 /* codec private data */
61 * wm8731 register cache
62 * We can't read the WM8731 register space when we are
63 * using 2 wire for device control, so we cache them instead.
64 * There is no point in caching the reset register
66 static const u16 wm8731_reg[WM8731_CACHEREGNUM] = {
67 0x0097, 0x0097, 0x0079, 0x0079,
68 0x000a, 0x0008, 0x009f, 0x000a,
73 * read wm8731 register cache
75 static inline unsigned int wm8731_read_reg_cache(struct snd_soc_codec *codec,
78 u16 *cache = codec->reg_cache;
79 if (reg == WM8731_RESET)
81 if (reg >= WM8731_CACHEREGNUM)
87 * write wm8731 register cache
89 static inline void wm8731_write_reg_cache(struct snd_soc_codec *codec,
90 u16 reg, unsigned int value)
92 u16 *cache = codec->reg_cache;
93 if (reg >= WM8731_CACHEREGNUM)
99 * write to the WM8731 register space
101 static int wm8731_write(struct snd_soc_codec *codec, unsigned int reg,
107 * D15..D9 WM8731 register offset
108 * D8...D0 register data
110 data[0] = (reg << 1) | ((value >> 8) & 0x0001);
111 data[1] = value & 0x00ff;
113 wm8731_write_reg_cache (codec, reg, value);
114 if (codec->hw_write(codec->control_data, data, 2) == 2)
120 #define wm8731_reset(c) wm8731_write(c, WM8731_RESET, 0)
122 static const char *wm8731_input_select[] = {"Line In", "Mic"};
123 static const char *wm8731_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
125 static const struct soc_enum wm8731_enum[] = {
126 SOC_ENUM_SINGLE(WM8731_APANA, 2, 2, wm8731_input_select),
127 SOC_ENUM_SINGLE(WM8731_APDIGI, 1, 4, wm8731_deemph),
130 static const struct snd_kcontrol_new wm8731_snd_controls[] = {
132 SOC_DOUBLE_R("Master Playback Volume", WM8731_LOUT1V, WM8731_ROUT1V,
134 SOC_DOUBLE_R("Master Playback ZC Switch", WM8731_LOUT1V, WM8731_ROUT1V,
137 SOC_DOUBLE_R("Capture Volume", WM8731_LINVOL, WM8731_RINVOL, 0, 31, 0),
138 SOC_DOUBLE_R("Line Capture Switch", WM8731_LINVOL, WM8731_RINVOL, 7, 1, 1),
140 SOC_SINGLE("Mic Boost (+20dB)", WM8731_APANA, 0, 1, 0),
141 SOC_SINGLE("Capture Mic Switch", WM8731_APANA, 1, 1, 1),
143 SOC_SINGLE("Sidetone Playback Volume", WM8731_APANA, 6, 3, 1),
145 SOC_SINGLE("ADC High Pass Filter Switch", WM8731_APDIGI, 0, 1, 1),
146 SOC_SINGLE("Store DC Offset Switch", WM8731_APDIGI, 4, 1, 0),
148 SOC_ENUM("Playback De-emphasis", wm8731_enum[1]),
151 /* add non dapm controls */
152 static int wm8731_add_controls(struct snd_soc_codec *codec)
156 for (i = 0; i < ARRAY_SIZE(wm8731_snd_controls); i++) {
157 if ((err = snd_ctl_add(codec->card,
158 snd_soc_cnew(&wm8731_snd_controls[i],codec, NULL))) < 0)
166 static const struct snd_kcontrol_new wm8731_output_mixer_controls[] = {
167 SOC_DAPM_SINGLE("Line Bypass Switch", WM8731_APANA, 3, 1, 0),
168 SOC_DAPM_SINGLE("Mic Sidetone Switch", WM8731_APANA, 5, 1, 0),
169 SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
173 static const struct snd_kcontrol_new wm8731_input_mux_controls =
174 SOC_DAPM_ENUM("Input Select", wm8731_enum[0]);
176 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
177 SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
178 &wm8731_output_mixer_controls[0],
179 ARRAY_SIZE(wm8731_output_mixer_controls)),
180 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
181 SND_SOC_DAPM_OUTPUT("LOUT"),
182 SND_SOC_DAPM_OUTPUT("LHPOUT"),
183 SND_SOC_DAPM_OUTPUT("ROUT"),
184 SND_SOC_DAPM_OUTPUT("RHPOUT"),
185 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
186 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
187 SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
188 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
189 SND_SOC_DAPM_INPUT("MICIN"),
190 SND_SOC_DAPM_INPUT("RLINEIN"),
191 SND_SOC_DAPM_INPUT("LLINEIN"),
194 static const char *intercon[][3] = {
196 {"Output Mixer", "Line Bypass Switch", "Line Input"},
197 {"Output Mixer", "HiFi Playback Switch", "DAC"},
198 {"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
201 {"RHPOUT", NULL, "Output Mixer"},
202 {"ROUT", NULL, "Output Mixer"},
203 {"LHPOUT", NULL, "Output Mixer"},
204 {"LOUT", NULL, "Output Mixer"},
207 {"Input Mux", "Line In", "Line Input"},
208 {"Input Mux", "Mic", "Mic Bias"},
209 {"ADC", NULL, "Input Mux"},
212 {"Line Input", NULL, "LLINEIN"},
213 {"Line Input", NULL, "RLINEIN"},
214 {"Mic Bias", NULL, "MICIN"},
220 static int wm8731_add_widgets(struct snd_soc_codec *codec)
224 for(i = 0; i < ARRAY_SIZE(wm8731_dapm_widgets); i++) {
225 snd_soc_dapm_new_control(codec, &wm8731_dapm_widgets[i]);
228 /* set up audio path interconnects */
229 for(i = 0; intercon[i][0] != NULL; i++) {
230 snd_soc_dapm_connect_input(codec, intercon[i][0],
231 intercon[i][1], intercon[i][2]);
234 snd_soc_dapm_new_widgets(codec);
247 /* codec mclk clock divider coefficients */
248 static const struct _coeff_div coeff_div[] = {
250 {12288000, 48000, 256, 0x0, 0x0, 0x0},
251 {18432000, 48000, 384, 0x0, 0x1, 0x0},
252 {12000000, 48000, 250, 0x0, 0x0, 0x1},
255 {12288000, 32000, 384, 0x6, 0x0, 0x0},
256 {18432000, 32000, 576, 0x6, 0x1, 0x0},
257 {12000000, 32000, 375, 0x6, 0x0, 0x1},
260 {12288000, 8000, 1536, 0x3, 0x0, 0x0},
261 {18432000, 8000, 2304, 0x3, 0x1, 0x0},
262 {11289600, 8000, 1408, 0xb, 0x0, 0x0},
263 {16934400, 8000, 2112, 0xb, 0x1, 0x0},
264 {12000000, 8000, 1500, 0x3, 0x0, 0x1},
267 {12288000, 96000, 128, 0x7, 0x0, 0x0},
268 {18432000, 96000, 192, 0x7, 0x1, 0x0},
269 {12000000, 96000, 125, 0x7, 0x0, 0x1},
272 {11289600, 44100, 256, 0x8, 0x0, 0x0},
273 {16934400, 44100, 384, 0x8, 0x1, 0x0},
274 {12000000, 44100, 272, 0x8, 0x1, 0x1},
277 {11289600, 88200, 128, 0xf, 0x0, 0x0},
278 {16934400, 88200, 192, 0xf, 0x1, 0x0},
279 {12000000, 88200, 136, 0xf, 0x1, 0x1},
282 static inline int get_coeff(int mclk, int rate)
286 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
287 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
293 static int wm8731_hw_params(struct snd_pcm_substream *substream,
294 struct snd_pcm_hw_params *params)
296 struct snd_soc_pcm_runtime *rtd = substream->private_data;
297 struct snd_soc_device *socdev = rtd->socdev;
298 struct snd_soc_codec *codec = socdev->codec;
299 struct wm8731_priv *wm8731 = codec->private_data;
300 u16 iface = wm8731_read_reg_cache(codec, WM8731_IFACE) & 0xfff3;
301 int i = get_coeff(wm8731->sysclk, params_rate(params));
302 u16 srate = (coeff_div[i].sr << 2) |
303 (coeff_div[i].bosr << 1) | coeff_div[i].usb;
305 wm8731_write(codec, WM8731_SRATE, srate);
308 switch (params_format(params)) {
309 case SNDRV_PCM_FORMAT_S16_LE:
311 case SNDRV_PCM_FORMAT_S20_3LE:
314 case SNDRV_PCM_FORMAT_S24_LE:
319 wm8731_write(codec, WM8731_IFACE, iface);
323 static int wm8731_pcm_prepare(struct snd_pcm_substream *substream)
325 struct snd_soc_pcm_runtime *rtd = substream->private_data;
326 struct snd_soc_device *socdev = rtd->socdev;
327 struct snd_soc_codec *codec = socdev->codec;
330 wm8731_write(codec, WM8731_ACTIVE, 0x0001);
335 static void wm8731_shutdown(struct snd_pcm_substream *substream)
337 struct snd_soc_pcm_runtime *rtd = substream->private_data;
338 struct snd_soc_device *socdev = rtd->socdev;
339 struct snd_soc_codec *codec = socdev->codec;
342 if (!codec->active) {
344 wm8731_write(codec, WM8731_ACTIVE, 0x0);
348 static int wm8731_mute(struct snd_soc_codec_dai *dai, int mute)
350 struct snd_soc_codec *codec = dai->codec;
351 u16 mute_reg = wm8731_read_reg_cache(codec, WM8731_APDIGI) & 0xfff7;
354 wm8731_write(codec, WM8731_APDIGI, mute_reg | 0x8);
356 wm8731_write(codec, WM8731_APDIGI, mute_reg);
360 static int wm8731_set_dai_sysclk(struct snd_soc_codec_dai *codec_dai,
361 int clk_id, unsigned int freq, int dir)
363 struct snd_soc_codec *codec = codec_dai->codec;
364 struct wm8731_priv *wm8731 = codec->private_data;
372 wm8731->sysclk = freq;
379 static int wm8731_set_dai_fmt(struct snd_soc_codec_dai *codec_dai,
382 struct snd_soc_codec *codec = codec_dai->codec;
385 /* set master/slave audio interface */
386 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
387 case SND_SOC_DAIFMT_CBM_CFM:
390 case SND_SOC_DAIFMT_CBS_CFS:
396 /* interface format */
397 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
398 case SND_SOC_DAIFMT_I2S:
401 case SND_SOC_DAIFMT_RIGHT_J:
403 case SND_SOC_DAIFMT_LEFT_J:
406 case SND_SOC_DAIFMT_DSP_A:
409 case SND_SOC_DAIFMT_DSP_B:
416 /* clock inversion */
417 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
418 case SND_SOC_DAIFMT_NB_NF:
420 case SND_SOC_DAIFMT_IB_IF:
423 case SND_SOC_DAIFMT_IB_NF:
426 case SND_SOC_DAIFMT_NB_IF:
434 wm8731_write(codec, WM8731_IFACE, iface);
438 static int wm8731_dapm_event(struct snd_soc_codec *codec, int event)
440 u16 reg = wm8731_read_reg_cache(codec, WM8731_PWR) & 0xff7f;
443 case SNDRV_CTL_POWER_D0: /* full On */
444 /* vref/mid, osc on, dac unmute */
445 wm8731_write(codec, WM8731_PWR, reg);
447 case SNDRV_CTL_POWER_D1: /* partial On */
448 case SNDRV_CTL_POWER_D2: /* partial On */
450 case SNDRV_CTL_POWER_D3hot: /* Off, with power */
451 /* everything off except vref/vmid, */
452 wm8731_write(codec, WM8731_PWR, reg | 0x0040);
454 case SNDRV_CTL_POWER_D3cold: /* Off, without power */
455 /* everything off, dac mute, inactive */
456 wm8731_write(codec, WM8731_ACTIVE, 0x0);
457 wm8731_write(codec, WM8731_PWR, 0xffff);
460 codec->dapm_state = event;
464 #define WM8731_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
465 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
466 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
467 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
468 SNDRV_PCM_RATE_96000)
470 #define WM8731_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
471 SNDRV_PCM_FMTBIT_S24_LE)
473 struct snd_soc_codec_dai wm8731_dai = {
476 .stream_name = "Playback",
479 .rates = WM8731_RATES,
480 .formats = WM8731_FORMATS,},
482 .stream_name = "Capture",
485 .rates = WM8731_RATES,
486 .formats = WM8731_FORMATS,},
488 .prepare = wm8731_pcm_prepare,
489 .hw_params = wm8731_hw_params,
490 .shutdown = wm8731_shutdown,
493 .digital_mute = wm8731_mute,
494 .set_sysclk = wm8731_set_dai_sysclk,
495 .set_fmt = wm8731_set_dai_fmt,
498 EXPORT_SYMBOL_GPL(wm8731_dai);
500 static int wm8731_suspend(struct platform_device *pdev, pm_message_t state)
502 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
503 struct snd_soc_codec *codec = socdev->codec;
505 wm8731_write(codec, WM8731_ACTIVE, 0x0);
506 wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
510 static int wm8731_resume(struct platform_device *pdev)
512 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
513 struct snd_soc_codec *codec = socdev->codec;
516 u16 *cache = codec->reg_cache;
518 /* Sync reg_cache with the hardware */
519 for (i = 0; i < ARRAY_SIZE(wm8731_reg); i++) {
520 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
521 data[1] = cache[i] & 0x00ff;
522 codec->hw_write(codec->control_data, data, 2);
524 wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
525 wm8731_dapm_event(codec, codec->suspend_dapm_state);
530 * initialise the WM8731 driver
531 * register the mixer and dsp interfaces with the kernel
533 static int wm8731_init(struct snd_soc_device *socdev)
535 struct snd_soc_codec *codec = socdev->codec;
538 codec->name = "WM8731";
539 codec->owner = THIS_MODULE;
540 codec->read = wm8731_read_reg_cache;
541 codec->write = wm8731_write;
542 codec->dapm_event = wm8731_dapm_event;
543 codec->dai = &wm8731_dai;
545 codec->reg_cache_size = sizeof(wm8731_reg);
546 codec->reg_cache = kmemdup(wm8731_reg, sizeof(wm8731_reg), GFP_KERNEL);
547 if (codec->reg_cache == NULL)
553 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
555 printk(KERN_ERR "wm8731: failed to create pcms\n");
559 /* power on device */
560 wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
562 /* set the update bits */
563 reg = wm8731_read_reg_cache(codec, WM8731_LOUT1V);
564 wm8731_write(codec, WM8731_LOUT1V, reg & ~0x0100);
565 reg = wm8731_read_reg_cache(codec, WM8731_ROUT1V);
566 wm8731_write(codec, WM8731_ROUT1V, reg & ~0x0100);
567 reg = wm8731_read_reg_cache(codec, WM8731_LINVOL);
568 wm8731_write(codec, WM8731_LINVOL, reg & ~0x0100);
569 reg = wm8731_read_reg_cache(codec, WM8731_RINVOL);
570 wm8731_write(codec, WM8731_RINVOL, reg & ~0x0100);
572 wm8731_add_controls(codec);
573 wm8731_add_widgets(codec);
574 ret = snd_soc_register_card(socdev);
576 printk(KERN_ERR "wm8731: failed to register card\n");
583 snd_soc_free_pcms(socdev);
584 snd_soc_dapm_free(socdev);
586 kfree(codec->reg_cache);
590 static struct snd_soc_device *wm8731_socdev;
592 #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
595 * WM8731 2 wire address is determined by GPIO5
596 * state during powerup.
600 static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END };
602 /* Magic definition of all other variables and things */
605 static struct i2c_driver wm8731_i2c_driver;
606 static struct i2c_client client_template;
608 /* If the i2c layer weren't so broken, we could pass this kind of data
611 static int wm8731_codec_probe(struct i2c_adapter *adap, int addr, int kind)
613 struct snd_soc_device *socdev = wm8731_socdev;
614 struct wm8731_setup_data *setup = socdev->codec_data;
615 struct snd_soc_codec *codec = socdev->codec;
616 struct i2c_client *i2c;
619 if (addr != setup->i2c_address)
622 client_template.adapter = adap;
623 client_template.addr = addr;
625 i2c = kmemdup(&client_template, sizeof(client_template), GFP_KERNEL);
630 i2c_set_clientdata(i2c, codec);
631 codec->control_data = i2c;
633 ret = i2c_attach_client(i2c);
635 err("failed to attach codec at addr %x\n", addr);
639 ret = wm8731_init(socdev);
641 err("failed to initialise WM8731\n");
652 static int wm8731_i2c_detach(struct i2c_client *client)
654 struct snd_soc_codec* codec = i2c_get_clientdata(client);
655 i2c_detach_client(client);
656 kfree(codec->reg_cache);
661 static int wm8731_i2c_attach(struct i2c_adapter *adap)
663 return i2c_probe(adap, &addr_data, wm8731_codec_probe);
666 /* corgi i2c codec control layer */
667 static struct i2c_driver wm8731_i2c_driver = {
669 .name = "WM8731 I2C Codec",
670 .owner = THIS_MODULE,
672 .id = I2C_DRIVERID_WM8731,
673 .attach_adapter = wm8731_i2c_attach,
674 .detach_client = wm8731_i2c_detach,
678 static struct i2c_client client_template = {
680 .driver = &wm8731_i2c_driver,
684 static int wm8731_probe(struct platform_device *pdev)
686 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
687 struct wm8731_setup_data *setup;
688 struct snd_soc_codec *codec;
689 struct wm8731_priv *wm8731;
692 info("WM8731 Audio Codec %s", WM8731_VERSION);
694 setup = socdev->codec_data;
695 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
699 wm8731 = kzalloc(sizeof(struct wm8731_priv), GFP_KERNEL);
700 if (wm8731 == NULL) {
705 codec->private_data = wm8731;
706 socdev->codec = codec;
707 mutex_init(&codec->mutex);
708 INIT_LIST_HEAD(&codec->dapm_widgets);
709 INIT_LIST_HEAD(&codec->dapm_paths);
711 wm8731_socdev = socdev;
712 #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
713 if (setup->i2c_address) {
714 normal_i2c[0] = setup->i2c_address;
715 codec->hw_write = (hw_write_t)i2c_master_send;
716 ret = i2c_add_driver(&wm8731_i2c_driver);
718 printk(KERN_ERR "can't add i2c driver");
721 /* Add other interfaces here */
726 /* power down chip */
727 static int wm8731_remove(struct platform_device *pdev)
729 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
730 struct snd_soc_codec *codec = socdev->codec;
732 if (codec->control_data)
733 wm8731_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
735 snd_soc_free_pcms(socdev);
736 snd_soc_dapm_free(socdev);
737 #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
738 i2c_del_driver(&wm8731_i2c_driver);
740 kfree(codec->private_data);
746 struct snd_soc_codec_device soc_codec_dev_wm8731 = {
747 .probe = wm8731_probe,
748 .remove = wm8731_remove,
749 .suspend = wm8731_suspend,
750 .resume = wm8731_resume,
753 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8731);
755 MODULE_DESCRIPTION("ASoC WM8731 driver");
756 MODULE_AUTHOR("Richard Purdie");
757 MODULE_LICENSE("GPL");