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