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