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