[ALSA] hda-codec - code cleanups in patch_sigmatel.c
[pandora-kernel.git] / sound / pci / hda / patch_sigmatel.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * HD audio interface patch for SigmaTel STAC92xx
5  *
6  * Copyright (c) 2005 Embedded Alley Solutions, Inc.
7  * Matt Porter <mporter@embeddedalley.com>
8  *
9  * Based on patch_cmedia.c and patch_realtek.c
10  * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
11  *
12  *  This driver is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This driver is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25  */
26
27 #include <sound/driver.h>
28 #include <linux/init.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/pci.h>
32 #include <sound/core.h>
33 #include <sound/asoundef.h>
34 #include "hda_codec.h"
35 #include "hda_local.h"
36
37 #define NUM_CONTROL_ALLOC       32
38 #define STAC_HP_EVENT           0x37
39
40 enum {
41         STAC_REF,
42         STAC_9200_DELL_D21,
43         STAC_9200_DELL_D22,
44         STAC_9200_DELL_D23,
45         STAC_9200_DELL_M21,
46         STAC_9200_DELL_M22,
47         STAC_9200_DELL_M23,
48         STAC_9200_DELL_M24,
49         STAC_9200_DELL_M25,
50         STAC_9200_DELL_M26,
51         STAC_9200_DELL_M27,
52         STAC_9200_MODELS
53 };
54
55 enum {
56         STAC_9205_REF,
57         STAC_9205_DELL_M42,
58         STAC_9205_DELL_M43,
59         STAC_9205_DELL_M44,
60         STAC_9205_M43xx,
61         STAC_9205_MODELS
62 };
63
64 enum {
65         STAC_925x_REF,
66         STAC_M2_2,
67         STAC_MA6,
68         STAC_PA6,
69         STAC_925x_MODELS
70 };
71
72 enum {
73         STAC_D945_REF,
74         STAC_D945GTP3,
75         STAC_D945GTP5,
76         STAC_INTEL_MAC_V1,
77         STAC_INTEL_MAC_V2,
78         STAC_INTEL_MAC_V3,
79         STAC_INTEL_MAC_V4,
80         STAC_INTEL_MAC_V5,
81         /* for backward compatibility */
82         STAC_MACMINI,
83         STAC_MACBOOK,
84         STAC_MACBOOK_PRO_V1,
85         STAC_MACBOOK_PRO_V2,
86         STAC_IMAC_INTEL,
87         STAC_IMAC_INTEL_20,
88         STAC_922X_DELL_D81,
89         STAC_922X_DELL_D82,
90         STAC_922X_DELL_M81,
91         STAC_922X_DELL_M82,
92         STAC_922X_MODELS
93 };
94
95 enum {
96         STAC_D965_REF,
97         STAC_D965_3ST,
98         STAC_D965_5ST,
99         STAC_DELL_3ST,
100         STAC_927X_MODELS
101 };
102
103 struct sigmatel_spec {
104         struct snd_kcontrol_new *mixers[4];
105         unsigned int num_mixers;
106
107         int board_config;
108         unsigned int surr_switch: 1;
109         unsigned int line_switch: 1;
110         unsigned int mic_switch: 1;
111         unsigned int alt_switch: 1;
112         unsigned int hp_detect: 1;
113         unsigned int gpio_mute: 1;
114
115         unsigned int gpio_mask, gpio_data;
116
117         /* playback */
118         struct hda_multi_out multiout;
119         hda_nid_t dac_nids[5];
120
121         /* capture */
122         hda_nid_t *adc_nids;
123         unsigned int num_adcs;
124         hda_nid_t *mux_nids;
125         unsigned int num_muxes;
126         hda_nid_t *dmic_nids;
127         unsigned int num_dmics;
128         hda_nid_t dmux_nid;
129         hda_nid_t dig_in_nid;
130
131         /* pin widgets */
132         hda_nid_t *pin_nids;
133         unsigned int num_pins;
134         unsigned int *pin_configs;
135         unsigned int *bios_pin_configs;
136
137         /* codec specific stuff */
138         struct hda_verb *init;
139         struct snd_kcontrol_new *mixer;
140
141         /* capture source */
142         struct hda_input_mux *dinput_mux;
143         unsigned int cur_dmux;
144         struct hda_input_mux *input_mux;
145         unsigned int cur_mux[3];
146
147         /* i/o switches */
148         unsigned int io_switch[2];
149
150         struct hda_pcm pcm_rec[2];      /* PCM information */
151
152         /* dynamic controls and input_mux */
153         struct auto_pin_cfg autocfg;
154         unsigned int num_kctl_alloc, num_kctl_used;
155         struct snd_kcontrol_new *kctl_alloc;
156         struct hda_input_mux private_dimux;
157         struct hda_input_mux private_imux;
158 };
159
160 static hda_nid_t stac9200_adc_nids[1] = {
161         0x03,
162 };
163
164 static hda_nid_t stac9200_mux_nids[1] = {
165         0x0c,
166 };
167
168 static hda_nid_t stac9200_dac_nids[1] = {
169         0x02,
170 };
171
172 static hda_nid_t stac925x_adc_nids[1] = {
173         0x03,
174 };
175
176 static hda_nid_t stac925x_mux_nids[1] = {
177         0x0f,
178 };
179
180 static hda_nid_t stac925x_dac_nids[1] = {
181         0x02,
182 };
183
184 static hda_nid_t stac925x_dmic_nids[1] = {
185         0x15, 
186 };
187
188 static hda_nid_t stac922x_adc_nids[2] = {
189         0x06, 0x07,
190 };
191
192 static hda_nid_t stac922x_mux_nids[2] = {
193         0x12, 0x13,
194 };
195
196 static hda_nid_t stac927x_adc_nids[3] = {
197         0x07, 0x08, 0x09
198 };
199
200 static hda_nid_t stac927x_mux_nids[3] = {
201         0x15, 0x16, 0x17
202 };
203
204 static hda_nid_t stac9205_adc_nids[2] = {
205         0x12, 0x13
206 };
207
208 static hda_nid_t stac9205_mux_nids[2] = {
209         0x19, 0x1a
210 };
211
212 static hda_nid_t stac9205_dmic_nids[2] = {
213         0x17, 0x18,
214 };
215
216 static hda_nid_t stac9200_pin_nids[8] = {
217         0x08, 0x09, 0x0d, 0x0e, 
218         0x0f, 0x10, 0x11, 0x12,
219 };
220
221 static hda_nid_t stac925x_pin_nids[8] = {
222         0x07, 0x08, 0x0a, 0x0b, 
223         0x0c, 0x0d, 0x10, 0x11,
224 };
225
226 static hda_nid_t stac922x_pin_nids[10] = {
227         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
228         0x0f, 0x10, 0x11, 0x15, 0x1b,
229 };
230
231 static hda_nid_t stac927x_pin_nids[14] = {
232         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
233         0x0f, 0x10, 0x11, 0x12, 0x13,
234         0x14, 0x21, 0x22, 0x23,
235 };
236
237 static hda_nid_t stac9205_pin_nids[12] = {
238         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
239         0x0f, 0x14, 0x16, 0x17, 0x18,
240         0x21, 0x22,
241 };
242
243 static int stac92xx_dmux_enum_info(struct snd_kcontrol *kcontrol,
244                                    struct snd_ctl_elem_info *uinfo)
245 {
246         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
247         struct sigmatel_spec *spec = codec->spec;
248         return snd_hda_input_mux_info(spec->dinput_mux, uinfo);
249 }
250
251 static int stac92xx_dmux_enum_get(struct snd_kcontrol *kcontrol,
252                                   struct snd_ctl_elem_value *ucontrol)
253 {
254         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
255         struct sigmatel_spec *spec = codec->spec;
256
257         ucontrol->value.enumerated.item[0] = spec->cur_dmux;
258         return 0;
259 }
260
261 static int stac92xx_dmux_enum_put(struct snd_kcontrol *kcontrol,
262                                   struct snd_ctl_elem_value *ucontrol)
263 {
264         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
265         struct sigmatel_spec *spec = codec->spec;
266
267         return snd_hda_input_mux_put(codec, spec->dinput_mux, ucontrol,
268                                      spec->dmux_nid, &spec->cur_dmux);
269 }
270
271 static int stac92xx_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
272 {
273         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
274         struct sigmatel_spec *spec = codec->spec;
275         return snd_hda_input_mux_info(spec->input_mux, uinfo);
276 }
277
278 static int stac92xx_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
279 {
280         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
281         struct sigmatel_spec *spec = codec->spec;
282         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
283
284         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
285         return 0;
286 }
287
288 static int stac92xx_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
289 {
290         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
291         struct sigmatel_spec *spec = codec->spec;
292         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
293
294         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
295                                      spec->mux_nids[adc_idx], &spec->cur_mux[adc_idx]);
296 }
297
298 static struct hda_verb stac9200_core_init[] = {
299         /* set dac0mux for dac converter */
300         { 0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
301         {}
302 };
303
304 static struct hda_verb stac925x_core_init[] = {
305         /* set dac0mux for dac converter */
306         { 0x06, AC_VERB_SET_CONNECT_SEL, 0x00},
307         {}
308 };
309
310 static struct hda_verb stac922x_core_init[] = {
311         /* set master volume and direct control */      
312         { 0x16, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
313         {}
314 };
315
316 static struct hda_verb d965_core_init[] = {
317         /* set master volume and direct control */      
318         { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
319         /* unmute node 0x1b */
320         { 0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
321         /* select node 0x03 as DAC */   
322         { 0x0b, AC_VERB_SET_CONNECT_SEL, 0x01},
323         {}
324 };
325
326 static struct hda_verb stac927x_core_init[] = {
327         /* set master volume and direct control */      
328         { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
329         {}
330 };
331
332 static struct hda_verb stac9205_core_init[] = {
333         /* set master volume and direct control */      
334         { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
335         {}
336 };
337
338 #define STAC_INPUT_SOURCE \
339         { \
340                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
341                 .name = "Input Source", \
342                 .count = 1, \
343                 .info = stac92xx_mux_enum_info, \
344                 .get = stac92xx_mux_enum_get, \
345                 .put = stac92xx_mux_enum_put, \
346         }
347
348
349 static struct snd_kcontrol_new stac9200_mixer[] = {
350         HDA_CODEC_VOLUME("Master Playback Volume", 0xb, 0, HDA_OUTPUT),
351         HDA_CODEC_MUTE("Master Playback Switch", 0xb, 0, HDA_OUTPUT),
352         STAC_INPUT_SOURCE,
353         HDA_CODEC_VOLUME("Capture Volume", 0x0a, 0, HDA_OUTPUT),
354         HDA_CODEC_MUTE("Capture Switch", 0x0a, 0, HDA_OUTPUT),
355         HDA_CODEC_VOLUME("Capture Mux Volume", 0x0c, 0, HDA_OUTPUT),
356         { } /* end */
357 };
358
359 static struct snd_kcontrol_new stac925x_mixer[] = {
360         STAC_INPUT_SOURCE,
361         HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_OUTPUT),
362         HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_OUTPUT),
363         HDA_CODEC_VOLUME("Capture Mux Volume", 0x0f, 0, HDA_OUTPUT),
364         { } /* end */
365 };
366
367 /* This needs to be generated dynamically based on sequence */
368 static struct snd_kcontrol_new stac922x_mixer[] = {
369         STAC_INPUT_SOURCE,
370         HDA_CODEC_VOLUME("Capture Volume", 0x17, 0x0, HDA_INPUT),
371         HDA_CODEC_MUTE("Capture Switch", 0x17, 0x0, HDA_INPUT),
372         HDA_CODEC_VOLUME("Mux Capture Volume", 0x12, 0x0, HDA_OUTPUT),
373         { } /* end */
374 };
375
376 /* This needs to be generated dynamically based on sequence */
377 static struct snd_kcontrol_new stac9227_mixer[] = {
378         STAC_INPUT_SOURCE,
379         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
380         HDA_CODEC_MUTE("Capture Switch", 0x1b, 0x0, HDA_OUTPUT),
381         { } /* end */
382 };
383
384 static struct snd_kcontrol_new stac927x_mixer[] = {
385         STAC_INPUT_SOURCE,
386         HDA_CODEC_VOLUME("InMux Capture Volume", 0x15, 0x0, HDA_OUTPUT),
387         HDA_CODEC_VOLUME("InVol Capture Volume", 0x18, 0x0, HDA_INPUT),
388         HDA_CODEC_MUTE("ADCMux Capture Switch", 0x1b, 0x0, HDA_OUTPUT),
389         { } /* end */
390 };
391
392 static struct snd_kcontrol_new stac9205_mixer[] = {
393         {
394                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
395                 .name = "Digital Input Source",
396                 .count = 1,
397                 .info = stac92xx_dmux_enum_info,
398                 .get = stac92xx_dmux_enum_get,
399                 .put = stac92xx_dmux_enum_put,
400         },
401         STAC_INPUT_SOURCE,
402         HDA_CODEC_VOLUME("InMux Capture Volume", 0x19, 0x0, HDA_OUTPUT),
403         HDA_CODEC_VOLUME("InVol Capture Volume", 0x1b, 0x0, HDA_INPUT),
404         HDA_CODEC_MUTE("ADCMux Capture Switch", 0x1d, 0x0, HDA_OUTPUT),
405         { } /* end */
406 };
407
408 static int stac92xx_build_controls(struct hda_codec *codec)
409 {
410         struct sigmatel_spec *spec = codec->spec;
411         int err;
412         int i;
413
414         err = snd_hda_add_new_ctls(codec, spec->mixer);
415         if (err < 0)
416                 return err;
417
418         for (i = 0; i < spec->num_mixers; i++) {
419                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
420                 if (err < 0)
421                         return err;
422         }
423
424         if (spec->multiout.dig_out_nid) {
425                 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
426                 if (err < 0)
427                         return err;
428         }
429         if (spec->dig_in_nid) {
430                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
431                 if (err < 0)
432                         return err;
433         }
434         return 0;       
435 }
436
437 static unsigned int ref9200_pin_configs[8] = {
438         0x01c47010, 0x01447010, 0x0221401f, 0x01114010,
439         0x02a19020, 0x01a19021, 0x90100140, 0x01813122,
440 };
441
442 /* 
443     STAC 9200 pin configs for
444     102801A8
445     102801DE
446     102801E8
447 */
448 static unsigned int dell9200_d21_pin_configs[8] = {
449         0x400001f0, 0x400001f1, 0x01a19021, 0x90100140,
450         0x01813122, 0x02214030, 0x01014010, 0x02a19020,
451 };
452
453 /* 
454     STAC 9200 pin configs for
455     102801C0
456     102801C1
457 */
458 static unsigned int dell9200_d22_pin_configs[8] = {
459         0x400001f0, 0x400001f1, 0x02a19021, 0x90100140,
460         0x400001f2, 0x0221401f, 0x01014010, 0x01813020,
461 };
462
463 /* 
464     STAC 9200 pin configs for
465     102801C4 (Dell Dimension E310)
466     102801C5
467     102801C7
468     102801D9
469     102801DA
470     102801E3
471 */
472 static unsigned int dell9200_d23_pin_configs[8] = {
473         0x400001f0, 0x400001f1, 0x01a19021, 0x90100140,
474         0x400001f2, 0x0221401f, 0x01014010, 0x01813020,
475 };
476
477
478 /* 
479     STAC 9200-32 pin configs for
480     102801B5 (Dell Inspiron 630m)
481     102801D8 (Dell Inspiron 640m)
482 */
483 static unsigned int dell9200_m21_pin_configs[8] = {
484         0x40c003fa, 0x03441340, 0x03a11020, 0x401003fc,
485         0x403003fd, 0x0321121f, 0x0321121f, 0x408003fb,
486 };
487
488 /* 
489     STAC 9200-32 pin configs for
490     102801C2 (Dell Latitude D620)
491     102801C8 
492     102801CC (Dell Latitude D820)
493     102801D4 
494     102801D6 
495 */
496 static unsigned int dell9200_m22_pin_configs[8] = {
497         0x40c003fa, 0x0144131f, 0x03A11020, 0x401003fb, 
498         0x40f000fc, 0x0321121f, 0x90170310, 0x90a70321, 
499 };
500
501 /* 
502     STAC 9200-32 pin configs for
503     102801CE (Dell XPS M1710)
504     102801CF (Dell Precision M90)
505 */
506 static unsigned int dell9200_m23_pin_configs[8] = {
507         0x40c003fa, 0x01441340, 0x0421421f, 0x90170310,
508         0x408003fb, 0x04a1102e, 0x90170311, 0x403003fc,
509 };
510
511 /*
512     STAC 9200-32 pin configs for 
513     102801C9
514     102801CA
515     102801CB (Dell Latitude 120L)
516     102801D3
517 */
518 static unsigned int dell9200_m24_pin_configs[8] = {
519         0x40c003fa, 0x404003fb, 0x03a11020, 0x401003fd, 
520         0x403003fe, 0x0321121f, 0x90170310, 0x408003fc,
521 };
522
523 /*
524     STAC 9200-32 pin configs for
525     102801BD (Dell Inspiron E1505n)
526     102801EE
527     102801EF
528 */
529 static unsigned int dell9200_m25_pin_configs[8] = {
530         0x40c003fa, 0x01441340, 0x04a11020, 0x401003fc,
531         0x403003fd, 0x0421121f, 0x90170310, 0x408003fb,
532 };
533
534 /*
535     STAC 9200-32 pin configs for
536     102801F5 (Dell Inspiron 1501)
537     102801F6
538 */
539 static unsigned int dell9200_m26_pin_configs[8] = {
540         0x40c003fa, 0x404003fb, 0x04a11020, 0x401003fd,
541         0x403003fe, 0x0421121f, 0x90170310, 0x408003fc,
542 };
543
544 /*
545     STAC 9200-32
546     102801CD (Dell Inspiron E1705/9400)
547 */
548 static unsigned int dell9200_m27_pin_configs[8] = {
549         0x40c003fa, 0x01441340, 0x04a11020, 0x90170310,
550         0x40f003fc, 0x0421121f, 0x90170310, 0x408003fb,
551 };
552
553
554 static unsigned int *stac9200_brd_tbl[STAC_9200_MODELS] = {
555         [STAC_REF] = ref9200_pin_configs,
556         [STAC_9200_DELL_D21] = dell9200_d21_pin_configs,
557         [STAC_9200_DELL_D22] = dell9200_d22_pin_configs,
558         [STAC_9200_DELL_D23] = dell9200_d23_pin_configs,
559         [STAC_9200_DELL_M21] = dell9200_m21_pin_configs,
560         [STAC_9200_DELL_M22] = dell9200_m22_pin_configs,
561         [STAC_9200_DELL_M23] = dell9200_m23_pin_configs,
562         [STAC_9200_DELL_M24] = dell9200_m24_pin_configs,
563         [STAC_9200_DELL_M25] = dell9200_m25_pin_configs,
564         [STAC_9200_DELL_M26] = dell9200_m26_pin_configs,
565         [STAC_9200_DELL_M27] = dell9200_m27_pin_configs,
566 };
567
568 static const char *stac9200_models[STAC_9200_MODELS] = {
569         [STAC_REF] = "ref",
570         [STAC_9200_DELL_D21] = "dell-d21",
571         [STAC_9200_DELL_D22] = "dell-d22",
572         [STAC_9200_DELL_D23] = "dell-d23",
573         [STAC_9200_DELL_M21] = "dell-m21",
574         [STAC_9200_DELL_M22] = "dell-m22",
575         [STAC_9200_DELL_M23] = "dell-m23",
576         [STAC_9200_DELL_M24] = "dell-m24",
577         [STAC_9200_DELL_M25] = "dell-m25",
578         [STAC_9200_DELL_M26] = "dell-m26",
579         [STAC_9200_DELL_M27] = "dell-m27",
580 };
581
582 static struct snd_pci_quirk stac9200_cfg_tbl[] = {
583         /* SigmaTel reference board */
584         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
585                       "DFI LanParty", STAC_REF),
586         /* Dell laptops have BIOS problem */
587         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01a8,
588                       "unknown Dell", STAC_9200_DELL_D21),
589         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01b5,
590                       "Dell Inspiron 630m", STAC_9200_DELL_M21),
591         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01bd,
592                       "Dell Inspiron E1505n", STAC_9200_DELL_M25),
593         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c0,
594                       "unknown Dell", STAC_9200_DELL_D22),
595         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c1,
596                       "unknown Dell", STAC_9200_DELL_D22),
597         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c2,
598                       "Dell Latitude D620", STAC_9200_DELL_M22),
599         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c5,
600                       "unknown Dell", STAC_9200_DELL_D23),
601         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c7,
602                       "unknown Dell", STAC_9200_DELL_D23),
603         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c8,
604                       "unknown Dell", STAC_9200_DELL_M22),
605         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c9,
606                       "unknown Dell", STAC_9200_DELL_M24),
607         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ca,
608                       "unknown Dell", STAC_9200_DELL_M24),
609         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cb,
610                       "Dell Latitude 120L", STAC_9200_DELL_M24),
611         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cc,
612                       "Dell Latitude D820", STAC_9200_DELL_M22),
613         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cd,
614                       "Dell Inspiron E1705/9400", STAC_9200_DELL_M27),
615         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ce,
616                       "Dell XPS M1710", STAC_9200_DELL_M23),
617         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cf,
618                       "Dell Precision M90", STAC_9200_DELL_M23),
619         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d3,
620                       "unknown Dell", STAC_9200_DELL_M22),
621         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d4,
622                       "unknown Dell", STAC_9200_DELL_M22),
623         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d6,
624                       "unknown Dell", STAC_9200_DELL_M22),
625         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d8,
626                       "Dell Inspiron 640m", STAC_9200_DELL_M21),
627         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d9,
628                       "unknown Dell", STAC_9200_DELL_D23),
629         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01da,
630                       "unknown Dell", STAC_9200_DELL_D23),
631         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01de,
632                       "unknown Dell", STAC_9200_DELL_D21),
633         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01e3,
634                       "unknown Dell", STAC_9200_DELL_D23),
635         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01e8,
636                       "unknown Dell", STAC_9200_DELL_D21),
637         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ee,
638                       "unknown Dell", STAC_9200_DELL_M25),
639         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ef,
640                       "unknown Dell", STAC_9200_DELL_M25),
641         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f5,
642                       "Dell Inspiron 1501", STAC_9200_DELL_M26),
643         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f6,
644                       "unknown Dell", STAC_9200_DELL_M26),
645         /* Panasonic */
646         SND_PCI_QUIRK(0x10f7, 0x8338, "Panasonic CF-74", STAC_REF),
647
648         {} /* terminator */
649 };
650
651 static unsigned int ref925x_pin_configs[8] = {
652         0x40c003f0, 0x424503f2, 0x01813022, 0x02a19021,
653         0x90a70320, 0x02214210, 0x400003f1, 0x9033032e,
654 };
655
656 static unsigned int stac925x_MA6_pin_configs[8] = {
657         0x40c003f0, 0x424503f2, 0x01813022, 0x02a19021,
658         0x90a70320, 0x90100211, 0x400003f1, 0x9033032e,
659 };
660
661 static unsigned int stac925x_PA6_pin_configs[8] = {
662         0x40c003f0, 0x424503f2, 0x01813022, 0x02a19021,
663         0x50a103f0, 0x90100211, 0x400003f1, 0x9033032e,
664 };
665
666 static unsigned int stac925xM2_2_pin_configs[8] = {
667         0x40c003f3, 0x424503f2, 0x04180011, 0x02a19020,
668         0x50a103f0, 0x90100212, 0x400003f1, 0x9033032e,
669 };
670
671 static unsigned int *stac925x_brd_tbl[STAC_925x_MODELS] = {
672         [STAC_REF] = ref925x_pin_configs,
673         [STAC_M2_2] = stac925xM2_2_pin_configs,
674         [STAC_MA6] = stac925x_MA6_pin_configs,
675         [STAC_PA6] = stac925x_PA6_pin_configs,
676 };
677
678 static const char *stac925x_models[STAC_925x_MODELS] = {
679         [STAC_REF] = "ref",
680         [STAC_M2_2] = "m2-2",
681         [STAC_MA6] = "m6",
682         [STAC_PA6] = "pa6",
683 };
684
685 static struct snd_pci_quirk stac925x_cfg_tbl[] = {
686         /* SigmaTel reference board */
687         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668, "DFI LanParty", STAC_REF),
688         SND_PCI_QUIRK(0x8384, 0x7632, "Stac9202 Reference Board", STAC_REF),
689         SND_PCI_QUIRK(0x107b, 0x0316, "Gateway M255", STAC_REF),
690         SND_PCI_QUIRK(0x107b, 0x0366, "Gateway MP6954", STAC_REF),
691         SND_PCI_QUIRK(0x107b, 0x0461, "Gateway NX560XL", STAC_MA6),
692         SND_PCI_QUIRK(0x107b, 0x0681, "Gateway NX860", STAC_PA6),
693         SND_PCI_QUIRK(0x1002, 0x437b, "Gateway MX6453", STAC_M2_2),
694         {} /* terminator */
695 };
696
697 static unsigned int ref922x_pin_configs[10] = {
698         0x01014010, 0x01016011, 0x01012012, 0x0221401f,
699         0x01813122, 0x01011014, 0x01441030, 0x01c41030,
700         0x40000100, 0x40000100,
701 };
702
703 /*
704     STAC 922X pin configs for
705     102801A7
706     102801AB
707     102801A9
708     102801D1
709     102801D2
710 */
711 static unsigned int dell_922x_d81_pin_configs[10] = {
712         0x02214030, 0x01a19021, 0x01111012, 0x01114010,
713         0x02a19020, 0x01117011, 0x400001f0, 0x400001f1,
714         0x01813122, 0x400001f2,
715 };
716
717 /*
718     STAC 922X pin configs for
719     102801AC
720     102801D0
721 */
722 static unsigned int dell_922x_d82_pin_configs[10] = {
723         0x02214030, 0x01a19021, 0x01111012, 0x01114010,
724         0x02a19020, 0x01117011, 0x01451140, 0x400001f0,
725         0x01813122, 0x400001f1,
726 };
727
728 /*
729     STAC 922X pin configs for
730     102801BF
731 */
732 static unsigned int dell_922x_m81_pin_configs[10] = {
733         0x0321101f, 0x01112024, 0x01111222, 0x91174220,
734         0x03a11050, 0x01116221, 0x90a70330, 0x01452340, 
735         0x40C003f1, 0x405003f0,
736 };
737
738 /*
739     STAC 9221 A1 pin configs for
740     102801D7 (Dell XPS M1210)
741 */
742 static unsigned int dell_922x_m82_pin_configs[10] = {
743         0x0221121f, 0x408103ff, 0x02111212, 0x90100310, 
744         0x408003f1, 0x02111211, 0x03451340, 0x40c003f2, 
745         0x508003f3, 0x405003f4, 
746 };
747
748 static unsigned int d945gtp3_pin_configs[10] = {
749         0x0221401f, 0x01a19022, 0x01813021, 0x01014010,
750         0x40000100, 0x40000100, 0x40000100, 0x40000100,
751         0x02a19120, 0x40000100,
752 };
753
754 static unsigned int d945gtp5_pin_configs[10] = {
755         0x0221401f, 0x01011012, 0x01813024, 0x01014010,
756         0x01a19021, 0x01016011, 0x01452130, 0x40000100,
757         0x02a19320, 0x40000100,
758 };
759
760 static unsigned int intel_mac_v1_pin_configs[10] = {
761         0x0121e21f, 0x400000ff, 0x9017e110, 0x400000fd,
762         0x400000fe, 0x0181e020, 0x1145e030, 0x11c5e240,
763         0x400000fc, 0x400000fb,
764 };
765
766 static unsigned int intel_mac_v2_pin_configs[10] = {
767         0x0121e21f, 0x90a7012e, 0x9017e110, 0x400000fd,
768         0x400000fe, 0x0181e020, 0x1145e230, 0x500000fa,
769         0x400000fc, 0x400000fb,
770 };
771
772 static unsigned int intel_mac_v3_pin_configs[10] = {
773         0x0121e21f, 0x90a7012e, 0x9017e110, 0x400000fd,
774         0x400000fe, 0x0181e020, 0x1145e230, 0x11c5e240,
775         0x400000fc, 0x400000fb,
776 };
777
778 static unsigned int intel_mac_v4_pin_configs[10] = {
779         0x0321e21f, 0x03a1e02e, 0x9017e110, 0x9017e11f,
780         0x400000fe, 0x0381e020, 0x1345e230, 0x13c5e240,
781         0x400000fc, 0x400000fb,
782 };
783
784 static unsigned int intel_mac_v5_pin_configs[10] = {
785         0x0321e21f, 0x03a1e02e, 0x9017e110, 0x9017e11f,
786         0x400000fe, 0x0381e020, 0x1345e230, 0x13c5e240,
787         0x400000fc, 0x400000fb,
788 };
789
790
791 static unsigned int *stac922x_brd_tbl[STAC_922X_MODELS] = {
792         [STAC_D945_REF] = ref922x_pin_configs,
793         [STAC_D945GTP3] = d945gtp3_pin_configs,
794         [STAC_D945GTP5] = d945gtp5_pin_configs,
795         [STAC_INTEL_MAC_V1] = intel_mac_v1_pin_configs,
796         [STAC_INTEL_MAC_V2] = intel_mac_v2_pin_configs,
797         [STAC_INTEL_MAC_V3] = intel_mac_v3_pin_configs,
798         [STAC_INTEL_MAC_V4] = intel_mac_v4_pin_configs,
799         [STAC_INTEL_MAC_V5] = intel_mac_v5_pin_configs,
800         /* for backward compatibility */
801         [STAC_MACMINI] = intel_mac_v3_pin_configs,
802         [STAC_MACBOOK] = intel_mac_v5_pin_configs,
803         [STAC_MACBOOK_PRO_V1] = intel_mac_v3_pin_configs,
804         [STAC_MACBOOK_PRO_V2] = intel_mac_v3_pin_configs,
805         [STAC_IMAC_INTEL] = intel_mac_v2_pin_configs,
806         [STAC_IMAC_INTEL_20] = intel_mac_v3_pin_configs,
807         [STAC_922X_DELL_D81] = dell_922x_d81_pin_configs,
808         [STAC_922X_DELL_D82] = dell_922x_d82_pin_configs,       
809         [STAC_922X_DELL_M81] = dell_922x_m81_pin_configs,
810         [STAC_922X_DELL_M82] = dell_922x_m82_pin_configs,       
811 };
812
813 static const char *stac922x_models[STAC_922X_MODELS] = {
814         [STAC_D945_REF] = "ref",
815         [STAC_D945GTP5] = "5stack",
816         [STAC_D945GTP3] = "3stack",
817         [STAC_INTEL_MAC_V1] = "intel-mac-v1",
818         [STAC_INTEL_MAC_V2] = "intel-mac-v2",
819         [STAC_INTEL_MAC_V3] = "intel-mac-v3",
820         [STAC_INTEL_MAC_V4] = "intel-mac-v4",
821         [STAC_INTEL_MAC_V5] = "intel-mac-v5",
822         /* for backward compatibility */
823         [STAC_MACMINI]  = "macmini",
824         [STAC_MACBOOK]  = "macbook",
825         [STAC_MACBOOK_PRO_V1]   = "macbook-pro-v1",
826         [STAC_MACBOOK_PRO_V2]   = "macbook-pro",
827         [STAC_IMAC_INTEL] = "imac-intel",
828         [STAC_IMAC_INTEL_20] = "imac-intel-20",
829         [STAC_922X_DELL_D81] = "dell-d81",
830         [STAC_922X_DELL_D82] = "dell-d82",
831         [STAC_922X_DELL_M81] = "dell-m81",
832         [STAC_922X_DELL_M82] = "dell-m82",
833 };
834
835 static struct snd_pci_quirk stac922x_cfg_tbl[] = {
836         /* SigmaTel reference board */
837         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
838                       "DFI LanParty", STAC_D945_REF),
839         /* Intel 945G based systems */
840         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0101,
841                       "Intel D945G", STAC_D945GTP3),
842         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0202,
843                       "Intel D945G", STAC_D945GTP3),
844         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0606,
845                       "Intel D945G", STAC_D945GTP3),
846         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0601,
847                       "Intel D945G", STAC_D945GTP3),
848         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0111,
849                       "Intel D945G", STAC_D945GTP3),
850         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1115,
851                       "Intel D945G", STAC_D945GTP3),
852         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1116,
853                       "Intel D945G", STAC_D945GTP3),
854         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1117,
855                       "Intel D945G", STAC_D945GTP3),
856         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1118,
857                       "Intel D945G", STAC_D945GTP3),
858         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1119,
859                       "Intel D945G", STAC_D945GTP3),
860         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x8826,
861                       "Intel D945G", STAC_D945GTP3),
862         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x5049,
863                       "Intel D945G", STAC_D945GTP3),
864         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x5055,
865                       "Intel D945G", STAC_D945GTP3),
866         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x5048,
867                       "Intel D945G", STAC_D945GTP3),
868         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0110,
869                       "Intel D945G", STAC_D945GTP3),
870         /* Intel D945G 5-stack systems */
871         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0404,
872                       "Intel D945G", STAC_D945GTP5),
873         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0303,
874                       "Intel D945G", STAC_D945GTP5),
875         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0013,
876                       "Intel D945G", STAC_D945GTP5),
877         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0417,
878                       "Intel D945G", STAC_D945GTP5),
879         /* Intel 945P based systems */
880         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0b0b,
881                       "Intel D945P", STAC_D945GTP3),
882         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0112,
883                       "Intel D945P", STAC_D945GTP3),
884         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0d0d,
885                       "Intel D945P", STAC_D945GTP3),
886         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0909,
887                       "Intel D945P", STAC_D945GTP3),
888         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0505,
889                       "Intel D945P", STAC_D945GTP3),
890         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0707,
891                       "Intel D945P", STAC_D945GTP5),
892         /* other systems  */
893         /* Apple Mac Mini (early 2006) */
894         SND_PCI_QUIRK(0x8384, 0x7680,
895                       "Mac Mini", STAC_INTEL_MAC_V3),
896         /* Dell systems  */
897         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01a7,
898                       "unknown Dell", STAC_922X_DELL_D81),
899         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01a9,
900                       "unknown Dell", STAC_922X_DELL_D81),
901         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ab,
902                       "unknown Dell", STAC_922X_DELL_D81),
903         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ac,
904                       "unknown Dell", STAC_922X_DELL_D82),
905         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01bf,
906                       "unknown Dell", STAC_922X_DELL_M81),
907         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d0,
908                       "unknown Dell", STAC_922X_DELL_D82),
909         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d1,
910                       "unknown Dell", STAC_922X_DELL_D81),
911         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d2,
912                       "unknown Dell", STAC_922X_DELL_D81),
913         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d7,
914                       "Dell XPS M1210", STAC_922X_DELL_M82),
915         {} /* terminator */
916 };
917
918 static unsigned int ref927x_pin_configs[14] = {
919         0x02214020, 0x02a19080, 0x0181304e, 0x01014010,
920         0x01a19040, 0x01011012, 0x01016011, 0x0101201f, 
921         0x183301f0, 0x18a001f0, 0x18a001f0, 0x01442070,
922         0x01c42190, 0x40000100,
923 };
924
925 static unsigned int d965_3st_pin_configs[14] = {
926         0x0221401f, 0x02a19120, 0x40000100, 0x01014011,
927         0x01a19021, 0x01813024, 0x40000100, 0x40000100,
928         0x40000100, 0x40000100, 0x40000100, 0x40000100,
929         0x40000100, 0x40000100
930 };
931
932 static unsigned int d965_5st_pin_configs[14] = {
933         0x02214020, 0x02a19080, 0x0181304e, 0x01014010,
934         0x01a19040, 0x01011012, 0x01016011, 0x40000100,
935         0x40000100, 0x40000100, 0x40000100, 0x01442070,
936         0x40000100, 0x40000100
937 };
938
939 static unsigned int dell_3st_pin_configs[14] = {
940         0x02211230, 0x02a11220, 0x01a19040, 0x01114210,
941         0x01111212, 0x01116211, 0x01813050, 0x01112214,
942         0x403003fa, 0x40000100, 0x40000100, 0x404003fb,
943         0x40c003fc, 0x40000100
944 };
945
946 static unsigned int *stac927x_brd_tbl[STAC_927X_MODELS] = {
947         [STAC_D965_REF] = ref927x_pin_configs,
948         [STAC_D965_3ST] = d965_3st_pin_configs,
949         [STAC_D965_5ST] = d965_5st_pin_configs,
950         [STAC_DELL_3ST] = dell_3st_pin_configs,
951 };
952
953 static const char *stac927x_models[STAC_927X_MODELS] = {
954         [STAC_D965_REF] = "ref",
955         [STAC_D965_3ST] = "3stack",
956         [STAC_D965_5ST] = "5stack",
957         [STAC_DELL_3ST] = "dell-3stack",
958 };
959
960 static struct snd_pci_quirk stac927x_cfg_tbl[] = {
961         /* SigmaTel reference board */
962         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
963                       "DFI LanParty", STAC_D965_REF),
964          /* Intel 946 based systems */
965         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x3d01, "Intel D946", STAC_D965_3ST),
966         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0xa301, "Intel D946", STAC_D965_3ST),
967         /* 965 based 3 stack systems */
968         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2116, "Intel D965", STAC_D965_3ST),
969         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2115, "Intel D965", STAC_D965_3ST),
970         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2114, "Intel D965", STAC_D965_3ST),
971         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2113, "Intel D965", STAC_D965_3ST),
972         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2112, "Intel D965", STAC_D965_3ST),
973         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2111, "Intel D965", STAC_D965_3ST),
974         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2110, "Intel D965", STAC_D965_3ST),
975         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2009, "Intel D965", STAC_D965_3ST),
976         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2008, "Intel D965", STAC_D965_3ST),
977         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2007, "Intel D965", STAC_D965_3ST),
978         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2006, "Intel D965", STAC_D965_3ST),
979         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2005, "Intel D965", STAC_D965_3ST),
980         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2004, "Intel D965", STAC_D965_3ST),
981         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2003, "Intel D965", STAC_D965_3ST),
982         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2002, "Intel D965", STAC_D965_3ST),
983         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2001, "Intel D965", STAC_D965_3ST),
984         /* Dell 3 stack systems */
985         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x01dd, "Dell Dimension E520", STAC_DELL_3ST),
986         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x01ed, "Dell     ", STAC_DELL_3ST),
987         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x01f4, "Dell     ", STAC_DELL_3ST),
988         /* 965 based 5 stack systems */
989         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2301, "Intel D965", STAC_D965_5ST),
990         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2302, "Intel D965", STAC_D965_5ST),
991         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2303, "Intel D965", STAC_D965_5ST),
992         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2304, "Intel D965", STAC_D965_5ST),
993         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2305, "Intel D965", STAC_D965_5ST),
994         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2501, "Intel D965", STAC_D965_5ST),
995         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2502, "Intel D965", STAC_D965_5ST),
996         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2503, "Intel D965", STAC_D965_5ST),
997         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2504, "Intel D965", STAC_D965_5ST),
998         {} /* terminator */
999 };
1000
1001 static unsigned int ref9205_pin_configs[12] = {
1002         0x40000100, 0x40000100, 0x01016011, 0x01014010,
1003         0x01813122, 0x01a19021, 0x40000100, 0x40000100,
1004         0x90a000f0, 0x90a000f0, 0x01441030, 0x01c41030
1005 };
1006
1007 /*
1008     STAC 9205 pin configs for
1009     102801F1
1010     102801F2
1011     102801FC
1012     102801FD
1013     10280204
1014     1028021F
1015 */
1016 static unsigned int dell_9205_m42_pin_configs[12] = {
1017         0x0321101F, 0x03A11020, 0x400003FA, 0x90170310,
1018         0x400003FB, 0x400003FC, 0x400003FD, 0x40F000F9,
1019         0x90A60330, 0x400003FF, 0x0144131F, 0x40C003FE,
1020 };
1021
1022 /*
1023     STAC 9205 pin configs for
1024     102801F9
1025     102801FA
1026     102801FE
1027     102801FF (Dell Precision M4300)
1028     10280206
1029     10280200
1030     10280201
1031 */
1032 static unsigned int dell_9205_m43_pin_configs[12] = {
1033         0x0321101f, 0x03a11020, 0x90a70330, 0x90170310,
1034         0x400000fe, 0x400000ff, 0x400000fd, 0x40f000f9,
1035         0x400000fa, 0x400000fc, 0x0144131f, 0x40c003f8,
1036 };
1037
1038 static unsigned int dell_9205_m44_pin_configs[12] = {
1039         0x0421101f, 0x04a11020, 0x400003fa, 0x90170310,
1040         0x400003fb, 0x400003fc, 0x400003fd, 0x400003f9,
1041         0x90a60330, 0x400003ff, 0x01441340, 0x40c003fe,
1042 };
1043
1044 static unsigned int *stac9205_brd_tbl[STAC_9205_MODELS] = {
1045         [STAC_9205_REF] = ref9205_pin_configs,
1046         [STAC_9205_DELL_M42] = dell_9205_m42_pin_configs,
1047         [STAC_9205_DELL_M43] = dell_9205_m43_pin_configs,
1048         [STAC_9205_DELL_M44] = dell_9205_m44_pin_configs,
1049         [STAC_9205_M43xx] = NULL,
1050 };
1051
1052 static const char *stac9205_models[STAC_9205_MODELS] = {
1053         [STAC_9205_REF] = "ref",
1054         [STAC_9205_DELL_M42] = "dell-m42",
1055         [STAC_9205_DELL_M43] = "dell-m43",
1056         [STAC_9205_DELL_M44] = "dell-m44",
1057 };
1058
1059 static struct snd_pci_quirk stac9205_cfg_tbl[] = {
1060         /* SigmaTel reference board */
1061         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
1062                       "DFI LanParty", STAC_9205_REF),
1063         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f1,
1064                       "unknown Dell", STAC_9205_DELL_M42),
1065         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f2,
1066                       "unknown Dell", STAC_9205_DELL_M42),
1067         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f8,
1068                       "Dell Precision", STAC_9205_M43xx),
1069         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f9,
1070                       "Dell Precision", STAC_9205_DELL_M43),
1071         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01fa,
1072                       "Dell Precision", STAC_9205_DELL_M43),
1073         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01fc,
1074                       "unknown Dell", STAC_9205_DELL_M42),
1075         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01fd,
1076                       "unknown Dell", STAC_9205_DELL_M42),
1077         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01fe,
1078                       "Dell Precision", STAC_9205_DELL_M43),
1079         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ff,
1080                       "Dell Precision M4300", STAC_9205_DELL_M43),
1081         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0206,
1082                       "Dell Precision", STAC_9205_DELL_M43),
1083         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f1,
1084                       "Dell Inspiron", STAC_9205_DELL_M44),
1085         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f2,
1086                       "Dell Inspiron", STAC_9205_DELL_M44),
1087         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01fc,
1088                       "Dell Inspiron", STAC_9205_DELL_M44),
1089         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01fd,
1090                       "Dell Inspiron", STAC_9205_DELL_M44),
1091         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0204,
1092                       "unknown Dell", STAC_9205_DELL_M42),
1093         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x021f,
1094                       "Dell Inspiron", STAC_9205_DELL_M44),
1095         {} /* terminator */
1096 };
1097
1098 static int stac92xx_save_bios_config_regs(struct hda_codec *codec)
1099 {
1100         int i;
1101         struct sigmatel_spec *spec = codec->spec;
1102         
1103         if (! spec->bios_pin_configs) {
1104                 spec->bios_pin_configs = kcalloc(spec->num_pins,
1105                                                  sizeof(*spec->bios_pin_configs), GFP_KERNEL);
1106                 if (! spec->bios_pin_configs)
1107                         return -ENOMEM;
1108         }
1109         
1110         for (i = 0; i < spec->num_pins; i++) {
1111                 hda_nid_t nid = spec->pin_nids[i];
1112                 unsigned int pin_cfg;
1113                 
1114                 pin_cfg = snd_hda_codec_read(codec, nid, 0, 
1115                         AC_VERB_GET_CONFIG_DEFAULT, 0x00);      
1116                 snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x bios pin config %8.8x\n",
1117                                         nid, pin_cfg);
1118                 spec->bios_pin_configs[i] = pin_cfg;
1119         }
1120         
1121         return 0;
1122 }
1123
1124 static void stac92xx_set_config_reg(struct hda_codec *codec,
1125                                     hda_nid_t pin_nid, unsigned int pin_config)
1126 {
1127         int i;
1128         snd_hda_codec_write(codec, pin_nid, 0,
1129                             AC_VERB_SET_CONFIG_DEFAULT_BYTES_0,
1130                             pin_config & 0x000000ff);
1131         snd_hda_codec_write(codec, pin_nid, 0,
1132                             AC_VERB_SET_CONFIG_DEFAULT_BYTES_1,
1133                             (pin_config & 0x0000ff00) >> 8);
1134         snd_hda_codec_write(codec, pin_nid, 0,
1135                             AC_VERB_SET_CONFIG_DEFAULT_BYTES_2,
1136                             (pin_config & 0x00ff0000) >> 16);
1137         snd_hda_codec_write(codec, pin_nid, 0,
1138                             AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
1139                             pin_config >> 24);
1140         i = snd_hda_codec_read(codec, pin_nid, 0,
1141                                AC_VERB_GET_CONFIG_DEFAULT,
1142                                0x00);   
1143         snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x pin config %8.8x\n",
1144                     pin_nid, i);
1145 }
1146
1147 static void stac92xx_set_config_regs(struct hda_codec *codec)
1148 {
1149         int i;
1150         struct sigmatel_spec *spec = codec->spec;
1151
1152         if (!spec->pin_configs)
1153                 return;
1154
1155         for (i = 0; i < spec->num_pins; i++)
1156                 stac92xx_set_config_reg(codec, spec->pin_nids[i],
1157                                         spec->pin_configs[i]);
1158 }
1159
1160 static void stac92xx_enable_gpio_mask(struct hda_codec *codec)
1161 {
1162         struct sigmatel_spec *spec = codec->spec;
1163         /* Configure GPIOx as output */
1164         snd_hda_codec_write_cache(codec, codec->afg, 0,
1165                                   AC_VERB_SET_GPIO_DIRECTION, spec->gpio_mask);
1166         /* Configure GPIOx as CMOS */
1167         snd_hda_codec_write_cache(codec, codec->afg, 0, 0x7e7, 0x00000000);
1168         /* Assert GPIOx */
1169         snd_hda_codec_write_cache(codec, codec->afg, 0,
1170                                   AC_VERB_SET_GPIO_DATA, spec->gpio_data);
1171         /* Enable GPIOx */
1172         snd_hda_codec_write_cache(codec, codec->afg, 0,
1173                                   AC_VERB_SET_GPIO_MASK, spec->gpio_mask);
1174 }
1175
1176 /*
1177  * Analog playback callbacks
1178  */
1179 static int stac92xx_playback_pcm_open(struct hda_pcm_stream *hinfo,
1180                                       struct hda_codec *codec,
1181                                       struct snd_pcm_substream *substream)
1182 {
1183         struct sigmatel_spec *spec = codec->spec;
1184         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
1185 }
1186
1187 static int stac92xx_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1188                                          struct hda_codec *codec,
1189                                          unsigned int stream_tag,
1190                                          unsigned int format,
1191                                          struct snd_pcm_substream *substream)
1192 {
1193         struct sigmatel_spec *spec = codec->spec;
1194         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag, format, substream);
1195 }
1196
1197 static int stac92xx_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1198                                         struct hda_codec *codec,
1199                                         struct snd_pcm_substream *substream)
1200 {
1201         struct sigmatel_spec *spec = codec->spec;
1202         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
1203 }
1204
1205 /*
1206  * Digital playback callbacks
1207  */
1208 static int stac92xx_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1209                                           struct hda_codec *codec,
1210                                           struct snd_pcm_substream *substream)
1211 {
1212         struct sigmatel_spec *spec = codec->spec;
1213         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1214 }
1215
1216 static int stac92xx_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1217                                            struct hda_codec *codec,
1218                                            struct snd_pcm_substream *substream)
1219 {
1220         struct sigmatel_spec *spec = codec->spec;
1221         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1222 }
1223
1224 static int stac92xx_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1225                                          struct hda_codec *codec,
1226                                          unsigned int stream_tag,
1227                                          unsigned int format,
1228                                          struct snd_pcm_substream *substream)
1229 {
1230         struct sigmatel_spec *spec = codec->spec;
1231         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1232                                              stream_tag, format, substream);
1233 }
1234
1235
1236 /*
1237  * Analog capture callbacks
1238  */
1239 static int stac92xx_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1240                                         struct hda_codec *codec,
1241                                         unsigned int stream_tag,
1242                                         unsigned int format,
1243                                         struct snd_pcm_substream *substream)
1244 {
1245         struct sigmatel_spec *spec = codec->spec;
1246
1247         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1248                                    stream_tag, 0, format);
1249         return 0;
1250 }
1251
1252 static int stac92xx_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1253                                         struct hda_codec *codec,
1254                                         struct snd_pcm_substream *substream)
1255 {
1256         struct sigmatel_spec *spec = codec->spec;
1257
1258         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
1259         return 0;
1260 }
1261
1262 static struct hda_pcm_stream stac92xx_pcm_digital_playback = {
1263         .substreams = 1,
1264         .channels_min = 2,
1265         .channels_max = 2,
1266         /* NID is set in stac92xx_build_pcms */
1267         .ops = {
1268                 .open = stac92xx_dig_playback_pcm_open,
1269                 .close = stac92xx_dig_playback_pcm_close,
1270                 .prepare = stac92xx_dig_playback_pcm_prepare
1271         },
1272 };
1273
1274 static struct hda_pcm_stream stac92xx_pcm_digital_capture = {
1275         .substreams = 1,
1276         .channels_min = 2,
1277         .channels_max = 2,
1278         /* NID is set in stac92xx_build_pcms */
1279 };
1280
1281 static struct hda_pcm_stream stac92xx_pcm_analog_playback = {
1282         .substreams = 1,
1283         .channels_min = 2,
1284         .channels_max = 8,
1285         .nid = 0x02, /* NID to query formats and rates */
1286         .ops = {
1287                 .open = stac92xx_playback_pcm_open,
1288                 .prepare = stac92xx_playback_pcm_prepare,
1289                 .cleanup = stac92xx_playback_pcm_cleanup
1290         },
1291 };
1292
1293 static struct hda_pcm_stream stac92xx_pcm_analog_alt_playback = {
1294         .substreams = 1,
1295         .channels_min = 2,
1296         .channels_max = 2,
1297         .nid = 0x06, /* NID to query formats and rates */
1298         .ops = {
1299                 .open = stac92xx_playback_pcm_open,
1300                 .prepare = stac92xx_playback_pcm_prepare,
1301                 .cleanup = stac92xx_playback_pcm_cleanup
1302         },
1303 };
1304
1305 static struct hda_pcm_stream stac92xx_pcm_analog_capture = {
1306         .substreams = 2,
1307         .channels_min = 2,
1308         .channels_max = 2,
1309         /* NID is set in stac92xx_build_pcms */
1310         .ops = {
1311                 .prepare = stac92xx_capture_pcm_prepare,
1312                 .cleanup = stac92xx_capture_pcm_cleanup
1313         },
1314 };
1315
1316 static int stac92xx_build_pcms(struct hda_codec *codec)
1317 {
1318         struct sigmatel_spec *spec = codec->spec;
1319         struct hda_pcm *info = spec->pcm_rec;
1320
1321         codec->num_pcms = 1;
1322         codec->pcm_info = info;
1323
1324         info->name = "STAC92xx Analog";
1325         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_playback;
1326         info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_analog_capture;
1327         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
1328
1329         if (spec->alt_switch) {
1330                 codec->num_pcms++;
1331                 info++;
1332                 info->name = "STAC92xx Analog Alt";
1333                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_alt_playback;
1334         }
1335
1336         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1337                 codec->num_pcms++;
1338                 info++;
1339                 info->name = "STAC92xx Digital";
1340                 if (spec->multiout.dig_out_nid) {
1341                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_digital_playback;
1342                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
1343                 }
1344                 if (spec->dig_in_nid) {
1345                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_digital_capture;
1346                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
1347                 }
1348         }
1349
1350         return 0;
1351 }
1352
1353 static unsigned int stac92xx_get_vref(struct hda_codec *codec, hda_nid_t nid)
1354 {
1355         unsigned int pincap = snd_hda_param_read(codec, nid,
1356                                                  AC_PAR_PIN_CAP);
1357         pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
1358         if (pincap & AC_PINCAP_VREF_100)
1359                 return AC_PINCTL_VREF_100;
1360         if (pincap & AC_PINCAP_VREF_80)
1361                 return AC_PINCTL_VREF_80;
1362         if (pincap & AC_PINCAP_VREF_50)
1363                 return AC_PINCTL_VREF_50;
1364         if (pincap & AC_PINCAP_VREF_GRD)
1365                 return AC_PINCTL_VREF_GRD;
1366         return 0;
1367 }
1368
1369 static void stac92xx_auto_set_pinctl(struct hda_codec *codec, hda_nid_t nid, int pin_type)
1370
1371 {
1372         snd_hda_codec_write_cache(codec, nid, 0,
1373                                   AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
1374 }
1375
1376 #define stac92xx_io_switch_info         snd_ctl_boolean_mono_info
1377
1378 static int stac92xx_io_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1379 {
1380         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1381         struct sigmatel_spec *spec = codec->spec;
1382         int io_idx = kcontrol-> private_value & 0xff;
1383
1384         ucontrol->value.integer.value[0] = spec->io_switch[io_idx];
1385         return 0;
1386 }
1387
1388 static int stac92xx_io_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1389 {
1390         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1391         struct sigmatel_spec *spec = codec->spec;
1392         hda_nid_t nid = kcontrol->private_value >> 8;
1393         int io_idx = kcontrol-> private_value & 0xff;
1394         unsigned short val = ucontrol->value.integer.value[0];
1395
1396         spec->io_switch[io_idx] = val;
1397
1398         if (val)
1399                 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
1400         else {
1401                 unsigned int pinctl = AC_PINCTL_IN_EN;
1402                 if (io_idx) /* set VREF for mic */
1403                         pinctl |= stac92xx_get_vref(codec, nid);
1404                 stac92xx_auto_set_pinctl(codec, nid, pinctl);
1405         }
1406         return 1;
1407 }
1408
1409 #define STAC_CODEC_IO_SWITCH(xname, xpval) \
1410         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1411           .name = xname, \
1412           .index = 0, \
1413           .info = stac92xx_io_switch_info, \
1414           .get = stac92xx_io_switch_get, \
1415           .put = stac92xx_io_switch_put, \
1416           .private_value = xpval, \
1417         }
1418
1419
1420 enum {
1421         STAC_CTL_WIDGET_VOL,
1422         STAC_CTL_WIDGET_MUTE,
1423         STAC_CTL_WIDGET_IO_SWITCH,
1424 };
1425
1426 static struct snd_kcontrol_new stac92xx_control_templates[] = {
1427         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
1428         HDA_CODEC_MUTE(NULL, 0, 0, 0),
1429         STAC_CODEC_IO_SWITCH(NULL, 0),
1430 };
1431
1432 /* add dynamic controls */
1433 static int stac92xx_add_control(struct sigmatel_spec *spec, int type, const char *name, unsigned long val)
1434 {
1435         struct snd_kcontrol_new *knew;
1436
1437         if (spec->num_kctl_used >= spec->num_kctl_alloc) {
1438                 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
1439
1440                 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
1441                 if (! knew)
1442                         return -ENOMEM;
1443                 if (spec->kctl_alloc) {
1444                         memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
1445                         kfree(spec->kctl_alloc);
1446                 }
1447                 spec->kctl_alloc = knew;
1448                 spec->num_kctl_alloc = num;
1449         }
1450
1451         knew = &spec->kctl_alloc[spec->num_kctl_used];
1452         *knew = stac92xx_control_templates[type];
1453         knew->name = kstrdup(name, GFP_KERNEL);
1454         if (! knew->name)
1455                 return -ENOMEM;
1456         knew->private_value = val;
1457         spec->num_kctl_used++;
1458         return 0;
1459 }
1460
1461 /* flag inputs as additional dynamic lineouts */
1462 static int stac92xx_add_dyn_out_pins(struct hda_codec *codec, struct auto_pin_cfg *cfg)
1463 {
1464         struct sigmatel_spec *spec = codec->spec;
1465         unsigned int wcaps, wtype;
1466         int i, num_dacs = 0;
1467         
1468         /* use the wcaps cache to count all DACs available for line-outs */
1469         for (i = 0; i < codec->num_nodes; i++) {
1470                 wcaps = codec->wcaps[i];
1471                 wtype = (wcaps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
1472                 if (wtype == AC_WID_AUD_OUT && !(wcaps & AC_WCAP_DIGITAL))
1473                         num_dacs++;
1474         }
1475
1476         snd_printdd("%s: total dac count=%d\n", __func__, num_dacs);
1477         
1478         switch (cfg->line_outs) {
1479         case 3:
1480                 /* add line-in as side */
1481                 if (cfg->input_pins[AUTO_PIN_LINE] && num_dacs > 3) {
1482                         cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_LINE];
1483                         spec->line_switch = 1;
1484                         cfg->line_outs++;
1485                 }
1486                 break;
1487         case 2:
1488                 /* add line-in as clfe and mic as side */
1489                 if (cfg->input_pins[AUTO_PIN_LINE] && num_dacs > 2) {
1490                         cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_LINE];
1491                         spec->line_switch = 1;
1492                         cfg->line_outs++;
1493                 }
1494                 if (cfg->input_pins[AUTO_PIN_MIC] && num_dacs > 3) {
1495                         cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_MIC];
1496                         spec->mic_switch = 1;
1497                         cfg->line_outs++;
1498                 }
1499                 break;
1500         case 1:
1501                 /* add line-in as surr and mic as clfe */
1502                 if (cfg->input_pins[AUTO_PIN_LINE] && num_dacs > 1) {
1503                         cfg->line_out_pins[1] = cfg->input_pins[AUTO_PIN_LINE];
1504                         spec->line_switch = 1;
1505                         cfg->line_outs++;
1506                 }
1507                 if (cfg->input_pins[AUTO_PIN_MIC] && num_dacs > 2) {
1508                         cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_MIC];
1509                         spec->mic_switch = 1;
1510                         cfg->line_outs++;
1511                 }
1512                 break;
1513         }
1514
1515         return 0;
1516 }
1517
1518
1519 static int is_in_dac_nids(struct sigmatel_spec *spec, hda_nid_t nid)
1520 {
1521         int i;
1522         
1523         for (i = 0; i < spec->multiout.num_dacs; i++) {
1524                 if (spec->multiout.dac_nids[i] == nid)
1525                         return 1;
1526         }
1527
1528         return 0;
1529 }
1530
1531 /*
1532  * Fill in the dac_nids table from the parsed pin configuration
1533  * This function only works when every pin in line_out_pins[]
1534  * contains atleast one DAC in its connection list. Some 92xx
1535  * codecs are not connected directly to a DAC, such as the 9200
1536  * and 9202/925x. For those, dac_nids[] must be hard-coded.
1537  */
1538 static int stac92xx_auto_fill_dac_nids(struct hda_codec *codec,
1539                                        struct auto_pin_cfg *cfg)
1540 {
1541         struct sigmatel_spec *spec = codec->spec;
1542         int i, j, conn_len = 0; 
1543         hda_nid_t nid, conn[HDA_MAX_CONNECTIONS];
1544         unsigned int wcaps, wtype;
1545         
1546         for (i = 0; i < cfg->line_outs; i++) {
1547                 nid = cfg->line_out_pins[i];
1548                 conn_len = snd_hda_get_connections(codec, nid, conn,
1549                                                    HDA_MAX_CONNECTIONS);
1550                 for (j = 0; j < conn_len; j++) {
1551                         wcaps = snd_hda_param_read(codec, conn[j],
1552                                                    AC_PAR_AUDIO_WIDGET_CAP);
1553                         wtype = (wcaps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
1554
1555                         if (wtype != AC_WID_AUD_OUT ||
1556                             (wcaps & AC_WCAP_DIGITAL))
1557                                 continue;
1558                         /* conn[j] is a DAC routed to this line-out */
1559                         if (!is_in_dac_nids(spec, conn[j]))
1560                                 break;
1561                 }
1562
1563                 if (j == conn_len) {
1564                         if (spec->multiout.num_dacs > 0) {
1565                                 /* we have already working output pins,
1566                                  * so let's drop the broken ones again
1567                                  */
1568                                 cfg->line_outs = spec->multiout.num_dacs;
1569                                 break;
1570                         }
1571                         /* error out, no available DAC found */
1572                         snd_printk(KERN_ERR
1573                                    "%s: No available DAC for pin 0x%x\n",
1574                                    __func__, nid);
1575                         return -ENODEV;
1576                 }
1577
1578                 spec->multiout.dac_nids[i] = conn[j];
1579                 spec->multiout.num_dacs++;
1580                 if (conn_len > 1) {
1581                         /* select this DAC in the pin's input mux */
1582                         snd_hda_codec_write_cache(codec, nid, 0,
1583                                                   AC_VERB_SET_CONNECT_SEL, j);
1584
1585                 }
1586         }
1587
1588         snd_printd("dac_nids=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
1589                    spec->multiout.num_dacs,
1590                    spec->multiout.dac_nids[0],
1591                    spec->multiout.dac_nids[1],
1592                    spec->multiout.dac_nids[2],
1593                    spec->multiout.dac_nids[3],
1594                    spec->multiout.dac_nids[4]);
1595         return 0;
1596 }
1597
1598 /* create volume control/switch for the given prefx type */
1599 static int create_controls(struct sigmatel_spec *spec, const char *pfx, hda_nid_t nid, int chs)
1600 {
1601         char name[32];
1602         int err;
1603
1604         sprintf(name, "%s Playback Volume", pfx);
1605         err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, name,
1606                                    HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1607         if (err < 0)
1608                 return err;
1609         sprintf(name, "%s Playback Switch", pfx);
1610         err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, name,
1611                                    HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1612         if (err < 0)
1613                 return err;
1614         return 0;
1615 }
1616
1617 /* add playback controls from the parsed DAC table */
1618 static int stac92xx_auto_create_multi_out_ctls(struct sigmatel_spec *spec,
1619                                                const struct auto_pin_cfg *cfg)
1620 {
1621         static const char *chname[4] = {
1622                 "Front", "Surround", NULL /*CLFE*/, "Side"
1623         };
1624         hda_nid_t nid;
1625         int i, err;
1626
1627         for (i = 0; i < cfg->line_outs; i++) {
1628                 if (!spec->multiout.dac_nids[i])
1629                         continue;
1630
1631                 nid = spec->multiout.dac_nids[i];
1632
1633                 if (i == 2) {
1634                         /* Center/LFE */
1635                         err = create_controls(spec, "Center", nid, 1);
1636                         if (err < 0)
1637                                 return err;
1638                         err = create_controls(spec, "LFE", nid, 2);
1639                         if (err < 0)
1640                                 return err;
1641                 } else {
1642                         err = create_controls(spec, chname[i], nid, 3);
1643                         if (err < 0)
1644                                 return err;
1645                 }
1646         }
1647
1648         if (spec->line_switch)
1649                 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Line In as Output Switch", cfg->input_pins[AUTO_PIN_LINE] << 8)) < 0)
1650                         return err;
1651
1652         if (spec->mic_switch)
1653                 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Mic as Output Switch", (cfg->input_pins[AUTO_PIN_MIC] << 8) | 1)) < 0)
1654                         return err;
1655
1656         return 0;
1657 }
1658
1659 static int check_in_dac_nids(struct sigmatel_spec *spec, hda_nid_t nid)
1660 {
1661         if (is_in_dac_nids(spec, nid))
1662                 return 1;
1663         if (spec->multiout.hp_nid == nid)
1664                 return 1;
1665         return 0;
1666 }
1667
1668 static int add_spec_dacs(struct sigmatel_spec *spec, hda_nid_t nid)
1669 {
1670         if (!spec->multiout.hp_nid)
1671                 spec->multiout.hp_nid = nid;
1672         else if (spec->multiout.num_dacs > 4) {
1673                 printk(KERN_WARNING "stac92xx: No space for DAC 0x%x\n", nid);
1674                 return 1;
1675         } else {
1676                 spec->multiout.dac_nids[spec->multiout.num_dacs] = nid;
1677                 spec->multiout.num_dacs++;
1678         }
1679         return 0;
1680 }
1681
1682 /* add playback controls for Speaker and HP outputs */
1683 static int stac92xx_auto_create_hp_ctls(struct hda_codec *codec,
1684                                         struct auto_pin_cfg *cfg)
1685 {
1686         struct sigmatel_spec *spec = codec->spec;
1687         hda_nid_t nid;
1688         int i, old_num_dacs, err;
1689
1690         old_num_dacs = spec->multiout.num_dacs;
1691         for (i = 0; i < cfg->hp_outs; i++) {
1692                 unsigned int wid_caps = get_wcaps(codec, cfg->hp_pins[i]);
1693                 if (wid_caps & AC_WCAP_UNSOL_CAP)
1694                         spec->hp_detect = 1;
1695                 nid = snd_hda_codec_read(codec, cfg->hp_pins[i], 0,
1696                                          AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
1697                 if (check_in_dac_nids(spec, nid))
1698                         nid = 0;
1699                 if (! nid)
1700                         continue;
1701                 add_spec_dacs(spec, nid);
1702         }
1703         for (i = 0; i < cfg->speaker_outs; i++) {
1704                 nid = snd_hda_codec_read(codec, cfg->speaker_pins[i], 0,
1705                                          AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
1706                 if (check_in_dac_nids(spec, nid))
1707                         nid = 0;
1708                 if (! nid)
1709                         continue;
1710                 add_spec_dacs(spec, nid);
1711         }
1712         for (i = 0; i < cfg->line_outs; i++) {
1713                 nid = snd_hda_codec_read(codec, cfg->line_out_pins[i], 0,
1714                                         AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
1715                 if (check_in_dac_nids(spec, nid))
1716                         nid = 0;
1717                 if (! nid)
1718                         continue;
1719                 add_spec_dacs(spec, nid);
1720         }
1721         for (i = old_num_dacs; i < spec->multiout.num_dacs; i++) {
1722                 static const char *pfxs[] = {
1723                         "Speaker", "External Speaker", "Speaker2",
1724                 };
1725                 err = create_controls(spec, pfxs[i - old_num_dacs],
1726                                       spec->multiout.dac_nids[i], 3);
1727                 if (err < 0)
1728                         return err;
1729         }
1730         if (spec->multiout.hp_nid) {
1731                 const char *pfx;
1732                 if (old_num_dacs == spec->multiout.num_dacs)
1733                         pfx = "Master";
1734                 else
1735                         pfx = "Headphone";
1736                 err = create_controls(spec, pfx, spec->multiout.hp_nid, 3);
1737                 if (err < 0)
1738                         return err;
1739         }
1740
1741         return 0;
1742 }
1743
1744 /* labels for dmic mux inputs */
1745 static const char *stac92xx_dmic_labels[5] = {
1746         "Analog Inputs", "Digital Mic 1", "Digital Mic 2",
1747         "Digital Mic 3", "Digital Mic 4"
1748 };
1749
1750 /* create playback/capture controls for input pins on dmic capable codecs */
1751 static int stac92xx_auto_create_dmic_input_ctls(struct hda_codec *codec,
1752                                                 const struct auto_pin_cfg *cfg)
1753 {
1754         struct sigmatel_spec *spec = codec->spec;
1755         struct hda_input_mux *dimux = &spec->private_dimux;
1756         hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
1757         int i, j;
1758
1759         dimux->items[dimux->num_items].label = stac92xx_dmic_labels[0];
1760         dimux->items[dimux->num_items].index = 0;
1761         dimux->num_items++;
1762
1763         for (i = 0; i < spec->num_dmics; i++) {
1764                 int index;
1765                 int num_cons;
1766                 unsigned int def_conf;
1767
1768                 def_conf = snd_hda_codec_read(codec,
1769                                               spec->dmic_nids[i],
1770                                               0,
1771                                               AC_VERB_GET_CONFIG_DEFAULT,
1772                                               0);
1773                 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
1774                         continue;
1775
1776                 num_cons = snd_hda_get_connections(codec,
1777                                 spec->dmux_nid,
1778                                 con_lst,
1779                                 HDA_MAX_NUM_INPUTS);
1780                 for (j = 0; j < num_cons; j++)
1781                         if (con_lst[j] == spec->dmic_nids[i]) {
1782                                 index = j;
1783                                 goto found;
1784                         }
1785                 continue;
1786 found:
1787                 dimux->items[dimux->num_items].label =
1788                         stac92xx_dmic_labels[dimux->num_items];
1789                 dimux->items[dimux->num_items].index = index;
1790                 dimux->num_items++;
1791         }
1792
1793         return 0;
1794 }
1795
1796 /* create playback/capture controls for input pins */
1797 static int stac92xx_auto_create_analog_input_ctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
1798 {
1799         struct sigmatel_spec *spec = codec->spec;
1800         struct hda_input_mux *imux = &spec->private_imux;
1801         hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
1802         int i, j, k;
1803
1804         for (i = 0; i < AUTO_PIN_LAST; i++) {
1805                 int index;
1806
1807                 if (!cfg->input_pins[i])
1808                         continue;
1809                 index = -1;
1810                 for (j = 0; j < spec->num_muxes; j++) {
1811                         int num_cons;
1812                         num_cons = snd_hda_get_connections(codec,
1813                                                            spec->mux_nids[j],
1814                                                            con_lst,
1815                                                            HDA_MAX_NUM_INPUTS);
1816                         for (k = 0; k < num_cons; k++)
1817                                 if (con_lst[k] == cfg->input_pins[i]) {
1818                                         index = k;
1819                                         goto found;
1820                                 }
1821                 }
1822                 continue;
1823         found:
1824                 imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
1825                 imux->items[imux->num_items].index = index;
1826                 imux->num_items++;
1827         }
1828
1829         if (imux->num_items) {
1830                 /*
1831                  * Set the current input for the muxes.
1832                  * The STAC9221 has two input muxes with identical source
1833                  * NID lists.  Hopefully this won't get confused.
1834                  */
1835                 for (i = 0; i < spec->num_muxes; i++) {
1836                         snd_hda_codec_write_cache(codec, spec->mux_nids[i], 0,
1837                                                   AC_VERB_SET_CONNECT_SEL,
1838                                                   imux->items[0].index);
1839                 }
1840         }
1841
1842         return 0;
1843 }
1844
1845 static void stac92xx_auto_init_multi_out(struct hda_codec *codec)
1846 {
1847         struct sigmatel_spec *spec = codec->spec;
1848         int i;
1849
1850         for (i = 0; i < spec->autocfg.line_outs; i++) {
1851                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
1852                 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
1853         }
1854 }
1855
1856 static void stac92xx_auto_init_hp_out(struct hda_codec *codec)
1857 {
1858         struct sigmatel_spec *spec = codec->spec;
1859         int i;
1860
1861         for (i = 0; i < spec->autocfg.hp_outs; i++) {
1862                 hda_nid_t pin;
1863                 pin = spec->autocfg.hp_pins[i];
1864                 if (pin) /* connect to front */
1865                         stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
1866         }
1867         for (i = 0; i < spec->autocfg.speaker_outs; i++) {
1868                 hda_nid_t pin;
1869                 pin = spec->autocfg.speaker_pins[i];
1870                 if (pin) /* connect to front */
1871                         stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN);
1872         }
1873 }
1874
1875 static int stac92xx_parse_auto_config(struct hda_codec *codec, hda_nid_t dig_out, hda_nid_t dig_in)
1876 {
1877         struct sigmatel_spec *spec = codec->spec;
1878         int err;
1879
1880         if ((err = snd_hda_parse_pin_def_config(codec,
1881                                                 &spec->autocfg,
1882                                                 spec->dmic_nids)) < 0)
1883                 return err;
1884         if (! spec->autocfg.line_outs)
1885                 return 0; /* can't find valid pin config */
1886
1887         if ((err = stac92xx_add_dyn_out_pins(codec, &spec->autocfg)) < 0)
1888                 return err;
1889         if (spec->multiout.num_dacs == 0)
1890                 if ((err = stac92xx_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
1891                         return err;
1892
1893         if ((err = stac92xx_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
1894             (err = stac92xx_auto_create_hp_ctls(codec, &spec->autocfg)) < 0 ||
1895             (err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
1896                 return err;
1897
1898         if (spec->num_dmics > 0)
1899                 if ((err = stac92xx_auto_create_dmic_input_ctls(codec,
1900                                                 &spec->autocfg)) < 0)
1901                         return err;
1902
1903         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
1904         if (spec->multiout.max_channels > 2)
1905                 spec->surr_switch = 1;
1906
1907         if (spec->autocfg.dig_out_pin)
1908                 spec->multiout.dig_out_nid = dig_out;
1909         if (spec->autocfg.dig_in_pin)
1910                 spec->dig_in_nid = dig_in;
1911
1912         if (spec->kctl_alloc)
1913                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
1914
1915         spec->input_mux = &spec->private_imux;
1916         spec->dinput_mux = &spec->private_dimux;
1917
1918         return 1;
1919 }
1920
1921 /* add playback controls for HP output */
1922 static int stac9200_auto_create_hp_ctls(struct hda_codec *codec,
1923                                         struct auto_pin_cfg *cfg)
1924 {
1925         struct sigmatel_spec *spec = codec->spec;
1926         hda_nid_t pin = cfg->hp_pins[0];
1927         unsigned int wid_caps;
1928
1929         if (! pin)
1930                 return 0;
1931
1932         wid_caps = get_wcaps(codec, pin);
1933         if (wid_caps & AC_WCAP_UNSOL_CAP)
1934                 spec->hp_detect = 1;
1935
1936         return 0;
1937 }
1938
1939 /* add playback controls for LFE output */
1940 static int stac9200_auto_create_lfe_ctls(struct hda_codec *codec,
1941                                         struct auto_pin_cfg *cfg)
1942 {
1943         struct sigmatel_spec *spec = codec->spec;
1944         int err;
1945         hda_nid_t lfe_pin = 0x0;
1946         int i;
1947
1948         /*
1949          * search speaker outs and line outs for a mono speaker pin
1950          * with an amp.  If one is found, add LFE controls
1951          * for it.
1952          */
1953         for (i = 0; i < spec->autocfg.speaker_outs && lfe_pin == 0x0; i++) {
1954                 hda_nid_t pin = spec->autocfg.speaker_pins[i];
1955                 unsigned long wcaps = get_wcaps(codec, pin);
1956                 wcaps &= (AC_WCAP_STEREO | AC_WCAP_OUT_AMP);
1957                 if (wcaps == AC_WCAP_OUT_AMP)
1958                         /* found a mono speaker with an amp, must be lfe */
1959                         lfe_pin = pin;
1960         }
1961
1962         /* if speaker_outs is 0, then speakers may be in line_outs */
1963         if (lfe_pin == 0 && spec->autocfg.speaker_outs == 0) {
1964                 for (i = 0; i < spec->autocfg.line_outs && lfe_pin == 0x0; i++) {
1965                         hda_nid_t pin = spec->autocfg.line_out_pins[i];
1966                         unsigned long cfg;
1967                         cfg = snd_hda_codec_read(codec, pin, 0,
1968                                                  AC_VERB_GET_CONFIG_DEFAULT,
1969                                                  0x00);
1970                         if (get_defcfg_device(cfg) == AC_JACK_SPEAKER) {
1971                                 unsigned long wcaps = get_wcaps(codec, pin);
1972                                 wcaps &= (AC_WCAP_STEREO | AC_WCAP_OUT_AMP);
1973                                 if (wcaps == AC_WCAP_OUT_AMP)
1974                                         /* found a mono speaker with an amp,
1975                                            must be lfe */
1976                                         lfe_pin = pin;
1977                         }
1978                 }
1979         }
1980
1981         if (lfe_pin) {
1982                 err = create_controls(spec, "LFE", lfe_pin, 1);
1983                 if (err < 0)
1984                         return err;
1985         }
1986
1987         return 0;
1988 }
1989
1990 static int stac9200_parse_auto_config(struct hda_codec *codec)
1991 {
1992         struct sigmatel_spec *spec = codec->spec;
1993         int err;
1994
1995         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
1996                 return err;
1997
1998         if ((err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
1999                 return err;
2000
2001         if ((err = stac9200_auto_create_hp_ctls(codec, &spec->autocfg)) < 0)
2002                 return err;
2003
2004         if ((err = stac9200_auto_create_lfe_ctls(codec, &spec->autocfg)) < 0)
2005                 return err;
2006
2007         if (spec->autocfg.dig_out_pin)
2008                 spec->multiout.dig_out_nid = 0x05;
2009         if (spec->autocfg.dig_in_pin)
2010                 spec->dig_in_nid = 0x04;
2011
2012         if (spec->kctl_alloc)
2013                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
2014
2015         spec->input_mux = &spec->private_imux;
2016         spec->dinput_mux = &spec->private_dimux;
2017
2018         return 1;
2019 }
2020
2021 /*
2022  * Early 2006 Intel Macintoshes with STAC9220X5 codecs seem to have a
2023  * funky external mute control using GPIO pins.
2024  */
2025
2026 static void stac922x_gpio_mute(struct hda_codec *codec, int pin, int muted)
2027 {
2028         unsigned int gpiostate, gpiomask, gpiodir;
2029
2030         gpiostate = snd_hda_codec_read(codec, codec->afg, 0,
2031                                        AC_VERB_GET_GPIO_DATA, 0);
2032
2033         if (!muted)
2034                 gpiostate |= (1 << pin);
2035         else
2036                 gpiostate &= ~(1 << pin);
2037
2038         gpiomask = snd_hda_codec_read(codec, codec->afg, 0,
2039                                       AC_VERB_GET_GPIO_MASK, 0);
2040         gpiomask |= (1 << pin);
2041
2042         gpiodir = snd_hda_codec_read(codec, codec->afg, 0,
2043                                      AC_VERB_GET_GPIO_DIRECTION, 0);
2044         gpiodir |= (1 << pin);
2045
2046         /* AppleHDA seems to do this -- WTF is this verb?? */
2047         snd_hda_codec_write(codec, codec->afg, 0, 0x7e7, 0);
2048
2049         snd_hda_codec_write(codec, codec->afg, 0,
2050                             AC_VERB_SET_GPIO_MASK, gpiomask);
2051         snd_hda_codec_write(codec, codec->afg, 0,
2052                             AC_VERB_SET_GPIO_DIRECTION, gpiodir);
2053
2054         msleep(1);
2055
2056         snd_hda_codec_write(codec, codec->afg, 0,
2057                             AC_VERB_SET_GPIO_DATA, gpiostate);
2058 }
2059
2060 static void enable_pin_detect(struct hda_codec *codec, hda_nid_t nid,
2061                               unsigned int event)
2062 {
2063         if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP)
2064                 snd_hda_codec_write(codec, nid, 0,
2065                                     AC_VERB_SET_UNSOLICITED_ENABLE,
2066                                     (AC_USRSP_EN | event));
2067 }
2068
2069 static int stac92xx_init(struct hda_codec *codec)
2070 {
2071         struct sigmatel_spec *spec = codec->spec;
2072         struct auto_pin_cfg *cfg = &spec->autocfg;
2073         int i;
2074
2075         snd_hda_sequence_write(codec, spec->init);
2076
2077         /* set up pins */
2078         if (spec->hp_detect) {
2079                 /* Enable unsolicited responses on the HP widget */
2080                 for (i = 0; i < cfg->hp_outs; i++)
2081                         enable_pin_detect(codec, cfg->hp_pins[i],
2082                                           STAC_HP_EVENT);
2083                 /* force to enable the first line-out; the others are set up
2084                  * in unsol_event
2085                  */
2086                 stac92xx_auto_set_pinctl(codec, spec->autocfg.line_out_pins[0],
2087                                          AC_PINCTL_OUT_EN);
2088                 stac92xx_auto_init_hp_out(codec);
2089                 /* fake event to set up pins */
2090                 codec->patch_ops.unsol_event(codec, STAC_HP_EVENT << 26);
2091         } else {
2092                 stac92xx_auto_init_multi_out(codec);
2093                 stac92xx_auto_init_hp_out(codec);
2094         }
2095         for (i = 0; i < AUTO_PIN_LAST; i++) {
2096                 hda_nid_t nid = cfg->input_pins[i];
2097                 if (nid) {
2098                         unsigned int pinctl = AC_PINCTL_IN_EN;
2099                         if (i == AUTO_PIN_MIC || i == AUTO_PIN_FRONT_MIC)
2100                                 pinctl |= stac92xx_get_vref(codec, nid);
2101                         stac92xx_auto_set_pinctl(codec, nid, pinctl);
2102                 }
2103         }
2104         if (spec->num_dmics > 0)
2105                 for (i = 0; i < spec->num_dmics; i++)
2106                         stac92xx_auto_set_pinctl(codec, spec->dmic_nids[i],
2107                                                  AC_PINCTL_IN_EN);
2108
2109         if (cfg->dig_out_pin)
2110                 stac92xx_auto_set_pinctl(codec, cfg->dig_out_pin,
2111                                          AC_PINCTL_OUT_EN);
2112         if (cfg->dig_in_pin)
2113                 stac92xx_auto_set_pinctl(codec, cfg->dig_in_pin,
2114                                          AC_PINCTL_IN_EN);
2115
2116         if (spec->gpio_mute) {
2117                 stac922x_gpio_mute(codec, 0, 0);
2118                 stac922x_gpio_mute(codec, 1, 0);
2119         }
2120
2121         return 0;
2122 }
2123
2124 static void stac92xx_free(struct hda_codec *codec)
2125 {
2126         struct sigmatel_spec *spec = codec->spec;
2127         int i;
2128
2129         if (! spec)
2130                 return;
2131
2132         if (spec->kctl_alloc) {
2133                 for (i = 0; i < spec->num_kctl_used; i++)
2134                         kfree(spec->kctl_alloc[i].name);
2135                 kfree(spec->kctl_alloc);
2136         }
2137
2138         if (spec->bios_pin_configs)
2139                 kfree(spec->bios_pin_configs);
2140
2141         kfree(spec);
2142 }
2143
2144 static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
2145                                 unsigned int flag)
2146 {
2147         unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
2148                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
2149
2150         if (pin_ctl & AC_PINCTL_IN_EN) {
2151                 /*
2152                  * we need to check the current set-up direction of
2153                  * shared input pins since they can be switched via
2154                  * "xxx as Output" mixer switch
2155                  */
2156                 struct sigmatel_spec *spec = codec->spec;
2157                 struct auto_pin_cfg *cfg = &spec->autocfg;
2158                 if ((nid == cfg->input_pins[AUTO_PIN_LINE] &&
2159                      spec->line_switch) ||
2160                     (nid == cfg->input_pins[AUTO_PIN_MIC] &&
2161                      spec->mic_switch))
2162                         return;
2163         }
2164
2165         /* if setting pin direction bits, clear the current
2166            direction bits first */
2167         if (flag & (AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN))
2168                 pin_ctl &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
2169         
2170         snd_hda_codec_write_cache(codec, nid, 0,
2171                         AC_VERB_SET_PIN_WIDGET_CONTROL,
2172                         pin_ctl | flag);
2173 }
2174
2175 static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
2176                                   unsigned int flag)
2177 {
2178         unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
2179                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
2180         snd_hda_codec_write_cache(codec, nid, 0,
2181                         AC_VERB_SET_PIN_WIDGET_CONTROL,
2182                         pin_ctl & ~flag);
2183 }
2184
2185 static int get_pin_presence(struct hda_codec *codec, hda_nid_t nid)
2186 {
2187         if (!nid)
2188                 return 0;
2189         if (snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_PIN_SENSE, 0x00)
2190             & (1 << 31))
2191                 return 1;
2192         return 0;
2193 }
2194
2195 static void stac92xx_hp_detect(struct hda_codec *codec, unsigned int res)
2196 {
2197         struct sigmatel_spec *spec = codec->spec;
2198         struct auto_pin_cfg *cfg = &spec->autocfg;
2199         int i, presence;
2200
2201         presence = 0;
2202         for (i = 0; i < cfg->hp_outs; i++) {
2203                 presence = get_pin_presence(codec, cfg->hp_pins[i]);
2204                 if (presence)
2205                         break;
2206         }
2207
2208         if (presence) {
2209                 /* disable lineouts, enable hp */
2210                 for (i = 0; i < cfg->line_outs; i++)
2211                         stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
2212                                                 AC_PINCTL_OUT_EN);
2213                 for (i = 0; i < cfg->speaker_outs; i++)
2214                         stac92xx_reset_pinctl(codec, cfg->speaker_pins[i],
2215                                                 AC_PINCTL_OUT_EN);
2216         } else {
2217                 /* enable lineouts, disable hp */
2218                 for (i = 0; i < cfg->line_outs; i++)
2219                         stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
2220                                                 AC_PINCTL_OUT_EN);
2221                 for (i = 0; i < cfg->speaker_outs; i++)
2222                         stac92xx_set_pinctl(codec, cfg->speaker_pins[i],
2223                                                 AC_PINCTL_OUT_EN);
2224         }
2225
2226
2227 static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
2228 {
2229         switch (res >> 26) {
2230         case STAC_HP_EVENT:
2231                 stac92xx_hp_detect(codec, res);
2232                 break;
2233         }
2234 }
2235
2236 #ifdef SND_HDA_NEEDS_RESUME
2237 static int stac92xx_resume(struct hda_codec *codec)
2238 {
2239         stac92xx_set_config_regs(codec);
2240         stac92xx_init(codec);
2241         snd_hda_codec_resume_amp(codec);
2242         snd_hda_codec_resume_cache(codec);
2243         return 0;
2244 }
2245 #endif
2246
2247 static struct hda_codec_ops stac92xx_patch_ops = {
2248         .build_controls = stac92xx_build_controls,
2249         .build_pcms = stac92xx_build_pcms,
2250         .init = stac92xx_init,
2251         .free = stac92xx_free,
2252         .unsol_event = stac92xx_unsol_event,
2253 #ifdef SND_HDA_NEEDS_RESUME
2254         .resume = stac92xx_resume,
2255 #endif
2256 };
2257
2258 static int patch_stac9200(struct hda_codec *codec)
2259 {
2260         struct sigmatel_spec *spec;
2261         int err;
2262
2263         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2264         if (spec == NULL)
2265                 return -ENOMEM;
2266
2267         codec->spec = spec;
2268         spec->num_pins = ARRAY_SIZE(stac9200_pin_nids);
2269         spec->pin_nids = stac9200_pin_nids;
2270         spec->board_config = snd_hda_check_board_config(codec, STAC_9200_MODELS,
2271                                                         stac9200_models,
2272                                                         stac9200_cfg_tbl);
2273         if (spec->board_config < 0) {
2274                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9200, using BIOS defaults\n");
2275                 err = stac92xx_save_bios_config_regs(codec);
2276                 if (err < 0) {
2277                         stac92xx_free(codec);
2278                         return err;
2279                 }
2280                 spec->pin_configs = spec->bios_pin_configs;
2281         } else {
2282                 spec->pin_configs = stac9200_brd_tbl[spec->board_config];
2283                 stac92xx_set_config_regs(codec);
2284         }
2285
2286         spec->multiout.max_channels = 2;
2287         spec->multiout.num_dacs = 1;
2288         spec->multiout.dac_nids = stac9200_dac_nids;
2289         spec->adc_nids = stac9200_adc_nids;
2290         spec->mux_nids = stac9200_mux_nids;
2291         spec->num_muxes = 1;
2292         spec->num_dmics = 0;
2293
2294         spec->init = stac9200_core_init;
2295         spec->mixer = stac9200_mixer;
2296
2297         err = stac9200_parse_auto_config(codec);
2298         if (err < 0) {
2299                 stac92xx_free(codec);
2300                 return err;
2301         }
2302
2303         codec->patch_ops = stac92xx_patch_ops;
2304
2305         return 0;
2306 }
2307
2308 static int patch_stac925x(struct hda_codec *codec)
2309 {
2310         struct sigmatel_spec *spec;
2311         int err;
2312
2313         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2314         if (spec == NULL)
2315                 return -ENOMEM;
2316
2317         codec->spec = spec;
2318         spec->num_pins = ARRAY_SIZE(stac925x_pin_nids);
2319         spec->pin_nids = stac925x_pin_nids;
2320         spec->board_config = snd_hda_check_board_config(codec, STAC_925x_MODELS,
2321                                                         stac925x_models,
2322                                                         stac925x_cfg_tbl);
2323  again:
2324         if (spec->board_config < 0) {
2325                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC925x," 
2326                                       "using BIOS defaults\n");
2327                 err = stac92xx_save_bios_config_regs(codec);
2328                 if (err < 0) {
2329                         stac92xx_free(codec);
2330                         return err;
2331                 }
2332                 spec->pin_configs = spec->bios_pin_configs;
2333         } else if (stac925x_brd_tbl[spec->board_config] != NULL){
2334                 spec->pin_configs = stac925x_brd_tbl[spec->board_config];
2335                 stac92xx_set_config_regs(codec);
2336         }
2337
2338         spec->multiout.max_channels = 2;
2339         spec->multiout.num_dacs = 1;
2340         spec->multiout.dac_nids = stac925x_dac_nids;
2341         spec->adc_nids = stac925x_adc_nids;
2342         spec->mux_nids = stac925x_mux_nids;
2343         spec->num_muxes = 1;
2344         switch (codec->vendor_id) {
2345         case 0x83847632: /* STAC9202  */
2346         case 0x83847633: /* STAC9202D */
2347         case 0x83847636: /* STAC9251  */
2348         case 0x83847637: /* STAC9251D */
2349                 spec->num_dmics = 1;
2350                 spec->dmic_nids = stac925x_dmic_nids;
2351                 break;
2352         default:
2353                 spec->num_dmics = 0;
2354                 break;
2355         }
2356
2357         spec->init = stac925x_core_init;
2358         spec->mixer = stac925x_mixer;
2359
2360         err = stac92xx_parse_auto_config(codec, 0x8, 0x7);
2361         if (!err) {
2362                 if (spec->board_config < 0) {
2363                         printk(KERN_WARNING "hda_codec: No auto-config is "
2364                                "available, default to model=ref\n");
2365                         spec->board_config = STAC_925x_REF;
2366                         goto again;
2367                 }
2368                 err = -EINVAL;
2369         }
2370         if (err < 0) {
2371                 stac92xx_free(codec);
2372                 return err;
2373         }
2374
2375         codec->patch_ops = stac92xx_patch_ops;
2376
2377         return 0;
2378 }
2379
2380 static int patch_stac922x(struct hda_codec *codec)
2381 {
2382         struct sigmatel_spec *spec;
2383         int err;
2384
2385         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2386         if (spec == NULL)
2387                 return -ENOMEM;
2388
2389         codec->spec = spec;
2390         spec->num_pins = ARRAY_SIZE(stac922x_pin_nids);
2391         spec->pin_nids = stac922x_pin_nids;
2392         spec->board_config = snd_hda_check_board_config(codec, STAC_922X_MODELS,
2393                                                         stac922x_models,
2394                                                         stac922x_cfg_tbl);
2395         if (spec->board_config == STAC_INTEL_MAC_V3) {
2396                 spec->gpio_mute = 1;
2397                 /* Intel Macs have all same PCI SSID, so we need to check
2398                  * codec SSID to distinguish the exact models
2399                  */
2400                 printk(KERN_INFO "hda_codec: STAC922x, Apple subsys_id=%x\n", codec->subsystem_id);
2401                 switch (codec->subsystem_id) {
2402
2403                 case 0x106b0800:
2404                         spec->board_config = STAC_INTEL_MAC_V1;
2405                         break;
2406                 case 0x106b0600:
2407                 case 0x106b0700:
2408                         spec->board_config = STAC_INTEL_MAC_V2;
2409                         break;
2410                 case 0x106b0e00:
2411                 case 0x106b0f00:
2412                 case 0x106b1600:
2413                 case 0x106b1700:
2414                 case 0x106b0200:
2415                 case 0x106b1e00:
2416                         spec->board_config = STAC_INTEL_MAC_V3;
2417                         break;
2418                 case 0x106b1a00:
2419                 case 0x00000100:
2420                         spec->board_config = STAC_INTEL_MAC_V4;
2421                         break;
2422                 case 0x106b0a00:
2423                 case 0x106b2200:
2424                         spec->board_config = STAC_INTEL_MAC_V5;
2425                         break;
2426                 }
2427         }
2428
2429  again:
2430         if (spec->board_config < 0) {
2431                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC922x, "
2432                         "using BIOS defaults\n");
2433                 err = stac92xx_save_bios_config_regs(codec);
2434                 if (err < 0) {
2435                         stac92xx_free(codec);
2436                         return err;
2437                 }
2438                 spec->pin_configs = spec->bios_pin_configs;
2439         } else if (stac922x_brd_tbl[spec->board_config] != NULL) {
2440                 spec->pin_configs = stac922x_brd_tbl[spec->board_config];
2441                 stac92xx_set_config_regs(codec);
2442         }
2443
2444         spec->adc_nids = stac922x_adc_nids;
2445         spec->mux_nids = stac922x_mux_nids;
2446         spec->num_muxes = ARRAY_SIZE(stac922x_mux_nids);
2447         spec->num_dmics = 0;
2448
2449         spec->init = stac922x_core_init;
2450         spec->mixer = stac922x_mixer;
2451
2452         spec->multiout.dac_nids = spec->dac_nids;
2453         
2454         err = stac92xx_parse_auto_config(codec, 0x08, 0x09);
2455         if (!err) {
2456                 if (spec->board_config < 0) {
2457                         printk(KERN_WARNING "hda_codec: No auto-config is "
2458                                "available, default to model=ref\n");
2459                         spec->board_config = STAC_D945_REF;
2460                         goto again;
2461                 }
2462                 err = -EINVAL;
2463         }
2464         if (err < 0) {
2465                 stac92xx_free(codec);
2466                 return err;
2467         }
2468
2469         codec->patch_ops = stac92xx_patch_ops;
2470
2471         /* Fix Mux capture level; max to 2 */
2472         snd_hda_override_amp_caps(codec, 0x12, HDA_OUTPUT,
2473                                   (0 << AC_AMPCAP_OFFSET_SHIFT) |
2474                                   (2 << AC_AMPCAP_NUM_STEPS_SHIFT) |
2475                                   (0x27 << AC_AMPCAP_STEP_SIZE_SHIFT) |
2476                                   (0 << AC_AMPCAP_MUTE_SHIFT));
2477
2478         return 0;
2479 }
2480
2481 static int patch_stac927x(struct hda_codec *codec)
2482 {
2483         struct sigmatel_spec *spec;
2484         int err;
2485
2486         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2487         if (spec == NULL)
2488                 return -ENOMEM;
2489
2490         codec->spec = spec;
2491         spec->num_pins = ARRAY_SIZE(stac927x_pin_nids);
2492         spec->pin_nids = stac927x_pin_nids;
2493         spec->board_config = snd_hda_check_board_config(codec, STAC_927X_MODELS,
2494                                                         stac927x_models,
2495                                                         stac927x_cfg_tbl);
2496  again:
2497         if (spec->board_config < 0) {
2498                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC927x, using BIOS defaults\n");
2499                 err = stac92xx_save_bios_config_regs(codec);
2500                 if (err < 0) {
2501                         stac92xx_free(codec);
2502                         return err;
2503                 }
2504                 spec->pin_configs = spec->bios_pin_configs;
2505         } else if (stac927x_brd_tbl[spec->board_config] != NULL) {
2506                 spec->pin_configs = stac927x_brd_tbl[spec->board_config];
2507                 stac92xx_set_config_regs(codec);
2508         }
2509
2510         switch (spec->board_config) {
2511         case STAC_D965_3ST:
2512                 spec->adc_nids = stac927x_adc_nids;
2513                 spec->mux_nids = stac927x_mux_nids;
2514                 spec->num_muxes = ARRAY_SIZE(stac927x_mux_nids);
2515                 spec->num_dmics = 0;
2516                 spec->init = d965_core_init;
2517                 spec->mixer = stac9227_mixer;
2518                 break;
2519         case STAC_D965_5ST:
2520                 spec->adc_nids = stac927x_adc_nids;
2521                 spec->mux_nids = stac927x_mux_nids;
2522                 spec->num_muxes = ARRAY_SIZE(stac927x_mux_nids);
2523                 spec->num_dmics = 0;
2524                 spec->init = d965_core_init;
2525                 spec->mixer = stac9227_mixer;
2526                 break;
2527         default:
2528                 spec->adc_nids = stac927x_adc_nids;
2529                 spec->mux_nids = stac927x_mux_nids;
2530                 spec->num_muxes = ARRAY_SIZE(stac927x_mux_nids);
2531                 spec->num_dmics = 0;
2532                 spec->init = stac927x_core_init;
2533                 spec->mixer = stac927x_mixer;
2534         }
2535
2536         spec->multiout.dac_nids = spec->dac_nids;
2537         /* GPIO0 High = Enable EAPD */
2538         spec->gpio_mask = spec->gpio_data = 0x00000001;
2539         stac92xx_enable_gpio_mask(codec); 
2540         
2541         err = stac92xx_parse_auto_config(codec, 0x1e, 0x20);
2542         if (!err) {
2543                 if (spec->board_config < 0) {
2544                         printk(KERN_WARNING "hda_codec: No auto-config is "
2545                                "available, default to model=ref\n");
2546                         spec->board_config = STAC_D965_REF;
2547                         goto again;
2548                 }
2549                 err = -EINVAL;
2550         }
2551         if (err < 0) {
2552                 stac92xx_free(codec);
2553                 return err;
2554         }
2555
2556         codec->patch_ops = stac92xx_patch_ops;
2557
2558         return 0;
2559 }
2560
2561 static int patch_stac9205(struct hda_codec *codec)
2562 {
2563         struct sigmatel_spec *spec;
2564         int err;
2565
2566         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2567         if (spec == NULL)
2568                 return -ENOMEM;
2569
2570         codec->spec = spec;
2571         spec->num_pins = ARRAY_SIZE(stac9205_pin_nids);
2572         spec->pin_nids = stac9205_pin_nids;
2573         spec->board_config = snd_hda_check_board_config(codec, STAC_9205_MODELS,
2574                                                         stac9205_models,
2575                                                         stac9205_cfg_tbl);
2576  again:
2577         if (spec->board_config < 0) {
2578                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9205, using BIOS defaults\n");
2579                 err = stac92xx_save_bios_config_regs(codec);
2580                 if (err < 0) {
2581                         stac92xx_free(codec);
2582                         return err;
2583                 }
2584                 spec->pin_configs = spec->bios_pin_configs;
2585         } else {
2586                 spec->pin_configs = stac9205_brd_tbl[spec->board_config];
2587                 stac92xx_set_config_regs(codec);
2588         }
2589
2590         spec->adc_nids = stac9205_adc_nids;
2591         spec->mux_nids = stac9205_mux_nids;
2592         spec->num_muxes = ARRAY_SIZE(stac9205_mux_nids);
2593         spec->dmic_nids = stac9205_dmic_nids;
2594         spec->num_dmics = ARRAY_SIZE(stac9205_dmic_nids);
2595         spec->dmux_nid = 0x1d;
2596
2597         spec->init = stac9205_core_init;
2598         spec->mixer = stac9205_mixer;
2599
2600         spec->multiout.dac_nids = spec->dac_nids;
2601         
2602         switch (spec->board_config){
2603         case STAC_9205_M43xx:
2604         case STAC_9205_DELL_M43:
2605                 /* Enable SPDIF in/out */
2606                 stac92xx_set_config_reg(codec, 0x1f, 0x01441030);
2607                 stac92xx_set_config_reg(codec, 0x20, 0x1c410030);
2608
2609                 spec->gpio_mask = 0x00000007; /* GPIO0-2 */
2610                 /* GPIO0 High = EAPD, GPIO1 Low = DRM,
2611                  * GPIO2 High = Headphone Mute
2612                  */
2613                 spec->gpio_data = 0x00000005;
2614                 break;
2615         default:
2616                 /* GPIO0 High = EAPD */
2617                 spec->gpio_mask = spec->gpio_data = 0x00000001;
2618                 break;
2619         }
2620
2621         stac92xx_enable_gpio_mask(codec);
2622         err = stac92xx_parse_auto_config(codec, 0x1f, 0x20);
2623         if (!err) {
2624                 if (spec->board_config < 0) {
2625                         printk(KERN_WARNING "hda_codec: No auto-config is "
2626                                "available, default to model=ref\n");
2627                         spec->board_config = STAC_9205_REF;
2628                         goto again;
2629                 }
2630                 err = -EINVAL;
2631         }
2632         if (err < 0) {
2633                 stac92xx_free(codec);
2634                 return err;
2635         }
2636
2637         codec->patch_ops = stac92xx_patch_ops;
2638
2639         return 0;
2640 }
2641
2642 /*
2643  * STAC9872 hack
2644  */
2645
2646 /* static config for Sony VAIO FE550G and Sony VAIO AR */
2647 static hda_nid_t vaio_dacs[] = { 0x2 };
2648 #define VAIO_HP_DAC     0x5
2649 static hda_nid_t vaio_adcs[] = { 0x8 /*,0x6*/ };
2650 static hda_nid_t vaio_mux_nids[] = { 0x15 };
2651
2652 static struct hda_input_mux vaio_mux = {
2653         .num_items = 2,
2654         .items = {
2655                 /* { "HP", 0x0 }, */
2656                 { "Mic Jack", 0x1 },
2657                 { "Internal Mic", 0x2 },
2658                 { "PCM", 0x3 },
2659         }
2660 };
2661
2662 static struct hda_verb vaio_init[] = {
2663         {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
2664         {0x0a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | STAC_HP_EVENT},
2665         {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
2666         {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
2667         {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
2668         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
2669         {0x15, AC_VERB_SET_CONNECT_SEL, 0x1}, /* mic-sel: 0a,0d,14,02 */
2670         {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
2671         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
2672         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
2673         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
2674         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
2675         {}
2676 };
2677
2678 static struct hda_verb vaio_ar_init[] = {
2679         {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
2680         {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
2681         {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
2682         {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
2683 /*      {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },*/ /* Optical Out */
2684         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
2685         {0x15, AC_VERB_SET_CONNECT_SEL, 0x1}, /* mic-sel: 0a,0d,14,02 */
2686         {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
2687         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
2688 /*      {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},*/ /* Optical Out */
2689         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
2690         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
2691         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
2692         {}
2693 };
2694
2695 /* bind volumes of both NID 0x02 and 0x05 */
2696 static struct hda_bind_ctls vaio_bind_master_vol = {
2697         .ops = &snd_hda_bind_vol,
2698         .values = {
2699                 HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
2700                 HDA_COMPOSE_AMP_VAL(0x05, 3, 0, HDA_OUTPUT),
2701                 0
2702         },
2703 };
2704
2705 /* bind volumes of both NID 0x02 and 0x05 */
2706 static struct hda_bind_ctls vaio_bind_master_sw = {
2707         .ops = &snd_hda_bind_sw,
2708         .values = {
2709                 HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
2710                 HDA_COMPOSE_AMP_VAL(0x05, 3, 0, HDA_OUTPUT),
2711                 0,
2712         },
2713 };
2714
2715 static struct snd_kcontrol_new vaio_mixer[] = {
2716         HDA_BIND_VOL("Master Playback Volume", &vaio_bind_master_vol),
2717         HDA_BIND_SW("Master Playback Switch", &vaio_bind_master_sw),
2718         /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
2719         HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
2720         HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
2721         {
2722                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2723                 .name = "Capture Source",
2724                 .count = 1,
2725                 .info = stac92xx_mux_enum_info,
2726                 .get = stac92xx_mux_enum_get,
2727                 .put = stac92xx_mux_enum_put,
2728         },
2729         {}
2730 };
2731
2732 static struct snd_kcontrol_new vaio_ar_mixer[] = {
2733         HDA_BIND_VOL("Master Playback Volume", &vaio_bind_master_vol),
2734         HDA_BIND_SW("Master Playback Switch", &vaio_bind_master_sw),
2735         /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
2736         HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
2737         HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
2738         /*HDA_CODEC_MUTE("Optical Out Switch", 0x10, 0, HDA_OUTPUT),
2739         HDA_CODEC_VOLUME("Optical Out Volume", 0x10, 0, HDA_OUTPUT),*/
2740         {
2741                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2742                 .name = "Capture Source",
2743                 .count = 1,
2744                 .info = stac92xx_mux_enum_info,
2745                 .get = stac92xx_mux_enum_get,
2746                 .put = stac92xx_mux_enum_put,
2747         },
2748         {}
2749 };
2750
2751 static struct hda_codec_ops stac9872_patch_ops = {
2752         .build_controls = stac92xx_build_controls,
2753         .build_pcms = stac92xx_build_pcms,
2754         .init = stac92xx_init,
2755         .free = stac92xx_free,
2756 #ifdef SND_HDA_NEEDS_RESUME
2757         .resume = stac92xx_resume,
2758 #endif
2759 };
2760
2761 static int stac9872_vaio_init(struct hda_codec *codec)
2762 {
2763         int err;
2764
2765         err = stac92xx_init(codec);
2766         if (err < 0)
2767                 return err;
2768         if (codec->patch_ops.unsol_event)
2769                 codec->patch_ops.unsol_event(codec, STAC_HP_EVENT << 26);
2770         return 0;
2771 }
2772
2773 static void stac9872_vaio_hp_detect(struct hda_codec *codec, unsigned int res)
2774 {
2775         if (get_pin_presence(codec, 0x0a)) {
2776                 stac92xx_reset_pinctl(codec, 0x0f, AC_PINCTL_OUT_EN);
2777                 stac92xx_set_pinctl(codec, 0x0a, AC_PINCTL_OUT_EN);
2778         } else {
2779                 stac92xx_reset_pinctl(codec, 0x0a, AC_PINCTL_OUT_EN);
2780                 stac92xx_set_pinctl(codec, 0x0f, AC_PINCTL_OUT_EN);
2781         }
2782
2783
2784 static void stac9872_vaio_unsol_event(struct hda_codec *codec, unsigned int res)
2785 {
2786         switch (res >> 26) {
2787         case STAC_HP_EVENT:
2788                 stac9872_vaio_hp_detect(codec, res);
2789                 break;
2790         }
2791 }
2792
2793 static struct hda_codec_ops stac9872_vaio_patch_ops = {
2794         .build_controls = stac92xx_build_controls,
2795         .build_pcms = stac92xx_build_pcms,
2796         .init = stac9872_vaio_init,
2797         .free = stac92xx_free,
2798         .unsol_event = stac9872_vaio_unsol_event,
2799 #ifdef CONFIG_PM
2800         .resume = stac92xx_resume,
2801 #endif
2802 };
2803
2804 enum { /* FE and SZ series. id=0x83847661 and subsys=0x104D0700 or 104D1000. */
2805        CXD9872RD_VAIO,
2806        /* Unknown. id=0x83847662 and subsys=0x104D1200 or 104D1000. */
2807        STAC9872AK_VAIO, 
2808        /* Unknown. id=0x83847661 and subsys=0x104D1200. */
2809        STAC9872K_VAIO,
2810        /* AR Series. id=0x83847664 and subsys=104D1300 */
2811        CXD9872AKD_VAIO,
2812        STAC_9872_MODELS,
2813 };
2814
2815 static const char *stac9872_models[STAC_9872_MODELS] = {
2816         [CXD9872RD_VAIO]        = "vaio",
2817         [CXD9872AKD_VAIO]       = "vaio-ar",
2818 };
2819
2820 static struct snd_pci_quirk stac9872_cfg_tbl[] = {
2821         SND_PCI_QUIRK(0x104d, 0x81e6, "Sony VAIO F/S", CXD9872RD_VAIO),
2822         SND_PCI_QUIRK(0x104d, 0x81ef, "Sony VAIO F/S", CXD9872RD_VAIO),
2823         SND_PCI_QUIRK(0x104d, 0x81fd, "Sony VAIO AR", CXD9872AKD_VAIO),
2824         SND_PCI_QUIRK(0x104d, 0x8205, "Sony VAIO AR", CXD9872AKD_VAIO),
2825         {}
2826 };
2827
2828 static int patch_stac9872(struct hda_codec *codec)
2829 {
2830         struct sigmatel_spec *spec;
2831         int board_config;
2832
2833         board_config = snd_hda_check_board_config(codec, STAC_9872_MODELS,
2834                                                   stac9872_models,
2835                                                   stac9872_cfg_tbl);
2836         if (board_config < 0)
2837                 /* unknown config, let generic-parser do its job... */
2838                 return snd_hda_parse_generic_codec(codec);
2839         
2840         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2841         if (spec == NULL)
2842                 return -ENOMEM;
2843
2844         codec->spec = spec;
2845         switch (board_config) {
2846         case CXD9872RD_VAIO:
2847         case STAC9872AK_VAIO:
2848         case STAC9872K_VAIO:
2849                 spec->mixer = vaio_mixer;
2850                 spec->init = vaio_init;
2851                 spec->multiout.max_channels = 2;
2852                 spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
2853                 spec->multiout.dac_nids = vaio_dacs;
2854                 spec->multiout.hp_nid = VAIO_HP_DAC;
2855                 spec->num_adcs = ARRAY_SIZE(vaio_adcs);
2856                 spec->adc_nids = vaio_adcs;
2857                 spec->input_mux = &vaio_mux;
2858                 spec->mux_nids = vaio_mux_nids;
2859                 codec->patch_ops = stac9872_vaio_patch_ops;
2860                 break;
2861         
2862         case CXD9872AKD_VAIO:
2863                 spec->mixer = vaio_ar_mixer;
2864                 spec->init = vaio_ar_init;
2865                 spec->multiout.max_channels = 2;
2866                 spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
2867                 spec->multiout.dac_nids = vaio_dacs;
2868                 spec->multiout.hp_nid = VAIO_HP_DAC;
2869                 spec->num_adcs = ARRAY_SIZE(vaio_adcs);
2870                 spec->adc_nids = vaio_adcs;
2871                 spec->input_mux = &vaio_mux;
2872                 spec->mux_nids = vaio_mux_nids;
2873                 codec->patch_ops = stac9872_patch_ops;
2874                 break;
2875         }
2876
2877         return 0;
2878 }
2879
2880
2881 /*
2882  * patch entries
2883  */
2884 struct hda_codec_preset snd_hda_preset_sigmatel[] = {
2885         { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
2886         { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
2887         { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
2888         { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
2889         { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
2890         { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
2891         { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
2892         { .id = 0x83847618, .name = "STAC9227", .patch = patch_stac927x },
2893         { .id = 0x83847619, .name = "STAC9227", .patch = patch_stac927x },
2894         { .id = 0x83847616, .name = "STAC9228", .patch = patch_stac927x },
2895         { .id = 0x83847617, .name = "STAC9228", .patch = patch_stac927x },
2896         { .id = 0x83847614, .name = "STAC9229", .patch = patch_stac927x },
2897         { .id = 0x83847615, .name = "STAC9229", .patch = patch_stac927x },
2898         { .id = 0x83847620, .name = "STAC9274", .patch = patch_stac927x },
2899         { .id = 0x83847621, .name = "STAC9274D", .patch = patch_stac927x },
2900         { .id = 0x83847622, .name = "STAC9273X", .patch = patch_stac927x },
2901         { .id = 0x83847623, .name = "STAC9273D", .patch = patch_stac927x },
2902         { .id = 0x83847624, .name = "STAC9272X", .patch = patch_stac927x },
2903         { .id = 0x83847625, .name = "STAC9272D", .patch = patch_stac927x },
2904         { .id = 0x83847626, .name = "STAC9271X", .patch = patch_stac927x },
2905         { .id = 0x83847627, .name = "STAC9271D", .patch = patch_stac927x },
2906         { .id = 0x83847628, .name = "STAC9274X5NH", .patch = patch_stac927x },
2907         { .id = 0x83847629, .name = "STAC9274D5NH", .patch = patch_stac927x },
2908         { .id = 0x83847632, .name = "STAC9202",  .patch = patch_stac925x },
2909         { .id = 0x83847633, .name = "STAC9202D", .patch = patch_stac925x },
2910         { .id = 0x83847634, .name = "STAC9250", .patch = patch_stac925x },
2911         { .id = 0x83847635, .name = "STAC9250D", .patch = patch_stac925x },
2912         { .id = 0x83847636, .name = "STAC9251", .patch = patch_stac925x },
2913         { .id = 0x83847637, .name = "STAC9250D", .patch = patch_stac925x },
2914         /* The following does not take into account .id=0x83847661 when subsys =
2915          * 104D0C00 which is STAC9225s. Because of this, some SZ Notebooks are
2916          * currently not fully supported.
2917          */
2918         { .id = 0x83847661, .name = "CXD9872RD/K", .patch = patch_stac9872 },
2919         { .id = 0x83847662, .name = "STAC9872AK", .patch = patch_stac9872 },
2920         { .id = 0x83847664, .name = "CXD9872AKD", .patch = patch_stac9872 },
2921         { .id = 0x838476a0, .name = "STAC9205", .patch = patch_stac9205 },
2922         { .id = 0x838476a1, .name = "STAC9205D", .patch = patch_stac9205 },
2923         { .id = 0x838476a2, .name = "STAC9204", .patch = patch_stac9205 },
2924         { .id = 0x838476a3, .name = "STAC9204D", .patch = patch_stac9205 },
2925         { .id = 0x838476a4, .name = "STAC9255", .patch = patch_stac9205 },
2926         { .id = 0x838476a5, .name = "STAC9255D", .patch = patch_stac9205 },
2927         { .id = 0x838476a6, .name = "STAC9254", .patch = patch_stac9205 },
2928         { .id = 0x838476a7, .name = "STAC9254D", .patch = patch_stac9205 },
2929         {} /* terminator */
2930 };