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