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