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