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