[ALSA] ASoC codec error reporting
[sfrench/cifs-2.6.git] / sound / soc / codecs / wm9712.c
1 /*
2  * wm9712.c  --  ALSA Soc WM9712 codec support
3  *
4  * Copyright 2006 Wolfson Microelectronics PLC.
5  * Author: Liam Girdwood
6  *         liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
7  *
8  *  This program is free software; you can redistribute  it and/or modify it
9  *  under  the terms of  the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the  License, or (at your
11  *  option) any later version.
12  *
13  *  Revision history
14  *    4th Feb 2006   Initial version.
15  */
16
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/version.h>
20 #include <linux/kernel.h>
21 #include <linux/device.h>
22 #include <sound/driver.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/ac97_codec.h>
26 #include <sound/initval.h>
27 #include <sound/soc.h>
28 #include <sound/soc-dapm.h>
29
30 #define WM9712_VERSION "0.4"
31
32 static unsigned int ac97_read(struct snd_soc_codec *codec,
33         unsigned int reg);
34 static int ac97_write(struct snd_soc_codec *codec,
35         unsigned int reg, unsigned int val);
36
37 #define AC97_DIR \
38         (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
39
40 #define AC97_RATES \
41         (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
42         SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
43         SNDRV_PCM_RATE_48000)
44
45 /* may need to expand this */
46 static struct snd_soc_dai_mode ac97_modes[] = {
47         {
48                 .pcmfmt = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S18_3LE,
49                 .pcmrate = AC97_RATES,
50                 .pcmdir = AC97_DIR,
51         },
52 };
53
54 /*
55  * WM9712 register cache
56  */
57 static const u16 wm9712_reg[] = {
58         0x6174, 0x8000, 0x8000, 0x8000, // 6
59         0xf0f0, 0xaaa0, 0xc008, 0x6808, // e
60         0xe808, 0xaaa0, 0xad00, 0x8000, // 16
61         0xe808, 0x3000, 0x8000, 0x0000, // 1e
62         0x0000, 0x0000, 0x0000, 0x000f, // 26
63         0x0405, 0x0410, 0xbb80, 0xbb80, // 2e
64         0x0000, 0xbb80, 0x0000, 0x0000, // 36
65         0x0000, 0x2000, 0x0000, 0x0000, // 3e
66         0x0000, 0x0000, 0x0000, 0x0000, // 46
67         0x0000, 0x0000, 0xf83e, 0xffff, // 4e
68         0x0000, 0x0000, 0x0000, 0xf83e, // 56
69         0x0008, 0x0000, 0x0000, 0x0000, // 5e
70         0xb032, 0x3e00, 0x0000, 0x0000, // 66
71         0x0000, 0x0000, 0x0000, 0x0000, // 6e
72         0x0000, 0x0000, 0x0000, 0x0006, // 76
73         0x0001, 0x0000, 0x574d, 0x4c12, // 7e
74         0x0000, 0x0000 // virtual hp mixers
75 };
76
77 /* virtual HP mixers regs */
78 #define HPL_MIXER       0x80
79 #define HPR_MIXER       0x82
80
81 static const char *wm9712_alc_select[] = {"None", "Left", "Right", "Stereo"};
82 static const char *wm9712_alc_mux[] = {"Stereo", "Left", "Right", "None"};
83 static const char *wm9712_out3_src[] = {"Left", "VREF", "Left + Right",
84         "Mono"};
85 static const char *wm9712_spk_src[] = {"Speaker Mix", "Headphone Mix"};
86 static const char *wm9712_rec_adc[] = {"Stereo", "Left", "Right", "Mute"};
87 static const char *wm9712_base[] = {"Linear Control", "Adaptive Boost"};
88 static const char *wm9712_rec_gain[] = {"+1.5dB Steps", "+0.75dB Steps"};
89 static const char *wm9712_mic[] = {"Mic 1", "Differential", "Mic 2",
90         "Stereo"};
91 static const char *wm9712_rec_sel[] = {"Mic", "NC", "NC", "Speaker Mixer",
92         "Line", "Headphone Mixer", "Phone Mixer", "Phone"};
93 static const char *wm9712_ng_type[] = {"Constant Gain", "Mute"};
94 static const char *wm9712_diff_sel[] = {"Mic", "Line"};
95
96 static const struct soc_enum wm9712_enum[] = {
97 SOC_ENUM_SINGLE(AC97_PCI_SVID, 14, 4, wm9712_alc_select),
98 SOC_ENUM_SINGLE(AC97_VIDEO, 12, 4, wm9712_alc_mux),
99 SOC_ENUM_SINGLE(AC97_AUX, 9, 4, wm9712_out3_src),
100 SOC_ENUM_SINGLE(AC97_AUX, 8, 2, wm9712_spk_src),
101 SOC_ENUM_SINGLE(AC97_REC_SEL, 12, 4, wm9712_rec_adc),
102 SOC_ENUM_SINGLE(AC97_MASTER_TONE, 15, 2, wm9712_base),
103 SOC_ENUM_DOUBLE(AC97_REC_GAIN, 14, 6, 2, wm9712_rec_gain),
104 SOC_ENUM_SINGLE(AC97_MIC, 5, 4, wm9712_mic),
105 SOC_ENUM_SINGLE(AC97_REC_SEL, 8, 8, wm9712_rec_sel),
106 SOC_ENUM_SINGLE(AC97_REC_SEL, 0, 8, wm9712_rec_sel),
107 SOC_ENUM_SINGLE(AC97_PCI_SVID, 5, 2, wm9712_ng_type),
108 SOC_ENUM_SINGLE(0x5c, 8, 2, wm9712_diff_sel),
109 };
110
111 static const struct snd_kcontrol_new wm9712_snd_ac97_controls[] = {
112 SOC_DOUBLE("Speaker Playback Volume", AC97_MASTER, 8, 0, 31, 1),
113 SOC_SINGLE("Speaker Playback Switch", AC97_MASTER, 15, 1, 1),
114 SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
115 SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE,15, 1, 1),
116
117 SOC_SINGLE("Speaker Playback ZC Switch", AC97_MASTER, 7, 1, 0),
118 SOC_SINGLE("Speaker Playback Invert Switch", AC97_MASTER, 6, 1, 0),
119 SOC_SINGLE("Headphone Playback ZC Switch", AC97_HEADPHONE, 7, 1, 0),
120 SOC_SINGLE("Mono Playback ZC Switch", AC97_MASTER_MONO, 7, 1, 0),
121 SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 0),
122
123 SOC_SINGLE("ALC Target Volume", AC97_CODEC_CLASS_REV, 12, 15, 0),
124 SOC_SINGLE("ALC Hold Time", AC97_CODEC_CLASS_REV, 8, 15, 0),
125 SOC_SINGLE("ALC Decay Time", AC97_CODEC_CLASS_REV, 4, 15, 0),
126 SOC_SINGLE("ALC Attack Time", AC97_CODEC_CLASS_REV, 0, 15, 0),
127 SOC_ENUM("ALC Function", wm9712_enum[0]),
128 SOC_SINGLE("ALC Max Volume", AC97_PCI_SVID, 11, 7, 0),
129 SOC_SINGLE("ALC ZC Timeout", AC97_PCI_SVID, 9, 3, 1),
130 SOC_SINGLE("ALC ZC Switch", AC97_PCI_SVID, 8, 1, 0),
131 SOC_SINGLE("ALC NG Switch", AC97_PCI_SVID, 7, 1, 0),
132 SOC_ENUM("ALC NG Type", wm9712_enum[10]),
133 SOC_SINGLE("ALC NG Threshold", AC97_PCI_SVID, 0, 31, 1),
134
135 SOC_SINGLE("Mic Headphone  Volume", AC97_VIDEO, 12, 7, 1),
136 SOC_SINGLE("ALC Headphone Volume", AC97_VIDEO, 7, 7, 1),
137
138 SOC_SINGLE("Out3 Switch", AC97_AUX, 15, 1, 1),
139 SOC_SINGLE("Out3 ZC Switch", AC97_AUX, 7, 1, 1),
140 SOC_SINGLE("Out3 Volume", AC97_AUX, 0, 31, 1),
141
142 SOC_SINGLE("PCBeep Bypass Headphone Volume", AC97_PC_BEEP, 12, 7, 1),
143 SOC_SINGLE("PCBeep Bypass Speaker Volume", AC97_PC_BEEP, 8, 7, 1),
144 SOC_SINGLE("PCBeep Bypass Phone Volume", AC97_PC_BEEP, 4, 7, 1),
145
146 SOC_SINGLE("Aux Playback Headphone Volume", AC97_CD, 12, 7, 1),
147 SOC_SINGLE("Aux Playback Speaker Volume", AC97_CD, 8, 7, 1),
148 SOC_SINGLE("Aux Playback Phone Volume", AC97_CD, 4, 7, 1),
149
150 SOC_SINGLE("Phone Volume", AC97_PHONE, 0, 15, 0),
151 SOC_DOUBLE("Line Capture Volume", AC97_LINE, 8, 0, 31, 1),
152
153 SOC_SINGLE("Capture 20dB Boost Switch", AC97_REC_SEL, 14, 1, 0),
154 SOC_SINGLE("Capture to Phone 20dB Boost Switch", AC97_REC_SEL, 11, 1, 1),
155
156 SOC_SINGLE("3D Upper Cut-off Switch", AC97_3D_CONTROL, 5, 1, 1),
157 SOC_SINGLE("3D Lower Cut-off Switch", AC97_3D_CONTROL, 4, 1, 1),
158 SOC_SINGLE("3D Playback Volume", AC97_3D_CONTROL, 0, 15, 0),
159
160 SOC_ENUM("Bass Control", wm9712_enum[5]),
161 SOC_SINGLE("Bass Cut-off Switch", AC97_MASTER_TONE, 12, 1, 1),
162 SOC_SINGLE("Tone Cut-off Switch", AC97_MASTER_TONE, 4, 1, 1),
163 SOC_SINGLE("Playback Attenuate (-6dB) Switch", AC97_MASTER_TONE, 6, 1, 0),
164 SOC_SINGLE("Bass Volume", AC97_MASTER_TONE, 8, 15, 0),
165 SOC_SINGLE("Treble Volume", AC97_MASTER_TONE, 0, 15, 0),
166
167 SOC_SINGLE("Capture ADC Switch", AC97_REC_GAIN, 15, 1, 1),
168 SOC_ENUM("Capture Volume Steps", wm9712_enum[6]),
169 SOC_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 63, 1),
170 SOC_SINGLE("Capture ZC Switch", AC97_REC_GAIN, 7, 1, 0),
171
172 SOC_SINGLE("Mic 1 Volume", AC97_MIC, 8, 31, 1),
173 SOC_SINGLE("Mic 2 Volume", AC97_MIC, 0, 31, 1),
174 SOC_SINGLE("Mic 20dB Boost Switch", AC97_MIC, 7, 1, 0),
175 };
176
177 /* add non dapm controls */
178 static int wm9712_add_controls(struct snd_soc_codec *codec)
179 {
180         int err, i;
181
182         for (i = 0; i < ARRAY_SIZE(wm9712_snd_ac97_controls); i++) {
183                 err = snd_ctl_add(codec->card,
184                                 snd_soc_cnew(&wm9712_snd_ac97_controls[i],codec, NULL));
185                 if (err < 0)
186                         return err;
187         }
188         return 0;
189 }
190
191 /* We have to create a fake left and right HP mixers because
192  * the codec only has a single control that is shared by both channels.
193  * This makes it impossible to determine the audio path.
194  */
195 static int mixer_event (struct snd_soc_dapm_widget *w, int event)
196 {
197         u16 l, r, beep, line, phone, mic, pcm, aux;
198
199         l = ac97_read(w->codec, HPL_MIXER);
200         r = ac97_read(w->codec, HPR_MIXER);
201         beep = ac97_read(w->codec, AC97_PC_BEEP);
202         mic = ac97_read(w->codec, AC97_VIDEO);
203         phone = ac97_read(w->codec, AC97_PHONE);
204         line = ac97_read(w->codec, AC97_LINE);
205         pcm = ac97_read(w->codec, AC97_PCM);
206         aux = ac97_read(w->codec, AC97_CD);
207
208         if (l & 0x1 || r & 0x1)
209                 ac97_write(w->codec, AC97_VIDEO, mic & 0x7fff);
210         else
211                 ac97_write(w->codec, AC97_VIDEO, mic | 0x8000);
212
213         if (l & 0x2 || r & 0x2)
214                 ac97_write(w->codec, AC97_PCM, pcm & 0x7fff);
215         else
216                 ac97_write(w->codec, AC97_PCM, pcm | 0x8000);
217
218         if (l & 0x4 || r & 0x4)
219                 ac97_write(w->codec, AC97_LINE, line & 0x7fff);
220         else
221                 ac97_write(w->codec, AC97_LINE, line | 0x8000);
222
223         if (l & 0x8 || r & 0x8)
224                 ac97_write(w->codec, AC97_PHONE, phone & 0x7fff);
225         else
226                 ac97_write(w->codec, AC97_PHONE, phone | 0x8000);
227
228         if (l & 0x10 || r & 0x10)
229                 ac97_write(w->codec, AC97_CD, aux & 0x7fff);
230         else
231                 ac97_write(w->codec, AC97_CD, aux | 0x8000);
232
233         if (l & 0x20 || r & 0x20)
234                 ac97_write(w->codec, AC97_PC_BEEP, beep & 0x7fff);
235         else
236                 ac97_write(w->codec, AC97_PC_BEEP, beep | 0x8000);
237
238         return 0;
239 }
240
241 /* Left Headphone Mixers */
242 static const struct snd_kcontrol_new wm9712_hpl_mixer_controls[] = {
243         SOC_DAPM_SINGLE("PCBeep Bypass Switch", HPL_MIXER, 5, 1, 0),
244         SOC_DAPM_SINGLE("Aux Playback Switch", HPL_MIXER, 4, 1, 0),
245         SOC_DAPM_SINGLE("Phone Bypass Switch", HPL_MIXER, 3, 1, 0),
246         SOC_DAPM_SINGLE("Line Bypass Switch", HPL_MIXER, 2, 1, 0),
247         SOC_DAPM_SINGLE("PCM Playback Switch", HPL_MIXER, 1, 1, 0),
248         SOC_DAPM_SINGLE("Mic Sidetone Switch", HPL_MIXER, 0, 1, 0),
249 };
250
251 /* Right Headphone Mixers */
252 static const struct snd_kcontrol_new wm9712_hpr_mixer_controls[] = {
253         SOC_DAPM_SINGLE("PCBeep Bypass Switch", HPR_MIXER, 5, 1, 0),
254         SOC_DAPM_SINGLE("Aux Playback Switch", HPR_MIXER, 4, 1, 0),
255         SOC_DAPM_SINGLE("Phone Bypass Switch", HPR_MIXER, 3, 1, 0),
256         SOC_DAPM_SINGLE("Line Bypass Switch", HPR_MIXER, 2, 1, 0),
257         SOC_DAPM_SINGLE("PCM Playback Switch", HPR_MIXER, 1, 1, 0),
258         SOC_DAPM_SINGLE("Mic Sidetone Switch", HPR_MIXER, 0, 1, 0),
259 };
260
261 /* Speaker Mixer */
262 static const struct snd_kcontrol_new wm9712_speaker_mixer_controls[] = {
263         SOC_DAPM_SINGLE("PCBeep Bypass Switch", AC97_PC_BEEP, 11, 1, 1),
264         SOC_DAPM_SINGLE("Aux Playback Switch", AC97_CD, 11, 1, 1),
265         SOC_DAPM_SINGLE("Phone Bypass Switch", AC97_PHONE, 14, 1, 1),
266         SOC_DAPM_SINGLE("Line Bypass Switch", AC97_LINE, 14, 1, 1),
267         SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PCM, 14, 1, 1),
268 };
269
270 /* Phone Mixer */
271 static const struct snd_kcontrol_new wm9712_phone_mixer_controls[] = {
272         SOC_DAPM_SINGLE("PCBeep Bypass Switch", AC97_PC_BEEP, 7, 1, 1),
273         SOC_DAPM_SINGLE("Aux Playback Switch", AC97_CD, 7, 1, 1),
274         SOC_DAPM_SINGLE("Line Bypass Switch", AC97_LINE, 13, 1, 1),
275         SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PCM, 13, 1, 1),
276         SOC_DAPM_SINGLE("Mic 1 Sidetone Switch", AC97_MIC, 14, 1, 1),
277         SOC_DAPM_SINGLE("Mic 2 Sidetone Switch", AC97_MIC, 13, 1, 1),
278 };
279
280 /* ALC headphone mux */
281 static const struct snd_kcontrol_new wm9712_alc_mux_controls =
282 SOC_DAPM_ENUM("Route", wm9712_enum[1]);
283
284 /* out 3 mux */
285 static const struct snd_kcontrol_new wm9712_out3_mux_controls =
286 SOC_DAPM_ENUM("Route", wm9712_enum[2]);
287
288 /* spk mux */
289 static const struct snd_kcontrol_new wm9712_spk_mux_controls =
290 SOC_DAPM_ENUM("Route", wm9712_enum[3]);
291
292 /* Capture to Phone mux */
293 static const struct snd_kcontrol_new wm9712_capture_phone_mux_controls =
294 SOC_DAPM_ENUM("Route", wm9712_enum[4]);
295
296 /* Capture left select */
297 static const struct snd_kcontrol_new wm9712_capture_selectl_controls =
298 SOC_DAPM_ENUM("Route", wm9712_enum[8]);
299
300 /* Capture right select */
301 static const struct snd_kcontrol_new wm9712_capture_selectr_controls =
302 SOC_DAPM_ENUM("Route", wm9712_enum[9]);
303
304 /* Mic select */
305 static const struct snd_kcontrol_new wm9712_mic_src_controls =
306 SOC_DAPM_ENUM("Route", wm9712_enum[7]);
307
308 /* diff select */
309 static const struct snd_kcontrol_new wm9712_diff_sel_controls =
310 SOC_DAPM_ENUM("Route", wm9712_enum[11]);
311
312 static const struct snd_soc_dapm_widget wm9712_dapm_widgets[] = {
313 SND_SOC_DAPM_MUX("ALC Sidetone Mux", SND_SOC_NOPM, 0, 0,
314         &wm9712_alc_mux_controls),
315 SND_SOC_DAPM_MUX("Out3 Mux", SND_SOC_NOPM, 0, 0,
316         &wm9712_out3_mux_controls),
317 SND_SOC_DAPM_MUX("Speaker Mux", SND_SOC_NOPM, 0, 0,
318         &wm9712_spk_mux_controls),
319 SND_SOC_DAPM_MUX("Capture Phone Mux", SND_SOC_NOPM, 0, 0,
320         &wm9712_capture_phone_mux_controls),
321 SND_SOC_DAPM_MUX("Left Capture Select", SND_SOC_NOPM, 0, 0,
322         &wm9712_capture_selectl_controls),
323 SND_SOC_DAPM_MUX("Right Capture Select", SND_SOC_NOPM, 0, 0,
324         &wm9712_capture_selectr_controls),
325 SND_SOC_DAPM_MUX("Mic Select Source", SND_SOC_NOPM, 0, 0,
326         &wm9712_mic_src_controls),
327 SND_SOC_DAPM_MUX("Differential Source", SND_SOC_NOPM, 0, 0,
328         &wm9712_diff_sel_controls),
329 SND_SOC_DAPM_MIXER("AC97 Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
330 SND_SOC_DAPM_MIXER_E("Left HP Mixer", AC97_INT_PAGING, 9, 1,
331         &wm9712_hpl_mixer_controls[0], ARRAY_SIZE(wm9712_hpl_mixer_controls),
332         mixer_event, SND_SOC_DAPM_POST_REG),
333 SND_SOC_DAPM_MIXER_E("Right HP Mixer", AC97_INT_PAGING, 8, 1,
334         &wm9712_hpr_mixer_controls[0], ARRAY_SIZE(wm9712_hpr_mixer_controls),
335          mixer_event, SND_SOC_DAPM_POST_REG),
336 SND_SOC_DAPM_MIXER("Phone Mixer", AC97_INT_PAGING, 6, 1,
337         &wm9712_phone_mixer_controls[0], ARRAY_SIZE(wm9712_phone_mixer_controls)),
338 SND_SOC_DAPM_MIXER("Speaker Mixer", AC97_INT_PAGING, 7, 1,
339         &wm9712_speaker_mixer_controls[0],
340         ARRAY_SIZE(wm9712_speaker_mixer_controls)),
341 SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
342 SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", AC97_INT_PAGING, 14, 1),
343 SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", AC97_INT_PAGING, 13, 1),
344 SND_SOC_DAPM_DAC("Aux DAC", "Aux Playback", SND_SOC_NOPM, 0, 0),
345 SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture", AC97_INT_PAGING, 12, 1),
346 SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture", AC97_INT_PAGING, 11, 1),
347 SND_SOC_DAPM_PGA("Headphone PGA", AC97_INT_PAGING, 4, 1, NULL, 0),
348 SND_SOC_DAPM_PGA("Speaker PGA", AC97_INT_PAGING, 3, 1, NULL, 0),
349 SND_SOC_DAPM_PGA("Out 3 PGA", AC97_INT_PAGING, 5, 1, NULL, 0),
350 SND_SOC_DAPM_PGA("Line PGA", AC97_INT_PAGING, 2, 1, NULL, 0),
351 SND_SOC_DAPM_PGA("Phone PGA", AC97_INT_PAGING, 1, 1, NULL, 0),
352 SND_SOC_DAPM_PGA("Mic PGA", AC97_INT_PAGING, 0, 1, NULL, 0),
353 SND_SOC_DAPM_MICBIAS("Mic Bias", AC97_INT_PAGING, 10, 1),
354 SND_SOC_DAPM_OUTPUT("MONOOUT"),
355 SND_SOC_DAPM_OUTPUT("HPOUTL"),
356 SND_SOC_DAPM_OUTPUT("HPOUTR"),
357 SND_SOC_DAPM_OUTPUT("LOUT2"),
358 SND_SOC_DAPM_OUTPUT("ROUT2"),
359 SND_SOC_DAPM_OUTPUT("OUT3"),
360 SND_SOC_DAPM_INPUT("LINEINL"),
361 SND_SOC_DAPM_INPUT("LINEINR"),
362 SND_SOC_DAPM_INPUT("PHONE"),
363 SND_SOC_DAPM_INPUT("PCBEEP"),
364 SND_SOC_DAPM_INPUT("MIC1"),
365 SND_SOC_DAPM_INPUT("MIC2"),
366 };
367
368 static const char *audio_map[][3] = {
369         /* virtual mixer - mixes left & right channels for spk and mono */
370         {"AC97 Mixer", NULL, "Left DAC"},
371         {"AC97 Mixer", NULL, "Right DAC"},
372
373         /* Left HP mixer */
374         {"Left HP Mixer", "PCBeep Bypass Switch", "PCBEEP"},
375         {"Left HP Mixer", "Aux Playback Switch",  "Aux DAC"},
376         {"Left HP Mixer", "Phone Bypass Switch",  "Phone PGA"},
377         {"Left HP Mixer", "Line Bypass Switch",   "Line PGA"},
378         {"Left HP Mixer", "PCM Playback Switch",  "Left DAC"},
379         {"Left HP Mixer", "Mic Sidetone Switch",  "Mic PGA"},
380         {"Left HP Mixer", NULL,  "ALC Sidetone Mux"},
381         //{"Right HP Mixer", NULL, "HP Mixer"},
382
383         /* Right HP mixer */
384         {"Right HP Mixer", "PCBeep Bypass Switch", "PCBEEP"},
385         {"Right HP Mixer", "Aux Playback Switch",  "Aux DAC"},
386         {"Right HP Mixer", "Phone Bypass Switch",  "Phone PGA"},
387         {"Right HP Mixer", "Line Bypass Switch",   "Line PGA"},
388         {"Right HP Mixer", "PCM Playback Switch",  "Right DAC"},
389         {"Right HP Mixer", "Mic Sidetone Switch",  "Mic PGA"},
390         {"Right HP Mixer", NULL,  "ALC Sidetone Mux"},
391
392         /* speaker mixer */
393         {"Speaker Mixer", "PCBeep Bypass Switch", "PCBEEP"},
394         {"Speaker Mixer", "Line Bypass Switch",   "Line PGA"},
395         {"Speaker Mixer", "PCM Playback Switch",  "AC97 Mixer"},
396         {"Speaker Mixer", "Phone Bypass Switch",  "Phone PGA"},
397         {"Speaker Mixer", "Aux Playback Switch",  "Aux DAC"},
398
399         /* Phone mixer */
400         {"Phone Mixer", "PCBeep Bypass Switch",  "PCBEEP"},
401         {"Phone Mixer", "Line Bypass Switch",    "Line PGA"},
402         {"Phone Mixer", "Aux Playback Switch",   "Aux DAC"},
403         {"Phone Mixer", "PCM Playback Switch",   "AC97 Mixer"},
404         {"Phone Mixer", "Mic 1 Sidetone Switch", "Mic PGA"},
405         {"Phone Mixer", "Mic 2 Sidetone Switch", "Mic PGA"},
406
407         /* inputs */
408         {"Line PGA", NULL, "LINEINL"},
409         {"Line PGA", NULL, "LINEINR"},
410         {"Phone PGA", NULL, "PHONE"},
411         {"Mic PGA", NULL, "MIC1"},
412         {"Mic PGA", NULL, "MIC2"},
413
414         /* left capture selector */
415         {"Left Capture Select", "Mic", "MIC1"},
416         {"Left Capture Select", "Speaker Mixer", "Speaker Mixer"},
417         {"Left Capture Select", "Line", "LINEINL"},
418         {"Left Capture Select", "Headphone Mixer", "Left HP Mixer"},
419         {"Left Capture Select", "Phone Mixer", "Phone Mixer"},
420         {"Left Capture Select", "Phone", "PHONE"},
421
422         /* right capture selector */
423         {"Right Capture Select", "Mic", "MIC2"},
424         {"Right Capture Select", "Speaker Mixer", "Speaker Mixer"},
425         {"Right Capture Select", "Line", "LINEINR"},
426         {"Right Capture Select", "Headphone Mixer", "Right HP Mixer"},
427         {"Right Capture Select", "Phone Mixer", "Phone Mixer"},
428         {"Right Capture Select", "Phone", "PHONE"},
429
430         /* ALC Sidetone */
431         {"ALC Sidetone Mux", "Stereo", "Left Capture Select"},
432         {"ALC Sidetone Mux", "Stereo", "Right Capture Select"},
433         {"ALC Sidetone Mux", "Left", "Left Capture Select"},
434         {"ALC Sidetone Mux", "Right", "Right Capture Select"},
435
436         /* ADC's */
437         {"Left ADC", NULL, "Left Capture Select"},
438         {"Right ADC", NULL, "Right Capture Select"},
439
440         /* outputs */
441         {"MONOOUT", NULL, "Phone Mixer"},
442         {"HPOUTL", NULL, "Headphone PGA"},
443         {"Headphone PGA", NULL, "Left HP Mixer"},
444         {"HPOUTR", NULL, "Headphone PGA"},
445         {"Headphone PGA", NULL, "Right HP Mixer"},
446
447         /* mono hp mixer */
448         {"Mono HP Mixer", NULL, "Left HP Mixer"},
449         {"Mono HP Mixer", NULL, "Right HP Mixer"},
450
451         /* Out3 Mux */
452         {"Out3 Mux", "Left", "Left HP Mixer"},
453         {"Out3 Mux", "Mono", "Phone Mixer"},
454         {"Out3 Mux", "Left + Right", "Mono HP Mixer"},
455         {"Out 3 PGA", NULL, "Out3 Mux"},
456         {"OUT3", NULL, "Out 3 PGA"},
457
458         /* speaker Mux */
459         {"Speaker Mux", "Speaker Mix", "Speaker Mixer"},
460         {"Speaker Mux", "Headphone Mix", "Mono HP Mixer"},
461         {"Speaker PGA", NULL, "Speaker Mux"},
462         {"LOUT2", NULL, "Speaker PGA"},
463         {"ROUT2", NULL, "Speaker PGA"},
464
465         {NULL, NULL, NULL},
466 };
467
468 static int wm9712_add_widgets(struct snd_soc_codec *codec)
469 {
470         int i;
471
472         for(i = 0; i < ARRAY_SIZE(wm9712_dapm_widgets); i++) {
473                 snd_soc_dapm_new_control(codec, &wm9712_dapm_widgets[i]);
474         }
475
476         /* set up audio path audio_mapnects */
477         for(i = 0; audio_map[i][0] != NULL; i++) {
478                 snd_soc_dapm_connect_input(codec, audio_map[i][0],
479                         audio_map[i][1], audio_map[i][2]);
480         }
481
482         snd_soc_dapm_new_widgets(codec);
483         return 0;
484 }
485
486 static unsigned int ac97_read(struct snd_soc_codec *codec,
487         unsigned int reg)
488 {
489         u16 *cache = codec->reg_cache;
490
491         if (reg == AC97_RESET || reg == AC97_GPIO_STATUS ||
492                 reg == AC97_VENDOR_ID1 || reg == AC97_VENDOR_ID2 ||
493                 reg == AC97_REC_GAIN)
494                 return soc_ac97_ops.read(codec->ac97, reg);
495         else {
496                 reg = reg >> 1;
497
498                 if (reg > (ARRAY_SIZE(wm9712_reg)))
499                         return -EIO;
500
501                 return cache[reg];
502         }
503 }
504
505 static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
506         unsigned int val)
507 {
508         u16 *cache = codec->reg_cache;
509
510         soc_ac97_ops.write(codec->ac97, reg, val);
511         reg = reg >> 1;
512         if (reg <= (ARRAY_SIZE(wm9712_reg)))
513                 cache[reg] = val;
514
515         return 0;
516 }
517
518 static int ac97_prepare(struct snd_pcm_substream *substream)
519 {
520         struct snd_pcm_runtime *runtime = substream->runtime;
521         struct snd_soc_pcm_runtime *rtd = substream->private_data;
522         struct snd_soc_device *socdev = rtd->socdev;
523         struct snd_soc_codec *codec = socdev->codec;
524         int reg;
525         u16 vra;
526
527         vra = ac97_read(codec, AC97_EXTENDED_STATUS);
528         ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
529
530         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
531                 reg = AC97_PCM_FRONT_DAC_RATE;
532         else
533                 reg = AC97_PCM_LR_ADC_RATE;
534
535         return ac97_write(codec, reg, runtime->rate);
536 }
537
538 static int ac97_aux_prepare(struct snd_pcm_substream *substream)
539 {
540         struct snd_pcm_runtime *runtime = substream->runtime;
541         struct snd_soc_pcm_runtime *rtd = substream->private_data;
542         struct snd_soc_device *socdev = rtd->socdev;
543         struct snd_soc_codec *codec = socdev->codec;
544         u16 vra, xsle;
545
546         vra = ac97_read(codec, AC97_EXTENDED_STATUS);
547         ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
548         xsle = ac97_read(codec, AC97_PCI_SID);
549         ac97_write(codec, AC97_PCI_SID, xsle | 0x8000);
550
551         if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
552                 return -ENODEV;
553
554         return ac97_write(codec, AC97_PCM_SURR_DAC_RATE, runtime->rate);
555 }
556
557 struct snd_soc_codec_dai wm9712_dai[] = {
558 {
559         .name = "AC97 HiFi",
560         .playback = {
561                 .stream_name = "HiFi Playback",
562                 .channels_min = 1,
563                 .channels_max = 2,},
564         .capture = {
565                 .stream_name = "HiFi Capture",
566                 .channels_min = 1,
567                 .channels_max = 2,},
568         .ops = {
569                 .prepare = ac97_prepare,},
570         .caps = {
571                 .num_modes = ARRAY_SIZE(ac97_modes),
572                 .mode = ac97_modes,},
573         },
574         {
575         .name = "AC97 Aux",
576         .playback = {
577                 .stream_name = "Aux Playback",
578                 .channels_min = 1,
579                 .channels_max = 1,},
580         .ops = {
581                 .prepare = ac97_aux_prepare,},
582         .caps = {
583                 .num_modes = ARRAY_SIZE(ac97_modes),
584                 .mode = ac97_modes,},
585         },
586 };
587 EXPORT_SYMBOL_GPL(wm9712_dai);
588
589 static int wm9712_dapm_event(struct snd_soc_codec *codec, int event)
590 {
591         u16 reg;
592
593         switch (event) {
594         case SNDRV_CTL_POWER_D0: /* full On */
595                 /* liam - maybe enable thermal shutdown */
596                 reg = ac97_read(codec, AC97_EXTENDED_MID) & 0xdfff;
597                 ac97_write(codec, AC97_EXTENDED_MID, reg);
598                 break;
599         case SNDRV_CTL_POWER_D1: /* partial On */
600         case SNDRV_CTL_POWER_D2: /* partial On */
601                 break;
602         case SNDRV_CTL_POWER_D3hot: /* Off, with power */
603                 /* enable master bias and vmid */
604                 reg = ac97_read(codec, AC97_EXTENDED_MID) & 0xbbff;
605                 ac97_write(codec, AC97_EXTENDED_MID, reg);
606                 ac97_write(codec, AC97_POWERDOWN, 0x0000);
607                 break;
608         case SNDRV_CTL_POWER_D3cold: /* Off, without power */
609                 /* disable everything including AC link */
610                 ac97_write(codec, AC97_EXTENDED_MID, 0xffff);
611                 ac97_write(codec, AC97_EXTENDED_MSTATUS, 0xffff);
612                 ac97_write(codec, AC97_POWERDOWN, 0xffff);
613                 break;
614         }
615         codec->dapm_state = event;
616         return 0;
617 }
618
619 static int wm9712_reset(struct snd_soc_codec *codec, int try_warm)
620 {
621         if (try_warm && soc_ac97_ops.warm_reset) {
622                 soc_ac97_ops.warm_reset(codec->ac97);
623                 if (!(ac97_read(codec, 0) & 0x8000))
624                         return 1;
625         }
626
627         soc_ac97_ops.reset(codec->ac97);
628         if (ac97_read(codec, 0) & 0x8000)
629                 goto err;
630         return 0;
631
632 err:
633         printk(KERN_ERR "WM9712 AC97 reset failed\n");
634         return -EIO;
635 }
636
637 static int wm9712_soc_suspend(struct platform_device *pdev,
638         pm_message_t state)
639 {
640         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
641         struct snd_soc_codec *codec = socdev->codec;
642
643         wm9712_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
644         return 0;
645 }
646
647 static int wm9712_soc_resume(struct platform_device *pdev)
648 {
649         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
650         struct snd_soc_codec *codec = socdev->codec;
651         int i, ret;
652         u16 *cache = codec->reg_cache;
653
654         ret = wm9712_reset(codec, 1);
655         if (ret < 0){
656                 printk(KERN_ERR "could not reset AC97 codec\n");
657                 return ret;
658         }
659
660         wm9712_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
661
662         if (ret == 0) {
663                 /* Sync reg_cache with the hardware after cold reset */
664                 for (i = 2; i < ARRAY_SIZE(wm9712_reg) << 1; i+=2) {
665                         if (i == AC97_INT_PAGING || i == AC97_POWERDOWN ||
666                                 (i > 0x58 && i != 0x5c))
667                                 continue;
668                         soc_ac97_ops.write(codec->ac97, i, cache[i>>1]);
669                 }
670         }
671
672         if (codec->suspend_dapm_state == SNDRV_CTL_POWER_D0)
673                 wm9712_dapm_event(codec, SNDRV_CTL_POWER_D0);
674
675         return ret;
676 }
677
678 static int wm9712_soc_probe(struct platform_device *pdev)
679 {
680         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
681         struct snd_soc_codec *codec;
682         int ret = 0;
683
684         printk(KERN_INFO "WM9711/WM9712 SoC Audio Codec %s\n", WM9712_VERSION);
685
686         socdev->codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
687         if (socdev->codec == NULL)
688                 return -ENOMEM;
689         codec = socdev->codec;
690         mutex_init(&codec->mutex);
691
692         codec->reg_cache =
693                 kzalloc(sizeof(u16) * ARRAY_SIZE(wm9712_reg), GFP_KERNEL);
694         if (codec->reg_cache == NULL) {
695                 ret = -ENOMEM;
696                 goto cache_err;
697         }
698         memcpy(codec->reg_cache, wm9712_reg, sizeof(u16) * ARRAY_SIZE(wm9712_reg));
699         codec->reg_cache_size = sizeof(u16) * ARRAY_SIZE(wm9712_reg);
700         codec->reg_cache_step = 2;
701
702         codec->name = "WM9712";
703         codec->owner = THIS_MODULE;
704         codec->dai = wm9712_dai;
705         codec->num_dai = ARRAY_SIZE(wm9712_dai);
706         codec->write = ac97_write;
707         codec->read = ac97_read;
708         codec->dapm_event = wm9712_dapm_event;
709         INIT_LIST_HEAD(&codec->dapm_widgets);
710         INIT_LIST_HEAD(&codec->dapm_paths);
711
712         ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
713         if (ret < 0) {
714                 printk(KERN_ERR "wm9712: failed to register AC97 codec\n");
715                 goto codec_err;
716         }
717
718         /* register pcms */
719         ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
720         if (ret < 0)
721                 goto pcm_err;
722
723         ret = wm9712_reset(codec, 0);
724         if (ret < 0) {
725                 printk(KERN_ERR "AC97 link error\n");
726                 goto reset_err;
727         }
728
729         /* set alc mux to none */
730         ac97_write(codec, AC97_VIDEO, ac97_read(codec, AC97_VIDEO) | 0x3000);
731
732         wm9712_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
733         wm9712_add_controls(codec);
734         wm9712_add_widgets(codec);
735         ret = snd_soc_register_card(socdev);
736         if (ret < 0) {
737                 printk(KERN_ERR "wm9712: failed to register card\n");
738                 goto reset_err;
739         }
740
741         return 0;
742
743 reset_err:
744         snd_soc_free_pcms(socdev);
745
746 pcm_err:
747         snd_soc_free_ac97_codec(codec);
748
749 codec_err:
750         kfree(codec->reg_cache);
751
752 cache_err:
753         kfree(socdev->codec);
754         socdev->codec = NULL;
755         return ret;
756 }
757
758 static int wm9712_soc_remove(struct platform_device *pdev)
759 {
760         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
761         struct snd_soc_codec *codec = socdev->codec;
762
763         if (codec == NULL)
764                 return 0;
765
766         snd_soc_dapm_free(socdev);
767         snd_soc_free_pcms(socdev);
768         snd_soc_free_ac97_codec(codec);
769         kfree(codec->reg_cache);
770         kfree(codec);
771         return 0;
772 }
773
774 struct snd_soc_codec_device soc_codec_dev_wm9712 = {
775         .probe =        wm9712_soc_probe,
776         .remove =       wm9712_soc_remove,
777         .suspend =      wm9712_soc_suspend,
778         .resume =       wm9712_soc_resume,
779 };
780
781 EXPORT_SYMBOL_GPL(soc_codec_dev_wm9712);
782
783 MODULE_DESCRIPTION("ASoC WM9711/WM9712 driver");
784 MODULE_AUTHOR("Liam Girdwood");
785 MODULE_LICENSE("GPL");