[ALSA] hda-codec - Small clean up and fixes
[safe/jmp/linux-2.6] / sound / pci / hda / patch_realtek.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * HD audio interface patch for ALC 260/880/882 codecs
5  *
6  * Copyright (c) 2004 Kailang Yang <kailang@realtek.com.tw>
7  *                    PeiSen Hou <pshou@realtek.com.tw>
8  *                    Takashi Iwai <tiwai@suse.de>
9  *
10  *  This driver is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This driver is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  */
24
25 #include <sound/driver.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/pci.h>
30 #include <sound/core.h>
31 #include "hda_codec.h"
32 #include "hda_local.h"
33
34
35 /* ALC880 board config type */
36 enum {
37         ALC880_3ST,
38         ALC880_3ST_DIG,
39         ALC880_5ST,
40         ALC880_5ST_DIG,
41         ALC880_W810,
42         ALC880_Z71V,
43         ALC880_6ST,
44         ALC880_6ST_DIG,
45         ALC880_F1734,
46         ALC880_ASUS,
47         ALC880_ASUS_DIG,
48         ALC880_ASUS_W1V,
49         ALC880_ASUS_DIG2,
50         ALC880_UNIWILL_DIG,
51         ALC880_CLEVO,
52         ALC880_TCL_S700,
53 #ifdef CONFIG_SND_DEBUG
54         ALC880_TEST,
55 #endif
56         ALC880_AUTO,
57         ALC880_MODEL_LAST /* last tag */
58 };
59
60 /* ALC260 models */
61 enum {
62         ALC260_BASIC,
63         ALC260_HP,
64         ALC260_HP_3013,
65         ALC260_FUJITSU_S702X,
66         ALC260_AUTO,
67         ALC260_MODEL_LAST /* last tag */
68 };
69
70 /* ALC262 models */
71 enum {
72         ALC262_BASIC,
73         ALC262_AUTO,
74         ALC262_MODEL_LAST /* last tag */
75 };
76
77 /* ALC861 models */
78 enum {
79         ALC861_3ST,
80         ALC861_3ST_DIG,
81         ALC861_6ST_DIG,
82         ALC861_AUTO,
83         ALC861_MODEL_LAST,
84 };
85
86 /* ALC882 models */
87 enum {
88         ALC882_3ST_DIG,
89         ALC882_6ST_DIG,
90         ALC882_AUTO,
91         ALC882_MODEL_LAST,
92 };
93
94 /* for GPIO Poll */
95 #define GPIO_MASK       0x03
96
97 struct alc_spec {
98         /* codec parameterization */
99         struct snd_kcontrol_new *mixers[5];     /* mixer arrays */
100         unsigned int num_mixers;
101
102         const struct hda_verb *init_verbs[5];   /* initialization verbs
103                                                  * don't forget NULL termination!
104                                                  */
105         unsigned int num_init_verbs;
106
107         char *stream_name_analog;       /* analog PCM stream */
108         struct hda_pcm_stream *stream_analog_playback;
109         struct hda_pcm_stream *stream_analog_capture;
110
111         char *stream_name_digital;      /* digital PCM stream */ 
112         struct hda_pcm_stream *stream_digital_playback;
113         struct hda_pcm_stream *stream_digital_capture;
114
115         /* playback */
116         struct hda_multi_out multiout;  /* playback set-up
117                                          * max_channels, dacs must be set
118                                          * dig_out_nid and hp_nid are optional
119                                          */
120
121         /* capture */
122         unsigned int num_adc_nids;
123         hda_nid_t *adc_nids;
124         hda_nid_t dig_in_nid;           /* digital-in NID; optional */
125
126         /* capture source */
127         const struct hda_input_mux *input_mux;
128         unsigned int cur_mux[3];
129
130         /* channel model */
131         const struct hda_channel_mode *channel_mode;
132         int num_channel_mode;
133
134         /* PCM information */
135         struct hda_pcm pcm_rec[2];      /* used in alc_build_pcms() */
136
137         /* dynamic controls, init_verbs and input_mux */
138         struct auto_pin_cfg autocfg;
139         unsigned int num_kctl_alloc, num_kctl_used;
140         struct snd_kcontrol_new *kctl_alloc;
141         struct hda_input_mux private_imux;
142         hda_nid_t private_dac_nids[5];
143 };
144
145 /*
146  * configuration template - to be copied to the spec instance
147  */
148 struct alc_config_preset {
149         struct snd_kcontrol_new *mixers[5]; /* should be identical size with spec */
150         const struct hda_verb *init_verbs[5];
151         unsigned int num_dacs;
152         hda_nid_t *dac_nids;
153         hda_nid_t dig_out_nid;          /* optional */
154         hda_nid_t hp_nid;               /* optional */
155         unsigned int num_adc_nids;
156         hda_nid_t *adc_nids;
157         hda_nid_t dig_in_nid;
158         unsigned int num_channel_mode;
159         const struct hda_channel_mode *channel_mode;
160         const struct hda_input_mux *input_mux;
161 };
162
163
164 /*
165  * input MUX handling
166  */
167 static int alc_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
168 {
169         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
170         struct alc_spec *spec = codec->spec;
171         return snd_hda_input_mux_info(spec->input_mux, uinfo);
172 }
173
174 static int alc_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
175 {
176         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
177         struct alc_spec *spec = codec->spec;
178         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
179
180         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
181         return 0;
182 }
183
184 static int alc_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
185 {
186         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
187         struct alc_spec *spec = codec->spec;
188         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
189         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
190                                      spec->adc_nids[adc_idx], &spec->cur_mux[adc_idx]);
191 }
192
193
194 /*
195  * channel mode setting
196  */
197 static int alc_ch_mode_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
198 {
199         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
200         struct alc_spec *spec = codec->spec;
201         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
202                                     spec->num_channel_mode);
203 }
204
205 static int alc_ch_mode_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
206 {
207         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
208         struct alc_spec *spec = codec->spec;
209         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
210                                    spec->num_channel_mode, spec->multiout.max_channels);
211 }
212
213 static int alc_ch_mode_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
214 {
215         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
216         struct alc_spec *spec = codec->spec;
217         return snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
218                                    spec->num_channel_mode, &spec->multiout.max_channels);
219 }
220
221
222 /*
223  * Control of pin widget settings via the mixer.  Only boolean settings are
224  * supported, so VrefEn can't be controlled using these functions as they
225  * stand.
226  */
227 static int alc_pinctl_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
228 {
229         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
230         uinfo->count = 1;
231         uinfo->value.integer.min = 0;
232         uinfo->value.integer.max = 1;
233         return 0;
234 }
235
236 static int alc_pinctl_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
237 {
238         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
239         hda_nid_t nid = kcontrol->private_value & 0xffff;
240         long mask = (kcontrol->private_value >> 16) & 0xff;
241         long *valp = ucontrol->value.integer.value;
242
243         *valp = 0;
244         if (snd_hda_codec_read(codec,nid,0,AC_VERB_GET_PIN_WIDGET_CONTROL,0x00) & mask)
245                 *valp = 1;
246         return 0;
247 }
248
249 static int alc_pinctl_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
250 {
251         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
252         hda_nid_t nid = kcontrol->private_value & 0xffff;
253         long mask = (kcontrol->private_value >> 16) & 0xff;
254         long *valp = ucontrol->value.integer.value;
255         unsigned int pinctl = snd_hda_codec_read(codec,nid,0,AC_VERB_GET_PIN_WIDGET_CONTROL,0x00);
256         int change = ((pinctl & mask)!=0) != *valp;
257
258         if (change)
259                 snd_hda_codec_write(codec,nid,0,AC_VERB_SET_PIN_WIDGET_CONTROL,
260                         *valp?(pinctl|mask):(pinctl&~mask));
261         return change;
262 }
263
264 #define ALC_PINCTL_SWITCH(xname, nid, mask) \
265         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
266           .info = alc_pinctl_switch_info, \
267           .get = alc_pinctl_switch_get, \
268           .put = alc_pinctl_switch_put, \
269           .private_value = (nid) | (mask<<16) }
270
271
272 /*
273  * set up from the preset table
274  */
275 static void setup_preset(struct alc_spec *spec, const struct alc_config_preset *preset)
276 {
277         int i;
278
279         for (i = 0; i < ARRAY_SIZE(preset->mixers) && preset->mixers[i]; i++)
280                 spec->mixers[spec->num_mixers++] = preset->mixers[i];
281         for (i = 0; i < ARRAY_SIZE(preset->init_verbs) && preset->init_verbs[i]; i++)
282                 spec->init_verbs[spec->num_init_verbs++] = preset->init_verbs[i];
283         
284         spec->channel_mode = preset->channel_mode;
285         spec->num_channel_mode = preset->num_channel_mode;
286
287         spec->multiout.max_channels = spec->channel_mode[0].channels;
288
289         spec->multiout.num_dacs = preset->num_dacs;
290         spec->multiout.dac_nids = preset->dac_nids;
291         spec->multiout.dig_out_nid = preset->dig_out_nid;
292         spec->multiout.hp_nid = preset->hp_nid;
293         
294         spec->input_mux = preset->input_mux;
295
296         spec->num_adc_nids = preset->num_adc_nids;
297         spec->adc_nids = preset->adc_nids;
298         spec->dig_in_nid = preset->dig_in_nid;
299 }
300
301 /*
302  * ALC880 3-stack model
303  *
304  * DAC: Front = 0x02 (0x0c), Surr = 0x05 (0x0f), CLFE = 0x04 (0x0e)
305  * Pin assignment: Front = 0x14, Line-In/Surr = 0x1a, Mic/CLFE = 0x18, F-Mic = 0x1b
306  *                 HP = 0x19
307  */
308
309 static hda_nid_t alc880_dac_nids[4] = {
310         /* front, rear, clfe, rear_surr */
311         0x02, 0x05, 0x04, 0x03
312 };
313
314 static hda_nid_t alc880_adc_nids[3] = {
315         /* ADC0-2 */
316         0x07, 0x08, 0x09,
317 };
318
319 /* The datasheet says the node 0x07 is connected from inputs,
320  * but it shows zero connection in the real implementation on some devices.
321  * Note: this is a 915GAV bug, fixed on 915GLV
322  */
323 static hda_nid_t alc880_adc_nids_alt[2] = {
324         /* ADC1-2 */
325         0x08, 0x09,
326 };
327
328 #define ALC880_DIGOUT_NID       0x06
329 #define ALC880_DIGIN_NID        0x0a
330
331 static struct hda_input_mux alc880_capture_source = {
332         .num_items = 4,
333         .items = {
334                 { "Mic", 0x0 },
335                 { "Front Mic", 0x3 },
336                 { "Line", 0x2 },
337                 { "CD", 0x4 },
338         },
339 };
340
341 /* channel source setting (2/6 channel selection for 3-stack) */
342 /* 2ch mode */
343 static struct hda_verb alc880_threestack_ch2_init[] = {
344         /* set line-in to input, mute it */
345         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
346         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
347         /* set mic-in to input vref 80%, mute it */
348         { 0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
349         { 0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
350         { } /* end */
351 };
352
353 /* 6ch mode */
354 static struct hda_verb alc880_threestack_ch6_init[] = {
355         /* set line-in to output, unmute it */
356         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
357         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
358         /* set mic-in to output, unmute it */
359         { 0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
360         { 0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
361         { } /* end */
362 };
363
364 static struct hda_channel_mode alc880_threestack_modes[2] = {
365         { 2, alc880_threestack_ch2_init },
366         { 6, alc880_threestack_ch6_init },
367 };
368
369 static struct snd_kcontrol_new alc880_three_stack_mixer[] = {
370         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
371         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
372         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
373         HDA_BIND_MUTE("Surround Playback Switch", 0x0f, 2, HDA_INPUT),
374         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
375         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
376         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
377         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
378         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
379         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
380         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
381         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
382         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
383         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
384         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x0b, 0x3, HDA_INPUT),
385         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x0b, 0x3, HDA_INPUT),
386         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x0b, 0x05, HDA_INPUT),
387         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x0b, 0x05, HDA_INPUT),
388         HDA_CODEC_MUTE("Headphone Playback Switch", 0x19, 0x0, HDA_OUTPUT),
389         {
390                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
391                 .name = "Channel Mode",
392                 .info = alc_ch_mode_info,
393                 .get = alc_ch_mode_get,
394                 .put = alc_ch_mode_put,
395         },
396         { } /* end */
397 };
398
399 /* capture mixer elements */
400 static struct snd_kcontrol_new alc880_capture_mixer[] = {
401         HDA_CODEC_VOLUME("Capture Volume", 0x07, 0x0, HDA_INPUT),
402         HDA_CODEC_MUTE("Capture Switch", 0x07, 0x0, HDA_INPUT),
403         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x08, 0x0, HDA_INPUT),
404         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x08, 0x0, HDA_INPUT),
405         HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x09, 0x0, HDA_INPUT),
406         HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x09, 0x0, HDA_INPUT),
407         {
408                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
409                 /* The multiple "Capture Source" controls confuse alsamixer
410                  * So call somewhat different..
411                  * FIXME: the controls appear in the "playback" view!
412                  */
413                 /* .name = "Capture Source", */
414                 .name = "Input Source",
415                 .count = 3,
416                 .info = alc_mux_enum_info,
417                 .get = alc_mux_enum_get,
418                 .put = alc_mux_enum_put,
419         },
420         { } /* end */
421 };
422
423 /* capture mixer elements (in case NID 0x07 not available) */
424 static struct snd_kcontrol_new alc880_capture_alt_mixer[] = {
425         HDA_CODEC_VOLUME("Capture Volume", 0x08, 0x0, HDA_INPUT),
426         HDA_CODEC_MUTE("Capture Switch", 0x08, 0x0, HDA_INPUT),
427         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x09, 0x0, HDA_INPUT),
428         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x09, 0x0, HDA_INPUT),
429         {
430                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
431                 /* The multiple "Capture Source" controls confuse alsamixer
432                  * So call somewhat different..
433                  * FIXME: the controls appear in the "playback" view!
434                  */
435                 /* .name = "Capture Source", */
436                 .name = "Input Source",
437                 .count = 2,
438                 .info = alc_mux_enum_info,
439                 .get = alc_mux_enum_get,
440                 .put = alc_mux_enum_put,
441         },
442         { } /* end */
443 };
444
445
446
447 /*
448  * ALC880 5-stack model
449  *
450  * DAC: Front = 0x02 (0x0c), Surr = 0x05 (0x0f), CLFE = 0x04 (0x0d), Side = 0x02 (0xd)
451  * Pin assignment: Front = 0x14, Surr = 0x17, CLFE = 0x16
452  *                 Line-In/Side = 0x1a, Mic = 0x18, F-Mic = 0x1b, HP = 0x19
453  */
454
455 /* additional mixers to alc880_three_stack_mixer */
456 static struct snd_kcontrol_new alc880_five_stack_mixer[] = {
457         HDA_CODEC_VOLUME("Side Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
458         HDA_BIND_MUTE("Side Playback Switch", 0x0d, 2, HDA_INPUT),
459         { } /* end */
460 };
461
462 /* channel source setting (6/8 channel selection for 5-stack) */
463 /* 6ch mode */
464 static struct hda_verb alc880_fivestack_ch6_init[] = {
465         /* set line-in to input, mute it */
466         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
467         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
468         { } /* end */
469 };
470
471 /* 8ch mode */
472 static struct hda_verb alc880_fivestack_ch8_init[] = {
473         /* set line-in to output, unmute it */
474         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
475         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
476         { } /* end */
477 };
478
479 static struct hda_channel_mode alc880_fivestack_modes[2] = {
480         { 6, alc880_fivestack_ch6_init },
481         { 8, alc880_fivestack_ch8_init },
482 };
483
484
485 /*
486  * ALC880 6-stack model
487  *
488  * DAC: Front = 0x02 (0x0c), Surr = 0x03 (0x0d), CLFE = 0x04 (0x0e), Side = 0x05 (0x0f)
489  * Pin assignment: Front = 0x14, Surr = 0x15, CLFE = 0x16, Side = 0x17,
490  *   Mic = 0x18, F-Mic = 0x19, Line = 0x1a, HP = 0x1b
491  */
492
493 static hda_nid_t alc880_6st_dac_nids[4] = {
494         /* front, rear, clfe, rear_surr */
495         0x02, 0x03, 0x04, 0x05
496 };      
497
498 static struct hda_input_mux alc880_6stack_capture_source = {
499         .num_items = 4,
500         .items = {
501                 { "Mic", 0x0 },
502                 { "Front Mic", 0x1 },
503                 { "Line", 0x2 },
504                 { "CD", 0x4 },
505         },
506 };
507
508 /* fixed 8-channels */
509 static struct hda_channel_mode alc880_sixstack_modes[1] = {
510         { 8, NULL },
511 };
512
513 static struct snd_kcontrol_new alc880_six_stack_mixer[] = {
514         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
515         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
516         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
517         HDA_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
518         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
519         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
520         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
521         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
522         HDA_CODEC_VOLUME("Side Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
523         HDA_BIND_MUTE("Side Playback Switch", 0x0f, 2, HDA_INPUT),
524         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
525         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
526         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
527         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
528         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
529         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
530         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x0b, 0x1, HDA_INPUT),
531         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x0b, 0x1, HDA_INPUT),
532         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x0b, 0x05, HDA_INPUT),
533         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x0b, 0x05, HDA_INPUT),
534         {
535                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
536                 .name = "Channel Mode",
537                 .info = alc_ch_mode_info,
538                 .get = alc_ch_mode_get,
539                 .put = alc_ch_mode_put,
540         },
541         { } /* end */
542 };
543
544
545 /*
546  * ALC880 W810 model
547  *
548  * W810 has rear IO for:
549  * Front (DAC 02)
550  * Surround (DAC 03)
551  * Center/LFE (DAC 04)
552  * Digital out (06)
553  *
554  * The system also has a pair of internal speakers, and a headphone jack.
555  * These are both connected to Line2 on the codec, hence to DAC 02.
556  * 
557  * There is a variable resistor to control the speaker or headphone
558  * volume. This is a hardware-only device without a software API.
559  *
560  * Plugging headphones in will disable the internal speakers. This is
561  * implemented in hardware, not via the driver using jack sense. In
562  * a similar fashion, plugging into the rear socket marked "front" will
563  * disable both the speakers and headphones.
564  *
565  * For input, there's a microphone jack, and an "audio in" jack.
566  * These may not do anything useful with this driver yet, because I
567  * haven't setup any initialization verbs for these yet...
568  */
569
570 static hda_nid_t alc880_w810_dac_nids[3] = {
571         /* front, rear/surround, clfe */
572         0x02, 0x03, 0x04
573 };
574
575 /* fixed 6 channels */
576 static struct hda_channel_mode alc880_w810_modes[1] = {
577         { 6, NULL }
578 };
579
580 /* Pin assignment: Front = 0x14, Surr = 0x15, CLFE = 0x16, HP = 0x1b */
581 static struct snd_kcontrol_new alc880_w810_base_mixer[] = {
582         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
583         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
584         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
585         HDA_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
586         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
587         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
588         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
589         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
590         HDA_CODEC_MUTE("Headphone Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
591         { } /* end */
592 };
593
594
595 /*
596  * Z710V model
597  *
598  * DAC: Front = 0x02 (0x0c), HP = 0x03 (0x0d)
599  * Pin assignment: Front = 0x14, HP = 0x15, Mic = 0x18, Mic2 = 0x19(?), Line = 0x1a
600  */
601
602 static hda_nid_t alc880_z71v_dac_nids[1] = {
603         0x02
604 };
605 #define ALC880_Z71V_HP_DAC      0x03
606
607 /* fixed 2 channels */
608 static struct hda_channel_mode alc880_2_jack_modes[1] = {
609         { 2, NULL }
610 };
611
612 static struct snd_kcontrol_new alc880_z71v_mixer[] = {
613         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
614         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
615         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
616         HDA_BIND_MUTE("Headphone Playback Switch", 0x0d, 2, HDA_INPUT),
617         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
618         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
619         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
620         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
621         { } /* end */
622 };
623
624
625 /* FIXME! */
626 /*
627  * ALC880 F1734 model
628  *
629  * DAC: HP = 0x02 (0x0c), Front = 0x03 (0x0d)
630  * Pin assignment: HP = 0x14, Front = 0x15, Mic = 0x18
631  */
632
633 static hda_nid_t alc880_f1734_dac_nids[1] = {
634         0x03
635 };
636 #define ALC880_F1734_HP_DAC     0x02
637
638 static struct snd_kcontrol_new alc880_f1734_mixer[] = {
639         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
640         HDA_BIND_MUTE("Headphone Playback Switch", 0x0c, 2, HDA_INPUT),
641         HDA_CODEC_VOLUME("Internal Speaker Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
642         HDA_BIND_MUTE("Internal Speaker Playback Switch", 0x0d, 2, HDA_INPUT),
643         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
644         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
645         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
646         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
647         { } /* end */
648 };
649
650
651 /* FIXME! */
652 /*
653  * ALC880 ASUS model
654  *
655  * DAC: HP/Front = 0x02 (0x0c), Surr = 0x03 (0x0d), CLFE = 0x04 (0x0e)
656  * Pin assignment: HP/Front = 0x14, Surr = 0x15, CLFE = 0x16,
657  *  Mic = 0x18, Line = 0x1a
658  */
659
660 #define alc880_asus_dac_nids    alc880_w810_dac_nids    /* identical with w810 */
661 #define alc880_asus_modes       alc880_threestack_modes /* 2/6 channel mode */
662
663 static struct snd_kcontrol_new alc880_asus_mixer[] = {
664         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
665         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
666         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
667         HDA_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
668         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
669         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
670         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
671         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
672         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
673         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
674         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
675         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
676         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
677         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
678         {
679                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
680                 .name = "Channel Mode",
681                 .info = alc_ch_mode_info,
682                 .get = alc_ch_mode_get,
683                 .put = alc_ch_mode_put,
684         },
685         { } /* end */
686 };
687
688 /* FIXME! */
689 /*
690  * ALC880 ASUS W1V model
691  *
692  * DAC: HP/Front = 0x02 (0x0c), Surr = 0x03 (0x0d), CLFE = 0x04 (0x0e)
693  * Pin assignment: HP/Front = 0x14, Surr = 0x15, CLFE = 0x16,
694  *  Mic = 0x18, Line = 0x1a, Line2 = 0x1b
695  */
696
697 /* additional mixers to alc880_asus_mixer */
698 static struct snd_kcontrol_new alc880_asus_w1v_mixer[] = {
699         HDA_CODEC_VOLUME("Line2 Playback Volume", 0x0b, 0x03, HDA_INPUT),
700         HDA_CODEC_MUTE("Line2 Playback Switch", 0x0b, 0x03, HDA_INPUT),
701         { } /* end */
702 };
703
704 /* additional mixers to alc880_asus_mixer */
705 static struct snd_kcontrol_new alc880_pcbeep_mixer[] = {
706         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x0b, 0x05, HDA_INPUT),
707         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x0b, 0x05, HDA_INPUT),
708         { } /* end */
709 };
710
711 /* TCL S700 */
712 static struct snd_kcontrol_new alc880_tcl_s700_mixer[] = {
713         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
714         HDA_CODEC_MUTE("Front Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
715         HDA_CODEC_MUTE("Headphone Playback Switch", 0x14, 0x0, HDA_OUTPUT),
716         HDA_CODEC_VOLUME("CD Playback Volume", 0x0B, 0x04, HDA_INPUT),
717         HDA_CODEC_MUTE("CD Playback Switch", 0x0B, 0x04, HDA_INPUT),
718         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0B, 0x0, HDA_INPUT),
719         HDA_CODEC_MUTE("Mic Playback Switch", 0x0B, 0x0, HDA_INPUT),
720         HDA_CODEC_VOLUME("Capture Volume", 0x08, 0x0, HDA_INPUT),
721         HDA_CODEC_MUTE("Capture Switch", 0x08, 0x0, HDA_INPUT),
722         {
723                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
724                 /* The multiple "Capture Source" controls confuse alsamixer
725                  * So call somewhat different..
726                  * FIXME: the controls appear in the "playback" view!
727                  */
728                 /* .name = "Capture Source", */
729                 .name = "Input Source",
730                 .count = 1,
731                 .info = alc_mux_enum_info,
732                 .get = alc_mux_enum_get,
733                 .put = alc_mux_enum_put,
734         },
735         { } /* end */
736 };
737
738 /*
739  * build control elements
740  */
741 static int alc_build_controls(struct hda_codec *codec)
742 {
743         struct alc_spec *spec = codec->spec;
744         int err;
745         int i;
746
747         for (i = 0; i < spec->num_mixers; i++) {
748                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
749                 if (err < 0)
750                         return err;
751         }
752
753         if (spec->multiout.dig_out_nid) {
754                 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
755                 if (err < 0)
756                         return err;
757         }
758         if (spec->dig_in_nid) {
759                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
760                 if (err < 0)
761                         return err;
762         }
763         return 0;
764 }
765
766
767 /*
768  * initialize the codec volumes, etc
769  */
770
771 /*
772  * generic initialization of ADC, input mixers and output mixers
773  */
774 static struct hda_verb alc880_volume_init_verbs[] = {
775         /*
776          * Unmute ADC0-2 and set the default input to mic-in
777          */
778         {0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
779         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
780         {0x08, AC_VERB_SET_CONNECT_SEL, 0x00},
781         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
782         {0x09, AC_VERB_SET_CONNECT_SEL, 0x00},
783         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
784
785         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
786          * mixer widget
787          * Note: PASD motherboards uses the Line In 2 as the input for front panel
788          * mic (mic 2)
789          */
790         /* Amp Indices: Mic1 = 0, Mic2 = 1, Line1 = 2, Line2 = 3, CD = 4 */
791         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
792         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
793         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
794         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
795         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
796
797         /*
798          * Set up output mixers (0x0c - 0x0f)
799          */
800         /* set vol=0 to output mixers */
801         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
802         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
803         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
804         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
805         /* set up input amps for analog loopback */
806         /* Amp Indices: DAC = 0, mixer = 1 */
807         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
808         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
809         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
810         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
811         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
812         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
813         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
814         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
815
816         { }
817 };
818
819 /*
820  * 3-stack pin configuration:
821  * front = 0x14, mic/clfe = 0x18, HP = 0x19, line/surr = 0x1a, f-mic = 0x1b
822  */
823 static struct hda_verb alc880_pin_3stack_init_verbs[] = {
824         /*
825          * preset connection lists of input pins
826          * 0 = front, 1 = rear_surr, 2 = CLFE, 3 = surround
827          */
828         {0x10, AC_VERB_SET_CONNECT_SEL, 0x02}, /* mic/clfe */
829         {0x11, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
830         {0x12, AC_VERB_SET_CONNECT_SEL, 0x03}, /* line/surround */
831
832         /*
833          * Set pin mode and muting
834          */
835         /* set front pin widgets 0x14 for output */
836         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
837         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
838         /* Mic1 (rear panel) pin widget for input and vref at 80% */
839         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
840         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
841         /* Mic2 (as headphone out) for HP output */
842         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
843         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
844         /* Line In pin widget for input */
845         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
846         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
847         /* Line2 (as front mic) pin widget for input and vref at 80% */
848         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
849         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
850         /* CD pin widget for input */
851         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
852
853         { }
854 };
855
856 /*
857  * 5-stack pin configuration:
858  * front = 0x14, surround = 0x17, clfe = 0x16, mic = 0x18, HP = 0x19,
859  * line-in/side = 0x1a, f-mic = 0x1b
860  */
861 static struct hda_verb alc880_pin_5stack_init_verbs[] = {
862         /*
863          * preset connection lists of input pins
864          * 0 = front, 1 = rear_surr, 2 = CLFE, 3 = surround
865          */
866         {0x11, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
867         {0x12, AC_VERB_SET_CONNECT_SEL, 0x01}, /* line/side */
868
869         /*
870          * Set pin mode and muting
871          */
872         /* set pin widgets 0x14-0x17 for output */
873         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
874         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
875         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
876         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
877         /* unmute pins for output (no gain on this amp) */
878         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
879         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
880         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
881         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
882
883         /* Mic1 (rear panel) pin widget for input and vref at 80% */
884         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
885         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
886         /* Mic2 (as headphone out) for HP output */
887         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
888         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
889         /* Line In pin widget for input */
890         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
891         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
892         /* Line2 (as front mic) pin widget for input and vref at 80% */
893         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
894         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
895         /* CD pin widget for input */
896         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
897
898         { }
899 };
900
901 /*
902  * W810 pin configuration:
903  * front = 0x14, surround = 0x15, clfe = 0x16, HP = 0x1b
904  */
905 static struct hda_verb alc880_pin_w810_init_verbs[] = {
906         /* hphone/speaker input selector: front DAC */
907         {0x13, AC_VERB_SET_CONNECT_SEL, 0x0},
908
909         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
910         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
911         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
912         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
913         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
914         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
915
916         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
917         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
918
919         { }
920 };
921
922 /*
923  * Z71V pin configuration:
924  * Speaker-out = 0x14, HP = 0x15, Mic = 0x18, Line-in = 0x1a, Mic2 = 0x1b (?)
925  */
926 static struct hda_verb alc880_pin_z71v_init_verbs[] = {
927         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
928         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
929         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
930         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
931
932         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
933         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
934         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
935         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
936
937         { }
938 };
939
940 /*
941  * 6-stack pin configuration:
942  * front = 0x14, surr = 0x15, clfe = 0x16, side = 0x17, mic = 0x18, f-mic = 0x19,
943  * line = 0x1a, HP = 0x1b
944  */
945 static struct hda_verb alc880_pin_6stack_init_verbs[] = {
946         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
947
948         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
949         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
950         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
951         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
952         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
953         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
954         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
955         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
956
957         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
958         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
959         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
960         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
961         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
962         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
963         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
964         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
965         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
966         
967         { }
968 };
969
970 /* FIXME! */
971 /*
972  * F1734 pin configuration:
973  * HP = 0x14, speaker-out = 0x15, mic = 0x18
974  */
975 static struct hda_verb alc880_pin_f1734_init_verbs[] = {
976         {0x10, AC_VERB_SET_CONNECT_SEL, 0x02},
977         {0x11, AC_VERB_SET_CONNECT_SEL, 0x00},
978         {0x12, AC_VERB_SET_CONNECT_SEL, 0x01},
979         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00},
980
981         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
982         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
983         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
984         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
985
986         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
987         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
988         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
989         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
990         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
991         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
992         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
993         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
994         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
995
996         { }
997 };
998
999 /* FIXME! */
1000 /*
1001  * ASUS pin configuration:
1002  * HP/front = 0x14, surr = 0x15, clfe = 0x16, mic = 0x18, line = 0x1a
1003  */
1004 static struct hda_verb alc880_pin_asus_init_verbs[] = {
1005         {0x10, AC_VERB_SET_CONNECT_SEL, 0x02},
1006         {0x11, AC_VERB_SET_CONNECT_SEL, 0x00},
1007         {0x12, AC_VERB_SET_CONNECT_SEL, 0x01},
1008         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00},
1009
1010         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1011         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1012         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1013         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1014         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1015         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1016         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1017         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1018
1019         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1020         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1021         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1022         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1023         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1024         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1025         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1026         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1027         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1028         
1029         { }
1030 };
1031
1032 /* Enable GPIO mask and set output */
1033 static struct hda_verb alc880_gpio1_init_verbs[] = {
1034         {0x01, AC_VERB_SET_GPIO_MASK, 0x01},
1035         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
1036         {0x01, AC_VERB_SET_GPIO_DATA, 0x01},
1037
1038         { }
1039 };
1040
1041 /* Enable GPIO mask and set output */
1042 static struct hda_verb alc880_gpio2_init_verbs[] = {
1043         {0x01, AC_VERB_SET_GPIO_MASK, 0x02},
1044         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
1045         {0x01, AC_VERB_SET_GPIO_DATA, 0x02},
1046
1047         { }
1048 };
1049
1050 /* Clevo m520g init */
1051 static struct hda_verb alc880_pin_clevo_init_verbs[] = {
1052         /* headphone output */
1053         {0x11, AC_VERB_SET_CONNECT_SEL, 0x01},
1054         /* line-out */
1055         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1056         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1057         /* Line-in */
1058         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1059         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1060         /* CD */
1061         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1062         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1063         /* Mic1 (rear panel) */
1064         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1065         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1066         /* Mic2 (front panel) */
1067         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1068         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1069         /* headphone */
1070         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1071         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1072         /* change to EAPD mode */
1073         {0x20, AC_VERB_SET_COEF_INDEX, 0x07},
1074         {0x20, AC_VERB_SET_PROC_COEF,  0x3060},
1075
1076         { }
1077 };
1078
1079 static struct hda_verb alc880_pin_tcl_S700_init_verbs[] = {
1080         /* Headphone output */
1081         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1082         /* Front output*/
1083         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1084         {0x1b, AC_VERB_SET_CONNECT_SEL, 0x00},
1085
1086         /* Line In pin widget for input */
1087         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1088         /* CD pin widget for input */
1089         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1090         /* Mic1 (rear panel) pin widget for input and vref at 80% */
1091         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1092
1093         /* change to EAPD mode */
1094         {0x20, AC_VERB_SET_COEF_INDEX, 0x07},
1095         {0x20, AC_VERB_SET_PROC_COEF,  0x3070},
1096
1097         { }
1098 };
1099
1100 /*
1101  */
1102
1103 static int alc_init(struct hda_codec *codec)
1104 {
1105         struct alc_spec *spec = codec->spec;
1106         unsigned int i;
1107
1108         for (i = 0; i < spec->num_init_verbs; i++)
1109                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
1110         return 0;
1111 }
1112
1113 #ifdef CONFIG_PM
1114 /*
1115  * resume
1116  */
1117 static int alc_resume(struct hda_codec *codec)
1118 {
1119         struct alc_spec *spec = codec->spec;
1120         int i;
1121
1122         alc_init(codec);
1123         for (i = 0; i < spec->num_mixers; i++)
1124                 snd_hda_resume_ctls(codec, spec->mixers[i]);
1125         if (spec->multiout.dig_out_nid)
1126                 snd_hda_resume_spdif_out(codec);
1127         if (spec->dig_in_nid)
1128                 snd_hda_resume_spdif_in(codec);
1129
1130         return 0;
1131 }
1132 #endif
1133
1134 /*
1135  * Analog playback callbacks
1136  */
1137 static int alc880_playback_pcm_open(struct hda_pcm_stream *hinfo,
1138                                     struct hda_codec *codec,
1139                                     struct snd_pcm_substream *substream)
1140 {
1141         struct alc_spec *spec = codec->spec;
1142         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
1143 }
1144
1145 static int alc880_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1146                                        struct hda_codec *codec,
1147                                        unsigned int stream_tag,
1148                                        unsigned int format,
1149                                        struct snd_pcm_substream *substream)
1150 {
1151         struct alc_spec *spec = codec->spec;
1152         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
1153                                                 format, substream);
1154 }
1155
1156 static int alc880_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1157                                        struct hda_codec *codec,
1158                                        struct snd_pcm_substream *substream)
1159 {
1160         struct alc_spec *spec = codec->spec;
1161         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
1162 }
1163
1164 /*
1165  * Digital out
1166  */
1167 static int alc880_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1168                                         struct hda_codec *codec,
1169                                         struct snd_pcm_substream *substream)
1170 {
1171         struct alc_spec *spec = codec->spec;
1172         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1173 }
1174
1175 static int alc880_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1176                                          struct hda_codec *codec,
1177                                          struct snd_pcm_substream *substream)
1178 {
1179         struct alc_spec *spec = codec->spec;
1180         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1181 }
1182
1183 /*
1184  * Analog capture
1185  */
1186 static int alc880_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1187                                       struct hda_codec *codec,
1188                                       unsigned int stream_tag,
1189                                       unsigned int format,
1190                                       struct snd_pcm_substream *substream)
1191 {
1192         struct alc_spec *spec = codec->spec;
1193
1194         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1195                                    stream_tag, 0, format);
1196         return 0;
1197 }
1198
1199 static int alc880_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1200                                       struct hda_codec *codec,
1201                                       struct snd_pcm_substream *substream)
1202 {
1203         struct alc_spec *spec = codec->spec;
1204
1205         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
1206         return 0;
1207 }
1208
1209
1210 /*
1211  */
1212 static struct hda_pcm_stream alc880_pcm_analog_playback = {
1213         .substreams = 1,
1214         .channels_min = 2,
1215         .channels_max = 8,
1216         /* NID is set in alc_build_pcms */
1217         .ops = {
1218                 .open = alc880_playback_pcm_open,
1219                 .prepare = alc880_playback_pcm_prepare,
1220                 .cleanup = alc880_playback_pcm_cleanup
1221         },
1222 };
1223
1224 static struct hda_pcm_stream alc880_pcm_analog_capture = {
1225         .substreams = 2,
1226         .channels_min = 2,
1227         .channels_max = 2,
1228         /* NID is set in alc_build_pcms */
1229         .ops = {
1230                 .prepare = alc880_capture_pcm_prepare,
1231                 .cleanup = alc880_capture_pcm_cleanup
1232         },
1233 };
1234
1235 static struct hda_pcm_stream alc880_pcm_digital_playback = {
1236         .substreams = 1,
1237         .channels_min = 2,
1238         .channels_max = 2,
1239         /* NID is set in alc_build_pcms */
1240         .ops = {
1241                 .open = alc880_dig_playback_pcm_open,
1242                 .close = alc880_dig_playback_pcm_close
1243         },
1244 };
1245
1246 static struct hda_pcm_stream alc880_pcm_digital_capture = {
1247         .substreams = 1,
1248         .channels_min = 2,
1249         .channels_max = 2,
1250         /* NID is set in alc_build_pcms */
1251 };
1252
1253 static int alc_build_pcms(struct hda_codec *codec)
1254 {
1255         struct alc_spec *spec = codec->spec;
1256         struct hda_pcm *info = spec->pcm_rec;
1257         int i;
1258
1259         codec->num_pcms = 1;
1260         codec->pcm_info = info;
1261
1262         info->name = spec->stream_name_analog;
1263         if (spec->stream_analog_playback) {
1264                 snd_assert(spec->multiout.dac_nids, return -EINVAL);
1265                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *(spec->stream_analog_playback);
1266                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
1267         }
1268         if (spec->stream_analog_capture) {
1269                 snd_assert(spec->adc_nids, return -EINVAL);
1270                 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_analog_capture);
1271                 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
1272         }
1273
1274         if (spec->channel_mode) {
1275                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = 0;
1276                 for (i = 0; i < spec->num_channel_mode; i++) {
1277                         if (spec->channel_mode[i].channels > info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max) {
1278                                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->channel_mode[i].channels;
1279                         }
1280                 }
1281         }
1282
1283         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1284                 codec->num_pcms++;
1285                 info++;
1286                 info->name = spec->stream_name_digital;
1287                 if (spec->multiout.dig_out_nid &&
1288                     spec->stream_digital_playback) {
1289                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *(spec->stream_digital_playback);
1290                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
1291                 }
1292                 if (spec->dig_in_nid &&
1293                     spec->stream_digital_capture) {
1294                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_digital_capture);
1295                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
1296                 }
1297         }
1298
1299         return 0;
1300 }
1301
1302 static void alc_free(struct hda_codec *codec)
1303 {
1304         struct alc_spec *spec = codec->spec;
1305         unsigned int i;
1306
1307         if (! spec)
1308                 return;
1309
1310         if (spec->kctl_alloc) {
1311                 for (i = 0; i < spec->num_kctl_used; i++)
1312                         kfree(spec->kctl_alloc[i].name);
1313                 kfree(spec->kctl_alloc);
1314         }
1315         kfree(spec);
1316 }
1317
1318 /*
1319  */
1320 static struct hda_codec_ops alc_patch_ops = {
1321         .build_controls = alc_build_controls,
1322         .build_pcms = alc_build_pcms,
1323         .init = alc_init,
1324         .free = alc_free,
1325 #ifdef CONFIG_PM
1326         .resume = alc_resume,
1327 #endif
1328 };
1329
1330
1331 /*
1332  * Test configuration for debugging
1333  *
1334  * Almost all inputs/outputs are enabled.  I/O pins can be configured via
1335  * enum controls.
1336  */
1337 #ifdef CONFIG_SND_DEBUG
1338 static hda_nid_t alc880_test_dac_nids[4] = {
1339         0x02, 0x03, 0x04, 0x05
1340 };
1341
1342 static struct hda_input_mux alc880_test_capture_source = {
1343         .num_items = 5,
1344         .items = {
1345                 { "In-1", 0x0 },
1346                 { "In-2", 0x1 },
1347                 { "In-3", 0x2 },
1348                 { "In-4", 0x3 },
1349                 { "CD", 0x4 },
1350         },
1351 };
1352
1353 static struct hda_channel_mode alc880_test_modes[4] = {
1354         { 2, NULL },
1355         { 4, NULL },
1356         { 6, NULL },
1357         { 8, NULL },
1358 };
1359
1360 static int alc_test_pin_ctl_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1361 {
1362         static char *texts[] = {
1363                 "N/A", "Line Out", "HP Out",
1364                 "In Hi-Z", "In 50%", "In Grd", "In 80%", "In 100%"
1365         };
1366         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1367         uinfo->count = 1;
1368         uinfo->value.enumerated.items = 8;
1369         if (uinfo->value.enumerated.item >= 8)
1370                 uinfo->value.enumerated.item = 7;
1371         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1372         return 0;
1373 }
1374
1375 static int alc_test_pin_ctl_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1376 {
1377         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1378         hda_nid_t nid = (hda_nid_t)kcontrol->private_value;
1379         unsigned int pin_ctl, item = 0;
1380
1381         pin_ctl = snd_hda_codec_read(codec, nid, 0,
1382                                      AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1383         if (pin_ctl & AC_PINCTL_OUT_EN) {
1384                 if (pin_ctl & AC_PINCTL_HP_EN)
1385                         item = 2;
1386                 else
1387                         item = 1;
1388         } else if (pin_ctl & AC_PINCTL_IN_EN) {
1389                 switch (pin_ctl & AC_PINCTL_VREFEN) {
1390                 case AC_PINCTL_VREF_HIZ: item = 3; break;
1391                 case AC_PINCTL_VREF_50:  item = 4; break;
1392                 case AC_PINCTL_VREF_GRD: item = 5; break;
1393                 case AC_PINCTL_VREF_80:  item = 6; break;
1394                 case AC_PINCTL_VREF_100: item = 7; break;
1395                 }
1396         }
1397         ucontrol->value.enumerated.item[0] = item;
1398         return 0;
1399 }
1400
1401 static int alc_test_pin_ctl_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1402 {
1403         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1404         hda_nid_t nid = (hda_nid_t)kcontrol->private_value;
1405         static unsigned int ctls[] = {
1406                 0, AC_PINCTL_OUT_EN, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN,
1407                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_HIZ,
1408                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_50,
1409                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_GRD,
1410                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_80,
1411                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_100,
1412         };
1413         unsigned int old_ctl, new_ctl;
1414
1415         old_ctl = snd_hda_codec_read(codec, nid, 0,
1416                                      AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1417         new_ctl = ctls[ucontrol->value.enumerated.item[0]];
1418         if (old_ctl != new_ctl) {
1419                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, new_ctl);
1420                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1421                                     ucontrol->value.enumerated.item[0] >= 3 ? 0xb080 : 0xb000);
1422                 return 1;
1423         }
1424         return 0;
1425 }
1426
1427 static int alc_test_pin_src_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1428 {
1429         static char *texts[] = {
1430                 "Front", "Surround", "CLFE", "Side"
1431         };
1432         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1433         uinfo->count = 1;
1434         uinfo->value.enumerated.items = 4;
1435         if (uinfo->value.enumerated.item >= 4)
1436                 uinfo->value.enumerated.item = 3;
1437         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1438         return 0;
1439 }
1440
1441 static int alc_test_pin_src_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1442 {
1443         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1444         hda_nid_t nid = (hda_nid_t)kcontrol->private_value;
1445         unsigned int sel;
1446
1447         sel = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_SEL, 0);
1448         ucontrol->value.enumerated.item[0] = sel & 3;
1449         return 0;
1450 }
1451
1452 static int alc_test_pin_src_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1453 {
1454         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1455         hda_nid_t nid = (hda_nid_t)kcontrol->private_value;
1456         unsigned int sel;
1457
1458         sel = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_SEL, 0) & 3;
1459         if (ucontrol->value.enumerated.item[0] != sel) {
1460                 sel = ucontrol->value.enumerated.item[0] & 3;
1461                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, sel);
1462                 return 1;
1463         }
1464         return 0;
1465 }
1466
1467 #define PIN_CTL_TEST(xname,nid) {                       \
1468                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,    \
1469                         .name = xname,                 \
1470                         .info = alc_test_pin_ctl_info, \
1471                         .get = alc_test_pin_ctl_get,   \
1472                         .put = alc_test_pin_ctl_put,   \
1473                         .private_value = nid           \
1474                         }
1475
1476 #define PIN_SRC_TEST(xname,nid) {                       \
1477                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,    \
1478                         .name = xname,                 \
1479                         .info = alc_test_pin_src_info, \
1480                         .get = alc_test_pin_src_get,   \
1481                         .put = alc_test_pin_src_put,   \
1482                         .private_value = nid           \
1483                         }
1484
1485 static struct snd_kcontrol_new alc880_test_mixer[] = {
1486         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
1487         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
1488         HDA_CODEC_VOLUME("CLFE Playback Volume", 0x0e, 0x0, HDA_OUTPUT),
1489         HDA_CODEC_VOLUME("Side Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
1490         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
1491         HDA_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
1492         HDA_BIND_MUTE("CLFE Playback Switch", 0x0e, 2, HDA_INPUT),
1493         HDA_BIND_MUTE("Side Playback Switch", 0x0f, 2, HDA_INPUT),
1494         PIN_CTL_TEST("Front Pin Mode", 0x14),
1495         PIN_CTL_TEST("Surround Pin Mode", 0x15),
1496         PIN_CTL_TEST("CLFE Pin Mode", 0x16),
1497         PIN_CTL_TEST("Side Pin Mode", 0x17),
1498         PIN_CTL_TEST("In-1 Pin Mode", 0x18),
1499         PIN_CTL_TEST("In-2 Pin Mode", 0x19),
1500         PIN_CTL_TEST("In-3 Pin Mode", 0x1a),
1501         PIN_CTL_TEST("In-4 Pin Mode", 0x1b),
1502         PIN_SRC_TEST("In-1 Pin Source", 0x18),
1503         PIN_SRC_TEST("In-2 Pin Source", 0x19),
1504         PIN_SRC_TEST("In-3 Pin Source", 0x1a),
1505         PIN_SRC_TEST("In-4 Pin Source", 0x1b),
1506         HDA_CODEC_VOLUME("In-1 Playback Volume", 0x0b, 0x0, HDA_INPUT),
1507         HDA_CODEC_MUTE("In-1 Playback Switch", 0x0b, 0x0, HDA_INPUT),
1508         HDA_CODEC_VOLUME("In-2 Playback Volume", 0x0b, 0x1, HDA_INPUT),
1509         HDA_CODEC_MUTE("In-2 Playback Switch", 0x0b, 0x1, HDA_INPUT),
1510         HDA_CODEC_VOLUME("In-3 Playback Volume", 0x0b, 0x2, HDA_INPUT),
1511         HDA_CODEC_MUTE("In-3 Playback Switch", 0x0b, 0x2, HDA_INPUT),
1512         HDA_CODEC_VOLUME("In-4 Playback Volume", 0x0b, 0x3, HDA_INPUT),
1513         HDA_CODEC_MUTE("In-4 Playback Switch", 0x0b, 0x3, HDA_INPUT),
1514         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x4, HDA_INPUT),
1515         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x4, HDA_INPUT),
1516         {
1517                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1518                 .name = "Channel Mode",
1519                 .info = alc_ch_mode_info,
1520                 .get = alc_ch_mode_get,
1521                 .put = alc_ch_mode_put,
1522         },
1523         { } /* end */
1524 };
1525
1526 static struct hda_verb alc880_test_init_verbs[] = {
1527         /* Unmute inputs of 0x0c - 0x0f */
1528         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1529         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1530         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1531         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1532         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1533         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1534         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1535         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1536         /* Vol output for 0x0c-0x0f */
1537         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1538         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1539         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1540         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1541         /* Set output pins 0x14-0x17 */
1542         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1543         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1544         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1545         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1546         /* Unmute output pins 0x14-0x17 */
1547         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1548         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1549         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1550         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1551         /* Set input pins 0x18-0x1c */
1552         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1553         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1554         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1555         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1556         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1557         /* Mute input pins 0x18-0x1b */
1558         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1559         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1560         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1561         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1562         /* ADC set up */
1563         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1564         {0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
1565         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1566         {0x08, AC_VERB_SET_CONNECT_SEL, 0x00},
1567         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1568         {0x09, AC_VERB_SET_CONNECT_SEL, 0x00},
1569         /* Analog input/passthru */
1570         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1571         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1572         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
1573         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
1574         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
1575         { }
1576 };
1577 #endif
1578
1579 /*
1580  */
1581
1582 static struct hda_board_config alc880_cfg_tbl[] = {
1583         /* Back 3 jack, front 2 jack */
1584         { .modelname = "3stack", .config = ALC880_3ST },
1585         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe200, .config = ALC880_3ST },
1586         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe201, .config = ALC880_3ST },
1587         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe202, .config = ALC880_3ST },
1588         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe203, .config = ALC880_3ST },
1589         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe204, .config = ALC880_3ST },
1590         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe205, .config = ALC880_3ST },
1591         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe206, .config = ALC880_3ST },
1592         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe207, .config = ALC880_3ST },
1593         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe208, .config = ALC880_3ST },
1594         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe209, .config = ALC880_3ST },
1595         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe20a, .config = ALC880_3ST },
1596         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe20b, .config = ALC880_3ST },
1597         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe20c, .config = ALC880_3ST },
1598         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe20d, .config = ALC880_3ST },
1599         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe20e, .config = ALC880_3ST },
1600         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe20f, .config = ALC880_3ST },
1601         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe210, .config = ALC880_3ST },
1602         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe211, .config = ALC880_3ST },
1603         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe214, .config = ALC880_3ST },
1604         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe302, .config = ALC880_3ST },
1605         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe303, .config = ALC880_3ST },
1606         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe304, .config = ALC880_3ST },
1607         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe306, .config = ALC880_3ST },
1608         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe307, .config = ALC880_3ST },
1609         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe404, .config = ALC880_3ST },
1610         { .pci_subvendor = 0x8086, .pci_subdevice = 0xa101, .config = ALC880_3ST },
1611         { .pci_subvendor = 0x107b, .pci_subdevice = 0x3031, .config = ALC880_3ST },
1612         { .pci_subvendor = 0x107b, .pci_subdevice = 0x4036, .config = ALC880_3ST },
1613         { .pci_subvendor = 0x107b, .pci_subdevice = 0x4037, .config = ALC880_3ST },
1614         { .pci_subvendor = 0x107b, .pci_subdevice = 0x4038, .config = ALC880_3ST },
1615         { .pci_subvendor = 0x107b, .pci_subdevice = 0x4040, .config = ALC880_3ST },
1616         { .pci_subvendor = 0x107b, .pci_subdevice = 0x4041, .config = ALC880_3ST },
1617         /* TCL S700 */
1618         { .pci_subvendor = 0x19db, .pci_subdevice = 0x4188, .config = ALC880_TCL_S700 },
1619
1620         /* Back 3 jack, front 2 jack (Internal add Aux-In) */
1621         { .pci_subvendor = 0x1025, .pci_subdevice = 0xe310, .config = ALC880_3ST },
1622         { .pci_subvendor = 0x104d, .pci_subdevice = 0x81d6, .config = ALC880_3ST }, 
1623         { .pci_subvendor = 0x104d, .pci_subdevice = 0x81a0, .config = ALC880_3ST },
1624
1625         /* Back 3 jack plus 1 SPDIF out jack, front 2 jack */
1626         { .modelname = "3stack-digout", .config = ALC880_3ST_DIG },
1627         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe308, .config = ALC880_3ST_DIG },
1628         { .pci_subvendor = 0x1025, .pci_subdevice = 0x0070, .config = ALC880_3ST_DIG },
1629         /* Clevo m520G NB */
1630         { .pci_subvendor = 0x1558, .pci_subdevice = 0x0520, .config = ALC880_CLEVO },
1631
1632         /* Back 3 jack plus 1 SPDIF out jack, front 2 jack (Internal add Aux-In)*/
1633         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe305, .config = ALC880_3ST_DIG },
1634         { .pci_subvendor = 0x8086, .pci_subdevice = 0xd402, .config = ALC880_3ST_DIG },
1635         { .pci_subvendor = 0x1025, .pci_subdevice = 0xe309, .config = ALC880_3ST_DIG },
1636
1637         /* Back 5 jack, front 2 jack */
1638         { .modelname = "5stack", .config = ALC880_5ST },
1639         { .pci_subvendor = 0x107b, .pci_subdevice = 0x3033, .config = ALC880_5ST },
1640         { .pci_subvendor = 0x107b, .pci_subdevice = 0x4039, .config = ALC880_5ST },
1641         { .pci_subvendor = 0x107b, .pci_subdevice = 0x3032, .config = ALC880_5ST },
1642         { .pci_subvendor = 0x103c, .pci_subdevice = 0x2a09, .config = ALC880_5ST },
1643         { .pci_subvendor = 0x1043, .pci_subdevice = 0x814e, .config = ALC880_5ST },
1644
1645         /* Back 5 jack plus 1 SPDIF out jack, front 2 jack */
1646         { .modelname = "5stack-digout", .config = ALC880_5ST_DIG },
1647         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe224, .config = ALC880_5ST_DIG },
1648         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe400, .config = ALC880_5ST_DIG },
1649         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe401, .config = ALC880_5ST_DIG },
1650         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe402, .config = ALC880_5ST_DIG },
1651         { .pci_subvendor = 0x8086, .pci_subdevice = 0xd400, .config = ALC880_5ST_DIG },
1652         { .pci_subvendor = 0x8086, .pci_subdevice = 0xd401, .config = ALC880_5ST_DIG },
1653         { .pci_subvendor = 0x8086, .pci_subdevice = 0xa100, .config = ALC880_5ST_DIG },
1654         { .pci_subvendor = 0x1565, .pci_subdevice = 0x8202, .config = ALC880_5ST_DIG },
1655         { .pci_subvendor = 0x1019, .pci_subdevice = 0xa880, .config = ALC880_5ST_DIG },
1656         /* { .pci_subvendor = 0x1019, .pci_subdevice = 0xa884, .config = ALC880_5ST_DIG }, */ /* conflict with 6stack */
1657         { .pci_subvendor = 0x1695, .pci_subdevice = 0x400d, .config = ALC880_5ST_DIG },
1658         /* note subvendor = 0 below */
1659         /* { .pci_subvendor = 0x0000, .pci_subdevice = 0x8086, .config = ALC880_5ST_DIG }, */
1660
1661         { .modelname = "w810", .config = ALC880_W810 },
1662         { .pci_subvendor = 0x161f, .pci_subdevice = 0x203d, .config = ALC880_W810 },
1663
1664         { .modelname = "z71v", .config = ALC880_Z71V },
1665         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1964, .config = ALC880_Z71V },
1666
1667         { .modelname = "6stack", .config = ALC880_6ST },
1668         { .pci_subvendor = 0x1043, .pci_subdevice = 0x81b4, .config = ALC880_6ST },
1669         { .pci_subvendor = 0x1019, .pci_subdevice = 0xa884, .config = ALC880_6ST }, /* Acer APFV */
1670
1671         { .modelname = "6stack-digout", .config = ALC880_6ST_DIG },
1672         { .pci_subvendor = 0x2668, .pci_subdevice = 0x8086, .config = ALC880_6ST_DIG },
1673         { .pci_subvendor = 0x8086, .pci_subdevice = 0x2668, .config = ALC880_6ST_DIG },
1674         { .pci_subvendor = 0x1462, .pci_subdevice = 0x1150, .config = ALC880_6ST_DIG },
1675         { .pci_subvendor = 0xe803, .pci_subdevice = 0x1019, .config = ALC880_6ST_DIG },
1676         { .pci_subvendor = 0x1039, .pci_subdevice = 0x1234, .config = ALC880_6ST_DIG },
1677         { .pci_subvendor = 0x1025, .pci_subdevice = 0x0077, .config = ALC880_6ST_DIG },
1678         { .pci_subvendor = 0x1025, .pci_subdevice = 0x0078, .config = ALC880_6ST_DIG },
1679         { .pci_subvendor = 0x1025, .pci_subdevice = 0x0087, .config = ALC880_6ST_DIG },
1680
1681         { .modelname = "asus", .config = ALC880_ASUS },
1682         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1964, .config = ALC880_ASUS_DIG },
1683         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1973, .config = ALC880_ASUS_DIG },
1684         { .pci_subvendor = 0x1043, .pci_subdevice = 0x19b3, .config = ALC880_ASUS_DIG },
1685         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1113, .config = ALC880_ASUS_DIG },
1686         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1173, .config = ALC880_ASUS_DIG },
1687         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1993, .config = ALC880_ASUS },
1688         { .pci_subvendor = 0x1043, .pci_subdevice = 0x10c3, .config = ALC880_ASUS_DIG },
1689         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1133, .config = ALC880_ASUS },
1690         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1123, .config = ALC880_ASUS_DIG },
1691         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1143, .config = ALC880_ASUS },
1692         { .pci_subvendor = 0x1043, .pci_subdevice = 0x10b3, .config = ALC880_ASUS_W1V },
1693         { .pci_subvendor = 0x1558, .pci_subdevice = 0x5401, .config = ALC880_ASUS_DIG2 },
1694
1695         { .modelname = "uniwill", .config = ALC880_UNIWILL_DIG },
1696         { .pci_subvendor = 0x1584, .pci_subdevice = 0x9050, .config = ALC880_UNIWILL_DIG },     
1697
1698         { .modelname = "F1734", .config = ALC880_F1734 },
1699         { .pci_subvendor = 0x1734, .pci_subdevice = 0x107c, .config = ALC880_F1734 },
1700         { .pci_subvendor = 0x1584, .pci_subdevice = 0x9054, .config = ALC880_F1734 },
1701
1702 #ifdef CONFIG_SND_DEBUG
1703         { .modelname = "test", .config = ALC880_TEST },
1704 #endif
1705         { .modelname = "auto", .config = ALC880_AUTO },
1706
1707         {}
1708 };
1709
1710 /*
1711  * ALC880 codec presets
1712  */
1713 static struct alc_config_preset alc880_presets[] = {
1714         [ALC880_3ST] = {
1715                 .mixers = { alc880_three_stack_mixer },
1716                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_3stack_init_verbs },
1717                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
1718                 .dac_nids = alc880_dac_nids,
1719                 .num_channel_mode = ARRAY_SIZE(alc880_threestack_modes),
1720                 .channel_mode = alc880_threestack_modes,
1721                 .input_mux = &alc880_capture_source,
1722         },
1723         [ALC880_3ST_DIG] = {
1724                 .mixers = { alc880_three_stack_mixer },
1725                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_3stack_init_verbs },
1726                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
1727                 .dac_nids = alc880_dac_nids,
1728                 .dig_out_nid = ALC880_DIGOUT_NID,
1729                 .num_channel_mode = ARRAY_SIZE(alc880_threestack_modes),
1730                 .channel_mode = alc880_threestack_modes,
1731                 .input_mux = &alc880_capture_source,
1732         },
1733         [ALC880_TCL_S700] = {
1734                 .mixers = { alc880_tcl_s700_mixer },
1735                 .init_verbs = { alc880_volume_init_verbs,
1736                                 alc880_pin_tcl_S700_init_verbs,
1737                                 alc880_gpio2_init_verbs },
1738                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
1739                 .dac_nids = alc880_dac_nids,
1740                 .hp_nid = 0x03,
1741                 .num_channel_mode = ARRAY_SIZE(alc880_2_jack_modes),
1742                 .channel_mode = alc880_2_jack_modes,
1743                 .input_mux = &alc880_capture_source,
1744         },
1745         [ALC880_5ST] = {
1746                 .mixers = { alc880_three_stack_mixer, alc880_five_stack_mixer},
1747                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_5stack_init_verbs },
1748                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
1749                 .dac_nids = alc880_dac_nids,
1750                 .num_channel_mode = ARRAY_SIZE(alc880_fivestack_modes),
1751                 .channel_mode = alc880_fivestack_modes,
1752                 .input_mux = &alc880_capture_source,
1753         },
1754         [ALC880_5ST_DIG] = {
1755                 .mixers = { alc880_three_stack_mixer, alc880_five_stack_mixer },
1756                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_5stack_init_verbs },
1757                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
1758                 .dac_nids = alc880_dac_nids,
1759                 .dig_out_nid = ALC880_DIGOUT_NID,
1760                 .num_channel_mode = ARRAY_SIZE(alc880_fivestack_modes),
1761                 .channel_mode = alc880_fivestack_modes,
1762                 .input_mux = &alc880_capture_source,
1763         },
1764         [ALC880_6ST] = {
1765                 .mixers = { alc880_six_stack_mixer },
1766                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_6stack_init_verbs },
1767                 .num_dacs = ARRAY_SIZE(alc880_6st_dac_nids),
1768                 .dac_nids = alc880_6st_dac_nids,
1769                 .num_channel_mode = ARRAY_SIZE(alc880_sixstack_modes),
1770                 .channel_mode = alc880_sixstack_modes,
1771                 .input_mux = &alc880_6stack_capture_source,
1772         },
1773         [ALC880_6ST_DIG] = {
1774                 .mixers = { alc880_six_stack_mixer },
1775                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_6stack_init_verbs },
1776                 .num_dacs = ARRAY_SIZE(alc880_6st_dac_nids),
1777                 .dac_nids = alc880_6st_dac_nids,
1778                 .dig_out_nid = ALC880_DIGOUT_NID,
1779                 .num_channel_mode = ARRAY_SIZE(alc880_sixstack_modes),
1780                 .channel_mode = alc880_sixstack_modes,
1781                 .input_mux = &alc880_6stack_capture_source,
1782         },
1783         [ALC880_W810] = {
1784                 .mixers = { alc880_w810_base_mixer },
1785                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_w810_init_verbs,
1786                                 alc880_gpio2_init_verbs },
1787                 .num_dacs = ARRAY_SIZE(alc880_w810_dac_nids),
1788                 .dac_nids = alc880_w810_dac_nids,
1789                 .dig_out_nid = ALC880_DIGOUT_NID,
1790                 .num_channel_mode = ARRAY_SIZE(alc880_w810_modes),
1791                 .channel_mode = alc880_w810_modes,
1792                 .input_mux = &alc880_capture_source,
1793         },
1794         [ALC880_Z71V] = {
1795                 .mixers = { alc880_z71v_mixer },
1796                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_z71v_init_verbs },
1797                 .num_dacs = ARRAY_SIZE(alc880_z71v_dac_nids),
1798                 .dac_nids = alc880_z71v_dac_nids,
1799                 .dig_out_nid = ALC880_DIGOUT_NID,
1800                 .hp_nid = 0x03,
1801                 .num_channel_mode = ARRAY_SIZE(alc880_2_jack_modes),
1802                 .channel_mode = alc880_2_jack_modes,
1803                 .input_mux = &alc880_capture_source,
1804         },
1805         [ALC880_F1734] = {
1806                 .mixers = { alc880_f1734_mixer },
1807                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_f1734_init_verbs },
1808                 .num_dacs = ARRAY_SIZE(alc880_f1734_dac_nids),
1809                 .dac_nids = alc880_f1734_dac_nids,
1810                 .hp_nid = 0x02,
1811                 .num_channel_mode = ARRAY_SIZE(alc880_2_jack_modes),
1812                 .channel_mode = alc880_2_jack_modes,
1813                 .input_mux = &alc880_capture_source,
1814         },
1815         [ALC880_ASUS] = {
1816                 .mixers = { alc880_asus_mixer },
1817                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_asus_init_verbs,
1818                                 alc880_gpio1_init_verbs },
1819                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
1820                 .dac_nids = alc880_asus_dac_nids,
1821                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
1822                 .channel_mode = alc880_asus_modes,
1823                 .input_mux = &alc880_capture_source,
1824         },
1825         [ALC880_ASUS_DIG] = {
1826                 .mixers = { alc880_asus_mixer },
1827                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_asus_init_verbs,
1828                                 alc880_gpio1_init_verbs },
1829                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
1830                 .dac_nids = alc880_asus_dac_nids,
1831                 .dig_out_nid = ALC880_DIGOUT_NID,
1832                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
1833                 .channel_mode = alc880_asus_modes,
1834                 .input_mux = &alc880_capture_source,
1835         },
1836         [ALC880_ASUS_DIG2] = {
1837                 .mixers = { alc880_asus_mixer },
1838                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_asus_init_verbs,
1839                                 alc880_gpio2_init_verbs }, /* use GPIO2 */
1840                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
1841                 .dac_nids = alc880_asus_dac_nids,
1842                 .dig_out_nid = ALC880_DIGOUT_NID,
1843                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
1844                 .channel_mode = alc880_asus_modes,
1845                 .input_mux = &alc880_capture_source,
1846         },
1847         [ALC880_ASUS_W1V] = {
1848                 .mixers = { alc880_asus_mixer, alc880_asus_w1v_mixer },
1849                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_asus_init_verbs,
1850                                 alc880_gpio1_init_verbs },
1851                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
1852                 .dac_nids = alc880_asus_dac_nids,
1853                 .dig_out_nid = ALC880_DIGOUT_NID,
1854                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
1855                 .channel_mode = alc880_asus_modes,
1856                 .input_mux = &alc880_capture_source,
1857         },
1858         [ALC880_UNIWILL_DIG] = {
1859                 .mixers = { alc880_asus_mixer, alc880_pcbeep_mixer },
1860                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_asus_init_verbs },
1861                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
1862                 .dac_nids = alc880_asus_dac_nids,
1863                 .dig_out_nid = ALC880_DIGOUT_NID,
1864                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
1865                 .channel_mode = alc880_asus_modes,
1866                 .input_mux = &alc880_capture_source,
1867         },
1868         [ALC880_CLEVO] = {
1869                 .mixers = { alc880_three_stack_mixer },
1870                 .init_verbs = { alc880_volume_init_verbs,
1871                                 alc880_pin_clevo_init_verbs },
1872                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
1873                 .dac_nids = alc880_dac_nids,
1874                 .hp_nid = 0x03,
1875                 .num_channel_mode = ARRAY_SIZE(alc880_threestack_modes),
1876                 .channel_mode = alc880_threestack_modes,
1877                 .input_mux = &alc880_capture_source,
1878         },
1879 #ifdef CONFIG_SND_DEBUG
1880         [ALC880_TEST] = {
1881                 .mixers = { alc880_test_mixer },
1882                 .init_verbs = { alc880_test_init_verbs },
1883                 .num_dacs = ARRAY_SIZE(alc880_test_dac_nids),
1884                 .dac_nids = alc880_test_dac_nids,
1885                 .dig_out_nid = ALC880_DIGOUT_NID,
1886                 .num_channel_mode = ARRAY_SIZE(alc880_test_modes),
1887                 .channel_mode = alc880_test_modes,
1888                 .input_mux = &alc880_test_capture_source,
1889         },
1890 #endif
1891 };
1892
1893 /*
1894  * Automatic parse of I/O pins from the BIOS configuration
1895  */
1896
1897 #define NUM_CONTROL_ALLOC       32
1898 #define NUM_VERB_ALLOC          32
1899
1900 enum {
1901         ALC_CTL_WIDGET_VOL,
1902         ALC_CTL_WIDGET_MUTE,
1903         ALC_CTL_BIND_MUTE,
1904 };
1905 static struct snd_kcontrol_new alc880_control_templates[] = {
1906         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
1907         HDA_CODEC_MUTE(NULL, 0, 0, 0),
1908         HDA_BIND_MUTE(NULL, 0, 0, 0),
1909 };
1910
1911 /* add dynamic controls */
1912 static int add_control(struct alc_spec *spec, int type, const char *name, unsigned long val)
1913 {
1914         struct snd_kcontrol_new *knew;
1915
1916         if (spec->num_kctl_used >= spec->num_kctl_alloc) {
1917                 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
1918
1919                 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
1920                 if (! knew)
1921                         return -ENOMEM;
1922                 if (spec->kctl_alloc) {
1923                         memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
1924                         kfree(spec->kctl_alloc);
1925                 }
1926                 spec->kctl_alloc = knew;
1927                 spec->num_kctl_alloc = num;
1928         }
1929
1930         knew = &spec->kctl_alloc[spec->num_kctl_used];
1931         *knew = alc880_control_templates[type];
1932         knew->name = kstrdup(name, GFP_KERNEL);
1933         if (! knew->name)
1934                 return -ENOMEM;
1935         knew->private_value = val;
1936         spec->num_kctl_used++;
1937         return 0;
1938 }
1939
1940 #define alc880_is_fixed_pin(nid)        ((nid) >= 0x14 && (nid) <= 0x17)
1941 #define alc880_fixed_pin_idx(nid)       ((nid) - 0x14)
1942 #define alc880_is_multi_pin(nid)        ((nid) >= 0x18)
1943 #define alc880_multi_pin_idx(nid)       ((nid) - 0x18)
1944 #define alc880_is_input_pin(nid)        ((nid) >= 0x18)
1945 #define alc880_input_pin_idx(nid)       ((nid) - 0x18)
1946 #define alc880_idx_to_dac(nid)          ((nid) + 0x02)
1947 #define alc880_dac_to_idx(nid)          ((nid) - 0x02)
1948 #define alc880_idx_to_mixer(nid)        ((nid) + 0x0c)
1949 #define alc880_idx_to_selector(nid)     ((nid) + 0x10)
1950 #define ALC880_PIN_CD_NID               0x1c
1951
1952 /* fill in the dac_nids table from the parsed pin configuration */
1953 static int alc880_auto_fill_dac_nids(struct alc_spec *spec, const struct auto_pin_cfg *cfg)
1954 {
1955         hda_nid_t nid;
1956         int assigned[4];
1957         int i, j;
1958
1959         memset(assigned, 0, sizeof(assigned));
1960         spec->multiout.dac_nids = spec->private_dac_nids;
1961
1962         /* check the pins hardwired to audio widget */
1963         for (i = 0; i < cfg->line_outs; i++) {
1964                 nid = cfg->line_out_pins[i];
1965                 if (alc880_is_fixed_pin(nid)) {
1966                         int idx = alc880_fixed_pin_idx(nid);
1967                         spec->multiout.dac_nids[i] = alc880_idx_to_dac(idx);
1968                         assigned[idx] = 1;
1969                 }
1970         }
1971         /* left pins can be connect to any audio widget */
1972         for (i = 0; i < cfg->line_outs; i++) {
1973                 nid = cfg->line_out_pins[i];
1974                 if (alc880_is_fixed_pin(nid))
1975                         continue;
1976                 /* search for an empty channel */
1977                 for (j = 0; j < cfg->line_outs; j++) {
1978                         if (! assigned[j]) {
1979                                 spec->multiout.dac_nids[i] = alc880_idx_to_dac(j);
1980                                 assigned[j] = 1;
1981                                 break;
1982                         }
1983                 }
1984         }
1985         spec->multiout.num_dacs = cfg->line_outs;
1986         return 0;
1987 }
1988
1989 /* add playback controls from the parsed DAC table */
1990 static int alc880_auto_create_multi_out_ctls(struct alc_spec *spec,
1991                                              const struct auto_pin_cfg *cfg)
1992 {
1993         char name[32];
1994         static const char *chname[4] = { "Front", "Surround", NULL /*CLFE*/, "Side" };
1995         hda_nid_t nid;
1996         int i, err;
1997
1998         for (i = 0; i < cfg->line_outs; i++) {
1999                 if (! spec->multiout.dac_nids[i])
2000                         continue;
2001                 nid = alc880_idx_to_mixer(alc880_dac_to_idx(spec->multiout.dac_nids[i]));
2002                 if (i == 2) {
2003                         /* Center/LFE */
2004                         if ((err = add_control(spec, ALC_CTL_WIDGET_VOL, "Center Playback Volume",
2005                                                HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
2006                                 return err;
2007                         if ((err = add_control(spec, ALC_CTL_WIDGET_VOL, "LFE Playback Volume",
2008                                                HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
2009                                 return err;
2010                         if ((err = add_control(spec, ALC_CTL_BIND_MUTE, "Center Playback Switch",
2011                                                HDA_COMPOSE_AMP_VAL(nid, 1, 2, HDA_INPUT))) < 0)
2012                                 return err;
2013                         if ((err = add_control(spec, ALC_CTL_BIND_MUTE, "LFE Playback Switch",
2014                                                HDA_COMPOSE_AMP_VAL(nid, 2, 2, HDA_INPUT))) < 0)
2015                                 return err;
2016                 } else {
2017                         sprintf(name, "%s Playback Volume", chname[i]);
2018                         if ((err = add_control(spec, ALC_CTL_WIDGET_VOL, name,
2019                                                HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
2020                                 return err;
2021                         sprintf(name, "%s Playback Switch", chname[i]);
2022                         if ((err = add_control(spec, ALC_CTL_BIND_MUTE, name,
2023                                                HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT))) < 0)
2024                                 return err;
2025                 }
2026         }
2027         return 0;
2028 }
2029
2030 /* add playback controls for speaker and HP outputs */
2031 static int alc880_auto_create_extra_out(struct alc_spec *spec, hda_nid_t pin,
2032                                         const char *pfx)
2033 {
2034         hda_nid_t nid;
2035         int err;
2036         char name[32];
2037
2038         if (! pin)
2039                 return 0;
2040
2041         if (alc880_is_fixed_pin(pin)) {
2042                 nid = alc880_idx_to_dac(alc880_fixed_pin_idx(pin));
2043                 if (! spec->multiout.dac_nids[0]) {
2044                         /* use this as the primary output */
2045                         spec->multiout.dac_nids[0] = nid;
2046                         if (! spec->multiout.num_dacs)
2047                                 spec->multiout.num_dacs = 1;
2048                 } else 
2049                         /* specify the DAC as the extra output */
2050                         spec->multiout.hp_nid = nid;
2051                 /* control HP volume/switch on the output mixer amp */
2052                 nid = alc880_idx_to_mixer(alc880_fixed_pin_idx(pin));
2053                 sprintf(name, "%s Playback Volume", pfx);
2054                 if ((err = add_control(spec, ALC_CTL_WIDGET_VOL, name,
2055                                        HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
2056                         return err;
2057                 sprintf(name, "%s Playback Switch", pfx);
2058                 if ((err = add_control(spec, ALC_CTL_BIND_MUTE, name,
2059                                        HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT))) < 0)
2060                         return err;
2061         } else if (alc880_is_multi_pin(pin)) {
2062                 /* set manual connection */
2063                 if (! spec->multiout.dac_nids[0]) {
2064                         /* use this as the primary output */
2065                         spec->multiout.dac_nids[0] = alc880_idx_to_dac(alc880_multi_pin_idx(pin));
2066                         if (! spec->multiout.num_dacs)
2067                                 spec->multiout.num_dacs = 1;
2068                 }
2069                 /* we have only a switch on HP-out PIN */
2070                 sprintf(name, "%s Playback Switch", pfx);
2071                 if ((err = add_control(spec, ALC_CTL_WIDGET_MUTE, name,
2072                                        HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT))) < 0)
2073                         return err;
2074         }
2075         return 0;
2076 }
2077
2078 /* create input playback/capture controls for the given pin */
2079 static int new_analog_input(struct alc_spec *spec, hda_nid_t pin, const char *ctlname,
2080                             int idx, hda_nid_t mix_nid)
2081 {
2082         char name[32];
2083         int err;
2084
2085         sprintf(name, "%s Playback Volume", ctlname);
2086         if ((err = add_control(spec, ALC_CTL_WIDGET_VOL, name,
2087                                HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT))) < 0)
2088                 return err;
2089         sprintf(name, "%s Playback Switch", ctlname);
2090         if ((err = add_control(spec, ALC_CTL_WIDGET_MUTE, name,
2091                                HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT))) < 0)
2092                 return err;
2093         return 0;
2094 }
2095
2096 /* create playback/capture controls for input pins */
2097 static int alc880_auto_create_analog_input_ctls(struct alc_spec *spec,
2098                                                 const struct auto_pin_cfg *cfg)
2099 {
2100         struct hda_input_mux *imux = &spec->private_imux;
2101         int i, err, idx;
2102
2103         for (i = 0; i < AUTO_PIN_LAST; i++) {
2104                 if (alc880_is_input_pin(cfg->input_pins[i])) {
2105                         idx = alc880_input_pin_idx(cfg->input_pins[i]);
2106                         err = new_analog_input(spec, cfg->input_pins[i],
2107                                                auto_pin_cfg_labels[i],
2108                                                idx, 0x0b);
2109                         if (err < 0)
2110                                 return err;
2111                         imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
2112                         imux->items[imux->num_items].index = alc880_input_pin_idx(cfg->input_pins[i]);
2113                         imux->num_items++;
2114                 }
2115         }
2116         return 0;
2117 }
2118
2119 static void alc880_auto_set_output_and_unmute(struct hda_codec *codec,
2120                                               hda_nid_t nid, int pin_type,
2121                                               int dac_idx)
2122 {
2123         /* set as output */
2124         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
2125         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
2126         /* need the manual connection? */
2127         if (alc880_is_multi_pin(nid)) {
2128                 struct alc_spec *spec = codec->spec;
2129                 int idx = alc880_multi_pin_idx(nid);
2130                 snd_hda_codec_write(codec, alc880_idx_to_selector(idx), 0,
2131                                     AC_VERB_SET_CONNECT_SEL,
2132                                     alc880_dac_to_idx(spec->multiout.dac_nids[dac_idx]));
2133         }
2134 }
2135
2136 static void alc880_auto_init_multi_out(struct hda_codec *codec)
2137 {
2138         struct alc_spec *spec = codec->spec;
2139         int i;
2140
2141         for (i = 0; i < spec->autocfg.line_outs; i++) {
2142                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
2143                 alc880_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
2144         }
2145 }
2146
2147 static void alc880_auto_init_extra_out(struct hda_codec *codec)
2148 {
2149         struct alc_spec *spec = codec->spec;
2150         hda_nid_t pin;
2151
2152         pin = spec->autocfg.speaker_pin;
2153         if (pin) /* connect to front */
2154                 alc880_auto_set_output_and_unmute(codec, pin, PIN_OUT, 0);
2155         pin = spec->autocfg.hp_pin;
2156         if (pin) /* connect to front */
2157                 alc880_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
2158 }
2159
2160 static void alc880_auto_init_analog_input(struct hda_codec *codec)
2161 {
2162         struct alc_spec *spec = codec->spec;
2163         int i;
2164
2165         for (i = 0; i < AUTO_PIN_LAST; i++) {
2166                 hda_nid_t nid = spec->autocfg.input_pins[i];
2167                 if (alc880_is_input_pin(nid)) {
2168                         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2169                                             i <= AUTO_PIN_FRONT_MIC ? PIN_VREF80 : PIN_IN);
2170                         if (nid != ALC880_PIN_CD_NID)
2171                                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2172                                                     AMP_OUT_MUTE);
2173                 }
2174         }
2175 }
2176
2177 /* parse the BIOS configuration and set up the alc_spec */
2178 /* return 1 if successful, 0 if the proper config is not found, or a negative error code */
2179 static int alc880_parse_auto_config(struct hda_codec *codec)
2180 {
2181         struct alc_spec *spec = codec->spec;
2182         int err;
2183         static hda_nid_t alc880_ignore[] = { 0x1d, 0 };
2184
2185         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg,
2186                                                 alc880_ignore)) < 0)
2187                 return err;
2188         if (! spec->autocfg.line_outs && ! spec->autocfg.speaker_pin &&
2189             ! spec->autocfg.hp_pin)
2190                 return 0; /* can't find valid BIOS pin config */
2191
2192         if ((err = alc880_auto_fill_dac_nids(spec, &spec->autocfg)) < 0 ||
2193             (err = alc880_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
2194             (err = alc880_auto_create_extra_out(spec, spec->autocfg.speaker_pin,
2195                                                 "Speaker")) < 0 ||
2196             (err = alc880_auto_create_extra_out(spec, spec->autocfg.speaker_pin,
2197                                                 "Headphone")) < 0 ||
2198             (err = alc880_auto_create_analog_input_ctls(spec, &spec->autocfg)) < 0)
2199                 return err;
2200
2201         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2202
2203         if (spec->autocfg.dig_out_pin)
2204                 spec->multiout.dig_out_nid = ALC880_DIGOUT_NID;
2205         if (spec->autocfg.dig_in_pin)
2206                 spec->dig_in_nid = ALC880_DIGIN_NID;
2207
2208         if (spec->kctl_alloc)
2209                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
2210
2211         spec->init_verbs[spec->num_init_verbs++] = alc880_volume_init_verbs;
2212
2213         spec->input_mux = &spec->private_imux;
2214
2215         return 1;
2216 }
2217
2218 /* init callback for auto-configuration model -- overriding the default init */
2219 static int alc880_auto_init(struct hda_codec *codec)
2220 {
2221         alc_init(codec);
2222         alc880_auto_init_multi_out(codec);
2223         alc880_auto_init_extra_out(codec);
2224         alc880_auto_init_analog_input(codec);
2225         return 0;
2226 }
2227
2228 /*
2229  * OK, here we have finally the patch for ALC880
2230  */
2231
2232 static int patch_alc880(struct hda_codec *codec)
2233 {
2234         struct alc_spec *spec;
2235         int board_config;
2236         int err;
2237
2238         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2239         if (spec == NULL)
2240                 return -ENOMEM;
2241
2242         codec->spec = spec;
2243
2244         board_config = snd_hda_check_board_config(codec, alc880_cfg_tbl);
2245         if (board_config < 0 || board_config >= ALC880_MODEL_LAST) {
2246                 printk(KERN_INFO "hda_codec: Unknown model for ALC880, trying auto-probe from BIOS...\n");
2247                 board_config = ALC880_AUTO;
2248         }
2249
2250         if (board_config == ALC880_AUTO) {
2251                 /* automatic parse from the BIOS config */
2252                 err = alc880_parse_auto_config(codec);
2253                 if (err < 0) {
2254                         alc_free(codec);
2255                         return err;
2256                 } else if (! err) {
2257                         printk(KERN_INFO "hda_codec: Cannot set up configuration from BIOS.  Using 3-stack mode...\n");
2258                         board_config = ALC880_3ST;
2259                 }
2260         }
2261
2262         if (board_config != ALC880_AUTO)
2263                 setup_preset(spec, &alc880_presets[board_config]);
2264
2265         spec->stream_name_analog = "ALC880 Analog";
2266         spec->stream_analog_playback = &alc880_pcm_analog_playback;
2267         spec->stream_analog_capture = &alc880_pcm_analog_capture;
2268
2269         spec->stream_name_digital = "ALC880 Digital";
2270         spec->stream_digital_playback = &alc880_pcm_digital_playback;
2271         spec->stream_digital_capture = &alc880_pcm_digital_capture;
2272
2273         if (! spec->adc_nids && spec->input_mux) {
2274                 /* check whether NID 0x07 is valid */
2275                 unsigned int wcap = get_wcaps(codec, alc880_adc_nids[0]);
2276                 wcap = (wcap & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT; /* get type */
2277                 if (wcap != AC_WID_AUD_IN) {
2278                         spec->adc_nids = alc880_adc_nids_alt;
2279                         spec->num_adc_nids = ARRAY_SIZE(alc880_adc_nids_alt);
2280                         spec->mixers[spec->num_mixers] = alc880_capture_alt_mixer;
2281                         spec->num_mixers++;
2282                 } else {
2283                         spec->adc_nids = alc880_adc_nids;
2284                         spec->num_adc_nids = ARRAY_SIZE(alc880_adc_nids);
2285                         spec->mixers[spec->num_mixers] = alc880_capture_mixer;
2286                         spec->num_mixers++;
2287                 }
2288         }
2289
2290         codec->patch_ops = alc_patch_ops;
2291         if (board_config == ALC880_AUTO)
2292                 codec->patch_ops.init = alc880_auto_init;
2293
2294         return 0;
2295 }
2296
2297
2298 /*
2299  * ALC260 support
2300  */
2301
2302 static hda_nid_t alc260_dac_nids[1] = {
2303         /* front */
2304         0x02,
2305 };
2306
2307 static hda_nid_t alc260_adc_nids[1] = {
2308         /* ADC0 */
2309         0x04,
2310 };
2311
2312 static hda_nid_t alc260_adc_nids_alt[1] = {
2313         /* ADC1 */
2314         0x05,
2315 };
2316
2317 static hda_nid_t alc260_hp_adc_nids[2] = {
2318         /* ADC1, 0 */
2319         0x05, 0x04
2320 };
2321
2322 #define ALC260_DIGOUT_NID       0x03
2323 #define ALC260_DIGIN_NID        0x06
2324
2325 static struct hda_input_mux alc260_capture_source = {
2326         .num_items = 4,
2327         .items = {
2328                 { "Mic", 0x0 },
2329                 { "Front Mic", 0x1 },
2330                 { "Line", 0x2 },
2331                 { "CD", 0x4 },
2332         },
2333 };
2334
2335 /* On Fujitsu S702x laptops capture only makes sense from Mic/LineIn jack
2336  * and the internal CD lines.
2337  */
2338 static struct hda_input_mux alc260_fujitsu_capture_source = {
2339         .num_items = 2,
2340         .items = {
2341                 { "Mic/Line", 0x0 },
2342                 { "CD", 0x4 },
2343         },
2344 };
2345
2346 /*
2347  * This is just place-holder, so there's something for alc_build_pcms to look
2348  * at when it calculates the maximum number of channels. ALC260 has no mixer
2349  * element which allows changing the channel mode, so the verb list is
2350  * never used.
2351  */
2352 static struct hda_channel_mode alc260_modes[1] = {
2353         { 2, NULL },
2354 };
2355
2356
2357 /* Mixer combinations
2358  *
2359  * basic: base_output + input + pc_beep + capture
2360  * HP: base_output + input + capture_alt
2361  * HP_3013: hp_3013 + input + capture
2362  * fujitsu: fujitsu + capture
2363  */
2364
2365 static struct snd_kcontrol_new alc260_base_output_mixer[] = {
2366         HDA_CODEC_VOLUME("Front Playback Volume", 0x08, 0x0, HDA_OUTPUT),
2367         HDA_BIND_MUTE("Front Playback Switch", 0x08, 2, HDA_INPUT),
2368         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x09, 0x0, HDA_OUTPUT),
2369         HDA_BIND_MUTE("Headphone Playback Switch", 0x09, 2, HDA_INPUT),
2370         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x0a, 1, 0x0, HDA_OUTPUT),
2371         HDA_BIND_MUTE_MONO("Mono Playback Switch", 0x0a, 1, 2, HDA_INPUT),
2372         { } /* end */
2373 };      
2374
2375 static struct snd_kcontrol_new alc260_input_mixer[] = {
2376         HDA_CODEC_VOLUME("CD Playback Volume", 0x07, 0x04, HDA_INPUT),
2377         HDA_CODEC_MUTE("CD Playback Switch", 0x07, 0x04, HDA_INPUT),
2378         HDA_CODEC_VOLUME("Line Playback Volume", 0x07, 0x02, HDA_INPUT),
2379         HDA_CODEC_MUTE("Line Playback Switch", 0x07, 0x02, HDA_INPUT),
2380         HDA_CODEC_VOLUME("Mic Playback Volume", 0x07, 0x0, HDA_INPUT),
2381         HDA_CODEC_MUTE("Mic Playback Switch", 0x07, 0x0, HDA_INPUT),
2382         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x07, 0x01, HDA_INPUT),
2383         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x07, 0x01, HDA_INPUT),
2384         { } /* end */
2385 };
2386
2387 static struct snd_kcontrol_new alc260_pc_beep_mixer[] = {
2388         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x07, 0x05, HDA_INPUT),
2389         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x07, 0x05, HDA_INPUT),
2390         { } /* end */
2391 };
2392
2393 static struct snd_kcontrol_new alc260_hp_3013_mixer[] = {
2394         HDA_CODEC_VOLUME("Front Playback Volume", 0x09, 0x0, HDA_OUTPUT),
2395         HDA_CODEC_MUTE("Front Playback Switch", 0x10, 0x0, HDA_OUTPUT),
2396         HDA_CODEC_VOLUME("Aux-In Playback Volume", 0x07, 0x06, HDA_INPUT),
2397         HDA_CODEC_MUTE("Aux-In Playback Switch", 0x07, 0x06, HDA_INPUT),
2398         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x08, 0x0, HDA_OUTPUT),
2399         HDA_CODEC_MUTE("Headphone Playback Switch", 0x15, 0x0, HDA_OUTPUT),
2400         HDA_CODEC_VOLUME_MONO("iSpeaker Playback Volume", 0x0a, 1, 0x0, HDA_OUTPUT),
2401         HDA_CODEC_MUTE_MONO("iSpeaker Playback Switch", 0x11, 1, 0x0, HDA_OUTPUT),
2402         { } /* end */
2403 };
2404
2405 static struct snd_kcontrol_new alc260_fujitsu_mixer[] = {
2406         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x08, 0x0, HDA_OUTPUT),
2407         HDA_BIND_MUTE("Headphone Playback Switch", 0x08, 2, HDA_INPUT),
2408         ALC_PINCTL_SWITCH("Headphone Amp Switch", 0x14, PIN_HP_AMP),
2409         HDA_CODEC_VOLUME("CD Playback Volume", 0x07, 0x04, HDA_INPUT),
2410         HDA_CODEC_MUTE("CD Playback Switch", 0x07, 0x04, HDA_INPUT),
2411         HDA_CODEC_VOLUME("Mic/Line Playback Volume", 0x07, 0x0, HDA_INPUT),
2412         HDA_CODEC_MUTE("Mic/Line Playback Switch", 0x07, 0x0, HDA_INPUT),
2413         HDA_CODEC_VOLUME("Beep Playback Volume", 0x07, 0x05, HDA_INPUT),
2414         HDA_CODEC_MUTE("Beep Playback Switch", 0x07, 0x05, HDA_INPUT),
2415         HDA_CODEC_VOLUME("Internal Speaker Playback Volume", 0x09, 0x0, HDA_OUTPUT),
2416         HDA_BIND_MUTE("Internal Speaker Playback Switch", 0x09, 2, HDA_INPUT),
2417         { } /* end */
2418 };
2419
2420 /* capture mixer elements */
2421 static struct snd_kcontrol_new alc260_capture_mixer[] = {
2422         HDA_CODEC_VOLUME("Capture Volume", 0x04, 0x0, HDA_INPUT),
2423         HDA_CODEC_MUTE("Capture Switch", 0x04, 0x0, HDA_INPUT),
2424         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x05, 0x0, HDA_INPUT),
2425         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x05, 0x0, HDA_INPUT),
2426         {
2427                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2428                 /* The multiple "Capture Source" controls confuse alsamixer
2429                  * So call somewhat different..
2430                  * FIXME: the controls appear in the "playback" view!
2431                  */
2432                 /* .name = "Capture Source", */
2433                 .name = "Input Source",
2434                 .count = 2,
2435                 .info = alc_mux_enum_info,
2436                 .get = alc_mux_enum_get,
2437                 .put = alc_mux_enum_put,
2438         },
2439         { } /* end */
2440 };
2441
2442 static struct snd_kcontrol_new alc260_capture_alt_mixer[] = {
2443         HDA_CODEC_VOLUME("Capture Volume", 0x05, 0x0, HDA_INPUT),
2444         HDA_CODEC_MUTE("Capture Switch", 0x05, 0x0, HDA_INPUT),
2445         {
2446                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2447                 /* The multiple "Capture Source" controls confuse alsamixer
2448                  * So call somewhat different..
2449                  * FIXME: the controls appear in the "playback" view!
2450                  */
2451                 /* .name = "Capture Source", */
2452                 .name = "Input Source",
2453                 .count = 1,
2454                 .info = alc_mux_enum_info,
2455                 .get = alc_mux_enum_get,
2456                 .put = alc_mux_enum_put,
2457         },
2458         { } /* end */
2459 };
2460
2461 /*
2462  * initialization verbs
2463  */
2464 static struct hda_verb alc260_init_verbs[] = {
2465         /* Line In pin widget for input */
2466         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2467         /* CD pin widget for input */
2468         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2469         /* Mic1 (rear panel) pin widget for input and vref at 80% */
2470         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2471         /* Mic2 (front panel) pin widget for input and vref at 80% */
2472         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2473         /* LINE-2 is used for line-out in rear */
2474         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2475         /* select line-out */
2476         {0x15, AC_VERB_SET_CONNECT_SEL, 0x00},
2477         /* LINE-OUT pin */
2478         {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2479         /* enable HP */
2480         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2481         /* enable Mono */
2482         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2483         /* mute capture amp left and right */
2484         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2485         /* set connection select to line in (default select for this ADC) */
2486         {0x04, AC_VERB_SET_CONNECT_SEL, 0x02},
2487         /* mute capture amp left and right */
2488         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2489         /* set connection select to line in (default select for this ADC) */
2490         {0x05, AC_VERB_SET_CONNECT_SEL, 0x02},
2491         /* set vol=0 Line-Out mixer amp left and right */
2492         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2493         /* unmute pin widget amp left and right (no gain on this amp) */
2494         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2495         /* set vol=0 HP mixer amp left and right */
2496         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2497         /* unmute pin widget amp left and right (no gain on this amp) */
2498         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2499         /* set vol=0 Mono mixer amp left and right */
2500         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2501         /* unmute pin widget amp left and right (no gain on this amp) */
2502         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2503         /* unmute LINE-2 out pin */
2504         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2505         /* Amp Indexes: CD = 0x04, Line In 1 = 0x02, Mic 1 = 0x00 & Line In 2 = 0x03 */
2506         /* mute CD */
2507         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2508         /* mute Line In */
2509         {0x07,  AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2510         /* mute Mic */
2511         {0x07,  AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2512         /* Amp Indexes: DAC = 0x01 & mixer = 0x00 */
2513         /* mute Front out path */
2514         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2515         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2516         /* mute Headphone out path */
2517         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2518         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2519         /* mute Mono out path */
2520         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2521         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2522         { }
2523 };
2524
2525 static struct hda_verb alc260_hp_init_verbs[] = {
2526         /* Headphone and output */
2527         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0},
2528         /* mono output */
2529         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2530         /* Mic1 (rear panel) pin widget for input and vref at 80% */
2531         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24},
2532         /* Mic2 (front panel) pin widget for input and vref at 80% */
2533         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24},
2534         /* Line In pin widget for input */
2535         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2536         /* Line-2 pin widget for output */
2537         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2538         /* CD pin widget for input */
2539         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2540         /* unmute amp left and right */
2541         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, 0x7000},
2542         /* set connection select to line in (default select for this ADC) */
2543         {0x04, AC_VERB_SET_CONNECT_SEL, 0x02},
2544         /* unmute Line-Out mixer amp left and right (volume = 0) */
2545         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
2546         /* mute pin widget amp left and right (no gain on this amp) */
2547         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0x0000},
2548         /* unmute HP mixer amp left and right (volume = 0) */
2549         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
2550         /* mute pin widget amp left and right (no gain on this amp) */
2551         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, 0x0000},
2552         /* Amp Indexes: CD = 0x04, Line In 1 = 0x02, Mic 1 = 0x00 & Line In 2 = 0x03 */
2553         /* unmute CD */
2554         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x04 << 8))},
2555         /* unmute Line In */
2556         {0x07,  AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x02 << 8))},
2557         /* unmute Mic */
2558         {0x07,  AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
2559         /* Amp Indexes: DAC = 0x01 & mixer = 0x00 */
2560         /* Unmute Front out path */
2561         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
2562         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
2563         /* Unmute Headphone out path */
2564         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
2565         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
2566         /* Unmute Mono out path */
2567         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
2568         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
2569         { }
2570 };
2571
2572 static struct hda_verb alc260_hp_3013_init_verbs[] = {
2573         /* Line out and output */
2574         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2575         /* mono output */
2576         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2577         /* Mic1 (rear panel) pin widget for input and vref at 80% */
2578         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24},
2579         /* Mic2 (front panel) pin widget for input and vref at 80% */
2580         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24},
2581         /* Line In pin widget for input */
2582         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2583         /* Headphone pin widget for output */
2584         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0},
2585         /* CD pin widget for input */
2586         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2587         /* unmute amp left and right */
2588         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, 0x7000},
2589         /* set connection select to line in (default select for this ADC) */
2590         {0x04, AC_VERB_SET_CONNECT_SEL, 0x02},
2591         /* unmute Line-Out mixer amp left and right (volume = 0) */
2592         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
2593         /* mute pin widget amp left and right (no gain on this amp) */
2594         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0x0000},
2595         /* unmute HP mixer amp left and right (volume = 0) */
2596         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
2597         /* mute pin widget amp left and right (no gain on this amp) */
2598         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, 0x0000},
2599         /* Amp Indexes: CD = 0x04, Line In 1 = 0x02, Mic 1 = 0x00 & Line In 2 = 0x03 */
2600         /* unmute CD */
2601         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x04 << 8))},
2602         /* unmute Line In */
2603         {0x07,  AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x02 << 8))},
2604         /* unmute Mic */
2605         {0x07,  AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
2606         /* Amp Indexes: DAC = 0x01 & mixer = 0x00 */
2607         /* Unmute Front out path */
2608         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
2609         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
2610         /* Unmute Headphone out path */
2611         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
2612         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
2613         /* Unmute Mono out path */
2614         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
2615         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
2616         { }
2617 };
2618
2619 /* Initialisation sequence for ALC260 as configured in Fujitsu S702x
2620  * laptops.
2621  */
2622 static struct hda_verb alc260_fujitsu_init_verbs[] = {
2623         /* Disable all GPIOs */
2624         {0x01, AC_VERB_SET_GPIO_MASK, 0},
2625         /* Internal speaker is connected to headphone pin */
2626         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2627         /* Headphone/Line-out jack connects to Line1 pin; make it an output */
2628         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2629         /* Mic/Line-in jack is connected to mic1 pin, so make it an input */
2630         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2631         /* Ensure all other unused pins are disabled and muted.
2632          * Note: trying to set widget 0x15 to anything blocks all audio
2633          * output for some reason, so just leave that at the default.
2634          */
2635         {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2636         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2637         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2638         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2639         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2640         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2641         /* Disable digital (SPDIF) pins */
2642         {0x03, AC_VERB_SET_DIGI_CONVERT_1, 0},
2643         {0x06, AC_VERB_SET_DIGI_CONVERT_1, 0},
2644
2645         /* Start with mixer outputs muted */
2646         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2647         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2648         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2649
2650         /* Unmute HP pin widget amp left and right (no equiv mixer ctrl) */
2651         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2652         /* Unmute Line1 pin widget amp left and right (no equiv mixer ctrl) */
2653         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2654         /* Unmute pin widget used for Line-in (no equiv mixer ctrl) */
2655         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2656
2657         /* Mute capture amp left and right */
2658         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2659         /* Set ADC connection select to line in (on mic1 pin) */
2660         {0x04, AC_VERB_SET_CONNECT_SEL, 0x00},
2661
2662         /* Mute all inputs to mixer widget (even unconnected ones) */
2663         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* mic1 pin */
2664         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* mic2 pin */
2665         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* line1 pin */
2666         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* line2 pin */
2667         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
2668         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)}, /* Beep-gen pin */
2669         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)}, /* Line-out pin */
2670         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)}, /* HP-pin pin */
2671
2672         { }
2673 };
2674
2675 static struct hda_pcm_stream alc260_pcm_analog_playback = {
2676         .substreams = 1,
2677         .channels_min = 2,
2678         .channels_max = 2,
2679 };
2680
2681 static struct hda_pcm_stream alc260_pcm_analog_capture = {
2682         .substreams = 1,
2683         .channels_min = 2,
2684         .channels_max = 2,
2685 };
2686
2687 #define alc260_pcm_digital_playback     alc880_pcm_digital_playback
2688 #define alc260_pcm_digital_capture      alc880_pcm_digital_capture
2689
2690 /*
2691  * for BIOS auto-configuration
2692  */
2693
2694 static int alc260_add_playback_controls(struct alc_spec *spec, hda_nid_t nid,
2695                                         const char *pfx)
2696 {
2697         hda_nid_t nid_vol;
2698         unsigned long vol_val, sw_val;
2699         char name[32];
2700         int err;
2701
2702         if (nid >= 0x0f && nid < 0x11) {
2703                 nid_vol = nid - 0x7;
2704                 vol_val = HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT);
2705                 sw_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2706         } else if (nid == 0x11) {
2707                 nid_vol = nid - 0x7;
2708                 vol_val = HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0, HDA_OUTPUT);
2709                 sw_val = HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT);
2710         } else if (nid >= 0x12 && nid <= 0x15) {
2711                 nid_vol = 0x08;
2712                 vol_val = HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT);
2713                 sw_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2714         } else
2715                 return 0; /* N/A */
2716         
2717         snprintf(name, sizeof(name), "%s Playback Volume", pfx);
2718         if ((err = add_control(spec, ALC_CTL_WIDGET_VOL, name, vol_val)) < 0)
2719                 return err;
2720         snprintf(name, sizeof(name), "%s Playback Switch", pfx);
2721         if ((err = add_control(spec, ALC_CTL_WIDGET_MUTE, name, sw_val)) < 0)
2722                 return err;
2723         return 1;
2724 }
2725
2726 /* add playback controls from the parsed DAC table */
2727 static int alc260_auto_create_multi_out_ctls(struct alc_spec *spec,
2728                                              const struct auto_pin_cfg *cfg)
2729 {
2730         hda_nid_t nid;
2731         int err;
2732
2733         spec->multiout.num_dacs = 1;
2734         spec->multiout.dac_nids = spec->private_dac_nids;
2735         spec->multiout.dac_nids[0] = 0x02;
2736
2737         nid = cfg->line_out_pins[0];
2738         if (nid) {
2739                 err = alc260_add_playback_controls(spec, nid, "Front");
2740                 if (err < 0)
2741                         return err;
2742         }
2743
2744         nid = cfg->speaker_pin;
2745         if (nid) {
2746                 err = alc260_add_playback_controls(spec, nid, "Speaker");
2747                 if (err < 0)
2748                         return err;
2749         }
2750
2751         nid = cfg->hp_pin;
2752         if (nid) {
2753                 err = alc260_add_playback_controls(spec, nid, "Headphone");
2754                 if (err < 0)
2755                         return err;
2756         }
2757         return 0;       
2758 }
2759
2760 /* create playback/capture controls for input pins */
2761 static int alc260_auto_create_analog_input_ctls(struct alc_spec *spec,
2762                                                 const struct auto_pin_cfg *cfg)
2763 {
2764         struct hda_input_mux *imux = &spec->private_imux;
2765         int i, err, idx;
2766
2767         for (i = 0; i < AUTO_PIN_LAST; i++) {
2768                 if (cfg->input_pins[i] >= 0x12) {
2769                         idx = cfg->input_pins[i] - 0x12;
2770                         err = new_analog_input(spec, cfg->input_pins[i],
2771                                                auto_pin_cfg_labels[i], idx, 0x07);
2772                         if (err < 0)
2773                                 return err;
2774                         imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
2775                         imux->items[imux->num_items].index = idx;
2776                         imux->num_items++;
2777                 }
2778                 if ((cfg->input_pins[i] >= 0x0f) && (cfg->input_pins[i] <= 0x10)){
2779                         idx = cfg->input_pins[i] - 0x09;
2780                         err = new_analog_input(spec, cfg->input_pins[i],
2781                                                auto_pin_cfg_labels[i], idx, 0x07);
2782                         if (err < 0)
2783                                 return err;
2784                         imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
2785                         imux->items[imux->num_items].index = idx;
2786                         imux->num_items++;
2787                 }
2788         }
2789         return 0;
2790 }
2791
2792 static void alc260_auto_set_output_and_unmute(struct hda_codec *codec,
2793                                               hda_nid_t nid, int pin_type,
2794                                               int sel_idx)
2795 {
2796         /* set as output */
2797         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
2798         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
2799         /* need the manual connection? */
2800         if (nid >= 0x12) {
2801                 int idx = nid - 0x12;
2802                 snd_hda_codec_write(codec, idx + 0x0b, 0,
2803                                     AC_VERB_SET_CONNECT_SEL, sel_idx);
2804                                     
2805         }
2806 }
2807
2808 static void alc260_auto_init_multi_out(struct hda_codec *codec)
2809 {
2810         struct alc_spec *spec = codec->spec;
2811         hda_nid_t nid;
2812
2813         nid = spec->autocfg.line_out_pins[0];   
2814         if (nid)
2815                 alc260_auto_set_output_and_unmute(codec, nid, PIN_OUT, 0);
2816         
2817         nid = spec->autocfg.speaker_pin;
2818         if (nid)
2819                 alc260_auto_set_output_and_unmute(codec, nid, PIN_OUT, 0);
2820
2821         nid = spec->autocfg.hp_pin;
2822         if (nid)
2823                 alc260_auto_set_output_and_unmute(codec, nid, PIN_OUT, 0);
2824 }       
2825
2826 #define ALC260_PIN_CD_NID               0x16
2827 static void alc260_auto_init_analog_input(struct hda_codec *codec)
2828 {
2829         struct alc_spec *spec = codec->spec;
2830         int i;
2831
2832         for (i = 0; i < AUTO_PIN_LAST; i++) {
2833                 hda_nid_t nid = spec->autocfg.input_pins[i];
2834                 if (nid >= 0x12) {
2835                         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2836                                             i <= AUTO_PIN_FRONT_MIC ? PIN_VREF80 : PIN_IN);
2837                         if (nid != ALC260_PIN_CD_NID)
2838                                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2839                                                     AMP_OUT_MUTE);
2840                 }
2841         }
2842 }
2843
2844 /*
2845  * generic initialization of ADC, input mixers and output mixers
2846  */
2847 static struct hda_verb alc260_volume_init_verbs[] = {
2848         /*
2849          * Unmute ADC0-1 and set the default input to mic-in
2850          */
2851         {0x04, AC_VERB_SET_CONNECT_SEL, 0x00},
2852         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2853         {0x05, AC_VERB_SET_CONNECT_SEL, 0x00},
2854         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2855         
2856         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2857          * mixer widget
2858          * Note: PASD motherboards uses the Line In 2 as the input for front panel
2859          * mic (mic 2)
2860          */
2861         /* Amp Indices: Mic1 = 0, Mic2 = 1, Line1 = 2, Line2 = 3, CD = 4 */
2862         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2863         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2864         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2865         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2866         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2867
2868         /*
2869          * Set up output mixers (0x08 - 0x0a)
2870          */
2871         /* set vol=0 to output mixers */
2872         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2873         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2874         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2875         /* set up input amps for analog loopback */
2876         /* Amp Indices: DAC = 0, mixer = 1 */
2877         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2878         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2879         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2880         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2881         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2882         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2883         
2884         { }
2885 };
2886
2887 static int alc260_parse_auto_config(struct hda_codec *codec)
2888 {
2889         struct alc_spec *spec = codec->spec;
2890         unsigned int wcap;
2891         int err;
2892         static hda_nid_t alc260_ignore[] = { 0x17, 0 };
2893
2894         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg,
2895                                                 alc260_ignore)) < 0)
2896                 return err;
2897         if ((err = alc260_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0)
2898                 return err;
2899         if (! spec->kctl_alloc)
2900                 return 0; /* can't find valid BIOS pin config */
2901         if ((err = alc260_auto_create_analog_input_ctls(spec, &spec->autocfg)) < 0)
2902                 return err;
2903
2904         spec->multiout.max_channels = 2;
2905
2906         if (spec->autocfg.dig_out_pin)
2907                 spec->multiout.dig_out_nid = ALC260_DIGOUT_NID;
2908         if (spec->kctl_alloc)
2909                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
2910
2911         spec->init_verbs[spec->num_init_verbs++] = alc260_volume_init_verbs;
2912
2913         spec->input_mux = &spec->private_imux;
2914
2915         /* check whether NID 0x04 is valid */
2916         wcap = get_wcaps(codec, 0x04);
2917         wcap = (wcap & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT; /* get type */
2918         if (wcap != AC_WID_AUD_IN) {
2919                 spec->adc_nids = alc260_adc_nids_alt;
2920                 spec->num_adc_nids = ARRAY_SIZE(alc260_adc_nids_alt);
2921                 spec->mixers[spec->num_mixers] = alc260_capture_alt_mixer;
2922         } else {
2923                 spec->adc_nids = alc260_adc_nids;
2924                 spec->num_adc_nids = ARRAY_SIZE(alc260_adc_nids);
2925                 spec->mixers[spec->num_mixers] = alc260_capture_mixer;
2926         }
2927         spec->num_mixers++;
2928
2929         return 1;
2930 }
2931
2932 /* init callback for auto-configuration model -- overriding the default init */
2933 static int alc260_auto_init(struct hda_codec *codec)
2934 {
2935         alc_init(codec);
2936         alc260_auto_init_multi_out(codec);
2937         alc260_auto_init_analog_input(codec);
2938         return 0;
2939 }
2940
2941 /*
2942  * ALC260 configurations
2943  */
2944 static struct hda_board_config alc260_cfg_tbl[] = {
2945         { .modelname = "basic", .config = ALC260_BASIC },
2946         { .modelname = "hp", .config = ALC260_HP },
2947         { .pci_subvendor = 0x103c, .pci_subdevice = 0x3010, .config = ALC260_HP },
2948         { .pci_subvendor = 0x103c, .pci_subdevice = 0x3011, .config = ALC260_HP },
2949         { .pci_subvendor = 0x103c, .pci_subdevice = 0x3012, .config = ALC260_HP },
2950         { .pci_subvendor = 0x103c, .pci_subdevice = 0x3013, .config = ALC260_HP_3013 },
2951         { .pci_subvendor = 0x103c, .pci_subdevice = 0x3014, .config = ALC260_HP },
2952         { .pci_subvendor = 0x103c, .pci_subdevice = 0x3015, .config = ALC260_HP },
2953         { .pci_subvendor = 0x103c, .pci_subdevice = 0x3016, .config = ALC260_HP },
2954         { .modelname = "fujitsu", .config = ALC260_FUJITSU_S702X },
2955         { .pci_subvendor = 0x10cf, .pci_subdevice = 0x1326, .config = ALC260_FUJITSU_S702X },
2956         { .modelname = "auto", .config = ALC260_AUTO },
2957         {}
2958 };
2959
2960 static struct alc_config_preset alc260_presets[] = {
2961         [ALC260_BASIC] = {
2962                 .mixers = { alc260_base_output_mixer,
2963                             alc260_input_mixer,
2964                             alc260_pc_beep_mixer,
2965                             alc260_capture_mixer },
2966                 .init_verbs = { alc260_init_verbs },
2967                 .num_dacs = ARRAY_SIZE(alc260_dac_nids),
2968                 .dac_nids = alc260_dac_nids,
2969                 .num_adc_nids = ARRAY_SIZE(alc260_adc_nids),
2970                 .adc_nids = alc260_adc_nids,
2971                 .num_channel_mode = ARRAY_SIZE(alc260_modes),
2972                 .channel_mode = alc260_modes,
2973                 .input_mux = &alc260_capture_source,
2974         },
2975         [ALC260_HP] = {
2976                 .mixers = { alc260_base_output_mixer,
2977                             alc260_input_mixer,
2978                             alc260_capture_alt_mixer },
2979                 .init_verbs = { alc260_hp_init_verbs },
2980                 .num_dacs = ARRAY_SIZE(alc260_dac_nids),
2981                 .dac_nids = alc260_dac_nids,
2982                 .num_adc_nids = ARRAY_SIZE(alc260_hp_adc_nids),
2983                 .adc_nids = alc260_hp_adc_nids,
2984                 .num_channel_mode = ARRAY_SIZE(alc260_modes),
2985                 .channel_mode = alc260_modes,
2986                 .input_mux = &alc260_capture_source,
2987         },
2988         [ALC260_HP_3013] = {
2989                 .mixers = { alc260_hp_3013_mixer,
2990                             alc260_input_mixer,
2991                             alc260_capture_alt_mixer },
2992                 .init_verbs = { alc260_hp_3013_init_verbs },
2993                 .num_dacs = ARRAY_SIZE(alc260_dac_nids),
2994                 .dac_nids = alc260_dac_nids,
2995                 .num_adc_nids = ARRAY_SIZE(alc260_hp_adc_nids),
2996                 .adc_nids = alc260_hp_adc_nids,
2997                 .num_channel_mode = ARRAY_SIZE(alc260_modes),
2998                 .channel_mode = alc260_modes,
2999                 .input_mux = &alc260_capture_source,
3000         },
3001         [ALC260_FUJITSU_S702X] = {
3002                 .mixers = { alc260_fujitsu_mixer,
3003                             alc260_capture_mixer },
3004                 .init_verbs = { alc260_fujitsu_init_verbs },
3005                 .num_dacs = ARRAY_SIZE(alc260_dac_nids),
3006                 .dac_nids = alc260_dac_nids,
3007                 .num_adc_nids = ARRAY_SIZE(alc260_adc_nids),
3008                 .adc_nids = alc260_adc_nids,
3009                 .num_channel_mode = ARRAY_SIZE(alc260_modes),
3010                 .channel_mode = alc260_modes,
3011                 .input_mux = &alc260_fujitsu_capture_source,
3012         },
3013 };
3014
3015 static int patch_alc260(struct hda_codec *codec)
3016 {
3017         struct alc_spec *spec;
3018         int err, board_config;
3019
3020         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3021         if (spec == NULL)
3022                 return -ENOMEM;
3023
3024         codec->spec = spec;
3025
3026         board_config = snd_hda_check_board_config(codec, alc260_cfg_tbl);
3027         if (board_config < 0 || board_config >= ALC260_MODEL_LAST) {
3028                 snd_printd(KERN_INFO "hda_codec: Unknown model for ALC260\n");
3029                 board_config = ALC260_AUTO;
3030         }
3031
3032         if (board_config == ALC260_AUTO) {
3033                 /* automatic parse from the BIOS config */
3034                 err = alc260_parse_auto_config(codec);
3035                 if (err < 0) {
3036                         alc_free(codec);
3037                         return err;
3038                 } else if (! err) {
3039                         printk(KERN_INFO "hda_codec: Cannot set up configuration from BIOS.  Using base mode...\n");
3040                         board_config = ALC260_BASIC;
3041                 }
3042         }
3043
3044         if (board_config != ALC260_AUTO)
3045                 setup_preset(spec, &alc260_presets[board_config]);
3046
3047         spec->stream_name_analog = "ALC260 Analog";
3048         spec->stream_analog_playback = &alc260_pcm_analog_playback;
3049         spec->stream_analog_capture = &alc260_pcm_analog_capture;
3050
3051         spec->stream_name_digital = "ALC260 Digital";
3052         spec->stream_digital_playback = &alc260_pcm_digital_playback;
3053         spec->stream_digital_capture = &alc260_pcm_digital_capture;
3054
3055         codec->patch_ops = alc_patch_ops;
3056         if (board_config == ALC260_AUTO)
3057                 codec->patch_ops.init = alc260_auto_init;
3058
3059         return 0;
3060 }
3061
3062
3063 /*
3064  * ALC882 support
3065  *
3066  * ALC882 is almost identical with ALC880 but has cleaner and more flexible
3067  * configuration.  Each pin widget can choose any input DACs and a mixer.
3068  * Each ADC is connected from a mixer of all inputs.  This makes possible
3069  * 6-channel independent captures.
3070  *
3071  * In addition, an independent DAC for the multi-playback (not used in this
3072  * driver yet).
3073  */
3074 #define ALC882_DIGOUT_NID       0x06
3075 #define ALC882_DIGIN_NID        0x0a
3076
3077 static struct hda_channel_mode alc882_ch_modes[1] = {
3078         { 8, NULL }
3079 };
3080
3081 static hda_nid_t alc882_dac_nids[4] = {
3082         /* front, rear, clfe, rear_surr */
3083         0x02, 0x03, 0x04, 0x05
3084 };
3085
3086 /* identical with ALC880 */
3087 #define alc882_adc_nids         alc880_adc_nids
3088 #define alc882_adc_nids_alt     alc880_adc_nids_alt
3089
3090 /* input MUX */
3091 /* FIXME: should be a matrix-type input source selection */
3092
3093 static struct hda_input_mux alc882_capture_source = {
3094         .num_items = 4,
3095         .items = {
3096                 { "Mic", 0x0 },
3097                 { "Front Mic", 0x1 },
3098                 { "Line", 0x2 },
3099                 { "CD", 0x4 },
3100         },
3101 };
3102
3103 #define alc882_mux_enum_info alc_mux_enum_info
3104 #define alc882_mux_enum_get alc_mux_enum_get
3105
3106 static int alc882_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
3107 {
3108         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3109         struct alc_spec *spec = codec->spec;
3110         const struct hda_input_mux *imux = spec->input_mux;
3111         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3112         static hda_nid_t capture_mixers[3] = { 0x24, 0x23, 0x22 };
3113         hda_nid_t nid = capture_mixers[adc_idx];
3114         unsigned int *cur_val = &spec->cur_mux[adc_idx];
3115         unsigned int i, idx;
3116
3117         idx = ucontrol->value.enumerated.item[0];
3118         if (idx >= imux->num_items)
3119                 idx = imux->num_items - 1;
3120         if (*cur_val == idx && ! codec->in_resume)
3121                 return 0;
3122         for (i = 0; i < imux->num_items; i++) {
3123                 unsigned int v = (i == idx) ? 0x7000 : 0x7080;
3124                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3125                                     v | (imux->items[i].index << 8));
3126         }
3127         *cur_val = idx;
3128         return 1;
3129 }
3130
3131 /*
3132  * 6ch mode
3133  */
3134 static struct hda_verb alc882_sixstack_ch6_init[] = {
3135         { 0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x00 },
3136         { 0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
3137         { 0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
3138         { 0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
3139         { } /* end */
3140 };
3141
3142 /*
3143  * 8ch mode
3144  */
3145 static struct hda_verb alc882_sixstack_ch8_init[] = {
3146         { 0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
3147         { 0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
3148         { 0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
3149         { 0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
3150         { } /* end */
3151 };
3152
3153 static struct hda_channel_mode alc882_sixstack_modes[2] = {
3154         { 6, alc882_sixstack_ch6_init },
3155         { 8, alc882_sixstack_ch8_init },
3156 };
3157
3158 /* Pin assignment: Front=0x14, Rear=0x15, CLFE=0x16, Side=0x17
3159  *                 Mic=0x18, Front Mic=0x19, Line-In=0x1a, HP=0x1b
3160  */
3161 static struct snd_kcontrol_new alc882_base_mixer[] = {
3162         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
3163         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
3164         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
3165         HDA_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
3166         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
3167         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
3168         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
3169         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
3170         HDA_CODEC_VOLUME("Side Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
3171         HDA_BIND_MUTE("Side Playback Switch", 0x0f, 2, HDA_INPUT),
3172         HDA_CODEC_MUTE("Headphone Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
3173         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
3174         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
3175         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
3176         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
3177         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
3178         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
3179         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x0b, 0x1, HDA_INPUT),
3180         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x0b, 0x1, HDA_INPUT),
3181         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x0b, 0x05, HDA_INPUT),
3182         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x0b, 0x05, HDA_INPUT),
3183         HDA_CODEC_VOLUME("Capture Volume", 0x07, 0x0, HDA_INPUT),
3184         HDA_CODEC_MUTE("Capture Switch", 0x07, 0x0, HDA_INPUT),
3185         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x08, 0x0, HDA_INPUT),
3186         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x08, 0x0, HDA_INPUT),
3187         HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x09, 0x0, HDA_INPUT),
3188         HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x09, 0x0, HDA_INPUT),
3189         {
3190                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3191                 /* .name = "Capture Source", */
3192                 .name = "Input Source",
3193                 .count = 3,
3194                 .info = alc882_mux_enum_info,
3195                 .get = alc882_mux_enum_get,
3196                 .put = alc882_mux_enum_put,
3197         },
3198         { } /* end */
3199 };
3200
3201 static struct snd_kcontrol_new alc882_chmode_mixer[] = {
3202         {
3203                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3204                 .name = "Channel Mode",
3205                 .info = alc_ch_mode_info,
3206                 .get = alc_ch_mode_get,
3207                 .put = alc_ch_mode_put,
3208         },
3209         { } /* end */
3210 };
3211
3212 static struct hda_verb alc882_init_verbs[] = {
3213         /* Front mixer: unmute input/output amp left and right (volume = 0) */
3214         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3215         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3216         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3217         /* Rear mixer */
3218         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3219         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3220         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3221         /* CLFE mixer */
3222         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3223         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3224         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3225         /* Side mixer */
3226         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3227         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3228         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3229
3230         /* Front Pin: output 0 (0x0c) */
3231         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3232         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3233         {0x14, AC_VERB_SET_CONNECT_SEL, 0x00},
3234         /* Rear Pin: output 1 (0x0d) */
3235         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3236         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3237         {0x15, AC_VERB_SET_CONNECT_SEL, 0x01},
3238         /* CLFE Pin: output 2 (0x0e) */
3239         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3240         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3241         {0x16, AC_VERB_SET_CONNECT_SEL, 0x02},
3242         /* Side Pin: output 3 (0x0f) */
3243         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3244         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3245         {0x17, AC_VERB_SET_CONNECT_SEL, 0x03},
3246         /* Mic (rear) pin: input vref at 80% */
3247         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3248         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3249         /* Front Mic pin: input vref at 80% */
3250         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3251         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3252         /* Line In pin: input */
3253         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3254         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3255         /* Line-2 In: Headphone output (output 0 - 0x0c) */
3256         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3257         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3258         {0x1b, AC_VERB_SET_CONNECT_SEL, 0x00},
3259         /* CD pin widget for input */
3260         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3261
3262         /* FIXME: use matrix-type input source selection */
3263         /* Mixer elements: 0x18, 19, 1a, 1b, 1c, 1d, 14, 15, 16, 17, 0b */
3264         /* Input mixer1: unmute Mic, F-Mic, Line, CD inputs */
3265         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3266         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
3267         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
3268         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3269         /* Input mixer2 */
3270         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3271         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
3272         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
3273         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3274         /* Input mixer3 */
3275         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3276         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
3277         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
3278         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3279         /* ADC1: mute amp left and right */
3280         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3281         {0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
3282         /* ADC2: mute amp left and right */
3283         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3284         {0x08, AC_VERB_SET_CONNECT_SEL, 0x00},
3285         /* ADC3: mute amp left and right */
3286         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3287         {0x09, AC_VERB_SET_CONNECT_SEL, 0x00},
3288
3289         { }
3290 };
3291
3292 /*
3293  * generic initialization of ADC, input mixers and output mixers
3294  */
3295 static struct hda_verb alc882_auto_init_verbs[] = {
3296         /*
3297          * Unmute ADC0-2 and set the default input to mic-in
3298          */
3299         {0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
3300         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3301         {0x08, AC_VERB_SET_CONNECT_SEL, 0x00},
3302         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3303         {0x09, AC_VERB_SET_CONNECT_SEL, 0x00},
3304         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3305
3306         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3307          * mixer widget
3308          * Note: PASD motherboards uses the Line In 2 as the input for front panel
3309          * mic (mic 2)
3310          */
3311         /* Amp Indices: Mic1 = 0, Mic2 = 1, Line1 = 2, Line2 = 3, CD = 4 */
3312         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3313         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3314         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3315         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3316         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
3317
3318         /*
3319          * Set up output mixers (0x0c - 0x0f)
3320          */
3321         /* set vol=0 to output mixers */
3322         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3323         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3324         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3325         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3326         /* set up input amps for analog loopback */
3327         /* Amp Indices: DAC = 0, mixer = 1 */
3328         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3329         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3330         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3331         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3332         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3333         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3334         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3335         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3336         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3337         {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3338
3339         /* FIXME: use matrix-type input source selection */
3340         /* Mixer elements: 0x18, 19, 1a, 1b, 1c, 1d, 14, 15, 16, 17, 0b */
3341         /* Input mixer1: unmute Mic, F-Mic, Line, CD inputs */
3342         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
3343         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x03 << 8))},
3344         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x02 << 8))},
3345         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x04 << 8))},
3346         /* Input mixer2 */
3347         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
3348         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x03 << 8))},
3349         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x02 << 8))},
3350         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x04 << 8))},
3351         /* Input mixer3 */
3352         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
3353         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x03 << 8))},
3354         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x02 << 8))},
3355         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x04 << 8))},
3356
3357         { }
3358 };
3359
3360 /* capture mixer elements */
3361 static struct snd_kcontrol_new alc882_capture_alt_mixer[] = {
3362         HDA_CODEC_VOLUME("Capture Volume", 0x08, 0x0, HDA_INPUT),
3363         HDA_CODEC_MUTE("Capture Switch", 0x08, 0x0, HDA_INPUT),
3364         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x09, 0x0, HDA_INPUT),
3365         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x09, 0x0, HDA_INPUT),
3366         {
3367                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3368                 /* The multiple "Capture Source" controls confuse alsamixer
3369                  * So call somewhat different..
3370                  * FIXME: the controls appear in the "playback" view!
3371                  */
3372                 /* .name = "Capture Source", */
3373                 .name = "Input Source",
3374                 .count = 2,
3375                 .info = alc882_mux_enum_info,
3376                 .get = alc882_mux_enum_get,
3377                 .put = alc882_mux_enum_put,
3378         },
3379         { } /* end */
3380 };
3381
3382 static struct snd_kcontrol_new alc882_capture_mixer[] = {
3383         HDA_CODEC_VOLUME("Capture Volume", 0x07, 0x0, HDA_INPUT),
3384         HDA_CODEC_MUTE("Capture Switch", 0x07, 0x0, HDA_INPUT),
3385         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x08, 0x0, HDA_INPUT),
3386         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x08, 0x0, HDA_INPUT),
3387         HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x09, 0x0, HDA_INPUT),
3388         HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x09, 0x0, HDA_INPUT),
3389         {
3390                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3391                 /* The multiple "Capture Source" controls confuse alsamixer
3392                  * So call somewhat different..
3393                  * FIXME: the controls appear in the "playback" view!
3394                  */
3395                 /* .name = "Capture Source", */
3396                 .name = "Input Source",
3397                 .count = 3,
3398                 .info = alc882_mux_enum_info,
3399                 .get = alc882_mux_enum_get,
3400                 .put = alc882_mux_enum_put,
3401         },
3402         { } /* end */
3403 };
3404
3405 /* pcm configuration: identiacal with ALC880 */
3406 #define alc882_pcm_analog_playback      alc880_pcm_analog_playback
3407 #define alc882_pcm_analog_capture       alc880_pcm_analog_capture
3408 #define alc882_pcm_digital_playback     alc880_pcm_digital_playback
3409 #define alc882_pcm_digital_capture      alc880_pcm_digital_capture
3410
3411 /*
3412  * configuration and preset
3413  */
3414 static struct hda_board_config alc882_cfg_tbl[] = {
3415         { .modelname = "3stack-dig", .config = ALC861_3ST_DIG },
3416         { .modelname = "6stack-dig", .config = ALC861_6ST_DIG },
3417         { .pci_subvendor = 0x1462, .pci_subdevice = 0x6668, .config = ALC882_6ST_DIG }, /* MSI  */
3418         { .pci_subvendor = 0x105b, .pci_subdevice = 0x6668, .config = ALC882_6ST_DIG }, /* Foxconn */
3419         { .pci_subvendor = 0x1019, .pci_subdevice = 0x6668, .config = ALC882_6ST_DIG }, /* ECS */
3420         { .modelname = "auto", .config = ALC861_AUTO },
3421         {}
3422 };
3423
3424 static struct alc_config_preset alc882_presets[] = {
3425         [ALC882_3ST_DIG] = {
3426                 .mixers = { alc882_base_mixer },
3427                 .init_verbs = { alc882_init_verbs },
3428                 .num_dacs = ARRAY_SIZE(alc882_dac_nids),
3429                 .dac_nids = alc882_dac_nids,
3430                 .dig_out_nid = ALC882_DIGOUT_NID,
3431                 .num_adc_nids = ARRAY_SIZE(alc882_adc_nids),
3432                 .adc_nids = alc882_adc_nids,
3433                 .dig_in_nid = ALC882_DIGIN_NID,
3434                 .num_channel_mode = ARRAY_SIZE(alc882_ch_modes),
3435                 .channel_mode = alc882_ch_modes,
3436                 .input_mux = &alc882_capture_source,
3437         },
3438         [ALC882_6ST_DIG] = {
3439                 .mixers = { alc882_base_mixer, alc882_chmode_mixer },
3440                 .init_verbs = { alc882_init_verbs },
3441                 .num_dacs = ARRAY_SIZE(alc882_dac_nids),
3442                 .dac_nids = alc882_dac_nids,
3443                 .dig_out_nid = ALC882_DIGOUT_NID,
3444                 .num_adc_nids = ARRAY_SIZE(alc882_adc_nids),
3445                 .adc_nids = alc882_adc_nids,
3446                 .dig_in_nid = ALC882_DIGIN_NID,
3447                 .num_channel_mode = ARRAY_SIZE(alc882_sixstack_modes),
3448                 .channel_mode = alc882_sixstack_modes,
3449                 .input_mux = &alc882_capture_source,
3450         },
3451 };
3452
3453
3454 /*
3455  * BIOS auto configuration
3456  */
3457 static void alc882_auto_set_output_and_unmute(struct hda_codec *codec,
3458                                               hda_nid_t nid, int pin_type,
3459                                               int dac_idx)
3460 {
3461         /* set as output */
3462         struct alc_spec *spec = codec->spec;
3463         int idx; 
3464         
3465         if (spec->multiout.dac_nids[dac_idx] == 0x25)
3466                 idx = 4;
3467         else
3468                 idx = spec->multiout.dac_nids[dac_idx] - 2;
3469
3470         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
3471         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
3472         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, idx);
3473
3474 }
3475
3476 static void alc882_auto_init_multi_out(struct hda_codec *codec)
3477 {
3478         struct alc_spec *spec = codec->spec;
3479         int i;
3480
3481         for (i = 0; i <= HDA_SIDE; i++) {
3482                 hda_nid_t nid = spec->autocfg.line_out_pins[i]; 
3483                 if (nid)
3484                         alc882_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
3485         }
3486 }
3487
3488 static void alc882_auto_init_hp_out(struct hda_codec *codec)
3489 {
3490         struct alc_spec *spec = codec->spec;
3491         hda_nid_t pin;
3492
3493         pin = spec->autocfg.hp_pin;
3494         if (pin) /* connect to front */
3495                 alc882_auto_set_output_and_unmute(codec, pin, PIN_HP, 0); /* use dac 0 */
3496 }
3497
3498 #define alc882_is_input_pin(nid)        alc880_is_input_pin(nid)
3499 #define ALC882_PIN_CD_NID               ALC880_PIN_CD_NID
3500
3501 static void alc882_auto_init_analog_input(struct hda_codec *codec)
3502 {
3503         struct alc_spec *spec = codec->spec;
3504         int i;
3505
3506         for (i = 0; i < AUTO_PIN_LAST; i++) {
3507                 hda_nid_t nid = spec->autocfg.input_pins[i];
3508                 if (alc882_is_input_pin(nid)) {
3509                         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
3510                                             i <= AUTO_PIN_FRONT_MIC ? PIN_VREF80 : PIN_IN);
3511                         if (nid != ALC882_PIN_CD_NID)
3512                                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3513                                                     AMP_OUT_MUTE);
3514                 }
3515         }
3516 }
3517
3518 /* almost identical with ALC880 parser... */
3519 static int alc882_parse_auto_config(struct hda_codec *codec)
3520 {
3521         struct alc_spec *spec = codec->spec;
3522         int err = alc880_parse_auto_config(codec);
3523
3524         if (err < 0)
3525                 return err;
3526         else if (err > 0)
3527                 /* hack - override the init verbs */
3528                 spec->init_verbs[0] = alc882_auto_init_verbs;
3529         return err;
3530 }
3531
3532 /* init callback for auto-configuration model -- overriding the default init */
3533 static int alc882_auto_init(struct hda_codec *codec)
3534 {
3535         alc_init(codec);
3536         alc882_auto_init_multi_out(codec);
3537         alc882_auto_init_hp_out(codec);
3538         alc882_auto_init_analog_input(codec);
3539         return 0;
3540 }
3541
3542 /*
3543  *  ALC882 Headphone poll in 3.5.1a or 3.5.2
3544  */
3545
3546 static int patch_alc882(struct hda_codec *codec)
3547 {
3548         struct alc_spec *spec;
3549         int err, board_config;
3550
3551         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3552         if (spec == NULL)
3553                 return -ENOMEM;
3554
3555         codec->spec = spec;
3556
3557         board_config = snd_hda_check_board_config(codec, alc882_cfg_tbl);
3558
3559         if (board_config < 0 || board_config >= ALC882_MODEL_LAST) {
3560                 printk(KERN_INFO "hda_codec: Unknown model for ALC882, trying auto-probe from BIOS...\n");
3561                 board_config = ALC882_AUTO;
3562         }
3563
3564         if (board_config == ALC882_AUTO) {
3565                 /* automatic parse from the BIOS config */
3566                 err = alc882_parse_auto_config(codec);
3567                 if (err < 0) {
3568                         alc_free(codec);
3569                         return err;
3570                 } else if (! err) {
3571                         printk(KERN_INFO "hda_codec: Cannot set up configuration from BIOS.  Using base mode...\n");
3572                         board_config = ALC882_3ST_DIG;
3573                 }
3574         }
3575
3576         if (board_config != ALC882_AUTO)
3577                 setup_preset(spec, &alc882_presets[board_config]);
3578
3579         spec->stream_name_analog = "ALC882 Analog";
3580         spec->stream_analog_playback = &alc882_pcm_analog_playback;
3581         spec->stream_analog_capture = &alc882_pcm_analog_capture;
3582
3583         spec->stream_name_digital = "ALC882 Digital";
3584         spec->stream_digital_playback = &alc882_pcm_digital_playback;
3585         spec->stream_digital_capture = &alc882_pcm_digital_capture;
3586
3587         if (! spec->adc_nids && spec->input_mux) {
3588                 /* check whether NID 0x07 is valid */
3589                 unsigned int wcap = get_wcaps(codec, 0x07);
3590                 wcap = (wcap & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT; /* get type */
3591                 if (wcap != AC_WID_AUD_IN) {
3592                         spec->adc_nids = alc882_adc_nids_alt;
3593                         spec->num_adc_nids = ARRAY_SIZE(alc882_adc_nids_alt);
3594                         spec->mixers[spec->num_mixers] = alc882_capture_alt_mixer;
3595                         spec->num_mixers++;
3596                 } else {
3597                         spec->adc_nids = alc882_adc_nids;
3598                         spec->num_adc_nids = ARRAY_SIZE(alc882_adc_nids);
3599                         spec->mixers[spec->num_mixers] = alc882_capture_mixer;
3600                         spec->num_mixers++;
3601                 }
3602         }
3603
3604         codec->patch_ops = alc_patch_ops;
3605         if (board_config == ALC882_AUTO)
3606                 codec->patch_ops.init = alc882_auto_init;
3607
3608         return 0;
3609 }
3610
3611 /*
3612  * ALC262 support
3613  */
3614
3615 #define ALC262_DIGOUT_NID       ALC880_DIGOUT_NID
3616 #define ALC262_DIGIN_NID        ALC880_DIGIN_NID
3617
3618 #define alc262_dac_nids         alc260_dac_nids
3619 #define alc262_adc_nids         alc882_adc_nids
3620 #define alc262_adc_nids_alt     alc882_adc_nids_alt
3621
3622 #define alc262_modes            alc260_modes
3623 #define alc262_capture_source   alc882_capture_source
3624
3625 static struct snd_kcontrol_new alc262_base_mixer[] = {
3626         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
3627         HDA_CODEC_MUTE("Front Playback Switch", 0x14, 0x0, HDA_OUTPUT),
3628         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
3629         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
3630         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
3631         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
3632         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
3633         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
3634         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x0b, 0x01, HDA_INPUT),
3635         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x0b, 0x01, HDA_INPUT),
3636         /* HDA_CODEC_VOLUME("PC Beep Playback Volume", 0x0b, 0x05, HDA_INPUT),
3637            HDA_CODEC_MUTE("PC Beelp Playback Switch", 0x0b, 0x05, HDA_INPUT), */
3638         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x0D, 0x0, HDA_OUTPUT),
3639         HDA_CODEC_MUTE("Headphone Playback Switch", 0x15, 0x0, HDA_OUTPUT),
3640         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
3641         HDA_CODEC_MUTE_MONO("Mono Playback Switch", 0x16, 2, 0x0, HDA_OUTPUT),
3642         HDA_CODEC_VOLUME("Capture Volume", 0x08, 0x0, HDA_INPUT),
3643         HDA_CODEC_MUTE("Capture Switch", 0x08, 0x0, HDA_INPUT),
3644         {
3645                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3646                 .name = "Capture Source",
3647                 .count = 1,
3648                 .info = alc882_mux_enum_info,
3649                 .get = alc882_mux_enum_get,
3650                 .put = alc882_mux_enum_put,
3651         },
3652         { } /* end */
3653 };                      
3654         
3655 #define alc262_capture_mixer            alc882_capture_mixer
3656 #define alc262_capture_alt_mixer        alc882_capture_alt_mixer
3657
3658 /*
3659  * generic initialization of ADC, input mixers and output mixers
3660  */
3661 static struct hda_verb alc262_init_verbs[] = {
3662         /*
3663          * Unmute ADC0-2 and set the default input to mic-in
3664          */
3665         {0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
3666         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3667         {0x08, AC_VERB_SET_CONNECT_SEL, 0x00},
3668         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3669         {0x09, AC_VERB_SET_CONNECT_SEL, 0x00},
3670         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3671
3672         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3673          * mixer widget
3674          * Note: PASD motherboards uses the Line In 2 as the input for front panel
3675          * mic (mic 2)
3676          */
3677         /* Amp Indices: Mic1 = 0, Mic2 = 1, Line1 = 2, Line2 = 3, CD = 4 */
3678         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3679         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3680         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3681         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3682         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
3683
3684         /*
3685          * Set up output mixers (0x0c - 0x0e)
3686          */
3687         /* set vol=0 to output mixers */
3688         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3689         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3690         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3691         /* set up input amps for analog loopback */
3692         /* Amp Indices: DAC = 0, mixer = 1 */
3693         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3694         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3695         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3696         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3697         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3698         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3699
3700         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3701         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0},
3702         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
3703         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24},
3704         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
3705         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
3706
3707         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0x0000},
3708         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0x0000},
3709         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, 0x0000},
3710         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, 0x0000},
3711         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, 0x0000},
3712         
3713         {0x14, AC_VERB_SET_CONNECT_SEL, 0x00},
3714         {0x15, AC_VERB_SET_CONNECT_SEL, 0x01},
3715         
3716         /* FIXME: use matrix-type input source selection */
3717         /* Mixer elements: 0x18, 19, 1a, 1b, 1c, 1d, 14, 15, 16, 17, 0b */
3718         /* Input mixer1: unmute Mic, F-Mic, Line, CD inputs */
3719         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
3720         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x03 << 8))},
3721         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x02 << 8))},
3722         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x04 << 8))},
3723         /* Input mixer2 */
3724         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
3725         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x03 << 8))},
3726         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x02 << 8))},
3727         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x04 << 8))},
3728         /* Input mixer3 */
3729         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
3730         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x03 << 8))},
3731         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x02 << 8))},
3732         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x04 << 8))},      
3733
3734         { }
3735 };
3736
3737 /* add playback controls from the parsed DAC table */
3738 static int alc262_auto_create_multi_out_ctls(struct alc_spec *spec, const struct auto_pin_cfg *cfg)
3739 {
3740         hda_nid_t nid;
3741         int err;
3742
3743         spec->multiout.num_dacs = 1;    /* only use one dac */
3744         spec->multiout.dac_nids = spec->private_dac_nids;
3745         spec->multiout.dac_nids[0] = 2;
3746
3747         nid = cfg->line_out_pins[0];
3748         if (nid) {
3749                 if ((err = add_control(spec, ALC_CTL_WIDGET_VOL, "Front Playback Volume",
3750                                        HDA_COMPOSE_AMP_VAL(0x0c, 3, 0, HDA_OUTPUT))) < 0)
3751                         return err;
3752                 if ((err = add_control(spec, ALC_CTL_WIDGET_MUTE, "Front Playback Switch",
3753                                        HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
3754                         return err;
3755         }
3756
3757         nid = cfg->speaker_pin;
3758         if (nid) {
3759                 if (nid == 0x16) {
3760                         if ((err = add_control(spec, ALC_CTL_WIDGET_VOL, "Speaker Playback Volume",
3761                                                HDA_COMPOSE_AMP_VAL(0x0e, 2, 0, HDA_OUTPUT))) < 0)
3762                                 return err;
3763                         if ((err = add_control(spec, ALC_CTL_WIDGET_MUTE, "Speaker Playback Switch",
3764                                                HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
3765                                 return err;
3766                 } else {
3767                         if (! cfg->line_out_pins[0])
3768                                 if ((err = add_control(spec, ALC_CTL_WIDGET_VOL, "Speaker Playback Volume",
3769                                                HDA_COMPOSE_AMP_VAL(0x0c, 3, 0, HDA_OUTPUT))) < 0)
3770                                         return err;
3771                         if ((err = add_control(spec, ALC_CTL_WIDGET_MUTE, "Speaker Playback Switch",
3772                                                HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
3773                                 return err;
3774                 }
3775         }
3776         nid = cfg->hp_pin;
3777         if (nid) {
3778                 /* spec->multiout.hp_nid = 2; */
3779                 if (nid == 0x16) {
3780                         if ((err = add_control(spec, ALC_CTL_WIDGET_VOL, "Headphone Playback Volume",
3781                                                HDA_COMPOSE_AMP_VAL(0x0e, 2, 0, HDA_OUTPUT))) < 0)
3782                                 return err;
3783                         if ((err = add_control(spec, ALC_CTL_WIDGET_MUTE, "Headphone Playback Switch",
3784                                                HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
3785                                 return err;
3786                 } else {
3787                         if (! cfg->line_out_pins[0])
3788                                 if ((err = add_control(spec, ALC_CTL_WIDGET_VOL, "Headphone Playback Volume",
3789                                                HDA_COMPOSE_AMP_VAL(0x0c, 3, 0, HDA_OUTPUT))) < 0)
3790                                         return err;
3791                         if ((err = add_control(spec, ALC_CTL_WIDGET_MUTE, "Headphone Playback Switch",
3792                                                HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
3793                                 return err;
3794                 }
3795         }
3796         return 0;       
3797 }
3798
3799 /* identical with ALC880 */
3800 #define alc262_auto_create_analog_input_ctls alc880_auto_create_analog_input_ctls
3801
3802 /*
3803  * generic initialization of ADC, input mixers and output mixers
3804  */
3805 static struct hda_verb alc262_volume_init_verbs[] = {
3806         /*
3807          * Unmute ADC0-2 and set the default input to mic-in
3808          */
3809         {0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
3810         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3811         {0x08, AC_VERB_SET_CONNECT_SEL, 0x00},
3812         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3813         {0x09, AC_VERB_SET_CONNECT_SEL, 0x00},
3814         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3815
3816         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3817          * mixer widget
3818          * Note: PASD motherboards uses the Line In 2 as the input for front panel
3819          * mic (mic 2)
3820          */
3821         /* Amp Indices: Mic1 = 0, Mic2 = 1, Line1 = 2, Line2 = 3, CD = 4 */
3822         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3823         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3824         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
3825         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
3826         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
3827
3828         /*
3829          * Set up output mixers (0x0c - 0x0f)
3830          */
3831         /* set vol=0 to output mixers */
3832         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3833         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3834         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3835         
3836         /* set up input amps for analog loopback */
3837         /* Amp Indices: DAC = 0, mixer = 1 */
3838         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3839         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3840         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3841         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3842         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3843         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3844
3845         /* FIXME: use matrix-type input source selection */
3846         /* Mixer elements: 0x18, 19, 1a, 1b, 1c, 1d, 14, 15, 16, 17, 0b */
3847         /* Input mixer1: unmute Mic, F-Mic, Line, CD inputs */
3848         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
3849         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x03 << 8))},
3850         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x02 << 8))},
3851         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x04 << 8))},
3852         /* Input mixer2 */
3853         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
3854         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x03 << 8))},
3855         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x02 << 8))},
3856         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x04 << 8))},
3857         /* Input mixer3 */
3858         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
3859         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x03 << 8))},
3860         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x02 << 8))},
3861         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x04 << 8))},
3862
3863         { }
3864 };
3865
3866 /* pcm configuration: identiacal with ALC880 */
3867 #define alc262_pcm_analog_playback      alc880_pcm_analog_playback
3868 #define alc262_pcm_analog_capture       alc880_pcm_analog_capture
3869 #define alc262_pcm_digital_playback     alc880_pcm_digital_playback
3870 #define alc262_pcm_digital_capture      alc880_pcm_digital_capture
3871
3872 /*
3873  * BIOS auto configuration
3874  */
3875 static int alc262_parse_auto_config(struct hda_codec *codec)
3876 {
3877         struct alc_spec *spec = codec->spec;
3878         int err;
3879         static hda_nid_t alc262_ignore[] = { 0x1d, 0 };
3880
3881         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg,
3882                                                 alc262_ignore)) < 0)
3883                 return err;
3884         if (! spec->autocfg.line_outs && ! spec->autocfg.speaker_pin &&
3885             ! spec->autocfg.hp_pin)
3886                 return 0; /* can't find valid BIOS pin config */
3887         if ((err = alc262_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
3888             (err = alc262_auto_create_analog_input_ctls(spec, &spec->autocfg)) < 0)
3889                 return err;
3890
3891         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3892
3893         if (spec->autocfg.dig_out_pin)
3894                 spec->multiout.dig_out_nid = ALC262_DIGOUT_NID;
3895         if (spec->autocfg.dig_in_pin)
3896                 spec->dig_in_nid = ALC262_DIGIN_NID;
3897
3898         if (spec->kctl_alloc)
3899                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
3900
3901         spec->init_verbs[spec->num_init_verbs++] = alc262_volume_init_verbs;
3902         spec->input_mux = &spec->private_imux;
3903
3904         return 1;
3905 }
3906
3907 #define alc262_auto_init_multi_out      alc882_auto_init_multi_out
3908 #define alc262_auto_init_hp_out         alc882_auto_init_hp_out
3909 #define alc262_auto_init_analog_input   alc882_auto_init_analog_input
3910
3911
3912 /* init callback for auto-configuration model -- overriding the default init */
3913 static int alc262_auto_init(struct hda_codec *codec)
3914 {
3915         alc_init(codec);
3916         alc262_auto_init_multi_out(codec);
3917         alc262_auto_init_hp_out(codec);
3918         alc262_auto_init_analog_input(codec);
3919         return 0;
3920 }
3921
3922 /*
3923  * configuration and preset
3924  */
3925 static struct hda_board_config alc262_cfg_tbl[] = {
3926         { .modelname = "basic", .config = ALC262_BASIC },
3927         { .modelname = "auto", .config = ALC262_AUTO },
3928         {}
3929 };
3930
3931 static struct alc_config_preset alc262_presets[] = {
3932         [ALC262_BASIC] = {
3933                 .mixers = { alc262_base_mixer },
3934                 .init_verbs = { alc262_init_verbs },
3935                 .num_dacs = ARRAY_SIZE(alc262_dac_nids),
3936                 .dac_nids = alc262_dac_nids,
3937                 .hp_nid = 0x03,
3938                 .num_channel_mode = ARRAY_SIZE(alc262_modes),
3939                 .channel_mode = alc262_modes,
3940                 .input_mux = &alc262_capture_source,
3941         },
3942 };
3943
3944 static int patch_alc262(struct hda_codec *codec)
3945 {
3946         struct alc_spec *spec;
3947         int board_config;
3948         int err;
3949
3950         spec = kcalloc(1, sizeof(*spec), GFP_KERNEL);
3951         if (spec == NULL)
3952                 return -ENOMEM;
3953
3954         codec->spec = spec;
3955 #if 0
3956         /* pshou 07/11/05  set a zero PCM sample to DAC when FIFO is under-run */
3957         {
3958         int tmp;
3959         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_COEF_INDEX, 7);
3960         tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
3961         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_COEF_INDEX, 7);
3962         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PROC_COEF, tmp | 0x80);
3963         }
3964 #endif
3965
3966         board_config = snd_hda_check_board_config(codec, alc262_cfg_tbl);
3967         if (board_config < 0 || board_config >= ALC262_MODEL_LAST) {
3968                 printk(KERN_INFO "hda_codec: Unknown model for ALC262, trying auto-probe from BIOS...\n");
3969                 board_config = ALC262_AUTO;
3970         }
3971
3972         if (board_config == ALC262_AUTO) {
3973                 /* automatic parse from the BIOS config */
3974                 err = alc262_parse_auto_config(codec);
3975                 if (err < 0) {
3976                         alc_free(codec);
3977                         return err;
3978                 } else if (! err) {
3979                         printk(KERN_INFO "hda_codec: Cannot set up configuration from BIOS.  Using base mode...\n");
3980                         board_config = ALC262_BASIC;
3981                 }
3982         }
3983
3984         if (board_config != ALC262_AUTO)
3985                 setup_preset(spec, &alc262_presets[board_config]);
3986
3987         spec->stream_name_analog = "ALC262 Analog";
3988         spec->stream_analog_playback = &alc262_pcm_analog_playback;
3989         spec->stream_analog_capture = &alc262_pcm_analog_capture;
3990                 
3991         spec->stream_name_digital = "ALC262 Digital";
3992         spec->stream_digital_playback = &alc262_pcm_digital_playback;
3993         spec->stream_digital_capture = &alc262_pcm_digital_capture;
3994
3995         if (! spec->adc_nids && spec->input_mux) {
3996                 /* check whether NID 0x07 is valid */
3997                 unsigned int wcap = get_wcaps(codec, 0x07);
3998
3999                 wcap = (wcap & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT; /* get type */
4000                 if (wcap != AC_WID_AUD_IN) {
4001                         spec->adc_nids = alc262_adc_nids_alt;
4002                         spec->num_adc_nids = ARRAY_SIZE(alc262_adc_nids_alt);
4003                         spec->mixers[spec->num_mixers] = alc262_capture_alt_mixer;
4004                         spec->num_mixers++;
4005                 } else {
4006                         spec->adc_nids = alc262_adc_nids;
4007                         spec->num_adc_nids = ARRAY_SIZE(alc262_adc_nids);
4008                         spec->mixers[spec->num_mixers] = alc262_capture_mixer;
4009                         spec->num_mixers++;
4010                 }
4011         }
4012
4013         codec->patch_ops = alc_patch_ops;
4014         if (board_config == ALC262_AUTO)
4015                 codec->patch_ops.init = alc262_auto_init;
4016         
4017         return 0;
4018 }
4019
4020
4021 /*
4022  *  ALC861 channel source setting (2/6 channel selection for 3-stack)
4023  */
4024
4025 /*
4026  * set the path ways for 2 channel output
4027  * need to set the codec line out and mic 1 pin widgets to inputs
4028  */
4029 static struct hda_verb alc861_threestack_ch2_init[] = {
4030         /* set pin widget 1Ah (line in) for input */
4031         { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
4032         /* set pin widget 18h (mic1/2) for input, for mic also enable the vref */
4033         { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
4034
4035         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb00c },
4036         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8)) }, //mic
4037         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x02 << 8)) }, //line in
4038         { } /* end */
4039 };
4040 /*
4041  * 6ch mode
4042  * need to set the codec line out and mic 1 pin widgets to outputs
4043  */
4044 static struct hda_verb alc861_threestack_ch6_init[] = {
4045         /* set pin widget 1Ah (line in) for output (Back Surround)*/
4046         { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
4047         /* set pin widget 18h (mic1) for output (CLFE)*/
4048         { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
4049
4050         { 0x0c, AC_VERB_SET_CONNECT_SEL, 0x00 },
4051         { 0x0d, AC_VERB_SET_CONNECT_SEL, 0x00 },
4052
4053         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080 },
4054         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x01 << 8)) }, //mic
4055         { 0x15, AC_VERB_SET_AMP_GAIN_MUTE, (0x7080 | (0x02 << 8)) }, //line in
4056         { } /* end */
4057 };
4058
4059 static struct hda_channel_mode alc861_threestack_modes[2] = {
4060         { 2, alc861_threestack_ch2_init },
4061         { 6, alc861_threestack_ch6_init },
4062 };
4063
4064 /* patch-ALC861 */
4065
4066 static struct snd_kcontrol_new alc861_base_mixer[] = {
4067         /* output mixer control */
4068         HDA_CODEC_MUTE("Front Playback Switch", 0x03, 0x0, HDA_OUTPUT),
4069         HDA_CODEC_MUTE("Surround Playback Switch", 0x06, 0x0, HDA_OUTPUT),
4070         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x05, 1, 0x0, HDA_OUTPUT),
4071         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x05, 2, 0x0, HDA_OUTPUT),
4072         HDA_CODEC_MUTE("Side Playback Switch", 0x04, 0x0, HDA_OUTPUT),
4073
4074         /*Input mixer control */
4075         /* HDA_CODEC_VOLUME("Input Playback Volume", 0x15, 0x0, HDA_OUTPUT),
4076            HDA_CODEC_MUTE("Input Playback Switch", 0x15, 0x0, HDA_OUTPUT), */
4077         HDA_CODEC_VOLUME("CD Playback Volume", 0x15, 0x0, HDA_INPUT),
4078         HDA_CODEC_MUTE("CD Playback Switch", 0x15, 0x0, HDA_INPUT),
4079         HDA_CODEC_VOLUME("Line Playback Volume", 0x15, 0x02, HDA_INPUT),
4080         HDA_CODEC_MUTE("Line Playback Switch", 0x15, 0x02, HDA_INPUT),
4081         HDA_CODEC_VOLUME("Mic Playback Volume", 0x15, 0x01, HDA_INPUT),
4082         HDA_CODEC_MUTE("Mic Playback Switch", 0x15, 0x01, HDA_INPUT),
4083         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x10, 0x01, HDA_OUTPUT),
4084         HDA_CODEC_MUTE("Headphone Playback Switch", 0x1a, 0x03, HDA_INPUT),
4085  
4086         /* Capture mixer control */
4087         HDA_CODEC_VOLUME("Capture Volume", 0x08, 0x0, HDA_INPUT),
4088         HDA_CODEC_MUTE("Capture Switch", 0x08, 0x0, HDA_INPUT),
4089         {
4090                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4091                 .name = "Capture Source",
4092                 .count = 1,
4093                 .info = alc_mux_enum_info,
4094                 .get = alc_mux_enum_get,
4095                 .put = alc_mux_enum_put,
4096         },
4097         { } /* end */
4098 };
4099
4100 static struct snd_kcontrol_new alc861_3ST_mixer[] = {
4101         /* output mixer control */
4102         HDA_CODEC_MUTE("Front Playback Switch", 0x03, 0x0, HDA_OUTPUT),
4103         HDA_CODEC_MUTE("Surround Playback Switch", 0x06, 0x0, HDA_OUTPUT),
4104         HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x05, 1, 0x0, HDA_OUTPUT),
4105         HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x05, 2, 0x0, HDA_OUTPUT),
4106         /*HDA_CODEC_MUTE("Side Playback Switch", 0x04, 0x0, HDA_OUTPUT), */
4107
4108         /* Input mixer control */
4109         /* HDA_CODEC_VOLUME("Input Playback Volume", 0x15, 0x0, HDA_OUTPUT),
4110            HDA_CODEC_MUTE("Input Playback Switch", 0x15, 0x0, HDA_OUTPUT), */
4111         HDA_CODEC_VOLUME("CD Playback Volume", 0x15, 0x0, HDA_INPUT),
4112         HDA_CODEC_MUTE("CD Playback Switch", 0x15, 0x0, HDA_INPUT),
4113         HDA_CODEC_VOLUME("Line Playback Volume", 0x15, 0x02, HDA_INPUT),
4114         HDA_CODEC_MUTE("Line Playback Switch", 0x15, 0x02, HDA_INPUT),
4115         HDA_CODEC_VOLUME("Mic Playback Volume", 0x15, 0x01, HDA_INPUT),
4116         HDA_CODEC_MUTE("Mic Playback Switch", 0x15, 0x01, HDA_INPUT),
4117         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x10, 0x01, HDA_OUTPUT),
4118         HDA_CODEC_MUTE("Headphone Playback Switch", 0x1a, 0x03, HDA_INPUT),
4119  
4120         /* Capture mixer control */
4121         HDA_CODEC_VOLUME("Capture Volume", 0x08, 0x0, HDA_INPUT),
4122         HDA_CODEC_MUTE("Capture Switch", 0x08, 0x0, HDA_INPUT),
4123         {
4124                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4125                 .name = "Capture Source",
4126                 .count = 1,
4127                 .info = alc_mux_enum_info,
4128                 .get = alc_mux_enum_get,
4129                 .put = alc_mux_enum_put,
4130         },
4131         {
4132                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4133                 .name = "Channel Mode",
4134                 .info = alc_ch_mode_info,
4135                 .get = alc_ch_mode_get,
4136                 .put = alc_ch_mode_put,
4137                 .private_value = ARRAY_SIZE(alc861_threestack_modes),
4138         },
4139         { } /* end */
4140 };                      
4141         
4142 /*
4143  * generic initialization of ADC, input mixers and output mixers
4144  */
4145 static struct hda_verb alc861_base_init_verbs[] = {
4146         /*
4147          * Unmute ADC0 and set the default input to mic-in
4148          */
4149         /* port-A for surround (rear panel) */
4150         { 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
4151         { 0x0e, AC_VERB_SET_CONNECT_SEL, 0x00 },
4152         /* port-B for mic-in (rear panel) with vref */
4153         { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
4154         /* port-C for line-in (rear panel) */
4155         { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
4156         /* port-D for Front */
4157         { 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
4158         { 0x0b, AC_VERB_SET_CONNECT_SEL, 0x00 },
4159         /* port-E for HP out (front panel) */
4160         { 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
4161         /* route front PCM to HP */
4162         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x01 },
4163         /* port-F for mic-in (front panel) with vref */
4164         { 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
4165         /* port-G for CLFE (rear panel) */
4166         { 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
4167         { 0x1f, AC_VERB_SET_CONNECT_SEL, 0x00 },
4168         /* port-H for side (rear panel) */
4169         { 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
4170         { 0x20, AC_VERB_SET_CONNECT_SEL, 0x00 },
4171         /* CD-in */
4172         { 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
4173         /* route front mic to ADC1*/
4174         {0x08, AC_VERB_SET_CONNECT_SEL, 0x00},
4175         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4176         
4177         /* Unmute DAC0~3 & spdif out*/
4178         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4179         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4180         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4181         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4182         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4183         
4184         /* Unmute Mixer 14 (mic) 1c (Line in)*/
4185         {0x014, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4186         {0x014, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4187         {0x01c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4188         {0x01c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4189         
4190         /* Unmute Stereo Mixer 15 */
4191         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4192         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4193         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
4194         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb00c          }, //Output 0~12 step
4195
4196         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4197         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4198         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4199         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4200         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4201         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4202         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4203         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4204         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)}, // hp used DAC 3 (Front)
4205         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
4206
4207         { }
4208 };
4209
4210 static struct hda_verb alc861_threestack_init_verbs[] = {
4211         /*
4212          * Unmute ADC0 and set the default input to mic-in
4213          */
4214         /* port-A for surround (rear panel) */
4215         { 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x00 },
4216         /* port-B for mic-in (rear panel) with vref */
4217         { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
4218         /* port-C for line-in (rear panel) */
4219         { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
4220         /* port-D for Front */
4221         { 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40 },
4222         { 0x0b, AC_VERB_SET_CONNECT_SEL, 0x00 },
4223         /* port-E for HP out (front panel) */
4224         { 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, 0xc0 },
4225         /* route front PCM to HP */
4226         { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x01 },
4227         /* port-F for mic-in (front panel) with vref */
4228         { 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24 },
4229         /* port-G for CLFE (rear panel) */
4230         { 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x00 },
4231         /* port-H for side (rear panel) */
4232         { 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x00 },
4233         /* CD-in */
4234         { 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20 },
4235         /* route front mic to ADC1*/
4236         {0x08, AC_VERB_SET_CONNECT_SEL, 0x00},
4237         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4238         /* Unmute DAC0~3 & spdif out*/
4239         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4240         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4241         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4242         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4243         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4244         
4245         /* Unmute Mixer 14 (mic) 1c (Line in)*/
4246         {0x014, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4247         {0x014, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4248         {0x01c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4249         {0x01c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4250         
4251         /* Unmute Stereo Mixer 15 */
4252         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4253         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4254         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
4255         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb00c          }, //Output 0~12 step
4256
4257         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4258         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4259         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4260         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4261         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4262         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4263         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4264         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4265         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)}, // hp used DAC 3 (Front)
4266         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
4267         { }
4268 };
4269 /*
4270  * generic initialization of ADC, input mixers and output mixers
4271  */
4272 static struct hda_verb alc861_auto_init_verbs[] = {
4273         /*
4274          * Unmute ADC0 and set the default input to mic-in
4275          */
4276 //      {0x08, AC_VERB_SET_CONNECT_SEL, 0x00},
4277         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4278         
4279         /* Unmute DAC0~3 & spdif out*/
4280         {0x03, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4281         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4282         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4283         {0x06, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4284         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4285         
4286         /* Unmute Mixer 14 (mic) 1c (Line in)*/
4287         {0x014, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4288         {0x014, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4289         {0x01c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4290         {0x01c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4291         
4292         /* Unmute Stereo Mixer 15 */
4293         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4294         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4295         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
4296         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb00c},
4297
4298         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4299         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4300         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4301         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4302         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4303         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4304         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4305         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4306
4307         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4308         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4309         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},    
4310         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},            
4311         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4312         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4313         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},    
4314         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},    
4315
4316         {0x08, AC_VERB_SET_CONNECT_SEL, 0x00},  // set Mic 1
4317
4318         { }
4319 };
4320
4321 /* pcm configuration: identiacal with ALC880 */
4322 #define alc861_pcm_analog_playback      alc880_pcm_analog_playback
4323 #define alc861_pcm_analog_capture       alc880_pcm_analog_capture
4324 #define alc861_pcm_digital_playback     alc880_pcm_digital_playback
4325 #define alc861_pcm_digital_capture      alc880_pcm_digital_capture
4326
4327
4328 #define ALC861_DIGOUT_NID       0x07
4329
4330 static struct hda_channel_mode alc861_8ch_modes[1] = {
4331         { 8, NULL }
4332 };
4333
4334 static hda_nid_t alc861_dac_nids[4] = {
4335         /* front, surround, clfe, side */
4336         0x03, 0x06, 0x05, 0x04
4337 };
4338
4339 static hda_nid_t alc861_adc_nids[1] = {
4340         /* ADC0-2 */
4341         0x08,
4342 };
4343
4344 static struct hda_input_mux alc861_capture_source = {
4345         .num_items = 5,
4346         .items = {
4347                 { "Mic", 0x0 },
4348                 { "Front Mic", 0x3 },
4349                 { "Line", 0x1 },
4350                 { "CD", 0x4 },
4351                 { "Mixer", 0x5 },
4352         },
4353 };
4354
4355 /* fill in the dac_nids table from the parsed pin configuration */
4356 static int alc861_auto_fill_dac_nids(struct alc_spec *spec, const struct auto_pin_cfg *cfg)
4357 {
4358         int i;
4359         hda_nid_t nid;
4360
4361         spec->multiout.dac_nids = spec->private_dac_nids;
4362         for (i = 0; i < cfg->line_outs; i++) {
4363                 nid = cfg->line_out_pins[i];
4364                 if (nid) {
4365                         if (i >= ARRAY_SIZE(alc861_dac_nids))
4366                                 continue;
4367                         spec->multiout.dac_nids[i] = alc861_dac_nids[i];
4368                 }
4369         }
4370         spec->multiout.num_dacs = cfg->line_outs;
4371         return 0;
4372 }
4373
4374 /* add playback controls from the parsed DAC table */
4375 static int alc861_auto_create_multi_out_ctls(struct alc_spec *spec,
4376                                              const struct auto_pin_cfg *cfg)
4377 {
4378         char name[32];
4379         static const char *chname[4] = { "Front", "Surround", NULL /*CLFE*/, "Side" };
4380         hda_nid_t nid;
4381         int i, idx, err;
4382
4383         for (i = 0; i < cfg->line_outs; i++) {
4384                 nid = spec->multiout.dac_nids[i];
4385                 if (! nid)
4386                         continue;
4387                 if (nid == 0x05) {
4388                         /* Center/LFE */
4389                         if ((err = add_control(spec, ALC_CTL_BIND_MUTE, "Center Playback Switch",
4390                                                HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
4391                                 return err;
4392                         if ((err = add_control(spec, ALC_CTL_BIND_MUTE, "LFE Playback Switch",
4393                                                HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
4394                                 return err;
4395                 } else {
4396                         for (idx = 0; idx < ARRAY_SIZE(alc861_dac_nids) - 1; idx++)
4397                                 if (nid == alc861_dac_nids[idx])
4398                                         break;
4399                         sprintf(name, "%s Playback Switch", chname[idx]);
4400                         if ((err = add_control(spec, ALC_CTL_BIND_MUTE, name,
4401                                                HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
4402                                 return err;
4403                 }
4404         }
4405         return 0;
4406 }
4407
4408 static int alc861_auto_create_hp_ctls(struct alc_spec *spec, hda_nid_t pin)
4409 {
4410         int err;
4411         hda_nid_t nid;
4412
4413         if (! pin)
4414                 return 0;
4415
4416         if ((pin >= 0x0b && pin <= 0x10) || pin == 0x1f || pin == 0x20) {
4417                 nid = 0x03;
4418                 if ((err = add_control(spec, ALC_CTL_WIDGET_MUTE, "Headphone Playback Switch",
4419                                        HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
4420                         return err;
4421                 spec->multiout.hp_nid = nid;
4422         }
4423         return 0;
4424 }
4425
4426 /* create playback/capture controls for input pins */
4427 static int alc861_auto_create_analog_input_ctls(struct alc_spec *spec, const struct auto_pin_cfg *cfg)
4428 {
4429         struct hda_input_mux *imux = &spec->private_imux;
4430         int i, err, idx, idx1;
4431
4432         for (i = 0; i < AUTO_PIN_LAST; i++) {
4433                 switch(cfg->input_pins[i]) {
4434                 case 0x0c:
4435                         idx1 = 1;
4436                         idx = 2;        // Line In
4437                         break;
4438                 case 0x0f:
4439                         idx1 = 2;
4440                         idx = 2;        // Line In
4441                         break;
4442                 case 0x0d:
4443                         idx1 = 0;
4444                         idx = 1;        // Mic In 
4445                         break;
4446                 case 0x10:      
4447                         idx1 = 3;
4448                         idx = 1;        // Mic In 
4449                         break;
4450                 case 0x11:
4451                         idx1 = 4;
4452                         idx = 0;        // CD
4453                         break;
4454                 default:
4455                         continue;
4456                 }
4457
4458                 err = new_analog_input(spec, cfg->input_pins[i],
4459                                        auto_pin_cfg_labels[i], idx, 0x15);
4460                 if (err < 0)
4461                         return err;
4462
4463                 imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
4464                 imux->items[imux->num_items].index = idx1;
4465                 imux->num_items++;      
4466         }
4467         return 0;
4468 }
4469
4470 static struct snd_kcontrol_new alc861_capture_mixer[] = {
4471         HDA_CODEC_VOLUME("Capture Volume", 0x08, 0x0, HDA_INPUT),
4472         HDA_CODEC_MUTE("Capture Switch", 0x08, 0x0, HDA_INPUT),
4473
4474         {
4475                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4476                 /* The multiple "Capture Source" controls confuse alsamixer
4477                  * So call somewhat different..
4478                  *FIXME: the controls appear in the "playback" view!
4479                  */
4480                 /* .name = "Capture Source", */
4481                 .name = "Input Source",
4482                 .count = 1,
4483                 .info = alc_mux_enum_info,
4484                 .get = alc_mux_enum_get,
4485                 .put = alc_mux_enum_put,
4486         },
4487         { } /* end */
4488 };
4489
4490 static void alc861_auto_set_output_and_unmute(struct hda_codec *codec, hda_nid_t nid,
4491                                               int pin_type, int dac_idx)
4492 {
4493         /* set as output */
4494
4495         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
4496         snd_hda_codec_write(codec, dac_idx, 0, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
4497
4498 }
4499
4500 static void alc861_auto_init_multi_out(struct hda_codec *codec)
4501 {
4502         struct alc_spec *spec = codec->spec;
4503         int i;
4504
4505         for (i = 0; i < spec->autocfg.line_outs; i++) {
4506                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
4507                 if (nid)
4508                         alc861_auto_set_output_and_unmute(codec, nid, PIN_OUT, spec->multiout.dac_nids[i]);
4509         }
4510 }
4511
4512 static void alc861_auto_init_hp_out(struct hda_codec *codec)
4513 {
4514         struct alc_spec *spec = codec->spec;
4515         hda_nid_t pin;
4516
4517         pin = spec->autocfg.hp_pin;
4518         if (pin) /* connect to front */
4519                 alc861_auto_set_output_and_unmute(codec, pin, PIN_HP, spec->multiout.dac_nids[0]);
4520 }
4521
4522 static void alc861_auto_init_analog_input(struct hda_codec *codec)
4523 {
4524         struct alc_spec *spec = codec->spec;
4525         int i;
4526
4527         for (i = 0; i < AUTO_PIN_LAST; i++) {
4528                 hda_nid_t nid = spec->autocfg.input_pins[i];
4529                 if ((nid>=0x0c) && (nid <=0x11)) {
4530                         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
4531                                             i <= AUTO_PIN_FRONT_MIC ? PIN_VREF80 : PIN_IN);
4532                 }
4533         }
4534 }
4535
4536 /* parse the BIOS configuration and set up the alc_spec */
4537 /* return 1 if successful, 0 if the proper config is not found, or a negative error code */
4538 static int alc861_parse_auto_config(struct hda_codec *codec)
4539 {
4540         struct alc_spec *spec = codec->spec;
4541         int err;
4542         static hda_nid_t alc861_ignore[] = { 0x1d, 0 };
4543
4544         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg,
4545                                                 alc861_ignore)) < 0)
4546                 return err;
4547         if (! spec->autocfg.line_outs && ! spec->autocfg.speaker_pin &&
4548             ! spec->autocfg.hp_pin)
4549                 return 0; /* can't find valid BIOS pin config */
4550
4551         if ((err = alc861_auto_fill_dac_nids(spec, &spec->autocfg)) < 0 ||
4552             (err = alc861_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
4553             (err = alc861_auto_create_hp_ctls(spec, spec->autocfg.hp_pin)) < 0 ||
4554             (err = alc861_auto_create_analog_input_ctls(spec, &spec->autocfg)) < 0)
4555                 return err;
4556
4557         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4558
4559         if (spec->autocfg.dig_out_pin)
4560                 spec->multiout.dig_out_nid = ALC861_DIGOUT_NID;
4561
4562         if (spec->kctl_alloc)
4563                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
4564
4565         spec->init_verbs[spec->num_init_verbs++] = alc861_auto_init_verbs;
4566
4567         spec->input_mux = &spec->private_imux;
4568
4569         spec->adc_nids = alc861_adc_nids;
4570         spec->num_adc_nids = ARRAY_SIZE(alc861_adc_nids);
4571         spec->mixers[spec->num_mixers] = alc861_capture_mixer;
4572         spec->num_mixers++;
4573
4574         return 1;
4575 }
4576
4577 /* init callback for auto-configuration model -- overriding the default init */
4578 static int alc861_auto_init(struct hda_codec *codec)
4579 {
4580         alc_init(codec);
4581         alc861_auto_init_multi_out(codec);
4582         alc861_auto_init_hp_out(codec);
4583         alc861_auto_init_analog_input(codec);
4584
4585         return 0;
4586 }
4587
4588
4589 /*
4590  * configuration and preset
4591  */
4592 static struct hda_board_config alc861_cfg_tbl[] = {
4593         { .modelname = "3stack", .config = ALC861_3ST },
4594         { .pci_subvendor = 0x8086, .pci_subdevice = 0xd600, .config = ALC861_3ST },
4595         { .modelname = "3stack-dig", .config = ALC861_3ST_DIG },
4596         { .modelname = "6stack-dig", .config = ALC861_6ST_DIG },
4597         { .modelname = "auto", .config = ALC861_AUTO },
4598         {}
4599 };
4600
4601 static struct alc_config_preset alc861_presets[] = {
4602         [ALC861_3ST] = {
4603                 .mixers = { alc861_3ST_mixer },
4604                 .init_verbs = { alc861_threestack_init_verbs },
4605                 .num_dacs = ARRAY_SIZE(alc861_dac_nids),
4606                 .dac_nids = alc861_dac_nids,
4607                 .num_channel_mode = ARRAY_SIZE(alc861_threestack_modes),
4608                 .channel_mode = alc861_threestack_modes,
4609                 .num_adc_nids = ARRAY_SIZE(alc861_adc_nids),
4610                 .adc_nids = alc861_adc_nids,
4611                 .input_mux = &alc861_capture_source,
4612         },
4613         [ALC861_3ST_DIG] = {
4614                 .mixers = { alc861_base_mixer },
4615                 .init_verbs = { alc861_threestack_init_verbs },
4616                 .num_dacs = ARRAY_SIZE(alc861_dac_nids),
4617                 .dac_nids = alc861_dac_nids,
4618                 .dig_out_nid = ALC861_DIGOUT_NID,
4619                 .num_channel_mode = ARRAY_SIZE(alc861_threestack_modes),
4620                 .channel_mode = alc861_threestack_modes,
4621                 .num_adc_nids = ARRAY_SIZE(alc861_adc_nids),
4622                 .adc_nids = alc861_adc_nids,
4623                 .input_mux = &alc861_capture_source,
4624         },
4625         [ALC861_6ST_DIG] = {
4626                 .mixers = { alc861_base_mixer },
4627                 .init_verbs = { alc861_base_init_verbs },
4628                 .num_dacs = ARRAY_SIZE(alc861_dac_nids),
4629                 .dac_nids = alc861_dac_nids,
4630                 .dig_out_nid = ALC861_DIGOUT_NID,
4631                 .num_channel_mode = ARRAY_SIZE(alc861_8ch_modes),
4632                 .channel_mode = alc861_8ch_modes,
4633                 .num_adc_nids = ARRAY_SIZE(alc861_adc_nids),
4634                 .adc_nids = alc861_adc_nids,
4635                 .input_mux = &alc861_capture_source,
4636         },
4637 };      
4638
4639
4640 static int patch_alc861(struct hda_codec *codec)
4641 {
4642         struct alc_spec *spec;
4643         int board_config;
4644         int err;
4645
4646         spec = kcalloc(1, sizeof(*spec), GFP_KERNEL);
4647         if (spec == NULL)
4648                 return -ENOMEM;
4649
4650         codec->spec = spec;     
4651
4652         board_config = snd_hda_check_board_config(codec, alc861_cfg_tbl);
4653         if (board_config < 0 || board_config >= ALC861_MODEL_LAST) {
4654                 printk(KERN_INFO "hda_codec: Unknown model for ALC861, trying auto-probe from BIOS...\n");
4655                 board_config = ALC861_AUTO;
4656         }
4657
4658         if (board_config == ALC861_AUTO) {
4659                 /* automatic parse from the BIOS config */
4660                 err = alc861_parse_auto_config(codec);
4661                 if (err < 0) {
4662                         alc_free(codec);
4663                         return err;
4664                 } else if (! err) {
4665                         printk(KERN_INFO "hda_codec: Cannot set up configuration from BIOS.  Using base mode...\n");
4666                    board_config = ALC861_3ST_DIG;
4667                 }
4668         }
4669
4670         if (board_config != ALC861_AUTO)
4671                 setup_preset(spec, &alc861_presets[board_config]);
4672
4673         spec->stream_name_analog = "ALC861 Analog";
4674         spec->stream_analog_playback = &alc861_pcm_analog_playback;
4675         spec->stream_analog_capture = &alc861_pcm_analog_capture;
4676
4677         spec->stream_name_digital = "ALC861 Digital";
4678         spec->stream_digital_playback = &alc861_pcm_digital_playback;
4679         spec->stream_digital_capture = &alc861_pcm_digital_capture;
4680
4681         codec->patch_ops = alc_patch_ops;
4682         if (board_config == ALC861_AUTO)
4683                 codec->patch_ops.init = alc861_auto_init;
4684                 
4685         return 0;
4686 }
4687
4688 /*
4689  * patch entries
4690  */
4691 struct hda_codec_preset snd_hda_preset_realtek[] = {
4692         { .id = 0x10ec0260, .name = "ALC260", .patch = patch_alc260 },
4693         { .id = 0x10ec0262, .name = "ALC262", .patch = patch_alc262 },
4694         { .id = 0x10ec0880, .name = "ALC880", .patch = patch_alc880 },
4695         { .id = 0x10ec0882, .name = "ALC882", .patch = patch_alc882 },
4696         { .id = 0x10ec0883, .name = "ALC883", .patch = patch_alc882 },
4697         { .id = 0x10ec0885, .name = "ALC885", .patch = patch_alc882 },
4698         { .id = 0x10ec0861, .name = "ALC861", .patch = patch_alc861 },
4699         {} /* terminator */
4700 };