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