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