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