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