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