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