]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/pci/hda/patch_sigmatel.c
be53e9685bc0c52eacdc82257375ca5335db6bde
[karo-tx-linux.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 <sound/driver.h>
28 #include <linux/init.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/pci.h>
32 #include <sound/core.h>
33 #include <sound/asoundef.h>
34 #include "hda_codec.h"
35 #include "hda_local.h"
36
37 #define NUM_CONTROL_ALLOC       32
38 #define STAC_HP_EVENT           0x37
39
40 enum {
41         STAC_REF,
42         STAC_9200_MODELS
43 };
44
45 enum {
46         STAC_9205_REF,
47         STAC_9205_MODELS
48 };
49
50 enum {
51         STAC_925x_REF,
52         STAC_M2_2,
53         STAC_MA6,
54         STAC_PA6,
55         STAC_925x_MODELS
56 };
57
58 enum {
59         STAC_D945_REF,
60         STAC_D945GTP3,
61         STAC_D945GTP5,
62         STAC_922X_DELL,
63         STAC_INTEL_MAC_V1,
64         STAC_INTEL_MAC_V2,
65         STAC_INTEL_MAC_V3,
66         STAC_INTEL_MAC_V4,
67         STAC_INTEL_MAC_V5,
68         /* for backward compitability */
69         STAC_MACMINI,
70         STAC_MACBOOK,
71         STAC_MACBOOK_PRO_V1,
72         STAC_MACBOOK_PRO_V2,
73         STAC_IMAC_INTEL,
74         STAC_IMAC_INTEL_20,
75         STAC_922X_MODELS
76 };
77
78 enum {
79         STAC_D965_REF,
80         STAC_D965_3ST,
81         STAC_D965_5ST,
82         STAC_927X_MODELS
83 };
84
85 struct sigmatel_spec {
86         struct snd_kcontrol_new *mixers[4];
87         unsigned int num_mixers;
88
89         int board_config;
90         unsigned int surr_switch: 1;
91         unsigned int line_switch: 1;
92         unsigned int mic_switch: 1;
93         unsigned int alt_switch: 1;
94         unsigned int hp_detect: 1;
95         unsigned int gpio_mute: 1;
96
97         /* playback */
98         struct hda_multi_out multiout;
99         hda_nid_t dac_nids[5];
100
101         /* capture */
102         hda_nid_t *adc_nids;
103         unsigned int num_adcs;
104         hda_nid_t *mux_nids;
105         unsigned int num_muxes;
106         hda_nid_t *dmic_nids;
107         unsigned int num_dmics;
108         hda_nid_t dmux_nid;
109         hda_nid_t dig_in_nid;
110
111         /* pin widgets */
112         hda_nid_t *pin_nids;
113         unsigned int num_pins;
114         unsigned int *pin_configs;
115         unsigned int *bios_pin_configs;
116
117         /* codec specific stuff */
118         struct hda_verb *init;
119         struct snd_kcontrol_new *mixer;
120
121         /* capture source */
122         struct hda_input_mux *dinput_mux;
123         unsigned int cur_dmux;
124         struct hda_input_mux *input_mux;
125         unsigned int cur_mux[3];
126
127         /* i/o switches */
128         unsigned int io_switch[2];
129
130         struct hda_pcm pcm_rec[2];      /* PCM information */
131
132         /* dynamic controls and input_mux */
133         struct auto_pin_cfg autocfg;
134         unsigned int num_kctl_alloc, num_kctl_used;
135         struct snd_kcontrol_new *kctl_alloc;
136         struct hda_input_mux private_dimux;
137         struct hda_input_mux private_imux;
138 };
139
140 static hda_nid_t stac9200_adc_nids[1] = {
141         0x03,
142 };
143
144 static hda_nid_t stac9200_mux_nids[1] = {
145         0x0c,
146 };
147
148 static hda_nid_t stac9200_dac_nids[1] = {
149         0x02,
150 };
151
152 static hda_nid_t stac925x_adc_nids[1] = {
153         0x03,
154 };
155
156 static hda_nid_t stac925x_mux_nids[1] = {
157         0x0f,
158 };
159
160 static hda_nid_t stac925x_dac_nids[1] = {
161         0x02,
162 };
163
164 static hda_nid_t stac925x_dmic_nids[1] = {
165         0x15, 
166 };
167
168 static hda_nid_t stac922x_adc_nids[2] = {
169         0x06, 0x07,
170 };
171
172 static hda_nid_t stac922x_mux_nids[2] = {
173         0x12, 0x13,
174 };
175
176 static hda_nid_t stac927x_adc_nids[3] = {
177         0x07, 0x08, 0x09
178 };
179
180 static hda_nid_t stac927x_mux_nids[3] = {
181         0x15, 0x16, 0x17
182 };
183
184 static hda_nid_t stac9205_adc_nids[2] = {
185         0x12, 0x13
186 };
187
188 static hda_nid_t stac9205_mux_nids[2] = {
189         0x19, 0x1a
190 };
191
192 static hda_nid_t stac9205_dmic_nids[2] = {
193         0x17, 0x18,
194 };
195
196 static hda_nid_t stac9200_pin_nids[8] = {
197         0x08, 0x09, 0x0d, 0x0e, 
198         0x0f, 0x10, 0x11, 0x12,
199 };
200
201 static hda_nid_t stac925x_pin_nids[8] = {
202         0x07, 0x08, 0x0a, 0x0b, 
203         0x0c, 0x0d, 0x10, 0x11,
204 };
205
206 static hda_nid_t stac922x_pin_nids[10] = {
207         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
208         0x0f, 0x10, 0x11, 0x15, 0x1b,
209 };
210
211 static hda_nid_t stac927x_pin_nids[14] = {
212         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
213         0x0f, 0x10, 0x11, 0x12, 0x13,
214         0x14, 0x21, 0x22, 0x23,
215 };
216
217 static hda_nid_t stac9205_pin_nids[12] = {
218         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
219         0x0f, 0x14, 0x16, 0x17, 0x18,
220         0x21, 0x22,
221         
222 };
223
224 static int stac92xx_dmux_enum_info(struct snd_kcontrol *kcontrol,
225                                    struct snd_ctl_elem_info *uinfo)
226 {
227         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
228         struct sigmatel_spec *spec = codec->spec;
229         return snd_hda_input_mux_info(spec->dinput_mux, uinfo);
230 }
231
232 static int stac92xx_dmux_enum_get(struct snd_kcontrol *kcontrol,
233                                   struct snd_ctl_elem_value *ucontrol)
234 {
235         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
236         struct sigmatel_spec *spec = codec->spec;
237
238         ucontrol->value.enumerated.item[0] = spec->cur_dmux;
239         return 0;
240 }
241
242 static int stac92xx_dmux_enum_put(struct snd_kcontrol *kcontrol,
243                                   struct snd_ctl_elem_value *ucontrol)
244 {
245         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
246         struct sigmatel_spec *spec = codec->spec;
247
248         return snd_hda_input_mux_put(codec, spec->dinput_mux, ucontrol,
249                                      spec->dmux_nid, &spec->cur_dmux);
250 }
251
252 static int stac92xx_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
253 {
254         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
255         struct sigmatel_spec *spec = codec->spec;
256         return snd_hda_input_mux_info(spec->input_mux, uinfo);
257 }
258
259 static int stac92xx_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
260 {
261         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
262         struct sigmatel_spec *spec = codec->spec;
263         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
264
265         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
266         return 0;
267 }
268
269 static int stac92xx_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
270 {
271         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
272         struct sigmatel_spec *spec = codec->spec;
273         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
274
275         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
276                                      spec->mux_nids[adc_idx], &spec->cur_mux[adc_idx]);
277 }
278
279 static struct hda_verb stac9200_core_init[] = {
280         /* set dac0mux for dac converter */
281         { 0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
282         {}
283 };
284
285 static struct hda_verb stac925x_core_init[] = {
286         /* set dac0mux for dac converter */
287         { 0x06, AC_VERB_SET_CONNECT_SEL, 0x00},
288         {}
289 };
290
291 static struct hda_verb stac922x_core_init[] = {
292         /* set master volume and direct control */      
293         { 0x16, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
294         {}
295 };
296
297 static struct hda_verb d965_core_init[] = {
298         /* set master volume and direct control */      
299         { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
300         /* unmute node 0x1b */
301         { 0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
302         /* select node 0x03 as DAC */   
303         { 0x0b, AC_VERB_SET_CONNECT_SEL, 0x01},
304         {}
305 };
306
307 static struct hda_verb stac927x_core_init[] = {
308         /* set master volume and direct control */      
309         { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
310         {}
311 };
312
313 static struct hda_verb stac9205_core_init[] = {
314         /* set master volume and direct control */      
315         { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
316         {}
317 };
318
319 static struct snd_kcontrol_new stac9200_mixer[] = {
320         HDA_CODEC_VOLUME("Master Playback Volume", 0xb, 0, HDA_OUTPUT),
321         HDA_CODEC_MUTE("Master Playback Switch", 0xb, 0, HDA_OUTPUT),
322         {
323                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
324                 .name = "Input Source",
325                 .count = 1,
326                 .info = stac92xx_mux_enum_info,
327                 .get = stac92xx_mux_enum_get,
328                 .put = stac92xx_mux_enum_put,
329         },
330         HDA_CODEC_VOLUME("Capture Volume", 0x0a, 0, HDA_OUTPUT),
331         HDA_CODEC_MUTE("Capture Switch", 0x0a, 0, HDA_OUTPUT),
332         HDA_CODEC_VOLUME("Capture Mux Volume", 0x0c, 0, HDA_OUTPUT),
333         { } /* end */
334 };
335
336 static struct snd_kcontrol_new stac925x_mixer[] = {
337         {
338                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
339                 .name = "Input Source",
340                 .count = 1,
341                 .info = stac92xx_mux_enum_info,
342                 .get = stac92xx_mux_enum_get,
343                 .put = stac92xx_mux_enum_put,
344         },
345         HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_OUTPUT),
346         HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_OUTPUT),
347         HDA_CODEC_VOLUME("Capture Mux Volume", 0x0f, 0, HDA_OUTPUT),
348         { } /* end */
349 };
350
351 /* This needs to be generated dynamically based on sequence */
352 static struct snd_kcontrol_new stac922x_mixer[] = {
353         {
354                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
355                 .name = "Input Source",
356                 .count = 1,
357                 .info = stac92xx_mux_enum_info,
358                 .get = stac92xx_mux_enum_get,
359                 .put = stac92xx_mux_enum_put,
360         },
361         HDA_CODEC_VOLUME("Capture Volume", 0x17, 0x0, HDA_INPUT),
362         HDA_CODEC_MUTE("Capture Switch", 0x17, 0x0, HDA_INPUT),
363         HDA_CODEC_VOLUME("Mux Capture Volume", 0x12, 0x0, HDA_OUTPUT),
364         { } /* end */
365 };
366
367 /* This needs to be generated dynamically based on sequence */
368 static struct snd_kcontrol_new stac9227_mixer[] = {
369         {
370                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
371                 .name = "Input Source",
372                 .count = 1,
373                 .info = stac92xx_mux_enum_info,
374                 .get = stac92xx_mux_enum_get,
375                 .put = stac92xx_mux_enum_put,
376         },
377         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
378         HDA_CODEC_MUTE("Capture Switch", 0x1b, 0x0, HDA_OUTPUT),
379         { } /* end */
380 };
381
382 static struct snd_kcontrol_new stac927x_mixer[] = {
383         {
384                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
385                 .name = "Input Source",
386                 .count = 1,
387                 .info = stac92xx_mux_enum_info,
388                 .get = stac92xx_mux_enum_get,
389                 .put = stac92xx_mux_enum_put,
390         },
391         HDA_CODEC_VOLUME("InMux Capture Volume", 0x15, 0x0, HDA_OUTPUT),
392         HDA_CODEC_VOLUME("InVol Capture Volume", 0x18, 0x0, HDA_INPUT),
393         HDA_CODEC_MUTE("ADCMux Capture Switch", 0x1b, 0x0, HDA_OUTPUT),
394         { } /* end */
395 };
396
397 static struct snd_kcontrol_new stac9205_mixer[] = {
398         {
399                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
400                 .name = "Digital Input Source",
401                 .count = 1,
402                 .info = stac92xx_dmux_enum_info,
403                 .get = stac92xx_dmux_enum_get,
404                 .put = stac92xx_dmux_enum_put,
405         },
406         {
407                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
408                 .name = "Input Source",
409                 .count = 1,
410                 .info = stac92xx_mux_enum_info,
411                 .get = stac92xx_mux_enum_get,
412                 .put = stac92xx_mux_enum_put,
413         },
414         HDA_CODEC_VOLUME("InMux Capture Volume", 0x19, 0x0, HDA_OUTPUT),
415         HDA_CODEC_VOLUME("InVol Capture Volume", 0x1b, 0x0, HDA_INPUT),
416         HDA_CODEC_MUTE("ADCMux Capture Switch", 0x1d, 0x0, HDA_OUTPUT),
417         { } /* end */
418 };
419
420 static int stac92xx_build_controls(struct hda_codec *codec)
421 {
422         struct sigmatel_spec *spec = codec->spec;
423         int err;
424         int i;
425
426         err = snd_hda_add_new_ctls(codec, spec->mixer);
427         if (err < 0)
428                 return err;
429
430         for (i = 0; i < spec->num_mixers; i++) {
431                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
432                 if (err < 0)
433                         return err;
434         }
435
436         if (spec->multiout.dig_out_nid) {
437                 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
438                 if (err < 0)
439                         return err;
440         }
441         if (spec->dig_in_nid) {
442                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
443                 if (err < 0)
444                         return err;
445         }
446         return 0;       
447 }
448
449 static unsigned int ref9200_pin_configs[8] = {
450         0x01c47010, 0x01447010, 0x0221401f, 0x01114010,
451         0x02a19020, 0x01a19021, 0x90100140, 0x01813122,
452 };
453
454 static unsigned int *stac9200_brd_tbl[STAC_9200_MODELS] = {
455         [STAC_REF] = ref9200_pin_configs,
456 };
457
458 static const char *stac9200_models[STAC_9200_MODELS] = {
459         [STAC_REF] = "ref",
460 };
461
462 static struct snd_pci_quirk stac9200_cfg_tbl[] = {
463         /* SigmaTel reference board */
464         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
465                       "DFI LanParty", STAC_REF),
466         /* Dell laptops have BIOS problem */
467         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01b5,
468                       "Dell Inspiron 630m", STAC_REF),
469         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c2,
470                       "Dell Latitude D620", STAC_REF),
471         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cb,
472                       "Dell Latitude 120L", STAC_REF),
473         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cc,
474                       "Dell Latitude D820", STAC_REF),
475         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cd,
476                       "Dell Inspiron E1705/9400", STAC_REF),
477         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ce,
478                       "Dell XPS M1710", STAC_REF),
479         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cf,
480                       "Dell Precision M90", STAC_REF),
481         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d6,
482                       "unknown Dell", STAC_REF),
483         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d8,
484                       "Dell Inspiron 640m", STAC_REF),
485         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f5,
486                       "Dell Inspiron 1501", STAC_REF),
487
488         /* Panasonic */
489         SND_PCI_QUIRK(0x10f7, 0x8338, "Panasonic CF-74", STAC_REF),
490
491         {} /* terminator */
492 };
493
494 static unsigned int ref925x_pin_configs[8] = {
495         0x40c003f0, 0x424503f2, 0x01813022, 0x02a19021,
496         0x90a70320, 0x02214210, 0x400003f1, 0x9033032e,
497 };
498
499 static unsigned int stac925x_MA6_pin_configs[8] = {
500         0x40c003f0, 0x424503f2, 0x01813022, 0x02a19021,
501         0x90a70320, 0x90100211, 0x400003f1, 0x9033032e,
502 };
503
504 static unsigned int stac925x_PA6_pin_configs[8] = {
505         0x40c003f0, 0x424503f2, 0x01813022, 0x02a19021,
506         0x50a103f0, 0x90100211, 0x400003f1, 0x9033032e,
507 };
508
509 static unsigned int stac925xM2_2_pin_configs[8] = {
510         0x40c003f3, 0x424503f2, 0x04180011, 0x02a19020,
511         0x50a103f0, 0x90100212, 0x400003f1, 0x9033032e,
512 };
513
514 static unsigned int *stac925x_brd_tbl[STAC_925x_MODELS] = {
515         [STAC_REF] = ref925x_pin_configs,
516         [STAC_M2_2] = stac925xM2_2_pin_configs,
517         [STAC_MA6] = stac925x_MA6_pin_configs,
518         [STAC_PA6] = stac925x_PA6_pin_configs,
519 };
520
521 static const char *stac925x_models[STAC_925x_MODELS] = {
522         [STAC_REF] = "ref",
523         [STAC_M2_2] = "m2-2",
524         [STAC_MA6] = "m6",
525         [STAC_PA6] = "pa6",
526 };
527
528 static struct snd_pci_quirk stac925x_cfg_tbl[] = {
529         /* SigmaTel reference board */
530         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668, "DFI LanParty", STAC_REF),
531         SND_PCI_QUIRK(0x8384, 0x7632, "Stac9202 Reference Board", STAC_REF),
532         SND_PCI_QUIRK(0x107b, 0x0316, "Gateway M255", STAC_REF),
533         SND_PCI_QUIRK(0x107b, 0x0366, "Gateway MP6954", STAC_REF),
534         SND_PCI_QUIRK(0x107b, 0x0461, "Gateway NX560XL", STAC_MA6),
535         SND_PCI_QUIRK(0x107b, 0x0681, "Gateway NX860", STAC_PA6),
536         SND_PCI_QUIRK(0x1002, 0x437b, "Gateway MX6453", STAC_M2_2),
537         {} /* terminator */
538 };
539
540 static unsigned int ref922x_pin_configs[10] = {
541         0x01014010, 0x01016011, 0x01012012, 0x0221401f,
542         0x01813122, 0x01011014, 0x01441030, 0x01c41030,
543         0x40000100, 0x40000100,
544 };
545
546 static unsigned int d945gtp3_pin_configs[10] = {
547         0x0221401f, 0x01a19022, 0x01813021, 0x01014010,
548         0x40000100, 0x40000100, 0x40000100, 0x40000100,
549         0x02a19120, 0x40000100,
550 };
551
552 static unsigned int d945gtp5_pin_configs[10] = {
553         0x0221401f, 0x01011012, 0x01813024, 0x01014010,
554         0x01a19021, 0x01016011, 0x01452130, 0x40000100,
555         0x02a19320, 0x40000100,
556 };
557
558 static unsigned int intel_mac_v1_pin_configs[10] = {
559         0x0121e21f, 0x400000ff, 0x9017e110, 0x400000fd,
560         0x400000fe, 0x0181e020, 0x1145e030, 0x11c5e240,
561         0x400000fc, 0x400000fb,
562 };
563
564 static unsigned int intel_mac_v2_pin_configs[10] = {
565         0x0121e21f, 0x90a7012e, 0x9017e110, 0x400000fd,
566         0x400000fe, 0x0181e020, 0x1145e230, 0x500000fa,
567         0x400000fc, 0x400000fb,
568 };
569
570 static unsigned int intel_mac_v3_pin_configs[10] = {
571         0x0121e21f, 0x90a7012e, 0x9017e110, 0x400000fd,
572         0x400000fe, 0x0181e020, 0x1145e230, 0x11c5e240,
573         0x400000fc, 0x400000fb,
574 };
575
576 static unsigned int intel_mac_v4_pin_configs[10] = {
577         0x0321e21f, 0x03a1e02e, 0x9017e110, 0x9017e11f,
578         0x400000fe, 0x0381e020, 0x1345e230, 0x13c5e240,
579         0x400000fc, 0x400000fb,
580 };
581
582 static unsigned int intel_mac_v5_pin_configs[10] = {
583         0x0321e21f, 0x03a1e02e, 0x9017e110, 0x9017e11f,
584         0x400000fe, 0x0381e020, 0x1345e230, 0x13c5e240,
585         0x400000fc, 0x400000fb,
586 };
587
588 static unsigned int stac922x_dell_pin_configs[10] = {
589         0x0221121e, 0x408103ff, 0x02a1123e, 0x90100310,
590         0x408003f1, 0x0221122f, 0x03451340, 0x40c003f2,
591         0x50a003f3, 0x405003f4
592 };
593
594 static unsigned int *stac922x_brd_tbl[STAC_922X_MODELS] = {
595         [STAC_D945_REF] = ref922x_pin_configs,
596         [STAC_D945GTP3] = d945gtp3_pin_configs,
597         [STAC_D945GTP5] = d945gtp5_pin_configs,
598         [STAC_922X_DELL] = stac922x_dell_pin_configs,
599         [STAC_INTEL_MAC_V1] = intel_mac_v1_pin_configs,
600         [STAC_INTEL_MAC_V2] = intel_mac_v2_pin_configs,
601         [STAC_INTEL_MAC_V3] = intel_mac_v3_pin_configs,
602         [STAC_INTEL_MAC_V4] = intel_mac_v4_pin_configs,
603         [STAC_INTEL_MAC_V5] = intel_mac_v5_pin_configs,
604         /* for backward compitability */
605         [STAC_MACMINI] = intel_mac_v3_pin_configs,
606         [STAC_MACBOOK] = intel_mac_v5_pin_configs,
607         [STAC_MACBOOK_PRO_V1] = intel_mac_v3_pin_configs,
608         [STAC_MACBOOK_PRO_V2] = intel_mac_v3_pin_configs,
609         [STAC_IMAC_INTEL] = intel_mac_v2_pin_configs,
610         [STAC_IMAC_INTEL_20] = intel_mac_v3_pin_configs,
611 };
612
613 static const char *stac922x_models[STAC_922X_MODELS] = {
614         [STAC_D945_REF] = "ref",
615         [STAC_D945GTP5] = "5stack",
616         [STAC_D945GTP3] = "3stack",
617         [STAC_922X_DELL] = "dell",
618         [STAC_INTEL_MAC_V1] = "intel-mac-v1",
619         [STAC_INTEL_MAC_V2] = "intel-mac-v2",
620         [STAC_INTEL_MAC_V3] = "intel-mac-v3",
621         [STAC_INTEL_MAC_V4] = "intel-mac-v4",
622         [STAC_INTEL_MAC_V5] = "intel-mac-v5",
623         /* for backward compitability */
624         [STAC_MACMINI]  = "macmini",
625         [STAC_MACBOOK]  = "macbook",
626         [STAC_MACBOOK_PRO_V1]   = "macbook-pro-v1",
627         [STAC_MACBOOK_PRO_V2]   = "macbook-pro",
628         [STAC_IMAC_INTEL] = "imac-intel",
629         [STAC_IMAC_INTEL_20] = "imac-intel-20",
630 };
631
632 static struct snd_pci_quirk stac922x_cfg_tbl[] = {
633         /* SigmaTel reference board */
634         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
635                       "DFI LanParty", STAC_D945_REF),
636         /* Intel 945G based systems */
637         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0101,
638                       "Intel D945G", STAC_D945GTP3),
639         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0202,
640                       "Intel D945G", STAC_D945GTP3),
641         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0606,
642                       "Intel D945G", STAC_D945GTP3),
643         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0601,
644                       "Intel D945G", STAC_D945GTP3),
645         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0111,
646                       "Intel D945G", STAC_D945GTP3),
647         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1115,
648                       "Intel D945G", STAC_D945GTP3),
649         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1116,
650                       "Intel D945G", STAC_D945GTP3),
651         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1117,
652                       "Intel D945G", STAC_D945GTP3),
653         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1118,
654                       "Intel D945G", STAC_D945GTP3),
655         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1119,
656                       "Intel D945G", STAC_D945GTP3),
657         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x8826,
658                       "Intel D945G", STAC_D945GTP3),
659         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x5049,
660                       "Intel D945G", STAC_D945GTP3),
661         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x5055,
662                       "Intel D945G", STAC_D945GTP3),
663         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x5048,
664                       "Intel D945G", STAC_D945GTP3),
665         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0110,
666                       "Intel D945G", STAC_D945GTP3),
667         /* Intel D945G 5-stack systems */
668         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0404,
669                       "Intel D945G", STAC_D945GTP5),
670         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0303,
671                       "Intel D945G", STAC_D945GTP5),
672         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0013,
673                       "Intel D945G", STAC_D945GTP5),
674         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0417,
675                       "Intel D945G", STAC_D945GTP5),
676         /* Intel 945P based systems */
677         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0b0b,
678                       "Intel D945P", STAC_D945GTP3),
679         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0112,
680                       "Intel D945P", STAC_D945GTP3),
681         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0d0d,
682                       "Intel D945P", STAC_D945GTP3),
683         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0909,
684                       "Intel D945P", STAC_D945GTP3),
685         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0505,
686                       "Intel D945P", STAC_D945GTP3),
687         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0707,
688                       "Intel D945P", STAC_D945GTP5),
689         /* other systems  */
690         /* Apple Mac Mini (early 2006) */
691         SND_PCI_QUIRK(0x8384, 0x7680,
692                       "Mac Mini", STAC_INTEL_MAC_V3),
693         /* Dell */
694         SND_PCI_QUIRK(0x1028, 0x01d7, "Dell XPS M1210", STAC_922X_DELL),
695
696         {} /* terminator */
697 };
698
699 static unsigned int ref927x_pin_configs[14] = {
700         0x02214020, 0x02a19080, 0x0181304e, 0x01014010,
701         0x01a19040, 0x01011012, 0x01016011, 0x0101201f, 
702         0x183301f0, 0x18a001f0, 0x18a001f0, 0x01442070,
703         0x01c42190, 0x40000100,
704 };
705
706 static unsigned int d965_3st_pin_configs[14] = {
707         0x0221401f, 0x02a19120, 0x40000100, 0x01014011,
708         0x01a19021, 0x01813024, 0x40000100, 0x40000100,
709         0x40000100, 0x40000100, 0x40000100, 0x40000100,
710         0x40000100, 0x40000100
711 };
712
713 static unsigned int d965_5st_pin_configs[14] = {
714         0x02214020, 0x02a19080, 0x0181304e, 0x01014010,
715         0x01a19040, 0x01011012, 0x01016011, 0x40000100,
716         0x40000100, 0x40000100, 0x40000100, 0x01442070,
717         0x40000100, 0x40000100
718 };
719
720 static unsigned int *stac927x_brd_tbl[STAC_927X_MODELS] = {
721         [STAC_D965_REF] = ref927x_pin_configs,
722         [STAC_D965_3ST] = d965_3st_pin_configs,
723         [STAC_D965_5ST] = d965_5st_pin_configs,
724 };
725
726 static const char *stac927x_models[STAC_927X_MODELS] = {
727         [STAC_D965_REF] = "ref",
728         [STAC_D965_3ST] = "3stack",
729         [STAC_D965_5ST] = "5stack",
730 };
731
732 static struct snd_pci_quirk stac927x_cfg_tbl[] = {
733         /* SigmaTel reference board */
734         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
735                       "DFI LanParty", STAC_D965_REF),
736          /* Intel 946 based systems */
737         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x3d01, "Intel D946", STAC_D965_3ST),
738         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0xa301, "Intel D946", STAC_D965_3ST),
739         /* 965 based 3 stack systems */
740         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2116, "Intel D965", STAC_D965_3ST),
741         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2115, "Intel D965", STAC_D965_3ST),
742         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2114, "Intel D965", STAC_D965_3ST),
743         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2113, "Intel D965", STAC_D965_3ST),
744         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2112, "Intel D965", STAC_D965_3ST),
745         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2111, "Intel D965", STAC_D965_3ST),
746         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2110, "Intel D965", STAC_D965_3ST),
747         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2009, "Intel D965", STAC_D965_3ST),
748         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2008, "Intel D965", STAC_D965_3ST),
749         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2007, "Intel D965", STAC_D965_3ST),
750         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2006, "Intel D965", STAC_D965_3ST),
751         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2005, "Intel D965", STAC_D965_3ST),
752         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2004, "Intel D965", STAC_D965_3ST),
753         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2003, "Intel D965", STAC_D965_3ST),
754         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2002, "Intel D965", STAC_D965_3ST),
755         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2001, "Intel D965", STAC_D965_3ST),
756         /* 965 based 5 stack systems */
757         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2301, "Intel D965", STAC_D965_5ST),
758         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2302, "Intel D965", STAC_D965_5ST),
759         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2303, "Intel D965", STAC_D965_5ST),
760         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2304, "Intel D965", STAC_D965_5ST),
761         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2305, "Intel D965", STAC_D965_5ST),
762         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2501, "Intel D965", STAC_D965_5ST),
763         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2502, "Intel D965", STAC_D965_5ST),
764         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2503, "Intel D965", STAC_D965_5ST),
765         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2504, "Intel D965", STAC_D965_5ST),
766         {} /* terminator */
767 };
768
769 static unsigned int ref9205_pin_configs[12] = {
770         0x40000100, 0x40000100, 0x01016011, 0x01014010,
771         0x01813122, 0x01a19021, 0x40000100, 0x40000100,
772         0x90a000f0, 0x90a000f0, 0x01441030, 0x01c41030
773 };
774
775 static unsigned int *stac9205_brd_tbl[STAC_9205_MODELS] = {
776         ref9205_pin_configs,
777 };
778
779 static const char *stac9205_models[STAC_9205_MODELS] = {
780         [STAC_9205_REF] = "ref",
781 };
782
783 static struct snd_pci_quirk stac9205_cfg_tbl[] = {
784         /* SigmaTel reference board */
785         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
786                       "DFI LanParty", STAC_9205_REF),
787         {} /* terminator */
788 };
789
790 static int stac92xx_save_bios_config_regs(struct hda_codec *codec)
791 {
792         int i;
793         struct sigmatel_spec *spec = codec->spec;
794         
795         if (! spec->bios_pin_configs) {
796                 spec->bios_pin_configs = kcalloc(spec->num_pins,
797                                                  sizeof(*spec->bios_pin_configs), GFP_KERNEL);
798                 if (! spec->bios_pin_configs)
799                         return -ENOMEM;
800         }
801         
802         for (i = 0; i < spec->num_pins; i++) {
803                 hda_nid_t nid = spec->pin_nids[i];
804                 unsigned int pin_cfg;
805                 
806                 pin_cfg = snd_hda_codec_read(codec, nid, 0, 
807                         AC_VERB_GET_CONFIG_DEFAULT, 0x00);      
808                 snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x bios pin config %8.8x\n",
809                                         nid, pin_cfg);
810                 spec->bios_pin_configs[i] = pin_cfg;
811         }
812         
813         return 0;
814 }
815
816 static void stac92xx_set_config_regs(struct hda_codec *codec)
817 {
818         int i;
819         struct sigmatel_spec *spec = codec->spec;
820         unsigned int pin_cfg;
821
822         if (! spec->pin_nids || ! spec->pin_configs)
823                 return;
824
825         for (i = 0; i < spec->num_pins; i++) {
826                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
827                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_0,
828                                     spec->pin_configs[i] & 0x000000ff);
829                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
830                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_1,
831                                     (spec->pin_configs[i] & 0x0000ff00) >> 8);
832                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
833                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_2,
834                                     (spec->pin_configs[i] & 0x00ff0000) >> 16);
835                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
836                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
837                                     spec->pin_configs[i] >> 24);
838                 pin_cfg = snd_hda_codec_read(codec, spec->pin_nids[i], 0,
839                                              AC_VERB_GET_CONFIG_DEFAULT,
840                                              0x00);     
841                 snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x pin config %8.8x\n", spec->pin_nids[i], pin_cfg);
842         }
843 }
844
845 static void stac92xx_enable_eapd(struct hda_codec *codec)
846 {
847         /* Configure GPIO0 as output */
848         snd_hda_codec_write(codec, codec->afg, 0,
849                             AC_VERB_SET_GPIO_DIRECTION, 0x00000001);
850         /* Configure GPIO0 as CMOS */
851         snd_hda_codec_write(codec, codec->afg, 0, 0x7e7, 0x00000000);
852         /* Assert GPIO0 high */
853         snd_hda_codec_write(codec, codec->afg, 0,
854                             AC_VERB_SET_GPIO_DATA, 0x00000001);
855         /* Enable GPIO0 */
856         snd_hda_codec_write(codec, codec->afg, 0,
857                             AC_VERB_SET_GPIO_MASK, 0x00000001);
858 }
859
860 /*
861  * Analog playback callbacks
862  */
863 static int stac92xx_playback_pcm_open(struct hda_pcm_stream *hinfo,
864                                       struct hda_codec *codec,
865                                       struct snd_pcm_substream *substream)
866 {
867         struct sigmatel_spec *spec = codec->spec;
868         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
869 }
870
871 static int stac92xx_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
872                                          struct hda_codec *codec,
873                                          unsigned int stream_tag,
874                                          unsigned int format,
875                                          struct snd_pcm_substream *substream)
876 {
877         struct sigmatel_spec *spec = codec->spec;
878         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag, format, substream);
879 }
880
881 static int stac92xx_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
882                                         struct hda_codec *codec,
883                                         struct snd_pcm_substream *substream)
884 {
885         struct sigmatel_spec *spec = codec->spec;
886         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
887 }
888
889 /*
890  * Digital playback callbacks
891  */
892 static int stac92xx_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
893                                           struct hda_codec *codec,
894                                           struct snd_pcm_substream *substream)
895 {
896         struct sigmatel_spec *spec = codec->spec;
897         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
898 }
899
900 static int stac92xx_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
901                                            struct hda_codec *codec,
902                                            struct snd_pcm_substream *substream)
903 {
904         struct sigmatel_spec *spec = codec->spec;
905         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
906 }
907
908 static int stac92xx_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
909                                          struct hda_codec *codec,
910                                          unsigned int stream_tag,
911                                          unsigned int format,
912                                          struct snd_pcm_substream *substream)
913 {
914         struct sigmatel_spec *spec = codec->spec;
915         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
916                                              stream_tag, format, substream);
917 }
918
919
920 /*
921  * Analog capture callbacks
922  */
923 static int stac92xx_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
924                                         struct hda_codec *codec,
925                                         unsigned int stream_tag,
926                                         unsigned int format,
927                                         struct snd_pcm_substream *substream)
928 {
929         struct sigmatel_spec *spec = codec->spec;
930
931         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
932                                    stream_tag, 0, format);
933         return 0;
934 }
935
936 static int stac92xx_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
937                                         struct hda_codec *codec,
938                                         struct snd_pcm_substream *substream)
939 {
940         struct sigmatel_spec *spec = codec->spec;
941
942         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
943         return 0;
944 }
945
946 static struct hda_pcm_stream stac92xx_pcm_digital_playback = {
947         .substreams = 1,
948         .channels_min = 2,
949         .channels_max = 2,
950         /* NID is set in stac92xx_build_pcms */
951         .ops = {
952                 .open = stac92xx_dig_playback_pcm_open,
953                 .close = stac92xx_dig_playback_pcm_close,
954                 .prepare = stac92xx_dig_playback_pcm_prepare
955         },
956 };
957
958 static struct hda_pcm_stream stac92xx_pcm_digital_capture = {
959         .substreams = 1,
960         .channels_min = 2,
961         .channels_max = 2,
962         /* NID is set in stac92xx_build_pcms */
963 };
964
965 static struct hda_pcm_stream stac92xx_pcm_analog_playback = {
966         .substreams = 1,
967         .channels_min = 2,
968         .channels_max = 8,
969         .nid = 0x02, /* NID to query formats and rates */
970         .ops = {
971                 .open = stac92xx_playback_pcm_open,
972                 .prepare = stac92xx_playback_pcm_prepare,
973                 .cleanup = stac92xx_playback_pcm_cleanup
974         },
975 };
976
977 static struct hda_pcm_stream stac92xx_pcm_analog_alt_playback = {
978         .substreams = 1,
979         .channels_min = 2,
980         .channels_max = 2,
981         .nid = 0x06, /* NID to query formats and rates */
982         .ops = {
983                 .open = stac92xx_playback_pcm_open,
984                 .prepare = stac92xx_playback_pcm_prepare,
985                 .cleanup = stac92xx_playback_pcm_cleanup
986         },
987 };
988
989 static struct hda_pcm_stream stac92xx_pcm_analog_capture = {
990         .substreams = 2,
991         .channels_min = 2,
992         .channels_max = 2,
993         /* NID is set in stac92xx_build_pcms */
994         .ops = {
995                 .prepare = stac92xx_capture_pcm_prepare,
996                 .cleanup = stac92xx_capture_pcm_cleanup
997         },
998 };
999
1000 static int stac92xx_build_pcms(struct hda_codec *codec)
1001 {
1002         struct sigmatel_spec *spec = codec->spec;
1003         struct hda_pcm *info = spec->pcm_rec;
1004
1005         codec->num_pcms = 1;
1006         codec->pcm_info = info;
1007
1008         info->name = "STAC92xx Analog";
1009         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_playback;
1010         info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_analog_capture;
1011         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
1012
1013         if (spec->alt_switch) {
1014                 codec->num_pcms++;
1015                 info++;
1016                 info->name = "STAC92xx Analog Alt";
1017                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_alt_playback;
1018         }
1019
1020         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1021                 codec->num_pcms++;
1022                 info++;
1023                 info->name = "STAC92xx Digital";
1024                 if (spec->multiout.dig_out_nid) {
1025                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_digital_playback;
1026                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
1027                 }
1028                 if (spec->dig_in_nid) {
1029                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_digital_capture;
1030                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
1031                 }
1032         }
1033
1034         return 0;
1035 }
1036
1037 static unsigned int stac92xx_get_vref(struct hda_codec *codec, hda_nid_t nid)
1038 {
1039         unsigned int pincap = snd_hda_param_read(codec, nid,
1040                                                  AC_PAR_PIN_CAP);
1041         pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
1042         if (pincap & AC_PINCAP_VREF_100)
1043                 return AC_PINCTL_VREF_100;
1044         if (pincap & AC_PINCAP_VREF_80)
1045                 return AC_PINCTL_VREF_80;
1046         if (pincap & AC_PINCAP_VREF_50)
1047                 return AC_PINCTL_VREF_50;
1048         if (pincap & AC_PINCAP_VREF_GRD)
1049                 return AC_PINCTL_VREF_GRD;
1050         return 0;
1051 }
1052
1053 static void stac92xx_auto_set_pinctl(struct hda_codec *codec, hda_nid_t nid, int pin_type)
1054
1055 {
1056         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
1057 }
1058
1059 static int stac92xx_io_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1060 {
1061         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1062         uinfo->count = 1;
1063         uinfo->value.integer.min = 0;
1064         uinfo->value.integer.max = 1;
1065         return 0;
1066 }
1067
1068 static int stac92xx_io_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1069 {
1070         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1071         struct sigmatel_spec *spec = codec->spec;
1072         int io_idx = kcontrol-> private_value & 0xff;
1073
1074         ucontrol->value.integer.value[0] = spec->io_switch[io_idx];
1075         return 0;
1076 }
1077
1078 static int stac92xx_io_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1079 {
1080         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1081         struct sigmatel_spec *spec = codec->spec;
1082         hda_nid_t nid = kcontrol->private_value >> 8;
1083         int io_idx = kcontrol-> private_value & 0xff;
1084         unsigned short val = ucontrol->value.integer.value[0];
1085
1086         spec->io_switch[io_idx] = val;
1087
1088         if (val)
1089                 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
1090         else {
1091                 unsigned int pinctl = AC_PINCTL_IN_EN;
1092                 if (io_idx) /* set VREF for mic */
1093                         pinctl |= stac92xx_get_vref(codec, nid);
1094                 stac92xx_auto_set_pinctl(codec, nid, pinctl);
1095         }
1096         return 1;
1097 }
1098
1099 #define STAC_CODEC_IO_SWITCH(xname, xpval) \
1100         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1101           .name = xname, \
1102           .index = 0, \
1103           .info = stac92xx_io_switch_info, \
1104           .get = stac92xx_io_switch_get, \
1105           .put = stac92xx_io_switch_put, \
1106           .private_value = xpval, \
1107         }
1108
1109
1110 enum {
1111         STAC_CTL_WIDGET_VOL,
1112         STAC_CTL_WIDGET_MUTE,
1113         STAC_CTL_WIDGET_IO_SWITCH,
1114 };
1115
1116 static struct snd_kcontrol_new stac92xx_control_templates[] = {
1117         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
1118         HDA_CODEC_MUTE(NULL, 0, 0, 0),
1119         STAC_CODEC_IO_SWITCH(NULL, 0),
1120 };
1121
1122 /* add dynamic controls */
1123 static int stac92xx_add_control(struct sigmatel_spec *spec, int type, const char *name, unsigned long val)
1124 {
1125         struct snd_kcontrol_new *knew;
1126
1127         if (spec->num_kctl_used >= spec->num_kctl_alloc) {
1128                 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
1129
1130                 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
1131                 if (! knew)
1132                         return -ENOMEM;
1133                 if (spec->kctl_alloc) {
1134                         memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
1135                         kfree(spec->kctl_alloc);
1136                 }
1137                 spec->kctl_alloc = knew;
1138                 spec->num_kctl_alloc = num;
1139         }
1140
1141         knew = &spec->kctl_alloc[spec->num_kctl_used];
1142         *knew = stac92xx_control_templates[type];
1143         knew->name = kstrdup(name, GFP_KERNEL);
1144         if (! knew->name)
1145                 return -ENOMEM;
1146         knew->private_value = val;
1147         spec->num_kctl_used++;
1148         return 0;
1149 }
1150
1151 /* flag inputs as additional dynamic lineouts */
1152 static int stac92xx_add_dyn_out_pins(struct hda_codec *codec, struct auto_pin_cfg *cfg)
1153 {
1154         struct sigmatel_spec *spec = codec->spec;
1155         unsigned int wcaps, wtype;
1156         int i, num_dacs = 0;
1157         
1158         /* use the wcaps cache to count all DACs available for line-outs */
1159         for (i = 0; i < codec->num_nodes; i++) {
1160                 wcaps = codec->wcaps[i];
1161                 wtype = (wcaps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
1162                 if (wtype == AC_WID_AUD_OUT && !(wcaps & AC_WCAP_DIGITAL))
1163                         num_dacs++;
1164         }
1165
1166         snd_printdd("%s: total dac count=%d\n", __func__, num_dacs);
1167         
1168         switch (cfg->line_outs) {
1169         case 3:
1170                 /* add line-in as side */
1171                 if (cfg->input_pins[AUTO_PIN_LINE] && num_dacs > 3) {
1172                         cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_LINE];
1173                         spec->line_switch = 1;
1174                         cfg->line_outs++;
1175                 }
1176                 break;
1177         case 2:
1178                 /* add line-in as clfe and mic as side */
1179                 if (cfg->input_pins[AUTO_PIN_LINE] && num_dacs > 2) {
1180                         cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_LINE];
1181                         spec->line_switch = 1;
1182                         cfg->line_outs++;
1183                 }
1184                 if (cfg->input_pins[AUTO_PIN_MIC] && num_dacs > 3) {
1185                         cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_MIC];
1186                         spec->mic_switch = 1;
1187                         cfg->line_outs++;
1188                 }
1189                 break;
1190         case 1:
1191                 /* add line-in as surr and mic as clfe */
1192                 if (cfg->input_pins[AUTO_PIN_LINE] && num_dacs > 1) {
1193                         cfg->line_out_pins[1] = cfg->input_pins[AUTO_PIN_LINE];
1194                         spec->line_switch = 1;
1195                         cfg->line_outs++;
1196                 }
1197                 if (cfg->input_pins[AUTO_PIN_MIC] && num_dacs > 2) {
1198                         cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_MIC];
1199                         spec->mic_switch = 1;
1200                         cfg->line_outs++;
1201                 }
1202                 break;
1203         }
1204
1205         return 0;
1206 }
1207
1208
1209 static int is_in_dac_nids(struct sigmatel_spec *spec, hda_nid_t nid)
1210 {
1211         int i;
1212         
1213         for (i = 0; i < spec->multiout.num_dacs; i++) {
1214                 if (spec->multiout.dac_nids[i] == nid)
1215                         return 1;
1216         }
1217
1218         return 0;
1219 }
1220
1221 /*
1222  * Fill in the dac_nids table from the parsed pin configuration
1223  * This function only works when every pin in line_out_pins[]
1224  * contains atleast one DAC in its connection list. Some 92xx
1225  * codecs are not connected directly to a DAC, such as the 9200
1226  * and 9202/925x. For those, dac_nids[] must be hard-coded.
1227  */
1228 static int stac92xx_auto_fill_dac_nids(struct hda_codec *codec,
1229                                        struct auto_pin_cfg *cfg)
1230 {
1231         struct sigmatel_spec *spec = codec->spec;
1232         int i, j, conn_len = 0; 
1233         hda_nid_t nid, conn[HDA_MAX_CONNECTIONS];
1234         unsigned int wcaps, wtype;
1235         
1236         for (i = 0; i < cfg->line_outs; i++) {
1237                 nid = cfg->line_out_pins[i];
1238                 conn_len = snd_hda_get_connections(codec, nid, conn,
1239                                                    HDA_MAX_CONNECTIONS);
1240                 for (j = 0; j < conn_len; j++) {
1241                         wcaps = snd_hda_param_read(codec, conn[j],
1242                                                    AC_PAR_AUDIO_WIDGET_CAP);
1243                         wtype = (wcaps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
1244
1245                         if (wtype != AC_WID_AUD_OUT ||
1246                             (wcaps & AC_WCAP_DIGITAL))
1247                                 continue;
1248                         /* conn[j] is a DAC routed to this line-out */
1249                         if (!is_in_dac_nids(spec, conn[j]))
1250                                 break;
1251                 }
1252
1253                 if (j == conn_len) {
1254                         if (spec->multiout.num_dacs > 0) {
1255                                 /* we have already working output pins,
1256                                  * so let's drop the broken ones again
1257                                  */
1258                                 cfg->line_outs = spec->multiout.num_dacs;
1259                                 break;
1260                         }
1261                         /* error out, no available DAC found */
1262                         snd_printk(KERN_ERR
1263                                    "%s: No available DAC for pin 0x%x\n",
1264                                    __func__, nid);
1265                         return -ENODEV;
1266                 }
1267
1268                 spec->multiout.dac_nids[i] = conn[j];
1269                 spec->multiout.num_dacs++;
1270                 if (conn_len > 1) {
1271                         /* select this DAC in the pin's input mux */
1272                         snd_hda_codec_write(codec, nid, 0,
1273                                             AC_VERB_SET_CONNECT_SEL, j);
1274
1275                 }
1276         }
1277
1278         snd_printd("dac_nids=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
1279                    spec->multiout.num_dacs,
1280                    spec->multiout.dac_nids[0],
1281                    spec->multiout.dac_nids[1],
1282                    spec->multiout.dac_nids[2],
1283                    spec->multiout.dac_nids[3],
1284                    spec->multiout.dac_nids[4]);
1285         return 0;
1286 }
1287
1288 /* create volume control/switch for the given prefx type */
1289 static int create_controls(struct sigmatel_spec *spec, const char *pfx, hda_nid_t nid, int chs)
1290 {
1291         char name[32];
1292         int err;
1293
1294         sprintf(name, "%s Playback Volume", pfx);
1295         err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, name,
1296                                    HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1297         if (err < 0)
1298                 return err;
1299         sprintf(name, "%s Playback Switch", pfx);
1300         err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, name,
1301                                    HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1302         if (err < 0)
1303                 return err;
1304         return 0;
1305 }
1306
1307 /* add playback controls from the parsed DAC table */
1308 static int stac92xx_auto_create_multi_out_ctls(struct sigmatel_spec *spec,
1309                                                const struct auto_pin_cfg *cfg)
1310 {
1311         static const char *chname[4] = {
1312                 "Front", "Surround", NULL /*CLFE*/, "Side"
1313         };
1314         hda_nid_t nid;
1315         int i, err;
1316
1317         for (i = 0; i < cfg->line_outs; i++) {
1318                 if (!spec->multiout.dac_nids[i])
1319                         continue;
1320
1321                 nid = spec->multiout.dac_nids[i];
1322
1323                 if (i == 2) {
1324                         /* Center/LFE */
1325                         err = create_controls(spec, "Center", nid, 1);
1326                         if (err < 0)
1327                                 return err;
1328                         err = create_controls(spec, "LFE", nid, 2);
1329                         if (err < 0)
1330                                 return err;
1331                 } else {
1332                         err = create_controls(spec, chname[i], nid, 3);
1333                         if (err < 0)
1334                                 return err;
1335                 }
1336         }
1337
1338         if (spec->line_switch)
1339                 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Line In as Output Switch", cfg->input_pins[AUTO_PIN_LINE] << 8)) < 0)
1340                         return err;
1341
1342         if (spec->mic_switch)
1343                 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Mic as Output Switch", (cfg->input_pins[AUTO_PIN_MIC] << 8) | 1)) < 0)
1344                         return err;
1345
1346         return 0;
1347 }
1348
1349 static int check_in_dac_nids(struct sigmatel_spec *spec, hda_nid_t nid)
1350 {
1351         if (is_in_dac_nids(spec, nid))
1352                 return 1;
1353         if (spec->multiout.hp_nid == nid)
1354                 return 1;
1355         return 0;
1356 }
1357
1358 static int add_spec_dacs(struct sigmatel_spec *spec, hda_nid_t nid)
1359 {
1360         if (!spec->multiout.hp_nid)
1361                 spec->multiout.hp_nid = nid;
1362         else if (spec->multiout.num_dacs > 4) {
1363                 printk(KERN_WARNING "stac92xx: No space for DAC 0x%x\n", nid);
1364                 return 1;
1365         } else {
1366                 spec->multiout.dac_nids[spec->multiout.num_dacs] = nid;
1367                 spec->multiout.num_dacs++;
1368         }
1369         return 0;
1370 }
1371
1372 /* add playback controls for Speaker and HP outputs */
1373 static int stac92xx_auto_create_hp_ctls(struct hda_codec *codec,
1374                                         struct auto_pin_cfg *cfg)
1375 {
1376         struct sigmatel_spec *spec = codec->spec;
1377         hda_nid_t nid;
1378         int i, old_num_dacs, err;
1379
1380         old_num_dacs = spec->multiout.num_dacs;
1381         for (i = 0; i < cfg->hp_outs; i++) {
1382                 unsigned int wid_caps = get_wcaps(codec, cfg->hp_pins[i]);
1383                 if (wid_caps & AC_WCAP_UNSOL_CAP)
1384                         spec->hp_detect = 1;
1385                 nid = snd_hda_codec_read(codec, cfg->hp_pins[i], 0,
1386                                          AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
1387                 if (check_in_dac_nids(spec, nid))
1388                         nid = 0;
1389                 if (! nid)
1390                         continue;
1391                 add_spec_dacs(spec, nid);
1392         }
1393         for (i = 0; i < cfg->speaker_outs; i++) {
1394                 nid = snd_hda_codec_read(codec, cfg->speaker_pins[i], 0,
1395                                          AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
1396                 if (check_in_dac_nids(spec, nid))
1397                         nid = 0;
1398                 if (! nid)
1399                         continue;
1400                 add_spec_dacs(spec, nid);
1401         }
1402         for (i = 0; i < cfg->line_outs; i++) {
1403                 nid = snd_hda_codec_read(codec, cfg->line_out_pins[i], 0,
1404                                         AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
1405                 if (check_in_dac_nids(spec, nid))
1406                         nid = 0;
1407                 if (! nid)
1408                         continue;
1409                 add_spec_dacs(spec, nid);
1410         }
1411         for (i = old_num_dacs; i < spec->multiout.num_dacs; i++) {
1412                 static const char *pfxs[] = {
1413                         "Speaker", "External Speaker", "Speaker2",
1414                 };
1415                 err = create_controls(spec, pfxs[i - old_num_dacs],
1416                                       spec->multiout.dac_nids[i], 3);
1417                 if (err < 0)
1418                         return err;
1419         }
1420         if (spec->multiout.hp_nid) {
1421                 const char *pfx;
1422                 if (old_num_dacs == spec->multiout.num_dacs)
1423                         pfx = "Master";
1424                 else
1425                         pfx = "Headphone";
1426                 err = create_controls(spec, pfx, spec->multiout.hp_nid, 3);
1427                 if (err < 0)
1428                         return err;
1429         }
1430
1431         return 0;
1432 }
1433
1434 /* labels for dmic mux inputs */
1435 static const char *stac92xx_dmic_labels[5] = {
1436         "Analog Inputs", "Digital Mic 1", "Digital Mic 2",
1437         "Digital Mic 3", "Digital Mic 4"
1438 };
1439
1440 /* create playback/capture controls for input pins on dmic capable codecs */
1441 static int stac92xx_auto_create_dmic_input_ctls(struct hda_codec *codec,
1442                                                 const struct auto_pin_cfg *cfg)
1443 {
1444         struct sigmatel_spec *spec = codec->spec;
1445         struct hda_input_mux *dimux = &spec->private_dimux;
1446         hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
1447         int i, j;
1448
1449         dimux->items[dimux->num_items].label = stac92xx_dmic_labels[0];
1450         dimux->items[dimux->num_items].index = 0;
1451         dimux->num_items++;
1452
1453         for (i = 0; i < spec->num_dmics; i++) {
1454                 int index;
1455                 int num_cons;
1456                 unsigned int def_conf;
1457
1458                 def_conf = snd_hda_codec_read(codec,
1459                                               spec->dmic_nids[i],
1460                                               0,
1461                                               AC_VERB_GET_CONFIG_DEFAULT,
1462                                               0);
1463                 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
1464                         continue;
1465
1466                 num_cons = snd_hda_get_connections(codec,
1467                                 spec->dmux_nid,
1468                                 con_lst,
1469                                 HDA_MAX_NUM_INPUTS);
1470                 for (j = 0; j < num_cons; j++)
1471                         if (con_lst[j] == spec->dmic_nids[i]) {
1472                                 index = j;
1473                                 goto found;
1474                         }
1475                 continue;
1476 found:
1477                 dimux->items[dimux->num_items].label =
1478                         stac92xx_dmic_labels[dimux->num_items];
1479                 dimux->items[dimux->num_items].index = index;
1480                 dimux->num_items++;
1481         }
1482
1483         return 0;
1484 }
1485
1486 /* create playback/capture controls for input pins */
1487 static int stac92xx_auto_create_analog_input_ctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
1488 {
1489         struct sigmatel_spec *spec = codec->spec;
1490         struct hda_input_mux *imux = &spec->private_imux;
1491         hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
1492         int i, j, k;
1493
1494         for (i = 0; i < AUTO_PIN_LAST; i++) {
1495                 int index;
1496
1497                 if (!cfg->input_pins[i])
1498                         continue;
1499                 index = -1;
1500                 for (j = 0; j < spec->num_muxes; j++) {
1501                         int num_cons;
1502                         num_cons = snd_hda_get_connections(codec,
1503                                                            spec->mux_nids[j],
1504                                                            con_lst,
1505                                                            HDA_MAX_NUM_INPUTS);
1506                         for (k = 0; k < num_cons; k++)
1507                                 if (con_lst[k] == cfg->input_pins[i]) {
1508                                         index = k;
1509                                         goto found;
1510                                 }
1511                 }
1512                 continue;
1513         found:
1514                 imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
1515                 imux->items[imux->num_items].index = index;
1516                 imux->num_items++;
1517         }
1518
1519         if (imux->num_items) {
1520                 /*
1521                  * Set the current input for the muxes.
1522                  * The STAC9221 has two input muxes with identical source
1523                  * NID lists.  Hopefully this won't get confused.
1524                  */
1525                 for (i = 0; i < spec->num_muxes; i++) {
1526                         snd_hda_codec_write(codec, spec->mux_nids[i], 0,
1527                                             AC_VERB_SET_CONNECT_SEL,
1528                                             imux->items[0].index);
1529                 }
1530         }
1531
1532         return 0;
1533 }
1534
1535 static void stac92xx_auto_init_multi_out(struct hda_codec *codec)
1536 {
1537         struct sigmatel_spec *spec = codec->spec;
1538         int i;
1539
1540         for (i = 0; i < spec->autocfg.line_outs; i++) {
1541                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
1542                 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
1543         }
1544 }
1545
1546 static void stac92xx_auto_init_hp_out(struct hda_codec *codec)
1547 {
1548         struct sigmatel_spec *spec = codec->spec;
1549         int i;
1550
1551         for (i = 0; i < spec->autocfg.hp_outs; i++) {
1552                 hda_nid_t pin;
1553                 pin = spec->autocfg.hp_pins[i];
1554                 if (pin) /* connect to front */
1555                         stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
1556         }
1557         for (i = 0; i < spec->autocfg.speaker_outs; i++) {
1558                 hda_nid_t pin;
1559                 pin = spec->autocfg.speaker_pins[i];
1560                 if (pin) /* connect to front */
1561                         stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN);
1562         }
1563 }
1564
1565 static int stac92xx_parse_auto_config(struct hda_codec *codec, hda_nid_t dig_out, hda_nid_t dig_in)
1566 {
1567         struct sigmatel_spec *spec = codec->spec;
1568         int err;
1569
1570         if ((err = snd_hda_parse_pin_def_config(codec,
1571                                                 &spec->autocfg,
1572                                                 spec->dmic_nids)) < 0)
1573                 return err;
1574         if (! spec->autocfg.line_outs)
1575                 return 0; /* can't find valid pin config */
1576
1577         if ((err = stac92xx_add_dyn_out_pins(codec, &spec->autocfg)) < 0)
1578                 return err;
1579         if (spec->multiout.num_dacs == 0)
1580                 if ((err = stac92xx_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
1581                         return err;
1582
1583         if ((err = stac92xx_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
1584             (err = stac92xx_auto_create_hp_ctls(codec, &spec->autocfg)) < 0 ||
1585             (err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
1586                 return err;
1587
1588         if (spec->num_dmics > 0)
1589                 if ((err = stac92xx_auto_create_dmic_input_ctls(codec,
1590                                                 &spec->autocfg)) < 0)
1591                         return err;
1592
1593         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
1594         if (spec->multiout.max_channels > 2)
1595                 spec->surr_switch = 1;
1596
1597         if (spec->autocfg.dig_out_pin)
1598                 spec->multiout.dig_out_nid = dig_out;
1599         if (spec->autocfg.dig_in_pin)
1600                 spec->dig_in_nid = dig_in;
1601
1602         if (spec->kctl_alloc)
1603                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
1604
1605         spec->input_mux = &spec->private_imux;
1606         spec->dinput_mux = &spec->private_dimux;
1607
1608         return 1;
1609 }
1610
1611 /* add playback controls for HP output */
1612 static int stac9200_auto_create_hp_ctls(struct hda_codec *codec,
1613                                         struct auto_pin_cfg *cfg)
1614 {
1615         struct sigmatel_spec *spec = codec->spec;
1616         hda_nid_t pin = cfg->hp_pins[0];
1617         unsigned int wid_caps;
1618
1619         if (! pin)
1620                 return 0;
1621
1622         wid_caps = get_wcaps(codec, pin);
1623         if (wid_caps & AC_WCAP_UNSOL_CAP)
1624                 spec->hp_detect = 1;
1625
1626         return 0;
1627 }
1628
1629 /* add playback controls for LFE output */
1630 static int stac9200_auto_create_lfe_ctls(struct hda_codec *codec,
1631                                         struct auto_pin_cfg *cfg)
1632 {
1633         struct sigmatel_spec *spec = codec->spec;
1634         int err;
1635         hda_nid_t lfe_pin = 0x0;
1636         int i;
1637
1638         /*
1639          * search speaker outs and line outs for a mono speaker pin
1640          * with an amp.  If one is found, add LFE controls
1641          * for it.
1642          */
1643         for (i = 0; i < spec->autocfg.speaker_outs && lfe_pin == 0x0; i++) {
1644                 hda_nid_t pin = spec->autocfg.speaker_pins[i];
1645                 unsigned long wcaps = get_wcaps(codec, pin);
1646                 wcaps &= (AC_WCAP_STEREO | AC_WCAP_OUT_AMP);
1647                 if (wcaps == AC_WCAP_OUT_AMP)
1648                         /* found a mono speaker with an amp, must be lfe */
1649                         lfe_pin = pin;
1650         }
1651
1652         /* if speaker_outs is 0, then speakers may be in line_outs */
1653         if (lfe_pin == 0 && spec->autocfg.speaker_outs == 0) {
1654                 for (i = 0; i < spec->autocfg.line_outs && lfe_pin == 0x0; i++) {
1655                         hda_nid_t pin = spec->autocfg.line_out_pins[i];
1656                         unsigned long cfg;
1657                         cfg = snd_hda_codec_read(codec, pin, 0,
1658                                                  AC_VERB_GET_CONFIG_DEFAULT,
1659                                                  0x00);
1660                         if (get_defcfg_device(cfg) == AC_JACK_SPEAKER) {
1661                                 unsigned long wcaps = get_wcaps(codec, pin);
1662                                 wcaps &= (AC_WCAP_STEREO | AC_WCAP_OUT_AMP);
1663                                 if (wcaps == AC_WCAP_OUT_AMP)
1664                                         /* found a mono speaker with an amp,
1665                                            must be lfe */
1666                                         lfe_pin = pin;
1667                         }
1668                 }
1669         }
1670
1671         if (lfe_pin) {
1672                 err = create_controls(spec, "LFE", lfe_pin, 1);
1673                 if (err < 0)
1674                         return err;
1675         }
1676
1677         return 0;
1678 }
1679
1680 static int stac9200_parse_auto_config(struct hda_codec *codec)
1681 {
1682         struct sigmatel_spec *spec = codec->spec;
1683         int err;
1684
1685         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
1686                 return err;
1687
1688         if ((err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
1689                 return err;
1690
1691         if ((err = stac9200_auto_create_hp_ctls(codec, &spec->autocfg)) < 0)
1692                 return err;
1693
1694         if ((err = stac9200_auto_create_lfe_ctls(codec, &spec->autocfg)) < 0)
1695                 return err;
1696
1697         if (spec->autocfg.dig_out_pin)
1698                 spec->multiout.dig_out_nid = 0x05;
1699         if (spec->autocfg.dig_in_pin)
1700                 spec->dig_in_nid = 0x04;
1701
1702         if (spec->kctl_alloc)
1703                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
1704
1705         spec->input_mux = &spec->private_imux;
1706         spec->dinput_mux = &spec->private_dimux;
1707
1708         return 1;
1709 }
1710
1711 /*
1712  * Early 2006 Intel Macintoshes with STAC9220X5 codecs seem to have a
1713  * funky external mute control using GPIO pins.
1714  */
1715
1716 static void stac922x_gpio_mute(struct hda_codec *codec, int pin, int muted)
1717 {
1718         unsigned int gpiostate, gpiomask, gpiodir;
1719
1720         gpiostate = snd_hda_codec_read(codec, codec->afg, 0,
1721                                        AC_VERB_GET_GPIO_DATA, 0);
1722
1723         if (!muted)
1724                 gpiostate |= (1 << pin);
1725         else
1726                 gpiostate &= ~(1 << pin);
1727
1728         gpiomask = snd_hda_codec_read(codec, codec->afg, 0,
1729                                       AC_VERB_GET_GPIO_MASK, 0);
1730         gpiomask |= (1 << pin);
1731
1732         gpiodir = snd_hda_codec_read(codec, codec->afg, 0,
1733                                      AC_VERB_GET_GPIO_DIRECTION, 0);
1734         gpiodir |= (1 << pin);
1735
1736         /* AppleHDA seems to do this -- WTF is this verb?? */
1737         snd_hda_codec_write(codec, codec->afg, 0, 0x7e7, 0);
1738
1739         snd_hda_codec_write(codec, codec->afg, 0,
1740                             AC_VERB_SET_GPIO_MASK, gpiomask);
1741         snd_hda_codec_write(codec, codec->afg, 0,
1742                             AC_VERB_SET_GPIO_DIRECTION, gpiodir);
1743
1744         msleep(1);
1745
1746         snd_hda_codec_write(codec, codec->afg, 0,
1747                             AC_VERB_SET_GPIO_DATA, gpiostate);
1748 }
1749
1750 static void enable_pin_detect(struct hda_codec *codec, hda_nid_t nid,
1751                               unsigned int event)
1752 {
1753         if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP)
1754                 snd_hda_codec_write(codec, nid, 0,
1755                                     AC_VERB_SET_UNSOLICITED_ENABLE,
1756                                     (AC_USRSP_EN | event));
1757 }
1758
1759 static int stac92xx_init(struct hda_codec *codec)
1760 {
1761         struct sigmatel_spec *spec = codec->spec;
1762         struct auto_pin_cfg *cfg = &spec->autocfg;
1763         int i;
1764
1765         snd_hda_sequence_write(codec, spec->init);
1766
1767         /* set up pins */
1768         if (spec->hp_detect) {
1769                 /* Enable unsolicited responses on the HP widget */
1770                 for (i = 0; i < cfg->hp_outs; i++)
1771                         enable_pin_detect(codec, cfg->hp_pins[i],
1772                                           STAC_HP_EVENT);
1773                 /* force to enable the first line-out; the others are set up
1774                  * in unsol_event
1775                  */
1776                 stac92xx_auto_set_pinctl(codec, spec->autocfg.line_out_pins[0],
1777                                          AC_PINCTL_OUT_EN);
1778                 stac92xx_auto_init_hp_out(codec);
1779                 /* fake event to set up pins */
1780                 codec->patch_ops.unsol_event(codec, STAC_HP_EVENT << 26);
1781         } else {
1782                 stac92xx_auto_init_multi_out(codec);
1783                 stac92xx_auto_init_hp_out(codec);
1784         }
1785         for (i = 0; i < AUTO_PIN_LAST; i++) {
1786                 hda_nid_t nid = cfg->input_pins[i];
1787                 if (nid) {
1788                         unsigned int pinctl = AC_PINCTL_IN_EN;
1789                         if (i == AUTO_PIN_MIC || i == AUTO_PIN_FRONT_MIC)
1790                                 pinctl |= stac92xx_get_vref(codec, nid);
1791                         stac92xx_auto_set_pinctl(codec, nid, pinctl);
1792                 }
1793         }
1794         if (spec->num_dmics > 0)
1795                 for (i = 0; i < spec->num_dmics; i++)
1796                         stac92xx_auto_set_pinctl(codec, spec->dmic_nids[i],
1797                                                  AC_PINCTL_IN_EN);
1798
1799         if (cfg->dig_out_pin)
1800                 stac92xx_auto_set_pinctl(codec, cfg->dig_out_pin,
1801                                          AC_PINCTL_OUT_EN);
1802         if (cfg->dig_in_pin)
1803                 stac92xx_auto_set_pinctl(codec, cfg->dig_in_pin,
1804                                          AC_PINCTL_IN_EN);
1805
1806         if (spec->gpio_mute) {
1807                 stac922x_gpio_mute(codec, 0, 0);
1808                 stac922x_gpio_mute(codec, 1, 0);
1809         }
1810
1811         return 0;
1812 }
1813
1814 static void stac92xx_free(struct hda_codec *codec)
1815 {
1816         struct sigmatel_spec *spec = codec->spec;
1817         int i;
1818
1819         if (! spec)
1820                 return;
1821
1822         if (spec->kctl_alloc) {
1823                 for (i = 0; i < spec->num_kctl_used; i++)
1824                         kfree(spec->kctl_alloc[i].name);
1825                 kfree(spec->kctl_alloc);
1826         }
1827
1828         if (spec->bios_pin_configs)
1829                 kfree(spec->bios_pin_configs);
1830
1831         kfree(spec);
1832 }
1833
1834 static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
1835                                 unsigned int flag)
1836 {
1837         unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
1838                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
1839
1840         if (pin_ctl & AC_PINCTL_IN_EN) {
1841                 /*
1842                  * we need to check the current set-up direction of
1843                  * shared input pins since they can be switched via
1844                  * "xxx as Output" mixer switch
1845                  */
1846                 struct sigmatel_spec *spec = codec->spec;
1847                 struct auto_pin_cfg *cfg = &spec->autocfg;
1848                 if ((nid == cfg->input_pins[AUTO_PIN_LINE] &&
1849                      spec->line_switch) ||
1850                     (nid == cfg->input_pins[AUTO_PIN_MIC] &&
1851                      spec->mic_switch))
1852                         return;
1853         }
1854
1855         /* if setting pin direction bits, clear the current
1856            direction bits first */
1857         if (flag & (AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN))
1858                 pin_ctl &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
1859         
1860         snd_hda_codec_write(codec, nid, 0,
1861                         AC_VERB_SET_PIN_WIDGET_CONTROL,
1862                         pin_ctl | flag);
1863 }
1864
1865 static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
1866                                   unsigned int flag)
1867 {
1868         unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
1869                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
1870         snd_hda_codec_write(codec, nid, 0,
1871                         AC_VERB_SET_PIN_WIDGET_CONTROL,
1872                         pin_ctl & ~flag);
1873 }
1874
1875 static int get_pin_presence(struct hda_codec *codec, hda_nid_t nid)
1876 {
1877         if (!nid)
1878                 return 0;
1879         if (snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_PIN_SENSE, 0x00)
1880             & (1 << 31))
1881                 return 1;
1882         return 0;
1883 }
1884
1885 static void stac92xx_hp_detect(struct hda_codec *codec, unsigned int res)
1886 {
1887         struct sigmatel_spec *spec = codec->spec;
1888         struct auto_pin_cfg *cfg = &spec->autocfg;
1889         int i, presence;
1890
1891         presence = 0;
1892         for (i = 0; i < cfg->hp_outs; i++) {
1893                 presence = get_pin_presence(codec, cfg->hp_pins[i]);
1894                 if (presence)
1895                         break;
1896         }
1897
1898         if (presence) {
1899                 /* disable lineouts, enable hp */
1900                 for (i = 0; i < cfg->line_outs; i++)
1901                         stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
1902                                                 AC_PINCTL_OUT_EN);
1903                 for (i = 0; i < cfg->speaker_outs; i++)
1904                         stac92xx_reset_pinctl(codec, cfg->speaker_pins[i],
1905                                                 AC_PINCTL_OUT_EN);
1906         } else {
1907                 /* enable lineouts, disable hp */
1908                 for (i = 0; i < cfg->line_outs; i++)
1909                         stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
1910                                                 AC_PINCTL_OUT_EN);
1911                 for (i = 0; i < cfg->speaker_outs; i++)
1912                         stac92xx_set_pinctl(codec, cfg->speaker_pins[i],
1913                                                 AC_PINCTL_OUT_EN);
1914         }
1915
1916
1917 static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
1918 {
1919         switch (res >> 26) {
1920         case STAC_HP_EVENT:
1921                 stac92xx_hp_detect(codec, res);
1922                 break;
1923         }
1924 }
1925
1926 #ifdef CONFIG_PM
1927 static int stac92xx_resume(struct hda_codec *codec)
1928 {
1929         struct sigmatel_spec *spec = codec->spec;
1930         int i;
1931
1932         stac92xx_init(codec);
1933         stac92xx_set_config_regs(codec);
1934         snd_hda_resume_ctls(codec, spec->mixer);
1935         for (i = 0; i < spec->num_mixers; i++)
1936                 snd_hda_resume_ctls(codec, spec->mixers[i]);
1937         if (spec->multiout.dig_out_nid)
1938                 snd_hda_resume_spdif_out(codec);
1939         if (spec->dig_in_nid)
1940                 snd_hda_resume_spdif_in(codec);
1941
1942         return 0;
1943 }
1944 #endif
1945
1946 static struct hda_codec_ops stac92xx_patch_ops = {
1947         .build_controls = stac92xx_build_controls,
1948         .build_pcms = stac92xx_build_pcms,
1949         .init = stac92xx_init,
1950         .free = stac92xx_free,
1951         .unsol_event = stac92xx_unsol_event,
1952 #ifdef CONFIG_PM
1953         .resume = stac92xx_resume,
1954 #endif
1955 };
1956
1957 static int patch_stac9200(struct hda_codec *codec)
1958 {
1959         struct sigmatel_spec *spec;
1960         int err;
1961
1962         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1963         if (spec == NULL)
1964                 return -ENOMEM;
1965
1966         codec->spec = spec;
1967         spec->num_pins = ARRAY_SIZE(stac9200_pin_nids);
1968         spec->pin_nids = stac9200_pin_nids;
1969         spec->board_config = snd_hda_check_board_config(codec, STAC_9200_MODELS,
1970                                                         stac9200_models,
1971                                                         stac9200_cfg_tbl);
1972         if (spec->board_config < 0) {
1973                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9200, using BIOS defaults\n");
1974                 err = stac92xx_save_bios_config_regs(codec);
1975                 if (err < 0) {
1976                         stac92xx_free(codec);
1977                         return err;
1978                 }
1979                 spec->pin_configs = spec->bios_pin_configs;
1980         } else {
1981                 spec->pin_configs = stac9200_brd_tbl[spec->board_config];
1982                 stac92xx_set_config_regs(codec);
1983         }
1984
1985         spec->multiout.max_channels = 2;
1986         spec->multiout.num_dacs = 1;
1987         spec->multiout.dac_nids = stac9200_dac_nids;
1988         spec->adc_nids = stac9200_adc_nids;
1989         spec->mux_nids = stac9200_mux_nids;
1990         spec->num_muxes = 1;
1991         spec->num_dmics = 0;
1992
1993         spec->init = stac9200_core_init;
1994         spec->mixer = stac9200_mixer;
1995
1996         err = stac9200_parse_auto_config(codec);
1997         if (err < 0) {
1998                 stac92xx_free(codec);
1999                 return err;
2000         }
2001
2002         codec->patch_ops = stac92xx_patch_ops;
2003
2004         return 0;
2005 }
2006
2007 static int patch_stac925x(struct hda_codec *codec)
2008 {
2009         struct sigmatel_spec *spec;
2010         int err;
2011
2012         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2013         if (spec == NULL)
2014                 return -ENOMEM;
2015
2016         codec->spec = spec;
2017         spec->num_pins = ARRAY_SIZE(stac925x_pin_nids);
2018         spec->pin_nids = stac925x_pin_nids;
2019         spec->board_config = snd_hda_check_board_config(codec, STAC_925x_MODELS,
2020                                                         stac925x_models,
2021                                                         stac925x_cfg_tbl);
2022  again:
2023         if (spec->board_config < 0) {
2024                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC925x," 
2025                                       "using BIOS defaults\n");
2026                 err = stac92xx_save_bios_config_regs(codec);
2027                 if (err < 0) {
2028                         stac92xx_free(codec);
2029                         return err;
2030                 }
2031                 spec->pin_configs = spec->bios_pin_configs;
2032         } else if (stac925x_brd_tbl[spec->board_config] != NULL){
2033                 spec->pin_configs = stac925x_brd_tbl[spec->board_config];
2034                 stac92xx_set_config_regs(codec);
2035         }
2036
2037         spec->multiout.max_channels = 2;
2038         spec->multiout.num_dacs = 1;
2039         spec->multiout.dac_nids = stac925x_dac_nids;
2040         spec->adc_nids = stac925x_adc_nids;
2041         spec->mux_nids = stac925x_mux_nids;
2042         spec->num_muxes = 1;
2043         switch (codec->vendor_id) {
2044         case 0x83847632: /* STAC9202  */
2045         case 0x83847633: /* STAC9202D */
2046         case 0x83847636: /* STAC9251  */
2047         case 0x83847637: /* STAC9251D */
2048                 spec->num_dmics = 1;
2049                 spec->dmic_nids = stac925x_dmic_nids;
2050                 break;
2051         default:
2052                 spec->num_dmics = 0;
2053                 break;
2054         }
2055
2056         spec->init = stac925x_core_init;
2057         spec->mixer = stac925x_mixer;
2058
2059         err = stac92xx_parse_auto_config(codec, 0x8, 0x7);
2060         if (!err) {
2061                 if (spec->board_config < 0) {
2062                         printk(KERN_WARNING "hda_codec: No auto-config is "
2063                                "available, default to model=ref\n");
2064                         spec->board_config = STAC_925x_REF;
2065                         goto again;
2066                 }
2067                 err = -EINVAL;
2068         }
2069         if (err < 0) {
2070                 stac92xx_free(codec);
2071                 return err;
2072         }
2073
2074         codec->patch_ops = stac92xx_patch_ops;
2075
2076         return 0;
2077 }
2078
2079 static int patch_stac922x(struct hda_codec *codec)
2080 {
2081         struct sigmatel_spec *spec;
2082         int err;
2083
2084         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2085         if (spec == NULL)
2086                 return -ENOMEM;
2087
2088         codec->spec = spec;
2089         spec->num_pins = ARRAY_SIZE(stac922x_pin_nids);
2090         spec->pin_nids = stac922x_pin_nids;
2091         spec->board_config = snd_hda_check_board_config(codec, STAC_922X_MODELS,
2092                                                         stac922x_models,
2093                                                         stac922x_cfg_tbl);
2094         if (spec->board_config == STAC_INTEL_MAC_V3) {
2095                 spec->gpio_mute = 1;
2096                 /* Intel Macs have all same PCI SSID, so we need to check
2097                  * codec SSID to distinguish the exact models
2098                  */
2099                 printk(KERN_INFO "hda_codec: STAC922x, Apple subsys_id=%x\n", codec->subsystem_id);
2100                 switch (codec->subsystem_id) {
2101
2102                 case 0x106b0800:
2103                         spec->board_config = STAC_INTEL_MAC_V1;
2104                         break;
2105                 case 0x106b0600:
2106                 case 0x106b0700:
2107                         spec->board_config = STAC_INTEL_MAC_V2;
2108                         break;
2109                 case 0x106b0e00:
2110                 case 0x106b0f00:
2111                 case 0x106b1600:
2112                 case 0x106b1700:
2113                 case 0x106b0200:
2114                 case 0x106b1e00:
2115                         spec->board_config = STAC_INTEL_MAC_V3;
2116                         break;
2117                 case 0x106b1a00:
2118                 case 0x00000100:
2119                         spec->board_config = STAC_INTEL_MAC_V4;
2120                         break;
2121                 case 0x106b0a00:
2122                 case 0x106b2200:
2123                         spec->board_config = STAC_INTEL_MAC_V5;
2124                         break;
2125                 }
2126         }
2127
2128  again:
2129         if (spec->board_config < 0) {
2130                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC922x, "
2131                         "using BIOS defaults\n");
2132                 err = stac92xx_save_bios_config_regs(codec);
2133                 if (err < 0) {
2134                         stac92xx_free(codec);
2135                         return err;
2136                 }
2137                 spec->pin_configs = spec->bios_pin_configs;
2138         } else if (stac922x_brd_tbl[spec->board_config] != NULL) {
2139                 spec->pin_configs = stac922x_brd_tbl[spec->board_config];
2140                 stac92xx_set_config_regs(codec);
2141         }
2142
2143         spec->adc_nids = stac922x_adc_nids;
2144         spec->mux_nids = stac922x_mux_nids;
2145         spec->num_muxes = ARRAY_SIZE(stac922x_mux_nids);
2146         spec->num_dmics = 0;
2147
2148         spec->init = stac922x_core_init;
2149         spec->mixer = stac922x_mixer;
2150
2151         spec->multiout.dac_nids = spec->dac_nids;
2152         
2153         err = stac92xx_parse_auto_config(codec, 0x08, 0x09);
2154         if (!err) {
2155                 if (spec->board_config < 0) {
2156                         printk(KERN_WARNING "hda_codec: No auto-config is "
2157                                "available, default to model=ref\n");
2158                         spec->board_config = STAC_D945_REF;
2159                         goto again;
2160                 }
2161                 err = -EINVAL;
2162         }
2163         if (err < 0) {
2164                 stac92xx_free(codec);
2165                 return err;
2166         }
2167
2168         codec->patch_ops = stac92xx_patch_ops;
2169
2170         /* Fix Mux capture level; max to 2 */
2171         snd_hda_override_amp_caps(codec, 0x12, HDA_OUTPUT,
2172                                   (0 << AC_AMPCAP_OFFSET_SHIFT) |
2173                                   (2 << AC_AMPCAP_NUM_STEPS_SHIFT) |
2174                                   (0x27 << AC_AMPCAP_STEP_SIZE_SHIFT) |
2175                                   (0 << AC_AMPCAP_MUTE_SHIFT));
2176
2177         return 0;
2178 }
2179
2180 static int patch_stac927x(struct hda_codec *codec)
2181 {
2182         struct sigmatel_spec *spec;
2183         int err;
2184
2185         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2186         if (spec == NULL)
2187                 return -ENOMEM;
2188
2189         codec->spec = spec;
2190         spec->num_pins = ARRAY_SIZE(stac927x_pin_nids);
2191         spec->pin_nids = stac927x_pin_nids;
2192         spec->board_config = snd_hda_check_board_config(codec, STAC_927X_MODELS,
2193                                                         stac927x_models,
2194                                                         stac927x_cfg_tbl);
2195  again:
2196         if (spec->board_config < 0) {
2197                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC927x, using BIOS defaults\n");
2198                 err = stac92xx_save_bios_config_regs(codec);
2199                 if (err < 0) {
2200                         stac92xx_free(codec);
2201                         return err;
2202                 }
2203                 spec->pin_configs = spec->bios_pin_configs;
2204         } else if (stac927x_brd_tbl[spec->board_config] != NULL) {
2205                 spec->pin_configs = stac927x_brd_tbl[spec->board_config];
2206                 stac92xx_set_config_regs(codec);
2207         }
2208
2209         switch (spec->board_config) {
2210         case STAC_D965_3ST:
2211                 spec->adc_nids = stac927x_adc_nids;
2212                 spec->mux_nids = stac927x_mux_nids;
2213                 spec->num_muxes = ARRAY_SIZE(stac927x_mux_nids);
2214                 spec->num_dmics = 0;
2215                 spec->init = d965_core_init;
2216                 spec->mixer = stac9227_mixer;
2217                 break;
2218         case STAC_D965_5ST:
2219                 spec->adc_nids = stac927x_adc_nids;
2220                 spec->mux_nids = stac927x_mux_nids;
2221                 spec->num_muxes = ARRAY_SIZE(stac927x_mux_nids);
2222                 spec->num_dmics = 0;
2223                 spec->init = d965_core_init;
2224                 spec->mixer = stac9227_mixer;
2225                 break;
2226         default:
2227                 spec->adc_nids = stac927x_adc_nids;
2228                 spec->mux_nids = stac927x_mux_nids;
2229                 spec->num_muxes = ARRAY_SIZE(stac927x_mux_nids);
2230                 spec->num_dmics = 0;
2231                 spec->init = stac927x_core_init;
2232                 spec->mixer = stac927x_mixer;
2233         }
2234
2235         spec->multiout.dac_nids = spec->dac_nids;
2236         stac92xx_enable_eapd(codec);
2237         
2238         err = stac92xx_parse_auto_config(codec, 0x1e, 0x20);
2239         if (!err) {
2240                 if (spec->board_config < 0) {
2241                         printk(KERN_WARNING "hda_codec: No auto-config is "
2242                                "available, default to model=ref\n");
2243                         spec->board_config = STAC_D965_REF;
2244                         goto again;
2245                 }
2246                 err = -EINVAL;
2247         }
2248         if (err < 0) {
2249                 stac92xx_free(codec);
2250                 return err;
2251         }
2252
2253         codec->patch_ops = stac92xx_patch_ops;
2254
2255         /* Fix Mux capture level; max to 2 */
2256         snd_hda_override_amp_caps(codec, 0x12, HDA_OUTPUT,
2257                                   (0 << AC_AMPCAP_OFFSET_SHIFT) |
2258                                   (2 << AC_AMPCAP_NUM_STEPS_SHIFT) |
2259                                   (0x27 << AC_AMPCAP_STEP_SIZE_SHIFT) |
2260                                   (0 << AC_AMPCAP_MUTE_SHIFT));
2261
2262         return 0;
2263 }
2264
2265 static int patch_stac9205(struct hda_codec *codec)
2266 {
2267         struct sigmatel_spec *spec;
2268         int err;
2269
2270         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2271         if (spec == NULL)
2272                 return -ENOMEM;
2273
2274         codec->spec = spec;
2275         spec->num_pins = ARRAY_SIZE(stac9205_pin_nids);
2276         spec->pin_nids = stac9205_pin_nids;
2277         spec->board_config = snd_hda_check_board_config(codec, STAC_9205_MODELS,
2278                                                         stac9205_models,
2279                                                         stac9205_cfg_tbl);
2280  again:
2281         if (spec->board_config < 0) {
2282                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9205, using BIOS defaults\n");
2283                 err = stac92xx_save_bios_config_regs(codec);
2284                 if (err < 0) {
2285                         stac92xx_free(codec);
2286                         return err;
2287                 }
2288                 spec->pin_configs = spec->bios_pin_configs;
2289         } else {
2290                 spec->pin_configs = stac9205_brd_tbl[spec->board_config];
2291                 stac92xx_set_config_regs(codec);
2292         }
2293
2294         spec->adc_nids = stac9205_adc_nids;
2295         spec->mux_nids = stac9205_mux_nids;
2296         spec->num_muxes = ARRAY_SIZE(stac9205_mux_nids);
2297         spec->dmic_nids = stac9205_dmic_nids;
2298         spec->num_dmics = ARRAY_SIZE(stac9205_dmic_nids);
2299         spec->dmux_nid = 0x1d;
2300
2301         spec->init = stac9205_core_init;
2302         spec->mixer = stac9205_mixer;
2303
2304         spec->multiout.dac_nids = spec->dac_nids;
2305         stac92xx_enable_eapd(codec);
2306
2307         err = stac92xx_parse_auto_config(codec, 0x1f, 0x20);
2308         if (!err) {
2309                 if (spec->board_config < 0) {
2310                         printk(KERN_WARNING "hda_codec: No auto-config is "
2311                                "available, default to model=ref\n");
2312                         spec->board_config = STAC_9205_REF;
2313                         goto again;
2314                 }
2315                 err = -EINVAL;
2316         }
2317         if (err < 0) {
2318                 stac92xx_free(codec);
2319                 return err;
2320         }
2321
2322         codec->patch_ops = stac92xx_patch_ops;
2323
2324         return 0;
2325 }
2326
2327 /*
2328  * STAC9872 hack
2329  */
2330
2331 /* static config for Sony VAIO FE550G and Sony VAIO AR */
2332 static hda_nid_t vaio_dacs[] = { 0x2 };
2333 #define VAIO_HP_DAC     0x5
2334 static hda_nid_t vaio_adcs[] = { 0x8 /*,0x6*/ };
2335 static hda_nid_t vaio_mux_nids[] = { 0x15 };
2336
2337 static struct hda_input_mux vaio_mux = {
2338         .num_items = 2,
2339         .items = {
2340                 /* { "HP", 0x0 }, */
2341                 { "Mic Jack", 0x1 },
2342                 { "Internal Mic", 0x2 },
2343                 { "PCM", 0x3 },
2344         }
2345 };
2346
2347 static struct hda_verb vaio_init[] = {
2348         {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
2349         {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
2350         {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
2351         {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
2352         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
2353         {0x15, AC_VERB_SET_CONNECT_SEL, 0x1}, /* mic-sel: 0a,0d,14,02 */
2354         {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
2355         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
2356         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
2357         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
2358         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
2359         {}
2360 };
2361
2362 static struct hda_verb vaio_ar_init[] = {
2363         {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
2364         {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
2365         {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
2366         {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
2367 /*      {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },*/ /* Optical Out */
2368         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
2369         {0x15, AC_VERB_SET_CONNECT_SEL, 0x1}, /* mic-sel: 0a,0d,14,02 */
2370         {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
2371         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
2372 /*      {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},*/ /* Optical Out */
2373         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
2374         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
2375         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
2376         {}
2377 };
2378
2379 /* bind volumes of both NID 0x02 and 0x05 */
2380 static int vaio_master_vol_put(struct snd_kcontrol *kcontrol,
2381                                struct snd_ctl_elem_value *ucontrol)
2382 {
2383         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2384         long *valp = ucontrol->value.integer.value;
2385         int change;
2386
2387         change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
2388                                           0x7f, valp[0] & 0x7f);
2389         change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
2390                                            0x7f, valp[1] & 0x7f);
2391         snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
2392                                  0x7f, valp[0] & 0x7f);
2393         snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
2394                                  0x7f, valp[1] & 0x7f);
2395         return change;
2396 }
2397
2398 /* bind volumes of both NID 0x02 and 0x05 */
2399 static int vaio_master_sw_put(struct snd_kcontrol *kcontrol,
2400                               struct snd_ctl_elem_value *ucontrol)
2401 {
2402         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2403         long *valp = ucontrol->value.integer.value;
2404         int change;
2405
2406         change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
2407                                           0x80, (valp[0] ? 0 : 0x80));
2408         change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
2409                                            0x80, (valp[1] ? 0 : 0x80));
2410         snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
2411                                  0x80, (valp[0] ? 0 : 0x80));
2412         snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
2413                                  0x80, (valp[1] ? 0 : 0x80));
2414         return change;
2415 }
2416
2417 static struct snd_kcontrol_new vaio_mixer[] = {
2418         {
2419                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2420                 .name = "Master Playback Volume",
2421                 .info = snd_hda_mixer_amp_volume_info,
2422                 .get = snd_hda_mixer_amp_volume_get,
2423                 .put = vaio_master_vol_put,
2424                 .tlv = { .c = snd_hda_mixer_amp_tlv },
2425                 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
2426         },
2427         {
2428                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2429                 .name = "Master Playback Switch",
2430                 .info = snd_hda_mixer_amp_switch_info,
2431                 .get = snd_hda_mixer_amp_switch_get,
2432                 .put = vaio_master_sw_put,
2433                 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
2434         },
2435         /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
2436         HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
2437         HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
2438         {
2439                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2440                 .name = "Capture Source",
2441                 .count = 1,
2442                 .info = stac92xx_mux_enum_info,
2443                 .get = stac92xx_mux_enum_get,
2444                 .put = stac92xx_mux_enum_put,
2445         },
2446         {}
2447 };
2448
2449 static struct snd_kcontrol_new vaio_ar_mixer[] = {
2450         {
2451                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2452                 .name = "Master Playback Volume",
2453                 .info = snd_hda_mixer_amp_volume_info,
2454                 .get = snd_hda_mixer_amp_volume_get,
2455                 .put = vaio_master_vol_put,
2456                 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
2457         },
2458         {
2459                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2460                 .name = "Master Playback Switch",
2461                 .info = snd_hda_mixer_amp_switch_info,
2462                 .get = snd_hda_mixer_amp_switch_get,
2463                 .put = vaio_master_sw_put,
2464                 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
2465         },
2466         /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
2467         HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
2468         HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
2469         /*HDA_CODEC_MUTE("Optical Out Switch", 0x10, 0, HDA_OUTPUT),
2470         HDA_CODEC_VOLUME("Optical Out Volume", 0x10, 0, HDA_OUTPUT),*/
2471         {
2472                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2473                 .name = "Capture Source",
2474                 .count = 1,
2475                 .info = stac92xx_mux_enum_info,
2476                 .get = stac92xx_mux_enum_get,
2477                 .put = stac92xx_mux_enum_put,
2478         },
2479         {}
2480 };
2481
2482 static struct hda_codec_ops stac9872_patch_ops = {
2483         .build_controls = stac92xx_build_controls,
2484         .build_pcms = stac92xx_build_pcms,
2485         .init = stac92xx_init,
2486         .free = stac92xx_free,
2487 #ifdef CONFIG_PM
2488         .resume = stac92xx_resume,
2489 #endif
2490 };
2491
2492 enum { /* FE and SZ series. id=0x83847661 and subsys=0x104D0700 or 104D1000. */
2493        CXD9872RD_VAIO,
2494        /* Unknown. id=0x83847662 and subsys=0x104D1200 or 104D1000. */
2495        STAC9872AK_VAIO, 
2496        /* Unknown. id=0x83847661 and subsys=0x104D1200. */
2497        STAC9872K_VAIO,
2498        /* AR Series. id=0x83847664 and subsys=104D1300 */
2499        CXD9872AKD_VAIO,
2500        STAC_9872_MODELS,
2501 };
2502
2503 static const char *stac9872_models[STAC_9872_MODELS] = {
2504         [CXD9872RD_VAIO]        = "vaio",
2505         [CXD9872AKD_VAIO]       = "vaio-ar",
2506 };
2507
2508 static struct snd_pci_quirk stac9872_cfg_tbl[] = {
2509         SND_PCI_QUIRK(0x104d, 0x81e6, "Sony VAIO F/S", CXD9872RD_VAIO),
2510         SND_PCI_QUIRK(0x104d, 0x81ef, "Sony VAIO F/S", CXD9872RD_VAIO),
2511         SND_PCI_QUIRK(0x104d, 0x81fd, "Sony VAIO AR", CXD9872AKD_VAIO),
2512         SND_PCI_QUIRK(0x104d, 0x8205, "Sony VAIO AR", CXD9872AKD_VAIO),
2513         {}
2514 };
2515
2516 static int patch_stac9872(struct hda_codec *codec)
2517 {
2518         struct sigmatel_spec *spec;
2519         int board_config;
2520
2521         board_config = snd_hda_check_board_config(codec, STAC_9872_MODELS,
2522                                                   stac9872_models,
2523                                                   stac9872_cfg_tbl);
2524         if (board_config < 0)
2525                 /* unknown config, let generic-parser do its job... */
2526                 return snd_hda_parse_generic_codec(codec);
2527         
2528         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2529         if (spec == NULL)
2530                 return -ENOMEM;
2531
2532         codec->spec = spec;
2533         switch (board_config) {
2534         case CXD9872RD_VAIO:
2535         case STAC9872AK_VAIO:
2536         case STAC9872K_VAIO:
2537                 spec->mixer = vaio_mixer;
2538                 spec->init = vaio_init;
2539                 spec->multiout.max_channels = 2;
2540                 spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
2541                 spec->multiout.dac_nids = vaio_dacs;
2542                 spec->multiout.hp_nid = VAIO_HP_DAC;
2543                 spec->num_adcs = ARRAY_SIZE(vaio_adcs);
2544                 spec->adc_nids = vaio_adcs;
2545                 spec->input_mux = &vaio_mux;
2546                 spec->mux_nids = vaio_mux_nids;
2547                 break;
2548         
2549         case CXD9872AKD_VAIO:
2550                 spec->mixer = vaio_ar_mixer;
2551                 spec->init = vaio_ar_init;
2552                 spec->multiout.max_channels = 2;
2553                 spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
2554                 spec->multiout.dac_nids = vaio_dacs;
2555                 spec->multiout.hp_nid = VAIO_HP_DAC;
2556                 spec->num_adcs = ARRAY_SIZE(vaio_adcs);
2557                 spec->adc_nids = vaio_adcs;
2558                 spec->input_mux = &vaio_mux;
2559                 spec->mux_nids = vaio_mux_nids;
2560                 break;
2561         }
2562
2563         codec->patch_ops = stac9872_patch_ops;
2564         return 0;
2565 }
2566
2567
2568 /*
2569  * patch entries
2570  */
2571 struct hda_codec_preset snd_hda_preset_sigmatel[] = {
2572         { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
2573         { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
2574         { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
2575         { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
2576         { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
2577         { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
2578         { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
2579         { .id = 0x83847618, .name = "STAC9227", .patch = patch_stac927x },
2580         { .id = 0x83847619, .name = "STAC9227", .patch = patch_stac927x },
2581         { .id = 0x83847616, .name = "STAC9228", .patch = patch_stac927x },
2582         { .id = 0x83847617, .name = "STAC9228", .patch = patch_stac927x },
2583         { .id = 0x83847614, .name = "STAC9229", .patch = patch_stac927x },
2584         { .id = 0x83847615, .name = "STAC9229", .patch = patch_stac927x },
2585         { .id = 0x83847620, .name = "STAC9274", .patch = patch_stac927x },
2586         { .id = 0x83847621, .name = "STAC9274D", .patch = patch_stac927x },
2587         { .id = 0x83847622, .name = "STAC9273X", .patch = patch_stac927x },
2588         { .id = 0x83847623, .name = "STAC9273D", .patch = patch_stac927x },
2589         { .id = 0x83847624, .name = "STAC9272X", .patch = patch_stac927x },
2590         { .id = 0x83847625, .name = "STAC9272D", .patch = patch_stac927x },
2591         { .id = 0x83847626, .name = "STAC9271X", .patch = patch_stac927x },
2592         { .id = 0x83847627, .name = "STAC9271D", .patch = patch_stac927x },
2593         { .id = 0x83847628, .name = "STAC9274X5NH", .patch = patch_stac927x },
2594         { .id = 0x83847629, .name = "STAC9274D5NH", .patch = patch_stac927x },
2595         { .id = 0x83847632, .name = "STAC9202",  .patch = patch_stac925x },
2596         { .id = 0x83847633, .name = "STAC9202D", .patch = patch_stac925x },
2597         { .id = 0x83847634, .name = "STAC9250", .patch = patch_stac925x },
2598         { .id = 0x83847635, .name = "STAC9250D", .patch = patch_stac925x },
2599         { .id = 0x83847636, .name = "STAC9251", .patch = patch_stac925x },
2600         { .id = 0x83847637, .name = "STAC9250D", .patch = patch_stac925x },
2601         /* The following does not take into account .id=0x83847661 when subsys =
2602          * 104D0C00 which is STAC9225s. Because of this, some SZ Notebooks are
2603          * currently not fully supported.
2604          */
2605         { .id = 0x83847661, .name = "CXD9872RD/K", .patch = patch_stac9872 },
2606         { .id = 0x83847662, .name = "STAC9872AK", .patch = patch_stac9872 },
2607         { .id = 0x83847664, .name = "CXD9872AKD", .patch = patch_stac9872 },
2608         { .id = 0x838476a0, .name = "STAC9205", .patch = patch_stac9205 },
2609         { .id = 0x838476a1, .name = "STAC9205D", .patch = patch_stac9205 },
2610         { .id = 0x838476a2, .name = "STAC9204", .patch = patch_stac9205 },
2611         { .id = 0x838476a3, .name = "STAC9204D", .patch = patch_stac9205 },
2612         { .id = 0x838476a4, .name = "STAC9255", .patch = patch_stac9205 },
2613         { .id = 0x838476a5, .name = "STAC9255D", .patch = patch_stac9205 },
2614         { .id = 0x838476a6, .name = "STAC9254", .patch = patch_stac9205 },
2615         { .id = 0x838476a7, .name = "STAC9254D", .patch = patch_stac9205 },
2616         {} /* terminator */
2617 };