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