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