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