[ALSA] hda-codec - Support multiple headphone pins
[safe/jmp/linux-2.6] / sound / pci / hda / patch_sigmatel.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * HD audio interface patch for SigmaTel STAC92xx
5  *
6  * Copyright (c) 2005 Embedded Alley Solutions, Inc.
7  * Matt Porter <mporter@embeddedalley.com>
8  *
9  * Based on patch_cmedia.c and patch_realtek.c
10  * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
11  *
12  *  This driver is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This driver is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25  */
26
27 #include <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 #define STAC_UNSOL_ENABLE       (AC_USRSP_EN | STAC_HP_EVENT)
40
41 #define STAC_REF                0
42 #define STAC_D945GTP3           1
43 #define STAC_D945GTP5           2
44 #define STAC_MACMINI            3
45 #define STAC_922X_MODELS        4       /* number of 922x models */
46 #define STAC_D965_3ST           4
47 #define STAC_D965_5ST           5
48 #define STAC_927X_MODELS        6       /* number of 922x models */
49
50 struct sigmatel_spec {
51         struct snd_kcontrol_new *mixers[4];
52         unsigned int num_mixers;
53
54         int board_config;
55         unsigned int surr_switch: 1;
56         unsigned int line_switch: 1;
57         unsigned int mic_switch: 1;
58         unsigned int alt_switch: 1;
59         unsigned int hp_detect: 1;
60         unsigned int gpio_mute: 1;
61
62         /* playback */
63         struct hda_multi_out multiout;
64         hda_nid_t dac_nids[5];
65
66         /* capture */
67         hda_nid_t *adc_nids;
68         unsigned int num_adcs;
69         hda_nid_t *mux_nids;
70         unsigned int num_muxes;
71         hda_nid_t dig_in_nid;
72
73         /* pin widgets */
74         hda_nid_t *pin_nids;
75         unsigned int num_pins;
76         unsigned int *pin_configs;
77         unsigned int *bios_pin_configs;
78
79         /* codec specific stuff */
80         struct hda_verb *init;
81         struct snd_kcontrol_new *mixer;
82
83         /* capture source */
84         struct hda_input_mux *input_mux;
85         unsigned int cur_mux[3];
86
87         /* i/o switches */
88         unsigned int io_switch[2];
89
90         struct hda_pcm pcm_rec[2];      /* PCM information */
91
92         /* dynamic controls and input_mux */
93         struct auto_pin_cfg autocfg;
94         unsigned int num_kctl_alloc, num_kctl_used;
95         struct snd_kcontrol_new *kctl_alloc;
96         struct hda_input_mux private_imux;
97 };
98
99 static hda_nid_t stac9200_adc_nids[1] = {
100         0x03,
101 };
102
103 static hda_nid_t stac9200_mux_nids[1] = {
104         0x0c,
105 };
106
107 static hda_nid_t stac9200_dac_nids[1] = {
108         0x02,
109 };
110
111 static hda_nid_t stac922x_adc_nids[2] = {
112         0x06, 0x07,
113 };
114
115 static hda_nid_t stac922x_mux_nids[2] = {
116         0x12, 0x13,
117 };
118
119 static hda_nid_t stac927x_adc_nids[3] = {
120         0x07, 0x08, 0x09
121 };
122
123 static hda_nid_t stac927x_mux_nids[3] = {
124         0x15, 0x16, 0x17
125 };
126
127 static hda_nid_t stac9205_adc_nids[2] = {
128         0x12, 0x13
129 };
130
131 static hda_nid_t stac9205_mux_nids[2] = {
132         0x19, 0x1a
133 };
134
135 static hda_nid_t stac9200_pin_nids[8] = {
136         0x08, 0x09, 0x0d, 0x0e, 
137         0x0f, 0x10, 0x11, 0x12,
138 };
139
140 static hda_nid_t stac922x_pin_nids[10] = {
141         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
142         0x0f, 0x10, 0x11, 0x15, 0x1b,
143 };
144
145 static hda_nid_t stac927x_pin_nids[14] = {
146         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
147         0x0f, 0x10, 0x11, 0x12, 0x13,
148         0x14, 0x21, 0x22, 0x23,
149 };
150
151 static hda_nid_t stac9205_pin_nids[12] = {
152         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
153         0x0f, 0x14, 0x16, 0x17, 0x18,
154         0x21, 0x22,
155         
156 };
157
158 static int stac92xx_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
159 {
160         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
161         struct sigmatel_spec *spec = codec->spec;
162         return snd_hda_input_mux_info(spec->input_mux, uinfo);
163 }
164
165 static int stac92xx_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
166 {
167         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
168         struct sigmatel_spec *spec = codec->spec;
169         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
170
171         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
172         return 0;
173 }
174
175 static int stac92xx_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
176 {
177         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
178         struct sigmatel_spec *spec = codec->spec;
179         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
180
181         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
182                                      spec->mux_nids[adc_idx], &spec->cur_mux[adc_idx]);
183 }
184
185 static struct hda_verb stac9200_core_init[] = {
186         /* set dac0mux for dac converter */
187         { 0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
188         {}
189 };
190
191 static struct hda_verb stac922x_core_init[] = {
192         /* set master volume and direct control */      
193         { 0x16, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
194         {}
195 };
196
197 static struct hda_verb d965_core_init[] = {
198         /* set master volume and direct control */      
199         { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
200         /* unmute node 0x1b */
201         { 0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
202         /* select node 0x03 as DAC */   
203         { 0x0b, AC_VERB_SET_CONNECT_SEL, 0x01},
204         {}
205 };
206
207 static struct hda_verb stac927x_core_init[] = {
208         /* set master volume and direct control */      
209         { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
210         {}
211 };
212
213 static struct hda_verb stac9205_core_init[] = {
214         /* set master volume and direct control */      
215         { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
216         {}
217 };
218
219 static struct snd_kcontrol_new stac9200_mixer[] = {
220         HDA_CODEC_VOLUME("Master Playback Volume", 0xb, 0, HDA_OUTPUT),
221         HDA_CODEC_MUTE("Master Playback Switch", 0xb, 0, HDA_OUTPUT),
222         {
223                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
224                 .name = "Input Source",
225                 .count = 1,
226                 .info = stac92xx_mux_enum_info,
227                 .get = stac92xx_mux_enum_get,
228                 .put = stac92xx_mux_enum_put,
229         },
230         HDA_CODEC_VOLUME("Capture Volume", 0x0a, 0, HDA_OUTPUT),
231         HDA_CODEC_MUTE("Capture Switch", 0x0a, 0, HDA_OUTPUT),
232         HDA_CODEC_VOLUME("Capture Mux Volume", 0x0c, 0, HDA_OUTPUT),
233         { } /* end */
234 };
235
236 /* This needs to be generated dynamically based on sequence */
237 static struct snd_kcontrol_new stac922x_mixer[] = {
238         {
239                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
240                 .name = "Input Source",
241                 .count = 1,
242                 .info = stac92xx_mux_enum_info,
243                 .get = stac92xx_mux_enum_get,
244                 .put = stac92xx_mux_enum_put,
245         },
246         HDA_CODEC_VOLUME("Capture Volume", 0x17, 0x0, HDA_INPUT),
247         HDA_CODEC_MUTE("Capture Switch", 0x17, 0x0, HDA_INPUT),
248         HDA_CODEC_VOLUME("Mux Capture Volume", 0x12, 0x0, HDA_OUTPUT),
249         { } /* end */
250 };
251
252 /* This needs to be generated dynamically based on sequence */
253 static struct snd_kcontrol_new stac9227_mixer[] = {
254         {
255                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
256                 .name = "Input Source",
257                 .count = 1,
258                 .info = stac92xx_mux_enum_info,
259                 .get = stac92xx_mux_enum_get,
260                 .put = stac92xx_mux_enum_put,
261         },
262         HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_OUTPUT),
263         HDA_CODEC_MUTE("Capture Switch", 0x1b, 0x0, HDA_OUTPUT),
264         { } /* end */
265 };
266
267 static snd_kcontrol_new_t stac927x_mixer[] = {
268         {
269                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
270                 .name = "Input Source",
271                 .count = 1,
272                 .info = stac92xx_mux_enum_info,
273                 .get = stac92xx_mux_enum_get,
274                 .put = stac92xx_mux_enum_put,
275         },
276         HDA_CODEC_VOLUME("InMux Capture Volume", 0x15, 0x0, HDA_OUTPUT),
277         HDA_CODEC_VOLUME("InVol Capture Volume", 0x18, 0x0, HDA_INPUT),
278         HDA_CODEC_MUTE("ADCMux Capture Switch", 0x1b, 0x0, HDA_OUTPUT),
279         { } /* end */
280 };
281
282 static snd_kcontrol_new_t stac9205_mixer[] = {
283         {
284                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
285                 .name = "Input Source",
286                 .count = 1,
287                 .info = stac92xx_mux_enum_info,
288                 .get = stac92xx_mux_enum_get,
289                 .put = stac92xx_mux_enum_put,
290         },
291         HDA_CODEC_VOLUME("InMux Capture Volume", 0x19, 0x0, HDA_OUTPUT),
292         HDA_CODEC_VOLUME("InVol Capture Volume", 0x1b, 0x0, HDA_INPUT),
293         HDA_CODEC_MUTE("ADCMux Capture Switch", 0x1d, 0x0, HDA_OUTPUT),
294         { } /* end */
295 };
296
297 static int stac92xx_build_controls(struct hda_codec *codec)
298 {
299         struct sigmatel_spec *spec = codec->spec;
300         int err;
301         int i;
302
303         err = snd_hda_add_new_ctls(codec, spec->mixer);
304         if (err < 0)
305                 return err;
306
307         for (i = 0; i < spec->num_mixers; i++) {
308                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
309                 if (err < 0)
310                         return err;
311         }
312
313         if (spec->multiout.dig_out_nid) {
314                 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
315                 if (err < 0)
316                         return err;
317         }
318         if (spec->dig_in_nid) {
319                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
320                 if (err < 0)
321                         return err;
322         }
323         return 0;       
324 }
325
326 static unsigned int ref9200_pin_configs[8] = {
327         0x01c47010, 0x01447010, 0x0221401f, 0x01114010,
328         0x02a19020, 0x01a19021, 0x90100140, 0x01813122,
329 };
330
331 static unsigned int *stac9200_brd_tbl[] = {
332         ref9200_pin_configs,
333 };
334
335 static struct hda_board_config stac9200_cfg_tbl[] = {
336         { .modelname = "ref",
337           .pci_subvendor = PCI_VENDOR_ID_INTEL,
338           .pci_subdevice = 0x2668,      /* DFI LanParty */
339           .config = STAC_REF },
340         {} /* terminator */
341 };
342
343 static unsigned int ref922x_pin_configs[10] = {
344         0x01014010, 0x01016011, 0x01012012, 0x0221401f,
345         0x01813122, 0x01011014, 0x01441030, 0x01c41030,
346         0x40000100, 0x40000100,
347 };
348
349 static unsigned int d945gtp3_pin_configs[10] = {
350         0x0221401f, 0x01a19022, 0x01813021, 0x01014010,
351         0x40000100, 0x40000100, 0x40000100, 0x40000100,
352         0x02a19120, 0x40000100,
353 };
354
355 static unsigned int d945gtp5_pin_configs[10] = {
356         0x0221401f, 0x01011012, 0x01813024, 0x01014010,
357         0x01a19021, 0x01016011, 0x01452130, 0x40000100,
358         0x02a19320, 0x40000100,
359 };
360
361 static unsigned int *stac922x_brd_tbl[STAC_922X_MODELS] = {
362         [STAC_REF] =    ref922x_pin_configs,
363         [STAC_D945GTP3] = d945gtp3_pin_configs,
364         [STAC_D945GTP5] = d945gtp5_pin_configs,
365         [STAC_MACMINI] = d945gtp5_pin_configs,
366 };
367
368 static struct hda_board_config stac922x_cfg_tbl[] = {
369         { .modelname = "5stack", .config = STAC_D945GTP5 },
370         { .modelname = "3stack", .config = STAC_D945GTP3 },
371         { .modelname = "ref",
372           .pci_subvendor = PCI_VENDOR_ID_INTEL,
373           .pci_subdevice = 0x2668,      /* DFI LanParty */
374           .config = STAC_REF },         /* SigmaTel reference board */
375          /* Intel 945G based systems */
376         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
377           .pci_subdevice = 0x0101,
378           .config = STAC_D945GTP3 },    /* Intel D945GTP - 3 Stack */
379         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
380           .pci_subdevice = 0x0202,
381           .config = STAC_D945GTP3 },    /* Intel D945GNT - 3 Stack */
382         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
383           .pci_subdevice = 0x0606,
384           .config = STAC_D945GTP3 },    /* Intel D945GTP - 3 Stack */
385         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
386           .pci_subdevice = 0x0601,
387           .config = STAC_D945GTP3 },    /* Intel D945GTP - 3 Stack */
388         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
389           .pci_subdevice = 0x0111,
390           .config = STAC_D945GTP3 },    /* Intel D945GZP - 3 Stack */
391         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
392           .pci_subdevice = 0x1115,
393           .config = STAC_D945GTP3 },    /* Intel D945GPM - 3 Stack */
394         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
395           .pci_subdevice = 0x1116,
396           .config = STAC_D945GTP3 },    /* Intel D945GBO - 3 Stack */
397         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
398           .pci_subdevice = 0x1117,
399           .config = STAC_D945GTP3 },    /* Intel D945GPM - 3 Stack */
400         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
401           .pci_subdevice = 0x1118,
402           .config = STAC_D945GTP3 },    /* Intel D945GPM - 3 Stack */
403         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
404           .pci_subdevice = 0x1119,
405           .config = STAC_D945GTP3 },    /* Intel D945GPM - 3 Stack */
406         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
407           .pci_subdevice = 0x8826,
408           .config = STAC_D945GTP3 },    /* Intel D945GPM - 3 Stack */
409         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
410           .pci_subdevice = 0x5049,
411           .config = STAC_D945GTP3 },    /* Intel D945GCZ - 3 Stack */
412         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
413           .pci_subdevice = 0x5055,
414           .config = STAC_D945GTP3 },    /* Intel D945GCZ - 3 Stack */
415         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
416           .pci_subdevice = 0x5048,
417           .config = STAC_D945GTP3 },    /* Intel D945GPB - 3 Stack */
418         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
419           .pci_subdevice = 0x0110,
420           .config = STAC_D945GTP3 },    /* Intel D945GLR - 3 Stack */
421         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
422           .pci_subdevice = 0x0404,
423           .config = STAC_D945GTP5 },    /* Intel D945GTP - 5 Stack */
424         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
425           .pci_subdevice = 0x0303,
426           .config = STAC_D945GTP5 },    /* Intel D945GNT - 5 Stack */
427         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
428           .pci_subdevice = 0x0013,
429           .config = STAC_D945GTP5 },    /* Intel D955XBK - 5 Stack */
430         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
431           .pci_subdevice = 0x0417,
432           .config = STAC_D945GTP5 },    /* Intel D975XBK - 5 Stack */
433           /* Intel 945P based systems */
434         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
435           .pci_subdevice = 0x0b0b,
436           .config = STAC_D945GTP3 },    /* Intel D945PSN - 3 Stack */
437         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
438           .pci_subdevice = 0x0112,
439           .config = STAC_D945GTP3 },    /* Intel D945PLN - 3 Stack */
440         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
441           .pci_subdevice = 0x0d0d,
442           .config = STAC_D945GTP3 },    /* Intel D945PLM - 3 Stack */
443         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
444           .pci_subdevice = 0x0909,
445           .config = STAC_D945GTP3 },    /* Intel D945PAW - 3 Stack */
446         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
447           .pci_subdevice = 0x0505,
448           .config = STAC_D945GTP3 },    /* Intel D945PLM - 3 Stack */
449         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
450           .pci_subdevice = 0x0707,
451           .config = STAC_D945GTP5 },    /* Intel D945PSV - 5 Stack */
452           /* other systems  */
453         { .pci_subvendor = 0x8384,
454           .pci_subdevice = 0x7680,
455           .config = STAC_MACMINI },     /* Apple Mac Mini (early 2006) */
456         {} /* terminator */
457 };
458
459 static unsigned int ref927x_pin_configs[14] = {
460         0x02214020, 0x02a19080, 0x0181304e, 0x01014010,
461         0x01a19040, 0x01011012, 0x01016011, 0x0101201f, 
462         0x183301f0, 0x18a001f0, 0x18a001f0, 0x01442070,
463         0x01c42190, 0x40000100,
464 };
465
466 static unsigned int d965_3st_pin_configs[14] = {
467         0x0221401f, 0x02a19120, 0x40000100, 0x01014011,
468         0x01a19021, 0x01813024, 0x40000100, 0x40000100,
469         0x40000100, 0x40000100, 0x40000100, 0x40000100,
470         0x40000100, 0x40000100
471 };
472
473 static unsigned int d965_5st_pin_configs[14] = {
474         0x02214020, 0x02a19080, 0x0181304e, 0x01014010,
475         0x01a19040, 0x01011012, 0x01016011, 0x40000100,
476         0x40000100, 0x40000100, 0x40000100, 0x01442070,
477         0x40000100, 0x40000100
478 };
479
480 static unsigned int *stac927x_brd_tbl[STAC_927X_MODELS] = {
481         [STAC_REF] =    ref927x_pin_configs,
482         [STAC_D965_3ST] = d965_3st_pin_configs,
483         [STAC_D965_5ST] = d965_5st_pin_configs,
484 };
485
486 static struct hda_board_config stac927x_cfg_tbl[] = {
487         { .modelname = "5stack", .config = STAC_D965_5ST },
488         { .modelname = "3stack", .config = STAC_D965_3ST },
489         { .modelname = "ref",
490           .pci_subvendor = PCI_VENDOR_ID_INTEL,
491           .pci_subdevice = 0x2668,      /* DFI LanParty */
492           .config = STAC_REF },         /* SigmaTel reference board */
493          /* Intel 946 based systems */
494         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
495           .pci_subdevice = 0x3d01,
496           .config = STAC_D965_3ST }, /* D946  configuration */
497         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
498           .pci_subdevice = 0xa301,
499           .config = STAC_D965_3ST }, /* Intel D946GZT - 3 stack  */
500         /* 965 based 3 stack systems */
501         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
502           .pci_subdevice = 0x2116,
503           .config = STAC_D965_3ST }, /* Intel D965 3Stack config */
504         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
505           .pci_subdevice = 0x2115,
506           .config = STAC_D965_3ST }, /* Intel DQ965WC - 3 Stack  */
507         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
508           .pci_subdevice = 0x2114,
509           .config = STAC_D965_3ST }, /* Intel D965 3Stack config */
510         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
511           .pci_subdevice = 0x2113,
512           .config = STAC_D965_3ST }, /* Intel D965 3Stack config */
513         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
514           .pci_subdevice = 0x2112,
515           .config = STAC_D965_3ST }, /* Intel DG965MS - 3 Stack  */
516         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
517           .pci_subdevice = 0x2111,
518           .config = STAC_D965_3ST }, /* Intel D965 3Stack config */
519         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
520           .pci_subdevice = 0x2110,
521           .config = STAC_D965_3ST }, /* Intel D965 3Stack config */
522         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
523           .pci_subdevice = 0x2009,
524           .config = STAC_D965_3ST }, /* Intel D965 3Stack config */
525         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
526           .pci_subdevice = 0x2008,
527           .config = STAC_D965_3ST }, /* Intel DQ965GF - 3 Stack  */
528         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
529           .pci_subdevice = 0x2007,
530           .config = STAC_D965_3ST }, /* Intel D965 3Stack config */
531         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
532           .pci_subdevice = 0x2006,
533           .config = STAC_D965_3ST }, /* Intel D965 3Stack config */
534         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
535           .pci_subdevice = 0x2005,
536           .config = STAC_D965_3ST }, /* Intel D965 3Stack config */
537         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
538           .pci_subdevice = 0x2004,
539           .config = STAC_D965_3ST }, /* Intel D965 3Stack config */
540         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
541           .pci_subdevice = 0x2003,
542           .config = STAC_D965_3ST }, /* Intel D965 3Stack config */
543         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
544           .pci_subdevice = 0x2002,
545           .config = STAC_D965_3ST }, /* Intel D965 3Stack config */
546         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
547           .pci_subdevice = 0x2001,
548           .config = STAC_D965_3ST }, /* Intel DQ965GF - 3 Stack */
549         /* 965 based 5 stack systems */
550         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
551           .pci_subdevice = 0x2301,
552           .config = STAC_D965_5ST }, /* Intel DG965 - 5 Stack */
553         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
554           .pci_subdevice = 0x2302,
555           .config = STAC_D965_5ST }, /* Intel DG965 - 5 Stack */
556         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
557           .pci_subdevice = 0x2303,
558           .config = STAC_D965_5ST }, /* Intel DG965 - 5 Stack */
559         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
560           .pci_subdevice = 0x2304,
561           .config = STAC_D965_5ST }, /* Intel DG965 - 5 Stack */
562         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
563           .pci_subdevice = 0x2305,
564           .config = STAC_D965_5ST }, /* Intel DG965 - 5 Stack */
565         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
566           .pci_subdevice = 0x2501,
567           .config = STAC_D965_5ST }, /* Intel DG965MQ - 5 Stack */
568         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
569           .pci_subdevice = 0x2502,
570           .config = STAC_D965_5ST }, /* Intel DG965 - 5 Stack */
571         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
572           .pci_subdevice = 0x2503,
573           .config = STAC_D965_5ST }, /* Intel DG965 - 5 Stack */
574         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
575           .pci_subdevice = 0x2504,
576           .config = STAC_D965_5ST }, /* Intel DQ965GF - 5 Stack */
577         {} /* terminator */
578 };
579
580 static unsigned int ref9205_pin_configs[12] = {
581         0x40000100, 0x40000100, 0x01016011, 0x01014010,
582         0x01813122, 0x01a19021, 0x40000100, 0x40000100, 
583         0x40000100, 0x40000100, 0x01441030, 0x01c41030
584 };
585
586 static unsigned int *stac9205_brd_tbl[] = {
587         ref9205_pin_configs,
588 };
589
590 static struct hda_board_config stac9205_cfg_tbl[] = {
591         { .modelname = "ref",
592           .pci_subvendor = PCI_VENDOR_ID_INTEL,
593           .pci_subdevice = 0x2668,      /* DFI LanParty */
594           .config = STAC_REF },         /* SigmaTel reference board */
595         /* Dell laptops have BIOS problem */
596         { .pci_subvendor = PCI_VENDOR_ID_DELL, .pci_subdevice = 0x01b5,
597           .config = STAC_REF }, /* Dell Inspiron 630m */
598         { .pci_subvendor = PCI_VENDOR_ID_DELL, .pci_subdevice = 0x01c2,
599           .config = STAC_REF }, /* Dell Latitude D620 */
600         { .pci_subvendor = PCI_VENDOR_ID_DELL, .pci_subdevice = 0x01cb,
601           .config = STAC_REF }, /* Dell Latitude 120L */
602         {} /* terminator */
603 };
604
605 static int stac92xx_save_bios_config_regs(struct hda_codec *codec)
606 {
607         int i;
608         struct sigmatel_spec *spec = codec->spec;
609         
610         if (! spec->bios_pin_configs) {
611                 spec->bios_pin_configs = kcalloc(spec->num_pins,
612                                                  sizeof(*spec->bios_pin_configs), GFP_KERNEL);
613                 if (! spec->bios_pin_configs)
614                         return -ENOMEM;
615         }
616         
617         for (i = 0; i < spec->num_pins; i++) {
618                 hda_nid_t nid = spec->pin_nids[i];
619                 unsigned int pin_cfg;
620                 
621                 pin_cfg = snd_hda_codec_read(codec, nid, 0, 
622                         AC_VERB_GET_CONFIG_DEFAULT, 0x00);      
623                 snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x bios pin config %8.8x\n",
624                                         nid, pin_cfg);
625                 spec->bios_pin_configs[i] = pin_cfg;
626         }
627         
628         return 0;
629 }
630
631 static void stac92xx_set_config_regs(struct hda_codec *codec)
632 {
633         int i;
634         struct sigmatel_spec *spec = codec->spec;
635         unsigned int pin_cfg;
636
637         if (! spec->pin_nids || ! spec->pin_configs)
638                 return;
639
640         for (i = 0; i < spec->num_pins; i++) {
641                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
642                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_0,
643                                     spec->pin_configs[i] & 0x000000ff);
644                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
645                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_1,
646                                     (spec->pin_configs[i] & 0x0000ff00) >> 8);
647                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
648                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_2,
649                                     (spec->pin_configs[i] & 0x00ff0000) >> 16);
650                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
651                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
652                                     spec->pin_configs[i] >> 24);
653                 pin_cfg = snd_hda_codec_read(codec, spec->pin_nids[i], 0,
654                                              AC_VERB_GET_CONFIG_DEFAULT,
655                                              0x00);     
656                 snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x pin config %8.8x\n", spec->pin_nids[i], pin_cfg);
657         }
658 }
659
660 /*
661  * Analog playback callbacks
662  */
663 static int stac92xx_playback_pcm_open(struct hda_pcm_stream *hinfo,
664                                       struct hda_codec *codec,
665                                       struct snd_pcm_substream *substream)
666 {
667         struct sigmatel_spec *spec = codec->spec;
668         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
669 }
670
671 static int stac92xx_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
672                                          struct hda_codec *codec,
673                                          unsigned int stream_tag,
674                                          unsigned int format,
675                                          struct snd_pcm_substream *substream)
676 {
677         struct sigmatel_spec *spec = codec->spec;
678         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag, format, substream);
679 }
680
681 static int stac92xx_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
682                                         struct hda_codec *codec,
683                                         struct snd_pcm_substream *substream)
684 {
685         struct sigmatel_spec *spec = codec->spec;
686         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
687 }
688
689 /*
690  * Digital playback callbacks
691  */
692 static int stac92xx_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
693                                           struct hda_codec *codec,
694                                           struct snd_pcm_substream *substream)
695 {
696         struct sigmatel_spec *spec = codec->spec;
697         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
698 }
699
700 static int stac92xx_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
701                                            struct hda_codec *codec,
702                                            struct snd_pcm_substream *substream)
703 {
704         struct sigmatel_spec *spec = codec->spec;
705         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
706 }
707
708
709 /*
710  * Analog capture callbacks
711  */
712 static int stac92xx_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
713                                         struct hda_codec *codec,
714                                         unsigned int stream_tag,
715                                         unsigned int format,
716                                         struct snd_pcm_substream *substream)
717 {
718         struct sigmatel_spec *spec = codec->spec;
719
720         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
721                                    stream_tag, 0, format);
722         return 0;
723 }
724
725 static int stac92xx_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
726                                         struct hda_codec *codec,
727                                         struct snd_pcm_substream *substream)
728 {
729         struct sigmatel_spec *spec = codec->spec;
730
731         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
732         return 0;
733 }
734
735 static struct hda_pcm_stream stac92xx_pcm_digital_playback = {
736         .substreams = 1,
737         .channels_min = 2,
738         .channels_max = 2,
739         /* NID is set in stac92xx_build_pcms */
740         .ops = {
741                 .open = stac92xx_dig_playback_pcm_open,
742                 .close = stac92xx_dig_playback_pcm_close
743         },
744 };
745
746 static struct hda_pcm_stream stac92xx_pcm_digital_capture = {
747         .substreams = 1,
748         .channels_min = 2,
749         .channels_max = 2,
750         /* NID is set in stac92xx_build_pcms */
751 };
752
753 static struct hda_pcm_stream stac92xx_pcm_analog_playback = {
754         .substreams = 1,
755         .channels_min = 2,
756         .channels_max = 8,
757         .nid = 0x02, /* NID to query formats and rates */
758         .ops = {
759                 .open = stac92xx_playback_pcm_open,
760                 .prepare = stac92xx_playback_pcm_prepare,
761                 .cleanup = stac92xx_playback_pcm_cleanup
762         },
763 };
764
765 static struct hda_pcm_stream stac92xx_pcm_analog_alt_playback = {
766         .substreams = 1,
767         .channels_min = 2,
768         .channels_max = 2,
769         .nid = 0x06, /* NID to query formats and rates */
770         .ops = {
771                 .open = stac92xx_playback_pcm_open,
772                 .prepare = stac92xx_playback_pcm_prepare,
773                 .cleanup = stac92xx_playback_pcm_cleanup
774         },
775 };
776
777 static struct hda_pcm_stream stac92xx_pcm_analog_capture = {
778         .substreams = 2,
779         .channels_min = 2,
780         .channels_max = 2,
781         /* NID is set in stac92xx_build_pcms */
782         .ops = {
783                 .prepare = stac92xx_capture_pcm_prepare,
784                 .cleanup = stac92xx_capture_pcm_cleanup
785         },
786 };
787
788 static int stac92xx_build_pcms(struct hda_codec *codec)
789 {
790         struct sigmatel_spec *spec = codec->spec;
791         struct hda_pcm *info = spec->pcm_rec;
792
793         codec->num_pcms = 1;
794         codec->pcm_info = info;
795
796         info->name = "STAC92xx Analog";
797         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_playback;
798         info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_analog_capture;
799         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
800
801         if (spec->alt_switch) {
802                 codec->num_pcms++;
803                 info++;
804                 info->name = "STAC92xx Analog Alt";
805                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_alt_playback;
806         }
807
808         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
809                 codec->num_pcms++;
810                 info++;
811                 info->name = "STAC92xx Digital";
812                 if (spec->multiout.dig_out_nid) {
813                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_digital_playback;
814                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
815                 }
816                 if (spec->dig_in_nid) {
817                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_digital_capture;
818                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
819                 }
820         }
821
822         return 0;
823 }
824
825 static unsigned int stac92xx_get_vref(struct hda_codec *codec, hda_nid_t nid)
826 {
827         unsigned int pincap = snd_hda_param_read(codec, nid,
828                                                  AC_PAR_PIN_CAP);
829         pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
830         if (pincap & AC_PINCAP_VREF_100)
831                 return AC_PINCTL_VREF_100;
832         if (pincap & AC_PINCAP_VREF_80)
833                 return AC_PINCTL_VREF_80;
834         if (pincap & AC_PINCAP_VREF_50)
835                 return AC_PINCTL_VREF_50;
836         if (pincap & AC_PINCAP_VREF_GRD)
837                 return AC_PINCTL_VREF_GRD;
838         return 0;
839 }
840
841 static void stac92xx_auto_set_pinctl(struct hda_codec *codec, hda_nid_t nid, int pin_type)
842
843 {
844         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
845 }
846
847 static int stac92xx_io_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
848 {
849         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
850         uinfo->count = 1;
851         uinfo->value.integer.min = 0;
852         uinfo->value.integer.max = 1;
853         return 0;
854 }
855
856 static int stac92xx_io_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
857 {
858         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
859         struct sigmatel_spec *spec = codec->spec;
860         int io_idx = kcontrol-> private_value & 0xff;
861
862         ucontrol->value.integer.value[0] = spec->io_switch[io_idx];
863         return 0;
864 }
865
866 static int stac92xx_io_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
867 {
868         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
869         struct sigmatel_spec *spec = codec->spec;
870         hda_nid_t nid = kcontrol->private_value >> 8;
871         int io_idx = kcontrol-> private_value & 0xff;
872         unsigned short val = ucontrol->value.integer.value[0];
873
874         spec->io_switch[io_idx] = val;
875
876         if (val)
877                 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
878         else {
879                 unsigned int pinctl = AC_PINCTL_IN_EN;
880                 if (io_idx) /* set VREF for mic */
881                         pinctl |= stac92xx_get_vref(codec, nid);
882                 stac92xx_auto_set_pinctl(codec, nid, pinctl);
883         }
884         return 1;
885 }
886
887 #define STAC_CODEC_IO_SWITCH(xname, xpval) \
888         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
889           .name = xname, \
890           .index = 0, \
891           .info = stac92xx_io_switch_info, \
892           .get = stac92xx_io_switch_get, \
893           .put = stac92xx_io_switch_put, \
894           .private_value = xpval, \
895         }
896
897
898 enum {
899         STAC_CTL_WIDGET_VOL,
900         STAC_CTL_WIDGET_MUTE,
901         STAC_CTL_WIDGET_IO_SWITCH,
902 };
903
904 static struct snd_kcontrol_new stac92xx_control_templates[] = {
905         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
906         HDA_CODEC_MUTE(NULL, 0, 0, 0),
907         STAC_CODEC_IO_SWITCH(NULL, 0),
908 };
909
910 /* add dynamic controls */
911 static int stac92xx_add_control(struct sigmatel_spec *spec, int type, const char *name, unsigned long val)
912 {
913         struct snd_kcontrol_new *knew;
914
915         if (spec->num_kctl_used >= spec->num_kctl_alloc) {
916                 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
917
918                 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
919                 if (! knew)
920                         return -ENOMEM;
921                 if (spec->kctl_alloc) {
922                         memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
923                         kfree(spec->kctl_alloc);
924                 }
925                 spec->kctl_alloc = knew;
926                 spec->num_kctl_alloc = num;
927         }
928
929         knew = &spec->kctl_alloc[spec->num_kctl_used];
930         *knew = stac92xx_control_templates[type];
931         knew->name = kstrdup(name, GFP_KERNEL);
932         if (! knew->name)
933                 return -ENOMEM;
934         knew->private_value = val;
935         spec->num_kctl_used++;
936         return 0;
937 }
938
939 /* flag inputs as additional dynamic lineouts */
940 static int stac92xx_add_dyn_out_pins(struct hda_codec *codec, struct auto_pin_cfg *cfg)
941 {
942         struct sigmatel_spec *spec = codec->spec;
943
944         switch (cfg->line_outs) {
945         case 3:
946                 /* add line-in as side */
947                 if (cfg->input_pins[AUTO_PIN_LINE]) {
948                         cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_LINE];
949                         spec->line_switch = 1;
950                         cfg->line_outs++;
951                 }
952                 break;
953         case 2:
954                 /* add line-in as clfe and mic as side */
955                 if (cfg->input_pins[AUTO_PIN_LINE]) {
956                         cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_LINE];
957                         spec->line_switch = 1;
958                         cfg->line_outs++;
959                 }
960                 if (cfg->input_pins[AUTO_PIN_MIC]) {
961                         cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_MIC];
962                         spec->mic_switch = 1;
963                         cfg->line_outs++;
964                 }
965                 break;
966         case 1:
967                 /* add line-in as surr and mic as clfe */
968                 if (cfg->input_pins[AUTO_PIN_LINE]) {
969                         cfg->line_out_pins[1] = cfg->input_pins[AUTO_PIN_LINE];
970                         spec->line_switch = 1;
971                         cfg->line_outs++;
972                 }
973                 if (cfg->input_pins[AUTO_PIN_MIC]) {
974                         cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_MIC];
975                         spec->mic_switch = 1;
976                         cfg->line_outs++;
977                 }
978                 break;
979         }
980
981         return 0;
982 }
983
984 /*
985  * XXX The line_out pin widget connection list may not be set to the
986  * desired DAC nid. This is the case on 927x where ports A and B can
987  * be routed to several DACs.
988  *
989  * This requires an analysis of the line-out/hp pin configuration
990  * to provide a best fit for pin/DAC configurations that are routable.
991  * For now, 927x DAC4 is not supported and 927x DAC1 output to ports
992  * A and B is not supported.
993  */
994 /* fill in the dac_nids table from the parsed pin configuration */
995 static int stac92xx_auto_fill_dac_nids(struct hda_codec *codec,
996                                        const struct auto_pin_cfg *cfg)
997 {
998         struct sigmatel_spec *spec = codec->spec;
999         hda_nid_t nid;
1000         int i;
1001
1002         /* check the pins hardwired to audio widget */
1003         for (i = 0; i < cfg->line_outs; i++) {
1004                 nid = cfg->line_out_pins[i];
1005                 spec->multiout.dac_nids[i] = snd_hda_codec_read(codec, nid, 0,
1006                                         AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
1007         }
1008
1009         spec->multiout.num_dacs = cfg->line_outs;
1010
1011         return 0;
1012 }
1013
1014 /* create volume control/switch for the given prefx type */
1015 static int create_controls(struct sigmatel_spec *spec, const char *pfx, hda_nid_t nid, int chs)
1016 {
1017         char name[32];
1018         int err;
1019
1020         sprintf(name, "%s Playback Volume", pfx);
1021         err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, name,
1022                                    HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1023         if (err < 0)
1024                 return err;
1025         sprintf(name, "%s Playback Switch", pfx);
1026         err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, name,
1027                                    HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1028         if (err < 0)
1029                 return err;
1030         return 0;
1031 }
1032
1033 /* add playback controls from the parsed DAC table */
1034 static int stac92xx_auto_create_multi_out_ctls(struct sigmatel_spec *spec,
1035                                                const struct auto_pin_cfg *cfg)
1036 {
1037         static const char *chname[4] = {
1038                 "Front", "Surround", NULL /*CLFE*/, "Side"
1039         };
1040         hda_nid_t nid;
1041         int i, err;
1042
1043         for (i = 0; i < cfg->line_outs; i++) {
1044                 if (!spec->multiout.dac_nids[i])
1045                         continue;
1046
1047                 nid = spec->multiout.dac_nids[i];
1048
1049                 if (i == 2) {
1050                         /* Center/LFE */
1051                         err = create_controls(spec, "Center", nid, 1);
1052                         if (err < 0)
1053                                 return err;
1054                         err = create_controls(spec, "LFE", nid, 2);
1055                         if (err < 0)
1056                                 return err;
1057                 } else {
1058                         err = create_controls(spec, chname[i], nid, 3);
1059                         if (err < 0)
1060                                 return err;
1061                 }
1062         }
1063
1064         if (spec->line_switch)
1065                 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Line In as Output Switch", cfg->input_pins[AUTO_PIN_LINE] << 8)) < 0)
1066                         return err;
1067
1068         if (spec->mic_switch)
1069                 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Mic as Output Switch", (cfg->input_pins[AUTO_PIN_MIC] << 8) | 1)) < 0)
1070                         return err;
1071
1072         return 0;
1073 }
1074
1075 static int check_in_dac_nids(struct sigmatel_spec *spec, hda_nid_t nid)
1076 {
1077         int i;
1078
1079         for (i = 0; i < spec->multiout.num_dacs; i++) {
1080                 if (spec->multiout.dac_nids[i] == nid)
1081                         return 1;
1082         }
1083         if (spec->multiout.hp_nid == nid)
1084                 return 1;
1085         return 0;
1086 }
1087
1088 static int add_spec_dacs(struct sigmatel_spec *spec, hda_nid_t nid)
1089 {
1090         if (!spec->multiout.hp_nid)
1091                 spec->multiout.hp_nid = nid;
1092         else if (spec->multiout.num_dacs > 4) {
1093                 printk(KERN_WARNING "stac92xx: No space for DAC 0x%x\n", nid);
1094                 return 1;
1095         } else {
1096                 spec->multiout.dac_nids[spec->multiout.num_dacs] = nid;
1097                 spec->multiout.num_dacs++;
1098         }
1099         return 0;
1100 }
1101
1102 /* add playback controls for Speaker and HP outputs */
1103 static int stac92xx_auto_create_hp_ctls(struct hda_codec *codec,
1104                                         struct auto_pin_cfg *cfg)
1105 {
1106         struct sigmatel_spec *spec = codec->spec;
1107         hda_nid_t nid;
1108         int i, old_num_dacs, err;
1109
1110         old_num_dacs = spec->multiout.num_dacs;
1111         for (i = 0; i < cfg->hp_outs; i++) {
1112                 unsigned int wid_caps = get_wcaps(codec, cfg->hp_pins[i]);
1113                 if (wid_caps & AC_WCAP_UNSOL_CAP)
1114                         spec->hp_detect = 1;
1115                 nid = snd_hda_codec_read(codec, cfg->hp_pins[i], 0,
1116                                          AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
1117                 if (check_in_dac_nids(spec, nid))
1118                         nid = 0;
1119                 if (! nid)
1120                         continue;
1121                 add_spec_dacs(spec, nid);
1122         }
1123         for (i = 0; i < cfg->speaker_outs; i++) {
1124                 nid = snd_hda_codec_read(codec, cfg->speaker_pins[0], 0,
1125                                          AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
1126                 if (check_in_dac_nids(spec, nid))
1127                         nid = 0;
1128                 if (check_in_dac_nids(spec, nid))
1129                         nid = 0;
1130                 if (! nid)
1131                         continue;
1132                 add_spec_dacs(spec, nid);
1133         }
1134
1135         for (i = old_num_dacs; i < spec->multiout.num_dacs; i++) {
1136                 static const char *pfxs[] = {
1137                         "Speaker", "External Speaker", "Speaker2",
1138                 };
1139                 err = create_controls(spec, pfxs[i - old_num_dacs],
1140                                       spec->multiout.dac_nids[i], 3);
1141                 if (err < 0)
1142                         return err;
1143         }
1144         if (spec->multiout.hp_nid) {
1145                 const char *pfx;
1146                 if (old_num_dacs == spec->multiout.num_dacs)
1147                         pfx = "Master";
1148                 else
1149                         pfx = "Headphone";
1150                 err = create_controls(spec, pfx, spec->multiout.hp_nid, 3);
1151                 if (err < 0)
1152                         return err;
1153         }
1154
1155         return 0;
1156 }
1157
1158 /* create playback/capture controls for input pins */
1159 static int stac92xx_auto_create_analog_input_ctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
1160 {
1161         struct sigmatel_spec *spec = codec->spec;
1162         struct hda_input_mux *imux = &spec->private_imux;
1163         hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
1164         int i, j, k;
1165
1166         for (i = 0; i < AUTO_PIN_LAST; i++) {
1167                 int index = -1;
1168                 if (cfg->input_pins[i]) {
1169                         imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
1170
1171                         for (j=0; j<spec->num_muxes; j++) {
1172                                 int num_cons = snd_hda_get_connections(codec, spec->mux_nids[j], con_lst, HDA_MAX_NUM_INPUTS);
1173                                 for (k=0; k<num_cons; k++)
1174                                         if (con_lst[k] == cfg->input_pins[i]) {
1175                                                 index = k;
1176                                                 break;
1177                                         }
1178                                 if (index >= 0)
1179                                         break;
1180                         }
1181                         imux->items[imux->num_items].index = index;
1182                         imux->num_items++;
1183                 }
1184         }
1185
1186         if (imux->num_items == 1) {
1187                 /*
1188                  * Set the current input for the muxes.
1189                  * The STAC9221 has two input muxes with identical source
1190                  * NID lists.  Hopefully this won't get confused.
1191                  */
1192                 for (i = 0; i < spec->num_muxes; i++) {
1193                         snd_hda_codec_write(codec, spec->mux_nids[i], 0,
1194                                             AC_VERB_SET_CONNECT_SEL,
1195                                             imux->items[0].index);
1196                 }
1197         }
1198
1199         return 0;
1200 }
1201
1202 static void stac92xx_auto_init_multi_out(struct hda_codec *codec)
1203 {
1204         struct sigmatel_spec *spec = codec->spec;
1205         int i;
1206
1207         for (i = 0; i < spec->autocfg.line_outs; i++) {
1208                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
1209                 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
1210         }
1211 }
1212
1213 static void stac92xx_auto_init_hp_out(struct hda_codec *codec)
1214 {
1215         struct sigmatel_spec *spec = codec->spec;
1216         int i;
1217
1218         for (i = 0; i < spec->autocfg.hp_outs; i++) {
1219                 hda_nid_t pin;
1220                 pin = spec->autocfg.hp_pins[i];
1221                 if (pin) /* connect to front */
1222                         stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
1223         }
1224         for (i = 0; i < spec->autocfg.speaker_outs; i++) {
1225                 hda_nid_t pin;
1226                 pin = spec->autocfg.speaker_pins[i];
1227                 if (pin) /* connect to front */
1228                         stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN);
1229         }
1230 }
1231
1232 static int stac92xx_parse_auto_config(struct hda_codec *codec, hda_nid_t dig_out, hda_nid_t dig_in)
1233 {
1234         struct sigmatel_spec *spec = codec->spec;
1235         int err;
1236
1237         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
1238                 return err;
1239         if (! spec->autocfg.line_outs)
1240                 return 0; /* can't find valid pin config */
1241
1242         if ((err = stac92xx_add_dyn_out_pins(codec, &spec->autocfg)) < 0)
1243                 return err;
1244         if (spec->multiout.num_dacs == 0)
1245                 if ((err = stac92xx_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
1246                         return err;
1247
1248         if ((err = stac92xx_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
1249             (err = stac92xx_auto_create_hp_ctls(codec, &spec->autocfg)) < 0 ||
1250             (err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
1251                 return err;
1252
1253         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
1254         if (spec->multiout.max_channels > 2)
1255                 spec->surr_switch = 1;
1256
1257         if (spec->autocfg.dig_out_pin)
1258                 spec->multiout.dig_out_nid = dig_out;
1259         if (spec->autocfg.dig_in_pin)
1260                 spec->dig_in_nid = dig_in;
1261
1262         if (spec->kctl_alloc)
1263                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
1264
1265         spec->input_mux = &spec->private_imux;
1266
1267         return 1;
1268 }
1269
1270 /* add playback controls for HP output */
1271 static int stac9200_auto_create_hp_ctls(struct hda_codec *codec,
1272                                         struct auto_pin_cfg *cfg)
1273 {
1274         struct sigmatel_spec *spec = codec->spec;
1275         hda_nid_t pin = cfg->hp_pins[0];
1276         unsigned int wid_caps;
1277
1278         if (! pin)
1279                 return 0;
1280
1281         wid_caps = get_wcaps(codec, pin);
1282         if (wid_caps & AC_WCAP_UNSOL_CAP)
1283                 spec->hp_detect = 1;
1284
1285         return 0;
1286 }
1287
1288 /* add playback controls for LFE output */
1289 static int stac9200_auto_create_lfe_ctls(struct hda_codec *codec,
1290                                         struct auto_pin_cfg *cfg)
1291 {
1292         struct sigmatel_spec *spec = codec->spec;
1293         int err;
1294         hda_nid_t lfe_pin = 0x0;
1295         int i;
1296
1297         /*
1298          * search speaker outs and line outs for a mono speaker pin
1299          * with an amp.  If one is found, add LFE controls
1300          * for it.
1301          */
1302         for (i = 0; i < spec->autocfg.speaker_outs && lfe_pin == 0x0; i++) {
1303                 hda_nid_t pin = spec->autocfg.speaker_pins[i];
1304                 unsigned long wcaps = get_wcaps(codec, pin);
1305                 wcaps &= (AC_WCAP_STEREO | AC_WCAP_OUT_AMP);
1306                 if (wcaps == AC_WCAP_OUT_AMP)
1307                         /* found a mono speaker with an amp, must be lfe */
1308                         lfe_pin = pin;
1309         }
1310
1311         /* if speaker_outs is 0, then speakers may be in line_outs */
1312         if (lfe_pin == 0 && spec->autocfg.speaker_outs == 0) {
1313                 for (i = 0; i < spec->autocfg.line_outs && lfe_pin == 0x0; i++) {
1314                         hda_nid_t pin = spec->autocfg.line_out_pins[i];
1315                         unsigned long cfg;
1316                         cfg = snd_hda_codec_read(codec, pin, 0,
1317                                                  AC_VERB_GET_CONFIG_DEFAULT,
1318                                                  0x00);
1319                         if (get_defcfg_device(cfg) == AC_JACK_SPEAKER) {
1320                                 unsigned long wcaps = get_wcaps(codec, pin);
1321                                 wcaps &= (AC_WCAP_STEREO | AC_WCAP_OUT_AMP);
1322                                 if (wcaps == AC_WCAP_OUT_AMP)
1323                                         /* found a mono speaker with an amp,
1324                                            must be lfe */
1325                                         lfe_pin = pin;
1326                         }
1327                 }
1328         }
1329
1330         if (lfe_pin) {
1331                 err = create_controls(spec, "LFE", lfe_pin, 1);
1332                 if (err < 0)
1333                         return err;
1334         }
1335
1336         return 0;
1337 }
1338
1339 static int stac9200_parse_auto_config(struct hda_codec *codec)
1340 {
1341         struct sigmatel_spec *spec = codec->spec;
1342         int err;
1343
1344         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
1345                 return err;
1346
1347         if ((err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
1348                 return err;
1349
1350         if ((err = stac9200_auto_create_hp_ctls(codec, &spec->autocfg)) < 0)
1351                 return err;
1352
1353         if ((err = stac9200_auto_create_lfe_ctls(codec, &spec->autocfg)) < 0)
1354                 return err;
1355
1356         if (spec->autocfg.dig_out_pin)
1357                 spec->multiout.dig_out_nid = 0x05;
1358         if (spec->autocfg.dig_in_pin)
1359                 spec->dig_in_nid = 0x04;
1360
1361         if (spec->kctl_alloc)
1362                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
1363
1364         spec->input_mux = &spec->private_imux;
1365
1366         return 1;
1367 }
1368
1369 /*
1370  * Early 2006 Intel Macintoshes with STAC9220X5 codecs seem to have a
1371  * funky external mute control using GPIO pins.
1372  */
1373
1374 static void stac922x_gpio_mute(struct hda_codec *codec, int pin, int muted)
1375 {
1376         unsigned int gpiostate, gpiomask, gpiodir;
1377
1378         gpiostate = snd_hda_codec_read(codec, codec->afg, 0,
1379                                        AC_VERB_GET_GPIO_DATA, 0);
1380
1381         if (!muted)
1382                 gpiostate |= (1 << pin);
1383         else
1384                 gpiostate &= ~(1 << pin);
1385
1386         gpiomask = snd_hda_codec_read(codec, codec->afg, 0,
1387                                       AC_VERB_GET_GPIO_MASK, 0);
1388         gpiomask |= (1 << pin);
1389
1390         gpiodir = snd_hda_codec_read(codec, codec->afg, 0,
1391                                      AC_VERB_GET_GPIO_DIRECTION, 0);
1392         gpiodir |= (1 << pin);
1393
1394         /* AppleHDA seems to do this -- WTF is this verb?? */
1395         snd_hda_codec_write(codec, codec->afg, 0, 0x7e7, 0);
1396
1397         snd_hda_codec_write(codec, codec->afg, 0,
1398                             AC_VERB_SET_GPIO_MASK, gpiomask);
1399         snd_hda_codec_write(codec, codec->afg, 0,
1400                             AC_VERB_SET_GPIO_DIRECTION, gpiodir);
1401
1402         msleep(1);
1403
1404         snd_hda_codec_write(codec, codec->afg, 0,
1405                             AC_VERB_SET_GPIO_DATA, gpiostate);
1406 }
1407
1408 static int stac92xx_init(struct hda_codec *codec)
1409 {
1410         struct sigmatel_spec *spec = codec->spec;
1411         struct auto_pin_cfg *cfg = &spec->autocfg;
1412         int i;
1413
1414         snd_hda_sequence_write(codec, spec->init);
1415
1416         /* set up pins */
1417         if (spec->hp_detect) {
1418                 /* Enable unsolicited responses on the HP widget */
1419                 for (i = 0; i < cfg->hp_outs; i++)
1420                         if (get_wcaps(codec, cfg->hp_pins[i]) & AC_WCAP_UNSOL_CAP)
1421                                 snd_hda_codec_write(codec, cfg->hp_pins[i], 0,
1422                                                     AC_VERB_SET_UNSOLICITED_ENABLE,
1423                                                     STAC_UNSOL_ENABLE);
1424                 /* fake event to set up pins */
1425                 codec->patch_ops.unsol_event(codec, STAC_HP_EVENT << 26);
1426                 /* enable the headphones by default.  If/when unsol_event detection works, this will be ignored */
1427                 stac92xx_auto_init_hp_out(codec);
1428         } else {
1429                 stac92xx_auto_init_multi_out(codec);
1430                 stac92xx_auto_init_hp_out(codec);
1431         }
1432         for (i = 0; i < AUTO_PIN_LAST; i++) {
1433                 hda_nid_t nid = cfg->input_pins[i];
1434                 if (nid) {
1435                         unsigned int pinctl = AC_PINCTL_IN_EN;
1436                         if (i == AUTO_PIN_MIC || i == AUTO_PIN_FRONT_MIC)
1437                                 pinctl |= stac92xx_get_vref(codec, nid);
1438                         stac92xx_auto_set_pinctl(codec, nid, pinctl);
1439                 }
1440         }
1441         if (cfg->dig_out_pin)
1442                 stac92xx_auto_set_pinctl(codec, cfg->dig_out_pin,
1443                                          AC_PINCTL_OUT_EN);
1444         if (cfg->dig_in_pin)
1445                 stac92xx_auto_set_pinctl(codec, cfg->dig_in_pin,
1446                                          AC_PINCTL_IN_EN);
1447
1448         if (spec->gpio_mute) {
1449                 stac922x_gpio_mute(codec, 0, 0);
1450                 stac922x_gpio_mute(codec, 1, 0);
1451         }
1452
1453         return 0;
1454 }
1455
1456 static void stac92xx_free(struct hda_codec *codec)
1457 {
1458         struct sigmatel_spec *spec = codec->spec;
1459         int i;
1460
1461         if (! spec)
1462                 return;
1463
1464         if (spec->kctl_alloc) {
1465                 for (i = 0; i < spec->num_kctl_used; i++)
1466                         kfree(spec->kctl_alloc[i].name);
1467                 kfree(spec->kctl_alloc);
1468         }
1469
1470         if (spec->bios_pin_configs)
1471                 kfree(spec->bios_pin_configs);
1472
1473         kfree(spec);
1474 }
1475
1476 static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
1477                                 unsigned int flag)
1478 {
1479         unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
1480                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
1481         snd_hda_codec_write(codec, nid, 0,
1482                         AC_VERB_SET_PIN_WIDGET_CONTROL,
1483                         pin_ctl | flag);
1484 }
1485
1486 static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
1487                                   unsigned int flag)
1488 {
1489         unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
1490                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
1491         snd_hda_codec_write(codec, nid, 0,
1492                         AC_VERB_SET_PIN_WIDGET_CONTROL,
1493                         pin_ctl & ~flag);
1494 }
1495
1496 static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
1497 {
1498         struct sigmatel_spec *spec = codec->spec;
1499         struct auto_pin_cfg *cfg = &spec->autocfg;
1500         int i, presence;
1501
1502         if ((res >> 26) != STAC_HP_EVENT)
1503                 return;
1504
1505         presence = 0;
1506         for (i = 0; i < cfg->hp_outs; i++) {
1507                 int p = snd_hda_codec_read(codec, cfg->hp_pins[i], 0,
1508                                            AC_VERB_GET_PIN_SENSE, 0x00);
1509                 if (p & (1 << 31))
1510                         presence++;
1511         }
1512
1513         if (presence) {
1514                 /* disable lineouts, enable hp */
1515                 for (i = 0; i < cfg->line_outs; i++)
1516                         stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
1517                                                 AC_PINCTL_OUT_EN);
1518                 for (i = 0; i < cfg->speaker_outs; i++)
1519                         stac92xx_reset_pinctl(codec, cfg->speaker_pins[i],
1520                                                 AC_PINCTL_OUT_EN);
1521                 for (i = 0; i < cfg->hp_outs; i++)
1522                         stac92xx_set_pinctl(codec, cfg->hp_pins[i],
1523                                             AC_PINCTL_OUT_EN);
1524         } else {
1525                 /* enable lineouts, disable hp */
1526                 for (i = 0; i < cfg->line_outs; i++)
1527                         stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
1528                                                 AC_PINCTL_OUT_EN);
1529                 for (i = 0; i < cfg->speaker_outs; i++)
1530                         stac92xx_set_pinctl(codec, cfg->speaker_pins[i],
1531                                                 AC_PINCTL_OUT_EN);
1532                 for (i = 0; i < cfg->hp_outs; i++)
1533                         stac92xx_reset_pinctl(codec, cfg->hp_pins[i],
1534                                               AC_PINCTL_OUT_EN);
1535         }
1536
1537
1538 #ifdef CONFIG_PM
1539 static int stac92xx_resume(struct hda_codec *codec)
1540 {
1541         struct sigmatel_spec *spec = codec->spec;
1542         int i;
1543
1544         stac92xx_init(codec);
1545         stac92xx_set_config_regs(codec);
1546         for (i = 0; i < spec->num_mixers; i++)
1547                 snd_hda_resume_ctls(codec, spec->mixers[i]);
1548         if (spec->multiout.dig_out_nid)
1549                 snd_hda_resume_spdif_out(codec);
1550         if (spec->dig_in_nid)
1551                 snd_hda_resume_spdif_in(codec);
1552
1553         return 0;
1554 }
1555 #endif
1556
1557 static struct hda_codec_ops stac92xx_patch_ops = {
1558         .build_controls = stac92xx_build_controls,
1559         .build_pcms = stac92xx_build_pcms,
1560         .init = stac92xx_init,
1561         .free = stac92xx_free,
1562         .unsol_event = stac92xx_unsol_event,
1563 #ifdef CONFIG_PM
1564         .resume = stac92xx_resume,
1565 #endif
1566 };
1567
1568 static int patch_stac9200(struct hda_codec *codec)
1569 {
1570         struct sigmatel_spec *spec;
1571         int err;
1572
1573         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1574         if (spec == NULL)
1575                 return -ENOMEM;
1576
1577         codec->spec = spec;
1578         spec->num_pins = 8;
1579         spec->pin_nids = stac9200_pin_nids;
1580         spec->board_config = snd_hda_check_board_config(codec, stac9200_cfg_tbl);
1581         if (spec->board_config < 0) {
1582                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9200, using BIOS defaults\n");
1583                 err = stac92xx_save_bios_config_regs(codec);
1584                 if (err < 0) {
1585                         stac92xx_free(codec);
1586                         return err;
1587                 }
1588                 spec->pin_configs = spec->bios_pin_configs;
1589         } else {
1590                 spec->pin_configs = stac9200_brd_tbl[spec->board_config];
1591                 stac92xx_set_config_regs(codec);
1592         }
1593
1594         spec->multiout.max_channels = 2;
1595         spec->multiout.num_dacs = 1;
1596         spec->multiout.dac_nids = stac9200_dac_nids;
1597         spec->adc_nids = stac9200_adc_nids;
1598         spec->mux_nids = stac9200_mux_nids;
1599         spec->num_muxes = 1;
1600
1601         spec->init = stac9200_core_init;
1602         spec->mixer = stac9200_mixer;
1603
1604         err = stac9200_parse_auto_config(codec);
1605         if (err < 0) {
1606                 stac92xx_free(codec);
1607                 return err;
1608         }
1609
1610         codec->patch_ops = stac92xx_patch_ops;
1611
1612         return 0;
1613 }
1614
1615 static int patch_stac922x(struct hda_codec *codec)
1616 {
1617         struct sigmatel_spec *spec;
1618         int err;
1619
1620         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1621         if (spec == NULL)
1622                 return -ENOMEM;
1623
1624         codec->spec = spec;
1625         spec->num_pins = 10;
1626         spec->pin_nids = stac922x_pin_nids;
1627         spec->board_config = snd_hda_check_board_config(codec, stac922x_cfg_tbl);
1628         if (spec->board_config < 0) {
1629                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC922x, "
1630                         "using BIOS defaults\n");
1631                 err = stac92xx_save_bios_config_regs(codec);
1632                 if (err < 0) {
1633                         stac92xx_free(codec);
1634                         return err;
1635                 }
1636                 spec->pin_configs = spec->bios_pin_configs;
1637         } else if (stac922x_brd_tbl[spec->board_config] != NULL) {
1638                 spec->pin_configs = stac922x_brd_tbl[spec->board_config];
1639                 stac92xx_set_config_regs(codec);
1640         }
1641
1642         spec->adc_nids = stac922x_adc_nids;
1643         spec->mux_nids = stac922x_mux_nids;
1644         spec->num_muxes = 2;
1645
1646         spec->init = stac922x_core_init;
1647         spec->mixer = stac922x_mixer;
1648
1649         spec->multiout.dac_nids = spec->dac_nids;
1650         
1651         err = stac92xx_parse_auto_config(codec, 0x08, 0x09);
1652         if (err < 0) {
1653                 stac92xx_free(codec);
1654                 return err;
1655         }
1656
1657         if (spec->board_config == STAC_MACMINI)
1658                 spec->gpio_mute = 1;
1659
1660         codec->patch_ops = stac92xx_patch_ops;
1661
1662         return 0;
1663 }
1664
1665 static int patch_stac927x(struct hda_codec *codec)
1666 {
1667         struct sigmatel_spec *spec;
1668         int err;
1669
1670         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1671         if (spec == NULL)
1672                 return -ENOMEM;
1673
1674         codec->spec = spec;
1675         spec->num_pins = 14;
1676         spec->pin_nids = stac927x_pin_nids;
1677         spec->board_config = snd_hda_check_board_config(codec, stac927x_cfg_tbl);
1678         if (spec->board_config < 0) {
1679                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC927x, using BIOS defaults\n");
1680                 err = stac92xx_save_bios_config_regs(codec);
1681                 if (err < 0) {
1682                         stac92xx_free(codec);
1683                         return err;
1684                 }
1685                 spec->pin_configs = spec->bios_pin_configs;
1686         } else if (stac927x_brd_tbl[spec->board_config] != NULL) {
1687                 spec->pin_configs = stac927x_brd_tbl[spec->board_config];
1688                 stac92xx_set_config_regs(codec);
1689         }
1690
1691         switch (spec->board_config) {
1692         case STAC_D965_3ST:
1693                 spec->adc_nids = stac927x_adc_nids;
1694                 spec->mux_nids = stac927x_mux_nids;
1695                 spec->num_muxes = 3;
1696                 spec->init = d965_core_init;
1697                 spec->mixer = stac9227_mixer;
1698                 break;
1699         case STAC_D965_5ST:
1700                 spec->adc_nids = stac927x_adc_nids;
1701                 spec->mux_nids = stac927x_mux_nids;
1702                 spec->num_muxes = 3;
1703                 spec->init = d965_core_init;
1704                 spec->mixer = stac9227_mixer;
1705                 break;
1706         default:
1707                 spec->adc_nids = stac927x_adc_nids;
1708                 spec->mux_nids = stac927x_mux_nids;
1709                 spec->num_muxes = 3;
1710                 spec->init = stac927x_core_init;
1711                 spec->mixer = stac927x_mixer;
1712         }
1713
1714         spec->multiout.dac_nids = spec->dac_nids;
1715
1716         err = stac92xx_parse_auto_config(codec, 0x1e, 0x20);
1717         if (err < 0) {
1718                 stac92xx_free(codec);
1719                 return err;
1720         }
1721
1722         codec->patch_ops = stac92xx_patch_ops;
1723
1724         return 0;
1725 }
1726
1727 static int patch_stac9205(struct hda_codec *codec)
1728 {
1729         struct sigmatel_spec *spec;
1730         int err;
1731
1732         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1733         if (spec == NULL)
1734                 return -ENOMEM;
1735
1736         codec->spec = spec;
1737         spec->num_pins = 14;
1738         spec->pin_nids = stac9205_pin_nids;
1739         spec->board_config = snd_hda_check_board_config(codec, stac9205_cfg_tbl);
1740         if (spec->board_config < 0) {
1741                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9205, using BIOS defaults\n");
1742                 err = stac92xx_save_bios_config_regs(codec);
1743                 if (err < 0) {
1744                         stac92xx_free(codec);
1745                         return err;
1746                 }
1747                 spec->pin_configs = spec->bios_pin_configs;
1748         } else {
1749                 spec->pin_configs = stac9205_brd_tbl[spec->board_config];
1750                 stac92xx_set_config_regs(codec);
1751         }
1752
1753         spec->adc_nids = stac9205_adc_nids;
1754         spec->mux_nids = stac9205_mux_nids;
1755         spec->num_muxes = 3;
1756
1757         spec->init = stac9205_core_init;
1758         spec->mixer = stac9205_mixer;
1759
1760         spec->multiout.dac_nids = spec->dac_nids;
1761
1762         err = stac92xx_parse_auto_config(codec, 0x1f, 0x20);
1763         if (err < 0) {
1764                 stac92xx_free(codec);
1765                 return err;
1766         }
1767
1768         codec->patch_ops = stac92xx_patch_ops;
1769
1770         return 0;
1771 }
1772
1773 /*
1774  * STAC9872 hack
1775  */
1776
1777 /* static config for Sony VAIO FE550G and Sony VAIO AR */
1778 static hda_nid_t vaio_dacs[] = { 0x2 };
1779 #define VAIO_HP_DAC     0x5
1780 static hda_nid_t vaio_adcs[] = { 0x8 /*,0x6*/ };
1781 static hda_nid_t vaio_mux_nids[] = { 0x15 };
1782
1783 static struct hda_input_mux vaio_mux = {
1784         .num_items = 2,
1785         .items = {
1786                 /* { "HP", 0x0 }, */
1787                 { "Line", 0x1 },
1788                 { "Mic", 0x2 },
1789                 { "PCM", 0x3 },
1790         }
1791 };
1792
1793 static struct hda_verb vaio_init[] = {
1794         {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
1795         {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
1796         {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
1797         {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
1798         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
1799         {0x15, AC_VERB_SET_CONNECT_SEL, 0x2}, /* mic-sel: 0a,0d,14,02 */
1800         {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
1801         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
1802         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
1803         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
1804         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
1805         {}
1806 };
1807
1808 static struct hda_verb vaio_ar_init[] = {
1809         {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
1810         {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
1811         {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
1812         {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
1813 /*      {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },*/ /* Optical Out */
1814         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
1815         {0x15, AC_VERB_SET_CONNECT_SEL, 0x2}, /* mic-sel: 0a,0d,14,02 */
1816         {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
1817         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
1818 /*      {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},*/ /* Optical Out */
1819         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
1820         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
1821         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
1822         {}
1823 };
1824
1825 /* bind volumes of both NID 0x02 and 0x05 */
1826 static int vaio_master_vol_put(struct snd_kcontrol *kcontrol,
1827                                struct snd_ctl_elem_value *ucontrol)
1828 {
1829         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1830         long *valp = ucontrol->value.integer.value;
1831         int change;
1832
1833         change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
1834                                           0x7f, valp[0] & 0x7f);
1835         change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
1836                                            0x7f, valp[1] & 0x7f);
1837         snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
1838                                  0x7f, valp[0] & 0x7f);
1839         snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
1840                                  0x7f, valp[1] & 0x7f);
1841         return change;
1842 }
1843
1844 /* bind volumes of both NID 0x02 and 0x05 */
1845 static int vaio_master_sw_put(struct snd_kcontrol *kcontrol,
1846                               struct snd_ctl_elem_value *ucontrol)
1847 {
1848         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1849         long *valp = ucontrol->value.integer.value;
1850         int change;
1851
1852         change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
1853                                           0x80, (valp[0] ? 0 : 0x80));
1854         change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
1855                                            0x80, (valp[1] ? 0 : 0x80));
1856         snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
1857                                  0x80, (valp[0] ? 0 : 0x80));
1858         snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
1859                                  0x80, (valp[1] ? 0 : 0x80));
1860         return change;
1861 }
1862
1863 static struct snd_kcontrol_new vaio_mixer[] = {
1864         {
1865                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1866                 .name = "Master Playback Volume",
1867                 .info = snd_hda_mixer_amp_volume_info,
1868                 .get = snd_hda_mixer_amp_volume_get,
1869                 .put = vaio_master_vol_put,
1870                 .tlv = { .c = snd_hda_mixer_amp_tlv },
1871                 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
1872         },
1873         {
1874                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1875                 .name = "Master Playback Switch",
1876                 .info = snd_hda_mixer_amp_switch_info,
1877                 .get = snd_hda_mixer_amp_switch_get,
1878                 .put = vaio_master_sw_put,
1879                 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
1880         },
1881         /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
1882         HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
1883         HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
1884         {
1885                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1886                 .name = "Capture Source",
1887                 .count = 1,
1888                 .info = stac92xx_mux_enum_info,
1889                 .get = stac92xx_mux_enum_get,
1890                 .put = stac92xx_mux_enum_put,
1891         },
1892         {}
1893 };
1894
1895 static struct snd_kcontrol_new vaio_ar_mixer[] = {
1896         {
1897                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1898                 .name = "Master Playback Volume",
1899                 .info = snd_hda_mixer_amp_volume_info,
1900                 .get = snd_hda_mixer_amp_volume_get,
1901                 .put = vaio_master_vol_put,
1902                 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
1903         },
1904         {
1905                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1906                 .name = "Master Playback Switch",
1907                 .info = snd_hda_mixer_amp_switch_info,
1908                 .get = snd_hda_mixer_amp_switch_get,
1909                 .put = vaio_master_sw_put,
1910                 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
1911         },
1912         /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
1913         HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
1914         HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
1915         /*HDA_CODEC_MUTE("Optical Out Switch", 0x10, 0, HDA_OUTPUT),
1916         HDA_CODEC_VOLUME("Optical Out Volume", 0x10, 0, HDA_OUTPUT),*/
1917         {
1918                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1919                 .name = "Capture Source",
1920                 .count = 1,
1921                 .info = stac92xx_mux_enum_info,
1922                 .get = stac92xx_mux_enum_get,
1923                 .put = stac92xx_mux_enum_put,
1924         },
1925         {}
1926 };
1927
1928 static struct hda_codec_ops stac9872_patch_ops = {
1929         .build_controls = stac92xx_build_controls,
1930         .build_pcms = stac92xx_build_pcms,
1931         .init = stac92xx_init,
1932         .free = stac92xx_free,
1933 #ifdef CONFIG_PM
1934         .resume = stac92xx_resume,
1935 #endif
1936 };
1937
1938 enum { /* FE and SZ series. id=0x83847661 and subsys=0x104D0700 or 104D1000. */
1939        CXD9872RD_VAIO,
1940        /* Unknown. id=0x83847662 and subsys=0x104D1200 or 104D1000. */
1941        STAC9872AK_VAIO, 
1942        /* Unknown. id=0x83847661 and subsys=0x104D1200. */
1943        STAC9872K_VAIO,
1944        /* AR Series. id=0x83847664 and subsys=104D1300 */
1945        CXD9872AKD_VAIO 
1946      };
1947
1948 static struct hda_board_config stac9872_cfg_tbl[] = {
1949         { .modelname = "vaio", .config = CXD9872RD_VAIO },
1950         { .modelname = "vaio-ar", .config = CXD9872AKD_VAIO },
1951         { .pci_subvendor = 0x104d, .pci_subdevice = 0x81e6,
1952           .config = CXD9872RD_VAIO },
1953         { .pci_subvendor = 0x104d, .pci_subdevice = 0x81ef,
1954           .config = CXD9872RD_VAIO },
1955         { .pci_subvendor = 0x104d, .pci_subdevice = 0x81fd,
1956           .config = CXD9872AKD_VAIO },
1957         {}
1958 };
1959
1960 static int patch_stac9872(struct hda_codec *codec)
1961 {
1962         struct sigmatel_spec *spec;
1963         int board_config;
1964
1965         board_config = snd_hda_check_board_config(codec, stac9872_cfg_tbl);
1966         if (board_config < 0)
1967                 /* unknown config, let generic-parser do its job... */
1968                 return snd_hda_parse_generic_codec(codec);
1969         
1970         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1971         if (spec == NULL)
1972                 return -ENOMEM;
1973
1974         codec->spec = spec;
1975         switch (board_config) {
1976         case CXD9872RD_VAIO:
1977         case STAC9872AK_VAIO:
1978         case STAC9872K_VAIO:
1979                 spec->mixer = vaio_mixer;
1980                 spec->init = vaio_init;
1981                 spec->multiout.max_channels = 2;
1982                 spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
1983                 spec->multiout.dac_nids = vaio_dacs;
1984                 spec->multiout.hp_nid = VAIO_HP_DAC;
1985                 spec->num_adcs = ARRAY_SIZE(vaio_adcs);
1986                 spec->adc_nids = vaio_adcs;
1987                 spec->input_mux = &vaio_mux;
1988                 spec->mux_nids = vaio_mux_nids;
1989                 break;
1990         
1991         case CXD9872AKD_VAIO:
1992                 spec->mixer = vaio_ar_mixer;
1993                 spec->init = vaio_ar_init;
1994                 spec->multiout.max_channels = 2;
1995                 spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
1996                 spec->multiout.dac_nids = vaio_dacs;
1997                 spec->multiout.hp_nid = VAIO_HP_DAC;
1998                 spec->num_adcs = ARRAY_SIZE(vaio_adcs);
1999                 spec->adc_nids = vaio_adcs;
2000                 spec->input_mux = &vaio_mux;
2001                 spec->mux_nids = vaio_mux_nids;
2002                 break;
2003         }
2004
2005         codec->patch_ops = stac9872_patch_ops;
2006         return 0;
2007 }
2008
2009
2010 /*
2011  * patch entries
2012  */
2013 struct hda_codec_preset snd_hda_preset_sigmatel[] = {
2014         { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
2015         { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
2016         { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
2017         { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
2018         { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
2019         { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
2020         { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
2021         { .id = 0x83847618, .name = "STAC9227", .patch = patch_stac927x },
2022         { .id = 0x83847619, .name = "STAC9227", .patch = patch_stac927x },
2023         { .id = 0x83847616, .name = "STAC9228", .patch = patch_stac927x },
2024         { .id = 0x83847617, .name = "STAC9228", .patch = patch_stac927x },
2025         { .id = 0x83847614, .name = "STAC9229", .patch = patch_stac927x },
2026         { .id = 0x83847615, .name = "STAC9229", .patch = patch_stac927x },
2027         { .id = 0x83847620, .name = "STAC9274", .patch = patch_stac927x },
2028         { .id = 0x83847621, .name = "STAC9274D", .patch = patch_stac927x },
2029         { .id = 0x83847622, .name = "STAC9273X", .patch = patch_stac927x },
2030         { .id = 0x83847623, .name = "STAC9273D", .patch = patch_stac927x },
2031         { .id = 0x83847624, .name = "STAC9272X", .patch = patch_stac927x },
2032         { .id = 0x83847625, .name = "STAC9272D", .patch = patch_stac927x },
2033         { .id = 0x83847626, .name = "STAC9271X", .patch = patch_stac927x },
2034         { .id = 0x83847627, .name = "STAC9271D", .patch = patch_stac927x },
2035         { .id = 0x83847628, .name = "STAC9274X5NH", .patch = patch_stac927x },
2036         { .id = 0x83847629, .name = "STAC9274D5NH", .patch = patch_stac927x },
2037         /* The following does not take into account .id=0x83847661 when subsys =
2038          * 104D0C00 which is STAC9225s. Because of this, some SZ Notebooks are
2039          * currently not fully supported.
2040          */
2041         { .id = 0x83847661, .name = "CXD9872RD/K", .patch = patch_stac9872 },
2042         { .id = 0x83847662, .name = "STAC9872AK", .patch = patch_stac9872 },
2043         { .id = 0x83847664, .name = "CXD9872AKD", .patch = patch_stac9872 },
2044         { .id = 0x838476a0, .name = "STAC9205", .patch = patch_stac9205 },
2045         { .id = 0x838476a1, .name = "STAC9205D", .patch = patch_stac9205 },
2046         { .id = 0x838476a2, .name = "STAC9204", .patch = patch_stac9205 },
2047         { .id = 0x838476a3, .name = "STAC9204D", .patch = patch_stac9205 },
2048         { .id = 0x838476a4, .name = "STAC9255", .patch = patch_stac9205 },
2049         { .id = 0x838476a5, .name = "STAC9255D", .patch = patch_stac9205 },
2050         { .id = 0x838476a6, .name = "STAC9254", .patch = patch_stac9205 },
2051         { .id = 0x838476a7, .name = "STAC9254D", .patch = patch_stac9205 },
2052         {} /* terminator */
2053 };