vserver 1.9.3
[linux-2.6.git] / sound / pci / ac97 / ac97_patch.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *  Universal interface for Audio Codec '97
4  *
5  *  For more details look to AC '97 component specification revision 2.2
6  *  by Intel Corporation (http://developer.intel.com) and to datasheets
7  *  for specific codecs.
8  *
9  *
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of the GNU General Public License as published by
12  *   the Free Software Foundation; either version 2 of the License, or
13  *   (at your option) any later version.
14  *
15  *   This program is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with this program; if not, write to the Free Software
22  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  *
24  */
25
26 #include <sound/driver.h>
27 #include <linux/delay.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <sound/core.h>
31 #include <sound/pcm.h>
32 #include <sound/control.h>
33 #include <sound/ac97_codec.h>
34 #include "ac97_patch.h"
35 #include "ac97_id.h"
36 #include "ac97_local.h"
37
38 /*
39  *  Chip specific initialization
40  */
41
42 static int patch_build_controls(ac97_t * ac97, const snd_kcontrol_new_t *controls, int count)
43 {
44         int idx, err;
45
46         for (idx = 0; idx < count; idx++)
47                 if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&controls[idx], ac97))) < 0)
48                         return err;
49         return 0;
50 }
51
52 /* set to the page, update bits and restore the page */
53 static int ac97_update_bits_page(ac97_t *ac97, unsigned short reg, unsigned short mask, unsigned short value, unsigned short page)
54 {
55         unsigned short page_save;
56         int ret;
57
58         down(&ac97->mutex);
59         page_save = snd_ac97_read(ac97, AC97_INT_PAGING) & AC97_PAGE_MASK;
60         snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page);
61         ret = snd_ac97_update_bits(ac97, reg, mask, value);
62         snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page_save);
63         up(&ac97->mutex); /* unlock paging */
64         return ret;
65 }
66
67 /* The following snd_ac97_ymf753_... items added by David Shust (dshust@shustring.com) */
68
69 /* It is possible to indicate to the Yamaha YMF753 the type of speakers being used. */
70 static int snd_ac97_ymf753_info_speaker(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
71 {
72         static char *texts[3] = {
73                 "Standard", "Small", "Smaller"
74         };
75
76         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
77         uinfo->count = 1;
78         uinfo->value.enumerated.items = 3;
79         if (uinfo->value.enumerated.item > 2)
80                 uinfo->value.enumerated.item = 2;
81         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
82         return 0;
83 }
84
85 static int snd_ac97_ymf753_get_speaker(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
86 {
87         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
88         unsigned short val;
89
90         val = ac97->regs[AC97_YMF753_3D_MODE_SEL];
91         val = (val >> 10) & 3;
92         if (val > 0)    /* 0 = invalid */
93                 val--;
94         ucontrol->value.enumerated.item[0] = val;
95         return 0;
96 }
97
98 static int snd_ac97_ymf753_put_speaker(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
99 {
100         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
101         unsigned short val;
102
103         if (ucontrol->value.enumerated.item[0] > 2)
104                 return -EINVAL;
105         val = (ucontrol->value.enumerated.item[0] + 1) << 10;
106         return snd_ac97_update(ac97, AC97_YMF753_3D_MODE_SEL, val);
107 }
108
109 static const snd_kcontrol_new_t snd_ac97_ymf753_controls_speaker =
110 {
111         .iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
112         .name   = "3D Control - Speaker",
113         .info   = snd_ac97_ymf753_info_speaker,
114         .get    = snd_ac97_ymf753_get_speaker,
115         .put    = snd_ac97_ymf753_put_speaker,
116 };
117
118 /* It is possible to indicate to the Yamaha YMF753 the source to direct to the S/PDIF output. */
119 static int snd_ac97_ymf753_spdif_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
120 {
121         static char *texts[2] = { "AC-Link", "A/D Converter" };
122
123         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
124         uinfo->count = 1;
125         uinfo->value.enumerated.items = 2;
126         if (uinfo->value.enumerated.item > 1)
127                 uinfo->value.enumerated.item = 1;
128         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
129         return 0;
130 }
131
132 static int snd_ac97_ymf753_spdif_source_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
133 {
134         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
135         unsigned short val;
136
137         val = ac97->regs[AC97_YMF753_DIT_CTRL2];
138         ucontrol->value.enumerated.item[0] = (val >> 1) & 1;
139         return 0;
140 }
141
142 static int snd_ac97_ymf753_spdif_source_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
143 {
144         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
145         unsigned short val;
146
147         if (ucontrol->value.enumerated.item[0] > 1)
148                 return -EINVAL;
149         val = ucontrol->value.enumerated.item[0] << 1;
150         return snd_ac97_update_bits(ac97, AC97_YMF753_DIT_CTRL2, 0x0002, val);
151 }
152
153 /* The AC'97 spec states that the S/PDIF signal is to be output at pin 48.
154    The YMF753 will output the S/PDIF signal to pin 43, 47 (EAPD), or 48.
155    By default, no output pin is selected, and the S/PDIF signal is not output.
156    There is also a bit to mute S/PDIF output in a vendor-specific register. */
157 static int snd_ac97_ymf753_spdif_output_pin_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
158 {
159         static char *texts[3] = { "Disabled", "Pin 43", "Pin 48" };
160
161         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
162         uinfo->count = 1;
163         uinfo->value.enumerated.items = 3;
164         if (uinfo->value.enumerated.item > 2)
165                 uinfo->value.enumerated.item = 2;
166         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
167         return 0;
168 }
169
170 static int snd_ac97_ymf753_spdif_output_pin_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
171 {
172         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
173         unsigned short val;
174
175         val = ac97->regs[AC97_YMF753_DIT_CTRL2];
176         ucontrol->value.enumerated.item[0] = (val & 0x0008) ? 2 : (val & 0x0020) ? 1 : 0;
177         return 0;
178 }
179
180 static int snd_ac97_ymf753_spdif_output_pin_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
181 {
182         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
183         unsigned short val;
184
185         if (ucontrol->value.enumerated.item[0] > 2)
186                 return -EINVAL;
187         val = (ucontrol->value.enumerated.item[0] == 2) ? 0x0008 :
188               (ucontrol->value.enumerated.item[0] == 1) ? 0x0020 : 0;
189         return snd_ac97_update_bits(ac97, AC97_YMF753_DIT_CTRL2, 0x0028, val);
190         /* The following can be used to direct S/PDIF output to pin 47 (EAPD).
191            snd_ac97_write_cache(ac97, 0x62, snd_ac97_read(ac97, 0x62) | 0x0008); */
192 }
193
194 static const snd_kcontrol_new_t snd_ac97_ymf753_controls_spdif[3] = {
195         {
196                 .iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
197                 .name   = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
198                 .info   = snd_ac97_ymf753_spdif_source_info,
199                 .get    = snd_ac97_ymf753_spdif_source_get,
200                 .put    = snd_ac97_ymf753_spdif_source_put,
201         },
202         {
203                 .iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
204                 .name   = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Output Pin",
205                 .info   = snd_ac97_ymf753_spdif_output_pin_info,
206                 .get    = snd_ac97_ymf753_spdif_output_pin_get,
207                 .put    = snd_ac97_ymf753_spdif_output_pin_put,
208         },
209         AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",NONE,NONE) "Mute", AC97_YMF753_DIT_CTRL2, 2, 1, 1)
210 };
211
212 static int patch_yamaha_ymf753_3d(ac97_t * ac97)
213 {
214         snd_kcontrol_t *kctl;
215         int err;
216
217         if ((err = snd_ctl_add(ac97->bus->card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
218                 return err;
219         strcpy(kctl->id.name, "3D Control - Wide");
220         kctl->private_value = AC97_SINGLE_VALUE(AC97_3D_CONTROL, 9, 7, 0);
221         snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000);
222         if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&snd_ac97_ymf753_controls_speaker, ac97))) < 0)
223                 return err;
224         snd_ac97_write_cache(ac97, AC97_YMF753_3D_MODE_SEL, 0x0c00);
225         return 0;
226 }
227
228 static int patch_yamaha_ymf753_post_spdif(ac97_t * ac97)
229 {
230         int err;
231
232         if ((err = patch_build_controls(ac97, snd_ac97_ymf753_controls_spdif, ARRAY_SIZE(snd_ac97_ymf753_controls_spdif))) < 0)
233                 return err;
234         return 0;
235 }
236
237 static struct snd_ac97_build_ops patch_yamaha_ymf753_ops = {
238         .build_3d       = patch_yamaha_ymf753_3d,
239         .build_post_spdif = patch_yamaha_ymf753_post_spdif
240 };
241
242 int patch_yamaha_ymf753(ac97_t * ac97)
243 {
244         /* Patch for Yamaha YMF753, Copyright (c) by David Shust, dshust@shustring.com.
245            This chip has nonstandard and extended behaviour with regard to its S/PDIF output.
246            The AC'97 spec states that the S/PDIF signal is to be output at pin 48.
247            The YMF753 will ouput the S/PDIF signal to pin 43, 47 (EAPD), or 48.
248            By default, no output pin is selected, and the S/PDIF signal is not output.
249            There is also a bit to mute S/PDIF output in a vendor-specific register.
250         */
251         ac97->build_ops = &patch_yamaha_ymf753_ops;
252         ac97->caps |= AC97_BC_BASS_TREBLE;
253         ac97->caps |= 0x04 << 10; /* Yamaha 3D enhancement */
254         return 0;
255 }
256
257 /*
258  * May 2, 2003 Liam Girdwood <liam.girdwood@wolfsonmicro.com>
259  *  removed broken wolfson00 patch.
260  *  added support for WM9705,WM9708,WM9709,WM9710,WM9711,WM9712 and WM9717.
261  */
262
263 int patch_wolfson03(ac97_t * ac97)
264 {
265         /* This is known to work for the ViewSonic ViewPad 1000
266            Randolph Bentson <bentson@holmsjoen.com> */
267
268         // WM9703/9707/9708/9717
269         snd_ac97_write_cache(ac97, AC97_WM97XX_FMIXER_VOL, 0x0808);
270         snd_ac97_write_cache(ac97, AC97_GENERAL_PURPOSE, 0x8000);
271         return 0;
272 }
273   
274 int patch_wolfson04(ac97_t * ac97)
275 {
276         // WM9704M/9704Q
277         // set front and rear mixer volume
278         snd_ac97_write_cache(ac97, AC97_WM97XX_FMIXER_VOL, 0x0808);
279         snd_ac97_write_cache(ac97, AC97_WM9704_RMIXER_VOL, 0x0808);
280         
281         // patch for DVD noise
282         snd_ac97_write_cache(ac97, AC97_WM9704_TEST, 0x0200);
283  
284         // init vol
285         snd_ac97_write_cache(ac97, AC97_WM9704_RPCM_VOL, 0x0808);
286  
287         // set rear surround volume
288         snd_ac97_write_cache(ac97, AC97_SURROUND_MASTER, 0x0000);
289         return 0;
290 }
291   
292 int patch_wolfson05(ac97_t * ac97)
293 {
294         // WM9705, WM9710
295         // set front mixer volume
296         snd_ac97_write_cache(ac97, AC97_WM97XX_FMIXER_VOL, 0x0808);
297         return 0;
298 }
299
300 int patch_wolfson11(ac97_t * ac97)
301 {
302         // WM9711, WM9712
303         // set out3 volume
304         snd_ac97_write_cache(ac97, AC97_WM9711_OUT3VOL, 0x0808);
305         return 0;
306 }
307
308 /*
309  * Tritech codec
310  */
311 int patch_tritech_tr28028(ac97_t * ac97)
312 {
313         snd_ac97_write_cache(ac97, 0x26, 0x0300);
314         snd_ac97_write_cache(ac97, 0x26, 0x0000);
315         snd_ac97_write_cache(ac97, AC97_SURROUND_MASTER, 0x0000);
316         snd_ac97_write_cache(ac97, AC97_SPDIF, 0x0000);
317         return 0;
318 }
319
320 /*
321  * Sigmatel STAC97xx codecs
322  */
323 static int patch_sigmatel_stac9700_3d(ac97_t * ac97)
324 {
325         snd_kcontrol_t *kctl;
326         int err;
327
328         if ((err = snd_ctl_add(ac97->bus->card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
329                 return err;
330         strcpy(kctl->id.name, "3D Control Sigmatel - Depth");
331         kctl->private_value = AC97_SINGLE_VALUE(AC97_3D_CONTROL, 2, 3, 0);
332         snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000);
333         return 0;
334 }
335
336 static int patch_sigmatel_stac9708_3d(ac97_t * ac97)
337 {
338         snd_kcontrol_t *kctl;
339         int err;
340
341         if ((err = snd_ctl_add(ac97->bus->card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
342                 return err;
343         strcpy(kctl->id.name, "3D Control Sigmatel - Depth");
344         kctl->private_value = AC97_SINGLE_VALUE(AC97_3D_CONTROL, 0, 3, 0);
345         if ((err = snd_ctl_add(ac97->bus->card, kctl = snd_ac97_cnew(&snd_ac97_controls_3d[0], ac97))) < 0)
346                 return err;
347         strcpy(kctl->id.name, "3D Control Sigmatel - Rear Depth");
348         kctl->private_value = AC97_SINGLE_VALUE(AC97_3D_CONTROL, 2, 3, 0);
349         snd_ac97_write_cache(ac97, AC97_3D_CONTROL, 0x0000);
350         return 0;
351 }
352
353 static const snd_kcontrol_new_t snd_ac97_sigmatel_4speaker =
354 AC97_SINGLE("Sigmatel 4-Speaker Stereo Playback Switch", AC97_SIGMATEL_DAC2INVERT, 2, 1, 0);
355
356 static const snd_kcontrol_new_t snd_ac97_sigmatel_phaseinvert =
357 AC97_SINGLE("Sigmatel Surround Phase Inversion Playback Switch", AC97_SIGMATEL_DAC2INVERT, 3, 1, 0);
358
359 static const snd_kcontrol_new_t snd_ac97_sigmatel_controls[] = {
360 AC97_SINGLE("Sigmatel DAC 6dB Attenuate", AC97_SIGMATEL_ANALOG, 1, 1, 0),
361 AC97_SINGLE("Sigmatel ADC 6dB Attenuate", AC97_SIGMATEL_ANALOG, 0, 1, 0)
362 };
363
364 static int patch_sigmatel_stac97xx_specific(ac97_t * ac97)
365 {
366         int err;
367
368         snd_ac97_write_cache(ac97, AC97_SIGMATEL_ANALOG, snd_ac97_read(ac97, AC97_SIGMATEL_ANALOG) & ~0x0003);
369         if (snd_ac97_try_bit(ac97, AC97_SIGMATEL_ANALOG, 1))
370                 if ((err = patch_build_controls(ac97, &snd_ac97_sigmatel_controls[0], 1)) < 0)
371                         return err;
372         if (snd_ac97_try_bit(ac97, AC97_SIGMATEL_ANALOG, 0))
373                 if ((err = patch_build_controls(ac97, &snd_ac97_sigmatel_controls[1], 1)) < 0)
374                         return err;
375         if (snd_ac97_try_bit(ac97, AC97_SIGMATEL_DAC2INVERT, 2))
376                 if ((err = patch_build_controls(ac97, &snd_ac97_sigmatel_4speaker, 1)) < 0)
377                         return err;
378         if (snd_ac97_try_bit(ac97, AC97_SIGMATEL_DAC2INVERT, 3))
379                 if ((err = patch_build_controls(ac97, &snd_ac97_sigmatel_phaseinvert, 1)) < 0)
380                         return err;
381         return 0;
382 }
383
384 static struct snd_ac97_build_ops patch_sigmatel_stac9700_ops = {
385         .build_3d       = patch_sigmatel_stac9700_3d,
386         .build_specific = patch_sigmatel_stac97xx_specific
387 };
388
389 int patch_sigmatel_stac9700(ac97_t * ac97)
390 {
391         ac97->build_ops = &patch_sigmatel_stac9700_ops;
392         return 0;
393 }
394
395 static int patch_sigmatel_stac9708_specific(ac97_t *ac97)
396 {
397         snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Sigmatel Surround Playback");
398         return patch_sigmatel_stac97xx_specific(ac97);
399 }
400
401 static struct snd_ac97_build_ops patch_sigmatel_stac9708_ops = {
402         .build_3d       = patch_sigmatel_stac9708_3d,
403         .build_specific = patch_sigmatel_stac9708_specific
404 };
405
406 int patch_sigmatel_stac9708(ac97_t * ac97)
407 {
408         unsigned int codec72, codec6c;
409
410         ac97->build_ops = &patch_sigmatel_stac9708_ops;
411         ac97->caps |= 0x10;     /* HP (sigmatel surround) support */
412
413         codec72 = snd_ac97_read(ac97, AC97_SIGMATEL_BIAS2) & 0x8000;
414         codec6c = snd_ac97_read(ac97, AC97_SIGMATEL_ANALOG);
415
416         if ((codec72==0) && (codec6c==0)) {
417                 snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC1, 0xabba);
418                 snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC2, 0x1000);
419                 snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS1, 0xabba);
420                 snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS2, 0x0007);
421         } else if ((codec72==0x8000) && (codec6c==0)) {
422                 snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC1, 0xabba);
423                 snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC2, 0x1001);
424                 snd_ac97_write_cache(ac97, AC97_SIGMATEL_DAC2INVERT, 0x0008);
425         } else if ((codec72==0x8000) && (codec6c==0x0080)) {
426                 /* nothing */
427         }
428         snd_ac97_write_cache(ac97, AC97_SIGMATEL_MULTICHN, 0x0000);
429         return 0;
430 }
431
432 int patch_sigmatel_stac9721(ac97_t * ac97)
433 {
434         ac97->build_ops = &patch_sigmatel_stac9700_ops;
435         if (snd_ac97_read(ac97, AC97_SIGMATEL_ANALOG) == 0) {
436                 // patch for SigmaTel
437                 snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC1, 0xabba);
438                 snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC2, 0x4000);
439                 snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS1, 0xabba);
440                 snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS2, 0x0002);
441         }
442         snd_ac97_write_cache(ac97, AC97_SIGMATEL_MULTICHN, 0x0000);
443         return 0;
444 }
445
446 int patch_sigmatel_stac9744(ac97_t * ac97)
447 {
448         // patch for SigmaTel
449         ac97->build_ops = &patch_sigmatel_stac9700_ops;
450         snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC1, 0xabba);
451         snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC2, 0x0000); /* is this correct? --jk */
452         snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS1, 0xabba);
453         snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS2, 0x0002);
454         snd_ac97_write_cache(ac97, AC97_SIGMATEL_MULTICHN, 0x0000);
455         return 0;
456 }
457
458 int patch_sigmatel_stac9756(ac97_t * ac97)
459 {
460         // patch for SigmaTel
461         ac97->build_ops = &patch_sigmatel_stac9700_ops;
462         snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC1, 0xabba);
463         snd_ac97_write_cache(ac97, AC97_SIGMATEL_CIC2, 0x0000); /* is this correct? --jk */
464         snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS1, 0xabba);
465         snd_ac97_write_cache(ac97, AC97_SIGMATEL_BIAS2, 0x0002);
466         snd_ac97_write_cache(ac97, AC97_SIGMATEL_MULTICHN, 0x0000);
467         return 0;
468 }
469
470 static int snd_ac97_stac9758_output_jack_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
471 {
472         static char *texts[5] = { "Input/Disabled", "Front Output",
473                 "Rear Output", "Center/LFE Output", "Mixer Output" };
474
475         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
476         uinfo->count = 1;
477         uinfo->value.enumerated.items = 5;
478         if (uinfo->value.enumerated.item > 4)
479                 uinfo->value.enumerated.item = 4;
480         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
481         return 0;
482 }
483
484 static int snd_ac97_stac9758_output_jack_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t* ucontrol)
485 {
486         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
487         int shift = kcontrol->private_value;
488         unsigned short val;
489
490         val = ac97->regs[AC97_SIGMATEL_OUTSEL] >> shift;
491         if (!(val & 4))
492                 ucontrol->value.enumerated.item[0] = 0;
493         else
494                 ucontrol->value.enumerated.item[0] = 1 + (val & 3);
495         return 0;
496 }
497
498 static int snd_ac97_stac9758_output_jack_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
499 {
500         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
501         int shift = kcontrol->private_value;
502         unsigned short val;
503
504         if (ucontrol->value.enumerated.item[0] > 4)
505                 return -EINVAL;
506         if (ucontrol->value.enumerated.item[0] == 0)
507                 val = 0;
508         else
509                 val = 4 | (ucontrol->value.enumerated.item[0] - 1);
510         return ac97_update_bits_page(ac97, AC97_SIGMATEL_OUTSEL,
511                                      7 << shift, val << shift, 0);
512 }
513
514 static int snd_ac97_stac9758_input_jack_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
515 {
516         static char *texts[7] = { "Mic2 Jack", "Mic1 Jack", "Line In Jack",
517                 "Front Jack", "Rear Jack", "Center/LFE Jack", "Mute" };
518
519         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
520         uinfo->count = 1;
521         uinfo->value.enumerated.items = 7;
522         if (uinfo->value.enumerated.item > 6)
523                 uinfo->value.enumerated.item = 6;
524         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
525         return 0;
526 }
527
528 static int snd_ac97_stac9758_input_jack_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t* ucontrol)
529 {
530         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
531         int shift = kcontrol->private_value;
532         unsigned short val;
533
534         val = ac97->regs[AC97_SIGMATEL_INSEL];
535         ucontrol->value.enumerated.item[0] = (val >> shift) & 7;
536         return 0;
537 }
538
539 static int snd_ac97_stac9758_input_jack_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
540 {
541         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
542         int shift = kcontrol->private_value;
543
544         return ac97_update_bits_page(ac97, AC97_SIGMATEL_INSEL, 7 << shift,
545                                      ucontrol->value.enumerated.item[0] << shift, 0);
546 }
547
548 static int snd_ac97_stac9758_phonesel_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
549 {
550         static char *texts[3] = { "None", "Front Jack", "Rear Jack" };
551
552         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
553         uinfo->count = 1;
554         uinfo->value.enumerated.items = 3;
555         if (uinfo->value.enumerated.item > 2)
556                 uinfo->value.enumerated.item = 2;
557         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
558         return 0;
559 }
560
561 static int snd_ac97_stac9758_phonesel_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t* ucontrol)
562 {
563         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
564
565         ucontrol->value.enumerated.item[0] = ac97->regs[AC97_SIGMATEL_IOMISC] & 3;
566         return 0;
567 }
568
569 static int snd_ac97_stac9758_phonesel_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
570 {
571         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
572
573         return ac97_update_bits_page(ac97, AC97_SIGMATEL_IOMISC, 3,
574                                      ucontrol->value.enumerated.item[0], 0);
575 }
576
577 #define STAC9758_OUTPUT_JACK(xname, shift) \
578 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
579         .info = snd_ac97_stac9758_output_jack_info, \
580         .get = snd_ac97_stac9758_output_jack_get, \
581         .put = snd_ac97_stac9758_output_jack_put, \
582         .private_value = shift }
583 #define STAC9758_INPUT_JACK(xname, shift) \
584 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
585         .info = snd_ac97_stac9758_input_jack_info, \
586         .get = snd_ac97_stac9758_input_jack_get, \
587         .put = snd_ac97_stac9758_input_jack_put, \
588         .private_value = shift }
589 static const snd_kcontrol_new_t snd_ac97_sigmatel_stac9758_controls[] = {
590         STAC9758_OUTPUT_JACK("Mic1 Jack", 1),
591         STAC9758_OUTPUT_JACK("LineIn Jack", 4),
592         STAC9758_OUTPUT_JACK("Front Jack", 7),
593         STAC9758_OUTPUT_JACK("Rear Jack", 10),
594         STAC9758_OUTPUT_JACK("Center/LFE Jack", 13),
595         STAC9758_INPUT_JACK("Mic Input Source", 0),
596         STAC9758_INPUT_JACK("Line Input Source", 8),
597         {
598                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
599                 .name = "Headphone Amp",
600                 .info = snd_ac97_stac9758_phonesel_info,
601                 .get = snd_ac97_stac9758_phonesel_get,
602                 .put = snd_ac97_stac9758_phonesel_put
603         },
604         AC97_SINGLE("Exchange Center/LFE", AC97_SIGMATEL_IOMISC, 4, 1, 0),
605         AC97_SINGLE("Headphone +3dB Boost", AC97_SIGMATEL_IOMISC, 8, 1, 0)
606 };
607
608 static int patch_sigmatel_stac9758_specific(ac97_t *ac97)
609 {
610         int err;
611
612         err = patch_sigmatel_stac97xx_specific(ac97);
613         if (err < 0)
614                 return err;
615         err = patch_build_controls(ac97, snd_ac97_sigmatel_stac9758_controls,
616                                    ARRAY_SIZE(snd_ac97_sigmatel_stac9758_controls));
617         if (err < 0)
618                 return err;
619         /* DAC-A direct */
620         snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Front Playback");
621         /* DAC-A to Mix = PCM */
622         /* DAC-B direct = Surround */
623         /* DAC-B to Mix */
624         snd_ac97_rename_vol_ctl(ac97, "Video Playback", "Surround Mix Playback");
625         /* DAC-C direct = Center/LFE */
626
627         return 0;
628 }
629
630 static struct snd_ac97_build_ops patch_sigmatel_stac9758_ops = {
631         .build_3d       = patch_sigmatel_stac9700_3d,
632         .build_specific = patch_sigmatel_stac9758_specific
633 };
634
635 int patch_sigmatel_stac9758(ac97_t * ac97)
636 {
637         static unsigned short regs[4] = {
638                 AC97_SIGMATEL_OUTSEL,
639                 AC97_SIGMATEL_IOMISC,
640                 AC97_SIGMATEL_INSEL,
641                 AC97_SIGMATEL_VARIOUS
642         };
643         static unsigned short def_regs[4] = {
644                 /* OUTSEL */ 0xd794, /* CL:CL, SR:SR, LO:MX, LI:DS, MI:DS */
645                 /* IOMISC */ 0x2001,
646                 /* INSEL */ 0x0201, /* LI:LI, MI:M1 */
647                 /* VARIOUS */ 0x0040
648         };
649         static unsigned short m675_regs[4] = {
650                 /* OUTSEL */ 0xfc70, /* CL:MX, SR:MX, LO:DS, LI:MX, MI:DS */
651                 /* IOMISC */ 0x2102, /* HP amp on */
652                 /* INSEL */ 0x0203, /* LI:LI, MI:FR */
653                 /* VARIOUS */ 0x0041 /* stereo mic */
654         };
655         unsigned short *pregs = def_regs;
656         int i;
657
658         /* Gateway M675 notebook */
659         if (ac97->pci && 
660             ac97->subsystem_vendor == 0x107b &&
661             ac97->subsystem_device == 0x0601)
662                 pregs = m675_regs;
663
664         // patch for SigmaTel
665         ac97->build_ops = &patch_sigmatel_stac9758_ops;
666         /* FIXME: assume only page 0 for writing cache */
667         snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, AC97_PAGE_VENDOR);
668         for (i = 0; i < 4; i++)
669                 snd_ac97_write_cache(ac97, regs[i], pregs[i]);
670
671         ac97->flags |= AC97_STEREO_MUTES;
672         return 0;
673 }
674
675 /*
676  * Cirrus Logic CS42xx codecs
677  */
678 static const snd_kcontrol_new_t snd_ac97_cirrus_controls_spdif[2] = {
679         AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), AC97_CSR_SPDIF, 15, 1, 0),
680         AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "AC97-SPSA", AC97_CSR_ACMODE, 0, 3, 0)
681 };
682
683 static int patch_cirrus_build_spdif(ac97_t * ac97)
684 {
685         int err;
686
687         /* con mask, pro mask, default */
688         if ((err = patch_build_controls(ac97, &snd_ac97_controls_spdif[0], 3)) < 0)
689                 return err;
690         /* switch, spsa */
691         if ((err = patch_build_controls(ac97, &snd_ac97_cirrus_controls_spdif[0], 1)) < 0)
692                 return err;
693         switch (ac97->id & AC97_ID_CS_MASK) {
694         case AC97_ID_CS4205:
695                 if ((err = patch_build_controls(ac97, &snd_ac97_cirrus_controls_spdif[1], 1)) < 0)
696                         return err;
697                 break;
698         }
699         /* set default PCM S/PDIF params */
700         /* consumer,PCM audio,no copyright,no preemphasis,PCM coder,original,48000Hz */
701         snd_ac97_write_cache(ac97, AC97_CSR_SPDIF, 0x0a20);
702         return 0;
703 }
704
705 static struct snd_ac97_build_ops patch_cirrus_ops = {
706         .build_spdif = patch_cirrus_build_spdif
707 };
708
709 int patch_cirrus_spdif(ac97_t * ac97)
710 {
711         /* Basically, the cs4201/cs4205/cs4297a has non-standard sp/dif registers.
712            WHY CAN'T ANYONE FOLLOW THE BLOODY SPEC?  *sigh*
713            - sp/dif EA ID is not set, but sp/dif is always present.
714            - enable/disable is spdif register bit 15.
715            - sp/dif control register is 0x68.  differs from AC97:
716            - valid is bit 14 (vs 15)
717            - no DRS
718            - only 44.1/48k [00 = 48, 01=44,1] (AC97 is 00=44.1, 10=48)
719            - sp/dif ssource select is in 0x5e bits 0,1.
720         */
721
722         ac97->build_ops = &patch_cirrus_ops;
723         ac97->flags |= AC97_CS_SPDIF; 
724         ac97->rates[AC97_RATES_SPDIF] &= ~SNDRV_PCM_RATE_32000;
725         ac97->ext_id |= AC97_EI_SPDIF;  /* force the detection of spdif */
726         snd_ac97_write_cache(ac97, AC97_CSR_ACMODE, 0x0080);
727         return 0;
728 }
729
730 int patch_cirrus_cs4299(ac97_t * ac97)
731 {
732         /* force the detection of PC Beep */
733         ac97->flags |= AC97_HAS_PC_BEEP;
734         
735         return patch_cirrus_spdif(ac97);
736 }
737
738 /*
739  * Conexant codecs
740  */
741 static const snd_kcontrol_new_t snd_ac97_conexant_controls_spdif[1] = {
742         AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH), AC97_CXR_AUDIO_MISC, 3, 1, 0),
743 };
744
745 static int patch_conexant_build_spdif(ac97_t * ac97)
746 {
747         int err;
748
749         /* con mask, pro mask, default */
750         if ((err = patch_build_controls(ac97, &snd_ac97_controls_spdif[0], 3)) < 0)
751                 return err;
752         /* switch */
753         if ((err = patch_build_controls(ac97, &snd_ac97_conexant_controls_spdif[0], 1)) < 0)
754                 return err;
755         /* set default PCM S/PDIF params */
756         /* consumer,PCM audio,no copyright,no preemphasis,PCM coder,original,48000Hz */
757         snd_ac97_write_cache(ac97, AC97_CXR_AUDIO_MISC,
758                              snd_ac97_read(ac97, AC97_CXR_AUDIO_MISC) & ~(AC97_CXR_SPDIFEN|AC97_CXR_COPYRGT|AC97_CXR_SPDIF_MASK));
759         return 0;
760 }
761
762 static struct snd_ac97_build_ops patch_conexant_ops = {
763         .build_spdif = patch_conexant_build_spdif
764 };
765
766 int patch_conexant(ac97_t * ac97)
767 {
768         ac97->build_ops = &patch_conexant_ops;
769         ac97->flags |= AC97_CX_SPDIF;
770         ac97->ext_id |= AC97_EI_SPDIF;  /* force the detection of spdif */
771         ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000; /* 48k only */
772         return 0;
773 }
774
775 /*
776  * Analog Device AD18xx, AD19xx codecs
777  */
778 int patch_ad1819(ac97_t * ac97)
779 {
780         unsigned short scfg;
781
782         // patch for Analog Devices
783         scfg = snd_ac97_read(ac97, AC97_AD_SERIAL_CFG);
784         snd_ac97_write_cache(ac97, AC97_AD_SERIAL_CFG, scfg | 0x7000); /* select all codecs */
785         return 0;
786 }
787
788 static unsigned short patch_ad1881_unchained(ac97_t * ac97, int idx, unsigned short mask)
789 {
790         unsigned short val;
791
792         // test for unchained codec
793         snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, mask);
794         snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, 0x0000);  /* ID0C, ID1C, SDIE = off */
795         val = snd_ac97_read(ac97, AC97_VENDOR_ID2);
796         if ((val & 0xff40) != 0x5340)
797                 return 0;
798         ac97->spec.ad18xx.unchained[idx] = mask;
799         ac97->spec.ad18xx.id[idx] = val;
800         ac97->spec.ad18xx.codec_cfg[idx] = 0x0000;
801         return mask;
802 }
803
804 static int patch_ad1881_chained1(ac97_t * ac97, int idx, unsigned short codec_bits)
805 {
806         static int cfg_bits[3] = { 1<<12, 1<<14, 1<<13 };
807         unsigned short val;
808         
809         snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, cfg_bits[idx]);
810         snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, 0x0004);  // SDIE
811         val = snd_ac97_read(ac97, AC97_VENDOR_ID2);
812         if ((val & 0xff40) != 0x5340)
813                 return 0;
814         if (codec_bits)
815                 snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, codec_bits);
816         ac97->spec.ad18xx.chained[idx] = cfg_bits[idx];
817         ac97->spec.ad18xx.id[idx] = val;
818         ac97->spec.ad18xx.codec_cfg[idx] = codec_bits ? codec_bits : 0x0004;
819         return 1;
820 }
821
822 static void patch_ad1881_chained(ac97_t * ac97, int unchained_idx, int cidx1, int cidx2)
823 {
824         // already detected?
825         if (ac97->spec.ad18xx.unchained[cidx1] || ac97->spec.ad18xx.chained[cidx1])
826                 cidx1 = -1;
827         if (ac97->spec.ad18xx.unchained[cidx2] || ac97->spec.ad18xx.chained[cidx2])
828                 cidx2 = -1;
829         if (cidx1 < 0 && cidx2 < 0)
830                 return;
831         // test for chained codecs
832         snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000,
833                              ac97->spec.ad18xx.unchained[unchained_idx]);
834         snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, 0x0002);          // ID1C
835         ac97->spec.ad18xx.codec_cfg[unchained_idx] = 0x0002;
836         if (cidx1 >= 0) {
837                 if (patch_ad1881_chained1(ac97, cidx1, 0x0006))         // SDIE | ID1C
838                         patch_ad1881_chained1(ac97, cidx2, 0);
839                 else if (patch_ad1881_chained1(ac97, cidx2, 0x0006))    // SDIE | ID1C
840                         patch_ad1881_chained1(ac97, cidx1, 0);
841         } else if (cidx2 >= 0) {
842                 patch_ad1881_chained1(ac97, cidx2, 0);
843         }
844 }
845
846 int patch_ad1881(ac97_t * ac97)
847 {
848         static const char cfg_idxs[3][2] = {
849                 {2, 1},
850                 {0, 2},
851                 {0, 1}
852         };
853         
854         // patch for Analog Devices
855         unsigned short codecs[3];
856         unsigned short val;
857         int idx, num;
858
859         val = snd_ac97_read(ac97, AC97_AD_SERIAL_CFG);
860         snd_ac97_write_cache(ac97, AC97_AD_SERIAL_CFG, val);
861         codecs[0] = patch_ad1881_unchained(ac97, 0, (1<<12));
862         codecs[1] = patch_ad1881_unchained(ac97, 1, (1<<14));
863         codecs[2] = patch_ad1881_unchained(ac97, 2, (1<<13));
864
865         snd_runtime_check(codecs[0] | codecs[1] | codecs[2], goto __end);
866
867         for (idx = 0; idx < 3; idx++)
868                 if (ac97->spec.ad18xx.unchained[idx])
869                         patch_ad1881_chained(ac97, idx, cfg_idxs[idx][0], cfg_idxs[idx][1]);
870
871         if (ac97->spec.ad18xx.id[1]) {
872                 ac97->flags |= AC97_AD_MULTI;
873                 ac97->scaps |= AC97_SCAP_SURROUND_DAC;
874         }
875         if (ac97->spec.ad18xx.id[2]) {
876                 ac97->flags |= AC97_AD_MULTI;
877                 ac97->scaps |= AC97_SCAP_CENTER_LFE_DAC;
878         }
879
880       __end:
881         /* select all codecs */
882         snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, 0x7000);
883         /* check if only one codec is present */
884         for (idx = num = 0; idx < 3; idx++)
885                 if (ac97->spec.ad18xx.id[idx])
886                         num++;
887         if (num == 1) {
888                 /* ok, deselect all ID bits */
889                 snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, 0x0000);
890                 ac97->spec.ad18xx.codec_cfg[0] = 
891                         ac97->spec.ad18xx.codec_cfg[1] = 
892                         ac97->spec.ad18xx.codec_cfg[2] = 0x0000;
893         }
894         /* required for AD1886/AD1885 combination */
895         ac97->ext_id = snd_ac97_read(ac97, AC97_EXTENDED_ID);
896         if (ac97->spec.ad18xx.id[0]) {
897                 ac97->id &= 0xffff0000;
898                 ac97->id |= ac97->spec.ad18xx.id[0];
899         }
900         return 0;
901 }
902
903 static const snd_kcontrol_new_t snd_ac97_controls_ad1885[] = {
904         AC97_SINGLE("Digital Mono Direct", AC97_AD_MISC, 11, 1, 0),
905         /* AC97_SINGLE("Digital Audio Mode", AC97_AD_MISC, 12, 1, 0), */ /* seems problematic */
906         AC97_SINGLE("Low Power Mixer", AC97_AD_MISC, 14, 1, 0),
907         AC97_SINGLE("Zero Fill DAC", AC97_AD_MISC, 15, 1, 0),
908         AC97_SINGLE("Headphone Jack Sense", AC97_AD_JACK_SPDIF, 9, 1, 1), /* inverted */
909         AC97_SINGLE("Line Jack Sense", AC97_AD_JACK_SPDIF, 8, 1, 1), /* inverted */
910 };
911
912 static int patch_ad1885_specific(ac97_t * ac97)
913 {
914         int err;
915
916         if ((err = patch_build_controls(ac97, snd_ac97_controls_ad1885, ARRAY_SIZE(snd_ac97_controls_ad1885))) < 0)
917                 return err;
918         return 0;
919 }
920
921 static struct snd_ac97_build_ops patch_ad1885_build_ops = {
922         .build_specific = &patch_ad1885_specific
923 };
924
925 int patch_ad1885(ac97_t * ac97)
926 {
927         patch_ad1881(ac97);
928         /* This is required to deal with the Intel D815EEAL2 */
929         /* i.e. Line out is actually headphone out from codec */
930
931         /* set default */
932         snd_ac97_write_cache(ac97, AC97_AD_MISC, 0x0404);
933
934         ac97->build_ops = &patch_ad1885_build_ops;
935         return 0;
936 }
937
938 int patch_ad1886(ac97_t * ac97)
939 {
940         patch_ad1881(ac97);
941         /* Presario700 workaround */
942         /* for Jack Sense/SPDIF Register misetting causing */
943         snd_ac97_write_cache(ac97, AC97_AD_JACK_SPDIF, 0x0010);
944         return 0;
945 }
946
947 /* MISC bits */
948 #define AC97_AD198X_MBC         0x0003  /* mic boost */
949 #define AC97_AD198X_MBC_20      0x0000  /* +20dB */
950 #define AC97_AD198X_MBC_10      0x0001  /* +10dB */
951 #define AC97_AD198X_MBC_30      0x0002  /* +30dB */
952 #define AC97_AD198X_VREFD       0x0004  /* VREF high-Z */
953 #define AC97_AD198X_VREFH       0x0008  /* 2.25V, 3.7V */
954 #define AC97_AD198X_VREF_0      0x000c  /* 0V */
955 #define AC97_AD198X_SRU         0x0010  /* sample rate unlock */
956 #define AC97_AD198X_LOSEL       0x0020  /* LINE_OUT amplifiers input select */
957 #define AC97_AD198X_2MIC        0x0040  /* 2-channel mic select */
958 #define AC97_AD198X_SPRD        0x0080  /* SPREAD enable */
959 #define AC97_AD198X_DMIX0       0x0100  /* downmix mode: 0 = 6-to-4, 1 = 6-to-2 downmix */
960 #define AC97_AD198X_DMIX1       0x0200  /* downmix mode: 1 = enabled */
961 #define AC97_AD198X_HPSEL       0x0400  /* headphone amplifier input select */
962 #define AC97_AD198X_CLDIS       0x0800  /* center/lfe disable */
963 #define AC97_AD198X_LODIS       0x1000  /* LINE_OUT disable */
964 #define AC97_AD198X_MSPLT       0x2000  /* mute split */
965 #define AC97_AD198X_AC97NC      0x4000  /* AC97 no compatible mode */
966 #define AC97_AD198X_DACZ        0x8000  /* DAC zero-fill mode */
967
968
969 static int snd_ac97_ad198x_spdif_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
970 {
971         static char *texts[2] = { "AC-Link", "A/D Converter" };
972
973         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
974         uinfo->count = 1;
975         uinfo->value.enumerated.items = 2;
976         if (uinfo->value.enumerated.item > 1)
977                 uinfo->value.enumerated.item = 1;
978         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
979         return 0;
980 }
981
982 static int snd_ac97_ad198x_spdif_source_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
983 {
984         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
985         unsigned short val;
986
987         val = ac97->regs[AC97_AD_SERIAL_CFG];
988         ucontrol->value.enumerated.item[0] = (val >> 2) & 1;
989         return 0;
990 }
991
992 static int snd_ac97_ad198x_spdif_source_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
993 {
994         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
995         unsigned short val;
996
997         if (ucontrol->value.enumerated.item[0] > 1)
998                 return -EINVAL;
999         val = ucontrol->value.enumerated.item[0] << 2;
1000         return snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x0004, val);
1001 }
1002
1003 static const snd_kcontrol_new_t snd_ac97_ad198x_spdif_source = {
1004         .iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
1005         .name   = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1006         .info   = snd_ac97_ad198x_spdif_source_info,
1007         .get    = snd_ac97_ad198x_spdif_source_get,
1008         .put    = snd_ac97_ad198x_spdif_source_put,
1009 };
1010
1011 static int patch_ad198x_post_spdif(ac97_t * ac97)
1012 {
1013         return patch_build_controls(ac97, &snd_ac97_ad198x_spdif_source, 1);
1014 }
1015
1016 static struct snd_ac97_build_ops patch_ad1981a_build_ops = {
1017         .build_post_spdif = patch_ad198x_post_spdif
1018 };
1019
1020 int patch_ad1981a(ac97_t *ac97)
1021 {
1022         patch_ad1881(ac97);
1023         ac97->build_ops = &patch_ad1981a_build_ops;
1024         snd_ac97_update_bits(ac97, AC97_AD_MISC, AC97_AD198X_MSPLT, AC97_AD198X_MSPLT);
1025         ac97->flags |= AC97_STEREO_MUTES;
1026         return 0;
1027 }
1028
1029 static const snd_kcontrol_new_t snd_ac97_ad198x_2cmic =
1030 AC97_SINGLE("Stereo Mic", AC97_AD_MISC, 6, 1, 0);
1031
1032 static int patch_ad1981b_specific(ac97_t *ac97)
1033 {
1034         return patch_build_controls(ac97, &snd_ac97_ad198x_2cmic, 1);
1035 }
1036
1037 static struct snd_ac97_build_ops patch_ad1981b_build_ops = {
1038         .build_post_spdif = patch_ad198x_post_spdif,
1039         .build_specific = patch_ad1981b_specific
1040 };
1041
1042 int patch_ad1981b(ac97_t *ac97)
1043 {
1044         patch_ad1881(ac97);
1045         ac97->build_ops = &patch_ad1981b_build_ops;
1046         snd_ac97_update_bits(ac97, AC97_AD_MISC, AC97_AD198X_MSPLT, AC97_AD198X_MSPLT);
1047         ac97->flags |= AC97_STEREO_MUTES;
1048         return 0;
1049 }
1050
1051 static int snd_ac97_ad1888_lohpsel_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1052 {
1053         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1054         uinfo->count = 1;
1055         uinfo->value.integer.min = 0;
1056         uinfo->value.integer.max = 1;
1057         return 0;
1058 }
1059
1060 static int snd_ac97_ad1888_lohpsel_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t* ucontrol)
1061 {
1062         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1063         unsigned short val;
1064
1065         val = ac97->regs[AC97_AD_MISC];
1066         ucontrol->value.integer.value[0] = !(val & AC97_AD198X_LOSEL);
1067         return 0;
1068 }
1069
1070 static int snd_ac97_ad1888_lohpsel_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1071 {
1072         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1073         unsigned short val;
1074
1075         val = !ucontrol->value.integer.value[0]
1076                 ? (AC97_AD198X_LOSEL | AC97_AD198X_HPSEL) : 0;
1077         return snd_ac97_update_bits(ac97, AC97_AD_MISC,
1078                                     AC97_AD198X_LOSEL | AC97_AD198X_HPSEL, val);
1079 }
1080
1081 static int snd_ac97_ad1888_downmix_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1082 {
1083         static char *texts[3] = {"Off", "6 -> 4", "6 -> 2"};
1084
1085         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1086         uinfo->count = 1;
1087         uinfo->value.enumerated.items = 3;
1088         if (uinfo->value.enumerated.item > 2)
1089                 uinfo->value.enumerated.item = 2;
1090         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1091         return 0;
1092 }
1093
1094 static int snd_ac97_ad1888_downmix_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t* ucontrol)
1095 {
1096         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1097         unsigned short val;
1098
1099         val = ac97->regs[AC97_AD_MISC];
1100         if (!(val & AC97_AD198X_DMIX1))
1101                 ucontrol->value.enumerated.item[0] = 0;
1102         else
1103                 ucontrol->value.enumerated.item[0] = 1 + ((val >> 8) & 1);
1104         return 0;
1105 }
1106
1107 static int snd_ac97_ad1888_downmix_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1108 {
1109         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1110         unsigned short val;
1111
1112         if (ucontrol->value.enumerated.item[0] > 2)
1113                 return -EINVAL;
1114         if (ucontrol->value.enumerated.item[0] == 0)
1115                 val = 0;
1116         else
1117                 val = AC97_AD198X_DMIX1 |
1118                         ((ucontrol->value.enumerated.item[0] - 1) << 8);
1119         return snd_ac97_update_bits(ac97, AC97_AD_MISC,
1120                                     AC97_AD198X_DMIX0 | AC97_AD198X_DMIX1, val);
1121 }
1122
1123 static const snd_kcontrol_new_t snd_ac97_ad1888_controls[] = {
1124         {
1125                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1126                 .name = "Exchange Front/Surround",
1127                 .info = snd_ac97_ad1888_lohpsel_info,
1128                 .get = snd_ac97_ad1888_lohpsel_get,
1129                 .put = snd_ac97_ad1888_lohpsel_put
1130         },
1131         AC97_SINGLE("Spread Front to Surround and Center/LFE", AC97_AD_MISC, 7, 1, 0),
1132         {
1133                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1134                 .name = "Downmix",
1135                 .info = snd_ac97_ad1888_downmix_info,
1136                 .get = snd_ac97_ad1888_downmix_get,
1137                 .put = snd_ac97_ad1888_downmix_put
1138         },
1139         AC97_SINGLE("Surround Jack as Input", AC97_AD_MISC, 12, 1, 0),
1140         AC97_SINGLE("Center/LFE Jack as Input", AC97_AD_MISC, 11, 1, 0),
1141 };
1142
1143 static int patch_ad1888_specific(ac97_t *ac97)
1144 {
1145         /* rename 0x04 as "Master" and 0x02 as "Master Surround" */
1146         snd_ac97_rename_vol_ctl(ac97, "Master Playback", "Master Surround Playback");
1147         snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback");
1148         return patch_build_controls(ac97, snd_ac97_ad1888_controls, ARRAY_SIZE(snd_ac97_ad1888_controls));
1149 }
1150
1151 static struct snd_ac97_build_ops patch_ad1888_build_ops = {
1152         .build_post_spdif = patch_ad198x_post_spdif,
1153         .build_specific = patch_ad1888_specific
1154 };
1155
1156 int patch_ad1888(ac97_t * ac97)
1157 {
1158         unsigned short misc;
1159         
1160         patch_ad1881(ac97);
1161         ac97->build_ops = &patch_ad1888_build_ops;
1162         /* Switch FRONT/SURROUND LINE-OUT/HP-OUT default connection */
1163         /* it seems that most vendors connect line-out connector to headphone out of AC'97 */
1164         /* AD-compatible mode */
1165         /* Stereo mutes enabled */
1166         misc = snd_ac97_read(ac97, AC97_AD_MISC);
1167         snd_ac97_write_cache(ac97, AC97_AD_MISC, misc |
1168                              AC97_AD198X_LOSEL |
1169                              AC97_AD198X_HPSEL |
1170                              AC97_AD198X_MSPLT |
1171                              AC97_AD198X_AC97NC);
1172         ac97->flags |= AC97_STEREO_MUTES;
1173         return 0;
1174 }
1175
1176 static int patch_ad1980_specific(ac97_t *ac97)
1177 {
1178         int err;
1179
1180         if ((err = patch_ad1888_specific(ac97)) < 0)
1181                 return err;
1182         return patch_build_controls(ac97, &snd_ac97_ad198x_2cmic, 1);
1183 }
1184
1185 static struct snd_ac97_build_ops patch_ad1980_build_ops = {
1186         .build_post_spdif = patch_ad198x_post_spdif,
1187         .build_specific = patch_ad1980_specific
1188 };
1189
1190 int patch_ad1980(ac97_t * ac97)
1191 {
1192         patch_ad1888(ac97);
1193         ac97->build_ops = &patch_ad1980_build_ops;
1194         return 0;
1195 }
1196
1197 static const snd_kcontrol_new_t snd_ac97_ad1985_controls[] = {
1198         AC97_SINGLE("Center/LFE Jack as Mic", AC97_AD_SERIAL_CFG, 9, 1, 0),
1199         AC97_SINGLE("Exchange Center/LFE", AC97_AD_SERIAL_CFG, 3, 1, 0)
1200 };
1201
1202 static int patch_ad1985_specific(ac97_t *ac97)
1203 {
1204         int err;
1205
1206         if ((err = patch_ad1980_specific(ac97)) < 0)
1207                 return err;
1208         return patch_build_controls(ac97, snd_ac97_ad1985_controls, ARRAY_SIZE(snd_ac97_ad1985_controls));
1209 }
1210
1211 static struct snd_ac97_build_ops patch_ad1985_build_ops = {
1212         .build_post_spdif = patch_ad198x_post_spdif,
1213         .build_specific = patch_ad1985_specific
1214 };
1215
1216 int patch_ad1985(ac97_t * ac97)
1217 {
1218         unsigned short misc;
1219         
1220         patch_ad1881(ac97);
1221         ac97->build_ops = &patch_ad1985_build_ops;
1222         misc = snd_ac97_read(ac97, AC97_AD_MISC);
1223         /* switch front/surround line-out/hp-out */
1224         /* center/LFE, mic in 3.75V mode */
1225         /* AD-compatible mode */
1226         /* Stereo mutes enabled */
1227         /* in accordance with ADI driver: misc | 0x5c28 */
1228         snd_ac97_write_cache(ac97, AC97_AD_MISC, misc |
1229                              AC97_AD198X_VREFH |
1230                              AC97_AD198X_LOSEL |
1231                              AC97_AD198X_HPSEL |
1232                              AC97_AD198X_CLDIS |
1233                              AC97_AD198X_LODIS |
1234                              AC97_AD198X_MSPLT |
1235                              AC97_AD198X_AC97NC);
1236         ac97->flags |= AC97_STEREO_MUTES;
1237         /* on AD1985 rev. 3, AC'97 revision bits are zero */
1238         ac97->ext_id = (ac97->ext_id & ~AC97_EI_REV_MASK) | AC97_EI_REV_23;
1239         return 0;
1240 }
1241
1242 /*
1243  * realtek ALC65x/850 codecs
1244  */
1245 static int snd_ac97_alc650_mic_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
1246 {
1247         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1248         ucontrol->value.integer.value[0] = (ac97->regs[AC97_ALC650_MULTICH] >> 10) & 1;
1249         return 0;
1250 }
1251
1252 static int snd_ac97_alc650_mic_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
1253 {
1254         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1255         int change, val;
1256         val = !!(snd_ac97_read(ac97, AC97_ALC650_MULTICH) & (1 << 10));
1257         change = (ucontrol->value.integer.value[0] != val);
1258         if (change) {
1259                 /* disable/enable vref */
1260                 snd_ac97_update_bits(ac97, AC97_ALC650_CLOCK, 1 << 12,
1261                                      ucontrol->value.integer.value[0] ? (1 << 12) : 0);
1262                 /* turn on/off center-on-mic */
1263                 snd_ac97_update_bits(ac97, AC97_ALC650_MULTICH, 1 << 10,
1264                                      ucontrol->value.integer.value[0] ? (1 << 10) : 0);
1265                 /* GPIO0 high for mic */
1266                 snd_ac97_update_bits(ac97, AC97_ALC650_GPIO_STATUS, 0x100,
1267                                      ucontrol->value.integer.value[0] ? 0 : 0x100);
1268         }
1269         return change;
1270 }
1271
1272 static const snd_kcontrol_new_t snd_ac97_controls_alc650[] = {
1273         AC97_SINGLE("Duplicate Front", AC97_ALC650_MULTICH, 0, 1, 0),
1274         AC97_SINGLE("Surround Down Mix", AC97_ALC650_MULTICH, 1, 1, 0),
1275         AC97_SINGLE("Center/LFE Down Mix", AC97_ALC650_MULTICH, 2, 1, 0),
1276         AC97_SINGLE("Exchange Center/LFE", AC97_ALC650_MULTICH, 3, 1, 0),
1277         /* 4: Analog Input To Surround */
1278         /* 5: Analog Input To Center/LFE */
1279         /* 6: Independent Master Volume Right */
1280         /* 7: Independent Master Volume Left */
1281         /* 8: reserved */
1282         AC97_SINGLE("Line-In As Surround", AC97_ALC650_MULTICH, 9, 1, 0),
1283         /* 10: mic, see below */
1284         /* 11-13: in IEC958 controls */
1285         AC97_SINGLE("Swap Surround Slot", AC97_ALC650_MULTICH, 14, 1, 0),
1286 #if 0 /* always set in patch_alc650 */
1287         AC97_SINGLE("IEC958 Input Clock Enable", AC97_ALC650_CLOCK, 0, 1, 0),
1288         AC97_SINGLE("IEC958 Input Pin Enable", AC97_ALC650_CLOCK, 1, 1, 0),
1289         AC97_SINGLE("Surround DAC Switch", AC97_ALC650_SURR_DAC_VOL, 15, 1, 1),
1290         AC97_DOUBLE("Surround DAC Volume", AC97_ALC650_SURR_DAC_VOL, 8, 0, 31, 1),
1291         AC97_SINGLE("Center/LFE DAC Switch", AC97_ALC650_LFE_DAC_VOL, 15, 1, 1),
1292         AC97_DOUBLE("Center/LFE DAC Volume", AC97_ALC650_LFE_DAC_VOL, 8, 0, 31, 1),
1293 #endif
1294         {
1295                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1296                 .name = "Mic As Center/LFE",
1297                 .info = snd_ac97_info_single,
1298                 .get = snd_ac97_alc650_mic_get,
1299                 .put = snd_ac97_alc650_mic_put,
1300                 .private_value = AC97_SINGLE_VALUE(0, 0, 1, 0) /* only mask needed */
1301         },
1302 };
1303
1304 static const snd_kcontrol_new_t snd_ac97_spdif_controls_alc650[] = {
1305         AC97_SINGLE("IEC958 Capture Switch", AC97_ALC650_MULTICH, 11, 1, 0),
1306         AC97_SINGLE("Analog to IEC958 Output", AC97_ALC650_MULTICH, 12, 1, 0),
1307         AC97_SINGLE("IEC958 Input Monitor", AC97_ALC650_MULTICH, 13, 1, 0),
1308 };
1309
1310 static int patch_alc650_specific(ac97_t * ac97)
1311 {
1312         int err;
1313
1314         if ((err = patch_build_controls(ac97, snd_ac97_controls_alc650, ARRAY_SIZE(snd_ac97_controls_alc650))) < 0)
1315                 return err;
1316         if (ac97->ext_id & AC97_EI_SPDIF) {
1317                 if ((err = patch_build_controls(ac97, snd_ac97_spdif_controls_alc650, ARRAY_SIZE(snd_ac97_spdif_controls_alc650))) < 0)
1318                         return err;
1319         }
1320         return 0;
1321 }
1322
1323 static struct snd_ac97_build_ops patch_alc650_ops = {
1324         .build_specific = patch_alc650_specific
1325 };
1326
1327 int patch_alc650(ac97_t * ac97)
1328 {
1329         unsigned short val;
1330
1331         ac97->build_ops = &patch_alc650_ops;
1332
1333         /* determine the revision */
1334         val = snd_ac97_read(ac97, AC97_ALC650_REVISION) & 0x3f;
1335         if (val < 3)
1336                 ac97->id = 0x414c4720;          /* Old version */
1337         else if (val < 0x10)
1338                 ac97->id = 0x414c4721;          /* D version */
1339         else if (val < 0x20)
1340                 ac97->id = 0x414c4722;          /* E version */
1341         else if (val < 0x30)
1342                 ac97->id = 0x414c4723;          /* F version */
1343
1344         /* revision E or F */
1345         /* FIXME: what about revision D ? */
1346         ac97->spec.dev_flags = (ac97->id == 0x414c4722 ||
1347                                 ac97->id == 0x414c4723);
1348
1349         /* enable AC97_ALC650_GPIO_SETUP, AC97_ALC650_CLOCK for R/W */
1350         snd_ac97_write_cache(ac97, AC97_ALC650_GPIO_STATUS, 
1351                 snd_ac97_read(ac97, AC97_ALC650_GPIO_STATUS) | 0x8000);
1352
1353         /* Enable SPDIF-IN only on Rev.E and above */
1354         val = snd_ac97_read(ac97, AC97_ALC650_CLOCK);
1355         /* SPDIF IN with pin 47 */
1356         if (ac97->spec.dev_flags)
1357                 val |= 0x03; /* enable */
1358         else
1359                 val &= ~0x03; /* disable */
1360         snd_ac97_write_cache(ac97, AC97_ALC650_CLOCK, val);
1361
1362         /* set default: slot 3,4,7,8,6,9
1363            spdif-in monitor off, analog-spdif off, spdif-in off
1364            center on mic off, surround on line-in off
1365            downmix off, duplicate front off
1366         */
1367         snd_ac97_write_cache(ac97, AC97_ALC650_MULTICH, 0);
1368
1369         /* set GPIO0 for mic bias */
1370         /* GPIO0 pin output, no interrupt, high */
1371         snd_ac97_write_cache(ac97, AC97_ALC650_GPIO_SETUP,
1372                              snd_ac97_read(ac97, AC97_ALC650_GPIO_SETUP) | 0x01);
1373         snd_ac97_write_cache(ac97, AC97_ALC650_GPIO_STATUS,
1374                              (snd_ac97_read(ac97, AC97_ALC650_GPIO_STATUS) | 0x100) & ~0x10);
1375
1376         /* full DAC volume */
1377         snd_ac97_write_cache(ac97, AC97_ALC650_SURR_DAC_VOL, 0x0808);
1378         snd_ac97_write_cache(ac97, AC97_ALC650_LFE_DAC_VOL, 0x0808);
1379         return 0;
1380 }
1381
1382 static int snd_ac97_alc655_mic_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
1383 {
1384         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1385         ucontrol->value.integer.value[0] = (ac97->regs[AC97_ALC650_MULTICH] >> 10) & 1;
1386         return 0;
1387 }
1388
1389 static int snd_ac97_alc655_mic_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
1390 {
1391         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1392
1393         /* misc control; vrefout disable */
1394         snd_ac97_update_bits(ac97, AC97_ALC650_CLOCK, 1 << 12,
1395                              ucontrol->value.integer.value[0] ? (1 << 12) : 0);
1396         return ac97_update_bits_page(ac97, AC97_ALC650_MULTICH, 1 << 10,
1397                                      ucontrol->value.integer.value[0] ? (1 << 10) : 0,
1398                                      0);
1399 }
1400
1401
1402 static const snd_kcontrol_new_t snd_ac97_controls_alc655[] = {
1403         AC97_PAGE_SINGLE("Duplicate Front", AC97_ALC650_MULTICH, 0, 1, 0, 0),
1404         AC97_PAGE_SINGLE("Line-In As Surround", AC97_ALC650_MULTICH, 9, 1, 0, 0),
1405         {
1406                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1407                 .name = "Mic As Center/LFE",
1408                 .info = snd_ac97_info_single,
1409                 .get = snd_ac97_alc655_mic_get,
1410                 .put = snd_ac97_alc655_mic_put,
1411                 .private_value = AC97_SINGLE_VALUE(0, 0, 1, 0) /* only mask needed */
1412         },
1413 };
1414
1415 static int alc655_iec958_route_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1416 {
1417         static char *texts_655[3] = { "PCM", "Analog In", "IEC958 In" };
1418         static char *texts_658[4] = { "PCM", "Analog1 In", "Analog2 In", "IEC958 In" };
1419         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1420
1421         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1422         uinfo->count = 1;
1423         uinfo->value.enumerated.items = ac97->spec.dev_flags ? 4 : 3;
1424         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1425                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1426         strcpy(uinfo->value.enumerated.name,
1427                ac97->spec.dev_flags ?
1428                texts_658[uinfo->value.enumerated.item] :
1429                texts_655[uinfo->value.enumerated.item]);
1430         return 0;
1431 }
1432
1433 static int alc655_iec958_route_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1434 {
1435         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1436         unsigned short val;
1437
1438         val = ac97->regs[AC97_ALC650_MULTICH];
1439         val = (val >> 12) & 3;
1440         if (ac97->spec.dev_flags && val == 3)
1441                 val = 0;
1442         ucontrol->value.enumerated.item[0] = val;
1443         return 0;
1444 }
1445
1446 static int alc655_iec958_route_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1447 {
1448         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1449
1450         return ac97_update_bits_page(ac97, AC97_ALC650_MULTICH, 3 << 12,
1451                                      (unsigned short)ucontrol->value.enumerated.item[0],
1452                                      0);
1453 }
1454
1455 static const snd_kcontrol_new_t snd_ac97_spdif_controls_alc655[] = {
1456         AC97_PAGE_SINGLE("IEC958 Capture Switch", AC97_ALC650_MULTICH, 11, 1, 0, 0),
1457         AC97_PAGE_SINGLE("IEC958 Input Monitor", AC97_ALC650_MULTICH, 14, 1, 0, 0),
1458         {
1459                 .iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
1460                 .name   = "IEC958 Playback Route",
1461                 .info   = alc655_iec958_route_info,
1462                 .get    = alc655_iec958_route_get,
1463                 .put    = alc655_iec958_route_put,
1464         },
1465 };
1466
1467 static int patch_alc655_specific(ac97_t * ac97)
1468 {
1469         int err;
1470
1471         if ((err = patch_build_controls(ac97, snd_ac97_controls_alc655, ARRAY_SIZE(snd_ac97_controls_alc655))) < 0)
1472                 return err;
1473         if (ac97->ext_id & AC97_EI_SPDIF) {
1474                 if ((err = patch_build_controls(ac97, snd_ac97_spdif_controls_alc655, ARRAY_SIZE(snd_ac97_spdif_controls_alc655))) < 0)
1475                         return err;
1476         }
1477         return 0;
1478 }
1479
1480 static struct snd_ac97_build_ops patch_alc655_ops = {
1481         .build_specific = patch_alc655_specific
1482 };
1483
1484 int patch_alc655(ac97_t * ac97)
1485 {
1486         unsigned int val;
1487
1488         ac97->spec.dev_flags = (ac97->id == 0x414c4780); /* ALC658 */
1489
1490         ac97->build_ops = &patch_alc655_ops;
1491
1492         /* assume only page 0 for writing cache */
1493         snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, AC97_PAGE_VENDOR);
1494
1495         /* adjust default values */
1496         val = snd_ac97_read(ac97, 0x7a); /* misc control */
1497         val |= (1 << 1); /* spdif input pin */
1498         val &= ~(1 << 12); /* vref enable */
1499         snd_ac97_write_cache(ac97, 0x7a, val);
1500         /* set default: spdif-in enabled,
1501            spdif-in monitor off, spdif-in PCM off
1502            center on mic off, surround on line-in off
1503            duplicate front off
1504         */
1505         snd_ac97_write_cache(ac97, AC97_ALC650_MULTICH, 1<<15);
1506
1507         /* full DAC volume */
1508         snd_ac97_write_cache(ac97, AC97_ALC650_SURR_DAC_VOL, 0x0808);
1509         snd_ac97_write_cache(ac97, AC97_ALC650_LFE_DAC_VOL, 0x0808);
1510         return 0;
1511 }
1512
1513
1514 #define AC97_ALC850_JACK_SELECT 0x76
1515 #define AC97_ALC850_MISC1       0x7a
1516
1517 static int ac97_alc850_surround_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
1518 {
1519         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1520         ucontrol->value.integer.value[0] = ((ac97->regs[AC97_ALC850_JACK_SELECT] >> 12) & 7) == 2;
1521         return 0;
1522 }
1523
1524 static int ac97_alc850_surround_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
1525 {
1526         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1527
1528         /* SURR 1kOhm (bit4), Amp (bit5) */
1529         snd_ac97_update_bits(ac97, AC97_ALC850_MISC1, (1<<4)|(1<<5),
1530                              ucontrol->value.integer.value[0] ? (1<<5) : (1<<4));
1531         /* LINE-IN = 0, SURROUND = 2 */
1532         return snd_ac97_update_bits(ac97, AC97_ALC850_JACK_SELECT, 7 << 12,
1533                                     ucontrol->value.integer.value[0] ? (2<<12) : (0<<12));
1534 }
1535
1536 static int ac97_alc850_mic_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
1537 {
1538         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1539         ucontrol->value.integer.value[0] = ((ac97->regs[AC97_ALC850_JACK_SELECT] >> 4) & 7) == 2;
1540         return 0;
1541 }
1542
1543 static int ac97_alc850_mic_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
1544 {
1545         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1546
1547         /* Vref disable (bit12), 1kOhm (bit13) */
1548         snd_ac97_update_bits(ac97, AC97_ALC850_MISC1, (1<<12)|(1<<13),
1549                              ucontrol->value.integer.value[0] ? (1<<12) : (1<<13));
1550         /* MIC-IN = 1, CENTER-LFE = 2 */
1551         return snd_ac97_update_bits(ac97, AC97_ALC850_JACK_SELECT, 7 << 4,
1552                                     ucontrol->value.integer.value[0] ? (2<<4) : (1<<4));
1553 }
1554
1555 static const snd_kcontrol_new_t snd_ac97_controls_alc850[] = {
1556         AC97_PAGE_SINGLE("Duplicate Front", AC97_ALC650_MULTICH, 0, 1, 0, 0),
1557         {
1558                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1559                 .name = "Line-In As Surround",
1560                 .info = snd_ac97_info_single,
1561                 .get = ac97_alc850_surround_get,
1562                 .put = ac97_alc850_surround_put,
1563                 .private_value = AC97_SINGLE_VALUE(0, 0, 1, 0) /* only mask needed */
1564         },
1565         {
1566                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1567                 .name = "Mic As Center/LFE",
1568                 .info = snd_ac97_info_single,
1569                 .get = ac97_alc850_mic_get,
1570                 .put = ac97_alc850_mic_put,
1571                 .private_value = AC97_SINGLE_VALUE(0, 0, 1, 0) /* only mask needed */
1572         },
1573
1574 };
1575
1576 static int patch_alc850_specific(ac97_t *ac97)
1577 {
1578         int err;
1579
1580         if ((err = patch_build_controls(ac97, snd_ac97_controls_alc850, ARRAY_SIZE(snd_ac97_controls_alc850))) < 0)
1581                 return err;
1582         if (ac97->ext_id & AC97_EI_SPDIF) {
1583                 if ((err = patch_build_controls(ac97, snd_ac97_spdif_controls_alc655, ARRAY_SIZE(snd_ac97_spdif_controls_alc655))) < 0)
1584                         return err;
1585         }
1586         return 0;
1587 }
1588
1589 static struct snd_ac97_build_ops patch_alc850_ops = {
1590         .build_specific = patch_alc850_specific
1591 };
1592
1593 int patch_alc850(ac97_t *ac97)
1594 {
1595         ac97->build_ops = &patch_alc850_ops;
1596
1597         ac97->spec.dev_flags = 0; /* for IEC958 playback route - ALC655 compatible */
1598
1599         /* assume only page 0 for writing cache */
1600         snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, AC97_PAGE_VENDOR);
1601
1602         /* adjust default values */
1603         /* set default: spdif-in enabled,
1604            spdif-in monitor off, spdif-in PCM off
1605            center on mic off, surround on line-in off
1606            duplicate front off
1607         */
1608         snd_ac97_write_cache(ac97, AC97_ALC650_MULTICH, 1<<15);
1609         /* SURR_OUT: on, Surr 1kOhm: on, Surr Amp: off, Front 1kOhm: off
1610          * Front Amp: on, Vref: enable, Center 1kOhm: on, Mix: on
1611          */
1612         snd_ac97_write_cache(ac97, 0x7a, (1<<1)|(1<<4)|(0<<5)|(1<<6)|
1613                              (1<<7)|(0<<12)|(1<<13)|(0<<14));
1614         /* detection UIO2,3: all path floating, UIO3: MIC, Vref2: disable,
1615          * UIO1: FRONT, Vref3: disable, UIO3: LINE, Front-Mic: mute
1616          */
1617         snd_ac97_write_cache(ac97, 0x76, (0<<0)|(0<<2)|(1<<4)|(1<<7)|(2<<8)|
1618                              (1<<11)|(0<<12)|(1<<15));
1619
1620         /* full DAC volume */
1621         snd_ac97_write_cache(ac97, AC97_ALC650_SURR_DAC_VOL, 0x0808);
1622         snd_ac97_write_cache(ac97, AC97_ALC650_LFE_DAC_VOL, 0x0808);
1623         return 0;
1624 }
1625
1626
1627 /*
1628  * C-Media CM97xx codecs
1629  */
1630 static const snd_kcontrol_new_t snd_ac97_cm9738_controls[] = {
1631         AC97_SINGLE("Line-In As Surround", AC97_CM9738_VENDOR_CTRL, 10, 1, 0),
1632         AC97_SINGLE("Duplicate Front", AC97_CM9738_VENDOR_CTRL, 13, 1, 0),
1633 };
1634
1635 static int patch_cm9738_specific(ac97_t * ac97)
1636 {
1637         return patch_build_controls(ac97, snd_ac97_cm9738_controls, ARRAY_SIZE(snd_ac97_cm9738_controls));
1638 }
1639
1640 static struct snd_ac97_build_ops patch_cm9738_ops = {
1641         .build_specific = patch_cm9738_specific
1642 };
1643
1644 int patch_cm9738(ac97_t * ac97)
1645 {
1646         ac97->build_ops = &patch_cm9738_ops;
1647         return 0;
1648 }
1649
1650 static int snd_ac97_cmedia_spdif_playback_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1651 {
1652         static char *texts[] = { "Analog", "Digital" };
1653
1654         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1655         uinfo->count = 1;
1656         uinfo->value.enumerated.items = 2;
1657         if (uinfo->value.enumerated.item > 1)
1658                 uinfo->value.enumerated.item = 1;
1659         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1660         return 0;
1661 }
1662
1663 static int snd_ac97_cmedia_spdif_playback_source_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1664 {
1665         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1666         unsigned short val;
1667
1668         val = ac97->regs[AC97_CM9739_SPDIF_CTRL];
1669         ucontrol->value.enumerated.item[0] = (val >> 1) & 0x01;
1670         return 0;
1671 }
1672
1673 static int snd_ac97_cmedia_spdif_playback_source_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1674 {
1675         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1676
1677         return snd_ac97_update_bits(ac97, AC97_CM9739_SPDIF_CTRL,
1678                                     0x01 << 1, 
1679                                     (ucontrol->value.enumerated.item[0] & 0x01) << 1);
1680 }
1681
1682 static const snd_kcontrol_new_t snd_ac97_cm9739_controls_spdif[] = {
1683         /* BIT 0: SPDI_EN - always true */
1684         { /* BIT 1: SPDIFS */
1685                 .iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
1686                 .name   = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1687                 .info   = snd_ac97_cmedia_spdif_playback_source_info,
1688                 .get    = snd_ac97_cmedia_spdif_playback_source_get,
1689                 .put    = snd_ac97_cmedia_spdif_playback_source_put,
1690         },
1691         /* BIT 2: IG_SPIV */
1692         AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,NONE) "Valid Switch", AC97_CM9739_SPDIF_CTRL, 2, 1, 0),
1693         /* BIT 3: SPI2F */
1694         AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,NONE) "Monitor", AC97_CM9739_SPDIF_CTRL, 3, 1, 0), 
1695         /* BIT 4: SPI2SDI */
1696         AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), AC97_CM9739_SPDIF_CTRL, 4, 1, 0),
1697         /* BIT 8: SPD32 - 32bit SPDIF - not supported yet */
1698 };
1699
1700 static int snd_ac97_cm9739_center_mic_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1701 {
1702         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1703         if (ac97->regs[AC97_CM9739_MULTI_CHAN] & 0x1000)
1704                 ucontrol->value.integer.value[0] = 1;
1705         else
1706                 ucontrol->value.integer.value[0] = 0;
1707         return 0;
1708 }
1709
1710 static int snd_ac97_cm9739_center_mic_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1711 {
1712         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1713         return snd_ac97_update_bits(ac97, AC97_CM9739_MULTI_CHAN, 0x3000,
1714                                     ucontrol->value.integer.value[0] ? 
1715                                     0x1000 : 0x2000);
1716 }
1717
1718 static const snd_kcontrol_new_t snd_ac97_cm9739_controls[] = {
1719         AC97_SINGLE("Line-In As Surround", AC97_CM9739_MULTI_CHAN, 10, 1, 0),
1720         {
1721                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1722                 .name = "Mic As Center/LFE",
1723                 .info = snd_ac97_info_single,
1724                 .get = snd_ac97_cm9739_center_mic_get,
1725                 .put = snd_ac97_cm9739_center_mic_put,
1726                 .private_value = AC97_SINGLE_VALUE(0, 0, 1, 0) /* only mask needed */
1727         },
1728 };
1729
1730 static int patch_cm9739_specific(ac97_t * ac97)
1731 {
1732         return patch_build_controls(ac97, snd_ac97_cm9739_controls, ARRAY_SIZE(snd_ac97_cm9739_controls));
1733 }
1734
1735 static int patch_cm9739_post_spdif(ac97_t * ac97)
1736 {
1737         return patch_build_controls(ac97, snd_ac97_cm9739_controls_spdif, ARRAY_SIZE(snd_ac97_cm9739_controls_spdif));
1738 }
1739
1740 static struct snd_ac97_build_ops patch_cm9739_ops = {
1741         .build_specific = patch_cm9739_specific,
1742         .build_post_spdif = patch_cm9739_post_spdif
1743 };
1744
1745 int patch_cm9739(ac97_t * ac97)
1746 {
1747         unsigned short val;
1748
1749         ac97->build_ops = &patch_cm9739_ops;
1750
1751         /* check spdif */
1752         val = snd_ac97_read(ac97, AC97_EXTENDED_STATUS);
1753         if (val & AC97_EA_SPCV) {
1754                 /* enable spdif in */
1755                 snd_ac97_write_cache(ac97, AC97_CM9739_SPDIF_CTRL,
1756                                      snd_ac97_read(ac97, AC97_CM9739_SPDIF_CTRL) | 0x01);
1757                 ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000; /* 48k only */
1758         } else {
1759                 ac97->ext_id &= ~AC97_EI_SPDIF; /* disable extended-id */
1760                 ac97->rates[AC97_RATES_SPDIF] = 0;
1761         }
1762
1763         /* set-up multi channel */
1764         /* bit 14: 0 = SPDIF, 1 = EAPD */
1765         /* bit 13: enable internal vref output for mic */
1766         /* bit 12: disable center/lfe (swithable) */
1767         /* bit 10: disable surround/line (switchable) */
1768         /* bit 9: mix 2 surround off */
1769         /* bit 0: dB */
1770         val = (1 << 13);
1771         if (! (ac97->ext_id & AC97_EI_SPDIF))
1772                 val |= (1 << 14);
1773         snd_ac97_write_cache(ac97, AC97_CM9739_MULTI_CHAN, val);
1774
1775         /* FIXME: set up GPIO */
1776         snd_ac97_write_cache(ac97, 0x70, 0x0100);
1777         snd_ac97_write_cache(ac97, 0x72, 0x0020);
1778
1779         return 0;
1780 }
1781
1782 /*
1783  * VIA VT1616 codec
1784  */
1785 static const snd_kcontrol_new_t snd_ac97_controls_vt1616[] = {
1786 AC97_SINGLE("DC Offset removal", 0x5a, 10, 1, 0),
1787 AC97_SINGLE("Alternate Level to Surround Out", 0x5a, 15, 1, 0),
1788 AC97_SINGLE("Downmix LFE and Center to Front", 0x5a, 12, 1, 0),
1789 AC97_SINGLE("Downmix Surround to Front", 0x5a, 11, 1, 0),
1790 };
1791
1792 static int patch_vt1616_specific(ac97_t * ac97)
1793 {
1794         int err;
1795
1796         if (snd_ac97_try_bit(ac97, 0x5a, 9))
1797                 if ((err = patch_build_controls(ac97, &snd_ac97_controls_vt1616[0], 1)) < 0)
1798                         return err;
1799         if ((err = patch_build_controls(ac97, &snd_ac97_controls_vt1616[1], ARRAY_SIZE(snd_ac97_controls_vt1616) - 1)) < 0)
1800                 return err;
1801         return 0;
1802 }
1803
1804 static struct snd_ac97_build_ops patch_vt1616_ops = {
1805         .build_specific = patch_vt1616_specific
1806 };
1807
1808 int patch_vt1616(ac97_t * ac97)
1809 {
1810         ac97->build_ops = &patch_vt1616_ops;
1811         return 0;
1812 }
1813
1814 static const snd_kcontrol_new_t snd_ac97_controls_it2646[] = {
1815         AC97_SINGLE("Line-In As Surround", 0x76, 9, 1, 0),
1816         AC97_SINGLE("Mic As Center/LFE", 0x76, 10, 1, 0),
1817 };
1818
1819 static const snd_kcontrol_new_t snd_ac97_spdif_controls_it2646[] = {
1820         AC97_SINGLE("IEC958 Capture Switch", 0x76, 11, 1, 0),
1821         AC97_SINGLE("Analog to IEC958 Output", 0x76, 12, 1, 0),
1822         AC97_SINGLE("IEC958 Input Monitor", 0x76, 13, 1, 0),
1823 };
1824
1825 static int patch_it2646_specific(ac97_t * ac97)
1826 {
1827         int err;
1828         if ((err = patch_build_controls(ac97, snd_ac97_controls_it2646, ARRAY_SIZE(snd_ac97_controls_it2646))) < 0)
1829                 return err;
1830         if ((err = patch_build_controls(ac97, snd_ac97_spdif_controls_it2646, ARRAY_SIZE(snd_ac97_spdif_controls_it2646))) < 0)
1831                 return err;
1832         return 0;
1833 }
1834
1835 static struct snd_ac97_build_ops patch_it2646_ops = {
1836         .build_specific = patch_it2646_specific
1837 };
1838
1839 int patch_it2646(ac97_t * ac97)
1840 {
1841         ac97->build_ops = &patch_it2646_ops;
1842         /* full DAC volume */
1843         snd_ac97_write_cache(ac97, 0x5E, 0x0808);
1844         snd_ac97_write_cache(ac97, 0x7A, 0x0808);
1845         return 0;
1846 }
1847
1848 /* Si3036/8 specific registers */
1849 #define AC97_SI3036_CHIP_ID     0x5a
1850
1851 int mpatch_si3036(ac97_t * ac97)
1852 {
1853         //printk("mpatch_si3036: chip id = %x\n", snd_ac97_read(ac97, 0x5a));
1854         snd_ac97_write_cache(ac97, 0x5c, 0xf210 );
1855         snd_ac97_write_cache(ac97, 0x68, 0);
1856         return 0;
1857 }