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