2 * wm9705.c -- ALSA Soc WM9705 codec support
4 * Copyright 2008 Ian Molton <spyro@f2s.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; Version 2 of the License only.
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/device.h>
16 #include <sound/core.h>
17 #include <sound/pcm.h>
18 #include <sound/ac97_codec.h>
19 #include <sound/initval.h>
20 #include <sound/soc.h>
21 #include <sound/soc-dapm.h>
24 * WM9705 register cache
26 static const u16 wm9705_reg[] = {
27 0x6150, 0x8000, 0x8000, 0x8000, /* 0x0 */
28 0x0000, 0x8000, 0x8008, 0x8008, /* 0x8 */
29 0x8808, 0x8808, 0x8808, 0x8808, /* 0x10 */
30 0x8808, 0x0000, 0x8000, 0x0000, /* 0x18 */
31 0x0000, 0x0000, 0x0000, 0x000f, /* 0x20 */
32 0x0605, 0x0000, 0xbb80, 0x0000, /* 0x28 */
33 0x0000, 0xbb80, 0x0000, 0x0000, /* 0x30 */
34 0x0000, 0x2000, 0x0000, 0x0000, /* 0x38 */
35 0x0000, 0x0000, 0x0000, 0x0000, /* 0x40 */
36 0x0000, 0x0000, 0x0000, 0x0000, /* 0x48 */
37 0x0000, 0x0000, 0x0000, 0x0000, /* 0x50 */
38 0x0000, 0x0000, 0x0000, 0x0000, /* 0x58 */
39 0x0000, 0x0000, 0x0000, 0x0000, /* 0x60 */
40 0x0000, 0x0000, 0x0000, 0x0000, /* 0x68 */
41 0x0000, 0x0808, 0x0000, 0x0006, /* 0x70 */
42 0x0000, 0x0000, 0x574d, 0x4c05, /* 0x78 */
45 static const struct snd_kcontrol_new wm9705_snd_ac97_controls[] = {
46 SOC_DOUBLE("Master Playback Volume", AC97_MASTER, 8, 0, 31, 1),
47 SOC_SINGLE("Master Playback Switch", AC97_MASTER, 15, 1, 1),
48 SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
49 SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 1, 1),
50 SOC_DOUBLE("PCM Playback Volume", AC97_PCM, 8, 0, 31, 1),
51 SOC_SINGLE("PCM Playback Switch", AC97_PCM, 15, 1, 1),
52 SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1),
53 SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
54 SOC_SINGLE("PCBeep Playback Volume", AC97_PC_BEEP, 1, 15, 1),
55 SOC_SINGLE("Phone Playback Volume", AC97_PHONE, 0, 31, 1),
56 SOC_DOUBLE("Line Playback Volume", AC97_LINE, 8, 0, 31, 1),
57 SOC_DOUBLE("CD Playback Volume", AC97_CD, 8, 0, 31, 1),
58 SOC_SINGLE("Mic Playback Volume", AC97_MIC, 0, 31, 1),
59 SOC_SINGLE("Mic 20dB Boost Switch", AC97_MIC, 6, 1, 0),
60 SOC_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0),
61 SOC_SINGLE("Capture Switch", AC97_REC_GAIN, 15, 1, 1),
64 static const char *wm9705_mic[] = {"Mic 1", "Mic 2"};
65 static const char *wm9705_rec_sel[] = {"Mic", "CD", "NC", "NC",
66 "Line", "Stereo Mix", "Mono Mix", "Phone"};
68 static const struct soc_enum wm9705_enum_mic =
69 SOC_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 8, 2, wm9705_mic);
70 static const struct soc_enum wm9705_enum_rec_l =
71 SOC_ENUM_SINGLE(AC97_REC_SEL, 8, 8, wm9705_rec_sel);
72 static const struct soc_enum wm9705_enum_rec_r =
73 SOC_ENUM_SINGLE(AC97_REC_SEL, 0, 8, wm9705_rec_sel);
76 static const struct snd_kcontrol_new wm9705_hp_mixer_controls[] = {
77 SOC_DAPM_SINGLE("PCBeep Playback Switch", AC97_PC_BEEP, 15, 1, 1),
78 SOC_DAPM_SINGLE("CD Playback Switch", AC97_CD, 15, 1, 1),
79 SOC_DAPM_SINGLE("Mic Playback Switch", AC97_MIC, 15, 1, 1),
80 SOC_DAPM_SINGLE("Phone Playback Switch", AC97_PHONE, 15, 1, 1),
81 SOC_DAPM_SINGLE("Line Playback Switch", AC97_LINE, 15, 1, 1),
85 static const struct snd_kcontrol_new wm9705_mic_src_controls =
86 SOC_DAPM_ENUM("Route", wm9705_enum_mic);
89 static const struct snd_kcontrol_new wm9705_capture_selectl_controls =
90 SOC_DAPM_ENUM("Route", wm9705_enum_rec_l);
91 static const struct snd_kcontrol_new wm9705_capture_selectr_controls =
92 SOC_DAPM_ENUM("Route", wm9705_enum_rec_r);
95 static const struct snd_soc_dapm_widget wm9705_dapm_widgets[] = {
96 SND_SOC_DAPM_MUX("Mic Source", SND_SOC_NOPM, 0, 0,
97 &wm9705_mic_src_controls),
98 SND_SOC_DAPM_MUX("Left Capture Source", SND_SOC_NOPM, 0, 0,
99 &wm9705_capture_selectl_controls),
100 SND_SOC_DAPM_MUX("Right Capture Source", SND_SOC_NOPM, 0, 0,
101 &wm9705_capture_selectr_controls),
102 SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback",
104 SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback",
106 SND_SOC_DAPM_MIXER_NAMED_CTL("HP Mixer", SND_SOC_NOPM, 0, 0,
107 &wm9705_hp_mixer_controls[0],
108 ARRAY_SIZE(wm9705_hp_mixer_controls)),
109 SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
110 SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture", SND_SOC_NOPM, 0, 0),
111 SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture", SND_SOC_NOPM, 0, 0),
112 SND_SOC_DAPM_PGA("Headphone PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
113 SND_SOC_DAPM_PGA("Speaker PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
114 SND_SOC_DAPM_PGA("Line PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
115 SND_SOC_DAPM_PGA("Line out PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
116 SND_SOC_DAPM_PGA("Mono PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
117 SND_SOC_DAPM_PGA("Phone PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
118 SND_SOC_DAPM_PGA("Mic PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
119 SND_SOC_DAPM_PGA("PCBEEP PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
120 SND_SOC_DAPM_PGA("CD PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
121 SND_SOC_DAPM_PGA("ADC PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
122 SND_SOC_DAPM_OUTPUT("HPOUTL"),
123 SND_SOC_DAPM_OUTPUT("HPOUTR"),
124 SND_SOC_DAPM_OUTPUT("LOUT"),
125 SND_SOC_DAPM_OUTPUT("ROUT"),
126 SND_SOC_DAPM_OUTPUT("MONOOUT"),
127 SND_SOC_DAPM_INPUT("PHONE"),
128 SND_SOC_DAPM_INPUT("LINEINL"),
129 SND_SOC_DAPM_INPUT("LINEINR"),
130 SND_SOC_DAPM_INPUT("CDINL"),
131 SND_SOC_DAPM_INPUT("CDINR"),
132 SND_SOC_DAPM_INPUT("PCBEEP"),
133 SND_SOC_DAPM_INPUT("MIC1"),
134 SND_SOC_DAPM_INPUT("MIC2"),
138 * WM9705 has no switches to disable the route from the inputs to the HP mixer
139 * so in order to prevent active inputs from forcing the audio outputs to be
140 * constantly enabled, we use the mutes on those inputs to simulate such
143 static const struct snd_soc_dapm_route audio_map[] = {
145 {"HP Mixer", "PCBeep Playback Switch", "PCBEEP PGA"},
146 {"HP Mixer", "CD Playback Switch", "CD PGA"},
147 {"HP Mixer", "Mic Playback Switch", "Mic PGA"},
148 {"HP Mixer", "Phone Playback Switch", "Phone PGA"},
149 {"HP Mixer", "Line Playback Switch", "Line PGA"},
150 {"HP Mixer", NULL, "Left DAC"},
151 {"HP Mixer", NULL, "Right DAC"},
154 {"Mono Mixer", NULL, "HP Mixer"},
157 {"Headphone PGA", NULL, "HP Mixer"},
158 {"HPOUTL", NULL, "Headphone PGA"},
159 {"HPOUTR", NULL, "Headphone PGA"},
160 {"Line out PGA", NULL, "HP Mixer"},
161 {"LOUT", NULL, "Line out PGA"},
162 {"ROUT", NULL, "Line out PGA"},
163 {"Mono PGA", NULL, "Mono Mixer"},
164 {"MONOOUT", NULL, "Mono PGA"},
167 {"CD PGA", NULL, "CDINL"},
168 {"CD PGA", NULL, "CDINR"},
169 {"Line PGA", NULL, "LINEINL"},
170 {"Line PGA", NULL, "LINEINR"},
171 {"Phone PGA", NULL, "PHONE"},
172 {"Mic Source", "Mic 1", "MIC1"},
173 {"Mic Source", "Mic 2", "MIC2"},
174 {"Mic PGA", NULL, "Mic Source"},
175 {"PCBEEP PGA", NULL, "PCBEEP"},
177 /* Left capture selector */
178 {"Left Capture Source", "Mic", "Mic Source"},
179 {"Left Capture Source", "CD", "CDINL"},
180 {"Left Capture Source", "Line", "LINEINL"},
181 {"Left Capture Source", "Stereo Mix", "HP Mixer"},
182 {"Left Capture Source", "Mono Mix", "HP Mixer"},
183 {"Left Capture Source", "Phone", "PHONE"},
185 /* Right capture source */
186 {"Right Capture Source", "Mic", "Mic Source"},
187 {"Right Capture Source", "CD", "CDINR"},
188 {"Right Capture Source", "Line", "LINEINR"},
189 {"Right Capture Source", "Stereo Mix", "HP Mixer"},
190 {"Right Capture Source", "Mono Mix", "HP Mixer"},
191 {"Right Capture Source", "Phone", "PHONE"},
193 {"ADC PGA", NULL, "Left Capture Source"},
194 {"ADC PGA", NULL, "Right Capture Source"},
197 {"Left ADC", NULL, "ADC PGA"},
198 {"Right ADC", NULL, "ADC PGA"},
201 static int wm9705_add_widgets(struct snd_soc_codec *codec)
203 snd_soc_dapm_new_controls(codec, wm9705_dapm_widgets,
204 ARRAY_SIZE(wm9705_dapm_widgets));
205 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
206 snd_soc_dapm_new_widgets(codec);
211 /* We use a register cache to enhance read performance. */
212 static unsigned int ac97_read(struct snd_soc_codec *codec, unsigned int reg)
214 u16 *cache = codec->reg_cache;
218 case AC97_VENDOR_ID1:
219 case AC97_VENDOR_ID2:
220 return soc_ac97_ops.read(codec->ac97, reg);
224 if (reg >= (ARRAY_SIZE(wm9705_reg)))
231 static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
234 u16 *cache = codec->reg_cache;
236 soc_ac97_ops.write(codec->ac97, reg, val);
238 if (reg < (ARRAY_SIZE(wm9705_reg)))
244 static int ac97_prepare(struct snd_pcm_substream *substream,
245 struct snd_soc_dai *dai)
247 struct snd_pcm_runtime *runtime = substream->runtime;
248 struct snd_soc_pcm_runtime *rtd = substream->private_data;
249 struct snd_soc_device *socdev = rtd->socdev;
250 struct snd_soc_codec *codec = socdev->codec;
254 vra = ac97_read(codec, AC97_EXTENDED_STATUS);
255 ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
257 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
258 reg = AC97_PCM_FRONT_DAC_RATE;
260 reg = AC97_PCM_LR_ADC_RATE;
262 return ac97_write(codec, reg, runtime->rate);
265 #define WM9705_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | \
266 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
267 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
268 SNDRV_PCM_RATE_48000)
270 struct snd_soc_dai wm9705_dai[] = {
275 .stream_name = "HiFi Playback",
278 .rates = WM9705_AC97_RATES,
279 .formats = SNDRV_PCM_FMTBIT_S16_LE,
282 .stream_name = "HiFi Capture",
285 .rates = WM9705_AC97_RATES,
286 .formats = SNDRV_PCM_FMTBIT_S16_LE,
289 .prepare = ac97_prepare,
295 .stream_name = "Aux Playback",
298 .rates = WM9705_AC97_RATES,
299 .formats = SNDRV_PCM_FMTBIT_S16_LE,
303 EXPORT_SYMBOL_GPL(wm9705_dai);
305 static int wm9705_reset(struct snd_soc_codec *codec)
307 if (soc_ac97_ops.reset) {
308 soc_ac97_ops.reset(codec->ac97);
309 if (ac97_read(codec, 0) == wm9705_reg[0])
310 return 0; /* Success */
316 static int wm9705_soc_probe(struct platform_device *pdev)
318 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
319 struct snd_soc_codec *codec;
322 printk(KERN_INFO "WM9705 SoC Audio Codec\n");
324 socdev->codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
325 if (socdev->codec == NULL)
327 codec = socdev->codec;
328 mutex_init(&codec->mutex);
330 codec->reg_cache = kmemdup(wm9705_reg, sizeof(wm9705_reg), GFP_KERNEL);
331 if (codec->reg_cache == NULL) {
335 codec->reg_cache_size = sizeof(wm9705_reg);
336 codec->reg_cache_step = 2;
338 codec->name = "WM9705";
339 codec->owner = THIS_MODULE;
340 codec->dai = wm9705_dai;
341 codec->num_dai = ARRAY_SIZE(wm9705_dai);
342 codec->write = ac97_write;
343 codec->read = ac97_read;
344 INIT_LIST_HEAD(&codec->dapm_widgets);
345 INIT_LIST_HEAD(&codec->dapm_paths);
347 ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
349 printk(KERN_ERR "wm9705: failed to register AC97 codec\n");
354 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
358 ret = wm9705_reset(codec);
362 snd_soc_add_controls(codec, wm9705_snd_ac97_controls,
363 ARRAY_SIZE(wm9705_snd_ac97_controls));
364 wm9705_add_widgets(codec);
366 ret = snd_soc_init_card(socdev);
368 printk(KERN_ERR "wm9705: failed to register card\n");
375 snd_soc_free_pcms(socdev);
377 snd_soc_free_ac97_codec(codec);
379 kfree(codec->reg_cache);
381 kfree(socdev->codec);
382 socdev->codec = NULL;
386 static int wm9705_soc_remove(struct platform_device *pdev)
388 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
389 struct snd_soc_codec *codec = socdev->codec;
394 snd_soc_dapm_free(socdev);
395 snd_soc_free_pcms(socdev);
396 snd_soc_free_ac97_codec(codec);
397 kfree(codec->reg_cache);
402 struct snd_soc_codec_device soc_codec_dev_wm9705 = {
403 .probe = wm9705_soc_probe,
404 .remove = wm9705_soc_remove,
406 EXPORT_SYMBOL_GPL(soc_codec_dev_wm9705);
408 MODULE_DESCRIPTION("ASoC WM9705 driver");
409 MODULE_AUTHOR("Ian Molton");
410 MODULE_LICENSE("GPL v2");