Merge branch 'master' of /home/trondmy/kernel/linux-2.6/
[linux-2.6.git] / sound / pci / hda / patch_realtek.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * HD audio interface patch for ALC 260/880/882 codecs
5  *
6  * Copyright (c) 2004 Kailang Yang <kailang@realtek.com.tw>
7  *                    PeiSen Hou <pshou@realtek.com.tw>
8  *                    Takashi Iwai <tiwai@suse.de>
9  *                    Jonathan Woithe <jwoithe@physics.adelaide.edu.au>
10  *
11  *  This driver is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This driver is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
24  */
25
26 #include <sound/driver.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/slab.h>
30 #include <linux/pci.h>
31 #include <sound/core.h>
32 #include "hda_codec.h"
33 #include "hda_local.h"
34
35 #define ALC880_FRONT_EVENT              0x01
36 #define ALC880_DCVOL_EVENT              0x02
37 #define ALC880_HP_EVENT                 0x04
38 #define ALC880_MIC_EVENT                0x08
39
40 /* ALC880 board config type */
41 enum {
42         ALC880_3ST,
43         ALC880_3ST_DIG,
44         ALC880_5ST,
45         ALC880_5ST_DIG,
46         ALC880_W810,
47         ALC880_Z71V,
48         ALC880_6ST,
49         ALC880_6ST_DIG,
50         ALC880_F1734,
51         ALC880_ASUS,
52         ALC880_ASUS_DIG,
53         ALC880_ASUS_W1V,
54         ALC880_ASUS_DIG2,
55         ALC880_FUJITSU,
56         ALC880_UNIWILL_DIG,
57         ALC880_UNIWILL,
58         ALC880_UNIWILL_P53,
59         ALC880_CLEVO,
60         ALC880_TCL_S700,
61         ALC880_LG,
62         ALC880_LG_LW,
63 #ifdef CONFIG_SND_DEBUG
64         ALC880_TEST,
65 #endif
66         ALC880_AUTO,
67         ALC880_MODEL_LAST /* last tag */
68 };
69
70 /* ALC260 models */
71 enum {
72         ALC260_BASIC,
73         ALC260_HP,
74         ALC260_HP_3013,
75         ALC260_FUJITSU_S702X,
76         ALC260_ACER,
77         ALC260_WILL,
78         ALC260_REPLACER_672V,
79 #ifdef CONFIG_SND_DEBUG
80         ALC260_TEST,
81 #endif
82         ALC260_AUTO,
83         ALC260_MODEL_LAST /* last tag */
84 };
85
86 /* ALC262 models */
87 enum {
88         ALC262_BASIC,
89         ALC262_HIPPO,
90         ALC262_HIPPO_1,
91         ALC262_FUJITSU,
92         ALC262_HP_BPC,
93         ALC262_HP_BPC_D7000_WL,
94         ALC262_HP_BPC_D7000_WF,
95         ALC262_BENQ_ED8,
96         ALC262_AUTO,
97         ALC262_MODEL_LAST /* last tag */
98 };
99
100 /* ALC861 models */
101 enum {
102         ALC861_3ST,
103         ALC660_3ST,
104         ALC861_3ST_DIG,
105         ALC861_6ST_DIG,
106         ALC861_UNIWILL_M31,
107         ALC861_TOSHIBA,
108         ALC861_ASUS,
109         ALC861_ASUS_LAPTOP,
110         ALC861_AUTO,
111         ALC861_MODEL_LAST,
112 };
113
114 /* ALC861-VD models */
115 enum {
116         ALC660VD_3ST,
117         ALC861VD_3ST,
118         ALC861VD_3ST_DIG,
119         ALC861VD_6ST_DIG,
120         ALC861VD_LENOVO,
121         ALC861VD_AUTO,
122         ALC861VD_MODEL_LAST,
123 };
124
125 /* ALC662 models */
126 enum {
127         ALC662_3ST_2ch_DIG,
128         ALC662_3ST_6ch_DIG,
129         ALC662_3ST_6ch,
130         ALC662_5ST_DIG,
131         ALC662_LENOVO_101E,
132         ALC662_AUTO,
133         ALC662_MODEL_LAST,
134 };
135
136 /* ALC882 models */
137 enum {
138         ALC882_3ST_DIG,
139         ALC882_6ST_DIG,
140         ALC882_ARIMA,
141         ALC882_W2JC,
142         ALC882_AUTO,
143         ALC885_MACPRO,
144         ALC882_MODEL_LAST,
145 };
146
147 /* ALC883 models */
148 enum {
149         ALC883_3ST_2ch_DIG,
150         ALC883_3ST_6ch_DIG,
151         ALC883_3ST_6ch,
152         ALC883_6ST_DIG,
153         ALC883_TARGA_DIG,
154         ALC883_TARGA_2ch_DIG,
155         ALC888_DEMO_BOARD,
156         ALC883_ACER,
157         ALC883_MEDION,
158         ALC883_LAPTOP_EAPD,
159         ALC883_LENOVO_101E_2ch,
160         ALC883_AUTO,
161         ALC883_MODEL_LAST,
162 };
163
164 /* for GPIO Poll */
165 #define GPIO_MASK       0x03
166
167 struct alc_spec {
168         /* codec parameterization */
169         struct snd_kcontrol_new *mixers[5];     /* mixer arrays */
170         unsigned int num_mixers;
171
172         const struct hda_verb *init_verbs[5];   /* initialization verbs
173                                                  * don't forget NULL
174                                                  * termination!
175                                                  */
176         unsigned int num_init_verbs;
177
178         char *stream_name_analog;       /* analog PCM stream */
179         struct hda_pcm_stream *stream_analog_playback;
180         struct hda_pcm_stream *stream_analog_capture;
181
182         char *stream_name_digital;      /* digital PCM stream */
183         struct hda_pcm_stream *stream_digital_playback;
184         struct hda_pcm_stream *stream_digital_capture;
185
186         /* playback */
187         struct hda_multi_out multiout;  /* playback set-up
188                                          * max_channels, dacs must be set
189                                          * dig_out_nid and hp_nid are optional
190                                          */
191
192         /* capture */
193         unsigned int num_adc_nids;
194         hda_nid_t *adc_nids;
195         hda_nid_t dig_in_nid;           /* digital-in NID; optional */
196
197         /* capture source */
198         unsigned int num_mux_defs;
199         const struct hda_input_mux *input_mux;
200         unsigned int cur_mux[3];
201
202         /* channel model */
203         const struct hda_channel_mode *channel_mode;
204         int num_channel_mode;
205         int need_dac_fix;
206
207         /* PCM information */
208         struct hda_pcm pcm_rec[3];      /* used in alc_build_pcms() */
209
210         /* dynamic controls, init_verbs and input_mux */
211         struct auto_pin_cfg autocfg;
212         unsigned int num_kctl_alloc, num_kctl_used;
213         struct snd_kcontrol_new *kctl_alloc;
214         struct hda_input_mux private_imux;
215         hda_nid_t private_dac_nids[5];
216
217         /* hooks */
218         void (*init_hook)(struct hda_codec *codec);
219         void (*unsol_event)(struct hda_codec *codec, unsigned int res);
220
221         /* for pin sensing */
222         unsigned int sense_updated: 1;
223         unsigned int jack_present: 1;
224 };
225
226 /*
227  * configuration template - to be copied to the spec instance
228  */
229 struct alc_config_preset {
230         struct snd_kcontrol_new *mixers[5]; /* should be identical size
231                                              * with spec
232                                              */
233         const struct hda_verb *init_verbs[5];
234         unsigned int num_dacs;
235         hda_nid_t *dac_nids;
236         hda_nid_t dig_out_nid;          /* optional */
237         hda_nid_t hp_nid;               /* optional */
238         unsigned int num_adc_nids;
239         hda_nid_t *adc_nids;
240         hda_nid_t dig_in_nid;
241         unsigned int num_channel_mode;
242         const struct hda_channel_mode *channel_mode;
243         int need_dac_fix;
244         unsigned int num_mux_defs;
245         const struct hda_input_mux *input_mux;
246         void (*unsol_event)(struct hda_codec *, unsigned int);
247         void (*init_hook)(struct hda_codec *);
248 };
249
250
251 /*
252  * input MUX handling
253  */
254 static int alc_mux_enum_info(struct snd_kcontrol *kcontrol,
255                              struct snd_ctl_elem_info *uinfo)
256 {
257         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
258         struct alc_spec *spec = codec->spec;
259         unsigned int mux_idx = snd_ctl_get_ioffidx(kcontrol, &uinfo->id);
260         if (mux_idx >= spec->num_mux_defs)
261                 mux_idx = 0;
262         return snd_hda_input_mux_info(&spec->input_mux[mux_idx], uinfo);
263 }
264
265 static int alc_mux_enum_get(struct snd_kcontrol *kcontrol,
266                             struct snd_ctl_elem_value *ucontrol)
267 {
268         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
269         struct alc_spec *spec = codec->spec;
270         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
271
272         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
273         return 0;
274 }
275
276 static int alc_mux_enum_put(struct snd_kcontrol *kcontrol,
277                             struct snd_ctl_elem_value *ucontrol)
278 {
279         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
280         struct alc_spec *spec = codec->spec;
281         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
282         unsigned int mux_idx = adc_idx >= spec->num_mux_defs ? 0 : adc_idx;
283         return snd_hda_input_mux_put(codec, &spec->input_mux[mux_idx], ucontrol,
284                                      spec->adc_nids[adc_idx],
285                                      &spec->cur_mux[adc_idx]);
286 }
287
288
289 /*
290  * channel mode setting
291  */
292 static int alc_ch_mode_info(struct snd_kcontrol *kcontrol,
293                             struct snd_ctl_elem_info *uinfo)
294 {
295         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
296         struct alc_spec *spec = codec->spec;
297         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
298                                     spec->num_channel_mode);
299 }
300
301 static int alc_ch_mode_get(struct snd_kcontrol *kcontrol,
302                            struct snd_ctl_elem_value *ucontrol)
303 {
304         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
305         struct alc_spec *spec = codec->spec;
306         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
307                                    spec->num_channel_mode,
308                                    spec->multiout.max_channels);
309 }
310
311 static int alc_ch_mode_put(struct snd_kcontrol *kcontrol,
312                            struct snd_ctl_elem_value *ucontrol)
313 {
314         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
315         struct alc_spec *spec = codec->spec;
316         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
317                                       spec->num_channel_mode,
318                                       &spec->multiout.max_channels);
319         if (err >= 0 && spec->need_dac_fix)
320                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
321         return err;
322 }
323
324 /*
325  * Control the mode of pin widget settings via the mixer.  "pc" is used
326  * instead of "%" to avoid consequences of accidently treating the % as 
327  * being part of a format specifier.  Maximum allowed length of a value is
328  * 63 characters plus NULL terminator.
329  *
330  * Note: some retasking pin complexes seem to ignore requests for input
331  * states other than HiZ (eg: PIN_VREFxx) and revert to HiZ if any of these
332  * are requested.  Therefore order this list so that this behaviour will not
333  * cause problems when mixer clients move through the enum sequentially.
334  * NIDs 0x0f and 0x10 have been observed to have this behaviour as of
335  * March 2006.
336  */
337 static char *alc_pin_mode_names[] = {
338         "Mic 50pc bias", "Mic 80pc bias",
339         "Line in", "Line out", "Headphone out",
340 };
341 static unsigned char alc_pin_mode_values[] = {
342         PIN_VREF50, PIN_VREF80, PIN_IN, PIN_OUT, PIN_HP,
343 };
344 /* The control can present all 5 options, or it can limit the options based
345  * in the pin being assumed to be exclusively an input or an output pin.  In
346  * addition, "input" pins may or may not process the mic bias option
347  * depending on actual widget capability (NIDs 0x0f and 0x10 don't seem to
348  * accept requests for bias as of chip versions up to March 2006) and/or
349  * wiring in the computer.
350  */
351 #define ALC_PIN_DIR_IN              0x00
352 #define ALC_PIN_DIR_OUT             0x01
353 #define ALC_PIN_DIR_INOUT           0x02
354 #define ALC_PIN_DIR_IN_NOMICBIAS    0x03
355 #define ALC_PIN_DIR_INOUT_NOMICBIAS 0x04
356
357 /* Info about the pin modes supported by the different pin direction modes. 
358  * For each direction the minimum and maximum values are given.
359  */
360 static signed char alc_pin_mode_dir_info[5][2] = {
361         { 0, 2 },    /* ALC_PIN_DIR_IN */
362         { 3, 4 },    /* ALC_PIN_DIR_OUT */
363         { 0, 4 },    /* ALC_PIN_DIR_INOUT */
364         { 2, 2 },    /* ALC_PIN_DIR_IN_NOMICBIAS */
365         { 2, 4 },    /* ALC_PIN_DIR_INOUT_NOMICBIAS */
366 };
367 #define alc_pin_mode_min(_dir) (alc_pin_mode_dir_info[_dir][0])
368 #define alc_pin_mode_max(_dir) (alc_pin_mode_dir_info[_dir][1])
369 #define alc_pin_mode_n_items(_dir) \
370         (alc_pin_mode_max(_dir)-alc_pin_mode_min(_dir)+1)
371
372 static int alc_pin_mode_info(struct snd_kcontrol *kcontrol,
373                              struct snd_ctl_elem_info *uinfo)
374 {
375         unsigned int item_num = uinfo->value.enumerated.item;
376         unsigned char dir = (kcontrol->private_value >> 16) & 0xff;
377
378         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
379         uinfo->count = 1;
380         uinfo->value.enumerated.items = alc_pin_mode_n_items(dir);
381
382         if (item_num<alc_pin_mode_min(dir) || item_num>alc_pin_mode_max(dir))
383                 item_num = alc_pin_mode_min(dir);
384         strcpy(uinfo->value.enumerated.name, alc_pin_mode_names[item_num]);
385         return 0;
386 }
387
388 static int alc_pin_mode_get(struct snd_kcontrol *kcontrol,
389                             struct snd_ctl_elem_value *ucontrol)
390 {
391         unsigned int i;
392         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
393         hda_nid_t nid = kcontrol->private_value & 0xffff;
394         unsigned char dir = (kcontrol->private_value >> 16) & 0xff;
395         long *valp = ucontrol->value.integer.value;
396         unsigned int pinctl = snd_hda_codec_read(codec, nid, 0,
397                                                  AC_VERB_GET_PIN_WIDGET_CONTROL,
398                                                  0x00);
399
400         /* Find enumerated value for current pinctl setting */
401         i = alc_pin_mode_min(dir);
402         while (alc_pin_mode_values[i] != pinctl && i <= alc_pin_mode_max(dir))
403                 i++;
404         *valp = i <= alc_pin_mode_max(dir) ? i: alc_pin_mode_min(dir);
405         return 0;
406 }
407
408 static int alc_pin_mode_put(struct snd_kcontrol *kcontrol,
409                             struct snd_ctl_elem_value *ucontrol)
410 {
411         signed int change;
412         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
413         hda_nid_t nid = kcontrol->private_value & 0xffff;
414         unsigned char dir = (kcontrol->private_value >> 16) & 0xff;
415         long val = *ucontrol->value.integer.value;
416         unsigned int pinctl = snd_hda_codec_read(codec, nid, 0,
417                                                  AC_VERB_GET_PIN_WIDGET_CONTROL,
418                                                  0x00);
419
420         if (val < alc_pin_mode_min(dir) || val > alc_pin_mode_max(dir))
421                 val = alc_pin_mode_min(dir);
422
423         change = pinctl != alc_pin_mode_values[val];
424         if (change) {
425                 /* Set pin mode to that requested */
426                 snd_hda_codec_write(codec,nid,0,AC_VERB_SET_PIN_WIDGET_CONTROL,
427                                     alc_pin_mode_values[val]);
428
429                 /* Also enable the retasking pin's input/output as required 
430                  * for the requested pin mode.  Enum values of 2 or less are
431                  * input modes.
432                  *
433                  * Dynamically switching the input/output buffers probably
434                  * reduces noise slightly (particularly on input) so we'll
435                  * do it.  However, having both input and output buffers
436                  * enabled simultaneously doesn't seem to be problematic if
437                  * this turns out to be necessary in the future.
438                  */
439                 if (val <= 2) {
440                         snd_hda_codec_write(codec, nid, 0,
441                                             AC_VERB_SET_AMP_GAIN_MUTE,
442                                             AMP_OUT_MUTE);
443                         snd_hda_codec_write(codec, nid, 0,
444                                             AC_VERB_SET_AMP_GAIN_MUTE,
445                                             AMP_IN_UNMUTE(0));
446                 } else {
447                         snd_hda_codec_write(codec, nid, 0,
448                                             AC_VERB_SET_AMP_GAIN_MUTE,
449                                             AMP_IN_MUTE(0));
450                         snd_hda_codec_write(codec, nid, 0,
451                                             AC_VERB_SET_AMP_GAIN_MUTE,
452                                             AMP_OUT_UNMUTE);
453                 }
454         }
455         return change;
456 }
457
458 #define ALC_PIN_MODE(xname, nid, dir) \
459         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
460           .info = alc_pin_mode_info, \
461           .get = alc_pin_mode_get, \
462           .put = alc_pin_mode_put, \
463           .private_value = nid | (dir<<16) }
464
465 /* A switch control for ALC260 GPIO pins.  Multiple GPIOs can be ganged
466  * together using a mask with more than one bit set.  This control is
467  * currently used only by the ALC260 test model.  At this stage they are not
468  * needed for any "production" models.
469  */
470 #ifdef CONFIG_SND_DEBUG
471 static int alc_gpio_data_info(struct snd_kcontrol *kcontrol,
472                               struct snd_ctl_elem_info *uinfo)
473 {
474         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
475         uinfo->count = 1;
476         uinfo->value.integer.min = 0;
477         uinfo->value.integer.max = 1;
478         return 0;
479 }
480
481 static int alc_gpio_data_get(struct snd_kcontrol *kcontrol,
482                              struct snd_ctl_elem_value *ucontrol)
483 {
484         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
485         hda_nid_t nid = kcontrol->private_value & 0xffff;
486         unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
487         long *valp = ucontrol->value.integer.value;
488         unsigned int val = snd_hda_codec_read(codec, nid, 0,
489                                               AC_VERB_GET_GPIO_DATA, 0x00);
490
491         *valp = (val & mask) != 0;
492         return 0;
493 }
494 static int alc_gpio_data_put(struct snd_kcontrol *kcontrol,
495                              struct snd_ctl_elem_value *ucontrol)
496 {
497         signed int change;
498         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
499         hda_nid_t nid = kcontrol->private_value & 0xffff;
500         unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
501         long val = *ucontrol->value.integer.value;
502         unsigned int gpio_data = snd_hda_codec_read(codec, nid, 0,
503                                                     AC_VERB_GET_GPIO_DATA,
504                                                     0x00);
505
506         /* Set/unset the masked GPIO bit(s) as needed */
507         change = (val == 0 ? 0 : mask) != (gpio_data & mask);
508         if (val == 0)
509                 gpio_data &= ~mask;
510         else
511                 gpio_data |= mask;
512         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_GPIO_DATA, gpio_data);
513
514         return change;
515 }
516 #define ALC_GPIO_DATA_SWITCH(xname, nid, mask) \
517         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
518           .info = alc_gpio_data_info, \
519           .get = alc_gpio_data_get, \
520           .put = alc_gpio_data_put, \
521           .private_value = nid | (mask<<16) }
522 #endif   /* CONFIG_SND_DEBUG */
523
524 /* A switch control to allow the enabling of the digital IO pins on the
525  * ALC260.  This is incredibly simplistic; the intention of this control is
526  * to provide something in the test model allowing digital outputs to be
527  * identified if present.  If models are found which can utilise these
528  * outputs a more complete mixer control can be devised for those models if
529  * necessary.
530  */
531 #ifdef CONFIG_SND_DEBUG
532 static int alc_spdif_ctrl_info(struct snd_kcontrol *kcontrol,
533                                struct snd_ctl_elem_info *uinfo)
534 {
535         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
536         uinfo->count = 1;
537         uinfo->value.integer.min = 0;
538         uinfo->value.integer.max = 1;
539         return 0;
540 }
541
542 static int alc_spdif_ctrl_get(struct snd_kcontrol *kcontrol,
543                               struct snd_ctl_elem_value *ucontrol)
544 {
545         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
546         hda_nid_t nid = kcontrol->private_value & 0xffff;
547         unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
548         long *valp = ucontrol->value.integer.value;
549         unsigned int val = snd_hda_codec_read(codec, nid, 0,
550                                               AC_VERB_GET_DIGI_CONVERT, 0x00);
551
552         *valp = (val & mask) != 0;
553         return 0;
554 }
555 static int alc_spdif_ctrl_put(struct snd_kcontrol *kcontrol,
556                               struct snd_ctl_elem_value *ucontrol)
557 {
558         signed int change;
559         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
560         hda_nid_t nid = kcontrol->private_value & 0xffff;
561         unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
562         long val = *ucontrol->value.integer.value;
563         unsigned int ctrl_data = snd_hda_codec_read(codec, nid, 0,
564                                                     AC_VERB_GET_DIGI_CONVERT,
565                                                     0x00);
566
567         /* Set/unset the masked control bit(s) as needed */
568         change = (val == 0 ? 0 : mask) != (ctrl_data & mask);
569         if (val==0)
570                 ctrl_data &= ~mask;
571         else
572                 ctrl_data |= mask;
573         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
574                             ctrl_data);
575
576         return change;
577 }
578 #define ALC_SPDIF_CTRL_SWITCH(xname, nid, mask) \
579         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
580           .info = alc_spdif_ctrl_info, \
581           .get = alc_spdif_ctrl_get, \
582           .put = alc_spdif_ctrl_put, \
583           .private_value = nid | (mask<<16) }
584 #endif   /* CONFIG_SND_DEBUG */
585
586 /*
587  * set up from the preset table
588  */
589 static void setup_preset(struct alc_spec *spec,
590                          const struct alc_config_preset *preset)
591 {
592         int i;
593
594         for (i = 0; i < ARRAY_SIZE(preset->mixers) && preset->mixers[i]; i++)
595                 spec->mixers[spec->num_mixers++] = preset->mixers[i];
596         for (i = 0; i < ARRAY_SIZE(preset->init_verbs) && preset->init_verbs[i];
597              i++)
598                 spec->init_verbs[spec->num_init_verbs++] =
599                         preset->init_verbs[i];
600         
601         spec->channel_mode = preset->channel_mode;
602         spec->num_channel_mode = preset->num_channel_mode;
603         spec->need_dac_fix = preset->need_dac_fix;
604
605         spec->multiout.max_channels = spec->channel_mode[0].channels;
606
607         spec->multiout.num_dacs = preset->num_dacs;
608         spec->multiout.dac_nids = preset->dac_nids;
609         spec->multiout.dig_out_nid = preset->dig_out_nid;
610         spec->multiout.hp_nid = preset->hp_nid;
611         
612         spec->num_mux_defs = preset->num_mux_defs;
613         if (!spec->num_mux_defs)
614                 spec->num_mux_defs = 1;
615         spec->input_mux = preset->input_mux;
616
617         spec->num_adc_nids = preset->num_adc_nids;
618         spec->adc_nids = preset->adc_nids;
619         spec->dig_in_nid = preset->dig_in_nid;
620
621         spec->unsol_event = preset->unsol_event;
622         spec->init_hook = preset->init_hook;
623 }
624
625 /* Enable GPIO mask and set output */
626 static struct hda_verb alc_gpio1_init_verbs[] = {
627         {0x01, AC_VERB_SET_GPIO_MASK, 0x01},
628         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
629         {0x01, AC_VERB_SET_GPIO_DATA, 0x01},
630         { }
631 };
632
633 static struct hda_verb alc_gpio2_init_verbs[] = {
634         {0x01, AC_VERB_SET_GPIO_MASK, 0x02},
635         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
636         {0x01, AC_VERB_SET_GPIO_DATA, 0x02},
637         { }
638 };
639
640 static struct hda_verb alc_gpio3_init_verbs[] = {
641         {0x01, AC_VERB_SET_GPIO_MASK, 0x03},
642         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x03},
643         {0x01, AC_VERB_SET_GPIO_DATA, 0x03},
644         { }
645 };
646
647 /* 32-bit subsystem ID for BIOS loading in HD Audio codec.
648  *      31 ~ 16 :       Manufacture ID
649  *      15 ~ 8  :       SKU ID
650  *      7  ~ 0  :       Assembly ID
651  *      port-A --> pin 39/41, port-E --> pin 14/15, port-D --> pin 35/36
652  */
653 static void alc_subsystem_id(struct hda_codec *codec,
654                              unsigned int porta, unsigned int porte,
655                              unsigned int portd)
656 {
657         unsigned int ass, tmp;
658
659         ass = codec->subsystem_id;
660         if (!(ass & 1))
661                 return;
662
663         /* Override */
664         tmp = (ass & 0x38) >> 3;        /* external Amp control */
665         switch (tmp) {
666         case 1:
667                 snd_hda_sequence_write(codec, alc_gpio1_init_verbs);
668                 break;
669         case 3:
670                 snd_hda_sequence_write(codec, alc_gpio2_init_verbs);
671                 break;
672         case 7:
673                 snd_hda_sequence_write(codec, alc_gpio3_init_verbs);
674                 break;
675         case 5:
676                 switch (codec->vendor_id) {
677                 case 0x10ec0862:
678                 case 0x10ec0660:
679                 case 0x10ec0662:        
680                 case 0x10ec0267:
681                 case 0x10ec0268:
682                         snd_hda_codec_write(codec, 0x14, 0,
683                                             AC_VERB_SET_EAPD_BTLENABLE, 2);
684                         snd_hda_codec_write(codec, 0x15, 0,
685                                             AC_VERB_SET_EAPD_BTLENABLE, 2);
686                         return;
687                 }
688         case 6:
689                 if (ass & 4) {  /* bit 2 : 0 = Desktop, 1 = Laptop */
690                         hda_nid_t port = 0;
691                         tmp = (ass & 0x1800) >> 11;
692                         switch (tmp) {
693                         case 0: port = porta; break;
694                         case 1: port = porte; break;
695                         case 2: port = portd; break;
696                         }
697                         if (port)
698                                 snd_hda_codec_write(codec, port, 0,
699                                                     AC_VERB_SET_EAPD_BTLENABLE,
700                                                     2);
701                 }
702                 snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_COEF_INDEX, 7);
703                 snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PROC_COEF,
704                                     (tmp == 5 ? 0x3040 : 0x3050));
705                 break;
706         }
707 }
708
709 /*
710  * ALC880 3-stack model
711  *
712  * DAC: Front = 0x02 (0x0c), Surr = 0x05 (0x0f), CLFE = 0x04 (0x0e)
713  * Pin assignment: Front = 0x14, Line-In/Surr = 0x1a, Mic/CLFE = 0x18,
714  *                 F-Mic = 0x1b, HP = 0x19
715  */
716
717 static hda_nid_t alc880_dac_nids[4] = {
718         /* front, rear, clfe, rear_surr */
719         0x02, 0x05, 0x04, 0x03
720 };
721
722 static hda_nid_t alc880_adc_nids[3] = {
723         /* ADC0-2 */
724         0x07, 0x08, 0x09,
725 };
726
727 /* The datasheet says the node 0x07 is connected from inputs,
728  * but it shows zero connection in the real implementation on some devices.
729  * Note: this is a 915GAV bug, fixed on 915GLV
730  */
731 static hda_nid_t alc880_adc_nids_alt[2] = {
732         /* ADC1-2 */
733         0x08, 0x09,
734 };
735
736 #define ALC880_DIGOUT_NID       0x06
737 #define ALC880_DIGIN_NID        0x0a
738
739 static struct hda_input_mux alc880_capture_source = {
740         .num_items = 4,
741         .items = {
742                 { "Mic", 0x0 },
743                 { "Front Mic", 0x3 },
744                 { "Line", 0x2 },
745                 { "CD", 0x4 },
746         },
747 };
748
749 /* channel source setting (2/6 channel selection for 3-stack) */
750 /* 2ch mode */
751 static struct hda_verb alc880_threestack_ch2_init[] = {
752         /* set line-in to input, mute it */
753         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
754         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
755         /* set mic-in to input vref 80%, mute it */
756         { 0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
757         { 0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
758         { } /* end */
759 };
760
761 /* 6ch mode */
762 static struct hda_verb alc880_threestack_ch6_init[] = {
763         /* set line-in to output, unmute it */
764         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
765         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
766         /* set mic-in to output, unmute it */
767         { 0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
768         { 0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
769         { } /* end */
770 };
771
772 static struct hda_channel_mode alc880_threestack_modes[2] = {
773         { 2, alc880_threestack_ch2_init },
774         { 6, alc880_threestack_ch6_init },
775 };
776
777 static struct snd_kcontrol_new alc880_three_stack_mixer[] = {
778         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
779         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
780         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
781         HDA_BIND_MUTE("Surround Playback Switch", 0x0f, 2, HDA_INPUT),
782         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
783         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
784         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
785         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
786         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
787         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
788         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
789         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
790         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
791         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
792         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x0b, 0x3, HDA_INPUT),
793         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x0b, 0x3, HDA_INPUT),
794         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x0b, 0x05, HDA_INPUT),
795         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x0b, 0x05, HDA_INPUT),
796         HDA_CODEC_MUTE("Headphone Playback Switch", 0x19, 0x0, HDA_OUTPUT),
797         {
798                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
799                 .name = "Channel Mode",
800                 .info = alc_ch_mode_info,
801                 .get = alc_ch_mode_get,
802                 .put = alc_ch_mode_put,
803         },
804         { } /* end */
805 };
806
807 /* capture mixer elements */
808 static struct snd_kcontrol_new alc880_capture_mixer[] = {
809         HDA_CODEC_VOLUME("Capture Volume", 0x07, 0x0, HDA_INPUT),
810         HDA_CODEC_MUTE("Capture Switch", 0x07, 0x0, HDA_INPUT),
811         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x08, 0x0, HDA_INPUT),
812         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x08, 0x0, HDA_INPUT),
813         HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x09, 0x0, HDA_INPUT),
814         HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x09, 0x0, HDA_INPUT),
815         {
816                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
817                 /* The multiple "Capture Source" controls confuse alsamixer
818                  * So call somewhat different..
819                  * FIXME: the controls appear in the "playback" view!
820                  */
821                 /* .name = "Capture Source", */
822                 .name = "Input Source",
823                 .count = 3,
824                 .info = alc_mux_enum_info,
825                 .get = alc_mux_enum_get,
826                 .put = alc_mux_enum_put,
827         },
828         { } /* end */
829 };
830
831 /* capture mixer elements (in case NID 0x07 not available) */
832 static struct snd_kcontrol_new alc880_capture_alt_mixer[] = {
833         HDA_CODEC_VOLUME("Capture Volume", 0x08, 0x0, HDA_INPUT),
834         HDA_CODEC_MUTE("Capture Switch", 0x08, 0x0, HDA_INPUT),
835         HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x09, 0x0, HDA_INPUT),
836         HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x09, 0x0, HDA_INPUT),
837         {
838                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
839                 /* The multiple "Capture Source" controls confuse alsamixer
840                  * So call somewhat different..
841                  * FIXME: the controls appear in the "playback" view!
842                  */
843                 /* .name = "Capture Source", */
844                 .name = "Input Source",
845                 .count = 2,
846                 .info = alc_mux_enum_info,
847                 .get = alc_mux_enum_get,
848                 .put = alc_mux_enum_put,
849         },
850         { } /* end */
851 };
852
853
854
855 /*
856  * ALC880 5-stack model
857  *
858  * DAC: Front = 0x02 (0x0c), Surr = 0x05 (0x0f), CLFE = 0x04 (0x0d),
859  *      Side = 0x02 (0xd)
860  * Pin assignment: Front = 0x14, Surr = 0x17, CLFE = 0x16
861  *                 Line-In/Side = 0x1a, Mic = 0x18, F-Mic = 0x1b, HP = 0x19
862  */
863
864 /* additional mixers to alc880_three_stack_mixer */
865 static struct snd_kcontrol_new alc880_five_stack_mixer[] = {
866         HDA_CODEC_VOLUME("Side Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
867         HDA_BIND_MUTE("Side Playback Switch", 0x0d, 2, HDA_INPUT),
868         { } /* end */
869 };
870
871 /* channel source setting (6/8 channel selection for 5-stack) */
872 /* 6ch mode */
873 static struct hda_verb alc880_fivestack_ch6_init[] = {
874         /* set line-in to input, mute it */
875         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
876         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
877         { } /* end */
878 };
879
880 /* 8ch mode */
881 static struct hda_verb alc880_fivestack_ch8_init[] = {
882         /* set line-in to output, unmute it */
883         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
884         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
885         { } /* end */
886 };
887
888 static struct hda_channel_mode alc880_fivestack_modes[2] = {
889         { 6, alc880_fivestack_ch6_init },
890         { 8, alc880_fivestack_ch8_init },
891 };
892
893
894 /*
895  * ALC880 6-stack model
896  *
897  * DAC: Front = 0x02 (0x0c), Surr = 0x03 (0x0d), CLFE = 0x04 (0x0e),
898  *      Side = 0x05 (0x0f)
899  * Pin assignment: Front = 0x14, Surr = 0x15, CLFE = 0x16, Side = 0x17,
900  *   Mic = 0x18, F-Mic = 0x19, Line = 0x1a, HP = 0x1b
901  */
902
903 static hda_nid_t alc880_6st_dac_nids[4] = {
904         /* front, rear, clfe, rear_surr */
905         0x02, 0x03, 0x04, 0x05
906 };
907
908 static struct hda_input_mux alc880_6stack_capture_source = {
909         .num_items = 4,
910         .items = {
911                 { "Mic", 0x0 },
912                 { "Front Mic", 0x1 },
913                 { "Line", 0x2 },
914                 { "CD", 0x4 },
915         },
916 };
917
918 /* fixed 8-channels */
919 static struct hda_channel_mode alc880_sixstack_modes[1] = {
920         { 8, NULL },
921 };
922
923 static struct snd_kcontrol_new alc880_six_stack_mixer[] = {
924         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
925         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
926         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
927         HDA_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
928         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
929         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
930         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
931         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
932         HDA_CODEC_VOLUME("Side Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
933         HDA_BIND_MUTE("Side Playback Switch", 0x0f, 2, HDA_INPUT),
934         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
935         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
936         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
937         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
938         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
939         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
940         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x0b, 0x1, HDA_INPUT),
941         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x0b, 0x1, HDA_INPUT),
942         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x0b, 0x05, HDA_INPUT),
943         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x0b, 0x05, HDA_INPUT),
944         {
945                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
946                 .name = "Channel Mode",
947                 .info = alc_ch_mode_info,
948                 .get = alc_ch_mode_get,
949                 .put = alc_ch_mode_put,
950         },
951         { } /* end */
952 };
953
954
955 /*
956  * ALC880 W810 model
957  *
958  * W810 has rear IO for:
959  * Front (DAC 02)
960  * Surround (DAC 03)
961  * Center/LFE (DAC 04)
962  * Digital out (06)
963  *
964  * The system also has a pair of internal speakers, and a headphone jack.
965  * These are both connected to Line2 on the codec, hence to DAC 02.
966  * 
967  * There is a variable resistor to control the speaker or headphone
968  * volume. This is a hardware-only device without a software API.
969  *
970  * Plugging headphones in will disable the internal speakers. This is
971  * implemented in hardware, not via the driver using jack sense. In
972  * a similar fashion, plugging into the rear socket marked "front" will
973  * disable both the speakers and headphones.
974  *
975  * For input, there's a microphone jack, and an "audio in" jack.
976  * These may not do anything useful with this driver yet, because I
977  * haven't setup any initialization verbs for these yet...
978  */
979
980 static hda_nid_t alc880_w810_dac_nids[3] = {
981         /* front, rear/surround, clfe */
982         0x02, 0x03, 0x04
983 };
984
985 /* fixed 6 channels */
986 static struct hda_channel_mode alc880_w810_modes[1] = {
987         { 6, NULL }
988 };
989
990 /* Pin assignment: Front = 0x14, Surr = 0x15, CLFE = 0x16, HP = 0x1b */
991 static struct snd_kcontrol_new alc880_w810_base_mixer[] = {
992         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
993         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
994         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
995         HDA_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
996         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
997         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
998         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
999         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
1000         HDA_CODEC_MUTE("Headphone Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
1001         { } /* end */
1002 };
1003
1004
1005 /*
1006  * Z710V model
1007  *
1008  * DAC: Front = 0x02 (0x0c), HP = 0x03 (0x0d)
1009  * Pin assignment: Front = 0x14, HP = 0x15, Mic = 0x18, Mic2 = 0x19(?),
1010  *                 Line = 0x1a
1011  */
1012
1013 static hda_nid_t alc880_z71v_dac_nids[1] = {
1014         0x02
1015 };
1016 #define ALC880_Z71V_HP_DAC      0x03
1017
1018 /* fixed 2 channels */
1019 static struct hda_channel_mode alc880_2_jack_modes[1] = {
1020         { 2, NULL }
1021 };
1022
1023 static struct snd_kcontrol_new alc880_z71v_mixer[] = {
1024         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
1025         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
1026         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
1027         HDA_BIND_MUTE("Headphone Playback Switch", 0x0d, 2, HDA_INPUT),
1028         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
1029         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
1030         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
1031         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
1032         { } /* end */
1033 };
1034
1035
1036 /* FIXME! */
1037 /*
1038  * ALC880 F1734 model
1039  *
1040  * DAC: HP = 0x02 (0x0c), Front = 0x03 (0x0d)
1041  * Pin assignment: HP = 0x14, Front = 0x15, Mic = 0x18
1042  */
1043
1044 static hda_nid_t alc880_f1734_dac_nids[1] = {
1045         0x03
1046 };
1047 #define ALC880_F1734_HP_DAC     0x02
1048
1049 static struct snd_kcontrol_new alc880_f1734_mixer[] = {
1050         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
1051         HDA_BIND_MUTE("Headphone Playback Switch", 0x0c, 2, HDA_INPUT),
1052         HDA_CODEC_VOLUME("Internal Speaker Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
1053         HDA_BIND_MUTE("Internal Speaker Playback Switch", 0x0d, 2, HDA_INPUT),
1054         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
1055         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
1056         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
1057         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
1058         { } /* end */
1059 };
1060
1061
1062 /* FIXME! */
1063 /*
1064  * ALC880 ASUS model
1065  *
1066  * DAC: HP/Front = 0x02 (0x0c), Surr = 0x03 (0x0d), CLFE = 0x04 (0x0e)
1067  * Pin assignment: HP/Front = 0x14, Surr = 0x15, CLFE = 0x16,
1068  *  Mic = 0x18, Line = 0x1a
1069  */
1070
1071 #define alc880_asus_dac_nids    alc880_w810_dac_nids    /* identical with w810 */
1072 #define alc880_asus_modes       alc880_threestack_modes /* 2/6 channel mode */
1073
1074 static struct snd_kcontrol_new alc880_asus_mixer[] = {
1075         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
1076         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
1077         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
1078         HDA_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
1079         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
1080         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
1081         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
1082         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
1083         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
1084         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
1085         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
1086         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
1087         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
1088         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
1089         {
1090                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1091                 .name = "Channel Mode",
1092                 .info = alc_ch_mode_info,
1093                 .get = alc_ch_mode_get,
1094                 .put = alc_ch_mode_put,
1095         },
1096         { } /* end */
1097 };
1098
1099 /* FIXME! */
1100 /*
1101  * ALC880 ASUS W1V model
1102  *
1103  * DAC: HP/Front = 0x02 (0x0c), Surr = 0x03 (0x0d), CLFE = 0x04 (0x0e)
1104  * Pin assignment: HP/Front = 0x14, Surr = 0x15, CLFE = 0x16,
1105  *  Mic = 0x18, Line = 0x1a, Line2 = 0x1b
1106  */
1107
1108 /* additional mixers to alc880_asus_mixer */
1109 static struct snd_kcontrol_new alc880_asus_w1v_mixer[] = {
1110         HDA_CODEC_VOLUME("Line2 Playback Volume", 0x0b, 0x03, HDA_INPUT),
1111         HDA_CODEC_MUTE("Line2 Playback Switch", 0x0b, 0x03, HDA_INPUT),
1112         { } /* end */
1113 };
1114
1115 /* additional mixers to alc880_asus_mixer */
1116 static struct snd_kcontrol_new alc880_pcbeep_mixer[] = {
1117         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x0b, 0x05, HDA_INPUT),
1118         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x0b, 0x05, HDA_INPUT),
1119         { } /* end */
1120 };
1121
1122 /* TCL S700 */
1123 static struct snd_kcontrol_new alc880_tcl_s700_mixer[] = {
1124         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
1125         HDA_CODEC_MUTE("Front Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
1126         HDA_CODEC_MUTE("Headphone Playback Switch", 0x14, 0x0, HDA_OUTPUT),
1127         HDA_CODEC_VOLUME("CD Playback Volume", 0x0B, 0x04, HDA_INPUT),
1128         HDA_CODEC_MUTE("CD Playback Switch", 0x0B, 0x04, HDA_INPUT),
1129         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0B, 0x0, HDA_INPUT),
1130         HDA_CODEC_MUTE("Mic Playback Switch", 0x0B, 0x0, HDA_INPUT),
1131         HDA_CODEC_VOLUME("Capture Volume", 0x08, 0x0, HDA_INPUT),
1132         HDA_CODEC_MUTE("Capture Switch", 0x08, 0x0, HDA_INPUT),
1133         {
1134                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1135                 /* The multiple "Capture Source" controls confuse alsamixer
1136                  * So call somewhat different..
1137                  * FIXME: the controls appear in the "playback" view!
1138                  */
1139                 /* .name = "Capture Source", */
1140                 .name = "Input Source",
1141                 .count = 1,
1142                 .info = alc_mux_enum_info,
1143                 .get = alc_mux_enum_get,
1144                 .put = alc_mux_enum_put,
1145         },
1146         { } /* end */
1147 };
1148
1149 /* Uniwill */
1150 static struct snd_kcontrol_new alc880_uniwill_mixer[] = {
1151         HDA_CODEC_VOLUME("HPhone Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
1152         HDA_BIND_MUTE("HPhone Playback Switch", 0x0c, 2, HDA_INPUT),
1153         HDA_CODEC_VOLUME("iSpeaker Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
1154         HDA_BIND_MUTE("iSpeaker Playback Switch", 0x0d, 2, HDA_INPUT),
1155         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
1156         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
1157         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
1158         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
1159         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
1160         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
1161         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
1162         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
1163         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
1164         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
1165         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x0b, 0x1, HDA_INPUT),
1166         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x0b, 0x1, HDA_INPUT),
1167         HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x0b, 0x05, HDA_INPUT),
1168         HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x0b, 0x05, HDA_INPUT),
1169         {
1170                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1171                 .name = "Channel Mode",
1172                 .info = alc_ch_mode_info,
1173                 .get = alc_ch_mode_get,
1174                 .put = alc_ch_mode_put,
1175         },
1176         { } /* end */
1177 };
1178
1179 static struct snd_kcontrol_new alc880_fujitsu_mixer[] = {
1180         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
1181         HDA_BIND_MUTE("Headphone Playback Switch", 0x0c, 2, HDA_INPUT),
1182         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
1183         HDA_BIND_MUTE("Speaker Playback Switch", 0x0d, 2, HDA_INPUT),
1184         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
1185         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
1186         HDA_CODEC_VOLUME("Ext Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
1187         HDA_CODEC_MUTE("Ext Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
1188         HDA_CODEC_VOLUME("Int Mic Playback Volume", 0x0b, 0x1, HDA_INPUT),
1189         HDA_CODEC_MUTE("Int Mic Playback Switch", 0x0b, 0x1, HDA_INPUT),
1190         { } /* end */
1191 };
1192
1193 static struct snd_kcontrol_new alc880_uniwill_p53_mixer[] = {
1194         HDA_CODEC_VOLUME("HPhone Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
1195         HDA_BIND_MUTE("HPhone Playback Switch", 0x0c, 2, HDA_INPUT),
1196         HDA_CODEC_VOLUME("iSpeaker Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
1197         HDA_BIND_MUTE("iSpeaker Playback Switch", 0x0d, 2, HDA_INPUT),
1198         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
1199         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
1200         { } /* end */
1201 };
1202
1203 /*
1204  * build control elements
1205  */
1206 static int alc_build_controls(struct hda_codec *codec)
1207 {
1208         struct alc_spec *spec = codec->spec;
1209         int err;
1210         int i;
1211
1212         for (i = 0; i < spec->num_mixers; i++) {
1213                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1214                 if (err < 0)
1215                         return err;
1216         }
1217
1218         if (spec->multiout.dig_out_nid) {
1219                 err = snd_hda_create_spdif_out_ctls(codec,
1220                                                     spec->multiout.dig_out_nid);
1221                 if (err < 0)
1222                         return err;
1223         }
1224         if (spec->dig_in_nid) {
1225                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1226                 if (err < 0)
1227                         return err;
1228         }
1229         return 0;
1230 }
1231
1232
1233 /*
1234  * initialize the codec volumes, etc
1235  */
1236
1237 /*
1238  * generic initialization of ADC, input mixers and output mixers
1239  */
1240 static struct hda_verb alc880_volume_init_verbs[] = {
1241         /*
1242          * Unmute ADC0-2 and set the default input to mic-in
1243          */
1244         {0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
1245         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1246         {0x08, AC_VERB_SET_CONNECT_SEL, 0x00},
1247         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1248         {0x09, AC_VERB_SET_CONNECT_SEL, 0x00},
1249         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1250
1251         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
1252          * mixer widget
1253          * Note: PASD motherboards uses the Line In 2 as the input for front
1254          * panel mic (mic 2)
1255          */
1256         /* Amp Indices: Mic1 = 0, Mic2 = 1, Line1 = 2, Line2 = 3, CD = 4 */
1257         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1258         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1259         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
1260         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
1261         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
1262
1263         /*
1264          * Set up output mixers (0x0c - 0x0f)
1265          */
1266         /* set vol=0 to output mixers */
1267         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1268         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1269         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1270         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1271         /* set up input amps for analog loopback */
1272         /* Amp Indices: DAC = 0, mixer = 1 */
1273         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1274         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1275         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1276         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1277         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1278         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1279         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1280         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1281
1282         { }
1283 };
1284
1285 /*
1286  * 3-stack pin configuration:
1287  * front = 0x14, mic/clfe = 0x18, HP = 0x19, line/surr = 0x1a, f-mic = 0x1b
1288  */
1289 static struct hda_verb alc880_pin_3stack_init_verbs[] = {
1290         /*
1291          * preset connection lists of input pins
1292          * 0 = front, 1 = rear_surr, 2 = CLFE, 3 = surround
1293          */
1294         {0x10, AC_VERB_SET_CONNECT_SEL, 0x02}, /* mic/clfe */
1295         {0x11, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
1296         {0x12, AC_VERB_SET_CONNECT_SEL, 0x03}, /* line/surround */
1297
1298         /*
1299          * Set pin mode and muting
1300          */
1301         /* set front pin widgets 0x14 for output */
1302         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1303         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1304         /* Mic1 (rear panel) pin widget for input and vref at 80% */
1305         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1306         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1307         /* Mic2 (as headphone out) for HP output */
1308         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1309         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1310         /* Line In pin widget for input */
1311         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1312         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1313         /* Line2 (as front mic) pin widget for input and vref at 80% */
1314         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1315         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1316         /* CD pin widget for input */
1317         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1318
1319         { }
1320 };
1321
1322 /*
1323  * 5-stack pin configuration:
1324  * front = 0x14, surround = 0x17, clfe = 0x16, mic = 0x18, HP = 0x19,
1325  * line-in/side = 0x1a, f-mic = 0x1b
1326  */
1327 static struct hda_verb alc880_pin_5stack_init_verbs[] = {
1328         /*
1329          * preset connection lists of input pins
1330          * 0 = front, 1 = rear_surr, 2 = CLFE, 3 = surround
1331          */
1332         {0x11, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
1333         {0x12, AC_VERB_SET_CONNECT_SEL, 0x01}, /* line/side */
1334
1335         /*
1336          * Set pin mode and muting
1337          */
1338         /* set pin widgets 0x14-0x17 for output */
1339         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1340         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1341         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1342         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1343         /* unmute pins for output (no gain on this amp) */
1344         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1345         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1346         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1347         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1348
1349         /* Mic1 (rear panel) pin widget for input and vref at 80% */
1350         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1351         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1352         /* Mic2 (as headphone out) for HP output */
1353         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1354         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1355         /* Line In pin widget for input */
1356         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1357         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1358         /* Line2 (as front mic) pin widget for input and vref at 80% */
1359         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1360         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1361         /* CD pin widget for input */
1362         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1363
1364         { }
1365 };
1366
1367 /*
1368  * W810 pin configuration:
1369  * front = 0x14, surround = 0x15, clfe = 0x16, HP = 0x1b
1370  */
1371 static struct hda_verb alc880_pin_w810_init_verbs[] = {
1372         /* hphone/speaker input selector: front DAC */
1373         {0x13, AC_VERB_SET_CONNECT_SEL, 0x0},
1374
1375         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1376         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1377         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1378         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1379         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1380         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1381
1382         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1383         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1384
1385         { }
1386 };
1387
1388 /*
1389  * Z71V pin configuration:
1390  * Speaker-out = 0x14, HP = 0x15, Mic = 0x18, Line-in = 0x1a, Mic2 = 0x1b (?)
1391  */
1392 static struct hda_verb alc880_pin_z71v_init_verbs[] = {
1393         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1394         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1395         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1396         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1397
1398         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1399         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1400         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1401         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1402
1403         { }
1404 };
1405
1406 /*
1407  * 6-stack pin configuration:
1408  * front = 0x14, surr = 0x15, clfe = 0x16, side = 0x17, mic = 0x18,
1409  * f-mic = 0x19, line = 0x1a, HP = 0x1b
1410  */
1411 static struct hda_verb alc880_pin_6stack_init_verbs[] = {
1412         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
1413
1414         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1415         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1416         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1417         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1418         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1419         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1420         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1421         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1422
1423         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1424         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1425         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1426         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1427         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1428         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1429         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1430         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1431         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1432         
1433         { }
1434 };
1435
1436 /*
1437  * Uniwill pin configuration:
1438  * HP = 0x14, InternalSpeaker = 0x15, mic = 0x18, internal mic = 0x19,
1439  * line = 0x1a
1440  */
1441 static struct hda_verb alc880_uniwill_init_verbs[] = {
1442         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
1443
1444         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1445         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1446         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1447         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1448         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1449         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1450         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1451         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1452         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
1453         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
1454         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
1455         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
1456         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
1457         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
1458
1459         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1460         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1461         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1462         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1463         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1464         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1465         /* {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP}, */
1466         /* {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, */
1467         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1468
1469         {0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | ALC880_HP_EVENT},
1470         {0x18, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | ALC880_MIC_EVENT},
1471
1472         { }
1473 };
1474
1475 /*
1476 * Uniwill P53
1477 * HP = 0x14, InternalSpeaker = 0x15, mic = 0x19, 
1478  */
1479 static struct hda_verb alc880_uniwill_p53_init_verbs[] = {
1480         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
1481
1482         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1483         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1484         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1485         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1486         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1487         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1488         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
1489         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
1490         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
1491         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
1492         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
1493         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
1494
1495         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1496         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1497         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1498         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1499         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1500         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1501
1502         {0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | ALC880_HP_EVENT},
1503         {0x21, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | ALC880_DCVOL_EVENT},
1504
1505         { }
1506 };
1507
1508 static struct hda_verb alc880_beep_init_verbs[] = {
1509         { 0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(5) },
1510         { }
1511 };
1512
1513 /* toggle speaker-output according to the hp-jack state */
1514 static void alc880_uniwill_hp_automute(struct hda_codec *codec)
1515 {
1516         unsigned int present;
1517         unsigned char bits;
1518
1519         present = snd_hda_codec_read(codec, 0x14, 0,
1520                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1521         bits = present ? 0x80 : 0;
1522         snd_hda_codec_amp_update(codec, 0x15, 0, HDA_OUTPUT, 0,
1523                                  0x80, bits);
1524         snd_hda_codec_amp_update(codec, 0x15, 1, HDA_OUTPUT, 0,
1525                                  0x80, bits);
1526         snd_hda_codec_amp_update(codec, 0x16, 0, HDA_OUTPUT, 0,
1527                                  0x80, bits);
1528         snd_hda_codec_amp_update(codec, 0x16, 1, HDA_OUTPUT, 0,
1529                                  0x80, bits);
1530 }
1531
1532 /* auto-toggle front mic */
1533 static void alc880_uniwill_mic_automute(struct hda_codec *codec)
1534 {
1535         unsigned int present;
1536         unsigned char bits;
1537
1538         present = snd_hda_codec_read(codec, 0x18, 0,
1539                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1540         bits = present ? 0x80 : 0;
1541         snd_hda_codec_amp_update(codec, 0x0b, 0, HDA_INPUT, 1,
1542                                  0x80, bits);
1543         snd_hda_codec_amp_update(codec, 0x0b, 1, HDA_INPUT, 1,
1544                                  0x80, bits);
1545 }
1546
1547 static void alc880_uniwill_automute(struct hda_codec *codec)
1548 {
1549         alc880_uniwill_hp_automute(codec);
1550         alc880_uniwill_mic_automute(codec);
1551 }
1552
1553 static void alc880_uniwill_unsol_event(struct hda_codec *codec,
1554                                        unsigned int res)
1555 {
1556         /* Looks like the unsol event is incompatible with the standard
1557          * definition.  4bit tag is placed at 28 bit!
1558          */
1559         switch (res >> 28) {
1560         case ALC880_HP_EVENT:
1561                 alc880_uniwill_hp_automute(codec);
1562                 break;
1563         case ALC880_MIC_EVENT:
1564                 alc880_uniwill_mic_automute(codec);
1565                 break;
1566         }
1567 }
1568
1569 static void alc880_uniwill_p53_hp_automute(struct hda_codec *codec)
1570 {
1571         unsigned int present;
1572         unsigned char bits;
1573
1574         present = snd_hda_codec_read(codec, 0x14, 0,
1575                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1576         bits = present ? 0x80 : 0;
1577         snd_hda_codec_amp_update(codec, 0x15, 0, HDA_INPUT, 0,
1578                                  0x80, bits);
1579         snd_hda_codec_amp_update(codec, 0x15, 1, HDA_INPUT, 0,
1580                                  0x80, bits);
1581 }
1582
1583 static void alc880_uniwill_p53_dcvol_automute(struct hda_codec *codec)
1584 {
1585         unsigned int present;
1586         
1587         present = snd_hda_codec_read(codec, 0x21, 0,
1588                                      AC_VERB_GET_VOLUME_KNOB_CONTROL, 0) & 0x7f;
1589
1590         snd_hda_codec_amp_update(codec, 0x0c, 0, HDA_OUTPUT, 0,
1591                                  0x7f, present);
1592         snd_hda_codec_amp_update(codec, 0x0c, 1, HDA_OUTPUT, 0,
1593                                  0x7f,  present);
1594
1595         snd_hda_codec_amp_update(codec, 0x0d, 0, HDA_OUTPUT, 0,
1596                                  0x7f,  present);
1597         snd_hda_codec_amp_update(codec, 0x0d, 1, HDA_OUTPUT, 0,
1598                                  0x7f, present);
1599
1600 }
1601 static void alc880_uniwill_p53_unsol_event(struct hda_codec *codec,
1602                                            unsigned int res)
1603 {
1604         /* Looks like the unsol event is incompatible with the standard
1605          * definition.  4bit tag is placed at 28 bit!
1606          */
1607         if ((res >> 28) == ALC880_HP_EVENT)
1608                 alc880_uniwill_p53_hp_automute(codec);
1609         if ((res >> 28) == ALC880_DCVOL_EVENT)
1610                 alc880_uniwill_p53_dcvol_automute(codec);
1611 }
1612
1613 /* FIXME! */
1614 /*
1615  * F1734 pin configuration:
1616  * HP = 0x14, speaker-out = 0x15, mic = 0x18
1617  */
1618 static struct hda_verb alc880_pin_f1734_init_verbs[] = {
1619         {0x10, AC_VERB_SET_CONNECT_SEL, 0x02},
1620         {0x11, AC_VERB_SET_CONNECT_SEL, 0x00},
1621         {0x12, AC_VERB_SET_CONNECT_SEL, 0x01},
1622         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00},
1623
1624         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1625         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1626         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1627         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1628
1629         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1630         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1631         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1632         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1633         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1634         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1635         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1636         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1637         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1638
1639         { }
1640 };
1641
1642 /* FIXME! */
1643 /*
1644  * ASUS pin configuration:
1645  * HP/front = 0x14, surr = 0x15, clfe = 0x16, mic = 0x18, line = 0x1a
1646  */
1647 static struct hda_verb alc880_pin_asus_init_verbs[] = {
1648         {0x10, AC_VERB_SET_CONNECT_SEL, 0x02},
1649         {0x11, AC_VERB_SET_CONNECT_SEL, 0x00},
1650         {0x12, AC_VERB_SET_CONNECT_SEL, 0x01},
1651         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00},
1652
1653         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1654         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1655         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1656         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1657         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1658         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1659         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1660         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1661
1662         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1663         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1664         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1665         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1666         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1667         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1668         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1669         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1670         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1671         
1672         { }
1673 };
1674
1675 /* Enable GPIO mask and set output */
1676 #define alc880_gpio1_init_verbs alc_gpio1_init_verbs
1677 #define alc880_gpio2_init_verbs alc_gpio2_init_verbs
1678
1679 /* Clevo m520g init */
1680 static struct hda_verb alc880_pin_clevo_init_verbs[] = {
1681         /* headphone output */
1682         {0x11, AC_VERB_SET_CONNECT_SEL, 0x01},
1683         /* line-out */
1684         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1685         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1686         /* Line-in */
1687         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1688         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1689         /* CD */
1690         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1691         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1692         /* Mic1 (rear panel) */
1693         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1694         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1695         /* Mic2 (front panel) */
1696         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1697         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1698         /* headphone */
1699         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1700         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1701         /* change to EAPD mode */
1702         {0x20, AC_VERB_SET_COEF_INDEX, 0x07},
1703         {0x20, AC_VERB_SET_PROC_COEF,  0x3060},
1704
1705         { }
1706 };
1707
1708 static struct hda_verb alc880_pin_tcl_S700_init_verbs[] = {
1709         /* change to EAPD mode */
1710         {0x20, AC_VERB_SET_COEF_INDEX, 0x07},
1711         {0x20, AC_VERB_SET_PROC_COEF,  0x3060},
1712
1713         /* Headphone output */
1714         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1715         /* Front output*/
1716         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1717         {0x1b, AC_VERB_SET_CONNECT_SEL, 0x00},
1718
1719         /* Line In pin widget for input */
1720         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1721         /* CD pin widget for input */
1722         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1723         /* Mic1 (rear panel) pin widget for input and vref at 80% */
1724         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1725
1726         /* change to EAPD mode */
1727         {0x20, AC_VERB_SET_COEF_INDEX, 0x07},
1728         {0x20, AC_VERB_SET_PROC_COEF,  0x3070},
1729
1730         { }
1731 };
1732
1733 /*
1734  * LG m1 express dual
1735  *
1736  * Pin assignment:
1737  *   Rear Line-In/Out (blue): 0x14
1738  *   Build-in Mic-In: 0x15
1739  *   Speaker-out: 0x17
1740  *   HP-Out (green): 0x1b
1741  *   Mic-In/Out (red): 0x19
1742  *   SPDIF-Out: 0x1e
1743  */
1744
1745 /* To make 5.1 output working (green=Front, blue=Surr, red=CLFE) */
1746 static hda_nid_t alc880_lg_dac_nids[3] = {
1747         0x05, 0x02, 0x03
1748 };
1749
1750 /* seems analog CD is not working */
1751 static struct hda_input_mux alc880_lg_capture_source = {
1752         .num_items = 3,
1753         .items = {
1754                 { "Mic", 0x1 },
1755                 { "Line", 0x5 },
1756                 { "Internal Mic", 0x6 },
1757         },
1758 };
1759
1760 /* 2,4,6 channel modes */
1761 static struct hda_verb alc880_lg_ch2_init[] = {
1762         /* set line-in and mic-in to input */
1763         { 0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
1764         { 0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
1765         { }
1766 };
1767
1768 static struct hda_verb alc880_lg_ch4_init[] = {
1769         /* set line-in to out and mic-in to input */
1770         { 0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
1771         { 0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
1772         { }
1773 };
1774
1775 static struct hda_verb alc880_lg_ch6_init[] = {
1776         /* set line-in and mic-in to output */
1777         { 0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
1778         { 0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
1779         { }
1780 };
1781
1782 static struct hda_channel_mode alc880_lg_ch_modes[3] = {
1783         { 2, alc880_lg_ch2_init },
1784         { 4, alc880_lg_ch4_init },
1785         { 6, alc880_lg_ch6_init },
1786 };
1787
1788 static struct snd_kcontrol_new alc880_lg_mixer[] = {
1789         /* FIXME: it's not really "master" but front channels */
1790         HDA_CODEC_VOLUME("Master Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
1791         HDA_BIND_MUTE("Master Playback Switch", 0x0f, 2, HDA_INPUT),
1792         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
1793         HDA_BIND_MUTE("Surround Playback Switch", 0x0c, 2, HDA_INPUT),
1794         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0d, 1, 0x0, HDA_OUTPUT),
1795         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0d, 2, 0x0, HDA_OUTPUT),
1796         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0d, 1, 2, HDA_INPUT),
1797         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0d, 2, 2, HDA_INPUT),
1798         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x1, HDA_INPUT),
1799         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x1, HDA_INPUT),
1800         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x06, HDA_INPUT),
1801         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x06, HDA_INPUT),
1802         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x0b, 0x07, HDA_INPUT),
1803         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x0b, 0x07, HDA_INPUT),
1804         {
1805                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1806                 .name = "Channel Mode",
1807                 .info = alc_ch_mode_info,
1808                 .get = alc_ch_mode_get,
1809                 .put = alc_ch_mode_put,
1810         },
1811         { } /* end */
1812 };
1813
1814 static struct hda_verb alc880_lg_init_verbs[] = {
1815         /* set capture source to mic-in */
1816         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1817         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1818         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1819         /* mute all amp mixer inputs */
1820         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(5)},
1821         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(6)},
1822         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(7)},
1823         /* line-in to input */
1824         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1825         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1826         /* built-in mic */
1827         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1828         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1829         /* speaker-out */
1830         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1831         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1832         /* mic-in to input */
1833         {0x11, AC_VERB_SET_CONNECT_SEL, 0x01},
1834         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1835         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1836         /* HP-out */
1837         {0x13, AC_VERB_SET_CONNECT_SEL, 0x03},
1838         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1839         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1840         /* jack sense */
1841         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | 0x1},
1842         { }
1843 };
1844
1845 /* toggle speaker-output according to the hp-jack state */
1846 static void alc880_lg_automute(struct hda_codec *codec)
1847 {
1848         unsigned int present;
1849         unsigned char bits;
1850
1851         present = snd_hda_codec_read(codec, 0x1b, 0,
1852                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1853         bits = present ? 0x80 : 0;
1854         snd_hda_codec_amp_update(codec, 0x17, 0, HDA_OUTPUT, 0,
1855                                  0x80, bits);
1856         snd_hda_codec_amp_update(codec, 0x17, 1, HDA_OUTPUT, 0,
1857                                  0x80, bits);
1858 }
1859
1860 static void alc880_lg_unsol_event(struct hda_codec *codec, unsigned int res)
1861 {
1862         /* Looks like the unsol event is incompatible with the standard
1863          * definition.  4bit tag is placed at 28 bit!
1864          */
1865         if ((res >> 28) == 0x01)
1866                 alc880_lg_automute(codec);
1867 }
1868
1869 /*
1870  * LG LW20
1871  *
1872  * Pin assignment:
1873  *   Speaker-out: 0x14
1874  *   Mic-In: 0x18
1875  *   Built-in Mic-In: 0x19 (?)
1876  *   HP-Out: 0x1b
1877  *   SPDIF-Out: 0x1e
1878  */
1879
1880 /* seems analog CD is not working */
1881 static struct hda_input_mux alc880_lg_lw_capture_source = {
1882         .num_items = 2,
1883         .items = {
1884                 { "Mic", 0x0 },
1885                 { "Internal Mic", 0x1 },
1886         },
1887 };
1888
1889 static struct snd_kcontrol_new alc880_lg_lw_mixer[] = {
1890         HDA_CODEC_VOLUME("Master Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
1891         HDA_BIND_MUTE("Master Playback Switch", 0x0c, 2, HDA_INPUT),
1892         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
1893         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
1894         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x0b, 0x01, HDA_INPUT),
1895         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x0b, 0x01, HDA_INPUT),
1896         { } /* end */
1897 };
1898
1899 static struct hda_verb alc880_lg_lw_init_verbs[] = {
1900         /* set capture source to mic-in */
1901         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1902         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1903         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1904         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(7)},
1905         /* speaker-out */
1906         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1907         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1908         /* HP-out */
1909         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00},
1910         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1911         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1912         /* mic-in to input */
1913         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1914         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1915         /* built-in mic */
1916         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1917         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1918         /* jack sense */
1919         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | 0x1},
1920         { }
1921 };
1922
1923 /* toggle speaker-output according to the hp-jack state */
1924 static void alc880_lg_lw_automute(struct hda_codec *codec)
1925 {
1926         unsigned int present;
1927         unsigned char bits;
1928
1929         present = snd_hda_codec_read(codec, 0x1b, 0,
1930                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1931         bits = present ? 0x80 : 0;
1932         snd_hda_codec_amp_update(codec, 0x14, 0, HDA_OUTPUT, 0,
1933                                  0x80, bits);
1934         snd_hda_codec_amp_update(codec, 0x14, 1, HDA_OUTPUT, 0,
1935                                  0x80, bits);
1936 }
1937
1938 static void alc880_lg_lw_unsol_event(struct hda_codec *codec, unsigned int res)
1939 {
1940         /* Looks like the unsol event is incompatible with the standard
1941          * definition.  4bit tag is placed at 28 bit!
1942          */
1943         if ((res >> 28) == 0x01)
1944                 alc880_lg_lw_automute(codec);
1945 }
1946
1947 /*
1948  * Common callbacks
1949  */
1950
1951 static int alc_init(struct hda_codec *codec)
1952 {
1953         struct alc_spec *spec = codec->spec;
1954         unsigned int i;
1955
1956         for (i = 0; i < spec->num_init_verbs; i++)
1957                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
1958
1959         if (spec->init_hook)
1960                 spec->init_hook(codec);
1961
1962         return 0;
1963 }
1964
1965 static void alc_unsol_event(struct hda_codec *codec, unsigned int res)
1966 {
1967         struct alc_spec *spec = codec->spec;
1968
1969         if (spec->unsol_event)
1970                 spec->unsol_event(codec, res);
1971 }
1972
1973 #ifdef CONFIG_PM
1974 /*
1975  * resume
1976  */
1977 static int alc_resume(struct hda_codec *codec)
1978 {
1979         struct alc_spec *spec = codec->spec;
1980         int i;
1981
1982         alc_init(codec);
1983         for (i = 0; i < spec->num_mixers; i++)
1984                 snd_hda_resume_ctls(codec, spec->mixers[i]);
1985         if (spec->multiout.dig_out_nid)
1986                 snd_hda_resume_spdif_out(codec);
1987         if (spec->dig_in_nid)
1988                 snd_hda_resume_spdif_in(codec);
1989
1990         return 0;
1991 }
1992 #endif
1993
1994 /*
1995  * Analog playback callbacks
1996  */
1997 static int alc880_playback_pcm_open(struct hda_pcm_stream *hinfo,
1998                                     struct hda_codec *codec,
1999                                     struct snd_pcm_substream *substream)
2000 {
2001         struct alc_spec *spec = codec->spec;
2002         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
2003 }
2004
2005 static int alc880_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2006                                        struct hda_codec *codec,
2007                                        unsigned int stream_tag,
2008                                        unsigned int format,
2009                                        struct snd_pcm_substream *substream)
2010 {
2011         struct alc_spec *spec = codec->spec;
2012         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
2013                                                 stream_tag, format, substream);
2014 }
2015
2016 static int alc880_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2017                                        struct hda_codec *codec,
2018                                        struct snd_pcm_substream *substream)
2019 {
2020         struct alc_spec *spec = codec->spec;
2021         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
2022 }
2023
2024 /*
2025  * Digital out
2026  */
2027 static int alc880_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
2028                                         struct hda_codec *codec,
2029                                         struct snd_pcm_substream *substream)
2030 {
2031         struct alc_spec *spec = codec->spec;
2032         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2033 }
2034
2035 static int alc880_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2036                                            struct hda_codec *codec,
2037                                            unsigned int stream_tag,
2038                                            unsigned int format,
2039                                            struct snd_pcm_substream *substream)
2040 {
2041         struct alc_spec *spec = codec->spec;
2042         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2043                                              stream_tag, format, substream);
2044 }
2045
2046 static int alc880_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
2047                                          struct hda_codec *codec,
2048                                          struct snd_pcm_substream *substream)
2049 {
2050         struct alc_spec *spec = codec->spec;
2051         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2052 }
2053
2054 /*
2055  * Analog capture
2056  */
2057 static int alc880_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2058                                       struct hda_codec *codec,
2059                                       unsigned int stream_tag,
2060                                       unsigned int format,
2061                                       struct snd_pcm_substream *substream)
2062 {
2063         struct alc_spec *spec = codec->spec;
2064
2065         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
2066                                    stream_tag, 0, format);
2067         return 0;
2068 }
2069
2070 static int alc880_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2071                                       struct hda_codec *codec,
2072                                       struct snd_pcm_substream *substream)
2073 {
2074         struct alc_spec *spec = codec->spec;
2075
2076         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
2077                                    0, 0, 0);
2078         return 0;
2079 }
2080
2081
2082 /*
2083  */
2084 static struct hda_pcm_stream alc880_pcm_analog_playback = {
2085         .substreams = 1,
2086         .channels_min = 2,
2087         .channels_max = 8,
2088         /* NID is set in alc_build_pcms */
2089         .ops = {
2090                 .open = alc880_playback_pcm_open,
2091                 .prepare = alc880_playback_pcm_prepare,
2092                 .cleanup = alc880_playback_pcm_cleanup
2093         },
2094 };
2095
2096 static struct hda_pcm_stream alc880_pcm_analog_capture = {
2097         .substreams = 2,
2098         .channels_min = 2,
2099         .channels_max = 2,
2100         /* NID is set in alc_build_pcms */
2101         .ops = {
2102                 .prepare = alc880_capture_pcm_prepare,
2103                 .cleanup = alc880_capture_pcm_cleanup
2104         },
2105 };
2106
2107 static struct hda_pcm_stream alc880_pcm_digital_playback = {
2108         .substreams = 1,
2109         .channels_min = 2,
2110         .channels_max = 2,
2111         /* NID is set in alc_build_pcms */
2112         .ops = {
2113                 .open = alc880_dig_playback_pcm_open,
2114                 .close = alc880_dig_playback_pcm_close,
2115                 .prepare = alc880_dig_playback_pcm_prepare
2116         },
2117 };
2118
2119 static struct hda_pcm_stream alc880_pcm_digital_capture = {
2120         .substreams = 1,
2121         .channels_min = 2,
2122         .channels_max = 2,
2123         /* NID is set in alc_build_pcms */
2124 };
2125
2126 /* Used by alc_build_pcms to flag that a PCM has no playback stream */
2127 static struct hda_pcm_stream alc_pcm_null_playback = {
2128         .substreams = 0,
2129         .channels_min = 0,
2130         .channels_max = 0,
2131 };
2132
2133 static int alc_build_pcms(struct hda_codec *codec)
2134 {
2135         struct alc_spec *spec = codec->spec;
2136         struct hda_pcm *info = spec->pcm_rec;
2137         int i;
2138
2139         codec->num_pcms = 1;
2140         codec->pcm_info = info;
2141
2142         info->name = spec->stream_name_analog;
2143         if (spec->stream_analog_playback) {
2144                 snd_assert(spec->multiout.dac_nids, return -EINVAL);
2145                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *(spec->stream_analog_playback);
2146                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
2147         }
2148         if (spec->stream_analog_capture) {
2149                 snd_assert(spec->adc_nids, return -EINVAL);
2150                 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_analog_capture);
2151                 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
2152         }
2153
2154         if (spec->channel_mode) {
2155                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = 0;
2156                 for (i = 0; i < spec->num_channel_mode; i++) {
2157                         if (spec->channel_mode[i].channels > info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max) {
2158                                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->channel_mode[i].channels;
2159                         }
2160                 }
2161         }
2162
2163         /* SPDIF for stream index #1 */
2164         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
2165                 codec->num_pcms = 2;
2166                 info = spec->pcm_rec + 1;
2167                 info->name = spec->stream_name_digital;
2168                 if (spec->multiout.dig_out_nid &&
2169                     spec->stream_digital_playback) {
2170                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *(spec->stream_digital_playback);
2171                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
2172                 }
2173                 if (spec->dig_in_nid &&
2174                     spec->stream_digital_capture) {
2175                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_digital_capture);
2176                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
2177                 }
2178         }
2179
2180         /* If the use of more than one ADC is requested for the current
2181          * model, configure a second analog capture-only PCM.
2182          */
2183         /* Additional Analaog capture for index #2 */
2184         if (spec->num_adc_nids > 1 && spec->stream_analog_capture &&
2185             spec->adc_nids) {
2186                 codec->num_pcms = 3;
2187                 info = spec->pcm_rec + 2;
2188                 info->name = spec->stream_name_analog;
2189                 /* No playback stream for second PCM */
2190                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = alc_pcm_null_playback;
2191                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
2192                 if (spec->stream_analog_capture) {
2193                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_analog_capture);
2194                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[1];
2195                 }
2196         }
2197
2198         return 0;
2199 }
2200
2201 static void alc_free(struct hda_codec *codec)
2202 {
2203         struct alc_spec *spec = codec->spec;
2204         unsigned int i;
2205
2206         if (!spec)
2207                 return;
2208
2209         if (spec->kctl_alloc) {
2210                 for (i = 0; i < spec->num_kctl_used; i++)
2211                         kfree(spec->kctl_alloc[i].name);
2212                 kfree(spec->kctl_alloc);
2213         }
2214         kfree(spec);
2215 }
2216
2217 /*
2218  */
2219 static struct hda_codec_ops alc_patch_ops = {
2220         .build_controls = alc_build_controls,
2221         .build_pcms = alc_build_pcms,
2222         .init = alc_init,
2223         .free = alc_free,
2224         .unsol_event = alc_unsol_event,
2225 #ifdef CONFIG_PM
2226         .resume = alc_resume,
2227 #endif
2228 };
2229
2230
2231 /*
2232  * Test configuration for debugging
2233  *
2234  * Almost all inputs/outputs are enabled.  I/O pins can be configured via
2235  * enum controls.
2236  */
2237 #ifdef CONFIG_SND_DEBUG
2238 static hda_nid_t alc880_test_dac_nids[4] = {
2239         0x02, 0x03, 0x04, 0x05
2240 };
2241
2242 static struct hda_input_mux alc880_test_capture_source = {
2243         .num_items = 7,
2244         .items = {
2245                 { "In-1", 0x0 },
2246                 { "In-2", 0x1 },
2247                 { "In-3", 0x2 },
2248                 { "In-4", 0x3 },
2249                 { "CD", 0x4 },
2250                 { "Front", 0x5 },
2251                 { "Surround", 0x6 },
2252         },
2253 };
2254
2255 static struct hda_channel_mode alc880_test_modes[4] = {
2256         { 2, NULL },
2257         { 4, NULL },
2258         { 6, NULL },
2259         { 8, NULL },
2260 };
2261
2262 static int alc_test_pin_ctl_info(struct snd_kcontrol *kcontrol,
2263                                  struct snd_ctl_elem_info *uinfo)
2264 {
2265         static char *texts[] = {
2266                 "N/A", "Line Out", "HP Out",
2267                 "In Hi-Z", "In 50%", "In Grd", "In 80%", "In 100%"
2268         };
2269         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2270         uinfo->count = 1;
2271         uinfo->value.enumerated.items = 8;
2272         if (uinfo->value.enumerated.item >= 8)
2273                 uinfo->value.enumerated.item = 7;
2274         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2275         return 0;
2276 }
2277
2278 static int alc_test_pin_ctl_get(struct snd_kcontrol *kcontrol,
2279                                 struct snd_ctl_elem_value *ucontrol)
2280 {
2281         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2282         hda_nid_t nid = (hda_nid_t)kcontrol->private_value;
2283         unsigned int pin_ctl, item = 0;
2284
2285         pin_ctl = snd_hda_codec_read(codec, nid, 0,
2286                                      AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2287         if (pin_ctl & AC_PINCTL_OUT_EN) {
2288                 if (pin_ctl & AC_PINCTL_HP_EN)
2289                         item = 2;
2290                 else
2291                         item = 1;
2292         } else if (pin_ctl & AC_PINCTL_IN_EN) {
2293                 switch (pin_ctl & AC_PINCTL_VREFEN) {
2294                 case AC_PINCTL_VREF_HIZ: item = 3; break;
2295                 case AC_PINCTL_VREF_50:  item = 4; break;
2296                 case AC_PINCTL_VREF_GRD: item = 5; break;
2297                 case AC_PINCTL_VREF_80:  item = 6; break;
2298                 case AC_PINCTL_VREF_100: item = 7; break;
2299                 }
2300         }
2301         ucontrol->value.enumerated.item[0] = item;
2302         return 0;
2303 }
2304
2305 static int alc_test_pin_ctl_put(struct snd_kcontrol *kcontrol,
2306                                 struct snd_ctl_elem_value *ucontrol)
2307 {
2308         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2309         hda_nid_t nid = (hda_nid_t)kcontrol->private_value;
2310         static unsigned int ctls[] = {
2311                 0, AC_PINCTL_OUT_EN, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN,
2312                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_HIZ,
2313                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_50,
2314                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_GRD,
2315                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_80,
2316                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_100,
2317         };
2318         unsigned int old_ctl, new_ctl;
2319
2320         old_ctl = snd_hda_codec_read(codec, nid, 0,
2321                                      AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2322         new_ctl = ctls[ucontrol->value.enumerated.item[0]];
2323         if (old_ctl != new_ctl) {
2324                 snd_hda_codec_write(codec, nid, 0,
2325                                     AC_VERB_SET_PIN_WIDGET_CONTROL, new_ctl);
2326                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2327                                     (ucontrol->value.enumerated.item[0] >= 3 ?
2328                                      0xb080 : 0xb000));
2329                 return 1;
2330         }
2331         return 0;
2332 }
2333
2334 static int alc_test_pin_src_info(struct snd_kcontrol *kcontrol,
2335                                  struct snd_ctl_elem_info *uinfo)
2336 {
2337         static char *texts[] = {
2338                 "Front", "Surround", "CLFE", "Side"
2339         };
2340         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2341         uinfo->count = 1;
2342         uinfo->value.enumerated.items = 4;
2343         if (uinfo->value.enumerated.item >= 4)
2344                 uinfo->value.enumerated.item = 3;
2345         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
2346         return 0;
2347 }
2348
2349 static int alc_test_pin_src_get(struct snd_kcontrol *kcontrol,
2350                                 struct snd_ctl_elem_value *ucontrol)
2351 {
2352         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2353         hda_nid_t nid = (hda_nid_t)kcontrol->private_value;
2354         unsigned int sel;
2355
2356         sel = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_SEL, 0);
2357         ucontrol->value.enumerated.item[0] = sel & 3;
2358         return 0;
2359 }
2360
2361 static int alc_test_pin_src_put(struct snd_kcontrol *kcontrol,
2362                                 struct snd_ctl_elem_value *ucontrol)
2363 {
2364         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2365         hda_nid_t nid = (hda_nid_t)kcontrol->private_value;
2366         unsigned int sel;
2367
2368         sel = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_SEL, 0) & 3;
2369         if (ucontrol->value.enumerated.item[0] != sel) {
2370                 sel = ucontrol->value.enumerated.item[0] & 3;
2371                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, sel);
2372                 return 1;
2373         }
2374         return 0;
2375 }
2376
2377 #define PIN_CTL_TEST(xname,nid) {                       \
2378                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,    \
2379                         .name = xname,                 \
2380                         .info = alc_test_pin_ctl_info, \
2381                         .get = alc_test_pin_ctl_get,   \
2382                         .put = alc_test_pin_ctl_put,   \
2383                         .private_value = nid           \
2384                         }
2385
2386 #define PIN_SRC_TEST(xname,nid) {                       \
2387                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,    \
2388                         .name = xname,                 \
2389                         .info = alc_test_pin_src_info, \
2390                         .get = alc_test_pin_src_get,   \
2391                         .put = alc_test_pin_src_put,   \
2392                         .private_value = nid           \
2393                         }
2394
2395 static struct snd_kcontrol_new alc880_test_mixer[] = {
2396         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
2397         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
2398         HDA_CODEC_VOLUME("CLFE Playback Volume", 0x0e, 0x0, HDA_OUTPUT),
2399         HDA_CODEC_VOLUME("Side Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
2400         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
2401         HDA_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
2402         HDA_BIND_MUTE("CLFE Playback Switch", 0x0e, 2, HDA_INPUT),
2403         HDA_BIND_MUTE("Side Playback Switch", 0x0f, 2, HDA_INPUT),
2404         PIN_CTL_TEST("Front Pin Mode", 0x14),
2405         PIN_CTL_TEST("Surround Pin Mode", 0x15),
2406         PIN_CTL_TEST("CLFE Pin Mode", 0x16),
2407         PIN_CTL_TEST("Side Pin Mode", 0x17),
2408         PIN_CTL_TEST("In-1 Pin Mode", 0x18),
2409         PIN_CTL_TEST("In-2 Pin Mode", 0x19),
2410         PIN_CTL_TEST("In-3 Pin Mode", 0x1a),
2411         PIN_CTL_TEST("In-4 Pin Mode", 0x1b),
2412         PIN_SRC_TEST("In-1 Pin Source", 0x18),
2413         PIN_SRC_TEST("In-2 Pin Source", 0x19),
2414         PIN_SRC_TEST("In-3 Pin Source", 0x1a),
2415         PIN_SRC_TEST("In-4 Pin Source", 0x1b),
2416         HDA_CODEC_VOLUME("In-1 Playback Volume", 0x0b, 0x0, HDA_INPUT),
2417         HDA_CODEC_MUTE("In-1 Playback Switch", 0x0b, 0x0, HDA_INPUT),
2418         HDA_CODEC_VOLUME("In-2 Playback Volume", 0x0b, 0x1, HDA_INPUT),
2419         HDA_CODEC_MUTE("In-2 Playback Switch", 0x0b, 0x1, HDA_INPUT),
2420         HDA_CODEC_VOLUME("In-3 Playback Volume", 0x0b, 0x2, HDA_INPUT),
2421         HDA_CODEC_MUTE("In-3 Playback Switch", 0x0b, 0x2, HDA_INPUT),
2422         HDA_CODEC_VOLUME("In-4 Playback Volume", 0x0b, 0x3, HDA_INPUT),
2423         HDA_CODEC_MUTE("In-4 Playback Switch", 0x0b, 0x3, HDA_INPUT),
2424         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x4, HDA_INPUT),
2425         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x4, HDA_INPUT),
2426         {
2427                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2428                 .name = "Channel Mode",
2429                 .info = alc_ch_mode_info,
2430                 .get = alc_ch_mode_get,
2431                 .put = alc_ch_mode_put,
2432         },
2433         { } /* end */
2434 };
2435
2436 static struct hda_verb alc880_test_init_verbs[] = {
2437         /* Unmute inputs of 0x0c - 0x0f */
2438         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2439         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2440         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2441         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2442         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2443         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2444         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2445         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2446         /* Vol output for 0x0c-0x0f */
2447         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2448         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2449         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2450         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2451         /* Set output pins 0x14-0x17 */
2452         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2453         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2454         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2455         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2456         /* Unmute output pins 0x14-0x17 */
2457         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2458         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2459         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2460         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2461         /* Set input pins 0x18-0x1c */
2462         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2463         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2464         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2465         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2466         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2467         /* Mute input pins 0x18-0x1b */
2468         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2469         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2470         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2471         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2472         /* ADC set up */
2473         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2474         {0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
2475         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2476         {0x08, AC_VERB_SET_CONNECT_SEL, 0x00},
2477         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2478         {0x09, AC_VERB_SET_CONNECT_SEL, 0x00},
2479         /* Analog input/passthru */
2480         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2481         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2482         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2483         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2484         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2485         { }
2486 };
2487 #endif
2488
2489 /*
2490  */
2491
2492 static const char *alc880_models[ALC880_MODEL_LAST] = {
2493         [ALC880_3ST]            = "3stack",
2494         [ALC880_TCL_S700]       = "tcl",
2495         [ALC880_3ST_DIG]        = "3stack-digout",
2496         [ALC880_CLEVO]          = "clevo",
2497         [ALC880_5ST]            = "5stack",
2498         [ALC880_5ST_DIG]        = "5stack-digout",
2499         [ALC880_W810]           = "w810",
2500         [ALC880_Z71V]           = "z71v",
2501         [ALC880_6ST]            = "6stack",
2502         [ALC880_6ST_DIG]        = "6stack-digout",
2503         [ALC880_ASUS]           = "asus",
2504         [ALC880_ASUS_W1V]       = "asus-w1v",
2505         [ALC880_ASUS_DIG]       = "asus-dig",
2506         [ALC880_ASUS_DIG2]      = "asus-dig2",
2507         [ALC880_UNIWILL_DIG]    = "uniwill",
2508         [ALC880_UNIWILL_P53]    = "uniwill-p53",
2509         [ALC880_FUJITSU]        = "fujitsu",
2510         [ALC880_F1734]          = "F1734",
2511         [ALC880_LG]             = "lg",
2512         [ALC880_LG_LW]          = "lg-lw",
2513 #ifdef CONFIG_SND_DEBUG
2514         [ALC880_TEST]           = "test",
2515 #endif
2516         [ALC880_AUTO]           = "auto",
2517 };
2518
2519 static struct snd_pci_quirk alc880_cfg_tbl[] = {
2520         /* Broken BIOS configuration */
2521         SND_PCI_QUIRK(0x2668, 0x8086, NULL, ALC880_6ST_DIG),
2522         SND_PCI_QUIRK(0x8086, 0x2668, NULL, ALC880_6ST_DIG),
2523
2524         SND_PCI_QUIRK(0x1019, 0xa880, "ECS", ALC880_5ST_DIG),
2525         SND_PCI_QUIRK(0x1019, 0xa884, "Acer APFV", ALC880_6ST),
2526         SND_PCI_QUIRK(0x1019, 0x0f69, "Coeus G610P", ALC880_W810),
2527         SND_PCI_QUIRK(0x1025, 0x0070, "ULI", ALC880_3ST_DIG),
2528         SND_PCI_QUIRK(0x1025, 0x0077, "ULI", ALC880_6ST_DIG),
2529         SND_PCI_QUIRK(0x1025, 0x0078, "ULI", ALC880_6ST_DIG),
2530         SND_PCI_QUIRK(0x1025, 0x0087, "ULI", ALC880_6ST_DIG),
2531         SND_PCI_QUIRK(0x1025, 0xe309, "ULI", ALC880_3ST_DIG),
2532         SND_PCI_QUIRK(0x1025, 0xe310, "ULI", ALC880_3ST),
2533
2534         SND_PCI_QUIRK(0x1039, 0x1234, NULL, ALC880_6ST_DIG),
2535         SND_PCI_QUIRK(0x103c, 0x2a09, "HP", ALC880_5ST),
2536
2537         SND_PCI_QUIRK(0x1043, 0x10b3, "ASUS W1V", ALC880_ASUS_W1V),
2538         SND_PCI_QUIRK(0x1043, 0x10c2, "ASUS W6A", ALC880_ASUS_DIG),
2539         SND_PCI_QUIRK(0x1043, 0x10c3, "ASUS Wxx", ALC880_ASUS_DIG),
2540         SND_PCI_QUIRK(0x1043, 0x1113, "ASUS", ALC880_ASUS_DIG),
2541         SND_PCI_QUIRK(0x1043, 0x1123, "ASUS", ALC880_ASUS_DIG),
2542         SND_PCI_QUIRK(0x1043, 0x1173, "ASUS", ALC880_ASUS_DIG),
2543         SND_PCI_QUIRK(0x1043, 0x1964, "ASUS Z71V", ALC880_Z71V),
2544         /* SND_PCI_QUIRK(0x1043, 0x1964, "ASUS", ALC880_ASUS_DIG), */
2545         SND_PCI_QUIRK(0x1043, 0x1973, "ASUS", ALC880_ASUS_DIG),
2546         SND_PCI_QUIRK(0x1043, 0x19b3, "ASUS", ALC880_ASUS_DIG),
2547         SND_PCI_QUIRK(0x1043, 0x814e, "ASUS", ALC880_ASUS),
2548         SND_PCI_QUIRK(0x1043, 0x8181, "ASUS P4GPL", ALC880_ASUS_DIG),
2549         SND_PCI_QUIRK(0x1043, 0x8196, "ASUS P5GD1", ALC880_6ST),
2550         SND_PCI_QUIRK(0x1043, 0x81b4, "ASUS", ALC880_6ST),
2551         SND_PCI_QUIRK(0x1043, 0, "ASUS", ALC880_ASUS),
2552
2553         SND_PCI_QUIRK(0x104d, 0x81d6, "Sony", ALC880_3ST),
2554         SND_PCI_QUIRK(0x104d, 0x81a0, "Sony", ALC880_3ST),
2555         SND_PCI_QUIRK(0x107b, 0x3033, "Gateway", ALC880_5ST),
2556         SND_PCI_QUIRK(0x107b, 0x4039, "Gateway", ALC880_5ST),
2557         SND_PCI_QUIRK(0x107b, 0x3032, "Gateway", ALC880_5ST),
2558         SND_PCI_QUIRK(0x1558, 0x0520, "Clevo m520G", ALC880_CLEVO),
2559         SND_PCI_QUIRK(0x1558, 0x0660, "Clevo m655n", ALC880_CLEVO),
2560         SND_PCI_QUIRK(0x1565, 0x8202, "Biostar", ALC880_5ST_DIG),
2561         SND_PCI_QUIRK(0x161f, 0x203d, "W810", ALC880_W810),
2562         SND_PCI_QUIRK(0x1695, 0x400d, "EPoX", ALC880_5ST_DIG),
2563         SND_PCI_QUIRK(0x19db, 0x4188, "TCL S700", ALC880_TCL_S700),
2564         SND_PCI_QUIRK(0xa0a0, 0x0560, "AOpen i915GMm-HFS", ALC880_5ST_DIG),
2565         SND_PCI_QUIRK(0xe803, 0x1019, NULL, ALC880_6ST_DIG),
2566         SND_PCI_QUIRK(0x1297, 0xc790, "Shuttle ST20G5", ALC880_6ST_DIG),
2567         SND_PCI_QUIRK(0x1458, 0xa102, "Gigabyte K8", ALC880_6ST_DIG),
2568         SND_PCI_QUIRK(0x1462, 0x1150, "MSI", ALC880_6ST_DIG),
2569         SND_PCI_QUIRK(0x1509, 0x925d, "FIC P4M", ALC880_6ST_DIG),
2570         SND_PCI_QUIRK(0x1558, 0x5401, "ASUS", ALC880_ASUS_DIG2),
2571
2572         SND_PCI_QUIRK(0x1584, 0x9050, "Uniwill", ALC880_UNIWILL_DIG),
2573         SND_PCI_QUIRK(0x1584, 0x9070, "Uniwill", ALC880_UNIWILL),
2574         SND_PCI_QUIRK(0x1584, 0x9077, "Uniwill P53", ALC880_UNIWILL_P53),
2575         SND_PCI_QUIRK(0x1584, 0x9054, "Uniwlll", ALC880_F1734),
2576
2577         SND_PCI_QUIRK(0x1695, 0x4012, "EPox EP-5LDA", ALC880_5ST_DIG),
2578         SND_PCI_QUIRK(0x1734, 0x10ac, "FSC", ALC880_UNIWILL),
2579         SND_PCI_QUIRK(0x1734, 0x107c, "FSC F1734", ALC880_F1734),
2580         SND_PCI_QUIRK(0x1734, 0x10b0, "Fujitsu", ALC880_FUJITSU),
2581
2582         SND_PCI_QUIRK(0x1854, 0x003b, "LG", ALC880_LG),
2583         SND_PCI_QUIRK(0x1854, 0x0068, "LG w1", ALC880_LG),
2584         SND_PCI_QUIRK(0x1854, 0x0018, "LG LW20", ALC880_LG_LW),
2585         SND_PCI_QUIRK(0x1854, 0x0077, "LG LW25", ALC880_LG_LW),
2586
2587         SND_PCI_QUIRK(0x8086, 0xe308, "Intel mobo", ALC880_3ST_DIG),
2588         SND_PCI_QUIRK(0x8086, 0xe305, "Intel mobo", ALC880_3ST_DIG),
2589         SND_PCI_QUIRK(0x8086, 0xd402, "Intel mobo", ALC880_3ST_DIG),
2590         SND_PCI_QUIRK(0x8086, 0xd400, "Intel mobo", ALC880_5ST_DIG),
2591         SND_PCI_QUIRK(0x8086, 0xd401, "Intel mobo", ALC880_5ST_DIG),
2592         SND_PCI_QUIRK(0x8086, 0xe224, "Intel mobo", ALC880_5ST_DIG),
2593         SND_PCI_QUIRK(0x8086, 0xe400, "Intel mobo", ALC880_5ST_DIG),
2594         SND_PCI_QUIRK(0x8086, 0xe401, "Intel mobo", ALC880_5ST_DIG),
2595         SND_PCI_QUIRK(0x8086, 0xe402, "Intel mobo", ALC880_5ST_DIG),
2596         SND_PCI_QUIRK(0x8086, 0xa100, "Intel mobo", ALC880_5ST_DIG),
2597         SND_PCI_QUIRK(0x8086, 0, "Intel mobo", ALC880_3ST),
2598
2599         {}
2600 };
2601
2602 /*
2603  * ALC880 codec presets
2604  */
2605 static struct alc_config_preset alc880_presets[] = {
2606         [ALC880_3ST] = {
2607                 .mixers = { alc880_three_stack_mixer },
2608                 .init_verbs = { alc880_volume_init_verbs,
2609                                 alc880_pin_3stack_init_verbs },
2610                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
2611                 .dac_nids = alc880_dac_nids,
2612                 .num_channel_mode = ARRAY_SIZE(alc880_threestack_modes),
2613                 .channel_mode = alc880_threestack_modes,
2614                 .need_dac_fix = 1,
2615                 .input_mux = &alc880_capture_source,
2616         },
2617         [ALC880_3ST_DIG] = {
2618                 .mixers = { alc880_three_stack_mixer },
2619                 .init_verbs = { alc880_volume_init_verbs,
2620                                 alc880_pin_3stack_init_verbs },
2621                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
2622                 .dac_nids = alc880_dac_nids,
2623                 .dig_out_nid = ALC880_DIGOUT_NID,
2624                 .num_channel_mode = ARRAY_SIZE(alc880_threestack_modes),
2625                 .channel_mode = alc880_threestack_modes,
2626                 .need_dac_fix = 1,
2627                 .input_mux = &alc880_capture_source,
2628         },
2629         [ALC880_TCL_S700] = {
2630                 .mixers = { alc880_tcl_s700_mixer },
2631                 .init_verbs = { alc880_volume_init_verbs,
2632                                 alc880_pin_tcl_S700_init_verbs,
2633                                 alc880_gpio2_init_verbs },
2634                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
2635                 .dac_nids = alc880_dac_nids,
2636                 .hp_nid = 0x03,
2637                 .num_channel_mode = ARRAY_SIZE(alc880_2_jack_modes),
2638                 .channel_mode = alc880_2_jack_modes,
2639                 .input_mux = &alc880_capture_source,
2640         },
2641         [ALC880_5ST] = {
2642                 .mixers = { alc880_three_stack_mixer,
2643                             alc880_five_stack_mixer},
2644                 .init_verbs = { alc880_volume_init_verbs,
2645                                 alc880_pin_5stack_init_verbs },
2646                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
2647                 .dac_nids = alc880_dac_nids,
2648                 .num_channel_mode = ARRAY_SIZE(alc880_fivestack_modes),
2649                 .channel_mode = alc880_fivestack_modes,
2650                 .input_mux = &alc880_capture_source,
2651         },
2652         [ALC880_5ST_DIG] = {
2653                 .mixers = { alc880_three_stack_mixer,
2654                             alc880_five_stack_mixer },
2655                 .init_verbs = { alc880_volume_init_verbs,
2656                                 alc880_pin_5stack_init_verbs },
2657                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
2658                 .dac_nids = alc880_dac_nids,
2659                 .dig_out_nid = ALC880_DIGOUT_NID,
2660                 .num_channel_mode = ARRAY_SIZE(alc880_fivestack_modes),
2661                 .channel_mode = alc880_fivestack_modes,
2662                 .input_mux = &alc880_capture_source,
2663         },
2664         [ALC880_6ST] = {
2665                 .mixers = { alc880_six_stack_mixer },
2666                 .init_verbs = { alc880_volume_init_verbs,
2667                                 alc880_pin_6stack_init_verbs },
2668                 .num_dacs = ARRAY_SIZE(alc880_6st_dac_nids),
2669                 .dac_nids = alc880_6st_dac_nids,
2670                 .num_channel_mode = ARRAY_SIZE(alc880_sixstack_modes),
2671                 .channel_mode = alc880_sixstack_modes,
2672                 .input_mux = &alc880_6stack_capture_source,
2673         },
2674         [ALC880_6ST_DIG] = {
2675                 .mixers = { alc880_six_stack_mixer },
2676                 .init_verbs = { alc880_volume_init_verbs,
2677                                 alc880_pin_6stack_init_verbs },
2678                 .num_dacs = ARRAY_SIZE(alc880_6st_dac_nids),
2679                 .dac_nids = alc880_6st_dac_nids,
2680                 .dig_out_nid = ALC880_DIGOUT_NID,
2681                 .num_channel_mode = ARRAY_SIZE(alc880_sixstack_modes),
2682                 .channel_mode = alc880_sixstack_modes,
2683                 .input_mux = &alc880_6stack_capture_source,
2684         },
2685         [ALC880_W810] = {
2686                 .mixers = { alc880_w810_base_mixer },
2687                 .init_verbs = { alc880_volume_init_verbs,
2688                                 alc880_pin_w810_init_verbs,
2689                                 alc880_gpio2_init_verbs },
2690                 .num_dacs = ARRAY_SIZE(alc880_w810_dac_nids),
2691                 .dac_nids = alc880_w810_dac_nids,
2692                 .dig_out_nid = ALC880_DIGOUT_NID,
2693                 .num_channel_mode = ARRAY_SIZE(alc880_w810_modes),
2694                 .channel_mode = alc880_w810_modes,
2695                 .input_mux = &alc880_capture_source,
2696         },
2697         [ALC880_Z71V] = {
2698                 .mixers = { alc880_z71v_mixer },
2699                 .init_verbs = { alc880_volume_init_verbs,
2700                                 alc880_pin_z71v_init_verbs },
2701                 .num_dacs = ARRAY_SIZE(alc880_z71v_dac_nids),
2702                 .dac_nids = alc880_z71v_dac_nids,
2703                 .dig_out_nid = ALC880_DIGOUT_NID,
2704                 .hp_nid = 0x03,
2705                 .num_channel_mode = ARRAY_SIZE(alc880_2_jack_modes),
2706                 .channel_mode = alc880_2_jack_modes,
2707                 .input_mux = &alc880_capture_source,
2708         },
2709         [ALC880_F1734] = {
2710                 .mixers = { alc880_f1734_mixer },
2711                 .init_verbs = { alc880_volume_init_verbs,
2712                                 alc880_pin_f1734_init_verbs },
2713                 .num_dacs = ARRAY_SIZE(alc880_f1734_dac_nids),
2714                 .dac_nids = alc880_f1734_dac_nids,
2715                 .hp_nid = 0x02,
2716                 .num_channel_mode = ARRAY_SIZE(alc880_2_jack_modes),
2717                 .channel_mode = alc880_2_jack_modes,
2718                 .input_mux = &alc880_capture_source,
2719         },
2720         [ALC880_ASUS] = {
2721                 .mixers = { alc880_asus_mixer },
2722                 .init_verbs = { alc880_volume_init_verbs,
2723                                 alc880_pin_asus_init_verbs,
2724                                 alc880_gpio1_init_verbs },
2725                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
2726                 .dac_nids = alc880_asus_dac_nids,
2727                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
2728                 .channel_mode = alc880_asus_modes,
2729                 .need_dac_fix = 1,
2730                 .input_mux = &alc880_capture_source,
2731         },
2732         [ALC880_ASUS_DIG] = {
2733                 .mixers = { alc880_asus_mixer },
2734                 .init_verbs = { alc880_volume_init_verbs,
2735                                 alc880_pin_asus_init_verbs,
2736                                 alc880_gpio1_init_verbs },
2737                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
2738                 .dac_nids = alc880_asus_dac_nids,
2739                 .dig_out_nid = ALC880_DIGOUT_NID,
2740                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
2741                 .channel_mode = alc880_asus_modes,
2742                 .need_dac_fix = 1,
2743                 .input_mux = &alc880_capture_source,
2744         },
2745         [ALC880_ASUS_DIG2] = {
2746                 .mixers = { alc880_asus_mixer },
2747                 .init_verbs = { alc880_volume_init_verbs,
2748                                 alc880_pin_asus_init_verbs,
2749                                 alc880_gpio2_init_verbs }, /* use GPIO2 */
2750                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
2751                 .dac_nids = alc880_asus_dac_nids,
2752                 .dig_out_nid = ALC880_DIGOUT_NID,
2753                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
2754                 .channel_mode = alc880_asus_modes,
2755                 .need_dac_fix = 1,
2756                 .input_mux = &alc880_capture_source,
2757         },
2758         [ALC880_ASUS_W1V] = {
2759                 .mixers = { alc880_asus_mixer, alc880_asus_w1v_mixer },
2760                 .init_verbs = { alc880_volume_init_verbs,
2761                                 alc880_pin_asus_init_verbs,
2762                                 alc880_gpio1_init_verbs },
2763                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
2764                 .dac_nids = alc880_asus_dac_nids,
2765                 .dig_out_nid = ALC880_DIGOUT_NID,
2766                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
2767                 .channel_mode = alc880_asus_modes,
2768                 .need_dac_fix = 1,
2769                 .input_mux = &alc880_capture_source,
2770         },
2771         [ALC880_UNIWILL_DIG] = {
2772                 .mixers = { alc880_asus_mixer, alc880_pcbeep_mixer },
2773                 .init_verbs = { alc880_volume_init_verbs,
2774                                 alc880_pin_asus_init_verbs },
2775                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
2776                 .dac_nids = alc880_asus_dac_nids,
2777                 .dig_out_nid = ALC880_DIGOUT_NID,
2778                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
2779                 .channel_mode = alc880_asus_modes,
2780                 .need_dac_fix = 1,
2781                 .input_mux = &alc880_capture_source,
2782         },
2783         [ALC880_UNIWILL] = {
2784                 .mixers = { alc880_uniwill_mixer },
2785                 .init_verbs = { alc880_volume_init_verbs,
2786                                 alc880_uniwill_init_verbs },
2787                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
2788                 .dac_nids = alc880_asus_dac_nids,
2789                 .dig_out_nid = ALC880_DIGOUT_NID,
2790                 .num_channel_mode = ARRAY_SIZE(alc880_threestack_modes),
2791                 .channel_mode = alc880_threestack_modes,
2792                 .need_dac_fix = 1,
2793                 .input_mux = &alc880_capture_source,
2794                 .unsol_event = alc880_uniwill_unsol_event,
2795                 .init_hook = alc880_uniwill_automute,
2796         },
2797         [ALC880_UNIWILL_P53] = {
2798                 .mixers = { alc880_uniwill_p53_mixer },
2799                 .init_verbs = { alc880_volume_init_verbs,
2800                                 alc880_uniwill_p53_init_verbs },
2801                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
2802                 .dac_nids = alc880_asus_dac_nids,
2803                 .num_channel_mode = ARRAY_SIZE(alc880_w810_modes),
2804                 .channel_mode = alc880_threestack_modes,
2805                 .input_mux = &alc880_capture_source,
2806                 .unsol_event = alc880_uniwill_p53_unsol_event,
2807                 .init_hook = alc880_uniwill_p53_hp_automute,
2808         },
2809         [ALC880_FUJITSU] = {
2810                 .mixers = { alc880_fujitsu_mixer,
2811                             alc880_pcbeep_mixer, },
2812                 .init_verbs = { alc880_volume_init_verbs,
2813                                 alc880_uniwill_p53_init_verbs,
2814                                 alc880_beep_init_verbs },
2815                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
2816                 .dac_nids = alc880_dac_nids,
2817                 .num_channel_mode = ARRAY_SIZE(alc880_2_jack_modes),
2818                 .channel_mode = alc880_2_jack_modes,
2819                 .input_mux = &alc880_capture_source,
2820                 .unsol_event = alc880_uniwill_p53_unsol_event,
2821                 .init_hook = alc880_uniwill_p53_hp_automute,
2822         },
2823         [ALC880_CLEVO] = {
2824                 .mixers = { alc880_three_stack_mixer },
2825                 .init_verbs = { alc880_volume_init_verbs,
2826                                 alc880_pin_clevo_init_verbs },
2827                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
2828                 .dac_nids = alc880_dac_nids,
2829                 .hp_nid = 0x03,
2830                 .num_channel_mode = ARRAY_SIZE(alc880_threestack_modes),
2831                 .channel_mode = alc880_threestack_modes,
2832                 .need_dac_fix = 1,
2833                 .input_mux = &alc880_capture_source,
2834         },
2835         [ALC880_LG] = {
2836                 .mixers = { alc880_lg_mixer },
2837                 .init_verbs = { alc880_volume_init_verbs,
2838                                 alc880_lg_init_verbs },
2839                 .num_dacs = ARRAY_SIZE(alc880_lg_dac_nids),
2840                 .dac_nids = alc880_lg_dac_nids,
2841                 .dig_out_nid = ALC880_DIGOUT_NID,
2842                 .num_channel_mode = ARRAY_SIZE(alc880_lg_ch_modes),
2843                 .channel_mode = alc880_lg_ch_modes,
2844                 .need_dac_fix = 1,
2845                 .input_mux = &alc880_lg_capture_source,
2846                 .unsol_event = alc880_lg_unsol_event,
2847                 .init_hook = alc880_lg_automute,
2848         },
2849         [ALC880_LG_LW] = {
2850                 .mixers = { alc880_lg_lw_mixer },
2851                 .init_verbs = { alc880_volume_init_verbs,
2852                                 alc880_lg_lw_init_verbs },
2853                 .num_dacs = 1,
2854                 .dac_nids = alc880_dac_nids,
2855                 .dig_out_nid = ALC880_DIGOUT_NID,
2856                 .num_channel_mode = ARRAY_SIZE(alc880_2_jack_modes),
2857                 .channel_mode = alc880_2_jack_modes,
2858                 .input_mux = &alc880_lg_lw_capture_source,
2859                 .unsol_event = alc880_lg_lw_unsol_event,
2860                 .init_hook = alc880_lg_lw_automute,
2861         },
2862 #ifdef CONFIG_SND_DEBUG
2863         [ALC880_TEST] = {
2864                 .mixers = { alc880_test_mixer },
2865                 .init_verbs = { alc880_test_init_verbs },
2866                 .num_dacs = ARRAY_SIZE(alc880_test_dac_nids),
2867                 .dac_nids = alc880_test_dac_nids,
2868                 .dig_out_nid = ALC880_DIGOUT_NID,
2869                 .num_channel_mode = ARRAY_SIZE(alc880_test_modes),
2870                 .channel_mode = alc880_test_modes,
2871                 .input_mux = &alc880_test_capture_source,
2872         },
2873 #endif
2874 };
2875
2876 /*
2877  * Automatic parse of I/O pins from the BIOS configuration
2878  */
2879
2880 #define NUM_CONTROL_ALLOC       32
2881 #define NUM_VERB_ALLOC          32
2882
2883 enum {
2884         ALC_CTL_WIDGET_VOL,
2885         ALC_CTL_WIDGET_MUTE,
2886         ALC_CTL_BIND_MUTE,
2887 };
2888 static struct snd_kcontrol_new alc880_control_templates[] = {
2889         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
2890         HDA_CODEC_MUTE(NULL, 0, 0, 0),
2891         HDA_BIND_MUTE(NULL, 0, 0, 0),
2892 };
2893
2894 /* add dynamic controls */
2895 static int add_control(struct alc_spec *spec, int type, const char *name,
2896                        unsigned long val)
2897 {
2898         struct snd_kcontrol_new *knew;
2899
2900         if (spec->num_kctl_used >= spec->num_kctl_alloc) {
2901                 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
2902
2903                 /* array + terminator */
2904                 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL);
2905                 if (!knew)
2906                         return -ENOMEM;
2907                 if (spec->kctl_alloc) {
2908                         memcpy(knew, spec->kctl_alloc,
2909                                sizeof(*knew) * spec->num_kctl_alloc);
2910                         kfree(spec->kctl_alloc);
2911                 }
2912                 spec->kctl_alloc = knew;
2913                 spec->num_kctl_alloc = num;
2914         }
2915
2916         knew = &spec->kctl_alloc[spec->num_kctl_used];
2917         *knew = alc880_control_templates[type];
2918         knew->name = kstrdup(name, GFP_KERNEL);
2919         if (!knew->name)
2920                 return -ENOMEM;
2921         knew->private_value = val;
2922         spec->num_kctl_used++;
2923         return 0;
2924 }
2925
2926 #define alc880_is_fixed_pin(nid)        ((nid) >= 0x14 && (nid) <= 0x17)
2927 #define alc880_fixed_pin_idx(nid)       ((nid) - 0x14)
2928 #define alc880_is_multi_pin(nid)        ((nid) >= 0x18)
2929 #define alc880_multi_pin_idx(nid)       ((nid) - 0x18)
2930 #define alc880_is_input_pin(nid)        ((nid) >= 0x18)
2931 #define alc880_input_pin_idx(nid)       ((nid) - 0x18)
2932 #define alc880_idx_to_dac(nid)          ((nid) + 0x02)
2933 #define alc880_dac_to_idx(nid)          ((nid) - 0x02)
2934 #define alc880_idx_to_mixer(nid)        ((nid) + 0x0c)
2935 #define alc880_idx_to_selector(nid)     ((nid) + 0x10)
2936 #define ALC880_PIN_CD_NID               0x1c
2937
2938 /* fill in the dac_nids table from the parsed pin configuration */
2939 static int alc880_auto_fill_dac_nids(struct alc_spec *spec,
2940                                      const struct auto_pin_cfg *cfg)
2941 {
2942         hda_nid_t nid;
2943         int assigned[4];
2944         int i, j;
2945
2946         memset(assigned, 0, sizeof(assigned));
2947         spec->multiout.dac_nids = spec->private_dac_nids;
2948
2949         /* check the pins hardwired to audio widget */
2950         for (i = 0; i < cfg->line_outs; i++) {
2951                 nid = cfg->line_out_pins[i];
2952                 if (alc880_is_fixed_pin(nid)) {
2953                         int idx = alc880_fixed_pin_idx(nid);
2954                         spec->multiout.dac_nids[i] = alc880_idx_to_dac(idx);
2955                         assigned[idx] = 1;
2956                 }
2957         }
2958         /* left pins can be connect to any audio widget */
2959         for (i = 0; i < cfg->line_outs; i++) {
2960                 nid = cfg->line_out_pins[i];
2961                 if (alc880_is_fixed_pin(nid))
2962                         continue;
2963                 /* search for an empty channel */
2964                 for (j = 0; j < cfg->line_outs; j++) {
2965                         if (!assigned[j]) {
2966                                 spec->multiout.dac_nids[i] =
2967                                         alc880_idx_to_dac(j);
2968                                 assigned[j] = 1;
2969                                 break;
2970                         }
2971                 }
2972         }
2973         spec->multiout.num_dacs = cfg->line_outs;
2974         return 0;
2975 }
2976
2977 /* add playback controls from the parsed DAC table */
2978 static int alc880_auto_create_multi_out_ctls(struct alc_spec *spec,
2979                                              const struct auto_pin_cfg *cfg)
2980 {
2981         char name[32];
2982         static const char *chname[4] = {
2983                 "Front", "Surround", NULL /*CLFE*/, "Side"
2984         };
2985         hda_nid_t nid;
2986         int i, err;
2987
2988         for (i = 0; i < cfg->line_outs; i++) {
2989                 if (!spec->multiout.dac_nids[i])
2990                         continue;
2991                 nid = alc880_idx_to_mixer(alc880_dac_to_idx(spec->multiout.dac_nids[i]));
2992                 if (i == 2) {
2993                         /* Center/LFE */
2994                         err = add_control(spec, ALC_CTL_WIDGET_VOL,
2995                                           "Center Playback Volume",
2996                                           HDA_COMPOSE_AMP_VAL(nid, 1, 0,
2997                                                               HDA_OUTPUT));
2998                         if (err < 0)
2999                                 return err;
3000                         err = add_control(spec, ALC_CTL_WIDGET_VOL,
3001                                           "LFE Playback Volume",
3002                                           HDA_COMPOSE_AMP_VAL(nid, 2, 0,
3003                                                               HDA_OUTPUT));
3004                         if (err < 0)
3005                                 return err;
3006                         err = add_control(spec, ALC_CTL_BIND_MUTE,
3007                                           "Center Playback Switch",
3008                                           HDA_COMPOSE_AMP_VAL(nid, 1, 2,
3009                                                               HDA_INPUT));
3010                         if (err < 0)
3011                                 return err;
3012                         err = add_control(spec, ALC_CTL_BIND_MUTE,
3013                                           "LFE Playback Switch",
3014                                           HDA_COMPOSE_AMP_VAL(nid, 2, 2,
3015                                                               HDA_INPUT));
3016                         if (err < 0)
3017                                 return err;
3018                 } else {
3019                         sprintf(name, "%s Playback Volume", chname[i]);
3020                         err = add_control(spec, ALC_CTL_WIDGET_VOL, name,
3021                                           HDA_COMPOSE_AMP_VAL(nid, 3, 0,
3022                                                               HDA_OUTPUT));
3023                         if (err < 0)
3024                                 return err;
3025                         sprintf(name, "%s Playback Switch", chname[i]);
3026                         err = add_control(spec, ALC_CTL_BIND_MUTE, name,
3027                                           HDA_COMPOSE_AMP_VAL(nid, 3, 2,
3028                                                               HDA_INPUT));
3029                         if (err < 0)
3030                                 return err;
3031                 }
3032         }
3033         return 0;
3034 }
3035
3036 /* add playback controls for speaker and HP outputs */
3037 static int alc880_auto_create_extra_out(struct alc_spec *spec, hda_nid_t pin,
3038                                         const char *pfx)
3039 {
3040         hda_nid_t nid;
3041         int err;
3042         char name[32];
3043
3044         if (!pin)
3045                 return 0;
3046
3047         if (alc880_is_fixed_pin(pin)) {
3048                 nid = alc880_idx_to_dac(alc880_fixed_pin_idx(pin));
3049                 /* specify the DAC as the extra output */
3050                 if (!spec->multiout.hp_nid)
3051                         spec->multiout.hp_nid = nid;
3052                 else
3053                         spec->multiout.extra_out_nid[0] = nid;
3054                 /* control HP volume/switch on the output mixer amp */
3055                 nid = alc880_idx_to_mixer(alc880_fixed_pin_idx(pin));
3056                 sprintf(name, "%s Playback Volume", pfx);
3057                 err = add_control(spec, ALC_CTL_WIDGET_VOL, name,
3058                                   HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT));
3059                 if (err < 0)
3060                         return err;
3061                 sprintf(name, "%s Playback Switch", pfx);
3062                 err = add_control(spec, ALC_CTL_BIND_MUTE, name,
3063                                   HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT));
3064                 if (err < 0)
3065                         return err;
3066         } else if (alc880_is_multi_pin(pin)) {
3067                 /* set manual connection */
3068                 /* we have only a switch on HP-out PIN */
3069                 sprintf(name, "%s Playback Switch", pfx);
3070                 err = add_control(spec, ALC_CTL_WIDGET_MUTE, name,
3071                                   HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3072                 if (err < 0)
3073                         return err;
3074         }
3075         return 0;
3076 }
3077
3078 /* create input playback/capture controls for the given pin */
3079 static int new_analog_input(struct alc_spec *spec, hda_nid_t pin,
3080                             const char *ctlname,
3081                             int idx, hda_nid_t mix_nid)
3082 {
3083         char name[32];
3084         int err;
3085
3086         sprintf(name, "%s Playback Volume", ctlname);
3087         err = add_control(spec, ALC_CTL_WIDGET_VOL, name,
3088                           HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
3089         if (err < 0)
3090                 return err;
3091         sprintf(name, "%s Playback Switch", ctlname);
3092         err = add_control(spec, ALC_CTL_WIDGET_MUTE, name,
3093                           HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
3094         if (err < 0)
3095                 return err;
3096         return 0;
3097 }
3098
3099 /* create playback/capture controls for input pins */
3100 static int alc880_auto_create_analog_input_ctls(struct alc_spec *spec,
3101                                                 const struct auto_pin_cfg *cfg)
3102 {
3103         struct hda_input_mux *imux = &spec->private_imux;
3104         int i, err, idx;
3105
3106         for (i = 0; i < AUTO_PIN_LAST; i++) {
3107                 if (alc880_is_input_pin(cfg->input_pins[i])) {
3108                         idx = alc880_input_pin_idx(cfg->input_pins[i]);
3109                         err = new_analog_input(spec, cfg->input_pins[i],
3110                                                auto_pin_cfg_labels[i],
3111                                                idx, 0x0b);
3112                         if (err < 0)
3113                                 return err;
3114                         imux->items[imux->num_items].label =
3115                                 auto_pin_cfg_labels[i];
3116                         imux->items[imux->num_items].index =
3117                                 alc880_input_pin_idx(cfg->input_pins[i]);
3118                         imux->num_items++;
3119                 }
3120         }
3121         return 0;
3122 }
3123
3124 static void alc880_auto_set_output_and_unmute(struct hda_codec *codec,
3125                                               hda_nid_t nid, int pin_type,
3126                                               int dac_idx)
3127 {
3128         /* set as output */
3129         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
3130                             pin_type);
3131         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3132                             AMP_OUT_UNMUTE);
3133         /* need the manual connection? */
3134         if (alc880_is_multi_pin(nid)) {
3135                 struct alc_spec *spec = codec->spec;
3136                 int idx = alc880_multi_pin_idx(nid);
3137                 snd_hda_codec_write(codec, alc880_idx_to_selector(idx), 0,
3138                                     AC_VERB_SET_CONNECT_SEL,
3139                                     alc880_dac_to_idx(spec->multiout.dac_nids[dac_idx]));
3140         }
3141 }
3142
3143 static int get_pin_type(int line_out_type)
3144 {
3145         if (line_out_type == AUTO_PIN_HP_OUT)
3146                 return PIN_HP;
3147         else
3148                 return PIN_OUT;
3149 }
3150
3151 static void alc880_auto_init_multi_out(struct hda_codec *codec)
3152 {
3153         struct alc_spec *spec = codec->spec;
3154         int i;
3155         
3156         alc_subsystem_id(codec, 0x15, 0x1b, 0x14);
3157         for (i = 0; i < spec->autocfg.line_outs; i++) {
3158                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
3159                 int pin_type = get_pin_type(spec->autocfg.line_out_type);
3160                 alc880_auto_set_output_and_unmute(codec, nid, pin_type, i);
3161         }
3162 }
3163
3164 static void alc880_auto_init_extra_out(struct hda_codec *codec)
3165 {
3166         struct alc_spec *spec = codec->spec;
3167         hda_nid_t pin;
3168
3169         pin = spec->autocfg.speaker_pins[0];
3170         if (pin) /* connect to front */
3171                 alc880_auto_set_output_and_unmute(codec, pin, PIN_OUT, 0);
3172         pin = spec->autocfg.hp_pins[0];
3173         if (pin) /* connect to front */
3174                 alc880_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
3175 }
3176
3177 static void alc880_auto_init_analog_input(struct hda_codec *codec)
3178 {
3179         struct alc_spec *spec = codec->spec;
3180         int i;
3181
3182         for (i = 0; i < AUTO_PIN_LAST; i++) {
3183                 hda_nid_t nid = spec->autocfg.input_pins[i];
3184                 if (alc880_is_input_pin(nid)) {
3185                         snd_hda_codec_write(codec, nid, 0,
3186                                             AC_VERB_SET_PIN_WIDGET_CONTROL,
3187                                             i <= AUTO_PIN_FRONT_MIC ?
3188                                             PIN_VREF80 : PIN_IN);
3189                         if (nid != ALC880_PIN_CD_NID)
3190                                 snd_hda_codec_write(codec, nid, 0,
3191                                                     AC_VERB_SET_AMP_GAIN_MUTE,
3192                                                     AMP_OUT_MUTE);
3193                 }
3194         }
3195 }
3196
3197 /* parse the BIOS configuration and set up the alc_spec */
3198 /* return 1 if successful, 0 if the proper config is not found,
3199  * or a negative error code
3200  */
3201 static int alc880_parse_auto_config(struct hda_codec *codec)