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