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