xref: /linux/sound/pci/ac97/ac97_patch.c (revision eed656493a459bbc0fdf687fa8f43f87946d8d3a)
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *  Universal interface for Audio Codec '97
4  *
5  *  For more details look to AC '97 component specification revision 2.2
6  *  by Intel Corporation (http://developer.intel.com) and to datasheets
7  *  for specific codecs.
8  *
9  *
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of the GNU General Public License as published by
12  *   the Free Software Foundation; either version 2 of the License, or
13  *   (at your option) any later version.
14  *
15  *   This program is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with this program; if not, write to the Free Software
22  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  *
24  */
25 
26 #include <sound/driver.h>
27 #include <linux/delay.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/mutex.h>
31 
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/control.h>
35 #include <sound/ac97_codec.h>
36 #include "ac97_patch.h"
37 #include "ac97_id.h"
38 #include "ac97_local.h"
39 
40 /*
41  *  Chip specific initialization
42  */
43 
44 static int patch_build_controls(struct snd_ac97 * ac97, const struct snd_kcontrol_new *controls, int count)
45 {
46 	int idx, err;
47 
48 	for (idx = 0; idx < count; idx++)
49 		if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&controls[idx], ac97))) < 0)
50 			return err;
51 	return 0;
52 }
53 
54 /* set to the page, update bits and restore the page */
55 static int ac97_update_bits_page(struct snd_ac97 *ac97, unsigned short reg, unsigned short mask, unsigned short value, unsigned short page)
56 {
57 	unsigned short page_save;
58 	int ret;
59 
60 	mutex_lock(&ac97->page_mutex);
61 	page_save = snd_ac97_read(ac97, AC97_INT_PAGING) & AC97_PAGE_MASK;
62 	snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page);
63 	ret = snd_ac97_update_bits(ac97, reg, mask, value);
64 	snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page_save);
65 	mutex_unlock(&ac97->page_mutex); /* unlock paging */
66 	return ret;
67 }
68 
69 /*
70  * shared line-in/mic controls
71  */
72 static int ac97_enum_text_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo,
73 			       const char **texts, unsigned int nums)
74 {
75 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
76 	uinfo->count = 1;
77 	uinfo->value.enumerated.items = nums;
78 	if (uinfo->value.enumerated.item > nums - 1)
79 		uinfo->value.enumerated.item = nums - 1;
80 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
81 	return 0;
82 }
83 
84 static int ac97_surround_jack_mode_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
85 {
86 	static const char *texts[] = { "Shared", "Independent" };
87 	return ac97_enum_text_info(kcontrol, uinfo, texts, 2);
88 }
89 
90 static int ac97_surround_jack_mode_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
91 {
92 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
93 
94 	ucontrol->value.enumerated.item[0] = ac97->indep_surround;
95 	return 0;
96 }
97 
98 static int ac97_surround_jack_mode_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
99 {
100 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
101 	unsigned char indep = !!ucontrol->value.enumerated.item[0];
102 
103 	if (indep != ac97->indep_surround) {
104 		ac97->indep_surround = indep;
105 		if (ac97->build_ops->update_jacks)
106 			ac97->build_ops->update_jacks(ac97);
107 		return 1;
108 	}
109 	return 0;
110 }
111 
112 static int ac97_channel_mode_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
113 {
114 	static const char *texts[] = { "2ch", "4ch", "6ch" };
115 	if (kcontrol->private_value)
116 		return ac97_enum_text_info(kcontrol, uinfo, texts, 2); /* 4ch only */
117 	return ac97_enum_text_info(kcontrol, uinfo, texts, 3);
118 }
119 
120 static int ac97_channel_mode_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
121 {
122 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
123 
124 	ucontrol->value.enumerated.item[0] = ac97->channel_mode;
125 	return 0;
126 }
127 
128 static int ac97_channel_mode_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
129 {
130 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
131 	unsigned char mode = ucontrol->value.enumerated.item[0];
132 
133 	if (mode != ac97->channel_mode) {
134 		ac97->channel_mode = mode;
135 		if (ac97->build_ops->update_jacks)
136 			ac97->build_ops->update_jacks(ac97);
137 		return 1;
138 	}
139 	return 0;
140 }
141 
142 #define AC97_SURROUND_JACK_MODE_CTL \
143 	{ \
144 		.iface	= SNDRV_CTL_ELEM_IFACE_MIXER, \
145 		.name	= "Surround Jack Mode", \
146 		.info = ac97_surround_jack_mode_info, \
147 		.get = ac97_surround_jack_mode_get, \
148 		.put = ac97_surround_jack_mode_put, \
149 	}
150 #define AC97_CHANNEL_MODE_CTL \
151 	{ \
152 		.iface	= SNDRV_CTL_ELEM_IFACE_MIXER, \
153 		.name	= "Channel Mode", \
154 		.info = ac97_channel_mode_info, \
155 		.get = ac97_channel_mode_get, \
156 		.put = ac97_channel_mode_put, \
157 	}
158 #define AC97_CHANNEL_MODE_4CH_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 = 1, \
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 static inline int is_shared_linein(struct snd_ac97 *ac97)
179 {
180 	return ! ac97->indep_surround && is_surround_on(ac97);
181 }
182 
183 static inline int is_shared_micin(struct snd_ac97 *ac97)
184 {
185 	return ! ac97->indep_surround && is_clfe_on(ac97);
186 }
187 
188 
189 /* The following snd_ac97_ymf753_... items added by David Shust (dshust@shustring.com) */
190 
191 /* It is possible to indicate to the Yamaha YMF753 the type of speakers being used. */
192 static int snd_ac97_ymf753_info_speaker(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
193 {
194 	static char *texts[3] = {
195 		"Standard", "Small", "Smaller"
196 	};
197 
198 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
199 	uinfo->count = 1;
200 	uinfo->value.enumerated.items = 3;
201 	if (uinfo->value.enumerated.item > 2)
202 		uinfo->value.enumerated.item = 2;
203 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
204 	return 0;
205 }
206 
207 static int snd_ac97_ymf753_get_speaker(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
208 {
209 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
210 	unsigned short val;
211 
212 	val = ac97->regs[AC97_YMF753_3D_MODE_SEL];
213 	val = (val >> 10) & 3;
214 	if (val > 0)    /* 0 = invalid */
215 		val--;
216 	ucontrol->value.enumerated.item[0] = val;
217 	return 0;
218 }
219 
220 static int snd_ac97_ymf753_put_speaker(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
221 {
222 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
223 	unsigned short val;
224 
225 	if (ucontrol->value.enumerated.item[0] > 2)
226 		return -EINVAL;
227 	val = (ucontrol->value.enumerated.item[0] + 1) << 10;
228 	return snd_ac97_update(ac97, AC97_YMF753_3D_MODE_SEL, val);
229 }
230 
231 static const struct snd_kcontrol_new snd_ac97_ymf753_controls_speaker =
232 {
233 	.iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
234 	.name   = "3D Control - Speaker",
235 	.info   = snd_ac97_ymf753_info_speaker,
236 	.get    = snd_ac97_ymf753_get_speaker,
237 	.put    = snd_ac97_ymf753_put_speaker,
238 };
239 
240 /* It is possible to indicate to the Yamaha YMF753 the source to direct to the S/PDIF output. */
241 static int snd_ac97_ymf753_spdif_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
242 {
243 	static char *texts[2] = { "AC-Link", "A/D Converter" };
244 
245 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
246 	uinfo->count = 1;
247 	uinfo->value.enumerated.items = 2;
248 	if (uinfo->value.enumerated.item > 1)
249 		uinfo->value.enumerated.item = 1;
250 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
251 	return 0;
252 }
253 
254 static int snd_ac97_ymf753_spdif_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
255 {
256 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
257 	unsigned short val;
258 
259 	val = ac97->regs[AC97_YMF753_DIT_CTRL2];
260 	ucontrol->value.enumerated.item[0] = (val >> 1) & 1;
261 	return 0;
262 }
263 
264 static int snd_ac97_ymf753_spdif_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
265 {
266 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
267 	unsigned short val;
268 
269 	if (ucontrol->value.enumerated.item[0] > 1)
270 		return -EINVAL;
271 	val = ucontrol->value.enumerated.item[0] << 1;
272 	return snd_ac97_update_bits(ac97, AC97_YMF753_DIT_CTRL2, 0x0002, val);
273 }
274 
275 /* The AC'97 spec states that the S/PDIF signal is to be output at pin 48.
276    The YMF753 will output the S/PDIF signal to pin 43, 47 (EAPD), or 48.
277    By default, no output pin is selected, and the S/PDIF signal is not output.
278    There is also a bit to mute S/PDIF output in a vendor-specific register. */
279 static int snd_ac97_ymf753_spdif_output_pin_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
280 {
281 	static char *texts[3] = { "Disabled", "Pin 43", "Pin 48" };
282 
283 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
284 	uinfo->count = 1;
285 	uinfo->value.enumerated.items = 3;
286 	if (uinfo->value.enumerated.item > 2)
287 		uinfo->value.enumerated.item = 2;
288 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
289 	return 0;
290 }
291 
292 static int snd_ac97_ymf753_spdif_output_pin_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
293 {
294 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
295 	unsigned short val;
296 
297 	val = ac97->regs[AC97_YMF753_DIT_CTRL2];
298 	ucontrol->value.enumerated.item[0] = (val & 0x0008) ? 2 : (val & 0x0020) ? 1 : 0;
299 	return 0;
300 }
301 
302 static int snd_ac97_ymf753_spdif_output_pin_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
303 {
304 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
305 	unsigned short val;
306 
307 	if (ucontrol->value.enumerated.item[0] > 2)
308 		return -EINVAL;
309 	val = (ucontrol->value.enumerated.item[0] == 2) ? 0x0008 :
310 	      (ucontrol->value.enumerated.item[0] == 1) ? 0x0020 : 0;
311 	return snd_ac97_update_bits(ac97, AC97_YMF753_DIT_CTRL2, 0x0028, val);
312 	/* The following can be used to direct S/PDIF output to pin 47 (EAPD).
313 	   snd_ac97_write_cache(ac97, 0x62, snd_ac97_read(ac97, 0x62) | 0x0008); */
314 }
315 
316 static const struct snd_kcontrol_new snd_ac97_ymf753_controls_spdif[3] = {
317 	{
318 		.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
319 		.name	= SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
320 		.info	= snd_ac97_ymf753_spdif_source_info,
321 		.get	= snd_ac97_ymf753_spdif_source_get,
322 		.put	= snd_ac97_ymf753_spdif_source_put,
323 	},
324 	{
325 		.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
326 		.name	= SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Output Pin",
327 		.info	= snd_ac97_ymf753_spdif_output_pin_info,
328 		.get	= snd_ac97_ymf753_spdif_output_pin_get,
329 		.put	= snd_ac97_ymf753_spdif_output_pin_put,
330 	},
331 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",NONE,NONE) "Mute", AC97_YMF753_DIT_CTRL2, 2, 1, 1)
332 };
333 
334 static int patch_yamaha_ymf753_3d(struct snd_ac97 * ac97)
335 {
336 	struct snd_kcontrol *kctl;
337 	int err;
338 
339 	if ((err = snd_ctl_add(ac97->bus->card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
340 		return err;
341 	strcpy(kctl->id.name, "3D Control - Wide");
342 	kctl->private_value = AC97_SINGLE_VALUE(AC97_3D_CONTROL, 9, 7, 0);
343 	snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000);
344 	if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&snd_ac97_ymf753_controls_speaker, ac97))) < 0)
345 		return err;
346 	snd_ac97_write_cache(ac97, AC97_YMF753_3D_MODE_SEL, 0x0c00);
347 	return 0;
348 }
349 
350 static int patch_yamaha_ymf753_post_spdif(struct snd_ac97 * ac97)
351 {
352 	int err;
353 
354 	if ((err = patch_build_controls(ac97, snd_ac97_ymf753_controls_spdif, ARRAY_SIZE(snd_ac97_ymf753_controls_spdif))) < 0)
355 		return err;
356 	return 0;
357 }
358 
359 static struct snd_ac97_build_ops patch_yamaha_ymf753_ops = {
360 	.build_3d	= patch_yamaha_ymf753_3d,
361 	.build_post_spdif = patch_yamaha_ymf753_post_spdif
362 };
363 
364 int patch_yamaha_ymf753(struct snd_ac97 * ac97)
365 {
366 	/* Patch for Yamaha YMF753, Copyright (c) by David Shust, dshust@shustring.com.
367 	   This chip has nonstandard and extended behaviour with regard to its S/PDIF output.
368 	   The AC'97 spec states that the S/PDIF signal is to be output at pin 48.
369 	   The YMF753 will ouput the S/PDIF signal to pin 43, 47 (EAPD), or 48.
370 	   By default, no output pin is selected, and the S/PDIF signal is not output.
371 	   There is also a bit to mute S/PDIF output in a vendor-specific register.
372 	*/
373 	ac97->build_ops = &patch_yamaha_ymf753_ops;
374 	ac97->caps |= AC97_BC_BASS_TREBLE;
375 	ac97->caps |= 0x04 << 10; /* Yamaha 3D enhancement */
376 	return 0;
377 }
378 
379 /*
380  * May 2, 2003 Liam Girdwood <liam.girdwood@wolfsonmicro.com>
381  *  removed broken wolfson00 patch.
382  *  added support for WM9705,WM9708,WM9709,WM9710,WM9711,WM9712 and WM9717.
383  */
384 
385 static const struct snd_kcontrol_new wm97xx_snd_ac97_controls[] = {
386 AC97_DOUBLE("Front Playback Volume", AC97_WM97XX_FMIXER_VOL, 8, 0, 31, 1),
387 AC97_SINGLE("Front Playback Switch", AC97_WM97XX_FMIXER_VOL, 15, 1, 1),
388 };
389 
390 static int patch_wolfson_wm9703_specific(struct snd_ac97 * ac97)
391 {
392 	/* This is known to work for the ViewSonic ViewPad 1000
393 	 * Randolph Bentson <bentson@holmsjoen.com>
394 	 * WM9703/9707/9708/9717
395 	 */
396 	int err, i;
397 
398 	for (i = 0; i < ARRAY_SIZE(wm97xx_snd_ac97_controls); i++) {
399 		if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&wm97xx_snd_ac97_controls[i], ac97))) < 0)
400 			return err;
401 	}
402 	snd_ac97_write_cache(ac97,  AC97_WM97XX_FMIXER_VOL, 0x0808);
403 	return 0;
404 }
405 
406 static struct snd_ac97_build_ops patch_wolfson_wm9703_ops = {
407 	.build_specific = patch_wolfson_wm9703_specific,
408 };
409 
410 int patch_wolfson03(struct snd_ac97 * ac97)
411 {
412 	ac97->build_ops = &patch_wolfson_wm9703_ops;
413 	return 0;
414 }
415 
416 static const struct snd_kcontrol_new wm9704_snd_ac97_controls[] = {
417 AC97_DOUBLE("Front Playback Volume", AC97_WM97XX_FMIXER_VOL, 8, 0, 31, 1),
418 AC97_SINGLE("Front Playback Switch", AC97_WM97XX_FMIXER_VOL, 15, 1, 1),
419 AC97_DOUBLE("Rear Playback Volume", AC97_WM9704_RMIXER_VOL, 8, 0, 31, 1),
420 AC97_SINGLE("Rear Playback Switch", AC97_WM9704_RMIXER_VOL, 15, 1, 1),
421 AC97_DOUBLE("Rear DAC Volume", AC97_WM9704_RPCM_VOL, 8, 0, 31, 1),
422 AC97_DOUBLE("Surround Volume", AC97_SURROUND_MASTER, 8, 0, 31, 1),
423 };
424 
425 static int patch_wolfson_wm9704_specific(struct snd_ac97 * ac97)
426 {
427 	int err, i;
428 	for (i = 0; i < ARRAY_SIZE(wm9704_snd_ac97_controls); i++) {
429 		if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&wm9704_snd_ac97_controls[i], ac97))) < 0)
430 			return err;
431 	}
432 	/* patch for DVD noise */
433 	snd_ac97_write_cache(ac97, AC97_WM9704_TEST, 0x0200);
434 	return 0;
435 }
436 
437 static struct snd_ac97_build_ops patch_wolfson_wm9704_ops = {
438 	.build_specific = patch_wolfson_wm9704_specific,
439 };
440 
441 int patch_wolfson04(struct snd_ac97 * ac97)
442 {
443 	/* WM9704M/9704Q */
444 	ac97->build_ops = &patch_wolfson_wm9704_ops;
445 	return 0;
446 }
447 
448 static int patch_wolfson_wm9705_specific(struct snd_ac97 * ac97)
449 {
450 	int err, i;
451 	for (i = 0; i < ARRAY_SIZE(wm97xx_snd_ac97_controls); i++) {
452 		if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&wm97xx_snd_ac97_controls[i], ac97))) < 0)
453 			return err;
454 	}
455 	snd_ac97_write_cache(ac97,  0x72, 0x0808);
456 	return 0;
457 }
458 
459 static struct snd_ac97_build_ops patch_wolfson_wm9705_ops = {
460 	.build_specific = patch_wolfson_wm9705_specific,
461 };
462 
463 int patch_wolfson05(struct snd_ac97 * ac97)
464 {
465 	/* WM9705, WM9710 */
466 	ac97->build_ops = &patch_wolfson_wm9705_ops;
467 	return 0;
468 }
469 
470 static const char* wm9711_alc_select[] = {"None", "Left", "Right", "Stereo"};
471 static const char* wm9711_alc_mix[] = {"Stereo", "Right", "Left", "None"};
472 static const char* wm9711_out3_src[] = {"Left", "VREF", "Left + Right", "Mono"};
473 static const char* wm9711_out3_lrsrc[] = {"Master Mix", "Headphone Mix"};
474 static const char* wm9711_rec_adc[] = {"Stereo", "Left", "Right", "Mute"};
475 static const char* wm9711_base[] = {"Linear Control", "Adaptive Boost"};
476 static const char* wm9711_rec_gain[] = {"+1.5dB Steps", "+0.75dB Steps"};
477 static const char* wm9711_mic[] = {"Mic 1", "Differential", "Mic 2", "Stereo"};
478 static const char* wm9711_rec_sel[] =
479 	{"Mic 1", "NC", "NC", "Master Mix", "Line", "Headphone Mix", "Phone Mix", "Phone"};
480 static const char* wm9711_ng_type[] = {"Constant Gain", "Mute"};
481 
482 static const struct ac97_enum wm9711_enum[] = {
483 AC97_ENUM_SINGLE(AC97_PCI_SVID, 14, 4, wm9711_alc_select),
484 AC97_ENUM_SINGLE(AC97_VIDEO, 10, 4, wm9711_alc_mix),
485 AC97_ENUM_SINGLE(AC97_AUX, 9, 4, wm9711_out3_src),
486 AC97_ENUM_SINGLE(AC97_AUX, 8, 2, wm9711_out3_lrsrc),
487 AC97_ENUM_SINGLE(AC97_REC_SEL, 12, 4, wm9711_rec_adc),
488 AC97_ENUM_SINGLE(AC97_MASTER_TONE, 15, 2, wm9711_base),
489 AC97_ENUM_DOUBLE(AC97_REC_GAIN, 14, 6, 2, wm9711_rec_gain),
490 AC97_ENUM_SINGLE(AC97_MIC, 5, 4, wm9711_mic),
491 AC97_ENUM_DOUBLE(AC97_REC_SEL, 8, 0, 8, wm9711_rec_sel),
492 AC97_ENUM_SINGLE(AC97_PCI_SVID, 5, 2, wm9711_ng_type),
493 };
494 
495 static const struct snd_kcontrol_new wm9711_snd_ac97_controls[] = {
496 AC97_SINGLE("ALC Target Volume", AC97_CODEC_CLASS_REV, 12, 15, 0),
497 AC97_SINGLE("ALC Hold Time", AC97_CODEC_CLASS_REV, 8, 15, 0),
498 AC97_SINGLE("ALC Decay Time", AC97_CODEC_CLASS_REV, 4, 15, 0),
499 AC97_SINGLE("ALC Attack Time", AC97_CODEC_CLASS_REV, 0, 15, 0),
500 AC97_ENUM("ALC Function", wm9711_enum[0]),
501 AC97_SINGLE("ALC Max Volume", AC97_PCI_SVID, 11, 7, 1),
502 AC97_SINGLE("ALC ZC Timeout", AC97_PCI_SVID, 9, 3, 1),
503 AC97_SINGLE("ALC ZC Switch", AC97_PCI_SVID, 8, 1, 0),
504 AC97_SINGLE("ALC NG Switch", AC97_PCI_SVID, 7, 1, 0),
505 AC97_ENUM("ALC NG Type", wm9711_enum[9]),
506 AC97_SINGLE("ALC NG Threshold", AC97_PCI_SVID, 0, 31, 1),
507 
508 AC97_SINGLE("Side Tone Switch", AC97_VIDEO, 15, 1, 1),
509 AC97_SINGLE("Side Tone Volume", AC97_VIDEO, 12, 7, 1),
510 AC97_ENUM("ALC Headphone Mux", wm9711_enum[1]),
511 AC97_SINGLE("ALC Headphone Volume", AC97_VIDEO, 7, 7, 1),
512 
513 AC97_SINGLE("Out3 Switch", AC97_AUX, 15, 1, 1),
514 AC97_SINGLE("Out3 ZC Switch", AC97_AUX, 7, 1, 1),
515 AC97_ENUM("Out3 Mux", wm9711_enum[2]),
516 AC97_ENUM("Out3 LR Mux", wm9711_enum[3]),
517 AC97_SINGLE("Out3 Volume", AC97_AUX, 0, 31, 1),
518 
519 AC97_SINGLE("Beep to Headphone Switch", AC97_PC_BEEP, 15, 1, 1),
520 AC97_SINGLE("Beep to Headphone Volume", AC97_PC_BEEP, 12, 7, 1),
521 AC97_SINGLE("Beep to Side Tone Switch", AC97_PC_BEEP, 11, 1, 1),
522 AC97_SINGLE("Beep to Side Tone Volume", AC97_PC_BEEP, 8, 7, 1),
523 AC97_SINGLE("Beep to Phone Switch", AC97_PC_BEEP, 7, 1, 1),
524 AC97_SINGLE("Beep to Phone Volume", AC97_PC_BEEP, 4, 7, 1),
525 
526 AC97_SINGLE("Aux to Headphone Switch", AC97_CD, 15, 1, 1),
527 AC97_SINGLE("Aux to Headphone Volume", AC97_CD, 12, 7, 1),
528 AC97_SINGLE("Aux to Side Tone Switch", AC97_CD, 11, 1, 1),
529 AC97_SINGLE("Aux to Side Tone Volume", AC97_CD, 8, 7, 1),
530 AC97_SINGLE("Aux to Phone Switch", AC97_CD, 7, 1, 1),
531 AC97_SINGLE("Aux to Phone Volume", AC97_CD, 4, 7, 1),
532 
533 AC97_SINGLE("Phone to Headphone Switch", AC97_PHONE, 15, 1, 1),
534 AC97_SINGLE("Phone to Master Switch", AC97_PHONE, 14, 1, 1),
535 
536 AC97_SINGLE("Line to Headphone Switch", AC97_LINE, 15, 1, 1),
537 AC97_SINGLE("Line to Master Switch", AC97_LINE, 14, 1, 1),
538 AC97_SINGLE("Line to Phone Switch", AC97_LINE, 13, 1, 1),
539 
540 AC97_SINGLE("PCM Playback to Headphone Switch", AC97_PCM, 15, 1, 1),
541 AC97_SINGLE("PCM Playback to Master Switch", AC97_PCM, 14, 1, 1),
542 AC97_SINGLE("PCM Playback to Phone Switch", AC97_PCM, 13, 1, 1),
543 
544 AC97_SINGLE("Capture 20dB Boost Switch", AC97_REC_SEL, 14, 1, 0),
545 AC97_ENUM("Capture to Phone Mux", wm9711_enum[4]),
546 AC97_SINGLE("Capture to Phone 20dB Boost Switch", AC97_REC_SEL, 11, 1, 1),
547 AC97_ENUM("Capture Select", wm9711_enum[8]),
548 
549 AC97_SINGLE("3D Upper Cut-off Switch", AC97_3D_CONTROL, 5, 1, 1),
550 AC97_SINGLE("3D Lower Cut-off Switch", AC97_3D_CONTROL, 4, 1, 1),
551 
552 AC97_ENUM("Bass Control", wm9711_enum[5]),
553 AC97_SINGLE("Bass Cut-off Switch", AC97_MASTER_TONE, 12, 1, 1),
554 AC97_SINGLE("Tone Cut-off Switch", AC97_MASTER_TONE, 4, 1, 1),
555 AC97_SINGLE("Playback Attenuate (-6dB) Switch", AC97_MASTER_TONE, 6, 1, 0),
556 
557 AC97_SINGLE("ADC Switch", AC97_REC_GAIN, 15, 1, 1),
558 AC97_ENUM("Capture Volume Steps", wm9711_enum[6]),
559 AC97_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 1),
560 AC97_SINGLE("Capture ZC Switch", AC97_REC_GAIN, 7, 1, 0),
561 
562 AC97_SINGLE("Mic 1 to Phone Switch", AC97_MIC, 14, 1, 1),
563 AC97_SINGLE("Mic 2 to Phone Switch", AC97_MIC, 13, 1, 1),
564 AC97_ENUM("Mic Select Source", wm9711_enum[7]),
565 AC97_SINGLE("Mic 1 Volume", AC97_MIC, 8, 32, 1),
566 AC97_SINGLE("Mic 20dB Boost Switch", AC97_MIC, 7, 1, 0),
567 
568 AC97_SINGLE("Master ZC Switch", AC97_MASTER, 7, 1, 0),
569 AC97_SINGLE("Headphone ZC Switch", AC97_HEADPHONE, 7, 1, 0),
570 AC97_SINGLE("Mono ZC Switch", AC97_MASTER_MONO, 7, 1, 0),
571 };
572 
573 static int patch_wolfson_wm9711_specific(struct snd_ac97 * ac97)
574 {
575 	int err, i;
576 
577 	for (i = 0; i < ARRAY_SIZE(wm9711_snd_ac97_controls); i++) {
578 		if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&wm9711_snd_ac97_controls[i], ac97))) < 0)
579 			return err;
580 	}
581 	snd_ac97_write_cache(ac97,  AC97_CODEC_CLASS_REV, 0x0808);
582 	snd_ac97_write_cache(ac97,  AC97_PCI_SVID, 0x0808);
583 	snd_ac97_write_cache(ac97,  AC97_VIDEO, 0x0808);
584 	snd_ac97_write_cache(ac97,  AC97_AUX, 0x0808);
585 	snd_ac97_write_cache(ac97,  AC97_PC_BEEP, 0x0808);
586 	snd_ac97_write_cache(ac97,  AC97_CD, 0x0000);
587 	return 0;
588 }
589 
590 static struct snd_ac97_build_ops patch_wolfson_wm9711_ops = {
591 	.build_specific = patch_wolfson_wm9711_specific,
592 };
593 
594 int patch_wolfson11(struct snd_ac97 * ac97)
595 {
596 	/* WM9711, WM9712 */
597 	ac97->build_ops = &patch_wolfson_wm9711_ops;
598 
599 	ac97->flags |= AC97_HAS_NO_REC_GAIN | AC97_STEREO_MUTES | AC97_HAS_NO_MIC |
600 		AC97_HAS_NO_PC_BEEP | AC97_HAS_NO_VIDEO | AC97_HAS_NO_CD;
601 
602 	return 0;
603 }
604 
605 static const char* wm9713_mic_mixer[] = {"Stereo", "Mic 1", "Mic 2", "Mute"};
606 static const char* wm9713_rec_mux[] = {"Stereo", "Left", "Right", "Mute"};
607 static const char* wm9713_rec_src[] =
608 	{"Mic 1", "Mic 2", "Line", "Mono In", "Headphone Mix", "Master Mix",
609 	"Mono Mix", "Zh"};
610 static const char* wm9713_rec_gain[] = {"+1.5dB Steps", "+0.75dB Steps"};
611 static const char* wm9713_alc_select[] = {"None", "Left", "Right", "Stereo"};
612 static const char* wm9713_mono_pga[] = {"Vmid", "Zh", "Mono Mix", "Inv 1"};
613 static const char* wm9713_spk_pga[] =
614 	{"Vmid", "Zh", "Headphone Mix", "Master Mix", "Inv", "NC", "NC", "NC"};
615 static const char* wm9713_hp_pga[] = {"Vmid", "Zh", "Headphone Mix", "NC"};
616 static const char* wm9713_out3_pga[] = {"Vmid", "Zh", "Inv 1", "NC"};
617 static const char* wm9713_out4_pga[] = {"Vmid", "Zh", "Inv 2", "NC"};
618 static const char* wm9713_dac_inv[] =
619 	{"Off", "Mono Mix", "Master Mix", "Headphone Mix L", "Headphone Mix R",
620 	"Headphone Mix Mono", "NC", "Vmid"};
621 static const char* wm9713_base[] = {"Linear Control", "Adaptive Boost"};
622 static const char* wm9713_ng_type[] = {"Constant Gain", "Mute"};
623 
624 static const struct ac97_enum wm9713_enum[] = {
625 AC97_ENUM_SINGLE(AC97_LINE, 3, 4, wm9713_mic_mixer),
626 AC97_ENUM_SINGLE(AC97_VIDEO, 14, 4, wm9713_rec_mux),
627 AC97_ENUM_SINGLE(AC97_VIDEO, 9, 4, wm9713_rec_mux),
628 AC97_ENUM_DOUBLE(AC97_VIDEO, 3, 0, 8, wm9713_rec_src),
629 AC97_ENUM_DOUBLE(AC97_CD, 14, 6, 2, wm9713_rec_gain),
630 AC97_ENUM_SINGLE(AC97_PCI_SVID, 14, 4, wm9713_alc_select),
631 AC97_ENUM_SINGLE(AC97_REC_GAIN, 14, 4, wm9713_mono_pga),
632 AC97_ENUM_DOUBLE(AC97_REC_GAIN, 11, 8, 8, wm9713_spk_pga),
633 AC97_ENUM_DOUBLE(AC97_REC_GAIN, 6, 4, 4, wm9713_hp_pga),
634 AC97_ENUM_SINGLE(AC97_REC_GAIN, 2, 4, wm9713_out3_pga),
635 AC97_ENUM_SINGLE(AC97_REC_GAIN, 0, 4, wm9713_out4_pga),
636 AC97_ENUM_DOUBLE(AC97_REC_GAIN_MIC, 13, 10, 8, wm9713_dac_inv),
637 AC97_ENUM_SINGLE(AC97_GENERAL_PURPOSE, 15, 2, wm9713_base),
638 AC97_ENUM_SINGLE(AC97_PCI_SVID, 5, 2, wm9713_ng_type),
639 };
640 
641 static const struct snd_kcontrol_new wm13_snd_ac97_controls[] = {
642 AC97_DOUBLE("Line In Volume", AC97_PC_BEEP, 8, 0, 31, 1),
643 AC97_SINGLE("Line In to Headphone Switch", AC97_PC_BEEP, 15, 1, 1),
644 AC97_SINGLE("Line In to Master Switch", AC97_PC_BEEP, 14, 1, 1),
645 AC97_SINGLE("Line In to Mono Switch", AC97_PC_BEEP, 13, 1, 1),
646 
647 AC97_DOUBLE("PCM Playback Volume", AC97_PHONE, 8, 0, 31, 1),
648 AC97_SINGLE("PCM Playback to Headphone Switch", AC97_PHONE, 15, 1, 1),
649 AC97_SINGLE("PCM Playback to Master Switch", AC97_PHONE, 14, 1, 1),
650 AC97_SINGLE("PCM Playback to Mono Switch", AC97_PHONE, 13, 1, 1),
651 
652 AC97_SINGLE("Mic 1 Volume", AC97_MIC, 8, 31, 1),
653 AC97_SINGLE("Mic 2 Volume", AC97_MIC, 0, 31, 1),
654 AC97_SINGLE("Mic 1 to Mono Switch", AC97_LINE, 7, 1, 1),
655 AC97_SINGLE("Mic 2 to Mono Switch", AC97_LINE, 6, 1, 1),
656 AC97_SINGLE("Mic Boost (+20dB) Switch", AC97_LINE, 5, 1, 0),
657 AC97_ENUM("Mic to Headphone Mux", wm9713_enum[0]),
658 AC97_SINGLE("Mic Headphone Mixer Volume", AC97_LINE, 0, 7, 1),
659 
660 AC97_SINGLE("Capture Switch", AC97_CD, 15, 1, 1),
661 AC97_ENUM("Capture Volume Steps", wm9713_enum[4]),
662 AC97_DOUBLE("Capture Volume", AC97_CD, 8, 0, 15, 0),
663 AC97_SINGLE("Capture ZC Switch", AC97_CD, 7, 1, 0),
664 
665 AC97_ENUM("Capture to Headphone Mux", wm9713_enum[1]),
666 AC97_SINGLE("Capture to Headphone Volume", AC97_VIDEO, 11, 7, 1),
667 AC97_ENUM("Capture to Mono Mux", wm9713_enum[2]),
668 AC97_SINGLE("Capture to Mono Boost (+20dB) Switch", AC97_VIDEO, 8, 1, 0),
669 AC97_SINGLE("Capture ADC Boost (+20dB) Switch", AC97_VIDEO, 6, 1, 0),
670 AC97_ENUM("Capture Select", wm9713_enum[3]),
671 
672 AC97_SINGLE("ALC Target Volume", AC97_CODEC_CLASS_REV, 12, 15, 0),
673 AC97_SINGLE("ALC Hold Time", AC97_CODEC_CLASS_REV, 8, 15, 0),
674 AC97_SINGLE("ALC Decay Time ", AC97_CODEC_CLASS_REV, 4, 15, 0),
675 AC97_SINGLE("ALC Attack Time", AC97_CODEC_CLASS_REV, 0, 15, 0),
676 AC97_ENUM("ALC Function", wm9713_enum[5]),
677 AC97_SINGLE("ALC Max Volume", AC97_PCI_SVID, 11, 7, 0),
678 AC97_SINGLE("ALC ZC Timeout", AC97_PCI_SVID, 9, 3, 0),
679 AC97_SINGLE("ALC ZC Switch", AC97_PCI_SVID, 8, 1, 0),
680 AC97_SINGLE("ALC NG Switch", AC97_PCI_SVID, 7, 1, 0),
681 AC97_ENUM("ALC NG Type", wm9713_enum[13]),
682 AC97_SINGLE("ALC NG Threshold", AC97_PCI_SVID, 0, 31, 0),
683 
684 AC97_DOUBLE("Master ZC Switch", AC97_MASTER, 14, 6, 1, 0),
685 AC97_DOUBLE("Headphone ZC Switch", AC97_HEADPHONE, 14, 6, 1, 0),
686 AC97_DOUBLE("Out3/4 ZC Switch", AC97_MASTER_MONO, 14, 6, 1, 0),
687 AC97_SINGLE("Master Right Switch", AC97_MASTER, 7, 1, 1),
688 AC97_SINGLE("Headphone Right Switch", AC97_HEADPHONE, 7, 1, 1),
689 AC97_SINGLE("Out3/4 Right Switch", AC97_MASTER_MONO, 7, 1, 1),
690 
691 AC97_SINGLE("Mono In to Headphone Switch", AC97_MASTER_TONE, 15, 1, 1),
692 AC97_SINGLE("Mono In to Master Switch", AC97_MASTER_TONE, 14, 1, 1),
693 AC97_SINGLE("Mono In Volume", AC97_MASTER_TONE, 8, 31, 1),
694 AC97_SINGLE("Mono Switch", AC97_MASTER_TONE, 7, 1, 1),
695 AC97_SINGLE("Mono ZC Switch", AC97_MASTER_TONE, 6, 1, 0),
696 AC97_SINGLE("Mono Volume", AC97_MASTER_TONE, 0, 31, 1),
697 
698 AC97_SINGLE("PC Beep to Headphone Switch", AC97_AUX, 15, 1, 1),
699 AC97_SINGLE("PC Beep to Headphone Volume", AC97_AUX, 12, 7, 1),
700 AC97_SINGLE("PC Beep to Master Switch", AC97_AUX, 11, 1, 1),
701 AC97_SINGLE("PC Beep to Master Volume", AC97_AUX, 8, 7, 1),
702 AC97_SINGLE("PC Beep to Mono Switch", AC97_AUX, 7, 1, 1),
703 AC97_SINGLE("PC Beep to Mono Volume", AC97_AUX, 4, 7, 1),
704 
705 AC97_SINGLE("Voice to Headphone Switch", AC97_PCM, 15, 1, 1),
706 AC97_SINGLE("Voice to Headphone Volume", AC97_PCM, 12, 7, 1),
707 AC97_SINGLE("Voice to Master Switch", AC97_PCM, 11, 1, 1),
708 AC97_SINGLE("Voice to Master Volume", AC97_PCM, 8, 7, 1),
709 AC97_SINGLE("Voice to Mono Switch", AC97_PCM, 7, 1, 1),
710 AC97_SINGLE("Voice to Mono Volume", AC97_PCM, 4, 7, 1),
711 
712 AC97_SINGLE("Aux to Headphone Switch", AC97_REC_SEL, 15, 1, 1),
713 AC97_SINGLE("Aux to Headphone Volume", AC97_REC_SEL, 12, 7, 1),
714 AC97_SINGLE("Aux to Master Switch", AC97_REC_SEL, 11, 1, 1),
715 AC97_SINGLE("Aux to Master Volume", AC97_REC_SEL, 8, 7, 1),
716 AC97_SINGLE("Aux to Mono Switch", AC97_REC_SEL, 7, 1, 1),
717 AC97_SINGLE("Aux to Mono Volume", AC97_REC_SEL, 4, 7, 1),
718 
719 AC97_ENUM("Mono Input Mux", wm9713_enum[6]),
720 AC97_ENUM("Master Input Mux", wm9713_enum[7]),
721 AC97_ENUM("Headphone Input Mux", wm9713_enum[8]),
722 AC97_ENUM("Out 3 Input Mux", wm9713_enum[9]),
723 AC97_ENUM("Out 4 Input Mux", wm9713_enum[10]),
724 
725 AC97_ENUM("Bass Control", wm9713_enum[12]),
726 AC97_SINGLE("Bass Cut-off Switch", AC97_GENERAL_PURPOSE, 12, 1, 1),
727 AC97_SINGLE("Tone Cut-off Switch", AC97_GENERAL_PURPOSE, 4, 1, 1),
728 AC97_SINGLE("Playback Attenuate (-6dB) Switch", AC97_GENERAL_PURPOSE, 6, 1, 0),
729 AC97_SINGLE("Bass Volume", AC97_GENERAL_PURPOSE, 8, 15, 1),
730 AC97_SINGLE("Tone Volume", AC97_GENERAL_PURPOSE, 0, 15, 1),
731 };
732 
733 static const struct snd_kcontrol_new wm13_snd_ac97_controls_3d[] = {
734 AC97_ENUM("Inv Input Mux", wm9713_enum[11]),
735 AC97_SINGLE("3D Upper Cut-off Switch", AC97_REC_GAIN_MIC, 5, 1, 0),
736 AC97_SINGLE("3D Lower Cut-off Switch", AC97_REC_GAIN_MIC, 4, 1, 0),
737 AC97_SINGLE("3D Depth", AC97_REC_GAIN_MIC, 0, 15, 1),
738 };
739 
740 static int patch_wolfson_wm9713_3d (struct snd_ac97 * ac97)
741 {
742 	int err, i;
743 
744 	for (i = 0; i < ARRAY_SIZE(wm13_snd_ac97_controls_3d); i++) {
745 		if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&wm13_snd_ac97_controls_3d[i], ac97))) < 0)
746 			return err;
747 	}
748 	return 0;
749 }
750 
751 static int patch_wolfson_wm9713_specific(struct snd_ac97 * ac97)
752 {
753 	int err, i;
754 
755 	for (i = 0; i < ARRAY_SIZE(wm13_snd_ac97_controls); i++) {
756 		if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&wm13_snd_ac97_controls[i], ac97))) < 0)
757 			return err;
758 	}
759 	snd_ac97_write_cache(ac97, AC97_PC_BEEP, 0x0808);
760 	snd_ac97_write_cache(ac97, AC97_PHONE, 0x0808);
761 	snd_ac97_write_cache(ac97, AC97_MIC, 0x0808);
762 	snd_ac97_write_cache(ac97, AC97_LINE, 0x00da);
763 	snd_ac97_write_cache(ac97, AC97_CD, 0x0808);
764 	snd_ac97_write_cache(ac97, AC97_VIDEO, 0xd612);
765 	snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x1ba0);
766 	return 0;
767 }
768 
769 #ifdef CONFIG_PM
770 static void patch_wolfson_wm9713_suspend (struct snd_ac97 * ac97)
771 {
772 	snd_ac97_write_cache(ac97, AC97_EXTENDED_MID, 0xfeff);
773 	snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0xffff);
774 }
775 
776 static void patch_wolfson_wm9713_resume (struct snd_ac97 * ac97)
777 {
778 	snd_ac97_write_cache(ac97, AC97_EXTENDED_MID, 0xda00);
779 	snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0x3810);
780 	snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0x0);
781 }
782 #endif
783 
784 static struct snd_ac97_build_ops patch_wolfson_wm9713_ops = {
785 	.build_specific = patch_wolfson_wm9713_specific,
786 	.build_3d = patch_wolfson_wm9713_3d,
787 #ifdef CONFIG_PM
788 	.suspend = patch_wolfson_wm9713_suspend,
789 	.resume = patch_wolfson_wm9713_resume
790 #endif
791 };
792 
793 int patch_wolfson13(struct snd_ac97 * ac97)
794 {
795 	/* WM9713, WM9714 */
796 	ac97->build_ops = &patch_wolfson_wm9713_ops;
797 
798 	ac97->flags |= AC97_HAS_NO_REC_GAIN | AC97_STEREO_MUTES | AC97_HAS_NO_PHONE |
799 		AC97_HAS_NO_PC_BEEP | AC97_HAS_NO_VIDEO | AC97_HAS_NO_CD | AC97_HAS_NO_TONE |
800 		AC97_HAS_NO_STD_PCM;
801     	ac97->scaps &= ~AC97_SCAP_MODEM;
802 
803 	snd_ac97_write_cache(ac97, AC97_EXTENDED_MID, 0xda00);
804 	snd_ac97_write_cache(ac97, AC97_EXTENDED_MSTATUS, 0x3810);
805 	snd_ac97_write_cache(ac97, AC97_POWERDOWN, 0x0);
806 
807 	return 0;
808 }
809 
810 /*
811  * Tritech codec
812  */
813 int patch_tritech_tr28028(struct snd_ac97 * ac97)
814 {
815 	snd_ac97_write_cache(ac97, 0x26, 0x0300);
816 	snd_ac97_write_cache(ac97, 0x26, 0x0000);
817 	snd_ac97_write_cache(ac97, AC97_SURROUND_MASTER, 0x0000);
818 	snd_ac97_write_cache(ac97, AC97_SPDIF, 0x0000);
819 	return 0;
820 }
821 
822 /*
823  * Sigmatel STAC97xx codecs
824  */
825 static int patch_sigmatel_stac9700_3d(struct snd_ac97 * ac97)
826 {
827 	struct snd_kcontrol *kctl;
828 	int err;
829 
830 	if ((err = snd_ctl_add(ac97->bus->card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
831 		return err;
832 	strcpy(kctl->id.name, "3D Control Sigmatel - Depth");
833 	kctl->private_value = AC97_SINGLE_VALUE(AC97_3D_CONTROL, 2, 3, 0);
834 	snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000);
835 	return 0;
836 }
837 
838 static int patch_sigmatel_stac9708_3d(struct snd_ac97 * ac97)
839 {
840 	struct snd_kcontrol *kctl;
841 	int err;
842 
843 	if ((err = snd_ctl_add(ac97->bus->card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
844 		return err;
845 	strcpy(kctl->id.name, "3D Control Sigmatel - Depth");
846 	kctl->private_value = AC97_SINGLE_VALUE(AC97_3D_CONTROL, 0, 3, 0);
847 	if ((err = snd_ctl_add(ac97->bus->card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
848 		return err;
849 	strcpy(kctl->id.name, "3D Control Sigmatel - Rear Depth");
850 	kctl->private_value = AC97_SINGLE_VALUE(AC97_3D_CONTROL, 2, 3, 0);
851 	snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000);
852 	return 0;
853 }
854 
855 static const struct snd_kcontrol_new snd_ac97_sigmatel_4speaker =
856 AC97_SINGLE("Sigmatel 4-Speaker Stereo Playback Switch", AC97_SIGMATEL_DAC2INVERT, 2, 1, 0);
857 
858 static const struct snd_kcontrol_new snd_ac97_sigmatel_phaseinvert =
859 AC97_SINGLE("Sigmatel Surround Phase Inversion Playback Switch", AC97_SIGMATEL_DAC2INVERT, 3, 1, 0);
860 
861 static const struct snd_kcontrol_new snd_ac97_sigmatel_controls[] = {
862 AC97_SINGLE("Sigmatel DAC 6dB Attenuate", AC97_SIGMATEL_ANALOG, 1, 1, 0),
863 AC97_SINGLE("Sigmatel ADC 6dB Attenuate", AC97_SIGMATEL_ANALOG, 0, 1, 0)
864 };
865 
866 static int patch_sigmatel_stac97xx_specific(struct snd_ac97 * ac97)
867 {
868 	int err;
869 
870 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_ANALOG, snd_ac97_read(ac97, AC97_SIGMATEL_ANALOG) & ~0x0003);
871 	if (snd_ac97_try_bit(ac97, AC97_SIGMATEL_ANALOG, 1))
872 		if ((err = patch_build_controls(ac97, &snd_ac97_sigmatel_controls[0], 1)) < 0)
873 			return err;
874 	if (snd_ac97_try_bit(ac97, AC97_SIGMATEL_ANALOG, 0))
875 		if ((err = patch_build_controls(ac97, &snd_ac97_sigmatel_controls[1], 1)) < 0)
876 			return err;
877 	if (snd_ac97_try_bit(ac97, AC97_SIGMATEL_DAC2INVERT, 2))
878 		if ((err = patch_build_controls(ac97, &snd_ac97_sigmatel_4speaker, 1)) < 0)
879 			return err;
880 	if (snd_ac97_try_bit(ac97, AC97_SIGMATEL_DAC2INVERT, 3))
881 		if ((err = patch_build_controls(ac97, &snd_ac97_sigmatel_phaseinvert, 1)) < 0)
882 			return err;
883 	return 0;
884 }
885 
886 static struct snd_ac97_build_ops patch_sigmatel_stac9700_ops = {
887 	.build_3d	= patch_sigmatel_stac9700_3d,
888 	.build_specific	= patch_sigmatel_stac97xx_specific
889 };
890 
891 int patch_sigmatel_stac9700(struct snd_ac97 * ac97)
892 {
893 	ac97->build_ops = &patch_sigmatel_stac9700_ops;
894 	return 0;
895 }
896 
897 static int snd_ac97_stac9708_put_bias(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
898 {
899 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
900 	int err;
901 
902 	mutex_lock(&ac97->page_mutex);
903 	snd_ac97_write(ac97, AC97_SIGMATEL_BIAS1, 0xabba);
904 	err = snd_ac97_update_bits(ac97, AC97_SIGMATEL_BIAS2, 0x0010,
905 				   (ucontrol->value.integer.value[0] & 1) << 4);
906 	snd_ac97_write(ac97, AC97_SIGMATEL_BIAS1, 0);
907 	mutex_unlock(&ac97->page_mutex);
908 	return err;
909 }
910 
911 static const struct snd_kcontrol_new snd_ac97_stac9708_bias_control = {
912 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
913 	.name = "Sigmatel Output Bias Switch",
914 	.info = snd_ac97_info_volsw,
915 	.get = snd_ac97_get_volsw,
916 	.put = snd_ac97_stac9708_put_bias,
917 	.private_value = AC97_SINGLE_VALUE(AC97_SIGMATEL_BIAS2, 4, 1, 0),
918 };
919 
920 static int patch_sigmatel_stac9708_specific(struct snd_ac97 *ac97)
921 {
922 	int err;
923 
924 	snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Sigmatel Surround Playback");
925 	if ((err = patch_build_controls(ac97, &snd_ac97_stac9708_bias_control, 1)) < 0)
926 		return err;
927 	return patch_sigmatel_stac97xx_specific(ac97);
928 }
929 
930 static struct snd_ac97_build_ops patch_sigmatel_stac9708_ops = {
931 	.build_3d	= patch_sigmatel_stac9708_3d,
932 	.build_specific	= patch_sigmatel_stac9708_specific
933 };
934 
935 int patch_sigmatel_stac9708(struct snd_ac97 * ac97)
936 {
937 	unsigned int codec72, codec6c;
938 
939 	ac97->build_ops = &patch_sigmatel_stac9708_ops;
940 	ac97->caps |= 0x10;	/* HP (sigmatel surround) support */
941 
942 	codec72 = snd_ac97_read(ac97, AC97_SIGMATEL_BIAS2) & 0x8000;
943 	codec6c = snd_ac97_read(ac97, AC97_SIGMATEL_ANALOG);
944 
945 	if ((codec72==0) && (codec6c==0)) {
946 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC1, 0xabba);
947 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC2, 0x1000);
948 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS1, 0xabba);
949 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS2, 0x0007);
950 	} else if ((codec72==0x8000) && (codec6c==0)) {
951 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC1, 0xabba);
952 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC2, 0x1001);
953 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_DAC2INVERT, 0x0008);
954 	} else if ((codec72==0x8000) && (codec6c==0x0080)) {
955 		/* nothing */
956 	}
957 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_MULTICHN, 0x0000);
958 	return 0;
959 }
960 
961 int patch_sigmatel_stac9721(struct snd_ac97 * ac97)
962 {
963 	ac97->build_ops = &patch_sigmatel_stac9700_ops;
964 	if (snd_ac97_read(ac97, AC97_SIGMATEL_ANALOG) == 0) {
965 		// patch for SigmaTel
966 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC1, 0xabba);
967 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC2, 0x4000);
968 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS1, 0xabba);
969 		snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS2, 0x0002);
970 	}
971 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_MULTICHN, 0x0000);
972 	return 0;
973 }
974 
975 int patch_sigmatel_stac9744(struct snd_ac97 * ac97)
976 {
977 	// patch for SigmaTel
978 	ac97->build_ops = &patch_sigmatel_stac9700_ops;
979 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC1, 0xabba);
980 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC2, 0x0000);	/* is this correct? --jk */
981 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS1, 0xabba);
982 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS2, 0x0002);
983 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_MULTICHN, 0x0000);
984 	return 0;
985 }
986 
987 int patch_sigmatel_stac9756(struct snd_ac97 * ac97)
988 {
989 	// patch for SigmaTel
990 	ac97->build_ops = &patch_sigmatel_stac9700_ops;
991 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC1, 0xabba);
992 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC2, 0x0000);	/* is this correct? --jk */
993 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS1, 0xabba);
994 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS2, 0x0002);
995 	snd_ac97_write_cache(ac97, AC97_SIGMATEL_MULTICHN, 0x0000);
996 	return 0;
997 }
998 
999 static int snd_ac97_stac9758_output_jack_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1000 {
1001 	static char *texts[5] = { "Input/Disabled", "Front Output",
1002 		"Rear Output", "Center/LFE Output", "Mixer Output" };
1003 
1004 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1005 	uinfo->count = 1;
1006 	uinfo->value.enumerated.items = 5;
1007 	if (uinfo->value.enumerated.item > 4)
1008 		uinfo->value.enumerated.item = 4;
1009 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1010 	return 0;
1011 }
1012 
1013 static int snd_ac97_stac9758_output_jack_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1014 {
1015 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1016 	int shift = kcontrol->private_value;
1017 	unsigned short val;
1018 
1019 	val = ac97->regs[AC97_SIGMATEL_OUTSEL] >> shift;
1020 	if (!(val & 4))
1021 		ucontrol->value.enumerated.item[0] = 0;
1022 	else
1023 		ucontrol->value.enumerated.item[0] = 1 + (val & 3);
1024 	return 0;
1025 }
1026 
1027 static int snd_ac97_stac9758_output_jack_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1028 {
1029 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1030 	int shift = kcontrol->private_value;
1031 	unsigned short val;
1032 
1033 	if (ucontrol->value.enumerated.item[0] > 4)
1034 		return -EINVAL;
1035 	if (ucontrol->value.enumerated.item[0] == 0)
1036 		val = 0;
1037 	else
1038 		val = 4 | (ucontrol->value.enumerated.item[0] - 1);
1039 	return ac97_update_bits_page(ac97, AC97_SIGMATEL_OUTSEL,
1040 				     7 << shift, val << shift, 0);
1041 }
1042 
1043 static int snd_ac97_stac9758_input_jack_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1044 {
1045 	static char *texts[7] = { "Mic2 Jack", "Mic1 Jack", "Line In Jack",
1046 		"Front Jack", "Rear Jack", "Center/LFE Jack", "Mute" };
1047 
1048 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1049 	uinfo->count = 1;
1050 	uinfo->value.enumerated.items = 7;
1051 	if (uinfo->value.enumerated.item > 6)
1052 		uinfo->value.enumerated.item = 6;
1053 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1054 	return 0;
1055 }
1056 
1057 static int snd_ac97_stac9758_input_jack_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1058 {
1059 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1060 	int shift = kcontrol->private_value;
1061 	unsigned short val;
1062 
1063 	val = ac97->regs[AC97_SIGMATEL_INSEL];
1064 	ucontrol->value.enumerated.item[0] = (val >> shift) & 7;
1065 	return 0;
1066 }
1067 
1068 static int snd_ac97_stac9758_input_jack_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1069 {
1070 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1071 	int shift = kcontrol->private_value;
1072 
1073 	return ac97_update_bits_page(ac97, AC97_SIGMATEL_INSEL, 7 << shift,
1074 				     ucontrol->value.enumerated.item[0] << shift, 0);
1075 }
1076 
1077 static int snd_ac97_stac9758_phonesel_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1078 {
1079 	static char *texts[3] = { "None", "Front Jack", "Rear Jack" };
1080 
1081 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1082 	uinfo->count = 1;
1083 	uinfo->value.enumerated.items = 3;
1084 	if (uinfo->value.enumerated.item > 2)
1085 		uinfo->value.enumerated.item = 2;
1086 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1087 	return 0;
1088 }
1089 
1090 static int snd_ac97_stac9758_phonesel_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1091 {
1092 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1093 
1094 	ucontrol->value.enumerated.item[0] = ac97->regs[AC97_SIGMATEL_IOMISC] & 3;
1095 	return 0;
1096 }
1097 
1098 static int snd_ac97_stac9758_phonesel_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1099 {
1100 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1101 
1102 	return ac97_update_bits_page(ac97, AC97_SIGMATEL_IOMISC, 3,
1103 				     ucontrol->value.enumerated.item[0], 0);
1104 }
1105 
1106 #define STAC9758_OUTPUT_JACK(xname, shift) \
1107 {	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1108 	.info = snd_ac97_stac9758_output_jack_info, \
1109 	.get = snd_ac97_stac9758_output_jack_get, \
1110 	.put = snd_ac97_stac9758_output_jack_put, \
1111 	.private_value = shift }
1112 #define STAC9758_INPUT_JACK(xname, shift) \
1113 {	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
1114 	.info = snd_ac97_stac9758_input_jack_info, \
1115 	.get = snd_ac97_stac9758_input_jack_get, \
1116 	.put = snd_ac97_stac9758_input_jack_put, \
1117 	.private_value = shift }
1118 static const struct snd_kcontrol_new snd_ac97_sigmatel_stac9758_controls[] = {
1119 	STAC9758_OUTPUT_JACK("Mic1 Jack", 1),
1120 	STAC9758_OUTPUT_JACK("LineIn Jack", 4),
1121 	STAC9758_OUTPUT_JACK("Front Jack", 7),
1122 	STAC9758_OUTPUT_JACK("Rear Jack", 10),
1123 	STAC9758_OUTPUT_JACK("Center/LFE Jack", 13),
1124 	STAC9758_INPUT_JACK("Mic Input Source", 0),
1125 	STAC9758_INPUT_JACK("Line Input Source", 8),
1126 	{
1127 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1128 		.name = "Headphone Amp",
1129 		.info = snd_ac97_stac9758_phonesel_info,
1130 		.get = snd_ac97_stac9758_phonesel_get,
1131 		.put = snd_ac97_stac9758_phonesel_put
1132 	},
1133 	AC97_SINGLE("Exchange Center/LFE", AC97_SIGMATEL_IOMISC, 4, 1, 0),
1134 	AC97_SINGLE("Headphone +3dB Boost", AC97_SIGMATEL_IOMISC, 8, 1, 0)
1135 };
1136 
1137 static int patch_sigmatel_stac9758_specific(struct snd_ac97 *ac97)
1138 {
1139 	int err;
1140 
1141 	err = patch_sigmatel_stac97xx_specific(ac97);
1142 	if (err < 0)
1143 		return err;
1144 	err = patch_build_controls(ac97, snd_ac97_sigmatel_stac9758_controls,
1145 				   ARRAY_SIZE(snd_ac97_sigmatel_stac9758_controls));
1146 	if (err < 0)
1147 		return err;
1148 	/* DAC-A direct */
1149 	snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Front Playback");
1150 	/* DAC-A to Mix = PCM */
1151 	/* DAC-B direct = Surround */
1152 	/* DAC-B to Mix */
1153 	snd_ac97_rename_vol_ctl(ac97, "Video Playback", "Surround Mix Playback");
1154 	/* DAC-C direct = Center/LFE */
1155 
1156 	return 0;
1157 }
1158 
1159 static struct snd_ac97_build_ops patch_sigmatel_stac9758_ops = {
1160 	.build_3d	= patch_sigmatel_stac9700_3d,
1161 	.build_specific	= patch_sigmatel_stac9758_specific
1162 };
1163 
1164 int patch_sigmatel_stac9758(struct snd_ac97 * ac97)
1165 {
1166 	static unsigned short regs[4] = {
1167 		AC97_SIGMATEL_OUTSEL,
1168 		AC97_SIGMATEL_IOMISC,
1169 		AC97_SIGMATEL_INSEL,
1170 		AC97_SIGMATEL_VARIOUS
1171 	};
1172 	static unsigned short def_regs[4] = {
1173 		/* OUTSEL */ 0xd794, /* CL:CL, SR:SR, LO:MX, LI:DS, MI:DS */
1174 		/* IOMISC */ 0x2001,
1175 		/* INSEL */ 0x0201, /* LI:LI, MI:M1 */
1176 		/* VARIOUS */ 0x0040
1177 	};
1178 	static unsigned short m675_regs[4] = {
1179 		/* OUTSEL */ 0xfc70, /* CL:MX, SR:MX, LO:DS, LI:MX, MI:DS */
1180 		/* IOMISC */ 0x2102, /* HP amp on */
1181 		/* INSEL */ 0x0203, /* LI:LI, MI:FR */
1182 		/* VARIOUS */ 0x0041 /* stereo mic */
1183 	};
1184 	unsigned short *pregs = def_regs;
1185 	int i;
1186 
1187 	/* Gateway M675 notebook */
1188 	if (ac97->pci &&
1189 	    ac97->subsystem_vendor == 0x107b &&
1190 	    ac97->subsystem_device == 0x0601)
1191 	    	pregs = m675_regs;
1192 
1193 	// patch for SigmaTel
1194 	ac97->build_ops = &patch_sigmatel_stac9758_ops;
1195 	/* FIXME: assume only page 0 for writing cache */
1196 	snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, AC97_PAGE_VENDOR);
1197 	for (i = 0; i < 4; i++)
1198 		snd_ac97_write_cache(ac97, regs[i], pregs[i]);
1199 
1200 	ac97->flags |= AC97_STEREO_MUTES;
1201 	return 0;
1202 }
1203 
1204 /*
1205  * Cirrus Logic CS42xx codecs
1206  */
1207 static const struct snd_kcontrol_new snd_ac97_cirrus_controls_spdif[2] = {
1208 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), AC97_CSR_SPDIF, 15, 1, 0),
1209 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "AC97-SPSA", AC97_CSR_ACMODE, 0, 3, 0)
1210 };
1211 
1212 static int patch_cirrus_build_spdif(struct snd_ac97 * ac97)
1213 {
1214 	int err;
1215 
1216 	/* con mask, pro mask, default */
1217 	if ((err = patch_build_controls(ac97, &snd_ac97_controls_spdif[0], 3)) < 0)
1218 		return err;
1219 	/* switch, spsa */
1220 	if ((err = patch_build_controls(ac97, &snd_ac97_cirrus_controls_spdif[0], 1)) < 0)
1221 		return err;
1222 	switch (ac97->id & AC97_ID_CS_MASK) {
1223 	case AC97_ID_CS4205:
1224 		if ((err = patch_build_controls(ac97, &snd_ac97_cirrus_controls_spdif[1], 1)) < 0)
1225 			return err;
1226 		break;
1227 	}
1228 	/* set default PCM S/PDIF params */
1229 	/* consumer,PCM audio,no copyright,no preemphasis,PCM coder,original,48000Hz */
1230 	snd_ac97_write_cache(ac97, AC97_CSR_SPDIF, 0x0a20);
1231 	return 0;
1232 }
1233 
1234 static struct snd_ac97_build_ops patch_cirrus_ops = {
1235 	.build_spdif = patch_cirrus_build_spdif
1236 };
1237 
1238 int patch_cirrus_spdif(struct snd_ac97 * ac97)
1239 {
1240 	/* Basically, the cs4201/cs4205/cs4297a has non-standard sp/dif registers.
1241 	   WHY CAN'T ANYONE FOLLOW THE BLOODY SPEC?  *sigh*
1242 	   - sp/dif EA ID is not set, but sp/dif is always present.
1243 	   - enable/disable is spdif register bit 15.
1244 	   - sp/dif control register is 0x68.  differs from AC97:
1245 	   - valid is bit 14 (vs 15)
1246 	   - no DRS
1247 	   - only 44.1/48k [00 = 48, 01=44,1] (AC97 is 00=44.1, 10=48)
1248 	   - sp/dif ssource select is in 0x5e bits 0,1.
1249 	*/
1250 
1251 	ac97->build_ops = &patch_cirrus_ops;
1252 	ac97->flags |= AC97_CS_SPDIF;
1253 	ac97->rates[AC97_RATES_SPDIF] &= ~SNDRV_PCM_RATE_32000;
1254         ac97->ext_id |= AC97_EI_SPDIF;	/* force the detection of spdif */
1255 	snd_ac97_write_cache(ac97, AC97_CSR_ACMODE, 0x0080);
1256 	return 0;
1257 }
1258 
1259 int patch_cirrus_cs4299(struct snd_ac97 * ac97)
1260 {
1261 	/* force the detection of PC Beep */
1262 	ac97->flags |= AC97_HAS_PC_BEEP;
1263 
1264 	return patch_cirrus_spdif(ac97);
1265 }
1266 
1267 /*
1268  * Conexant codecs
1269  */
1270 static const struct snd_kcontrol_new snd_ac97_conexant_controls_spdif[1] = {
1271 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), AC97_CXR_AUDIO_MISC, 3, 1, 0),
1272 };
1273 
1274 static int patch_conexant_build_spdif(struct snd_ac97 * ac97)
1275 {
1276 	int err;
1277 
1278 	/* con mask, pro mask, default */
1279 	if ((err = patch_build_controls(ac97, &snd_ac97_controls_spdif[0], 3)) < 0)
1280 		return err;
1281 	/* switch */
1282 	if ((err = patch_build_controls(ac97, &snd_ac97_conexant_controls_spdif[0], 1)) < 0)
1283 		return err;
1284 	/* set default PCM S/PDIF params */
1285 	/* consumer,PCM audio,no copyright,no preemphasis,PCM coder,original,48000Hz */
1286 	snd_ac97_write_cache(ac97, AC97_CXR_AUDIO_MISC,
1287 			     snd_ac97_read(ac97, AC97_CXR_AUDIO_MISC) & ~(AC97_CXR_SPDIFEN|AC97_CXR_COPYRGT|AC97_CXR_SPDIF_MASK));
1288 	return 0;
1289 }
1290 
1291 static struct snd_ac97_build_ops patch_conexant_ops = {
1292 	.build_spdif = patch_conexant_build_spdif
1293 };
1294 
1295 int patch_conexant(struct snd_ac97 * ac97)
1296 {
1297 	ac97->build_ops = &patch_conexant_ops;
1298 	ac97->flags |= AC97_CX_SPDIF;
1299         ac97->ext_id |= AC97_EI_SPDIF;	/* force the detection of spdif */
1300 	ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000; /* 48k only */
1301 	return 0;
1302 }
1303 
1304 /*
1305  * Analog Device AD18xx, AD19xx codecs
1306  */
1307 #ifdef CONFIG_PM
1308 static void ad18xx_resume(struct snd_ac97 *ac97)
1309 {
1310 	static unsigned short setup_regs[] = {
1311 		AC97_AD_MISC, AC97_AD_SERIAL_CFG, AC97_AD_JACK_SPDIF,
1312 	};
1313 	int i, codec;
1314 
1315 	for (i = 0; i < (int)ARRAY_SIZE(setup_regs); i++) {
1316 		unsigned short reg = setup_regs[i];
1317 		if (test_bit(reg, ac97->reg_accessed)) {
1318 			snd_ac97_write(ac97, reg, ac97->regs[reg]);
1319 			snd_ac97_read(ac97, reg);
1320 		}
1321 	}
1322 
1323 	if (! (ac97->flags & AC97_AD_MULTI))
1324 		/* normal restore */
1325 		snd_ac97_restore_status(ac97);
1326 	else {
1327 		/* restore the AD18xx codec configurations */
1328 		for (codec = 0; codec < 3; codec++) {
1329 			if (! ac97->spec.ad18xx.id[codec])
1330 				continue;
1331 			/* select single codec */
1332 			snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000,
1333 					     ac97->spec.ad18xx.unchained[codec] | ac97->spec.ad18xx.chained[codec]);
1334 			ac97->bus->ops->write(ac97, AC97_AD_CODEC_CFG, ac97->spec.ad18xx.codec_cfg[codec]);
1335 		}
1336 		/* select all codecs */
1337 		snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, 0x7000);
1338 
1339 		/* restore status */
1340 		for (i = 2; i < 0x7c ; i += 2) {
1341 			if (i == AC97_POWERDOWN || i == AC97_EXTENDED_ID)
1342 				continue;
1343 			if (test_bit(i, ac97->reg_accessed)) {
1344 				/* handle multi codecs for AD18xx */
1345 				if (i == AC97_PCM) {
1346 					for (codec = 0; codec < 3; codec++) {
1347 						if (! ac97->spec.ad18xx.id[codec])
1348 							continue;
1349 						/* select single codec */
1350 						snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000,
1351 								     ac97->spec.ad18xx.unchained[codec] | ac97->spec.ad18xx.chained[codec]);
1352 						/* update PCM bits */
1353 						ac97->bus->ops->write(ac97, AC97_PCM, ac97->spec.ad18xx.pcmreg[codec]);
1354 					}
1355 					/* select all codecs */
1356 					snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, 0x7000);
1357 					continue;
1358 				} else if (i == AC97_AD_TEST ||
1359 					   i == AC97_AD_CODEC_CFG ||
1360 					   i == AC97_AD_SERIAL_CFG)
1361 					continue; /* ignore */
1362 			}
1363 			snd_ac97_write(ac97, i, ac97->regs[i]);
1364 			snd_ac97_read(ac97, i);
1365 		}
1366 	}
1367 
1368 	snd_ac97_restore_iec958(ac97);
1369 }
1370 #endif
1371 
1372 int patch_ad1819(struct snd_ac97 * ac97)
1373 {
1374 	unsigned short scfg;
1375 
1376 	// patch for Analog Devices
1377 	scfg = snd_ac97_read(ac97, AC97_AD_SERIAL_CFG);
1378 	snd_ac97_write_cache(ac97, AC97_AD_SERIAL_CFG, scfg | 0x7000); /* select all codecs */
1379 	return 0;
1380 }
1381 
1382 static unsigned short patch_ad1881_unchained(struct snd_ac97 * ac97, int idx, unsigned short mask)
1383 {
1384 	unsigned short val;
1385 
1386 	// test for unchained codec
1387 	snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, mask);
1388 	snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, 0x0000);	/* ID0C, ID1C, SDIE = off */
1389 	val = snd_ac97_read(ac97, AC97_VENDOR_ID2);
1390 	if ((val & 0xff40) != 0x5340)
1391 		return 0;
1392 	ac97->spec.ad18xx.unchained[idx] = mask;
1393 	ac97->spec.ad18xx.id[idx] = val;
1394 	ac97->spec.ad18xx.codec_cfg[idx] = 0x0000;
1395 	return mask;
1396 }
1397 
1398 static int patch_ad1881_chained1(struct snd_ac97 * ac97, int idx, unsigned short codec_bits)
1399 {
1400 	static int cfg_bits[3] = { 1<<12, 1<<14, 1<<13 };
1401 	unsigned short val;
1402 
1403 	snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, cfg_bits[idx]);
1404 	snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, 0x0004);	// SDIE
1405 	val = snd_ac97_read(ac97, AC97_VENDOR_ID2);
1406 	if ((val & 0xff40) != 0x5340)
1407 		return 0;
1408 	if (codec_bits)
1409 		snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, codec_bits);
1410 	ac97->spec.ad18xx.chained[idx] = cfg_bits[idx];
1411 	ac97->spec.ad18xx.id[idx] = val;
1412 	ac97->spec.ad18xx.codec_cfg[idx] = codec_bits ? codec_bits : 0x0004;
1413 	return 1;
1414 }
1415 
1416 static void patch_ad1881_chained(struct snd_ac97 * ac97, int unchained_idx, int cidx1, int cidx2)
1417 {
1418 	// already detected?
1419 	if (ac97->spec.ad18xx.unchained[cidx1] || ac97->spec.ad18xx.chained[cidx1])
1420 		cidx1 = -1;
1421 	if (ac97->spec.ad18xx.unchained[cidx2] || ac97->spec.ad18xx.chained[cidx2])
1422 		cidx2 = -1;
1423 	if (cidx1 < 0 && cidx2 < 0)
1424 		return;
1425 	// test for chained codecs
1426 	snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000,
1427 			     ac97->spec.ad18xx.unchained[unchained_idx]);
1428 	snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, 0x0002);		// ID1C
1429 	ac97->spec.ad18xx.codec_cfg[unchained_idx] = 0x0002;
1430 	if (cidx1 >= 0) {
1431 		if (patch_ad1881_chained1(ac97, cidx1, 0x0006))		// SDIE | ID1C
1432 			patch_ad1881_chained1(ac97, cidx2, 0);
1433 		else if (patch_ad1881_chained1(ac97, cidx2, 0x0006))	// SDIE | ID1C
1434 			patch_ad1881_chained1(ac97, cidx1, 0);
1435 	} else if (cidx2 >= 0) {
1436 		patch_ad1881_chained1(ac97, cidx2, 0);
1437 	}
1438 }
1439 
1440 static struct snd_ac97_build_ops patch_ad1881_build_ops = {
1441 #ifdef CONFIG_PM
1442 	.resume = ad18xx_resume
1443 #endif
1444 };
1445 
1446 int patch_ad1881(struct snd_ac97 * ac97)
1447 {
1448 	static const char cfg_idxs[3][2] = {
1449 		{2, 1},
1450 		{0, 2},
1451 		{0, 1}
1452 	};
1453 
1454 	// patch for Analog Devices
1455 	unsigned short codecs[3];
1456 	unsigned short val;
1457 	int idx, num;
1458 
1459 	val = snd_ac97_read(ac97, AC97_AD_SERIAL_CFG);
1460 	snd_ac97_write_cache(ac97, AC97_AD_SERIAL_CFG, val);
1461 	codecs[0] = patch_ad1881_unchained(ac97, 0, (1<<12));
1462 	codecs[1] = patch_ad1881_unchained(ac97, 1, (1<<14));
1463 	codecs[2] = patch_ad1881_unchained(ac97, 2, (1<<13));
1464 
1465 	if (! (codecs[0] || codecs[1] || codecs[2]))
1466 		goto __end;
1467 
1468 	for (idx = 0; idx < 3; idx++)
1469 		if (ac97->spec.ad18xx.unchained[idx])
1470 			patch_ad1881_chained(ac97, idx, cfg_idxs[idx][0], cfg_idxs[idx][1]);
1471 
1472 	if (ac97->spec.ad18xx.id[1]) {
1473 		ac97->flags |= AC97_AD_MULTI;
1474 		ac97->scaps |= AC97_SCAP_SURROUND_DAC;
1475 	}
1476 	if (ac97->spec.ad18xx.id[2]) {
1477 		ac97->flags |= AC97_AD_MULTI;
1478 		ac97->scaps |= AC97_SCAP_CENTER_LFE_DAC;
1479 	}
1480 
1481       __end:
1482 	/* select all codecs */
1483 	snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, 0x7000);
1484 	/* check if only one codec is present */
1485 	for (idx = num = 0; idx < 3; idx++)
1486 		if (ac97->spec.ad18xx.id[idx])
1487 			num++;
1488 	if (num == 1) {
1489 		/* ok, deselect all ID bits */
1490 		snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, 0x0000);
1491 		ac97->spec.ad18xx.codec_cfg[0] =
1492 			ac97->spec.ad18xx.codec_cfg[1] =
1493 			ac97->spec.ad18xx.codec_cfg[2] = 0x0000;
1494 	}
1495 	/* required for AD1886/AD1885 combination */
1496 	ac97->ext_id = snd_ac97_read(ac97, AC97_EXTENDED_ID);
1497 	if (ac97->spec.ad18xx.id[0]) {
1498 		ac97->id &= 0xffff0000;
1499 		ac97->id |= ac97->spec.ad18xx.id[0];
1500 	}
1501 	ac97->build_ops = &patch_ad1881_build_ops;
1502 	return 0;
1503 }
1504 
1505 static const struct snd_kcontrol_new snd_ac97_controls_ad1885[] = {
1506 	AC97_SINGLE("Digital Mono Direct", AC97_AD_MISC, 11, 1, 0),
1507 	/* AC97_SINGLE("Digital Audio Mode", AC97_AD_MISC, 12, 1, 0), */ /* seems problematic */
1508 	AC97_SINGLE("Low Power Mixer", AC97_AD_MISC, 14, 1, 0),
1509 	AC97_SINGLE("Zero Fill DAC", AC97_AD_MISC, 15, 1, 0),
1510 	AC97_SINGLE("Headphone Jack Sense", AC97_AD_JACK_SPDIF, 9, 1, 1), /* inverted */
1511 	AC97_SINGLE("Line Jack Sense", AC97_AD_JACK_SPDIF, 8, 1, 1), /* inverted */
1512 };
1513 
1514 static int patch_ad1885_specific(struct snd_ac97 * ac97)
1515 {
1516 	int err;
1517 
1518 	if ((err = patch_build_controls(ac97, snd_ac97_controls_ad1885, ARRAY_SIZE(snd_ac97_controls_ad1885))) < 0)
1519 		return err;
1520 	return 0;
1521 }
1522 
1523 static struct snd_ac97_build_ops patch_ad1885_build_ops = {
1524 	.build_specific = &patch_ad1885_specific,
1525 #ifdef CONFIG_PM
1526 	.resume = ad18xx_resume
1527 #endif
1528 };
1529 
1530 int patch_ad1885(struct snd_ac97 * ac97)
1531 {
1532 	patch_ad1881(ac97);
1533 	/* This is required to deal with the Intel D815EEAL2 */
1534 	/* i.e. Line out is actually headphone out from codec */
1535 
1536 	/* set default */
1537 	snd_ac97_write_cache(ac97, AC97_AD_MISC, 0x0404);
1538 
1539 	ac97->build_ops = &patch_ad1885_build_ops;
1540 	return 0;
1541 }
1542 
1543 int patch_ad1886(struct snd_ac97 * ac97)
1544 {
1545 	patch_ad1881(ac97);
1546 	/* Presario700 workaround */
1547 	/* for Jack Sense/SPDIF Register misetting causing */
1548 	snd_ac97_write_cache(ac97, AC97_AD_JACK_SPDIF, 0x0010);
1549 	return 0;
1550 }
1551 
1552 /* MISC bits */
1553 #define AC97_AD198X_MBC		0x0003	/* mic boost */
1554 #define AC97_AD198X_MBC_20	0x0000	/* +20dB */
1555 #define AC97_AD198X_MBC_10	0x0001	/* +10dB */
1556 #define AC97_AD198X_MBC_30	0x0002	/* +30dB */
1557 #define AC97_AD198X_VREFD	0x0004	/* VREF high-Z */
1558 #define AC97_AD198X_VREFH	0x0008	/* 2.25V, 3.7V */
1559 #define AC97_AD198X_VREF_0	0x000c	/* 0V */
1560 #define AC97_AD198X_SRU		0x0010	/* sample rate unlock */
1561 #define AC97_AD198X_LOSEL	0x0020	/* LINE_OUT amplifiers input select */
1562 #define AC97_AD198X_2MIC	0x0040	/* 2-channel mic select */
1563 #define AC97_AD198X_SPRD	0x0080	/* SPREAD enable */
1564 #define AC97_AD198X_DMIX0	0x0100	/* downmix mode: 0 = 6-to-4, 1 = 6-to-2 downmix */
1565 #define AC97_AD198X_DMIX1	0x0200	/* downmix mode: 1 = enabled */
1566 #define AC97_AD198X_HPSEL	0x0400	/* headphone amplifier input select */
1567 #define AC97_AD198X_CLDIS	0x0800	/* center/lfe disable */
1568 #define AC97_AD198X_LODIS	0x1000	/* LINE_OUT disable */
1569 #define AC97_AD198X_MSPLT	0x2000	/* mute split */
1570 #define AC97_AD198X_AC97NC	0x4000	/* AC97 no compatible mode */
1571 #define AC97_AD198X_DACZ	0x8000	/* DAC zero-fill mode */
1572 
1573 
1574 static int snd_ac97_ad198x_spdif_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1575 {
1576 	static char *texts[2] = { "AC-Link", "A/D Converter" };
1577 
1578 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1579 	uinfo->count = 1;
1580 	uinfo->value.enumerated.items = 2;
1581 	if (uinfo->value.enumerated.item > 1)
1582 		uinfo->value.enumerated.item = 1;
1583 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1584 	return 0;
1585 }
1586 
1587 static int snd_ac97_ad198x_spdif_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1588 {
1589 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1590 	unsigned short val;
1591 
1592 	val = ac97->regs[AC97_AD_SERIAL_CFG];
1593 	ucontrol->value.enumerated.item[0] = (val >> 2) & 1;
1594 	return 0;
1595 }
1596 
1597 static int snd_ac97_ad198x_spdif_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1598 {
1599 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1600 	unsigned short val;
1601 
1602 	if (ucontrol->value.enumerated.item[0] > 1)
1603 		return -EINVAL;
1604 	val = ucontrol->value.enumerated.item[0] << 2;
1605 	return snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x0004, val);
1606 }
1607 
1608 static const struct snd_kcontrol_new snd_ac97_ad198x_spdif_source = {
1609 	.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
1610 	.name	= SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1611 	.info	= snd_ac97_ad198x_spdif_source_info,
1612 	.get	= snd_ac97_ad198x_spdif_source_get,
1613 	.put	= snd_ac97_ad198x_spdif_source_put,
1614 };
1615 
1616 static int patch_ad198x_post_spdif(struct snd_ac97 * ac97)
1617 {
1618  	return patch_build_controls(ac97, &snd_ac97_ad198x_spdif_source, 1);
1619 }
1620 
1621 static const struct snd_kcontrol_new snd_ac97_ad1981x_jack_sense[] = {
1622 	AC97_SINGLE("Headphone Jack Sense", AC97_AD_JACK_SPDIF, 11, 1, 0),
1623 	AC97_SINGLE("Line Jack Sense", AC97_AD_JACK_SPDIF, 12, 1, 0),
1624 };
1625 
1626 /* black list to avoid HP/Line jack-sense controls
1627  * (SS vendor << 16 | device)
1628  */
1629 static unsigned int ad1981_jacks_blacklist[] = {
1630 	0x10140554, /* Thinkpad T42p/R50p */
1631 	0 /* end */
1632 };
1633 
1634 static int check_list(struct snd_ac97 *ac97, const unsigned int *list)
1635 {
1636 	u32 subid = ((u32)ac97->subsystem_vendor << 16) | ac97->subsystem_device;
1637 	for (; *list; list++)
1638 		if (*list == subid)
1639 			return 1;
1640 	return 0;
1641 }
1642 
1643 static int patch_ad1981a_specific(struct snd_ac97 * ac97)
1644 {
1645 	if (check_list(ac97, ad1981_jacks_blacklist))
1646 		return 0;
1647 	return patch_build_controls(ac97, snd_ac97_ad1981x_jack_sense,
1648 				    ARRAY_SIZE(snd_ac97_ad1981x_jack_sense));
1649 }
1650 
1651 static struct snd_ac97_build_ops patch_ad1981a_build_ops = {
1652 	.build_post_spdif = patch_ad198x_post_spdif,
1653 	.build_specific = patch_ad1981a_specific,
1654 #ifdef CONFIG_PM
1655 	.resume = ad18xx_resume
1656 #endif
1657 };
1658 
1659 /* white list to enable HP jack-sense bits
1660  * (SS vendor << 16 | device)
1661  */
1662 static unsigned int ad1981_jacks_whitelist[] = {
1663 	0x0e11005a, /* HP nc4000/4010 */
1664 	0x103c0890, /* HP nc6000 */
1665 	0x103c0938, /* HP nc4220 */
1666 	0x103c099c, /* HP nx6110 */
1667 	0x103c0944, /* HP nc6220 */
1668 	0x103c0934, /* HP nc8220 */
1669 	0x103c006d, /* HP nx9105 */
1670 	0x17340088, /* FSC Scenic-W */
1671 	0 /* end */
1672 };
1673 
1674 static void check_ad1981_hp_jack_sense(struct snd_ac97 *ac97)
1675 {
1676 	if (check_list(ac97, ad1981_jacks_whitelist))
1677 		/* enable headphone jack sense */
1678 		snd_ac97_update_bits(ac97, AC97_AD_JACK_SPDIF, 1<<11, 1<<11);
1679 }
1680 
1681 int patch_ad1981a(struct snd_ac97 *ac97)
1682 {
1683 	patch_ad1881(ac97);
1684 	ac97->build_ops = &patch_ad1981a_build_ops;
1685 	snd_ac97_update_bits(ac97, AC97_AD_MISC, AC97_AD198X_MSPLT, AC97_AD198X_MSPLT);
1686 	ac97->flags |= AC97_STEREO_MUTES;
1687 	check_ad1981_hp_jack_sense(ac97);
1688 	return 0;
1689 }
1690 
1691 static const struct snd_kcontrol_new snd_ac97_ad198x_2cmic =
1692 AC97_SINGLE("Stereo Mic", AC97_AD_MISC, 6, 1, 0);
1693 
1694 static int patch_ad1981b_specific(struct snd_ac97 *ac97)
1695 {
1696 	int err;
1697 
1698 	if ((err = patch_build_controls(ac97, &snd_ac97_ad198x_2cmic, 1)) < 0)
1699 		return err;
1700 	if (check_list(ac97, ad1981_jacks_blacklist))
1701 		return 0;
1702 	return patch_build_controls(ac97, snd_ac97_ad1981x_jack_sense,
1703 				    ARRAY_SIZE(snd_ac97_ad1981x_jack_sense));
1704 }
1705 
1706 static struct snd_ac97_build_ops patch_ad1981b_build_ops = {
1707 	.build_post_spdif = patch_ad198x_post_spdif,
1708 	.build_specific = patch_ad1981b_specific,
1709 #ifdef CONFIG_PM
1710 	.resume = ad18xx_resume
1711 #endif
1712 };
1713 
1714 int patch_ad1981b(struct snd_ac97 *ac97)
1715 {
1716 	patch_ad1881(ac97);
1717 	ac97->build_ops = &patch_ad1981b_build_ops;
1718 	snd_ac97_update_bits(ac97, AC97_AD_MISC, AC97_AD198X_MSPLT, AC97_AD198X_MSPLT);
1719 	ac97->flags |= AC97_STEREO_MUTES;
1720 	check_ad1981_hp_jack_sense(ac97);
1721 	return 0;
1722 }
1723 
1724 static int snd_ac97_ad1888_lohpsel_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1725 {
1726 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1727 	uinfo->count = 1;
1728 	uinfo->value.integer.min = 0;
1729 	uinfo->value.integer.max = 1;
1730 	return 0;
1731 }
1732 
1733 static int snd_ac97_ad1888_lohpsel_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1734 {
1735 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1736 	unsigned short val;
1737 
1738 	val = ac97->regs[AC97_AD_MISC];
1739 	ucontrol->value.integer.value[0] = !(val & AC97_AD198X_LOSEL);
1740 	return 0;
1741 }
1742 
1743 static int snd_ac97_ad1888_lohpsel_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1744 {
1745 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1746 	unsigned short val;
1747 
1748 	val = !ucontrol->value.integer.value[0]
1749 		? (AC97_AD198X_LOSEL | AC97_AD198X_HPSEL) : 0;
1750 	return snd_ac97_update_bits(ac97, AC97_AD_MISC,
1751 				    AC97_AD198X_LOSEL | AC97_AD198X_HPSEL, val);
1752 }
1753 
1754 static int snd_ac97_ad1888_downmix_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1755 {
1756 	static char *texts[3] = {"Off", "6 -> 4", "6 -> 2"};
1757 
1758 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1759 	uinfo->count = 1;
1760 	uinfo->value.enumerated.items = 3;
1761 	if (uinfo->value.enumerated.item > 2)
1762 		uinfo->value.enumerated.item = 2;
1763 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1764 	return 0;
1765 }
1766 
1767 static int snd_ac97_ad1888_downmix_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1768 {
1769 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1770 	unsigned short val;
1771 
1772 	val = ac97->regs[AC97_AD_MISC];
1773 	if (!(val & AC97_AD198X_DMIX1))
1774 		ucontrol->value.enumerated.item[0] = 0;
1775 	else
1776 		ucontrol->value.enumerated.item[0] = 1 + ((val >> 8) & 1);
1777 	return 0;
1778 }
1779 
1780 static int snd_ac97_ad1888_downmix_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1781 {
1782 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
1783 	unsigned short val;
1784 
1785 	if (ucontrol->value.enumerated.item[0] > 2)
1786 		return -EINVAL;
1787 	if (ucontrol->value.enumerated.item[0] == 0)
1788 		val = 0;
1789 	else
1790 		val = AC97_AD198X_DMIX1 |
1791 			((ucontrol->value.enumerated.item[0] - 1) << 8);
1792 	return snd_ac97_update_bits(ac97, AC97_AD_MISC,
1793 				    AC97_AD198X_DMIX0 | AC97_AD198X_DMIX1, val);
1794 }
1795 
1796 static void ad1888_update_jacks(struct snd_ac97 *ac97)
1797 {
1798 	unsigned short val = 0;
1799 	if (! is_shared_linein(ac97))
1800 		val |= (1 << 12);
1801 	if (! is_shared_micin(ac97))
1802 		val |= (1 << 11);
1803 	/* shared Line-In */
1804 	snd_ac97_update_bits(ac97, AC97_AD_MISC, (1 << 11) | (1 << 12), val);
1805 }
1806 
1807 static const struct snd_kcontrol_new snd_ac97_ad1888_controls[] = {
1808 	{
1809 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1810 		.name = "Exchange Front/Surround",
1811 		.info = snd_ac97_ad1888_lohpsel_info,
1812 		.get = snd_ac97_ad1888_lohpsel_get,
1813 		.put = snd_ac97_ad1888_lohpsel_put
1814 	},
1815 	AC97_SINGLE("Spread Front to Surround and Center/LFE", AC97_AD_MISC, 7, 1, 0),
1816 	{
1817 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1818 		.name = "Downmix",
1819 		.info = snd_ac97_ad1888_downmix_info,
1820 		.get = snd_ac97_ad1888_downmix_get,
1821 		.put = snd_ac97_ad1888_downmix_put
1822 	},
1823 	AC97_SURROUND_JACK_MODE_CTL,
1824 	AC97_CHANNEL_MODE_CTL,
1825 
1826 	AC97_SINGLE("Headphone Jack Sense", AC97_AD_JACK_SPDIF, 10, 1, 0),
1827 	AC97_SINGLE("Line Jack Sense", AC97_AD_JACK_SPDIF, 12, 1, 0),
1828 };
1829 
1830 static int patch_ad1888_specific(struct snd_ac97 *ac97)
1831 {
1832 	/* rename 0x04 as "Master" and 0x02 as "Master Surround" */
1833 	snd_ac97_rename_vol_ctl(ac97, "Master Playback", "Master Surround Playback");
1834 	snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback");
1835 	return patch_build_controls(ac97, snd_ac97_ad1888_controls, ARRAY_SIZE(snd_ac97_ad1888_controls));
1836 }
1837 
1838 static struct snd_ac97_build_ops patch_ad1888_build_ops = {
1839 	.build_post_spdif = patch_ad198x_post_spdif,
1840 	.build_specific = patch_ad1888_specific,
1841 #ifdef CONFIG_PM
1842 	.resume = ad18xx_resume,
1843 #endif
1844 	.update_jacks = ad1888_update_jacks,
1845 };
1846 
1847 int patch_ad1888(struct snd_ac97 * ac97)
1848 {
1849 	unsigned short misc;
1850 
1851 	patch_ad1881(ac97);
1852 	ac97->build_ops = &patch_ad1888_build_ops;
1853 	/* Switch FRONT/SURROUND LINE-OUT/HP-OUT default connection */
1854 	/* it seems that most vendors connect line-out connector to headphone out of AC'97 */
1855 	/* AD-compatible mode */
1856 	/* Stereo mutes enabled */
1857 	misc = snd_ac97_read(ac97, AC97_AD_MISC);
1858 	snd_ac97_write_cache(ac97, AC97_AD_MISC, misc |
1859 			     AC97_AD198X_LOSEL |
1860 			     AC97_AD198X_HPSEL |
1861 			     AC97_AD198X_MSPLT |
1862 			     AC97_AD198X_AC97NC);
1863 	ac97->flags |= AC97_STEREO_MUTES;
1864 	return 0;
1865 }
1866 
1867 static int patch_ad1980_specific(struct snd_ac97 *ac97)
1868 {
1869 	int err;
1870 
1871 	if ((err = patch_ad1888_specific(ac97)) < 0)
1872 		return err;
1873 	return patch_build_controls(ac97, &snd_ac97_ad198x_2cmic, 1);
1874 }
1875 
1876 static struct snd_ac97_build_ops patch_ad1980_build_ops = {
1877 	.build_post_spdif = patch_ad198x_post_spdif,
1878 	.build_specific = patch_ad1980_specific,
1879 #ifdef CONFIG_PM
1880 	.resume = ad18xx_resume,
1881 #endif
1882 	.update_jacks = ad1888_update_jacks,
1883 };
1884 
1885 int patch_ad1980(struct snd_ac97 * ac97)
1886 {
1887 	patch_ad1888(ac97);
1888 	ac97->build_ops = &patch_ad1980_build_ops;
1889 	return 0;
1890 }
1891 
1892 static const struct snd_kcontrol_new snd_ac97_ad1985_controls[] = {
1893 	AC97_SINGLE("Exchange Center/LFE", AC97_AD_SERIAL_CFG, 3, 1, 0)
1894 };
1895 
1896 static void ad1985_update_jacks(struct snd_ac97 *ac97)
1897 {
1898 	ad1888_update_jacks(ac97);
1899 	snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 1 << 9,
1900 			     is_shared_micin(ac97) ? 0 : 1 << 9);
1901 }
1902 
1903 static int patch_ad1985_specific(struct snd_ac97 *ac97)
1904 {
1905 	int err;
1906 
1907 	if ((err = patch_ad1980_specific(ac97)) < 0)
1908 		return err;
1909 	return patch_build_controls(ac97, snd_ac97_ad1985_controls, ARRAY_SIZE(snd_ac97_ad1985_controls));
1910 }
1911 
1912 static struct snd_ac97_build_ops patch_ad1985_build_ops = {
1913 	.build_post_spdif = patch_ad198x_post_spdif,
1914 	.build_specific = patch_ad1985_specific,
1915 #ifdef CONFIG_PM
1916 	.resume = ad18xx_resume,
1917 #endif
1918 	.update_jacks = ad1985_update_jacks,
1919 };
1920 
1921 int patch_ad1985(struct snd_ac97 * ac97)
1922 {
1923 	unsigned short misc;
1924 
1925 	patch_ad1881(ac97);
1926 	ac97->build_ops = &patch_ad1985_build_ops;
1927 	misc = snd_ac97_read(ac97, AC97_AD_MISC);
1928 	/* switch front/surround line-out/hp-out */
1929 	/* center/LFE, mic in 3.75V mode */
1930 	/* AD-compatible mode */
1931 	/* Stereo mutes enabled */
1932 	/* in accordance with ADI driver: misc | 0x5c28 */
1933 	snd_ac97_write_cache(ac97, AC97_AD_MISC, misc |
1934 			     AC97_AD198X_VREFH |
1935 			     AC97_AD198X_LOSEL |
1936 			     AC97_AD198X_HPSEL |
1937 			     AC97_AD198X_CLDIS |
1938 			     AC97_AD198X_LODIS |
1939 			     AC97_AD198X_MSPLT |
1940 			     AC97_AD198X_AC97NC);
1941 	ac97->flags |= AC97_STEREO_MUTES;
1942 	/* on AD1985 rev. 3, AC'97 revision bits are zero */
1943 	ac97->ext_id = (ac97->ext_id & ~AC97_EI_REV_MASK) | AC97_EI_REV_23;
1944 	return 0;
1945 }
1946 
1947 /*
1948  * realtek ALC65x/850 codecs
1949  */
1950 static void alc650_update_jacks(struct snd_ac97 *ac97)
1951 {
1952 	int shared;
1953 
1954 	/* shared Line-In */
1955 	shared = is_shared_linein(ac97);
1956 	snd_ac97_update_bits(ac97, AC97_ALC650_MULTICH, 1 << 9,
1957 			     shared ? (1 << 9) : 0);
1958 	/* update shared Mic */
1959 	shared = is_shared_micin(ac97);
1960 	/* disable/enable vref */
1961 	snd_ac97_update_bits(ac97, AC97_ALC650_CLOCK, 1 << 12,
1962 			     shared ? (1 << 12) : 0);
1963 	/* turn on/off center-on-mic */
1964 	snd_ac97_update_bits(ac97, AC97_ALC650_MULTICH, 1 << 10,
1965 			     shared ? (1 << 10) : 0);
1966 	/* GPIO0 high for mic */
1967 	snd_ac97_update_bits(ac97, AC97_ALC650_GPIO_STATUS, 0x100,
1968 			     shared ? 0 : 0x100);
1969 }
1970 
1971 static const struct snd_kcontrol_new snd_ac97_controls_alc650[] = {
1972 	AC97_SINGLE("Duplicate Front", AC97_ALC650_MULTICH, 0, 1, 0),
1973 	AC97_SINGLE("Surround Down Mix", AC97_ALC650_MULTICH, 1, 1, 0),
1974 	AC97_SINGLE("Center/LFE Down Mix", AC97_ALC650_MULTICH, 2, 1, 0),
1975 	AC97_SINGLE("Exchange Center/LFE", AC97_ALC650_MULTICH, 3, 1, 0),
1976 	/* 4: Analog Input To Surround */
1977 	/* 5: Analog Input To Center/LFE */
1978 	/* 6: Independent Master Volume Right */
1979 	/* 7: Independent Master Volume Left */
1980 	/* 8: reserved */
1981 	/* 9: Line-In/Surround share */
1982 	/* 10: Mic/CLFE share */
1983 	/* 11-13: in IEC958 controls */
1984 	AC97_SINGLE("Swap Surround Slot", AC97_ALC650_MULTICH, 14, 1, 0),
1985 #if 0 /* always set in patch_alc650 */
1986 	AC97_SINGLE("IEC958 Input Clock Enable", AC97_ALC650_CLOCK, 0, 1, 0),
1987 	AC97_SINGLE("IEC958 Input Pin Enable", AC97_ALC650_CLOCK, 1, 1, 0),
1988 	AC97_SINGLE("Surround DAC Switch", AC97_ALC650_SURR_DAC_VOL, 15, 1, 1),
1989 	AC97_DOUBLE("Surround DAC Volume", AC97_ALC650_SURR_DAC_VOL, 8, 0, 31, 1),
1990 	AC97_SINGLE("Center/LFE DAC Switch", AC97_ALC650_LFE_DAC_VOL, 15, 1, 1),
1991 	AC97_DOUBLE("Center/LFE DAC Volume", AC97_ALC650_LFE_DAC_VOL, 8, 0, 31, 1),
1992 #endif
1993 	AC97_SURROUND_JACK_MODE_CTL,
1994 	AC97_CHANNEL_MODE_CTL,
1995 };
1996 
1997 static const struct snd_kcontrol_new snd_ac97_spdif_controls_alc650[] = {
1998         AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), AC97_ALC650_MULTICH, 11, 1, 0),
1999         AC97_SINGLE("Analog to IEC958 Output", AC97_ALC650_MULTICH, 12, 1, 0),
2000 	/* disable this controls since it doesn't work as expected */
2001 	/* AC97_SINGLE("IEC958 Input Monitor", AC97_ALC650_MULTICH, 13, 1, 0), */
2002 };
2003 
2004 static int patch_alc650_specific(struct snd_ac97 * ac97)
2005 {
2006 	int err;
2007 
2008 	if ((err = patch_build_controls(ac97, snd_ac97_controls_alc650, ARRAY_SIZE(snd_ac97_controls_alc650))) < 0)
2009 		return err;
2010 	if (ac97->ext_id & AC97_EI_SPDIF) {
2011 		if ((err = patch_build_controls(ac97, snd_ac97_spdif_controls_alc650, ARRAY_SIZE(snd_ac97_spdif_controls_alc650))) < 0)
2012 			return err;
2013 	}
2014 	return 0;
2015 }
2016 
2017 static struct snd_ac97_build_ops patch_alc650_ops = {
2018 	.build_specific	= patch_alc650_specific,
2019 	.update_jacks = alc650_update_jacks
2020 };
2021 
2022 int patch_alc650(struct snd_ac97 * ac97)
2023 {
2024 	unsigned short val;
2025 
2026 	ac97->build_ops = &patch_alc650_ops;
2027 
2028 	/* determine the revision */
2029 	val = snd_ac97_read(ac97, AC97_ALC650_REVISION) & 0x3f;
2030 	if (val < 3)
2031 		ac97->id = 0x414c4720;          /* Old version */
2032 	else if (val < 0x10)
2033 		ac97->id = 0x414c4721;          /* D version */
2034 	else if (val < 0x20)
2035 		ac97->id = 0x414c4722;          /* E version */
2036 	else if (val < 0x30)
2037 		ac97->id = 0x414c4723;          /* F version */
2038 
2039 	/* revision E or F */
2040 	/* FIXME: what about revision D ? */
2041 	ac97->spec.dev_flags = (ac97->id == 0x414c4722 ||
2042 				ac97->id == 0x414c4723);
2043 
2044 	/* enable AC97_ALC650_GPIO_SETUP, AC97_ALC650_CLOCK for R/W */
2045 	snd_ac97_write_cache(ac97, AC97_ALC650_GPIO_STATUS,
2046 		snd_ac97_read(ac97, AC97_ALC650_GPIO_STATUS) | 0x8000);
2047 
2048 	/* Enable SPDIF-IN only on Rev.E and above */
2049 	val = snd_ac97_read(ac97, AC97_ALC650_CLOCK);
2050 	/* SPDIF IN with pin 47 */
2051 	if (ac97->spec.dev_flags &&
2052 	    /* ASUS A6KM requires EAPD */
2053 	    ! (ac97->subsystem_vendor == 0x1043 &&
2054 	       ac97->subsystem_device == 0x1103))
2055 		val |= 0x03; /* enable */
2056 	else
2057 		val &= ~0x03; /* disable */
2058 	snd_ac97_write_cache(ac97, AC97_ALC650_CLOCK, val);
2059 
2060 	/* set default: slot 3,4,7,8,6,9
2061 	   spdif-in monitor off, analog-spdif off, spdif-in off
2062 	   center on mic off, surround on line-in off
2063 	   downmix off, duplicate front off
2064 	*/
2065 	snd_ac97_write_cache(ac97, AC97_ALC650_MULTICH, 0);
2066 
2067 	/* set GPIO0 for mic bias */
2068 	/* GPIO0 pin output, no interrupt, high */
2069 	snd_ac97_write_cache(ac97, AC97_ALC650_GPIO_SETUP,
2070 			     snd_ac97_read(ac97, AC97_ALC650_GPIO_SETUP) | 0x01);
2071 	snd_ac97_write_cache(ac97, AC97_ALC650_GPIO_STATUS,
2072 			     (snd_ac97_read(ac97, AC97_ALC650_GPIO_STATUS) | 0x100) & ~0x10);
2073 
2074 	/* full DAC volume */
2075 	snd_ac97_write_cache(ac97, AC97_ALC650_SURR_DAC_VOL, 0x0808);
2076 	snd_ac97_write_cache(ac97, AC97_ALC650_LFE_DAC_VOL, 0x0808);
2077 	return 0;
2078 }
2079 
2080 static void alc655_update_jacks(struct snd_ac97 *ac97)
2081 {
2082 	int shared;
2083 
2084 	/* shared Line-In */
2085 	shared = is_shared_linein(ac97);
2086 	ac97_update_bits_page(ac97, AC97_ALC650_MULTICH, 1 << 9,
2087 			      shared ? (1 << 9) : 0, 0);
2088 	/* update shared mic */
2089 	shared = is_shared_micin(ac97);
2090 	/* misc control; vrefout disable */
2091 	snd_ac97_update_bits(ac97, AC97_ALC650_CLOCK, 1 << 12,
2092 			     shared ? (1 << 12) : 0);
2093 	ac97_update_bits_page(ac97, AC97_ALC650_MULTICH, 1 << 10,
2094 			      shared ? (1 << 10) : 0, 0);
2095 }
2096 
2097 static const struct snd_kcontrol_new snd_ac97_controls_alc655[] = {
2098 	AC97_PAGE_SINGLE("Duplicate Front", AC97_ALC650_MULTICH, 0, 1, 0, 0),
2099 	AC97_SURROUND_JACK_MODE_CTL,
2100 	AC97_CHANNEL_MODE_CTL,
2101 };
2102 
2103 static int alc655_iec958_route_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2104 {
2105 	static char *texts_655[3] = { "PCM", "Analog In", "IEC958 In" };
2106 	static char *texts_658[4] = { "PCM", "Analog1 In", "Analog2 In", "IEC958 In" };
2107 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2108 
2109 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2110 	uinfo->count = 1;
2111 	uinfo->value.enumerated.items = ac97->spec.dev_flags ? 4 : 3;
2112 	if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2113 		uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2114 	strcpy(uinfo->value.enumerated.name,
2115 	       ac97->spec.dev_flags ?
2116 	       texts_658[uinfo->value.enumerated.item] :
2117 	       texts_655[uinfo->value.enumerated.item]);
2118 	return 0;
2119 }
2120 
2121 static int alc655_iec958_route_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2122 {
2123 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2124 	unsigned short val;
2125 
2126 	val = ac97->regs[AC97_ALC650_MULTICH];
2127 	val = (val >> 12) & 3;
2128 	if (ac97->spec.dev_flags && val == 3)
2129 		val = 0;
2130 	ucontrol->value.enumerated.item[0] = val;
2131 	return 0;
2132 }
2133 
2134 static int alc655_iec958_route_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2135 {
2136 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2137 
2138 	return ac97_update_bits_page(ac97, AC97_ALC650_MULTICH, 3 << 12,
2139 				     (unsigned short)ucontrol->value.enumerated.item[0] << 12,
2140 				     0);
2141 }
2142 
2143 static const struct snd_kcontrol_new snd_ac97_spdif_controls_alc655[] = {
2144         AC97_PAGE_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), AC97_ALC650_MULTICH, 11, 1, 0, 0),
2145 	/* disable this controls since it doesn't work as expected */
2146         /* AC97_PAGE_SINGLE("IEC958 Input Monitor", AC97_ALC650_MULTICH, 14, 1, 0, 0), */
2147 	{
2148 		.iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
2149 		.name   = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
2150 		.info   = alc655_iec958_route_info,
2151 		.get    = alc655_iec958_route_get,
2152 		.put    = alc655_iec958_route_put,
2153 	},
2154 };
2155 
2156 static int patch_alc655_specific(struct snd_ac97 * ac97)
2157 {
2158 	int err;
2159 
2160 	if ((err = patch_build_controls(ac97, snd_ac97_controls_alc655, ARRAY_SIZE(snd_ac97_controls_alc655))) < 0)
2161 		return err;
2162 	if (ac97->ext_id & AC97_EI_SPDIF) {
2163 		if ((err = patch_build_controls(ac97, snd_ac97_spdif_controls_alc655, ARRAY_SIZE(snd_ac97_spdif_controls_alc655))) < 0)
2164 			return err;
2165 	}
2166 	return 0;
2167 }
2168 
2169 static struct snd_ac97_build_ops patch_alc655_ops = {
2170 	.build_specific	= patch_alc655_specific,
2171 	.update_jacks = alc655_update_jacks
2172 };
2173 
2174 int patch_alc655(struct snd_ac97 * ac97)
2175 {
2176 	unsigned int val;
2177 
2178 	if (ac97->id == AC97_ID_ALC658) {
2179 		ac97->spec.dev_flags = 1; /* ALC658 */
2180 		if ((snd_ac97_read(ac97, AC97_ALC650_REVISION) & 0x3f) == 2) {
2181 			ac97->id = AC97_ID_ALC658D;
2182 			ac97->spec.dev_flags = 2;
2183 		}
2184 	}
2185 
2186 	ac97->build_ops = &patch_alc655_ops;
2187 
2188 	/* assume only page 0 for writing cache */
2189 	snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, AC97_PAGE_VENDOR);
2190 
2191 	/* adjust default values */
2192 	val = snd_ac97_read(ac97, 0x7a); /* misc control */
2193 	if (ac97->spec.dev_flags) /* ALC658 */
2194 		val &= ~(1 << 1); /* Pin 47 is spdif input pin */
2195 	else { /* ALC655 */
2196 		if (ac97->subsystem_vendor == 0x1462 &&
2197 		    ac97->subsystem_device == 0x0131) /* MSI S270 laptop */
2198 			val &= ~(1 << 1); /* Pin 47 is EAPD (for internal speaker) */
2199 		else
2200 			val |= (1 << 1); /* Pin 47 is spdif input pin */
2201 	}
2202 	val &= ~(1 << 12); /* vref enable */
2203 	snd_ac97_write_cache(ac97, 0x7a, val);
2204 	/* set default: spdif-in enabled,
2205 	   spdif-in monitor off, spdif-in PCM off
2206 	   center on mic off, surround on line-in off
2207 	   duplicate front off
2208 	*/
2209 	snd_ac97_write_cache(ac97, AC97_ALC650_MULTICH, 1<<15);
2210 
2211 	/* full DAC volume */
2212 	snd_ac97_write_cache(ac97, AC97_ALC650_SURR_DAC_VOL, 0x0808);
2213 	snd_ac97_write_cache(ac97, AC97_ALC650_LFE_DAC_VOL, 0x0808);
2214 
2215 	/* update undocumented bit... */
2216 	if (ac97->id == AC97_ID_ALC658D)
2217 		snd_ac97_update_bits(ac97, 0x74, 0x0800, 0x0800);
2218 
2219 	return 0;
2220 }
2221 
2222 
2223 #define AC97_ALC850_JACK_SELECT	0x76
2224 #define AC97_ALC850_MISC1	0x7a
2225 
2226 static void alc850_update_jacks(struct snd_ac97 *ac97)
2227 {
2228 	int shared;
2229 
2230 	/* shared Line-In */
2231 	shared = is_shared_linein(ac97);
2232 	/* SURR 1kOhm (bit4), Amp (bit5) */
2233 	snd_ac97_update_bits(ac97, AC97_ALC850_MISC1, (1<<4)|(1<<5),
2234 			     shared ? (1<<5) : (1<<4));
2235 	/* LINE-IN = 0, SURROUND = 2 */
2236 	snd_ac97_update_bits(ac97, AC97_ALC850_JACK_SELECT, 7 << 12,
2237 			     shared ? (2<<12) : (0<<12));
2238 	/* update shared mic */
2239 	shared = is_shared_micin(ac97);
2240 	/* Vref disable (bit12), 1kOhm (bit13) */
2241 	snd_ac97_update_bits(ac97, AC97_ALC850_MISC1, (1<<12)|(1<<13),
2242 			     shared ? (1<<12) : (1<<13));
2243 	/* MIC-IN = 1, CENTER-LFE = 5 */
2244 	snd_ac97_update_bits(ac97, AC97_ALC850_JACK_SELECT, 7 << 4,
2245 			     shared ? (5<<4) : (1<<4));
2246 }
2247 
2248 static const struct snd_kcontrol_new snd_ac97_controls_alc850[] = {
2249 	AC97_PAGE_SINGLE("Duplicate Front", AC97_ALC650_MULTICH, 0, 1, 0, 0),
2250 	AC97_SINGLE("Mic Front Input Switch", AC97_ALC850_JACK_SELECT, 15, 1, 1),
2251 	AC97_SURROUND_JACK_MODE_CTL,
2252 	AC97_CHANNEL_MODE_CTL,
2253 };
2254 
2255 static int patch_alc850_specific(struct snd_ac97 *ac97)
2256 {
2257 	int err;
2258 
2259 	if ((err = patch_build_controls(ac97, snd_ac97_controls_alc850, ARRAY_SIZE(snd_ac97_controls_alc850))) < 0)
2260 		return err;
2261 	if (ac97->ext_id & AC97_EI_SPDIF) {
2262 		if ((err = patch_build_controls(ac97, snd_ac97_spdif_controls_alc655, ARRAY_SIZE(snd_ac97_spdif_controls_alc655))) < 0)
2263 			return err;
2264 	}
2265 	return 0;
2266 }
2267 
2268 static struct snd_ac97_build_ops patch_alc850_ops = {
2269 	.build_specific	= patch_alc850_specific,
2270 	.update_jacks = alc850_update_jacks
2271 };
2272 
2273 int patch_alc850(struct snd_ac97 *ac97)
2274 {
2275 	ac97->build_ops = &patch_alc850_ops;
2276 
2277 	ac97->spec.dev_flags = 0; /* for IEC958 playback route - ALC655 compatible */
2278 
2279 	/* assume only page 0 for writing cache */
2280 	snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, AC97_PAGE_VENDOR);
2281 
2282 	/* adjust default values */
2283 	/* set default: spdif-in enabled,
2284 	   spdif-in monitor off, spdif-in PCM off
2285 	   center on mic off, surround on line-in off
2286 	   duplicate front off
2287 	*/
2288 	snd_ac97_write_cache(ac97, AC97_ALC650_MULTICH, 1<<15);
2289 	/* SURR_OUT: on, Surr 1kOhm: on, Surr Amp: off, Front 1kOhm: off
2290 	 * Front Amp: on, Vref: enable, Center 1kOhm: on, Mix: on
2291 	 */
2292 	snd_ac97_write_cache(ac97, 0x7a, (1<<1)|(1<<4)|(0<<5)|(1<<6)|
2293 			     (1<<7)|(0<<12)|(1<<13)|(0<<14));
2294 	/* detection UIO2,3: all path floating, UIO3: MIC, Vref2: disable,
2295 	 * UIO1: FRONT, Vref3: disable, UIO3: LINE, Front-Mic: mute
2296 	 */
2297 	snd_ac97_write_cache(ac97, 0x76, (0<<0)|(0<<2)|(1<<4)|(1<<7)|(2<<8)|
2298 			     (1<<11)|(0<<12)|(1<<15));
2299 
2300 	/* full DAC volume */
2301 	snd_ac97_write_cache(ac97, AC97_ALC650_SURR_DAC_VOL, 0x0808);
2302 	snd_ac97_write_cache(ac97, AC97_ALC650_LFE_DAC_VOL, 0x0808);
2303 	return 0;
2304 }
2305 
2306 
2307 /*
2308  * C-Media CM97xx codecs
2309  */
2310 static void cm9738_update_jacks(struct snd_ac97 *ac97)
2311 {
2312 	/* shared Line-In */
2313 	snd_ac97_update_bits(ac97, AC97_CM9738_VENDOR_CTRL, 1 << 10,
2314 			     is_shared_linein(ac97) ? (1 << 10) : 0);
2315 }
2316 
2317 static const struct snd_kcontrol_new snd_ac97_cm9738_controls[] = {
2318 	AC97_SINGLE("Duplicate Front", AC97_CM9738_VENDOR_CTRL, 13, 1, 0),
2319 	AC97_SURROUND_JACK_MODE_CTL,
2320 	AC97_CHANNEL_MODE_4CH_CTL,
2321 };
2322 
2323 static int patch_cm9738_specific(struct snd_ac97 * ac97)
2324 {
2325 	return patch_build_controls(ac97, snd_ac97_cm9738_controls, ARRAY_SIZE(snd_ac97_cm9738_controls));
2326 }
2327 
2328 static struct snd_ac97_build_ops patch_cm9738_ops = {
2329 	.build_specific	= patch_cm9738_specific,
2330 	.update_jacks = cm9738_update_jacks
2331 };
2332 
2333 int patch_cm9738(struct snd_ac97 * ac97)
2334 {
2335 	ac97->build_ops = &patch_cm9738_ops;
2336 	/* FIXME: can anyone confirm below? */
2337 	/* CM9738 has no PCM volume although the register reacts */
2338 	ac97->flags |= AC97_HAS_NO_PCM_VOL;
2339 	snd_ac97_write_cache(ac97, AC97_PCM, 0x8000);
2340 
2341 	return 0;
2342 }
2343 
2344 static int snd_ac97_cmedia_spdif_playback_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2345 {
2346 	static char *texts[] = { "Analog", "Digital" };
2347 
2348 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2349 	uinfo->count = 1;
2350 	uinfo->value.enumerated.items = 2;
2351 	if (uinfo->value.enumerated.item > 1)
2352 		uinfo->value.enumerated.item = 1;
2353 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2354 	return 0;
2355 }
2356 
2357 static int snd_ac97_cmedia_spdif_playback_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2358 {
2359 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2360 	unsigned short val;
2361 
2362 	val = ac97->regs[AC97_CM9739_SPDIF_CTRL];
2363 	ucontrol->value.enumerated.item[0] = (val >> 1) & 0x01;
2364 	return 0;
2365 }
2366 
2367 static int snd_ac97_cmedia_spdif_playback_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2368 {
2369 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2370 
2371 	return snd_ac97_update_bits(ac97, AC97_CM9739_SPDIF_CTRL,
2372 				    0x01 << 1,
2373 				    (ucontrol->value.enumerated.item[0] & 0x01) << 1);
2374 }
2375 
2376 static const struct snd_kcontrol_new snd_ac97_cm9739_controls_spdif[] = {
2377 	/* BIT 0: SPDI_EN - always true */
2378 	{ /* BIT 1: SPDIFS */
2379 		.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
2380 		.name	= SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
2381 		.info	= snd_ac97_cmedia_spdif_playback_source_info,
2382 		.get	= snd_ac97_cmedia_spdif_playback_source_get,
2383 		.put	= snd_ac97_cmedia_spdif_playback_source_put,
2384 	},
2385 	/* BIT 2: IG_SPIV */
2386 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,NONE) "Valid Switch", AC97_CM9739_SPDIF_CTRL, 2, 1, 0),
2387 	/* BIT 3: SPI2F */
2388 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,NONE) "Monitor", AC97_CM9739_SPDIF_CTRL, 3, 1, 0),
2389 	/* BIT 4: SPI2SDI */
2390 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), AC97_CM9739_SPDIF_CTRL, 4, 1, 0),
2391 	/* BIT 8: SPD32 - 32bit SPDIF - not supported yet */
2392 };
2393 
2394 static void cm9739_update_jacks(struct snd_ac97 *ac97)
2395 {
2396 	/* shared Line-In */
2397 	snd_ac97_update_bits(ac97, AC97_CM9739_MULTI_CHAN, 1 << 10,
2398 			     is_shared_linein(ac97) ? (1 << 10) : 0);
2399 	/* shared Mic */
2400 	snd_ac97_update_bits(ac97, AC97_CM9739_MULTI_CHAN, 0x3000,
2401 			     is_shared_micin(ac97) ? 0x1000 : 0x2000);
2402 }
2403 
2404 static const struct snd_kcontrol_new snd_ac97_cm9739_controls[] = {
2405 	AC97_SURROUND_JACK_MODE_CTL,
2406 	AC97_CHANNEL_MODE_CTL,
2407 };
2408 
2409 static int patch_cm9739_specific(struct snd_ac97 * ac97)
2410 {
2411 	return patch_build_controls(ac97, snd_ac97_cm9739_controls, ARRAY_SIZE(snd_ac97_cm9739_controls));
2412 }
2413 
2414 static int patch_cm9739_post_spdif(struct snd_ac97 * ac97)
2415 {
2416 	return patch_build_controls(ac97, snd_ac97_cm9739_controls_spdif, ARRAY_SIZE(snd_ac97_cm9739_controls_spdif));
2417 }
2418 
2419 static struct snd_ac97_build_ops patch_cm9739_ops = {
2420 	.build_specific	= patch_cm9739_specific,
2421 	.build_post_spdif = patch_cm9739_post_spdif,
2422 	.update_jacks = cm9739_update_jacks
2423 };
2424 
2425 int patch_cm9739(struct snd_ac97 * ac97)
2426 {
2427 	unsigned short val;
2428 
2429 	ac97->build_ops = &patch_cm9739_ops;
2430 
2431 	/* CM9739/A has no Master and PCM volume although the register reacts */
2432 	ac97->flags |= AC97_HAS_NO_MASTER_VOL | AC97_HAS_NO_PCM_VOL;
2433 	snd_ac97_write_cache(ac97, AC97_MASTER, 0x8000);
2434 	snd_ac97_write_cache(ac97, AC97_PCM, 0x8000);
2435 
2436 	/* check spdif */
2437 	val = snd_ac97_read(ac97, AC97_EXTENDED_STATUS);
2438 	if (val & AC97_EA_SPCV) {
2439 		/* enable spdif in */
2440 		snd_ac97_write_cache(ac97, AC97_CM9739_SPDIF_CTRL,
2441 				     snd_ac97_read(ac97, AC97_CM9739_SPDIF_CTRL) | 0x01);
2442 		ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000; /* 48k only */
2443 	} else {
2444 		ac97->ext_id &= ~AC97_EI_SPDIF; /* disable extended-id */
2445 		ac97->rates[AC97_RATES_SPDIF] = 0;
2446 	}
2447 
2448 	/* set-up multi channel */
2449 	/* bit 14: 0 = SPDIF, 1 = EAPD */
2450 	/* bit 13: enable internal vref output for mic */
2451 	/* bit 12: disable center/lfe (swithable) */
2452 	/* bit 10: disable surround/line (switchable) */
2453 	/* bit 9: mix 2 surround off */
2454 	/* bit 4: undocumented; 0 mutes the CM9739A, which defaults to 1 */
2455 	/* bit 3: undocumented; surround? */
2456 	/* bit 0: dB */
2457 	val = snd_ac97_read(ac97, AC97_CM9739_MULTI_CHAN) & (1 << 4);
2458 	val |= (1 << 3);
2459 	val |= (1 << 13);
2460 	if (! (ac97->ext_id & AC97_EI_SPDIF))
2461 		val |= (1 << 14);
2462 	snd_ac97_write_cache(ac97, AC97_CM9739_MULTI_CHAN, val);
2463 
2464 	/* FIXME: set up GPIO */
2465 	snd_ac97_write_cache(ac97, 0x70, 0x0100);
2466 	snd_ac97_write_cache(ac97, 0x72, 0x0020);
2467 	/* Special exception for ASUS W1000/CMI9739. It does not have an SPDIF in. */
2468 	if (ac97->pci &&
2469 	     ac97->subsystem_vendor == 0x1043 &&
2470 	     ac97->subsystem_device == 0x1843) {
2471 		snd_ac97_write_cache(ac97, AC97_CM9739_SPDIF_CTRL,
2472 			snd_ac97_read(ac97, AC97_CM9739_SPDIF_CTRL) & ~0x01);
2473 		snd_ac97_write_cache(ac97, AC97_CM9739_MULTI_CHAN,
2474 			snd_ac97_read(ac97, AC97_CM9739_MULTI_CHAN) | (1 << 14));
2475 	}
2476 
2477 	return 0;
2478 }
2479 
2480 #define AC97_CM9761_MULTI_CHAN	0x64
2481 #define AC97_CM9761_FUNC	0x66
2482 #define AC97_CM9761_SPDIF_CTRL	0x6c
2483 
2484 static void cm9761_update_jacks(struct snd_ac97 *ac97)
2485 {
2486 	/* FIXME: check the bits for each model
2487 	 *        model 83 is confirmed to work
2488 	 */
2489 	static unsigned short surr_on[3][2] = {
2490 		{ 0x0008, 0x0000 }, /* 9761-78 & 82 */
2491 		{ 0x0000, 0x0008 }, /* 9761-82 rev.B */
2492 		{ 0x0000, 0x0008 }, /* 9761-83 */
2493 	};
2494 	static unsigned short clfe_on[3][2] = {
2495 		{ 0x0000, 0x1000 }, /* 9761-78 & 82 */
2496 		{ 0x1000, 0x0000 }, /* 9761-82 rev.B */
2497 		{ 0x0000, 0x1000 }, /* 9761-83 */
2498 	};
2499 	static unsigned short surr_shared[3][2] = {
2500 		{ 0x0000, 0x0400 }, /* 9761-78 & 82 */
2501 		{ 0x0000, 0x0400 }, /* 9761-82 rev.B */
2502 		{ 0x0000, 0x0400 }, /* 9761-83 */
2503 	};
2504 	static unsigned short clfe_shared[3][2] = {
2505 		{ 0x2000, 0x0880 }, /* 9761-78 & 82 */
2506 		{ 0x0000, 0x2880 }, /* 9761-82 rev.B */
2507 		{ 0x2000, 0x0800 }, /* 9761-83 */
2508 	};
2509 	unsigned short val = 0;
2510 
2511 	val |= surr_on[ac97->spec.dev_flags][is_surround_on(ac97)];
2512 	val |= clfe_on[ac97->spec.dev_flags][is_clfe_on(ac97)];
2513 	val |= surr_shared[ac97->spec.dev_flags][is_shared_linein(ac97)];
2514 	val |= clfe_shared[ac97->spec.dev_flags][is_shared_micin(ac97)];
2515 
2516 	snd_ac97_update_bits(ac97, AC97_CM9761_MULTI_CHAN, 0x3c88, val);
2517 }
2518 
2519 static const struct snd_kcontrol_new snd_ac97_cm9761_controls[] = {
2520 	AC97_SURROUND_JACK_MODE_CTL,
2521 	AC97_CHANNEL_MODE_CTL,
2522 };
2523 
2524 static int cm9761_spdif_out_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
2525 {
2526 	static char *texts[] = { "AC-Link", "ADC", "SPDIF-In" };
2527 
2528 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2529 	uinfo->count = 1;
2530 	uinfo->value.enumerated.items = 3;
2531 	if (uinfo->value.enumerated.item > 2)
2532 		uinfo->value.enumerated.item = 2;
2533 	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2534 	return 0;
2535 }
2536 
2537 static int cm9761_spdif_out_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2538 {
2539 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2540 
2541 	if (ac97->regs[AC97_CM9761_FUNC] & 0x1)
2542 		ucontrol->value.enumerated.item[0] = 2; /* SPDIF-loopback */
2543 	else if (ac97->regs[AC97_CM9761_SPDIF_CTRL] & 0x2)
2544 		ucontrol->value.enumerated.item[0] = 1; /* ADC loopback */
2545 	else
2546 		ucontrol->value.enumerated.item[0] = 0; /* AC-link */
2547 	return 0;
2548 }
2549 
2550 static int cm9761_spdif_out_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2551 {
2552 	struct snd_ac97 *ac97 = snd_kcontrol_chip(kcontrol);
2553 
2554 	if (ucontrol->value.enumerated.item[0] == 2)
2555 		return snd_ac97_update_bits(ac97, AC97_CM9761_FUNC, 0x1, 0x1);
2556 	snd_ac97_update_bits(ac97, AC97_CM9761_FUNC, 0x1, 0);
2557 	return snd_ac97_update_bits(ac97, AC97_CM9761_SPDIF_CTRL, 0x2,
2558 				    ucontrol->value.enumerated.item[0] == 1 ? 0x2 : 0);
2559 }
2560 
2561 static const char *cm9761_dac_clock[] = { "AC-Link", "SPDIF-In", "Both" };
2562 static const struct ac97_enum cm9761_dac_clock_enum =
2563 	AC97_ENUM_SINGLE(AC97_CM9761_SPDIF_CTRL, 9, 3, cm9761_dac_clock);
2564 
2565 static const struct snd_kcontrol_new snd_ac97_cm9761_controls_spdif[] = {
2566 	{ /* BIT 1: SPDIFS */
2567 		.iface	= SNDRV_CTL_ELEM_IFACE_MIXER,
2568 		.name	= SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
2569 		.info = cm9761_spdif_out_source_info,
2570 		.get = cm9761_spdif_out_source_get,
2571 		.put = cm9761_spdif_out_source_put,
2572 	},
2573 	/* BIT 2: IG_SPIV */
2574 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,NONE) "Valid Switch", AC97_CM9761_SPDIF_CTRL, 2, 1, 0),
2575 	/* BIT 3: SPI2F */
2576 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,NONE) "Monitor", AC97_CM9761_SPDIF_CTRL, 3, 1, 0),
2577 	/* BIT 4: SPI2SDI */
2578 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), AC97_CM9761_SPDIF_CTRL, 4, 1, 0),
2579 	/* BIT 9-10: DAC_CTL */
2580 	AC97_ENUM("DAC Clock Source", cm9761_dac_clock_enum),
2581 };
2582 
2583 static int patch_cm9761_post_spdif(struct snd_ac97 * ac97)
2584 {
2585 	return patch_build_controls(ac97, snd_ac97_cm9761_controls_spdif, ARRAY_SIZE(snd_ac97_cm9761_controls_spdif));
2586 }
2587 
2588 static int patch_cm9761_specific(struct snd_ac97 * ac97)
2589 {
2590 	return patch_build_controls(ac97, snd_ac97_cm9761_controls, ARRAY_SIZE(snd_ac97_cm9761_controls));
2591 }
2592 
2593 static struct snd_ac97_build_ops patch_cm9761_ops = {
2594 	.build_specific	= patch_cm9761_specific,
2595 	.build_post_spdif = patch_cm9761_post_spdif,
2596 	.update_jacks = cm9761_update_jacks
2597 };
2598 
2599 int patch_cm9761(struct snd_ac97 *ac97)
2600 {
2601 	unsigned short val;
2602 
2603 	/* CM9761 has no PCM volume although the register reacts */
2604 	/* Master volume seems to have _some_ influence on the analog
2605 	 * input sounds
2606 	 */
2607 	ac97->flags |= /*AC97_HAS_NO_MASTER_VOL |*/ AC97_HAS_NO_PCM_VOL;
2608 	snd_ac97_write_cache(ac97, AC97_MASTER, 0x8808);
2609 	snd_ac97_write_cache(ac97, AC97_PCM, 0x8808);
2610 
2611 	ac97->spec.dev_flags = 0; /* 1 = model 82 revision B, 2 = model 83 */
2612 	if (ac97->id == AC97_ID_CM9761_82) {
2613 		unsigned short tmp;
2614 		/* check page 1, reg 0x60 */
2615 		val = snd_ac97_read(ac97, AC97_INT_PAGING);
2616 		snd_ac97_write_cache(ac97, AC97_INT_PAGING, (val & ~0x0f) | 0x01);
2617 		tmp = snd_ac97_read(ac97, 0x60);
2618 		ac97->spec.dev_flags = tmp & 1; /* revision B? */
2619 		snd_ac97_write_cache(ac97, AC97_INT_PAGING, val);
2620 	} else if (ac97->id == AC97_ID_CM9761_83)
2621 		ac97->spec.dev_flags = 2;
2622 
2623 	ac97->build_ops = &patch_cm9761_ops;
2624 
2625 	/* enable spdif */
2626 	/* force the SPDIF bit in ext_id - codec doesn't set this bit! */
2627         ac97->ext_id |= AC97_EI_SPDIF;
2628 	/* to be sure: we overwrite the ext status bits */
2629 	snd_ac97_write_cache(ac97, AC97_EXTENDED_STATUS, 0x05c0);
2630 	/* Don't set 0x0200 here.  This results in the silent analog output */
2631 	snd_ac97_write_cache(ac97, AC97_CM9761_SPDIF_CTRL, 0x0001); /* enable spdif-in */
2632 	ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000; /* 48k only */
2633 
2634 	/* set-up multi channel */
2635 	/* bit 15: pc master beep off
2636 	 * bit 14: pin47 = EAPD/SPDIF
2637 	 * bit 13: vref ctl [= cm9739]
2638 	 * bit 12: CLFE control (reverted on rev B)
2639 	 * bit 11: Mic/center share (reverted on rev B)
2640 	 * bit 10: suddound/line share
2641 	 * bit  9: Analog-in mix -> surround
2642 	 * bit  8: Analog-in mix -> CLFE
2643 	 * bit  7: Mic/LFE share (mic/center/lfe)
2644 	 * bit  5: vref select (9761A)
2645 	 * bit  4: front control
2646 	 * bit  3: surround control (revereted with rev B)
2647 	 * bit  2: front mic
2648 	 * bit  1: stereo mic
2649 	 * bit  0: mic boost level (0=20dB, 1=30dB)
2650 	 */
2651 
2652 #if 0
2653 	if (ac97->spec.dev_flags)
2654 		val = 0x0214;
2655 	else
2656 		val = 0x321c;
2657 #endif
2658 	val = snd_ac97_read(ac97, AC97_CM9761_MULTI_CHAN);
2659 	val |= (1 << 4); /* front on */
2660 	snd_ac97_write_cache(ac97, AC97_CM9761_MULTI_CHAN, val);
2661 
2662 	/* FIXME: set up GPIO */
2663 	snd_ac97_write_cache(ac97, 0x70, 0x0100);
2664 	snd_ac97_write_cache(ac97, 0x72, 0x0020);
2665 
2666 	return 0;
2667 }
2668 
2669 #define AC97_CM9780_SIDE	0x60
2670 #define AC97_CM9780_JACK	0x62
2671 #define AC97_CM9780_MIXER	0x64
2672 #define AC97_CM9780_MULTI_CHAN	0x66
2673 #define AC97_CM9780_SPDIF	0x6c
2674 
2675 static const char *cm9780_ch_select[] = { "Front", "Side", "Center/LFE", "Rear" };
2676 static const struct ac97_enum cm9780_ch_select_enum =
2677 	AC97_ENUM_SINGLE(AC97_CM9780_MULTI_CHAN, 6, 4, cm9780_ch_select);
2678 static const struct snd_kcontrol_new cm9780_controls[] = {
2679 	AC97_DOUBLE("Side Playback Switch", AC97_CM9780_SIDE, 15, 7, 1, 1),
2680 	AC97_DOUBLE("Side Playback Volume", AC97_CM9780_SIDE, 8, 0, 31, 0),
2681 	AC97_ENUM("Side Playback Route", cm9780_ch_select_enum),
2682 };
2683 
2684 static int patch_cm9780_specific(struct snd_ac97 *ac97)
2685 {
2686 	return patch_build_controls(ac97, cm9780_controls, ARRAY_SIZE(cm9780_controls));
2687 }
2688 
2689 static struct snd_ac97_build_ops patch_cm9780_ops = {
2690 	.build_specific	= patch_cm9780_specific,
2691 	.build_post_spdif = patch_cm9761_post_spdif	/* identical with CM9761 */
2692 };
2693 
2694 int patch_cm9780(struct snd_ac97 *ac97)
2695 {
2696 	unsigned short val;
2697 
2698 	ac97->build_ops = &patch_cm9780_ops;
2699 
2700 	/* enable spdif */
2701 	if (ac97->ext_id & AC97_EI_SPDIF) {
2702 		ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000; /* 48k only */
2703 		val = snd_ac97_read(ac97, AC97_CM9780_SPDIF);
2704 		val |= 0x1; /* SPDI_EN */
2705 		snd_ac97_write_cache(ac97, AC97_CM9780_SPDIF, val);
2706 	}
2707 
2708 	return 0;
2709 }
2710 
2711 /*
2712  * VIA VT1616 codec
2713  */
2714 static const struct snd_kcontrol_new snd_ac97_controls_vt1616[] = {
2715 AC97_SINGLE("DC Offset removal", 0x5a, 10, 1, 0),
2716 AC97_SINGLE("Alternate Level to Surround Out", 0x5a, 15, 1, 0),
2717 AC97_SINGLE("Downmix LFE and Center to Front", 0x5a, 12, 1, 0),
2718 AC97_SINGLE("Downmix Surround to Front", 0x5a, 11, 1, 0),
2719 };
2720 
2721 static int patch_vt1616_specific(struct snd_ac97 * ac97)
2722 {
2723 	int err;
2724 
2725 	if (snd_ac97_try_bit(ac97, 0x5a, 9))
2726 		if ((err = patch_build_controls(ac97, &snd_ac97_controls_vt1616[0], 1)) < 0)
2727 			return err;
2728 	if ((err = patch_build_controls(ac97, &snd_ac97_controls_vt1616[1], ARRAY_SIZE(snd_ac97_controls_vt1616) - 1)) < 0)
2729 		return err;
2730 	return 0;
2731 }
2732 
2733 static struct snd_ac97_build_ops patch_vt1616_ops = {
2734 	.build_specific	= patch_vt1616_specific
2735 };
2736 
2737 int patch_vt1616(struct snd_ac97 * ac97)
2738 {
2739 	ac97->build_ops = &patch_vt1616_ops;
2740 	return 0;
2741 }
2742 
2743 /*
2744  * VT1617A codec
2745  */
2746 int patch_vt1617a(struct snd_ac97 * ac97)
2747 {
2748 	ac97->ext_id |= AC97_EI_SPDIF;	/* force the detection of spdif */
2749 	ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000;
2750 	return 0;
2751 }
2752 
2753 /*
2754  */
2755 static void it2646_update_jacks(struct snd_ac97 *ac97)
2756 {
2757 	/* shared Line-In */
2758 	snd_ac97_update_bits(ac97, 0x76, 1 << 9,
2759 			     is_shared_linein(ac97) ? (1<<9) : 0);
2760 	/* shared Mic */
2761 	snd_ac97_update_bits(ac97, 0x76, 1 << 10,
2762 			     is_shared_micin(ac97) ? (1<<10) : 0);
2763 }
2764 
2765 static const struct snd_kcontrol_new snd_ac97_controls_it2646[] = {
2766 	AC97_SURROUND_JACK_MODE_CTL,
2767 	AC97_CHANNEL_MODE_CTL,
2768 };
2769 
2770 static const struct snd_kcontrol_new snd_ac97_spdif_controls_it2646[] = {
2771 	AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), 0x76, 11, 1, 0),
2772 	AC97_SINGLE("Analog to IEC958 Output", 0x76, 12, 1, 0),
2773 	AC97_SINGLE("IEC958 Input Monitor", 0x76, 13, 1, 0),
2774 };
2775 
2776 static int patch_it2646_specific(struct snd_ac97 * ac97)
2777 {
2778 	int err;
2779 	if ((err = patch_build_controls(ac97, snd_ac97_controls_it2646, ARRAY_SIZE(snd_ac97_controls_it2646))) < 0)
2780 		return err;
2781 	if ((err = patch_build_controls(ac97, snd_ac97_spdif_controls_it2646, ARRAY_SIZE(snd_ac97_spdif_controls_it2646))) < 0)
2782 		return err;
2783 	return 0;
2784 }
2785 
2786 static struct snd_ac97_build_ops patch_it2646_ops = {
2787 	.build_specific	= patch_it2646_specific,
2788 	.update_jacks = it2646_update_jacks
2789 };
2790 
2791 int patch_it2646(struct snd_ac97 * ac97)
2792 {
2793 	ac97->build_ops = &patch_it2646_ops;
2794 	/* full DAC volume */
2795 	snd_ac97_write_cache(ac97, 0x5E, 0x0808);
2796 	snd_ac97_write_cache(ac97, 0x7A, 0x0808);
2797 	return 0;
2798 }
2799 
2800 /*
2801  * Si3036 codec
2802  */
2803 
2804 #define AC97_SI3036_CHIP_ID     0x5a
2805 #define AC97_SI3036_LINE_CFG    0x5c
2806 
2807 static const struct snd_kcontrol_new snd_ac97_controls_si3036[] = {
2808 AC97_DOUBLE("Modem Speaker Volume", 0x5c, 14, 12, 3, 1)
2809 };
2810 
2811 static int patch_si3036_specific(struct snd_ac97 * ac97)
2812 {
2813 	int idx, err;
2814 	for (idx = 0; idx < ARRAY_SIZE(snd_ac97_controls_si3036); idx++)
2815 		if ((err = snd_ctl_add(ac97->bus->card, snd_ctl_new1(&snd_ac97_controls_si3036[idx], ac97))) < 0)
2816 			return err;
2817 	return 0;
2818 }
2819 
2820 static struct snd_ac97_build_ops patch_si3036_ops = {
2821 	.build_specific	= patch_si3036_specific,
2822 };
2823 
2824 int mpatch_si3036(struct snd_ac97 * ac97)
2825 {
2826 	ac97->build_ops = &patch_si3036_ops;
2827 	snd_ac97_write_cache(ac97, 0x5c, 0xf210 );
2828 	snd_ac97_write_cache(ac97, 0x68, 0);
2829 	return 0;
2830 }
2831 
2832 /*
2833  * LM 4550 Codec
2834  *
2835  * We use a static resolution table since LM4550 codec cannot be
2836  * properly autoprobed to determine the resolution via
2837  * check_volume_resolution().
2838  */
2839 
2840 static struct snd_ac97_res_table lm4550_restbl[] = {
2841 	{ AC97_MASTER, 0x1f1f },
2842 	{ AC97_HEADPHONE, 0x1f1f },
2843 	{ AC97_MASTER_MONO, 0x001f },
2844 	{ AC97_PC_BEEP, 0x001f },	/* LSB is ignored */
2845 	{ AC97_PHONE, 0x001f },
2846 	{ AC97_MIC, 0x001f },
2847 	{ AC97_LINE, 0x1f1f },
2848 	{ AC97_CD, 0x1f1f },
2849 	{ AC97_VIDEO, 0x1f1f },
2850 	{ AC97_AUX, 0x1f1f },
2851 	{ AC97_PCM, 0x1f1f },
2852 	{ AC97_REC_GAIN, 0x0f0f },
2853 	{ } /* terminator */
2854 };
2855 
2856 int patch_lm4550(struct snd_ac97 *ac97)
2857 {
2858 	ac97->res_table = lm4550_restbl;
2859 	return 0;
2860 }
2861