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