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