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