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