Merge branch 'stable-3.2' into pandora-3.2
[pandora-kernel.git] / sound / pci / hda / patch_sigmatel.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * HD audio interface patch for SigmaTel STAC92xx
5  *
6  * Copyright (c) 2005 Embedded Alley Solutions, Inc.
7  * Matt Porter <mporter@embeddedalley.com>
8  *
9  * Based on patch_cmedia.c and patch_realtek.c
10  * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
11  *
12  *  This driver is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This driver is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25  */
26
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/slab.h>
30 #include <linux/pci.h>
31 #include <linux/dmi.h>
32 #include <linux/module.h>
33 #include <sound/core.h>
34 #include <sound/asoundef.h>
35 #include <sound/jack.h>
36 #include <sound/tlv.h>
37 #include "hda_codec.h"
38 #include "hda_local.h"
39 #include "hda_beep.h"
40
41 enum {
42         STAC_VREF_EVENT = 1,
43         STAC_INSERT_EVENT,
44         STAC_PWR_EVENT,
45         STAC_HP_EVENT,
46         STAC_LO_EVENT,
47         STAC_MIC_EVENT,
48 };
49
50 enum {
51         STAC_AUTO,
52         STAC_REF,
53         STAC_9200_OQO,
54         STAC_9200_DELL_D21,
55         STAC_9200_DELL_D22,
56         STAC_9200_DELL_D23,
57         STAC_9200_DELL_M21,
58         STAC_9200_DELL_M22,
59         STAC_9200_DELL_M23,
60         STAC_9200_DELL_M24,
61         STAC_9200_DELL_M25,
62         STAC_9200_DELL_M26,
63         STAC_9200_DELL_M27,
64         STAC_9200_M4,
65         STAC_9200_M4_2,
66         STAC_9200_PANASONIC,
67         STAC_9200_MODELS
68 };
69
70 enum {
71         STAC_9205_AUTO,
72         STAC_9205_REF,
73         STAC_9205_DELL_M42,
74         STAC_9205_DELL_M43,
75         STAC_9205_DELL_M44,
76         STAC_9205_EAPD,
77         STAC_9205_MODELS
78 };
79
80 enum {
81         STAC_92HD73XX_AUTO,
82         STAC_92HD73XX_NO_JD, /* no jack-detection */
83         STAC_92HD73XX_REF,
84         STAC_92HD73XX_INTEL,
85         STAC_DELL_M6_AMIC,
86         STAC_DELL_M6_DMIC,
87         STAC_DELL_M6_BOTH,
88         STAC_DELL_EQ,
89         STAC_ALIENWARE_M17X,
90         STAC_92HD73XX_MODELS
91 };
92
93 enum {
94         STAC_92HD83XXX_AUTO,
95         STAC_92HD83XXX_REF,
96         STAC_92HD83XXX_PWR_REF,
97         STAC_DELL_S14,
98         STAC_DELL_VOSTRO_3500,
99         STAC_92HD83XXX_HP,
100         STAC_92HD83XXX_HP_cNB11_INTQUAD,
101         STAC_HP_DV7_4000,
102         STAC_92HD83XXX_MODELS
103 };
104
105 enum {
106         STAC_92HD71BXX_AUTO,
107         STAC_92HD71BXX_REF,
108         STAC_DELL_M4_1,
109         STAC_DELL_M4_2,
110         STAC_DELL_M4_3,
111         STAC_HP_M4,
112         STAC_HP_DV4,
113         STAC_HP_DV5,
114         STAC_HP_HDX,
115         STAC_HP_DV4_1222NR,
116         STAC_92HD71BXX_MODELS
117 };
118
119 enum {
120         STAC_925x_AUTO,
121         STAC_925x_REF,
122         STAC_M1,
123         STAC_M1_2,
124         STAC_M2,
125         STAC_M2_2,
126         STAC_M3,
127         STAC_M5,
128         STAC_M6,
129         STAC_925x_MODELS
130 };
131
132 enum {
133         STAC_922X_AUTO,
134         STAC_D945_REF,
135         STAC_D945GTP3,
136         STAC_D945GTP5,
137         STAC_INTEL_MAC_V1,
138         STAC_INTEL_MAC_V2,
139         STAC_INTEL_MAC_V3,
140         STAC_INTEL_MAC_V4,
141         STAC_INTEL_MAC_V5,
142         STAC_INTEL_MAC_AUTO, /* This model is selected if no module parameter
143                               * is given, one of the above models will be
144                               * chosen according to the subsystem id. */
145         /* for backward compatibility */
146         STAC_MACMINI,
147         STAC_MACBOOK,
148         STAC_MACBOOK_PRO_V1,
149         STAC_MACBOOK_PRO_V2,
150         STAC_IMAC_INTEL,
151         STAC_IMAC_INTEL_20,
152         STAC_ECS_202,
153         STAC_922X_DELL_D81,
154         STAC_922X_DELL_D82,
155         STAC_922X_DELL_M81,
156         STAC_922X_DELL_M82,
157         STAC_922X_MODELS
158 };
159
160 enum {
161         STAC_927X_AUTO,
162         STAC_D965_REF_NO_JD, /* no jack-detection */
163         STAC_D965_REF,
164         STAC_D965_3ST,
165         STAC_D965_5ST,
166         STAC_D965_5ST_NO_FP,
167         STAC_DELL_3ST,
168         STAC_DELL_BIOS,
169         STAC_927X_VOLKNOB,
170         STAC_927X_MODELS
171 };
172
173 enum {
174         STAC_9872_AUTO,
175         STAC_9872_VAIO,
176         STAC_9872_MODELS
177 };
178
179 struct sigmatel_event {
180         hda_nid_t nid;
181         unsigned char type;
182         unsigned char tag;
183         int data;
184 };
185
186 struct sigmatel_mic_route {
187         hda_nid_t pin;
188         signed char mux_idx;
189         signed char dmux_idx;
190 };
191
192 #define MAX_PINS_NUM 16
193 #define MAX_ADCS_NUM 4
194 #define MAX_DMICS_NUM 4
195
196 struct sigmatel_spec {
197         struct snd_kcontrol_new *mixers[4];
198         unsigned int num_mixers;
199
200         int board_config;
201         unsigned int eapd_switch: 1;
202         unsigned int surr_switch: 1;
203         unsigned int alt_switch: 1;
204         unsigned int hp_detect: 1;
205         unsigned int spdif_mute: 1;
206         unsigned int check_volume_offset:1;
207         unsigned int auto_mic:1;
208         unsigned int linear_tone_beep:1;
209
210         /* gpio lines */
211         unsigned int eapd_mask;
212         unsigned int gpio_mask;
213         unsigned int gpio_dir;
214         unsigned int gpio_data;
215         unsigned int gpio_mute;
216         unsigned int gpio_led;
217         unsigned int gpio_led_polarity;
218         unsigned int vref_mute_led_nid; /* pin NID for mute-LED vref control */
219         unsigned int vref_led;
220
221         /* stream */
222         unsigned int stream_delay;
223
224         /* analog loopback */
225         const struct snd_kcontrol_new *aloopback_ctl;
226         unsigned char aloopback_mask;
227         unsigned char aloopback_shift;
228
229         /* power management */
230         unsigned int num_pwrs;
231         const hda_nid_t *pwr_nids;
232         const hda_nid_t *dac_list;
233
234         /* events */
235         struct snd_array events;
236
237         /* playback */
238         struct hda_input_mux *mono_mux;
239         unsigned int cur_mmux;
240         struct hda_multi_out multiout;
241         hda_nid_t dac_nids[5];
242         hda_nid_t hp_dacs[5];
243         hda_nid_t speaker_dacs[5];
244
245         int volume_offset;
246
247         /* capture */
248         const hda_nid_t *adc_nids;
249         unsigned int num_adcs;
250         const hda_nid_t *mux_nids;
251         unsigned int num_muxes;
252         const hda_nid_t *dmic_nids;
253         unsigned int num_dmics;
254         const hda_nid_t *dmux_nids;
255         unsigned int num_dmuxes;
256         const hda_nid_t *smux_nids;
257         unsigned int num_smuxes;
258         unsigned int num_analog_muxes;
259
260         const unsigned long *capvols; /* amp-volume attr: HDA_COMPOSE_AMP_VAL() */
261         const unsigned long *capsws; /* amp-mute attr: HDA_COMPOSE_AMP_VAL() */
262         unsigned int num_caps; /* number of capture volume/switch elements */
263
264         struct sigmatel_mic_route ext_mic;
265         struct sigmatel_mic_route int_mic;
266         struct sigmatel_mic_route dock_mic;
267
268         const char * const *spdif_labels;
269
270         hda_nid_t dig_in_nid;
271         hda_nid_t mono_nid;
272         hda_nid_t anabeep_nid;
273         hda_nid_t digbeep_nid;
274
275         /* pin widgets */
276         const hda_nid_t *pin_nids;
277         unsigned int num_pins;
278
279         /* codec specific stuff */
280         const struct hda_verb *init;
281         const struct snd_kcontrol_new *mixer;
282
283         /* capture source */
284         struct hda_input_mux *dinput_mux;
285         unsigned int cur_dmux[2];
286         struct hda_input_mux *input_mux;
287         unsigned int cur_mux[3];
288         struct hda_input_mux *sinput_mux;
289         unsigned int cur_smux[2];
290         unsigned int cur_amux;
291         hda_nid_t *amp_nids;
292         unsigned int powerdown_adcs;
293
294         /* i/o switches */
295         unsigned int io_switch[2];
296         unsigned int clfe_swap;
297         hda_nid_t line_switch;  /* shared line-in for input and output */
298         hda_nid_t mic_switch;   /* shared mic-in for input and output */
299         hda_nid_t hp_switch; /* NID of HP as line-out */
300         unsigned int aloopback;
301
302         struct hda_pcm pcm_rec[2];      /* PCM information */
303
304         /* dynamic controls and input_mux */
305         struct auto_pin_cfg autocfg;
306         struct snd_array kctls;
307         struct hda_input_mux private_dimux;
308         struct hda_input_mux private_imux;
309         struct hda_input_mux private_smux;
310         struct hda_input_mux private_mono_mux;
311
312         /* auto spec */
313         unsigned auto_pin_cnt;
314         hda_nid_t auto_pin_nids[MAX_PINS_NUM];
315         unsigned auto_adc_cnt;
316         hda_nid_t auto_adc_nids[MAX_ADCS_NUM];
317         hda_nid_t auto_mux_nids[MAX_ADCS_NUM];
318         hda_nid_t auto_dmux_nids[MAX_ADCS_NUM];
319         unsigned long auto_capvols[MAX_ADCS_NUM];
320         unsigned auto_dmic_cnt;
321         hda_nid_t auto_dmic_nids[MAX_DMICS_NUM];
322 };
323
324 static const hda_nid_t stac9200_adc_nids[1] = {
325         0x03,
326 };
327
328 static const hda_nid_t stac9200_mux_nids[1] = {
329         0x0c,
330 };
331
332 static const hda_nid_t stac9200_dac_nids[1] = {
333         0x02,
334 };
335
336 static const hda_nid_t stac92hd73xx_pwr_nids[8] = {
337         0x0a, 0x0b, 0x0c, 0xd, 0x0e,
338         0x0f, 0x10, 0x11
339 };
340
341 static const hda_nid_t stac92hd73xx_slave_dig_outs[2] = {
342         0x26, 0,
343 };
344
345 static const hda_nid_t stac92hd73xx_adc_nids[2] = {
346         0x1a, 0x1b
347 };
348
349 #define STAC92HD73XX_NUM_DMICS  2
350 static const hda_nid_t stac92hd73xx_dmic_nids[STAC92HD73XX_NUM_DMICS + 1] = {
351         0x13, 0x14, 0
352 };
353
354 #define STAC92HD73_DAC_COUNT 5
355
356 static const hda_nid_t stac92hd73xx_mux_nids[2] = {
357         0x20, 0x21,
358 };
359
360 static const hda_nid_t stac92hd73xx_dmux_nids[2] = {
361         0x20, 0x21,
362 };
363
364 static const hda_nid_t stac92hd73xx_smux_nids[2] = {
365         0x22, 0x23,
366 };
367
368 #define STAC92HD73XX_NUM_CAPS   2
369 static const unsigned long stac92hd73xx_capvols[] = {
370         HDA_COMPOSE_AMP_VAL(0x20, 3, 0, HDA_OUTPUT),
371         HDA_COMPOSE_AMP_VAL(0x21, 3, 0, HDA_OUTPUT),
372 };
373 #define stac92hd73xx_capsws     stac92hd73xx_capvols
374
375 #define STAC92HD83_DAC_COUNT 3
376
377 static const hda_nid_t stac92hd83xxx_pwr_nids[7] = {
378         0x0a, 0x0b, 0x0c, 0xd, 0x0e,
379         0x0f, 0x10
380 };
381
382 static const hda_nid_t stac92hd83xxx_slave_dig_outs[2] = {
383         0x1e, 0,
384 };
385
386 static const hda_nid_t stac92hd83xxx_dmic_nids[] = {
387                 0x11, 0x20,
388 };
389
390 static const hda_nid_t stac92hd71bxx_pwr_nids[3] = {
391         0x0a, 0x0d, 0x0f
392 };
393
394 static const hda_nid_t stac92hd71bxx_adc_nids[2] = {
395         0x12, 0x13,
396 };
397
398 static const hda_nid_t stac92hd71bxx_mux_nids[2] = {
399         0x1a, 0x1b
400 };
401
402 static const hda_nid_t stac92hd71bxx_dmux_nids[2] = {
403         0x1c, 0x1d,
404 };
405
406 static const hda_nid_t stac92hd71bxx_smux_nids[2] = {
407         0x24, 0x25,
408 };
409
410 #define STAC92HD71BXX_NUM_DMICS 2
411 static const hda_nid_t stac92hd71bxx_dmic_nids[STAC92HD71BXX_NUM_DMICS + 1] = {
412         0x18, 0x19, 0
413 };
414
415 static const hda_nid_t stac92hd71bxx_dmic_5port_nids[STAC92HD71BXX_NUM_DMICS] = {
416         0x18, 0
417 };
418
419 static const hda_nid_t stac92hd71bxx_slave_dig_outs[2] = {
420         0x22, 0
421 };
422
423 #define STAC92HD71BXX_NUM_CAPS          2
424 static const unsigned long stac92hd71bxx_capvols[] = {
425         HDA_COMPOSE_AMP_VAL(0x1c, 3, 0, HDA_OUTPUT),
426         HDA_COMPOSE_AMP_VAL(0x1d, 3, 0, HDA_OUTPUT),
427 };
428 #define stac92hd71bxx_capsws    stac92hd71bxx_capvols
429
430 static const hda_nid_t stac925x_adc_nids[1] = {
431         0x03,
432 };
433
434 static const hda_nid_t stac925x_mux_nids[1] = {
435         0x0f,
436 };
437
438 static const hda_nid_t stac925x_dac_nids[1] = {
439         0x02,
440 };
441
442 #define STAC925X_NUM_DMICS      1
443 static const hda_nid_t stac925x_dmic_nids[STAC925X_NUM_DMICS + 1] = {
444         0x15, 0
445 };
446
447 static const hda_nid_t stac925x_dmux_nids[1] = {
448         0x14,
449 };
450
451 static const unsigned long stac925x_capvols[] = {
452         HDA_COMPOSE_AMP_VAL(0x09, 3, 0, HDA_OUTPUT),
453 };
454 static const unsigned long stac925x_capsws[] = {
455         HDA_COMPOSE_AMP_VAL(0x14, 3, 0, HDA_OUTPUT),
456 };
457
458 static const hda_nid_t stac922x_adc_nids[2] = {
459         0x06, 0x07,
460 };
461
462 static const hda_nid_t stac922x_mux_nids[2] = {
463         0x12, 0x13,
464 };
465
466 #define STAC922X_NUM_CAPS       2
467 static const unsigned long stac922x_capvols[] = {
468         HDA_COMPOSE_AMP_VAL(0x17, 3, 0, HDA_INPUT),
469         HDA_COMPOSE_AMP_VAL(0x18, 3, 0, HDA_INPUT),
470 };
471 #define stac922x_capsws         stac922x_capvols
472
473 static const hda_nid_t stac927x_slave_dig_outs[2] = {
474         0x1f, 0,
475 };
476
477 static const hda_nid_t stac927x_adc_nids[3] = {
478         0x07, 0x08, 0x09
479 };
480
481 static const hda_nid_t stac927x_mux_nids[3] = {
482         0x15, 0x16, 0x17
483 };
484
485 static const hda_nid_t stac927x_smux_nids[1] = {
486         0x21,
487 };
488
489 static const hda_nid_t stac927x_dac_nids[6] = {
490         0x02, 0x03, 0x04, 0x05, 0x06, 0
491 };
492
493 static const hda_nid_t stac927x_dmux_nids[1] = {
494         0x1b,
495 };
496
497 #define STAC927X_NUM_DMICS 2
498 static const hda_nid_t stac927x_dmic_nids[STAC927X_NUM_DMICS + 1] = {
499         0x13, 0x14, 0
500 };
501
502 #define STAC927X_NUM_CAPS       3
503 static const unsigned long stac927x_capvols[] = {
504         HDA_COMPOSE_AMP_VAL(0x18, 3, 0, HDA_INPUT),
505         HDA_COMPOSE_AMP_VAL(0x19, 3, 0, HDA_INPUT),
506         HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_INPUT),
507 };
508 static const unsigned long stac927x_capsws[] = {
509         HDA_COMPOSE_AMP_VAL(0x1b, 3, 0, HDA_OUTPUT),
510         HDA_COMPOSE_AMP_VAL(0x1c, 3, 0, HDA_OUTPUT),
511         HDA_COMPOSE_AMP_VAL(0x1d, 3, 0, HDA_OUTPUT),
512 };
513
514 static const char * const stac927x_spdif_labels[5] = {
515         "Digital Playback", "ADAT", "Analog Mux 1",
516         "Analog Mux 2", "Analog Mux 3"
517 };
518
519 static const hda_nid_t stac9205_adc_nids[2] = {
520         0x12, 0x13
521 };
522
523 static const hda_nid_t stac9205_mux_nids[2] = {
524         0x19, 0x1a
525 };
526
527 static const hda_nid_t stac9205_dmux_nids[1] = {
528         0x1d,
529 };
530
531 static const hda_nid_t stac9205_smux_nids[1] = {
532         0x21,
533 };
534
535 #define STAC9205_NUM_DMICS      2
536 static const hda_nid_t stac9205_dmic_nids[STAC9205_NUM_DMICS + 1] = {
537         0x17, 0x18, 0
538 };
539
540 #define STAC9205_NUM_CAPS       2
541 static const unsigned long stac9205_capvols[] = {
542         HDA_COMPOSE_AMP_VAL(0x1b, 3, 0, HDA_INPUT),
543         HDA_COMPOSE_AMP_VAL(0x1c, 3, 0, HDA_INPUT),
544 };
545 static const unsigned long stac9205_capsws[] = {
546         HDA_COMPOSE_AMP_VAL(0x1d, 3, 0, HDA_OUTPUT),
547         HDA_COMPOSE_AMP_VAL(0x1e, 3, 0, HDA_OUTPUT),
548 };
549
550 static const hda_nid_t stac9200_pin_nids[8] = {
551         0x08, 0x09, 0x0d, 0x0e, 
552         0x0f, 0x10, 0x11, 0x12,
553 };
554
555 static const hda_nid_t stac925x_pin_nids[8] = {
556         0x07, 0x08, 0x0a, 0x0b, 
557         0x0c, 0x0d, 0x10, 0x11,
558 };
559
560 static const hda_nid_t stac922x_pin_nids[10] = {
561         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
562         0x0f, 0x10, 0x11, 0x15, 0x1b,
563 };
564
565 static const hda_nid_t stac92hd73xx_pin_nids[13] = {
566         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
567         0x0f, 0x10, 0x11, 0x12, 0x13,
568         0x14, 0x22, 0x23
569 };
570
571 #define STAC92HD71BXX_NUM_PINS 13
572 static const hda_nid_t stac92hd71bxx_pin_nids_4port[STAC92HD71BXX_NUM_PINS] = {
573         0x0a, 0x0b, 0x0c, 0x0d, 0x00,
574         0x00, 0x14, 0x18, 0x19, 0x1e,
575         0x1f, 0x20, 0x27
576 };
577 static const hda_nid_t stac92hd71bxx_pin_nids_6port[STAC92HD71BXX_NUM_PINS] = {
578         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
579         0x0f, 0x14, 0x18, 0x19, 0x1e,
580         0x1f, 0x20, 0x27
581 };
582
583 static const hda_nid_t stac927x_pin_nids[14] = {
584         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
585         0x0f, 0x10, 0x11, 0x12, 0x13,
586         0x14, 0x21, 0x22, 0x23,
587 };
588
589 static const hda_nid_t stac9205_pin_nids[12] = {
590         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
591         0x0f, 0x14, 0x16, 0x17, 0x18,
592         0x21, 0x22,
593 };
594
595 static int stac92xx_dmux_enum_info(struct snd_kcontrol *kcontrol,
596                                    struct snd_ctl_elem_info *uinfo)
597 {
598         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
599         struct sigmatel_spec *spec = codec->spec;
600         return snd_hda_input_mux_info(spec->dinput_mux, uinfo);
601 }
602
603 static int stac92xx_dmux_enum_get(struct snd_kcontrol *kcontrol,
604                                   struct snd_ctl_elem_value *ucontrol)
605 {
606         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
607         struct sigmatel_spec *spec = codec->spec;
608         unsigned int dmux_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
609
610         ucontrol->value.enumerated.item[0] = spec->cur_dmux[dmux_idx];
611         return 0;
612 }
613
614 static int stac92xx_dmux_enum_put(struct snd_kcontrol *kcontrol,
615                                   struct snd_ctl_elem_value *ucontrol)
616 {
617         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
618         struct sigmatel_spec *spec = codec->spec;
619         unsigned int dmux_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
620
621         return snd_hda_input_mux_put(codec, spec->dinput_mux, ucontrol,
622                         spec->dmux_nids[dmux_idx], &spec->cur_dmux[dmux_idx]);
623 }
624
625 static int stac92xx_smux_enum_info(struct snd_kcontrol *kcontrol,
626                                    struct snd_ctl_elem_info *uinfo)
627 {
628         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
629         struct sigmatel_spec *spec = codec->spec;
630         return snd_hda_input_mux_info(spec->sinput_mux, uinfo);
631 }
632
633 static int stac92xx_smux_enum_get(struct snd_kcontrol *kcontrol,
634                                   struct snd_ctl_elem_value *ucontrol)
635 {
636         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
637         struct sigmatel_spec *spec = codec->spec;
638         unsigned int smux_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
639
640         ucontrol->value.enumerated.item[0] = spec->cur_smux[smux_idx];
641         return 0;
642 }
643
644 static int stac92xx_smux_enum_put(struct snd_kcontrol *kcontrol,
645                                   struct snd_ctl_elem_value *ucontrol)
646 {
647         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
648         struct sigmatel_spec *spec = codec->spec;
649         struct hda_input_mux *smux = &spec->private_smux;
650         unsigned int smux_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
651         int err, val;
652         hda_nid_t nid;
653
654         err = snd_hda_input_mux_put(codec, spec->sinput_mux, ucontrol,
655                         spec->smux_nids[smux_idx], &spec->cur_smux[smux_idx]);
656         if (err < 0)
657                 return err;
658
659         if (spec->spdif_mute) {
660                 if (smux_idx == 0)
661                         nid = spec->multiout.dig_out_nid;
662                 else
663                         nid = codec->slave_dig_outs[smux_idx - 1];
664                 if (spec->cur_smux[smux_idx] == smux->num_items - 1)
665                         val = HDA_AMP_MUTE;
666                 else
667                         val = 0;
668                 /* un/mute SPDIF out */
669                 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
670                                          HDA_AMP_MUTE, val);
671         }
672         return 0;
673 }
674
675 #ifdef CONFIG_SND_HDA_POWER_SAVE
676 static int stac_vrefout_set(struct hda_codec *codec,
677                                         hda_nid_t nid, unsigned int new_vref)
678 {
679         int error, pinctl;
680
681         snd_printdd("%s, nid %x ctl %x\n", __func__, nid, new_vref);
682         pinctl = snd_hda_codec_read(codec, nid, 0,
683                                 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
684
685         if (pinctl < 0)
686                 return pinctl;
687
688         pinctl &= 0xff;
689         pinctl &= ~AC_PINCTL_VREFEN;
690         pinctl |= (new_vref & AC_PINCTL_VREFEN);
691
692         error = snd_hda_codec_write_cache(codec, nid, 0,
693                                         AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
694         if (error < 0)
695                 return error;
696
697         return 1;
698 }
699 #endif
700
701 static unsigned int stac92xx_vref_set(struct hda_codec *codec,
702                                         hda_nid_t nid, unsigned int new_vref)
703 {
704         int error;
705         unsigned int pincfg;
706         pincfg = snd_hda_codec_read(codec, nid, 0,
707                                 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
708
709         pincfg &= 0xff;
710         pincfg &= ~(AC_PINCTL_VREFEN | AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
711         pincfg |= new_vref;
712
713         if (new_vref == AC_PINCTL_VREF_HIZ)
714                 pincfg |= AC_PINCTL_OUT_EN;
715         else
716                 pincfg |= AC_PINCTL_IN_EN;
717
718         error = snd_hda_codec_write_cache(codec, nid, 0,
719                                         AC_VERB_SET_PIN_WIDGET_CONTROL, pincfg);
720         if (error < 0)
721                 return error;
722         else
723                 return 1;
724 }
725
726 static unsigned int stac92xx_vref_get(struct hda_codec *codec, hda_nid_t nid)
727 {
728         unsigned int vref;
729         vref = snd_hda_codec_read(codec, nid, 0,
730                                 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
731         vref &= AC_PINCTL_VREFEN;
732         return vref;
733 }
734
735 static int stac92xx_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
736 {
737         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
738         struct sigmatel_spec *spec = codec->spec;
739         return snd_hda_input_mux_info(spec->input_mux, uinfo);
740 }
741
742 static int stac92xx_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
743 {
744         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
745         struct sigmatel_spec *spec = codec->spec;
746         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
747
748         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
749         return 0;
750 }
751
752 static int stac92xx_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
753 {
754         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
755         struct sigmatel_spec *spec = codec->spec;
756         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
757         const struct hda_input_mux *imux = spec->input_mux;
758         unsigned int idx, prev_idx, didx;
759
760         idx = ucontrol->value.enumerated.item[0];
761         if (idx >= imux->num_items)
762                 idx = imux->num_items - 1;
763         prev_idx = spec->cur_mux[adc_idx];
764         if (prev_idx == idx)
765                 return 0;
766         if (idx < spec->num_analog_muxes) {
767                 snd_hda_codec_write_cache(codec, spec->mux_nids[adc_idx], 0,
768                                           AC_VERB_SET_CONNECT_SEL,
769                                           imux->items[idx].index);
770                 if (prev_idx >= spec->num_analog_muxes &&
771                     spec->mux_nids[adc_idx] != spec->dmux_nids[adc_idx]) {
772                         imux = spec->dinput_mux;
773                         /* 0 = analog */
774                         snd_hda_codec_write_cache(codec,
775                                                   spec->dmux_nids[adc_idx], 0,
776                                                   AC_VERB_SET_CONNECT_SEL,
777                                                   imux->items[0].index);
778                 }
779         } else {
780                 imux = spec->dinput_mux;
781                 /* first dimux item is hardcoded to select analog imux,
782                  * so lets skip it
783                  */
784                 didx = idx - spec->num_analog_muxes + 1;
785                 snd_hda_codec_write_cache(codec, spec->dmux_nids[adc_idx], 0,
786                                           AC_VERB_SET_CONNECT_SEL,
787                                           imux->items[didx].index);
788         }
789         spec->cur_mux[adc_idx] = idx;
790         return 1;
791 }
792
793 static int stac92xx_mono_mux_enum_info(struct snd_kcontrol *kcontrol,
794         struct snd_ctl_elem_info *uinfo)
795 {
796         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
797         struct sigmatel_spec *spec = codec->spec;
798         return snd_hda_input_mux_info(spec->mono_mux, uinfo);
799 }
800
801 static int stac92xx_mono_mux_enum_get(struct snd_kcontrol *kcontrol,
802         struct snd_ctl_elem_value *ucontrol)
803 {
804         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
805         struct sigmatel_spec *spec = codec->spec;
806
807         ucontrol->value.enumerated.item[0] = spec->cur_mmux;
808         return 0;
809 }
810
811 static int stac92xx_mono_mux_enum_put(struct snd_kcontrol *kcontrol,
812         struct snd_ctl_elem_value *ucontrol)
813 {
814         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
815         struct sigmatel_spec *spec = codec->spec;
816
817         return snd_hda_input_mux_put(codec, spec->mono_mux, ucontrol,
818                                      spec->mono_nid, &spec->cur_mmux);
819 }
820
821 #define stac92xx_aloopback_info snd_ctl_boolean_mono_info
822
823 static int stac92xx_aloopback_get(struct snd_kcontrol *kcontrol,
824         struct snd_ctl_elem_value *ucontrol)
825 {
826         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
827         unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
828         struct sigmatel_spec *spec = codec->spec;
829
830         ucontrol->value.integer.value[0] = !!(spec->aloopback &
831                                               (spec->aloopback_mask << idx));
832         return 0;
833 }
834
835 static int stac92xx_aloopback_put(struct snd_kcontrol *kcontrol,
836                 struct snd_ctl_elem_value *ucontrol)
837 {
838         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
839         struct sigmatel_spec *spec = codec->spec;
840         unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
841         unsigned int dac_mode;
842         unsigned int val, idx_val;
843
844         idx_val = spec->aloopback_mask << idx;
845         if (ucontrol->value.integer.value[0])
846                 val = spec->aloopback | idx_val;
847         else
848                 val = spec->aloopback & ~idx_val;
849         if (spec->aloopback == val)
850                 return 0;
851
852         spec->aloopback = val;
853
854         /* Only return the bits defined by the shift value of the
855          * first two bytes of the mask
856          */
857         dac_mode = snd_hda_codec_read(codec, codec->afg, 0,
858                                       kcontrol->private_value & 0xFFFF, 0x0);
859         dac_mode >>= spec->aloopback_shift;
860
861         if (spec->aloopback & idx_val) {
862                 snd_hda_power_up(codec);
863                 dac_mode |= idx_val;
864         } else {
865                 snd_hda_power_down(codec);
866                 dac_mode &= ~idx_val;
867         }
868
869         snd_hda_codec_write_cache(codec, codec->afg, 0,
870                 kcontrol->private_value >> 16, dac_mode);
871
872         return 1;
873 }
874
875 static const struct hda_verb stac9200_core_init[] = {
876         /* set dac0mux for dac converter */
877         { 0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
878         {}
879 };
880
881 static const struct hda_verb stac9200_eapd_init[] = {
882         /* set dac0mux for dac converter */
883         {0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
884         {0x08, AC_VERB_SET_EAPD_BTLENABLE, 0x02},
885         {}
886 };
887
888 static const struct hda_verb dell_eq_core_init[] = {
889         /* set master volume to max value without distortion
890          * and direct control */
891         { 0x1f, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xec},
892         {}
893 };
894
895 static const struct hda_verb stac92hd73xx_core_init[] = {
896         /* set master volume and direct control */
897         { 0x1f, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
898         {}
899 };
900
901 static const struct hda_verb stac92hd83xxx_core_init[] = {
902         /* power state controls amps */
903         { 0x01, AC_VERB_SET_EAPD, 1 << 2},
904         {}
905 };
906
907 static const struct hda_verb stac92hd71bxx_core_init[] = {
908         /* set master volume and direct control */
909         { 0x28, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
910         {}
911 };
912
913 static const struct hda_verb stac92hd71bxx_unmute_core_init[] = {
914         /* unmute right and left channels for nodes 0x0f, 0xa, 0x0d */
915         { 0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
916         { 0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
917         { 0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
918         {}
919 };
920
921 static const struct hda_verb stac925x_core_init[] = {
922         /* set dac0mux for dac converter */
923         { 0x06, AC_VERB_SET_CONNECT_SEL, 0x00},
924         /* mute the master volume */
925         { 0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
926         {}
927 };
928
929 static const struct hda_verb stac922x_core_init[] = {
930         /* set master volume and direct control */      
931         { 0x16, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
932         {}
933 };
934
935 static const struct hda_verb d965_core_init[] = {
936         /* set master volume and direct control */      
937         { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
938         /* unmute node 0x1b */
939         { 0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
940         /* select node 0x03 as DAC */   
941         { 0x0b, AC_VERB_SET_CONNECT_SEL, 0x01},
942         {}
943 };
944
945 static const struct hda_verb dell_3st_core_init[] = {
946         /* don't set delta bit */
947         {0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0x7f},
948         /* unmute node 0x1b */
949         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
950         /* select node 0x03 as DAC */
951         {0x0b, AC_VERB_SET_CONNECT_SEL, 0x01},
952         {}
953 };
954
955 static const struct hda_verb stac927x_core_init[] = {
956         /* set master volume and direct control */      
957         { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
958         /* enable analog pc beep path */
959         { 0x01, AC_VERB_SET_DIGI_CONVERT_2, 1 << 5},
960         {}
961 };
962
963 static const struct hda_verb stac927x_volknob_core_init[] = {
964         /* don't set delta bit */
965         {0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0x7f},
966         /* enable analog pc beep path */
967         {0x01, AC_VERB_SET_DIGI_CONVERT_2, 1 << 5},
968         {}
969 };
970
971 static const struct hda_verb stac9205_core_init[] = {
972         /* set master volume and direct control */      
973         { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
974         /* enable analog pc beep path */
975         { 0x01, AC_VERB_SET_DIGI_CONVERT_2, 1 << 5},
976         {}
977 };
978
979 #define STAC_MONO_MUX \
980         { \
981                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
982                 .name = "Mono Mux", \
983                 .count = 1, \
984                 .info = stac92xx_mono_mux_enum_info, \
985                 .get = stac92xx_mono_mux_enum_get, \
986                 .put = stac92xx_mono_mux_enum_put, \
987         }
988
989 #define STAC_ANALOG_LOOPBACK(verb_read, verb_write, cnt) \
990         { \
991                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
992                 .name  = "Analog Loopback", \
993                 .count = cnt, \
994                 .info  = stac92xx_aloopback_info, \
995                 .get   = stac92xx_aloopback_get, \
996                 .put   = stac92xx_aloopback_put, \
997                 .private_value = verb_read | (verb_write << 16), \
998         }
999
1000 #define DC_BIAS(xname, idx, nid) \
1001         { \
1002                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1003                 .name = xname, \
1004                 .index = idx, \
1005                 .info = stac92xx_dc_bias_info, \
1006                 .get = stac92xx_dc_bias_get, \
1007                 .put = stac92xx_dc_bias_put, \
1008                 .private_value = nid, \
1009         }
1010
1011 static const struct snd_kcontrol_new stac9200_mixer[] = {
1012         HDA_CODEC_VOLUME_MIN_MUTE("Master Playback Volume", 0xb, 0, HDA_OUTPUT),
1013         HDA_CODEC_MUTE("Master Playback Switch", 0xb, 0, HDA_OUTPUT),
1014         HDA_CODEC_VOLUME("Capture Volume", 0x0a, 0, HDA_OUTPUT),
1015         HDA_CODEC_MUTE("Capture Switch", 0x0a, 0, HDA_OUTPUT),
1016         { } /* end */
1017 };
1018
1019 static const struct snd_kcontrol_new stac92hd73xx_6ch_loopback[] = {
1020         STAC_ANALOG_LOOPBACK(0xFA0, 0x7A1, 3),
1021         {}
1022 };
1023
1024 static const struct snd_kcontrol_new stac92hd73xx_8ch_loopback[] = {
1025         STAC_ANALOG_LOOPBACK(0xFA0, 0x7A1, 4),
1026         {}
1027 };
1028
1029 static const struct snd_kcontrol_new stac92hd73xx_10ch_loopback[] = {
1030         STAC_ANALOG_LOOPBACK(0xFA0, 0x7A1, 5),
1031         {}
1032 };
1033
1034
1035 static const struct snd_kcontrol_new stac92hd71bxx_loopback[] = {
1036         STAC_ANALOG_LOOPBACK(0xFA0, 0x7A0, 2)
1037 };
1038
1039 static const struct snd_kcontrol_new stac925x_mixer[] = {
1040         HDA_CODEC_VOLUME_MIN_MUTE("Master Playback Volume", 0xe, 0, HDA_OUTPUT),
1041         HDA_CODEC_MUTE("Master Playback Switch", 0x0e, 0, HDA_OUTPUT),
1042         { } /* end */
1043 };
1044
1045 static const struct snd_kcontrol_new stac9205_loopback[] = {
1046         STAC_ANALOG_LOOPBACK(0xFE0, 0x7E0, 1),
1047         {}
1048 };
1049
1050 static const struct snd_kcontrol_new stac927x_loopback[] = {
1051         STAC_ANALOG_LOOPBACK(0xFEB, 0x7EB, 1),
1052         {}
1053 };
1054
1055 static struct snd_kcontrol_new stac_dmux_mixer = {
1056         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1057         .name = "Digital Input Source",
1058         /* count set later */
1059         .info = stac92xx_dmux_enum_info,
1060         .get = stac92xx_dmux_enum_get,
1061         .put = stac92xx_dmux_enum_put,
1062 };
1063
1064 static struct snd_kcontrol_new stac_smux_mixer = {
1065         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1066         .name = "IEC958 Playback Source",
1067         /* count set later */
1068         .info = stac92xx_smux_enum_info,
1069         .get = stac92xx_smux_enum_get,
1070         .put = stac92xx_smux_enum_put,
1071 };
1072
1073 static const char * const slave_vols[] = {
1074         "Front Playback Volume",
1075         "Surround Playback Volume",
1076         "Center Playback Volume",
1077         "LFE Playback Volume",
1078         "Side Playback Volume",
1079         "Headphone Playback Volume",
1080         "Speaker Playback Volume",
1081         NULL
1082 };
1083
1084 static const char * const slave_sws[] = {
1085         "Front Playback Switch",
1086         "Surround Playback Switch",
1087         "Center Playback Switch",
1088         "LFE Playback Switch",
1089         "Side Playback Switch",
1090         "Headphone Playback Switch",
1091         "Speaker Playback Switch",
1092         "IEC958 Playback Switch",
1093         NULL
1094 };
1095
1096 static void stac92xx_free_kctls(struct hda_codec *codec);
1097 static int stac92xx_add_jack(struct hda_codec *codec, hda_nid_t nid, int type);
1098
1099 static int stac92xx_build_controls(struct hda_codec *codec)
1100 {
1101         struct sigmatel_spec *spec = codec->spec;
1102         struct auto_pin_cfg *cfg = &spec->autocfg;
1103         hda_nid_t nid;
1104         int err;
1105         int i;
1106
1107         if (spec->mixer) {
1108                 err = snd_hda_add_new_ctls(codec, spec->mixer);
1109                 if (err < 0)
1110                         return err;
1111         }
1112
1113         for (i = 0; i < spec->num_mixers; i++) {
1114                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1115                 if (err < 0)
1116                         return err;
1117         }
1118         if (!spec->auto_mic && spec->num_dmuxes > 0 &&
1119             snd_hda_get_bool_hint(codec, "separate_dmux") == 1) {
1120                 stac_dmux_mixer.count = spec->num_dmuxes;
1121                 err = snd_hda_ctl_add(codec, 0,
1122                                   snd_ctl_new1(&stac_dmux_mixer, codec));
1123                 if (err < 0)
1124                         return err;
1125         }
1126         if (spec->num_smuxes > 0) {
1127                 int wcaps = get_wcaps(codec, spec->multiout.dig_out_nid);
1128                 struct hda_input_mux *smux = &spec->private_smux;
1129                 /* check for mute support on SPDIF out */
1130                 if (wcaps & AC_WCAP_OUT_AMP) {
1131                         snd_hda_add_imux_item(smux, "Off", 0, NULL);
1132                         spec->spdif_mute = 1;
1133                 }
1134                 stac_smux_mixer.count = spec->num_smuxes;
1135                 err = snd_hda_ctl_add(codec, 0,
1136                                   snd_ctl_new1(&stac_smux_mixer, codec));
1137                 if (err < 0)
1138                         return err;
1139         }
1140
1141         if (spec->multiout.dig_out_nid) {
1142                 err = snd_hda_create_spdif_out_ctls(codec,
1143                                                     spec->multiout.dig_out_nid,
1144                                                     spec->multiout.dig_out_nid);
1145                 if (err < 0)
1146                         return err;
1147                 err = snd_hda_create_spdif_share_sw(codec,
1148                                                     &spec->multiout);
1149                 if (err < 0)
1150                         return err;
1151                 spec->multiout.share_spdif = 1;
1152         }
1153         if (spec->dig_in_nid && !(spec->gpio_dir & 0x01)) {
1154                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1155                 if (err < 0)
1156                         return err;
1157         }
1158
1159         /* if we have no master control, let's create it */
1160         if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1161                 unsigned int vmaster_tlv[4];
1162                 snd_hda_set_vmaster_tlv(codec, spec->multiout.dac_nids[0],
1163                                         HDA_OUTPUT, vmaster_tlv);
1164                 /* correct volume offset */
1165                 vmaster_tlv[2] += vmaster_tlv[3] * spec->volume_offset;
1166                 /* minimum value is actually mute */
1167                 vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
1168                 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
1169                                           vmaster_tlv, slave_vols);
1170                 if (err < 0)
1171                         return err;
1172         }
1173         if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1174                 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
1175                                           NULL, slave_sws);
1176                 if (err < 0)
1177                         return err;
1178         }
1179
1180         if (spec->aloopback_ctl &&
1181             snd_hda_get_bool_hint(codec, "loopback") == 1) {
1182                 err = snd_hda_add_new_ctls(codec, spec->aloopback_ctl);
1183                 if (err < 0)
1184                         return err;
1185         }
1186
1187         stac92xx_free_kctls(codec); /* no longer needed */
1188
1189         /* create jack input elements */
1190         if (spec->hp_detect) {
1191                 for (i = 0; i < cfg->hp_outs; i++) {
1192                         int type = SND_JACK_HEADPHONE;
1193                         nid = cfg->hp_pins[i];
1194                         /* jack detection */
1195                         if (cfg->hp_outs == i)
1196                                 type |= SND_JACK_LINEOUT;
1197                         err = stac92xx_add_jack(codec, nid, type);
1198                         if (err < 0)
1199                                 return err;
1200                 }
1201         }
1202         for (i = 0; i < cfg->line_outs; i++) {
1203                 err = stac92xx_add_jack(codec, cfg->line_out_pins[i],
1204                                         SND_JACK_LINEOUT);
1205                 if (err < 0)
1206                         return err;
1207         }
1208         for (i = 0; i < cfg->num_inputs; i++) {
1209                 nid = cfg->inputs[i].pin;
1210                 err = stac92xx_add_jack(codec, nid, SND_JACK_MICROPHONE);
1211                 if (err < 0)
1212                         return err;
1213         }
1214
1215         return 0;       
1216 }
1217
1218 static const unsigned int ref9200_pin_configs[8] = {
1219         0x01c47010, 0x01447010, 0x0221401f, 0x01114010,
1220         0x02a19020, 0x01a19021, 0x90100140, 0x01813122,
1221 };
1222
1223 static const unsigned int gateway9200_m4_pin_configs[8] = {
1224         0x400000fe, 0x404500f4, 0x400100f0, 0x90110010,
1225         0x400100f1, 0x02a1902e, 0x500000f2, 0x500000f3,
1226 };
1227 static const unsigned int gateway9200_m4_2_pin_configs[8] = {
1228         0x400000fe, 0x404500f4, 0x400100f0, 0x90110010,
1229         0x400100f1, 0x02a1902e, 0x500000f2, 0x500000f3,
1230 };
1231
1232 /*
1233     STAC 9200 pin configs for
1234     102801A8
1235     102801DE
1236     102801E8
1237 */
1238 static const unsigned int dell9200_d21_pin_configs[8] = {
1239         0x400001f0, 0x400001f1, 0x02214030, 0x01014010, 
1240         0x02a19020, 0x01a19021, 0x90100140, 0x01813122,
1241 };
1242
1243 /* 
1244     STAC 9200 pin configs for
1245     102801C0
1246     102801C1
1247 */
1248 static const unsigned int dell9200_d22_pin_configs[8] = {
1249         0x400001f0, 0x400001f1, 0x0221401f, 0x01014010, 
1250         0x01813020, 0x02a19021, 0x90100140, 0x400001f2,
1251 };
1252
1253 /* 
1254     STAC 9200 pin configs for
1255     102801C4 (Dell Dimension E310)
1256     102801C5
1257     102801C7
1258     102801D9
1259     102801DA
1260     102801E3
1261 */
1262 static const unsigned int dell9200_d23_pin_configs[8] = {
1263         0x400001f0, 0x400001f1, 0x0221401f, 0x01014010, 
1264         0x01813020, 0x01a19021, 0x90100140, 0x400001f2, 
1265 };
1266
1267
1268 /* 
1269     STAC 9200-32 pin configs for
1270     102801B5 (Dell Inspiron 630m)
1271     102801D8 (Dell Inspiron 640m)
1272 */
1273 static const unsigned int dell9200_m21_pin_configs[8] = {
1274         0x40c003fa, 0x03441340, 0x0321121f, 0x90170310,
1275         0x408003fb, 0x03a11020, 0x401003fc, 0x403003fd,
1276 };
1277
1278 /* 
1279     STAC 9200-32 pin configs for
1280     102801C2 (Dell Latitude D620)
1281     102801C8 
1282     102801CC (Dell Latitude D820)
1283     102801D4 
1284     102801D6 
1285 */
1286 static const unsigned int dell9200_m22_pin_configs[8] = {
1287         0x40c003fa, 0x0144131f, 0x0321121f, 0x90170310, 
1288         0x90a70321, 0x03a11020, 0x401003fb, 0x40f000fc,
1289 };
1290
1291 /* 
1292     STAC 9200-32 pin configs for
1293     102801CE (Dell XPS M1710)
1294     102801CF (Dell Precision M90)
1295 */
1296 static const unsigned int dell9200_m23_pin_configs[8] = {
1297         0x40c003fa, 0x01441340, 0x0421421f, 0x90170310,
1298         0x408003fb, 0x04a1102e, 0x90170311, 0x403003fc,
1299 };
1300
1301 /*
1302     STAC 9200-32 pin configs for 
1303     102801C9
1304     102801CA
1305     102801CB (Dell Latitude 120L)
1306     102801D3
1307 */
1308 static const unsigned int dell9200_m24_pin_configs[8] = {
1309         0x40c003fa, 0x404003fb, 0x0321121f, 0x90170310, 
1310         0x408003fc, 0x03a11020, 0x401003fd, 0x403003fe, 
1311 };
1312
1313 /*
1314     STAC 9200-32 pin configs for
1315     102801BD (Dell Inspiron E1505n)
1316     102801EE
1317     102801EF
1318 */
1319 static const unsigned int dell9200_m25_pin_configs[8] = {
1320         0x40c003fa, 0x01441340, 0x0421121f, 0x90170310, 
1321         0x408003fb, 0x04a11020, 0x401003fc, 0x403003fd,
1322 };
1323
1324 /*
1325     STAC 9200-32 pin configs for
1326     102801F5 (Dell Inspiron 1501)
1327     102801F6
1328 */
1329 static const unsigned int dell9200_m26_pin_configs[8] = {
1330         0x40c003fa, 0x404003fb, 0x0421121f, 0x90170310, 
1331         0x408003fc, 0x04a11020, 0x401003fd, 0x403003fe,
1332 };
1333
1334 /*
1335     STAC 9200-32
1336     102801CD (Dell Inspiron E1705/9400)
1337 */
1338 static const unsigned int dell9200_m27_pin_configs[8] = {
1339         0x40c003fa, 0x01441340, 0x0421121f, 0x90170310,
1340         0x90170310, 0x04a11020, 0x90170310, 0x40f003fc,
1341 };
1342
1343 static const unsigned int oqo9200_pin_configs[8] = {
1344         0x40c000f0, 0x404000f1, 0x0221121f, 0x02211210,
1345         0x90170111, 0x90a70120, 0x400000f2, 0x400000f3,
1346 };
1347
1348
1349 static const unsigned int *stac9200_brd_tbl[STAC_9200_MODELS] = {
1350         [STAC_REF] = ref9200_pin_configs,
1351         [STAC_9200_OQO] = oqo9200_pin_configs,
1352         [STAC_9200_DELL_D21] = dell9200_d21_pin_configs,
1353         [STAC_9200_DELL_D22] = dell9200_d22_pin_configs,
1354         [STAC_9200_DELL_D23] = dell9200_d23_pin_configs,
1355         [STAC_9200_DELL_M21] = dell9200_m21_pin_configs,
1356         [STAC_9200_DELL_M22] = dell9200_m22_pin_configs,
1357         [STAC_9200_DELL_M23] = dell9200_m23_pin_configs,
1358         [STAC_9200_DELL_M24] = dell9200_m24_pin_configs,
1359         [STAC_9200_DELL_M25] = dell9200_m25_pin_configs,
1360         [STAC_9200_DELL_M26] = dell9200_m26_pin_configs,
1361         [STAC_9200_DELL_M27] = dell9200_m27_pin_configs,
1362         [STAC_9200_M4] = gateway9200_m4_pin_configs,
1363         [STAC_9200_M4_2] = gateway9200_m4_2_pin_configs,
1364         [STAC_9200_PANASONIC] = ref9200_pin_configs,
1365 };
1366
1367 static const char * const stac9200_models[STAC_9200_MODELS] = {
1368         [STAC_AUTO] = "auto",
1369         [STAC_REF] = "ref",
1370         [STAC_9200_OQO] = "oqo",
1371         [STAC_9200_DELL_D21] = "dell-d21",
1372         [STAC_9200_DELL_D22] = "dell-d22",
1373         [STAC_9200_DELL_D23] = "dell-d23",
1374         [STAC_9200_DELL_M21] = "dell-m21",
1375         [STAC_9200_DELL_M22] = "dell-m22",
1376         [STAC_9200_DELL_M23] = "dell-m23",
1377         [STAC_9200_DELL_M24] = "dell-m24",
1378         [STAC_9200_DELL_M25] = "dell-m25",
1379         [STAC_9200_DELL_M26] = "dell-m26",
1380         [STAC_9200_DELL_M27] = "dell-m27",
1381         [STAC_9200_M4] = "gateway-m4",
1382         [STAC_9200_M4_2] = "gateway-m4-2",
1383         [STAC_9200_PANASONIC] = "panasonic",
1384 };
1385
1386 static const struct snd_pci_quirk stac9200_cfg_tbl[] = {
1387         /* SigmaTel reference board */
1388         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
1389                       "DFI LanParty", STAC_REF),
1390         SND_PCI_QUIRK(PCI_VENDOR_ID_DFI, 0x3101,
1391                       "DFI LanParty", STAC_REF),
1392         /* Dell laptops have BIOS problem */
1393         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01a8,
1394                       "unknown Dell", STAC_9200_DELL_D21),
1395         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01b5,
1396                       "Dell Inspiron 630m", STAC_9200_DELL_M21),
1397         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01bd,
1398                       "Dell Inspiron E1505n", STAC_9200_DELL_M25),
1399         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c0,
1400                       "unknown Dell", STAC_9200_DELL_D22),
1401         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c1,
1402                       "unknown Dell", STAC_9200_DELL_D22),
1403         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c2,
1404                       "Dell Latitude D620", STAC_9200_DELL_M22),
1405         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c5,
1406                       "unknown Dell", STAC_9200_DELL_D23),
1407         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c7,
1408                       "unknown Dell", STAC_9200_DELL_D23),
1409         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c8,
1410                       "unknown Dell", STAC_9200_DELL_M22),
1411         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c9,
1412                       "unknown Dell", STAC_9200_DELL_M24),
1413         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ca,
1414                       "unknown Dell", STAC_9200_DELL_M24),
1415         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cb,
1416                       "Dell Latitude 120L", STAC_9200_DELL_M24),
1417         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cc,
1418                       "Dell Latitude D820", STAC_9200_DELL_M22),
1419         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cd,
1420                       "Dell Inspiron E1705/9400", STAC_9200_DELL_M27),
1421         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ce,
1422                       "Dell XPS M1710", STAC_9200_DELL_M23),
1423         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cf,
1424                       "Dell Precision M90", STAC_9200_DELL_M23),
1425         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d3,
1426                       "unknown Dell", STAC_9200_DELL_M22),
1427         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d4,
1428                       "unknown Dell", STAC_9200_DELL_M22),
1429         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d6,
1430                       "unknown Dell", STAC_9200_DELL_M22),
1431         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d8,
1432                       "Dell Inspiron 640m", STAC_9200_DELL_M21),
1433         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d9,
1434                       "unknown Dell", STAC_9200_DELL_D23),
1435         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01da,
1436                       "unknown Dell", STAC_9200_DELL_D23),
1437         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01de,
1438                       "unknown Dell", STAC_9200_DELL_D21),
1439         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01e3,
1440                       "unknown Dell", STAC_9200_DELL_D23),
1441         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01e8,
1442                       "unknown Dell", STAC_9200_DELL_D21),
1443         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ee,
1444                       "unknown Dell", STAC_9200_DELL_M25),
1445         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ef,
1446                       "unknown Dell", STAC_9200_DELL_M25),
1447         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f5,
1448                       "Dell Inspiron 1501", STAC_9200_DELL_M26),
1449         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f6,
1450                       "unknown Dell", STAC_9200_DELL_M26),
1451         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0201,
1452                       "Dell Latitude D430", STAC_9200_DELL_M22),
1453         /* Panasonic */
1454         SND_PCI_QUIRK(0x10f7, 0x8338, "Panasonic CF-74", STAC_9200_PANASONIC),
1455         /* Gateway machines needs EAPD to be set on resume */
1456         SND_PCI_QUIRK(0x107b, 0x0205, "Gateway S-7110M", STAC_9200_M4),
1457         SND_PCI_QUIRK(0x107b, 0x0317, "Gateway MT3423, MX341*", STAC_9200_M4_2),
1458         SND_PCI_QUIRK(0x107b, 0x0318, "Gateway ML3019, MT3707", STAC_9200_M4_2),
1459         /* OQO Mobile */
1460         SND_PCI_QUIRK(0x1106, 0x3288, "OQO Model 2", STAC_9200_OQO),
1461         {} /* terminator */
1462 };
1463
1464 static const unsigned int ref925x_pin_configs[8] = {
1465         0x40c003f0, 0x424503f2, 0x01813022, 0x02a19021,
1466         0x90a70320, 0x02214210, 0x01019020, 0x9033032e,
1467 };
1468
1469 static const unsigned int stac925xM1_pin_configs[8] = {
1470         0x40c003f4, 0x424503f2, 0x400000f3, 0x02a19020,
1471         0x40a000f0, 0x90100210, 0x400003f1, 0x9033032e,
1472 };
1473
1474 static const unsigned int stac925xM1_2_pin_configs[8] = {
1475         0x40c003f4, 0x424503f2, 0x400000f3, 0x02a19020,
1476         0x40a000f0, 0x90100210, 0x400003f1, 0x9033032e,
1477 };
1478
1479 static const unsigned int stac925xM2_pin_configs[8] = {
1480         0x40c003f4, 0x424503f2, 0x400000f3, 0x02a19020,
1481         0x40a000f0, 0x90100210, 0x400003f1, 0x9033032e,
1482 };
1483
1484 static const unsigned int stac925xM2_2_pin_configs[8] = {
1485         0x40c003f4, 0x424503f2, 0x400000f3, 0x02a19020,
1486         0x40a000f0, 0x90100210, 0x400003f1, 0x9033032e,
1487 };
1488
1489 static const unsigned int stac925xM3_pin_configs[8] = {
1490         0x40c003f4, 0x424503f2, 0x400000f3, 0x02a19020,
1491         0x40a000f0, 0x90100210, 0x400003f1, 0x503303f3,
1492 };
1493
1494 static const unsigned int stac925xM5_pin_configs[8] = {
1495         0x40c003f4, 0x424503f2, 0x400000f3, 0x02a19020,
1496         0x40a000f0, 0x90100210, 0x400003f1, 0x9033032e,
1497 };
1498
1499 static const unsigned int stac925xM6_pin_configs[8] = {
1500         0x40c003f4, 0x424503f2, 0x400000f3, 0x02a19020,
1501         0x40a000f0, 0x90100210, 0x400003f1, 0x90330320,
1502 };
1503
1504 static const unsigned int *stac925x_brd_tbl[STAC_925x_MODELS] = {
1505         [STAC_REF] = ref925x_pin_configs,
1506         [STAC_M1] = stac925xM1_pin_configs,
1507         [STAC_M1_2] = stac925xM1_2_pin_configs,
1508         [STAC_M2] = stac925xM2_pin_configs,
1509         [STAC_M2_2] = stac925xM2_2_pin_configs,
1510         [STAC_M3] = stac925xM3_pin_configs,
1511         [STAC_M5] = stac925xM5_pin_configs,
1512         [STAC_M6] = stac925xM6_pin_configs,
1513 };
1514
1515 static const char * const stac925x_models[STAC_925x_MODELS] = {
1516         [STAC_925x_AUTO] = "auto",
1517         [STAC_REF] = "ref",
1518         [STAC_M1] = "m1",
1519         [STAC_M1_2] = "m1-2",
1520         [STAC_M2] = "m2",
1521         [STAC_M2_2] = "m2-2",
1522         [STAC_M3] = "m3",
1523         [STAC_M5] = "m5",
1524         [STAC_M6] = "m6",
1525 };
1526
1527 static const struct snd_pci_quirk stac925x_codec_id_cfg_tbl[] = {
1528         SND_PCI_QUIRK(0x107b, 0x0316, "Gateway M255", STAC_M2),
1529         SND_PCI_QUIRK(0x107b, 0x0366, "Gateway MP6954", STAC_M5),
1530         SND_PCI_QUIRK(0x107b, 0x0461, "Gateway NX560XL", STAC_M1),
1531         SND_PCI_QUIRK(0x107b, 0x0681, "Gateway NX860", STAC_M2),
1532         SND_PCI_QUIRK(0x107b, 0x0367, "Gateway MX6453", STAC_M1_2),
1533         /* Not sure about the brand name for those */
1534         SND_PCI_QUIRK(0x107b, 0x0281, "Gateway mobile", STAC_M1),
1535         SND_PCI_QUIRK(0x107b, 0x0507, "Gateway mobile", STAC_M3),
1536         SND_PCI_QUIRK(0x107b, 0x0281, "Gateway mobile", STAC_M6),
1537         SND_PCI_QUIRK(0x107b, 0x0685, "Gateway mobile", STAC_M2_2),
1538         {} /* terminator */
1539 };
1540
1541 static const struct snd_pci_quirk stac925x_cfg_tbl[] = {
1542         /* SigmaTel reference board */
1543         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668, "DFI LanParty", STAC_REF),
1544         SND_PCI_QUIRK(PCI_VENDOR_ID_DFI, 0x3101, "DFI LanParty", STAC_REF),
1545         SND_PCI_QUIRK(0x8384, 0x7632, "Stac9202 Reference Board", STAC_REF),
1546
1547         /* Default table for unknown ID */
1548         SND_PCI_QUIRK(0x1002, 0x437b, "Gateway mobile", STAC_M2_2),
1549
1550         {} /* terminator */
1551 };
1552
1553 static const unsigned int ref92hd73xx_pin_configs[13] = {
1554         0x02214030, 0x02a19040, 0x01a19020, 0x02214030,
1555         0x0181302e, 0x01014010, 0x01014020, 0x01014030,
1556         0x02319040, 0x90a000f0, 0x90a000f0, 0x01452050,
1557         0x01452050,
1558 };
1559
1560 static const unsigned int dell_m6_pin_configs[13] = {
1561         0x0321101f, 0x4f00000f, 0x4f0000f0, 0x90170110,
1562         0x03a11020, 0x0321101f, 0x4f0000f0, 0x4f0000f0,
1563         0x4f0000f0, 0x90a60160, 0x4f0000f0, 0x4f0000f0,
1564         0x4f0000f0,
1565 };
1566
1567 static const unsigned int alienware_m17x_pin_configs[13] = {
1568         0x0321101f, 0x0321101f, 0x03a11020, 0x03014020,
1569         0x90170110, 0x4f0000f0, 0x4f0000f0, 0x4f0000f0,
1570         0x4f0000f0, 0x90a60160, 0x4f0000f0, 0x4f0000f0,
1571         0x904601b0,
1572 };
1573
1574 static const unsigned int intel_dg45id_pin_configs[13] = {
1575         0x02214230, 0x02A19240, 0x01013214, 0x01014210,
1576         0x01A19250, 0x01011212, 0x01016211
1577 };
1578
1579 static const unsigned int *stac92hd73xx_brd_tbl[STAC_92HD73XX_MODELS] = {
1580         [STAC_92HD73XX_REF]     = ref92hd73xx_pin_configs,
1581         [STAC_DELL_M6_AMIC]     = dell_m6_pin_configs,
1582         [STAC_DELL_M6_DMIC]     = dell_m6_pin_configs,
1583         [STAC_DELL_M6_BOTH]     = dell_m6_pin_configs,
1584         [STAC_DELL_EQ]  = dell_m6_pin_configs,
1585         [STAC_ALIENWARE_M17X]   = alienware_m17x_pin_configs,
1586         [STAC_92HD73XX_INTEL]   = intel_dg45id_pin_configs,
1587 };
1588
1589 static const char * const stac92hd73xx_models[STAC_92HD73XX_MODELS] = {
1590         [STAC_92HD73XX_AUTO] = "auto",
1591         [STAC_92HD73XX_NO_JD] = "no-jd",
1592         [STAC_92HD73XX_REF] = "ref",
1593         [STAC_92HD73XX_INTEL] = "intel",
1594         [STAC_DELL_M6_AMIC] = "dell-m6-amic",
1595         [STAC_DELL_M6_DMIC] = "dell-m6-dmic",
1596         [STAC_DELL_M6_BOTH] = "dell-m6",
1597         [STAC_DELL_EQ] = "dell-eq",
1598         [STAC_ALIENWARE_M17X] = "alienware",
1599 };
1600
1601 static const struct snd_pci_quirk stac92hd73xx_cfg_tbl[] = {
1602         /* SigmaTel reference board */
1603         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
1604                                 "DFI LanParty", STAC_92HD73XX_REF),
1605         SND_PCI_QUIRK(PCI_VENDOR_ID_DFI, 0x3101,
1606                                 "DFI LanParty", STAC_92HD73XX_REF),
1607         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x5002,
1608                                 "Intel DG45ID", STAC_92HD73XX_INTEL),
1609         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x5003,
1610                                 "Intel DG45FC", STAC_92HD73XX_INTEL),
1611         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0254,
1612                                 "Dell Studio 1535", STAC_DELL_M6_DMIC),
1613         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0255,
1614                                 "unknown Dell", STAC_DELL_M6_DMIC),
1615         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0256,
1616                                 "unknown Dell", STAC_DELL_M6_BOTH),
1617         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0257,
1618                                 "unknown Dell", STAC_DELL_M6_BOTH),
1619         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x025e,
1620                                 "unknown Dell", STAC_DELL_M6_AMIC),
1621         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x025f,
1622                                 "unknown Dell", STAC_DELL_M6_AMIC),
1623         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0271,
1624                                 "unknown Dell", STAC_DELL_M6_DMIC),
1625         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0272,
1626                                 "unknown Dell", STAC_DELL_M6_DMIC),
1627         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x029f,
1628                                 "Dell Studio 1537", STAC_DELL_M6_DMIC),
1629         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x02a0,
1630                                 "Dell Studio 17", STAC_DELL_M6_DMIC),
1631         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x02be,
1632                                 "Dell Studio 1555", STAC_DELL_M6_DMIC),
1633         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x02bd,
1634                                 "Dell Studio 1557", STAC_DELL_M6_DMIC),
1635         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x02fe,
1636                                 "Dell Studio XPS 1645", STAC_DELL_M6_DMIC),
1637         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0413,
1638                                 "Dell Studio 1558", STAC_DELL_M6_DMIC),
1639         {} /* terminator */
1640 };
1641
1642 static const struct snd_pci_quirk stac92hd73xx_codec_id_cfg_tbl[] = {
1643         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x02a1,
1644                       "Alienware M17x", STAC_ALIENWARE_M17X),
1645         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x043a,
1646                       "Alienware M17x", STAC_ALIENWARE_M17X),
1647         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0490,
1648                       "Alienware M17x", STAC_ALIENWARE_M17X),
1649         {} /* terminator */
1650 };
1651
1652 static const unsigned int ref92hd83xxx_pin_configs[10] = {
1653         0x02214030, 0x02211010, 0x02a19020, 0x02170130,
1654         0x01014050, 0x01819040, 0x01014020, 0x90a3014e,
1655         0x01451160, 0x98560170,
1656 };
1657
1658 static const unsigned int dell_s14_pin_configs[10] = {
1659         0x0221403f, 0x0221101f, 0x02a19020, 0x90170110,
1660         0x40f000f0, 0x40f000f0, 0x40f000f0, 0x90a60160,
1661         0x40f000f0, 0x40f000f0,
1662 };
1663
1664 static const unsigned int dell_vostro_3500_pin_configs[10] = {
1665         0x02a11020, 0x0221101f, 0x400000f0, 0x90170110,
1666         0x400000f1, 0x400000f2, 0x400000f3, 0x90a60160,
1667         0x400000f4, 0x400000f5,
1668 };
1669
1670 static const unsigned int hp_dv7_4000_pin_configs[10] = {
1671         0x03a12050, 0x0321201f, 0x40f000f0, 0x90170110,
1672         0x40f000f0, 0x40f000f0, 0x90170110, 0xd5a30140,
1673         0x40f000f0, 0x40f000f0,
1674 };
1675
1676 static const unsigned int hp_cNB11_intquad_pin_configs[10] = {
1677         0x40f000f0, 0x0221101f, 0x02a11020, 0x92170110,
1678         0x40f000f0, 0x92170110, 0x40f000f0, 0xd5a30130,
1679         0x40f000f0, 0x40f000f0,
1680 };
1681
1682 static const unsigned int *stac92hd83xxx_brd_tbl[STAC_92HD83XXX_MODELS] = {
1683         [STAC_92HD83XXX_REF] = ref92hd83xxx_pin_configs,
1684         [STAC_92HD83XXX_PWR_REF] = ref92hd83xxx_pin_configs,
1685         [STAC_DELL_S14] = dell_s14_pin_configs,
1686         [STAC_DELL_VOSTRO_3500] = dell_vostro_3500_pin_configs,
1687         [STAC_92HD83XXX_HP_cNB11_INTQUAD] = hp_cNB11_intquad_pin_configs,
1688         [STAC_HP_DV7_4000] = hp_dv7_4000_pin_configs,
1689 };
1690
1691 static const char * const stac92hd83xxx_models[STAC_92HD83XXX_MODELS] = {
1692         [STAC_92HD83XXX_AUTO] = "auto",
1693         [STAC_92HD83XXX_REF] = "ref",
1694         [STAC_92HD83XXX_PWR_REF] = "mic-ref",
1695         [STAC_DELL_S14] = "dell-s14",
1696         [STAC_DELL_VOSTRO_3500] = "dell-vostro-3500",
1697         [STAC_92HD83XXX_HP] = "hp",
1698         [STAC_92HD83XXX_HP_cNB11_INTQUAD] = "hp_cNB11_intquad",
1699         [STAC_HP_DV7_4000] = "hp-dv7-4000",
1700 };
1701
1702 static const struct snd_pci_quirk stac92hd83xxx_cfg_tbl[] = {
1703         /* SigmaTel reference board */
1704         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
1705                       "DFI LanParty", STAC_92HD83XXX_REF),
1706         SND_PCI_QUIRK(PCI_VENDOR_ID_DFI, 0x3101,
1707                       "DFI LanParty", STAC_92HD83XXX_REF),
1708         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x02ba,
1709                       "unknown Dell", STAC_DELL_S14),
1710         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x1028,
1711                       "Dell Vostro 3500", STAC_DELL_VOSTRO_3500),
1712         SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_HP, 0xff00, 0x3600,
1713                           "HP", STAC_92HD83XXX_HP),
1714         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x1656,
1715                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1716         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x1657,
1717                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1718         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x1658,
1719                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1720         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x1659,
1721                           "HP Pavilion dv7", STAC_HP_DV7_4000),
1722         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x165A,
1723                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1724         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x165B,
1725                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1726         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x3388,
1727                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1728         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x3389,
1729                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1730         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x355B,
1731                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1732         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x355C,
1733                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1734         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x355D,
1735                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1736         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x355E,
1737                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1738         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x355F,
1739                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1740         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x3560,
1741                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1742         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x358B,
1743                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1744         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x358C,
1745                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1746         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x358D,
1747                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1748         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x3591,
1749                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1750         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x3592,
1751                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1752         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x3593,
1753                           "HP", STAC_92HD83XXX_HP_cNB11_INTQUAD),
1754         {} /* terminator */
1755 };
1756
1757 static const unsigned int ref92hd71bxx_pin_configs[STAC92HD71BXX_NUM_PINS] = {
1758         0x02214030, 0x02a19040, 0x01a19020, 0x01014010,
1759         0x0181302e, 0x01014010, 0x01019020, 0x90a000f0,
1760         0x90a000f0, 0x01452050, 0x01452050, 0x00000000,
1761         0x00000000
1762 };
1763
1764 static const unsigned int dell_m4_1_pin_configs[STAC92HD71BXX_NUM_PINS] = {
1765         0x0421101f, 0x04a11221, 0x40f000f0, 0x90170110,
1766         0x23a1902e, 0x23014250, 0x40f000f0, 0x90a000f0,
1767         0x40f000f0, 0x4f0000f0, 0x4f0000f0, 0x00000000,
1768         0x00000000
1769 };
1770
1771 static const unsigned int dell_m4_2_pin_configs[STAC92HD71BXX_NUM_PINS] = {
1772         0x0421101f, 0x04a11221, 0x90a70330, 0x90170110,
1773         0x23a1902e, 0x23014250, 0x40f000f0, 0x40f000f0,
1774         0x40f000f0, 0x044413b0, 0x044413b0, 0x00000000,
1775         0x00000000
1776 };
1777
1778 static const unsigned int dell_m4_3_pin_configs[STAC92HD71BXX_NUM_PINS] = {
1779         0x0421101f, 0x04a11221, 0x90a70330, 0x90170110,
1780         0x40f000f0, 0x40f000f0, 0x40f000f0, 0x90a000f0,
1781         0x40f000f0, 0x044413b0, 0x044413b0, 0x00000000,
1782         0x00000000
1783 };
1784
1785 static const unsigned int *stac92hd71bxx_brd_tbl[STAC_92HD71BXX_MODELS] = {
1786         [STAC_92HD71BXX_REF] = ref92hd71bxx_pin_configs,
1787         [STAC_DELL_M4_1]        = dell_m4_1_pin_configs,
1788         [STAC_DELL_M4_2]        = dell_m4_2_pin_configs,
1789         [STAC_DELL_M4_3]        = dell_m4_3_pin_configs,
1790         [STAC_HP_M4]            = NULL,
1791         [STAC_HP_DV4]           = NULL,
1792         [STAC_HP_DV5]           = NULL,
1793         [STAC_HP_HDX]           = NULL,
1794         [STAC_HP_DV4_1222NR]    = NULL,
1795 };
1796
1797 static const char * const stac92hd71bxx_models[STAC_92HD71BXX_MODELS] = {
1798         [STAC_92HD71BXX_AUTO] = "auto",
1799         [STAC_92HD71BXX_REF] = "ref",
1800         [STAC_DELL_M4_1] = "dell-m4-1",
1801         [STAC_DELL_M4_2] = "dell-m4-2",
1802         [STAC_DELL_M4_3] = "dell-m4-3",
1803         [STAC_HP_M4] = "hp-m4",
1804         [STAC_HP_DV4] = "hp-dv4",
1805         [STAC_HP_DV5] = "hp-dv5",
1806         [STAC_HP_HDX] = "hp-hdx",
1807         [STAC_HP_DV4_1222NR] = "hp-dv4-1222nr",
1808 };
1809
1810 static const struct snd_pci_quirk stac92hd71bxx_cfg_tbl[] = {
1811         /* SigmaTel reference board */
1812         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
1813                       "DFI LanParty", STAC_92HD71BXX_REF),
1814         SND_PCI_QUIRK(PCI_VENDOR_ID_DFI, 0x3101,
1815                       "DFI LanParty", STAC_92HD71BXX_REF),
1816         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x30fb,
1817                       "HP dv4-1222nr", STAC_HP_DV4_1222NR),
1818         SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_HP, 0xfff0, 0x1720,
1819                           "HP", STAC_HP_DV5),
1820         SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_HP, 0xfff0, 0x3080,
1821                       "HP", STAC_HP_DV5),
1822         SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_HP, 0xfff0, 0x30f0,
1823                       "HP dv4-7", STAC_HP_DV4),
1824         SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_HP, 0xfff0, 0x3600,
1825                       "HP dv4-7", STAC_HP_DV5),
1826         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x3610,
1827                       "HP HDX", STAC_HP_HDX),  /* HDX18 */
1828         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x361a,
1829                       "HP mini 1000", STAC_HP_M4),
1830         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x361b,
1831                       "HP HDX", STAC_HP_HDX),  /* HDX16 */
1832         SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_HP, 0xfff0, 0x3620,
1833                       "HP dv6", STAC_HP_DV5),
1834         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x3061,
1835                       "HP dv6", STAC_HP_DV5), /* HP dv6-1110ax */
1836         SND_PCI_QUIRK(PCI_VENDOR_ID_HP, 0x363e,
1837                       "HP DV6", STAC_HP_DV5),
1838         SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_HP, 0xfff0, 0x7010,
1839                       "HP", STAC_HP_DV5),
1840         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0233,
1841                                 "unknown Dell", STAC_DELL_M4_1),
1842         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0234,
1843                                 "unknown Dell", STAC_DELL_M4_1),
1844         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0250,
1845                                 "unknown Dell", STAC_DELL_M4_1),
1846         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x024f,
1847                                 "unknown Dell", STAC_DELL_M4_1),
1848         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x024d,
1849                                 "unknown Dell", STAC_DELL_M4_1),
1850         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0251,
1851                                 "unknown Dell", STAC_DELL_M4_1),
1852         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0277,
1853                                 "unknown Dell", STAC_DELL_M4_1),
1854         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0263,
1855                                 "unknown Dell", STAC_DELL_M4_2),
1856         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0265,
1857                                 "unknown Dell", STAC_DELL_M4_2),
1858         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0262,
1859                                 "unknown Dell", STAC_DELL_M4_2),
1860         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0264,
1861                                 "unknown Dell", STAC_DELL_M4_2),
1862         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x02aa,
1863                                 "unknown Dell", STAC_DELL_M4_3),
1864         {} /* terminator */
1865 };
1866
1867 static const unsigned int ref922x_pin_configs[10] = {
1868         0x01014010, 0x01016011, 0x01012012, 0x0221401f,
1869         0x01813122, 0x01011014, 0x01441030, 0x01c41030,
1870         0x40000100, 0x40000100,
1871 };
1872
1873 /*
1874     STAC 922X pin configs for
1875     102801A7
1876     102801AB
1877     102801A9
1878     102801D1
1879     102801D2
1880 */
1881 static const unsigned int dell_922x_d81_pin_configs[10] = {
1882         0x02214030, 0x01a19021, 0x01111012, 0x01114010,
1883         0x02a19020, 0x01117011, 0x400001f0, 0x400001f1,
1884         0x01813122, 0x400001f2,
1885 };
1886
1887 /*
1888     STAC 922X pin configs for
1889     102801AC
1890     102801D0
1891 */
1892 static const unsigned int dell_922x_d82_pin_configs[10] = {
1893         0x02214030, 0x01a19021, 0x01111012, 0x01114010,
1894         0x02a19020, 0x01117011, 0x01451140, 0x400001f0,
1895         0x01813122, 0x400001f1,
1896 };
1897
1898 /*
1899     STAC 922X pin configs for
1900     102801BF
1901 */
1902 static const unsigned int dell_922x_m81_pin_configs[10] = {
1903         0x0321101f, 0x01112024, 0x01111222, 0x91174220,
1904         0x03a11050, 0x01116221, 0x90a70330, 0x01452340, 
1905         0x40C003f1, 0x405003f0,
1906 };
1907
1908 /*
1909     STAC 9221 A1 pin configs for
1910     102801D7 (Dell XPS M1210)
1911 */
1912 static const unsigned int dell_922x_m82_pin_configs[10] = {
1913         0x02211211, 0x408103ff, 0x02a1123e, 0x90100310, 
1914         0x408003f1, 0x0221121f, 0x03451340, 0x40c003f2, 
1915         0x508003f3, 0x405003f4, 
1916 };
1917
1918 static const unsigned int d945gtp3_pin_configs[10] = {
1919         0x0221401f, 0x01a19022, 0x01813021, 0x01014010,
1920         0x40000100, 0x40000100, 0x40000100, 0x40000100,
1921         0x02a19120, 0x40000100,
1922 };
1923
1924 static const unsigned int d945gtp5_pin_configs[10] = {
1925         0x0221401f, 0x01011012, 0x01813024, 0x01014010,
1926         0x01a19021, 0x01016011, 0x01452130, 0x40000100,
1927         0x02a19320, 0x40000100,
1928 };
1929
1930 static const unsigned int intel_mac_v1_pin_configs[10] = {
1931         0x0121e21f, 0x400000ff, 0x9017e110, 0x400000fd,
1932         0x400000fe, 0x0181e020, 0x1145e030, 0x11c5e240,
1933         0x400000fc, 0x400000fb,
1934 };
1935
1936 static const unsigned int intel_mac_v2_pin_configs[10] = {
1937         0x0121e21f, 0x90a7012e, 0x9017e110, 0x400000fd,
1938         0x400000fe, 0x0181e020, 0x1145e230, 0x500000fa,
1939         0x400000fc, 0x400000fb,
1940 };
1941
1942 static const unsigned int intel_mac_v3_pin_configs[10] = {
1943         0x0121e21f, 0x90a7012e, 0x9017e110, 0x400000fd,
1944         0x400000fe, 0x0181e020, 0x1145e230, 0x11c5e240,
1945         0x400000fc, 0x400000fb,
1946 };
1947
1948 static const unsigned int intel_mac_v4_pin_configs[10] = {
1949         0x0321e21f, 0x03a1e02e, 0x9017e110, 0x9017e11f,
1950         0x400000fe, 0x0381e020, 0x1345e230, 0x13c5e240,
1951         0x400000fc, 0x400000fb,
1952 };
1953
1954 static const unsigned int intel_mac_v5_pin_configs[10] = {
1955         0x0321e21f, 0x03a1e02e, 0x9017e110, 0x9017e11f,
1956         0x400000fe, 0x0381e020, 0x1345e230, 0x13c5e240,
1957         0x400000fc, 0x400000fb,
1958 };
1959
1960 static const unsigned int ecs202_pin_configs[10] = {
1961         0x0221401f, 0x02a19020, 0x01a19020, 0x01114010,
1962         0x408000f0, 0x01813022, 0x074510a0, 0x40c400f1,
1963         0x9037012e, 0x40e000f2,
1964 };
1965
1966 static const unsigned int *stac922x_brd_tbl[STAC_922X_MODELS] = {
1967         [STAC_D945_REF] = ref922x_pin_configs,
1968         [STAC_D945GTP3] = d945gtp3_pin_configs,
1969         [STAC_D945GTP5] = d945gtp5_pin_configs,
1970         [STAC_INTEL_MAC_V1] = intel_mac_v1_pin_configs,
1971         [STAC_INTEL_MAC_V2] = intel_mac_v2_pin_configs,
1972         [STAC_INTEL_MAC_V3] = intel_mac_v3_pin_configs,
1973         [STAC_INTEL_MAC_V4] = intel_mac_v4_pin_configs,
1974         [STAC_INTEL_MAC_V5] = intel_mac_v5_pin_configs,
1975         [STAC_INTEL_MAC_AUTO] = intel_mac_v3_pin_configs,
1976         /* for backward compatibility */
1977         [STAC_MACMINI] = intel_mac_v3_pin_configs,
1978         [STAC_MACBOOK] = intel_mac_v5_pin_configs,
1979         [STAC_MACBOOK_PRO_V1] = intel_mac_v3_pin_configs,
1980         [STAC_MACBOOK_PRO_V2] = intel_mac_v3_pin_configs,
1981         [STAC_IMAC_INTEL] = intel_mac_v2_pin_configs,
1982         [STAC_IMAC_INTEL_20] = intel_mac_v3_pin_configs,
1983         [STAC_ECS_202] = ecs202_pin_configs,
1984         [STAC_922X_DELL_D81] = dell_922x_d81_pin_configs,
1985         [STAC_922X_DELL_D82] = dell_922x_d82_pin_configs,       
1986         [STAC_922X_DELL_M81] = dell_922x_m81_pin_configs,
1987         [STAC_922X_DELL_M82] = dell_922x_m82_pin_configs,       
1988 };
1989
1990 static const char * const stac922x_models[STAC_922X_MODELS] = {
1991         [STAC_922X_AUTO] = "auto",
1992         [STAC_D945_REF] = "ref",
1993         [STAC_D945GTP5] = "5stack",
1994         [STAC_D945GTP3] = "3stack",
1995         [STAC_INTEL_MAC_V1] = "intel-mac-v1",
1996         [STAC_INTEL_MAC_V2] = "intel-mac-v2",
1997         [STAC_INTEL_MAC_V3] = "intel-mac-v3",
1998         [STAC_INTEL_MAC_V4] = "intel-mac-v4",
1999         [STAC_INTEL_MAC_V5] = "intel-mac-v5",
2000         [STAC_INTEL_MAC_AUTO] = "intel-mac-auto",
2001         /* for backward compatibility */
2002         [STAC_MACMINI]  = "macmini",
2003         [STAC_MACBOOK]  = "macbook",
2004         [STAC_MACBOOK_PRO_V1]   = "macbook-pro-v1",
2005         [STAC_MACBOOK_PRO_V2]   = "macbook-pro",
2006         [STAC_IMAC_INTEL] = "imac-intel",
2007         [STAC_IMAC_INTEL_20] = "imac-intel-20",
2008         [STAC_ECS_202] = "ecs202",
2009         [STAC_922X_DELL_D81] = "dell-d81",
2010         [STAC_922X_DELL_D82] = "dell-d82",
2011         [STAC_922X_DELL_M81] = "dell-m81",
2012         [STAC_922X_DELL_M82] = "dell-m82",
2013 };
2014
2015 static const struct snd_pci_quirk stac922x_cfg_tbl[] = {
2016         /* SigmaTel reference board */
2017         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
2018                       "DFI LanParty", STAC_D945_REF),
2019         SND_PCI_QUIRK(PCI_VENDOR_ID_DFI, 0x3101,
2020                       "DFI LanParty", STAC_D945_REF),
2021         /* Intel 945G based systems */
2022         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0101,
2023                       "Intel D945G", STAC_D945GTP3),
2024         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0202,
2025                       "Intel D945G", STAC_D945GTP3),
2026         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0606,
2027                       "Intel D945G", STAC_D945GTP3),
2028         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0601,
2029                       "Intel D945G", STAC_D945GTP3),
2030         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0111,
2031                       "Intel D945G", STAC_D945GTP3),
2032         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1115,
2033                       "Intel D945G", STAC_D945GTP3),
2034         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1116,
2035                       "Intel D945G", STAC_D945GTP3),
2036         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1117,
2037                       "Intel D945G", STAC_D945GTP3),
2038         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1118,
2039                       "Intel D945G", STAC_D945GTP3),
2040         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1119,
2041                       "Intel D945G", STAC_D945GTP3),
2042         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x8826,
2043                       "Intel D945G", STAC_D945GTP3),
2044         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x5049,
2045                       "Intel D945G", STAC_D945GTP3),
2046         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x5055,
2047                       "Intel D945G", STAC_D945GTP3),
2048         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x5048,
2049                       "Intel D945G", STAC_D945GTP3),
2050         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0110,
2051                       "Intel D945G", STAC_D945GTP3),
2052         /* Intel D945G 5-stack systems */
2053         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0404,
2054                       "Intel D945G", STAC_D945GTP5),
2055         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0303,
2056                       "Intel D945G", STAC_D945GTP5),
2057         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0013,
2058                       "Intel D945G", STAC_D945GTP5),
2059         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0417,
2060                       "Intel D945G", STAC_D945GTP5),
2061         /* Intel 945P based systems */
2062         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0b0b,
2063                       "Intel D945P", STAC_D945GTP3),
2064         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0112,
2065                       "Intel D945P", STAC_D945GTP3),
2066         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0d0d,
2067                       "Intel D945P", STAC_D945GTP3),
2068         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0909,
2069                       "Intel D945P", STAC_D945GTP3),
2070         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0505,
2071                       "Intel D945P", STAC_D945GTP3),
2072         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0707,
2073                       "Intel D945P", STAC_D945GTP5),
2074         /* other intel */
2075         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0204,
2076                       "Intel D945", STAC_D945_REF),
2077         /* other systems  */
2078         /* Apple Intel Mac (Mac Mini, MacBook, MacBook Pro...) */
2079         SND_PCI_QUIRK(0x8384, 0x7680,
2080                       "Mac", STAC_INTEL_MAC_AUTO),
2081         /* Dell systems  */
2082         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01a7,
2083                       "unknown Dell", STAC_922X_DELL_D81),
2084         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01a9,
2085                       "unknown Dell", STAC_922X_DELL_D81),
2086         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ab,
2087                       "unknown Dell", STAC_922X_DELL_D81),
2088         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ac,
2089                       "unknown Dell", STAC_922X_DELL_D82),
2090         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01bf,
2091                       "unknown Dell", STAC_922X_DELL_M81),
2092         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d0,
2093                       "unknown Dell", STAC_922X_DELL_D82),
2094         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d1,
2095                       "unknown Dell", STAC_922X_DELL_D81),
2096         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d2,
2097                       "unknown Dell", STAC_922X_DELL_D81),
2098         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d7,
2099                       "Dell XPS M1210", STAC_922X_DELL_M82),
2100         /* ECS/PC Chips boards */
2101         SND_PCI_QUIRK_MASK(0x1019, 0xf000, 0x2000,
2102                       "ECS/PC chips", STAC_ECS_202),
2103         {} /* terminator */
2104 };
2105
2106 static const unsigned int ref927x_pin_configs[14] = {
2107         0x02214020, 0x02a19080, 0x0181304e, 0x01014010,
2108         0x01a19040, 0x01011012, 0x01016011, 0x0101201f, 
2109         0x183301f0, 0x18a001f0, 0x18a001f0, 0x01442070,
2110         0x01c42190, 0x40000100,
2111 };
2112
2113 static const unsigned int d965_3st_pin_configs[14] = {
2114         0x0221401f, 0x02a19120, 0x40000100, 0x01014011,
2115         0x01a19021, 0x01813024, 0x40000100, 0x40000100,
2116         0x40000100, 0x40000100, 0x40000100, 0x40000100,
2117         0x40000100, 0x40000100
2118 };
2119
2120 static const unsigned int d965_5st_pin_configs[14] = {
2121         0x02214020, 0x02a19080, 0x0181304e, 0x01014010,
2122         0x01a19040, 0x01011012, 0x01016011, 0x40000100,
2123         0x40000100, 0x40000100, 0x40000100, 0x01442070,
2124         0x40000100, 0x40000100
2125 };
2126
2127 static const unsigned int d965_5st_no_fp_pin_configs[14] = {
2128         0x40000100, 0x40000100, 0x0181304e, 0x01014010,
2129         0x01a19040, 0x01011012, 0x01016011, 0x40000100,
2130         0x40000100, 0x40000100, 0x40000100, 0x01442070,
2131         0x40000100, 0x40000100
2132 };
2133
2134 static const unsigned int dell_3st_pin_configs[14] = {
2135         0x02211230, 0x02a11220, 0x01a19040, 0x01114210,
2136         0x01111212, 0x01116211, 0x01813050, 0x01112214,
2137         0x403003fa, 0x90a60040, 0x90a60040, 0x404003fb,
2138         0x40c003fc, 0x40000100
2139 };
2140
2141 static const unsigned int *stac927x_brd_tbl[STAC_927X_MODELS] = {
2142         [STAC_D965_REF_NO_JD] = ref927x_pin_configs,
2143         [STAC_D965_REF]  = ref927x_pin_configs,
2144         [STAC_D965_3ST]  = d965_3st_pin_configs,
2145         [STAC_D965_5ST]  = d965_5st_pin_configs,
2146         [STAC_D965_5ST_NO_FP]  = d965_5st_no_fp_pin_configs,
2147         [STAC_DELL_3ST]  = dell_3st_pin_configs,
2148         [STAC_DELL_BIOS] = NULL,
2149         [STAC_927X_VOLKNOB] = NULL,
2150 };
2151
2152 static const char * const stac927x_models[STAC_927X_MODELS] = {
2153         [STAC_927X_AUTO]        = "auto",
2154         [STAC_D965_REF_NO_JD]   = "ref-no-jd",
2155         [STAC_D965_REF]         = "ref",
2156         [STAC_D965_3ST]         = "3stack",
2157         [STAC_D965_5ST]         = "5stack",
2158         [STAC_D965_5ST_NO_FP]   = "5stack-no-fp",
2159         [STAC_DELL_3ST]         = "dell-3stack",
2160         [STAC_DELL_BIOS]        = "dell-bios",
2161         [STAC_927X_VOLKNOB]     = "volknob",
2162 };
2163
2164 static const struct snd_pci_quirk stac927x_cfg_tbl[] = {
2165         /* SigmaTel reference board */
2166         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
2167                       "DFI LanParty", STAC_D965_REF),
2168         SND_PCI_QUIRK(PCI_VENDOR_ID_DFI, 0x3101,
2169                       "DFI LanParty", STAC_D965_REF),
2170          /* Intel 946 based systems */
2171         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x3d01, "Intel D946", STAC_D965_3ST),
2172         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0xa301, "Intel D946", STAC_D965_3ST),
2173         /* 965 based 3 stack systems */
2174         SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_INTEL, 0xff00, 0x2100,
2175                            "Intel D965", STAC_D965_3ST),
2176         SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_INTEL, 0xff00, 0x2000,
2177                            "Intel D965", STAC_D965_3ST),
2178         /* Dell 3 stack systems */
2179         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x01dd, "Dell Dimension E520", STAC_DELL_3ST),
2180         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x01ed, "Dell     ", STAC_DELL_3ST),
2181         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x01f4, "Dell     ", STAC_DELL_3ST),
2182         /* Dell 3 stack systems with verb table in BIOS */
2183         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x01f3, "Dell Inspiron 1420", STAC_DELL_BIOS),
2184         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x01f7, "Dell XPS M1730", STAC_DELL_BIOS),
2185         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x0227, "Dell Vostro 1400  ", STAC_DELL_BIOS),
2186         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x022e, "Dell     ", STAC_DELL_BIOS),
2187         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x022f, "Dell Inspiron 1525", STAC_DELL_BIOS),
2188         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x0242, "Dell     ", STAC_DELL_BIOS),
2189         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x0243, "Dell     ", STAC_DELL_BIOS),
2190         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x02ff, "Dell     ", STAC_DELL_BIOS),
2191         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x0209, "Dell XPS 1330", STAC_DELL_BIOS),
2192         /* 965 based 5 stack systems */
2193         SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_INTEL, 0xff00, 0x2300,
2194                            "Intel D965", STAC_D965_5ST),
2195         SND_PCI_QUIRK_MASK(PCI_VENDOR_ID_INTEL, 0xff00, 0x2500,
2196                            "Intel D965", STAC_D965_5ST),
2197         /* volume-knob fixes */
2198         SND_PCI_QUIRK_VENDOR(0x10cf, "FSC", STAC_927X_VOLKNOB),
2199         {} /* terminator */
2200 };
2201
2202 static const unsigned int ref9205_pin_configs[12] = {
2203         0x40000100, 0x40000100, 0x01016011, 0x01014010,
2204         0x01813122, 0x01a19021, 0x01019020, 0x40000100,
2205         0x90a000f0, 0x90a000f0, 0x01441030, 0x01c41030
2206 };
2207
2208 /*
2209     STAC 9205 pin configs for
2210     102801F1
2211     102801F2
2212     102801FC
2213     102801FD
2214     10280204
2215     1028021F
2216     10280228 (Dell Vostro 1500)
2217     10280229 (Dell Vostro 1700)
2218 */
2219 static const unsigned int dell_9205_m42_pin_configs[12] = {
2220         0x0321101F, 0x03A11020, 0x400003FA, 0x90170310,
2221         0x400003FB, 0x400003FC, 0x400003FD, 0x40F000F9,
2222         0x90A60330, 0x400003FF, 0x0144131F, 0x40C003FE,
2223 };
2224
2225 /*
2226     STAC 9205 pin configs for
2227     102801F9
2228     102801FA
2229     102801FE
2230     102801FF (Dell Precision M4300)
2231     10280206
2232     10280200
2233     10280201
2234 */
2235 static const unsigned int dell_9205_m43_pin_configs[12] = {
2236         0x0321101f, 0x03a11020, 0x90a70330, 0x90170310,
2237         0x400000fe, 0x400000ff, 0x400000fd, 0x40f000f9,
2238         0x400000fa, 0x400000fc, 0x0144131f, 0x40c003f8,
2239 };
2240
2241 static const unsigned int dell_9205_m44_pin_configs[12] = {
2242         0x0421101f, 0x04a11020, 0x400003fa, 0x90170310,
2243         0x400003fb, 0x400003fc, 0x400003fd, 0x400003f9,
2244         0x90a60330, 0x400003ff, 0x01441340, 0x40c003fe,
2245 };
2246
2247 static const unsigned int *stac9205_brd_tbl[STAC_9205_MODELS] = {
2248         [STAC_9205_REF] = ref9205_pin_configs,
2249         [STAC_9205_DELL_M42] = dell_9205_m42_pin_configs,
2250         [STAC_9205_DELL_M43] = dell_9205_m43_pin_configs,
2251         [STAC_9205_DELL_M44] = dell_9205_m44_pin_configs,
2252         [STAC_9205_EAPD] = NULL,
2253 };
2254
2255 static const char * const stac9205_models[STAC_9205_MODELS] = {
2256         [STAC_9205_AUTO] = "auto",
2257         [STAC_9205_REF] = "ref",
2258         [STAC_9205_DELL_M42] = "dell-m42",
2259         [STAC_9205_DELL_M43] = "dell-m43",
2260         [STAC_9205_DELL_M44] = "dell-m44",
2261         [STAC_9205_EAPD] = "eapd",
2262 };
2263
2264 static const struct snd_pci_quirk stac9205_cfg_tbl[] = {
2265         /* SigmaTel reference board */
2266         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
2267                       "DFI LanParty", STAC_9205_REF),
2268         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0xfb30,
2269                       "SigmaTel", STAC_9205_REF),
2270         SND_PCI_QUIRK(PCI_VENDOR_ID_DFI, 0x3101,
2271                       "DFI LanParty", STAC_9205_REF),
2272         /* Dell */
2273         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f1,
2274                       "unknown Dell", STAC_9205_DELL_M42),
2275         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f2,
2276                       "unknown Dell", STAC_9205_DELL_M42),
2277         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f8,
2278                       "Dell Precision", STAC_9205_DELL_M43),
2279         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f9,
2280                       "Dell Precision", STAC_9205_DELL_M43),
2281         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01fa,
2282                       "Dell Precision", STAC_9205_DELL_M43),
2283         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01fc,
2284                       "unknown Dell", STAC_9205_DELL_M42),
2285         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01fd,
2286                       "unknown Dell", STAC_9205_DELL_M42),
2287         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01fe,
2288                       "Dell Precision", STAC_9205_DELL_M43),
2289         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ff,
2290                       "Dell Precision M4300", STAC_9205_DELL_M43),
2291         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0204,
2292                       "unknown Dell", STAC_9205_DELL_M42),
2293         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0206,
2294                       "Dell Precision", STAC_9205_DELL_M43),
2295         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x021b,
2296                       "Dell Precision", STAC_9205_DELL_M43),
2297         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x021c,
2298                       "Dell Precision", STAC_9205_DELL_M43),
2299         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x021f,
2300                       "Dell Inspiron", STAC_9205_DELL_M44),
2301         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0228,
2302                       "Dell Vostro 1500", STAC_9205_DELL_M42),
2303         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0229,
2304                       "Dell Vostro 1700", STAC_9205_DELL_M42),
2305         /* Gateway */
2306         SND_PCI_QUIRK(0x107b, 0x0560, "Gateway T6834c", STAC_9205_EAPD),
2307         SND_PCI_QUIRK(0x107b, 0x0565, "Gateway T1616", STAC_9205_EAPD),
2308         {} /* terminator */
2309 };
2310
2311 static void stac92xx_set_config_regs(struct hda_codec *codec,
2312                                      const unsigned int *pincfgs)
2313 {
2314         int i;
2315         struct sigmatel_spec *spec = codec->spec;
2316
2317         if (!pincfgs)
2318                 return;
2319
2320         for (i = 0; i < spec->num_pins; i++)
2321                 if (spec->pin_nids[i] && pincfgs[i])
2322                         snd_hda_codec_set_pincfg(codec, spec->pin_nids[i],
2323                                                  pincfgs[i]);
2324 }
2325
2326 /*
2327  * Analog playback callbacks
2328  */
2329 static int stac92xx_playback_pcm_open(struct hda_pcm_stream *hinfo,
2330                                       struct hda_codec *codec,
2331                                       struct snd_pcm_substream *substream)
2332 {
2333         struct sigmatel_spec *spec = codec->spec;
2334         if (spec->stream_delay)
2335                 msleep(spec->stream_delay);
2336         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
2337                                              hinfo);
2338 }
2339
2340 static int stac92xx_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2341                                          struct hda_codec *codec,
2342                                          unsigned int stream_tag,
2343                                          unsigned int format,
2344                                          struct snd_pcm_substream *substream)
2345 {
2346         struct sigmatel_spec *spec = codec->spec;
2347         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag, format, substream);
2348 }
2349
2350 static int stac92xx_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2351                                         struct hda_codec *codec,
2352                                         struct snd_pcm_substream *substream)
2353 {
2354         struct sigmatel_spec *spec = codec->spec;
2355         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
2356 }
2357
2358 /*
2359  * Digital playback callbacks
2360  */
2361 static int stac92xx_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
2362                                           struct hda_codec *codec,
2363                                           struct snd_pcm_substream *substream)
2364 {
2365         struct sigmatel_spec *spec = codec->spec;
2366         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2367 }
2368
2369 static int stac92xx_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
2370                                            struct hda_codec *codec,
2371                                            struct snd_pcm_substream *substream)
2372 {
2373         struct sigmatel_spec *spec = codec->spec;
2374         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2375 }
2376
2377 static int stac92xx_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2378                                          struct hda_codec *codec,
2379                                          unsigned int stream_tag,
2380                                          unsigned int format,
2381                                          struct snd_pcm_substream *substream)
2382 {
2383         struct sigmatel_spec *spec = codec->spec;
2384         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2385                                              stream_tag, format, substream);
2386 }
2387
2388 static int stac92xx_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2389                                         struct hda_codec *codec,
2390                                         struct snd_pcm_substream *substream)
2391 {
2392         struct sigmatel_spec *spec = codec->spec;
2393         return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
2394 }
2395
2396
2397 /*
2398  * Analog capture callbacks
2399  */
2400 static int stac92xx_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2401                                         struct hda_codec *codec,
2402                                         unsigned int stream_tag,
2403                                         unsigned int format,
2404                                         struct snd_pcm_substream *substream)
2405 {
2406         struct sigmatel_spec *spec = codec->spec;
2407         hda_nid_t nid = spec->adc_nids[substream->number];
2408
2409         if (spec->powerdown_adcs) {
2410                 msleep(40);
2411                 snd_hda_codec_write(codec, nid, 0,
2412                         AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
2413         }
2414         snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2415         return 0;
2416 }
2417
2418 static int stac92xx_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2419                                         struct hda_codec *codec,
2420                                         struct snd_pcm_substream *substream)
2421 {
2422         struct sigmatel_spec *spec = codec->spec;
2423         hda_nid_t nid = spec->adc_nids[substream->number];
2424
2425         snd_hda_codec_cleanup_stream(codec, nid);
2426         if (spec->powerdown_adcs)
2427                 snd_hda_codec_write(codec, nid, 0,
2428                         AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
2429         return 0;
2430 }
2431
2432 static const struct hda_pcm_stream stac92xx_pcm_digital_playback = {
2433         .substreams = 1,
2434         .channels_min = 2,
2435         .channels_max = 2,
2436         /* NID is set in stac92xx_build_pcms */
2437         .ops = {
2438                 .open = stac92xx_dig_playback_pcm_open,
2439                 .close = stac92xx_dig_playback_pcm_close,
2440                 .prepare = stac92xx_dig_playback_pcm_prepare,
2441                 .cleanup = stac92xx_dig_playback_pcm_cleanup
2442         },
2443 };
2444
2445 static const struct hda_pcm_stream stac92xx_pcm_digital_capture = {
2446         .substreams = 1,
2447         .channels_min = 2,
2448         .channels_max = 2,
2449         /* NID is set in stac92xx_build_pcms */
2450 };
2451
2452 static const struct hda_pcm_stream stac92xx_pcm_analog_playback = {
2453         .substreams = 1,
2454         .channels_min = 2,
2455         .channels_max = 8,
2456         .nid = 0x02, /* NID to query formats and rates */
2457         .ops = {
2458                 .open = stac92xx_playback_pcm_open,
2459                 .prepare = stac92xx_playback_pcm_prepare,
2460                 .cleanup = stac92xx_playback_pcm_cleanup
2461         },
2462 };
2463
2464 static const struct hda_pcm_stream stac92xx_pcm_analog_alt_playback = {
2465         .substreams = 1,
2466         .channels_min = 2,
2467         .channels_max = 2,
2468         .nid = 0x06, /* NID to query formats and rates */
2469         .ops = {
2470                 .open = stac92xx_playback_pcm_open,
2471                 .prepare = stac92xx_playback_pcm_prepare,
2472                 .cleanup = stac92xx_playback_pcm_cleanup
2473         },
2474 };
2475
2476 static const struct hda_pcm_stream stac92xx_pcm_analog_capture = {
2477         .channels_min = 2,
2478         .channels_max = 2,
2479         /* NID + .substreams is set in stac92xx_build_pcms */
2480         .ops = {
2481                 .prepare = stac92xx_capture_pcm_prepare,
2482                 .cleanup = stac92xx_capture_pcm_cleanup
2483         },
2484 };
2485
2486 static int stac92xx_build_pcms(struct hda_codec *codec)
2487 {
2488         struct sigmatel_spec *spec = codec->spec;
2489         struct hda_pcm *info = spec->pcm_rec;
2490
2491         codec->num_pcms = 1;
2492         codec->pcm_info = info;
2493
2494         info->name = "STAC92xx Analog";
2495         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_playback;
2496         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
2497                 spec->multiout.dac_nids[0];
2498         info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_analog_capture;
2499         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
2500         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_adcs;
2501
2502         if (spec->alt_switch) {
2503                 codec->num_pcms++;
2504                 info++;
2505                 info->name = "STAC92xx Analog Alt";
2506                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_alt_playback;
2507         }
2508
2509         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
2510                 codec->num_pcms++;
2511                 info++;
2512                 info->name = "STAC92xx Digital";
2513                 info->pcm_type = spec->autocfg.dig_out_type[0];
2514                 if (spec->multiout.dig_out_nid) {
2515                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_digital_playback;
2516                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
2517                 }
2518                 if (spec->dig_in_nid) {
2519                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_digital_capture;
2520                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
2521                 }
2522         }
2523
2524         return 0;
2525 }
2526
2527 static unsigned int stac92xx_get_default_vref(struct hda_codec *codec,
2528                                         hda_nid_t nid)
2529 {
2530         unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2531         pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2532         if (pincap & AC_PINCAP_VREF_100)
2533                 return AC_PINCTL_VREF_100;
2534         if (pincap & AC_PINCAP_VREF_80)
2535                 return AC_PINCTL_VREF_80;
2536         if (pincap & AC_PINCAP_VREF_50)
2537                 return AC_PINCTL_VREF_50;
2538         if (pincap & AC_PINCAP_VREF_GRD)
2539                 return AC_PINCTL_VREF_GRD;
2540         return 0;
2541 }
2542
2543 static void stac92xx_auto_set_pinctl(struct hda_codec *codec, hda_nid_t nid, int pin_type)
2544
2545 {
2546         snd_hda_codec_write_cache(codec, nid, 0,
2547                                   AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
2548 }
2549
2550 #define stac92xx_hp_switch_info         snd_ctl_boolean_mono_info
2551
2552 static int stac92xx_hp_switch_get(struct snd_kcontrol *kcontrol,
2553                         struct snd_ctl_elem_value *ucontrol)
2554 {
2555         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2556         struct sigmatel_spec *spec = codec->spec;
2557
2558         ucontrol->value.integer.value[0] = !!spec->hp_switch;
2559         return 0;
2560 }
2561
2562 static void stac_issue_unsol_event(struct hda_codec *codec, hda_nid_t nid);
2563
2564 static int stac92xx_hp_switch_put(struct snd_kcontrol *kcontrol,
2565                         struct snd_ctl_elem_value *ucontrol)
2566 {
2567         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2568         struct sigmatel_spec *spec = codec->spec;
2569         int nid = kcontrol->private_value;
2570  
2571         spec->hp_switch = ucontrol->value.integer.value[0] ? nid : 0;
2572
2573         /* check to be sure that the ports are up to date with
2574          * switch changes
2575          */
2576         stac_issue_unsol_event(codec, nid);
2577
2578         return 1;
2579 }
2580
2581 static int stac92xx_dc_bias_info(struct snd_kcontrol *kcontrol,
2582                                 struct snd_ctl_elem_info *uinfo)
2583 {
2584         int i;
2585         static const char * const texts[] = {
2586                 "Mic In", "Line In", "Line Out"
2587         };
2588
2589         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2590         struct sigmatel_spec *spec = codec->spec;
2591         hda_nid_t nid = kcontrol->private_value;
2592
2593         if (nid == spec->mic_switch || nid == spec->line_switch)
2594                 i = 3;
2595         else
2596                 i = 2;
2597
2598         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2599         uinfo->value.enumerated.items = i;
2600         uinfo->count = 1;
2601         if (uinfo->value.enumerated.item >= i)
2602                 uinfo->value.enumerated.item = i-1;
2603         strcpy(uinfo->value.enumerated.name,
2604                 texts[uinfo->value.enumerated.item]);
2605
2606         return 0;
2607 }
2608
2609 static int stac92xx_dc_bias_get(struct snd_kcontrol *kcontrol,
2610                                 struct snd_ctl_elem_value *ucontrol)
2611 {
2612         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2613         hda_nid_t nid = kcontrol->private_value;
2614         unsigned int vref = stac92xx_vref_get(codec, nid);
2615
2616         if (vref == stac92xx_get_default_vref(codec, nid))
2617                 ucontrol->value.enumerated.item[0] = 0;
2618         else if (vref == AC_PINCTL_VREF_GRD)
2619                 ucontrol->value.enumerated.item[0] = 1;
2620         else if (vref == AC_PINCTL_VREF_HIZ)
2621                 ucontrol->value.enumerated.item[0] = 2;
2622
2623         return 0;
2624 }
2625
2626 static int stac92xx_dc_bias_put(struct snd_kcontrol *kcontrol,
2627                                 struct snd_ctl_elem_value *ucontrol)
2628 {
2629         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2630         unsigned int new_vref = 0;
2631         int error;
2632         hda_nid_t nid = kcontrol->private_value;
2633
2634         if (ucontrol->value.enumerated.item[0] == 0)
2635                 new_vref = stac92xx_get_default_vref(codec, nid);
2636         else if (ucontrol->value.enumerated.item[0] == 1)
2637                 new_vref = AC_PINCTL_VREF_GRD;
2638         else if (ucontrol->value.enumerated.item[0] == 2)
2639                 new_vref = AC_PINCTL_VREF_HIZ;
2640         else
2641                 return 0;
2642
2643         if (new_vref != stac92xx_vref_get(codec, nid)) {
2644                 error = stac92xx_vref_set(codec, nid, new_vref);
2645                 return error;
2646         }
2647
2648         return 0;
2649 }
2650
2651 static int stac92xx_io_switch_info(struct snd_kcontrol *kcontrol,
2652                                 struct snd_ctl_elem_info *uinfo)
2653 {
2654         char *texts[2];
2655         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2656         struct sigmatel_spec *spec = codec->spec;
2657
2658         if (kcontrol->private_value == spec->line_switch)
2659                 texts[0] = "Line In";
2660         else
2661                 texts[0] = "Mic In";
2662         texts[1] = "Line Out";
2663         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2664         uinfo->value.enumerated.items = 2;
2665         uinfo->count = 1;
2666
2667         if (uinfo->value.enumerated.item >= 2)
2668                 uinfo->value.enumerated.item = 1;
2669         strcpy(uinfo->value.enumerated.name,
2670                 texts[uinfo->value.enumerated.item]);
2671
2672         return 0;
2673 }
2674
2675 static int stac92xx_io_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2676 {
2677         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2678         struct sigmatel_spec *spec = codec->spec;
2679         hda_nid_t nid = kcontrol->private_value;
2680         int io_idx = (nid == spec->mic_switch) ? 1 : 0;
2681
2682         ucontrol->value.enumerated.item[0] = spec->io_switch[io_idx];
2683         return 0;
2684 }
2685
2686 static int stac92xx_io_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
2687 {
2688         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2689         struct sigmatel_spec *spec = codec->spec;
2690         hda_nid_t nid = kcontrol->private_value;
2691         int io_idx = (nid == spec->mic_switch) ? 1 : 0;
2692         unsigned short val = !!ucontrol->value.enumerated.item[0];
2693
2694         spec->io_switch[io_idx] = val;
2695
2696         if (val)
2697                 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
2698         else {
2699                 unsigned int pinctl = AC_PINCTL_IN_EN;
2700                 if (io_idx) /* set VREF for mic */
2701                         pinctl |= stac92xx_get_default_vref(codec, nid);
2702                 stac92xx_auto_set_pinctl(codec, nid, pinctl);
2703         }
2704
2705         /* check the auto-mute again: we need to mute/unmute the speaker
2706          * appropriately according to the pin direction
2707          */
2708         if (spec->hp_detect)
2709                 stac_issue_unsol_event(codec, nid);
2710
2711         return 1;
2712 }
2713
2714 #define stac92xx_clfe_switch_info snd_ctl_boolean_mono_info
2715
2716 static int stac92xx_clfe_switch_get(struct snd_kcontrol *kcontrol,
2717                 struct snd_ctl_elem_value *ucontrol)
2718 {
2719         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2720         struct sigmatel_spec *spec = codec->spec;
2721
2722         ucontrol->value.integer.value[0] = spec->clfe_swap;
2723         return 0;
2724 }
2725
2726 static int stac92xx_clfe_switch_put(struct snd_kcontrol *kcontrol,
2727                 struct snd_ctl_elem_value *ucontrol)
2728 {
2729         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2730         struct sigmatel_spec *spec = codec->spec;
2731         hda_nid_t nid = kcontrol->private_value & 0xff;
2732         unsigned int val = !!ucontrol->value.integer.value[0];
2733
2734         if (spec->clfe_swap == val)
2735                 return 0;
2736
2737         spec->clfe_swap = val;
2738
2739         snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_EAPD_BTLENABLE,
2740                 spec->clfe_swap ? 0x4 : 0x0);
2741
2742         return 1;
2743 }
2744
2745 #define STAC_CODEC_HP_SWITCH(xname) \
2746         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2747           .name = xname, \
2748           .index = 0, \
2749           .info = stac92xx_hp_switch_info, \
2750           .get = stac92xx_hp_switch_get, \
2751           .put = stac92xx_hp_switch_put, \
2752         }
2753
2754 #define STAC_CODEC_IO_SWITCH(xname, xpval) \
2755         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2756           .name = xname, \
2757           .index = 0, \
2758           .info = stac92xx_io_switch_info, \
2759           .get = stac92xx_io_switch_get, \
2760           .put = stac92xx_io_switch_put, \
2761           .private_value = xpval, \
2762         }
2763
2764 #define STAC_CODEC_CLFE_SWITCH(xname, xpval) \
2765         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2766           .name = xname, \
2767           .index = 0, \
2768           .info = stac92xx_clfe_switch_info, \
2769           .get = stac92xx_clfe_switch_get, \
2770           .put = stac92xx_clfe_switch_put, \
2771           .private_value = xpval, \
2772         }
2773
2774 enum {
2775         STAC_CTL_WIDGET_VOL,
2776         STAC_CTL_WIDGET_MUTE,
2777         STAC_CTL_WIDGET_MUTE_BEEP,
2778         STAC_CTL_WIDGET_MONO_MUX,
2779         STAC_CTL_WIDGET_HP_SWITCH,
2780         STAC_CTL_WIDGET_IO_SWITCH,
2781         STAC_CTL_WIDGET_CLFE_SWITCH,
2782         STAC_CTL_WIDGET_DC_BIAS
2783 };
2784
2785 static const struct snd_kcontrol_new stac92xx_control_templates[] = {
2786         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
2787         HDA_CODEC_MUTE(NULL, 0, 0, 0),
2788         HDA_CODEC_MUTE_BEEP(NULL, 0, 0, 0),
2789         STAC_MONO_MUX,
2790         STAC_CODEC_HP_SWITCH(NULL),
2791         STAC_CODEC_IO_SWITCH(NULL, 0),
2792         STAC_CODEC_CLFE_SWITCH(NULL, 0),
2793         DC_BIAS(NULL, 0, 0),
2794 };
2795
2796 /* add dynamic controls */
2797 static struct snd_kcontrol_new *
2798 stac_control_new(struct sigmatel_spec *spec,
2799                  const struct snd_kcontrol_new *ktemp,
2800                  const char *name,
2801                  unsigned int subdev)
2802 {
2803         struct snd_kcontrol_new *knew;
2804
2805         snd_array_init(&spec->kctls, sizeof(*knew), 32);
2806         knew = snd_array_new(&spec->kctls);
2807         if (!knew)
2808                 return NULL;
2809         *knew = *ktemp;
2810         knew->name = kstrdup(name, GFP_KERNEL);
2811         if (!knew->name) {
2812                 /* roolback */
2813                 memset(knew, 0, sizeof(*knew));
2814                 spec->kctls.alloced--;
2815                 return NULL;
2816         }
2817         knew->subdevice = subdev;
2818         return knew;
2819 }
2820
2821 static int stac92xx_add_control_temp(struct sigmatel_spec *spec,
2822                                      const struct snd_kcontrol_new *ktemp,
2823                                      int idx, const char *name,
2824                                      unsigned long val)
2825 {
2826         struct snd_kcontrol_new *knew = stac_control_new(spec, ktemp, name,
2827                                                          HDA_SUBDEV_AMP_FLAG);
2828         if (!knew)
2829                 return -ENOMEM;
2830         knew->index = idx;
2831         knew->private_value = val;
2832         return 0;
2833 }
2834
2835 static inline int stac92xx_add_control_idx(struct sigmatel_spec *spec,
2836                                            int type, int idx, const char *name,
2837                                            unsigned long val)
2838 {
2839         return stac92xx_add_control_temp(spec,
2840                                          &stac92xx_control_templates[type],
2841                                          idx, name, val);
2842 }
2843
2844
2845 /* add dynamic controls */
2846 static inline int stac92xx_add_control(struct sigmatel_spec *spec, int type,
2847                                        const char *name, unsigned long val)
2848 {
2849         return stac92xx_add_control_idx(spec, type, 0, name, val);
2850 }
2851
2852 static const struct snd_kcontrol_new stac_input_src_temp = {
2853         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2854         .name = "Input Source",
2855         .info = stac92xx_mux_enum_info,
2856         .get = stac92xx_mux_enum_get,
2857         .put = stac92xx_mux_enum_put,
2858 };
2859
2860 static inline int stac92xx_add_jack_mode_control(struct hda_codec *codec,
2861                                                 hda_nid_t nid, int idx)
2862 {
2863         int def_conf = snd_hda_codec_get_pincfg(codec, nid);
2864         int control = 0;
2865         struct sigmatel_spec *spec = codec->spec;
2866         char name[22];
2867
2868         if (snd_hda_get_input_pin_attr(def_conf) != INPUT_PIN_ATTR_INT) {
2869                 if (stac92xx_get_default_vref(codec, nid) == AC_PINCTL_VREF_GRD
2870                         && nid == spec->line_switch)
2871                         control = STAC_CTL_WIDGET_IO_SWITCH;
2872                 else if (snd_hda_query_pin_caps(codec, nid)
2873                         & (AC_PINCAP_VREF_GRD << AC_PINCAP_VREF_SHIFT))
2874                         control = STAC_CTL_WIDGET_DC_BIAS;
2875                 else if (nid == spec->mic_switch)
2876                         control = STAC_CTL_WIDGET_IO_SWITCH;
2877         }
2878
2879         if (control) {
2880                 strcpy(name, hda_get_input_pin_label(codec, nid, 1));
2881                 return stac92xx_add_control(codec->spec, control,
2882                                         strcat(name, " Jack Mode"), nid);
2883         }
2884
2885         return 0;
2886 }
2887
2888 static int stac92xx_add_input_source(struct sigmatel_spec *spec)
2889 {
2890         struct snd_kcontrol_new *knew;
2891         struct hda_input_mux *imux = &spec->private_imux;
2892
2893         if (spec->auto_mic)
2894                 return 0; /* no need for input source */
2895         if (!spec->num_adcs || imux->num_items <= 1)
2896                 return 0; /* no need for input source control */
2897         knew = stac_control_new(spec, &stac_input_src_temp,
2898                                 stac_input_src_temp.name, 0);
2899         if (!knew)
2900                 return -ENOMEM;
2901         knew->count = spec->num_adcs;
2902         return 0;
2903 }
2904
2905 /* check whether the line-input can be used as line-out */
2906 static hda_nid_t check_line_out_switch(struct hda_codec *codec)
2907 {
2908         struct sigmatel_spec *spec = codec->spec;
2909         struct auto_pin_cfg *cfg = &spec->autocfg;
2910         hda_nid_t nid;
2911         unsigned int pincap;
2912         int i;
2913
2914         if (cfg->line_out_type != AUTO_PIN_LINE_OUT)
2915                 return 0;
2916         for (i = 0; i < cfg->num_inputs; i++) {
2917                 if (cfg->inputs[i].type == AUTO_PIN_LINE_IN) {
2918                         nid = cfg->inputs[i].pin;
2919                         pincap = snd_hda_query_pin_caps(codec, nid);
2920                         if (pincap & AC_PINCAP_OUT)
2921                                 return nid;
2922                 }
2923         }
2924         return 0;
2925 }
2926
2927 static hda_nid_t get_unassigned_dac(struct hda_codec *codec, hda_nid_t nid);
2928
2929 /* check whether the mic-input can be used as line-out */
2930 static hda_nid_t check_mic_out_switch(struct hda_codec *codec, hda_nid_t *dac)
2931 {
2932         struct sigmatel_spec *spec = codec->spec;
2933         struct auto_pin_cfg *cfg = &spec->autocfg;
2934         unsigned int def_conf, pincap;
2935         int i;
2936
2937         *dac = 0;
2938         if (cfg->line_out_type != AUTO_PIN_LINE_OUT)
2939                 return 0;
2940         for (i = 0; i < cfg->num_inputs; i++) {
2941                 hda_nid_t nid = cfg->inputs[i].pin;
2942                 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2943                         continue;
2944                 def_conf = snd_hda_codec_get_pincfg(codec, nid);
2945                 /* some laptops have an internal analog microphone
2946                  * which can't be used as a output */
2947                 if (snd_hda_get_input_pin_attr(def_conf) != INPUT_PIN_ATTR_INT) {
2948                         pincap = snd_hda_query_pin_caps(codec, nid);
2949                         if (pincap & AC_PINCAP_OUT) {
2950                                 *dac = get_unassigned_dac(codec, nid);
2951                                 if (*dac)
2952                                         return nid;
2953                         }
2954                 }
2955         }
2956         return 0;
2957 }
2958
2959 static int is_in_dac_nids(struct sigmatel_spec *spec, hda_nid_t nid)
2960 {
2961         int i;
2962         
2963         for (i = 0; i < spec->multiout.num_dacs; i++) {
2964                 if (spec->multiout.dac_nids[i] == nid)
2965                         return 1;
2966         }
2967
2968         return 0;
2969 }
2970
2971 static int check_all_dac_nids(struct sigmatel_spec *spec, hda_nid_t nid)
2972 {
2973         int i;
2974         if (is_in_dac_nids(spec, nid))
2975                 return 1;
2976         for (i = 0; i < spec->autocfg.hp_outs; i++)
2977                 if (spec->hp_dacs[i] == nid)
2978                         return 1;
2979         for (i = 0; i < spec->autocfg.speaker_outs; i++)
2980                 if (spec->speaker_dacs[i] == nid)
2981                         return 1;
2982         return 0;
2983 }
2984
2985 static hda_nid_t get_unassigned_dac(struct hda_codec *codec, hda_nid_t nid)
2986 {
2987         struct sigmatel_spec *spec = codec->spec;
2988         struct auto_pin_cfg *cfg = &spec->autocfg;
2989         int j, conn_len;
2990         hda_nid_t conn[HDA_MAX_CONNECTIONS], fallback_dac;
2991         unsigned int wcaps, wtype;
2992
2993         conn_len = snd_hda_get_connections(codec, nid, conn,
2994                                            HDA_MAX_CONNECTIONS);
2995         /* 92HD88: trace back up the link of nids to find the DAC */
2996         while (conn_len == 1 && (get_wcaps_type(get_wcaps(codec, conn[0]))
2997                                         != AC_WID_AUD_OUT)) {
2998                 nid = conn[0];
2999                 conn_len = snd_hda_get_connections(codec, nid, conn,
3000                         HDA_MAX_CONNECTIONS);
3001         }
3002         for (j = 0; j < conn_len; j++) {
3003                 wcaps = get_wcaps(codec, conn[j]);
3004                 wtype = get_wcaps_type(wcaps);
3005                 /* we check only analog outputs */
3006                 if (wtype != AC_WID_AUD_OUT || (wcaps & AC_WCAP_DIGITAL))
3007                         continue;
3008                 /* if this route has a free DAC, assign it */
3009                 if (!check_all_dac_nids(spec, conn[j])) {
3010                         if (conn_len > 1) {
3011                                 /* select this DAC in the pin's input mux */
3012                                 snd_hda_codec_write_cache(codec, nid, 0,
3013                                                   AC_VERB_SET_CONNECT_SEL, j);
3014                         }
3015                         return conn[j];
3016                 }
3017         }
3018
3019         /* if all DACs are already assigned, connect to the primary DAC,
3020            unless we're assigning a secondary headphone */
3021         fallback_dac = spec->multiout.dac_nids[0];
3022         if (spec->multiout.hp_nid) {
3023                 for (j = 0; j < cfg->hp_outs; j++)
3024                         if (cfg->hp_pins[j] == nid) {
3025                                 fallback_dac = spec->multiout.hp_nid;
3026                                 break;
3027                         }
3028         }
3029
3030         if (conn_len > 1) {
3031                 for (j = 0; j < conn_len; j++) {
3032                         if (conn[j] == fallback_dac) {
3033                                 snd_hda_codec_write_cache(codec, nid, 0,
3034                                                   AC_VERB_SET_CONNECT_SEL, j);
3035                                 break;
3036                         }
3037                 }
3038         }
3039         return 0;
3040 }
3041
3042 static int add_spec_dacs(struct sigmatel_spec *spec, hda_nid_t nid);
3043 static int add_spec_extra_dacs(struct sigmatel_spec *spec, hda_nid_t nid);
3044
3045 /*
3046  * Fill in the dac_nids table from the parsed pin configuration
3047  * This function only works when every pin in line_out_pins[]
3048  * contains atleast one DAC in its connection list. Some 92xx
3049  * codecs are not connected directly to a DAC, such as the 9200
3050  * and 9202/925x. For those, dac_nids[] must be hard-coded.
3051  */
3052 static int stac92xx_auto_fill_dac_nids(struct hda_codec *codec)
3053 {
3054         struct sigmatel_spec *spec = codec->spec;
3055         struct auto_pin_cfg *cfg = &spec->autocfg;
3056         int i;
3057         hda_nid_t nid, dac;
3058         
3059         for (i = 0; i < cfg->line_outs; i++) {
3060                 nid = cfg->line_out_pins[i];
3061                 dac = get_unassigned_dac(codec, nid);
3062                 if (!dac) {
3063                         if (spec->multiout.num_dacs > 0) {
3064                                 /* we have already working output pins,
3065                                  * so let's drop the broken ones again
3066                                  */
3067                                 cfg->line_outs = spec->multiout.num_dacs;
3068                                 break;
3069                         }
3070                         /* error out, no available DAC found */
3071                         snd_printk(KERN_ERR
3072                                    "%s: No available DAC for pin 0x%x\n",
3073                                    __func__, nid);
3074                         return -ENODEV;
3075                 }
3076                 add_spec_dacs(spec, dac);
3077         }
3078
3079         for (i = 0; i < cfg->hp_outs; i++) {
3080                 nid = cfg->hp_pins[i];
3081                 dac = get_unassigned_dac(codec, nid);
3082                 if (dac) {
3083                         if (!spec->multiout.hp_nid)
3084                                 spec->multiout.hp_nid = dac;
3085                         else
3086                                 add_spec_extra_dacs(spec, dac);
3087                 }
3088                 spec->hp_dacs[i] = dac;
3089         }
3090
3091         for (i = 0; i < cfg->speaker_outs; i++) {
3092                 nid = cfg->speaker_pins[i];
3093                 dac = get_unassigned_dac(codec, nid);
3094                 if (dac)
3095                         add_spec_extra_dacs(spec, dac);
3096                 spec->speaker_dacs[i] = dac;
3097         }
3098
3099         /* add line-in as output */
3100         nid = check_line_out_switch(codec);
3101         if (nid) {
3102                 dac = get_unassigned_dac(codec, nid);
3103                 if (dac) {
3104                         snd_printdd("STAC: Add line-in 0x%x as output %d\n",
3105                                     nid, cfg->line_outs);
3106                         cfg->line_out_pins[cfg->line_outs] = nid;
3107                         cfg->line_outs++;
3108                         spec->line_switch = nid;
3109                         add_spec_dacs(spec, dac);
3110                 }
3111         }
3112         /* add mic as output */
3113         nid = check_mic_out_switch(codec, &dac);
3114         if (nid && dac) {
3115                 snd_printdd("STAC: Add mic-in 0x%x as output %d\n",
3116                             nid, cfg->line_outs);
3117                 cfg->line_out_pins[cfg->line_outs] = nid;
3118                 cfg->line_outs++;
3119                 spec->mic_switch = nid;
3120                 add_spec_dacs(spec, dac);
3121         }
3122
3123         snd_printd("stac92xx: dac_nids=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3124                    spec->multiout.num_dacs,
3125                    spec->multiout.dac_nids[0],
3126                    spec->multiout.dac_nids[1],
3127                    spec->multiout.dac_nids[2],
3128                    spec->multiout.dac_nids[3],
3129                    spec->multiout.dac_nids[4]);
3130
3131         return 0;
3132 }
3133
3134 /* create volume control/switch for the given prefx type */
3135 static int create_controls_idx(struct hda_codec *codec, const char *pfx,
3136                                int idx, hda_nid_t nid, int chs)
3137 {
3138         struct sigmatel_spec *spec = codec->spec;
3139         char name[32];
3140         int err;
3141
3142         if (!spec->check_volume_offset) {
3143                 unsigned int caps, step, nums, db_scale;
3144                 caps = query_amp_caps(codec, nid, HDA_OUTPUT);
3145                 step = (caps & AC_AMPCAP_STEP_SIZE) >>
3146                         AC_AMPCAP_STEP_SIZE_SHIFT;
3147                 step = (step + 1) * 25; /* in .01dB unit */
3148                 nums = (caps & AC_AMPCAP_NUM_STEPS) >>
3149                         AC_AMPCAP_NUM_STEPS_SHIFT;
3150                 db_scale = nums * step;
3151                 /* if dB scale is over -64dB, and finer enough,
3152                  * let's reduce it to half
3153                  */
3154                 if (db_scale > 6400 && nums >= 0x1f)
3155                         spec->volume_offset = nums / 2;
3156                 spec->check_volume_offset = 1;
3157         }
3158
3159         sprintf(name, "%s Playback Volume", pfx);
3160         err = stac92xx_add_control_idx(spec, STAC_CTL_WIDGET_VOL, idx, name,
3161                 HDA_COMPOSE_AMP_VAL_OFS(nid, chs, 0, HDA_OUTPUT,
3162                                         spec->volume_offset));
3163         if (err < 0)
3164                 return err;
3165         sprintf(name, "%s Playback Switch", pfx);
3166         err = stac92xx_add_control_idx(spec, STAC_CTL_WIDGET_MUTE, idx, name,
3167                                    HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
3168         if (err < 0)
3169                 return err;
3170         return 0;
3171 }
3172
3173 #define create_controls(codec, pfx, nid, chs) \
3174         create_controls_idx(codec, pfx, 0, nid, chs)
3175
3176 static int add_spec_dacs(struct sigmatel_spec *spec, hda_nid_t nid)
3177 {
3178         if (spec->multiout.num_dacs > 4) {
3179                 printk(KERN_WARNING "stac92xx: No space for DAC 0x%x\n", nid);
3180                 return 1;
3181         } else {
3182                 snd_BUG_ON(spec->multiout.dac_nids != spec->dac_nids);
3183                 spec->dac_nids[spec->multiout.num_dacs] = nid;
3184                 spec->multiout.num_dacs++;
3185         }
3186         return 0;
3187 }
3188
3189 static int add_spec_extra_dacs(struct sigmatel_spec *spec, hda_nid_t nid)
3190 {
3191         int i;
3192         for (i = 0; i < ARRAY_SIZE(spec->multiout.extra_out_nid); i++) {
3193                 if (!spec->multiout.extra_out_nid[i]) {
3194                         spec->multiout.extra_out_nid[i] = nid;
3195                         return 0;
3196                 }
3197         }
3198         printk(KERN_WARNING "stac92xx: No space for extra DAC 0x%x\n", nid);
3199         return 1;
3200 }
3201
3202 /* Create output controls
3203  * The mixer elements are named depending on the given type (AUTO_PIN_XXX_OUT)
3204  */
3205 static int create_multi_out_ctls(struct hda_codec *codec, int num_outs,
3206                                  const hda_nid_t *pins,
3207                                  const hda_nid_t *dac_nids,
3208                                  int type)
3209 {
3210         struct sigmatel_spec *spec = codec->spec;
3211         static const char * const chname[4] = {
3212                 "Front", "Surround", NULL /*CLFE*/, "Side"
3213         };
3214         hda_nid_t nid;
3215         int i, err;
3216         unsigned int wid_caps;
3217
3218         for (i = 0; i < num_outs && i < ARRAY_SIZE(chname); i++) {
3219                 if (type == AUTO_PIN_HP_OUT && !spec->hp_detect) {
3220                         if (is_jack_detectable(codec, pins[i]))
3221                                 spec->hp_detect = 1;
3222                 }
3223                 nid = dac_nids[i];
3224                 if (!nid)
3225                         continue;
3226                 if (type != AUTO_PIN_HP_OUT && i == 2) {
3227                         /* Center/LFE */
3228                         err = create_controls(codec, "Center", nid, 1);
3229                         if (err < 0)
3230                                 return err;
3231                         err = create_controls(codec, "LFE", nid, 2);
3232                         if (err < 0)
3233                                 return err;
3234
3235                         wid_caps = get_wcaps(codec, nid);
3236
3237                         if (wid_caps & AC_WCAP_LR_SWAP) {
3238                                 err = stac92xx_add_control(spec,
3239                                         STAC_CTL_WIDGET_CLFE_SWITCH,
3240                                         "Swap Center/LFE Playback Switch", nid);
3241
3242                                 if (err < 0)
3243                                         return err;
3244                         }
3245
3246                 } else {
3247                         const char *name;
3248                         int idx;
3249                         switch (type) {
3250                         case AUTO_PIN_HP_OUT:
3251                                 name = "Headphone";
3252                                 idx = i;
3253                                 break;
3254                         case AUTO_PIN_SPEAKER_OUT:
3255                                 name = "Speaker";
3256                                 idx = i;
3257                                 break;
3258                         default:
3259                                 name = chname[i];
3260                                 idx = 0;
3261                                 break;
3262                         }
3263                         err = create_controls_idx(codec, name, idx, nid, 3);
3264                         if (err < 0)
3265                                 return err;
3266                 }
3267         }
3268         return 0;
3269 }
3270
3271 static int stac92xx_add_capvol_ctls(struct hda_codec *codec, unsigned long vol,
3272                                     unsigned long sw, int idx)
3273 {
3274         int err;
3275         err = stac92xx_add_control_idx(codec->spec, STAC_CTL_WIDGET_VOL, idx,
3276                                        "Capture Volume", vol);
3277         if (err < 0)
3278                 return err;
3279         err = stac92xx_add_control_idx(codec->spec, STAC_CTL_WIDGET_MUTE, idx,
3280                                        "Capture Switch", sw);
3281         if (err < 0)
3282                 return err;
3283         return 0;
3284 }
3285
3286 /* add playback controls from the parsed DAC table */
3287 static int stac92xx_auto_create_multi_out_ctls(struct hda_codec *codec,
3288                                                const struct auto_pin_cfg *cfg)
3289 {
3290         struct sigmatel_spec *spec = codec->spec;
3291         hda_nid_t nid;
3292         int err;
3293         int idx;
3294
3295         err = create_multi_out_ctls(codec, cfg->line_outs, cfg->line_out_pins,
3296                                     spec->multiout.dac_nids,
3297                                     cfg->line_out_type);
3298         if (err < 0)
3299                 return err;
3300
3301         if (cfg->hp_outs > 1 && cfg->line_out_type == AUTO_PIN_LINE_OUT) {
3302                 err = stac92xx_add_control(spec,
3303                         STAC_CTL_WIDGET_HP_SWITCH,
3304                         "Headphone as Line Out Switch",
3305                         cfg->hp_pins[cfg->hp_outs - 1]);
3306                 if (err < 0)
3307                         return err;
3308         }
3309
3310         for (idx = 0; idx < cfg->num_inputs; idx++) {
3311                 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3312                         break;
3313                 nid = cfg->inputs[idx].pin;
3314                 err = stac92xx_add_jack_mode_control(codec, nid, idx);
3315                 if (err < 0)
3316                         return err;
3317         }
3318
3319         return 0;
3320 }
3321
3322 /* add playback controls for Speaker and HP outputs */
3323 static int stac92xx_auto_create_hp_ctls(struct hda_codec *codec,
3324                                         struct auto_pin_cfg *cfg)
3325 {
3326         struct sigmatel_spec *spec = codec->spec;
3327         int err;
3328
3329         err = create_multi_out_ctls(codec, cfg->hp_outs, cfg->hp_pins,
3330                                     spec->hp_dacs, AUTO_PIN_HP_OUT);
3331         if (err < 0)
3332                 return err;
3333
3334         err = create_multi_out_ctls(codec, cfg->speaker_outs, cfg->speaker_pins,
3335                                     spec->speaker_dacs, AUTO_PIN_SPEAKER_OUT);
3336         if (err < 0)
3337                 return err;
3338
3339         return 0;
3340 }
3341
3342 /* labels for mono mux outputs */
3343 static const char * const stac92xx_mono_labels[4] = {
3344         "DAC0", "DAC1", "Mixer", "DAC2"
3345 };
3346
3347 /* create mono mux for mono out on capable codecs */
3348 static int stac92xx_auto_create_mono_output_ctls(struct hda_codec *codec)
3349 {
3350         struct sigmatel_spec *spec = codec->spec;
3351         struct hda_input_mux *mono_mux = &spec->private_mono_mux;
3352         int i, num_cons;
3353         hda_nid_t con_lst[ARRAY_SIZE(stac92xx_mono_labels)];
3354
3355         num_cons = snd_hda_get_connections(codec,
3356                                 spec->mono_nid,
3357                                 con_lst,
3358                                 HDA_MAX_NUM_INPUTS);
3359         if (num_cons <= 0 || num_cons > ARRAY_SIZE(stac92xx_mono_labels))
3360                 return -EINVAL;
3361
3362         for (i = 0; i < num_cons; i++)
3363                 snd_hda_add_imux_item(mono_mux, stac92xx_mono_labels[i], i,
3364                                       NULL);
3365
3366         return stac92xx_add_control(spec, STAC_CTL_WIDGET_MONO_MUX,
3367                                 "Mono Mux", spec->mono_nid);
3368 }
3369
3370 /* create PC beep volume controls */
3371 static int stac92xx_auto_create_beep_ctls(struct hda_codec *codec,
3372                                                 hda_nid_t nid)
3373 {
3374         struct sigmatel_spec *spec = codec->spec;
3375         u32 caps = query_amp_caps(codec, nid, HDA_OUTPUT);
3376         int err, type = STAC_CTL_WIDGET_MUTE_BEEP;
3377
3378         if (spec->anabeep_nid == nid)
3379                 type = STAC_CTL_WIDGET_MUTE;
3380
3381         /* check for mute support for the the amp */
3382         if ((caps & AC_AMPCAP_MUTE) >> AC_AMPCAP_MUTE_SHIFT) {
3383                 err = stac92xx_add_control(spec, type,
3384                         "Beep Playback Switch",
3385                         HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT));
3386                         if (err < 0)
3387                                 return err;
3388         }
3389
3390         /* check to see if there is volume support for the amp */
3391         if ((caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT) {
3392                 err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL,
3393                         "Beep Playback Volume",
3394                         HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT));
3395                         if (err < 0)
3396                                 return err;
3397         }
3398         return 0;
3399 }
3400
3401 #ifdef CONFIG_SND_HDA_INPUT_BEEP
3402 #define stac92xx_dig_beep_switch_info snd_ctl_boolean_mono_info
3403
3404 static int stac92xx_dig_beep_switch_get(struct snd_kcontrol *kcontrol,
3405                                         struct snd_ctl_elem_value *ucontrol)
3406 {
3407         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3408         ucontrol->value.integer.value[0] = codec->beep->enabled;
3409         return 0;
3410 }
3411
3412 static int stac92xx_dig_beep_switch_put(struct snd_kcontrol *kcontrol,
3413                                         struct snd_ctl_elem_value *ucontrol)
3414 {
3415         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3416         return snd_hda_enable_beep_device(codec, ucontrol->value.integer.value[0]);
3417 }
3418
3419 static const struct snd_kcontrol_new stac92xx_dig_beep_ctrl = {
3420         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3421         .info = stac92xx_dig_beep_switch_info,
3422         .get = stac92xx_dig_beep_switch_get,
3423         .put = stac92xx_dig_beep_switch_put,
3424 };
3425
3426 static int stac92xx_beep_switch_ctl(struct hda_codec *codec)
3427 {
3428         return stac92xx_add_control_temp(codec->spec, &stac92xx_dig_beep_ctrl,
3429                                          0, "Beep Playback Switch", 0);
3430 }
3431 #endif
3432
3433 static int stac92xx_auto_create_mux_input_ctls(struct hda_codec *codec)
3434 {
3435         struct sigmatel_spec *spec = codec->spec;
3436         int i, j, err = 0;
3437
3438         for (i = 0; i < spec->num_muxes; i++) {
3439                 hda_nid_t nid;
3440                 unsigned int wcaps;
3441                 unsigned long val;
3442
3443                 nid = spec->mux_nids[i];
3444                 wcaps = get_wcaps(codec, nid);
3445                 if (!(wcaps & AC_WCAP_OUT_AMP))
3446                         continue;
3447
3448                 /* check whether already the same control was created as
3449                  * normal Capture Volume.
3450                  */
3451                 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3452                 for (j = 0; j < spec->num_caps; j++) {
3453                         if (spec->capvols[j] == val)
3454                                 break;
3455                 }
3456                 if (j < spec->num_caps)
3457                         continue;
3458
3459                 err = stac92xx_add_control_idx(spec, STAC_CTL_WIDGET_VOL, i,
3460                                                "Mux Capture Volume", val);
3461                 if (err < 0)
3462                         return err;
3463         }
3464         return 0;
3465 };
3466
3467 static const char * const stac92xx_spdif_labels[3] = {
3468         "Digital Playback", "Analog Mux 1", "Analog Mux 2",
3469 };
3470
3471 static int stac92xx_auto_create_spdif_mux_ctls(struct hda_codec *codec)
3472 {
3473         struct sigmatel_spec *spec = codec->spec;
3474         struct hda_input_mux *spdif_mux = &spec->private_smux;
3475         const char * const *labels = spec->spdif_labels;
3476         int i, num_cons;
3477         hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
3478
3479         num_cons = snd_hda_get_connections(codec,
3480                                 spec->smux_nids[0],
3481                                 con_lst,
3482                                 HDA_MAX_NUM_INPUTS);
3483         if (num_cons <= 0)
3484                 return -EINVAL;
3485
3486         if (!labels)
3487                 labels = stac92xx_spdif_labels;
3488
3489         for (i = 0; i < num_cons; i++)
3490                 snd_hda_add_imux_item(spdif_mux, labels[i], i, NULL);
3491
3492         return 0;
3493 }
3494
3495 /* labels for dmic mux inputs */
3496 static const char * const stac92xx_dmic_labels[5] = {
3497         "Analog Inputs", "Digital Mic 1", "Digital Mic 2",
3498         "Digital Mic 3", "Digital Mic 4"
3499 };
3500
3501 static hda_nid_t get_connected_node(struct hda_codec *codec, hda_nid_t mux,
3502                                     int idx)
3503 {
3504         hda_nid_t conn[HDA_MAX_NUM_INPUTS];
3505         int nums;
3506         nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
3507         if (idx >= 0 && idx < nums)
3508                 return conn[idx];
3509         return 0;
3510 }
3511
3512 /* look for NID recursively */
3513 #define get_connection_index(codec, mux, nid) \
3514         snd_hda_get_conn_index(codec, mux, nid, 1)
3515
3516 /* create a volume assigned to the given pin (only if supported) */
3517 /* return 1 if the volume control is created */
3518 static int create_elem_capture_vol(struct hda_codec *codec, hda_nid_t nid,
3519                                    const char *label, int idx, int direction)
3520 {
3521         unsigned int caps, nums;
3522         char name[32];
3523         int err;
3524
3525         if (direction == HDA_OUTPUT)
3526                 caps = AC_WCAP_OUT_AMP;
3527         else
3528                 caps = AC_WCAP_IN_AMP;
3529         if (!(get_wcaps(codec, nid) & caps))
3530                 return 0;
3531         caps = query_amp_caps(codec, nid, direction);
3532         nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
3533         if (!nums)
3534                 return 0;
3535         snprintf(name, sizeof(name), "%s Capture Volume", label);
3536         err = stac92xx_add_control_idx(codec->spec, STAC_CTL_WIDGET_VOL, idx, name,
3537                                        HDA_COMPOSE_AMP_VAL(nid, 3, 0, direction));
3538         if (err < 0)
3539                 return err;
3540         return 1;
3541 }
3542
3543 /* create playback/capture controls for input pins on dmic capable codecs */
3544 static int stac92xx_auto_create_dmic_input_ctls(struct hda_codec *codec,
3545                                                 const struct auto_pin_cfg *cfg)
3546 {
3547         struct sigmatel_spec *spec = codec->spec;
3548         struct hda_input_mux *imux = &spec->private_imux;
3549         struct hda_input_mux *dimux = &spec->private_dimux;
3550         int err, i;
3551         unsigned int def_conf;
3552
3553         snd_hda_add_imux_item(dimux, stac92xx_dmic_labels[0], 0, NULL);
3554
3555         for (i = 0; i < spec->num_dmics; i++) {
3556                 hda_nid_t nid;
3557                 int index, type_idx;
3558                 const char *label;
3559
3560                 nid = spec->dmic_nids[i];
3561                 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
3562                         continue;
3563                 def_conf = snd_hda_codec_get_pincfg(codec, nid);
3564                 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3565                         continue;
3566
3567                 index = get_connection_index(codec, spec->dmux_nids[0], nid);
3568                 if (index < 0)
3569                         continue;
3570
3571                 label = hda_get_input_pin_label(codec, nid, 1);
3572                 snd_hda_add_imux_item(dimux, label, index, &type_idx);
3573                 if (snd_hda_get_bool_hint(codec, "separate_dmux") != 1)
3574                         snd_hda_add_imux_item(imux, label, index, &type_idx);
3575
3576                 err = create_elem_capture_vol(codec, nid, label, type_idx,
3577                                               HDA_INPUT);
3578                 if (err < 0)
3579                         return err;
3580                 if (!err) {
3581                         err = create_elem_capture_vol(codec, nid, label,
3582                                                       type_idx, HDA_OUTPUT);
3583                         if (err < 0)
3584                                 return err;
3585                         if (!err) {
3586                                 nid = get_connected_node(codec,
3587                                                 spec->dmux_nids[0], index);
3588                                 if (nid)
3589                                         err = create_elem_capture_vol(codec,
3590                                                         nid, label,
3591                                                         type_idx, HDA_INPUT);
3592                                 if (err < 0)
3593                                         return err;
3594                         }
3595                 }
3596         }
3597
3598         return 0;
3599 }
3600
3601 static int check_mic_pin(struct hda_codec *codec, hda_nid_t nid,
3602                          hda_nid_t *fixed, hda_nid_t *ext, hda_nid_t *dock)
3603 {
3604         unsigned int cfg;
3605         unsigned int type;
3606
3607         if (!nid)
3608                 return 0;
3609         cfg = snd_hda_codec_get_pincfg(codec, nid);
3610         type = get_defcfg_device(cfg);
3611         switch (snd_hda_get_input_pin_attr(cfg)) {
3612         case INPUT_PIN_ATTR_INT:
3613                 if (*fixed)
3614                         return 1; /* already occupied */
3615                 if (type != AC_JACK_MIC_IN)
3616                         return 1; /* invalid type */
3617                 *fixed = nid;
3618                 break;
3619         case INPUT_PIN_ATTR_UNUSED:
3620                 break;
3621         case INPUT_PIN_ATTR_DOCK:
3622                 if (*dock)
3623                         return 1; /* already occupied */
3624                 if (type != AC_JACK_MIC_IN && type != AC_JACK_LINE_IN)
3625                         return 1; /* invalid type */
3626                 *dock = nid;
3627                 break;
3628         default:
3629                 if (*ext)
3630                         return 1; /* already occupied */
3631                 if (type != AC_JACK_MIC_IN)
3632                         return 1; /* invalid type */
3633                 *ext = nid;
3634                 break;
3635         }
3636         return 0;
3637 }
3638
3639 static int set_mic_route(struct hda_codec *codec,
3640                          struct sigmatel_mic_route *mic,
3641                          hda_nid_t pin)
3642 {
3643         struct sigmatel_spec *spec = codec->spec;
3644         struct auto_pin_cfg *cfg = &spec->autocfg;
3645         int i;
3646
3647         mic->pin = pin;
3648         if (pin == 0)
3649                 return 0;
3650         for (i = 0; i < cfg->num_inputs; i++) {
3651                 if (pin == cfg->inputs[i].pin)
3652                         break;
3653         }
3654         if (i < cfg->num_inputs && cfg->inputs[i].type == AUTO_PIN_MIC) {
3655                 /* analog pin */
3656                 i = get_connection_index(codec, spec->mux_nids[0], pin);
3657                 if (i < 0)
3658                         return -1;
3659                 mic->mux_idx = i;
3660                 mic->dmux_idx = -1;
3661                 if (spec->dmux_nids)
3662                         mic->dmux_idx = get_connection_index(codec,
3663                                                              spec->dmux_nids[0],
3664                                                              spec->mux_nids[0]);
3665         }  else if (spec->dmux_nids) {
3666                 /* digital pin */
3667                 i = get_connection_index(codec, spec->dmux_nids[0], pin);
3668                 if (i < 0)
3669                         return -1;
3670                 mic->dmux_idx = i;
3671                 mic->mux_idx = -1;
3672                 if (spec->mux_nids)
3673                         mic->mux_idx = get_connection_index(codec,
3674                                                             spec->mux_nids[0],
3675                                                             spec->dmux_nids[0]);
3676         }
3677         return 0;
3678 }
3679
3680 /* return non-zero if the device is for automatic mic switch */
3681 static int stac_check_auto_mic(struct hda_codec *codec)
3682 {
3683         struct sigmatel_spec *spec = codec->spec;
3684         struct auto_pin_cfg *cfg = &spec->autocfg;
3685         hda_nid_t fixed, ext, dock;
3686         int i;
3687
3688         fixed = ext = dock = 0;
3689         for (i = 0; i < cfg->num_inputs; i++)
3690                 if (check_mic_pin(codec, cfg->inputs[i].pin,
3691                     &fixed, &ext, &dock))
3692                         return 0;
3693         for (i = 0; i < spec->num_dmics; i++)
3694                 if (check_mic_pin(codec, spec->dmic_nids[i],
3695                     &fixed, &ext, &dock))
3696                         return 0;
3697         if (!fixed || (!ext && !dock))
3698                 return 0; /* no input to switch */
3699         if (!is_jack_detectable(codec, ext))
3700                 return 0; /* no unsol support */
3701         if (set_mic_route(codec, &spec->ext_mic, ext) ||
3702             set_mic_route(codec, &spec->int_mic, fixed) ||
3703             set_mic_route(codec, &spec->dock_mic, dock))
3704                 return 0; /* something is wrong */
3705         return 1;
3706 }
3707
3708 /* create playback/capture controls for input pins */
3709 static int stac92xx_auto_create_analog_input_ctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
3710 {
3711         struct sigmatel_spec *spec = codec->spec;
3712         struct hda_input_mux *imux = &spec->private_imux;
3713         int i, j;
3714         const char *label;
3715
3716         for (i = 0; i < cfg->num_inputs; i++) {
3717                 hda_nid_t nid = cfg->inputs[i].pin;
3718                 int index, err, type_idx;
3719
3720                 index = -1;
3721                 for (j = 0; j < spec->num_muxes; j++) {
3722                         index = get_connection_index(codec, spec->mux_nids[j],
3723                                                      nid);
3724                         if (index >= 0)
3725                                 break;
3726                 }
3727                 if (index < 0)
3728                         continue;
3729
3730                 label = hda_get_autocfg_input_label(codec, cfg, i);
3731                 snd_hda_add_imux_item(imux, label, index, &type_idx);
3732
3733                 err = create_elem_capture_vol(codec, nid,
3734                                               label, type_idx,
3735                                               HDA_INPUT);
3736                 if (err < 0)
3737                         return err;
3738         }
3739         spec->num_analog_muxes = imux->num_items;
3740
3741         if (imux->num_items) {
3742                 /*
3743                  * Set the current input for the muxes.
3744                  * The STAC9221 has two input muxes with identical source
3745                  * NID lists.  Hopefully this won't get confused.
3746                  */
3747                 for (i = 0; i < spec->num_muxes; i++) {
3748                         snd_hda_codec_write_cache(codec, spec->mux_nids[i], 0,
3749                                                   AC_VERB_SET_CONNECT_SEL,
3750                                                   imux->items[0].index);
3751                 }
3752         }
3753
3754         return 0;
3755 }
3756
3757 static void stac92xx_auto_init_multi_out(struct hda_codec *codec)
3758 {
3759         struct sigmatel_spec *spec = codec->spec;
3760         int i;
3761
3762         for (i = 0; i < spec->autocfg.line_outs; i++) {
3763                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
3764                 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
3765         }
3766 }
3767
3768 static void stac92xx_auto_init_hp_out(struct hda_codec *codec)
3769 {
3770         struct sigmatel_spec *spec = codec->spec;
3771         int i;
3772
3773         for (i = 0; i < spec->autocfg.hp_outs; i++) {
3774                 hda_nid_t pin;
3775                 pin = spec->autocfg.hp_pins[i];
3776                 if (pin) /* connect to front */
3777                         stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
3778         }
3779         for (i = 0; i < spec->autocfg.speaker_outs; i++) {
3780                 hda_nid_t pin;
3781                 pin = spec->autocfg.speaker_pins[i];
3782                 if (pin) /* connect to front */
3783                         stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN);
3784         }
3785 }
3786
3787 static int is_dual_headphones(struct hda_codec *codec)
3788 {
3789         struct sigmatel_spec *spec = codec->spec;
3790         int i, valid_hps;
3791
3792         if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT ||
3793             spec->autocfg.hp_outs <= 1)
3794                 return 0;
3795         valid_hps = 0;
3796         for (i = 0; i < spec->autocfg.hp_outs; i++) {
3797                 hda_nid_t nid = spec->autocfg.hp_pins[i];
3798                 unsigned int cfg = snd_hda_codec_get_pincfg(codec, nid);
3799                 if (get_defcfg_location(cfg) & AC_JACK_LOC_SEPARATE)
3800                         continue;
3801                 valid_hps++;
3802         }
3803         return (valid_hps > 1);
3804 }
3805
3806
3807 static int stac92xx_parse_auto_config(struct hda_codec *codec)
3808 {
3809         struct sigmatel_spec *spec = codec->spec;
3810         hda_nid_t dig_out = 0, dig_in = 0;
3811         int hp_swap = 0;
3812         int i, err;
3813
3814         if ((err = snd_hda_parse_pin_def_config(codec,
3815                                                 &spec->autocfg,
3816                                                 spec->dmic_nids)) < 0)
3817                 return err;
3818         if (! spec->autocfg.line_outs)
3819                 return 0; /* can't find valid pin config */
3820
3821         /* If we have no real line-out pin and multiple hp-outs, HPs should
3822          * be set up as multi-channel outputs.
3823          */
3824         if (is_dual_headphones(codec)) {
3825                 /* Copy hp_outs to line_outs, backup line_outs in
3826                  * speaker_outs so that the following routines can handle
3827                  * HP pins as primary outputs.
3828                  */
3829                 snd_printdd("stac92xx: Enabling multi-HPs workaround\n");
3830                 memcpy(spec->autocfg.speaker_pins, spec->autocfg.line_out_pins,
3831                        sizeof(spec->autocfg.line_out_pins));
3832                 spec->autocfg.speaker_outs = spec->autocfg.line_outs;
3833                 memcpy(spec->autocfg.line_out_pins, spec->autocfg.hp_pins,
3834                        sizeof(spec->autocfg.hp_pins));
3835                 spec->autocfg.line_outs = spec->autocfg.hp_outs;
3836                 spec->autocfg.line_out_type = AUTO_PIN_HP_OUT;
3837                 spec->autocfg.hp_outs = 0;
3838                 hp_swap = 1;
3839         }
3840         if (spec->autocfg.mono_out_pin) {
3841                 int dir = get_wcaps(codec, spec->autocfg.mono_out_pin) &
3842                         (AC_WCAP_OUT_AMP | AC_WCAP_IN_AMP);
3843                 u32 caps = query_amp_caps(codec,
3844                                 spec->autocfg.mono_out_pin, dir);
3845                 hda_nid_t conn_list[1];
3846
3847                 /* get the mixer node and then the mono mux if it exists */
3848                 if (snd_hda_get_connections(codec,
3849                                 spec->autocfg.mono_out_pin, conn_list, 1) &&
3850                                 snd_hda_get_connections(codec, conn_list[0],
3851                                 conn_list, 1) > 0) {
3852
3853                                 int wcaps = get_wcaps(codec, conn_list[0]);
3854                                 int wid_type = get_wcaps_type(wcaps);
3855                                 /* LR swap check, some stac925x have a mux that
3856                                  * changes the DACs output path instead of the
3857                                  * mono-mux path.
3858                                  */
3859                                 if (wid_type == AC_WID_AUD_SEL &&
3860                                                 !(wcaps & AC_WCAP_LR_SWAP))
3861                                         spec->mono_nid = conn_list[0];
3862                 }
3863                 if (dir) {
3864                         hda_nid_t nid = spec->autocfg.mono_out_pin;
3865
3866                         /* most mono outs have a least a mute/unmute switch */
3867                         dir = (dir & AC_WCAP_OUT_AMP) ? HDA_OUTPUT : HDA_INPUT;
3868                         err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE,
3869                                 "Mono Playback Switch",
3870                                 HDA_COMPOSE_AMP_VAL(nid, 1, 0, dir));
3871                         if (err < 0)
3872                                 return err;
3873                         /* check for volume support for the amp */
3874                         if ((caps & AC_AMPCAP_NUM_STEPS)
3875                                         >> AC_AMPCAP_NUM_STEPS_SHIFT) {
3876                                 err = stac92xx_add_control(spec,
3877                                         STAC_CTL_WIDGET_VOL,
3878                                         "Mono Playback Volume",
3879                                 HDA_COMPOSE_AMP_VAL(nid, 1, 0, dir));
3880                                 if (err < 0)
3881                                         return err;
3882                         }
3883                 }
3884
3885                 stac92xx_auto_set_pinctl(codec, spec->autocfg.mono_out_pin,
3886                                          AC_PINCTL_OUT_EN);
3887         }
3888
3889         if (!spec->multiout.num_dacs) {
3890                 err = stac92xx_auto_fill_dac_nids(codec);
3891                 if (err < 0)
3892                         return err;
3893                 err = stac92xx_auto_create_multi_out_ctls(codec,
3894                                                           &spec->autocfg);
3895                 if (err < 0)
3896                         return err;
3897         }
3898
3899         /* setup analog beep controls */
3900         if (spec->anabeep_nid > 0) {
3901                 err = stac92xx_auto_create_beep_ctls(codec,
3902                         spec->anabeep_nid);
3903                 if (err < 0)
3904                         return err;
3905         }
3906
3907         /* setup digital beep controls and input device */
3908 #ifdef CONFIG_SND_HDA_INPUT_BEEP
3909         if (spec->digbeep_nid > 0) {
3910                 hda_nid_t nid = spec->digbeep_nid;
3911                 unsigned int caps;
3912
3913                 err = stac92xx_auto_create_beep_ctls(codec, nid);
3914                 if (err < 0)
3915                         return err;
3916                 err = snd_hda_attach_beep_device(codec, nid);
3917                 if (err < 0)
3918                         return err;
3919                 if (codec->beep) {
3920                         /* IDT/STAC codecs have linear beep tone parameter */
3921                         codec->beep->linear_tone = spec->linear_tone_beep;
3922                         /* if no beep switch is available, make its own one */
3923                         caps = query_amp_caps(codec, nid, HDA_OUTPUT);
3924                         if (!(caps & AC_AMPCAP_MUTE)) {
3925                                 err = stac92xx_beep_switch_ctl(codec);
3926                                 if (err < 0)
3927                                         return err;
3928                         }
3929                 }
3930         }
3931 #endif
3932
3933         err = stac92xx_auto_create_hp_ctls(codec, &spec->autocfg);
3934         if (err < 0)
3935                 return err;
3936
3937         /* All output parsing done, now restore the swapped hp pins */
3938         if (hp_swap) {
3939                 memcpy(spec->autocfg.hp_pins, spec->autocfg.line_out_pins,
3940                        sizeof(spec->autocfg.hp_pins));
3941                 spec->autocfg.hp_outs = spec->autocfg.line_outs;
3942                 spec->autocfg.line_out_type = AUTO_PIN_HP_OUT;
3943                 spec->autocfg.line_outs = 0;
3944         }
3945
3946         if (stac_check_auto_mic(codec)) {
3947                 spec->auto_mic = 1;
3948                 /* only one capture for auto-mic */
3949                 spec->num_adcs = 1;
3950                 spec->num_caps = 1;
3951                 spec->num_muxes = 1;
3952         }
3953
3954         for (i = 0; i < spec->num_caps; i++) {
3955                 err = stac92xx_add_capvol_ctls(codec, spec->capvols[i],
3956                                                spec->capsws[i], i);
3957                 if (err < 0)
3958                         return err;
3959         }
3960
3961         err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg);
3962         if (err < 0)
3963                 return err;
3964
3965         if (spec->mono_nid > 0) {
3966                 err = stac92xx_auto_create_mono_output_ctls(codec);
3967                 if (err < 0)
3968                         return err;
3969         }
3970         if (spec->num_dmics > 0 && !spec->dinput_mux)
3971                 if ((err = stac92xx_auto_create_dmic_input_ctls(codec,
3972                                                 &spec->autocfg)) < 0)
3973                         return err;
3974         if (spec->num_muxes > 0) {
3975                 err = stac92xx_auto_create_mux_input_ctls(codec);
3976                 if (err < 0)
3977                         return err;
3978         }
3979         if (spec->num_smuxes > 0) {
3980                 err = stac92xx_auto_create_spdif_mux_ctls(codec);
3981                 if (err < 0)
3982                         return err;
3983         }
3984
3985         err = stac92xx_add_input_source(spec);
3986         if (err < 0)
3987                 return err;
3988
3989         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3990         if (spec->multiout.max_channels > 2)
3991                 spec->surr_switch = 1;
3992
3993         /* find digital out and in converters */
3994         for (i = codec->start_nid; i < codec->start_nid + codec->num_nodes; i++) {
3995                 unsigned int wid_caps = get_wcaps(codec, i);
3996                 if (wid_caps & AC_WCAP_DIGITAL) {
3997                         switch (get_wcaps_type(wid_caps)) {
3998                         case AC_WID_AUD_OUT:
3999                                 if (!dig_out)
4000                                         dig_out = i;
4001                                 break;
4002                         case AC_WID_AUD_IN:
4003                                 if (!dig_in)
4004                                         dig_in = i;
4005                                 break;
4006                         }
4007                 }
4008         }
4009         if (spec->autocfg.dig_outs)
4010                 spec->multiout.dig_out_nid = dig_out;
4011         if (dig_in && spec->autocfg.dig_in_pin)
4012                 spec->dig_in_nid = dig_in;
4013
4014         if (spec->kctls.list)
4015                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
4016
4017         spec->input_mux = &spec->private_imux;
4018         if (!spec->dinput_mux)
4019                 spec->dinput_mux = &spec->private_dimux;
4020         spec->sinput_mux = &spec->private_smux;
4021         spec->mono_mux = &spec->private_mono_mux;
4022         return 1;
4023 }
4024
4025 /* add playback controls for HP output */
4026 static int stac9200_auto_create_hp_ctls(struct hda_codec *codec,
4027                                         struct auto_pin_cfg *cfg)
4028 {
4029         struct sigmatel_spec *spec = codec->spec;
4030         hda_nid_t pin = cfg->hp_pins[0];
4031
4032         if (! pin)
4033                 return 0;
4034
4035         if (is_jack_detectable(codec, pin))
4036                 spec->hp_detect = 1;
4037
4038         return 0;
4039 }
4040
4041 /* add playback controls for LFE output */
4042 static int stac9200_auto_create_lfe_ctls(struct hda_codec *codec,
4043                                         struct auto_pin_cfg *cfg)
4044 {
4045         struct sigmatel_spec *spec = codec->spec;
4046         int err;
4047         hda_nid_t lfe_pin = 0x0;
4048         int i;
4049
4050         /*
4051          * search speaker outs and line outs for a mono speaker pin
4052          * with an amp.  If one is found, add LFE controls
4053          * for it.
4054          */
4055         for (i = 0; i < spec->autocfg.speaker_outs && lfe_pin == 0x0; i++) {
4056                 hda_nid_t pin = spec->autocfg.speaker_pins[i];
4057                 unsigned int wcaps = get_wcaps(codec, pin);
4058                 wcaps &= (AC_WCAP_STEREO | AC_WCAP_OUT_AMP);
4059                 if (wcaps == AC_WCAP_OUT_AMP)
4060                         /* found a mono speaker with an amp, must be lfe */
4061                         lfe_pin = pin;
4062         }
4063
4064         /* if speaker_outs is 0, then speakers may be in line_outs */
4065         if (lfe_pin == 0 && spec->autocfg.speaker_outs == 0) {
4066                 for (i = 0; i < spec->autocfg.line_outs && lfe_pin == 0x0; i++) {
4067                         hda_nid_t pin = spec->autocfg.line_out_pins[i];
4068                         unsigned int defcfg;
4069                         defcfg = snd_hda_codec_get_pincfg(codec, pin);
4070                         if (get_defcfg_device(defcfg) == AC_JACK_SPEAKER) {
4071                                 unsigned int wcaps = get_wcaps(codec, pin);
4072                                 wcaps &= (AC_WCAP_STEREO | AC_WCAP_OUT_AMP);
4073                                 if (wcaps == AC_WCAP_OUT_AMP)
4074                                         /* found a mono speaker with an amp,
4075                                            must be lfe */
4076                                         lfe_pin = pin;
4077                         }
4078                 }
4079         }
4080
4081         if (lfe_pin) {
4082                 err = create_controls(codec, "LFE", lfe_pin, 1);
4083                 if (err < 0)
4084                         return err;
4085         }
4086
4087         return 0;
4088 }
4089
4090 static int stac9200_parse_auto_config(struct hda_codec *codec)
4091 {
4092         struct sigmatel_spec *spec = codec->spec;
4093         int err;
4094
4095         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
4096                 return err;
4097
4098         if ((err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
4099                 return err;
4100
4101         if ((err = stac9200_auto_create_hp_ctls(codec, &spec->autocfg)) < 0)
4102                 return err;
4103
4104         if ((err = stac9200_auto_create_lfe_ctls(codec, &spec->autocfg)) < 0)
4105                 return err;
4106
4107         if (spec->num_muxes > 0) {
4108                 err = stac92xx_auto_create_mux_input_ctls(codec);
4109                 if (err < 0)
4110                         return err;
4111         }
4112
4113         err = stac92xx_add_input_source(spec);
4114         if (err < 0)
4115                 return err;
4116
4117         if (spec->autocfg.dig_outs)
4118                 spec->multiout.dig_out_nid = 0x05;
4119         if (spec->autocfg.dig_in_pin)
4120                 spec->dig_in_nid = 0x04;
4121
4122         if (spec->kctls.list)
4123                 spec->mixers[spec->num_mixers++] = spec->kctls.list;
4124
4125         spec->input_mux = &spec->private_imux;
4126         spec->dinput_mux = &spec->private_dimux;
4127
4128         return 1;
4129 }
4130
4131 /*
4132  * Early 2006 Intel Macintoshes with STAC9220X5 codecs seem to have a
4133  * funky external mute control using GPIO pins.
4134  */
4135
4136 static void stac_gpio_set(struct hda_codec *codec, unsigned int mask,
4137                           unsigned int dir_mask, unsigned int data)
4138 {
4139         unsigned int gpiostate, gpiomask, gpiodir;
4140
4141         snd_printdd("%s msk %x dir %x gpio %x\n", __func__, mask, dir_mask, data);
4142
4143         gpiostate = snd_hda_codec_read(codec, codec->afg, 0,
4144                                        AC_VERB_GET_GPIO_DATA, 0);
4145         gpiostate = (gpiostate & ~dir_mask) | (data & dir_mask);
4146
4147         gpiomask = snd_hda_codec_read(codec, codec->afg, 0,
4148                                       AC_VERB_GET_GPIO_MASK, 0);
4149         gpiomask |= mask;
4150
4151         gpiodir = snd_hda_codec_read(codec, codec->afg, 0,
4152                                      AC_VERB_GET_GPIO_DIRECTION, 0);
4153         gpiodir |= dir_mask;
4154
4155         /* Configure GPIOx as CMOS */
4156         snd_hda_codec_write(codec, codec->afg, 0, 0x7e7, 0);
4157
4158         snd_hda_codec_write(codec, codec->afg, 0,
4159                             AC_VERB_SET_GPIO_MASK, gpiomask);
4160         snd_hda_codec_read(codec, codec->afg, 0,
4161                            AC_VERB_SET_GPIO_DIRECTION, gpiodir); /* sync */
4162
4163         msleep(1);
4164
4165         snd_hda_codec_read(codec, codec->afg, 0,
4166                            AC_VERB_SET_GPIO_DATA, gpiostate); /* sync */
4167 }
4168
4169 static int stac92xx_add_jack(struct hda_codec *codec,
4170                 hda_nid_t nid, int type)
4171 {
4172 #ifdef CONFIG_SND_HDA_INPUT_JACK
4173         int def_conf = snd_hda_codec_get_pincfg(codec, nid);
4174         int connectivity = get_defcfg_connect(def_conf);
4175
4176         if (connectivity && connectivity != AC_JACK_PORT_FIXED)
4177                 return 0;
4178
4179         return snd_hda_input_jack_add(codec, nid, type, NULL);
4180 #else
4181         return 0;
4182 #endif /* CONFIG_SND_HDA_INPUT_JACK */
4183 }
4184
4185 static int stac_add_event(struct sigmatel_spec *spec, hda_nid_t nid,
4186                           unsigned char type, int data)
4187 {
4188         struct sigmatel_event *event;
4189
4190         snd_array_init(&spec->events, sizeof(*event), 32);
4191         event = snd_array_new(&spec->events);
4192         if (!event)
4193                 return -ENOMEM;
4194         event->nid = nid;
4195         event->type = type;
4196         event->tag = spec->events.used;
4197         event->data = data;
4198
4199         return event->tag;
4200 }
4201
4202 static struct sigmatel_event *stac_get_event(struct hda_codec *codec,
4203                                              hda_nid_t nid)
4204 {
4205         struct sigmatel_spec *spec = codec->spec;
4206         struct sigmatel_event *event = spec->events.list;
4207         int i;
4208
4209         for (i = 0; i < spec->events.used; i++, event++) {
4210                 if (event->nid == nid)
4211                         return event;
4212         }
4213         return NULL;
4214 }
4215
4216 static struct sigmatel_event *stac_get_event_from_tag(struct hda_codec *codec,
4217                                                       unsigned char tag)
4218 {
4219         struct sigmatel_spec *spec = codec->spec;
4220         struct sigmatel_event *event = spec->events.list;
4221         int i;
4222
4223         for (i = 0; i < spec->events.used; i++, event++) {
4224                 if (event->tag == tag)
4225                         return event;
4226         }
4227         return NULL;
4228 }
4229
4230 /* check if given nid is a valid pin and no other events are assigned
4231  * to it.  If OK, assign the event, set the unsol flag, and returns 1.
4232  * Otherwise, returns zero.
4233  */
4234 static int enable_pin_detect(struct hda_codec *codec, hda_nid_t nid,
4235                              unsigned int type)
4236 {
4237         struct sigmatel_event *event;
4238         int tag;
4239
4240         if (!is_jack_detectable(codec, nid))
4241                 return 0;
4242         event = stac_get_event(codec, nid);
4243         if (event) {
4244                 if (event->type != type)
4245                         return 0;
4246                 tag = event->tag;
4247         } else {
4248                 tag = stac_add_event(codec->spec, nid, type, 0);
4249                 if (tag < 0)
4250                         return 0;
4251         }
4252         snd_hda_codec_write_cache(codec, nid, 0,
4253                                   AC_VERB_SET_UNSOLICITED_ENABLE,
4254                                   AC_USRSP_EN | tag);
4255         return 1;
4256 }
4257
4258 static int is_nid_out_jack_pin(struct auto_pin_cfg *cfg, hda_nid_t nid)
4259 {
4260         int i;
4261         for (i = 0; i < cfg->hp_outs; i++)
4262                 if (cfg->hp_pins[i] == nid)
4263                         return 1; /* nid is a HP-Out */
4264         for (i = 0; i < cfg->line_outs; i++)
4265                 if (cfg->line_out_pins[i] == nid)
4266                         return 1; /* nid is a line-Out */
4267         return 0; /* nid is not a HP-Out */
4268 };
4269
4270 static void stac92xx_power_down(struct hda_codec *codec)
4271 {
4272         struct sigmatel_spec *spec = codec->spec;
4273
4274         /* power down inactive DACs */
4275         const hda_nid_t *dac;
4276         for (dac = spec->dac_list; *dac; dac++)
4277                 if (!check_all_dac_nids(spec, *dac))
4278                         snd_hda_codec_write(codec, *dac, 0,
4279                                         AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
4280 }
4281
4282 static void stac_toggle_power_map(struct hda_codec *codec, hda_nid_t nid,
4283                                   int enable);
4284
4285 static inline int get_int_hint(struct hda_codec *codec, const char *key,
4286                                int *valp)
4287 {
4288         const char *p;
4289         p = snd_hda_get_hint(codec, key);
4290         if (p) {
4291                 unsigned long val;
4292                 if (!strict_strtoul(p, 0, &val)) {
4293                         *valp = val;
4294                         return 1;
4295                 }
4296         }
4297         return 0;
4298 }
4299
4300 /* override some hints from the hwdep entry */
4301 static void stac_store_hints(struct hda_codec *codec)
4302 {
4303         struct sigmatel_spec *spec = codec->spec;
4304         int val;
4305
4306         val = snd_hda_get_bool_hint(codec, "hp_detect");
4307         if (val >= 0)
4308                 spec->hp_detect = val;
4309         if (get_int_hint(codec, "gpio_mask", &spec->gpio_mask)) {
4310                 spec->eapd_mask = spec->gpio_dir = spec->gpio_data =
4311                         spec->gpio_mask;
4312         }
4313         if (get_int_hint(codec, "gpio_dir", &spec->gpio_dir))
4314                 spec->gpio_dir &= spec->gpio_mask;
4315         if (get_int_hint(codec, "gpio_data", &spec->gpio_data))
4316                 spec->gpio_data &= spec->gpio_mask;
4317         if (get_int_hint(codec, "eapd_mask", &spec->eapd_mask))
4318                 spec->eapd_mask &= spec->gpio_mask;
4319         if (get_int_hint(codec, "gpio_mute", &spec->gpio_mute))
4320                 spec->gpio_mute &= spec->gpio_mask;
4321         val = snd_hda_get_bool_hint(codec, "eapd_switch");
4322         if (val >= 0)
4323                 spec->eapd_switch = val;
4324         get_int_hint(codec, "gpio_led_polarity", &spec->gpio_led_polarity);
4325         if (get_int_hint(codec, "gpio_led", &spec->gpio_led)) {
4326                 spec->gpio_mask |= spec->gpio_led;
4327                 spec->gpio_dir |= spec->gpio_led;
4328                 if (spec->gpio_led_polarity)
4329                         spec->gpio_data |= spec->gpio_led;
4330         }
4331 }
4332
4333 static void stac_issue_unsol_events(struct hda_codec *codec, int num_pins,
4334                                     const hda_nid_t *pins)
4335 {
4336         while (num_pins--)
4337                 stac_issue_unsol_event(codec, *pins++);
4338 }
4339
4340 /* fake event to set up pins */
4341 static void stac_fake_hp_events(struct hda_codec *codec)
4342 {
4343         struct sigmatel_spec *spec = codec->spec;
4344
4345         if (spec->autocfg.hp_outs)
4346                 stac_issue_unsol_events(codec, spec->autocfg.hp_outs,
4347                                         spec->autocfg.hp_pins);
4348         if (spec->autocfg.line_outs &&
4349             spec->autocfg.line_out_pins[0] != spec->autocfg.hp_pins[0])
4350                 stac_issue_unsol_events(codec, spec->autocfg.line_outs,
4351                                         spec->autocfg.line_out_pins);
4352 }
4353
4354 static int stac92xx_init(struct hda_codec *codec)
4355 {
4356         struct sigmatel_spec *spec = codec->spec;
4357         struct auto_pin_cfg *cfg = &spec->autocfg;
4358         unsigned int gpio;
4359         int i;
4360
4361         snd_hda_sequence_write(codec, spec->init);
4362
4363         /* power down adcs initially */
4364         if (spec->powerdown_adcs)
4365                 for (i = 0; i < spec->num_adcs; i++)
4366                         snd_hda_codec_write(codec,
4367                                 spec->adc_nids[i], 0,
4368                                 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
4369
4370         /* override some hints */
4371         stac_store_hints(codec);
4372
4373         /* set up GPIO */
4374         gpio = spec->gpio_data;
4375         /* turn on EAPD statically when spec->eapd_switch isn't set.
4376          * otherwise, unsol event will turn it on/off dynamically
4377          */
4378         if (!spec->eapd_switch)
4379                 gpio |= spec->eapd_mask;
4380         stac_gpio_set(codec, spec->gpio_mask, spec->gpio_dir, gpio);
4381
4382         /* set up pins */
4383         if (spec->hp_detect) {
4384                 /* Enable unsolicited responses on the HP widget */
4385                 for (i = 0; i < cfg->hp_outs; i++) {
4386                         hda_nid_t nid = cfg->hp_pins[i];
4387                         enable_pin_detect(codec, nid, STAC_HP_EVENT);
4388                 }
4389                 if (cfg->line_out_type == AUTO_PIN_LINE_OUT &&
4390                     cfg->speaker_outs > 0) {
4391                         /* enable pin-detect for line-outs as well */
4392                         for (i = 0; i < cfg->line_outs; i++) {
4393                                 hda_nid_t nid = cfg->line_out_pins[i];
4394                                 enable_pin_detect(codec, nid, STAC_LO_EVENT);
4395                         }
4396                 }
4397
4398                 /* force to enable the first line-out; the others are set up
4399                  * in unsol_event
4400                  */
4401                 stac92xx_auto_set_pinctl(codec, spec->autocfg.line_out_pins[0],
4402                                 AC_PINCTL_OUT_EN);
4403                 /* fake event to set up pins */
4404                 stac_fake_hp_events(codec);
4405         } else {
4406                 stac92xx_auto_init_multi_out(codec);
4407                 stac92xx_auto_init_hp_out(codec);
4408                 for (i = 0; i < cfg->hp_outs; i++)
4409                         stac_toggle_power_map(codec, cfg->hp_pins[i], 1);
4410         }
4411         if (spec->auto_mic) {
4412                 /* initialize connection to analog input */
4413                 if (spec->dmux_nids)
4414                         snd_hda_codec_write_cache(codec, spec->dmux_nids[0], 0,
4415                                           AC_VERB_SET_CONNECT_SEL, 0);
4416                 if (enable_pin_detect(codec, spec->ext_mic.pin, STAC_MIC_EVENT))
4417                         stac_issue_unsol_event(codec, spec->ext_mic.pin);
4418                 if (enable_pin_detect(codec, spec->dock_mic.pin,
4419                     STAC_MIC_EVENT))
4420                         stac_issue_unsol_event(codec, spec->dock_mic.pin);
4421         }
4422         for (i = 0; i < cfg->num_inputs; i++) {
4423                 hda_nid_t nid = cfg->inputs[i].pin;
4424                 int type = cfg->inputs[i].type;
4425                 unsigned int pinctl, conf;
4426                 if (type == AUTO_PIN_MIC) {
4427                         /* for mic pins, force to initialize */
4428                         pinctl = stac92xx_get_default_vref(codec, nid);
4429                         pinctl |= AC_PINCTL_IN_EN;
4430                         stac92xx_auto_set_pinctl(codec, nid, pinctl);
4431                 } else {
4432                         pinctl = snd_hda_codec_read(codec, nid, 0,
4433                                         AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4434                         /* if PINCTL already set then skip */
4435                         /* Also, if both INPUT and OUTPUT are set,
4436                          * it must be a BIOS bug; need to override, too
4437                          */
4438                         if (!(pinctl & AC_PINCTL_IN_EN) ||
4439                             (pinctl & AC_PINCTL_OUT_EN)) {
4440                                 pinctl &= ~AC_PINCTL_OUT_EN;
4441                                 pinctl |= AC_PINCTL_IN_EN;
4442                                 stac92xx_auto_set_pinctl(codec, nid, pinctl);
4443                         }
4444                 }
4445                 conf = snd_hda_codec_get_pincfg(codec, nid);
4446                 if (get_defcfg_connect(conf) != AC_JACK_PORT_FIXED) {
4447                         if (enable_pin_detect(codec, nid, STAC_INSERT_EVENT))
4448                                 stac_issue_unsol_event(codec, nid);
4449                 }
4450         }
4451         for (i = 0; i < spec->num_dmics; i++)
4452                 stac92xx_auto_set_pinctl(codec, spec->dmic_nids[i],
4453                                         AC_PINCTL_IN_EN);
4454         if (cfg->dig_out_pins[0])
4455                 stac92xx_auto_set_pinctl(codec, cfg->dig_out_pins[0],
4456                                          AC_PINCTL_OUT_EN);
4457         if (cfg->dig_in_pin)
4458                 stac92xx_auto_set_pinctl(codec, cfg->dig_in_pin,
4459                                          AC_PINCTL_IN_EN);
4460         for (i = 0; i < spec->num_pwrs; i++)  {
4461                 hda_nid_t nid = spec->pwr_nids[i];
4462                 unsigned int pinctl, def_conf;
4463
4464                 /* power on when no jack detection is available */
4465                 /* or when the VREF is used for controlling LED */
4466                 if (!spec->hp_detect ||
4467                     spec->vref_mute_led_nid == nid) {
4468                         stac_toggle_power_map(codec, nid, 1);
4469                         continue;
4470                 }
4471
4472                 if (is_nid_out_jack_pin(cfg, nid))
4473                         continue; /* already has an unsol event */
4474
4475                 pinctl = snd_hda_codec_read(codec, nid, 0,
4476                                             AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4477                 /* outputs are only ports capable of power management
4478                  * any attempts on powering down a input port cause the
4479                  * referenced VREF to act quirky.
4480                  */
4481                 if (pinctl & AC_PINCTL_IN_EN) {
4482                         stac_toggle_power_map(codec, nid, 1);
4483                         continue;
4484                 }
4485                 def_conf = snd_hda_codec_get_pincfg(codec, nid);
4486                 def_conf = get_defcfg_connect(def_conf);
4487                 /* skip any ports that don't have jacks since presence
4488                  * detection is useless */
4489                 if (def_conf != AC_JACK_PORT_COMPLEX ||
4490                     !is_jack_detectable(codec, nid)) {
4491                         stac_toggle_power_map(codec, nid, 1);
4492                         continue;
4493                 }
4494                 if (enable_pin_detect(codec, nid, STAC_PWR_EVENT)) {
4495                         stac_issue_unsol_event(codec, nid);
4496                         continue;
4497                 }
4498                 /* none of the above, turn the port OFF */
4499                 stac_toggle_power_map(codec, nid, 0);
4500         }
4501
4502         /* sync mute LED */
4503         if (spec->gpio_led)
4504                 hda_call_check_power_status(codec, 0x01);
4505         if (spec->dac_list)
4506                 stac92xx_power_down(codec);
4507         return 0;
4508 }
4509
4510 static void stac92xx_free_kctls(struct hda_codec *codec)
4511 {
4512         struct sigmatel_spec *spec = codec->spec;
4513
4514         if (spec->kctls.list) {
4515                 struct snd_kcontrol_new *kctl = spec->kctls.list;
4516                 int i;
4517                 for (i = 0; i < spec->kctls.used; i++)
4518                         kfree(kctl[i].name);
4519         }
4520         snd_array_free(&spec->kctls);
4521 }
4522
4523 static void stac92xx_shutup_pins(struct hda_codec *codec)
4524 {
4525         unsigned int i, def_conf;
4526
4527         if (codec->bus->shutdown)
4528                 return;
4529         for (i = 0; i < codec->init_pins.used; i++) {
4530                 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4531                 def_conf = snd_hda_codec_get_pincfg(codec, pin->nid);
4532                 if (get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)
4533                         snd_hda_codec_write(codec, pin->nid, 0,
4534                                     AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
4535         }
4536 }
4537
4538 static void stac92xx_shutup(struct hda_codec *codec)
4539 {
4540         struct sigmatel_spec *spec = codec->spec;
4541
4542         stac92xx_shutup_pins(codec);
4543
4544         if (spec->eapd_mask)
4545                 stac_gpio_set(codec, spec->gpio_mask,
4546                                 spec->gpio_dir, spec->gpio_data &
4547                                 ~spec->eapd_mask);
4548 }
4549
4550 static void stac92xx_free(struct hda_codec *codec)
4551 {
4552         struct sigmatel_spec *spec = codec->spec;
4553
4554         if (! spec)
4555                 return;
4556
4557         stac92xx_shutup(codec);
4558         snd_hda_input_jack_free(codec);
4559         snd_array_free(&spec->events);
4560
4561         kfree(spec);
4562         snd_hda_detach_beep_device(codec);
4563 }
4564
4565 static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
4566                                 unsigned int flag)
4567 {
4568         unsigned int old_ctl, pin_ctl;
4569
4570         pin_ctl = snd_hda_codec_read(codec, nid,
4571                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
4572
4573         if (pin_ctl & AC_PINCTL_IN_EN) {
4574                 /*
4575                  * we need to check the current set-up direction of
4576                  * shared input pins since they can be switched via
4577                  * "xxx as Output" mixer switch
4578                  */
4579                 struct sigmatel_spec *spec = codec->spec;
4580                 if (nid == spec->line_switch || nid == spec->mic_switch)
4581                         return;
4582         }
4583
4584         old_ctl = pin_ctl;
4585         /* if setting pin direction bits, clear the current
4586            direction bits first */
4587         if (flag & (AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN))
4588                 pin_ctl &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
4589         
4590         pin_ctl |= flag;
4591         if (old_ctl != pin_ctl)
4592                 snd_hda_codec_write_cache(codec, nid, 0,
4593                                           AC_VERB_SET_PIN_WIDGET_CONTROL,
4594                                           pin_ctl);
4595 }
4596
4597 static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
4598                                   unsigned int flag)
4599 {
4600         unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
4601                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
4602         if (pin_ctl & flag)
4603                 snd_hda_codec_write_cache(codec, nid, 0,
4604                                           AC_VERB_SET_PIN_WIDGET_CONTROL,
4605                                           pin_ctl & ~flag);
4606 }
4607
4608 static inline int get_pin_presence(struct hda_codec *codec, hda_nid_t nid)
4609 {
4610         if (!nid)
4611                 return 0;
4612         return snd_hda_jack_detect(codec, nid);
4613 }
4614
4615 static void stac92xx_line_out_detect(struct hda_codec *codec,
4616                                      int presence)
4617 {
4618         struct sigmatel_spec *spec = codec->spec;
4619         struct auto_pin_cfg *cfg = &spec->autocfg;
4620         int i;
4621
4622         for (i = 0; i < cfg->line_outs; i++) {
4623                 if (presence)
4624                         break;
4625                 presence = get_pin_presence(codec, cfg->line_out_pins[i]);
4626                 if (presence) {
4627                         unsigned int pinctl;
4628                         pinctl = snd_hda_codec_read(codec,
4629                                                     cfg->line_out_pins[i], 0,
4630                                             AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4631                         if (pinctl & AC_PINCTL_IN_EN)
4632                                 presence = 0; /* mic- or line-input */
4633                 }
4634         }
4635
4636         if (presence) {
4637                 /* disable speakers */
4638                 for (i = 0; i < cfg->speaker_outs; i++)
4639                         stac92xx_reset_pinctl(codec, cfg->speaker_pins[i],
4640                                                 AC_PINCTL_OUT_EN);
4641                 if (spec->eapd_mask && spec->eapd_switch)
4642                         stac_gpio_set(codec, spec->gpio_mask,
4643                                 spec->gpio_dir, spec->gpio_data &
4644                                 ~spec->eapd_mask);
4645         } else {
4646                 /* enable speakers */
4647                 for (i = 0; i < cfg->speaker_outs; i++)
4648                         stac92xx_set_pinctl(codec, cfg->speaker_pins[i],
4649                                                 AC_PINCTL_OUT_EN);
4650                 if (spec->eapd_mask && spec->eapd_switch)
4651                         stac_gpio_set(codec, spec->gpio_mask,
4652                                 spec->gpio_dir, spec->gpio_data |
4653                                 spec->eapd_mask);
4654         }
4655
4656
4657 /* return non-zero if the hp-pin of the given array index isn't
4658  * a jack-detection target
4659  */
4660 static int no_hp_sensing(struct sigmatel_spec *spec, int i)
4661 {
4662         struct auto_pin_cfg *cfg = &spec->autocfg;
4663
4664         /* ignore sensing of shared line and mic jacks */
4665         if (cfg->hp_pins[i] == spec->line_switch)
4666                 return 1;
4667         if (cfg->hp_pins[i] == spec->mic_switch)
4668                 return 1;
4669         /* ignore if the pin is set as line-out */
4670         if (cfg->hp_pins[i] == spec->hp_switch)
4671                 return 1;
4672         return 0;
4673 }
4674
4675 static void stac92xx_hp_detect(struct hda_codec *codec)
4676 {
4677         struct sigmatel_spec *spec = codec->spec;
4678         struct auto_pin_cfg *cfg = &spec->autocfg;
4679         int i, presence;
4680
4681         presence = 0;
4682         if (spec->gpio_mute)
4683                 presence = !(snd_hda_codec_read(codec, codec->afg, 0,
4684                         AC_VERB_GET_GPIO_DATA, 0) & spec->gpio_mute);
4685
4686         for (i = 0; i < cfg->hp_outs; i++) {
4687                 if (presence)
4688                         break;
4689                 if (no_hp_sensing(spec, i))
4690                         continue;
4691                 presence = get_pin_presence(codec, cfg->hp_pins[i]);
4692                 if (presence) {
4693                         unsigned int pinctl;
4694                         pinctl = snd_hda_codec_read(codec, cfg->hp_pins[i], 0,
4695                                             AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4696                         if (pinctl & AC_PINCTL_IN_EN)
4697                                 presence = 0; /* mic- or line-input */
4698                 }
4699         }
4700
4701         if (presence) {
4702                 /* disable lineouts */
4703                 if (spec->hp_switch)
4704                         stac92xx_reset_pinctl(codec, spec->hp_switch,
4705                                               AC_PINCTL_OUT_EN);
4706                 for (i = 0; i < cfg->line_outs; i++)
4707                         stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
4708                                                 AC_PINCTL_OUT_EN);
4709         } else {
4710                 /* enable lineouts */
4711                 if (spec->hp_switch)
4712                         stac92xx_set_pinctl(codec, spec->hp_switch,
4713                                             AC_PINCTL_OUT_EN);
4714                 for (i = 0; i < cfg->line_outs; i++)
4715                         stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
4716                                                 AC_PINCTL_OUT_EN);
4717         }
4718         stac92xx_line_out_detect(codec, presence);
4719         /* toggle hp outs */
4720         for (i = 0; i < cfg->hp_outs; i++) {
4721                 unsigned int val = AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN;
4722                 if (no_hp_sensing(spec, i))
4723                         continue;
4724                 if (1 /*presence*/)
4725                         stac92xx_set_pinctl(codec, cfg->hp_pins[i], val);
4726 #if 0 /* FIXME */
4727 /* Resetting the pinctl like below may lead to (a sort of) regressions
4728  * on some devices since they use the HP pin actually for line/speaker
4729  * outs although the default pin config shows a different pin (that is
4730  * wrong and useless).
4731  *
4732  * So, it's basically a problem of default pin configs, likely a BIOS issue.
4733  * But, disabling the code below just works around it, and I'm too tired of
4734  * bug reports with such devices... 
4735  */
4736                 else
4737                         stac92xx_reset_pinctl(codec, cfg->hp_pins[i], val);
4738 #endif /* FIXME */
4739         }
4740
4741
4742 static void stac_toggle_power_map(struct hda_codec *codec, hda_nid_t nid,
4743                                   int enable)
4744 {
4745         struct sigmatel_spec *spec = codec->spec;
4746         unsigned int idx, val;
4747
4748         for (idx = 0; idx < spec->num_pwrs; idx++) {
4749                 if (spec->pwr_nids[idx] == nid)
4750                         break;
4751         }
4752         if (idx >= spec->num_pwrs)
4753                 return;
4754
4755         idx = 1 << idx;
4756
4757         val = snd_hda_codec_read(codec, codec->afg, 0, 0x0fec, 0x0) & 0xff;
4758         if (enable)
4759                 val &= ~idx;
4760         else
4761                 val |= idx;
4762
4763         /* power down unused output ports */
4764         snd_hda_codec_write(codec, codec->afg, 0, 0x7ec, val);
4765 }
4766
4767 static void stac92xx_pin_sense(struct hda_codec *codec, hda_nid_t nid)
4768 {
4769         stac_toggle_power_map(codec, nid, get_pin_presence(codec, nid));
4770 }
4771
4772 /* get the pin connection (fixed, none, etc) */
4773 static unsigned int stac_get_defcfg_connect(struct hda_codec *codec, int idx)
4774 {
4775         struct sigmatel_spec *spec = codec->spec;
4776         unsigned int cfg;
4777
4778         cfg = snd_hda_codec_get_pincfg(codec, spec->pin_nids[idx]);
4779         return get_defcfg_connect(cfg);
4780 }
4781
4782 static int stac92xx_connected_ports(struct hda_codec *codec,
4783                                     const hda_nid_t *nids, int num_nids)
4784 {
4785         struct sigmatel_spec *spec = codec->spec;
4786         int idx, num;
4787         unsigned int def_conf;
4788
4789         for (num = 0; num < num_nids; num++) {
4790                 for (idx = 0; idx < spec->num_pins; idx++)
4791                         if (spec->pin_nids[idx] == nids[num])
4792                                 break;
4793                 if (idx >= spec->num_pins)
4794                         break;
4795                 def_conf = stac_get_defcfg_connect(codec, idx);
4796                 if (def_conf == AC_JACK_PORT_NONE)
4797                         break;
4798         }
4799         return num;
4800 }
4801
4802 static void stac92xx_mic_detect(struct hda_codec *codec)
4803 {
4804         struct sigmatel_spec *spec = codec->spec;
4805         struct sigmatel_mic_route *mic;
4806
4807         if (get_pin_presence(codec, spec->ext_mic.pin))
4808                 mic = &spec->ext_mic;
4809         else if (get_pin_presence(codec, spec->dock_mic.pin))
4810                 mic = &spec->dock_mic;
4811         else
4812                 mic = &spec->int_mic;
4813         if (mic->dmux_idx >= 0)
4814                 snd_hda_codec_write_cache(codec, spec->dmux_nids[0], 0,
4815                                           AC_VERB_SET_CONNECT_SEL,
4816                                           mic->dmux_idx);
4817         if (mic->mux_idx >= 0)
4818                 snd_hda_codec_write_cache(codec, spec->mux_nids[0], 0,
4819                                           AC_VERB_SET_CONNECT_SEL,
4820                                           mic->mux_idx);
4821 }
4822
4823 static void stac_issue_unsol_event(struct hda_codec *codec, hda_nid_t nid)
4824 {
4825         struct sigmatel_event *event = stac_get_event(codec, nid);
4826         if (!event)
4827                 return;
4828         codec->patch_ops.unsol_event(codec, (unsigned)event->tag << 26);
4829 }
4830
4831 static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
4832 {
4833         struct sigmatel_spec *spec = codec->spec;
4834         struct sigmatel_event *event;
4835         int tag, data;
4836
4837         tag = (res >> 26) & 0x7f;
4838         event = stac_get_event_from_tag(codec, tag);
4839         if (!event)
4840                 return;
4841
4842         switch (event->type) {
4843         case STAC_HP_EVENT:
4844         case STAC_LO_EVENT:
4845                 stac92xx_hp_detect(codec);
4846                 break;
4847         case STAC_MIC_EVENT:
4848                 stac92xx_mic_detect(codec);
4849                 break;
4850         }
4851
4852         switch (event->type) {
4853         case STAC_HP_EVENT:
4854         case STAC_LO_EVENT:
4855         case STAC_MIC_EVENT:
4856         case STAC_INSERT_EVENT:
4857         case STAC_PWR_EVENT:
4858                 if (spec->num_pwrs > 0)
4859                         stac92xx_pin_sense(codec, event->nid);
4860                 snd_hda_input_jack_report(codec, event->nid);
4861
4862                 switch (codec->subsystem_id) {
4863                 case 0x103c308f:
4864                         if (event->nid == 0xb) {
4865                                 int pin = AC_PINCTL_IN_EN;
4866
4867                                 if (get_pin_presence(codec, 0xa)
4868                                                 && get_pin_presence(codec, 0xb))
4869                                         pin |= AC_PINCTL_VREF_80;
4870                                 if (!get_pin_presence(codec, 0xb))
4871                                         pin |= AC_PINCTL_VREF_80;
4872
4873                                 /* toggle VREF state based on mic + hp pin
4874                                  * status
4875                                  */
4876                                 stac92xx_auto_set_pinctl(codec, 0x0a, pin);
4877                         }
4878                 }
4879                 break;
4880         case STAC_VREF_EVENT:
4881                 data = snd_hda_codec_read(codec, codec->afg, 0,
4882                                           AC_VERB_GET_GPIO_DATA, 0);
4883                 /* toggle VREF state based on GPIOx status */
4884                 snd_hda_codec_write(codec, codec->afg, 0, 0x7e0,
4885                                     !!(data & (1 << event->data)));
4886                 break;
4887         }
4888 }
4889
4890 static int hp_blike_system(u32 subsystem_id);
4891
4892 static void set_hp_led_gpio(struct hda_codec *codec)
4893 {
4894         struct sigmatel_spec *spec = codec->spec;
4895         unsigned int gpio;
4896
4897         if (spec->gpio_led)
4898                 return;
4899
4900         gpio = snd_hda_param_read(codec, codec->afg, AC_PAR_GPIO_CAP);
4901         gpio &= AC_GPIO_IO_COUNT;
4902         if (gpio > 3)
4903                 spec->gpio_led = 0x08; /* GPIO 3 */
4904         else
4905                 spec->gpio_led = 0x01; /* GPIO 0 */
4906 }
4907
4908 /*
4909  * This method searches for the mute LED GPIO configuration
4910  * provided as OEM string in SMBIOS. The format of that string
4911  * is HP_Mute_LED_P_G or HP_Mute_LED_P
4912  * where P can be 0 or 1 and defines mute LED GPIO control state (low/high)
4913  * that corresponds to the NOT muted state of the master volume
4914  * and G is the index of the GPIO to use as the mute LED control (0..9)
4915  * If _G portion is missing it is assigned based on the codec ID
4916  *
4917  * So, HP B-series like systems may have HP_Mute_LED_0 (current models)
4918  * or  HP_Mute_LED_0_3 (future models) OEM SMBIOS strings
4919  *
4920  *
4921  * The dv-series laptops don't seem to have the HP_Mute_LED* strings in
4922  * SMBIOS - at least the ones I have seen do not have them - which include
4923  * my own system (HP Pavilion dv6-1110ax) and my cousin's
4924  * HP Pavilion dv9500t CTO.
4925  * Need more information on whether it is true across the entire series.
4926  * -- kunal
4927  */
4928 static int find_mute_led_gpio(struct hda_codec *codec, int default_polarity)
4929 {
4930         struct sigmatel_spec *spec = codec->spec;
4931         const struct dmi_device *dev = NULL;
4932
4933         if ((codec->subsystem_id >> 16) == PCI_VENDOR_ID_HP) {
4934                 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING,
4935                                                                 NULL, dev))) {
4936                         if (sscanf(dev->name, "HP_Mute_LED_%d_%x",
4937                                   &spec->gpio_led_polarity,
4938                                   &spec->gpio_led) == 2) {
4939                                 unsigned int max_gpio;
4940                                 max_gpio = snd_hda_param_read(codec, codec->afg,
4941                                                               AC_PAR_GPIO_CAP);
4942                                 max_gpio &= AC_GPIO_IO_COUNT;
4943                                 if (spec->gpio_led < max_gpio)
4944                                         spec->gpio_led = 1 << spec->gpio_led;
4945                                 else
4946                                         spec->vref_mute_led_nid = spec->gpio_led;
4947                                 return 1;
4948                         }
4949                         if (sscanf(dev->name, "HP_Mute_LED_%d",
4950                                   &spec->gpio_led_polarity) == 1) {
4951                                 set_hp_led_gpio(codec);
4952                                 return 1;
4953                         }
4954                         /* BIOS bug: unfilled OEM string */
4955                         if (strstr(dev->name, "HP_Mute_LED_P_G")) {
4956                                 set_hp_led_gpio(codec);
4957                                 switch (codec->subsystem_id) {
4958                                 case 0x103c148a:
4959                                         spec->gpio_led_polarity = 0;
4960                                         break;
4961                                 default:
4962                                         spec->gpio_led_polarity = 1;
4963                                         break;
4964                                 }
4965                                 return 1;
4966                         }
4967                 }
4968
4969                 /*
4970                  * Fallback case - if we don't find the DMI strings,
4971                  * we statically set the GPIO - if not a B-series system.
4972                  */
4973                 if (!hp_blike_system(codec->subsystem_id)) {
4974                         set_hp_led_gpio(codec);
4975                         spec->gpio_led_polarity = default_polarity;
4976                         return 1;
4977                 }
4978         }
4979         return 0;
4980 }
4981
4982 static int hp_blike_system(u32 subsystem_id)
4983 {
4984         switch (subsystem_id) {
4985         case 0x103c1473: /* HP ProBook 6550b */
4986         case 0x103c1520:
4987         case 0x103c1521:
4988         case 0x103c1523:
4989         case 0x103c1524:
4990         case 0x103c1525:
4991         case 0x103c1722:
4992         case 0x103c1723:
4993         case 0x103c1724:
4994         case 0x103c1725:
4995         case 0x103c1726:
4996         case 0x103c1727:
4997         case 0x103c1728:
4998         case 0x103c1729:
4999         case 0x103c172a:
5000         case 0x103c172b:
5001         case 0x103c307e:
5002         case 0x103c307f:
5003         case 0x103c3080:
5004         case 0x103c3081:
5005         case 0x103c7007:
5006         case 0x103c7008:
5007                 return 1;
5008         }
5009         return 0;
5010 }
5011
5012 #ifdef CONFIG_PROC_FS
5013 static void stac92hd_proc_hook(struct snd_info_buffer *buffer,
5014                                struct hda_codec *codec, hda_nid_t nid)
5015 {
5016         if (nid == codec->afg)
5017                 snd_iprintf(buffer, "Power-Map: 0x%02x\n", 
5018                             snd_hda_codec_read(codec, nid, 0, 0x0fec, 0x0));
5019 }
5020
5021 static void analog_loop_proc_hook(struct snd_info_buffer *buffer,
5022                                   struct hda_codec *codec,
5023                                   unsigned int verb)
5024 {
5025         snd_iprintf(buffer, "Analog Loopback: 0x%02x\n",
5026                     snd_hda_codec_read(codec, codec->afg, 0, verb, 0));
5027 }
5028
5029 /* stac92hd71bxx, stac92hd73xx */
5030 static void stac92hd7x_proc_hook(struct snd_info_buffer *buffer,
5031                                  struct hda_codec *codec, hda_nid_t nid)
5032 {
5033         stac92hd_proc_hook(buffer, codec, nid);
5034         if (nid == codec->afg)
5035                 analog_loop_proc_hook(buffer, codec, 0xfa0);
5036 }
5037
5038 static void stac9205_proc_hook(struct snd_info_buffer *buffer,
5039                                struct hda_codec *codec, hda_nid_t nid)
5040 {
5041         if (nid == codec->afg)
5042                 analog_loop_proc_hook(buffer, codec, 0xfe0);
5043 }
5044
5045 static void stac927x_proc_hook(struct snd_info_buffer *buffer,
5046                                struct hda_codec *codec, hda_nid_t nid)
5047 {
5048         if (nid == codec->afg)
5049                 analog_loop_proc_hook(buffer, codec, 0xfeb);
5050 }
5051 #else
5052 #define stac92hd_proc_hook      NULL
5053 #define stac92hd7x_proc_hook    NULL
5054 #define stac9205_proc_hook      NULL
5055 #define stac927x_proc_hook      NULL
5056 #endif
5057
5058 #ifdef CONFIG_PM
5059 static int stac92xx_resume(struct hda_codec *codec)
5060 {
5061         stac92xx_init(codec);
5062         snd_hda_codec_resume_amp(codec);
5063         snd_hda_codec_resume_cache(codec);
5064         /* fake event to set up pins again to override cached values */
5065         stac_fake_hp_events(codec);
5066         return 0;
5067 }
5068
5069 static int stac92xx_suspend(struct hda_codec *codec, pm_message_t state)
5070 {
5071         stac92xx_shutup(codec);
5072         return 0;
5073 }
5074
5075 #ifdef CONFIG_SND_HDA_POWER_SAVE
5076 static int stac92xx_pre_resume(struct hda_codec *codec)
5077 {
5078         struct sigmatel_spec *spec = codec->spec;
5079
5080         /* sync mute LED */
5081         if (spec->vref_mute_led_nid)
5082                 stac_vrefout_set(codec, spec->vref_mute_led_nid,
5083                                  spec->vref_led);
5084         else if (spec->gpio_led)
5085                 stac_gpio_set(codec, spec->gpio_mask,
5086                               spec->gpio_dir, spec->gpio_data);
5087         return 0;
5088 }
5089
5090 static void stac92xx_set_power_state(struct hda_codec *codec, hda_nid_t fg,
5091                                 unsigned int power_state)
5092 {
5093         unsigned int afg_power_state = power_state;
5094         struct sigmatel_spec *spec = codec->spec;
5095
5096         if (power_state == AC_PWRST_D3) {
5097                 if (spec->vref_mute_led_nid) {
5098                         /* with vref-out pin used for mute led control
5099                          * codec AFG is prevented from D3 state
5100                          */
5101                         afg_power_state = AC_PWRST_D1;
5102                 }
5103                 /* this delay seems necessary to avoid click noise at power-down */
5104                 msleep(100);
5105         }
5106         snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
5107                         afg_power_state);
5108         snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
5109 }
5110
5111 /*
5112  * For this feature CONFIG_SND_HDA_POWER_SAVE is needed
5113  * as mute LED state is updated in check_power_status hook
5114  */
5115 static int stac92xx_update_led_status(struct hda_codec *codec)
5116 {
5117         struct sigmatel_spec *spec = codec->spec;
5118         int i, num_ext_dacs, muted = 1;
5119         unsigned int muted_lvl, notmtd_lvl;
5120         hda_nid_t nid;
5121
5122         if (!spec->gpio_led)
5123                 return 0;
5124
5125         for (i = 0; i < spec->multiout.num_dacs; i++) {
5126                 nid = spec->multiout.dac_nids[i];
5127                 if (!(snd_hda_codec_amp_read(codec, nid, 0, HDA_OUTPUT, 0) &
5128                       HDA_AMP_MUTE)) {
5129                         muted = 0; /* something heard */
5130                         break;
5131                 }
5132         }
5133         if (muted && spec->multiout.hp_nid)
5134                 if (!(snd_hda_codec_amp_read(codec,
5135                                 spec->multiout.hp_nid, 0, HDA_OUTPUT, 0) &
5136                                         HDA_AMP_MUTE)) {
5137                         muted = 0; /* HP is not muted */
5138                 }
5139         num_ext_dacs = ARRAY_SIZE(spec->multiout.extra_out_nid);
5140         for (i = 0; muted && i < num_ext_dacs; i++) {
5141                 nid = spec->multiout.extra_out_nid[i];
5142                 if (nid == 0)
5143                         break;
5144                 if (!(snd_hda_codec_amp_read(codec, nid, 0, HDA_OUTPUT, 0) &
5145                       HDA_AMP_MUTE)) {
5146                         muted = 0; /* extra output is not muted */
5147                 }
5148         }
5149         /*polarity defines *not* muted state level*/
5150         if (!spec->vref_mute_led_nid) {
5151                 if (muted)
5152                         spec->gpio_data &= ~spec->gpio_led; /* orange */
5153                 else
5154                         spec->gpio_data |= spec->gpio_led; /* white */
5155
5156                 if (!spec->gpio_led_polarity) {
5157                         /* LED state is inverted on these systems */
5158                         spec->gpio_data ^= spec->gpio_led;
5159                 }
5160                 stac_gpio_set(codec, spec->gpio_mask,
5161                                 spec->gpio_dir, spec->gpio_data);
5162         } else {
5163                 notmtd_lvl = spec->gpio_led_polarity ?
5164                                 AC_PINCTL_VREF_HIZ : AC_PINCTL_VREF_GRD;
5165                 muted_lvl = spec->gpio_led_polarity ?
5166                                 AC_PINCTL_VREF_GRD : AC_PINCTL_VREF_HIZ;
5167                 spec->vref_led = muted ? muted_lvl : notmtd_lvl;
5168                 stac_vrefout_set(codec, spec->vref_mute_led_nid,
5169                                  spec->vref_led);
5170         }
5171         return 0;
5172 }
5173
5174 /*
5175  * use power check for controlling mute led of HP notebooks
5176  */
5177 static int stac92xx_check_power_status(struct hda_codec *codec,
5178                                               hda_nid_t nid)
5179 {
5180         stac92xx_update_led_status(codec);
5181
5182         return 0;
5183 }
5184 #endif /* CONFIG_SND_HDA_POWER_SAVE */
5185 #endif /* CONFIG_PM */
5186
5187 static const struct hda_codec_ops stac92xx_patch_ops = {
5188         .build_controls = stac92xx_build_controls,
5189         .build_pcms = stac92xx_build_pcms,
5190         .init = stac92xx_init,
5191         .free = stac92xx_free,
5192         .unsol_event = stac92xx_unsol_event,
5193 #ifdef CONFIG_PM
5194         .suspend = stac92xx_suspend,
5195         .resume = stac92xx_resume,
5196 #endif
5197         .reboot_notify = stac92xx_shutup,
5198 };
5199
5200 static int patch_stac9200(struct hda_codec *codec)
5201 {
5202         struct sigmatel_spec *spec;
5203         int err;
5204
5205         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
5206         if (spec == NULL)
5207                 return -ENOMEM;
5208
5209         codec->no_trigger_sense = 1;
5210         codec->spec = spec;
5211         spec->linear_tone_beep = 1;
5212         spec->num_pins = ARRAY_SIZE(stac9200_pin_nids);
5213         spec->pin_nids = stac9200_pin_nids;
5214         spec->board_config = snd_hda_check_board_config(codec, STAC_9200_MODELS,
5215                                                         stac9200_models,
5216                                                         stac9200_cfg_tbl);
5217         if (spec->board_config < 0)
5218                 snd_printdd(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
5219                             codec->chip_name);
5220         else
5221                 stac92xx_set_config_regs(codec,
5222                                          stac9200_brd_tbl[spec->board_config]);
5223
5224         spec->multiout.max_channels = 2;
5225         spec->multiout.num_dacs = 1;
5226         spec->multiout.dac_nids = stac9200_dac_nids;
5227         spec->adc_nids = stac9200_adc_nids;
5228         spec->mux_nids = stac9200_mux_nids;
5229         spec->num_muxes = 1;
5230         spec->num_dmics = 0;
5231         spec->num_adcs = 1;
5232         spec->num_pwrs = 0;
5233
5234         if (spec->board_config == STAC_9200_M4 ||
5235             spec->board_config == STAC_9200_M4_2 ||
5236             spec->board_config == STAC_9200_OQO)
5237                 spec->init = stac9200_eapd_init;
5238         else
5239                 spec->init = stac9200_core_init;
5240         spec->mixer = stac9200_mixer;
5241
5242         if (spec->board_config == STAC_9200_PANASONIC) {
5243                 spec->gpio_mask = spec->gpio_dir = 0x09;
5244                 spec->gpio_data = 0x00;
5245         }
5246
5247         err = stac9200_parse_auto_config(codec);
5248         if (err < 0) {
5249                 stac92xx_free(codec);
5250                 return err;
5251         }
5252
5253         /* CF-74 has no headphone detection, and the driver should *NOT*
5254          * do detection and HP/speaker toggle because the hardware does it.
5255          */
5256         if (spec->board_config == STAC_9200_PANASONIC)
5257                 spec->hp_detect = 0;
5258
5259         codec->patch_ops = stac92xx_patch_ops;
5260
5261         return 0;
5262 }
5263
5264 static int patch_stac925x(struct hda_codec *codec)
5265 {
5266         struct sigmatel_spec *spec;
5267         int err;
5268
5269         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
5270         if (spec == NULL)
5271                 return -ENOMEM;
5272
5273         codec->no_trigger_sense = 1;
5274         codec->spec = spec;
5275         spec->linear_tone_beep = 1;
5276         spec->num_pins = ARRAY_SIZE(stac925x_pin_nids);
5277         spec->pin_nids = stac925x_pin_nids;
5278
5279         /* Check first for codec ID */
5280         spec->board_config = snd_hda_check_board_codec_sid_config(codec,
5281                                                         STAC_925x_MODELS,
5282                                                         stac925x_models,
5283                                                         stac925x_codec_id_cfg_tbl);
5284
5285         /* Now checks for PCI ID, if codec ID is not found */
5286         if (spec->board_config < 0)
5287                 spec->board_config = snd_hda_check_board_config(codec,
5288                                                         STAC_925x_MODELS,
5289                                                         stac925x_models,
5290                                                         stac925x_cfg_tbl);
5291  again:
5292         if (spec->board_config < 0)
5293                 snd_printdd(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
5294                             codec->chip_name);
5295         else
5296                 stac92xx_set_config_regs(codec,
5297                                          stac925x_brd_tbl[spec->board_config]);
5298
5299         spec->multiout.max_channels = 2;
5300         spec->multiout.num_dacs = 1;
5301         spec->multiout.dac_nids = stac925x_dac_nids;
5302         spec->adc_nids = stac925x_adc_nids;
5303         spec->mux_nids = stac925x_mux_nids;
5304         spec->num_muxes = 1;
5305         spec->num_adcs = 1;
5306         spec->num_pwrs = 0;
5307         switch (codec->vendor_id) {
5308         case 0x83847632: /* STAC9202  */
5309         case 0x83847633: /* STAC9202D */
5310         case 0x83847636: /* STAC9251  */
5311         case 0x83847637: /* STAC9251D */
5312                 spec->num_dmics = STAC925X_NUM_DMICS;
5313                 spec->dmic_nids = stac925x_dmic_nids;
5314                 spec->num_dmuxes = ARRAY_SIZE(stac925x_dmux_nids);
5315                 spec->dmux_nids = stac925x_dmux_nids;
5316                 break;
5317         default:
5318                 spec->num_dmics = 0;
5319                 break;
5320         }
5321
5322         spec->init = stac925x_core_init;
5323         spec->mixer = stac925x_mixer;
5324         spec->num_caps = 1;
5325         spec->capvols = stac925x_capvols;
5326         spec->capsws = stac925x_capsws;
5327
5328         err = stac92xx_parse_auto_config(codec);
5329         if (!err) {
5330                 if (spec->board_config < 0) {
5331                         printk(KERN_WARNING "hda_codec: No auto-config is "
5332                                "available, default to model=ref\n");
5333                         spec->board_config = STAC_925x_REF;
5334                         goto again;
5335                 }
5336                 err = -EINVAL;
5337         }
5338         if (err < 0) {
5339                 stac92xx_free(codec);
5340                 return err;
5341         }
5342
5343         codec->patch_ops = stac92xx_patch_ops;
5344
5345         return 0;
5346 }
5347
5348 static int patch_stac92hd73xx(struct hda_codec *codec)
5349 {
5350         struct sigmatel_spec *spec;
5351         hda_nid_t conn[STAC92HD73_DAC_COUNT + 2];
5352         int err = 0;
5353         int num_dacs;
5354
5355         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
5356         if (spec == NULL)
5357                 return -ENOMEM;
5358
5359         codec->no_trigger_sense = 1;
5360         codec->spec = spec;
5361         spec->linear_tone_beep = 0;
5362         codec->slave_dig_outs = stac92hd73xx_slave_dig_outs;
5363         spec->num_pins = ARRAY_SIZE(stac92hd73xx_pin_nids);
5364         spec->pin_nids = stac92hd73xx_pin_nids;
5365         spec->board_config = snd_hda_check_board_config(codec,
5366                                                         STAC_92HD73XX_MODELS,
5367                                                         stac92hd73xx_models,
5368                                                         stac92hd73xx_cfg_tbl);
5369         /* check codec subsystem id if not found */
5370         if (spec->board_config < 0)
5371                 spec->board_config =
5372                         snd_hda_check_board_codec_sid_config(codec,
5373                                 STAC_92HD73XX_MODELS, stac92hd73xx_models,
5374                                 stac92hd73xx_codec_id_cfg_tbl);
5375 again:
5376         if (spec->board_config < 0)
5377                 snd_printdd(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
5378                             codec->chip_name);
5379         else
5380                 stac92xx_set_config_regs(codec,
5381                                 stac92hd73xx_brd_tbl[spec->board_config]);
5382
5383         num_dacs = snd_hda_get_connections(codec, 0x0a,
5384                         conn, STAC92HD73_DAC_COUNT + 2) - 1;
5385
5386         if (num_dacs < 3 || num_dacs > 5) {
5387                 printk(KERN_WARNING "hda_codec: Could not determine "
5388                        "number of channels defaulting to DAC count\n");
5389                 num_dacs = STAC92HD73_DAC_COUNT;
5390         }
5391         spec->init = stac92hd73xx_core_init;
5392         switch (num_dacs) {
5393         case 0x3: /* 6 Channel */
5394                 spec->aloopback_ctl = stac92hd73xx_6ch_loopback;
5395                 break;
5396         case 0x4: /* 8 Channel */
5397                 spec->aloopback_ctl = stac92hd73xx_8ch_loopback;
5398                 break;
5399         case 0x5: /* 10 Channel */
5400                 spec->aloopback_ctl = stac92hd73xx_10ch_loopback;
5401                 break;
5402         }
5403         spec->multiout.dac_nids = spec->dac_nids;
5404
5405         spec->aloopback_mask = 0x01;
5406         spec->aloopback_shift = 8;
5407
5408         spec->digbeep_nid = 0x1c;
5409         spec->mux_nids = stac92hd73xx_mux_nids;
5410         spec->adc_nids = stac92hd73xx_adc_nids;
5411         spec->dmic_nids = stac92hd73xx_dmic_nids;
5412         spec->dmux_nids = stac92hd73xx_dmux_nids;
5413         spec->smux_nids = stac92hd73xx_smux_nids;
5414
5415         spec->num_muxes = ARRAY_SIZE(stac92hd73xx_mux_nids);
5416         spec->num_adcs = ARRAY_SIZE(stac92hd73xx_adc_nids);
5417         spec->num_dmuxes = ARRAY_SIZE(stac92hd73xx_dmux_nids);
5418
5419         spec->num_caps = STAC92HD73XX_NUM_CAPS;
5420         spec->capvols = stac92hd73xx_capvols;
5421         spec->capsws = stac92hd73xx_capsws;
5422
5423         switch (spec->board_config) {
5424         case STAC_DELL_EQ:
5425                 spec->init = dell_eq_core_init;
5426                 /* fallthru */
5427         case STAC_DELL_M6_AMIC:
5428         case STAC_DELL_M6_DMIC:
5429         case STAC_DELL_M6_BOTH:
5430                 spec->num_smuxes = 0;
5431                 spec->eapd_switch = 0;
5432
5433                 switch (spec->board_config) {
5434                 case STAC_DELL_M6_AMIC: /* Analog Mics */
5435                         snd_hda_codec_set_pincfg(codec, 0x0b, 0x90A70170);
5436                         spec->num_dmics = 0;
5437                         break;
5438                 case STAC_DELL_M6_DMIC: /* Digital Mics */
5439                         snd_hda_codec_set_pincfg(codec, 0x13, 0x90A60160);
5440                         spec->num_dmics = 1;
5441                         break;
5442                 case STAC_DELL_M6_BOTH: /* Both */
5443                         snd_hda_codec_set_pincfg(codec, 0x0b, 0x90A70170);
5444                         snd_hda_codec_set_pincfg(codec, 0x13, 0x90A60160);
5445                         spec->num_dmics = 1;
5446                         break;
5447                 }
5448                 break;
5449         case STAC_ALIENWARE_M17X:
5450                 spec->num_dmics = STAC92HD73XX_NUM_DMICS;
5451                 spec->num_smuxes = ARRAY_SIZE(stac92hd73xx_smux_nids);
5452                 spec->eapd_switch = 0;
5453                 break;
5454         default:
5455                 spec->num_dmics = STAC92HD73XX_NUM_DMICS;
5456                 spec->num_smuxes = ARRAY_SIZE(stac92hd73xx_smux_nids);
5457                 spec->eapd_switch = 1;
5458                 break;
5459         }
5460         if (spec->board_config != STAC_92HD73XX_REF) {
5461                 /* GPIO0 High = Enable EAPD */
5462                 spec->eapd_mask = spec->gpio_mask = spec->gpio_dir = 0x1;
5463                 spec->gpio_data = 0x01;
5464         }
5465
5466         spec->num_pwrs = ARRAY_SIZE(stac92hd73xx_pwr_nids);
5467         spec->pwr_nids = stac92hd73xx_pwr_nids;
5468
5469         err = stac92xx_parse_auto_config(codec);
5470
5471         if (!err) {
5472                 if (spec->board_config < 0) {
5473                         printk(KERN_WARNING "hda_codec: No auto-config is "
5474                                "available, default to model=ref\n");
5475                         spec->board_config = STAC_92HD73XX_REF;
5476                         goto again;
5477                 }
5478                 err = -EINVAL;
5479         }
5480
5481         if (err < 0) {
5482                 stac92xx_free(codec);
5483                 return err;
5484         }
5485
5486         if (spec->board_config == STAC_92HD73XX_NO_JD)
5487                 spec->hp_detect = 0;
5488
5489         codec->patch_ops = stac92xx_patch_ops;
5490
5491         codec->proc_widget_hook = stac92hd7x_proc_hook;
5492
5493         return 0;
5494 }
5495
5496 static int hp_bnb2011_with_dock(struct hda_codec *codec)
5497 {
5498         if (codec->vendor_id != 0x111d7605 &&
5499             codec->vendor_id != 0x111d76d1)
5500                 return 0;
5501
5502         switch (codec->subsystem_id) {
5503         case 0x103c1618:
5504         case 0x103c1619:
5505         case 0x103c161a:
5506         case 0x103c161b:
5507         case 0x103c161c:
5508         case 0x103c161d:
5509         case 0x103c161e:
5510         case 0x103c161f:
5511
5512         case 0x103c162a:
5513         case 0x103c162b:
5514
5515         case 0x103c1630:
5516         case 0x103c1631:
5517
5518         case 0x103c1633:
5519         case 0x103c1634:
5520         case 0x103c1635:
5521
5522         case 0x103c3587:
5523         case 0x103c3588:
5524         case 0x103c3589:
5525         case 0x103c358a:
5526
5527         case 0x103c3667:
5528         case 0x103c3668:
5529         case 0x103c3669:
5530
5531                 return 1;
5532         }
5533         return 0;
5534 }
5535
5536 static void stac92hd8x_add_pin(struct hda_codec *codec, hda_nid_t nid)
5537 {
5538         struct sigmatel_spec *spec = codec->spec;
5539         unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
5540         int i;
5541
5542         spec->auto_pin_nids[spec->auto_pin_cnt] = nid;
5543         spec->auto_pin_cnt++;
5544
5545         if (get_defcfg_device(def_conf) == AC_JACK_MIC_IN &&
5546             get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE) {
5547                 for (i = 0; i < ARRAY_SIZE(stac92hd83xxx_dmic_nids); i++) {
5548                         if (nid == stac92hd83xxx_dmic_nids[i]) {
5549                                 spec->auto_dmic_nids[spec->auto_dmic_cnt] = nid;
5550                                 spec->auto_dmic_cnt++;
5551                         }
5552                 }
5553         }
5554 }
5555
5556 static void stac92hd8x_add_adc(struct hda_codec *codec, hda_nid_t nid)
5557 {
5558         struct sigmatel_spec *spec = codec->spec;
5559
5560         spec->auto_adc_nids[spec->auto_adc_cnt] = nid;
5561         spec->auto_adc_cnt++;
5562 }
5563
5564 static void stac92hd8x_add_mux(struct hda_codec *codec, hda_nid_t nid)
5565 {
5566         int i, j;
5567         struct sigmatel_spec *spec = codec->spec;
5568
5569         for (i = 0; i < spec->auto_adc_cnt; i++) {
5570                 if (get_connection_index(codec,
5571                                 spec->auto_adc_nids[i], nid) >= 0) {
5572                         /* mux and volume for adc_nids[i] */
5573                         if (!spec->auto_mux_nids[i]) {
5574                                 spec->auto_mux_nids[i] = nid;
5575                                 /* 92hd codecs capture volume is in mux */
5576                                 spec->auto_capvols[i] = HDA_COMPOSE_AMP_VAL(nid,
5577                                                         3, 0, HDA_OUTPUT);
5578                         }
5579                         for (j = 0; j < spec->auto_dmic_cnt; j++) {
5580                                 if (get_connection_index(codec, nid,
5581                                                 spec->auto_dmic_nids[j]) >= 0) {
5582                                         /* dmux for adc_nids[i] */
5583                                         if (!spec->auto_dmux_nids[i])
5584                                                 spec->auto_dmux_nids[i] = nid;
5585                                         break;
5586                                 }
5587                         }
5588                         break;
5589                 }
5590         }
5591 }
5592
5593 static void stac92hd8x_fill_auto_spec(struct hda_codec *codec)
5594 {
5595         hda_nid_t nid, end_nid;
5596         unsigned int wid_caps, wid_type;
5597         struct sigmatel_spec *spec = codec->spec;
5598
5599         end_nid = codec->start_nid + codec->num_nodes;
5600
5601         for (nid = codec->start_nid; nid < end_nid; nid++) {
5602                 wid_caps = get_wcaps(codec, nid);
5603                 wid_type = get_wcaps_type(wid_caps);
5604
5605                 if (wid_type == AC_WID_PIN)
5606                         stac92hd8x_add_pin(codec, nid);
5607
5608                 if (wid_type == AC_WID_AUD_IN && !(wid_caps & AC_WCAP_DIGITAL))
5609                         stac92hd8x_add_adc(codec, nid);
5610         }
5611
5612         for (nid = codec->start_nid; nid < end_nid; nid++) {
5613                 wid_caps = get_wcaps(codec, nid);
5614                 wid_type = get_wcaps_type(wid_caps);
5615
5616                 if (wid_type == AC_WID_AUD_SEL)
5617                         stac92hd8x_add_mux(codec, nid);
5618         }
5619
5620         spec->pin_nids = spec->auto_pin_nids;
5621         spec->num_pins = spec->auto_pin_cnt;
5622         spec->adc_nids = spec->auto_adc_nids;
5623         spec->num_adcs = spec->auto_adc_cnt;
5624         spec->capvols = spec->auto_capvols;
5625         spec->capsws = spec->auto_capvols;
5626         spec->num_caps = spec->auto_adc_cnt;
5627         spec->mux_nids = spec->auto_mux_nids;
5628         spec->num_muxes = spec->auto_adc_cnt;
5629         spec->dmux_nids = spec->auto_dmux_nids;
5630         spec->num_dmuxes = spec->auto_adc_cnt;
5631         spec->dmic_nids = spec->auto_dmic_nids;
5632         spec->num_dmics = spec->auto_dmic_cnt;
5633 }
5634
5635 static int patch_stac92hd83xxx(struct hda_codec *codec)
5636 {
5637         struct sigmatel_spec *spec;
5638         int err;
5639
5640         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
5641         if (spec == NULL)
5642                 return -ENOMEM;
5643
5644         if (hp_bnb2011_with_dock(codec)) {
5645                 snd_hda_codec_set_pincfg(codec, 0xa, 0x2101201f);
5646                 snd_hda_codec_set_pincfg(codec, 0xf, 0x2181205e);
5647         }
5648
5649         codec->no_trigger_sense = 1;
5650         codec->spec = spec;
5651
5652         stac92hd8x_fill_auto_spec(codec);
5653
5654         spec->linear_tone_beep = 0;
5655         codec->slave_dig_outs = stac92hd83xxx_slave_dig_outs;
5656         spec->digbeep_nid = 0x21;
5657         spec->pwr_nids = stac92hd83xxx_pwr_nids;
5658         spec->num_pwrs = ARRAY_SIZE(stac92hd83xxx_pwr_nids);
5659         spec->multiout.dac_nids = spec->dac_nids;
5660         spec->init = stac92hd83xxx_core_init;
5661
5662         spec->board_config = snd_hda_check_board_config(codec,
5663                                                         STAC_92HD83XXX_MODELS,
5664                                                         stac92hd83xxx_models,
5665                                                         stac92hd83xxx_cfg_tbl);
5666 again:
5667         if (spec->board_config < 0)
5668                 snd_printdd(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
5669                             codec->chip_name);
5670         else
5671                 stac92xx_set_config_regs(codec,
5672                                 stac92hd83xxx_brd_tbl[spec->board_config]);
5673
5674         codec->patch_ops = stac92xx_patch_ops;
5675
5676         if (find_mute_led_gpio(codec, 0))
5677                 snd_printd("mute LED gpio %d polarity %d\n",
5678                                 spec->gpio_led,
5679                                 spec->gpio_led_polarity);
5680
5681 #ifdef CONFIG_SND_HDA_POWER_SAVE
5682         if (spec->gpio_led) {
5683                 if (!spec->vref_mute_led_nid) {
5684                         spec->gpio_mask |= spec->gpio_led;
5685                         spec->gpio_dir |= spec->gpio_led;
5686                         spec->gpio_data |= spec->gpio_led;
5687                 } else {
5688                         codec->patch_ops.set_power_state =
5689                                         stac92xx_set_power_state;
5690                 }
5691                 codec->patch_ops.pre_resume = stac92xx_pre_resume;
5692                 codec->patch_ops.check_power_status =
5693                         stac92xx_check_power_status;
5694         }
5695 #endif  
5696
5697         err = stac92xx_parse_auto_config(codec);
5698         if (!err) {
5699                 if (spec->board_config < 0) {
5700                         printk(KERN_WARNING "hda_codec: No auto-config is "
5701                                "available, default to model=ref\n");
5702                         spec->board_config = STAC_92HD83XXX_REF;
5703                         goto again;
5704                 }
5705                 err = -EINVAL;
5706         }
5707
5708         if (err < 0) {
5709                 stac92xx_free(codec);
5710                 return err;
5711         }
5712
5713         codec->proc_widget_hook = stac92hd_proc_hook;
5714
5715         return 0;
5716 }
5717
5718 static int stac92hd71bxx_connected_smuxes(struct hda_codec *codec,
5719                                           hda_nid_t dig0pin)
5720 {
5721         struct sigmatel_spec *spec = codec->spec;
5722         int idx;
5723
5724         for (idx = 0; idx < spec->num_pins; idx++)
5725                 if (spec->pin_nids[idx] == dig0pin)
5726                         break;
5727         if ((idx + 2) >= spec->num_pins)
5728                 return 0;
5729
5730         /* dig1pin case */
5731         if (stac_get_defcfg_connect(codec, idx + 1) != AC_JACK_PORT_NONE)
5732                 return 2;
5733
5734         /* dig0pin + dig2pin case */
5735         if (stac_get_defcfg_connect(codec, idx + 2) != AC_JACK_PORT_NONE)
5736                 return 2;
5737         if (stac_get_defcfg_connect(codec, idx) != AC_JACK_PORT_NONE)
5738                 return 1;
5739         else
5740                 return 0;
5741 }
5742
5743 /* HP dv7 bass switch - GPIO5 */
5744 #define stac_hp_bass_gpio_info  snd_ctl_boolean_mono_info
5745 static int stac_hp_bass_gpio_get(struct snd_kcontrol *kcontrol,
5746                                  struct snd_ctl_elem_value *ucontrol)
5747 {
5748         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5749         struct sigmatel_spec *spec = codec->spec;
5750         ucontrol->value.integer.value[0] = !!(spec->gpio_data & 0x20);
5751         return 0;
5752 }
5753
5754 static int stac_hp_bass_gpio_put(struct snd_kcontrol *kcontrol,
5755                                  struct snd_ctl_elem_value *ucontrol)
5756 {
5757         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
5758         struct sigmatel_spec *spec = codec->spec;
5759         unsigned int gpio_data;
5760
5761         gpio_data = (spec->gpio_data & ~0x20) |
5762                 (ucontrol->value.integer.value[0] ? 0x20 : 0);
5763         if (gpio_data == spec->gpio_data)
5764                 return 0;
5765         spec->gpio_data = gpio_data;
5766         stac_gpio_set(codec, spec->gpio_mask, spec->gpio_dir, spec->gpio_data);
5767         return 1;
5768 }
5769
5770 static const struct snd_kcontrol_new stac_hp_bass_sw_ctrl = {
5771         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
5772         .info = stac_hp_bass_gpio_info,
5773         .get = stac_hp_bass_gpio_get,
5774         .put = stac_hp_bass_gpio_put,
5775 };
5776
5777 static int stac_add_hp_bass_switch(struct hda_codec *codec)
5778 {
5779         struct sigmatel_spec *spec = codec->spec;
5780
5781         if (!stac_control_new(spec, &stac_hp_bass_sw_ctrl,
5782                               "Bass Speaker Playback Switch", 0))
5783                 return -ENOMEM;
5784
5785         spec->gpio_mask |= 0x20;
5786         spec->gpio_dir |= 0x20;
5787         spec->gpio_data |= 0x20;
5788         return 0;
5789 }
5790
5791 static int patch_stac92hd71bxx(struct hda_codec *codec)
5792 {
5793         struct sigmatel_spec *spec;
5794         const struct hda_verb *unmute_init = stac92hd71bxx_unmute_core_init;
5795         unsigned int pin_cfg;
5796         int err = 0;
5797
5798         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
5799         if (spec == NULL)
5800                 return -ENOMEM;
5801
5802         codec->no_trigger_sense = 1;
5803         codec->spec = spec;
5804         spec->linear_tone_beep = 0;
5805         codec->patch_ops = stac92xx_patch_ops;
5806         spec->num_pins = STAC92HD71BXX_NUM_PINS;
5807         switch (codec->vendor_id) {
5808         case 0x111d76b6:
5809         case 0x111d76b7:
5810                 spec->pin_nids = stac92hd71bxx_pin_nids_4port;
5811                 break;
5812         case 0x111d7603:
5813         case 0x111d7608:
5814                 /* On 92HD75Bx 0x27 isn't a pin nid */
5815                 spec->num_pins--;
5816                 /* fallthrough */
5817         default:
5818                 spec->pin_nids = stac92hd71bxx_pin_nids_6port;
5819         }
5820         spec->num_pwrs = ARRAY_SIZE(stac92hd71bxx_pwr_nids);
5821         spec->board_config = snd_hda_check_board_config(codec,
5822                                                         STAC_92HD71BXX_MODELS,
5823                                                         stac92hd71bxx_models,
5824                                                         stac92hd71bxx_cfg_tbl);
5825 again:
5826         if (spec->board_config < 0)
5827                 snd_printdd(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
5828                             codec->chip_name);
5829         else
5830                 stac92xx_set_config_regs(codec,
5831                                 stac92hd71bxx_brd_tbl[spec->board_config]);
5832
5833         if (spec->board_config != STAC_92HD71BXX_REF) {
5834                 /* GPIO0 = EAPD */
5835                 spec->gpio_mask = 0x01;
5836                 spec->gpio_dir = 0x01;
5837                 spec->gpio_data = 0x01;
5838         }
5839
5840         spec->dmic_nids = stac92hd71bxx_dmic_nids;
5841         spec->dmux_nids = stac92hd71bxx_dmux_nids;
5842
5843         spec->num_caps = STAC92HD71BXX_NUM_CAPS;
5844         spec->capvols = stac92hd71bxx_capvols;
5845         spec->capsws = stac92hd71bxx_capsws;
5846
5847         switch (codec->vendor_id) {
5848         case 0x111d76b6: /* 4 Port without Analog Mixer */
5849         case 0x111d76b7:
5850                 unmute_init++;
5851                 /* fallthru */
5852         case 0x111d76b4: /* 6 Port without Analog Mixer */
5853         case 0x111d76b5:
5854                 spec->init = stac92hd71bxx_core_init;
5855                 codec->slave_dig_outs = stac92hd71bxx_slave_dig_outs;
5856                 spec->num_dmics = stac92xx_connected_ports(codec,
5857                                         stac92hd71bxx_dmic_nids,
5858                                         STAC92HD71BXX_NUM_DMICS);
5859                 break;
5860         case 0x111d7608: /* 5 Port with Analog Mixer */
5861                 switch (spec->board_config) {
5862                 case STAC_HP_M4:
5863                         /* Enable VREF power saving on GPIO1 detect */
5864                         err = stac_add_event(spec, codec->afg,
5865                                              STAC_VREF_EVENT, 0x02);
5866                         if (err < 0)
5867                                 return err;
5868                         snd_hda_codec_write_cache(codec, codec->afg, 0,
5869                                 AC_VERB_SET_GPIO_UNSOLICITED_RSP_MASK, 0x02);
5870                         snd_hda_codec_write_cache(codec, codec->afg, 0,
5871                                 AC_VERB_SET_UNSOLICITED_ENABLE,
5872                                 AC_USRSP_EN | err);
5873                         spec->gpio_mask |= 0x02;
5874                         break;
5875                 }
5876                 if ((codec->revision_id & 0xf) == 0 ||
5877                     (codec->revision_id & 0xf) == 1)
5878                         spec->stream_delay = 40; /* 40 milliseconds */
5879
5880                 /* disable VSW */
5881                 spec->init = stac92hd71bxx_core_init;
5882                 unmute_init++;
5883                 snd_hda_codec_set_pincfg(codec, 0x0f, 0x40f000f0);
5884                 snd_hda_codec_set_pincfg(codec, 0x19, 0x40f000f3);
5885                 spec->dmic_nids = stac92hd71bxx_dmic_5port_nids;
5886                 spec->num_dmics = stac92xx_connected_ports(codec,
5887                                         stac92hd71bxx_dmic_5port_nids,
5888                                         STAC92HD71BXX_NUM_DMICS - 1);
5889                 break;
5890         case 0x111d7603: /* 6 Port with Analog Mixer */
5891                 if ((codec->revision_id & 0xf) == 1)
5892                         spec->stream_delay = 40; /* 40 milliseconds */
5893
5894                 /* fallthru */
5895         default:
5896                 spec->init = stac92hd71bxx_core_init;
5897                 codec->slave_dig_outs = stac92hd71bxx_slave_dig_outs;
5898                 spec->num_dmics = stac92xx_connected_ports(codec,
5899                                         stac92hd71bxx_dmic_nids,
5900                                         STAC92HD71BXX_NUM_DMICS);
5901                 break;
5902         }
5903
5904         if (get_wcaps(codec, 0xa) & AC_WCAP_IN_AMP)
5905                 snd_hda_sequence_write_cache(codec, unmute_init);
5906
5907         spec->aloopback_ctl = stac92hd71bxx_loopback;
5908         spec->aloopback_mask = 0x50;
5909         spec->aloopback_shift = 0;
5910
5911         spec->powerdown_adcs = 1;
5912         spec->digbeep_nid = 0x26;
5913         spec->mux_nids = stac92hd71bxx_mux_nids;
5914         spec->adc_nids = stac92hd71bxx_adc_nids;
5915         spec->smux_nids = stac92hd71bxx_smux_nids;
5916         spec->pwr_nids = stac92hd71bxx_pwr_nids;
5917
5918         spec->num_muxes = ARRAY_SIZE(stac92hd71bxx_mux_nids);
5919         spec->num_adcs = ARRAY_SIZE(stac92hd71bxx_adc_nids);
5920         spec->num_dmuxes = ARRAY_SIZE(stac92hd71bxx_dmux_nids);
5921         spec->num_smuxes = stac92hd71bxx_connected_smuxes(codec, 0x1e);
5922
5923         snd_printdd("Found board config: %d\n", spec->board_config);
5924
5925         switch (spec->board_config) {
5926         case STAC_HP_M4:
5927                 /* enable internal microphone */
5928                 snd_hda_codec_set_pincfg(codec, 0x0e, 0x01813040);
5929                 stac92xx_auto_set_pinctl(codec, 0x0e,
5930                         AC_PINCTL_IN_EN | AC_PINCTL_VREF_80);
5931                 /* fallthru */
5932         case STAC_DELL_M4_2:
5933                 spec->num_dmics = 0;
5934                 spec->num_smuxes = 0;
5935                 spec->num_dmuxes = 0;
5936                 break;
5937         case STAC_DELL_M4_1:
5938         case STAC_DELL_M4_3:
5939                 spec->num_dmics = 1;
5940                 spec->num_smuxes = 0;
5941                 spec->num_dmuxes = 1;
5942                 break;
5943         case STAC_HP_DV4_1222NR:
5944                 spec->num_dmics = 1;
5945                 /* I don't know if it needs 1 or 2 smuxes - will wait for
5946                  * bug reports to fix if needed
5947                  */
5948                 spec->num_smuxes = 1;
5949                 spec->num_dmuxes = 1;
5950                 /* fallthrough */
5951         case STAC_HP_DV4:
5952                 spec->gpio_led = 0x01;
5953                 /* fallthrough */
5954         case STAC_HP_DV5:
5955                 snd_hda_codec_set_pincfg(codec, 0x0d, 0x90170010);
5956                 stac92xx_auto_set_pinctl(codec, 0x0d, AC_PINCTL_OUT_EN);
5957                 /* HP dv6 gives the headphone pin as a line-out.  Thus we
5958                  * need to set hp_detect flag here to force to enable HP
5959                  * detection.
5960                  */
5961                 spec->hp_detect = 1;
5962                 break;
5963         case STAC_HP_HDX:
5964                 spec->num_dmics = 1;
5965                 spec->num_dmuxes = 1;
5966                 spec->num_smuxes = 1;
5967                 spec->gpio_led = 0x08;
5968                 break;
5969         }
5970
5971         if (hp_blike_system(codec->subsystem_id)) {
5972                 pin_cfg = snd_hda_codec_get_pincfg(codec, 0x0f);
5973                 if (get_defcfg_device(pin_cfg) == AC_JACK_LINE_OUT ||
5974                         get_defcfg_device(pin_cfg) == AC_JACK_SPEAKER  ||
5975                         get_defcfg_device(pin_cfg) == AC_JACK_HP_OUT) {
5976                         /* It was changed in the BIOS to just satisfy MS DTM.
5977                          * Lets turn it back into slaved HP
5978                          */
5979                         pin_cfg = (pin_cfg & (~AC_DEFCFG_DEVICE))
5980                                         | (AC_JACK_HP_OUT <<
5981                                                 AC_DEFCFG_DEVICE_SHIFT);
5982                         pin_cfg = (pin_cfg & (~(AC_DEFCFG_DEF_ASSOC
5983                                                         | AC_DEFCFG_SEQUENCE)))
5984                                                                 | 0x1f;
5985                         snd_hda_codec_set_pincfg(codec, 0x0f, pin_cfg);
5986                 }
5987         }
5988
5989         if (find_mute_led_gpio(codec, 1))
5990                 snd_printd("mute LED gpio %d polarity %d\n",
5991                                 spec->gpio_led,
5992                                 spec->gpio_led_polarity);
5993
5994 #ifdef CONFIG_SND_HDA_POWER_SAVE
5995         if (spec->gpio_led) {
5996                 if (!spec->vref_mute_led_nid) {
5997                         spec->gpio_mask |= spec->gpio_led;
5998                         spec->gpio_dir |= spec->gpio_led;
5999                         spec->gpio_data |= spec->gpio_led;
6000                 } else {
6001                         codec->patch_ops.set_power_state =
6002                                         stac92xx_set_power_state;
6003                 }
6004                 codec->patch_ops.pre_resume = stac92xx_pre_resume;
6005                 codec->patch_ops.check_power_status =
6006                         stac92xx_check_power_status;
6007         }
6008 #endif  
6009
6010         spec->multiout.dac_nids = spec->dac_nids;
6011
6012         err = stac92xx_parse_auto_config(codec);
6013         if (!err) {
6014                 if (spec->board_config < 0) {
6015                         printk(KERN_WARNING "hda_codec: No auto-config is "
6016                                "available, default to model=ref\n");
6017                         spec->board_config = STAC_92HD71BXX_REF;
6018                         goto again;
6019                 }
6020                 err = -EINVAL;
6021         }
6022
6023         if (err < 0) {
6024                 stac92xx_free(codec);
6025                 return err;
6026         }
6027
6028         /* enable bass on HP dv7 */
6029         if (spec->board_config == STAC_HP_DV4 ||
6030             spec->board_config == STAC_HP_DV5) {
6031                 unsigned int cap;
6032                 cap = snd_hda_param_read(codec, 0x1, AC_PAR_GPIO_CAP);
6033                 cap &= AC_GPIO_IO_COUNT;
6034                 if (cap >= 6)
6035                         stac_add_hp_bass_switch(codec);
6036         }
6037
6038         codec->proc_widget_hook = stac92hd7x_proc_hook;
6039
6040         return 0;
6041 }
6042
6043 static int patch_stac922x(struct hda_codec *codec)
6044 {
6045         struct sigmatel_spec *spec;
6046         int err;
6047
6048         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
6049         if (spec == NULL)
6050                 return -ENOMEM;
6051
6052         codec->no_trigger_sense = 1;
6053         codec->spec = spec;
6054         spec->linear_tone_beep = 1;
6055         spec->num_pins = ARRAY_SIZE(stac922x_pin_nids);
6056         spec->pin_nids = stac922x_pin_nids;
6057         spec->board_config = snd_hda_check_board_config(codec, STAC_922X_MODELS,
6058                                                         stac922x_models,
6059                                                         stac922x_cfg_tbl);
6060         if (spec->board_config == STAC_INTEL_MAC_AUTO) {
6061                 spec->gpio_mask = spec->gpio_dir = 0x03;
6062                 spec->gpio_data = 0x03;
6063                 /* Intel Macs have all same PCI SSID, so we need to check
6064                  * codec SSID to distinguish the exact models
6065                  */
6066                 printk(KERN_INFO "hda_codec: STAC922x, Apple subsys_id=%x\n", codec->subsystem_id);
6067                 switch (codec->subsystem_id) {
6068
6069                 case 0x106b0800:
6070                         spec->board_config = STAC_INTEL_MAC_V1;
6071                         break;
6072                 case 0x106b0600:
6073                 case 0x106b0700:
6074                         spec->board_config = STAC_INTEL_MAC_V2;
6075                         break;
6076                 case 0x106b0e00:
6077                 case 0x106b0f00:
6078                 case 0x106b1600:
6079                 case 0x106b1700:
6080                 case 0x106b0200:
6081                 case 0x106b1e00:
6082                         spec->board_config = STAC_INTEL_MAC_V3;
6083                         break;
6084                 case 0x106b1a00:
6085                 case 0x00000100:
6086                         spec->board_config = STAC_INTEL_MAC_V4;
6087                         break;
6088                 case 0x106b0a00:
6089                 case 0x106b2200:
6090                         spec->board_config = STAC_INTEL_MAC_V5;
6091                         break;
6092                 default:
6093                         spec->board_config = STAC_INTEL_MAC_V3;
6094                         break;
6095                 }
6096         }
6097
6098  again:
6099         if (spec->board_config < 0)
6100                 snd_printdd(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
6101                             codec->chip_name);
6102         else
6103                 stac92xx_set_config_regs(codec,
6104                                 stac922x_brd_tbl[spec->board_config]);
6105
6106         spec->adc_nids = stac922x_adc_nids;
6107         spec->mux_nids = stac922x_mux_nids;
6108         spec->num_muxes = ARRAY_SIZE(stac922x_mux_nids);
6109         spec->num_adcs = ARRAY_SIZE(stac922x_adc_nids);
6110         spec->num_dmics = 0;
6111         spec->num_pwrs = 0;
6112
6113         spec->init = stac922x_core_init;
6114
6115         spec->num_caps = STAC922X_NUM_CAPS;
6116         spec->capvols = stac922x_capvols;
6117         spec->capsws = stac922x_capsws;
6118
6119         spec->multiout.dac_nids = spec->dac_nids;
6120         
6121         err = stac92xx_parse_auto_config(codec);
6122         if (!err) {
6123                 if (spec->board_config < 0) {
6124                         printk(KERN_WARNING "hda_codec: No auto-config is "
6125                                "available, default to model=ref\n");
6126                         spec->board_config = STAC_D945_REF;
6127                         goto again;
6128                 }
6129                 err = -EINVAL;
6130         }
6131         if (err < 0) {
6132                 stac92xx_free(codec);
6133                 return err;
6134         }
6135
6136         codec->patch_ops = stac92xx_patch_ops;
6137
6138         /* Fix Mux capture level; max to 2 */
6139         snd_hda_override_amp_caps(codec, 0x12, HDA_OUTPUT,
6140                                   (0 << AC_AMPCAP_OFFSET_SHIFT) |
6141                                   (2 << AC_AMPCAP_NUM_STEPS_SHIFT) |
6142                                   (0x27 << AC_AMPCAP_STEP_SIZE_SHIFT) |
6143                                   (0 << AC_AMPCAP_MUTE_SHIFT));
6144
6145         return 0;
6146 }
6147
6148 static int patch_stac927x(struct hda_codec *codec)
6149 {
6150         struct sigmatel_spec *spec;
6151         int err;
6152
6153         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
6154         if (spec == NULL)
6155                 return -ENOMEM;
6156
6157         codec->no_trigger_sense = 1;
6158         codec->spec = spec;
6159         spec->linear_tone_beep = 1;
6160         codec->slave_dig_outs = stac927x_slave_dig_outs;
6161         spec->num_pins = ARRAY_SIZE(stac927x_pin_nids);
6162         spec->pin_nids = stac927x_pin_nids;
6163         spec->board_config = snd_hda_check_board_config(codec, STAC_927X_MODELS,
6164                                                         stac927x_models,
6165                                                         stac927x_cfg_tbl);
6166  again:
6167         if (spec->board_config < 0)
6168                 snd_printdd(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
6169                             codec->chip_name);
6170         else
6171                 stac92xx_set_config_regs(codec,
6172                                 stac927x_brd_tbl[spec->board_config]);
6173
6174         spec->digbeep_nid = 0x23;
6175         spec->adc_nids = stac927x_adc_nids;
6176         spec->num_adcs = ARRAY_SIZE(stac927x_adc_nids);
6177         spec->mux_nids = stac927x_mux_nids;
6178         spec->num_muxes = ARRAY_SIZE(stac927x_mux_nids);
6179         spec->smux_nids = stac927x_smux_nids;
6180         spec->num_smuxes = ARRAY_SIZE(stac927x_smux_nids);
6181         spec->spdif_labels = stac927x_spdif_labels;
6182         spec->dac_list = stac927x_dac_nids;
6183         spec->multiout.dac_nids = spec->dac_nids;
6184
6185         if (spec->board_config != STAC_D965_REF) {
6186                 /* GPIO0 High = Enable EAPD */
6187                 spec->eapd_mask = spec->gpio_mask = 0x01;
6188                 spec->gpio_dir = spec->gpio_data = 0x01;
6189         }
6190
6191         switch (spec->board_config) {
6192         case STAC_D965_3ST:
6193         case STAC_D965_5ST:
6194                 /* GPIO0 High = Enable EAPD */
6195                 spec->num_dmics = 0;
6196                 spec->init = d965_core_init;
6197                 break;
6198         case STAC_DELL_BIOS:
6199                 switch (codec->subsystem_id) {
6200                 case 0x10280209:
6201                 case 0x1028022e:
6202                         /* correct the device field to SPDIF out */
6203                         snd_hda_codec_set_pincfg(codec, 0x21, 0x01442070);
6204                         break;
6205                 }
6206                 /* configure the analog microphone on some laptops */
6207                 snd_hda_codec_set_pincfg(codec, 0x0c, 0x90a79130);
6208                 /* correct the front output jack as a hp out */
6209                 snd_hda_codec_set_pincfg(codec, 0x0f, 0x0227011f);
6210                 /* correct the front input jack as a mic */
6211                 snd_hda_codec_set_pincfg(codec, 0x0e, 0x02a79130);
6212                 /* fallthru */
6213         case STAC_DELL_3ST:
6214                 if (codec->subsystem_id != 0x1028022f) {
6215                         /* GPIO2 High = Enable EAPD */
6216                         spec->eapd_mask = spec->gpio_mask = 0x04;
6217                         spec->gpio_dir = spec->gpio_data = 0x04;
6218                 }
6219                 spec->dmic_nids = stac927x_dmic_nids;
6220                 spec->num_dmics = STAC927X_NUM_DMICS;
6221
6222                 spec->init = dell_3st_core_init;
6223                 spec->dmux_nids = stac927x_dmux_nids;
6224                 spec->num_dmuxes = ARRAY_SIZE(stac927x_dmux_nids);
6225                 break;
6226         case STAC_927X_VOLKNOB:
6227                 spec->num_dmics = 0;
6228                 spec->init = stac927x_volknob_core_init;
6229                 break;
6230         default:
6231                 spec->num_dmics = 0;
6232                 spec->init = stac927x_core_init;
6233                 break;
6234         }
6235
6236         spec->num_caps = STAC927X_NUM_CAPS;
6237         spec->capvols = stac927x_capvols;
6238         spec->capsws = stac927x_capsws;
6239
6240         spec->num_pwrs = 0;
6241         spec->aloopback_ctl = stac927x_loopback;
6242         spec->aloopback_mask = 0x40;
6243         spec->aloopback_shift = 0;
6244         spec->eapd_switch = 1;
6245
6246         err = stac92xx_parse_auto_config(codec);
6247         if (!err) {
6248                 if (spec->board_config < 0) {
6249                         printk(KERN_WARNING "hda_codec: No auto-config is "
6250                                "available, default to model=ref\n");
6251                         spec->board_config = STAC_D965_REF;
6252                         goto again;
6253                 }
6254                 err = -EINVAL;
6255         }
6256         if (err < 0) {
6257                 stac92xx_free(codec);
6258                 return err;
6259         }
6260
6261         codec->patch_ops = stac92xx_patch_ops;
6262
6263         codec->proc_widget_hook = stac927x_proc_hook;
6264
6265         /*
6266          * !!FIXME!!
6267          * The STAC927x seem to require fairly long delays for certain
6268          * command sequences.  With too short delays (even if the answer
6269          * is set to RIRB properly), it results in the silence output
6270          * on some hardwares like Dell.
6271          *
6272          * The below flag enables the longer delay (see get_response
6273          * in hda_intel.c).
6274          */
6275         codec->bus->needs_damn_long_delay = 1;
6276
6277         /* no jack detecion for ref-no-jd model */
6278         if (spec->board_config == STAC_D965_REF_NO_JD)
6279                 spec->hp_detect = 0;
6280
6281         return 0;
6282 }
6283
6284 static int patch_stac9205(struct hda_codec *codec)
6285 {
6286         struct sigmatel_spec *spec;
6287         int err;
6288
6289         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
6290         if (spec == NULL)
6291                 return -ENOMEM;
6292
6293         codec->no_trigger_sense = 1;
6294         codec->spec = spec;
6295         spec->linear_tone_beep = 1;
6296         spec->num_pins = ARRAY_SIZE(stac9205_pin_nids);
6297         spec->pin_nids = stac9205_pin_nids;
6298         spec->board_config = snd_hda_check_board_config(codec, STAC_9205_MODELS,
6299                                                         stac9205_models,
6300                                                         stac9205_cfg_tbl);
6301  again:
6302         if (spec->board_config < 0)
6303                 snd_printdd(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
6304                             codec->chip_name);
6305         else
6306                 stac92xx_set_config_regs(codec,
6307                                          stac9205_brd_tbl[spec->board_config]);
6308
6309         spec->digbeep_nid = 0x23;
6310         spec->adc_nids = stac9205_adc_nids;
6311         spec->num_adcs = ARRAY_SIZE(stac9205_adc_nids);
6312         spec->mux_nids = stac9205_mux_nids;
6313         spec->num_muxes = ARRAY_SIZE(stac9205_mux_nids);
6314         spec->smux_nids = stac9205_smux_nids;
6315         spec->num_smuxes = ARRAY_SIZE(stac9205_smux_nids);
6316         spec->dmic_nids = stac9205_dmic_nids;
6317         spec->num_dmics = STAC9205_NUM_DMICS;
6318         spec->dmux_nids = stac9205_dmux_nids;
6319         spec->num_dmuxes = ARRAY_SIZE(stac9205_dmux_nids);
6320         spec->num_pwrs = 0;
6321
6322         spec->init = stac9205_core_init;
6323         spec->aloopback_ctl = stac9205_loopback;
6324
6325         spec->num_caps = STAC9205_NUM_CAPS;
6326         spec->capvols = stac9205_capvols;
6327         spec->capsws = stac9205_capsws;
6328
6329         spec->aloopback_mask = 0x40;
6330         spec->aloopback_shift = 0;
6331         /* Turn on/off EAPD per HP plugging */
6332         if (spec->board_config != STAC_9205_EAPD)
6333                 spec->eapd_switch = 1;
6334         spec->multiout.dac_nids = spec->dac_nids;
6335         
6336         switch (spec->board_config){
6337         case STAC_9205_DELL_M43:
6338                 /* Enable SPDIF in/out */
6339                 snd_hda_codec_set_pincfg(codec, 0x1f, 0x01441030);
6340                 snd_hda_codec_set_pincfg(codec, 0x20, 0x1c410030);
6341
6342                 /* Enable unsol response for GPIO4/Dock HP connection */
6343                 err = stac_add_event(spec, codec->afg, STAC_VREF_EVENT, 0x01);
6344                 if (err < 0)
6345                         return err;
6346                 snd_hda_codec_write_cache(codec, codec->afg, 0,
6347                         AC_VERB_SET_GPIO_UNSOLICITED_RSP_MASK, 0x10);
6348                 snd_hda_codec_write_cache(codec, codec->afg, 0,
6349                                           AC_VERB_SET_UNSOLICITED_ENABLE,
6350                                           AC_USRSP_EN | err);
6351
6352                 spec->gpio_dir = 0x0b;
6353                 spec->eapd_mask = 0x01;
6354                 spec->gpio_mask = 0x1b;
6355                 spec->gpio_mute = 0x10;
6356                 /* GPIO0 High = EAPD, GPIO1 Low = Headphone Mute,
6357                  * GPIO3 Low = DRM
6358                  */
6359                 spec->gpio_data = 0x01;
6360                 break;
6361         case STAC_9205_REF:
6362                 /* SPDIF-In enabled */
6363                 break;
6364         default:
6365                 /* GPIO0 High = EAPD */
6366                 spec->eapd_mask = spec->gpio_mask = spec->gpio_dir = 0x1;
6367                 spec->gpio_data = 0x01;
6368                 break;
6369         }
6370
6371         err = stac92xx_parse_auto_config(codec);
6372         if (!err) {
6373                 if (spec->board_config < 0) {
6374                         printk(KERN_WARNING "hda_codec: No auto-config is "
6375                                "available, default to model=ref\n");
6376                         spec->board_config = STAC_9205_REF;
6377                         goto again;
6378                 }
6379                 err = -EINVAL;
6380         }
6381         if (err < 0) {
6382                 stac92xx_free(codec);
6383                 return err;
6384         }
6385
6386         codec->patch_ops = stac92xx_patch_ops;
6387
6388         codec->proc_widget_hook = stac9205_proc_hook;
6389
6390         return 0;
6391 }
6392
6393 /*
6394  * STAC9872 hack
6395  */
6396
6397 static const struct hda_verb stac9872_core_init[] = {
6398         {0x15, AC_VERB_SET_CONNECT_SEL, 0x1}, /* mic-sel: 0a,0d,14,02 */
6399         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
6400         {}
6401 };
6402
6403 static const hda_nid_t stac9872_pin_nids[] = {
6404         0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
6405         0x11, 0x13, 0x14,
6406 };
6407
6408 static const hda_nid_t stac9872_adc_nids[] = {
6409         0x8 /*,0x6*/
6410 };
6411
6412 static const hda_nid_t stac9872_mux_nids[] = {
6413         0x15
6414 };
6415
6416 static const unsigned long stac9872_capvols[] = {
6417         HDA_COMPOSE_AMP_VAL(0x09, 3, 0, HDA_INPUT),
6418 };
6419 #define stac9872_capsws         stac9872_capvols
6420
6421 static const unsigned int stac9872_vaio_pin_configs[9] = {
6422         0x03211020, 0x411111f0, 0x411111f0, 0x03a15030,
6423         0x411111f0, 0x90170110, 0x411111f0, 0x411111f0,
6424         0x90a7013e
6425 };
6426
6427 static const char * const stac9872_models[STAC_9872_MODELS] = {
6428         [STAC_9872_AUTO] = "auto",
6429         [STAC_9872_VAIO] = "vaio",
6430 };
6431
6432 static const unsigned int *stac9872_brd_tbl[STAC_9872_MODELS] = {
6433         [STAC_9872_VAIO] = stac9872_vaio_pin_configs,
6434 };
6435
6436 static const struct snd_pci_quirk stac9872_cfg_tbl[] = {
6437         SND_PCI_QUIRK_MASK(0x104d, 0xfff0, 0x81e0,
6438                            "Sony VAIO F/S", STAC_9872_VAIO),
6439         {} /* terminator */
6440 };
6441
6442 static int patch_stac9872(struct hda_codec *codec)
6443 {
6444         struct sigmatel_spec *spec;
6445         int err;
6446
6447         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
6448         if (spec == NULL)
6449                 return -ENOMEM;
6450         codec->no_trigger_sense = 1;
6451         codec->spec = spec;
6452         spec->linear_tone_beep = 1;
6453         spec->num_pins = ARRAY_SIZE(stac9872_pin_nids);
6454         spec->pin_nids = stac9872_pin_nids;
6455
6456         spec->board_config = snd_hda_check_board_config(codec, STAC_9872_MODELS,
6457                                                         stac9872_models,
6458                                                         stac9872_cfg_tbl);
6459         if (spec->board_config < 0)
6460                 snd_printdd(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
6461                             codec->chip_name);
6462         else
6463                 stac92xx_set_config_regs(codec,
6464                                          stac9872_brd_tbl[spec->board_config]);
6465
6466         spec->multiout.dac_nids = spec->dac_nids;
6467         spec->num_adcs = ARRAY_SIZE(stac9872_adc_nids);
6468         spec->adc_nids = stac9872_adc_nids;
6469         spec->num_muxes = ARRAY_SIZE(stac9872_mux_nids);
6470         spec->mux_nids = stac9872_mux_nids;
6471         spec->init = stac9872_core_init;
6472         spec->num_caps = 1;
6473         spec->capvols = stac9872_capvols;
6474         spec->capsws = stac9872_capsws;
6475
6476         err = stac92xx_parse_auto_config(codec);
6477         if (err < 0) {
6478                 stac92xx_free(codec);
6479                 return -EINVAL;
6480         }
6481         spec->input_mux = &spec->private_imux;
6482         codec->patch_ops = stac92xx_patch_ops;
6483         return 0;
6484 }
6485
6486
6487 /*
6488  * patch entries
6489  */
6490 static const struct hda_codec_preset snd_hda_preset_sigmatel[] = {
6491         { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
6492         { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
6493         { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
6494         { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
6495         { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
6496         { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
6497         { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
6498         { .id = 0x83847618, .name = "STAC9227", .patch = patch_stac927x },
6499         { .id = 0x83847619, .name = "STAC9227", .patch = patch_stac927x },
6500         { .id = 0x83847616, .name = "STAC9228", .patch = patch_stac927x },
6501         { .id = 0x83847617, .name = "STAC9228", .patch = patch_stac927x },
6502         { .id = 0x83847614, .name = "STAC9229", .patch = patch_stac927x },
6503         { .id = 0x83847615, .name = "STAC9229", .patch = patch_stac927x },
6504         { .id = 0x83847620, .name = "STAC9274", .patch = patch_stac927x },
6505         { .id = 0x83847621, .name = "STAC9274D", .patch = patch_stac927x },
6506         { .id = 0x83847622, .name = "STAC9273X", .patch = patch_stac927x },
6507         { .id = 0x83847623, .name = "STAC9273D", .patch = patch_stac927x },
6508         { .id = 0x83847624, .name = "STAC9272X", .patch = patch_stac927x },
6509         { .id = 0x83847625, .name = "STAC9272D", .patch = patch_stac927x },
6510         { .id = 0x83847626, .name = "STAC9271X", .patch = patch_stac927x },
6511         { .id = 0x83847627, .name = "STAC9271D", .patch = patch_stac927x },
6512         { .id = 0x83847628, .name = "STAC9274X5NH", .patch = patch_stac927x },
6513         { .id = 0x83847629, .name = "STAC9274D5NH", .patch = patch_stac927x },
6514         { .id = 0x83847632, .name = "STAC9202",  .patch = patch_stac925x },
6515         { .id = 0x83847633, .name = "STAC9202D", .patch = patch_stac925x },
6516         { .id = 0x83847634, .name = "STAC9250", .patch = patch_stac925x },
6517         { .id = 0x83847635, .name = "STAC9250D", .patch = patch_stac925x },
6518         { .id = 0x83847636, .name = "STAC9251", .patch = patch_stac925x },
6519         { .id = 0x83847637, .name = "STAC9250D", .patch = patch_stac925x },
6520         { .id = 0x83847645, .name = "92HD206X", .patch = patch_stac927x },
6521         { .id = 0x83847646, .name = "92HD206D", .patch = patch_stac927x },
6522         /* The following does not take into account .id=0x83847661 when subsys =
6523          * 104D0C00 which is STAC9225s. Because of this, some SZ Notebooks are
6524          * currently not fully supported.
6525          */
6526         { .id = 0x83847661, .name = "CXD9872RD/K", .patch = patch_stac9872 },
6527         { .id = 0x83847662, .name = "STAC9872AK", .patch = patch_stac9872 },
6528         { .id = 0x83847664, .name = "CXD9872AKD", .patch = patch_stac9872 },
6529         { .id = 0x83847698, .name = "STAC9205", .patch = patch_stac9205 },
6530         { .id = 0x838476a0, .name = "STAC9205", .patch = patch_stac9205 },
6531         { .id = 0x838476a1, .name = "STAC9205D", .patch = patch_stac9205 },
6532         { .id = 0x838476a2, .name = "STAC9204", .patch = patch_stac9205 },
6533         { .id = 0x838476a3, .name = "STAC9204D", .patch = patch_stac9205 },
6534         { .id = 0x838476a4, .name = "STAC9255", .patch = patch_stac9205 },
6535         { .id = 0x838476a5, .name = "STAC9255D", .patch = patch_stac9205 },
6536         { .id = 0x838476a6, .name = "STAC9254", .patch = patch_stac9205 },
6537         { .id = 0x838476a7, .name = "STAC9254D", .patch = patch_stac9205 },
6538         { .id = 0x111d7603, .name = "92HD75B3X5", .patch = patch_stac92hd71bxx},
6539         { .id = 0x111d7604, .name = "92HD83C1X5", .patch = patch_stac92hd83xxx},
6540         { .id = 0x111d76d4, .name = "92HD83C1C5", .patch = patch_stac92hd83xxx},
6541         { .id = 0x111d7605, .name = "92HD81B1X5", .patch = patch_stac92hd83xxx},
6542         { .id = 0x111d76d5, .name = "92HD81B1C5", .patch = patch_stac92hd83xxx},
6543         { .id = 0x111d76d1, .name = "92HD87B1/3", .patch = patch_stac92hd83xxx},
6544         { .id = 0x111d76d9, .name = "92HD87B2/4", .patch = patch_stac92hd83xxx},
6545         { .id = 0x111d7666, .name = "92HD88B3", .patch = patch_stac92hd83xxx},
6546         { .id = 0x111d7667, .name = "92HD88B1", .patch = patch_stac92hd83xxx},
6547         { .id = 0x111d7668, .name = "92HD88B2", .patch = patch_stac92hd83xxx},
6548         { .id = 0x111d7669, .name = "92HD88B4", .patch = patch_stac92hd83xxx},
6549         { .id = 0x111d7608, .name = "92HD75B2X5", .patch = patch_stac92hd71bxx},
6550         { .id = 0x111d7674, .name = "92HD73D1X5", .patch = patch_stac92hd73xx },
6551         { .id = 0x111d7675, .name = "92HD73C1X5", .patch = patch_stac92hd73xx },
6552         { .id = 0x111d7676, .name = "92HD73E1X5", .patch = patch_stac92hd73xx },
6553         { .id = 0x111d76b0, .name = "92HD71B8X", .patch = patch_stac92hd71bxx },
6554         { .id = 0x111d76b1, .name = "92HD71B8X", .patch = patch_stac92hd71bxx },
6555         { .id = 0x111d76b2, .name = "92HD71B7X", .patch = patch_stac92hd71bxx },
6556         { .id = 0x111d76b3, .name = "92HD71B7X", .patch = patch_stac92hd71bxx },
6557         { .id = 0x111d76b4, .name = "92HD71B6X", .patch = patch_stac92hd71bxx },
6558         { .id = 0x111d76b5, .name = "92HD71B6X", .patch = patch_stac92hd71bxx },
6559         { .id = 0x111d76b6, .name = "92HD71B5X", .patch = patch_stac92hd71bxx },
6560         { .id = 0x111d76b7, .name = "92HD71B5X", .patch = patch_stac92hd71bxx },
6561         { .id = 0x111d76c0, .name = "92HD89C3", .patch = patch_stac92hd73xx },
6562         { .id = 0x111d76c1, .name = "92HD89C2", .patch = patch_stac92hd73xx },
6563         { .id = 0x111d76c2, .name = "92HD89C1", .patch = patch_stac92hd73xx },
6564         { .id = 0x111d76c3, .name = "92HD89B3", .patch = patch_stac92hd73xx },
6565         { .id = 0x111d76c4, .name = "92HD89B2", .patch = patch_stac92hd73xx },
6566         { .id = 0x111d76c5, .name = "92HD89B1", .patch = patch_stac92hd73xx },
6567         { .id = 0x111d76c6, .name = "92HD89E3", .patch = patch_stac92hd73xx },
6568         { .id = 0x111d76c7, .name = "92HD89E2", .patch = patch_stac92hd73xx },
6569         { .id = 0x111d76c8, .name = "92HD89E1", .patch = patch_stac92hd73xx },
6570         { .id = 0x111d76c9, .name = "92HD89D3", .patch = patch_stac92hd73xx },
6571         { .id = 0x111d76ca, .name = "92HD89D2", .patch = patch_stac92hd73xx },
6572         { .id = 0x111d76cb, .name = "92HD89D1", .patch = patch_stac92hd73xx },
6573         { .id = 0x111d76cc, .name = "92HD89F3", .patch = patch_stac92hd73xx },
6574         { .id = 0x111d76cd, .name = "92HD89F2", .patch = patch_stac92hd73xx },
6575         { .id = 0x111d76ce, .name = "92HD89F1", .patch = patch_stac92hd73xx },
6576         { .id = 0x111d76df, .name = "92HD93BXX", .patch = patch_stac92hd83xxx},
6577         { .id = 0x111d76e0, .name = "92HD91BXX", .patch = patch_stac92hd83xxx},
6578         { .id = 0x111d76e3, .name = "92HD98BXX", .patch = patch_stac92hd83xxx},
6579         { .id = 0x111d76e5, .name = "92HD99BXX", .patch = patch_stac92hd83xxx},
6580         { .id = 0x111d76e7, .name = "92HD90BXX", .patch = patch_stac92hd83xxx},
6581         { .id = 0x111d76e8, .name = "92HD66B1X5", .patch = patch_stac92hd83xxx},
6582         { .id = 0x111d76e9, .name = "92HD66B2X5", .patch = patch_stac92hd83xxx},
6583         { .id = 0x111d76ea, .name = "92HD66B3X5", .patch = patch_stac92hd83xxx},
6584         { .id = 0x111d76eb, .name = "92HD66C1X5", .patch = patch_stac92hd83xxx},
6585         { .id = 0x111d76ec, .name = "92HD66C2X5", .patch = patch_stac92hd83xxx},
6586         { .id = 0x111d76ed, .name = "92HD66C3X5", .patch = patch_stac92hd83xxx},
6587         { .id = 0x111d76ee, .name = "92HD66B1X3", .patch = patch_stac92hd83xxx},
6588         { .id = 0x111d76ef, .name = "92HD66B2X3", .patch = patch_stac92hd83xxx},
6589         { .id = 0x111d76f0, .name = "92HD66B3X3", .patch = patch_stac92hd83xxx},
6590         { .id = 0x111d76f1, .name = "92HD66C1X3", .patch = patch_stac92hd83xxx},
6591         { .id = 0x111d76f2, .name = "92HD66C2X3", .patch = patch_stac92hd83xxx},
6592         { .id = 0x111d76f3, .name = "92HD66C3/65", .patch = patch_stac92hd83xxx},
6593         {} /* terminator */
6594 };
6595
6596 MODULE_ALIAS("snd-hda-codec-id:8384*");
6597 MODULE_ALIAS("snd-hda-codec-id:111d*");
6598
6599 MODULE_LICENSE("GPL");
6600 MODULE_DESCRIPTION("IDT/Sigmatel HD-audio codec");
6601
6602 static struct hda_codec_preset_list sigmatel_list = {
6603         .preset = snd_hda_preset_sigmatel,
6604         .owner = THIS_MODULE,
6605 };
6606
6607 static int __init patch_sigmatel_init(void)
6608 {
6609         return snd_hda_add_codec_preset(&sigmatel_list);
6610 }
6611
6612 static void __exit patch_sigmatel_exit(void)
6613 {
6614         snd_hda_delete_codec_preset(&sigmatel_list);
6615 }
6616
6617 module_init(patch_sigmatel_init)
6618 module_exit(patch_sigmatel_exit)