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