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