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