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