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