2 * Universal Interface for Intel High Definition Audio Codec
4 * Generic widget tree parser
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/sort.h>
27 #include <linux/delay.h>
28 #include <linux/ctype.h>
29 #include <linux/string.h>
30 #include <linux/bitops.h>
31 #include <linux/module.h>
32 #include <sound/core.h>
33 #include <sound/jack.h>
34 #include <sound/tlv.h>
35 #include "hda_codec.h"
36 #include "hda_local.h"
37 #include "hda_auto_parser.h"
40 #include "hda_generic.h"
43 /* initialize hda_gen_spec struct */
44 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
46 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
47 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
48 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
49 mutex_init(&spec->pcm_mutex);
52 EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
54 struct snd_kcontrol_new *
55 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
56 const struct snd_kcontrol_new *temp)
58 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
63 knew->name = kstrdup(name, GFP_KERNEL);
65 knew->name = kstrdup(knew->name, GFP_KERNEL);
70 EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
72 static void free_kctls(struct hda_gen_spec *spec)
74 if (spec->kctls.list) {
75 struct snd_kcontrol_new *kctl = spec->kctls.list;
77 for (i = 0; i < spec->kctls.used; i++)
80 snd_array_free(&spec->kctls);
83 static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
88 snd_array_free(&spec->paths);
89 snd_array_free(&spec->loopback_list);
95 static void parse_user_hints(struct hda_codec *codec)
97 struct hda_gen_spec *spec = codec->spec;
100 val = snd_hda_get_bool_hint(codec, "jack_detect");
102 codec->no_jack_detect = !val;
103 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
105 codec->inv_jack_detect = !!val;
106 val = snd_hda_get_bool_hint(codec, "trigger_sense");
108 codec->no_trigger_sense = !val;
109 val = snd_hda_get_bool_hint(codec, "inv_eapd");
111 codec->inv_eapd = !!val;
112 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
114 codec->pcm_format_first = !!val;
115 val = snd_hda_get_bool_hint(codec, "sticky_stream");
117 codec->no_sticky_stream = !val;
118 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
120 codec->spdif_status_reset = !!val;
121 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
123 codec->pin_amp_workaround = !!val;
124 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
126 codec->single_adc_amp = !!val;
128 val = snd_hda_get_bool_hint(codec, "auto_mute");
130 spec->suppress_auto_mute = !val;
131 val = snd_hda_get_bool_hint(codec, "auto_mic");
133 spec->suppress_auto_mic = !val;
134 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
136 spec->line_in_auto_switch = !!val;
137 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
139 spec->auto_mute_via_amp = !!val;
140 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
142 spec->need_dac_fix = !!val;
143 val = snd_hda_get_bool_hint(codec, "primary_hp");
145 spec->no_primary_hp = !val;
146 val = snd_hda_get_bool_hint(codec, "multi_io");
148 spec->no_multi_io = !val;
149 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
151 spec->multi_cap_vol = !!val;
152 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
154 spec->inv_dmic_split = !!val;
155 val = snd_hda_get_bool_hint(codec, "indep_hp");
157 spec->indep_hp = !!val;
158 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
160 spec->add_stereo_mix_input = !!val;
161 /* the following two are just for compatibility */
162 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
164 spec->add_jack_modes = !!val;
165 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
167 spec->add_jack_modes = !!val;
168 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
170 spec->add_jack_modes = !!val;
171 val = snd_hda_get_bool_hint(codec, "power_down_unused");
173 spec->power_down_unused = !!val;
174 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
176 spec->hp_mic = !!val;
177 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
179 spec->suppress_hp_mic_detect = !val;
181 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
182 spec->mixer_nid = val;
186 * pin control value accesses
189 #define update_pin_ctl(codec, pin, val) \
190 snd_hda_codec_update_cache(codec, pin, 0, \
191 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
193 /* restore the pinctl based on the cached value */
194 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
196 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
199 /* set the pinctl target value and write it if requested */
200 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
201 unsigned int val, bool do_write)
205 val = snd_hda_correct_pin_ctl(codec, pin, val);
206 snd_hda_codec_set_pin_target(codec, pin, val);
208 update_pin_ctl(codec, pin, val);
211 /* set pinctl target values for all given pins */
212 static void set_pin_targets(struct hda_codec *codec, int num_pins,
213 hda_nid_t *pins, unsigned int val)
216 for (i = 0; i < num_pins; i++)
217 set_pin_target(codec, pins[i], val, false);
224 /* return the position of NID in the list, or -1 if not found */
225 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
228 for (i = 0; i < nums; i++)
234 /* return true if the given NID is contained in the path */
235 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
237 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
240 static struct nid_path *get_nid_path(struct hda_codec *codec,
241 hda_nid_t from_nid, hda_nid_t to_nid,
244 struct hda_gen_spec *spec = codec->spec;
247 for (i = 0; i < spec->paths.used; i++) {
248 struct nid_path *path = snd_array_elem(&spec->paths, i);
249 if (path->depth <= 0)
251 if ((!from_nid || path->path[0] == from_nid) &&
252 (!to_nid || path->path[path->depth - 1] == to_nid)) {
254 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
255 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
262 /* get the path between the given NIDs;
263 * passing 0 to either @pin or @dac behaves as a wildcard
265 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
266 hda_nid_t from_nid, hda_nid_t to_nid)
268 return get_nid_path(codec, from_nid, to_nid, 0);
270 EXPORT_SYMBOL_GPL(snd_hda_get_nid_path);
272 /* get the index number corresponding to the path instance;
273 * the index starts from 1, for easier checking the invalid value
275 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
277 struct hda_gen_spec *spec = codec->spec;
278 struct nid_path *array = spec->paths.list;
281 if (!spec->paths.used)
284 if (idx < 0 || idx >= spec->paths.used)
288 EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
290 /* get the path instance corresponding to the given index number */
291 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
293 struct hda_gen_spec *spec = codec->spec;
295 if (idx <= 0 || idx > spec->paths.used)
297 return snd_array_elem(&spec->paths, idx - 1);
299 EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
301 /* check whether the given DAC is already found in any existing paths */
302 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
304 struct hda_gen_spec *spec = codec->spec;
307 for (i = 0; i < spec->paths.used; i++) {
308 struct nid_path *path = snd_array_elem(&spec->paths, i);
309 if (path->path[0] == nid)
315 /* check whether the given two widgets can be connected */
316 static bool is_reachable_path(struct hda_codec *codec,
317 hda_nid_t from_nid, hda_nid_t to_nid)
319 if (!from_nid || !to_nid)
321 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
324 /* nid, dir and idx */
325 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
327 /* check whether the given ctl is already assigned in any path elements */
328 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
330 struct hda_gen_spec *spec = codec->spec;
333 val &= AMP_VAL_COMPARE_MASK;
334 for (i = 0; i < spec->paths.used; i++) {
335 struct nid_path *path = snd_array_elem(&spec->paths, i);
336 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
342 /* check whether a control with the given (nid, dir, idx) was assigned */
343 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
344 int dir, int idx, int type)
346 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
347 return is_ctl_used(codec, val, type);
350 static void print_nid_path(struct hda_codec *codec,
351 const char *pfx, struct nid_path *path)
358 for (i = 0; i < path->depth; i++)
359 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
360 pos != buf ? ":" : "",
363 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
366 /* called recursively */
367 static bool __parse_nid_path(struct hda_codec *codec,
368 hda_nid_t from_nid, hda_nid_t to_nid,
369 int anchor_nid, struct nid_path *path,
372 const hda_nid_t *conn;
375 if (to_nid == anchor_nid)
376 anchor_nid = 0; /* anchor passed */
377 else if (to_nid == (hda_nid_t)(-anchor_nid))
378 return false; /* hit the exclusive nid */
380 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
381 for (i = 0; i < nums; i++) {
382 if (conn[i] != from_nid) {
383 /* special case: when from_nid is 0,
384 * try to find an empty DAC
387 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
388 is_dac_already_used(codec, conn[i]))
391 /* anchor is not requested or already passed? */
395 if (depth >= MAX_NID_PATH_DEPTH)
397 for (i = 0; i < nums; i++) {
399 type = get_wcaps_type(get_wcaps(codec, conn[i]));
400 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
403 if (__parse_nid_path(codec, from_nid, conn[i],
404 anchor_nid, path, depth + 1))
410 path->path[path->depth] = conn[i];
411 path->idx[path->depth + 1] = i;
412 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
413 path->multi[path->depth + 1] = 1;
418 /* parse the widget path from the given nid to the target nid;
419 * when @from_nid is 0, try to find an empty DAC;
420 * when @anchor_nid is set to a positive value, only paths through the widget
421 * with the given value are evaluated.
422 * when @anchor_nid is set to a negative value, paths through the widget
423 * with the negative of given value are excluded, only other paths are chosen.
424 * when @anchor_nid is zero, no special handling about path selection.
426 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
427 hda_nid_t to_nid, int anchor_nid,
428 struct nid_path *path)
430 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
431 path->path[path->depth] = to_nid;
437 EXPORT_SYMBOL_GPL(snd_hda_parse_nid_path);
440 * parse the path between the given NIDs and add to the path list.
441 * if no valid path is found, return NULL
444 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
445 hda_nid_t to_nid, int anchor_nid)
447 struct hda_gen_spec *spec = codec->spec;
448 struct nid_path *path;
450 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
453 /* check whether the path has been already added */
454 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
458 path = snd_array_new(&spec->paths);
461 memset(path, 0, sizeof(*path));
462 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
468 EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
470 /* clear the given path as invalid so that it won't be picked up later */
471 static void invalidate_nid_path(struct hda_codec *codec, int idx)
473 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
476 memset(path, 0, sizeof(*path));
479 /* return a DAC if paired to the given pin by codec driver */
480 static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
482 struct hda_gen_spec *spec = codec->spec;
483 const hda_nid_t *list = spec->preferred_dacs;
487 for (; *list; list += 2)
493 /* look for an empty DAC slot */
494 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
497 struct hda_gen_spec *spec = codec->spec;
501 for (i = 0; i < spec->num_all_dacs; i++) {
502 hda_nid_t nid = spec->all_dacs[i];
503 if (!nid || is_dac_already_used(codec, nid))
505 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
506 if (is_digital != cap_digital)
508 if (is_reachable_path(codec, nid, pin))
514 /* replace the channels in the composed amp value with the given number */
515 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
517 val &= ~(0x3U << 16);
522 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
523 hda_nid_t nid2, int dir)
525 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
526 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
527 return (query_amp_caps(codec, nid1, dir) ==
528 query_amp_caps(codec, nid2, dir));
531 /* look for a widget suitable for assigning a mute switch in the path */
532 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
533 struct nid_path *path)
537 for (i = path->depth - 1; i >= 0; i--) {
538 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
539 return path->path[i];
540 if (i != path->depth - 1 && i != 0 &&
541 nid_has_mute(codec, path->path[i], HDA_INPUT))
542 return path->path[i];
547 /* look for a widget suitable for assigning a volume ctl in the path */
548 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
549 struct nid_path *path)
551 struct hda_gen_spec *spec = codec->spec;
554 for (i = path->depth - 1; i >= 0; i--) {
555 hda_nid_t nid = path->path[i];
556 if ((spec->out_vol_mask >> nid) & 1)
558 if (nid_has_volume(codec, nid, HDA_OUTPUT))
565 * path activation / deactivation
568 /* can have the amp-in capability? */
569 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
571 hda_nid_t nid = path->path[idx];
572 unsigned int caps = get_wcaps(codec, nid);
573 unsigned int type = get_wcaps_type(caps);
575 if (!(caps & AC_WCAP_IN_AMP))
577 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
582 /* can have the amp-out capability? */
583 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
585 hda_nid_t nid = path->path[idx];
586 unsigned int caps = get_wcaps(codec, nid);
587 unsigned int type = get_wcaps_type(caps);
589 if (!(caps & AC_WCAP_OUT_AMP))
591 if (type == AC_WID_PIN && !idx) /* only for output pins */
596 /* check whether the given (nid,dir,idx) is active */
597 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
598 unsigned int dir, unsigned int idx)
600 struct hda_gen_spec *spec = codec->spec;
603 for (n = 0; n < spec->paths.used; n++) {
604 struct nid_path *path = snd_array_elem(&spec->paths, n);
607 for (i = 0; i < path->depth; i++) {
608 if (path->path[i] == nid) {
609 if (dir == HDA_OUTPUT || path->idx[i] == idx)
618 /* check whether the NID is referred by any active paths */
619 #define is_active_nid_for_any(codec, nid) \
620 is_active_nid(codec, nid, HDA_OUTPUT, 0)
622 /* get the default amp value for the target state */
623 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
624 int dir, unsigned int caps, bool enable)
626 unsigned int val = 0;
628 if (caps & AC_AMPCAP_NUM_STEPS) {
631 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
633 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
640 /* initialize the amp value (only at the first time) */
641 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
643 unsigned int caps = query_amp_caps(codec, nid, dir);
644 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
645 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
648 /* calculate amp value mask we can modify;
649 * if the given amp is controlled by mixers, don't touch it
651 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
652 hda_nid_t nid, int dir, int idx,
655 unsigned int mask = 0xff;
657 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
658 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
661 if (caps & AC_AMPCAP_NUM_STEPS) {
662 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
663 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
669 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
670 int idx, int idx_to_check, bool enable)
673 unsigned int mask, val;
675 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
678 caps = query_amp_caps(codec, nid, dir);
679 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
680 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
685 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
688 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
691 hda_nid_t nid = path->path[i];
692 init_amp(codec, nid, HDA_OUTPUT, 0);
693 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
696 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
697 int i, bool enable, bool add_aamix)
699 struct hda_gen_spec *spec = codec->spec;
700 const hda_nid_t *conn;
703 hda_nid_t nid = path->path[i];
705 nums = snd_hda_get_conn_list(codec, nid, &conn);
706 type = get_wcaps_type(get_wcaps(codec, nid));
707 if (type == AC_WID_PIN ||
708 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
714 for (n = 0; n < nums; n++)
715 init_amp(codec, nid, HDA_INPUT, n);
717 /* here is a little bit tricky in comparison with activate_amp_out();
718 * when aa-mixer is available, we need to enable the path as well
720 for (n = 0; n < nums; n++) {
721 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
723 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
727 /* activate or deactivate the given path
728 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
730 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
731 bool enable, bool add_aamix)
733 struct hda_gen_spec *spec = codec->spec;
737 path->active = false;
739 for (i = path->depth - 1; i >= 0; i--) {
740 hda_nid_t nid = path->path[i];
741 if (enable && spec->power_down_unused) {
742 /* make sure the widget is powered up */
743 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
744 snd_hda_codec_write(codec, nid, 0,
745 AC_VERB_SET_POWER_STATE,
748 if (enable && path->multi[i])
749 snd_hda_codec_update_cache(codec, nid, 0,
750 AC_VERB_SET_CONNECT_SEL,
752 if (has_amp_in(codec, path, i))
753 activate_amp_in(codec, path, i, enable, add_aamix);
754 if (has_amp_out(codec, path, i))
755 activate_amp_out(codec, path, i, enable);
761 EXPORT_SYMBOL_GPL(snd_hda_activate_path);
763 /* if the given path is inactive, put widgets into D3 (only if suitable) */
764 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
766 struct hda_gen_spec *spec = codec->spec;
767 bool changed = false;
770 if (!spec->power_down_unused || path->active)
773 for (i = 0; i < path->depth; i++) {
774 hda_nid_t nid = path->path[i];
775 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
776 !is_active_nid_for_any(codec, nid)) {
777 snd_hda_codec_write(codec, nid, 0,
778 AC_VERB_SET_POWER_STATE,
786 snd_hda_codec_read(codec, path->path[0], 0,
787 AC_VERB_GET_POWER_STATE, 0);
791 /* turn on/off EAPD on the given pin */
792 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
794 struct hda_gen_spec *spec = codec->spec;
795 if (spec->own_eapd_ctl ||
796 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
798 if (spec->keep_eapd_on && !enable)
802 snd_hda_codec_update_cache(codec, pin, 0,
803 AC_VERB_SET_EAPD_BTLENABLE,
804 enable ? 0x02 : 0x00);
807 /* re-initialize the path specified by the given path index */
808 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
810 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
812 snd_hda_activate_path(codec, path, path->active, false);
817 * Helper functions for creating mixer ctl elements
820 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
821 struct snd_ctl_elem_value *ucontrol);
822 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
823 struct snd_ctl_elem_value *ucontrol);
830 static const struct snd_kcontrol_new control_templates[] = {
831 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
832 /* only the put callback is replaced for handling the special mute */
834 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
835 .subdevice = HDA_SUBDEV_AMP_FLAG,
836 .info = snd_hda_mixer_amp_switch_info,
837 .get = snd_hda_mixer_amp_switch_get,
838 .put = hda_gen_mixer_mute_put, /* replaced */
839 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
842 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
843 .info = snd_hda_mixer_amp_switch_info,
844 .get = snd_hda_mixer_bind_switch_get,
845 .put = hda_gen_bind_mute_put, /* replaced */
846 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
850 /* add dynamic controls from template */
851 static struct snd_kcontrol_new *
852 add_control(struct hda_gen_spec *spec, int type, const char *name,
853 int cidx, unsigned long val)
855 struct snd_kcontrol_new *knew;
857 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
861 if (get_amp_nid_(val))
862 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
863 knew->private_value = val;
867 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
868 const char *pfx, const char *dir,
869 const char *sfx, int cidx, unsigned long val)
871 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
872 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
873 if (!add_control(spec, type, name, cidx, val))
878 #define add_pb_vol_ctrl(spec, type, pfx, val) \
879 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
880 #define add_pb_sw_ctrl(spec, type, pfx, val) \
881 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
882 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
883 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
884 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
885 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
887 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
888 unsigned int chs, struct nid_path *path)
893 val = path->ctls[NID_PATH_VOL_CTL];
896 val = amp_val_replace_channels(val, chs);
897 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
900 /* return the channel bits suitable for the given path->ctls[] */
901 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
904 int chs = 1; /* mono (left only) */
906 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
907 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
908 chs = 3; /* stereo */
913 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
914 struct nid_path *path)
916 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
917 return add_vol_ctl(codec, pfx, cidx, chs, path);
920 /* create a mute-switch for the given mixer widget;
921 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
923 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
924 unsigned int chs, struct nid_path *path)
927 int type = HDA_CTL_WIDGET_MUTE;
931 val = path->ctls[NID_PATH_MUTE_CTL];
934 val = amp_val_replace_channels(val, chs);
935 if (get_amp_direction_(val) == HDA_INPUT) {
936 hda_nid_t nid = get_amp_nid_(val);
937 int nums = snd_hda_get_num_conns(codec, nid);
939 type = HDA_CTL_BIND_MUTE;
943 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
946 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
947 int cidx, struct nid_path *path)
949 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
950 return add_sw_ctl(codec, pfx, cidx, chs, path);
953 /* playback mute control with the software mute bit check */
954 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
955 struct snd_ctl_elem_value *ucontrol)
957 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
958 struct hda_gen_spec *spec = codec->spec;
960 if (spec->auto_mute_via_amp) {
961 hda_nid_t nid = get_amp_nid(kcontrol);
962 bool enabled = !((spec->mute_bits >> nid) & 1);
963 ucontrol->value.integer.value[0] &= enabled;
964 ucontrol->value.integer.value[1] &= enabled;
968 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
969 struct snd_ctl_elem_value *ucontrol)
971 sync_auto_mute_bits(kcontrol, ucontrol);
972 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
975 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
976 struct snd_ctl_elem_value *ucontrol)
978 sync_auto_mute_bits(kcontrol, ucontrol);
979 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
982 /* any ctl assigned to the path with the given index? */
983 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
985 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
986 return path && path->ctls[ctl_type];
989 static const char * const channel_name[4] = {
990 "Front", "Surround", "CLFE", "Side"
993 /* give some appropriate ctl name prefix for the given line out channel */
994 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
995 int *index, int ctl_type)
997 struct hda_gen_spec *spec = codec->spec;
998 struct auto_pin_cfg *cfg = &spec->autocfg;
1001 if (cfg->line_outs == 1 && !spec->multi_ios &&
1002 !cfg->hp_outs && !cfg->speaker_outs)
1003 return spec->vmaster_mute.hook ? "PCM" : "Master";
1005 /* if there is really a single DAC used in the whole output paths,
1006 * use it master (or "PCM" if a vmaster hook is present)
1008 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1009 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1010 return spec->vmaster_mute.hook ? "PCM" : "Master";
1012 /* multi-io channels */
1013 if (ch >= cfg->line_outs)
1014 return channel_name[ch];
1016 switch (cfg->line_out_type) {
1017 case AUTO_PIN_SPEAKER_OUT:
1018 /* if the primary channel vol/mute is shared with HP volume,
1019 * don't name it as Speaker
1021 if (!ch && cfg->hp_outs &&
1022 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1024 if (cfg->line_outs == 1)
1026 if (cfg->line_outs == 2)
1027 return ch ? "Bass Speaker" : "Speaker";
1029 case AUTO_PIN_HP_OUT:
1030 /* if the primary channel vol/mute is shared with spk volume,
1031 * don't name it as Headphone
1033 if (!ch && cfg->speaker_outs &&
1034 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1036 /* for multi-io case, only the primary out */
1037 if (ch && spec->multi_ios)
1043 /* for a single channel output, we don't have to name the channel */
1044 if (cfg->line_outs == 1 && !spec->multi_ios)
1047 if (ch >= ARRAY_SIZE(channel_name)) {
1052 return channel_name[ch];
1056 * Parse output paths
1059 /* badness definition */
1061 /* No primary DAC is found for the main output */
1062 BAD_NO_PRIMARY_DAC = 0x10000,
1063 /* No DAC is found for the extra output */
1064 BAD_NO_DAC = 0x4000,
1065 /* No possible multi-ios */
1066 BAD_MULTI_IO = 0x120,
1067 /* No individual DAC for extra output */
1068 BAD_NO_EXTRA_DAC = 0x102,
1069 /* No individual DAC for extra surrounds */
1070 BAD_NO_EXTRA_SURR_DAC = 0x101,
1071 /* Primary DAC shared with main surrounds */
1072 BAD_SHARED_SURROUND = 0x100,
1073 /* No independent HP possible */
1074 BAD_NO_INDEP_HP = 0x10,
1075 /* Primary DAC shared with main CLFE */
1076 BAD_SHARED_CLFE = 0x10,
1077 /* Primary DAC shared with extra surrounds */
1078 BAD_SHARED_EXTRA_SURROUND = 0x10,
1079 /* Volume widget is shared */
1080 BAD_SHARED_VOL = 0x10,
1083 /* look for widgets in the given path which are appropriate for
1084 * volume and mute controls, and assign the values to ctls[].
1086 * When no appropriate widget is found in the path, the badness value
1087 * is incremented depending on the situation. The function returns the
1088 * total badness for both volume and mute controls.
1090 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1092 struct hda_gen_spec *spec = codec->spec;
1098 return BAD_SHARED_VOL * 2;
1100 if (path->ctls[NID_PATH_VOL_CTL] ||
1101 path->ctls[NID_PATH_MUTE_CTL])
1102 return 0; /* already evaluated */
1104 nid = look_for_out_vol_nid(codec, path);
1106 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1107 if (spec->dac_min_mute)
1108 val |= HDA_AMP_VAL_MIN_MUTE;
1109 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1110 badness += BAD_SHARED_VOL;
1112 path->ctls[NID_PATH_VOL_CTL] = val;
1114 badness += BAD_SHARED_VOL;
1115 nid = look_for_out_mute_nid(codec, path);
1117 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1118 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1119 nid_has_mute(codec, nid, HDA_OUTPUT))
1120 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1122 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1123 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1124 badness += BAD_SHARED_VOL;
1126 path->ctls[NID_PATH_MUTE_CTL] = val;
1128 badness += BAD_SHARED_VOL;
1132 const struct badness_table hda_main_out_badness = {
1133 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1134 .no_dac = BAD_NO_DAC,
1135 .shared_primary = BAD_NO_PRIMARY_DAC,
1136 .shared_surr = BAD_SHARED_SURROUND,
1137 .shared_clfe = BAD_SHARED_CLFE,
1138 .shared_surr_main = BAD_SHARED_SURROUND,
1140 EXPORT_SYMBOL_GPL(hda_main_out_badness);
1142 const struct badness_table hda_extra_out_badness = {
1143 .no_primary_dac = BAD_NO_DAC,
1144 .no_dac = BAD_NO_DAC,
1145 .shared_primary = BAD_NO_EXTRA_DAC,
1146 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1147 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1148 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1150 EXPORT_SYMBOL_GPL(hda_extra_out_badness);
1152 /* get the DAC of the primary output corresponding to the given array index */
1153 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1155 struct hda_gen_spec *spec = codec->spec;
1156 struct auto_pin_cfg *cfg = &spec->autocfg;
1158 if (cfg->line_outs > idx)
1159 return spec->private_dac_nids[idx];
1160 idx -= cfg->line_outs;
1161 if (spec->multi_ios > idx)
1162 return spec->multi_io[idx].dac;
1166 /* return the DAC if it's reachable, otherwise zero */
1167 static inline hda_nid_t try_dac(struct hda_codec *codec,
1168 hda_nid_t dac, hda_nid_t pin)
1170 return is_reachable_path(codec, dac, pin) ? dac : 0;
1173 /* try to assign DACs to pins and return the resultant badness */
1174 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1175 const hda_nid_t *pins, hda_nid_t *dacs,
1177 const struct badness_table *bad)
1179 struct hda_gen_spec *spec = codec->spec;
1187 for (i = 0; i < num_outs; i++) {
1188 struct nid_path *path;
1189 hda_nid_t pin = pins[i];
1191 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1193 badness += assign_out_path_ctls(codec, path);
1197 dacs[i] = get_preferred_dac(codec, pin);
1199 if (is_dac_already_used(codec, dacs[i]))
1200 badness += bad->shared_primary;
1204 dacs[i] = look_for_dac(codec, pin, false);
1205 if (!dacs[i] && !i) {
1206 /* try to steal the DAC of surrounds for the front */
1207 for (j = 1; j < num_outs; j++) {
1208 if (is_reachable_path(codec, dacs[j], pin)) {
1211 invalidate_nid_path(codec, path_idx[j]);
1220 dac = try_dac(codec, get_primary_out(codec, i), pin);
1222 dac = try_dac(codec, dacs[0], pin);
1224 dac = try_dac(codec, get_primary_out(codec, i), pin);
1227 badness += bad->shared_primary;
1229 badness += bad->shared_surr;
1231 badness += bad->shared_clfe;
1232 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1233 dac = spec->private_dac_nids[0];
1234 badness += bad->shared_surr_main;
1236 badness += bad->no_primary_dac;
1238 badness += bad->no_dac;
1242 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1243 if (!path && !i && spec->mixer_nid) {
1244 /* try with aamix */
1245 path = snd_hda_add_new_path(codec, dac, pin, 0);
1249 badness += bad->no_dac;
1251 /* print_nid_path(codec, "output", path); */
1252 path->active = true;
1253 path_idx[i] = snd_hda_get_path_idx(codec, path);
1254 badness += assign_out_path_ctls(codec, path);
1261 /* return NID if the given pin has only a single connection to a certain DAC */
1262 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1264 struct hda_gen_spec *spec = codec->spec;
1266 hda_nid_t nid_found = 0;
1268 for (i = 0; i < spec->num_all_dacs; i++) {
1269 hda_nid_t nid = spec->all_dacs[i];
1270 if (!nid || is_dac_already_used(codec, nid))
1272 if (is_reachable_path(codec, nid, pin)) {
1281 /* check whether the given pin can be a multi-io pin */
1282 static bool can_be_multiio_pin(struct hda_codec *codec,
1283 unsigned int location, hda_nid_t nid)
1285 unsigned int defcfg, caps;
1287 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1288 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1290 if (location && get_defcfg_location(defcfg) != location)
1292 caps = snd_hda_query_pin_caps(codec, nid);
1293 if (!(caps & AC_PINCAP_OUT))
1298 /* count the number of input pins that are capable to be multi-io */
1299 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1301 struct hda_gen_spec *spec = codec->spec;
1302 struct auto_pin_cfg *cfg = &spec->autocfg;
1303 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1304 unsigned int location = get_defcfg_location(defcfg);
1308 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1309 for (i = 0; i < cfg->num_inputs; i++) {
1310 if (cfg->inputs[i].type != type)
1312 if (can_be_multiio_pin(codec, location,
1313 cfg->inputs[i].pin))
1323 * When hardwired is set, try to fill ony hardwired pins, and returns
1324 * zero if any pins are filled, non-zero if nothing found.
1325 * When hardwired is off, try to fill possible input pins, and returns
1326 * the badness value.
1328 static int fill_multi_ios(struct hda_codec *codec,
1329 hda_nid_t reference_pin,
1332 struct hda_gen_spec *spec = codec->spec;
1333 struct auto_pin_cfg *cfg = &spec->autocfg;
1334 int type, i, j, num_pins, old_pins;
1335 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1336 unsigned int location = get_defcfg_location(defcfg);
1338 struct nid_path *path;
1340 old_pins = spec->multi_ios;
1344 num_pins = count_multiio_pins(codec, reference_pin);
1348 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1349 for (i = 0; i < cfg->num_inputs; i++) {
1350 hda_nid_t nid = cfg->inputs[i].pin;
1353 if (cfg->inputs[i].type != type)
1355 if (!can_be_multiio_pin(codec, location, nid))
1357 for (j = 0; j < spec->multi_ios; j++) {
1358 if (nid == spec->multi_io[j].pin)
1361 if (j < spec->multi_ios)
1365 dac = get_dac_if_single(codec, nid);
1367 dac = look_for_dac(codec, nid, false);
1372 path = snd_hda_add_new_path(codec, dac, nid,
1378 /* print_nid_path(codec, "multiio", path); */
1379 spec->multi_io[spec->multi_ios].pin = nid;
1380 spec->multi_io[spec->multi_ios].dac = dac;
1381 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1382 snd_hda_get_path_idx(codec, path);
1384 if (spec->multi_ios >= 2)
1390 badness = BAD_MULTI_IO;
1391 if (old_pins == spec->multi_ios) {
1393 return 1; /* nothing found */
1395 return badness; /* no badness if nothing found */
1397 if (!hardwired && spec->multi_ios < 2) {
1398 /* cancel newly assigned paths */
1399 spec->paths.used -= spec->multi_ios - old_pins;
1400 spec->multi_ios = old_pins;
1404 /* assign volume and mute controls */
1405 for (i = old_pins; i < spec->multi_ios; i++) {
1406 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1407 badness += assign_out_path_ctls(codec, path);
1413 /* map DACs for all pins in the list if they are single connections */
1414 static bool map_singles(struct hda_codec *codec, int outs,
1415 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1417 struct hda_gen_spec *spec = codec->spec;
1420 for (i = 0; i < outs; i++) {
1421 struct nid_path *path;
1425 dac = get_dac_if_single(codec, pins[i]);
1428 path = snd_hda_add_new_path(codec, dac, pins[i],
1430 if (!path && !i && spec->mixer_nid)
1431 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1435 /* print_nid_path(codec, "output", path); */
1436 path->active = true;
1437 path_idx[i] = snd_hda_get_path_idx(codec, path);
1443 /* create a new path including aamix if available, and return its index */
1444 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1446 struct hda_gen_spec *spec = codec->spec;
1447 struct nid_path *path;
1448 hda_nid_t path_dac, dac, pin;
1450 path = snd_hda_get_path_from_idx(codec, path_idx);
1451 if (!path || !path->depth ||
1452 is_nid_contained(path, spec->mixer_nid))
1454 path_dac = path->path[0];
1455 dac = spec->private_dac_nids[0];
1456 pin = path->path[path->depth - 1];
1457 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1459 if (dac != path_dac)
1461 else if (spec->multiout.hp_out_nid[0])
1462 dac = spec->multiout.hp_out_nid[0];
1463 else if (spec->multiout.extra_out_nid[0])
1464 dac = spec->multiout.extra_out_nid[0];
1468 path = snd_hda_add_new_path(codec, dac, pin,
1473 /* print_nid_path(codec, "output-aamix", path); */
1474 path->active = false; /* unused as default */
1475 return snd_hda_get_path_idx(codec, path);
1478 /* check whether the independent HP is available with the current config */
1479 static bool indep_hp_possible(struct hda_codec *codec)
1481 struct hda_gen_spec *spec = codec->spec;
1482 struct auto_pin_cfg *cfg = &spec->autocfg;
1483 struct nid_path *path;
1486 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1487 idx = spec->out_paths[0];
1489 idx = spec->hp_paths[0];
1490 path = snd_hda_get_path_from_idx(codec, idx);
1494 /* assume no path conflicts unless aamix is involved */
1495 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1498 /* check whether output paths contain aamix */
1499 for (i = 0; i < cfg->line_outs; i++) {
1500 if (spec->out_paths[i] == idx)
1502 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1503 if (path && is_nid_contained(path, spec->mixer_nid))
1506 for (i = 0; i < cfg->speaker_outs; i++) {
1507 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1508 if (path && is_nid_contained(path, spec->mixer_nid))
1515 /* fill the empty entries in the dac array for speaker/hp with the
1516 * shared dac pointed by the paths
1518 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1519 hda_nid_t *dacs, int *path_idx)
1521 struct nid_path *path;
1524 for (i = 0; i < num_outs; i++) {
1527 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1530 dacs[i] = path->path[0];
1534 /* fill in the dac_nids table from the parsed pin configuration */
1535 static int fill_and_eval_dacs(struct hda_codec *codec,
1536 bool fill_hardwired,
1537 bool fill_mio_first)
1539 struct hda_gen_spec *spec = codec->spec;
1540 struct auto_pin_cfg *cfg = &spec->autocfg;
1541 int i, err, badness;
1543 /* set num_dacs once to full for look_for_dac() */
1544 spec->multiout.num_dacs = cfg->line_outs;
1545 spec->multiout.dac_nids = spec->private_dac_nids;
1546 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1547 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1548 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1549 spec->multi_ios = 0;
1550 snd_array_free(&spec->paths);
1552 /* clear path indices */
1553 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1554 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1555 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1556 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1557 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1558 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1559 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1560 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1564 /* fill hard-wired DACs first */
1565 if (fill_hardwired) {
1568 mapped = map_singles(codec, cfg->line_outs,
1570 spec->private_dac_nids,
1572 mapped |= map_singles(codec, cfg->hp_outs,
1574 spec->multiout.hp_out_nid,
1576 mapped |= map_singles(codec, cfg->speaker_outs,
1578 spec->multiout.extra_out_nid,
1579 spec->speaker_paths);
1580 if (!spec->no_multi_io &&
1581 fill_mio_first && cfg->line_outs == 1 &&
1582 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1583 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1590 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1591 spec->private_dac_nids, spec->out_paths,
1592 spec->main_out_badness);
1594 if (!spec->no_multi_io && fill_mio_first &&
1595 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1596 /* try to fill multi-io first */
1597 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1600 /* we don't count badness at this stage yet */
1603 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1604 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1605 spec->multiout.hp_out_nid,
1607 spec->extra_out_badness);
1612 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1613 err = try_assign_dacs(codec, cfg->speaker_outs,
1615 spec->multiout.extra_out_nid,
1616 spec->speaker_paths,
1617 spec->extra_out_badness);
1622 if (!spec->no_multi_io &&
1623 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1624 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1630 if (spec->mixer_nid) {
1631 spec->aamix_out_paths[0] =
1632 check_aamix_out_path(codec, spec->out_paths[0]);
1633 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1634 spec->aamix_out_paths[1] =
1635 check_aamix_out_path(codec, spec->hp_paths[0]);
1636 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1637 spec->aamix_out_paths[2] =
1638 check_aamix_out_path(codec, spec->speaker_paths[0]);
1641 if (!spec->no_multi_io &&
1642 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1643 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1644 spec->multi_ios = 1; /* give badness */
1646 /* re-count num_dacs and squash invalid entries */
1647 spec->multiout.num_dacs = 0;
1648 for (i = 0; i < cfg->line_outs; i++) {
1649 if (spec->private_dac_nids[i])
1650 spec->multiout.num_dacs++;
1652 memmove(spec->private_dac_nids + i,
1653 spec->private_dac_nids + i + 1,
1654 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1655 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1659 spec->ext_channel_count = spec->min_channel_count =
1660 spec->multiout.num_dacs * 2;
1662 if (spec->multi_ios == 2) {
1663 for (i = 0; i < 2; i++)
1664 spec->private_dac_nids[spec->multiout.num_dacs++] =
1665 spec->multi_io[i].dac;
1666 } else if (spec->multi_ios) {
1667 spec->multi_ios = 0;
1668 badness += BAD_MULTI_IO;
1671 if (spec->indep_hp && !indep_hp_possible(codec))
1672 badness += BAD_NO_INDEP_HP;
1674 /* re-fill the shared DAC for speaker / headphone */
1675 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1676 refill_shared_dacs(codec, cfg->hp_outs,
1677 spec->multiout.hp_out_nid,
1679 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1680 refill_shared_dacs(codec, cfg->speaker_outs,
1681 spec->multiout.extra_out_nid,
1682 spec->speaker_paths);
1687 #define DEBUG_BADNESS
1689 #ifdef DEBUG_BADNESS
1690 #define debug_badness(fmt, ...) \
1691 codec_dbg(codec, fmt, ##__VA_ARGS__)
1693 #define debug_badness(fmt, ...) \
1694 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1697 #ifdef DEBUG_BADNESS
1698 static inline void print_nid_path_idx(struct hda_codec *codec,
1699 const char *pfx, int idx)
1701 struct nid_path *path;
1703 path = snd_hda_get_path_from_idx(codec, idx);
1705 print_nid_path(codec, pfx, path);
1708 static void debug_show_configs(struct hda_codec *codec,
1709 struct auto_pin_cfg *cfg)
1711 struct hda_gen_spec *spec = codec->spec;
1712 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1715 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1716 cfg->line_out_pins[0], cfg->line_out_pins[1],
1717 cfg->line_out_pins[2], cfg->line_out_pins[3],
1718 spec->multiout.dac_nids[0],
1719 spec->multiout.dac_nids[1],
1720 spec->multiout.dac_nids[2],
1721 spec->multiout.dac_nids[3],
1722 lo_type[cfg->line_out_type]);
1723 for (i = 0; i < cfg->line_outs; i++)
1724 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1725 if (spec->multi_ios > 0)
1726 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1728 spec->multi_io[0].pin, spec->multi_io[1].pin,
1729 spec->multi_io[0].dac, spec->multi_io[1].dac);
1730 for (i = 0; i < spec->multi_ios; i++)
1731 print_nid_path_idx(codec, " mio",
1732 spec->out_paths[cfg->line_outs + i]);
1734 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1735 cfg->hp_pins[0], cfg->hp_pins[1],
1736 cfg->hp_pins[2], cfg->hp_pins[3],
1737 spec->multiout.hp_out_nid[0],
1738 spec->multiout.hp_out_nid[1],
1739 spec->multiout.hp_out_nid[2],
1740 spec->multiout.hp_out_nid[3]);
1741 for (i = 0; i < cfg->hp_outs; i++)
1742 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1743 if (cfg->speaker_outs)
1744 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1745 cfg->speaker_pins[0], cfg->speaker_pins[1],
1746 cfg->speaker_pins[2], cfg->speaker_pins[3],
1747 spec->multiout.extra_out_nid[0],
1748 spec->multiout.extra_out_nid[1],
1749 spec->multiout.extra_out_nid[2],
1750 spec->multiout.extra_out_nid[3]);
1751 for (i = 0; i < cfg->speaker_outs; i++)
1752 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1753 for (i = 0; i < 3; i++)
1754 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1757 #define debug_show_configs(codec, cfg) /* NOP */
1760 /* find all available DACs of the codec */
1761 static void fill_all_dac_nids(struct hda_codec *codec)
1763 struct hda_gen_spec *spec = codec->spec;
1765 hda_nid_t nid = codec->start_nid;
1767 spec->num_all_dacs = 0;
1768 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1769 for (i = 0; i < codec->num_nodes; i++, nid++) {
1770 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1772 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1773 codec_err(codec, "Too many DACs!\n");
1776 spec->all_dacs[spec->num_all_dacs++] = nid;
1780 static int parse_output_paths(struct hda_codec *codec)
1782 struct hda_gen_spec *spec = codec->spec;
1783 struct auto_pin_cfg *cfg = &spec->autocfg;
1784 struct auto_pin_cfg *best_cfg;
1786 int best_badness = INT_MAX;
1788 bool fill_hardwired = true, fill_mio_first = true;
1789 bool best_wired = true, best_mio = true;
1790 bool hp_spk_swapped = false;
1792 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1798 badness = fill_and_eval_dacs(codec, fill_hardwired,
1804 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1805 cfg->line_out_type, fill_hardwired, fill_mio_first,
1807 debug_show_configs(codec, cfg);
1808 if (badness < best_badness) {
1809 best_badness = badness;
1811 best_wired = fill_hardwired;
1812 best_mio = fill_mio_first;
1816 fill_mio_first = !fill_mio_first;
1817 if (!fill_mio_first)
1819 fill_hardwired = !fill_hardwired;
1820 if (!fill_hardwired)
1824 hp_spk_swapped = true;
1825 if (cfg->speaker_outs > 0 &&
1826 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1827 cfg->hp_outs = cfg->line_outs;
1828 memcpy(cfg->hp_pins, cfg->line_out_pins,
1829 sizeof(cfg->hp_pins));
1830 cfg->line_outs = cfg->speaker_outs;
1831 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1832 sizeof(cfg->speaker_pins));
1833 cfg->speaker_outs = 0;
1834 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1835 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1836 fill_hardwired = true;
1839 if (cfg->hp_outs > 0 &&
1840 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1841 cfg->speaker_outs = cfg->line_outs;
1842 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1843 sizeof(cfg->speaker_pins));
1844 cfg->line_outs = cfg->hp_outs;
1845 memcpy(cfg->line_out_pins, cfg->hp_pins,
1846 sizeof(cfg->hp_pins));
1848 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1849 cfg->line_out_type = AUTO_PIN_HP_OUT;
1850 fill_hardwired = true;
1857 debug_badness("==> restoring best_cfg\n");
1859 fill_and_eval_dacs(codec, best_wired, best_mio);
1861 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1862 cfg->line_out_type, best_wired, best_mio);
1863 debug_show_configs(codec, cfg);
1865 if (cfg->line_out_pins[0]) {
1866 struct nid_path *path;
1867 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1869 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1870 if (spec->vmaster_nid) {
1871 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1872 HDA_OUTPUT, spec->vmaster_tlv);
1873 if (spec->dac_min_mute)
1874 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
1878 /* set initial pinctl targets */
1879 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1883 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1884 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1885 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1886 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1887 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1888 set_pin_targets(codec, cfg->speaker_outs,
1889 cfg->speaker_pins, val);
1892 /* clear indep_hp flag if not available */
1893 if (spec->indep_hp && !indep_hp_possible(codec))
1900 /* add playback controls from the parsed DAC table */
1901 static int create_multi_out_ctls(struct hda_codec *codec,
1902 const struct auto_pin_cfg *cfg)
1904 struct hda_gen_spec *spec = codec->spec;
1905 int i, err, noutputs;
1907 noutputs = cfg->line_outs;
1908 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1909 noutputs += spec->multi_ios;
1911 for (i = 0; i < noutputs; i++) {
1914 struct nid_path *path;
1916 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1920 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
1921 if (!name || !strcmp(name, "CLFE")) {
1923 err = add_vol_ctl(codec, "Center", 0, 1, path);
1926 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1930 err = add_stereo_vol(codec, name, index, path);
1935 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1936 if (!name || !strcmp(name, "CLFE")) {
1937 err = add_sw_ctl(codec, "Center", 0, 1, path);
1940 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1944 err = add_stereo_sw(codec, name, index, path);
1952 static int create_extra_out(struct hda_codec *codec, int path_idx,
1953 const char *pfx, int cidx)
1955 struct nid_path *path;
1958 path = snd_hda_get_path_from_idx(codec, path_idx);
1961 err = add_stereo_vol(codec, pfx, cidx, path);
1964 err = add_stereo_sw(codec, pfx, cidx, path);
1970 /* add playback controls for speaker and HP outputs */
1971 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1972 const int *paths, const char *pfx)
1976 for (i = 0; i < num_pins; i++) {
1978 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1981 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1982 name = "Bass Speaker";
1983 else if (num_pins >= 3) {
1984 snprintf(tmp, sizeof(tmp), "%s %s",
1985 pfx, channel_name[i]);
1991 err = create_extra_out(codec, paths[i], name, idx);
1998 static int create_hp_out_ctls(struct hda_codec *codec)
2000 struct hda_gen_spec *spec = codec->spec;
2001 return create_extra_outs(codec, spec->autocfg.hp_outs,
2006 static int create_speaker_out_ctls(struct hda_codec *codec)
2008 struct hda_gen_spec *spec = codec->spec;
2009 return create_extra_outs(codec, spec->autocfg.speaker_outs,
2010 spec->speaker_paths,
2015 * independent HP controls
2018 static void call_hp_automute(struct hda_codec *codec,
2019 struct hda_jack_callback *jack);
2020 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2021 struct snd_ctl_elem_info *uinfo)
2023 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2026 static int indep_hp_get(struct snd_kcontrol *kcontrol,
2027 struct snd_ctl_elem_value *ucontrol)
2029 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2030 struct hda_gen_spec *spec = codec->spec;
2031 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2035 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2036 int nomix_path_idx, int mix_path_idx,
2039 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2040 struct snd_ctl_elem_value *ucontrol)
2042 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2043 struct hda_gen_spec *spec = codec->spec;
2044 unsigned int select = ucontrol->value.enumerated.item[0];
2047 mutex_lock(&spec->pcm_mutex);
2048 if (spec->active_streams) {
2053 if (spec->indep_hp_enabled != select) {
2055 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2056 dacp = &spec->private_dac_nids[0];
2058 dacp = &spec->multiout.hp_out_nid[0];
2060 /* update HP aamix paths in case it conflicts with indep HP */
2061 if (spec->have_aamix_ctl) {
2062 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2063 update_aamix_paths(codec, spec->aamix_mode,
2065 spec->aamix_out_paths[0],
2066 spec->autocfg.line_out_type);
2068 update_aamix_paths(codec, spec->aamix_mode,
2070 spec->aamix_out_paths[1],
2074 spec->indep_hp_enabled = select;
2075 if (spec->indep_hp_enabled)
2078 *dacp = spec->alt_dac_nid;
2080 call_hp_automute(codec, NULL);
2084 mutex_unlock(&spec->pcm_mutex);
2088 static const struct snd_kcontrol_new indep_hp_ctl = {
2089 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2090 .name = "Independent HP",
2091 .info = indep_hp_info,
2092 .get = indep_hp_get,
2093 .put = indep_hp_put,
2097 static int create_indep_hp_ctls(struct hda_codec *codec)
2099 struct hda_gen_spec *spec = codec->spec;
2102 if (!spec->indep_hp)
2104 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2105 dac = spec->multiout.dac_nids[0];
2107 dac = spec->multiout.hp_out_nid[0];
2113 spec->indep_hp_enabled = false;
2114 spec->alt_dac_nid = dac;
2115 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2121 * channel mode enum control
2124 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2125 struct snd_ctl_elem_info *uinfo)
2127 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2128 struct hda_gen_spec *spec = codec->spec;
2131 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2133 uinfo->value.enumerated.items = spec->multi_ios + 1;
2134 if (uinfo->value.enumerated.item > spec->multi_ios)
2135 uinfo->value.enumerated.item = spec->multi_ios;
2136 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2137 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2141 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2142 struct snd_ctl_elem_value *ucontrol)
2144 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2145 struct hda_gen_spec *spec = codec->spec;
2146 ucontrol->value.enumerated.item[0] =
2147 (spec->ext_channel_count - spec->min_channel_count) / 2;
2151 static inline struct nid_path *
2152 get_multiio_path(struct hda_codec *codec, int idx)
2154 struct hda_gen_spec *spec = codec->spec;
2155 return snd_hda_get_path_from_idx(codec,
2156 spec->out_paths[spec->autocfg.line_outs + idx]);
2159 static void update_automute_all(struct hda_codec *codec);
2161 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2162 * used for output paths
2164 static bool aamix_default(struct hda_gen_spec *spec)
2166 return !spec->have_aamix_ctl || spec->aamix_mode;
2169 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2171 struct hda_gen_spec *spec = codec->spec;
2172 hda_nid_t nid = spec->multi_io[idx].pin;
2173 struct nid_path *path;
2175 path = get_multiio_path(codec, idx);
2179 if (path->active == output)
2183 set_pin_target(codec, nid, PIN_OUT, true);
2184 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2185 set_pin_eapd(codec, nid, true);
2187 set_pin_eapd(codec, nid, false);
2188 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2189 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2190 path_power_down_sync(codec, path);
2193 /* update jack retasking in case it modifies any of them */
2194 update_automute_all(codec);
2199 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2200 struct snd_ctl_elem_value *ucontrol)
2202 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2203 struct hda_gen_spec *spec = codec->spec;
2206 ch = ucontrol->value.enumerated.item[0];
2207 if (ch < 0 || ch > spec->multi_ios)
2209 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2211 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2212 for (i = 0; i < spec->multi_ios; i++)
2213 set_multi_io(codec, i, i < ch);
2214 spec->multiout.max_channels = max(spec->ext_channel_count,
2215 spec->const_channel_count);
2216 if (spec->need_dac_fix)
2217 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2221 static const struct snd_kcontrol_new channel_mode_enum = {
2222 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2223 .name = "Channel Mode",
2224 .info = ch_mode_info,
2229 static int create_multi_channel_mode(struct hda_codec *codec)
2231 struct hda_gen_spec *spec = codec->spec;
2233 if (spec->multi_ios > 0) {
2234 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2241 * aamix loopback enable/disable switch
2244 #define loopback_mixing_info indep_hp_info
2246 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2247 struct snd_ctl_elem_value *ucontrol)
2249 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2250 struct hda_gen_spec *spec = codec->spec;
2251 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2255 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2256 int nomix_path_idx, int mix_path_idx,
2259 struct hda_gen_spec *spec = codec->spec;
2260 struct nid_path *nomix_path, *mix_path;
2262 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2263 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2264 if (!nomix_path || !mix_path)
2267 /* if HP aamix path is driven from a different DAC and the
2268 * independent HP mode is ON, can't turn on aamix path
2270 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2271 mix_path->path[0] != spec->alt_dac_nid)
2275 snd_hda_activate_path(codec, nomix_path, false, true);
2276 snd_hda_activate_path(codec, mix_path, true, true);
2277 path_power_down_sync(codec, nomix_path);
2279 snd_hda_activate_path(codec, mix_path, false, false);
2280 snd_hda_activate_path(codec, nomix_path, true, false);
2281 path_power_down_sync(codec, mix_path);
2285 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2286 struct snd_ctl_elem_value *ucontrol)
2288 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2289 struct hda_gen_spec *spec = codec->spec;
2290 unsigned int val = ucontrol->value.enumerated.item[0];
2292 if (val == spec->aamix_mode)
2294 spec->aamix_mode = val;
2295 update_aamix_paths(codec, val, spec->out_paths[0],
2296 spec->aamix_out_paths[0],
2297 spec->autocfg.line_out_type);
2298 update_aamix_paths(codec, val, spec->hp_paths[0],
2299 spec->aamix_out_paths[1],
2301 update_aamix_paths(codec, val, spec->speaker_paths[0],
2302 spec->aamix_out_paths[2],
2303 AUTO_PIN_SPEAKER_OUT);
2307 static const struct snd_kcontrol_new loopback_mixing_enum = {
2308 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2309 .name = "Loopback Mixing",
2310 .info = loopback_mixing_info,
2311 .get = loopback_mixing_get,
2312 .put = loopback_mixing_put,
2315 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2317 struct hda_gen_spec *spec = codec->spec;
2319 if (!spec->mixer_nid)
2321 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2322 spec->aamix_out_paths[2]))
2324 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2326 spec->have_aamix_ctl = 1;
2331 * shared headphone/mic handling
2334 static void call_update_outputs(struct hda_codec *codec);
2336 /* for shared I/O, change the pin-control accordingly */
2337 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2339 struct hda_gen_spec *spec = codec->spec;
2344 pin = spec->hp_mic_pin;
2345 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2348 val = snd_hda_codec_get_pin_target(codec, pin);
2358 val = snd_hda_get_default_vref(codec, pin);
2359 /* if the HP pin doesn't support VREF and the codec driver gives an
2360 * alternative pin, set up the VREF on that pin instead
2362 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2363 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2364 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2365 if (vref_val != AC_PINCTL_VREF_HIZ)
2366 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2367 PIN_IN | (as_mic ? vref_val : 0));
2370 if (!spec->hp_mic_jack_modes) {
2375 set_pin_target(codec, pin, val, true);
2376 call_hp_automute(codec, NULL);
2380 /* create a shared input with the headphone out */
2381 static int create_hp_mic(struct hda_codec *codec)
2383 struct hda_gen_spec *spec = codec->spec;
2384 struct auto_pin_cfg *cfg = &spec->autocfg;
2385 unsigned int defcfg;
2388 if (!spec->hp_mic) {
2389 if (spec->suppress_hp_mic_detect)
2391 /* automatic detection: only if no input or a single internal
2392 * input pin is found, try to detect the shared hp/mic
2394 if (cfg->num_inputs > 1)
2396 else if (cfg->num_inputs == 1) {
2397 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2398 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2403 spec->hp_mic = 0; /* clear once */
2404 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2408 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2409 nid = cfg->line_out_pins[0];
2410 else if (cfg->hp_outs > 0)
2411 nid = cfg->hp_pins[0];
2415 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2416 return 0; /* no input */
2418 cfg->inputs[cfg->num_inputs].pin = nid;
2419 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2420 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2423 spec->hp_mic_pin = nid;
2424 /* we can't handle auto-mic together with HP-mic */
2425 spec->suppress_auto_mic = 1;
2426 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
2434 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2436 static const char * const out_jack_texts[] = {
2437 "Line Out", "Headphone Out",
2440 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2441 struct snd_ctl_elem_info *uinfo)
2443 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2446 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2447 struct snd_ctl_elem_value *ucontrol)
2449 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2450 hda_nid_t nid = kcontrol->private_value;
2451 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2452 ucontrol->value.enumerated.item[0] = 1;
2454 ucontrol->value.enumerated.item[0] = 0;
2458 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2459 struct snd_ctl_elem_value *ucontrol)
2461 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2462 hda_nid_t nid = kcontrol->private_value;
2465 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2466 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2468 snd_hda_set_pin_ctl_cache(codec, nid, val);
2472 static const struct snd_kcontrol_new out_jack_mode_enum = {
2473 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2474 .info = out_jack_mode_info,
2475 .get = out_jack_mode_get,
2476 .put = out_jack_mode_put,
2479 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2481 struct hda_gen_spec *spec = codec->spec;
2484 for (i = 0; i < spec->kctls.used; i++) {
2485 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2486 if (!strcmp(kctl->name, name) && kctl->index == idx)
2492 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2493 char *name, size_t name_len)
2495 struct hda_gen_spec *spec = codec->spec;
2498 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2499 strlcat(name, " Jack Mode", name_len);
2501 for (; find_kctl_name(codec, name, idx); idx++)
2505 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2507 struct hda_gen_spec *spec = codec->spec;
2508 if (spec->add_jack_modes) {
2509 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2510 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2516 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2519 struct hda_gen_spec *spec = codec->spec;
2522 for (i = 0; i < num_pins; i++) {
2523 hda_nid_t pin = pins[i];
2524 if (pin == spec->hp_mic_pin)
2526 if (get_out_jack_num_items(codec, pin) > 1) {
2527 struct snd_kcontrol_new *knew;
2528 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2529 get_jack_mode_name(codec, pin, name, sizeof(name));
2530 knew = snd_hda_gen_add_kctl(spec, name,
2531 &out_jack_mode_enum);
2534 knew->private_value = pin;
2545 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2548 static const char * const vref_texts[NUM_VREFS] = {
2549 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2550 "", "Mic 80pc Bias", "Mic 100pc Bias"
2553 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2555 unsigned int pincap;
2557 pincap = snd_hda_query_pin_caps(codec, pin);
2558 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2559 /* filter out unusual vrefs */
2560 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2564 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2565 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2567 unsigned int i, n = 0;
2569 for (i = 0; i < NUM_VREFS; i++) {
2570 if (vref_caps & (1 << i)) {
2579 /* convert back from the vref ctl index to the enum item index */
2580 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2582 unsigned int i, n = 0;
2584 for (i = 0; i < NUM_VREFS; i++) {
2587 if (vref_caps & (1 << i))
2593 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2594 struct snd_ctl_elem_info *uinfo)
2596 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2597 hda_nid_t nid = kcontrol->private_value;
2598 unsigned int vref_caps = get_vref_caps(codec, nid);
2600 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2602 /* set the right text */
2603 strcpy(uinfo->value.enumerated.name,
2604 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2608 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2609 struct snd_ctl_elem_value *ucontrol)
2611 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2612 hda_nid_t nid = kcontrol->private_value;
2613 unsigned int vref_caps = get_vref_caps(codec, nid);
2616 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2617 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2621 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2622 struct snd_ctl_elem_value *ucontrol)
2624 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2625 hda_nid_t nid = kcontrol->private_value;
2626 unsigned int vref_caps = get_vref_caps(codec, nid);
2627 unsigned int val, idx;
2629 val = snd_hda_codec_get_pin_target(codec, nid);
2630 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2631 if (idx == ucontrol->value.enumerated.item[0])
2634 val &= ~AC_PINCTL_VREFEN;
2635 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2636 snd_hda_set_pin_ctl_cache(codec, nid, val);
2640 static const struct snd_kcontrol_new in_jack_mode_enum = {
2641 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2642 .info = in_jack_mode_info,
2643 .get = in_jack_mode_get,
2644 .put = in_jack_mode_put,
2647 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2649 struct hda_gen_spec *spec = codec->spec;
2651 if (spec->add_jack_modes)
2652 nitems = hweight32(get_vref_caps(codec, pin));
2653 return nitems ? nitems : 1;
2656 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2658 struct hda_gen_spec *spec = codec->spec;
2659 struct snd_kcontrol_new *knew;
2660 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2661 unsigned int defcfg;
2663 if (pin == spec->hp_mic_pin)
2664 return 0; /* already done in create_out_jack_mode() */
2666 /* no jack mode for fixed pins */
2667 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2668 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2671 /* no multiple vref caps? */
2672 if (get_in_jack_num_items(codec, pin) <= 1)
2675 get_jack_mode_name(codec, pin, name, sizeof(name));
2676 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2679 knew->private_value = pin;
2684 * HP/mic shared jack mode
2686 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2687 struct snd_ctl_elem_info *uinfo)
2689 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2690 hda_nid_t nid = kcontrol->private_value;
2691 int out_jacks = get_out_jack_num_items(codec, nid);
2692 int in_jacks = get_in_jack_num_items(codec, nid);
2693 const char *text = NULL;
2696 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2698 uinfo->value.enumerated.items = out_jacks + in_jacks;
2699 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2700 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2701 idx = uinfo->value.enumerated.item;
2702 if (idx < out_jacks) {
2704 text = out_jack_texts[idx];
2706 text = "Headphone Out";
2710 unsigned int vref_caps = get_vref_caps(codec, nid);
2711 text = vref_texts[get_vref_idx(vref_caps, idx)];
2716 strcpy(uinfo->value.enumerated.name, text);
2720 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2722 int out_jacks = get_out_jack_num_items(codec, nid);
2723 int in_jacks = get_in_jack_num_items(codec, nid);
2724 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2727 if (val & PIN_OUT) {
2728 if (out_jacks > 1 && val == PIN_HP)
2730 } else if (val & PIN_IN) {
2733 unsigned int vref_caps = get_vref_caps(codec, nid);
2734 val &= AC_PINCTL_VREFEN;
2735 idx += cvt_from_vref_idx(vref_caps, val);
2741 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2742 struct snd_ctl_elem_value *ucontrol)
2744 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2745 hda_nid_t nid = kcontrol->private_value;
2746 ucontrol->value.enumerated.item[0] =
2747 get_cur_hp_mic_jack_mode(codec, nid);
2751 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2752 struct snd_ctl_elem_value *ucontrol)
2754 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2755 hda_nid_t nid = kcontrol->private_value;
2756 int out_jacks = get_out_jack_num_items(codec, nid);
2757 int in_jacks = get_in_jack_num_items(codec, nid);
2758 unsigned int val, oldval, idx;
2760 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2761 idx = ucontrol->value.enumerated.item[0];
2765 if (idx < out_jacks) {
2767 val = idx ? PIN_HP : PIN_OUT;
2773 unsigned int vref_caps = get_vref_caps(codec, nid);
2774 val = snd_hda_codec_get_pin_target(codec, nid);
2775 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2776 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
2778 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
2780 snd_hda_set_pin_ctl_cache(codec, nid, val);
2781 call_hp_automute(codec, NULL);
2786 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2787 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2788 .info = hp_mic_jack_mode_info,
2789 .get = hp_mic_jack_mode_get,
2790 .put = hp_mic_jack_mode_put,
2793 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2795 struct hda_gen_spec *spec = codec->spec;
2796 struct snd_kcontrol_new *knew;
2798 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2799 &hp_mic_jack_mode_enum);
2802 knew->private_value = pin;
2803 spec->hp_mic_jack_modes = 1;
2811 /* add the powersave loopback-list entry */
2812 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2814 struct hda_amp_list *list;
2816 list = snd_array_new(&spec->loopback_list);
2820 list->dir = HDA_INPUT;
2822 spec->loopback.amplist = spec->loopback_list.list;
2826 /* return true if either a volume or a mute amp is found for the given
2827 * aamix path; the amp has to be either in the mixer node or its direct leaf
2829 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2830 hda_nid_t pin, unsigned int *mix_val,
2831 unsigned int *mute_val)
2834 const hda_nid_t *list;
2837 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2841 *mix_val = *mute_val = 0;
2842 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2843 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2844 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2845 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2846 if (*mix_val && *mute_val)
2849 /* check leaf node */
2850 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2851 if (num_conns < idx)
2854 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2855 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
2856 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2857 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2858 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
2859 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2861 return *mix_val || *mute_val;
2864 /* create input playback/capture controls for the given pin */
2865 static int new_analog_input(struct hda_codec *codec, int input_idx,
2866 hda_nid_t pin, const char *ctlname, int ctlidx,
2869 struct hda_gen_spec *spec = codec->spec;
2870 struct nid_path *path;
2871 unsigned int mix_val, mute_val;
2874 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
2877 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
2880 print_nid_path(codec, "loopback", path);
2881 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
2883 idx = path->idx[path->depth - 1];
2885 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
2888 path->ctls[NID_PATH_VOL_CTL] = mix_val;
2892 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
2895 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
2898 path->active = true;
2899 err = add_loopback_list(spec, mix_nid, idx);
2903 if (spec->mixer_nid != spec->mixer_merge_nid &&
2904 !spec->loopback_merge_path) {
2905 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2906 spec->mixer_merge_nid, 0);
2908 print_nid_path(codec, "loopback-merge", path);
2909 path->active = true;
2910 spec->loopback_merge_path =
2911 snd_hda_get_path_idx(codec, path);
2918 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2920 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2921 return (pincap & AC_PINCAP_IN) != 0;
2924 /* Parse the codec tree and retrieve ADCs */
2925 static int fill_adc_nids(struct hda_codec *codec)
2927 struct hda_gen_spec *spec = codec->spec;
2929 hda_nid_t *adc_nids = spec->adc_nids;
2930 int max_nums = ARRAY_SIZE(spec->adc_nids);
2933 nid = codec->start_nid;
2934 for (i = 0; i < codec->num_nodes; i++, nid++) {
2935 unsigned int caps = get_wcaps(codec, nid);
2936 int type = get_wcaps_type(caps);
2938 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2940 adc_nids[nums] = nid;
2941 if (++nums >= max_nums)
2944 spec->num_adc_nids = nums;
2946 /* copy the detected ADCs to all_adcs[] */
2947 spec->num_all_adcs = nums;
2948 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2953 /* filter out invalid adc_nids that don't give all active input pins;
2954 * if needed, check whether dynamic ADC-switching is available
2956 static int check_dyn_adc_switch(struct hda_codec *codec)
2958 struct hda_gen_spec *spec = codec->spec;
2959 struct hda_input_mux *imux = &spec->input_mux;
2960 unsigned int ok_bits;
2965 for (n = 0; n < spec->num_adc_nids; n++) {
2966 for (i = 0; i < imux->num_items; i++) {
2967 if (!spec->input_paths[i][n])
2970 if (i >= imux->num_items) {
2971 ok_bits |= (1 << n);
2977 /* check whether ADC-switch is possible */
2978 for (i = 0; i < imux->num_items; i++) {
2979 for (n = 0; n < spec->num_adc_nids; n++) {
2980 if (spec->input_paths[i][n]) {
2981 spec->dyn_adc_idx[i] = n;
2987 codec_dbg(codec, "enabling ADC switching\n");
2988 spec->dyn_adc_switch = 1;
2989 } else if (nums != spec->num_adc_nids) {
2990 /* shrink the invalid adcs and input paths */
2992 for (n = 0; n < spec->num_adc_nids; n++) {
2993 if (!(ok_bits & (1 << n)))
2996 spec->adc_nids[nums] = spec->adc_nids[n];
2997 for (i = 0; i < imux->num_items; i++) {
2998 invalidate_nid_path(codec,
2999 spec->input_paths[i][nums]);
3000 spec->input_paths[i][nums] =
3001 spec->input_paths[i][n];
3006 spec->num_adc_nids = nums;
3009 if (imux->num_items == 1 ||
3010 (imux->num_items == 2 && spec->hp_mic)) {
3011 codec_dbg(codec, "reducing to a single ADC\n");
3012 spec->num_adc_nids = 1; /* reduce to a single ADC */
3015 /* single index for individual volumes ctls */
3016 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3017 spec->num_adc_nids = 1;
3022 /* parse capture source paths from the given pin and create imux items */
3023 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3024 int cfg_idx, int num_adcs,
3025 const char *label, int anchor)
3027 struct hda_gen_spec *spec = codec->spec;
3028 struct hda_input_mux *imux = &spec->input_mux;
3029 int imux_idx = imux->num_items;
3030 bool imux_added = false;
3033 for (c = 0; c < num_adcs; c++) {
3034 struct nid_path *path;
3035 hda_nid_t adc = spec->adc_nids[c];
3037 if (!is_reachable_path(codec, pin, adc))
3039 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3042 print_nid_path(codec, "input", path);
3043 spec->input_paths[imux_idx][c] =
3044 snd_hda_get_path_idx(codec, path);
3047 if (spec->hp_mic_pin == pin)
3048 spec->hp_mic_mux_idx = imux->num_items;
3049 spec->imux_pins[imux->num_items] = pin;
3050 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3052 if (spec->dyn_adc_switch)
3053 spec->dyn_adc_idx[imux_idx] = c;
3061 * create playback/capture controls for input pins
3064 /* fill the label for each input at first */
3065 static int fill_input_pin_labels(struct hda_codec *codec)
3067 struct hda_gen_spec *spec = codec->spec;
3068 const struct auto_pin_cfg *cfg = &spec->autocfg;
3071 for (i = 0; i < cfg->num_inputs; i++) {
3072 hda_nid_t pin = cfg->inputs[i].pin;
3076 if (!is_input_pin(codec, pin))
3079 label = hda_get_autocfg_input_label(codec, cfg, i);
3081 for (j = i - 1; j >= 0; j--) {
3082 if (spec->input_labels[j] &&
3083 !strcmp(spec->input_labels[j], label)) {
3084 idx = spec->input_label_idxs[j] + 1;
3089 spec->input_labels[i] = label;
3090 spec->input_label_idxs[i] = idx;
3096 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3098 static int create_input_ctls(struct hda_codec *codec)
3100 struct hda_gen_spec *spec = codec->spec;
3101 const struct auto_pin_cfg *cfg = &spec->autocfg;
3102 hda_nid_t mixer = spec->mixer_nid;
3107 num_adcs = fill_adc_nids(codec);
3111 err = fill_input_pin_labels(codec);
3115 for (i = 0; i < cfg->num_inputs; i++) {
3118 pin = cfg->inputs[i].pin;
3119 if (!is_input_pin(codec, pin))
3123 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3124 val |= snd_hda_get_default_vref(codec, pin);
3125 if (pin != spec->hp_mic_pin)
3126 set_pin_target(codec, pin, val, false);
3129 if (is_reachable_path(codec, pin, mixer)) {
3130 err = new_analog_input(codec, i, pin,
3131 spec->input_labels[i],
3132 spec->input_label_idxs[i],
3139 err = parse_capture_source(codec, pin, i, num_adcs,
3140 spec->input_labels[i], -mixer);
3144 if (spec->add_jack_modes) {
3145 err = create_in_jack_mode(codec, pin);
3151 /* add stereo mix when explicitly enabled via hint */
3152 if (mixer && spec->add_stereo_mix_input &&
3153 snd_hda_get_bool_hint(codec, "add_stereo_mix_input") > 0) {
3154 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3168 /* get the input path specified by the given adc and imux indices */
3169 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3171 struct hda_gen_spec *spec = codec->spec;
3172 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3176 if (spec->dyn_adc_switch)
3177 adc_idx = spec->dyn_adc_idx[imux_idx];
3178 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3182 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3185 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3188 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3189 struct snd_ctl_elem_info *uinfo)
3191 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3192 struct hda_gen_spec *spec = codec->spec;
3193 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3196 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3197 struct snd_ctl_elem_value *ucontrol)
3199 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3200 struct hda_gen_spec *spec = codec->spec;
3201 /* the ctls are created at once with multiple counts */
3202 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3204 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3208 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3209 struct snd_ctl_elem_value *ucontrol)
3211 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3212 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3213 return mux_select(codec, adc_idx,
3214 ucontrol->value.enumerated.item[0]);
3217 static const struct snd_kcontrol_new cap_src_temp = {
3218 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3219 .name = "Input Source",
3220 .info = mux_enum_info,
3221 .get = mux_enum_get,
3222 .put = mux_enum_put,
3226 * capture volume and capture switch ctls
3229 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3230 struct snd_ctl_elem_value *ucontrol);
3232 /* call the given amp update function for all amps in the imux list at once */
3233 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3234 struct snd_ctl_elem_value *ucontrol,
3235 put_call_t func, int type)
3237 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3238 struct hda_gen_spec *spec = codec->spec;
3239 const struct hda_input_mux *imux;
3240 struct nid_path *path;
3241 int i, adc_idx, err = 0;
3243 imux = &spec->input_mux;
3244 adc_idx = kcontrol->id.index;
3245 mutex_lock(&codec->control_mutex);
3246 /* we use the cache-only update at first since multiple input paths
3247 * may shared the same amp; by updating only caches, the redundant
3248 * writes to hardware can be reduced.
3250 codec->cached_write = 1;
3251 for (i = 0; i < imux->num_items; i++) {
3252 path = get_input_path(codec, adc_idx, i);
3253 if (!path || !path->ctls[type])
3255 kcontrol->private_value = path->ctls[type];
3256 err = func(kcontrol, ucontrol);
3261 codec->cached_write = 0;
3262 mutex_unlock(&codec->control_mutex);
3263 snd_hda_codec_flush_cache(codec); /* flush the updates */
3264 if (err >= 0 && spec->cap_sync_hook)
3265 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3269 /* capture volume ctl callbacks */
3270 #define cap_vol_info snd_hda_mixer_amp_volume_info
3271 #define cap_vol_get snd_hda_mixer_amp_volume_get
3272 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3274 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3275 struct snd_ctl_elem_value *ucontrol)
3277 return cap_put_caller(kcontrol, ucontrol,
3278 snd_hda_mixer_amp_volume_put,
3282 static const struct snd_kcontrol_new cap_vol_temp = {
3283 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3284 .name = "Capture Volume",
3285 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3286 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3287 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3288 .info = cap_vol_info,
3291 .tlv = { .c = cap_vol_tlv },
3294 /* capture switch ctl callbacks */
3295 #define cap_sw_info snd_ctl_boolean_stereo_info
3296 #define cap_sw_get snd_hda_mixer_amp_switch_get
3298 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3299 struct snd_ctl_elem_value *ucontrol)
3301 return cap_put_caller(kcontrol, ucontrol,
3302 snd_hda_mixer_amp_switch_put,
3306 static const struct snd_kcontrol_new cap_sw_temp = {
3307 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3308 .name = "Capture Switch",
3309 .info = cap_sw_info,
3314 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3319 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3320 for (depth = 0; depth < 3; depth++) {
3321 if (depth >= path->depth)
3323 i = path->depth - depth - 1;
3324 nid = path->path[i];
3325 if (!path->ctls[NID_PATH_VOL_CTL]) {
3326 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3327 path->ctls[NID_PATH_VOL_CTL] =
3328 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3329 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3330 int idx = path->idx[i];
3331 if (!depth && codec->single_adc_amp)
3333 path->ctls[NID_PATH_VOL_CTL] =
3334 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3337 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3338 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3339 path->ctls[NID_PATH_MUTE_CTL] =
3340 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3341 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3342 int idx = path->idx[i];
3343 if (!depth && codec->single_adc_amp)
3345 path->ctls[NID_PATH_MUTE_CTL] =
3346 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3353 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3355 struct hda_gen_spec *spec = codec->spec;
3356 struct auto_pin_cfg *cfg = &spec->autocfg;
3360 if (!spec->inv_dmic_split)
3362 for (i = 0; i < cfg->num_inputs; i++) {
3363 if (cfg->inputs[i].pin != nid)
3365 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3367 val = snd_hda_codec_get_pincfg(codec, nid);
3368 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3373 /* capture switch put callback for a single control with hook call */
3374 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3375 struct snd_ctl_elem_value *ucontrol)
3377 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3378 struct hda_gen_spec *spec = codec->spec;
3381 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3385 if (spec->cap_sync_hook)
3386 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3391 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3392 int idx, bool is_switch, unsigned int ctl,
3395 struct hda_gen_spec *spec = codec->spec;
3396 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3397 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3398 const char *sfx = is_switch ? "Switch" : "Volume";
3399 unsigned int chs = inv_dmic ? 1 : 3;
3400 struct snd_kcontrol_new *knew;
3406 snprintf(tmpname, sizeof(tmpname),
3407 "%s Capture %s", label, sfx);
3409 snprintf(tmpname, sizeof(tmpname),
3411 knew = add_control(spec, type, tmpname, idx,
3412 amp_val_replace_channels(ctl, chs));
3416 knew->put = cap_single_sw_put;
3420 /* Make independent right kcontrol */
3422 snprintf(tmpname, sizeof(tmpname),
3423 "Inverted %s Capture %s", label, sfx);
3425 snprintf(tmpname, sizeof(tmpname),
3426 "Inverted Capture %s", sfx);
3427 knew = add_control(spec, type, tmpname, idx,
3428 amp_val_replace_channels(ctl, 2));
3432 knew->put = cap_single_sw_put;
3436 /* create single (and simple) capture volume and switch controls */
3437 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3438 unsigned int vol_ctl, unsigned int sw_ctl,
3442 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3445 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3451 /* create bound capture volume and switch controls */
3452 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3453 unsigned int vol_ctl, unsigned int sw_ctl)
3455 struct hda_gen_spec *spec = codec->spec;
3456 struct snd_kcontrol_new *knew;
3459 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3463 knew->private_value = vol_ctl;
3464 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3467 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3471 knew->private_value = sw_ctl;
3472 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3477 /* return the vol ctl when used first in the imux list */
3478 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3480 struct nid_path *path;
3484 path = get_input_path(codec, 0, idx);
3487 ctl = path->ctls[type];
3490 for (i = 0; i < idx - 1; i++) {
3491 path = get_input_path(codec, 0, i);
3492 if (path && path->ctls[type] == ctl)
3498 /* create individual capture volume and switch controls per input */
3499 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3501 struct hda_gen_spec *spec = codec->spec;
3502 struct hda_input_mux *imux = &spec->input_mux;
3505 for (i = 0; i < imux->num_items; i++) {
3509 idx = imux->items[i].index;
3510 if (idx >= spec->autocfg.num_inputs)
3512 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3514 for (type = 0; type < 2; type++) {
3515 err = add_single_cap_ctl(codec,
3516 spec->input_labels[idx],
3517 spec->input_label_idxs[idx],
3519 get_first_cap_ctl(codec, i, type),
3528 static int create_capture_mixers(struct hda_codec *codec)
3530 struct hda_gen_spec *spec = codec->spec;
3531 struct hda_input_mux *imux = &spec->input_mux;
3532 int i, n, nums, err;
3534 if (spec->dyn_adc_switch)
3537 nums = spec->num_adc_nids;
3539 if (!spec->auto_mic && imux->num_items > 1) {
3540 struct snd_kcontrol_new *knew;
3542 name = nums > 1 ? "Input Source" : "Capture Source";
3543 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3549 for (n = 0; n < nums; n++) {
3551 bool multi_cap_vol = spec->multi_cap_vol;
3552 bool inv_dmic = false;
3556 for (i = 0; i < imux->num_items; i++) {
3557 struct nid_path *path;
3558 path = get_input_path(codec, n, i);
3561 parse_capvol_in_path(codec, path);
3563 vol = path->ctls[NID_PATH_VOL_CTL];
3564 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3566 if (!same_amp_caps(codec, vol,
3567 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3568 multi_cap_vol = true;
3571 sw = path->ctls[NID_PATH_MUTE_CTL];
3572 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3574 if (!same_amp_caps(codec, sw,
3575 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3576 multi_cap_vol = true;
3578 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3583 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3585 else if (!multi_cap_vol && !inv_dmic)
3586 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3588 err = create_multi_cap_vol_ctl(codec);
3597 * add mic boosts if needed
3600 /* check whether the given amp is feasible as a boost volume */
3601 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3606 if (!nid_has_volume(codec, nid, dir) ||
3607 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3608 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3611 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3612 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3618 /* look for a boost amp in a widget close to the pin */
3619 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3620 struct nid_path *path)
3622 unsigned int val = 0;
3626 for (depth = 0; depth < 3; depth++) {
3627 if (depth >= path->depth - 1)
3629 nid = path->path[depth];
3630 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3631 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3633 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3634 path->idx[depth])) {
3635 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3644 static int parse_mic_boost(struct hda_codec *codec)
3646 struct hda_gen_spec *spec = codec->spec;
3647 struct auto_pin_cfg *cfg = &spec->autocfg;
3648 struct hda_input_mux *imux = &spec->input_mux;
3651 if (!spec->num_adc_nids)
3654 for (i = 0; i < imux->num_items; i++) {
3655 struct nid_path *path;
3658 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3660 idx = imux->items[i].index;
3661 if (idx >= imux->num_items)
3664 /* check only line-in and mic pins */
3665 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3668 path = get_input_path(codec, 0, i);
3672 val = look_for_boost_amp(codec, path);
3676 /* create a boost control */
3677 snprintf(boost_label, sizeof(boost_label),
3678 "%s Boost Volume", spec->input_labels[idx]);
3679 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3680 spec->input_label_idxs[idx], val))
3683 path->ctls[NID_PATH_BOOST_CTL] = val;
3689 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3691 static void parse_digital(struct hda_codec *codec)
3693 struct hda_gen_spec *spec = codec->spec;
3694 struct nid_path *path;
3696 hda_nid_t dig_nid, pin;
3698 /* support multiple SPDIFs; the secondary is set up as a slave */
3700 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3701 pin = spec->autocfg.dig_out_pins[i];
3702 dig_nid = look_for_dac(codec, pin, true);
3705 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3708 print_nid_path(codec, "digout", path);
3709 path->active = true;
3710 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3711 set_pin_target(codec, pin, PIN_OUT, false);
3713 spec->multiout.dig_out_nid = dig_nid;
3714 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3716 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3717 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3719 spec->slave_dig_outs[nums - 1] = dig_nid;
3724 if (spec->autocfg.dig_in_pin) {
3725 pin = spec->autocfg.dig_in_pin;
3726 dig_nid = codec->start_nid;
3727 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3728 unsigned int wcaps = get_wcaps(codec, dig_nid);
3729 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3731 if (!(wcaps & AC_WCAP_DIGITAL))
3733 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3735 print_nid_path(codec, "digin", path);
3736 path->active = true;
3737 spec->dig_in_nid = dig_nid;
3738 spec->digin_path = snd_hda_get_path_idx(codec, path);
3739 set_pin_target(codec, pin, PIN_IN, false);
3748 * input MUX handling
3751 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3753 /* select the given imux item; either unmute exclusively or select the route */
3754 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3757 struct hda_gen_spec *spec = codec->spec;
3758 const struct hda_input_mux *imux;
3759 struct nid_path *old_path, *path;
3761 imux = &spec->input_mux;
3762 if (!imux->num_items)
3765 if (idx >= imux->num_items)
3766 idx = imux->num_items - 1;
3767 if (spec->cur_mux[adc_idx] == idx)
3770 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3773 if (old_path->active)
3774 snd_hda_activate_path(codec, old_path, false, false);
3776 spec->cur_mux[adc_idx] = idx;
3779 update_hp_mic(codec, adc_idx, false);
3781 if (spec->dyn_adc_switch)
3782 dyn_adc_pcm_resetup(codec, idx);
3784 path = get_input_path(codec, adc_idx, idx);
3789 snd_hda_activate_path(codec, path, true, false);
3790 if (spec->cap_sync_hook)
3791 spec->cap_sync_hook(codec, NULL, NULL);
3792 path_power_down_sync(codec, old_path);
3798 * Jack detections for HP auto-mute and mic-switch
3801 /* check each pin in the given array; returns true if any of them is plugged */
3802 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3805 bool present = false;
3807 for (i = 0; i < num_pins; i++) {
3808 hda_nid_t nid = pins[i];
3811 /* don't detect pins retasked as inputs */
3812 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3814 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3820 /* standard HP/line-out auto-mute helper */
3821 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
3822 int *paths, bool mute)
3824 struct hda_gen_spec *spec = codec->spec;
3827 for (i = 0; i < num_pins; i++) {
3828 hda_nid_t nid = pins[i];
3829 unsigned int val, oldval;
3833 if (spec->auto_mute_via_amp) {
3834 struct nid_path *path;
3837 path = snd_hda_get_path_from_idx(codec, paths[i]);
3840 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
3844 spec->mute_bits |= (1ULL << mute_nid);
3846 spec->mute_bits &= ~(1ULL << mute_nid);
3847 set_pin_eapd(codec, nid, !mute);
3851 oldval = snd_hda_codec_get_pin_target(codec, nid);
3852 if (oldval & PIN_IN)
3853 continue; /* no mute for inputs */
3854 /* don't reset VREF value in case it's controlling
3855 * the amp (see alc861_fixup_asus_amp_vref_0f())
3857 if (spec->keep_vref_in_automute)
3858 val = oldval & ~PIN_HP;
3863 /* here we call update_pin_ctl() so that the pinctl is changed
3864 * without changing the pinctl target value;
3865 * the original target value will be still referred at the
3866 * init / resume again
3868 update_pin_ctl(codec, nid, val);
3869 set_pin_eapd(codec, nid, !mute);
3873 /* Toggle outputs muting */
3874 void snd_hda_gen_update_outputs(struct hda_codec *codec)
3876 struct hda_gen_spec *spec = codec->spec;
3880 /* Control HP pins/amps depending on master_mute state;
3881 * in general, HP pins/amps control should be enabled in all cases,
3882 * but currently set only for master_mute, just to be safe
3884 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3885 paths = spec->out_paths;
3887 paths = spec->hp_paths;
3888 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3889 spec->autocfg.hp_pins, paths, spec->master_mute);
3891 if (!spec->automute_speaker)
3894 on = spec->hp_jack_present | spec->line_jack_present;
3895 on |= spec->master_mute;
3896 spec->speaker_muted = on;
3897 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3898 paths = spec->out_paths;
3900 paths = spec->speaker_paths;
3901 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
3902 spec->autocfg.speaker_pins, paths, on);
3904 /* toggle line-out mutes if needed, too */
3905 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3906 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3907 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3909 if (!spec->automute_lo)
3912 on = spec->hp_jack_present;
3913 on |= spec->master_mute;
3914 spec->line_out_muted = on;
3915 paths = spec->out_paths;
3916 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3917 spec->autocfg.line_out_pins, paths, on);
3919 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
3921 static void call_update_outputs(struct hda_codec *codec)
3923 struct hda_gen_spec *spec = codec->spec;
3924 if (spec->automute_hook)
3925 spec->automute_hook(codec);
3927 snd_hda_gen_update_outputs(codec);
3929 /* sync the whole vmaster slaves to reflect the new auto-mute status */
3930 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
3931 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
3934 /* standard HP-automute helper */
3935 void snd_hda_gen_hp_automute(struct hda_codec *codec,
3936 struct hda_jack_callback *jack)
3938 struct hda_gen_spec *spec = codec->spec;
3939 hda_nid_t *pins = spec->autocfg.hp_pins;
3940 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
3942 /* No detection for the first HP jack during indep-HP mode */
3943 if (spec->indep_hp_enabled) {
3948 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
3949 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3951 call_update_outputs(codec);
3953 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
3955 /* standard line-out-automute helper */
3956 void snd_hda_gen_line_automute(struct hda_codec *codec,
3957 struct hda_jack_callback *jack)
3959 struct hda_gen_spec *spec = codec->spec;
3961 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3963 /* check LO jack only when it's different from HP */
3964 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3967 spec->line_jack_present =
3968 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3969 spec->autocfg.line_out_pins);
3970 if (!spec->automute_speaker || !spec->detect_lo)
3972 call_update_outputs(codec);
3974 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
3976 /* standard mic auto-switch helper */
3977 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
3978 struct hda_jack_callback *jack)
3980 struct hda_gen_spec *spec = codec->spec;
3983 if (!spec->auto_mic)
3986 for (i = spec->am_num_entries - 1; i > 0; i--) {
3987 hda_nid_t pin = spec->am_entry[i].pin;
3988 /* don't detect pins retasked as outputs */
3989 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3991 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
3992 mux_select(codec, 0, spec->am_entry[i].idx);
3996 mux_select(codec, 0, spec->am_entry[0].idx);
3998 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4000 /* call appropriate hooks */
4001 static void call_hp_automute(struct hda_codec *codec,
4002 struct hda_jack_callback *jack)
4004 struct hda_gen_spec *spec = codec->spec;
4005 if (spec->hp_automute_hook)
4006 spec->hp_automute_hook(codec, jack);
4008 snd_hda_gen_hp_automute(codec, jack);
4011 static void call_line_automute(struct hda_codec *codec,
4012 struct hda_jack_callback *jack)
4014 struct hda_gen_spec *spec = codec->spec;
4015 if (spec->line_automute_hook)
4016 spec->line_automute_hook(codec, jack);
4018 snd_hda_gen_line_automute(codec, jack);
4021 static void call_mic_autoswitch(struct hda_codec *codec,
4022 struct hda_jack_callback *jack)
4024 struct hda_gen_spec *spec = codec->spec;
4025 if (spec->mic_autoswitch_hook)
4026 spec->mic_autoswitch_hook(codec, jack);
4028 snd_hda_gen_mic_autoswitch(codec, jack);
4031 /* update jack retasking */
4032 static void update_automute_all(struct hda_codec *codec)
4034 call_hp_automute(codec, NULL);
4035 call_line_automute(codec, NULL);
4036 call_mic_autoswitch(codec, NULL);
4040 * Auto-Mute mode mixer enum support
4042 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4043 struct snd_ctl_elem_info *uinfo)
4045 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4046 struct hda_gen_spec *spec = codec->spec;
4047 static const char * const texts3[] = {
4048 "Disabled", "Speaker Only", "Line Out+Speaker"
4051 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4052 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4053 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4056 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4057 struct snd_ctl_elem_value *ucontrol)
4059 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4060 struct hda_gen_spec *spec = codec->spec;
4061 unsigned int val = 0;
4062 if (spec->automute_speaker)
4064 if (spec->automute_lo)
4067 ucontrol->value.enumerated.item[0] = val;
4071 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4072 struct snd_ctl_elem_value *ucontrol)
4074 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4075 struct hda_gen_spec *spec = codec->spec;
4077 switch (ucontrol->value.enumerated.item[0]) {
4079 if (!spec->automute_speaker && !spec->automute_lo)
4081 spec->automute_speaker = 0;
4082 spec->automute_lo = 0;
4085 if (spec->automute_speaker_possible) {
4086 if (!spec->automute_lo && spec->automute_speaker)
4088 spec->automute_speaker = 1;
4089 spec->automute_lo = 0;
4090 } else if (spec->automute_lo_possible) {
4091 if (spec->automute_lo)
4093 spec->automute_lo = 1;
4098 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4100 if (spec->automute_speaker && spec->automute_lo)
4102 spec->automute_speaker = 1;
4103 spec->automute_lo = 1;
4108 call_update_outputs(codec);
4112 static const struct snd_kcontrol_new automute_mode_enum = {
4113 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4114 .name = "Auto-Mute Mode",
4115 .info = automute_mode_info,
4116 .get = automute_mode_get,
4117 .put = automute_mode_put,
4120 static int add_automute_mode_enum(struct hda_codec *codec)
4122 struct hda_gen_spec *spec = codec->spec;
4124 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4130 * Check the availability of HP/line-out auto-mute;
4131 * Set up appropriately if really supported
4133 static int check_auto_mute_availability(struct hda_codec *codec)
4135 struct hda_gen_spec *spec = codec->spec;
4136 struct auto_pin_cfg *cfg = &spec->autocfg;
4140 if (spec->suppress_auto_mute)
4143 if (cfg->hp_pins[0])
4145 if (cfg->line_out_pins[0])
4147 if (cfg->speaker_pins[0])
4149 if (present < 2) /* need two different output types */
4152 if (!cfg->speaker_pins[0] &&
4153 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4154 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4155 sizeof(cfg->speaker_pins));
4156 cfg->speaker_outs = cfg->line_outs;
4159 if (!cfg->hp_pins[0] &&
4160 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4161 memcpy(cfg->hp_pins, cfg->line_out_pins,
4162 sizeof(cfg->hp_pins));
4163 cfg->hp_outs = cfg->line_outs;
4166 for (i = 0; i < cfg->hp_outs; i++) {
4167 hda_nid_t nid = cfg->hp_pins[i];
4168 if (!is_jack_detectable(codec, nid))
4170 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4171 snd_hda_jack_detect_enable_callback(codec, nid,
4173 spec->detect_hp = 1;
4176 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4177 if (cfg->speaker_outs)
4178 for (i = 0; i < cfg->line_outs; i++) {
4179 hda_nid_t nid = cfg->line_out_pins[i];
4180 if (!is_jack_detectable(codec, nid))
4182 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4183 snd_hda_jack_detect_enable_callback(codec, nid,
4184 call_line_automute);
4185 spec->detect_lo = 1;
4187 spec->automute_lo_possible = spec->detect_hp;
4190 spec->automute_speaker_possible = cfg->speaker_outs &&
4191 (spec->detect_hp || spec->detect_lo);
4193 spec->automute_lo = spec->automute_lo_possible;
4194 spec->automute_speaker = spec->automute_speaker_possible;
4196 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4197 /* create a control for automute mode */
4198 err = add_automute_mode_enum(codec);
4205 /* check whether all auto-mic pins are valid; setup indices if OK */
4206 static bool auto_mic_check_imux(struct hda_codec *codec)
4208 struct hda_gen_spec *spec = codec->spec;
4209 const struct hda_input_mux *imux;
4212 imux = &spec->input_mux;
4213 for (i = 0; i < spec->am_num_entries; i++) {
4214 spec->am_entry[i].idx =
4215 find_idx_in_nid_list(spec->am_entry[i].pin,
4216 spec->imux_pins, imux->num_items);
4217 if (spec->am_entry[i].idx < 0)
4218 return false; /* no corresponding imux */
4221 /* we don't need the jack detection for the first pin */
4222 for (i = 1; i < spec->am_num_entries; i++)
4223 snd_hda_jack_detect_enable_callback(codec,
4224 spec->am_entry[i].pin,
4225 call_mic_autoswitch);
4229 static int compare_attr(const void *ap, const void *bp)
4231 const struct automic_entry *a = ap;
4232 const struct automic_entry *b = bp;
4233 return (int)(a->attr - b->attr);
4237 * Check the availability of auto-mic switch;
4238 * Set up if really supported
4240 static int check_auto_mic_availability(struct hda_codec *codec)
4242 struct hda_gen_spec *spec = codec->spec;
4243 struct auto_pin_cfg *cfg = &spec->autocfg;
4247 if (spec->suppress_auto_mic)
4252 for (i = 0; i < cfg->num_inputs; i++) {
4253 hda_nid_t nid = cfg->inputs[i].pin;
4255 attr = snd_hda_codec_get_pincfg(codec, nid);
4256 attr = snd_hda_get_input_pin_attr(attr);
4257 if (types & (1 << attr))
4258 return 0; /* already occupied */
4260 case INPUT_PIN_ATTR_INT:
4261 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4262 return 0; /* invalid type */
4264 case INPUT_PIN_ATTR_UNUSED:
4265 return 0; /* invalid entry */
4267 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4268 return 0; /* invalid type */
4269 if (!spec->line_in_auto_switch &&
4270 cfg->inputs[i].type != AUTO_PIN_MIC)
4271 return 0; /* only mic is allowed */
4272 if (!is_jack_detectable(codec, nid))
4273 return 0; /* no unsol support */
4276 if (num_pins >= MAX_AUTO_MIC_PINS)
4278 types |= (1 << attr);
4279 spec->am_entry[num_pins].pin = nid;
4280 spec->am_entry[num_pins].attr = attr;
4287 spec->am_num_entries = num_pins;
4288 /* sort the am_entry in the order of attr so that the pin with a
4289 * higher attr will be selected when the jack is plugged.
4291 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4292 compare_attr, NULL);
4294 if (!auto_mic_check_imux(codec))
4298 spec->num_adc_nids = 1;
4299 spec->cur_mux[0] = spec->am_entry[0].idx;
4300 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4301 spec->am_entry[0].pin,
4302 spec->am_entry[1].pin,
4303 spec->am_entry[2].pin);
4308 /* power_filter hook; make inactive widgets into power down */
4309 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4311 unsigned int power_state)
4313 if (power_state != AC_PWRST_D0 || nid == codec->afg)
4315 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4317 if (is_active_nid_for_any(codec, nid))
4321 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4323 /* mute all aamix inputs initially; parse up to the first leaves */
4324 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4327 const hda_nid_t *conn;
4330 nums = snd_hda_get_conn_list(codec, mix, &conn);
4331 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4332 for (i = 0; i < nums; i++) {
4334 snd_hda_codec_amp_stereo(codec, mix,
4336 0xff, HDA_AMP_MUTE);
4337 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4338 snd_hda_codec_amp_stereo(codec, conn[i],
4340 0xff, HDA_AMP_MUTE);
4345 * Parse the given BIOS configuration and set up the hda_gen_spec
4347 * return 1 if successful, 0 if the proper config is not found,
4348 * or a negative error code
4350 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4351 struct auto_pin_cfg *cfg)
4353 struct hda_gen_spec *spec = codec->spec;
4356 parse_user_hints(codec);
4358 if (spec->mixer_nid && !spec->mixer_merge_nid)
4359 spec->mixer_merge_nid = spec->mixer_nid;
4361 if (cfg != &spec->autocfg) {
4362 spec->autocfg = *cfg;
4363 cfg = &spec->autocfg;
4366 if (!spec->main_out_badness)
4367 spec->main_out_badness = &hda_main_out_badness;
4368 if (!spec->extra_out_badness)
4369 spec->extra_out_badness = &hda_extra_out_badness;
4371 fill_all_dac_nids(codec);
4373 if (!cfg->line_outs) {
4374 if (cfg->dig_outs || cfg->dig_in_pin) {
4375 spec->multiout.max_channels = 2;
4376 spec->no_analog = 1;
4379 if (!cfg->num_inputs && !cfg->dig_in_pin)
4380 return 0; /* can't find valid BIOS pin config */
4383 if (!spec->no_primary_hp &&
4384 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4385 cfg->line_outs <= cfg->hp_outs) {
4386 /* use HP as primary out */
4387 cfg->speaker_outs = cfg->line_outs;
4388 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4389 sizeof(cfg->speaker_pins));
4390 cfg->line_outs = cfg->hp_outs;
4391 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4393 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4394 cfg->line_out_type = AUTO_PIN_HP_OUT;
4397 err = parse_output_paths(codec);
4400 err = create_multi_channel_mode(codec);
4403 err = create_multi_out_ctls(codec, cfg);
4406 err = create_hp_out_ctls(codec);
4409 err = create_speaker_out_ctls(codec);
4412 err = create_indep_hp_ctls(codec);
4415 err = create_loopback_mixing_ctl(codec);
4418 err = create_hp_mic(codec);
4421 err = create_input_ctls(codec);
4425 spec->const_channel_count = spec->ext_channel_count;
4426 /* check the multiple speaker and headphone pins */
4427 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4428 spec->const_channel_count = max(spec->const_channel_count,
4429 cfg->speaker_outs * 2);
4430 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4431 spec->const_channel_count = max(spec->const_channel_count,
4433 spec->multiout.max_channels = max(spec->ext_channel_count,
4434 spec->const_channel_count);
4436 err = check_auto_mute_availability(codec);
4440 err = check_dyn_adc_switch(codec);
4444 err = check_auto_mic_availability(codec);
4448 /* add stereo mix if available and not enabled yet */
4449 if (!spec->auto_mic && spec->mixer_nid &&
4450 spec->add_stereo_mix_input &&
4451 spec->input_mux.num_items > 1 &&
4452 snd_hda_get_bool_hint(codec, "add_stereo_mix_input") < 0) {
4453 err = parse_capture_source(codec, spec->mixer_nid,
4454 CFG_IDX_MIX, spec->num_all_adcs,
4461 err = create_capture_mixers(codec);
4465 err = parse_mic_boost(codec);
4469 /* create "Headphone Mic Jack Mode" if no input selection is
4470 * available (or user specifies add_jack_modes hint)
4472 if (spec->hp_mic_pin &&
4473 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4474 spec->add_jack_modes)) {
4475 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4480 if (spec->add_jack_modes) {
4481 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4482 err = create_out_jack_modes(codec, cfg->line_outs,
4483 cfg->line_out_pins);
4487 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4488 err = create_out_jack_modes(codec, cfg->hp_outs,
4495 /* mute all aamix input initially */
4496 if (spec->mixer_nid)
4497 mute_all_mixer_nid(codec, spec->mixer_nid);
4500 parse_digital(codec);
4502 if (spec->power_down_unused)
4503 codec->power_filter = snd_hda_gen_path_power_filter;
4505 if (!spec->no_analog && spec->beep_nid) {
4506 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4513 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
4517 * Build control elements
4520 /* slave controls for virtual master */
4521 static const char * const slave_pfxs[] = {
4522 "Front", "Surround", "Center", "LFE", "Side",
4523 "Headphone", "Speaker", "Mono", "Line Out",
4524 "CLFE", "Bass Speaker", "PCM",
4525 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4526 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4531 int snd_hda_gen_build_controls(struct hda_codec *codec)
4533 struct hda_gen_spec *spec = codec->spec;
4536 if (spec->kctls.used) {
4537 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4542 if (spec->multiout.dig_out_nid) {
4543 err = snd_hda_create_dig_out_ctls(codec,
4544 spec->multiout.dig_out_nid,
4545 spec->multiout.dig_out_nid,
4546 spec->pcm_rec[1].pcm_type);
4549 if (!spec->no_analog) {
4550 err = snd_hda_create_spdif_share_sw(codec,
4554 spec->multiout.share_spdif = 1;
4557 if (spec->dig_in_nid) {
4558 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4563 /* if we have no master control, let's create it */
4564 if (!spec->no_analog &&
4565 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
4566 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
4567 spec->vmaster_tlv, slave_pfxs,
4572 if (!spec->no_analog &&
4573 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4574 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4577 true, &spec->vmaster_mute.sw_kctl);
4580 if (spec->vmaster_mute.hook) {
4581 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4582 spec->vmaster_mute_enum);
4583 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4587 free_kctls(spec); /* no longer needed */
4589 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4595 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
4602 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4603 struct hda_codec *codec,
4604 struct snd_pcm_substream *substream,
4607 struct hda_gen_spec *spec = codec->spec;
4608 if (spec->pcm_playback_hook)
4609 spec->pcm_playback_hook(hinfo, codec, substream, action);
4612 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4613 struct hda_codec *codec,
4614 struct snd_pcm_substream *substream,
4617 struct hda_gen_spec *spec = codec->spec;
4618 if (spec->pcm_capture_hook)
4619 spec->pcm_capture_hook(hinfo, codec, substream, action);
4623 * Analog playback callbacks
4625 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4626 struct hda_codec *codec,
4627 struct snd_pcm_substream *substream)
4629 struct hda_gen_spec *spec = codec->spec;
4632 mutex_lock(&spec->pcm_mutex);
4633 err = snd_hda_multi_out_analog_open(codec,
4634 &spec->multiout, substream,
4637 spec->active_streams |= 1 << STREAM_MULTI_OUT;
4638 call_pcm_playback_hook(hinfo, codec, substream,
4639 HDA_GEN_PCM_ACT_OPEN);
4641 mutex_unlock(&spec->pcm_mutex);
4645 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4646 struct hda_codec *codec,
4647 unsigned int stream_tag,
4648 unsigned int format,
4649 struct snd_pcm_substream *substream)
4651 struct hda_gen_spec *spec = codec->spec;
4654 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4655 stream_tag, format, substream);
4657 call_pcm_playback_hook(hinfo, codec, substream,
4658 HDA_GEN_PCM_ACT_PREPARE);
4662 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4663 struct hda_codec *codec,
4664 struct snd_pcm_substream *substream)
4666 struct hda_gen_spec *spec = codec->spec;
4669 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4671 call_pcm_playback_hook(hinfo, codec, substream,
4672 HDA_GEN_PCM_ACT_CLEANUP);
4676 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4677 struct hda_codec *codec,
4678 struct snd_pcm_substream *substream)
4680 struct hda_gen_spec *spec = codec->spec;
4681 mutex_lock(&spec->pcm_mutex);
4682 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
4683 call_pcm_playback_hook(hinfo, codec, substream,
4684 HDA_GEN_PCM_ACT_CLOSE);
4685 mutex_unlock(&spec->pcm_mutex);
4689 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4690 struct hda_codec *codec,
4691 struct snd_pcm_substream *substream)
4693 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4697 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4698 struct hda_codec *codec,
4699 unsigned int stream_tag,
4700 unsigned int format,
4701 struct snd_pcm_substream *substream)
4703 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4704 call_pcm_capture_hook(hinfo, codec, substream,
4705 HDA_GEN_PCM_ACT_PREPARE);
4709 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4710 struct hda_codec *codec,
4711 struct snd_pcm_substream *substream)
4713 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4714 call_pcm_capture_hook(hinfo, codec, substream,
4715 HDA_GEN_PCM_ACT_CLEANUP);
4719 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4720 struct hda_codec *codec,
4721 struct snd_pcm_substream *substream)
4723 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4727 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4728 struct hda_codec *codec,
4729 struct snd_pcm_substream *substream)
4731 struct hda_gen_spec *spec = codec->spec;
4734 mutex_lock(&spec->pcm_mutex);
4735 if (!spec->indep_hp_enabled)
4738 spec->active_streams |= 1 << STREAM_INDEP_HP;
4739 call_pcm_playback_hook(hinfo, codec, substream,
4740 HDA_GEN_PCM_ACT_OPEN);
4741 mutex_unlock(&spec->pcm_mutex);
4745 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4746 struct hda_codec *codec,
4747 struct snd_pcm_substream *substream)
4749 struct hda_gen_spec *spec = codec->spec;
4750 mutex_lock(&spec->pcm_mutex);
4751 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
4752 call_pcm_playback_hook(hinfo, codec, substream,
4753 HDA_GEN_PCM_ACT_CLOSE);
4754 mutex_unlock(&spec->pcm_mutex);
4758 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4759 struct hda_codec *codec,
4760 unsigned int stream_tag,
4761 unsigned int format,
4762 struct snd_pcm_substream *substream)
4764 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4765 call_pcm_playback_hook(hinfo, codec, substream,
4766 HDA_GEN_PCM_ACT_PREPARE);
4770 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4771 struct hda_codec *codec,
4772 struct snd_pcm_substream *substream)
4774 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4775 call_pcm_playback_hook(hinfo, codec, substream,
4776 HDA_GEN_PCM_ACT_CLEANUP);
4783 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4784 struct hda_codec *codec,
4785 struct snd_pcm_substream *substream)
4787 struct hda_gen_spec *spec = codec->spec;
4788 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4791 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4792 struct hda_codec *codec,
4793 unsigned int stream_tag,
4794 unsigned int format,
4795 struct snd_pcm_substream *substream)
4797 struct hda_gen_spec *spec = codec->spec;
4798 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4799 stream_tag, format, substream);
4802 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4803 struct hda_codec *codec,
4804 struct snd_pcm_substream *substream)
4806 struct hda_gen_spec *spec = codec->spec;
4807 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4810 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4811 struct hda_codec *codec,
4812 struct snd_pcm_substream *substream)
4814 struct hda_gen_spec *spec = codec->spec;
4815 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4821 #define alt_capture_pcm_open capture_pcm_open
4822 #define alt_capture_pcm_close capture_pcm_close
4824 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4825 struct hda_codec *codec,
4826 unsigned int stream_tag,
4827 unsigned int format,
4828 struct snd_pcm_substream *substream)
4830 struct hda_gen_spec *spec = codec->spec;
4832 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
4833 stream_tag, 0, format);
4834 call_pcm_capture_hook(hinfo, codec, substream,
4835 HDA_GEN_PCM_ACT_PREPARE);
4839 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4840 struct hda_codec *codec,
4841 struct snd_pcm_substream *substream)
4843 struct hda_gen_spec *spec = codec->spec;
4845 snd_hda_codec_cleanup_stream(codec,
4846 spec->adc_nids[substream->number + 1]);
4847 call_pcm_capture_hook(hinfo, codec, substream,
4848 HDA_GEN_PCM_ACT_CLEANUP);
4854 static const struct hda_pcm_stream pcm_analog_playback = {
4858 /* NID is set in build_pcms */
4860 .open = playback_pcm_open,
4861 .close = playback_pcm_close,
4862 .prepare = playback_pcm_prepare,
4863 .cleanup = playback_pcm_cleanup
4867 static const struct hda_pcm_stream pcm_analog_capture = {
4871 /* NID is set in build_pcms */
4873 .open = capture_pcm_open,
4874 .close = capture_pcm_close,
4875 .prepare = capture_pcm_prepare,
4876 .cleanup = capture_pcm_cleanup
4880 static const struct hda_pcm_stream pcm_analog_alt_playback = {
4884 /* NID is set in build_pcms */
4886 .open = alt_playback_pcm_open,
4887 .close = alt_playback_pcm_close,
4888 .prepare = alt_playback_pcm_prepare,
4889 .cleanup = alt_playback_pcm_cleanup
4893 static const struct hda_pcm_stream pcm_analog_alt_capture = {
4894 .substreams = 2, /* can be overridden */
4897 /* NID is set in build_pcms */
4899 .open = alt_capture_pcm_open,
4900 .close = alt_capture_pcm_close,
4901 .prepare = alt_capture_pcm_prepare,
4902 .cleanup = alt_capture_pcm_cleanup
4906 static const struct hda_pcm_stream pcm_digital_playback = {
4910 /* NID is set in build_pcms */
4912 .open = dig_playback_pcm_open,
4913 .close = dig_playback_pcm_close,
4914 .prepare = dig_playback_pcm_prepare,
4915 .cleanup = dig_playback_pcm_cleanup
4919 static const struct hda_pcm_stream pcm_digital_capture = {
4923 /* NID is set in build_pcms */
4926 /* Used by build_pcms to flag that a PCM has no playback stream */
4927 static const struct hda_pcm_stream pcm_null_stream = {
4934 * dynamic changing ADC PCM streams
4936 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4938 struct hda_gen_spec *spec = codec->spec;
4939 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4941 if (spec->cur_adc && spec->cur_adc != new_adc) {
4942 /* stream is running, let's swap the current ADC */
4943 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4944 spec->cur_adc = new_adc;
4945 snd_hda_codec_setup_stream(codec, new_adc,
4946 spec->cur_adc_stream_tag, 0,
4947 spec->cur_adc_format);
4953 /* analog capture with dynamic dual-adc changes */
4954 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4955 struct hda_codec *codec,
4956 unsigned int stream_tag,
4957 unsigned int format,
4958 struct snd_pcm_substream *substream)
4960 struct hda_gen_spec *spec = codec->spec;
4961 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4962 spec->cur_adc_stream_tag = stream_tag;
4963 spec->cur_adc_format = format;
4964 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4968 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4969 struct hda_codec *codec,
4970 struct snd_pcm_substream *substream)
4972 struct hda_gen_spec *spec = codec->spec;
4973 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4978 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4982 .nid = 0, /* fill later */
4984 .prepare = dyn_adc_capture_pcm_prepare,
4985 .cleanup = dyn_adc_capture_pcm_cleanup
4989 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4990 const char *chip_name)
4996 strlcpy(str, chip_name, len);
4998 /* drop non-alnum chars after a space */
4999 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5000 if (!isalnum(p[1])) {
5005 strlcat(str, sfx, len);
5008 /* build PCM streams based on the parsed results */
5009 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5011 struct hda_gen_spec *spec = codec->spec;
5012 struct hda_pcm *info = spec->pcm_rec;
5013 const struct hda_pcm_stream *p;
5014 bool have_multi_adcs;
5016 codec->num_pcms = 1;
5017 codec->pcm_info = info;
5019 if (spec->no_analog)
5022 fill_pcm_stream_name(spec->stream_name_analog,
5023 sizeof(spec->stream_name_analog),
5024 " Analog", codec->chip_name);
5025 info->name = spec->stream_name_analog;
5027 if (spec->multiout.num_dacs > 0) {
5028 p = spec->stream_analog_playback;
5030 p = &pcm_analog_playback;
5031 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5032 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
5033 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5034 spec->multiout.max_channels;
5035 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5036 spec->autocfg.line_outs == 2)
5037 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5040 if (spec->num_adc_nids) {
5041 p = spec->stream_analog_capture;
5043 if (spec->dyn_adc_switch)
5044 p = &dyn_adc_pcm_analog_capture;
5046 p = &pcm_analog_capture;
5048 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5049 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
5053 /* SPDIF for stream index #1 */
5054 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5055 fill_pcm_stream_name(spec->stream_name_digital,
5056 sizeof(spec->stream_name_digital),
5057 " Digital", codec->chip_name);
5058 codec->num_pcms = 2;
5059 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5060 info = spec->pcm_rec + 1;
5061 info->name = spec->stream_name_digital;
5062 if (spec->dig_out_type)
5063 info->pcm_type = spec->dig_out_type;
5065 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5066 if (spec->multiout.dig_out_nid) {
5067 p = spec->stream_digital_playback;
5069 p = &pcm_digital_playback;
5070 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5071 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
5073 if (spec->dig_in_nid) {
5074 p = spec->stream_digital_capture;
5076 p = &pcm_digital_capture;
5077 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5078 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
5082 if (spec->no_analog)
5085 /* If the use of more than one ADC is requested for the current
5086 * model, configure a second analog capture-only PCM.
5088 have_multi_adcs = (spec->num_adc_nids > 1) &&
5089 !spec->dyn_adc_switch && !spec->auto_mic;
5090 /* Additional Analaog capture for index #2 */
5091 if (spec->alt_dac_nid || have_multi_adcs) {
5092 fill_pcm_stream_name(spec->stream_name_alt_analog,
5093 sizeof(spec->stream_name_alt_analog),
5094 " Alt Analog", codec->chip_name);
5095 codec->num_pcms = 3;
5096 info = spec->pcm_rec + 2;
5097 info->name = spec->stream_name_alt_analog;
5098 if (spec->alt_dac_nid) {
5099 p = spec->stream_analog_alt_playback;
5101 p = &pcm_analog_alt_playback;
5102 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5103 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
5106 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
5108 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
5110 if (have_multi_adcs) {
5111 p = spec->stream_analog_alt_capture;
5113 p = &pcm_analog_alt_capture;
5114 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5115 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
5117 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5118 spec->num_adc_nids - 1;
5120 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
5122 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
5128 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5132 * Standard auto-parser initializations
5135 /* configure the given path as a proper output */
5136 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5138 struct nid_path *path;
5141 path = snd_hda_get_path_from_idx(codec, path_idx);
5142 if (!path || !path->depth)
5144 pin = path->path[path->depth - 1];
5145 restore_pin_ctl(codec, pin);
5146 snd_hda_activate_path(codec, path, path->active,
5147 aamix_default(codec->spec));
5148 set_pin_eapd(codec, pin, path->active);
5151 /* initialize primary output paths */
5152 static void init_multi_out(struct hda_codec *codec)
5154 struct hda_gen_spec *spec = codec->spec;
5157 for (i = 0; i < spec->autocfg.line_outs; i++)
5158 set_output_and_unmute(codec, spec->out_paths[i]);
5162 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5166 for (i = 0; i < num_outs; i++)
5167 set_output_and_unmute(codec, paths[i]);
5170 /* initialize hp and speaker paths */
5171 static void init_extra_out(struct hda_codec *codec)
5173 struct hda_gen_spec *spec = codec->spec;
5175 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5176 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5177 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5178 __init_extra_out(codec, spec->autocfg.speaker_outs,
5179 spec->speaker_paths);
5182 /* initialize multi-io paths */
5183 static void init_multi_io(struct hda_codec *codec)
5185 struct hda_gen_spec *spec = codec->spec;
5188 for (i = 0; i < spec->multi_ios; i++) {
5189 hda_nid_t pin = spec->multi_io[i].pin;
5190 struct nid_path *path;
5191 path = get_multiio_path(codec, i);
5194 if (!spec->multi_io[i].ctl_in)
5195 spec->multi_io[i].ctl_in =
5196 snd_hda_codec_get_pin_target(codec, pin);
5197 snd_hda_activate_path(codec, path, path->active,
5198 aamix_default(spec));
5202 static void init_aamix_paths(struct hda_codec *codec)
5204 struct hda_gen_spec *spec = codec->spec;
5206 if (!spec->have_aamix_ctl)
5208 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5209 spec->aamix_out_paths[0],
5210 spec->autocfg.line_out_type);
5211 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5212 spec->aamix_out_paths[1],
5214 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5215 spec->aamix_out_paths[2],
5216 AUTO_PIN_SPEAKER_OUT);
5219 /* set up input pins and loopback paths */
5220 static void init_analog_input(struct hda_codec *codec)
5222 struct hda_gen_spec *spec = codec->spec;
5223 struct auto_pin_cfg *cfg = &spec->autocfg;
5226 for (i = 0; i < cfg->num_inputs; i++) {
5227 hda_nid_t nid = cfg->inputs[i].pin;
5228 if (is_input_pin(codec, nid))
5229 restore_pin_ctl(codec, nid);
5231 /* init loopback inputs */
5232 if (spec->mixer_nid) {
5233 resume_path_from_idx(codec, spec->loopback_paths[i]);
5234 resume_path_from_idx(codec, spec->loopback_merge_path);
5239 /* initialize ADC paths */
5240 static void init_input_src(struct hda_codec *codec)
5242 struct hda_gen_spec *spec = codec->spec;
5243 struct hda_input_mux *imux = &spec->input_mux;
5244 struct nid_path *path;
5247 if (spec->dyn_adc_switch)
5250 nums = spec->num_adc_nids;
5252 for (c = 0; c < nums; c++) {
5253 for (i = 0; i < imux->num_items; i++) {
5254 path = get_input_path(codec, c, i);
5256 bool active = path->active;
5257 if (i == spec->cur_mux[c])
5259 snd_hda_activate_path(codec, path, active, false);
5263 update_hp_mic(codec, c, true);
5266 if (spec->cap_sync_hook)
5267 spec->cap_sync_hook(codec, NULL, NULL);
5270 /* set right pin controls for digital I/O */
5271 static void init_digital(struct hda_codec *codec)
5273 struct hda_gen_spec *spec = codec->spec;
5277 for (i = 0; i < spec->autocfg.dig_outs; i++)
5278 set_output_and_unmute(codec, spec->digout_paths[i]);
5279 pin = spec->autocfg.dig_in_pin;
5281 restore_pin_ctl(codec, pin);
5282 resume_path_from_idx(codec, spec->digin_path);
5286 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5287 * invalid unsol tags by some reason
5289 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5293 for (i = 0; i < codec->init_pins.used; i++) {
5294 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5295 hda_nid_t nid = pin->nid;
5296 if (is_jack_detectable(codec, nid) &&
5297 !snd_hda_jack_tbl_get(codec, nid))
5298 snd_hda_codec_update_cache(codec, nid, 0,
5299 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5304 * initialize the generic spec;
5305 * this can be put as patch_ops.init function
5307 int snd_hda_gen_init(struct hda_codec *codec)
5309 struct hda_gen_spec *spec = codec->spec;
5311 if (spec->init_hook)
5312 spec->init_hook(codec);
5314 snd_hda_apply_verbs(codec);
5316 codec->cached_write = 1;
5318 init_multi_out(codec);
5319 init_extra_out(codec);
5320 init_multi_io(codec);
5321 init_aamix_paths(codec);
5322 init_analog_input(codec);
5323 init_input_src(codec);
5324 init_digital(codec);
5326 clear_unsol_on_unused_pins(codec);
5328 /* call init functions of standard auto-mute helpers */
5329 update_automute_all(codec);
5331 snd_hda_codec_flush_cache(codec);
5333 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5334 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5336 hda_call_check_power_status(codec, 0x01);
5339 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
5342 * free the generic spec;
5343 * this can be put as patch_ops.free function
5345 void snd_hda_gen_free(struct hda_codec *codec)
5347 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
5348 snd_hda_gen_spec_free(codec->spec);
5352 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
5356 * check the loopback power save state;
5357 * this can be put as patch_ops.check_power_status function
5359 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5361 struct hda_gen_spec *spec = codec->spec;
5362 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5364 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
5369 * the generic codec support
5372 static const struct hda_codec_ops generic_patch_ops = {
5373 .build_controls = snd_hda_gen_build_controls,
5374 .build_pcms = snd_hda_gen_build_pcms,
5375 .init = snd_hda_gen_init,
5376 .free = snd_hda_gen_free,
5377 .unsol_event = snd_hda_jack_unsol_event,
5379 .check_power_status = snd_hda_gen_check_power_status,
5383 int snd_hda_parse_generic_codec(struct hda_codec *codec)
5385 struct hda_gen_spec *spec;
5388 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5391 snd_hda_gen_spec_init(spec);
5394 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5398 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5402 codec->patch_ops = generic_patch_ops;
5406 snd_hda_gen_free(codec);
5409 EXPORT_SYMBOL_GPL(snd_hda_parse_generic_codec);
5411 MODULE_LICENSE("GPL");
5412 MODULE_DESCRIPTION("Generic HD-audio codec parser");