Merge branch 'fix/hda' into topic/hda
[~shefty/rdma-dev.git] / sound / pci / hda / patch_realtek.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * HD audio interface patch for Realtek ALC codecs
5  *
6  * Copyright (c) 2004 Kailang Yang <kailang@realtek.com.tw>
7  *                    PeiSen Hou <pshou@realtek.com.tw>
8  *                    Takashi Iwai <tiwai@suse.de>
9  *                    Jonathan Woithe <jwoithe@physics.adelaide.edu.au>
10  *
11  *  This driver is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This driver is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
24  */
25
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/pci.h>
30 #include <linux/module.h>
31 #include <sound/core.h>
32 #include <sound/jack.h>
33 #include "hda_codec.h"
34 #include "hda_local.h"
35 #include "hda_beep.h"
36 #include "hda_jack.h"
37
38 /* unsol event tags */
39 #define ALC_FRONT_EVENT         0x01
40 #define ALC_DCVOL_EVENT         0x02
41 #define ALC_HP_EVENT            0x04
42 #define ALC_MIC_EVENT           0x08
43
44 /* for GPIO Poll */
45 #define GPIO_MASK       0x03
46
47 /* extra amp-initialization sequence types */
48 enum {
49         ALC_INIT_NONE,
50         ALC_INIT_DEFAULT,
51         ALC_INIT_GPIO1,
52         ALC_INIT_GPIO2,
53         ALC_INIT_GPIO3,
54 };
55
56 struct alc_customize_define {
57         unsigned int  sku_cfg;
58         unsigned char port_connectivity;
59         unsigned char check_sum;
60         unsigned char customization;
61         unsigned char external_amp;
62         unsigned int  enable_pcbeep:1;
63         unsigned int  platform_type:1;
64         unsigned int  swap:1;
65         unsigned int  override:1;
66         unsigned int  fixup:1; /* Means that this sku is set by driver, not read from hw */
67 };
68
69 struct alc_fixup;
70
71 struct alc_multi_io {
72         hda_nid_t pin;          /* multi-io widget pin NID */
73         hda_nid_t dac;          /* DAC to be connected */
74         unsigned int ctl_in;    /* cached input-pin control value */
75 };
76
77 enum {
78         ALC_AUTOMUTE_PIN,       /* change the pin control */
79         ALC_AUTOMUTE_AMP,       /* mute/unmute the pin AMP */
80         ALC_AUTOMUTE_MIXER,     /* mute/unmute mixer widget AMP */
81 };
82
83 struct alc_spec {
84         /* codec parameterization */
85         const struct snd_kcontrol_new *mixers[5];       /* mixer arrays */
86         unsigned int num_mixers;
87         const struct snd_kcontrol_new *cap_mixer;       /* capture mixer */
88         unsigned int beep_amp;  /* beep amp value, set via set_beep_amp() */
89
90         const struct hda_verb *init_verbs[10];  /* initialization verbs
91                                                  * don't forget NULL
92                                                  * termination!
93                                                  */
94         unsigned int num_init_verbs;
95
96         char stream_name_analog[32];    /* analog PCM stream */
97         const struct hda_pcm_stream *stream_analog_playback;
98         const struct hda_pcm_stream *stream_analog_capture;
99         const struct hda_pcm_stream *stream_analog_alt_playback;
100         const struct hda_pcm_stream *stream_analog_alt_capture;
101
102         char stream_name_digital[32];   /* digital PCM stream */
103         const struct hda_pcm_stream *stream_digital_playback;
104         const struct hda_pcm_stream *stream_digital_capture;
105
106         /* playback */
107         struct hda_multi_out multiout;  /* playback set-up
108                                          * max_channels, dacs must be set
109                                          * dig_out_nid and hp_nid are optional
110                                          */
111         hda_nid_t alt_dac_nid;
112         hda_nid_t slave_dig_outs[3];    /* optional - for auto-parsing */
113         int dig_out_type;
114
115         /* capture */
116         unsigned int num_adc_nids;
117         const hda_nid_t *adc_nids;
118         const hda_nid_t *capsrc_nids;
119         hda_nid_t dig_in_nid;           /* digital-in NID; optional */
120         hda_nid_t mixer_nid;            /* analog-mixer NID */
121         DECLARE_BITMAP(vol_ctls, 0x20 << 1);
122         DECLARE_BITMAP(sw_ctls, 0x20 << 1);
123
124         /* capture setup for dynamic dual-adc switch */
125         hda_nid_t cur_adc;
126         unsigned int cur_adc_stream_tag;
127         unsigned int cur_adc_format;
128
129         /* capture source */
130         unsigned int num_mux_defs;
131         const struct hda_input_mux *input_mux;
132         unsigned int cur_mux[3];
133         hda_nid_t ext_mic_pin;
134         hda_nid_t dock_mic_pin;
135         hda_nid_t int_mic_pin;
136
137         /* channel model */
138         const struct hda_channel_mode *channel_mode;
139         int num_channel_mode;
140         int need_dac_fix;
141         int const_channel_count;
142         int ext_channel_count;
143
144         /* PCM information */
145         struct hda_pcm pcm_rec[3];      /* used in alc_build_pcms() */
146
147         /* dynamic controls, init_verbs and input_mux */
148         struct auto_pin_cfg autocfg;
149         struct alc_customize_define cdefine;
150         struct snd_array kctls;
151         struct hda_input_mux private_imux[3];
152         hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
153         hda_nid_t private_adc_nids[AUTO_CFG_MAX_OUTS];
154         hda_nid_t private_capsrc_nids[AUTO_CFG_MAX_OUTS];
155         hda_nid_t imux_pins[HDA_MAX_NUM_INPUTS];
156         unsigned int dyn_adc_idx[HDA_MAX_NUM_INPUTS];
157         int int_mic_idx, ext_mic_idx, dock_mic_idx; /* for auto-mic */
158
159         /* hooks */
160         void (*init_hook)(struct hda_codec *codec);
161         void (*unsol_event)(struct hda_codec *codec, unsigned int res);
162 #ifdef CONFIG_SND_HDA_POWER_SAVE
163         void (*power_hook)(struct hda_codec *codec);
164 #endif
165         void (*shutup)(struct hda_codec *codec);
166         void (*automute_hook)(struct hda_codec *codec);
167
168         /* for pin sensing */
169         unsigned int hp_jack_present:1;
170         unsigned int line_jack_present:1;
171         unsigned int master_mute:1;
172         unsigned int auto_mic:1;
173         unsigned int auto_mic_valid_imux:1;     /* valid imux for auto-mic */
174         unsigned int automute_speaker:1; /* automute speaker outputs */
175         unsigned int automute_lo:1; /* automute LO outputs */
176         unsigned int detect_hp:1;       /* Headphone detection enabled */
177         unsigned int detect_lo:1;       /* Line-out detection enabled */
178         unsigned int automute_speaker_possible:1; /* there are speakers and either LO or HP */
179         unsigned int automute_lo_possible:1;      /* there are line outs and HP */
180
181         /* other flags */
182         unsigned int no_analog :1; /* digital I/O only */
183         unsigned int dyn_adc_switch:1; /* switch ADCs (for ALC275) */
184         unsigned int single_input_src:1;
185         unsigned int vol_in_capsrc:1; /* use capsrc volume (ADC has no vol) */
186         unsigned int parse_flags; /* passed to snd_hda_parse_pin_defcfg() */
187         unsigned int shared_mic_hp:1; /* HP/Mic-in sharing */
188
189         /* auto-mute control */
190         int automute_mode;
191         hda_nid_t automute_mixer_nid[AUTO_CFG_MAX_OUTS];
192
193         int init_amp;
194         int codec_variant;      /* flag for other variants */
195
196         /* for virtual master */
197         hda_nid_t vmaster_nid;
198 #ifdef CONFIG_SND_HDA_POWER_SAVE
199         struct hda_loopback_check loopback;
200 #endif
201
202         /* for PLL fix */
203         hda_nid_t pll_nid;
204         unsigned int pll_coef_idx, pll_coef_bit;
205         unsigned int coef0;
206
207         /* fix-up list */
208         int fixup_id;
209         const struct alc_fixup *fixup_list;
210         const char *fixup_name;
211
212         /* multi-io */
213         int multi_ios;
214         struct alc_multi_io multi_io[4];
215
216         /* bind volumes */
217         struct snd_array bind_ctls;
218 };
219
220 #define ALC_MODEL_AUTO          0       /* common for all chips */
221
222 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
223                            int dir, unsigned int bits)
224 {
225         if (!nid)
226                 return false;
227         if (get_wcaps(codec, nid) & (1 << (dir + 1)))
228                 if (query_amp_caps(codec, nid, dir) & bits)
229                         return true;
230         return false;
231 }
232
233 #define nid_has_mute(codec, nid, dir) \
234         check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
235 #define nid_has_volume(codec, nid, dir) \
236         check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
237
238 /*
239  * input MUX handling
240  */
241 static int alc_mux_enum_info(struct snd_kcontrol *kcontrol,
242                              struct snd_ctl_elem_info *uinfo)
243 {
244         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
245         struct alc_spec *spec = codec->spec;
246         unsigned int mux_idx = snd_ctl_get_ioffidx(kcontrol, &uinfo->id);
247         if (mux_idx >= spec->num_mux_defs)
248                 mux_idx = 0;
249         if (!spec->input_mux[mux_idx].num_items && mux_idx > 0)
250                 mux_idx = 0;
251         return snd_hda_input_mux_info(&spec->input_mux[mux_idx], uinfo);
252 }
253
254 static int alc_mux_enum_get(struct snd_kcontrol *kcontrol,
255                             struct snd_ctl_elem_value *ucontrol)
256 {
257         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
258         struct alc_spec *spec = codec->spec;
259         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
260
261         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
262         return 0;
263 }
264
265 static bool alc_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
266 {
267         struct alc_spec *spec = codec->spec;
268         hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
269
270         if (spec->cur_adc && spec->cur_adc != new_adc) {
271                 /* stream is running, let's swap the current ADC */
272                 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
273                 spec->cur_adc = new_adc;
274                 snd_hda_codec_setup_stream(codec, new_adc,
275                                            spec->cur_adc_stream_tag, 0,
276                                            spec->cur_adc_format);
277                 return true;
278         }
279         return false;
280 }
281
282 static inline hda_nid_t get_capsrc(struct alc_spec *spec, int idx)
283 {
284         return spec->capsrc_nids ?
285                 spec->capsrc_nids[idx] : spec->adc_nids[idx];
286 }
287
288 static void call_update_outputs(struct hda_codec *codec);
289
290 /* select the given imux item; either unmute exclusively or select the route */
291 static int alc_mux_select(struct hda_codec *codec, unsigned int adc_idx,
292                           unsigned int idx, bool force)
293 {
294         struct alc_spec *spec = codec->spec;
295         const struct hda_input_mux *imux;
296         unsigned int mux_idx;
297         int i, type, num_conns;
298         hda_nid_t nid;
299
300         mux_idx = adc_idx >= spec->num_mux_defs ? 0 : adc_idx;
301         imux = &spec->input_mux[mux_idx];
302         if (!imux->num_items && mux_idx > 0)
303                 imux = &spec->input_mux[0];
304         if (!imux->num_items)
305                 return 0;
306
307         if (idx >= imux->num_items)
308                 idx = imux->num_items - 1;
309         if (spec->cur_mux[adc_idx] == idx && !force)
310                 return 0;
311         spec->cur_mux[adc_idx] = idx;
312
313         /* for shared I/O, change the pin-control accordingly */
314         if (spec->shared_mic_hp) {
315                 /* NOTE: this assumes that there are only two inputs, the
316                  * first is the real internal mic and the second is HP jack.
317                  */
318                 snd_hda_codec_write(codec, spec->autocfg.inputs[1].pin, 0,
319                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
320                                     spec->cur_mux[adc_idx] ?
321                                     PIN_VREF80 : PIN_HP);
322                 spec->automute_speaker = !spec->cur_mux[adc_idx];
323                 call_update_outputs(codec);
324         }
325
326         if (spec->dyn_adc_switch) {
327                 alc_dyn_adc_pcm_resetup(codec, idx);
328                 adc_idx = spec->dyn_adc_idx[idx];
329         }
330
331         nid = get_capsrc(spec, adc_idx);
332
333         /* no selection? */
334         num_conns = snd_hda_get_conn_list(codec, nid, NULL);
335         if (num_conns <= 1)
336                 return 1;
337
338         type = get_wcaps_type(get_wcaps(codec, nid));
339         if (type == AC_WID_AUD_MIX) {
340                 /* Matrix-mixer style (e.g. ALC882) */
341                 int active = imux->items[idx].index;
342                 for (i = 0; i < num_conns; i++) {
343                         unsigned int v = (i == active) ? 0 : HDA_AMP_MUTE;
344                         snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, i,
345                                                  HDA_AMP_MUTE, v);
346                 }
347         } else {
348                 /* MUX style (e.g. ALC880) */
349                 snd_hda_codec_write_cache(codec, nid, 0,
350                                           AC_VERB_SET_CONNECT_SEL,
351                                           imux->items[idx].index);
352         }
353         return 1;
354 }
355
356 static int alc_mux_enum_put(struct snd_kcontrol *kcontrol,
357                             struct snd_ctl_elem_value *ucontrol)
358 {
359         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
360         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
361         return alc_mux_select(codec, adc_idx,
362                               ucontrol->value.enumerated.item[0], false);
363 }
364
365 /*
366  * set up the input pin config (depending on the given auto-pin type)
367  */
368 static void alc_set_input_pin(struct hda_codec *codec, hda_nid_t nid,
369                               int auto_pin_type)
370 {
371         unsigned int val = PIN_IN;
372
373         if (auto_pin_type == AUTO_PIN_MIC) {
374                 unsigned int pincap;
375                 unsigned int oldval;
376                 oldval = snd_hda_codec_read(codec, nid, 0,
377                                             AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
378                 pincap = snd_hda_query_pin_caps(codec, nid);
379                 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
380                 /* if the default pin setup is vref50, we give it priority */
381                 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
382                         val = PIN_VREF80;
383                 else if (pincap & AC_PINCAP_VREF_50)
384                         val = PIN_VREF50;
385                 else if (pincap & AC_PINCAP_VREF_100)
386                         val = PIN_VREF100;
387                 else if (pincap & AC_PINCAP_VREF_GRD)
388                         val = PIN_VREFGRD;
389         }
390         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, val);
391 }
392
393 /*
394  * Append the given mixer and verb elements for the later use
395  * The mixer array is referred in build_controls(), and init_verbs are
396  * called in init().
397  */
398 static void add_mixer(struct alc_spec *spec, const struct snd_kcontrol_new *mix)
399 {
400         if (snd_BUG_ON(spec->num_mixers >= ARRAY_SIZE(spec->mixers)))
401                 return;
402         spec->mixers[spec->num_mixers++] = mix;
403 }
404
405 static void add_verb(struct alc_spec *spec, const struct hda_verb *verb)
406 {
407         if (snd_BUG_ON(spec->num_init_verbs >= ARRAY_SIZE(spec->init_verbs)))
408                 return;
409         spec->init_verbs[spec->num_init_verbs++] = verb;
410 }
411
412 /*
413  * GPIO setup tables, used in initialization
414  */
415 /* Enable GPIO mask and set output */
416 static const struct hda_verb alc_gpio1_init_verbs[] = {
417         {0x01, AC_VERB_SET_GPIO_MASK, 0x01},
418         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
419         {0x01, AC_VERB_SET_GPIO_DATA, 0x01},
420         { }
421 };
422
423 static const struct hda_verb alc_gpio2_init_verbs[] = {
424         {0x01, AC_VERB_SET_GPIO_MASK, 0x02},
425         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
426         {0x01, AC_VERB_SET_GPIO_DATA, 0x02},
427         { }
428 };
429
430 static const struct hda_verb alc_gpio3_init_verbs[] = {
431         {0x01, AC_VERB_SET_GPIO_MASK, 0x03},
432         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x03},
433         {0x01, AC_VERB_SET_GPIO_DATA, 0x03},
434         { }
435 };
436
437 /*
438  * Fix hardware PLL issue
439  * On some codecs, the analog PLL gating control must be off while
440  * the default value is 1.
441  */
442 static void alc_fix_pll(struct hda_codec *codec)
443 {
444         struct alc_spec *spec = codec->spec;
445         unsigned int val;
446
447         if (!spec->pll_nid)
448                 return;
449         snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
450                             spec->pll_coef_idx);
451         val = snd_hda_codec_read(codec, spec->pll_nid, 0,
452                                  AC_VERB_GET_PROC_COEF, 0);
453         snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
454                             spec->pll_coef_idx);
455         snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_PROC_COEF,
456                             val & ~(1 << spec->pll_coef_bit));
457 }
458
459 static void alc_fix_pll_init(struct hda_codec *codec, hda_nid_t nid,
460                              unsigned int coef_idx, unsigned int coef_bit)
461 {
462         struct alc_spec *spec = codec->spec;
463         spec->pll_nid = nid;
464         spec->pll_coef_idx = coef_idx;
465         spec->pll_coef_bit = coef_bit;
466         alc_fix_pll(codec);
467 }
468
469 /*
470  * Jack detections for HP auto-mute and mic-switch
471  */
472
473 /* check each pin in the given array; returns true if any of them is plugged */
474 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
475 {
476         int i, present = 0;
477
478         for (i = 0; i < num_pins; i++) {
479                 hda_nid_t nid = pins[i];
480                 if (!nid)
481                         break;
482                 present |= snd_hda_jack_detect(codec, nid);
483         }
484         return present;
485 }
486
487 /* standard HP/line-out auto-mute helper */
488 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
489                         bool mute, bool hp_out)
490 {
491         struct alc_spec *spec = codec->spec;
492         unsigned int mute_bits = mute ? HDA_AMP_MUTE : 0;
493         unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
494         int i;
495
496         for (i = 0; i < num_pins; i++) {
497                 hda_nid_t nid = pins[i];
498                 if (!nid)
499                         break;
500                 switch (spec->automute_mode) {
501                 case ALC_AUTOMUTE_PIN:
502                         snd_hda_codec_write(codec, nid, 0,
503                                             AC_VERB_SET_PIN_WIDGET_CONTROL,
504                                             pin_bits);
505                         break;
506                 case ALC_AUTOMUTE_AMP:
507                         snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
508                                                  HDA_AMP_MUTE, mute_bits);
509                         break;
510                 case ALC_AUTOMUTE_MIXER:
511                         nid = spec->automute_mixer_nid[i];
512                         if (!nid)
513                                 break;
514                         snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 0,
515                                                  HDA_AMP_MUTE, mute_bits);
516                         snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 1,
517                                                  HDA_AMP_MUTE, mute_bits);
518                         break;
519                 }
520         }
521 }
522
523 /* Toggle outputs muting */
524 static void update_outputs(struct hda_codec *codec)
525 {
526         struct alc_spec *spec = codec->spec;
527         int on;
528
529         /* Control HP pins/amps depending on master_mute state;
530          * in general, HP pins/amps control should be enabled in all cases,
531          * but currently set only for master_mute, just to be safe
532          */
533         if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
534                 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
535                     spec->autocfg.hp_pins, spec->master_mute, true);
536
537         if (!spec->automute_speaker)
538                 on = 0;
539         else
540                 on = spec->hp_jack_present | spec->line_jack_present;
541         on |= spec->master_mute;
542         do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
543                     spec->autocfg.speaker_pins, on, false);
544
545         /* toggle line-out mutes if needed, too */
546         /* if LO is a copy of either HP or Speaker, don't need to handle it */
547         if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
548             spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
549                 return;
550         if (!spec->automute_lo)
551                 on = 0;
552         else
553                 on = spec->hp_jack_present;
554         on |= spec->master_mute;
555         do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
556                     spec->autocfg.line_out_pins, on, false);
557 }
558
559 static void call_update_outputs(struct hda_codec *codec)
560 {
561         struct alc_spec *spec = codec->spec;
562         if (spec->automute_hook)
563                 spec->automute_hook(codec);
564         else
565                 update_outputs(codec);
566 }
567
568 /* standard HP-automute helper */
569 static void alc_hp_automute(struct hda_codec *codec)
570 {
571         struct alc_spec *spec = codec->spec;
572
573         spec->hp_jack_present =
574                 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
575                              spec->autocfg.hp_pins);
576         if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
577                 return;
578         call_update_outputs(codec);
579 }
580
581 /* standard line-out-automute helper */
582 static void alc_line_automute(struct hda_codec *codec)
583 {
584         struct alc_spec *spec = codec->spec;
585
586         /* check LO jack only when it's different from HP */
587         if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
588                 return;
589
590         spec->line_jack_present =
591                 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
592                              spec->autocfg.line_out_pins);
593         if (!spec->automute_speaker || !spec->detect_lo)
594                 return;
595         call_update_outputs(codec);
596 }
597
598 #define get_connection_index(codec, mux, nid) \
599         snd_hda_get_conn_index(codec, mux, nid, 0)
600
601 /* standard mic auto-switch helper */
602 static void alc_mic_automute(struct hda_codec *codec)
603 {
604         struct alc_spec *spec = codec->spec;
605         hda_nid_t *pins = spec->imux_pins;
606
607         if (!spec->auto_mic || !spec->auto_mic_valid_imux)
608                 return;
609         if (snd_BUG_ON(!spec->adc_nids))
610                 return;
611         if (snd_BUG_ON(spec->int_mic_idx < 0 || spec->ext_mic_idx < 0))
612                 return;
613
614         if (snd_hda_jack_detect(codec, pins[spec->ext_mic_idx]))
615                 alc_mux_select(codec, 0, spec->ext_mic_idx, false);
616         else if (spec->dock_mic_idx >= 0 &&
617                    snd_hda_jack_detect(codec, pins[spec->dock_mic_idx]))
618                 alc_mux_select(codec, 0, spec->dock_mic_idx, false);
619         else
620                 alc_mux_select(codec, 0, spec->int_mic_idx, false);
621 }
622
623 /* handle the specified unsol action (ALC_XXX_EVENT) */
624 static void alc_exec_unsol_event(struct hda_codec *codec, int action)
625 {
626         switch (action) {
627         case ALC_HP_EVENT:
628                 alc_hp_automute(codec);
629                 break;
630         case ALC_FRONT_EVENT:
631                 alc_line_automute(codec);
632                 break;
633         case ALC_MIC_EVENT:
634                 alc_mic_automute(codec);
635                 break;
636         }
637         snd_hda_jack_report_sync(codec);
638 }
639
640 /* unsolicited event for HP jack sensing */
641 static void alc_sku_unsol_event(struct hda_codec *codec, unsigned int res)
642 {
643         if (codec->vendor_id == 0x10ec0880)
644                 res >>= 28;
645         else
646                 res >>= 26;
647         res = snd_hda_jack_get_action(codec, res);
648         alc_exec_unsol_event(codec, res);
649 }
650
651 /* call init functions of standard auto-mute helpers */
652 static void alc_inithook(struct hda_codec *codec)
653 {
654         alc_hp_automute(codec);
655         alc_line_automute(codec);
656         alc_mic_automute(codec);
657 }
658
659 /* additional initialization for ALC888 variants */
660 static void alc888_coef_init(struct hda_codec *codec)
661 {
662         unsigned int tmp;
663
664         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 0);
665         tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
666         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
667         if ((tmp & 0xf0) == 0x20)
668                 /* alc888S-VC */
669                 snd_hda_codec_read(codec, 0x20, 0,
670                                    AC_VERB_SET_PROC_COEF, 0x830);
671          else
672                  /* alc888-VB */
673                  snd_hda_codec_read(codec, 0x20, 0,
674                                     AC_VERB_SET_PROC_COEF, 0x3030);
675 }
676
677 /* additional initialization for ALC889 variants */
678 static void alc889_coef_init(struct hda_codec *codec)
679 {
680         unsigned int tmp;
681
682         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
683         tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
684         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
685         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_PROC_COEF, tmp|0x2010);
686 }
687
688 /* turn on/off EAPD control (only if available) */
689 static void set_eapd(struct hda_codec *codec, hda_nid_t nid, int on)
690 {
691         if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
692                 return;
693         if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
694                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_EAPD_BTLENABLE,
695                                     on ? 2 : 0);
696 }
697
698 /* turn on/off EAPD controls of the codec */
699 static void alc_auto_setup_eapd(struct hda_codec *codec, bool on)
700 {
701         /* We currently only handle front, HP */
702         static hda_nid_t pins[] = {
703                 0x0f, 0x10, 0x14, 0x15, 0
704         };
705         hda_nid_t *p;
706         for (p = pins; *p; p++)
707                 set_eapd(codec, *p, on);
708 }
709
710 /* generic shutup callback;
711  * just turning off EPAD and a little pause for avoiding pop-noise
712  */
713 static void alc_eapd_shutup(struct hda_codec *codec)
714 {
715         alc_auto_setup_eapd(codec, false);
716         msleep(200);
717 }
718
719 /* generic EAPD initialization */
720 static void alc_auto_init_amp(struct hda_codec *codec, int type)
721 {
722         unsigned int tmp;
723
724         alc_auto_setup_eapd(codec, true);
725         switch (type) {
726         case ALC_INIT_GPIO1:
727                 snd_hda_sequence_write(codec, alc_gpio1_init_verbs);
728                 break;
729         case ALC_INIT_GPIO2:
730                 snd_hda_sequence_write(codec, alc_gpio2_init_verbs);
731                 break;
732         case ALC_INIT_GPIO3:
733                 snd_hda_sequence_write(codec, alc_gpio3_init_verbs);
734                 break;
735         case ALC_INIT_DEFAULT:
736                 switch (codec->vendor_id) {
737                 case 0x10ec0260:
738                         snd_hda_codec_write(codec, 0x1a, 0,
739                                             AC_VERB_SET_COEF_INDEX, 7);
740                         tmp = snd_hda_codec_read(codec, 0x1a, 0,
741                                                  AC_VERB_GET_PROC_COEF, 0);
742                         snd_hda_codec_write(codec, 0x1a, 0,
743                                             AC_VERB_SET_COEF_INDEX, 7);
744                         snd_hda_codec_write(codec, 0x1a, 0,
745                                             AC_VERB_SET_PROC_COEF,
746                                             tmp | 0x2010);
747                         break;
748                 case 0x10ec0262:
749                 case 0x10ec0880:
750                 case 0x10ec0882:
751                 case 0x10ec0883:
752                 case 0x10ec0885:
753                 case 0x10ec0887:
754                 /*case 0x10ec0889:*/ /* this causes an SPDIF problem */
755                         alc889_coef_init(codec);
756                         break;
757                 case 0x10ec0888:
758                         alc888_coef_init(codec);
759                         break;
760 #if 0 /* XXX: This may cause the silent output on speaker on some machines */
761                 case 0x10ec0267:
762                 case 0x10ec0268:
763                         snd_hda_codec_write(codec, 0x20, 0,
764                                             AC_VERB_SET_COEF_INDEX, 7);
765                         tmp = snd_hda_codec_read(codec, 0x20, 0,
766                                                  AC_VERB_GET_PROC_COEF, 0);
767                         snd_hda_codec_write(codec, 0x20, 0,
768                                             AC_VERB_SET_COEF_INDEX, 7);
769                         snd_hda_codec_write(codec, 0x20, 0,
770                                             AC_VERB_SET_PROC_COEF,
771                                             tmp | 0x3000);
772                         break;
773 #endif /* XXX */
774                 }
775                 break;
776         }
777 }
778
779 /*
780  * Auto-Mute mode mixer enum support
781  */
782 static int alc_automute_mode_info(struct snd_kcontrol *kcontrol,
783                                   struct snd_ctl_elem_info *uinfo)
784 {
785         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
786         struct alc_spec *spec = codec->spec;
787         static const char * const texts2[] = {
788                 "Disabled", "Enabled"
789         };
790         static const char * const texts3[] = {
791                 "Disabled", "Speaker Only", "Line-Out+Speaker"
792         };
793         const char * const *texts;
794
795         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
796         uinfo->count = 1;
797         if (spec->automute_speaker_possible && spec->automute_lo_possible) {
798                 uinfo->value.enumerated.items = 3;
799                 texts = texts3;
800         } else {
801                 uinfo->value.enumerated.items = 2;
802                 texts = texts2;
803         }
804         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
805                 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
806         strcpy(uinfo->value.enumerated.name,
807                texts[uinfo->value.enumerated.item]);
808         return 0;
809 }
810
811 static int alc_automute_mode_get(struct snd_kcontrol *kcontrol,
812                                  struct snd_ctl_elem_value *ucontrol)
813 {
814         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
815         struct alc_spec *spec = codec->spec;
816         unsigned int val = 0;
817         if (spec->automute_speaker)
818                 val++;
819         if (spec->automute_lo)
820                 val++;
821
822         ucontrol->value.enumerated.item[0] = val;
823         return 0;
824 }
825
826 static int alc_automute_mode_put(struct snd_kcontrol *kcontrol,
827                                  struct snd_ctl_elem_value *ucontrol)
828 {
829         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
830         struct alc_spec *spec = codec->spec;
831
832         switch (ucontrol->value.enumerated.item[0]) {
833         case 0:
834                 if (!spec->automute_speaker && !spec->automute_lo)
835                         return 0;
836                 spec->automute_speaker = 0;
837                 spec->automute_lo = 0;
838                 break;
839         case 1:
840                 if (spec->automute_speaker_possible) {
841                         if (!spec->automute_lo && spec->automute_speaker)
842                                 return 0;
843                         spec->automute_speaker = 1;
844                         spec->automute_lo = 0;
845                 } else if (spec->automute_lo_possible) {
846                         if (spec->automute_lo)
847                                 return 0;
848                         spec->automute_lo = 1;
849                 } else
850                         return -EINVAL;
851                 break;
852         case 2:
853                 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
854                         return -EINVAL;
855                 if (spec->automute_speaker && spec->automute_lo)
856                         return 0;
857                 spec->automute_speaker = 1;
858                 spec->automute_lo = 1;
859                 break;
860         default:
861                 return -EINVAL;
862         }
863         call_update_outputs(codec);
864         return 1;
865 }
866
867 static const struct snd_kcontrol_new alc_automute_mode_enum = {
868         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
869         .name = "Auto-Mute Mode",
870         .info = alc_automute_mode_info,
871         .get = alc_automute_mode_get,
872         .put = alc_automute_mode_put,
873 };
874
875 static struct snd_kcontrol_new *alc_kcontrol_new(struct alc_spec *spec)
876 {
877         snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
878         return snd_array_new(&spec->kctls);
879 }
880
881 static int alc_add_automute_mode_enum(struct hda_codec *codec)
882 {
883         struct alc_spec *spec = codec->spec;
884         struct snd_kcontrol_new *knew;
885
886         knew = alc_kcontrol_new(spec);
887         if (!knew)
888                 return -ENOMEM;
889         *knew = alc_automute_mode_enum;
890         knew->name = kstrdup("Auto-Mute Mode", GFP_KERNEL);
891         if (!knew->name)
892                 return -ENOMEM;
893         return 0;
894 }
895
896 /*
897  * Check the availability of HP/line-out auto-mute;
898  * Set up appropriately if really supported
899  */
900 static void alc_init_automute(struct hda_codec *codec)
901 {
902         struct alc_spec *spec = codec->spec;
903         struct auto_pin_cfg *cfg = &spec->autocfg;
904         int present = 0;
905         int i;
906
907         if (cfg->hp_pins[0])
908                 present++;
909         if (cfg->line_out_pins[0])
910                 present++;
911         if (cfg->speaker_pins[0])
912                 present++;
913         if (present < 2) /* need two different output types */
914                 return;
915
916         if (!cfg->speaker_pins[0] &&
917             cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
918                 memcpy(cfg->speaker_pins, cfg->line_out_pins,
919                        sizeof(cfg->speaker_pins));
920                 cfg->speaker_outs = cfg->line_outs;
921         }
922
923         if (!cfg->hp_pins[0] &&
924             cfg->line_out_type == AUTO_PIN_HP_OUT) {
925                 memcpy(cfg->hp_pins, cfg->line_out_pins,
926                        sizeof(cfg->hp_pins));
927                 cfg->hp_outs = cfg->line_outs;
928         }
929
930         spec->automute_mode = ALC_AUTOMUTE_PIN;
931
932         for (i = 0; i < cfg->hp_outs; i++) {
933                 hda_nid_t nid = cfg->hp_pins[i];
934                 if (!is_jack_detectable(codec, nid))
935                         continue;
936                 snd_printdd("realtek: Enable HP auto-muting on NID 0x%x\n",
937                             nid);
938                 snd_hda_jack_detect_enable(codec, nid, ALC_HP_EVENT);
939                 spec->detect_hp = 1;
940         }
941
942         if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
943                 if (cfg->speaker_outs)
944                         for (i = 0; i < cfg->line_outs; i++) {
945                                 hda_nid_t nid = cfg->line_out_pins[i];
946                                 if (!is_jack_detectable(codec, nid))
947                                         continue;
948                                 snd_printdd("realtek: Enable Line-Out "
949                                             "auto-muting on NID 0x%x\n", nid);
950                                 snd_hda_jack_detect_enable(codec, nid,
951                                                            ALC_FRONT_EVENT);
952                                 spec->detect_lo = 1;
953                 }
954                 spec->automute_lo_possible = spec->detect_hp;
955         }
956
957         spec->automute_speaker_possible = cfg->speaker_outs &&
958                 (spec->detect_hp || spec->detect_lo);
959
960         spec->automute_lo = spec->automute_lo_possible;
961         spec->automute_speaker = spec->automute_speaker_possible;
962
963         if (spec->automute_speaker_possible || spec->automute_lo_possible) {
964                 /* create a control for automute mode */
965                 alc_add_automute_mode_enum(codec);
966                 spec->unsol_event = alc_sku_unsol_event;
967         }
968 }
969
970 /* return the position of NID in the list, or -1 if not found */
971 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
972 {
973         int i;
974         for (i = 0; i < nums; i++)
975                 if (list[i] == nid)
976                         return i;
977         return -1;
978 }
979
980 /* check whether dynamic ADC-switching is available */
981 static bool alc_check_dyn_adc_switch(struct hda_codec *codec)
982 {
983         struct alc_spec *spec = codec->spec;
984         struct hda_input_mux *imux = &spec->private_imux[0];
985         int i, n, idx;
986         hda_nid_t cap, pin;
987
988         if (imux != spec->input_mux) /* no dynamic imux? */
989                 return false;
990
991         for (n = 0; n < spec->num_adc_nids; n++) {
992                 cap = spec->private_capsrc_nids[n];
993                 for (i = 0; i < imux->num_items; i++) {
994                         pin = spec->imux_pins[i];
995                         if (!pin)
996                                 return false;
997                         if (get_connection_index(codec, cap, pin) < 0)
998                                 break;
999                 }
1000                 if (i >= imux->num_items)
1001                         return true; /* no ADC-switch is needed */
1002         }
1003
1004         for (i = 0; i < imux->num_items; i++) {
1005                 pin = spec->imux_pins[i];
1006                 for (n = 0; n < spec->num_adc_nids; n++) {
1007                         cap = spec->private_capsrc_nids[n];
1008                         idx = get_connection_index(codec, cap, pin);
1009                         if (idx >= 0) {
1010                                 imux->items[i].index = idx;
1011                                 spec->dyn_adc_idx[i] = n;
1012                                 break;
1013                         }
1014                 }
1015         }
1016
1017         snd_printdd("realtek: enabling ADC switching\n");
1018         spec->dyn_adc_switch = 1;
1019         return true;
1020 }
1021
1022 /* rebuild imux for matching with the given auto-mic pins (if not yet) */
1023 static bool alc_rebuild_imux_for_auto_mic(struct hda_codec *codec)
1024 {
1025         struct alc_spec *spec = codec->spec;
1026         struct hda_input_mux *imux;
1027         static char * const texts[3] = {
1028                 "Mic", "Internal Mic", "Dock Mic"
1029         };
1030         int i;
1031
1032         if (!spec->auto_mic)
1033                 return false;
1034         imux = &spec->private_imux[0];
1035         if (spec->input_mux == imux)
1036                 return true;
1037         spec->imux_pins[0] = spec->ext_mic_pin;
1038         spec->imux_pins[1] = spec->int_mic_pin;
1039         spec->imux_pins[2] = spec->dock_mic_pin;
1040         for (i = 0; i < 3; i++) {
1041                 strcpy(imux->items[i].label, texts[i]);
1042                 if (spec->imux_pins[i]) {
1043                         hda_nid_t pin = spec->imux_pins[i];
1044                         int c;
1045                         for (c = 0; c < spec->num_adc_nids; c++) {
1046                                 hda_nid_t cap = get_capsrc(spec, c);
1047                                 int idx = get_connection_index(codec, cap, pin);
1048                                 if (idx >= 0) {
1049                                         imux->items[i].index = idx;
1050                                         break;
1051                                 }
1052                         }
1053                         imux->num_items = i + 1;
1054                 }
1055         }
1056         spec->num_mux_defs = 1;
1057         spec->input_mux = imux;
1058         return true;
1059 }
1060
1061 /* check whether all auto-mic pins are valid; setup indices if OK */
1062 static bool alc_auto_mic_check_imux(struct hda_codec *codec)
1063 {
1064         struct alc_spec *spec = codec->spec;
1065         const struct hda_input_mux *imux;
1066
1067         if (!spec->auto_mic)
1068                 return false;
1069         if (spec->auto_mic_valid_imux)
1070                 return true; /* already checked */
1071
1072         /* fill up imux indices */
1073         if (!alc_check_dyn_adc_switch(codec)) {
1074                 spec->auto_mic = 0;
1075                 return false;
1076         }
1077
1078         imux = spec->input_mux;
1079         spec->ext_mic_idx = find_idx_in_nid_list(spec->ext_mic_pin,
1080                                         spec->imux_pins, imux->num_items);
1081         spec->int_mic_idx = find_idx_in_nid_list(spec->int_mic_pin,
1082                                         spec->imux_pins, imux->num_items);
1083         spec->dock_mic_idx = find_idx_in_nid_list(spec->dock_mic_pin,
1084                                         spec->imux_pins, imux->num_items);
1085         if (spec->ext_mic_idx < 0 || spec->int_mic_idx < 0) {
1086                 spec->auto_mic = 0;
1087                 return false; /* no corresponding imux */
1088         }
1089
1090         snd_hda_jack_detect_enable(codec, spec->ext_mic_pin, ALC_MIC_EVENT);
1091         if (spec->dock_mic_pin)
1092                 snd_hda_jack_detect_enable(codec, spec->dock_mic_pin,
1093                                            ALC_MIC_EVENT);
1094
1095         spec->auto_mic_valid_imux = 1;
1096         spec->auto_mic = 1;
1097         return true;
1098 }
1099
1100 /*
1101  * Check the availability of auto-mic switch;
1102  * Set up if really supported
1103  */
1104 static void alc_init_auto_mic(struct hda_codec *codec)
1105 {
1106         struct alc_spec *spec = codec->spec;
1107         struct auto_pin_cfg *cfg = &spec->autocfg;
1108         hda_nid_t fixed, ext, dock;
1109         int i;
1110
1111         if (spec->shared_mic_hp)
1112                 return; /* no auto-mic for the shared I/O */
1113
1114         spec->ext_mic_idx = spec->int_mic_idx = spec->dock_mic_idx = -1;
1115
1116         fixed = ext = dock = 0;
1117         for (i = 0; i < cfg->num_inputs; i++) {
1118                 hda_nid_t nid = cfg->inputs[i].pin;
1119                 unsigned int defcfg;
1120                 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1121                 switch (snd_hda_get_input_pin_attr(defcfg)) {
1122                 case INPUT_PIN_ATTR_INT:
1123                         if (fixed)
1124                                 return; /* already occupied */
1125                         if (cfg->inputs[i].type != AUTO_PIN_MIC)
1126                                 return; /* invalid type */
1127                         fixed = nid;
1128                         break;
1129                 case INPUT_PIN_ATTR_UNUSED:
1130                         return; /* invalid entry */
1131                 case INPUT_PIN_ATTR_DOCK:
1132                         if (dock)
1133                                 return; /* already occupied */
1134                         if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
1135                                 return; /* invalid type */
1136                         dock = nid;
1137                         break;
1138                 default:
1139                         if (ext)
1140                                 return; /* already occupied */
1141                         if (cfg->inputs[i].type != AUTO_PIN_MIC)
1142                                 return; /* invalid type */
1143                         ext = nid;
1144                         break;
1145                 }
1146         }
1147         if (!ext && dock) {
1148                 ext = dock;
1149                 dock = 0;
1150         }
1151         if (!ext || !fixed)
1152                 return;
1153         if (!is_jack_detectable(codec, ext))
1154                 return; /* no unsol support */
1155         if (dock && !is_jack_detectable(codec, dock))
1156                 return; /* no unsol support */
1157
1158         /* check imux indices */
1159         spec->ext_mic_pin = ext;
1160         spec->int_mic_pin = fixed;
1161         spec->dock_mic_pin = dock;
1162
1163         spec->auto_mic = 1;
1164         if (!alc_auto_mic_check_imux(codec))
1165                 return;
1166
1167         snd_printdd("realtek: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
1168                     ext, fixed, dock);
1169         spec->unsol_event = alc_sku_unsol_event;
1170 }
1171
1172 /* check the availabilities of auto-mute and auto-mic switches */
1173 static void alc_auto_check_switches(struct hda_codec *codec)
1174 {
1175         alc_init_automute(codec);
1176         alc_init_auto_mic(codec);
1177 }
1178
1179 /*
1180  * Realtek SSID verification
1181  */
1182
1183 /* Could be any non-zero and even value. When used as fixup, tells
1184  * the driver to ignore any present sku defines.
1185  */
1186 #define ALC_FIXUP_SKU_IGNORE (2)
1187
1188 static int alc_auto_parse_customize_define(struct hda_codec *codec)
1189 {
1190         unsigned int ass, tmp, i;
1191         unsigned nid = 0;
1192         struct alc_spec *spec = codec->spec;
1193
1194         spec->cdefine.enable_pcbeep = 1; /* assume always enabled */
1195
1196         if (spec->cdefine.fixup) {
1197                 ass = spec->cdefine.sku_cfg;
1198                 if (ass == ALC_FIXUP_SKU_IGNORE)
1199                         return -1;
1200                 goto do_sku;
1201         }
1202
1203         ass = codec->subsystem_id & 0xffff;
1204         if (ass != codec->bus->pci->subsystem_device && (ass & 1))
1205                 goto do_sku;
1206
1207         nid = 0x1d;
1208         if (codec->vendor_id == 0x10ec0260)
1209                 nid = 0x17;
1210         ass = snd_hda_codec_get_pincfg(codec, nid);
1211
1212         if (!(ass & 1)) {
1213                 printk(KERN_INFO "hda_codec: %s: SKU not ready 0x%08x\n",
1214                        codec->chip_name, ass);
1215                 return -1;
1216         }
1217
1218         /* check sum */
1219         tmp = 0;
1220         for (i = 1; i < 16; i++) {
1221                 if ((ass >> i) & 1)
1222                         tmp++;
1223         }
1224         if (((ass >> 16) & 0xf) != tmp)
1225                 return -1;
1226
1227         spec->cdefine.port_connectivity = ass >> 30;
1228         spec->cdefine.enable_pcbeep = (ass & 0x100000) >> 20;
1229         spec->cdefine.check_sum = (ass >> 16) & 0xf;
1230         spec->cdefine.customization = ass >> 8;
1231 do_sku:
1232         spec->cdefine.sku_cfg = ass;
1233         spec->cdefine.external_amp = (ass & 0x38) >> 3;
1234         spec->cdefine.platform_type = (ass & 0x4) >> 2;
1235         spec->cdefine.swap = (ass & 0x2) >> 1;
1236         spec->cdefine.override = ass & 0x1;
1237
1238         snd_printd("SKU: Nid=0x%x sku_cfg=0x%08x\n",
1239                    nid, spec->cdefine.sku_cfg);
1240         snd_printd("SKU: port_connectivity=0x%x\n",
1241                    spec->cdefine.port_connectivity);
1242         snd_printd("SKU: enable_pcbeep=0x%x\n", spec->cdefine.enable_pcbeep);
1243         snd_printd("SKU: check_sum=0x%08x\n", spec->cdefine.check_sum);
1244         snd_printd("SKU: customization=0x%08x\n", spec->cdefine.customization);
1245         snd_printd("SKU: external_amp=0x%x\n", spec->cdefine.external_amp);
1246         snd_printd("SKU: platform_type=0x%x\n", spec->cdefine.platform_type);
1247         snd_printd("SKU: swap=0x%x\n", spec->cdefine.swap);
1248         snd_printd("SKU: override=0x%x\n", spec->cdefine.override);
1249
1250         return 0;
1251 }
1252
1253 /* return true if the given NID is found in the list */
1254 static bool found_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
1255 {
1256         return find_idx_in_nid_list(nid, list, nums) >= 0;
1257 }
1258
1259 /* check subsystem ID and set up device-specific initialization;
1260  * return 1 if initialized, 0 if invalid SSID
1261  */
1262 /* 32-bit subsystem ID for BIOS loading in HD Audio codec.
1263  *      31 ~ 16 :       Manufacture ID
1264  *      15 ~ 8  :       SKU ID
1265  *      7  ~ 0  :       Assembly ID
1266  *      port-A --> pin 39/41, port-E --> pin 14/15, port-D --> pin 35/36
1267  */
1268 static int alc_subsystem_id(struct hda_codec *codec,
1269                             hda_nid_t porta, hda_nid_t porte,
1270                             hda_nid_t portd, hda_nid_t porti)
1271 {
1272         unsigned int ass, tmp, i;
1273         unsigned nid;
1274         struct alc_spec *spec = codec->spec;
1275
1276         if (spec->cdefine.fixup) {
1277                 ass = spec->cdefine.sku_cfg;
1278                 if (ass == ALC_FIXUP_SKU_IGNORE)
1279                         return 0;
1280                 goto do_sku;
1281         }
1282
1283         ass = codec->subsystem_id & 0xffff;
1284         if ((ass != codec->bus->pci->subsystem_device) && (ass & 1))
1285                 goto do_sku;
1286
1287         /* invalid SSID, check the special NID pin defcfg instead */
1288         /*
1289          * 31~30        : port connectivity
1290          * 29~21        : reserve
1291          * 20           : PCBEEP input
1292          * 19~16        : Check sum (15:1)
1293          * 15~1         : Custom
1294          * 0            : override
1295         */
1296         nid = 0x1d;
1297         if (codec->vendor_id == 0x10ec0260)
1298                 nid = 0x17;
1299         ass = snd_hda_codec_get_pincfg(codec, nid);
1300         snd_printd("realtek: No valid SSID, "
1301                    "checking pincfg 0x%08x for NID 0x%x\n",
1302                    ass, nid);
1303         if (!(ass & 1))
1304                 return 0;
1305         if ((ass >> 30) != 1)   /* no physical connection */
1306                 return 0;
1307
1308         /* check sum */
1309         tmp = 0;
1310         for (i = 1; i < 16; i++) {
1311                 if ((ass >> i) & 1)
1312                         tmp++;
1313         }
1314         if (((ass >> 16) & 0xf) != tmp)
1315                 return 0;
1316 do_sku:
1317         snd_printd("realtek: Enabling init ASM_ID=0x%04x CODEC_ID=%08x\n",
1318                    ass & 0xffff, codec->vendor_id);
1319         /*
1320          * 0 : override
1321          * 1 :  Swap Jack
1322          * 2 : 0 --> Desktop, 1 --> Laptop
1323          * 3~5 : External Amplifier control
1324          * 7~6 : Reserved
1325         */
1326         tmp = (ass & 0x38) >> 3;        /* external Amp control */
1327         switch (tmp) {
1328         case 1:
1329                 spec->init_amp = ALC_INIT_GPIO1;
1330                 break;
1331         case 3:
1332                 spec->init_amp = ALC_INIT_GPIO2;
1333                 break;
1334         case 7:
1335                 spec->init_amp = ALC_INIT_GPIO3;
1336                 break;
1337         case 5:
1338         default:
1339                 spec->init_amp = ALC_INIT_DEFAULT;
1340                 break;
1341         }
1342
1343         /* is laptop or Desktop and enable the function "Mute internal speaker
1344          * when the external headphone out jack is plugged"
1345          */
1346         if (!(ass & 0x8000))
1347                 return 1;
1348         /*
1349          * 10~8 : Jack location
1350          * 12~11: Headphone out -> 00: PortA, 01: PortE, 02: PortD, 03: Resvered
1351          * 14~13: Resvered
1352          * 15   : 1 --> enable the function "Mute internal speaker
1353          *              when the external headphone out jack is plugged"
1354          */
1355         if (!spec->autocfg.hp_pins[0] &&
1356             !(spec->autocfg.line_out_pins[0] &&
1357               spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)) {
1358                 hda_nid_t nid;
1359                 tmp = (ass >> 11) & 0x3;        /* HP to chassis */
1360                 if (tmp == 0)
1361                         nid = porta;
1362                 else if (tmp == 1)
1363                         nid = porte;
1364                 else if (tmp == 2)
1365                         nid = portd;
1366                 else if (tmp == 3)
1367                         nid = porti;
1368                 else
1369                         return 1;
1370                 if (found_in_nid_list(nid, spec->autocfg.line_out_pins,
1371                                       spec->autocfg.line_outs))
1372                         return 1;
1373                 spec->autocfg.hp_pins[0] = nid;
1374         }
1375         return 1;
1376 }
1377
1378 /* Check the validity of ALC subsystem-id
1379  * ports contains an array of 4 pin NIDs for port-A, E, D and I */
1380 static void alc_ssid_check(struct hda_codec *codec, const hda_nid_t *ports)
1381 {
1382         if (!alc_subsystem_id(codec, ports[0], ports[1], ports[2], ports[3])) {
1383                 struct alc_spec *spec = codec->spec;
1384                 snd_printd("realtek: "
1385                            "Enable default setup for auto mode as fallback\n");
1386                 spec->init_amp = ALC_INIT_DEFAULT;
1387         }
1388 }
1389
1390 /*
1391  * Fix-up pin default configurations and add default verbs
1392  */
1393
1394 struct alc_pincfg {
1395         hda_nid_t nid;
1396         u32 val;
1397 };
1398
1399 struct alc_model_fixup {
1400         const int id;
1401         const char *name;
1402 };
1403
1404 struct alc_fixup {
1405         int type;
1406         bool chained;
1407         int chain_id;
1408         union {
1409                 unsigned int sku;
1410                 const struct alc_pincfg *pins;
1411                 const struct hda_verb *verbs;
1412                 void (*func)(struct hda_codec *codec,
1413                              const struct alc_fixup *fix,
1414                              int action);
1415         } v;
1416 };
1417
1418 enum {
1419         ALC_FIXUP_INVALID,
1420         ALC_FIXUP_SKU,
1421         ALC_FIXUP_PINS,
1422         ALC_FIXUP_VERBS,
1423         ALC_FIXUP_FUNC,
1424 };
1425
1426 enum {
1427         ALC_FIXUP_ACT_PRE_PROBE,
1428         ALC_FIXUP_ACT_PROBE,
1429         ALC_FIXUP_ACT_INIT,
1430 };
1431
1432 static void alc_apply_fixup(struct hda_codec *codec, int action)
1433 {
1434         struct alc_spec *spec = codec->spec;
1435         int id = spec->fixup_id;
1436 #ifdef CONFIG_SND_DEBUG_VERBOSE
1437         const char *modelname = spec->fixup_name;
1438 #endif
1439         int depth = 0;
1440
1441         if (!spec->fixup_list)
1442                 return;
1443
1444         while (id >= 0) {
1445                 const struct alc_fixup *fix = spec->fixup_list + id;
1446                 const struct alc_pincfg *cfg;
1447
1448                 switch (fix->type) {
1449                 case ALC_FIXUP_SKU:
1450                         if (action != ALC_FIXUP_ACT_PRE_PROBE || !fix->v.sku)
1451                                 break;
1452                         snd_printdd(KERN_INFO "hda_codec: %s: "
1453                                     "Apply sku override for %s\n",
1454                                     codec->chip_name, modelname);
1455                         spec->cdefine.sku_cfg = fix->v.sku;
1456                         spec->cdefine.fixup = 1;
1457                         break;
1458                 case ALC_FIXUP_PINS:
1459                         cfg = fix->v.pins;
1460                         if (action != ALC_FIXUP_ACT_PRE_PROBE || !cfg)
1461                                 break;
1462                         snd_printdd(KERN_INFO "hda_codec: %s: "
1463                                     "Apply pincfg for %s\n",
1464                                     codec->chip_name, modelname);
1465                         for (; cfg->nid; cfg++)
1466                                 snd_hda_codec_set_pincfg(codec, cfg->nid,
1467                                                          cfg->val);
1468                         break;
1469                 case ALC_FIXUP_VERBS:
1470                         if (action != ALC_FIXUP_ACT_PROBE || !fix->v.verbs)
1471                                 break;
1472                         snd_printdd(KERN_INFO "hda_codec: %s: "
1473                                     "Apply fix-verbs for %s\n",
1474                                     codec->chip_name, modelname);
1475                         add_verb(codec->spec, fix->v.verbs);
1476                         break;
1477                 case ALC_FIXUP_FUNC:
1478                         if (!fix->v.func)
1479                                 break;
1480                         snd_printdd(KERN_INFO "hda_codec: %s: "
1481                                     "Apply fix-func for %s\n",
1482                                     codec->chip_name, modelname);
1483                         fix->v.func(codec, fix, action);
1484                         break;
1485                 default:
1486                         snd_printk(KERN_ERR "hda_codec: %s: "
1487                                    "Invalid fixup type %d\n",
1488                                    codec->chip_name, fix->type);
1489                         break;
1490                 }
1491                 if (!fix->chained)
1492                         break;
1493                 if (++depth > 10)
1494                         break;
1495                 id = fix->chain_id;
1496         }
1497 }
1498
1499 static void alc_pick_fixup(struct hda_codec *codec,
1500                            const struct alc_model_fixup *models,
1501                            const struct snd_pci_quirk *quirk,
1502                            const struct alc_fixup *fixlist)
1503 {
1504         struct alc_spec *spec = codec->spec;
1505         const struct snd_pci_quirk *q;
1506         int id = -1;
1507         const char *name = NULL;
1508
1509         if (codec->modelname && models) {
1510                 while (models->name) {
1511                         if (!strcmp(codec->modelname, models->name)) {
1512                                 id = models->id;
1513                                 name = models->name;
1514                                 break;
1515                         }
1516                         models++;
1517                 }
1518         }
1519         if (id < 0) {
1520                 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
1521                 if (q) {
1522                         id = q->value;
1523 #ifdef CONFIG_SND_DEBUG_VERBOSE
1524                         name = q->name;
1525 #endif
1526                 }
1527         }
1528         if (id < 0) {
1529                 for (q = quirk; q->subvendor; q++) {
1530                         unsigned int vendorid =
1531                                 q->subdevice | (q->subvendor << 16);
1532                         if (vendorid == codec->subsystem_id) {
1533                                 id = q->value;
1534 #ifdef CONFIG_SND_DEBUG_VERBOSE
1535                                 name = q->name;
1536 #endif
1537                                 break;
1538                         }
1539                 }
1540         }
1541
1542         spec->fixup_id = id;
1543         if (id >= 0) {
1544                 spec->fixup_list = fixlist;
1545                 spec->fixup_name = name;
1546         }
1547 }
1548
1549 /*
1550  * COEF access helper functions
1551  */
1552 static int alc_read_coef_idx(struct hda_codec *codec,
1553                         unsigned int coef_idx)
1554 {
1555         unsigned int val;
1556         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX,
1557                                 coef_idx);
1558         val = snd_hda_codec_read(codec, 0x20, 0,
1559                                 AC_VERB_GET_PROC_COEF, 0);
1560         return val;
1561 }
1562
1563 static void alc_write_coef_idx(struct hda_codec *codec, unsigned int coef_idx,
1564                                                         unsigned int coef_val)
1565 {
1566         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX,
1567                             coef_idx);
1568         snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_PROC_COEF,
1569                             coef_val);
1570 }
1571
1572 /* a special bypass for COEF 0; read the cached value at the second time */
1573 static unsigned int alc_get_coef0(struct hda_codec *codec)
1574 {
1575         struct alc_spec *spec = codec->spec;
1576         if (!spec->coef0)
1577                 spec->coef0 = alc_read_coef_idx(codec, 0);
1578         return spec->coef0;
1579 }
1580
1581 /*
1582  * Digital I/O handling
1583  */
1584
1585 /* set right pin controls for digital I/O */
1586 static void alc_auto_init_digital(struct hda_codec *codec)
1587 {
1588         struct alc_spec *spec = codec->spec;
1589         int i;
1590         hda_nid_t pin, dac;
1591
1592         for (i = 0; i < spec->autocfg.dig_outs; i++) {
1593                 pin = spec->autocfg.dig_out_pins[i];
1594                 if (!pin)
1595                         continue;
1596                 snd_hda_codec_write(codec, pin, 0,
1597                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1598                 if (!i)
1599                         dac = spec->multiout.dig_out_nid;
1600                 else
1601                         dac = spec->slave_dig_outs[i - 1];
1602                 if (!dac || !(get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
1603                         continue;
1604                 snd_hda_codec_write(codec, dac, 0,
1605                                     AC_VERB_SET_AMP_GAIN_MUTE,
1606                                     AMP_OUT_UNMUTE);
1607         }
1608         pin = spec->autocfg.dig_in_pin;
1609         if (pin)
1610                 snd_hda_codec_write(codec, pin, 0,
1611                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1612                                     PIN_IN);
1613 }
1614
1615 /* parse digital I/Os and set up NIDs in BIOS auto-parse mode */
1616 static void alc_auto_parse_digital(struct hda_codec *codec)
1617 {
1618         struct alc_spec *spec = codec->spec;
1619         int i, err, nums;
1620         hda_nid_t dig_nid;
1621
1622         /* support multiple SPDIFs; the secondary is set up as a slave */
1623         nums = 0;
1624         for (i = 0; i < spec->autocfg.dig_outs; i++) {
1625                 hda_nid_t conn[4];
1626                 err = snd_hda_get_connections(codec,
1627                                               spec->autocfg.dig_out_pins[i],
1628                                               conn, ARRAY_SIZE(conn));
1629                 if (err <= 0)
1630                         continue;
1631                 dig_nid = conn[0]; /* assume the first element is audio-out */
1632                 if (!nums) {
1633                         spec->multiout.dig_out_nid = dig_nid;
1634                         spec->dig_out_type = spec->autocfg.dig_out_type[0];
1635                 } else {
1636                         spec->multiout.slave_dig_outs = spec->slave_dig_outs;
1637                         if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
1638                                 break;
1639                         spec->slave_dig_outs[nums - 1] = dig_nid;
1640                 }
1641                 nums++;
1642         }
1643
1644         if (spec->autocfg.dig_in_pin) {
1645                 dig_nid = codec->start_nid;
1646                 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
1647                         unsigned int wcaps = get_wcaps(codec, dig_nid);
1648                         if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
1649                                 continue;
1650                         if (!(wcaps & AC_WCAP_DIGITAL))
1651                                 continue;
1652                         if (!(wcaps & AC_WCAP_CONN_LIST))
1653                                 continue;
1654                         err = get_connection_index(codec, dig_nid,
1655                                                    spec->autocfg.dig_in_pin);
1656                         if (err >= 0) {
1657                                 spec->dig_in_nid = dig_nid;
1658                                 break;
1659                         }
1660                 }
1661         }
1662 }
1663
1664 /*
1665  * capture mixer elements
1666  */
1667 static int alc_cap_vol_info(struct snd_kcontrol *kcontrol,
1668                             struct snd_ctl_elem_info *uinfo)
1669 {
1670         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1671         struct alc_spec *spec = codec->spec;
1672         unsigned long val;
1673         int err;
1674
1675         mutex_lock(&codec->control_mutex);
1676         if (spec->vol_in_capsrc)
1677                 val = HDA_COMPOSE_AMP_VAL(spec->capsrc_nids[0], 3, 0, HDA_OUTPUT);
1678         else
1679                 val = HDA_COMPOSE_AMP_VAL(spec->adc_nids[0], 3, 0, HDA_INPUT);
1680         kcontrol->private_value = val;
1681         err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
1682         mutex_unlock(&codec->control_mutex);
1683         return err;
1684 }
1685
1686 static int alc_cap_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1687                            unsigned int size, unsigned int __user *tlv)
1688 {
1689         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1690         struct alc_spec *spec = codec->spec;
1691         unsigned long val;
1692         int err;
1693
1694         mutex_lock(&codec->control_mutex);
1695         if (spec->vol_in_capsrc)
1696                 val = HDA_COMPOSE_AMP_VAL(spec->capsrc_nids[0], 3, 0, HDA_OUTPUT);
1697         else
1698                 val = HDA_COMPOSE_AMP_VAL(spec->adc_nids[0], 3, 0, HDA_INPUT);
1699         kcontrol->private_value = val;
1700         err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
1701         mutex_unlock(&codec->control_mutex);
1702         return err;
1703 }
1704
1705 typedef int (*getput_call_t)(struct snd_kcontrol *kcontrol,
1706                              struct snd_ctl_elem_value *ucontrol);
1707
1708 static int alc_cap_getput_caller(struct snd_kcontrol *kcontrol,
1709                                  struct snd_ctl_elem_value *ucontrol,
1710                                  getput_call_t func, bool check_adc_switch)
1711 {
1712         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1713         struct alc_spec *spec = codec->spec;
1714         int i, err = 0;
1715
1716         mutex_lock(&codec->control_mutex);
1717         if (check_adc_switch && spec->dyn_adc_switch) {
1718                 for (i = 0; i < spec->num_adc_nids; i++) {
1719                         kcontrol->private_value =
1720                                 HDA_COMPOSE_AMP_VAL(spec->adc_nids[i],
1721                                                     3, 0, HDA_INPUT);
1722                         err = func(kcontrol, ucontrol);
1723                         if (err < 0)
1724                                 goto error;
1725                 }
1726         } else {
1727                 i = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1728                 if (spec->vol_in_capsrc)
1729                         kcontrol->private_value =
1730                                 HDA_COMPOSE_AMP_VAL(spec->capsrc_nids[i],
1731                                                     3, 0, HDA_OUTPUT);
1732                 else
1733                         kcontrol->private_value =
1734                                 HDA_COMPOSE_AMP_VAL(spec->adc_nids[i],
1735                                                     3, 0, HDA_INPUT);
1736                 err = func(kcontrol, ucontrol);
1737         }
1738  error:
1739         mutex_unlock(&codec->control_mutex);
1740         return err;
1741 }
1742
1743 static int alc_cap_vol_get(struct snd_kcontrol *kcontrol,
1744                            struct snd_ctl_elem_value *ucontrol)
1745 {
1746         return alc_cap_getput_caller(kcontrol, ucontrol,
1747                                      snd_hda_mixer_amp_volume_get, false);
1748 }
1749
1750 static int alc_cap_vol_put(struct snd_kcontrol *kcontrol,
1751                            struct snd_ctl_elem_value *ucontrol)
1752 {
1753         return alc_cap_getput_caller(kcontrol, ucontrol,
1754                                      snd_hda_mixer_amp_volume_put, true);
1755 }
1756
1757 /* capture mixer elements */
1758 #define alc_cap_sw_info         snd_ctl_boolean_stereo_info
1759
1760 static int alc_cap_sw_get(struct snd_kcontrol *kcontrol,
1761                           struct snd_ctl_elem_value *ucontrol)
1762 {
1763         return alc_cap_getput_caller(kcontrol, ucontrol,
1764                                      snd_hda_mixer_amp_switch_get, false);
1765 }
1766
1767 static int alc_cap_sw_put(struct snd_kcontrol *kcontrol,
1768                           struct snd_ctl_elem_value *ucontrol)
1769 {
1770         return alc_cap_getput_caller(kcontrol, ucontrol,
1771                                      snd_hda_mixer_amp_switch_put, true);
1772 }
1773
1774 #define _DEFINE_CAPMIX(num) \
1775         { \
1776                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1777                 .name = "Capture Switch", \
1778                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
1779                 .count = num, \
1780                 .info = alc_cap_sw_info, \
1781                 .get = alc_cap_sw_get, \
1782                 .put = alc_cap_sw_put, \
1783         }, \
1784         { \
1785                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1786                 .name = "Capture Volume", \
1787                 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE | \
1788                            SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
1789                            SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK), \
1790                 .count = num, \
1791                 .info = alc_cap_vol_info, \
1792                 .get = alc_cap_vol_get, \
1793                 .put = alc_cap_vol_put, \
1794                 .tlv = { .c = alc_cap_vol_tlv }, \
1795         }
1796
1797 #define _DEFINE_CAPSRC(num) \
1798         { \
1799                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1800                 /* .name = "Capture Source", */ \
1801                 .name = "Input Source", \
1802                 .count = num, \
1803                 .info = alc_mux_enum_info, \
1804                 .get = alc_mux_enum_get, \
1805                 .put = alc_mux_enum_put, \
1806         }
1807
1808 #define DEFINE_CAPMIX(num) \
1809 static const struct snd_kcontrol_new alc_capture_mixer ## num[] = { \
1810         _DEFINE_CAPMIX(num),                                  \
1811         _DEFINE_CAPSRC(num),                                  \
1812         { } /* end */                                         \
1813 }
1814
1815 #define DEFINE_CAPMIX_NOSRC(num) \
1816 static const struct snd_kcontrol_new alc_capture_mixer_nosrc ## num[] = { \
1817         _DEFINE_CAPMIX(num),                                        \
1818         { } /* end */                                               \
1819 }
1820
1821 /* up to three ADCs */
1822 DEFINE_CAPMIX(1);
1823 DEFINE_CAPMIX(2);
1824 DEFINE_CAPMIX(3);
1825 DEFINE_CAPMIX_NOSRC(1);
1826 DEFINE_CAPMIX_NOSRC(2);
1827 DEFINE_CAPMIX_NOSRC(3);
1828
1829 /*
1830  * virtual master controls
1831  */
1832
1833 /*
1834  * slave controls for virtual master
1835  */
1836 static const char * const alc_slave_vols[] = {
1837         "Front Playback Volume",
1838         "Surround Playback Volume",
1839         "Center Playback Volume",
1840         "LFE Playback Volume",
1841         "Side Playback Volume",
1842         "Headphone Playback Volume",
1843         "Speaker Playback Volume",
1844         "Mono Playback Volume",
1845         "Line-Out Playback Volume",
1846         "PCM Playback Volume",
1847         NULL,
1848 };
1849
1850 static const char * const alc_slave_sws[] = {
1851         "Front Playback Switch",
1852         "Surround Playback Switch",
1853         "Center Playback Switch",
1854         "LFE Playback Switch",
1855         "Side Playback Switch",
1856         "Headphone Playback Switch",
1857         "Speaker Playback Switch",
1858         "Mono Playback Switch",
1859         "IEC958 Playback Switch",
1860         "Line-Out Playback Switch",
1861         "PCM Playback Switch",
1862         NULL,
1863 };
1864
1865 /*
1866  * build control elements
1867  */
1868
1869 #define NID_MAPPING             (-1)
1870
1871 #define SUBDEV_SPEAKER_         (0 << 6)
1872 #define SUBDEV_HP_              (1 << 6)
1873 #define SUBDEV_LINE_            (2 << 6)
1874 #define SUBDEV_SPEAKER(x)       (SUBDEV_SPEAKER_ | ((x) & 0x3f))
1875 #define SUBDEV_HP(x)            (SUBDEV_HP_ | ((x) & 0x3f))
1876 #define SUBDEV_LINE(x)          (SUBDEV_LINE_ | ((x) & 0x3f))
1877
1878 static void alc_free_kctls(struct hda_codec *codec);
1879
1880 #ifdef CONFIG_SND_HDA_INPUT_BEEP
1881 /* additional beep mixers; the actual parameters are overwritten at build */
1882 static const struct snd_kcontrol_new alc_beep_mixer[] = {
1883         HDA_CODEC_VOLUME("Beep Playback Volume", 0, 0, HDA_INPUT),
1884         HDA_CODEC_MUTE_BEEP("Beep Playback Switch", 0, 0, HDA_INPUT),
1885         { } /* end */
1886 };
1887 #endif
1888
1889 static int __alc_build_controls(struct hda_codec *codec)
1890 {
1891         struct alc_spec *spec = codec->spec;
1892         struct snd_kcontrol *kctl = NULL;
1893         const struct snd_kcontrol_new *knew;
1894         int i, j, err;
1895         unsigned int u;
1896         hda_nid_t nid;
1897
1898         for (i = 0; i < spec->num_mixers; i++) {
1899                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1900                 if (err < 0)
1901                         return err;
1902         }
1903         if (spec->cap_mixer) {
1904                 err = snd_hda_add_new_ctls(codec, spec->cap_mixer);
1905                 if (err < 0)
1906                         return err;
1907         }
1908         if (spec->multiout.dig_out_nid) {
1909                 err = snd_hda_create_spdif_out_ctls(codec,
1910                                                     spec->multiout.dig_out_nid,
1911                                                     spec->multiout.dig_out_nid);
1912                 if (err < 0)
1913                         return err;
1914                 if (!spec->no_analog) {
1915                         err = snd_hda_create_spdif_share_sw(codec,
1916                                                             &spec->multiout);
1917                         if (err < 0)
1918                                 return err;
1919                         spec->multiout.share_spdif = 1;
1920                 }
1921         }
1922         if (spec->dig_in_nid) {
1923                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1924                 if (err < 0)
1925                         return err;
1926         }
1927
1928 #ifdef CONFIG_SND_HDA_INPUT_BEEP
1929         /* create beep controls if needed */
1930         if (spec->beep_amp) {
1931                 const struct snd_kcontrol_new *knew;
1932                 for (knew = alc_beep_mixer; knew->name; knew++) {
1933                         struct snd_kcontrol *kctl;
1934                         kctl = snd_ctl_new1(knew, codec);
1935                         if (!kctl)
1936                                 return -ENOMEM;
1937                         kctl->private_value = spec->beep_amp;
1938                         err = snd_hda_ctl_add(codec, 0, kctl);
1939                         if (err < 0)
1940                                 return err;
1941                 }
1942         }
1943 #endif
1944
1945         /* if we have no master control, let's create it */
1946         if (!spec->no_analog &&
1947             !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1948                 unsigned int vmaster_tlv[4];
1949                 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1950                                         HDA_OUTPUT, vmaster_tlv);
1951                 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
1952                                           vmaster_tlv, alc_slave_vols);
1953                 if (err < 0)
1954                         return err;
1955         }
1956         if (!spec->no_analog &&
1957             !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1958                 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
1959                                           NULL, alc_slave_sws);
1960                 if (err < 0)
1961                         return err;
1962         }
1963
1964         /* assign Capture Source enums to NID */
1965         if (spec->capsrc_nids || spec->adc_nids) {
1966                 kctl = snd_hda_find_mixer_ctl(codec, "Capture Source");
1967                 if (!kctl)
1968                         kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1969                 for (i = 0; kctl && i < kctl->count; i++) {
1970                         err = snd_hda_add_nid(codec, kctl, i,
1971                                               get_capsrc(spec, i));
1972                         if (err < 0)
1973                                 return err;
1974                 }
1975         }
1976         if (spec->cap_mixer && spec->adc_nids) {
1977                 const char *kname = kctl ? kctl->id.name : NULL;
1978                 for (knew = spec->cap_mixer; knew->name; knew++) {
1979                         if (kname && strcmp(knew->name, kname) == 0)
1980                                 continue;
1981                         kctl = snd_hda_find_mixer_ctl(codec, knew->name);
1982                         for (i = 0; kctl && i < kctl->count; i++) {
1983                                 err = snd_hda_add_nid(codec, kctl, i,
1984                                                       spec->adc_nids[i]);
1985                                 if (err < 0)
1986                                         return err;
1987                         }
1988                 }
1989         }
1990
1991         /* other nid->control mapping */
1992         for (i = 0; i < spec->num_mixers; i++) {
1993                 for (knew = spec->mixers[i]; knew->name; knew++) {
1994                         if (knew->iface != NID_MAPPING)
1995                                 continue;
1996                         kctl = snd_hda_find_mixer_ctl(codec, knew->name);
1997                         if (kctl == NULL)
1998                                 continue;
1999                         u = knew->subdevice;
2000                         for (j = 0; j < 4; j++, u >>= 8) {
2001                                 nid = u & 0x3f;
2002                                 if (nid == 0)
2003                                         continue;
2004                                 switch (u & 0xc0) {
2005                                 case SUBDEV_SPEAKER_:
2006                                         nid = spec->autocfg.speaker_pins[nid];
2007                                         break;
2008                                 case SUBDEV_LINE_:
2009                                         nid = spec->autocfg.line_out_pins[nid];
2010                                         break;
2011                                 case SUBDEV_HP_:
2012                                         nid = spec->autocfg.hp_pins[nid];
2013                                         break;
2014                                 default:
2015                                         continue;
2016                                 }
2017                                 err = snd_hda_add_nid(codec, kctl, 0, nid);
2018                                 if (err < 0)
2019                                         return err;
2020                         }
2021                         u = knew->private_value;
2022                         for (j = 0; j < 4; j++, u >>= 8) {
2023                                 nid = u & 0xff;
2024                                 if (nid == 0)
2025                                         continue;
2026                                 err = snd_hda_add_nid(codec, kctl, 0, nid);
2027                                 if (err < 0)
2028                                         return err;
2029                         }
2030                 }
2031         }
2032
2033         alc_free_kctls(codec); /* no longer needed */
2034
2035         return 0;
2036 }
2037
2038 static int alc_build_controls(struct hda_codec *codec)
2039 {
2040         struct alc_spec *spec = codec->spec;
2041         int err = __alc_build_controls(codec);
2042         if (err < 0)
2043                 return err;
2044         return snd_hda_jack_add_kctls(codec, &spec->autocfg);
2045 }
2046
2047
2048 /*
2049  * Common callbacks
2050  */
2051
2052 static void alc_init_special_input_src(struct hda_codec *codec);
2053
2054 static int alc_init(struct hda_codec *codec)
2055 {
2056         struct alc_spec *spec = codec->spec;
2057         unsigned int i;
2058
2059         alc_fix_pll(codec);
2060         alc_auto_init_amp(codec, spec->init_amp);
2061
2062         for (i = 0; i < spec->num_init_verbs; i++)
2063                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
2064         alc_init_special_input_src(codec);
2065
2066         if (spec->init_hook)
2067                 spec->init_hook(codec);
2068
2069         alc_apply_fixup(codec, ALC_FIXUP_ACT_INIT);
2070
2071         snd_hda_jack_report_sync(codec);
2072
2073         hda_call_check_power_status(codec, 0x01);
2074         return 0;
2075 }
2076
2077 static void alc_unsol_event(struct hda_codec *codec, unsigned int res)
2078 {
2079         struct alc_spec *spec = codec->spec;
2080
2081         if (spec->unsol_event)
2082                 spec->unsol_event(codec, res);
2083 }
2084
2085 #ifdef CONFIG_SND_HDA_POWER_SAVE
2086 static int alc_check_power_status(struct hda_codec *codec, hda_nid_t nid)
2087 {
2088         struct alc_spec *spec = codec->spec;
2089         return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
2090 }
2091 #endif
2092
2093 /*
2094  * Analog playback callbacks
2095  */
2096 static int alc_playback_pcm_open(struct hda_pcm_stream *hinfo,
2097                                     struct hda_codec *codec,
2098                                     struct snd_pcm_substream *substream)
2099 {
2100         struct alc_spec *spec = codec->spec;
2101         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
2102                                              hinfo);
2103 }
2104
2105 static int alc_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2106                                        struct hda_codec *codec,
2107                                        unsigned int stream_tag,
2108                                        unsigned int format,
2109                                        struct snd_pcm_substream *substream)
2110 {
2111         struct alc_spec *spec = codec->spec;
2112         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
2113                                                 stream_tag, format, substream);
2114 }
2115
2116 static int alc_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2117                                        struct hda_codec *codec,
2118                                        struct snd_pcm_substream *substream)
2119 {
2120         struct alc_spec *spec = codec->spec;
2121         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
2122 }
2123
2124 /*
2125  * Digital out
2126  */
2127 static int alc_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
2128                                         struct hda_codec *codec,
2129                                         struct snd_pcm_substream *substream)
2130 {
2131         struct alc_spec *spec = codec->spec;
2132         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2133 }
2134
2135 static int alc_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2136                                            struct hda_codec *codec,
2137                                            unsigned int stream_tag,
2138                                            unsigned int format,
2139                                            struct snd_pcm_substream *substream)
2140 {
2141         struct alc_spec *spec = codec->spec;
2142         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2143                                              stream_tag, format, substream);
2144 }
2145
2146 static int alc_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2147                                            struct hda_codec *codec,
2148                                            struct snd_pcm_substream *substream)
2149 {
2150         struct alc_spec *spec = codec->spec;
2151         return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
2152 }
2153
2154 static int alc_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
2155                                          struct hda_codec *codec,
2156                                          struct snd_pcm_substream *substream)
2157 {
2158         struct alc_spec *spec = codec->spec;
2159         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2160 }
2161
2162 /*
2163  * Analog capture
2164  */
2165 static int alc_alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2166                                       struct hda_codec *codec,
2167                                       unsigned int stream_tag,
2168                                       unsigned int format,
2169                                       struct snd_pcm_substream *substream)
2170 {
2171         struct alc_spec *spec = codec->spec;
2172
2173         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
2174                                    stream_tag, 0, format);
2175         return 0;
2176 }
2177
2178 static int alc_alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2179                                       struct hda_codec *codec,
2180                                       struct snd_pcm_substream *substream)
2181 {
2182         struct alc_spec *spec = codec->spec;
2183
2184         snd_hda_codec_cleanup_stream(codec,
2185                                      spec->adc_nids[substream->number + 1]);
2186         return 0;
2187 }
2188
2189 /* analog capture with dynamic dual-adc changes */
2190 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2191                                        struct hda_codec *codec,
2192                                        unsigned int stream_tag,
2193                                        unsigned int format,
2194                                        struct snd_pcm_substream *substream)
2195 {
2196         struct alc_spec *spec = codec->spec;
2197         spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
2198         spec->cur_adc_stream_tag = stream_tag;
2199         spec->cur_adc_format = format;
2200         snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
2201         return 0;
2202 }
2203
2204 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2205                                        struct hda_codec *codec,
2206                                        struct snd_pcm_substream *substream)
2207 {
2208         struct alc_spec *spec = codec->spec;
2209         snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
2210         spec->cur_adc = 0;
2211         return 0;
2212 }
2213
2214 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
2215         .substreams = 1,
2216         .channels_min = 2,
2217         .channels_max = 2,
2218         .nid = 0, /* fill later */
2219         .ops = {
2220                 .prepare = dyn_adc_capture_pcm_prepare,
2221                 .cleanup = dyn_adc_capture_pcm_cleanup
2222         },
2223 };
2224
2225 /*
2226  */
2227 static const struct hda_pcm_stream alc_pcm_analog_playback = {
2228         .substreams = 1,
2229         .channels_min = 2,
2230         .channels_max = 8,
2231         /* NID is set in alc_build_pcms */
2232         .ops = {
2233                 .open = alc_playback_pcm_open,
2234                 .prepare = alc_playback_pcm_prepare,
2235                 .cleanup = alc_playback_pcm_cleanup
2236         },
2237 };
2238
2239 static const struct hda_pcm_stream alc_pcm_analog_capture = {
2240         .substreams = 1,
2241         .channels_min = 2,
2242         .channels_max = 2,
2243         /* NID is set in alc_build_pcms */
2244 };
2245
2246 static const struct hda_pcm_stream alc_pcm_analog_alt_playback = {
2247         .substreams = 1,
2248         .channels_min = 2,
2249         .channels_max = 2,
2250         /* NID is set in alc_build_pcms */
2251 };
2252
2253 static const struct hda_pcm_stream alc_pcm_analog_alt_capture = {
2254         .substreams = 2, /* can be overridden */
2255         .channels_min = 2,
2256         .channels_max = 2,
2257         /* NID is set in alc_build_pcms */
2258         .ops = {
2259                 .prepare = alc_alt_capture_pcm_prepare,
2260                 .cleanup = alc_alt_capture_pcm_cleanup
2261         },
2262 };
2263
2264 static const struct hda_pcm_stream alc_pcm_digital_playback = {
2265         .substreams = 1,
2266         .channels_min = 2,
2267         .channels_max = 2,
2268         /* NID is set in alc_build_pcms */
2269         .ops = {
2270                 .open = alc_dig_playback_pcm_open,
2271                 .close = alc_dig_playback_pcm_close,
2272                 .prepare = alc_dig_playback_pcm_prepare,
2273                 .cleanup = alc_dig_playback_pcm_cleanup
2274         },
2275 };
2276
2277 static const struct hda_pcm_stream alc_pcm_digital_capture = {
2278         .substreams = 1,
2279         .channels_min = 2,
2280         .channels_max = 2,
2281         /* NID is set in alc_build_pcms */
2282 };
2283
2284 /* Used by alc_build_pcms to flag that a PCM has no playback stream */
2285 static const struct hda_pcm_stream alc_pcm_null_stream = {
2286         .substreams = 0,
2287         .channels_min = 0,
2288         .channels_max = 0,
2289 };
2290
2291 static int alc_build_pcms(struct hda_codec *codec)
2292 {
2293         struct alc_spec *spec = codec->spec;
2294         struct hda_pcm *info = spec->pcm_rec;
2295         const struct hda_pcm_stream *p;
2296         bool have_multi_adcs;
2297         int i;
2298
2299         codec->num_pcms = 1;
2300         codec->pcm_info = info;
2301
2302         if (spec->no_analog)
2303                 goto skip_analog;
2304
2305         snprintf(spec->stream_name_analog, sizeof(spec->stream_name_analog),
2306                  "%s Analog", codec->chip_name);
2307         info->name = spec->stream_name_analog;
2308
2309         if (spec->multiout.dac_nids > 0) {
2310                 p = spec->stream_analog_playback;
2311                 if (!p)
2312                         p = &alc_pcm_analog_playback;
2313                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2314                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
2315         }
2316         if (spec->adc_nids) {
2317                 p = spec->stream_analog_capture;
2318                 if (!p) {
2319                         if (spec->dyn_adc_switch)
2320                                 p = &dyn_adc_pcm_analog_capture;
2321                         else
2322                                 p = &alc_pcm_analog_capture;
2323                 }
2324                 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2325                 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
2326         }
2327
2328         if (spec->channel_mode) {
2329                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = 0;
2330                 for (i = 0; i < spec->num_channel_mode; i++) {
2331                         if (spec->channel_mode[i].channels > info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max) {
2332                                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->channel_mode[i].channels;
2333                         }
2334                 }
2335         }
2336
2337  skip_analog:
2338         /* SPDIF for stream index #1 */
2339         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
2340                 snprintf(spec->stream_name_digital,
2341                          sizeof(spec->stream_name_digital),
2342                          "%s Digital", codec->chip_name);
2343                 codec->num_pcms = 2;
2344                 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
2345                 info = spec->pcm_rec + 1;
2346                 info->name = spec->stream_name_digital;
2347                 if (spec->dig_out_type)
2348                         info->pcm_type = spec->dig_out_type;
2349                 else
2350                         info->pcm_type = HDA_PCM_TYPE_SPDIF;
2351                 if (spec->multiout.dig_out_nid) {
2352                         p = spec->stream_digital_playback;
2353                         if (!p)
2354                                 p = &alc_pcm_digital_playback;
2355                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2356                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
2357                 }
2358                 if (spec->dig_in_nid) {
2359                         p = spec->stream_digital_capture;
2360                         if (!p)
2361                                 p = &alc_pcm_digital_capture;
2362                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2363                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
2364                 }
2365                 /* FIXME: do we need this for all Realtek codec models? */
2366                 codec->spdif_status_reset = 1;
2367         }
2368
2369         if (spec->no_analog)
2370                 return 0;
2371
2372         /* If the use of more than one ADC is requested for the current
2373          * model, configure a second analog capture-only PCM.
2374          */
2375         have_multi_adcs = (spec->num_adc_nids > 1) &&
2376                 !spec->dyn_adc_switch && !spec->auto_mic &&
2377                 (!spec->input_mux || spec->input_mux->num_items > 1);
2378         /* Additional Analaog capture for index #2 */
2379         if (spec->alt_dac_nid || have_multi_adcs) {
2380                 codec->num_pcms = 3;
2381                 info = spec->pcm_rec + 2;
2382                 info->name = spec->stream_name_analog;
2383                 if (spec->alt_dac_nid) {
2384                         p = spec->stream_analog_alt_playback;
2385                         if (!p)
2386                                 p = &alc_pcm_analog_alt_playback;
2387                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2388                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
2389                                 spec->alt_dac_nid;
2390                 } else {
2391                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
2392                                 alc_pcm_null_stream;
2393                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
2394                 }
2395                 if (have_multi_adcs) {
2396                         p = spec->stream_analog_alt_capture;
2397                         if (!p)
2398                                 p = &alc_pcm_analog_alt_capture;
2399                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2400                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
2401                                 spec->adc_nids[1];
2402                         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
2403                                 spec->num_adc_nids - 1;
2404                 } else {
2405                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
2406                                 alc_pcm_null_stream;
2407                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
2408                 }
2409         }
2410
2411         return 0;
2412 }
2413
2414 static inline void alc_shutup(struct hda_codec *codec)
2415 {
2416         struct alc_spec *spec = codec->spec;
2417
2418         if (spec && spec->shutup)
2419                 spec->shutup(codec);
2420         snd_hda_shutup_pins(codec);
2421 }
2422
2423 static void alc_free_kctls(struct hda_codec *codec)
2424 {
2425         struct alc_spec *spec = codec->spec;
2426
2427         if (spec->kctls.list) {
2428                 struct snd_kcontrol_new *kctl = spec->kctls.list;
2429                 int i;
2430                 for (i = 0; i < spec->kctls.used; i++)
2431                         kfree(kctl[i].name);
2432         }
2433         snd_array_free(&spec->kctls);
2434 }
2435
2436 static void alc_free_bind_ctls(struct hda_codec *codec)
2437 {
2438         struct alc_spec *spec = codec->spec;
2439         if (spec->bind_ctls.list) {
2440                 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
2441                 int i;
2442                 for (i = 0; i < spec->bind_ctls.used; i++)
2443                         kfree(ctl[i]);
2444         }
2445         snd_array_free(&spec->bind_ctls);
2446 }
2447
2448 static void alc_free(struct hda_codec *codec)
2449 {
2450         struct alc_spec *spec = codec->spec;
2451
2452         if (!spec)
2453                 return;
2454
2455         alc_shutup(codec);
2456         alc_free_kctls(codec);
2457         alc_free_bind_ctls(codec);
2458         kfree(spec);
2459         snd_hda_detach_beep_device(codec);
2460 }
2461
2462 #ifdef CONFIG_SND_HDA_POWER_SAVE
2463 static void alc_power_eapd(struct hda_codec *codec)
2464 {
2465         alc_auto_setup_eapd(codec, false);
2466 }
2467
2468 static int alc_suspend(struct hda_codec *codec, pm_message_t state)
2469 {
2470         struct alc_spec *spec = codec->spec;
2471         alc_shutup(codec);
2472         if (spec && spec->power_hook)
2473                 spec->power_hook(codec);
2474         return 0;
2475 }
2476 #endif
2477
2478 #ifdef CONFIG_PM
2479 static int alc_resume(struct hda_codec *codec)
2480 {
2481         msleep(150); /* to avoid pop noise */
2482         codec->patch_ops.init(codec);
2483         snd_hda_codec_resume_amp(codec);
2484         snd_hda_codec_resume_cache(codec);
2485         hda_call_check_power_status(codec, 0x01);
2486         return 0;
2487 }
2488 #endif
2489
2490 /*
2491  */
2492 static const struct hda_codec_ops alc_patch_ops = {
2493         .build_controls = alc_build_controls,
2494         .build_pcms = alc_build_pcms,
2495         .init = alc_init,
2496         .free = alc_free,
2497         .unsol_event = alc_unsol_event,
2498 #ifdef CONFIG_PM
2499         .resume = alc_resume,
2500 #endif
2501 #ifdef CONFIG_SND_HDA_POWER_SAVE
2502         .suspend = alc_suspend,
2503         .check_power_status = alc_check_power_status,
2504 #endif
2505         .reboot_notify = alc_shutup,
2506 };
2507
2508 /* replace the codec chip_name with the given string */
2509 static int alc_codec_rename(struct hda_codec *codec, const char *name)
2510 {
2511         kfree(codec->chip_name);
2512         codec->chip_name = kstrdup(name, GFP_KERNEL);
2513         if (!codec->chip_name) {
2514                 alc_free(codec);
2515                 return -ENOMEM;
2516         }
2517         return 0;
2518 }
2519
2520 /*
2521  * Rename codecs appropriately from COEF value
2522  */
2523 struct alc_codec_rename_table {
2524         unsigned int vendor_id;
2525         unsigned short coef_mask;
2526         unsigned short coef_bits;
2527         const char *name;
2528 };
2529
2530 static struct alc_codec_rename_table rename_tbl[] = {
2531         { 0x10ec0269, 0xfff0, 0x3010, "ALC277" },
2532         { 0x10ec0269, 0xf0f0, 0x2010, "ALC259" },
2533         { 0x10ec0269, 0xf0f0, 0x3010, "ALC258" },
2534         { 0x10ec0269, 0x00f0, 0x0010, "ALC269VB" },
2535         { 0x10ec0269, 0xffff, 0xa023, "ALC259" },
2536         { 0x10ec0269, 0xffff, 0x6023, "ALC281X" },
2537         { 0x10ec0269, 0x00f0, 0x0020, "ALC269VC" },
2538         { 0x10ec0887, 0x00f0, 0x0030, "ALC887-VD" },
2539         { 0x10ec0888, 0x00f0, 0x0030, "ALC888-VD" },
2540         { 0x10ec0888, 0xf0f0, 0x3020, "ALC886" },
2541         { 0x10ec0899, 0x2000, 0x2000, "ALC899" },
2542         { 0x10ec0892, 0xffff, 0x8020, "ALC661" },
2543         { 0x10ec0892, 0xffff, 0x8011, "ALC661" },
2544         { 0x10ec0892, 0xffff, 0x4011, "ALC656" },
2545         { } /* terminator */
2546 };
2547
2548 static int alc_codec_rename_from_preset(struct hda_codec *codec)
2549 {
2550         const struct alc_codec_rename_table *p;
2551
2552         for (p = rename_tbl; p->vendor_id; p++) {
2553                 if (p->vendor_id != codec->vendor_id)
2554                         continue;
2555                 if ((alc_get_coef0(codec) & p->coef_mask) == p->coef_bits)
2556                         return alc_codec_rename(codec, p->name);
2557         }
2558         return 0;
2559 }
2560
2561 /*
2562  * Automatic parse of I/O pins from the BIOS configuration
2563  */
2564
2565 enum {
2566         ALC_CTL_WIDGET_VOL,
2567         ALC_CTL_WIDGET_MUTE,
2568         ALC_CTL_BIND_MUTE,
2569         ALC_CTL_BIND_VOL,
2570         ALC_CTL_BIND_SW,
2571 };
2572 static const struct snd_kcontrol_new alc_control_templates[] = {
2573         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
2574         HDA_CODEC_MUTE(NULL, 0, 0, 0),
2575         HDA_BIND_MUTE(NULL, 0, 0, 0),
2576         HDA_BIND_VOL(NULL, 0),
2577         HDA_BIND_SW(NULL, 0),
2578 };
2579
2580 /* add dynamic controls */
2581 static int add_control(struct alc_spec *spec, int type, const char *name,
2582                        int cidx, unsigned long val)
2583 {
2584         struct snd_kcontrol_new *knew;
2585
2586         knew = alc_kcontrol_new(spec);
2587         if (!knew)
2588                 return -ENOMEM;
2589         *knew = alc_control_templates[type];
2590         knew->name = kstrdup(name, GFP_KERNEL);
2591         if (!knew->name)
2592                 return -ENOMEM;
2593         knew->index = cidx;
2594         if (get_amp_nid_(val))
2595                 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2596         knew->private_value = val;
2597         return 0;
2598 }
2599
2600 static int add_control_with_pfx(struct alc_spec *spec, int type,
2601                                 const char *pfx, const char *dir,
2602                                 const char *sfx, int cidx, unsigned long val)
2603 {
2604         char name[32];
2605         snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
2606         return add_control(spec, type, name, cidx, val);
2607 }
2608
2609 #define add_pb_vol_ctrl(spec, type, pfx, val)                   \
2610         add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
2611 #define add_pb_sw_ctrl(spec, type, pfx, val)                    \
2612         add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
2613 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val)                   \
2614         add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
2615 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val)                    \
2616         add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
2617
2618 static const char * const channel_name[4] = {
2619         "Front", "Surround", "CLFE", "Side"
2620 };
2621
2622 static const char *alc_get_line_out_pfx(struct alc_spec *spec, int ch,
2623                                         bool can_be_master, int *index)
2624 {
2625         struct auto_pin_cfg *cfg = &spec->autocfg;
2626
2627         *index = 0;
2628         if (cfg->line_outs == 1 && !spec->multi_ios &&
2629             !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
2630                 return "Master";
2631
2632         switch (cfg->line_out_type) {
2633         case AUTO_PIN_SPEAKER_OUT:
2634                 if (cfg->line_outs == 1)
2635                         return "Speaker";
2636                 if (cfg->line_outs == 2)
2637                         return ch ? "Bass Speaker" : "Speaker";
2638                 break;
2639         case AUTO_PIN_HP_OUT:
2640                 /* for multi-io case, only the primary out */
2641                 if (ch && spec->multi_ios)
2642                         break;
2643                 *index = ch;
2644                 return "Headphone";
2645         default:
2646                 if (cfg->line_outs == 1 && !spec->multi_ios)
2647                         return "PCM";
2648                 break;
2649         }
2650         if (snd_BUG_ON(ch >= ARRAY_SIZE(channel_name)))
2651                 return "PCM";
2652
2653         return channel_name[ch];
2654 }
2655
2656 /* create input playback/capture controls for the given pin */
2657 static int new_analog_input(struct alc_spec *spec, hda_nid_t pin,
2658                             const char *ctlname, int ctlidx,
2659                             int idx, hda_nid_t mix_nid)
2660 {
2661         int err;
2662
2663         err = __add_pb_vol_ctrl(spec, ALC_CTL_WIDGET_VOL, ctlname, ctlidx,
2664                           HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
2665         if (err < 0)
2666                 return err;
2667         err = __add_pb_sw_ctrl(spec, ALC_CTL_WIDGET_MUTE, ctlname, ctlidx,
2668                           HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
2669         if (err < 0)
2670                 return err;
2671         return 0;
2672 }
2673
2674 static int alc_is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2675 {
2676         unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2677         return (pincap & AC_PINCAP_IN) != 0;
2678 }
2679
2680 /* Parse the codec tree and retrieve ADCs and corresponding capsrc MUXs */
2681 static int alc_auto_fill_adc_caps(struct hda_codec *codec)
2682 {
2683         struct alc_spec *spec = codec->spec;
2684         hda_nid_t nid;
2685         hda_nid_t *adc_nids = spec->private_adc_nids;
2686         hda_nid_t *cap_nids = spec->private_capsrc_nids;
2687         int max_nums = ARRAY_SIZE(spec->private_adc_nids);
2688         int i, nums = 0;
2689
2690         if (spec->shared_mic_hp)
2691                 max_nums = 1; /* no multi streams with the shared HP/mic */
2692
2693         nid = codec->start_nid;
2694         for (i = 0; i < codec->num_nodes; i++, nid++) {
2695                 hda_nid_t src;
2696                 const hda_nid_t *list;
2697                 unsigned int caps = get_wcaps(codec, nid);
2698                 int type = get_wcaps_type(caps);
2699
2700                 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2701                         continue;
2702                 adc_nids[nums] = nid;
2703                 cap_nids[nums] = nid;
2704                 src = nid;
2705                 for (;;) {
2706                         int n;
2707                         type = get_wcaps_type(get_wcaps(codec, src));
2708                         if (type == AC_WID_PIN)
2709                                 break;
2710                         if (type == AC_WID_AUD_SEL) {
2711                                 cap_nids[nums] = src;
2712                                 break;
2713                         }
2714                         n = snd_hda_get_conn_list(codec, src, &list);
2715                         if (n > 1) {
2716                                 cap_nids[nums] = src;
2717                                 break;
2718                         } else if (n != 1)
2719                                 break;
2720                         src = *list;
2721                 }
2722                 if (++nums >= max_nums)
2723                         break;
2724         }
2725         spec->adc_nids = spec->private_adc_nids;
2726         spec->capsrc_nids = spec->private_capsrc_nids;
2727         spec->num_adc_nids = nums;
2728         return nums;
2729 }
2730
2731 /* create playback/capture controls for input pins */
2732 static int alc_auto_create_input_ctls(struct hda_codec *codec)
2733 {
2734         struct alc_spec *spec = codec->spec;
2735         const struct auto_pin_cfg *cfg = &spec->autocfg;
2736         hda_nid_t mixer = spec->mixer_nid;
2737         struct hda_input_mux *imux = &spec->private_imux[0];
2738         int num_adcs;
2739         int i, c, err, idx, type_idx = 0;
2740         const char *prev_label = NULL;
2741
2742         num_adcs = alc_auto_fill_adc_caps(codec);
2743         if (num_adcs < 0)
2744                 return 0;
2745
2746         for (i = 0; i < cfg->num_inputs; i++) {
2747                 hda_nid_t pin;
2748                 const char *label;
2749
2750                 pin = cfg->inputs[i].pin;
2751                 if (!alc_is_input_pin(codec, pin))
2752                         continue;
2753
2754                 label = hda_get_autocfg_input_label(codec, cfg, i);
2755                 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2756                         label = "Headphone Mic";
2757                 if (prev_label && !strcmp(label, prev_label))
2758                         type_idx++;
2759                 else
2760                         type_idx = 0;
2761                 prev_label = label;
2762
2763                 if (mixer) {
2764                         idx = get_connection_index(codec, mixer, pin);
2765                         if (idx >= 0) {
2766                                 err = new_analog_input(spec, pin,
2767                                                        label, type_idx,
2768                                                        idx, mixer);
2769                                 if (err < 0)
2770                                         return err;
2771                         }
2772                 }
2773
2774                 for (c = 0; c < num_adcs; c++) {
2775                         hda_nid_t cap = get_capsrc(spec, c);
2776                         idx = get_connection_index(codec, cap, pin);
2777                         if (idx >= 0) {
2778                                 spec->imux_pins[imux->num_items] = pin;
2779                                 snd_hda_add_imux_item(imux, label, idx, NULL);
2780                                 break;
2781                         }
2782                 }
2783         }
2784
2785         spec->num_mux_defs = 1;
2786         spec->input_mux = imux;
2787
2788         return 0;
2789 }
2790
2791 /* create a shared input with the headphone out */
2792 static int alc_auto_create_shared_input(struct hda_codec *codec)
2793 {
2794         struct alc_spec *spec = codec->spec;
2795         struct auto_pin_cfg *cfg = &spec->autocfg;
2796         unsigned int defcfg;
2797         hda_nid_t nid;
2798
2799         /* only one internal input pin? */
2800         if (cfg->num_inputs != 1)
2801                 return 0;
2802         defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2803         if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2804                 return 0;
2805
2806         if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2807                 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2808         else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2809                 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2810         else
2811                 return 0; /* both not available */
2812
2813         if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2814                 return 0; /* no input */
2815
2816         cfg->inputs[1].pin = nid;
2817         cfg->inputs[1].type = AUTO_PIN_MIC;
2818         cfg->num_inputs = 2;
2819         spec->shared_mic_hp = 1;
2820         snd_printdd("realtek: Enable shared I/O jack on NID 0x%x\n", nid);
2821         return 0;
2822 }
2823
2824 static void alc_set_pin_output(struct hda_codec *codec, hda_nid_t nid,
2825                                unsigned int pin_type)
2826 {
2827         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2828                             pin_type);
2829         /* unmute pin */
2830         if (nid_has_mute(codec, nid, HDA_OUTPUT))
2831                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2832                             AMP_OUT_UNMUTE);
2833 }
2834
2835 static int get_pin_type(int line_out_type)
2836 {
2837         if (line_out_type == AUTO_PIN_HP_OUT)
2838                 return PIN_HP;
2839         else
2840                 return PIN_OUT;
2841 }
2842
2843 static void alc_auto_init_analog_input(struct hda_codec *codec)
2844 {
2845         struct alc_spec *spec = codec->spec;
2846         struct auto_pin_cfg *cfg = &spec->autocfg;
2847         int i;
2848
2849         for (i = 0; i < cfg->num_inputs; i++) {
2850                 hda_nid_t nid = cfg->inputs[i].pin;
2851                 if (alc_is_input_pin(codec, nid)) {
2852                         alc_set_input_pin(codec, nid, cfg->inputs[i].type);
2853                         if (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP)
2854                                 snd_hda_codec_write(codec, nid, 0,
2855                                                     AC_VERB_SET_AMP_GAIN_MUTE,
2856                                                     AMP_OUT_MUTE);
2857                 }
2858         }
2859
2860         /* mute all loopback inputs */
2861         if (spec->mixer_nid) {
2862                 int nums = snd_hda_get_conn_list(codec, spec->mixer_nid, NULL);
2863                 for (i = 0; i < nums; i++)
2864                         snd_hda_codec_write(codec, spec->mixer_nid, 0,
2865                                             AC_VERB_SET_AMP_GAIN_MUTE,
2866                                             AMP_IN_MUTE(i));
2867         }
2868 }
2869
2870 /* convert from MIX nid to DAC */
2871 static hda_nid_t alc_auto_mix_to_dac(struct hda_codec *codec, hda_nid_t nid)
2872 {
2873         hda_nid_t list[5];
2874         int i, num;
2875
2876         if (get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_AUD_OUT)
2877                 return nid;
2878         num = snd_hda_get_connections(codec, nid, list, ARRAY_SIZE(list));
2879         for (i = 0; i < num; i++) {
2880                 if (get_wcaps_type(get_wcaps(codec, list[i])) == AC_WID_AUD_OUT)
2881                         return list[i];
2882         }
2883         return 0;
2884 }
2885
2886 /* go down to the selector widget before the mixer */
2887 static hda_nid_t alc_go_down_to_selector(struct hda_codec *codec, hda_nid_t pin)
2888 {
2889         hda_nid_t srcs[5];
2890         int num = snd_hda_get_connections(codec, pin, srcs,
2891                                           ARRAY_SIZE(srcs));
2892         if (num != 1 ||
2893             get_wcaps_type(get_wcaps(codec, srcs[0])) != AC_WID_AUD_SEL)
2894                 return pin;
2895         return srcs[0];
2896 }
2897
2898 /* get MIX nid connected to the given pin targeted to DAC */
2899 static hda_nid_t alc_auto_dac_to_mix(struct hda_codec *codec, hda_nid_t pin,
2900                                    hda_nid_t dac)
2901 {
2902         hda_nid_t mix[5];
2903         int i, num;
2904
2905         pin = alc_go_down_to_selector(codec, pin);
2906         num = snd_hda_get_connections(codec, pin, mix, ARRAY_SIZE(mix));
2907         for (i = 0; i < num; i++) {
2908                 if (alc_auto_mix_to_dac(codec, mix[i]) == dac)
2909                         return mix[i];
2910         }
2911         return 0;
2912 }
2913
2914 /* select the connection from pin to DAC if needed */
2915 static int alc_auto_select_dac(struct hda_codec *codec, hda_nid_t pin,
2916                                hda_nid_t dac)
2917 {
2918         hda_nid_t mix[5];
2919         int i, num;
2920
2921         pin = alc_go_down_to_selector(codec, pin);
2922         num = snd_hda_get_connections(codec, pin, mix, ARRAY_SIZE(mix));
2923         if (num < 2)
2924                 return 0;
2925         for (i = 0; i < num; i++) {
2926                 if (alc_auto_mix_to_dac(codec, mix[i]) == dac) {
2927                         snd_hda_codec_update_cache(codec, pin, 0,
2928                                                    AC_VERB_SET_CONNECT_SEL, i);
2929                         return 0;
2930                 }
2931         }
2932         return 0;
2933 }
2934
2935 /* look for an empty DAC slot */
2936 static hda_nid_t alc_auto_look_for_dac(struct hda_codec *codec, hda_nid_t pin)
2937 {
2938         struct alc_spec *spec = codec->spec;
2939         hda_nid_t srcs[5];
2940         int i, num;
2941
2942         pin = alc_go_down_to_selector(codec, pin);
2943         num = snd_hda_get_connections(codec, pin, srcs, ARRAY_SIZE(srcs));
2944         for (i = 0; i < num; i++) {
2945                 hda_nid_t nid = alc_auto_mix_to_dac(codec, srcs[i]);
2946                 if (!nid)
2947                         continue;
2948                 if (found_in_nid_list(nid, spec->multiout.dac_nids,
2949                                       ARRAY_SIZE(spec->private_dac_nids)))
2950                         continue;
2951                 if (found_in_nid_list(nid, spec->multiout.hp_out_nid,
2952                                       ARRAY_SIZE(spec->multiout.hp_out_nid)))
2953                     continue;
2954                 if (found_in_nid_list(nid, spec->multiout.extra_out_nid,
2955                                       ARRAY_SIZE(spec->multiout.extra_out_nid)))
2956                     continue;
2957                 return nid;
2958         }
2959         return 0;
2960 }
2961
2962 /* check whether the DAC is reachable from the pin */
2963 static bool alc_auto_is_dac_reachable(struct hda_codec *codec,
2964                                       hda_nid_t pin, hda_nid_t dac)
2965 {
2966         hda_nid_t srcs[5];
2967         int i, num;
2968
2969         pin = alc_go_down_to_selector(codec, pin);
2970         num = snd_hda_get_connections(codec, pin, srcs, ARRAY_SIZE(srcs));
2971         for (i = 0; i < num; i++) {
2972                 hda_nid_t nid = alc_auto_mix_to_dac(codec, srcs[i]);
2973                 if (nid == dac)
2974                         return true;
2975         }
2976         return false;
2977 }
2978
2979 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
2980 {
2981         hda_nid_t sel = alc_go_down_to_selector(codec, pin);
2982         if (snd_hda_get_conn_list(codec, sel, NULL) == 1)
2983                 return alc_auto_look_for_dac(codec, pin);
2984         return 0;
2985 }
2986
2987 /* return 0 if no possible DAC is found, 1 if one or more found */
2988 static int alc_auto_fill_extra_dacs(struct hda_codec *codec, int num_outs,
2989                                     const hda_nid_t *pins, hda_nid_t *dacs)
2990 {
2991         int i;
2992
2993         if (num_outs && !dacs[0]) {
2994                 dacs[0] = alc_auto_look_for_dac(codec, pins[0]);
2995                 if (!dacs[0])
2996                         return 0;
2997         }
2998
2999         for (i = 1; i < num_outs; i++)
3000                 dacs[i] = get_dac_if_single(codec, pins[i]);
3001         for (i = 1; i < num_outs; i++) {
3002                 if (!dacs[i])
3003                         dacs[i] = alc_auto_look_for_dac(codec, pins[i]);
3004         }
3005         return 1;
3006 }
3007
3008 static int alc_auto_fill_multi_ios(struct hda_codec *codec,
3009                                    unsigned int location, int offset);
3010 static hda_nid_t alc_look_for_out_vol_nid(struct hda_codec *codec,
3011                                           hda_nid_t pin, hda_nid_t dac);
3012
3013 /* fill in the dac_nids table from the parsed pin configuration */
3014 static int alc_auto_fill_dac_nids(struct hda_codec *codec)
3015 {
3016         struct alc_spec *spec = codec->spec;
3017         struct auto_pin_cfg *cfg = &spec->autocfg;
3018         unsigned int location, defcfg;
3019         int num_pins;
3020         bool redone = false;
3021         int i;
3022
3023  again:
3024         /* set num_dacs once to full for alc_auto_look_for_dac() */
3025         spec->multiout.num_dacs = cfg->line_outs;
3026         spec->multiout.hp_out_nid[0] = 0;
3027         spec->multiout.extra_out_nid[0] = 0;
3028         memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
3029         spec->multiout.dac_nids = spec->private_dac_nids;
3030         spec->multi_ios = 0;
3031
3032         /* fill hard-wired DACs first */
3033         if (!redone) {
3034                 for (i = 0; i < cfg->line_outs; i++)
3035                         spec->private_dac_nids[i] =
3036                                 get_dac_if_single(codec, cfg->line_out_pins[i]);
3037                 if (cfg->hp_outs)
3038                         spec->multiout.hp_out_nid[0] =
3039                                 get_dac_if_single(codec, cfg->hp_pins[0]);
3040                 if (cfg->speaker_outs)
3041                         spec->multiout.extra_out_nid[0] =
3042                                 get_dac_if_single(codec, cfg->speaker_pins[0]);
3043         }
3044
3045         for (i = 0; i < cfg->line_outs; i++) {
3046                 hda_nid_t pin = cfg->line_out_pins[i];
3047                 if (spec->private_dac_nids[i])
3048                         continue;
3049                 spec->private_dac_nids[i] = alc_auto_look_for_dac(codec, pin);
3050                 if (!spec->private_dac_nids[i] && !redone) {
3051                         /* if we can't find primary DACs, re-probe without
3052                          * checking the hard-wired DACs
3053                          */
3054                         redone = true;
3055                         goto again;
3056                 }
3057         }
3058
3059         /* re-count num_dacs and squash invalid entries */
3060         spec->multiout.num_dacs = 0;
3061         for (i = 0; i < cfg->line_outs; i++) {
3062                 if (spec->private_dac_nids[i])
3063                         spec->multiout.num_dacs++;
3064                 else {
3065                         memmove(spec->private_dac_nids + i,
3066                                 spec->private_dac_nids + i + 1,
3067                                 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
3068                         spec->private_dac_nids[cfg->line_outs - 1] = 0;
3069                 }
3070         }
3071
3072         if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3073                 /* try to fill multi-io first */
3074                 defcfg = snd_hda_codec_get_pincfg(codec, cfg->line_out_pins[0]);
3075                 location = get_defcfg_location(defcfg);
3076
3077                 num_pins = alc_auto_fill_multi_ios(codec, location, 0);
3078                 if (num_pins > 0) {
3079                         spec->multi_ios = num_pins;
3080                         spec->ext_channel_count = 2;
3081                         spec->multiout.num_dacs = num_pins + 1;
3082                 }
3083         }
3084
3085         if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3086                 alc_auto_fill_extra_dacs(codec, cfg->hp_outs, cfg->hp_pins,
3087                                  spec->multiout.hp_out_nid);
3088         if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3089                 int err = alc_auto_fill_extra_dacs(codec, cfg->speaker_outs,
3090                                         cfg->speaker_pins,
3091                                         spec->multiout.extra_out_nid);
3092                 /* if no speaker volume is assigned, try again as the primary
3093                  * output
3094                  */
3095                 if (!err && cfg->speaker_outs > 0 &&
3096                     cfg->line_out_type == AUTO_PIN_HP_OUT) {
3097                         cfg->hp_outs = cfg->line_outs;
3098                         memcpy(cfg->hp_pins, cfg->line_out_pins,
3099                                sizeof(cfg->hp_pins));
3100                         cfg->line_outs = cfg->speaker_outs;
3101                         memcpy(cfg->line_out_pins, cfg->speaker_pins,
3102                                sizeof(cfg->speaker_pins));
3103                         cfg->speaker_outs = 0;
3104                         memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3105                         cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3106                         redone = false;
3107                         goto again;
3108                 }
3109         }
3110
3111         if (!spec->multi_ios &&
3112             cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3113             cfg->hp_outs) {
3114                 /* try multi-ios with HP + inputs */
3115                 defcfg = snd_hda_codec_get_pincfg(codec, cfg->hp_pins[0]);
3116                 location = get_defcfg_location(defcfg);
3117
3118                 num_pins = alc_auto_fill_multi_ios(codec, location, 1);
3119                 if (num_pins > 0) {
3120                         spec->multi_ios = num_pins;
3121                         spec->ext_channel_count = 2;
3122                         spec->multiout.num_dacs = num_pins + 1;
3123                 }
3124         }
3125
3126         if (cfg->line_out_pins[0])
3127                 spec->vmaster_nid =
3128                         alc_look_for_out_vol_nid(codec, cfg->line_out_pins[0],
3129                                                  spec->multiout.dac_nids[0]);
3130         return 0;
3131 }
3132
3133 static inline unsigned int get_ctl_pos(unsigned int data)
3134 {
3135         hda_nid_t nid = get_amp_nid_(data);
3136         unsigned int dir = get_amp_direction_(data);
3137         return (nid << 1) | dir;
3138 }
3139
3140 #define is_ctl_used(bits, data) \
3141         test_bit(get_ctl_pos(data), bits)
3142 #define mark_ctl_usage(bits, data) \
3143         set_bit(get_ctl_pos(data), bits)
3144
3145 static int alc_auto_add_vol_ctl(struct hda_codec *codec,
3146                               const char *pfx, int cidx,
3147                               hda_nid_t nid, unsigned int chs)
3148 {
3149         struct alc_spec *spec = codec->spec;
3150         unsigned int val;
3151         if (!nid)
3152                 return 0;
3153         val = HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT);
3154         if (is_ctl_used(spec->vol_ctls, val) && chs != 2) /* exclude LFE */
3155                 return 0;
3156         mark_ctl_usage(spec->vol_ctls, val);
3157         return __add_pb_vol_ctrl(codec->spec, ALC_CTL_WIDGET_VOL, pfx, cidx,
3158                                  val);
3159 }
3160
3161 static int alc_auto_add_stereo_vol(struct hda_codec *codec,
3162                                    const char *pfx, int cidx,
3163                                    hda_nid_t nid)
3164 {
3165         int chs = 1;
3166         if (get_wcaps(codec, nid) & AC_WCAP_STEREO)
3167                 chs = 3;
3168         return alc_auto_add_vol_ctl(codec, pfx, cidx, nid, chs);