vserver 1.9.5.x5
[linux-2.6.git] / sound / pci / ac97 / ac97_patch.c
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3  *  Universal interface for Audio Codec '97
4  *
5  *  For more details look to AC '97 component specification revision 2.2
6  *  by Intel Corporation (http://developer.intel.com) and to datasheets
7  *  for specific codecs.
8  *
9  *
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of the GNU General Public License as published by
12  *   the Free Software Foundation; either version 2 of the License, or
13  *   (at your option) any later version.
14  *
15  *   This program is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with this program; if not, write to the Free Software
22  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  *
24  */
25
26 #include <sound/driver.h>
27 #include <linux/delay.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <sound/core.h>
31 #include <sound/pcm.h>
32 #include <sound/control.h>
33 #include <sound/ac97_codec.h>
34 #include "ac97_patch.h"
35 #include "ac97_id.h"
36 #include "ac97_local.h"
37
38 /*
39  *  Chip specific initialization
40  */
41
42 static int patch_build_controls(ac97_t * ac97, const snd_kcontrol_new_t *controls, int count)
43 {
44         int idx, err;
45
46         for (idx = 0; idx < count; idx++)
47                 if ((err = snd_ctl_add(ac97->bus->card, snd_ac97_cnew(&controls[idx], ac97))) < 0)
48                         return err;
49         return 0;
50 }
51
52 /* set to the page, update bits and restore the page */
53 static int ac97_update_bits_page(ac97_t *ac97, unsigned short reg, unsigned short mask, unsigned short value, unsigned short page)
54 {
55         unsigned short page_save;
56         int ret;
57
58         down(&ac97->page_mutex);
59         page_save = snd_ac97_read(ac97, AC97_INT_PAGING) & AC97_PAGE_MASK;
60         snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page);
61         ret = snd_ac97_update_bits(ac97, reg, mask, value);
62         snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, page_save);
63         up(&ac97->page_mutex); /* unlock paging */
64         return ret;
65 }
66
67 /* 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 #ifdef CONFIG_PM
779 static void ad18xx_resume(ac97_t *ac97)
780 {
781         static unsigned short setup_regs[] = {
782                 AC97_AD_MISC, AC97_AD_SERIAL_CFG, AC97_AD_JACK_SPDIF,
783         };
784         int i, codec;
785
786         for (i = 0; i < (int)ARRAY_SIZE(setup_regs); i++) {
787                 unsigned short reg = setup_regs[i];
788                 if (test_bit(reg, ac97->reg_accessed)) {
789                         snd_ac97_write(ac97, reg, ac97->regs[reg]);
790                         snd_ac97_read(ac97, reg);
791                 }
792         }
793
794         if (! (ac97->flags & AC97_AD_MULTI))
795                 /* normal restore */
796                 snd_ac97_restore_status(ac97);
797         else {
798                 /* restore the AD18xx codec configurations */
799                 for (codec = 0; codec < 3; codec++) {
800                         if (! ac97->spec.ad18xx.id[codec])
801                                 continue;
802                         /* select single codec */
803                         snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000,
804                                              ac97->spec.ad18xx.unchained[codec] | ac97->spec.ad18xx.chained[codec]);
805                         ac97->bus->ops->write(ac97, AC97_AD_CODEC_CFG, ac97->spec.ad18xx.codec_cfg[codec]);
806                 }
807                 /* select all codecs */
808                 snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, 0x7000);
809
810                 /* restore status */
811                 for (i = 2; i < 0x7c ; i += 2) {
812                         if (i == AC97_POWERDOWN || i == AC97_EXTENDED_ID)
813                                 continue;
814                         if (test_bit(i, ac97->reg_accessed)) {
815                                 /* handle multi codecs for AD18xx */
816                                 if (i == AC97_PCM) {
817                                         for (codec = 0; codec < 3; codec++) {
818                                                 if (! ac97->spec.ad18xx.id[codec])
819                                                         continue;
820                                                 /* select single codec */
821                                                 snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000,
822                                                                      ac97->spec.ad18xx.unchained[codec] | ac97->spec.ad18xx.chained[codec]);
823                                                 /* update PCM bits */
824                                                 ac97->bus->ops->write(ac97, AC97_PCM, ac97->spec.ad18xx.pcmreg[codec]);
825                                         }
826                                         /* select all codecs */
827                                         snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, 0x7000);
828                                         continue;
829                                 } else if (i == AC97_AD_TEST ||
830                                            i == AC97_AD_CODEC_CFG ||
831                                            i == AC97_AD_SERIAL_CFG)
832                                         continue; /* ignore */
833                         }
834                         snd_ac97_write(ac97, i, ac97->regs[i]);
835                         snd_ac97_read(ac97, i);
836                 }
837         }
838
839         snd_ac97_restore_iec958(ac97);
840 }
841 #endif
842
843 int patch_ad1819(ac97_t * ac97)
844 {
845         unsigned short scfg;
846
847         // patch for Analog Devices
848         scfg = snd_ac97_read(ac97, AC97_AD_SERIAL_CFG);
849         snd_ac97_write_cache(ac97, AC97_AD_SERIAL_CFG, scfg | 0x7000); /* select all codecs */
850         return 0;
851 }
852
853 static unsigned short patch_ad1881_unchained(ac97_t * ac97, int idx, unsigned short mask)
854 {
855         unsigned short val;
856
857         // test for unchained codec
858         snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, mask);
859         snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, 0x0000);  /* ID0C, ID1C, SDIE = off */
860         val = snd_ac97_read(ac97, AC97_VENDOR_ID2);
861         if ((val & 0xff40) != 0x5340)
862                 return 0;
863         ac97->spec.ad18xx.unchained[idx] = mask;
864         ac97->spec.ad18xx.id[idx] = val;
865         ac97->spec.ad18xx.codec_cfg[idx] = 0x0000;
866         return mask;
867 }
868
869 static int patch_ad1881_chained1(ac97_t * ac97, int idx, unsigned short codec_bits)
870 {
871         static int cfg_bits[3] = { 1<<12, 1<<14, 1<<13 };
872         unsigned short val;
873         
874         snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, cfg_bits[idx]);
875         snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, 0x0004);  // SDIE
876         val = snd_ac97_read(ac97, AC97_VENDOR_ID2);
877         if ((val & 0xff40) != 0x5340)
878                 return 0;
879         if (codec_bits)
880                 snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, codec_bits);
881         ac97->spec.ad18xx.chained[idx] = cfg_bits[idx];
882         ac97->spec.ad18xx.id[idx] = val;
883         ac97->spec.ad18xx.codec_cfg[idx] = codec_bits ? codec_bits : 0x0004;
884         return 1;
885 }
886
887 static void patch_ad1881_chained(ac97_t * ac97, int unchained_idx, int cidx1, int cidx2)
888 {
889         // already detected?
890         if (ac97->spec.ad18xx.unchained[cidx1] || ac97->spec.ad18xx.chained[cidx1])
891                 cidx1 = -1;
892         if (ac97->spec.ad18xx.unchained[cidx2] || ac97->spec.ad18xx.chained[cidx2])
893                 cidx2 = -1;
894         if (cidx1 < 0 && cidx2 < 0)
895                 return;
896         // test for chained codecs
897         snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000,
898                              ac97->spec.ad18xx.unchained[unchained_idx]);
899         snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, 0x0002);          // ID1C
900         ac97->spec.ad18xx.codec_cfg[unchained_idx] = 0x0002;
901         if (cidx1 >= 0) {
902                 if (patch_ad1881_chained1(ac97, cidx1, 0x0006))         // SDIE | ID1C
903                         patch_ad1881_chained1(ac97, cidx2, 0);
904                 else if (patch_ad1881_chained1(ac97, cidx2, 0x0006))    // SDIE | ID1C
905                         patch_ad1881_chained1(ac97, cidx1, 0);
906         } else if (cidx2 >= 0) {
907                 patch_ad1881_chained1(ac97, cidx2, 0);
908         }
909 }
910
911 static struct snd_ac97_build_ops patch_ad1881_build_ops = {
912 #ifdef CONFIG_PM
913         .resume = ad18xx_resume
914 #endif
915 };
916
917 int patch_ad1881(ac97_t * ac97)
918 {
919         static const char cfg_idxs[3][2] = {
920                 {2, 1},
921                 {0, 2},
922                 {0, 1}
923         };
924         
925         // patch for Analog Devices
926         unsigned short codecs[3];
927         unsigned short val;
928         int idx, num;
929
930         val = snd_ac97_read(ac97, AC97_AD_SERIAL_CFG);
931         snd_ac97_write_cache(ac97, AC97_AD_SERIAL_CFG, val);
932         codecs[0] = patch_ad1881_unchained(ac97, 0, (1<<12));
933         codecs[1] = patch_ad1881_unchained(ac97, 1, (1<<14));
934         codecs[2] = patch_ad1881_unchained(ac97, 2, (1<<13));
935
936         snd_runtime_check(codecs[0] | codecs[1] | codecs[2], goto __end);
937
938         for (idx = 0; idx < 3; idx++)
939                 if (ac97->spec.ad18xx.unchained[idx])
940                         patch_ad1881_chained(ac97, idx, cfg_idxs[idx][0], cfg_idxs[idx][1]);
941
942         if (ac97->spec.ad18xx.id[1]) {
943                 ac97->flags |= AC97_AD_MULTI;
944                 ac97->scaps |= AC97_SCAP_SURROUND_DAC;
945         }
946         if (ac97->spec.ad18xx.id[2]) {
947                 ac97->flags |= AC97_AD_MULTI;
948                 ac97->scaps |= AC97_SCAP_CENTER_LFE_DAC;
949         }
950
951       __end:
952         /* select all codecs */
953         snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x7000, 0x7000);
954         /* check if only one codec is present */
955         for (idx = num = 0; idx < 3; idx++)
956                 if (ac97->spec.ad18xx.id[idx])
957                         num++;
958         if (num == 1) {
959                 /* ok, deselect all ID bits */
960                 snd_ac97_write_cache(ac97, AC97_AD_CODEC_CFG, 0x0000);
961                 ac97->spec.ad18xx.codec_cfg[0] = 
962                         ac97->spec.ad18xx.codec_cfg[1] = 
963                         ac97->spec.ad18xx.codec_cfg[2] = 0x0000;
964         }
965         /* required for AD1886/AD1885 combination */
966         ac97->ext_id = snd_ac97_read(ac97, AC97_EXTENDED_ID);
967         if (ac97->spec.ad18xx.id[0]) {
968                 ac97->id &= 0xffff0000;
969                 ac97->id |= ac97->spec.ad18xx.id[0];
970         }
971         ac97->build_ops = &patch_ad1881_build_ops;
972         return 0;
973 }
974
975 static const snd_kcontrol_new_t snd_ac97_controls_ad1885[] = {
976         AC97_SINGLE("Digital Mono Direct", AC97_AD_MISC, 11, 1, 0),
977         /* AC97_SINGLE("Digital Audio Mode", AC97_AD_MISC, 12, 1, 0), */ /* seems problematic */
978         AC97_SINGLE("Low Power Mixer", AC97_AD_MISC, 14, 1, 0),
979         AC97_SINGLE("Zero Fill DAC", AC97_AD_MISC, 15, 1, 0),
980         AC97_SINGLE("Headphone Jack Sense", AC97_AD_JACK_SPDIF, 9, 1, 1), /* inverted */
981         AC97_SINGLE("Line Jack Sense", AC97_AD_JACK_SPDIF, 8, 1, 1), /* inverted */
982 };
983
984 static int patch_ad1885_specific(ac97_t * ac97)
985 {
986         int err;
987
988         if ((err = patch_build_controls(ac97, snd_ac97_controls_ad1885, ARRAY_SIZE(snd_ac97_controls_ad1885))) < 0)
989                 return err;
990         return 0;
991 }
992
993 static struct snd_ac97_build_ops patch_ad1885_build_ops = {
994         .build_specific = &patch_ad1885_specific,
995 #ifdef CONFIG_PM
996         .resume = ad18xx_resume
997 #endif
998 };
999
1000 int patch_ad1885(ac97_t * ac97)
1001 {
1002         patch_ad1881(ac97);
1003         /* This is required to deal with the Intel D815EEAL2 */
1004         /* i.e. Line out is actually headphone out from codec */
1005
1006         /* set default */
1007         snd_ac97_write_cache(ac97, AC97_AD_MISC, 0x0404);
1008
1009         ac97->build_ops = &patch_ad1885_build_ops;
1010         return 0;
1011 }
1012
1013 int patch_ad1886(ac97_t * ac97)
1014 {
1015         patch_ad1881(ac97);
1016         /* Presario700 workaround */
1017         /* for Jack Sense/SPDIF Register misetting causing */
1018         snd_ac97_write_cache(ac97, AC97_AD_JACK_SPDIF, 0x0010);
1019         return 0;
1020 }
1021
1022 /* MISC bits */
1023 #define AC97_AD198X_MBC         0x0003  /* mic boost */
1024 #define AC97_AD198X_MBC_20      0x0000  /* +20dB */
1025 #define AC97_AD198X_MBC_10      0x0001  /* +10dB */
1026 #define AC97_AD198X_MBC_30      0x0002  /* +30dB */
1027 #define AC97_AD198X_VREFD       0x0004  /* VREF high-Z */
1028 #define AC97_AD198X_VREFH       0x0008  /* 2.25V, 3.7V */
1029 #define AC97_AD198X_VREF_0      0x000c  /* 0V */
1030 #define AC97_AD198X_SRU         0x0010  /* sample rate unlock */
1031 #define AC97_AD198X_LOSEL       0x0020  /* LINE_OUT amplifiers input select */
1032 #define AC97_AD198X_2MIC        0x0040  /* 2-channel mic select */
1033 #define AC97_AD198X_SPRD        0x0080  /* SPREAD enable */
1034 #define AC97_AD198X_DMIX0       0x0100  /* downmix mode: 0 = 6-to-4, 1 = 6-to-2 downmix */
1035 #define AC97_AD198X_DMIX1       0x0200  /* downmix mode: 1 = enabled */
1036 #define AC97_AD198X_HPSEL       0x0400  /* headphone amplifier input select */
1037 #define AC97_AD198X_CLDIS       0x0800  /* center/lfe disable */
1038 #define AC97_AD198X_LODIS       0x1000  /* LINE_OUT disable */
1039 #define AC97_AD198X_MSPLT       0x2000  /* mute split */
1040 #define AC97_AD198X_AC97NC      0x4000  /* AC97 no compatible mode */
1041 #define AC97_AD198X_DACZ        0x8000  /* DAC zero-fill mode */
1042
1043
1044 static int snd_ac97_ad198x_spdif_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1045 {
1046         static char *texts[2] = { "AC-Link", "A/D Converter" };
1047
1048         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1049         uinfo->count = 1;
1050         uinfo->value.enumerated.items = 2;
1051         if (uinfo->value.enumerated.item > 1)
1052                 uinfo->value.enumerated.item = 1;
1053         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1054         return 0;
1055 }
1056
1057 static int snd_ac97_ad198x_spdif_source_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1058 {
1059         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1060         unsigned short val;
1061
1062         val = ac97->regs[AC97_AD_SERIAL_CFG];
1063         ucontrol->value.enumerated.item[0] = (val >> 2) & 1;
1064         return 0;
1065 }
1066
1067 static int snd_ac97_ad198x_spdif_source_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1068 {
1069         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1070         unsigned short val;
1071
1072         if (ucontrol->value.enumerated.item[0] > 1)
1073                 return -EINVAL;
1074         val = ucontrol->value.enumerated.item[0] << 2;
1075         return snd_ac97_update_bits(ac97, AC97_AD_SERIAL_CFG, 0x0004, val);
1076 }
1077
1078 static const snd_kcontrol_new_t snd_ac97_ad198x_spdif_source = {
1079         .iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
1080         .name   = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1081         .info   = snd_ac97_ad198x_spdif_source_info,
1082         .get    = snd_ac97_ad198x_spdif_source_get,
1083         .put    = snd_ac97_ad198x_spdif_source_put,
1084 };
1085
1086 static int patch_ad198x_post_spdif(ac97_t * ac97)
1087 {
1088         return patch_build_controls(ac97, &snd_ac97_ad198x_spdif_source, 1);
1089 }
1090
1091 static const snd_kcontrol_new_t snd_ac97_ad1981x_jack_sense[] = {
1092         AC97_SINGLE("Headphone Jack Sense", AC97_AD_JACK_SPDIF, 11, 1, 0),
1093         AC97_SINGLE("Line Jack Sense", AC97_AD_JACK_SPDIF, 12, 1, 0),
1094 };
1095
1096 static int patch_ad1981a_specific(ac97_t * ac97)
1097 {
1098         return patch_build_controls(ac97, snd_ac97_ad1981x_jack_sense,
1099                                     ARRAY_SIZE(snd_ac97_ad1981x_jack_sense));
1100 }
1101
1102 static struct snd_ac97_build_ops patch_ad1981a_build_ops = {
1103         .build_post_spdif = patch_ad198x_post_spdif,
1104         .build_specific = patch_ad1981a_specific,
1105 #ifdef CONFIG_PM
1106         .resume = ad18xx_resume
1107 #endif
1108 };
1109
1110 static void check_ad1981_hp_jack_sense(ac97_t *ac97)
1111 {
1112         u32 subid = ((u32)ac97->subsystem_vendor << 16) | ac97->subsystem_device;
1113         switch (subid) {
1114         case 0x103c0890: /* HP nc6000 */
1115         case 0x103c006d: /* HP nx9105 */
1116         case 0x17340088: /* FSC Scenic-W */
1117                 /* enable headphone jack sense */
1118                 snd_ac97_update_bits(ac97, AC97_AD_JACK_SPDIF, 1<<11, 1<<11);
1119                 break;
1120         }
1121 }
1122
1123 int patch_ad1981a(ac97_t *ac97)
1124 {
1125         patch_ad1881(ac97);
1126         ac97->build_ops = &patch_ad1981a_build_ops;
1127         snd_ac97_update_bits(ac97, AC97_AD_MISC, AC97_AD198X_MSPLT, AC97_AD198X_MSPLT);
1128         ac97->flags |= AC97_STEREO_MUTES;
1129         check_ad1981_hp_jack_sense(ac97);
1130         return 0;
1131 }
1132
1133 static const snd_kcontrol_new_t snd_ac97_ad198x_2cmic =
1134 AC97_SINGLE("Stereo Mic", AC97_AD_MISC, 6, 1, 0);
1135
1136 static int patch_ad1981b_specific(ac97_t *ac97)
1137 {
1138         int err;
1139
1140         if ((err = patch_build_controls(ac97, &snd_ac97_ad198x_2cmic, 1)) < 0)
1141                 return err;
1142         return patch_build_controls(ac97, snd_ac97_ad1981x_jack_sense,
1143                                     ARRAY_SIZE(snd_ac97_ad1981x_jack_sense));
1144 }
1145
1146 static struct snd_ac97_build_ops patch_ad1981b_build_ops = {
1147         .build_post_spdif = patch_ad198x_post_spdif,
1148         .build_specific = patch_ad1981b_specific,
1149 #ifdef CONFIG_PM
1150         .resume = ad18xx_resume
1151 #endif
1152 };
1153
1154 int patch_ad1981b(ac97_t *ac97)
1155 {
1156         patch_ad1881(ac97);
1157         ac97->build_ops = &patch_ad1981b_build_ops;
1158         snd_ac97_update_bits(ac97, AC97_AD_MISC, AC97_AD198X_MSPLT, AC97_AD198X_MSPLT);
1159         ac97->flags |= AC97_STEREO_MUTES;
1160         check_ad1981_hp_jack_sense(ac97);
1161         return 0;
1162 }
1163
1164 static int snd_ac97_ad1888_lohpsel_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1165 {
1166         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1167         uinfo->count = 1;
1168         uinfo->value.integer.min = 0;
1169         uinfo->value.integer.max = 1;
1170         return 0;
1171 }
1172
1173 static int snd_ac97_ad1888_lohpsel_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t* ucontrol)
1174 {
1175         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1176         unsigned short val;
1177
1178         val = ac97->regs[AC97_AD_MISC];
1179         ucontrol->value.integer.value[0] = !(val & AC97_AD198X_LOSEL);
1180         return 0;
1181 }
1182
1183 static int snd_ac97_ad1888_lohpsel_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1184 {
1185         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1186         unsigned short val;
1187
1188         val = !ucontrol->value.integer.value[0]
1189                 ? (AC97_AD198X_LOSEL | AC97_AD198X_HPSEL) : 0;
1190         return snd_ac97_update_bits(ac97, AC97_AD_MISC,
1191                                     AC97_AD198X_LOSEL | AC97_AD198X_HPSEL, val);
1192 }
1193
1194 static int snd_ac97_ad1888_downmix_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1195 {
1196         static char *texts[3] = {"Off", "6 -> 4", "6 -> 2"};
1197
1198         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1199         uinfo->count = 1;
1200         uinfo->value.enumerated.items = 3;
1201         if (uinfo->value.enumerated.item > 2)
1202                 uinfo->value.enumerated.item = 2;
1203         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1204         return 0;
1205 }
1206
1207 static int snd_ac97_ad1888_downmix_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t* ucontrol)
1208 {
1209         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1210         unsigned short val;
1211
1212         val = ac97->regs[AC97_AD_MISC];
1213         if (!(val & AC97_AD198X_DMIX1))
1214                 ucontrol->value.enumerated.item[0] = 0;
1215         else
1216                 ucontrol->value.enumerated.item[0] = 1 + ((val >> 8) & 1);
1217         return 0;
1218 }
1219
1220 static int snd_ac97_ad1888_downmix_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1221 {
1222         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1223         unsigned short val;
1224
1225         if (ucontrol->value.enumerated.item[0] > 2)
1226                 return -EINVAL;
1227         if (ucontrol->value.enumerated.item[0] == 0)
1228                 val = 0;
1229         else
1230                 val = AC97_AD198X_DMIX1 |
1231                         ((ucontrol->value.enumerated.item[0] - 1) << 8);
1232         return snd_ac97_update_bits(ac97, AC97_AD_MISC,
1233                                     AC97_AD198X_DMIX0 | AC97_AD198X_DMIX1, val);
1234 }
1235
1236 static const snd_kcontrol_new_t snd_ac97_ad1888_controls[] = {
1237         {
1238                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1239                 .name = "Exchange Front/Surround",
1240                 .info = snd_ac97_ad1888_lohpsel_info,
1241                 .get = snd_ac97_ad1888_lohpsel_get,
1242                 .put = snd_ac97_ad1888_lohpsel_put
1243         },
1244         AC97_SINGLE("Spread Front to Surround and Center/LFE", AC97_AD_MISC, 7, 1, 0),
1245         {
1246                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1247                 .name = "Downmix",
1248                 .info = snd_ac97_ad1888_downmix_info,
1249                 .get = snd_ac97_ad1888_downmix_get,
1250                 .put = snd_ac97_ad1888_downmix_put
1251         },
1252         AC97_SINGLE("Surround Jack as Input", AC97_AD_MISC, 12, 1, 0),
1253         AC97_SINGLE("Center/LFE Jack as Input", AC97_AD_MISC, 11, 1, 0),
1254 };
1255
1256 static int patch_ad1888_specific(ac97_t *ac97)
1257 {
1258         /* rename 0x04 as "Master" and 0x02 as "Master Surround" */
1259         snd_ac97_rename_vol_ctl(ac97, "Master Playback", "Master Surround Playback");
1260         snd_ac97_rename_vol_ctl(ac97, "Headphone Playback", "Master Playback");
1261         return patch_build_controls(ac97, snd_ac97_ad1888_controls, ARRAY_SIZE(snd_ac97_ad1888_controls));
1262 }
1263
1264 static struct snd_ac97_build_ops patch_ad1888_build_ops = {
1265         .build_post_spdif = patch_ad198x_post_spdif,
1266         .build_specific = patch_ad1888_specific,
1267 #ifdef CONFIG_PM
1268         .resume = ad18xx_resume
1269 #endif
1270 };
1271
1272 int patch_ad1888(ac97_t * ac97)
1273 {
1274         unsigned short misc;
1275         
1276         patch_ad1881(ac97);
1277         ac97->build_ops = &patch_ad1888_build_ops;
1278         /* Switch FRONT/SURROUND LINE-OUT/HP-OUT default connection */
1279         /* it seems that most vendors connect line-out connector to headphone out of AC'97 */
1280         /* AD-compatible mode */
1281         /* Stereo mutes enabled */
1282         misc = snd_ac97_read(ac97, AC97_AD_MISC);
1283         snd_ac97_write_cache(ac97, AC97_AD_MISC, misc |
1284                              AC97_AD198X_LOSEL |
1285                              AC97_AD198X_HPSEL |
1286                              AC97_AD198X_MSPLT |
1287                              AC97_AD198X_AC97NC);
1288         ac97->flags |= AC97_STEREO_MUTES;
1289         return 0;
1290 }
1291
1292 static int patch_ad1980_specific(ac97_t *ac97)
1293 {
1294         int err;
1295
1296         if ((err = patch_ad1888_specific(ac97)) < 0)
1297                 return err;
1298         return patch_build_controls(ac97, &snd_ac97_ad198x_2cmic, 1);
1299 }
1300
1301 static struct snd_ac97_build_ops patch_ad1980_build_ops = {
1302         .build_post_spdif = patch_ad198x_post_spdif,
1303         .build_specific = patch_ad1980_specific,
1304 #ifdef CONFIG_PM
1305         .resume = ad18xx_resume
1306 #endif
1307 };
1308
1309 int patch_ad1980(ac97_t * ac97)
1310 {
1311         patch_ad1888(ac97);
1312         ac97->build_ops = &patch_ad1980_build_ops;
1313         return 0;
1314 }
1315
1316 static const snd_kcontrol_new_t snd_ac97_ad1985_controls[] = {
1317         AC97_SINGLE("Center/LFE Jack as Mic", AC97_AD_SERIAL_CFG, 9, 1, 0),
1318         AC97_SINGLE("Exchange Center/LFE", AC97_AD_SERIAL_CFG, 3, 1, 0)
1319 };
1320
1321 static int patch_ad1985_specific(ac97_t *ac97)
1322 {
1323         int err;
1324
1325         if ((err = patch_ad1980_specific(ac97)) < 0)
1326                 return err;
1327         return patch_build_controls(ac97, snd_ac97_ad1985_controls, ARRAY_SIZE(snd_ac97_ad1985_controls));
1328 }
1329
1330 static struct snd_ac97_build_ops patch_ad1985_build_ops = {
1331         .build_post_spdif = patch_ad198x_post_spdif,
1332         .build_specific = patch_ad1985_specific,
1333 #ifdef CONFIG_PM
1334         .resume = ad18xx_resume
1335 #endif
1336 };
1337
1338 int patch_ad1985(ac97_t * ac97)
1339 {
1340         unsigned short misc;
1341         
1342         patch_ad1881(ac97);
1343         ac97->build_ops = &patch_ad1985_build_ops;
1344         misc = snd_ac97_read(ac97, AC97_AD_MISC);
1345         /* switch front/surround line-out/hp-out */
1346         /* center/LFE, mic in 3.75V mode */
1347         /* AD-compatible mode */
1348         /* Stereo mutes enabled */
1349         /* in accordance with ADI driver: misc | 0x5c28 */
1350         snd_ac97_write_cache(ac97, AC97_AD_MISC, misc |
1351                              AC97_AD198X_VREFH |
1352                              AC97_AD198X_LOSEL |
1353                              AC97_AD198X_HPSEL |
1354                              AC97_AD198X_CLDIS |
1355                              AC97_AD198X_LODIS |
1356                              AC97_AD198X_MSPLT |
1357                              AC97_AD198X_AC97NC);
1358         ac97->flags |= AC97_STEREO_MUTES;
1359         /* on AD1985 rev. 3, AC'97 revision bits are zero */
1360         ac97->ext_id = (ac97->ext_id & ~AC97_EI_REV_MASK) | AC97_EI_REV_23;
1361         return 0;
1362 }
1363
1364 /*
1365  * realtek ALC65x/850 codecs
1366  */
1367 static int snd_ac97_alc650_mic_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
1368 {
1369         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1370         ucontrol->value.integer.value[0] = (ac97->regs[AC97_ALC650_MULTICH] >> 10) & 1;
1371         return 0;
1372 }
1373
1374 static int snd_ac97_alc650_mic_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
1375 {
1376         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1377         int change, val;
1378         val = !!(snd_ac97_read(ac97, AC97_ALC650_MULTICH) & (1 << 10));
1379         change = (ucontrol->value.integer.value[0] != val);
1380         if (change) {
1381                 /* disable/enable vref */
1382                 snd_ac97_update_bits(ac97, AC97_ALC650_CLOCK, 1 << 12,
1383                                      ucontrol->value.integer.value[0] ? (1 << 12) : 0);
1384                 /* turn on/off center-on-mic */
1385                 snd_ac97_update_bits(ac97, AC97_ALC650_MULTICH, 1 << 10,
1386                                      ucontrol->value.integer.value[0] ? (1 << 10) : 0);
1387                 /* GPIO0 high for mic */
1388                 snd_ac97_update_bits(ac97, AC97_ALC650_GPIO_STATUS, 0x100,
1389                                      ucontrol->value.integer.value[0] ? 0 : 0x100);
1390         }
1391         return change;
1392 }
1393
1394 static const snd_kcontrol_new_t snd_ac97_controls_alc650[] = {
1395         AC97_SINGLE("Duplicate Front", AC97_ALC650_MULTICH, 0, 1, 0),
1396         AC97_SINGLE("Surround Down Mix", AC97_ALC650_MULTICH, 1, 1, 0),
1397         AC97_SINGLE("Center/LFE Down Mix", AC97_ALC650_MULTICH, 2, 1, 0),
1398         AC97_SINGLE("Exchange Center/LFE", AC97_ALC650_MULTICH, 3, 1, 0),
1399         /* 4: Analog Input To Surround */
1400         /* 5: Analog Input To Center/LFE */
1401         /* 6: Independent Master Volume Right */
1402         /* 7: Independent Master Volume Left */
1403         /* 8: reserved */
1404         AC97_SINGLE("Line-In As Surround", AC97_ALC650_MULTICH, 9, 1, 0),
1405         /* 10: mic, see below */
1406         /* 11-13: in IEC958 controls */
1407         AC97_SINGLE("Swap Surround Slot", AC97_ALC650_MULTICH, 14, 1, 0),
1408 #if 0 /* always set in patch_alc650 */
1409         AC97_SINGLE("IEC958 Input Clock Enable", AC97_ALC650_CLOCK, 0, 1, 0),
1410         AC97_SINGLE("IEC958 Input Pin Enable", AC97_ALC650_CLOCK, 1, 1, 0),
1411         AC97_SINGLE("Surround DAC Switch", AC97_ALC650_SURR_DAC_VOL, 15, 1, 1),
1412         AC97_DOUBLE("Surround DAC Volume", AC97_ALC650_SURR_DAC_VOL, 8, 0, 31, 1),
1413         AC97_SINGLE("Center/LFE DAC Switch", AC97_ALC650_LFE_DAC_VOL, 15, 1, 1),
1414         AC97_DOUBLE("Center/LFE DAC Volume", AC97_ALC650_LFE_DAC_VOL, 8, 0, 31, 1),
1415 #endif
1416         {
1417                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1418                 .name = "Mic As Center/LFE",
1419                 .info = snd_ac97_info_volsw,
1420                 .get = snd_ac97_alc650_mic_get,
1421                 .put = snd_ac97_alc650_mic_put,
1422                 .private_value = AC97_SINGLE_VALUE(0, 0, 1, 0) /* only mask needed */
1423         },
1424 };
1425
1426 static const snd_kcontrol_new_t snd_ac97_spdif_controls_alc650[] = {
1427         AC97_SINGLE("IEC958 Capture Switch", AC97_ALC650_MULTICH, 11, 1, 0),
1428         AC97_SINGLE("Analog to IEC958 Output", AC97_ALC650_MULTICH, 12, 1, 0),
1429         /* disable this controls since it doesn't work as expected */
1430         /* AC97_SINGLE("IEC958 Input Monitor", AC97_ALC650_MULTICH, 13, 1, 0), */
1431 };
1432
1433 static int patch_alc650_specific(ac97_t * ac97)
1434 {
1435         int err;
1436
1437         if ((err = patch_build_controls(ac97, snd_ac97_controls_alc650, ARRAY_SIZE(snd_ac97_controls_alc650))) < 0)
1438                 return err;
1439         if (ac97->ext_id & AC97_EI_SPDIF) {
1440                 if ((err = patch_build_controls(ac97, snd_ac97_spdif_controls_alc650, ARRAY_SIZE(snd_ac97_spdif_controls_alc650))) < 0)
1441                         return err;
1442         }
1443         return 0;
1444 }
1445
1446 static struct snd_ac97_build_ops patch_alc650_ops = {
1447         .build_specific = patch_alc650_specific
1448 };
1449
1450 int patch_alc650(ac97_t * ac97)
1451 {
1452         unsigned short val;
1453
1454         ac97->build_ops = &patch_alc650_ops;
1455
1456         /* determine the revision */
1457         val = snd_ac97_read(ac97, AC97_ALC650_REVISION) & 0x3f;
1458         if (val < 3)
1459                 ac97->id = 0x414c4720;          /* Old version */
1460         else if (val < 0x10)
1461                 ac97->id = 0x414c4721;          /* D version */
1462         else if (val < 0x20)
1463                 ac97->id = 0x414c4722;          /* E version */
1464         else if (val < 0x30)
1465                 ac97->id = 0x414c4723;          /* F version */
1466
1467         /* revision E or F */
1468         /* FIXME: what about revision D ? */
1469         ac97->spec.dev_flags = (ac97->id == 0x414c4722 ||
1470                                 ac97->id == 0x414c4723);
1471
1472         /* enable AC97_ALC650_GPIO_SETUP, AC97_ALC650_CLOCK for R/W */
1473         snd_ac97_write_cache(ac97, AC97_ALC650_GPIO_STATUS, 
1474                 snd_ac97_read(ac97, AC97_ALC650_GPIO_STATUS) | 0x8000);
1475
1476         /* Enable SPDIF-IN only on Rev.E and above */
1477         val = snd_ac97_read(ac97, AC97_ALC650_CLOCK);
1478         /* SPDIF IN with pin 47 */
1479         if (ac97->spec.dev_flags)
1480                 val |= 0x03; /* enable */
1481         else
1482                 val &= ~0x03; /* disable */
1483         snd_ac97_write_cache(ac97, AC97_ALC650_CLOCK, val);
1484
1485         /* set default: slot 3,4,7,8,6,9
1486            spdif-in monitor off, analog-spdif off, spdif-in off
1487            center on mic off, surround on line-in off
1488            downmix off, duplicate front off
1489         */
1490         snd_ac97_write_cache(ac97, AC97_ALC650_MULTICH, 0);
1491
1492         /* set GPIO0 for mic bias */
1493         /* GPIO0 pin output, no interrupt, high */
1494         snd_ac97_write_cache(ac97, AC97_ALC650_GPIO_SETUP,
1495                              snd_ac97_read(ac97, AC97_ALC650_GPIO_SETUP) | 0x01);
1496         snd_ac97_write_cache(ac97, AC97_ALC650_GPIO_STATUS,
1497                              (snd_ac97_read(ac97, AC97_ALC650_GPIO_STATUS) | 0x100) & ~0x10);
1498
1499         /* full DAC volume */
1500         snd_ac97_write_cache(ac97, AC97_ALC650_SURR_DAC_VOL, 0x0808);
1501         snd_ac97_write_cache(ac97, AC97_ALC650_LFE_DAC_VOL, 0x0808);
1502         return 0;
1503 }
1504
1505 static int snd_ac97_alc655_mic_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
1506 {
1507         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1508         ucontrol->value.integer.value[0] = (ac97->regs[AC97_ALC650_MULTICH] >> 10) & 1;
1509         return 0;
1510 }
1511
1512 static int snd_ac97_alc655_mic_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
1513 {
1514         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1515
1516         /* misc control; vrefout disable */
1517         snd_ac97_update_bits(ac97, AC97_ALC650_CLOCK, 1 << 12,
1518                              ucontrol->value.integer.value[0] ? (1 << 12) : 0);
1519         return ac97_update_bits_page(ac97, AC97_ALC650_MULTICH, 1 << 10,
1520                                      ucontrol->value.integer.value[0] ? (1 << 10) : 0,
1521                                      0);
1522 }
1523
1524
1525 static const snd_kcontrol_new_t snd_ac97_controls_alc655[] = {
1526         AC97_PAGE_SINGLE("Duplicate Front", AC97_ALC650_MULTICH, 0, 1, 0, 0),
1527         AC97_PAGE_SINGLE("Line-In As Surround", AC97_ALC650_MULTICH, 9, 1, 0, 0),
1528         {
1529                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1530                 .name = "Mic As Center/LFE",
1531                 .info = snd_ac97_info_volsw,
1532                 .get = snd_ac97_alc655_mic_get,
1533                 .put = snd_ac97_alc655_mic_put,
1534                 .private_value = AC97_SINGLE_VALUE(0, 0, 1, 0) /* only mask needed */
1535         },
1536 };
1537
1538 static int alc655_iec958_route_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1539 {
1540         static char *texts_655[3] = { "PCM", "Analog In", "IEC958 In" };
1541         static char *texts_658[4] = { "PCM", "Analog1 In", "Analog2 In", "IEC958 In" };
1542         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1543
1544         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1545         uinfo->count = 1;
1546         uinfo->value.enumerated.items = ac97->spec.dev_flags ? 4 : 3;
1547         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1548                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1549         strcpy(uinfo->value.enumerated.name,
1550                ac97->spec.dev_flags ?
1551                texts_658[uinfo->value.enumerated.item] :
1552                texts_655[uinfo->value.enumerated.item]);
1553         return 0;
1554 }
1555
1556 static int alc655_iec958_route_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1557 {
1558         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1559         unsigned short val;
1560
1561         val = ac97->regs[AC97_ALC650_MULTICH];
1562         val = (val >> 12) & 3;
1563         if (ac97->spec.dev_flags && val == 3)
1564                 val = 0;
1565         ucontrol->value.enumerated.item[0] = val;
1566         return 0;
1567 }
1568
1569 static int alc655_iec958_route_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1570 {
1571         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1572
1573         return ac97_update_bits_page(ac97, AC97_ALC650_MULTICH, 3 << 12,
1574                                      (unsigned short)ucontrol->value.enumerated.item[0],
1575                                      0);
1576 }
1577
1578 static const snd_kcontrol_new_t snd_ac97_spdif_controls_alc655[] = {
1579         AC97_PAGE_SINGLE("IEC958 Capture Switch", AC97_ALC650_MULTICH, 11, 1, 0, 0),
1580         /* disable this controls since it doesn't work as expected */
1581         /* AC97_PAGE_SINGLE("IEC958 Input Monitor", AC97_ALC650_MULTICH, 14, 1, 0, 0), */
1582         {
1583                 .iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
1584                 .name   = "IEC958 Playback Route",
1585                 .info   = alc655_iec958_route_info,
1586                 .get    = alc655_iec958_route_get,
1587                 .put    = alc655_iec958_route_put,
1588         },
1589 };
1590
1591 static int patch_alc655_specific(ac97_t * ac97)
1592 {
1593         int err;
1594
1595         if ((err = patch_build_controls(ac97, snd_ac97_controls_alc655, ARRAY_SIZE(snd_ac97_controls_alc655))) < 0)
1596                 return err;
1597         if (ac97->ext_id & AC97_EI_SPDIF) {
1598                 if ((err = patch_build_controls(ac97, snd_ac97_spdif_controls_alc655, ARRAY_SIZE(snd_ac97_spdif_controls_alc655))) < 0)
1599                         return err;
1600         }
1601         return 0;
1602 }
1603
1604 static struct snd_ac97_build_ops patch_alc655_ops = {
1605         .build_specific = patch_alc655_specific
1606 };
1607
1608 int patch_alc655(ac97_t * ac97)
1609 {
1610         unsigned int val;
1611
1612         ac97->spec.dev_flags = (ac97->id == 0x414c4780); /* ALC658 */
1613
1614         ac97->build_ops = &patch_alc655_ops;
1615
1616         /* assume only page 0 for writing cache */
1617         snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, AC97_PAGE_VENDOR);
1618
1619         /* adjust default values */
1620         val = snd_ac97_read(ac97, 0x7a); /* misc control */
1621         if (ac97->id == 0x414c4780) /* ALC658 */
1622                 val &= ~(1 << 1); /* Pin 47 is spdif input pin */
1623         else /* ALC655 */
1624                 val |= (1 << 1); /* Pin 47 is spdif input pin */
1625         val &= ~(1 << 12); /* vref enable */
1626         snd_ac97_write_cache(ac97, 0x7a, val);
1627         /* set default: spdif-in enabled,
1628            spdif-in monitor off, spdif-in PCM off
1629            center on mic off, surround on line-in off
1630            duplicate front off
1631         */
1632         snd_ac97_write_cache(ac97, AC97_ALC650_MULTICH, 1<<15);
1633
1634         /* full DAC volume */
1635         snd_ac97_write_cache(ac97, AC97_ALC650_SURR_DAC_VOL, 0x0808);
1636         snd_ac97_write_cache(ac97, AC97_ALC650_LFE_DAC_VOL, 0x0808);
1637         return 0;
1638 }
1639
1640
1641 #define AC97_ALC850_JACK_SELECT 0x76
1642 #define AC97_ALC850_MISC1       0x7a
1643
1644 static int ac97_alc850_surround_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
1645 {
1646         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1647         ucontrol->value.integer.value[0] = ((ac97->regs[AC97_ALC850_JACK_SELECT] >> 12) & 7) == 2;
1648         return 0;
1649 }
1650
1651 static int ac97_alc850_surround_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
1652 {
1653         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1654
1655         /* SURR 1kOhm (bit4), Amp (bit5) */
1656         snd_ac97_update_bits(ac97, AC97_ALC850_MISC1, (1<<4)|(1<<5),
1657                              ucontrol->value.integer.value[0] ? (1<<5) : (1<<4));
1658         /* LINE-IN = 0, SURROUND = 2 */
1659         return snd_ac97_update_bits(ac97, AC97_ALC850_JACK_SELECT, 7 << 12,
1660                                     ucontrol->value.integer.value[0] ? (2<<12) : (0<<12));
1661 }
1662
1663 static int ac97_alc850_mic_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
1664 {
1665         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1666         ucontrol->value.integer.value[0] = ((ac97->regs[AC97_ALC850_JACK_SELECT] >> 4) & 7) == 2;
1667         return 0;
1668 }
1669
1670 static int ac97_alc850_mic_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t * ucontrol)
1671 {
1672         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1673
1674         /* Vref disable (bit12), 1kOhm (bit13) */
1675         snd_ac97_update_bits(ac97, AC97_ALC850_MISC1, (1<<12)|(1<<13),
1676                              ucontrol->value.integer.value[0] ? (1<<12) : (1<<13));
1677         /* MIC-IN = 1, CENTER-LFE = 2 */
1678         return snd_ac97_update_bits(ac97, AC97_ALC850_JACK_SELECT, 7 << 4,
1679                                     ucontrol->value.integer.value[0] ? (2<<4) : (1<<4));
1680 }
1681
1682 static const snd_kcontrol_new_t snd_ac97_controls_alc850[] = {
1683         AC97_PAGE_SINGLE("Duplicate Front", AC97_ALC650_MULTICH, 0, 1, 0, 0),
1684         {
1685                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1686                 .name = "Line-In As Surround",
1687                 .info = snd_ac97_info_volsw,
1688                 .get = ac97_alc850_surround_get,
1689                 .put = ac97_alc850_surround_put,
1690                 .private_value = AC97_SINGLE_VALUE(0, 0, 1, 0) /* only mask needed */
1691         },
1692         {
1693                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1694                 .name = "Mic As Center/LFE",
1695                 .info = snd_ac97_info_volsw,
1696                 .get = ac97_alc850_mic_get,
1697                 .put = ac97_alc850_mic_put,
1698                 .private_value = AC97_SINGLE_VALUE(0, 0, 1, 0) /* only mask needed */
1699         },
1700
1701 };
1702
1703 static int patch_alc850_specific(ac97_t *ac97)
1704 {
1705         int err;
1706
1707         if ((err = patch_build_controls(ac97, snd_ac97_controls_alc850, ARRAY_SIZE(snd_ac97_controls_alc850))) < 0)
1708                 return err;
1709         if (ac97->ext_id & AC97_EI_SPDIF) {
1710                 if ((err = patch_build_controls(ac97, snd_ac97_spdif_controls_alc655, ARRAY_SIZE(snd_ac97_spdif_controls_alc655))) < 0)
1711                         return err;
1712         }
1713         return 0;
1714 }
1715
1716 static struct snd_ac97_build_ops patch_alc850_ops = {
1717         .build_specific = patch_alc850_specific
1718 };
1719
1720 int patch_alc850(ac97_t *ac97)
1721 {
1722         ac97->build_ops = &patch_alc850_ops;
1723
1724         ac97->spec.dev_flags = 0; /* for IEC958 playback route - ALC655 compatible */
1725
1726         /* assume only page 0 for writing cache */
1727         snd_ac97_update_bits(ac97, AC97_INT_PAGING, AC97_PAGE_MASK, AC97_PAGE_VENDOR);
1728
1729         /* adjust default values */
1730         /* set default: spdif-in enabled,
1731            spdif-in monitor off, spdif-in PCM off
1732            center on mic off, surround on line-in off
1733            duplicate front off
1734         */
1735         snd_ac97_write_cache(ac97, AC97_ALC650_MULTICH, 1<<15);
1736         /* SURR_OUT: on, Surr 1kOhm: on, Surr Amp: off, Front 1kOhm: off
1737          * Front Amp: on, Vref: enable, Center 1kOhm: on, Mix: on
1738          */
1739         snd_ac97_write_cache(ac97, 0x7a, (1<<1)|(1<<4)|(0<<5)|(1<<6)|
1740                              (1<<7)|(0<<12)|(1<<13)|(0<<14));
1741         /* detection UIO2,3: all path floating, UIO3: MIC, Vref2: disable,
1742          * UIO1: FRONT, Vref3: disable, UIO3: LINE, Front-Mic: mute
1743          */
1744         snd_ac97_write_cache(ac97, 0x76, (0<<0)|(0<<2)|(1<<4)|(1<<7)|(2<<8)|
1745                              (1<<11)|(0<<12)|(1<<15));
1746
1747         /* full DAC volume */
1748         snd_ac97_write_cache(ac97, AC97_ALC650_SURR_DAC_VOL, 0x0808);
1749         snd_ac97_write_cache(ac97, AC97_ALC650_LFE_DAC_VOL, 0x0808);
1750         return 0;
1751 }
1752
1753
1754 /*
1755  * C-Media CM97xx codecs
1756  */
1757 static const snd_kcontrol_new_t snd_ac97_cm9738_controls[] = {
1758         AC97_SINGLE("Line-In As Surround", AC97_CM9738_VENDOR_CTRL, 10, 1, 0),
1759         AC97_SINGLE("Duplicate Front", AC97_CM9738_VENDOR_CTRL, 13, 1, 0),
1760 };
1761
1762 static int patch_cm9738_specific(ac97_t * ac97)
1763 {
1764         return patch_build_controls(ac97, snd_ac97_cm9738_controls, ARRAY_SIZE(snd_ac97_cm9738_controls));
1765 }
1766
1767 static struct snd_ac97_build_ops patch_cm9738_ops = {
1768         .build_specific = patch_cm9738_specific
1769 };
1770
1771 int patch_cm9738(ac97_t * ac97)
1772 {
1773         ac97->build_ops = &patch_cm9738_ops;
1774         /* FIXME: can anyone confirm below? */
1775         /* CM9738 has no PCM volume although the register reacts */
1776         ac97->flags |= AC97_HAS_NO_PCM_VOL;
1777         snd_ac97_write_cache(ac97, AC97_PCM, 0x8000);
1778
1779         return 0;
1780 }
1781
1782 static int snd_ac97_cmedia_spdif_playback_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1783 {
1784         static char *texts[] = { "Analog", "Digital" };
1785
1786         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1787         uinfo->count = 1;
1788         uinfo->value.enumerated.items = 2;
1789         if (uinfo->value.enumerated.item > 1)
1790                 uinfo->value.enumerated.item = 1;
1791         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1792         return 0;
1793 }
1794
1795 static int snd_ac97_cmedia_spdif_playback_source_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1796 {
1797         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1798         unsigned short val;
1799
1800         val = ac97->regs[AC97_CM9739_SPDIF_CTRL];
1801         ucontrol->value.enumerated.item[0] = (val >> 1) & 0x01;
1802         return 0;
1803 }
1804
1805 static int snd_ac97_cmedia_spdif_playback_source_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1806 {
1807         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1808
1809         return snd_ac97_update_bits(ac97, AC97_CM9739_SPDIF_CTRL,
1810                                     0x01 << 1, 
1811                                     (ucontrol->value.enumerated.item[0] & 0x01) << 1);
1812 }
1813
1814 static const snd_kcontrol_new_t snd_ac97_cm9739_controls_spdif[] = {
1815         /* BIT 0: SPDI_EN - always true */
1816         { /* BIT 1: SPDIFS */
1817                 .iface  = SNDRV_CTL_ELEM_IFACE_MIXER,
1818                 .name   = SNDRV_CTL_NAME_IEC958("",PLAYBACK,NONE) "Source",
1819                 .info   = snd_ac97_cmedia_spdif_playback_source_info,
1820                 .get    = snd_ac97_cmedia_spdif_playback_source_get,
1821                 .put    = snd_ac97_cmedia_spdif_playback_source_put,
1822         },
1823         /* BIT 2: IG_SPIV */
1824         AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,NONE) "Valid Switch", AC97_CM9739_SPDIF_CTRL, 2, 1, 0),
1825         /* BIT 3: SPI2F */
1826         AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,NONE) "Monitor", AC97_CM9739_SPDIF_CTRL, 3, 1, 0), 
1827         /* BIT 4: SPI2SDI */
1828         AC97_SINGLE(SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH), AC97_CM9739_SPDIF_CTRL, 4, 1, 0),
1829         /* BIT 8: SPD32 - 32bit SPDIF - not supported yet */
1830 };
1831
1832 static int snd_ac97_cm9739_center_mic_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1833 {
1834         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1835         if (ac97->regs[AC97_CM9739_MULTI_CHAN] & 0x1000)
1836                 ucontrol->value.integer.value[0] = 1;
1837         else
1838                 ucontrol->value.integer.value[0] = 0;
1839         return 0;
1840 }
1841
1842 static int snd_ac97_cm9739_center_mic_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1843 {
1844         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1845         return snd_ac97_update_bits(ac97, AC97_CM9739_MULTI_CHAN, 0x3000,
1846                                     ucontrol->value.integer.value[0] ? 
1847                                     0x1000 : 0x2000);
1848 }
1849
1850 static const snd_kcontrol_new_t snd_ac97_cm9739_controls[] = {
1851         AC97_SINGLE("Line-In As Surround", AC97_CM9739_MULTI_CHAN, 10, 1, 0),
1852         {
1853                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1854                 .name = "Mic As Center/LFE",
1855                 .info = snd_ac97_info_volsw,
1856                 .get = snd_ac97_cm9739_center_mic_get,
1857                 .put = snd_ac97_cm9739_center_mic_put,
1858                 .private_value = AC97_SINGLE_VALUE(0, 0, 1, 0) /* only mask needed */
1859         },
1860 };
1861
1862 static int patch_cm9739_specific(ac97_t * ac97)
1863 {
1864         return patch_build_controls(ac97, snd_ac97_cm9739_controls, ARRAY_SIZE(snd_ac97_cm9739_controls));
1865 }
1866
1867 static int patch_cm9739_post_spdif(ac97_t * ac97)
1868 {
1869         return patch_build_controls(ac97, snd_ac97_cm9739_controls_spdif, ARRAY_SIZE(snd_ac97_cm9739_controls_spdif));
1870 }
1871
1872 static struct snd_ac97_build_ops patch_cm9739_ops = {
1873         .build_specific = patch_cm9739_specific,
1874         .build_post_spdif = patch_cm9739_post_spdif
1875 };
1876
1877 int patch_cm9739(ac97_t * ac97)
1878 {
1879         unsigned short val;
1880
1881         ac97->build_ops = &patch_cm9739_ops;
1882
1883         /* CM9739/A has no Master and PCM volume although the register reacts */
1884         ac97->flags |= AC97_HAS_NO_MASTER_VOL | AC97_HAS_NO_PCM_VOL;
1885         snd_ac97_write_cache(ac97, AC97_MASTER, 0x8000);
1886         snd_ac97_write_cache(ac97, AC97_PCM, 0x8000);
1887
1888         /* check spdif */
1889         val = snd_ac97_read(ac97, AC97_EXTENDED_STATUS);
1890         if (val & AC97_EA_SPCV) {
1891                 /* enable spdif in */
1892                 snd_ac97_write_cache(ac97, AC97_CM9739_SPDIF_CTRL,
1893                                      snd_ac97_read(ac97, AC97_CM9739_SPDIF_CTRL) | 0x01);
1894                 ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000; /* 48k only */
1895         } else {
1896                 ac97->ext_id &= ~AC97_EI_SPDIF; /* disable extended-id */
1897                 ac97->rates[AC97_RATES_SPDIF] = 0;
1898         }
1899
1900         /* set-up multi channel */
1901         /* bit 14: 0 = SPDIF, 1 = EAPD */
1902         /* bit 13: enable internal vref output for mic */
1903         /* bit 12: disable center/lfe (swithable) */
1904         /* bit 10: disable surround/line (switchable) */
1905         /* bit 9: mix 2 surround off */
1906         /* bit 4: undocumented; 0 mutes the CM9739A, which defaults to 1 */
1907         /* bit 3: undocumented; surround? */
1908         /* bit 0: dB */
1909         val = snd_ac97_read(ac97, AC97_CM9739_MULTI_CHAN) & (1 << 4);
1910         val |= (1 << 3);
1911         val |= (1 << 13);
1912         if (! (ac97->ext_id & AC97_EI_SPDIF))
1913                 val |= (1 << 14);
1914         snd_ac97_write_cache(ac97, AC97_CM9739_MULTI_CHAN, val);
1915
1916         /* FIXME: set up GPIO */
1917         snd_ac97_write_cache(ac97, 0x70, 0x0100);
1918         snd_ac97_write_cache(ac97, 0x72, 0x0020);
1919         /* Special exception for ASUS W1000/CMI9739. It does not have an SPDIF in. */
1920         if (ac97->pci &&
1921              ac97->subsystem_vendor == 0x1043 &&
1922              ac97->subsystem_device == 0x1843) {
1923                 snd_ac97_write_cache(ac97, AC97_CM9739_SPDIF_CTRL,
1924                         snd_ac97_read(ac97, AC97_CM9739_SPDIF_CTRL) & ~0x01);
1925                 snd_ac97_write_cache(ac97, AC97_CM9739_MULTI_CHAN,
1926                         snd_ac97_read(ac97, AC97_CM9739_MULTI_CHAN) | (1 << 14));
1927         }
1928
1929         return 0;
1930 }
1931
1932 #define AC97_CM9761_MULTI_CHAN  0x64
1933 #define AC97_CM9761_SPDIF_CTRL  0x6c
1934
1935 static int snd_ac97_cm9761_linein_rear_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1936 {
1937         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1938         if (ac97->regs[AC97_CM9739_MULTI_CHAN] & 0x0400)
1939                 ucontrol->value.integer.value[0] = 1;
1940         else
1941                 ucontrol->value.integer.value[0] = 0;
1942         return 0;
1943 }
1944
1945 static int snd_ac97_cm9761_linein_rear_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1946 {
1947         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1948         unsigned short vals[2][2] = {
1949                 { 0x0008, 0x0400 }, /* off, on */
1950                 { 0x0000, 0x0408 }, /* off, on (9761-82 rev.B) */
1951         };
1952         return snd_ac97_update_bits(ac97, AC97_CM9739_MULTI_CHAN, 0x0408,
1953                                     vals[ac97->spec.dev_flags][!!ucontrol->value.integer.value[0]]);
1954 }
1955
1956 static int snd_ac97_cm9761_center_mic_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1957 {
1958         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1959         if (ac97->regs[AC97_CM9739_MULTI_CHAN] & 0x1000)
1960                 ucontrol->value.integer.value[0] = 1;
1961         else
1962                 ucontrol->value.integer.value[0] = 0;
1963         if (ac97->spec.dev_flags) /* 9761-82 rev.B */
1964                 ucontrol->value.integer.value[0] = !ucontrol->value.integer.value[0];
1965         return 0;
1966 }
1967
1968 static int snd_ac97_cm9761_center_mic_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1969 {
1970         ac97_t *ac97 = snd_kcontrol_chip(kcontrol);
1971         unsigned short vals[2][2] = {
1972                 { 0x2000, 0x1880 }, /* off, on */
1973                 { 0x1000, 0x2880 }, /* off, on (9761-82 rev.B) */
1974         };
1975         return snd_ac97_update_bits(ac97, AC97_CM9739_MULTI_CHAN, 0x3880,
1976                                     vals[ac97->spec.dev_flags][!!ucontrol->value.integer.value[0]]);
1977 }
1978
1979 static const snd_kcontrol_new_t snd_ac97_cm9761_controls[] = {
1980         {
1981                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1982                 .name = "Line-In As Surround",
1983                 .info = snd_ac97_info_volsw,
1984                 .get = snd_ac97_cm9761_linein_rear_get,
1985                 .put = snd_ac97_cm9761_linein_rear_put,
1986                 .private_value = AC97_SINGLE_VALUE(0, 0, 1, 0) /* only mask needed */
1987         },
1988         {
1989                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1990                 .name = "Mic As Center/LFE",
1991                 .info = snd_ac97_info_volsw,
1992                 .get = snd_ac97_cm9761_center_mic_get,
1993                 .put = snd_ac97_cm9761_center_mic_put,
1994                 .private_value = AC97_SINGLE_VALUE(0, 0, 1, 0) /* only mask needed */
1995         },
1996 };
1997
1998 static int patch_cm9761_specific(ac97_t * ac97)
1999 {
2000         return patch_build_controls(ac97, snd_ac97_cm9761_controls, ARRAY_SIZE(snd_ac97_cm9761_controls));
2001 }
2002
2003 static struct snd_ac97_build_ops patch_cm9761_ops = {
2004         .build_specific = patch_cm9761_specific,
2005         .build_post_spdif = patch_cm9739_post_spdif /* hope it's identical... */
2006 };
2007
2008 int patch_cm9761(ac97_t *ac97)
2009 {
2010         unsigned short val;
2011
2012         /* CM9761 has no Master and PCM volume although the register reacts */
2013         ac97->flags |= AC97_HAS_NO_MASTER_VOL | AC97_HAS_NO_PCM_VOL;
2014         snd_ac97_write_cache(ac97, AC97_MASTER, 0x8000);
2015         snd_ac97_write_cache(ac97, AC97_PCM, 0x8000);
2016
2017         ac97->spec.dev_flags = 0; /* 1 = model 82 revision B */
2018         if (ac97->id == AC97_ID_CM9761_82) {
2019                 unsigned short tmp;
2020                 /* check page 1, reg 0x60 */
2021                 val = snd_ac97_read(ac97, AC97_INT_PAGING);
2022                 snd_ac97_write_cache(ac97, AC97_INT_PAGING, (val & ~0x0f) | 0x01);
2023                 tmp = snd_ac97_read(ac97, 0x60);
2024                 ac97->spec.dev_flags = tmp & 1; /* revision B? */
2025                 snd_ac97_write_cache(ac97, AC97_INT_PAGING, val);
2026         }
2027
2028         ac97->build_ops = &patch_cm9761_ops;
2029
2030         /* enable spdif */
2031         /* force the SPDIF bit in ext_id - codec doesn't set this bit! */
2032         ac97->ext_id |= AC97_EI_SPDIF;
2033         /* to be sure: we overwrite the ext status bits */
2034         snd_ac97_write_cache(ac97, AC97_EXTENDED_STATUS, 0x05c0);
2035         snd_ac97_write_cache(ac97, AC97_CM9761_SPDIF_CTRL, 0x0209);
2036         ac97->rates[AC97_RATES_SPDIF] = SNDRV_PCM_RATE_48000; /* 48k only */
2037
2038         /* set-up multi channel */
2039         /* bit 15: pc master beep off
2040          * bit 14: ??
2041          * bit 13: vref ctl [= cm9739]
2042          * bit 12: center/mic [= cm9739] (reverted on rev B)
2043          * bit 11: ?? (mic/center/lfe) (reverted on rev B)
2044          * bit 10: suddound/line [= cm9739]
2045          * bit  9: mix 2 surround
2046          * bit  8: ?
2047          * bit  7: ?? (mic/center/lfe)
2048          * bit  4: ?? (front)
2049          * bit  3: ?? (line-in/rear share) (revereted with rev B)
2050          * bit  2: ?? (surround)
2051          * bit  1: front mic
2052          * bit  0: mic boost
2053          */
2054
2055 #if 0
2056         if (ac97->spec.dev_flags)
2057                 val = 0x0214;
2058         else
2059                 val = 0x321c;
2060         snd_ac97_write_cache(ac97, AC97_CM9761_MULTI_CHAN, val);
2061 #endif
2062
2063         /* FIXME: set up GPIO */
2064         snd_ac97_write_cache(ac97, 0x70, 0x0100);
2065         snd_ac97_write_cache(ac97, 0x72, 0x0020);
2066
2067         return 0;
2068 }
2069        
2070
2071 /*
2072  * VIA VT1616 codec
2073  */
2074 static const snd_kcontrol_new_t snd_ac97_controls_vt1616[] = {
2075 AC97_SINGLE("DC Offset removal", 0x5a, 10, 1, 0),
2076 AC97_SINGLE("Alternate Level to Surround Out", 0x5a, 15, 1, 0),
2077 AC97_SINGLE("Downmix LFE and Center to Front", 0x5a, 12, 1, 0),
2078 AC97_SINGLE("Downmix Surround to Front", 0x5a, 11, 1, 0),
2079 };
2080
2081 static int patch_vt1616_specific(ac97_t * ac97)
2082 {
2083         int err;
2084
2085         if (snd_ac97_try_bit(ac97, 0x5a, 9))
2086                 if ((err = patch_build_controls(ac97, &snd_ac97_controls_vt1616[0], 1)) < 0)
2087                         return err;
2088         if ((err = patch_build_controls(ac97, &snd_ac97_controls_vt1616[1], ARRAY_SIZE(snd_ac97_controls_vt1616) - 1)) < 0)
2089                 return err;
2090         return 0;
2091 }
2092
2093 static struct snd_ac97_build_ops patch_vt1616_ops = {
2094         .build_specific = patch_vt1616_specific
2095 };
2096
2097 int patch_vt1616(ac97_t * ac97)
2098 {
2099         ac97->build_ops = &patch_vt1616_ops;
2100         return 0;
2101 }
2102
2103 static const snd_kcontrol_new_t snd_ac97_controls_it2646[] = {
2104         AC97_SINGLE("Line-In As Surround", 0x76, 9, 1, 0),
2105         AC97_SINGLE("Mic As Center/LFE", 0x76, 10, 1, 0),
2106 };
2107
2108 static const snd_kcontrol_new_t snd_ac97_spdif_controls_it2646[] = {
2109         AC97_SINGLE("IEC958 Capture Switch", 0x76, 11, 1, 0),
2110         AC97_SINGLE("Analog to IEC958 Output", 0x76, 12, 1, 0),
2111         AC97_SINGLE("IEC958 Input Monitor", 0x76, 13, 1, 0),
2112 };
2113
2114 static int patch_it2646_specific(ac97_t * ac97)
2115 {
2116         int err;
2117         if ((err = patch_build_controls(ac97, snd_ac97_controls_it2646, ARRAY_SIZE(snd_ac97_controls_it2646))) < 0)
2118                 return err;
2119         if ((err = patch_build_controls(ac97, snd_ac97_spdif_controls_it2646, ARRAY_SIZE(snd_ac97_spdif_controls_it2646))) < 0)
2120                 return err;
2121         return 0;
2122 }
2123
2124 static struct snd_ac97_build_ops patch_it2646_ops = {
2125         .build_specific = patch_it2646_specific
2126 };
2127
2128 int patch_it2646(ac97_t * ac97)
2129 {
2130         ac97->build_ops = &patch_it2646_ops;
2131         /* full DAC volume */
2132         snd_ac97_write_cache(ac97, 0x5E, 0x0808);
2133         snd_ac97_write_cache(ac97, 0x7A, 0x0808);
2134         return 0;
2135 }
2136
2137 /* Si3036/8 specific registers */
2138 #define AC97_SI3036_CHIP_ID     0x5a
2139
2140 int mpatch_si3036(ac97_t * ac97)
2141 {
2142         //printk("mpatch_si3036: chip id = %x\n", snd_ac97_read(ac97, 0x5a));
2143         snd_ac97_write_cache(ac97, 0x5c, 0xf210 );
2144         snd_ac97_write_cache(ac97, 0x68, 0);
2145         return 0;
2146 }