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