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