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