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