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