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