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