]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - sound/pci/hda/patch_realtek.c
ALSA: hda - Add new codec ALC668 and ALC900 (default name ALC1150)
[karo-tx-linux.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@just42.net>
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_auto_parser.h"
36 #include "hda_beep.h"
37 #include "hda_jack.h"
38
39 /* unsol event tags */
40 #define ALC_FRONT_EVENT         0x01
41 #define ALC_DCVOL_EVENT         0x02
42 #define ALC_HP_EVENT            0x04
43 #define ALC_MIC_EVENT           0x08
44
45 /* for GPIO Poll */
46 #define GPIO_MASK       0x03
47
48 /* extra amp-initialization sequence types */
49 enum {
50         ALC_INIT_NONE,
51         ALC_INIT_DEFAULT,
52         ALC_INIT_GPIO1,
53         ALC_INIT_GPIO2,
54         ALC_INIT_GPIO3,
55 };
56
57 struct alc_customize_define {
58         unsigned int  sku_cfg;
59         unsigned char port_connectivity;
60         unsigned char check_sum;
61         unsigned char customization;
62         unsigned char external_amp;
63         unsigned int  enable_pcbeep:1;
64         unsigned int  platform_type:1;
65         unsigned int  swap:1;
66         unsigned int  override:1;
67         unsigned int  fixup:1; /* Means that this sku is set by driver, not read from hw */
68 };
69
70 struct alc_multi_io {
71         hda_nid_t pin;          /* multi-io widget pin NID */
72         hda_nid_t dac;          /* DAC to be connected */
73         unsigned int ctl_in;    /* cached input-pin control value */
74 };
75
76 enum {
77         ALC_AUTOMUTE_PIN,       /* change the pin control */
78         ALC_AUTOMUTE_AMP,       /* mute/unmute the pin AMP */
79         ALC_AUTOMUTE_MIXER,     /* mute/unmute mixer widget AMP */
80 };
81
82 #define MAX_VOL_NIDS    0x40
83
84 /* make compatible with old code */
85 #define alc_apply_pincfgs       snd_hda_apply_pincfgs
86 #define alc_apply_fixup         snd_hda_apply_fixup
87 #define alc_pick_fixup          snd_hda_pick_fixup
88 #define alc_fixup               hda_fixup
89 #define alc_pincfg              hda_pintbl
90 #define alc_model_fixup         hda_model_fixup
91
92 #define ALC_FIXUP_PINS  HDA_FIXUP_PINS
93 #define ALC_FIXUP_VERBS HDA_FIXUP_VERBS
94 #define ALC_FIXUP_FUNC  HDA_FIXUP_FUNC
95
96 #define ALC_FIXUP_ACT_PRE_PROBE HDA_FIXUP_ACT_PRE_PROBE
97 #define ALC_FIXUP_ACT_PROBE     HDA_FIXUP_ACT_PROBE
98 #define ALC_FIXUP_ACT_INIT      HDA_FIXUP_ACT_INIT
99 #define ALC_FIXUP_ACT_BUILD     HDA_FIXUP_ACT_BUILD
100
101
102 struct alc_spec {
103         struct hda_gen_spec gen;
104
105         /* codec parameterization */
106         const struct snd_kcontrol_new *mixers[5];       /* mixer arrays */
107         unsigned int num_mixers;
108         const struct snd_kcontrol_new *cap_mixer;       /* capture mixer */
109         unsigned int beep_amp;  /* beep amp value, set via set_beep_amp() */
110
111         char stream_name_analog[32];    /* analog PCM stream */
112         const struct hda_pcm_stream *stream_analog_playback;
113         const struct hda_pcm_stream *stream_analog_capture;
114         const struct hda_pcm_stream *stream_analog_alt_playback;
115         const struct hda_pcm_stream *stream_analog_alt_capture;
116
117         char stream_name_digital[32];   /* digital PCM stream */
118         const struct hda_pcm_stream *stream_digital_playback;
119         const struct hda_pcm_stream *stream_digital_capture;
120
121         /* playback */
122         struct hda_multi_out multiout;  /* playback set-up
123                                          * max_channels, dacs must be set
124                                          * dig_out_nid and hp_nid are optional
125                                          */
126         hda_nid_t alt_dac_nid;
127         hda_nid_t slave_dig_outs[3];    /* optional - for auto-parsing */
128         int dig_out_type;
129
130         /* capture */
131         unsigned int num_adc_nids;
132         const hda_nid_t *adc_nids;
133         const hda_nid_t *capsrc_nids;
134         hda_nid_t dig_in_nid;           /* digital-in NID; optional */
135         hda_nid_t mixer_nid;            /* analog-mixer NID */
136         DECLARE_BITMAP(vol_ctls, MAX_VOL_NIDS << 1);
137         DECLARE_BITMAP(sw_ctls, MAX_VOL_NIDS << 1);
138
139         /* capture setup for dynamic dual-adc switch */
140         hda_nid_t cur_adc;
141         unsigned int cur_adc_stream_tag;
142         unsigned int cur_adc_format;
143
144         /* capture source */
145         unsigned int num_mux_defs;
146         const struct hda_input_mux *input_mux;
147         unsigned int cur_mux[3];
148         hda_nid_t ext_mic_pin;
149         hda_nid_t dock_mic_pin;
150         hda_nid_t int_mic_pin;
151
152         /* channel model */
153         const struct hda_channel_mode *channel_mode;
154         int num_channel_mode;
155         int need_dac_fix;
156         int const_channel_count;
157         int ext_channel_count;
158
159         /* PCM information */
160         struct hda_pcm pcm_rec[3];      /* used in alc_build_pcms() */
161
162         /* dynamic controls, init_verbs and input_mux */
163         struct auto_pin_cfg autocfg;
164         struct alc_customize_define cdefine;
165         struct snd_array kctls;
166         struct hda_input_mux private_imux[3];
167         hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
168         hda_nid_t private_adc_nids[AUTO_CFG_MAX_OUTS];
169         hda_nid_t private_capsrc_nids[AUTO_CFG_MAX_OUTS];
170         hda_nid_t imux_pins[HDA_MAX_NUM_INPUTS];
171         unsigned int dyn_adc_idx[HDA_MAX_NUM_INPUTS];
172         int int_mic_idx, ext_mic_idx, dock_mic_idx; /* for auto-mic */
173         hda_nid_t inv_dmic_pin;
174
175         /* hooks */
176         void (*init_hook)(struct hda_codec *codec);
177 #ifdef CONFIG_SND_HDA_POWER_SAVE
178         void (*power_hook)(struct hda_codec *codec);
179 #endif
180         void (*shutup)(struct hda_codec *codec);
181         void (*automute_hook)(struct hda_codec *codec);
182
183         /* for pin sensing */
184         unsigned int hp_jack_present:1;
185         unsigned int line_jack_present:1;
186         unsigned int master_mute:1;
187         unsigned int auto_mic:1;
188         unsigned int auto_mic_valid_imux:1;     /* valid imux for auto-mic */
189         unsigned int automute_speaker:1; /* automute speaker outputs */
190         unsigned int automute_lo:1; /* automute LO outputs */
191         unsigned int detect_hp:1;       /* Headphone detection enabled */
192         unsigned int detect_lo:1;       /* Line-out detection enabled */
193         unsigned int automute_speaker_possible:1; /* there are speakers and either LO or HP */
194         unsigned int automute_lo_possible:1;      /* there are line outs and HP */
195         unsigned int keep_vref_in_automute:1; /* Don't clear VREF in automute */
196
197         /* other flags */
198         unsigned int no_analog :1; /* digital I/O only */
199         unsigned int dyn_adc_switch:1; /* switch ADCs (for ALC275) */
200         unsigned int single_input_src:1;
201         unsigned int vol_in_capsrc:1; /* use capsrc volume (ADC has no vol) */
202         unsigned int parse_flags; /* passed to snd_hda_parse_pin_defcfg() */
203         unsigned int shared_mic_hp:1; /* HP/Mic-in sharing */
204         unsigned int inv_dmic_fixup:1; /* has inverted digital-mic workaround */
205         unsigned int inv_dmic_muted:1; /* R-ch of inv d-mic is muted? */
206         unsigned int no_primary_hp:1; /* Don't prefer HP pins to speaker pins */
207
208         /* auto-mute control */
209         int automute_mode;
210         hda_nid_t automute_mixer_nid[AUTO_CFG_MAX_OUTS];
211
212         int init_amp;
213         int codec_variant;      /* flag for other variants */
214
215         /* for virtual master */
216         hda_nid_t vmaster_nid;
217         struct hda_vmaster_mute_hook vmaster_mute;
218 #ifdef CONFIG_SND_HDA_POWER_SAVE
219         struct hda_loopback_check loopback;
220         int num_loopbacks;
221         struct hda_amp_list loopback_list[8];
222 #endif
223
224         /* for PLL fix */
225         hda_nid_t pll_nid;
226         unsigned int pll_coef_idx, pll_coef_bit;
227         unsigned int coef0;
228
229         /* multi-io */
230         int multi_ios;
231         struct alc_multi_io multi_io[4];
232
233         /* bind volumes */
234         struct snd_array bind_ctls;
235 };
236
237 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
238                            int dir, unsigned int bits)
239 {
240         if (!nid)
241                 return false;
242         if (get_wcaps(codec, nid) & (1 << (dir + 1)))
243                 if (query_amp_caps(codec, nid, dir) & bits)
244                         return true;
245         return false;
246 }
247
248 #define nid_has_mute(codec, nid, dir) \
249         check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
250 #define nid_has_volume(codec, nid, dir) \
251         check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
252
253 /*
254  * input MUX handling
255  */
256 static int alc_mux_enum_info(struct snd_kcontrol *kcontrol,
257                              struct snd_ctl_elem_info *uinfo)
258 {
259         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
260         struct alc_spec *spec = codec->spec;
261         unsigned int mux_idx = snd_ctl_get_ioffidx(kcontrol, &uinfo->id);
262         if (mux_idx >= spec->num_mux_defs)
263                 mux_idx = 0;
264         if (!spec->input_mux[mux_idx].num_items && mux_idx > 0)
265                 mux_idx = 0;
266         return snd_hda_input_mux_info(&spec->input_mux[mux_idx], uinfo);
267 }
268
269 static int alc_mux_enum_get(struct snd_kcontrol *kcontrol,
270                             struct snd_ctl_elem_value *ucontrol)
271 {
272         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
273         struct alc_spec *spec = codec->spec;
274         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
275
276         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
277         return 0;
278 }
279
280 static bool alc_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
281 {
282         struct alc_spec *spec = codec->spec;
283         hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
284
285         if (spec->cur_adc && spec->cur_adc != new_adc) {
286                 /* stream is running, let's swap the current ADC */
287                 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
288                 spec->cur_adc = new_adc;
289                 snd_hda_codec_setup_stream(codec, new_adc,
290                                            spec->cur_adc_stream_tag, 0,
291                                            spec->cur_adc_format);
292                 return true;
293         }
294         return false;
295 }
296
297 static inline hda_nid_t get_capsrc(struct alc_spec *spec, int idx)
298 {
299         return spec->capsrc_nids ?
300                 spec->capsrc_nids[idx] : spec->adc_nids[idx];
301 }
302
303 static void call_update_outputs(struct hda_codec *codec);
304 static void alc_inv_dmic_sync(struct hda_codec *codec, bool force);
305
306 /* for shared I/O, change the pin-control accordingly */
307 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
308 {
309         struct alc_spec *spec = codec->spec;
310         unsigned int val;
311         hda_nid_t pin = spec->autocfg.inputs[1].pin;
312         /* NOTE: this assumes that there are only two inputs, the
313          * first is the real internal mic and the second is HP/mic jack.
314          */
315
316         val = snd_hda_get_default_vref(codec, pin);
317
318         /* This pin does not have vref caps - let's enable vref on pin 0x18
319            instead, as suggested by Realtek */
320         if (val == AC_PINCTL_VREF_HIZ) {
321                 const hda_nid_t vref_pin = 0x18;
322                 /* Sanity check pin 0x18 */
323                 if (get_wcaps_type(get_wcaps(codec, vref_pin)) == AC_WID_PIN &&
324                     get_defcfg_connect(snd_hda_codec_get_pincfg(codec, vref_pin)) == AC_JACK_PORT_NONE) {
325                         unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
326                         if (vref_val != AC_PINCTL_VREF_HIZ)
327                                 snd_hda_set_pin_ctl(codec, vref_pin, PIN_IN | (set_as_mic ? vref_val : 0));
328                 }
329         }
330
331         val = set_as_mic ? val | PIN_IN : PIN_HP;
332         snd_hda_set_pin_ctl(codec, pin, val);
333
334         spec->automute_speaker = !set_as_mic;
335         call_update_outputs(codec);
336 }
337
338 /* select the given imux item; either unmute exclusively or select the route */
339 static int alc_mux_select(struct hda_codec *codec, unsigned int adc_idx,
340                           unsigned int idx, bool force)
341 {
342         struct alc_spec *spec = codec->spec;
343         const struct hda_input_mux *imux;
344         unsigned int mux_idx;
345         int i, type, num_conns;
346         hda_nid_t nid;
347
348         if (!spec->input_mux)
349                 return 0;
350
351         mux_idx = adc_idx >= spec->num_mux_defs ? 0 : adc_idx;
352         imux = &spec->input_mux[mux_idx];
353         if (!imux->num_items && mux_idx > 0)
354                 imux = &spec->input_mux[0];
355         if (!imux->num_items)
356                 return 0;
357
358         if (idx >= imux->num_items)
359                 idx = imux->num_items - 1;
360         if (spec->cur_mux[adc_idx] == idx && !force)
361                 return 0;
362         spec->cur_mux[adc_idx] = idx;
363
364         if (spec->shared_mic_hp)
365                 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
366
367         if (spec->dyn_adc_switch) {
368                 alc_dyn_adc_pcm_resetup(codec, idx);
369                 adc_idx = spec->dyn_adc_idx[idx];
370         }
371
372         nid = get_capsrc(spec, adc_idx);
373
374         /* no selection? */
375         num_conns = snd_hda_get_num_conns(codec, nid);
376         if (num_conns <= 1)
377                 return 1;
378
379         type = get_wcaps_type(get_wcaps(codec, nid));
380         if (type == AC_WID_AUD_MIX) {
381                 /* Matrix-mixer style (e.g. ALC882) */
382                 int active = imux->items[idx].index;
383                 for (i = 0; i < num_conns; i++) {
384                         unsigned int v = (i == active) ? 0 : HDA_AMP_MUTE;
385                         snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, i,
386                                                  HDA_AMP_MUTE, v);
387                 }
388         } else {
389                 /* MUX style (e.g. ALC880) */
390                 snd_hda_codec_write_cache(codec, nid, 0,
391                                           AC_VERB_SET_CONNECT_SEL,
392                                           imux->items[idx].index);
393         }
394         alc_inv_dmic_sync(codec, true);
395         return 1;
396 }
397
398 static int alc_mux_enum_put(struct snd_kcontrol *kcontrol,
399                             struct snd_ctl_elem_value *ucontrol)
400 {
401         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
402         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
403         return alc_mux_select(codec, adc_idx,
404                               ucontrol->value.enumerated.item[0], false);
405 }
406
407 /*
408  * set up the input pin config (depending on the given auto-pin type)
409  */
410 static void alc_set_input_pin(struct hda_codec *codec, hda_nid_t nid,
411                               int auto_pin_type)
412 {
413         unsigned int val = PIN_IN;
414         if (auto_pin_type == AUTO_PIN_MIC)
415                 val |= snd_hda_get_default_vref(codec, nid);
416         snd_hda_set_pin_ctl(codec, nid, val);
417 }
418
419 /*
420  * Append the given mixer and verb elements for the later use
421  * The mixer array is referred in build_controls(), and init_verbs are
422  * called in init().
423  */
424 static void add_mixer(struct alc_spec *spec, const struct snd_kcontrol_new *mix)
425 {
426         if (snd_BUG_ON(spec->num_mixers >= ARRAY_SIZE(spec->mixers)))
427                 return;
428         spec->mixers[spec->num_mixers++] = mix;
429 }
430
431 /*
432  * GPIO setup tables, used in initialization
433  */
434 /* Enable GPIO mask and set output */
435 static const struct hda_verb alc_gpio1_init_verbs[] = {
436         {0x01, AC_VERB_SET_GPIO_MASK, 0x01},
437         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
438         {0x01, AC_VERB_SET_GPIO_DATA, 0x01},
439         { }
440 };
441
442 static const struct hda_verb alc_gpio2_init_verbs[] = {
443         {0x01, AC_VERB_SET_GPIO_MASK, 0x02},
444         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
445         {0x01, AC_VERB_SET_GPIO_DATA, 0x02},
446         { }
447 };
448
449 static const struct hda_verb alc_gpio3_init_verbs[] = {
450         {0x01, AC_VERB_SET_GPIO_MASK, 0x03},
451         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x03},
452         {0x01, AC_VERB_SET_GPIO_DATA, 0x03},
453         { }
454 };
455
456 /*
457  * Fix hardware PLL issue
458  * On some codecs, the analog PLL gating control must be off while
459  * the default value is 1.
460  */
461 static void alc_fix_pll(struct hda_codec *codec)
462 {
463         struct alc_spec *spec = codec->spec;
464         unsigned int val;
465
466         if (!spec->pll_nid)
467                 return;
468         snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
469                             spec->pll_coef_idx);
470         val = snd_hda_codec_read(codec, spec->pll_nid, 0,
471                                  AC_VERB_GET_PROC_COEF, 0);
472         snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
473                             spec->pll_coef_idx);
474         snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_PROC_COEF,
475                             val & ~(1 << spec->pll_coef_bit));
476 }
477
478 static void alc_fix_pll_init(struct hda_codec *codec, hda_nid_t nid,
479                              unsigned int coef_idx, unsigned int coef_bit)
480 {
481         struct alc_spec *spec = codec->spec;
482         spec->pll_nid = nid;
483         spec->pll_coef_idx = coef_idx;
484         spec->pll_coef_bit = coef_bit;
485         alc_fix_pll(codec);
486 }
487
488 /*
489  * Jack detections for HP auto-mute and mic-switch
490  */
491
492 /* check each pin in the given array; returns true if any of them is plugged */
493 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
494 {
495         int i, present = 0;
496
497         for (i = 0; i < num_pins; i++) {
498                 hda_nid_t nid = pins[i];
499                 if (!nid)
500                         break;
501                 present |= snd_hda_jack_detect(codec, nid);
502         }
503         return present;
504 }
505
506 /* standard HP/line-out auto-mute helper */
507 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
508                         bool mute, bool hp_out)
509 {
510         struct alc_spec *spec = codec->spec;
511         unsigned int mute_bits = mute ? HDA_AMP_MUTE : 0;
512         unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
513         int i;
514
515         for (i = 0; i < num_pins; i++) {
516                 hda_nid_t nid = pins[i];
517                 unsigned int val;
518                 if (!nid)
519                         break;
520                 switch (spec->automute_mode) {
521                 case ALC_AUTOMUTE_PIN:
522                         /* don't reset VREF value in case it's controlling
523                          * the amp (see alc861_fixup_asus_amp_vref_0f())
524                          */
525                         if (spec->keep_vref_in_automute) {
526                                 val = snd_hda_codec_read(codec, nid, 0,
527                                         AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
528                                 val &= ~PIN_HP;
529                         } else
530                                 val = 0;
531                         val |= pin_bits;
532                         snd_hda_set_pin_ctl(codec, nid, val);
533                         break;
534                 case ALC_AUTOMUTE_AMP:
535                         snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
536                                                  HDA_AMP_MUTE, mute_bits);
537                         break;
538                 case ALC_AUTOMUTE_MIXER:
539                         nid = spec->automute_mixer_nid[i];
540                         if (!nid)
541                                 break;
542                         snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 0,
543                                                  HDA_AMP_MUTE, mute_bits);
544                         snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 1,
545                                                  HDA_AMP_MUTE, mute_bits);
546                         break;
547                 }
548         }
549 }
550
551 /* Toggle outputs muting */
552 static void update_outputs(struct hda_codec *codec)
553 {
554         struct alc_spec *spec = codec->spec;
555         int on;
556
557         /* Control HP pins/amps depending on master_mute state;
558          * in general, HP pins/amps control should be enabled in all cases,
559          * but currently set only for master_mute, just to be safe
560          */
561         if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
562                 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
563                     spec->autocfg.hp_pins, spec->master_mute, true);
564
565         if (!spec->automute_speaker)
566                 on = 0;
567         else
568                 on = spec->hp_jack_present | spec->line_jack_present;
569         on |= spec->master_mute;
570         do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
571                     spec->autocfg.speaker_pins, on, false);
572
573         /* toggle line-out mutes if needed, too */
574         /* if LO is a copy of either HP or Speaker, don't need to handle it */
575         if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
576             spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
577                 return;
578         if (!spec->automute_lo)
579                 on = 0;
580         else
581                 on = spec->hp_jack_present;
582         on |= spec->master_mute;
583         do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
584                     spec->autocfg.line_out_pins, on, false);
585 }
586
587 static void call_update_outputs(struct hda_codec *codec)
588 {
589         struct alc_spec *spec = codec->spec;
590         if (spec->automute_hook)
591                 spec->automute_hook(codec);
592         else
593                 update_outputs(codec);
594 }
595
596 /* standard HP-automute helper */
597 static void alc_hp_automute(struct hda_codec *codec)
598 {
599         struct alc_spec *spec = codec->spec;
600
601         spec->hp_jack_present =
602                 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
603                              spec->autocfg.hp_pins);
604         if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
605                 return;
606         call_update_outputs(codec);
607 }
608
609 /* standard line-out-automute helper */
610 static void alc_line_automute(struct hda_codec *codec)
611 {
612         struct alc_spec *spec = codec->spec;
613
614         if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
615                 return;
616         /* check LO jack only when it's different from HP */
617         if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
618                 return;
619
620         spec->line_jack_present =
621                 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
622                              spec->autocfg.line_out_pins);
623         if (!spec->automute_speaker || !spec->detect_lo)
624                 return;
625         call_update_outputs(codec);
626 }
627
628 #define get_connection_index(codec, mux, nid) \
629         snd_hda_get_conn_index(codec, mux, nid, 0)
630
631 /* standard mic auto-switch helper */
632 static void alc_mic_automute(struct hda_codec *codec)
633 {
634         struct alc_spec *spec = codec->spec;
635         hda_nid_t *pins = spec->imux_pins;
636
637         if (!spec->auto_mic || !spec->auto_mic_valid_imux)
638                 return;
639         if (snd_BUG_ON(!spec->adc_nids))
640                 return;
641         if (snd_BUG_ON(spec->int_mic_idx < 0 || spec->ext_mic_idx < 0))
642                 return;
643
644         if (snd_hda_jack_detect(codec, pins[spec->ext_mic_idx]))
645                 alc_mux_select(codec, 0, spec->ext_mic_idx, false);
646         else if (spec->dock_mic_idx >= 0 &&
647                    snd_hda_jack_detect(codec, pins[spec->dock_mic_idx]))
648                 alc_mux_select(codec, 0, spec->dock_mic_idx, false);
649         else
650                 alc_mux_select(codec, 0, spec->int_mic_idx, false);
651 }
652
653 /* handle the specified unsol action (ALC_XXX_EVENT) */
654 static void alc_exec_unsol_event(struct hda_codec *codec, int action)
655 {
656         switch (action) {
657         case ALC_HP_EVENT:
658                 alc_hp_automute(codec);
659                 break;
660         case ALC_FRONT_EVENT:
661                 alc_line_automute(codec);
662                 break;
663         case ALC_MIC_EVENT:
664                 alc_mic_automute(codec);
665                 break;
666         }
667         snd_hda_jack_report_sync(codec);
668 }
669
670 /* update the master volume per volume-knob's unsol event */
671 static void alc_update_knob_master(struct hda_codec *codec, hda_nid_t nid)
672 {
673         unsigned int val;
674         struct snd_kcontrol *kctl;
675         struct snd_ctl_elem_value *uctl;
676
677         kctl = snd_hda_find_mixer_ctl(codec, "Master Playback Volume");
678         if (!kctl)
679                 return;
680         uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
681         if (!uctl)
682                 return;
683         val = snd_hda_codec_read(codec, nid, 0,
684                                  AC_VERB_GET_VOLUME_KNOB_CONTROL, 0);
685         val &= HDA_AMP_VOLMASK;
686         uctl->value.integer.value[0] = val;
687         uctl->value.integer.value[1] = val;
688         kctl->put(kctl, uctl);
689         kfree(uctl);
690 }
691
692 /* unsolicited event for HP jack sensing */
693 static void alc_unsol_event(struct hda_codec *codec, unsigned int res)
694 {
695         int action;
696
697         if (codec->vendor_id == 0x10ec0880)
698                 res >>= 28;
699         else
700                 res >>= 26;
701         action = snd_hda_jack_get_action(codec, res);
702         if (action == ALC_DCVOL_EVENT) {
703                 /* Execute the dc-vol event here as it requires the NID
704                  * but we don't pass NID to alc_exec_unsol_event().
705                  * Once when we convert all static quirks to the auto-parser,
706                  * this can be integerated into there.
707                  */
708                 struct hda_jack_tbl *jack;
709                 jack = snd_hda_jack_tbl_get_from_tag(codec, res);
710                 if (jack)
711                         alc_update_knob_master(codec, jack->nid);
712                 return;
713         }
714         alc_exec_unsol_event(codec, action);
715 }
716
717 /* call init functions of standard auto-mute helpers */
718 static void alc_inithook(struct hda_codec *codec)
719 {
720         alc_hp_automute(codec);
721         alc_line_automute(codec);
722         alc_mic_automute(codec);
723 }
724
725 /* additional initialization for ALC888 variants */
726 static void alc888_coef_init(struct hda_codec *codec)
727 {
728         unsigned int tmp;
729
730         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 0);
731         tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
732         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
733         if ((tmp & 0xf0) == 0x20)
734                 /* alc888S-VC */
735                 snd_hda_codec_read(codec, 0x20, 0,
736                                    AC_VERB_SET_PROC_COEF, 0x830);
737          else
738                  /* alc888-VB */
739                  snd_hda_codec_read(codec, 0x20, 0,
740                                     AC_VERB_SET_PROC_COEF, 0x3030);
741 }
742
743 /* additional initialization for ALC889 variants */
744 static void alc889_coef_init(struct hda_codec *codec)
745 {
746         unsigned int tmp;
747
748         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
749         tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
750         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
751         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_PROC_COEF, tmp|0x2010);
752 }
753
754 /* turn on/off EAPD control (only if available) */
755 static void set_eapd(struct hda_codec *codec, hda_nid_t nid, int on)
756 {
757         if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
758                 return;
759         if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
760                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_EAPD_BTLENABLE,
761                                     on ? 2 : 0);
762 }
763
764 /* turn on/off EAPD controls of the codec */
765 static void alc_auto_setup_eapd(struct hda_codec *codec, bool on)
766 {
767         /* We currently only handle front, HP */
768         static hda_nid_t pins[] = {
769                 0x0f, 0x10, 0x14, 0x15, 0
770         };
771         hda_nid_t *p;
772         for (p = pins; *p; p++)
773                 set_eapd(codec, *p, on);
774 }
775
776 /* generic shutup callback;
777  * just turning off EPAD and a little pause for avoiding pop-noise
778  */
779 static void alc_eapd_shutup(struct hda_codec *codec)
780 {
781         alc_auto_setup_eapd(codec, false);
782         msleep(200);
783 }
784
785 /* generic EAPD initialization */
786 static void alc_auto_init_amp(struct hda_codec *codec, int type)
787 {
788         unsigned int tmp;
789
790         alc_auto_setup_eapd(codec, true);
791         switch (type) {
792         case ALC_INIT_GPIO1:
793                 snd_hda_sequence_write(codec, alc_gpio1_init_verbs);
794                 break;
795         case ALC_INIT_GPIO2:
796                 snd_hda_sequence_write(codec, alc_gpio2_init_verbs);
797                 break;
798         case ALC_INIT_GPIO3:
799                 snd_hda_sequence_write(codec, alc_gpio3_init_verbs);
800                 break;
801         case ALC_INIT_DEFAULT:
802                 switch (codec->vendor_id) {
803                 case 0x10ec0260:
804                         snd_hda_codec_write(codec, 0x1a, 0,
805                                             AC_VERB_SET_COEF_INDEX, 7);
806                         tmp = snd_hda_codec_read(codec, 0x1a, 0,
807                                                  AC_VERB_GET_PROC_COEF, 0);
808                         snd_hda_codec_write(codec, 0x1a, 0,
809                                             AC_VERB_SET_COEF_INDEX, 7);
810                         snd_hda_codec_write(codec, 0x1a, 0,
811                                             AC_VERB_SET_PROC_COEF,
812                                             tmp | 0x2010);
813                         break;
814                 case 0x10ec0262:
815                 case 0x10ec0880:
816                 case 0x10ec0882:
817                 case 0x10ec0883:
818                 case 0x10ec0885:
819                 case 0x10ec0887:
820                 /*case 0x10ec0889:*/ /* this causes an SPDIF problem */
821                         alc889_coef_init(codec);
822                         break;
823                 case 0x10ec0888:
824                         alc888_coef_init(codec);
825                         break;
826 #if 0 /* XXX: This may cause the silent output on speaker on some machines */
827                 case 0x10ec0267:
828                 case 0x10ec0268:
829                         snd_hda_codec_write(codec, 0x20, 0,
830                                             AC_VERB_SET_COEF_INDEX, 7);
831                         tmp = snd_hda_codec_read(codec, 0x20, 0,
832                                                  AC_VERB_GET_PROC_COEF, 0);
833                         snd_hda_codec_write(codec, 0x20, 0,
834                                             AC_VERB_SET_COEF_INDEX, 7);
835                         snd_hda_codec_write(codec, 0x20, 0,
836                                             AC_VERB_SET_PROC_COEF,
837                                             tmp | 0x3000);
838                         break;
839 #endif /* XXX */
840                 }
841                 break;
842         }
843 }
844
845 /*
846  * Auto-Mute mode mixer enum support
847  */
848 static int alc_automute_mode_info(struct snd_kcontrol *kcontrol,
849                                   struct snd_ctl_elem_info *uinfo)
850 {
851         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
852         struct alc_spec *spec = codec->spec;
853         static const char * const texts2[] = {
854                 "Disabled", "Enabled"
855         };
856         static const char * const texts3[] = {
857                 "Disabled", "Speaker Only", "Line Out+Speaker"
858         };
859         const char * const *texts;
860
861         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
862         uinfo->count = 1;
863         if (spec->automute_speaker_possible && spec->automute_lo_possible) {
864                 uinfo->value.enumerated.items = 3;
865                 texts = texts3;
866         } else {
867                 uinfo->value.enumerated.items = 2;
868                 texts = texts2;
869         }
870         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
871                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
872         strcpy(uinfo->value.enumerated.name,
873                texts[uinfo->value.enumerated.item]);
874         return 0;
875 }
876
877 static int alc_automute_mode_get(struct snd_kcontrol *kcontrol,
878                                  struct snd_ctl_elem_value *ucontrol)
879 {
880         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
881         struct alc_spec *spec = codec->spec;
882         unsigned int val = 0;
883         if (spec->automute_speaker)
884                 val++;
885         if (spec->automute_lo)
886                 val++;
887
888         ucontrol->value.enumerated.item[0] = val;
889         return 0;
890 }
891
892 static int alc_automute_mode_put(struct snd_kcontrol *kcontrol,
893                                  struct snd_ctl_elem_value *ucontrol)
894 {
895         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
896         struct alc_spec *spec = codec->spec;
897
898         switch (ucontrol->value.enumerated.item[0]) {
899         case 0:
900                 if (!spec->automute_speaker && !spec->automute_lo)
901                         return 0;
902                 spec->automute_speaker = 0;
903                 spec->automute_lo = 0;
904                 break;
905         case 1:
906                 if (spec->automute_speaker_possible) {
907                         if (!spec->automute_lo && spec->automute_speaker)
908                                 return 0;
909                         spec->automute_speaker = 1;
910                         spec->automute_lo = 0;
911                 } else if (spec->automute_lo_possible) {
912                         if (spec->automute_lo)
913                                 return 0;
914                         spec->automute_lo = 1;
915                 } else
916                         return -EINVAL;
917                 break;
918         case 2:
919                 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
920                         return -EINVAL;
921                 if (spec->automute_speaker && spec->automute_lo)
922                         return 0;
923                 spec->automute_speaker = 1;
924                 spec->automute_lo = 1;
925                 break;
926         default:
927                 return -EINVAL;
928         }
929         call_update_outputs(codec);
930         return 1;
931 }
932
933 static const struct snd_kcontrol_new alc_automute_mode_enum = {
934         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
935         .name = "Auto-Mute Mode",
936         .info = alc_automute_mode_info,
937         .get = alc_automute_mode_get,
938         .put = alc_automute_mode_put,
939 };
940
941 static struct snd_kcontrol_new *alc_kcontrol_new(struct alc_spec *spec)
942 {
943         snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
944         return snd_array_new(&spec->kctls);
945 }
946
947 static int alc_add_automute_mode_enum(struct hda_codec *codec)
948 {
949         struct alc_spec *spec = codec->spec;
950         struct snd_kcontrol_new *knew;
951
952         knew = alc_kcontrol_new(spec);
953         if (!knew)
954                 return -ENOMEM;
955         *knew = alc_automute_mode_enum;
956         knew->name = kstrdup("Auto-Mute Mode", GFP_KERNEL);
957         if (!knew->name)
958                 return -ENOMEM;
959         return 0;
960 }
961
962 /*
963  * Check the availability of HP/line-out auto-mute;
964  * Set up appropriately if really supported
965  */
966 static void alc_init_automute(struct hda_codec *codec)
967 {
968         struct alc_spec *spec = codec->spec;
969         struct auto_pin_cfg *cfg = &spec->autocfg;
970         int present = 0;
971         int i;
972
973         if (cfg->hp_pins[0])
974                 present++;
975         if (cfg->line_out_pins[0])
976                 present++;
977         if (cfg->speaker_pins[0])
978                 present++;
979         if (present < 2) /* need two different output types */
980                 return;
981
982         if (!cfg->speaker_pins[0] &&
983             cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
984                 memcpy(cfg->speaker_pins, cfg->line_out_pins,
985                        sizeof(cfg->speaker_pins));
986                 cfg->speaker_outs = cfg->line_outs;
987         }
988
989         if (!cfg->hp_pins[0] &&
990             cfg->line_out_type == AUTO_PIN_HP_OUT) {
991                 memcpy(cfg->hp_pins, cfg->line_out_pins,
992                        sizeof(cfg->hp_pins));
993                 cfg->hp_outs = cfg->line_outs;
994         }
995
996         spec->automute_mode = ALC_AUTOMUTE_PIN;
997
998         for (i = 0; i < cfg->hp_outs; i++) {
999                 hda_nid_t nid = cfg->hp_pins[i];
1000                 if (!is_jack_detectable(codec, nid))
1001                         continue;
1002                 snd_printdd("realtek: Enable HP auto-muting on NID 0x%x\n",
1003                             nid);
1004                 snd_hda_jack_detect_enable(codec, nid, ALC_HP_EVENT);
1005                 spec->detect_hp = 1;
1006         }
1007
1008         if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
1009                 if (cfg->speaker_outs)
1010                         for (i = 0; i < cfg->line_outs; i++) {
1011                                 hda_nid_t nid = cfg->line_out_pins[i];
1012                                 if (!is_jack_detectable(codec, nid))
1013                                         continue;
1014                                 snd_printdd("realtek: Enable Line-Out "
1015                                             "auto-muting on NID 0x%x\n", nid);
1016                                 snd_hda_jack_detect_enable(codec, nid,
1017                                                            ALC_FRONT_EVENT);
1018                                 spec->detect_lo = 1;
1019                 }
1020                 spec->automute_lo_possible = spec->detect_hp;
1021         }
1022
1023         spec->automute_speaker_possible = cfg->speaker_outs &&
1024                 (spec->detect_hp || spec->detect_lo);
1025
1026         spec->automute_lo = spec->automute_lo_possible;
1027         spec->automute_speaker = spec->automute_speaker_possible;
1028
1029         if (spec->automute_speaker_possible || spec->automute_lo_possible)
1030                 /* create a control for automute mode */
1031                 alc_add_automute_mode_enum(codec);
1032 }
1033
1034 /* return the position of NID in the list, or -1 if not found */
1035 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
1036 {
1037         int i;
1038         for (i = 0; i < nums; i++)
1039                 if (list[i] == nid)
1040                         return i;
1041         return -1;
1042 }
1043
1044 /* check whether dynamic ADC-switching is available */
1045 static bool alc_check_dyn_adc_switch(struct hda_codec *codec)
1046 {
1047         struct alc_spec *spec = codec->spec;
1048         struct hda_input_mux *imux = &spec->private_imux[0];
1049         int i, n, idx;
1050         hda_nid_t cap, pin;
1051
1052         if (imux != spec->input_mux) /* no dynamic imux? */
1053                 return false;
1054
1055         for (n = 0; n < spec->num_adc_nids; n++) {
1056                 cap = spec->private_capsrc_nids[n];
1057                 for (i = 0; i < imux->num_items; i++) {
1058                         pin = spec->imux_pins[i];
1059                         if (!pin)
1060                                 return false;
1061                         if (get_connection_index(codec, cap, pin) < 0)
1062                                 break;
1063                 }
1064                 if (i >= imux->num_items)
1065                         return true; /* no ADC-switch is needed */
1066         }
1067
1068         for (i = 0; i < imux->num_items; i++) {
1069                 pin = spec->imux_pins[i];
1070                 for (n = 0; n < spec->num_adc_nids; n++) {
1071                         cap = spec->private_capsrc_nids[n];
1072                         idx = get_connection_index(codec, cap, pin);
1073                         if (idx >= 0) {
1074                                 imux->items[i].index = idx;
1075                                 spec->dyn_adc_idx[i] = n;
1076                                 break;
1077                         }
1078                 }
1079         }
1080
1081         snd_printdd("realtek: enabling ADC switching\n");
1082         spec->dyn_adc_switch = 1;
1083         return true;
1084 }
1085
1086 /* check whether all auto-mic pins are valid; setup indices if OK */
1087 static bool alc_auto_mic_check_imux(struct hda_codec *codec)
1088 {
1089         struct alc_spec *spec = codec->spec;
1090         const struct hda_input_mux *imux;
1091
1092         if (!spec->auto_mic)
1093                 return false;
1094         if (spec->auto_mic_valid_imux)
1095                 return true; /* already checked */
1096
1097         /* fill up imux indices */
1098         if (!alc_check_dyn_adc_switch(codec)) {
1099                 spec->auto_mic = 0;
1100                 return false;
1101         }
1102
1103         imux = spec->input_mux;
1104         spec->ext_mic_idx = find_idx_in_nid_list(spec->ext_mic_pin,
1105                                         spec->imux_pins, imux->num_items);
1106         spec->int_mic_idx = find_idx_in_nid_list(spec->int_mic_pin,
1107                                         spec->imux_pins, imux->num_items);
1108         spec->dock_mic_idx = find_idx_in_nid_list(spec->dock_mic_pin,
1109                                         spec->imux_pins, imux->num_items);
1110         if (spec->ext_mic_idx < 0 || spec->int_mic_idx < 0) {
1111                 spec->auto_mic = 0;
1112                 return false; /* no corresponding imux */
1113         }
1114
1115         snd_hda_jack_detect_enable(codec, spec->ext_mic_pin, ALC_MIC_EVENT);
1116         if (spec->dock_mic_pin)
1117                 snd_hda_jack_detect_enable(codec, spec->dock_mic_pin,
1118                                            ALC_MIC_EVENT);
1119
1120         spec->auto_mic_valid_imux = 1;
1121         spec->auto_mic = 1;
1122         return true;
1123 }
1124
1125 /*
1126  * Check the availability of auto-mic switch;
1127  * Set up if really supported
1128  */
1129 static void alc_init_auto_mic(struct hda_codec *codec)
1130 {
1131         struct alc_spec *spec = codec->spec;
1132         struct auto_pin_cfg *cfg = &spec->autocfg;
1133         hda_nid_t fixed, ext, dock;
1134         int i;
1135
1136         if (spec->shared_mic_hp)
1137                 return; /* no auto-mic for the shared I/O */
1138
1139         spec->ext_mic_idx = spec->int_mic_idx = spec->dock_mic_idx = -1;
1140
1141         fixed = ext = dock = 0;
1142         for (i = 0; i < cfg->num_inputs; i++) {
1143                 hda_nid_t nid = cfg->inputs[i].pin;
1144                 unsigned int defcfg;
1145                 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1146                 switch (snd_hda_get_input_pin_attr(defcfg)) {
1147                 case INPUT_PIN_ATTR_INT:
1148                         if (fixed)
1149                                 return; /* already occupied */
1150                         if (cfg->inputs[i].type != AUTO_PIN_MIC)
1151                                 return; /* invalid type */
1152                         fixed = nid;
1153                         break;
1154                 case INPUT_PIN_ATTR_UNUSED:
1155                         return; /* invalid entry */
1156                 case INPUT_PIN_ATTR_DOCK:
1157                         if (dock)
1158                                 return; /* already occupied */
1159                         if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
1160                                 return; /* invalid type */
1161                         dock = nid;
1162                         break;
1163                 default:
1164                         if (ext)
1165                                 return; /* already occupied */
1166                         if (cfg->inputs[i].type != AUTO_PIN_MIC)
1167                                 return; /* invalid type */
1168                         ext = nid;
1169                         break;
1170                 }
1171         }
1172         if (!ext && dock) {
1173                 ext = dock;
1174                 dock = 0;
1175         }
1176         if (!ext || !fixed)
1177                 return;
1178         if (!is_jack_detectable(codec, ext))
1179                 return; /* no unsol support */
1180         if (dock && !is_jack_detectable(codec, dock))
1181                 return; /* no unsol support */
1182
1183         /* check imux indices */
1184         spec->ext_mic_pin = ext;
1185         spec->int_mic_pin = fixed;
1186         spec->dock_mic_pin = dock;
1187
1188         spec->auto_mic = 1;
1189         if (!alc_auto_mic_check_imux(codec))
1190                 return;
1191
1192         snd_printdd("realtek: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
1193                     ext, fixed, dock);
1194 }
1195
1196 /* check the availabilities of auto-mute and auto-mic switches */
1197 static void alc_auto_check_switches(struct hda_codec *codec)
1198 {
1199         alc_init_automute(codec);
1200         alc_init_auto_mic(codec);
1201 }
1202
1203 /*
1204  * Realtek SSID verification
1205  */
1206
1207 /* Could be any non-zero and even value. When used as fixup, tells
1208  * the driver to ignore any present sku defines.
1209  */
1210 #define ALC_FIXUP_SKU_IGNORE (2)
1211
1212 static void alc_fixup_sku_ignore(struct hda_codec *codec,
1213                                  const struct hda_fixup *fix, int action)
1214 {
1215         struct alc_spec *spec = codec->spec;
1216         if (action == HDA_FIXUP_ACT_PRE_PROBE) {
1217                 spec->cdefine.fixup = 1;
1218                 spec->cdefine.sku_cfg = ALC_FIXUP_SKU_IGNORE;
1219         }
1220 }
1221
1222 static int alc_auto_parse_customize_define(struct hda_codec *codec)
1223 {
1224         unsigned int ass, tmp, i;
1225         unsigned nid = 0;
1226         struct alc_spec *spec = codec->spec;
1227
1228         spec->cdefine.enable_pcbeep = 1; /* assume always enabled */
1229
1230         if (spec->cdefine.fixup) {
1231                 ass = spec->cdefine.sku_cfg;
1232                 if (ass == ALC_FIXUP_SKU_IGNORE)
1233                         return -1;
1234                 goto do_sku;
1235         }
1236
1237         ass = codec->subsystem_id & 0xffff;
1238         if (ass != codec->bus->pci->subsystem_device && (ass & 1))
1239                 goto do_sku;
1240
1241         nid = 0x1d;
1242         if (codec->vendor_id == 0x10ec0260)
1243                 nid = 0x17;
1244         ass = snd_hda_codec_get_pincfg(codec, nid);
1245
1246         if (!(ass & 1)) {
1247                 printk(KERN_INFO "hda_codec: %s: SKU not ready 0x%08x\n",
1248                        codec->chip_name, ass);
1249                 return -1;
1250         }
1251
1252         /* check sum */
1253         tmp = 0;
1254         for (i = 1; i < 16; i++) {
1255                 if ((ass >> i) & 1)
1256                         tmp++;
1257         }
1258         if (((ass >> 16) & 0xf) != tmp)
1259                 return -1;
1260
1261         spec->cdefine.port_connectivity = ass >> 30;
1262         spec->cdefine.enable_pcbeep = (ass & 0x100000) >> 20;
1263         spec->cdefine.check_sum = (ass >> 16) & 0xf;
1264         spec->cdefine.customization = ass >> 8;
1265 do_sku:
1266         spec->cdefine.sku_cfg = ass;
1267         spec->cdefine.external_amp = (ass & 0x38) >> 3;
1268         spec->cdefine.platform_type = (ass & 0x4) >> 2;
1269         spec->cdefine.swap = (ass & 0x2) >> 1;
1270         spec->cdefine.override = ass & 0x1;
1271
1272         snd_printd("SKU: Nid=0x%x sku_cfg=0x%08x\n",
1273                    nid, spec->cdefine.sku_cfg);
1274         snd_printd("SKU: port_connectivity=0x%x\n",
1275                    spec->cdefine.port_connectivity);
1276         snd_printd("SKU: enable_pcbeep=0x%x\n", spec->cdefine.enable_pcbeep);
1277         snd_printd("SKU: check_sum=0x%08x\n", spec->cdefine.check_sum);
1278         snd_printd("SKU: customization=0x%08x\n", spec->cdefine.customization);
1279         snd_printd("SKU: external_amp=0x%x\n", spec->cdefine.external_amp);
1280         snd_printd("SKU: platform_type=0x%x\n", spec->cdefine.platform_type);
1281         snd_printd("SKU: swap=0x%x\n", spec->cdefine.swap);
1282         snd_printd("SKU: override=0x%x\n", spec->cdefine.override);
1283
1284         return 0;
1285 }
1286
1287 /* return true if the given NID is found in the list */
1288 static bool found_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
1289 {
1290         return find_idx_in_nid_list(nid, list, nums) >= 0;
1291 }
1292
1293 /* check subsystem ID and set up device-specific initialization;
1294  * return 1 if initialized, 0 if invalid SSID
1295  */
1296 /* 32-bit subsystem ID for BIOS loading in HD Audio codec.
1297  *      31 ~ 16 :       Manufacture ID
1298  *      15 ~ 8  :       SKU ID
1299  *      7  ~ 0  :       Assembly ID
1300  *      port-A --> pin 39/41, port-E --> pin 14/15, port-D --> pin 35/36
1301  */
1302 static int alc_subsystem_id(struct hda_codec *codec,
1303                             hda_nid_t porta, hda_nid_t porte,
1304                             hda_nid_t portd, hda_nid_t porti)
1305 {
1306         unsigned int ass, tmp, i;
1307         unsigned nid;
1308         struct alc_spec *spec = codec->spec;
1309
1310         if (spec->cdefine.fixup) {
1311                 ass = spec->cdefine.sku_cfg;
1312                 if (ass == ALC_FIXUP_SKU_IGNORE)
1313                         return 0;
1314                 goto do_sku;
1315         }
1316
1317         ass = codec->subsystem_id & 0xffff;
1318         if ((ass != codec->bus->pci->subsystem_device) && (ass & 1))
1319                 goto do_sku;
1320
1321         /* invalid SSID, check the special NID pin defcfg instead */
1322         /*
1323          * 31~30        : port connectivity
1324          * 29~21        : reserve
1325          * 20           : PCBEEP input
1326          * 19~16        : Check sum (15:1)
1327          * 15~1         : Custom
1328          * 0            : override
1329         */
1330         nid = 0x1d;
1331         if (codec->vendor_id == 0x10ec0260)
1332                 nid = 0x17;
1333         ass = snd_hda_codec_get_pincfg(codec, nid);
1334         snd_printd("realtek: No valid SSID, "
1335                    "checking pincfg 0x%08x for NID 0x%x\n",
1336                    ass, nid);
1337         if (!(ass & 1))
1338                 return 0;
1339         if ((ass >> 30) != 1)   /* no physical connection */
1340                 return 0;
1341
1342         /* check sum */
1343         tmp = 0;
1344         for (i = 1; i < 16; i++) {
1345                 if ((ass >> i) & 1)
1346                         tmp++;
1347         }
1348         if (((ass >> 16) & 0xf) != tmp)
1349                 return 0;
1350 do_sku:
1351         snd_printd("realtek: Enabling init ASM_ID=0x%04x CODEC_ID=%08x\n",
1352                    ass & 0xffff, codec->vendor_id);
1353         /*
1354          * 0 : override
1355          * 1 :  Swap Jack
1356          * 2 : 0 --> Desktop, 1 --> Laptop
1357          * 3~5 : External Amplifier control
1358          * 7~6 : Reserved
1359         */
1360         tmp = (ass & 0x38) >> 3;        /* external Amp control */
1361         switch (tmp) {
1362         case 1:
1363                 spec->init_amp = ALC_INIT_GPIO1;
1364                 break;
1365         case 3:
1366                 spec->init_amp = ALC_INIT_GPIO2;
1367                 break;
1368         case 7:
1369                 spec->init_amp = ALC_INIT_GPIO3;
1370                 break;
1371         case 5:
1372         default:
1373                 spec->init_amp = ALC_INIT_DEFAULT;
1374                 break;
1375         }
1376
1377         /* is laptop or Desktop and enable the function "Mute internal speaker
1378          * when the external headphone out jack is plugged"
1379          */
1380         if (!(ass & 0x8000))
1381                 return 1;
1382         /*
1383          * 10~8 : Jack location
1384          * 12~11: Headphone out -> 00: PortA, 01: PortE, 02: PortD, 03: Resvered
1385          * 14~13: Resvered
1386          * 15   : 1 --> enable the function "Mute internal speaker
1387          *              when the external headphone out jack is plugged"
1388          */
1389         if (!spec->autocfg.hp_pins[0] &&
1390             !(spec->autocfg.line_out_pins[0] &&
1391               spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)) {
1392                 hda_nid_t nid;
1393                 tmp = (ass >> 11) & 0x3;        /* HP to chassis */
1394                 if (tmp == 0)
1395                         nid = porta;
1396                 else if (tmp == 1)
1397                         nid = porte;
1398                 else if (tmp == 2)
1399                         nid = portd;
1400                 else if (tmp == 3)
1401                         nid = porti;
1402                 else
1403                         return 1;
1404                 if (found_in_nid_list(nid, spec->autocfg.line_out_pins,
1405                                       spec->autocfg.line_outs))
1406                         return 1;
1407                 spec->autocfg.hp_pins[0] = nid;
1408         }
1409         return 1;
1410 }
1411
1412 /* Check the validity of ALC subsystem-id
1413  * ports contains an array of 4 pin NIDs for port-A, E, D and I */
1414 static void alc_ssid_check(struct hda_codec *codec, const hda_nid_t *ports)
1415 {
1416         if (!alc_subsystem_id(codec, ports[0], ports[1], ports[2], ports[3])) {
1417                 struct alc_spec *spec = codec->spec;
1418                 snd_printd("realtek: "
1419                            "Enable default setup for auto mode as fallback\n");
1420                 spec->init_amp = ALC_INIT_DEFAULT;
1421         }
1422 }
1423
1424 /*
1425  * COEF access helper functions
1426  */
1427 static int alc_read_coef_idx(struct hda_codec *codec,
1428                         unsigned int coef_idx)
1429 {
1430         unsigned int val;
1431         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX,
1432                                 coef_idx);
1433         val = snd_hda_codec_read(codec, 0x20, 0,
1434                                 AC_VERB_GET_PROC_COEF, 0);
1435         return val;
1436 }
1437
1438 static void alc_write_coef_idx(struct hda_codec *codec, unsigned int coef_idx,
1439                                                         unsigned int coef_val)
1440 {
1441         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX,
1442                             coef_idx);
1443         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_PROC_COEF,
1444                             coef_val);
1445 }
1446
1447 /* a special bypass for COEF 0; read the cached value at the second time */
1448 static unsigned int alc_get_coef0(struct hda_codec *codec)
1449 {
1450         struct alc_spec *spec = codec->spec;
1451         if (!spec->coef0)
1452                 spec->coef0 = alc_read_coef_idx(codec, 0);
1453         return spec->coef0;
1454 }
1455
1456 /*
1457  * Digital I/O handling
1458  */
1459
1460 /* set right pin controls for digital I/O */
1461 static void alc_auto_init_digital(struct hda_codec *codec)
1462 {
1463         struct alc_spec *spec = codec->spec;
1464         int i;
1465         hda_nid_t pin, dac;
1466
1467         for (i = 0; i < spec->autocfg.dig_outs; i++) {
1468                 pin = spec->autocfg.dig_out_pins[i];
1469                 if (!pin)
1470                         continue;
1471                 snd_hda_set_pin_ctl(codec, pin, PIN_OUT);
1472                 if (!i)
1473                         dac = spec->multiout.dig_out_nid;
1474                 else
1475                         dac = spec->slave_dig_outs[i - 1];
1476                 if (!dac || !(get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
1477                         continue;
1478                 snd_hda_codec_write(codec, dac, 0,
1479                                     AC_VERB_SET_AMP_GAIN_MUTE,
1480                                     AMP_OUT_UNMUTE);
1481         }
1482         pin = spec->autocfg.dig_in_pin;
1483         if (pin)
1484                 snd_hda_set_pin_ctl(codec, pin, PIN_IN);
1485 }
1486
1487 /* parse digital I/Os and set up NIDs in BIOS auto-parse mode */
1488 static void alc_auto_parse_digital(struct hda_codec *codec)
1489 {
1490         struct alc_spec *spec = codec->spec;
1491         int i, err, nums;
1492         hda_nid_t dig_nid;
1493
1494         /* support multiple SPDIFs; the secondary is set up as a slave */
1495         nums = 0;
1496         for (i = 0; i < spec->autocfg.dig_outs; i++) {
1497                 hda_nid_t conn[4];
1498                 err = snd_hda_get_connections(codec,
1499                                               spec->autocfg.dig_out_pins[i],
1500                                               conn, ARRAY_SIZE(conn));
1501                 if (err <= 0)
1502                         continue;
1503                 dig_nid = conn[0]; /* assume the first element is audio-out */
1504                 if (!nums) {
1505                         spec->multiout.dig_out_nid = dig_nid;
1506                         spec->dig_out_type = spec->autocfg.dig_out_type[0];
1507                 } else {
1508                         spec->multiout.slave_dig_outs = spec->slave_dig_outs;
1509                         if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
1510                                 break;
1511                         spec->slave_dig_outs[nums - 1] = dig_nid;
1512                 }
1513                 nums++;
1514         }
1515
1516         if (spec->autocfg.dig_in_pin) {
1517                 dig_nid = codec->start_nid;
1518                 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
1519                         unsigned int wcaps = get_wcaps(codec, dig_nid);
1520                         if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
1521                                 continue;
1522                         if (!(wcaps & AC_WCAP_DIGITAL))
1523                                 continue;
1524                         if (!(wcaps & AC_WCAP_CONN_LIST))
1525                                 continue;
1526                         err = get_connection_index(codec, dig_nid,
1527                                                    spec->autocfg.dig_in_pin);
1528                         if (err >= 0) {
1529                                 spec->dig_in_nid = dig_nid;
1530                                 break;
1531                         }
1532                 }
1533         }
1534 }
1535
1536 /*
1537  * capture mixer elements
1538  */
1539 static int alc_cap_vol_info(struct snd_kcontrol *kcontrol,
1540                             struct snd_ctl_elem_info *uinfo)
1541 {
1542         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1543         struct alc_spec *spec = codec->spec;
1544         unsigned long val;
1545         int err;
1546
1547         mutex_lock(&codec->control_mutex);
1548         if (spec->vol_in_capsrc)
1549                 val = HDA_COMPOSE_AMP_VAL(spec->capsrc_nids[0], 3, 0, HDA_OUTPUT);
1550         else
1551                 val = HDA_COMPOSE_AMP_VAL(spec->adc_nids[0], 3, 0, HDA_INPUT);
1552         kcontrol->private_value = val;
1553         err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
1554         mutex_unlock(&codec->control_mutex);
1555         return err;
1556 }
1557
1558 static int alc_cap_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1559                            unsigned int size, unsigned int __user *tlv)
1560 {
1561         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1562         struct alc_spec *spec = codec->spec;
1563         unsigned long val;
1564         int err;
1565
1566         mutex_lock(&codec->control_mutex);
1567         if (spec->vol_in_capsrc)
1568                 val = HDA_COMPOSE_AMP_VAL(spec->capsrc_nids[0], 3, 0, HDA_OUTPUT);
1569         else
1570                 val = HDA_COMPOSE_AMP_VAL(spec->adc_nids[0], 3, 0, HDA_INPUT);
1571         kcontrol->private_value = val;
1572         err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
1573         mutex_unlock(&codec->control_mutex);
1574         return err;
1575 }
1576
1577 typedef int (*getput_call_t)(struct snd_kcontrol *kcontrol,
1578                              struct snd_ctl_elem_value *ucontrol);
1579
1580 static int alc_cap_getput_caller(struct snd_kcontrol *kcontrol,
1581                                  struct snd_ctl_elem_value *ucontrol,
1582                                  getput_call_t func, bool is_put)
1583 {
1584         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1585         struct alc_spec *spec = codec->spec;
1586         int i, err = 0;
1587
1588         mutex_lock(&codec->control_mutex);
1589         if (is_put && spec->dyn_adc_switch) {
1590                 for (i = 0; i < spec->num_adc_nids; i++) {
1591                         kcontrol->private_value =
1592                                 HDA_COMPOSE_AMP_VAL(spec->adc_nids[i],
1593                                                     3, 0, HDA_INPUT);
1594                         err = func(kcontrol, ucontrol);
1595                         if (err < 0)
1596                                 goto error;
1597                 }
1598         } else {
1599                 i = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1600                 if (spec->vol_in_capsrc)
1601                         kcontrol->private_value =
1602                                 HDA_COMPOSE_AMP_VAL(spec->capsrc_nids[i],
1603                                                     3, 0, HDA_OUTPUT);
1604                 else
1605                         kcontrol->private_value =
1606                                 HDA_COMPOSE_AMP_VAL(spec->adc_nids[i],
1607                                                     3, 0, HDA_INPUT);
1608                 err = func(kcontrol, ucontrol);
1609         }
1610         if (err >= 0 && is_put)
1611                 alc_inv_dmic_sync(codec, false);
1612  error:
1613         mutex_unlock(&codec->control_mutex);
1614         return err;
1615 }
1616
1617 static int alc_cap_vol_get(struct snd_kcontrol *kcontrol,
1618                            struct snd_ctl_elem_value *ucontrol)
1619 {
1620         return alc_cap_getput_caller(kcontrol, ucontrol,
1621                                      snd_hda_mixer_amp_volume_get, false);
1622 }
1623
1624 static int alc_cap_vol_put(struct snd_kcontrol *kcontrol,
1625                            struct snd_ctl_elem_value *ucontrol)
1626 {
1627         return alc_cap_getput_caller(kcontrol, ucontrol,
1628                                      snd_hda_mixer_amp_volume_put, true);
1629 }
1630
1631 /* capture mixer elements */
1632 #define alc_cap_sw_info         snd_ctl_boolean_stereo_info
1633
1634 static int alc_cap_sw_get(struct snd_kcontrol *kcontrol,
1635                           struct snd_ctl_elem_value *ucontrol)
1636 {
1637         return alc_cap_getput_caller(kcontrol, ucontrol,
1638                                      snd_hda_mixer_amp_switch_get, false);
1639 }
1640
1641 static int alc_cap_sw_put(struct snd_kcontrol *kcontrol,
1642                           struct snd_ctl_elem_value *ucontrol)
1643 {
1644         return alc_cap_getput_caller(kcontrol, ucontrol,
1645                                      snd_hda_mixer_amp_switch_put, true);
1646 }
1647
1648 #define _DEFINE_CAPMIX(num) \
1649         { \
1650                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1651                 .name = "Capture Switch", \
1652                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
1653                 .count = num, \
1654                 .info = alc_cap_sw_info, \
1655                 .get = alc_cap_sw_get, \
1656                 .put = alc_cap_sw_put, \
1657         }, \
1658         { \
1659                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1660                 .name = "Capture Volume", \
1661                 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE | \
1662                            SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
1663                            SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK), \
1664                 .count = num, \
1665                 .info = alc_cap_vol_info, \
1666                 .get = alc_cap_vol_get, \
1667                 .put = alc_cap_vol_put, \
1668                 .tlv = { .c = alc_cap_vol_tlv }, \
1669         }
1670
1671 #define _DEFINE_CAPSRC(num) \
1672         { \
1673                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1674                 /* .name = "Capture Source", */ \
1675                 .name = "Input Source", \
1676                 .count = num, \
1677                 .info = alc_mux_enum_info, \
1678                 .get = alc_mux_enum_get, \
1679                 .put = alc_mux_enum_put, \
1680         }
1681
1682 #define DEFINE_CAPMIX(num) \
1683 static const struct snd_kcontrol_new alc_capture_mixer ## num[] = { \
1684         _DEFINE_CAPMIX(num),                                  \
1685         _DEFINE_CAPSRC(num),                                  \
1686         { } /* end */                                         \
1687 }
1688
1689 #define DEFINE_CAPMIX_NOSRC(num) \
1690 static const struct snd_kcontrol_new alc_capture_mixer_nosrc ## num[] = { \
1691         _DEFINE_CAPMIX(num),                                        \
1692         { } /* end */                                               \
1693 }
1694
1695 /* up to three ADCs */
1696 DEFINE_CAPMIX(1);
1697 DEFINE_CAPMIX(2);
1698 DEFINE_CAPMIX(3);
1699 DEFINE_CAPMIX_NOSRC(1);
1700 DEFINE_CAPMIX_NOSRC(2);
1701 DEFINE_CAPMIX_NOSRC(3);
1702
1703 /*
1704  * Inverted digital-mic handling
1705  *
1706  * First off, it's a bit tricky.  The "Inverted Internal Mic Capture Switch"
1707  * gives the additional mute only to the right channel of the digital mic
1708  * capture stream.  This is a workaround for avoiding the almost silence
1709  * by summing the stereo stream from some (known to be ForteMedia)
1710  * digital mic unit.
1711  *
1712  * The logic is to call alc_inv_dmic_sync() after each action (possibly)
1713  * modifying ADC amp.  When the mute flag is set, it mutes the R-channel
1714  * without caching so that the cache can still keep the original value.
1715  * The cached value is then restored when the flag is set off or any other
1716  * than d-mic is used as the current input source.
1717  */
1718 static void alc_inv_dmic_sync(struct hda_codec *codec, bool force)
1719 {
1720         struct alc_spec *spec = codec->spec;
1721         int i;
1722
1723         if (!spec->inv_dmic_fixup)
1724                 return;
1725         if (!spec->inv_dmic_muted && !force)
1726                 return;
1727         for (i = 0; i < spec->num_adc_nids; i++) {
1728                 int src = spec->dyn_adc_switch ? 0 : i;
1729                 bool dmic_fixup = false;
1730                 hda_nid_t nid;
1731                 int parm, dir, v;
1732
1733                 if (spec->inv_dmic_muted &&
1734                     spec->imux_pins[spec->cur_mux[src]] == spec->inv_dmic_pin)
1735                         dmic_fixup = true;
1736                 if (!dmic_fixup && !force)
1737                         continue;
1738                 if (spec->vol_in_capsrc) {
1739                         nid = spec->capsrc_nids[i];
1740                         parm = AC_AMP_SET_RIGHT | AC_AMP_SET_OUTPUT;
1741                         dir = HDA_OUTPUT;
1742                 } else {
1743                         nid = spec->adc_nids[i];
1744                         parm = AC_AMP_SET_RIGHT | AC_AMP_SET_INPUT;
1745                         dir = HDA_INPUT;
1746                 }
1747                 /* we care only right channel */
1748                 v = snd_hda_codec_amp_read(codec, nid, 1, dir, 0);
1749                 if (v & 0x80) /* if already muted, we don't need to touch */
1750                         continue;
1751                 if (dmic_fixup) /* add mute for d-mic */
1752                         v |= 0x80;
1753                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1754                                     parm | v);
1755         }
1756 }
1757
1758 static int alc_inv_dmic_sw_get(struct snd_kcontrol *kcontrol,
1759                                struct snd_ctl_elem_value *ucontrol)
1760 {
1761         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1762         struct alc_spec *spec = codec->spec;
1763
1764         ucontrol->value.integer.value[0] = !spec->inv_dmic_muted;
1765         return 0;
1766 }
1767
1768 static int alc_inv_dmic_sw_put(struct snd_kcontrol *kcontrol,
1769                                struct snd_ctl_elem_value *ucontrol)
1770 {
1771         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1772         struct alc_spec *spec = codec->spec;
1773         unsigned int val = !ucontrol->value.integer.value[0];
1774
1775         if (val == spec->inv_dmic_muted)
1776                 return 0;
1777         spec->inv_dmic_muted = val;
1778         alc_inv_dmic_sync(codec, true);
1779         return 0;
1780 }
1781
1782 static const struct snd_kcontrol_new alc_inv_dmic_sw = {
1783         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1784         .info = snd_ctl_boolean_mono_info,
1785         .get = alc_inv_dmic_sw_get,
1786         .put = alc_inv_dmic_sw_put,
1787 };
1788
1789 static int alc_add_inv_dmic_mixer(struct hda_codec *codec, hda_nid_t nid)
1790 {
1791         struct alc_spec *spec = codec->spec;
1792         struct snd_kcontrol_new *knew = alc_kcontrol_new(spec);
1793         if (!knew)
1794                 return -ENOMEM;
1795         *knew = alc_inv_dmic_sw;
1796         knew->name = kstrdup("Inverted Internal Mic Capture Switch", GFP_KERNEL);
1797         if (!knew->name)
1798                 return -ENOMEM;
1799         spec->inv_dmic_fixup = 1;
1800         spec->inv_dmic_muted = 0;
1801         spec->inv_dmic_pin = nid;
1802         return 0;
1803 }
1804
1805 /* typically the digital mic is put at node 0x12 */
1806 static void alc_fixup_inv_dmic_0x12(struct hda_codec *codec,
1807                                     const struct alc_fixup *fix, int action)
1808 {
1809         if (action == ALC_FIXUP_ACT_PROBE)
1810                 alc_add_inv_dmic_mixer(codec, 0x12);
1811 }
1812
1813 /*
1814  * virtual master controls
1815  */
1816
1817 /*
1818  * slave controls for virtual master
1819  */
1820 static const char * const alc_slave_pfxs[] = {
1821         "Front", "Surround", "Center", "LFE", "Side",
1822         "Headphone", "Speaker", "Mono", "Line Out",
1823         "CLFE", "Bass Speaker", "PCM",
1824         NULL,
1825 };
1826
1827 /*
1828  * build control elements
1829  */
1830
1831 #define NID_MAPPING             (-1)
1832
1833 #define SUBDEV_SPEAKER_         (0 << 6)
1834 #define SUBDEV_HP_              (1 << 6)
1835 #define SUBDEV_LINE_            (2 << 6)
1836 #define SUBDEV_SPEAKER(x)       (SUBDEV_SPEAKER_ | ((x) & 0x3f))
1837 #define SUBDEV_HP(x)            (SUBDEV_HP_ | ((x) & 0x3f))
1838 #define SUBDEV_LINE(x)          (SUBDEV_LINE_ | ((x) & 0x3f))
1839
1840 static void alc_free_kctls(struct hda_codec *codec);
1841
1842 #ifdef CONFIG_SND_HDA_INPUT_BEEP
1843 /* additional beep mixers; the actual parameters are overwritten at build */
1844 static const struct snd_kcontrol_new alc_beep_mixer[] = {
1845         HDA_CODEC_VOLUME("Beep Playback Volume", 0, 0, HDA_INPUT),
1846         HDA_CODEC_MUTE_BEEP("Beep Playback Switch", 0, 0, HDA_INPUT),
1847         { } /* end */
1848 };
1849 #endif
1850
1851 static int __alc_build_controls(struct hda_codec *codec)
1852 {
1853         struct alc_spec *spec = codec->spec;
1854         struct snd_kcontrol *kctl = NULL;
1855         const struct snd_kcontrol_new *knew;
1856         int i, j, err;
1857         unsigned int u;
1858         hda_nid_t nid;
1859
1860         for (i = 0; i < spec->num_mixers; i++) {
1861                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1862                 if (err < 0)
1863                         return err;
1864         }
1865         if (spec->cap_mixer) {
1866                 err = snd_hda_add_new_ctls(codec, spec->cap_mixer);
1867                 if (err < 0)
1868                         return err;
1869         }
1870         if (spec->multiout.dig_out_nid) {
1871                 err = snd_hda_create_spdif_out_ctls(codec,
1872                                                     spec->multiout.dig_out_nid,
1873                                                     spec->multiout.dig_out_nid);
1874                 if (err < 0)
1875                         return err;
1876                 if (!spec->no_analog) {
1877                         err = snd_hda_create_spdif_share_sw(codec,
1878                                                             &spec->multiout);
1879                         if (err < 0)
1880                                 return err;
1881                         spec->multiout.share_spdif = 1;
1882                 }
1883         }
1884         if (spec->dig_in_nid) {
1885                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1886                 if (err < 0)
1887                         return err;
1888         }
1889
1890 #ifdef CONFIG_SND_HDA_INPUT_BEEP
1891         /* create beep controls if needed */
1892         if (spec->beep_amp) {
1893                 const struct snd_kcontrol_new *knew;
1894                 for (knew = alc_beep_mixer; knew->name; knew++) {
1895                         struct snd_kcontrol *kctl;
1896                         kctl = snd_ctl_new1(knew, codec);
1897                         if (!kctl)
1898                                 return -ENOMEM;
1899                         kctl->private_value = spec->beep_amp;
1900                         err = snd_hda_ctl_add(codec, 0, kctl);
1901                         if (err < 0)
1902                                 return err;
1903                 }
1904         }
1905 #endif
1906
1907         /* if we have no master control, let's create it */
1908         if (!spec->no_analog &&
1909             !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1910                 unsigned int vmaster_tlv[4];
1911                 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1912                                         HDA_OUTPUT, vmaster_tlv);
1913                 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
1914                                           vmaster_tlv, alc_slave_pfxs,
1915                                           "Playback Volume");
1916                 if (err < 0)
1917                         return err;
1918         }
1919         if (!spec->no_analog &&
1920             !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1921                 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
1922                                             NULL, alc_slave_pfxs,
1923                                             "Playback Switch",
1924                                             true, &spec->vmaster_mute.sw_kctl);
1925                 if (err < 0)
1926                         return err;
1927         }
1928
1929         /* assign Capture Source enums to NID */
1930         if (spec->capsrc_nids || spec->adc_nids) {
1931                 kctl = snd_hda_find_mixer_ctl(codec, "Capture Source");
1932                 if (!kctl)
1933                         kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1934                 for (i = 0; kctl && i < kctl->count; i++) {
1935                         err = snd_hda_add_nid(codec, kctl, i,
1936                                               get_capsrc(spec, i));
1937                         if (err < 0)
1938                                 return err;
1939                 }
1940         }
1941         if (spec->cap_mixer && spec->adc_nids) {
1942                 const char *kname = kctl ? kctl->id.name : NULL;
1943                 for (knew = spec->cap_mixer; knew->name; knew++) {
1944                         if (kname && strcmp(knew->name, kname) == 0)
1945                                 continue;
1946                         kctl = snd_hda_find_mixer_ctl(codec, knew->name);
1947                         for (i = 0; kctl && i < kctl->count; i++) {
1948                                 err = snd_hda_add_nid(codec, kctl, i,
1949                                                       spec->adc_nids[i]);
1950                                 if (err < 0)
1951                                         return err;
1952                         }
1953                 }
1954         }
1955
1956         /* other nid->control mapping */
1957         for (i = 0; i < spec->num_mixers; i++) {
1958                 for (knew = spec->mixers[i]; knew->name; knew++) {
1959                         if (knew->iface != NID_MAPPING)
1960                                 continue;
1961                         kctl = snd_hda_find_mixer_ctl(codec, knew->name);
1962                         if (kctl == NULL)
1963                                 continue;
1964                         u = knew->subdevice;
1965                         for (j = 0; j < 4; j++, u >>= 8) {
1966                                 nid = u & 0x3f;
1967                                 if (nid == 0)
1968                                         continue;
1969                                 switch (u & 0xc0) {
1970                                 case SUBDEV_SPEAKER_:
1971                                         nid = spec->autocfg.speaker_pins[nid];
1972                                         break;
1973                                 case SUBDEV_LINE_:
1974                                         nid = spec->autocfg.line_out_pins[nid];
1975                                         break;
1976                                 case SUBDEV_HP_:
1977                                         nid = spec->autocfg.hp_pins[nid];
1978                                         break;
1979                                 default:
1980                                         continue;
1981                                 }
1982                                 err = snd_hda_add_nid(codec, kctl, 0, nid);
1983                                 if (err < 0)
1984                                         return err;
1985                         }
1986                         u = knew->private_value;
1987                         for (j = 0; j < 4; j++, u >>= 8) {
1988                                 nid = u & 0xff;
1989                                 if (nid == 0)
1990                                         continue;
1991                                 err = snd_hda_add_nid(codec, kctl, 0, nid);
1992                                 if (err < 0)
1993                                         return err;
1994                         }
1995                 }
1996         }
1997
1998         alc_free_kctls(codec); /* no longer needed */
1999
2000         return 0;
2001 }
2002
2003 static int alc_build_jacks(struct hda_codec *codec)
2004 {
2005         struct alc_spec *spec = codec->spec;
2006
2007         if (spec->shared_mic_hp) {
2008                 int err;
2009                 int nid = spec->autocfg.inputs[1].pin;
2010                 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
2011                 if (err < 0)
2012                         return err;
2013                 err = snd_hda_jack_detect_enable(codec, nid, 0);
2014                 if (err < 0)
2015                         return err;
2016         }
2017
2018         return snd_hda_jack_add_kctls(codec, &spec->autocfg);
2019 }
2020
2021 static int alc_build_controls(struct hda_codec *codec)
2022 {
2023         int err = __alc_build_controls(codec);
2024         if (err < 0)
2025                 return err;
2026
2027         err = alc_build_jacks(codec);
2028         if (err < 0)
2029                 return err;
2030         alc_apply_fixup(codec, ALC_FIXUP_ACT_BUILD);
2031         return 0;
2032 }
2033
2034
2035 /*
2036  * Common callbacks
2037  */
2038
2039 static void alc_init_special_input_src(struct hda_codec *codec);
2040 static void alc_auto_init_std(struct hda_codec *codec);
2041
2042 static int alc_init(struct hda_codec *codec)
2043 {
2044         struct alc_spec *spec = codec->spec;
2045
2046         if (spec->init_hook)
2047                 spec->init_hook(codec);
2048
2049         alc_fix_pll(codec);
2050         alc_auto_init_amp(codec, spec->init_amp);
2051
2052         snd_hda_gen_apply_verbs(codec);
2053         alc_init_special_input_src(codec);
2054         alc_auto_init_std(codec);
2055
2056         alc_apply_fixup(codec, ALC_FIXUP_ACT_INIT);
2057
2058         snd_hda_jack_report_sync(codec);
2059
2060         hda_call_check_power_status(codec, 0x01);
2061         return 0;
2062 }
2063
2064 #ifdef CONFIG_SND_HDA_POWER_SAVE
2065 static int alc_check_power_status(struct hda_codec *codec, hda_nid_t nid)
2066 {
2067         struct alc_spec *spec = codec->spec;
2068         return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
2069 }
2070 #endif
2071
2072 /*
2073  * Analog playback callbacks
2074  */
2075 static int alc_playback_pcm_open(struct hda_pcm_stream *hinfo,
2076                                     struct hda_codec *codec,
2077                                     struct snd_pcm_substream *substream)
2078 {
2079         struct alc_spec *spec = codec->spec;
2080         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
2081                                              hinfo);
2082 }
2083
2084 static int alc_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2085                                        struct hda_codec *codec,
2086                                        unsigned int stream_tag,
2087                                        unsigned int format,
2088                                        struct snd_pcm_substream *substream)
2089 {
2090         struct alc_spec *spec = codec->spec;
2091         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
2092                                                 stream_tag, format, substream);
2093 }
2094
2095 static int alc_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2096                                        struct hda_codec *codec,
2097                                        struct snd_pcm_substream *substream)
2098 {
2099         struct alc_spec *spec = codec->spec;
2100         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
2101 }
2102
2103 /*
2104  * Digital out
2105  */
2106 static int alc_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
2107                                         struct hda_codec *codec,
2108                                         struct snd_pcm_substream *substream)
2109 {
2110         struct alc_spec *spec = codec->spec;
2111         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2112 }
2113
2114 static int alc_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2115                                            struct hda_codec *codec,
2116                                            unsigned int stream_tag,
2117                                            unsigned int format,
2118                                            struct snd_pcm_substream *substream)
2119 {
2120         struct alc_spec *spec = codec->spec;
2121         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2122                                              stream_tag, format, substream);
2123 }
2124
2125 static int alc_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2126                                            struct hda_codec *codec,
2127                                            struct snd_pcm_substream *substream)
2128 {
2129         struct alc_spec *spec = codec->spec;
2130         return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
2131 }
2132
2133 static int alc_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
2134                                          struct hda_codec *codec,
2135                                          struct snd_pcm_substream *substream)
2136 {
2137         struct alc_spec *spec = codec->spec;
2138         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2139 }
2140
2141 /*
2142  * Analog capture
2143  */
2144 static int alc_alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2145                                       struct hda_codec *codec,
2146                                       unsigned int stream_tag,
2147                                       unsigned int format,
2148                                       struct snd_pcm_substream *substream)
2149 {
2150         struct alc_spec *spec = codec->spec;
2151
2152         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
2153                                    stream_tag, 0, format);
2154         return 0;
2155 }
2156
2157 static int alc_alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2158                                       struct hda_codec *codec,
2159                                       struct snd_pcm_substream *substream)
2160 {
2161         struct alc_spec *spec = codec->spec;
2162
2163         snd_hda_codec_cleanup_stream(codec,
2164                                      spec->adc_nids[substream->number + 1]);
2165         return 0;
2166 }
2167
2168 /* analog capture with dynamic dual-adc changes */
2169 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2170                                        struct hda_codec *codec,
2171                                        unsigned int stream_tag,
2172                                        unsigned int format,
2173                                        struct snd_pcm_substream *substream)
2174 {
2175         struct alc_spec *spec = codec->spec;
2176         spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
2177         spec->cur_adc_stream_tag = stream_tag;
2178         spec->cur_adc_format = format;
2179         snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
2180         return 0;
2181 }
2182
2183 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2184                                        struct hda_codec *codec,
2185                                        struct snd_pcm_substream *substream)
2186 {
2187         struct alc_spec *spec = codec->spec;
2188         snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
2189         spec->cur_adc = 0;
2190         return 0;
2191 }
2192
2193 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
2194         .substreams = 1,
2195         .channels_min = 2,
2196         .channels_max = 2,
2197         .nid = 0, /* fill later */
2198         .ops = {
2199                 .prepare = dyn_adc_capture_pcm_prepare,
2200                 .cleanup = dyn_adc_capture_pcm_cleanup
2201         },
2202 };
2203
2204 /*
2205  */
2206 static const struct hda_pcm_stream alc_pcm_analog_playback = {
2207         .substreams = 1,
2208         .channels_min = 2,
2209         .channels_max = 8,
2210         /* NID is set in alc_build_pcms */
2211         .ops = {
2212                 .open = alc_playback_pcm_open,
2213                 .prepare = alc_playback_pcm_prepare,
2214                 .cleanup = alc_playback_pcm_cleanup
2215         },
2216 };
2217
2218 static const struct hda_pcm_stream alc_pcm_analog_capture = {
2219         .substreams = 1,
2220         .channels_min = 2,
2221         .channels_max = 2,
2222         /* NID is set in alc_build_pcms */
2223 };
2224
2225 static const struct hda_pcm_stream alc_pcm_analog_alt_playback = {
2226         .substreams = 1,
2227         .channels_min = 2,
2228         .channels_max = 2,
2229         /* NID is set in alc_build_pcms */
2230 };
2231
2232 static const struct hda_pcm_stream alc_pcm_analog_alt_capture = {
2233         .substreams = 2, /* can be overridden */
2234         .channels_min = 2,
2235         .channels_max = 2,
2236         /* NID is set in alc_build_pcms */
2237         .ops = {
2238                 .prepare = alc_alt_capture_pcm_prepare,
2239                 .cleanup = alc_alt_capture_pcm_cleanup
2240         },
2241 };
2242
2243 static const struct hda_pcm_stream alc_pcm_digital_playback = {
2244         .substreams = 1,
2245         .channels_min = 2,
2246         .channels_max = 2,
2247         /* NID is set in alc_build_pcms */
2248         .ops = {
2249                 .open = alc_dig_playback_pcm_open,
2250                 .close = alc_dig_playback_pcm_close,
2251                 .prepare = alc_dig_playback_pcm_prepare,
2252                 .cleanup = alc_dig_playback_pcm_cleanup
2253         },
2254 };
2255
2256 static const struct hda_pcm_stream alc_pcm_digital_capture = {
2257         .substreams = 1,
2258         .channels_min = 2,
2259         .channels_max = 2,
2260         /* NID is set in alc_build_pcms */
2261 };
2262
2263 /* Used by alc_build_pcms to flag that a PCM has no playback stream */
2264 static const struct hda_pcm_stream alc_pcm_null_stream = {
2265         .substreams = 0,
2266         .channels_min = 0,
2267         .channels_max = 0,
2268 };
2269
2270 static int alc_build_pcms(struct hda_codec *codec)
2271 {
2272         struct alc_spec *spec = codec->spec;
2273         struct hda_pcm *info = spec->pcm_rec;
2274         const struct hda_pcm_stream *p;
2275         bool have_multi_adcs;
2276         int i;
2277
2278         codec->num_pcms = 1;
2279         codec->pcm_info = info;
2280
2281         if (spec->no_analog)
2282                 goto skip_analog;
2283
2284         snprintf(spec->stream_name_analog, sizeof(spec->stream_name_analog),
2285                  "%s Analog", codec->chip_name);
2286         info->name = spec->stream_name_analog;
2287
2288         if (spec->multiout.num_dacs > 0) {
2289                 p = spec->stream_analog_playback;
2290                 if (!p)
2291                         p = &alc_pcm_analog_playback;
2292                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2293                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
2294         }
2295         if (spec->adc_nids) {
2296                 p = spec->stream_analog_capture;
2297                 if (!p) {
2298                         if (spec->dyn_adc_switch)
2299                                 p = &dyn_adc_pcm_analog_capture;
2300                         else
2301                                 p = &alc_pcm_analog_capture;
2302                 }
2303                 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2304                 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
2305         }
2306
2307         if (spec->channel_mode) {
2308                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = 0;
2309                 for (i = 0; i < spec->num_channel_mode; i++) {
2310                         if (spec->channel_mode[i].channels > info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max) {
2311                                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->channel_mode[i].channels;
2312                         }
2313                 }
2314         }
2315
2316  skip_analog:
2317         /* SPDIF for stream index #1 */
2318         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
2319                 snprintf(spec->stream_name_digital,
2320                          sizeof(spec->stream_name_digital),
2321                          "%s Digital", codec->chip_name);
2322                 codec->num_pcms = 2;
2323                 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
2324                 info = spec->pcm_rec + 1;
2325                 info->name = spec->stream_name_digital;
2326                 if (spec->dig_out_type)
2327                         info->pcm_type = spec->dig_out_type;
2328                 else
2329                         info->pcm_type = HDA_PCM_TYPE_SPDIF;
2330                 if (spec->multiout.dig_out_nid) {
2331                         p = spec->stream_digital_playback;
2332                         if (!p)
2333                                 p = &alc_pcm_digital_playback;
2334                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2335                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
2336                 }
2337                 if (spec->dig_in_nid) {
2338                         p = spec->stream_digital_capture;
2339                         if (!p)
2340                                 p = &alc_pcm_digital_capture;
2341                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2342                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
2343                 }
2344                 /* FIXME: do we need this for all Realtek codec models? */
2345                 codec->spdif_status_reset = 1;
2346         }
2347
2348         if (spec->no_analog)
2349                 return 0;
2350
2351         /* If the use of more than one ADC is requested for the current
2352          * model, configure a second analog capture-only PCM.
2353          */
2354         have_multi_adcs = (spec->num_adc_nids > 1) &&
2355                 !spec->dyn_adc_switch && !spec->auto_mic &&
2356                 (!spec->input_mux || spec->input_mux->num_items > 1);
2357         /* Additional Analaog capture for index #2 */
2358         if (spec->alt_dac_nid || have_multi_adcs) {
2359                 codec->num_pcms = 3;
2360                 info = spec->pcm_rec + 2;
2361                 info->name = spec->stream_name_analog;
2362                 if (spec->alt_dac_nid) {
2363                         p = spec->stream_analog_alt_playback;
2364                         if (!p)
2365                                 p = &alc_pcm_analog_alt_playback;
2366                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2367                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
2368                                 spec->alt_dac_nid;
2369                 } else {
2370                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
2371                                 alc_pcm_null_stream;
2372                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
2373                 }
2374                 if (have_multi_adcs) {
2375                         p = spec->stream_analog_alt_capture;
2376                         if (!p)
2377                                 p = &alc_pcm_analog_alt_capture;
2378                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2379                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
2380                                 spec->adc_nids[1];
2381                         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
2382                                 spec->num_adc_nids - 1;
2383                 } else {
2384                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
2385                                 alc_pcm_null_stream;
2386                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
2387                 }
2388         }
2389
2390         return 0;
2391 }
2392
2393 static inline void alc_shutup(struct hda_codec *codec)
2394 {
2395         struct alc_spec *spec = codec->spec;
2396
2397         if (spec && spec->shutup)
2398                 spec->shutup(codec);
2399         snd_hda_shutup_pins(codec);
2400 }
2401
2402 static void alc_free_kctls(struct hda_codec *codec)
2403 {
2404         struct alc_spec *spec = codec->spec;
2405
2406         if (spec->kctls.list) {
2407                 struct snd_kcontrol_new *kctl = spec->kctls.list;
2408                 int i;
2409                 for (i = 0; i < spec->kctls.used; i++)
2410                         kfree(kctl[i].name);
2411         }
2412         snd_array_free(&spec->kctls);
2413 }
2414
2415 static void alc_free_bind_ctls(struct hda_codec *codec)
2416 {
2417         struct alc_spec *spec = codec->spec;
2418         if (spec->bind_ctls.list) {
2419                 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
2420                 int i;
2421                 for (i = 0; i < spec->bind_ctls.used; i++)
2422                         kfree(ctl[i]);
2423         }
2424         snd_array_free(&spec->bind_ctls);
2425 }
2426
2427 static void alc_free(struct hda_codec *codec)
2428 {
2429         struct alc_spec *spec = codec->spec;
2430
2431         if (!spec)
2432                 return;
2433
2434         alc_shutup(codec);
2435         alc_free_kctls(codec);
2436         alc_free_bind_ctls(codec);
2437         snd_hda_gen_free(&spec->gen);
2438         kfree(spec);
2439         snd_hda_detach_beep_device(codec);
2440 }
2441
2442 #ifdef CONFIG_SND_HDA_POWER_SAVE
2443 static void alc_power_eapd(struct hda_codec *codec)
2444 {
2445         alc_auto_setup_eapd(codec, false);
2446 }
2447
2448 static int alc_suspend(struct hda_codec *codec)
2449 {
2450         struct alc_spec *spec = codec->spec;
2451         alc_shutup(codec);
2452         if (spec && spec->power_hook)
2453                 spec->power_hook(codec);
2454         return 0;
2455 }
2456 #endif
2457
2458 #ifdef CONFIG_PM
2459 static int alc_resume(struct hda_codec *codec)
2460 {
2461         msleep(150); /* to avoid pop noise */
2462         codec->patch_ops.init(codec);
2463         snd_hda_codec_resume_amp(codec);
2464         snd_hda_codec_resume_cache(codec);
2465         alc_inv_dmic_sync(codec, true);
2466         hda_call_check_power_status(codec, 0x01);
2467         return 0;
2468 }
2469 #endif
2470
2471 /*
2472  */
2473 static const struct hda_codec_ops alc_patch_ops = {
2474         .build_controls = alc_build_controls,
2475         .build_pcms = alc_build_pcms,
2476         .init = alc_init,
2477         .free = alc_free,
2478         .unsol_event = alc_unsol_event,
2479 #ifdef CONFIG_PM
2480         .resume = alc_resume,
2481 #endif
2482 #ifdef CONFIG_SND_HDA_POWER_SAVE
2483         .suspend = alc_suspend,
2484         .check_power_status = alc_check_power_status,
2485 #endif
2486         .reboot_notify = alc_shutup,
2487 };
2488
2489 /* replace the codec chip_name with the given string */
2490 static int alc_codec_rename(struct hda_codec *codec, const char *name)
2491 {
2492         kfree(codec->chip_name);
2493         codec->chip_name = kstrdup(name, GFP_KERNEL);
2494         if (!codec->chip_name) {
2495                 alc_free(codec);
2496                 return -ENOMEM;
2497         }
2498         return 0;
2499 }
2500
2501 /*
2502  * Rename codecs appropriately from COEF value
2503  */
2504 struct alc_codec_rename_table {
2505         unsigned int vendor_id;
2506         unsigned short coef_mask;
2507         unsigned short coef_bits;
2508         const char *name;
2509 };
2510
2511 static struct alc_codec_rename_table rename_tbl[] = {
2512         { 0x10ec0269, 0xfff0, 0x3010, "ALC277" },
2513         { 0x10ec0269, 0xf0f0, 0x2010, "ALC259" },
2514         { 0x10ec0269, 0xf0f0, 0x3010, "ALC258" },
2515         { 0x10ec0269, 0x00f0, 0x0010, "ALC269VB" },
2516         { 0x10ec0269, 0xffff, 0xa023, "ALC259" },
2517         { 0x10ec0269, 0xffff, 0x6023, "ALC281X" },
2518         { 0x10ec0269, 0x00f0, 0x0020, "ALC269VC" },
2519         { 0x10ec0269, 0x00f0, 0x0030, "ALC269VD" },
2520         { 0x10ec0887, 0x00f0, 0x0030, "ALC887-VD" },
2521         { 0x10ec0888, 0x00f0, 0x0030, "ALC888-VD" },
2522         { 0x10ec0888, 0xf0f0, 0x3020, "ALC886" },
2523         { 0x10ec0899, 0x2000, 0x2000, "ALC899" },
2524         { 0x10ec0892, 0xffff, 0x8020, "ALC661" },
2525         { 0x10ec0892, 0xffff, 0x8011, "ALC661" },
2526         { 0x10ec0892, 0xffff, 0x4011, "ALC656" },
2527         { } /* terminator */
2528 };
2529
2530 static int alc_codec_rename_from_preset(struct hda_codec *codec)
2531 {
2532         const struct alc_codec_rename_table *p;
2533
2534         for (p = rename_tbl; p->vendor_id; p++) {
2535                 if (p->vendor_id != codec->vendor_id)
2536                         continue;
2537                 if ((alc_get_coef0(codec) & p->coef_mask) == p->coef_bits)
2538                         return alc_codec_rename(codec, p->name);
2539         }
2540         return 0;
2541 }
2542
2543 /*
2544  * Automatic parse of I/O pins from the BIOS configuration
2545  */
2546
2547 enum {
2548         ALC_CTL_WIDGET_VOL,
2549         ALC_CTL_WIDGET_MUTE,
2550         ALC_CTL_BIND_MUTE,
2551         ALC_CTL_BIND_VOL,
2552         ALC_CTL_BIND_SW,
2553 };
2554 static const struct snd_kcontrol_new alc_control_templates[] = {
2555         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
2556         HDA_CODEC_MUTE(NULL, 0, 0, 0),
2557         HDA_BIND_MUTE(NULL, 0, 0, 0),
2558         HDA_BIND_VOL(NULL, 0),
2559         HDA_BIND_SW(NULL, 0),
2560 };
2561
2562 /* add dynamic controls */
2563 static int add_control(struct alc_spec *spec, int type, const char *name,
2564                        int cidx, unsigned long val)
2565 {
2566         struct snd_kcontrol_new *knew;
2567
2568         knew = alc_kcontrol_new(spec);
2569         if (!knew)
2570                 return -ENOMEM;
2571         *knew = alc_control_templates[type];
2572         knew->name = kstrdup(name, GFP_KERNEL);
2573         if (!knew->name)
2574                 return -ENOMEM;
2575         knew->index = cidx;
2576         if (get_amp_nid_(val))
2577                 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2578         knew->private_value = val;
2579         return 0;
2580 }
2581
2582 static int add_control_with_pfx(struct alc_spec *spec, int type,
2583                                 const char *pfx, const char *dir,
2584                                 const char *sfx, int cidx, unsigned long val)
2585 {
2586         char name[32];
2587         snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
2588         return add_control(spec, type, name, cidx, val);
2589 }
2590
2591 #define add_pb_vol_ctrl(spec, type, pfx, val)                   \
2592         add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
2593 #define add_pb_sw_ctrl(spec, type, pfx, val)                    \
2594         add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
2595 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val)                   \
2596         add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
2597 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val)                    \
2598         add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
2599
2600 static const char * const channel_name[4] = {
2601         "Front", "Surround", "CLFE", "Side"
2602 };
2603
2604 static const char *alc_get_line_out_pfx(struct alc_spec *spec, int ch,
2605                                         bool can_be_master, int *index)
2606 {
2607         struct auto_pin_cfg *cfg = &spec->autocfg;
2608
2609         *index = 0;
2610         if (cfg->line_outs == 1 && !spec->multi_ios &&
2611             !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
2612                 return "Master";
2613
2614         switch (cfg->line_out_type) {
2615         case AUTO_PIN_SPEAKER_OUT:
2616                 if (cfg->line_outs == 1)
2617                         return "Speaker";
2618                 if (cfg->line_outs == 2)
2619                         return ch ? "Bass Speaker" : "Speaker";
2620                 break;
2621         case AUTO_PIN_HP_OUT:
2622                 /* for multi-io case, only the primary out */
2623                 if (ch && spec->multi_ios)
2624                         break;
2625                 *index = ch;
2626                 return "Headphone";
2627         default:
2628                 if (cfg->line_outs == 1 && !spec->multi_ios)
2629                         return "PCM";
2630                 break;
2631         }
2632         if (ch >= ARRAY_SIZE(channel_name)) {
2633                 snd_BUG();
2634                 return "PCM";
2635         }
2636
2637         return channel_name[ch];
2638 }
2639
2640 #ifdef CONFIG_SND_HDA_POWER_SAVE
2641 /* add the powersave loopback-list entry */
2642 static void add_loopback_list(struct alc_spec *spec, hda_nid_t mix, int idx)
2643 {
2644         struct hda_amp_list *list;
2645
2646         if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2647                 return;
2648         list = spec->loopback_list + spec->num_loopbacks;
2649         list->nid = mix;
2650         list->dir = HDA_INPUT;
2651         list->idx = idx;
2652         spec->num_loopbacks++;
2653         spec->loopback.amplist = spec->loopback_list;
2654 }
2655 #else
2656 #define add_loopback_list(spec, mix, idx) /* NOP */
2657 #endif
2658
2659 /* create input playback/capture controls for the given pin */
2660 static int new_analog_input(struct alc_spec *spec, hda_nid_t pin,
2661                             const char *ctlname, int ctlidx,
2662                             int idx, hda_nid_t mix_nid)
2663 {
2664         int err;
2665
2666         err = __add_pb_vol_ctrl(spec, ALC_CTL_WIDGET_VOL, ctlname, ctlidx,
2667                           HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
2668         if (err < 0)
2669                 return err;
2670         err = __add_pb_sw_ctrl(spec, ALC_CTL_WIDGET_MUTE, ctlname, ctlidx,
2671                           HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
2672         if (err < 0)
2673                 return err;
2674         add_loopback_list(spec, mix_nid, idx);
2675         return 0;
2676 }
2677
2678 static int alc_is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2679 {
2680         unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2681         return (pincap & AC_PINCAP_IN) != 0;
2682 }
2683
2684 /* Parse the codec tree and retrieve ADCs and corresponding capsrc MUXs */
2685 static int alc_auto_fill_adc_caps(struct hda_codec *codec)
2686 {
2687         struct alc_spec *spec = codec->spec;
2688         hda_nid_t nid;
2689         hda_nid_t *adc_nids = spec->private_adc_nids;
2690         hda_nid_t *cap_nids = spec->private_capsrc_nids;
2691         int max_nums = ARRAY_SIZE(spec->private_adc_nids);
2692         int i, nums = 0;
2693
2694         nid = codec->start_nid;
2695         for (i = 0; i < codec->num_nodes; i++, nid++) {
2696                 hda_nid_t src;
2697                 unsigned int caps = get_wcaps(codec, nid);
2698                 int type = get_wcaps_type(caps);
2699
2700                 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2701                         continue;
2702                 adc_nids[nums] = nid;
2703                 cap_nids[nums] = nid;
2704                 src = nid;
2705                 for (;;) {
2706                         int n;
2707                         type = get_wcaps_type(get_wcaps(codec, src));
2708                         if (type == AC_WID_PIN)
2709                                 break;
2710                         if (type == AC_WID_AUD_SEL) {
2711                                 cap_nids[nums] = src;
2712                                 break;
2713                         }
2714                         n = snd_hda_get_num_conns(codec, src);
2715                         if (n > 1) {
2716                                 cap_nids[nums] = src;
2717                                 break;
2718                         } else if (n != 1)
2719                                 break;
2720                         if (snd_hda_get_connections(codec, src, &src, 1) != 1)
2721                                 break;
2722                 }
2723                 if (++nums >= max_nums)
2724                         break;
2725         }
2726         spec->adc_nids = spec->private_adc_nids;
2727         spec->capsrc_nids = spec->private_capsrc_nids;
2728         spec->num_adc_nids = nums;
2729         return nums;
2730 }
2731
2732 /* create playback/capture controls for input pins */
2733 static int alc_auto_create_input_ctls(struct hda_codec *codec)
2734 {
2735         struct alc_spec *spec = codec->spec;
2736         const struct auto_pin_cfg *cfg = &spec->autocfg;
2737         hda_nid_t mixer = spec->mixer_nid;
2738         struct hda_input_mux *imux = &spec->private_imux[0];
2739         int num_adcs;
2740         int i, c, err, idx, type_idx = 0;
2741         const char *prev_label = NULL;
2742
2743         num_adcs = alc_auto_fill_adc_caps(codec);
2744         if (num_adcs < 0)
2745                 return 0;
2746
2747         for (i = 0; i < cfg->num_inputs; i++) {
2748                 hda_nid_t pin;
2749                 const char *label;
2750
2751                 pin = cfg->inputs[i].pin;
2752                 if (!alc_is_input_pin(codec, pin))
2753                         continue;
2754
2755                 label = hda_get_autocfg_input_label(codec, cfg, i);
2756                 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2757                         label = "Headphone Mic";
2758                 if (prev_label && !strcmp(label, prev_label))
2759                         type_idx++;
2760                 else
2761                         type_idx = 0;
2762                 prev_label = label;
2763
2764                 if (mixer) {
2765                         idx = get_connection_index(codec, mixer, pin);
2766                         if (idx >= 0) {
2767                                 err = new_analog_input(spec, pin,
2768                                                        label, type_idx,
2769                                                        idx, mixer);
2770                                 if (err < 0)
2771                                         return err;
2772                         }
2773                 }
2774
2775                 for (c = 0; c < num_adcs; c++) {
2776                         hda_nid_t cap = get_capsrc(spec, c);
2777                         idx = get_connection_index(codec, cap, pin);
2778                         if (idx >= 0) {
2779                                 spec->imux_pins[imux->num_items] = pin;
2780                                 snd_hda_add_imux_item(imux, label, idx, NULL);
2781                                 break;
2782                         }
2783                 }
2784         }
2785
2786         spec->num_mux_defs = 1;
2787         spec->input_mux = imux;
2788
2789         return 0;
2790 }
2791
2792 /* create a shared input with the headphone out */
2793 static int alc_auto_create_shared_input(struct hda_codec *codec)
2794 {
2795         struct alc_spec *spec = codec->spec;
2796         struct auto_pin_cfg *cfg = &spec->autocfg;
2797         unsigned int defcfg;
2798         hda_nid_t nid;
2799
2800         /* only one internal input pin? */
2801         if (cfg->num_inputs != 1)
2802                 return 0;
2803         defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2804         if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2805                 return 0;
2806
2807         if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2808                 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2809         else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2810                 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2811         else
2812                 return 0; /* both not available */
2813
2814         if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2815                 return 0; /* no input */
2816
2817         cfg->inputs[1].pin = nid;
2818         cfg->inputs[1].type = AUTO_PIN_MIC;
2819         cfg->num_inputs = 2;
2820         spec->shared_mic_hp = 1;
2821         snd_printdd("realtek: Enable shared I/O jack on NID 0x%x\n", nid);
2822         return 0;
2823 }
2824
2825 static void alc_set_pin_output(struct hda_codec *codec, hda_nid_t nid,
2826                                unsigned int pin_type)
2827 {
2828         snd_hda_set_pin_ctl(codec, nid, pin_type);
2829         /* unmute pin */
2830         if (nid_has_mute(codec, nid, HDA_OUTPUT))
2831                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2832                             AMP_OUT_UNMUTE);
2833 }
2834
2835 static int get_pin_type(int line_out_type)
2836 {
2837         if (line_out_type == AUTO_PIN_HP_OUT)
2838                 return PIN_HP;
2839         else
2840                 return PIN_OUT;
2841 }
2842
2843 static void alc_auto_init_analog_input(struct hda_codec *codec)
2844 {
2845         struct alc_spec *spec = codec->spec;
2846         struct auto_pin_cfg *cfg = &spec->autocfg;
2847         int i;
2848
2849         for (i = 0; i < cfg->num_inputs; i++) {
2850                 hda_nid_t nid = cfg->inputs[i].pin;
2851                 if (alc_is_input_pin(codec, nid)) {
2852                         alc_set_input_pin(codec, nid, cfg->inputs[i].type);
2853                         if (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP)
2854                                 snd_hda_codec_write(codec, nid, 0,
2855                                                     AC_VERB_SET_AMP_GAIN_MUTE,
2856                                                     AMP_OUT_MUTE);
2857                 }
2858         }
2859
2860         /* mute all loopback inputs */
2861         if (spec->mixer_nid) {
2862                 int nums = snd_hda_get_num_conns(codec, spec->mixer_nid);
2863                 for (i = 0; i < nums; i++)
2864                         snd_hda_codec_write(codec, spec->mixer_nid, 0,
2865                                             AC_VERB_SET_AMP_GAIN_MUTE,
2866                                             AMP_IN_MUTE(i));
2867         }
2868 }
2869
2870 /* convert from MIX nid to DAC */
2871 static hda_nid_t alc_auto_mix_to_dac(struct hda_codec *codec, hda_nid_t nid)
2872 {
2873         hda_nid_t list[5];
2874         int i, num;
2875
2876         if (get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_AUD_OUT)
2877                 return nid;
2878         num = snd_hda_get_connections(codec, nid, list, ARRAY_SIZE(list));
2879         for (i = 0; i < num; i++) {
2880                 if (get_wcaps_type(get_wcaps(codec, list[i])) == AC_WID_AUD_OUT)
2881                         return list[i];
2882         }
2883         return 0;
2884 }
2885
2886 /* go down to the selector widget before the mixer */
2887 static hda_nid_t alc_go_down_to_selector(struct hda_codec *codec, hda_nid_t pin)
2888 {
2889         hda_nid_t srcs[5];
2890         int num = snd_hda_get_connections(codec, pin, srcs,
2891                                           ARRAY_SIZE(srcs));
2892         if (num != 1 ||
2893             get_wcaps_type(get_wcaps(codec, srcs[0])) != AC_WID_AUD_SEL)
2894                 return pin;
2895         return srcs[0];
2896 }
2897
2898 /* get MIX nid connected to the given pin targeted to DAC */
2899 static hda_nid_t alc_auto_dac_to_mix(struct hda_codec *codec, hda_nid_t pin,
2900                                    hda_nid_t dac)
2901 {
2902         hda_nid_t mix[5];
2903         int i, num;
2904
2905         pin = alc_go_down_to_selector(codec, pin);
2906         num = snd_hda_get_connections(codec, pin, mix, ARRAY_SIZE(mix));
2907         for (i = 0; i < num; i++) {
2908                 if (alc_auto_mix_to_dac(codec, mix[i]) == dac)
2909                         return mix[i];
2910         }
2911         return 0;
2912 }
2913
2914 /* select the connection from pin to DAC if needed */
2915 static int alc_auto_select_dac(struct hda_codec *codec, hda_nid_t pin,
2916                                hda_nid_t dac)
2917 {
2918         hda_nid_t mix[5];
2919         int i, num;
2920
2921         pin = alc_go_down_to_selector(codec, pin);
2922         num = snd_hda_get_connections(codec, pin, mix, ARRAY_SIZE(mix));
2923         if (num < 2)
2924                 return 0;
2925         for (i = 0; i < num; i++) {
2926                 if (alc_auto_mix_to_dac(codec, mix[i]) == dac) {
2927                         snd_hda_codec_update_cache(codec, pin, 0,
2928                                                    AC_VERB_SET_CONNECT_SEL, i);
2929                         return 0;
2930                 }
2931         }
2932         return 0;
2933 }
2934
2935 static bool alc_is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
2936 {
2937         struct alc_spec *spec = codec->spec;
2938         int i;
2939         if (found_in_nid_list(nid, spec->multiout.dac_nids,
2940                               ARRAY_SIZE(spec->private_dac_nids)) ||
2941             found_in_nid_list(nid, spec->multiout.hp_out_nid,
2942                               ARRAY_SIZE(spec->multiout.hp_out_nid)) ||
2943             found_in_nid_list(nid, spec->multiout.extra_out_nid,
2944                               ARRAY_SIZE(spec->multiout.extra_out_nid)))
2945                 return true;
2946         for (i = 0; i < spec->multi_ios; i++) {
2947                 if (spec->multi_io[i].dac == nid)
2948                         return true;
2949         }
2950         return false;
2951 }
2952
2953 /* look for an empty DAC slot */
2954 static hda_nid_t alc_auto_look_for_dac(struct hda_codec *codec, hda_nid_t pin)
2955 {
2956         hda_nid_t srcs[5];
2957         int i, num;
2958
2959         pin = alc_go_down_to_selector(codec, pin);
2960         num = snd_hda_get_connections(codec, pin, srcs, ARRAY_SIZE(srcs));
2961         for (i = 0; i < num; i++) {
2962                 hda_nid_t nid = alc_auto_mix_to_dac(codec, srcs[i]);
2963                 if (!nid)
2964                         continue;
2965                 if (!alc_is_dac_already_used(codec, nid))
2966                         return nid;
2967         }
2968         return 0;
2969 }
2970
2971 /* check whether the DAC is reachable from the pin */
2972 static bool alc_auto_is_dac_reachable(struct hda_codec *codec,
2973                                       hda_nid_t pin, hda_nid_t dac)
2974 {
2975         hda_nid_t srcs[5];
2976         int i, num;
2977
2978         if (!pin || !dac)
2979                 return false;
2980         pin = alc_go_down_to_selector(codec, pin);
2981         num = snd_hda_get_connections(codec, pin, srcs, ARRAY_SIZE(srcs));
2982         for (i = 0; i < num; i++) {
2983                 hda_nid_t nid = alc_auto_mix_to_dac(codec, srcs[i]);
2984                 if (nid == dac)
2985                         return true;
2986         }
2987         return false;
2988 }
2989
2990 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
2991 {
2992         struct alc_spec *spec = codec->spec;
2993         hda_nid_t sel = alc_go_down_to_selector(codec, pin);
2994         hda_nid_t nid, nid_found, srcs[5];
2995         int i, num = snd_hda_get_connections(codec, sel, srcs,
2996                                           ARRAY_SIZE(srcs));
2997         if (num == 1)
2998                 return alc_auto_look_for_dac(codec, pin);
2999         nid_found = 0;
3000         for (i = 0; i < num; i++) {
3001                 if (srcs[i] == spec->mixer_nid)
3002                         continue;
3003                 nid = alc_auto_mix_to_dac(codec, srcs[i]);
3004                 if (nid && !alc_is_dac_already_used(codec, nid)) {
3005                         if (nid_found)
3006                                 return 0;
3007                         nid_found = nid;
3008                 }
3009         }
3010         return nid_found;
3011 }
3012
3013 /* mark up volume and mute control NIDs: used during badness parsing and
3014  * at creating actual controls
3015  */
3016 static inline unsigned int get_ctl_pos(unsigned int data)
3017 {
3018         hda_nid_t nid = get_amp_nid_(data);
3019         unsigned int dir;
3020         if (snd_BUG_ON(nid >= MAX_VOL_NIDS))
3021                 return 0;
3022         dir = get_amp_direction_(data);
3023         return (nid << 1) | dir;
3024 }
3025
3026 #define is_ctl_used(bits, data) \
3027         test_bit(get_ctl_pos(data), bits)
3028 #define mark_ctl_usage(bits, data) \
3029         set_bit(get_ctl_pos(data), bits)
3030
3031 static void clear_vol_marks(struct hda_codec *codec)
3032 {
3033         struct alc_spec *spec = codec->spec;
3034         memset(spec->vol_ctls, 0, sizeof(spec->vol_ctls));
3035         memset(spec->sw_ctls, 0, sizeof(spec->sw_ctls));
3036 }
3037
3038 /* badness definition */
3039 enum {
3040         /* No primary DAC is found for the main output */
3041         BAD_NO_PRIMARY_DAC = 0x10000,
3042         /* No DAC is found for the extra output */
3043         BAD_NO_DAC = 0x4000,
3044         /* No possible multi-ios */
3045         BAD_MULTI_IO = 0x103,
3046         /* No individual DAC for extra output */
3047         BAD_NO_EXTRA_DAC = 0x102,
3048         /* No individual DAC for extra surrounds */
3049         BAD_NO_EXTRA_SURR_DAC = 0x101,
3050         /* Primary DAC shared with main surrounds */
3051         BAD_SHARED_SURROUND = 0x100,
3052         /* Primary DAC shared with main CLFE */
3053         BAD_SHARED_CLFE = 0x10,
3054         /* Primary DAC shared with extra surrounds */
3055         BAD_SHARED_EXTRA_SURROUND = 0x10,
3056         /* Volume widget is shared */
3057         BAD_SHARED_VOL = 0x10,
3058 };
3059
3060 static hda_nid_t alc_look_for_out_mute_nid(struct hda_codec *codec,
3061                                            hda_nid_t pin, hda_nid_t dac);
3062 static hda_nid_t alc_look_for_out_vol_nid(struct hda_codec *codec,
3063                                           hda_nid_t pin, hda_nid_t dac);
3064
3065 static int eval_shared_vol_badness(struct hda_codec *codec, hda_nid_t pin,
3066                                    hda_nid_t dac)
3067 {
3068         struct alc_spec *spec = codec->spec;
3069         hda_nid_t nid;
3070         unsigned int val;
3071         int badness = 0;
3072
3073         nid = alc_look_for_out_vol_nid(codec, pin, dac);
3074         if (nid) {
3075                 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3076                 if (is_ctl_used(spec->vol_ctls, nid))
3077                         badness += BAD_SHARED_VOL;
3078                 else
3079                         mark_ctl_usage(spec->vol_ctls, val);
3080         } else
3081                 badness += BAD_SHARED_VOL;
3082         nid = alc_look_for_out_mute_nid(codec, pin, dac);
3083         if (nid) {
3084                 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
3085                 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT)
3086                         val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3087                 else
3088                         val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
3089                 if (is_ctl_used(spec->sw_ctls, val))
3090                         badness += BAD_SHARED_VOL;
3091                 else
3092                         mark_ctl_usage(spec->sw_ctls, val);
3093         } else
3094                 badness += BAD_SHARED_VOL;
3095         return badness;
3096 }
3097
3098 struct badness_table {
3099         int no_primary_dac;     /* no primary DAC */
3100         int no_dac;             /* no secondary DACs */
3101         int shared_primary;     /* primary DAC is shared with main output */
3102         int shared_surr;        /* secondary DAC shared with main or primary */
3103         int shared_clfe;        /* third DAC shared with main or primary */
3104         int shared_surr_main;   /* secondary DAC sahred with main/DAC0 */
3105 };
3106
3107 static struct badness_table main_out_badness = {
3108         .no_primary_dac = BAD_NO_PRIMARY_DAC,
3109         .no_dac = BAD_NO_DAC,
3110         .shared_primary = BAD_NO_PRIMARY_DAC,
3111         .shared_surr = BAD_SHARED_SURROUND,
3112         .shared_clfe = BAD_SHARED_CLFE,
3113         .shared_surr_main = BAD_SHARED_SURROUND,
3114 };
3115
3116 static struct badness_table extra_out_badness = {
3117         .no_primary_dac = BAD_NO_DAC,
3118         .no_dac = BAD_NO_DAC,
3119         .shared_primary = BAD_NO_EXTRA_DAC,
3120         .shared_surr = BAD_SHARED_EXTRA_SURROUND,
3121         .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
3122         .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
3123 };
3124
3125 /* try to assign DACs to pins and return the resultant badness */
3126 static int alc_auto_fill_dacs(struct hda_codec *codec, int num_outs,
3127                               const hda_nid_t *pins, hda_nid_t *dacs,
3128                               const struct badness_table *bad)
3129 {
3130         struct alc_spec *spec = codec->spec;
3131         struct auto_pin_cfg *cfg = &spec->autocfg;
3132         int i, j;
3133         int badness = 0;
3134         hda_nid_t dac;
3135
3136         if (!num_outs)
3137                 return 0;
3138
3139         for (i = 0; i < num_outs; i++) {
3140                 hda_nid_t pin = pins[i];
3141                 if (!dacs[i])
3142                         dacs[i] = alc_auto_look_for_dac(codec, pin);
3143                 if (!dacs[i] && !i) {
3144                         for (j = 1; j < num_outs; j++) {
3145                                 if (alc_auto_is_dac_reachable(codec, pin, dacs[j])) {
3146                                         dacs[0] = dacs[j];
3147                                         dacs[j] = 0;
3148                                         break;
3149                                 }
3150                         }
3151                 }
3152                 dac = dacs[i];
3153                 if (!dac) {
3154                         if (alc_auto_is_dac_reachable(codec, pin, dacs[0]))
3155                                 dac = dacs[0];
3156                         else if (cfg->line_outs > i &&
3157                                  alc_auto_is_dac_reachable(codec, pin,
3158                                         spec->private_dac_nids[i]))
3159                                 dac = spec->private_dac_nids[i];
3160                         if (dac) {
3161                                 if (!i)
3162                                         badness += bad->shared_primary;
3163                                 else if (i == 1)
3164                                         badness += bad->shared_surr;
3165                                 else
3166                                         badness += bad->shared_clfe;
3167                         } else if (alc_auto_is_dac_reachable(codec, pin,
3168                                         spec->private_dac_nids[0])) {
3169                                 dac = spec->private_dac_nids[0];
3170                                 badness += bad->shared_surr_main;
3171                         } else if (!i)
3172                                 badness += bad->no_primary_dac;
3173                         else
3174                                 badness += bad->no_dac;
3175                 }
3176                 if (dac)
3177                         badness += eval_shared_vol_badness(codec, pin, dac);
3178         }
3179
3180         return badness;
3181 }
3182
3183 static int alc_auto_fill_multi_ios(struct hda_codec *codec,
3184                                    hda_nid_t reference_pin,
3185                                    bool hardwired, int offset);
3186
3187 static bool alc_map_singles(struct hda_codec *codec, int outs,
3188                             const hda_nid_t *pins, hda_nid_t *dacs)
3189 {
3190         int i;
3191         bool found = false;
3192         for (i = 0; i < outs; i++) {
3193                 if (dacs[i])
3194                         continue;
3195                 dacs[i] = get_dac_if_single(codec, pins[i]);
3196                 if (dacs[i])
3197                         found = true;
3198         }
3199         return found;
3200 }
3201
3202 /* fill in the dac_nids table from the parsed pin configuration */
3203 static int fill_and_eval_dacs(struct hda_codec *codec,
3204                               bool fill_hardwired,
3205                               bool fill_mio_first)
3206 {
3207         struct alc_spec *spec = codec->spec;
3208         struct auto_pin_cfg *cfg = &spec->autocfg;
3209         int i, err, badness;
3210
3211         /* set num_dacs once to full for alc_auto_look_for_dac() */
3212         spec->multiout.num_dacs = cfg->line_outs;
3213         spec->multiout.dac_nids = spec->private_dac_nids;
3214         memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
3215         memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
3216         memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
3217         spec->multi_ios = 0;
3218         clear_vol_marks(codec);
3219         badness = 0;
3220
3221         /* fill hard-wired DACs first */
3222         if (fill_hardwired) {
3223                 bool mapped;
3224                 do {
3225                         mapped = alc_map_singles(codec, cfg->line_outs,
3226                                                  cfg->line_out_pins,
3227                                                  spec->private_dac_nids);
3228                         mapped |= alc_map_singles(codec, cfg->hp_outs,
3229                                                   cfg->hp_pins,
3230                                                   spec->multiout.hp_out_nid);
3231                         mapped |= alc_map_singles(codec, cfg->speaker_outs,
3232                                                   cfg->speaker_pins,
3233                                                   spec->multiout.extra_out_nid);
3234                         if (fill_mio_first && cfg->line_outs == 1 &&
3235                             cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3236                                 err = alc_auto_fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
3237                                 if (!err)
3238                                         mapped = true;
3239                         }
3240                 } while (mapped);
3241         }
3242
3243         badness += alc_auto_fill_dacs(codec, cfg->line_outs, cfg->line_out_pins,
3244                                       spec->private_dac_nids,
3245                                       &main_out_badness);
3246
3247         /* re-count num_dacs and squash invalid entries */
3248         spec->multiout.num_dacs = 0;
3249         for (i = 0; i < cfg->line_outs; i++) {
3250                 if (spec->private_dac_nids[i])
3251                         spec->multiout.num_dacs++;
3252                 else {
3253                         memmove(spec->private_dac_nids + i,
3254                                 spec->private_dac_nids + i + 1,
3255                                 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
3256                         spec->private_dac_nids[cfg->line_outs - 1] = 0;
3257                 }
3258         }
3259
3260         if (fill_mio_first &&
3261             cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3262                 /* try to fill multi-io first */
3263                 err = alc_auto_fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
3264                 if (err < 0)
3265                         return err;
3266                 /* we don't count badness at this stage yet */
3267         }
3268
3269         if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3270                 err = alc_auto_fill_dacs(codec, cfg->hp_outs, cfg->hp_pins,
3271                                          spec->multiout.hp_out_nid,
3272                                          &extra_out_badness);
3273                 if (err < 0)
3274                         return err;
3275                 badness += err;
3276         }
3277         if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3278                 err = alc_auto_fill_dacs(codec, cfg->speaker_outs,
3279                                          cfg->speaker_pins,
3280                                          spec->multiout.extra_out_nid,
3281                                          &extra_out_badness);
3282                 if (err < 0)
3283                         return err;
3284                 badness += err;
3285         }
3286         if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3287                 err = alc_auto_fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
3288                 if (err < 0)
3289                         return err;
3290                 badness += err;
3291         }
3292         if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3293                 /* try multi-ios with HP + inputs */
3294                 int offset = 0;
3295                 if (cfg->line_outs >= 3)
3296                         offset = 1;
3297                 err = alc_auto_fill_multi_ios(codec, cfg->hp_pins[0], false,
3298                                               offset);
3299                 if (err < 0)
3300                         return err;
3301                 badness += err;
3302         }
3303
3304         if (spec->multi_ios == 2) {
3305                 for (i = 0; i < 2; i++)
3306                         spec->private_dac_nids[spec->multiout.num_dacs++] =
3307                                 spec->multi_io[i].dac;
3308                 spec->ext_channel_count = 2;
3309         } else if (spec->multi_ios) {
3310                 spec->multi_ios = 0;
3311                 badness += BAD_MULTI_IO;
3312         }
3313
3314         return badness;
3315 }
3316
3317 #define DEBUG_BADNESS
3318
3319 #ifdef DEBUG_BADNESS
3320 #define debug_badness   snd_printdd
3321 #else
3322 #define debug_badness(...)
3323 #endif
3324
3325 static void debug_show_configs(struct alc_spec *spec, struct auto_pin_cfg *cfg)
3326 {
3327         debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
3328                       cfg->line_out_pins[0], cfg->line_out_pins[1],
3329                       cfg->line_out_pins[2], cfg->line_out_pins[2],
3330                       spec->multiout.dac_nids[0],
3331                       spec->multiout.dac_nids[1],
3332                       spec->multiout.dac_nids[2],
3333                       spec->multiout.dac_nids[3]);
3334         if (spec->multi_ios > 0)
3335                 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
3336                               spec->multi_ios,
3337                               spec->multi_io[0].pin, spec->multi_io[1].pin,
3338                               spec->multi_io[0].dac, spec->multi_io[1].dac);
3339         debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
3340                       cfg->hp_pins[0], cfg->hp_pins[1],
3341                       cfg->hp_pins[2], cfg->hp_pins[2],
3342                       spec->multiout.hp_out_nid[0],
3343                       spec->multiout.hp_out_nid[1],
3344                       spec->multiout.hp_out_nid[2],
3345                       spec->multiout.hp_out_nid[3]);
3346         debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
3347                       cfg->speaker_pins[0], cfg->speaker_pins[1],
3348                       cfg->speaker_pins[2], cfg->speaker_pins[3],
3349                       spec->multiout.extra_out_nid[0],
3350                       spec->multiout.extra_out_nid[1],
3351                       spec->multiout.extra_out_nid[2],
3352                       spec->multiout.extra_out_nid[3]);
3353 }
3354
3355 static int alc_auto_fill_dac_nids(struct hda_codec *codec)
3356 {
3357         struct alc_spec *spec = codec->spec;
3358         struct auto_pin_cfg *cfg = &spec->autocfg;
3359         struct auto_pin_cfg *best_cfg;
3360         int best_badness = INT_MAX;
3361         int badness;
3362         bool fill_hardwired = true, fill_mio_first = true;
3363         bool best_wired = true, best_mio = true;
3364         bool hp_spk_swapped = false;
3365
3366         best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
3367         if (!best_cfg)
3368                 return -ENOMEM;
3369         *best_cfg = *cfg;
3370
3371         for (;;) {
3372                 badness = fill_and_eval_dacs(codec, fill_hardwired,
3373                                              fill_mio_first);
3374                 if (badness < 0) {
3375                         kfree(best_cfg);
3376                         return badness;
3377                 }
3378                 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
3379                               cfg->line_out_type, fill_hardwired, fill_mio_first,
3380                               badness);
3381                 debug_show_configs(spec, cfg);
3382                 if (badness < best_badness) {
3383                         best_badness = badness;
3384                         *best_cfg = *cfg;
3385                         best_wired = fill_hardwired;
3386                         best_mio = fill_mio_first;
3387                 }
3388                 if (!badness)
3389                         break;
3390                 fill_mio_first = !fill_mio_first;
3391                 if (!fill_mio_first)
3392                         continue;
3393                 fill_hardwired = !fill_hardwired;
3394                 if (!fill_hardwired)
3395                         continue;
3396                 if (hp_spk_swapped)
3397                         break;
3398                 hp_spk_swapped = true;
3399                 if (cfg->speaker_outs > 0 &&
3400                     cfg->line_out_type == AUTO_PIN_HP_OUT) {
3401                         cfg->hp_outs = cfg->line_outs;
3402                         memcpy(cfg->hp_pins, cfg->line_out_pins,
3403                                sizeof(cfg->hp_pins));
3404                         cfg->line_outs = cfg->speaker_outs;
3405                         memcpy(cfg->line_out_pins, cfg->speaker_pins,
3406                                sizeof(cfg->speaker_pins));
3407                         cfg->speaker_outs = 0;
3408                         memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3409                         cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3410                         fill_hardwired = true;
3411                         continue;
3412                 }
3413                 if (cfg->hp_outs > 0 &&
3414                     cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3415                         cfg->speaker_outs = cfg->line_outs;
3416                         memcpy(cfg->speaker_pins, cfg->line_out_pins,
3417                                sizeof(cfg->speaker_pins));
3418                         cfg->line_outs = cfg->hp_outs;
3419                         memcpy(cfg->line_out_pins, cfg->hp_pins,
3420                                sizeof(cfg->hp_pins));
3421                         cfg->hp_outs = 0;
3422                         memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3423                         cfg->line_out_type = AUTO_PIN_HP_OUT;
3424                         fill_hardwired = true;
3425                         continue;
3426                 }
3427                 break;
3428         }
3429
3430         if (badness) {
3431                 *cfg = *best_cfg;
3432                 fill_and_eval_dacs(codec, best_wired, best_mio);
3433         }
3434         debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
3435                       cfg->line_out_type, best_wired, best_mio);
3436         debug_show_configs(spec, cfg);
3437
3438         if (cfg->line_out_pins[0])
3439                 spec->vmaster_nid =
3440                         alc_look_for_out_vol_nid(codec, cfg->line_out_pins[0],
3441                                                  spec->multiout.dac_nids[0]);
3442
3443         /* clear the bitmap flags for creating controls */
3444         clear_vol_marks(codec);
3445         kfree(best_cfg);
3446         return 0;
3447 }
3448
3449 static int alc_auto_add_vol_ctl(struct hda_codec *codec,
3450                               const char *pfx, int cidx,
3451                               hda_nid_t nid, unsigned int chs)
3452 {
3453         struct alc_spec *spec = codec->spec;
3454         unsigned int val;
3455         if (!nid)
3456                 return 0;
3457         val = HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT);
3458         if (is_ctl_used(spec->vol_ctls, val) && chs != 2) /* exclude LFE */
3459                 return 0;
3460         mark_ctl_usage(spec->vol_ctls, val);
3461         return __add_pb_vol_ctrl(codec->spec, ALC_CTL_WIDGET_VOL, pfx, cidx,
3462                                  val);
3463 }
3464
3465 static int alc_auto_add_stereo_vol(struct hda_codec *codec,
3466                                    const char *pfx, int cidx,
3467                                    hda_nid_t nid)
3468 {
3469         int chs = 1;
3470         if (get_wcaps(codec, nid) & AC_WCAP_STEREO)
3471                 chs = 3;
3472         return alc_auto_add_vol_ctl(codec, pfx, cidx, nid, chs);
3473 }
3474
3475 /* create a mute-switch for the given mixer widget;
3476  * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
3477  */
3478 static int alc_auto_add_sw_ctl(struct hda_codec *codec,
3479                              const char *pfx, int cidx,
3480                              hda_nid_t nid, unsigned int chs)
3481 {
3482         struct alc_spec *spec = codec->spec;
3483         int wid_type;
3484         int type;
3485         unsigned long val;
3486         if (!nid)
3487                 return 0;
3488         wid_type = get_wcaps_type(get_wcaps(codec, nid));
3489         if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT) {
3490                 type = ALC_CTL_WIDGET_MUTE;
3491                 val = HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT);
3492         } else if (snd_hda_get_num_conns(codec, nid) == 1) {
3493                 type = ALC_CTL_WIDGET_MUTE;
3494                 val = HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_INPUT);
3495         } else {
3496                 type = ALC_CTL_BIND_MUTE;
3497                 val = HDA_COMPOSE_AMP_VAL(nid, chs, 2, HDA_INPUT);
3498         }
3499         if (is_ctl_used(spec->sw_ctls, val) && chs != 2) /* exclude LFE */
3500                 return 0;
3501         mark_ctl_usage(spec->sw_ctls, val);
3502         return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
3503 }
3504
3505 static int alc_auto_add_stereo_sw(struct hda_codec *codec, const char *pfx,
3506                                   int cidx, hda_nid_t nid)
3507 {
3508         int chs = 1;
3509         if (get_wcaps(codec, nid) & AC_WCAP_STEREO)
3510                 chs = 3;
3511         return alc_auto_add_sw_ctl(codec, pfx, cidx, nid, chs);
3512 }
3513
3514 static hda_nid_t alc_look_for_out_mute_nid(struct hda_codec *codec,
3515                                            hda_nid_t pin, hda_nid_t dac)
3516 {
3517         hda_nid_t mix = alc_auto_dac_to_mix(codec, pin, dac);
3518         if (nid_has_mute(codec, pin, HDA_OUTPUT))
3519                 return pin;
3520         else if (mix && nid_has_mute(codec, mix, HDA_INPUT))
3521                 return mix;
3522         else if (nid_has_mute(codec, dac, HDA_OUTPUT))
3523                 return dac;
3524         return 0;
3525 }
3526
3527 static hda_nid_t alc_look_for_out_vol_nid(struct hda_codec *codec,
3528                                           hda_nid_t pin, hda_nid_t dac)
3529 {
3530         hda_nid_t mix = alc_auto_dac_to_mix(codec, pin, dac);
3531         if (nid_has_volume(codec, dac, HDA_OUTPUT))
3532                 return dac;
3533         else if (nid_has_volume(codec, mix, HDA_OUTPUT))
3534                 return mix;
3535         else if (nid_has_volume(codec, pin, HDA_OUTPUT))
3536                 return pin;
3537         return 0;
3538 }
3539
3540 /* add playback controls from the parsed DAC table */
3541 static int alc_auto_create_multi_out_ctls(struct hda_codec *codec,
3542                                              const struct auto_pin_cfg *cfg)
3543 {
3544         struct alc_spec *spec = codec->spec;
3545         int i, err, noutputs;
3546
3547         noutputs = cfg->line_outs;
3548         if (spec->multi_ios > 0 && cfg->line_outs < 3)
3549                 noutputs += spec->multi_ios;
3550
3551         for (i = 0; i < noutputs; i++) {
3552                 const char *name;
3553                 int index;
3554                 hda_nid_t dac, pin;
3555                 hda_nid_t sw, vol;
3556
3557                 dac = spec->multiout.dac_nids[i];
3558                 if (!dac)
3559                         continue;
3560                 if (i >= cfg->line_outs) {
3561                         pin = spec->multi_io[i - 1].pin;
3562                         index = 0;
3563                         name = channel_name[i];
3564                 } else {
3565                         pin = cfg->line_out_pins[i];
3566                         name = alc_get_line_out_pfx(spec, i, true, &index);
3567                 }
3568
3569                 sw = alc_look_for_out_mute_nid(codec, pin, dac);
3570                 vol = alc_look_for_out_vol_nid(codec, pin, dac);
3571                 if (!name || !strcmp(name, "CLFE")) {
3572                         /* Center/LFE */
3573                         err = alc_auto_add_vol_ctl(codec, "Center", 0, vol, 1);
3574                         if (err < 0)
3575                                 return err;
3576                         err = alc_auto_add_vol_ctl(codec, "LFE", 0, vol, 2);
3577                         if (err < 0)
3578                                 return err;
3579                         err = alc_auto_add_sw_ctl(codec, "Center", 0, sw, 1);
3580                         if (err < 0)
3581                                 return err;
3582                         err = alc_auto_add_sw_ctl(codec, "LFE", 0, sw, 2);
3583                         if (err < 0)
3584                                 return err;
3585                 } else {
3586                         err = alc_auto_add_stereo_vol(codec, name, index, vol);
3587                         if (err < 0)
3588                                 return err;
3589                         err = alc_auto_add_stereo_sw(codec, name, index, sw);
3590                         if (err < 0)
3591                                 return err;
3592                 }
3593         }
3594         return 0;
3595 }
3596
3597 static int alc_auto_create_extra_out(struct hda_codec *codec, hda_nid_t pin,
3598                                      hda_nid_t dac, const char *pfx,
3599                                      int cidx)
3600 {
3601         struct alc_spec *spec = codec->spec;
3602         hda_nid_t sw, vol;
3603         int err;
3604
3605         if (!dac) {
3606                 unsigned int val;
3607                 /* the corresponding DAC is already occupied */
3608                 if (!(get_wcaps(codec, pin) & AC_WCAP_OUT_AMP))
3609                         return 0; /* no way */
3610                 /* create a switch only */
3611                 val = HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT);
3612                 if (is_ctl_used(spec->sw_ctls, val))
3613                         return 0; /* already created */
3614                 mark_ctl_usage(spec->sw_ctls, val);
3615                 return __add_pb_sw_ctrl(spec, ALC_CTL_WIDGET_MUTE, pfx, cidx, val);
3616         }
3617
3618         sw = alc_look_for_out_mute_nid(codec, pin, dac);
3619         vol = alc_look_for_out_vol_nid(codec, pin, dac);
3620         err = alc_auto_add_stereo_vol(codec, pfx, cidx, vol);
3621         if (err < 0)
3622                 return err;
3623         err = alc_auto_add_stereo_sw(codec, pfx, cidx, sw);
3624         if (err < 0)
3625                 return err;
3626         return 0;
3627 }
3628
3629 static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
3630                                           unsigned int nums,
3631                                           struct hda_ctl_ops *ops)
3632 {
3633         struct alc_spec *spec = codec->spec;
3634         struct hda_bind_ctls **ctlp, *ctl;
3635         snd_array_init(&spec->bind_ctls, sizeof(ctl), 8);
3636         ctlp = snd_array_new(&spec->bind_ctls);
3637         if (!ctlp)
3638                 return NULL;
3639         ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
3640         *ctlp = ctl;
3641         if (ctl)
3642                 ctl->ops = ops;
3643         return ctl;
3644 }
3645
3646 /* add playback controls for speaker and HP outputs */
3647 static int alc_auto_create_extra_outs(struct hda_codec *codec, int num_pins,
3648                                       const hda_nid_t *pins,
3649                                       const hda_nid_t *dacs,
3650                                       const char *pfx)
3651 {
3652         struct alc_spec *spec = codec->spec;
3653         struct hda_bind_ctls *ctl;
3654         char name[32];
3655         int i, n, err;
3656
3657         if (!num_pins || !pins[0])
3658                 return 0;
3659
3660         if (num_pins == 1) {
3661                 hda_nid_t dac = *dacs;
3662                 if (!dac)
3663                         dac = spec->multiout.dac_nids[0];
3664                 return alc_auto_create_extra_out(codec, *pins, dac, pfx, 0);
3665         }
3666
3667         for (i = 0; i < num_pins; i++) {
3668                 hda_nid_t dac;
3669                 if (dacs[num_pins - 1])
3670                         dac = dacs[i]; /* with individual volumes */
3671                 else
3672                         dac = 0;
3673                 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
3674                         err = alc_auto_create_extra_out(codec, pins[i], dac,
3675                                                         "Bass Speaker", 0);
3676                 } else if (num_pins >= 3) {
3677                         snprintf(name, sizeof(name), "%s %s",
3678                                  pfx, channel_name[i]);
3679                         err = alc_auto_create_extra_out(codec, pins[i], dac,
3680                                                         name, 0);
3681                 } else {
3682                         err = alc_auto_create_extra_out(codec, pins[i], dac,
3683                                                         pfx, i);
3684                 }
3685                 if (err < 0)
3686                         return err;
3687         }
3688         if (dacs[num_pins - 1])
3689                 return 0;
3690
3691         /* Let's create a bind-controls for volumes */
3692         ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
3693         if (!ctl)
3694                 return -ENOMEM;
3695         n = 0;
3696         for (i = 0; i < num_pins; i++) {
3697                 hda_nid_t vol;
3698                 if (!pins[i] || !dacs[i])
3699                         continue;
3700                 vol = alc_look_for_out_vol_nid(codec, pins[i], dacs[i]);
3701                 if (vol)
3702                         ctl->values[n++] =
3703                                 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
3704         }
3705         if (n) {
3706                 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
3707                 err = add_control(spec, ALC_CTL_BIND_VOL, name, 0, (long)ctl);
3708                 if (err < 0)
3709                         return err;
3710         }
3711         return 0;
3712 }
3713
3714 static int alc_auto_create_hp_out(struct hda_codec *codec)
3715 {
3716         struct alc_spec *spec = codec->spec;
3717         return alc_auto_create_extra_outs(codec, spec->autocfg.hp_outs,
3718                                           spec->autocfg.hp_pins,
3719                                           spec->multiout.hp_out_nid,
3720                                           "Headphone");
3721 }
3722
3723 static int alc_auto_create_speaker_out(struct hda_codec *codec)
3724 {
3725         struct alc_spec *spec = codec->spec;
3726         return alc_auto_create_extra_outs(codec, spec->autocfg.speaker_outs,
3727                                           spec->autocfg.speaker_pins,
3728                                           spec->multiout.extra_out_nid,
3729                                           "Speaker");
3730 }
3731
3732 static void alc_auto_set_output_and_unmute(struct hda_codec *codec,
3733                                               hda_nid_t pin, int pin_type,
3734                                               hda_nid_t dac)
3735 {
3736         int i, num;
3737         hda_nid_t nid, mix = 0;
3738         hda_nid_t srcs[HDA_MAX_CONNECTIONS];
3739
3740         alc_set_pin_output(codec, pin, pin_type);
3741         nid = alc_go_down_to_selector(codec, pin);
3742         num = snd_hda_get_connections(codec, nid, srcs, ARRAY_SIZE(srcs));
3743         for (i = 0; i < num; i++) {
3744                 if (alc_auto_mix_to_dac(codec, srcs[i]) != dac)
3745                         continue;
3746                 mix = srcs[i];
3747                 break;
3748         }
3749         if (!mix)
3750                 return;
3751
3752         /* need the manual connection? */
3753         if (num > 1)
3754                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, i);
3755         /* unmute mixer widget inputs */
3756         if (nid_has_mute(codec, mix, HDA_INPUT)) {
3757                 snd_hda_codec_write(codec, mix, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3758                             AMP_IN_UNMUTE(0));
3759                 snd_hda_codec_write(codec, mix, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3760                             AMP_IN_UNMUTE(1));
3761         }
3762         /* initialize volume */
3763         nid = alc_look_for_out_vol_nid(codec, pin, dac);
3764         if (nid)
3765                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3766                                     AMP_OUT_ZERO);
3767
3768         /* unmute DAC if it's not assigned to a mixer */
3769         nid = alc_look_for_out_mute_nid(codec, pin, dac);
3770         if (nid == mix && nid_has_mute(codec, dac, HDA_OUTPUT))
3771                 snd_hda_codec_write(codec, dac, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3772                                     AMP_OUT_ZERO);
3773 }
3774
3775 static void alc_auto_init_multi_out(struct hda_codec *codec)
3776 {
3777         struct alc_spec *spec = codec->spec;
3778         int pin_type = get_pin_type(spec->autocfg.line_out_type);
3779         int i;
3780
3781         for (i = 0; i <= HDA_SIDE; i++) {
3782                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
3783                 if (nid)
3784                         alc_auto_set_output_and_unmute(codec, nid, pin_type,
3785                                         spec->multiout.dac_nids[i]);
3786         }
3787 }
3788
3789 static void alc_auto_init_extra_out(struct hda_codec *codec)
3790 {
3791         struct alc_spec *spec = codec->spec;
3792         int i;
3793         hda_nid_t pin, dac;
3794
3795         for (i = 0; i < spec->autocfg.hp_outs; i++) {
3796                 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3797                         break;
3798                 pin = spec->autocfg.hp_pins[i];
3799                 if (!pin)
3800                         break;
3801                 dac = spec->multiout.hp_out_nid[i];
3802                 if (!dac) {
3803                         if (i > 0 && spec->multiout.hp_out_nid[0])
3804                                 dac = spec->multiout.hp_out_nid[0];
3805                         else
3806                                 dac = spec->multiout.dac_nids[0];
3807                 }
3808                 alc_auto_set_output_and_unmute(codec, pin, PIN_HP, dac);
3809         }
3810         for (i = 0; i < spec->autocfg.speaker_outs; i++) {
3811                 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3812                         break;
3813                 pin = spec->autocfg.speaker_pins[i];
3814                 if (!pin)
3815                         break;
3816                 dac = spec->multiout.extra_out_nid[i];
3817                 if (!dac) {
3818                         if (i > 0 && spec->multiout.extra_out_nid[0])
3819                                 dac = spec->multiout.extra_out_nid[0];
3820                         else
3821                                 dac = spec->multiout.dac_nids[0];
3822                 }
3823                 alc_auto_set_output_and_unmute(codec, pin, PIN_OUT, dac);
3824         }
3825 }
3826
3827 /* check whether the given pin can be a multi-io pin */
3828 static bool can_be_multiio_pin(struct hda_codec *codec,
3829                                unsigned int location, hda_nid_t nid)
3830 {
3831         unsigned int defcfg, caps;
3832
3833         defcfg = snd_hda_codec_get_pincfg(codec, nid);
3834         if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
3835                 return false;
3836         if (location && get_defcfg_location(defcfg) != location)
3837                 return false;
3838         caps = snd_hda_query_pin_caps(codec, nid);
3839         if (!(caps & AC_PINCAP_OUT))
3840                 return false;
3841         return true;
3842 }
3843
3844 /*
3845  * multi-io helper
3846  *
3847  * When hardwired is set, try to fill ony hardwired pins, and returns
3848  * zero if any pins are filled, non-zero if nothing found.
3849  * When hardwired is off, try to fill possible input pins, and returns
3850  * the badness value.
3851  */
3852 static int alc_auto_fill_multi_ios(struct hda_codec *codec,
3853                                    hda_nid_t reference_pin,
3854                                    bool hardwired, int offset)
3855 {
3856         struct alc_spec *spec = codec->spec;
3857         struct auto_pin_cfg *cfg = &spec->autocfg;
3858         int type, i, j, dacs, num_pins, old_pins;
3859         unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
3860         unsigned int location = get_defcfg_location(defcfg);
3861         int badness = 0;
3862
3863         old_pins = spec->multi_ios;
3864         if (old_pins >= 2)
3865                 goto end_fill;
3866
3867         num_pins = 0;
3868         for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
3869                 for (i = 0; i < cfg->num_inputs; i++) {
3870                         if (cfg->inputs[i].type != type)
3871                                 continue;
3872                         if (can_be_multiio_pin(codec, location,
3873                                                cfg->inputs[i].pin))
3874                                 num_pins++;
3875                 }
3876         }
3877         if (num_pins < 2)
3878                 goto end_fill;
3879
3880         dacs = spec->multiout.num_dacs;
3881         for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
3882                 for (i = 0; i < cfg->num_inputs; i++) {
3883                         hda_nid_t nid = cfg->inputs[i].pin;
3884                         hda_nid_t dac = 0;
3885
3886                         if (cfg->inputs[i].type != type)
3887                                 continue;
3888                         if (!can_be_multiio_pin(codec, location, nid))
3889                                 continue;
3890                         for (j = 0; j < spec->multi_ios; j++) {
3891                                 if (nid == spec->multi_io[j].pin)
3892                                         break;
3893                         }
3894                         if (j < spec->multi_ios)
3895                                 continue;
3896
3897                         if (offset && offset + spec->multi_ios < dacs) {
3898                                 dac = spec->private_dac_nids[offset + spec->multi_ios];
3899                                 if (!alc_auto_is_dac_reachable(codec, nid, dac))
3900                                         dac = 0;
3901                         }
3902                         if (hardwired)
3903                                 dac = get_dac_if_single(codec, nid);
3904                         else if (!dac)
3905                                 dac = alc_auto_look_for_dac(codec, nid);
3906                         if (!dac) {
3907                                 badness++;
3908                                 continue;
3909                         }
3910                         spec->multi_io[spec->multi_ios].pin = nid;
3911                         spec->multi_io[spec->multi_ios].dac = dac;
3912                         spec->multi_ios++;
3913                         if (spec->multi_ios >= 2)
3914                                 break;
3915                 }
3916         }
3917  end_fill:
3918         if (badness)
3919                 badness = BAD_MULTI_IO;
3920         if (old_pins == spec->multi_ios) {
3921                 if (hardwired)
3922                         return 1; /* nothing found */
3923                 else
3924                         return badness; /* no badness if nothing found */
3925         }
3926         if (!hardwired && spec->multi_ios < 2) {
3927                 spec->multi_ios = old_pins;
3928                 return badness;
3929         }
3930
3931         return 0;
3932 }
3933
3934 static int alc_auto_ch_mode_info(struct snd_kcontrol *kcontrol,
3935                                  struct snd_ctl_elem_info *uinfo)
3936 {
3937         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3938         struct alc_spec *spec = codec->spec;
3939
3940         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3941         uinfo->count = 1;
3942         uinfo->value.enumerated.items = spec->multi_ios + 1;
3943         if (uinfo->value.enumerated.item > spec->multi_ios)
3944                 uinfo->value.enumerated.item = spec->multi_ios;
3945         sprintf(uinfo->value.enumerated.name, "%dch",
3946                 (uinfo->value.enumerated.item + 1) * 2);
3947         return 0;
3948 }
3949
3950 static int alc_auto_ch_mode_get(struct snd_kcontrol *kcontrol,
3951                                 struct snd_ctl_elem_value *ucontrol)
3952 {
3953         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3954         struct alc_spec *spec = codec->spec;
3955         ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
3956         return 0;
3957 }
3958
3959 static int alc_set_multi_io(struct hda_codec *codec, int idx, bool output)
3960 {
3961         struct alc_spec *spec = codec->spec;
3962         hda_nid_t nid = spec->multi_io[idx].pin;
3963
3964         if (!spec->multi_io[idx].ctl_in)
3965                 spec->multi_io[idx].ctl_in =
3966                         snd_hda_codec_read(codec, nid, 0,
3967                                            AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3968         if (output) {
3969                 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
3970                 if (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP)
3971                         snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3972                                                  HDA_AMP_MUTE, 0);
3973                 alc_auto_select_dac(codec, nid, spec->multi_io[idx].dac);
3974         } else {
3975                 if (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP)
3976                         snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
3977                                                  HDA_AMP_MUTE, HDA_AMP_MUTE);
3978                 snd_hda_set_pin_ctl_cache(codec, nid,
3979                                           spec->multi_io[idx].ctl_in);
3980         }
3981         return 0;
3982 }
3983
3984 static int alc_auto_ch_mode_put(struct snd_kcontrol *kcontrol,
3985                                 struct snd_ctl_elem_value *ucontrol)
3986 {
3987         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3988         struct alc_spec *spec = codec->spec;
3989         int i, ch;
3990
3991         ch = ucontrol->value.enumerated.item[0];
3992         if (ch < 0 || ch > spec->multi_ios)
3993                 return -EINVAL;
3994         if (ch == (spec->ext_channel_count - 1) / 2)
3995                 return 0;
3996         spec->ext_channel_count = (ch + 1) * 2;
3997         for (i = 0; i < spec->multi_ios; i++)
3998                 alc_set_multi_io(codec, i, i < ch);
3999         spec->multiout.max_channels = spec->ext_channel_count;
4000         if (spec->need_dac_fix && !spec->const_channel_count)
4001                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
4002         return 1;
4003 }
4004
4005 static const struct snd_kcontrol_new alc_auto_channel_mode_enum = {
4006         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4007         .name = "Channel Mode",
4008         .info = alc_auto_ch_mode_info,
4009         .get = alc_auto_ch_mode_get,
4010         .put = alc_auto_ch_mode_put,
4011 };
4012
4013 static int alc_auto_add_multi_channel_mode(struct hda_codec *codec)
4014 {
4015         struct alc_spec *spec = codec->spec;
4016
4017         if (spec->multi_ios > 0) {
4018                 struct snd_kcontrol_new *knew;
4019
4020                 knew = alc_kcontrol_new(spec);
4021                 if (!knew)
4022                         return -ENOMEM;
4023                 *knew = alc_auto_channel_mode_enum;
4024                 knew->name = kstrdup("Channel Mode", GFP_KERNEL);
4025                 if (!knew->name)
4026                         return -ENOMEM;
4027         }
4028         return 0;
4029 }
4030
4031 /* filter out invalid adc_nids (and capsrc_nids) that don't give all
4032  * active input pins
4033  */
4034 static void alc_remove_invalid_adc_nids(struct hda_codec *codec)
4035 {
4036         struct alc_spec *spec = codec->spec;
4037         const struct hda_input_mux *imux;
4038         hda_nid_t adc_nids[ARRAY_SIZE(spec->private_adc_nids)];
4039         hda_nid_t capsrc_nids[ARRAY_SIZE(spec->private_adc_nids)];
4040         int i, n, nums;
4041
4042         imux = spec->input_mux;
4043         if (!imux)
4044                 return;
4045         if (spec->dyn_adc_switch)
4046                 return;
4047
4048  again:
4049         nums = 0;
4050         for (n = 0; n < spec->num_adc_nids; n++) {
4051                 hda_nid_t cap = spec->private_capsrc_nids[n];
4052                 int num_conns = snd_hda_get_num_conns(codec, cap);
4053                 for (i = 0; i < imux->num_items; i++) {
4054                         hda_nid_t pin = spec->imux_pins[i];
4055                         if (pin) {
4056                                 if (get_connection_index(codec, cap, pin) < 0)
4057                                         break;
4058                         } else if (num_conns <= imux->items[i].index)
4059                                 break;
4060                 }
4061                 if (i >= imux->num_items) {
4062                         adc_nids[nums] = spec->private_adc_nids[n];
4063                         capsrc_nids[nums++] = cap;
4064                 }
4065         }
4066         if (!nums) {
4067                 /* check whether ADC-switch is possible */
4068                 if (!alc_check_dyn_adc_switch(codec)) {
4069                         if (spec->shared_mic_hp) {
4070                                 spec->shared_mic_hp = 0;
4071                                 spec->private_imux[0].num_items = 1;
4072                                 goto again;
4073                         }
4074                         printk(KERN_WARNING "hda_codec: %s: no valid ADC found;"
4075                                " using fallback 0x%x\n",
4076                                codec->chip_name, spec->private_adc_nids[0]);
4077                         spec->num_adc_nids = 1;
4078                         spec->auto_mic = 0;
4079                         return;
4080                 }
4081         } else if (nums != spec->num_adc_nids) {
4082                 memcpy(spec->private_adc_nids, adc_nids,
4083                        nums * sizeof(hda_nid_t));
4084                 memcpy(spec->private_capsrc_nids, capsrc_nids,
4085                        nums * sizeof(hda_nid_t));
4086                 spec->num_adc_nids = nums;
4087         }
4088
4089         if (spec->auto_mic)
4090                 alc_auto_mic_check_imux(codec); /* check auto-mic setups */
4091         else if (spec->input_mux->num_items == 1 || spec->shared_mic_hp)
4092                 spec->num_adc_nids = 1; /* reduce to a single ADC */
4093 }
4094
4095 /*
4096  * initialize ADC paths
4097  */
4098 static void alc_auto_init_adc(struct hda_codec *codec, int adc_idx)
4099 {
4100         struct alc_spec *spec = codec->spec;
4101         hda_nid_t nid;
4102
4103         nid = spec->adc_nids[adc_idx];
4104         /* mute ADC */
4105         if (nid_has_mute(codec, nid, HDA_INPUT)) {
4106                 snd_hda_codec_write(codec, nid, 0,
4107                                     AC_VERB_SET_AMP_GAIN_MUTE,
4108                                     AMP_IN_MUTE(0));
4109                 return;
4110         }
4111         if (!spec->capsrc_nids)
4112                 return;
4113         nid = spec->capsrc_nids[adc_idx];
4114         if (nid_has_mute(codec, nid, HDA_OUTPUT))
4115                 snd_hda_codec_write(codec, nid, 0,
4116                                     AC_VERB_SET_AMP_GAIN_MUTE,
4117                                     AMP_OUT_MUTE);
4118 }
4119
4120 static void alc_auto_init_input_src(struct hda_codec *codec)
4121 {
4122         struct alc_spec *spec = codec->spec;
4123         int c, nums;
4124
4125         for (c = 0; c < spec->num_adc_nids; c++)
4126                 alc_auto_init_adc(codec, c);
4127         if (spec->dyn_adc_switch)
4128                 nums = 1;
4129         else
4130                 nums = spec->num_adc_nids;
4131         for (c = 0; c < nums; c++)
4132                 alc_mux_select(codec, c, spec->cur_mux[c], true);
4133 }
4134
4135 /* add mic boosts if needed */
4136 static int alc_auto_add_mic_boost(struct hda_codec *codec)
4137 {
4138         struct alc_spec *spec = codec->spec;
4139         struct auto_pin_cfg *cfg = &spec->autocfg;
4140         int i, err;
4141         int type_idx = 0;
4142         hda_nid_t nid;
4143         const char *prev_label = NULL;
4144
4145         for (i = 0; i < cfg->num_inputs; i++) {
4146                 if (cfg->inputs[i].type > AUTO_PIN_MIC)
4147                         break;
4148                 nid = cfg->inputs[i].pin;
4149                 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
4150                         const char *label;
4151                         char boost_label[32];
4152
4153                         label = hda_get_autocfg_input_label(codec, cfg, i);
4154                         if (spec->shared_mic_hp && !strcmp(label, "Misc"))
4155                                 label = "Headphone Mic";
4156                         if (prev_label && !strcmp(label, prev_label))
4157                                 type_idx++;
4158                         else
4159                                 type_idx = 0;
4160                         prev_label = label;
4161
4162                         snprintf(boost_label, sizeof(boost_label),
4163                                  "%s Boost Volume", label);
4164                         err = add_control(spec, ALC_CTL_WIDGET_VOL,
4165                                           boost_label, type_idx,
4166                                   HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT));
4167                         if (err < 0)
4168                                 return err;
4169                 }
4170         }
4171         return 0;
4172 }
4173
4174 /* select or unmute the given capsrc route */
4175 static void select_or_unmute_capsrc(struct hda_codec *codec, hda_nid_t cap,
4176                                     int idx)
4177 {
4178         if (get_wcaps_type(get_wcaps(codec, cap)) == AC_WID_AUD_MIX) {
4179                 snd_hda_codec_amp_stereo(codec, cap, HDA_INPUT, idx,
4180                                          HDA_AMP_MUTE, 0);
4181         } else if (snd_hda_get_num_conns(codec, cap) > 1) {
4182                 snd_hda_codec_write_cache(codec, cap, 0,
4183                                           AC_VERB_SET_CONNECT_SEL, idx);
4184         }
4185 }
4186
4187 /* set the default connection to that pin */
4188 static int init_capsrc_for_pin(struct hda_codec *codec, hda_nid_t pin)
4189 {
4190         struct alc_spec *spec = codec->spec;
4191         int i;
4192
4193         if (!pin)
4194                 return 0;
4195         for (i = 0; i < spec->num_adc_nids; i++) {
4196                 hda_nid_t cap = get_capsrc(spec, i);
4197                 int idx;
4198
4199                 idx = get_connection_index(codec, cap, pin);
4200                 if (idx < 0)
4201                         continue;
4202                 select_or_unmute_capsrc(codec, cap, idx);
4203                 return i; /* return the found index */
4204         }
4205         return -1; /* not found */
4206 }
4207
4208 /* initialize some special cases for input sources */
4209 static void alc_init_special_input_src(struct hda_codec *codec)
4210 {
4211         struct alc_spec *spec = codec->spec;
4212         int i;
4213
4214         for (i = 0; i < spec->autocfg.num_inputs; i++)
4215                 init_capsrc_for_pin(codec, spec->autocfg.inputs[i].pin);
4216 }
4217
4218 /* assign appropriate capture mixers */
4219 static void set_capture_mixer(struct hda_codec *codec)
4220 {
4221         struct alc_spec *spec = codec->spec;
4222         static const struct snd_kcontrol_new *caps[2][3] = {
4223                 { alc_capture_mixer_nosrc1,
4224                   alc_capture_mixer_nosrc2,
4225                   alc_capture_mixer_nosrc3 },
4226                 { alc_capture_mixer1,
4227                   alc_capture_mixer2,
4228                   alc_capture_mixer3 },
4229         };
4230
4231         /* check whether either of ADC or MUX has a volume control */
4232         if (!nid_has_volume(codec, spec->adc_nids[0], HDA_INPUT)) {
4233                 if (!spec->capsrc_nids)
4234                         return; /* no volume */
4235                 if (!nid_has_volume(codec, spec->capsrc_nids[0], HDA_OUTPUT))
4236                         return; /* no volume in capsrc, too */
4237                 spec->vol_in_capsrc = 1;
4238         }
4239
4240         if (spec->num_adc_nids > 0) {
4241                 int mux = 0;
4242                 int num_adcs = 0;
4243
4244                 if (spec->input_mux && spec->input_mux->num_items > 1)
4245                         mux = 1;
4246                 if (spec->auto_mic) {
4247                         num_adcs = 1;
4248                         mux = 0;
4249                 } else if (spec->dyn_adc_switch)
4250                         num_adcs = 1;
4251                 if (!num_adcs) {
4252                         if (spec->num_adc_nids > 3)
4253                                 spec->num_adc_nids = 3;
4254                         else if (!spec->num_adc_nids)
4255                                 return;
4256                         num_adcs = spec->num_adc_nids;
4257                 }
4258                 spec->cap_mixer = caps[mux][num_adcs - 1];
4259         }
4260 }
4261
4262 /*
4263  * standard auto-parser initializations
4264  */
4265 static void alc_auto_init_std(struct hda_codec *codec)
4266 {
4267         alc_auto_init_multi_out(codec);
4268         alc_auto_init_extra_out(codec);
4269         alc_auto_init_analog_input(codec);
4270         alc_auto_init_input_src(codec);
4271         alc_auto_init_digital(codec);
4272         alc_inithook(codec);
4273 }
4274
4275 /*
4276  * Digital-beep handlers
4277  */
4278 #ifdef CONFIG_SND_HDA_INPUT_BEEP
4279 #define set_beep_amp(spec, nid, idx, dir) \
4280         ((spec)->beep_amp = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir))
4281
4282 static const struct snd_pci_quirk beep_white_list[] = {
4283         SND_PCI_QUIRK(0x1043, 0x829f, "ASUS", 1),
4284         SND_PCI_QUIRK(0x1043, 0x83ce, "EeePC", 1),
4285         SND_PCI_QUIRK(0x1043, 0x831a, "EeePC", 1),
4286         SND_PCI_QUIRK(0x1043, 0x834a, "EeePC", 1),
4287         SND_PCI_QUIRK(0x1458, 0xa002, "GA-MA790X", 1),
4288         SND_PCI_QUIRK(0x8086, 0xd613, "Intel", 1),
4289         {}
4290 };
4291
4292 static inline int has_cdefine_beep(struct hda_codec *codec)
4293 {
4294         struct alc_spec *spec = codec->spec;
4295         const struct snd_pci_quirk *q;
4296         q = snd_pci_quirk_lookup(codec->bus->pci, beep_white_list);
4297         if (q)
4298                 return q->value;
4299         return spec->cdefine.enable_pcbeep;
4300 }
4301 #else
4302 #define set_beep_amp(spec, nid, idx, dir) /* NOP */
4303 #define has_cdefine_beep(codec)         0
4304 #endif
4305
4306 /* parse the BIOS configuration and set up the alc_spec */
4307 /* return 1 if successful, 0 if the proper config is not found,
4308  * or a negative error code
4309  */
4310 static int alc_parse_auto_config(struct hda_codec *codec,
4311                                  const hda_nid_t *ignore_nids,
4312                                  const hda_nid_t *ssid_nids)
4313 {
4314         struct alc_spec *spec = codec->spec;
4315         struct auto_pin_cfg *cfg = &spec->autocfg;
4316         int err;
4317
4318         err = snd_hda_parse_pin_defcfg(codec, cfg, ignore_nids,
4319                                        spec->parse_flags);
4320         if (err < 0)
4321                 return err;
4322         if (!cfg->line_outs) {
4323                 if (cfg->dig_outs || cfg->dig_in_pin) {
4324                         spec->multiout.max_channels = 2;
4325                         spec->no_analog = 1;
4326                         goto dig_only;
4327                 }
4328                 return 0; /* can't find valid BIOS pin config */
4329         }
4330
4331         if (!spec->no_primary_hp &&
4332             cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4333             cfg->line_outs <= cfg->hp_outs) {
4334                 /* use HP as primary out */
4335                 cfg->speaker_outs = cfg->line_outs;
4336                 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4337                        sizeof(cfg->speaker_pins));
4338                 cfg->line_outs = cfg->hp_outs;
4339                 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4340                 cfg->hp_outs = 0;
4341                 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4342                 cfg->line_out_type = AUTO_PIN_HP_OUT;
4343         }
4344
4345         err = alc_auto_fill_dac_nids(codec);
4346         if (err < 0)
4347                 return err;
4348         err = alc_auto_add_multi_channel_mode(codec);
4349         if (err < 0)
4350                 return err;
4351         err = alc_auto_create_multi_out_ctls(codec, cfg);
4352         if (err < 0)
4353                 return err;
4354         err = alc_auto_create_hp_out(codec);
4355         if (err < 0)
4356                 return err;
4357         err = alc_auto_create_speaker_out(codec);
4358         if (err < 0)
4359                 return err;
4360         err = alc_auto_create_shared_input(codec);
4361         if (err < 0)
4362                 return err;
4363         err = alc_auto_create_input_ctls(codec);
4364         if (err < 0)
4365                 return err;
4366
4367         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
4368
4369  dig_only:
4370         alc_auto_parse_digital(codec);
4371
4372         if (!spec->no_analog)
4373                 alc_remove_invalid_adc_nids(codec);
4374
4375         if (ssid_nids)
4376                 alc_ssid_check(codec, ssid_nids);
4377
4378         if (!spec->no_analog) {
4379                 alc_auto_check_switches(codec);
4380                 err = alc_auto_add_mic_boost(codec);
4381                 if (err < 0)
4382                         return err;
4383         }
4384
4385         if (spec->kctls.list)
4386                 add_mixer(spec, spec->kctls.list);
4387
4388         if (!spec->no_analog && !spec->cap_mixer)
4389                 set_capture_mixer(codec);
4390
4391         return 1;
4392 }
4393
4394 /* common preparation job for alc_spec */
4395 static int alc_alloc_spec(struct hda_codec *codec, hda_nid_t mixer_nid)
4396 {
4397         struct alc_spec *spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4398         int err;
4399
4400         if (!spec)
4401                 return -ENOMEM;
4402         codec->spec = spec;
4403         spec->mixer_nid = mixer_nid;
4404         snd_hda_gen_init(&spec->gen);
4405
4406         err = alc_codec_rename_from_preset(codec);
4407         if (err < 0) {
4408                 kfree(spec);
4409                 return err;
4410         }
4411         return 0;
4412 }
4413
4414 static int alc880_parse_auto_config(struct hda_codec *codec)
4415 {
4416         static const hda_nid_t alc880_ignore[] = { 0x1d, 0 };
4417         static const hda_nid_t alc880_ssids[] = { 0x15, 0x1b, 0x14, 0 };
4418         return alc_parse_auto_config(codec, alc880_ignore, alc880_ssids);
4419 }
4420
4421 /*
4422  * ALC880 fix-ups
4423  */
4424 enum {
4425         ALC880_FIXUP_GPIO1,
4426         ALC880_FIXUP_GPIO2,
4427         ALC880_FIXUP_MEDION_RIM,
4428         ALC880_FIXUP_LG,
4429         ALC880_FIXUP_W810,
4430         ALC880_FIXUP_EAPD_COEF,
4431         ALC880_FIXUP_TCL_S700,
4432         ALC880_FIXUP_VOL_KNOB,
4433         ALC880_FIXUP_FUJITSU,
4434         ALC880_FIXUP_F1734,
4435         ALC880_FIXUP_UNIWILL,
4436         ALC880_FIXUP_UNIWILL_DIG,
4437         ALC880_FIXUP_Z71V,
4438         ALC880_FIXUP_3ST_BASE,
4439         ALC880_FIXUP_3ST,
4440         ALC880_FIXUP_3ST_DIG,
4441         ALC880_FIXUP_5ST_BASE,
4442         ALC880_FIXUP_5ST,
4443         ALC880_FIXUP_5ST_DIG,
4444         ALC880_FIXUP_6ST_BASE,
4445         ALC880_FIXUP_6ST,
4446         ALC880_FIXUP_6ST_DIG,
4447 };
4448
4449 /* enable the volume-knob widget support on NID 0x21 */
4450 static void alc880_fixup_vol_knob(struct hda_codec *codec,
4451                                   const struct alc_fixup *fix, int action)
4452 {
4453         if (action == ALC_FIXUP_ACT_PROBE)
4454                 snd_hda_jack_detect_enable(codec, 0x21, ALC_DCVOL_EVENT);
4455 }
4456
4457 static const struct alc_fixup alc880_fixups[] = {
4458         [ALC880_FIXUP_GPIO1] = {
4459                 .type = ALC_FIXUP_VERBS,
4460                 .v.verbs = alc_gpio1_init_verbs,
4461         },
4462         [ALC880_FIXUP_GPIO2] = {
4463                 .type = ALC_FIXUP_VERBS,
4464                 .v.verbs = alc_gpio2_init_verbs,
4465         },
4466         [ALC880_FIXUP_MEDION_RIM] = {
4467                 .type = ALC_FIXUP_VERBS,
4468                 .v.verbs = (const struct hda_verb[]) {
4469                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4470                         { 0x20, AC_VERB_SET_PROC_COEF,  0x3060 },
4471                         { }
4472                 },
4473                 .chained = true,
4474                 .chain_id = ALC880_FIXUP_GPIO2,
4475         },
4476         [ALC880_FIXUP_LG] = {
4477                 .type = ALC_FIXUP_PINS,
4478                 .v.pins = (const struct alc_pincfg[]) {
4479                         /* disable bogus unused pins */
4480                         { 0x16, 0x411111f0 },
4481                         { 0x18, 0x411111f0 },
4482                         { 0x1a, 0x411111f0 },
4483                         { }
4484                 }
4485         },
4486         [ALC880_FIXUP_W810] = {
4487                 .type = ALC_FIXUP_PINS,
4488                 .v.pins = (const struct alc_pincfg[]) {
4489                         /* disable bogus unused pins */
4490                         { 0x17, 0x411111f0 },
4491                         { }
4492                 },
4493                 .chained = true,
4494                 .chain_id = ALC880_FIXUP_GPIO2,
4495         },
4496         [ALC880_FIXUP_EAPD_COEF] = {
4497                 .type = ALC_FIXUP_VERBS,
4498                 .v.verbs = (const struct hda_verb[]) {
4499                         /* change to EAPD mode */
4500                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4501                         { 0x20, AC_VERB_SET_PROC_COEF,  0x3060 },
4502                         {}
4503                 },
4504         },
4505         [ALC880_FIXUP_TCL_S700] = {
4506                 .type = ALC_FIXUP_VERBS,
4507                 .v.verbs = (const struct hda_verb[]) {
4508                         /* change to EAPD mode */
4509                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4510                         { 0x20, AC_VERB_SET_PROC_COEF,  0x3070 },
4511                         {}
4512                 },
4513                 .chained = true,
4514                 .chain_id = ALC880_FIXUP_GPIO2,
4515         },
4516         [ALC880_FIXUP_VOL_KNOB] = {
4517                 .type = ALC_FIXUP_FUNC,
4518                 .v.func = alc880_fixup_vol_knob,
4519         },
4520         [ALC880_FIXUP_FUJITSU] = {
4521                 /* override all pins as BIOS on old Amilo is broken */
4522                 .type = ALC_FIXUP_PINS,
4523                 .v.pins = (const struct alc_pincfg[]) {
4524                         { 0x14, 0x0121411f }, /* HP */
4525                         { 0x15, 0x99030120 }, /* speaker */
4526                         { 0x16, 0x99030130 }, /* bass speaker */
4527                         { 0x17, 0x411111f0 }, /* N/A */
4528                         { 0x18, 0x411111f0 }, /* N/A */
4529                         { 0x19, 0x01a19950 }, /* mic-in */
4530                         { 0x1a, 0x411111f0 }, /* N/A */
4531                         { 0x1b, 0x411111f0 }, /* N/A */
4532                         { 0x1c, 0x411111f0 }, /* N/A */
4533                         { 0x1d, 0x411111f0 }, /* N/A */
4534                         { 0x1e, 0x01454140 }, /* SPDIF out */
4535                         { }
4536                 },
4537                 .chained = true,
4538                 .chain_id = ALC880_FIXUP_VOL_KNOB,
4539         },
4540         [ALC880_FIXUP_F1734] = {
4541                 /* almost compatible with FUJITSU, but no bass and SPDIF */
4542                 .type = ALC_FIXUP_PINS,
4543                 .v.pins = (const struct alc_pincfg[]) {
4544                         { 0x14, 0x0121411f }, /* HP */
4545                         { 0x15, 0x99030120 }, /* speaker */
4546                         { 0x16, 0x411111f0 }, /* N/A */
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, 0x411111f0 }, /* N/A */
4555                         { }
4556                 },
4557                 .chained = true,
4558                 .chain_id = ALC880_FIXUP_VOL_KNOB,
4559         },
4560         [ALC880_FIXUP_UNIWILL] = {
4561                 /* need to fix HP and speaker pins to be parsed correctly */
4562                 .type = ALC_FIXUP_PINS,
4563                 .v.pins = (const struct alc_pincfg[]) {
4564                         { 0x14, 0x0121411f }, /* HP */
4565                         { 0x15, 0x99030120 }, /* speaker */
4566                         { 0x16, 0x99030130 }, /* bass speaker */
4567                         { }
4568                 },
4569         },
4570         [ALC880_FIXUP_UNIWILL_DIG] = {
4571                 .type = ALC_FIXUP_PINS,
4572                 .v.pins = (const struct alc_pincfg[]) {
4573                         /* disable bogus unused pins */
4574                         { 0x17, 0x411111f0 },
4575                         { 0x19, 0x411111f0 },
4576                         { 0x1b, 0x411111f0 },
4577                         { 0x1f, 0x411111f0 },
4578                         { }
4579                 }
4580         },
4581         [ALC880_FIXUP_Z71V] = {
4582                 .type = ALC_FIXUP_PINS,
4583                 .v.pins = (const struct alc_pincfg[]) {
4584                         /* set up the whole pins as BIOS is utterly broken */
4585                         { 0x14, 0x99030120 }, /* speaker */
4586                         { 0x15, 0x0121411f }, /* HP */
4587                         { 0x16, 0x411111f0 }, /* N/A */
4588                         { 0x17, 0x411111f0 }, /* N/A */
4589                         { 0x18, 0x01a19950 }, /* mic-in */
4590                         { 0x19, 0x411111f0 }, /* N/A */
4591                         { 0x1a, 0x01813031 }, /* line-in */
4592                         { 0x1b, 0x411111f0 }, /* N/A */
4593                         { 0x1c, 0x411111f0 }, /* N/A */
4594                         { 0x1d, 0x411111f0 }, /* N/A */
4595                         { 0x1e, 0x0144111e }, /* SPDIF */
4596                         { }
4597                 }
4598         },
4599         [ALC880_FIXUP_3ST_BASE] = {
4600                 .type = ALC_FIXUP_PINS,
4601                 .v.pins = (const struct alc_pincfg[]) {
4602                         { 0x14, 0x01014010 }, /* line-out */
4603                         { 0x15, 0x411111f0 }, /* N/A */
4604                         { 0x16, 0x411111f0 }, /* N/A */
4605                         { 0x17, 0x411111f0 }, /* N/A */
4606                         { 0x18, 0x01a19c30 }, /* mic-in */
4607                         { 0x19, 0x0121411f }, /* HP */
4608                         { 0x1a, 0x01813031 }, /* line-in */
4609                         { 0x1b, 0x02a19c40 }, /* front-mic */
4610                         { 0x1c, 0x411111f0 }, /* N/A */
4611                         { 0x1d, 0x411111f0 }, /* N/A */
4612                         /* 0x1e is filled in below */
4613                         { 0x1f, 0x411111f0 }, /* N/A */
4614                         { }
4615                 }
4616         },
4617         [ALC880_FIXUP_3ST] = {
4618                 .type = ALC_FIXUP_PINS,
4619                 .v.pins = (const struct alc_pincfg[]) {
4620                         { 0x1e, 0x411111f0 }, /* N/A */
4621                         { }
4622                 },
4623                 .chained = true,
4624                 .chain_id = ALC880_FIXUP_3ST_BASE,
4625         },
4626         [ALC880_FIXUP_3ST_DIG] = {
4627                 .type = ALC_FIXUP_PINS,
4628                 .v.pins = (const struct alc_pincfg[]) {
4629                         { 0x1e, 0x0144111e }, /* SPDIF */
4630                         { }
4631                 },
4632                 .chained = true,
4633                 .chain_id = ALC880_FIXUP_3ST_BASE,
4634         },
4635         [ALC880_FIXUP_5ST_BASE] = {
4636                 .type = ALC_FIXUP_PINS,
4637                 .v.pins = (const struct alc_pincfg[]) {
4638                         { 0x14, 0x01014010 }, /* front */
4639                         { 0x15, 0x411111f0 }, /* N/A */
4640                         { 0x16, 0x01011411 }, /* CLFE */
4641                         { 0x17, 0x01016412 }, /* surr */
4642                         { 0x18, 0x01a19c30 }, /* mic-in */
4643                         { 0x19, 0x0121411f }, /* HP */
4644                         { 0x1a, 0x01813031 }, /* line-in */
4645                         { 0x1b, 0x02a19c40 }, /* front-mic */
4646                         { 0x1c, 0x411111f0 }, /* N/A */
4647                         { 0x1d, 0x411111f0 }, /* N/A */
4648                         /* 0x1e is filled in below */
4649                         { 0x1f, 0x411111f0 }, /* N/A */
4650                         { }
4651                 }
4652         },
4653         [ALC880_FIXUP_5ST] = {
4654                 .type = ALC_FIXUP_PINS,
4655                 .v.pins = (const struct alc_pincfg[]) {
4656                         { 0x1e, 0x411111f0 }, /* N/A */
4657                         { }
4658                 },
4659                 .chained = true,
4660                 .chain_id = ALC880_FIXUP_5ST_BASE,
4661         },
4662         [ALC880_FIXUP_5ST_DIG] = {
4663                 .type = ALC_FIXUP_PINS,
4664                 .v.pins = (const struct alc_pincfg[]) {
4665                         { 0x1e, 0x0144111e }, /* SPDIF */
4666                         { }
4667                 },
4668                 .chained = true,
4669                 .chain_id = ALC880_FIXUP_5ST_BASE,
4670         },
4671         [ALC880_FIXUP_6ST_BASE] = {
4672                 .type = ALC_FIXUP_PINS,
4673                 .v.pins = (const struct alc_pincfg[]) {
4674                         { 0x14, 0x01014010 }, /* front */
4675                         { 0x15, 0x01016412 }, /* surr */
4676                         { 0x16, 0x01011411 }, /* CLFE */
4677                         { 0x17, 0x01012414 }, /* side */
4678                         { 0x18, 0x01a19c30 }, /* mic-in */
4679                         { 0x19, 0x02a19c40 }, /* front-mic */
4680                         { 0x1a, 0x01813031 }, /* line-in */
4681                         { 0x1b, 0x0121411f }, /* HP */
4682                         { 0x1c, 0x411111f0 }, /* N/A */
4683                         { 0x1d, 0x411111f0 }, /* N/A */
4684                         /* 0x1e is filled in below */
4685                         { 0x1f, 0x411111f0 }, /* N/A */
4686                         { }
4687                 }
4688         },
4689         [ALC880_FIXUP_6ST] = {
4690                 .type = ALC_FIXUP_PINS,
4691                 .v.pins = (const struct alc_pincfg[]) {
4692                         { 0x1e, 0x411111f0 }, /* N/A */
4693                         { }
4694                 },
4695                 .chained = true,
4696                 .chain_id = ALC880_FIXUP_6ST_BASE,
4697         },
4698         [ALC880_FIXUP_6ST_DIG] = {
4699                 .type = ALC_FIXUP_PINS,
4700                 .v.pins = (const struct alc_pincfg[]) {
4701                         { 0x1e, 0x0144111e }, /* SPDIF */
4702                         { }
4703                 },
4704                 .chained = true,
4705                 .chain_id = ALC880_FIXUP_6ST_BASE,
4706         },
4707 };
4708
4709 static const struct snd_pci_quirk alc880_fixup_tbl[] = {
4710         SND_PCI_QUIRK(0x1019, 0x0f69, "Coeus G610P", ALC880_FIXUP_W810),
4711         SND_PCI_QUIRK(0x1043, 0x1964, "ASUS Z71V", ALC880_FIXUP_Z71V),
4712         SND_PCI_QUIRK_VENDOR(0x1043, "ASUS", ALC880_FIXUP_GPIO1),
4713         SND_PCI_QUIRK(0x1558, 0x5401, "Clevo GPIO2", ALC880_FIXUP_GPIO2),
4714         SND_PCI_QUIRK_VENDOR(0x1558, "Clevo", ALC880_FIXUP_EAPD_COEF),
4715         SND_PCI_QUIRK(0x1584, 0x9050, "Uniwill", ALC880_FIXUP_UNIWILL_DIG),
4716         SND_PCI_QUIRK(0x1584, 0x9054, "Uniwill", ALC880_FIXUP_F1734),
4717         SND_PCI_QUIRK(0x1584, 0x9070, "Uniwill", ALC880_FIXUP_UNIWILL),
4718         SND_PCI_QUIRK(0x1584, 0x9077, "Uniwill P53", ALC880_FIXUP_VOL_KNOB),
4719         SND_PCI_QUIRK(0x161f, 0x203d, "W810", ALC880_FIXUP_W810),
4720         SND_PCI_QUIRK(0x161f, 0x205d, "Medion Rim 2150", ALC880_FIXUP_MEDION_RIM),
4721         SND_PCI_QUIRK(0x1734, 0x107c, "FSC F1734", ALC880_FIXUP_F1734),
4722         SND_PCI_QUIRK(0x1734, 0x1094, "FSC Amilo M1451G", ALC880_FIXUP_FUJITSU),
4723         SND_PCI_QUIRK(0x1734, 0x10ac, "FSC AMILO Xi 1526", ALC880_FIXUP_F1734),
4724         SND_PCI_QUIRK(0x1734, 0x10b0, "FSC Amilo Pi1556", ALC880_FIXUP_FUJITSU),
4725         SND_PCI_QUIRK(0x1854, 0x003b, "LG", ALC880_FIXUP_LG),
4726         SND_PCI_QUIRK(0x1854, 0x005f, "LG P1 Express", ALC880_FIXUP_LG),
4727         SND_PCI_QUIRK(0x1854, 0x0068, "LG w1", ALC880_FIXUP_LG),
4728         SND_PCI_QUIRK(0x19db, 0x4188, "TCL S700", ALC880_FIXUP_TCL_S700),
4729
4730         /* Below is the copied entries from alc880_quirks.c.
4731          * It's not quite sure whether BIOS sets the correct pin-config table
4732          * on these machines, thus they are kept to be compatible with
4733          * the old static quirks.  Once when it's confirmed to work without
4734          * these overrides, it'd be better to remove.
4735          */
4736         SND_PCI_QUIRK(0x1019, 0xa880, "ECS", ALC880_FIXUP_5ST_DIG),
4737         SND_PCI_QUIRK(0x1019, 0xa884, "Acer APFV", ALC880_FIXUP_6ST),
4738         SND_PCI_QUIRK(0x1025, 0x0070, "ULI", ALC880_FIXUP_3ST_DIG),
4739         SND_PCI_QUIRK(0x1025, 0x0077, "ULI", ALC880_FIXUP_6ST_DIG),
4740         SND_PCI_QUIRK(0x1025, 0x0078, "ULI", ALC880_FIXUP_6ST_DIG),
4741         SND_PCI_QUIRK(0x1025, 0x0087, "ULI", ALC880_FIXUP_6ST_DIG),
4742         SND_PCI_QUIRK(0x1025, 0xe309, "ULI", ALC880_FIXUP_3ST_DIG),
4743         SND_PCI_QUIRK(0x1025, 0xe310, "ULI", ALC880_FIXUP_3ST),
4744         SND_PCI_QUIRK(0x1039, 0x1234, NULL, ALC880_FIXUP_6ST_DIG),
4745         SND_PCI_QUIRK(0x104d, 0x81a0, "Sony", ALC880_FIXUP_3ST),
4746         SND_PCI_QUIRK(0x104d, 0x81d6, "Sony", ALC880_FIXUP_3ST),
4747         SND_PCI_QUIRK(0x107b, 0x3032, "Gateway", ALC880_FIXUP_5ST),
4748         SND_PCI_QUIRK(0x107b, 0x3033, "Gateway", ALC880_FIXUP_5ST),
4749         SND_PCI_QUIRK(0x107b, 0x4039, "Gateway", ALC880_FIXUP_5ST),
4750         SND_PCI_QUIRK(0x1297, 0xc790, "Shuttle ST20G5", ALC880_FIXUP_6ST_DIG),
4751         SND_PCI_QUIRK(0x1458, 0xa102, "Gigabyte K8", ALC880_FIXUP_6ST_DIG),
4752         SND_PCI_QUIRK(0x1462, 0x1150, "MSI", ALC880_FIXUP_6ST_DIG),
4753         SND_PCI_QUIRK(0x1509, 0x925d, "FIC P4M", ALC880_FIXUP_6ST_DIG),
4754         SND_PCI_QUIRK(0x1565, 0x8202, "Biostar", ALC880_FIXUP_5ST_DIG),
4755         SND_PCI_QUIRK(0x1695, 0x400d, "EPoX", ALC880_FIXUP_5ST_DIG),
4756         SND_PCI_QUIRK(0x1695, 0x4012, "EPox EP-5LDA", ALC880_FIXUP_5ST_DIG),
4757         SND_PCI_QUIRK(0x2668, 0x8086, NULL, ALC880_FIXUP_6ST_DIG), /* broken BIOS */
4758         SND_PCI_QUIRK(0x8086, 0x2668, NULL, ALC880_FIXUP_6ST_DIG),
4759         SND_PCI_QUIRK(0x8086, 0xa100, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4760         SND_PCI_QUIRK(0x8086, 0xd400, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4761         SND_PCI_QUIRK(0x8086, 0xd401, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4762         SND_PCI_QUIRK(0x8086, 0xd402, "Intel mobo", ALC880_FIXUP_3ST_DIG),
4763         SND_PCI_QUIRK(0x8086, 0xe224, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4764         SND_PCI_QUIRK(0x8086, 0xe305, "Intel mobo", ALC880_FIXUP_3ST_DIG),
4765         SND_PCI_QUIRK(0x8086, 0xe308, "Intel mobo", ALC880_FIXUP_3ST_DIG),
4766         SND_PCI_QUIRK(0x8086, 0xe400, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4767         SND_PCI_QUIRK(0x8086, 0xe401, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4768         SND_PCI_QUIRK(0x8086, 0xe402, "Intel mobo", ALC880_FIXUP_5ST_DIG),
4769         /* default Intel */
4770         SND_PCI_QUIRK_VENDOR(0x8086, "Intel mobo", ALC880_FIXUP_3ST),
4771         SND_PCI_QUIRK(0xa0a0, 0x0560, "AOpen i915GMm-HFS", ALC880_FIXUP_5ST_DIG),
4772         SND_PCI_QUIRK(0xe803, 0x1019, NULL, ALC880_FIXUP_6ST_DIG),
4773         {}
4774 };
4775
4776 static const struct alc_model_fixup alc880_fixup_models[] = {
4777         {.id = ALC880_FIXUP_3ST, .name = "3stack"},
4778         {.id = ALC880_FIXUP_3ST_DIG, .name = "3stack-digout"},
4779         {.id = ALC880_FIXUP_5ST, .name = "5stack"},
4780         {.id = ALC880_FIXUP_5ST_DIG, .name = "5stack-digout"},
4781         {.id = ALC880_FIXUP_6ST, .name = "6stack"},
4782         {.id = ALC880_FIXUP_6ST_DIG, .name = "6stack-digout"},
4783         {}
4784 };
4785
4786
4787 /*
4788  * OK, here we have finally the patch for ALC880
4789  */
4790 static int patch_alc880(struct hda_codec *codec)
4791 {
4792         struct alc_spec *spec;
4793         int err;
4794
4795         err = alc_alloc_spec(codec, 0x0b);
4796         if (err < 0)
4797                 return err;
4798
4799         spec = codec->spec;
4800         spec->need_dac_fix = 1;
4801
4802         alc_pick_fixup(codec, alc880_fixup_models, alc880_fixup_tbl,
4803                        alc880_fixups);
4804         alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
4805
4806         /* automatic parse from the BIOS config */
4807         err = alc880_parse_auto_config(codec);
4808         if (err < 0)
4809                 goto error;
4810
4811         if (!spec->no_analog) {
4812                 err = snd_hda_attach_beep_device(codec, 0x1);
4813                 if (err < 0)
4814                         goto error;
4815                 set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
4816         }
4817
4818         codec->patch_ops = alc_patch_ops;
4819
4820         alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
4821
4822         return 0;
4823
4824  error:
4825         alc_free(codec);
4826         return err;
4827 }
4828
4829
4830 /*
4831  * ALC260 support
4832  */
4833 static int alc260_parse_auto_config(struct hda_codec *codec)
4834 {
4835         static const hda_nid_t alc260_ignore[] = { 0x17, 0 };
4836         static const hda_nid_t alc260_ssids[] = { 0x10, 0x15, 0x0f, 0 };
4837         return alc_parse_auto_config(codec, alc260_ignore, alc260_ssids);
4838 }
4839
4840 /*
4841  * Pin config fixes
4842  */
4843 enum {
4844         ALC260_FIXUP_HP_DC5750,
4845         ALC260_FIXUP_HP_PIN_0F,
4846         ALC260_FIXUP_COEF,
4847         ALC260_FIXUP_GPIO1,
4848         ALC260_FIXUP_GPIO1_TOGGLE,
4849         ALC260_FIXUP_REPLACER,
4850         ALC260_FIXUP_HP_B1900,
4851         ALC260_FIXUP_KN1,
4852 };
4853
4854 static void alc260_gpio1_automute(struct hda_codec *codec)
4855 {
4856         struct alc_spec *spec = codec->spec;
4857         snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
4858                             spec->hp_jack_present);
4859 }
4860
4861 static void alc260_fixup_gpio1_toggle(struct hda_codec *codec,
4862                                       const struct alc_fixup *fix, int action)
4863 {
4864         struct alc_spec *spec = codec->spec;
4865         if (action == ALC_FIXUP_ACT_PROBE) {
4866                 /* although the machine has only one output pin, we need to
4867                  * toggle GPIO1 according to the jack state
4868                  */
4869                 spec->automute_hook = alc260_gpio1_automute;
4870                 spec->detect_hp = 1;
4871                 spec->automute_speaker = 1;
4872                 spec->autocfg.hp_pins[0] = 0x0f; /* copy it for automute */
4873                 snd_hda_jack_detect_enable(codec, 0x0f, ALC_HP_EVENT);
4874                 snd_hda_gen_add_verbs(&spec->gen, alc_gpio1_init_verbs);
4875         }
4876 }
4877
4878 static void alc260_fixup_kn1(struct hda_codec *codec,
4879                              const struct alc_fixup *fix, int action)
4880 {
4881         struct alc_spec *spec = codec->spec;
4882         static const struct alc_pincfg pincfgs[] = {
4883                 { 0x0f, 0x02214000 }, /* HP/speaker */
4884                 { 0x12, 0x90a60160 }, /* int mic */
4885                 { 0x13, 0x02a19000 }, /* ext mic */
4886                 { 0x18, 0x01446000 }, /* SPDIF out */
4887                 /* disable bogus I/O pins */
4888                 { 0x10, 0x411111f0 },
4889                 { 0x11, 0x411111f0 },
4890                 { 0x14, 0x411111f0 },
4891                 { 0x15, 0x411111f0 },
4892                 { 0x16, 0x411111f0 },
4893                 { 0x17, 0x411111f0 },
4894                 { 0x19, 0x411111f0 },
4895                 { }
4896         };
4897
4898         switch (action) {
4899         case ALC_FIXUP_ACT_PRE_PROBE:
4900                 alc_apply_pincfgs(codec, pincfgs);
4901                 break;
4902         case ALC_FIXUP_ACT_PROBE:
4903                 spec->init_amp = ALC_INIT_NONE;
4904                 break;
4905         }
4906 }
4907
4908 static const struct alc_fixup alc260_fixups[] = {
4909         [ALC260_FIXUP_HP_DC5750] = {
4910                 .type = ALC_FIXUP_PINS,
4911                 .v.pins = (const struct alc_pincfg[]) {
4912                         { 0x11, 0x90130110 }, /* speaker */
4913                         { }
4914                 }
4915         },
4916         [ALC260_FIXUP_HP_PIN_0F] = {
4917                 .type = ALC_FIXUP_PINS,
4918                 .v.pins = (const struct alc_pincfg[]) {
4919                         { 0x0f, 0x01214000 }, /* HP */
4920                         { }
4921                 }
4922         },
4923         [ALC260_FIXUP_COEF] = {
4924                 .type = ALC_FIXUP_VERBS,
4925                 .v.verbs = (const struct hda_verb[]) {
4926                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4927                         { 0x20, AC_VERB_SET_PROC_COEF,  0x3040 },
4928                         { }
4929                 },
4930                 .chained = true,
4931                 .chain_id = ALC260_FIXUP_HP_PIN_0F,
4932         },
4933         [ALC260_FIXUP_GPIO1] = {
4934                 .type = ALC_FIXUP_VERBS,
4935                 .v.verbs = alc_gpio1_init_verbs,
4936         },
4937         [ALC260_FIXUP_GPIO1_TOGGLE] = {
4938                 .type = ALC_FIXUP_FUNC,
4939                 .v.func = alc260_fixup_gpio1_toggle,
4940                 .chained = true,
4941                 .chain_id = ALC260_FIXUP_HP_PIN_0F,
4942         },
4943         [ALC260_FIXUP_REPLACER] = {
4944                 .type = ALC_FIXUP_VERBS,
4945                 .v.verbs = (const struct hda_verb[]) {
4946                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
4947                         { 0x20, AC_VERB_SET_PROC_COEF,  0x3050 },
4948                         { }
4949                 },
4950                 .chained = true,
4951                 .chain_id = ALC260_FIXUP_GPIO1_TOGGLE,
4952         },
4953         [ALC260_FIXUP_HP_B1900] = {
4954                 .type = ALC_FIXUP_FUNC,
4955                 .v.func = alc260_fixup_gpio1_toggle,
4956                 .chained = true,
4957                 .chain_id = ALC260_FIXUP_COEF,
4958         },
4959         [ALC260_FIXUP_KN1] = {
4960                 .type = ALC_FIXUP_FUNC,
4961                 .v.func = alc260_fixup_kn1,
4962         },
4963 };
4964
4965 static const struct snd_pci_quirk alc260_fixup_tbl[] = {
4966         SND_PCI_QUIRK(0x1025, 0x007b, "Acer C20x", ALC260_FIXUP_GPIO1),
4967         SND_PCI_QUIRK(0x1025, 0x007f, "Acer Aspire 9500", ALC260_FIXUP_COEF),
4968         SND_PCI_QUIRK(0x1025, 0x008f, "Acer", ALC260_FIXUP_GPIO1),
4969         SND_PCI_QUIRK(0x103c, 0x280a, "HP dc5750", ALC260_FIXUP_HP_DC5750),
4970         SND_PCI_QUIRK(0x103c, 0x30ba, "HP Presario B1900", ALC260_FIXUP_HP_B1900),
4971         SND_PCI_QUIRK(0x1509, 0x4540, "Favorit 100XS", ALC260_FIXUP_GPIO1),
4972         SND_PCI_QUIRK(0x152d, 0x0729, "Quanta KN1", ALC260_FIXUP_KN1),
4973         SND_PCI_QUIRK(0x161f, 0x2057, "Replacer 672V", ALC260_FIXUP_REPLACER),
4974         SND_PCI_QUIRK(0x1631, 0xc017, "PB V7900", ALC260_FIXUP_COEF),
4975         {}
4976 };
4977
4978 /*
4979  */
4980 static int patch_alc260(struct hda_codec *codec)
4981 {
4982         struct alc_spec *spec;
4983         int err;
4984
4985         err = alc_alloc_spec(codec, 0x07);
4986         if (err < 0)
4987                 return err;
4988
4989         spec = codec->spec;
4990
4991         alc_pick_fixup(codec, NULL, alc260_fixup_tbl, alc260_fixups);
4992         alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
4993
4994         /* automatic parse from the BIOS config */
4995         err = alc260_parse_auto_config(codec);
4996         if (err < 0)
4997                 goto error;
4998
4999         if (!spec->no_analog) {
5000                 err = snd_hda_attach_beep_device(codec, 0x1);
5001                 if (err < 0)
5002                         goto error;
5003                 set_beep_amp(spec, 0x07, 0x05, HDA_INPUT);
5004         }
5005
5006         codec->patch_ops = alc_patch_ops;
5007         spec->shutup = alc_eapd_shutup;
5008
5009         alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
5010
5011         return 0;
5012
5013  error:
5014         alc_free(codec);
5015         return err;
5016 }
5017
5018
5019 /*
5020  * ALC882/883/885/888/889 support
5021  *
5022  * ALC882 is almost identical with ALC880 but has cleaner and more flexible
5023  * configuration.  Each pin widget can choose any input DACs and a mixer.
5024  * Each ADC is connected from a mixer of all inputs.  This makes possible
5025  * 6-channel independent captures.
5026  *
5027  * In addition, an independent DAC for the multi-playback (not used in this
5028  * driver yet).
5029  */
5030
5031 /*
5032  * Pin config fixes
5033  */
5034 enum {
5035         ALC882_FIXUP_ABIT_AW9D_MAX,
5036         ALC882_FIXUP_LENOVO_Y530,
5037         ALC882_FIXUP_PB_M5210,
5038         ALC882_FIXUP_ACER_ASPIRE_7736,
5039         ALC882_FIXUP_ASUS_W90V,
5040         ALC889_FIXUP_CD,
5041         ALC889_FIXUP_VAIO_TT,
5042         ALC888_FIXUP_EEE1601,
5043         ALC882_FIXUP_EAPD,
5044         ALC883_FIXUP_EAPD,
5045         ALC883_FIXUP_ACER_EAPD,
5046         ALC882_FIXUP_GPIO1,
5047         ALC882_FIXUP_GPIO2,
5048         ALC882_FIXUP_GPIO3,
5049         ALC889_FIXUP_COEF,
5050         ALC882_FIXUP_ASUS_W2JC,
5051         ALC882_FIXUP_ACER_ASPIRE_4930G,
5052         ALC882_FIXUP_ACER_ASPIRE_8930G,
5053         ALC882_FIXUP_ASPIRE_8930G_VERBS,
5054         ALC885_FIXUP_MACPRO_GPIO,
5055         ALC889_FIXUP_DAC_ROUTE,
5056         ALC889_FIXUP_MBP_VREF,
5057         ALC889_FIXUP_IMAC91_VREF,
5058         ALC882_FIXUP_INV_DMIC,
5059         ALC882_FIXUP_NO_PRIMARY_HP,
5060 };
5061
5062 static void alc889_fixup_coef(struct hda_codec *codec,
5063                               const struct alc_fixup *fix, int action)
5064 {
5065         if (action != ALC_FIXUP_ACT_INIT)
5066                 return;
5067         alc889_coef_init(codec);
5068 }
5069
5070 /* toggle speaker-output according to the hp-jack state */
5071 static void alc882_gpio_mute(struct hda_codec *codec, int pin, int muted)
5072 {
5073         unsigned int gpiostate, gpiomask, gpiodir;
5074
5075         gpiostate = snd_hda_codec_read(codec, codec->afg, 0,
5076                                        AC_VERB_GET_GPIO_DATA, 0);
5077
5078         if (!muted)
5079                 gpiostate |= (1 << pin);
5080         else
5081                 gpiostate &= ~(1 << pin);
5082
5083         gpiomask = snd_hda_codec_read(codec, codec->afg, 0,
5084                                       AC_VERB_GET_GPIO_MASK, 0);
5085         gpiomask |= (1 << pin);
5086
5087         gpiodir = snd_hda_codec_read(codec, codec->afg, 0,
5088                                      AC_VERB_GET_GPIO_DIRECTION, 0);
5089         gpiodir |= (1 << pin);
5090
5091
5092         snd_hda_codec_write(codec, codec->afg, 0,
5093                             AC_VERB_SET_GPIO_MASK, gpiomask);
5094         snd_hda_codec_write(codec, codec->afg, 0,
5095                             AC_VERB_SET_GPIO_DIRECTION, gpiodir);
5096
5097         msleep(1);
5098
5099         snd_hda_codec_write(codec, codec->afg, 0,
5100                             AC_VERB_SET_GPIO_DATA, gpiostate);
5101 }
5102
5103 /* set up GPIO at initialization */
5104 static void alc885_fixup_macpro_gpio(struct hda_codec *codec,
5105                                      const struct alc_fixup *fix, int action)
5106 {
5107         if (action != ALC_FIXUP_ACT_INIT)
5108                 return;
5109         alc882_gpio_mute(codec, 0, 0);
5110         alc882_gpio_mute(codec, 1, 0);
5111 }
5112
5113 /* Fix the connection of some pins for ALC889:
5114  * At least, Acer Aspire 5935 shows the connections to DAC3/4 don't
5115  * work correctly (bko#42740)
5116  */
5117 static void alc889_fixup_dac_route(struct hda_codec *codec,
5118                                    const struct alc_fixup *fix, int action)
5119 {
5120         if (action == ALC_FIXUP_ACT_PRE_PROBE) {
5121                 /* fake the connections during parsing the tree */
5122                 hda_nid_t conn1[2] = { 0x0c, 0x0d };
5123                 hda_nid_t conn2[2] = { 0x0e, 0x0f };
5124                 snd_hda_override_conn_list(codec, 0x14, 2, conn1);
5125                 snd_hda_override_conn_list(codec, 0x15, 2, conn1);
5126                 snd_hda_override_conn_list(codec, 0x18, 2, conn2);
5127                 snd_hda_override_conn_list(codec, 0x1a, 2, conn2);
5128         } else if (action == ALC_FIXUP_ACT_PROBE) {
5129                 /* restore the connections */
5130                 hda_nid_t conn[5] = { 0x0c, 0x0d, 0x0e, 0x0f, 0x26 };
5131                 snd_hda_override_conn_list(codec, 0x14, 5, conn);
5132                 snd_hda_override_conn_list(codec, 0x15, 5, conn);
5133                 snd_hda_override_conn_list(codec, 0x18, 5, conn);
5134                 snd_hda_override_conn_list(codec, 0x1a, 5, conn);
5135         }
5136 }
5137
5138 /* Set VREF on HP pin */
5139 static void alc889_fixup_mbp_vref(struct hda_codec *codec,
5140                                   const struct alc_fixup *fix, int action)
5141 {
5142         struct alc_spec *spec = codec->spec;
5143         static hda_nid_t nids[2] = { 0x14, 0x15 };
5144         int i;
5145
5146         if (action != ALC_FIXUP_ACT_INIT)
5147                 return;
5148         for (i = 0; i < ARRAY_SIZE(nids); i++) {
5149                 unsigned int val = snd_hda_codec_get_pincfg(codec, nids[i]);
5150                 if (get_defcfg_device(val) != AC_JACK_HP_OUT)
5151                         continue;
5152                 val = snd_hda_codec_read(codec, nids[i], 0,
5153                                          AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5154                 val |= AC_PINCTL_VREF_80;
5155                 snd_hda_set_pin_ctl(codec, nids[i], val);
5156                 spec->keep_vref_in_automute = 1;
5157                 break;
5158         }
5159 }
5160
5161 /* Set VREF on speaker pins on imac91 */
5162 static void alc889_fixup_imac91_vref(struct hda_codec *codec,
5163                                      const struct alc_fixup *fix, int action)
5164 {
5165         struct alc_spec *spec = codec->spec;
5166         static hda_nid_t nids[2] = { 0x18, 0x1a };
5167         int i;
5168
5169         if (action != ALC_FIXUP_ACT_INIT)
5170                 return;
5171         for (i = 0; i < ARRAY_SIZE(nids); i++) {
5172                 unsigned int val;
5173                 val = snd_hda_codec_read(codec, nids[i], 0,
5174                                          AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
5175                 val |= AC_PINCTL_VREF_50;
5176                 snd_hda_set_pin_ctl(codec, nids[i], val);
5177         }
5178         spec->keep_vref_in_automute = 1;
5179 }
5180
5181 /* Don't take HP output as primary
5182  * strangely, the speaker output doesn't work on VAIO Z through DAC 0x05
5183  */
5184 static void alc882_fixup_no_primary_hp(struct hda_codec *codec,
5185                                        const struct alc_fixup *fix, int action)
5186 {
5187         struct alc_spec *spec = codec->spec;
5188         if (action == ALC_FIXUP_ACT_PRE_PROBE)
5189                 spec->no_primary_hp = 1;
5190 }
5191
5192 static const struct alc_fixup alc882_fixups[] = {
5193         [ALC882_FIXUP_ABIT_AW9D_MAX] = {
5194                 .type = ALC_FIXUP_PINS,
5195                 .v.pins = (const struct alc_pincfg[]) {
5196                         { 0x15, 0x01080104 }, /* side */
5197                         { 0x16, 0x01011012 }, /* rear */
5198                         { 0x17, 0x01016011 }, /* clfe */
5199                         { }
5200                 }
5201         },
5202         [ALC882_FIXUP_LENOVO_Y530] = {
5203                 .type = ALC_FIXUP_PINS,
5204                 .v.pins = (const struct alc_pincfg[]) {
5205                         { 0x15, 0x99130112 }, /* rear int speakers */
5206                         { 0x16, 0x99130111 }, /* subwoofer */
5207                         { }
5208                 }
5209         },
5210         [ALC882_FIXUP_PB_M5210] = {
5211                 .type = ALC_FIXUP_VERBS,
5212                 .v.verbs = (const struct hda_verb[]) {
5213                         { 0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF50 },
5214                         {}
5215                 }
5216         },
5217         [ALC882_FIXUP_ACER_ASPIRE_7736] = {
5218                 .type = ALC_FIXUP_FUNC,
5219                 .v.func = alc_fixup_sku_ignore,
5220         },
5221         [ALC882_FIXUP_ASUS_W90V] = {
5222                 .type = ALC_FIXUP_PINS,
5223                 .v.pins = (const struct alc_pincfg[]) {
5224                         { 0x16, 0x99130110 }, /* fix sequence for CLFE */
5225                         { }
5226                 }
5227         },
5228         [ALC889_FIXUP_CD] = {
5229                 .type = ALC_FIXUP_PINS,
5230                 .v.pins = (const struct alc_pincfg[]) {
5231                         { 0x1c, 0x993301f0 }, /* CD */
5232                         { }
5233                 }
5234         },
5235         [ALC889_FIXUP_VAIO_TT] = {
5236                 .type = ALC_FIXUP_PINS,
5237                 .v.pins = (const struct alc_pincfg[]) {
5238                         { 0x17, 0x90170111 }, /* hidden surround speaker */
5239                         { }
5240                 }
5241         },
5242         [ALC888_FIXUP_EEE1601] = {
5243                 .type = ALC_FIXUP_VERBS,
5244                 .v.verbs = (const struct hda_verb[]) {
5245                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x0b },
5246                         { 0x20, AC_VERB_SET_PROC_COEF,  0x0838 },
5247                         { }
5248                 }
5249         },
5250         [ALC882_FIXUP_EAPD] = {
5251                 .type = ALC_FIXUP_VERBS,
5252                 .v.verbs = (const struct hda_verb[]) {
5253                         /* change to EAPD mode */
5254                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5255                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3060 },
5256                         { }
5257                 }
5258         },
5259         [ALC883_FIXUP_EAPD] = {
5260                 .type = ALC_FIXUP_VERBS,
5261                 .v.verbs = (const struct hda_verb[]) {
5262                         /* change to EAPD mode */
5263                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5264                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3070 },
5265                         { }
5266                 }
5267         },
5268         [ALC883_FIXUP_ACER_EAPD] = {
5269                 .type = ALC_FIXUP_VERBS,
5270                 .v.verbs = (const struct hda_verb[]) {
5271                         /* eanable EAPD on Acer laptops */
5272                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5273                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3050 },
5274                         { }
5275                 }
5276         },
5277         [ALC882_FIXUP_GPIO1] = {
5278                 .type = ALC_FIXUP_VERBS,
5279                 .v.verbs = alc_gpio1_init_verbs,
5280         },
5281         [ALC882_FIXUP_GPIO2] = {
5282                 .type = ALC_FIXUP_VERBS,
5283                 .v.verbs = alc_gpio2_init_verbs,
5284         },
5285         [ALC882_FIXUP_GPIO3] = {
5286                 .type = ALC_FIXUP_VERBS,
5287                 .v.verbs = alc_gpio3_init_verbs,
5288         },
5289         [ALC882_FIXUP_ASUS_W2JC] = {
5290                 .type = ALC_FIXUP_VERBS,
5291                 .v.verbs = alc_gpio1_init_verbs,
5292                 .chained = true,
5293                 .chain_id = ALC882_FIXUP_EAPD,
5294         },
5295         [ALC889_FIXUP_COEF] = {
5296                 .type = ALC_FIXUP_FUNC,
5297                 .v.func = alc889_fixup_coef,
5298         },
5299         [ALC882_FIXUP_ACER_ASPIRE_4930G] = {
5300                 .type = ALC_FIXUP_PINS,
5301                 .v.pins = (const struct alc_pincfg[]) {
5302                         { 0x16, 0x99130111 }, /* CLFE speaker */
5303                         { 0x17, 0x99130112 }, /* surround speaker */
5304                         { }
5305                 },
5306                 .chained = true,
5307                 .chain_id = ALC882_FIXUP_GPIO1,
5308         },
5309         [ALC882_FIXUP_ACER_ASPIRE_8930G] = {
5310                 .type = ALC_FIXUP_PINS,
5311                 .v.pins = (const struct alc_pincfg[]) {
5312                         { 0x16, 0x99130111 }, /* CLFE speaker */
5313                         { 0x1b, 0x99130112 }, /* surround speaker */
5314                         { }
5315                 },
5316                 .chained = true,
5317                 .chain_id = ALC882_FIXUP_ASPIRE_8930G_VERBS,
5318         },
5319         [ALC882_FIXUP_ASPIRE_8930G_VERBS] = {
5320                 /* additional init verbs for Acer Aspire 8930G */
5321                 .type = ALC_FIXUP_VERBS,
5322                 .v.verbs = (const struct hda_verb[]) {
5323                         /* Enable all DACs */
5324                         /* DAC DISABLE/MUTE 1? */
5325                         /*  setting bits 1-5 disables DAC nids 0x02-0x06
5326                          *  apparently. Init=0x38 */
5327                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x03 },
5328                         { 0x20, AC_VERB_SET_PROC_COEF, 0x0000 },
5329                         /* DAC DISABLE/MUTE 2? */
5330                         /*  some bit here disables the other DACs.
5331                          *  Init=0x4900 */
5332                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x08 },
5333                         { 0x20, AC_VERB_SET_PROC_COEF, 0x0000 },
5334                         /* DMIC fix
5335                          * This laptop has a stereo digital microphone.
5336                          * The mics are only 1cm apart which makes the stereo
5337                          * useless. However, either the mic or the ALC889
5338                          * makes the signal become a difference/sum signal
5339                          * instead of standard stereo, which is annoying.
5340                          * So instead we flip this bit which makes the
5341                          * codec replicate the sum signal to both channels,
5342                          * turning it into a normal mono mic.
5343                          */
5344                         /* DMIC_CONTROL? Init value = 0x0001 */
5345                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x0b },
5346                         { 0x20, AC_VERB_SET_PROC_COEF, 0x0003 },
5347                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5348                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3050 },
5349                         { }
5350                 },
5351                 .chained = true,
5352                 .chain_id = ALC882_FIXUP_GPIO1,
5353         },
5354         [ALC885_FIXUP_MACPRO_GPIO] = {
5355                 .type = ALC_FIXUP_FUNC,
5356                 .v.func = alc885_fixup_macpro_gpio,
5357         },
5358         [ALC889_FIXUP_DAC_ROUTE] = {
5359                 .type = ALC_FIXUP_FUNC,
5360                 .v.func = alc889_fixup_dac_route,
5361         },
5362         [ALC889_FIXUP_MBP_VREF] = {
5363                 .type = ALC_FIXUP_FUNC,
5364                 .v.func = alc889_fixup_mbp_vref,
5365                 .chained = true,
5366                 .chain_id = ALC882_FIXUP_GPIO1,
5367         },
5368         [ALC889_FIXUP_IMAC91_VREF] = {
5369                 .type = ALC_FIXUP_FUNC,
5370                 .v.func = alc889_fixup_imac91_vref,
5371                 .chained = true,
5372                 .chain_id = ALC882_FIXUP_GPIO1,
5373         },
5374         [ALC882_FIXUP_INV_DMIC] = {
5375                 .type = ALC_FIXUP_FUNC,
5376                 .v.func = alc_fixup_inv_dmic_0x12,
5377         },
5378         [ALC882_FIXUP_NO_PRIMARY_HP] = {
5379                 .type = ALC_FIXUP_FUNC,
5380                 .v.func = alc882_fixup_no_primary_hp,
5381         },
5382 };
5383
5384 static const struct snd_pci_quirk alc882_fixup_tbl[] = {
5385         SND_PCI_QUIRK(0x1025, 0x006c, "Acer Aspire 9810", ALC883_FIXUP_ACER_EAPD),
5386         SND_PCI_QUIRK(0x1025, 0x0090, "Acer Aspire", ALC883_FIXUP_ACER_EAPD),
5387         SND_PCI_QUIRK(0x1025, 0x010a, "Acer Ferrari 5000", ALC883_FIXUP_ACER_EAPD),
5388         SND_PCI_QUIRK(0x1025, 0x0110, "Acer Aspire", ALC883_FIXUP_ACER_EAPD),
5389         SND_PCI_QUIRK(0x1025, 0x0112, "Acer Aspire 9303", ALC883_FIXUP_ACER_EAPD),
5390         SND_PCI_QUIRK(0x1025, 0x0121, "Acer Aspire 5920G", ALC883_FIXUP_ACER_EAPD),
5391         SND_PCI_QUIRK(0x1025, 0x013e, "Acer Aspire 4930G",
5392                       ALC882_FIXUP_ACER_ASPIRE_4930G),
5393         SND_PCI_QUIRK(0x1025, 0x013f, "Acer Aspire 5930G",
5394                       ALC882_FIXUP_ACER_ASPIRE_4930G),
5395         SND_PCI_QUIRK(0x1025, 0x0145, "Acer Aspire 8930G",
5396                       ALC882_FIXUP_ACER_ASPIRE_8930G),
5397         SND_PCI_QUIRK(0x1025, 0x0146, "Acer Aspire 6935G",
5398                       ALC882_FIXUP_ACER_ASPIRE_8930G),
5399         SND_PCI_QUIRK(0x1025, 0x015e, "Acer Aspire 6930G",
5400                       ALC882_FIXUP_ACER_ASPIRE_4930G),
5401         SND_PCI_QUIRK(0x1025, 0x0166, "Acer Aspire 6530G",
5402                       ALC882_FIXUP_ACER_ASPIRE_4930G),
5403         SND_PCI_QUIRK(0x1025, 0x0142, "Acer Aspire 7730G",
5404                       ALC882_FIXUP_ACER_ASPIRE_4930G),
5405         SND_PCI_QUIRK(0x1025, 0x0155, "Packard-Bell M5120", ALC882_FIXUP_PB_M5210),
5406         SND_PCI_QUIRK(0x1025, 0x021e, "Acer Aspire 5739G",
5407                       ALC882_FIXUP_ACER_ASPIRE_4930G),
5408         SND_PCI_QUIRK(0x1025, 0x0259, "Acer Aspire 5935", ALC889_FIXUP_DAC_ROUTE),
5409         SND_PCI_QUIRK(0x1025, 0x026b, "Acer Aspire 8940G", ALC882_FIXUP_ACER_ASPIRE_8930G),
5410         SND_PCI_QUIRK(0x1025, 0x0296, "Acer Aspire 7736z", ALC882_FIXUP_ACER_ASPIRE_7736),
5411         SND_PCI_QUIRK(0x1043, 0x13c2, "Asus A7M", ALC882_FIXUP_EAPD),
5412         SND_PCI_QUIRK(0x1043, 0x1873, "ASUS W90V", ALC882_FIXUP_ASUS_W90V),
5413         SND_PCI_QUIRK(0x1043, 0x1971, "Asus W2JC", ALC882_FIXUP_ASUS_W2JC),
5414         SND_PCI_QUIRK(0x1043, 0x835f, "Asus Eee 1601", ALC888_FIXUP_EEE1601),
5415         SND_PCI_QUIRK(0x104d, 0x9047, "Sony Vaio TT", ALC889_FIXUP_VAIO_TT),
5416         SND_PCI_QUIRK(0x104d, 0x905a, "Sony Vaio Z", ALC882_FIXUP_NO_PRIMARY_HP),
5417
5418         /* All Apple entries are in codec SSIDs */
5419         SND_PCI_QUIRK(0x106b, 0x00a0, "MacBookPro 3,1", ALC889_FIXUP_MBP_VREF),
5420         SND_PCI_QUIRK(0x106b, 0x00a1, "Macbook", ALC889_FIXUP_MBP_VREF),
5421         SND_PCI_QUIRK(0x106b, 0x00a4, "MacbookPro 4,1", ALC889_FIXUP_MBP_VREF),
5422         SND_PCI_QUIRK(0x106b, 0x0c00, "Mac Pro", ALC885_FIXUP_MACPRO_GPIO),
5423         SND_PCI_QUIRK(0x106b, 0x1000, "iMac 24", ALC885_FIXUP_MACPRO_GPIO),
5424         SND_PCI_QUIRK(0x106b, 0x2800, "AppleTV", ALC885_FIXUP_MACPRO_GPIO),
5425         SND_PCI_QUIRK(0x106b, 0x2c00, "MacbookPro rev3", ALC889_FIXUP_MBP_VREF),
5426         SND_PCI_QUIRK(0x106b, 0x3000, "iMac", ALC889_FIXUP_MBP_VREF),
5427         SND_PCI_QUIRK(0x106b, 0x3200, "iMac 7,1 Aluminum", ALC882_FIXUP_EAPD),
5428         SND_PCI_QUIRK(0x106b, 0x3400, "MacBookAir 1,1", ALC889_FIXUP_MBP_VREF),
5429         SND_PCI_QUIRK(0x106b, 0x3500, "MacBookAir 2,1", ALC889_FIXUP_MBP_VREF),
5430         SND_PCI_QUIRK(0x106b, 0x3600, "Macbook 3,1", ALC889_FIXUP_MBP_VREF),
5431         SND_PCI_QUIRK(0x106b, 0x3800, "MacbookPro 4,1", ALC889_FIXUP_MBP_VREF),
5432         SND_PCI_QUIRK(0x106b, 0x3e00, "iMac 24 Aluminum", ALC885_FIXUP_MACPRO_GPIO),
5433         SND_PCI_QUIRK(0x106b, 0x3f00, "Macbook 5,1", ALC889_FIXUP_IMAC91_VREF),
5434         SND_PCI_QUIRK(0x106b, 0x4000, "MacbookPro 5,1", ALC889_FIXUP_IMAC91_VREF),
5435         SND_PCI_QUIRK(0x106b, 0x4100, "Macmini 3,1", ALC889_FIXUP_IMAC91_VREF),
5436         SND_PCI_QUIRK(0x106b, 0x4200, "Mac Pro 5,1", ALC885_FIXUP_MACPRO_GPIO),
5437         SND_PCI_QUIRK(0x106b, 0x4600, "MacbookPro 5,2", ALC889_FIXUP_IMAC91_VREF),
5438         SND_PCI_QUIRK(0x106b, 0x4900, "iMac 9,1 Aluminum", ALC889_FIXUP_IMAC91_VREF),
5439         SND_PCI_QUIRK(0x106b, 0x4a00, "Macbook 5,2", ALC889_FIXUP_IMAC91_VREF),
5440
5441         SND_PCI_QUIRK(0x1071, 0x8258, "Evesham Voyaeger", ALC882_FIXUP_EAPD),
5442         SND_PCI_QUIRK(0x1462, 0x7350, "MSI-7350", ALC889_FIXUP_CD),
5443         SND_PCI_QUIRK_VENDOR(0x1462, "MSI", ALC882_FIXUP_GPIO3),
5444         SND_PCI_QUIRK(0x1458, 0xa002, "Gigabyte EP45-DS3", ALC889_FIXUP_CD),
5445         SND_PCI_QUIRK(0x147b, 0x107a, "Abit AW9D-MAX", ALC882_FIXUP_ABIT_AW9D_MAX),
5446         SND_PCI_QUIRK_VENDOR(0x1558, "Clevo laptop", ALC882_FIXUP_EAPD),
5447         SND_PCI_QUIRK(0x161f, 0x2054, "Medion laptop", ALC883_FIXUP_EAPD),
5448         SND_PCI_QUIRK(0x17aa, 0x3a0d, "Lenovo Y530", ALC882_FIXUP_LENOVO_Y530),
5449         SND_PCI_QUIRK(0x8086, 0x0022, "DX58SO", ALC889_FIXUP_COEF),
5450         {}
5451 };
5452
5453 static const struct alc_model_fixup alc882_fixup_models[] = {
5454         {.id = ALC882_FIXUP_ACER_ASPIRE_4930G, .name = "acer-aspire-4930g"},
5455         {.id = ALC882_FIXUP_ACER_ASPIRE_8930G, .name = "acer-aspire-8930g"},
5456         {.id = ALC883_FIXUP_ACER_EAPD, .name = "acer-aspire"},
5457         {.id = ALC882_FIXUP_INV_DMIC, .name = "inv-dmic"},
5458         {.id = ALC882_FIXUP_NO_PRIMARY_HP, .name = "no-primary-hp"},
5459         {}
5460 };
5461
5462 /*
5463  * BIOS auto configuration
5464  */
5465 /* almost identical with ALC880 parser... */
5466 static int alc882_parse_auto_config(struct hda_codec *codec)
5467 {
5468         static const hda_nid_t alc882_ignore[] = { 0x1d, 0 };
5469         static const hda_nid_t alc882_ssids[] = { 0x15, 0x1b, 0x14, 0 };
5470         return alc_parse_auto_config(codec, alc882_ignore, alc882_ssids);
5471 }
5472
5473 /*
5474  */
5475 static int patch_alc882(struct hda_codec *codec)
5476 {
5477         struct alc_spec *spec;
5478         int err;
5479
5480         err = alc_alloc_spec(codec, 0x0b);
5481         if (err < 0)
5482                 return err;
5483
5484         spec = codec->spec;
5485
5486         switch (codec->vendor_id) {
5487         case 0x10ec0882:
5488         case 0x10ec0885:
5489                 break;
5490         default:
5491                 /* ALC883 and variants */
5492                 alc_fix_pll_init(codec, 0x20, 0x0a, 10);
5493                 break;
5494         }
5495
5496         alc_pick_fixup(codec, alc882_fixup_models, alc882_fixup_tbl,
5497                        alc882_fixups);
5498         alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
5499
5500         alc_auto_parse_customize_define(codec);
5501
5502         /* automatic parse from the BIOS config */
5503         err = alc882_parse_auto_config(codec);
5504         if (err < 0)
5505                 goto error;
5506
5507         if (!spec->no_analog && has_cdefine_beep(codec)) {
5508                 err = snd_hda_attach_beep_device(codec, 0x1);
5509                 if (err < 0)
5510                         goto error;
5511                 set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
5512         }
5513
5514         codec->patch_ops = alc_patch_ops;
5515
5516         alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
5517
5518         return 0;
5519
5520  error:
5521         alc_free(codec);
5522         return err;
5523 }
5524
5525
5526 /*
5527  * ALC262 support
5528  */
5529 static int alc262_parse_auto_config(struct hda_codec *codec)
5530 {
5531         static const hda_nid_t alc262_ignore[] = { 0x1d, 0 };
5532         static const hda_nid_t alc262_ssids[] = { 0x15, 0x1b, 0x14, 0 };
5533         return alc_parse_auto_config(codec, alc262_ignore, alc262_ssids);
5534 }
5535
5536 /*
5537  * Pin config fixes
5538  */
5539 enum {
5540         ALC262_FIXUP_FSC_H270,
5541         ALC262_FIXUP_HP_Z200,
5542         ALC262_FIXUP_TYAN,
5543         ALC262_FIXUP_LENOVO_3000,
5544         ALC262_FIXUP_BENQ,
5545         ALC262_FIXUP_BENQ_T31,
5546         ALC262_FIXUP_INV_DMIC,
5547 };
5548
5549 static const struct alc_fixup alc262_fixups[] = {
5550         [ALC262_FIXUP_FSC_H270] = {
5551                 .type = ALC_FIXUP_PINS,
5552                 .v.pins = (const struct alc_pincfg[]) {
5553                         { 0x14, 0x99130110 }, /* speaker */
5554                         { 0x15, 0x0221142f }, /* front HP */
5555                         { 0x1b, 0x0121141f }, /* rear HP */
5556                         { }
5557                 }
5558         },
5559         [ALC262_FIXUP_HP_Z200] = {
5560                 .type = ALC_FIXUP_PINS,
5561                 .v.pins = (const struct alc_pincfg[]) {
5562                         { 0x16, 0x99130120 }, /* internal speaker */
5563                         { }
5564                 }
5565         },
5566         [ALC262_FIXUP_TYAN] = {
5567                 .type = ALC_FIXUP_PINS,
5568                 .v.pins = (const struct alc_pincfg[]) {
5569                         { 0x14, 0x1993e1f0 }, /* int AUX */
5570                         { }
5571                 }
5572         },
5573         [ALC262_FIXUP_LENOVO_3000] = {
5574                 .type = ALC_FIXUP_VERBS,
5575                 .v.verbs = (const struct hda_verb[]) {
5576                         { 0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF50 },
5577                         {}
5578                 },
5579                 .chained = true,
5580                 .chain_id = ALC262_FIXUP_BENQ,
5581         },
5582         [ALC262_FIXUP_BENQ] = {
5583                 .type = ALC_FIXUP_VERBS,
5584                 .v.verbs = (const struct hda_verb[]) {
5585                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5586                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3070 },
5587                         {}
5588                 }
5589         },
5590         [ALC262_FIXUP_BENQ_T31] = {
5591                 .type = ALC_FIXUP_VERBS,
5592                 .v.verbs = (const struct hda_verb[]) {
5593                         { 0x20, AC_VERB_SET_COEF_INDEX, 0x07 },
5594                         { 0x20, AC_VERB_SET_PROC_COEF, 0x3050 },
5595                         {}
5596                 }
5597         },
5598         [ALC262_FIXUP_INV_DMIC] = {
5599                 .type = ALC_FIXUP_FUNC,
5600                 .v.func = alc_fixup_inv_dmic_0x12,
5601         },
5602 };
5603
5604 static const struct snd_pci_quirk alc262_fixup_tbl[] = {
5605         SND_PCI_QUIRK(0x103c, 0x170b, "HP Z200", ALC262_FIXUP_HP_Z200),
5606         SND_PCI_QUIRK(0x10cf, 0x1397, "Fujitsu", ALC262_FIXUP_BENQ),
5607         SND_PCI_QUIRK(0x10cf, 0x142d, "Fujitsu Lifebook E8410", ALC262_FIXUP_BENQ),
5608         SND_PCI_QUIRK(0x10f1, 0x2915, "Tyan Thunder n6650W", ALC262_FIXUP_TYAN),
5609         SND_PCI_QUIRK(0x1734, 0x1147, "FSC Celsius H270", ALC262_FIXUP_FSC_H270),
5610         SND_PCI_QUIRK(0x17aa, 0x384e, "Lenovo 3000", ALC262_FIXUP_LENOVO_3000),
5611         SND_PCI_QUIRK(0x17ff, 0x0560, "Benq ED8", ALC262_FIXUP_BENQ),
5612         SND_PCI_QUIRK(0x17ff, 0x058d, "Benq T31-16", ALC262_FIXUP_BENQ_T31),
5613         {}
5614 };
5615
5616 static const struct alc_model_fixup alc262_fixup_models[] = {
5617         {.id = ALC262_FIXUP_INV_DMIC, .name = "inv-dmic"},
5618         {}
5619 };
5620
5621 /*
5622  */
5623 static int patch_alc262(struct hda_codec *codec)
5624 {
5625         struct alc_spec *spec;
5626         int err;
5627
5628         err = alc_alloc_spec(codec, 0x0b);
5629         if (err < 0)
5630                 return err;
5631
5632         spec = codec->spec;
5633
5634 #if 0
5635         /* pshou 07/11/05  set a zero PCM sample to DAC when FIFO is
5636          * under-run
5637          */
5638         {
5639         int tmp;
5640         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_COEF_INDEX, 7);
5641         tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
5642         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_COEF_INDEX, 7);
5643         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PROC_COEF, tmp | 0x80);
5644         }
5645 #endif
5646         alc_fix_pll_init(codec, 0x20, 0x0a, 10);
5647
5648         alc_pick_fixup(codec, alc262_fixup_models, alc262_fixup_tbl,
5649                        alc262_fixups);
5650         alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
5651
5652         alc_auto_parse_customize_define(codec);
5653
5654         /* automatic parse from the BIOS config */
5655         err = alc262_parse_auto_config(codec);
5656         if (err < 0)
5657                 goto error;
5658
5659         if (!spec->no_analog && has_cdefine_beep(codec)) {
5660                 err = snd_hda_attach_beep_device(codec, 0x1);
5661                 if (err < 0)
5662                         goto error;
5663                 set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
5664         }
5665
5666         codec->patch_ops = alc_patch_ops;
5667         spec->shutup = alc_eapd_shutup;
5668
5669         alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
5670
5671         return 0;
5672
5673  error:
5674         alc_free(codec);
5675         return err;
5676 }
5677
5678 /*
5679  *  ALC268
5680  */
5681 /* bind Beep switches of both NID 0x0f and 0x10 */
5682 static const struct hda_bind_ctls alc268_bind_beep_sw = {
5683         .ops = &snd_hda_bind_sw,
5684         .values = {
5685                 HDA_COMPOSE_AMP_VAL(0x0f, 3, 1, HDA_INPUT),
5686                 HDA_COMPOSE_AMP_VAL(0x10, 3, 1, HDA_INPUT),
5687                 0
5688         },
5689 };
5690
5691 static const struct snd_kcontrol_new alc268_beep_mixer[] = {
5692         HDA_CODEC_VOLUME("Beep Playback Volume", 0x1d, 0x0, HDA_INPUT),
5693         HDA_BIND_SW("Beep Playback Switch", &alc268_bind_beep_sw),
5694         { }
5695 };
5696
5697 /* set PCBEEP vol = 0, mute connections */
5698 static const struct hda_verb alc268_beep_init_verbs[] = {
5699         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
5700         {0x0f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5701         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
5702         { }
5703 };
5704
5705 enum {
5706         ALC268_FIXUP_INV_DMIC,
5707         ALC268_FIXUP_HP_EAPD,
5708 };
5709
5710 static const struct alc_fixup alc268_fixups[] = {
5711         [ALC268_FIXUP_INV_DMIC] = {
5712                 .type = ALC_FIXUP_FUNC,
5713                 .v.func = alc_fixup_inv_dmic_0x12,
5714         },
5715         [ALC268_FIXUP_HP_EAPD] = {
5716                 .type = ALC_FIXUP_VERBS,
5717                 .v.verbs = (const struct hda_verb[]) {
5718                         {0x15, AC_VERB_SET_EAPD_BTLENABLE, 0},
5719                         {}
5720                 }
5721         },
5722 };
5723
5724 static const struct alc_model_fixup alc268_fixup_models[] = {
5725         {.id = ALC268_FIXUP_INV_DMIC, .name = "inv-dmic"},
5726         {.id = ALC268_FIXUP_HP_EAPD, .name = "hp-eapd"},
5727         {}
5728 };
5729
5730 static const struct snd_pci_quirk alc268_fixup_tbl[] = {
5731         /* below is codec SSID since multiple Toshiba laptops have the
5732          * same PCI SSID 1179:ff00
5733          */
5734         SND_PCI_QUIRK(0x1179, 0xff06, "Toshiba P200", ALC268_FIXUP_HP_EAPD),
5735         {}
5736 };
5737
5738 /*
5739  * BIOS auto configuration
5740  */
5741 static int alc268_parse_auto_config(struct hda_codec *codec)
5742 {
5743         static const hda_nid_t alc268_ssids[] = { 0x15, 0x1b, 0x14, 0 };
5744         struct alc_spec *spec = codec->spec;
5745         int err = alc_parse_auto_config(codec, NULL, alc268_ssids);
5746         if (err > 0) {
5747                 if (!spec->no_analog && spec->autocfg.speaker_pins[0] != 0x1d) {
5748                         add_mixer(spec, alc268_beep_mixer);
5749                         snd_hda_gen_add_verbs(&spec->gen, alc268_beep_init_verbs);
5750                 }
5751         }
5752         return err;
5753 }
5754
5755 /*
5756  */
5757 static int patch_alc268(struct hda_codec *codec)
5758 {
5759         struct alc_spec *spec;
5760         int i, has_beep, err;
5761
5762         /* ALC268 has no aa-loopback mixer */
5763         err = alc_alloc_spec(codec, 0);
5764         if (err < 0)
5765                 return err;
5766
5767         spec = codec->spec;
5768
5769         alc_pick_fixup(codec, alc268_fixup_models, alc268_fixup_tbl, alc268_fixups);
5770         alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
5771
5772         /* automatic parse from the BIOS config */
5773         err = alc268_parse_auto_config(codec);
5774         if (err < 0)
5775                 goto error;
5776
5777         has_beep = 0;
5778         for (i = 0; i < spec->num_mixers; i++) {
5779                 if (spec->mixers[i] == alc268_beep_mixer) {
5780                         has_beep = 1;
5781                         break;
5782                 }
5783         }
5784
5785         if (has_beep) {
5786                 err = snd_hda_attach_beep_device(codec, 0x1);
5787                 if (err < 0)
5788                         goto error;
5789                 if (!query_amp_caps(codec, 0x1d, HDA_INPUT))
5790                         /* override the amp caps for beep generator */
5791                         snd_hda_override_amp_caps(codec, 0x1d, HDA_INPUT,
5792                                           (0x0c << AC_AMPCAP_OFFSET_SHIFT) |
5793                                           (0x0c << AC_AMPCAP_NUM_STEPS_SHIFT) |
5794                                           (0x07 << AC_AMPCAP_STEP_SIZE_SHIFT) |
5795                                           (0 << AC_AMPCAP_MUTE_SHIFT));
5796         }
5797
5798         codec->patch_ops = alc_patch_ops;
5799         spec->shutup = alc_eapd_shutup;
5800
5801         alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
5802
5803         return 0;
5804
5805  error:
5806         alc_free(codec);
5807         return err;
5808 }
5809
5810 /*
5811  * ALC269
5812  */
5813 static const struct hda_pcm_stream alc269_44k_pcm_analog_playback = {
5814         .substreams = 1,
5815         .channels_min = 2,
5816         .channels_max = 8,
5817         .rates = SNDRV_PCM_RATE_44100, /* fixed rate */
5818         /* NID is set in alc_build_pcms */
5819         .ops = {
5820                 .open = alc_playback_pcm_open,
5821                 .prepare = alc_playback_pcm_prepare,
5822                 .cleanup = alc_playback_pcm_cleanup
5823         },
5824 };
5825
5826 static const struct hda_pcm_stream alc269_44k_pcm_analog_capture = {
5827         .substreams = 1,
5828         .channels_min = 2,
5829         .channels_max = 2,
5830         .rates = SNDRV_PCM_RATE_44100, /* fixed rate */
5831         /* NID is set in alc_build_pcms */
5832 };
5833
5834 /* different alc269-variants */
5835 enum {
5836         ALC269_TYPE_ALC269VA,
5837         ALC269_TYPE_ALC269VB,
5838         ALC269_TYPE_ALC269VC,
5839         ALC269_TYPE_ALC269VD,
5840 };
5841
5842 /*
5843  * BIOS auto configuration
5844  */
5845 static int alc269_parse_auto_config(struct hda_codec *codec)
5846 {
5847         static const hda_nid_t alc269_ignore[] = { 0x1d, 0 };
5848         static const hda_nid_t alc269_ssids[] = { 0, 0x1b, 0x14, 0x21 };
5849         static const hda_nid_t alc269va_ssids[] = { 0x15, 0x1b, 0x14, 0 };
5850         struct alc_spec *spec = codec->spec;
5851         const hda_nid_t *ssids;
5852
5853         switch (spec->codec_variant) {
5854         case ALC269_TYPE_ALC269VA:
5855         case ALC269_TYPE_ALC269VC:
5856                 ssids = alc269va_ssids;
5857                 break;
5858         case ALC269_TYPE_ALC269VB:
5859         case ALC269_TYPE_ALC269VD:
5860                 ssids = alc269_ssids;
5861                 break;
5862         default:
5863                 ssids = alc269_ssids;
5864                 break;
5865         }
5866
5867         return alc_parse_auto_config(codec, alc269_ignore, ssids);
5868 }
5869
5870 static void alc269vb_toggle_power_output(struct hda_codec *codec, int power_up)
5871 {
5872         int val = alc_read_coef_idx(codec, 0x04);
5873         if (power_up)
5874                 val |= 1 << 11;
5875         else
5876                 val &= ~(1 << 11);
5877         alc_write_coef_idx(codec, 0x04, val);
5878 }
5879
5880 static void alc269_shutup(struct hda_codec *codec)
5881 {
5882         struct alc_spec *spec = codec->spec;
5883
5884         if (spec->codec_variant != ALC269_TYPE_ALC269VB)
5885                 return;
5886
5887         if (spec->codec_variant == ALC269_TYPE_ALC269VB)
5888                 alc269vb_toggle_power_output(codec, 0);
5889         if (spec->codec_variant == ALC269_TYPE_ALC269VB &&
5890                         (alc_get_coef0(codec) & 0x00ff) == 0x018) {
5891                 msleep(150);
5892         }
5893 }
5894
5895 #ifdef CONFIG_PM
5896 static int alc269_resume(struct hda_codec *codec)
5897 {
5898         struct alc_spec *spec = codec->spec;
5899
5900         if (spec->codec_variant == ALC269_TYPE_ALC269VB)
5901                 alc269vb_toggle_power_output(codec, 0);
5902         if (spec->codec_variant == ALC269_TYPE_ALC269VB &&
5903                         (alc_get_coef0(codec) & 0x00ff) == 0x018) {
5904                 msleep(150);
5905         }
5906
5907         codec->patch_ops.init(codec);
5908
5909         if (spec->codec_variant == ALC269_TYPE_ALC269VB)
5910                 alc269vb_toggle_power_output(codec, 1);
5911         if (spec->codec_variant == ALC269_TYPE_ALC269VB &&
5912                         (alc_get_coef0(codec) & 0x00ff) == 0x017) {
5913                 msleep(200);
5914         }
5915
5916         snd_hda_codec_resume_amp(codec);
5917         snd_hda_codec_resume_cache(codec);
5918         hda_call_check_power_status(codec, 0x01);
5919         return 0;
5920 }
5921 #endif /* CONFIG_PM */
5922
5923 static void alc269_fixup_pincfg_no_hp_to_lineout(struct hda_codec *codec,
5924                                                  const struct alc_fixup *fix, int action)
5925 {
5926         struct alc_spec *spec = codec->spec;
5927
5928         if (action == ALC_FIXUP_ACT_PRE_PROBE)
5929                 spec->parse_flags = HDA_PINCFG_NO_HP_FIXUP;
5930 }
5931
5932 static void alc269_fixup_hweq(struct hda_codec *codec,
5933                                const struct alc_fixup *fix, int action)
5934 {
5935         int coef;
5936
5937         if (action != ALC_FIXUP_ACT_INIT)
5938                 return;
5939         coef = alc_read_coef_idx(codec, 0x1e);
5940         alc_write_coef_idx(codec, 0x1e, coef | 0x80);
5941 }
5942
5943 static void alc271_fixup_dmic(struct hda_codec *codec,
5944                               const struct alc_fixup *fix, int action)
5945 {
5946         static const struct hda_verb verbs[] = {
5947                 {0x20, AC_VERB_SET_COEF_INDEX, 0x0d},
5948                 {0x20, AC_VERB_SET_PROC_COEF, 0x4000},
5949                 {}
5950         };
5951         unsigned int cfg;
5952
5953         if (strcmp(codec->chip_name, "ALC271X"))
5954                 return;
5955         cfg = snd_hda_codec_get_pincfg(codec, 0x12);
5956         if (get_defcfg_connect(cfg) == AC_JACK_PORT_FIXED)
5957                 snd_hda_sequence_write(codec, verbs);
5958 }
5959
5960 static void alc269_fixup_pcm_44k(struct hda_codec *codec,
5961                                  const struct alc_fixup *fix, int action)
5962 {
5963         struct alc_spec *spec = codec->spec;
5964
5965         if (action != ALC_FIXUP_ACT_PROBE)
5966                 return;
5967
5968         /* Due to a hardware problem on Lenovo Ideadpad, we need to
5969          * fix the sample rate of analog I/O to 44.1kHz
5970          */
5971         spec->stream_analog_playback = &alc269_44k_pcm_analog_playback;
5972         spec->stream_analog_capture = &alc269_44k_pcm_analog_capture;
5973 }
5974
5975 static void alc269_fixup_stereo_dmic(struct hda_codec *codec,
5976                                      const struct alc_fixup *fix, int action)
5977 {
5978         int coef;
5979
5980         if (action != ALC_FIXUP_ACT_INIT)
5981                 return;
5982         /* The digital-mic unit sends PDM (differential signal) instead of
5983          * the standard PCM, thus you can't record a valid mono stream as is.
5984          * Below is a workaround specific to ALC269 to control the dmic
5985          * signal source as mono.
5986          */
5987         coef = alc_read_coef_idx(codec, 0x07);
5988         alc_write_coef_idx(codec, 0x07, coef | 0x80);
5989 }
5990
5991 static void alc269_quanta_automute(struct hda_codec *codec)
5992 {
5993         update_outputs(codec);
5994
5995         snd_hda_codec_write(codec, 0x20, 0,
5996                         AC_VERB_SET_COEF_INDEX, 0x0c);
5997         snd_hda_codec_write(codec, 0x20, 0,
5998                         AC_VERB_SET_PROC_COEF, 0x680);
5999
6000         snd_hda_codec_write(codec, 0x20, 0,
6001                         AC_VERB_SET_COEF_INDEX, 0x0c);
6002         snd_hda_codec_write(codec, 0x20, 0,
6003                         AC_VERB_SET_PROC_COEF, 0x480);
6004 }
6005
6006 static void alc269_fixup_quanta_mute(struct hda_codec *codec,
6007                                      const struct alc_fixup *fix, int action)
6008 {
6009         struct alc_spec *spec = codec->spec;
6010         if (action != ALC_FIXUP_ACT_PROBE)
6011                 return;
6012         spec->automute_hook = alc269_quanta_automute;
6013 }
6014
6015 /* update mute-LED according to the speaker mute state via mic2 VREF pin */
6016 static void alc269_fixup_mic2_mute_hook(void *private_data, int enabled)
6017 {
6018         struct hda_codec *codec = private_data;
6019         unsigned int pinval = enabled ? 0x20 : 0x24;
6020         snd_hda_set_pin_ctl_cache(codec, 0x19, pinval);
6021 }
6022
6023 static void alc269_fixup_mic2_mute(struct hda_codec *codec,
6024                                    const struct alc_fixup *fix, int action)
6025 {
6026         struct alc_spec *spec = codec->spec;
6027         switch (action) {
6028         case ALC_FIXUP_ACT_BUILD:
6029                 spec->vmaster_mute.hook = alc269_fixup_mic2_mute_hook;
6030                 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute, true);
6031                 /* fallthru */
6032         case ALC_FIXUP_ACT_INIT:
6033                 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
6034                 break;
6035         }
6036 }
6037
6038
6039 enum {
6040         ALC269_FIXUP_SONY_VAIO,
6041         ALC275_FIXUP_SONY_VAIO_GPIO2,
6042         ALC269_FIXUP_DELL_M101Z,
6043         ALC269_FIXUP_SKU_IGNORE,
6044         ALC269_FIXUP_ASUS_G73JW,
6045         ALC269_FIXUP_LENOVO_EAPD,
6046         ALC275_FIXUP_SONY_HWEQ,
6047         ALC271_FIXUP_DMIC,
6048         ALC269_FIXUP_PCM_44K,
6049         ALC269_FIXUP_STEREO_DMIC,
6050         ALC269_FIXUP_QUANTA_MUTE,
6051         ALC269_FIXUP_LIFEBOOK,
6052         ALC269_FIXUP_AMIC,
6053         ALC269_FIXUP_DMIC,
6054         ALC269VB_FIXUP_AMIC,
6055         ALC269VB_FIXUP_DMIC,
6056         ALC269_FIXUP_MIC2_MUTE_LED,
6057         ALC269_FIXUP_INV_DMIC,
6058         ALC269_FIXUP_LENOVO_DOCK,
6059         ALC269_FIXUP_PINCFG_NO_HP_TO_LINEOUT,
6060 };
6061
6062 static const struct alc_fixup alc269_fixups[] = {
6063         [ALC269_FIXUP_SONY_VAIO] = {
6064                 .type = ALC_FIXUP_VERBS,
6065                 .v.verbs = (const struct hda_verb[]) {
6066                         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREFGRD},
6067                         {}
6068                 }
6069         },
6070         [ALC275_FIXUP_SONY_VAIO_GPIO2] = {
6071                 .type = ALC_FIXUP_VERBS,
6072                 .v.verbs = (const struct hda_verb[]) {
6073                         {0x01, AC_VERB_SET_GPIO_MASK, 0x04},
6074                         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x04},
6075                         {0x01, AC_VERB_SET_GPIO_DATA, 0x00},
6076                         { }
6077                 },
6078                 .chained = true,
6079                 .chain_id = ALC269_FIXUP_SONY_VAIO
6080         },
6081         [ALC269_FIXUP_DELL_M101Z] = {
6082                 .type = ALC_FIXUP_VERBS,
6083                 .v.verbs = (const struct hda_verb[]) {
6084                         /* Enables internal speaker */
6085                         {0x20, AC_VERB_SET_COEF_INDEX, 13},
6086                         {0x20, AC_VERB_SET_PROC_COEF, 0x4040},
6087                         {}
6088                 }
6089         },
6090         [ALC269_FIXUP_SKU_IGNORE] = {
6091                 .type = ALC_FIXUP_FUNC,
6092                 .v.func = alc_fixup_sku_ignore,
6093         },
6094         [ALC269_FIXUP_ASUS_G73JW] = {
6095                 .type = ALC_FIXUP_PINS,
6096                 .v.pins = (const struct alc_pincfg[]) {
6097                         { 0x17, 0x99130111 }, /* subwoofer */
6098                         { }
6099                 }
6100         },
6101         [ALC269_FIXUP_LENOVO_EAPD] = {
6102                 .type = ALC_FIXUP_VERBS,
6103                 .v.verbs = (const struct hda_verb[]) {
6104                         {0x14, AC_VERB_SET_EAPD_BTLENABLE, 0},
6105                         {}
6106                 }
6107         },
6108         [ALC275_FIXUP_SONY_HWEQ] = {
6109                 .type = ALC_FIXUP_FUNC,
6110                 .v.func = alc269_fixup_hweq,
6111                 .chained = true,
6112                 .chain_id = ALC275_FIXUP_SONY_VAIO_GPIO2
6113         },
6114         [ALC271_FIXUP_DMIC] = {
6115                 .type = ALC_FIXUP_FUNC,
6116                 .v.func = alc271_fixup_dmic,
6117         },
6118         [ALC269_FIXUP_PCM_44K] = {
6119                 .type = ALC_FIXUP_FUNC,
6120                 .v.func = alc269_fixup_pcm_44k,
6121                 .chained = true,
6122                 .chain_id = ALC269_FIXUP_QUANTA_MUTE
6123         },
6124         [ALC269_FIXUP_STEREO_DMIC] = {
6125                 .type = ALC_FIXUP_FUNC,
6126                 .v.func = alc269_fixup_stereo_dmic,
6127         },
6128         [ALC269_FIXUP_QUANTA_MUTE] = {
6129                 .type = ALC_FIXUP_FUNC,
6130                 .v.func = alc269_fixup_quanta_mute,
6131         },
6132         [ALC269_FIXUP_LIFEBOOK] = {
6133                 .type = ALC_FIXUP_PINS,
6134                 .v.pins = (const struct alc_pincfg[]) {
6135                         { 0x1a, 0x2101103f }, /* dock line-out */
6136                         { 0x1b, 0x23a11040 }, /* dock mic-in */
6137                         { }
6138                 },
6139                 .chained = true,
6140                 .chain_id = ALC269_FIXUP_QUANTA_MUTE
6141         },
6142         [ALC269_FIXUP_AMIC] = {
6143                 .type = ALC_FIXUP_PINS,
6144                 .v.pins = (const struct alc_pincfg[]) {
6145                         { 0x14, 0x99130110 }, /* speaker */
6146                         { 0x15, 0x0121401f }, /* HP out */
6147                         { 0x18, 0x01a19c20 }, /* mic */
6148                         { 0x19, 0x99a3092f }, /* int-mic */
6149                         { }
6150                 },
6151         },
6152         [ALC269_FIXUP_DMIC] = {
6153                 .type = ALC_FIXUP_PINS,
6154                 .v.pins = (const struct alc_pincfg[]) {
6155                         { 0x12, 0x99a3092f }, /* int-mic */
6156                         { 0x14, 0x99130110 }, /* speaker */
6157                         { 0x15, 0x0121401f }, /* HP out */
6158                         { 0x18, 0x01a19c20 }, /* mic */
6159                         { }
6160                 },
6161         },
6162         [ALC269VB_FIXUP_AMIC] = {
6163                 .type = ALC_FIXUP_PINS,
6164                 .v.pins = (const struct alc_pincfg[]) {
6165                         { 0x14, 0x99130110 }, /* speaker */
6166                         { 0x18, 0x01a19c20 }, /* mic */
6167                         { 0x19, 0x99a3092f }, /* int-mic */
6168                         { 0x21, 0x0121401f }, /* HP out */
6169                         { }
6170                 },
6171         },
6172         [ALC269VB_FIXUP_DMIC] = {
6173                 .type = ALC_FIXUP_PINS,
6174                 .v.pins = (const struct alc_pincfg[]) {
6175                         { 0x12, 0x99a3092f }, /* int-mic */
6176                         { 0x14, 0x99130110 }, /* speaker */
6177                         { 0x18, 0x01a19c20 }, /* mic */
6178                         { 0x21, 0x0121401f }, /* HP out */
6179                         { }
6180                 },
6181         },
6182         [ALC269_FIXUP_MIC2_MUTE_LED] = {
6183                 .type = ALC_FIXUP_FUNC,
6184                 .v.func = alc269_fixup_mic2_mute,
6185         },
6186         [ALC269_FIXUP_INV_DMIC] = {
6187                 .type = ALC_FIXUP_FUNC,
6188                 .v.func = alc_fixup_inv_dmic_0x12,
6189         },
6190         [ALC269_FIXUP_LENOVO_DOCK] = {
6191                 .type = ALC_FIXUP_PINS,
6192                 .v.pins = (const struct alc_pincfg[]) {
6193                         { 0x19, 0x23a11040 }, /* dock mic */
6194                         { 0x1b, 0x2121103f }, /* dock headphone */
6195                         { }
6196                 },
6197                 .chained = true,
6198                 .chain_id = ALC269_FIXUP_PINCFG_NO_HP_TO_LINEOUT
6199         },
6200         [ALC269_FIXUP_PINCFG_NO_HP_TO_LINEOUT] = {
6201                 .type = ALC_FIXUP_FUNC,
6202                 .v.func = alc269_fixup_pincfg_no_hp_to_lineout,
6203         },
6204 };
6205
6206 static const struct snd_pci_quirk alc269_fixup_tbl[] = {
6207         SND_PCI_QUIRK(0x1025, 0x029b, "Acer 1810TZ", ALC269_FIXUP_INV_DMIC),
6208         SND_PCI_QUIRK(0x1025, 0x0349, "Acer AOD260", ALC269_FIXUP_INV_DMIC),
6209         SND_PCI_QUIRK(0x103c, 0x1586, "HP", ALC269_FIXUP_MIC2_MUTE_LED),
6210         SND_PCI_QUIRK(0x1043, 0x1427, "Asus Zenbook UX31E", ALC269VB_FIXUP_DMIC),
6211         SND_PCI_QUIRK(0x1043, 0x1a13, "Asus G73Jw", ALC269_FIXUP_ASUS_G73JW),
6212         SND_PCI_QUIRK(0x1043, 0x1b13, "Asus U41SV", ALC269_FIXUP_INV_DMIC),
6213         SND_PCI_QUIRK(0x1043, 0x16e3, "ASUS UX50", ALC269_FIXUP_STEREO_DMIC),
6214         SND_PCI_QUIRK(0x1043, 0x831a, "ASUS P901", ALC269_FIXUP_STEREO_DMIC),
6215         SND_PCI_QUIRK(0x1043, 0x834a, "ASUS S101", ALC269_FIXUP_STEREO_DMIC),
6216         SND_PCI_QUIRK(0x1043, 0x8398, "ASUS P1005", ALC269_FIXUP_STEREO_DMIC),
6217         SND_PCI_QUIRK(0x1043, 0x83ce, "ASUS P1005", ALC269_FIXUP_STEREO_DMIC),
6218         SND_PCI_QUIRK(0x104d, 0x9073, "Sony VAIO", ALC275_FIXUP_SONY_VAIO_GPIO2),
6219         SND_PCI_QUIRK(0x104d, 0x907b, "Sony VAIO", ALC275_FIXUP_SONY_HWEQ),
6220         SND_PCI_QUIRK(0x104d, 0x9084, "Sony VAIO", ALC275_FIXUP_SONY_HWEQ),
6221         SND_PCI_QUIRK_VENDOR(0x104d, "Sony VAIO", ALC269_FIXUP_SONY_VAIO),
6222         SND_PCI_QUIRK(0x1028, 0x0470, "Dell M101z", ALC269_FIXUP_DELL_M101Z),
6223         SND_PCI_QUIRK_VENDOR(0x1025, "Acer Aspire", ALC271_FIXUP_DMIC),
6224         SND_PCI_QUIRK(0x10cf, 0x1475, "Lifebook", ALC269_FIXUP_LIFEBOOK),
6225         SND_PCI_QUIRK(0x17aa, 0x20f2, "Thinkpad SL410/510", ALC269_FIXUP_SKU_IGNORE),
6226         SND_PCI_QUIRK(0x17aa, 0x215e, "Thinkpad L512", ALC269_FIXUP_SKU_IGNORE),
6227         SND_PCI_QUIRK(0x17aa, 0x21b8, "Thinkpad Edge 14", ALC269_FIXUP_SKU_IGNORE),
6228         SND_PCI_QUIRK(0x17aa, 0x21ca, "Thinkpad L412", ALC269_FIXUP_SKU_IGNORE),
6229         SND_PCI_QUIRK(0x17aa, 0x21e9, "Thinkpad Edge 15", ALC269_FIXUP_SKU_IGNORE),
6230         SND_PCI_QUIRK(0x17aa, 0x21f6, "Thinkpad T530", ALC269_FIXUP_LENOVO_DOCK),
6231         SND_PCI_QUIRK(0x17aa, 0x21fa, "Thinkpad X230", ALC269_FIXUP_LENOVO_DOCK),
6232         SND_PCI_QUIRK(0x17aa, 0x21f3, "Thinkpad T430", ALC269_FIXUP_LENOVO_DOCK),
6233         SND_PCI_QUIRK(0x17aa, 0x21fb, "Thinkpad T430s", ALC269_FIXUP_LENOVO_DOCK),
6234         SND_PCI_QUIRK(0x17aa, 0x2203, "Thinkpad X230 Tablet", ALC269_FIXUP_LENOVO_DOCK),
6235         SND_PCI_QUIRK(0x17aa, 0x3bf8, "Quanta FL1", ALC269_FIXUP_PCM_44K),
6236         SND_PCI_QUIRK(0x17aa, 0x9e54, "LENOVO NB", ALC269_FIXUP_LENOVO_EAPD),
6237
6238 #if 0
6239         /* Below is a quirk table taken from the old code.
6240          * Basically the device should work as is without the fixup table.
6241          * If BIOS doesn't give a proper info, enable the corresponding
6242          * fixup entry.
6243          */
6244         SND_PCI_QUIRK(0x1043, 0x8330, "ASUS Eeepc P703 P900A",
6245                       ALC269_FIXUP_AMIC),
6246         SND_PCI_QUIRK(0x1043, 0x1013, "ASUS N61Da", ALC269_FIXUP_AMIC),
6247         SND_PCI_QUIRK(0x1043, 0x1143, "ASUS B53f", ALC269_FIXUP_AMIC),
6248         SND_PCI_QUIRK(0x1043, 0x1133, "ASUS UJ20ft", ALC269_FIXUP_AMIC),
6249         SND_PCI_QUIRK(0x1043, 0x1183, "ASUS K72DR", ALC269_FIXUP_AMIC),
6250         SND_PCI_QUIRK(0x1043, 0x11b3, "ASUS K52DR", ALC269_FIXUP_AMIC),
6251         SND_PCI_QUIRK(0x1043, 0x11e3, "ASUS U33Jc", ALC269_FIXUP_AMIC),
6252         SND_PCI_QUIRK(0x1043, 0x1273, "ASUS UL80Jt", ALC269_FIXUP_AMIC),
6253         SND_PCI_QUIRK(0x1043, 0x1283, "ASUS U53Jc", ALC269_FIXUP_AMIC),
6254         SND_PCI_QUIRK(0x1043, 0x12b3, "ASUS N82JV", ALC269_FIXUP_AMIC),
6255         SND_PCI_QUIRK(0x1043, 0x12d3, "ASUS N61Jv", ALC269_FIXUP_AMIC),
6256         SND_PCI_QUIRK(0x1043, 0x13a3, "ASUS UL30Vt", ALC269_FIXUP_AMIC),
6257         SND_PCI_QUIRK(0x1043, 0x1373, "ASUS G73JX", ALC269_FIXUP_AMIC),
6258         SND_PCI_QUIRK(0x1043, 0x1383, "ASUS UJ30Jc", ALC269_FIXUP_AMIC),
6259         SND_PCI_QUIRK(0x1043, 0x13d3, "ASUS N61JA", ALC269_FIXUP_AMIC),
6260         SND_PCI_QUIRK(0x1043, 0x1413, "ASUS UL50", ALC269_FIXUP_AMIC),
6261         SND_PCI_QUIRK(0x1043, 0x1443, "ASUS UL30", ALC269_FIXUP_AMIC),
6262         SND_PCI_QUIRK(0x1043, 0x1453, "ASUS M60Jv", ALC269_FIXUP_AMIC),
6263         SND_PCI_QUIRK(0x1043, 0x1483, "ASUS UL80", ALC269_FIXUP_AMIC),
6264         SND_PCI_QUIRK(0x1043, 0x14f3, "ASUS F83Vf", ALC269_FIXUP_AMIC),
6265         SND_PCI_QUIRK(0x1043, 0x14e3, "ASUS UL20", ALC269_FIXUP_AMIC),
6266         SND_PCI_QUIRK(0x1043, 0x1513, "ASUS UX30", ALC269_FIXUP_AMIC),
6267         SND_PCI_QUIRK(0x1043, 0x1593, "ASUS N51Vn", ALC269_FIXUP_AMIC),
6268         SND_PCI_QUIRK(0x1043, 0x15a3, "ASUS N60Jv", ALC269_FIXUP_AMIC),
6269         SND_PCI_QUIRK(0x1043, 0x15b3, "ASUS N60Dp", ALC269_FIXUP_AMIC),
6270         SND_PCI_QUIRK(0x1043, 0x15c3, "ASUS N70De", ALC269_FIXUP_AMIC),
6271         SND_PCI_QUIRK(0x1043, 0x15e3, "ASUS F83T", ALC269_FIXUP_AMIC),
6272         SND_PCI_QUIRK(0x1043, 0x1643, "ASUS M60J", ALC269_FIXUP_AMIC),
6273         SND_PCI_QUIRK(0x1043, 0x1653, "ASUS U50", ALC269_FIXUP_AMIC),
6274         SND_PCI_QUIRK(0x1043, 0x1693, "ASUS F50N", ALC269_FIXUP_AMIC),
6275         SND_PCI_QUIRK(0x1043, 0x16a3, "ASUS F5Q", ALC269_FIXUP_AMIC),
6276         SND_PCI_QUIRK(0x1043, 0x1723, "ASUS P80", ALC269_FIXUP_AMIC),
6277         SND_PCI_QUIRK(0x1043, 0x1743, "ASUS U80", ALC269_FIXUP_AMIC),
6278         SND_PCI_QUIRK(0x1043, 0x1773, "ASUS U20A", ALC269_FIXUP_AMIC),
6279         SND_PCI_QUIRK(0x1043, 0x1883, "ASUS F81Se", ALC269_FIXUP_AMIC),
6280         SND_PCI_QUIRK(0x152d, 0x1778, "Quanta ON1", ALC269_FIXUP_DMIC),
6281         SND_PCI_QUIRK(0x17aa, 0x3be9, "Quanta Wistron", ALC269_FIXUP_AMIC),
6282         SND_PCI_QUIRK(0x17aa, 0x3bf8, "Quanta FL1", ALC269_FIXUP_AMIC),
6283         SND_PCI_QUIRK(0x17ff, 0x059a, "Quanta EL3", ALC269_FIXUP_DMIC),
6284         SND_PCI_QUIRK(0x17ff, 0x059b, "Quanta JR1", ALC269_FIXUP_DMIC),
6285 #endif
6286         {}
6287 };
6288
6289 static const struct alc_model_fixup alc269_fixup_models[] = {
6290         {.id = ALC269_FIXUP_AMIC, .name = "laptop-amic"},
6291         {.id = ALC269_FIXUP_DMIC, .name = "laptop-dmic"},
6292         {.id = ALC269_FIXUP_STEREO_DMIC, .name = "alc269-dmic"},
6293         {.id = ALC271_FIXUP_DMIC, .name = "alc271-dmic"},
6294         {.id = ALC269_FIXUP_INV_DMIC, .name = "inv-dmic"},
6295         {.id = ALC269_FIXUP_LENOVO_DOCK, .name = "lenovo-dock"},
6296         {}
6297 };
6298
6299
6300 static void alc269_fill_coef(struct hda_codec *codec)
6301 {
6302         struct alc_spec *spec = codec->spec;
6303         int val;
6304
6305         if (spec->codec_variant != ALC269_TYPE_ALC269VB)
6306                 return;
6307
6308         if ((alc_get_coef0(codec) & 0x00ff) < 0x015) {
6309                 alc_write_coef_idx(codec, 0xf, 0x960b);
6310                 alc_write_coef_idx(codec, 0xe, 0x8817);
6311         }
6312
6313         if ((alc_get_coef0(codec) & 0x00ff) == 0x016) {
6314                 alc_write_coef_idx(codec, 0xf, 0x960b);
6315                 alc_write_coef_idx(codec, 0xe, 0x8814);
6316         }
6317
6318         if ((alc_get_coef0(codec) & 0x00ff) == 0x017) {
6319                 val = alc_read_coef_idx(codec, 0x04);
6320                 /* Power up output pin */
6321                 alc_write_coef_idx(codec, 0x04, val | (1<<11));
6322         }
6323
6324         if ((alc_get_coef0(codec) & 0x00ff) == 0x018) {
6325                 val = alc_read_coef_idx(codec, 0xd);
6326                 if ((val & 0x0c00) >> 10 != 0x1) {
6327                         /* Capless ramp up clock control */
6328                         alc_write_coef_idx(codec, 0xd, val | (1<<10));
6329                 }
6330                 val = alc_read_coef_idx(codec, 0x17);
6331                 if ((val & 0x01c0) >> 6 != 0x4) {
6332                         /* Class D power on reset */
6333                         alc_write_coef_idx(codec, 0x17, val | (1<<7));
6334                 }
6335         }
6336
6337         val = alc_read_coef_idx(codec, 0xd); /* Class D */
6338         alc_write_coef_idx(codec, 0xd, val | (1<<14));
6339
6340         val = alc_read_coef_idx(codec, 0x4); /* HP */
6341         alc_write_coef_idx(codec, 0x4, val | (1<<11));
6342 }
6343
6344 /*
6345  */
6346 static int patch_alc269(struct hda_codec *codec)
6347 {
6348         struct alc_spec *spec;
6349         int err;
6350
6351         err = alc_alloc_spec(codec, 0x0b);
6352         if (err < 0)
6353                 return err;
6354
6355         spec = codec->spec;
6356
6357         alc_pick_fixup(codec, alc269_fixup_models,
6358                        alc269_fixup_tbl, alc269_fixups);
6359         alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
6360
6361         alc_auto_parse_customize_define(codec);
6362
6363         if (codec->vendor_id == 0x10ec0269) {
6364                 spec->codec_variant = ALC269_TYPE_ALC269VA;
6365                 switch (alc_get_coef0(codec) & 0x00f0) {
6366                 case 0x0010:
6367                         if (codec->bus->pci->subsystem_vendor == 0x1025 &&
6368                             spec->cdefine.platform_type == 1)
6369                                 err = alc_codec_rename(codec, "ALC271X");
6370                         spec->codec_variant = ALC269_TYPE_ALC269VB;
6371                         break;
6372                 case 0x0020:
6373                         if (codec->bus->pci->subsystem_vendor == 0x17aa &&
6374                             codec->bus->pci->subsystem_device == 0x21f3)
6375                                 err = alc_codec_rename(codec, "ALC3202");
6376                         spec->codec_variant = ALC269_TYPE_ALC269VC;
6377                         break;
6378                 case 0x0030:
6379                         spec->codec_variant = ALC269_TYPE_ALC269VD;
6380                         break;
6381                 default:
6382                         alc_fix_pll_init(codec, 0x20, 0x04, 15);
6383                 }
6384                 if (err < 0)
6385                         goto error;
6386                 spec->init_hook = alc269_fill_coef;
6387                 alc269_fill_coef(codec);
6388         }
6389
6390         /* automatic parse from the BIOS config */
6391         err = alc269_parse_auto_config(codec);
6392         if (err < 0)
6393                 goto error;
6394
6395         if (!spec->no_analog && has_cdefine_beep(codec)) {
6396                 err = snd_hda_attach_beep_device(codec, 0x1);
6397                 if (err < 0)
6398                         goto error;
6399                 set_beep_amp(spec, 0x0b, 0x04, HDA_INPUT);
6400         }
6401
6402         codec->patch_ops = alc_patch_ops;
6403 #ifdef CONFIG_PM
6404         codec->patch_ops.resume = alc269_resume;
6405 #endif
6406         spec->shutup = alc269_shutup;
6407
6408         alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
6409
6410         return 0;
6411
6412  error:
6413         alc_free(codec);
6414         return err;
6415 }
6416
6417 /*
6418  * ALC861
6419  */
6420
6421 static int alc861_parse_auto_config(struct hda_codec *codec)
6422 {
6423         static const hda_nid_t alc861_ignore[] = { 0x1d, 0 };
6424         static const hda_nid_t alc861_ssids[] = { 0x0e, 0x0f, 0x0b, 0 };
6425         return alc_parse_auto_config(codec, alc861_ignore, alc861_ssids);
6426 }
6427
6428 /* Pin config fixes */
6429 enum {
6430         ALC861_FIXUP_FSC_AMILO_PI1505,
6431         ALC861_FIXUP_AMP_VREF_0F,
6432         ALC861_FIXUP_NO_JACK_DETECT,
6433         ALC861_FIXUP_ASUS_A6RP,
6434 };
6435
6436 /* On some laptops, VREF of pin 0x0f is abused for controlling the main amp */
6437 static void alc861_fixup_asus_amp_vref_0f(struct hda_codec *codec,
6438                         const struct alc_fixup *fix, int action)
6439 {
6440         struct alc_spec *spec = codec->spec;
6441         unsigned int val;
6442
6443         if (action != ALC_FIXUP_ACT_INIT)
6444                 return;
6445         val = snd_hda_codec_read(codec, 0x0f, 0,
6446                                  AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
6447         if (!(val & (AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN)))
6448                 val |= AC_PINCTL_IN_EN;
6449         val |= AC_PINCTL_VREF_50;
6450         snd_hda_set_pin_ctl(codec, 0x0f, val);
6451         spec->keep_vref_in_automute = 1;
6452 }
6453
6454 /* suppress the jack-detection */
6455 static void alc_fixup_no_jack_detect(struct hda_codec *codec,
6456                                      const struct alc_fixup *fix, int action)
6457 {
6458         if (action == ALC_FIXUP_ACT_PRE_PROBE)
6459                 codec->no_jack_detect = 1;
6460 }
6461
6462 static const struct alc_fixup alc861_fixups[] = {
6463         [ALC861_FIXUP_FSC_AMILO_PI1505] = {
6464                 .type = ALC_FIXUP_PINS,
6465                 .v.pins = (const struct alc_pincfg[]) {
6466                         { 0x0b, 0x0221101f }, /* HP */
6467                         { 0x0f, 0x90170310 }, /* speaker */
6468                         { }
6469                 }
6470         },
6471         [ALC861_FIXUP_AMP_VREF_0F] = {
6472                 .type = ALC_FIXUP_FUNC,
6473                 .v.func = alc861_fixup_asus_amp_vref_0f,
6474         },
6475         [ALC861_FIXUP_NO_JACK_DETECT] = {
6476                 .type = ALC_FIXUP_FUNC,
6477                 .v.func = alc_fixup_no_jack_detect,
6478         },
6479         [ALC861_FIXUP_ASUS_A6RP] = {
6480                 .type = ALC_FIXUP_FUNC,
6481                 .v.func = alc861_fixup_asus_amp_vref_0f,
6482                 .chained = true,
6483                 .chain_id = ALC861_FIXUP_NO_JACK_DETECT,
6484         }
6485 };
6486
6487 static const struct snd_pci_quirk alc861_fixup_tbl[] = {
6488         SND_PCI_QUIRK(0x1043, 0x1393, "ASUS A6Rp", ALC861_FIXUP_ASUS_A6RP),
6489         SND_PCI_QUIRK_VENDOR(0x1043, "ASUS laptop", ALC861_FIXUP_AMP_VREF_0F),
6490         SND_PCI_QUIRK(0x1462, 0x7254, "HP DX2200", ALC861_FIXUP_NO_JACK_DETECT),
6491         SND_PCI_QUIRK(0x1584, 0x2b01, "Haier W18", ALC861_FIXUP_AMP_VREF_0F),
6492         SND_PCI_QUIRK(0x1584, 0x0000, "Uniwill ECS M31EI", ALC861_FIXUP_AMP_VREF_0F),
6493         SND_PCI_QUIRK(0x1734, 0x10c7, "FSC Amilo Pi1505", ALC861_FIXUP_FSC_AMILO_PI1505),
6494         {}
6495 };
6496
6497 /*
6498  */
6499 static int patch_alc861(struct hda_codec *codec)
6500 {
6501         struct alc_spec *spec;
6502         int err;
6503
6504         err = alc_alloc_spec(codec, 0x15);
6505         if (err < 0)
6506                 return err;
6507
6508         spec = codec->spec;
6509
6510         alc_pick_fixup(codec, NULL, alc861_fixup_tbl, alc861_fixups);
6511         alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
6512
6513         /* automatic parse from the BIOS config */
6514         err = alc861_parse_auto_config(codec);
6515         if (err < 0)
6516                 goto error;
6517
6518         if (!spec->no_analog) {
6519                 err = snd_hda_attach_beep_device(codec, 0x23);
6520                 if (err < 0)
6521                         goto error;
6522                 set_beep_amp(spec, 0x23, 0, HDA_OUTPUT);
6523         }
6524
6525         codec->patch_ops = alc_patch_ops;
6526 #ifdef CONFIG_SND_HDA_POWER_SAVE
6527         spec->power_hook = alc_power_eapd;
6528 #endif
6529
6530         alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
6531
6532         return 0;
6533
6534  error:
6535         alc_free(codec);
6536         return err;
6537 }
6538
6539 /*
6540  * ALC861-VD support
6541  *
6542  * Based on ALC882
6543  *
6544  * In addition, an independent DAC
6545  */
6546 static int alc861vd_parse_auto_config(struct hda_codec *codec)
6547 {
6548         static const hda_nid_t alc861vd_ignore[] = { 0x1d, 0 };
6549         static const hda_nid_t alc861vd_ssids[] = { 0x15, 0x1b, 0x14, 0 };
6550         return alc_parse_auto_config(codec, alc861vd_ignore, alc861vd_ssids);
6551 }
6552
6553 enum {
6554         ALC660VD_FIX_ASUS_GPIO1,
6555         ALC861VD_FIX_DALLAS,
6556 };
6557
6558 /* exclude VREF80 */
6559 static void alc861vd_fixup_dallas(struct hda_codec *codec,
6560                                   const struct alc_fixup *fix, int action)
6561 {
6562         if (action == ALC_FIXUP_ACT_PRE_PROBE) {
6563                 snd_hda_override_pin_caps(codec, 0x18, 0x00001714);
6564                 snd_hda_override_pin_caps(codec, 0x19, 0x0000171c);
6565         }
6566 }
6567
6568 static const struct alc_fixup alc861vd_fixups[] = {
6569         [ALC660VD_FIX_ASUS_GPIO1] = {
6570                 .type = ALC_FIXUP_VERBS,
6571                 .v.verbs = (const struct hda_verb[]) {
6572                         /* reset GPIO1 */
6573                         {0x01, AC_VERB_SET_GPIO_MASK, 0x03},
6574                         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
6575                         {0x01, AC_VERB_SET_GPIO_DATA, 0x01},
6576                         { }
6577                 }
6578         },
6579         [ALC861VD_FIX_DALLAS] = {
6580                 .type = ALC_FIXUP_FUNC,
6581                 .v.func = alc861vd_fixup_dallas,
6582         },
6583 };
6584
6585 static const struct snd_pci_quirk alc861vd_fixup_tbl[] = {
6586         SND_PCI_QUIRK(0x103c, 0x30bf, "HP TX1000", ALC861VD_FIX_DALLAS),
6587         SND_PCI_QUIRK(0x1043, 0x1339, "ASUS A7-K", ALC660VD_FIX_ASUS_GPIO1),
6588         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba L30-149", ALC861VD_FIX_DALLAS),
6589         {}
6590 };
6591
6592 /*
6593  */
6594 static int patch_alc861vd(struct hda_codec *codec)
6595 {
6596         struct alc_spec *spec;
6597         int err;
6598
6599         err = alc_alloc_spec(codec, 0x0b);
6600         if (err < 0)
6601                 return err;
6602
6603         spec = codec->spec;
6604
6605         alc_pick_fixup(codec, NULL, alc861vd_fixup_tbl, alc861vd_fixups);
6606         alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
6607
6608         /* automatic parse from the BIOS config */
6609         err = alc861vd_parse_auto_config(codec);
6610         if (err < 0)
6611                 goto error;
6612
6613         if (!spec->no_analog) {
6614                 err = snd_hda_attach_beep_device(codec, 0x23);
6615                 if (err < 0)
6616                         goto error;
6617                 set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
6618         }
6619
6620         codec->patch_ops = alc_patch_ops;
6621
6622         spec->shutup = alc_eapd_shutup;
6623
6624         alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
6625
6626         return 0;
6627
6628  error:
6629         alc_free(codec);
6630         return err;
6631 }
6632
6633 /*
6634  * ALC662 support
6635  *
6636  * ALC662 is almost identical with ALC880 but has cleaner and more flexible
6637  * configuration.  Each pin widget can choose any input DACs and a mixer.
6638  * Each ADC is connected from a mixer of all inputs.  This makes possible
6639  * 6-channel independent captures.
6640  *
6641  * In addition, an independent DAC for the multi-playback (not used in this
6642  * driver yet).
6643  */
6644
6645 /*
6646  * BIOS auto configuration
6647  */
6648
6649 static int alc662_parse_auto_config(struct hda_codec *codec)
6650 {
6651         static const hda_nid_t alc662_ignore[] = { 0x1d, 0 };
6652         static const hda_nid_t alc663_ssids[] = { 0x15, 0x1b, 0x14, 0x21 };
6653         static const hda_nid_t alc662_ssids[] = { 0x15, 0x1b, 0x14, 0 };
6654         const hda_nid_t *ssids;
6655
6656         if (codec->vendor_id == 0x10ec0272 || codec->vendor_id == 0x10ec0663 ||
6657             codec->vendor_id == 0x10ec0665 || codec->vendor_id == 0x10ec0670)
6658                 ssids = alc663_ssids;
6659         else
6660                 ssids = alc662_ssids;
6661         return alc_parse_auto_config(codec, alc662_ignore, ssids);
6662 }
6663
6664 static void alc272_fixup_mario(struct hda_codec *codec,
6665                                const struct alc_fixup *fix, int action)
6666 {
6667         if (action != ALC_FIXUP_ACT_PROBE)
6668                 return;
6669         if (snd_hda_override_amp_caps(codec, 0x2, HDA_OUTPUT,
6670                                       (0x3b << AC_AMPCAP_OFFSET_SHIFT) |
6671                                       (0x3b << AC_AMPCAP_NUM_STEPS_SHIFT) |
6672                                       (0x03 << AC_AMPCAP_STEP_SIZE_SHIFT) |
6673                                       (0 << AC_AMPCAP_MUTE_SHIFT)))
6674                 printk(KERN_WARNING
6675                        "hda_codec: failed to override amp caps for NID 0x2\n");
6676 }
6677
6678 enum {
6679         ALC662_FIXUP_ASPIRE,
6680         ALC662_FIXUP_IDEAPAD,
6681         ALC272_FIXUP_MARIO,
6682         ALC662_FIXUP_CZC_P10T,
6683         ALC662_FIXUP_SKU_IGNORE,
6684         ALC662_FIXUP_HP_RP5800,
6685         ALC662_FIXUP_ASUS_MODE1,
6686         ALC662_FIXUP_ASUS_MODE2,
6687         ALC662_FIXUP_ASUS_MODE3,
6688         ALC662_FIXUP_ASUS_MODE4,
6689         ALC662_FIXUP_ASUS_MODE5,
6690         ALC662_FIXUP_ASUS_MODE6,
6691         ALC662_FIXUP_ASUS_MODE7,
6692         ALC662_FIXUP_ASUS_MODE8,
6693         ALC662_FIXUP_NO_JACK_DETECT,
6694         ALC662_FIXUP_ZOTAC_Z68,
6695         ALC662_FIXUP_INV_DMIC,
6696 };
6697
6698 static const struct alc_fixup alc662_fixups[] = {
6699         [ALC662_FIXUP_ASPIRE] = {
6700                 .type = ALC_FIXUP_PINS,
6701                 .v.pins = (const struct alc_pincfg[]) {
6702                         { 0x15, 0x99130112 }, /* subwoofer */
6703                         { }
6704                 }
6705         },
6706         [ALC662_FIXUP_IDEAPAD] = {
6707                 .type = ALC_FIXUP_PINS,
6708                 .v.pins = (const struct alc_pincfg[]) {
6709                         { 0x17, 0x99130112 }, /* subwoofer */
6710                         { }
6711                 }
6712         },
6713         [ALC272_FIXUP_MARIO] = {
6714                 .type = ALC_FIXUP_FUNC,
6715                 .v.func = alc272_fixup_mario,
6716         },
6717         [ALC662_FIXUP_CZC_P10T] = {
6718                 .type = ALC_FIXUP_VERBS,
6719                 .v.verbs = (const struct hda_verb[]) {
6720                         {0x14, AC_VERB_SET_EAPD_BTLENABLE, 0},
6721                         {}
6722                 }
6723         },
6724         [ALC662_FIXUP_SKU_IGNORE] = {
6725                 .type = ALC_FIXUP_FUNC,
6726                 .v.func = alc_fixup_sku_ignore,
6727         },
6728         [ALC662_FIXUP_HP_RP5800] = {
6729                 .type = ALC_FIXUP_PINS,
6730                 .v.pins = (const struct alc_pincfg[]) {
6731                         { 0x14, 0x0221201f }, /* HP out */
6732                         { }
6733                 },
6734                 .chained = true,
6735                 .chain_id = ALC662_FIXUP_SKU_IGNORE
6736         },
6737         [ALC662_FIXUP_ASUS_MODE1] = {
6738                 .type = ALC_FIXUP_PINS,
6739                 .v.pins = (const struct alc_pincfg[]) {
6740                         { 0x14, 0x99130110 }, /* speaker */
6741                         { 0x18, 0x01a19c20 }, /* mic */
6742                         { 0x19, 0x99a3092f }, /* int-mic */
6743                         { 0x21, 0x0121401f }, /* HP out */
6744                         { }
6745                 },
6746                 .chained = true,
6747                 .chain_id = ALC662_FIXUP_SKU_IGNORE
6748         },
6749         [ALC662_FIXUP_ASUS_MODE2] = {
6750                 .type = ALC_FIXUP_PINS,
6751                 .v.pins = (const struct alc_pincfg[]) {
6752                         { 0x14, 0x99130110 }, /* speaker */
6753                         { 0x18, 0x01a19820 }, /* mic */
6754                         { 0x19, 0x99a3092f }, /* int-mic */
6755                         { 0x1b, 0x0121401f }, /* HP out */
6756                         { }
6757                 },
6758                 .chained = true,
6759                 .chain_id = ALC662_FIXUP_SKU_IGNORE
6760         },
6761         [ALC662_FIXUP_ASUS_MODE3] = {
6762                 .type = ALC_FIXUP_PINS,
6763                 .v.pins = (const struct alc_pincfg[]) {
6764                         { 0x14, 0x99130110 }, /* speaker */
6765                         { 0x15, 0x0121441f }, /* HP */
6766                         { 0x18, 0x01a19840 }, /* mic */
6767                         { 0x19, 0x99a3094f }, /* int-mic */
6768                         { 0x21, 0x01211420 }, /* HP2 */
6769                         { }
6770                 },
6771                 .chained = true,
6772                 .chain_id = ALC662_FIXUP_SKU_IGNORE
6773         },
6774         [ALC662_FIXUP_ASUS_MODE4] = {
6775                 .type = ALC_FIXUP_PINS,
6776                 .v.pins = (const struct alc_pincfg[]) {
6777                         { 0x14, 0x99130110 }, /* speaker */
6778                         { 0x16, 0x99130111 }, /* speaker */
6779                         { 0x18, 0x01a19840 }, /* mic */
6780                         { 0x19, 0x99a3094f }, /* int-mic */
6781                         { 0x21, 0x0121441f }, /* HP */
6782                         { }
6783                 },
6784                 .chained = true,
6785                 .chain_id = ALC662_FIXUP_SKU_IGNORE
6786         },
6787         [ALC662_FIXUP_ASUS_MODE5] = {
6788                 .type = ALC_FIXUP_PINS,
6789                 .v.pins = (const struct alc_pincfg[]) {
6790                         { 0x14, 0x99130110 }, /* speaker */
6791                         { 0x15, 0x0121441f }, /* HP */
6792                         { 0x16, 0x99130111 }, /* speaker */
6793                         { 0x18, 0x01a19840 }, /* mic */
6794                         { 0x19, 0x99a3094f }, /* int-mic */
6795                         { }
6796                 },
6797                 .chained = true,
6798                 .chain_id = ALC662_FIXUP_SKU_IGNORE
6799         },
6800         [ALC662_FIXUP_ASUS_MODE6] = {
6801                 .type = ALC_FIXUP_PINS,
6802                 .v.pins = (const struct alc_pincfg[]) {
6803                         { 0x14, 0x99130110 }, /* speaker */
6804                         { 0x15, 0x01211420 }, /* HP2 */
6805                         { 0x18, 0x01a19840 }, /* mic */
6806                         { 0x19, 0x99a3094f }, /* int-mic */
6807                         { 0x1b, 0x0121441f }, /* HP */
6808                         { }
6809                 },
6810                 .chained = true,
6811                 .chain_id = ALC662_FIXUP_SKU_IGNORE
6812         },
6813         [ALC662_FIXUP_ASUS_MODE7] = {
6814                 .type = ALC_FIXUP_PINS,
6815                 .v.pins = (const struct alc_pincfg[]) {
6816                         { 0x14, 0x99130110 }, /* speaker */
6817                         { 0x17, 0x99130111 }, /* speaker */
6818                         { 0x18, 0x01a19840 }, /* mic */
6819                         { 0x19, 0x99a3094f }, /* int-mic */
6820                         { 0x1b, 0x01214020 }, /* HP */
6821                         { 0x21, 0x0121401f }, /* HP */
6822                         { }
6823                 },
6824                 .chained = true,
6825                 .chain_id = ALC662_FIXUP_SKU_IGNORE
6826         },
6827         [ALC662_FIXUP_ASUS_MODE8] = {
6828                 .type = ALC_FIXUP_PINS,
6829                 .v.pins = (const struct alc_pincfg[]) {
6830                         { 0x14, 0x99130110 }, /* speaker */
6831                         { 0x12, 0x99a30970 }, /* int-mic */
6832                         { 0x15, 0x01214020 }, /* HP */
6833                         { 0x17, 0x99130111 }, /* speaker */
6834                         { 0x18, 0x01a19840 }, /* mic */
6835                         { 0x21, 0x0121401f }, /* HP */
6836                         { }
6837                 },
6838                 .chained = true,
6839                 .chain_id = ALC662_FIXUP_SKU_IGNORE
6840         },
6841         [ALC662_FIXUP_NO_JACK_DETECT] = {
6842                 .type = ALC_FIXUP_FUNC,
6843                 .v.func = alc_fixup_no_jack_detect,
6844         },
6845         [ALC662_FIXUP_ZOTAC_Z68] = {
6846                 .type = ALC_FIXUP_PINS,
6847                 .v.pins = (const struct alc_pincfg[]) {
6848                         { 0x1b, 0x02214020 }, /* Front HP */
6849                         { }
6850                 }
6851         },
6852         [ALC662_FIXUP_INV_DMIC] = {
6853                 .type = ALC_FIXUP_FUNC,
6854                 .v.func = alc_fixup_inv_dmic_0x12,
6855         },
6856 };
6857
6858 static const struct snd_pci_quirk alc662_fixup_tbl[] = {
6859         SND_PCI_QUIRK(0x1019, 0x9087, "ECS", ALC662_FIXUP_ASUS_MODE2),
6860         SND_PCI_QUIRK(0x1025, 0x0308, "Acer Aspire 8942G", ALC662_FIXUP_ASPIRE),
6861         SND_PCI_QUIRK(0x1025, 0x031c, "Gateway NV79", ALC662_FIXUP_SKU_IGNORE),
6862         SND_PCI_QUIRK(0x1025, 0x0349, "eMachines eM250", ALC662_FIXUP_INV_DMIC),
6863         SND_PCI_QUIRK(0x1025, 0x038b, "Acer Aspire 8943G", ALC662_FIXUP_ASPIRE),
6864         SND_PCI_QUIRK(0x103c, 0x1632, "HP RP5800", ALC662_FIXUP_HP_RP5800),
6865         SND_PCI_QUIRK(0x1043, 0x8469, "ASUS mobo", ALC662_FIXUP_NO_JACK_DETECT),
6866         SND_PCI_QUIRK(0x105b, 0x0cd6, "Foxconn", ALC662_FIXUP_ASUS_MODE2),
6867         SND_PCI_QUIRK(0x144d, 0xc051, "Samsung R720", ALC662_FIXUP_IDEAPAD),
6868         SND_PCI_QUIRK(0x17aa, 0x38af, "Lenovo Ideapad Y550P", ALC662_FIXUP_IDEAPAD),
6869         SND_PCI_QUIRK(0x17aa, 0x3a0d, "Lenovo Ideapad Y550", ALC662_FIXUP_IDEAPAD),
6870         SND_PCI_QUIRK(0x19da, 0xa130, "Zotac Z68", ALC662_FIXUP_ZOTAC_Z68),
6871         SND_PCI_QUIRK(0x1b35, 0x2206, "CZC P10T", ALC662_FIXUP_CZC_P10T),
6872
6873 #if 0
6874         /* Below is a quirk table taken from the old code.
6875          * Basically the device should work as is without the fixup table.
6876          * If BIOS doesn't give a proper info, enable the corresponding
6877          * fixup entry.
6878          */
6879         SND_PCI_QUIRK(0x1043, 0x1000, "ASUS N50Vm", ALC662_FIXUP_ASUS_MODE1),
6880         SND_PCI_QUIRK(0x1043, 0x1092, "ASUS NB", ALC662_FIXUP_ASUS_MODE3),
6881         SND_PCI_QUIRK(0x1043, 0x1173, "ASUS K73Jn", ALC662_FIXUP_ASUS_MODE1),
6882         SND_PCI_QUIRK(0x1043, 0x11c3, "ASUS M70V", ALC662_FIXUP_ASUS_MODE3),
6883         SND_PCI_QUIRK(0x1043, 0x11d3, "ASUS NB", ALC662_FIXUP_ASUS_MODE1),
6884         SND_PCI_QUIRK(0x1043, 0x11f3, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6885         SND_PCI_QUIRK(0x1043, 0x1203, "ASUS NB", ALC662_FIXUP_ASUS_MODE1),
6886         SND_PCI_QUIRK(0x1043, 0x1303, "ASUS G60J", ALC662_FIXUP_ASUS_MODE1),
6887         SND_PCI_QUIRK(0x1043, 0x1333, "ASUS G60Jx", ALC662_FIXUP_ASUS_MODE1),
6888         SND_PCI_QUIRK(0x1043, 0x1339, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6889         SND_PCI_QUIRK(0x1043, 0x13e3, "ASUS N71JA", ALC662_FIXUP_ASUS_MODE7),
6890         SND_PCI_QUIRK(0x1043, 0x1463, "ASUS N71", ALC662_FIXUP_ASUS_MODE7),
6891         SND_PCI_QUIRK(0x1043, 0x14d3, "ASUS G72", ALC662_FIXUP_ASUS_MODE8),
6892         SND_PCI_QUIRK(0x1043, 0x1563, "ASUS N90", ALC662_FIXUP_ASUS_MODE3),
6893         SND_PCI_QUIRK(0x1043, 0x15d3, "ASUS N50SF F50SF", ALC662_FIXUP_ASUS_MODE1),
6894         SND_PCI_QUIRK(0x1043, 0x16c3, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6895         SND_PCI_QUIRK(0x1043, 0x16f3, "ASUS K40C K50C", ALC662_FIXUP_ASUS_MODE2),
6896         SND_PCI_QUIRK(0x1043, 0x1733, "ASUS N81De", ALC662_FIXUP_ASUS_MODE1),
6897         SND_PCI_QUIRK(0x1043, 0x1753, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6898         SND_PCI_QUIRK(0x1043, 0x1763, "ASUS NB", ALC662_FIXUP_ASUS_MODE6),
6899         SND_PCI_QUIRK(0x1043, 0x1765, "ASUS NB", ALC662_FIXUP_ASUS_MODE6),
6900         SND_PCI_QUIRK(0x1043, 0x1783, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6901         SND_PCI_QUIRK(0x1043, 0x1793, "ASUS F50GX", ALC662_FIXUP_ASUS_MODE1),
6902         SND_PCI_QUIRK(0x1043, 0x17b3, "ASUS F70SL", ALC662_FIXUP_ASUS_MODE3),
6903         SND_PCI_QUIRK(0x1043, 0x17f3, "ASUS X58LE", ALC662_FIXUP_ASUS_MODE2),
6904         SND_PCI_QUIRK(0x1043, 0x1813, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6905         SND_PCI_QUIRK(0x1043, 0x1823, "ASUS NB", ALC662_FIXUP_ASUS_MODE5),
6906         SND_PCI_QUIRK(0x1043, 0x1833, "ASUS NB", ALC662_FIXUP_ASUS_MODE6),
6907         SND_PCI_QUIRK(0x1043, 0x1843, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6908         SND_PCI_QUIRK(0x1043, 0x1853, "ASUS F50Z", ALC662_FIXUP_ASUS_MODE1),
6909         SND_PCI_QUIRK(0x1043, 0x1864, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6910         SND_PCI_QUIRK(0x1043, 0x1876, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6911         SND_PCI_QUIRK(0x1043, 0x1893, "ASUS M50Vm", ALC662_FIXUP_ASUS_MODE3),
6912         SND_PCI_QUIRK(0x1043, 0x1894, "ASUS X55", ALC662_FIXUP_ASUS_MODE3),
6913         SND_PCI_QUIRK(0x1043, 0x18b3, "ASUS N80Vc", ALC662_FIXUP_ASUS_MODE1),
6914         SND_PCI_QUIRK(0x1043, 0x18c3, "ASUS VX5", ALC662_FIXUP_ASUS_MODE1),
6915         SND_PCI_QUIRK(0x1043, 0x18d3, "ASUS N81Te", ALC662_FIXUP_ASUS_MODE1),
6916         SND_PCI_QUIRK(0x1043, 0x18f3, "ASUS N505Tp", ALC662_FIXUP_ASUS_MODE1),
6917         SND_PCI_QUIRK(0x1043, 0x1903, "ASUS F5GL", ALC662_FIXUP_ASUS_MODE1),
6918         SND_PCI_QUIRK(0x1043, 0x1913, "ASUS NB", ALC662_FIXUP_ASUS_MODE2),
6919         SND_PCI_QUIRK(0x1043, 0x1933, "ASUS F80Q", ALC662_FIXUP_ASUS_MODE2),
6920         SND_PCI_QUIRK(0x1043, 0x1943, "ASUS Vx3V", ALC662_FIXUP_ASUS_MODE1),
6921         SND_PCI_QUIRK(0x1043, 0x1953, "ASUS NB", ALC662_FIXUP_ASUS_MODE1),
6922         SND_PCI_QUIRK(0x1043, 0x1963, "ASUS X71C", ALC662_FIXUP_ASUS_MODE3),
6923         SND_PCI_QUIRK(0x1043, 0x1983, "ASUS N5051A", ALC662_FIXUP_ASUS_MODE1),
6924         SND_PCI_QUIRK(0x1043, 0x1993, "ASUS N20", ALC662_FIXUP_ASUS_MODE1),
6925         SND_PCI_QUIRK(0x1043, 0x19b3, "ASUS F7Z", ALC662_FIXUP_ASUS_MODE1),
6926         SND_PCI_QUIRK(0x1043, 0x19c3, "ASUS F5Z/F6x", ALC662_FIXUP_ASUS_MODE2),
6927         SND_PCI_QUIRK(0x1043, 0x19e3, "ASUS NB", ALC662_FIXUP_ASUS_MODE1),
6928         SND_PCI_QUIRK(0x1043, 0x19f3, "ASUS NB", ALC662_FIXUP_ASUS_MODE4),
6929 #endif
6930         {}
6931 };
6932
6933 static const struct alc_model_fixup alc662_fixup_models[] = {
6934         {.id = ALC272_FIXUP_MARIO, .name = "mario"},
6935         {.id = ALC662_FIXUP_ASUS_MODE1, .name = "asus-mode1"},
6936         {.id = ALC662_FIXUP_ASUS_MODE2, .name = "asus-mode2"},
6937         {.id = ALC662_FIXUP_ASUS_MODE3, .name = "asus-mode3"},
6938         {.id = ALC662_FIXUP_ASUS_MODE4, .name = "asus-mode4"},
6939         {.id = ALC662_FIXUP_ASUS_MODE5, .name = "asus-mode5"},
6940         {.id = ALC662_FIXUP_ASUS_MODE6, .name = "asus-mode6"},
6941         {.id = ALC662_FIXUP_ASUS_MODE7, .name = "asus-mode7"},
6942         {.id = ALC662_FIXUP_ASUS_MODE8, .name = "asus-mode8"},
6943         {.id = ALC662_FIXUP_INV_DMIC, .name = "inv-dmic"},
6944         {}
6945 };
6946
6947 static void alc662_fill_coef(struct hda_codec *codec)
6948 {
6949         int val, coef;
6950
6951         coef = alc_get_coef0(codec);
6952
6953         switch (codec->vendor_id) {
6954         case 0x10ec0662:
6955                 if ((coef & 0x00f0) == 0x0030) {
6956                         val = alc_read_coef_idx(codec, 0x4); /* EAPD Ctrl */
6957                         alc_write_coef_idx(codec, 0x4, val & ~(1<<10));
6958                 }
6959                 break;
6960         case 0x10ec0272:
6961         case 0x10ec0273:
6962         case 0x10ec0663:
6963         case 0x10ec0665:
6964         case 0x10ec0670:
6965         case 0x10ec0671:
6966         case 0x10ec0672:
6967                 val = alc_read_coef_idx(codec, 0xd); /* EAPD Ctrl */
6968                 alc_write_coef_idx(codec, 0xd, val | (1<<14));
6969                 break;
6970         }
6971 }
6972
6973 /*
6974  */
6975 static int patch_alc662(struct hda_codec *codec)
6976 {
6977         struct alc_spec *spec;
6978         int err;
6979
6980         err = alc_alloc_spec(codec, 0x0b);
6981         if (err < 0)
6982                 return err;
6983
6984         spec = codec->spec;
6985
6986         /* handle multiple HPs as is */
6987         spec->parse_flags = HDA_PINCFG_NO_HP_FIXUP;
6988
6989         alc_fix_pll_init(codec, 0x20, 0x04, 15);
6990
6991         spec->init_hook = alc662_fill_coef;
6992         alc662_fill_coef(codec);
6993
6994         alc_pick_fixup(codec, alc662_fixup_models,
6995                        alc662_fixup_tbl, alc662_fixups);
6996         alc_apply_fixup(codec, ALC_FIXUP_ACT_PRE_PROBE);
6997
6998         alc_auto_parse_customize_define(codec);
6999
7000         if ((alc_get_coef0(codec) & (1 << 14)) &&
7001             codec->bus->pci->subsystem_vendor == 0x1025 &&
7002             spec->cdefine.platform_type == 1) {
7003                 if (alc_codec_rename(codec, "ALC272X") < 0)
7004                         goto error;
7005         }
7006
7007         /* automatic parse from the BIOS config */
7008         err = alc662_parse_auto_config(codec);
7009         if (err < 0)
7010                 goto error;
7011
7012         if (!spec->no_analog && has_cdefine_beep(codec)) {
7013                 err = snd_hda_attach_beep_device(codec, 0x1);
7014                 if (err < 0)
7015                         goto error;
7016                 switch (codec->vendor_id) {
7017                 case 0x10ec0662:
7018                         set_beep_amp(spec, 0x0b, 0x05, HDA_INPUT);
7019                         break;
7020                 case 0x10ec0272:
7021                 case 0x10ec0663:
7022                 case 0x10ec0665:
7023                         set_beep_amp(spec, 0x0b, 0x04, HDA_INPUT);
7024                         break;
7025                 case 0x10ec0273:
7026                         set_beep_amp(spec, 0x0b, 0x03, HDA_INPUT);
7027                         break;
7028                 }
7029         }
7030
7031         codec->patch_ops = alc_patch_ops;
7032         spec->shutup = alc_eapd_shutup;
7033
7034         alc_apply_fixup(codec, ALC_FIXUP_ACT_PROBE);
7035
7036         return 0;
7037
7038  error:
7039         alc_free(codec);
7040         return err;
7041 }
7042
7043 /*
7044  * ALC680 support
7045  */
7046
7047 static int alc680_parse_auto_config(struct hda_codec *codec)
7048 {
7049         return alc_parse_auto_config(codec, NULL, NULL);
7050 }
7051
7052 /*
7053  */
7054 static int patch_alc680(struct hda_codec *codec)
7055 {
7056         int err;
7057
7058         /* ALC680 has no aa-loopback mixer */
7059         err = alc_alloc_spec(codec, 0);
7060         if (err < 0)
7061                 return err;
7062
7063         /* automatic parse from the BIOS config */
7064         err = alc680_parse_auto_config(codec);
7065         if (err < 0) {
7066                 alc_free(codec);
7067                 return err;
7068         }
7069
7070         codec->patch_ops = alc_patch_ops;
7071
7072         return 0;
7073 }
7074
7075 /*
7076  * patch entries
7077  */
7078 static const struct hda_codec_preset snd_hda_preset_realtek[] = {
7079         { .id = 0x10ec0221, .name = "ALC221", .patch = patch_alc269 },
7080         { .id = 0x10ec0260, .name = "ALC260", .patch = patch_alc260 },
7081         { .id = 0x10ec0262, .name = "ALC262", .patch = patch_alc262 },
7082         { .id = 0x10ec0267, .name = "ALC267", .patch = patch_alc268 },
7083         { .id = 0x10ec0268, .name = "ALC268", .patch = patch_alc268 },
7084         { .id = 0x10ec0269, .name = "ALC269", .patch = patch_alc269 },
7085         { .id = 0x10ec0270, .name = "ALC270", .patch = patch_alc269 },
7086         { .id = 0x10ec0272, .name = "ALC272", .patch = patch_alc662 },
7087         { .id = 0x10ec0275, .name = "ALC275", .patch = patch_alc269 },
7088         { .id = 0x10ec0276, .name = "ALC276", .patch = patch_alc269 },
7089         { .id = 0x10ec0280, .name = "ALC280", .patch = patch_alc269 },
7090         { .id = 0x10ec0282, .name = "ALC282", .patch = patch_alc269 },
7091         { .id = 0x10ec0861, .rev = 0x100340, .name = "ALC660",
7092           .patch = patch_alc861 },
7093         { .id = 0x10ec0660, .name = "ALC660-VD", .patch = patch_alc861vd },
7094         { .id = 0x10ec0861, .name = "ALC861", .patch = patch_alc861 },
7095         { .id = 0x10ec0862, .name = "ALC861-VD", .patch = patch_alc861vd },
7096         { .id = 0x10ec0662, .rev = 0x100002, .name = "ALC662 rev2",
7097           .patch = patch_alc882 },
7098         { .id = 0x10ec0662, .rev = 0x100101, .name = "ALC662 rev1",
7099           .patch = patch_alc662 },
7100         { .id = 0x10ec0662, .rev = 0x100300, .name = "ALC662 rev3",
7101           .patch = patch_alc662 },
7102         { .id = 0x10ec0663, .name = "ALC663", .patch = patch_alc662 },
7103         { .id = 0x10ec0665, .name = "ALC665", .patch = patch_alc662 },
7104         { .id = 0x10ec0668, .name = "ALC668", .patch = patch_alc662 },
7105         { .id = 0x10ec0670, .name = "ALC670", .patch = patch_alc662 },
7106         { .id = 0x10ec0680, .name = "ALC680", .patch = patch_alc680 },
7107         { .id = 0x10ec0880, .name = "ALC880", .patch = patch_alc880 },
7108         { .id = 0x10ec0882, .name = "ALC882", .patch = patch_alc882 },
7109         { .id = 0x10ec0883, .name = "ALC883", .patch = patch_alc882 },
7110         { .id = 0x10ec0885, .rev = 0x100101, .name = "ALC889A",
7111           .patch = patch_alc882 },
7112         { .id = 0x10ec0885, .rev = 0x100103, .name = "ALC889A",
7113           .patch = patch_alc882 },
7114         { .id = 0x10ec0885, .name = "ALC885", .patch = patch_alc882 },
7115         { .id = 0x10ec0887, .name = "ALC887", .patch = patch_alc882 },
7116         { .id = 0x10ec0888, .rev = 0x100101, .name = "ALC1200",
7117           .patch = patch_alc882 },
7118         { .id = 0x10ec0888, .name = "ALC888", .patch = patch_alc882 },
7119         { .id = 0x10ec0889, .name = "ALC889", .patch = patch_alc882 },
7120         { .id = 0x10ec0892, .name = "ALC892", .patch = patch_alc662 },
7121         { .id = 0x10ec0899, .name = "ALC898", .patch = patch_alc882 },
7122         { .id = 0x10ec0900, .name = "ALC1150", .patch = patch_alc882 },
7123         {} /* terminator */
7124 };
7125
7126 MODULE_ALIAS("snd-hda-codec-id:10ec*");
7127
7128 MODULE_LICENSE("GPL");
7129 MODULE_DESCRIPTION("Realtek HD-audio codec");
7130
7131 static struct hda_codec_preset_list realtek_list = {
7132         .preset = snd_hda_preset_realtek,
7133         .owner = THIS_MODULE,
7134 };
7135
7136 static int __init patch_realtek_init(void)
7137 {
7138         return snd_hda_add_codec_preset(&realtek_list);
7139 }
7140
7141 static void __exit patch_realtek_exit(void)
7142 {
7143         snd_hda_delete_codec_preset(&realtek_list);
7144 }
7145
7146 module_init(patch_realtek_init)
7147 module_exit(patch_realtek_exit)