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