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