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