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