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