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