ALSA: hda - do not detect jack on internal speakers for Realtek
[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 Realtek ALC 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 <linux/module.h>
31 #include <sound/core.h>
32 #include <sound/jack.h>
33 #include "hda_codec.h"
34 #include "hda_local.h"
35 #include "hda_beep.h"
36 #include "hda_jack.h"
37
38 /* unsol event tags */
39 #define ALC_FRONT_EVENT         0x01
40 #define ALC_DCVOL_EVENT         0x02
41 #define ALC_HP_EVENT            0x04
42 #define ALC_MIC_EVENT           0x08
43
44 /* for GPIO Poll */
45 #define GPIO_MASK       0x03
46
47 /* extra amp-initialization sequence types */
48 enum {
49         ALC_INIT_NONE,
50         ALC_INIT_DEFAULT,
51         ALC_INIT_GPIO1,
52         ALC_INIT_GPIO2,
53         ALC_INIT_GPIO3,
54 };
55
56 struct alc_customize_define {
57         unsigned int  sku_cfg;
58         unsigned char port_connectivity;
59         unsigned char check_sum;
60         unsigned char customization;
61         unsigned char external_amp;
62         unsigned int  enable_pcbeep:1;
63         unsigned int  platform_type:1;
64         unsigned int  swap:1;
65         unsigned int  override:1;
66         unsigned int  fixup:1; /* Means that this sku is set by driver, not read from hw */
67 };
68
69 struct alc_fixup;
70
71 struct alc_multi_io {
72         hda_nid_t pin;          /* multi-io widget pin NID */
73         hda_nid_t dac;          /* DAC to be connected */
74         unsigned int ctl_in;    /* cached input-pin control value */
75 };
76
77 enum {
78         ALC_AUTOMUTE_PIN,       /* change the pin control */
79         ALC_AUTOMUTE_AMP,       /* mute/unmute the pin AMP */
80         ALC_AUTOMUTE_MIXER,     /* mute/unmute mixer widget AMP */
81 };
82
83 #define MAX_VOL_NIDS    0x40
84
85 struct alc_spec {
86         /* codec parameterization */
87         const struct snd_kcontrol_new *mixers[5];       /* mixer arrays */
88         unsigned int num_mixers;
89         const struct snd_kcontrol_new *cap_mixer;       /* capture mixer */
90         unsigned int beep_amp;  /* beep amp value, set via set_beep_amp() */
91
92         const struct hda_verb *init_verbs[10];  /* initialization verbs
93                                                  * don't forget NULL
94                                                  * termination!
95                                                  */
96         unsigned int num_init_verbs;
97
98         char stream_name_analog[32];    /* analog PCM stream */
99         const struct hda_pcm_stream *stream_analog_playback;
100         const struct hda_pcm_stream *stream_analog_capture;
101         const struct hda_pcm_stream *stream_analog_alt_playback;
102         const struct hda_pcm_stream *stream_analog_alt_capture;
103
104         char stream_name_digital[32];   /* digital PCM stream */
105         const struct hda_pcm_stream *stream_digital_playback;
106         const struct hda_pcm_stream *stream_digital_capture;
107
108         /* playback */
109         struct hda_multi_out multiout;  /* playback set-up
110                                          * max_channels, dacs must be set
111                                          * dig_out_nid and hp_nid are optional
112                                          */
113         hda_nid_t alt_dac_nid;
114         hda_nid_t slave_dig_outs[3];    /* optional - for auto-parsing */
115         int dig_out_type;
116
117         /* capture */
118         unsigned int num_adc_nids;
119         const hda_nid_t *adc_nids;
120         const hda_nid_t *capsrc_nids;
121         hda_nid_t dig_in_nid;           /* digital-in NID; optional */
122         hda_nid_t mixer_nid;            /* analog-mixer NID */
123         DECLARE_BITMAP(vol_ctls, MAX_VOL_NIDS << 1);
124         DECLARE_BITMAP(sw_ctls, MAX_VOL_NIDS << 1);
125
126         /* capture setup for dynamic dual-adc switch */
127         hda_nid_t cur_adc;
128         unsigned int cur_adc_stream_tag;
129         unsigned int cur_adc_format;
130
131         /* capture source */
132         unsigned int num_mux_defs;
133         const struct hda_input_mux *input_mux;
134         unsigned int cur_mux[3];
135         hda_nid_t ext_mic_pin;
136         hda_nid_t dock_mic_pin;
137         hda_nid_t int_mic_pin;
138
139         /* channel model */
140         const struct hda_channel_mode *channel_mode;
141         int num_channel_mode;
142         int need_dac_fix;
143         int const_channel_count;
144         int ext_channel_count;
145
146         /* PCM information */
147         struct hda_pcm pcm_rec[3];      /* used in alc_build_pcms() */
148
149         /* dynamic controls, init_verbs and input_mux */
150         struct auto_pin_cfg autocfg;
151         struct alc_customize_define cdefine;
152         struct snd_array kctls;
153         struct hda_input_mux private_imux[3];
154         hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
155         hda_nid_t private_adc_nids[AUTO_CFG_MAX_OUTS];
156         hda_nid_t private_capsrc_nids[AUTO_CFG_MAX_OUTS];
157         hda_nid_t imux_pins[HDA_MAX_NUM_INPUTS];
158         unsigned int dyn_adc_idx[HDA_MAX_NUM_INPUTS];
159         int int_mic_idx, ext_mic_idx, dock_mic_idx; /* for auto-mic */
160
161         /* hooks */
162         void (*init_hook)(struct hda_codec *codec);
163         void (*unsol_event)(struct hda_codec *codec, unsigned int res);
164 #ifdef CONFIG_SND_HDA_POWER_SAVE
165         void (*power_hook)(struct hda_codec *codec);
166 #endif
167         void (*shutup)(struct hda_codec *codec);
168         void (*automute_hook)(struct hda_codec *codec);
169
170         /* for pin sensing */
171         unsigned int hp_jack_present:1;
172         unsigned int line_jack_present:1;
173         unsigned int master_mute:1;
174         unsigned int auto_mic:1;
175         unsigned int auto_mic_valid_imux:1;     /* valid imux for auto-mic */
176         unsigned int automute_speaker:1; /* automute speaker outputs */
177         unsigned int automute_lo:1; /* automute LO outputs */
178         unsigned int detect_hp:1;       /* Headphone detection enabled */
179         unsigned int detect_lo:1;       /* Line-out detection enabled */
180         unsigned int automute_speaker_possible:1; /* there are speakers and either LO or HP */
181         unsigned int automute_lo_possible:1;      /* there are line outs and HP */
182         unsigned int keep_vref_in_automute:1; /* Don't clear VREF in automute */
183
184         /* other flags */
185         unsigned int no_analog :1; /* digital I/O only */
186         unsigned int dyn_adc_switch:1; /* switch ADCs (for ALC275) */
187         unsigned int single_input_src:1;
188         unsigned int vol_in_capsrc:1; /* use capsrc volume (ADC has no vol) */
189         unsigned int parse_flags; /* passed to snd_hda_parse_pin_defcfg() */
190         unsigned int shared_mic_hp:1; /* HP/Mic-in sharing */
191         unsigned int no_primary_hp:1; /* Don't prefer HP pins to speaker pins */
192
193         /* auto-mute control */
194         int automute_mode;
195         hda_nid_t automute_mixer_nid[AUTO_CFG_MAX_OUTS];
196
197         int init_amp;
198         int codec_variant;      /* flag for other variants */
199
200         /* for virtual master */
201         hda_nid_t vmaster_nid;
202         struct hda_vmaster_mute_hook vmaster_mute;
203 #ifdef CONFIG_SND_HDA_POWER_SAVE
204         struct hda_loopback_check loopback;
205         int num_loopbacks;
206         struct hda_amp_list loopback_list[8];
207 #endif
208
209         /* for PLL fix */
210         hda_nid_t pll_nid;
211         unsigned int pll_coef_idx, pll_coef_bit;
212         unsigned int coef0;
213
214         /* fix-up list */
215         int fixup_id;
216         const struct alc_fixup *fixup_list;
217         const char *fixup_name;
218
219         /* multi-io */
220         int multi_ios;
221         struct alc_multi_io multi_io[4];
222
223         /* bind volumes */
224         struct snd_array bind_ctls;
225 };
226
227 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
228                            int dir, unsigned int bits)
229 {
230         if (!nid)
231                 return false;
232         if (get_wcaps(codec, nid) & (1 << (dir + 1)))
233                 if (query_amp_caps(codec, nid, dir) & bits)
234                         return true;
235         return false;
236 }
237
238 #define nid_has_mute(codec, nid, dir) \
239         check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
240 #define nid_has_volume(codec, nid, dir) \
241         check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
242
243 /*
244  * input MUX handling
245  */
246 static int alc_mux_enum_info(struct snd_kcontrol *kcontrol,
247                              struct snd_ctl_elem_info *uinfo)
248 {
249         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
250         struct alc_spec *spec = codec->spec;
251         unsigned int mux_idx = snd_ctl_get_ioffidx(kcontrol, &uinfo->id);
252         if (mux_idx >= spec->num_mux_defs)
253                 mux_idx = 0;
254         if (!spec->input_mux[mux_idx].num_items && mux_idx > 0)
255                 mux_idx = 0;
256         return snd_hda_input_mux_info(&spec->input_mux[mux_idx], uinfo);
257 }
258
259 static int alc_mux_enum_get(struct snd_kcontrol *kcontrol,
260                             struct snd_ctl_elem_value *ucontrol)
261 {
262         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
263         struct alc_spec *spec = codec->spec;
264         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
265
266         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
267         return 0;
268 }
269
270 static bool alc_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
271 {
272         struct alc_spec *spec = codec->spec;
273         hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
274
275         if (spec->cur_adc && spec->cur_adc != new_adc) {
276                 /* stream is running, let's swap the current ADC */
277                 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
278                 spec->cur_adc = new_adc;
279                 snd_hda_codec_setup_stream(codec, new_adc,
280                                            spec->cur_adc_stream_tag, 0,
281                                            spec->cur_adc_format);
282                 return true;
283         }
284         return false;
285 }
286
287 static inline hda_nid_t get_capsrc(struct alc_spec *spec, int idx)
288 {
289         return spec->capsrc_nids ?
290                 spec->capsrc_nids[idx] : spec->adc_nids[idx];
291 }
292
293 static void call_update_outputs(struct hda_codec *codec);
294
295 /* select the given imux item; either unmute exclusively or select the route */
296 static int alc_mux_select(struct hda_codec *codec, unsigned int adc_idx,
297                           unsigned int idx, bool force)
298 {
299         struct alc_spec *spec = codec->spec;
300         const struct hda_input_mux *imux;
301         unsigned int mux_idx;
302         int i, type, num_conns;
303         hda_nid_t nid;
304
305         if (!spec->input_mux)
306                 return 0;
307
308         mux_idx = adc_idx >= spec->num_mux_defs ? 0 : adc_idx;
309         imux = &spec->input_mux[mux_idx];
310         if (!imux->num_items && mux_idx > 0)
311                 imux = &spec->input_mux[0];
312         if (!imux->num_items)
313                 return 0;
314
315         if (idx >= imux->num_items)
316                 idx = imux->num_items - 1;
317         if (spec->cur_mux[adc_idx] == idx && !force)
318                 return 0;
319         spec->cur_mux[adc_idx] = idx;
320
321         /* for shared I/O, change the pin-control accordingly */
322         if (spec->shared_mic_hp) {
323                 /* NOTE: this assumes that there are only two inputs, the
324                  * first is the real internal mic and the second is HP jack.
325                  */
326                 snd_hda_codec_write(codec, spec->autocfg.inputs[1].pin, 0,
327                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
328                                     spec->cur_mux[adc_idx] ?
329                                     PIN_VREF80 : PIN_HP);
330                 spec->automute_speaker = !spec->cur_mux[adc_idx];
331                 call_update_outputs(codec);
332         }
333
334         if (spec->dyn_adc_switch) {
335                 alc_dyn_adc_pcm_resetup(codec, idx);
336                 adc_idx = spec->dyn_adc_idx[idx];
337         }
338
339         nid = get_capsrc(spec, adc_idx);
340
341         /* no selection? */
342         num_conns = snd_hda_get_conn_list(codec, nid, NULL);
343         if (num_conns <= 1)
344                 return 1;
345
346         type = get_wcaps_type(get_wcaps(codec, nid));
347         if (type == AC_WID_AUD_MIX) {
348                 /* Matrix-mixer style (e.g. ALC882) */
349                 int active = imux->items[idx].index;
350                 for (i = 0; i < num_conns; i++) {
351                         unsigned int v = (i == active) ? 0 : HDA_AMP_MUTE;
352                         snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, i,
353                                                  HDA_AMP_MUTE, v);
354                 }
355         } else {
356                 /* MUX style (e.g. ALC880) */
357                 snd_hda_codec_write_cache(codec, nid, 0,
358                                           AC_VERB_SET_CONNECT_SEL,
359                                           imux->items[idx].index);
360         }
361         return 1;
362 }
363
364 static int alc_mux_enum_put(struct snd_kcontrol *kcontrol,
365                             struct snd_ctl_elem_value *ucontrol)
366 {
367         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
368         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
369         return alc_mux_select(codec, adc_idx,
370                               ucontrol->value.enumerated.item[0], false);
371 }
372
373 /*
374  * set up the input pin config (depending on the given auto-pin type)
375  */
376 static void alc_set_input_pin(struct hda_codec *codec, hda_nid_t nid,
377                               int auto_pin_type)
378 {
379         unsigned int val = PIN_IN;
380
381         if (auto_pin_type == AUTO_PIN_MIC) {
382                 unsigned int pincap;
383                 unsigned int oldval;
384                 oldval = snd_hda_codec_read(codec, nid, 0,
385                                             AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
386                 pincap = snd_hda_query_pin_caps(codec, nid);
387                 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
388                 /* if the default pin setup is vref50, we give it priority */
389                 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
390                         val = PIN_VREF80;
391                 else if (pincap & AC_PINCAP_VREF_50)
392                         val = PIN_VREF50;
393                 else if (pincap & AC_PINCAP_VREF_100)
394                         val = PIN_VREF100;
395                 else if (pincap & AC_PINCAP_VREF_GRD)
396                         val = PIN_VREFGRD;
397         }
398         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, val);
399 }
400
401 /*
402  * Append the given mixer and verb elements for the later use
403  * The mixer array is referred in build_controls(), and init_verbs are
404  * called in init().
405  */
406 static void add_mixer(struct alc_spec *spec, const struct snd_kcontrol_new *mix)
407 {
408         if (snd_BUG_ON(spec->num_mixers >= ARRAY_SIZE(spec->mixers)))
409                 return;
410         spec->mixers[spec->num_mixers++] = mix;
411 }
412
413 static void add_verb(struct alc_spec *spec, const struct hda_verb *verb)
414 {
415         if (snd_BUG_ON(spec->num_init_verbs >= ARRAY_SIZE(spec->init_verbs)))
416                 return;
417         spec->init_verbs[spec->num_init_verbs++] = verb;
418 }
419
420 /*
421  * GPIO setup tables, used in initialization
422  */
423 /* Enable GPIO mask and set output */
424 static const struct hda_verb alc_gpio1_init_verbs[] = {
425         {0x01, AC_VERB_SET_GPIO_MASK, 0x01},
426         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
427         {0x01, AC_VERB_SET_GPIO_DATA, 0x01},
428         { }
429 };
430
431 static const struct hda_verb alc_gpio2_init_verbs[] = {
432         {0x01, AC_VERB_SET_GPIO_MASK, 0x02},
433         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
434         {0x01, AC_VERB_SET_GPIO_DATA, 0x02},
435         { }
436 };
437
438 static const struct hda_verb alc_gpio3_init_verbs[] = {
439         {0x01, AC_VERB_SET_GPIO_MASK, 0x03},
440         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x03},
441         {0x01, AC_VERB_SET_GPIO_DATA, 0x03},
442         { }
443 };
444
445 /*
446  * Fix hardware PLL issue
447  * On some codecs, the analog PLL gating control must be off while
448  * the default value is 1.
449  */
450 static void alc_fix_pll(struct hda_codec *codec)
451 {
452         struct alc_spec *spec = codec->spec;
453         unsigned int val;
454
455         if (!spec->pll_nid)
456                 return;
457         snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
458                             spec->pll_coef_idx);
459         val = snd_hda_codec_read(codec, spec->pll_nid, 0,
460                                  AC_VERB_GET_PROC_COEF, 0);
461         snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
462                             spec->pll_coef_idx);
463         snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_PROC_COEF,
464                             val & ~(1 << spec->pll_coef_bit));
465 }
466
467 static void alc_fix_pll_init(struct hda_codec *codec, hda_nid_t nid,
468                              unsigned int coef_idx, unsigned int coef_bit)
469 {
470         struct alc_spec *spec = codec->spec;
471         spec->pll_nid = nid;
472         spec->pll_coef_idx = coef_idx;
473         spec->pll_coef_bit = coef_bit;
474         alc_fix_pll(codec);
475 }
476
477 /*
478  * Jack detections for HP auto-mute and mic-switch
479  */
480
481 /* check each pin in the given array; returns true if any of them is plugged */
482 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
483 {
484         int i, present = 0;
485
486         for (i = 0; i < num_pins; i++) {
487                 hda_nid_t nid = pins[i];
488                 if (!nid)
489                         break;
490                 present |= snd_hda_jack_detect(codec, nid);
491         }
492         return present;
493 }
494
495 /* standard HP/line-out auto-mute helper */
496 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
497                         bool mute, bool hp_out)
498 {
499         struct alc_spec *spec = codec->spec;
500         unsigned int mute_bits = mute ? HDA_AMP_MUTE : 0;
501         unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
502         int i;
503
504         for (i = 0; i < num_pins; i++) {
505                 hda_nid_t nid = pins[i];
506                 unsigned int val;
507                 if (!nid)
508                         break;
509                 switch (spec->automute_mode) {
510                 case ALC_AUTOMUTE_PIN:
511                         /* don't reset VREF value in case it's controlling
512                          * the amp (see alc861_fixup_asus_amp_vref_0f())
513                          */
514                         if (spec->keep_vref_in_automute) {
515                                 val = snd_hda_codec_read(codec, nid, 0,
516                                         AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
517                                 val &= ~PIN_HP;
518                         } else
519                                 val = 0;
520                         val |= pin_bits;
521                         snd_hda_codec_write(codec, nid, 0,
522                                             AC_VERB_SET_PIN_WIDGET_CONTROL,
523                                             val);
524                         break;
525                 case ALC_AUTOMUTE_AMP:
526                         snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
527                                                  HDA_AMP_MUTE, mute_bits);
528                         break;
529                 case ALC_AUTOMUTE_MIXER:
530                         nid = spec->automute_mixer_nid[i];
531                         if (!nid)
532                                 break;
533                         snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 0,
534                                                  HDA_AMP_MUTE, mute_bits);
535                         snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 1,
536                                                  HDA_AMP_MUTE, mute_bits);
537                         break;
538                 }
539         }
540 }
541
542 /* Toggle outputs muting */
543 static void update_outputs(struct hda_codec *codec)
544 {
545         struct alc_spec *spec = codec->spec;
546         int on;
547
548         /* Control HP pins/amps depending on master_mute state;
549          * in general, HP pins/amps control should be enabled in all cases,
550          * but currently set only for master_mute, just to be safe
551          */
552         if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
553                 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
554                     spec->autocfg.hp_pins, spec->master_mute, true);
555
556         if (!spec->automute_speaker)
557                 on = 0;
558         else
559                 on = spec->hp_jack_present | spec->line_jack_present;
560         on |= spec->master_mute;
561         do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
562                     spec->autocfg.speaker_pins, on, false);
563
564         /* toggle line-out mutes if needed, too */
565         /* if LO is a copy of either HP or Speaker, don't need to handle it */
566         if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
567             spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
568                 return;
569         if (!spec->automute_lo)
570                 on = 0;
571         else
572                 on = spec->hp_jack_present;
573         on |= spec->master_mute;
574         do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
575                     spec->autocfg.line_out_pins, on, false);
576 }
577
578 static void call_update_outputs(struct hda_codec *codec)
579 {
580         struct alc_spec *spec = codec->spec;
581         if (spec->automute_hook)
582                 spec->automute_hook(codec);
583         else
584                 update_outputs(codec);
585 }
586
587 /* standard HP-automute helper */
588 static void alc_hp_automute(struct hda_codec *codec)
589 {
590         struct alc_spec *spec = codec->spec;
591
592         spec->hp_jack_present =
593                 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
594                              spec->autocfg.hp_pins);
595         if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
596                 return;
597         call_update_outputs(codec);
598 }
599
600 /* standard line-out-automute helper */
601 static void alc_line_automute(struct hda_codec *codec)
602 {
603         struct alc_spec *spec = codec->spec;
604
605         if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
606                 return;
607         /* check LO jack only when it's different from HP */
608         if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
609                 return;
610
611         spec->line_jack_present =
612                 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
613                              spec->autocfg.line_out_pins);
614         if (!spec->automute_speaker || !spec->detect_lo)
615                 return;
616         call_update_outputs(codec);
617 }
618
619 #define get_connection_index(codec, mux, nid) \
620         snd_hda_get_conn_index(codec, mux, nid, 0)
621
622 /* standard mic auto-switch helper */
623 static void alc_mic_automute(struct hda_codec *codec)
624 {
625         struct alc_spec *spec = codec->spec;
626         hda_nid_t *pins = spec->imux_pins;
627
628         if (!spec->auto_mic || !spec->auto_mic_valid_imux)
629                 return;
630         if (snd_BUG_ON(!spec->adc_nids))
631                 return;
632         if (snd_BUG_ON(spec->int_mic_idx < 0 || spec->ext_mic_idx < 0))
633                 return;
634
635         if (snd_hda_jack_detect(codec, pins[spec->ext_mic_idx]))
636                 alc_mux_select(codec, 0, spec->ext_mic_idx, false);
637         else if (spec->dock_mic_idx >= 0 &&
638                    snd_hda_jack_detect(codec, pins[spec->dock_mic_idx]))
639                 alc_mux_select(codec, 0, spec->dock_mic_idx, false);
640         else
641                 alc_mux_select(codec, 0, spec->int_mic_idx, false);
642 }
643
644 /* handle the specified unsol action (ALC_XXX_EVENT) */
645 static void alc_exec_unsol_event(struct hda_codec *codec, int action)
646 {
647         switch (action) {
648         case ALC_HP_EVENT:
649                 alc_hp_automute(codec);
650                 break;
651         case ALC_FRONT_EVENT:
652                 alc_line_automute(codec);
653                 break;
654         case ALC_MIC_EVENT:
655                 alc_mic_automute(codec);
656                 break;
657         }
658         snd_hda_jack_report_sync(codec);
659 }
660
661 /* update the master volume per volume-knob's unsol event */
662 static void alc_update_knob_master(struct hda_codec *codec, hda_nid_t nid)
663 {
664         unsigned int val;
665         struct snd_kcontrol *kctl;
666         struct snd_ctl_elem_value *uctl;
667
668         kctl = snd_hda_find_mixer_ctl(codec, "Master Playback Volume");
669         if (!kctl)
670                 return;
671         uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
672         if (!uctl)
673                 return;
674         val = snd_hda_codec_read(codec, nid, 0,
675                                  AC_VERB_GET_VOLUME_KNOB_CONTROL, 0);
676         val &= HDA_AMP_VOLMASK;
677         uctl->value.integer.value[0] = val;
678         uctl->value.integer.value[1] = val;
679         kctl->put(kctl, uctl);
680         kfree(uctl);
681 }
682
683 /* unsolicited event for HP jack sensing */
684 static void alc_sku_unsol_event(struct hda_codec *codec, unsigned int res)
685 {
686         int action;
687
688         if (codec->vendor_id == 0x10ec0880)
689                 res >>= 28;
690         else
691                 res >>= 26;
692         action = snd_hda_jack_get_action(codec, res);
693         if (action == ALC_DCVOL_EVENT) {
694                 /* Execute the dc-vol event here as it requires the NID
695                  * but we don't pass NID to alc_exec_unsol_event().
696                  * Once when we convert all static quirks to the auto-parser,
697                  * this can be integerated into there.
698                  */
699                 struct hda_jack_tbl *jack;
700                 jack = snd_hda_jack_tbl_get_from_tag(codec, res);
701                 if (jack)
702                         alc_update_knob_master(codec, jack->nid);
703                 return;
704         }
705         alc_exec_unsol_event(codec, action);
706 }
707
708 /* call init functions of standard auto-mute helpers */
709 static void alc_inithook(struct hda_codec *codec)
710 {
711         alc_hp_automute(codec);
712         alc_line_automute(codec);
713         alc_mic_automute(codec);
714 }
715
716 /* additional initialization for ALC888 variants */
717 static void alc888_coef_init(struct hda_codec *codec)
718 {
719         unsigned int tmp;
720
721         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 0);
722         tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
723         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
724         if ((tmp & 0xf0) == 0x20)
725                 /* alc888S-VC */
726                 snd_hda_codec_read(codec, 0x20, 0,
727                                    AC_VERB_SET_PROC_COEF, 0x830);
728          else
729                  /* alc888-VB */
730                  snd_hda_codec_read(codec, 0x20, 0,
731                                     AC_VERB_SET_PROC_COEF, 0x3030);
732 }
733
734 /* additional initialization for ALC889 variants */
735 static void alc889_coef_init(struct hda_codec *codec)
736 {
737         unsigned int tmp;
738
739         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
740         tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
741         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
742         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_PROC_COEF, tmp|0x2010);
743 }
744
745 /* turn on/off EAPD control (only if available) */
746 static void set_eapd(struct hda_codec *codec, hda_nid_t nid, int on)
747 {
748         if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
749                 return;
750         if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
751                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_EAPD_BTLENABLE,
752                                     on ? 2 : 0);
753 }
754
755 /* turn on/off EAPD controls of the codec */
756 static void alc_auto_setup_eapd(struct hda_codec *codec, bool on)
757 {
758         /* We currently only handle front, HP */
759         static hda_nid_t pins[] = {
760                 0x0f, 0x10, 0x14, 0x15, 0
761         };
762         hda_nid_t *p;
763         for (p = pins; *p; p++)
764                 set_eapd(codec, *p, on);
765 }
766
767 /* generic shutup callback;
768  * just turning off EPAD and a little pause for avoiding pop-noise
769  */
770 static void alc_eapd_shutup(struct hda_codec *codec)
771 {
772         alc_auto_setup_eapd(codec, false);
773         msleep(200);
774 }
775
776 /* generic EAPD initialization */
777 static void alc_auto_init_amp(struct hda_codec *codec, int type)
778 {
779         unsigned int tmp;
780
781         alc_auto_setup_eapd(codec, true);
782         switch (type) {
783         case ALC_INIT_GPIO1:
784                 snd_hda_sequence_write(codec, alc_gpio1_init_verbs);
785                 break;
786         case ALC_INIT_GPIO2:
787                 snd_hda_sequence_write(codec, alc_gpio2_init_verbs);
788                 break;
789         case ALC_INIT_GPIO3:
790                 snd_hda_sequence_write(codec, alc_gpio3_init_verbs);
791                 break;
792         case ALC_INIT_DEFAULT:
793                 switch (codec->vendor_id) {
794                 case 0x10ec0260:
795                         snd_hda_codec_write(codec, 0x1a, 0,
796                                             AC_VERB_SET_COEF_INDEX, 7);
797                         tmp = snd_hda_codec_read(codec, 0x1a, 0,
798                                                  AC_VERB_GET_PROC_COEF, 0);
799                         snd_hda_codec_write(codec, 0x1a, 0,
800                                             AC_VERB_SET_COEF_INDEX, 7);
801                         snd_hda_codec_write(codec, 0x1a, 0,
802                                             AC_VERB_SET_PROC_COEF,
803                                             tmp | 0x2010);
804                         break;
805                 case 0x10ec0262:
806                 case 0x10ec0880:
807                 case 0x10ec0882:
808                 case 0x10ec0883:
809                 case 0x10ec0885:
810                 case 0x10ec0887:
811                 /*case 0x10ec0889:*/ /* this causes an SPDIF problem */
812                         alc889_coef_init(codec);
813                         break;
814                 case 0x10ec0888:
815                         alc888_coef_init(codec);
816                         break;
817 #if 0 /* XXX: This may cause the silent output on speaker on some machines */
818                 case 0x10ec0267:
819                 case 0x10ec0268:
820                         snd_hda_codec_write(codec, 0x20, 0,
821                                             AC_VERB_SET_COEF_INDEX, 7);
822                         tmp = snd_hda_codec_read(codec, 0x20, 0,
823                                                  AC_VERB_GET_PROC_COEF, 0);
824                         snd_hda_codec_write(codec, 0x20, 0,
825                                             AC_VERB_SET_COEF_INDEX, 7);
826                         snd_hda_codec_write(codec, 0x20, 0,
827                                             AC_VERB_SET_PROC_COEF,
828                                             tmp | 0x3000);
829                         break;
830 #endif /* XXX */
831                 }
832                 break;
833         }
834 }
835
836 /*
837  * Auto-Mute mode mixer enum support
838  */
839 static int alc_automute_mode_info(struct snd_kcontrol *kcontrol,
840                                   struct snd_ctl_elem_info *uinfo)
841 {
842         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
843         struct alc_spec *spec = codec->spec;
844         static const char * const texts2[] = {
845                 "Disabled", "Enabled"
846         };
847         static const char * const texts3[] = {
848                 "Disabled", "Speaker Only", "Line Out+Speaker"
849         };
850         const char * const *texts;
851
852         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
853         uinfo->count = 1;
854         if (spec->automute_speaker_possible && spec->automute_lo_possible) {
855                 uinfo->value.enumerated.items = 3;
856                 texts = texts3;
857         } else {
858                 uinfo->value.enumerated.items = 2;
859                 texts = texts2;
860         }
861         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
862                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
863         strcpy(uinfo->value.enumerated.name,
864                texts[uinfo->value.enumerated.item]);
865         return 0;
866 }
867
868 static int alc_automute_mode_get(struct snd_kcontrol *kcontrol,
869                                  struct snd_ctl_elem_value *ucontrol)
870 {
871         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
872         struct alc_spec *spec = codec->spec;
873         unsigned int val = 0;
874         if (spec->automute_speaker)
875                 val++;
876         if (spec->automute_lo)
877                 val++;
878
879         ucontrol->value.enumerated.item[0] = val;
880         return 0;
881 }
882
883 static int alc_automute_mode_put(struct snd_kcontrol *kcontrol,
884                                  struct snd_ctl_elem_value *ucontrol)
885 {
886         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
887         struct alc_spec *spec = codec->spec;
888
889         switch (ucontrol->value.enumerated.item[0]) {
890         case 0:
891                 if (!spec->automute_speaker && !spec->automute_lo)
892                         return 0;
893                 spec->automute_speaker = 0;
894                 spec->automute_lo = 0;
895                 break;
896         case 1:
897                 if (spec->automute_speaker_possible) {
898                         if (!spec->automute_lo && spec->automute_speaker)
899                                 return 0;
900                         spec->automute_speaker = 1;
901                         spec->automute_lo = 0;
902                 } else if (spec->automute_lo_possible) {
903                         if (spec->automute_lo)
904                                 return 0;
905                         spec->automute_lo = 1;
906                 } else
907                         return -EINVAL;
908                 break;
909         case 2:
910                 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
911                         return -EINVAL;
912                 if (spec->automute_speaker && spec->automute_lo)
913                         return 0;
914                 spec->automute_speaker = 1;
915                 spec->automute_lo = 1;
916                 break;
917         default:
918                 return -EINVAL;
919         }
920         call_update_outputs(codec);
921         return 1;
922 }
923
924 static const struct snd_kcontrol_new alc_automute_mode_enum = {
925         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
926         .name = "Auto-Mute Mode",
927         .info = alc_automute_mode_info,
928         .get = alc_automute_mode_get,
929         .put = alc_automute_mode_put,
930 };
931
932 static struct snd_kcontrol_new *alc_kcontrol_new(struct alc_spec *spec)
933 {
934         snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
935         return snd_array_new(&spec->kctls);
936 }
937
938 static int alc_add_automute_mode_enum(struct hda_codec *codec)
939 {
940         struct alc_spec *spec = codec->spec;
941         struct snd_kcontrol_new *knew;
942
943         knew = alc_kcontrol_new(spec);
944         if (!knew)
945                 return -ENOMEM;
946         *knew = alc_automute_mode_enum;
947         knew->name = kstrdup("Auto-Mute Mode", GFP_KERNEL);
948         if (!knew->name)
949                 return -ENOMEM;
950         return 0;
951 }
952
953 /*
954  * Check the availability of HP/line-out auto-mute;
955  * Set up appropriately if really supported
956  */
957 static void alc_init_automute(struct hda_codec *codec)
958 {
959         struct alc_spec *spec = codec->spec;
960         struct auto_pin_cfg *cfg = &spec->autocfg;
961         int present = 0;
962         int i;
963
964         if (cfg->hp_pins[0])
965                 present++;
966         if (cfg->line_out_pins[0])
967                 present++;
968         if (cfg->speaker_pins[0])
969                 present++;
970         if (present < 2) /* need two different output types */
971                 return;
972
973         if (!cfg->speaker_pins[0] &&
974             cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
975                 memcpy(cfg->speaker_pins, cfg->line_out_pins,
976                        sizeof(cfg->speaker_pins));
977                 cfg->speaker_outs = cfg->line_outs;
978         }
979
980         if (!cfg->hp_pins[0] &&
981             cfg->line_out_type == AUTO_PIN_HP_OUT) {
982                 memcpy(cfg->hp_pins, cfg->line_out_pins,
983                        sizeof(cfg->hp_pins));
984                 cfg->hp_outs = cfg->line_outs;
985         }
986
987         spec->automute_mode = ALC_AUTOMUTE_PIN;
988
989         for (i = 0; i < cfg->hp_outs; i++) {
990                 hda_nid_t nid = cfg->hp_pins[i];
991                 if (!is_jack_detectable(codec, nid))
992                         continue;
993                 snd_printdd("realtek: Enable HP auto-muting on NID 0x%x\n",
994                             nid);
995                 snd_hda_jack_detect_enable(codec, nid, ALC_HP_EVENT);
996                 spec->detect_hp = 1;
997         }
998
999         if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
1000                 if (cfg->speaker_outs)
1001                         for (i = 0; i < cfg->line_outs; i++) {
1002                                 hda_nid_t nid = cfg->line_out_pins[i];
1003                                 if (!is_jack_detectable(codec, nid))
1004                                         continue;
1005                                 snd_printdd("realtek: Enable Line-Out "
1006                                             "auto-muting on NID 0x%x\n", nid);
1007                                 snd_hda_jack_detect_enable(codec, nid,
1008                                                            ALC_FRONT_EVENT);
1009                                 spec->detect_lo = 1;
1010                 }
1011                 spec->automute_lo_possible = spec->detect_hp;
1012         }
1013
1014         spec->automute_speaker_possible = cfg->speaker_outs &&
1015                 (spec->detect_hp || spec->detect_lo);
1016
1017         spec->automute_lo = spec->automute_lo_possible;
1018         spec->automute_speaker = spec->automute_speaker_possible;
1019
1020         if (spec->automute_speaker_possible || spec->automute_lo_possible) {
1021                 /* create a control for automute mode */
1022                 alc_add_automute_mode_enum(codec);
1023                 spec->unsol_event = alc_sku_unsol_event;
1024         }
1025 }
1026
1027 /* return the position of NID in the list, or -1 if not found */
1028 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
1029 {
1030         int i;
1031         for (i = 0; i < nums; i++)
1032                 if (list[i] == nid)
1033                         return i;
1034         return -1;
1035 }
1036
1037 /* check whether dynamic ADC-switching is available */
1038 static bool alc_check_dyn_adc_switch(struct hda_codec *codec)
1039 {
1040         struct alc_spec *spec = codec->spec;
1041         struct hda_input_mux *imux = &spec->private_imux[0];
1042         int i, n, idx;
1043         hda_nid_t cap, pin;
1044
1045         if (imux != spec->input_mux) /* no dynamic imux? */
1046                 return false;
1047
1048         for (n = 0; n < spec->num_adc_nids; n++) {
1049                 cap = spec->private_capsrc_nids[n];
1050                 for (i = 0; i < imux->num_items; i++) {
1051                         pin = spec->imux_pins[i];
1052                         if (!pin)
1053                                 return false;
1054                         if (get_connection_index(codec, cap, pin) < 0)
1055                                 break;
1056                 }
1057                 if (i >= imux->num_items)
1058                         return true; /* no ADC-switch is needed */
1059         }
1060
1061         for (i = 0; i < imux->num_items; i++) {
1062                 pin = spec->imux_pins[i];
1063                 for (n = 0; n < spec->num_adc_nids; n++) {
1064                         cap = spec->private_capsrc_nids[n];
1065                         idx = get_connection_index(codec, cap, pin);
1066                         if (idx >= 0) {
1067                                 imux->items[i].index = idx;
1068                                 spec->dyn_adc_idx[i] = n;
1069                                 break;
1070                         }
1071                 }
1072         }
1073
1074         snd_printdd("realtek: enabling ADC switching\n");
1075         spec->dyn_adc_switch = 1;
1076         return true;
1077 }
1078
1079 /* check whether all auto-mic pins are valid; setup indices if OK */
1080 static bool alc_auto_mic_check_imux(struct hda_codec *codec)
1081 {
1082         struct alc_spec *spec = codec->spec;
1083         const struct hda_input_mux *imux;
1084
1085         if (!spec->auto_mic)
1086                 return false;
1087         if (spec->auto_mic_valid_imux)
1088                 return true; /* already checked */
1089
1090         /* fill up imux indices */
1091         if (!alc_check_dyn_adc_switch(codec)) {
1092                 spec->auto_mic = 0;
1093                 return false;
1094         }
1095
1096         imux = spec->input_mux;
1097         spec->ext_mic_idx = find_idx_in_nid_list(spec->ext_mic_pin,
1098                                         spec->imux_pins, imux->num_items);
1099         spec->int_mic_idx = find_idx_in_nid_list(spec->int_mic_pin,
1100                                         spec->imux_pins, imux->num_items);
1101         spec->dock_mic_idx = find_idx_in_nid_list(spec->dock_mic_pin,
1102                                         spec->imux_pins, imux->num_items);
1103         if (spec->ext_mic_idx < 0 || spec->int_mic_idx < 0) {
1104                 spec->auto_mic = 0;
1105                 return false; /* no corresponding imux */
1106         }
1107
1108         snd_hda_jack_detect_enable(codec, spec->ext_mic_pin, ALC_MIC_EVENT);
1109         if (spec->dock_mic_pin)
1110                 snd_hda_jack_detect_enable(codec, spec->dock_mic_pin,
1111                                            ALC_MIC_EVENT);
1112
1113         spec->auto_mic_valid_imux = 1;
1114         spec->auto_mic = 1;
1115         return true;
1116 }
1117
1118 /*
1119  * Check the availability of auto-mic switch;
1120  * Set up if really supported
1121  */
1122 static void alc_init_auto_mic(struct hda_codec *codec)
1123 {
1124         struct alc_spec *spec = codec->spec;
1125         struct auto_pin_cfg *cfg = &spec->autocfg;
1126         hda_nid_t fixed, ext, dock;
1127         int i;
1128
1129         if (spec->shared_mic_hp)
1130                 return; /* no auto-mic for the shared I/O */
1131
1132         spec->ext_mic_idx = spec->int_mic_idx = spec->dock_mic_idx = -1;
1133
1134         fixed = ext = dock = 0;
1135         for (i = 0; i < cfg->num_inputs; i++) {
1136                 hda_nid_t nid = cfg->inputs[i].pin;
1137                 unsigned int defcfg;
1138                 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1139                 switch (snd_hda_get_input_pin_attr(defcfg)) {
1140                 case INPUT_PIN_ATTR_INT:
1141                         if (fixed)
1142                                 return; /* already occupied */
1143                         if (cfg->inputs[i].type != AUTO_PIN_MIC)
1144                                 return; /* invalid type */
1145                         fixed = nid;
1146                         break;
1147                 case INPUT_PIN_ATTR_UNUSED:
1148                         return; /* invalid entry */
1149                 case INPUT_PIN_ATTR_DOCK:
1150                         if (dock)
1151                                 return; /* already occupied */
1152                         if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
1153                                 return; /* invalid type */
1154                         dock = nid;
1155                         break;
1156                 default:
1157                         if (ext)
1158                                 return; /* already occupied */
1159                         if (cfg->inputs[i].type != AUTO_PIN_MIC)
1160                                 return; /* invalid type */
1161                         ext = nid;
1162                         break;
1163                 }
1164         }
1165         if (!ext && dock) {
1166                 ext = dock;
1167                 dock = 0;
1168         }
1169         if (!ext || !fixed)
1170                 return;
1171         if (!is_jack_detectable(codec, ext))
1172                 return; /* no unsol support */
1173         if (dock && !is_jack_detectable(codec, dock))
1174                 return; /* no unsol support */
1175
1176         /* check imux indices */
1177         spec->ext_mic_pin = ext;
1178         spec->int_mic_pin = fixed;
1179         spec->dock_mic_pin = dock;
1180
1181         spec->auto_mic = 1;
1182         if (!alc_auto_mic_check_imux(codec))
1183                 return;
1184
1185         snd_printdd("realtek: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
1186                     ext, fixed, dock);
1187         spec->unsol_event = alc_sku_unsol_event;
1188 }
1189
1190 /* check the availabilities of auto-mute and auto-mic switches */
1191 static void alc_auto_check_switches(struct hda_codec *codec)
1192 {
1193         alc_init_automute(codec);
1194         alc_init_auto_mic(codec);
1195 }
1196
1197 /*
1198  * Realtek SSID verification
1199  */
1200
1201 /* Could be any non-zero and even value. When used as fixup, tells
1202  * the driver to ignore any present sku defines.
1203  */
1204 #define ALC_FIXUP_SKU_IGNORE (2)
1205
1206 static int alc_auto_parse_customize_define(struct hda_codec *codec)
1207 {
1208         unsigned int ass, tmp, i;
1209         unsigned nid = 0;
1210         struct alc_spec *spec = codec->spec;
1211
1212         spec->cdefine.enable_pcbeep = 1; /* assume always enabled */
1213
1214         if (spec->cdefine.fixup) {
1215                 ass = spec->cdefine.sku_cfg;
1216                 if (ass == ALC_FIXUP_SKU_IGNORE)
1217                         return -1;
1218                 goto do_sku;
1219         }
1220
1221         ass = codec->subsystem_id & 0xffff;
1222         if (ass != codec->bus->pci->subsystem_device && (ass & 1))
1223                 goto do_sku;
1224
1225         nid = 0x1d;
1226         if (codec->vendor_id == 0x10ec0260)
1227                 nid = 0x17;
1228         ass = snd_hda_codec_get_pincfg(codec, nid);
1229
1230         if (!(ass & 1)) {
1231                 printk(KERN_INFO "hda_codec: %s: SKU not ready 0x%08x\n",
1232                        codec->chip_name, ass);
1233                 return -1;
1234         }
1235
1236         /* check sum */
1237         tmp = 0;
1238         for (i = 1; i < 16; i++) {
1239                 if ((ass >> i) & 1)
1240                         tmp++;
1241         }
1242         if (((ass >> 16) & 0xf) != tmp)
1243                 return -1;
1244
1245         spec->cdefine.port_connectivity = ass >> 30;
1246         spec->cdefine.enable_pcbeep = (ass & 0x100000) >> 20;
1247         spec->cdefine.check_sum = (ass >> 16) & 0xf;
1248         spec->cdefine.customization = ass >> 8;
1249 do_sku:
1250         spec->cdefine.sku_cfg = ass;
1251         spec->cdefine.external_amp = (ass & 0x38) >> 3;
1252         spec->cdefine.platform_type = (ass & 0x4) >> 2;
1253         spec->cdefine.swap = (ass & 0x2) >> 1;
1254         spec->cdefine.override = ass & 0x1;
1255
1256         snd_printd("SKU: Nid=0x%x sku_cfg=0x%08x\n",
1257                    nid, spec->cdefine.sku_cfg);
1258         snd_printd("SKU: port_connectivity=0x%x\n",
1259                    spec->cdefine.port_connectivity);
1260         snd_printd("SKU: enable_pcbeep=0x%x\n", spec->cdefine.enable_pcbeep);
1261         snd_printd("SKU: check_sum=0x%08x\n", spec->cdefine.check_sum);
1262         snd_printd("SKU: customization=0x%08x\n", spec->cdefine.customization);
1263         snd_printd("SKU: external_amp=0x%x\n", spec->cdefine.external_amp);
1264         snd_printd("SKU: platform_type=0x%x\n", spec->cdefine.platform_type);
1265         snd_printd("SKU: swap=0x%x\n", spec->cdefine.swap);
1266         snd_printd("SKU: override=0x%x\n", spec->cdefine.override);
1267
1268         return 0;
1269 }
1270
1271 /* return true if the given NID is found in the list */
1272 static bool found_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
1273 {
1274         return find_idx_in_nid_list(nid, list, nums) >= 0;
1275 }
1276
1277 /* check subsystem ID and set up device-specific initialization;
1278  * return 1 if initialized, 0 if invalid SSID
1279  */
1280 /* 32-bit subsystem ID for BIOS loading in HD Audio codec.
1281  *      31 ~ 16 :       Manufacture ID
1282  *      15 ~ 8  :       SKU ID
1283  *      7  ~ 0  :       Assembly ID
1284  *      port-A --> pin 39/41, port-E --> pin 14/15, port-D --> pin 35/36
1285  */
1286 static int alc_subsystem_id(struct hda_codec *codec,
1287                             hda_nid_t porta, hda_nid_t porte,
1288                             hda_nid_t portd, hda_nid_t porti)
1289 {
1290         unsigned int ass, tmp, i;
1291         unsigned nid;
1292         struct alc_spec *spec = codec->spec;
1293
1294         if (spec->cdefine.fixup) {
1295                 ass = spec->cdefine.sku_cfg;
1296                 if (ass == ALC_FIXUP_SKU_IGNORE)
1297                         return 0;
1298                 goto do_sku;
1299         }
1300
1301         ass = codec->subsystem_id & 0xffff;
1302         if ((ass != codec->bus->pci->subsystem_device) && (ass & 1))
1303                 goto do_sku;
1304
1305         /* invalid SSID, check the special NID pin defcfg instead */
1306         /*
1307          * 31~30        : port connectivity
1308          * 29~21        : reserve
1309          * 20           : PCBEEP input
1310          * 19~16        : Check sum (15:1)
1311          * 15~1         : Custom
1312          * 0            : override
1313         */
1314         nid = 0x1d;
1315         if (codec->vendor_id == 0x10ec0260)
1316                 nid = 0x17;
1317         ass = snd_hda_codec_get_pincfg(codec, nid);
1318         snd_printd("realtek: No valid SSID, "
1319                    "checking pincfg 0x%08x for NID 0x%x\n",
1320                    ass, nid);
1321         if (!(ass & 1))
1322                 return 0;
1323         if ((ass >> 30) != 1)   /* no physical connection */
1324                 return 0;
1325
1326         /* check sum */
1327         tmp = 0;
1328         for (i = 1; i < 16; i++) {
1329                 if ((ass >> i) & 1)
1330                         tmp++;
1331         }
1332         if (((ass >> 16) & 0xf) != tmp)
1333                 return 0;
1334 do_sku:
1335         snd_printd("realtek: Enabling init ASM_ID=0x%04x CODEC_ID=%08x\n",
1336                    ass & 0xffff, codec->vendor_id);
1337         /*
1338          * 0 : override
1339          * 1 :  Swap Jack
1340          * 2 : 0 --> Desktop, 1 --> Laptop
1341          * 3~5 : External Amplifier control
1342          * 7~6 : Reserved
1343         */
1344         tmp = (ass & 0x38) >> 3;        /* external Amp control */
1345         switch (tmp) {
1346         case 1:
1347                 spec->init_amp = ALC_INIT_GPIO1;
1348                 break;
1349         case 3:
1350                 spec->init_amp = ALC_INIT_GPIO2;
1351                 break;
1352         case 7:
1353                 spec->init_amp = ALC_INIT_GPIO3;
1354                 break;
1355         case 5:
1356         default:
1357                 spec->init_amp = ALC_INIT_DEFAULT;
1358                 break;
1359         }
1360
1361         /* is laptop or Desktop and enable the function "Mute internal speaker
1362          * when the external headphone out jack is plugged"
1363          */
1364         if (!(ass & 0x8000))
1365                 return 1;
1366         /*
1367          * 10~8 : Jack location
1368          * 12~11: Headphone out -> 00: PortA, 01: PortE, 02: PortD, 03: Resvered
1369          * 14~13: Resvered
1370          * 15   : 1 --> enable the function "Mute internal speaker
1371          *              when the external headphone out jack is plugged"
1372          */
1373         if (!spec->autocfg.hp_pins[0] &&
1374             !(spec->autocfg.line_out_pins[0] &&
1375               spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)) {
1376                 hda_nid_t nid;
1377                 tmp = (ass >> 11) & 0x3;        /* HP to chassis */
1378                 if (tmp == 0)
1379                         nid = porta;
1380                 else if (tmp == 1)
1381                         nid = porte;
1382                 else if (tmp == 2)
1383                         nid = portd;
1384                 else if (tmp == 3)
1385                         nid = porti;
1386                 else
1387                         return 1;
1388                 if (found_in_nid_list(nid, spec->autocfg.line_out_pins,
1389                                       spec->autocfg.line_outs))
1390                         return 1;
1391                 spec->autocfg.hp_pins[0] = nid;
1392         }
1393         return 1;
1394 }
1395
1396 /* Check the validity of ALC subsystem-id
1397  * ports contains an array of 4 pin NIDs for port-A, E, D and I */
1398 static void alc_ssid_check(struct hda_codec *codec, const hda_nid_t *ports)
1399 {
1400         if (!alc_subsystem_id(codec, ports[0], ports[1], ports[2], ports[3])) {
1401                 struct alc_spec *spec = codec->spec;
1402                 snd_printd("realtek: "
1403                            "Enable default setup for auto mode as fallback\n");
1404                 spec->init_amp = ALC_INIT_DEFAULT;
1405         }
1406 }
1407
1408 /*
1409  * Fix-up pin default configurations and add default verbs
1410  */
1411
1412 struct alc_pincfg {
1413         hda_nid_t nid;
1414         u32 val;
1415 };
1416
1417 struct alc_model_fixup {
1418         const int id;
1419         const char *name;
1420 };
1421
1422 struct alc_fixup {
1423         int type;
1424         bool chained;
1425         int chain_id;
1426         union {
1427                 unsigned int sku;
1428                 const struct alc_pincfg *pins;
1429                 const struct hda_verb *verbs;
1430                 void (*func)(struct hda_codec *codec,
1431                              const struct alc_fixup *fix,
1432                              int action);
1433         } v;
1434 };
1435
1436 enum {
1437         ALC_FIXUP_INVALID,
1438         ALC_FIXUP_SKU,
1439         ALC_FIXUP_PINS,
1440         ALC_FIXUP_VERBS,
1441         ALC_FIXUP_FUNC,
1442 };
1443
1444 enum {
1445         ALC_FIXUP_ACT_PRE_PROBE,
1446         ALC_FIXUP_ACT_PROBE,
1447         ALC_FIXUP_ACT_INIT,
1448         ALC_FIXUP_ACT_BUILD,
1449 };
1450
1451 static void alc_apply_pincfgs(struct hda_codec *codec,
1452                               const struct alc_pincfg *cfg)
1453 {
1454         for (; cfg->nid; cfg++)
1455                 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
1456 }
1457
1458 static void alc_apply_fixup(struct hda_codec *codec, int action)
1459 {
1460         struct alc_spec *spec = codec->spec;
1461         int id = spec->fixup_id;
1462 #ifdef CONFIG_SND_DEBUG_VERBOSE
1463         const char *modelname = spec->fixup_name;
1464 #endif
1465         int depth = 0;
1466
1467         if (!spec->fixup_list)
1468                 return;
1469
1470         while (id >= 0) {
1471                 const struct alc_fixup *fix = spec->fixup_list + id;
1472                 const struct alc_pincfg *cfg;
1473
1474                 switch (fix->type) {
1475                 case ALC_FIXUP_SKU:
1476                         if (action != ALC_FIXUP_ACT_PRE_PROBE || !fix->v.sku)
1477                                 break;
1478                         snd_printdd(KERN_INFO "hda_codec: %s: "
1479                                     "Apply sku override for %s\n",
1480                                     codec->chip_name, modelname);
1481                         spec->cdefine.sku_cfg = fix->v.sku;
1482                         spec->cdefine.fixup = 1;
1483                         break;
1484                 case ALC_FIXUP_PINS:
1485                         cfg = fix->v.pins;
1486                         if (action != ALC_FIXUP_ACT_PRE_PROBE || !cfg)
1487                                 break;
1488                         snd_printdd(KERN_INFO "hda_codec: %s: "
1489                                     "Apply pincfg for %s\n",
1490                                     codec->chip_name, modelname);
1491                         alc_apply_pincfgs(codec, cfg);
1492                         break;
1493                 case ALC_FIXUP_VERBS:
1494                         if (action != ALC_FIXUP_ACT_PROBE || !fix->v.verbs)
1495                                 break;
1496                         snd_printdd(KERN_INFO "hda_codec: %s: "
1497                                     "Apply fix-verbs for %s\n",
1498                                     codec->chip_name, modelname);
1499                         add_verb(codec->spec, fix->v.verbs);
1500                         break;
1501                 case ALC_FIXUP_FUNC:
1502                         if (!fix->v.func)
1503                                 break;
1504                         snd_printdd(KERN_INFO "hda_codec: %s: "
1505                                     "Apply fix-func for %s\n",
1506                                     codec->chip_name, modelname);
1507                         fix->v.func(codec, fix, action);
1508                         break;
1509                 default:
1510                         snd_printk(KERN_ERR "hda_codec: %s: "
1511                                    "Invalid fixup type %d\n",
1512                                    codec->chip_name, fix->type);
1513                         break;
1514                 }
1515                 if (!fix->chained)
1516                         break;
1517                 if (++depth > 10)
1518                         break;
1519                 id = fix->chain_id;
1520         }
1521 }
1522
1523 static void alc_pick_fixup(struct hda_codec *codec,
1524                            const struct alc_model_fixup *models,
1525                            const struct snd_pci_quirk *quirk,
1526                            const struct alc_fixup *fixlist)
1527 {
1528         struct alc_spec *spec = codec->spec;
1529         const struct snd_pci_quirk *q;
1530         int id = -1;
1531         const char *name = NULL;
1532
1533         /* when model=nofixup is given, don't pick up any fixups */
1534         if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
1535                 spec->fixup_list = NULL;
1536                 spec->fixup_id = -1;
1537                 return;
1538         }
1539
1540         if (codec->modelname && models) {
1541                 while (models->name) {
1542                         if (!strcmp(codec->modelname, models->name)) {
1543                                 id = models->id;
1544                                 name = models->name;
1545                                 break;
1546                         }
1547                         models++;
1548                 }
1549         }
1550         if (id < 0) {
1551                 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
1552                 if (q) {
1553                         id = q->value;
1554 #ifdef CONFIG_SND_DEBUG_VERBOSE
1555                         name = q->name;
1556 #endif
1557                 }
1558         }
1559         if (id < 0) {
1560                 for (q = quirk; q->subvendor; q++) {
1561                         unsigned int vendorid =
1562                                 q->subdevice | (q->subvendor << 16);
1563                         if (vendorid == codec->subsystem_id) {
1564                                 id = q->value;
1565 #ifdef CONFIG_SND_DEBUG_VERBOSE
1566                                 name = q->name;
1567 #endif
1568                                 break;
1569                         }
1570                 }
1571         }
1572
1573         spec->fixup_id = id;
1574         if (id >= 0) {
1575                 spec->fixup_list = fixlist;
1576                 spec->fixup_name = name;
1577         }
1578 }
1579
1580 /*
1581  * COEF access helper functions
1582  */
1583 static int alc_read_coef_idx(struct hda_codec *codec,
1584                         unsigned int coef_idx)
1585 {
1586         unsigned int val;
1587         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX,
1588                                 coef_idx);
1589         val = snd_hda_codec_read(codec, 0x20, 0,
1590                                 AC_VERB_GET_PROC_COEF, 0);
1591         return val;
1592 }
1593
1594 static void alc_write_coef_idx(struct hda_codec *codec, unsigned int coef_idx,
1595                                                         unsigned int coef_val)
1596 {
1597         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX,
1598                             coef_idx);
1599         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_PROC_COEF,
1600                             coef_val);
1601 }
1602
1603 /* a special bypass for COEF 0; read the cached value at the second time */
1604 static unsigned int alc_get_coef0(struct hda_codec *codec)
1605 {
1606         struct alc_spec *spec = codec->spec;
1607         if (!spec->coef0)
1608                 spec->coef0 = alc_read_coef_idx(codec, 0);
1609         return spec->coef0;
1610 }
1611
1612 /*
1613  * Digital I/O handling
1614  */
1615
1616 /* set right pin controls for digital I/O */
1617 static void alc_auto_init_digital(struct hda_codec *codec)
1618 {
1619         struct alc_spec *spec = codec->spec;
1620         int i;
1621         hda_nid_t pin, dac;
1622
1623         for (i = 0; i < spec->autocfg.dig_outs; i++) {
1624                 pin = spec->autocfg.dig_out_pins[i];
1625                 if (!pin)
1626                         continue;
1627                 snd_hda_codec_write(codec, pin, 0,
1628                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1629                 if (!i)
1630                         dac = spec->multiout.dig_out_nid;
1631                 else
1632                         dac = spec->slave_dig_outs[i - 1];
1633                 if (!dac || !(get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
1634                         continue;
1635                 snd_hda_codec_write(codec, dac, 0,
1636                                     AC_VERB_SET_AMP_GAIN_MUTE,
1637                                     AMP_OUT_UNMUTE);
1638         }
1639         pin = spec->autocfg.dig_in_pin;
1640         if (pin)
1641                 snd_hda_codec_write(codec, pin, 0,
1642                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1643                                     PIN_IN);
1644 }
1645
1646 /* parse digital I/Os and set up NIDs in BIOS auto-parse mode */
1647 static void alc_auto_parse_digital(struct hda_codec *codec)
1648 {
1649         struct alc_spec *spec = codec->spec;
1650         int i, err, nums;
1651         hda_nid_t dig_nid;
1652
1653         /* support multiple SPDIFs; the secondary is set up as a slave */
1654         nums = 0;
1655         for (i = 0; i < spec->autocfg.dig_outs; i++) {
1656                 hda_nid_t conn[4];
1657                 err = snd_hda_get_connections(codec,
1658                                               spec->autocfg.dig_out_pins[i],
1659                                               conn, ARRAY_SIZE(conn));
1660                 if (err <= 0)
1661                         continue;
1662                 dig_nid = conn[0]; /* assume the first element is audio-out */
1663                 if (!nums) {
1664                         spec->multiout.dig_out_nid = dig_nid;
1665                         spec->dig_out_type = spec->autocfg.dig_out_type[0];
1666                 } else {
1667                         spec->multiout.slave_dig_outs = spec->slave_dig_outs;
1668                         if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
1669                                 break;
1670                         spec->slave_dig_outs[nums - 1] = dig_nid;
1671                 }
1672                 nums++;
1673         }
1674
1675         if (spec->autocfg.dig_in_pin) {
1676                 dig_nid = codec->start_nid;
1677                 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
1678                         unsigned int wcaps = get_wcaps(codec, dig_nid);
1679                         if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
1680                                 continue;
1681                         if (!(wcaps & AC_WCAP_DIGITAL))
1682                                 continue;
1683                         if (!(wcaps & AC_WCAP_CONN_LIST))
1684                                 continue;
1685                         err = get_connection_index(codec, dig_nid,
1686                                                    spec->autocfg.dig_in_pin);
1687                         if (err >= 0) {
1688                                 spec->dig_in_nid = dig_nid;
1689                                 break;
1690                         }
1691                 }
1692         }
1693 }
1694
1695 /*
1696  * capture mixer elements
1697  */
1698 static int alc_cap_vol_info(struct snd_kcontrol *kcontrol,
1699                             struct snd_ctl_elem_info *uinfo)
1700 {
1701         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1702         struct alc_spec *spec = codec->spec;
1703         unsigned long val;
1704         int err;
1705
1706         mutex_lock(&codec->control_mutex);
1707         if (spec->vol_in_capsrc)
1708                 val = HDA_COMPOSE_AMP_VAL(spec->capsrc_nids[0], 3, 0, HDA_OUTPUT);
1709         else
1710                 val = HDA_COMPOSE_AMP_VAL(spec->adc_nids[0], 3, 0, HDA_INPUT);
1711         kcontrol->private_value = val;
1712         err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
1713         mutex_unlock(&codec->control_mutex);
1714         return err;
1715 }
1716
1717 static int alc_cap_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1718                            unsigned int size, unsigned int __user *tlv)
1719 {
1720         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1721         struct alc_spec *spec = codec->spec;
1722         unsigned long val;
1723         int err;
1724
1725         mutex_lock(&codec->control_mutex);
1726         if (spec->vol_in_capsrc)
1727                 val = HDA_COMPOSE_AMP_VAL(spec->capsrc_nids[0], 3, 0, HDA_OUTPUT);
1728         else
1729                 val = HDA_COMPOSE_AMP_VAL(spec->adc_nids[0], 3, 0, HDA_INPUT);
1730         kcontrol->private_value = val;
1731         err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
1732         mutex_unlock(&codec->control_mutex);
1733         return err;
1734 }
1735
1736 typedef int (*getput_call_t)(struct snd_kcontrol *kcontrol,
1737                              struct snd_ctl_elem_value *ucontrol);
1738
1739 static int alc_cap_getput_caller(struct snd_kcontrol *kcontrol,
1740                                  struct snd_ctl_elem_value *ucontrol,
1741                                  getput_call_t func, bool check_adc_switch)
1742 {
1743         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1744         struct alc_spec *spec = codec->spec;
1745         int i, err = 0;
1746
1747         mutex_lock(&codec->control_mutex);
1748         if (check_adc_switch && spec->dyn_adc_switch) {
1749                 for (i = 0; i < spec->num_adc_nids; i++) {
1750                         kcontrol->private_value =
1751                                 HDA_COMPOSE_AMP_VAL(spec->adc_nids[i],
1752                                                     3, 0, HDA_INPUT);
1753                         err = func(kcontrol, ucontrol);
1754                         if (err < 0)
1755                                 goto error;
1756                 }
1757         } else {
1758                 i = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1759                 if (spec->vol_in_capsrc)
1760                         kcontrol->private_value =
1761                                 HDA_COMPOSE_AMP_VAL(spec->capsrc_nids[i],
1762                                                     3, 0, HDA_OUTPUT);
1763                 else
1764                         kcontrol->private_value =
1765                                 HDA_COMPOSE_AMP_VAL(spec->adc_nids[i],
1766                                                     3, 0, HDA_INPUT);
1767                 err = func(kcontrol, ucontrol);
1768         }
1769  error:
1770         mutex_unlock(&codec->control_mutex);
1771         return err;
1772 }
1773
1774 static int alc_cap_vol_get(struct snd_kcontrol *kcontrol,
1775                            struct snd_ctl_elem_value *ucontrol)
1776 {
1777         return alc_cap_getput_caller(kcontrol, ucontrol,
1778                                      snd_hda_mixer_amp_volume_get, false);
1779 }
1780
1781 static int alc_cap_vol_put(struct snd_kcontrol *kcontrol,
1782                            struct snd_ctl_elem_value *ucontrol)
1783 {
1784         return alc_cap_getput_caller(kcontrol, ucontrol,
1785                                      snd_hda_mixer_amp_volume_put, true);
1786 }
1787
1788 /* capture mixer elements */
1789 #define alc_cap_sw_info         snd_ctl_boolean_stereo_info
1790
1791 static int alc_cap_sw_get(struct snd_kcontrol *kcontrol,
1792                           struct snd_ctl_elem_value *ucontrol)
1793 {
1794         return alc_cap_getput_caller(kcontrol, ucontrol,
1795                                      snd_hda_mixer_amp_switch_get, false);
1796 }
1797
1798 static int alc_cap_sw_put(struct snd_kcontrol *kcontrol,
1799                           struct snd_ctl_elem_value *ucontrol)
1800 {
1801         return alc_cap_getput_caller(kcontrol, ucontrol,
1802                                      snd_hda_mixer_amp_switch_put, true);
1803 }
1804
1805 #define _DEFINE_CAPMIX(num) \
1806         { \
1807                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1808                 .name = "Capture Switch", \
1809                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
1810                 .count = num, \
1811                 .info = alc_cap_sw_info, \
1812                 .get = alc_cap_sw_get, \
1813                 .put = alc_cap_sw_put, \
1814         }, \
1815         { \
1816                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1817                 .name = "Capture Volume", \
1818                 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE | \
1819                            SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
1820                            SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK), \
1821                 .count = num, \
1822                 .info = alc_cap_vol_info, \
1823                 .get = alc_cap_vol_get, \
1824                 .put = alc_cap_vol_put, \
1825                 .tlv = { .c = alc_cap_vol_tlv }, \
1826         }
1827
1828 #define _DEFINE_CAPSRC(num) \
1829         { \
1830                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1831                 /* .name = "Capture Source", */ \
1832                 .name = "Input Source", \
1833                 .count = num, \
1834                 .info = alc_mux_enum_info, \
1835                 .get = alc_mux_enum_get, \
1836                 .put = alc_mux_enum_put, \
1837         }
1838
1839 #define DEFINE_CAPMIX(num) \
1840 static const struct snd_kcontrol_new alc_capture_mixer ## num[] = { \
1841         _DEFINE_CAPMIX(num),                                  \
1842         _DEFINE_CAPSRC(num),                                  \
1843         { } /* end */                                         \
1844 }
1845
1846 #define DEFINE_CAPMIX_NOSRC(num) \
1847 static const struct snd_kcontrol_new alc_capture_mixer_nosrc ## num[] = { \
1848         _DEFINE_CAPMIX(num),                                        \
1849         { } /* end */                                               \
1850 }
1851
1852 /* up to three ADCs */
1853 DEFINE_CAPMIX(1);
1854 DEFINE_CAPMIX(2);
1855 DEFINE_CAPMIX(3);
1856 DEFINE_CAPMIX_NOSRC(1);
1857 DEFINE_CAPMIX_NOSRC(2);
1858 DEFINE_CAPMIX_NOSRC(3);
1859
1860 /*
1861  * virtual master controls
1862  */
1863
1864 /*
1865  * slave controls for virtual master
1866  */
1867 static const char * const alc_slave_pfxs[] = {
1868         "Front", "Surround", "Center", "LFE", "Side",
1869         "Headphone", "Speaker", "Mono", "Line Out",
1870         "CLFE", "Bass Speaker", "PCM",
1871         NULL,
1872 };
1873
1874 /*
1875  * build control elements
1876  */
1877
1878 #define NID_MAPPING             (-1)
1879
1880 #define SUBDEV_SPEAKER_         (0 << 6)
1881 #define SUBDEV_HP_              (1 << 6)
1882 #define SUBDEV_LINE_            (2 << 6)
1883 #define SUBDEV_SPEAKER(x)       (SUBDEV_SPEAKER_ | ((x) & 0x3f))
1884 #define SUBDEV_HP(x)            (SUBDEV_HP_ | ((x) & 0x3f))
1885 #define SUBDEV_LINE(x)          (SUBDEV_LINE_ | ((x) & 0x3f))
1886
1887 static void alc_free_kctls(struct hda_codec *codec);
1888
1889 #ifdef CONFIG_SND_HDA_INPUT_BEEP
1890 /* additional beep mixers; the actual parameters are overwritten at build */
1891 static const struct snd_kcontrol_new alc_beep_mixer[] = {
1892         HDA_CODEC_VOLUME("Beep Playback Volume", 0, 0, HDA_INPUT),
1893         HDA_CODEC_MUTE_BEEP("Beep Playback Switch", 0, 0, HDA_INPUT),
1894         { } /* end */
1895 };
1896 #endif
1897
1898 static int __alc_build_controls(struct hda_codec *codec)
1899 {
1900         struct alc_spec *spec = codec->spec;
1901         struct snd_kcontrol *kctl = NULL;
1902         const struct snd_kcontrol_new *knew;
1903         int i, j, err;
1904         unsigned int u;
1905         hda_nid_t nid;
1906
1907         for (i = 0; i < spec->num_mixers; i++) {
1908                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1909                 if (err < 0)
1910                         return err;
1911         }
1912         if (spec->cap_mixer) {
1913                 err = snd_hda_add_new_ctls(codec, spec->cap_mixer);
1914                 if (err < 0)
1915                         return err;
1916         }
1917         if (spec->multiout.dig_out_nid) {
1918                 err = snd_hda_create_spdif_out_ctls(codec,
1919                                                     spec->multiout.dig_out_nid,
1920                                                     spec->multiout.dig_out_nid);
1921                 if (err < 0)
1922                         return err;
1923                 if (!spec->no_analog) {
1924                         err = snd_hda_create_spdif_share_sw(codec,
1925                                                             &spec->multiout);
1926                         if (err < 0)
1927                                 return err;
1928                         spec->multiout.share_spdif = 1;
1929                 }
1930         }
1931         if (spec->dig_in_nid) {
1932                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1933                 if (err < 0)
1934                         return err;
1935         }
1936
1937 #ifdef CONFIG_SND_HDA_INPUT_BEEP
1938         /* create beep controls if needed */
1939         if (spec->beep_amp) {
1940                 const struct snd_kcontrol_new *knew;
1941                 for (knew = alc_beep_mixer; knew->name; knew++) {
1942                         struct snd_kcontrol *kctl;
1943                         kctl = snd_ctl_new1(knew, codec);
1944                         if (!kctl)
1945                                 return -ENOMEM;
1946                         kctl->private_value = spec->beep_amp;
1947                         err = snd_hda_ctl_add(codec, 0, kctl);
1948                         if (err < 0)
1949                                 return err;
1950                 }
1951         }
1952 #endif
1953
1954         /* if we have no master control, let's create it */
1955         if (!spec->no_analog &&
1956             !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1957                 unsigned int vmaster_tlv[4];
1958                 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1959                                         HDA_OUTPUT, vmaster_tlv);
1960                 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
1961                                           vmaster_tlv, alc_slave_pfxs,
1962                                           "Playback Volume");
1963                 if (err < 0)
1964                         return err;
1965         }
1966         if (!spec->no_analog &&
1967             !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1968                 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
1969                                             NULL, alc_slave_pfxs,
1970                                             "Playback Switch",
1971                                             true, &spec->vmaster_mute.sw_kctl);
1972                 if (err < 0)
1973                         return err;
1974         }
1975
1976         /* assign Capture Source enums to NID */
1977         if (spec->capsrc_nids || spec->adc_nids) {
1978                 kctl = snd_hda_find_mixer_ctl(codec, "Capture Source");
1979                 if (!kctl)
1980                         kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1981                 for (i = 0; kctl && i < kctl->count; i++) {
1982                         err = snd_hda_add_nid(codec, kctl, i,
1983                                               get_capsrc(spec, i));
1984                         if (err < 0)
1985                                 return err;
1986                 }
1987         }
1988         if (spec->cap_mixer && spec->adc_nids) {
1989                 const char *kname = kctl ? kctl->id.name : NULL;
1990                 for (knew = spec->cap_mixer; knew->name; knew++) {
1991                         if (kname && strcmp(knew->name, kname) == 0)
1992                                 continue;
1993                         kctl = snd_hda_find_mixer_ctl(codec, knew->name);
1994                         for (i = 0; kctl && i < kctl->count; i++) {
1995                                 err = snd_hda_add_nid(codec, kctl, i,
1996                                                       spec->adc_nids[i]);
1997                                 if (err < 0)
1998                                         return err;
1999                         }
2000                 }
2001         }
2002
2003         /* other nid->control mapping */
2004         for (i = 0; i < spec->num_mixers; i++) {
2005                 for (knew = spec->mixers[i]; knew->name; knew++) {
2006                         if (knew->iface != NID_MAPPING)
2007                                 continue;
2008                         kctl = snd_hda_find_mixer_ctl(codec, knew->name);
2009                         if (kctl == NULL)
2010                                 continue;
2011                         u = knew->subdevice;
2012                         for (j = 0; j < 4; j++, u >>= 8) {
2013                                 nid = u & 0x3f;
2014                                 if (nid == 0)
2015                                         continue;
2016                                 switch (u & 0xc0) {
2017                                 case SUBDEV_SPEAKER_:
2018                                         nid = spec->autocfg.speaker_pins[nid];
2019                                         break;
2020                                 case SUBDEV_LINE_:
2021                                         nid = spec->autocfg.line_out_pins[nid];
2022                                         break;
2023                                 case SUBDEV_HP_:
2024                                         nid = spec->autocfg.hp_pins[nid];
2025                                         break;
2026                                 default:
2027                                         continue;
2028                                 }
2029                                 err = snd_hda_add_nid(codec, kctl, 0, nid);
2030                                 if (err < 0)
2031                                         return err;
2032                         }
2033                         u = knew->private_value;
2034                         for (j = 0; j < 4; j++, u >>= 8) {
2035                                 nid = u & 0xff;
2036                                 if (nid == 0)
2037                                         continue;
2038                                 err = snd_hda_add_nid(codec, kctl, 0, nid);
2039                                 if (err < 0)
2040                                         return err;
2041                         }
2042                 }
2043         }
2044
2045         alc_free_kctls(codec); /* no longer needed */
2046
2047         return 0;
2048 }
2049
2050 static int alc_build_controls(struct hda_codec *codec)
2051 {
2052         struct alc_spec *spec = codec->spec;
2053         int err = __alc_build_controls(codec);
2054         if (err < 0)
2055                 return err;
2056         err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
2057         if (err < 0)
2058                 return err;
2059         alc_apply_fixup(codec, ALC_FIXUP_ACT_BUILD);
2060         return 0;
2061 }
2062
2063
2064 /*
2065  * Common callbacks
2066  */
2067
2068 static void alc_init_special_input_src(struct hda_codec *codec);
2069 static void alc_auto_init_std(struct hda_codec *codec);
2070
2071 static int alc_init(struct hda_codec *codec)
2072 {
2073         struct alc_spec *spec = codec->spec;
2074         unsigned int i;
2075
2076         if (spec->init_hook)
2077                 spec->init_hook(codec);
2078
2079         alc_fix_pll(codec);
2080         alc_auto_init_amp(codec, spec->init_amp);
2081
2082         for (i = 0; i < spec->num_init_verbs; i++)
2083                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
2084         alc_init_special_input_src(codec);
2085         alc_auto_init_std(codec);
2086
2087         alc_apply_fixup(codec, ALC_FIXUP_ACT_INIT);
2088
2089         snd_hda_jack_report_sync(codec);
2090
2091         hda_call_check_power_status(codec, 0x01);
2092         return 0;
2093 }
2094
2095 static void alc_unsol_event(struct hda_codec *codec, unsigned int res)
2096 {
2097         struct alc_spec *spec = codec->spec;
2098
2099         if (spec->unsol_event)
2100                 spec->unsol_event(codec, res);
2101 }
2102
2103 #ifdef CONFIG_SND_HDA_POWER_SAVE
2104 static int alc_check_power_status(struct hda_codec *codec, hda_nid_t nid)
2105 {
2106         struct alc_spec *spec = codec->spec;
2107         return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
2108 }
2109 #endif
2110
2111 /*
2112  * Analog playback callbacks
2113  */
2114 static int alc_playback_pcm_open(struct hda_pcm_stream *hinfo,
2115                                     struct hda_codec *codec,
2116                                     struct snd_pcm_substream *substream)
2117 {
2118         struct alc_spec *spec = codec->spec;
2119         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
2120                                              hinfo);
2121 }
2122
2123 static int alc_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2124                                        struct hda_codec *codec,
2125                                        unsigned int stream_tag,
2126                                        unsigned int format,
2127                                        struct snd_pcm_substream *substream)
2128 {
2129         struct alc_spec *spec = codec->spec;
2130         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
2131                                                 stream_tag, format, substream);
2132 }
2133
2134 static int alc_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2135                                        struct hda_codec *codec,
2136                                        struct snd_pcm_substream *substream)
2137 {
2138         struct alc_spec *spec = codec->spec;
2139         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
2140 }
2141
2142 /*
2143  * Digital out
2144  */
2145 static int alc_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
2146                                         struct hda_codec *codec,
2147                                         struct snd_pcm_substream *substream)
2148 {
2149         struct alc_spec *spec = codec->spec;
2150         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2151 }
2152
2153 static int alc_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2154                                            struct hda_codec *codec,
2155                                            unsigned int stream_tag,
2156                                            unsigned int format,
2157                                            struct snd_pcm_substream *substream)
2158 {
2159         struct alc_spec *spec = codec->spec;
2160         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2161                                              stream_tag, format, substream);
2162 }
2163
2164 static int alc_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2165                                            struct hda_codec *codec,
2166                                            struct snd_pcm_substream *substream)
2167 {
2168         struct alc_spec *spec = codec->spec;
2169         return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
2170 }
2171
2172 static int alc_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
2173                                          struct hda_codec *codec,
2174                                          struct snd_pcm_substream *substream)
2175 {
2176         struct alc_spec *spec = codec->spec;
2177         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2178 }
2179
2180 /*
2181  * Analog capture
2182  */
2183 static int alc_alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2184                                       struct hda_codec *codec,
2185                                       unsigned int stream_tag,
2186                                       unsigned int format,
2187                                       struct snd_pcm_substream *substream)
2188 {
2189         struct alc_spec *spec = codec->spec;
2190
2191         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
2192                                    stream_tag, 0, format);
2193         return 0;
2194 }
2195
2196 static int alc_alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2197                                       struct hda_codec *codec,
2198                                       struct snd_pcm_substream *substream)
2199 {
2200         struct alc_spec *spec = codec->spec;
2201
2202         snd_hda_codec_cleanup_stream(codec,
2203                                      spec->adc_nids[substream->number + 1]);
2204         return 0;
2205 }
2206
2207 /* analog capture with dynamic dual-adc changes */
2208 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2209                                        struct hda_codec *codec,
2210                                        unsigned int stream_tag,
2211                                        unsigned int format,
2212                                        struct snd_pcm_substream *substream)
2213 {
2214         struct alc_spec *spec = codec->spec;
2215         spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
2216         spec->cur_adc_stream_tag = stream_tag;
2217         spec->cur_adc_format = format;
2218         snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
2219         return 0;
2220 }
2221
2222 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2223                                        struct hda_codec *codec,
2224                                        struct snd_pcm_substream *substream)
2225 {
2226         struct alc_spec *spec = codec->spec;
2227         snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
2228         spec->cur_adc = 0;
2229         return 0;
2230 }
2231
2232 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
2233         .substreams = 1,
2234         .channels_min = 2,
2235         .channels_max = 2,
2236         .nid = 0, /* fill later */
2237         .ops = {
2238                 .prepare = dyn_adc_capture_pcm_prepare,
2239                 .cleanup = dyn_adc_capture_pcm_cleanup
2240         },
2241 };
2242
2243 /*
2244  */
2245 static const struct hda_pcm_stream alc_pcm_analog_playback = {
2246         .substreams = 1,
2247         .channels_min = 2,
2248         .channels_max = 8,
2249         /* NID is set in alc_build_pcms */
2250         .ops = {
2251                 .open = alc_playback_pcm_open,
2252                 .prepare = alc_playback_pcm_prepare,
2253                 .cleanup = alc_playback_pcm_cleanup
2254         },
2255 };
2256
2257 static const struct hda_pcm_stream alc_pcm_analog_capture = {
2258         .substreams = 1,
2259         .channels_min = 2,
2260         .channels_max = 2,
2261         /* NID is set in alc_build_pcms */
2262 };
2263
2264 static const struct hda_pcm_stream alc_pcm_analog_alt_playback = {
2265         .substreams = 1,
2266         .channels_min = 2,
2267         .channels_max = 2,
2268         /* NID is set in alc_build_pcms */
2269 };
2270
2271 static const struct hda_pcm_stream alc_pcm_analog_alt_capture = {
2272         .substreams = 2, /* can be overridden */
2273         .channels_min = 2,
2274         .channels_max = 2,
2275         /* NID is set in alc_build_pcms */
2276         .ops = {
2277                 .prepare = alc_alt_capture_pcm_prepare,
2278                 .cleanup = alc_alt_capture_pcm_cleanup
2279         },
2280 };
2281
2282 static const struct hda_pcm_stream alc_pcm_digital_playback = {
2283         .substreams = 1,
2284         .channels_min = 2,
2285         .channels_max = 2,
2286         /* NID is set in alc_build_pcms */
2287         .ops = {
2288                 .open = alc_dig_playback_pcm_open,
2289                 .close = alc_dig_playback_pcm_close,
2290                 .prepare = alc_dig_playback_pcm_prepare,
2291                 .cleanup = alc_dig_playback_pcm_cleanup
2292         },
2293 };
2294
2295 static const struct hda_pcm_stream alc_pcm_digital_capture = {
2296         .substreams = 1,
2297         .channels_min = 2,
2298         .channels_max = 2,
2299         /* NID is set in alc_build_pcms */
2300 };
2301
2302 /* Used by alc_build_pcms to flag that a PCM has no playback stream */
2303 static const struct hda_pcm_stream alc_pcm_null_stream = {
2304         .substreams = 0,
2305         .channels_min = 0,
2306         .channels_max = 0,
2307 };
2308
2309 static int alc_build_pcms(struct hda_codec *codec)
2310 {
2311         struct alc_spec *spec = codec->spec;
2312         struct hda_pcm *info = spec->pcm_rec;
2313         const struct hda_pcm_stream *p;
2314         bool have_multi_adcs;
2315         int i;
2316
2317         codec->num_pcms = 1;
2318         codec->pcm_info = info;
2319
2320         if (spec->no_analog)
2321                 goto skip_analog;
2322
2323         snprintf(spec->stream_name_analog, sizeof(spec->stream_name_analog),
2324                  "%s Analog", codec->chip_name);
2325         info->name = spec->stream_name_analog;
2326
2327         if (spec->multiout.num_dacs > 0) {
2328                 p = spec->stream_analog_playback;
2329                 if (!p)
2330                         p = &alc_pcm_analog_playback;
2331                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2332                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
2333         }
2334         if (spec->adc_nids) {
2335                 p = spec->stream_analog_capture;
2336                 if (!p) {
2337                         if (spec->dyn_adc_switch)
2338                                 p = &dyn_adc_pcm_analog_capture;
2339                         else
2340                                 p = &alc_pcm_analog_capture;
2341                 }
2342                 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2343                 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
2344         }
2345
2346         if (spec->channel_mode) {
2347                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = 0;
2348                 for (i = 0; i < spec->num_channel_mode; i++) {
2349                         if (spec->channel_mode[i].channels > info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max) {
2350                                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->channel_mode[i].channels;
2351                         }
2352                 }
2353         }
2354
2355  skip_analog:
2356         /* SPDIF for stream index #1 */
2357         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
2358                 snprintf(spec->stream_name_digital,
2359                          sizeof(spec->stream_name_digital),
2360                          "%s Digital", codec->chip_name);
2361                 codec->num_pcms = 2;
2362                 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
2363                 info = spec->pcm_rec + 1;
2364                 info->name = spec->stream_name_digital;
2365                 if (spec->dig_out_type)
2366                         info->pcm_type = spec->dig_out_type;
2367                 else
2368                         info->pcm_type = HDA_PCM_TYPE_SPDIF;
2369                 if (spec->multiout.dig_out_nid) {
2370                         p = spec->stream_digital_playback;
2371                         if (!p)
2372                                 p = &alc_pcm_digital_playback;
2373                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2374                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
2375                 }
2376                 if (spec->dig_in_nid) {
2377                         p = spec->stream_digital_capture;
2378                         if (!p)
2379                                 p = &alc_pcm_digital_capture;
2380                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2381                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
2382                 }
2383                 /* FIXME: do we need this for all Realtek codec models? */
2384                 codec->spdif_status_reset = 1;
2385         }
2386
2387         if (spec->no_analog)
2388                 return 0;
2389
2390         /* If the use of more than one ADC is requested for the current
2391          * model, configure a second analog capture-only PCM.
2392          */
2393         have_multi_adcs = (spec->num_adc_nids > 1) &&
2394                 !spec->dyn_adc_switch && !spec->auto_mic &&
2395                 (!spec->input_mux || spec->input_mux->num_items > 1);
2396         /* Additional Analaog capture for index #2 */
2397         if (spec->alt_dac_nid || have_multi_adcs) {
2398                 codec->num_pcms = 3;
2399                 info = spec->pcm_rec + 2;
2400                 info->name = spec->stream_name_analog;
2401                 if (spec->alt_dac_nid) {
2402                         p = spec->stream_analog_alt_playback;
2403                         if (!p)
2404                                 p = &alc_pcm_analog_alt_playback;
2405                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2406                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
2407                                 spec->alt_dac_nid;
2408                 } else {
2409                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
2410                                 alc_pcm_null_stream;
2411                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
2412                 }
2413                 if (have_multi_adcs) {
2414                         p = spec->stream_analog_alt_capture;
2415                         if (!p)
2416                                 p = &alc_pcm_analog_alt_capture;
2417                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2418                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
2419                                 spec->adc_nids[1];
2420                         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
2421                                 spec->num_adc_nids - 1;
2422                 } else {
2423                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
2424                                 alc_pcm_null_stream;
2425                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
2426                 }
2427         }
2428
2429         return 0;
2430 }
2431
2432 static inline void alc_shutup(struct hda_codec *codec)
2433 {
2434         struct alc_spec *spec = codec->spec;
2435
2436         if (spec && spec->shutup)
2437                 spec->shutup(codec);
2438         snd_hda_shutup_pins(codec);
2439 }
2440
2441 static void alc_free_kctls(struct hda_codec *codec)
2442 {
2443         struct alc_spec *spec = codec->spec;
2444
2445         if (spec->kctls.list) {
2446                 struct snd_kcontrol_new *kctl = spec->kctls.list;
2447                 int i;
2448                 for (i = 0; i < spec->kctls.used; i++)
2449                         kfree(kctl[i].name);
2450         }
2451         snd_array_free(&spec->kctls);
2452 }
2453
2454 static void alc_free_bind_ctls(struct hda_codec *codec)
2455 {
2456         struct alc_spec *spec = codec->spec;
2457         if (spec->bind_ctls.list) {
2458                 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
2459                 int i;
2460                 for (i = 0; i < spec->bind_ctls.used; i++)
2461                         kfree(ctl[i]);
2462         }
2463         snd_array_free(&spec->bind_ctls);
2464 }
2465
2466 static void alc_free(struct hda_codec *codec)
2467 {
2468         struct alc_spec *spec = codec->spec;
2469
2470         if (!spec)
2471                 return;
2472
2473         alc_shutup(codec);
2474         alc_free_kctls(codec);
2475         alc_free_bind_ctls(codec);
2476         kfree(spec);
2477         snd_hda_detach_beep_device(codec);
2478 }
2479
2480 #ifdef CONFIG_SND_HDA_POWER_SAVE
2481 static void alc_power_eapd(struct hda_codec *codec)
2482 {
2483         alc_auto_setup_eapd(codec, false);
2484 }
2485
2486 static int alc_suspend(struct hda_codec *codec, pm_message_t state)
2487 {
2488         struct alc_spec *spec = codec->spec;
2489         alc_shutup(codec);
2490         if (spec && spec->power_hook)
2491                 spec->power_hook(codec);
2492         return 0;
2493 }
2494 #endif
2495
2496 #ifdef CONFIG_PM
2497 static int alc_resume(struct hda_codec *codec)
2498 {
2499         msleep(150); /* to avoid pop noise */
2500         codec->patch_ops.init(codec);
2501         snd_hda_codec_resume_amp(codec);
2502         snd_hda_codec_resume_cache(codec);
2503         hda_call_check_power_status(codec, 0x01);
2504         return 0;
2505 }
2506 #endif
2507
2508 /*
2509  */
2510 static const struct hda_codec_ops alc_patch_ops = {
2511         .build_controls = alc_build_controls,
2512         .build_pcms = alc_build_pcms,
2513         .init = alc_init,
2514         .free = alc_free,
2515         .unsol_event = alc_unsol_event,
2516 #ifdef CONFIG_PM
2517         .resume = alc_resume,
2518 #endif
2519 #ifdef CONFIG_SND_HDA_POWER_SAVE
2520         .suspend = alc_suspend,
2521         .check_power_status = alc_check_power_status,
2522 #endif
2523         .reboot_notify = alc_shutup,
2524 };
2525
2526 /* replace the codec chip_name with the given string */
2527 static int alc_codec_rename(struct hda_codec *codec, const char *name)
2528 {
2529         kfree(codec->chip_name);
2530         codec->chip_name = kstrdup(name, GFP_KERNEL);
2531         if (!codec->chip_name) {
2532                 alc_free(codec);
2533                 return -ENOMEM;
2534         }
2535         return 0;
2536 }
2537
2538 /*
2539  * Rename codecs appropriately from COEF value
2540  */
2541 struct alc_codec_rename_table {
2542         unsigned int vendor_id;
2543         unsigned short coef_mask;
2544         unsigned short coef_bits;
2545         const char *name;
2546 };
2547
2548 static struct alc_codec_rename_table rename_tbl[] = {
2549         { 0x10ec0269, 0xfff0, 0x3010, "ALC277" },
2550         { 0x10ec0269, 0xf0f0, 0x2010, "ALC259" },
2551         { 0x10ec0269, 0xf0f0, 0x3010, "ALC258" },
2552         { 0x10ec0269, 0x00f0, 0x0010, "ALC269VB" },
2553         { 0x10ec0269, 0xffff, 0xa023, "ALC259" },
2554         { 0x10ec0269, 0xffff, 0x6023, "ALC281X" },
2555         { 0x10ec0269, 0x00f0, 0x0020, "ALC269VC" },
2556         { 0x10ec0887, 0x00f0, 0x0030, "ALC887-VD" },
2557         { 0x10ec0888, 0x00f0, 0x0030, "ALC888-VD" },
2558         { 0x10ec0888, 0xf0f0, 0x3020, "ALC886" },
2559         { 0x10ec0899, 0x2000, 0x2000, "ALC899" },
2560         { 0x10ec0892, 0xffff, 0x8020, "ALC661" },
2561         { 0x10ec0892, 0xffff, 0x8011, "ALC661" },
2562         { 0x10ec0892, 0xffff, 0x4011, "ALC656" },
2563         { } /* terminator */
2564 };
2565
2566 static int alc_codec_rename_from_preset(struct hda_codec *codec)
2567 {
2568         const struct alc_codec_rename_table *p;
2569
2570         for (p = rename_tbl; p->vendor_id; p++) {
2571                 if (p->vendor_id != codec->vendor_id)
2572                         continue;
2573                 if ((alc_get_coef0(codec) & p->coef_mask) == p->coef_bits)
2574                         return alc_codec_rename(codec, p->name);
2575         }
2576         return 0;
2577 }
2578
2579 /*
2580  * Automatic parse of I/O pins from the BIOS configuration
2581  */
2582
2583 enum {
2584         ALC_CTL_WIDGET_VOL,
2585         ALC_CTL_WIDGET_MUTE,
2586         ALC_CTL_BIND_MUTE,
2587         ALC_CTL_BIND_VOL,
2588         ALC_CTL_BIND_SW,
2589 };
2590 static const struct snd_kcontrol_new alc_control_templates[] = {
2591         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
2592         HDA_CODEC_MUTE(NULL, 0, 0, 0),
2593         HDA_BIND_MUTE(NULL, 0, 0, 0),
2594         HDA_BIND_VOL(NULL, 0),
2595         HDA_BIND_SW(NULL, 0),
2596 };
2597
2598 /* add dynamic controls */
2599 static int add_control(struct alc_spec *spec, int type, const char *name,
2600                        int cidx, unsigned long val)
2601 {
2602         struct snd_kcontrol_new *knew;
2603
2604         knew = alc_kcontrol_new(spec);
2605         if (!knew)
2606                 return -ENOMEM;
2607         *knew = alc_control_templates[type];
2608         knew->name = kstrdup(name, GFP_KERNEL);
2609         if (!knew->name)
2610                 return -ENOMEM;
2611         knew->index = cidx;
2612         if (get_amp_nid_(val))
2613                 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2614         knew->private_value = val;
2615         return 0;
2616 }
2617
2618 static int add_control_with_pfx(struct alc_spec *spec, int type,
2619                                 const char *pfx, const char *dir,
2620                                 const char *sfx, int cidx, unsigned long val)
2621 {
2622         char name[32];
2623         snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
2624         return add_control(spec, type, name, cidx, val);
2625 }
2626
2627 #define add_pb_vol_ctrl(spec, type, pfx, val)                   \
2628         add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
2629 #define add_pb_sw_ctrl(spec, type, pfx, val)                    \
2630         add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
2631 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val)                   \
2632         add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
2633 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val)                    \
2634         add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
2635
2636 static const char * const channel_name[4] = {
2637         "Front", "Surround", "CLFE", "Side"
2638 };
2639
2640 static const char *alc_get_line_out_pfx(struct alc_spec *spec, int ch,
2641                                         bool can_be_master, int *index)
2642 {
2643         struct auto_pin_cfg *cfg = &spec->autocfg;
2644
2645         *index = 0;
2646         if (cfg->line_outs == 1 && !spec->multi_ios &&
2647             !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
2648                 return "Master";
2649
2650         switch (cfg->line_out_type) {
2651         case AUTO_PIN_SPEAKER_OUT:
2652                 if (cfg->line_outs == 1)
2653                         return "Speaker";
2654                 if (cfg->line_outs == 2)
2655                         return ch ? "Bass Speaker" : "Speaker";
2656                 break;
2657         case AUTO_PIN_HP_OUT:
2658                 /* for multi-io case, only the primary out */
2659                 if (ch && spec->multi_ios)
2660                         break;
2661                 *index = ch;
2662                 return "Headphone";
2663         default:
2664                 if (cfg->line_outs == 1 && !spec->multi_ios)
2665                         return "PCM";
2666                 break;
2667         }
2668         if (snd_BUG_ON(ch >= ARRAY_SIZE(channel_name)))
2669                 return "PCM";
2670
2671         return channel_name[ch];
2672 }
2673
2674 #ifdef CONFIG_SND_HDA_POWER_SAVE
2675 /* add the powersave loopback-list entry */
2676 static void add_loopback_list(struct alc_spec *spec, hda_nid_t mix, int idx)
2677 {
2678         struct hda_amp_list *list;
2679
2680         if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2681                 return;
2682         list = spec->loopback_list + spec->num_loopbacks;
2683         list->nid = mix;
2684         list->dir = HDA_INPUT;
2685         list->idx = idx;
2686         spec->num_loopbacks++;
2687         spec->loopback.amplist = spec->loopback_list;
2688 }
2689 #else
2690 #define add_loopback_list(spec, mix, idx) /* NOP */
2691 #endif
2692
2693 /* create input playback/capture controls for the given pin */
2694 static int new_analog_input(struct alc_spec *spec, hda_nid_t pin,
2695                             const char *ctlname, int ctlidx,
2696                             int idx, hda_nid_t mix_nid)
2697 {
2698         int err;
2699
2700         err = __add_pb_vol_ctrl(spec, ALC_CTL_WIDGET_VOL, ctlname, ctlidx,
2701                           HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
2702         if (err < 0)
2703                 return err;
2704         err = __add_pb_sw_ctrl(spec, ALC_CTL_WIDGET_MUTE, ctlname, ctlidx,
2705                           HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
2706         if (err < 0)
2707                 return err;
2708         add_loopback_list(spec, mix_nid, idx);
2709         return 0;
2710 }
2711
2712 static int alc_is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2713 {
2714         unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2715         return (pincap & AC_PINCAP_IN) != 0;
2716 }
2717
2718 /* Parse the codec tree and retrieve ADCs and corresponding capsrc MUXs */
2719 static int alc_auto_fill_adc_caps(struct hda_codec *codec)
2720 {
2721         struct alc_spec *spec = codec->spec;
2722         hda_nid_t nid;
2723         hda_nid_t *adc_nids = spec->private_adc_nids;
2724         hda_nid_t *cap_nids = spec->private_capsrc_nids;
2725         int max_nums = ARRAY_SIZE(spec->private_adc_nids);
2726         int i, nums = 0;
2727
2728         nid = codec->start_nid;
2729         for (i = 0; i < codec->num_nodes; i++, nid++) {
2730                 hda_nid_t src;
2731                 const hda_nid_t *list;
2732                 unsigned int caps = get_wcaps(codec, nid);
2733                 int type = get_wcaps_type(caps);
2734
2735                 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2736                         continue;
2737                 adc_nids[nums] = nid;
2738                 cap_nids[nums] = nid;
2739                 src = nid;
2740                 for (;;) {
2741                         int n;
2742                         type = get_wcaps_type(get_wcaps(codec, src));
2743                         if (type == AC_WID_PIN)
2744                                 break;
2745                         if (type == AC_WID_AUD_SEL) {
2746                                 cap_nids[nums] = src;
2747                                 break;
2748                         }
2749                         n = snd_hda_get_conn_list(codec, src, &list);
2750                         if (n > 1) {
2751                                 cap_nids[nums] = src;
2752                                 break;
2753                         } else if (n != 1)
2754                                 break;
2755                         src = *list;
2756                 }
2757                 if (++nums >= max_nums)
2758                         break;
2759         }
2760         spec->adc_nids = spec->private_adc_nids;
2761         spec->capsrc_nids = spec->private_capsrc_nids;
2762         spec->num_adc_nids = nums;
2763         return nums;
2764 }
2765
2766 /* create playback/capture controls for input pins */
2767 static int alc_auto_create_input_ctls(struct hda_codec *codec)
2768 {
2769         struct alc_spec *spec = codec->spec;
2770         const struct auto_pin_cfg *cfg = &spec->autocfg;
2771         hda_nid_t mixer = spec->mixer_nid;
2772         struct hda_input_mux *imux = &spec->private_imux[0];
2773         int num_adcs;
2774         int i, c, err, idx, type_idx = 0;
2775         const char *prev_label = NULL;
2776
2777         num_adcs = alc_auto_fill_adc_caps(codec);
2778         if (num_adcs < 0)
2779                 return 0;
2780
2781         for (i = 0; i < cfg->num_inputs; i++) {
2782                 hda_nid_t pin;
2783                 const char *label;
2784
2785                 pin = cfg->inputs[i].pin;
2786                 if (!alc_is_input_pin(codec, pin))
2787                         continue;
2788
2789                 label = hda_get_autocfg_input_label(codec, cfg, i);
2790                 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2791                         label = "Headphone Mic";
2792                 if (prev_label && !strcmp(label, prev_label))
2793                         type_idx++;
2794                 else
2795                         type_idx = 0;
2796                 prev_label = label;
2797
2798                 if (mixer) {
2799                         idx = get_connection_index(codec, mixer, pin);
2800                         if (idx >= 0) {
2801                                 err = new_analog_input(spec, pin,
2802                                                        label, type_idx,
2803                                                        idx, mixer);
2804                                 if (err < 0)
2805                                         return err;
2806                         }
2807                 }
2808
2809                 for (c = 0; c < num_adcs; c++) {
2810                         hda_nid_t cap = get_capsrc(spec, c);
2811                         idx = get_connection_index(codec, cap, pin);
2812                         if (idx >= 0) {
2813                                 spec->imux_pins[imux->num_items] = pin;
2814                                 snd_hda_add_imux_item(imux, label, idx, NULL);
2815                                 break;
2816                         }
2817                 }
2818         }
2819
2820         spec->num_mux_defs = 1;
2821         spec->input_mux = imux;
2822
2823         return 0;
2824 }
2825
2826 /* create a shared input with the headphone out */
2827 static int alc_auto_create_shared_input(struct hda_codec *codec)
2828 {
2829         struct alc_spec *spec = codec->spec;
2830         struct auto_pin_cfg *cfg = &spec->autocfg;
2831         unsigned int defcfg;
2832         hda_nid_t nid;
2833
2834         /* only one internal input pin? */
2835         if (cfg->num_inputs != 1)
2836                 return 0;
2837         defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2838         if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2839                 return 0;
2840
2841         if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2842                 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2843         else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2844                 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2845         else
2846                 return 0; /* both not available */
2847
2848         if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2849                 return 0; /* no input */
2850
2851         cfg->inputs[1].pin = nid;
2852         cfg->inputs[1].type = AUTO_PIN_MIC;
2853         cfg->num_inputs = 2;
2854         spec->shared_mic_hp = 1;
2855         snd_printdd("realtek: Enable shared I/O jack on NID 0x%x\n", nid);
2856         return 0;
2857 }
2858
2859 static void alc_set_pin_output(struct hda_codec *codec, hda_nid_t nid,
2860                                unsigned int pin_type)
2861 {
2862         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2863                             pin_type);
2864         /* unmute pin */
2865         if (nid_has_mute(codec, nid, HDA_OUTPUT))
2866                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2867                             AMP_OUT_UNMUTE);
2868 }
2869
2870 static int get_pin_type(int line_out_type)
2871 {
2872         if (line_out_type == AUTO_PIN_HP_OUT)
2873                 return PIN_HP;
2874         else
2875                 return PIN_OUT;
2876 }
2877
2878 static void alc_auto_init_analog_input(struct hda_codec *codec)
2879 {
2880         struct alc_spec *spec = codec->spec;
2881         struct auto_pin_cfg *cfg = &spec->autocfg;
2882         int i;
2883
2884         for (i = 0; i < cfg->num_inputs; i++) {
2885                 hda_nid_t nid = cfg->inputs[i].pin;
2886                 if (alc_is_input_pin(codec, nid)) {
2887                         alc_set_input_pin(codec, nid, cfg->inputs[i].type);
2888                         if (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP)
2889                                 snd_hda_codec_write(codec, nid, 0,
2890                                                     AC_VERB_SET_AMP_GAIN_MUTE,
2891                                                     AMP_OUT_MUTE);
2892                 }
2893         }
2894
2895         /* mute all loopback inputs */
2896         if (spec->mixer_nid) {
2897                 int nums = snd_hda_get_conn_list(codec, spec->mixer_nid, NULL);
2898                 for (i = 0; i < nums; i++)
2899                         snd_hda_codec_write(codec, spec->mixer_nid, 0,
2900                                             AC_VERB_SET_AMP_GAIN_MUTE,
2901                                             AMP_IN_MUTE(i));
2902         }
2903 }
2904
2905 /* convert from MIX nid to DAC */
2906 static hda_nid_t alc_auto_mix_to_dac(struct hda_codec *codec, hda_nid_t nid)
2907 {
2908         hda_nid_t list[5];
2909         int i, num;
2910
2911         if (get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_AUD_OUT)
2912                 return nid;
2913         num = snd_hda_get_connections(codec, nid, list, ARRAY_SIZE(list));
2914         for (i = 0; i < num; i++) {
2915                 if (get_wcaps_type(get_wcaps(codec, list[i])) == AC_WID_AUD_OUT)
2916                         return list[i];
2917         }
2918         return 0;
2919 }
2920
2921 /* go down to the selector widget before the mixer */
2922 static hda_nid_t alc_go_down_to_selector(struct hda_codec *codec, hda_nid_t pin)
2923 {
2924         hda_nid_t srcs[5];
2925         int num = snd_hda_get_connections(codec, pin, srcs,
2926                                           ARRAY_SIZE(srcs));
2927         if (num != 1 ||
2928             get_wcaps_type(get_wcaps(codec, srcs[0])) != AC_WID_AUD_SEL)
2929                 return pin;
2930         return srcs[0];
2931 }
2932
2933 /* get MIX nid connected to the given pin targeted to DAC */
2934 static hda_nid_t alc_auto_dac_to_mix(struct hda_codec *codec, hda_nid_t pin,
2935                                    hda_nid_t dac)
2936 {
2937         hda_nid_t mix[5];
2938         int i, num;
2939
2940         pin = alc_go_down_to_selector(codec, pin);
2941         num = snd_hda_get_connections(codec, pin, mix, ARRAY_SIZE(mix));
2942         for (i = 0; i < num; i++) {
2943                 if (alc_auto_mix_to_dac(codec, mix[i]) == dac)
2944                         return mix[i];
2945         }
2946         return 0;
2947 }
2948
2949 /* select the connection from pin to DAC if needed */
2950 static int alc_auto_select_dac(struct hda_codec *codec, hda_nid_t pin,
2951                                hda_nid_t dac)
2952 {
2953         hda_nid_t mix[5];
2954         int i, num;
2955
2956         pin = alc_go_down_to_selector(codec, pin);
2957         num = snd_hda_get_connections(codec, pin, mix, ARRAY_SIZE(mix));
2958         if (num < 2)
2959                 return 0;
2960         for (i = 0; i < num; i++) {
2961                 if (alc_auto_mix_to_dac(codec, mix[i]) == dac) {
2962                         snd_hda_codec_update_cache(codec, pin, 0,
2963                                                    AC_VERB_SET_CONNECT_SEL, i);
2964                         return 0;
2965                 }
2966         }
2967         return 0;
2968 }
2969
2970 static bool alc_is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
2971 {
2972         struct alc_spec *spec = codec->spec;
2973         int i;
2974         if (found_in_nid_list(nid, spec->multiout.dac_nids,
2975                               ARRAY_SIZE(spec->private_dac_nids)) ||
2976             found_in_nid_list(nid, spec->multiout.hp_out_nid,
2977                               ARRAY_SIZE(spec->multiout.hp_out_nid)) ||
2978             found_in_nid_list(nid, spec->multiout.extra_out_nid,
2979                               ARRAY_SIZE(spec->multiout.extra_out_nid)))
2980                 return true;
2981         for (i = 0; i < spec->multi_ios; i++) {
2982                 if (spec->multi_io[i].dac == nid)
2983                         return true;
2984         }
2985         return false;
2986 }
2987
2988 /* look for an empty DAC slot */
2989 static hda_nid_t alc_auto_look_for_dac(struct hda_codec *codec, hda_nid_t pin)
2990 {
2991         hda_nid_t srcs[5];
2992         int i, num;
2993
2994         pin = alc_go_down_to_selector(codec, pin);
2995         num = snd_hda_get_connections(codec, pin, srcs, ARRAY_SIZE(srcs));
2996         for (i = 0; i < num; i++) {
2997                 hda_nid_t nid = alc_auto_mix_to_dac(codec, srcs[i]);
2998                 if (!nid)
2999                         continue;
3000                 if (!alc_is_dac_already_used(codec, nid))
3001                         return nid;
3002         }
3003         return 0;
3004 }
3005
3006 /* check whether the DAC is reachable from the pin */
3007 static bool alc_auto_is_dac_reachable(struct hda_codec *codec,
3008                                       hda_nid_t pin, hda_nid_t dac)
3009 {
3010         hda_nid_t srcs[5];
3011         int i, num;
3012
3013         if (!pin || !dac)
3014                 return false;
3015         pin = alc_go_down_to_selector(codec, pin);
3016         num = snd_hda_get_connections(codec, pin, srcs, ARRAY_SIZE(srcs));
3017         for (i = 0; i < num; i++) {
3018                 hda_nid_t nid = alc_auto_mix_to_dac(codec, srcs[i]);
3019                 if (nid == dac)
3020                         return true;
3021         }
3022         return false;
3023 }
3024
3025 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
3026 {
3027         struct alc_spec *spec = codec->spec;
3028         hda_nid_t sel = alc_go_down_to_selector(codec, pin);
3029         hda_nid_t nid, nid_found, srcs[5];
3030         int i, num = snd_hda_get_connections(codec, sel, srcs,
3031                                           ARRAY_SIZE(srcs));
3032         if (num == 1)
3033                 return alc_auto_look_for_dac(codec, pin);
3034         nid_found = 0;
3035         for (i = 0; i < num; i++) {
3036                 if (srcs[i] == spec->mixer_nid)
3037                         continue;
3038                 nid = alc_auto_mix_to_dac(codec, srcs[i]);
3039                 if (nid && !alc_is_dac_already_used(codec, nid)) {
3040                         if (nid_found)
3041                                 return 0;
3042                         nid_found = nid;
3043                 }
3044         }
3045         return nid_found;
3046 }
3047
3048 /* mark up volume and mute control NIDs: used during badness parsing and
3049  * at creating actual controls
3050  */
3051 static inline unsigned int get_ctl_pos(unsigned int data)
3052 {
3053         hda_nid_t nid = get_amp_nid_(data);
3054         unsigned int dir;
3055         if (snd_BUG_ON(nid >= MAX_VOL_NIDS))
3056                 return 0;
3057         dir = get_amp_direction_(data);
3058         return (nid << 1) | dir;
3059 }
3060
3061 #define is_ctl_used(bits, data) \
3062         test_bit(get_ctl_pos(data), bits)
3063 #define mark_ctl_usage(bits, data) \
3064         set_bit(get_ctl_pos(data), bits)
3065
3066 static void clear_vol_marks(struct hda_codec *codec)
3067 {
3068         struct alc_spec *spec = codec->spec;
3069         memset(spec->vol_ctls, 0, sizeof(spec->vol_ctls));
3070         memset(spec->sw_ctls, 0, sizeof(spec->sw_ctls));
3071 }
3072
3073 /* badness definition */
3074 enum {
3075         /* No primary DAC is found for the main output */
3076         BAD_NO_PRIMARY_DAC = 0x10000,
3077         /* No DAC is found for the extra output */
3078         BAD_NO_DAC = 0x4000,
3079         /* No possible multi-ios */
3080         BAD_MULTI_IO = 0x103,
3081         /* No individual DAC for extra output */
3082         BAD_NO_EXTRA_DAC = 0x102,
3083         /* No individual DAC for extra surrounds */
3084         BAD_NO_EXTRA_SURR_DAC = 0x101,
3085         /* Primary DAC shared with main surrounds */
3086         BAD_SHARED_SURROUND = 0x100,
3087         /* Primary DAC shared with main CLFE */
3088         BAD_SHARED_CLFE = 0x10,
3089         /* Primary DAC shared with extra surrounds */
3090         BAD_SHARED_EXTRA_SURROUND = 0x10,
3091         /* Volume widget is shared */
3092         BAD_SHARED_VOL = 0x10,
3093 };
3094
3095 static hda_nid_t alc_look_for_out_mute_nid(struct hda_codec *codec,
3096                                            hda_nid_t pin, hda_nid_t dac);
3097 static hda_nid_t alc_look_for_out_vol_nid(struct hda_codec *codec,
3098                                           hda_nid_t pin, hda_nid_t dac);
3099
3100 static int eval_shared_vol_badness(struct hda_codec *codec, hda_nid_t pin,
3101                                    hda_nid_t dac)
3102 {
3103         struct alc_spec *spec = codec->spec;
3104         hda_nid_t nid;
3105         unsigned int val;
3106         int badness = 0;
3107
3108         nid = alc_look_for_out_vol_nid(codec, pin, dac);
3109         if (nid) {
3110                 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3111                 if (is_ctl_used(spec->vol_ctls, nid))
3112                         badness += BAD_SHARED_VOL;
3113                 else
3114                         mark_ctl_usage(spec->vol_ctls, val);
3115         } else
3116                 badness += BAD_SHARED_VOL;
3117         nid = alc_look_for_out_mute_nid(codec, pin, dac);
3118         if (nid) {
3119                 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
3120                 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT)
3121                         val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3122                 else
3123                         val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
3124                 if (is_ctl_used(spec->sw_ctls, val))
3125                         badness += BAD_SHARED_VOL;
3126                 else
3127                         mark_ctl_usage(spec->sw_ctls, val);
3128         } else
3129                 badness += BAD_SHARED_VOL;
3130         return badness;
3131 }
3132
3133 struct badness_table {
3134         int no_primary_dac;     /* no primary DAC */
3135         int no_dac;             /* no secondary DACs */
3136         int shared_primary;     /* primary DAC is shared with main output */
3137         int shared_surr;        /* secondary DAC shared with main or primary */
3138         int shared_clfe;        /* third DAC shared with main or primary */
3139         int shared_surr_main;   /* secondary DAC sahred with main/DAC0 */
3140 };
3141
3142 static struct badness_table main_out_badness = {
3143         .no_primary_dac = BAD_NO_PRIMARY_DAC,
3144         .no_dac = BAD_NO_DAC,
3145         .shared_primary = BAD_NO_PRIMARY_DAC,
3146         .shared_surr = BAD_SHARED_SURROUND,
3147         .shared_clfe = BAD_SHARED_CLFE,
3148         .shared_surr_main = BAD_SHARED_SURROUND,
3149 };
3150
3151 static struct badness_table extra_out_badness = {
3152         .no_primary_dac = BAD_NO_DAC,
3153         .no_dac = BAD_NO_DAC,
3154         .shared_primary = BAD_NO_EXTRA_DAC,
3155         .shared_surr = BAD_SHARED_EXTRA_SURROUND,
3156         .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
3157         .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
3158 };
3159
3160 /* try to assign DACs to pins and return the resultant badness */
3161 static int alc_auto_fill_dacs(struct hda_codec *codec, int num_outs,
3162                               const hda_nid_t *pins, hda_nid_t *dacs,
3163                               const struct badness_table *bad)
3164 {
3165         struct alc_spec *spec = codec->spec;
3166         struct auto_pin_cfg *cfg = &spec->autocfg;
3167         int i, j;
3168         int badness = 0;
3169         hda_nid_t dac;
3170
3171         if (!num_outs)
3172                 return 0;
3173
3174         for (i = 0; i < num_outs; i++) {
3175                 hda_nid_t pin = pins[i];
3176                 if (!dacs[i])
3177                         dacs[i] = alc_auto_look_for_dac(codec, pin);
3178                 if (!dacs[i] && !i) {
3179                         for (j = 1; j < num_outs; j++) {
3180                                 if (alc_auto_is_dac_reachable(codec, pin, dacs[j])) {
3181                                         dacs[0] = dacs[j];
3182                                         dacs[j] = 0;
3183                                         break;
3184                                 }
3185                         }
3186                 }
3187                 dac = dacs[i];
3188                 if (!dac) {
3189                         if (alc_auto_is_dac_reachable(codec, pin, dacs[0]))
3190                                 dac = dacs[0];
3191                         else if (cfg->line_outs > i &&
3192                                  alc_auto_is_dac_reachable(codec, pin,
3193                                         spec->private_dac_nids[i]))
3194                                 dac = spec->private_dac_nids[i];
3195                         if (dac) {
3196                                 if (!i)
3197                                         badness += bad->shared_primary;
3198                                 else if (i == 1)
3199                                         badness += bad->shared_surr;
3200                                 else
3201                                         badness += bad->shared_clfe;
3202                         } else if (alc_auto_is_dac_reachable(codec, pin,
3203                                         spec->private_dac_nids[0])) {
3204                                 dac = spec->private_dac_nids[0];
3205                                 badness += bad->shared_surr_main;
3206                         } else if (!i)
3207                                 badness += bad->no_primary_dac;
3208                         else
3209                                 badness += bad->no_dac;
3210                 }
3211                 if (dac)
3212                         badness += eval_shared_vol_badness(codec, pin, dac);
3213         }
3214
3215         return badness;
3216 }
3217
3218 static int alc_auto_fill_multi_ios(struct hda_codec *codec,
3219                                    hda_nid_t reference_pin,
3220                                    bool hardwired, int offset);
3221
3222 static bool alc_map_singles(struct hda_codec *codec, int outs,
3223                             const hda_nid_t *pins, hda_nid_t *dacs)
3224 {
3225         int i;
3226         bool found = false;
3227         for (i = 0; i < outs; i++) {
3228                 if (dacs[i])
3229                         continue;
3230                 dacs[i] = get_dac_if_single(codec, pins[i]);
3231                 if (dacs[i])
3232                         found = true;
3233         }
3234         return found;
3235 }
3236
3237 /* fill in the dac_nids table from the parsed pin configuration */
3238 static int fill_and_eval_dacs(struct hda_codec *codec,
3239                               bool fill_hardwired,
3240                               bool fill_mio_first)
3241 {
3242         struct alc_spec *spec = codec->spec;
3243         struct auto_pin_cfg *cfg = &spec->autocfg;
3244         int i, err, badness;
3245
3246         /* set num_dacs once to full for alc_auto_look_for_dac() */
3247         spec->multiout.num_dacs = cfg->line_outs;
3248         spec->multiout.dac_nids = spec->private_dac_nids;
3249         memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
3250         memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
3251         memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
3252         spec->multi_ios = 0;
3253         clear_vol_marks(codec);
3254         badness = 0;
3255
3256         /* fill hard-wired DACs first */
3257         if (fill_hardwired) {
3258                 bool mapped;
3259                 do {
3260                         mapped = alc_map_singles(codec, cfg->line_outs,
3261                                                  cfg->line_out_pins,
3262                                                  spec->private_dac_nids);
3263                         mapped |= alc_map_singles(codec, cfg->hp_outs,
3264                                                   cfg->hp_pins,
3265                                                   spec->multiout.hp_out_nid);
3266                         mapped |= alc_map_singles(codec, cfg->speaker_outs,
3267                                                   cfg->speaker_pins,
3268                                                   spec->multiout.extra_out_nid);
3269                         if (fill_mio_first && cfg->line_outs == 1 &&
3270                             cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3271                                 err = alc_auto_fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
3272                                 if (!err)
3273                                         mapped = true;
3274                         }
3275                 } while (mapped);
3276         }
3277
3278         badness += alc_auto_fill_dacs(codec, cfg->line_outs, cfg->line_out_pins,
3279                                       spec->private_dac_nids,
3280                                       &main_out_badness);
3281
3282         /* re-count num_dacs and squash invalid entries */
3283         spec->multiout.num_dacs = 0;
3284         for (i = 0; i < cfg->line_outs; i++) {
3285                 if (spec->private_dac_nids[i])
3286                         spec->multiout.num_dacs++;
3287                 else {
3288                         memmove(spec->private_dac_nids + i,
3289                                 spec->private_dac_nids + i + 1,
3290                                 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
3291                         spec->private_dac_nids[cfg->line_outs - 1] = 0;
3292                 }
3293         }
3294
3295         if (fill_mio_first &&
3296             cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3297                 /* try to fill multi-io first */
3298                 err = alc_auto_fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
3299                 if (err < 0)
3300                         return err;
3301                 /* we don't count badness at this stage yet */
3302         }
3303
3304         if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3305                 err = alc_auto_fill_dacs(codec, cfg->hp_outs, cfg->hp_pins,
3306                                          spec->multiout.hp_out_nid,
3307                                          &extra_out_badness);
3308                 if (err < 0)
3309                         return err;
3310                 badness += err;
3311         }
3312         if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3313                 err = alc_auto_fill_dacs(codec, cfg->speaker_outs,
3314                                          cfg->speaker_pins,
3315                                          spec->multiout.extra_out_nid,
3316                                          &extra_out_badness);
3317                 if (err < 0)
3318                         return err;
3319                 badness += err;
3320         }
3321         if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3322                 err = alc_auto_fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
3323                 if (err < 0)
3324                         return err;
3325                 badness += err;
3326         }
3327         if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3328                 /* try multi-ios with HP + inputs */
3329                 int offset = 0;
3330                 if (cfg->line_outs >= 3)
3331                         offset = 1;
3332                 err = alc_auto_fill_multi_ios(codec, cfg->hp_pins[0], false,
3333                                               offset);
3334                 if (err < 0)
3335                         return err;
3336                 badness += err;
3337         }
3338
3339         if (spec->multi_ios == 2) {
3340                 for (i = 0; i < 2; i++)
3341                         spec->private_dac_nids[spec->multiout.num_dacs++] =
3342                                 spec->multi_io[i].dac;
3343                 spec->ext_channel_count = 2;
3344         } else if (spec->multi_ios) {
3345                 spec->multi_ios = 0;
3346                 badness += BAD_MULTI_IO;
3347         }
3348
3349         return badness;
3350 }
3351
3352 #define DEBUG_BADNESS
3353
3354 #ifdef DEBUG_BADNESS
3355 #define debug_badness   snd_printdd
3356 #else
3357 #define debug_badness(...)
3358 #endif
3359
3360 static void debug_show_configs(struct alc_spec *spec, struct auto_pin_cfg *cfg)
3361 {
3362         debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
3363                       cfg->line_out_pins[0], cfg->line_out_pins[1],
3364                       cfg->line_out_pins[2], cfg->line_out_pins[2],
3365                       spec->multiout.dac_nids[0],
3366                       spec->multiout.dac_nids[1],
3367                       spec->multiout.dac_nids[2],
3368                       spec->multiout.dac_nids[3]);
3369         if (spec->multi_ios > 0)
3370                 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
3371                               spec->multi_ios,
3372                               spec->multi_io[0].pin, spec->multi_io[1].pin,
3373                               spec->multi_io[0].dac, spec->multi_io[1].dac);
3374         debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
3375                       cfg->hp_pins[0], cfg->hp_pins[1],
3376                       cfg->hp_pins[2], cfg->hp_pins[2],
3377                       spec->multiout.hp_out_nid[0],
3378                       spec->multiout.hp_out_nid[1],
3379                       spec->multiout.hp_out_nid[2],
3380                       spec->multiout.hp_out_nid[3]);
3381         debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
3382                       cfg->speaker_pins[0], cfg->speaker_pins[1],
3383                       cfg->speaker_pins[2], cfg->speaker_pins[3],
3384                       spec->multiout.extra_out_nid[0],
3385                       spec->multiout.extra_out_nid[1],
3386                       spec->multiout.extra_out_nid[2],
3387                       spec->multiout.extra_out_nid[3]);
3388 }
3389
3390 static int alc_auto_fill_dac_nids(struct hda_codec *codec)
3391 {
3392         struct alc_spec *spec = codec->spec;
3393         struct auto_pin_cfg *cfg = &spec->autocfg;
3394         struct auto_pin_cfg *best_cfg;
3395         int best_badness = INT_MAX;
3396         int badness;
3397         bool fill_hardwired = true, fill_mio_first = true;
3398         bool best_wired = true, best_mio = true;
3399         bool hp_spk_swapped = false;
3400
3401         best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
3402         if (!best_cfg)
3403                 return -ENOMEM;
3404         *best_cfg = *cfg;
3405
3406         for (;;) {
3407                 badness = fill_and_eval_dacs(codec, fill_hardwired,
3408                                              fill_mio_first);
3409                 if (badness < 0) {
3410                         kfree(best_cfg);
3411                         return badness;
3412                 }
3413                 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
3414                               cfg->line_out_type, fill_hardwired, fill_mio_first,
3415                               badness);
3416                 debug_show_configs(spec, cfg);
3417                 if (badness < best_badness) {
3418                         best_badness = badness;
3419                         *best_cfg = *cfg;
3420                         best_wired = fill_hardwired;
3421                         best_mio = fill_mio_first;
3422                 }
3423                 if (!badness)
3424                         break;
3425                 fill_mio_first = !fill_mio_first;
3426                 if (!fill_mio_first)
3427                         continue;
3428                 fill_hardwired = !fill_hardwired;
3429                 if (!fill_hardwired)
3430                         continue;
3431                 if (hp_spk_swapped)
3432                         break;
3433                 hp_spk_swapped = true;
3434                 if (cfg->speaker_outs > 0 &&
3435                     cfg->line_out_type == AUTO_PIN_HP_OUT) {
3436                         cfg->hp_outs = cfg->line_outs;
3437                         memcpy(cfg->hp_pins, cfg->line_out_pins,
3438                                sizeof(cfg->hp_pins));
3439                         cfg->line_outs = cfg->speaker_outs;
3440                         memcpy(cfg->line_out_pins, cfg->speaker_pins,
3441                                sizeof(cfg->speaker_pins));
3442                         cfg->speaker_outs = 0;
3443                         memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3444                         cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3445                         fill_hardwired = true;
3446                         continue;
3447                 }
3448                 if (cfg->hp_outs > 0 &&
3449                     cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3450                         cfg->speaker_outs = cfg->line_outs;
3451                         memcpy(cfg->speaker_pins, cfg->line_out_pins,
3452                                sizeof(cfg->speaker_pins));
3453                         cfg->line_outs = cfg->hp_outs;
3454                         memcpy(cfg->line_out_pins, cfg->hp_pins,
3455                                sizeof(cfg->hp_pins));
3456                         cfg->hp_outs = 0;
3457                         memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3458                         cfg->line_out_type = AUTO_PIN_HP_OUT;
3459                         fill_hardwired = true;
3460                         continue;
3461                 }
3462                 break;
3463         }
3464
3465         if (badness) {
3466                 *cfg = *best_cfg;
3467                 fill_and_eval_dacs(codec, best_wired, best_mio);
3468         }
3469         debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
3470                       cfg->line_out_type, best_wired, best_mio);
3471         debug_show_configs(spec, cfg);
3472
3473         if (cfg->line_out_pins[0])
3474                 spec->vmaster_nid =
3475                         alc_look_for_out_vol_nid(codec, cfg->line_out_pins[0],
3476                                                  spec->multiout.dac_nids[0]);
3477
3478         /* clear the bitmap flags for creating controls */
3479         clear_vol_marks(codec);
3480         kfree(best_cfg);
3481         return 0;
3482 }
3483
3484 static int alc_auto_add_vol_ctl(struct hda_codec *codec,
3485                               const char *pfx, int cidx,
3486                               hda_nid_t nid, unsigned int chs)
3487 {
3488         struct alc_spec *spec = codec->spec;
3489         unsigned int val;
3490         if (!nid)
3491                 return 0;
3492         val = HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT);
3493         if (is_ctl_used(spec->vol_ctls, val) && chs != 2) /* exclude LFE */
3494                 return 0;
3495         mark_ctl_usage(spec->vol_ctls, val);
3496         return __add_pb_vol_ctrl(codec->spec, ALC_CTL_WIDGET_VOL, pfx, cidx,
3497                                  val);
3498 }
3499
3500 static int alc_auto_add_stereo_vol(struct hda_codec *codec,
3501                                    const char *pfx, int cidx,
3502                                    hda_nid_t nid)
3503 {
3504         int chs = 1;
3505         if (get_wcaps(codec, nid) & AC_WCAP_STEREO)
3506                 chs = 3;
3507         return alc_auto_add_vol_ctl(codec, pfx, cidx, nid, chs);
3508 }
3509
3510 /* create a mute-switch for the given mixer widget;
3511  * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
3512  */
3513 static int alc_auto_add_sw_ctl(struct hda_codec *codec,
3514                              const char *pfx, int cidx,
3515                              hda_nid_t nid, unsigned int chs)
3516 {
3517         struct alc_spec *spec = codec->spec;
3518         int wid_type;
3519         int type;
3520         unsigned long val;
3521         if (!nid)
3522                 return 0;
3523         wid_type = get_wcaps_type(get_wcaps(codec, nid));
3524         if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT) {
3525                 type = ALC_CTL_WIDGET_MUTE;
3526                 val = HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT);
3527         } else if (snd_hda_get_conn_list(codec, nid, NULL) == 1) {
3528                 type = ALC_CTL_WIDGET_MUTE;
3529                 val = HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_INPUT);
3530         } else {
3531                 type = ALC_CTL_BIND_MUTE;
3532                 val = HDA_COMPOSE_AMP_VAL(nid, chs, 2, HDA_INPUT);
3533         }
3534         if (is_ctl_used(spec->sw_ctls, val) && chs != 2) /* exclude LFE */
3535                 return 0;
3536         mark_ctl_usage(spec->sw_ctls, val);
3537         return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
3538 }
3539
3540 static int alc_auto_add_stereo_sw(struct hda_codec *codec, const char *pfx,
3541                                   int cidx, hda_nid_t nid)
3542 {
3543         int chs = 1;
3544         if (get_wcaps(codec, nid) & AC_WCAP_STEREO)
3545                 chs = 3;
3546         return alc_auto_add_sw_ctl(codec, pfx, cidx, nid, chs);
3547 }
3548
3549 static hda_nid_t alc_look_for_out_mute_nid(struct hda_codec *codec,
3550                                            hda_nid_t pin, hda_nid_t dac)
3551 {
3552         hda_nid_t mix = alc_auto_dac_to_mix(codec, pin, dac);
3553         if (nid_has_mute(codec, pin, HDA_OUTPUT))
3554                 return pin;
3555         else if (mix && nid_has_mute(codec, mix, HDA_INPUT))
3556                 return mix;
3557         else if (nid_has_mute(codec, dac, HDA_OUTPUT))
3558                 return dac;
3559         return 0;
3560 }
3561
3562 static hda_nid_t alc_look_for_out_vol_nid(struct hda_codec *codec,
3563                                           hda_nid_t pin, hda_nid_t dac)
3564 {
3565         hda_nid_t mix = alc_auto_dac_to_mix(codec, pin, dac);
3566         if (nid_has_volume(codec, dac, HDA_OUTPUT))
3567                 return dac;
3568         else if (nid_has_volume(codec, mix, HDA_OUTPUT))
3569                 return mix;
3570         else if (nid_has_volume(codec, pin, HDA_OUTPUT))
3571                 return pin;
3572         return 0;
3573 }
3574
3575 /* add playback controls from the parsed DAC table */
3576 static int alc_auto_create_multi_out_ctls(struct hda_codec *codec,
3577                                              const struct auto_pin_cfg *cfg)
3578 {
3579         struct alc_spec *spec = codec->spec;
3580         int i, err, noutputs;
3581
3582         noutputs = cfg->line_outs;
3583         if (spec->multi_ios > 0 && cfg->line_outs < 3)
3584                 noutputs += spec->multi_ios;
3585
3586         for (i = 0; i < noutputs; i++) {
3587                 const char *name;
3588                 int index;
3589                 hda_nid_t dac, pin;
3590                 hda_nid_t sw, vol;
3591
3592                 dac = spec->multiout.dac_nids[i];
3593                 if (!dac)
3594                         continue;
3595                 if (i >= cfg->line_outs) {
3596                         pin = spec->multi_io[i - 1].pin;
3597                         index = 0;
3598                         name = channel_name[i];
3599                 } else {
3600                         pin = cfg->line_out_pins[i];
3601                         name = alc_get_line_out_pfx(spec, i, true, &index);
3602                 }
3603
3604                 sw = alc_look_for_out_mute_nid(codec, pin, dac);
3605                 vol = alc_look_for_out_vol_nid(codec, pin, dac);
3606                 if (!name || !strcmp(name, "CLFE")) {
3607                         /* Center/LFE */
3608                         err = alc_auto_add_vol_ctl(codec, "Center", 0, vol, 1);
3609                         if (err < 0)
3610                                 return err;
3611                         err = alc_auto_add_vol_ctl(codec, "LFE", 0, vol, 2);
3612                         if (err < 0)
3613                                 return err;
3614                         err = alc_auto_add_sw_ctl(codec, "Center", 0, sw, 1);
3615                         if (err < 0)
3616                                 return err;
3617                         err = alc_auto_add_sw_ctl(codec, "LFE", 0, sw, 2);
3618                         if (err < 0)
3619                                 return err;
3620                 } else {
3621                         err = alc_auto_add_stereo_vol(codec, name, index, vol);
3622                         if (err < 0)
3623                                 return err;
3624                         err = alc_auto_add_stereo_sw(codec, name, index, sw);
3625                         if (err < 0)
3626                                 return err;
3627                 }
3628         }
3629         return 0;
3630 }
3631
3632 static int alc_auto_create_extra_out(struct hda_codec *codec, hda_nid_t pin,
3633                                      hda_nid_t dac, const char *pfx,
3634                                      int cidx)
3635 {
3636         struct alc_spec *spec = codec->spec;
3637         hda_nid_t sw, vol;
3638         int err;
3639
3640         if (!dac) {
3641                 unsigned int val;
3642                 /* the corresponding DAC is already occupied */
3643                 if (!(get_wcaps(codec, pin) & AC_WCAP_OUT_AMP))
3644                         return 0; /* no way */
3645                 /* create a switch only */
3646                 val = HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT);
3647                 if (is_ctl_used(spec->sw_ctls, val))
3648                         return 0; /* already created */
3649                 mark_ctl_usage(spec->sw_ctls, val);
3650                 return __add_pb_sw_ctrl(spec, ALC_CTL_WIDGET_MUTE, pfx, cidx, val);
3651         }
3652
3653         sw = alc_look_for_out_mute_nid(codec, pin, dac);
3654         vol = alc_look_for_out_vol_nid(codec, pin, dac);
3655         err = alc_auto_add_stereo_vol(codec, pfx, cidx, vol);
3656         if (err < 0)
3657                 return err;
3658         err = alc_auto_add_stereo_sw(codec, pfx, cidx, sw);
3659         if (err < 0)
3660                 return err;
3661         return 0;
3662 }
3663
3664 static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
3665                                           unsigned int nums,
3666                                           struct hda_ctl_ops *ops)
3667 {
3668         struct alc_spec *spec = codec->spec;
3669         struct hda_bind_ctls **ctlp, *ctl;
3670         snd_array_init(&spec->bind_ctls, sizeof(ctl), 8);
3671         ctlp = snd_array_new(&spec->bind_ctls);
3672         if (!ctlp)
3673                 return NULL;
3674         ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
3675         *ctlp = ctl;
3676         if (ctl)
3677                 ctl->ops = ops;
3678         return ctl;
3679 }
3680
3681 /* add playback controls for speaker and HP outputs */
3682 static int alc_auto_create_extra_outs(struct hda_codec *codec, int num_pins,
3683                                       const hda_nid_t *pins,
3684                                       const hda_nid_t *dacs,
3685                                       const char *pfx)
3686 {
3687         struct alc_spec *spec = codec->spec;
3688         struct hda_bind_ctls *ctl;
3689         char name[32];
3690         int i, n, err;
3691
3692         if (!num_pins || !pins[0])
3693                 return 0;
3694
3695         if (num_pins == 1) {
3696                 hda_nid_t dac = *dacs;
3697                 if (!dac)
3698                         dac = spec->multiout.dac_nids[0];
3699                 return alc_auto_create_extra_out(codec, *pins, dac, pfx, 0);
3700         }
3701
3702         for (i = 0; i < num_pins; i++) {
3703                 hda_nid_t dac;
3704                 if (dacs[num_pins - 1])
3705                         dac = dacs[i]; /* with individual volumes */
3706                 else
3707                         dac = 0;
3708                 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
3709                         err = alc_auto_create_extra_out(codec, pins[i], dac,
3710                                                         "Bass Speaker", 0);
3711                 } else if (num_pins >= 3) {
3712                         snprintf(name, sizeof(name), "%s %s",
3713                                  pfx, channel_name[i]);
3714                         err = alc_auto_create_extra_out(codec, pins[i], dac,
3715                                                         name, 0);
3716                 } else {
3717                         err = alc_auto_create_extra_out(codec, pins[i], dac,
3718                                                         pfx, i);
3719                 }
3720                 if (err < 0)
3721                         return err;
3722         }
3723         if (dacs[num_pins - 1])
3724                 return 0;
3725
3726         /* Let's create a bind-controls for volumes */
3727         ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
3728         if (!ctl)
3729                 return -ENOMEM;
3730         n = 0;
3731         for (i = 0; i < num_pins; i++) {
3732                 hda_nid_t vol;
3733                 if (!pins[i] || !dacs[i])
3734                         continue;
3735                 vol = alc_look_for_out_vol_nid(codec, pins[i], dacs[i]);
3736                 if (vol)
3737                         ctl->values[n++] =
3738                                 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
3739         }
3740         if (n) {
3741                 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
3742                 err = add_control(spec, ALC_CTL_BIND_VOL, name, 0, (long)ctl);
3743                 if (err < 0)
3744                         return err;
3745         }
3746         return 0;
3747 }
3748
3749 static int alc_auto_create_hp_out(struct hda_codec *codec)
3750 {
3751         struct alc_spec *spec = codec->spec;
3752         return alc_auto_create_extra_outs(codec, spec->autocfg.hp_outs,
3753                                           spec->autocfg.hp_pins,
3754                                           spec->multiout.hp_out_nid,
3755                                           "Headphone");
3756 }
3757
3758 static int alc_auto_create_speaker_out(struct hda_codec *codec)
3759 {
3760         struct alc_spec *spec = codec->spec;
3761         return alc_auto_create_extra_outs(codec, spec->autocfg.speaker_outs,
3762                                           spec->autocfg.speaker_pins,
3763                                           spec->multiout.extra_out_nid,
3764                                           "Speaker");
3765 }
3766
3767 static void alc_auto_set_output_and_unmute(struct hda_codec *codec,
3768                                               hda_nid_t pin, int pin_type,
3769                                               hda_nid_t dac)
3770 {
3771         int i, num;
3772         hda_nid_t nid, mix = 0;
3773         hda_nid_t srcs[HDA_MAX_CONNECTIONS];
3774
3775         alc_set_pin_output(codec, pin, pin_type);
3776         nid = alc_go_down_to_selector(codec, pin);
3777         num = snd_hda_get_connections(codec, nid, srcs, ARRAY_SIZE(srcs));
3778         for (i = 0; i < num; i++) {
3779                 if (alc_auto_mix_to_dac(codec, srcs[i]) != dac)
3780                         continue;
3781                 mix = srcs[i];
3782                 break;
3783         }
3784         if (!mix)
3785                 return;
3786
3787         /* need the manual connection? */
3788         if (num > 1)
3789                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, i);
3790         /* unmute mixer widget inputs */
3791         if (nid_has_mute(codec, mix, HDA_INPUT)) {
3792                 snd_hda_codec_write(codec, mix, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3793                             AMP_IN_UNMUTE(0));
3794                 snd_hda_codec_write(codec, mix, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3795                             AMP_IN_UNMUTE(1));
3796         }
3797         /* initialize volume */
3798         nid = alc_look_for_out_vol_nid(codec, pin, dac);
3799         if (nid)
3800                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3801                                     AMP_OUT_ZERO);
3802
3803         /* unmute DAC if it's not assigned to a mixer */
3804         nid = alc_look_for_out_mute_nid(codec, pin, dac);
3805         if (nid == mix && nid_has_mute(codec, dac, HDA_OUTPUT))
3806                 snd_hda_codec_write(codec, dac, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3807                                     AMP_OUT_ZERO);
3808 }
3809
3810 static void alc_auto_init_multi_out(struct hda_codec *codec)
3811 {
3812         struct alc_spec *spec = codec->spec;
3813         int pin_type = get_pin_type(spec->autocfg.line_out_type);
3814         int i;
3815
3816         for (i = 0; i <= HDA_SIDE; i++) {
3817                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
3818                 if (nid)
3819                         alc_auto_set_output_and_unmute(codec, nid, pin_type,
3820                                         spec->multiout.dac_nids[i]);
3821         }
3822 }
3823
3824 static void alc_auto_init_extra_out(struct hda_codec *codec)
3825 {
3826         struct alc_spec *spec = codec->spec;
3827         int i;
3828         hda_nid_t pin, dac;
3829
3830         for (i = 0; i < spec->autocfg.hp_outs; i++) {
3831                 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3832                         break;
3833                 pin = spec->autocfg.hp_pins[i];
3834                 if (!pin)
3835                         break;
3836                 dac = spec->multiout.hp_out_nid[i];
3837                 if (!dac) {
3838                         if (i > 0 && spec->multiout.hp_out_nid[0])
3839                                 dac = spec->multiout.hp_out_nid[0];
3840                         else
3841                                 dac = spec->multiout.dac_nids[0];
3842                 }
3843                 alc_auto_set_output_and_unmute(codec, pin, PIN_HP, dac);
3844         }
3845         for (i = 0; i < spec->autocfg.speaker_outs; i++) {
3846                 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3847                         break;
3848                 pin = spec->autocfg.speaker_pins[i];
3849                 if (!pin)
3850                         break;
3851                 dac = spec->multiout.extra_out_nid[i];
3852                 if (!dac) {
3853                         if (i > 0 && spec->multiout.extra_out_nid[0])
3854                                 dac = spec->multiout.extra_out_nid[0];
3855                         else
3856                                 dac = spec->multiout.dac_nids[0];
3857                 }
3858                 alc_auto_set_output_and_unmute(codec, pin, PIN_OUT, dac);
3859         }
3860 }
3861
3862 /* check whether the given pin can be a multi-io pin */
3863 static bool can_be_multiio_pin(struct hda_codec *codec,
3864                                unsigned int location, hda_nid_t nid)
3865 {
3866         unsigned int defcfg, caps;
3867
3868         defcfg = snd_hda_codec_get_pincfg(codec, nid);
3869         if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
3870                 return false;
3871         if (location && get_defcfg_location(defcfg) != location)
3872                 return false;
3873         caps = snd_hda_query_pin_caps(codec, nid);
3874         if (!(caps & AC_PINCAP_OUT))
3875                 return false;
3876         return true;
3877 }
3878
3879 /*
3880  * multi-io helper
3881  *
3882  * When hardwired is set, try to fill ony hardwired pins, and returns
3883  * zero if any pins are filled, non-zero if nothing found.
3884  * When hardwired is off, try to fill possible input pins, and returns
3885  * the badness value.
3886  */
3887 static int alc_auto_fill_multi_ios(struct hda_codec *codec,
3888                                    hda_nid_t reference_pin,
3889                                    bool hardwired, int offset)
3890 {
3891         struct alc_spec *spec = codec->spec;
3892         struct auto_pin_cfg *cfg = &spec->autocfg;
3893         int type, i, j, dacs, num_pins, old_pins;
3894         unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
3895         unsigned int location = get_defcfg_location(defcfg);
3896         int badness = 0;
3897
3898         old_pins = spec->multi_ios;
3899         if (old_pins >= 2)
3900                 goto end_fill;
3901
3902         num_pins = 0;
3903         for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
3904                 for (i = 0; i < cfg->num_inputs; i++) {
3905                         if (cfg->inputs[i].type != type)
3906                                 continue;
3907                         if (can_be_multiio_pin(codec, location,
3908                                                cfg->inputs[i].pin))
3909                                 num_pins++;
3910                 }
3911         }
3912         if (num_pins < 2)
3913                 goto end_fill;
3914
3915         dacs = spec->multiout.num_dacs;
3916         for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
3917                 for (i = 0; i < cfg->num_inputs; i++) {
3918                         hda_nid_t nid = cfg->inputs[i].pin;
3919                         hda_nid_t dac = 0;
3920
3921                         if (cfg->inputs[i].type != type)
3922                                 continue;
3923                         if (!can_be_multiio_pin(codec, location, nid))
3924                                 continue;
3925                         for (j = 0; j < spec->multi_ios; j++) {
3926                                 if (nid == spec->multi_io[j].pin)
3927                                         break;
3928                         }
3929                         if (j < spec->multi_ios)
3930                                 continue;
3931
3932                         if (offset && offset + spec->multi_ios < dacs) {
3933                                 dac = spec->private_dac_nids[offset + spec->multi_ios];
3934                                 if (!alc_auto_is_dac_reachable(codec, nid, dac))
3935                                         dac = 0;
3936                         }
3937                         if (hardwired)
3938                                 dac = get_dac_if_single(codec, nid);
3939                         else if (!dac)
3940                                 dac = alc_auto_look_for_dac(codec, nid);
3941                         if (!dac) {
3942                                 badness++;
3943                                 continue;
3944                         }
3945                         spec->multi_io[spec->multi_ios].pin = nid;
3946                         spec->multi_io[spec->multi_ios].dac = dac;
3947                         spec->multi_ios++;
3948                         if (spec->multi_ios >= 2)
3949                                 break;
3950                 }
3951         }
3952  end_fill:
3953         if (badness)
3954                 badness = BAD_MULTI_IO;
3955         if (old_pins == spec->multi_ios) {
3956                 if (hardwired)
3957                         return 1; /* nothing found */
3958                 else
3959                         return badness; /* no badness if nothing found */
3960         }
3961         if (!hardwired && spec->multi_ios < 2) {
3962                 spec->multi_ios = old_pins;
3963                 return badness;
3964         }
3965
3966         return 0;
3967 }
3968
3969 static int alc_auto_ch_mode_info(struct snd_kcontrol *kcontrol,
3970                                  struct snd_ctl_elem_info *uinfo)
3971 {
3972         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3973         struct alc_spec *spec = codec->spec;
3974
3975         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3976         uinfo->count = 1;
3977         uinfo->value.enumerated.items = spec->multi_ios + 1;
3978         if (uinfo->value.enumerated.item > spec->multi_ios)
3979                 uinfo->value.enumerated.item = spec->multi_ios;
3980         sprintf(uinfo->value.enumerated.name, "%dch",
3981                 (uinfo->value.enumerated.item + 1) * 2);
3982         return 0;
3983 }
3984
3985 static int alc_auto_ch_mode_get(struct snd_kcontrol *kcontrol,
3986                                 struct snd_ctl_elem_value *ucontrol)
3987 {
3988         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3989         struct alc_spec *spec = codec->spec;
3990         ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
3991         return 0;
3992 }
3993
3994 static int alc_set_multi_io(struct hda_codec *codec, int idx, bool output)
3995 {
3996         struct alc_spec *spec = codec->spec;
3997         hda_nid_t nid = spec->multi_io[idx].pin;
3998
3999         if (!spec->multi_io[idx].ctl_in)
4000                 spec->multi_io[idx].ctl_in =
4001                         snd_hda_codec_read(codec, nid, 0,
4002                                            AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
4003         if (output) {
4004                 snd_hda_codec_update_cache(codec, nid, 0,
4005                                            AC_VERB_SET_PIN_WIDGET_CONTROL,
4006                                            PIN_OUT);
4007                 if (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP)
4008                         snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
4009                                                  HDA_AMP_MUTE, 0);
4010                 alc_auto_select_dac(codec, nid, spec->multi_io[idx].dac);
4011         } else {
4012                 if (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP)
4013                         snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
4014                                                  HDA_AMP_MUTE, HDA_AMP_MUTE);
4015                 snd_hda_codec_update_cache(codec, nid, 0,
4016                                            AC_VERB_SET_PIN_WIDGET_CONTROL,
4017                                            spec->multi_io[idx].ctl_in);
4018         }
4019         return 0;
4020 }
4021
4022 static int alc_auto_ch_mode_put(struct snd_kcontrol *kcontrol,
4023                                 struct snd_ctl_elem_value *ucontrol)
4024 {
4025         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4026         struct alc_spec *spec = codec->spec;
4027         int i, ch;
4028
4029         ch = ucontrol->value.enumerated.item[0];
4030         if (ch < 0 || ch > spec->multi_ios)
4031                 return -EINVAL;
4032         if (ch == (spec->ext_channel_count - 1) / 2)
4033                 return 0;
4034         spec->ext_channel_count = (ch + 1) * 2;
4035         for (i = 0; i < spec->multi_ios; i++)
4036                 alc_set_multi_io(codec, i, i < ch);
4037         spec->multiout.max_channels = spec->ext_channel_count;
4038         if (spec->need_dac_fix && !spec->const_channel_count)
4039                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
4040         return 1;
4041 }
4042
4043 static const struct snd_kcontrol_new alc_auto_channel_mode_enum = {
4044         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4045         .name = "Channel Mode",
4046         .info = alc_auto_ch_mode_info,
4047         .get = alc_auto_ch_mode_get,
4048         .put = alc_auto_ch_mode_put,
4049 };
4050
4051 static int alc_auto_add_multi_channel_mode(struct hda_codec *codec)
4052 {
4053         struct alc_spec *spec = codec->spec;
4054
4055         if (spec->multi_ios > 0) {
4056                 struct snd_kcontrol_new *knew;
4057
4058                 knew = alc_kcontrol_new(spec);
4059                 if (!knew)
4060                         return -ENOMEM;
4061                 *knew = alc_auto_channel_mode_enum;
4062                 knew->name = kstrdup("Channel Mode", GFP_KERNEL);
4063                 if (!knew->name)
4064                         return -ENOMEM;
4065         }
4066         return 0;
4067 }
4068
4069 /* filter out invalid adc_nids (and capsrc_nids) that don't give all
4070  * active input pins
4071  */
4072 static void alc_remove_invalid_adc_nids(struct hda_codec *codec)
4073 {
4074         struct alc_spec *spec = codec->spec;
4075         const struct hda_input_mux *imux;
4076         hda_nid_t adc_nids[ARRAY_SIZE(spec->private_adc_nids)];
4077         hda_nid_t capsrc_nids[ARRAY_SIZE(spec->private_adc_nids)];
4078         int i, n, nums;
4079
4080         imux = spec->input_mux;
4081         if (!imux)
4082                 return;
4083         if (spec->dyn_adc_switch)
4084                 return;
4085
4086  again:
4087         nums = 0;
4088         for (n = 0; n < spec->num_adc_nids; n++) {
4089                 hda_nid_t cap = spec->private_capsrc_nids[n];
4090                 int num_conns = snd_hda_get_conn_list(codec, cap, NULL);
4091                 for (i = 0; i < imux->num_items; i++) {
4092                         hda_nid_t pin = spec->imux_pins[i];
4093                         if (pin) {
4094                                 if (get_connection_index(codec, cap, pin) < 0)
4095                                         break;
4096                         } else if (num_conns <= imux->items[i].index)
4097                                 break;
4098                 }
4099                 if (i >= imux->num_items) {
4100                         adc_nids[nums] = spec->private_adc_nids[n];
4101                         capsrc_nids[nums++] = cap;
4102                 }
4103         }
4104         if (!nums) {
4105                 /* check whether ADC-switch is possible */
4106                 if (!alc_check_dyn_adc_switch(codec)) {
4107                         if (spec->shared_mic_hp) {
4108                                 spec->shared_mic_hp = 0;
4109                                 spec->private_imux[0].num_items = 1;
4110                                 goto again;
4111                         }
4112                         printk(KERN_WARNING "hda_codec: %s: no valid ADC found;"
4113                                " using fallback 0x%x\n",
4114                                codec->chip_name, spec->private_adc_nids[0]);
4115                         spec->num_adc_nids = 1;
4116                         spec->auto_mic = 0;
4117                         return;
4118                 }
4119         } else if (nums != spec->num_adc_nids) {
4120                 memcpy(spec->private_adc_nids, adc_nids,
4121                        nums * sizeof(hda_nid_t));
4122                 memcpy(spec->private_capsrc_nids, capsrc_nids,
4123                        nums * sizeof(hda_nid_t));
4124                 spec->num_adc_nids = nums;
4125         }
4126
4127         if (spec->auto_mic)
4128                 alc_auto_mic_check_imux(codec); /* check auto-mic setups */
4129         else if (spec->input_mux->num_items == 1 || spec->shared_mic_hp)
4130                 spec->num_adc_nids = 1; /* reduce to a single ADC */
4131 }
4132
4133 /*
4134  * initialize ADC paths
4135  */
4136 static void alc_auto_init_adc(struct hda_codec *codec, int adc_idx)
4137 {
4138         struct alc_spec *spec = codec->spec;
4139         hda_nid_t nid;
4140
4141         nid = spec->adc_nids[adc_idx];
4142         /* mute ADC */
4143         if (nid_has_mute(codec, nid, HDA_INPUT)) {
4144                 snd_hda_codec_write(codec, nid, 0,
4145                                     AC_VERB_SET_AMP_GAIN_MUTE,
4146                                     AMP_IN_MUTE(0));
4147                 return;
4148         }
4149         if (!spec->capsrc_nids)
4150                 return;
4151         nid = spec->capsrc_nids[adc_idx];
4152         if (nid_has_mute(codec, nid, HDA_OUTPUT))
4153                 snd_hda_codec_write(codec, nid, 0,
4154                                     AC_VERB_SET_AMP_GAIN_MUTE,
4155                                     AMP_OUT_MUTE);
4156 }
4157
4158 static void alc_auto_init_input_src(struct hda_codec *codec)
4159 {
4160         struct alc_spec *spec = codec->spec;
4161         int c, nums;
4162
4163         for (c = 0; c < spec->num_adc_nids; c++)
4164                 alc_auto_init_adc(codec, c);
4165         if (spec->dyn_adc_switch)
4166                 nums = 1;
4167         else
4168                 nums = spec->num_adc_nids;
4169         for (c = 0; c < nums; c++)
4170                 alc_mux_select(codec, c, spec->cur_mux[c], true);
4171 }
4172
4173 /* add mic boosts if needed */
4174 static int alc_auto_add_mic_boost(struct hda_codec *codec)
4175 {
4176         struct alc_spec *spec = codec->spec;
4177         struct auto_pin_cfg *cfg = &spec->autocfg;
4178         int i, err;
4179         int type_idx = 0;
4180         hda_nid_t nid;
4181         const char *prev_label = NULL;
4182
4183         for (i = 0; i < cfg->num_inputs; i++) {
4184                 if (cfg->inputs[i].type > AUTO_PIN_MIC)
4185                         break;
4186                 nid = cfg->inputs[i].pin;
4187                 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
4188                         const char *label;
4189                         char boost_label[32];
4190
4191                         label = hda_get_autocfg_input_label(codec, cfg, i);
4192                         if (spec->shared_mic_hp && !strcmp(label, "Misc"))
4193                                 label = "Headphone Mic";
4194                         if (prev_label && !strcmp(label, prev_label))
4195                                 type_idx++;
4196                         else
4197                                 type_idx = 0;
4198                         prev_label = label;
4199
4200                         snprintf(boost_label, sizeof(boost_label),
4201                                  "%s Boost Volume", label);
4202                         err = add_control(spec, ALC_CTL_WIDGET_VOL,
4203                                           boost_label, type_idx,
4204                                   HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT));
4205                         if (err < 0)
4206                                 return err;
4207                 }
4208         }
4209         return 0;
4210 }
4211
4212 /* select or unmute the given capsrc route */
4213 static void select_or_unmute_capsrc(struct hda_codec *codec, hda_nid_t cap,
4214                                     int idx)
4215 {
4216         if (get_wcaps_type(get_wcaps(codec, cap)) == AC_WID_AUD_MIX) {
4217                 snd_hda_codec_amp_stereo(codec, cap, HDA_INPUT, idx,
4218                                          HDA_AMP_MUTE, 0);
4219         } else if (snd_hda_get_conn_list(codec, cap, NULL) > 1) {
4220                 snd_hda_codec_write_cache(codec, cap, 0,
4221                                           AC_VERB_SET_CONNECT_SEL, idx);
4222         }
4223 }
4224
4225 /* set the default connection to that pin */
4226 static int init_capsrc_for_pin(struct hda_codec *codec, hda_nid_t pin)
4227 {
4228         struct alc_spec *spec = codec->spec;
4229         int i;
4230
4231         if (!pin)
4232                 return 0;
4233         for (i = 0; i < spec->num_adc_nids; i++) {
4234                 hda_nid_t cap = get_capsrc(spec, i);
4235                 int idx;
4236
4237                 idx = get_connection_index(codec, cap, pin);
4238                 if (idx < 0)
4239                         continue;
4240                 select_or_unmute_capsrc(codec, cap, idx);
4241                 return i; /* return the found index */
4242         }
4243         return -1; /* not found */
4244 }
4245
4246 /* initialize some special cases for input sources */
4247 static void alc_init_special_input_src(struct hda_codec *codec)
4248 {
4249         struct alc_spec *spec = codec->spec;
4250         int i;
4251
4252         for (i = 0; i < spec->autocfg.num_inputs; i++)
4253                 init_capsrc_for_pin(codec, spec->autocfg.inputs[i].pin);
4254 }
4255
4256 /* assign appropriate capture mixers */
4257 static void set_capture_mixer(struct hda_codec *codec)
4258 {
4259         struct alc_spec *spec = codec->spec;
4260         static const struct snd_kcontrol_new *caps[2][3] = {
4261                 { alc_capture_mixer_nosrc1,
4262                   alc_capture_mixer_nosrc2,
4263                   alc_capture_mixer_nosrc3 },
4264                 { alc_capture_mixer1,
4265                   alc_capture_mixer2,
4266                   alc_capture_mixer3 },
4267         };
4268
4269         /* check whether either of ADC or MUX has a volume control */
4270         if (!nid_has_volume(codec, spec->adc_nids[0], HDA_INPUT)) {
4271                 if (!spec->capsrc_nids)
4272                         return; /* no volume */
4273                 if (!nid_has_volume(codec, spec->capsrc_nids[0], HDA_OUTPUT))
4274                         return; /* no volume in capsrc, too */
4275                 spec->vol_in_capsrc = 1;
4276         }
4277
4278         if (spec->num_adc_nids > 0) {
4279                 int mux = 0;
4280                 int num_adcs = 0;
4281
4282                 if (spec->input_mux && spec->input_mux->num_items > 1)
4283                         mux = 1;
4284                 if (spec->auto_mic) {
4285                         num_adcs = 1;
4286                         mux = 0;
4287                 } else if (spec->dyn_adc_switch)
4288                         num_adcs = 1;
4289                 if (!num_adcs) {
4290                         if (spec->num_adc_nids > 3)
4291                                 spec->num_adc_nids = 3;
4292                         else if (!spec->num_adc_nids)
4293                                 return;
4294                         num_adcs = spec->num_adc_nids;
4295                 }
4296                 spec->cap_mixer = caps[mux][num_adcs - 1];
4297         }
4298 }
4299
4300 /*
4301  * standard auto-parser initializations
4302  */
4303 static void alc_auto_init_std(struct hda_codec *codec)
4304 {
4305         struct alc_spec *spec = codec->spec;
4306         alc_auto_init_multi_out(codec);
4307         alc_auto_init_extra_out(codec);
4308         alc_auto_init_analog_input(codec);
4309         alc_auto_init_input_src(codec);
4310         alc_auto_init_digital(codec);
4311         if (spec->unsol_event)
4312                 alc_inithook(codec);
4313 }
4314
4315 /*
4316  * Digital-beep handlers
4317  */
4318 #ifdef CONFIG_SND_HDA_INPUT_BEEP
4319 #define set_beep_amp(spec, nid, idx, dir) \
4320         ((spec)->beep_amp = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir))
4321
4322 static const struct snd_pci_quirk beep_white_list[] = {
4323         SND_PCI_QUIRK(0x1043, 0x829f, "ASUS", 1),
4324         SND_PCI_QUIRK(0x1043, 0x83ce, "EeePC", 1),
4325         SND_PCI_QUIRK(0x1043, 0x831a, "EeePC", 1),
4326         SND_PCI_QUIRK(0x1043, 0x834a, "EeePC", 1),
4327         SND_PCI_QUIRK(0x1458, 0xa002, "GA-MA790X", 1),
4328         SND_PCI_QUIRK(0x8086, 0xd613, "Intel", 1),
4329         {}
4330 };
4331
4332 static inline int has_cdefine_beep(struct hda_codec *codec)
4333 {
4334         struct alc_spec *spec = codec->spec;
4335         const struct snd_pci_quirk *q;
4336         q = snd_pci_quirk_lookup(codec->bus->pci, beep_white_list);
4337         if (q)
4338                 return q->value;
4339         return spec->cdefine.enable_pcbeep;
4340 }
4341 #else
4342 #define set_beep_amp(spec, nid, idx, dir) /* NOP */
4343 #define has_cdefine_beep(codec)         0
4344 #endif
4345
4346 /* parse the BIOS configuration and set up the alc_spec */
4347 /* return 1 if successful, 0 if the proper config is not found,
4348  * or a negative error code
4349  */
4350 static int alc_parse_auto_config(struct hda_codec *codec,
4351                                  const hda_nid_t *ignore_nids,
4352                                  const hda_nid_t *ssid_nids)
4353 {
4354         struct alc_spec *spec = codec->spec;
4355         struct auto_pin_cfg *cfg = &spec->autocfg;
4356         int err;
4357
4358         err = snd_hda_parse_pin_defcfg(codec, cfg, ignore_nids,
4359                                        spec->parse_flags);
4360         if (err < 0)
4361                 return err;
4362         if (!cfg->line_outs) {
4363                 if (cfg->dig_outs || cfg->dig_in_pin) {
4364                         spec->multiout.max_channels = 2;
4365                         spec->no_analog = 1;
4366                         goto dig_only;
4367                 }
4368                 return 0; /* can't find valid BIOS pin config */
4369         }
4370
4371         if (!spec->no_primary_hp &&
4372             cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4373             cfg->line_outs <= cfg->hp_outs) {
4374                 /* use HP as primary out */
4375                 cfg->speaker_outs = cfg->line_outs;
4376                 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4377                        sizeof(cfg->speaker_pins));
4378                 cfg->line_outs = cfg->hp_outs;
4379                 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4380                 cfg->hp_outs = 0;
4381                 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4382                 cfg->line_out_type = AUTO_PIN_HP_OUT;
4383         }
4384
4385         err = alc_auto_fill_dac_nids(codec);
4386         if (err < 0)
4387                 return err;
4388         err = alc_auto_add_multi_channel_mode(codec);
4389         if (err < 0)
4390                 return err;
4391         err = alc_auto_create_multi_out_ctls(codec, cfg);
4392         if (err < 0)
4393                 return err;
4394         err = alc_auto_create_hp_out(codec);
4395         if (err < 0)
4396                 return err;
4397         err = alc_auto_create_speaker_out(codec);
4398         if (err < 0)
4399                 return err;
4400         err = alc_auto_create_shared_input(codec);
4401         if (err < 0)
4402                 return err;
4403         err = alc_auto_create_input_ctls(codec);
4404         if (err < 0)
4405                 return err;
4406
4407         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4408
4409  dig_only:
4410         alc_auto_parse_digital(codec);
4411
4412         if (!spec->no_analog)
4413                 alc_remove_invalid_adc_nids(codec);
4414
4415         if (ssid_nids)
4416                 alc_ssid_check(codec, ssid_nids);
4417
4418         if (!spec->no_analog) {
4419                 alc_auto_check_switches(codec);
4420                 err = alc_auto_add_mic_boost(codec);
4421                 if (err < 0)
4422                         return err;
4423         }
4424
4425         if (spec->kctls.list)
4426                 add_mixer(spec, spec->kctls.list);
4427
4428         if (!spec->no_analog && !spec->cap_mixer)
4429                 set_capture_mixer(codec);
4430
4431         return 1;
4432 }
4433
4434 static int alc880_parse_auto_config(struct hda_codec *codec)
4435 {
4436         static const hda_nid_t alc880_ignore[] = { 0x1d, 0 };
4437         static const hda_nid_t alc880_ssids[] = { 0x15, 0x1b, 0x14, 0 };
4438         return alc_parse_auto_config(codec, alc880_ignore, alc880_ssids);
4439 }
4440
4441 /*
4442  * ALC880 fix-ups
4443  */
4444 enum {
4445         ALC880_FIXUP_GPIO1,
4446         ALC880_FIXUP_GPIO2,
4447         ALC880_FIXUP_MEDION_RIM,
4448         ALC880_FIXUP_LG,
4449         ALC880_FIXUP_W810,
4450         ALC880_FIXUP_EAPD_COEF,
4451         ALC880_FIXUP_TCL_S700,
4452         ALC880_FIXUP_VOL_KNOB,
4453         ALC880_FIXUP_FUJITSU,
4454         ALC880_FIXUP_F1734,
4455         ALC880_FIXUP_UNIWILL,
4456         ALC880_FIXUP_UNIWILL_DIG,
4457         ALC880_FIXUP_Z71V,
4458         ALC880_FIXUP_3ST_BASE,
4459         ALC880_FIXUP_3ST,
4460         ALC880_FIXUP_3ST_DIG,
4461         ALC880_FIXUP_5ST_BASE,
4462         ALC880_FIXUP_5ST,
4463         ALC880_FIXUP_5ST_DIG,
4464         ALC880_FIXUP_6ST_BASE,
4465         ALC880_FIXUP_6ST,
4466         ALC880_FIXUP_6ST_DIG,
4467 };
4468
4469 /* enable the volume-knob widget support on NID 0x21 */
4470 static void alc880_fixup_vol_knob(struct hda_codec *codec,
4471                                   const struct alc_fixup *fix, int action)
4472 {
4473         if (action == ALC_FIXUP_ACT_PROBE)
4474                 snd_hda_jack_detect_enable(codec, 0x21, ALC_DCVOL_EVENT);
4475 }
4476
4477 static const struct alc_fixup alc880_fixups[] = {
4478         [ALC880_FIXUP_GPIO1] = {
4479                 .type = ALC_FIXUP_VERBS,
4480                 .v.verbs = alc_gpio1_init_verbs,
4481         },
4482         [ALC880_FIXUP_GPIO2] = {
4483                 .type = ALC_FIXUP_VERBS,
4484                 .v.verbs = alc_gpio2_init_verbs,
4485         },
4486         [ALC880_FIXUP_MEDION_RIM] = {
4487                 .type = ALC_FIXUP_VERBS,
4488                 .v.verbs = (const struct hda_verb[]) {
4489                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4490                         { 0x20, AC_VERB_SET_PROC_COEF,  0x3060 },
4491                         { }
4492                 },
4493                 .chained = true,
4494                 .chain_id = ALC880_FIXUP_GPIO2,
4495         },
4496         [ALC880_FIXUP_LG] = {
4497                 .type = ALC_FIXUP_PINS,
4498                 .v.pins = (const struct alc_pincfg[]) {
4499                         /* disable bogus unused pins */
4500                         { 0x16, 0x411111f0 },
4501                         { 0x18, 0x411111f0 },
4502                         { 0x1a, 0x411111f0 },
4503                         { }
4504                 }
4505         },
4506         [ALC880_FIXUP_W810] = {
4507                 .type = ALC_FIXUP_PINS,
4508                 .v.pins = (const struct alc_pincfg[]) {
4509                         /* disable bogus unused pins */
4510                         { 0x17, 0x411111f0 },
4511                         { }
4512                 },
4513                 .chained = true,
4514                 .chain_id = ALC880_FIXUP_GPIO2,
4515         },
4516         [ALC880_FIXUP_EAPD_COEF] = {
4517                 .type = ALC_FIXUP_VERBS,
4518                 .v.verbs = (const struct hda_verb[]) {
4519                         /* change to EAPD mode */
4520                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4521                         { 0x20, AC_VERB_SET_PROC_COEF,  0x3060 },
4522                         {}
4523                 },
4524         },
4525         [ALC880_FIXUP_TCL_S700] = {
4526                 .type = ALC_FIXUP_VERBS,
4527                 .v.verbs = (const struct hda_verb[]) {
4528                         /* change to EAPD mode */
4529                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4530                         { 0x20, AC_VERB_SET_PROC_COEF,  0x3070 },
4531                         {}
4532                 },
4533                 .chained = true,
4534                 .chain_id = ALC880_FIXUP_GPIO2,
4535         },
4536         [ALC880_FIXUP_VOL_KNOB] = {
4537                 .type = ALC_FIXUP_FUNC,
4538                 .v.func = alc880_fixup_vol_knob,
4539         },
4540         [ALC880_FIXUP_FUJITSU] = {
4541                 /* override all pins as BIOS on old Amilo is broken */
4542                 .type = ALC_FIXUP_PINS,
4543                 .v.pins = (const struct alc_pincfg[]) {
4544                         { 0x14, 0x0121411f }, /* HP */
4545                         { 0x15, 0x99030120 }, /* speaker */
4546                         { 0x16, 0x99030130 }, /* bass speaker */
4547                         { 0x17, 0x411111f0 }, /* N/A */
4548                         { 0x18, 0x411111f0 }, /* N/A */
4549                         { 0x19, 0x01a19950 }, /* mic-in */
4550                         { 0x1a, 0x411111f0 }, /* N/A */
4551                         { 0x1b, 0x411111f0 }, /* N/A */
4552                         { 0x1c, 0x411111f0 }, /* N/A */
4553                         { 0x1d, 0x411111f0 }, /* N/A */
4554                         { 0x1e, 0x01454140 }, /* SPDIF out */
4555                         { }
4556                 },
4557                 .chained = true,
4558                 .chain_id = ALC880_FIXUP_VOL_KNOB,
4559         },
4560         [ALC880_FIXUP_F1734] = {
4561                 /* almost compatible with FUJITSU, but no bass and SPDIF */
4562                 .type = ALC_FIXUP_PINS,
4563                 .v.pins = (const struct alc_pincfg[]) {
4564                         { 0x14, 0x0121411f }, /* HP */
4565                         { 0x15, 0x99030120 }, /* speaker */
4566                         { 0x16, 0x411111f0 }, /* N/A */
4567                         { 0x17, 0x411111f0 }, /* N/A */
4568                         { 0x18, 0x411111f0 }, /* N/A */
4569                         { 0x19, 0x01a19950 }, /* mic-in */
4570                         { 0x1a, 0x411111f0 }, /* N/A */
4571                         { 0x1b, 0x411111f0 }, /* N/A */
4572                         { 0x1c, 0x411111f0 }, /* N/A */
4573                         { 0x1d, 0x411111f0 }, /* N/A */
4574                         { 0x1e, 0x411111f0 }, /* N/A */
4575                         { }
4576                 },
4577                 .chained = true,
4578                 .chain_id = ALC880_FIXUP_VOL_KNOB,
4579         },
4580         [ALC880_FIXUP_UNIWILL] = {
4581                 /* need to fix HP and speaker pins to be parsed correctly */
4582                 .type = ALC_FIXUP_PINS,
4583                 .v.pins = (const struct alc_pincfg[]) {
4584                         { 0x14, 0x0121411f }, /* HP */
4585                         { 0x15, 0x99030120 }, /* speaker */
4586                         { 0x16, 0x99030130 }, /* bass speaker */
4587                         { }
4588                 },
4589         },
4590         [ALC880_FIXUP_UNIWILL_DIG] = {
4591                 .type = ALC_FIXUP_PINS,
4592                 .v.pins = (const struct alc_pincfg[]) {
4593                         /* disable bogus unused pins */
4594                         { 0x17, 0x411111f0 },
4595                         { 0x19, 0x411111f0 },
4596                         { 0x1b, 0x411111f0 },
4597                         { 0x1f, 0x411111f0 },
4598                         { }
4599                 }
4600         },
4601         [ALC880_FIXUP_Z71V] = {
4602                 .type = ALC_FIXUP_PINS,
4603                 .v.pins = (const struct alc_pincfg[]) {
4604                         /* set up the whole pins as BIOS is utterly broken */
4605                         { 0x14, 0x99030120 }, /* speaker */
4606                         { 0x15, 0x0121411f }, /* HP */
4607                         { 0x16, 0x411111f0 }, /* N/A */
4608                         { 0x17, 0x411111f0 }, /* N/A */
4609                         { 0x18, 0x01a19950 }, /* mic-in */
4610                         { 0x19, 0x411111f0 }, /* N/A */
4611                         { 0x1a, 0x01813031 }, /* line-in */
4612                         { 0x1b, 0x411111f0 }, /* N/A */
4613                         { 0x1c, 0x411111f0 }, /* N/A */
4614                         { 0x1d, 0x411111f0 }, /* N/A */
4615                         { 0x1e, 0x0144111e }, /* SPDIF */
4616                         { }
4617                 }
4618         },
4619         [ALC880_FIXUP_3ST_BASE] = {
4620                 .type = ALC_FIXUP_PINS,
4621                 .v.pins = (const struct alc_pincfg[]) {
4622                         { 0x14, 0x01014010 }, /* line-out */
4623                         { 0x15, 0x411111f0 }, /* N/A */
4624                         { 0x16, 0x411111f0 }, /* N/A */
4625                         { 0x17, 0x411111f0 }, /* N/A */
4626                         { 0x18, 0x01a19c30 }, /* mic-in */
4627                         { 0x19, 0x0121411f }, /* HP */
4628                         { 0x1a, 0x01813031 }, /* line-in */
4629                         { 0x1b, 0x02a19c40 }, /* front-mic */
4630                         { 0x1c, 0x411111f0 }, /* N/A */
4631                         { 0x1d, 0x411111f0 }, /* N/A */
4632                         /* 0x1e is filled in below */
4633                         { 0x1f, 0x411111f0 }, /* N/A */
4634                         { }
4635                 }
4636         },
4637         [ALC880_FIXUP_3ST] = {
4638                 .type = ALC_FIXUP_PINS,
4639                 .v.pins = (const struct alc_pincfg[]) {
4640                         { 0x1e, 0x411111f0 }, /* N/A */
4641                         { }
4642                 },
4643                 .chained = true,
4644                 .chain_id = ALC880_FIXUP_3ST_BASE,
4645         },
4646         [ALC880_FIXUP_3ST_DIG] = {
4647                 .type = ALC_FIXUP_PINS,
4648                 .v.pins = (const struct alc_pincfg[]) {
4649                         { 0x1e, 0x0144111e }, /* SPDIF */
4650                         { }
4651                 },
4652                 .chained = true,
4653                 .chain_id = ALC880_FIXUP_3ST_BASE,
4654         },
4655         [ALC880_FIXUP_5ST_BASE] = {
4656                 .type = ALC_FIXUP_PINS,
4657                 .v.pins = (const struct alc_pincfg[]) {
4658                         { 0x14, 0x01014010 }, /* front */
4659                         { 0x15, 0x411111f0 }, /* N/A */
4660                         { 0x16, 0x01011411 }, /* CLFE */
4661                         { 0x17, 0x01016412 }, /* surr */
4662                         { 0x18, 0x01a19c30 }, /* mic-in */
4663                         { 0x19, 0x0121411f }, /* HP */
4664                         { 0x1a, 0x01813031 }, /* line-in */
4665                         { 0x1b, 0x02a19c40 }, /* front-mic */
4666                         { 0x1c, 0x411111f0 }, /* N/A */
4667                         { 0x1d, 0x411111f0 }, /* N/A */
4668                         /* 0x1e is filled in below */
4669                         { 0x1f, 0x411111f0 }, /* N/A */
4670                         { }
4671                 }
4672         },
4673         [ALC880_FIXUP_5ST] = {
4674                 .type = ALC_FIXUP_PINS,
4675                 .v.pins = (const struct alc_pincfg[]) {
4676                         { 0x1e, 0x411111f0 }, /* N/A */
4677                         { }
4678                 },
4679                 .chained = true,
4680                 .chain_id = ALC880_FIXUP_5ST_BASE,
4681         },
4682         [ALC880_FIXUP_5ST_DIG] = {
4683                 .type = ALC_FIXUP_PINS,
4684                 .v.pins = (const struct alc_pincfg[]) {
4685                         { 0x1e, 0x0144111e }, /* SPDIF */
4686                         { }
4687                 },
4688                 .chained = true,
4689                 .chain_id = ALC880_FIXUP_5ST_BASE,
4690         },
4691         [ALC880_FIXUP_6ST_BASE] = {
4692                 .type = ALC_FIXUP_PINS,
4693                 .v.pins = (const struct alc_pincfg[]) {
4694                         { 0x14, 0x01014010 }, /* front */
4695                         { 0x15, 0x01016412 }, /* surr */
4696                         { 0x16, 0x01011411 }, /* CLFE */
4697                         { 0x17, 0x01012414 }, /* side */
4698                         { 0x18, 0x01a19c30 }, /* mic-in */
4699                         { 0x19, 0x02a19c40 }, /* front-mic */
4700                         { 0x1a, 0x01813031 }, /* line-in */
4701                         { 0x1b, 0x0121411f }, /* HP */
4702                         { 0x1c, 0x411111f0 }, /* N/A */
4703                         { 0x1d, 0x411111f0 }, /* N/A */
4704                         /* 0x1e is filled in below */
4705                         { 0x1f, 0x411111f0 }, /* N/A */
4706                         { }
4707                 }
4708         },
4709         [ALC880_FIXUP_6ST] = {
4710                 .type = ALC_FIXUP_PINS,
4711                 .v.pins = (const struct alc_pincfg[]) {
4712                         { 0x1e, 0x411111f0 }, /* N/A */
4713                         { }
4714                 },
4715                 .chained = true,
4716                 .chain_id = ALC880_FIXUP_6ST_BASE,
4717         },
4718         [ALC880_FIXUP_6ST_DIG] = {
4719                 .type = ALC_FIXUP_PINS,
4720                 .v.pins = (const struct alc_pincfg[]) {
4721                         { 0x1e, 0x0144111e }, /* SPDIF */
4722                         { }
4723                 },
4724                 .chained = true,
4725                 .chain_id = ALC880_FIXUP_6ST_BASE,
4726         },
4727 };
4728
4729 static const struct snd_pci_quirk alc880_fixup_tbl[] = {
4730         SND_PCI_QUIRK(0x1019, 0x0f69, "Coeus G610P", ALC880_FIXUP_W810),
4731         SND_PCI_QUIRK(0x1043, 0x1964, "ASUS Z71V", ALC880_FIXUP_Z71V),
4732         SND_PCI_QUIRK_VENDOR(0x1043, "ASUS", ALC880_FIXUP_GPIO1),
4733         SND_PCI_QUIRK(0x1558, 0x5401, "Clevo GPIO2", ALC880_FIXUP_GPIO2),
4734         SND_PCI_QUIRK_VENDOR(0x1558, "Clevo", ALC880_FIXUP_EAPD_COEF),
4735         SND_PCI_QUIRK(0x1584, 0x9050, "Uniwill", ALC880_FIXUP_UNIWILL_DIG),
4736         SND_PCI_QUIRK(0x1584, 0x9054, "Uniwill", ALC880_FIXUP_F1734),
4737         SND_PCI_QUIRK(0x1584, 0x9070, "Uniwill", ALC880_FIXUP_UNIWILL),
4738         SND_PCI_QUIRK(0x1584, 0x9077, "Uniwill P53", ALC880_FIXUP_VOL_KNOB),
4739         SND_PCI_QUIRK(0x161f, 0x203d, "W810", ALC880_FIXUP_W810),
4740         SND_PCI_QUIRK(0x161f, 0x205d, "Medion Rim 2150", ALC880_FIXUP_MEDION_RIM),
4741         SND_PCI_QUIRK(0x1734, 0x107c, "FSC F1734", ALC880_FIXUP_F1734),
4742         SND_PCI_QUIRK(0x1734, 0x1094, "FSC Amilo M1451G", ALC880_FIXUP_FUJITSU),
4743         SND_PCI_QUIRK(0x1734, 0x10ac, "FSC AMILO Xi 1526", ALC880_FIXUP_F1734),
4744         SND_PCI_QUIRK(0x1734, 0x10b0, "FSC Amilo Pi1556", ALC880_FIXUP_FUJITSU),
4745         SND_PCI_QUIRK(0x1854, 0x003b, "LG", ALC880_FIXUP_LG),
4746         SND_PCI_QUIRK(0x1854, 0x005f, "LG P1 Express", ALC880_FIXUP_LG),
4747         SND_PCI_QUIRK(0x1854, 0x0068, "LG w1", ALC880_FIXUP_LG),
4748         SND_PCI_QUIRK(0x19db, 0x4188, "TCL S700", ALC880_FIXUP_TCL_S700),
4749
4750         /* Below is the copied entries from alc880_quirks.c.
4751          * It's not quite sure whether BIOS sets the correct pin-config table
4752          * on these machines, thus they are kept to be compatible with
4753          * the old static quirks.  Once when it's confirmed to work without
4754          * these overrides, it'd be better to remove.
4755          */
4756         SND_PCI_QUIRK(0x1019, 0xa880, "ECS", ALC880_FIXUP_5ST_DIG),
4757         SND_PCI_QUIRK(0x1019, 0xa884, "Acer APFV", ALC880_FIXUP_6ST),
4758         SND_PCI_QUIRK(0x1025, 0x0070, "ULI", ALC880_FIXUP_3ST_DIG),
4759         SND_PCI_QUIRK(0x1025, 0x0077, "ULI", ALC880_FIXUP_6ST_DIG),
4760         SND_PCI_QUIRK(0x1025, 0x0078, "ULI", ALC880_FIXUP_6ST_DIG),
4761         SND_PCI_QUIRK(0x1025, 0x0087, "ULI", ALC880_FIXUP_6ST_DIG),
4762         SND_PCI_QUIRK(0x1025, 0xe309, "ULI", ALC880_FIXUP_3ST_DIG),
4763         SND_PCI_QUIRK(0x1025, 0xe310, "ULI", ALC880_FIXUP_3ST),
4764         SND_PCI_QUIRK(0x1039, 0x1234, NULL, ALC880_FIXUP_6ST_DIG),
4765         SND_PCI_QUIRK(0x104d, 0x81a0, "Sony", ALC880_FIXUP_3ST),
4766         SND_PCI_QUIRK(0x104d, 0x81d6, "Sony", ALC880_FIXUP_3ST),
4767         SND_PCI_QUIRK(0x107b, 0x3032, "Gateway", ALC880_FIXUP_5ST),
4768         SND_PCI_QUIRK(0x107b, 0x3033, "Gateway", ALC880_FIXUP_5ST),
4769         SND_PCI_QUIRK(0x107b, 0x4039, "Gateway", ALC880_FIXUP_5ST),
4770         SND_PCI_QUIRK(0x1297, 0xc790, "Shuttle ST20G5", ALC880_FIXUP_6ST_DIG),
4771         SND_PCI_QUIRK(0x1458, 0xa102, "Gigabyte K8", ALC880_FIXUP_6ST_DIG),
4772         SND_PCI_QUIRK(0x1462, 0x1150, "MSI", ALC880_FIXUP_6ST_DIG),
4773         SND_PCI_QUIRK(0x1509, 0x925d, "FIC P4M", ALC880_FIXUP_6ST_DIG),
4774         SND_PCI_QUIRK(0x1565, 0x8202, "Biostar", ALC880_FIXUP_5ST_DIG),
4775         SND_PCI_QUIRK(0x1695, 0x400d, "EPoX", ALC880_FIXUP_5ST_DIG),
4776         SND_PCI_QUIRK(0x1695, 0x4012, "EPox EP-5LDA", ALC880_FIXUP_5ST_DIG),
4777         SND_PCI_QUIRK(0x2668, 0x8086, NULL, ALC880_FIXUP_6ST_DIG), /* broken BIOS */
4778         SND_PCI_QUIRK(0x8086, 0x2668, NULL, ALC880_FIXUP_6ST_DIG),
4779         SND_PCI_QUIRK(0x8086, 0xa100, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4780         SND_PCI_QUIRK(0x8086, 0xd400, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4781         SND_PCI_QUIRK(0x8086, 0xd401, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4782         SND_PCI_QUIRK(0x8086, 0xd402, "Intel mobo", ALC880_FIXUP_3ST_DIG),
4783         SND_PCI_QUIRK(0x8086, 0xe224, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4784         SND_PCI_QUIRK(0x8086, 0xe305, "Intel mobo", ALC880_FIXUP_3ST_DIG),
4785         SND_PCI_QUIRK(0x8086, 0xe308, "Intel mobo", ALC880_FIXUP_3ST_DIG),
4786         SND_PCI_QUIRK(0x8086, 0xe400, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4787         SND_PCI_QUIRK(0x8086, 0xe401, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4788         SND_PCI_QUIRK(0x8086, 0xe402, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4789         /* default Intel */
4790         SND_PCI_QUIRK_VENDOR(0x8086, "Intel mobo", ALC880_FIXUP_3ST),
4791         SND_PCI_QUIRK(0xa0a0, 0x0560, "AOpen i915GMm-HFS", ALC880_FIXUP_5ST_DIG),
4792         SND_PCI_QUIRK(0xe803, 0x1019, NULL, ALC880_FIXUP_6ST_DIG),
4793         {}
4794 };
4795
4796 static const struct alc_model_fixup alc880_fixup_models[] = {
4797         {.id = ALC880_FIXUP_3ST, .name = "3stack"},
4798         {.id = ALC880_FIXUP_3ST_DIG, .name = "3stack-digout"},
4799         {.id = ALC880_FIXUP_5ST, .name = "5stack"},
4800         {.id = ALC880_FIXUP_5ST_DIG, .name = "5stack-digout"},
4801         {.id = ALC880_FIXUP_6ST, .name = "6stack"},
4802         {.id = ALC880_FIXUP_6ST_DIG, .name = "6stack-digout"},
4803         {}
4804 };
4805
4806
4807 /*
4808  * OK, here we have finally the patch for ALC880
4809  */
4810 static int patch_alc880(struct hda_codec *codec)
4811 {
4812         struct alc_spec *spec;
4813         int err;
4814
4815         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4816         if (spec == NULL)
4817                 return -ENOMEM;
4818
4819         codec->spec = spec;
4820
4821         spec->mixer_nid = 0x0b;
4822         spec->need_dac_fix = 1;
4823
4824         alc_pick_fixup(codec, alc880_fixup_models, alc880_fixup_tbl,
4825                        alc880_fixups);
4826         alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
4827
4828         /* automatic parse from the BIOS config */
4829         err = alc880_parse_auto_config(codec);
4830         if (err < 0)
4831                 goto error;
4832
4833         if (!spec->no_analog) {
4834                 err = snd_hda_attach_beep_device(codec, 0x1);
4835                 if (err < 0)
4836                         goto error;
4837                 set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
4838         }
4839
4840         codec->patch_ops = alc_patch_ops;
4841
4842         alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
4843
4844         return 0;
4845
4846  error:
4847         alc_free(codec);
4848         return err;
4849 }
4850
4851
4852 /*
4853  * ALC260 support
4854  */
4855 static int alc260_parse_auto_config(struct hda_codec *codec)
4856 {
4857         static const hda_nid_t alc260_ignore[] = { 0x17, 0 };
4858         static const hda_nid_t alc260_ssids[] = { 0x10, 0x15, 0x0f, 0 };
4859         return alc_parse_auto_config(codec, alc260_ignore, alc260_ssids);
4860 }
4861
4862 /*
4863  * Pin config fixes
4864  */
4865 enum {
4866         ALC260_FIXUP_HP_DC5750,
4867         ALC260_FIXUP_HP_PIN_0F,
4868         ALC260_FIXUP_COEF,
4869         ALC260_FIXUP_GPIO1,
4870         ALC260_FIXUP_GPIO1_TOGGLE,
4871         ALC260_FIXUP_REPLACER,
4872         ALC260_FIXUP_HP_B1900,
4873         ALC260_FIXUP_KN1,
4874 };
4875
4876 static void alc260_gpio1_automute(struct hda_codec *codec)
4877 {
4878         struct alc_spec *spec = codec->spec;
4879         snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
4880                             spec->hp_jack_present);
4881 }
4882
4883 static void alc260_fixup_gpio1_toggle(struct hda_codec *codec,
4884                                       const struct alc_fixup *fix, int action)
4885 {
4886         struct alc_spec *spec = codec->spec;
4887         if (action == ALC_FIXUP_ACT_PROBE) {
4888                 /* although the machine has only one output pin, we need to
4889                  * toggle GPIO1 according to the jack state
4890                  */
4891                 spec->automute_hook = alc260_gpio1_automute;
4892                 spec->detect_hp = 1;
4893                 spec->automute_speaker = 1;
4894                 spec->autocfg.hp_pins[0] = 0x0f; /* copy it for automute */
4895                 snd_hda_jack_detect_enable(codec, 0x0f, ALC_HP_EVENT);
4896                 spec->unsol_event = alc_sku_unsol_event;
4897                 add_verb(codec->spec, alc_gpio1_init_verbs);
4898         }
4899 }
4900
4901 static void alc260_fixup_kn1(struct hda_codec *codec,
4902                              const struct alc_fixup *fix, int action)
4903 {
4904         struct alc_spec *spec = codec->spec;
4905         static const struct alc_pincfg pincfgs[] = {
4906                 { 0x0f, 0x02214000 }, /* HP/speaker */
4907                 { 0x12, 0x90a60160 }, /* int mic */
4908                 { 0x13, 0x02a19000 }, /* ext mic */
4909                 { 0x18, 0x01446000 }, /* SPDIF out */
4910                 /* disable bogus I/O pins */
4911                 { 0x10, 0x411111f0 },
4912                 { 0x11, 0x411111f0 },
4913                 { 0x14, 0x411111f0 },
4914                 { 0x15, 0x411111f0 },
4915                 { 0x16, 0x411111f0 },
4916                 { 0x17, 0x411111f0 },
4917                 { 0x19, 0x411111f0 },
4918                 { }
4919         };
4920
4921         switch (action) {
4922         case ALC_FIXUP_ACT_PRE_PROBE:
4923                 alc_apply_pincfgs(codec, pincfgs);
4924                 break;
4925         case ALC_FIXUP_ACT_PROBE:
4926                 spec->init_amp = ALC_INIT_NONE;
4927                 break;
4928         }
4929 }
4930
4931 static const struct alc_fixup alc260_fixups[] = {
4932         [ALC260_FIXUP_HP_DC5750] = {
4933                 .type = ALC_FIXUP_PINS,
4934                 .v.pins = (const struct alc_pincfg[]) {
4935                         { 0x11, 0x90130110 }, /* speaker */
4936                         { }
4937                 }
4938         },
4939         [ALC260_FIXUP_HP_PIN_0F] = {
4940                 .type = ALC_FIXUP_PINS,
4941                 .v.pins = (const struct alc_pincfg[]) {
4942                         { 0x0f, 0x01214000 }, /* HP */
4943                         { }
4944                 }
4945         },
4946         [ALC260_FIXUP_COEF] = {
4947                 .type = ALC_FIXUP_VERBS,
4948                 .v.verbs = (const struct hda_verb[]) {
4949                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4950                         { 0x20, AC_VERB_SET_PROC_COEF,  0x3040 },
4951                         { }
4952                 },
4953                 .chained = true,
4954                 .chain_id = ALC260_FIXUP_HP_PIN_0F,
4955         },
4956         [ALC260_FIXUP_GPIO1] = {
4957                 .type = ALC_FIXUP_VERBS,
4958                 .v.verbs = alc_gpio1_init_verbs,
4959         },
4960         [ALC260_FIXUP_GPIO1_TOGGLE] = {
4961                 .type = ALC_FIXUP_FUNC,
4962                 .v.func = alc260_fixup_gpio1_toggle,
4963                 .chained = true,
4964                 .chain_id = ALC260_FIXUP_HP_PIN_0F,
4965         },
4966         [ALC260_FIXUP_REPLACER] = {
4967                 .type = ALC_FIXUP_VERBS,
4968                 .v.verbs = (const struct hda_verb[]) {
4969                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4970                         { 0x20, AC_VERB_SET_PROC_COEF,  0x3050 },
4971                         { }
4972                 },
4973                 .chained = true,
4974                 .chain_id = ALC260_FIXUP_GPIO1_TOGGLE,
4975         },
4976         [ALC260_FIXUP_HP_B1900] = {
4977                 .type = ALC_FIXUP_FUNC,
4978                 .v.func = alc260_fixup_gpio1_toggle,
4979                 .chained = true,
4980                 .chain_id = ALC260_FIXUP_COEF,
4981         },
4982         [ALC260_FIXUP_KN1] = {
4983                 .type = ALC_FIXUP_FUNC,
4984                 .v.func = alc260_fixup_kn1,
4985         },
4986 };
4987
4988 static const struct snd_pci_quirk alc260_fixup_tbl[] = {
4989         SND_PCI_QUIRK(0x1025, 0x007b, "Acer C20x", ALC260_FIXUP_GPIO1),
4990         SND_PCI_QUIRK(0x1025, 0x007f, "Acer Aspire 9500", ALC260_FIXUP_COEF),
4991         SND_PCI_QUIRK(0x1025, 0x008f, "Acer", ALC260_FIXUP_GPIO1),
4992         SND_PCI_QUIRK(0x103c, 0x280a, "HP dc5750", ALC260_FIXUP_HP_DC5750),
4993         SND_PCI_QUIRK(0x103c, 0x30ba, "HP Presario B1900", ALC260_FIXUP_HP_B1900),
4994         SND_PCI_QUIRK(0x1509, 0x4540, "Favorit 100XS", ALC260_FIXUP_GPIO1),
4995         SND_PCI_QUIRK(0x152d, 0x0729, "Quanta KN1", ALC260_FIXUP_KN1),
4996         SND_PCI_QUIRK(0x161f, 0x2057, "Replacer 672V", ALC260_FIXUP_REPLACER),
4997         SND_PCI_QUIRK(0x1631, 0xc017, "PB V7900", ALC260_FIXUP_COEF),
4998         {}
4999 };
5000
5001 /*
5002  */
5003 static int patch_alc260(struct hda_codec *codec)
5004 {
5005         struct alc_spec *spec;
5006         int err;
5007
5008         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5009         if (spec == NULL)
5010                 return -ENOMEM;
5011
5012         codec->spec = spec;
5013
5014         spec->mixer_nid = 0x07;
5015
5016         alc_pick_fixup(codec, NULL, alc260_fixup_tbl, alc260_fixups);
5017         alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
5018
5019         /* automatic parse from the BIOS config */
5020         err = alc260_parse_auto_config(codec);
5021         if (err < 0)
5022                 goto error;
5023
5024         if (!spec->no_analog) {
5025                 err = snd_hda_attach_beep_device(codec, 0x1);
5026                 if (err < 0)
5027                         goto error;
5028                 set_beep_amp(spec, 0x07, 0x05, HDA_INPUT);
5029         }
5030
5031         codec->patch_ops = alc_patch_ops;
5032         spec->shutup = alc_eapd_shutup;
5033
5034         alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
5035
5036         return 0;
5037
5038  error:
5039         alc_free(codec);
5040         return err;
5041 }
5042
5043
5044 /*
5045  * ALC882/883/885/888/889 support
5046  *
5047  * ALC882 is almost identical with ALC880 but has cleaner and more flexible
5048  * configuration.  Each pin widget can choose any input DACs and a mixer.
5049  * Each ADC is connected from a mixer of all inputs.  This makes possible
5050  * 6-channel independent captures.
5051  *
5052  * In addition, an independent DAC for the multi-playback (not used in this
5053  * driver yet).
5054  */
5055
5056 /*
5057  * Pin config fixes
5058  */
5059 enum {
5060         ALC882_FIXUP_ABIT_AW9D_MAX,
5061         ALC882_FIXUP_LENOVO_Y530,
5062         ALC882_FIXUP_PB_M5210,
5063         ALC882_FIXUP_ACER_ASPIRE_7736,
5064         ALC882_FIXUP_ASUS_W90V,
5065         ALC889_FIXUP_CD,
5066         ALC889_FIXUP_VAIO_TT,
5067         ALC888_FIXUP_EEE1601,
5068         ALC882_FIXUP_EAPD,
5069         ALC883_FIXUP_EAPD,
5070         ALC883_FIXUP_ACER_EAPD,
5071         ALC882_FIXUP_GPIO1,
5072         ALC882_FIXUP_GPIO2,
5073         ALC882_FIXUP_GPIO3,
5074         ALC889_FIXUP_COEF,
5075         ALC882_FIXUP_ASUS_W2JC,
5076         ALC882_FIXUP_ACER_ASPIRE_4930G,
5077         ALC882_FIXUP_ACER_ASPIRE_8930G,
5078         ALC882_FIXUP_ASPIRE_8930G_VERBS,
5079         ALC885_FIXUP_MACPRO_GPIO,
5080         ALC889_FIXUP_DAC_ROUTE,
5081         ALC889_FIXUP_MBP_VREF,
5082         ALC889_FIXUP_IMAC91_VREF,
5083         ALC882_FIXUP_NO_PRIMARY_HP,
5084 };
5085
5086 static void alc889_fixup_coef(struct hda_codec *codec,
5087                               const struct alc_fixup *fix, int action)
5088 {
5089         if (action != ALC_FIXUP_ACT_INIT)
5090                 return;
5091         alc889_coef_init(codec);
5092 }
5093
5094 /* toggle speaker-output according to the hp-jack state */
5095 static void alc882_gpio_mute(struct hda_codec *codec, int pin, int muted)
5096 {
5097         unsigned int gpiostate, gpiomask, gpiodir;
5098
5099         gpiostate = snd_hda_codec_read(codec, codec->afg, 0,
5100                                        AC_VERB_GET_GPIO_DATA, 0);
5101
5102         if (!muted)
5103                 gpiostate |= (1 << pin);
5104         else
5105                 gpiostate &= ~(1 << pin);
5106
5107         gpiomask = snd_hda_codec_read(codec, codec->afg, 0,
5108                                       AC_VERB_GET_GPIO_MASK, 0);
5109         gpiomask |= (1 << pin);
5110
5111         gpiodir = snd_hda_codec_read(codec, codec->afg, 0,
5112                                      AC_VERB_GET_GPIO_DIRECTION, 0);
5113         gpiodir |= (1 << pin);
5114
5115
5116         snd_hda_codec_write(codec, codec->afg, 0,
5117                             AC_VERB_SET_GPIO_MASK, gpiomask);
5118         snd_hda_codec_write(codec, codec->afg, 0,
5119                             AC_VERB_SET_GPIO_DIRECTION, gpiodir);
5120
5121         msleep(1);
5122
5123         snd_hda_codec_write(codec, codec->afg, 0,
5124                             AC_VERB_SET_GPIO_DATA, gpiostate);
5125 }
5126
5127 /* set up GPIO at initialization */
5128 static void alc885_fixup_macpro_gpio(struct hda_codec *codec,
5129                                      const struct alc_fixup *fix, int action)
5130 {
5131         if (action != ALC_FIXUP_ACT_INIT)
5132                 return;
5133         alc882_gpio_mute(codec, 0, 0);
5134         alc882_gpio_mute(codec, 1, 0);
5135 }
5136
5137 /* Fix the connection of some pins for ALC889:
5138  * At least, Acer Aspire 5935 shows the connections to DAC3/4 don't
5139  * work correctly (bko#42740)
5140  */
5141 static void alc889_fixup_dac_route(struct hda_codec *codec,
5142                                    const struct alc_fixup *fix, int action)
5143 {
5144         if (action == ALC_FIXUP_ACT_PRE_PROBE) {
5145                 /* fake the connections during parsing the tree */
5146                 hda_nid_t conn1[2] = { 0x0c, 0x0d };
5147                 hda_nid_t conn2[2] = { 0x0e, 0x0f };
5148                 snd_hda_override_conn_list(codec, 0x14, 2, conn1);
5149                 snd_hda_override_conn_list(codec, 0x15, 2, conn1);
5150                 snd_hda_override_conn_list(codec, 0x18, 2, conn2);
5151                 snd_hda_override_conn_list(codec, 0x1a, 2, conn2);
5152         } else if (action == ALC_FIXUP_ACT_PROBE) {
5153                 /* restore the connections */
5154                 hda_nid_t conn[5] = { 0x0c, 0x0d, 0x0e, 0x0f, 0x26 };
5155                 snd_hda_override_conn_list(codec, 0x14, 5, conn);
5156                 snd_hda_override_conn_list(codec, 0x15, 5, conn);
5157                 snd_hda_override_conn_list(codec, 0x18, 5, conn);
5158                 snd_hda_override_conn_list(codec, 0x1a, 5, conn);
5159         }
5160 }
5161
5162 /* Set VREF on HP pin */
5163 static void alc889_fixup_mbp_vref(struct hda_codec *codec,
5164                                   const struct alc_fixup *fix, int action)
5165 {
5166         struct alc_spec *spec = codec->spec;
5167         static hda_nid_t nids[2] = { 0x14, 0x15 };
5168         int i;
5169
5170         if (action != ALC_FIXUP_ACT_INIT)
5171                 return;
5172         for (i = 0; i < ARRAY_SIZE(nids); i++) {
5173                 unsigned int val = snd_hda_codec_get_pincfg(codec, nids[i]);
5174                 if (get_defcfg_device(val) != AC_JACK_HP_OUT)
5175                         continue;
5176                 val = snd_hda_codec_read(codec, nids[i], 0,
5177                                          AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5178                 val |= AC_PINCTL_VREF_80;
5179                 snd_hda_codec_write(codec, nids[i], 0,
5180                                     AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5181                 spec->keep_vref_in_automute = 1;
5182                 break;
5183         }
5184 }
5185
5186 /* Set VREF on speaker pins on imac91 */
5187 static void alc889_fixup_imac91_vref(struct hda_codec *codec,
5188                                      const struct alc_fixup *fix, int action)
5189 {
5190         struct alc_spec *spec = codec->spec;
5191         static hda_nid_t nids[2] = { 0x18, 0x1a };
5192         int i;
5193
5194         if (action != ALC_FIXUP_ACT_INIT)
5195                 return;
5196         for (i = 0; i < ARRAY_SIZE(nids); i++) {
5197                 unsigned int val;
5198                 val = snd_hda_codec_read(codec, nids[i], 0,
5199                                          AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5200                 val |= AC_PINCTL_VREF_50;
5201                 snd_hda_codec_write(codec, nids[i], 0,
5202                                     AC_VERB_SET_PIN_WIDGET_CONTROL, val);
5203         }
5204         spec->keep_vref_in_automute = 1;
5205 }
5206
5207 /* Don't take HP output as primary
5208  * strangely, the speaker output doesn't work on VAIO Z through DAC 0x05
5209  */
5210 static void alc882_fixup_no_primary_hp(struct hda_codec *codec,
5211                                        const struct alc_fixup *fix, int action)
5212 {
5213         struct alc_spec *spec = codec->spec;
5214         if (action == ALC_FIXUP_ACT_PRE_PROBE)
5215                 spec->no_primary_hp = 1;
5216 }
5217
5218 static const struct alc_fixup alc882_fixups[] = {
5219         [ALC882_FIXUP_ABIT_AW9D_MAX] = {
5220                 .type = ALC_FIXUP_PINS,
5221                 .v.pins = (const struct alc_pincfg[]) {
5222                         { 0x15, 0x01080104 }, /* side */
5223                         { 0x16, 0x01011012 }, /* rear */
5224                         { 0x17, 0x01016011 }, /* clfe */
5225                         { }
5226                 }
5227         },
5228         [ALC882_FIXUP_LENOVO_Y530] = {
5229                 .type = ALC_FIXUP_PINS,
5230                 .v.pins = (const struct alc_pincfg[]) {
5231                         { 0x15, 0x99130112 }, /* rear int speakers */
5232                         { 0x16, 0x99130111 }, /* subwoofer */
5233                         { }
5234                 }
5235         },
5236         [ALC882_FIXUP_PB_M5210] = {
5237                 .type = ALC_FIXUP_VERBS,
5238                 .v.verbs = (const struct hda_verb[]) {
5239                         { 0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF50 },
5240                         {}
5241                 }
5242         },
5243         [ALC882_FIXUP_ACER_ASPIRE_7736] = {
5244                 .type = ALC_FIXUP_SKU,
5245                 .v.sku = ALC_FIXUP_SKU_IGNORE,
5246         },
5247         [ALC882_FIXUP_ASUS_W90V] = {
5248                 .type = ALC_FIXUP_PINS,
5249                 .v.pins = (const struct alc_pincfg[]) {
5250                         { 0x16, 0x99130110 }, /* fix sequence for CLFE */
5251                         { }
5252                 }
5253         },
5254         [ALC889_FIXUP_CD] = {
5255                 .type = ALC_FIXUP_PINS,
5256                 .v.pins = (const struct alc_pincfg[]) {
5257                         { 0x1c, 0x993301f0 }, /* CD */
5258                         { }
5259                 }
5260         },
5261         [ALC889_FIXUP_VAIO_TT] = {
5262                 .type = ALC_FIXUP_PINS,
5263                 .v.pins = (const struct alc_pincfg[]) {
5264                         { 0x17, 0x90170111 }, /* hidden surround speaker */
5265                         { }
5266                 }
5267         },
5268         [ALC888_FIXUP_EEE1601] = {
5269                 .type = ALC_FIXUP_VERBS,
5270                 .v.verbs = (const struct hda_verb[]) {
5271                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x0b },
5272                         { 0x20, AC_VERB_SET_PROC_COEF,  0x0838 },
5273                         { }
5274                 }
5275         },
5276         [ALC882_FIXUP_EAPD] = {
5277                 .type = ALC_FIXUP_VERBS,
5278                 .v.verbs = (const struct hda_verb[]) {
5279                         /* change to EAPD mode */
5280                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5281                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3060 },
5282                         { }
5283                 }
5284         },
5285         [ALC883_FIXUP_EAPD] = {
5286                 .type = ALC_FIXUP_VERBS,
5287                 .v.verbs = (const struct hda_verb[]) {
5288                         /* change to EAPD mode */
5289                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5290                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3070 },
5291                         { }
5292                 }
5293         },
5294         [ALC883_FIXUP_ACER_EAPD] = {
5295                 .type = ALC_FIXUP_VERBS,
5296                 .v.verbs = (const struct hda_verb[]) {
5297                         /* eanable EAPD on Acer laptops */
5298                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5299                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3050 },
5300                         { }
5301                 }
5302         },
5303         [ALC882_FIXUP_GPIO1] = {
5304                 .type = ALC_FIXUP_VERBS,
5305                 .v.verbs = alc_gpio1_init_verbs,
5306         },
5307         [ALC882_FIXUP_GPIO2] = {
5308                 .type = ALC_FIXUP_VERBS,
5309                 .v.verbs = alc_gpio2_init_verbs,
5310         },
5311         [ALC882_FIXUP_GPIO3] = {
5312                 .type = ALC_FIXUP_VERBS,
5313                 .v.verbs = alc_gpio3_init_verbs,
5314         },
5315         [ALC882_FIXUP_ASUS_W2JC] = {
5316                 .type = ALC_FIXUP_VERBS,
5317                 .v.verbs = alc_gpio1_init_verbs,
5318                 .chained = true,
5319                 .chain_id = ALC882_FIXUP_EAPD,
5320         },
5321         [ALC889_FIXUP_COEF] = {
5322                 .type = ALC_FIXUP_FUNC,
5323                 .v.func = alc889_fixup_coef,
5324         },
5325         [ALC882_FIXUP_ACER_ASPIRE_4930G] = {
5326                 .type = ALC_FIXUP_PINS,
5327                 .v.pins = (const struct alc_pincfg[]) {
5328                         { 0x16, 0x99130111 }, /* CLFE speaker */
5329                         { 0x17, 0x99130112 }, /* surround speaker */
5330                         { }
5331                 },
5332                 .chained = true,
5333                 .chain_id = ALC882_FIXUP_GPIO1,
5334         },
5335         [ALC882_FIXUP_ACER_ASPIRE_8930G] = {
5336                 .type = ALC_FIXUP_PINS,
5337                 .v.pins = (const struct alc_pincfg[]) {
5338                         { 0x16, 0x99130111 }, /* CLFE speaker */
5339                         { 0x1b, 0x99130112 }, /* surround speaker */
5340                         { }
5341                 },
5342                 .chained = true,
5343                 .chain_id = ALC882_FIXUP_ASPIRE_8930G_VERBS,
5344         },
5345         [ALC882_FIXUP_ASPIRE_8930G_VERBS] = {
5346                 /* additional init verbs for Acer Aspire 8930G */
5347                 .type = ALC_FIXUP_VERBS,
5348                 .v.verbs = (const struct hda_verb[]) {
5349                         /* Enable all DACs */
5350                         /* DAC DISABLE/MUTE 1? */
5351                         /*  setting bits 1-5 disables DAC nids 0x02-0x06
5352                          *  apparently. Init=0x38 */
5353                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x03 },
5354                         { 0x20, AC_VERB_SET_PROC_COEF, 0x0000 },
5355                         /* DAC DISABLE/MUTE 2? */
5356                         /*  some bit here disables the other DACs.
5357                          *  Init=0x4900 */
5358                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x08 },
5359                         { 0x20, AC_VERB_SET_PROC_COEF, 0x0000 },
5360                         /* DMIC fix
5361                          * This laptop has a stereo digital microphone.
5362                          * The mics are only 1cm apart which makes the stereo
5363                          * useless. However, either the mic or the ALC889
5364                          * makes the signal become a difference/sum signal
5365                          * instead of standard stereo, which is annoying.
5366                          * So instead we flip this bit which makes the
5367                          * codec replicate the sum signal to both channels,
5368                          * turning it into a normal mono mic.
5369                          */
5370                         /* DMIC_CONTROL? Init value = 0x0001 */
5371                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x0b },
5372                         { 0x20, AC_VERB_SET_PROC_COEF, 0x0003 },
5373                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5374                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3050 },
5375                         { }
5376                 },
5377                 .chained = true,
5378                 .chain_id = ALC882_FIXUP_GPIO1,
5379         },
5380         [ALC885_FIXUP_MACPRO_GPIO] = {
5381                 .type = ALC_FIXUP_FUNC,
5382                 .v.func = alc885_fixup_macpro_gpio,
5383         },
5384         [ALC889_FIXUP_DAC_ROUTE] = {
5385                 .type = ALC_FIXUP_FUNC,
5386                 .v.func = alc889_fixup_dac_route,
5387         },
5388         [ALC889_FIXUP_MBP_VREF] = {
5389                 .type = ALC_FIXUP_FUNC,
5390                 .v.func = alc889_fixup_mbp_vref,
5391                 .chained = true,
5392                 .chain_id = ALC882_FIXUP_GPIO1,
5393         },
5394         [ALC889_FIXUP_IMAC91_VREF] = {
5395                 .type = ALC_FIXUP_FUNC,
5396                 .v.func = alc889_fixup_imac91_vref,
5397                 .chained = true,
5398                 .chain_id = ALC882_FIXUP_GPIO1,
5399         },
5400         [ALC882_FIXUP_NO_PRIMARY_HP] = {
5401                 .type = ALC_FIXUP_FUNC,
5402                 .v.func = alc882_fixup_no_primary_hp,
5403         },
5404 };
5405
5406 static const struct snd_pci_quirk alc882_fixup_tbl[] = {
5407         SND_PCI_QUIRK(0x1025, 0x006c, "Acer Aspire 9810", ALC883_FIXUP_ACER_EAPD),
5408         SND_PCI_QUIRK(0x1025, 0x0090, "Acer Aspire", ALC883_FIXUP_ACER_EAPD),
5409         SND_PCI_QUIRK(0x1025, 0x010a, "Acer Ferrari 5000", ALC883_FIXUP_ACER_EAPD),
5410         SND_PCI_QUIRK(0x1025, 0x0110, "Acer Aspire", ALC883_FIXUP_ACER_EAPD),
5411         SND_PCI_QUIRK(0x1025, 0x0112, "Acer Aspire 9303", ALC883_FIXUP_ACER_EAPD),
5412         SND_PCI_QUIRK(0x1025, 0x0121, "Acer Aspire 5920G", ALC883_FIXUP_ACER_EAPD),
5413         SND_PCI_QUIRK(0x1025, 0x013e, "Acer Aspire 4930G",
5414                       ALC882_FIXUP_ACER_ASPIRE_4930G),
5415         SND_PCI_QUIRK(0x1025, 0x013f, "Acer Aspire 5930G",
5416                       ALC882_FIXUP_ACER_ASPIRE_4930G),
5417         SND_PCI_QUIRK(0x1025, 0x0145, "Acer Aspire 8930G",
5418                       ALC882_FIXUP_ACER_ASPIRE_8930G),
5419         SND_PCI_QUIRK(0x1025, 0x0146, "Acer Aspire 6935G",
5420                       ALC882_FIXUP_ACER_ASPIRE_8930G),
5421         SND_PCI_QUIRK(0x1025, 0x015e, "Acer Aspire 6930G",
5422                       ALC882_FIXUP_ACER_ASPIRE_4930G),
5423         SND_PCI_QUIRK(0x1025, 0x0166, "Acer Aspire 6530G",
5424                       ALC882_FIXUP_ACER_ASPIRE_4930G),
5425         SND_PCI_QUIRK(0x1025, 0x0142, "Acer Aspire 7730G",
5426                       ALC882_FIXUP_ACER_ASPIRE_4930G),
5427         SND_PCI_QUIRK(0x1025, 0x0155, "Packard-Bell M5120", ALC882_FIXUP_PB_M5210),
5428         SND_PCI_QUIRK(0x1025, 0x021e, "Acer Aspire 5739G",
5429                       ALC882_FIXUP_ACER_ASPIRE_4930G),
5430         SND_PCI_QUIRK(0x1025, 0x0259, "Acer Aspire 5935", ALC889_FIXUP_DAC_ROUTE),
5431         SND_PCI_QUIRK(0x1025, 0x026b, "Acer Aspire 8940G", ALC882_FIXUP_ACER_ASPIRE_8930G),
5432         SND_PCI_QUIRK(0x1025, 0x0296, "Acer Aspire 7736z", ALC882_FIXUP_ACER_ASPIRE_7736),
5433         SND_PCI_QUIRK(0x1043, 0x13c2, "Asus A7M", ALC882_FIXUP_EAPD),
5434         SND_PCI_QUIRK(0x1043, 0x1873, "ASUS W90V", ALC882_FIXUP_ASUS_W90V),
5435         SND_PCI_QUIRK(0x1043, 0x1971, "Asus W2JC", ALC882_FIXUP_ASUS_W2JC),
5436         SND_PCI_QUIRK(0x1043, 0x835f, "Asus Eee 1601", ALC888_FIXUP_EEE1601),
5437         SND_PCI_QUIRK(0x104d, 0x9047, "Sony Vaio TT", ALC889_FIXUP_VAIO_TT),
5438         SND_PCI_QUIRK(0x104d, 0x905a, "Sony Vaio Z", ALC882_FIXUP_NO_PRIMARY_HP),
5439
5440         /* All Apple entries are in codec SSIDs */
5441         SND_PCI_QUIRK(0x106b, 0x00a0, "MacBookPro 3,1", ALC889_FIXUP_MBP_VREF),
5442         SND_PCI_QUIRK(0x106b, 0x00a1, "Macbook", ALC889_FIXUP_MBP_VREF),
5443         SND_PCI_QUIRK(0x106b, 0x00a4, "MacbookPro 4,1", ALC889_FIXUP_MBP_VREF),
5444         SND_PCI_QUIRK(0x106b, 0x0c00, "Mac Pro", ALC885_FIXUP_MACPRO_GPIO),
5445         SND_PCI_QUIRK(0x106b, 0x1000, "iMac 24", ALC885_FIXUP_MACPRO_GPIO),
5446         SND_PCI_QUIRK(0x106b, 0x2800, "AppleTV", ALC885_FIXUP_MACPRO_GPIO),
5447         SND_PCI_QUIRK(0x106b, 0x2c00, "MacbookPro rev3", ALC889_FIXUP_MBP_VREF),
5448         SND_PCI_QUIRK(0x106b, 0x3000, "iMac", ALC889_FIXUP_MBP_VREF),
5449         SND_PCI_QUIRK(0x106b, 0x3200, "iMac 7,1 Aluminum", ALC882_FIXUP_EAPD),
5450         SND_PCI_QUIRK(0x106b, 0x3400, "MacBookAir 1,1", ALC889_FIXUP_MBP_VREF),
5451         SND_PCI_QUIRK(0x106b, 0x3500, "MacBookAir 2,1", ALC889_FIXUP_MBP_VREF),
5452         SND_PCI_QUIRK(0x106b, 0x3600, "Macbook 3,1", ALC889_FIXUP_MBP_VREF),
5453         SND_PCI_QUIRK(0x106b, 0x3800, "MacbookPro 4,1", ALC889_FIXUP_MBP_VREF),
5454         SND_PCI_QUIRK(0x106b, 0x3e00, "iMac 24 Aluminum", ALC885_FIXUP_MACPRO_GPIO),
5455         SND_PCI_QUIRK(0x106b, 0x3f00, "Macbook 5,1", ALC889_FIXUP_IMAC91_VREF),
5456         SND_PCI_QUIRK(0x106b, 0x4000, "MacbookPro 5,1", ALC889_FIXUP_IMAC91_VREF),
5457         SND_PCI_QUIRK(0x106b, 0x4100, "Macmini 3,1", ALC889_FIXUP_IMAC91_VREF),
5458         SND_PCI_QUIRK(0x106b, 0x4200, "Mac Pro 5,1", ALC885_FIXUP_MACPRO_GPIO),
5459         SND_PCI_QUIRK(0x106b, 0x4600, "MacbookPro 5,2", ALC889_FIXUP_IMAC91_VREF),
5460         SND_PCI_QUIRK(0x106b, 0x4900, "iMac 9,1 Aluminum", ALC889_FIXUP_IMAC91_VREF),
5461         SND_PCI_QUIRK(0x106b, 0x4a00, "Macbook 5,2", ALC889_FIXUP_IMAC91_VREF),
5462
5463         SND_PCI_QUIRK(0x1071, 0x8258, "Evesham Voyaeger", ALC882_FIXUP_EAPD),
5464         SND_PCI_QUIRK(0x1462, 0x7350, "MSI-7350", ALC889_FIXUP_CD),
5465         SND_PCI_QUIRK_VENDOR(0x1462, "MSI", ALC882_FIXUP_GPIO3),
5466         SND_PCI_QUIRK(0x1458, 0xa002, "Gigabyte EP45-DS3", ALC889_FIXUP_CD),
5467         SND_PCI_QUIRK(0x147b, 0x107a, "Abit AW9D-MAX", ALC882_FIXUP_ABIT_AW9D_MAX),
5468         SND_PCI_QUIRK_VENDOR(0x1558, "Clevo laptop", ALC882_FIXUP_EAPD),
5469         SND_PCI_QUIRK(0x161f, 0x2054, "Medion laptop", ALC883_FIXUP_EAPD),
5470         SND_PCI_QUIRK(0x17aa, 0x3a0d, "Lenovo Y530", ALC882_FIXUP_LENOVO_Y530),
5471         SND_PCI_QUIRK(0x8086, 0x0022, "DX58SO", ALC889_FIXUP_COEF),
5472         {}
5473 };
5474
5475 static const struct alc_model_fixup alc882_fixup_models[] = {
5476         {.id = ALC882_FIXUP_ACER_ASPIRE_4930G, .name = "acer-aspire-4930g"},
5477         {.id = ALC882_FIXUP_ACER_ASPIRE_8930G, .name = "acer-aspire-8930g"},
5478         {.id = ALC883_FIXUP_ACER_EAPD, .name = "acer-aspire"},
5479         {.id = ALC882_FIXUP_NO_PRIMARY_HP, .name = "no-primary-hp"},
5480         {}
5481 };
5482
5483 /*
5484  * BIOS auto configuration
5485  */
5486 /* almost identical with ALC880 parser... */
5487 static int alc882_parse_auto_config(struct hda_codec *codec)
5488 {
5489         static const hda_nid_t alc882_ignore[] = { 0x1d, 0 };
5490         static const hda_nid_t alc882_ssids[] = { 0x15, 0x1b, 0x14, 0 };
5491         return alc_parse_auto_config(codec, alc882_ignore, alc882_ssids);
5492 }
5493
5494 /*
5495  */
5496 static int patch_alc882(struct hda_codec *codec)
5497 {
5498         struct alc_spec *spec;
5499         int err;
5500
5501         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5502         if (spec == NULL)
5503                 return -ENOMEM;
5504
5505         codec->spec = spec;
5506
5507         spec->mixer_nid = 0x0b;
5508
5509         switch (codec->vendor_id) {
5510         case 0x10ec0882:
5511         case 0x10ec0885:
5512                 break;
5513         default:
5514                 /* ALC883 and variants */
5515                 alc_fix_pll_init(codec, 0x20, 0x0a, 10);
5516                 break;
5517         }
5518
5519         err = alc_codec_rename_from_preset(codec);
5520         if (err < 0)
5521                 goto error;
5522
5523         alc_pick_fixup(codec, alc882_fixup_models, alc882_fixup_tbl,
5524                        alc882_fixups);
5525         alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
5526
5527         alc_auto_parse_customize_define(codec);
5528
5529         /* automatic parse from the BIOS config */
5530         err = alc882_parse_auto_config(codec);
5531         if (err < 0)
5532                 goto error;
5533
5534         if (!spec->no_analog && has_cdefine_beep(codec)) {
5535                 err = snd_hda_attach_beep_device(codec, 0x1);
5536                 if (err < 0)
5537                         goto error;
5538                 set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
5539         }
5540
5541         codec->patch_ops = alc_patch_ops;
5542
5543         alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
5544
5545         return 0;
5546
5547  error:
5548         alc_free(codec);
5549         return err;
5550 }
5551
5552
5553 /*
5554  * ALC262 support
5555  */
5556 static int alc262_parse_auto_config(struct hda_codec *codec)
5557 {
5558         static const hda_nid_t alc262_ignore[] = { 0x1d, 0 };
5559         static const hda_nid_t alc262_ssids[] = { 0x15, 0x1b, 0x14, 0 };
5560         return alc_parse_auto_config(codec, alc262_ignore, alc262_ssids);
5561 }
5562
5563 /*
5564  * Pin config fixes
5565  */
5566 enum {
5567         ALC262_FIXUP_FSC_H270,
5568         ALC262_FIXUP_HP_Z200,
5569         ALC262_FIXUP_TYAN,
5570         ALC262_FIXUP_LENOVO_3000,
5571         ALC262_FIXUP_BENQ,
5572         ALC262_FIXUP_BENQ_T31,
5573 };
5574
5575 static const struct alc_fixup alc262_fixups[] = {
5576         [ALC262_FIXUP_FSC_H270] = {
5577                 .type = ALC_FIXUP_PINS,
5578                 .v.pins = (const struct alc_pincfg[]) {
5579                         { 0x14, 0x99130110 }, /* speaker */
5580                         { 0x15, 0x0221142f }, /* front HP */
5581                         { 0x1b, 0x0121141f }, /* rear HP */
5582                         { }
5583                 }
5584         },
5585         [ALC262_FIXUP_HP_Z200] = {
5586                 .type = ALC_FIXUP_PINS,
5587                 .v.pins = (const struct alc_pincfg[]) {
5588                         { 0x16, 0x99130120 }, /* internal speaker */
5589                         { }
5590                 }
5591         },
5592         [ALC262_FIXUP_TYAN] = {
5593                 .type = ALC_FIXUP_PINS,
5594                 .v.pins = (const struct alc_pincfg[]) {
5595                         { 0x14, 0x1993e1f0 }, /* int AUX */
5596                         { }
5597                 }
5598         },
5599         [ALC262_FIXUP_LENOVO_3000] = {
5600                 .type = ALC_FIXUP_VERBS,
5601                 .v.verbs = (const struct hda_verb[]) {
5602                         { 0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF50 },
5603                         {}
5604                 },
5605                 .chained = true,
5606                 .chain_id = ALC262_FIXUP_BENQ,
5607         },
5608         [ALC262_FIXUP_BENQ] = {
5609                 .type = ALC_FIXUP_VERBS,
5610                 .v.verbs = (const struct hda_verb[]) {
5611                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5612                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3070 },
5613                         {}
5614                 }
5615         },
5616         [ALC262_FIXUP_BENQ_T31] = {
5617                 .type = ALC_FIXUP_VERBS,
5618                 .v.verbs = (const struct hda_verb[]) {
5619                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5620                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3050 },
5621                         {}
5622                 }
5623         },
5624 };
5625
5626 static const struct snd_pci_quirk alc262_fixup_tbl[] = {
5627         SND_PCI_QUIRK(0x103c, 0x170b, "HP Z200", ALC262_FIXUP_HP_Z200),
5628         SND_PCI_QUIRK(0x10cf, 0x1397, "Fujitsu", ALC262_FIXUP_BENQ),
5629         SND_PCI_QUIRK(0x10cf, 0x142d, "Fujitsu Lifebook E8410", ALC262_FIXUP_BENQ),
5630         SND_PCI_QUIRK(0x10f1, 0x2915, "Tyan Thunder n6650W", ALC262_FIXUP_TYAN),
5631         SND_PCI_QUIRK(0x1734, 0x1147, "FSC Celsius H270", ALC262_FIXUP_FSC_H270),
5632         SND_PCI_QUIRK(0x17aa, 0x384e, "Lenovo 3000", ALC262_FIXUP_LENOVO_3000),
5633         SND_PCI_QUIRK(0x17ff, 0x0560, "Benq ED8", ALC262_FIXUP_BENQ),
5634         SND_PCI_QUIRK(0x17ff, 0x058d, "Benq T31-16", ALC262_FIXUP_BENQ_T31),
5635         {}
5636 };
5637
5638
5639 /*
5640  */
5641 static int patch_alc262(struct hda_codec *codec)
5642 {
5643         struct alc_spec *spec;
5644         int err;
5645
5646         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5647         if (spec == NULL)
5648                 return -ENOMEM;
5649
5650         codec->spec = spec;
5651
5652         spec->mixer_nid = 0x0b;
5653
5654 #if 0
5655         /* pshou 07/11/05  set a zero PCM sample to DAC when FIFO is
5656          * under-run
5657          */
5658         {
5659         int tmp;
5660         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_COEF_INDEX, 7);
5661         tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
5662         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_COEF_INDEX, 7);
5663         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PROC_COEF, tmp | 0x80);
5664         }
5665 #endif
5666         alc_fix_pll_init(codec, 0x20, 0x0a, 10);
5667
5668         alc_pick_fixup(codec, NULL, alc262_fixup_tbl, alc262_fixups);
5669         alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
5670
5671         alc_auto_parse_customize_define(codec);
5672
5673         /* automatic parse from the BIOS config */
5674         err = alc262_parse_auto_config(codec);
5675         if (err < 0)
5676                 goto error;
5677
5678         if (!spec->no_analog && has_cdefine_beep(codec)) {
5679                 err =&