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