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