blob: 58d120824498dbeb58a0034ab340edd9bfb807a6 [file] [log] [blame]
Ian Molton2aceefe2009-01-16 11:04:18 +00001/*
2 * wm9705.c -- ALSA Soc WM9705 codec support
3 *
4 * Copyright 2008 Ian Molton <spyro@f2s.com>
5 *
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.
9 *
10 */
11
12#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Ian Molton2aceefe2009-01-16 11:04:18 +000014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/device.h>
17#include <sound/core.h>
18#include <sound/pcm.h>
19#include <sound/ac97_codec.h>
20#include <sound/initval.h>
21#include <sound/soc.h>
22#include <sound/soc-dapm.h>
23
Mark Brown01e097d2009-01-23 15:07:45 +000024#include "wm9705.h"
25
Ian Molton2aceefe2009-01-16 11:04:18 +000026/*
27 * WM9705 register cache
28 */
29static const u16 wm9705_reg[] = {
30 0x6150, 0x8000, 0x8000, 0x8000, /* 0x0 */
31 0x0000, 0x8000, 0x8008, 0x8008, /* 0x8 */
32 0x8808, 0x8808, 0x8808, 0x8808, /* 0x10 */
33 0x8808, 0x0000, 0x8000, 0x0000, /* 0x18 */
34 0x0000, 0x0000, 0x0000, 0x000f, /* 0x20 */
35 0x0605, 0x0000, 0xbb80, 0x0000, /* 0x28 */
36 0x0000, 0xbb80, 0x0000, 0x0000, /* 0x30 */
37 0x0000, 0x2000, 0x0000, 0x0000, /* 0x38 */
38 0x0000, 0x0000, 0x0000, 0x0000, /* 0x40 */
39 0x0000, 0x0000, 0x0000, 0x0000, /* 0x48 */
40 0x0000, 0x0000, 0x0000, 0x0000, /* 0x50 */
41 0x0000, 0x0000, 0x0000, 0x0000, /* 0x58 */
42 0x0000, 0x0000, 0x0000, 0x0000, /* 0x60 */
43 0x0000, 0x0000, 0x0000, 0x0000, /* 0x68 */
44 0x0000, 0x0808, 0x0000, 0x0006, /* 0x70 */
45 0x0000, 0x0000, 0x574d, 0x4c05, /* 0x78 */
46};
47
48static const struct snd_kcontrol_new wm9705_snd_ac97_controls[] = {
49 SOC_DOUBLE("Master Playback Volume", AC97_MASTER, 8, 0, 31, 1),
50 SOC_SINGLE("Master Playback Switch", AC97_MASTER, 15, 1, 1),
51 SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
52 SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 1, 1),
53 SOC_DOUBLE("PCM Playback Volume", AC97_PCM, 8, 0, 31, 1),
54 SOC_SINGLE("PCM Playback Switch", AC97_PCM, 15, 1, 1),
55 SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1),
56 SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
57 SOC_SINGLE("PCBeep Playback Volume", AC97_PC_BEEP, 1, 15, 1),
58 SOC_SINGLE("Phone Playback Volume", AC97_PHONE, 0, 31, 1),
59 SOC_DOUBLE("Line Playback Volume", AC97_LINE, 8, 0, 31, 1),
60 SOC_DOUBLE("CD Playback Volume", AC97_CD, 8, 0, 31, 1),
61 SOC_SINGLE("Mic Playback Volume", AC97_MIC, 0, 31, 1),
62 SOC_SINGLE("Mic 20dB Boost Switch", AC97_MIC, 6, 1, 0),
Ian Molton927b0ae2009-01-19 17:23:11 +000063 SOC_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0),
64 SOC_SINGLE("Capture Switch", AC97_REC_GAIN, 15, 1, 1),
Ian Molton2aceefe2009-01-16 11:04:18 +000065};
66
67static const char *wm9705_mic[] = {"Mic 1", "Mic 2"};
68static const char *wm9705_rec_sel[] = {"Mic", "CD", "NC", "NC",
69 "Line", "Stereo Mix", "Mono Mix", "Phone"};
70
71static const struct soc_enum wm9705_enum_mic =
72 SOC_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 8, 2, wm9705_mic);
73static const struct soc_enum wm9705_enum_rec_l =
74 SOC_ENUM_SINGLE(AC97_REC_SEL, 8, 8, wm9705_rec_sel);
75static const struct soc_enum wm9705_enum_rec_r =
76 SOC_ENUM_SINGLE(AC97_REC_SEL, 0, 8, wm9705_rec_sel);
77
78/* Headphone Mixer */
79static const struct snd_kcontrol_new wm9705_hp_mixer_controls[] = {
80 SOC_DAPM_SINGLE("PCBeep Playback Switch", AC97_PC_BEEP, 15, 1, 1),
81 SOC_DAPM_SINGLE("CD Playback Switch", AC97_CD, 15, 1, 1),
82 SOC_DAPM_SINGLE("Mic Playback Switch", AC97_MIC, 15, 1, 1),
83 SOC_DAPM_SINGLE("Phone Playback Switch", AC97_PHONE, 15, 1, 1),
84 SOC_DAPM_SINGLE("Line Playback Switch", AC97_LINE, 15, 1, 1),
85};
86
87/* Mic source */
88static const struct snd_kcontrol_new wm9705_mic_src_controls =
89 SOC_DAPM_ENUM("Route", wm9705_enum_mic);
90
91/* Capture source */
92static const struct snd_kcontrol_new wm9705_capture_selectl_controls =
93 SOC_DAPM_ENUM("Route", wm9705_enum_rec_l);
94static const struct snd_kcontrol_new wm9705_capture_selectr_controls =
95 SOC_DAPM_ENUM("Route", wm9705_enum_rec_r);
96
97/* DAPM widgets */
98static const struct snd_soc_dapm_widget wm9705_dapm_widgets[] = {
99 SND_SOC_DAPM_MUX("Mic Source", SND_SOC_NOPM, 0, 0,
100 &wm9705_mic_src_controls),
101 SND_SOC_DAPM_MUX("Left Capture Source", SND_SOC_NOPM, 0, 0,
102 &wm9705_capture_selectl_controls),
103 SND_SOC_DAPM_MUX("Right Capture Source", SND_SOC_NOPM, 0, 0,
104 &wm9705_capture_selectr_controls),
105 SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback",
106 SND_SOC_NOPM, 0, 0),
107 SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback",
108 SND_SOC_NOPM, 0, 0),
109 SND_SOC_DAPM_MIXER_NAMED_CTL("HP Mixer", SND_SOC_NOPM, 0, 0,
110 &wm9705_hp_mixer_controls[0],
111 ARRAY_SIZE(wm9705_hp_mixer_controls)),
112 SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
113 SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture", SND_SOC_NOPM, 0, 0),
114 SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture", SND_SOC_NOPM, 0, 0),
115 SND_SOC_DAPM_PGA("Headphone PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
116 SND_SOC_DAPM_PGA("Speaker PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
117 SND_SOC_DAPM_PGA("Line PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
118 SND_SOC_DAPM_PGA("Line out PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
119 SND_SOC_DAPM_PGA("Mono PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
120 SND_SOC_DAPM_PGA("Phone PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
121 SND_SOC_DAPM_PGA("Mic PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
122 SND_SOC_DAPM_PGA("PCBEEP PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
123 SND_SOC_DAPM_PGA("CD PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
124 SND_SOC_DAPM_PGA("ADC PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
125 SND_SOC_DAPM_OUTPUT("HPOUTL"),
126 SND_SOC_DAPM_OUTPUT("HPOUTR"),
127 SND_SOC_DAPM_OUTPUT("LOUT"),
128 SND_SOC_DAPM_OUTPUT("ROUT"),
129 SND_SOC_DAPM_OUTPUT("MONOOUT"),
130 SND_SOC_DAPM_INPUT("PHONE"),
131 SND_SOC_DAPM_INPUT("LINEINL"),
132 SND_SOC_DAPM_INPUT("LINEINR"),
133 SND_SOC_DAPM_INPUT("CDINL"),
134 SND_SOC_DAPM_INPUT("CDINR"),
135 SND_SOC_DAPM_INPUT("PCBEEP"),
136 SND_SOC_DAPM_INPUT("MIC1"),
137 SND_SOC_DAPM_INPUT("MIC2"),
138};
139
140/* Audio map
141 * WM9705 has no switches to disable the route from the inputs to the HP mixer
142 * so in order to prevent active inputs from forcing the audio outputs to be
143 * constantly enabled, we use the mutes on those inputs to simulate such
144 * controls.
145 */
146static const struct snd_soc_dapm_route audio_map[] = {
147 /* HP mixer */
148 {"HP Mixer", "PCBeep Playback Switch", "PCBEEP PGA"},
149 {"HP Mixer", "CD Playback Switch", "CD PGA"},
150 {"HP Mixer", "Mic Playback Switch", "Mic PGA"},
151 {"HP Mixer", "Phone Playback Switch", "Phone PGA"},
152 {"HP Mixer", "Line Playback Switch", "Line PGA"},
153 {"HP Mixer", NULL, "Left DAC"},
154 {"HP Mixer", NULL, "Right DAC"},
155
156 /* mono mixer */
157 {"Mono Mixer", NULL, "HP Mixer"},
158
159 /* outputs */
160 {"Headphone PGA", NULL, "HP Mixer"},
161 {"HPOUTL", NULL, "Headphone PGA"},
162 {"HPOUTR", NULL, "Headphone PGA"},
163 {"Line out PGA", NULL, "HP Mixer"},
164 {"LOUT", NULL, "Line out PGA"},
165 {"ROUT", NULL, "Line out PGA"},
166 {"Mono PGA", NULL, "Mono Mixer"},
167 {"MONOOUT", NULL, "Mono PGA"},
168
169 /* inputs */
170 {"CD PGA", NULL, "CDINL"},
171 {"CD PGA", NULL, "CDINR"},
172 {"Line PGA", NULL, "LINEINL"},
173 {"Line PGA", NULL, "LINEINR"},
174 {"Phone PGA", NULL, "PHONE"},
175 {"Mic Source", "Mic 1", "MIC1"},
176 {"Mic Source", "Mic 2", "MIC2"},
177 {"Mic PGA", NULL, "Mic Source"},
178 {"PCBEEP PGA", NULL, "PCBEEP"},
179
180 /* Left capture selector */
181 {"Left Capture Source", "Mic", "Mic Source"},
182 {"Left Capture Source", "CD", "CDINL"},
183 {"Left Capture Source", "Line", "LINEINL"},
184 {"Left Capture Source", "Stereo Mix", "HP Mixer"},
185 {"Left Capture Source", "Mono Mix", "HP Mixer"},
186 {"Left Capture Source", "Phone", "PHONE"},
187
188 /* Right capture source */
189 {"Right Capture Source", "Mic", "Mic Source"},
190 {"Right Capture Source", "CD", "CDINR"},
191 {"Right Capture Source", "Line", "LINEINR"},
192 {"Right Capture Source", "Stereo Mix", "HP Mixer"},
193 {"Right Capture Source", "Mono Mix", "HP Mixer"},
194 {"Right Capture Source", "Phone", "PHONE"},
195
196 {"ADC PGA", NULL, "Left Capture Source"},
197 {"ADC PGA", NULL, "Right Capture Source"},
198
199 /* ADC's */
200 {"Left ADC", NULL, "ADC PGA"},
201 {"Right ADC", NULL, "ADC PGA"},
202};
203
204static int wm9705_add_widgets(struct snd_soc_codec *codec)
205{
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200206 struct snd_soc_dapm_context *dapm = &codec->dapm;
207
208 snd_soc_dapm_new_controls(dapm, wm9705_dapm_widgets,
Ian Molton2aceefe2009-01-16 11:04:18 +0000209 ARRAY_SIZE(wm9705_dapm_widgets));
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200210 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
Ian Molton2aceefe2009-01-16 11:04:18 +0000211
212 return 0;
213}
214
215/* We use a register cache to enhance read performance. */
216static unsigned int ac97_read(struct snd_soc_codec *codec, unsigned int reg)
217{
218 u16 *cache = codec->reg_cache;
219
220 switch (reg) {
221 case AC97_RESET:
222 case AC97_VENDOR_ID1:
223 case AC97_VENDOR_ID2:
224 return soc_ac97_ops.read(codec->ac97, reg);
225 default:
226 reg = reg >> 1;
227
228 if (reg >= (ARRAY_SIZE(wm9705_reg)))
229 return -EIO;
230
231 return cache[reg];
232 }
233}
234
235static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
236 unsigned int val)
237{
238 u16 *cache = codec->reg_cache;
239
240 soc_ac97_ops.write(codec->ac97, reg, val);
241 reg = reg >> 1;
242 if (reg < (ARRAY_SIZE(wm9705_reg)))
243 cache[reg] = val;
244
245 return 0;
246}
247
248static int ac97_prepare(struct snd_pcm_substream *substream,
249 struct snd_soc_dai *dai)
250{
251 struct snd_pcm_runtime *runtime = substream->runtime;
252 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000253 struct snd_soc_codec *codec = rtd->codec;
Ian Molton2aceefe2009-01-16 11:04:18 +0000254 int reg;
255 u16 vra;
256
257 vra = ac97_read(codec, AC97_EXTENDED_STATUS);
258 ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
259
260 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
261 reg = AC97_PCM_FRONT_DAC_RATE;
262 else
263 reg = AC97_PCM_LR_ADC_RATE;
264
265 return ac97_write(codec, reg, runtime->rate);
266}
267
268#define WM9705_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | \
269 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
270 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
271 SNDRV_PCM_RATE_48000)
272
Eric Miao6335d052009-03-03 09:41:00 +0800273static struct snd_soc_dai_ops wm9705_dai_ops = {
274 .prepare = ac97_prepare,
275};
276
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000277static struct snd_soc_dai_driver wm9705_dai[] = {
Ian Molton2aceefe2009-01-16 11:04:18 +0000278 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000279 .name = "wm9705-hifi",
Ian Molton2aceefe2009-01-16 11:04:18 +0000280 .ac97_control = 1,
281 .playback = {
282 .stream_name = "HiFi Playback",
283 .channels_min = 1,
284 .channels_max = 2,
285 .rates = WM9705_AC97_RATES,
Mark Brown33f503c2009-05-02 12:24:55 +0100286 .formats = SND_SOC_STD_AC97_FMTS,
Ian Molton2aceefe2009-01-16 11:04:18 +0000287 },
288 .capture = {
289 .stream_name = "HiFi Capture",
290 .channels_min = 1,
291 .channels_max = 2,
292 .rates = WM9705_AC97_RATES,
Mark Brown33f503c2009-05-02 12:24:55 +0100293 .formats = SND_SOC_STD_AC97_FMTS,
Ian Molton2aceefe2009-01-16 11:04:18 +0000294 },
Eric Miao6335d052009-03-03 09:41:00 +0800295 .ops = &wm9705_dai_ops,
Ian Molton2aceefe2009-01-16 11:04:18 +0000296 },
297 {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000298 .name = "wm9705-aux",
Ian Molton2aceefe2009-01-16 11:04:18 +0000299 .playback = {
300 .stream_name = "Aux Playback",
301 .channels_min = 1,
302 .channels_max = 1,
303 .rates = WM9705_AC97_RATES,
304 .formats = SNDRV_PCM_FMTBIT_S16_LE,
305 },
306 }
307};
Ian Molton2aceefe2009-01-16 11:04:18 +0000308
309static int wm9705_reset(struct snd_soc_codec *codec)
310{
311 if (soc_ac97_ops.reset) {
312 soc_ac97_ops.reset(codec->ac97);
313 if (ac97_read(codec, 0) == wm9705_reg[0])
314 return 0; /* Success */
315 }
316
317 return -EIO;
318}
319
Mark Brown0a11b162009-04-02 15:49:41 +0100320#ifdef CONFIG_PM
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000321static int wm9705_soc_suspend(struct snd_soc_codec *codec, pm_message_t msg)
Mark Brown0a11b162009-04-02 15:49:41 +0100322{
Mark Brown0a11b162009-04-02 15:49:41 +0100323 soc_ac97_ops.write(codec->ac97, AC97_POWERDOWN, 0xffff);
324
325 return 0;
326}
327
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000328static int wm9705_soc_resume(struct snd_soc_codec *codec)
Mark Brown0a11b162009-04-02 15:49:41 +0100329{
Mark Brown0a11b162009-04-02 15:49:41 +0100330 int i, ret;
331 u16 *cache = codec->reg_cache;
332
333 ret = wm9705_reset(codec);
334 if (ret < 0) {
335 printk(KERN_ERR "could not reset AC97 codec\n");
336 return ret;
337 }
338
339 for (i = 2; i < ARRAY_SIZE(wm9705_reg) << 1; i += 2) {
340 soc_ac97_ops.write(codec->ac97, i, cache[i>>1]);
341 }
342
343 return 0;
344}
345#else
346#define wm9705_soc_suspend NULL
347#define wm9705_soc_resume NULL
348#endif
349
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000350static int wm9705_soc_probe(struct snd_soc_codec *codec)
Ian Molton2aceefe2009-01-16 11:04:18 +0000351{
Ian Molton2aceefe2009-01-16 11:04:18 +0000352 int ret = 0;
353
354 printk(KERN_INFO "WM9705 SoC Audio Codec\n");
355
Ian Molton2aceefe2009-01-16 11:04:18 +0000356 ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
357 if (ret < 0) {
358 printk(KERN_ERR "wm9705: failed to register AC97 codec\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000359 return ret;
Ian Molton2aceefe2009-01-16 11:04:18 +0000360 }
361
Ian Molton2aceefe2009-01-16 11:04:18 +0000362 ret = wm9705_reset(codec);
363 if (ret)
364 goto reset_err;
365
366 snd_soc_add_controls(codec, wm9705_snd_ac97_controls,
367 ARRAY_SIZE(wm9705_snd_ac97_controls));
368 wm9705_add_widgets(codec);
369
Ian Molton2aceefe2009-01-16 11:04:18 +0000370 return 0;
371
Takashi Iwaia22eaf42009-11-27 15:14:09 +0100372reset_err:
Ian Molton2aceefe2009-01-16 11:04:18 +0000373 snd_soc_free_ac97_codec(codec);
Ian Molton2aceefe2009-01-16 11:04:18 +0000374 return ret;
375}
376
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000377static int wm9705_soc_remove(struct snd_soc_codec *codec)
Ian Molton2aceefe2009-01-16 11:04:18 +0000378{
Ian Molton2aceefe2009-01-16 11:04:18 +0000379 snd_soc_free_ac97_codec(codec);
Ian Molton2aceefe2009-01-16 11:04:18 +0000380 return 0;
381}
382
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000383static struct snd_soc_codec_driver soc_codec_dev_wm9705 = {
Ian Molton2aceefe2009-01-16 11:04:18 +0000384 .probe = wm9705_soc_probe,
385 .remove = wm9705_soc_remove,
Mark Brown0a11b162009-04-02 15:49:41 +0100386 .suspend = wm9705_soc_suspend,
387 .resume = wm9705_soc_resume,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000388 .read = ac97_read,
389 .write = ac97_write,
Dimitris Papastamose5eec342010-09-10 18:14:56 +0100390 .reg_cache_size = ARRAY_SIZE(wm9705_reg),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000391 .reg_word_size = sizeof(u16),
392 .reg_cache_step = 2,
393 .reg_cache_default = wm9705_reg,
Ian Molton2aceefe2009-01-16 11:04:18 +0000394};
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000395
396static __devinit int wm9705_probe(struct platform_device *pdev)
397{
398 return snd_soc_register_codec(&pdev->dev,
399 &soc_codec_dev_wm9705, wm9705_dai, ARRAY_SIZE(wm9705_dai));
400}
401
402static int __devexit wm9705_remove(struct platform_device *pdev)
403{
404 snd_soc_unregister_codec(&pdev->dev);
405 return 0;
406}
407
408static struct platform_driver wm9705_codec_driver = {
409 .driver = {
410 .name = "wm9705-codec",
411 .owner = THIS_MODULE,
412 },
413
414 .probe = wm9705_probe,
415 .remove = __devexit_p(wm9705_remove),
416};
417
418static int __init wm9705_init(void)
419{
420 return platform_driver_register(&wm9705_codec_driver);
421}
422module_init(wm9705_init);
423
424static void __exit wm9705_exit(void)
425{
426 platform_driver_unregister(&wm9705_codec_driver);
427}
428module_exit(wm9705_exit);
Ian Molton2aceefe2009-01-16 11:04:18 +0000429
430MODULE_DESCRIPTION("ASoC WM9705 driver");
431MODULE_AUTHOR("Ian Molton");
432MODULE_LICENSE("GPL v2");