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