Merge branch 'fix/hda' into topic/hda
[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 <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/pci.h>
30 #include <sound/core.h>
31 #include <sound/jack.h>
32 #include "hda_codec.h"
33 #include "hda_local.h"
34 #include "hda_beep.h"
35
36 #define ALC880_FRONT_EVENT              0x01
37 #define ALC880_DCVOL_EVENT              0x02
38 #define ALC880_HP_EVENT                 0x04
39 #define ALC880_MIC_EVENT                0x08
40
41 /* ALC880 board config type */
42 enum {
43         ALC880_3ST,
44         ALC880_3ST_DIG,
45         ALC880_5ST,
46         ALC880_5ST_DIG,
47         ALC880_W810,
48         ALC880_Z71V,
49         ALC880_6ST,
50         ALC880_6ST_DIG,
51         ALC880_F1734,
52         ALC880_ASUS,
53         ALC880_ASUS_DIG,
54         ALC880_ASUS_W1V,
55         ALC880_ASUS_DIG2,
56         ALC880_FUJITSU,
57         ALC880_UNIWILL_DIG,
58         ALC880_UNIWILL,
59         ALC880_UNIWILL_P53,
60         ALC880_CLEVO,
61         ALC880_TCL_S700,
62         ALC880_LG,
63         ALC880_LG_LW,
64         ALC880_MEDION_RIM,
65 #ifdef CONFIG_SND_DEBUG
66         ALC880_TEST,
67 #endif
68         ALC880_AUTO,
69         ALC880_MODEL_LAST /* last tag */
70 };
71
72 /* ALC260 models */
73 enum {
74         ALC260_BASIC,
75         ALC260_HP,
76         ALC260_HP_DC7600,
77         ALC260_HP_3013,
78         ALC260_FUJITSU_S702X,
79         ALC260_ACER,
80         ALC260_WILL,
81         ALC260_REPLACER_672V,
82         ALC260_FAVORIT100,
83 #ifdef CONFIG_SND_DEBUG
84         ALC260_TEST,
85 #endif
86         ALC260_AUTO,
87         ALC260_MODEL_LAST /* last tag */
88 };
89
90 /* ALC262 models */
91 enum {
92         ALC262_BASIC,
93         ALC262_HIPPO,
94         ALC262_HIPPO_1,
95         ALC262_FUJITSU,
96         ALC262_HP_BPC,
97         ALC262_HP_BPC_D7000_WL,
98         ALC262_HP_BPC_D7000_WF,
99         ALC262_HP_TC_T5735,
100         ALC262_HP_RP5700,
101         ALC262_BENQ_ED8,
102         ALC262_SONY_ASSAMD,
103         ALC262_BENQ_T31,
104         ALC262_ULTRA,
105         ALC262_LENOVO_3000,
106         ALC262_NEC,
107         ALC262_TOSHIBA_S06,
108         ALC262_TOSHIBA_RX1,
109         ALC262_TYAN,
110         ALC262_AUTO,
111         ALC262_MODEL_LAST /* last tag */
112 };
113
114 /* ALC268 models */
115 enum {
116         ALC267_QUANTA_IL1,
117         ALC268_3ST,
118         ALC268_TOSHIBA,
119         ALC268_ACER,
120         ALC268_ACER_DMIC,
121         ALC268_ACER_ASPIRE_ONE,
122         ALC268_DELL,
123         ALC268_ZEPTO,
124 #ifdef CONFIG_SND_DEBUG
125         ALC268_TEST,
126 #endif
127         ALC268_AUTO,
128         ALC268_MODEL_LAST /* last tag */
129 };
130
131 /* ALC269 models */
132 enum {
133         ALC269_BASIC,
134         ALC269_QUANTA_FL1,
135         ALC269_AMIC,
136         ALC269_DMIC,
137         ALC269VB_AMIC,
138         ALC269VB_DMIC,
139         ALC269_FUJITSU,
140         ALC269_LIFEBOOK,
141         ALC271_ACER,
142         ALC269_AUTO,
143         ALC269_MODEL_LAST /* last tag */
144 };
145
146 /* ALC861 models */
147 enum {
148         ALC861_3ST,
149         ALC660_3ST,
150         ALC861_3ST_DIG,
151         ALC861_6ST_DIG,
152         ALC861_UNIWILL_M31,
153         ALC861_TOSHIBA,
154         ALC861_ASUS,
155         ALC861_ASUS_LAPTOP,
156         ALC861_AUTO,
157         ALC861_MODEL_LAST,
158 };
159
160 /* ALC861-VD models */
161 enum {
162         ALC660VD_3ST,
163         ALC660VD_3ST_DIG,
164         ALC660VD_ASUS_V1S,
165         ALC861VD_3ST,
166         ALC861VD_3ST_DIG,
167         ALC861VD_6ST_DIG,
168         ALC861VD_LENOVO,
169         ALC861VD_DALLAS,
170         ALC861VD_HP,
171         ALC861VD_AUTO,
172         ALC861VD_MODEL_LAST,
173 };
174
175 /* ALC662 models */
176 enum {
177         ALC662_3ST_2ch_DIG,
178         ALC662_3ST_6ch_DIG,
179         ALC662_3ST_6ch,
180         ALC662_5ST_DIG,
181         ALC662_LENOVO_101E,
182         ALC662_ASUS_EEEPC_P701,
183         ALC662_ASUS_EEEPC_EP20,
184         ALC663_ASUS_M51VA,
185         ALC663_ASUS_G71V,
186         ALC663_ASUS_H13,
187         ALC663_ASUS_G50V,
188         ALC662_ECS,
189         ALC663_ASUS_MODE1,
190         ALC662_ASUS_MODE2,
191         ALC663_ASUS_MODE3,
192         ALC663_ASUS_MODE4,
193         ALC663_ASUS_MODE5,
194         ALC663_ASUS_MODE6,
195         ALC663_ASUS_MODE7,
196         ALC663_ASUS_MODE8,
197         ALC272_DELL,
198         ALC272_DELL_ZM1,
199         ALC272_SAMSUNG_NC10,
200         ALC662_AUTO,
201         ALC662_MODEL_LAST,
202 };
203
204 /* ALC882 models */
205 enum {
206         ALC882_3ST_DIG,
207         ALC882_6ST_DIG,
208         ALC882_ARIMA,
209         ALC882_W2JC,
210         ALC882_TARGA,
211         ALC882_ASUS_A7J,
212         ALC882_ASUS_A7M,
213         ALC885_MACPRO,
214         ALC885_MBA21,
215         ALC885_MBP3,
216         ALC885_MB5,
217         ALC885_MACMINI3,
218         ALC885_IMAC24,
219         ALC885_IMAC91,
220         ALC883_3ST_2ch_DIG,
221         ALC883_3ST_6ch_DIG,
222         ALC883_3ST_6ch,
223         ALC883_6ST_DIG,
224         ALC883_TARGA_DIG,
225         ALC883_TARGA_2ch_DIG,
226         ALC883_TARGA_8ch_DIG,
227         ALC883_ACER,
228         ALC883_ACER_ASPIRE,
229         ALC888_ACER_ASPIRE_4930G,
230         ALC888_ACER_ASPIRE_6530G,
231         ALC888_ACER_ASPIRE_8930G,
232         ALC888_ACER_ASPIRE_7730G,
233         ALC883_MEDION,
234         ALC883_MEDION_WIM2160,
235         ALC883_LAPTOP_EAPD,
236         ALC883_LENOVO_101E_2ch,
237         ALC883_LENOVO_NB0763,
238         ALC888_LENOVO_MS7195_DIG,
239         ALC888_LENOVO_SKY,
240         ALC883_HAIER_W66,
241         ALC888_3ST_HP,
242         ALC888_6ST_DELL,
243         ALC883_MITAC,
244         ALC883_CLEVO_M540R,
245         ALC883_CLEVO_M720,
246         ALC883_FUJITSU_PI2515,
247         ALC888_FUJITSU_XA3530,
248         ALC883_3ST_6ch_INTEL,
249         ALC889A_INTEL,
250         ALC889_INTEL,
251         ALC888_ASUS_M90V,
252         ALC888_ASUS_EEE1601,
253         ALC889A_MB31,
254         ALC1200_ASUS_P5Q,
255         ALC883_SONY_VAIO_TT,
256         ALC882_AUTO,
257         ALC882_MODEL_LAST,
258 };
259
260 /* ALC680 models */
261 enum {
262         ALC680_BASE,
263         ALC680_AUTO,
264         ALC680_MODEL_LAST,
265 };
266
267 /* for GPIO Poll */
268 #define GPIO_MASK       0x03
269
270 /* extra amp-initialization sequence types */
271 enum {
272         ALC_INIT_NONE,
273         ALC_INIT_DEFAULT,
274         ALC_INIT_GPIO1,
275         ALC_INIT_GPIO2,
276         ALC_INIT_GPIO3,
277 };
278
279 struct alc_mic_route {
280         hda_nid_t pin;
281         unsigned char mux_idx;
282         unsigned char amix_idx;
283 };
284
285 #define MUX_IDX_UNDEF   ((unsigned char)-1)
286
287 struct alc_customize_define {
288         unsigned int  sku_cfg;
289         unsigned char port_connectivity;
290         unsigned char check_sum;
291         unsigned char customization;
292         unsigned char external_amp;
293         unsigned int  enable_pcbeep:1;
294         unsigned int  platform_type:1;
295         unsigned int  swap:1;
296         unsigned int  override:1;
297         unsigned int  fixup:1; /* Means that this sku is set by driver, not read from hw */
298 };
299
300 struct alc_fixup;
301
302 struct alc_multi_io {
303         hda_nid_t pin;          /* multi-io widget pin NID */
304         hda_nid_t dac;          /* DAC to be connected */
305         unsigned int ctl_in;    /* cached input-pin control value */
306 };
307
308 enum {
309         ALC_AUTOMUTE_PIN,       /* change the pin control */
310         ALC_AUTOMUTE_AMP,       /* mute/unmute the pin AMP */
311         ALC_AUTOMUTE_MIXER,     /* mute/unmute mixer widget AMP */
312 };
313
314 struct alc_spec {
315         /* codec parameterization */
316         struct snd_kcontrol_new *mixers[5];     /* mixer arrays */
317         unsigned int num_mixers;
318         struct snd_kcontrol_new *cap_mixer;     /* capture mixer */
319         unsigned int beep_amp;  /* beep amp value, set via set_beep_amp() */
320
321         const struct hda_verb *init_verbs[10];  /* initialization verbs
322                                                  * don't forget NULL
323                                                  * termination!
324                                                  */
325         unsigned int num_init_verbs;
326
327         char stream_name_analog[32];    /* analog PCM stream */
328         struct hda_pcm_stream *stream_analog_playback;
329         struct hda_pcm_stream *stream_analog_capture;
330         struct hda_pcm_stream *stream_analog_alt_playback;
331         struct hda_pcm_stream *stream_analog_alt_capture;
332
333         char stream_name_digital[32];   /* digital PCM stream */
334         struct hda_pcm_stream *stream_digital_playback;
335         struct hda_pcm_stream *stream_digital_capture;
336
337         /* playback */
338         struct hda_multi_out multiout;  /* playback set-up
339                                          * max_channels, dacs must be set
340                                          * dig_out_nid and hp_nid are optional
341                                          */
342         hda_nid_t alt_dac_nid;
343         hda_nid_t slave_dig_outs[3];    /* optional - for auto-parsing */
344         int dig_out_type;
345
346         /* capture */
347         unsigned int num_adc_nids;
348         hda_nid_t *adc_nids;
349         hda_nid_t *capsrc_nids;
350         hda_nid_t dig_in_nid;           /* digital-in NID; optional */
351
352         /* capture setup for dynamic dual-adc switch */
353         unsigned int cur_adc_idx;
354         hda_nid_t cur_adc;
355         unsigned int cur_adc_stream_tag;
356         unsigned int cur_adc_format;
357
358         /* capture source */
359         unsigned int num_mux_defs;
360         const struct hda_input_mux *input_mux;
361         unsigned int cur_mux[3];
362         struct alc_mic_route ext_mic;
363         struct alc_mic_route int_mic;
364
365         /* channel model */
366         const struct hda_channel_mode *channel_mode;
367         int num_channel_mode;
368         int need_dac_fix;
369         int const_channel_count;
370         int ext_channel_count;
371
372         /* PCM information */
373         struct hda_pcm pcm_rec[3];      /* used in alc_build_pcms() */
374
375         /* dynamic controls, init_verbs and input_mux */
376         struct auto_pin_cfg autocfg;
377         struct alc_customize_define cdefine;
378         struct snd_array kctls;
379         struct hda_input_mux private_imux[3];
380         hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
381         hda_nid_t private_adc_nids[AUTO_CFG_MAX_OUTS];
382         hda_nid_t private_capsrc_nids[AUTO_CFG_MAX_OUTS];
383
384         /* hooks */
385         void (*init_hook)(struct hda_codec *codec);
386         void (*unsol_event)(struct hda_codec *codec, unsigned int res);
387 #ifdef CONFIG_SND_HDA_POWER_SAVE
388         void (*power_hook)(struct hda_codec *codec);
389 #endif
390         void (*shutup)(struct hda_codec *codec);
391
392         /* for pin sensing */
393         unsigned int jack_present: 1;
394         unsigned int line_jack_present:1;
395         unsigned int master_mute:1;
396         unsigned int auto_mic:1;
397         unsigned int automute:1;        /* HP automute enabled */
398         unsigned int detect_line:1;     /* Line-out detection enabled */
399         unsigned int automute_lines:1;  /* automute line-out as well */
400         unsigned int automute_hp_lo:1;  /* both HP and LO available */
401
402         /* other flags */
403         unsigned int no_analog :1; /* digital I/O only */
404         unsigned int dual_adc_switch:1; /* switch ADCs (for ALC275) */
405         unsigned int single_input_src:1;
406
407         /* auto-mute control */
408         int automute_mode;
409         hda_nid_t automute_mixer_nid[AUTO_CFG_MAX_OUTS];
410
411         int init_amp;
412         int codec_variant;      /* flag for other variants */
413
414         /* for virtual master */
415         hda_nid_t vmaster_nid;
416 #ifdef CONFIG_SND_HDA_POWER_SAVE
417         struct hda_loopback_check loopback;
418 #endif
419
420         /* for PLL fix */
421         hda_nid_t pll_nid;
422         unsigned int pll_coef_idx, pll_coef_bit;
423
424         /* fix-up list */
425         int fixup_id;
426         const struct alc_fixup *fixup_list;
427         const char *fixup_name;
428
429         /* multi-io */
430         int multi_ios;
431         struct alc_multi_io multi_io[4];
432 };
433
434 /*
435  * configuration template - to be copied to the spec instance
436  */
437 struct alc_config_preset {
438         struct snd_kcontrol_new *mixers[5]; /* should be identical size
439                                              * with spec
440                                              */
441         struct snd_kcontrol_new *cap_mixer; /* capture mixer */
442         const struct hda_verb *init_verbs[5];
443         unsigned int num_dacs;
444         hda_nid_t *dac_nids;
445         hda_nid_t dig_out_nid;          /* optional */
446         hda_nid_t hp_nid;               /* optional */
447         hda_nid_t *slave_dig_outs;
448         unsigned int num_adc_nids;
449         hda_nid_t *adc_nids;
450         hda_nid_t *capsrc_nids;
451         hda_nid_t dig_in_nid;
452         unsigned int num_channel_mode;
453         const struct hda_channel_mode *channel_mode;
454         int need_dac_fix;
455         int const_channel_count;
456         unsigned int num_mux_defs;
457         const struct hda_input_mux *input_mux;
458         void (*unsol_event)(struct hda_codec *, unsigned int);
459         void (*setup)(struct hda_codec *);
460         void (*init_hook)(struct hda_codec *);
461 #ifdef CONFIG_SND_HDA_POWER_SAVE
462         struct hda_amp_list *loopbacks;
463         void (*power_hook)(struct hda_codec *codec);
464 #endif
465 };
466
467
468 /*
469  * input MUX handling
470  */
471 static int alc_mux_enum_info(struct snd_kcontrol *kcontrol,
472                              struct snd_ctl_elem_info *uinfo)
473 {
474         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
475         struct alc_spec *spec = codec->spec;
476         unsigned int mux_idx = snd_ctl_get_ioffidx(kcontrol, &uinfo->id);
477         if (mux_idx >= spec->num_mux_defs)
478                 mux_idx = 0;
479         if (!spec->input_mux[mux_idx].num_items && mux_idx > 0)
480                 mux_idx = 0;
481         return snd_hda_input_mux_info(&spec->input_mux[mux_idx], uinfo);
482 }
483
484 static int alc_mux_enum_get(struct snd_kcontrol *kcontrol,
485                             struct snd_ctl_elem_value *ucontrol)
486 {
487         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
488         struct alc_spec *spec = codec->spec;
489         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
490
491         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
492         return 0;
493 }
494
495 static int alc_mux_enum_put(struct snd_kcontrol *kcontrol,
496                             struct snd_ctl_elem_value *ucontrol)
497 {
498         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
499         struct alc_spec *spec = codec->spec;
500         const struct hda_input_mux *imux;
501         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
502         unsigned int mux_idx;
503         hda_nid_t nid = spec->capsrc_nids ?
504                 spec->capsrc_nids[adc_idx] : spec->adc_nids[adc_idx];
505         unsigned int type;
506
507         mux_idx = adc_idx >= spec->num_mux_defs ? 0 : adc_idx;
508         imux = &spec->input_mux[mux_idx];
509         if (!imux->num_items && mux_idx > 0)
510                 imux = &spec->input_mux[0];
511
512         type = get_wcaps_type(get_wcaps(codec, nid));
513         if (type == AC_WID_AUD_MIX) {
514                 /* Matrix-mixer style (e.g. ALC882) */
515                 unsigned int *cur_val = &spec->cur_mux[adc_idx];
516                 unsigned int i, idx;
517
518                 idx = ucontrol->value.enumerated.item[0];
519                 if (idx >= imux->num_items)
520                         idx = imux->num_items - 1;
521                 if (*cur_val == idx)
522                         return 0;
523                 for (i = 0; i < imux->num_items; i++) {
524                         unsigned int v = (i == idx) ? 0 : HDA_AMP_MUTE;
525                         snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT,
526                                                  imux->items[i].index,
527                                                  HDA_AMP_MUTE, v);
528                 }
529                 *cur_val = idx;
530                 return 1;
531         } else {
532                 /* MUX style (e.g. ALC880) */
533                 return snd_hda_input_mux_put(codec, imux, ucontrol, nid,
534                                              &spec->cur_mux[adc_idx]);
535         }
536 }
537
538 /*
539  * channel mode setting
540  */
541 static int alc_ch_mode_info(struct snd_kcontrol *kcontrol,
542                             struct snd_ctl_elem_info *uinfo)
543 {
544         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
545         struct alc_spec *spec = codec->spec;
546         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
547                                     spec->num_channel_mode);
548 }
549
550 static int alc_ch_mode_get(struct snd_kcontrol *kcontrol,
551                            struct snd_ctl_elem_value *ucontrol)
552 {
553         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
554         struct alc_spec *spec = codec->spec;
555         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
556                                    spec->num_channel_mode,
557                                    spec->ext_channel_count);
558 }
559
560 static int alc_ch_mode_put(struct snd_kcontrol *kcontrol,
561                            struct snd_ctl_elem_value *ucontrol)
562 {
563         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
564         struct alc_spec *spec = codec->spec;
565         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
566                                       spec->num_channel_mode,
567                                       &spec->ext_channel_count);
568         if (err >= 0 && !spec->const_channel_count) {
569                 spec->multiout.max_channels = spec->ext_channel_count;
570                 if (spec->need_dac_fix)
571                         spec->multiout.num_dacs = spec->multiout.max_channels / 2;
572         }
573         return err;
574 }
575
576 /*
577  * Control the mode of pin widget settings via the mixer.  "pc" is used
578  * instead of "%" to avoid consequences of accidentally treating the % as
579  * being part of a format specifier.  Maximum allowed length of a value is
580  * 63 characters plus NULL terminator.
581  *
582  * Note: some retasking pin complexes seem to ignore requests for input
583  * states other than HiZ (eg: PIN_VREFxx) and revert to HiZ if any of these
584  * are requested.  Therefore order this list so that this behaviour will not
585  * cause problems when mixer clients move through the enum sequentially.
586  * NIDs 0x0f and 0x10 have been observed to have this behaviour as of
587  * March 2006.
588  */
589 static char *alc_pin_mode_names[] = {
590         "Mic 50pc bias", "Mic 80pc bias",
591         "Line in", "Line out", "Headphone out",
592 };
593 static unsigned char alc_pin_mode_values[] = {
594         PIN_VREF50, PIN_VREF80, PIN_IN, PIN_OUT, PIN_HP,
595 };
596 /* The control can present all 5 options, or it can limit the options based
597  * in the pin being assumed to be exclusively an input or an output pin.  In
598  * addition, "input" pins may or may not process the mic bias option
599  * depending on actual widget capability (NIDs 0x0f and 0x10 don't seem to
600  * accept requests for bias as of chip versions up to March 2006) and/or
601  * wiring in the computer.
602  */
603 #define ALC_PIN_DIR_IN              0x00
604 #define ALC_PIN_DIR_OUT             0x01
605 #define ALC_PIN_DIR_INOUT           0x02
606 #define ALC_PIN_DIR_IN_NOMICBIAS    0x03
607 #define ALC_PIN_DIR_INOUT_NOMICBIAS 0x04
608
609 /* Info about the pin modes supported by the different pin direction modes.
610  * For each direction the minimum and maximum values are given.
611  */
612 static signed char alc_pin_mode_dir_info[5][2] = {
613         { 0, 2 },    /* ALC_PIN_DIR_IN */
614         { 3, 4 },    /* ALC_PIN_DIR_OUT */
615         { 0, 4 },    /* ALC_PIN_DIR_INOUT */
616         { 2, 2 },    /* ALC_PIN_DIR_IN_NOMICBIAS */
617         { 2, 4 },    /* ALC_PIN_DIR_INOUT_NOMICBIAS */
618 };
619 #define alc_pin_mode_min(_dir) (alc_pin_mode_dir_info[_dir][0])
620 #define alc_pin_mode_max(_dir) (alc_pin_mode_dir_info[_dir][1])
621 #define alc_pin_mode_n_items(_dir) \
622         (alc_pin_mode_max(_dir)-alc_pin_mode_min(_dir)+1)
623
624 static int alc_pin_mode_info(struct snd_kcontrol *kcontrol,
625                              struct snd_ctl_elem_info *uinfo)
626 {
627         unsigned int item_num = uinfo->value.enumerated.item;
628         unsigned char dir = (kcontrol->private_value >> 16) & 0xff;
629
630         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
631         uinfo->count = 1;
632         uinfo->value.enumerated.items = alc_pin_mode_n_items(dir);
633
634         if (item_num<alc_pin_mode_min(dir) || item_num>alc_pin_mode_max(dir))
635                 item_num = alc_pin_mode_min(dir);
636         strcpy(uinfo->value.enumerated.name, alc_pin_mode_names[item_num]);
637         return 0;
638 }
639
640 static int alc_pin_mode_get(struct snd_kcontrol *kcontrol,
641                             struct snd_ctl_elem_value *ucontrol)
642 {
643         unsigned int i;
644         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
645         hda_nid_t nid = kcontrol->private_value & 0xffff;
646         unsigned char dir = (kcontrol->private_value >> 16) & 0xff;
647         long *valp = ucontrol->value.integer.value;
648         unsigned int pinctl = snd_hda_codec_read(codec, nid, 0,
649                                                  AC_VERB_GET_PIN_WIDGET_CONTROL,
650                                                  0x00);
651
652         /* Find enumerated value for current pinctl setting */
653         i = alc_pin_mode_min(dir);
654         while (i <= alc_pin_mode_max(dir) && alc_pin_mode_values[i] != pinctl)
655                 i++;
656         *valp = i <= alc_pin_mode_max(dir) ? i: alc_pin_mode_min(dir);
657         return 0;
658 }
659
660 static int alc_pin_mode_put(struct snd_kcontrol *kcontrol,
661                             struct snd_ctl_elem_value *ucontrol)
662 {
663         signed int change;
664         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
665         hda_nid_t nid = kcontrol->private_value & 0xffff;
666         unsigned char dir = (kcontrol->private_value >> 16) & 0xff;
667         long val = *ucontrol->value.integer.value;
668         unsigned int pinctl = snd_hda_codec_read(codec, nid, 0,
669                                                  AC_VERB_GET_PIN_WIDGET_CONTROL,
670                                                  0x00);
671
672         if (val < alc_pin_mode_min(dir) || val > alc_pin_mode_max(dir))
673                 val = alc_pin_mode_min(dir);
674
675         change = pinctl != alc_pin_mode_values[val];
676         if (change) {
677                 /* Set pin mode to that requested */
678                 snd_hda_codec_write_cache(codec, nid, 0,
679                                           AC_VERB_SET_PIN_WIDGET_CONTROL,
680                                           alc_pin_mode_values[val]);
681
682                 /* Also enable the retasking pin's input/output as required
683                  * for the requested pin mode.  Enum values of 2 or less are
684                  * input modes.
685                  *
686                  * Dynamically switching the input/output buffers probably
687                  * reduces noise slightly (particularly on input) so we'll
688                  * do it.  However, having both input and output buffers
689                  * enabled simultaneously doesn't seem to be problematic if
690                  * this turns out to be necessary in the future.
691                  */
692                 if (val <= 2) {
693                         snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
694                                                  HDA_AMP_MUTE, HDA_AMP_MUTE);
695                         snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 0,
696                                                  HDA_AMP_MUTE, 0);
697                 } else {
698                         snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 0,
699                                                  HDA_AMP_MUTE, HDA_AMP_MUTE);
700                         snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
701                                                  HDA_AMP_MUTE, 0);
702                 }
703         }
704         return change;
705 }
706
707 #define ALC_PIN_MODE(xname, nid, dir) \
708         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
709           .subdevice = HDA_SUBDEV_NID_FLAG | nid, \
710           .info = alc_pin_mode_info, \
711           .get = alc_pin_mode_get, \
712           .put = alc_pin_mode_put, \
713           .private_value = nid | (dir<<16) }
714
715 /* A switch control for ALC260 GPIO pins.  Multiple GPIOs can be ganged
716  * together using a mask with more than one bit set.  This control is
717  * currently used only by the ALC260 test model.  At this stage they are not
718  * needed for any "production" models.
719  */
720 #ifdef CONFIG_SND_DEBUG
721 #define alc_gpio_data_info      snd_ctl_boolean_mono_info
722
723 static int alc_gpio_data_get(struct snd_kcontrol *kcontrol,
724                              struct snd_ctl_elem_value *ucontrol)
725 {
726         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
727         hda_nid_t nid = kcontrol->private_value & 0xffff;
728         unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
729         long *valp = ucontrol->value.integer.value;
730         unsigned int val = snd_hda_codec_read(codec, nid, 0,
731                                               AC_VERB_GET_GPIO_DATA, 0x00);
732
733         *valp = (val & mask) != 0;
734         return 0;
735 }
736 static int alc_gpio_data_put(struct snd_kcontrol *kcontrol,
737                              struct snd_ctl_elem_value *ucontrol)
738 {
739         signed int change;
740         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
741         hda_nid_t nid = kcontrol->private_value & 0xffff;
742         unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
743         long val = *ucontrol->value.integer.value;
744         unsigned int gpio_data = snd_hda_codec_read(codec, nid, 0,
745                                                     AC_VERB_GET_GPIO_DATA,
746                                                     0x00);
747
748         /* Set/unset the masked GPIO bit(s) as needed */
749         change = (val == 0 ? 0 : mask) != (gpio_data & mask);
750         if (val == 0)
751                 gpio_data &= ~mask;
752         else
753                 gpio_data |= mask;
754         snd_hda_codec_write_cache(codec, nid, 0,
755                                   AC_VERB_SET_GPIO_DATA, gpio_data);
756
757         return change;
758 }
759 #define ALC_GPIO_DATA_SWITCH(xname, nid, mask) \
760         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
761           .subdevice = HDA_SUBDEV_NID_FLAG | nid, \
762           .info = alc_gpio_data_info, \
763           .get = alc_gpio_data_get, \
764           .put = alc_gpio_data_put, \
765           .private_value = nid | (mask<<16) }
766 #endif   /* CONFIG_SND_DEBUG */
767
768 /* A switch control to allow the enabling of the digital IO pins on the
769  * ALC260.  This is incredibly simplistic; the intention of this control is
770  * to provide something in the test model allowing digital outputs to be
771  * identified if present.  If models are found which can utilise these
772  * outputs a more complete mixer control can be devised for those models if
773  * necessary.
774  */
775 #ifdef CONFIG_SND_DEBUG
776 #define alc_spdif_ctrl_info     snd_ctl_boolean_mono_info
777
778 static int alc_spdif_ctrl_get(struct snd_kcontrol *kcontrol,
779                               struct snd_ctl_elem_value *ucontrol)
780 {
781         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
782         hda_nid_t nid = kcontrol->private_value & 0xffff;
783         unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
784         long *valp = ucontrol->value.integer.value;
785         unsigned int val = snd_hda_codec_read(codec, nid, 0,
786                                               AC_VERB_GET_DIGI_CONVERT_1, 0x00);
787
788         *valp = (val & mask) != 0;
789         return 0;
790 }
791 static int alc_spdif_ctrl_put(struct snd_kcontrol *kcontrol,
792                               struct snd_ctl_elem_value *ucontrol)
793 {
794         signed int change;
795         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
796         hda_nid_t nid = kcontrol->private_value & 0xffff;
797         unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
798         long val = *ucontrol->value.integer.value;
799         unsigned int ctrl_data = snd_hda_codec_read(codec, nid, 0,
800                                                     AC_VERB_GET_DIGI_CONVERT_1,
801                                                     0x00);
802
803         /* Set/unset the masked control bit(s) as needed */
804         change = (val == 0 ? 0 : mask) != (ctrl_data & mask);
805         if (val==0)
806                 ctrl_data &= ~mask;
807         else
808                 ctrl_data |= mask;
809         snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
810                                   ctrl_data);
811
812         return change;
813 }
814 #define ALC_SPDIF_CTRL_SWITCH(xname, nid, mask) \
815         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
816           .subdevice = HDA_SUBDEV_NID_FLAG | nid, \
817           .info = alc_spdif_ctrl_info, \
818           .get = alc_spdif_ctrl_get, \
819           .put = alc_spdif_ctrl_put, \
820           .private_value = nid | (mask<<16) }
821 #endif   /* CONFIG_SND_DEBUG */
822
823 /* A switch control to allow the enabling EAPD digital outputs on the ALC26x.
824  * Again, this is only used in the ALC26x test models to help identify when
825  * the EAPD line must be asserted for features to work.
826  */
827 #ifdef CONFIG_SND_DEBUG
828 #define alc_eapd_ctrl_info      snd_ctl_boolean_mono_info
829
830 static int alc_eapd_ctrl_get(struct snd_kcontrol *kcontrol,
831                               struct snd_ctl_elem_value *ucontrol)
832 {
833         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
834         hda_nid_t nid = kcontrol->private_value & 0xffff;
835         unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
836         long *valp = ucontrol->value.integer.value;
837         unsigned int val = snd_hda_codec_read(codec, nid, 0,
838                                               AC_VERB_GET_EAPD_BTLENABLE, 0x00);
839
840         *valp = (val & mask) != 0;
841         return 0;
842 }
843
844 static int alc_eapd_ctrl_put(struct snd_kcontrol *kcontrol,
845                               struct snd_ctl_elem_value *ucontrol)
846 {
847         int change;
848         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
849         hda_nid_t nid = kcontrol->private_value & 0xffff;
850         unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
851         long val = *ucontrol->value.integer.value;
852         unsigned int ctrl_data = snd_hda_codec_read(codec, nid, 0,
853                                                     AC_VERB_GET_EAPD_BTLENABLE,
854                                                     0x00);
855
856         /* Set/unset the masked control bit(s) as needed */
857         change = (!val ? 0 : mask) != (ctrl_data & mask);
858         if (!val)
859                 ctrl_data &= ~mask;
860         else
861                 ctrl_data |= mask;
862         snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_EAPD_BTLENABLE,
863                                   ctrl_data);
864
865         return change;
866 }
867
868 #define ALC_EAPD_CTRL_SWITCH(xname, nid, mask) \
869         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
870           .subdevice = HDA_SUBDEV_NID_FLAG | nid, \
871           .info = alc_eapd_ctrl_info, \
872           .get = alc_eapd_ctrl_get, \
873           .put = alc_eapd_ctrl_put, \
874           .private_value = nid | (mask<<16) }
875 #endif   /* CONFIG_SND_DEBUG */
876
877 /*
878  * set up the input pin config (depending on the given auto-pin type)
879  */
880 static void alc_set_input_pin(struct hda_codec *codec, hda_nid_t nid,
881                               int auto_pin_type)
882 {
883         unsigned int val = PIN_IN;
884
885         if (auto_pin_type == AUTO_PIN_MIC) {
886                 unsigned int pincap;
887                 unsigned int oldval;
888                 oldval = snd_hda_codec_read(codec, nid, 0,
889                                             AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
890                 pincap = snd_hda_query_pin_caps(codec, nid);
891                 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
892                 /* if the default pin setup is vref50, we give it priority */
893                 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
894                         val = PIN_VREF80;
895                 else if (pincap & AC_PINCAP_VREF_50)
896                         val = PIN_VREF50;
897                 else if (pincap & AC_PINCAP_VREF_100)
898                         val = PIN_VREF100;
899                 else if (pincap & AC_PINCAP_VREF_GRD)
900                         val = PIN_VREFGRD;
901         }
902         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, val);
903 }
904
905 static void alc_fixup_autocfg_pin_nums(struct hda_codec *codec)
906 {
907         struct alc_spec *spec = codec->spec;
908         struct auto_pin_cfg *cfg = &spec->autocfg;
909
910         if (!cfg->line_outs) {
911                 while (cfg->line_outs < AUTO_CFG_MAX_OUTS &&
912                        cfg->line_out_pins[cfg->line_outs])
913                         cfg->line_outs++;
914         }
915         if (!cfg->speaker_outs) {
916                 while (cfg->speaker_outs < AUTO_CFG_MAX_OUTS &&
917                        cfg->speaker_pins[cfg->speaker_outs])
918                         cfg->speaker_outs++;
919         }
920         if (!cfg->hp_outs) {
921                 while (cfg->hp_outs < AUTO_CFG_MAX_OUTS &&
922                        cfg->hp_pins[cfg->hp_outs])
923                         cfg->hp_outs++;
924         }
925 }
926
927 /*
928  */
929 static void add_mixer(struct alc_spec *spec, struct snd_kcontrol_new *mix)
930 {
931         if (snd_BUG_ON(spec->num_mixers >= ARRAY_SIZE(spec->mixers)))
932                 return;
933         spec->mixers[spec->num_mixers++] = mix;
934 }
935
936 static void add_verb(struct alc_spec *spec, const struct hda_verb *verb)
937 {
938         if (snd_BUG_ON(spec->num_init_verbs >= ARRAY_SIZE(spec->init_verbs)))
939                 return;
940         spec->init_verbs[spec->num_init_verbs++] = verb;
941 }
942
943 /*
944  * set up from the preset table
945  */
946 static void setup_preset(struct hda_codec *codec,
947                          const struct alc_config_preset *preset)
948 {
949         struct alc_spec *spec = codec->spec;
950         int i;
951
952         for (i = 0; i < ARRAY_SIZE(preset->mixers) && preset->mixers[i]; i++)
953                 add_mixer(spec, preset->mixers[i]);
954         spec->cap_mixer = preset->cap_mixer;
955         for (i = 0; i < ARRAY_SIZE(preset->init_verbs) && preset->init_verbs[i];
956              i++)
957                 add_verb(spec, preset->init_verbs[i]);
958
959         spec->channel_mode = preset->channel_mode;
960         spec->num_channel_mode = preset->num_channel_mode;
961         spec->need_dac_fix = preset->need_dac_fix;
962         spec->const_channel_count = preset->const_channel_count;
963
964         if (preset->const_channel_count)
965                 spec->multiout.max_channels = preset->const_channel_count;
966         else
967                 spec->multiout.max_channels = spec->channel_mode[0].channels;
968         spec->ext_channel_count = spec->channel_mode[0].channels;
969
970         spec->multiout.num_dacs = preset->num_dacs;
971         spec->multiout.dac_nids = preset->dac_nids;
972         spec->multiout.dig_out_nid = preset->dig_out_nid;
973         spec->multiout.slave_dig_outs = preset->slave_dig_outs;
974         spec->multiout.hp_nid = preset->hp_nid;
975
976         spec->num_mux_defs = preset->num_mux_defs;
977         if (!spec->num_mux_defs)
978                 spec->num_mux_defs = 1;
979         spec->input_mux = preset->input_mux;
980
981         spec->num_adc_nids = preset->num_adc_nids;
982         spec->adc_nids = preset->adc_nids;
983         spec->capsrc_nids = preset->capsrc_nids;
984         spec->dig_in_nid = preset->dig_in_nid;
985
986         spec->unsol_event = preset->unsol_event;
987         spec->init_hook = preset->init_hook;
988 #ifdef CONFIG_SND_HDA_POWER_SAVE
989         spec->power_hook = preset->power_hook;
990         spec->loopback.amplist = preset->loopbacks;
991 #endif
992
993         if (preset->setup)
994                 preset->setup(codec);
995
996         alc_fixup_autocfg_pin_nums(codec);
997 }
998
999 /* Enable GPIO mask and set output */
1000 static struct hda_verb alc_gpio1_init_verbs[] = {
1001         {0x01, AC_VERB_SET_GPIO_MASK, 0x01},
1002         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
1003         {0x01, AC_VERB_SET_GPIO_DATA, 0x01},
1004         { }
1005 };
1006
1007 static struct hda_verb alc_gpio2_init_verbs[] = {
1008         {0x01, AC_VERB_SET_GPIO_MASK, 0x02},
1009         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
1010         {0x01, AC_VERB_SET_GPIO_DATA, 0x02},
1011         { }
1012 };
1013
1014 static struct hda_verb alc_gpio3_init_verbs[] = {
1015         {0x01, AC_VERB_SET_GPIO_MASK, 0x03},
1016         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x03},
1017         {0x01, AC_VERB_SET_GPIO_DATA, 0x03},
1018         { }
1019 };
1020
1021 /*
1022  * Fix hardware PLL issue
1023  * On some codecs, the analog PLL gating control must be off while
1024  * the default value is 1.
1025  */
1026 static void alc_fix_pll(struct hda_codec *codec)
1027 {
1028         struct alc_spec *spec = codec->spec;
1029         unsigned int val;
1030
1031         if (!spec->pll_nid)
1032                 return;
1033         snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
1034                             spec->pll_coef_idx);
1035         val = snd_hda_codec_read(codec, spec->pll_nid, 0,
1036                                  AC_VERB_GET_PROC_COEF, 0);
1037         snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
1038                             spec->pll_coef_idx);
1039         snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_PROC_COEF,
1040                             val & ~(1 << spec->pll_coef_bit));
1041 }
1042
1043 static void alc_fix_pll_init(struct hda_codec *codec, hda_nid_t nid,
1044                              unsigned int coef_idx, unsigned int coef_bit)
1045 {
1046         struct alc_spec *spec = codec->spec;
1047         spec->pll_nid = nid;
1048         spec->pll_coef_idx = coef_idx;
1049         spec->pll_coef_bit = coef_bit;
1050         alc_fix_pll(codec);
1051 }
1052
1053 static int alc_init_jacks(struct hda_codec *codec)
1054 {
1055 #ifdef CONFIG_SND_HDA_INPUT_JACK
1056         struct alc_spec *spec = codec->spec;
1057         int err;
1058         unsigned int hp_nid = spec->autocfg.hp_pins[0];
1059         unsigned int mic_nid = spec->ext_mic.pin;
1060
1061         if (hp_nid) {
1062                 err = snd_hda_input_jack_add(codec, hp_nid,
1063                                              SND_JACK_HEADPHONE, NULL);
1064                 if (err < 0)
1065                         return err;
1066                 snd_hda_input_jack_report(codec, hp_nid);
1067         }
1068
1069         if (mic_nid) {
1070                 err = snd_hda_input_jack_add(codec, mic_nid,
1071                                              SND_JACK_MICROPHONE, NULL);
1072                 if (err < 0)
1073                         return err;
1074                 snd_hda_input_jack_report(codec, mic_nid);
1075         }
1076 #endif /* CONFIG_SND_HDA_INPUT_JACK */
1077         return 0;
1078 }
1079
1080 static int detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
1081 {
1082         int i, present = 0;
1083
1084         for (i = 0; i < num_pins; i++) {
1085                 hda_nid_t nid = pins[i];
1086                 if (!nid)
1087                         break;
1088                 snd_hda_input_jack_report(codec, nid);
1089                 present |= snd_hda_jack_detect(codec, nid);
1090         }
1091         return present;
1092 }
1093
1094 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
1095                         bool mute, bool hp_out)
1096 {
1097         struct alc_spec *spec = codec->spec;
1098         unsigned int mute_bits = mute ? HDA_AMP_MUTE : 0;
1099         unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
1100         int i;
1101
1102         for (i = 0; i < num_pins; i++) {
1103                 hda_nid_t nid = pins[i];
1104                 if (!nid)
1105                         break;
1106                 switch (spec->automute_mode) {
1107                 case ALC_AUTOMUTE_PIN:
1108                         snd_hda_codec_write(codec, nid, 0,
1109                                             AC_VERB_SET_PIN_WIDGET_CONTROL,
1110                                             pin_bits);
1111                         break;
1112                 case ALC_AUTOMUTE_AMP:
1113                         snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1114                                                  HDA_AMP_MUTE, mute_bits);
1115                         break;
1116                 case ALC_AUTOMUTE_MIXER:
1117                         nid = spec->automute_mixer_nid[i];
1118                         if (!nid)
1119                                 break;
1120                         snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 0,
1121                                                  HDA_AMP_MUTE, mute_bits);
1122                         snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 1,
1123                                                  HDA_AMP_MUTE, mute_bits);
1124                         break;
1125                 }
1126         }
1127 }
1128
1129 /* Toggle internal speakers muting */
1130 static void update_speakers(struct hda_codec *codec)
1131 {
1132         struct alc_spec *spec = codec->spec;
1133         int on;
1134
1135         if (!spec->automute)
1136                 on = 0;
1137         else
1138                 on = spec->jack_present | spec->line_jack_present;
1139         on |= spec->master_mute;
1140         do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
1141                     spec->autocfg.speaker_pins, on, false);
1142
1143         /* toggle line-out mutes if needed, too */
1144         /* if LO is a copy of either HP or Speaker, don't need to handle it */
1145         if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
1146             spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
1147                 return;
1148         if (!spec->automute_lines || !spec->automute)
1149                 on = 0;
1150         else
1151                 on = spec->jack_present;
1152         on |= spec->master_mute;
1153         do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
1154                     spec->autocfg.line_out_pins, on, false);
1155 }
1156
1157 static void alc_hp_automute(struct hda_codec *codec)
1158 {
1159         struct alc_spec *spec = codec->spec;
1160
1161         if (!spec->automute)
1162                 return;
1163         spec->jack_present =
1164                 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
1165                              spec->autocfg.hp_pins);
1166         update_speakers(codec);
1167 }
1168
1169 static void alc_line_automute(struct hda_codec *codec)
1170 {
1171         struct alc_spec *spec = codec->spec;
1172
1173         if (!spec->automute || !spec->detect_line)
1174                 return;
1175         spec->line_jack_present =
1176                 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
1177                              spec->autocfg.line_out_pins);
1178         update_speakers(codec);
1179 }
1180
1181 static int get_connection_index(struct hda_codec *codec, hda_nid_t mux,
1182                                 hda_nid_t nid)
1183 {
1184         hda_nid_t conn[HDA_MAX_NUM_INPUTS];
1185         int i, nums;
1186
1187         nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
1188         for (i = 0; i < nums; i++)
1189                 if (conn[i] == nid)
1190                         return i;
1191         return -1;
1192 }
1193
1194 /* switch the current ADC according to the jack state */
1195 static void alc_dual_mic_adc_auto_switch(struct hda_codec *codec)
1196 {
1197         struct alc_spec *spec = codec->spec;
1198         unsigned int present;
1199         hda_nid_t new_adc;
1200
1201         present = snd_hda_jack_detect(codec, spec->ext_mic.pin);
1202         if (present)
1203                 spec->cur_adc_idx = 1;
1204         else
1205                 spec->cur_adc_idx = 0;
1206         new_adc = spec->adc_nids[spec->cur_adc_idx];
1207         if (spec->cur_adc && spec->cur_adc != new_adc) {
1208                 /* stream is running, let's swap the current ADC */
1209                 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1210                 spec->cur_adc = new_adc;
1211                 snd_hda_codec_setup_stream(codec, new_adc,
1212                                            spec->cur_adc_stream_tag, 0,
1213                                            spec->cur_adc_format);
1214         }
1215 }
1216
1217 static void alc_mic_automute(struct hda_codec *codec)
1218 {
1219         struct alc_spec *spec = codec->spec;
1220         struct alc_mic_route *dead, *alive;
1221         unsigned int present, type;
1222         hda_nid_t cap_nid;
1223
1224         if (!spec->auto_mic)
1225                 return;
1226         if (!spec->int_mic.pin || !spec->ext_mic.pin)
1227                 return;
1228         if (snd_BUG_ON(!spec->adc_nids))
1229                 return;
1230
1231         if (spec->dual_adc_switch) {
1232                 alc_dual_mic_adc_auto_switch(codec);
1233                 return;
1234         }
1235
1236         cap_nid = spec->capsrc_nids ? spec->capsrc_nids[0] : spec->adc_nids[0];
1237
1238         present = snd_hda_jack_detect(codec, spec->ext_mic.pin);
1239         if (present) {
1240                 alive = &spec->ext_mic;
1241                 dead = &spec->int_mic;
1242         } else {
1243                 alive = &spec->int_mic;
1244                 dead = &spec->ext_mic;
1245         }
1246
1247         type = get_wcaps_type(get_wcaps(codec, cap_nid));
1248         if (type == AC_WID_AUD_MIX) {
1249                 /* Matrix-mixer style (e.g. ALC882) */
1250                 snd_hda_codec_amp_stereo(codec, cap_nid, HDA_INPUT,
1251                                          alive->mux_idx,
1252                                          HDA_AMP_MUTE, 0);
1253                 snd_hda_codec_amp_stereo(codec, cap_nid, HDA_INPUT,
1254                                          dead->mux_idx,
1255                                          HDA_AMP_MUTE, HDA_AMP_MUTE);
1256         } else {
1257                 /* MUX style (e.g. ALC880) */
1258                 snd_hda_codec_write_cache(codec, cap_nid, 0,
1259                                           AC_VERB_SET_CONNECT_SEL,
1260                                           alive->mux_idx);
1261         }
1262         snd_hda_input_jack_report(codec, spec->ext_mic.pin);
1263
1264         /* FIXME: analog mixer */
1265 }
1266
1267 /* unsolicited event for HP jack sensing */
1268 static void alc_sku_unsol_event(struct hda_codec *codec, unsigned int res)
1269 {
1270         if (codec->vendor_id == 0x10ec0880)
1271                 res >>= 28;
1272         else
1273                 res >>= 26;
1274         switch (res) {
1275         case ALC880_HP_EVENT:
1276                 alc_hp_automute(codec);
1277                 break;
1278         case ALC880_FRONT_EVENT:
1279                 alc_line_automute(codec);
1280                 break;
1281         case ALC880_MIC_EVENT:
1282                 alc_mic_automute(codec);
1283                 break;
1284         }
1285 }
1286
1287 static void alc_inithook(struct hda_codec *codec)
1288 {
1289         alc_hp_automute(codec);
1290         alc_line_automute(codec);
1291         alc_mic_automute(codec);
1292 }
1293
1294 /* additional initialization for ALC888 variants */
1295 static void alc888_coef_init(struct hda_codec *codec)
1296 {
1297         unsigned int tmp;
1298
1299         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 0);
1300         tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
1301         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
1302         if ((tmp & 0xf0) == 0x20)
1303                 /* alc888S-VC */
1304                 snd_hda_codec_read(codec, 0x20, 0,
1305                                    AC_VERB_SET_PROC_COEF, 0x830);
1306          else
1307                  /* alc888-VB */
1308                  snd_hda_codec_read(codec, 0x20, 0,
1309                                     AC_VERB_SET_PROC_COEF, 0x3030);
1310 }
1311
1312 static void alc889_coef_init(struct hda_codec *codec)
1313 {
1314         unsigned int tmp;
1315
1316         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
1317         tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
1318         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
1319         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_PROC_COEF, tmp|0x2010);
1320 }
1321
1322 /* turn on/off EAPD control (only if available) */
1323 static void set_eapd(struct hda_codec *codec, hda_nid_t nid, int on)
1324 {
1325         if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1326                 return;
1327         if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
1328                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_EAPD_BTLENABLE,
1329                                     on ? 2 : 0);
1330 }
1331
1332 /* turn on/off EAPD controls of the codec */
1333 static void alc_auto_setup_eapd(struct hda_codec *codec, bool on)
1334 {
1335         /* We currently only handle front, HP */
1336         switch (codec->vendor_id) {
1337         case 0x10ec0260:
1338                 set_eapd(codec, 0x0f, on);
1339                 set_eapd(codec, 0x10, on);
1340                 break;
1341         case 0x10ec0262:
1342         case 0x10ec0267:
1343         case 0x10ec0268:
1344         case 0x10ec0269:
1345         case 0x10ec0270:
1346         case 0x10ec0272:
1347         case 0x10ec0660:
1348         case 0x10ec0662:
1349         case 0x10ec0663:
1350         case 0x10ec0665:
1351         case 0x10ec0862:
1352         case 0x10ec0889:
1353         case 0x10ec0892:
1354                 set_eapd(codec, 0x14, on);
1355                 set_eapd(codec, 0x15, on);
1356                 break;
1357         }
1358 }
1359
1360 /* generic shutup callback;
1361  * just turning off EPAD and a little pause for avoiding pop-noise
1362  */
1363 static void alc_eapd_shutup(struct hda_codec *codec)
1364 {
1365         alc_auto_setup_eapd(codec, false);
1366         msleep(200);
1367 }
1368
1369 static void alc_auto_init_amp(struct hda_codec *codec, int type)
1370 {
1371         unsigned int tmp;
1372
1373         switch (type) {
1374         case ALC_INIT_GPIO1:
1375                 snd_hda_sequence_write(codec, alc_gpio1_init_verbs);
1376                 break;
1377         case ALC_INIT_GPIO2:
1378                 snd_hda_sequence_write(codec, alc_gpio2_init_verbs);
1379                 break;
1380         case ALC_INIT_GPIO3:
1381                 snd_hda_sequence_write(codec, alc_gpio3_init_verbs);
1382                 break;
1383         case ALC_INIT_DEFAULT:
1384                 alc_auto_setup_eapd(codec, true);
1385                 switch (codec->vendor_id) {
1386                 case 0x10ec0260:
1387                         snd_hda_codec_write(codec, 0x1a, 0,
1388                                             AC_VERB_SET_COEF_INDEX, 7);
1389                         tmp = snd_hda_codec_read(codec, 0x1a, 0,
1390                                                  AC_VERB_GET_PROC_COEF, 0);
1391                         snd_hda_codec_write(codec, 0x1a, 0,
1392                                             AC_VERB_SET_COEF_INDEX, 7);
1393                         snd_hda_codec_write(codec, 0x1a, 0,
1394                                             AC_VERB_SET_PROC_COEF,
1395                                             tmp | 0x2010);
1396                         break;
1397                 case 0x10ec0262:
1398                 case 0x10ec0880:
1399                 case 0x10ec0882:
1400                 case 0x10ec0883:
1401                 case 0x10ec0885:
1402                 case 0x10ec0887:
1403                 /*case 0x10ec0889:*/ /* this causes an SPDIF problem */
1404                         alc889_coef_init(codec);
1405                         break;
1406                 case 0x10ec0888:
1407                         alc888_coef_init(codec);
1408                         break;
1409 #if 0 /* XXX: This may cause the silent output on speaker on some machines */
1410                 case 0x10ec0267:
1411                 case 0x10ec0268:
1412                         snd_hda_codec_write(codec, 0x20, 0,
1413                                             AC_VERB_SET_COEF_INDEX, 7);
1414                         tmp = snd_hda_codec_read(codec, 0x20, 0,
1415                                                  AC_VERB_GET_PROC_COEF, 0);
1416                         snd_hda_codec_write(codec, 0x20, 0,
1417                                             AC_VERB_SET_COEF_INDEX, 7);
1418                         snd_hda_codec_write(codec, 0x20, 0,
1419                                             AC_VERB_SET_PROC_COEF,
1420                                             tmp | 0x3000);
1421                         break;
1422 #endif /* XXX */
1423                 }
1424                 break;
1425         }
1426 }
1427
1428 static int alc_automute_mode_info(struct snd_kcontrol *kcontrol,
1429                                   struct snd_ctl_elem_info *uinfo)
1430 {
1431         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1432         struct alc_spec *spec = codec->spec;
1433         static const char * const texts2[] = {
1434                 "Disabled", "Enabled"
1435         };
1436         static const char * const texts3[] = {
1437                 "Disabled", "Speaker Only", "Line-Out+Speaker"
1438         };
1439         const char * const *texts;
1440
1441         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1442         uinfo->count = 1;
1443         if (spec->automute_hp_lo) {
1444                 uinfo->value.enumerated.items = 3;
1445                 texts = texts3;
1446         } else {
1447                 uinfo->value.enumerated.items = 2;
1448                 texts = texts2;
1449         }
1450         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1451                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
1452         strcpy(uinfo->value.enumerated.name,
1453                texts[uinfo->value.enumerated.item]);
1454         return 0;
1455 }
1456
1457 static int alc_automute_mode_get(struct snd_kcontrol *kcontrol,
1458                                  struct snd_ctl_elem_value *ucontrol)
1459 {
1460         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1461         struct alc_spec *spec = codec->spec;
1462         unsigned int val;
1463         if (!spec->automute)
1464                 val = 0;
1465         else if (!spec->automute_lines)
1466                 val = 1;
1467         else
1468                 val = 2;
1469         ucontrol->value.enumerated.item[0] = val;
1470         return 0;
1471 }
1472
1473 static int alc_automute_mode_put(struct snd_kcontrol *kcontrol,
1474                                  struct snd_ctl_elem_value *ucontrol)
1475 {
1476         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1477         struct alc_spec *spec = codec->spec;
1478
1479         switch (ucontrol->value.enumerated.item[0]) {
1480         case 0:
1481                 if (!spec->automute)
1482                         return 0;
1483                 spec->automute = 0;
1484                 break;
1485         case 1:
1486                 if (spec->automute && !spec->automute_lines)
1487                         return 0;
1488                 spec->automute = 1;
1489                 spec->automute_lines = 0;
1490                 break;
1491         case 2:
1492                 if (!spec->automute_hp_lo)
1493                         return -EINVAL;
1494                 if (spec->automute && spec->automute_lines)
1495                         return 0;
1496                 spec->automute = 1;
1497                 spec->automute_lines = 1;
1498                 break;
1499         default:
1500                 return -EINVAL;
1501         }
1502         update_speakers(codec);
1503         return 1;
1504 }
1505
1506 static struct snd_kcontrol_new alc_automute_mode_enum = {
1507         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1508         .name = "Auto-Mute Mode",
1509         .info = alc_automute_mode_info,
1510         .get = alc_automute_mode_get,
1511         .put = alc_automute_mode_put,
1512 };
1513
1514 static struct snd_kcontrol_new *alc_kcontrol_new(struct alc_spec *spec);
1515
1516 static int alc_add_automute_mode_enum(struct hda_codec *codec)
1517 {
1518         struct alc_spec *spec = codec->spec;
1519         struct snd_kcontrol_new *knew;
1520
1521         knew = alc_kcontrol_new(spec);
1522         if (!knew)
1523                 return -ENOMEM;
1524         *knew = alc_automute_mode_enum;
1525         knew->name = kstrdup("Auto-Mute Mode", GFP_KERNEL);
1526         if (!knew->name)
1527                 return -ENOMEM;
1528         return 0;
1529 }
1530
1531 static void alc_init_auto_hp(struct hda_codec *codec)
1532 {
1533         struct alc_spec *spec = codec->spec;
1534         struct auto_pin_cfg *cfg = &spec->autocfg;
1535         int present = 0;
1536         int i;
1537
1538         if (cfg->hp_pins[0])
1539                 present++;
1540         if (cfg->line_out_pins[0])
1541                 present++;
1542         if (cfg->speaker_pins[0])
1543                 present++;
1544         if (present < 2) /* need two different output types */
1545                 return;
1546         if (present == 3)
1547                 spec->automute_hp_lo = 1; /* both HP and LO automute */
1548
1549         if (!cfg->speaker_pins[0]) {
1550                 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1551                        sizeof(cfg->speaker_pins));
1552                 cfg->speaker_outs = cfg->line_outs;
1553         }
1554
1555         if (!cfg->hp_pins[0]) {
1556                 memcpy(cfg->hp_pins, cfg->line_out_pins,
1557                        sizeof(cfg->hp_pins));
1558                 cfg->hp_outs = cfg->line_outs;
1559         }
1560
1561         for (i = 0; i < cfg->hp_outs; i++) {
1562                 hda_nid_t nid = cfg->hp_pins[i];
1563                 if (!(snd_hda_query_pin_caps(codec, nid) &
1564                       AC_PINCAP_PRES_DETECT))
1565                         continue;
1566                 snd_printdd("realtek: Enable HP auto-muting on NID 0x%x\n",
1567                             nid);
1568                 snd_hda_codec_write_cache(codec, nid, 0,
1569                                   AC_VERB_SET_UNSOLICITED_ENABLE,
1570                                   AC_USRSP_EN | ALC880_HP_EVENT);
1571                 spec->automute = 1;
1572                 spec->automute_mode = ALC_AUTOMUTE_PIN;
1573         }
1574         if (spec->automute && cfg->line_out_pins[0] &&
1575             cfg->line_out_pins[0] != cfg->hp_pins[0] &&
1576             cfg->line_out_pins[0] != cfg->speaker_pins[0]) {
1577                 for (i = 0; i < cfg->line_outs; i++) {
1578                         hda_nid_t nid = cfg->line_out_pins[i];
1579                         if (!(snd_hda_query_pin_caps(codec, nid) &
1580                               AC_PINCAP_PRES_DETECT))
1581                                 continue;
1582                         snd_printdd("realtek: Enable Line-Out auto-muting "
1583                                     "on NID 0x%x\n", nid);
1584                         snd_hda_codec_write_cache(codec, nid, 0,
1585                                         AC_VERB_SET_UNSOLICITED_ENABLE,
1586                                         AC_USRSP_EN | ALC880_FRONT_EVENT);
1587                         spec->detect_line = 1;
1588                 }
1589                 spec->automute_lines = 1;
1590         }
1591
1592         if (spec->automute) {
1593                 /* create a control for automute mode */
1594                 alc_add_automute_mode_enum(codec);
1595                 spec->unsol_event = alc_sku_unsol_event;
1596         }
1597 }
1598
1599 static void alc_init_auto_mic(struct hda_codec *codec)
1600 {
1601         struct alc_spec *spec = codec->spec;
1602         struct auto_pin_cfg *cfg = &spec->autocfg;
1603         hda_nid_t fixed, ext;
1604         int i;
1605
1606         /* there must be only two mic inputs exclusively */
1607         for (i = 0; i < cfg->num_inputs; i++)
1608                 if (cfg->inputs[i].type >= AUTO_PIN_LINE_IN)
1609                         return;
1610
1611         fixed = ext = 0;
1612         for (i = 0; i < cfg->num_inputs; i++) {
1613                 hda_nid_t nid = cfg->inputs[i].pin;
1614                 unsigned int defcfg;
1615                 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1616                 switch (snd_hda_get_input_pin_attr(defcfg)) {
1617                 case INPUT_PIN_ATTR_INT:
1618                         if (fixed)
1619                                 return; /* already occupied */
1620                         fixed = nid;
1621                         break;
1622                 case INPUT_PIN_ATTR_UNUSED:
1623                         return; /* invalid entry */
1624                 default:
1625                         if (ext)
1626                                 return; /* already occupied */
1627                         ext = nid;
1628                         break;
1629                 }
1630         }
1631         if (!ext || !fixed)
1632                 return;
1633         if (!(get_wcaps(codec, ext) & AC_WCAP_UNSOL_CAP))
1634                 return; /* no unsol support */
1635         snd_printdd("realtek: Enable auto-mic switch on NID 0x%x/0x%x\n",
1636                     ext, fixed);
1637         spec->ext_mic.pin = ext;
1638         spec->int_mic.pin = fixed;
1639         spec->ext_mic.mux_idx = MUX_IDX_UNDEF; /* set later */
1640         spec->int_mic.mux_idx = MUX_IDX_UNDEF; /* set later */
1641         spec->auto_mic = 1;
1642         snd_hda_codec_write_cache(codec, spec->ext_mic.pin, 0,
1643                                   AC_VERB_SET_UNSOLICITED_ENABLE,
1644                                   AC_USRSP_EN | ALC880_MIC_EVENT);
1645         spec->unsol_event = alc_sku_unsol_event;
1646 }
1647
1648 /* Could be any non-zero and even value. When used as fixup, tells
1649  * the driver to ignore any present sku defines.
1650  */
1651 #define ALC_FIXUP_SKU_IGNORE (2)
1652
1653 static int alc_auto_parse_customize_define(struct hda_codec *codec)
1654 {
1655         unsigned int ass, tmp, i;
1656         unsigned nid = 0;
1657         struct alc_spec *spec = codec->spec;
1658
1659         spec->cdefine.enable_pcbeep = 1; /* assume always enabled */
1660
1661         if (spec->cdefine.fixup) {
1662                 ass = spec->cdefine.sku_cfg;
1663                 if (ass == ALC_FIXUP_SKU_IGNORE)
1664                         return -1;
1665                 goto do_sku;
1666         }
1667
1668         ass = codec->subsystem_id & 0xffff;
1669         if (ass != codec->bus->pci->subsystem_device && (ass & 1))
1670                 goto do_sku;
1671
1672         nid = 0x1d;
1673         if (codec->vendor_id == 0x10ec0260)
1674                 nid = 0x17;
1675         ass = snd_hda_codec_get_pincfg(codec, nid);
1676
1677         if (!(ass & 1)) {
1678                 printk(KERN_INFO "hda_codec: %s: SKU not ready 0x%08x\n",
1679                        codec->chip_name, ass);
1680                 return -1;
1681         }
1682
1683         /* check sum */
1684         tmp = 0;
1685         for (i = 1; i < 16; i++) {
1686                 if ((ass >> i) & 1)
1687                         tmp++;
1688         }
1689         if (((ass >> 16) & 0xf) != tmp)
1690                 return -1;
1691
1692         spec->cdefine.port_connectivity = ass >> 30;
1693         spec->cdefine.enable_pcbeep = (ass & 0x100000) >> 20;
1694         spec->cdefine.check_sum = (ass >> 16) & 0xf;
1695         spec->cdefine.customization = ass >> 8;
1696 do_sku:
1697         spec->cdefine.sku_cfg = ass;
1698         spec->cdefine.external_amp = (ass & 0x38) >> 3;
1699         spec->cdefine.platform_type = (ass & 0x4) >> 2;
1700         spec->cdefine.swap = (ass & 0x2) >> 1;
1701         spec->cdefine.override = ass & 0x1;
1702
1703         snd_printd("SKU: Nid=0x%x sku_cfg=0x%08x\n",
1704                    nid, spec->cdefine.sku_cfg);
1705         snd_printd("SKU: port_connectivity=0x%x\n",
1706                    spec->cdefine.port_connectivity);
1707         snd_printd("SKU: enable_pcbeep=0x%x\n", spec->cdefine.enable_pcbeep);
1708         snd_printd("SKU: check_sum=0x%08x\n", spec->cdefine.check_sum);
1709         snd_printd("SKU: customization=0x%08x\n", spec->cdefine.customization);
1710         snd_printd("SKU: external_amp=0x%x\n", spec->cdefine.external_amp);
1711         snd_printd("SKU: platform_type=0x%x\n", spec->cdefine.platform_type);
1712         snd_printd("SKU: swap=0x%x\n", spec->cdefine.swap);
1713         snd_printd("SKU: override=0x%x\n", spec->cdefine.override);
1714
1715         return 0;
1716 }
1717
1718 /* check subsystem ID and set up device-specific initialization;
1719  * return 1 if initialized, 0 if invalid SSID
1720  */
1721 /* 32-bit subsystem ID for BIOS loading in HD Audio codec.
1722  *      31 ~ 16 :       Manufacture ID
1723  *      15 ~ 8  :       SKU ID
1724  *      7  ~ 0  :       Assembly ID
1725  *      port-A --> pin 39/41, port-E --> pin 14/15, port-D --> pin 35/36
1726  */
1727 static int alc_subsystem_id(struct hda_codec *codec,
1728                             hda_nid_t porta, hda_nid_t porte,
1729                             hda_nid_t portd, hda_nid_t porti)
1730 {
1731         unsigned int ass, tmp, i;
1732         unsigned nid;
1733         struct alc_spec *spec = codec->spec;
1734
1735         if (spec->cdefine.fixup) {
1736                 ass = spec->cdefine.sku_cfg;
1737                 if (ass == ALC_FIXUP_SKU_IGNORE)
1738                         return 0;
1739                 goto do_sku;
1740         }
1741
1742         ass = codec->subsystem_id & 0xffff;
1743         if ((ass != codec->bus->pci->subsystem_device) && (ass & 1))
1744                 goto do_sku;
1745
1746         /* invalid SSID, check the special NID pin defcfg instead */
1747         /*
1748          * 31~30        : port connectivity
1749          * 29~21        : reserve
1750          * 20           : PCBEEP input
1751          * 19~16        : Check sum (15:1)
1752          * 15~1         : Custom
1753          * 0            : override
1754         */
1755         nid = 0x1d;
1756         if (codec->vendor_id == 0x10ec0260)
1757                 nid = 0x17;
1758         ass = snd_hda_codec_get_pincfg(codec, nid);
1759         snd_printd("realtek: No valid SSID, "
1760                    "checking pincfg 0x%08x for NID 0x%x\n",
1761                    ass, nid);
1762         if (!(ass & 1))
1763                 return 0;
1764         if ((ass >> 30) != 1)   /* no physical connection */
1765                 return 0;
1766
1767         /* check sum */
1768         tmp = 0;
1769         for (i = 1; i < 16; i++) {
1770                 if ((ass >> i) & 1)
1771                         tmp++;
1772         }
1773         if (((ass >> 16) & 0xf) != tmp)
1774                 return 0;
1775 do_sku:
1776         snd_printd("realtek: Enabling init ASM_ID=0x%04x CODEC_ID=%08x\n",
1777                    ass & 0xffff, codec->vendor_id);
1778         /*
1779          * 0 : override
1780          * 1 :  Swap Jack
1781          * 2 : 0 --> Desktop, 1 --> Laptop
1782          * 3~5 : External Amplifier control
1783          * 7~6 : Reserved
1784         */
1785         tmp = (ass & 0x38) >> 3;        /* external Amp control */
1786         switch (tmp) {
1787         case 1:
1788                 spec->init_amp = ALC_INIT_GPIO1;
1789                 break;
1790         case 3:
1791                 spec->init_amp = ALC_INIT_GPIO2;
1792                 break;
1793         case 7:
1794                 spec->init_amp = ALC_INIT_GPIO3;
1795                 break;
1796         case 5:
1797         default:
1798                 spec->init_amp = ALC_INIT_DEFAULT;
1799                 break;
1800         }
1801
1802         /* is laptop or Desktop and enable the function "Mute internal speaker
1803          * when the external headphone out jack is plugged"
1804          */
1805         if (!(ass & 0x8000))
1806                 return 1;
1807         /*
1808          * 10~8 : Jack location
1809          * 12~11: Headphone out -> 00: PortA, 01: PortE, 02: PortD, 03: Resvered
1810          * 14~13: Resvered
1811          * 15   : 1 --> enable the function "Mute internal speaker
1812          *              when the external headphone out jack is plugged"
1813          */
1814         if (!spec->autocfg.hp_pins[0]) {
1815                 hda_nid_t nid;
1816                 tmp = (ass >> 11) & 0x3;        /* HP to chassis */
1817                 if (tmp == 0)
1818                         nid = porta;
1819                 else if (tmp == 1)
1820                         nid = porte;
1821                 else if (tmp == 2)
1822                         nid = portd;
1823                 else if (tmp == 3)
1824                         nid = porti;
1825                 else
1826                         return 1;
1827                 for (i = 0; i < spec->autocfg.line_outs; i++)
1828                         if (spec->autocfg.line_out_pins[i] == nid)
1829                                 return 1;
1830                 spec->autocfg.hp_pins[0] = nid;
1831         }
1832         return 1;
1833 }
1834
1835 static void alc_ssid_check(struct hda_codec *codec,
1836                            hda_nid_t porta, hda_nid_t porte,
1837                            hda_nid_t portd, hda_nid_t porti)
1838 {
1839         if (!alc_subsystem_id(codec, porta, porte, portd, porti)) {
1840                 struct alc_spec *spec = codec->spec;
1841                 snd_printd("realtek: "
1842                            "Enable default setup for auto mode as fallback\n");
1843                 spec->init_amp = ALC_INIT_DEFAULT;
1844         }
1845
1846         alc_init_auto_hp(codec);
1847         alc_init_auto_mic(codec);
1848 }
1849
1850 /*
1851  * Fix-up pin default configurations and add default verbs
1852  */
1853
1854 struct alc_pincfg {
1855         hda_nid_t nid;
1856         u32 val;
1857 };
1858
1859 struct alc_model_fixup {
1860         const int id;
1861         const char *name;
1862 };
1863
1864 struct alc_fixup {
1865         int type;
1866         bool chained;
1867         int chain_id;
1868         union {
1869                 unsigned int sku;
1870                 const struct alc_pincfg *pins;
1871                 const struct hda_verb *verbs;
1872                 void (*func)(struct hda_codec *codec,
1873                              const struct alc_fixup *fix,
1874                              int action);
1875         } v;
1876 };
1877
1878 enum {
1879         ALC_FIXUP_INVALID,
1880         ALC_FIXUP_SKU,
1881         ALC_FIXUP_PINS,
1882         ALC_FIXUP_VERBS,
1883         ALC_FIXUP_FUNC,
1884 };
1885
1886 enum {
1887         ALC_FIXUP_ACT_PRE_PROBE,
1888         ALC_FIXUP_ACT_PROBE,
1889         ALC_FIXUP_ACT_INIT,
1890 };
1891
1892 static void alc_apply_fixup(struct hda_codec *codec, int action)
1893 {
1894         struct alc_spec *spec = codec->spec;
1895         int id = spec->fixup_id;
1896 #ifdef CONFIG_SND_DEBUG_VERBOSE
1897         const char *modelname = spec->fixup_name;
1898 #endif
1899         int depth = 0;
1900
1901         if (!spec->fixup_list)
1902                 return;
1903
1904         while (id >= 0) {
1905                 const struct alc_fixup *fix = spec->fixup_list + id;
1906                 const struct alc_pincfg *cfg;
1907
1908                 switch (fix->type) {
1909                 case ALC_FIXUP_SKU:
1910                         if (action != ALC_FIXUP_ACT_PRE_PROBE || !fix->v.sku)
1911                                 break;;
1912                         snd_printdd(KERN_INFO "hda_codec: %s: "
1913                                     "Apply sku override for %s\n",
1914                                     codec->chip_name, modelname);
1915                         spec->cdefine.sku_cfg = fix->v.sku;
1916                         spec->cdefine.fixup = 1;
1917                         break;
1918                 case ALC_FIXUP_PINS:
1919                         cfg = fix->v.pins;
1920                         if (action != ALC_FIXUP_ACT_PRE_PROBE || !cfg)
1921                                 break;
1922                         snd_printdd(KERN_INFO "hda_codec: %s: "
1923                                     "Apply pincfg for %s\n",
1924                                     codec->chip_name, modelname);
1925                         for (; cfg->nid; cfg++)
1926                                 snd_hda_codec_set_pincfg(codec, cfg->nid,
1927                                                          cfg->val);
1928                         break;
1929                 case ALC_FIXUP_VERBS:
1930                         if (action != ALC_FIXUP_ACT_PROBE || !fix->v.verbs)
1931                                 break;
1932                         snd_printdd(KERN_INFO "hda_codec: %s: "
1933                                     "Apply fix-verbs for %s\n",
1934                                     codec->chip_name, modelname);
1935                         add_verb(codec->spec, fix->v.verbs);
1936                         break;
1937                 case ALC_FIXUP_FUNC:
1938                         if (!fix->v.func)
1939                                 break;
1940                         snd_printdd(KERN_INFO "hda_codec: %s: "
1941                                     "Apply fix-func for %s\n",
1942                                     codec->chip_name, modelname);
1943                         fix->v.func(codec, fix, action);
1944                         break;
1945                 default:
1946                         snd_printk(KERN_ERR "hda_codec: %s: "
1947                                    "Invalid fixup type %d\n",
1948                                    codec->chip_name, fix->type);
1949                         break;
1950                 }
1951                 if (!fix[id].chained)
1952                         break;
1953                 if (++depth > 10)
1954                         break;
1955                 id = fix[id].chain_id;
1956         }
1957 }
1958
1959 static void alc_pick_fixup(struct hda_codec *codec,
1960                            const struct alc_model_fixup *models,
1961                            const struct snd_pci_quirk *quirk,
1962                            const struct alc_fixup *fixlist)
1963 {
1964         struct alc_spec *spec = codec->spec;
1965         int id = -1;
1966         const char *name = NULL;
1967
1968         if (codec->modelname && models) {
1969                 while (models->name) {
1970                         if (!strcmp(codec->modelname, models->name)) {
1971                                 id = models->id;
1972                                 name = models->name;
1973                                 break;
1974                         }
1975                         models++;
1976                 }
1977         }
1978         if (id < 0) {
1979                 quirk = snd_pci_quirk_lookup(codec->bus->pci, quirk);
1980                 if (quirk) {
1981                         id = quirk->value;
1982 #ifdef CONFIG_SND_DEBUG_VERBOSE
1983                         name = quirk->name;
1984 #endif
1985                 }
1986         }
1987
1988         spec->fixup_id = id;
1989         if (id >= 0) {
1990                 spec->fixup_list = fixlist;
1991                 spec->fixup_name = name;
1992         }
1993 }
1994
1995 static int alc_read_coef_idx(struct hda_codec *codec,
1996                         unsigned int coef_idx)
1997 {
1998         unsigned int val;
1999         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX,
2000                                 coef_idx);
2001         val = snd_hda_codec_read(codec, 0x20, 0,
2002                                 AC_VERB_GET_PROC_COEF, 0);
2003         return val;
2004 }
2005
2006 static void alc_write_coef_idx(struct hda_codec *codec, unsigned int coef_idx,
2007                                                         unsigned int coef_val)
2008 {
2009         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX,
2010                             coef_idx);
2011         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_PROC_COEF,
2012                             coef_val);
2013 }
2014
2015 /* set right pin controls for digital I/O */
2016 static void alc_auto_init_digital(struct hda_codec *codec)
2017 {
2018         struct alc_spec *spec = codec->spec;
2019         int i;
2020         hda_nid_t pin;
2021
2022         for (i = 0; i < spec->autocfg.dig_outs; i++) {
2023                 pin = spec->autocfg.dig_out_pins[i];
2024                 if (pin) {
2025                         snd_hda_codec_write(codec, pin, 0,
2026                                             AC_VERB_SET_PIN_WIDGET_CONTROL,
2027                                             PIN_OUT);
2028                 }
2029         }
2030         pin = spec->autocfg.dig_in_pin;
2031         if (pin)
2032                 snd_hda_codec_write(codec, pin, 0,
2033                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
2034                                     PIN_IN);
2035 }
2036
2037 /* parse digital I/Os and set up NIDs in BIOS auto-parse mode */
2038 static void alc_auto_parse_digital(struct hda_codec *codec)
2039 {
2040         struct alc_spec *spec = codec->spec;
2041         int i, err;
2042         hda_nid_t dig_nid;
2043
2044         /* support multiple SPDIFs; the secondary is set up as a slave */
2045         for (i = 0; i < spec->autocfg.dig_outs; i++) {
2046                 err = snd_hda_get_connections(codec,
2047                                               spec->autocfg.dig_out_pins[i],
2048                                               &dig_nid, 1);
2049                 if (err < 0)
2050                         continue;
2051                 if (!i) {
2052                         spec->multiout.dig_out_nid = dig_nid;
2053                         spec->dig_out_type = spec->autocfg.dig_out_type[0];
2054                 } else {
2055                         spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2056                         if (i >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2057                                 break;
2058                         spec->slave_dig_outs[i - 1] = dig_nid;
2059                 }
2060         }
2061
2062         if (spec->autocfg.dig_in_pin) {
2063                 dig_nid = codec->start_nid;
2064                 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2065                         unsigned int wcaps = get_wcaps(codec, dig_nid);
2066                         if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2067                                 continue;
2068                         if (!(wcaps & AC_WCAP_DIGITAL))
2069                                 continue;
2070                         if (!(wcaps & AC_WCAP_CONN_LIST))
2071                                 continue;
2072                         err = get_connection_index(codec, dig_nid,
2073                                                    spec->autocfg.dig_in_pin);
2074                         if (err >= 0) {
2075                                 spec->dig_in_nid = dig_nid;
2076                                 break;
2077                         }
2078                 }
2079         }
2080 }
2081
2082 /*
2083  * ALC888
2084  */
2085
2086 /*
2087  * 2ch mode
2088  */
2089 static struct hda_verb alc888_4ST_ch2_intel_init[] = {
2090 /* Mic-in jack as mic in */
2091         { 0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
2092         { 0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2093 /* Line-in jack as Line in */
2094         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
2095         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2096 /* Line-Out as Front */
2097         { 0x17, AC_VERB_SET_CONNECT_SEL, 0x00},
2098         { } /* end */
2099 };
2100
2101 /*
2102  * 4ch mode
2103  */
2104 static struct hda_verb alc888_4ST_ch4_intel_init[] = {
2105 /* Mic-in jack as mic in */
2106         { 0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
2107         { 0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2108 /* Line-in jack as Surround */
2109         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2110         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2111 /* Line-Out as Front */
2112         { 0x17, AC_VERB_SET_CONNECT_SEL, 0x00},
2113         { } /* end */
2114 };
2115
2116 /*
2117  * 6ch mode
2118  */
2119 static struct hda_verb alc888_4ST_ch6_intel_init[] = {
2120 /* Mic-in jack as CLFE */
2121         { 0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2122         { 0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2123 /* Line-in jack as Surround */
2124         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2125         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2126 /* Line-Out as CLFE (workaround because Mic-in is not loud enough) */
2127         { 0x17, AC_VERB_SET_CONNECT_SEL, 0x03},
2128         { } /* end */
2129 };
2130
2131 /*
2132  * 8ch mode
2133  */
2134 static struct hda_verb alc888_4ST_ch8_intel_init[] = {
2135 /* Mic-in jack as CLFE */
2136         { 0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2137         { 0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2138 /* Line-in jack as Surround */
2139         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2140         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2141 /* Line-Out as Side */
2142         { 0x17, AC_VERB_SET_CONNECT_SEL, 0x03},
2143         { } /* end */
2144 };
2145
2146 static struct hda_channel_mode alc888_4ST_8ch_intel_modes[4] = {
2147         { 2, alc888_4ST_ch2_intel_init },
2148         { 4, alc888_4ST_ch4_intel_init },
2149         { 6, alc888_4ST_ch6_intel_init },
2150         { 8, alc888_4ST_ch8_intel_init },
2151 };
2152
2153 /*
2154  * ALC888 Fujitsu Siemens Amillo xa3530
2155  */
2156
2157 static struct hda_verb alc888_fujitsu_xa3530_verbs[] = {
2158 /* Front Mic: set to PIN_IN (empty by default) */
2159         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2160 /* Connect Internal HP to Front */
2161         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2162         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2163         {0x14, AC_VERB_SET_CONNECT_SEL, 0x00},
2164 /* Connect Bass HP to Front */
2165         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2166         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2167         {0x15, AC_VERB_SET_CONNECT_SEL, 0x00},
2168 /* Connect Line-Out side jack (SPDIF) to Side */
2169         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2170         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2171         {0x17, AC_VERB_SET_CONNECT_SEL, 0x03},
2172 /* Connect Mic jack to CLFE */
2173         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2174         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2175         {0x18, AC_VERB_SET_CONNECT_SEL, 0x02},
2176 /* Connect Line-in jack to Surround */
2177         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2178         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2179         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x01},
2180 /* Connect HP out jack to Front */
2181         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2182         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2183         {0x1b, AC_VERB_SET_CONNECT_SEL, 0x00},
2184 /* Enable unsolicited event for HP jack and Line-out jack */
2185         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, ALC880_HP_EVENT | AC_USRSP_EN},
2186         {0x17, AC_VERB_SET_UNSOLICITED_ENABLE, ALC880_HP_EVENT | AC_USRSP_EN},
2187         {}
2188 };
2189
2190 static void alc889_automute_setup(struct hda_codec *codec)
2191 {
2192         struct alc_spec *spec = codec->spec;
2193
2194         spec->autocfg.hp_pins[0] = 0x15;
2195         spec->autocfg.speaker_pins[0] = 0x14;
2196         spec->autocfg.speaker_pins[1] = 0x16;
2197         spec->autocfg.speaker_pins[2] = 0x17;
2198         spec->autocfg.speaker_pins[3] = 0x19;
2199         spec->autocfg.speaker_pins[4] = 0x1a;
2200         spec->automute = 1;
2201         spec->automute_mode = ALC_AUTOMUTE_AMP;
2202 }
2203
2204 static void alc889_intel_init_hook(struct hda_codec *codec)
2205 {
2206         alc889_coef_init(codec);
2207         alc_hp_automute(codec);
2208 }
2209
2210 static void alc888_fujitsu_xa3530_setup(struct hda_codec *codec)
2211 {
2212         struct alc_spec *spec = codec->spec;
2213
2214         spec->autocfg.hp_pins[0] = 0x17; /* line-out */
2215         spec->autocfg.hp_pins[1] = 0x1b; /* hp */
2216         spec->autocfg.speaker_pins[0] = 0x14; /* speaker */
2217         spec->autocfg.speaker_pins[1] = 0x15; /* bass */
2218         spec->automute = 1;
2219         spec->automute_mode = ALC_AUTOMUTE_AMP;
2220 }
2221
2222 /*
2223  * ALC888 Acer Aspire 4930G model
2224  */
2225
2226 static struct hda_verb alc888_acer_aspire_4930g_verbs[] = {
2227 /* Front Mic: set to PIN_IN (empty by default) */
2228         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2229 /* Unselect Front Mic by default in input mixer 3 */
2230         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0xb)},
2231 /* Enable unsolicited event for HP jack */
2232         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, ALC880_HP_EVENT | AC_USRSP_EN},
2233 /* Connect Internal HP to front */
2234         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2235         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2236         {0x14, AC_VERB_SET_CONNECT_SEL, 0x00},
2237 /* Connect HP out to front */
2238         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2239         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2240         {0x15, AC_VERB_SET_CONNECT_SEL, 0x00},
2241         {0x15, AC_VERB_SET_EAPD_BTLENABLE, 2},
2242         { }
2243 };
2244
2245 /*
2246  * ALC888 Acer Aspire 6530G model
2247  */
2248
2249 static struct hda_verb alc888_acer_aspire_6530g_verbs[] = {
2250 /* Route to built-in subwoofer as well as speakers */
2251         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2252         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2253         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2254         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2255 /* Bias voltage on for external mic port */
2256         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN | PIN_VREF80},
2257 /* Front Mic: set to PIN_IN (empty by default) */
2258         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2259 /* Unselect Front Mic by default in input mixer 3 */
2260         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0xb)},
2261 /* Enable unsolicited event for HP jack */
2262         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, ALC880_HP_EVENT | AC_USRSP_EN},
2263 /* Enable speaker output */
2264         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2265         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2266         {0x14, AC_VERB_SET_EAPD_BTLENABLE, 2},
2267 /* Enable headphone output */
2268         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | PIN_HP},
2269         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2270         {0x15, AC_VERB_SET_CONNECT_SEL, 0x00},
2271         {0x15, AC_VERB_SET_EAPD_BTLENABLE, 2},
2272         { }
2273 };
2274
2275 /*
2276  *ALC888 Acer Aspire 7730G model
2277  */
2278
2279 static struct hda_verb alc888_acer_aspire_7730G_verbs[] = {
2280 /* Bias voltage on for external mic port */
2281         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN | PIN_VREF80},
2282 /* Front Mic: set to PIN_IN (empty by default) */
2283         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2284 /* Unselect Front Mic by default in input mixer 3 */
2285         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0xb)},
2286 /* Enable unsolicited event for HP jack */
2287         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, ALC880_HP_EVENT | AC_USRSP_EN},
2288 /* Enable speaker output */
2289         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2290         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2291         {0x14, AC_VERB_SET_EAPD_BTLENABLE, 2},
2292 /* Enable headphone output */
2293         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | PIN_HP},
2294         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2295         {0x15, AC_VERB_SET_CONNECT_SEL, 0x00},
2296         {0x15, AC_VERB_SET_EAPD_BTLENABLE, 2},
2297 /*Enable internal subwoofer */
2298         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2299         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2300         {0x17, AC_VERB_SET_CONNECT_SEL, 0x02},
2301         {0x17, AC_VERB_SET_EAPD_BTLENABLE, 2},
2302         { }
2303 };
2304
2305 /*
2306  * ALC889 Acer Aspire 8930G model
2307  */
2308
2309 static struct hda_verb alc889_acer_aspire_8930g_verbs[] = {
2310 /* Front Mic: set to PIN_IN (empty by default) */
2311         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2312 /* Unselect Front Mic by default in input mixer 3 */
2313         {0x22, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0xb)},
2314 /* Enable unsolicited event for HP jack */
2315         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, ALC880_HP_EVENT | AC_USRSP_EN},
2316 /* Connect Internal Front to Front */
2317         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2318         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2319         {0x14, AC_VERB_SET_CONNECT_SEL, 0x00},
2320 /* Connect Internal Rear to Rear */
2321         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2322         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2323         {0x1b, AC_VERB_SET_CONNECT_SEL, 0x01},
2324 /* Connect Internal CLFE to CLFE */
2325         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2326         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2327         {0x16, AC_VERB_SET_CONNECT_SEL, 0x02},
2328 /* Connect HP out to Front */
2329         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | PIN_HP},
2330         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2331         {0x15, AC_VERB_SET_CONNECT_SEL, 0x00},
2332 /* Enable all DACs */
2333 /*  DAC DISABLE/MUTE 1? */
2334 /*  setting bits 1-5 disables DAC nids 0x02-0x06 apparently. Init=0x38 */
2335         {0x20, AC_VERB_SET_COEF_INDEX, 0x03},
2336         {0x20, AC_VERB_SET_PROC_COEF, 0x0000},
2337 /*  DAC DISABLE/MUTE 2? */
2338 /*  some bit here disables the other DACs. Init=0x4900 */
2339         {0x20, AC_VERB_SET_COEF_INDEX, 0x08},
2340         {0x20, AC_VERB_SET_PROC_COEF, 0x0000},
2341 /* DMIC fix
2342  * This laptop has a stereo digital microphone. The mics are only 1cm apart
2343  * which makes the stereo useless. However, either the mic or the ALC889
2344  * makes the signal become a difference/sum signal instead of standard
2345  * stereo, which is annoying. So instead we flip this bit which makes the
2346  * codec replicate the sum signal to both channels, turning it into a
2347  * normal mono mic.
2348  */
2349 /*  DMIC_CONTROL? Init value = 0x0001 */
2350         {0x20, AC_VERB_SET_COEF_INDEX, 0x0b},
2351         {0x20, AC_VERB_SET_PROC_COEF, 0x0003},
2352         { }
2353 };
2354
2355 static struct hda_input_mux alc888_2_capture_sources[2] = {
2356         /* Front mic only available on one ADC */
2357         {
2358                 .num_items = 4,
2359                 .items = {
2360                         { "Mic", 0x0 },
2361                         { "Line", 0x2 },
2362                         { "CD", 0x4 },
2363                         { "Front Mic", 0xb },
2364                 },
2365         },
2366         {
2367                 .num_items = 3,
2368                 .items = {
2369                         { "Mic", 0x0 },
2370                         { "Line", 0x2 },
2371                         { "CD", 0x4 },
2372                 },
2373         }
2374 };
2375
2376 static struct hda_input_mux alc888_acer_aspire_6530_sources[2] = {
2377         /* Interal mic only available on one ADC */
2378         {
2379                 .num_items = 5,
2380                 .items = {
2381                         { "Mic", 0x0 },
2382                         { "Line In", 0x2 },
2383                         { "CD", 0x4 },
2384                         { "Input Mix", 0xa },
2385                         { "Internal Mic", 0xb },
2386                 },
2387         },
2388         {
2389                 .num_items = 4,
2390                 .items = {
2391                         { "Mic", 0x0 },
2392                         { "Line In", 0x2 },
2393                         { "CD", 0x4 },
2394                         { "Input Mix", 0xa },
2395                 },
2396         }
2397 };
2398
2399 static struct hda_input_mux alc889_capture_sources[3] = {
2400         /* Digital mic only available on first "ADC" */
2401         {
2402                 .num_items = 5,
2403                 .items = {
2404                         { "Mic", 0x0 },
2405                         { "Line", 0x2 },
2406                         { "CD", 0x4 },
2407                         { "Front Mic", 0xb },
2408                         { "Input Mix", 0xa },
2409                 },
2410         },
2411         {
2412                 .num_items = 4,
2413                 .items = {
2414                         { "Mic", 0x0 },
2415                         { "Line", 0x2 },
2416                         { "CD", 0x4 },
2417                         { "Input Mix", 0xa },
2418                 },
2419         },
2420         {
2421                 .num_items = 4,
2422                 .items = {
2423                         { "Mic", 0x0 },
2424                         { "Line", 0x2 },
2425                         { "CD", 0x4 },
2426                         { "Input Mix", 0xa },
2427                 },
2428         }
2429 };
2430
2431 static struct snd_kcontrol_new alc888_base_mixer[] = {
2432         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
2433         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
2434         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
2435         HDA_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
2436         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0,
2437                 HDA_OUTPUT),
2438         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
2439         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
2440         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
2441         HDA_CODEC_VOLUME("Side Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
2442         HDA_BIND_MUTE("Side Playback Switch", 0x0f, 2, HDA_INPUT),
2443         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
2444         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
2445         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
2446         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
2447         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
2448         HDA_CODEC_VOLUME("Mic Boost Volume", 0x18, 0, HDA_INPUT),
2449         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
2450         { } /* end */
2451 };
2452
2453 static struct snd_kcontrol_new alc888_acer_aspire_4930g_mixer[] = {
2454         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
2455         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
2456         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
2457         HDA_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
2458         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0,
2459                 HDA_OUTPUT),
2460         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
2461         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
2462         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
2463         HDA_CODEC_VOLUME_MONO("Internal LFE Playback Volume", 0x0f, 1, 0x0, HDA_OUTPUT),
2464         HDA_BIND_MUTE_MONO("Internal LFE Playback Switch", 0x0f, 1, 2, HDA_INPUT),
2465         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
2466         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
2467         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
2468         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
2469         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
2470         HDA_CODEC_VOLUME("Mic Boost Volume", 0x18, 0, HDA_INPUT),
2471         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
2472         { } /* end */
2473 };
2474
2475 static struct snd_kcontrol_new alc889_acer_aspire_8930g_mixer[] = {
2476         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
2477         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
2478         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
2479         HDA_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
2480         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0,
2481                 HDA_OUTPUT),
2482         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
2483         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
2484         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
2485         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
2486         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
2487         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
2488         HDA_CODEC_VOLUME("Mic Boost Volume", 0x18, 0, HDA_INPUT),
2489         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
2490         { } /* end */
2491 };
2492
2493
2494 static void alc888_acer_aspire_4930g_setup(struct hda_codec *codec)
2495 {
2496         struct alc_spec *spec = codec->spec;
2497
2498         spec->autocfg.hp_pins[0] = 0x15;
2499         spec->autocfg.speaker_pins[0] = 0x14;
2500         spec->autocfg.speaker_pins[1] = 0x16;
2501         spec->autocfg.speaker_pins[2] = 0x17;
2502         spec->automute = 1;
2503         spec->automute_mode = ALC_AUTOMUTE_AMP;
2504 }
2505
2506 static void alc888_acer_aspire_6530g_setup(struct hda_codec *codec)
2507 {
2508         struct alc_spec *spec = codec->spec;
2509
2510         spec->autocfg.hp_pins[0] = 0x15;
2511         spec->autocfg.speaker_pins[0] = 0x14;
2512         spec->autocfg.speaker_pins[1] = 0x16;
2513         spec->autocfg.speaker_pins[2] = 0x17;
2514         spec->automute = 1;
2515         spec->automute_mode = ALC_AUTOMUTE_AMP;
2516 }
2517
2518 static void alc888_acer_aspire_7730g_setup(struct hda_codec *codec)
2519 {
2520         struct alc_spec *spec = codec->spec;
2521
2522         spec->autocfg.hp_pins[0] = 0x15;
2523         spec->autocfg.speaker_pins[0] = 0x14;
2524         spec->autocfg.speaker_pins[1] = 0x16;
2525         spec->autocfg.speaker_pins[2] = 0x17;
2526         spec->automute = 1;
2527         spec->automute_mode = ALC_AUTOMUTE_AMP;
2528 }
2529
2530 static void alc889_acer_aspire_8930g_setup(struct hda_codec *codec)
2531 {
2532         struct alc_spec *spec = codec->spec;
2533
2534         spec->autocfg.hp_pins[0] = 0x15;
2535         spec->autocfg.speaker_pins[0] = 0x14;
2536         spec->autocfg.speaker_pins[1] = 0x16;
2537         spec->autocfg.speaker_pins[2] = 0x1b;
2538         spec->automute = 1;
2539         spec->automute_mode = ALC_AUTOMUTE_AMP;
2540 }
2541
2542 /*
2543  * ALC880 3-stack model
2544  *
2545  * DAC: Front = 0x02 (0x0c), Surr = 0x05 (0x0f), CLFE = 0x04 (0x0e)
2546  * Pin assignment: Front = 0x14, Line-In/Surr = 0x1a, Mic/CLFE = 0x18,
2547  *                 F-Mic = 0x1b, HP = 0x19
2548  */
2549
2550 static hda_nid_t alc880_dac_nids[4] = {
2551         /* front, rear, clfe, rear_surr */
2552         0x02, 0x05, 0x04, 0x03
2553 };
2554
2555 static hda_nid_t alc880_adc_nids[3] = {
2556         /* ADC0-2 */
2557         0x07, 0x08, 0x09,
2558 };
2559
2560 /* The datasheet says the node 0x07 is connected from inputs,
2561  * but it shows zero connection in the real implementation on some devices.
2562  * Note: this is a 915GAV bug, fixed on 915GLV
2563  */
2564 static hda_nid_t alc880_adc_nids_alt[2] = {
2565         /* ADC1-2 */
2566         0x08, 0x09,
2567 };
2568
2569 #define ALC880_DIGOUT_NID       0x06
2570 #define ALC880_DIGIN_NID        0x0a
2571
2572 static struct hda_input_mux alc880_capture_source = {
2573         .num_items = 4,
2574         .items = {
2575                 { "Mic", 0x0 },
2576                 { "Front Mic", 0x3 },
2577                 { "Line", 0x2 },
2578                 { "CD", 0x4 },
2579         },
2580 };
2581
2582 /* channel source setting (2/6 channel selection for 3-stack) */
2583 /* 2ch mode */
2584 static struct hda_verb alc880_threestack_ch2_init[] = {
2585         /* set line-in to input, mute it */
2586         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
2587         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2588         /* set mic-in to input vref 80%, mute it */
2589         { 0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
2590         { 0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2591         { } /* end */
2592 };
2593
2594 /* 6ch mode */
2595 static struct hda_verb alc880_threestack_ch6_init[] = {
2596         /* set line-in to output, unmute it */
2597         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2598         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2599         /* set mic-in to output, unmute it */
2600         { 0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2601         { 0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2602         { } /* end */
2603 };
2604
2605 static struct hda_channel_mode alc880_threestack_modes[2] = {
2606         { 2, alc880_threestack_ch2_init },
2607         { 6, alc880_threestack_ch6_init },
2608 };
2609
2610 static struct snd_kcontrol_new alc880_three_stack_mixer[] = {
2611         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
2612         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
2613         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
2614         HDA_BIND_MUTE("Surround Playback Switch", 0x0f, 2, HDA_INPUT),
2615         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
2616         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
2617         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
2618         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
2619         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
2620         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
2621         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
2622         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
2623         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
2624         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
2625         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x0b, 0x3, HDA_INPUT),
2626         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x0b, 0x3, HDA_INPUT),
2627         HDA_CODEC_MUTE("Headphone Playback Switch", 0x19, 0x0, HDA_OUTPUT),
2628         {
2629                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2630                 .name = "Channel Mode",
2631                 .info = alc_ch_mode_info,
2632                 .get = alc_ch_mode_get,
2633                 .put = alc_ch_mode_put,
2634         },
2635         { } /* end */
2636 };
2637
2638 /* capture mixer elements */
2639 static int alc_cap_vol_info(struct snd_kcontrol *kcontrol,
2640                             struct snd_ctl_elem_info *uinfo)
2641 {
2642         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2643         struct alc_spec *spec = codec->spec;
2644         int err;
2645
2646         mutex_lock(&codec->control_mutex);
2647         kcontrol->private_value = HDA_COMPOSE_AMP_VAL(spec->adc_nids[0], 3, 0,
2648                                                       HDA_INPUT);
2649         err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
2650         mutex_unlock(&codec->control_mutex);
2651         return err;
2652 }
2653
2654 static int alc_cap_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2655                            unsigned int size, unsigned int __user *tlv)
2656 {
2657         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2658         struct alc_spec *spec = codec->spec;
2659         int err;
2660
2661         mutex_lock(&codec->control_mutex);
2662         kcontrol->private_value = HDA_COMPOSE_AMP_VAL(spec->adc_nids[0], 3, 0,
2663                                                       HDA_INPUT);
2664         err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
2665         mutex_unlock(&codec->control_mutex);
2666         return err;
2667 }
2668
2669 typedef int (*getput_call_t)(struct snd_kcontrol *kcontrol,
2670                              struct snd_ctl_elem_value *ucontrol);
2671
2672 static int alc_cap_getput_caller(struct snd_kcontrol *kcontrol,
2673                                  struct snd_ctl_elem_value *ucontrol,
2674                                  getput_call_t func)
2675 {
2676         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2677         struct alc_spec *spec = codec->spec;
2678         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2679         int err;
2680
2681         mutex_lock(&codec->control_mutex);
2682         kcontrol->private_value = HDA_COMPOSE_AMP_VAL(spec->adc_nids[adc_idx],
2683                                                       3, 0, HDA_INPUT);
2684         err = func(kcontrol, ucontrol);
2685         mutex_unlock(&codec->control_mutex);
2686         return err;
2687 }
2688
2689 static int alc_cap_vol_get(struct snd_kcontrol *kcontrol,
2690                            struct snd_ctl_elem_value *ucontrol)
2691 {
2692         return alc_cap_getput_caller(kcontrol, ucontrol,
2693                                      snd_hda_mixer_amp_volume_get);
2694 }
2695
2696 static int alc_cap_vol_put(struct snd_kcontrol *kcontrol,
2697                            struct snd_ctl_elem_value *ucontrol)
2698 {
2699         return alc_cap_getput_caller(kcontrol, ucontrol,
2700                                      snd_hda_mixer_amp_volume_put);
2701 }
2702
2703 /* capture mixer elements */
2704 #define alc_cap_sw_info         snd_ctl_boolean_stereo_info
2705
2706 static int alc_cap_sw_get(struct snd_kcontrol *kcontrol,
2707                           struct snd_ctl_elem_value *ucontrol)
2708 {
2709         return alc_cap_getput_caller(kcontrol, ucontrol,
2710                                      snd_hda_mixer_amp_switch_get);
2711 }
2712
2713 static int alc_cap_sw_put(struct snd_kcontrol *kcontrol,
2714                           struct snd_ctl_elem_value *ucontrol)
2715 {
2716         return alc_cap_getput_caller(kcontrol, ucontrol,
2717                                      snd_hda_mixer_amp_switch_put);
2718 }
2719
2720 #define _DEFINE_CAPMIX(num) \
2721         { \
2722                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2723                 .name = "Capture Switch", \
2724                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
2725                 .count = num, \
2726                 .info = alc_cap_sw_info, \
2727                 .get = alc_cap_sw_get, \
2728                 .put = alc_cap_sw_put, \
2729         }, \
2730         { \
2731                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2732                 .name = "Capture Volume", \
2733                 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE | \
2734                            SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
2735                            SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK), \
2736                 .count = num, \
2737                 .info = alc_cap_vol_info, \
2738                 .get = alc_cap_vol_get, \
2739                 .put = alc_cap_vol_put, \
2740                 .tlv = { .c = alc_cap_vol_tlv }, \
2741         }
2742
2743 #define _DEFINE_CAPSRC(num) \
2744         { \
2745                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
2746                 /* .name = "Capture Source", */ \
2747                 .name = "Input Source", \
2748                 .count = num, \
2749                 .info = alc_mux_enum_info, \
2750                 .get = alc_mux_enum_get, \
2751                 .put = alc_mux_enum_put, \
2752         }
2753
2754 #define DEFINE_CAPMIX(num) \
2755 static struct snd_kcontrol_new alc_capture_mixer ## num[] = { \
2756         _DEFINE_CAPMIX(num),                                  \
2757         _DEFINE_CAPSRC(num),                                  \
2758         { } /* end */                                         \
2759 }
2760
2761 #define DEFINE_CAPMIX_NOSRC(num) \
2762 static struct snd_kcontrol_new alc_capture_mixer_nosrc ## num[] = { \
2763         _DEFINE_CAPMIX(num),                                        \
2764         { } /* end */                                               \
2765 }
2766
2767 /* up to three ADCs */
2768 DEFINE_CAPMIX(1);
2769 DEFINE_CAPMIX(2);
2770 DEFINE_CAPMIX(3);
2771 DEFINE_CAPMIX_NOSRC(1);
2772 DEFINE_CAPMIX_NOSRC(2);
2773 DEFINE_CAPMIX_NOSRC(3);
2774
2775 /*
2776  * ALC880 5-stack model
2777  *
2778  * DAC: Front = 0x02 (0x0c), Surr = 0x05 (0x0f), CLFE = 0x04 (0x0d),
2779  *      Side = 0x02 (0xd)
2780  * Pin assignment: Front = 0x14, Surr = 0x17, CLFE = 0x16
2781  *                 Line-In/Side = 0x1a, Mic = 0x18, F-Mic = 0x1b, HP = 0x19
2782  */
2783
2784 /* additional mixers to alc880_three_stack_mixer */
2785 static struct snd_kcontrol_new alc880_five_stack_mixer[] = {
2786         HDA_CODEC_VOLUME("Side Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
2787         HDA_BIND_MUTE("Side Playback Switch", 0x0d, 2, HDA_INPUT),
2788         { } /* end */
2789 };
2790
2791 /* channel source setting (6/8 channel selection for 5-stack) */
2792 /* 6ch mode */
2793 static struct hda_verb alc880_fivestack_ch6_init[] = {
2794         /* set line-in to input, mute it */
2795         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
2796         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2797         { } /* end */
2798 };
2799
2800 /* 8ch mode */
2801 static struct hda_verb alc880_fivestack_ch8_init[] = {
2802         /* set line-in to output, unmute it */
2803         { 0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
2804         { 0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE },
2805         { } /* end */
2806 };
2807
2808 static struct hda_channel_mode alc880_fivestack_modes[2] = {
2809         { 6, alc880_fivestack_ch6_init },
2810         { 8, alc880_fivestack_ch8_init },
2811 };
2812
2813
2814 /*
2815  * ALC880 6-stack model
2816  *
2817  * DAC: Front = 0x02 (0x0c), Surr = 0x03 (0x0d), CLFE = 0x04 (0x0e),
2818  *      Side = 0x05 (0x0f)
2819  * Pin assignment: Front = 0x14, Surr = 0x15, CLFE = 0x16, Side = 0x17,
2820  *   Mic = 0x18, F-Mic = 0x19, Line = 0x1a, HP = 0x1b
2821  */
2822
2823 static hda_nid_t alc880_6st_dac_nids[4] = {
2824         /* front, rear, clfe, rear_surr */
2825         0x02, 0x03, 0x04, 0x05
2826 };
2827
2828 static struct hda_input_mux alc880_6stack_capture_source = {
2829         .num_items = 4,
2830         .items = {
2831                 { "Mic", 0x0 },
2832                 { "Front Mic", 0x1 },
2833                 { "Line", 0x2 },
2834                 { "CD", 0x4 },
2835         },
2836 };
2837
2838 /* fixed 8-channels */
2839 static struct hda_channel_mode alc880_sixstack_modes[1] = {
2840         { 8, NULL },
2841 };
2842
2843 static struct snd_kcontrol_new alc880_six_stack_mixer[] = {
2844         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
2845         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
2846         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
2847         HDA_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
2848         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
2849         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
2850         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
2851         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
2852         HDA_CODEC_VOLUME("Side Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
2853         HDA_BIND_MUTE("Side Playback Switch", 0x0f, 2, HDA_INPUT),
2854         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
2855         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
2856         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
2857         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
2858         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
2859         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
2860         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x0b, 0x1, HDA_INPUT),
2861         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x0b, 0x1, HDA_INPUT),
2862         {
2863                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2864                 .name = "Channel Mode",
2865                 .info = alc_ch_mode_info,
2866                 .get = alc_ch_mode_get,
2867                 .put = alc_ch_mode_put,
2868         },
2869         { } /* end */
2870 };
2871
2872
2873 /*
2874  * ALC880 W810 model
2875  *
2876  * W810 has rear IO for:
2877  * Front (DAC 02)
2878  * Surround (DAC 03)
2879  * Center/LFE (DAC 04)
2880  * Digital out (06)
2881  *
2882  * The system also has a pair of internal speakers, and a headphone jack.
2883  * These are both connected to Line2 on the codec, hence to DAC 02.
2884  *
2885  * There is a variable resistor to control the speaker or headphone
2886  * volume. This is a hardware-only device without a software API.
2887  *
2888  * Plugging headphones in will disable the internal speakers. This is
2889  * implemented in hardware, not via the driver using jack sense. In
2890  * a similar fashion, plugging into the rear socket marked "front" will
2891  * disable both the speakers and headphones.
2892  *
2893  * For input, there's a microphone jack, and an "audio in" jack.
2894  * These may not do anything useful with this driver yet, because I
2895  * haven't setup any initialization verbs for these yet...
2896  */
2897
2898 static hda_nid_t alc880_w810_dac_nids[3] = {
2899         /* front, rear/surround, clfe */
2900         0x02, 0x03, 0x04
2901 };
2902
2903 /* fixed 6 channels */
2904 static struct hda_channel_mode alc880_w810_modes[1] = {
2905         { 6, NULL }
2906 };
2907
2908 /* Pin assignment: Front = 0x14, Surr = 0x15, CLFE = 0x16, HP = 0x1b */
2909 static struct snd_kcontrol_new alc880_w810_base_mixer[] = {
2910         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
2911         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
2912         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
2913         HDA_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
2914         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
2915         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
2916         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
2917         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
2918         HDA_CODEC_MUTE("Headphone Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
2919         { } /* end */
2920 };
2921
2922
2923 /*
2924  * Z710V model
2925  *
2926  * DAC: Front = 0x02 (0x0c), HP = 0x03 (0x0d)
2927  * Pin assignment: Front = 0x14, HP = 0x15, Mic = 0x18, Mic2 = 0x19(?),
2928  *                 Line = 0x1a
2929  */
2930
2931 static hda_nid_t alc880_z71v_dac_nids[1] = {
2932         0x02
2933 };
2934 #define ALC880_Z71V_HP_DAC      0x03
2935
2936 /* fixed 2 channels */
2937 static struct hda_channel_mode alc880_2_jack_modes[1] = {
2938         { 2, NULL }
2939 };
2940
2941 static struct snd_kcontrol_new alc880_z71v_mixer[] = {
2942         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
2943         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
2944         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
2945         HDA_BIND_MUTE("Headphone Playback Switch", 0x0d, 2, HDA_INPUT),
2946         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
2947         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
2948         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
2949         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
2950         { } /* end */
2951 };
2952
2953
2954 /*
2955  * ALC880 F1734 model
2956  *
2957  * DAC: HP = 0x02 (0x0c), Front = 0x03 (0x0d)
2958  * Pin assignment: HP = 0x14, Front = 0x15, Mic = 0x18
2959  */
2960
2961 static hda_nid_t alc880_f1734_dac_nids[1] = {
2962         0x03
2963 };
2964 #define ALC880_F1734_HP_DAC     0x02
2965
2966 static struct snd_kcontrol_new alc880_f1734_mixer[] = {
2967         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
2968         HDA_BIND_MUTE("Headphone Playback Switch", 0x0c, 2, HDA_INPUT),
2969         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
2970         HDA_BIND_MUTE("Speaker Playback Switch", 0x0d, 2, HDA_INPUT),
2971         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
2972         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
2973         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x1, HDA_INPUT),
2974         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x1, HDA_INPUT),
2975         { } /* end */
2976 };
2977
2978 static struct hda_input_mux alc880_f1734_capture_source = {
2979         .num_items = 2,
2980         .items = {
2981                 { "Mic", 0x1 },
2982                 { "CD", 0x4 },
2983         },
2984 };
2985
2986
2987 /*
2988  * ALC880 ASUS model
2989  *
2990  * DAC: HP/Front = 0x02 (0x0c), Surr = 0x03 (0x0d), CLFE = 0x04 (0x0e)
2991  * Pin assignment: HP/Front = 0x14, Surr = 0x15, CLFE = 0x16,
2992  *  Mic = 0x18, Line = 0x1a
2993  */
2994
2995 #define alc880_asus_dac_nids    alc880_w810_dac_nids    /* identical with w810 */
2996 #define alc880_asus_modes       alc880_threestack_modes /* 2/6 channel mode */
2997
2998 static struct snd_kcontrol_new alc880_asus_mixer[] = {
2999         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
3000         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
3001         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
3002         HDA_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
3003         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
3004         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
3005         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
3006         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
3007         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
3008         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
3009         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
3010         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
3011         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
3012         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
3013         {
3014                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3015                 .name = "Channel Mode",
3016                 .info = alc_ch_mode_info,
3017                 .get = alc_ch_mode_get,
3018                 .put = alc_ch_mode_put,
3019         },
3020         { } /* end */
3021 };
3022
3023 /*
3024  * ALC880 ASUS W1V model
3025  *
3026  * DAC: HP/Front = 0x02 (0x0c), Surr = 0x03 (0x0d), CLFE = 0x04 (0x0e)
3027  * Pin assignment: HP/Front = 0x14, Surr = 0x15, CLFE = 0x16,
3028  *  Mic = 0x18, Line = 0x1a, Line2 = 0x1b
3029  */
3030
3031 /* additional mixers to alc880_asus_mixer */
3032 static struct snd_kcontrol_new alc880_asus_w1v_mixer[] = {
3033         HDA_CODEC_VOLUME("Line2 Playback Volume", 0x0b, 0x03, HDA_INPUT),
3034         HDA_CODEC_MUTE("Line2 Playback Switch", 0x0b, 0x03, HDA_INPUT),
3035         { } /* end */
3036 };
3037
3038 /* TCL S700 */
3039 static struct snd_kcontrol_new alc880_tcl_s700_mixer[] = {
3040         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
3041         HDA_CODEC_MUTE("Front Playback Switch", 0x1b, 0x0, HDA_OUTPUT),
3042         HDA_CODEC_MUTE("Headphone Playback Switch", 0x14, 0x0, HDA_OUTPUT),
3043         HDA_CODEC_VOLUME("CD Playback Volume", 0x0B, 0x04, HDA_INPUT),
3044         HDA_CODEC_MUTE("CD Playback Switch", 0x0B, 0x04, HDA_INPUT),
3045         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0B, 0x0, HDA_INPUT),
3046         HDA_CODEC_MUTE("Mic Playback Switch", 0x0B, 0x0, HDA_INPUT),
3047         HDA_CODEC_VOLUME("Capture Volume", 0x08, 0x0, HDA_INPUT),
3048         HDA_CODEC_MUTE("Capture Switch", 0x08, 0x0, HDA_INPUT),
3049         { } /* end */
3050 };
3051
3052 /* Uniwill */
3053 static struct snd_kcontrol_new alc880_uniwill_mixer[] = {
3054         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
3055         HDA_BIND_MUTE("Headphone Playback Switch", 0x0c, 2, HDA_INPUT),
3056         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
3057         HDA_BIND_MUTE("Speaker Playback Switch", 0x0d, 2, HDA_INPUT),
3058         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
3059         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
3060         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
3061         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
3062         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
3063         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
3064         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
3065         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
3066         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
3067         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
3068         HDA_CODEC_VOLUME("Front Mic Playback Volume", 0x0b, 0x1, HDA_INPUT),
3069         HDA_CODEC_MUTE("Front Mic Playback Switch", 0x0b, 0x1, HDA_INPUT),
3070         {
3071                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3072                 .name = "Channel Mode",
3073                 .info = alc_ch_mode_info,
3074                 .get = alc_ch_mode_get,
3075                 .put = alc_ch_mode_put,
3076         },
3077         { } /* end */
3078 };
3079
3080 static struct snd_kcontrol_new alc880_fujitsu_mixer[] = {
3081         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
3082         HDA_BIND_MUTE("Headphone Playback Switch", 0x0c, 2, HDA_INPUT),
3083         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
3084         HDA_BIND_MUTE("Speaker Playback Switch", 0x0d, 2, HDA_INPUT),
3085         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x04, HDA_INPUT),
3086         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x04, HDA_INPUT),
3087         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
3088         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
3089         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x0b, 0x1, HDA_INPUT),
3090         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x0b, 0x1, HDA_INPUT),
3091         { } /* end */
3092 };
3093
3094 static struct snd_kcontrol_new alc880_uniwill_p53_mixer[] = {
3095         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
3096         HDA_BIND_MUTE("Headphone Playback Switch", 0x0c, 2, HDA_INPUT),
3097         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
3098         HDA_BIND_MUTE("Speaker Playback Switch", 0x0d, 2, HDA_INPUT),
3099         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
3100         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
3101         { } /* end */
3102 };
3103
3104 /*
3105  * virtual master controls
3106  */
3107
3108 /*
3109  * slave controls for virtual master
3110  */
3111 static const char * const alc_slave_vols[] = {
3112         "Front Playback Volume",
3113         "Surround Playback Volume",
3114         "Center Playback Volume",
3115         "LFE Playback Volume",
3116         "Side Playback Volume",
3117         "Headphone Playback Volume",
3118         "Speaker Playback Volume",
3119         "Mono Playback Volume",
3120         "Line-Out Playback Volume",
3121         "PCM Playback Volume",
3122         NULL,
3123 };
3124
3125 static const char * const alc_slave_sws[] = {
3126         "Front Playback Switch",
3127         "Surround Playback Switch",
3128         "Center Playback Switch",
3129         "LFE Playback Switch",
3130         "Side Playback Switch",
3131         "Headphone Playback Switch",
3132         "Speaker Playback Switch",
3133         "Mono Playback Switch",
3134         "IEC958 Playback Switch",
3135         "Line-Out Playback Switch",
3136         "PCM Playback Switch",
3137         NULL,
3138 };
3139
3140 /*
3141  * build control elements
3142  */
3143
3144 #define NID_MAPPING             (-1)
3145
3146 #define SUBDEV_SPEAKER_         (0 << 6)
3147 #define SUBDEV_HP_              (1 << 6)
3148 #define SUBDEV_LINE_            (2 << 6)
3149 #define SUBDEV_SPEAKER(x)       (SUBDEV_SPEAKER_ | ((x) & 0x3f))
3150 #define SUBDEV_HP(x)            (SUBDEV_HP_ | ((x) & 0x3f))
3151 #define SUBDEV_LINE(x)          (SUBDEV_LINE_ | ((x) & 0x3f))
3152
3153 static void alc_free_kctls(struct hda_codec *codec);
3154
3155 #ifdef CONFIG_SND_HDA_INPUT_BEEP
3156 /* additional beep mixers; the actual parameters are overwritten at build */
3157 static struct snd_kcontrol_new alc_beep_mixer[] = {
3158         HDA_CODEC_VOLUME("Beep Playback Volume", 0, 0, HDA_INPUT),
3159         HDA_CODEC_MUTE_BEEP("Beep Playback Switch", 0, 0, HDA_INPUT),
3160         { } /* end */
3161 };
3162 #endif
3163
3164 static int alc_build_controls(struct hda_codec *codec)
3165 {
3166         struct alc_spec *spec = codec->spec;
3167         struct snd_kcontrol *kctl = NULL;
3168         struct snd_kcontrol_new *knew;
3169         int i, j, err;
3170         unsigned int u;
3171         hda_nid_t nid;
3172
3173         for (i = 0; i < spec->num_mixers; i++) {
3174                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
3175                 if (err < 0)
3176                         return err;
3177         }
3178         if (spec->cap_mixer) {
3179                 err = snd_hda_add_new_ctls(codec, spec->cap_mixer);
3180                 if (err < 0)
3181                         return err;
3182         }
3183         if (spec->multiout.dig_out_nid) {
3184                 err = snd_hda_create_spdif_out_ctls(codec,
3185                                                     spec->multiout.dig_out_nid);
3186                 if (err < 0)
3187                         return err;
3188                 if (!spec->no_analog) {
3189                         err = snd_hda_create_spdif_share_sw(codec,
3190                                                             &spec->multiout);
3191                         if (err < 0)
3192                                 return err;
3193                         spec->multiout.share_spdif = 1;
3194                 }
3195         }
3196         if (spec->dig_in_nid) {
3197                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3198                 if (err < 0)
3199                         return err;
3200         }
3201
3202 #ifdef CONFIG_SND_HDA_INPUT_BEEP
3203         /* create beep controls if needed */
3204         if (spec->beep_amp) {
3205                 struct snd_kcontrol_new *knew;
3206                 for (knew = alc_beep_mixer; knew->name; knew++) {
3207                         struct snd_kcontrol *kctl;
3208                         kctl = snd_ctl_new1(knew, codec);
3209                         if (!kctl)
3210                                 return -ENOMEM;
3211                         kctl->private_value = spec->beep_amp;
3212                         err = snd_hda_ctl_add(codec, 0, kctl);
3213                         if (err < 0)
3214                                 return err;
3215                 }
3216         }
3217 #endif
3218
3219         /* if we have no master control, let's create it */
3220         if (!spec->no_analog &&
3221             !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3222                 unsigned int vmaster_tlv[4];
3223                 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3224                                         HDA_OUTPUT, vmaster_tlv);
3225                 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3226                                           vmaster_tlv, alc_slave_vols);
3227                 if (err < 0)
3228                         return err;
3229         }
3230         if (!spec->no_analog &&
3231             !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3232                 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
3233                                           NULL, alc_slave_sws);
3234                 if (err < 0)
3235                         return err;
3236         }
3237
3238         /* assign Capture Source enums to NID */
3239         if (spec->capsrc_nids || spec->adc_nids) {
3240                 kctl = snd_hda_find_mixer_ctl(codec, "Capture Source");
3241                 if (!kctl)
3242                         kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
3243                 for (i = 0; kctl && i < kctl->count; i++) {
3244                         hda_nid_t *nids = spec->capsrc_nids;
3245                         if (!nids)
3246                                 nids = spec->adc_nids;
3247                         err = snd_hda_add_nid(codec, kctl, i, nids[i]);
3248                         if (err < 0)
3249                                 return err;
3250                 }
3251         }
3252         if (spec->cap_mixer) {
3253                 const char *kname = kctl ? kctl->id.name : NULL;
3254                 for (knew = spec->cap_mixer; knew->name; knew++) {
3255                         if (kname && strcmp(knew->name, kname) == 0)
3256                                 continue;
3257                         kctl = snd_hda_find_mixer_ctl(codec, knew->name);
3258                         for (i = 0; kctl && i < kctl->count; i++) {
3259                                 err = snd_hda_add_nid(codec, kctl, i,
3260                                                       spec->adc_nids[i]);
3261                                 if (err < 0)
3262                                         return err;
3263                         }
3264                 }
3265         }
3266
3267         /* other nid->control mapping */
3268         for (i = 0; i < spec->num_mixers; i++) {
3269                 for (knew = spec->mixers[i]; knew->name; knew++) {
3270                         if (knew->iface != NID_MAPPING)
3271                                 continue;
3272                         kctl = snd_hda_find_mixer_ctl(codec, knew->name);
3273                         if (kctl == NULL)
3274                                 continue;
3275                         u = knew->subdevice;
3276                         for (j = 0; j < 4; j++, u >>= 8) {
3277                                 nid = u & 0x3f;
3278                                 if (nid == 0)
3279                                         continue;
3280                                 switch (u & 0xc0) {
3281                                 case SUBDEV_SPEAKER_:
3282                                         nid = spec->autocfg.speaker_pins[nid];
3283                                         break;
3284                                 case SUBDEV_LINE_:
3285                                         nid = spec->autocfg.line_out_pins[nid];
3286                                         break;
3287                                 case SUBDEV_HP_:
3288                                         nid = spec->autocfg.hp_pins[nid];
3289                                         break;
3290                                 default:
3291                                         continue;
3292                                 }
3293                                 err = snd_hda_add_nid(codec, kctl, 0, nid);
3294                                 if (err < 0)
3295                                         return err;
3296                         }
3297                         u = knew->private_value;
3298                         for (j = 0; j < 4; j++, u >>= 8) {
3299                                 nid = u & 0xff;
3300                                 if (nid == 0)
3301                                         continue;
3302                                 err = snd_hda_add_nid(codec, kctl, 0, nid);
3303                                 if (err < 0)
3304                                         return err;
3305                         }
3306                 }
3307         }
3308
3309         alc_free_kctls(codec); /* no longer needed */
3310
3311         return 0;
3312 }
3313
3314
3315 /*
3316  * initialize the codec volumes, etc
3317  */
3318
3319 /*
3320  * generic initialization of ADC, input mixers and output mixers
3321  */
3322 static struct hda_verb alc880_volume_init_verbs[] = {
3323         /*
3324          * Unmute ADC0-2 and set the default input to mic-in
3325          */
3326         {0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
3327         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3328         {0x08, AC_VERB_SET_CONNECT_SEL, 0x00},
3329         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3330         {0x09, AC_VERB_SET_CONNECT_SEL, 0x00},
3331         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3332
3333         /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
3334          * mixer widget
3335          * Note: PASD motherboards uses the Line In 2 as the input for front
3336          * panel mic (mic 2)
3337          */
3338         /* Amp Indices: Mic1 = 0, Mic2 = 1, Line1 = 2, Line2 = 3, CD = 4 */
3339         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3340         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3341         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
3342         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
3343         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
3344         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
3345         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
3346
3347         /*
3348          * Set up output mixers (0x0c - 0x0f)
3349          */
3350         /* set vol=0 to output mixers */
3351         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3352         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3353         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3354         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
3355         /* set up input amps for analog loopback */
3356         /* Amp Indices: DAC = 0, mixer = 1 */
3357         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3358         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3359         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3360         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3361         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3362         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3363         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
3364         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
3365
3366         { }
3367 };
3368
3369 /*
3370  * 3-stack pin configuration:
3371  * front = 0x14, mic/clfe = 0x18, HP = 0x19, line/surr = 0x1a, f-mic = 0x1b
3372  */
3373 static struct hda_verb alc880_pin_3stack_init_verbs[] = {
3374         /*
3375          * preset connection lists of input pins
3376          * 0 = front, 1 = rear_surr, 2 = CLFE, 3 = surround
3377          */
3378         {0x10, AC_VERB_SET_CONNECT_SEL, 0x02}, /* mic/clfe */
3379         {0x11, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
3380         {0x12, AC_VERB_SET_CONNECT_SEL, 0x03}, /* line/surround */
3381
3382         /*
3383          * Set pin mode and muting
3384          */
3385         /* set front pin widgets 0x14 for output */
3386         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3387         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3388         /* Mic1 (rear panel) pin widget for input and vref at 80% */
3389         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3390         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3391         /* Mic2 (as headphone out) for HP output */
3392         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3393         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3394         /* Line In pin widget for input */
3395         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3396         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3397         /* Line2 (as front mic) pin widget for input and vref at 80% */
3398         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3399         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3400         /* CD pin widget for input */
3401         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3402
3403         { }
3404 };
3405
3406 /*
3407  * 5-stack pin configuration:
3408  * front = 0x14, surround = 0x17, clfe = 0x16, mic = 0x18, HP = 0x19,
3409  * line-in/side = 0x1a, f-mic = 0x1b
3410  */
3411 static struct hda_verb alc880_pin_5stack_init_verbs[] = {
3412         /*
3413          * preset connection lists of input pins
3414          * 0 = front, 1 = rear_surr, 2 = CLFE, 3 = surround
3415          */
3416         {0x11, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
3417         {0x12, AC_VERB_SET_CONNECT_SEL, 0x01}, /* line/side */
3418
3419         /*
3420          * Set pin mode and muting
3421          */
3422         /* set pin widgets 0x14-0x17 for output */
3423         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3424         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3425         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3426         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3427         /* unmute pins for output (no gain on this amp) */
3428         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3429         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3430         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3431         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3432
3433         /* Mic1 (rear panel) pin widget for input and vref at 80% */
3434         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3435         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3436         /* Mic2 (as headphone out) for HP output */
3437         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3438         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3439         /* Line In pin widget for input */
3440         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3441         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3442         /* Line2 (as front mic) pin widget for input and vref at 80% */
3443         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3444         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3445         /* CD pin widget for input */
3446         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3447
3448         { }
3449 };
3450
3451 /*
3452  * W810 pin configuration:
3453  * front = 0x14, surround = 0x15, clfe = 0x16, HP = 0x1b
3454  */
3455 static struct hda_verb alc880_pin_w810_init_verbs[] = {
3456         /* hphone/speaker input selector: front DAC */
3457         {0x13, AC_VERB_SET_CONNECT_SEL, 0x0},
3458
3459         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3460         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3461         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3462         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3463         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3464         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3465
3466         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3467         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3468
3469         { }
3470 };
3471
3472 /*
3473  * Z71V pin configuration:
3474  * Speaker-out = 0x14, HP = 0x15, Mic = 0x18, Line-in = 0x1a, Mic2 = 0x1b (?)
3475  */
3476 static struct hda_verb alc880_pin_z71v_init_verbs[] = {
3477         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3478         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3479         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3480         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3481
3482         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3483         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3484         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3485         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3486
3487         { }
3488 };
3489
3490 /*
3491  * 6-stack pin configuration:
3492  * front = 0x14, surr = 0x15, clfe = 0x16, side = 0x17, mic = 0x18,
3493  * f-mic = 0x19, line = 0x1a, HP = 0x1b
3494  */
3495 static struct hda_verb alc880_pin_6stack_init_verbs[] = {
3496         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
3497
3498         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3499         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3500         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3501         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3502         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3503         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3504         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3505         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3506
3507         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3508         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3509         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3510         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3511         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3512         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3513         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3514         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3515         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3516
3517         { }
3518 };
3519
3520 /*
3521  * Uniwill pin configuration:
3522  * HP = 0x14, InternalSpeaker = 0x15, mic = 0x18, internal mic = 0x19,
3523  * line = 0x1a
3524  */
3525 static struct hda_verb alc880_uniwill_init_verbs[] = {
3526         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
3527
3528         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3529         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3530         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3531         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3532         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3533         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3534         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3535         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3536         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
3537         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
3538         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
3539         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
3540         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
3541         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
3542
3543         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3544         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3545         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3546         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3547         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3548         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3549         /* {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP}, */
3550         /* {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, */
3551         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3552
3553         {0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | ALC880_HP_EVENT},
3554         {0x18, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | ALC880_MIC_EVENT},
3555
3556         { }
3557 };
3558
3559 /*
3560 * Uniwill P53
3561 * HP = 0x14, InternalSpeaker = 0x15, mic = 0x19,
3562  */
3563 static struct hda_verb alc880_uniwill_p53_init_verbs[] = {
3564         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
3565
3566         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3567         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3568         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3569         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3570         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3571         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3572         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
3573         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
3574         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
3575         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
3576         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x00 << 8))},
3577         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, (0x7000 | (0x01 << 8))},
3578
3579         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3580         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3581         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3582         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3583         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3584         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3585
3586         {0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | ALC880_HP_EVENT},
3587         {0x21, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | ALC880_DCVOL_EVENT},
3588
3589         { }
3590 };
3591
3592 static struct hda_verb alc880_beep_init_verbs[] = {
3593         { 0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(5) },
3594         { }
3595 };
3596
3597 /* auto-toggle front mic */
3598 static void alc88x_simple_mic_automute(struct hda_codec *codec)
3599 {
3600         unsigned int present;
3601         unsigned char bits;
3602
3603         present = snd_hda_jack_detect(codec, 0x18);
3604         bits = present ? HDA_AMP_MUTE : 0;
3605         snd_hda_codec_amp_stereo(codec, 0x0b, HDA_INPUT, 1, HDA_AMP_MUTE, bits);
3606 }
3607
3608 static void alc880_uniwill_setup(struct hda_codec *codec)
3609 {
3610         struct alc_spec *spec = codec->spec;
3611
3612         spec->autocfg.hp_pins[0] = 0x14;
3613         spec->autocfg.speaker_pins[0] = 0x15;
3614         spec->autocfg.speaker_pins[0] = 0x16;
3615         spec->automute = 1;
3616         spec->automute_mode = ALC_AUTOMUTE_AMP;
3617 }
3618
3619 static void alc880_uniwill_init_hook(struct hda_codec *codec)
3620 {
3621         alc_hp_automute(codec);
3622         alc88x_simple_mic_automute(codec);
3623 }
3624
3625 static void alc880_uniwill_unsol_event(struct hda_codec *codec,
3626                                        unsigned int res)
3627 {
3628         /* Looks like the unsol event is incompatible with the standard
3629          * definition.  4bit tag is placed at 28 bit!
3630          */
3631         switch (res >> 28) {
3632         case ALC880_MIC_EVENT:
3633                 alc88x_simple_mic_automute(codec);
3634                 break;
3635         default:
3636                 alc_sku_unsol_event(codec, res);
3637                 break;
3638         }
3639 }
3640
3641 static void alc880_uniwill_p53_setup(struct hda_codec *codec)
3642 {
3643         struct alc_spec *spec = codec->spec;
3644
3645         spec->autocfg.hp_pins[0] = 0x14;
3646         spec->autocfg.speaker_pins[0] = 0x15;
3647         spec->automute = 1;
3648         spec->automute_mode = ALC_AUTOMUTE_AMP;
3649 }
3650
3651 static void alc880_uniwill_p53_dcvol_automute(struct hda_codec *codec)
3652 {
3653         unsigned int present;
3654
3655         present = snd_hda_codec_read(codec, 0x21, 0,
3656                                      AC_VERB_GET_VOLUME_KNOB_CONTROL, 0);
3657         present &= HDA_AMP_VOLMASK;
3658         snd_hda_codec_amp_stereo(codec, 0x0c, HDA_OUTPUT, 0,
3659                                  HDA_AMP_VOLMASK, present);
3660         snd_hda_codec_amp_stereo(codec, 0x0d, HDA_OUTPUT, 0,
3661                                  HDA_AMP_VOLMASK, present);
3662 }
3663
3664 static void alc880_uniwill_p53_unsol_event(struct hda_codec *codec,
3665                                            unsigned int res)
3666 {
3667         /* Looks like the unsol event is incompatible with the standard
3668          * definition.  4bit tag is placed at 28 bit!
3669          */
3670         if ((res >> 28) == ALC880_DCVOL_EVENT)
3671                 alc880_uniwill_p53_dcvol_automute(codec);
3672         else
3673                 alc_sku_unsol_event(codec, res);
3674 }
3675
3676 /*
3677  * F1734 pin configuration:
3678  * HP = 0x14, speaker-out = 0x15, mic = 0x18
3679  */
3680 static struct hda_verb alc880_pin_f1734_init_verbs[] = {
3681         {0x07, AC_VERB_SET_CONNECT_SEL, 0x01},
3682         {0x10, AC_VERB_SET_CONNECT_SEL, 0x02},
3683         {0x11, AC_VERB_SET_CONNECT_SEL, 0x00},
3684         {0x12, AC_VERB_SET_CONNECT_SEL, 0x01},
3685         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00},
3686
3687         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3688         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3689         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3690         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3691
3692         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3693         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3694         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF50},
3695         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3696         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3697         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3698         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3699         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3700         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3701
3702         {0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|ALC880_HP_EVENT},
3703         {0x21, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|ALC880_DCVOL_EVENT},
3704
3705         { }
3706 };
3707
3708 /*
3709  * ASUS pin configuration:
3710  * HP/front = 0x14, surr = 0x15, clfe = 0x16, mic = 0x18, line = 0x1a
3711  */
3712 static struct hda_verb alc880_pin_asus_init_verbs[] = {
3713         {0x10, AC_VERB_SET_CONNECT_SEL, 0x02},
3714         {0x11, AC_VERB_SET_CONNECT_SEL, 0x00},
3715         {0x12, AC_VERB_SET_CONNECT_SEL, 0x01},
3716         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00},
3717
3718         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3719         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3720         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3721         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3722         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3723         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3724         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3725         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3726
3727         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3728         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3729         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3730         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3731         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3732         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
3733         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3734         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3735         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3736
3737         { }
3738 };
3739
3740 /* Enable GPIO mask and set output */
3741 #define alc880_gpio1_init_verbs alc_gpio1_init_verbs
3742 #define alc880_gpio2_init_verbs alc_gpio2_init_verbs
3743 #define alc880_gpio3_init_verbs alc_gpio3_init_verbs
3744
3745 /* Clevo m520g init */
3746 static struct hda_verb alc880_pin_clevo_init_verbs[] = {
3747         /* headphone output */
3748         {0x11, AC_VERB_SET_CONNECT_SEL, 0x01},
3749         /* line-out */
3750         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3751         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3752         /* Line-in */
3753         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3754         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3755         /* CD */
3756         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3757         {0x1c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3758         /* Mic1 (rear panel) */
3759         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3760         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3761         /* Mic2 (front panel) */
3762         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3763         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3764         /* headphone */
3765         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3766         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3767         /* change to EAPD mode */
3768         {0x20, AC_VERB_SET_COEF_INDEX, 0x07},
3769         {0x20, AC_VERB_SET_PROC_COEF,  0x3060},
3770
3771         { }
3772 };
3773
3774 static struct hda_verb alc880_pin_tcl_S700_init_verbs[] = {
3775         /* change to EAPD mode */
3776         {0x20, AC_VERB_SET_COEF_INDEX, 0x07},
3777         {0x20, AC_VERB_SET_PROC_COEF,  0x3060},
3778
3779         /* Headphone output */
3780         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3781         /* Front output*/
3782         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
3783         {0x1b, AC_VERB_SET_CONNECT_SEL, 0x00},
3784
3785         /* Line In pin widget for input */
3786         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3787         /* CD pin widget for input */
3788         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3789         /* Mic1 (rear panel) pin widget for input and vref at 80% */
3790         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3791
3792         /* change to EAPD mode */
3793         {0x20, AC_VERB_SET_COEF_INDEX, 0x07},
3794         {0x20, AC_VERB_SET_PROC_COEF,  0x3070},
3795
3796         { }
3797 };
3798
3799 /*
3800  * LG m1 express dual
3801  *
3802  * Pin assignment:
3803  *   Rear Line-In/Out (blue): 0x14
3804  *   Build-in Mic-In: 0x15
3805  *   Speaker-out: 0x17
3806  *   HP-Out (green): 0x1b
3807  *   Mic-In/Out (red): 0x19
3808  *   SPDIF-Out: 0x1e
3809  */
3810
3811 /* To make 5.1 output working (green=Front, blue=Surr, red=CLFE) */
3812 static hda_nid_t alc880_lg_dac_nids[3] = {
3813         0x05, 0x02, 0x03
3814 };
3815
3816 /* seems analog CD is not working */
3817 static struct hda_input_mux alc880_lg_capture_source = {
3818         .num_items = 3,
3819         .items = {
3820                 { "Mic", 0x1 },
3821                 { "Line", 0x5 },
3822                 { "Internal Mic", 0x6 },
3823         },
3824 };
3825
3826 /* 2,4,6 channel modes */
3827 static struct hda_verb alc880_lg_ch2_init[] = {
3828         /* set line-in and mic-in to input */
3829         { 0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
3830         { 0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
3831         { }
3832 };
3833
3834 static struct hda_verb alc880_lg_ch4_init[] = {
3835         /* set line-in to out and mic-in to input */
3836         { 0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
3837         { 0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
3838         { }
3839 };
3840
3841 static struct hda_verb alc880_lg_ch6_init[] = {
3842         /* set line-in and mic-in to output */
3843         { 0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
3844         { 0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
3845         { }
3846 };
3847
3848 static struct hda_channel_mode alc880_lg_ch_modes[3] = {
3849         { 2, alc880_lg_ch2_init },
3850         { 4, alc880_lg_ch4_init },
3851         { 6, alc880_lg_ch6_init },
3852 };
3853
3854 static struct snd_kcontrol_new alc880_lg_mixer[] = {
3855         HDA_CODEC_VOLUME("Front Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
3856         HDA_BIND_MUTE("Front Playback Switch", 0x0f, 2, HDA_INPUT),
3857         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
3858         HDA_BIND_MUTE("Surround Playback Switch", 0x0c, 2, HDA_INPUT),
3859         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0d, 1, 0x0, HDA_OUTPUT),
3860         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0d, 2, 0x0, HDA_OUTPUT),
3861         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0d, 1, 2, HDA_INPUT),
3862         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0d, 2, 2, HDA_INPUT),
3863         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x1, HDA_INPUT),
3864         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x1, HDA_INPUT),
3865         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x06, HDA_INPUT),
3866         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x06, HDA_INPUT),
3867         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x0b, 0x07, HDA_INPUT),
3868         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x0b, 0x07, HDA_INPUT),
3869         {
3870                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3871                 .name = "Channel Mode",
3872                 .info = alc_ch_mode_info,
3873                 .get = alc_ch_mode_get,
3874                 .put = alc_ch_mode_put,
3875         },
3876         { } /* end */
3877 };
3878
3879 static struct hda_verb alc880_lg_init_verbs[] = {
3880         /* set capture source to mic-in */
3881         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3882         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3883         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
3884         /* mute all amp mixer inputs */
3885         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(5)},
3886         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)},
3887         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
3888         /* line-in to input */
3889         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
3890         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3891         /* built-in mic */
3892         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3893         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3894         /* speaker-out */
3895         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3896         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3897         /* mic-in to input */
3898         {0x11, AC_VERB_SET_CONNECT_SEL, 0x01},
3899         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3900         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3901         /* HP-out */
3902         {0x13, AC_VERB_SET_CONNECT_SEL, 0x03},
3903         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3904         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3905         /* jack sense */
3906         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | ALC880_HP_EVENT},
3907         { }
3908 };
3909
3910 /* toggle speaker-output according to the hp-jack state */
3911 static void alc880_lg_setup(struct hda_codec *codec)
3912 {
3913         struct alc_spec *spec = codec->spec;
3914
3915         spec->autocfg.hp_pins[0] = 0x1b;
3916         spec->autocfg.speaker_pins[0] = 0x17;
3917         spec->automute = 1;
3918         spec->automute_mode = ALC_AUTOMUTE_AMP;
3919 }
3920
3921 /*
3922  * LG LW20
3923  *
3924  * Pin assignment:
3925  *   Speaker-out: 0x14
3926  *   Mic-In: 0x18
3927  *   Built-in Mic-In: 0x19
3928  *   Line-In: 0x1b
3929  *   HP-Out: 0x1a
3930  *   SPDIF-Out: 0x1e
3931  */
3932
3933 static struct hda_input_mux alc880_lg_lw_capture_source = {
3934         .num_items = 3,
3935         .items = {
3936                 { "Mic", 0x0 },
3937                 { "Internal Mic", 0x1 },
3938                 { "Line In", 0x2 },
3939         },
3940 };
3941
3942 #define alc880_lg_lw_modes alc880_threestack_modes
3943
3944 static struct snd_kcontrol_new alc880_lg_lw_mixer[] = {
3945         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
3946         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
3947         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
3948         HDA_BIND_MUTE("Surround Playback Switch", 0x0f, 2, HDA_INPUT),
3949         HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x0e, 1, 0x0, HDA_OUTPUT),
3950         HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x0e, 2, 0x0, HDA_OUTPUT),
3951         HDA_BIND_MUTE_MONO("Center Playback Switch", 0x0e, 1, 2, HDA_INPUT),
3952         HDA_BIND_MUTE_MONO("LFE Playback Switch", 0x0e, 2, 2, HDA_INPUT),
3953         HDA_CODEC_VOLUME("Line Playback Volume", 0x0b, 0x02, HDA_INPUT),
3954         HDA_CODEC_MUTE("Line Playback Switch", 0x0b, 0x02, HDA_INPUT),
3955         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
3956         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
3957         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x0b, 0x01, HDA_INPUT),
3958         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x0b, 0x01, HDA_INPUT),
3959         {
3960                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3961                 .name = "Channel Mode",
3962                 .info = alc_ch_mode_info,
3963                 .get = alc_ch_mode_get,
3964                 .put = alc_ch_mode_put,
3965         },
3966         { } /* end */
3967 };
3968
3969 static struct hda_verb alc880_lg_lw_init_verbs[] = {
3970         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
3971         {0x10, AC_VERB_SET_CONNECT_SEL, 0x02}, /* mic/clfe */
3972         {0x12, AC_VERB_SET_CONNECT_SEL, 0x03}, /* line/surround */
3973
3974         /* set capture source to mic-in */
3975         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3976         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3977         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
3978         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)},
3979         /* speaker-out */
3980         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3981         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3982         /* HP-out */
3983         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
3984         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3985         /* mic-in to input */
3986         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3987         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3988         /* built-in mic */
3989         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
3990         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
3991         /* jack sense */
3992         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | ALC880_HP_EVENT},
3993         { }
3994 };
3995
3996 /* toggle speaker-output according to the hp-jack state */
3997 static void alc880_lg_lw_setup(struct hda_codec *codec)
3998 {
3999         struct alc_spec *spec = codec->spec;
4000
4001         spec->autocfg.hp_pins[0] = 0x1b;
4002         spec->autocfg.speaker_pins[0] = 0x14;
4003         spec->automute = 1;
4004         spec->automute_mode = ALC_AUTOMUTE_AMP;
4005 }
4006
4007 static struct snd_kcontrol_new alc880_medion_rim_mixer[] = {
4008         HDA_CODEC_VOLUME("Master Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
4009         HDA_BIND_MUTE("Master Playback Switch", 0x0c, 2, HDA_INPUT),
4010         HDA_CODEC_VOLUME("Mic Playback Volume", 0x0b, 0x0, HDA_INPUT),
4011         HDA_CODEC_MUTE("Mic Playback Switch", 0x0b, 0x0, HDA_INPUT),
4012         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x0b, 0x1, HDA_INPUT),
4013         HDA_CODEC_MUTE("Internal Playback Switch", 0x0b, 0x1, HDA_INPUT),
4014         { } /* end */
4015 };
4016
4017 static struct hda_input_mux alc880_medion_rim_capture_source = {
4018         .num_items = 2,
4019         .items = {
4020                 { "Mic", 0x0 },
4021                 { "Internal Mic", 0x1 },
4022         },
4023 };
4024
4025 static struct hda_verb alc880_medion_rim_init_verbs[] = {
4026         {0x13, AC_VERB_SET_CONNECT_SEL, 0x00}, /* HP */
4027
4028         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
4029         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4030
4031         /* Mic1 (rear panel) pin widget for input and vref at 80% */
4032         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
4033         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4034         /* Mic2 (as headphone out) for HP output */
4035         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
4036         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4037         /* Internal Speaker */
4038         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
4039         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4040
4041         {0x20, AC_VERB_SET_COEF_INDEX, 0x07},
4042         {0x20, AC_VERB_SET_PROC_COEF,  0x3060},
4043
4044         {0x14, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | ALC880_HP_EVENT},
4045         { }
4046 };
4047
4048 /* toggle speaker-output according to the hp-jack state */
4049 static void alc880_medion_rim_automute(struct hda_codec *codec)
4050 {
4051         struct alc_spec *spec = codec->spec;
4052         alc_hp_automute(codec);
4053         /* toggle EAPD */
4054         if (spec->jack_present)
4055                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA, 0);
4056         else
4057                 snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA, 2);
4058 }
4059
4060 static void alc880_medion_rim_unsol_event(struct hda_codec *codec,
4061                                           unsigned int res)
4062 {
4063         /* Looks like the unsol event is incompatible with the standard
4064          * definition.  4bit tag is placed at 28 bit!
4065          */
4066         if ((res >> 28) == ALC880_HP_EVENT)
4067                 alc880_medion_rim_automute(codec);
4068 }
4069
4070 static void alc880_medion_rim_setup(struct hda_codec *codec)
4071 {
4072         struct alc_spec *spec = codec->spec;
4073
4074         spec->autocfg.hp_pins[0] = 0x14;
4075         spec->autocfg.speaker_pins[0] = 0x1b;
4076         spec->automute = 1;
4077         spec->automute_mode = ALC_AUTOMUTE_AMP;
4078 }
4079
4080 #ifdef CONFIG_SND_HDA_POWER_SAVE
4081 static struct hda_amp_list alc880_loopbacks[] = {
4082         { 0x0b, HDA_INPUT, 0 },
4083         { 0x0b, HDA_INPUT, 1 },
4084         { 0x0b, HDA_INPUT, 2 },
4085         { 0x0b, HDA_INPUT, 3 },
4086         { 0x0b, HDA_INPUT, 4 },
4087         { } /* end */
4088 };
4089
4090 static struct hda_amp_list alc880_lg_loopbacks[] = {
4091         { 0x0b, HDA_INPUT, 1 },
4092         { 0x0b, HDA_INPUT, 6 },
4093         { 0x0b, HDA_INPUT, 7 },
4094         { } /* end */
4095 };
4096 #endif
4097
4098 /*
4099  * Common callbacks
4100  */
4101
4102 static void alc_init_special_input_src(struct hda_codec *codec);
4103
4104 static int alc_init(struct hda_codec *codec)
4105 {
4106         struct alc_spec *spec = codec->spec;
4107         unsigned int i;
4108
4109         alc_fix_pll(codec);
4110         alc_auto_init_amp(codec, spec->init_amp);
4111
4112         for (i = 0; i < spec->num_init_verbs; i++)
4113                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
4114         alc_init_special_input_src(codec);
4115
4116         if (spec->init_hook)
4117                 spec->init_hook(codec);
4118
4119         alc_apply_fixup(codec, ALC_FIXUP_ACT_INIT);
4120
4121         hda_call_check_power_status(codec, 0x01);
4122         return 0;
4123 }
4124
4125 static void alc_unsol_event(struct hda_codec *codec, unsigned int res)
4126 {
4127         struct alc_spec *spec = codec->spec;
4128
4129         if (spec->unsol_event)
4130                 spec->unsol_event(codec, res);
4131 }
4132
4133 #ifdef CONFIG_SND_HDA_POWER_SAVE
4134 static int alc_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4135 {
4136         struct alc_spec *spec = codec->spec;
4137         return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4138 }
4139 #endif
4140
4141 /*
4142  * Analog playback callbacks
4143  */
4144 static int alc880_playback_pcm_open(struct hda_pcm_stream *hinfo,
4145                                     struct hda_codec *codec,
4146                                     struct snd_pcm_substream *substream)
4147 {
4148         struct alc_spec *spec = codec->spec;
4149         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
4150                                              hinfo);
4151 }
4152
4153 static int alc880_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4154                                        struct hda_codec *codec,
4155                                        unsigned int stream_tag,
4156                                        unsigned int format,
4157                                        struct snd_pcm_substream *substream)
4158 {
4159         struct alc_spec *spec = codec->spec;
4160         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4161                                                 stream_tag, format, substream);
4162 }
4163
4164 static int alc880_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4165                                        struct hda_codec *codec,
4166                                        struct snd_pcm_substream *substream)
4167 {
4168         struct alc_spec *spec = codec->spec;
4169         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4170 }
4171
4172 /*
4173  * Digital out
4174  */
4175 static int alc880_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4176                                         struct hda_codec *codec,
4177                                         struct snd_pcm_substream *substream)
4178 {
4179         struct alc_spec *spec = codec->spec;
4180         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4181 }
4182
4183 static int alc880_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4184                                            struct hda_codec *codec,
4185                                            unsigned int stream_tag,
4186                                            unsigned int format,
4187                                            struct snd_pcm_substream *substream)
4188 {
4189         struct alc_spec *spec = codec->spec;
4190         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4191                                              stream_tag, format, substream);
4192 }
4193
4194 static int alc880_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4195                                            struct hda_codec *codec,
4196                                            struct snd_pcm_substream *substream)
4197 {
4198         struct alc_spec *spec = codec->spec;
4199         return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4200 }
4201
4202 static int alc880_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4203                                          struct hda_codec *codec,
4204                                          struct snd_pcm_substream *substream)
4205 {
4206         struct alc_spec *spec = codec->spec;
4207         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4208 }
4209
4210 /*
4211  * Analog capture
4212  */
4213 static int alc880_alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4214                                       struct hda_codec *codec,
4215                                       unsigned int stream_tag,
4216                                       unsigned int format,
4217                                       struct snd_pcm_substream *substream)
4218 {
4219         struct alc_spec *spec = codec->spec;
4220
4221         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
4222                                    stream_tag, 0, format);
4223         return 0;
4224 }
4225
4226 static int alc880_alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4227                                       struct hda_codec *codec,
4228                                       struct snd_pcm_substream *substream)
4229 {
4230         struct alc_spec *spec = codec->spec;
4231
4232         snd_hda_codec_cleanup_stream(codec,
4233                                      spec->adc_nids[substream->number + 1]);
4234         return 0;
4235 }
4236
4237 /* analog capture with dynamic dual-adc changes */
4238 static int dualmic_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4239                                        struct hda_codec *codec,
4240                                        unsigned int stream_tag,
4241                                        unsigned int format,
4242                                        struct snd_pcm_substream *substream)
4243 {
4244         struct alc_spec *spec = codec->spec;
4245         spec->cur_adc = spec->adc_nids[spec->cur_adc_idx];
4246         spec->cur_adc_stream_tag = stream_tag;
4247         spec->cur_adc_format = format;
4248         snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4249         return 0;
4250 }
4251
4252 static int dualmic_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4253                                        struct hda_codec *codec,
4254                                        struct snd_pcm_substream *substream)
4255 {
4256         struct alc_spec *spec = codec->spec;
4257         snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4258         spec->cur_adc = 0;
4259         return 0;
4260 }
4261
4262 static struct hda_pcm_stream dualmic_pcm_analog_capture = {
4263         .substreams = 1,
4264         .channels_min = 2,
4265         .channels_max = 2,
4266         .nid = 0, /* fill later */
4267         .ops = {
4268                 .prepare = dualmic_capture_pcm_prepare,
4269                 .cleanup = dualmic_capture_pcm_cleanup
4270         },
4271 };
4272
4273 /*
4274  */
4275 static struct hda_pcm_stream alc880_pcm_analog_playback = {
4276         .substreams = 1,
4277         .channels_min = 2,
4278         .channels_max = 8,
4279         /* NID is set in alc_build_pcms */
4280         .ops = {
4281                 .open = alc880_playback_pcm_open,
4282                 .prepare = alc880_playback_pcm_prepare,
4283                 .cleanup = alc880_playback_pcm_cleanup
4284         },
4285 };
4286
4287 static struct hda_pcm_stream alc880_pcm_analog_capture = {
4288         .substreams = 1,
4289         .channels_min = 2,
4290         .channels_max = 2,
4291         /* NID is set in alc_build_pcms */
4292 };
4293
4294 static struct hda_pcm_stream alc880_pcm_analog_alt_playback = {
4295         .substreams = 1,
4296         .channels_min = 2,
4297         .channels_max = 2,
4298         /* NID is set in alc_build_pcms */
4299 };
4300
4301 static struct hda_pcm_stream alc880_pcm_analog_alt_capture = {
4302         .substreams = 2, /* can be overridden */
4303         .channels_min = 2,
4304         .channels_max = 2,
4305         /* NID is set in alc_build_pcms */
4306         .ops = {
4307                 .prepare = alc880_alt_capture_pcm_prepare,
4308                 .cleanup = alc880_alt_capture_pcm_cleanup
4309         },
4310 };
4311
4312 static struct hda_pcm_stream alc880_pcm_digital_playback = {
4313         .substreams = 1,
4314         .channels_min = 2,
4315         .channels_max = 2,
4316         /* NID is set in alc_build_pcms */
4317         .ops = {
4318                 .open = alc880_dig_playback_pcm_open,
4319                 .close = alc880_dig_playback_pcm_close,
4320                 .prepare = alc880_dig_playback_pcm_prepare,
4321                 .cleanup = alc880_dig_playback_pcm_cleanup
4322         },
4323 };
4324
4325 static struct hda_pcm_stream alc880_pcm_digital_capture = {
4326         .substreams = 1,
4327         .channels_min = 2,
4328         .channels_max = 2,
4329         /* NID is set in alc_build_pcms */
4330 };
4331
4332 /* Used by alc_build_pcms to flag that a PCM has no playback stream */
4333 static struct hda_pcm_stream alc_pcm_null_stream = {
4334         .substreams = 0,
4335         .channels_min = 0,
4336         .channels_max = 0,
4337 };
4338
4339 static int alc_build_pcms(struct hda_codec *codec)
4340 {
4341         struct alc_spec *spec = codec->spec;
4342         struct hda_pcm *info = spec->pcm_rec;
4343         int i;
4344
4345         codec->num_pcms = 1;
4346         codec->pcm_info = info;
4347
4348         if (spec->no_analog)
4349                 goto skip_analog;
4350
4351         snprintf(spec->stream_name_analog, sizeof(spec->stream_name_analog),
4352                  "%s Analog", codec->chip_name);
4353         info->name = spec->stream_name_analog;
4354
4355         if (spec->stream_analog_playback) {
4356                 if (snd_BUG_ON(!spec->multiout.dac_nids))
4357                         return -EINVAL;
4358                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *(spec->stream_analog_playback);
4359                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4360         }
4361         if (spec->stream_analog_capture) {
4362                 if (snd_BUG_ON(!spec->adc_nids))
4363                         return -EINVAL;
4364                 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_analog_capture);
4365                 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4366         }
4367
4368         if (spec->channel_mode) {
4369                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = 0;
4370                 for (i = 0; i < spec->num_channel_mode; i++) {
4371                         if (spec->channel_mode[i].channels > info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max) {
4372                                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->channel_mode[i].channels;
4373                         }
4374                 }
4375         }
4376
4377  skip_analog:
4378         /* SPDIF for stream index #1 */
4379         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
4380                 snprintf(spec->stream_name_digital,
4381                          sizeof(spec->stream_name_digital),
4382                          "%s Digital", codec->chip_name);
4383                 codec->num_pcms = 2;
4384                 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4385                 info = spec->pcm_rec + 1;
4386                 info->name = spec->stream_name_digital;
4387                 if (spec->dig_out_type)
4388                         info->pcm_type = spec->dig_out_type;
4389                 else
4390                         info->pcm_type = HDA_PCM_TYPE_SPDIF;
4391                 if (spec->multiout.dig_out_nid &&
4392                     spec->stream_digital_playback) {
4393                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *(spec->stream_digital_playback);
4394                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4395                 }
4396                 if (spec->dig_in_nid &&
4397                     spec->stream_digital_capture) {
4398                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_digital_capture);
4399                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4400                 }
4401                 /* FIXME: do we need this for all Realtek codec models? */
4402                 codec->spdif_status_reset = 1;
4403         }
4404
4405         if (spec->no_analog)
4406                 return 0;
4407
4408         /* If the use of more than one ADC is requested for the current
4409          * model, configure a second analog capture-only PCM.
4410          */
4411         /* Additional Analaog capture for index #2 */
4412         if ((spec->alt_dac_nid && spec->stream_analog_alt_playback) ||
4413             (spec->num_adc_nids > 1 && spec->stream_analog_alt_capture)) {
4414                 codec->num_pcms = 3;
4415                 info = spec->pcm_rec + 2;
4416                 info->name = spec->stream_name_analog;
4417                 if (spec->alt_dac_nid) {
4418                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4419                                 *spec->stream_analog_alt_playback;
4420                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4421                                 spec->alt_dac_nid;
4422                 } else {
4423                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4424                                 alc_pcm_null_stream;
4425                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4426                 }
4427                 if (spec->num_adc_nids > 1) {
4428                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4429                                 *spec->stream_analog_alt_capture;
4430                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4431                                 spec->adc_nids[1];
4432                         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4433                                 spec->num_adc_nids - 1;
4434                 } else {
4435                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4436                                 alc_pcm_null_stream;
4437                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4438                 }
4439         }
4440
4441         return 0;
4442 }
4443
4444 static inline void alc_shutup(struct hda_codec *codec)
4445 {
4446         struct alc_spec *spec = codec->spec;
4447
4448         if (spec && spec->shutup)
4449                 spec->shutup(codec);
4450         snd_hda_shutup_pins(codec);
4451 }
4452
4453 static void alc_free_kctls(struct hda_codec *codec)
4454 {
4455         struct alc_spec *spec = codec->spec;
4456
4457         if (spec->kctls.list) {
4458                 struct snd_kcontrol_new *kctl = spec->kctls.list;
4459                 int i;
4460                 for (i = 0; i < spec->kctls.used; i++)
4461                         kfree(kctl[i].name);
4462         }
4463         snd_array_free(&spec->kctls);
4464 }
4465
4466 static void alc_free(struct hda_codec *codec)
4467 {
4468         struct alc_spec *spec = codec->spec;
4469
4470         if (!spec)
4471                 return;
4472
4473         alc_shutup(codec);
4474         snd_hda_input_jack_free(codec);
4475         alc_free_kctls(codec);
4476         kfree(spec);
4477         snd_hda_detach_beep_device(codec);
4478 }
4479
4480 #ifdef CONFIG_SND_HDA_POWER_SAVE
4481 static void alc_power_eapd(struct hda_codec *codec)
4482 {
4483         alc_auto_setup_eapd(codec, false);
4484 }
4485
4486 static int alc_suspend(struct hda_codec *codec, pm_message_t state)
4487 {
4488         struct alc_spec *spec = codec->spec;
4489         alc_shutup(codec);
4490         if (spec && spec->power_hook)
4491                 spec->power_hook(codec);
4492         return 0;
4493 }
4494 #endif
4495
4496 #ifdef SND_HDA_NEEDS_RESUME
4497 static int alc_resume(struct hda_codec *codec)
4498 {
4499         msleep(150); /* to avoid pop noise */
4500         codec->patch_ops.init(codec);
4501         snd_hda_codec_resume_amp(codec);
4502         snd_hda_codec_resume_cache(codec);
4503         hda_call_check_power_status(codec, 0x01);
4504         return 0;
4505 }
4506 #endif
4507
4508 /*
4509  */
4510 static struct hda_codec_ops alc_patch_ops = {
4511         .build_controls = alc_build_controls,
4512         .build_pcms = alc_build_pcms,
4513         .init = alc_init,
4514         .free = alc_free,
4515         .unsol_event = alc_unsol_event,
4516 #ifdef SND_HDA_NEEDS_RESUME
4517         .resume = alc_resume,
4518 #endif
4519 #ifdef CONFIG_SND_HDA_POWER_SAVE
4520         .suspend = alc_suspend,
4521         .check_power_status = alc_check_power_status,
4522 #endif
4523         .reboot_notify = alc_shutup,
4524 };
4525
4526 /* replace the codec chip_name with the given string */
4527 static int alc_codec_rename(struct hda_codec *codec, const char *name)
4528 {
4529         kfree(codec->chip_name);
4530         codec->chip_name = kstrdup(name, GFP_KERNEL);
4531         if (!codec->chip_name) {
4532                 alc_free(codec);
4533                 return -ENOMEM;
4534         }
4535         return 0;
4536 }
4537
4538 /*
4539  * Test configuration for debugging
4540  *
4541  * Almost all inputs/outputs are enabled.  I/O pins can be configured via
4542  * enum controls.
4543  */
4544 #ifdef CONFIG_SND_DEBUG
4545 static hda_nid_t alc880_test_dac_nids[4] = {
4546         0x02, 0x03, 0x04, 0x05
4547 };
4548
4549 static struct hda_input_mux alc880_test_capture_source = {
4550         .num_items = 7,
4551         .items = {
4552                 { "In-1", 0x0 },
4553                 { "In-2", 0x1 },
4554                 { "In-3", 0x2 },
4555                 { "In-4", 0x3 },
4556                 { "CD", 0x4 },
4557                 { "Front", 0x5 },
4558                 { "Surround", 0x6 },
4559         },
4560 };
4561
4562 static struct hda_channel_mode alc880_test_modes[4] = {
4563         { 2, NULL },
4564         { 4, NULL },
4565         { 6, NULL },
4566         { 8, NULL },
4567 };
4568
4569 static int alc_test_pin_ctl_info(struct snd_kcontrol *kcontrol,
4570                                  struct snd_ctl_elem_info *uinfo)
4571 {
4572         static char *texts[] = {
4573                 "N/A", "Line Out", "HP Out",
4574                 "In Hi-Z", "In 50%", "In Grd", "In 80%", "In 100%"
4575         };
4576         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4577         uinfo->count = 1;
4578         uinfo->value.enumerated.items = 8;
4579         if (uinfo->value.enumerated.item >= 8)
4580                 uinfo->value.enumerated.item = 7;
4581         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
4582         return 0;
4583 }
4584
4585 static int alc_test_pin_ctl_get(struct snd_kcontrol *kcontrol,
4586                                 struct snd_ctl_elem_value *ucontrol)
4587 {
4588         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4589         hda_nid_t nid = (hda_nid_t)kcontrol->private_value;
4590         unsigned int pin_ctl, item = 0;
4591
4592         pin_ctl = snd_hda_codec_read(codec, nid, 0,
4593                                      AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4594         if (pin_ctl & AC_PINCTL_OUT_EN) {
4595                 if (pin_ctl & AC_PINCTL_HP_EN)
4596                         item = 2;
4597                 else
4598                         item = 1;
4599         } else if (pin_ctl & AC_PINCTL_IN_EN) {
4600                 switch (pin_ctl & AC_PINCTL_VREFEN) {
4601                 case AC_PINCTL_VREF_HIZ: item = 3; break;
4602                 case AC_PINCTL_VREF_50:  item = 4; break;
4603                 case AC_PINCTL_VREF_GRD: item = 5; break;
4604                 case AC_PINCTL_VREF_80:  item = 6; break;
4605                 case AC_PINCTL_VREF_100: item = 7; break;
4606                 }
4607         }
4608         ucontrol->value.enumerated.item[0] = item;
4609         return 0;
4610 }
4611
4612 static int alc_test_pin_ctl_put(struct snd_kcontrol *kcontrol,
4613                                 struct snd_ctl_elem_value *ucontrol)
4614 {
4615         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4616         hda_nid_t nid = (hda_nid_t)kcontrol->private_value;
4617         static unsigned int ctls[] = {
4618                 0, AC_PINCTL_OUT_EN, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN,
4619                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_HIZ,
4620                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_50,
4621                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_GRD,
4622                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_80,
4623                 AC_PINCTL_IN_EN | AC_PINCTL_VREF_100,
4624         };
4625         unsigned int old_ctl, new_ctl;
4626
4627         old_ctl = snd_hda_codec_read(codec, nid, 0,
4628                                      AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4629         new_ctl = ctls[ucontrol->value.enumerated.item[0]];
4630         if (old_ctl != new_ctl) {
4631                 int val;
4632                 snd_hda_codec_write_cache(codec, nid, 0,
4633                                           AC_VERB_SET_PIN_WIDGET_CONTROL,
4634                                           new_ctl);
4635                 val = ucontrol->value.enumerated.item[0] >= 3 ?
4636                         HDA_AMP_MUTE : 0;
4637                 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
4638                                          HDA_AMP_MUTE, val);
4639                 return 1;
4640         }
4641         return 0;
4642 }
4643
4644 static int alc_test_pin_src_info(struct snd_kcontrol *kcontrol,
4645                                  struct snd_ctl_elem_info *uinfo)
4646 {
4647         static char *texts[] = {
4648                 "Front", "Surround", "CLFE", "Side"
4649         };
4650         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4651         uinfo->count = 1;
4652         uinfo->value.enumerated.items = 4;
4653         if (uinfo->value.enumerated.item >= 4)
4654                 uinfo->value.enumerated.item = 3;
4655         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
4656         return 0;
4657 }
4658
4659 static int alc_test_pin_src_get(struct snd_kcontrol *kcontrol,
4660                                 struct snd_ctl_elem_value *ucontrol)
4661 {
4662         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4663         hda_nid_t nid = (hda_nid_t)kcontrol->private_value;
4664         unsigned int sel;
4665
4666         sel = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_SEL, 0);
4667         ucontrol->value.enumerated.item[0] = sel & 3;
4668         return 0;
4669 }
4670
4671 static int alc_test_pin_src_put(struct snd_kcontrol *kcontrol,
4672                                 struct snd_ctl_elem_value *ucontrol)
4673 {
4674         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4675         hda_nid_t nid = (hda_nid_t)kcontrol->private_value;
4676         unsigned int sel;
4677
4678         sel = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_SEL, 0) & 3;
4679         if (ucontrol->value.enumerated.item[0] != sel) {
4680                 sel = ucontrol->value.enumerated.item[0] & 3;
4681                 snd_hda_codec_write_cache(codec, nid, 0,
4682                                           AC_VERB_SET_CONNECT_SEL, sel);
4683                 return 1;
4684         }
4685         return 0;
4686 }
4687
4688 #define PIN_CTL_TEST(xname,nid) {                       \
4689                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,    \
4690                         .name = xname,                 \
4691                         .subdevice = HDA_SUBDEV_NID_FLAG | nid, \
4692                         .info = alc_test_pin_ctl_info, \
4693                         .get = alc_test_pin_ctl_get,   \
4694                         .put = alc_test_pin_ctl_put,   \
4695                         .private_value = nid           \
4696                         }
4697
4698 #define PIN_SRC_TEST(xname,nid) {                       \
4699                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,    \
4700                         .name = xname,                 \
4701                         .subdevice = HDA_SUBDEV_NID_FLAG | nid, \
4702                         .info = alc_test_pin_src_info, \
4703                         .get = alc_test_pin_src_get,   \
4704                         .put = alc_test_pin_src_put,   \
4705                         .private_value = nid           \
4706                         }
4707
4708 static struct snd_kcontrol_new alc880_test_mixer[] = {
4709         HDA_CODEC_VOLUME("Front Playback Volume", 0x0c, 0x0, HDA_OUTPUT),
4710         HDA_CODEC_VOLUME("Surround Playback Volume", 0x0d, 0x0, HDA_OUTPUT),
4711         HDA_CODEC_VOLUME("CLFE Playback Volume", 0x0e, 0x0, HDA_OUTPUT),
4712         HDA_CODEC_VOLUME("Side Playback Volume", 0x0f, 0x0, HDA_OUTPUT),
4713         HDA_BIND_MUTE("Front Playback Switch", 0x0c, 2, HDA_INPUT),
4714         HDA_BIND_MUTE("Surround Playback Switch", 0x0d, 2, HDA_INPUT),
4715         HDA_BIND_MUTE("CLFE Playback Switch", 0x0e, 2, HDA_INPUT),
4716         HDA_BIND_MUTE("Side Playback Switch", 0x0f, 2, HDA_INPUT),
4717         PIN_CTL_TEST("Front Pin Mode", 0x14),
4718         PIN_CTL_TEST("Surround Pin Mode", 0x15),
4719         PIN_CTL_TEST("CLFE Pin Mode", 0x16),
4720         PIN_CTL_TEST("Side Pin Mode", 0x17),
4721         PIN_CTL_TEST("In-1 Pin Mode", 0x18),
4722         PIN_CTL_TEST("In-2 Pin Mode", 0x19),
4723         PIN_CTL_TEST("In-3 Pin Mode", 0x1a),
4724         PIN_CTL_TEST("In-4 Pin Mode", 0x1b),
4725         PIN_SRC_TEST("In-1 Pin Source", 0x18),
4726         PIN_SRC_TEST("In-2 Pin Source", 0x19),
4727         PIN_SRC_TEST("In-3 Pin Source", 0x1a),
4728         PIN_SRC_TEST("In-4 Pin Source", 0x1b),
4729         HDA_CODEC_VOLUME("In-1 Playback Volume", 0x0b, 0x0, HDA_INPUT),
4730         HDA_CODEC_MUTE("In-1 Playback Switch", 0x0b, 0x0, HDA_INPUT),
4731         HDA_CODEC_VOLUME("In-2 Playback Volume", 0x0b, 0x1, HDA_INPUT),
4732         HDA_CODEC_MUTE("In-2 Playback Switch", 0x0b, 0x1, HDA_INPUT),
4733         HDA_CODEC_VOLUME("In-3 Playback Volume", 0x0b, 0x2, HDA_INPUT),
4734         HDA_CODEC_MUTE("In-3 Playback Switch", 0x0b, 0x2, HDA_INPUT),
4735         HDA_CODEC_VOLUME("In-4 Playback Volume", 0x0b, 0x3, HDA_INPUT),
4736         HDA_CODEC_MUTE("In-4 Playback Switch", 0x0b, 0x3, HDA_INPUT),
4737         HDA_CODEC_VOLUME("CD Playback Volume", 0x0b, 0x4, HDA_INPUT),
4738         HDA_CODEC_MUTE("CD Playback Switch", 0x0b, 0x4, HDA_INPUT),
4739         {
4740                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4741                 .name = "Channel Mode",
4742                 .info = alc_ch_mode_info,
4743                 .get = alc_ch_mode_get,
4744                 .put = alc_ch_mode_put,
4745         },
4746         { } /* end */
4747 };
4748
4749 static struct hda_verb alc880_test_init_verbs[] = {
4750         /* Unmute inputs of 0x0c - 0x0f */
4751         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4752         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4753         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4754         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4755         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4756         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4757         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
4758         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
4759         /* Vol output for 0x0c-0x0f */
4760         {0x0c, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
4761         {0x0d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
4762         {0x0e, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
4763         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
4764         /* Set output pins 0x14-0x17 */
4765         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
4766         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
4767         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
4768         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
4769         /* Unmute output pins 0x14-0x17 */
4770         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4771         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4772         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4773         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
4774         /* Set input pins 0x18-0x1c */
4775         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
4776         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
4777         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
4778         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
4779         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
4780         /* Mute input pins 0x18-0x1b */
4781         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4782         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4783         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4784         {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
4785         /* ADC set up */
4786         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4787         {0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
4788         {0x08, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4789         {0x08, AC_VERB_SET_CONNECT_SEL, 0x00},
4790         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4791         {0x09, AC_VERB_SET_CONNECT_SEL, 0x00},
4792         /* Analog input/passthru */
4793         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
4794         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
4795         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
4796         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
4797         {0x0b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
4798         { }
4799 };
4800 #endif
4801
4802 /*
4803  */
4804
4805 static const char * const alc880_models[ALC880_MODEL_LAST] = {
4806         [ALC880_3ST]            = "3stack",
4807         [ALC880_TCL_S700]       = "tcl",
4808         [ALC880_3ST_DIG]        = "3stack-digout",
4809         [ALC880_CLEVO]          = "clevo",
4810         [ALC880_5ST]            = "5stack",
4811         [ALC880_5ST_DIG]        = "5stack-digout",
4812         [ALC880_W810]           = "w810",
4813         [ALC880_Z71V]           = "z71v",
4814         [ALC880_6ST]            = "6stack",
4815         [ALC880_6ST_DIG]        = "6stack-digout",
4816         [ALC880_ASUS]           = "asus",
4817         [ALC880_ASUS_W1V]       = "asus-w1v",
4818         [ALC880_ASUS_DIG]       = "asus-dig",
4819         [ALC880_ASUS_DIG2]      = "asus-dig2",
4820         [ALC880_UNIWILL_DIG]    = "uniwill",
4821         [ALC880_UNIWILL_P53]    = "uniwill-p53",
4822         [ALC880_FUJITSU]        = "fujitsu",
4823         [ALC880_F1734]          = "F1734",
4824         [ALC880_LG]             = "lg",
4825         [ALC880_LG_LW]          = "lg-lw",
4826         [ALC880_MEDION_RIM]     = "medion",
4827 #ifdef CONFIG_SND_DEBUG
4828         [ALC880_TEST]           = "test",
4829 #endif
4830         [ALC880_AUTO]           = "auto",
4831 };
4832
4833 static struct snd_pci_quirk alc880_cfg_tbl[] = {
4834         SND_PCI_QUIRK(0x1019, 0x0f69, "Coeus G610P", ALC880_W810),
4835         SND_PCI_QUIRK(0x1019, 0xa880, "ECS", ALC880_5ST_DIG),
4836         SND_PCI_QUIRK(0x1019, 0xa884, "Acer APFV", ALC880_6ST),
4837         SND_PCI_QUIRK(0x1025, 0x0070, "ULI", ALC880_3ST_DIG),
4838         SND_PCI_QUIRK(0x1025, 0x0077, "ULI", ALC880_6ST_DIG),
4839         SND_PCI_QUIRK(0x1025, 0x0078, "ULI", ALC880_6ST_DIG),
4840         SND_PCI_QUIRK(0x1025, 0x0087, "ULI", ALC880_6ST_DIG),
4841         SND_PCI_QUIRK(0x1025, 0xe309, "ULI", ALC880_3ST_DIG),
4842         SND_PCI_QUIRK(0x1025, 0xe310, "ULI", ALC880_3ST),
4843         SND_PCI_QUIRK(0x1039, 0x1234, NULL, ALC880_6ST_DIG),
4844         SND_PCI_QUIRK(0x103c, 0x2a09, "HP", ALC880_5ST),
4845         SND_PCI_QUIRK(0x1043, 0x10b3, "ASUS W1V", ALC880_ASUS_W1V),
4846         SND_PCI_QUIRK(0x1043, 0x10c2, "ASUS W6A", ALC880_ASUS_DIG),
4847         SND_PCI_QUIRK(0x1043, 0x10c3, "ASUS Wxx", ALC880_ASUS_DIG),
4848         SND_PCI_QUIRK(0x1043, 0x1113, "ASUS", ALC880_ASUS_DIG),
4849         SND_PCI_QUIRK(0x1043, 0x1123, "ASUS", ALC880_ASUS_DIG),
4850         SND_PCI_QUIRK(0x1043, 0x1173, "ASUS", ALC880_ASUS_DIG),
4851         SND_PCI_QUIRK(0x1043, 0x1964, "ASUS Z71V", ALC880_Z71V),
4852         /* SND_PCI_QUIRK(0x1043, 0x1964, "ASUS", ALC880_ASUS_DIG), */
4853         SND_PCI_QUIRK(0x1043, 0x1973, "ASUS", ALC880_ASUS_DIG),
4854         SND_PCI_QUIRK(0x1043, 0x19b3, "ASUS", ALC880_ASUS_DIG),
4855         SND_PCI_QUIRK(0x1043, 0x814e, "ASUS P5GD1 w/SPDIF", ALC880_6ST_DIG),
4856         SND_PCI_QUIRK(0x1043, 0x8181, "ASUS P4GPL", ALC880_ASUS_DIG),
4857         SND_PCI_QUIRK(0x1043, 0x8196, "ASUS P5GD1", ALC880_6ST),
4858         SND_PCI_QUIRK(0x1043, 0x81b4, "ASUS", ALC880_6ST),
4859         SND_PCI_QUIRK_VENDOR(0x1043, "ASUS", ALC880_ASUS), /* default ASUS */
4860         SND_PCI_QUIRK(0x104d, 0x81a0, "Sony", ALC880_3ST),
4861         SND_PCI_QUIRK(0x104d, 0x81d6, "Sony", ALC880_3ST),
4862         SND_PCI_QUIRK(0x107b, 0x3032, "Gateway", ALC880_5ST),
4863         SND_PCI_QUIRK(0x107b, 0x3033, "Gateway", ALC880_5ST),
4864         SND_PCI_QUIRK(0x107b, 0x4039, "Gateway", ALC880_5ST),
4865         SND_PCI_QUIRK(0x1297, 0xc790, "Shuttle ST20G5", ALC880_6ST_DIG),
4866         SND_PCI_QUIRK(0x1458, 0xa102, "Gigabyte K8", ALC880_6ST_DIG),
4867         SND_PCI_QUIRK(0x1462, 0x1150, "MSI", ALC880_6ST_DIG),
4868         SND_PCI_QUIRK(0x1509, 0x925d, "FIC P4M", ALC880_6ST_DIG),
4869         SND_PCI_QUIRK(0x1558, 0x0520, "Clevo m520G", ALC880_CLEVO),
4870         SND_PCI_QUIRK(0x1558, 0x0660, "Clevo m655n", ALC880_CLEVO),
4871         SND_PCI_QUIRK(0x1558, 0x5401, "ASUS", ALC880_ASUS_DIG2),
4872         SND_PCI_QUIRK(0x1565, 0x8202, "Biostar", ALC880_5ST_DIG),
4873         SND_PCI_QUIRK(0x1584, 0x9050, "Uniwill", ALC880_UNIWILL_DIG),
4874         SND_PCI_QUIRK(0x1584, 0x9054, "Uniwill", ALC880_F1734),
4875         SND_PCI_QUIRK(0x1584, 0x9070, "Uniwill", ALC880_UNIWILL),
4876         SND_PCI_QUIRK(0x1584, 0x9077, "Uniwill P53", ALC880_UNIWILL_P53),
4877         SND_PCI_QUIRK(0x161f, 0x203d, "W810", ALC880_W810),
4878         SND_PCI_QUIRK(0x161f, 0x205d, "Medion Rim 2150", ALC880_MEDION_RIM),
4879         SND_PCI_QUIRK(0x1695, 0x400d, "EPoX", ALC880_5ST_DIG),
4880         SND_PCI_QUIRK(0x1695, 0x4012, "EPox EP-5LDA", ALC880_5ST_DIG),
4881         SND_PCI_QUIRK(0x1734, 0x107c, "FSC F1734", ALC880_F1734),
4882         SND_PCI_QUIRK(0x1734, 0x1094, "FSC Amilo M1451G", ALC880_FUJITSU),
4883         SND_PCI_QUIRK(0x1734, 0x10ac, "FSC AMILO Xi 1526", ALC880_F1734),
4884         SND_PCI_QUIRK(0x1734, 0x10b0, "Fujitsu", ALC880_FUJITSU),
4885         SND_PCI_QUIRK(0x1854, 0x0018, "LG LW20", ALC880_LG_LW),
4886         SND_PCI_QUIRK(0x1854, 0x003b, "LG", ALC880_LG),
4887         SND_PCI_QUIRK(0x1854, 0x005f, "LG P1 Express", ALC880_LG),
4888         SND_PCI_QUIRK(0x1854, 0x0068, "LG w1", ALC880_LG),
4889         SND_PCI_QUIRK(0x1854, 0x0077, "LG LW25", ALC880_LG_LW),
4890         SND_PCI_QUIRK(0x19db, 0x4188, "TCL S700", ALC880_TCL_S700),
4891         SND_PCI_QUIRK(0x2668, 0x8086, NULL, ALC880_6ST_DIG), /* broken BIOS */
4892         SND_PCI_QUIRK(0x8086, 0x2668, NULL, ALC880_6ST_DIG),
4893         SND_PCI_QUIRK(0x8086, 0xa100, "Intel mobo", ALC880_5ST_DIG),
4894         SND_PCI_QUIRK(0x8086, 0xd400, "Intel mobo", ALC880_5ST_DIG),
4895         SND_PCI_QUIRK(0x8086, 0xd401, "Intel mobo", ALC880_5ST_DIG),
4896         SND_PCI_QUIRK(0x8086, 0xd402, "Intel mobo", ALC880_3ST_DIG),
4897         SND_PCI_QUIRK(0x8086, 0xe224, "Intel mobo", ALC880_5ST_DIG),
4898         SND_PCI_QUIRK(0x8086, 0xe305, "Intel mobo", ALC880_3ST_DIG),
4899         SND_PCI_QUIRK(0x8086, 0xe308, "Intel mobo", ALC880_3ST_DIG),
4900         SND_PCI_QUIRK(0x8086, 0xe400, "Intel mobo", ALC880_5ST_DIG),
4901         SND_PCI_QUIRK(0x8086, 0xe401, "Intel mobo", ALC880_5ST_DIG),
4902         SND_PCI_QUIRK(0x8086, 0xe402, "Intel mobo", ALC880_5ST_DIG),
4903         /* default Intel */
4904         SND_PCI_QUIRK_VENDOR(0x8086, "Intel mobo", ALC880_3ST),
4905         SND_PCI_QUIRK(0xa0a0, 0x0560, "AOpen i915GMm-HFS", ALC880_5ST_DIG),
4906         SND_PCI_QUIRK(0xe803, 0x1019, NULL, ALC880_6ST_DIG),
4907         {}
4908 };
4909
4910 /*
4911  * ALC880 codec presets
4912  */
4913 static struct alc_config_preset alc880_presets[] = {
4914         [ALC880_3ST] = {
4915                 .mixers = { alc880_three_stack_mixer },
4916                 .init_verbs = { alc880_volume_init_verbs,
4917                                 alc880_pin_3stack_init_verbs },
4918                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
4919                 .dac_nids = alc880_dac_nids,
4920                 .num_channel_mode = ARRAY_SIZE(alc880_threestack_modes),
4921                 .channel_mode = alc880_threestack_modes,
4922                 .need_dac_fix = 1,
4923                 .input_mux = &alc880_capture_source,
4924         },
4925         [ALC880_3ST_DIG] = {
4926                 .mixers = { alc880_three_stack_mixer },
4927                 .init_verbs = { alc880_volume_init_verbs,
4928                                 alc880_pin_3stack_init_verbs },
4929                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
4930                 .dac_nids = alc880_dac_nids,
4931                 .dig_out_nid = ALC880_DIGOUT_NID,
4932                 .num_channel_mode = ARRAY_SIZE(alc880_threestack_modes),
4933                 .channel_mode = alc880_threestack_modes,
4934                 .need_dac_fix = 1,
4935                 .input_mux = &alc880_capture_source,
4936         },
4937         [ALC880_TCL_S700] = {
4938                 .mixers = { alc880_tcl_s700_mixer },
4939                 .init_verbs = { alc880_volume_init_verbs,
4940                                 alc880_pin_tcl_S700_init_verbs,
4941                                 alc880_gpio2_init_verbs },
4942                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
4943                 .dac_nids = alc880_dac_nids,
4944                 .adc_nids = alc880_adc_nids_alt, /* FIXME: correct? */
4945                 .num_adc_nids = 1, /* single ADC */
4946                 .hp_nid = 0x03,
4947                 .num_channel_mode = ARRAY_SIZE(alc880_2_jack_modes),
4948                 .channel_mode = alc880_2_jack_modes,
4949                 .input_mux = &alc880_capture_source,
4950         },
4951         [ALC880_5ST] = {
4952                 .mixers = { alc880_three_stack_mixer,
4953                             alc880_five_stack_mixer},
4954                 .init_verbs = { alc880_volume_init_verbs,
4955                                 alc880_pin_5stack_init_verbs },
4956                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
4957                 .dac_nids = alc880_dac_nids,
4958                 .num_channel_mode = ARRAY_SIZE(alc880_fivestack_modes),
4959                 .channel_mode = alc880_fivestack_modes,
4960                 .input_mux = &alc880_capture_source,
4961         },
4962         [ALC880_5ST_DIG] = {
4963                 .mixers = { alc880_three_stack_mixer,
4964                             alc880_five_stack_mixer },
4965                 .init_verbs = { alc880_volume_init_verbs,
4966                                 alc880_pin_5stack_init_verbs },
4967                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
4968                 .dac_nids = alc880_dac_nids,
4969                 .dig_out_nid = ALC880_DIGOUT_NID,
4970                 .num_channel_mode = ARRAY_SIZE(alc880_fivestack_modes),
4971                 .channel_mode = alc880_fivestack_modes,
4972                 .input_mux = &alc880_capture_source,
4973         },
4974         [ALC880_6ST] = {
4975                 .mixers = { alc880_six_stack_mixer },
4976                 .init_verbs = { alc880_volume_init_verbs,
4977                                 alc880_pin_6stack_init_verbs },
4978                 .num_dacs = ARRAY_SIZE(alc880_6st_dac_nids),
4979                 .dac_nids = alc880_6st_dac_nids,
4980                 .num_channel_mode = ARRAY_SIZE(alc880_sixstack_modes),
4981                 .channel_mode = alc880_sixstack_modes,
4982                 .input_mux = &alc880_6stack_capture_source,
4983         },
4984         [ALC880_6ST_DIG] = {
4985                 .mixers = { alc880_six_stack_mixer },
4986                 .init_verbs = { alc880_volume_init_verbs,
4987                                 alc880_pin_6stack_init_verbs },
4988                 .num_dacs = ARRAY_SIZE(alc880_6st_dac_nids),
4989                 .dac_nids = alc880_6st_dac_nids,
4990                 .dig_out_nid = ALC880_DIGOUT_NID,
4991                 .num_channel_mode = ARRAY_SIZE(alc880_sixstack_modes),
4992                 .channel_mode = alc880_sixstack_modes,
4993                 .input_mux = &alc880_6stack_capture_source,
4994         },
4995         [ALC880_W810] = {
4996                 .mixers = { alc880_w810_base_mixer },
4997                 .init_verbs = { alc880_volume_init_verbs,
4998                                 alc880_pin_w810_init_verbs,
4999                                 alc880_gpio2_init_verbs },
5000                 .num_dacs = ARRAY_SIZE(alc880_w810_dac_nids),
5001                 .dac_nids = alc880_w810_dac_nids,
5002                 .dig_out_nid = ALC880_DIGOUT_NID,
5003                 .num_channel_mode = ARRAY_SIZE(alc880_w810_modes),
5004                 .channel_mode = alc880_w810_modes,
5005                 .input_mux = &alc880_capture_source,
5006         },
5007         [ALC880_Z71V] = {
5008                 .mixers = { alc880_z71v_mixer },
5009                 .init_verbs = { alc880_volume_init_verbs,
5010                                 alc880_pin_z71v_init_verbs },
5011                 .num_dacs = ARRAY_SIZE(alc880_z71v_dac_nids),
5012                 .dac_nids = alc880_z71v_dac_nids,
5013                 .dig_out_nid = ALC880_DIGOUT_NID,
5014                 .hp_nid = 0x03,
5015                 .num_channel_mode = ARRAY_SIZE(alc880_2_jack_modes),
5016                 .channel_mode = alc880_2_jack_modes,
5017                 .input_mux = &alc880_capture_source,
5018         },
5019         [ALC880_F1734] = {
5020                 .mixers = { alc880_f1734_mixer },
5021                 .init_verbs = { alc880_volume_init_verbs,
5022                                 alc880_pin_f1734_init_verbs },
5023                 .num_dacs = ARRAY_SIZE(alc880_f1734_dac_nids),
5024                 .dac_nids = alc880_f1734_dac_nids,
5025                 .hp_nid = 0x02,
5026                 .num_channel_mode = ARRAY_SIZE(alc880_2_jack_modes),
5027                 .channel_mode = alc880_2_jack_modes,
5028                 .input_mux = &alc880_f1734_capture_source,
5029                 .unsol_event = alc880_uniwill_p53_unsol_event,
5030                 .setup = alc880_uniwill_p53_setup,
5031                 .init_hook = alc_hp_automute,
5032         },
5033         [ALC880_ASUS] = {
5034                 .mixers = { alc880_asus_mixer },
5035                 .init_verbs = { alc880_volume_init_verbs,
5036                                 alc880_pin_asus_init_verbs,
5037                                 alc880_gpio1_init_verbs },
5038                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
5039                 .dac_nids = alc880_asus_dac_nids,
5040                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
5041                 .channel_mode = alc880_asus_modes,
5042                 .need_dac_fix = 1,
5043                 .input_mux = &alc880_capture_source,
5044         },
5045         [ALC880_ASUS_DIG] = {
5046                 .mixers = { alc880_asus_mixer },
5047                 .init_verbs = { alc880_volume_init_verbs,
5048                                 alc880_pin_asus_init_verbs,
5049                                 alc880_gpio1_init_verbs },
5050                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
5051                 .dac_nids = alc880_asus_dac_nids,
5052                 .dig_out_nid = ALC880_DIGOUT_NID,
5053                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
5054                 .channel_mode = alc880_asus_modes,
5055                 .need_dac_fix = 1,
5056                 .input_mux = &alc880_capture_source,
5057         },
5058         [ALC880_ASUS_DIG2] = {
5059                 .mixers = { alc880_asus_mixer },
5060                 .init_verbs = { alc880_volume_init_verbs,
5061                                 alc880_pin_asus_init_verbs,
5062                                 alc880_gpio2_init_verbs }, /* use GPIO2 */
5063                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
5064                 .dac_nids = alc880_asus_dac_nids,
5065                 .dig_out_nid = ALC880_DIGOUT_NID,
5066                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
5067                 .channel_mode = alc880_asus_modes,
5068                 .need_dac_fix = 1,
5069                 .input_mux = &alc880_capture_source,
5070         },
5071         [ALC880_ASUS_W1V] = {
5072                 .mixers = { alc880_asus_mixer, alc880_asus_w1v_mixer },
5073                 .init_verbs = { alc880_volume_init_verbs,
5074                                 alc880_pin_asus_init_verbs,
5075                                 alc880_gpio1_init_verbs },
5076                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
5077                 .dac_nids = alc880_asus_dac_nids,
5078                 .dig_out_nid = ALC880_DIGOUT_NID,
5079                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
5080                 .channel_mode = alc880_asus_modes,
5081                 .need_dac_fix = 1,
5082                 .input_mux = &alc880_capture_source,
5083         },
5084         [ALC880_UNIWILL_DIG] = {
5085                 .mixers = { alc880_asus_mixer },
5086                 .init_verbs = { alc880_volume_init_verbs,
5087                                 alc880_pin_asus_init_verbs },
5088                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
5089                 .dac_nids = alc880_asus_dac_nids,
5090                 .dig_out_nid = ALC880_DIGOUT_NID,
5091                 .num_channel_mode = ARRAY_SIZE(alc880_asus_modes),
5092                 .channel_mode = alc880_asus_modes,
5093                 .need_dac_fix = 1,
5094                 .input_mux = &alc880_capture_source,
5095         },
5096         [ALC880_UNIWILL] = {
5097                 .mixers = { alc880_uniwill_mixer },
5098                 .init_verbs = { alc880_volume_init_verbs,
5099                                 alc880_uniwill_init_verbs },
5100                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
5101                 .dac_nids = alc880_asus_dac_nids,
5102                 .dig_out_nid = ALC880_DIGOUT_NID,
5103                 .num_channel_mode = ARRAY_SIZE(alc880_threestack_modes),
5104                 .channel_mode = alc880_threestack_modes,
5105                 .need_dac_fix = 1,
5106                 .input_mux = &alc880_capture_source,
5107                 .unsol_event = alc880_uniwill_unsol_event,
5108                 .setup = alc880_uniwill_setup,
5109                 .init_hook = alc880_uniwill_init_hook,
5110         },
5111         [ALC880_UNIWILL_P53] = {
5112                 .mixers = { alc880_uniwill_p53_mixer },
5113                 .init_verbs = { alc880_volume_init_verbs,
5114                                 alc880_uniwill_p53_init_verbs },
5115                 .num_dacs = ARRAY_SIZE(alc880_asus_dac_nids),
5116                 .dac_nids = alc880_asus_dac_nids,
5117                 .num_channel_mode = ARRAY_SIZE(alc880_w810_modes),
5118                 .channel_mode = alc880_threestack_modes,
5119                 .input_mux = &alc880_capture_source,
5120                 .unsol_event = alc880_uniwill_p53_unsol_event,
5121                 .setup = alc880_uniwill_p53_setup,
5122                 .init_hook = alc_hp_automute,
5123         },
5124         [ALC880_FUJITSU] = {
5125                 .mixers = { alc880_fujitsu_mixer },
5126                 .init_verbs = { alc880_volume_init_verbs,
5127                                 alc880_uniwill_p53_init_verbs,
5128                                 alc880_beep_init_verbs },
5129                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
5130                 .dac_nids = alc880_dac_nids,
5131                 .dig_out_nid = ALC880_DIGOUT_NID,
5132                 .num_channel_mode = ARRAY_SIZE(alc880_2_jack_modes),
5133                 .channel_mode = alc880_2_jack_modes,
5134                 .input_mux = &alc880_capture_source,
5135                 .unsol_event = alc880_uniwill_p53_unsol_event,
5136                 .setup = alc880_uniwill_p53_setup,
5137                 .init_hook = alc_hp_automute,
5138         },
5139         [ALC880_CLEVO] = {
5140                 .mixers = { alc880_three_stack_mixer },
5141                 .init_verbs = { alc880_volume_init_verbs,
5142                                 alc880_pin_clevo_init_verbs },
5143                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
5144                 .dac_nids = alc880_dac_nids,
5145                 .hp_nid = 0x03,
5146                 .num_channel_mode = ARRAY_SIZE(alc880_threestack_modes),
5147                 .channel_mode = alc880_threestack_modes,
5148                 .need_dac_fix = 1,
5149                 .input_mux = &alc880_capture_source,
5150         },
5151         [ALC880_LG] = {
5152                 .mixers = { alc880_lg_mixer },
5153                 .init_verbs = { alc880_volume_init_verbs,
5154                                 alc880_lg_init_verbs },
5155                 .num_dacs = ARRAY_SIZE(alc880_lg_dac_nids),
5156                 .dac_nids = alc880_lg_dac_nids,
5157                 .dig_out_nid = ALC880_DIGOUT_NID,
5158                 .num_channel_mode = ARRAY_SIZE(alc880_lg_ch_modes),
5159                 .channel_mode = alc880_lg_ch_modes,
5160                 .need_dac_fix = 1,
5161                 .input_mux = &alc880_lg_capture_source,
5162                 .unsol_event = alc_sku_unsol_event,
5163                 .setup = alc880_lg_setup,
5164                 .init_hook = alc_hp_automute,
5165 #ifdef CONFIG_SND_HDA_POWER_SAVE
5166                 .loopbacks = alc880_lg_loopbacks,
5167 #endif
5168         },
5169         [ALC880_LG_LW] = {
5170                 .mixers = { alc880_lg_lw_mixer },
5171                 .init_verbs = { alc880_volume_init_verbs,
5172                                 alc880_lg_lw_init_verbs },
5173                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
5174                 .dac_nids = alc880_dac_nids,
5175                 .dig_out_nid = ALC880_DIGOUT_NID,
5176                 .num_channel_mode = ARRAY_SIZE(alc880_lg_lw_modes),
5177                 .channel_mode = alc880_lg_lw_modes,
5178                 .input_mux = &alc880_lg_lw_capture_source,
5179                 .unsol_event = alc_sku_unsol_event,
5180                 .setup = alc880_lg_lw_setup,
5181                 .init_hook = alc_hp_automute,
5182         },
5183         [ALC880_MEDION_RIM] = {
5184                 .mixers = { alc880_medion_rim_mixer },
5185                 .init_verbs = { alc880_volume_init_verbs,
5186                                 alc880_medion_rim_init_verbs,
5187                                 alc_gpio2_init_verbs },
5188                 .num_dacs = ARRAY_SIZE(alc880_dac_nids),
5189                 .dac_nids = alc880_dac_nids,
5190                 .dig_out_nid = ALC880_DIGOUT_NID,
5191                 .num_channel_mode = ARRAY_SIZE(alc880_2_jack_modes),
5192                 .channel_mode = alc880_2_jack_modes,
5193                 .input_mux = &alc880_medion_rim_capture_source,
5194                 .unsol_event = alc880_medion_rim_unsol_event,
5195                 .setup = alc880_medion_rim_setup,
5196                 .init_hook = alc880_medion_rim_automute,
5197         },
5198 #ifdef CONFIG_SND_DEBUG
5199         [ALC880_TEST] = {
5200                 .mixers = { alc880_test_mixer },
5201                 .init_verbs = { alc880_test_init_verbs },
5202                 .num_dacs = ARRAY_SIZE(alc880_test_dac_nids),
5203                 .dac_nids = alc880_test_dac_nids,
5204                 .dig_out_nid = ALC880_DIGOUT_NID,
5205                 .num_channel_mode = ARRAY_SIZE(alc880_test_modes),
5206                 .channel_mode = alc880_test_modes,
5207                 .input_mux = &alc880_test_capture_source,
5208         },
5209 #endif
5210 };
5211
5212 /*
5213  * Automatic parse of I/O pins from the BIOS configuration
5214  */
5215
5216 enum {
5217         ALC_CTL_WIDGET_VOL,
5218         ALC_CTL_WIDGET_MUTE,
5219         ALC_CTL_BIND_MUTE,
5220 };
5221 static struct snd_kcontrol_new alc880_control_templates[] = {
5222         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
5223         HDA_CODEC_MUTE(NULL, 0, 0, 0),
5224         HDA_BIND_MUTE(NULL, 0, 0, 0),
5225 };
5226
5227 static struct snd_kcontrol_new *alc_kcontrol_new(struct alc_spec *spec)
5228 {
5229         snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
5230         return snd_array_new(&spec->kctls);
5231 }
5232
5233 /* add dynamic controls */
5234 static int add_control(struct alc_spec *spec, int type, const char *name,
5235                        int cidx, unsigned long val)
5236 {
5237         struct snd_kcontrol_new *knew;
5238
5239         knew = alc_kcontrol_new(spec);
5240         if (!knew)
5241                 return -ENOMEM;
5242         *knew = alc880_control_templates[type];
5243         knew->name = kstrdup(name, GFP_KERNEL);
5244         if (!knew->name)
5245                 return -ENOMEM;
5246         knew->index = cidx;
5247         if (get_amp_nid_(val))
5248                 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
5249         knew->private_value = val;
5250         return 0;
5251 }
5252
5253 static int add_control_with_pfx(struct alc_spec *spec, int type,
5254                                 const char *pfx, const char *dir,
5255                                 const char *sfx, int cidx, unsigned long val)
5256 {
5257         char name[32];
5258         snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
5259         return add_control(spec, type, name, cidx, val);
5260 }
5261
5262 #define add_pb_vol_ctrl(spec, type, pfx, val)                   \
5263         add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
5264 #define add_pb_sw_ctrl(spec, type, pfx, val)                    \
5265         add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
5266 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val)                   \
5267         add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
5268 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val)                    \
5269         add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
5270
5271 #define alc880_is_fixed_pin(nid)        ((nid) >= 0x14 && (nid) <= 0x17)
5272 #define alc880_fixed_pin_idx(nid)       ((nid) - 0x14)
5273 #define alc880_is_multi_pin(nid)        ((nid) >= 0x18)
5274 #define alc880_multi_pin_idx(nid)       ((nid) - 0x18)
5275 #define alc880_idx_to_dac(nid)          ((nid) + 0x02)
5276 #define alc880_dac_to_idx(nid)          ((nid) - 0x02)
5277 #define alc880_idx_to_mixer(nid)        ((nid) + 0x0c)
5278 #define alc880_idx_to_selector(nid)     ((nid) + 0x10)
5279 #define ALC880_PIN_CD_NID               0x1c
5280
5281 /* fill in the dac_nids table from the parsed pin configuration */
5282 static int alc880_auto_fill_dac_nids(struct alc_spec *spec,
5283                                      const struct auto_pin_cfg *cfg)
5284 {
5285         hda_nid_t nid;
5286         int assigned[4];
5287         int i, j;
5288
5289         memset(assigned, 0, sizeof(assigned));
5290         spec->multiout.dac_nids = spec->private_dac_nids;
5291
5292         /* check the pins hardwired to audio widget */
5293         for (i = 0; i < cfg->line_outs; i++) {
5294                 nid = cfg->line_out_pins[i];
5295                 if (alc880_is_fixed_pin(nid)) {
5296                         int idx = alc880_fixed_pin_idx(nid);
5297                         spec->multiout.dac_nids[i] = alc880_idx_to_dac(idx);
5298                         assigned[idx] = 1;
5299                 }
5300         }
5301         /* left pins can be connect to any audio widget */
5302         for (i = 0; i < cfg->line_outs; i++) {
5303                 nid = cfg->line_out_pins[i];
5304                 if (alc880_is_fixed_pin(nid))
5305                         continue;
5306                 /* search for an empty channel */
5307                 for (j = 0; j < cfg->line_outs; j++) {
5308                         if (!assigned[j]) {
5309                                 spec->multiout.dac_nids[i] =
5310                                         alc880_idx_to_dac(j);
5311                                 assigned[j] = 1;
5312                                 break;
5313                         }
5314                 }
5315         }
5316         spec->multiout.num_dacs = cfg->line_outs;
5317         return 0;
5318 }
5319
5320 static const char *alc_get_line_out_pfx(struct alc_spec *spec,
5321                                         bool can_be_master)
5322 {
5323         struct auto_pin_cfg *cfg = &spec->autocfg;
5324
5325         if (cfg->line_outs == 1 && !spec->multi_ios &&
5326             !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
5327                 return "Master";
5328
5329         switch (cfg->line_out_type) {
5330         case AUTO_PIN_SPEAKER_OUT:
5331                 if (cfg->line_outs == 1)
5332                         return "Speaker";
5333                 break;
5334         case AUTO_PIN_HP_OUT:
5335                 return "Headphone";
5336         default:
5337                 if (cfg->line_outs == 1 && !spec->multi_ios)
5338                         return "PCM";
5339                 break;
5340         }
5341         return NULL;
5342 }
5343
5344 /* add playback controls from the parsed DAC table */
5345 static int alc880_auto_create_multi_out_ctls(struct alc_spec *spec,
5346                                              const struct auto_pin_cfg *cfg)
5347 {
5348         static const char * const chname[4] = {
5349                 "Front", "Surround", NULL /*CLFE*/, "Side"
5350         };
5351         const char *pfx = alc_get_line_out_pfx(spec, false);
5352         hda_nid_t nid;
5353         int i, err, noutputs;
5354
5355         noutputs = cfg->line_outs;
5356         if (spec->multi_ios > 0)
5357                 noutputs += spec->multi_ios;
5358
5359         for (i = 0; i < noutputs; i++) {
5360                 if (!spec->multiout.dac_nids[i])
5361                         continue;
5362                 nid = alc880_idx_to_mixer(alc880_dac_to_idx(spec->multiout.dac_nids[i]));
5363                 if (!pfx && i == 2) {
5364                         /* Center/LFE */
5365                         err = add_pb_vol_ctrl(spec, ALC_CTL_WIDGET_VOL,
5366                                               "Center",
5367                                           HDA_COMPOSE_AMP_VAL(nid, 1, 0,
5368                                                               HDA_OUTPUT));
5369                         if (err < 0)
5370                                 return err;
5371                         err = add_pb_vol_ctrl(spec, ALC_CTL_WIDGET_VOL,
5372                                               "LFE",
5373                                           HDA_COMPOSE_AMP_VAL(nid, 2, 0,
5374                                                               HDA_OUTPUT));
5375                         if (err < 0)
5376                                 return err;
5377                         err = add_pb_sw_ctrl(spec, ALC_CTL_BIND_MUTE,
5378                                              "Center",
5379                                           HDA_COMPOSE_AMP_VAL(nid, 1, 2,
5380                                                               HDA_INPUT));
5381                         if (err < 0)
5382                                 return err;
5383                         err = add_pb_sw_ctrl(spec, ALC_CTL_BIND_MUTE,
5384                                              "LFE",
5385                                           HDA_COMPOSE_AMP_VAL(nid, 2, 2,
5386                                                               HDA_INPUT));
5387                         if (err < 0)
5388                                 return err;
5389                 } else {
5390                         const char *name = pfx;
5391                         int index = i;
5392                         if (!name) {
5393                                 name = chname[i];
5394                                 index = 0;
5395                         }
5396                         err = __add_pb_vol_ctrl(spec, ALC_CTL_WIDGET_VOL,
5397                                                 name, index,
5398                                           HDA_COMPOSE_AMP_VAL(nid, 3, 0,
5399                                                               HDA_OUTPUT));
5400                         if (err < 0)
5401                                 return err;
5402                         err = __add_pb_sw_ctrl(spec, ALC_CTL_BIND_MUTE,
5403                                                name, index,
5404                                           HDA_COMPOSE_AMP_VAL(nid, 3, 2,
5405                                                               HDA_INPUT));
5406                         if (err < 0)
5407                                 return err;
5408                 }
5409         }
5410         return 0;
5411 }
5412
5413 /* add playback controls for speaker and HP outputs */
5414 static int alc880_auto_create_extra_out(struct alc_spec *spec, hda_nid_t pin,
5415                                         const char *pfx)
5416 {
5417         hda_nid_t nid;
5418         int err;
5419
5420         if (!pin)
5421                 return 0;
5422
5423         if (alc880_is_fixed_pin(pin)) {
5424                 nid = alc880_idx_to_dac(alc880_fixed_pin_idx(pin));
5425                 /* specify the DAC as the extra output */
5426                 if (!spec->multiout.hp_nid)
5427                         spec->multiout.hp_nid = nid;
5428                 else
5429                         spec->multiout.extra_out_nid[0] = nid;
5430                 /* control HP volume/switch on the output mixer amp */
5431                 nid = alc880_idx_to_mixer(alc880_fixed_pin_idx(pin));
5432                 err = add_pb_vol_ctrl(spec, ALC_CTL_WIDGET_VOL, pfx,
5433                                   HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT));
5434                 if (err < 0)
5435                         return err;
5436                 err = add_pb_sw_ctrl(spec, ALC_CTL_BIND_MUTE, pfx,
5437                                   HDA_COMPOSE_AMP_VAL(nid, 3, 2, HDA_INPUT));
5438                 if (err < 0)
5439                         return err;
5440         } else if (alc880_is_multi_pin(pin)) {
5441                 /* set manual connection */
5442                 /* we have only a switch on HP-out PIN */
5443                 err = add_pb_sw_ctrl(spec, ALC_CTL_WIDGET_MUTE, pfx,
5444                                   HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
5445                 if (err < 0)
5446                         return err;
5447         }
5448         return 0;
5449 }
5450
5451 /* create input playback/capture controls for the given pin */
5452 static int new_analog_input(struct alc_spec *spec, hda_nid_t pin,
5453                             const char *ctlname, int ctlidx,
5454                             int idx, hda_nid_t mix_nid)
5455 {
5456         int err;
5457
5458         err = __add_pb_vol_ctrl(spec, ALC_CTL_WIDGET_VOL, ctlname, ctlidx,
5459                           HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
5460         if (err < 0)
5461                 return err;
5462         err = __add_pb_sw_ctrl(spec, ALC_CTL_WIDGET_MUTE, ctlname, ctlidx,
5463                           HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
5464         if (err < 0)
5465                 return err;
5466         return 0;
5467 }
5468
5469 static int alc_is_input_pin(struct hda_codec *codec, hda_nid_t nid)
5470 {
5471         unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
5472         return (pincap & AC_PINCAP_IN) != 0;
5473 }
5474
5475 /* create playback/capture controls for input pins */
5476 static int alc_auto_create_input_ctls(struct hda_codec *codec,
5477                                       const struct auto_pin_cfg *cfg,
5478                                       hda_nid_t mixer,
5479                                       hda_nid_t cap1, hda_nid_t cap2)
5480 {
5481         struct alc_spec *spec = codec->spec;
5482         struct hda_input_mux *imux = &spec->private_imux[0];
5483         int i, err, idx, type_idx = 0;
5484         const char *prev_label = NULL;
5485
5486         for (i = 0; i < cfg->num_inputs; i++) {
5487                 hda_nid_t pin;
5488                 const char *label;
5489
5490                 pin = cfg->inputs[i].pin;
5491                 if (!alc_is_input_pin(codec, pin))
5492                         continue;
5493
5494                 label = hda_get_autocfg_input_label(codec, cfg, i);
5495                 if (prev_label && !strcmp(label, prev_label))
5496                         type_idx++;
5497                 else
5498                         type_idx = 0;
5499                 prev_label = label;
5500
5501                 if (mixer) {
5502                         idx = get_connection_index(codec, mixer, pin);
5503                         if (idx >= 0) {
5504                                 err = new_analog_input(spec, pin,
5505                                                        label, type_idx,
5506                                                        idx, mixer);
5507                                 if (err < 0)
5508                                         return err;
5509                         }
5510                 }
5511
5512                 if (!cap1)
5513                         continue;
5514                 idx = get_connection_index(codec, cap1, pin);
5515                 if (idx < 0 && cap2)
5516                         idx = get_connection_index(codec, cap2, pin);
5517                 if (idx >= 0)
5518                         snd_hda_add_imux_item(imux, label, idx, NULL);
5519         }
5520         return 0;
5521 }
5522
5523 static int alc880_auto_create_input_ctls(struct hda_codec *codec,
5524                                                 const struct auto_pin_cfg *cfg)
5525 {
5526         return alc_auto_create_input_ctls(codec, cfg, 0x0b, 0x08, 0x09);
5527 }
5528
5529 static void alc_set_pin_output(struct hda_codec *codec, hda_nid_t nid,
5530                                unsigned int pin_type)
5531 {
5532         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
5533                             pin_type);
5534         /* unmute pin */
5535         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
5536                             AMP_OUT_UNMUTE);
5537 }
5538
5539 static void alc880_auto_set_output_and_unmute(struct hda_codec *codec,
5540                                               hda_nid_t nid, int pin_type,
5541                                               int dac_idx)
5542 {
5543         alc_set_pin_output(codec, nid, pin_type);
5544         /* need the manual connection? */
5545         if (alc880_is_multi_pin(nid)) {
5546                 struct alc_spec *spec = codec->spec;
5547                 int idx = alc880_multi_pin_idx(nid);
5548                 snd_hda_codec_write(codec, alc880_idx_to_selector(idx), 0,
5549                                     AC_VERB_SET_CONNECT_SEL,
5550                                     alc880_dac_to_idx(spec->multiout.dac_nids[dac_idx]));
5551         }
5552 }
5553
5554 static int get_pin_type(int line_out_type)
5555 {
5556         if (line_out_type == AUTO_PIN_HP_OUT)
5557                 return PIN_HP;
5558         else
5559                 return PIN_OUT;
5560 }
5561
5562 static void alc880_auto_init_multi_out(struct hda_codec *codec)
5563 {
5564         struct alc_spec *spec = codec->spec;
5565         int i;
5566
5567         for (i = 0; i < spec->autocfg.line_outs; i++) {
5568                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
5569                 int pin_type = get_pin_type(spec->autocfg.line_out_type);
5570                 alc880_auto_set_output_and_unmute(codec, nid, pin_type, i);
5571         }
5572 }
5573
5574 static void alc880_auto_init_extra_out(struct hda_codec *codec)
5575 {
5576         struct alc_spec *spec = codec->spec;
5577         hda_nid_t pin;
5578
5579         pin = spec->autocfg.speaker_pins[0];
5580         if (pin) /* connect to front */
5581                 alc880_auto_set_output_and_unmute(codec, pin, PIN_OUT, 0);
5582         pin = spec->autocfg.hp_pins[0];
5583         if (pin) /* connect to front */
5584                 alc880_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
5585 }
5586
5587 static void alc880_auto_init_analog_input(struct hda_codec *codec)
5588 {
5589         struct alc_spec *spec = codec->spec;
5590         struct auto_pin_cfg *cfg = &spec->autocfg;
5591         int i;
5592
5593         for (i = 0; i < cfg->num_inputs; i++) {
5594                 hda_nid_t nid = cfg->inputs[i].pin;
5595                 if (alc_is_input_pin(codec, nid)) {
5596                         alc_set_input_pin(codec, nid, cfg->inputs[i].type);
5597                         if (nid != ALC880_PIN_CD_NID &&
5598                             (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP))
5599                                 snd_hda_codec_write(codec, nid, 0,
5600                                                     AC_VERB_SET_AMP_GAIN_MUTE,
5601                                                     AMP_OUT_MUTE);
5602                 }
5603         }
5604 }
5605
5606 static void alc880_auto_init_input_src(struct hda_codec *codec)
5607 {
5608         struct alc_spec *spec = codec->spec;
5609         int c;
5610
5611         for (c = 0; c < spec->num_adc_nids; c++) {
5612                 unsigned int mux_idx;
5613                 const struct hda_input_mux *imux;
5614                 mux_idx = c >= spec->num_mux_defs ? 0 : c;
5615                 imux = &spec->input_mux[mux_idx];
5616                 if (!imux->num_items && mux_idx > 0)
5617                         imux = &spec->input_mux[0];
5618                 if (imux)
5619                         snd_hda_codec_write(codec, spec->adc_nids[c], 0,
5620                                             AC_VERB_SET_CONNECT_SEL,
5621                                             imux->items[0].index);
5622         }
5623 }
5624
5625 static int alc_auto_add_multi_channel_mode(struct hda_codec *codec);
5626
5627 /* parse the BIOS configuration and set up the alc_spec */
5628 /* return 1 if successful, 0 if the proper config is not found,
5629  * or a negative error code
5630  */
5631 static int alc880_parse_auto_config(struct hda_codec *codec)
5632 {
5633         struct alc_spec *spec = codec->spec;
5634         int err;
5635         static hda_nid_t alc880_ignore[] = { 0x1d, 0 };
5636
5637         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg,
5638                                            alc880_ignore);
5639         if (err < 0)
5640                 return err;
5641         if (!spec->autocfg.line_outs)
5642                 return 0; /* can't find valid BIOS pin config */
5643
5644         err = alc880_auto_fill_dac_nids(spec, &spec->autocfg);
5645         if (err < 0)
5646                 return err;
5647         err = alc_auto_add_multi_channel_mode(codec);
5648         if (err < 0)
5649                 return err;
5650         err = alc880_auto_create_multi_out_ctls(spec, &spec->autocfg);
5651         if (err < 0)
5652                 return err;
5653         err = alc880_auto_create_extra_out(spec,
5654                                            spec->autocfg.speaker_pins[0],
5655                                            "Speaker");
5656         if (err < 0)
5657                 return err;
5658         err = alc880_auto_create_extra_out(spec, spec->autocfg.hp_pins[0],
5659                                            "Headphone");
5660         if (err < 0)
5661                 return err;
5662         err = alc880_auto_create_input_ctls(codec, &spec->autocfg);
5663         if (err < 0)
5664                 return err;
5665
5666         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
5667
5668         alc_auto_parse_digital(codec);
5669
5670         if (spec->kctls.list)
5671                 add_mixer(spec, spec->kctls.list);
5672
5673         add_verb(spec, alc880_volume_init_verbs);
5674
5675         spec->num_mux_defs = 1;
5676         spec->input_mux = &spec->private_imux[0];
5677
5678         alc_ssid_check(codec, 0x15, 0x1b, 0x14, 0);
5679
5680         return 1;
5681 }
5682
5683 /* additional initialization for auto-configuration model */
5684 static void alc880_auto_init(struct hda_codec *codec)
5685 {
5686         struct alc_spec *spec = codec->spec;
5687         alc880_auto_init_multi_out(codec);
5688         alc880_auto_init_extra_out(codec);
5689         alc880_auto_init_analog_input(codec);
5690         alc880_auto_init_input_src(codec);
5691         alc_auto_init_digital(codec);
5692         if (spec->unsol_event)
5693                 alc_inithook(codec);
5694 }
5695
5696 /* check the ADC/MUX contains all input pins; some ADC/MUX contains only
5697  * one of two digital mic pins, e.g. on ALC272
5698  */
5699 static void fixup_automic_adc(struct hda_codec *codec)
5700 {
5701         struct alc_spec *spec = codec->spec;
5702         int i;
5703
5704         for (i = 0; i < spec->num_adc_nids; i++) {
5705                 hda_nid_t cap = spec->capsrc_nids ?
5706                         spec->capsrc_nids[i] : spec->adc_nids[i];
5707                 int iidx, eidx;
5708
5709                 iidx = get_connection_index(codec, cap, spec->int_mic.pin);
5710                 if (iidx < 0)
5711                         continue;
5712                 eidx = get_connection_index(codec, cap, spec->ext_mic.pin);
5713                 if (eidx < 0)
5714                         continue;
5715                 spec->int_mic.mux_idx = iidx;
5716                 spec->ext_mic.mux_idx = eidx;
5717                 if (spec->capsrc_nids)
5718                         spec->capsrc_nids += i;
5719                 spec->adc_nids += i;
5720                 spec->num_adc_nids = 1;
5721                 return;
5722         }
5723         snd_printd(KERN_INFO "hda_codec: %s: "
5724                    "No ADC/MUX containing both 0x%x and 0x%x pins\n",
5725                    codec->chip_name, spec->int_mic.pin, spec->ext_mic.pin);
5726         spec->auto_mic = 0; /* disable auto-mic to be sure */
5727 }
5728
5729 /* select or unmute the given capsrc route */
5730 static void select_or_unmute_capsrc(struct hda_codec *codec, hda_nid_t cap,
5731                                     int idx)
5732 {
5733         if (get_wcaps_type(get_wcaps(codec, cap)) == AC_WID_AUD_MIX) {
5734                 snd_hda_codec_amp_stereo(codec, cap, HDA_INPUT, idx,
5735                                          HDA_AMP_MUTE, 0);
5736         } else {
5737                 snd_hda_codec_write_cache(codec, cap, 0,
5738                                           AC_VERB_SET_CONNECT_SEL, idx);
5739         }
5740 }
5741
5742 /* set the default connection to that pin */
5743 static int init_capsrc_for_pin(struct hda_codec *codec, hda_nid_t pin)
5744 {
5745         struct alc_spec *spec = codec->spec;
5746         int i;
5747
5748         for (i = 0; i < spec->num_adc_nids; i++) {
5749                 hda_nid_t cap = spec->capsrc_nids ?
5750                         spec->capsrc_nids[i] : spec->adc_nids[i];
5751                 int idx;
5752
5753                 idx = get_connection_index(codec, cap, pin);
5754                 if (idx < 0)
5755                         continue;
5756                 select_or_unmute_capsrc(codec, cap, idx);
5757                 return i; /* return the found index */
5758         }
5759         return -1; /* not found */
5760 }
5761
5762 /* choose the ADC/MUX containing the input pin and initialize the setup */
5763 static void fixup_single_adc(struct hda_codec *codec)
5764 {
5765         struct alc_spec *spec = codec->spec;
5766         struct auto_pin_cfg *cfg = &spec->autocfg;
5767         int i;
5768
5769         /* search for the input pin; there must be only one */
5770         if (cfg->num_inputs != 1)
5771                 return;
5772         i = init_capsrc_for_pin(codec, cfg->inputs[0].pin);
5773         if (i >= 0) {
5774                 /* use only this ADC */
5775                 if (spec->capsrc_nids)
5776                         spec->capsrc_nids += i;
5777                 spec->adc_nids += i;
5778                 spec->num_adc_nids = 1;
5779                 spec->single_input_src = 1;
5780         }
5781 }
5782
5783 /* initialize dual adcs */
5784 static void fixup_dual_adc_switch(struct hda_codec *codec)
5785 {
5786         struct alc_spec *spec = codec->spec;
5787         init_capsrc_for_pin(codec, spec->ext_mic.pin);
5788         init_capsrc_for_pin(codec, spec->int_mic.pin);
5789 }
5790
5791 /* initialize some special cases for input sources */
5792 static void alc_init_special_input_src(struct hda_codec *codec)
5793 {
5794         struct alc_spec *spec = codec->spec;
5795         if (spec->dual_adc_switch)
5796                 fixup_dual_adc_switch(codec);
5797         else if (spec->single_input_src)
5798                 init_capsrc_for_pin(codec, spec->autocfg.inputs[0].pin);
5799 }
5800
5801 static void set_capture_mixer(struct hda_codec *codec)
5802 {
5803         struct alc_spec *spec = codec->spec;
5804         static struct snd_kcontrol_new *caps[2][3] = {
5805                 { alc_capture_mixer_nosrc1,
5806                   alc_capture_mixer_nosrc2,
5807                   alc_capture_mixer_nosrc3 },
5808                 { alc_capture_mixer1,
5809                   alc_capture_mixer2,
5810                   alc_capture_mixer3 },
5811         };
5812         if (spec->num_adc_nids > 0 && spec->num_adc_nids <= 3) {
5813                 int mux = 0;
5814                 int num_adcs = spec->num_adc_nids;
5815                 if (spec->dual_adc_switch)
5816                         num_adcs = 1;
5817                 else if (spec->auto_mic)
5818                         fixup_automic_adc(codec);
5819                 else if (spec->input_mux) {
5820                         if (spec->input_mux->num_items > 1)
5821                                 mux = 1;
5822                         else if (spec->input_mux->num_items == 1)
5823                                 fixup_single_adc(codec);
5824                 }
5825                 spec->cap_mixer = caps[mux][num_adcs - 1];
5826         }
5827 }
5828
5829 /* fill adc_nids (and capsrc_nids) containing all active input pins */
5830 static void fillup_priv_adc_nids(struct hda_codec *codec, hda_nid_t *nids,
5831                                  int num_nids)
5832 {
5833         struct alc_spec *spec = codec->spec;
5834         struct auto_pin_cfg *cfg = &spec->autocfg;
5835         int n;
5836         hda_nid_t fallback_adc = 0, fallback_cap = 0;
5837
5838         for (n = 0; n < num_nids; n++) {
5839                 hda_nid_t adc, cap;
5840                 hda_nid_t conn[HDA_MAX_NUM_INPUTS];
5841                 int nconns, i, j;
5842
5843                 adc = nids[n];
5844                 if (get_wcaps_type(get_wcaps(codec, adc)) != AC_WID_AUD_IN)
5845                         continue;
5846                 cap = adc;
5847                 nconns = snd_hda_get_connections(codec, cap, conn,
5848                                                  ARRAY_SIZE(conn));
5849                 if (nconns == 1) {
5850                         cap = conn[0];
5851                         nconns = snd_hda_get_connections(codec, cap, conn,
5852                                                          ARRAY_SIZE(conn));
5853                 }
5854                 if (nconns <= 0)
5855                         continue;
5856                 if (!fallback_adc) {
5857                         fallback_adc = adc;
5858                         fallback_cap = cap;
5859                 }
5860                 for (i = 0; i < cfg->num_inputs; i++) {
5861                         hda_nid_t nid = cfg->inputs[i].pin;
5862                         for (j = 0; j < nconns; j++) {
5863                                 if (conn[j] == nid)
5864                                         break;
5865                         }
5866                         if (j >= nconns)
5867                                 break;
5868                 }
5869                 if (i >= cfg->num_inputs) {
5870                         int num_adcs = spec->num_adc_nids;
5871                         spec->private_adc_nids[num_adcs] = adc;
5872                         spec->private_capsrc_nids[num_adcs] = cap;
5873                         spec->num_adc_nids++;
5874                         spec->adc_nids = spec->private_adc_nids;
5875                         if (adc != cap)
5876                                 spec->capsrc_nids = spec->private_capsrc_nids;
5877                 }
5878         }
5879         if (!spec->num_adc_nids) {
5880                 printk(KERN_WARNING "hda_codec: %s: no valid ADC found;"
5881                        " using fallback 0x%x\n",
5882                        codec->chip_name, fallback_adc);
5883                 spec->private_adc_nids[0] = fallback_adc;
5884                 spec->adc_nids = spec->private_adc_nids;
5885                 if (fallback_adc != fallback_cap) {
5886                         spec->private_capsrc_nids[0] = fallback_cap;
5887                         spec->capsrc_nids = spec->private_adc_nids;
5888                 }
5889         }
5890 }
5891
5892 #ifdef CONFIG_SND_HDA_INPUT_BEEP
5893 #define set_beep_amp(spec, nid, idx, dir) \
5894         ((spec)->beep_amp = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir))
5895
5896 static struct snd_pci_quirk beep_white_list[] = {
5897         SND_PCI_QUIRK(0x1043, 0x829f, "ASUS", 1),
5898         SND_PCI_QUIRK(0x1043, 0x83ce, "EeePC", 1),
5899         SND_PCI_QUIRK(0x1043, 0x831a, "EeePC", 1),
5900         SND_PCI_QUIRK(0x8086, 0xd613, "Intel", 1),
5901         {}
5902 };
5903
5904 static inline int has_cdefine_beep(struct hda_codec *codec)
5905 {
5906         struct alc_spec *spec = codec->spec;
5907         const struct snd_pci_quirk *q;
5908         q = snd_pci_quirk_lookup(codec->bus->pci, beep_white_list);
5909         if (q)
5910                 return q->value;
5911         return spec->cdefine.enable_pcbeep;
5912 }
5913 #else
5914 #define set_beep_amp(spec, nid, idx, dir) /* NOP */
5915 #define has_cdefine_beep(codec)         0
5916 #endif
5917
5918 /*
5919  * OK, here we have finally the patch for ALC880
5920  */
5921
5922 static int patch_alc880(struct hda_codec *codec)
5923 {
5924         struct alc_spec *spec;
5925         int board_config;
5926         int err;
5927
5928         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5929         if (spec == NULL)
5930                 return -ENOMEM;
5931
5932         codec->spec = spec;
5933
5934         board_config = snd_hda_check_board_config(codec, ALC880_MODEL_LAST,
5935                                                   alc880_models,
5936                                                   alc880_cfg_tbl);
5937         if (board_config < 0) {
5938                 printk(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
5939                        codec->chip_name);
5940                 board_config = ALC880_AUTO;
5941         }
5942
5943         if (board_config == ALC880_AUTO) {
5944                 /* automatic parse from the BIOS config */
5945                 err = alc880_parse_auto_config(codec);
5946                 if (err < 0) {
5947                         alc_free(codec);
5948                         return err;
5949                 } else if (!err) {
5950                         printk(KERN_INFO
5951                                "hda_codec: Cannot set up configuration "
5952                                "from BIOS.  Using 3-stack mode...\n");