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