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