[ALSA] hda-codec - Add ALC889/ALC267/ALC269 support
[powerpc.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_DELL_D21,
43         STAC_9200_DELL_D22,
44         STAC_9200_DELL_D23,
45         STAC_9200_DELL_M21,
46         STAC_9200_DELL_M22,
47         STAC_9200_DELL_M23,
48         STAC_9200_DELL_M24,
49         STAC_9200_DELL_M25,
50         STAC_9200_DELL_M26,
51         STAC_9200_DELL_M27,
52         STAC_9200_GATEWAY,
53         STAC_9200_MODELS
54 };
55
56 enum {
57         STAC_9205_REF,
58         STAC_9205_DELL_M42,
59         STAC_9205_DELL_M43,
60         STAC_9205_DELL_M44,
61         STAC_9205_MODELS
62 };
63
64 enum {
65         STAC_92HD71BXX_REF,
66         STAC_92HD71BXX_MODELS
67 };
68
69 enum {
70         STAC_925x_REF,
71         STAC_M2_2,
72         STAC_MA6,
73         STAC_PA6,
74         STAC_925x_MODELS
75 };
76
77 enum {
78         STAC_D945_REF,
79         STAC_D945GTP3,
80         STAC_D945GTP5,
81         STAC_INTEL_MAC_V1,
82         STAC_INTEL_MAC_V2,
83         STAC_INTEL_MAC_V3,
84         STAC_INTEL_MAC_V4,
85         STAC_INTEL_MAC_V5,
86         /* for backward compatibility */
87         STAC_MACMINI,
88         STAC_MACBOOK,
89         STAC_MACBOOK_PRO_V1,
90         STAC_MACBOOK_PRO_V2,
91         STAC_IMAC_INTEL,
92         STAC_IMAC_INTEL_20,
93         STAC_922X_DELL_D81,
94         STAC_922X_DELL_D82,
95         STAC_922X_DELL_M81,
96         STAC_922X_DELL_M82,
97         STAC_922X_MODELS
98 };
99
100 enum {
101         STAC_D965_REF,
102         STAC_D965_3ST,
103         STAC_D965_5ST,
104         STAC_DELL_3ST,
105         STAC_927X_MODELS
106 };
107
108 struct sigmatel_spec {
109         struct snd_kcontrol_new *mixers[4];
110         unsigned int num_mixers;
111
112         int board_config;
113         unsigned int surr_switch: 1;
114         unsigned int line_switch: 1;
115         unsigned int mic_switch: 1;
116         unsigned int alt_switch: 1;
117         unsigned int hp_detect: 1;
118         unsigned int gpio_mute: 1;
119
120         unsigned int gpio_mask, gpio_data;
121
122         /* playback */
123         struct hda_multi_out multiout;
124         hda_nid_t dac_nids[5];
125
126         /* capture */
127         hda_nid_t *adc_nids;
128         unsigned int num_adcs;
129         hda_nid_t *mux_nids;
130         unsigned int num_muxes;
131         hda_nid_t *dmic_nids;
132         unsigned int num_dmics;
133         hda_nid_t dmux_nid;
134         hda_nid_t dig_in_nid;
135
136         /* pin widgets */
137         hda_nid_t *pin_nids;
138         unsigned int num_pins;
139         unsigned int *pin_configs;
140         unsigned int *bios_pin_configs;
141
142         /* codec specific stuff */
143         struct hda_verb *init;
144         struct snd_kcontrol_new *mixer;
145
146         /* capture source */
147         struct hda_input_mux *dinput_mux;
148         unsigned int cur_dmux;
149         struct hda_input_mux *input_mux;
150         unsigned int cur_mux[3];
151
152         /* i/o switches */
153         unsigned int io_switch[2];
154         unsigned int clfe_swap;
155         unsigned int aloopback;
156
157         struct hda_pcm pcm_rec[2];      /* PCM information */
158
159         /* dynamic controls and input_mux */
160         struct auto_pin_cfg autocfg;
161         unsigned int num_kctl_alloc, num_kctl_used;
162         struct snd_kcontrol_new *kctl_alloc;
163         struct hda_input_mux private_dimux;
164         struct hda_input_mux private_imux;
165 };
166
167 static hda_nid_t stac9200_adc_nids[1] = {
168         0x03,
169 };
170
171 static hda_nid_t stac9200_mux_nids[1] = {
172         0x0c,
173 };
174
175 static hda_nid_t stac9200_dac_nids[1] = {
176         0x02,
177 };
178
179 static hda_nid_t stac92hd71bxx_adc_nids[2] = {
180         0x12, 0x13,
181 };
182
183 static hda_nid_t stac92hd71bxx_mux_nids[2] = {
184         0x1a, 0x1b
185 };
186
187 static hda_nid_t stac92hd71bxx_dac_nids[2] = {
188         0x10, /*0x11, */
189 };
190
191 #define STAC92HD71BXX_NUM_DMICS 2
192 static hda_nid_t stac92hd71bxx_dmic_nids[STAC92HD71BXX_NUM_DMICS + 1] = {
193         0x18, 0x19, 0
194 };
195
196 static hda_nid_t stac925x_adc_nids[1] = {
197         0x03,
198 };
199
200 static hda_nid_t stac925x_mux_nids[1] = {
201         0x0f,
202 };
203
204 static hda_nid_t stac925x_dac_nids[1] = {
205         0x02,
206 };
207
208 #define STAC925X_NUM_DMICS      1
209 static hda_nid_t stac925x_dmic_nids[STAC925X_NUM_DMICS + 1] = {
210         0x15, 0
211 };
212
213 static hda_nid_t stac922x_adc_nids[2] = {
214         0x06, 0x07,
215 };
216
217 static hda_nid_t stac922x_mux_nids[2] = {
218         0x12, 0x13,
219 };
220
221 static hda_nid_t stac927x_adc_nids[3] = {
222         0x07, 0x08, 0x09
223 };
224
225 static hda_nid_t stac927x_mux_nids[3] = {
226         0x15, 0x16, 0x17
227 };
228
229 #define STAC927X_NUM_DMICS 2
230 static hda_nid_t stac927x_dmic_nids[STAC927X_NUM_DMICS + 1] = {
231         0x13, 0x14, 0
232 };
233
234 static hda_nid_t stac9205_adc_nids[2] = {
235         0x12, 0x13
236 };
237
238 static hda_nid_t stac9205_mux_nids[2] = {
239         0x19, 0x1a
240 };
241
242 #define STAC9205_NUM_DMICS      2
243 static hda_nid_t stac9205_dmic_nids[STAC9205_NUM_DMICS + 1] = {
244         0x17, 0x18, 0
245 };
246
247 static hda_nid_t stac9200_pin_nids[8] = {
248         0x08, 0x09, 0x0d, 0x0e, 
249         0x0f, 0x10, 0x11, 0x12,
250 };
251
252 static hda_nid_t stac925x_pin_nids[8] = {
253         0x07, 0x08, 0x0a, 0x0b, 
254         0x0c, 0x0d, 0x10, 0x11,
255 };
256
257 static hda_nid_t stac922x_pin_nids[10] = {
258         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
259         0x0f, 0x10, 0x11, 0x15, 0x1b,
260 };
261
262 static hda_nid_t stac92hd71bxx_pin_nids[10] = {
263         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
264         0x0f, 0x14, 0x18, 0x19, 0x1e,
265 };
266
267 static hda_nid_t stac927x_pin_nids[14] = {
268         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
269         0x0f, 0x10, 0x11, 0x12, 0x13,
270         0x14, 0x21, 0x22, 0x23,
271 };
272
273 static hda_nid_t stac9205_pin_nids[12] = {
274         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
275         0x0f, 0x14, 0x16, 0x17, 0x18,
276         0x21, 0x22,
277 };
278
279 static int stac92xx_dmux_enum_info(struct snd_kcontrol *kcontrol,
280                                    struct snd_ctl_elem_info *uinfo)
281 {
282         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
283         struct sigmatel_spec *spec = codec->spec;
284         return snd_hda_input_mux_info(spec->dinput_mux, uinfo);
285 }
286
287 static int stac92xx_dmux_enum_get(struct snd_kcontrol *kcontrol,
288                                   struct snd_ctl_elem_value *ucontrol)
289 {
290         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
291         struct sigmatel_spec *spec = codec->spec;
292
293         ucontrol->value.enumerated.item[0] = spec->cur_dmux;
294         return 0;
295 }
296
297 static int stac92xx_dmux_enum_put(struct snd_kcontrol *kcontrol,
298                                   struct snd_ctl_elem_value *ucontrol)
299 {
300         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
301         struct sigmatel_spec *spec = codec->spec;
302
303         return snd_hda_input_mux_put(codec, spec->dinput_mux, ucontrol,
304                                      spec->dmux_nid, &spec->cur_dmux);
305 }
306
307 static int stac92xx_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
308 {
309         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
310         struct sigmatel_spec *spec = codec->spec;
311         return snd_hda_input_mux_info(spec->input_mux, uinfo);
312 }
313
314 static int stac92xx_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
315 {
316         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
317         struct sigmatel_spec *spec = codec->spec;
318         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
319
320         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
321         return 0;
322 }
323
324 static int stac92xx_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
325 {
326         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
327         struct sigmatel_spec *spec = codec->spec;
328         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
329
330         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
331                                      spec->mux_nids[adc_idx], &spec->cur_mux[adc_idx]);
332 }
333
334 #define stac92xx_aloopback_info snd_ctl_boolean_mono_info
335
336 static int stac92xx_aloopback_get(struct snd_kcontrol *kcontrol,
337         struct snd_ctl_elem_value *ucontrol)
338 {
339         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
340         struct sigmatel_spec *spec = codec->spec;
341
342         ucontrol->value.integer.value[0] = spec->aloopback;
343         return 0;
344 }
345
346 static int stac92xx_aloopback_put(struct snd_kcontrol *kcontrol,
347                 struct snd_ctl_elem_value *ucontrol)
348 {
349         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
350         struct sigmatel_spec *spec = codec->spec;
351         unsigned int dac_mode;
352         unsigned int val;
353
354         val = !!ucontrol->value.integer.value[0];
355         if (spec->aloopback == val)
356                 return 0;
357
358         spec->aloopback = val;
359
360         dac_mode = snd_hda_codec_read(codec, codec->afg, 0,
361                 kcontrol->private_value & 0xFFFF, 0x0);
362
363         if (spec->aloopback) {
364                 snd_hda_power_up(codec);
365                 dac_mode |= 0x40;
366         } else {
367                 snd_hda_power_down(codec);
368                 dac_mode &= ~0x40;
369         }
370
371         snd_hda_codec_write_cache(codec, codec->afg, 0,
372                 kcontrol->private_value >> 16, dac_mode);
373
374         return 1;
375 }
376
377 static struct hda_verb stac9200_core_init[] = {
378         /* set dac0mux for dac converter */
379         { 0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
380         {}
381 };
382
383 static struct hda_verb stac9200_eapd_init[] = {
384         /* set dac0mux for dac converter */
385         {0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
386         {0x08, AC_VERB_SET_EAPD_BTLENABLE, 0x02},
387         {}
388 };
389
390 static struct hda_verb stac92hd71bxx_core_init[] = {
391         /* set master volume and direct control */
392         { 0x28, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
393         /* connect headphone jack to dac1 */
394         { 0x0a, AC_VERB_SET_CONNECT_SEL, 0x01},
395         /* connect ports 0d and 0f to audio mixer */
396         { 0x0d, AC_VERB_SET_CONNECT_SEL, 0x2},
397         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x2},
398         /* unmute dac0 input in audio mixer */
399         { 0x17, AC_VERB_SET_AMP_GAIN_MUTE, 0x701f},
400         /* unmute right and left channels for nodes 0x0a, 0xd, 0x0f */
401         { 0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
402         { 0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
403         { 0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
404         /* unmute mono out node */
405         { 0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
406         {}
407 };
408
409 static struct hda_verb stac925x_core_init[] = {
410         /* set dac0mux for dac converter */
411         { 0x06, AC_VERB_SET_CONNECT_SEL, 0x00},
412         {}
413 };
414
415 static struct hda_verb stac922x_core_init[] = {
416         /* set master volume and direct control */      
417         { 0x16, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
418         {}
419 };
420
421 static struct hda_verb d965_core_init[] = {
422         /* set master volume and direct control */      
423         { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
424         /* unmute node 0x1b */
425         { 0x1b, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
426         /* select node 0x03 as DAC */   
427         { 0x0b, AC_VERB_SET_CONNECT_SEL, 0x01},
428         {}
429 };
430
431 static struct hda_verb stac927x_core_init[] = {
432         /* set master volume and direct control */      
433         { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
434         {}
435 };
436
437 static struct hda_verb stac9205_core_init[] = {
438         /* set master volume and direct control */      
439         { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
440         {}
441 };
442
443 #define STAC_DIGITAL_INPUT_SOURCE(cnt) \
444         { \
445                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
446                 .name = "Digital Input Source", \
447                 .count = cnt, \
448                 .info = stac92xx_dmux_enum_info, \
449                 .get = stac92xx_dmux_enum_get, \
450                 .put = stac92xx_dmux_enum_put,\
451         }
452
453 #define STAC_INPUT_SOURCE(cnt) \
454         { \
455                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
456                 .name = "Input Source", \
457                 .count = cnt, \
458                 .info = stac92xx_mux_enum_info, \
459                 .get = stac92xx_mux_enum_get, \
460                 .put = stac92xx_mux_enum_put, \
461         }
462
463 #define STAC_ANALOG_LOOPBACK(verb_read,verb_write) \
464         { \
465                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
466                 .name  = "Analog Loopback", \
467                 .count = 1, \
468                 .info  = stac92xx_aloopback_info, \
469                 .get   = stac92xx_aloopback_get, \
470                 .put   = stac92xx_aloopback_put, \
471                 .private_value = verb_read | (verb_write << 16), \
472         }
473
474 static struct snd_kcontrol_new stac9200_mixer[] = {
475         HDA_CODEC_VOLUME("Master Playback Volume", 0xb, 0, HDA_OUTPUT),
476         HDA_CODEC_MUTE("Master Playback Switch", 0xb, 0, HDA_OUTPUT),
477         STAC_INPUT_SOURCE(1),
478         HDA_CODEC_VOLUME("Capture Volume", 0x0a, 0, HDA_OUTPUT),
479         HDA_CODEC_MUTE("Capture Switch", 0x0a, 0, HDA_OUTPUT),
480         HDA_CODEC_VOLUME("Capture Mux Volume", 0x0c, 0, HDA_OUTPUT),
481         { } /* end */
482 };
483
484 static struct snd_kcontrol_new stac92hd71bxx_mixer[] = {
485         STAC_DIGITAL_INPUT_SOURCE(1),
486         STAC_INPUT_SOURCE(2),
487
488         /* hardware gain controls */
489         HDA_CODEC_VOLUME_IDX("Digital Mic Volume", 0x0, 0x18, 0x0, HDA_OUTPUT),
490         HDA_CODEC_VOLUME_IDX("Digital Mic Volume", 0x1, 0x19, 0x0, HDA_OUTPUT),
491
492         HDA_CODEC_VOLUME_IDX("Capture Volume", 0x0, 0x1c, 0x0, HDA_OUTPUT),
493         HDA_CODEC_MUTE_IDX("Capture Switch", 0x0, 0x1c, 0x0, HDA_OUTPUT),
494         HDA_CODEC_VOLUME_IDX("Capture Mux Volume", 0x0, 0x1a, 0x0, HDA_OUTPUT),
495
496         HDA_CODEC_VOLUME_IDX("Capture Volume", 0x1, 0x1d, 0x0, HDA_OUTPUT),
497         HDA_CODEC_MUTE_IDX("Capture Switch", 0x1, 0x1d, 0x0, HDA_OUTPUT),
498         HDA_CODEC_VOLUME_IDX("Capture Mux Volume", 0x1, 0x1b, 0x0, HDA_OUTPUT),
499
500         HDA_CODEC_MUTE("Analog Loopback 1", 0x17, 0x3, HDA_INPUT),
501         HDA_CODEC_MUTE("Analog Loopback 2", 0x17, 0x4, HDA_INPUT),
502         { } /* end */
503 };
504
505 static struct snd_kcontrol_new stac925x_mixer[] = {
506         STAC_INPUT_SOURCE(1),
507         HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_OUTPUT),
508         HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_OUTPUT),
509         HDA_CODEC_VOLUME("Capture Mux Volume", 0x0f, 0, HDA_OUTPUT),
510         { } /* end */
511 };
512
513 static struct snd_kcontrol_new stac9205_mixer[] = {
514         STAC_DIGITAL_INPUT_SOURCE(1),
515         STAC_INPUT_SOURCE(2),
516         STAC_ANALOG_LOOPBACK(0xFE0, 0x7E0),
517
518         HDA_CODEC_VOLUME_IDX("Capture Volume", 0x0, 0x1b, 0x0, HDA_INPUT),
519         HDA_CODEC_MUTE_IDX("Capture Switch", 0x0, 0x1d, 0x0, HDA_OUTPUT),
520         HDA_CODEC_VOLUME_IDX("Mux Capture Volume", 0x0, 0x19, 0x0, HDA_OUTPUT),
521
522         HDA_CODEC_VOLUME_IDX("Capture Volume", 0x1, 0x1c, 0x0, HDA_INPUT),
523         HDA_CODEC_MUTE_IDX("Capture Switch", 0x1, 0x1e, 0x0, HDA_OUTPUT),
524         HDA_CODEC_VOLUME_IDX("Mux Capture Volume", 0x1, 0x1A, 0x0, HDA_OUTPUT),
525
526         { } /* end */
527 };
528
529 /* This needs to be generated dynamically based on sequence */
530 static struct snd_kcontrol_new stac922x_mixer[] = {
531         STAC_INPUT_SOURCE(2),
532         HDA_CODEC_VOLUME_IDX("Capture Volume", 0x0, 0x17, 0x0, HDA_INPUT),
533         HDA_CODEC_MUTE_IDX("Capture Switch", 0x0, 0x17, 0x0, HDA_INPUT),
534         HDA_CODEC_VOLUME_IDX("Mux Capture Volume", 0x0, 0x12, 0x0, HDA_OUTPUT),
535
536         HDA_CODEC_VOLUME_IDX("Capture Volume", 0x1, 0x18, 0x0, HDA_INPUT),
537         HDA_CODEC_MUTE_IDX("Capture Switch", 0x1, 0x18, 0x0, HDA_INPUT),
538         HDA_CODEC_VOLUME_IDX("Mux Capture Volume", 0x1, 0x13, 0x0, HDA_OUTPUT),
539         { } /* end */
540 };
541
542
543 static struct snd_kcontrol_new stac927x_mixer[] = {
544         STAC_DIGITAL_INPUT_SOURCE(1),
545         STAC_INPUT_SOURCE(3),
546         STAC_ANALOG_LOOPBACK(0xFEB, 0x7EB),
547
548         HDA_CODEC_VOLUME_IDX("Capture Volume", 0x0, 0x18, 0x0, HDA_INPUT),
549         HDA_CODEC_MUTE_IDX("Capture Switch", 0x0, 0x1b, 0x0, HDA_OUTPUT),
550         HDA_CODEC_VOLUME_IDX("Mux Capture Volume", 0x0, 0x15, 0x0, HDA_OUTPUT),
551
552         HDA_CODEC_VOLUME_IDX("Capture Volume", 0x1, 0x19, 0x0, HDA_INPUT),
553         HDA_CODEC_MUTE_IDX("Capture Switch", 0x1, 0x1c, 0x0, HDA_OUTPUT),
554         HDA_CODEC_VOLUME_IDX("Mux Capture Volume", 0x1, 0x16, 0x0, HDA_OUTPUT),
555
556         HDA_CODEC_VOLUME_IDX("Capture Volume", 0x2, 0x1A, 0x0, HDA_INPUT),
557         HDA_CODEC_MUTE_IDX("Capture Switch", 0x2, 0x1d, 0x0, HDA_OUTPUT),
558         HDA_CODEC_VOLUME_IDX("Mux Capture Volume", 0x2, 0x17, 0x0, HDA_OUTPUT),
559         { } /* end */
560 };
561
562 static int stac92xx_build_controls(struct hda_codec *codec)
563 {
564         struct sigmatel_spec *spec = codec->spec;
565         int err;
566         int i;
567
568         err = snd_hda_add_new_ctls(codec, spec->mixer);
569         if (err < 0)
570                 return err;
571
572         for (i = 0; i < spec->num_mixers; i++) {
573                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
574                 if (err < 0)
575                         return err;
576         }
577
578         if (spec->multiout.dig_out_nid) {
579                 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
580                 if (err < 0)
581                         return err;
582         }
583         if (spec->dig_in_nid) {
584                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
585                 if (err < 0)
586                         return err;
587         }
588         return 0;       
589 }
590
591 static unsigned int ref9200_pin_configs[8] = {
592         0x01c47010, 0x01447010, 0x0221401f, 0x01114010,
593         0x02a19020, 0x01a19021, 0x90100140, 0x01813122,
594 };
595
596 /* 
597     STAC 9200 pin configs for
598     102801A8
599     102801DE
600     102801E8
601 */
602 static unsigned int dell9200_d21_pin_configs[8] = {
603         0x400001f0, 0x400001f1, 0x02214030, 0x01014010, 
604         0x02a19020, 0x01a19021, 0x90100140, 0x01813122,
605 };
606
607 /* 
608     STAC 9200 pin configs for
609     102801C0
610     102801C1
611 */
612 static unsigned int dell9200_d22_pin_configs[8] = {
613         0x400001f0, 0x400001f1, 0x0221401f, 0x01014010, 
614         0x01813020, 0x02a19021, 0x90100140, 0x400001f2,
615 };
616
617 /* 
618     STAC 9200 pin configs for
619     102801C4 (Dell Dimension E310)
620     102801C5
621     102801C7
622     102801D9
623     102801DA
624     102801E3
625 */
626 static unsigned int dell9200_d23_pin_configs[8] = {
627         0x400001f0, 0x400001f1, 0x0221401f, 0x01014010, 
628         0x01813020, 0x01a19021, 0x90100140, 0x400001f2, 
629 };
630
631
632 /* 
633     STAC 9200-32 pin configs for
634     102801B5 (Dell Inspiron 630m)
635     102801D8 (Dell Inspiron 640m)
636 */
637 static unsigned int dell9200_m21_pin_configs[8] = {
638         0x40c003fa, 0x03441340, 0x0321121f, 0x90170310,
639         0x408003fb, 0x03a11020, 0x401003fc, 0x403003fd,
640 };
641
642 /* 
643     STAC 9200-32 pin configs for
644     102801C2 (Dell Latitude D620)
645     102801C8 
646     102801CC (Dell Latitude D820)
647     102801D4 
648     102801D6 
649 */
650 static unsigned int dell9200_m22_pin_configs[8] = {
651         0x40c003fa, 0x0144131f, 0x0321121f, 0x90170310, 
652         0x90a70321, 0x03a11020, 0x401003fb, 0x40f000fc,
653 };
654
655 /* 
656     STAC 9200-32 pin configs for
657     102801CE (Dell XPS M1710)
658     102801CF (Dell Precision M90)
659 */
660 static unsigned int dell9200_m23_pin_configs[8] = {
661         0x40c003fa, 0x01441340, 0x0421421f, 0x90170310,
662         0x408003fb, 0x04a1102e, 0x90170311, 0x403003fc,
663 };
664
665 /*
666     STAC 9200-32 pin configs for 
667     102801C9
668     102801CA
669     102801CB (Dell Latitude 120L)
670     102801D3
671 */
672 static unsigned int dell9200_m24_pin_configs[8] = {
673         0x40c003fa, 0x404003fb, 0x0321121f, 0x90170310, 
674         0x408003fc, 0x03a11020, 0x401003fd, 0x403003fe, 
675 };
676
677 /*
678     STAC 9200-32 pin configs for
679     102801BD (Dell Inspiron E1505n)
680     102801EE
681     102801EF
682 */
683 static unsigned int dell9200_m25_pin_configs[8] = {
684         0x40c003fa, 0x01441340, 0x0421121f, 0x90170310, 
685         0x408003fb, 0x04a11020, 0x401003fc, 0x403003fd,
686 };
687
688 /*
689     STAC 9200-32 pin configs for
690     102801F5 (Dell Inspiron 1501)
691     102801F6
692 */
693 static unsigned int dell9200_m26_pin_configs[8] = {
694         0x40c003fa, 0x404003fb, 0x0421121f, 0x90170310, 
695         0x408003fc, 0x04a11020, 0x401003fd, 0x403003fe,
696 };
697
698 /*
699     STAC 9200-32
700     102801CD (Dell Inspiron E1705/9400)
701 */
702 static unsigned int dell9200_m27_pin_configs[8] = {
703         0x40c003fa, 0x01441340, 0x0421121f, 0x90170310,
704         0x90170310, 0x04a11020, 0x90170310, 0x40f003fc,
705 };
706
707
708 static unsigned int *stac9200_brd_tbl[STAC_9200_MODELS] = {
709         [STAC_REF] = ref9200_pin_configs,
710         [STAC_9200_DELL_D21] = dell9200_d21_pin_configs,
711         [STAC_9200_DELL_D22] = dell9200_d22_pin_configs,
712         [STAC_9200_DELL_D23] = dell9200_d23_pin_configs,
713         [STAC_9200_DELL_M21] = dell9200_m21_pin_configs,
714         [STAC_9200_DELL_M22] = dell9200_m22_pin_configs,
715         [STAC_9200_DELL_M23] = dell9200_m23_pin_configs,
716         [STAC_9200_DELL_M24] = dell9200_m24_pin_configs,
717         [STAC_9200_DELL_M25] = dell9200_m25_pin_configs,
718         [STAC_9200_DELL_M26] = dell9200_m26_pin_configs,
719         [STAC_9200_DELL_M27] = dell9200_m27_pin_configs,
720 };
721
722 static const char *stac9200_models[STAC_9200_MODELS] = {
723         [STAC_REF] = "ref",
724         [STAC_9200_DELL_D21] = "dell-d21",
725         [STAC_9200_DELL_D22] = "dell-d22",
726         [STAC_9200_DELL_D23] = "dell-d23",
727         [STAC_9200_DELL_M21] = "dell-m21",
728         [STAC_9200_DELL_M22] = "dell-m22",
729         [STAC_9200_DELL_M23] = "dell-m23",
730         [STAC_9200_DELL_M24] = "dell-m24",
731         [STAC_9200_DELL_M25] = "dell-m25",
732         [STAC_9200_DELL_M26] = "dell-m26",
733         [STAC_9200_DELL_M27] = "dell-m27",
734         [STAC_9200_GATEWAY] = "gateway",
735 };
736
737 static struct snd_pci_quirk stac9200_cfg_tbl[] = {
738         /* SigmaTel reference board */
739         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
740                       "DFI LanParty", STAC_REF),
741         /* Dell laptops have BIOS problem */
742         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01a8,
743                       "unknown Dell", STAC_9200_DELL_D21),
744         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01b5,
745                       "Dell Inspiron 630m", STAC_9200_DELL_M21),
746         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01bd,
747                       "Dell Inspiron E1505n", STAC_9200_DELL_M25),
748         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c0,
749                       "unknown Dell", STAC_9200_DELL_D22),
750         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c1,
751                       "unknown Dell", STAC_9200_DELL_D22),
752         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c2,
753                       "Dell Latitude D620", STAC_9200_DELL_M22),
754         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c5,
755                       "unknown Dell", STAC_9200_DELL_D23),
756         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c7,
757                       "unknown Dell", STAC_9200_DELL_D23),
758         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c8,
759                       "unknown Dell", STAC_9200_DELL_M22),
760         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01c9,
761                       "unknown Dell", STAC_9200_DELL_M24),
762         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ca,
763                       "unknown Dell", STAC_9200_DELL_M24),
764         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cb,
765                       "Dell Latitude 120L", STAC_9200_DELL_M24),
766         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cc,
767                       "Dell Latitude D820", STAC_9200_DELL_M22),
768         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cd,
769                       "Dell Inspiron E1705/9400", STAC_9200_DELL_M27),
770         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ce,
771                       "Dell XPS M1710", STAC_9200_DELL_M23),
772         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01cf,
773                       "Dell Precision M90", STAC_9200_DELL_M23),
774         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d3,
775                       "unknown Dell", STAC_9200_DELL_M22),
776         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d4,
777                       "unknown Dell", STAC_9200_DELL_M22),
778         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d6,
779                       "unknown Dell", STAC_9200_DELL_M22),
780         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d8,
781                       "Dell Inspiron 640m", STAC_9200_DELL_M21),
782         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d9,
783                       "unknown Dell", STAC_9200_DELL_D23),
784         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01da,
785                       "unknown Dell", STAC_9200_DELL_D23),
786         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01de,
787                       "unknown Dell", STAC_9200_DELL_D21),
788         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01e3,
789                       "unknown Dell", STAC_9200_DELL_D23),
790         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01e8,
791                       "unknown Dell", STAC_9200_DELL_D21),
792         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ee,
793                       "unknown Dell", STAC_9200_DELL_M25),
794         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ef,
795                       "unknown Dell", STAC_9200_DELL_M25),
796         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f5,
797                       "Dell Inspiron 1501", STAC_9200_DELL_M26),
798         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f6,
799                       "unknown Dell", STAC_9200_DELL_M26),
800         /* Panasonic */
801         SND_PCI_QUIRK(0x10f7, 0x8338, "Panasonic CF-74", STAC_REF),
802         /* Gateway machines needs EAPD to be set on resume */
803         SND_PCI_QUIRK(0x107b, 0x0205, "Gateway S-7110M", STAC_9200_GATEWAY),
804         SND_PCI_QUIRK(0x107b, 0x0317, "Gateway MT3423, MX341*",
805                       STAC_9200_GATEWAY),
806         SND_PCI_QUIRK(0x107b, 0x0318, "Gateway ML3019, MT3707",
807                       STAC_9200_GATEWAY),
808         {} /* terminator */
809 };
810
811 static unsigned int ref925x_pin_configs[8] = {
812         0x40c003f0, 0x424503f2, 0x01813022, 0x02a19021,
813         0x90a70320, 0x02214210, 0x400003f1, 0x9033032e,
814 };
815
816 static unsigned int stac925x_MA6_pin_configs[8] = {
817         0x40c003f0, 0x424503f2, 0x01813022, 0x02a19021,
818         0x90a70320, 0x90100211, 0x400003f1, 0x9033032e,
819 };
820
821 static unsigned int stac925x_PA6_pin_configs[8] = {
822         0x40c003f0, 0x424503f2, 0x01813022, 0x02a19021,
823         0x50a103f0, 0x90100211, 0x400003f1, 0x9033032e,
824 };
825
826 static unsigned int stac925xM2_2_pin_configs[8] = {
827         0x40c003f3, 0x424503f2, 0x04180011, 0x02a19020,
828         0x50a103f0, 0x90100212, 0x400003f1, 0x9033032e,
829 };
830
831 static unsigned int *stac925x_brd_tbl[STAC_925x_MODELS] = {
832         [STAC_REF] = ref925x_pin_configs,
833         [STAC_M2_2] = stac925xM2_2_pin_configs,
834         [STAC_MA6] = stac925x_MA6_pin_configs,
835         [STAC_PA6] = stac925x_PA6_pin_configs,
836 };
837
838 static const char *stac925x_models[STAC_925x_MODELS] = {
839         [STAC_REF] = "ref",
840         [STAC_M2_2] = "m2-2",
841         [STAC_MA6] = "m6",
842         [STAC_PA6] = "pa6",
843 };
844
845 static struct snd_pci_quirk stac925x_cfg_tbl[] = {
846         /* SigmaTel reference board */
847         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668, "DFI LanParty", STAC_REF),
848         SND_PCI_QUIRK(0x8384, 0x7632, "Stac9202 Reference Board", STAC_REF),
849         SND_PCI_QUIRK(0x107b, 0x0316, "Gateway M255", STAC_REF),
850         SND_PCI_QUIRK(0x107b, 0x0366, "Gateway MP6954", STAC_REF),
851         SND_PCI_QUIRK(0x107b, 0x0461, "Gateway NX560XL", STAC_MA6),
852         SND_PCI_QUIRK(0x107b, 0x0681, "Gateway NX860", STAC_PA6),
853         SND_PCI_QUIRK(0x1002, 0x437b, "Gateway MX6453", STAC_M2_2),
854         {} /* terminator */
855 };
856
857 static unsigned int ref92hd71bxx_pin_configs[10] = {
858         0x02214030, 0x02a19040, 0x01a19020, 0x01014010,
859         0x0181302e, 0x01114010, 0x01a19020, 0x90a000f0,
860         0x90a000f0, 0x01452050,
861 };
862
863 static unsigned int *stac92hd71bxx_brd_tbl[STAC_92HD71BXX_MODELS] = {
864         [STAC_92HD71BXX_REF] = ref92hd71bxx_pin_configs,
865 };
866
867 static const char *stac92hd71bxx_models[STAC_92HD71BXX_MODELS] = {
868         [STAC_92HD71BXX_REF] = "ref",
869 };
870
871 static struct snd_pci_quirk stac92hd71bxx_cfg_tbl[] = {
872         /* SigmaTel reference board */
873         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
874                       "DFI LanParty", STAC_92HD71BXX_REF),
875         {} /* terminator */
876 };
877
878 static unsigned int ref922x_pin_configs[10] = {
879         0x01014010, 0x01016011, 0x01012012, 0x0221401f,
880         0x01813122, 0x01011014, 0x01441030, 0x01c41030,
881         0x40000100, 0x40000100,
882 };
883
884 /*
885     STAC 922X pin configs for
886     102801A7
887     102801AB
888     102801A9
889     102801D1
890     102801D2
891 */
892 static unsigned int dell_922x_d81_pin_configs[10] = {
893         0x02214030, 0x01a19021, 0x01111012, 0x01114010,
894         0x02a19020, 0x01117011, 0x400001f0, 0x400001f1,
895         0x01813122, 0x400001f2,
896 };
897
898 /*
899     STAC 922X pin configs for
900     102801AC
901     102801D0
902 */
903 static unsigned int dell_922x_d82_pin_configs[10] = {
904         0x02214030, 0x01a19021, 0x01111012, 0x01114010,
905         0x02a19020, 0x01117011, 0x01451140, 0x400001f0,
906         0x01813122, 0x400001f1,
907 };
908
909 /*
910     STAC 922X pin configs for
911     102801BF
912 */
913 static unsigned int dell_922x_m81_pin_configs[10] = {
914         0x0321101f, 0x01112024, 0x01111222, 0x91174220,
915         0x03a11050, 0x01116221, 0x90a70330, 0x01452340, 
916         0x40C003f1, 0x405003f0,
917 };
918
919 /*
920     STAC 9221 A1 pin configs for
921     102801D7 (Dell XPS M1210)
922 */
923 static unsigned int dell_922x_m82_pin_configs[10] = {
924         0x02211211, 0x408103ff, 0x02a1123e, 0x90100310, 
925         0x408003f1, 0x0221121f, 0x03451340, 0x40c003f2, 
926         0x508003f3, 0x405003f4, 
927 };
928
929 static unsigned int d945gtp3_pin_configs[10] = {
930         0x0221401f, 0x01a19022, 0x01813021, 0x01014010,
931         0x40000100, 0x40000100, 0x40000100, 0x40000100,
932         0x02a19120, 0x40000100,
933 };
934
935 static unsigned int d945gtp5_pin_configs[10] = {
936         0x0221401f, 0x01011012, 0x01813024, 0x01014010,
937         0x01a19021, 0x01016011, 0x01452130, 0x40000100,
938         0x02a19320, 0x40000100,
939 };
940
941 static unsigned int intel_mac_v1_pin_configs[10] = {
942         0x0121e21f, 0x400000ff, 0x9017e110, 0x400000fd,
943         0x400000fe, 0x0181e020, 0x1145e030, 0x11c5e240,
944         0x400000fc, 0x400000fb,
945 };
946
947 static unsigned int intel_mac_v2_pin_configs[10] = {
948         0x0121e21f, 0x90a7012e, 0x9017e110, 0x400000fd,
949         0x400000fe, 0x0181e020, 0x1145e230, 0x500000fa,
950         0x400000fc, 0x400000fb,
951 };
952
953 static unsigned int intel_mac_v3_pin_configs[10] = {
954         0x0121e21f, 0x90a7012e, 0x9017e110, 0x400000fd,
955         0x400000fe, 0x0181e020, 0x1145e230, 0x11c5e240,
956         0x400000fc, 0x400000fb,
957 };
958
959 static unsigned int intel_mac_v4_pin_configs[10] = {
960         0x0321e21f, 0x03a1e02e, 0x9017e110, 0x9017e11f,
961         0x400000fe, 0x0381e020, 0x1345e230, 0x13c5e240,
962         0x400000fc, 0x400000fb,
963 };
964
965 static unsigned int intel_mac_v5_pin_configs[10] = {
966         0x0321e21f, 0x03a1e02e, 0x9017e110, 0x9017e11f,
967         0x400000fe, 0x0381e020, 0x1345e230, 0x13c5e240,
968         0x400000fc, 0x400000fb,
969 };
970
971
972 static unsigned int *stac922x_brd_tbl[STAC_922X_MODELS] = {
973         [STAC_D945_REF] = ref922x_pin_configs,
974         [STAC_D945GTP3] = d945gtp3_pin_configs,
975         [STAC_D945GTP5] = d945gtp5_pin_configs,
976         [STAC_INTEL_MAC_V1] = intel_mac_v1_pin_configs,
977         [STAC_INTEL_MAC_V2] = intel_mac_v2_pin_configs,
978         [STAC_INTEL_MAC_V3] = intel_mac_v3_pin_configs,
979         [STAC_INTEL_MAC_V4] = intel_mac_v4_pin_configs,
980         [STAC_INTEL_MAC_V5] = intel_mac_v5_pin_configs,
981         /* for backward compatibility */
982         [STAC_MACMINI] = intel_mac_v3_pin_configs,
983         [STAC_MACBOOK] = intel_mac_v5_pin_configs,
984         [STAC_MACBOOK_PRO_V1] = intel_mac_v3_pin_configs,
985         [STAC_MACBOOK_PRO_V2] = intel_mac_v3_pin_configs,
986         [STAC_IMAC_INTEL] = intel_mac_v2_pin_configs,
987         [STAC_IMAC_INTEL_20] = intel_mac_v3_pin_configs,
988         [STAC_922X_DELL_D81] = dell_922x_d81_pin_configs,
989         [STAC_922X_DELL_D82] = dell_922x_d82_pin_configs,       
990         [STAC_922X_DELL_M81] = dell_922x_m81_pin_configs,
991         [STAC_922X_DELL_M82] = dell_922x_m82_pin_configs,       
992 };
993
994 static const char *stac922x_models[STAC_922X_MODELS] = {
995         [STAC_D945_REF] = "ref",
996         [STAC_D945GTP5] = "5stack",
997         [STAC_D945GTP3] = "3stack",
998         [STAC_INTEL_MAC_V1] = "intel-mac-v1",
999         [STAC_INTEL_MAC_V2] = "intel-mac-v2",
1000         [STAC_INTEL_MAC_V3] = "intel-mac-v3",
1001         [STAC_INTEL_MAC_V4] = "intel-mac-v4",
1002         [STAC_INTEL_MAC_V5] = "intel-mac-v5",
1003         /* for backward compatibility */
1004         [STAC_MACMINI]  = "macmini",
1005         [STAC_MACBOOK]  = "macbook",
1006         [STAC_MACBOOK_PRO_V1]   = "macbook-pro-v1",
1007         [STAC_MACBOOK_PRO_V2]   = "macbook-pro",
1008         [STAC_IMAC_INTEL] = "imac-intel",
1009         [STAC_IMAC_INTEL_20] = "imac-intel-20",
1010         [STAC_922X_DELL_D81] = "dell-d81",
1011         [STAC_922X_DELL_D82] = "dell-d82",
1012         [STAC_922X_DELL_M81] = "dell-m81",
1013         [STAC_922X_DELL_M82] = "dell-m82",
1014 };
1015
1016 static struct snd_pci_quirk stac922x_cfg_tbl[] = {
1017         /* SigmaTel reference board */
1018         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
1019                       "DFI LanParty", STAC_D945_REF),
1020         /* Intel 945G based systems */
1021         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0101,
1022                       "Intel D945G", STAC_D945GTP3),
1023         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0202,
1024                       "Intel D945G", STAC_D945GTP3),
1025         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0606,
1026                       "Intel D945G", STAC_D945GTP3),
1027         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0601,
1028                       "Intel D945G", STAC_D945GTP3),
1029         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0111,
1030                       "Intel D945G", STAC_D945GTP3),
1031         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1115,
1032                       "Intel D945G", STAC_D945GTP3),
1033         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1116,
1034                       "Intel D945G", STAC_D945GTP3),
1035         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1117,
1036                       "Intel D945G", STAC_D945GTP3),
1037         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1118,
1038                       "Intel D945G", STAC_D945GTP3),
1039         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x1119,
1040                       "Intel D945G", STAC_D945GTP3),
1041         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x8826,
1042                       "Intel D945G", STAC_D945GTP3),
1043         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x5049,
1044                       "Intel D945G", STAC_D945GTP3),
1045         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x5055,
1046                       "Intel D945G", STAC_D945GTP3),
1047         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x5048,
1048                       "Intel D945G", STAC_D945GTP3),
1049         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0110,
1050                       "Intel D945G", STAC_D945GTP3),
1051         /* Intel D945G 5-stack systems */
1052         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0404,
1053                       "Intel D945G", STAC_D945GTP5),
1054         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0303,
1055                       "Intel D945G", STAC_D945GTP5),
1056         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0013,
1057                       "Intel D945G", STAC_D945GTP5),
1058         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0417,
1059                       "Intel D945G", STAC_D945GTP5),
1060         /* Intel 945P based systems */
1061         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0b0b,
1062                       "Intel D945P", STAC_D945GTP3),
1063         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0112,
1064                       "Intel D945P", STAC_D945GTP3),
1065         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0d0d,
1066                       "Intel D945P", STAC_D945GTP3),
1067         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0909,
1068                       "Intel D945P", STAC_D945GTP3),
1069         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0505,
1070                       "Intel D945P", STAC_D945GTP3),
1071         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x0707,
1072                       "Intel D945P", STAC_D945GTP5),
1073         /* other systems  */
1074         /* Apple Mac Mini (early 2006) */
1075         SND_PCI_QUIRK(0x8384, 0x7680,
1076                       "Mac Mini", STAC_INTEL_MAC_V3),
1077         /* Dell systems  */
1078         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01a7,
1079                       "unknown Dell", STAC_922X_DELL_D81),
1080         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01a9,
1081                       "unknown Dell", STAC_922X_DELL_D81),
1082         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ab,
1083                       "unknown Dell", STAC_922X_DELL_D81),
1084         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ac,
1085                       "unknown Dell", STAC_922X_DELL_D82),
1086         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01bf,
1087                       "unknown Dell", STAC_922X_DELL_M81),
1088         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d0,
1089                       "unknown Dell", STAC_922X_DELL_D82),
1090         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d1,
1091                       "unknown Dell", STAC_922X_DELL_D81),
1092         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d2,
1093                       "unknown Dell", STAC_922X_DELL_D81),
1094         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01d7,
1095                       "Dell XPS M1210", STAC_922X_DELL_M82),
1096         {} /* terminator */
1097 };
1098
1099 static unsigned int ref927x_pin_configs[14] = {
1100         0x02214020, 0x02a19080, 0x0181304e, 0x01014010,
1101         0x01a19040, 0x01011012, 0x01016011, 0x0101201f, 
1102         0x183301f0, 0x18a001f0, 0x18a001f0, 0x01442070,
1103         0x01c42190, 0x40000100,
1104 };
1105
1106 static unsigned int d965_3st_pin_configs[14] = {
1107         0x0221401f, 0x02a19120, 0x40000100, 0x01014011,
1108         0x01a19021, 0x01813024, 0x40000100, 0x40000100,
1109         0x40000100, 0x40000100, 0x40000100, 0x40000100,
1110         0x40000100, 0x40000100
1111 };
1112
1113 static unsigned int d965_5st_pin_configs[14] = {
1114         0x02214020, 0x02a19080, 0x0181304e, 0x01014010,
1115         0x01a19040, 0x01011012, 0x01016011, 0x40000100,
1116         0x40000100, 0x40000100, 0x40000100, 0x01442070,
1117         0x40000100, 0x40000100
1118 };
1119
1120 static unsigned int dell_3st_pin_configs[14] = {
1121         0x02211230, 0x02a11220, 0x01a19040, 0x01114210,
1122         0x01111212, 0x01116211, 0x01813050, 0x01112214,
1123         0x403003fa, 0x40000100, 0x40000100, 0x404003fb,
1124         0x40c003fc, 0x40000100
1125 };
1126
1127 static unsigned int *stac927x_brd_tbl[STAC_927X_MODELS] = {
1128         [STAC_D965_REF] = ref927x_pin_configs,
1129         [STAC_D965_3ST] = d965_3st_pin_configs,
1130         [STAC_D965_5ST] = d965_5st_pin_configs,
1131         [STAC_DELL_3ST] = dell_3st_pin_configs,
1132 };
1133
1134 static const char *stac927x_models[STAC_927X_MODELS] = {
1135         [STAC_D965_REF] = "ref",
1136         [STAC_D965_3ST] = "3stack",
1137         [STAC_D965_5ST] = "5stack",
1138         [STAC_DELL_3ST] = "dell-3stack",
1139 };
1140
1141 static struct snd_pci_quirk stac927x_cfg_tbl[] = {
1142         /* SigmaTel reference board */
1143         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
1144                       "DFI LanParty", STAC_D965_REF),
1145          /* Intel 946 based systems */
1146         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x3d01, "Intel D946", STAC_D965_3ST),
1147         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0xa301, "Intel D946", STAC_D965_3ST),
1148         /* 965 based 3 stack systems */
1149         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2116, "Intel D965", STAC_D965_3ST),
1150         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2115, "Intel D965", STAC_D965_3ST),
1151         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2114, "Intel D965", STAC_D965_3ST),
1152         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2113, "Intel D965", STAC_D965_3ST),
1153         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2112, "Intel D965", STAC_D965_3ST),
1154         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2111, "Intel D965", STAC_D965_3ST),
1155         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2110, "Intel D965", STAC_D965_3ST),
1156         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2009, "Intel D965", STAC_D965_3ST),
1157         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2008, "Intel D965", STAC_D965_3ST),
1158         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2007, "Intel D965", STAC_D965_3ST),
1159         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2006, "Intel D965", STAC_D965_3ST),
1160         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2005, "Intel D965", STAC_D965_3ST),
1161         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2004, "Intel D965", STAC_D965_3ST),
1162         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2003, "Intel D965", STAC_D965_3ST),
1163         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2002, "Intel D965", STAC_D965_3ST),
1164         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2001, "Intel D965", STAC_D965_3ST),
1165         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x01f3, "Dell Inspiron 1420", STAC_D965_3ST),
1166         /* Dell 3 stack systems */
1167         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x01dd, "Dell Dimension E520", STAC_DELL_3ST),
1168         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x01ed, "Dell     ", STAC_DELL_3ST),
1169         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x01f4, "Dell     ", STAC_DELL_3ST),
1170         /* 965 based 5 stack systems */
1171         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL,  0x0209, "Dell XPS 1330", STAC_D965_5ST),
1172         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2301, "Intel D965", STAC_D965_5ST),
1173         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2302, "Intel D965", STAC_D965_5ST),
1174         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2303, "Intel D965", STAC_D965_5ST),
1175         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2304, "Intel D965", STAC_D965_5ST),
1176         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2305, "Intel D965", STAC_D965_5ST),
1177         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2501, "Intel D965", STAC_D965_5ST),
1178         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2502, "Intel D965", STAC_D965_5ST),
1179         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2503, "Intel D965", STAC_D965_5ST),
1180         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2504, "Intel D965", STAC_D965_5ST),
1181         {} /* terminator */
1182 };
1183
1184 static unsigned int ref9205_pin_configs[12] = {
1185         0x40000100, 0x40000100, 0x01016011, 0x01014010,
1186         0x01813122, 0x01a19021, 0x40000100, 0x40000100,
1187         0x90a000f0, 0x90a000f0, 0x01441030, 0x01c41030
1188 };
1189
1190 /*
1191     STAC 9205 pin configs for
1192     102801F1
1193     102801F2
1194     102801FC
1195     102801FD
1196     10280204
1197     1028021F
1198 */
1199 static unsigned int dell_9205_m42_pin_configs[12] = {
1200         0x0321101F, 0x03A11020, 0x400003FA, 0x90170310,
1201         0x400003FB, 0x400003FC, 0x400003FD, 0x40F000F9,
1202         0x90A60330, 0x400003FF, 0x0144131F, 0x40C003FE,
1203 };
1204
1205 /*
1206     STAC 9205 pin configs for
1207     102801F9
1208     102801FA
1209     102801FE
1210     102801FF (Dell Precision M4300)
1211     10280206
1212     10280200
1213     10280201
1214 */
1215 static unsigned int dell_9205_m43_pin_configs[12] = {
1216         0x0321101f, 0x03a11020, 0x90a70330, 0x90170310,
1217         0x400000fe, 0x400000ff, 0x400000fd, 0x40f000f9,
1218         0x400000fa, 0x400000fc, 0x0144131f, 0x40c003f8,
1219 };
1220
1221 static unsigned int dell_9205_m44_pin_configs[12] = {
1222         0x0421101f, 0x04a11020, 0x400003fa, 0x90170310,
1223         0x400003fb, 0x400003fc, 0x400003fd, 0x400003f9,
1224         0x90a60330, 0x400003ff, 0x01441340, 0x40c003fe,
1225 };
1226
1227 static unsigned int *stac9205_brd_tbl[STAC_9205_MODELS] = {
1228         [STAC_9205_REF] = ref9205_pin_configs,
1229         [STAC_9205_DELL_M42] = dell_9205_m42_pin_configs,
1230         [STAC_9205_DELL_M43] = dell_9205_m43_pin_configs,
1231         [STAC_9205_DELL_M44] = dell_9205_m44_pin_configs,
1232 };
1233
1234 static const char *stac9205_models[STAC_9205_MODELS] = {
1235         [STAC_9205_REF] = "ref",
1236         [STAC_9205_DELL_M42] = "dell-m42",
1237         [STAC_9205_DELL_M43] = "dell-m43",
1238         [STAC_9205_DELL_M44] = "dell-m44",
1239 };
1240
1241 static struct snd_pci_quirk stac9205_cfg_tbl[] = {
1242         /* SigmaTel reference board */
1243         SND_PCI_QUIRK(PCI_VENDOR_ID_INTEL, 0x2668,
1244                       "DFI LanParty", STAC_9205_REF),
1245         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f1,
1246                       "unknown Dell", STAC_9205_DELL_M42),
1247         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f2,
1248                       "unknown Dell", STAC_9205_DELL_M42),
1249         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f8,
1250                       "Dell Precision", STAC_9205_DELL_M43),
1251         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x021c,
1252                           "Dell Precision", STAC_9205_DELL_M43),
1253         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f9,
1254                       "Dell Precision", STAC_9205_DELL_M43),
1255         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x021b,
1256                       "Dell Precision", STAC_9205_DELL_M43),
1257         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01fa,
1258                       "Dell Precision", STAC_9205_DELL_M43),
1259         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01fc,
1260                       "unknown Dell", STAC_9205_DELL_M42),
1261         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01fd,
1262                       "unknown Dell", STAC_9205_DELL_M42),
1263         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01fe,
1264                       "Dell Precision", STAC_9205_DELL_M43),
1265         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01ff,
1266                       "Dell Precision M4300", STAC_9205_DELL_M43),
1267         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0206,
1268                       "Dell Precision", STAC_9205_DELL_M43),
1269         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f1,
1270                       "Dell Inspiron", STAC_9205_DELL_M44),
1271         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01f2,
1272                       "Dell Inspiron", STAC_9205_DELL_M44),
1273         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01fc,
1274                       "Dell Inspiron", STAC_9205_DELL_M44),
1275         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x01fd,
1276                       "Dell Inspiron", STAC_9205_DELL_M44),
1277         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x0204,
1278                       "unknown Dell", STAC_9205_DELL_M42),
1279         SND_PCI_QUIRK(PCI_VENDOR_ID_DELL, 0x021f,
1280                       "Dell Inspiron", STAC_9205_DELL_M44),
1281         {} /* terminator */
1282 };
1283
1284 static int stac92xx_save_bios_config_regs(struct hda_codec *codec)
1285 {
1286         int i;
1287         struct sigmatel_spec *spec = codec->spec;
1288         
1289         if (! spec->bios_pin_configs) {
1290                 spec->bios_pin_configs = kcalloc(spec->num_pins,
1291                                                  sizeof(*spec->bios_pin_configs), GFP_KERNEL);
1292                 if (! spec->bios_pin_configs)
1293                         return -ENOMEM;
1294         }
1295         
1296         for (i = 0; i < spec->num_pins; i++) {
1297                 hda_nid_t nid = spec->pin_nids[i];
1298                 unsigned int pin_cfg;
1299                 
1300                 pin_cfg = snd_hda_codec_read(codec, nid, 0, 
1301                         AC_VERB_GET_CONFIG_DEFAULT, 0x00);      
1302                 snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x bios pin config %8.8x\n",
1303                                         nid, pin_cfg);
1304                 spec->bios_pin_configs[i] = pin_cfg;
1305         }
1306         
1307         return 0;
1308 }
1309
1310 static void stac92xx_set_config_reg(struct hda_codec *codec,
1311                                     hda_nid_t pin_nid, unsigned int pin_config)
1312 {
1313         int i;
1314         snd_hda_codec_write(codec, pin_nid, 0,
1315                             AC_VERB_SET_CONFIG_DEFAULT_BYTES_0,
1316                             pin_config & 0x000000ff);
1317         snd_hda_codec_write(codec, pin_nid, 0,
1318                             AC_VERB_SET_CONFIG_DEFAULT_BYTES_1,
1319                             (pin_config & 0x0000ff00) >> 8);
1320         snd_hda_codec_write(codec, pin_nid, 0,
1321                             AC_VERB_SET_CONFIG_DEFAULT_BYTES_2,
1322                             (pin_config & 0x00ff0000) >> 16);
1323         snd_hda_codec_write(codec, pin_nid, 0,
1324                             AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
1325                             pin_config >> 24);
1326         i = snd_hda_codec_read(codec, pin_nid, 0,
1327                                AC_VERB_GET_CONFIG_DEFAULT,
1328                                0x00);   
1329         snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x pin config %8.8x\n",
1330                     pin_nid, i);
1331 }
1332
1333 static void stac92xx_set_config_regs(struct hda_codec *codec)
1334 {
1335         int i;
1336         struct sigmatel_spec *spec = codec->spec;
1337
1338         if (!spec->pin_configs)
1339                 return;
1340
1341         for (i = 0; i < spec->num_pins; i++)
1342                 stac92xx_set_config_reg(codec, spec->pin_nids[i],
1343                                         spec->pin_configs[i]);
1344 }
1345
1346 static void stac92xx_enable_gpio_mask(struct hda_codec *codec)
1347 {
1348         struct sigmatel_spec *spec = codec->spec;
1349         /* Configure GPIOx as output */
1350         snd_hda_codec_write_cache(codec, codec->afg, 0,
1351                                   AC_VERB_SET_GPIO_DIRECTION, spec->gpio_mask);
1352         /* Configure GPIOx as CMOS */
1353         snd_hda_codec_write_cache(codec, codec->afg, 0, 0x7e7, 0x00000000);
1354         /* Assert GPIOx */
1355         snd_hda_codec_write_cache(codec, codec->afg, 0,
1356                                   AC_VERB_SET_GPIO_DATA, spec->gpio_data);
1357         /* Enable GPIOx */
1358         snd_hda_codec_write_cache(codec, codec->afg, 0,
1359                                   AC_VERB_SET_GPIO_MASK, spec->gpio_mask);
1360 }
1361
1362 /*
1363  * Analog playback callbacks
1364  */
1365 static int stac92xx_playback_pcm_open(struct hda_pcm_stream *hinfo,
1366                                       struct hda_codec *codec,
1367                                       struct snd_pcm_substream *substream)
1368 {
1369         struct sigmatel_spec *spec = codec->spec;
1370         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
1371 }
1372
1373 static int stac92xx_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1374                                          struct hda_codec *codec,
1375                                          unsigned int stream_tag,
1376                                          unsigned int format,
1377                                          struct snd_pcm_substream *substream)
1378 {
1379         struct sigmatel_spec *spec = codec->spec;
1380         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag, format, substream);
1381 }
1382
1383 static int stac92xx_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1384                                         struct hda_codec *codec,
1385                                         struct snd_pcm_substream *substream)
1386 {
1387         struct sigmatel_spec *spec = codec->spec;
1388         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
1389 }
1390
1391 /*
1392  * Digital playback callbacks
1393  */
1394 static int stac92xx_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1395                                           struct hda_codec *codec,
1396                                           struct snd_pcm_substream *substream)
1397 {
1398         struct sigmatel_spec *spec = codec->spec;
1399         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1400 }
1401
1402 static int stac92xx_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1403                                            struct hda_codec *codec,
1404                                            struct snd_pcm_substream *substream)
1405 {
1406         struct sigmatel_spec *spec = codec->spec;
1407         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1408 }
1409
1410 static int stac92xx_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1411                                          struct hda_codec *codec,
1412                                          unsigned int stream_tag,
1413                                          unsigned int format,
1414                                          struct snd_pcm_substream *substream)
1415 {
1416         struct sigmatel_spec *spec = codec->spec;
1417         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1418                                              stream_tag, format, substream);
1419 }
1420
1421
1422 /*
1423  * Analog capture callbacks
1424  */
1425 static int stac92xx_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1426                                         struct hda_codec *codec,
1427                                         unsigned int stream_tag,
1428                                         unsigned int format,
1429                                         struct snd_pcm_substream *substream)
1430 {
1431         struct sigmatel_spec *spec = codec->spec;
1432
1433         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1434                                    stream_tag, 0, format);
1435         return 0;
1436 }
1437
1438 static int stac92xx_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1439                                         struct hda_codec *codec,
1440                                         struct snd_pcm_substream *substream)
1441 {
1442         struct sigmatel_spec *spec = codec->spec;
1443
1444         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
1445         return 0;
1446 }
1447
1448 static struct hda_pcm_stream stac92xx_pcm_digital_playback = {
1449         .substreams = 1,
1450         .channels_min = 2,
1451         .channels_max = 2,
1452         /* NID is set in stac92xx_build_pcms */
1453         .ops = {
1454                 .open = stac92xx_dig_playback_pcm_open,
1455                 .close = stac92xx_dig_playback_pcm_close,
1456                 .prepare = stac92xx_dig_playback_pcm_prepare
1457         },
1458 };
1459
1460 static struct hda_pcm_stream stac92xx_pcm_digital_capture = {
1461         .substreams = 1,
1462         .channels_min = 2,
1463         .channels_max = 2,
1464         /* NID is set in stac92xx_build_pcms */
1465 };
1466
1467 static struct hda_pcm_stream stac92xx_pcm_analog_playback = {
1468         .substreams = 1,
1469         .channels_min = 2,
1470         .channels_max = 8,
1471         .nid = 0x02, /* NID to query formats and rates */
1472         .ops = {
1473                 .open = stac92xx_playback_pcm_open,
1474                 .prepare = stac92xx_playback_pcm_prepare,
1475                 .cleanup = stac92xx_playback_pcm_cleanup
1476         },
1477 };
1478
1479 static struct hda_pcm_stream stac92xx_pcm_analog_alt_playback = {
1480         .substreams = 1,
1481         .channels_min = 2,
1482         .channels_max = 2,
1483         .nid = 0x06, /* NID to query formats and rates */
1484         .ops = {
1485                 .open = stac92xx_playback_pcm_open,
1486                 .prepare = stac92xx_playback_pcm_prepare,
1487                 .cleanup = stac92xx_playback_pcm_cleanup
1488         },
1489 };
1490
1491 static struct hda_pcm_stream stac92xx_pcm_analog_capture = {
1492         .channels_min = 2,
1493         .channels_max = 2,
1494         /* NID + .substreams is set in stac92xx_build_pcms */
1495         .ops = {
1496                 .prepare = stac92xx_capture_pcm_prepare,
1497                 .cleanup = stac92xx_capture_pcm_cleanup
1498         },
1499 };
1500
1501 static int stac92xx_build_pcms(struct hda_codec *codec)
1502 {
1503         struct sigmatel_spec *spec = codec->spec;
1504         struct hda_pcm *info = spec->pcm_rec;
1505
1506         codec->num_pcms = 1;
1507         codec->pcm_info = info;
1508
1509         info->name = "STAC92xx Analog";
1510         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_playback;
1511         info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_analog_capture;
1512         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
1513         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_adcs;
1514
1515         if (spec->alt_switch) {
1516                 codec->num_pcms++;
1517                 info++;
1518                 info->name = "STAC92xx Analog Alt";
1519                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_alt_playback;
1520         }
1521
1522         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1523                 codec->num_pcms++;
1524                 info++;
1525                 info->name = "STAC92xx Digital";
1526                 if (spec->multiout.dig_out_nid) {
1527                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_digital_playback;
1528                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
1529                 }
1530                 if (spec->dig_in_nid) {
1531                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_digital_capture;
1532                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
1533                 }
1534         }
1535
1536         return 0;
1537 }
1538
1539 static unsigned int stac92xx_get_vref(struct hda_codec *codec, hda_nid_t nid)
1540 {
1541         unsigned int pincap = snd_hda_param_read(codec, nid,
1542                                                  AC_PAR_PIN_CAP);
1543         pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
1544         if (pincap & AC_PINCAP_VREF_100)
1545                 return AC_PINCTL_VREF_100;
1546         if (pincap & AC_PINCAP_VREF_80)
1547                 return AC_PINCTL_VREF_80;
1548         if (pincap & AC_PINCAP_VREF_50)
1549                 return AC_PINCTL_VREF_50;
1550         if (pincap & AC_PINCAP_VREF_GRD)
1551                 return AC_PINCTL_VREF_GRD;
1552         return 0;
1553 }
1554
1555 static void stac92xx_auto_set_pinctl(struct hda_codec *codec, hda_nid_t nid, int pin_type)
1556
1557 {
1558         snd_hda_codec_write_cache(codec, nid, 0,
1559                                   AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
1560 }
1561
1562 #define stac92xx_io_switch_info         snd_ctl_boolean_mono_info
1563
1564 static int stac92xx_io_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1565 {
1566         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1567         struct sigmatel_spec *spec = codec->spec;
1568         int io_idx = kcontrol-> private_value & 0xff;
1569
1570         ucontrol->value.integer.value[0] = spec->io_switch[io_idx];
1571         return 0;
1572 }
1573
1574 static int stac92xx_io_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1575 {
1576         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1577         struct sigmatel_spec *spec = codec->spec;
1578         hda_nid_t nid = kcontrol->private_value >> 8;
1579         int io_idx = kcontrol-> private_value & 0xff;
1580         unsigned short val = !!ucontrol->value.integer.value[0];
1581
1582         spec->io_switch[io_idx] = val;
1583
1584         if (val)
1585                 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
1586         else {
1587                 unsigned int pinctl = AC_PINCTL_IN_EN;
1588                 if (io_idx) /* set VREF for mic */
1589                         pinctl |= stac92xx_get_vref(codec, nid);
1590                 stac92xx_auto_set_pinctl(codec, nid, pinctl);
1591         }
1592
1593         /* check the auto-mute again: we need to mute/unmute the speaker
1594          * appropriately according to the pin direction
1595          */
1596         if (spec->hp_detect)
1597                 codec->patch_ops.unsol_event(codec, STAC_HP_EVENT << 26);
1598
1599         return 1;
1600 }
1601
1602 #define stac92xx_clfe_switch_info snd_ctl_boolean_mono_info
1603
1604 static int stac92xx_clfe_switch_get(struct snd_kcontrol *kcontrol,
1605                 struct snd_ctl_elem_value *ucontrol)
1606 {
1607         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1608         struct sigmatel_spec *spec = codec->spec;
1609
1610         ucontrol->value.integer.value[0] = spec->clfe_swap;
1611         return 0;
1612 }
1613
1614 static int stac92xx_clfe_switch_put(struct snd_kcontrol *kcontrol,
1615                 struct snd_ctl_elem_value *ucontrol)
1616 {
1617         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1618         struct sigmatel_spec *spec = codec->spec;
1619         hda_nid_t nid = kcontrol->private_value & 0xff;
1620         unsigned int val = !!ucontrol->value.integer.value[0];
1621
1622         if (spec->clfe_swap == val)
1623                 return 0;
1624
1625         spec->clfe_swap = val;
1626
1627         snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_EAPD_BTLENABLE,
1628                 spec->clfe_swap ? 0x4 : 0x0);
1629
1630         return 1;
1631 }
1632
1633 #define STAC_CODEC_IO_SWITCH(xname, xpval) \
1634         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1635           .name = xname, \
1636           .index = 0, \
1637           .info = stac92xx_io_switch_info, \
1638           .get = stac92xx_io_switch_get, \
1639           .put = stac92xx_io_switch_put, \
1640           .private_value = xpval, \
1641         }
1642
1643 #define STAC_CODEC_CLFE_SWITCH(xname, xpval) \
1644         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1645           .name = xname, \
1646           .index = 0, \
1647           .info = stac92xx_clfe_switch_info, \
1648           .get = stac92xx_clfe_switch_get, \
1649           .put = stac92xx_clfe_switch_put, \
1650           .private_value = xpval, \
1651         }
1652
1653 enum {
1654         STAC_CTL_WIDGET_VOL,
1655         STAC_CTL_WIDGET_MUTE,
1656         STAC_CTL_WIDGET_IO_SWITCH,
1657         STAC_CTL_WIDGET_CLFE_SWITCH
1658 };
1659
1660 static struct snd_kcontrol_new stac92xx_control_templates[] = {
1661         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
1662         HDA_CODEC_MUTE(NULL, 0, 0, 0),
1663         STAC_CODEC_IO_SWITCH(NULL, 0),
1664         STAC_CODEC_CLFE_SWITCH(NULL, 0),
1665 };
1666
1667 /* add dynamic controls */
1668 static int stac92xx_add_control(struct sigmatel_spec *spec, int type, const char *name, unsigned long val)
1669 {
1670         struct snd_kcontrol_new *knew;
1671
1672         if (spec->num_kctl_used >= spec->num_kctl_alloc) {
1673                 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
1674
1675                 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
1676                 if (! knew)
1677                         return -ENOMEM;
1678                 if (spec->kctl_alloc) {
1679                         memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
1680                         kfree(spec->kctl_alloc);
1681                 }
1682                 spec->kctl_alloc = knew;
1683                 spec->num_kctl_alloc = num;
1684         }
1685
1686         knew = &spec->kctl_alloc[spec->num_kctl_used];
1687         *knew = stac92xx_control_templates[type];
1688         knew->name = kstrdup(name, GFP_KERNEL);
1689         if (! knew->name)
1690                 return -ENOMEM;
1691         knew->private_value = val;
1692         spec->num_kctl_used++;
1693         return 0;
1694 }
1695
1696 /* flag inputs as additional dynamic lineouts */
1697 static int stac92xx_add_dyn_out_pins(struct hda_codec *codec, struct auto_pin_cfg *cfg)
1698 {
1699         struct sigmatel_spec *spec = codec->spec;
1700         unsigned int wcaps, wtype;
1701         int i, num_dacs = 0;
1702         
1703         /* use the wcaps cache to count all DACs available for line-outs */
1704         for (i = 0; i < codec->num_nodes; i++) {
1705                 wcaps = codec->wcaps[i];
1706                 wtype = (wcaps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
1707                 if (wtype == AC_WID_AUD_OUT && !(wcaps & AC_WCAP_DIGITAL))
1708                         num_dacs++;
1709         }
1710
1711         snd_printdd("%s: total dac count=%d\n", __func__, num_dacs);
1712         
1713         switch (cfg->line_outs) {
1714         case 3:
1715                 /* add line-in as side */
1716                 if (cfg->input_pins[AUTO_PIN_LINE] && num_dacs > 3) {
1717                         cfg->line_out_pins[cfg->line_outs] =
1718                                 cfg->input_pins[AUTO_PIN_LINE];
1719                         spec->line_switch = 1;
1720                         cfg->line_outs++;
1721                 }
1722                 break;
1723         case 2:
1724                 /* add line-in as clfe and mic as side */
1725                 if (cfg->input_pins[AUTO_PIN_LINE] && num_dacs > 2) {
1726                         cfg->line_out_pins[cfg->line_outs] =
1727                                 cfg->input_pins[AUTO_PIN_LINE];
1728                         spec->line_switch = 1;
1729                         cfg->line_outs++;
1730                 }
1731                 if (cfg->input_pins[AUTO_PIN_MIC] && num_dacs > 3) {
1732                         cfg->line_out_pins[cfg->line_outs] =
1733                                 cfg->input_pins[AUTO_PIN_MIC];
1734                         spec->mic_switch = 1;
1735                         cfg->line_outs++;
1736                 }
1737                 break;
1738         case 1:
1739                 /* add line-in as surr and mic as clfe */
1740                 if (cfg->input_pins[AUTO_PIN_LINE] && num_dacs > 1) {
1741                         cfg->line_out_pins[cfg->line_outs] =
1742                                 cfg->input_pins[AUTO_PIN_LINE];
1743                         spec->line_switch = 1;
1744                         cfg->line_outs++;
1745                 }
1746                 if (cfg->input_pins[AUTO_PIN_MIC] && num_dacs > 2) {
1747                         cfg->line_out_pins[cfg->line_outs] =
1748                                 cfg->input_pins[AUTO_PIN_MIC];
1749                         spec->mic_switch = 1;
1750                         cfg->line_outs++;
1751                 }
1752                 break;
1753         }
1754
1755         return 0;
1756 }
1757
1758
1759 static int is_in_dac_nids(struct sigmatel_spec *spec, hda_nid_t nid)
1760 {
1761         int i;
1762         
1763         for (i = 0; i < spec->multiout.num_dacs; i++) {
1764                 if (spec->multiout.dac_nids[i] == nid)
1765                         return 1;
1766         }
1767
1768         return 0;
1769 }
1770
1771 /*
1772  * Fill in the dac_nids table from the parsed pin configuration
1773  * This function only works when every pin in line_out_pins[]
1774  * contains atleast one DAC in its connection list. Some 92xx
1775  * codecs are not connected directly to a DAC, such as the 9200
1776  * and 9202/925x. For those, dac_nids[] must be hard-coded.
1777  */
1778 static int stac92xx_auto_fill_dac_nids(struct hda_codec *codec,
1779                                        struct auto_pin_cfg *cfg)
1780 {
1781         struct sigmatel_spec *spec = codec->spec;
1782         int i, j, conn_len = 0; 
1783         hda_nid_t nid, conn[HDA_MAX_CONNECTIONS];
1784         unsigned int wcaps, wtype;
1785         
1786         for (i = 0; i < cfg->line_outs; i++) {
1787                 nid = cfg->line_out_pins[i];
1788                 conn_len = snd_hda_get_connections(codec, nid, conn,
1789                                                    HDA_MAX_CONNECTIONS);
1790                 for (j = 0; j < conn_len; j++) {
1791                         wcaps = snd_hda_param_read(codec, conn[j],
1792                                                    AC_PAR_AUDIO_WIDGET_CAP);
1793                         wtype = (wcaps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
1794
1795                         if (wtype != AC_WID_AUD_OUT ||
1796                             (wcaps & AC_WCAP_DIGITAL))
1797                                 continue;
1798                         /* conn[j] is a DAC routed to this line-out */
1799                         if (!is_in_dac_nids(spec, conn[j]))
1800                                 break;
1801                 }
1802
1803                 if (j == conn_len) {
1804                         if (spec->multiout.num_dacs > 0) {
1805                                 /* we have already working output pins,
1806                                  * so let's drop the broken ones again
1807                                  */
1808                                 cfg->line_outs = spec->multiout.num_dacs;
1809                                 break;
1810                         }
1811                         /* error out, no available DAC found */
1812                         snd_printk(KERN_ERR
1813                                    "%s: No available DAC for pin 0x%x\n",
1814                                    __func__, nid);
1815                         return -ENODEV;
1816                 }
1817
1818                 spec->multiout.dac_nids[i] = conn[j];
1819                 spec->multiout.num_dacs++;
1820                 if (conn_len > 1) {
1821                         /* select this DAC in the pin's input mux */
1822                         snd_hda_codec_write_cache(codec, nid, 0,
1823                                                   AC_VERB_SET_CONNECT_SEL, j);
1824
1825                 }
1826         }
1827
1828         snd_printd("dac_nids=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
1829                    spec->multiout.num_dacs,
1830                    spec->multiout.dac_nids[0],
1831                    spec->multiout.dac_nids[1],
1832                    spec->multiout.dac_nids[2],
1833                    spec->multiout.dac_nids[3],
1834                    spec->multiout.dac_nids[4]);
1835         return 0;
1836 }
1837
1838 /* create volume control/switch for the given prefx type */
1839 static int create_controls(struct sigmatel_spec *spec, const char *pfx, hda_nid_t nid, int chs)
1840 {
1841         char name[32];
1842         int err;
1843
1844         sprintf(name, "%s Playback Volume", pfx);
1845         err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, name,
1846                                    HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1847         if (err < 0)
1848                 return err;
1849         sprintf(name, "%s Playback Switch", pfx);
1850         err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, name,
1851                                    HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1852         if (err < 0)
1853                 return err;
1854         return 0;
1855 }
1856
1857 /* add playback controls from the parsed DAC table */
1858 static int stac92xx_auto_create_multi_out_ctls(struct hda_codec *codec,
1859                                                const struct auto_pin_cfg *cfg)
1860 {
1861         static const char *chname[4] = {
1862                 "Front", "Surround", NULL /*CLFE*/, "Side"
1863         };
1864         hda_nid_t nid;
1865         int i, err;
1866
1867         struct sigmatel_spec *spec = codec->spec;
1868         unsigned int wid_caps;
1869
1870
1871         for (i = 0; i < cfg->line_outs; i++) {
1872                 if (!spec->multiout.dac_nids[i])
1873                         continue;
1874
1875                 nid = spec->multiout.dac_nids[i];
1876
1877                 if (i == 2) {
1878                         /* Center/LFE */
1879                         err = create_controls(spec, "Center", nid, 1);
1880                         if (err < 0)
1881                                 return err;
1882                         err = create_controls(spec, "LFE", nid, 2);
1883                         if (err < 0)
1884                                 return err;
1885
1886                         wid_caps = get_wcaps(codec, nid);
1887
1888                         if (wid_caps & AC_WCAP_LR_SWAP) {
1889                                 err = stac92xx_add_control(spec,
1890                                         STAC_CTL_WIDGET_CLFE_SWITCH,
1891                                         "Swap Center/LFE Playback Switch", nid);
1892
1893                                 if (err < 0)
1894                                         return err;
1895                         }
1896
1897                 } else {
1898                         err = create_controls(spec, chname[i], nid, 3);
1899                         if (err < 0)
1900                                 return err;
1901                 }
1902         }
1903
1904         if (spec->line_switch)
1905                 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Line In as Output Switch", cfg->input_pins[AUTO_PIN_LINE] << 8)) < 0)
1906                         return err;
1907
1908         if (spec->mic_switch)
1909                 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Mic as Output Switch", (cfg->input_pins[AUTO_PIN_MIC] << 8) | 1)) < 0)
1910                         return err;
1911
1912         return 0;
1913 }
1914
1915 static int check_in_dac_nids(struct sigmatel_spec *spec, hda_nid_t nid)
1916 {
1917         if (is_in_dac_nids(spec, nid))
1918                 return 1;
1919         if (spec->multiout.hp_nid == nid)
1920                 return 1;
1921         return 0;
1922 }
1923
1924 static int add_spec_dacs(struct sigmatel_spec *spec, hda_nid_t nid)
1925 {
1926         if (!spec->multiout.hp_nid)
1927                 spec->multiout.hp_nid = nid;
1928         else if (spec->multiout.num_dacs > 4) {
1929                 printk(KERN_WARNING "stac92xx: No space for DAC 0x%x\n", nid);
1930                 return 1;
1931         } else {
1932                 spec->multiout.dac_nids[spec->multiout.num_dacs] = nid;
1933                 spec->multiout.num_dacs++;
1934         }
1935         return 0;
1936 }
1937
1938 /* add playback controls for Speaker and HP outputs */
1939 static int stac92xx_auto_create_hp_ctls(struct hda_codec *codec,
1940                                         struct auto_pin_cfg *cfg)
1941 {
1942         struct sigmatel_spec *spec = codec->spec;
1943         hda_nid_t nid;
1944         int i, old_num_dacs, err;
1945
1946         old_num_dacs = spec->multiout.num_dacs;
1947         for (i = 0; i < cfg->hp_outs; i++) {
1948                 unsigned int wid_caps = get_wcaps(codec, cfg->hp_pins[i]);
1949                 if (wid_caps & AC_WCAP_UNSOL_CAP)
1950                         spec->hp_detect = 1;
1951                 nid = snd_hda_codec_read(codec, cfg->hp_pins[i], 0,
1952                                          AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
1953                 if (check_in_dac_nids(spec, nid))
1954                         nid = 0;
1955                 if (! nid)
1956                         continue;
1957                 add_spec_dacs(spec, nid);
1958         }
1959         for (i = 0; i < cfg->speaker_outs; i++) {
1960                 nid = snd_hda_codec_read(codec, cfg->speaker_pins[i], 0,
1961                                          AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
1962                 if (check_in_dac_nids(spec, nid))
1963                         nid = 0;
1964                 if (! nid)
1965                         continue;
1966                 add_spec_dacs(spec, nid);
1967         }
1968         for (i = 0; i < cfg->line_outs; i++) {
1969                 nid = snd_hda_codec_read(codec, cfg->line_out_pins[i], 0,
1970                                         AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
1971                 if (check_in_dac_nids(spec, nid))
1972                         nid = 0;
1973                 if (! nid)
1974                         continue;
1975                 add_spec_dacs(spec, nid);
1976         }
1977         for (i = old_num_dacs; i < spec->multiout.num_dacs; i++) {
1978                 static const char *pfxs[] = {
1979                         "Speaker", "External Speaker", "Speaker2",
1980                 };
1981                 err = create_controls(spec, pfxs[i - old_num_dacs],
1982                                       spec->multiout.dac_nids[i], 3);
1983                 if (err < 0)
1984                         return err;
1985         }
1986         if (spec->multiout.hp_nid) {
1987                 const char *pfx;
1988                 if (old_num_dacs == spec->multiout.num_dacs)
1989                         pfx = "Master";
1990                 else
1991                         pfx = "Headphone";
1992                 err = create_controls(spec, pfx, spec->multiout.hp_nid, 3);
1993                 if (err < 0)
1994                         return err;
1995         }
1996
1997         return 0;
1998 }
1999
2000 /* labels for dmic mux inputs */
2001 static const char *stac92xx_dmic_labels[5] = {
2002         "Analog Inputs", "Digital Mic 1", "Digital Mic 2",
2003         "Digital Mic 3", "Digital Mic 4"
2004 };
2005
2006 /* create playback/capture controls for input pins on dmic capable codecs */
2007 static int stac92xx_auto_create_dmic_input_ctls(struct hda_codec *codec,
2008                                                 const struct auto_pin_cfg *cfg)
2009 {
2010         struct sigmatel_spec *spec = codec->spec;
2011         struct hda_input_mux *dimux = &spec->private_dimux;
2012         hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
2013         int i, j;
2014
2015         dimux->items[dimux->num_items].label = stac92xx_dmic_labels[0];
2016         dimux->items[dimux->num_items].index = 0;
2017         dimux->num_items++;
2018
2019         for (i = 0; i < spec->num_dmics; i++) {
2020                 int index;
2021                 int num_cons;
2022                 unsigned int def_conf;
2023
2024                 def_conf = snd_hda_codec_read(codec,
2025                                               spec->dmic_nids[i],
2026                                               0,
2027                                               AC_VERB_GET_CONFIG_DEFAULT,
2028                                               0);
2029                 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2030                         continue;
2031
2032                 num_cons = snd_hda_get_connections(codec,
2033                                 spec->dmux_nid,
2034                                 con_lst,
2035                                 HDA_MAX_NUM_INPUTS);
2036                 for (j = 0; j < num_cons; j++)
2037                         if (con_lst[j] == spec->dmic_nids[i]) {
2038                                 index = j;
2039                                 goto found;
2040                         }
2041                 continue;
2042 found:
2043                 dimux->items[dimux->num_items].label =
2044                         stac92xx_dmic_labels[dimux->num_items];
2045                 dimux->items[dimux->num_items].index = index;
2046                 dimux->num_items++;
2047         }
2048
2049         return 0;
2050 }
2051
2052 /* create playback/capture controls for input pins */
2053 static int stac92xx_auto_create_analog_input_ctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
2054 {
2055         struct sigmatel_spec *spec = codec->spec;
2056         struct hda_input_mux *imux = &spec->private_imux;
2057         hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
2058         int i, j, k;
2059
2060         for (i = 0; i < AUTO_PIN_LAST; i++) {
2061                 int index;
2062
2063                 if (!cfg->input_pins[i])
2064                         continue;
2065                 index = -1;
2066                 for (j = 0; j < spec->num_muxes; j++) {
2067                         int num_cons;
2068                         num_cons = snd_hda_get_connections(codec,
2069                                                            spec->mux_nids[j],
2070                                                            con_lst,
2071                                                            HDA_MAX_NUM_INPUTS);
2072                         for (k = 0; k < num_cons; k++)
2073                                 if (con_lst[k] == cfg->input_pins[i]) {
2074                                         index = k;
2075                                         goto found;
2076                                 }
2077                 }
2078                 continue;
2079         found:
2080                 imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
2081                 imux->items[imux->num_items].index = index;
2082                 imux->num_items++;
2083         }
2084
2085         if (imux->num_items) {
2086                 /*
2087                  * Set the current input for the muxes.
2088                  * The STAC9221 has two input muxes with identical source
2089                  * NID lists.  Hopefully this won't get confused.
2090                  */
2091                 for (i = 0; i < spec->num_muxes; i++) {
2092                         snd_hda_codec_write_cache(codec, spec->mux_nids[i], 0,
2093                                                   AC_VERB_SET_CONNECT_SEL,
2094                                                   imux->items[0].index);
2095                 }
2096         }
2097
2098         return 0;
2099 }
2100
2101 static void stac92xx_auto_init_multi_out(struct hda_codec *codec)
2102 {
2103         struct sigmatel_spec *spec = codec->spec;
2104         int i;
2105
2106         for (i = 0; i < spec->autocfg.line_outs; i++) {
2107                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
2108                 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
2109         }
2110 }
2111
2112 static void stac92xx_auto_init_hp_out(struct hda_codec *codec)
2113 {
2114         struct sigmatel_spec *spec = codec->spec;
2115         int i;
2116
2117         for (i = 0; i < spec->autocfg.hp_outs; i++) {
2118                 hda_nid_t pin;
2119                 pin = spec->autocfg.hp_pins[i];
2120                 if (pin) /* connect to front */
2121                         stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
2122         }
2123         for (i = 0; i < spec->autocfg.speaker_outs; i++) {
2124                 hda_nid_t pin;
2125                 pin = spec->autocfg.speaker_pins[i];
2126                 if (pin) /* connect to front */
2127                         stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN);
2128         }
2129 }
2130
2131 static int stac92xx_parse_auto_config(struct hda_codec *codec, hda_nid_t dig_out, hda_nid_t dig_in)
2132 {
2133         struct sigmatel_spec *spec = codec->spec;
2134         int err;
2135         int hp_speaker_swap = 0;
2136
2137         if ((err = snd_hda_parse_pin_def_config(codec,
2138                                                 &spec->autocfg,
2139                                                 spec->dmic_nids)) < 0)
2140                 return err;
2141         if (! spec->autocfg.line_outs)
2142                 return 0; /* can't find valid pin config */
2143
2144         /* If we have no real line-out pin and multiple hp-outs, HPs should
2145          * be set up as multi-channel outputs.
2146          */
2147         if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
2148             spec->autocfg.hp_outs > 1) {
2149                 /* Copy hp_outs to line_outs, backup line_outs in
2150                  * speaker_outs so that the following routines can handle
2151                  * HP pins as primary outputs.
2152                  */
2153                 memcpy(spec->autocfg.speaker_pins, spec->autocfg.line_out_pins,
2154                        sizeof(spec->autocfg.line_out_pins));
2155                 spec->autocfg.speaker_outs = spec->autocfg.line_outs;
2156                 memcpy(spec->autocfg.line_out_pins, spec->autocfg.hp_pins,
2157                        sizeof(spec->autocfg.hp_pins));
2158                 spec->autocfg.line_outs = spec->autocfg.hp_outs;
2159                 hp_speaker_swap = 1;
2160         }
2161
2162         if ((err = stac92xx_add_dyn_out_pins(codec, &spec->autocfg)) < 0)
2163                 return err;
2164         if (spec->multiout.num_dacs == 0)
2165                 if ((err = stac92xx_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
2166                         return err;
2167
2168         err = stac92xx_auto_create_multi_out_ctls(codec, &spec->autocfg);
2169
2170         if (err < 0)
2171                 return err;
2172
2173         if (hp_speaker_swap == 1) {
2174                 /* Restore the hp_outs and line_outs */
2175                 memcpy(spec->autocfg.hp_pins, spec->autocfg.line_out_pins,
2176                        sizeof(spec->autocfg.line_out_pins));
2177                 spec->autocfg.hp_outs = spec->autocfg.line_outs;
2178                 memcpy(spec->autocfg.line_out_pins, spec->autocfg.speaker_pins,
2179                        sizeof(spec->autocfg.speaker_pins));
2180                 spec->autocfg.line_outs = spec->autocfg.speaker_outs;
2181                 memset(spec->autocfg.speaker_pins, 0,
2182                        sizeof(spec->autocfg.speaker_pins));
2183                 spec->autocfg.speaker_outs = 0;
2184         }
2185
2186         err = stac92xx_auto_create_hp_ctls(codec, &spec->autocfg);
2187
2188         if (err < 0)
2189                 return err;
2190
2191         err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg);
2192
2193         if (err < 0)
2194                 return err;
2195
2196         if (spec->num_dmics > 0)
2197                 if ((err = stac92xx_auto_create_dmic_input_ctls(codec,
2198                                                 &spec->autocfg)) < 0)
2199                         return err;
2200
2201         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2202         if (spec->multiout.max_channels > 2)
2203                 spec->surr_switch = 1;
2204
2205         if (spec->autocfg.dig_out_pin)
2206                 spec->multiout.dig_out_nid = dig_out;
2207         if (spec->autocfg.dig_in_pin)
2208                 spec->dig_in_nid = dig_in;
2209
2210         if (spec->kctl_alloc)
2211                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
2212
2213         spec->input_mux = &spec->private_imux;
2214         spec->dinput_mux = &spec->private_dimux;
2215
2216         return 1;
2217 }
2218
2219 /* add playback controls for HP output */
2220 static int stac9200_auto_create_hp_ctls(struct hda_codec *codec,
2221                                         struct auto_pin_cfg *cfg)
2222 {
2223         struct sigmatel_spec *spec = codec->spec;
2224         hda_nid_t pin = cfg->hp_pins[0];
2225         unsigned int wid_caps;
2226
2227         if (! pin)
2228                 return 0;
2229
2230         wid_caps = get_wcaps(codec, pin);
2231         if (wid_caps & AC_WCAP_UNSOL_CAP)
2232                 spec->hp_detect = 1;
2233
2234         return 0;
2235 }
2236
2237 /* add playback controls for LFE output */
2238 static int stac9200_auto_create_lfe_ctls(struct hda_codec *codec,
2239                                         struct auto_pin_cfg *cfg)
2240 {
2241         struct sigmatel_spec *spec = codec->spec;
2242         int err;
2243         hda_nid_t lfe_pin = 0x0;
2244         int i;
2245
2246         /*
2247          * search speaker outs and line outs for a mono speaker pin
2248          * with an amp.  If one is found, add LFE controls
2249          * for it.
2250          */
2251         for (i = 0; i < spec->autocfg.speaker_outs && lfe_pin == 0x0; i++) {
2252                 hda_nid_t pin = spec->autocfg.speaker_pins[i];
2253                 unsigned long wcaps = get_wcaps(codec, pin);
2254                 wcaps &= (AC_WCAP_STEREO | AC_WCAP_OUT_AMP);
2255                 if (wcaps == AC_WCAP_OUT_AMP)
2256                         /* found a mono speaker with an amp, must be lfe */
2257                         lfe_pin = pin;
2258         }
2259
2260         /* if speaker_outs is 0, then speakers may be in line_outs */
2261         if (lfe_pin == 0 && spec->autocfg.speaker_outs == 0) {
2262                 for (i = 0; i < spec->autocfg.line_outs && lfe_pin == 0x0; i++) {
2263                         hda_nid_t pin = spec->autocfg.line_out_pins[i];
2264                         unsigned long cfg;
2265                         cfg = snd_hda_codec_read(codec, pin, 0,
2266                                                  AC_VERB_GET_CONFIG_DEFAULT,
2267                                                  0x00);
2268                         if (get_defcfg_device(cfg) == AC_JACK_SPEAKER) {
2269                                 unsigned long wcaps = get_wcaps(codec, pin);
2270                                 wcaps &= (AC_WCAP_STEREO | AC_WCAP_OUT_AMP);
2271                                 if (wcaps == AC_WCAP_OUT_AMP)
2272                                         /* found a mono speaker with an amp,
2273                                            must be lfe */
2274                                         lfe_pin = pin;
2275                         }
2276                 }
2277         }
2278
2279         if (lfe_pin) {
2280                 err = create_controls(spec, "LFE", lfe_pin, 1);
2281                 if (err < 0)
2282                         return err;
2283         }
2284
2285         return 0;
2286 }
2287
2288 static int stac9200_parse_auto_config(struct hda_codec *codec)
2289 {
2290         struct sigmatel_spec *spec = codec->spec;
2291         int err;
2292
2293         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
2294                 return err;
2295
2296         if ((err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
2297                 return err;
2298
2299         if ((err = stac9200_auto_create_hp_ctls(codec, &spec->autocfg)) < 0)
2300                 return err;
2301
2302         if ((err = stac9200_auto_create_lfe_ctls(codec, &spec->autocfg)) < 0)
2303                 return err;
2304
2305         if (spec->autocfg.dig_out_pin)
2306                 spec->multiout.dig_out_nid = 0x05;
2307         if (spec->autocfg.dig_in_pin)
2308                 spec->dig_in_nid = 0x04;
2309
2310         if (spec->kctl_alloc)
2311                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
2312
2313         spec->input_mux = &spec->private_imux;
2314         spec->dinput_mux = &spec->private_dimux;
2315
2316         return 1;
2317 }
2318
2319 /*
2320  * Early 2006 Intel Macintoshes with STAC9220X5 codecs seem to have a
2321  * funky external mute control using GPIO pins.
2322  */
2323
2324 static void stac922x_gpio_mute(struct hda_codec *codec, int pin, int muted)
2325 {
2326         unsigned int gpiostate, gpiomask, gpiodir;
2327
2328         gpiostate = snd_hda_codec_read(codec, codec->afg, 0,
2329                                        AC_VERB_GET_GPIO_DATA, 0);
2330
2331         if (!muted)
2332                 gpiostate |= (1 << pin);
2333         else
2334                 gpiostate &= ~(1 << pin);
2335
2336         gpiomask = snd_hda_codec_read(codec, codec->afg, 0,
2337                                       AC_VERB_GET_GPIO_MASK, 0);
2338         gpiomask |= (1 << pin);
2339
2340         gpiodir = snd_hda_codec_read(codec, codec->afg, 0,
2341                                      AC_VERB_GET_GPIO_DIRECTION, 0);
2342         gpiodir |= (1 << pin);
2343
2344         /* AppleHDA seems to do this -- WTF is this verb?? */
2345         snd_hda_codec_write(codec, codec->afg, 0, 0x7e7, 0);
2346
2347         snd_hda_codec_write(codec, codec->afg, 0,
2348                             AC_VERB_SET_GPIO_MASK, gpiomask);
2349         snd_hda_codec_write(codec, codec->afg, 0,
2350                             AC_VERB_SET_GPIO_DIRECTION, gpiodir);
2351
2352         msleep(1);
2353
2354         snd_hda_codec_write(codec, codec->afg, 0,
2355                             AC_VERB_SET_GPIO_DATA, gpiostate);
2356 }
2357
2358 static void enable_pin_detect(struct hda_codec *codec, hda_nid_t nid,
2359                               unsigned int event)
2360 {
2361         if (get_wcaps(codec, nid) & AC_WCAP_UNSOL_CAP)
2362                 snd_hda_codec_write_cache(codec, nid, 0,
2363                                           AC_VERB_SET_UNSOLICITED_ENABLE,
2364                                           (AC_USRSP_EN | event));
2365 }
2366
2367 static int stac92xx_init(struct hda_codec *codec)
2368 {
2369         struct sigmatel_spec *spec = codec->spec;
2370         struct auto_pin_cfg *cfg = &spec->autocfg;
2371         int i;
2372
2373         snd_hda_sequence_write(codec, spec->init);
2374
2375         /* set up pins */
2376         if (spec->hp_detect) {
2377                 /* Enable unsolicited responses on the HP widget */
2378                 for (i = 0; i < cfg->hp_outs; i++)
2379                         enable_pin_detect(codec, cfg->hp_pins[i],
2380                                           STAC_HP_EVENT);
2381                 /* force to enable the first line-out; the others are set up
2382                  * in unsol_event
2383                  */
2384                 stac92xx_auto_set_pinctl(codec, spec->autocfg.line_out_pins[0],
2385                                          AC_PINCTL_OUT_EN);
2386                 stac92xx_auto_init_hp_out(codec);
2387                 /* fake event to set up pins */
2388                 codec->patch_ops.unsol_event(codec, STAC_HP_EVENT << 26);
2389         } else {
2390                 stac92xx_auto_init_multi_out(codec);
2391                 stac92xx_auto_init_hp_out(codec);
2392         }
2393         for (i = 0; i < AUTO_PIN_LAST; i++) {
2394                 hda_nid_t nid = cfg->input_pins[i];
2395                 if (nid) {
2396                         unsigned int pinctl = AC_PINCTL_IN_EN;
2397                         if (i == AUTO_PIN_MIC || i == AUTO_PIN_FRONT_MIC)
2398                                 pinctl |= stac92xx_get_vref(codec, nid);
2399                         stac92xx_auto_set_pinctl(codec, nid, pinctl);
2400                 }
2401         }
2402         if (spec->num_dmics > 0)
2403                 for (i = 0; i < spec->num_dmics; i++)
2404                         stac92xx_auto_set_pinctl(codec, spec->dmic_nids[i],
2405                                                  AC_PINCTL_IN_EN);
2406
2407         if (cfg->dig_out_pin)
2408                 stac92xx_auto_set_pinctl(codec, cfg->dig_out_pin,
2409                                          AC_PINCTL_OUT_EN);
2410         if (cfg->dig_in_pin)
2411                 stac92xx_auto_set_pinctl(codec, cfg->dig_in_pin,
2412                                          AC_PINCTL_IN_EN);
2413
2414         if (spec->gpio_mute) {
2415                 stac922x_gpio_mute(codec, 0, 0);
2416                 stac922x_gpio_mute(codec, 1, 0);
2417         }
2418
2419         return 0;
2420 }
2421
2422 static void stac92xx_free(struct hda_codec *codec)
2423 {
2424         struct sigmatel_spec *spec = codec->spec;
2425         int i;
2426
2427         if (! spec)
2428                 return;
2429
2430         if (spec->kctl_alloc) {
2431                 for (i = 0; i < spec->num_kctl_used; i++)
2432                         kfree(spec->kctl_alloc[i].name);
2433                 kfree(spec->kctl_alloc);
2434         }
2435
2436         if (spec->bios_pin_configs)
2437                 kfree(spec->bios_pin_configs);
2438
2439         kfree(spec);
2440 }
2441
2442 static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
2443                                 unsigned int flag)
2444 {
2445         unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
2446                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
2447
2448         if (pin_ctl & AC_PINCTL_IN_EN) {
2449                 /*
2450                  * we need to check the current set-up direction of
2451                  * shared input pins since they can be switched via
2452                  * "xxx as Output" mixer switch
2453                  */
2454                 struct sigmatel_spec *spec = codec->spec;
2455                 struct auto_pin_cfg *cfg = &spec->autocfg;
2456                 if ((nid == cfg->input_pins[AUTO_PIN_LINE] &&
2457                      spec->line_switch) ||
2458                     (nid == cfg->input_pins[AUTO_PIN_MIC] &&
2459                      spec->mic_switch))
2460                         return;
2461         }
2462
2463         /* if setting pin direction bits, clear the current
2464            direction bits first */
2465         if (flag & (AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN))
2466                 pin_ctl &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
2467         
2468         snd_hda_codec_write_cache(codec, nid, 0,
2469                         AC_VERB_SET_PIN_WIDGET_CONTROL,
2470                         pin_ctl | flag);
2471 }
2472
2473 static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
2474                                   unsigned int flag)
2475 {
2476         unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
2477                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
2478         snd_hda_codec_write_cache(codec, nid, 0,
2479                         AC_VERB_SET_PIN_WIDGET_CONTROL,
2480                         pin_ctl & ~flag);
2481 }
2482
2483 static int get_hp_pin_presence(struct hda_codec *codec, hda_nid_t nid)
2484 {
2485         if (!nid)
2486                 return 0;
2487         if (snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_PIN_SENSE, 0x00)
2488             & (1 << 31)) {
2489                 unsigned int pinctl;
2490                 pinctl = snd_hda_codec_read(codec, nid, 0,
2491                                             AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2492                 if (pinctl & AC_PINCTL_IN_EN)
2493                         return 0; /* mic- or line-input */
2494                 else
2495                         return 1; /* HP-output */
2496         }
2497         return 0;
2498 }
2499
2500 static void stac92xx_hp_detect(struct hda_codec *codec, unsigned int res)
2501 {
2502         struct sigmatel_spec *spec = codec->spec;
2503         struct auto_pin_cfg *cfg = &spec->autocfg;
2504         int i, presence;
2505
2506         presence = 0;
2507         for (i = 0; i < cfg->hp_outs; i++) {
2508                 presence = get_hp_pin_presence(codec, cfg->hp_pins[i]);
2509                 if (presence)
2510                         break;
2511         }
2512
2513         if (presence) {
2514                 /* disable lineouts, enable hp */
2515                 for (i = 0; i < cfg->line_outs; i++)
2516                         stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
2517                                                 AC_PINCTL_OUT_EN);
2518                 for (i = 0; i < cfg->speaker_outs; i++)
2519                         stac92xx_reset_pinctl(codec, cfg->speaker_pins[i],
2520                                                 AC_PINCTL_OUT_EN);
2521         } else {
2522                 /* enable lineouts, disable hp */
2523                 for (i = 0; i < cfg->line_outs; i++)
2524                         stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
2525                                                 AC_PINCTL_OUT_EN);
2526                 for (i = 0; i < cfg->speaker_outs; i++)
2527                         stac92xx_set_pinctl(codec, cfg->speaker_pins[i],
2528                                                 AC_PINCTL_OUT_EN);
2529         }
2530
2531
2532 static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
2533 {
2534         switch (res >> 26) {
2535         case STAC_HP_EVENT:
2536                 stac92xx_hp_detect(codec, res);
2537                 break;
2538         }
2539 }
2540
2541 #ifdef SND_HDA_NEEDS_RESUME
2542 static int stac92xx_resume(struct hda_codec *codec)
2543 {
2544         struct sigmatel_spec *spec = codec->spec;
2545
2546         stac92xx_set_config_regs(codec);
2547         snd_hda_sequence_write(codec, spec->init);
2548         if (spec->gpio_mute) {
2549                 stac922x_gpio_mute(codec, 0, 0);
2550                 stac922x_gpio_mute(codec, 1, 0);
2551         }
2552         snd_hda_codec_resume_amp(codec);
2553         snd_hda_codec_resume_cache(codec);
2554         /* invoke unsolicited event to reset the HP state */
2555         if (spec->hp_detect)
2556                 codec->patch_ops.unsol_event(codec, STAC_HP_EVENT << 26);
2557         return 0;
2558 }
2559 #endif
2560
2561 static struct hda_codec_ops stac92xx_patch_ops = {
2562         .build_controls = stac92xx_build_controls,
2563         .build_pcms = stac92xx_build_pcms,
2564         .init = stac92xx_init,
2565         .free = stac92xx_free,
2566         .unsol_event = stac92xx_unsol_event,
2567 #ifdef SND_HDA_NEEDS_RESUME
2568         .resume = stac92xx_resume,
2569 #endif
2570 };
2571
2572 static int patch_stac9200(struct hda_codec *codec)
2573 {
2574         struct sigmatel_spec *spec;
2575         int err;
2576
2577         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2578         if (spec == NULL)
2579                 return -ENOMEM;
2580
2581         codec->spec = spec;
2582         spec->num_pins = ARRAY_SIZE(stac9200_pin_nids);
2583         spec->pin_nids = stac9200_pin_nids;
2584         spec->board_config = snd_hda_check_board_config(codec, STAC_9200_MODELS,
2585                                                         stac9200_models,
2586                                                         stac9200_cfg_tbl);
2587         if (spec->board_config < 0) {
2588                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9200, using BIOS defaults\n");
2589                 err = stac92xx_save_bios_config_regs(codec);
2590                 if (err < 0) {
2591                         stac92xx_free(codec);
2592                         return err;
2593                 }
2594                 spec->pin_configs = spec->bios_pin_configs;
2595         } else {
2596                 spec->pin_configs = stac9200_brd_tbl[spec->board_config];
2597                 stac92xx_set_config_regs(codec);
2598         }
2599
2600         spec->multiout.max_channels = 2;
2601         spec->multiout.num_dacs = 1;
2602         spec->multiout.dac_nids = stac9200_dac_nids;
2603         spec->adc_nids = stac9200_adc_nids;
2604         spec->mux_nids = stac9200_mux_nids;
2605         spec->num_muxes = 1;
2606         spec->num_dmics = 0;
2607         spec->num_adcs = 1;
2608
2609         if (spec->board_config == STAC_9200_GATEWAY)
2610                 spec->init = stac9200_eapd_init;
2611         else
2612                 spec->init = stac9200_core_init;
2613         spec->mixer = stac9200_mixer;
2614
2615         err = stac9200_parse_auto_config(codec);
2616         if (err < 0) {
2617                 stac92xx_free(codec);
2618                 return err;
2619         }
2620
2621         codec->patch_ops = stac92xx_patch_ops;
2622
2623         return 0;
2624 }
2625
2626 static int patch_stac925x(struct hda_codec *codec)
2627 {
2628         struct sigmatel_spec *spec;
2629         int err;
2630
2631         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2632         if (spec == NULL)
2633                 return -ENOMEM;
2634
2635         codec->spec = spec;
2636         spec->num_pins = ARRAY_SIZE(stac925x_pin_nids);
2637         spec->pin_nids = stac925x_pin_nids;
2638         spec->board_config = snd_hda_check_board_config(codec, STAC_925x_MODELS,
2639                                                         stac925x_models,
2640                                                         stac925x_cfg_tbl);
2641  again:
2642         if (spec->board_config < 0) {
2643                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC925x," 
2644                                       "using BIOS defaults\n");
2645                 err = stac92xx_save_bios_config_regs(codec);
2646                 if (err < 0) {
2647                         stac92xx_free(codec);
2648                         return err;
2649                 }
2650                 spec->pin_configs = spec->bios_pin_configs;
2651         } else if (stac925x_brd_tbl[spec->board_config] != NULL){
2652                 spec->pin_configs = stac925x_brd_tbl[spec->board_config];
2653                 stac92xx_set_config_regs(codec);
2654         }
2655
2656         spec->multiout.max_channels = 2;
2657         spec->multiout.num_dacs = 1;
2658         spec->multiout.dac_nids = stac925x_dac_nids;
2659         spec->adc_nids = stac925x_adc_nids;
2660         spec->mux_nids = stac925x_mux_nids;
2661         spec->num_muxes = 1;
2662         spec->num_adcs = 1;
2663         switch (codec->vendor_id) {
2664         case 0x83847632: /* STAC9202  */
2665         case 0x83847633: /* STAC9202D */
2666         case 0x83847636: /* STAC9251  */
2667         case 0x83847637: /* STAC9251D */
2668                 spec->num_dmics = STAC925X_NUM_DMICS;
2669                 spec->dmic_nids = stac925x_dmic_nids;
2670                 break;
2671         default:
2672                 spec->num_dmics = 0;
2673                 break;
2674         }
2675
2676         spec->init = stac925x_core_init;
2677         spec->mixer = stac925x_mixer;
2678
2679         err = stac92xx_parse_auto_config(codec, 0x8, 0x7);
2680         if (!err) {
2681                 if (spec->board_config < 0) {
2682                         printk(KERN_WARNING "hda_codec: No auto-config is "
2683                                "available, default to model=ref\n");
2684                         spec->board_config = STAC_925x_REF;
2685                         goto again;
2686                 }
2687                 err = -EINVAL;
2688         }
2689         if (err < 0) {
2690                 stac92xx_free(codec);
2691                 return err;
2692         }
2693
2694         codec->patch_ops = stac92xx_patch_ops;
2695
2696         return 0;
2697 }
2698
2699 static int patch_stac92hd71bxx(struct hda_codec *codec)
2700 {
2701         struct sigmatel_spec *spec;
2702         int err = 0;
2703
2704         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2705         if (spec == NULL)
2706                 return -ENOMEM;
2707
2708         codec->spec = spec;
2709         spec->num_pins = ARRAY_SIZE(stac92hd71bxx_pin_nids);
2710         spec->pin_nids = stac92hd71bxx_pin_nids;
2711         spec->board_config = snd_hda_check_board_config(codec,
2712                                                         STAC_92HD71BXX_MODELS,
2713                                                         stac92hd71bxx_models,
2714                                                         stac92hd71bxx_cfg_tbl);
2715 again:
2716         if (spec->board_config < 0) {
2717                 snd_printdd(KERN_INFO "hda_codec: Unknown model for"
2718                         " STAC92HD71BXX, using BIOS defaults\n");
2719                 err = stac92xx_save_bios_config_regs(codec);
2720                 if (err < 0) {
2721                         stac92xx_free(codec);
2722                         return err;
2723                 }
2724                 spec->pin_configs = spec->bios_pin_configs;
2725         } else {
2726                 spec->pin_configs = stac92hd71bxx_brd_tbl[spec->board_config];
2727                 stac92xx_set_config_regs(codec);
2728         }
2729
2730         spec->gpio_mask = spec->gpio_data = 0x00000001; /* GPIO0 High = EAPD */
2731         stac92xx_enable_gpio_mask(codec);
2732
2733         spec->init = stac92hd71bxx_core_init;
2734         spec->mixer = stac92hd71bxx_mixer;
2735
2736         spec->mux_nids = stac92hd71bxx_mux_nids;
2737         spec->adc_nids = stac92hd71bxx_adc_nids;
2738         spec->dmic_nids = stac92hd71bxx_dmic_nids;
2739         spec->dmux_nid = 0x1c;
2740
2741         spec->num_muxes = ARRAY_SIZE(stac92hd71bxx_mux_nids);
2742         spec->num_adcs = ARRAY_SIZE(stac92hd71bxx_adc_nids);
2743         spec->num_dmics = STAC92HD71BXX_NUM_DMICS;
2744
2745         spec->multiout.num_dacs = 2;
2746         spec->multiout.hp_nid = 0x11;
2747         spec->multiout.dac_nids = stac92hd71bxx_dac_nids;
2748
2749         err = stac92xx_parse_auto_config(codec, 0x21, 0x23);
2750         if (!err) {
2751                 if (spec->board_config < 0) {
2752                         printk(KERN_WARNING "hda_codec: No auto-config is "
2753                                "available, default to model=ref\n");
2754                         spec->board_config = STAC_92HD71BXX_REF;
2755                         goto again;
2756                 }
2757                 err = -EINVAL;
2758         }
2759
2760         if (err < 0) {
2761                 stac92xx_free(codec);
2762                 return err;
2763         }
2764
2765         codec->patch_ops = stac92xx_patch_ops;
2766
2767         return 0;
2768 };
2769
2770 static int patch_stac922x(struct hda_codec *codec)
2771 {
2772         struct sigmatel_spec *spec;
2773         int err;
2774
2775         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2776         if (spec == NULL)
2777                 return -ENOMEM;
2778
2779         codec->spec = spec;
2780         spec->num_pins = ARRAY_SIZE(stac922x_pin_nids);
2781         spec->pin_nids = stac922x_pin_nids;
2782         spec->board_config = snd_hda_check_board_config(codec, STAC_922X_MODELS,
2783                                                         stac922x_models,
2784                                                         stac922x_cfg_tbl);
2785         if (spec->board_config == STAC_INTEL_MAC_V3) {
2786                 spec->gpio_mute = 1;
2787                 /* Intel Macs have all same PCI SSID, so we need to check
2788                  * codec SSID to distinguish the exact models
2789                  */
2790                 printk(KERN_INFO "hda_codec: STAC922x, Apple subsys_id=%x\n", codec->subsystem_id);
2791                 switch (codec->subsystem_id) {
2792
2793                 case 0x106b0800:
2794                         spec->board_config = STAC_INTEL_MAC_V1;
2795                         break;
2796                 case 0x106b0600:
2797                 case 0x106b0700:
2798                         spec->board_config = STAC_INTEL_MAC_V2;
2799                         break;
2800                 case 0x106b0e00:
2801                 case 0x106b0f00:
2802                 case 0x106b1600:
2803                 case 0x106b1700:
2804                 case 0x106b0200:
2805                 case 0x106b1e00:
2806                         spec->board_config = STAC_INTEL_MAC_V3;
2807                         break;
2808                 case 0x106b1a00:
2809                 case 0x00000100:
2810                         spec->board_config = STAC_INTEL_MAC_V4;
2811                         break;
2812                 case 0x106b0a00:
2813                 case 0x106b2200:
2814                         spec->board_config = STAC_INTEL_MAC_V5;
2815                         break;
2816                 }
2817         }
2818
2819  again:
2820         if (spec->board_config < 0) {
2821                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC922x, "
2822                         "using BIOS defaults\n");
2823                 err = stac92xx_save_bios_config_regs(codec);
2824                 if (err < 0) {
2825                         stac92xx_free(codec);
2826                         return err;
2827                 }
2828                 spec->pin_configs = spec->bios_pin_configs;
2829         } else if (stac922x_brd_tbl[spec->board_config] != NULL) {
2830                 spec->pin_configs = stac922x_brd_tbl[spec->board_config];
2831                 stac92xx_set_config_regs(codec);
2832         }
2833
2834         spec->adc_nids = stac922x_adc_nids;
2835         spec->mux_nids = stac922x_mux_nids;
2836         spec->num_muxes = ARRAY_SIZE(stac922x_mux_nids);
2837         spec->num_adcs = ARRAY_SIZE(stac922x_adc_nids);
2838         spec->num_dmics = 0;
2839
2840         spec->init = stac922x_core_init;
2841         spec->mixer = stac922x_mixer;
2842
2843         spec->multiout.dac_nids = spec->dac_nids;
2844         
2845         err = stac92xx_parse_auto_config(codec, 0x08, 0x09);
2846         if (!err) {
2847                 if (spec->board_config < 0) {
2848                         printk(KERN_WARNING "hda_codec: No auto-config is "
2849                                "available, default to model=ref\n");
2850                         spec->board_config = STAC_D945_REF;
2851                         goto again;
2852                 }
2853                 err = -EINVAL;
2854         }
2855         if (err < 0) {
2856                 stac92xx_free(codec);
2857                 return err;
2858         }
2859
2860         codec->patch_ops = stac92xx_patch_ops;
2861
2862         /* Fix Mux capture level; max to 2 */
2863         snd_hda_override_amp_caps(codec, 0x12, HDA_OUTPUT,
2864                                   (0 << AC_AMPCAP_OFFSET_SHIFT) |
2865                                   (2 << AC_AMPCAP_NUM_STEPS_SHIFT) |
2866                                   (0x27 << AC_AMPCAP_STEP_SIZE_SHIFT) |
2867                                   (0 << AC_AMPCAP_MUTE_SHIFT));
2868
2869         return 0;
2870 }
2871
2872 static int patch_stac927x(struct hda_codec *codec)
2873 {
2874         struct sigmatel_spec *spec;
2875         int err;
2876
2877         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2878         if (spec == NULL)
2879                 return -ENOMEM;
2880
2881         codec->spec = spec;
2882         spec->num_pins = ARRAY_SIZE(stac927x_pin_nids);
2883         spec->pin_nids = stac927x_pin_nids;
2884         spec->board_config = snd_hda_check_board_config(codec, STAC_927X_MODELS,
2885                                                         stac927x_models,
2886                                                         stac927x_cfg_tbl);
2887  again:
2888         if (spec->board_config < 0) {
2889                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC927x, using BIOS defaults\n");
2890                 err = stac92xx_save_bios_config_regs(codec);
2891                 if (err < 0) {
2892                         stac92xx_free(codec);
2893                         return err;
2894                 }
2895                 spec->pin_configs = spec->bios_pin_configs;
2896         } else if (stac927x_brd_tbl[spec->board_config] != NULL) {
2897                 spec->pin_configs = stac927x_brd_tbl[spec->board_config];
2898                 stac92xx_set_config_regs(codec);
2899         }
2900
2901         switch (spec->board_config) {
2902         case STAC_D965_3ST:
2903                 spec->adc_nids = stac927x_adc_nids;
2904                 spec->mux_nids = stac927x_mux_nids;
2905                 spec->num_muxes = ARRAY_SIZE(stac927x_mux_nids);
2906                 spec->num_adcs = ARRAY_SIZE(stac927x_adc_nids);
2907                 spec->init = d965_core_init;
2908                 spec->mixer = stac927x_mixer;
2909                 break;
2910         case STAC_D965_5ST:
2911                 spec->adc_nids = stac927x_adc_nids;
2912                 spec->mux_nids = stac927x_mux_nids;
2913                 spec->num_muxes = ARRAY_SIZE(stac927x_mux_nids);
2914                 spec->num_adcs = ARRAY_SIZE(stac927x_adc_nids);
2915                 spec->init = d965_core_init;
2916                 spec->mixer = stac927x_mixer;
2917                 break;
2918         default:
2919                 spec->adc_nids = stac927x_adc_nids;
2920                 spec->mux_nids = stac927x_mux_nids;
2921                 spec->num_muxes = ARRAY_SIZE(stac927x_mux_nids);
2922                 spec->num_adcs = ARRAY_SIZE(stac927x_adc_nids);
2923                 spec->init = stac927x_core_init;
2924                 spec->mixer = stac927x_mixer;
2925         }
2926
2927         switch (codec->subsystem_id) {
2928         case 0x10280242: /* STAC 9228 */
2929         case 0x102801f3:
2930         case 0x1028020A:
2931         case 0x10280209:
2932                 spec->dmic_nids = stac927x_dmic_nids;
2933                 spec->num_dmics = STAC927X_NUM_DMICS;
2934                 spec->dmux_nid = 0x1b;
2935
2936                 /* Enable DMIC0 */
2937                 stac92xx_set_config_reg(codec, 0x13, 0x90a60040);
2938
2939                 /* GPIO2 High = Enable EAPD */
2940                 spec->gpio_mask = spec->gpio_data = 0x00000004;
2941                 break;
2942         default:
2943                 spec->num_dmics = 0;
2944
2945                 /* GPIO0 High = Enable EAPD */
2946                 spec->gpio_mask = spec->gpio_data = 0x00000001;
2947         }
2948
2949         spec->multiout.dac_nids = spec->dac_nids;
2950         stac92xx_enable_gpio_mask(codec); 
2951         
2952         err = stac92xx_parse_auto_config(codec, 0x1e, 0x20);
2953         if (!err) {
2954                 if (spec->board_config < 0) {
2955                         printk(KERN_WARNING "hda_codec: No auto-config is "
2956                                "available, default to model=ref\n");
2957                         spec->board_config = STAC_D965_REF;
2958                         goto again;
2959                 }
2960                 err = -EINVAL;
2961         }
2962         if (err < 0) {
2963                 stac92xx_free(codec);
2964                 return err;
2965         }
2966
2967         codec->patch_ops = stac92xx_patch_ops;
2968
2969         return 0;
2970 }
2971
2972 static int patch_stac9205(struct hda_codec *codec)
2973 {
2974         struct sigmatel_spec *spec;
2975         int err;
2976
2977         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
2978         if (spec == NULL)
2979                 return -ENOMEM;
2980
2981         codec->spec = spec;
2982         spec->num_pins = ARRAY_SIZE(stac9205_pin_nids);
2983         spec->pin_nids = stac9205_pin_nids;
2984         spec->board_config = snd_hda_check_board_config(codec, STAC_9205_MODELS,
2985                                                         stac9205_models,
2986                                                         stac9205_cfg_tbl);
2987  again:
2988         if (spec->board_config < 0) {
2989                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9205, using BIOS defaults\n");
2990                 err = stac92xx_save_bios_config_regs(codec);
2991                 if (err < 0) {
2992                         stac92xx_free(codec);
2993                         return err;
2994                 }
2995                 spec->pin_configs = spec->bios_pin_configs;
2996         } else {
2997                 spec->pin_configs = stac9205_brd_tbl[spec->board_config];
2998                 stac92xx_set_config_regs(codec);
2999         }
3000
3001         spec->adc_nids = stac9205_adc_nids;
3002         spec->num_adcs = ARRAY_SIZE(stac9205_adc_nids);
3003         spec->mux_nids = stac9205_mux_nids;
3004         spec->num_muxes = ARRAY_SIZE(stac9205_mux_nids);
3005         spec->dmic_nids = stac9205_dmic_nids;
3006         spec->num_dmics = STAC9205_NUM_DMICS;
3007         spec->dmux_nid = 0x1d;
3008
3009         spec->init = stac9205_core_init;
3010         spec->mixer = stac9205_mixer;
3011
3012         spec->multiout.dac_nids = spec->dac_nids;
3013         
3014         switch (spec->board_config){
3015         case STAC_9205_DELL_M43:
3016                 /* Enable SPDIF in/out */
3017                 stac92xx_set_config_reg(codec, 0x1f, 0x01441030);
3018                 stac92xx_set_config_reg(codec, 0x20, 0x1c410030);
3019
3020                 spec->gpio_mask = 0x00000007; /* GPIO0-2 */
3021                 /* GPIO0 High = EAPD, GPIO1 Low = DRM,
3022                  * GPIO2 High = Headphone Mute
3023                  */
3024                 spec->gpio_data = 0x00000005;
3025                 break;
3026         default:
3027                 /* GPIO0 High = EAPD */
3028                 spec->gpio_mask = spec->gpio_data = 0x00000001;
3029                 break;
3030         }
3031
3032         stac92xx_enable_gpio_mask(codec);
3033         err = stac92xx_parse_auto_config(codec, 0x1f, 0x20);
3034         if (!err) {
3035                 if (spec->board_config < 0) {
3036                         printk(KERN_WARNING "hda_codec: No auto-config is "
3037                                "available, default to model=ref\n");
3038                         spec->board_config = STAC_9205_REF;
3039                         goto again;
3040                 }
3041                 err = -EINVAL;
3042         }
3043         if (err < 0) {
3044                 stac92xx_free(codec);
3045                 return err;
3046         }
3047
3048         codec->patch_ops = stac92xx_patch_ops;
3049
3050         return 0;
3051 }
3052
3053 /*
3054  * STAC9872 hack
3055  */
3056
3057 /* static config for Sony VAIO FE550G and Sony VAIO AR */
3058 static hda_nid_t vaio_dacs[] = { 0x2 };
3059 #define VAIO_HP_DAC     0x5
3060 static hda_nid_t vaio_adcs[] = { 0x8 /*,0x6*/ };
3061 static hda_nid_t vaio_mux_nids[] = { 0x15 };
3062
3063 static struct hda_input_mux vaio_mux = {
3064         .num_items = 3,
3065         .items = {
3066                 /* { "HP", 0x0 }, */
3067                 { "Mic Jack", 0x1 },
3068                 { "Internal Mic", 0x2 },
3069                 { "PCM", 0x3 },
3070         }
3071 };
3072
3073 static struct hda_verb vaio_init[] = {
3074         {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
3075         {0x0a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | STAC_HP_EVENT},
3076         {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
3077         {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
3078         {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
3079         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
3080         {0x15, AC_VERB_SET_CONNECT_SEL, 0x1}, /* mic-sel: 0a,0d,14,02 */
3081         {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
3082         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
3083         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
3084         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
3085         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
3086         {}
3087 };
3088
3089 static struct hda_verb vaio_ar_init[] = {
3090         {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
3091         {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
3092         {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
3093         {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
3094 /*      {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },*/ /* Optical Out */
3095         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
3096         {0x15, AC_VERB_SET_CONNECT_SEL, 0x1}, /* mic-sel: 0a,0d,14,02 */
3097         {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
3098         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
3099 /*      {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},*/ /* Optical Out */
3100         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
3101         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
3102         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
3103         {}
3104 };
3105
3106 /* bind volumes of both NID 0x02 and 0x05 */
3107 static struct hda_bind_ctls vaio_bind_master_vol = {
3108         .ops = &snd_hda_bind_vol,
3109         .values = {
3110                 HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
3111                 HDA_COMPOSE_AMP_VAL(0x05, 3, 0, HDA_OUTPUT),
3112                 0
3113         },
3114 };
3115
3116 /* bind volumes of both NID 0x02 and 0x05 */
3117 static struct hda_bind_ctls vaio_bind_master_sw = {
3118         .ops = &snd_hda_bind_sw,
3119         .values = {
3120                 HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
3121                 HDA_COMPOSE_AMP_VAL(0x05, 3, 0, HDA_OUTPUT),
3122                 0,
3123         },
3124 };
3125
3126 static struct snd_kcontrol_new vaio_mixer[] = {
3127         HDA_BIND_VOL("Master Playback Volume", &vaio_bind_master_vol),
3128         HDA_BIND_SW("Master Playback Switch", &vaio_bind_master_sw),
3129         /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
3130         HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
3131         HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
3132         {
3133                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3134                 .name = "Capture Source",
3135                 .count = 1,
3136                 .info = stac92xx_mux_enum_info,
3137                 .get = stac92xx_mux_enum_get,
3138                 .put = stac92xx_mux_enum_put,
3139         },
3140         {}
3141 };
3142
3143 static struct snd_kcontrol_new vaio_ar_mixer[] = {
3144         HDA_BIND_VOL("Master Playback Volume", &vaio_bind_master_vol),
3145         HDA_BIND_SW("Master Playback Switch", &vaio_bind_master_sw),
3146         /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
3147         HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
3148         HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
3149         /*HDA_CODEC_MUTE("Optical Out Switch", 0x10, 0, HDA_OUTPUT),
3150         HDA_CODEC_VOLUME("Optical Out Volume", 0x10, 0, HDA_OUTPUT),*/
3151         {
3152                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3153                 .name = "Capture Source",
3154                 .count = 1,
3155                 .info = stac92xx_mux_enum_info,
3156                 .get = stac92xx_mux_enum_get,
3157                 .put = stac92xx_mux_enum_put,
3158         },
3159         {}
3160 };
3161
3162 static struct hda_codec_ops stac9872_patch_ops = {
3163         .build_controls = stac92xx_build_controls,
3164         .build_pcms = stac92xx_build_pcms,
3165         .init = stac92xx_init,
3166         .free = stac92xx_free,
3167 #ifdef SND_HDA_NEEDS_RESUME
3168         .resume = stac92xx_resume,
3169 #endif
3170 };
3171
3172 static int stac9872_vaio_init(struct hda_codec *codec)
3173 {
3174         int err;
3175
3176         err = stac92xx_init(codec);
3177         if (err < 0)
3178                 return err;
3179         if (codec->patch_ops.unsol_event)
3180                 codec->patch_ops.unsol_event(codec, STAC_HP_EVENT << 26);
3181         return 0;
3182 }
3183
3184 static void stac9872_vaio_hp_detect(struct hda_codec *codec, unsigned int res)
3185 {
3186         if (get_hp_pin_presence(codec, 0x0a)) {
3187                 stac92xx_reset_pinctl(codec, 0x0f, AC_PINCTL_OUT_EN);
3188                 stac92xx_set_pinctl(codec, 0x0a, AC_PINCTL_OUT_EN);
3189         } else {
3190                 stac92xx_reset_pinctl(codec, 0x0a, AC_PINCTL_OUT_EN);
3191                 stac92xx_set_pinctl(codec, 0x0f, AC_PINCTL_OUT_EN);
3192         }
3193
3194
3195 static void stac9872_vaio_unsol_event(struct hda_codec *codec, unsigned int res)
3196 {
3197         switch (res >> 26) {
3198         case STAC_HP_EVENT:
3199                 stac9872_vaio_hp_detect(codec, res);
3200                 break;
3201         }
3202 }
3203
3204 static struct hda_codec_ops stac9872_vaio_patch_ops = {
3205         .build_controls = stac92xx_build_controls,
3206         .build_pcms = stac92xx_build_pcms,
3207         .init = stac9872_vaio_init,
3208         .free = stac92xx_free,
3209         .unsol_event = stac9872_vaio_unsol_event,
3210 #ifdef CONFIG_PM
3211         .resume = stac92xx_resume,
3212 #endif
3213 };
3214
3215 enum { /* FE and SZ series. id=0x83847661 and subsys=0x104D0700 or 104D1000. */
3216        CXD9872RD_VAIO,
3217        /* Unknown. id=0x83847662 and subsys=0x104D1200 or 104D1000. */
3218        STAC9872AK_VAIO, 
3219        /* Unknown. id=0x83847661 and subsys=0x104D1200. */
3220        STAC9872K_VAIO,
3221        /* AR Series. id=0x83847664 and subsys=104D1300 */
3222        CXD9872AKD_VAIO,
3223        STAC_9872_MODELS,
3224 };
3225
3226 static const char *stac9872_models[STAC_9872_MODELS] = {
3227         [CXD9872RD_VAIO]        = "vaio",
3228         [CXD9872AKD_VAIO]       = "vaio-ar",
3229 };
3230
3231 static struct snd_pci_quirk stac9872_cfg_tbl[] = {
3232         SND_PCI_QUIRK(0x104d, 0x81e6, "Sony VAIO F/S", CXD9872RD_VAIO),
3233         SND_PCI_QUIRK(0x104d, 0x81ef, "Sony VAIO F/S", CXD9872RD_VAIO),
3234         SND_PCI_QUIRK(0x104d, 0x81fd, "Sony VAIO AR", CXD9872AKD_VAIO),
3235         SND_PCI_QUIRK(0x104d, 0x8205, "Sony VAIO AR", CXD9872AKD_VAIO),
3236         {}
3237 };
3238
3239 static int patch_stac9872(struct hda_codec *codec)
3240 {
3241         struct sigmatel_spec *spec;
3242         int board_config;
3243
3244         board_config = snd_hda_check_board_config(codec, STAC_9872_MODELS,
3245                                                   stac9872_models,
3246                                                   stac9872_cfg_tbl);
3247         if (board_config < 0)
3248                 /* unknown config, let generic-parser do its job... */
3249                 return snd_hda_parse_generic_codec(codec);
3250         
3251         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
3252         if (spec == NULL)
3253                 return -ENOMEM;
3254
3255         codec->spec = spec;
3256         switch (board_config) {
3257         case CXD9872RD_VAIO:
3258         case STAC9872AK_VAIO:
3259         case STAC9872K_VAIO:
3260                 spec->mixer = vaio_mixer;
3261                 spec->init = vaio_init;
3262                 spec->multiout.max_channels = 2;
3263                 spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
3264                 spec->multiout.dac_nids = vaio_dacs;
3265                 spec->multiout.hp_nid = VAIO_HP_DAC;
3266                 spec->num_adcs = ARRAY_SIZE(vaio_adcs);
3267                 spec->adc_nids = vaio_adcs;
3268                 spec->input_mux = &vaio_mux;
3269                 spec->mux_nids = vaio_mux_nids;
3270                 codec->patch_ops = stac9872_vaio_patch_ops;
3271                 break;
3272         
3273         case CXD9872AKD_VAIO:
3274                 spec->mixer = vaio_ar_mixer;
3275                 spec->init = vaio_ar_init;
3276                 spec->multiout.max_channels = 2;
3277                 spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
3278                 spec->multiout.dac_nids = vaio_dacs;
3279                 spec->multiout.hp_nid = VAIO_HP_DAC;
3280                 spec->num_adcs = ARRAY_SIZE(vaio_adcs);
3281                 spec->adc_nids = vaio_adcs;
3282                 spec->input_mux = &vaio_mux;
3283                 spec->mux_nids = vaio_mux_nids;
3284                 codec->patch_ops = stac9872_patch_ops;
3285                 break;
3286         }
3287
3288         return 0;
3289 }
3290
3291
3292 /*
3293  * patch entries
3294  */
3295 struct hda_codec_preset snd_hda_preset_sigmatel[] = {
3296         { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
3297         { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
3298         { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
3299         { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
3300         { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
3301         { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
3302         { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
3303         { .id = 0x83847618, .name = "STAC9227", .patch = patch_stac927x },
3304         { .id = 0x83847619, .name = "STAC9227", .patch = patch_stac927x },
3305         { .id = 0x83847616, .name = "STAC9228", .patch = patch_stac927x },
3306         { .id = 0x83847617, .name = "STAC9228", .patch = patch_stac927x },
3307         { .id = 0x83847614, .name = "STAC9229", .patch = patch_stac927x },
3308         { .id = 0x83847615, .name = "STAC9229", .patch = patch_stac927x },
3309         { .id = 0x83847620, .name = "STAC9274", .patch = patch_stac927x },
3310         { .id = 0x83847621, .name = "STAC9274D", .patch = patch_stac927x },
3311         { .id = 0x83847622, .name = "STAC9273X", .patch = patch_stac927x },
3312         { .id = 0x83847623, .name = "STAC9273D", .patch = patch_stac927x },
3313         { .id = 0x83847624, .name = "STAC9272X", .patch = patch_stac927x },
3314         { .id = 0x83847625, .name = "STAC9272D", .patch = patch_stac927x },
3315         { .id = 0x83847626, .name = "STAC9271X", .patch = patch_stac927x },
3316         { .id = 0x83847627, .name = "STAC9271D", .patch = patch_stac927x },
3317         { .id = 0x83847628, .name = "STAC9274X5NH", .patch = patch_stac927x },
3318         { .id = 0x83847629, .name = "STAC9274D5NH", .patch = patch_stac927x },
3319         { .id = 0x83847632, .name = "STAC9202",  .patch = patch_stac925x },
3320         { .id = 0x83847633, .name = "STAC9202D", .patch = patch_stac925x },
3321         { .id = 0x83847634, .name = "STAC9250", .patch = patch_stac925x },
3322         { .id = 0x83847635, .name = "STAC9250D", .patch = patch_stac925x },
3323         { .id = 0x83847636, .name = "STAC9251", .patch = patch_stac925x },
3324         { .id = 0x83847637, .name = "STAC9250D", .patch = patch_stac925x },
3325         /* The following does not take into account .id=0x83847661 when subsys =
3326          * 104D0C00 which is STAC9225s. Because of this, some SZ Notebooks are
3327          * currently not fully supported.
3328          */
3329         { .id = 0x83847661, .name = "CXD9872RD/K", .patch = patch_stac9872 },
3330         { .id = 0x83847662, .name = "STAC9872AK", .patch = patch_stac9872 },
3331         { .id = 0x83847664, .name = "CXD9872AKD", .patch = patch_stac9872 },
3332         { .id = 0x838476a0, .name = "STAC9205", .patch = patch_stac9205 },
3333         { .id = 0x838476a1, .name = "STAC9205D", .patch = patch_stac9205 },
3334         { .id = 0x838476a2, .name = "STAC9204", .patch = patch_stac9205 },
3335         { .id = 0x838476a3, .name = "STAC9204D", .patch = patch_stac9205 },
3336         { .id = 0x838476a4, .name = "STAC9255", .patch = patch_stac9205 },
3337         { .id = 0x838476a5, .name = "STAC9255D", .patch = patch_stac9205 },
3338         { .id = 0x838476a6, .name = "STAC9254", .patch = patch_stac9205 },
3339         { .id = 0x838476a7, .name = "STAC9254D", .patch = patch_stac9205 },
3340         { .id = 0x111d76b0, .name = "92HD71BXX", .patch = patch_stac92hd71bxx },
3341         {} /* terminator */
3342 };