xref: /openbmc/linux/sound/soc/codecs/wm9713.c (revision b627b4ed)
1 /*
2  * wm9713.c  --  ALSA Soc WM9713 codec support
3  *
4  * Copyright 2006 Wolfson Microelectronics PLC.
5  * Author: Liam Girdwood <lrg@slimlogic.co.uk>
6  *
7  *  This program is free software; you can redistribute  it and/or modify it
8  *  under  the terms of  the GNU General  Public License as published by the
9  *  Free Software Foundation;  either version 2 of the  License, or (at your
10  *  option) any later version.
11  *
12  *  Features:-
13  *
14  *   o Support for AC97 Codec, Voice DAC and Aux DAC
15  *   o Support for DAPM
16  */
17 
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/ac97_codec.h>
24 #include <sound/initval.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/soc-dapm.h>
28 
29 #include "wm9713.h"
30 
31 #define WM9713_VERSION "0.15"
32 
33 struct wm9713_priv {
34 	u32 pll_in; /* PLL input frequency */
35 };
36 
37 static unsigned int ac97_read(struct snd_soc_codec *codec,
38 	unsigned int reg);
39 static int ac97_write(struct snd_soc_codec *codec,
40 	unsigned int reg, unsigned int val);
41 
42 /*
43  * WM9713 register cache
44  * Reg 0x3c bit 15 is used by touch driver.
45  */
46 static const u16 wm9713_reg[] = {
47 	0x6174, 0x8080, 0x8080, 0x8080,
48 	0xc880, 0xe808, 0xe808, 0x0808,
49 	0x00da, 0x8000, 0xd600, 0xaaa0,
50 	0xaaa0, 0xaaa0, 0x0000, 0x0000,
51 	0x0f0f, 0x0040, 0x0000, 0x7f00,
52 	0x0405, 0x0410, 0xbb80, 0xbb80,
53 	0x0000, 0xbb80, 0x0000, 0x4523,
54 	0x0000, 0x2000, 0x7eff, 0xffff,
55 	0x0000, 0x0000, 0x0080, 0x0000,
56 	0x0000, 0x0000, 0xfffe, 0xffff,
57 	0x0000, 0x0000, 0x0000, 0xfffe,
58 	0x4000, 0x0000, 0x0000, 0x0000,
59 	0xb032, 0x3e00, 0x0000, 0x0000,
60 	0x0000, 0x0000, 0x0000, 0x0000,
61 	0x0000, 0x0000, 0x0000, 0x0006,
62 	0x0001, 0x0000, 0x574d, 0x4c13,
63 	0x0000, 0x0000, 0x0000
64 };
65 
66 /* virtual HP mixers regs */
67 #define HPL_MIXER	0x80
68 #define HPR_MIXER	0x82
69 #define MICB_MUX	0x82
70 
71 static const char *wm9713_mic_mixer[] = {"Stereo", "Mic 1", "Mic 2", "Mute"};
72 static const char *wm9713_rec_mux[] = {"Stereo", "Left", "Right", "Mute"};
73 static const char *wm9713_rec_src[] =
74 	{"Mic 1", "Mic 2", "Line", "Mono In", "Headphone", "Speaker",
75 	"Mono Out", "Zh"};
76 static const char *wm9713_rec_gain[] = {"+1.5dB Steps", "+0.75dB Steps"};
77 static const char *wm9713_alc_select[] = {"None", "Left", "Right", "Stereo"};
78 static const char *wm9713_mono_pga[] = {"Vmid", "Zh", "Mono", "Inv",
79 	"Mono Vmid", "Inv Vmid"};
80 static const char *wm9713_spk_pga[] =
81 	{"Vmid", "Zh", "Headphone", "Speaker", "Inv", "Headphone Vmid",
82 	"Speaker Vmid", "Inv Vmid"};
83 static const char *wm9713_hp_pga[] = {"Vmid", "Zh", "Headphone",
84 	"Headphone Vmid"};
85 static const char *wm9713_out3_pga[] = {"Vmid", "Zh", "Inv 1", "Inv 1 Vmid"};
86 static const char *wm9713_out4_pga[] = {"Vmid", "Zh", "Inv 2", "Inv 2 Vmid"};
87 static const char *wm9713_dac_inv[] =
88 	{"Off", "Mono", "Speaker", "Left Headphone", "Right Headphone",
89 	"Headphone Mono", "NC", "Vmid"};
90 static const char *wm9713_bass[] = {"Linear Control", "Adaptive Boost"};
91 static const char *wm9713_ng_type[] = {"Constant Gain", "Mute"};
92 static const char *wm9713_mic_select[] = {"Mic 1", "Mic 2 A", "Mic 2 B"};
93 static const char *wm9713_micb_select[] = {"MPB", "MPA"};
94 
95 static const struct soc_enum wm9713_enum[] = {
96 SOC_ENUM_SINGLE(AC97_LINE, 3, 4, wm9713_mic_mixer), /* record mic mixer 0 */
97 SOC_ENUM_SINGLE(AC97_VIDEO, 14, 4, wm9713_rec_mux), /* record mux hp 1 */
98 SOC_ENUM_SINGLE(AC97_VIDEO, 9, 4, wm9713_rec_mux),  /* record mux mono 2 */
99 SOC_ENUM_SINGLE(AC97_VIDEO, 3, 8, wm9713_rec_src),  /* record mux left 3 */
100 SOC_ENUM_SINGLE(AC97_VIDEO, 0, 8, wm9713_rec_src),  /* record mux right 4*/
101 SOC_ENUM_DOUBLE(AC97_CD, 14, 6, 2, wm9713_rec_gain), /* record step size 5 */
102 SOC_ENUM_SINGLE(AC97_PCI_SVID, 14, 4, wm9713_alc_select), /* alc source select 6*/
103 SOC_ENUM_SINGLE(AC97_REC_GAIN, 14, 4, wm9713_mono_pga), /* mono input select 7 */
104 SOC_ENUM_SINGLE(AC97_REC_GAIN, 11, 8, wm9713_spk_pga), /* speaker left input select 8 */
105 SOC_ENUM_SINGLE(AC97_REC_GAIN, 8, 8, wm9713_spk_pga), /* speaker right input select 9 */
106 SOC_ENUM_SINGLE(AC97_REC_GAIN, 6, 3, wm9713_hp_pga), /* headphone left input 10 */
107 SOC_ENUM_SINGLE(AC97_REC_GAIN, 4, 3, wm9713_hp_pga), /* headphone right input 11 */
108 SOC_ENUM_SINGLE(AC97_REC_GAIN, 2, 4, wm9713_out3_pga), /* out 3 source 12 */
109 SOC_ENUM_SINGLE(AC97_REC_GAIN, 0, 4, wm9713_out4_pga), /* out 4 source 13 */
110 SOC_ENUM_SINGLE(AC97_REC_GAIN_MIC, 13, 8, wm9713_dac_inv), /* dac invert 1 14 */
111 SOC_ENUM_SINGLE(AC97_REC_GAIN_MIC, 10, 8, wm9713_dac_inv), /* dac invert 2 15 */
112 SOC_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 15, 2, wm9713_bass), /* bass control 16 */
113 SOC_ENUM_SINGLE(AC97_PCI_SVID, 5, 2, wm9713_ng_type), /* noise gate type 17 */
114 SOC_ENUM_SINGLE(AC97_3D_CONTROL, 12, 3, wm9713_mic_select), /* mic selection 18 */
115 SOC_ENUM_SINGLE(MICB_MUX, 0, 2, wm9713_micb_select), /* mic selection 19 */
116 };
117 
118 static const struct snd_kcontrol_new wm9713_snd_ac97_controls[] = {
119 SOC_DOUBLE("Speaker Playback Volume", AC97_MASTER, 8, 0, 31, 1),
120 SOC_DOUBLE("Speaker Playback Switch", AC97_MASTER, 15, 7, 1, 1),
121 SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
122 SOC_DOUBLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 7, 1, 1),
123 SOC_DOUBLE("Line In Volume", AC97_PC_BEEP, 8, 0, 31, 1),
124 SOC_DOUBLE("PCM Playback Volume", AC97_PHONE, 8, 0, 31, 1),
125 SOC_SINGLE("Mic 1 Volume", AC97_MIC, 8, 31, 1),
126 SOC_SINGLE("Mic 2 Volume", AC97_MIC, 0, 31, 1),
127 
128 SOC_SINGLE("Mic Boost (+20dB) Switch", AC97_LINE, 5, 1, 0),
129 SOC_SINGLE("Mic Headphone Mixer Volume", AC97_LINE, 0, 7, 1),
130 
131 SOC_SINGLE("Capture Switch", AC97_CD, 15, 1, 1),
132 SOC_ENUM("Capture Volume Steps", wm9713_enum[5]),
133 SOC_DOUBLE("Capture Volume", AC97_CD, 8, 0, 31, 0),
134 SOC_SINGLE("Capture ZC Switch", AC97_CD, 7, 1, 0),
135 
136 SOC_SINGLE("Capture to Headphone Volume", AC97_VIDEO, 11, 7, 1),
137 SOC_SINGLE("Capture to Mono Boost (+20dB) Switch", AC97_VIDEO, 8, 1, 0),
138 SOC_SINGLE("Capture ADC Boost (+20dB) Switch", AC97_VIDEO, 6, 1, 0),
139 
140 SOC_SINGLE("ALC Target Volume", AC97_CODEC_CLASS_REV, 12, 15, 0),
141 SOC_SINGLE("ALC Hold Time", AC97_CODEC_CLASS_REV, 8, 15, 0),
142 SOC_SINGLE("ALC Decay Time", AC97_CODEC_CLASS_REV, 4, 15, 0),
143 SOC_SINGLE("ALC Attack Time", AC97_CODEC_CLASS_REV, 0, 15, 0),
144 SOC_ENUM("ALC Function", wm9713_enum[6]),
145 SOC_SINGLE("ALC Max Volume", AC97_PCI_SVID, 11, 7, 0),
146 SOC_SINGLE("ALC ZC Timeout", AC97_PCI_SVID, 9, 3, 0),
147 SOC_SINGLE("ALC ZC Switch", AC97_PCI_SVID, 8, 1, 0),
148 SOC_SINGLE("ALC NG Switch", AC97_PCI_SVID, 7, 1, 0),
149 SOC_ENUM("ALC NG Type", wm9713_enum[17]),
150 SOC_SINGLE("ALC NG Threshold", AC97_PCI_SVID, 0, 31, 0),
151 
152 SOC_DOUBLE("Speaker Playback ZC Switch", AC97_MASTER, 14, 6, 1, 0),
153 SOC_DOUBLE("Headphone Playback ZC Switch", AC97_HEADPHONE, 14, 6, 1, 0),
154 
155 SOC_SINGLE("Out4 Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
156 SOC_SINGLE("Out4 Playback ZC Switch", AC97_MASTER_MONO, 14, 1, 0),
157 SOC_SINGLE("Out4 Playback Volume", AC97_MASTER_MONO, 8, 63, 1),
158 
159 SOC_SINGLE("Out3 Playback Switch", AC97_MASTER_MONO, 7, 1, 1),
160 SOC_SINGLE("Out3 Playback ZC Switch", AC97_MASTER_MONO, 6, 1, 0),
161 SOC_SINGLE("Out3 Playback Volume", AC97_MASTER_MONO, 0, 63, 1),
162 
163 SOC_SINGLE("Mono Capture Volume", AC97_MASTER_TONE, 8, 31, 1),
164 SOC_SINGLE("Mono Playback Switch", AC97_MASTER_TONE, 7, 1, 1),
165 SOC_SINGLE("Mono Playback ZC Switch", AC97_MASTER_TONE, 6, 1, 0),
166 SOC_SINGLE("Mono Playback Volume", AC97_MASTER_TONE, 0, 31, 1),
167 
168 SOC_SINGLE("PC Beep Playback Headphone Volume", AC97_AUX, 12, 7, 1),
169 SOC_SINGLE("PC Beep Playback Speaker Volume", AC97_AUX, 8, 7, 1),
170 SOC_SINGLE("PC Beep Playback Mono Volume", AC97_AUX, 4, 7, 1),
171 
172 SOC_SINGLE("Voice Playback Headphone Volume", AC97_PCM, 12, 7, 1),
173 SOC_SINGLE("Voice Playback Master Volume", AC97_PCM, 8, 7, 1),
174 SOC_SINGLE("Voice Playback Mono Volume", AC97_PCM, 4, 7, 1),
175 
176 SOC_SINGLE("Aux Playback Headphone Volume", AC97_REC_SEL, 12, 7, 1),
177 SOC_SINGLE("Aux Playback Master Volume", AC97_REC_SEL, 8, 7, 1),
178 SOC_SINGLE("Aux Playback Mono Volume", AC97_REC_SEL, 4, 7, 1),
179 
180 SOC_ENUM("Bass Control", wm9713_enum[16]),
181 SOC_SINGLE("Bass Cut-off Switch", AC97_GENERAL_PURPOSE, 12, 1, 1),
182 SOC_SINGLE("Tone Cut-off Switch", AC97_GENERAL_PURPOSE, 4, 1, 1),
183 SOC_SINGLE("Playback Attenuate (-6dB) Switch", AC97_GENERAL_PURPOSE, 6, 1, 0),
184 SOC_SINGLE("Bass Volume", AC97_GENERAL_PURPOSE, 8, 15, 1),
185 SOC_SINGLE("Tone Volume", AC97_GENERAL_PURPOSE, 0, 15, 1),
186 
187 SOC_SINGLE("3D Upper Cut-off Switch", AC97_REC_GAIN_MIC, 5, 1, 0),
188 SOC_SINGLE("3D Lower Cut-off Switch", AC97_REC_GAIN_MIC, 4, 1, 0),
189 SOC_SINGLE("3D Depth", AC97_REC_GAIN_MIC, 0, 15, 1),
190 };
191 
192 /* We have to create a fake left and right HP mixers because
193  * the codec only has a single control that is shared by both channels.
194  * This makes it impossible to determine the audio path using the current
195  * register map, thus we add a new (virtual) register to help determine the
196  * audio route within the device.
197  */
198 static int mixer_event(struct snd_soc_dapm_widget *w,
199 	struct snd_kcontrol *kcontrol, int event)
200 {
201 	u16 l, r, beep, tone, phone, rec, pcm, aux;
202 
203 	l = ac97_read(w->codec, HPL_MIXER);
204 	r = ac97_read(w->codec, HPR_MIXER);
205 	beep = ac97_read(w->codec, AC97_PC_BEEP);
206 	tone = ac97_read(w->codec, AC97_MASTER_TONE);
207 	phone = ac97_read(w->codec, AC97_PHONE);
208 	rec = ac97_read(w->codec, AC97_REC_SEL);
209 	pcm = ac97_read(w->codec, AC97_PCM);
210 	aux = ac97_read(w->codec, AC97_AUX);
211 
212 	if (event & SND_SOC_DAPM_PRE_REG)
213 		return 0;
214 	if ((l & 0x1) || (r & 0x1))
215 		ac97_write(w->codec, AC97_PC_BEEP, beep & 0x7fff);
216 	else
217 		ac97_write(w->codec, AC97_PC_BEEP, beep | 0x8000);
218 
219 	if ((l & 0x2) || (r & 0x2))
220 		ac97_write(w->codec, AC97_MASTER_TONE, tone & 0x7fff);
221 	else
222 		ac97_write(w->codec, AC97_MASTER_TONE, tone | 0x8000);
223 
224 	if ((l & 0x4) || (r & 0x4))
225 		ac97_write(w->codec, AC97_PHONE, phone & 0x7fff);
226 	else
227 		ac97_write(w->codec, AC97_PHONE, phone | 0x8000);
228 
229 	if ((l & 0x8) || (r & 0x8))
230 		ac97_write(w->codec, AC97_REC_SEL, rec & 0x7fff);
231 	else
232 		ac97_write(w->codec, AC97_REC_SEL, rec | 0x8000);
233 
234 	if ((l & 0x10) || (r & 0x10))
235 		ac97_write(w->codec, AC97_PCM, pcm & 0x7fff);
236 	else
237 		ac97_write(w->codec, AC97_PCM, pcm | 0x8000);
238 
239 	if ((l & 0x20) || (r & 0x20))
240 		ac97_write(w->codec, AC97_AUX, aux & 0x7fff);
241 	else
242 		ac97_write(w->codec, AC97_AUX, aux | 0x8000);
243 
244 	return 0;
245 }
246 
247 /* Left Headphone Mixers */
248 static const struct snd_kcontrol_new wm9713_hpl_mixer_controls[] = {
249 SOC_DAPM_SINGLE("PC Beep Playback Switch", HPL_MIXER, 5, 1, 0),
250 SOC_DAPM_SINGLE("Voice Playback Switch", HPL_MIXER, 4, 1, 0),
251 SOC_DAPM_SINGLE("Aux Playback Switch", HPL_MIXER, 3, 1, 0),
252 SOC_DAPM_SINGLE("PCM Playback Switch", HPL_MIXER, 2, 1, 0),
253 SOC_DAPM_SINGLE("MonoIn Playback Switch", HPL_MIXER, 1, 1, 0),
254 SOC_DAPM_SINGLE("Bypass Playback Switch", HPL_MIXER, 0, 1, 0),
255 };
256 
257 /* Right Headphone Mixers */
258 static const struct snd_kcontrol_new wm9713_hpr_mixer_controls[] = {
259 SOC_DAPM_SINGLE("PC Beep Playback Switch", HPR_MIXER, 5, 1, 0),
260 SOC_DAPM_SINGLE("Voice Playback Switch", HPR_MIXER, 4, 1, 0),
261 SOC_DAPM_SINGLE("Aux Playback Switch", HPR_MIXER, 3, 1, 0),
262 SOC_DAPM_SINGLE("PCM Playback Switch", HPR_MIXER, 2, 1, 0),
263 SOC_DAPM_SINGLE("MonoIn Playback Switch", HPR_MIXER, 1, 1, 0),
264 SOC_DAPM_SINGLE("Bypass Playback Switch", HPR_MIXER, 0, 1, 0),
265 };
266 
267 /* headphone capture mux */
268 static const struct snd_kcontrol_new wm9713_hp_rec_mux_controls =
269 SOC_DAPM_ENUM("Route", wm9713_enum[1]);
270 
271 /* headphone mic mux */
272 static const struct snd_kcontrol_new wm9713_hp_mic_mux_controls =
273 SOC_DAPM_ENUM("Route", wm9713_enum[0]);
274 
275 /* Speaker Mixer */
276 static const struct snd_kcontrol_new wm9713_speaker_mixer_controls[] = {
277 SOC_DAPM_SINGLE("PC Beep Playback Switch", AC97_AUX, 11, 1, 1),
278 SOC_DAPM_SINGLE("Voice Playback Switch", AC97_PCM, 11, 1, 1),
279 SOC_DAPM_SINGLE("Aux Playback Switch", AC97_REC_SEL, 11, 1, 1),
280 SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PHONE, 14, 1, 1),
281 SOC_DAPM_SINGLE("MonoIn Playback Switch", AC97_MASTER_TONE, 14, 1, 1),
282 SOC_DAPM_SINGLE("Bypass Playback Switch", AC97_PC_BEEP, 14, 1, 1),
283 };
284 
285 /* Mono Mixer */
286 static const struct snd_kcontrol_new wm9713_mono_mixer_controls[] = {
287 SOC_DAPM_SINGLE("PC Beep Playback Switch", AC97_AUX, 7, 1, 1),
288 SOC_DAPM_SINGLE("Voice Playback Switch", AC97_PCM, 7, 1, 1),
289 SOC_DAPM_SINGLE("Aux Playback Switch", AC97_REC_SEL, 7, 1, 1),
290 SOC_DAPM_SINGLE("PCM Playback Switch", AC97_PHONE, 13, 1, 1),
291 SOC_DAPM_SINGLE("MonoIn Playback Switch", AC97_MASTER_TONE, 13, 1, 1),
292 SOC_DAPM_SINGLE("Bypass Playback Switch", AC97_PC_BEEP, 13, 1, 1),
293 SOC_DAPM_SINGLE("Mic 1 Sidetone Switch", AC97_LINE, 7, 1, 1),
294 SOC_DAPM_SINGLE("Mic 2 Sidetone Switch", AC97_LINE, 6, 1, 1),
295 };
296 
297 /* mono mic mux */
298 static const struct snd_kcontrol_new wm9713_mono_mic_mux_controls =
299 SOC_DAPM_ENUM("Route", wm9713_enum[2]);
300 
301 /* mono output mux */
302 static const struct snd_kcontrol_new wm9713_mono_mux_controls =
303 SOC_DAPM_ENUM("Route", wm9713_enum[7]);
304 
305 /* speaker left output mux */
306 static const struct snd_kcontrol_new wm9713_hp_spkl_mux_controls =
307 SOC_DAPM_ENUM("Route", wm9713_enum[8]);
308 
309 /* speaker right output mux */
310 static const struct snd_kcontrol_new wm9713_hp_spkr_mux_controls =
311 SOC_DAPM_ENUM("Route", wm9713_enum[9]);
312 
313 /* headphone left output mux */
314 static const struct snd_kcontrol_new wm9713_hpl_out_mux_controls =
315 SOC_DAPM_ENUM("Route", wm9713_enum[10]);
316 
317 /* headphone right output mux */
318 static const struct snd_kcontrol_new wm9713_hpr_out_mux_controls =
319 SOC_DAPM_ENUM("Route", wm9713_enum[11]);
320 
321 /* Out3 mux */
322 static const struct snd_kcontrol_new wm9713_out3_mux_controls =
323 SOC_DAPM_ENUM("Route", wm9713_enum[12]);
324 
325 /* Out4 mux */
326 static const struct snd_kcontrol_new wm9713_out4_mux_controls =
327 SOC_DAPM_ENUM("Route", wm9713_enum[13]);
328 
329 /* DAC inv mux 1 */
330 static const struct snd_kcontrol_new wm9713_dac_inv1_mux_controls =
331 SOC_DAPM_ENUM("Route", wm9713_enum[14]);
332 
333 /* DAC inv mux 2 */
334 static const struct snd_kcontrol_new wm9713_dac_inv2_mux_controls =
335 SOC_DAPM_ENUM("Route", wm9713_enum[15]);
336 
337 /* Capture source left */
338 static const struct snd_kcontrol_new wm9713_rec_srcl_mux_controls =
339 SOC_DAPM_ENUM("Route", wm9713_enum[3]);
340 
341 /* Capture source right */
342 static const struct snd_kcontrol_new wm9713_rec_srcr_mux_controls =
343 SOC_DAPM_ENUM("Route", wm9713_enum[4]);
344 
345 /* mic source */
346 static const struct snd_kcontrol_new wm9713_mic_sel_mux_controls =
347 SOC_DAPM_ENUM("Route", wm9713_enum[18]);
348 
349 /* mic source B virtual control */
350 static const struct snd_kcontrol_new wm9713_micb_sel_mux_controls =
351 SOC_DAPM_ENUM("Route", wm9713_enum[19]);
352 
353 static const struct snd_soc_dapm_widget wm9713_dapm_widgets[] = {
354 SND_SOC_DAPM_MUX("Capture Headphone Mux", SND_SOC_NOPM, 0, 0,
355 	&wm9713_hp_rec_mux_controls),
356 SND_SOC_DAPM_MUX("Sidetone Mux", SND_SOC_NOPM, 0, 0,
357 	&wm9713_hp_mic_mux_controls),
358 SND_SOC_DAPM_MUX("Capture Mono Mux", SND_SOC_NOPM, 0, 0,
359 	&wm9713_mono_mic_mux_controls),
360 SND_SOC_DAPM_MUX("Mono Out Mux", SND_SOC_NOPM, 0, 0,
361 	&wm9713_mono_mux_controls),
362 SND_SOC_DAPM_MUX("Left Speaker Out Mux", SND_SOC_NOPM, 0, 0,
363 	&wm9713_hp_spkl_mux_controls),
364 SND_SOC_DAPM_MUX("Right Speaker Out Mux", SND_SOC_NOPM, 0, 0,
365 	&wm9713_hp_spkr_mux_controls),
366 SND_SOC_DAPM_MUX("Left Headphone Out Mux", SND_SOC_NOPM, 0, 0,
367 	&wm9713_hpl_out_mux_controls),
368 SND_SOC_DAPM_MUX("Right Headphone Out Mux", SND_SOC_NOPM, 0, 0,
369 	&wm9713_hpr_out_mux_controls),
370 SND_SOC_DAPM_MUX("Out 3 Mux", SND_SOC_NOPM, 0, 0,
371 	&wm9713_out3_mux_controls),
372 SND_SOC_DAPM_MUX("Out 4 Mux", SND_SOC_NOPM, 0, 0,
373 	&wm9713_out4_mux_controls),
374 SND_SOC_DAPM_MUX("DAC Inv Mux 1", SND_SOC_NOPM, 0, 0,
375 	&wm9713_dac_inv1_mux_controls),
376 SND_SOC_DAPM_MUX("DAC Inv Mux 2", SND_SOC_NOPM, 0, 0,
377 	&wm9713_dac_inv2_mux_controls),
378 SND_SOC_DAPM_MUX("Left Capture Source", SND_SOC_NOPM, 0, 0,
379 	&wm9713_rec_srcl_mux_controls),
380 SND_SOC_DAPM_MUX("Right Capture Source", SND_SOC_NOPM, 0, 0,
381 	&wm9713_rec_srcr_mux_controls),
382 SND_SOC_DAPM_MUX("Mic A Source", SND_SOC_NOPM, 0, 0,
383 	&wm9713_mic_sel_mux_controls),
384 SND_SOC_DAPM_MUX("Mic B Source", SND_SOC_NOPM, 0, 0,
385 	&wm9713_micb_sel_mux_controls),
386 SND_SOC_DAPM_MIXER_E("Left HP Mixer", AC97_EXTENDED_MID, 3, 1,
387 	&wm9713_hpl_mixer_controls[0], ARRAY_SIZE(wm9713_hpl_mixer_controls),
388 	mixer_event, SND_SOC_DAPM_POST_REG),
389 SND_SOC_DAPM_MIXER_E("Right HP Mixer", AC97_EXTENDED_MID, 2, 1,
390 	&wm9713_hpr_mixer_controls[0], ARRAY_SIZE(wm9713_hpr_mixer_controls),
391 	mixer_event, SND_SOC_DAPM_POST_REG),
392 SND_SOC_DAPM_MIXER("Mono Mixer", AC97_EXTENDED_MID, 0, 1,
393 	&wm9713_mono_mixer_controls[0], ARRAY_SIZE(wm9713_mono_mixer_controls)),
394 SND_SOC_DAPM_MIXER("Speaker Mixer", AC97_EXTENDED_MID, 1, 1,
395 	&wm9713_speaker_mixer_controls[0],
396 	ARRAY_SIZE(wm9713_speaker_mixer_controls)),
397 SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", AC97_EXTENDED_MID, 7, 1),
398 SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", AC97_EXTENDED_MID, 6, 1),
399 SND_SOC_DAPM_MIXER("AC97 Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
400 SND_SOC_DAPM_MIXER("HP Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
401 SND_SOC_DAPM_MIXER("Line Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
402 SND_SOC_DAPM_MIXER("Capture Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
403 SND_SOC_DAPM_DAC("Voice DAC", "Voice Playback", AC97_EXTENDED_MID, 12, 1),
404 SND_SOC_DAPM_DAC("Aux DAC", "Aux Playback", AC97_EXTENDED_MID, 11, 1),
405 SND_SOC_DAPM_PGA("Left ADC", AC97_EXTENDED_MID, 5, 1, NULL, 0),
406 SND_SOC_DAPM_PGA("Right ADC", AC97_EXTENDED_MID, 4, 1, NULL, 0),
407 SND_SOC_DAPM_ADC("Left HiFi ADC", "Left HiFi Capture", SND_SOC_NOPM, 0, 0),
408 SND_SOC_DAPM_ADC("Right HiFi ADC", "Right HiFi Capture", SND_SOC_NOPM, 0, 0),
409 SND_SOC_DAPM_ADC("Left Voice ADC", "Left Voice Capture", SND_SOC_NOPM, 0, 0),
410 SND_SOC_DAPM_ADC("Right Voice ADC", "Right Voice Capture", SND_SOC_NOPM, 0, 0),
411 SND_SOC_DAPM_PGA("Left Headphone", AC97_EXTENDED_MSTATUS, 10, 1, NULL, 0),
412 SND_SOC_DAPM_PGA("Right Headphone", AC97_EXTENDED_MSTATUS, 9, 1, NULL, 0),
413 SND_SOC_DAPM_PGA("Left Speaker", AC97_EXTENDED_MSTATUS, 8, 1, NULL, 0),
414 SND_SOC_DAPM_PGA("Right Speaker", AC97_EXTENDED_MSTATUS, 7, 1, NULL, 0),
415 SND_SOC_DAPM_PGA("Out 3", AC97_EXTENDED_MSTATUS, 11, 1, NULL, 0),
416 SND_SOC_DAPM_PGA("Out 4", AC97_EXTENDED_MSTATUS, 12, 1, NULL, 0),
417 SND_SOC_DAPM_PGA("Mono Out", AC97_EXTENDED_MSTATUS, 13, 1, NULL, 0),
418 SND_SOC_DAPM_PGA("Left Line In", AC97_EXTENDED_MSTATUS, 6, 1, NULL, 0),
419 SND_SOC_DAPM_PGA("Right Line In", AC97_EXTENDED_MSTATUS, 5, 1, NULL, 0),
420 SND_SOC_DAPM_PGA("Mono In", AC97_EXTENDED_MSTATUS, 4, 1, NULL, 0),
421 SND_SOC_DAPM_PGA("Mic A PGA", AC97_EXTENDED_MSTATUS, 3, 1, NULL, 0),
422 SND_SOC_DAPM_PGA("Mic B PGA", AC97_EXTENDED_MSTATUS, 2, 1, NULL, 0),
423 SND_SOC_DAPM_PGA("Mic A Pre Amp", AC97_EXTENDED_MSTATUS, 1, 1, NULL, 0),
424 SND_SOC_DAPM_PGA("Mic B Pre Amp", AC97_EXTENDED_MSTATUS, 0, 1, NULL, 0),
425 SND_SOC_DAPM_MICBIAS("Mic Bias", AC97_EXTENDED_MSTATUS, 14, 1),
426 SND_SOC_DAPM_OUTPUT("MONO"),
427 SND_SOC_DAPM_OUTPUT("HPL"),
428 SND_SOC_DAPM_OUTPUT("HPR"),
429 SND_SOC_DAPM_OUTPUT("SPKL"),
430 SND_SOC_DAPM_OUTPUT("SPKR"),
431 SND_SOC_DAPM_OUTPUT("OUT3"),
432 SND_SOC_DAPM_OUTPUT("OUT4"),
433 SND_SOC_DAPM_INPUT("LINEL"),
434 SND_SOC_DAPM_INPUT("LINER"),
435 SND_SOC_DAPM_INPUT("MONOIN"),
436 SND_SOC_DAPM_INPUT("PCBEEP"),
437 SND_SOC_DAPM_INPUT("MIC1"),
438 SND_SOC_DAPM_INPUT("MIC2A"),
439 SND_SOC_DAPM_INPUT("MIC2B"),
440 SND_SOC_DAPM_VMID("VMID"),
441 };
442 
443 static const struct snd_soc_dapm_route audio_map[] = {
444 	/* left HP mixer */
445 	{"Left HP Mixer", "PC Beep Playback Switch", "PCBEEP"},
446 	{"Left HP Mixer", "Voice Playback Switch",   "Voice DAC"},
447 	{"Left HP Mixer", "Aux Playback Switch",     "Aux DAC"},
448 	{"Left HP Mixer", "Bypass Playback Switch",  "Left Line In"},
449 	{"Left HP Mixer", "PCM Playback Switch",     "Left DAC"},
450 	{"Left HP Mixer", "MonoIn Playback Switch",  "Mono In"},
451 	{"Left HP Mixer", NULL,  "Capture Headphone Mux"},
452 
453 	/* right HP mixer */
454 	{"Right HP Mixer", "PC Beep Playback Switch", "PCBEEP"},
455 	{"Right HP Mixer", "Voice Playback Switch",   "Voice DAC"},
456 	{"Right HP Mixer", "Aux Playback Switch",     "Aux DAC"},
457 	{"Right HP Mixer", "Bypass Playback Switch",  "Right Line In"},
458 	{"Right HP Mixer", "PCM Playback Switch",     "Right DAC"},
459 	{"Right HP Mixer", "MonoIn Playback Switch",  "Mono In"},
460 	{"Right HP Mixer", NULL,  "Capture Headphone Mux"},
461 
462 	/* virtual mixer - mixes left & right channels for spk and mono */
463 	{"AC97 Mixer", NULL, "Left DAC"},
464 	{"AC97 Mixer", NULL, "Right DAC"},
465 	{"Line Mixer", NULL, "Right Line In"},
466 	{"Line Mixer", NULL, "Left Line In"},
467 	{"HP Mixer", NULL, "Left HP Mixer"},
468 	{"HP Mixer", NULL, "Right HP Mixer"},
469 	{"Capture Mixer", NULL, "Left Capture Source"},
470 	{"Capture Mixer", NULL, "Right Capture Source"},
471 
472 	/* speaker mixer */
473 	{"Speaker Mixer", "PC Beep Playback Switch", "PCBEEP"},
474 	{"Speaker Mixer", "Voice Playback Switch",   "Voice DAC"},
475 	{"Speaker Mixer", "Aux Playback Switch",     "Aux DAC"},
476 	{"Speaker Mixer", "Bypass Playback Switch",  "Line Mixer"},
477 	{"Speaker Mixer", "PCM Playback Switch",     "AC97 Mixer"},
478 	{"Speaker Mixer", "MonoIn Playback Switch",  "Mono In"},
479 
480 	/* mono mixer */
481 	{"Mono Mixer", "PC Beep Playback Switch", "PCBEEP"},
482 	{"Mono Mixer", "Voice Playback Switch",   "Voice DAC"},
483 	{"Mono Mixer", "Aux Playback Switch",     "Aux DAC"},
484 	{"Mono Mixer", "Bypass Playback Switch",  "Line Mixer"},
485 	{"Mono Mixer", "PCM Playback Switch",     "AC97 Mixer"},
486 	{"Mono Mixer", "Mic 1 Sidetone Switch", "Mic A PGA"},
487 	{"Mono Mixer", "Mic 2 Sidetone Switch", "Mic B PGA"},
488 	{"Mono Mixer", NULL,  "Capture Mono Mux"},
489 
490 	/* DAC inv mux 1 */
491 	{"DAC Inv Mux 1", "Mono", "Mono Mixer"},
492 	{"DAC Inv Mux 1", "Speaker", "Speaker Mixer"},
493 	{"DAC Inv Mux 1", "Left Headphone", "Left HP Mixer"},
494 	{"DAC Inv Mux 1", "Right Headphone", "Right HP Mixer"},
495 	{"DAC Inv Mux 1", "Headphone Mono", "HP Mixer"},
496 
497 	/* DAC inv mux 2 */
498 	{"DAC Inv Mux 2", "Mono", "Mono Mixer"},
499 	{"DAC Inv Mux 2", "Speaker", "Speaker Mixer"},
500 	{"DAC Inv Mux 2", "Left Headphone", "Left HP Mixer"},
501 	{"DAC Inv Mux 2", "Right Headphone", "Right HP Mixer"},
502 	{"DAC Inv Mux 2", "Headphone Mono", "HP Mixer"},
503 
504 	/* headphone left mux */
505 	{"Left Headphone Out Mux", "Headphone", "Left HP Mixer"},
506 
507 	/* headphone right mux */
508 	{"Right Headphone Out Mux", "Headphone", "Right HP Mixer"},
509 
510 	/* speaker left mux */
511 	{"Left Speaker Out Mux", "Headphone", "Left HP Mixer"},
512 	{"Left Speaker Out Mux", "Speaker", "Speaker Mixer"},
513 	{"Left Speaker Out Mux", "Inv", "DAC Inv Mux 1"},
514 
515 	/* speaker right mux */
516 	{"Right Speaker Out Mux", "Headphone", "Right HP Mixer"},
517 	{"Right Speaker Out Mux", "Speaker", "Speaker Mixer"},
518 	{"Right Speaker Out Mux", "Inv", "DAC Inv Mux 2"},
519 
520 	/* mono mux */
521 	{"Mono Out Mux", "Mono", "Mono Mixer"},
522 	{"Mono Out Mux", "Inv", "DAC Inv Mux 1"},
523 
524 	/* out 3 mux */
525 	{"Out 3 Mux", "Inv 1", "DAC Inv Mux 1"},
526 
527 	/* out 4 mux */
528 	{"Out 4 Mux", "Inv 2", "DAC Inv Mux 2"},
529 
530 	/* output pga */
531 	{"HPL", NULL, "Left Headphone"},
532 	{"Left Headphone", NULL, "Left Headphone Out Mux"},
533 	{"HPR", NULL, "Right Headphone"},
534 	{"Right Headphone", NULL, "Right Headphone Out Mux"},
535 	{"OUT3", NULL, "Out 3"},
536 	{"Out 3", NULL, "Out 3 Mux"},
537 	{"OUT4", NULL, "Out 4"},
538 	{"Out 4", NULL, "Out 4 Mux"},
539 	{"SPKL", NULL, "Left Speaker"},
540 	{"Left Speaker", NULL, "Left Speaker Out Mux"},
541 	{"SPKR", NULL, "Right Speaker"},
542 	{"Right Speaker", NULL, "Right Speaker Out Mux"},
543 	{"MONO", NULL, "Mono Out"},
544 	{"Mono Out", NULL, "Mono Out Mux"},
545 
546 	/* input pga */
547 	{"Left Line In", NULL, "LINEL"},
548 	{"Right Line In", NULL, "LINER"},
549 	{"Mono In", NULL, "MONOIN"},
550 	{"Mic A PGA", NULL, "Mic A Pre Amp"},
551 	{"Mic B PGA", NULL, "Mic B Pre Amp"},
552 
553 	/* left capture select */
554 	{"Left Capture Source", "Mic 1", "Mic A Pre Amp"},
555 	{"Left Capture Source", "Mic 2", "Mic B Pre Amp"},
556 	{"Left Capture Source", "Line", "LINEL"},
557 	{"Left Capture Source", "Mono In", "MONOIN"},
558 	{"Left Capture Source", "Headphone", "Left HP Mixer"},
559 	{"Left Capture Source", "Speaker", "Speaker Mixer"},
560 	{"Left Capture Source", "Mono Out", "Mono Mixer"},
561 
562 	/* right capture select */
563 	{"Right Capture Source", "Mic 1", "Mic A Pre Amp"},
564 	{"Right Capture Source", "Mic 2", "Mic B Pre Amp"},
565 	{"Right Capture Source", "Line", "LINER"},
566 	{"Right Capture Source", "Mono In", "MONOIN"},
567 	{"Right Capture Source", "Headphone", "Right HP Mixer"},
568 	{"Right Capture Source", "Speaker", "Speaker Mixer"},
569 	{"Right Capture Source", "Mono Out", "Mono Mixer"},
570 
571 	/* left ADC */
572 	{"Left ADC", NULL, "Left Capture Source"},
573 	{"Left Voice ADC", NULL, "Left ADC"},
574 	{"Left HiFi ADC", NULL, "Left ADC"},
575 
576 	/* right ADC */
577 	{"Right ADC", NULL, "Right Capture Source"},
578 	{"Right Voice ADC", NULL, "Right ADC"},
579 	{"Right HiFi ADC", NULL, "Right ADC"},
580 
581 	/* mic */
582 	{"Mic A Pre Amp", NULL, "Mic A Source"},
583 	{"Mic A Source", "Mic 1", "MIC1"},
584 	{"Mic A Source", "Mic 2 A", "MIC2A"},
585 	{"Mic A Source", "Mic 2 B", "Mic B Source"},
586 	{"Mic B Pre Amp", "MPB", "Mic B Source"},
587 	{"Mic B Source", NULL, "MIC2B"},
588 
589 	/* headphone capture */
590 	{"Capture Headphone Mux", "Stereo", "Capture Mixer"},
591 	{"Capture Headphone Mux", "Left", "Left Capture Source"},
592 	{"Capture Headphone Mux", "Right", "Right Capture Source"},
593 
594 	/* mono capture */
595 	{"Capture Mono Mux", "Stereo", "Capture Mixer"},
596 	{"Capture Mono Mux", "Left", "Left Capture Source"},
597 	{"Capture Mono Mux", "Right", "Right Capture Source"},
598 };
599 
600 static int wm9713_add_widgets(struct snd_soc_codec *codec)
601 {
602 	snd_soc_dapm_new_controls(codec, wm9713_dapm_widgets,
603 				  ARRAY_SIZE(wm9713_dapm_widgets));
604 
605 	snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
606 
607 	snd_soc_dapm_new_widgets(codec);
608 	return 0;
609 }
610 
611 static unsigned int ac97_read(struct snd_soc_codec *codec,
612 	unsigned int reg)
613 {
614 	u16 *cache = codec->reg_cache;
615 
616 	if (reg == AC97_RESET || reg == AC97_GPIO_STATUS ||
617 		reg == AC97_VENDOR_ID1 || reg == AC97_VENDOR_ID2 ||
618 		reg == AC97_CD)
619 		return soc_ac97_ops.read(codec->ac97, reg);
620 	else {
621 		reg = reg >> 1;
622 
623 		if (reg >= (ARRAY_SIZE(wm9713_reg)))
624 			return -EIO;
625 
626 		return cache[reg];
627 	}
628 }
629 
630 static int ac97_write(struct snd_soc_codec *codec, unsigned int reg,
631 	unsigned int val)
632 {
633 	u16 *cache = codec->reg_cache;
634 	if (reg < 0x7c)
635 		soc_ac97_ops.write(codec->ac97, reg, val);
636 	reg = reg >> 1;
637 	if (reg < (ARRAY_SIZE(wm9713_reg)))
638 		cache[reg] = val;
639 
640 	return 0;
641 }
642 
643 /* PLL divisors */
644 struct _pll_div {
645 	u32 divsel:1;
646 	u32 divctl:1;
647 	u32 lf:1;
648 	u32 n:4;
649 	u32 k:24;
650 };
651 
652 /* The size in bits of the PLL divide multiplied by 10
653  * to allow rounding later */
654 #define FIXED_PLL_SIZE ((1 << 22) * 10)
655 
656 static void pll_factors(struct _pll_div *pll_div, unsigned int source)
657 {
658 	u64 Kpart;
659 	unsigned int K, Ndiv, Nmod, target;
660 
661 	/* The the PLL output is always 98.304MHz. */
662 	target = 98304000;
663 
664 	/* If the input frequency is over 14.4MHz then scale it down. */
665 	if (source > 14400000) {
666 		source >>= 1;
667 		pll_div->divsel = 1;
668 
669 		if (source > 14400000) {
670 			source >>= 1;
671 			pll_div->divctl = 1;
672 		} else
673 			pll_div->divctl = 0;
674 
675 	} else {
676 		pll_div->divsel = 0;
677 		pll_div->divctl = 0;
678 	}
679 
680 	/* Low frequency sources require an additional divide in the
681 	 * loop.
682 	 */
683 	if (source < 8192000) {
684 		pll_div->lf = 1;
685 		target >>= 2;
686 	} else
687 		pll_div->lf = 0;
688 
689 	Ndiv = target / source;
690 	if ((Ndiv < 5) || (Ndiv > 12))
691 		printk(KERN_WARNING
692 			"WM9713 PLL N value %d out of recommended range!\n",
693 			Ndiv);
694 
695 	pll_div->n = Ndiv;
696 	Nmod = target % source;
697 	Kpart = FIXED_PLL_SIZE * (long long)Nmod;
698 
699 	do_div(Kpart, source);
700 
701 	K = Kpart & 0xFFFFFFFF;
702 
703 	/* Check if we need to round */
704 	if ((K % 10) >= 5)
705 		K += 5;
706 
707 	/* Move down to proper range now rounding is done */
708 	K /= 10;
709 
710 	pll_div->k = K;
711 }
712 
713 /**
714  * Please note that changing the PLL input frequency may require
715  * resynchronisation with the AC97 controller.
716  */
717 static int wm9713_set_pll(struct snd_soc_codec *codec,
718 	int pll_id, unsigned int freq_in, unsigned int freq_out)
719 {
720 	struct wm9713_priv *wm9713 = codec->private_data;
721 	u16 reg, reg2;
722 	struct _pll_div pll_div;
723 
724 	/* turn PLL off ? */
725 	if (freq_in == 0) {
726 		/* disable PLL power and select ext source */
727 		reg = ac97_read(codec, AC97_HANDSET_RATE);
728 		ac97_write(codec, AC97_HANDSET_RATE, reg | 0x0080);
729 		reg = ac97_read(codec, AC97_EXTENDED_MID);
730 		ac97_write(codec, AC97_EXTENDED_MID, reg | 0x0200);
731 		wm9713->pll_in = 0;
732 		return 0;
733 	}
734 
735 	pll_factors(&pll_div, freq_in);
736 
737 	if (pll_div.k == 0) {
738 		reg = (pll_div.n << 12) | (pll_div.lf << 11) |
739 			(pll_div.divsel << 9) | (pll_div.divctl << 8);
740 		ac97_write(codec, AC97_LINE1_LEVEL, reg);
741 	} else {
742 		/* write the fractional k to the reg 0x46 pages */
743 		reg2 = (pll_div.n << 12) | (pll_div.lf << 11) | (1 << 10) |
744 			(pll_div.divsel << 9) | (pll_div.divctl << 8);
745 
746 		/* K [21:20] */
747 		reg = reg2 | (0x5 << 4) | (pll_div.k >> 20);
748 		ac97_write(codec, AC97_LINE1_LEVEL, reg);
749 
750 		/* K [19:16] */
751 		reg = reg2 | (0x4 << 4) | ((pll_div.k >> 16) & 0xf);
752 		ac97_write(codec, AC97_LINE1_LEVEL, reg);
753 
754 		/* K [15:12] */
755 		reg = reg2 | (0x3 << 4) | ((pll_div.k >> 12) & 0xf);
756 		ac97_write(codec, AC97_LINE1_LEVEL, reg);
757 
758 		/* K [11:8] */
759 		reg = reg2 | (0x2 << 4) | ((pll_div.k >> 8) & 0xf);
760 		ac97_write(codec, AC97_LINE1_LEVEL, reg);
761 
762 		/* K [7:4] */
763 		reg = reg2 | (0x1 << 4) | ((pll_div.k >> 4) & 0xf);
764 		ac97_write(codec, AC97_LINE1_LEVEL, reg);
765 
766 		reg = reg2 | (0x0 << 4) | (pll_div.k & 0xf); /* K [3:0] */
767 		ac97_write(codec, AC97_LINE1_LEVEL, reg);
768 	}
769 
770 	/* turn PLL on and select as source */
771 	reg = ac97_read(codec, AC97_EXTENDED_MID);
772 	ac97_write(codec, AC97_EXTENDED_MID, reg & 0xfdff);
773 	reg = ac97_read(codec, AC97_HANDSET_RATE);
774 	ac97_write(codec, AC97_HANDSET_RATE, reg & 0xff7f);
775 	wm9713->pll_in = freq_in;
776 
777 	/* wait 10ms AC97 link frames for the link to stabilise */
778 	schedule_timeout_interruptible(msecs_to_jiffies(10));
779 	return 0;
780 }
781 
782 static int wm9713_set_dai_pll(struct snd_soc_dai *codec_dai,
783 		int pll_id, unsigned int freq_in, unsigned int freq_out)
784 {
785 	struct snd_soc_codec *codec = codec_dai->codec;
786 	return wm9713_set_pll(codec, pll_id, freq_in, freq_out);
787 }
788 
789 /*
790  * Tristate the PCM DAI lines, tristate can be disabled by calling
791  * wm9713_set_dai_fmt()
792  */
793 static int wm9713_set_dai_tristate(struct snd_soc_dai *codec_dai,
794 	int tristate)
795 {
796 	struct snd_soc_codec *codec = codec_dai->codec;
797 	u16 reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0x9fff;
798 
799 	if (tristate)
800 		ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
801 
802 	return 0;
803 }
804 
805 /*
806  * Configure WM9713 clock dividers.
807  * Voice DAC needs 256 FS
808  */
809 static int wm9713_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
810 		int div_id, int div)
811 {
812 	struct snd_soc_codec *codec = codec_dai->codec;
813 	u16 reg;
814 
815 	switch (div_id) {
816 	case WM9713_PCMCLK_DIV:
817 		reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xf0ff;
818 		ac97_write(codec, AC97_HANDSET_RATE, reg | div);
819 		break;
820 	case WM9713_CLKA_MULT:
821 		reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xfffd;
822 		ac97_write(codec, AC97_HANDSET_RATE, reg | div);
823 		break;
824 	case WM9713_CLKB_MULT:
825 		reg = ac97_read(codec, AC97_HANDSET_RATE) & 0xfffb;
826 		ac97_write(codec, AC97_HANDSET_RATE, reg | div);
827 		break;
828 	case WM9713_HIFI_DIV:
829 		reg = ac97_read(codec, AC97_HANDSET_RATE) & 0x8fff;
830 		ac97_write(codec, AC97_HANDSET_RATE, reg | div);
831 		break;
832 	case WM9713_PCMBCLK_DIV:
833 		reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0xf1ff;
834 		ac97_write(codec, AC97_CENTER_LFE_MASTER, reg | div);
835 		break;
836 	case WM9713_PCMCLK_PLL_DIV:
837 		reg = ac97_read(codec, AC97_LINE1_LEVEL) & 0xff80;
838 		ac97_write(codec, AC97_LINE1_LEVEL, reg | 0x60 | div);
839 		break;
840 	case WM9713_HIFI_PLL_DIV:
841 		reg = ac97_read(codec, AC97_LINE1_LEVEL) & 0xff80;
842 		ac97_write(codec, AC97_LINE1_LEVEL, reg | 0x70 | div);
843 		break;
844 	default:
845 		return -EINVAL;
846 	}
847 
848 	return 0;
849 }
850 
851 static int wm9713_set_dai_fmt(struct snd_soc_dai *codec_dai,
852 		unsigned int fmt)
853 {
854 	struct snd_soc_codec *codec = codec_dai->codec;
855 	u16 gpio = ac97_read(codec, AC97_GPIO_CFG) & 0xffc5;
856 	u16 reg = 0x8000;
857 
858 	/* clock masters */
859 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
860 	case SND_SOC_DAIFMT_CBM_CFM:
861 		reg |= 0x4000;
862 		gpio |= 0x0010;
863 		break;
864 	case SND_SOC_DAIFMT_CBM_CFS:
865 		reg |= 0x6000;
866 		gpio |= 0x0018;
867 		break;
868 	case SND_SOC_DAIFMT_CBS_CFS:
869 		reg |= 0x2000;
870 		gpio |= 0x001a;
871 		break;
872 	case SND_SOC_DAIFMT_CBS_CFM:
873 		gpio |= 0x0012;
874 		break;
875 	}
876 
877 	/* clock inversion */
878 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
879 	case SND_SOC_DAIFMT_IB_IF:
880 		reg |= 0x00c0;
881 		break;
882 	case SND_SOC_DAIFMT_IB_NF:
883 		reg |= 0x0080;
884 		break;
885 	case SND_SOC_DAIFMT_NB_IF:
886 		reg |= 0x0040;
887 		break;
888 	}
889 
890 	/* DAI format */
891 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
892 	case SND_SOC_DAIFMT_I2S:
893 		reg |= 0x0002;
894 		break;
895 	case SND_SOC_DAIFMT_RIGHT_J:
896 		break;
897 	case SND_SOC_DAIFMT_LEFT_J:
898 		reg |= 0x0001;
899 		break;
900 	case SND_SOC_DAIFMT_DSP_A:
901 		reg |= 0x0003;
902 		break;
903 	case SND_SOC_DAIFMT_DSP_B:
904 		reg |= 0x0043;
905 		break;
906 	}
907 
908 	ac97_write(codec, AC97_GPIO_CFG, gpio);
909 	ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
910 	return 0;
911 }
912 
913 static int wm9713_pcm_hw_params(struct snd_pcm_substream *substream,
914 				struct snd_pcm_hw_params *params,
915 				struct snd_soc_dai *dai)
916 {
917 	struct snd_soc_codec *codec = dai->codec;
918 	u16 reg = ac97_read(codec, AC97_CENTER_LFE_MASTER) & 0xfff3;
919 
920 	switch (params_format(params)) {
921 	case SNDRV_PCM_FORMAT_S16_LE:
922 		break;
923 	case SNDRV_PCM_FORMAT_S20_3LE:
924 		reg |= 0x0004;
925 		break;
926 	case SNDRV_PCM_FORMAT_S24_LE:
927 		reg |= 0x0008;
928 		break;
929 	case SNDRV_PCM_FORMAT_S32_LE:
930 		reg |= 0x000c;
931 		break;
932 	}
933 
934 	/* enable PCM interface in master mode */
935 	ac97_write(codec, AC97_CENTER_LFE_MASTER, reg);
936 	return 0;
937 }
938 
939 static void wm9713_voiceshutdown(struct snd_pcm_substream *substream,
940 				 struct snd_soc_dai *dai)
941 {
942 	struct snd_soc_codec *codec = dai->codec;
943 	u16 status, rate;
944 
945 	/* Gracefully shut down the voice interface. */
946 	status = ac97_read(codec, AC97_EXTENDED_STATUS) | 0x1000;
947 	rate = ac97_read(codec, AC97_HANDSET_RATE) & 0xF0FF;
948 	ac97_write(codec, AC97_HANDSET_RATE, rate | 0x0200);
949 	schedule_timeout_interruptible(msecs_to_jiffies(1));
950 	ac97_write(codec, AC97_HANDSET_RATE, rate | 0x0F00);
951 	ac97_write(codec, AC97_EXTENDED_MID, status);
952 }
953 
954 static int ac97_hifi_prepare(struct snd_pcm_substream *substream,
955 			     struct snd_soc_dai *dai)
956 {
957 	struct snd_soc_codec *codec = dai->codec;
958 	struct snd_pcm_runtime *runtime = substream->runtime;
959 	int reg;
960 	u16 vra;
961 
962 	vra = ac97_read(codec, AC97_EXTENDED_STATUS);
963 	ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
964 
965 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
966 		reg = AC97_PCM_FRONT_DAC_RATE;
967 	else
968 		reg = AC97_PCM_LR_ADC_RATE;
969 
970 	return ac97_write(codec, reg, runtime->rate);
971 }
972 
973 static int ac97_aux_prepare(struct snd_pcm_substream *substream,
974 			    struct snd_soc_dai *dai)
975 {
976 	struct snd_soc_codec *codec = dai->codec;
977 	struct snd_pcm_runtime *runtime = substream->runtime;
978 	u16 vra, xsle;
979 
980 	vra = ac97_read(codec, AC97_EXTENDED_STATUS);
981 	ac97_write(codec, AC97_EXTENDED_STATUS, vra | 0x1);
982 	xsle = ac97_read(codec, AC97_PCI_SID);
983 	ac97_write(codec, AC97_PCI_SID, xsle | 0x8000);
984 
985 	if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
986 		return -ENODEV;
987 
988 	return ac97_write(codec, AC97_PCM_SURR_DAC_RATE, runtime->rate);
989 }
990 
991 #define WM9713_RATES (SNDRV_PCM_RATE_8000  |	\
992 		      SNDRV_PCM_RATE_11025 |	\
993 		      SNDRV_PCM_RATE_22050 |	\
994 		      SNDRV_PCM_RATE_44100 |	\
995 		      SNDRV_PCM_RATE_48000)
996 
997 #define WM9713_PCM_RATES (SNDRV_PCM_RATE_8000  |	\
998 			  SNDRV_PCM_RATE_11025 |	\
999 			  SNDRV_PCM_RATE_16000 |	\
1000 			  SNDRV_PCM_RATE_22050 |	\
1001 			  SNDRV_PCM_RATE_44100 |	\
1002 			  SNDRV_PCM_RATE_48000)
1003 
1004 #define WM9713_PCM_FORMATS \
1005 	(SNDRV_PCM_FORMAT_S16_LE | SNDRV_PCM_FORMAT_S20_3LE | \
1006 	 SNDRV_PCM_FORMAT_S24_LE)
1007 
1008 static struct snd_soc_dai_ops wm9713_dai_ops_hifi = {
1009 	.prepare	= ac97_hifi_prepare,
1010 	.set_clkdiv	= wm9713_set_dai_clkdiv,
1011 	.set_pll	= wm9713_set_dai_pll,
1012 };
1013 
1014 static struct snd_soc_dai_ops wm9713_dai_ops_aux = {
1015 	.prepare	= ac97_aux_prepare,
1016 	.set_clkdiv	= wm9713_set_dai_clkdiv,
1017 	.set_pll	= wm9713_set_dai_pll,
1018 };
1019 
1020 static struct snd_soc_dai_ops wm9713_dai_ops_voice = {
1021 	.hw_params	= wm9713_pcm_hw_params,
1022 	.shutdown	= wm9713_voiceshutdown,
1023 	.set_clkdiv	= wm9713_set_dai_clkdiv,
1024 	.set_pll	= wm9713_set_dai_pll,
1025 	.set_fmt	= wm9713_set_dai_fmt,
1026 	.set_tristate	= wm9713_set_dai_tristate,
1027 };
1028 
1029 struct snd_soc_dai wm9713_dai[] = {
1030 {
1031 	.name = "AC97 HiFi",
1032 	.ac97_control = 1,
1033 	.playback = {
1034 		.stream_name = "HiFi Playback",
1035 		.channels_min = 1,
1036 		.channels_max = 2,
1037 		.rates = WM9713_RATES,
1038 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
1039 	.capture = {
1040 		.stream_name = "HiFi Capture",
1041 		.channels_min = 1,
1042 		.channels_max = 2,
1043 		.rates = WM9713_RATES,
1044 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
1045 	.ops = &wm9713_dai_ops_hifi,
1046 	},
1047 	{
1048 	.name = "AC97 Aux",
1049 	.playback = {
1050 		.stream_name = "Aux Playback",
1051 		.channels_min = 1,
1052 		.channels_max = 1,
1053 		.rates = WM9713_RATES,
1054 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
1055 	.ops = &wm9713_dai_ops_aux,
1056 	},
1057 	{
1058 	.name = "WM9713 Voice",
1059 	.playback = {
1060 		.stream_name = "Voice Playback",
1061 		.channels_min = 1,
1062 		.channels_max = 1,
1063 		.rates = WM9713_PCM_RATES,
1064 		.formats = WM9713_PCM_FORMATS,},
1065 	.capture = {
1066 		.stream_name = "Voice Capture",
1067 		.channels_min = 1,
1068 		.channels_max = 2,
1069 		.rates = WM9713_PCM_RATES,
1070 		.formats = WM9713_PCM_FORMATS,},
1071 	.ops = &wm9713_dai_ops_voice,
1072 	},
1073 };
1074 EXPORT_SYMBOL_GPL(wm9713_dai);
1075 
1076 int wm9713_reset(struct snd_soc_codec *codec, int try_warm)
1077 {
1078 	if (try_warm && soc_ac97_ops.warm_reset) {
1079 		soc_ac97_ops.warm_reset(codec->ac97);
1080 		if (ac97_read(codec, 0) == wm9713_reg[0])
1081 			return 1;
1082 	}
1083 
1084 	soc_ac97_ops.reset(codec->ac97);
1085 	if (soc_ac97_ops.warm_reset)
1086 		soc_ac97_ops.warm_reset(codec->ac97);
1087 	if (ac97_read(codec, 0) != wm9713_reg[0])
1088 		return -EIO;
1089 	return 0;
1090 }
1091 EXPORT_SYMBOL_GPL(wm9713_reset);
1092 
1093 static int wm9713_set_bias_level(struct snd_soc_codec *codec,
1094 				 enum snd_soc_bias_level level)
1095 {
1096 	u16 reg;
1097 
1098 	switch (level) {
1099 	case SND_SOC_BIAS_ON:
1100 		/* enable thermal shutdown */
1101 		reg = ac97_read(codec, AC97_EXTENDED_MID) & 0x1bff;
1102 		ac97_write(codec, AC97_EXTENDED_MID, reg);
1103 		break;
1104 	case SND_SOC_BIAS_PREPARE:
1105 		break;
1106 	case SND_SOC_BIAS_STANDBY:
1107 		/* enable master bias and vmid */
1108 		reg = ac97_read(codec, AC97_EXTENDED_MID) & 0x3bff;
1109 		ac97_write(codec, AC97_EXTENDED_MID, reg);
1110 		ac97_write(codec, AC97_POWERDOWN, 0x0000);
1111 		break;
1112 	case SND_SOC_BIAS_OFF:
1113 		/* disable everything including AC link */
1114 		ac97_write(codec, AC97_EXTENDED_MID, 0xffff);
1115 		ac97_write(codec, AC97_EXTENDED_MSTATUS, 0xffff);
1116 		ac97_write(codec, AC97_POWERDOWN, 0xffff);
1117 		break;
1118 	}
1119 	codec->bias_level = level;
1120 	return 0;
1121 }
1122 
1123 static int wm9713_soc_suspend(struct platform_device *pdev,
1124 	pm_message_t state)
1125 {
1126 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1127 	struct snd_soc_codec *codec = socdev->card->codec;
1128 	u16 reg;
1129 
1130 	/* Disable everything except touchpanel - that will be handled
1131 	 * by the touch driver and left disabled if touch is not in
1132 	 * use. */
1133 	reg = ac97_read(codec, AC97_EXTENDED_MID);
1134 	ac97_write(codec, AC97_EXTENDED_MID, reg | 0x7fff);
1135 	ac97_write(codec, AC97_EXTENDED_MSTATUS, 0xffff);
1136 	ac97_write(codec, AC97_POWERDOWN, 0x6f00);
1137 	ac97_write(codec, AC97_POWERDOWN, 0xffff);
1138 
1139 	return 0;
1140 }
1141 
1142 static int wm9713_soc_resume(struct platform_device *pdev)
1143 {
1144 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1145 	struct snd_soc_codec *codec = socdev->card->codec;
1146 	struct wm9713_priv *wm9713 = codec->private_data;
1147 	int i, ret;
1148 	u16 *cache = codec->reg_cache;
1149 
1150 	ret = wm9713_reset(codec, 1);
1151 	if (ret < 0) {
1152 		printk(KERN_ERR "could not reset AC97 codec\n");
1153 		return ret;
1154 	}
1155 
1156 	wm9713_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1157 
1158 	/* do we need to re-start the PLL ? */
1159 	if (wm9713->pll_in)
1160 		wm9713_set_pll(codec, 0, wm9713->pll_in, 0);
1161 
1162 	/* only synchronise the codec if warm reset failed */
1163 	if (ret == 0) {
1164 		for (i = 2; i < ARRAY_SIZE(wm9713_reg) << 1; i += 2) {
1165 			if (i == AC97_POWERDOWN || i == AC97_EXTENDED_MID ||
1166 				i == AC97_EXTENDED_MSTATUS || i > 0x66)
1167 				continue;
1168 			soc_ac97_ops.write(codec->ac97, i, cache[i>>1]);
1169 		}
1170 	}
1171 
1172 	if (codec->suspend_bias_level == SND_SOC_BIAS_ON)
1173 		wm9713_set_bias_level(codec, SND_SOC_BIAS_ON);
1174 
1175 	return ret;
1176 }
1177 
1178 static int wm9713_soc_probe(struct platform_device *pdev)
1179 {
1180 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1181 	struct snd_soc_codec *codec;
1182 	int ret = 0, reg;
1183 
1184 	printk(KERN_INFO "WM9713/WM9714 SoC Audio Codec %s\n", WM9713_VERSION);
1185 
1186 	socdev->card->codec = kzalloc(sizeof(struct snd_soc_codec),
1187 				      GFP_KERNEL);
1188 	if (socdev->card->codec == NULL)
1189 		return -ENOMEM;
1190 	codec = socdev->card->codec;
1191 	mutex_init(&codec->mutex);
1192 
1193 	codec->reg_cache = kmemdup(wm9713_reg, sizeof(wm9713_reg), GFP_KERNEL);
1194 	if (codec->reg_cache == NULL) {
1195 		ret = -ENOMEM;
1196 		goto cache_err;
1197 	}
1198 	codec->reg_cache_size = sizeof(wm9713_reg);
1199 	codec->reg_cache_step = 2;
1200 
1201 	codec->private_data = kzalloc(sizeof(struct wm9713_priv), GFP_KERNEL);
1202 	if (codec->private_data == NULL) {
1203 		ret = -ENOMEM;
1204 		goto priv_err;
1205 	}
1206 
1207 	codec->name = "WM9713";
1208 	codec->owner = THIS_MODULE;
1209 	codec->dai = wm9713_dai;
1210 	codec->num_dai = ARRAY_SIZE(wm9713_dai);
1211 	codec->write = ac97_write;
1212 	codec->read = ac97_read;
1213 	codec->set_bias_level = wm9713_set_bias_level;
1214 	INIT_LIST_HEAD(&codec->dapm_widgets);
1215 	INIT_LIST_HEAD(&codec->dapm_paths);
1216 
1217 	ret = snd_soc_new_ac97_codec(codec, &soc_ac97_ops, 0);
1218 	if (ret < 0)
1219 		goto codec_err;
1220 
1221 	/* register pcms */
1222 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
1223 	if (ret < 0)
1224 		goto pcm_err;
1225 
1226 	/* do a cold reset for the controller and then try
1227 	 * a warm reset followed by an optional cold reset for codec */
1228 	wm9713_reset(codec, 0);
1229 	ret = wm9713_reset(codec, 1);
1230 	if (ret < 0) {
1231 		printk(KERN_ERR "Failed to reset WM9713: AC97 link error\n");
1232 		goto reset_err;
1233 	}
1234 
1235 	wm9713_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1236 
1237 	/* unmute the adc - move to kcontrol */
1238 	reg = ac97_read(codec, AC97_CD) & 0x7fff;
1239 	ac97_write(codec, AC97_CD, reg);
1240 
1241 	snd_soc_add_controls(codec, wm9713_snd_ac97_controls,
1242 				ARRAY_SIZE(wm9713_snd_ac97_controls));
1243 	wm9713_add_widgets(codec);
1244 	ret = snd_soc_init_card(socdev);
1245 	if (ret < 0)
1246 		goto reset_err;
1247 	return 0;
1248 
1249 reset_err:
1250 	snd_soc_free_pcms(socdev);
1251 
1252 pcm_err:
1253 	snd_soc_free_ac97_codec(codec);
1254 
1255 codec_err:
1256 	kfree(codec->private_data);
1257 
1258 priv_err:
1259 	kfree(codec->reg_cache);
1260 
1261 cache_err:
1262 	kfree(socdev->card->codec);
1263 	socdev->card->codec = NULL;
1264 	return ret;
1265 }
1266 
1267 static int wm9713_soc_remove(struct platform_device *pdev)
1268 {
1269 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1270 	struct snd_soc_codec *codec = socdev->card->codec;
1271 
1272 	if (codec == NULL)
1273 		return 0;
1274 
1275 	snd_soc_dapm_free(socdev);
1276 	snd_soc_free_pcms(socdev);
1277 	snd_soc_free_ac97_codec(codec);
1278 	kfree(codec->private_data);
1279 	kfree(codec->reg_cache);
1280 	kfree(codec);
1281 	return 0;
1282 }
1283 
1284 struct snd_soc_codec_device soc_codec_dev_wm9713 = {
1285 	.probe = 	wm9713_soc_probe,
1286 	.remove = 	wm9713_soc_remove,
1287 	.suspend =	wm9713_soc_suspend,
1288 	.resume = 	wm9713_soc_resume,
1289 };
1290 EXPORT_SYMBOL_GPL(soc_codec_dev_wm9713);
1291 
1292 MODULE_DESCRIPTION("ASoC WM9713/WM9714 driver");
1293 MODULE_AUTHOR("Liam Girdwood");
1294 MODULE_LICENSE("GPL");
1295