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