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