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