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