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