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