xref: /openbmc/linux/sound/pci/ac97/ac97_patch.c (revision 96de0e252cedffad61b3cb5e05662c591898e69a)
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
3  *  Universal interface for Audio Codec '97
4  *
5  *  For more details look to AC '97 component specification revision 2.2
6  *  by Intel Corporation (http://developer.intel.com) and to datasheets
7  *  for specific codecs.
8  *
9  *
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of the GNU General Public License as published by
12  *   the Free Software Foundation; either version 2 of the License, or
13  *   (at your option) any later version.
14  *
15  *   This program is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with this program; if not, write to the Free Software
22  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  *
24  */
25 
26 #include "ac97_local.h"
27 #include "ac97_patch.h"
28 
29 /*
30  *  Chip specific initialization
31  */
32 
33 static int patch_build_controls(struct snd_ac97 * ac97, const struct snd_kcontrol_new *controls, int count)
34 {
35 	int idx, err;
36 
37 	for (idx = 0; idx < count; idx++)
38 		if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&controls[idx], ac97))) < 0)
39 			return err;
40 	return 0;
41 }
42 
43 /* replace with a new TLV */
44 static void reset_tlv(struct snd_ac97 *ac97, const char *name,
45 		      const unsigned int *tlv)
46 {
47 	struct snd_ctl_elem_id sid;
48 	struct snd_kcontrol *kctl;
49 	memset(&sid, 0, sizeof(sid));
50 	strcpy(sid.name, name);
51 	sid.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
52 	kctl = snd_ctl_find_id(ac97->bus->card, &sid);
53 	if (kctl && kctl->tlv.p)
54 		kctl->tlv.p = tlv;
55 }
56 
57 /* set to the page, update bits and restore the page */
58 static int ac97_update_bits_page(struct snd_ac97 *ac97, unsigned short reg, unsigned short mask, unsigned short value, unsigned short page)
59 {
60 	unsigned short page_save;
61 	int ret;
62 
63 	mutex_lock(&ac97->page_mutex);
64 	page_save = snd_ac97_read(ac97, AC97_INT_PAGING) & AC97_PAGE_MASK;
65 	snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page);
66 	ret = snd_ac97_update_bits(ac97, reg, mask, value);
67 	snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page_save);
68 	mutex_unlock(&ac97->page_mutex); /* unlock paging */
69 	return ret;
70 }
71 
72 /*
73  * shared line-in/mic controls
74  */
75 static int ac97_enum_text_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo,
76 			       const char **texts, unsigned int nums)
77 {
78 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
79 	uinfo->count = 1;
80 	uinfo->value.enumerated.items = nums;
81 	if (uinfo->value.enumerated.item > nums - 1)
82 		uinfo->value.enumerated.item = nums - 1;
83 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
84 	return 0;
85 }
86 
87 static int ac97_surround_jack_mode_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
88 {
89 	static const char *texts[] = { "Shared", "Independent" };
90 	return ac97_enum_text_info(kcontrol, uinfo, texts, 2);
91 }
92 
93 static int ac97_surround_jack_mode_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
94 {
95 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
96 
97 	ucontrol->value.enumerated.item[0] = ac97->indep_surround;
98 	return 0;
99 }
100 
101 static int ac97_surround_jack_mode_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
102 {
103 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
104 	unsigned char indep = !!ucontrol->value.enumerated.item[0];
105 
106 	if (indep != ac97->indep_surround) {
107 		ac97->indep_surround = indep;
108 		if (ac97->build_ops->update_jacks)
109 			ac97->build_ops->update_jacks(ac97);
110 		return 1;
111 	}
112 	return 0;
113 }
114 
115 static int ac97_channel_mode_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
116 {
117 	static const char *texts[] = { "2ch", "4ch", "6ch" };
118 	if (kcontrol->private_value)
119 		return ac97_enum_text_info(kcontrol, uinfo, texts, 2); /* 4ch only */
120 	return ac97_enum_text_info(kcontrol, uinfo, texts, 3);
121 }
122 
123 static int ac97_channel_mode_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
124 {
125 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
126 
127 	ucontrol->value.enumerated.item[0] = ac97->channel_mode;
128 	return 0;
129 }
130 
131 static int ac97_channel_mode_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
132 {
133 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
134 	unsigned char mode = ucontrol->value.enumerated.item[0];
135 
136 	if (mode != ac97->channel_mode) {
137 		ac97->channel_mode = mode;
138 		if (ac97->build_ops->update_jacks)
139 			ac97->build_ops->update_jacks(ac97);
140 		return 1;
141 	}
142 	return 0;
143 }
144 
145 #define AC97_SURROUND_JACK_MODE_CTL \
146 	{ \
147 		.iface	= SNDRV_CTL_ELEM_IFACE_MIXER, \
148 		.name	= "Surround Jack Mode", \
149 		.info = ac97_surround_jack_mode_info, \
150 		.get = ac97_surround_jack_mode_get, \
151 		.put = ac97_surround_jack_mode_put, \
152 	}
153 #define AC97_CHANNEL_MODE_CTL \
154 	{ \
155 		.iface	= SNDRV_CTL_ELEM_IFACE_MIXER, \
156 		.name	= "Channel Mode", \
157 		.info = ac97_channel_mode_info, \
158 		.get = ac97_channel_mode_get, \
159 		.put = ac97_channel_mode_put, \
160 	}
161 #define AC97_CHANNEL_MODE_4CH_CTL \
162 	{ \
163 		.iface	= SNDRV_CTL_ELEM_IFACE_MIXER, \
164 		.name	= "Channel Mode", \
165 		.info = ac97_channel_mode_info, \
166 		.get = ac97_channel_mode_get, \
167 		.put = ac97_channel_mode_put, \
168 		.private_value = 1, \
169 	}
170 
171 static inline int is_surround_on(struct snd_ac97 *ac97)
172 {
173 	return ac97->channel_mode >= 1;
174 }
175 
176 static inline int is_clfe_on(struct snd_ac97 *ac97)
177 {
178 	return ac97->channel_mode >= 2;
179 }
180 
181 /* system has shared jacks with surround out enabled */
182 static inline int is_shared_surrout(struct snd_ac97 *ac97)
183 {
184 	return !ac97->indep_surround && is_surround_on(ac97);
185 }
186 
187 /* system has shared jacks with center/lfe out enabled */
188 static inline int is_shared_clfeout(struct snd_ac97 *ac97)
189 {
190 	return !ac97->indep_surround && is_clfe_on(ac97);
191 }
192 
193 /* system has shared jacks with line in enabled */
194 static inline int is_shared_linein(struct snd_ac97 *ac97)
195 {
196 	return !ac97->indep_surround && !is_surround_on(ac97);
197 }
198 
199 /* system has shared jacks with mic in enabled */
200 static inline int is_shared_micin(struct snd_ac97 *ac97)
201 {
202 	return !ac97->indep_surround && !is_clfe_on(ac97);
203 }
204 
205 
206 /* The following snd_ac97_ymf753_... items added by David Shust (dshust@shustring.com) */
207 /* Modified for YMF743 by Keita Maehara <maehara@debian.org> */
208 
209 /* It is possible to indicate to the Yamaha YMF7x3 the type of
210    speakers being used. */
211 
212 static int snd_ac97_ymf7x3_info_speaker(struct snd_kcontrol *kcontrol,
213 					struct snd_ctl_elem_info *uinfo)
214 {
215 	static char *texts[3] = {
216 		"Standard", "Small", "Smaller"
217 	};
218 
219 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
220 	uinfo->count = 1;
221 	uinfo->value.enumerated.items = 3;
222 	if (uinfo->value.enumerated.item > 2)
223 		uinfo->value.enumerated.item = 2;
224 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
225 	return 0;
226 }
227 
228 static int snd_ac97_ymf7x3_get_speaker(struct snd_kcontrol *kcontrol,
229 				       struct snd_ctl_elem_value *ucontrol)
230 {
231 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
232 	unsigned short val;
233 
234 	val = ac97->regs[AC97_YMF7X3_3D_MODE_SEL];
235 	val = (val >> 10) & 3;
236 	if (val > 0)    /* 0 = invalid */
237 		val--;
238 	ucontrol->value.enumerated.item[0] = val;
239 	return 0;
240 }
241 
242 static int snd_ac97_ymf7x3_put_speaker(struct snd_kcontrol *kcontrol,
243 				       struct snd_ctl_elem_value *ucontrol)
244 {
245 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
246 	unsigned short val;
247 
248 	if (ucontrol->value.enumerated.item[0] > 2)
249 		return -EINVAL;
250 	val = (ucontrol->value.enumerated.item[0] + 1) << 10;
251 	return snd_ac97_update(ac97, AC97_YMF7X3_3D_MODE_SEL, val);
252 }
253 
254 static const struct snd_kcontrol_new snd_ac97_ymf7x3_controls_speaker =
255 {
256 	.iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
257 	.name   = "3D Control - Speaker",
258 	.info   = snd_ac97_ymf7x3_info_speaker,
259 	.get    = snd_ac97_ymf7x3_get_speaker,
260 	.put    = snd_ac97_ymf7x3_put_speaker,
261 };
262 
263 /* It is possible to indicate to the Yamaha YMF7x3 the source to
264    direct to the S/PDIF output. */
265 static int snd_ac97_ymf7x3_spdif_source_info(struct snd_kcontrol *kcontrol,
266 					     struct snd_ctl_elem_info *uinfo)
267 {
268 	static char *texts[2] = { "AC-Link", "A/D Converter" };
269 
270 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
271 	uinfo->count = 1;
272 	uinfo->value.enumerated.items = 2;
273 	if (uinfo->value.enumerated.item > 1)
274 		uinfo->value.enumerated.item = 1;
275 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
276 	return 0;
277 }
278 
279 static int snd_ac97_ymf7x3_spdif_source_get(struct snd_kcontrol *kcontrol,
280 					    struct snd_ctl_elem_value *ucontrol)
281 {
282 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
283 	unsigned short val;
284 
285 	val = ac97->regs[AC97_YMF7X3_DIT_CTRL];
286 	ucontrol->value.enumerated.item[0] = (val >> 1) & 1;
287 	return 0;
288 }
289 
290 static int snd_ac97_ymf7x3_spdif_source_put(struct snd_kcontrol *kcontrol,
291 					    struct snd_ctl_elem_value *ucontrol)
292 {
293 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
294 	unsigned short val;
295 
296 	if (ucontrol->value.enumerated.item[0] > 1)
297 		return -EINVAL;
298 	val = ucontrol->value.enumerated.item[0] << 1;
299 	return snd_ac97_update_bits(ac97, AC97_YMF7X3_DIT_CTRL, 0x0002, val);
300 }
301 
302 static int patch_yamaha_ymf7x3_3d(struct snd_ac97 *ac97)
303 {
304 	struct snd_kcontrol *kctl;
305 	int err;
306 
307 	kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97);
308 	err = snd_ctl_add(ac97->bus->card, kctl);
309 	if (err < 0)
310 		return err;
311 	strcpy(kctl->id.name, "3D Control - Wide");
312 	kctl->private_value = AC97_SINGLE_VALUE(AC97_3D_CONTROL, 9, 7, 0);
313 	snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000);
314 	err = snd_ctl_add(ac97->bus->card,
315 			  snd_ac97_cnew(&snd_ac97_ymf7x3_controls_speaker,
316 					ac97));
317 	if (err < 0)
318 		return err;
319 	snd_ac97_write_cache(ac97, AC97_YMF7X3_3D_MODE_SEL, 0x0c00);
320 	return 0;
321 }
322 
323 static const struct snd_kcontrol_new snd_ac97_yamaha_ymf743_controls_spdif[3] =
324 {
325 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
326 		    AC97_YMF7X3_DIT_CTRL, 0, 1, 0),
327 	{
328 		.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
329 		.name	= SNDRV_CTL_NAME_IEC958("", PLAYBACK, NONE) "Source",
330 		.info	= snd_ac97_ymf7x3_spdif_source_info,
331 		.get	= snd_ac97_ymf7x3_spdif_source_get,
332 		.put	= snd_ac97_ymf7x3_spdif_source_put,
333 	},
334 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("", NONE, NONE) "Mute",
335 		    AC97_YMF7X3_DIT_CTRL, 2, 1, 1)
336 };
337 
338 static int patch_yamaha_ymf743_build_spdif(struct snd_ac97 *ac97)
339 {
340 	int err;
341 
342 	err = patch_build_controls(ac97, &snd_ac97_controls_spdif[0], 3);
343 	if (err < 0)
344 		return err;
345 	err = patch_build_controls(ac97,
346 				   snd_ac97_yamaha_ymf743_controls_spdif, 3);
347 	if (err < 0)
348 		return err;
349 	/* set default PCM S/PDIF params */
350 	/* PCM audio,no copyright,no preemphasis,PCM coder,original */
351 	snd_ac97_write_cache(ac97, AC97_YMF7X3_DIT_CTRL, 0xa201);
352 	return 0;
353 }
354 
355 static struct snd_ac97_build_ops patch_yamaha_ymf743_ops = {
356 	.build_spdif	= patch_yamaha_ymf743_build_spdif,
357 	.build_3d	= patch_yamaha_ymf7x3_3d,
358 };
359 
360 static int patch_yamaha_ymf743(struct snd_ac97 *ac97)
361 {
362 	ac97->build_ops = &patch_yamaha_ymf743_ops;
363 	ac97->caps |= AC97_BC_BASS_TREBLE;
364 	ac97->caps |= 0x04 << 10; /* Yamaha 3D enhancement */
365 	ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000; /* 48k only */
366 	ac97->ext_id |= AC97_EI_SPDIF; /* force the detection of spdif */
367 	return 0;
368 }
369 
370 /* The AC'97 spec states that the S/PDIF signal is to be output at pin 48.
371    The YMF753 will output the S/PDIF signal to pin 43, 47 (EAPD), or 48.
372    By default, no output pin is selected, and the S/PDIF signal is not output.
373    There is also a bit to mute S/PDIF output in a vendor-specific register. */
374 static int snd_ac97_ymf753_spdif_output_pin_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
375 {
376 	static char *texts[3] = { "Disabled", "Pin 43", "Pin 48" };
377 
378 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
379 	uinfo->count = 1;
380 	uinfo->value.enumerated.items = 3;
381 	if (uinfo->value.enumerated.item > 2)
382 		uinfo->value.enumerated.item = 2;
383 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
384 	return 0;
385 }
386 
387 static int snd_ac97_ymf753_spdif_output_pin_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
388 {
389 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
390 	unsigned short val;
391 
392 	val = ac97->regs[AC97_YMF7X3_DIT_CTRL];
393 	ucontrol->value.enumerated.item[0] = (val & 0x0008) ? 2 : (val & 0x0020) ? 1 : 0;
394 	return 0;
395 }
396 
397 static int snd_ac97_ymf753_spdif_output_pin_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
398 {
399 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
400 	unsigned short val;
401 
402 	if (ucontrol->value.enumerated.item[0] > 2)
403 		return -EINVAL;
404 	val = (ucontrol->value.enumerated.item[0] == 2) ? 0x0008 :
405 	      (ucontrol->value.enumerated.item[0] == 1) ? 0x0020 : 0;
406 	return snd_ac97_update_bits(ac97, AC97_YMF7X3_DIT_CTRL, 0x0028, val);
407 	/* The following can be used to direct S/PDIF output to pin 47 (EAPD).
408 	   snd_ac97_write_cache(ac97, 0x62, snd_ac97_read(ac97, 0x62) | 0x0008); */
409 }
410 
411 static const struct snd_kcontrol_new snd_ac97_ymf753_controls_spdif[3] = {
412 	{
413 		.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
414 		.name	= SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
415 		.info	= snd_ac97_ymf7x3_spdif_source_info,
416 		.get	= snd_ac97_ymf7x3_spdif_source_get,
417 		.put	= snd_ac97_ymf7x3_spdif_source_put,
418 	},
419 	{
420 		.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
421 		.name	= SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Output Pin",
422 		.info	= snd_ac97_ymf753_spdif_output_pin_info,
423 		.get	= snd_ac97_ymf753_spdif_output_pin_get,
424 		.put	= snd_ac97_ymf753_spdif_output_pin_put,
425 	},
426 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("", NONE, NONE) "Mute",
427 		    AC97_YMF7X3_DIT_CTRL, 2, 1, 1)
428 };
429 
430 static int patch_yamaha_ymf753_post_spdif(struct snd_ac97 * ac97)
431 {
432 	int err;
433 
434 	if ((err = patch_build_controls(ac97, snd_ac97_ymf753_controls_spdif, ARRAY_SIZE(snd_ac97_ymf753_controls_spdif))) < 0)
435 		return err;
436 	return 0;
437 }
438 
439 static struct snd_ac97_build_ops patch_yamaha_ymf753_ops = {
440 	.build_3d	= patch_yamaha_ymf7x3_3d,
441 	.build_post_spdif = patch_yamaha_ymf753_post_spdif
442 };
443 
444 static int patch_yamaha_ymf753(struct snd_ac97 * ac97)
445 {
446 	/* Patch for Yamaha YMF753, Copyright (c) by David Shust, dshust@shustring.com.
447 	   This chip has nonstandard and extended behaviour with regard to its S/PDIF output.
448 	   The AC'97 spec states that the S/PDIF signal is to be output at pin 48.
449 	   The YMF753 will ouput the S/PDIF signal to pin 43, 47 (EAPD), or 48.
450 	   By default, no output pin is selected, and the S/PDIF signal is not output.
451 	   There is also a bit to mute S/PDIF output in a vendor-specific register.
452 	*/
453 	ac97->build_ops = &patch_yamaha_ymf753_ops;
454 	ac97->caps |= AC97_BC_BASS_TREBLE;
455 	ac97->caps |= 0x04 << 10; /* Yamaha 3D enhancement */
456 	return 0;
457 }
458 
459 /*
460  * May 2, 2003 Liam Girdwood <liam.girdwood@wolfsonmicro.com>
461  *  removed broken wolfson00 patch.
462  *  added support for WM9705,WM9708,WM9709,WM9710,WM9711,WM9712 and WM9717.
463  */
464 
465 static const struct snd_kcontrol_new wm97xx_snd_ac97_controls[] = {
466 AC97_DOUBLE("Front Playback Volume", AC97_WM97XX_FMIXER_VOL, 8, 0, 31, 1),
467 AC97_SINGLE("Front Playback Switch", AC97_WM97XX_FMIXER_VOL, 15, 1, 1),
468 };
469 
470 static int patch_wolfson_wm9703_specific(struct snd_ac97 * ac97)
471 {
472 	/* This is known to work for the ViewSonic ViewPad 1000
473 	 * Randolph Bentson <bentson@holmsjoen.com>
474 	 * WM9703/9707/9708/9717
475 	 */
476 	int err, i;
477 
478 	for (i = 0; i < ARRAY_SIZE(wm97xx_snd_ac97_controls); i++) {
479 		if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&wm97xx_snd_ac97_controls[i], ac97))) < 0)
480 			return err;
481 	}
482 	snd_ac97_write_cache(ac97,  AC97_WM97XX_FMIXER_VOL, 0x0808);
483 	return 0;
484 }
485 
486 static struct snd_ac97_build_ops patch_wolfson_wm9703_ops = {
487 	.build_specific = patch_wolfson_wm9703_specific,
488 };
489 
490 static int patch_wolfson03(struct snd_ac97 * ac97)
491 {
492 	ac97->build_ops = &patch_wolfson_wm9703_ops;
493 	return 0;
494 }
495 
496 static const struct snd_kcontrol_new wm9704_snd_ac97_controls[] = {
497 AC97_DOUBLE("Front Playback Volume", AC97_WM97XX_FMIXER_VOL, 8, 0, 31, 1),
498 AC97_SINGLE("Front Playback Switch", AC97_WM97XX_FMIXER_VOL, 15, 1, 1),
499 AC97_DOUBLE("Rear Playback Volume", AC97_WM9704_RMIXER_VOL, 8, 0, 31, 1),
500 AC97_SINGLE("Rear Playback Switch", AC97_WM9704_RMIXER_VOL, 15, 1, 1),
501 AC97_DOUBLE("Rear DAC Volume", AC97_WM9704_RPCM_VOL, 8, 0, 31, 1),
502 AC97_DOUBLE("Surround Volume", AC97_SURROUND_MASTER, 8, 0, 31, 1),
503 };
504 
505 static int patch_wolfson_wm9704_specific(struct snd_ac97 * ac97)
506 {
507 	int err, i;
508 	for (i = 0; i < ARRAY_SIZE(wm9704_snd_ac97_controls); i++) {
509 		if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&wm9704_snd_ac97_controls[i], ac97))) < 0)
510 			return err;
511 	}
512 	/* patch for DVD noise */
513 	snd_ac97_write_cache(ac97, AC97_WM9704_TEST, 0x0200);
514 	return 0;
515 }
516 
517 static struct snd_ac97_build_ops patch_wolfson_wm9704_ops = {
518 	.build_specific = patch_wolfson_wm9704_specific,
519 };
520 
521 static int patch_wolfson04(struct snd_ac97 * ac97)
522 {
523 	/* WM9704M/9704Q */
524 	ac97->build_ops = &patch_wolfson_wm9704_ops;
525 	return 0;
526 }
527 
528 static int patch_wolfson_wm9705_specific(struct snd_ac97 * ac97)
529 {
530 	int err, i;
531 	for (i = 0; i < ARRAY_SIZE(wm97xx_snd_ac97_controls); i++) {
532 		if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&wm97xx_snd_ac97_controls[i], ac97))) < 0)
533 			return err;
534 	}
535 	snd_ac97_write_cache(ac97,  0x72, 0x0808);
536 	return 0;
537 }
538 
539 static struct snd_ac97_build_ops patch_wolfson_wm9705_ops = {
540 	.build_specific = patch_wolfson_wm9705_specific,
541 };
542 
543 static int patch_wolfson05(struct snd_ac97 * ac97)
544 {
545 	/* WM9705, WM9710 */
546 	ac97->build_ops = &patch_wolfson_wm9705_ops;
547 #ifdef CONFIG_TOUCHSCREEN_WM9705
548 	/* WM9705 touchscreen uses AUX and VIDEO for touch */
549 	ac97->flags |= AC97_HAS_NO_VIDEO | AC97_HAS_NO_AUX;
550 #endif
551 	return 0;
552 }
553 
554 static const char* wm9711_alc_select[] = {"None", "Left", "Right", "Stereo"};
555 static const char* wm9711_alc_mix[] = {"Stereo", "Right", "Left", "None"};
556 static const char* wm9711_out3_src[] = {"Left", "VREF", "Left + Right", "Mono"};
557 static const char* wm9711_out3_lrsrc[] = {"Master Mix", "Headphone Mix"};
558 static const char* wm9711_rec_adc[] = {"Stereo", "Left", "Right", "Mute"};
559 static const char* wm9711_base[] = {"Linear Control", "Adaptive Boost"};
560 static const char* wm9711_rec_gain[] = {"+1.5dB Steps", "+0.75dB Steps"};
561 static const char* wm9711_mic[] = {"Mic 1", "Differential", "Mic 2", "Stereo"};
562 static const char* wm9711_rec_sel[] =
563 	{"Mic 1", "NC", "NC", "Master Mix", "Line", "Headphone Mix", "Phone Mix", "Phone"};
564 static const char* wm9711_ng_type[] = {"Constant Gain", "Mute"};
565 
566 static const struct ac97_enum wm9711_enum[] = {
567 AC97_ENUM_SINGLE(AC97_PCI_SVID, 14, 4, wm9711_alc_select),
568 AC97_ENUM_SINGLE(AC97_VIDEO, 10, 4, wm9711_alc_mix),
569 AC97_ENUM_SINGLE(AC97_AUX, 9, 4, wm9711_out3_src),
570 AC97_ENUM_SINGLE(AC97_AUX, 8, 2, wm9711_out3_lrsrc),
571 AC97_ENUM_SINGLE(AC97_REC_SEL, 12, 4, wm9711_rec_adc),
572 AC97_ENUM_SINGLE(AC97_MASTER_TONE, 15, 2, wm9711_base),
573 AC97_ENUM_DOUBLE(AC97_REC_GAIN, 14, 6, 2, wm9711_rec_gain),
574 AC97_ENUM_SINGLE(AC97_MIC, 5, 4, wm9711_mic),
575 AC97_ENUM_DOUBLE(AC97_REC_SEL, 8, 0, 8, wm9711_rec_sel),
576 AC97_ENUM_SINGLE(AC97_PCI_SVID, 5, 2, wm9711_ng_type),
577 };
578 
579 static const struct snd_kcontrol_new wm9711_snd_ac97_controls[] = {
580 AC97_SINGLE("ALC Target Volume", AC97_CODEC_CLASS_REV, 12, 15, 0),
581 AC97_SINGLE("ALC Hold Time", AC97_CODEC_CLASS_REV, 8, 15, 0),
582 AC97_SINGLE("ALC Decay Time", AC97_CODEC_CLASS_REV, 4, 15, 0),
583 AC97_SINGLE("ALC Attack Time", AC97_CODEC_CLASS_REV, 0, 15, 0),
584 AC97_ENUM("ALC Function", wm9711_enum[0]),
585 AC97_SINGLE("ALC Max Volume", AC97_PCI_SVID, 11, 7, 1),
586 AC97_SINGLE("ALC ZC Timeout", AC97_PCI_SVID, 9, 3, 1),
587 AC97_SINGLE("ALC ZC Switch", AC97_PCI_SVID, 8, 1, 0),
588 AC97_SINGLE("ALC NG Switch", AC97_PCI_SVID, 7, 1, 0),
589 AC97_ENUM("ALC NG Type", wm9711_enum[9]),
590 AC97_SINGLE("ALC NG Threshold", AC97_PCI_SVID, 0, 31, 1),
591 
592 AC97_SINGLE("Side Tone Switch", AC97_VIDEO, 15, 1, 1),
593 AC97_SINGLE("Side Tone Volume", AC97_VIDEO, 12, 7, 1),
594 AC97_ENUM("ALC Headphone Mux", wm9711_enum[1]),
595 AC97_SINGLE("ALC Headphone Volume", AC97_VIDEO, 7, 7, 1),
596 
597 AC97_SINGLE("Out3 Switch", AC97_AUX, 15, 1, 1),
598 AC97_SINGLE("Out3 ZC Switch", AC97_AUX, 7, 1, 0),
599 AC97_ENUM("Out3 Mux", wm9711_enum[2]),
600 AC97_ENUM("Out3 LR Mux", wm9711_enum[3]),
601 AC97_SINGLE("Out3 Volume", AC97_AUX, 0, 31, 1),
602 
603 AC97_SINGLE("Beep to Headphone Switch", AC97_PC_BEEP, 15, 1, 1),
604 AC97_SINGLE("Beep to Headphone Volume", AC97_PC_BEEP, 12, 7, 1),
605 AC97_SINGLE("Beep to Side Tone Switch", AC97_PC_BEEP, 11, 1, 1),
606 AC97_SINGLE("Beep to Side Tone Volume", AC97_PC_BEEP, 8, 7, 1),
607 AC97_SINGLE("Beep to Phone Switch", AC97_PC_BEEP, 7, 1, 1),
608 AC97_SINGLE("Beep to Phone Volume", AC97_PC_BEEP, 4, 7, 1),
609 
610 AC97_SINGLE("Aux to Headphone Switch", AC97_CD, 15, 1, 1),
611 AC97_SINGLE("Aux to Headphone Volume", AC97_CD, 12, 7, 1),
612 AC97_SINGLE("Aux to Side Tone Switch", AC97_CD, 11, 1, 1),
613 AC97_SINGLE("Aux to Side Tone Volume", AC97_CD, 8, 7, 1),
614 AC97_SINGLE("Aux to Phone Switch", AC97_CD, 7, 1, 1),
615 AC97_SINGLE("Aux to Phone Volume", AC97_CD, 4, 7, 1),
616 
617 AC97_SINGLE("Phone to Headphone Switch", AC97_PHONE, 15, 1, 1),
618 AC97_SINGLE("Phone to Master Switch", AC97_PHONE, 14, 1, 1),
619 
620 AC97_SINGLE("Line to Headphone Switch", AC97_LINE, 15, 1, 1),
621 AC97_SINGLE("Line to Master Switch", AC97_LINE, 14, 1, 1),
622 AC97_SINGLE("Line to Phone Switch", AC97_LINE, 13, 1, 1),
623 
624 AC97_SINGLE("PCM Playback to Headphone Switch", AC97_PCM, 15, 1, 1),
625 AC97_SINGLE("PCM Playback to Master Switch", AC97_PCM, 14, 1, 1),
626 AC97_SINGLE("PCM Playback to Phone Switch", AC97_PCM, 13, 1, 1),
627 
628 AC97_SINGLE("Capture 20dB Boost Switch", AC97_REC_SEL, 14, 1, 0),
629 AC97_ENUM("Capture to Phone Mux", wm9711_enum[4]),
630 AC97_SINGLE("Capture to Phone 20dB Boost Switch", AC97_REC_SEL, 11, 1, 1),
631 AC97_ENUM("Capture Select", wm9711_enum[8]),
632 
633 AC97_SINGLE("3D Upper Cut-off Switch", AC97_3D_CONTROL, 5, 1, 1),
634 AC97_SINGLE("3D Lower Cut-off Switch", AC97_3D_CONTROL, 4, 1, 1),
635 
636 AC97_ENUM("Bass Control", wm9711_enum[5]),
637 AC97_SINGLE("Bass Cut-off Switch", AC97_MASTER_TONE, 12, 1, 1),
638 AC97_SINGLE("Tone Cut-off Switch", AC97_MASTER_TONE, 4, 1, 1),
639 AC97_SINGLE("Playback Attenuate (-6dB) Switch", AC97_MASTER_TONE, 6, 1, 0),
640 
641 AC97_SINGLE("ADC Switch", AC97_REC_GAIN, 15, 1, 1),
642 AC97_ENUM("Capture Volume Steps", wm9711_enum[6]),
643 AC97_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 63, 1),
644 AC97_SINGLE("Capture ZC Switch", AC97_REC_GAIN, 7, 1, 0),
645 
646 AC97_SINGLE("Mic 1 to Phone Switch", AC97_MIC, 14, 1, 1),
647 AC97_SINGLE("Mic 2 to Phone Switch", AC97_MIC, 13, 1, 1),
648 AC97_ENUM("Mic Select Source", wm9711_enum[7]),
649 AC97_SINGLE("Mic 1 Volume", AC97_MIC, 8, 31, 1),
650 AC97_SINGLE("Mic 2 Volume", AC97_MIC, 0, 31, 1),
651 AC97_SINGLE("Mic 20dB Boost Switch", AC97_MIC, 7, 1, 0),
652 
653 AC97_SINGLE("Master ZC Switch", AC97_MASTER, 7, 1, 0),
654 AC97_SINGLE("Headphone ZC Switch", AC97_HEADPHONE, 7, 1, 0),
655 AC97_SINGLE("Mono ZC Switch", AC97_MASTER_MONO, 7, 1, 0),
656 };
657 
658 static int patch_wolfson_wm9711_specific(struct snd_ac97 * ac97)
659 {
660 	int err, i;
661 
662 	for (i = 0; i < ARRAY_SIZE(wm9711_snd_ac97_controls); i++) {
663 		if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&wm9711_snd_ac97_controls[i], ac97))) < 0)
664 			return err;
665 	}
666 	snd_ac97_write_cache(ac97,  AC97_CODEC_CLASS_REV, 0x0808);
667 	snd_ac97_write_cache(ac97,  AC97_PCI_SVID, 0x0808);
668 	snd_ac97_write_cache(ac97,  AC97_VIDEO, 0x0808);
669 	snd_ac97_write_cache(ac97,  AC97_AUX, 0x0808);
670 	snd_ac97_write_cache(ac97,  AC97_PC_BEEP, 0x0808);
671 	snd_ac97_write_cache(ac97,  AC97_CD, 0x0000);
672 	return 0;
673 }
674 
675 static struct snd_ac97_build_ops patch_wolfson_wm9711_ops = {
676 	.build_specific = patch_wolfson_wm9711_specific,
677 };
678 
679 static int patch_wolfson11(struct snd_ac97 * ac97)
680 {
681 	/* WM9711, WM9712 */
682 	ac97->build_ops = &patch_wolfson_wm9711_ops;
683 
684 	ac97->flags |= AC97_HAS_NO_REC_GAIN | AC97_STEREO_MUTES | AC97_HAS_NO_MIC |
685 		AC97_HAS_NO_PC_BEEP | AC97_HAS_NO_VIDEO | AC97_HAS_NO_CD;
686 
687 	return 0;
688 }
689 
690 static const char* wm9713_mic_mixer[] = {"Stereo", "Mic 1", "Mic 2", "Mute"};
691 static const char* wm9713_rec_mux[] = {"Stereo", "Left", "Right", "Mute"};
692 static const char* wm9713_rec_src[] =
693 	{"Mic 1", "Mic 2", "Line", "Mono In", "Headphone Mix", "Master Mix",
694 	"Mono Mix", "Zh"};
695 static const char* wm9713_rec_gain[] = {"+1.5dB Steps", "+0.75dB Steps"};
696 static const char* wm9713_alc_select[] = {"None", "Left", "Right", "Stereo"};
697 static const char* wm9713_mono_pga[] = {"Vmid", "Zh", "Mono Mix", "Inv 1"};
698 static const char* wm9713_spk_pga[] =
699 	{"Vmid", "Zh", "Headphone Mix", "Master Mix", "Inv", "NC", "NC", "NC"};
700 static const char* wm9713_hp_pga[] = {"Vmid", "Zh", "Headphone Mix", "NC"};
701 static const char* wm9713_out3_pga[] = {"Vmid", "Zh", "Inv 1", "NC"};
702 static const char* wm9713_out4_pga[] = {"Vmid", "Zh", "Inv 2", "NC"};
703 static const char* wm9713_dac_inv[] =
704 	{"Off", "Mono Mix", "Master Mix", "Headphone Mix L", "Headphone Mix R",
705 	"Headphone Mix Mono", "NC", "Vmid"};
706 static const char* wm9713_base[] = {"Linear Control", "Adaptive Boost"};
707 static const char* wm9713_ng_type[] = {"Constant Gain", "Mute"};
708 
709 static const struct ac97_enum wm9713_enum[] = {
710 AC97_ENUM_SINGLE(AC97_LINE, 3, 4, wm9713_mic_mixer),
711 AC97_ENUM_SINGLE(AC97_VIDEO, 14, 4, wm9713_rec_mux),
712 AC97_ENUM_SINGLE(AC97_VIDEO, 9, 4, wm9713_rec_mux),
713 AC97_ENUM_DOUBLE(AC97_VIDEO, 3, 0, 8, wm9713_rec_src),
714 AC97_ENUM_DOUBLE(AC97_CD, 14, 6, 2, wm9713_rec_gain),
715 AC97_ENUM_SINGLE(AC97_PCI_SVID, 14, 4, wm9713_alc_select),
716 AC97_ENUM_SINGLE(AC97_REC_GAIN, 14, 4, wm9713_mono_pga),
717 AC97_ENUM_DOUBLE(AC97_REC_GAIN, 11, 8, 8, wm9713_spk_pga),
718 AC97_ENUM_DOUBLE(AC97_REC_GAIN, 6, 4, 4, wm9713_hp_pga),
719 AC97_ENUM_SINGLE(AC97_REC_GAIN, 2, 4, wm9713_out3_pga),
720 AC97_ENUM_SINGLE(AC97_REC_GAIN, 0, 4, wm9713_out4_pga),
721 AC97_ENUM_DOUBLE(AC97_REC_GAIN_MIC, 13, 10, 8, wm9713_dac_inv),
722 AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 15, 2, wm9713_base),
723 AC97_ENUM_SINGLE(AC97_PCI_SVID, 5, 2, wm9713_ng_type),
724 };
725 
726 static const struct snd_kcontrol_new wm13_snd_ac97_controls[] = {
727 AC97_DOUBLE("Line In Volume", AC97_PC_BEEP, 8, 0, 31, 1),
728 AC97_SINGLE("Line In to Headphone Switch", AC97_PC_BEEP, 15, 1, 1),
729 AC97_SINGLE("Line In to Master Switch", AC97_PC_BEEP, 14, 1, 1),
730 AC97_SINGLE("Line In to Mono Switch", AC97_PC_BEEP, 13, 1, 1),
731 
732 AC97_DOUBLE("PCM Playback Volume", AC97_PHONE, 8, 0, 31, 1),
733 AC97_SINGLE("PCM Playback to Headphone Switch", AC97_PHONE, 15, 1, 1),
734 AC97_SINGLE("PCM Playback to Master Switch", AC97_PHONE, 14, 1, 1),
735 AC97_SINGLE("PCM Playback to Mono Switch", AC97_PHONE, 13, 1, 1),
736 
737 AC97_SINGLE("Mic 1 Volume", AC97_MIC, 8, 31, 1),
738 AC97_SINGLE("Mic 2 Volume", AC97_MIC, 0, 31, 1),
739 AC97_SINGLE("Mic 1 to Mono Switch", AC97_LINE, 7, 1, 1),
740 AC97_SINGLE("Mic 2 to Mono Switch", AC97_LINE, 6, 1, 1),
741 AC97_SINGLE("Mic Boost (+20dB) Switch", AC97_LINE, 5, 1, 0),
742 AC97_ENUM("Mic to Headphone Mux", wm9713_enum[0]),
743 AC97_SINGLE("Mic Headphone Mixer Volume", AC97_LINE, 0, 7, 1),
744 
745 AC97_SINGLE("Capture Switch", AC97_CD, 15, 1, 1),
746 AC97_ENUM("Capture Volume Steps", wm9713_enum[4]),
747 AC97_DOUBLE("Capture Volume", AC97_CD, 8, 0, 15, 0),
748 AC97_SINGLE("Capture ZC Switch", AC97_CD, 7, 1, 0),
749 
750 AC97_ENUM("Capture to Headphone Mux", wm9713_enum[1]),
751 AC97_SINGLE("Capture to Headphone Volume", AC97_VIDEO, 11, 7, 1),
752 AC97_ENUM("Capture to Mono Mux", wm9713_enum[2]),
753 AC97_SINGLE("Capture to Mono Boost (+20dB) Switch", AC97_VIDEO, 8, 1, 0),
754 AC97_SINGLE("Capture ADC Boost (+20dB) Switch", AC97_VIDEO, 6, 1, 0),
755 AC97_ENUM("Capture Select", wm9713_enum[3]),
756 
757 AC97_SINGLE("ALC Target Volume", AC97_CODEC_CLASS_REV, 12, 15, 0),
758 AC97_SINGLE("ALC Hold Time", AC97_CODEC_CLASS_REV, 8, 15, 0),
759 AC97_SINGLE("ALC Decay Time ", AC97_CODEC_CLASS_REV, 4, 15, 0),
760 AC97_SINGLE("ALC Attack Time", AC97_CODEC_CLASS_REV, 0, 15, 0),
761 AC97_ENUM("ALC Function", wm9713_enum[5]),
762 AC97_SINGLE("ALC Max Volume", AC97_PCI_SVID, 11, 7, 0),
763 AC97_SINGLE("ALC ZC Timeout", AC97_PCI_SVID, 9, 3, 0),
764 AC97_SINGLE("ALC ZC Switch", AC97_PCI_SVID, 8, 1, 0),
765 AC97_SINGLE("ALC NG Switch", AC97_PCI_SVID, 7, 1, 0),
766 AC97_ENUM("ALC NG Type", wm9713_enum[13]),
767 AC97_SINGLE("ALC NG Threshold", AC97_PCI_SVID, 0, 31, 0),
768 
769 AC97_DOUBLE("Master ZC Switch", AC97_MASTER, 14, 6, 1, 0),
770 AC97_DOUBLE("Headphone ZC Switch", AC97_HEADPHONE, 14, 6, 1, 0),
771 AC97_DOUBLE("Out3/4 ZC Switch", AC97_MASTER_MONO, 14, 6, 1, 0),
772 AC97_SINGLE("Master Right Switch", AC97_MASTER, 7, 1, 1),
773 AC97_SINGLE("Headphone Right Switch", AC97_HEADPHONE, 7, 1, 1),
774 AC97_SINGLE("Out3/4 Right Switch", AC97_MASTER_MONO, 7, 1, 1),
775 
776 AC97_SINGLE("Mono In to Headphone Switch", AC97_MASTER_TONE, 15, 1, 1),
777 AC97_SINGLE("Mono In to Master Switch", AC97_MASTER_TONE, 14, 1, 1),
778 AC97_SINGLE("Mono In Volume", AC97_MASTER_TONE, 8, 31, 1),
779 AC97_SINGLE("Mono Switch", AC97_MASTER_TONE, 7, 1, 1),
780 AC97_SINGLE("Mono ZC Switch", AC97_MASTER_TONE, 6, 1, 0),
781 AC97_SINGLE("Mono Volume", AC97_MASTER_TONE, 0, 31, 1),
782 
783 AC97_SINGLE("PC Beep to Headphone Switch", AC97_AUX, 15, 1, 1),
784 AC97_SINGLE("PC Beep to Headphone Volume", AC97_AUX, 12, 7, 1),
785 AC97_SINGLE("PC Beep to Master Switch", AC97_AUX, 11, 1, 1),
786 AC97_SINGLE("PC Beep to Master Volume", AC97_AUX, 8, 7, 1),
787 AC97_SINGLE("PC Beep to Mono Switch", AC97_AUX, 7, 1, 1),
788 AC97_SINGLE("PC Beep to Mono Volume", AC97_AUX, 4, 7, 1),
789 
790 AC97_SINGLE("Voice to Headphone Switch", AC97_PCM, 15, 1, 1),
791 AC97_SINGLE("Voice to Headphone Volume", AC97_PCM, 12, 7, 1),
792 AC97_SINGLE("Voice to Master Switch", AC97_PCM, 11, 1, 1),
793 AC97_SINGLE("Voice to Master Volume", AC97_PCM, 8, 7, 1),
794 AC97_SINGLE("Voice to Mono Switch", AC97_PCM, 7, 1, 1),
795 AC97_SINGLE("Voice to Mono Volume", AC97_PCM, 4, 7, 1),
796 
797 AC97_SINGLE("Aux to Headphone Switch", AC97_REC_SEL, 15, 1, 1),
798 AC97_SINGLE("Aux to Headphone Volume", AC97_REC_SEL, 12, 7, 1),
799 AC97_SINGLE("Aux to Master Switch", AC97_REC_SEL, 11, 1, 1),
800 AC97_SINGLE("Aux to Master Volume", AC97_REC_SEL, 8, 7, 1),
801 AC97_SINGLE("Aux to Mono Switch", AC97_REC_SEL, 7, 1, 1),
802 AC97_SINGLE("Aux to Mono Volume", AC97_REC_SEL, 4, 7, 1),
803 
804 AC97_ENUM("Mono Input Mux", wm9713_enum[6]),
805 AC97_ENUM("Master Input Mux", wm9713_enum[7]),
806 AC97_ENUM("Headphone Input Mux", wm9713_enum[8]),
807 AC97_ENUM("Out 3 Input Mux", wm9713_enum[9]),
808 AC97_ENUM("Out 4 Input Mux", wm9713_enum[10]),
809 
810 AC97_ENUM("Bass Control", wm9713_enum[12]),
811 AC97_SINGLE("Bass Cut-off Switch", AC97_GENERAL_PURPOSE, 12, 1, 1),
812 AC97_SINGLE("Tone Cut-off Switch", AC97_GENERAL_PURPOSE, 4, 1, 1),
813 AC97_SINGLE("Playback Attenuate (-6dB) Switch", AC97_GENERAL_PURPOSE, 6, 1, 0),
814 AC97_SINGLE("Bass Volume", AC97_GENERAL_PURPOSE, 8, 15, 1),
815 AC97_SINGLE("Tone Volume", AC97_GENERAL_PURPOSE, 0, 15, 1),
816 };
817 
818 static const struct snd_kcontrol_new wm13_snd_ac97_controls_3d[] = {
819 AC97_ENUM("Inv Input Mux", wm9713_enum[11]),
820 AC97_SINGLE("3D Upper Cut-off Switch", AC97_REC_GAIN_MIC, 5, 1, 0),
821 AC97_SINGLE("3D Lower Cut-off Switch", AC97_REC_GAIN_MIC, 4, 1, 0),
822 AC97_SINGLE("3D Depth", AC97_REC_GAIN_MIC, 0, 15, 1),
823 };
824 
825 static int patch_wolfson_wm9713_3d (struct snd_ac97 * ac97)
826 {
827 	int err, i;
828 
829 	for (i = 0; i < ARRAY_SIZE(wm13_snd_ac97_controls_3d); i++) {
830 		if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&wm13_snd_ac97_controls_3d[i], ac97))) < 0)
831 			return err;
832 	}
833 	return 0;
834 }
835 
836 static int patch_wolfson_wm9713_specific(struct snd_ac97 * ac97)
837 {
838 	int err, i;
839 
840 	for (i = 0; i < ARRAY_SIZE(wm13_snd_ac97_controls); i++) {
841 		if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&wm13_snd_ac97_controls[i], ac97))) < 0)
842 			return err;
843 	}
844 	snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x0808);
845 	snd_ac97_write_cache(ac97, AC97_PHONE, 0x0808);
846 	snd_ac97_write_cache(ac97, AC97_MIC, 0x0808);
847 	snd_ac97_write_cache(ac97, AC97_LINE, 0x00da);
848 	snd_ac97_write_cache(ac97, AC97_CD, 0x0808);
849 	snd_ac97_write_cache(ac97, AC97_VIDEO, 0xd612);
850 	snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x1ba0);
851 	return 0;
852 }
853 
854 #ifdef CONFIG_PM
855 static void patch_wolfson_wm9713_suspend (struct snd_ac97 * ac97)
856 {
857 	snd_ac97_write_cache(ac97, AC97_EXTENDED_MID, 0xfeff);
858 	snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0xffff);
859 }
860 
861 static void patch_wolfson_wm9713_resume (struct snd_ac97 * ac97)
862 {
863 	snd_ac97_write_cache(ac97, AC97_EXTENDED_MID, 0xda00);
864 	snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0x3810);
865 	snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0x0);
866 }
867 #endif
868 
869 static struct snd_ac97_build_ops patch_wolfson_wm9713_ops = {
870 	.build_specific = patch_wolfson_wm9713_specific,
871 	.build_3d = patch_wolfson_wm9713_3d,
872 #ifdef CONFIG_PM
873 	.suspend = patch_wolfson_wm9713_suspend,
874 	.resume = patch_wolfson_wm9713_resume
875 #endif
876 };
877 
878 static int patch_wolfson13(struct snd_ac97 * ac97)
879 {
880 	/* WM9713, WM9714 */
881 	ac97->build_ops = &patch_wolfson_wm9713_ops;
882 
883 	ac97->flags |= AC97_HAS_NO_REC_GAIN | AC97_STEREO_MUTES | AC97_HAS_NO_PHONE |
884 		AC97_HAS_NO_PC_BEEP | AC97_HAS_NO_VIDEO | AC97_HAS_NO_CD | AC97_HAS_NO_TONE |
885 		AC97_HAS_NO_STD_PCM;
886     	ac97->scaps &= ~AC97_SCAP_MODEM;
887 
888 	snd_ac97_write_cache(ac97, AC97_EXTENDED_MID, 0xda00);
889 	snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0x3810);
890 	snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0x0);
891 
892 	return 0;
893 }
894 
895 /*
896  * Tritech codec
897  */
898 static int patch_tritech_tr28028(struct snd_ac97 * ac97)
899 {
900 	snd_ac97_write_cache(ac97, 0x26, 0x0300);
901 	snd_ac97_write_cache(ac97, 0x26, 0x0000);
902 	snd_ac97_write_cache(ac97, AC97_SURROUND_MASTER, 0x0000);
903 	snd_ac97_write_cache(ac97, AC97_SPDIF, 0x0000);
904 	return 0;
905 }
906 
907 /*
908  * Sigmatel STAC97xx codecs
909  */
910 static int patch_sigmatel_stac9700_3d(struct snd_ac97 * ac97)
911 {
912 	struct snd_kcontrol *kctl;
913 	int err;
914 
915 	if ((err = snd_ctl_add(ac97->bus->card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
916 		return err;
917 	strcpy(kctl->id.name, "3D Control Sigmatel - Depth");
918 	kctl->private_value = AC97_SINGLE_VALUE(AC97_3D_CONTROL, 2, 3, 0);
919 	snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000);
920 	return 0;
921 }
922 
923 static int patch_sigmatel_stac9708_3d(struct snd_ac97 * ac97)
924 {
925 	struct snd_kcontrol *kctl;
926 	int err;
927 
928 	if ((err = snd_ctl_add(ac97->bus->card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
929 		return err;
930 	strcpy(kctl->id.name, "3D Control Sigmatel - Depth");
931 	kctl->private_value = AC97_SINGLE_VALUE(AC97_3D_CONTROL, 0, 3, 0);
932 	if ((err = snd_ctl_add(ac97->bus->card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
933 		return err;
934 	strcpy(kctl->id.name, "3D Control Sigmatel - Rear Depth");
935 	kctl->private_value = AC97_SINGLE_VALUE(AC97_3D_CONTROL, 2, 3, 0);
936 	snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000);
937 	return 0;
938 }
939 
940 static const struct snd_kcontrol_new snd_ac97_sigmatel_4speaker =
941 AC97_SINGLE("Sigmatel 4-Speaker Stereo Playback Switch", AC97_SIGMATEL_DAC2INVERT, 2, 1, 0);
942 
943 static const struct snd_kcontrol_new snd_ac97_sigmatel_phaseinvert =
944 AC97_SINGLE("Sigmatel Surround Phase Inversion Playback Switch", AC97_SIGMATEL_DAC2INVERT, 3, 1, 0);
945 
946 static const struct snd_kcontrol_new snd_ac97_sigmatel_controls[] = {
947 AC97_SINGLE("Sigmatel DAC 6dB Attenuate", AC97_SIGMATEL_ANALOG, 1, 1, 0),
948 AC97_SINGLE("Sigmatel ADC 6dB Attenuate", AC97_SIGMATEL_ANALOG, 0, 1, 0)
949 };
950 
951 static int patch_sigmatel_stac97xx_specific(struct snd_ac97 * ac97)
952 {
953 	int err;
954 
955 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_ANALOG, snd_ac97_read(ac97, AC97_SIGMATEL_ANALOG) & ~0x0003);
956 	if (snd_ac97_try_bit(ac97, AC97_SIGMATEL_ANALOG, 1))
957 		if ((err = patch_build_controls(ac97, &snd_ac97_sigmatel_controls[0], 1)) < 0)
958 			return err;
959 	if (snd_ac97_try_bit(ac97, AC97_SIGMATEL_ANALOG, 0))
960 		if ((err = patch_build_controls(ac97, &snd_ac97_sigmatel_controls[1], 1)) < 0)
961 			return err;
962 	if (snd_ac97_try_bit(ac97, AC97_SIGMATEL_DAC2INVERT, 2))
963 		if ((err = patch_build_controls(ac97, &snd_ac97_sigmatel_4speaker, 1)) < 0)
964 			return err;
965 	if (snd_ac97_try_bit(ac97, AC97_SIGMATEL_DAC2INVERT, 3))
966 		if ((err = patch_build_controls(ac97, &snd_ac97_sigmatel_phaseinvert, 1)) < 0)
967 			return err;
968 	return 0;
969 }
970 
971 static struct snd_ac97_build_ops patch_sigmatel_stac9700_ops = {
972 	.build_3d	= patch_sigmatel_stac9700_3d,
973 	.build_specific	= patch_sigmatel_stac97xx_specific
974 };
975 
976 static int patch_sigmatel_stac9700(struct snd_ac97 * ac97)
977 {
978 	ac97->build_ops = &patch_sigmatel_stac9700_ops;
979 	return 0;
980 }
981 
982 static int snd_ac97_stac9708_put_bias(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
983 {
984 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
985 	int err;
986 
987 	mutex_lock(&ac97->page_mutex);
988 	snd_ac97_write(ac97, AC97_SIGMATEL_BIAS1, 0xabba);
989 	err = snd_ac97_update_bits(ac97, AC97_SIGMATEL_BIAS2, 0x0010,
990 				   (ucontrol->value.integer.value[0] & 1) << 4);
991 	snd_ac97_write(ac97, AC97_SIGMATEL_BIAS1, 0);
992 	mutex_unlock(&ac97->page_mutex);
993 	return err;
994 }
995 
996 static const struct snd_kcontrol_new snd_ac97_stac9708_bias_control = {
997 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
998 	.name = "Sigmatel Output Bias Switch",
999 	.info = snd_ac97_info_volsw,
1000 	.get = snd_ac97_get_volsw,
1001 	.put = snd_ac97_stac9708_put_bias,
1002 	.private_value = AC97_SINGLE_VALUE(AC97_SIGMATEL_BIAS2, 4, 1, 0),
1003 };
1004 
1005 static int patch_sigmatel_stac9708_specific(struct snd_ac97 *ac97)
1006 {
1007 	int err;
1008 
1009 	/* the register bit is writable, but the function is not implemented: */
1010 	snd_ac97_remove_ctl(ac97, "PCM Out Path & Mute", NULL);
1011 
1012 	snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Sigmatel Surround Playback");
1013 	if ((err = patch_build_controls(ac97, &snd_ac97_stac9708_bias_control, 1)) < 0)
1014 		return err;
1015 	return patch_sigmatel_stac97xx_specific(ac97);
1016 }
1017 
1018 static struct snd_ac97_build_ops patch_sigmatel_stac9708_ops = {
1019 	.build_3d	= patch_sigmatel_stac9708_3d,
1020 	.build_specific	= patch_sigmatel_stac9708_specific
1021 };
1022 
1023 static int patch_sigmatel_stac9708(struct snd_ac97 * ac97)
1024 {
1025 	unsigned int codec72, codec6c;
1026 
1027 	ac97->build_ops = &patch_sigmatel_stac9708_ops;
1028 	ac97->caps |= 0x10;	/* HP (sigmatel surround) support */
1029 
1030 	codec72 = snd_ac97_read(ac97, AC97_SIGMATEL_BIAS2) & 0x8000;
1031 	codec6c = snd_ac97_read(ac97, AC97_SIGMATEL_ANALOG);
1032 
1033 	if ((codec72==0) && (codec6c==0)) {
1034 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC1, 0xabba);
1035 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC2, 0x1000);
1036 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS1, 0xabba);
1037 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS2, 0x0007);
1038 	} else if ((codec72==0x8000) && (codec6c==0)) {
1039 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC1, 0xabba);
1040 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC2, 0x1001);
1041 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_DAC2INVERT, 0x0008);
1042 	} else if ((codec72==0x8000) && (codec6c==0x0080)) {
1043 		/* nothing */
1044 	}
1045 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_MULTICHN, 0x0000);
1046 	return 0;
1047 }
1048 
1049 static int patch_sigmatel_stac9721(struct snd_ac97 * ac97)
1050 {
1051 	ac97->build_ops = &patch_sigmatel_stac9700_ops;
1052 	if (snd_ac97_read(ac97, AC97_SIGMATEL_ANALOG) == 0) {
1053 		// patch for SigmaTel
1054 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC1, 0xabba);
1055 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC2, 0x4000);
1056 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS1, 0xabba);
1057 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS2, 0x0002);
1058 	}
1059 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_MULTICHN, 0x0000);
1060 	return 0;
1061 }
1062 
1063 static int patch_sigmatel_stac9744(struct snd_ac97 * ac97)
1064 {
1065 	// patch for SigmaTel
1066 	ac97->build_ops = &patch_sigmatel_stac9700_ops;
1067 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC1, 0xabba);
1068 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC2, 0x0000);	/* is this correct? --jk */
1069 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS1, 0xabba);
1070 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS2, 0x0002);
1071 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_MULTICHN, 0x0000);
1072 	return 0;
1073 }
1074 
1075 static int patch_sigmatel_stac9756(struct snd_ac97 * ac97)
1076 {
1077 	// patch for SigmaTel
1078 	ac97->build_ops = &patch_sigmatel_stac9700_ops;
1079 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC1, 0xabba);
1080 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC2, 0x0000);	/* is this correct? --jk */
1081 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS1, 0xabba);
1082 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS2, 0x0002);
1083 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_MULTICHN, 0x0000);
1084 	return 0;
1085 }
1086 
1087 static int snd_ac97_stac9758_output_jack_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1088 {
1089 	static char *texts[5] = { "Input/Disabled", "Front Output",
1090 		"Rear Output", "Center/LFE Output", "Mixer Output" };
1091 
1092 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1093 	uinfo->count = 1;
1094 	uinfo->value.enumerated.items = 5;
1095 	if (uinfo->value.enumerated.item > 4)
1096 		uinfo->value.enumerated.item = 4;
1097 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1098 	return 0;
1099 }
1100 
1101 static int snd_ac97_stac9758_output_jack_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1102 {
1103 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1104 	int shift = kcontrol->private_value;
1105 	unsigned short val;
1106 
1107 	val = ac97->regs[AC97_SIGMATEL_OUTSEL] >> shift;
1108 	if (!(val & 4))
1109 		ucontrol->value.enumerated.item[0] = 0;
1110 	else
1111 		ucontrol->value.enumerated.item[0] = 1 + (val & 3);
1112 	return 0;
1113 }
1114 
1115 static int snd_ac97_stac9758_output_jack_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1116 {
1117 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1118 	int shift = kcontrol->private_value;
1119 	unsigned short val;
1120 
1121 	if (ucontrol->value.enumerated.item[0] > 4)
1122 		return -EINVAL;
1123 	if (ucontrol->value.enumerated.item[0] == 0)
1124 		val = 0;
1125 	else
1126 		val = 4 | (ucontrol->value.enumerated.item[0] - 1);
1127 	return ac97_update_bits_page(ac97, AC97_SIGMATEL_OUTSEL,
1128 				     7 << shift, val << shift, 0);
1129 }
1130 
1131 static int snd_ac97_stac9758_input_jack_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1132 {
1133 	static char *texts[7] = { "Mic2 Jack", "Mic1 Jack", "Line In Jack",
1134 		"Front Jack", "Rear Jack", "Center/LFE Jack", "Mute" };
1135 
1136 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1137 	uinfo->count = 1;
1138 	uinfo->value.enumerated.items = 7;
1139 	if (uinfo->value.enumerated.item > 6)
1140 		uinfo->value.enumerated.item = 6;
1141 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1142 	return 0;
1143 }
1144 
1145 static int snd_ac97_stac9758_input_jack_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1146 {
1147 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1148 	int shift = kcontrol->private_value;
1149 	unsigned short val;
1150 
1151 	val = ac97->regs[AC97_SIGMATEL_INSEL];
1152 	ucontrol->value.enumerated.item[0] = (val >> shift) & 7;
1153 	return 0;
1154 }
1155 
1156 static int snd_ac97_stac9758_input_jack_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1157 {
1158 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1159 	int shift = kcontrol->private_value;
1160 
1161 	return ac97_update_bits_page(ac97, AC97_SIGMATEL_INSEL, 7 << shift,
1162 				     ucontrol->value.enumerated.item[0] << shift, 0);
1163 }
1164 
1165 static int snd_ac97_stac9758_phonesel_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1166 {
1167 	static char *texts[3] = { "None", "Front Jack", "Rear Jack" };
1168 
1169 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1170 	uinfo->count = 1;
1171 	uinfo->value.enumerated.items = 3;
1172 	if (uinfo->value.enumerated.item > 2)
1173 		uinfo->value.enumerated.item = 2;
1174 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1175 	return 0;
1176 }
1177 
1178 static int snd_ac97_stac9758_phonesel_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1179 {
1180 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1181 
1182 	ucontrol->value.enumerated.item[0] = ac97->regs[AC97_SIGMATEL_IOMISC] & 3;
1183 	return 0;
1184 }
1185 
1186 static int snd_ac97_stac9758_phonesel_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1187 {
1188 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1189 
1190 	return ac97_update_bits_page(ac97, AC97_SIGMATEL_IOMISC, 3,
1191 				     ucontrol->value.enumerated.item[0], 0);
1192 }
1193 
1194 #define STAC9758_OUTPUT_JACK(xname, shift) \
1195 {	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1196 	.info = snd_ac97_stac9758_output_jack_info, \
1197 	.get = snd_ac97_stac9758_output_jack_get, \
1198 	.put = snd_ac97_stac9758_output_jack_put, \
1199 	.private_value = shift }
1200 #define STAC9758_INPUT_JACK(xname, shift) \
1201 {	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1202 	.info = snd_ac97_stac9758_input_jack_info, \
1203 	.get = snd_ac97_stac9758_input_jack_get, \
1204 	.put = snd_ac97_stac9758_input_jack_put, \
1205 	.private_value = shift }
1206 static const struct snd_kcontrol_new snd_ac97_sigmatel_stac9758_controls[] = {
1207 	STAC9758_OUTPUT_JACK("Mic1 Jack", 1),
1208 	STAC9758_OUTPUT_JACK("LineIn Jack", 4),
1209 	STAC9758_OUTPUT_JACK("Front Jack", 7),
1210 	STAC9758_OUTPUT_JACK("Rear Jack", 10),
1211 	STAC9758_OUTPUT_JACK("Center/LFE Jack", 13),
1212 	STAC9758_INPUT_JACK("Mic Input Source", 0),
1213 	STAC9758_INPUT_JACK("Line Input Source", 8),
1214 	{
1215 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1216 		.name = "Headphone Amp",
1217 		.info = snd_ac97_stac9758_phonesel_info,
1218 		.get = snd_ac97_stac9758_phonesel_get,
1219 		.put = snd_ac97_stac9758_phonesel_put
1220 	},
1221 	AC97_SINGLE("Exchange Center/LFE", AC97_SIGMATEL_IOMISC, 4, 1, 0),
1222 	AC97_SINGLE("Headphone +3dB Boost", AC97_SIGMATEL_IOMISC, 8, 1, 0)
1223 };
1224 
1225 static int patch_sigmatel_stac9758_specific(struct snd_ac97 *ac97)
1226 {
1227 	int err;
1228 
1229 	err = patch_sigmatel_stac97xx_specific(ac97);
1230 	if (err < 0)
1231 		return err;
1232 	err = patch_build_controls(ac97, snd_ac97_sigmatel_stac9758_controls,
1233 				   ARRAY_SIZE(snd_ac97_sigmatel_stac9758_controls));
1234 	if (err < 0)
1235 		return err;
1236 	/* DAC-A direct */
1237 	snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Front Playback");
1238 	/* DAC-A to Mix = PCM */
1239 	/* DAC-B direct = Surround */
1240 	/* DAC-B to Mix */
1241 	snd_ac97_rename_vol_ctl(ac97, "Video Playback", "Surround Mix Playback");
1242 	/* DAC-C direct = Center/LFE */
1243 
1244 	return 0;
1245 }
1246 
1247 static struct snd_ac97_build_ops patch_sigmatel_stac9758_ops = {
1248 	.build_3d	= patch_sigmatel_stac9700_3d,
1249 	.build_specific	= patch_sigmatel_stac9758_specific
1250 };
1251 
1252 static int patch_sigmatel_stac9758(struct snd_ac97 * ac97)
1253 {
1254 	static unsigned short regs[4] = {
1255 		AC97_SIGMATEL_OUTSEL,
1256 		AC97_SIGMATEL_IOMISC,
1257 		AC97_SIGMATEL_INSEL,
1258 		AC97_SIGMATEL_VARIOUS
1259 	};
1260 	static unsigned short def_regs[4] = {
1261 		/* OUTSEL */ 0xd794, /* CL:CL, SR:SR, LO:MX, LI:DS, MI:DS */
1262 		/* IOMISC */ 0x2001,
1263 		/* INSEL */ 0x0201, /* LI:LI, MI:M1 */
1264 		/* VARIOUS */ 0x0040
1265 	};
1266 	static unsigned short m675_regs[4] = {
1267 		/* OUTSEL */ 0xfc70, /* CL:MX, SR:MX, LO:DS, LI:MX, MI:DS */
1268 		/* IOMISC */ 0x2102, /* HP amp on */
1269 		/* INSEL */ 0x0203, /* LI:LI, MI:FR */
1270 		/* VARIOUS */ 0x0041 /* stereo mic */
1271 	};
1272 	unsigned short *pregs = def_regs;
1273 	int i;
1274 
1275 	/* Gateway M675 notebook */
1276 	if (ac97->pci &&
1277 	    ac97->subsystem_vendor == 0x107b &&
1278 	    ac97->subsystem_device == 0x0601)
1279 	    	pregs = m675_regs;
1280 
1281 	// patch for SigmaTel
1282 	ac97->build_ops = &patch_sigmatel_stac9758_ops;
1283 	/* FIXME: assume only page 0 for writing cache */
1284 	snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, AC97_PAGE_VENDOR);
1285 	for (i = 0; i < 4; i++)
1286 		snd_ac97_write_cache(ac97, regs[i], pregs[i]);
1287 
1288 	ac97->flags |= AC97_STEREO_MUTES;
1289 	return 0;
1290 }
1291 
1292 /*
1293  * Cirrus Logic CS42xx codecs
1294  */
1295 static const struct snd_kcontrol_new snd_ac97_cirrus_controls_spdif[2] = {
1296 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), AC97_CSR_SPDIF, 15, 1, 0),
1297 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "AC97-SPSA", AC97_CSR_ACMODE, 0, 3, 0)
1298 };
1299 
1300 static int patch_cirrus_build_spdif(struct snd_ac97 * ac97)
1301 {
1302 	int err;
1303 
1304 	/* con mask, pro mask, default */
1305 	if ((err = patch_build_controls(ac97, &snd_ac97_controls_spdif[0], 3)) < 0)
1306 		return err;
1307 	/* switch, spsa */
1308 	if ((err = patch_build_controls(ac97, &snd_ac97_cirrus_controls_spdif[0], 1)) < 0)
1309 		return err;
1310 	switch (ac97->id & AC97_ID_CS_MASK) {
1311 	case AC97_ID_CS4205:
1312 		if ((err = patch_build_controls(ac97, &snd_ac97_cirrus_controls_spdif[1], 1)) < 0)
1313 			return err;
1314 		break;
1315 	}
1316 	/* set default PCM S/PDIF params */
1317 	/* consumer,PCM audio,no copyright,no preemphasis,PCM coder,original,48000Hz */
1318 	snd_ac97_write_cache(ac97, AC97_CSR_SPDIF, 0x0a20);
1319 	return 0;
1320 }
1321 
1322 static struct snd_ac97_build_ops patch_cirrus_ops = {
1323 	.build_spdif = patch_cirrus_build_spdif
1324 };
1325 
1326 static int patch_cirrus_spdif(struct snd_ac97 * ac97)
1327 {
1328 	/* Basically, the cs4201/cs4205/cs4297a has non-standard sp/dif registers.
1329 	   WHY CAN'T ANYONE FOLLOW THE BLOODY SPEC?  *sigh*
1330 	   - sp/dif EA ID is not set, but sp/dif is always present.
1331 	   - enable/disable is spdif register bit 15.
1332 	   - sp/dif control register is 0x68.  differs from AC97:
1333 	   - valid is bit 14 (vs 15)
1334 	   - no DRS
1335 	   - only 44.1/48k [00 = 48, 01=44,1] (AC97 is 00=44.1, 10=48)
1336 	   - sp/dif ssource select is in 0x5e bits 0,1.
1337 	*/
1338 
1339 	ac97->build_ops = &patch_cirrus_ops;
1340 	ac97->flags |= AC97_CS_SPDIF;
1341 	ac97->rates[AC97_RATES_SPDIF] &= ~SNDRV_PCM_RATE_32000;
1342         ac97->ext_id |= AC97_EI_SPDIF;	/* force the detection of spdif */
1343 	snd_ac97_write_cache(ac97, AC97_CSR_ACMODE, 0x0080);
1344 	return 0;
1345 }
1346 
1347 static int patch_cirrus_cs4299(struct snd_ac97 * ac97)
1348 {
1349 	/* force the detection of PC Beep */
1350 	ac97->flags |= AC97_HAS_PC_BEEP;
1351 
1352 	return patch_cirrus_spdif(ac97);
1353 }
1354 
1355 /*
1356  * Conexant codecs
1357  */
1358 static const struct snd_kcontrol_new snd_ac97_conexant_controls_spdif[1] = {
1359 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), AC97_CXR_AUDIO_MISC, 3, 1, 0),
1360 };
1361 
1362 static int patch_conexant_build_spdif(struct snd_ac97 * ac97)
1363 {
1364 	int err;
1365 
1366 	/* con mask, pro mask, default */
1367 	if ((err = patch_build_controls(ac97, &snd_ac97_controls_spdif[0], 3)) < 0)
1368 		return err;
1369 	/* switch */
1370 	if ((err = patch_build_controls(ac97, &snd_ac97_conexant_controls_spdif[0], 1)) < 0)
1371 		return err;
1372 	/* set default PCM S/PDIF params */
1373 	/* consumer,PCM audio,no copyright,no preemphasis,PCM coder,original,48000Hz */
1374 	snd_ac97_write_cache(ac97, AC97_CXR_AUDIO_MISC,
1375 			     snd_ac97_read(ac97, AC97_CXR_AUDIO_MISC) & ~(AC97_CXR_SPDIFEN|AC97_CXR_COPYRGT|AC97_CXR_SPDIF_MASK));
1376 	return 0;
1377 }
1378 
1379 static struct snd_ac97_build_ops patch_conexant_ops = {
1380 	.build_spdif = patch_conexant_build_spdif
1381 };
1382 
1383 static int patch_conexant(struct snd_ac97 * ac97)
1384 {
1385 	ac97->build_ops = &patch_conexant_ops;
1386 	ac97->flags |= AC97_CX_SPDIF;
1387         ac97->ext_id |= AC97_EI_SPDIF;	/* force the detection of spdif */
1388 	ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000; /* 48k only */
1389 	return 0;
1390 }
1391 
1392 static int patch_cx20551(struct snd_ac97 *ac97)
1393 {
1394 	snd_ac97_update_bits(ac97, 0x5c, 0x01, 0x01);
1395 	return 0;
1396 }
1397 
1398 /*
1399  * Analog Device AD18xx, AD19xx codecs
1400  */
1401 #ifdef CONFIG_PM
1402 static void ad18xx_resume(struct snd_ac97 *ac97)
1403 {
1404 	static unsigned short setup_regs[] = {
1405 		AC97_AD_MISC, AC97_AD_SERIAL_CFG, AC97_AD_JACK_SPDIF,
1406 	};
1407 	int i, codec;
1408 
1409 	for (i = 0; i < (int)ARRAY_SIZE(setup_regs); i++) {
1410 		unsigned short reg = setup_regs[i];
1411 		if (test_bit(reg, ac97->reg_accessed)) {
1412 			snd_ac97_write(ac97, reg, ac97->regs[reg]);
1413 			snd_ac97_read(ac97, reg);
1414 		}
1415 	}
1416 
1417 	if (! (ac97->flags & AC97_AD_MULTI))
1418 		/* normal restore */
1419 		snd_ac97_restore_status(ac97);
1420 	else {
1421 		/* restore the AD18xx codec configurations */
1422 		for (codec = 0; codec < 3; codec++) {
1423 			if (! ac97->spec.ad18xx.id[codec])
1424 				continue;
1425 			/* select single codec */
1426 			snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000,
1427 					     ac97->spec.ad18xx.unchained[codec] | ac97->spec.ad18xx.chained[codec]);
1428 			ac97->bus->ops->write(ac97, AC97_AD_CODEC_CFG, ac97->spec.ad18xx.codec_cfg[codec]);
1429 		}
1430 		/* select all codecs */
1431 		snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, 0x7000);
1432 
1433 		/* restore status */
1434 		for (i = 2; i < 0x7c ; i += 2) {
1435 			if (i == AC97_POWERDOWN || i == AC97_EXTENDED_ID)
1436 				continue;
1437 			if (test_bit(i, ac97->reg_accessed)) {
1438 				/* handle multi codecs for AD18xx */
1439 				if (i == AC97_PCM) {
1440 					for (codec = 0; codec < 3; codec++) {
1441 						if (! ac97->spec.ad18xx.id[codec])
1442 							continue;
1443 						/* select single codec */
1444 						snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000,
1445 								     ac97->spec.ad18xx.unchained[codec] | ac97->spec.ad18xx.chained[codec]);
1446 						/* update PCM bits */
1447 						ac97->bus->ops->write(ac97, AC97_PCM, ac97->spec.ad18xx.pcmreg[codec]);
1448 					}
1449 					/* select all codecs */
1450 					snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, 0x7000);
1451 					continue;
1452 				} else if (i == AC97_AD_TEST ||
1453 					   i == AC97_AD_CODEC_CFG ||
1454 					   i == AC97_AD_SERIAL_CFG)
1455 					continue; /* ignore */
1456 			}
1457 			snd_ac97_write(ac97, i, ac97->regs[i]);
1458 			snd_ac97_read(ac97, i);
1459 		}
1460 	}
1461 
1462 	snd_ac97_restore_iec958(ac97);
1463 }
1464 
1465 static void ad1888_resume(struct snd_ac97 *ac97)
1466 {
1467 	ad18xx_resume(ac97);
1468 	snd_ac97_write_cache(ac97, AC97_CODEC_CLASS_REV, 0x8080);
1469 }
1470 
1471 #endif
1472 
1473 static const struct snd_ac97_res_table ad1819_restbl[] = {
1474 	{ AC97_PHONE, 0x9f1f },
1475 	{ AC97_MIC, 0x9f1f },
1476 	{ AC97_LINE, 0x9f1f },
1477 	{ AC97_CD, 0x9f1f },
1478 	{ AC97_VIDEO, 0x9f1f },
1479 	{ AC97_AUX, 0x9f1f },
1480 	{ AC97_PCM, 0x9f1f },
1481 	{ } /* terminator */
1482 };
1483 
1484 static int patch_ad1819(struct snd_ac97 * ac97)
1485 {
1486 	unsigned short scfg;
1487 
1488 	// patch for Analog Devices
1489 	scfg = snd_ac97_read(ac97, AC97_AD_SERIAL_CFG);
1490 	snd_ac97_write_cache(ac97, AC97_AD_SERIAL_CFG, scfg | 0x7000); /* select all codecs */
1491 	ac97->res_table = ad1819_restbl;
1492 	return 0;
1493 }
1494 
1495 static unsigned short patch_ad1881_unchained(struct snd_ac97 * ac97, int idx, unsigned short mask)
1496 {
1497 	unsigned short val;
1498 
1499 	// test for unchained codec
1500 	snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, mask);
1501 	snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, 0x0000);	/* ID0C, ID1C, SDIE = off */
1502 	val = snd_ac97_read(ac97, AC97_VENDOR_ID2);
1503 	if ((val & 0xff40) != 0x5340)
1504 		return 0;
1505 	ac97->spec.ad18xx.unchained[idx] = mask;
1506 	ac97->spec.ad18xx.id[idx] = val;
1507 	ac97->spec.ad18xx.codec_cfg[idx] = 0x0000;
1508 	return mask;
1509 }
1510 
1511 static int patch_ad1881_chained1(struct snd_ac97 * ac97, int idx, unsigned short codec_bits)
1512 {
1513 	static int cfg_bits[3] = { 1<<12, 1<<14, 1<<13 };
1514 	unsigned short val;
1515 
1516 	snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, cfg_bits[idx]);
1517 	snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, 0x0004);	// SDIE
1518 	val = snd_ac97_read(ac97, AC97_VENDOR_ID2);
1519 	if ((val & 0xff40) != 0x5340)
1520 		return 0;
1521 	if (codec_bits)
1522 		snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, codec_bits);
1523 	ac97->spec.ad18xx.chained[idx] = cfg_bits[idx];
1524 	ac97->spec.ad18xx.id[idx] = val;
1525 	ac97->spec.ad18xx.codec_cfg[idx] = codec_bits ? codec_bits : 0x0004;
1526 	return 1;
1527 }
1528 
1529 static void patch_ad1881_chained(struct snd_ac97 * ac97, int unchained_idx, int cidx1, int cidx2)
1530 {
1531 	// already detected?
1532 	if (ac97->spec.ad18xx.unchained[cidx1] || ac97->spec.ad18xx.chained[cidx1])
1533 		cidx1 = -1;
1534 	if (ac97->spec.ad18xx.unchained[cidx2] || ac97->spec.ad18xx.chained[cidx2])
1535 		cidx2 = -1;
1536 	if (cidx1 < 0 && cidx2 < 0)
1537 		return;
1538 	// test for chained codecs
1539 	snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000,
1540 			     ac97->spec.ad18xx.unchained[unchained_idx]);
1541 	snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, 0x0002);		// ID1C
1542 	ac97->spec.ad18xx.codec_cfg[unchained_idx] = 0x0002;
1543 	if (cidx1 >= 0) {
1544 		if (cidx2 < 0)
1545 			patch_ad1881_chained1(ac97, cidx1, 0);
1546 		else if (patch_ad1881_chained1(ac97, cidx1, 0x0006))	// SDIE | ID1C
1547 			patch_ad1881_chained1(ac97, cidx2, 0);
1548 		else if (patch_ad1881_chained1(ac97, cidx2, 0x0006))	// SDIE | ID1C
1549 			patch_ad1881_chained1(ac97, cidx1, 0);
1550 	} else if (cidx2 >= 0) {
1551 		patch_ad1881_chained1(ac97, cidx2, 0);
1552 	}
1553 }
1554 
1555 static struct snd_ac97_build_ops patch_ad1881_build_ops = {
1556 #ifdef CONFIG_PM
1557 	.resume = ad18xx_resume
1558 #endif
1559 };
1560 
1561 static int patch_ad1881(struct snd_ac97 * ac97)
1562 {
1563 	static const char cfg_idxs[3][2] = {
1564 		{2, 1},
1565 		{0, 2},
1566 		{0, 1}
1567 	};
1568 
1569 	// patch for Analog Devices
1570 	unsigned short codecs[3];
1571 	unsigned short val;
1572 	int idx, num;
1573 
1574 	val = snd_ac97_read(ac97, AC97_AD_SERIAL_CFG);
1575 	snd_ac97_write_cache(ac97, AC97_AD_SERIAL_CFG, val);
1576 	codecs[0] = patch_ad1881_unchained(ac97, 0, (1<<12));
1577 	codecs[1] = patch_ad1881_unchained(ac97, 1, (1<<14));
1578 	codecs[2] = patch_ad1881_unchained(ac97, 2, (1<<13));
1579 
1580 	if (! (codecs[0] || codecs[1] || codecs[2]))
1581 		goto __end;
1582 
1583 	for (idx = 0; idx < 3; idx++)
1584 		if (ac97->spec.ad18xx.unchained[idx])
1585 			patch_ad1881_chained(ac97, idx, cfg_idxs[idx][0], cfg_idxs[idx][1]);
1586 
1587 	if (ac97->spec.ad18xx.id[1]) {
1588 		ac97->flags |= AC97_AD_MULTI;
1589 		ac97->scaps |= AC97_SCAP_SURROUND_DAC;
1590 	}
1591 	if (ac97->spec.ad18xx.id[2]) {
1592 		ac97->flags |= AC97_AD_MULTI;
1593 		ac97->scaps |= AC97_SCAP_CENTER_LFE_DAC;
1594 	}
1595 
1596       __end:
1597 	/* select all codecs */
1598 	snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, 0x7000);
1599 	/* check if only one codec is present */
1600 	for (idx = num = 0; idx < 3; idx++)
1601 		if (ac97->spec.ad18xx.id[idx])
1602 			num++;
1603 	if (num == 1) {
1604 		/* ok, deselect all ID bits */
1605 		snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, 0x0000);
1606 		ac97->spec.ad18xx.codec_cfg[0] =
1607 			ac97->spec.ad18xx.codec_cfg[1] =
1608 			ac97->spec.ad18xx.codec_cfg[2] = 0x0000;
1609 	}
1610 	/* required for AD1886/AD1885 combination */
1611 	ac97->ext_id = snd_ac97_read(ac97, AC97_EXTENDED_ID);
1612 	if (ac97->spec.ad18xx.id[0]) {
1613 		ac97->id &= 0xffff0000;
1614 		ac97->id |= ac97->spec.ad18xx.id[0];
1615 	}
1616 	ac97->build_ops = &patch_ad1881_build_ops;
1617 	return 0;
1618 }
1619 
1620 static const struct snd_kcontrol_new snd_ac97_controls_ad1885[] = {
1621 	AC97_SINGLE("Digital Mono Direct", AC97_AD_MISC, 11, 1, 0),
1622 	/* AC97_SINGLE("Digital Audio Mode", AC97_AD_MISC, 12, 1, 0), */ /* seems problematic */
1623 	AC97_SINGLE("Low Power Mixer", AC97_AD_MISC, 14, 1, 0),
1624 	AC97_SINGLE("Zero Fill DAC", AC97_AD_MISC, 15, 1, 0),
1625 	AC97_SINGLE("Headphone Jack Sense", AC97_AD_JACK_SPDIF, 9, 1, 1), /* inverted */
1626 	AC97_SINGLE("Line Jack Sense", AC97_AD_JACK_SPDIF, 8, 1, 1), /* inverted */
1627 };
1628 
1629 static const DECLARE_TLV_DB_SCALE(db_scale_6bit_6db_max, -8850, 150, 0);
1630 
1631 static int patch_ad1885_specific(struct snd_ac97 * ac97)
1632 {
1633 	int err;
1634 
1635 	if ((err = patch_build_controls(ac97, snd_ac97_controls_ad1885, ARRAY_SIZE(snd_ac97_controls_ad1885))) < 0)
1636 		return err;
1637 	reset_tlv(ac97, "Headphone Playback Volume",
1638 		  db_scale_6bit_6db_max);
1639 	return 0;
1640 }
1641 
1642 static struct snd_ac97_build_ops patch_ad1885_build_ops = {
1643 	.build_specific = &patch_ad1885_specific,
1644 #ifdef CONFIG_PM
1645 	.resume = ad18xx_resume
1646 #endif
1647 };
1648 
1649 static int patch_ad1885(struct snd_ac97 * ac97)
1650 {
1651 	patch_ad1881(ac97);
1652 	/* This is required to deal with the Intel D815EEAL2 */
1653 	/* i.e. Line out is actually headphone out from codec */
1654 
1655 	/* set default */
1656 	snd_ac97_write_cache(ac97, AC97_AD_MISC, 0x0404);
1657 
1658 	ac97->build_ops = &patch_ad1885_build_ops;
1659 	return 0;
1660 }
1661 
1662 static int patch_ad1886_specific(struct snd_ac97 * ac97)
1663 {
1664 	reset_tlv(ac97, "Headphone Playback Volume",
1665 		  db_scale_6bit_6db_max);
1666 	return 0;
1667 }
1668 
1669 static struct snd_ac97_build_ops patch_ad1886_build_ops = {
1670 	.build_specific = &patch_ad1886_specific,
1671 #ifdef CONFIG_PM
1672 	.resume = ad18xx_resume
1673 #endif
1674 };
1675 
1676 static int patch_ad1886(struct snd_ac97 * ac97)
1677 {
1678 	patch_ad1881(ac97);
1679 	/* Presario700 workaround */
1680 	/* for Jack Sense/SPDIF Register misetting causing */
1681 	snd_ac97_write_cache(ac97, AC97_AD_JACK_SPDIF, 0x0010);
1682 	ac97->build_ops = &patch_ad1886_build_ops;
1683 	return 0;
1684 }
1685 
1686 /* MISC bits (AD1888/AD1980/AD1985 register 0x76) */
1687 #define AC97_AD198X_MBC		0x0003	/* mic boost */
1688 #define AC97_AD198X_MBC_20	0x0000	/* +20dB */
1689 #define AC97_AD198X_MBC_10	0x0001	/* +10dB */
1690 #define AC97_AD198X_MBC_30	0x0002	/* +30dB */
1691 #define AC97_AD198X_VREFD	0x0004	/* VREF high-Z */
1692 #define AC97_AD198X_VREFH	0x0008	/* 0=2.25V, 1=3.7V */
1693 #define AC97_AD198X_VREF_0	0x000c	/* 0V (AD1985 only) */
1694 #define AC97_AD198X_VREF_MASK	(AC97_AD198X_VREFH | AC97_AD198X_VREFD)
1695 #define AC97_AD198X_VREF_SHIFT	2
1696 #define AC97_AD198X_SRU		0x0010	/* sample rate unlock */
1697 #define AC97_AD198X_LOSEL	0x0020	/* LINE_OUT amplifiers input select */
1698 #define AC97_AD198X_2MIC	0x0040	/* 2-channel mic select */
1699 #define AC97_AD198X_SPRD	0x0080	/* SPREAD enable */
1700 #define AC97_AD198X_DMIX0	0x0100	/* downmix mode: */
1701 					/*  0 = 6-to-4, 1 = 6-to-2 downmix */
1702 #define AC97_AD198X_DMIX1	0x0200	/* downmix mode: 1 = enabled */
1703 #define AC97_AD198X_HPSEL	0x0400	/* headphone amplifier input select */
1704 #define AC97_AD198X_CLDIS	0x0800	/* center/lfe disable */
1705 #define AC97_AD198X_LODIS	0x1000	/* LINE_OUT disable */
1706 #define AC97_AD198X_MSPLT	0x2000	/* mute split */
1707 #define AC97_AD198X_AC97NC	0x4000	/* AC97 no compatible mode */
1708 #define AC97_AD198X_DACZ	0x8000	/* DAC zero-fill mode */
1709 
1710 /* MISC 1 bits (AD1986 register 0x76) */
1711 #define AC97_AD1986_MBC		0x0003	/* mic boost */
1712 #define AC97_AD1986_MBC_20	0x0000	/* +20dB */
1713 #define AC97_AD1986_MBC_10	0x0001	/* +10dB */
1714 #define AC97_AD1986_MBC_30	0x0002	/* +30dB */
1715 #define AC97_AD1986_LISEL0	0x0004	/* LINE_IN select bit 0 */
1716 #define AC97_AD1986_LISEL1	0x0008	/* LINE_IN select bit 1 */
1717 #define AC97_AD1986_LISEL_MASK	(AC97_AD1986_LISEL1 | AC97_AD1986_LISEL0)
1718 #define AC97_AD1986_LISEL_LI	0x0000  /* LINE_IN pins as LINE_IN source */
1719 #define AC97_AD1986_LISEL_SURR	0x0004  /* SURROUND pins as LINE_IN source */
1720 #define AC97_AD1986_LISEL_MIC	0x0008  /* MIC_1/2 pins as LINE_IN source */
1721 #define AC97_AD1986_SRU		0x0010	/* sample rate unlock */
1722 #define AC97_AD1986_SOSEL	0x0020	/* SURROUND_OUT amplifiers input sel */
1723 #define AC97_AD1986_2MIC	0x0040	/* 2-channel mic select */
1724 #define AC97_AD1986_SPRD	0x0080	/* SPREAD enable */
1725 #define AC97_AD1986_DMIX0	0x0100	/* downmix mode: */
1726 					/*  0 = 6-to-4, 1 = 6-to-2 downmix */
1727 #define AC97_AD1986_DMIX1	0x0200	/* downmix mode: 1 = enabled */
1728 #define AC97_AD1986_CLDIS	0x0800	/* center/lfe disable */
1729 #define AC97_AD1986_SODIS	0x1000	/* SURROUND_OUT disable */
1730 #define AC97_AD1986_MSPLT	0x2000	/* mute split (read only 1) */
1731 #define AC97_AD1986_AC97NC	0x4000	/* AC97 no compatible mode (r/o 1) */
1732 #define AC97_AD1986_DACZ	0x8000	/* DAC zero-fill mode */
1733 
1734 /* MISC 2 bits (AD1986 register 0x70) */
1735 #define AC97_AD_MISC2		0x70	/* Misc Control Bits 2 (AD1986) */
1736 
1737 #define AC97_AD1986_CVREF0	0x0004	/* C/LFE VREF_OUT 2.25V */
1738 #define AC97_AD1986_CVREF1	0x0008	/* C/LFE VREF_OUT 0V */
1739 #define AC97_AD1986_CVREF2	0x0010	/* C/LFE VREF_OUT 3.7V */
1740 #define AC97_AD1986_CVREF_MASK \
1741 	(AC97_AD1986_CVREF2 | AC97_AD1986_CVREF1 | AC97_AD1986_CVREF0)
1742 #define AC97_AD1986_JSMAP	0x0020	/* Jack Sense Mapping 1 = alternate */
1743 #define AC97_AD1986_MMDIS	0x0080	/* Mono Mute Disable */
1744 #define AC97_AD1986_MVREF0	0x0400	/* MIC VREF_OUT 2.25V */
1745 #define AC97_AD1986_MVREF1	0x0800	/* MIC VREF_OUT 0V */
1746 #define AC97_AD1986_MVREF2	0x1000	/* MIC VREF_OUT 3.7V */
1747 #define AC97_AD1986_MVREF_MASK \
1748 	(AC97_AD1986_MVREF2 | AC97_AD1986_MVREF1 | AC97_AD1986_MVREF0)
1749 
1750 /* MISC 3 bits (AD1986 register 0x7a) */
1751 #define AC97_AD_MISC3		0x7a	/* Misc Control Bits 3 (AD1986) */
1752 
1753 #define AC97_AD1986_MMIX	0x0004	/* Mic Mix, left/right */
1754 #define AC97_AD1986_GPO		0x0008	/* General Purpose Out */
1755 #define AC97_AD1986_LOHPEN	0x0010	/* LINE_OUT headphone drive */
1756 #define AC97_AD1986_LVREF0	0x0100	/* LINE_OUT VREF_OUT 2.25V */
1757 #define AC97_AD1986_LVREF1	0x0200	/* LINE_OUT VREF_OUT 0V */
1758 #define AC97_AD1986_LVREF2	0x0400	/* LINE_OUT VREF_OUT 3.7V */
1759 #define AC97_AD1986_LVREF_MASK \
1760 	(AC97_AD1986_LVREF2 | AC97_AD1986_LVREF1 | AC97_AD1986_LVREF0)
1761 #define AC97_AD1986_JSINVA	0x0800	/* Jack Sense Invert SENSE_A */
1762 #define AC97_AD1986_LOSEL	0x1000	/* LINE_OUT amplifiers input select */
1763 #define AC97_AD1986_HPSEL0	0x2000	/* Headphone amplifiers */
1764 					/*   input select Surround DACs */
1765 #define AC97_AD1986_HPSEL1	0x4000	/* Headphone amplifiers input */
1766 					/*   select C/LFE DACs */
1767 #define AC97_AD1986_JSINVB	0x8000	/* Jack Sense Invert SENSE_B */
1768 
1769 /* Serial Config bits (AD1986 register 0x74) (incomplete) */
1770 #define AC97_AD1986_OMS0	0x0100	/* Optional Mic Selector bit 0 */
1771 #define AC97_AD1986_OMS1	0x0200	/* Optional Mic Selector bit 1 */
1772 #define AC97_AD1986_OMS2	0x0400	/* Optional Mic Selector bit 2 */
1773 #define AC97_AD1986_OMS_MASK \
1774 	(AC97_AD1986_OMS2 | AC97_AD1986_OMS1 | AC97_AD1986_OMS0)
1775 #define AC97_AD1986_OMS_M	0x0000  /* MIC_1/2 pins are MIC sources */
1776 #define AC97_AD1986_OMS_L	0x0100  /* LINE_IN pins are MIC sources */
1777 #define AC97_AD1986_OMS_C	0x0200  /* Center/LFE pins are MCI sources */
1778 #define AC97_AD1986_OMS_MC	0x0400  /* Mix of MIC and C/LFE pins */
1779 					/*   are MIC sources */
1780 #define AC97_AD1986_OMS_ML	0x0500  /* MIX of MIC and LINE_IN pins */
1781 					/*   are MIC sources */
1782 #define AC97_AD1986_OMS_LC	0x0600  /* MIX of LINE_IN and C/LFE pins */
1783 					/*   are MIC sources */
1784 #define AC97_AD1986_OMS_MLC	0x0700  /* MIX of MIC, LINE_IN, C/LFE pins */
1785 					/*   are MIC sources */
1786 
1787 
1788 static int snd_ac97_ad198x_spdif_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1789 {
1790 	static char *texts[2] = { "AC-Link", "A/D Converter" };
1791 
1792 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1793 	uinfo->count = 1;
1794 	uinfo->value.enumerated.items = 2;
1795 	if (uinfo->value.enumerated.item > 1)
1796 		uinfo->value.enumerated.item = 1;
1797 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1798 	return 0;
1799 }
1800 
1801 static int snd_ac97_ad198x_spdif_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1802 {
1803 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1804 	unsigned short val;
1805 
1806 	val = ac97->regs[AC97_AD_SERIAL_CFG];
1807 	ucontrol->value.enumerated.item[0] = (val >> 2) & 1;
1808 	return 0;
1809 }
1810 
1811 static int snd_ac97_ad198x_spdif_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1812 {
1813 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1814 	unsigned short val;
1815 
1816 	if (ucontrol->value.enumerated.item[0] > 1)
1817 		return -EINVAL;
1818 	val = ucontrol->value.enumerated.item[0] << 2;
1819 	return snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x0004, val);
1820 }
1821 
1822 static const struct snd_kcontrol_new snd_ac97_ad198x_spdif_source = {
1823 	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
1824 	.name	= SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1825 	.info	= snd_ac97_ad198x_spdif_source_info,
1826 	.get	= snd_ac97_ad198x_spdif_source_get,
1827 	.put	= snd_ac97_ad198x_spdif_source_put,
1828 };
1829 
1830 static int patch_ad198x_post_spdif(struct snd_ac97 * ac97)
1831 {
1832  	return patch_build_controls(ac97, &snd_ac97_ad198x_spdif_source, 1);
1833 }
1834 
1835 static const struct snd_kcontrol_new snd_ac97_ad1981x_jack_sense[] = {
1836 	AC97_SINGLE("Headphone Jack Sense", AC97_AD_JACK_SPDIF, 11, 1, 0),
1837 	AC97_SINGLE("Line Jack Sense", AC97_AD_JACK_SPDIF, 12, 1, 0),
1838 };
1839 
1840 /* black list to avoid HP/Line jack-sense controls
1841  * (SS vendor << 16 | device)
1842  */
1843 static unsigned int ad1981_jacks_blacklist[] = {
1844 	0x10140523, /* Thinkpad R40 */
1845 	0x10140534, /* Thinkpad X31 */
1846 	0x10140537, /* Thinkpad T41p */
1847 	0x10140554, /* Thinkpad T42p/R50p */
1848 	0x10140567, /* Thinkpad T43p 2668-G7U */
1849 	0x10140581, /* Thinkpad X41-2527 */
1850 	0x104380b0, /* Asus A7V8X-MX */
1851 	0x11790241, /* Toshiba Satellite A-15 S127 */
1852 	0x144dc01a, /* Samsung NP-X20C004/SEG */
1853 	0 /* end */
1854 };
1855 
1856 static int check_list(struct snd_ac97 *ac97, const unsigned int *list)
1857 {
1858 	u32 subid = ((u32)ac97->subsystem_vendor << 16) | ac97->subsystem_device;
1859 	for (; *list; list++)
1860 		if (*list == subid)
1861 			return 1;
1862 	return 0;
1863 }
1864 
1865 static int patch_ad1981a_specific(struct snd_ac97 * ac97)
1866 {
1867 	if (check_list(ac97, ad1981_jacks_blacklist))
1868 		return 0;
1869 	return patch_build_controls(ac97, snd_ac97_ad1981x_jack_sense,
1870 				    ARRAY_SIZE(snd_ac97_ad1981x_jack_sense));
1871 }
1872 
1873 static struct snd_ac97_build_ops patch_ad1981a_build_ops = {
1874 	.build_post_spdif = patch_ad198x_post_spdif,
1875 	.build_specific = patch_ad1981a_specific,
1876 #ifdef CONFIG_PM
1877 	.resume = ad18xx_resume
1878 #endif
1879 };
1880 
1881 /* white list to enable HP jack-sense bits
1882  * (SS vendor << 16 | device)
1883  */
1884 static unsigned int ad1981_jacks_whitelist[] = {
1885 	0x0e11005a, /* HP nc4000/4010 */
1886 	0x103c0890, /* HP nc6000 */
1887 	0x103c0938, /* HP nc4220 */
1888 	0x103c099c, /* HP nx6110 */
1889 	0x103c0944, /* HP nc6220 */
1890 	0x103c0934, /* HP nc8220 */
1891 	0x103c006d, /* HP nx9105 */
1892 	0x17340088, /* FSC Scenic-W */
1893 	0 /* end */
1894 };
1895 
1896 static void check_ad1981_hp_jack_sense(struct snd_ac97 *ac97)
1897 {
1898 	if (check_list(ac97, ad1981_jacks_whitelist))
1899 		/* enable headphone jack sense */
1900 		snd_ac97_update_bits(ac97, AC97_AD_JACK_SPDIF, 1<<11, 1<<11);
1901 }
1902 
1903 static int patch_ad1981a(struct snd_ac97 *ac97)
1904 {
1905 	patch_ad1881(ac97);
1906 	ac97->build_ops = &patch_ad1981a_build_ops;
1907 	snd_ac97_update_bits(ac97, AC97_AD_MISC, AC97_AD198X_MSPLT, AC97_AD198X_MSPLT);
1908 	ac97->flags |= AC97_STEREO_MUTES;
1909 	check_ad1981_hp_jack_sense(ac97);
1910 	return 0;
1911 }
1912 
1913 static const struct snd_kcontrol_new snd_ac97_ad198x_2cmic =
1914 AC97_SINGLE("Stereo Mic", AC97_AD_MISC, 6, 1, 0);
1915 
1916 static int patch_ad1981b_specific(struct snd_ac97 *ac97)
1917 {
1918 	int err;
1919 
1920 	if ((err = patch_build_controls(ac97, &snd_ac97_ad198x_2cmic, 1)) < 0)
1921 		return err;
1922 	if (check_list(ac97, ad1981_jacks_blacklist))
1923 		return 0;
1924 	return patch_build_controls(ac97, snd_ac97_ad1981x_jack_sense,
1925 				    ARRAY_SIZE(snd_ac97_ad1981x_jack_sense));
1926 }
1927 
1928 static struct snd_ac97_build_ops patch_ad1981b_build_ops = {
1929 	.build_post_spdif = patch_ad198x_post_spdif,
1930 	.build_specific = patch_ad1981b_specific,
1931 #ifdef CONFIG_PM
1932 	.resume = ad18xx_resume
1933 #endif
1934 };
1935 
1936 static int patch_ad1981b(struct snd_ac97 *ac97)
1937 {
1938 	patch_ad1881(ac97);
1939 	ac97->build_ops = &patch_ad1981b_build_ops;
1940 	snd_ac97_update_bits(ac97, AC97_AD_MISC, AC97_AD198X_MSPLT, AC97_AD198X_MSPLT);
1941 	ac97->flags |= AC97_STEREO_MUTES;
1942 	check_ad1981_hp_jack_sense(ac97);
1943 	return 0;
1944 }
1945 
1946 #define snd_ac97_ad1888_lohpsel_info	snd_ctl_boolean_mono_info
1947 
1948 static int snd_ac97_ad1888_lohpsel_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1949 {
1950 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1951 	unsigned short val;
1952 
1953 	val = ac97->regs[AC97_AD_MISC];
1954 	ucontrol->value.integer.value[0] = !(val & AC97_AD198X_LOSEL);
1955 	return 0;
1956 }
1957 
1958 static int snd_ac97_ad1888_lohpsel_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1959 {
1960 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1961 	unsigned short val;
1962 
1963 	val = !ucontrol->value.integer.value[0]
1964 		? (AC97_AD198X_LOSEL | AC97_AD198X_HPSEL) : 0;
1965 	return snd_ac97_update_bits(ac97, AC97_AD_MISC,
1966 				    AC97_AD198X_LOSEL | AC97_AD198X_HPSEL, val);
1967 }
1968 
1969 static int snd_ac97_ad1888_downmix_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1970 {
1971 	static char *texts[3] = {"Off", "6 -> 4", "6 -> 2"};
1972 
1973 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1974 	uinfo->count = 1;
1975 	uinfo->value.enumerated.items = 3;
1976 	if (uinfo->value.enumerated.item > 2)
1977 		uinfo->value.enumerated.item = 2;
1978 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1979 	return 0;
1980 }
1981 
1982 static int snd_ac97_ad1888_downmix_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1983 {
1984 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1985 	unsigned short val;
1986 
1987 	val = ac97->regs[AC97_AD_MISC];
1988 	if (!(val & AC97_AD198X_DMIX1))
1989 		ucontrol->value.enumerated.item[0] = 0;
1990 	else
1991 		ucontrol->value.enumerated.item[0] = 1 + ((val >> 8) & 1);
1992 	return 0;
1993 }
1994 
1995 static int snd_ac97_ad1888_downmix_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1996 {
1997 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1998 	unsigned short val;
1999 
2000 	if (ucontrol->value.enumerated.item[0] > 2)
2001 		return -EINVAL;
2002 	if (ucontrol->value.enumerated.item[0] == 0)
2003 		val = 0;
2004 	else
2005 		val = AC97_AD198X_DMIX1 |
2006 			((ucontrol->value.enumerated.item[0] - 1) << 8);
2007 	return snd_ac97_update_bits(ac97, AC97_AD_MISC,
2008 				    AC97_AD198X_DMIX0 | AC97_AD198X_DMIX1, val);
2009 }
2010 
2011 static void ad1888_update_jacks(struct snd_ac97 *ac97)
2012 {
2013 	unsigned short val = 0;
2014 	/* clear LODIS if shared jack is to be used for Surround out */
2015 	if (is_shared_linein(ac97))
2016 		val |= (1 << 12);
2017 	/* clear CLDIS if shared jack is to be used for C/LFE out */
2018 	if (is_shared_micin(ac97))
2019 		val |= (1 << 11);
2020 	/* shared Line-In */
2021 	snd_ac97_update_bits(ac97, AC97_AD_MISC, (1 << 11) | (1 << 12), val);
2022 }
2023 
2024 static const struct snd_kcontrol_new snd_ac97_ad1888_controls[] = {
2025 	{
2026 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2027 		.name = "Exchange Front/Surround",
2028 		.info = snd_ac97_ad1888_lohpsel_info,
2029 		.get = snd_ac97_ad1888_lohpsel_get,
2030 		.put = snd_ac97_ad1888_lohpsel_put
2031 	},
2032 	AC97_SINGLE("V_REFOUT Enable", AC97_AD_MISC, 2, 1, 1),
2033 	AC97_SINGLE("High Pass Filter Enable", AC97_AD_TEST2, 12, 1, 1),
2034 	AC97_SINGLE("Spread Front to Surround and Center/LFE", AC97_AD_MISC, 7, 1, 0),
2035 	{
2036 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2037 		.name = "Downmix",
2038 		.info = snd_ac97_ad1888_downmix_info,
2039 		.get = snd_ac97_ad1888_downmix_get,
2040 		.put = snd_ac97_ad1888_downmix_put
2041 	},
2042 	AC97_SURROUND_JACK_MODE_CTL,
2043 	AC97_CHANNEL_MODE_CTL,
2044 
2045 	AC97_SINGLE("Headphone Jack Sense", AC97_AD_JACK_SPDIF, 10, 1, 0),
2046 	AC97_SINGLE("Line Jack Sense", AC97_AD_JACK_SPDIF, 12, 1, 0),
2047 };
2048 
2049 static int patch_ad1888_specific(struct snd_ac97 *ac97)
2050 {
2051 	/* rename 0x04 as "Master" and 0x02 as "Master Surround" */
2052 	snd_ac97_rename_vol_ctl(ac97, "Master Playback", "Master Surround Playback");
2053 	snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback");
2054 	return patch_build_controls(ac97, snd_ac97_ad1888_controls, ARRAY_SIZE(snd_ac97_ad1888_controls));
2055 }
2056 
2057 static struct snd_ac97_build_ops patch_ad1888_build_ops = {
2058 	.build_post_spdif = patch_ad198x_post_spdif,
2059 	.build_specific = patch_ad1888_specific,
2060 #ifdef CONFIG_PM
2061 	.resume = ad1888_resume,
2062 #endif
2063 	.update_jacks = ad1888_update_jacks,
2064 };
2065 
2066 static int patch_ad1888(struct snd_ac97 * ac97)
2067 {
2068 	unsigned short misc;
2069 
2070 	patch_ad1881(ac97);
2071 	ac97->build_ops = &patch_ad1888_build_ops;
2072 	/* Switch FRONT/SURROUND LINE-OUT/HP-OUT default connection */
2073 	/* it seems that most vendors connect line-out connector to headphone out of AC'97 */
2074 	/* AD-compatible mode */
2075 	/* Stereo mutes enabled */
2076 	misc = snd_ac97_read(ac97, AC97_AD_MISC);
2077 	snd_ac97_write_cache(ac97, AC97_AD_MISC, misc |
2078 			     AC97_AD198X_LOSEL |
2079 			     AC97_AD198X_HPSEL |
2080 			     AC97_AD198X_MSPLT |
2081 			     AC97_AD198X_AC97NC);
2082 	ac97->flags |= AC97_STEREO_MUTES;
2083 	return 0;
2084 }
2085 
2086 static int patch_ad1980_specific(struct snd_ac97 *ac97)
2087 {
2088 	int err;
2089 
2090 	if ((err = patch_ad1888_specific(ac97)) < 0)
2091 		return err;
2092 	return patch_build_controls(ac97, &snd_ac97_ad198x_2cmic, 1);
2093 }
2094 
2095 static struct snd_ac97_build_ops patch_ad1980_build_ops = {
2096 	.build_post_spdif = patch_ad198x_post_spdif,
2097 	.build_specific = patch_ad1980_specific,
2098 #ifdef CONFIG_PM
2099 	.resume = ad18xx_resume,
2100 #endif
2101 	.update_jacks = ad1888_update_jacks,
2102 };
2103 
2104 static int patch_ad1980(struct snd_ac97 * ac97)
2105 {
2106 	patch_ad1888(ac97);
2107 	ac97->build_ops = &patch_ad1980_build_ops;
2108 	return 0;
2109 }
2110 
2111 static int snd_ac97_ad1985_vrefout_info(struct snd_kcontrol *kcontrol,
2112 					struct snd_ctl_elem_info *uinfo)
2113 {
2114 	static char *texts[4] = {"High-Z", "3.7 V", "2.25 V", "0 V"};
2115 
2116 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2117 	uinfo->count = 1;
2118 	uinfo->value.enumerated.items = 4;
2119 	if (uinfo->value.enumerated.item > 3)
2120 		uinfo->value.enumerated.item = 3;
2121 	strcpy(uinfo->value.enumerated.name,
2122 	       texts[uinfo->value.enumerated.item]);
2123 	return 0;
2124 }
2125 
2126 static int snd_ac97_ad1985_vrefout_get(struct snd_kcontrol *kcontrol,
2127 				       struct snd_ctl_elem_value *ucontrol)
2128 {
2129 	static const int reg2ctrl[4] = {2, 0, 1, 3};
2130 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2131 	unsigned short val;
2132 	val = (ac97->regs[AC97_AD_MISC] & AC97_AD198X_VREF_MASK)
2133 	      >> AC97_AD198X_VREF_SHIFT;
2134 	ucontrol->value.enumerated.item[0] = reg2ctrl[val];
2135 	return 0;
2136 }
2137 
2138 static int snd_ac97_ad1985_vrefout_put(struct snd_kcontrol *kcontrol,
2139 				       struct snd_ctl_elem_value *ucontrol)
2140 {
2141 	static const int ctrl2reg[4] = {1, 2, 0, 3};
2142 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2143 	unsigned short val;
2144 
2145 	if (ucontrol->value.enumerated.item[0] > 3
2146 	    || ucontrol->value.enumerated.item[0] < 0)
2147 		return -EINVAL;
2148 	val = ctrl2reg[ucontrol->value.enumerated.item[0]]
2149 	      << AC97_AD198X_VREF_SHIFT;
2150 	return snd_ac97_update_bits(ac97, AC97_AD_MISC,
2151 				    AC97_AD198X_VREF_MASK, val);
2152 }
2153 
2154 static const struct snd_kcontrol_new snd_ac97_ad1985_controls[] = {
2155 	AC97_SINGLE("Exchange Center/LFE", AC97_AD_SERIAL_CFG, 3, 1, 0),
2156 	{
2157 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2158 		.name = "Exchange Front/Surround",
2159 		.info = snd_ac97_ad1888_lohpsel_info,
2160 		.get = snd_ac97_ad1888_lohpsel_get,
2161 		.put = snd_ac97_ad1888_lohpsel_put
2162 	},
2163 	AC97_SINGLE("High Pass Filter Enable", AC97_AD_TEST2, 12, 1, 1),
2164 	AC97_SINGLE("Spread Front to Surround and Center/LFE",
2165 		    AC97_AD_MISC, 7, 1, 0),
2166 	{
2167 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2168 		.name = "Downmix",
2169 		.info = snd_ac97_ad1888_downmix_info,
2170 		.get = snd_ac97_ad1888_downmix_get,
2171 		.put = snd_ac97_ad1888_downmix_put
2172 	},
2173 	{
2174 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2175 		.name = "V_REFOUT",
2176 		.info = snd_ac97_ad1985_vrefout_info,
2177 		.get = snd_ac97_ad1985_vrefout_get,
2178 		.put = snd_ac97_ad1985_vrefout_put
2179 	},
2180 	AC97_SURROUND_JACK_MODE_CTL,
2181 	AC97_CHANNEL_MODE_CTL,
2182 
2183 	AC97_SINGLE("Headphone Jack Sense", AC97_AD_JACK_SPDIF, 10, 1, 0),
2184 	AC97_SINGLE("Line Jack Sense", AC97_AD_JACK_SPDIF, 12, 1, 0),
2185 };
2186 
2187 static void ad1985_update_jacks(struct snd_ac97 *ac97)
2188 {
2189 	ad1888_update_jacks(ac97);
2190 	/* clear OMS if shared jack is to be used for C/LFE out */
2191 	snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 1 << 9,
2192 			     is_shared_micin(ac97) ? 1 << 9 : 0);
2193 }
2194 
2195 static int patch_ad1985_specific(struct snd_ac97 *ac97)
2196 {
2197 	int err;
2198 
2199 	/* rename 0x04 as "Master" and 0x02 as "Master Surround" */
2200 	snd_ac97_rename_vol_ctl(ac97, "Master Playback",
2201 				"Master Surround Playback");
2202 	snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback");
2203 
2204 	if ((err = patch_build_controls(ac97, &snd_ac97_ad198x_2cmic, 1)) < 0)
2205 		return err;
2206 
2207 	return patch_build_controls(ac97, snd_ac97_ad1985_controls,
2208 				    ARRAY_SIZE(snd_ac97_ad1985_controls));
2209 }
2210 
2211 static struct snd_ac97_build_ops patch_ad1985_build_ops = {
2212 	.build_post_spdif = patch_ad198x_post_spdif,
2213 	.build_specific = patch_ad1985_specific,
2214 #ifdef CONFIG_PM
2215 	.resume = ad18xx_resume,
2216 #endif
2217 	.update_jacks = ad1985_update_jacks,
2218 };
2219 
2220 static int patch_ad1985(struct snd_ac97 * ac97)
2221 {
2222 	unsigned short misc;
2223 
2224 	patch_ad1881(ac97);
2225 	ac97->build_ops = &patch_ad1985_build_ops;
2226 	misc = snd_ac97_read(ac97, AC97_AD_MISC);
2227 	/* switch front/surround line-out/hp-out */
2228 	/* AD-compatible mode */
2229 	/* Stereo mutes enabled */
2230 	snd_ac97_write_cache(ac97, AC97_AD_MISC, misc |
2231 			     AC97_AD198X_LOSEL |
2232 			     AC97_AD198X_HPSEL |
2233 			     AC97_AD198X_MSPLT |
2234 			     AC97_AD198X_AC97NC);
2235 	ac97->flags |= AC97_STEREO_MUTES;
2236 
2237 	/* update current jack configuration */
2238 	ad1985_update_jacks(ac97);
2239 
2240 	/* on AD1985 rev. 3, AC'97 revision bits are zero */
2241 	ac97->ext_id = (ac97->ext_id & ~AC97_EI_REV_MASK) | AC97_EI_REV_23;
2242 	return 0;
2243 }
2244 
2245 #define snd_ac97_ad1986_bool_info	snd_ctl_boolean_mono_info
2246 
2247 static int snd_ac97_ad1986_lososel_get(struct snd_kcontrol *kcontrol,
2248 				       struct snd_ctl_elem_value *ucontrol)
2249 {
2250 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2251 	unsigned short val;
2252 
2253 	val = ac97->regs[AC97_AD_MISC3];
2254 	ucontrol->value.integer.value[0] = (val & AC97_AD1986_LOSEL) != 0;
2255 	return 0;
2256 }
2257 
2258 static int snd_ac97_ad1986_lososel_put(struct snd_kcontrol *kcontrol,
2259 				       struct snd_ctl_elem_value *ucontrol)
2260 {
2261 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2262 	int ret0;
2263 	int ret1;
2264 	int sprd = (ac97->regs[AC97_AD_MISC] & AC97_AD1986_SPRD) != 0;
2265 
2266 	ret0 = snd_ac97_update_bits(ac97, AC97_AD_MISC3, AC97_AD1986_LOSEL,
2267 					ucontrol->value.integer.value[0] != 0
2268 				    ? AC97_AD1986_LOSEL : 0);
2269 	if (ret0 < 0)
2270 		return ret0;
2271 
2272 	/* SOSEL is set to values of "Spread" or "Exchange F/S" controls */
2273 	ret1 = snd_ac97_update_bits(ac97, AC97_AD_MISC, AC97_AD1986_SOSEL,
2274 				    (ucontrol->value.integer.value[0] != 0
2275 				     || sprd)
2276 				    ? AC97_AD1986_SOSEL : 0);
2277 	if (ret1 < 0)
2278 		return ret1;
2279 
2280 	return (ret0 > 0 || ret1 > 0) ? 1 : 0;
2281 }
2282 
2283 static int snd_ac97_ad1986_spread_get(struct snd_kcontrol *kcontrol,
2284 				      struct snd_ctl_elem_value *ucontrol)
2285 {
2286 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2287 	unsigned short val;
2288 
2289 	val = ac97->regs[AC97_AD_MISC];
2290 	ucontrol->value.integer.value[0] = (val & AC97_AD1986_SPRD) != 0;
2291 	return 0;
2292 }
2293 
2294 static int snd_ac97_ad1986_spread_put(struct snd_kcontrol *kcontrol,
2295 				      struct snd_ctl_elem_value *ucontrol)
2296 {
2297 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2298 	int ret0;
2299 	int ret1;
2300 	int sprd = (ac97->regs[AC97_AD_MISC3] & AC97_AD1986_LOSEL) != 0;
2301 
2302 	ret0 = snd_ac97_update_bits(ac97, AC97_AD_MISC, AC97_AD1986_SPRD,
2303 					ucontrol->value.integer.value[0] != 0
2304 				    ? AC97_AD1986_SPRD : 0);
2305 	if (ret0 < 0)
2306 		return ret0;
2307 
2308 	/* SOSEL is set to values of "Spread" or "Exchange F/S" controls */
2309 	ret1 = snd_ac97_update_bits(ac97, AC97_AD_MISC, AC97_AD1986_SOSEL,
2310 				    (ucontrol->value.integer.value[0] != 0
2311 				     || sprd)
2312 				    ? AC97_AD1986_SOSEL : 0);
2313 	if (ret1 < 0)
2314 		return ret1;
2315 
2316 	return (ret0 > 0 || ret1 > 0) ? 1 : 0;
2317 }
2318 
2319 static int snd_ac97_ad1986_miclisel_get(struct snd_kcontrol *kcontrol,
2320 					struct snd_ctl_elem_value *ucontrol)
2321 {
2322 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2323 
2324 	ucontrol->value.integer.value[0] = ac97->spec.ad18xx.swap_mic_linein;
2325 	return 0;
2326 }
2327 
2328 static int snd_ac97_ad1986_miclisel_put(struct snd_kcontrol *kcontrol,
2329 					struct snd_ctl_elem_value *ucontrol)
2330 {
2331 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2332 	unsigned char swap = ucontrol->value.integer.value[0] != 0;
2333 
2334 	if (swap != ac97->spec.ad18xx.swap_mic_linein) {
2335 		ac97->spec.ad18xx.swap_mic_linein = swap;
2336 		if (ac97->build_ops->update_jacks)
2337 			ac97->build_ops->update_jacks(ac97);
2338 		return 1;
2339 	}
2340 	return 0;
2341 }
2342 
2343 static int snd_ac97_ad1986_vrefout_get(struct snd_kcontrol *kcontrol,
2344 				       struct snd_ctl_elem_value *ucontrol)
2345 {
2346 	/* Use MIC_1/2 V_REFOUT as the "get" value */
2347 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2348 	unsigned short val;
2349 	unsigned short reg = ac97->regs[AC97_AD_MISC2];
2350 	if ((reg & AC97_AD1986_MVREF0) != 0)
2351 		val = 2;
2352 	else if ((reg & AC97_AD1986_MVREF1) != 0)
2353 		val = 3;
2354 	else if ((reg & AC97_AD1986_MVREF2) != 0)
2355 		val = 1;
2356 	else
2357 		val = 0;
2358 	ucontrol->value.enumerated.item[0] = val;
2359 	return 0;
2360 }
2361 
2362 static int snd_ac97_ad1986_vrefout_put(struct snd_kcontrol *kcontrol,
2363 				       struct snd_ctl_elem_value *ucontrol)
2364 {
2365 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2366 	unsigned short cval;
2367 	unsigned short lval;
2368 	unsigned short mval;
2369 	int cret;
2370 	int lret;
2371 	int mret;
2372 
2373 	switch (ucontrol->value.enumerated.item[0])
2374 	{
2375 	case 0: /* High-Z */
2376 		cval = 0;
2377 		lval = 0;
2378 		mval = 0;
2379 		break;
2380 	case 1: /* 3.7 V */
2381 		cval = AC97_AD1986_CVREF2;
2382 		lval = AC97_AD1986_LVREF2;
2383 		mval = AC97_AD1986_MVREF2;
2384 		break;
2385 	case 2: /* 2.25 V */
2386 		cval = AC97_AD1986_CVREF0;
2387 		lval = AC97_AD1986_LVREF0;
2388 		mval = AC97_AD1986_MVREF0;
2389 		break;
2390 	case 3: /* 0 V */
2391 		cval = AC97_AD1986_CVREF1;
2392 		lval = AC97_AD1986_LVREF1;
2393 		mval = AC97_AD1986_MVREF1;
2394 		break;
2395 	default:
2396 		return -EINVAL;
2397 	}
2398 
2399 	cret = snd_ac97_update_bits(ac97, AC97_AD_MISC2,
2400 				    AC97_AD1986_CVREF_MASK, cval);
2401 	if (cret < 0)
2402 		return cret;
2403 	lret = snd_ac97_update_bits(ac97, AC97_AD_MISC3,
2404 				    AC97_AD1986_LVREF_MASK, lval);
2405 	if (lret < 0)
2406 		return lret;
2407 	mret = snd_ac97_update_bits(ac97, AC97_AD_MISC2,
2408 				    AC97_AD1986_MVREF_MASK, mval);
2409 	if (mret < 0)
2410 		return mret;
2411 
2412 	return (cret > 0 || lret > 0 || mret > 0) ? 1 : 0;
2413 }
2414 
2415 static const struct snd_kcontrol_new snd_ac97_ad1986_controls[] = {
2416 	AC97_SINGLE("Exchange Center/LFE", AC97_AD_SERIAL_CFG, 3, 1, 0),
2417 	{
2418 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2419 		.name = "Exchange Front/Surround",
2420 		.info = snd_ac97_ad1986_bool_info,
2421 		.get = snd_ac97_ad1986_lososel_get,
2422 		.put = snd_ac97_ad1986_lososel_put
2423 	},
2424 	{
2425 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2426 		.name = "Exchange Mic/Line In",
2427 		.info = snd_ac97_ad1986_bool_info,
2428 		.get = snd_ac97_ad1986_miclisel_get,
2429 		.put = snd_ac97_ad1986_miclisel_put
2430 	},
2431 	{
2432 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2433 		.name = "Spread Front to Surround and Center/LFE",
2434 		.info = snd_ac97_ad1986_bool_info,
2435 		.get = snd_ac97_ad1986_spread_get,
2436 		.put = snd_ac97_ad1986_spread_put
2437 	},
2438 	{
2439 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2440 		.name = "Downmix",
2441 		.info = snd_ac97_ad1888_downmix_info,
2442 		.get = snd_ac97_ad1888_downmix_get,
2443 		.put = snd_ac97_ad1888_downmix_put
2444 	},
2445 	{
2446 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2447 		.name = "V_REFOUT",
2448 		.info = snd_ac97_ad1985_vrefout_info,
2449 		.get = snd_ac97_ad1986_vrefout_get,
2450 		.put = snd_ac97_ad1986_vrefout_put
2451 	},
2452 	AC97_SURROUND_JACK_MODE_CTL,
2453 	AC97_CHANNEL_MODE_CTL,
2454 
2455 	AC97_SINGLE("Headphone Jack Sense", AC97_AD_JACK_SPDIF, 10, 1, 0),
2456 	AC97_SINGLE("Line Jack Sense", AC97_AD_JACK_SPDIF, 12, 1, 0)
2457 };
2458 
2459 static void ad1986_update_jacks(struct snd_ac97 *ac97)
2460 {
2461 	unsigned short misc_val = 0;
2462 	unsigned short ser_val;
2463 
2464 	/* disable SURROUND and CENTER/LFE if not surround mode */
2465 	if (!is_surround_on(ac97))
2466 		misc_val |= AC97_AD1986_SODIS;
2467 	if (!is_clfe_on(ac97))
2468 		misc_val |= AC97_AD1986_CLDIS;
2469 
2470 	/* select line input (default=LINE_IN, SURROUND or MIC_1/2) */
2471 	if (is_shared_linein(ac97))
2472 		misc_val |= AC97_AD1986_LISEL_SURR;
2473 	else if (ac97->spec.ad18xx.swap_mic_linein != 0)
2474 		misc_val |= AC97_AD1986_LISEL_MIC;
2475 	snd_ac97_update_bits(ac97, AC97_AD_MISC,
2476 			     AC97_AD1986_SODIS | AC97_AD1986_CLDIS |
2477 			     AC97_AD1986_LISEL_MASK,
2478 			     misc_val);
2479 
2480 	/* select microphone input (MIC_1/2, Center/LFE or LINE_IN) */
2481 	if (is_shared_micin(ac97))
2482 		ser_val = AC97_AD1986_OMS_C;
2483 	else if (ac97->spec.ad18xx.swap_mic_linein != 0)
2484 		ser_val = AC97_AD1986_OMS_L;
2485 	else
2486 		ser_val = AC97_AD1986_OMS_M;
2487 	snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG,
2488 			     AC97_AD1986_OMS_MASK,
2489 			     ser_val);
2490 }
2491 
2492 static int patch_ad1986_specific(struct snd_ac97 *ac97)
2493 {
2494 	int err;
2495 
2496 	if ((err = patch_build_controls(ac97, &snd_ac97_ad198x_2cmic, 1)) < 0)
2497 		return err;
2498 
2499 	return patch_build_controls(ac97, snd_ac97_ad1986_controls,
2500 				    ARRAY_SIZE(snd_ac97_ad1985_controls));
2501 }
2502 
2503 static struct snd_ac97_build_ops patch_ad1986_build_ops = {
2504 	.build_post_spdif = patch_ad198x_post_spdif,
2505 	.build_specific = patch_ad1986_specific,
2506 #ifdef CONFIG_PM
2507 	.resume = ad18xx_resume,
2508 #endif
2509 	.update_jacks = ad1986_update_jacks,
2510 };
2511 
2512 static int patch_ad1986(struct snd_ac97 * ac97)
2513 {
2514 	patch_ad1881(ac97);
2515 	ac97->build_ops = &patch_ad1986_build_ops;
2516 	ac97->flags |= AC97_STEREO_MUTES;
2517 
2518 	/* update current jack configuration */
2519 	ad1986_update_jacks(ac97);
2520 
2521 	return 0;
2522 }
2523 
2524 
2525 /*
2526  * realtek ALC65x/850 codecs
2527  */
2528 static void alc650_update_jacks(struct snd_ac97 *ac97)
2529 {
2530 	int shared;
2531 
2532 	/* shared Line-In / Surround Out */
2533 	shared = is_shared_surrout(ac97);
2534 	snd_ac97_update_bits(ac97, AC97_ALC650_MULTICH, 1 << 9,
2535 			     shared ? (1 << 9) : 0);
2536 	/* update shared Mic In / Center/LFE Out */
2537 	shared = is_shared_clfeout(ac97);
2538 	/* disable/enable vref */
2539 	snd_ac97_update_bits(ac97, AC97_ALC650_CLOCK, 1 << 12,
2540 			     shared ? (1 << 12) : 0);
2541 	/* turn on/off center-on-mic */
2542 	snd_ac97_update_bits(ac97, AC97_ALC650_MULTICH, 1 << 10,
2543 			     shared ? (1 << 10) : 0);
2544 	/* GPIO0 high for mic */
2545 	snd_ac97_update_bits(ac97, AC97_ALC650_GPIO_STATUS, 0x100,
2546 			     shared ? 0 : 0x100);
2547 }
2548 
2549 static const struct snd_kcontrol_new snd_ac97_controls_alc650[] = {
2550 	AC97_SINGLE("Duplicate Front", AC97_ALC650_MULTICH, 0, 1, 0),
2551 	AC97_SINGLE("Surround Down Mix", AC97_ALC650_MULTICH, 1, 1, 0),
2552 	AC97_SINGLE("Center/LFE Down Mix", AC97_ALC650_MULTICH, 2, 1, 0),
2553 	AC97_SINGLE("Exchange Center/LFE", AC97_ALC650_MULTICH, 3, 1, 0),
2554 	/* 4: Analog Input To Surround */
2555 	/* 5: Analog Input To Center/LFE */
2556 	/* 6: Independent Master Volume Right */
2557 	/* 7: Independent Master Volume Left */
2558 	/* 8: reserved */
2559 	/* 9: Line-In/Surround share */
2560 	/* 10: Mic/CLFE share */
2561 	/* 11-13: in IEC958 controls */
2562 	AC97_SINGLE("Swap Surround Slot", AC97_ALC650_MULTICH, 14, 1, 0),
2563 #if 0 /* always set in patch_alc650 */
2564 	AC97_SINGLE("IEC958 Input Clock Enable", AC97_ALC650_CLOCK, 0, 1, 0),
2565 	AC97_SINGLE("IEC958 Input Pin Enable", AC97_ALC650_CLOCK, 1, 1, 0),
2566 	AC97_SINGLE("Surround DAC Switch", AC97_ALC650_SURR_DAC_VOL, 15, 1, 1),
2567 	AC97_DOUBLE("Surround DAC Volume", AC97_ALC650_SURR_DAC_VOL, 8, 0, 31, 1),
2568 	AC97_SINGLE("Center/LFE DAC Switch", AC97_ALC650_LFE_DAC_VOL, 15, 1, 1),
2569 	AC97_DOUBLE("Center/LFE DAC Volume", AC97_ALC650_LFE_DAC_VOL, 8, 0, 31, 1),
2570 #endif
2571 	AC97_SURROUND_JACK_MODE_CTL,
2572 	AC97_CHANNEL_MODE_CTL,
2573 };
2574 
2575 static const struct snd_kcontrol_new snd_ac97_spdif_controls_alc650[] = {
2576         AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), AC97_ALC650_MULTICH, 11, 1, 0),
2577         AC97_SINGLE("Analog to IEC958 Output", AC97_ALC650_MULTICH, 12, 1, 0),
2578 	/* disable this controls since it doesn't work as expected */
2579 	/* AC97_SINGLE("IEC958 Input Monitor", AC97_ALC650_MULTICH, 13, 1, 0), */
2580 };
2581 
2582 static const DECLARE_TLV_DB_SCALE(db_scale_5bit_3db_max, -4350, 150, 0);
2583 
2584 static int patch_alc650_specific(struct snd_ac97 * ac97)
2585 {
2586 	int err;
2587 
2588 	if ((err = patch_build_controls(ac97, snd_ac97_controls_alc650, ARRAY_SIZE(snd_ac97_controls_alc650))) < 0)
2589 		return err;
2590 	if (ac97->ext_id & AC97_EI_SPDIF) {
2591 		if ((err = patch_build_controls(ac97, snd_ac97_spdif_controls_alc650, ARRAY_SIZE(snd_ac97_spdif_controls_alc650))) < 0)
2592 			return err;
2593 	}
2594 	if (ac97->id != AC97_ID_ALC650F)
2595 		reset_tlv(ac97, "Master Playback Volume",
2596 			  db_scale_5bit_3db_max);
2597 	return 0;
2598 }
2599 
2600 static struct snd_ac97_build_ops patch_alc650_ops = {
2601 	.build_specific	= patch_alc650_specific,
2602 	.update_jacks = alc650_update_jacks
2603 };
2604 
2605 static int patch_alc650(struct snd_ac97 * ac97)
2606 {
2607 	unsigned short val;
2608 
2609 	ac97->build_ops = &patch_alc650_ops;
2610 
2611 	/* determine the revision */
2612 	val = snd_ac97_read(ac97, AC97_ALC650_REVISION) & 0x3f;
2613 	if (val < 3)
2614 		ac97->id = 0x414c4720;          /* Old version */
2615 	else if (val < 0x10)
2616 		ac97->id = 0x414c4721;          /* D version */
2617 	else if (val < 0x20)
2618 		ac97->id = 0x414c4722;          /* E version */
2619 	else if (val < 0x30)
2620 		ac97->id = 0x414c4723;          /* F version */
2621 
2622 	/* revision E or F */
2623 	/* FIXME: what about revision D ? */
2624 	ac97->spec.dev_flags = (ac97->id == 0x414c4722 ||
2625 				ac97->id == 0x414c4723);
2626 
2627 	/* enable AC97_ALC650_GPIO_SETUP, AC97_ALC650_CLOCK for R/W */
2628 	snd_ac97_write_cache(ac97, AC97_ALC650_GPIO_STATUS,
2629 		snd_ac97_read(ac97, AC97_ALC650_GPIO_STATUS) | 0x8000);
2630 
2631 	/* Enable SPDIF-IN only on Rev.E and above */
2632 	val = snd_ac97_read(ac97, AC97_ALC650_CLOCK);
2633 	/* SPDIF IN with pin 47 */
2634 	if (ac97->spec.dev_flags &&
2635 	    /* ASUS A6KM requires EAPD */
2636 	    ! (ac97->subsystem_vendor == 0x1043 &&
2637 	       ac97->subsystem_device == 0x1103))
2638 		val |= 0x03; /* enable */
2639 	else
2640 		val &= ~0x03; /* disable */
2641 	snd_ac97_write_cache(ac97, AC97_ALC650_CLOCK, val);
2642 
2643 	/* set default: slot 3,4,7,8,6,9
2644 	   spdif-in monitor off, analog-spdif off, spdif-in off
2645 	   center on mic off, surround on line-in off
2646 	   downmix off, duplicate front off
2647 	*/
2648 	snd_ac97_write_cache(ac97, AC97_ALC650_MULTICH, 0);
2649 
2650 	/* set GPIO0 for mic bias */
2651 	/* GPIO0 pin output, no interrupt, high */
2652 	snd_ac97_write_cache(ac97, AC97_ALC650_GPIO_SETUP,
2653 			     snd_ac97_read(ac97, AC97_ALC650_GPIO_SETUP) | 0x01);
2654 	snd_ac97_write_cache(ac97, AC97_ALC650_GPIO_STATUS,
2655 			     (snd_ac97_read(ac97, AC97_ALC650_GPIO_STATUS) | 0x100) & ~0x10);
2656 
2657 	/* full DAC volume */
2658 	snd_ac97_write_cache(ac97, AC97_ALC650_SURR_DAC_VOL, 0x0808);
2659 	snd_ac97_write_cache(ac97, AC97_ALC650_LFE_DAC_VOL, 0x0808);
2660 	return 0;
2661 }
2662 
2663 static void alc655_update_jacks(struct snd_ac97 *ac97)
2664 {
2665 	int shared;
2666 
2667 	/* shared Line-In / Surround Out */
2668 	shared = is_shared_surrout(ac97);
2669 	ac97_update_bits_page(ac97, AC97_ALC650_MULTICH, 1 << 9,
2670 			      shared ? (1 << 9) : 0, 0);
2671 	/* update shared Mic In / Center/LFE Out */
2672 	shared = is_shared_clfeout(ac97);
2673 	/* misc control; vrefout disable */
2674 	snd_ac97_update_bits(ac97, AC97_ALC650_CLOCK, 1 << 12,
2675 			     shared ? (1 << 12) : 0);
2676 	ac97_update_bits_page(ac97, AC97_ALC650_MULTICH, 1 << 10,
2677 			      shared ? (1 << 10) : 0, 0);
2678 }
2679 
2680 static const struct snd_kcontrol_new snd_ac97_controls_alc655[] = {
2681 	AC97_PAGE_SINGLE("Duplicate Front", AC97_ALC650_MULTICH, 0, 1, 0, 0),
2682 	AC97_SURROUND_JACK_MODE_CTL,
2683 	AC97_CHANNEL_MODE_CTL,
2684 };
2685 
2686 static int alc655_iec958_route_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2687 {
2688 	static char *texts_655[3] = { "PCM", "Analog In", "IEC958 In" };
2689 	static char *texts_658[4] = { "PCM", "Analog1 In", "Analog2 In", "IEC958 In" };
2690 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2691 
2692 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2693 	uinfo->count = 1;
2694 	uinfo->value.enumerated.items = ac97->spec.dev_flags ? 4 : 3;
2695 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2696 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2697 	strcpy(uinfo->value.enumerated.name,
2698 	       ac97->spec.dev_flags ?
2699 	       texts_658[uinfo->value.enumerated.item] :
2700 	       texts_655[uinfo->value.enumerated.item]);
2701 	return 0;
2702 }
2703 
2704 static int alc655_iec958_route_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2705 {
2706 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2707 	unsigned short val;
2708 
2709 	val = ac97->regs[AC97_ALC650_MULTICH];
2710 	val = (val >> 12) & 3;
2711 	if (ac97->spec.dev_flags && val == 3)
2712 		val = 0;
2713 	ucontrol->value.enumerated.item[0] = val;
2714 	return 0;
2715 }
2716 
2717 static int alc655_iec958_route_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2718 {
2719 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2720 
2721 	return ac97_update_bits_page(ac97, AC97_ALC650_MULTICH, 3 << 12,
2722 				     (unsigned short)ucontrol->value.enumerated.item[0] << 12,
2723 				     0);
2724 }
2725 
2726 static const struct snd_kcontrol_new snd_ac97_spdif_controls_alc655[] = {
2727         AC97_PAGE_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), AC97_ALC650_MULTICH, 11, 1, 0, 0),
2728 	/* disable this controls since it doesn't work as expected */
2729         /* AC97_PAGE_SINGLE("IEC958 Input Monitor", AC97_ALC650_MULTICH, 14, 1, 0, 0), */
2730 	{
2731 		.iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
2732 		.name   = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
2733 		.info   = alc655_iec958_route_info,
2734 		.get    = alc655_iec958_route_get,
2735 		.put    = alc655_iec958_route_put,
2736 	},
2737 };
2738 
2739 static int patch_alc655_specific(struct snd_ac97 * ac97)
2740 {
2741 	int err;
2742 
2743 	if ((err = patch_build_controls(ac97, snd_ac97_controls_alc655, ARRAY_SIZE(snd_ac97_controls_alc655))) < 0)
2744 		return err;
2745 	if (ac97->ext_id & AC97_EI_SPDIF) {
2746 		if ((err = patch_build_controls(ac97, snd_ac97_spdif_controls_alc655, ARRAY_SIZE(snd_ac97_spdif_controls_alc655))) < 0)
2747 			return err;
2748 	}
2749 	return 0;
2750 }
2751 
2752 static struct snd_ac97_build_ops patch_alc655_ops = {
2753 	.build_specific	= patch_alc655_specific,
2754 	.update_jacks = alc655_update_jacks
2755 };
2756 
2757 static int patch_alc655(struct snd_ac97 * ac97)
2758 {
2759 	unsigned int val;
2760 
2761 	if (ac97->id == AC97_ID_ALC658) {
2762 		ac97->spec.dev_flags = 1; /* ALC658 */
2763 		if ((snd_ac97_read(ac97, AC97_ALC650_REVISION) & 0x3f) == 2) {
2764 			ac97->id = AC97_ID_ALC658D;
2765 			ac97->spec.dev_flags = 2;
2766 		}
2767 	}
2768 
2769 	ac97->build_ops = &patch_alc655_ops;
2770 
2771 	/* assume only page 0 for writing cache */
2772 	snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, AC97_PAGE_VENDOR);
2773 
2774 	/* adjust default values */
2775 	val = snd_ac97_read(ac97, 0x7a); /* misc control */
2776 	if (ac97->spec.dev_flags) /* ALC658 */
2777 		val &= ~(1 << 1); /* Pin 47 is spdif input pin */
2778 	else { /* ALC655 */
2779 		if (ac97->subsystem_vendor == 0x1462 &&
2780 		    (ac97->subsystem_device == 0x0131 || /* MSI S270 laptop */
2781 		     ac97->subsystem_device == 0x0161 || /* LG K1 Express */
2782 		     ac97->subsystem_device == 0x0351 || /* MSI L725 laptop */
2783 		     ac97->subsystem_device == 0x0471 || /* MSI L720 laptop */
2784 		     ac97->subsystem_device == 0x0061))  /* MSI S250 laptop */
2785 			val &= ~(1 << 1); /* Pin 47 is EAPD (for internal speaker) */
2786 		else
2787 			val |= (1 << 1); /* Pin 47 is spdif input pin */
2788 	}
2789 	val &= ~(1 << 12); /* vref enable */
2790 	snd_ac97_write_cache(ac97, 0x7a, val);
2791 	/* set default: spdif-in enabled,
2792 	   spdif-in monitor off, spdif-in PCM off
2793 	   center on mic off, surround on line-in off
2794 	   duplicate front off
2795 	*/
2796 	snd_ac97_write_cache(ac97, AC97_ALC650_MULTICH, 1<<15);
2797 
2798 	/* full DAC volume */
2799 	snd_ac97_write_cache(ac97, AC97_ALC650_SURR_DAC_VOL, 0x0808);
2800 	snd_ac97_write_cache(ac97, AC97_ALC650_LFE_DAC_VOL, 0x0808);
2801 
2802 	/* update undocumented bit... */
2803 	if (ac97->id == AC97_ID_ALC658D)
2804 		snd_ac97_update_bits(ac97, 0x74, 0x0800, 0x0800);
2805 
2806 	return 0;
2807 }
2808 
2809 
2810 #define AC97_ALC850_JACK_SELECT	0x76
2811 #define AC97_ALC850_MISC1	0x7a
2812 
2813 static void alc850_update_jacks(struct snd_ac97 *ac97)
2814 {
2815 	int shared;
2816 
2817 	/* shared Line-In / Surround Out */
2818 	shared = is_shared_surrout(ac97);
2819 	/* SURR 1kOhm (bit4), Amp (bit5) */
2820 	snd_ac97_update_bits(ac97, AC97_ALC850_MISC1, (1<<4)|(1<<5),
2821 			     shared ? (1<<5) : (1<<4));
2822 	/* LINE-IN = 0, SURROUND = 2 */
2823 	snd_ac97_update_bits(ac97, AC97_ALC850_JACK_SELECT, 7 << 12,
2824 			     shared ? (2<<12) : (0<<12));
2825 	/* update shared Mic In / Center/LFE Out */
2826 	shared = is_shared_clfeout(ac97);
2827 	/* Vref disable (bit12), 1kOhm (bit13) */
2828 	snd_ac97_update_bits(ac97, AC97_ALC850_MISC1, (1<<12)|(1<<13),
2829 			     shared ? (1<<12) : (1<<13));
2830 	/* MIC-IN = 1, CENTER-LFE = 5 */
2831 	snd_ac97_update_bits(ac97, AC97_ALC850_JACK_SELECT, 7 << 4,
2832 			     shared ? (5<<4) : (1<<4));
2833 }
2834 
2835 static const struct snd_kcontrol_new snd_ac97_controls_alc850[] = {
2836 	AC97_PAGE_SINGLE("Duplicate Front", AC97_ALC650_MULTICH, 0, 1, 0, 0),
2837 	AC97_SINGLE("Mic Front Input Switch", AC97_ALC850_JACK_SELECT, 15, 1, 1),
2838 	AC97_SURROUND_JACK_MODE_CTL,
2839 	AC97_CHANNEL_MODE_CTL,
2840 };
2841 
2842 static int patch_alc850_specific(struct snd_ac97 *ac97)
2843 {
2844 	int err;
2845 
2846 	if ((err = patch_build_controls(ac97, snd_ac97_controls_alc850, ARRAY_SIZE(snd_ac97_controls_alc850))) < 0)
2847 		return err;
2848 	if (ac97->ext_id & AC97_EI_SPDIF) {
2849 		if ((err = patch_build_controls(ac97, snd_ac97_spdif_controls_alc655, ARRAY_SIZE(snd_ac97_spdif_controls_alc655))) < 0)
2850 			return err;
2851 	}
2852 	return 0;
2853 }
2854 
2855 static struct snd_ac97_build_ops patch_alc850_ops = {
2856 	.build_specific	= patch_alc850_specific,
2857 	.update_jacks = alc850_update_jacks
2858 };
2859 
2860 static int patch_alc850(struct snd_ac97 *ac97)
2861 {
2862 	ac97->build_ops = &patch_alc850_ops;
2863 
2864 	ac97->spec.dev_flags = 0; /* for IEC958 playback route - ALC655 compatible */
2865 
2866 	/* assume only page 0 for writing cache */
2867 	snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, AC97_PAGE_VENDOR);
2868 
2869 	/* adjust default values */
2870 	/* set default: spdif-in enabled,
2871 	   spdif-in monitor off, spdif-in PCM off
2872 	   center on mic off, surround on line-in off
2873 	   duplicate front off
2874 	*/
2875 	snd_ac97_write_cache(ac97, AC97_ALC650_MULTICH, 1<<15);
2876 	/* SURR_OUT: on, Surr 1kOhm: on, Surr Amp: off, Front 1kOhm: off
2877 	 * Front Amp: on, Vref: enable, Center 1kOhm: on, Mix: on
2878 	 */
2879 	snd_ac97_write_cache(ac97, 0x7a, (1<<1)|(1<<4)|(0<<5)|(1<<6)|
2880 			     (1<<7)|(0<<12)|(1<<13)|(0<<14));
2881 	/* detection UIO2,3: all path floating, UIO3: MIC, Vref2: disable,
2882 	 * UIO1: FRONT, Vref3: disable, UIO3: LINE, Front-Mic: mute
2883 	 */
2884 	snd_ac97_write_cache(ac97, 0x76, (0<<0)|(0<<2)|(1<<4)|(1<<7)|(2<<8)|
2885 			     (1<<11)|(0<<12)|(1<<15));
2886 
2887 	/* full DAC volume */
2888 	snd_ac97_write_cache(ac97, AC97_ALC650_SURR_DAC_VOL, 0x0808);
2889 	snd_ac97_write_cache(ac97, AC97_ALC650_LFE_DAC_VOL, 0x0808);
2890 	return 0;
2891 }
2892 
2893 
2894 /*
2895  * C-Media CM97xx codecs
2896  */
2897 static void cm9738_update_jacks(struct snd_ac97 *ac97)
2898 {
2899 	/* shared Line-In / Surround Out */
2900 	snd_ac97_update_bits(ac97, AC97_CM9738_VENDOR_CTRL, 1 << 10,
2901 			     is_shared_surrout(ac97) ? (1 << 10) : 0);
2902 }
2903 
2904 static const struct snd_kcontrol_new snd_ac97_cm9738_controls[] = {
2905 	AC97_SINGLE("Duplicate Front", AC97_CM9738_VENDOR_CTRL, 13, 1, 0),
2906 	AC97_SURROUND_JACK_MODE_CTL,
2907 	AC97_CHANNEL_MODE_4CH_CTL,
2908 };
2909 
2910 static int patch_cm9738_specific(struct snd_ac97 * ac97)
2911 {
2912 	return patch_build_controls(ac97, snd_ac97_cm9738_controls, ARRAY_SIZE(snd_ac97_cm9738_controls));
2913 }
2914 
2915 static struct snd_ac97_build_ops patch_cm9738_ops = {
2916 	.build_specific	= patch_cm9738_specific,
2917 	.update_jacks = cm9738_update_jacks
2918 };
2919 
2920 static int patch_cm9738(struct snd_ac97 * ac97)
2921 {
2922 	ac97->build_ops = &patch_cm9738_ops;
2923 	/* FIXME: can anyone confirm below? */
2924 	/* CM9738 has no PCM volume although the register reacts */
2925 	ac97->flags |= AC97_HAS_NO_PCM_VOL;
2926 	snd_ac97_write_cache(ac97, AC97_PCM, 0x8000);
2927 
2928 	return 0;
2929 }
2930 
2931 static int snd_ac97_cmedia_spdif_playback_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2932 {
2933 	static char *texts[] = { "Analog", "Digital" };
2934 
2935 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2936 	uinfo->count = 1;
2937 	uinfo->value.enumerated.items = 2;
2938 	if (uinfo->value.enumerated.item > 1)
2939 		uinfo->value.enumerated.item = 1;
2940 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2941 	return 0;
2942 }
2943 
2944 static int snd_ac97_cmedia_spdif_playback_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2945 {
2946 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2947 	unsigned short val;
2948 
2949 	val = ac97->regs[AC97_CM9739_SPDIF_CTRL];
2950 	ucontrol->value.enumerated.item[0] = (val >> 1) & 0x01;
2951 	return 0;
2952 }
2953 
2954 static int snd_ac97_cmedia_spdif_playback_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2955 {
2956 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2957 
2958 	return snd_ac97_update_bits(ac97, AC97_CM9739_SPDIF_CTRL,
2959 				    0x01 << 1,
2960 				    (ucontrol->value.enumerated.item[0] & 0x01) << 1);
2961 }
2962 
2963 static const struct snd_kcontrol_new snd_ac97_cm9739_controls_spdif[] = {
2964 	/* BIT 0: SPDI_EN - always true */
2965 	{ /* BIT 1: SPDIFS */
2966 		.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
2967 		.name	= SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
2968 		.info	= snd_ac97_cmedia_spdif_playback_source_info,
2969 		.get	= snd_ac97_cmedia_spdif_playback_source_get,
2970 		.put	= snd_ac97_cmedia_spdif_playback_source_put,
2971 	},
2972 	/* BIT 2: IG_SPIV */
2973 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,NONE) "Valid Switch", AC97_CM9739_SPDIF_CTRL, 2, 1, 0),
2974 	/* BIT 3: SPI2F */
2975 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,NONE) "Monitor", AC97_CM9739_SPDIF_CTRL, 3, 1, 0),
2976 	/* BIT 4: SPI2SDI */
2977 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), AC97_CM9739_SPDIF_CTRL, 4, 1, 0),
2978 	/* BIT 8: SPD32 - 32bit SPDIF - not supported yet */
2979 };
2980 
2981 static void cm9739_update_jacks(struct snd_ac97 *ac97)
2982 {
2983 	/* shared Line-In / Surround Out */
2984 	snd_ac97_update_bits(ac97, AC97_CM9739_MULTI_CHAN, 1 << 10,
2985 			     is_shared_surrout(ac97) ? (1 << 10) : 0);
2986 	/* shared Mic In / Center/LFE Out **/
2987 	snd_ac97_update_bits(ac97, AC97_CM9739_MULTI_CHAN, 0x3000,
2988 			     is_shared_clfeout(ac97) ? 0x1000 : 0x2000);
2989 }
2990 
2991 static const struct snd_kcontrol_new snd_ac97_cm9739_controls[] = {
2992 	AC97_SURROUND_JACK_MODE_CTL,
2993 	AC97_CHANNEL_MODE_CTL,
2994 };
2995 
2996 static int patch_cm9739_specific(struct snd_ac97 * ac97)
2997 {
2998 	return patch_build_controls(ac97, snd_ac97_cm9739_controls, ARRAY_SIZE(snd_ac97_cm9739_controls));
2999 }
3000 
3001 static int patch_cm9739_post_spdif(struct snd_ac97 * ac97)
3002 {
3003 	return patch_build_controls(ac97, snd_ac97_cm9739_controls_spdif, ARRAY_SIZE(snd_ac97_cm9739_controls_spdif));
3004 }
3005 
3006 static struct snd_ac97_build_ops patch_cm9739_ops = {
3007 	.build_specific	= patch_cm9739_specific,
3008 	.build_post_spdif = patch_cm9739_post_spdif,
3009 	.update_jacks = cm9739_update_jacks
3010 };
3011 
3012 static int patch_cm9739(struct snd_ac97 * ac97)
3013 {
3014 	unsigned short val;
3015 
3016 	ac97->build_ops = &patch_cm9739_ops;
3017 
3018 	/* CM9739/A has no Master and PCM volume although the register reacts */
3019 	ac97->flags |= AC97_HAS_NO_MASTER_VOL | AC97_HAS_NO_PCM_VOL;
3020 	snd_ac97_write_cache(ac97, AC97_MASTER, 0x8000);
3021 	snd_ac97_write_cache(ac97, AC97_PCM, 0x8000);
3022 
3023 	/* check spdif */
3024 	val = snd_ac97_read(ac97, AC97_EXTENDED_STATUS);
3025 	if (val & AC97_EA_SPCV) {
3026 		/* enable spdif in */
3027 		snd_ac97_write_cache(ac97, AC97_CM9739_SPDIF_CTRL,
3028 				     snd_ac97_read(ac97, AC97_CM9739_SPDIF_CTRL) | 0x01);
3029 		ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000; /* 48k only */
3030 	} else {
3031 		ac97->ext_id &= ~AC97_EI_SPDIF; /* disable extended-id */
3032 		ac97->rates[AC97_RATES_SPDIF] = 0;
3033 	}
3034 
3035 	/* set-up multi channel */
3036 	/* bit 14: 0 = SPDIF, 1 = EAPD */
3037 	/* bit 13: enable internal vref output for mic */
3038 	/* bit 12: disable center/lfe (swithable) */
3039 	/* bit 10: disable surround/line (switchable) */
3040 	/* bit 9: mix 2 surround off */
3041 	/* bit 4: undocumented; 0 mutes the CM9739A, which defaults to 1 */
3042 	/* bit 3: undocumented; surround? */
3043 	/* bit 0: dB */
3044 	val = snd_ac97_read(ac97, AC97_CM9739_MULTI_CHAN) & (1 << 4);
3045 	val |= (1 << 3);
3046 	val |= (1 << 13);
3047 	if (! (ac97->ext_id & AC97_EI_SPDIF))
3048 		val |= (1 << 14);
3049 	snd_ac97_write_cache(ac97, AC97_CM9739_MULTI_CHAN, val);
3050 
3051 	/* FIXME: set up GPIO */
3052 	snd_ac97_write_cache(ac97, 0x70, 0x0100);
3053 	snd_ac97_write_cache(ac97, 0x72, 0x0020);
3054 	/* Special exception for ASUS W1000/CMI9739. It does not have an SPDIF in. */
3055 	if (ac97->pci &&
3056 	     ac97->subsystem_vendor == 0x1043 &&
3057 	     ac97->subsystem_device == 0x1843) {
3058 		snd_ac97_write_cache(ac97, AC97_CM9739_SPDIF_CTRL,
3059 			snd_ac97_read(ac97, AC97_CM9739_SPDIF_CTRL) & ~0x01);
3060 		snd_ac97_write_cache(ac97, AC97_CM9739_MULTI_CHAN,
3061 			snd_ac97_read(ac97, AC97_CM9739_MULTI_CHAN) | (1 << 14));
3062 	}
3063 
3064 	return 0;
3065 }
3066 
3067 #define AC97_CM9761_MULTI_CHAN	0x64
3068 #define AC97_CM9761_FUNC	0x66
3069 #define AC97_CM9761_SPDIF_CTRL	0x6c
3070 
3071 static void cm9761_update_jacks(struct snd_ac97 *ac97)
3072 {
3073 	/* FIXME: check the bits for each model
3074 	 *        model 83 is confirmed to work
3075 	 */
3076 	static unsigned short surr_on[3][2] = {
3077 		{ 0x0008, 0x0000 }, /* 9761-78 & 82 */
3078 		{ 0x0000, 0x0008 }, /* 9761-82 rev.B */
3079 		{ 0x0000, 0x0008 }, /* 9761-83 */
3080 	};
3081 	static unsigned short clfe_on[3][2] = {
3082 		{ 0x0000, 0x1000 }, /* 9761-78 & 82 */
3083 		{ 0x1000, 0x0000 }, /* 9761-82 rev.B */
3084 		{ 0x0000, 0x1000 }, /* 9761-83 */
3085 	};
3086 	static unsigned short surr_shared[3][2] = {
3087 		{ 0x0000, 0x0400 }, /* 9761-78 & 82 */
3088 		{ 0x0000, 0x0400 }, /* 9761-82 rev.B */
3089 		{ 0x0000, 0x0400 }, /* 9761-83 */
3090 	};
3091 	static unsigned short clfe_shared[3][2] = {
3092 		{ 0x2000, 0x0880 }, /* 9761-78 & 82 */
3093 		{ 0x0000, 0x2880 }, /* 9761-82 rev.B */
3094 		{ 0x2000, 0x0800 }, /* 9761-83 */
3095 	};
3096 	unsigned short val = 0;
3097 
3098 	val |= surr_on[ac97->spec.dev_flags][is_surround_on(ac97)];
3099 	val |= clfe_on[ac97->spec.dev_flags][is_clfe_on(ac97)];
3100 	val |= surr_shared[ac97->spec.dev_flags][is_shared_surrout(ac97)];
3101 	val |= clfe_shared[ac97->spec.dev_flags][is_shared_clfeout(ac97)];
3102 
3103 	snd_ac97_update_bits(ac97, AC97_CM9761_MULTI_CHAN, 0x3c88, val);
3104 }
3105 
3106 static const struct snd_kcontrol_new snd_ac97_cm9761_controls[] = {
3107 	AC97_SURROUND_JACK_MODE_CTL,
3108 	AC97_CHANNEL_MODE_CTL,
3109 };
3110 
3111 static int cm9761_spdif_out_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
3112 {
3113 	static char *texts[] = { "AC-Link", "ADC", "SPDIF-In" };
3114 
3115 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3116 	uinfo->count = 1;
3117 	uinfo->value.enumerated.items = 3;
3118 	if (uinfo->value.enumerated.item > 2)
3119 		uinfo->value.enumerated.item = 2;
3120 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
3121 	return 0;
3122 }
3123 
3124 static int cm9761_spdif_out_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3125 {
3126 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
3127 
3128 	if (ac97->regs[AC97_CM9761_FUNC] & 0x1)
3129 		ucontrol->value.enumerated.item[0] = 2; /* SPDIF-loopback */
3130 	else if (ac97->regs[AC97_CM9761_SPDIF_CTRL] & 0x2)
3131 		ucontrol->value.enumerated.item[0] = 1; /* ADC loopback */
3132 	else
3133 		ucontrol->value.enumerated.item[0] = 0; /* AC-link */
3134 	return 0;
3135 }
3136 
3137 static int cm9761_spdif_out_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3138 {
3139 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
3140 
3141 	if (ucontrol->value.enumerated.item[0] == 2)
3142 		return snd_ac97_update_bits(ac97, AC97_CM9761_FUNC, 0x1, 0x1);
3143 	snd_ac97_update_bits(ac97, AC97_CM9761_FUNC, 0x1, 0);
3144 	return snd_ac97_update_bits(ac97, AC97_CM9761_SPDIF_CTRL, 0x2,
3145 				    ucontrol->value.enumerated.item[0] == 1 ? 0x2 : 0);
3146 }
3147 
3148 static const char *cm9761_dac_clock[] = { "AC-Link", "SPDIF-In", "Both" };
3149 static const struct ac97_enum cm9761_dac_clock_enum =
3150 	AC97_ENUM_SINGLE(AC97_CM9761_SPDIF_CTRL, 9, 3, cm9761_dac_clock);
3151 
3152 static const struct snd_kcontrol_new snd_ac97_cm9761_controls_spdif[] = {
3153 	{ /* BIT 1: SPDIFS */
3154 		.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
3155 		.name	= SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
3156 		.info = cm9761_spdif_out_source_info,
3157 		.get = cm9761_spdif_out_source_get,
3158 		.put = cm9761_spdif_out_source_put,
3159 	},
3160 	/* BIT 2: IG_SPIV */
3161 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,NONE) "Valid Switch", AC97_CM9761_SPDIF_CTRL, 2, 1, 0),
3162 	/* BIT 3: SPI2F */
3163 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,NONE) "Monitor", AC97_CM9761_SPDIF_CTRL, 3, 1, 0),
3164 	/* BIT 4: SPI2SDI */
3165 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), AC97_CM9761_SPDIF_CTRL, 4, 1, 0),
3166 	/* BIT 9-10: DAC_CTL */
3167 	AC97_ENUM("DAC Clock Source", cm9761_dac_clock_enum),
3168 };
3169 
3170 static int patch_cm9761_post_spdif(struct snd_ac97 * ac97)
3171 {
3172 	return patch_build_controls(ac97, snd_ac97_cm9761_controls_spdif, ARRAY_SIZE(snd_ac97_cm9761_controls_spdif));
3173 }
3174 
3175 static int patch_cm9761_specific(struct snd_ac97 * ac97)
3176 {
3177 	return patch_build_controls(ac97, snd_ac97_cm9761_controls, ARRAY_SIZE(snd_ac97_cm9761_controls));
3178 }
3179 
3180 static struct snd_ac97_build_ops patch_cm9761_ops = {
3181 	.build_specific	= patch_cm9761_specific,
3182 	.build_post_spdif = patch_cm9761_post_spdif,
3183 	.update_jacks = cm9761_update_jacks
3184 };
3185 
3186 static int patch_cm9761(struct snd_ac97 *ac97)
3187 {
3188 	unsigned short val;
3189 
3190 	/* CM9761 has no PCM volume although the register reacts */
3191 	/* Master volume seems to have _some_ influence on the analog
3192 	 * input sounds
3193 	 */
3194 	ac97->flags |= /*AC97_HAS_NO_MASTER_VOL |*/ AC97_HAS_NO_PCM_VOL;
3195 	snd_ac97_write_cache(ac97, AC97_MASTER, 0x8808);
3196 	snd_ac97_write_cache(ac97, AC97_PCM, 0x8808);
3197 
3198 	ac97->spec.dev_flags = 0; /* 1 = model 82 revision B, 2 = model 83 */
3199 	if (ac97->id == AC97_ID_CM9761_82) {
3200 		unsigned short tmp;
3201 		/* check page 1, reg 0x60 */
3202 		val = snd_ac97_read(ac97, AC97_INT_PAGING);
3203 		snd_ac97_write_cache(ac97, AC97_INT_PAGING, (val & ~0x0f) | 0x01);
3204 		tmp = snd_ac97_read(ac97, 0x60);
3205 		ac97->spec.dev_flags = tmp & 1; /* revision B? */
3206 		snd_ac97_write_cache(ac97, AC97_INT_PAGING, val);
3207 	} else if (ac97->id == AC97_ID_CM9761_83)
3208 		ac97->spec.dev_flags = 2;
3209 
3210 	ac97->build_ops = &patch_cm9761_ops;
3211 
3212 	/* enable spdif */
3213 	/* force the SPDIF bit in ext_id - codec doesn't set this bit! */
3214         ac97->ext_id |= AC97_EI_SPDIF;
3215 	/* to be sure: we overwrite the ext status bits */
3216 	snd_ac97_write_cache(ac97, AC97_EXTENDED_STATUS, 0x05c0);
3217 	/* Don't set 0x0200 here.  This results in the silent analog output */
3218 	snd_ac97_write_cache(ac97, AC97_CM9761_SPDIF_CTRL, 0x0001); /* enable spdif-in */
3219 	ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000; /* 48k only */
3220 
3221 	/* set-up multi channel */
3222 	/* bit 15: pc master beep off
3223 	 * bit 14: pin47 = EAPD/SPDIF
3224 	 * bit 13: vref ctl [= cm9739]
3225 	 * bit 12: CLFE control (reverted on rev B)
3226 	 * bit 11: Mic/center share (reverted on rev B)
3227 	 * bit 10: suddound/line share
3228 	 * bit  9: Analog-in mix -> surround
3229 	 * bit  8: Analog-in mix -> CLFE
3230 	 * bit  7: Mic/LFE share (mic/center/lfe)
3231 	 * bit  5: vref select (9761A)
3232 	 * bit  4: front control
3233 	 * bit  3: surround control (revereted with rev B)
3234 	 * bit  2: front mic
3235 	 * bit  1: stereo mic
3236 	 * bit  0: mic boost level (0=20dB, 1=30dB)
3237 	 */
3238 
3239 #if 0
3240 	if (ac97->spec.dev_flags)
3241 		val = 0x0214;
3242 	else
3243 		val = 0x321c;
3244 #endif
3245 	val = snd_ac97_read(ac97, AC97_CM9761_MULTI_CHAN);
3246 	val |= (1 << 4); /* front on */
3247 	snd_ac97_write_cache(ac97, AC97_CM9761_MULTI_CHAN, val);
3248 
3249 	/* FIXME: set up GPIO */
3250 	snd_ac97_write_cache(ac97, 0x70, 0x0100);
3251 	snd_ac97_write_cache(ac97, 0x72, 0x0020);
3252 
3253 	return 0;
3254 }
3255 
3256 #define AC97_CM9780_SIDE	0x60
3257 #define AC97_CM9780_JACK	0x62
3258 #define AC97_CM9780_MIXER	0x64
3259 #define AC97_CM9780_MULTI_CHAN	0x66
3260 #define AC97_CM9780_SPDIF	0x6c
3261 
3262 static const char *cm9780_ch_select[] = { "Front", "Side", "Center/LFE", "Rear" };
3263 static const struct ac97_enum cm9780_ch_select_enum =
3264 	AC97_ENUM_SINGLE(AC97_CM9780_MULTI_CHAN, 6, 4, cm9780_ch_select);
3265 static const struct snd_kcontrol_new cm9780_controls[] = {
3266 	AC97_DOUBLE("Side Playback Switch", AC97_CM9780_SIDE, 15, 7, 1, 1),
3267 	AC97_DOUBLE("Side Playback Volume", AC97_CM9780_SIDE, 8, 0, 31, 0),
3268 	AC97_ENUM("Side Playback Route", cm9780_ch_select_enum),
3269 };
3270 
3271 static int patch_cm9780_specific(struct snd_ac97 *ac97)
3272 {
3273 	return patch_build_controls(ac97, cm9780_controls, ARRAY_SIZE(cm9780_controls));
3274 }
3275 
3276 static struct snd_ac97_build_ops patch_cm9780_ops = {
3277 	.build_specific	= patch_cm9780_specific,
3278 	.build_post_spdif = patch_cm9761_post_spdif	/* identical with CM9761 */
3279 };
3280 
3281 static int patch_cm9780(struct snd_ac97 *ac97)
3282 {
3283 	unsigned short val;
3284 
3285 	ac97->build_ops = &patch_cm9780_ops;
3286 
3287 	/* enable spdif */
3288 	if (ac97->ext_id & AC97_EI_SPDIF) {
3289 		ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000; /* 48k only */
3290 		val = snd_ac97_read(ac97, AC97_CM9780_SPDIF);
3291 		val |= 0x1; /* SPDI_EN */
3292 		snd_ac97_write_cache(ac97, AC97_CM9780_SPDIF, val);
3293 	}
3294 
3295 	return 0;
3296 }
3297 
3298 /*
3299  * VIA VT1616 codec
3300  */
3301 static const struct snd_kcontrol_new snd_ac97_controls_vt1616[] = {
3302 AC97_SINGLE("DC Offset removal", 0x5a, 10, 1, 0),
3303 AC97_SINGLE("Alternate Level to Surround Out", 0x5a, 15, 1, 0),
3304 AC97_SINGLE("Downmix LFE and Center to Front", 0x5a, 12, 1, 0),
3305 AC97_SINGLE("Downmix Surround to Front", 0x5a, 11, 1, 0),
3306 };
3307 
3308 static int patch_vt1616_specific(struct snd_ac97 * ac97)
3309 {
3310 	int err;
3311 
3312 	if (snd_ac97_try_bit(ac97, 0x5a, 9))
3313 		if ((err = patch_build_controls(ac97, &snd_ac97_controls_vt1616[0], 1)) < 0)
3314 			return err;
3315 	if ((err = patch_build_controls(ac97, &snd_ac97_controls_vt1616[1], ARRAY_SIZE(snd_ac97_controls_vt1616) - 1)) < 0)
3316 		return err;
3317 	return 0;
3318 }
3319 
3320 static struct snd_ac97_build_ops patch_vt1616_ops = {
3321 	.build_specific	= patch_vt1616_specific
3322 };
3323 
3324 static int patch_vt1616(struct snd_ac97 * ac97)
3325 {
3326 	ac97->build_ops = &patch_vt1616_ops;
3327 	return 0;
3328 }
3329 
3330 /*
3331  * VT1617A codec
3332  */
3333 
3334 /*
3335  * unfortunately, the vt1617a stashes the twiddlers required for
3336  * nooding the i/o jacks on 2 different regs. * thameans that we cant
3337  * use the easy way provided by AC97_ENUM_DOUBLE() we have to write
3338  * are own funcs.
3339  *
3340  * NB: this is absolutely and utterly different from the vt1618. dunno
3341  * about the 1616.
3342  */
3343 
3344 /* copied from ac97_surround_jack_mode_info() */
3345 static int snd_ac97_vt1617a_smart51_info(struct snd_kcontrol *kcontrol,
3346 					 struct snd_ctl_elem_info *uinfo)
3347 {
3348 	/* ordering in this list reflects vt1617a docs for Reg 20 and
3349 	 * 7a and Table 6 that lays out the matrix NB WRT Table6: SM51
3350 	 * is SM51EN *AND* it's Bit14, not Bit15 so the table is very
3351 	 * counter-intuitive */
3352 
3353 	static const char* texts[] = { "LineIn Mic1", "LineIn Mic1 Mic3",
3354 				       "Surr LFE/C Mic3", "LineIn LFE/C Mic3",
3355 				       "LineIn Mic2", "LineIn Mic2 Mic1",
3356 				       "Surr LFE Mic1", "Surr LFE Mic1 Mic2"};
3357 	return ac97_enum_text_info(kcontrol, uinfo, texts, 8);
3358 }
3359 
3360 static int snd_ac97_vt1617a_smart51_get(struct snd_kcontrol *kcontrol,
3361 					struct snd_ctl_elem_value *ucontrol)
3362 {
3363 	ushort usSM51, usMS;
3364 
3365 	struct snd_ac97 *pac97;
3366 
3367 	pac97 = snd_kcontrol_chip(kcontrol); /* grab codec handle */
3368 
3369 	/* grab our desirec bits, then mash them together in a manner
3370 	 * consistent with Table 6 on page 17 in the 1617a docs */
3371 
3372 	usSM51 = snd_ac97_read(pac97, 0x7a) >> 14;
3373 	usMS   = snd_ac97_read(pac97, 0x20) >> 8;
3374 
3375 	ucontrol->value.enumerated.item[0] = (usSM51 << 1) + usMS;
3376 
3377 	return 0;
3378 }
3379 
3380 static int snd_ac97_vt1617a_smart51_put(struct snd_kcontrol *kcontrol,
3381 					struct snd_ctl_elem_value *ucontrol)
3382 {
3383 	ushort usSM51, usMS, usReg;
3384 
3385 	struct snd_ac97 *pac97;
3386 
3387 	pac97 = snd_kcontrol_chip(kcontrol); /* grab codec handle */
3388 
3389 	usSM51 = ucontrol->value.enumerated.item[0] >> 1;
3390 	usMS   = ucontrol->value.enumerated.item[0] &  1;
3391 
3392 	/* push our values into the register - consider that things will be left
3393 	 * in a funky state if the write fails */
3394 
3395 	usReg = snd_ac97_read(pac97, 0x7a);
3396 	snd_ac97_write_cache(pac97, 0x7a, (usReg & 0x3FFF) + (usSM51 << 14));
3397 	usReg = snd_ac97_read(pac97, 0x20);
3398 	snd_ac97_write_cache(pac97, 0x20, (usReg & 0xFEFF) + (usMS   <<  8));
3399 
3400 	return 0;
3401 }
3402 
3403 static const struct snd_kcontrol_new snd_ac97_controls_vt1617a[] = {
3404 
3405 	AC97_SINGLE("Center/LFE Exchange", 0x5a, 8, 1, 0),
3406 	/*
3407 	 * These are used to enable/disable surround sound on motherboards
3408 	 * that have 3 bidirectional analog jacks
3409 	 */
3410 	{
3411 		.iface         = SNDRV_CTL_ELEM_IFACE_MIXER,
3412 		.name          = "Smart 5.1 Select",
3413 		.info          = snd_ac97_vt1617a_smart51_info,
3414 		.get           = snd_ac97_vt1617a_smart51_get,
3415 		.put           = snd_ac97_vt1617a_smart51_put,
3416 	},
3417 };
3418 
3419 int patch_vt1617a(struct snd_ac97 * ac97)
3420 {
3421 	int err = 0;
3422 
3423 	/* we choose to not fail out at this point, but we tell the
3424 	   caller when we return */
3425 
3426 	err = patch_build_controls(ac97, &snd_ac97_controls_vt1617a[0],
3427 				   ARRAY_SIZE(snd_ac97_controls_vt1617a));
3428 
3429 	/* bring analog power consumption to normal by turning off the
3430 	 * headphone amplifier, like WinXP driver for EPIA SP
3431 	 */
3432 	snd_ac97_write_cache(ac97, 0x5c, 0x20);
3433 	ac97->ext_id |= AC97_EI_SPDIF;	/* force the detection of spdif */
3434 	ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000;
3435 	ac97->build_ops = &patch_vt1616_ops;
3436 
3437 	return err;
3438 }
3439 
3440 /*
3441  */
3442 static void it2646_update_jacks(struct snd_ac97 *ac97)
3443 {
3444 	/* shared Line-In / Surround Out */
3445 	snd_ac97_update_bits(ac97, 0x76, 1 << 9,
3446 			     is_shared_surrout(ac97) ? (1<<9) : 0);
3447 	/* shared Mic / Center/LFE Out */
3448 	snd_ac97_update_bits(ac97, 0x76, 1 << 10,
3449 			     is_shared_clfeout(ac97) ? (1<<10) : 0);
3450 }
3451 
3452 static const struct snd_kcontrol_new snd_ac97_controls_it2646[] = {
3453 	AC97_SURROUND_JACK_MODE_CTL,
3454 	AC97_CHANNEL_MODE_CTL,
3455 };
3456 
3457 static const struct snd_kcontrol_new snd_ac97_spdif_controls_it2646[] = {
3458 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0x76, 11, 1, 0),
3459 	AC97_SINGLE("Analog to IEC958 Output", 0x76, 12, 1, 0),
3460 	AC97_SINGLE("IEC958 Input Monitor", 0x76, 13, 1, 0),
3461 };
3462 
3463 static int patch_it2646_specific(struct snd_ac97 * ac97)
3464 {
3465 	int err;
3466 	if ((err = patch_build_controls(ac97, snd_ac97_controls_it2646, ARRAY_SIZE(snd_ac97_controls_it2646))) < 0)
3467 		return err;
3468 	if ((err = patch_build_controls(ac97, snd_ac97_spdif_controls_it2646, ARRAY_SIZE(snd_ac97_spdif_controls_it2646))) < 0)
3469 		return err;
3470 	return 0;
3471 }
3472 
3473 static struct snd_ac97_build_ops patch_it2646_ops = {
3474 	.build_specific	= patch_it2646_specific,
3475 	.update_jacks = it2646_update_jacks
3476 };
3477 
3478 static int patch_it2646(struct snd_ac97 * ac97)
3479 {
3480 	ac97->build_ops = &patch_it2646_ops;
3481 	/* full DAC volume */
3482 	snd_ac97_write_cache(ac97, 0x5E, 0x0808);
3483 	snd_ac97_write_cache(ac97, 0x7A, 0x0808);
3484 	return 0;
3485 }
3486 
3487 /*
3488  * Si3036 codec
3489  */
3490 
3491 #define AC97_SI3036_CHIP_ID     0x5a
3492 #define AC97_SI3036_LINE_CFG    0x5c
3493 
3494 static const struct snd_kcontrol_new snd_ac97_controls_si3036[] = {
3495 AC97_DOUBLE("Modem Speaker Volume", 0x5c, 14, 12, 3, 1)
3496 };
3497 
3498 static int patch_si3036_specific(struct snd_ac97 * ac97)
3499 {
3500 	int idx, err;
3501 	for (idx = 0; idx < ARRAY_SIZE(snd_ac97_controls_si3036); idx++)
3502 		if ((err = snd_ctl_add(ac97->bus->card, snd_ctl_new1(&snd_ac97_controls_si3036[idx], ac97))) < 0)
3503 			return err;
3504 	return 0;
3505 }
3506 
3507 static struct snd_ac97_build_ops patch_si3036_ops = {
3508 	.build_specific	= patch_si3036_specific,
3509 };
3510 
3511 static int mpatch_si3036(struct snd_ac97 * ac97)
3512 {
3513 	ac97->build_ops = &patch_si3036_ops;
3514 	snd_ac97_write_cache(ac97, 0x5c, 0xf210 );
3515 	snd_ac97_write_cache(ac97, 0x68, 0);
3516 	return 0;
3517 }
3518 
3519 /*
3520  * LM 4550 Codec
3521  *
3522  * We use a static resolution table since LM4550 codec cannot be
3523  * properly autoprobed to determine the resolution via
3524  * check_volume_resolution().
3525  */
3526 
3527 static struct snd_ac97_res_table lm4550_restbl[] = {
3528 	{ AC97_MASTER, 0x1f1f },
3529 	{ AC97_HEADPHONE, 0x1f1f },
3530 	{ AC97_MASTER_MONO, 0x001f },
3531 	{ AC97_PC_BEEP, 0x001f },	/* LSB is ignored */
3532 	{ AC97_PHONE, 0x001f },
3533 	{ AC97_MIC, 0x001f },
3534 	{ AC97_LINE, 0x1f1f },
3535 	{ AC97_CD, 0x1f1f },
3536 	{ AC97_VIDEO, 0x1f1f },
3537 	{ AC97_AUX, 0x1f1f },
3538 	{ AC97_PCM, 0x1f1f },
3539 	{ AC97_REC_GAIN, 0x0f0f },
3540 	{ } /* terminator */
3541 };
3542 
3543 static int patch_lm4550(struct snd_ac97 *ac97)
3544 {
3545 	ac97->res_table = lm4550_restbl;
3546 	return 0;
3547 }
3548 
3549 /*
3550  *  UCB1400 codec (http://www.semiconductors.philips.com/acrobat_download/datasheets/UCB1400-02.pdf)
3551  */
3552 static const struct snd_kcontrol_new snd_ac97_controls_ucb1400[] = {
3553 /* enable/disable headphone driver which allows direct connection to
3554    stereo headphone without the use of external DC blocking
3555    capacitors */
3556 AC97_SINGLE("Headphone Driver", 0x6a, 6, 1, 0),
3557 /* Filter used to compensate the DC offset is added in the ADC to remove idle
3558    tones from the audio band. */
3559 AC97_SINGLE("DC Filter", 0x6a, 4, 1, 0),
3560 /* Control smart-low-power mode feature. Allows automatic power down
3561    of unused blocks in the ADC analog front end and the PLL. */
3562 AC97_SINGLE("Smart Low Power Mode", 0x6c, 4, 3, 0),
3563 };
3564 
3565 static int patch_ucb1400_specific(struct snd_ac97 * ac97)
3566 {
3567 	int idx, err;
3568 	for (idx = 0; idx < ARRAY_SIZE(snd_ac97_controls_ucb1400); idx++)
3569 		if ((err = snd_ctl_add(ac97->bus->card, snd_ctl_new1(&snd_ac97_controls_ucb1400[idx], ac97))) < 0)
3570 			return err;
3571 	return 0;
3572 }
3573 
3574 static struct snd_ac97_build_ops patch_ucb1400_ops = {
3575 	.build_specific	= patch_ucb1400_specific,
3576 };
3577 
3578 static int patch_ucb1400(struct snd_ac97 * ac97)
3579 {
3580 	ac97->build_ops = &patch_ucb1400_ops;
3581 	/* enable headphone driver and smart low power mode by default */
3582 	snd_ac97_write(ac97, 0x6a, 0x0050);
3583 	snd_ac97_write(ac97, 0x6c, 0x0030);
3584 	return 0;
3585 }
3586