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