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