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 */
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() */
89 const struct hda_verb *init_verbs[10]; /* initialization verbs
93 unsigned int num_init_verbs;
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;
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;
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
110 hda_nid_t alt_dac_nid;
111 hda_nid_t slave_dig_outs[3]; /* optional - for auto-parsing */
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);
123 /* capture setup for dynamic dual-adc switch */
125 unsigned int cur_adc_stream_tag;
126 unsigned int cur_adc_format;
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;
137 const struct hda_channel_mode *channel_mode;
138 int num_channel_mode;
140 int const_channel_count;
141 int ext_channel_count;
143 /* PCM information */
144 struct hda_pcm pcm_rec[3]; /* used in alc_build_pcms() */
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 */
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);
164 void (*shutup)(struct hda_codec *codec);
165 void (*automute_hook)(struct hda_codec *codec);
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 */
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() */
187 /* auto-mute control */
189 hda_nid_t automute_mixer_nid[AUTO_CFG_MAX_OUTS];
192 int codec_variant; /* flag for other variants */
194 /* for virtual master */
195 hda_nid_t vmaster_nid;
196 #ifdef CONFIG_SND_HDA_POWER_SAVE
197 struct hda_loopback_check loopback;
202 unsigned int pll_coef_idx, pll_coef_bit;
207 const struct alc_fixup *fixup_list;
208 const char *fixup_name;
212 struct alc_multi_io multi_io[4];
215 struct snd_array bind_ctls;
218 #define ALC_MODEL_AUTO 0 /* common for all chips */
220 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
221 int dir, unsigned int bits)
225 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
226 if (query_amp_caps(codec, nid, dir) & bits)
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)
239 static int alc_mux_enum_info(struct snd_kcontrol *kcontrol,
240 struct snd_ctl_elem_info *uinfo)
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)
247 if (!spec->input_mux[mux_idx].num_items && mux_idx > 0)
249 return snd_hda_input_mux_info(&spec->input_mux[mux_idx], uinfo);
252 static int alc_mux_enum_get(struct snd_kcontrol *kcontrol,
253 struct snd_ctl_elem_value *ucontrol)
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);
259 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
263 static bool alc_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
265 struct alc_spec *spec = codec->spec;
266 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
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);
280 /* select the given imux item; either unmute exclusively or select the route */
281 static int alc_mux_select(struct hda_codec *codec, unsigned int adc_idx,
282 unsigned int idx, bool force)
284 struct alc_spec *spec = codec->spec;
285 const struct hda_input_mux *imux;
286 unsigned int mux_idx;
287 int i, type, num_conns;
290 mux_idx = adc_idx >= spec->num_mux_defs ? 0 : adc_idx;
291 imux = &spec->input_mux[mux_idx];
292 if (!imux->num_items && mux_idx > 0)
293 imux = &spec->input_mux[0];
295 if (idx >= imux->num_items)
296 idx = imux->num_items - 1;
297 if (spec->cur_mux[adc_idx] == idx && !force)
299 spec->cur_mux[adc_idx] = idx;
301 if (spec->dyn_adc_switch) {
302 alc_dyn_adc_pcm_resetup(codec, idx);
303 adc_idx = spec->dyn_adc_idx[idx];
306 nid = spec->capsrc_nids ?
307 spec->capsrc_nids[adc_idx] : spec->adc_nids[adc_idx];
310 num_conns = snd_hda_get_conn_list(codec, nid, NULL);
314 type = get_wcaps_type(get_wcaps(codec, nid));
315 if (type == AC_WID_AUD_MIX) {
316 /* Matrix-mixer style (e.g. ALC882) */
317 int active = imux->items[idx].index;
318 for (i = 0; i < num_conns; i++) {
319 unsigned int v = (i == active) ? 0 : HDA_AMP_MUTE;
320 snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, i,
324 /* MUX style (e.g. ALC880) */
325 snd_hda_codec_write_cache(codec, nid, 0,
326 AC_VERB_SET_CONNECT_SEL,
327 imux->items[idx].index);
332 static int alc_mux_enum_put(struct snd_kcontrol *kcontrol,
333 struct snd_ctl_elem_value *ucontrol)
335 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
336 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
337 return alc_mux_select(codec, adc_idx,
338 ucontrol->value.enumerated.item[0], false);
342 * set up the input pin config (depending on the given auto-pin type)
344 static void alc_set_input_pin(struct hda_codec *codec, hda_nid_t nid,
347 unsigned int val = PIN_IN;
349 if (auto_pin_type == AUTO_PIN_MIC) {
352 oldval = snd_hda_codec_read(codec, nid, 0,
353 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
354 pincap = snd_hda_query_pin_caps(codec, nid);
355 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
356 /* if the default pin setup is vref50, we give it priority */
357 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
359 else if (pincap & AC_PINCAP_VREF_50)
361 else if (pincap & AC_PINCAP_VREF_100)
363 else if (pincap & AC_PINCAP_VREF_GRD)
366 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, val);
370 * Append the given mixer and verb elements for the later use
371 * The mixer array is referred in build_controls(), and init_verbs are
374 static void add_mixer(struct alc_spec *spec, const struct snd_kcontrol_new *mix)
376 if (snd_BUG_ON(spec->num_mixers >= ARRAY_SIZE(spec->mixers)))
378 spec->mixers[spec->num_mixers++] = mix;
381 static void add_verb(struct alc_spec *spec, const struct hda_verb *verb)
383 if (snd_BUG_ON(spec->num_init_verbs >= ARRAY_SIZE(spec->init_verbs)))
385 spec->init_verbs[spec->num_init_verbs++] = verb;
389 * GPIO setup tables, used in initialization
391 /* Enable GPIO mask and set output */
392 static const struct hda_verb alc_gpio1_init_verbs[] = {
393 {0x01, AC_VERB_SET_GPIO_MASK, 0x01},
394 {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01},
395 {0x01, AC_VERB_SET_GPIO_DATA, 0x01},
399 static const struct hda_verb alc_gpio2_init_verbs[] = {
400 {0x01, AC_VERB_SET_GPIO_MASK, 0x02},
401 {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x02},
402 {0x01, AC_VERB_SET_GPIO_DATA, 0x02},
406 static const struct hda_verb alc_gpio3_init_verbs[] = {
407 {0x01, AC_VERB_SET_GPIO_MASK, 0x03},
408 {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x03},
409 {0x01, AC_VERB_SET_GPIO_DATA, 0x03},
414 * Fix hardware PLL issue
415 * On some codecs, the analog PLL gating control must be off while
416 * the default value is 1.
418 static void alc_fix_pll(struct hda_codec *codec)
420 struct alc_spec *spec = codec->spec;
425 snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
427 val = snd_hda_codec_read(codec, spec->pll_nid, 0,
428 AC_VERB_GET_PROC_COEF, 0);
429 snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_COEF_INDEX,
431 snd_hda_codec_write(codec, spec->pll_nid, 0, AC_VERB_SET_PROC_COEF,
432 val & ~(1 << spec->pll_coef_bit));
435 static void alc_fix_pll_init(struct hda_codec *codec, hda_nid_t nid,
436 unsigned int coef_idx, unsigned int coef_bit)
438 struct alc_spec *spec = codec->spec;
440 spec->pll_coef_idx = coef_idx;
441 spec->pll_coef_bit = coef_bit;
446 * Jack-reporting via input-jack layer
449 /* initialization of jacks; currently checks only a few known pins */
450 static int alc_init_jacks(struct hda_codec *codec)
452 #ifdef CONFIG_SND_HDA_INPUT_JACK
453 struct alc_spec *spec = codec->spec;
455 unsigned int hp_nid = spec->autocfg.hp_pins[0];
456 unsigned int mic_nid = spec->ext_mic_pin;
457 unsigned int dock_nid = spec->dock_mic_pin;
460 err = snd_hda_input_jack_add(codec, hp_nid,
461 SND_JACK_HEADPHONE, NULL);
464 snd_hda_input_jack_report(codec, hp_nid);
468 err = snd_hda_input_jack_add(codec, mic_nid,
469 SND_JACK_MICROPHONE, NULL);
472 snd_hda_input_jack_report(codec, mic_nid);
475 err = snd_hda_input_jack_add(codec, dock_nid,
476 SND_JACK_MICROPHONE, NULL);
479 snd_hda_input_jack_report(codec, dock_nid);
481 #endif /* CONFIG_SND_HDA_INPUT_JACK */
486 * Jack detections for HP auto-mute and mic-switch
489 /* check each pin in the given array; returns true if any of them is plugged */
490 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
494 for (i = 0; i < num_pins; i++) {
495 hda_nid_t nid = pins[i];
498 snd_hda_input_jack_report(codec, nid);
499 present |= snd_hda_jack_detect(codec, nid);
504 /* standard HP/line-out auto-mute helper */
505 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
506 bool mute, bool hp_out)
508 struct alc_spec *spec = codec->spec;
509 unsigned int mute_bits = mute ? HDA_AMP_MUTE : 0;
510 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
513 for (i = 0; i < num_pins; i++) {
514 hda_nid_t nid = pins[i];
517 switch (spec->automute_mode) {
518 case ALC_AUTOMUTE_PIN:
519 snd_hda_codec_write(codec, nid, 0,
520 AC_VERB_SET_PIN_WIDGET_CONTROL,
523 case ALC_AUTOMUTE_AMP:
524 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
525 HDA_AMP_MUTE, mute_bits);
527 case ALC_AUTOMUTE_MIXER:
528 nid = spec->automute_mixer_nid[i];
531 snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 0,
532 HDA_AMP_MUTE, mute_bits);
533 snd_hda_codec_amp_stereo(codec, nid, HDA_INPUT, 1,
534 HDA_AMP_MUTE, mute_bits);
540 /* Toggle outputs muting */
541 static void update_outputs(struct hda_codec *codec)
543 struct alc_spec *spec = codec->spec;
546 /* Control HP pins/amps depending on master_mute state;
547 * in general, HP pins/amps control should be enabled in all cases,
548 * but currently set only for master_mute, just to be safe
550 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
551 spec->autocfg.hp_pins, spec->master_mute, true);
553 if (!spec->automute_speaker)
556 on = spec->hp_jack_present | spec->line_jack_present;
557 on |= spec->master_mute;
558 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
559 spec->autocfg.speaker_pins, on, false);
561 /* toggle line-out mutes if needed, too */
562 /* if LO is a copy of either HP or Speaker, don't need to handle it */
563 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
564 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
566 if (!spec->automute_lo)
569 on = spec->hp_jack_present;
570 on |= spec->master_mute;
571 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
572 spec->autocfg.line_out_pins, on, false);
575 static void call_update_outputs(struct hda_codec *codec)
577 struct alc_spec *spec = codec->spec;
578 if (spec->automute_hook)
579 spec->automute_hook(codec);
581 update_outputs(codec);
584 /* standard HP-automute helper */
585 static void alc_hp_automute(struct hda_codec *codec)
587 struct alc_spec *spec = codec->spec;
589 spec->hp_jack_present =
590 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
591 spec->autocfg.hp_pins);
592 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
594 call_update_outputs(codec);
597 /* standard line-out-automute helper */
598 static void alc_line_automute(struct hda_codec *codec)
600 struct alc_spec *spec = codec->spec;
602 /* check LO jack only when it's different from HP */
603 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
606 spec->line_jack_present =
607 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
608 spec->autocfg.line_out_pins);
609 if (!spec->automute_speaker || !spec->detect_lo)
611 call_update_outputs(codec);
614 #define get_connection_index(codec, mux, nid) \
615 snd_hda_get_conn_index(codec, mux, nid, 0)
617 /* standard mic auto-switch helper */
618 static void alc_mic_automute(struct hda_codec *codec)
620 struct alc_spec *spec = codec->spec;
621 hda_nid_t *pins = spec->imux_pins;
623 if (!spec->auto_mic || !spec->auto_mic_valid_imux)
625 if (snd_BUG_ON(!spec->adc_nids))
627 if (snd_BUG_ON(spec->int_mic_idx < 0 || spec->ext_mic_idx < 0))
630 if (snd_hda_jack_detect(codec, pins[spec->ext_mic_idx]))
631 alc_mux_select(codec, 0, spec->ext_mic_idx, false);
632 else if (spec->dock_mic_idx >= 0 &&
633 snd_hda_jack_detect(codec, pins[spec->dock_mic_idx]))
634 alc_mux_select(codec, 0, spec->dock_mic_idx, false);
636 alc_mux_select(codec, 0, spec->int_mic_idx, false);
638 snd_hda_input_jack_report(codec, pins[spec->ext_mic_idx]);
639 if (spec->dock_mic_idx >= 0)
640 snd_hda_input_jack_report(codec, pins[spec->dock_mic_idx]);
643 /* unsolicited event for HP jack sensing */
644 static void alc_sku_unsol_event(struct hda_codec *codec, unsigned int res)
646 if (codec->vendor_id == 0x10ec0880)
652 alc_hp_automute(codec);
654 case ALC_FRONT_EVENT:
655 alc_line_automute(codec);
658 alc_mic_automute(codec);
663 /* call init functions of standard auto-mute helpers */
664 static void alc_inithook(struct hda_codec *codec)
666 alc_hp_automute(codec);
667 alc_line_automute(codec);
668 alc_mic_automute(codec);
671 /* additional initialization for ALC888 variants */
672 static void alc888_coef_init(struct hda_codec *codec)
676 snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 0);
677 tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
678 snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
679 if ((tmp & 0xf0) == 0x20)
681 snd_hda_codec_read(codec, 0x20, 0,
682 AC_VERB_SET_PROC_COEF, 0x830);
685 snd_hda_codec_read(codec, 0x20, 0,
686 AC_VERB_SET_PROC_COEF, 0x3030);
689 /* additional initialization for ALC889 variants */
690 static void alc889_coef_init(struct hda_codec *codec)
694 snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
695 tmp = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_PROC_COEF, 0);
696 snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX, 7);
697 snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_PROC_COEF, tmp|0x2010);
700 /* turn on/off EAPD control (only if available) */
701 static void set_eapd(struct hda_codec *codec, hda_nid_t nid, int on)
703 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
705 if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
706 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_EAPD_BTLENABLE,
710 /* turn on/off EAPD controls of the codec */
711 static void alc_auto_setup_eapd(struct hda_codec *codec, bool on)
713 /* We currently only handle front, HP */
714 static hda_nid_t pins[] = {
715 0x0f, 0x10, 0x14, 0x15, 0
718 for (p = pins; *p; p++)
719 set_eapd(codec, *p, on);
722 /* generic shutup callback;
723 * just turning off EPAD and a little pause for avoiding pop-noise
725 static void alc_eapd_shutup(struct hda_codec *codec)
727 alc_auto_setup_eapd(codec, false);
731 /* generic EAPD initialization */
732 static void alc_auto_init_amp(struct hda_codec *codec, int type)
736 alc_auto_setup_eapd(codec, true);
739 snd_hda_sequence_write(codec, alc_gpio1_init_verbs);
742 snd_hda_sequence_write(codec, alc_gpio2_init_verbs);
745 snd_hda_sequence_write(codec, alc_gpio3_init_verbs);
747 case ALC_INIT_DEFAULT:
748 switch (codec->vendor_id) {
750 snd_hda_codec_write(codec, 0x1a, 0,
751 AC_VERB_SET_COEF_INDEX, 7);
752 tmp = snd_hda_codec_read(codec, 0x1a, 0,
753 AC_VERB_GET_PROC_COEF, 0);
754 snd_hda_codec_write(codec, 0x1a, 0,
755 AC_VERB_SET_COEF_INDEX, 7);
756 snd_hda_codec_write(codec, 0x1a, 0,
757 AC_VERB_SET_PROC_COEF,
766 /*case 0x10ec0889:*/ /* this causes an SPDIF problem */
767 alc889_coef_init(codec);
770 alc888_coef_init(codec);
772 #if 0 /* XXX: This may cause the silent output on speaker on some machines */
775 snd_hda_codec_write(codec, 0x20, 0,
776 AC_VERB_SET_COEF_INDEX, 7);
777 tmp = snd_hda_codec_read(codec, 0x20, 0,
778 AC_VERB_GET_PROC_COEF, 0);
779 snd_hda_codec_write(codec, 0x20, 0,
780 AC_VERB_SET_COEF_INDEX, 7);
781 snd_hda_codec_write(codec, 0x20, 0,
782 AC_VERB_SET_PROC_COEF,
792 * Auto-Mute mode mixer enum support
794 static int alc_automute_mode_info(struct snd_kcontrol *kcontrol,
795 struct snd_ctl_elem_info *uinfo)
797 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
798 struct alc_spec *spec = codec->spec;
799 static const char * const texts2[] = {
800 "Disabled", "Enabled"
802 static const char * const texts3[] = {
803 "Disabled", "Speaker Only", "Line-Out+Speaker"
805 const char * const *texts;
807 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
809 if (spec->automute_speaker_possible && spec->automute_lo_possible) {
810 uinfo->value.enumerated.items = 3;
813 uinfo->value.enumerated.items = 2;
816 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
817 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
818 strcpy(uinfo->value.enumerated.name,
819 texts[uinfo->value.enumerated.item]);
823 static int alc_automute_mode_get(struct snd_kcontrol *kcontrol,
824 struct snd_ctl_elem_value *ucontrol)
826 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
827 struct alc_spec *spec = codec->spec;
828 unsigned int val = 0;
829 if (spec->automute_speaker)
831 if (spec->automute_lo)
834 ucontrol->value.enumerated.item[0] = val;
838 static int alc_automute_mode_put(struct snd_kcontrol *kcontrol,
839 struct snd_ctl_elem_value *ucontrol)
841 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
842 struct alc_spec *spec = codec->spec;
844 switch (ucontrol->value.enumerated.item[0]) {
846 if (!spec->automute_speaker && !spec->automute_lo)
848 spec->automute_speaker = 0;
849 spec->automute_lo = 0;
852 if (spec->automute_speaker_possible) {
853 if (!spec->automute_lo && spec->automute_speaker)
855 spec->automute_speaker = 1;
856 spec->automute_lo = 0;
857 } else if (spec->automute_lo_possible) {
858 if (spec->automute_lo)
860 spec->automute_lo = 1;
865 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
867 if (spec->automute_speaker && spec->automute_lo)
869 spec->automute_speaker = 1;
870 spec->automute_lo = 1;
875 call_update_outputs(codec);
879 static const struct snd_kcontrol_new alc_automute_mode_enum = {
880 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
881 .name = "Auto-Mute Mode",
882 .info = alc_automute_mode_info,
883 .get = alc_automute_mode_get,
884 .put = alc_automute_mode_put,
887 static struct snd_kcontrol_new *alc_kcontrol_new(struct alc_spec *spec)
889 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
890 return snd_array_new(&spec->kctls);
893 static int alc_add_automute_mode_enum(struct hda_codec *codec)
895 struct alc_spec *spec = codec->spec;
896 struct snd_kcontrol_new *knew;
898 knew = alc_kcontrol_new(spec);
901 *knew = alc_automute_mode_enum;
902 knew->name = kstrdup("Auto-Mute Mode", GFP_KERNEL);
909 * Check the availability of HP/line-out auto-mute;
910 * Set up appropriately if really supported
912 static void alc_init_automute(struct hda_codec *codec)
914 struct alc_spec *spec = codec->spec;
915 struct auto_pin_cfg *cfg = &spec->autocfg;
921 if (cfg->line_out_pins[0])
923 if (cfg->speaker_pins[0])
925 if (present < 2) /* need two different output types */
928 if (!cfg->speaker_pins[0] &&
929 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
930 memcpy(cfg->speaker_pins, cfg->line_out_pins,
931 sizeof(cfg->speaker_pins));
932 cfg->speaker_outs = cfg->line_outs;
935 if (!cfg->hp_pins[0] &&
936 cfg->line_out_type == AUTO_PIN_HP_OUT) {
937 memcpy(cfg->hp_pins, cfg->line_out_pins,
938 sizeof(cfg->hp_pins));
939 cfg->hp_outs = cfg->line_outs;
942 spec->automute_mode = ALC_AUTOMUTE_PIN;
944 for (i = 0; i < cfg->hp_outs; i++) {
945 hda_nid_t nid = cfg->hp_pins[i];
946 if (!is_jack_detectable(codec, nid))
948 snd_printdd("realtek: Enable HP auto-muting on NID 0x%x\n",
950 snd_hda_codec_write_cache(codec, nid, 0,
951 AC_VERB_SET_UNSOLICITED_ENABLE,
952 AC_USRSP_EN | ALC_HP_EVENT);
956 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
957 if (cfg->speaker_outs)
958 for (i = 0; i < cfg->line_outs; i++) {
959 hda_nid_t nid = cfg->line_out_pins[i];
960 if (!is_jack_detectable(codec, nid))
962 snd_printdd("realtek: Enable Line-Out "
963 "auto-muting on NID 0x%x\n", nid);
964 snd_hda_codec_write_cache(codec, nid, 0,
965 AC_VERB_SET_UNSOLICITED_ENABLE,
966 AC_USRSP_EN | ALC_FRONT_EVENT);
969 spec->automute_lo_possible = spec->detect_hp;
972 spec->automute_speaker_possible = cfg->speaker_outs &&
973 (spec->detect_hp || spec->detect_lo);
975 spec->automute_lo = spec->automute_lo_possible;
976 spec->automute_speaker = spec->automute_speaker_possible;
978 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
979 /* create a control for automute mode */
980 alc_add_automute_mode_enum(codec);
981 spec->unsol_event = alc_sku_unsol_event;
985 /* return the position of NID in the list, or -1 if not found */
986 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
989 for (i = 0; i < nums; i++)
995 /* check whether dynamic ADC-switching is available */
996 static bool alc_check_dyn_adc_switch(struct hda_codec *codec)
998 struct alc_spec *spec = codec->spec;
999 struct hda_input_mux *imux = &spec->private_imux[0];
1003 if (imux != spec->input_mux) /* no dynamic imux? */
1006 for (n = 0; n < spec->num_adc_nids; n++) {
1007 cap = spec->private_capsrc_nids[n];
1008 for (i = 0; i < imux->num_items; i++) {
1009 pin = spec->imux_pins[i];
1012 if (get_connection_index(codec, cap, pin) < 0)
1015 if (i >= imux->num_items)
1016 return true; /* no ADC-switch is needed */
1019 for (i = 0; i < imux->num_items; i++) {
1020 pin = spec->imux_pins[i];
1021 for (n = 0; n < spec->num_adc_nids; n++) {
1022 cap = spec->private_capsrc_nids[n];
1023 idx = get_connection_index(codec, cap, pin);
1025 imux->items[i].index = idx;
1026 spec->dyn_adc_idx[i] = n;
1032 snd_printdd("realtek: enabling ADC switching\n");
1033 spec->dyn_adc_switch = 1;
1037 /* rebuild imux for matching with the given auto-mic pins (if not yet) */
1038 static bool alc_rebuild_imux_for_auto_mic(struct hda_codec *codec)
1040 struct alc_spec *spec = codec->spec;
1041 struct hda_input_mux *imux;
1042 static char * const texts[3] = {
1043 "Mic", "Internal Mic", "Dock Mic"
1047 if (!spec->auto_mic)
1049 imux = &spec->private_imux[0];
1050 if (spec->input_mux == imux)
1052 spec->imux_pins[0] = spec->ext_mic_pin;
1053 spec->imux_pins[1] = spec->int_mic_pin;
1054 spec->imux_pins[2] = spec->dock_mic_pin;
1055 for (i = 0; i < 3; i++) {
1056 strcpy(imux->items[i].label, texts[i]);
1057 if (spec->imux_pins[i])
1058 imux->num_items = i + 1;
1060 spec->num_mux_defs = 1;
1061 spec->input_mux = imux;
1065 /* check whether all auto-mic pins are valid; setup indices if OK */
1066 static bool alc_auto_mic_check_imux(struct hda_codec *codec)
1068 struct alc_spec *spec = codec->spec;
1069 const struct hda_input_mux *imux;
1071 if (!spec->auto_mic)
1073 if (spec->auto_mic_valid_imux)
1074 return true; /* already checked */
1076 /* fill up imux indices */
1077 if (!alc_check_dyn_adc_switch(codec)) {
1082 imux = spec->input_mux;
1083 spec->ext_mic_idx = find_idx_in_nid_list(spec->ext_mic_pin,
1084 spec->imux_pins, imux->num_items);
1085 spec->int_mic_idx = find_idx_in_nid_list(spec->int_mic_pin,
1086 spec->imux_pins, imux->num_items);
1087 spec->dock_mic_idx = find_idx_in_nid_list(spec->dock_mic_pin,
1088 spec->imux_pins, imux->num_items);
1089 if (spec->ext_mic_idx < 0 || spec->int_mic_idx < 0) {
1091 return false; /* no corresponding imux */
1094 snd_hda_codec_write_cache(codec, spec->ext_mic_pin, 0,
1095 AC_VERB_SET_UNSOLICITED_ENABLE,
1096 AC_USRSP_EN | ALC_MIC_EVENT);
1097 if (spec->dock_mic_pin)
1098 snd_hda_codec_write_cache(codec, spec->dock_mic_pin, 0,
1099 AC_VERB_SET_UNSOLICITED_ENABLE,
1100 AC_USRSP_EN | ALC_MIC_EVENT);
1102 spec->auto_mic_valid_imux = 1;
1108 * Check the availability of auto-mic switch;
1109 * Set up if really supported
1111 static void alc_init_auto_mic(struct hda_codec *codec)
1113 struct alc_spec *spec = codec->spec;
1114 struct auto_pin_cfg *cfg = &spec->autocfg;
1115 hda_nid_t fixed, ext, dock;
1118 spec->ext_mic_idx = spec->int_mic_idx = spec->dock_mic_idx = -1;
1120 fixed = ext = dock = 0;
1121 for (i = 0; i < cfg->num_inputs; i++) {
1122 hda_nid_t nid = cfg->inputs[i].pin;
1123 unsigned int defcfg;
1124 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1125 switch (snd_hda_get_input_pin_attr(defcfg)) {
1126 case INPUT_PIN_ATTR_INT:
1128 return; /* already occupied */
1129 if (cfg->inputs[i].type != AUTO_PIN_MIC)
1130 return; /* invalid type */
1133 case INPUT_PIN_ATTR_UNUSED:
1134 return; /* invalid entry */
1135 case INPUT_PIN_ATTR_DOCK:
1137 return; /* already occupied */
1138 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
1139 return; /* invalid type */
1144 return; /* already occupied */
1145 if (cfg->inputs[i].type != AUTO_PIN_MIC)
1146 return; /* invalid type */
1157 if (!is_jack_detectable(codec, ext))
1158 return; /* no unsol support */
1159 if (dock && !is_jack_detectable(codec, dock))
1160 return; /* no unsol support */
1162 /* check imux indices */
1163 spec->ext_mic_pin = ext;
1164 spec->int_mic_pin = fixed;
1165 spec->dock_mic_pin = dock;
1168 if (!alc_auto_mic_check_imux(codec))
1171 snd_printdd("realtek: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
1173 spec->unsol_event = alc_sku_unsol_event;
1176 /* check the availabilities of auto-mute and auto-mic switches */
1177 static void alc_auto_check_switches(struct hda_codec *codec)
1179 alc_init_automute(codec);
1180 alc_init_auto_mic(codec);
1184 * Realtek SSID verification
1187 /* Could be any non-zero and even value. When used as fixup, tells
1188 * the driver to ignore any present sku defines.
1190 #define ALC_FIXUP_SKU_IGNORE (2)
1192 static int alc_auto_parse_customize_define(struct hda_codec *codec)
1194 unsigned int ass, tmp, i;
1196 struct alc_spec *spec = codec->spec;
1198 spec->cdefine.enable_pcbeep = 1; /* assume always enabled */
1200 if (spec->cdefine.fixup) {
1201 ass = spec->cdefine.sku_cfg;
1202 if (ass == ALC_FIXUP_SKU_IGNORE)
1207 ass = codec->subsystem_id & 0xffff;
1208 if (ass != codec->bus->pci->subsystem_device && (ass & 1))
1212 if (codec->vendor_id == 0x10ec0260)
1214 ass = snd_hda_codec_get_pincfg(codec, nid);
1217 printk(KERN_INFO "hda_codec: %s: SKU not ready 0x%08x\n",
1218 codec->chip_name, ass);
1224 for (i = 1; i < 16; i++) {
1228 if (((ass >> 16) & 0xf) != tmp)
1231 spec->cdefine.port_connectivity = ass >> 30;
1232 spec->cdefine.enable_pcbeep = (ass & 0x100000) >> 20;
1233 spec->cdefine.check_sum = (ass >> 16) & 0xf;
1234 spec->cdefine.customization = ass >> 8;
1236 spec->cdefine.sku_cfg = ass;
1237 spec->cdefine.external_amp = (ass & 0x38) >> 3;
1238 spec->cdefine.platform_type = (ass & 0x4) >> 2;
1239 spec->cdefine.swap = (ass & 0x2) >> 1;
1240 spec->cdefine.override = ass & 0x1;
1242 snd_printd("SKU: Nid=0x%x sku_cfg=0x%08x\n",
1243 nid, spec->cdefine.sku_cfg);
1244 snd_printd("SKU: port_connectivity=0x%x\n",
1245 spec->cdefine.port_connectivity);
1246 snd_printd("SKU: enable_pcbeep=0x%x\n", spec->cdefine.enable_pcbeep);
1247 snd_printd("SKU: check_sum=0x%08x\n", spec->cdefine.check_sum);
1248 snd_printd("SKU: customization=0x%08x\n", spec->cdefine.customization);
1249 snd_printd("SKU: external_amp=0x%x\n", spec->cdefine.external_amp);
1250 snd_printd("SKU: platform_type=0x%x\n", spec->cdefine.platform_type);
1251 snd_printd("SKU: swap=0x%x\n", spec->cdefine.swap);
1252 snd_printd("SKU: override=0x%x\n", spec->cdefine.override);
1257 /* return true if the given NID is found in the list */
1258 static bool found_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
1260 return find_idx_in_nid_list(nid, list, nums) >= 0;
1263 /* check subsystem ID and set up device-specific initialization;
1264 * return 1 if initialized, 0 if invalid SSID
1266 /* 32-bit subsystem ID for BIOS loading in HD Audio codec.
1267 * 31 ~ 16 : Manufacture ID
1269 * 7 ~ 0 : Assembly ID
1270 * port-A --> pin 39/41, port-E --> pin 14/15, port-D --> pin 35/36
1272 static int alc_subsystem_id(struct hda_codec *codec,
1273 hda_nid_t porta, hda_nid_t porte,
1274 hda_nid_t portd, hda_nid_t porti)
1276 unsigned int ass, tmp, i;
1278 struct alc_spec *spec = codec->spec;
1280 if (spec->cdefine.fixup) {
1281 ass = spec->cdefine.sku_cfg;
1282 if (ass == ALC_FIXUP_SKU_IGNORE)
1287 ass = codec->subsystem_id & 0xffff;
1288 if ((ass != codec->bus->pci->subsystem_device) && (ass & 1))
1291 /* invalid SSID, check the special NID pin defcfg instead */
1293 * 31~30 : port connectivity
1296 * 19~16 : Check sum (15:1)
1301 if (codec->vendor_id == 0x10ec0260)
1303 ass = snd_hda_codec_get_pincfg(codec, nid);
1304 snd_printd("realtek: No valid SSID, "
1305 "checking pincfg 0x%08x for NID 0x%x\n",
1309 if ((ass >> 30) != 1) /* no physical connection */
1314 for (i = 1; i < 16; i++) {
1318 if (((ass >> 16) & 0xf) != tmp)
1321 snd_printd("realtek: Enabling init ASM_ID=0x%04x CODEC_ID=%08x\n",
1322 ass & 0xffff, codec->vendor_id);
1326 * 2 : 0 --> Desktop, 1 --> Laptop
1327 * 3~5 : External Amplifier control
1330 tmp = (ass & 0x38) >> 3; /* external Amp control */
1333 spec->init_amp = ALC_INIT_GPIO1;
1336 spec->init_amp = ALC_INIT_GPIO2;
1339 spec->init_amp = ALC_INIT_GPIO3;
1343 spec->init_amp = ALC_INIT_DEFAULT;
1347 /* is laptop or Desktop and enable the function "Mute internal speaker
1348 * when the external headphone out jack is plugged"
1350 if (!(ass & 0x8000))
1353 * 10~8 : Jack location
1354 * 12~11: Headphone out -> 00: PortA, 01: PortE, 02: PortD, 03: Resvered
1356 * 15 : 1 --> enable the function "Mute internal speaker
1357 * when the external headphone out jack is plugged"
1359 if (!spec->autocfg.hp_pins[0] &&
1360 !(spec->autocfg.line_out_pins[0] &&
1361 spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)) {
1363 tmp = (ass >> 11) & 0x3; /* HP to chassis */
1374 if (found_in_nid_list(nid, spec->autocfg.line_out_pins,
1375 spec->autocfg.line_outs))
1377 spec->autocfg.hp_pins[0] = nid;
1382 /* Check the validity of ALC subsystem-id
1383 * ports contains an array of 4 pin NIDs for port-A, E, D and I */
1384 static void alc_ssid_check(struct hda_codec *codec, const hda_nid_t *ports)
1386 if (!alc_subsystem_id(codec, ports[0], ports[1], ports[2], ports[3])) {
1387 struct alc_spec *spec = codec->spec;
1388 snd_printd("realtek: "
1389 "Enable default setup for auto mode as fallback\n");
1390 spec->init_amp = ALC_INIT_DEFAULT;
1395 * Fix-up pin default configurations and add default verbs
1403 struct alc_model_fixup {
1414 const struct alc_pincfg *pins;
1415 const struct hda_verb *verbs;
1416 void (*func)(struct hda_codec *codec,
1417 const struct alc_fixup *fix,
1431 ALC_FIXUP_ACT_PRE_PROBE,
1432 ALC_FIXUP_ACT_PROBE,
1436 static void alc_apply_fixup(struct hda_codec *codec, int action)
1438 struct alc_spec *spec = codec->spec;
1439 int id = spec->fixup_id;
1440 #ifdef CONFIG_SND_DEBUG_VERBOSE
1441 const char *modelname = spec->fixup_name;
1445 if (!spec->fixup_list)
1449 const struct alc_fixup *fix = spec->fixup_list + id;
1450 const struct alc_pincfg *cfg;
1452 switch (fix->type) {
1454 if (action != ALC_FIXUP_ACT_PRE_PROBE || !fix->v.sku)
1456 snd_printdd(KERN_INFO "hda_codec: %s: "
1457 "Apply sku override for %s\n",
1458 codec->chip_name, modelname);
1459 spec->cdefine.sku_cfg = fix->v.sku;
1460 spec->cdefine.fixup = 1;
1462 case ALC_FIXUP_PINS:
1464 if (action != ALC_FIXUP_ACT_PRE_PROBE || !cfg)
1466 snd_printdd(KERN_INFO "hda_codec: %s: "
1467 "Apply pincfg for %s\n",
1468 codec->chip_name, modelname);
1469 for (; cfg->nid; cfg++)
1470 snd_hda_codec_set_pincfg(codec, cfg->nid,
1473 case ALC_FIXUP_VERBS:
1474 if (action != ALC_FIXUP_ACT_PROBE || !fix->v.verbs)
1476 snd_printdd(KERN_INFO "hda_codec: %s: "
1477 "Apply fix-verbs for %s\n",
1478 codec->chip_name, modelname);
1479 add_verb(codec->spec, fix->v.verbs);
1481 case ALC_FIXUP_FUNC:
1484 snd_printdd(KERN_INFO "hda_codec: %s: "
1485 "Apply fix-func for %s\n",
1486 codec->chip_name, modelname);
1487 fix->v.func(codec, fix, action);
1490 snd_printk(KERN_ERR "hda_codec: %s: "
1491 "Invalid fixup type %d\n",
1492 codec->chip_name, fix->type);
1503 static void alc_pick_fixup(struct hda_codec *codec,
1504 const struct alc_model_fixup *models,
1505 const struct snd_pci_quirk *quirk,
1506 const struct alc_fixup *fixlist)
1508 struct alc_spec *spec = codec->spec;
1510 const char *name = NULL;
1512 if (codec->modelname && models) {
1513 while (models->name) {
1514 if (!strcmp(codec->modelname, models->name)) {
1516 name = models->name;
1523 quirk = snd_pci_quirk_lookup(codec->bus->pci, quirk);
1526 #ifdef CONFIG_SND_DEBUG_VERBOSE
1532 spec->fixup_id = id;
1534 spec->fixup_list = fixlist;
1535 spec->fixup_name = name;
1540 * COEF access helper functions
1542 static int alc_read_coef_idx(struct hda_codec *codec,
1543 unsigned int coef_idx)
1546 snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX,
1548 val = snd_hda_codec_read(codec, 0x20, 0,
1549 AC_VERB_GET_PROC_COEF, 0);
1553 static void alc_write_coef_idx(struct hda_codec *codec, unsigned int coef_idx,
1554 unsigned int coef_val)
1556 snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_COEF_INDEX,
1558 snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_PROC_COEF,
1562 /* a special bypass for COEF 0; read the cached value at the second time */
1563 static unsigned int alc_get_coef0(struct hda_codec *codec)
1565 struct alc_spec *spec = codec->spec;
1567 spec->coef0 = alc_read_coef_idx(codec, 0);
1572 * Digital I/O handling
1575 /* set right pin controls for digital I/O */
1576 static void alc_auto_init_digital(struct hda_codec *codec)
1578 struct alc_spec *spec = codec->spec;
1582 for (i = 0; i < spec->autocfg.dig_outs; i++) {
1583 pin = spec->autocfg.dig_out_pins[i];
1586 snd_hda_codec_write(codec, pin, 0,
1587 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1589 dac = spec->multiout.dig_out_nid;
1591 dac = spec->slave_dig_outs[i - 1];
1592 if (!dac || !(get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
1594 snd_hda_codec_write(codec, dac, 0,
1595 AC_VERB_SET_AMP_GAIN_MUTE,
1598 pin = spec->autocfg.dig_in_pin;
1600 snd_hda_codec_write(codec, pin, 0,
1601 AC_VERB_SET_PIN_WIDGET_CONTROL,
1605 /* parse digital I/Os and set up NIDs in BIOS auto-parse mode */
1606 static void alc_auto_parse_digital(struct hda_codec *codec)
1608 struct alc_spec *spec = codec->spec;
1612 /* support multiple SPDIFs; the secondary is set up as a slave */
1614 for (i = 0; i < spec->autocfg.dig_outs; i++) {
1616 err = snd_hda_get_connections(codec,
1617 spec->autocfg.dig_out_pins[i],
1618 conn, ARRAY_SIZE(conn));
1621 dig_nid = conn[0]; /* assume the first element is audio-out */
1623 spec->multiout.dig_out_nid = dig_nid;
1624 spec->dig_out_type = spec->autocfg.dig_out_type[0];
1626 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
1627 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
1629 spec->slave_dig_outs[nums - 1] = dig_nid;
1634 if (spec->autocfg.dig_in_pin) {
1635 dig_nid = codec->start_nid;
1636 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
1637 unsigned int wcaps = get_wcaps(codec, dig_nid);
1638 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
1640 if (!(wcaps & AC_WCAP_DIGITAL))
1642 if (!(wcaps & AC_WCAP_CONN_LIST))
1644 err = get_connection_index(codec, dig_nid,
1645 spec->autocfg.dig_in_pin);
1647 spec->dig_in_nid = dig_nid;
1655 * capture mixer elements
1657 static int alc_cap_vol_info(struct snd_kcontrol *kcontrol,
1658 struct snd_ctl_elem_info *uinfo)
1660 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1661 struct alc_spec *spec = codec->spec;
1665 mutex_lock(&codec->control_mutex);
1666 if (spec->vol_in_capsrc)
1667 val = HDA_COMPOSE_AMP_VAL(spec->capsrc_nids[0], 3, 0, HDA_OUTPUT);
1669 val = HDA_COMPOSE_AMP_VAL(spec->adc_nids[0], 3, 0, HDA_INPUT);
1670 kcontrol->private_value = val;
1671 err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
1672 mutex_unlock(&codec->control_mutex);
1676 static int alc_cap_vol_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1677 unsigned int size, unsigned int __user *tlv)
1679 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1680 struct alc_spec *spec = codec->spec;
1684 mutex_lock(&codec->control_mutex);
1685 if (spec->vol_in_capsrc)
1686 val = HDA_COMPOSE_AMP_VAL(spec->capsrc_nids[0], 3, 0, HDA_OUTPUT);
1688 val = HDA_COMPOSE_AMP_VAL(spec->adc_nids[0], 3, 0, HDA_INPUT);
1689 kcontrol->private_value = val;
1690 err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
1691 mutex_unlock(&codec->control_mutex);
1695 typedef int (*getput_call_t)(struct snd_kcontrol *kcontrol,
1696 struct snd_ctl_elem_value *ucontrol);
1698 static int alc_cap_getput_caller(struct snd_kcontrol *kcontrol,
1699 struct snd_ctl_elem_value *ucontrol,
1700 getput_call_t func, bool check_adc_switch)
1702 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1703 struct alc_spec *spec = codec->spec;
1706 mutex_lock(&codec->control_mutex);
1707 if (check_adc_switch && spec->dyn_adc_switch) {
1708 for (i = 0; i < spec->num_adc_nids; i++) {
1709 kcontrol->private_value =
1710 HDA_COMPOSE_AMP_VAL(spec->adc_nids[i],
1712 err = func(kcontrol, ucontrol);
1717 i = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1718 if (spec->vol_in_capsrc)
1719 kcontrol->private_value =
1720 HDA_COMPOSE_AMP_VAL(spec->capsrc_nids[i],
1723 kcontrol->private_value =
1724 HDA_COMPOSE_AMP_VAL(spec->adc_nids[i],
1726 err = func(kcontrol, ucontrol);
1729 mutex_unlock(&codec->control_mutex);
1733 static int alc_cap_vol_get(struct snd_kcontrol *kcontrol,
1734 struct snd_ctl_elem_value *ucontrol)
1736 return alc_cap_getput_caller(kcontrol, ucontrol,
1737 snd_hda_mixer_amp_volume_get, false);
1740 static int alc_cap_vol_put(struct snd_kcontrol *kcontrol,
1741 struct snd_ctl_elem_value *ucontrol)
1743 return alc_cap_getput_caller(kcontrol, ucontrol,
1744 snd_hda_mixer_amp_volume_put, true);
1747 /* capture mixer elements */
1748 #define alc_cap_sw_info snd_ctl_boolean_stereo_info
1750 static int alc_cap_sw_get(struct snd_kcontrol *kcontrol,
1751 struct snd_ctl_elem_value *ucontrol)
1753 return alc_cap_getput_caller(kcontrol, ucontrol,
1754 snd_hda_mixer_amp_switch_get, false);
1757 static int alc_cap_sw_put(struct snd_kcontrol *kcontrol,
1758 struct snd_ctl_elem_value *ucontrol)
1760 return alc_cap_getput_caller(kcontrol, ucontrol,
1761 snd_hda_mixer_amp_switch_put, true);
1764 #define _DEFINE_CAPMIX(num) \
1766 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1767 .name = "Capture Switch", \
1768 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
1770 .info = alc_cap_sw_info, \
1771 .get = alc_cap_sw_get, \
1772 .put = alc_cap_sw_put, \
1775 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1776 .name = "Capture Volume", \
1777 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE | \
1778 SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
1779 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK), \
1781 .info = alc_cap_vol_info, \
1782 .get = alc_cap_vol_get, \
1783 .put = alc_cap_vol_put, \
1784 .tlv = { .c = alc_cap_vol_tlv }, \
1787 #define _DEFINE_CAPSRC(num) \
1789 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
1790 /* .name = "Capture Source", */ \
1791 .name = "Input Source", \
1793 .info = alc_mux_enum_info, \
1794 .get = alc_mux_enum_get, \
1795 .put = alc_mux_enum_put, \
1798 #define DEFINE_CAPMIX(num) \
1799 static const struct snd_kcontrol_new alc_capture_mixer ## num[] = { \
1800 _DEFINE_CAPMIX(num), \
1801 _DEFINE_CAPSRC(num), \
1805 #define DEFINE_CAPMIX_NOSRC(num) \
1806 static const struct snd_kcontrol_new alc_capture_mixer_nosrc ## num[] = { \
1807 _DEFINE_CAPMIX(num), \
1811 /* up to three ADCs */
1815 DEFINE_CAPMIX_NOSRC(1);
1816 DEFINE_CAPMIX_NOSRC(2);
1817 DEFINE_CAPMIX_NOSRC(3);
1820 * virtual master controls
1824 * slave controls for virtual master
1826 static const char * const alc_slave_vols[] = {
1827 "Front Playback Volume",
1828 "Surround Playback Volume",
1829 "Center Playback Volume",
1830 "LFE Playback Volume",
1831 "Side Playback Volume",
1832 "Headphone Playback Volume",
1833 "Speaker Playback Volume",
1834 "Mono Playback Volume",
1835 "Line-Out Playback Volume",
1836 "PCM Playback Volume",
1840 static const char * const alc_slave_sws[] = {
1841 "Front Playback Switch",
1842 "Surround Playback Switch",
1843 "Center Playback Switch",
1844 "LFE Playback Switch",
1845 "Side Playback Switch",
1846 "Headphone Playback Switch",
1847 "Speaker Playback Switch",
1848 "Mono Playback Switch",
1849 "IEC958 Playback Switch",
1850 "Line-Out Playback Switch",
1851 "PCM Playback Switch",
1856 * build control elements
1859 #define NID_MAPPING (-1)
1861 #define SUBDEV_SPEAKER_ (0 << 6)
1862 #define SUBDEV_HP_ (1 << 6)
1863 #define SUBDEV_LINE_ (2 << 6)
1864 #define SUBDEV_SPEAKER(x) (SUBDEV_SPEAKER_ | ((x) & 0x3f))
1865 #define SUBDEV_HP(x) (SUBDEV_HP_ | ((x) & 0x3f))
1866 #define SUBDEV_LINE(x) (SUBDEV_LINE_ | ((x) & 0x3f))
1868 static void alc_free_kctls(struct hda_codec *codec);
1870 #ifdef CONFIG_SND_HDA_INPUT_BEEP
1871 /* additional beep mixers; the actual parameters are overwritten at build */
1872 static const struct snd_kcontrol_new alc_beep_mixer[] = {
1873 HDA_CODEC_VOLUME("Beep Playback Volume", 0, 0, HDA_INPUT),
1874 HDA_CODEC_MUTE_BEEP("Beep Playback Switch", 0, 0, HDA_INPUT),
1879 static int alc_build_controls(struct hda_codec *codec)
1881 struct alc_spec *spec = codec->spec;
1882 struct snd_kcontrol *kctl = NULL;
1883 const struct snd_kcontrol_new *knew;
1888 for (i = 0; i < spec->num_mixers; i++) {
1889 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1893 if (spec->cap_mixer) {
1894 err = snd_hda_add_new_ctls(codec, spec->cap_mixer);
1898 if (spec->multiout.dig_out_nid) {
1899 err = snd_hda_create_spdif_out_ctls(codec,
1900 spec->multiout.dig_out_nid,
1901 spec->multiout.dig_out_nid);
1904 if (!spec->no_analog) {
1905 err = snd_hda_create_spdif_share_sw(codec,
1909 spec->multiout.share_spdif = 1;
1912 if (spec->dig_in_nid) {
1913 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1918 #ifdef CONFIG_SND_HDA_INPUT_BEEP
1919 /* create beep controls if needed */
1920 if (spec->beep_amp) {
1921 const struct snd_kcontrol_new *knew;
1922 for (knew = alc_beep_mixer; knew->name; knew++) {
1923 struct snd_kcontrol *kctl;
1924 kctl = snd_ctl_new1(knew, codec);
1927 kctl->private_value = spec->beep_amp;
1928 err = snd_hda_ctl_add(codec, 0, kctl);
1935 /* if we have no master control, let's create it */
1936 if (!spec->no_analog &&
1937 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1938 unsigned int vmaster_tlv[4];
1939 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1940 HDA_OUTPUT, vmaster_tlv);
1941 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
1942 vmaster_tlv, alc_slave_vols);
1946 if (!spec->no_analog &&
1947 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1948 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
1949 NULL, alc_slave_sws);
1954 /* assign Capture Source enums to NID */
1955 if (spec->capsrc_nids || spec->adc_nids) {
1956 kctl = snd_hda_find_mixer_ctl(codec, "Capture Source");
1958 kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1959 for (i = 0; kctl && i < kctl->count; i++) {
1960 const hda_nid_t *nids = spec->capsrc_nids;
1962 nids = spec->adc_nids;
1963 err = snd_hda_add_nid(codec, kctl, i, nids[i]);
1968 if (spec->cap_mixer && spec->adc_nids) {
1969 const char *kname = kctl ? kctl->id.name : NULL;
1970 for (knew = spec->cap_mixer; knew->name; knew++) {
1971 if (kname && strcmp(knew->name, kname) == 0)
1973 kctl = snd_hda_find_mixer_ctl(codec, knew->name);
1974 for (i = 0; kctl && i < kctl->count; i++) {
1975 err = snd_hda_add_nid(codec, kctl, i,
1983 /* other nid->control mapping */
1984 for (i = 0; i < spec->num_mixers; i++) {
1985 for (knew = spec->mixers[i]; knew->name; knew++) {
1986 if (knew->iface != NID_MAPPING)
1988 kctl = snd_hda_find_mixer_ctl(codec, knew->name);
1991 u = knew->subdevice;
1992 for (j = 0; j < 4; j++, u >>= 8) {
1997 case SUBDEV_SPEAKER_:
1998 nid = spec->autocfg.speaker_pins[nid];
2001 nid = spec->autocfg.line_out_pins[nid];
2004 nid = spec->autocfg.hp_pins[nid];
2009 err = snd_hda_add_nid(codec, kctl, 0, nid);
2013 u = knew->private_value;
2014 for (j = 0; j < 4; j++, u >>= 8) {
2018 err = snd_hda_add_nid(codec, kctl, 0, nid);
2025 alc_free_kctls(codec); /* no longer needed */
2035 static void alc_init_special_input_src(struct hda_codec *codec);
2037 static int alc_init(struct hda_codec *codec)
2039 struct alc_spec *spec = codec->spec;
2043 alc_auto_init_amp(codec, spec->init_amp);
2045 for (i = 0; i < spec->num_init_verbs; i++)
2046 snd_hda_sequence_write(codec, spec->init_verbs[i]);
2047 alc_init_special_input_src(codec);
2049 if (spec->init_hook)
2050 spec->init_hook(codec);
2052 alc_apply_fixup(codec, ALC_FIXUP_ACT_INIT);
2054 hda_call_check_power_status(codec, 0x01);
2058 static void alc_unsol_event(struct hda_codec *codec, unsigned int res)
2060 struct alc_spec *spec = codec->spec;
2062 if (spec->unsol_event)
2063 spec->unsol_event(codec, res);
2066 #ifdef CONFIG_SND_HDA_POWER_SAVE
2067 static int alc_check_power_status(struct hda_codec *codec, hda_nid_t nid)
2069 struct alc_spec *spec = codec->spec;
2070 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
2075 * Analog playback callbacks
2077 static int alc_playback_pcm_open(struct hda_pcm_stream *hinfo,
2078 struct hda_codec *codec,
2079 struct snd_pcm_substream *substream)
2081 struct alc_spec *spec = codec->spec;
2082 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
2086 static int alc_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2087 struct hda_codec *codec,
2088 unsigned int stream_tag,
2089 unsigned int format,
2090 struct snd_pcm_substream *substream)
2092 struct alc_spec *spec = codec->spec;
2093 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
2094 stream_tag, format, substream);
2097 static int alc_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2098 struct hda_codec *codec,
2099 struct snd_pcm_substream *substream)
2101 struct alc_spec *spec = codec->spec;
2102 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
2108 static int alc_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
2109 struct hda_codec *codec,
2110 struct snd_pcm_substream *substream)
2112 struct alc_spec *spec = codec->spec;
2113 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2116 static int alc_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2117 struct hda_codec *codec,
2118 unsigned int stream_tag,
2119 unsigned int format,
2120 struct snd_pcm_substream *substream)
2122 struct alc_spec *spec = codec->spec;
2123 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2124 stream_tag, format, substream);
2127 static int alc_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2128 struct hda_codec *codec,
2129 struct snd_pcm_substream *substream)
2131 struct alc_spec *spec = codec->spec;
2132 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
2135 static int alc_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
2136 struct hda_codec *codec,
2137 struct snd_pcm_substream *substream)
2139 struct alc_spec *spec = codec->spec;
2140 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2146 static int alc_alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2147 struct hda_codec *codec,
2148 unsigned int stream_tag,
2149 unsigned int format,
2150 struct snd_pcm_substream *substream)
2152 struct alc_spec *spec = codec->spec;
2154 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
2155 stream_tag, 0, format);
2159 static int alc_alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2160 struct hda_codec *codec,
2161 struct snd_pcm_substream *substream)
2163 struct alc_spec *spec = codec->spec;
2165 snd_hda_codec_cleanup_stream(codec,
2166 spec->adc_nids[substream->number + 1]);
2170 /* analog capture with dynamic dual-adc changes */
2171 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
2172 struct hda_codec *codec,
2173 unsigned int stream_tag,
2174 unsigned int format,
2175 struct snd_pcm_substream *substream)
2177 struct alc_spec *spec = codec->spec;
2178 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
2179 spec->cur_adc_stream_tag = stream_tag;
2180 spec->cur_adc_format = format;
2181 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
2185 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
2186 struct hda_codec *codec,
2187 struct snd_pcm_substream *substream)
2189 struct alc_spec *spec = codec->spec;
2190 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
2195 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
2199 .nid = 0, /* fill later */
2201 .prepare = dyn_adc_capture_pcm_prepare,
2202 .cleanup = dyn_adc_capture_pcm_cleanup
2208 static const struct hda_pcm_stream alc_pcm_analog_playback = {
2212 /* NID is set in alc_build_pcms */
2214 .open = alc_playback_pcm_open,
2215 .prepare = alc_playback_pcm_prepare,
2216 .cleanup = alc_playback_pcm_cleanup
2220 static const struct hda_pcm_stream alc_pcm_analog_capture = {
2224 /* NID is set in alc_build_pcms */
2227 static const struct hda_pcm_stream alc_pcm_analog_alt_playback = {
2231 /* NID is set in alc_build_pcms */
2234 static const struct hda_pcm_stream alc_pcm_analog_alt_capture = {
2235 .substreams = 2, /* can be overridden */
2238 /* NID is set in alc_build_pcms */
2240 .prepare = alc_alt_capture_pcm_prepare,
2241 .cleanup = alc_alt_capture_pcm_cleanup
2245 static const struct hda_pcm_stream alc_pcm_digital_playback = {
2249 /* NID is set in alc_build_pcms */
2251 .open = alc_dig_playback_pcm_open,
2252 .close = alc_dig_playback_pcm_close,
2253 .prepare = alc_dig_playback_pcm_prepare,
2254 .cleanup = alc_dig_playback_pcm_cleanup
2258 static const struct hda_pcm_stream alc_pcm_digital_capture = {
2262 /* NID is set in alc_build_pcms */
2265 /* Used by alc_build_pcms to flag that a PCM has no playback stream */
2266 static const struct hda_pcm_stream alc_pcm_null_stream = {
2272 static int alc_build_pcms(struct hda_codec *codec)
2274 struct alc_spec *spec = codec->spec;
2275 struct hda_pcm *info = spec->pcm_rec;
2276 const struct hda_pcm_stream *p;
2277 bool have_multi_adcs;
2280 codec->num_pcms = 1;
2281 codec->pcm_info = info;
2283 if (spec->no_analog)
2286 snprintf(spec->stream_name_analog, sizeof(spec->stream_name_analog),
2287 "%s Analog", codec->chip_name);
2288 info->name = spec->stream_name_analog;
2290 if (spec->multiout.dac_nids > 0) {
2291 p = spec->stream_analog_playback;
2293 p = &alc_pcm_analog_playback;
2294 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2295 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
2297 if (spec->adc_nids) {
2298 p = spec->stream_analog_capture;
2300 if (spec->dyn_adc_switch)
2301 p = &dyn_adc_pcm_analog_capture;
2303 p = &alc_pcm_analog_capture;
2305 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2306 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
2309 if (spec->channel_mode) {
2310 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = 0;
2311 for (i = 0; i < spec->num_channel_mode; i++) {
2312 if (spec->channel_mode[i].channels > info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max) {
2313 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->channel_mode[i].channels;
2319 /* SPDIF for stream index #1 */
2320 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
2321 snprintf(spec->stream_name_digital,
2322 sizeof(spec->stream_name_digital),
2323 "%s Digital", codec->chip_name);
2324 codec->num_pcms = 2;
2325 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
2326 info = spec->pcm_rec + 1;
2327 info->name = spec->stream_name_digital;
2328 if (spec->dig_out_type)
2329 info->pcm_type = spec->dig_out_type;
2331 info->pcm_type = HDA_PCM_TYPE_SPDIF;
2332 if (spec->multiout.dig_out_nid) {
2333 p = spec->stream_digital_playback;
2335 p = &alc_pcm_digital_playback;
2336 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2337 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
2339 if (spec->dig_in_nid) {
2340 p = spec->stream_digital_capture;
2342 p = &alc_pcm_digital_capture;
2343 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2344 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
2346 /* FIXME: do we need this for all Realtek codec models? */
2347 codec->spdif_status_reset = 1;
2350 if (spec->no_analog)
2353 /* If the use of more than one ADC is requested for the current
2354 * model, configure a second analog capture-only PCM.
2356 have_multi_adcs = (spec->num_adc_nids > 1) &&
2357 !spec->dyn_adc_switch && !spec->auto_mic &&
2358 (!spec->input_mux || spec->input_mux->num_items > 1);
2359 /* Additional Analaog capture for index #2 */
2360 if (spec->alt_dac_nid || have_multi_adcs) {
2361 codec->num_pcms = 3;
2362 info = spec->pcm_rec + 2;
2363 info->name = spec->stream_name_analog;
2364 if (spec->alt_dac_nid) {
2365 p = spec->stream_analog_alt_playback;
2367 p = &alc_pcm_analog_alt_playback;
2368 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
2369 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
2372 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
2373 alc_pcm_null_stream;
2374 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
2376 if (have_multi_adcs) {
2377 p = spec->stream_analog_alt_capture;
2379 p = &alc_pcm_analog_alt_capture;
2380 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
2381 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
2383 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
2384 spec->num_adc_nids - 1;
2386 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
2387 alc_pcm_null_stream;
2388 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
2395 static inline void alc_shutup(struct hda_codec *codec)
2397 struct alc_spec *spec = codec->spec;
2399 if (spec && spec->shutup)
2400 spec->shutup(codec);
2401 snd_hda_shutup_pins(codec);
2404 static void alc_free_kctls(struct hda_codec *codec)
2406 struct alc_spec *spec = codec->spec;
2408 if (spec->kctls.list) {
2409 struct snd_kcontrol_new *kctl = spec->kctls.list;
2411 for (i = 0; i < spec->kctls.used; i++)
2412 kfree(kctl[i].name);
2414 snd_array_free(&spec->kctls);
2417 static void alc_free_bind_ctls(struct hda_codec *codec)
2419 struct alc_spec *spec = codec->spec;
2420 if (spec->bind_ctls.list) {
2421 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
2423 for (i = 0; i < spec->bind_ctls.used; i++)
2426 snd_array_free(&spec->bind_ctls);
2429 static void alc_free(struct hda_codec *codec)
2431 struct alc_spec *spec = codec->spec;
2437 snd_hda_input_jack_free(codec);
2438 alc_free_kctls(codec);
2439 alc_free_bind_ctls(codec);
2441 snd_hda_detach_beep_device(codec);
2444 #ifdef CONFIG_SND_HDA_POWER_SAVE
2445 static void alc_power_eapd(struct hda_codec *codec)
2447 alc_auto_setup_eapd(codec, false);
2450 static int alc_suspend(struct hda_codec *codec, pm_message_t state)
2452 struct alc_spec *spec = codec->spec;
2454 if (spec && spec->power_hook)
2455 spec->power_hook(codec);
2461 static int alc_resume(struct hda_codec *codec)
2463 msleep(150); /* to avoid pop noise */
2464 codec->patch_ops.init(codec);
2465 snd_hda_codec_resume_amp(codec);
2466 snd_hda_codec_resume_cache(codec);
2467 hda_call_check_power_status(codec, 0x01);
2474 static const struct hda_codec_ops alc_patch_ops = {
2475 .build_controls = alc_build_controls,
2476 .build_pcms = alc_build_pcms,
2479 .unsol_event = alc_unsol_event,
2481 .resume = alc_resume,
2483 #ifdef CONFIG_SND_HDA_POWER_SAVE
2484 .suspend = alc_suspend,
2485 .check_power_status = alc_check_power_status,
2487 .reboot_notify = alc_shutup,
2490 /* replace the codec chip_name with the given string */
2491 static int alc_codec_rename(struct hda_codec *codec, const char *name)
2493 kfree(codec->chip_name);
2494 codec->chip_name = kstrdup(name, GFP_KERNEL);
2495 if (!codec->chip_name) {
2503 * Rename codecs appropriately from COEF value
2505 struct alc_codec_rename_table {
2506 unsigned int vendor_id;
2507 unsigned short coef_mask;
2508 unsigned short coef_bits;
2512 static struct alc_codec_rename_table rename_tbl[] = {
2513 { 0x10ec0269, 0xfff0, 0x3010, "ALC277" },
2514 { 0x10ec0269, 0xf0f0, 0x2010, "ALC259" },
2515 { 0x10ec0269, 0xf0f0, 0x3010, "ALC258" },
2516 { 0x10ec0269, 0x00f0, 0x0010, "ALC269VB" },
2517 { 0x10ec0269, 0xffff, 0xa023, "ALC259" },
2518 { 0x10ec0269, 0xffff, 0x6023, "ALC281X" },
2519 { 0x10ec0269, 0x00f0, 0x0020, "ALC269VC" },
2520 { 0x10ec0887, 0x00f0, 0x0030, "ALC887-VD" },
2521 { 0x10ec0888, 0x00f0, 0x0030, "ALC888-VD" },
2522 { 0x10ec0888, 0xf0f0, 0x3020, "ALC886" },
2523 { 0x10ec0899, 0x2000, 0x2000, "ALC899" },
2524 { 0x10ec0892, 0xffff, 0x8020, "ALC661" },
2525 { 0x10ec0892, 0xffff, 0x8011, "ALC661" },
2526 { 0x10ec0892, 0xffff, 0x4011, "ALC656" },
2527 { } /* terminator */
2530 static int alc_codec_rename_from_preset(struct hda_codec *codec)
2532 const struct alc_codec_rename_table *p;
2534 for (p = rename_tbl; p->vendor_id; p++) {
2535 if (p->vendor_id != codec->vendor_id)
2537 if ((alc_get_coef0(codec) & p->coef_mask) == p->coef_bits)
2538 return alc_codec_rename(codec, p->name);
2544 * Automatic parse of I/O pins from the BIOS configuration
2549 ALC_CTL_WIDGET_MUTE,
2554 static const struct snd_kcontrol_new alc_control_templates[] = {
2555 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
2556 HDA_CODEC_MUTE(NULL, 0, 0, 0),
2557 HDA_BIND_MUTE(NULL, 0, 0, 0),
2558 HDA_BIND_VOL(NULL, 0),
2559 HDA_BIND_SW(NULL, 0),
2562 /* add dynamic controls */
2563 static int add_control(struct alc_spec *spec, int type, const char *name,
2564 int cidx, unsigned long val)
2566 struct snd_kcontrol_new *knew;
2568 knew = alc_kcontrol_new(spec);
2571 *knew = alc_control_templates[type];
2572 knew->name = kstrdup(name, GFP_KERNEL);
2576 if (get_amp_nid_(val))
2577 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2578 knew->private_value = val;
2582 static int add_control_with_pfx(struct alc_spec *spec, int type,
2583 const char *pfx, const char *dir,
2584 const char *sfx, int cidx, unsigned long val)
2587 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
2588 return add_control(spec, type, name, cidx, val);
2591 #define add_pb_vol_ctrl(spec, type, pfx, val) \
2592 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
2593 #define add_pb_sw_ctrl(spec, type, pfx, val) \
2594 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
2595 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
2596 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
2597 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
2598 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
2600 static const char * const channel_name[4] = {
2601 "Front", "Surround", "CLFE", "Side"
2604 static const char *alc_get_line_out_pfx(struct alc_spec *spec, int ch,
2605 bool can_be_master, int *index)
2607 struct auto_pin_cfg *cfg = &spec->autocfg;
2610 if (cfg->line_outs == 1 && !spec->multi_ios &&
2611 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
2614 switch (cfg->line_out_type) {
2615 case AUTO_PIN_SPEAKER_OUT:
2616 if (cfg->line_outs == 1)
2619 case AUTO_PIN_HP_OUT:
2620 /* for multi-io case, only the primary out */
2621 if (ch && spec->multi_ios)
2626 if (cfg->line_outs == 1 && !spec->multi_ios)
2630 if (snd_BUG_ON(ch >= ARRAY_SIZE(channel_name)))
2633 return channel_name[ch];
2636 /* create input playback/capture controls for the given pin */
2637 static int new_analog_input(struct alc_spec *spec, hda_nid_t pin,
2638 const char *ctlname, int ctlidx,
2639 int idx, hda_nid_t mix_nid)
2643 err = __add_pb_vol_ctrl(spec, ALC_CTL_WIDGET_VOL, ctlname, ctlidx,
2644 HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
2647 err = __add_pb_sw_ctrl(spec, ALC_CTL_WIDGET_MUTE, ctlname, ctlidx,
2648 HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
2654 static int alc_is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2656 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2657 return (pincap & AC_PINCAP_IN) != 0;
2660 /* Parse the codec tree and retrieve ADCs and corresponding capsrc MUXs */
2661 static int alc_auto_fill_adc_caps(struct hda_codec *codec)
2663 struct alc_spec *spec = codec->spec;
2665 hda_nid_t *adc_nids = spec->private_adc_nids;
2666 hda_nid_t *cap_nids = spec->private_capsrc_nids;
2667 int max_nums = ARRAY_SIZE(spec->private_adc_nids);
2670 nid = codec->start_nid;
2671 for (i = 0; i < codec->num_nodes; i++, nid++) {
2673 const hda_nid_t *list;
2674 unsigned int caps = get_wcaps(codec, nid);
2675 int type = get_wcaps_type(caps);
2677 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2679 adc_nids[nums] = nid;
2680 cap_nids[nums] = nid;
2684 type = get_wcaps_type(get_wcaps(codec, src));
2685 if (type == AC_WID_PIN)
2687 if (type == AC_WID_AUD_SEL) {
2688 cap_nids[nums] = src;
2691 n = snd_hda_get_conn_list(codec, src, &list);
2693 cap_nids[nums] = src;
2699 if (++nums >= max_nums)
2702 spec->adc_nids = spec->private_adc_nids;
2703 spec->capsrc_nids = spec->private_capsrc_nids;
2704 spec->num_adc_nids = nums;
2708 /* create playback/capture controls for input pins */
2709 static int alc_auto_create_input_ctls(struct hda_codec *codec)
2711 struct alc_spec *spec = codec->spec;
2712 const struct auto_pin_cfg *cfg = &spec->autocfg;
2713 hda_nid_t mixer = spec->mixer_nid;
2714 struct hda_input_mux *imux = &spec->private_imux[0];
2716 int i, c, err, idx, type_idx = 0;
2717 const char *prev_label = NULL;
2719 num_adcs = alc_auto_fill_adc_caps(codec);
2723 for (i = 0; i < cfg->num_inputs; i++) {
2727 pin = cfg->inputs[i].pin;
2728 if (!alc_is_input_pin(codec, pin))
2731 label = hda_get_autocfg_input_label(codec, cfg, i);
2732 if (prev_label && !strcmp(label, prev_label))
2739 idx = get_connection_index(codec, mixer, pin);
2741 err = new_analog_input(spec, pin,
2749 for (c = 0; c < num_adcs; c++) {
2750 hda_nid_t cap = spec->capsrc_nids ?
2751 spec->capsrc_nids[c] : spec->adc_nids[c];
2752 idx = get_connection_index(codec, cap, pin);
2754 spec->imux_pins[imux->num_items] = pin;
2755 snd_hda_add_imux_item(imux, label, idx, NULL);
2761 spec->num_mux_defs = 1;
2762 spec->input_mux = imux;
2767 static void alc_set_pin_output(struct hda_codec *codec, hda_nid_t nid,
2768 unsigned int pin_type)
2770 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2773 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2774 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2778 static int get_pin_type(int line_out_type)
2780 if (line_out_type == AUTO_PIN_HP_OUT)
2786 static void alc_auto_init_analog_input(struct hda_codec *codec)
2788 struct alc_spec *spec = codec->spec;
2789 struct auto_pin_cfg *cfg = &spec->autocfg;
2792 for (i = 0; i < cfg->num_inputs; i++) {
2793 hda_nid_t nid = cfg->inputs[i].pin;
2794 if (alc_is_input_pin(codec, nid)) {
2795 alc_set_input_pin(codec, nid, cfg->inputs[i].type);
2796 if (get_wcaps(codec, nid) & AC_WCAP_OUT_AMP)
2797 snd_hda_codec_write(codec, nid, 0,
2798 AC_VERB_SET_AMP_GAIN_MUTE,
2803 /* mute all loopback inputs */
2804 if (spec->mixer_nid) {
2805 int nums = snd_hda_get_conn_list(codec, spec->mixer_nid, NULL);
2806 for (i = 0; i < nums; i++)
2807 snd_hda_codec_write(codec, spec->mixer_nid, 0,
2808 AC_VERB_SET_AMP_GAIN_MUTE,
2813 /* convert from MIX nid to DAC */
2814 static hda_nid_t alc_auto_mix_to_dac(struct hda_codec *codec, hda_nid_t nid)
2819 if (get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_AUD_OUT)
2821 num = snd_hda_get_connections(codec, nid, list, ARRAY_SIZE(list));
2822 for (i = 0; i < num; i++) {
2823 if (get_wcaps_type(get_wcaps(codec, list[i])) == AC_WID_AUD_OUT)
2829 /* go down to the selector widget before the mixer */
2830 static hda_nid_t alc_go_down_to_selector(struct hda_codec *codec, hda_nid_t pin)
2833 int num = snd_hda_get_connections(codec, pin, srcs,
2836 get_wcaps_type(get_wcaps(codec, srcs[0])) != AC_WID_AUD_SEL)
2841 /* get MIX nid connected to the given pin targeted to DAC */
2842 static hda_nid_t alc_auto_dac_to_mix(struct hda_codec *codec, hda_nid_t pin,
2848 pin = alc_go_down_to_selector(codec, pin);
2849 num = snd_hda_get_connections(codec, pin, mix, ARRAY_SIZE(mix));
2850 for (i = 0; i < num; i++) {
2851 if (alc_auto_mix_to_dac(codec, mix[i]) == dac)
2857 /* select the connection from pin to DAC if needed */
2858 static int alc_auto_select_dac(struct hda_codec *codec, hda_nid_t pin,
2864 pin = alc_go_down_to_selector(codec, pin);
2865 num = snd_hda_get_connections(codec, pin, mix, ARRAY_SIZE(mix));
2868 for (i = 0; i < num; i++) {
2869 if (alc_auto_mix_to_dac(codec, mix[i]) == dac) {
2870 snd_hda_codec_update_cache(codec, pin, 0,
2871 AC_VERB_SET_CONNECT_SEL, i);
2878 /* look for an empty DAC slot */
2879 static hda_nid_t alc_auto_look_for_dac(struct hda_codec *codec, hda_nid_t pin)
2881 struct alc_spec *spec = codec->spec;
2885 pin = alc_go_down_to_selector(codec, pin);
2886 num = snd_hda_get_connections(codec, pin, srcs, ARRAY_SIZE(srcs));
2887 for (i = 0; i < num; i++) {
2888 hda_nid_t nid = alc_auto_mix_to_dac(codec, srcs[i]);
2891 if (found_in_nid_list(nid, spec->multiout.dac_nids,
2892 spec->multiout.num_dacs))
2894 if (found_in_nid_list(nid, spec->multiout.hp_out_nid,
2895 ARRAY_SIZE(spec->multiout.hp_out_nid)))
2897 if (found_in_nid_list(nid, spec->multiout.extra_out_nid,
2898 ARRAY_SIZE(spec->multiout.extra_out_nid)))
2905 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
2907 hda_nid_t sel = alc_go_down_to_selector(codec, pin);
2908 if (snd_hda_get_conn_list(codec, sel, NULL) == 1)
2909 return alc_auto_look_for_dac(codec, pin);
2913 static int alc_auto_fill_extra_dacs(struct hda_codec *codec, int num_outs,
2914 const hda_nid_t *pins, hda_nid_t *dacs)
2918 if (num_outs && !dacs[0]) {
2919 dacs[0] = alc_auto_look_for_dac(codec, pins[0]);
2924 for (i = 1; i < num_outs; i++)
2925 dacs[i] = get_dac_if_single(codec, pins[i]);
2926 for (i = 1; i < num_outs; i++) {
2928 dacs[i] = alc_auto_look_for_dac(codec, pins[i]);
2933 static int alc_auto_fill_multi_ios(struct hda_codec *codec,
2934 unsigned int location);
2936 /* fill in the dac_nids table from the parsed pin configuration */
2937 static int alc_auto_fill_dac_nids(struct hda_codec *codec)
2939 struct alc_spec *spec = codec->spec;
2940 const struct auto_pin_cfg *cfg = &spec->autocfg;
2941 bool redone = false;
2945 /* set num_dacs once to full for alc_auto_look_for_dac() */
2946 spec->multiout.num_dacs = cfg->line_outs;
2947 spec->multiout.hp_out_nid[0] = 0;
2948 spec->multiout.extra_out_nid[0] = 0;
2949 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
2950 spec->multiout.dac_nids = spec->private_dac_nids;
2952 /* fill hard-wired DACs first */
2954 for (i = 0; i < cfg->line_outs; i++)
2955 spec->private_dac_nids[i] =
2956 get_dac_if_single(codec, cfg->line_out_pins[i]);
2958 spec->multiout.hp_out_nid[0] =
2959 get_dac_if_single(codec, cfg->hp_pins[0]);
2960 if (cfg->speaker_outs)
2961 spec->multiout.extra_out_nid[0] =
2962 get_dac_if_single(codec, cfg->speaker_pins[0]);
2965 for (i = 0; i < cfg->line_outs; i++) {
2966 hda_nid_t pin = cfg->line_out_pins[i];
2967 if (spec->private_dac_nids[i])
2969 spec->private_dac_nids[i] = alc_auto_look_for_dac(codec, pin);
2970 if (!spec->private_dac_nids[i] && !redone) {
2971 /* if we can't find primary DACs, re-probe without
2972 * checking the hard-wired DACs
2979 /* re-count num_dacs and squash invalid entries */
2980 spec->multiout.num_dacs = 0;
2981 for (i = 0; i < cfg->line_outs; i++) {
2982 if (spec->private_dac_nids[i])
2983 spec->multiout.num_dacs++;
2985 memmove(spec->private_dac_nids + i,
2986 spec->private_dac_nids + i + 1,
2987 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
2990 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2991 /* try to fill multi-io first */
2992 unsigned int location, defcfg;
2995 defcfg = snd_hda_codec_get_pincfg(codec, cfg->line_out_pins[0]);
2996 location = get_defcfg_location(defcfg);
2998 num_pins = alc_auto_fill_multi_ios(codec, location);
3000 spec->multi_ios = num_pins;
3001 spec->ext_channel_count = 2;
3002 spec->multiout.num_dacs = num_pins + 1;
3006 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3007 alc_auto_fill_extra_dacs(codec, cfg->hp_outs, cfg->hp_pins,
3008 spec->multiout.hp_out_nid);
3009 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3010 alc_auto_fill_extra_dacs(codec, cfg->speaker_outs, cfg->speaker_pins,
3011 spec->multiout.extra_out_nid);
3016 static inline unsigned int get_ctl_pos(unsigned int data)
3018 hda_nid_t nid = get_amp_nid_(data);
3019 unsigned int dir = get_amp_direction_(data);
3020 return (nid << 1) | dir;
3023 #define is_ctl_used(bits, data) \
3024 test_bit(get_ctl_pos(data), bits)
3025 #define mark_ctl_usage(bits, data) \
3026 set_bit(get_ctl_pos(data), bits)
3028 static int alc_auto_add_vol_ctl(struct hda_codec *codec,
3029 const char *pfx, int cidx,
3030 hda_nid_t nid, unsigned int chs)
3032 struct alc_spec *spec = codec->spec;
3036 val = HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT);
3037 if (is_ctl_used(spec->vol_ctls, val) && chs != 2) /* exclude LFE */
3039 mark_ctl_usage(spec->vol_ctls, val);
3040 return __add_pb_vol_ctrl(codec->spec, ALC_CTL_WIDGET_VOL, pfx, cidx,
3044 #define alc_auto_add_stereo_vol(codec, pfx, cidx, nid) \
3045 alc_auto_add_vol_ctl(codec, pfx, cidx, nid, 3)
3047 /* create a mute-switch for the given mixer widget;
3048 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
3050 static int alc_auto_add_sw_ctl(struct hda_codec *codec,
3051 const char *pfx, int cidx,
3052 hda_nid_t nid, unsigned int chs)
3054 struct alc_spec *spec = codec->spec;
3060 wid_type = get_wcaps_type(get_wcaps(codec, nid));
3061 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT) {
3062 type = ALC_CTL_WIDGET_MUTE;
3063 val = HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT);
3064 } else if (snd_hda_get_conn_list(codec, nid, NULL) == 1) {
3065 type = ALC_CTL_WIDGET_MUTE;
3066 val = HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_INPUT);
3068 type = ALC_CTL_BIND_MUTE;
3069 val = HDA_COMPOSE_AMP_VAL(nid, chs, 2, HDA_INPUT);
3071 if (is_ctl_used(spec->sw_ctls, val) && chs != 2) /* exclude LFE */
3073 mark_ctl_usage(spec->sw_ctls, val);
3074 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
3077 #define alc_auto_add_stereo_sw(codec, pfx, cidx, nid) \
3078 alc_auto_add_sw_ctl(codec, pfx, cidx, nid, 3)
3080 static hda_nid_t alc_look_for_out_mute_nid(struct hda_codec *codec,
3081 hda_nid_t pin, hda_nid_t dac)
3083 hda_nid_t mix = alc_auto_dac_to_mix(codec, pin, dac);
3084 if (nid_has_mute(codec, pin, HDA_OUTPUT))
3086 else if (mix && nid_has_mute(codec, mix, HDA_INPUT))
3088 else if (nid_has_mute(codec, dac, HDA_OUTPUT))
3093 static hda_nid_t alc_look_for_out_vol_nid(struct hda_codec *codec,
3094 hda_nid_t pin, hda_nid_t dac)
3096 hda_nid_t mix = alc_auto_dac_to_mix(codec, pin, dac);
3097 if (nid_has_volume(codec, dac, HDA_OUTPUT))
3099 else if (nid_has_volume(codec, mix, HDA_OUTPUT))
3101 else if (nid_has_volume(codec, pin, HDA_OUTPUT))
3106 /* add playback controls from the parsed DAC table */
3107 static int alc_auto_create_multi_out_ctls(struct hda_codec *codec,
3108 const struct auto_pin_cfg *cfg)
3110 struct alc_spec *spec = codec->spec;
3111 int i, err, noutputs;
3113 noutputs = cfg->line_outs;
3114 if (spec->multi_ios > 0)
3115 noutputs += spec->multi_ios;
3117 for (i = 0; i < noutputs; i++) {
3123 dac = spec->multiout.dac_nids[i];
3126 if (i >= cfg->line_outs)
3127 pin = spec->multi_io[i - 1].pin;
3129 pin = cfg->line_out_pins[i];
3131 sw = alc_look_for_out_mute_nid(codec, pin, dac);
3132 vol = alc_look_for_out_vol_nid(codec, pin, dac);
3133 name = alc_get_line_out_pfx(spec, i, true, &index);
3134 if (!name || !strcmp(name, "CLFE")) {
3136 err = alc_auto_add_vol_ctl(codec, "Center", 0, vol, 1);
3139 err = alc_auto_add_vol_ctl(codec, "LFE", 0, vol, 2);
3142 err = alc_auto_add_sw_ctl(codec, "Center", 0, sw, 1);
3145 err = alc_auto_add_sw_ctl(codec, "LFE", 0, sw, 2);
3149 err = alc_auto_add_stereo_vol(codec, name, index, vol);
3152 err = alc_auto_add_stereo_sw(codec, name, index, sw);
3160 static int alc_auto_create_extra_out(struct hda_codec *codec, hda_nid_t pin,
3161 hda_nid_t dac, const char *pfx)
3163 struct alc_spec *spec = codec->spec;
3169 /* the corresponding DAC is already occupied */
3170 if (!(get_wcaps(codec, pin) & AC_WCAP_OUT_AMP))
3171 return 0; /* no way */
3172 /* create a switch only */
3173 val = HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT);
3174 if (is_ctl_used(spec->sw_ctls, val))
3175 return 0; /* already created */
3176 mark_ctl_usage(spec->sw_ctls, val);
3177 return add_pb_sw_ctrl(spec, ALC_CTL_WIDGET_MUTE, pfx, val);
3180 sw = alc_look_for_out_mute_nid(codec, pin, dac);
3181 vol = alc_look_for_out_vol_nid(codec, pin, dac);
3182 err = alc_auto_add_stereo_vol(codec, pfx, 0, vol);
3185 err = alc_auto_add_stereo_sw(codec, pfx, 0, sw);
3191 static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
3193 struct hda_ctl_ops *ops)
3195 struct alc_spec *spec = codec->spec;
3196 struct hda_bind_ctls **ctlp, *ctl;
3197 snd_array_init(&spec->bind_ctls, sizeof(ctl), 8);
3198 ctlp = snd_array_new(&spec->bind_ctls);
3201 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
3208 /* add playback controls for speaker and HP outputs */
3209 static int alc_auto_create_extra_outs(struct hda_codec *codec, int num_pins,
3210 const hda_nid_t *pins,
3211 const hda_nid_t *dacs,
3214 struct alc_spec *spec = codec->spec;
3215 struct hda_bind_ctls *ctl;
3219 if (!num_pins || !pins[0])
3222 if (num_pins == 1) {
3223 hda_nid_t dac = *dacs;
3225 dac = spec->multiout.dac_nids[0];
3226 return alc_auto_create_extra_out(codec, *pins, dac, pfx);
3229 if (dacs[num_pins - 1]) {
3230 /* OK, we have a multi-output system with individual volumes */
3231 for (i = 0; i < num_pins; i++) {
3232 snprintf(name, sizeof(name), "%s %s",
3233 pfx, channel_name[i]);
3234 err = alc_auto_create_extra_out(codec, pins[i], dacs[i],
3242 /* Let's create a bind-controls */
3243 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_sw);
3247 for (i = 0; i < num_pins; i++) {
3248 if (get_wcaps(codec, pins[i]) & AC_WCAP_OUT_AMP)
3250 HDA_COMPOSE_AMP_VAL(pins[i], 3, 0, HDA_OUTPUT);
3253 snprintf(name, sizeof(name), "%s Playback Switch", pfx);
3254 err = add_control(spec, ALC_CTL_BIND_SW, name, 0, (long)ctl);
3259 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
3263 for (i = 0; i < num_pins; i++) {
3265 if (!pins[i] || !dacs[i])
3267 vol = alc_look_for_out_vol_nid(codec, pins[i], dacs[i]);
3270 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
3273 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
3274 err = add_control(spec, ALC_CTL_BIND_VOL, name, 0, (long)ctl);
3281 static int alc_auto_create_hp_out(struct hda_codec *codec)
3283 struct alc_spec *spec = codec->spec;
3284 return alc_auto_create_extra_outs(codec, spec->autocfg.hp_outs,
3285 spec->autocfg.hp_pins,
3286 spec->multiout.hp_out_nid,
3290 static int alc_auto_create_speaker_out(struct hda_codec *codec)
3292 struct alc_spec *spec = codec->spec;
3293 return alc_auto_create_extra_outs(codec, spec->autocfg.speaker_outs,
3294 spec->autocfg.speaker_pins,
3295 spec->multiout.extra_out_nid,
3299 static void alc_auto_set_output_and_unmute(struct hda_codec *codec,
3300 hda_nid_t pin, int pin_type,
3304 hda_nid_t nid, mix = 0;
3305 hda_nid_t srcs[HDA_MAX_CONNECTIONS];
3307 alc_set_pin_output(codec, pin, pin_type);
3308 nid = alc_go_down_to_selector(codec, pin);
3309 num = snd_hda_get_connections(codec, nid, srcs, ARRAY_SIZE(srcs));
3310 for (i = 0; i < num; i++) {
3311 if (alc_auto_mix_to_dac(codec, srcs[i]) != dac)
3319 /* need the manual connection? */
3321 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, i);
3322 /* unmute mixer widget inputs */
3323 if (nid_has_mute(codec, mix, HDA_INPUT)) {
3324 snd_hda_codec_write(codec, mix, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3326 snd_hda_codec_write(codec, mix, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3329 /* initialize volume */
3330 nid = alc_look_for_out_vol_nid(codec, pin, dac);
3332 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3335 /* unmute DAC if it's not assigned to a mixer */
3336 nid = alc_look_for_out_mute_nid(codec, pin, dac);
3337 if (nid == mix && nid_has_mute(codec, dac, HDA_OUTPUT))
3338 snd_hda_codec_write(codec, dac, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3342 static void alc_auto_init_multi_out(struct hda_codec *codec)
3344 struct alc_spec *spec = codec->spec;
3345 int pin_type = get_pin_type(spec->autocfg.line_out_type);
3348 for (i = 0; i <= HDA_SIDE; i++) {
3349 hda_nid_t nid = spec->autocfg.line_out_pins[i];
3351 alc_auto_set_output_and_unmute(codec, nid, pin_type,
3352 spec->multiout.dac_nids[i]);
3356 static void alc_auto_init_extra_out(struct hda_codec *codec)
3358 struct alc_spec *spec = codec->spec;
3362 for (i = 0; i < spec->autocfg.hp_outs; i++) {
3363 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3365 pin = spec->autocfg.hp_pins[i];
3368 dac = spec->multiout.hp_out_nid[i];
3370 if (i > 0 && spec->multiout.hp_out_nid[0])
3371 dac = spec->multiout.hp_out_nid[0];
3373 dac = spec->multiout.dac_nids[0];
3375 alc_auto_set_output_and_unmute(codec, pin, PIN_HP, dac);
3377 for (i = 0; i < spec->autocfg.speaker_outs; i++) {
3378 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3380 pin = spec->autocfg.speaker_pins[i];
3383 dac = spec->multiout.extra_out_nid[i];
3385 if (i > 0 && spec->multiout.extra_out_nid[0])
3386 dac = spec->multiout.extra_out_nid[0];
3388 dac = spec->multiout.dac_nids[0];
3390 alc_auto_set_output_and_unmute(codec, pin, PIN_OUT, dac);
3397 static int alc_auto_fill_multi_ios(struct hda_codec *codec,
3398 unsigned int location)
3400 struct alc_spec *spec = codec->spec;
3401 struct auto_pin_cfg *cfg = &spec->autocfg;
3402 hda_nid_t prime_dac = spec->private_dac_nids[0];
3403 int type, i, num_pins = 0;
3405 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
3406 for (i = 0; i < cfg->num_inputs; i++) {
3407 hda_nid_t nid = cfg->inputs[i].pin;
3409 unsigned int defcfg, caps;
3410 if (cfg->inputs[i].type != type)
3412 defcfg = snd_hda_codec_get_pincfg(codec, nid);
3413 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
3415 if (location && get_defcfg_location(defcfg) != location)
3417 caps = snd_hda_query_pin_caps(codec, nid);
3418 if (!(caps & AC_PINCAP_OUT))
3420 dac = alc_auto_look_for_dac(codec, nid);
3423 spec->multi_io[num_pins].pin = nid;
3424 spec->multi_io[num_pins].dac = dac;
3426 spec->private_dac_nids[spec->multiout.num_dacs++] = dac;
3429 spec->multiout.num_dacs = 1;
3431 /* clear up again */
3432 memset(spec->private_dac_nids, 0,
3433 sizeof(spec->private_dac_nids));
3434 spec->private_dac_nids[0] = prime_dac;
3440 static int alc_auto_ch_mode_info(struct snd_kcontrol *kcontrol,
3441 struct snd_ctl_elem_info *uinfo)
3443 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3444 struct alc_spec *spec = codec->spec;
3446 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3448 uinfo->value.enumerated.items = spec->multi_ios + 1;
3449 if (uinfo->value.enumerated.item > spec->multi_ios)
3450 uinfo->value.enumerated.item = spec->multi_ios;
3451 sprintf(uinfo->value.enumerated.name, "%dch",
3452 (uinfo->value.enumerated.item + 1) * 2);