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