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