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