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