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