2 * Universal Interface for Intel High Definition Audio Codec
4 * HD audio interface patch for Realtek ALC codecs
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>
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.
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.
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
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"
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
44 #define GPIO_MASK 0x03
46 /* extra amp-initialization sequence types */
55 struct alc_customize_define {
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;
64 unsigned int override:1;
65 unsigned int fixup:1; /* Means that this sku is set by driver, not read from hw */
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 */
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 */
82 #define MAX_VOL_NIDS 0x40
85 /* codec parameterization */
86 const struct snd_kcontrol_new *mixers[5]; /* mixer arrays */
87 unsigned int num_mixers;
88 const struct snd_kcontrol_new *cap_mixer; /* capture mixer */
89 unsigned int beep_amp; /* beep amp value, set via set_beep_amp() */
91 const struct hda_verb *init_verbs[10]; /* initialization verbs
95 unsigned int num_init_verbs;
97 char stream_name_analog[32]; /* analog PCM stream */
98 const struct hda_pcm_stream *stream_analog_playback;
99 const struct hda_pcm_stream *stream_analog_capture;
100 const struct hda_pcm_stream *stream_analog_alt_playback;
101 const struct hda_pcm_stream *stream_analog_alt_capture;
103 char stream_name_digital[32]; /* digital PCM stream */
104 const struct hda_pcm_stream *stream_digital_playback;
105 const struct hda_pcm_stream *stream_digital_capture;
108 struct hda_multi_out multiout; /* playback set-up
109 * max_channels, dacs must be set
110 * dig_out_nid and hp_nid are optional
112 hda_nid_t alt_dac_nid;
113 hda_nid_t slave_dig_outs[3]; /* optional - for auto-parsing */
117 unsigned int num_adc_nids;
118 const hda_nid_t *adc_nids;
119 const hda_nid_t *capsrc_nids;
120 hda_nid_t dig_in_nid; /* digital-in NID; optional */
121 hda_nid_t mixer_nid; /* analog-mixer NID */
122 DECLARE_BITMAP(vol_ctls, MAX_VOL_NIDS << 1);
123 DECLARE_BITMAP(sw_ctls, MAX_VOL_NIDS << 1);
125 /* capture setup for dynamic dual-adc switch */
127 unsigned int cur_adc_stream_tag;
128 unsigned int cur_adc_format;
131 unsigned int num_mux_defs;
132 const struct hda_input_mux *input_mux;
133 unsigned int cur_mux[3];
134 hda_nid_t ext_mic_pin;
135 hda_nid_t dock_mic_pin;
136 hda_nid_t int_mic_pin;
139 const struct hda_channel_mode *channel_mode;
140 int num_channel_mode;
142 int const_channel_count;
143 int ext_channel_count;
145 /* PCM information */
146 struct hda_pcm pcm_rec[3]; /* used in alc_build_pcms() */
148 /* dynamic controls, init_verbs and input_mux */
149 struct auto_pin_cfg autocfg;
150 struct alc_customize_define cdefine;
151 struct snd_array kctls;
152 struct hda_input_mux private_imux[3];
153 hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
154 hda_nid_t private_adc_nids[AUTO_CFG_MAX_OUTS];
155 hda_nid_t private_capsrc_nids[AUTO_CFG_MAX_OUTS];
156 hda_nid_t imux_pins[HDA_MAX_NUM_INPUTS];
157 unsigned int dyn_adc_idx[HDA_MAX_NUM_INPUTS];
158 int int_mic_idx, ext_mic_idx, dock_mic_idx; /* for auto-mic */
161 void (*init_hook)(struct hda_codec *codec);
162 void (*unsol_event)(struct hda_codec *codec, unsigned int res);
163 #ifdef CONFIG_SND_HDA_POWER_SAVE
164 void (*power_hook)(struct hda_codec *codec);
166 void (*shutup)(struct hda_codec *codec);
167 void (*automute_hook)(struct hda_codec *codec);
169 /* for pin sensing */
170 unsigned int hp_jack_present:1;
171 unsigned int line_jack_present:1;
172 unsigned int master_mute:1;
173 unsigned int auto_mic:1;
174 unsigned int auto_mic_valid_imux:1; /* valid imux for auto-mic */
175 unsigned int automute_speaker:1; /* automute speaker outputs */
176 unsigned int automute_lo:1; /* automute LO outputs */
177 unsigned int detect_hp:1; /* Headphone detection enabled */
178 unsigned int detect_lo:1; /* Line-out detection enabled */
179 unsigned int automute_speaker_possible:1; /* there are speakers and either LO or HP */
180 unsigned int automute_lo_possible:1; /* there are line outs and HP */
181 unsigned int keep_vref_in_automute:1; /* Don't clear VREF in automute */
184 unsigned int no_analog :1; /* digital I/O only */
185 unsigned int dyn_adc_switch:1; /* switch ADCs (for ALC275) */
186 unsigned int single_input_src:1;
187 unsigned int vol_in_capsrc:1; /* use capsrc volume (ADC has no vol) */
188 unsigned int parse_flags; /* passed to snd_hda_parse_pin_defcfg() */
190 /* auto-mute control */
192 hda_nid_t automute_mixer_nid[AUTO_CFG_MAX_OUTS];
195 int codec_variant; /* flag for other variants */
197 /* for virtual master */
198 hda_nid_t vmaster_nid;
199 #ifdef CONFIG_SND_HDA_POWER_SAVE
200 struct hda_loopback_check loopback;
205 unsigned int pll_coef_idx, pll_coef_bit;
210 const struct alc_fixup *fixup_list;
211 const char *fixup_name;
215 struct alc_multi_io multi_io[4];
218 struct snd_array bind_ctls;
221 #define ALC_MODEL_AUTO 0 /* common for all chips */
223 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
224 int dir, unsigned int bits)
228 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
229 if (query_amp_caps(codec, nid, dir) & bits)
234 #define nid_has_mute(codec, nid, dir) \
235 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
236 #define nid_has_volume(codec, nid, dir) \
237 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
242 static int alc_mux_enum_info(struct snd_kcontrol *kcontrol,
243 struct snd_ctl_elem_info *uinfo)
245 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
246 struct alc_spec *spec = codec->spec;
247 unsigned int mux_idx = snd_ctl_get_ioffidx(kcontrol, &uinfo->id);
248 if (mux_idx >= spec->num_mux_defs)
250 if (!spec->input_mux[mux_idx].num_items && mux_idx > 0)
252 return snd_hda_input_mux_info(&spec->input_mux[mux_idx], uinfo);
255 static int alc_mux_enum_get(struct snd_kcontrol *kcontrol,
256 struct snd_ctl_elem_value *ucontrol)
258 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
259 struct alc_spec *spec = codec->spec;
260 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
262 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
266 static bool alc_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
268 struct alc_spec *spec = codec->spec;
269 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
271 if (spec->cur_adc && spec->cur_adc != new_adc) {
272 /* stream is running, let's swap the current ADC */
273 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
274 spec->cur_adc = new_adc;
275 snd_hda_codec_setup_stream(codec, new_adc,
276 spec->cur_adc_stream_tag, 0,
277 spec->cur_adc_format);
283 static inline hda_nid_t get_capsrc(struct alc_spec *spec, int idx)
285 return spec->capsrc_nids ?
286 spec->capsrc_nids[idx] : spec->adc_nids[idx];
289 /* select the given imux item; either unmute exclusively or select the route */
290 static int alc_mux_select(struct hda_codec *codec, unsigned int adc_idx,
291 unsigned int idx, bool force)
293 struct alc_spec *spec = codec->spec;
294 const struct hda_input_mux *imux;
295 unsigned int mux_idx;
296 int i, type, num_conns;
299 mux_idx = adc_idx >= spec->num_mux_defs ? 0 : adc_idx;
300 imux = &spec->input_mux[mux_idx];
301 if (!imux->num_items && mux_idx > 0)
302 imux = &spec->input_mux[0];
303 if (!imux->num_items)
306 if (idx >= imux->num_items)
307 idx = imux->num_items - 1;
308 if (spec->cur_mux[adc_idx] == idx && !force)
310 spec->cur_mux[adc_idx] = idx;
312 if (spec->dyn_adc_switch) {
313 alc_dyn_adc_pcm_resetup(codec, idx);
314 adc_idx = spec->dyn_adc_idx[idx];
317 nid = get_capsrc(spec, adc_idx);
320 num_conns = snd_hda_get_conn_list(codec, nid, NULL);
324 type = get_wcaps_type(get_wcaps(codec, nid));
325 if (type == AC_WID_AUD_MIX) {
326 /* Matrix-mixer style (e.g. ALC882) */
327 int active = imux->items[idx].index;
328 for (i = 0; i < num_conns; i++) {
329 unsigned int v = (i == active) ? 0 : HDA_AMP_MUTE;
330 snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, i,
334 /* MUX style (e.g. ALC880) */
335 snd_hda_codec_write_cache(codec, nid, 0,
336 AC_VERB_SET_CONNECT_SEL,
337 imux->items[idx].index);
342 static int alc_mux_enum_put(struct snd_kcontrol *kcontrol,
343 struct snd_ctl_elem_value *ucontrol)
345 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
346 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
347 return alc_mux_select(codec, adc_idx,
348 ucontrol->value.enumerated.item[0], false);
352 * set up the input pin config (depending on the given auto-pin type)
354 static void alc_set_input_pin(struct hda_codec *codec, hda_nid_t nid,
357 unsigned int val = PIN_IN;
359 if (auto_pin_type == AUTO_PIN_MIC) {
362 oldval = snd_hda_codec_read(codec, nid, 0,
363 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
364 pincap = snd_hda_query_pin_caps(codec, nid);
365 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
366 /* if the default pin setup is vref50, we give it priority */
367 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
369 else if (pincap & AC_PINCAP_VREF_50)
371 else if (pincap & AC_PINCAP_VREF_100)
373 else if (pincap & AC_PINCAP_VREF_GRD)
376 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, val);
380 * Append the given mixer and verb elements for the later use
381 * The mixer array is referred in build_controls(), and init_verbs are
384 static void add_mixer(struct alc_spec *spec, const struct snd_kcontrol_new *mix)
386 if (snd_BUG_ON(spec->num_mixers >= ARRAY_SIZE(spec->mixers)))
388 spec->mixers[spec->num_mixers++] = mix;
391 static void add_verb(struct alc_spec *spec, const struct hda_verb *verb)
393 if (snd_BUG_ON(spec->num_init_verbs >= ARRAY_SIZE(spec->init_verbs)))
395 spec->init_verbs[spec->num_init_verbs++] = verb;
399 * GPIO setup tables, used in initialization
401 /* Enable GPIO mask and set output */
402 static const struct hda_verb alc_gpio1_init_verbs[] = {
403 {0x01, AC_VERB_SET_GPIO_MASK, 0x01},
404 {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
405 {0x01, AC_VERB_SET_GPIO_DATA, 0x01},
409 static const struct hda_verb alc_gpio2_init_verbs[] = {
410 {0x01, AC_VERB_SET_GPIO_MASK, 0x02},
411 {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
412 {0x01, AC_VERB_SET_GPIO_DATA, 0x02},
416 static const struct hda_verb alc_gpio3_init_verbs[] = {
417 {0x01, AC_VERB_SET_GPIO_MASK, 0x03},
418 {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x03},
419 {0x01, AC_VERB_SET_GPIO_DATA, 0x03},
424 * Fix hardware PLL issue
425 * On some codecs, the analog PLL gating control must be off while
426 * the default value is 1.
428 static void alc_fix_pll(struct hda_codec *codec)
430 struct alc_spec *spec = codec->spec;
435 snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
437 val = snd_hda_codec_read(codec, spec->pll_nid, 0,
438 AC_VERB_GET_PROC_COEF, 0);
441 snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
443 snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_PROC_COEF,
444 val & ~(1 << spec->pll_coef_bit));
447 static void alc_fix_pll_init(struct hda_codec *codec, hda_nid_t nid,
448 unsigned int coef_idx, unsigned int coef_bit)
450 struct alc_spec *spec = codec->spec;
452 spec->pll_coef_idx = coef_idx;
453 spec->pll_coef_bit = coef_bit;
458 * Jack-reporting via input-jack layer
461 /* initialization of jacks; currently checks only a few known pins */
462 static int alc_init_jacks(struct hda_codec *codec)
464 #ifdef CONFIG_SND_HDA_INPUT_JACK
465 struct alc_spec *spec = codec->spec;
467 unsigned int hp_nid = spec->autocfg.hp_pins[0];
468 unsigned int mic_nid = spec->ext_mic_pin;
469 unsigned int dock_nid = spec->dock_mic_pin;
472 err = snd_hda_input_jack_add(codec, hp_nid,
473 SND_JACK_HEADPHONE, NULL);
476 snd_hda_input_jack_report(codec, hp_nid);
480 err = snd_hda_input_jack_add(codec, mic_nid,
481 SND_JACK_MICROPHONE, NULL);
484 snd_hda_input_jack_report(codec, mic_nid);
487 err = snd_hda_input_jack_add(codec, dock_nid,
488 SND_JACK_MICROPHONE, NULL);
491 snd_hda_input_jack_report(codec, dock_nid);
493 #endif /* CONFIG_SND_HDA_INPUT_JACK */
498 * Jack detections for HP auto-mute and mic-switch
501 /* check each pin in the given array; returns true if any of them is plugged */
502 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
506 for (i = 0; i < num_pins; i++) {
507 hda_nid_t nid = pins[i];
510 snd_hda_input_jack_report(codec, nid);
511 present |= snd_hda_jack_detect(codec, nid);
516 /* standard HP/line-out auto-mute helper */
517 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
518 bool mute, bool hp_out)
520 struct alc_spec *spec = codec->spec;
521 unsigned int mute_bits = mute ? HDA_AMP_MUTE : 0;
522 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
525 for (i = 0; i < num_pins; i++) {
526 hda_nid_t nid = pins[i];
530 switch (spec->automute_mode) {
531 case ALC_AUTOMUTE_PIN:
532 /* don't reset VREF value in case it's controlling
533 * the amp (see alc861_fixup_asus_amp_vref_0f())
535 if (spec->keep_vref_in_automute) {
536 val = snd_hda_codec_read(codec, nid, 0,
537 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
542 snd_hda_codec_write(codec, nid, 0,
543 AC_VERB_SET_PIN_WIDGET_CONTROL,
546 case ALC_AUTOMUTE_AMP:
547 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
548 HDA_AMP_MUTE, mute_bits);
550 case ALC_AUTOMUTE_MIXER:
551 nid = spec->automute_mixer_nid[i];
554 snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 0,
555 HDA_AMP_MUTE, mute_bits);
556 snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 1,
557 HDA_AMP_MUTE, mute_bits);
563 /* Toggle outputs muting */
564 static void update_outputs(struct hda_codec *codec)
566 struct alc_spec *spec = codec->spec;
569 /* Control HP pins/amps depending on master_mute state;
570 * in general, HP pins/amps control should be enabled in all cases,
571 * but currently set only for master_mute, just to be safe
573 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
574 spec->autocfg.hp_pins, spec->master_mute, true);
576 if (!spec->automute_speaker)
579 on = spec->hp_jack_present | spec->line_jack_present;
580 on |= spec->master_mute;
581 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
582 spec->autocfg.speaker_pins, on, false);
584 /* toggle line-out mutes if needed, too */
585 /* if LO is a copy of either HP or Speaker, don't need to handle it */
586 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
587 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
589 if (!spec->automute_lo)
592 on = spec->hp_jack_present;
593 on |= spec->master_mute;
594 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
595 spec->autocfg.line_out_pins, on, false);
598 static void call_update_outputs(struct hda_codec *codec)
600 struct alc_spec *spec = codec->spec;
601 if (spec->automute_hook)
602 spec->automute_hook(codec);
604 update_outputs(codec);
607 /* standard HP-automute helper */
608 static void alc_hp_automute(struct hda_codec *codec)
610 struct alc_spec *spec = codec->spec;
612 spec->hp_jack_present =
613 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
614 spec->autocfg.hp_pins);
615 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
617 call_update_outputs(codec);
620 /* standard line-out-automute helper */
621 static void alc_line_automute(struct hda_codec *codec)
623 struct alc_spec *spec = codec->spec;
625 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
627 /* check LO jack only when it's different from HP */
628 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
631 spec->line_jack_present =
632 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
633 spec->autocfg.line_out_pins);
634 if (!spec->automute_speaker || !spec->detect_lo)
636 call_update_outputs(codec);
639 #define get_connection_index(codec, mux, nid) \
640 snd_hda_get_conn_index(codec, mux, nid, 0)
642 /* standard mic auto-switch helper */
643 static void alc_mic_automute(struct hda_codec *codec)
645 struct alc_spec *spec = codec->spec;
646 hda_nid_t *pins = spec->imux_pins;
648 if (!spec->auto_mic || !spec->auto_mic_valid_imux)
650 if (snd_BUG_ON(!spec->adc_nids))
652 if (snd_BUG_ON(spec->int_mic_idx < 0 || spec->ext_mic_idx < 0))
655 if (snd_hda_jack_detect(codec, pins[spec->ext_mic_idx]))
656 alc_mux_select(codec, 0, spec->ext_mic_idx, false);
657 else if (spec->dock_mic_idx >= 0 &&
658 snd_hda_jack_detect(codec, pins[spec->dock_mic_idx]))
659 alc_mux_select(codec, 0, spec->dock_mic_idx, false);
661 alc_mux_select(codec, 0, spec->int_mic_idx, false);
663 snd_hda_input_jack_report(codec, pins[spec->ext_mic_idx]);
664 if (spec->dock_mic_idx >= 0)
665 snd_hda_input_jack_report(codec, pins[spec->dock_mic_idx]);
668 /* unsolicited event for HP jack sensing */
669 static void alc_sku_unsol_event(struct hda_codec *codec, unsigned int res)
671 if (codec->vendor_id == 0x10ec0880)
677 alc_hp_automute(codec);
679 case ALC_FRONT_EVENT:
680 alc_line_automute(codec);
683 alc_mic_automute(codec);
688 /* call init functions of standard auto-mute helpers */
689 static void alc_inithook(struct hda_codec *codec)
691 alc_hp_automute(codec);
692 alc_line_automute(codec);
693 alc_mic_automute(codec);
696 /* additional initialization for ALC888 variants */
697 static void alc888_coef_init(struct hda_codec *codec)
701 snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 0);
702 tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
703 snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
704 if ((tmp & 0xf0) == 0x20)
706 snd_hda_codec_read(codec, 0x20, 0,
707 AC_VERB_SET_PROC_COEF, 0x830);
710 snd_hda_codec_read(codec, 0x20, 0,
711 AC_VERB_SET_PROC_COEF, 0x3030);
714 /* additional initialization for ALC889 variants */
715 static void alc889_coef_init(struct hda_codec *codec)
719 snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
720 tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
721 snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
722 snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_PROC_COEF, tmp|0x2010);
725 /* turn on/off EAPD control (only if available) */
726 static void set_eapd(struct hda_codec *codec, hda_nid_t nid, int on)
728 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
730 if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
731 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_EAPD_BTLENABLE,
735 /* turn on/off EAPD controls of the codec */
736 static void alc_auto_setup_eapd(struct hda_codec *codec, bool on)
738 /* We currently only handle front, HP */
739 static hda_nid_t pins[] = {
740 0x0f, 0x10, 0x14, 0x15, 0
743 for (p = pins; *p; p++)
744 set_eapd(codec, *p, on);
747 /* generic shutup callback;
748 * just turning off EPAD and a little pause for avoiding pop-noise
750 static void alc_eapd_shutup(struct hda_codec *codec)
752 alc_auto_setup_eapd(codec, false);
756 /* generic EAPD initialization */
757 static void alc_auto_init_amp(struct hda_codec *codec, int type)
761 alc_auto_setup_eapd(codec, true);
764 snd_hda_sequence_write(codec, alc_gpio1_init_verbs);
767 snd_hda_sequence_write(codec, alc_gpio2_init_verbs);
770 snd_hda_sequence_write(codec, alc_gpio3_init_verbs);
772 case ALC_INIT_DEFAULT:
773 switch (codec->vendor_id) {
775 snd_hda_codec_write(codec, 0x1a, 0,
776 AC_VERB_SET_COEF_INDEX, 7);
777 tmp = snd_hda_codec_read(codec, 0x1a, 0,
778 AC_VERB_GET_PROC_COEF, 0);
779 snd_hda_codec_write(codec, 0x1a, 0,
780 AC_VERB_SET_COEF_INDEX, 7);
781 snd_hda_codec_write(codec, 0x1a, 0,
782 AC_VERB_SET_PROC_COEF,
791 /*case 0x10ec0889:*/ /* this causes an SPDIF problem */
793 alc889_coef_init(codec);
796 alc888_coef_init(codec);
798 #if 0 /* XXX: This may cause the silent output on speaker on some machines */
801 snd_hda_codec_write(codec, 0x20, 0,
802 AC_VERB_SET_COEF_INDEX, 7);
803 tmp = snd_hda_codec_read(codec, 0x20, 0,
804 AC_VERB_GET_PROC_COEF, 0);
805 snd_hda_codec_write(codec, 0x20, 0,
806 AC_VERB_SET_COEF_INDEX, 7);
807 snd_hda_codec_write(codec, 0x20, 0,
808 AC_VERB_SET_PROC_COEF,
818 * Auto-Mute mode mixer enum support
820 static int alc_automute_mode_info(struct snd_kcontrol *kcontrol,
821 struct snd_ctl_elem_info *uinfo)
823 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
824 struct alc_spec *spec = codec->spec;
825 static const char * const texts2[] = {
826 "Disabled", "Enabled"
828 static const char * const texts3[] = {
829 "Disabled", "Speaker Only", "Line-Out+Speaker"
831 const char * const *texts;
833 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
835 if (spec->automute_speaker_possible && spec->automute_lo_possible) {
836 uinfo->value.enumerated.items = 3;
839 uinfo->value.enumerated.items = 2;
842 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
843 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
844 strcpy(uinfo->value.enumerated.name,
845 texts[uinfo->value.enumerated.item]);
849 static int alc_automute_mode_get(struct snd_kcontrol *kcontrol,
850 struct snd_ctl_elem_value *ucontrol)
852 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
853 struct alc_spec *spec = codec->spec;
854 unsigned int val = 0;
855 if (spec->automute_speaker)
857 if (spec->automute_lo)
860 ucontrol->value.enumerated.item[0] = val;
864 static int alc_automute_mode_put(struct snd_kcontrol *kcontrol,
865 struct snd_ctl_elem_value *ucontrol)
867 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
868 struct alc_spec *spec = codec->spec;
870 switch (ucontrol->value.enumerated.item[0]) {
872 if (!spec->automute_speaker && !spec->automute_lo)
874 spec->automute_speaker = 0;
875 spec->automute_lo = 0;
878 if (spec->automute_speaker_possible) {
879 if (!spec->automute_lo && spec->automute_speaker)
881 spec->automute_speaker = 1;
882 spec->automute_lo = 0;
883 } else if (spec->automute_lo_possible) {
884 if (spec->automute_lo)
886 spec->automute_lo = 1;
891 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
893 if (spec->automute_speaker && spec->automute_lo)
895 spec->automute_speaker = 1;
896 spec->automute_lo = 1;
901 call_update_outputs(codec);
905 static const struct snd_kcontrol_new alc_automute_mode_enum = {
906 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
907 .name = "Auto-Mute Mode",
908 .info = alc_automute_mode_info,
909 .get = alc_automute_mode_get,
910 .put = alc_automute_mode_put,
913 static struct snd_kcontrol_new *alc_kcontrol_new(struct alc_spec *spec)
915 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
916 return snd_array_new(&spec->kctls);
919 static int alc_add_automute_mode_enum(struct hda_codec *codec)
921 struct alc_spec *spec = codec->spec;
922 struct snd_kcontrol_new *knew;
924 knew = alc_kcontrol_new(spec);
927 *knew = alc_automute_mode_enum;
928 knew->name = kstrdup("Auto-Mute Mode", GFP_KERNEL);
935 * Check the availability of HP/line-out auto-mute;
936 * Set up appropriately if really supported
938 static void alc_init_automute(struct hda_codec *codec)
940 struct alc_spec *spec = codec->spec;
941 struct auto_pin_cfg *cfg = &spec->autocfg;
947 if (cfg->line_out_pins[0])
949 if (cfg->speaker_pins[0])
951 if (present < 2) /* need two different output types */
954 if (!cfg->speaker_pins[0] &&
955 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
956 memcpy(cfg->speaker_pins, cfg->line_out_pins,
957 sizeof(cfg->speaker_pins));
958 cfg->speaker_outs = cfg->line_outs;
961 if (!cfg->hp_pins[0] &&
962 cfg->line_out_type == AUTO_PIN_HP_OUT) {
963 memcpy(cfg->hp_pins, cfg->line_out_pins,
964 sizeof(cfg->hp_pins));
965 cfg->hp_outs = cfg->line_outs;
968 spec->automute_mode = ALC_AUTOMUTE_PIN;
970 for (i = 0; i < cfg->hp_outs; i++) {
971 hda_nid_t nid = cfg->hp_pins[i];
972 if (!is_jack_detectable(codec, nid))
974 snd_printdd("realtek: Enable HP auto-muting on NID 0x%x\n",
976 snd_hda_codec_write_cache(codec, nid, 0,
977 AC_VERB_SET_UNSOLICITED_ENABLE,
978 AC_USRSP_EN | ALC_HP_EVENT);
982 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
983 if (cfg->speaker_outs)
984 for (i = 0; i < cfg->line_outs; i++) {
985 hda_nid_t nid = cfg->line_out_pins[i];
986 if (!is_jack_detectable(codec, nid))
988 snd_printdd("realtek: Enable Line-Out "
989 "auto-muting on NID 0x%x\n", nid);
990 snd_hda_codec_write_cache(codec, nid, 0,
991 AC_VERB_SET_UNSOLICITED_ENABLE,
992 AC_USRSP_EN | ALC_FRONT_EVENT);
995 spec->automute_lo_possible = spec->detect_hp;
998 spec->automute_speaker_possible = cfg->speaker_outs &&
999 (spec->detect_hp || spec->detect_lo);
1001 spec->automute_lo = spec->automute_lo_possible;
1002 spec->automute_speaker = spec->automute_speaker_possible;
1004 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
1005 /* create a control for automute mode */
1006 alc_add_automute_mode_enum(codec);
1007 spec->unsol_event = alc_sku_unsol_event;
1011 /* return the position of NID in the list, or -1 if not found */
1012 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
1015 for (i = 0; i < nums; i++)
1021 /* check whether dynamic ADC-switching is available */
1022 static bool alc_check_dyn_adc_switch(struct hda_codec *codec)
1024 struct alc_spec *spec = codec->spec;
1025 struct hda_input_mux *imux = &spec->private_imux[0];
1029 if (imux != spec->input_mux) /* no dynamic imux? */
1032 for (n = 0; n < spec->num_adc_nids; n++) {
1033 cap = spec->private_capsrc_nids[n];
1034 for (i = 0; i < imux->num_items; i++) {
1035 pin = spec->imux_pins[i];
1038 if (get_connection_index(codec, cap, pin) < 0)
1041 if (i >= imux->num_items)
1042 return true; /* no ADC-switch is needed */
1045 for (i = 0; i < imux->num_items; i++) {
1046 pin = spec->imux_pins[i];
1047 for (n = 0; n < spec->num_adc_nids; n++) {
1048 cap = spec->private_capsrc_nids[n];
1049 idx = get_connection_index(codec, cap, pin);
1051 imux->items[i].index = idx;
1052 spec->dyn_adc_idx[i] = n;
1058 snd_printdd("realtek: enabling ADC switching\n");
1059 spec->dyn_adc_switch = 1;
1063 /* rebuild imux for matching with the given auto-mic pins (if not yet) */
1064 static bool alc_rebuild_imux_for_auto_mic(struct hda_codec *codec)
1066 struct alc_spec *spec = codec->spec;
1067 struct hda_input_mux *imux;
1068 static char * const texts[3] = {
1069 "Mic", "Internal Mic", "Dock Mic"
1073 if (!spec->auto_mic)
1075 imux = &spec->private_imux[0];
1076 if (spec->input_mux == imux)
1078 spec->imux_pins[0] = spec->ext_mic_pin;
1079 spec->imux_pins[1] = spec->int_mic_pin;
1080 spec->imux_pins[2] = spec->dock_mic_pin;
1081 for (i = 0; i < 3; i++) {
1082 strcpy(imux->items[i].label, texts[i]);
1083 if (spec->imux_pins[i]) {
1084 hda_nid_t pin = spec->imux_pins[i];
1086 for (c = 0; c < spec->num_adc_nids; c++) {
1087 hda_nid_t cap = get_capsrc(spec, c);
1088 int idx = get_connection_index(codec, cap, pin);
1090 imux->items[i].index = idx;
1094 imux->num_items = i + 1;
1097 spec->num_mux_defs = 1;
1098 spec->input_mux = imux;
1102 /* check whether all auto-mic pins are valid; setup indices if OK */
1103 static bool alc_auto_mic_check_imux(struct hda_codec *codec)
1105 struct alc_spec *spec = codec->spec;
1106 const struct hda_input_mux *imux;
1108 if (!spec->auto_mic)
1110 if (spec->auto_mic_valid_imux)
1111 return true; /* already checked */
1113 /* fill up imux indices */
1114 if (!alc_check_dyn_adc_switch(codec)) {
1119 imux = spec->input_mux;
1120 spec->ext_mic_idx = find_idx_in_nid_list(spec->ext_mic_pin,
1121 spec->imux_pins, imux->num_items);
1122 spec->int_mic_idx = find_idx_in_nid_list(spec->int_mic_pin,
1123 spec->imux_pins, imux->num_items);
1124 spec->dock_mic_idx = find_idx_in_nid_list(spec->dock_mic_pin,
1125 spec->imux_pins, imux->num_items);
1126 if (spec->ext_mic_idx < 0 || spec->int_mic_idx < 0) {
1128 return false; /* no corresponding imux */
1131 snd_hda_codec_write_cache(codec, spec->ext_mic_pin, 0,
1132 AC_VERB_SET_UNSOLICITED_ENABLE,
1133 AC_USRSP_EN | ALC_MIC_EVENT);
1134 if (spec->dock_mic_pin)
1135 snd_hda_codec_write_cache(codec, spec->dock_mic_pin, 0,
1136 AC_VERB_SET_UNSOLICITED_ENABLE,
1137 AC_USRSP_EN | ALC_MIC_EVENT);
1139 spec->auto_mic_valid_imux = 1;
1145 * Check the availability of auto-mic switch;
1146 * Set up if really supported
1148 static void alc_init_auto_mic(struct hda_codec *codec)
1150 struct alc_spec *spec = codec->spec;
1151 struct auto_pin_cfg *cfg = &spec->autocfg;
1152 hda_nid_t fixed, ext, dock;
1155 spec->ext_mic_idx = spec->int_mic_idx = spec->dock_mic_idx = -1;
1157 fixed = ext = dock = 0;
1158 for (i = 0; i < cfg->num_inputs; i++) {
1159 hda_nid_t nid = cfg->inputs[i].pin;
1160 unsigned int defcfg;
1161 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1162 switch (snd_hda_get_input_pin_attr(defcfg)) {
1163 case INPUT_PIN_ATTR_INT:
1165 return; /* already occupied */
1166 if (cfg->inputs[i].type != AUTO_PIN_MIC)
1167 return; /* invalid type */
1170 case INPUT_PIN_ATTR_UNUSED:
1171 return; /* invalid entry */
1172 case INPUT_PIN_ATTR_DOCK:
1174 return; /* already occupied */
1175 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
1176 return; /* invalid type */
1181 return; /* already occupied */
1182 if (cfg->inputs[i].type != AUTO_PIN_MIC)
1183 return; /* invalid type */
1194 if (!is_jack_detectable(codec, ext))
1195 return; /* no unsol support */
1196 if (dock && !is_jack_detectable(codec, dock))
1197 return; /* no unsol support */
1199 /* check imux indices */
1200 spec->ext_mic_pin = ext;
1201 spec->int_mic_pin = fixed;
1202 spec->dock_mic_pin = dock;
1205 if (!alc_auto_mic_check_imux(codec))
1208 snd_printdd("realtek: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
1210 spec->unsol_event = alc_sku_unsol_event;
1213 /* check the availabilities of auto-mute and auto-mic switches */
1214 static void alc_auto_check_switches(struct hda_codec *codec)
1216 alc_init_automute(codec);
1217 alc_init_auto_mic(codec);
1221 * Realtek SSID verification
1224 /* Could be any non-zero and even value. When used as fixup, tells
1225 * the driver to ignore any present sku defines.
1227 #define ALC_FIXUP_SKU_IGNORE (2)
1229 static int alc_auto_parse_customize_define(struct hda_codec *codec)
1231 unsigned int ass, tmp, i;
1233 struct alc_spec *spec = codec->spec;
1235 spec->cdefine.enable_pcbeep = 1; /* assume always enabled */
1237 if (spec->cdefine.fixup) {
1238 ass = spec->cdefine.sku_cfg;
1239 if (ass == ALC_FIXUP_SKU_IGNORE)
1244 ass = codec->subsystem_id & 0xffff;
1245 if (ass != codec->bus->pci->subsystem_device && (ass & 1))
1249 if (codec->vendor_id == 0x10ec0260)
1251 ass = snd_hda_codec_get_pincfg(codec, nid);
1254 printk(KERN_INFO "hda_codec: %s: SKU not ready 0x%08x\n",
1255 codec->chip_name, ass);
1261 for (i = 1; i < 16; i++) {
1265 if (((ass >> 16) & 0xf) != tmp)
1268 spec->cdefine.port_connectivity = ass >> 30;
1269 spec->cdefine.enable_pcbeep = (ass & 0x100000) >> 20;
1270 spec->cdefine.check_sum = (ass >> 16) & 0xf;
1271 spec->cdefine.customization = ass >> 8;
1273 spec->cdefine.sku_cfg = ass;
1274 spec->cdefine.external_amp = (ass & 0x38) >> 3;
1275 spec->cdefine.platform_type = (ass & 0x4) >> 2;
1276 spec->cdefine.swap = (ass & 0x2) >> 1;
1277 spec->cdefine.override = ass & 0x1;
1279 snd_printd("SKU: Nid=0x%x sku_cfg=0x%08x\n",
1280 nid, spec->cdefine.sku_cfg);
1281 snd_printd("SKU: port_connectivity=0x%x\n",
1282 spec->cdefine.port_connectivity);
1283 snd_printd("SKU: enable_pcbeep=0x%x\n", spec->cdefine.enable_pcbeep);
1284 snd_printd("SKU: check_sum=0x%08x\n", spec->cdefine.check_sum);
1285 snd_printd("SKU: customization=0x%08x\n", spec->cdefine.customization);
1286 snd_printd("SKU: external_amp=0x%x\n", spec->cdefine.external_amp);
1287 snd_printd("SKU: platform_type=0x%x\n", spec->cdefine.platform_type);
1288 snd_printd("SKU: swap=0x%x\n", spec->cdefine.swap);
1289 snd_printd("SKU: override=0x%x\n", spec->cdefine.override);
1294 /* return true if the given NID is found in the list */
1295 static bool found_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
1297 return find_idx_in_nid_list(nid, list, nums) >= 0;
1300 /* check subsystem ID and set up device-specific initialization;
1301 * return 1 if initialized, 0 if invalid SSID
1303 /* 32-bit subsystem ID for BIOS loading in HD Audio codec.
1304 * 31 ~ 16 : Manufacture ID
1306 * 7 ~ 0 : Assembly ID
1307 * port-A --> pin 39/41, port-E --> pin 14/15, port-D --> pin 35/36
1309 static int alc_subsystem_id(struct hda_codec *codec,
1310 hda_nid_t porta, hda_nid_t porte,
1311 hda_nid_t portd, hda_nid_t porti)
1313 unsigned int ass, tmp, i;
1315 struct alc_spec *spec = codec->spec;
1317 if (spec->cdefine.fixup) {
1318 ass = spec->cdefine.sku_cfg;
1319 if (ass == ALC_FIXUP_SKU_IGNORE)
1324 ass = codec->subsystem_id & 0xffff;
1325 if ((ass != codec->bus->pci->subsystem_device) && (ass & 1))
1328 /* invalid SSID, check the special NID pin defcfg instead */
1330 * 31~30 : port connectivity
1333 * 19~16 : Check sum (15:1)
1338 if (codec->vendor_id == 0x10ec0260)
1340 ass = snd_hda_codec_get_pincfg(codec, nid);
1341 snd_printd("realtek: No valid SSID, "
1342 "checking pincfg 0x%08x for NID 0x%x\n",
1346 if ((ass >> 30) != 1) /* no physical connection */
1351 for (i = 1; i < 16; i++) {
1355 if (((ass >> 16) & 0xf) != tmp)
1358 snd_printd("realtek: Enabling init ASM_ID=0x%04x CODEC_ID=%08x\n",
1359 ass & 0xffff, codec->vendor_id);
1363 * 2 : 0 --> Desktop, 1 --> Laptop
1364 * 3~5 : External Amplifier control
1367 tmp = (ass & 0x38) >> 3; /* external Amp control */
1370 spec->init_amp = ALC_INIT_GPIO1;
1373 spec->init_amp = ALC_INIT_GPIO2;
1376 spec->init_amp = ALC_INIT_GPIO3;
1380 spec->init_amp = ALC_INIT_DEFAULT;
1384 /* is laptop or Desktop and enable the function "Mute internal speaker
1385 * when the external headphone out jack is plugged"
1387 if (!(ass & 0x8000))
1390 * 10~8 : Jack location
1391 * 12~11: Headphone out -> 00: PortA, 01: PortE, 02: PortD, 03: Resvered
1393 * 15 : 1 --> enable the function "Mute internal speaker
1394 * when the external headphone out jack is plugged"
1396 if (!spec->autocfg.hp_pins[0] &&
1397 !(spec->autocfg.line_out_pins[0] &&
1398 spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)) {
1400 tmp = (ass >> 11) & 0x3; /* HP to chassis */
1411 if (found_in_nid_list(nid, spec->autocfg.line_out_pins,
1412 spec->autocfg.line_outs))
1414 spec->autocfg.hp_pins[0] = nid;
1419 /* Check the validity of ALC subsystem-id
1420 * ports contains an array of 4 pin NIDs for port-A, E, D and I */
1421 static void alc_ssid_check(struct hda_codec *codec, const hda_nid_t *ports)
1423 if (!alc_subsystem_id(codec, ports[0], ports[1], ports[2], ports[3])) {
1424 struct alc_spec *spec = codec->spec;
1425 snd_printd("realtek: "
1426 "Enable default setup for auto mode as fallback\n");
1427 spec->init_amp = ALC_INIT_DEFAULT;
1432 * Fix-up pin default configurations and add default verbs
1440 struct alc_model_fixup {
1451 const struct alc_pincfg *pins;
1452 const struct hda_verb *verbs;
1453 void (*func)(struct hda_codec *codec,
1454 const struct alc_fixup *fix,
1468 ALC_FIXUP_ACT_PRE_PROBE,
1469 ALC_FIXUP_ACT_PROBE,
1473 static void alc_apply_fixup(struct hda_codec *codec, int action)
1475 struct alc_spec *spec = codec->spec;
1476 int id = spec->fixup_id;
1477 #ifdef CONFIG_SND_DEBUG_VERBOSE
1478 const char *modelname = spec->fixup_name;
1482 if (!spec->fixup_list)
1486 const struct alc_fixup *fix = spec->fixup_list + id;
1487 const struct alc_pincfg *cfg;
1489 switch (fix->type) {
1491 if (action != ALC_FIXUP_ACT_PRE_PROBE || !fix->v.sku)
1493 snd_printdd(KERN_INFO "hda_codec: %s: "
1494 "Apply sku override for %s\n",
1495 codec->chip_name, modelname);
1496 spec->cdefine.sku_cfg = fix->v.sku;
1497 spec->cdefine.fixup = 1;
1499 case ALC_FIXUP_PINS:
1501 if (action != ALC_FIXUP_ACT_PRE_PROBE || !cfg)
1503 snd_printdd(KERN_INFO "hda_codec: %s: "
1504 "Apply pincfg for %s\n",
1505 codec->chip_name, modelname);
1506 for (; cfg->nid; cfg++)
1507 snd_hda_codec_set_pincfg(codec, cfg->nid,
1510 case ALC_FIXUP_VERBS:
1511 if (action != ALC_FIXUP_ACT_PROBE || !fix->v.verbs)
1513 snd_printdd(KERN_INFO "hda_codec: %s: "
1514 "Apply fix-verbs for %s\n",
1515 codec->chip_name, modelname);
1516 add_verb(codec->spec, fix->v.verbs);
1518 case ALC_FIXUP_FUNC:
1521 snd_printdd(KERN_INFO "hda_codec: %s: "
1522 "Apply fix-func for %s\n",
1523 codec->chip_name, modelname);
1524 fix->v.func(codec, fix, action);
1527 snd_printk(KERN_ERR "hda_codec: %s: "
1528 "Invalid fixup type %d\n",
1529 codec->chip_name, fix->type);
1540 static void alc_pick_fixup(struct hda_codec *codec,
1541 const struct alc_model_fixup *models,
1542 const struct snd_pci_quirk *quirk,
1543 const struct alc_fixup *fixlist)
1545 struct alc_spec *spec = codec->spec;
1547 const char *name = NULL;
1549 if (codec->modelname && models) {
1550 while (models->name) {
1551 if (!strcmp(codec->modelname, models->name)) {
1553 name = models->name;
1560 quirk = snd_pci_quirk_lookup(codec->bus->pci, quirk);
1563 #ifdef CONFIG_SND_DEBUG_VERBOSE
1569 spec->fixup_id = id;
1571 spec->fixup_list = fixlist;
1572 spec->fixup_name = name;
1577 * COEF access helper functions
1579 static int alc_read_coef_idx(struct hda_codec *codec,
1580 unsigned int coef_idx)
1583 snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX,
1585 val = snd_hda_codec_read(codec, 0x20, 0,
1586 AC_VERB_GET_PROC_COEF, 0);
1590 static void alc_write_coef_idx(struct hda_codec *codec, unsigned int coef_idx,
1591 unsigned int coef_val)
1593 snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX,
1595 snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_PROC_COEF,
1599 /* a special bypass for COEF 0; read the cached value at the second time */
1600 static unsigned int alc_get_coef0(struct hda_codec *codec)
1602 struct alc_spec *spec = codec->spec;
1604 spec->coef0 = alc_read_coef_idx(codec, 0);
1609 * Digital I/O handling
1612 /* set right pin controls for digital I/O */
1613 static void alc_auto_init_digital(struct hda_codec *codec)
1615 struct alc_spec *spec = codec->spec;
1619 for (i = 0; i < spec->autocfg.dig_outs; i++) {
1620 pin = spec->autocfg.dig_out_pins[i];
1623 snd_hda_codec_write(codec, pin, 0,
1624 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1626 dac = spec->multiout.dig_out_nid;
1628 dac = spec->slave_dig_outs[i - 1];
1629 if (!dac || !(get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
1631 snd_hda_codec_write(codec, dac, 0,
1632 AC_VERB_SET_AMP_GAIN_MUTE,
1635 pin = spec->autocfg.dig_in_pin;
1637 snd_hda_codec_write(codec, pin, 0,
1638 AC_VERB_SET_PIN_WIDGET_CONTROL,
1642 /* parse digital I/Os and set up NIDs in BIOS auto-parse mode */
1643 static void alc_auto_parse_digital(struct hda_codec *codec)
1645 struct alc_spec *spec = codec->spec;
1649 /* support multiple SPDIFs; the secondary is set up as a slave */
1651 for (i = 0; i < spec->autocfg.dig_outs; i++) {
1653 err = snd_hda_get_connections(codec,
1654 spec->autocfg.dig_out_pins[i],
1655 conn, ARRAY_SIZE(conn));
1658 dig_nid = conn[0]; /* assume the first element is audio-out */
1660 spec->multiout.dig_out_nid = dig_nid;
1661 spec->dig_out_type = spec->autocfg.dig_out_type[0];
1663 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
1664 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
1666 spec->slave_dig_outs[nums - 1] = dig_nid;
1671 if (spec->autocfg.dig_in_pin) {
1672 dig_nid = codec->start_nid;
1673 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
1674 unsigned int wcaps = get_wcaps(codec, dig_nid);
1675 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
1677 if (!(wcaps & AC_WCAP_DIGITAL))
1679 if (!(wcaps & AC_WCAP_CONN_LIST))
1681 err = get_connection_index(codec, dig_nid,
1682 spec->autocfg.dig_in_pin);
1684 spec->dig_in_nid = dig_nid;
1692 * capture mixer elements
1694 static int alc_cap_vol_info(struct snd_kcontrol *kcontrol,
1695 struct snd_ctl_elem_info *uinfo)
1697 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1698 struct alc_spec *spec = codec->spec;
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);
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_volume_info(kcontrol, uinfo);
1709 mutex_unlock(&codec->control_mutex);
1713 static int alc_cap_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1714 unsigned int size, unsigned int __user *tlv)
1716 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1717 struct alc_spec *spec = codec->spec;
1721 mutex_lock(&codec->control_mutex);
1722 if (spec->vol_in_capsrc)
1723 val = HDA_COMPOSE_AMP_VAL(spec->capsrc_nids[0], 3, 0, HDA_OUTPUT);
1725 val = HDA_COMPOSE_AMP_VAL(spec->adc_nids[0], 3, 0, HDA_INPUT);
1726 kcontrol->private_value = val;
1727 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
1728 mutex_unlock(&codec->control_mutex);
1732 typedef int (*getput_call_t)(struct snd_kcontrol *kcontrol,
1733 struct snd_ctl_elem_value *ucontrol);
1735 static int alc_cap_getput_caller(struct snd_kcontrol *kcontrol,
1736 struct snd_ctl_elem_value *ucontrol,
1737 getput_call_t func, bool check_adc_switch)
1739 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1740 struct alc_spec *spec = codec->spec;
1743 mutex_lock(&codec->control_mutex);
1744 if (check_adc_switch && spec->dyn_adc_switch) {
1745 for (i = 0; i < spec->num_adc_nids; i++) {
1746 kcontrol->private_value =
1747 HDA_COMPOSE_AMP_VAL(spec->adc_nids[i],
1749 err = func(kcontrol, ucontrol);
1754 i = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1755 if (spec->vol_in_capsrc)
1756 kcontrol->private_value =
1757 HDA_COMPOSE_AMP_VAL(spec->capsrc_nids[i],
1760 kcontrol->private_value =
1761 HDA_COMPOSE_AMP_VAL(spec->adc_nids[i],
1763 err = func(kcontrol, ucontrol);
1766 mutex_unlock(&codec->control_mutex);
1770 static int alc_cap_vol_get(struct snd_kcontrol *kcontrol,
1771 struct snd_ctl_elem_value *ucontrol)
1773 return alc_cap_getput_caller(kcontrol, ucontrol,
1774 snd_hda_mixer_amp_volume_get, false);
1777 static int alc_cap_vol_put(struct snd_kcontrol *kcontrol,
1778 struct snd_ctl_elem_value *ucontrol)
1780 return alc_cap_getput_caller(kcontrol, ucontrol,
1781 snd_hda_mixer_amp_volume_put, true);
1784 /* capture mixer elements */
1785 #define alc_cap_sw_info snd_ctl_boolean_stereo_info
1787 static int alc_cap_sw_get(struct snd_kcontrol *kcontrol,
1788 struct snd_ctl_elem_value *ucontrol)
1790 return alc_cap_getput_caller(kcontrol, ucontrol,
1791 snd_hda_mixer_amp_switch_get, false);
1794 static int alc_cap_sw_put(struct snd_kcontrol *kcontrol,
1795 struct snd_ctl_elem_value *ucontrol)
1797 return alc_cap_getput_caller(kcontrol, ucontrol,
1798 snd_hda_mixer_amp_switch_put, true);
1801 #define _DEFINE_CAPMIX(num) \
1803 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1804 .name = "Capture Switch", \
1805 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
1807 .info = alc_cap_sw_info, \
1808 .get = alc_cap_sw_get, \
1809 .put = alc_cap_sw_put, \
1812 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1813 .name = "Capture Volume", \
1814 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE | \
1815 SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
1816 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK), \
1818 .info = alc_cap_vol_info, \
1819 .get = alc_cap_vol_get, \
1820 .put = alc_cap_vol_put, \
1821 .tlv = { .c = alc_cap_vol_tlv }, \
1824 #define _DEFINE_CAPSRC(num) \
1826 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1827 /* .name = "Capture Source", */ \
1828 .name = "Input Source", \
1830 .info = alc_mux_enum_info, \
1831 .get = alc_mux_enum_get, \
1832 .put = alc_mux_enum_put, \
1835 #define DEFINE_CAPMIX(num) \
1836 static const struct snd_kcontrol_new alc_capture_mixer ## num[] = { \
1837 _DEFINE_CAPMIX(num), \
1838 _DEFINE_CAPSRC(num), \
1842 #define DEFINE_CAPMIX_NOSRC(num) \
1843 static const struct snd_kcontrol_new alc_capture_mixer_nosrc ## num[] = { \
1844 _DEFINE_CAPMIX(num), \
1848 /* up to three ADCs */
1852 DEFINE_CAPMIX_NOSRC(1);
1853 DEFINE_CAPMIX_NOSRC(2);
1854 DEFINE_CAPMIX_NOSRC(3);
1857 * virtual master controls
1861 * slave controls for virtual master
1863 static const char * const alc_slave_vols[] = {
1864 "Front Playback Volume",
1865 "Surround Playback Volume",
1866 "Center Playback Volume",
1867 "LFE Playback Volume",
1868 "Side Playback Volume",
1869 "Headphone Playback Volume",
1870 "Speaker Playback Volume",
1871 "Mono Playback Volume",
1872 "Line-Out Playback Volume",
1873 "PCM Playback Volume",
1877 static const char * const alc_slave_sws[] = {
1878 "Front Playback Switch",
1879 "Surround Playback Switch",
1880 "Center Playback Switch",
1881 "LFE Playback Switch",
1882 "Side Playback Switch",
1883 "Headphone Playback Switch",
1884 "Speaker Playback Switch",
1885 "Mono Playback Switch",
1886 "IEC958 Playback Switch",
1887 "Line-Out Playback Switch",
1888 "PCM Playback Switch",
1893 * build control elements
1896 #define NID_MAPPING (-1)
1898 #define SUBDEV_SPEAKER_ (0 << 6)
1899 #define SUBDEV_HP_ (1 << 6)
1900 #define SUBDEV_LINE_ (2 << 6)
1901 #define SUBDEV_SPEAKER(x) (SUBDEV_SPEAKER_ | ((x) & 0x3f))
1902 #define SUBDEV_HP(x) (SUBDEV_HP_ | ((x) & 0x3f))
1903 #define SUBDEV_LINE(x) (SUBDEV_LINE_ | ((x) & 0x3f))
1905 static void alc_free_kctls(struct hda_codec *codec);
1907 #ifdef CONFIG_SND_HDA_INPUT_BEEP
1908 /* additional beep mixers; the actual parameters are overwritten at build */
1909 static const struct snd_kcontrol_new alc_beep_mixer[] = {
1910 HDA_CODEC_VOLUME("Beep Playback Volume", 0, 0, HDA_INPUT),
1911 HDA_CODEC_MUTE_BEEP("Beep Playback Switch", 0, 0, HDA_INPUT),
1916 static int alc_build_controls(struct hda_codec *codec)
1918 struct alc_spec *spec = codec->spec;
1919 struct snd_kcontrol *kctl = NULL;
1920 const struct snd_kcontrol_new *knew;
1925 for (i = 0; i < spec->num_mixers; i++) {
1926 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1930 if (spec->cap_mixer) {
1931 err = snd_hda_add_new_ctls(codec, spec->cap_mixer);
1935 if (spec->multiout.dig_out_nid) {
1936 err = snd_hda_create_spdif_out_ctls(codec,
1937 spec->multiout.dig_out_nid,
1938 spec->multiout.dig_out_nid);
1941 if (!spec->no_analog) {
1942 err = snd_hda_create_spdif_share_sw(codec,
1946 spec->multiout.share_spdif = 1;
1949 if (spec->dig_in_nid) {
1950 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1955 #ifdef CONFIG_SND_HDA_INPUT_BEEP
1956 /* create beep controls if needed */
1957 if (spec->beep_amp) {
1958 const struct snd_kcontrol_new *knew;
1959 for (knew = alc_beep_mixer; knew->name; knew++) {
1960 struct snd_kcontrol *kctl;
1961 kctl = snd_ctl_new1(knew, codec);
1964 kctl->private_value = spec->beep_amp;
1965 err = snd_hda_ctl_add(codec, 0, kctl);
1972 /* if we have no master control, let's create it */
1973 if (!spec->no_analog &&
1974 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1975 unsigned int vmaster_tlv[4];
1976 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1977 HDA_OUTPUT, vmaster_tlv);
1978 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
1979 vmaster_tlv, alc_slave_vols);
1983 if (!spec->no_analog &&
1984 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1985 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
1986 NULL, alc_slave_sws);
1991 /* assign Capture Source enums to NID */
1992 if (spec->capsrc_nids || spec->adc_nids) {
1993 kctl = snd_hda_find_mixer_ctl(codec, "Capture Source");
1995 kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1996 for (i = 0; kctl && i < kctl->count; i++) {
1997 err = snd_hda_add_nid(codec, kctl, i,
1998 get_capsrc(spec, i));
2003 if (spec->cap_mixer && spec->adc_nids) {
2004 const char *kname = kctl ? kctl->id.name : NULL;
2005 for (knew = spec->cap_mixer; knew->name; knew++) {
2006 if (kname && strcmp(knew->name, kname) == 0)
2008 kctl = snd_hda_find_mixer_ctl(codec, knew->name);
2009 for (i = 0; kctl && i < kctl->count; i++) {
2010 err = snd_hda_add_nid(codec, kctl, i,
2018 /* other nid->control mapping */
2019 for (i = 0; i < spec->num_mixers; i++) {
2020 for (knew = spec->mixers[i]; knew->name; knew++) {
2021 if (knew->iface != NID_MAPPING)
2023 kctl = snd_hda_find_mixer_ctl(codec, knew->name);
2026 u = knew->subdevice;
2027 for (j = 0; j < 4; j++, u >>= 8) {
2032 case SUBDEV_SPEAKER_:
2033 nid = spec->autocfg.speaker_pins[nid];
2036 nid = spec->autocfg.line_out_pins[nid];
2039 nid = spec->autocfg.hp_pins[nid];
2044 err = snd_hda_add_nid(codec, kctl, 0, nid);
2048 u = knew->private_value;
2049 for (j = 0; j < 4; j++, u >>= 8) {
2053 err = snd_hda_add_nid(codec, kctl, 0, nid);
2060 alc_free_kctls(codec); /* no longer needed */
2070 static void alc_init_special_input_src(struct hda_codec *codec);
2071 static int alc269_fill_coef(struct hda_codec *codec);
2073 static int alc_init(struct hda_codec *codec)
2075 struct alc_spec *spec = codec->spec;
2078 if (codec->vendor_id == 0x10ec0269)
2079 alc269_fill_coef(codec);
2082 alc_auto_init_amp(codec, spec->init_amp);
2084 for (i = 0; i < spec->num_init_verbs; i++)
2085 snd_hda_sequence_write(codec, spec->init_verbs[i]);
2086 alc_init_special_input_src(codec);
2088 if (spec->init_hook)
2089 spec->init_hook(codec);
2091 alc_apply_fixup(codec, ALC_FIXUP_ACT_INIT);
2093 hda_call_check_power_status(codec, 0x01);
2097 static void alc_unsol_event(struct hda_codec *codec, unsigned int res)
2099 struct alc_spec *spec = codec->spec;
2101 if (spec->unsol_event)
2102 spec->unsol_event(codec, res);
2105 #ifdef CONFIG_SND_HDA_POWER_SAVE
2106 static int alc_check_power_status(struct hda_codec *codec, hda_nid_t nid)
2108 struct alc_spec *spec = codec->spec;
2109 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
2114 * Analog playback callbacks
2116 static int alc_playback_pcm_open(struct hda_pcm_stream *hinfo,
2117 struct hda_codec *codec,
2118 struct snd_pcm_substream *substream)
2120 struct alc_spec *spec = codec->spec;
2121 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
2125 static int alc_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2126 struct hda_codec *codec,
2127 unsigned int stream_tag,
2128 unsigned int format,
2129 struct snd_pcm_substream *substream)
2131 struct alc_spec *spec = codec->spec;
2132 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
2133 stream_tag, format, substream);
2136 static int alc_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2137 struct hda_codec *codec,
2138 struct snd_pcm_substream *substream)
2140 struct alc_spec *spec = codec->spec;
2141 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
2147 static int alc_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
2148 struct hda_codec *codec,
2149 struct snd_pcm_substream *substream)
2151 struct alc_spec *spec = codec->spec;
2152 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2155 static int alc_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2156 struct hda_codec *codec,
2157 unsigned int stream_tag,
2158 unsigned int format,
2159 struct snd_pcm_substream *substream)
2161 struct alc_spec *spec = codec->spec;
2162 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2163 stream_tag, format, substream);
2166 static int alc_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2167 struct hda_codec *codec,
2168 struct snd_pcm_substream *substream)
2170 struct alc_spec *spec = codec->spec;
2171 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
2174 static int alc_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
2175 struct hda_codec *codec,
2176 struct snd_pcm_substream *substream)
2178 struct alc_spec *spec = codec->spec;
2179 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2185 static int alc_alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2186 struct hda_codec *codec,
2187 unsigned int stream_tag,
2188 unsigned int format,
2189 struct snd_pcm_substream *substream)
2191 struct alc_spec *spec = codec->spec;
2193 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
2194 stream_tag, 0, format);
2198 static int alc_alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2199 struct hda_codec *codec,
2200 struct snd_pcm_substream *substream)
2202 struct alc_spec *spec = codec->spec;
2204 snd_hda_codec_cleanup_stream(codec,
2205 spec->adc_nids[substream->number + 1]);
2209 /* analog capture with dynamic dual-adc changes */
2210 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2211 struct hda_codec *codec,
2212 unsigned int stream_tag,
2213 unsigned int format,
2214 struct snd_pcm_substream *substream)
2216 struct alc_spec *spec = codec->spec;
2217 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
2218 spec->cur_adc_stream_tag = stream_tag;
2219 spec->cur_adc_format = format;
2220 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
2224 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2225 struct hda_codec *codec,
2226 struct snd_pcm_substream *substream)
2228 struct alc_spec *spec = codec->spec;
2229 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
2234 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
2238 .nid = 0, /* fill later */
2240 .prepare = dyn_adc_capture_pcm_prepare,
2241 .cleanup = dyn_adc_capture_pcm_cleanup
2247 static const struct hda_pcm_stream alc_pcm_analog_playback = {
2251 /* NID is set in alc_build_pcms */
2253 .open = alc_playback_pcm_open,
2254 .prepare = alc_playback_pcm_prepare,
2255 .cleanup = alc_playback_pcm_cleanup
2259 static const struct hda_pcm_stream alc_pcm_analog_capture = {
2263 /* NID is set in alc_build_pcms */
2266 static const struct hda_pcm_stream alc_pcm_analog_alt_playback = {
2270 /* NID is set in alc_build_pcms */
2273 static const struct hda_pcm_stream alc_pcm_analog_alt_capture = {
2274 .substreams = 2, /* can be overridden */
2277 /* NID is set in alc_build_pcms */
2279 .prepare = alc_alt_capture_pcm_prepare,
2280 .cleanup = alc_alt_capture_pcm_cleanup
2284 static const struct hda_pcm_stream alc_pcm_digital_playback = {
2288 /* NID is set in alc_build_pcms */
2290 .open = alc_dig_playback_pcm_open,
2291 .close = alc_dig_playback_pcm_close,
2292 .prepare = alc_dig_playback_pcm_prepare,
2293 .cleanup = alc_dig_playback_pcm_cleanup
2297 static const struct hda_pcm_stream alc_pcm_digital_capture = {
2301 /* NID is set in alc_build_pcms */
2304 /* Used by alc_build_pcms to flag that a PCM has no playback stream */
2305 static const struct hda_pcm_stream alc_pcm_null_stream = {
2311 static int alc_build_pcms(struct hda_codec *codec)
2313 struct alc_spec *spec = codec->spec;
2314 struct hda_pcm *info = spec->pcm_rec;
2315 const struct hda_pcm_stream *p;
2316 bool have_multi_adcs;
2319 codec->num_pcms = 1;
2320 codec->pcm_info = info;
2322 if (spec->no_analog)
2325 snprintf(spec->stream_name_analog, sizeof(spec->stream_name_analog),
2326 "%s Analog", codec->chip_name);
2327 info->name = spec->stream_name_analog;
2329 if (spec->multiout.dac_nids > 0) {
2330 p = spec->stream_analog_playback;
2332 p = &alc_pcm_analog_playback;
2333 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2334 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
2336 if (spec->adc_nids) {
2337 p = spec->stream_analog_capture;
2339 if (spec->dyn_adc_switch)
2340 p = &dyn_adc_pcm_analog_capture;
2342 p = &alc_pcm_analog_capture;
2344 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2345 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
2348 if (spec->channel_mode) {
2349 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = 0;
2350 for (i = 0; i < spec->num_channel_mode; i++) {
2351 if (spec->channel_mode[i].channels > info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max) {
2352 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->channel_mode[i].channels;
2358 /* SPDIF for stream index #1 */
2359 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
2360 snprintf(spec->stream_name_digital,
2361 sizeof(spec->stream_name_digital),
2362 "%s Digital", codec->chip_name);
2363 codec->num_pcms = 2;
2364 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
2365 info = spec->pcm_rec + 1;
2366 info->name = spec->stream_name_digital;
2367 if (spec->dig_out_type)
2368 info->pcm_type = spec->dig_out_type;
2370 info->pcm_type = HDA_PCM_TYPE_SPDIF;
2371 if (spec->multiout.dig_out_nid) {
2372 p = spec->stream_digital_playback;
2374 p = &alc_pcm_digital_playback;
2375 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2376 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
2378 if (spec->dig_in_nid) {
2379 p = spec->stream_digital_capture;
2381 p = &alc_pcm_digital_capture;
2382 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2383 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
2385 /* FIXME: do we need this for all Realtek codec models? */
2386 codec->spdif_status_reset = 1;
2389 if (spec->no_analog)
2392 /* If the use of more than one ADC is requested for the current
2393 * model, configure a second analog capture-only PCM.
2395 have_multi_adcs = (spec->num_adc_nids > 1) &&
2396 !spec->dyn_adc_switch && !spec->auto_mic &&
2397 (!spec->input_mux || spec->input_mux->num_items > 1);
2398 /* Additional Analaog capture for index #2 */
2399 if (spec->alt_dac_nid || have_multi_adcs) {
2400 codec->num_pcms = 3;
2401 info = spec->pcm_rec + 2;
2402 info->name = spec->stream_name_analog;
2403 if (spec->alt_dac_nid) {
2404 p = spec->stream_analog_alt_playback;
2406 p = &alc_pcm_analog_alt_playback;
2407 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2408 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
2411 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
2412 alc_pcm_null_stream;
2413 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
2415 if (have_multi_adcs) {
2416 p = spec->stream_analog_alt_capture;
2418 p = &alc_pcm_analog_alt_capture;
2419 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2420 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
2422 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
2423 spec->num_adc_nids - 1;
2425 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
2426 alc_pcm_null_stream;
2427 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
2434 static inline void alc_shutup(struct hda_codec *codec)
2436 struct alc_spec *spec = codec->spec;
2438 if (spec && spec->shutup)
2439 spec->shutup(codec);
2440 snd_hda_shutup_pins(codec);
2443 static void alc_free_kctls(struct hda_codec *codec)
2445 struct alc_spec *spec = codec->spec;
2447 if (spec->kctls.list) {
2448 struct snd_kcontrol_new *kctl = spec->kctls.list;
2450 for (i = 0; i < spec->kctls.used; i++)
2451 kfree(kctl[i].name);
2453 snd_array_free(&spec->kctls);
2456 static void alc_free_bind_ctls(struct hda_codec *codec)
2458 struct alc_spec *spec = codec->spec;
2459 if (spec->bind_ctls.list) {
2460 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
2462 for (i = 0; i < spec->bind_ctls.used; i++)
2465 snd_array_free(&spec->bind_ctls);
2468 static void alc_free(struct hda_codec *codec)
2470 struct alc_spec *spec = codec->spec;
2476 snd_hda_input_jack_free(codec);
2477 alc_free_kctls(codec);
2478 alc_free_bind_ctls(codec);
2480 snd_hda_detach_beep_device(codec);
2483 #ifdef CONFIG_SND_HDA_POWER_SAVE
2484 static void alc_power_eapd(struct hda_codec *codec)
2486 alc_auto_setup_eapd(codec, false);
2489 static int alc_suspend(struct hda_codec *codec, pm_message_t state)
2491 struct alc_spec *spec = codec->spec;
2493 if (spec && spec->power_hook)
2494 spec->power_hook(codec);
2500 static int alc_resume(struct hda_codec *codec)
2502 msleep(150); /* to avoid pop noise */
2503 codec->patch_ops.init(codec);
2504 snd_hda_codec_resume_amp(codec);
2505 snd_hda_codec_resume_cache(codec);
2506 hda_call_check_power_status(codec, 0x01);
2513 static const struct hda_codec_ops alc_patch_ops = {
2514 .build_controls = alc_build_controls,
2515 .build_pcms = alc_build_pcms,
2518 .unsol_event = alc_unsol_event,
2520 .resume = alc_resume,
2522 #ifdef CONFIG_SND_HDA_POWER_SAVE
2523 .suspend = alc_suspend,
2524 .check_power_status = alc_check_power_status,
2526 .reboot_notify = alc_shutup,
2529 /* replace the codec chip_name with the given string */
2530 static int alc_codec_rename(struct hda_codec *codec, const char *name)
2532 kfree(codec->chip_name);
2533 codec->chip_name = kstrdup(name, GFP_KERNEL);
2534 if (!codec->chip_name) {
2542 * Rename codecs appropriately from COEF value
2544 struct alc_codec_rename_table {
2545 unsigned int vendor_id;
2546 unsigned short coef_mask;
2547 unsigned short coef_bits;
2551 static struct alc_codec_rename_table rename_tbl[] = {
2552 { 0x10ec0269, 0xfff0, 0x3010, "ALC277" },
2553 { 0x10ec0269, 0xf0f0, 0x2010, "ALC259" },
2554 { 0x10ec0269, 0xf0f0, 0x3010, "ALC258" },
2555 { 0x10ec0269, 0x00f0, 0x0010, "ALC269VB" },
2556 { 0x10ec0269, 0xffff, 0xa023, "ALC259" },
2557 { 0x10ec0269, 0xffff, 0x6023, "ALC281X" },
2558 { 0x10ec0269, 0x00f0, 0x0020, "ALC269VC" },
2559 { 0x10ec0887, 0x00f0, 0x0030, "ALC887-VD" },
2560 { 0x10ec0888, 0x00f0, 0x0030, "ALC888-VD" },
2561 { 0x10ec0888, 0xf0f0, 0x3020, "ALC886" },
2562 { 0x10ec0899, 0x2000, 0x2000, "ALC899" },
2563 { 0x10ec0892, 0xffff, 0x8020, "ALC661" },
2564 { 0x10ec0892, 0xffff, 0x8011, "ALC661" },
2565 { 0x10ec0892, 0xffff, 0x4011, "ALC656" },
2566 { } /* terminator */
2569 static int alc_codec_rename_from_preset(struct hda_codec *codec)
2571 const struct alc_codec_rename_table *p;
2573 for (p = rename_tbl; p->vendor_id; p++) {
2574 if (p->vendor_id != codec->vendor_id)
2576 if ((alc_get_coef0(codec) & p->coef_mask) == p->coef_bits)
2577 return alc_codec_rename(codec, p->name);
2583 * Automatic parse of I/O pins from the BIOS configuration
2588 ALC_CTL_WIDGET_MUTE,
2593 static const struct snd_kcontrol_new alc_control_templates[] = {
2594 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
2595 HDA_CODEC_MUTE(NULL, 0, 0, 0),
2596 HDA_BIND_MUTE(NULL, 0, 0, 0),
2597 HDA_BIND_VOL(NULL, 0),
2598 HDA_BIND_SW(NULL, 0),
2601 /* add dynamic controls */
2602 static int add_control(struct alc_spec *spec, int type, const char *name,
2603 int cidx, unsigned long val)
2605 struct snd_kcontrol_new *knew;
2607 knew = alc_kcontrol_new(spec);
2610 *knew = alc_control_templates[type];
2611 knew->name = kstrdup(name, GFP_KERNEL);
2615 if (get_amp_nid_(val))
2616 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2617 knew->private_value = val;
2621 static int add_control_with_pfx(struct alc_spec *spec, int type,
2622 const char *pfx, const char *dir,
2623 const char *sfx, int cidx, unsigned long val)
2626 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
2627 return add_control(spec, type, name, cidx, val);
2630 #define add_pb_vol_ctrl(spec, type, pfx, val) \
2631 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
2632 #define add_pb_sw_ctrl(spec, type, pfx, val) \
2633 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
2634 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
2635 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
2636 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
2637 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
2639 static const char * const channel_name[4] = {
2640 "Front", "Surround", "CLFE", "Side"
2643 static const char *alc_get_line_out_pfx(struct alc_spec *spec, int ch,
2644 bool can_be_master, int *index)
2646 struct auto_pin_cfg *cfg = &spec->autocfg;
2649 if (cfg->line_outs == 1 && !spec->multi_ios &&
2650 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
2653 switch (cfg->line_out_type) {
2654 case AUTO_PIN_SPEAKER_OUT:
2655 if (cfg->line_outs == 1)
2657 if (cfg->line_outs == 2)
2658 return ch ? "Bass Speaker" : "Speaker";
2660 case AUTO_PIN_HP_OUT:
2661 /* for multi-io case, only the primary out */
2662 if (ch && spec->multi_ios)
2667 if (cfg->line_outs == 1 && !spec->multi_ios)
2671 if (ch >= ARRAY_SIZE(channel_name)) {
2676 return channel_name[ch];
2679 /* create input playback/capture controls for the given pin */
2680 static int new_analog_input(struct alc_spec *spec, hda_nid_t pin,
2681 const char *ctlname, int ctlidx,
2682 int idx, hda_nid_t mix_nid)
2686 err = __add_pb_vol_ctrl(spec, ALC_CTL_WIDGET_VOL, ctlname, ctlidx,
2687 HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
2690 err = __add_pb_sw_ctrl(spec, ALC_CTL_WIDGET_MUTE, ctlname, ctlidx,
2691 HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
2697 static int alc_is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2699 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2700 return (pincap & AC_PINCAP_IN) != 0;
2703 /* Parse the codec tree and retrieve ADCs and corresponding capsrc MUXs */
2704 static int alc_auto_fill_adc_caps(struct hda_codec *codec)
2706 struct alc_spec *spec = codec->spec;
2708 hda_nid_t *adc_nids = spec->private_adc_nids;
2709 hda_nid_t *cap_nids = spec->private_capsrc_nids;
2710 int max_nums = ARRAY_SIZE(spec->private_adc_nids);
2713 nid = codec->start_nid;
2714 for (i = 0; i < codec->num_nodes; i++, nid++) {
2716 const hda_nid_t *list;
2717 unsigned int caps = get_wcaps(codec, nid);
2718 int type = get_wcaps_type(caps);
2720 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2722 adc_nids[nums] = nid;
2723 cap_nids[nums] = nid;
2727 type = get_wcaps_type(get_wcaps(codec, src));
2728 if (type == AC_WID_PIN)
2730 if (type == AC_WID_AUD_SEL) {
2731 cap_nids[nums] = src;
2734 n = snd_hda_get_conn_list(codec, src, &list);
2736 cap_nids[nums] = src;
2742 if (++nums >= max_nums)
2745 spec->adc_nids = spec->private_adc_nids;
2746 spec->capsrc_nids = spec->private_capsrc_nids;
2747 spec->num_adc_nids = nums;
2751 /* create playback/capture controls for input pins */
2752 static int alc_auto_create_input_ctls(struct hda_codec *codec)
2754 struct alc_spec *spec = codec->spec;
2755 const struct auto_pin_cfg *cfg = &spec->autocfg;
2756 hda_nid_t mixer = spec->mixer_nid;
2757 struct hda_input_mux *imux = &spec->private_imux[0];
2759 int i, c, err, idx, type_idx = 0;
2760 const char *prev_label = NULL;
2762 num_adcs = alc_auto_fill_adc_caps(codec);
2766 for (i = 0; i < cfg->num_inputs; i++) {
2770 pin = cfg->inputs[i].pin;
2771 if (!alc_is_input_pin(codec, pin))
2774 label = hda_get_autocfg_input_label(codec, cfg, i);
2775 if (prev_label && !strcmp(label, prev_label))
2782 idx = get_connection_index(codec, mixer, pin);
2784 err = new_analog_input(spec, pin,
2792 for (c = 0; c < num_adcs; c++) {
2793 hda_nid_t cap = get_capsrc(spec, c);
2794 idx = get_connection_index(codec, cap, pin);
2796 spec->imux_pins[imux->num_items] = pin;
2797 snd_hda_add_imux_item(imux, label, idx, NULL);
2803 spec->num_mux_defs = 1;
2804 spec->input_mux = imux;
2809 static void alc_set_pin_output(struct hda_codec *codec, hda_nid_t nid,
2810 unsigned int pin_type)
2812 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2815 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2816 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2820 static int get_pin_type(int line_out_type)
2822 if (line_out_type == AUTO_PIN_HP_OUT)
2828 static void alc_auto_init_analog_input(struct hda_codec *codec)
2830 struct alc_spec *spec = codec->spec;
2831 struct auto_pin_cfg *cfg = &spec->autocfg;
2834 for (i = 0; i < cfg->num_inputs; i++) {
2835 hda_nid_t nid = cfg->inputs[i].pin;
2836 if (alc_is_input_pin(codec, nid)) {
2837 alc_set_input_pin(codec, nid, cfg->inputs[i].type);
2838 if (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP)
2839 snd_hda_codec_write(codec, nid, 0,
2840 AC_VERB_SET_AMP_GAIN_MUTE,
2845 /* mute all loopback inputs */
2846 if (spec->mixer_nid) {
2847 int nums = snd_hda_get_conn_list(codec, spec->mixer_nid, NULL);
2848 for (i = 0; i < nums; i++)
2849 snd_hda_codec_write(codec, spec->mixer_nid, 0,
2850 AC_VERB_SET_AMP_GAIN_MUTE,
2855 /* convert from MIX nid to DAC */
2856 static hda_nid_t alc_auto_mix_to_dac(struct hda_codec *codec, hda_nid_t nid)
2861 if (get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_AUD_OUT)
2863 num = snd_hda_get_connections(codec, nid, list, ARRAY_SIZE(list));
2864 for (i = 0; i < num; i++) {
2865 if (get_wcaps_type(get_wcaps(codec, list[i])) == AC_WID_AUD_OUT)
2871 /* go down to the selector widget before the mixer */
2872 static hda_nid_t alc_go_down_to_selector(struct hda_codec *codec, hda_nid_t pin)
2875 int num = snd_hda_get_connections(codec, pin, srcs,
2878 get_wcaps_type(get_wcaps(codec, srcs[0])) != AC_WID_AUD_SEL)
2883 /* get MIX nid connected to the given pin targeted to DAC */
2884 static hda_nid_t alc_auto_dac_to_mix(struct hda_codec *codec, hda_nid_t pin,
2890 pin = alc_go_down_to_selector(codec, pin);
2891 num = snd_hda_get_connections(codec, pin, mix, ARRAY_SIZE(mix));
2892 for (i = 0; i < num; i++) {
2893 if (alc_auto_mix_to_dac(codec, mix[i]) == dac)
2899 /* select the connection from pin to DAC if needed */
2900 static int alc_auto_select_dac(struct hda_codec *codec, hda_nid_t pin,
2906 pin = alc_go_down_to_selector(codec, pin);
2907 num = snd_hda_get_connections(codec, pin, mix, ARRAY_SIZE(mix));
2910 for (i = 0; i < num; i++) {
2911 if (alc_auto_mix_to_dac(codec, mix[i]) == dac) {
2912 snd_hda_codec_update_cache(codec, pin, 0,
2913 AC_VERB_SET_CONNECT_SEL, i);
2920 /* look for an empty DAC slot */
2921 static hda_nid_t alc_auto_look_for_dac(struct hda_codec *codec, hda_nid_t pin)
2923 struct alc_spec *spec = codec->spec;
2927 pin = alc_go_down_to_selector(codec, pin);
2928 num = snd_hda_get_connections(codec, pin, srcs, ARRAY_SIZE(srcs));
2929 for (i = 0; i < num; i++) {
2930 hda_nid_t nid = alc_auto_mix_to_dac(codec, srcs[i]);
2933 if (found_in_nid_list(nid, spec->multiout.dac_nids,
2934 ARRAY_SIZE(spec->private_dac_nids)))
2936 if (found_in_nid_list(nid, spec->multiout.hp_out_nid,
2937 ARRAY_SIZE(spec->multiout.hp_out_nid)))
2939 if (found_in_nid_list(nid, spec->multiout.extra_out_nid,
2940 ARRAY_SIZE(spec->multiout.extra_out_nid)))
2947 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
2949 hda_nid_t sel = alc_go_down_to_selector(codec, pin);
2950 if (snd_hda_get_conn_list(codec, sel, NULL) == 1)
2951 return alc_auto_look_for_dac(codec, pin);
2955 /* return 0 if no possible DAC is found, 1 if one or more found */
2956 static int alc_auto_fill_extra_dacs(struct hda_codec *codec, int num_outs,
2957 const hda_nid_t *pins, hda_nid_t *dacs)
2961 if (num_outs && !dacs[0]) {
2962 dacs[0] = alc_auto_look_for_dac(codec, pins[0]);
2967 for (i = 1; i < num_outs; i++)
2968 dacs[i] = get_dac_if_single(codec, pins[i]);
2969 for (i = 1; i < num_outs; i++) {
2971 dacs[i] = alc_auto_look_for_dac(codec, pins[i]);
2976 static int alc_auto_fill_multi_ios(struct hda_codec *codec,
2977 unsigned int location);
2979 /* fill in the dac_nids table from the parsed pin configuration */
2980 static int alc_auto_fill_dac_nids(struct hda_codec *codec)
2982 struct alc_spec *spec = codec->spec;
2983 struct auto_pin_cfg *cfg = &spec->autocfg;
2984 bool redone = false;
2988 /* set num_dacs once to full for alc_auto_look_for_dac() */
2989 spec->multiout.num_dacs = cfg->line_outs;
2990 spec->multiout.hp_out_nid[0] = 0;
2991 spec->multiout.extra_out_nid[0] = 0;
2992 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
2993 spec->multiout.dac_nids = spec->private_dac_nids;
2994 spec->multi_ios = 0;
2996 /* fill hard-wired DACs first */
2998 for (i = 0; i < cfg->line_outs; i++)
2999 spec->private_dac_nids[i] =
3000 get_dac_if_single(codec, cfg->line_out_pins[i]);
3002 spec->multiout.hp_out_nid[0] =
3003 get_dac_if_single(codec, cfg->hp_pins[0]);
3004 if (cfg->speaker_outs)
3005 spec->multiout.extra_out_nid[0] =
3006 get_dac_if_single(codec, cfg->speaker_pins[0]);
3009 for (i = 0; i < cfg->line_outs; i++) {
3010 hda_nid_t pin = cfg->line_out_pins[i];
3011 if (spec->private_dac_nids[i])
3013 spec->private_dac_nids[i] = alc_auto_look_for_dac(codec, pin);
3014 if (!spec->private_dac_nids[i] && !redone) {
3015 /* if we can't find primary DACs, re-probe without
3016 * checking the hard-wired DACs
3023 /* re-count num_dacs and squash invalid entries */