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