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