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