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