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