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