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