[ALSA] hda-codec - Fix speaker output on MacPro
[safe/jmp/linux-2.6] / sound / pci / hda / patch_sigmatel.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * HD audio interface patch for SigmaTel STAC92xx
5  *
6  * Copyright (c) 2005 Embedded Alley Solutions, Inc.
7  * Matt Porter <mporter@embeddedalley.com>
8  *
9  * Based on patch_cmedia.c and patch_realtek.c
10  * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
11  *
12  *  This driver is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This driver is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25  */
26
27 #include <sound/driver.h>
28 #include <linux/init.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/pci.h>
32 #include <sound/core.h>
33 #include <sound/asoundef.h>
34 #include "hda_codec.h"
35 #include "hda_local.h"
36
37 #define NUM_CONTROL_ALLOC       32
38 #define STAC_HP_EVENT           0x37
39
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                 /* force to enable the first line-out; the others are set up
1604                  * in unsol_event
1605                  */
1606                 stac92xx_auto_set_pinctl(codec, spec->autocfg.line_out_pins[0],
1607                                          AC_PINCTL_OUT_EN);
1608                 stac92xx_auto_init_hp_out(codec);
1609                 /* fake event to set up pins */
1610                 codec->patch_ops.unsol_event(codec, STAC_HP_EVENT << 26);
1611         } else {
1612                 stac92xx_auto_init_multi_out(codec);
1613                 stac92xx_auto_init_hp_out(codec);
1614         }
1615         for (i = 0; i < AUTO_PIN_LAST; i++) {
1616                 hda_nid_t nid = cfg->input_pins[i];
1617                 if (nid) {
1618                         unsigned int pinctl = AC_PINCTL_IN_EN;
1619                         if (i == AUTO_PIN_MIC || i == AUTO_PIN_FRONT_MIC)
1620                                 pinctl |= stac92xx_get_vref(codec, nid);
1621                         stac92xx_auto_set_pinctl(codec, nid, pinctl);
1622                 }
1623         }
1624         if (spec->num_dmics > 0)
1625                 for (i = 0; i < spec->num_dmics; i++)
1626                         stac92xx_auto_set_pinctl(codec, spec->dmic_nids[i],
1627                                                  AC_PINCTL_IN_EN);
1628
1629         if (cfg->dig_out_pin)
1630                 stac92xx_auto_set_pinctl(codec, cfg->dig_out_pin,
1631                                          AC_PINCTL_OUT_EN);
1632         if (cfg->dig_in_pin)
1633                 stac92xx_auto_set_pinctl(codec, cfg->dig_in_pin,
1634                                          AC_PINCTL_IN_EN);
1635
1636         if (spec->gpio_mute) {
1637                 stac922x_gpio_mute(codec, 0, 0);
1638                 stac922x_gpio_mute(codec, 1, 0);
1639         }
1640
1641         return 0;
1642 }
1643
1644 static void stac92xx_free(struct hda_codec *codec)
1645 {
1646         struct sigmatel_spec *spec = codec->spec;
1647         int i;
1648
1649         if (! spec)
1650                 return;
1651
1652         if (spec->kctl_alloc) {
1653                 for (i = 0; i < spec->num_kctl_used; i++)
1654                         kfree(spec->kctl_alloc[i].name);
1655                 kfree(spec->kctl_alloc);
1656         }
1657
1658         if (spec->bios_pin_configs)
1659                 kfree(spec->bios_pin_configs);
1660
1661         kfree(spec);
1662 }
1663
1664 static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
1665                                 unsigned int flag)
1666 {
1667         unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
1668                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
1669         if (flag == AC_PINCTL_OUT_EN && (pin_ctl & AC_PINCTL_IN_EN))
1670                 return;
1671         snd_hda_codec_write(codec, nid, 0,
1672                         AC_VERB_SET_PIN_WIDGET_CONTROL,
1673                         pin_ctl | flag);
1674 }
1675
1676 static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
1677                                   unsigned int flag)
1678 {
1679         unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
1680                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
1681         snd_hda_codec_write(codec, nid, 0,
1682                         AC_VERB_SET_PIN_WIDGET_CONTROL,
1683                         pin_ctl & ~flag);
1684 }
1685
1686 static int get_pin_presence(struct hda_codec *codec, hda_nid_t nid)
1687 {
1688         if (!nid)
1689                 return 0;
1690         if (snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_PIN_SENSE, 0x00)
1691             & (1 << 31))
1692                 return 1;
1693         return 0;
1694 }
1695
1696 static void stac92xx_hp_detect(struct hda_codec *codec, unsigned int res)
1697 {
1698         struct sigmatel_spec *spec = codec->spec;
1699         struct auto_pin_cfg *cfg = &spec->autocfg;
1700         int i, presence;
1701
1702         presence = 0;
1703         for (i = 0; i < cfg->hp_outs; i++) {
1704                 presence = get_pin_presence(codec, cfg->hp_pins[i]);
1705                 if (presence)
1706                         break;
1707         }
1708
1709         if (presence) {
1710                 /* disable lineouts, enable hp */
1711                 for (i = 0; i < cfg->line_outs; i++)
1712                         stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
1713                                                 AC_PINCTL_OUT_EN);
1714                 for (i = 0; i < cfg->speaker_outs; i++)
1715                         stac92xx_reset_pinctl(codec, cfg->speaker_pins[i],
1716                                                 AC_PINCTL_OUT_EN);
1717         } else {
1718                 /* enable lineouts, disable hp */
1719                 for (i = 0; i < cfg->line_outs; i++)
1720                         stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
1721                                                 AC_PINCTL_OUT_EN);
1722                 for (i = 0; i < cfg->speaker_outs; i++)
1723                         stac92xx_set_pinctl(codec, cfg->speaker_pins[i],
1724                                                 AC_PINCTL_OUT_EN);
1725         }
1726
1727
1728 static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
1729 {
1730         switch (res >> 26) {
1731         case STAC_HP_EVENT:
1732                 stac92xx_hp_detect(codec, res);
1733                 break;
1734         }
1735 }
1736
1737 #ifdef CONFIG_PM
1738 static int stac92xx_resume(struct hda_codec *codec)
1739 {
1740         struct sigmatel_spec *spec = codec->spec;
1741         int i;
1742
1743         stac92xx_init(codec);
1744         stac92xx_set_config_regs(codec);
1745         for (i = 0; i < spec->num_mixers; i++)
1746                 snd_hda_resume_ctls(codec, spec->mixers[i]);
1747         if (spec->multiout.dig_out_nid)
1748                 snd_hda_resume_spdif_out(codec);
1749         if (spec->dig_in_nid)
1750                 snd_hda_resume_spdif_in(codec);
1751
1752         return 0;
1753 }
1754 #endif
1755
1756 static struct hda_codec_ops stac92xx_patch_ops = {
1757         .build_controls = stac92xx_build_controls,
1758         .build_pcms = stac92xx_build_pcms,
1759         .init = stac92xx_init,
1760         .free = stac92xx_free,
1761         .unsol_event = stac92xx_unsol_event,
1762 #ifdef CONFIG_PM
1763         .resume = stac92xx_resume,
1764 #endif
1765 };
1766
1767 static int patch_stac9200(struct hda_codec *codec)
1768 {
1769         struct sigmatel_spec *spec;
1770         int err;
1771
1772         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1773         if (spec == NULL)
1774                 return -ENOMEM;
1775
1776         codec->spec = spec;
1777         spec->num_pins = 8;
1778         spec->pin_nids = stac9200_pin_nids;
1779         spec->board_config = snd_hda_check_board_config(codec, STAC_9200_MODELS,
1780                                                         stac9200_models,
1781                                                         stac9200_cfg_tbl);
1782         if (spec->board_config < 0) {
1783                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9200, using BIOS defaults\n");
1784                 err = stac92xx_save_bios_config_regs(codec);
1785                 if (err < 0) {
1786                         stac92xx_free(codec);
1787                         return err;
1788                 }
1789                 spec->pin_configs = spec->bios_pin_configs;
1790         } else {
1791                 spec->pin_configs = stac9200_brd_tbl[spec->board_config];
1792                 stac92xx_set_config_regs(codec);
1793         }
1794
1795         spec->multiout.max_channels = 2;
1796         spec->multiout.num_dacs = 1;
1797         spec->multiout.dac_nids = stac9200_dac_nids;
1798         spec->adc_nids = stac9200_adc_nids;
1799         spec->mux_nids = stac9200_mux_nids;
1800         spec->num_muxes = 1;
1801         spec->num_dmics = 0;
1802
1803         spec->init = stac9200_core_init;
1804         spec->mixer = stac9200_mixer;
1805
1806         err = stac9200_parse_auto_config(codec);
1807         if (err < 0) {
1808                 stac92xx_free(codec);
1809                 return err;
1810         }
1811
1812         codec->patch_ops = stac92xx_patch_ops;
1813
1814         return 0;
1815 }
1816
1817 static int patch_stac925x(struct hda_codec *codec)
1818 {
1819         struct sigmatel_spec *spec;
1820         int err;
1821
1822         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1823         if (spec == NULL)
1824                 return -ENOMEM;
1825
1826         codec->spec = spec;
1827         spec->num_pins = 8;
1828         spec->pin_nids = stac925x_pin_nids;
1829         spec->board_config = snd_hda_check_board_config(codec, STAC_925x_MODELS,
1830                                                         stac925x_models,
1831                                                         stac925x_cfg_tbl);
1832  again:
1833         if (spec->board_config < 0) {
1834                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC925x, using BIOS defaults\n");
1835                 err = stac92xx_save_bios_config_regs(codec);
1836                 if (err < 0) {
1837                         stac92xx_free(codec);
1838                         return err;
1839                 }
1840                 spec->pin_configs = spec->bios_pin_configs;
1841         } else if (stac925x_brd_tbl[spec->board_config] != NULL){
1842                 spec->pin_configs = stac925x_brd_tbl[spec->board_config];
1843                 stac92xx_set_config_regs(codec);
1844         }
1845
1846         spec->multiout.max_channels = 2;
1847         spec->multiout.num_dacs = 1;
1848         spec->multiout.dac_nids = stac925x_dac_nids;
1849         spec->adc_nids = stac925x_adc_nids;
1850         spec->mux_nids = stac925x_mux_nids;
1851         spec->num_muxes = 1;
1852         spec->num_dmics = 0;
1853
1854         spec->init = stac925x_core_init;
1855         spec->mixer = stac925x_mixer;
1856
1857         err = stac92xx_parse_auto_config(codec, 0x8, 0x7);
1858         if (!err) {
1859                 if (spec->board_config < 0) {
1860                         printk(KERN_WARNING "hda_codec: No auto-config is "
1861                                "available, default to model=ref\n");
1862                         spec->board_config = STAC_925x_REF;
1863                         goto again;
1864                 }
1865                 err = -EINVAL;
1866         }
1867         if (err < 0) {
1868                 stac92xx_free(codec);
1869                 return err;
1870         }
1871
1872         codec->patch_ops = stac92xx_patch_ops;
1873
1874         return 0;
1875 }
1876
1877 static int patch_stac922x(struct hda_codec *codec)
1878 {
1879         struct sigmatel_spec *spec;
1880         int err;
1881
1882         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1883         if (spec == NULL)
1884                 return -ENOMEM;
1885
1886         codec->spec = spec;
1887         spec->num_pins = 10;
1888         spec->pin_nids = stac922x_pin_nids;
1889         spec->board_config = snd_hda_check_board_config(codec, STAC_922X_MODELS,
1890                                                         stac922x_models,
1891                                                         stac922x_cfg_tbl);
1892         if (spec->board_config == STAC_MACMINI) {
1893                 spec->gpio_mute = 1;
1894                 /* Intel Macs have all same PCI SSID, so we need to check
1895                  * codec SSID to distinguish the exact models
1896                  */
1897                 switch (codec->subsystem_id) {
1898                 case 0x106b1e00:
1899                         spec->board_config = STAC_MACBOOK_PRO;
1900                         break;
1901                 }
1902         }
1903
1904  again:
1905         if (spec->board_config < 0) {
1906                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC922x, "
1907                         "using BIOS defaults\n");
1908                 err = stac92xx_save_bios_config_regs(codec);
1909                 if (err < 0) {
1910                         stac92xx_free(codec);
1911                         return err;
1912                 }
1913                 spec->pin_configs = spec->bios_pin_configs;
1914         } else if (stac922x_brd_tbl[spec->board_config] != NULL) {
1915                 spec->pin_configs = stac922x_brd_tbl[spec->board_config];
1916                 stac92xx_set_config_regs(codec);
1917         }
1918
1919         spec->adc_nids = stac922x_adc_nids;
1920         spec->mux_nids = stac922x_mux_nids;
1921         spec->num_muxes = 2;
1922         spec->num_dmics = 0;
1923
1924         spec->init = stac922x_core_init;
1925         spec->mixer = stac922x_mixer;
1926
1927         spec->multiout.dac_nids = spec->dac_nids;
1928         
1929         err = stac92xx_parse_auto_config(codec, 0x08, 0x09);
1930         if (!err) {
1931                 if (spec->board_config < 0) {
1932                         printk(KERN_WARNING "hda_codec: No auto-config is "
1933                                "available, default to model=ref\n");
1934                         spec->board_config = STAC_D945_REF;
1935                         goto again;
1936                 }
1937                 err = -EINVAL;
1938         }
1939         if (err < 0) {
1940                 stac92xx_free(codec);
1941                 return err;
1942         }
1943
1944         codec->patch_ops = stac92xx_patch_ops;
1945
1946         return 0;
1947 }
1948
1949 static int patch_stac927x(struct hda_codec *codec)
1950 {
1951         struct sigmatel_spec *spec;
1952         int err;
1953
1954         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1955         if (spec == NULL)
1956                 return -ENOMEM;
1957
1958         codec->spec = spec;
1959         spec->num_pins = 14;
1960         spec->pin_nids = stac927x_pin_nids;
1961         spec->board_config = snd_hda_check_board_config(codec, STAC_927X_MODELS,
1962                                                         stac927x_models,
1963                                                         stac927x_cfg_tbl);
1964  again:
1965         if (spec->board_config < 0) {
1966                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC927x, using BIOS defaults\n");
1967                 err = stac92xx_save_bios_config_regs(codec);
1968                 if (err < 0) {
1969                         stac92xx_free(codec);
1970                         return err;
1971                 }
1972                 spec->pin_configs = spec->bios_pin_configs;
1973         } else if (stac927x_brd_tbl[spec->board_config] != NULL) {
1974                 spec->pin_configs = stac927x_brd_tbl[spec->board_config];
1975                 stac92xx_set_config_regs(codec);
1976         }
1977
1978         switch (spec->board_config) {
1979         case STAC_D965_3ST:
1980                 spec->adc_nids = stac927x_adc_nids;
1981                 spec->mux_nids = stac927x_mux_nids;
1982                 spec->num_muxes = 3;
1983                 spec->num_dmics = 0;
1984                 spec->init = d965_core_init;
1985                 spec->mixer = stac9227_mixer;
1986                 break;
1987         case STAC_D965_5ST:
1988                 spec->adc_nids = stac927x_adc_nids;
1989                 spec->mux_nids = stac927x_mux_nids;
1990                 spec->num_muxes = 3;
1991                 spec->num_dmics = 0;
1992                 spec->init = d965_core_init;
1993                 spec->mixer = stac9227_mixer;
1994                 break;
1995         default:
1996                 spec->adc_nids = stac927x_adc_nids;
1997                 spec->mux_nids = stac927x_mux_nids;
1998                 spec->num_muxes = 3;
1999                 spec->num_dmics = 0;
2000                 spec->init = stac927x_core_init;
2001                 spec->mixer = stac927x_mixer;
2002         }
2003
2004         spec->multiout.dac_nids = spec->dac_nids;
2005
2006         err = stac92xx_parse_auto_config(codec, 0x1e, 0x20);
2007         if (!err) {
2008                 if (spec->board_config < 0) {
2009                         printk(KERN_WARNING "hda_codec: No auto-config is "
2010                                "available, default to model=ref\n");
2011                         spec->board_config = STAC_D965_REF;
2012                         goto again;
2013                 }
2014                 err = -EINVAL;
2015         }
2016         if (err < 0) {
2017                 stac92xx_free(codec);
2018                 return err;
2019         }
2020
2021         codec->patch_ops = stac92xx_patch_ops;
2022
2023         return 0;
2024 }
2025
2026 static int patch_stac9205(struct hda_codec *codec)
2027 {
2028         struct sigmatel_spec *spec;
2029         int err;
2030
2031         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2032         if (spec == NULL)
2033                 return -ENOMEM;
2034
2035         codec->spec = spec;
2036         spec->num_pins = 14;
2037         spec->pin_nids = stac9205_pin_nids;
2038         spec->board_config = snd_hda_check_board_config(codec, STAC_9205_MODELS,
2039                                                         stac9205_models,
2040                                                         stac9205_cfg_tbl);
2041  again:
2042         if (spec->board_config < 0) {
2043                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9205, using BIOS defaults\n");
2044                 err = stac92xx_save_bios_config_regs(codec);
2045                 if (err < 0) {
2046                         stac92xx_free(codec);
2047                         return err;
2048                 }
2049                 spec->pin_configs = spec->bios_pin_configs;
2050         } else {
2051                 spec->pin_configs = stac9205_brd_tbl[spec->board_config];
2052                 stac92xx_set_config_regs(codec);
2053         }
2054
2055         spec->adc_nids = stac9205_adc_nids;
2056         spec->mux_nids = stac9205_mux_nids;
2057         spec->num_muxes = 2;
2058         spec->dmic_nids = stac9205_dmic_nids;
2059         spec->num_dmics = 2;
2060         spec->dmux_nid = 0x1d;
2061
2062         spec->init = stac9205_core_init;
2063         spec->mixer = stac9205_mixer;
2064
2065         spec->multiout.dac_nids = spec->dac_nids;
2066
2067         /* Configure GPIO0 as EAPD output */
2068         snd_hda_codec_write(codec, codec->afg, 0,
2069                             AC_VERB_SET_GPIO_DIRECTION, 0x00000001);
2070         /* Configure GPIO0 as CMOS */
2071         snd_hda_codec_write(codec, codec->afg, 0, 0x7e7, 0x00000000);
2072         /* Assert GPIO0 high */
2073         snd_hda_codec_write(codec, codec->afg, 0,
2074                             AC_VERB_SET_GPIO_DATA, 0x00000001);
2075         /* Enable GPIO0 */
2076         snd_hda_codec_write(codec, codec->afg, 0,
2077                             AC_VERB_SET_GPIO_MASK, 0x00000001);
2078
2079         err = stac92xx_parse_auto_config(codec, 0x1f, 0x20);
2080         if (!err) {
2081                 if (spec->board_config < 0) {
2082                         printk(KERN_WARNING "hda_codec: No auto-config is "
2083                                "available, default to model=ref\n");
2084                         spec->board_config = STAC_9205_REF;
2085                         goto again;
2086                 }
2087                 err = -EINVAL;
2088         }
2089         if (err < 0) {
2090                 stac92xx_free(codec);
2091                 return err;
2092         }
2093
2094         codec->patch_ops = stac92xx_patch_ops;
2095
2096         return 0;
2097 }
2098
2099 /*
2100  * STAC9872 hack
2101  */
2102
2103 /* static config for Sony VAIO FE550G and Sony VAIO AR */
2104 static hda_nid_t vaio_dacs[] = { 0x2 };
2105 #define VAIO_HP_DAC     0x5
2106 static hda_nid_t vaio_adcs[] = { 0x8 /*,0x6*/ };
2107 static hda_nid_t vaio_mux_nids[] = { 0x15 };
2108
2109 static struct hda_input_mux vaio_mux = {
2110         .num_items = 2,
2111         .items = {
2112                 /* { "HP", 0x0 }, */
2113                 { "Line", 0x1 },
2114                 { "Mic", 0x2 },
2115                 { "PCM", 0x3 },
2116         }
2117 };
2118
2119 static struct hda_verb vaio_init[] = {
2120         {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
2121         {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
2122         {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
2123         {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
2124         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
2125         {0x15, AC_VERB_SET_CONNECT_SEL, 0x2}, /* mic-sel: 0a,0d,14,02 */
2126         {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
2127         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
2128         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
2129         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
2130         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
2131         {}
2132 };
2133
2134 static struct hda_verb vaio_ar_init[] = {
2135         {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
2136         {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
2137         {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
2138         {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
2139 /*      {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },*/ /* Optical Out */
2140         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
2141         {0x15, AC_VERB_SET_CONNECT_SEL, 0x2}, /* mic-sel: 0a,0d,14,02 */
2142         {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
2143         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
2144 /*      {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},*/ /* Optical Out */
2145         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
2146         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
2147         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
2148         {}
2149 };
2150
2151 /* bind volumes of both NID 0x02 and 0x05 */
2152 static int vaio_master_vol_put(struct snd_kcontrol *kcontrol,
2153                                struct snd_ctl_elem_value *ucontrol)
2154 {
2155         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2156         long *valp = ucontrol->value.integer.value;
2157         int change;
2158
2159         change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
2160                                           0x7f, valp[0] & 0x7f);
2161         change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
2162                                            0x7f, valp[1] & 0x7f);
2163         snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
2164                                  0x7f, valp[0] & 0x7f);
2165         snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
2166                                  0x7f, valp[1] & 0x7f);
2167         return change;
2168 }
2169
2170 /* bind volumes of both NID 0x02 and 0x05 */
2171 static int vaio_master_sw_put(struct snd_kcontrol *kcontrol,
2172                               struct snd_ctl_elem_value *ucontrol)
2173 {
2174         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2175         long *valp = ucontrol->value.integer.value;
2176         int change;
2177
2178         change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
2179                                           0x80, (valp[0] ? 0 : 0x80));
2180         change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
2181                                            0x80, (valp[1] ? 0 : 0x80));
2182         snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
2183                                  0x80, (valp[0] ? 0 : 0x80));
2184         snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
2185                                  0x80, (valp[1] ? 0 : 0x80));
2186         return change;
2187 }
2188
2189 static struct snd_kcontrol_new vaio_mixer[] = {
2190         {
2191                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2192                 .name = "Master Playback Volume",
2193                 .info = snd_hda_mixer_amp_volume_info,
2194                 .get = snd_hda_mixer_amp_volume_get,
2195                 .put = vaio_master_vol_put,
2196                 .tlv = { .c = snd_hda_mixer_amp_tlv },
2197                 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
2198         },
2199         {
2200                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2201                 .name = "Master Playback Switch",
2202                 .info = snd_hda_mixer_amp_switch_info,
2203                 .get = snd_hda_mixer_amp_switch_get,
2204                 .put = vaio_master_sw_put,
2205                 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
2206         },
2207         /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
2208         HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
2209         HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
2210         {
2211                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2212                 .name = "Capture Source",
2213                 .count = 1,
2214                 .info = stac92xx_mux_enum_info,
2215                 .get = stac92xx_mux_enum_get,
2216                 .put = stac92xx_mux_enum_put,
2217         },
2218         {}
2219 };
2220
2221 static struct snd_kcontrol_new vaio_ar_mixer[] = {
2222         {
2223                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2224                 .name = "Master Playback Volume",
2225                 .info = snd_hda_mixer_amp_volume_info,
2226                 .get = snd_hda_mixer_amp_volume_get,
2227                 .put = vaio_master_vol_put,
2228                 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
2229         },
2230         {
2231                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2232                 .name = "Master Playback Switch",
2233                 .info = snd_hda_mixer_amp_switch_info,
2234                 .get = snd_hda_mixer_amp_switch_get,
2235                 .put = vaio_master_sw_put,
2236                 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
2237         },
2238         /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
2239         HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
2240         HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
2241         /*HDA_CODEC_MUTE("Optical Out Switch", 0x10, 0, HDA_OUTPUT),
2242         HDA_CODEC_VOLUME("Optical Out Volume", 0x10, 0, HDA_OUTPUT),*/
2243         {
2244                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2245                 .name = "Capture Source",
2246                 .count = 1,
2247                 .info = stac92xx_mux_enum_info,
2248                 .get = stac92xx_mux_enum_get,
2249                 .put = stac92xx_mux_enum_put,
2250         },
2251         {}
2252 };
2253
2254 static struct hda_codec_ops stac9872_patch_ops = {
2255         .build_controls = stac92xx_build_controls,
2256         .build_pcms = stac92xx_build_pcms,
2257         .init = stac92xx_init,
2258         .free = stac92xx_free,
2259 #ifdef CONFIG_PM
2260         .resume = stac92xx_resume,
2261 #endif
2262 };
2263
2264 enum { /* FE and SZ series. id=0x83847661 and subsys=0x104D0700 or 104D1000. */
2265        CXD9872RD_VAIO,
2266        /* Unknown. id=0x83847662 and subsys=0x104D1200 or 104D1000. */
2267        STAC9872AK_VAIO, 
2268        /* Unknown. id=0x83847661 and subsys=0x104D1200. */
2269        STAC9872K_VAIO,
2270        /* AR Series. id=0x83847664 and subsys=104D1300 */
2271        CXD9872AKD_VAIO,
2272        STAC_9872_MODELS,
2273 };
2274
2275 static const char *stac9872_models[STAC_9872_MODELS] = {
2276         [CXD9872RD_VAIO]        = "vaio",
2277         [CXD9872AKD_VAIO]       = "vaio-ar",
2278 };
2279
2280 static struct snd_pci_quirk stac9872_cfg_tbl[] = {
2281         SND_PCI_QUIRK(0x104d, 0x81e6, "Sony VAIO F/S", CXD9872RD_VAIO),
2282         SND_PCI_QUIRK(0x104d, 0x81ef, "Sony VAIO F/S", CXD9872RD_VAIO),
2283         SND_PCI_QUIRK(0x104d, 0x81fd, "Sony VAIO AR", CXD9872AKD_VAIO),
2284         {}
2285 };
2286
2287 static int patch_stac9872(struct hda_codec *codec)
2288 {
2289         struct sigmatel_spec *spec;
2290         int board_config;
2291
2292         board_config = snd_hda_check_board_config(codec, STAC_9872_MODELS,
2293                                                   stac9872_models,
2294                                                   stac9872_cfg_tbl);
2295         if (board_config < 0)
2296                 /* unknown config, let generic-parser do its job... */
2297                 return snd_hda_parse_generic_codec(codec);
2298         
2299         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2300         if (spec == NULL)
2301                 return -ENOMEM;
2302
2303         codec->spec = spec;
2304         switch (board_config) {
2305         case CXD9872RD_VAIO:
2306         case STAC9872AK_VAIO:
2307         case STAC9872K_VAIO:
2308                 spec->mixer = vaio_mixer;
2309                 spec->init = vaio_init;
2310                 spec->multiout.max_channels = 2;
2311                 spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
2312                 spec->multiout.dac_nids = vaio_dacs;
2313                 spec->multiout.hp_nid = VAIO_HP_DAC;
2314                 spec->num_adcs = ARRAY_SIZE(vaio_adcs);
2315                 spec->adc_nids = vaio_adcs;
2316                 spec->input_mux = &vaio_mux;
2317                 spec->mux_nids = vaio_mux_nids;
2318                 break;
2319         
2320         case CXD9872AKD_VAIO:
2321                 spec->mixer = vaio_ar_mixer;
2322                 spec->init = vaio_ar_init;
2323                 spec->multiout.max_channels = 2;
2324                 spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
2325                 spec->multiout.dac_nids = vaio_dacs;
2326                 spec->multiout.hp_nid = VAIO_HP_DAC;
2327                 spec->num_adcs = ARRAY_SIZE(vaio_adcs);
2328                 spec->adc_nids = vaio_adcs;
2329                 spec->input_mux = &vaio_mux;
2330                 spec->mux_nids = vaio_mux_nids;
2331                 break;
2332         }
2333
2334         codec->patch_ops = stac9872_patch_ops;
2335         return 0;
2336 }
2337
2338
2339 /*
2340  * patch entries
2341  */
2342 struct hda_codec_preset snd_hda_preset_sigmatel[] = {
2343         { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
2344         { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
2345         { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
2346         { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
2347         { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
2348         { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
2349         { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
2350         { .id = 0x83847618, .name = "STAC9227", .patch = patch_stac927x },
2351         { .id = 0x83847619, .name = "STAC9227", .patch = patch_stac927x },
2352         { .id = 0x83847616, .name = "STAC9228", .patch = patch_stac927x },
2353         { .id = 0x83847617, .name = "STAC9228", .patch = patch_stac927x },
2354         { .id = 0x83847614, .name = "STAC9229", .patch = patch_stac927x },
2355         { .id = 0x83847615, .name = "STAC9229", .patch = patch_stac927x },
2356         { .id = 0x83847620, .name = "STAC9274", .patch = patch_stac927x },
2357         { .id = 0x83847621, .name = "STAC9274D", .patch = patch_stac927x },
2358         { .id = 0x83847622, .name = "STAC9273X", .patch = patch_stac927x },
2359         { .id = 0x83847623, .name = "STAC9273D", .patch = patch_stac927x },
2360         { .id = 0x83847624, .name = "STAC9272X", .patch = patch_stac927x },
2361         { .id = 0x83847625, .name = "STAC9272D", .patch = patch_stac927x },
2362         { .id = 0x83847626, .name = "STAC9271X", .patch = patch_stac927x },
2363         { .id = 0x83847627, .name = "STAC9271D", .patch = patch_stac927x },
2364         { .id = 0x83847628, .name = "STAC9274X5NH", .patch = patch_stac927x },
2365         { .id = 0x83847629, .name = "STAC9274D5NH", .patch = patch_stac927x },
2366         { .id = 0x83847632, .name = "STAC9202",  .patch = patch_stac925x },
2367         { .id = 0x83847633, .name = "STAC9202D", .patch = patch_stac925x },
2368         { .id = 0x83847634, .name = "STAC9250", .patch = patch_stac925x },
2369         { .id = 0x83847635, .name = "STAC9250D", .patch = patch_stac925x },
2370         { .id = 0x83847636, .name = "STAC9251", .patch = patch_stac925x },
2371         { .id = 0x83847637, .name = "STAC9250D", .patch = patch_stac925x },
2372         /* The following does not take into account .id=0x83847661 when subsys =
2373          * 104D0C00 which is STAC9225s. Because of this, some SZ Notebooks are
2374          * currently not fully supported.
2375          */
2376         { .id = 0x83847661, .name = "CXD9872RD/K", .patch = patch_stac9872 },
2377         { .id = 0x83847662, .name = "STAC9872AK", .patch = patch_stac9872 },
2378         { .id = 0x83847664, .name = "CXD9872AKD", .patch = patch_stac9872 },
2379         { .id = 0x838476a0, .name = "STAC9205", .patch = patch_stac9205 },
2380         { .id = 0x838476a1, .name = "STAC9205D", .patch = patch_stac9205 },
2381         { .id = 0x838476a2, .name = "STAC9204", .patch = patch_stac9205 },
2382         { .id = 0x838476a3, .name = "STAC9204D", .patch = patch_stac9205 },
2383         { .id = 0x838476a4, .name = "STAC9255", .patch = patch_stac9205 },
2384         { .id = 0x838476a5, .name = "STAC9255D", .patch = patch_stac9205 },
2385         { .id = 0x838476a6, .name = "STAC9254", .patch = patch_stac9205 },
2386         { .id = 0x838476a7, .name = "STAC9254D", .patch = patch_stac9205 },
2387         {} /* terminator */
2388 };