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