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