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