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