[ALSA] hda-codec - Fix oops with ALC880
[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 PeiSen Hou <pshou@realtek.com.tw>
7  *                    Takashi Iwai <tiwai@suse.de>
8  *
9  *  This driver is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This driver is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  */
23
24 #include <sound/driver.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/pci.h>
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include "hda_local.h"
32
33
34 /* ALC880 board config type */
35 enum {
36         ALC880_3ST,
37         ALC880_3ST_DIG,
38         ALC880_5ST,
39         ALC880_5ST_DIG,
40         ALC880_W810,
41         ALC880_Z71V,
42         ALC880_AUTO,
43         ALC880_6ST_DIG,
44         ALC880_F1734,
45         ALC880_ASUS,
46         ALC880_ASUS_DIG,
47         ALC880_ASUS_W1V,
48         ALC880_UNIWILL_DIG,
49 #ifdef CONFIG_SND_DEBUG
50         ALC880_TEST,
51 #endif
52         ALC880_MODEL_LAST /* last tag */
53 };
54
55 /* ALC260 models */
56 enum {
57         ALC260_BASIC,
58         ALC260_HP,
59         ALC260_MODEL_LAST /* last tag */
60 };
61
62 /* amp values */
63 #define AMP_IN_MUTE(idx)        (0x7080 | ((idx)<<8))
64 #define AMP_IN_UNMUTE(idx)      (0x7000 | ((idx)<<8))
65 #define AMP_OUT_MUTE    0xb080
66 #define AMP_OUT_UNMUTE  0xb000
67 #define AMP_OUT_ZERO    0xb000
68 /* pinctl values */
69 #define PIN_IN          0x20
70 #define PIN_VREF80      0x24
71 #define PIN_VREF50      0x21
72 #define PIN_OUT         0x40
73 #define PIN_HP          0xc0
74
75 struct alc_spec {
76         /* codec parameterization */
77         snd_kcontrol_new_t *mixers[3];  /* mixer arrays */
78         unsigned int num_mixers;
79
80         const struct hda_verb *init_verbs[3];   /* initialization verbs
81                                                  * don't forget NULL termination!
82                                                  */
83         unsigned int num_init_verbs;
84
85         char *stream_name_analog;       /* analog PCM stream */
86         struct hda_pcm_stream *stream_analog_playback;
87         struct hda_pcm_stream *stream_analog_capture;
88
89         char *stream_name_digital;      /* digital PCM stream */ 
90         struct hda_pcm_stream *stream_digital_playback;
91         struct hda_pcm_stream *stream_digital_capture;
92
93         /* playback */
94         struct hda_multi_out multiout;  /* playback set-up
95                                          * max_channels, dacs must be set
96                                          * dig_out_nid and hp_nid are optional
97                                          */
98
99         /* capture */
100         unsigned int num_adc_nids;
101         hda_nid_t *adc_nids;
102         hda_nid_t dig_in_nid;           /* digital-in NID; optional */
103
104         /* capture source */
105         const struct hda_input_mux *input_mux;
106         unsigned int cur_mux[3];
107
108         /* channel model */
109         const struct alc_channel_mode *channel_mode;
110         int num_channel_mode;
111
112         /* PCM information */
113         struct hda_pcm pcm_rec[2];      /* used in alc_build_pcms() */
114
115         struct semaphore bind_mutex;    /* for bound controls */
116
117         /* dynamic controls, init_verbs and input_mux */
118         struct auto_pin_cfg autocfg;
119         unsigned int num_kctl_alloc, num_kctl_used;
120         snd_kcontrol_new_t *kctl_alloc;
121         struct hda_input_mux private_imux;
122         hda_nid_t private_dac_nids[4];
123 };
124
125
126 /*
127  * input MUX handling
128  */
129 static int alc_mux_enum_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
130 {
131         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
132         struct alc_spec *spec = codec->spec;
133         return snd_hda_input_mux_info(spec->input_mux, uinfo);
134 }
135
136 static int alc_mux_enum_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
137 {
138         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
139         struct alc_spec *spec = codec->spec;
140         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
141
142         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
143         return 0;
144 }
145
146 static int alc_mux_enum_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
147 {
148         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
149         struct alc_spec *spec = codec->spec;
150         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
151         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
152                                      spec->adc_nids[adc_idx], &spec->cur_mux[adc_idx]);
153 }
154
155
156 /*
157  * channel mode setting
158  */
159 struct alc_channel_mode {
160         int channels;
161         const struct hda_verb *sequence;
162 };
163
164 static int alc880_ch_mode_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
165 {
166         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
167         struct alc_spec *spec = codec->spec;
168         int items = kcontrol->private_value ? (int)kcontrol->private_value : 2;
169
170         snd_assert(spec->channel_mode, return -ENXIO);
171         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
172         uinfo->count = 1;
173         uinfo->value.enumerated.items = items;
174         if (uinfo->value.enumerated.item >= items)
175                 uinfo->value.enumerated.item = items - 1;
176         sprintf(uinfo->value.enumerated.name, "%dch",
177                 spec->channel_mode[uinfo->value.enumerated.item].channels);
178         return 0;
179 }
180
181 static int alc880_ch_mode_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
182 {
183         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
184         struct alc_spec *spec = codec->spec;
185         int items = kcontrol->private_value ? (int)kcontrol->private_value : 2;
186         int i;
187
188         snd_assert(spec->channel_mode, return -ENXIO);
189         for (i = 0; i < items; i++) {
190                 if (spec->multiout.max_channels == spec->channel_mode[i].channels) {
191                         ucontrol->value.enumerated.item[0] = i;
192                         break;
193                 }
194         }
195         return 0;
196 }
197
198 static int alc880_ch_mode_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
199 {
200         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
201         struct alc_spec *spec = codec->spec;
202         int mode;
203
204         snd_assert(spec->channel_mode, return -ENXIO);
205         mode = ucontrol->value.enumerated.item[0] ? 1 : 0;
206         if (spec->multiout.max_channels == spec->channel_mode[mode].channels &&
207             ! codec->in_resume)
208                 return 0;
209
210         /* change the current channel setting */
211         spec->multiout.max_channels = spec->channel_mode[mode].channels;
212         if (spec->channel_mode[mode].sequence)
213                 snd_hda_sequence_write(codec, spec->channel_mode[mode].sequence);
214
215         return 1;
216 }
217
218
219 /*
220  * bound volume controls
221  *
222  * bind multiple volumes (# indices, from 0)
223  */
224
225 #define AMP_VAL_IDX_SHIFT       19
226 #define AMP_VAL_IDX_MASK        (0x0f<<19)
227
228 static int alc_bind_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
229 {
230         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
231         struct alc_spec *spec = codec->spec;
232         unsigned long pval;
233
234         down(&spec->bind_mutex);
235         pval = kcontrol->private_value;
236         kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
237         snd_hda_mixer_amp_switch_info(kcontrol, uinfo);
238         kcontrol->private_value = pval;
239         up(&spec->bind_mutex);
240         return 0;
241 }
242
243 static int alc_bind_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
244 {
245         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
246         struct alc_spec *spec = codec->spec;
247         unsigned long pval;
248
249         down(&spec->bind_mutex);
250         pval = kcontrol->private_value;
251         kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
252         snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
253         kcontrol->private_value = pval;
254         up(&spec->bind_mutex);
255         return 0;
256 }
257
258 static int alc_bind_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
259 {
260         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
261         struct alc_spec *spec = codec->spec;
262         unsigned long pval;
263         int i, indices, change = 0;
264
265         down(&spec->bind_mutex);
266         pval = kcontrol->private_value;
267         indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
268         for (i = 0; i < indices; i++) {
269                 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | (i << AMP_VAL_IDX_SHIFT);
270                 change |= snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
271         }
272         kcontrol->private_value = pval;
273         up(&spec->bind_mutex);
274         return change;
275 }
276
277 #define ALC_BIND_MUTE_MONO(xname, nid, channel, indices, direction) \
278         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
279           .info = alc_bind_switch_info, \
280           .get = alc_bind_switch_get, \
281           .put = alc_bind_switch_put, \
282           .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, indices, direction) }
283
284 #define ALC_BIND_MUTE(xname,nid,indices,dir) ALC_BIND_MUTE_MONO(xname,nid,3,indices,dir)
285
286
287 /*
288  * ALC880 3-stack model
289  *
290  * DAC: Front = 0x02 (0x0c), Surr = 0x05 (0x0f), CLFE = 0x04 (0x0e)
291  * Pin assignment: Front = 0x14, Line-In/Surr = 0x1a, Mic/CLFE = 0x18, F-Mic = 0x1b
292  *                 HP = 0x19
293  */
294
295 static hda_nid_t alc880_dac_nids[4] = {
296         /* front, rear, clfe, rear_surr */
297         0x02, 0x05, 0x04, 0x03
298 };
299
300 static hda_nid_t alc880_adc_nids[3] = {
301         /* ADC0-2 */
302         0x07, 0x08, 0x09,
303 };
304
305 /* The datasheet says the node 0x07 is connected from inputs,
306  * but it shows zero connection in the real implementation on some devices.
307  */
308 static hda_nid_t alc880_adc_nids_alt[2] = {
309         /* ADC1-2 */
310         0x08, 0x09,
311 };
312
313 #define ALC880_DIGOUT_NID       0x06
314 #define ALC880_DIGIN_NID        0x0a
315
316 static struct hda_input_mux alc880_capture_source = {
317         .num_items = 4,
318         .items = {
319                 { "Mic", 0x0 },
320                 { "Front Mic", 0x3 },
321                 { "Line", 0x2 },
322                 { "CD", 0x4 },
323         },
324 };
325
326 /* channel source setting (2/6 channel selection for 3-stack) */
327 /* 2ch mode */
328 static struct hda_verb alc880_threestack_ch2_init[] = {
329         /* set line-in to input, mute it */
330         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
331         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
332         /* set mic-in to input vref 80%, mute it */
333         { 0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
334         { 0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
335         { } /* end */
336 };
337
338 /* 6ch mode */
339 static struct hda_verb alc880_threestack_ch6_init[] = {
340         /* set line-in to output, unmute it */
341         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
342         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
343         /* set mic-in to output, unmute it */
344         { 0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
345         { 0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
346         { } /* end */
347 };
348
349 static struct alc_channel_mode alc880_threestack_modes[2] = {
350         { 2, alc880_threestack_ch2_init },
351         { 6, alc880_threestack_ch6_init },
352 };
353
354 static snd_kcontrol_new_t alc880_three_stack_mixer[] = {
355         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
356         ALC_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
357         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
358         ALC_BIND_MUTE("Surround Playback Switch", 0x0f, 2, HDA_INPUT),
359         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
360         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
361         ALC_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
362         ALC_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
363         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
364         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
365         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
366         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
367         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
368         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
369         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x0b, 0x3, HDA_INPUT),
370         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x0b, 0x3, HDA_INPUT),
371         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x0b, 0x05, HDA_INPUT),
372         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x0b, 0x05, HDA_INPUT),
373         HDA_CODEC_MUTE("Headphone Playback Switch", 0x19, 0x0, HDA_OUTPUT),
374         {
375                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
376                 .name = "Channel Mode",
377                 .info = alc880_ch_mode_info,
378                 .get = alc880_ch_mode_get,
379                 .put = alc880_ch_mode_put,
380         },
381         { } /* end */
382 };
383
384 /* capture mixer elements */
385 static snd_kcontrol_new_t alc880_capture_mixer[] = {
386         HDA_CODEC_VOLUME("Capture Volume", 0x07, 0x0, HDA_INPUT),
387         HDA_CODEC_MUTE("Capture Switch", 0x07, 0x0, HDA_INPUT),
388         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x08, 0x0, HDA_INPUT),
389         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x08, 0x0, HDA_INPUT),
390         HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x09, 0x0, HDA_INPUT),
391         HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x09, 0x0, HDA_INPUT),
392         {
393                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
394                 /* The multiple "Capture Source" controls confuse alsamixer
395                  * So call somewhat different..
396                  * FIXME: the controls appear in the "playback" view!
397                  */
398                 /* .name = "Capture Source", */
399                 .name = "Input Source",
400                 .count = 3,
401                 .info = alc_mux_enum_info,
402                 .get = alc_mux_enum_get,
403                 .put = alc_mux_enum_put,
404         },
405         { } /* end */
406 };
407
408 /* capture mixer elements (in case NID 0x07 not available) */
409 static snd_kcontrol_new_t alc880_capture_alt_mixer[] = {
410         HDA_CODEC_VOLUME("Capture Volume", 0x08, 0x0, HDA_INPUT),
411         HDA_CODEC_MUTE("Capture Switch", 0x08, 0x0, HDA_INPUT),
412         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x09, 0x0, HDA_INPUT),
413         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x09, 0x0, HDA_INPUT),
414         {
415                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
416                 /* The multiple "Capture Source" controls confuse alsamixer
417                  * So call somewhat different..
418                  * FIXME: the controls appear in the "playback" view!
419                  */
420                 /* .name = "Capture Source", */
421                 .name = "Input Source",
422                 .count = 2,
423                 .info = alc_mux_enum_info,
424                 .get = alc_mux_enum_get,
425                 .put = alc_mux_enum_put,
426         },
427         { } /* end */
428 };
429
430
431
432 /*
433  * ALC880 5-stack model
434  *
435  * DAC: Front = 0x02 (0x0c), Surr = 0x05 (0x0f), CLFE = 0x04 (0x0d), Side = 0x02 (0xd)
436  * Pin assignment: Front = 0x14, Surr = 0x17, CLFE = 0x16
437  *                 Line-In/Side = 0x1a, Mic = 0x18, F-Mic = 0x1b, HP = 0x19
438  */
439
440 /* additional mixers to alc880_three_stack_mixer */
441 static snd_kcontrol_new_t alc880_five_stack_mixer[] = {
442         HDA_CODEC_VOLUME("Side Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
443         ALC_BIND_MUTE("Side Playback Switch", 0x0d, 2, HDA_INPUT),
444         { } /* end */
445 };
446
447 /* channel source setting (6/8 channel selection for 5-stack) */
448 /* 6ch mode */
449 static struct hda_verb alc880_fivestack_ch6_init[] = {
450         /* set line-in to input, mute it */
451         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
452         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
453         { } /* end */
454 };
455
456 /* 8ch mode */
457 static struct hda_verb alc880_fivestack_ch8_init[] = {
458         /* set line-in to output, unmute it */
459         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
460         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
461         { } /* end */
462 };
463
464 static struct alc_channel_mode alc880_fivestack_modes[2] = {
465         { 6, alc880_fivestack_ch6_init },
466         { 8, alc880_fivestack_ch8_init },
467 };
468
469
470 /*
471  * ALC880 6-stack model
472  *
473  * DAC: Front = 0x02 (0x0c), Surr = 0x03 (0x0d), CLFE = 0x04 (0x0e), Side = 0x05 (0x0f)
474  * Pin assignment: Front = 0x14, Surr = 0x15, CLFE = 0x16, Side = 0x17,
475  *   Mic = 0x18, F-Mic = 0x19, Line = 0x1a, HP = 0x1b
476  */
477
478 static hda_nid_t alc880_6st_dac_nids[4] = {
479         /* front, rear, clfe, rear_surr */
480         0x02, 0x03, 0x04, 0x05
481 };      
482
483 static struct hda_input_mux alc880_6stack_capture_source = {
484         .num_items = 4,
485         .items = {
486                 { "Mic", 0x0 },
487                 { "Front Mic", 0x1 },
488                 { "Line", 0x2 },
489                 { "CD", 0x4 },
490         },
491 };
492
493 /* fixed 8-channels */
494 static struct alc_channel_mode alc880_sixstack_modes[1] = {
495         { 8, NULL },
496 };
497
498 static snd_kcontrol_new_t alc880_six_stack_mixer[] = {
499         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
500         ALC_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
501         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
502         ALC_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
503         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
504         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
505         ALC_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
506         ALC_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
507         HDA_CODEC_VOLUME("Side Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
508         ALC_BIND_MUTE("Side Playback Switch", 0x0f, 2, HDA_INPUT),
509         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
510         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
511         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
512         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
513         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
514         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
515         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x0b, 0x1, HDA_INPUT),
516         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x0b, 0x1, HDA_INPUT),
517         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x0b, 0x05, HDA_INPUT),
518         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x0b, 0x05, HDA_INPUT),
519         {
520                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
521                 .name = "Channel Mode",
522                 .info = alc880_ch_mode_info,
523                 .get = alc880_ch_mode_get,
524                 .put = alc880_ch_mode_put,
525         },
526         { } /* end */
527 };
528
529
530 /*
531  * ALC880 W810 model
532  *
533  * W810 has rear IO for:
534  * Front (DAC 02)
535  * Surround (DAC 03)
536  * Center/LFE (DAC 04)
537  * Digital out (06)
538  *
539  * The system also has a pair of internal speakers, and a headphone jack.
540  * These are both connected to Line2 on the codec, hence to DAC 02.
541  * 
542  * There is a variable resistor to control the speaker or headphone
543  * volume. This is a hardware-only device without a software API.
544  *
545  * Plugging headphones in will disable the internal speakers. This is
546  * implemented in hardware, not via the driver using jack sense. In
547  * a similar fashion, plugging into the rear socket marked "front" will
548  * disable both the speakers and headphones.
549  *
550  * For input, there's a microphone jack, and an "audio in" jack.
551  * These may not do anything useful with this driver yet, because I
552  * haven't setup any initialization verbs for these yet...
553  */
554
555 static hda_nid_t alc880_w810_dac_nids[3] = {
556         /* front, rear/surround, clfe */
557         0x02, 0x03, 0x04
558 };
559
560 /* fixed 6 channels */
561 static struct alc_channel_mode alc880_w810_modes[1] = {
562         { 6, NULL }
563 };
564
565 /* Pin assignment: Front = 0x14, Surr = 0x15, CLFE = 0x16, HP = 0x1b */
566 static snd_kcontrol_new_t alc880_w810_base_mixer[] = {
567         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
568         ALC_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
569         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
570         ALC_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
571         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
572         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
573         ALC_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
574         ALC_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
575         HDA_CODEC_MUTE("Headphone Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
576         { } /* end */
577 };
578
579
580 /*
581  * Z710V model
582  *
583  * DAC: Front = 0x02 (0x0c), HP = 0x03 (0x0d)
584  * Pin assignment: Front = 0x14, HP = 0x15, Mic = 0x18, Mic2 = 0x19(?), Line = 0x1a
585  */
586
587 static hda_nid_t alc880_z71v_dac_nids[1] = {
588         0x02
589 };
590 #define ALC880_Z71V_HP_DAC      0x03
591
592 /* fixed 2 channels */
593 static struct alc_channel_mode alc880_2_jack_modes[1] = {
594         { 2, NULL }
595 };
596
597 static snd_kcontrol_new_t alc880_z71v_mixer[] = {
598         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
599         ALC_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
600         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
601         ALC_BIND_MUTE("Headphone Playback Switch", 0x0d, 2, HDA_INPUT),
602         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
603         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
604         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
605         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
606         { } /* end */
607 };
608
609
610 /* FIXME! */
611 /*
612  * ALC880 F1734 model
613  *
614  * DAC: HP = 0x02 (0x0c), Front = 0x03 (0x0d)
615  * Pin assignment: HP = 0x14, Front = 0x15, Mic = 0x18
616  */
617
618 static hda_nid_t alc880_f1734_dac_nids[1] = {
619         0x03
620 };
621 #define ALC880_F1734_HP_DAC     0x02
622
623 static snd_kcontrol_new_t alc880_f1734_mixer[] = {
624         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
625         ALC_BIND_MUTE("Headphone Playback Switch", 0x0c, 2, HDA_INPUT),
626         HDA_CODEC_VOLUME("Internal Speaker Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
627         ALC_BIND_MUTE("Internal Speaker Playback Switch", 0x0d, 2, HDA_INPUT),
628         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
629         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
630         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
631         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
632         { } /* end */
633 };
634
635
636 /* FIXME! */
637 /*
638  * ALC880 ASUS model
639  *
640  * DAC: HP/Front = 0x02 (0x0c), Surr = 0x03 (0x0d), CLFE = 0x04 (0x0e)
641  * Pin assignment: HP/Front = 0x14, Surr = 0x15, CLFE = 0x16,
642  *  Mic = 0x18, Line = 0x1a
643  */
644
645 #define alc880_asus_dac_nids    alc880_w810_dac_nids    /* identical with w810 */
646 #define alc880_asus_modes       alc880_threestack_modes /* 2/6 channel mode */
647
648 static snd_kcontrol_new_t alc880_asus_mixer[] = {
649         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
650         ALC_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
651         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
652         ALC_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
653         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
654         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
655         ALC_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
656         ALC_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
657         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
658         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
659         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
660         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
661         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
662         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
663         {
664                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
665                 .name = "Channel Mode",
666                 .info = alc880_ch_mode_info,
667                 .get = alc880_ch_mode_get,
668                 .put = alc880_ch_mode_put,
669         },
670         { } /* end */
671 };
672
673 /* FIXME! */
674 /*
675  * ALC880 ASUS W1V model
676  *
677  * DAC: HP/Front = 0x02 (0x0c), Surr = 0x03 (0x0d), CLFE = 0x04 (0x0e)
678  * Pin assignment: HP/Front = 0x14, Surr = 0x15, CLFE = 0x16,
679  *  Mic = 0x18, Line = 0x1a, Line2 = 0x1b
680  */
681
682 /* additional mixers to alc880_asus_mixer */
683 static snd_kcontrol_new_t alc880_asus_w1v_mixer[] = {
684         HDA_CODEC_VOLUME("Line2 Playback Volume", 0x0b, 0x03, HDA_INPUT),
685         HDA_CODEC_MUTE("Line2 Playback Switch", 0x0b, 0x03, HDA_INPUT),
686         { } /* end */
687 };
688
689
690 /*
691  * build control elements
692  */
693 static int alc_build_controls(struct hda_codec *codec)
694 {
695         struct alc_spec *spec = codec->spec;
696         int err;
697         int i;
698
699         for (i = 0; i < spec->num_mixers; i++) {
700                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
701                 if (err < 0)
702                         return err;
703         }
704
705         if (spec->multiout.dig_out_nid) {
706                 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
707                 if (err < 0)
708                         return err;
709         }
710         if (spec->dig_in_nid) {
711                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
712                 if (err < 0)
713                         return err;
714         }
715         return 0;
716 }
717
718
719 /*
720  * initialize the codec volumes, etc
721  */
722
723 /*
724  * generic initialization of ADC, input mixers and output mixers
725  */
726 static struct hda_verb alc880_volume_init_verbs[] = {
727         /*
728          * Unmute ADC0-2 and set the default input to mic-in
729          */
730         {0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
731         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
732         {0x08, AC_VERB_SET_CONNECT_SEL, 0x00},
733         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
734         {0x09, AC_VERB_SET_CONNECT_SEL, 0x00},
735         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
736
737         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
738          * mixer widget
739          * Note: PASD motherboards uses the Line In 2 as the input for front panel
740          * mic (mic 2)
741          */
742         /* Amp Indices: Mic1 = 0, Mic2 = 1, Line1 = 2, Line2 = 3, CD = 4 */
743         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
744         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
745         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
746         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
747         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
748
749         /*
750          * Set up output mixers (0x0c - 0x0f)
751          */
752         /* set vol=0 to output mixers */
753         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
754         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
755         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
756         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
757         /* set up input amps for analog loopback */
758         /* Amp Indices: DAC = 0, mixer = 1 */
759         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
760         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
761         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
762         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
763         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
764         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
765         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
766         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
767
768         { }
769 };
770
771 /*
772  * 3-stack pin configuration:
773  * front = 0x14, mic/clfe = 0x18, HP = 0x19, line/surr = 0x1a, f-mic = 0x1b
774  */
775 static struct hda_verb alc880_pin_3stack_init_verbs[] = {
776         /*
777          * preset connection lists of input pins
778          * 0 = front, 1 = rear_surr, 2 = CLFE, 3 = surround
779          */
780         {0x10, AC_VERB_SET_CONNECT_SEL, 0x02}, /* mic/clfe */
781         {0x11, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
782         {0x12, AC_VERB_SET_CONNECT_SEL, 0x03}, /* line/surround */
783
784         /*
785          * Set pin mode and muting
786          */
787         /* set front pin widgets 0x14 for output */
788         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
789         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
790         /* Mic1 (rear panel) pin widget for input and vref at 80% */
791         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
792         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
793         /* Mic2 (as headphone out) for HP output */
794         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
795         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
796         /* Line In pin widget for input */
797         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
798         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
799         /* Line2 (as front mic) pin widget for input and vref at 80% */
800         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
801         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
802         /* CD pin widget for input */
803         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
804
805         { }
806 };
807
808 /*
809  * 5-stack pin configuration:
810  * front = 0x14, surround = 0x17, clfe = 0x16, mic = 0x18, HP = 0x19,
811  * line-in/side = 0x1a, f-mic = 0x1b
812  */
813 static struct hda_verb alc880_pin_5stack_init_verbs[] = {
814         /*
815          * preset connection lists of input pins
816          * 0 = front, 1 = rear_surr, 2 = CLFE, 3 = surround
817          */
818         {0x11, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
819         {0x12, AC_VERB_SET_CONNECT_SEL, 0x01}, /* line/side */
820
821         /*
822          * Set pin mode and muting
823          */
824         /* set pin widgets 0x14-0x17 for output */
825         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
826         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
827         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
828         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
829         /* unmute pins for output (no gain on this amp) */
830         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
831         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
832         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
833         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
834
835         /* Mic1 (rear panel) pin widget for input and vref at 80% */
836         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
837         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
838         /* Mic2 (as headphone out) for HP output */
839         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
840         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
841         /* Line In pin widget for input */
842         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
843         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
844         /* Line2 (as front mic) pin widget for input and vref at 80% */
845         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
846         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
847         /* CD pin widget for input */
848         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
849
850         { }
851 };
852
853 /*
854  * W810 pin configuration:
855  * front = 0x14, surround = 0x15, clfe = 0x16, HP = 0x1b
856  */
857 static struct hda_verb alc880_pin_w810_init_verbs[] = {
858         /* hphone/speaker input selector: front DAC */
859         {0x13, AC_VERB_SET_CONNECT_SEL, 0x0},
860
861         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
862         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
863         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
864         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
865         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
866         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
867
868         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
869         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
870
871         { }
872 };
873
874 /*
875  * Z71V pin configuration:
876  * Speaker-out = 0x14, HP = 0x15, Mic = 0x18, Line-in = 0x1a, Mic2 = 0x1b (?)
877  */
878 static struct hda_verb alc880_pin_z71v_init_verbs[] = {
879         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
880         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
881         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
882         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
883
884         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
885         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
886         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
887         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
888
889         { }
890 };
891
892 /*
893  * 6-stack pin configuration:
894  * front = 0x14, surr = 0x15, clfe = 0x16, side = 0x17, mic = 0x18, f-mic = 0x19,
895  * line = 0x1a, HP = 0x1b
896  */
897 static struct hda_verb alc880_pin_6stack_init_verbs[] = {
898         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
899
900         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
901         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
902         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
903         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
904         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
905         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
906         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
907         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
908
909         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
910         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
911         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
912         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
913         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
914         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
915         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
916         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
917         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
918         
919         { }
920 };
921
922 /* FIXME! */
923 /*
924  * F1734 pin configuration:
925  * HP = 0x14, speaker-out = 0x15, mic = 0x18
926  */
927 static struct hda_verb alc880_pin_f1734_init_verbs[] = {
928         {0x10, AC_VERB_SET_CONNECT_SEL, 0x02},
929         {0x11, AC_VERB_SET_CONNECT_SEL, 0x00},
930         {0x12, AC_VERB_SET_CONNECT_SEL, 0x01},
931         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00},
932
933         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
934         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
935         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
936         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
937
938         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
939         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
940         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
941         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
942         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
943         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
944         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
945         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
946         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
947
948         { }
949 };
950
951 /* FIXME! */
952 /*
953  * ASUS pin configuration:
954  * HP/front = 0x14, surr = 0x15, clfe = 0x16, mic = 0x18, line = 0x1a
955  */
956 static struct hda_verb alc880_pin_asus_init_verbs[] = {
957         {0x10, AC_VERB_SET_CONNECT_SEL, 0x02},
958         {0x11, AC_VERB_SET_CONNECT_SEL, 0x00},
959         {0x12, AC_VERB_SET_CONNECT_SEL, 0x01},
960         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00},
961
962         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
963         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
964         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
965         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
966         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
967         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
968         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
969         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
970
971         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
972         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
973         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
974         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
975         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
976         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
977         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
978         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
979         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
980         
981         { }
982 };
983
984 /* Enable GPIO mask and set output */
985 static struct hda_verb alc880_gpio1_init_verbs[] = {
986         {0x01, AC_VERB_SET_GPIO_MASK, 0x01},
987         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
988         {0x01, AC_VERB_SET_GPIO_DATA, 0x01},
989 };
990
991 /* Enable GPIO mask and set output */
992 static struct hda_verb alc880_gpio2_init_verbs[] = {
993         {0x01, AC_VERB_SET_GPIO_MASK, 0x02},
994         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
995         {0x01, AC_VERB_SET_GPIO_DATA, 0x02},
996 };
997
998
999 /*
1000  */
1001
1002 static int alc_init(struct hda_codec *codec)
1003 {
1004         struct alc_spec *spec = codec->spec;
1005         unsigned int i;
1006
1007         for (i = 0; i < spec->num_init_verbs; i++)
1008                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
1009         return 0;
1010 }
1011
1012 #ifdef CONFIG_PM
1013 /*
1014  * resume
1015  */
1016 static int alc_resume(struct hda_codec *codec)
1017 {
1018         struct alc_spec *spec = codec->spec;
1019         int i;
1020
1021         alc_init(codec);
1022         for (i = 0; i < spec->num_mixers; i++)
1023                 snd_hda_resume_ctls(codec, spec->mixers[i]);
1024         if (spec->multiout.dig_out_nid)
1025                 snd_hda_resume_spdif_out(codec);
1026         if (spec->dig_in_nid)
1027                 snd_hda_resume_spdif_in(codec);
1028
1029         return 0;
1030 }
1031 #endif
1032
1033 /*
1034  * Analog playback callbacks
1035  */
1036 static int alc880_playback_pcm_open(struct hda_pcm_stream *hinfo,
1037                                     struct hda_codec *codec,
1038                                     snd_pcm_substream_t *substream)
1039 {
1040         struct alc_spec *spec = codec->spec;
1041         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
1042 }
1043
1044 static int alc880_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1045                                        struct hda_codec *codec,
1046                                        unsigned int stream_tag,
1047                                        unsigned int format,
1048                                        snd_pcm_substream_t *substream)
1049 {
1050         struct alc_spec *spec = codec->spec;
1051         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
1052                                                 format, substream);
1053 }
1054
1055 static int alc880_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1056                                        struct hda_codec *codec,
1057                                        snd_pcm_substream_t *substream)
1058 {
1059         struct alc_spec *spec = codec->spec;
1060         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
1061 }
1062
1063 /*
1064  * Digital out
1065  */
1066 static int alc880_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1067                                         struct hda_codec *codec,
1068                                         snd_pcm_substream_t *substream)
1069 {
1070         struct alc_spec *spec = codec->spec;
1071         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1072 }
1073
1074 static int alc880_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1075                                          struct hda_codec *codec,
1076                                          snd_pcm_substream_t *substream)
1077 {
1078         struct alc_spec *spec = codec->spec;
1079         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1080 }
1081
1082 /*
1083  * Analog capture
1084  */
1085 static int alc880_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1086                                       struct hda_codec *codec,
1087                                       unsigned int stream_tag,
1088                                       unsigned int format,
1089                                       snd_pcm_substream_t *substream)
1090 {
1091         struct alc_spec *spec = codec->spec;
1092
1093         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1094                                    stream_tag, 0, format);
1095         return 0;
1096 }
1097
1098 static int alc880_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1099                                       struct hda_codec *codec,
1100                                       snd_pcm_substream_t *substream)
1101 {
1102         struct alc_spec *spec = codec->spec;
1103
1104         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
1105         return 0;
1106 }
1107
1108
1109 /*
1110  */
1111 static struct hda_pcm_stream alc880_pcm_analog_playback = {
1112         .substreams = 1,
1113         .channels_min = 2,
1114         .channels_max = 8,
1115         /* NID is set in alc_build_pcms */
1116         .ops = {
1117                 .open = alc880_playback_pcm_open,
1118                 .prepare = alc880_playback_pcm_prepare,
1119                 .cleanup = alc880_playback_pcm_cleanup
1120         },
1121 };
1122
1123 static struct hda_pcm_stream alc880_pcm_analog_capture = {
1124         .substreams = 2,
1125         .channels_min = 2,
1126         .channels_max = 2,
1127         /* NID is set in alc_build_pcms */
1128         .ops = {
1129                 .prepare = alc880_capture_pcm_prepare,
1130                 .cleanup = alc880_capture_pcm_cleanup
1131         },
1132 };
1133
1134 static struct hda_pcm_stream alc880_pcm_digital_playback = {
1135         .substreams = 1,
1136         .channels_min = 2,
1137         .channels_max = 2,
1138         /* NID is set in alc_build_pcms */
1139         .ops = {
1140                 .open = alc880_dig_playback_pcm_open,
1141                 .close = alc880_dig_playback_pcm_close
1142         },
1143 };
1144
1145 static struct hda_pcm_stream alc880_pcm_digital_capture = {
1146         .substreams = 1,
1147         .channels_min = 2,
1148         .channels_max = 2,
1149         /* NID is set in alc_build_pcms */
1150 };
1151
1152 static int alc_build_pcms(struct hda_codec *codec)
1153 {
1154         struct alc_spec *spec = codec->spec;
1155         struct hda_pcm *info = spec->pcm_rec;
1156         int i;
1157
1158         codec->num_pcms = 1;
1159         codec->pcm_info = info;
1160
1161         info->name = spec->stream_name_analog;
1162         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *(spec->stream_analog_playback);
1163         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
1164         info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_analog_capture);
1165         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
1166
1167         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = 0;
1168         for (i = 0; i < spec->num_channel_mode; i++) {
1169                 if (spec->channel_mode[i].channels > info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max) {
1170                     info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->channel_mode[i].channels;
1171                 }
1172         }
1173
1174         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1175                 codec->num_pcms++;
1176                 info++;
1177                 info->name = spec->stream_name_digital;
1178                 if (spec->multiout.dig_out_nid) {
1179                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *(spec->stream_digital_playback);
1180                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
1181                 }
1182                 if (spec->dig_in_nid) {
1183                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_digital_capture);
1184                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
1185                 }
1186         }
1187
1188         return 0;
1189 }
1190
1191 static void alc_free(struct hda_codec *codec)
1192 {
1193         struct alc_spec *spec = codec->spec;
1194         unsigned int i;
1195
1196         if (! spec)
1197                 return;
1198
1199         if (spec->kctl_alloc) {
1200                 for (i = 0; i < spec->num_kctl_used; i++)
1201                         kfree(spec->kctl_alloc[i].name);
1202                 kfree(spec->kctl_alloc);
1203         }
1204         kfree(spec);
1205 }
1206
1207 /*
1208  */
1209 static struct hda_codec_ops alc_patch_ops = {
1210         .build_controls = alc_build_controls,
1211         .build_pcms = alc_build_pcms,
1212         .init = alc_init,
1213         .free = alc_free,
1214 #ifdef CONFIG_PM
1215         .resume = alc_resume,
1216 #endif
1217 };
1218
1219
1220 /*
1221  * Test configuration for debugging
1222  *
1223  * Almost all inputs/outputs are enabled.  I/O pins can be configured via
1224  * enum controls.
1225  */
1226 #ifdef CONFIG_SND_DEBUG
1227 static hda_nid_t alc880_test_dac_nids[4] = {
1228         0x02, 0x03, 0x04, 0x05
1229 };
1230
1231 static struct hda_input_mux alc880_test_capture_source = {
1232         .num_items = 5,
1233         .items = {
1234                 { "In-1", 0x0 },
1235                 { "In-2", 0x1 },
1236                 { "In-3", 0x2 },
1237                 { "In-4", 0x3 },
1238                 { "CD", 0x4 },
1239         },
1240 };
1241
1242 static struct alc_channel_mode alc880_test_modes[4] = {
1243         { 2, NULL },
1244         { 4, NULL },
1245         { 6, NULL },
1246         { 8, NULL },
1247 };
1248
1249 static int alc_test_pin_ctl_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1250 {
1251         static char *texts[] = {
1252                 "N/A", "Line Out", "HP Out",
1253                 "In Hi-Z", "In 50%", "In Grd", "In 80%", "In 100%"
1254         };
1255         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1256         uinfo->count = 1;
1257         uinfo->value.enumerated.items = 8;
1258         if (uinfo->value.enumerated.item >= 8)
1259                 uinfo->value.enumerated.item = 7;
1260         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1261         return 0;
1262 }
1263
1264 static int alc_test_pin_ctl_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1265 {
1266         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1267         hda_nid_t nid = (hda_nid_t)kcontrol->private_value;
1268         unsigned int pin_ctl, item = 0;
1269
1270         pin_ctl = snd_hda_codec_read(codec, nid, 0,
1271                                      AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1272         if (pin_ctl & AC_PINCTL_OUT_EN) {
1273                 if (pin_ctl & AC_PINCTL_HP_EN)
1274                         item = 2;
1275                 else
1276                         item = 1;
1277         } else if (pin_ctl & AC_PINCTL_IN_EN) {
1278                 switch (pin_ctl & AC_PINCTL_VREFEN) {
1279                 case AC_PINCTL_VREF_HIZ: item = 3; break;
1280                 case AC_PINCTL_VREF_50:  item = 4; break;
1281                 case AC_PINCTL_VREF_GRD: item = 5; break;
1282                 case AC_PINCTL_VREF_80:  item = 6; break;
1283                 case AC_PINCTL_VREF_100: item = 7; break;
1284                 }
1285         }
1286         ucontrol->value.enumerated.item[0] = item;
1287         return 0;
1288 }
1289
1290 static int alc_test_pin_ctl_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1291 {
1292         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1293         hda_nid_t nid = (hda_nid_t)kcontrol->private_value;
1294         static unsigned int ctls[] = {
1295                 0, AC_PINCTL_OUT_EN, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN,
1296                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_HIZ,
1297                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_50,
1298                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_GRD,
1299                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_80,
1300                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_100,
1301         };
1302         unsigned int old_ctl, new_ctl;
1303
1304         old_ctl = snd_hda_codec_read(codec, nid, 0,
1305                                      AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1306         new_ctl = ctls[ucontrol->value.enumerated.item[0]];
1307         if (old_ctl != new_ctl) {
1308                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, new_ctl);
1309                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1310                                     ucontrol->value.enumerated.item[0] >= 3 ? 0xb080 : 0xb000);
1311                 return 1;
1312         }
1313         return 0;
1314 }
1315
1316 static int alc_test_pin_src_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1317 {
1318         static char *texts[] = {
1319                 "Front", "Surround", "CLFE", "Side"
1320         };
1321         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1322         uinfo->count = 1;
1323         uinfo->value.enumerated.items = 4;
1324         if (uinfo->value.enumerated.item >= 4)
1325                 uinfo->value.enumerated.item = 3;
1326         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1327         return 0;
1328 }
1329
1330 static int alc_test_pin_src_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1331 {
1332         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1333         hda_nid_t nid = (hda_nid_t)kcontrol->private_value;
1334         unsigned int sel;
1335
1336         sel = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_SEL, 0);
1337         ucontrol->value.enumerated.item[0] = sel & 3;
1338         return 0;
1339 }
1340
1341 static int alc_test_pin_src_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1342 {
1343         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1344         hda_nid_t nid = (hda_nid_t)kcontrol->private_value;
1345         unsigned int sel;
1346
1347         sel = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_SEL, 0) & 3;
1348         if (ucontrol->value.enumerated.item[0] != sel) {
1349                 sel = ucontrol->value.enumerated.item[0] & 3;
1350                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, sel);
1351                 return 1;
1352         }
1353         return 0;
1354 }
1355
1356 #define PIN_CTL_TEST(xname,nid) {                       \
1357                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,    \
1358                         .name = xname,                 \
1359                         .info = alc_test_pin_ctl_info, \
1360                         .get = alc_test_pin_ctl_get,   \
1361                         .put = alc_test_pin_ctl_put,   \
1362                         .private_value = nid           \
1363                         }
1364
1365 #define PIN_SRC_TEST(xname,nid) {                       \
1366                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,    \
1367                         .name = xname,                 \
1368                         .info = alc_test_pin_src_info, \
1369                         .get = alc_test_pin_src_get,   \
1370                         .put = alc_test_pin_src_put,   \
1371                         .private_value = nid           \
1372                         }
1373
1374 static snd_kcontrol_new_t alc880_test_mixer[] = {
1375         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
1376         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
1377         HDA_CODEC_VOLUME("CLFE Playback Volume", 0x0e, 0x0, HDA_OUTPUT),
1378         HDA_CODEC_VOLUME("Side Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
1379         ALC_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
1380         ALC_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
1381         ALC_BIND_MUTE("CLFE Playback Volume", 0x0e, 2, HDA_INPUT),
1382         ALC_BIND_MUTE("Side Playback Volume", 0x0f, 2, HDA_INPUT),
1383         PIN_CTL_TEST("Front Pin Mode", 0x14),
1384         PIN_CTL_TEST("Surround Pin Mode", 0x15),
1385         PIN_CTL_TEST("CLFE Pin Mode", 0x16),
1386         PIN_CTL_TEST("Side Pin Mode", 0x17),
1387         PIN_CTL_TEST("In-1 Pin Mode", 0x18),
1388         PIN_CTL_TEST("In-2 Pin Mode", 0x19),
1389         PIN_CTL_TEST("In-3 Pin Mode", 0x1a),
1390         PIN_CTL_TEST("In-4 Pin Mode", 0x1b),
1391         PIN_SRC_TEST("In-1 Pin Source", 0x18),
1392         PIN_SRC_TEST("In-2 Pin Source", 0x19),
1393         PIN_SRC_TEST("In-3 Pin Source", 0x1a),
1394         PIN_SRC_TEST("In-4 Pin Source", 0x1b),
1395         HDA_CODEC_VOLUME("In-1 Playback Volume", 0x0b, 0x0, HDA_INPUT),
1396         HDA_CODEC_MUTE("In-1 Playback Switch", 0x0b, 0x0, HDA_INPUT),
1397         HDA_CODEC_VOLUME("In-2 Playback Volume", 0x0b, 0x1, HDA_INPUT),
1398         HDA_CODEC_MUTE("In-2 Playback Switch", 0x0b, 0x1, HDA_INPUT),
1399         HDA_CODEC_VOLUME("In-3 Playback Volume", 0x0b, 0x2, HDA_INPUT),
1400         HDA_CODEC_MUTE("In-3 Playback Switch", 0x0b, 0x2, HDA_INPUT),
1401         HDA_CODEC_VOLUME("In-4 Playback Volume", 0x0b, 0x3, HDA_INPUT),
1402         HDA_CODEC_MUTE("In-4 Playback Switch", 0x0b, 0x3, HDA_INPUT),
1403         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x4, HDA_INPUT),
1404         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x4, HDA_INPUT),
1405         HDA_CODEC_VOLUME("Capture Volume", 0x08, 0x0, HDA_INPUT),
1406         HDA_CODEC_MUTE("Capture Switch", 0x08, 0x0, HDA_INPUT),
1407         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x09, 0x0, HDA_INPUT),
1408         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x09, 0x0, HDA_INPUT),
1409         {
1410                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1411                 .name = "Input Source",
1412                 .count = 2,
1413                 .info = alc_mux_enum_info,
1414                 .get = alc_mux_enum_get,
1415                 .put = alc_mux_enum_put,
1416         },
1417         {
1418                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1419                 .name = "Channel Mode",
1420                 .info = alc880_ch_mode_info,
1421                 .get = alc880_ch_mode_get,
1422                 .put = alc880_ch_mode_put,
1423         },
1424         { } /* end */
1425 };
1426
1427 static struct hda_verb alc880_test_init_verbs[] = {
1428         /* Unmute inputs of 0x0c - 0x0f */
1429         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1430         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1431         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1432         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1433         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1434         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1435         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1436         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1437         /* Vol output for 0x0c-0x0f */
1438         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1439         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1440         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1441         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1442         /* Set output pins 0x14-0x17 */
1443         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1444         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1445         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1446         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1447         /* Unmute output pins 0x14-0x17 */
1448         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1449         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1450         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1451         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1452         /* Set input pins 0x18-0x1c */
1453         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1454         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1455         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1456         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1457         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1458         /* Mute input pins 0x18-0x1b */
1459         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1460         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1461         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1462         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1463         /* ADC set up */
1464         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1465         {0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
1466         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1467         {0x08, AC_VERB_SET_CONNECT_SEL, 0x00},
1468         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1469         {0x09, AC_VERB_SET_CONNECT_SEL, 0x00},
1470         /* Analog input/passthru */
1471         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1472         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1473         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
1474         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
1475         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
1476         { }
1477 };
1478 #endif
1479
1480 /*
1481  */
1482
1483 static struct hda_board_config alc880_cfg_tbl[] = {
1484         /* Back 3 jack, front 2 jack */
1485         { .modelname = "3stack", .config = ALC880_3ST },
1486         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe200, .config = ALC880_3ST },
1487         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe201, .config = ALC880_3ST },
1488         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe202, .config = ALC880_3ST },
1489         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe203, .config = ALC880_3ST },
1490         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe204, .config = ALC880_3ST },
1491         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe205, .config = ALC880_3ST },
1492         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe206, .config = ALC880_3ST },
1493         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe207, .config = ALC880_3ST },
1494         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe208, .config = ALC880_3ST },
1495         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe209, .config = ALC880_3ST },
1496         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe20a, .config = ALC880_3ST },
1497         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe20b, .config = ALC880_3ST },
1498         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe20c, .config = ALC880_3ST },
1499         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe20d, .config = ALC880_3ST },
1500         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe20e, .config = ALC880_3ST },
1501         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe20f, .config = ALC880_3ST },
1502         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe210, .config = ALC880_3ST },
1503         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe211, .config = ALC880_3ST },
1504         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe214, .config = ALC880_3ST },
1505         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe302, .config = ALC880_3ST },
1506         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe303, .config = ALC880_3ST },
1507         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe304, .config = ALC880_3ST },
1508         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe306, .config = ALC880_3ST },
1509         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe307, .config = ALC880_3ST },
1510         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe404, .config = ALC880_3ST },
1511         { .pci_subvendor = 0x8086, .pci_subdevice = 0xa101, .config = ALC880_3ST },
1512         { .pci_subvendor = 0x107b, .pci_subdevice = 0x3031, .config = ALC880_3ST },
1513         { .pci_subvendor = 0x107b, .pci_subdevice = 0x4036, .config = ALC880_3ST },
1514         { .pci_subvendor = 0x107b, .pci_subdevice = 0x4037, .config = ALC880_3ST },
1515         { .pci_subvendor = 0x107b, .pci_subdevice = 0x4038, .config = ALC880_3ST },
1516         { .pci_subvendor = 0x107b, .pci_subdevice = 0x4040, .config = ALC880_3ST },
1517         { .pci_subvendor = 0x107b, .pci_subdevice = 0x4041, .config = ALC880_3ST },
1518
1519         /* Back 3 jack, front 2 jack (Internal add Aux-In) */
1520         { .pci_subvendor = 0x1025, .pci_subdevice = 0xe310, .config = ALC880_3ST },
1521         { .pci_subvendor = 0x104d, .pci_subdevice = 0x81d6, .config = ALC880_3ST }, 
1522
1523         /* Back 3 jack plus 1 SPDIF out jack, front 2 jack */
1524         { .modelname = "3stack-digout", .config = ALC880_3ST_DIG },
1525         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe308, .config = ALC880_3ST_DIG },
1526
1527         /* Back 3 jack plus 1 SPDIF out jack, front 2 jack (Internal add Aux-In)*/
1528         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe305, .config = ALC880_3ST_DIG },
1529         { .pci_subvendor = 0x8086, .pci_subdevice = 0xd402, .config = ALC880_3ST_DIG },
1530         { .pci_subvendor = 0x1025, .pci_subdevice = 0xe309, .config = ALC880_3ST_DIG },
1531
1532         /* Back 5 jack, front 2 jack */
1533         { .modelname = "5stack", .config = ALC880_5ST },
1534         { .pci_subvendor = 0x107b, .pci_subdevice = 0x3033, .config = ALC880_5ST },
1535         { .pci_subvendor = 0x107b, .pci_subdevice = 0x4039, .config = ALC880_5ST },
1536         { .pci_subvendor = 0x107b, .pci_subdevice = 0x3032, .config = ALC880_5ST },
1537         { .pci_subvendor = 0x103c, .pci_subdevice = 0x2a09, .config = ALC880_5ST },
1538         { .pci_subvendor = 0x1043, .pci_subdevice = 0x814e, .config = ALC880_5ST },
1539
1540         /* Back 5 jack plus 1 SPDIF out jack, front 2 jack */
1541         { .modelname = "5stack-digout", .config = ALC880_5ST_DIG },
1542         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe224, .config = ALC880_5ST_DIG },
1543         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe400, .config = ALC880_5ST_DIG },
1544         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe401, .config = ALC880_5ST_DIG },
1545         { .pci_subvendor = 0x8086, .pci_subdevice = 0xe402, .config = ALC880_5ST_DIG },
1546         { .pci_subvendor = 0x8086, .pci_subdevice = 0xd400, .config = ALC880_5ST_DIG },
1547         { .pci_subvendor = 0x8086, .pci_subdevice = 0xd401, .config = ALC880_5ST_DIG },
1548         { .pci_subvendor = 0x8086, .pci_subdevice = 0xa100, .config = ALC880_5ST_DIG },
1549         { .pci_subvendor = 0x1565, .pci_subdevice = 0x8202, .config = ALC880_5ST_DIG },
1550         { .pci_subvendor = 0x1019, .pci_subdevice = 0xa880, .config = ALC880_5ST_DIG },
1551         { .pci_subvendor = 0x1019, .pci_subdevice = 0xa884, .config = ALC880_5ST_DIG },
1552         { .pci_subvendor = 0x1695, .pci_subdevice = 0x400d, .config = ALC880_5ST_DIG },
1553         /* note subvendor = 0 below */
1554         /* { .pci_subvendor = 0x0000, .pci_subdevice = 0x8086, .config = ALC880_5ST_DIG }, */
1555
1556         { .modelname = "w810", .config = ALC880_W810 },
1557         { .pci_subvendor = 0x161f, .pci_subdevice = 0x203d, .config = ALC880_W810 },
1558
1559         { .modelname = "z71v", .config = ALC880_Z71V },
1560         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1964, .config = ALC880_Z71V },
1561
1562         { .modelname = "6statack-digout", .config = ALC880_6ST_DIG },
1563         { .pci_subvendor = 0x2668, .pci_subdevice = 0x8086, .config = ALC880_6ST_DIG },
1564         { .pci_subvendor = 0x8086, .pci_subdevice = 0x2668, .config = ALC880_6ST_DIG },
1565         { .pci_subvendor = 0x1462, .pci_subdevice = 0x1150, .config = ALC880_6ST_DIG },
1566         { .pci_subvendor = 0xe803, .pci_subdevice = 0x1019, .config = ALC880_6ST_DIG },
1567
1568         { .modelname = "asus", .config = ALC880_ASUS },
1569         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1964, .config = ALC880_ASUS_DIG },
1570         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1973, .config = ALC880_ASUS_DIG },
1571         { .pci_subvendor = 0x1043, .pci_subdevice = 0x19b3, .config = ALC880_ASUS_DIG },
1572         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1113, .config = ALC880_ASUS_DIG },
1573         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1993, .config = ALC880_ASUS },
1574         { .pci_subvendor = 0x1043, .pci_subdevice = 0x10c3, .config = ALC880_ASUS_DIG },
1575         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1133, .config = ALC880_ASUS },
1576         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1123, .config = ALC880_ASUS_DIG },
1577         { .pci_subvendor = 0x1043, .pci_subdevice = 0x1143, .config = ALC880_ASUS },
1578         { .pci_subvendor = 0x1043, .pci_subdevice = 0x10b3, .config = ALC880_ASUS_W1V },
1579
1580         { .modelname = "uniwill", .config = ALC880_UNIWILL_DIG },
1581         { .pci_subvendor = 0x1584, .pci_subdevice = 0x9050, .config = ALC880_UNIWILL_DIG },     
1582
1583         { .modelname = "F1734", .config = ALC880_F1734 },
1584         { .pci_subvendor = 0x1734, .pci_subdevice = 0x107c, .config = ALC880_F1734 },
1585
1586 #ifdef CONFIG_SND_DEBUG
1587         { .modelname = "test", .config = ALC880_TEST },
1588 #endif
1589
1590         {}
1591 };
1592
1593 /*
1594  * configuration template - to be copied to the spec instance
1595  */
1596 struct alc_config_preset {
1597         snd_kcontrol_new_t *mixers[4];
1598         const struct hda_verb *init_verbs[4];
1599         unsigned int num_dacs;
1600         hda_nid_t *dac_nids;
1601         hda_nid_t dig_out_nid;          /* optional */
1602         hda_nid_t hp_nid;               /* optional */
1603         unsigned int num_adc_nids;
1604         hda_nid_t *adc_nids;
1605         unsigned int num_channel_mode;
1606         const struct alc_channel_mode *channel_mode;
1607         const struct hda_input_mux *input_mux;
1608 };
1609
1610 static struct alc_config_preset alc880_presets[] = {
1611         [ALC880_3ST] = {
1612                 .mixers = { alc880_three_stack_mixer },
1613                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_3stack_init_verbs },
1614                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
1615                 .dac_nids = alc880_dac_nids,
1616                 .num_channel_mode = ARRAY_SIZE(alc880_threestack_modes),
1617                 .channel_mode = alc880_threestack_modes,
1618                 .input_mux = &alc880_capture_source,
1619         },
1620         [ALC880_3ST_DIG] = {
1621                 .mixers = { alc880_three_stack_mixer },
1622                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_3stack_init_verbs },
1623                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
1624                 .dac_nids = alc880_dac_nids,
1625                 .dig_out_nid = ALC880_DIGOUT_NID,
1626                 .num_channel_mode = ARRAY_SIZE(alc880_threestack_modes),
1627                 .channel_mode = alc880_threestack_modes,
1628                 .input_mux = &alc880_capture_source,
1629         },
1630         [ALC880_5ST] = {
1631                 .mixers = { alc880_three_stack_mixer, alc880_five_stack_mixer},
1632                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_5stack_init_verbs },
1633                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
1634                 .dac_nids = alc880_dac_nids,
1635                 .num_channel_mode = ARRAY_SIZE(alc880_fivestack_modes),
1636                 .channel_mode = alc880_fivestack_modes,
1637                 .input_mux = &alc880_capture_source,
1638         },
1639         [ALC880_5ST_DIG] = {
1640                 .mixers = { alc880_three_stack_mixer, alc880_five_stack_mixer },
1641                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_5stack_init_verbs },
1642                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
1643                 .dac_nids = alc880_dac_nids,
1644                 .dig_out_nid = ALC880_DIGOUT_NID,
1645                 .num_channel_mode = ARRAY_SIZE(alc880_fivestack_modes),
1646                 .channel_mode = alc880_fivestack_modes,
1647                 .input_mux = &alc880_capture_source,
1648         },
1649         [ALC880_6ST_DIG] = {
1650                 .mixers = { alc880_six_stack_mixer },
1651                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_6stack_init_verbs },
1652                 .num_dacs = ARRAY_SIZE(alc880_6st_dac_nids),
1653                 .dac_nids = alc880_6st_dac_nids,
1654                 .dig_out_nid = ALC880_DIGOUT_NID,
1655                 .num_channel_mode = ARRAY_SIZE(alc880_sixstack_modes),
1656                 .channel_mode = alc880_sixstack_modes,
1657                 .input_mux = &alc880_6stack_capture_source,
1658         },
1659         [ALC880_W810] = {
1660                 .mixers = { alc880_w810_base_mixer },
1661                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_w810_init_verbs,
1662                                 alc880_gpio2_init_verbs },
1663                 .num_dacs = ARRAY_SIZE(alc880_w810_dac_nids),
1664                 .dac_nids = alc880_w810_dac_nids,
1665                 .dig_out_nid = ALC880_DIGOUT_NID,
1666                 .num_channel_mode = ARRAY_SIZE(alc880_w810_modes),
1667                 .channel_mode = alc880_w810_modes,
1668                 .input_mux = &alc880_capture_source,
1669         },
1670         [ALC880_Z71V] = {
1671                 .mixers = { alc880_z71v_mixer },
1672                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_z71v_init_verbs },
1673                 .num_dacs = ARRAY_SIZE(alc880_z71v_dac_nids),
1674                 .dac_nids = alc880_z71v_dac_nids,
1675                 .dig_out_nid = ALC880_DIGOUT_NID,
1676                 .hp_nid = 0x03,
1677                 .num_channel_mode = ARRAY_SIZE(alc880_2_jack_modes),
1678                 .channel_mode = alc880_2_jack_modes,
1679                 .input_mux = &alc880_capture_source,
1680         },
1681         [ALC880_F1734] = {
1682                 .mixers = { alc880_f1734_mixer },
1683                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_f1734_init_verbs },
1684                 .num_dacs = ARRAY_SIZE(alc880_f1734_dac_nids),
1685                 .dac_nids = alc880_f1734_dac_nids,
1686                 .hp_nid = 0x02,
1687                 .num_channel_mode = ARRAY_SIZE(alc880_2_jack_modes),
1688                 .channel_mode = alc880_2_jack_modes,
1689                 .input_mux = &alc880_capture_source,
1690         },
1691         [ALC880_ASUS] = {
1692                 .mixers = { alc880_asus_mixer },
1693                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_asus_init_verbs,
1694                                 alc880_gpio1_init_verbs },
1695                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
1696                 .dac_nids = alc880_asus_dac_nids,
1697                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
1698                 .channel_mode = alc880_asus_modes,
1699                 .input_mux = &alc880_capture_source,
1700         },
1701         [ALC880_ASUS_DIG] = {
1702                 .mixers = { alc880_asus_mixer },
1703                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_asus_init_verbs,
1704                                 alc880_gpio1_init_verbs },
1705                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
1706                 .dac_nids = alc880_asus_dac_nids,
1707                 .dig_out_nid = ALC880_DIGOUT_NID,
1708                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
1709                 .channel_mode = alc880_asus_modes,
1710                 .input_mux = &alc880_capture_source,
1711         },
1712         [ALC880_ASUS_W1V] = {
1713                 .mixers = { alc880_asus_mixer, alc880_asus_w1v_mixer },
1714                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_asus_init_verbs,
1715                                 alc880_gpio1_init_verbs },
1716                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
1717                 .dac_nids = alc880_asus_dac_nids,
1718                 .dig_out_nid = ALC880_DIGOUT_NID,
1719                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
1720                 .channel_mode = alc880_asus_modes,
1721                 .input_mux = &alc880_capture_source,
1722         },
1723         [ALC880_UNIWILL_DIG] = {
1724                 .mixers = { alc880_asus_mixer },
1725                 .init_verbs = { alc880_volume_init_verbs, alc880_pin_asus_init_verbs },
1726                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
1727                 .dac_nids = alc880_asus_dac_nids,
1728                 .dig_out_nid = ALC880_DIGOUT_NID,
1729                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
1730                 .channel_mode = alc880_asus_modes,
1731                 .input_mux = &alc880_capture_source,
1732         },
1733 #ifdef CONFIG_SND_DEBUG
1734         [ALC880_TEST] = {
1735                 .mixers = { alc880_test_mixer },
1736                 .init_verbs = { alc880_test_init_verbs },
1737                 .num_dacs = ARRAY_SIZE(alc880_test_dac_nids),
1738                 .dac_nids = alc880_test_dac_nids,
1739                 .dig_out_nid = ALC880_DIGOUT_NID,
1740                 .num_channel_mode = ARRAY_SIZE(alc880_test_modes),
1741                 .channel_mode = alc880_test_modes,
1742                 .input_mux = &alc880_test_capture_source,
1743         },
1744 #endif
1745 };
1746
1747 /*
1748  * Automatic parse of I/O pins from the BIOS configuration
1749  */
1750
1751 #define NUM_CONTROL_ALLOC       32
1752 #define NUM_VERB_ALLOC          32
1753
1754 enum {
1755         ALC_CTL_WIDGET_VOL,
1756         ALC_CTL_WIDGET_MUTE,
1757         ALC_CTL_BIND_MUTE,
1758 };
1759 static snd_kcontrol_new_t alc880_control_templates[] = {
1760         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
1761         HDA_CODEC_MUTE(NULL, 0, 0, 0),
1762         ALC_BIND_MUTE(NULL, 0, 0, 0),
1763 };
1764
1765 /* add dynamic controls */
1766 static int add_control(struct alc_spec *spec, int type, const char *name, unsigned long val)
1767 {
1768         snd_kcontrol_new_t *knew;
1769
1770         if (spec->num_kctl_used >= spec->num_kctl_alloc) {
1771                 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
1772
1773                 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
1774                 if (! knew)
1775                         return -ENOMEM;
1776                 if (spec->kctl_alloc) {
1777                         memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
1778                         kfree(spec->kctl_alloc);
1779                 }
1780                 spec->kctl_alloc = knew;
1781                 spec->num_kctl_alloc = num;
1782         }
1783
1784         knew = &spec->kctl_alloc[spec->num_kctl_used];
1785         *knew = alc880_control_templates[type];
1786         knew->name = kstrdup(name, GFP_KERNEL);
1787         if (! knew->name)
1788                 return -ENOMEM;
1789         knew->private_value = val;
1790         spec->num_kctl_used++;
1791         return 0;
1792 }
1793
1794 #define alc880_is_fixed_pin(nid)        ((nid) >= 0x14 && (nid) <= 0x17)
1795 #define alc880_fixed_pin_idx(nid)       ((nid) - 0x14)
1796 #define alc880_is_multi_pin(nid)        ((nid) >= 0x18)
1797 #define alc880_multi_pin_idx(nid)       ((nid) - 0x18)
1798 #define alc880_is_input_pin(nid)        ((nid) >= 0x18)
1799 #define alc880_input_pin_idx(nid)       ((nid) - 0x18)
1800 #define alc880_idx_to_dac(nid)          ((nid) + 0x02)
1801 #define alc880_dac_to_idx(nid)          ((nid) - 0x02)
1802 #define alc880_idx_to_mixer(nid)        ((nid) + 0x0c)
1803 #define alc880_idx_to_selector(nid)     ((nid) + 0x10)
1804 #define ALC880_PIN_CD_NID               0x1c
1805
1806 /* fill in the dac_nids table from the parsed pin configuration */
1807 static int alc880_auto_fill_dac_nids(struct alc_spec *spec, const struct auto_pin_cfg *cfg)
1808 {
1809         hda_nid_t nid;
1810         int assigned[4];
1811         int i, j;
1812
1813         memset(assigned, 0, sizeof(assigned));
1814         spec->multiout.dac_nids = spec->private_dac_nids;
1815
1816         /* check the pins hardwired to audio widget */
1817         for (i = 0; i < cfg->line_outs; i++) {
1818                 nid = cfg->line_out_pins[i];
1819                 if (alc880_is_fixed_pin(nid)) {
1820                         int idx = alc880_fixed_pin_idx(nid);
1821                         spec->multiout.dac_nids[i] = alc880_dac_to_idx(idx);
1822                         assigned[idx] = 1;
1823                 }
1824         }
1825         /* left pins can be connect to any audio widget */
1826         for (i = 0; i < cfg->line_outs; i++) {
1827                 nid = cfg->line_out_pins[i];
1828                 if (alc880_is_fixed_pin(nid))
1829                         continue;
1830                 /* search for an empty channel */
1831                 for (j = 0; j < cfg->line_outs; j++) {
1832                         if (! assigned[j]) {
1833                                 spec->multiout.dac_nids[i] = alc880_idx_to_dac(j);
1834                                 assigned[j] = 1;
1835                                 break;
1836                         }
1837                 }
1838         }
1839         spec->multiout.num_dacs = cfg->line_outs;
1840         return 0;
1841 }
1842
1843 /* add playback controls from the parsed DAC table */
1844 static int alc880_auto_create_multi_out_ctls(struct alc_spec *spec, const struct auto_pin_cfg *cfg)
1845 {
1846         char name[32];
1847         static const char *chname[4] = { "Front", "Surround", NULL /*CLFE*/, "Side" };
1848         hda_nid_t nid;
1849         int i, err;
1850
1851         for (i = 0; i < cfg->line_outs; i++) {
1852                 if (! spec->multiout.dac_nids[i])
1853                         continue;
1854                 nid = alc880_idx_to_mixer(alc880_dac_to_idx(spec->multiout.dac_nids[i]));
1855                 if (i == 2) {
1856                         /* Center/LFE */
1857                         if ((err = add_control(spec, ALC_CTL_WIDGET_VOL, "Center Playback Volume",
1858                                                HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
1859                                 return err;
1860                         if ((err = add_control(spec, ALC_CTL_WIDGET_VOL, "LFE Playback Volume",
1861                                                HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
1862                                 return err;
1863                         if ((err = add_control(spec, ALC_CTL_BIND_MUTE, "Center Playback Switch",
1864                                                HDA_COMPOSE_AMP_VAL(nid, 1, 2, HDA_INPUT))) < 0)
1865                                 return err;
1866                         if ((err = add_control(spec, ALC_CTL_BIND_MUTE, "LFE Playback Switch",
1867                                                HDA_COMPOSE_AMP_VAL(nid, 2, 2, HDA_INPUT))) < 0)
1868                                 return err;
1869                 } else {
1870                         sprintf(name, "%s Playback Volume", chname[i]);
1871                         if ((err = add_control(spec, ALC_CTL_WIDGET_VOL, name,
1872                                                HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
1873                                 return err;
1874                         sprintf(name, "%s Playback Switch", chname[i]);
1875                         if ((err = add_control(spec, ALC_CTL_BIND_MUTE, name,
1876                                                HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT))) < 0)
1877                                 return err;
1878                 }
1879         }
1880
1881         return 0;
1882 }
1883
1884 /* add playback controls for HP output */
1885 static int alc880_auto_create_hp_ctls(struct alc_spec *spec, hda_nid_t pin)
1886 {
1887         hda_nid_t nid;
1888         int err;
1889
1890         if (! pin)
1891                 return 0;
1892
1893         if (alc880_is_fixed_pin(pin)) {
1894                 nid = alc880_idx_to_dac(alc880_fixed_pin_idx(pin));
1895                 if (! spec->multiout.dac_nids[0]) {
1896                         /* use this as the primary output */
1897                         spec->multiout.dac_nids[0] = nid;
1898                         if (! spec->multiout.num_dacs)
1899                                 spec->multiout.num_dacs = 1;
1900                 } else 
1901                         /* specify the DAC as the extra HP output */
1902                         spec->multiout.hp_nid = nid;
1903                 /* control HP volume/switch on the output mixer amp */
1904                 nid = alc880_idx_to_mixer(alc880_fixed_pin_idx(pin));
1905                 if ((err = add_control(spec, ALC_CTL_WIDGET_VOL, "Headphone Playback Volume",
1906                                        HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
1907                         return err;
1908                 if ((err = add_control(spec, ALC_CTL_BIND_MUTE, "Headphone Playback Switch",
1909                                        HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT))) < 0)
1910                         return err;
1911         } else if (alc880_is_multi_pin(pin)) {
1912                 /* set manual connection */
1913                 if (! spec->multiout.dac_nids[0]) {
1914                         /* use this as the primary output */
1915                         spec->multiout.dac_nids[0] = alc880_idx_to_dac(alc880_multi_pin_idx(pin));
1916                         if (! spec->multiout.num_dacs)
1917                                 spec->multiout.num_dacs = 1;
1918                 }
1919                 /* we have only a switch on HP-out PIN */
1920                 if ((err = add_control(spec, ALC_CTL_WIDGET_MUTE, "Headphone Playback Switch",
1921                                        HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT))) < 0)
1922                         return err;
1923         }
1924         return 0;
1925 }
1926
1927 /* create input playback/capture controls for the given pin */
1928 static int new_analog_input(struct alc_spec *spec, hda_nid_t pin, const char *ctlname)
1929 {
1930         char name[32];
1931         int err, idx;
1932
1933         sprintf(name, "%s Playback Volume", ctlname);
1934         idx = alc880_input_pin_idx(pin);
1935         if ((err = add_control(spec, ALC_CTL_WIDGET_VOL, name,
1936                                HDA_COMPOSE_AMP_VAL(0x0b, 3, idx, HDA_INPUT))) < 0)
1937                 return err;
1938         sprintf(name, "%s Playback Switch", ctlname);
1939         if ((err = add_control(spec, ALC_CTL_WIDGET_MUTE, name,
1940                                HDA_COMPOSE_AMP_VAL(0x0b, 3, idx, HDA_INPUT))) < 0)
1941                 return err;
1942         return 0;
1943 }
1944
1945 /* create playback/capture controls for input pins */
1946 static int alc880_auto_create_analog_input_ctls(struct alc_spec *spec, const struct auto_pin_cfg *cfg)
1947 {
1948         static char *labels[AUTO_PIN_LAST] = {
1949                 "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
1950         };
1951         struct hda_input_mux *imux = &spec->private_imux;
1952         int i, err;
1953
1954         for (i = 0; i < AUTO_PIN_LAST; i++) {
1955                 if (alc880_is_input_pin(cfg->input_pins[i])) {
1956                         err = new_analog_input(spec, cfg->input_pins[i], labels[i]);
1957                         if (err < 0)
1958                                 return err;
1959                         imux->items[imux->num_items].label = labels[i];
1960                         imux->items[imux->num_items].index = alc880_input_pin_idx(cfg->input_pins[i]);
1961                         imux->num_items++;
1962                 }
1963         }
1964         return 0;
1965 }
1966
1967 static void alc880_auto_set_output_and_unmute(struct hda_codec *codec, hda_nid_t nid, int pin_type,
1968                                               int dac_idx)
1969 {
1970         /* set as output */
1971         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
1972         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
1973         /* need the manual connection? */
1974         if (alc880_is_multi_pin(nid)) {
1975                 struct alc_spec *spec = codec->spec;
1976                 int idx = alc880_multi_pin_idx(nid);
1977                 snd_hda_codec_write(codec, alc880_idx_to_selector(idx), 0,
1978                                     AC_VERB_SET_CONNECT_SEL,
1979                                     alc880_dac_to_idx(spec->multiout.dac_nids[dac_idx]));
1980         }
1981 }
1982
1983 static void alc880_auto_init_multi_out(struct hda_codec *codec)
1984 {
1985         struct alc_spec *spec = codec->spec;
1986         int i;
1987
1988         for (i = 0; i < spec->autocfg.line_outs; i++) {
1989                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
1990                 alc880_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
1991         }
1992 }
1993
1994 static void alc880_auto_init_hp_out(struct hda_codec *codec)
1995 {
1996         struct alc_spec *spec = codec->spec;
1997         hda_nid_t pin;
1998
1999         pin = spec->autocfg.hp_pin;
2000         if (pin) /* connect to front */
2001                 alc880_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
2002 }
2003
2004 static void alc880_auto_init_analog_input(struct hda_codec *codec)
2005 {
2006         struct alc_spec *spec = codec->spec;
2007         int i;
2008
2009         for (i = 0; i < AUTO_PIN_LAST; i++) {
2010                 hda_nid_t nid = spec->autocfg.input_pins[i];
2011                 if (alc880_is_input_pin(nid)) {
2012                         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2013                                             i <= AUTO_PIN_FRONT_MIC ? PIN_VREF80 : PIN_IN);
2014                         if (nid != ALC880_PIN_CD_NID)
2015                                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2016                                                     AMP_OUT_MUTE);
2017                 }
2018         }
2019 }
2020
2021 /* parse the BIOS configuration and set up the alc_spec */
2022 /* return 1 if successful, 0 if the proper config is not found, or a negative error code */
2023 static int alc880_parse_auto_config(struct hda_codec *codec)
2024 {
2025         struct alc_spec *spec = codec->spec;
2026         int err;
2027
2028         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg)) < 0)
2029                 return err;
2030         if ((err = alc880_auto_fill_dac_nids(spec, &spec->autocfg)) < 0)
2031                 return err;
2032         if (! spec->autocfg.line_outs && ! spec->autocfg.hp_pin)
2033                 return 0; /* can't find valid BIOS pin config */
2034         if ((err = alc880_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
2035             (err = alc880_auto_create_hp_ctls(spec, spec->autocfg.hp_pin)) < 0 ||
2036             (err = alc880_auto_create_analog_input_ctls(spec, &spec->autocfg)) < 0)
2037                 return err;
2038
2039         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2040
2041         if (spec->autocfg.dig_out_pin)
2042                 spec->multiout.dig_out_nid = ALC880_DIGOUT_NID;
2043         if (spec->autocfg.dig_in_pin)
2044                 spec->dig_in_nid = ALC880_DIGIN_NID;
2045
2046         if (spec->kctl_alloc)
2047                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
2048
2049         spec->init_verbs[spec->num_init_verbs++] = alc880_volume_init_verbs;
2050
2051         spec->input_mux = &spec->private_imux;
2052
2053         return 1;
2054 }
2055
2056 /* init callback for auto-configuration model -- overriding the default init */
2057 static int alc880_auto_init(struct hda_codec *codec)
2058 {
2059         alc_init(codec);
2060         alc880_auto_init_multi_out(codec);
2061         alc880_auto_init_hp_out(codec);
2062         alc880_auto_init_analog_input(codec);
2063         return 0;
2064 }
2065
2066 /*
2067  * OK, here we have finally the patch for ALC880
2068  */
2069
2070 static int patch_alc880(struct hda_codec *codec)
2071 {
2072         struct alc_spec *spec;
2073         int board_config;
2074         int i, err;
2075
2076         spec = kcalloc(1, sizeof(*spec), GFP_KERNEL);
2077         if (spec == NULL)
2078                 return -ENOMEM;
2079
2080         init_MUTEX(&spec->bind_mutex);
2081         codec->spec = spec;
2082
2083         board_config = snd_hda_check_board_config(codec, alc880_cfg_tbl);
2084         if (board_config < 0 || board_config >= ALC880_MODEL_LAST) {
2085                 printk(KERN_INFO "hda_codec: Unknown model for ALC880, trying auto-probe from BIOS...\n");
2086                 board_config = ALC880_AUTO;
2087         }
2088
2089         if (board_config == ALC880_AUTO) {
2090                 /* automatic parse from the BIOS config */
2091                 err = alc880_parse_auto_config(codec);
2092                 if (err < 0) {
2093                         alc_free(codec);
2094                         return err;
2095                 } else if (! err) {
2096                         printk(KERN_INFO "hda_codec: Cannot set up configuration from BIOS.  Using 3-stack mode...\n");
2097                         board_config = ALC880_3ST;
2098                 }
2099         }
2100
2101         if (board_config != ALC880_AUTO) {
2102                 /* set up from the preset table */
2103                 const struct alc_config_preset *preset;
2104
2105                 preset = &alc880_presets[board_config];
2106
2107                 for (i = 0; preset->mixers[i]; i++) {
2108                         snd_assert(spec->num_mixers < ARRAY_SIZE(spec->mixers), break);
2109                         spec->mixers[spec->num_mixers++] = preset->mixers[i];
2110                 }
2111                 for (i = 0; preset->init_verbs[i]; i++) {
2112                         snd_assert(spec->num_init_verbs < ARRAY_SIZE(spec->init_verbs), break);
2113                         spec->init_verbs[spec->num_init_verbs++] = preset->init_verbs[i];
2114                 }
2115
2116                 spec->channel_mode = preset->channel_mode;
2117                 spec->num_channel_mode = preset->num_channel_mode;
2118
2119                 spec->multiout.max_channels = spec->channel_mode[0].channels;
2120
2121                 spec->multiout.num_dacs = preset->num_dacs;
2122                 spec->multiout.dac_nids = preset->dac_nids;
2123                 spec->multiout.dig_out_nid = preset->dig_out_nid;
2124                 spec->multiout.hp_nid = preset->hp_nid;
2125
2126                 spec->input_mux = preset->input_mux;
2127
2128                 spec->num_adc_nids = preset->num_adc_nids;
2129                 spec->adc_nids = preset->adc_nids;
2130         }
2131
2132         spec->stream_name_analog = "ALC880 Analog";
2133         spec->stream_analog_playback = &alc880_pcm_analog_playback;
2134         spec->stream_analog_capture = &alc880_pcm_analog_capture;
2135
2136         spec->stream_name_digital = "ALC880 Digital";
2137         spec->stream_digital_playback = &alc880_pcm_digital_playback;
2138         spec->stream_digital_capture = &alc880_pcm_digital_capture;
2139
2140         if (! spec->adc_nids && spec->input_mux) {
2141                 /* check whether NID 0x07 is valid */
2142                 unsigned int wcap = snd_hda_param_read(codec, alc880_adc_nids[0],
2143                                                        AC_PAR_AUDIO_WIDGET_CAP);
2144                 wcap = (wcap & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT; /* get type */
2145                 if (wcap != AC_WID_AUD_IN) {
2146                         spec->adc_nids = alc880_adc_nids_alt;
2147                         spec->num_adc_nids = ARRAY_SIZE(alc880_adc_nids_alt);
2148                         spec->mixers[spec->num_mixers] = alc880_capture_alt_mixer;
2149                         spec->num_mixers++;
2150                 } else {
2151                         spec->adc_nids = alc880_adc_nids;
2152                         spec->num_adc_nids = ARRAY_SIZE(alc880_adc_nids);
2153                         spec->mixers[spec->num_mixers] = alc880_capture_mixer;
2154                         spec->num_mixers++;
2155                 }
2156         }
2157
2158         codec->patch_ops = alc_patch_ops;
2159         if (board_config == ALC880_AUTO)
2160                 codec->patch_ops.init = alc880_auto_init;
2161
2162         return 0;
2163 }
2164
2165
2166 /*
2167  * ALC260 support
2168  */
2169
2170 static hda_nid_t alc260_dac_nids[1] = {
2171         /* front */
2172         0x02,
2173 };
2174
2175 static hda_nid_t alc260_adc_nids[1] = {
2176         /* ADC0 */
2177         0x04,
2178 };
2179
2180 static hda_nid_t alc260_hp_adc_nids[1] = {
2181         /* ADC1 */
2182         0x05,
2183 };
2184
2185 #define ALC260_DIGOUT_NID       0x03
2186 #define ALC260_DIGIN_NID        0x06
2187
2188 static struct hda_input_mux alc260_capture_source = {
2189         .num_items = 4,
2190         .items = {
2191                 { "Mic", 0x0 },
2192                 { "Front Mic", 0x1 },
2193                 { "Line", 0x2 },
2194                 { "CD", 0x4 },
2195         },
2196 };
2197
2198 /*
2199  * This is just place-holder, so there's something for alc_build_pcms to look
2200  * at when it calculates the maximum number of channels. ALC260 has no mixer
2201  * element which allows changing the channel mode, so the verb list is
2202  * never used.
2203  */
2204 static struct alc_channel_mode alc260_modes[1] = {
2205         { 2, NULL },
2206 };
2207
2208 static snd_kcontrol_new_t alc260_base_mixer[] = {
2209         HDA_CODEC_VOLUME("Front Playback Volume", 0x08, 0x0, HDA_OUTPUT),
2210         ALC_BIND_MUTE("Front Playback Switch", 0x08, 2, HDA_INPUT),
2211         HDA_CODEC_VOLUME("CD Playback Volume", 0x07, 0x04, HDA_INPUT),
2212         HDA_CODEC_MUTE("CD Playback Switch", 0x07, 0x04, HDA_INPUT),
2213         HDA_CODEC_VOLUME("Line Playback Volume", 0x07, 0x02, HDA_INPUT),
2214         HDA_CODEC_MUTE("Line Playback Switch", 0x07, 0x02, HDA_INPUT),
2215         HDA_CODEC_VOLUME("Mic Playback Volume", 0x07, 0x0, HDA_INPUT),
2216         HDA_CODEC_MUTE("Mic Playback Switch", 0x07, 0x0, HDA_INPUT),
2217         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x07, 0x01, HDA_INPUT),
2218         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x07, 0x01, HDA_INPUT),
2219         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x07, 0x05, HDA_INPUT),
2220         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x07, 0x05, HDA_INPUT),
2221         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x09, 0x0, HDA_OUTPUT),
2222         ALC_BIND_MUTE("Headphone Playback Switch", 0x09, 2, HDA_INPUT),
2223         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x0a, 1, 0x0, HDA_OUTPUT),
2224         ALC_BIND_MUTE_MONO("Mono Playback Switch", 0x0a, 1, 2, HDA_OUTPUT),
2225         HDA_CODEC_VOLUME("Capture Volume", 0x04, 0x0, HDA_INPUT),
2226         HDA_CODEC_MUTE("Capture Switch", 0x04, 0x0, HDA_INPUT),
2227         {
2228                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2229                 .name = "Capture Source",
2230                 .info = alc_mux_enum_info,
2231                 .get = alc_mux_enum_get,
2232                 .put = alc_mux_enum_put,
2233         },
2234         { } /* end */
2235 };
2236
2237 static snd_kcontrol_new_t alc260_hp_mixer[] = {
2238         HDA_CODEC_VOLUME("Front Playback Volume", 0x08, 0x0, HDA_OUTPUT),
2239         ALC_BIND_MUTE("Front Playback Switch", 0x08, 2, HDA_INPUT),
2240         HDA_CODEC_VOLUME("CD Playback Volume", 0x07, 0x04, HDA_INPUT),
2241         HDA_CODEC_MUTE("CD Playback Switch", 0x07, 0x04, HDA_INPUT),
2242         HDA_CODEC_VOLUME("Line Playback Volume", 0x07, 0x02, HDA_INPUT),
2243         HDA_CODEC_MUTE("Line Playback Switch", 0x07, 0x02, HDA_INPUT),
2244         HDA_CODEC_VOLUME("Mic Playback Volume", 0x07, 0x0, HDA_INPUT),
2245         HDA_CODEC_MUTE("Mic Playback Switch", 0x07, 0x0, HDA_INPUT),
2246         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x07, 0x01, HDA_INPUT),
2247         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x07, 0x01, HDA_INPUT),
2248         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x09, 0x0, HDA_OUTPUT),
2249         ALC_BIND_MUTE("Headphone Playback Switch", 0x09, 2, HDA_INPUT),
2250         HDA_CODEC_VOLUME_MONO("Mono Playback Volume", 0x0a, 1, 0x0, HDA_OUTPUT),
2251         ALC_BIND_MUTE_MONO("Mono Playback Switch", 0x0a, 1, 2, HDA_OUTPUT),
2252         HDA_CODEC_VOLUME("Capture Volume", 0x05, 0x0, HDA_INPUT),
2253         HDA_CODEC_MUTE("Capture Switch", 0x05, 0x0, HDA_INPUT),
2254         {
2255                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2256                 .name = "Capture Source",
2257                 .info = alc_mux_enum_info,
2258                 .get = alc_mux_enum_get,
2259                 .put = alc_mux_enum_put,
2260         },
2261         { } /* end */
2262 };
2263
2264 static struct hda_verb alc260_init_verbs[] = {
2265         /* Line In pin widget for input */
2266         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2267         /* CD pin widget for input */
2268         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2269         /* Mic1 (rear panel) pin widget for input and vref at 80% */
2270         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2271         /* Mic2 (front panel) pin widget for input and vref at 80% */
2272         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2273         /* LINE-2 is used for line-out in rear */
2274         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2275         /* select line-out */
2276         {0x15, AC_VERB_SET_CONNECT_SEL, 0x00},
2277         /* LINE-OUT pin */
2278         {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2279         /* enable HP */
2280         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2281         /* enable Mono */
2282         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2283         /* mute capture amp left and right */
2284         {0x04, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2285         /* set connection select to line in (default select for this ADC) */
2286         {0x04, AC_VERB_SET_CONNECT_SEL, 0x02},
2287         /* mute capture amp left and right */
2288         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2289         /* set connection select to line in (default select for this ADC) */
2290         {0x05, AC_VERB_SET_CONNECT_SEL, 0x02},
2291         /* set vol=0 Line-Out mixer amp left and right */
2292         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2293         /* unmute pin widget amp left and right (no gain on this amp) */
2294         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2295         /* set vol=0 HP mixer amp left and right */
2296         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2297         /* unmute pin widget amp left and right (no gain on this amp) */
2298         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2299         /* set vol=0 Mono mixer amp left and right */
2300         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2301         /* unmute pin widget amp left and right (no gain on this amp) */
2302         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2303         /* unmute LINE-2 out pin */
2304         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2305         /* Amp Indexes: CD = 0x04, Line In 1 = 0x02, Mic 1 = 0x00 & Line In 2 = 0x03 */
2306         /* mute CD */
2307         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2308         /* mute Line In */
2309         {0x07,  AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2310         /* mute Mic */
2311         {0x07,  AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2312         /* Amp Indexes: DAC = 0x01 & mixer = 0x00 */
2313         /* mute Front out path */
2314         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2315         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2316         /* mute Headphone out path */
2317         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2318         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2319         /* mute Mono out path */
2320         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2321         {0x0a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2322         { }
2323 };
2324
2325 static struct hda_pcm_stream alc260_pcm_analog_playback = {
2326         .substreams = 1,
2327         .channels_min = 2,
2328         .channels_max = 2,
2329 };
2330
2331 static struct hda_pcm_stream alc260_pcm_analog_capture = {
2332         .substreams = 1,
2333         .channels_min = 2,
2334         .channels_max = 2,
2335 };
2336
2337 static struct hda_board_config alc260_cfg_tbl[] = {
2338         { .modelname = "hp", .config = ALC260_HP },
2339         { .pci_subvendor = 0x103c, .config = ALC260_HP },
2340         {}
2341 };
2342
2343 static int patch_alc260(struct hda_codec *codec)
2344 {
2345         struct alc_spec *spec;
2346         int board_config;
2347
2348         spec = kcalloc(1, sizeof(*spec), GFP_KERNEL);
2349         if (spec == NULL)
2350                 return -ENOMEM;
2351
2352         init_MUTEX(&spec->bind_mutex);
2353         codec->spec = spec;
2354
2355         board_config = snd_hda_check_board_config(codec, alc260_cfg_tbl);
2356         if (board_config < 0 || board_config >= ALC260_MODEL_LAST) {
2357                 snd_printd(KERN_INFO "hda_codec: Unknown model for ALC260\n");
2358                 board_config = ALC260_BASIC;
2359         }
2360
2361         switch (board_config) {
2362         case ALC260_HP:
2363                 spec->mixers[spec->num_mixers] = alc260_hp_mixer;
2364                 spec->num_mixers++;
2365                 break;
2366         default:
2367                 spec->mixers[spec->num_mixers] = alc260_base_mixer;
2368                 spec->num_mixers++;
2369                 break;
2370         }
2371
2372         spec->init_verbs[0] = alc260_init_verbs;
2373         spec->num_init_verbs = 1;
2374
2375         spec->channel_mode = alc260_modes;
2376         spec->num_channel_mode = ARRAY_SIZE(alc260_modes);
2377
2378         spec->stream_name_analog = "ALC260 Analog";
2379         spec->stream_analog_playback = &alc260_pcm_analog_playback;
2380         spec->stream_analog_capture = &alc260_pcm_analog_capture;
2381
2382         spec->multiout.max_channels = spec->channel_mode[0].channels;
2383         spec->multiout.num_dacs = ARRAY_SIZE(alc260_dac_nids);
2384         spec->multiout.dac_nids = alc260_dac_nids;
2385
2386         spec->input_mux = &alc260_capture_source;
2387         switch (board_config) {
2388         case ALC260_HP:
2389                 spec->num_adc_nids = ARRAY_SIZE(alc260_hp_adc_nids);
2390                 spec->adc_nids = alc260_hp_adc_nids;
2391                 break;
2392         default:
2393                 spec->num_adc_nids = ARRAY_SIZE(alc260_adc_nids);
2394                 spec->adc_nids = alc260_adc_nids;
2395                 break;
2396         }
2397
2398         codec->patch_ops = alc_patch_ops;
2399
2400         return 0;
2401 }
2402
2403
2404 /*
2405  * ALC882 support
2406  *
2407  * ALC882 is almost identical with ALC880 but has cleaner and more flexible
2408  * configuration.  Each pin widget can choose any input DACs and a mixer.
2409  * Each ADC is connected from a mixer of all inputs.  This makes possible
2410  * 6-channel independent captures.
2411  *
2412  * In addition, an independent DAC for the multi-playback (not used in this
2413  * driver yet).
2414  */
2415
2416 static struct alc_channel_mode alc882_ch_modes[1] = {
2417         { 8, NULL }
2418 };
2419
2420 static hda_nid_t alc882_dac_nids[4] = {
2421         /* front, rear, clfe, rear_surr */
2422         0x02, 0x03, 0x04, 0x05
2423 };
2424
2425 static hda_nid_t alc882_adc_nids[3] = {
2426         /* ADC0-2 */
2427         0x07, 0x08, 0x09,
2428 };
2429
2430 /* input MUX */
2431 /* FIXME: should be a matrix-type input source selection */
2432
2433 static struct hda_input_mux alc882_capture_source = {
2434         .num_items = 4,
2435         .items = {
2436                 { "Mic", 0x0 },
2437                 { "Front Mic", 0x1 },
2438                 { "Line", 0x2 },
2439                 { "CD", 0x4 },
2440         },
2441 };
2442
2443 #define alc882_mux_enum_info alc_mux_enum_info
2444 #define alc882_mux_enum_get alc_mux_enum_get
2445
2446 static int alc882_mux_enum_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
2447 {
2448         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2449         struct alc_spec *spec = codec->spec;
2450         const struct hda_input_mux *imux = spec->input_mux;
2451         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2452         static hda_nid_t capture_mixers[3] = { 0x24, 0x23, 0x22 };
2453         hda_nid_t nid = capture_mixers[adc_idx];
2454         unsigned int *cur_val = &spec->cur_mux[adc_idx];
2455         unsigned int i, idx;
2456
2457         idx = ucontrol->value.enumerated.item[0];
2458         if (idx >= imux->num_items)
2459                 idx = imux->num_items - 1;
2460         if (*cur_val == idx && ! codec->in_resume)
2461                 return 0;
2462         for (i = 0; i < imux->num_items; i++) {
2463                 unsigned int v = (i == idx) ? 0x7000 : 0x7080;
2464                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2465                                     v | (imux->items[i].index << 8));
2466         }
2467         *cur_val = idx;
2468         return 1;
2469 }
2470
2471 /* Pin assignment: Front=0x14, Rear=0x15, CLFE=0x16, Side=0x17
2472  *                 Mic=0x18, Front Mic=0x19, Line-In=0x1a, HP=0x1b
2473  */
2474 static snd_kcontrol_new_t alc882_base_mixer[] = {
2475         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
2476         ALC_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
2477         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
2478         ALC_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
2479         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
2480         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
2481         ALC_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
2482         ALC_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_OUTPUT),
2483         HDA_CODEC_VOLUME("Side Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
2484         ALC_BIND_MUTE("Side Playback Switch", 0x0f, 2, HDA_INPUT),
2485         HDA_CODEC_MUTE("Headphone Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
2486         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
2487         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
2488         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
2489         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
2490         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
2491         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
2492         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x0b, 0x1, HDA_INPUT),
2493         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x0b, 0x1, HDA_INPUT),
2494         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x0b, 0x05, HDA_INPUT),
2495         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x0b, 0x05, HDA_INPUT),
2496         HDA_CODEC_VOLUME("Capture Volume", 0x07, 0x0, HDA_INPUT),
2497         HDA_CODEC_MUTE("Capture Switch", 0x07, 0x0, HDA_INPUT),
2498         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x08, 0x0, HDA_INPUT),
2499         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x08, 0x0, HDA_INPUT),
2500         HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x09, 0x0, HDA_INPUT),
2501         HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x09, 0x0, HDA_INPUT),
2502         {
2503                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2504                 /* .name = "Capture Source", */
2505                 .name = "Input Source",
2506                 .count = 3,
2507                 .info = alc882_mux_enum_info,
2508                 .get = alc882_mux_enum_get,
2509                 .put = alc882_mux_enum_put,
2510         },
2511         { } /* end */
2512 };
2513
2514 static struct hda_verb alc882_init_verbs[] = {
2515         /* Front mixer: unmute input/output amp left and right (volume = 0) */
2516         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2517         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2518         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2519         /* Rear mixer */
2520         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2521         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2522         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2523         /* CLFE mixer */
2524         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2525         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2526         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2527         /* Side mixer */
2528         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2529         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2530         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2531
2532         /* Front Pin: output 0 (0x0c) */
2533         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2534         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2535         {0x14, AC_VERB_SET_CONNECT_SEL, 0x00},
2536         /* Rear Pin: output 1 (0x0d) */
2537         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2538         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2539         {0x15, AC_VERB_SET_CONNECT_SEL, 0x01},
2540         /* CLFE Pin: output 2 (0x0e) */
2541         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2542         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2543         {0x16, AC_VERB_SET_CONNECT_SEL, 0x02},
2544         /* Side Pin: output 3 (0x0f) */
2545         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2546         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2547         {0x17, AC_VERB_SET_CONNECT_SEL, 0x03},
2548         /* Mic (rear) pin: input vref at 80% */
2549         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2550         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2551         /* Front Mic pin: input vref at 80% */
2552         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2553         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2554         /* Line In pin: input */
2555         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2556         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2557         /* Line-2 In: Headphone output (output 0 - 0x0c) */
2558         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2559         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2560         {0x1b, AC_VERB_SET_CONNECT_SEL, 0x00},
2561         /* CD pin widget for input */
2562         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2563
2564         /* FIXME: use matrix-type input source selection */
2565         /* Mixer elements: 0x18, 19, 1a, 1b, 1c, 1d, 14, 15, 16, 17, 0b */
2566         /* Input mixer1: unmute Mic, F-Mic, Line, CD inputs */
2567         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2568         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2569         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2570         {0x24, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2571         /* Input mixer2 */
2572         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2573         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2574         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2575         {0x23, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2576         /* Input mixer3 */
2577         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2578         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2579         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2580         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2581         /* ADC1: mute amp left and right */
2582         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2583         {0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
2584         /* ADC2: mute amp left and right */
2585         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2586         {0x08, AC_VERB_SET_CONNECT_SEL, 0x00},
2587         /* ADC3: mute amp left and right */
2588         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2589         {0x09, AC_VERB_SET_CONNECT_SEL, 0x00},
2590
2591         { }
2592 };
2593
2594 static int patch_alc882(struct hda_codec *codec)
2595 {
2596         struct alc_spec *spec;
2597
2598         spec = kcalloc(1, sizeof(*spec), GFP_KERNEL);
2599         if (spec == NULL)
2600                 return -ENOMEM;
2601
2602         init_MUTEX(&spec->bind_mutex);
2603         codec->spec = spec;
2604
2605         spec->mixers[spec->num_mixers] = alc882_base_mixer;
2606         spec->num_mixers++;
2607
2608         spec->multiout.dig_out_nid = ALC880_DIGOUT_NID;
2609         spec->dig_in_nid = ALC880_DIGIN_NID;
2610         spec->init_verbs[0] = alc882_init_verbs;
2611         spec->num_init_verbs = 1;
2612
2613         spec->channel_mode = alc882_ch_modes;
2614         spec->num_channel_mode = ARRAY_SIZE(alc882_ch_modes);
2615
2616         spec->stream_name_analog = "ALC882 Analog";
2617         spec->stream_analog_playback = &alc880_pcm_analog_playback;
2618         spec->stream_analog_capture = &alc880_pcm_analog_capture;
2619
2620         spec->stream_name_digital = "ALC882 Digital";
2621         spec->stream_digital_playback = &alc880_pcm_digital_playback;
2622         spec->stream_digital_capture = &alc880_pcm_digital_capture;
2623
2624         spec->multiout.max_channels = spec->channel_mode[0].channels;
2625         spec->multiout.num_dacs = ARRAY_SIZE(alc882_dac_nids);
2626         spec->multiout.dac_nids = alc882_dac_nids;
2627
2628         spec->input_mux = &alc882_capture_source;
2629         spec->num_adc_nids = ARRAY_SIZE(alc882_adc_nids);
2630         spec->adc_nids = alc882_adc_nids;
2631
2632         codec->patch_ops = alc_patch_ops;
2633
2634         return 0;
2635 }
2636
2637 /*
2638  * patch entries
2639  */
2640 struct hda_codec_preset snd_hda_preset_realtek[] = {
2641         { .id = 0x10ec0260, .name = "ALC260", .patch = patch_alc260 },
2642         { .id = 0x10ec0880, .name = "ALC880", .patch = patch_alc880 },
2643         { .id = 0x10ec0882, .name = "ALC882", .patch = patch_alc882 },
2644         {} /* terminator */
2645 };