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"
44 * snd_hda_gen_spec_init - initialize hda_gen_spec struct
45 * @spec: hda_gen_spec object to initialize
47 * Initialize the given hda_gen_spec object.
49 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
51 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
52 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
53 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
54 mutex_init(&spec->pcm_mutex);
57 EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
60 * snd_hda_gen_add_kctl - Add a new kctl_new struct from the template
61 * @spec: hda_gen_spec object
62 * @name: name string to override the template, NULL if unchanged
63 * @temp: template for the new kctl
65 * Add a new kctl (actually snd_kcontrol_new to be instantiated later)
66 * element based on the given snd_kcontrol_new template @temp and the
67 * name string @name to the list in @spec.
68 * Returns the newly created object or NULL as error.
70 struct snd_kcontrol_new *
71 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
72 const struct snd_kcontrol_new *temp)
74 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
79 knew->name = kstrdup(name, GFP_KERNEL);
81 knew->name = kstrdup(knew->name, GFP_KERNEL);
86 EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
88 static void free_kctls(struct hda_gen_spec *spec)
90 if (spec->kctls.list) {
91 struct snd_kcontrol_new *kctl = spec->kctls.list;
93 for (i = 0; i < spec->kctls.used; i++)
96 snd_array_free(&spec->kctls);
99 static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
104 snd_array_free(&spec->paths);
105 snd_array_free(&spec->loopback_list);
111 static void parse_user_hints(struct hda_codec *codec)
113 struct hda_gen_spec *spec = codec->spec;
116 val = snd_hda_get_bool_hint(codec, "jack_detect");
118 codec->no_jack_detect = !val;
119 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
121 codec->inv_jack_detect = !!val;
122 val = snd_hda_get_bool_hint(codec, "trigger_sense");
124 codec->no_trigger_sense = !val;
125 val = snd_hda_get_bool_hint(codec, "inv_eapd");
127 codec->inv_eapd = !!val;
128 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
130 codec->pcm_format_first = !!val;
131 val = snd_hda_get_bool_hint(codec, "sticky_stream");
133 codec->no_sticky_stream = !val;
134 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
136 codec->spdif_status_reset = !!val;
137 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
139 codec->pin_amp_workaround = !!val;
140 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
142 codec->single_adc_amp = !!val;
143 val = snd_hda_get_bool_hint(codec, "power_save_node");
145 codec->power_save_node = !!val;
147 val = snd_hda_get_bool_hint(codec, "auto_mute");
149 spec->suppress_auto_mute = !val;
150 val = snd_hda_get_bool_hint(codec, "auto_mic");
152 spec->suppress_auto_mic = !val;
153 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
155 spec->line_in_auto_switch = !!val;
156 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
158 spec->auto_mute_via_amp = !!val;
159 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
161 spec->need_dac_fix = !!val;
162 val = snd_hda_get_bool_hint(codec, "primary_hp");
164 spec->no_primary_hp = !val;
165 val = snd_hda_get_bool_hint(codec, "multi_io");
167 spec->no_multi_io = !val;
168 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
170 spec->multi_cap_vol = !!val;
171 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
173 spec->inv_dmic_split = !!val;
174 val = snd_hda_get_bool_hint(codec, "indep_hp");
176 spec->indep_hp = !!val;
177 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
179 spec->add_stereo_mix_input = !!val;
180 /* the following two are just for compatibility */
181 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
183 spec->add_jack_modes = !!val;
184 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
186 spec->add_jack_modes = !!val;
187 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
189 spec->add_jack_modes = !!val;
190 val = snd_hda_get_bool_hint(codec, "power_down_unused");
192 spec->power_down_unused = !!val;
193 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
195 spec->hp_mic = !!val;
196 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
198 spec->suppress_hp_mic_detect = !val;
199 val = snd_hda_get_bool_hint(codec, "vmaster");
201 spec->suppress_vmaster = !val;
203 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
204 spec->mixer_nid = val;
208 * pin control value accesses
211 #define update_pin_ctl(codec, pin, val) \
212 snd_hda_codec_update_cache(codec, pin, 0, \
213 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
215 /* restore the pinctl based on the cached value */
216 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
218 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
221 /* set the pinctl target value and write it if requested */
222 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
223 unsigned int val, bool do_write)
227 val = snd_hda_correct_pin_ctl(codec, pin, val);
228 snd_hda_codec_set_pin_target(codec, pin, val);
230 update_pin_ctl(codec, pin, val);
233 /* set pinctl target values for all given pins */
234 static void set_pin_targets(struct hda_codec *codec, int num_pins,
235 hda_nid_t *pins, unsigned int val)
238 for (i = 0; i < num_pins; i++)
239 set_pin_target(codec, pins[i], val, false);
246 /* return the position of NID in the list, or -1 if not found */
247 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
250 for (i = 0; i < nums; i++)
256 /* return true if the given NID is contained in the path */
257 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
259 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
262 static struct nid_path *get_nid_path(struct hda_codec *codec,
263 hda_nid_t from_nid, hda_nid_t to_nid,
266 struct hda_gen_spec *spec = codec->spec;
269 for (i = 0; i < spec->paths.used; i++) {
270 struct nid_path *path = snd_array_elem(&spec->paths, i);
271 if (path->depth <= 0)
273 if ((!from_nid || path->path[0] == from_nid) &&
274 (!to_nid || path->path[path->depth - 1] == to_nid)) {
276 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
277 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
285 * snd_hda_get_path_idx - get the index number corresponding to the path
287 * @codec: the HDA codec
288 * @path: nid_path object
290 * The returned index starts from 1, i.e. the actual array index with offset 1,
291 * and zero is handled as an invalid path
293 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
295 struct hda_gen_spec *spec = codec->spec;
296 struct nid_path *array = spec->paths.list;
299 if (!spec->paths.used)
302 if (idx < 0 || idx >= spec->paths.used)
306 EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
309 * snd_hda_get_path_from_idx - get the path instance corresponding to the
311 * @codec: the HDA codec
312 * @idx: the path index
314 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
316 struct hda_gen_spec *spec = codec->spec;
318 if (idx <= 0 || idx > spec->paths.used)
320 return snd_array_elem(&spec->paths, idx - 1);
322 EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
324 /* check whether the given DAC is already found in any existing paths */
325 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
327 struct hda_gen_spec *spec = codec->spec;
330 for (i = 0; i < spec->paths.used; i++) {
331 struct nid_path *path = snd_array_elem(&spec->paths, i);
332 if (path->path[0] == nid)
338 /* check whether the given two widgets can be connected */
339 static bool is_reachable_path(struct hda_codec *codec,
340 hda_nid_t from_nid, hda_nid_t to_nid)
342 if (!from_nid || !to_nid)
344 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
347 /* nid, dir and idx */
348 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
350 /* check whether the given ctl is already assigned in any path elements */
351 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
353 struct hda_gen_spec *spec = codec->spec;
356 val &= AMP_VAL_COMPARE_MASK;
357 for (i = 0; i < spec->paths.used; i++) {
358 struct nid_path *path = snd_array_elem(&spec->paths, i);
359 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
365 /* check whether a control with the given (nid, dir, idx) was assigned */
366 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
367 int dir, int idx, int type)
369 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
370 return is_ctl_used(codec, val, type);
373 static void print_nid_path(struct hda_codec *codec,
374 const char *pfx, struct nid_path *path)
381 for (i = 0; i < path->depth; i++)
382 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
383 pos != buf ? ":" : "",
386 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
389 /* called recursively */
390 static bool __parse_nid_path(struct hda_codec *codec,
391 hda_nid_t from_nid, hda_nid_t to_nid,
392 int anchor_nid, struct nid_path *path,
395 const hda_nid_t *conn;
398 if (to_nid == anchor_nid)
399 anchor_nid = 0; /* anchor passed */
400 else if (to_nid == (hda_nid_t)(-anchor_nid))
401 return false; /* hit the exclusive nid */
403 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
404 for (i = 0; i < nums; i++) {
405 if (conn[i] != from_nid) {
406 /* special case: when from_nid is 0,
407 * try to find an empty DAC
410 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
411 is_dac_already_used(codec, conn[i]))
414 /* anchor is not requested or already passed? */
418 if (depth >= MAX_NID_PATH_DEPTH)
420 for (i = 0; i < nums; i++) {
422 type = get_wcaps_type(get_wcaps(codec, conn[i]));
423 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
426 if (__parse_nid_path(codec, from_nid, conn[i],
427 anchor_nid, path, depth + 1))
433 path->path[path->depth] = conn[i];
434 path->idx[path->depth + 1] = i;
435 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
436 path->multi[path->depth + 1] = 1;
442 * snd_hda_parse_nid_path - parse the widget path from the given nid to
444 * @codec: the HDA codec
445 * @from_nid: the NID where the path start from
446 * @to_nid: the NID where the path ends at
447 * @anchor_nid: the anchor indication
448 * @path: the path object to store the result
450 * Returns true if a matching path is found.
452 * The parsing behavior depends on parameters:
453 * when @from_nid is 0, try to find an empty DAC;
454 * when @anchor_nid is set to a positive value, only paths through the widget
455 * with the given value are evaluated.
456 * when @anchor_nid is set to a negative value, paths through the widget
457 * with the negative of given value are excluded, only other paths are chosen.
458 * when @anchor_nid is zero, no special handling about path selection.
460 static bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
461 hda_nid_t to_nid, int anchor_nid,
462 struct nid_path *path)
464 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
465 path->path[path->depth] = to_nid;
473 * snd_hda_add_new_path - parse the path between the given NIDs and
474 * add to the path list
475 * @codec: the HDA codec
476 * @from_nid: the NID where the path start from
477 * @to_nid: the NID where the path ends at
478 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
480 * If no valid path is found, returns NULL.
483 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
484 hda_nid_t to_nid, int anchor_nid)
486 struct hda_gen_spec *spec = codec->spec;
487 struct nid_path *path;
489 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
492 /* check whether the path has been already added */
493 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
497 path = snd_array_new(&spec->paths);
500 memset(path, 0, sizeof(*path));
501 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
507 EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
509 /* clear the given path as invalid so that it won't be picked up later */
510 static void invalidate_nid_path(struct hda_codec *codec, int idx)
512 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
515 memset(path, 0, sizeof(*path));
518 /* return a DAC if paired to the given pin by codec driver */
519 static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
521 struct hda_gen_spec *spec = codec->spec;
522 const hda_nid_t *list = spec->preferred_dacs;
526 for (; *list; list += 2)
532 /* look for an empty DAC slot */
533 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
536 struct hda_gen_spec *spec = codec->spec;
540 for (i = 0; i < spec->num_all_dacs; i++) {
541 hda_nid_t nid = spec->all_dacs[i];
542 if (!nid || is_dac_already_used(codec, nid))
544 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
545 if (is_digital != cap_digital)
547 if (is_reachable_path(codec, nid, pin))
553 /* replace the channels in the composed amp value with the given number */
554 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
556 val &= ~(0x3U << 16);
561 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
562 hda_nid_t nid2, int dir)
564 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
565 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
566 return (query_amp_caps(codec, nid1, dir) ==
567 query_amp_caps(codec, nid2, dir));
570 /* look for a widget suitable for assigning a mute switch in the path */
571 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
572 struct nid_path *path)
576 for (i = path->depth - 1; i >= 0; i--) {
577 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
578 return path->path[i];
579 if (i != path->depth - 1 && i != 0 &&
580 nid_has_mute(codec, path->path[i], HDA_INPUT))
581 return path->path[i];
586 /* look for a widget suitable for assigning a volume ctl in the path */
587 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
588 struct nid_path *path)
590 struct hda_gen_spec *spec = codec->spec;
593 for (i = path->depth - 1; i >= 0; i--) {
594 hda_nid_t nid = path->path[i];
595 if ((spec->out_vol_mask >> nid) & 1)
597 if (nid_has_volume(codec, nid, HDA_OUTPUT))
604 * path activation / deactivation
607 /* can have the amp-in capability? */
608 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
610 hda_nid_t nid = path->path[idx];
611 unsigned int caps = get_wcaps(codec, nid);
612 unsigned int type = get_wcaps_type(caps);
614 if (!(caps & AC_WCAP_IN_AMP))
616 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
621 /* can have the amp-out capability? */
622 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
624 hda_nid_t nid = path->path[idx];
625 unsigned int caps = get_wcaps(codec, nid);
626 unsigned int type = get_wcaps_type(caps);
628 if (!(caps & AC_WCAP_OUT_AMP))
630 if (type == AC_WID_PIN && !idx) /* only for output pins */
635 /* check whether the given (nid,dir,idx) is active */
636 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
637 unsigned int dir, unsigned int idx)
639 struct hda_gen_spec *spec = codec->spec;
640 int type = get_wcaps_type(get_wcaps(codec, nid));
643 if (nid == codec->core.afg)
646 for (n = 0; n < spec->paths.used; n++) {
647 struct nid_path *path = snd_array_elem(&spec->paths, n);
650 if (codec->power_save_node) {
651 if (!path->stream_enabled)
653 /* ignore unplugged paths except for DAC/ADC */
654 if (!(path->pin_enabled || path->pin_fixed) &&
655 type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
658 for (i = 0; i < path->depth; i++) {
659 if (path->path[i] == nid) {
660 if (dir == HDA_OUTPUT || idx == -1 ||
670 /* check whether the NID is referred by any active paths */
671 #define is_active_nid_for_any(codec, nid) \
672 is_active_nid(codec, nid, HDA_OUTPUT, -1)
674 /* get the default amp value for the target state */
675 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
676 int dir, unsigned int caps, bool enable)
678 unsigned int val = 0;
680 if (caps & AC_AMPCAP_NUM_STEPS) {
683 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
685 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
692 /* is this a stereo widget or a stereo-to-mono mix? */
693 static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
695 unsigned int wcaps = get_wcaps(codec, nid);
698 if (wcaps & AC_WCAP_STEREO)
700 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
702 if (snd_hda_get_num_conns(codec, nid) != 1)
704 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
706 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
709 /* initialize the amp value (only at the first time) */
710 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
712 unsigned int caps = query_amp_caps(codec, nid, dir);
713 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
715 if (is_stereo_amps(codec, nid, dir))
716 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
718 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
721 /* update the amp, doing in stereo or mono depending on NID */
722 static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
723 unsigned int mask, unsigned int val)
725 if (is_stereo_amps(codec, nid, dir))
726 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
729 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
733 /* calculate amp value mask we can modify;
734 * if the given amp is controlled by mixers, don't touch it
736 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
737 hda_nid_t nid, int dir, int idx,
740 unsigned int mask = 0xff;
742 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
743 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
746 if (caps & AC_AMPCAP_NUM_STEPS) {
747 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
748 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
754 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
755 int idx, int idx_to_check, bool enable)
758 unsigned int mask, val;
760 caps = query_amp_caps(codec, nid, dir);
761 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
762 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
767 update_amp(codec, nid, dir, idx, mask, val);
770 static void check_and_activate_amp(struct hda_codec *codec, hda_nid_t nid,
771 int dir, int idx, int idx_to_check,
774 /* check whether the given amp is still used by others */
775 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
777 activate_amp(codec, nid, dir, idx, idx_to_check, enable);
780 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
783 hda_nid_t nid = path->path[i];
784 init_amp(codec, nid, HDA_OUTPUT, 0);
785 check_and_activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
788 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
789 int i, bool enable, bool add_aamix)
791 struct hda_gen_spec *spec = codec->spec;
792 const hda_nid_t *conn;
795 hda_nid_t nid = path->path[i];
797 nums = snd_hda_get_conn_list(codec, nid, &conn);
798 type = get_wcaps_type(get_wcaps(codec, nid));
799 if (type == AC_WID_PIN ||
800 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
806 for (n = 0; n < nums; n++)
807 init_amp(codec, nid, HDA_INPUT, n);
809 /* here is a little bit tricky in comparison with activate_amp_out();
810 * when aa-mixer is available, we need to enable the path as well
812 for (n = 0; n < nums; n++) {
814 if (conn[n] != spec->mixer_merge_nid)
816 /* when aamix is disabled, force to off */
818 activate_amp(codec, nid, HDA_INPUT, n, n, false);
822 check_and_activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
826 /* sync power of each widget in the the given path */
827 static hda_nid_t path_power_update(struct hda_codec *codec,
828 struct nid_path *path,
829 bool allow_powerdown)
831 hda_nid_t nid, changed = 0;
834 for (i = 0; i < path->depth; i++) {
836 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
838 if (nid == codec->core.afg)
840 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
844 power = snd_hda_codec_read(codec, nid, 0,
845 AC_VERB_GET_POWER_STATE, 0);
846 if (power != (state | (state << 4))) {
847 snd_hda_codec_write(codec, nid, 0,
848 AC_VERB_SET_POWER_STATE, state);
850 /* all known codecs seem to be capable to handl
851 * widgets state even in D3, so far.
852 * if any new codecs need to restore the widget
853 * states after D0 transition, call the function
857 if (state == AC_PWRST_D0)
858 snd_hdac_regmap_sync_node(&codec->core, nid);
865 /* do sync with the last power state change */
866 static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
870 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
875 * snd_hda_activate_path - activate or deactivate the given path
876 * @codec: the HDA codec
877 * @path: the path to activate/deactivate
878 * @enable: flag to activate or not
879 * @add_aamix: enable the input from aamix NID
881 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
883 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
884 bool enable, bool add_aamix)
886 struct hda_gen_spec *spec = codec->spec;
889 path->active = enable;
891 /* make sure the widget is powered up */
892 if (enable && (spec->power_down_unused || codec->power_save_node))
893 path_power_update(codec, path, codec->power_save_node);
895 for (i = path->depth - 1; i >= 0; i--) {
896 hda_nid_t nid = path->path[i];
898 if (enable && path->multi[i])
899 snd_hda_codec_update_cache(codec, nid, 0,
900 AC_VERB_SET_CONNECT_SEL,
902 if (has_amp_in(codec, path, i))
903 activate_amp_in(codec, path, i, enable, add_aamix);
904 if (has_amp_out(codec, path, i))
905 activate_amp_out(codec, path, i, enable);
908 EXPORT_SYMBOL_GPL(snd_hda_activate_path);
910 /* if the given path is inactive, put widgets into D3 (only if suitable) */
911 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
913 struct hda_gen_spec *spec = codec->spec;
915 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
917 sync_power_state_change(codec, path_power_update(codec, path, true));
920 /* turn on/off EAPD on the given pin */
921 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
923 struct hda_gen_spec *spec = codec->spec;
924 if (spec->own_eapd_ctl ||
925 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
927 if (spec->keep_eapd_on && !enable)
931 snd_hda_codec_update_cache(codec, pin, 0,
932 AC_VERB_SET_EAPD_BTLENABLE,
933 enable ? 0x02 : 0x00);
936 /* re-initialize the path specified by the given path index */
937 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
939 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
941 snd_hda_activate_path(codec, path, path->active, false);
946 * Helper functions for creating mixer ctl elements
949 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
950 struct snd_ctl_elem_value *ucontrol);
951 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
952 struct snd_ctl_elem_value *ucontrol);
959 static const struct snd_kcontrol_new control_templates[] = {
960 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
961 /* only the put callback is replaced for handling the special mute */
963 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
964 .subdevice = HDA_SUBDEV_AMP_FLAG,
965 .info = snd_hda_mixer_amp_switch_info,
966 .get = snd_hda_mixer_amp_switch_get,
967 .put = hda_gen_mixer_mute_put, /* replaced */
968 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
971 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
972 .info = snd_hda_mixer_amp_switch_info,
973 .get = snd_hda_mixer_bind_switch_get,
974 .put = hda_gen_bind_mute_put, /* replaced */
975 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
979 /* add dynamic controls from template */
980 static struct snd_kcontrol_new *
981 add_control(struct hda_gen_spec *spec, int type, const char *name,
982 int cidx, unsigned long val)
984 struct snd_kcontrol_new *knew;
986 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
990 if (get_amp_nid_(val))
991 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
992 knew->private_value = val;
996 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
997 const char *pfx, const char *dir,
998 const char *sfx, int cidx, unsigned long val)
1000 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1001 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
1002 if (!add_control(spec, type, name, cidx, val))
1007 #define add_pb_vol_ctrl(spec, type, pfx, val) \
1008 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1009 #define add_pb_sw_ctrl(spec, type, pfx, val) \
1010 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1011 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1012 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1013 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1014 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1016 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1017 unsigned int chs, struct nid_path *path)
1022 val = path->ctls[NID_PATH_VOL_CTL];
1025 val = amp_val_replace_channels(val, chs);
1026 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1029 /* return the channel bits suitable for the given path->ctls[] */
1030 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1033 int chs = 1; /* mono (left only) */
1035 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1036 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1037 chs = 3; /* stereo */
1042 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1043 struct nid_path *path)
1045 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1046 return add_vol_ctl(codec, pfx, cidx, chs, path);
1049 /* create a mute-switch for the given mixer widget;
1050 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1052 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1053 unsigned int chs, struct nid_path *path)
1056 int type = HDA_CTL_WIDGET_MUTE;
1060 val = path->ctls[NID_PATH_MUTE_CTL];
1063 val = amp_val_replace_channels(val, chs);
1064 if (get_amp_direction_(val) == HDA_INPUT) {
1065 hda_nid_t nid = get_amp_nid_(val);
1066 int nums = snd_hda_get_num_conns(codec, nid);
1068 type = HDA_CTL_BIND_MUTE;
1072 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1075 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1076 int cidx, struct nid_path *path)
1078 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1079 return add_sw_ctl(codec, pfx, cidx, chs, path);
1082 /* playback mute control with the software mute bit check */
1083 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1084 struct snd_ctl_elem_value *ucontrol)
1086 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1087 struct hda_gen_spec *spec = codec->spec;
1089 if (spec->auto_mute_via_amp) {
1090 hda_nid_t nid = get_amp_nid(kcontrol);
1091 bool enabled = !((spec->mute_bits >> nid) & 1);
1092 ucontrol->value.integer.value[0] &= enabled;
1093 ucontrol->value.integer.value[1] &= enabled;
1097 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1098 struct snd_ctl_elem_value *ucontrol)
1100 sync_auto_mute_bits(kcontrol, ucontrol);
1101 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1104 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1105 struct snd_ctl_elem_value *ucontrol)
1107 sync_auto_mute_bits(kcontrol, ucontrol);
1108 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1111 /* any ctl assigned to the path with the given index? */
1112 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1114 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1115 return path && path->ctls[ctl_type];
1118 static const char * const channel_name[4] = {
1119 "Front", "Surround", "CLFE", "Side"
1122 /* give some appropriate ctl name prefix for the given line out channel */
1123 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1124 int *index, int ctl_type)
1126 struct hda_gen_spec *spec = codec->spec;
1127 struct auto_pin_cfg *cfg = &spec->autocfg;
1130 if (cfg->line_outs == 1 && !spec->multi_ios &&
1131 !codec->force_pin_prefix &&
1132 !cfg->hp_outs && !cfg->speaker_outs)
1133 return spec->vmaster_mute.hook ? "PCM" : "Master";
1135 /* if there is really a single DAC used in the whole output paths,
1136 * use it master (or "PCM" if a vmaster hook is present)
1138 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1139 !codec->force_pin_prefix &&
1140 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1141 return spec->vmaster_mute.hook ? "PCM" : "Master";
1143 /* multi-io channels */
1144 if (ch >= cfg->line_outs)
1145 return channel_name[ch];
1147 switch (cfg->line_out_type) {
1148 case AUTO_PIN_SPEAKER_OUT:
1149 /* if the primary channel vol/mute is shared with HP volume,
1150 * don't name it as Speaker
1152 if (!ch && cfg->hp_outs &&
1153 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1155 if (cfg->line_outs == 1)
1157 if (cfg->line_outs == 2)
1158 return ch ? "Bass Speaker" : "Speaker";
1160 case AUTO_PIN_HP_OUT:
1161 /* if the primary channel vol/mute is shared with spk volume,
1162 * don't name it as Headphone
1164 if (!ch && cfg->speaker_outs &&
1165 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1167 /* for multi-io case, only the primary out */
1168 if (ch && spec->multi_ios)
1172 case AUTO_PIN_LINE_OUT:
1173 /* This deals with the case where we have two DACs and
1174 * one LO, one HP and one Speaker */
1175 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1176 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1177 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1178 if (hp_lo_shared && spk_lo_shared)
1179 return spec->vmaster_mute.hook ? "PCM" : "Master";
1181 return "Headphone+LO";
1183 return "Speaker+LO";
1187 /* for a single channel output, we don't have to name the channel */
1188 if (cfg->line_outs == 1 && !spec->multi_ios)
1191 if (ch >= ARRAY_SIZE(channel_name)) {
1196 return channel_name[ch];
1200 * Parse output paths
1203 /* badness definition */
1205 /* No primary DAC is found for the main output */
1206 BAD_NO_PRIMARY_DAC = 0x10000,
1207 /* No DAC is found for the extra output */
1208 BAD_NO_DAC = 0x4000,
1209 /* No possible multi-ios */
1210 BAD_MULTI_IO = 0x120,
1211 /* No individual DAC for extra output */
1212 BAD_NO_EXTRA_DAC = 0x102,
1213 /* No individual DAC for extra surrounds */
1214 BAD_NO_EXTRA_SURR_DAC = 0x101,
1215 /* Primary DAC shared with main surrounds */
1216 BAD_SHARED_SURROUND = 0x100,
1217 /* No independent HP possible */
1218 BAD_NO_INDEP_HP = 0x10,
1219 /* Primary DAC shared with main CLFE */
1220 BAD_SHARED_CLFE = 0x10,
1221 /* Primary DAC shared with extra surrounds */
1222 BAD_SHARED_EXTRA_SURROUND = 0x10,
1223 /* Volume widget is shared */
1224 BAD_SHARED_VOL = 0x10,
1227 /* look for widgets in the given path which are appropriate for
1228 * volume and mute controls, and assign the values to ctls[].
1230 * When no appropriate widget is found in the path, the badness value
1231 * is incremented depending on the situation. The function returns the
1232 * total badness for both volume and mute controls.
1234 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1236 struct hda_gen_spec *spec = codec->spec;
1242 return BAD_SHARED_VOL * 2;
1244 if (path->ctls[NID_PATH_VOL_CTL] ||
1245 path->ctls[NID_PATH_MUTE_CTL])
1246 return 0; /* already evaluated */
1248 nid = look_for_out_vol_nid(codec, path);
1250 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1251 if (spec->dac_min_mute)
1252 val |= HDA_AMP_VAL_MIN_MUTE;
1253 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1254 badness += BAD_SHARED_VOL;
1256 path->ctls[NID_PATH_VOL_CTL] = val;
1258 badness += BAD_SHARED_VOL;
1259 nid = look_for_out_mute_nid(codec, path);
1261 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1262 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1263 nid_has_mute(codec, nid, HDA_OUTPUT))
1264 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1266 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1267 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1268 badness += BAD_SHARED_VOL;
1270 path->ctls[NID_PATH_MUTE_CTL] = val;
1272 badness += BAD_SHARED_VOL;
1276 const struct badness_table hda_main_out_badness = {
1277 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1278 .no_dac = BAD_NO_DAC,
1279 .shared_primary = BAD_NO_PRIMARY_DAC,
1280 .shared_surr = BAD_SHARED_SURROUND,
1281 .shared_clfe = BAD_SHARED_CLFE,
1282 .shared_surr_main = BAD_SHARED_SURROUND,
1284 EXPORT_SYMBOL_GPL(hda_main_out_badness);
1286 const struct badness_table hda_extra_out_badness = {
1287 .no_primary_dac = BAD_NO_DAC,
1288 .no_dac = BAD_NO_DAC,
1289 .shared_primary = BAD_NO_EXTRA_DAC,
1290 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1291 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1292 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1294 EXPORT_SYMBOL_GPL(hda_extra_out_badness);
1296 /* get the DAC of the primary output corresponding to the given array index */
1297 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1299 struct hda_gen_spec *spec = codec->spec;
1300 struct auto_pin_cfg *cfg = &spec->autocfg;
1302 if (cfg->line_outs > idx)
1303 return spec->private_dac_nids[idx];
1304 idx -= cfg->line_outs;
1305 if (spec->multi_ios > idx)
1306 return spec->multi_io[idx].dac;
1310 /* return the DAC if it's reachable, otherwise zero */
1311 static inline hda_nid_t try_dac(struct hda_codec *codec,
1312 hda_nid_t dac, hda_nid_t pin)
1314 return is_reachable_path(codec, dac, pin) ? dac : 0;
1317 /* try to assign DACs to pins and return the resultant badness */
1318 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1319 const hda_nid_t *pins, hda_nid_t *dacs,
1321 const struct badness_table *bad)
1323 struct hda_gen_spec *spec = codec->spec;
1331 for (i = 0; i < num_outs; i++) {
1332 struct nid_path *path;
1333 hda_nid_t pin = pins[i];
1335 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1337 badness += assign_out_path_ctls(codec, path);
1341 dacs[i] = get_preferred_dac(codec, pin);
1343 if (is_dac_already_used(codec, dacs[i]))
1344 badness += bad->shared_primary;
1348 dacs[i] = look_for_dac(codec, pin, false);
1349 if (!dacs[i] && !i) {
1350 /* try to steal the DAC of surrounds for the front */
1351 for (j = 1; j < num_outs; j++) {
1352 if (is_reachable_path(codec, dacs[j], pin)) {
1355 invalidate_nid_path(codec, path_idx[j]);
1364 dac = try_dac(codec, get_primary_out(codec, i), pin);
1366 dac = try_dac(codec, dacs[0], pin);
1368 dac = try_dac(codec, get_primary_out(codec, i), pin);
1371 badness += bad->shared_primary;
1373 badness += bad->shared_surr;
1375 badness += bad->shared_clfe;
1376 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1377 dac = spec->private_dac_nids[0];
1378 badness += bad->shared_surr_main;
1380 badness += bad->no_primary_dac;
1382 badness += bad->no_dac;
1386 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1387 if (!path && !i && spec->mixer_nid) {
1388 /* try with aamix */
1389 path = snd_hda_add_new_path(codec, dac, pin, 0);
1393 badness += bad->no_dac;
1395 /* print_nid_path(codec, "output", path); */
1396 path->active = true;
1397 path_idx[i] = snd_hda_get_path_idx(codec, path);
1398 badness += assign_out_path_ctls(codec, path);
1405 /* return NID if the given pin has only a single connection to a certain DAC */
1406 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1408 struct hda_gen_spec *spec = codec->spec;
1410 hda_nid_t nid_found = 0;
1412 for (i = 0; i < spec->num_all_dacs; i++) {
1413 hda_nid_t nid = spec->all_dacs[i];
1414 if (!nid || is_dac_already_used(codec, nid))
1416 if (is_reachable_path(codec, nid, pin)) {
1425 /* check whether the given pin can be a multi-io pin */
1426 static bool can_be_multiio_pin(struct hda_codec *codec,
1427 unsigned int location, hda_nid_t nid)
1429 unsigned int defcfg, caps;
1431 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1432 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1434 if (location && get_defcfg_location(defcfg) != location)
1436 caps = snd_hda_query_pin_caps(codec, nid);
1437 if (!(caps & AC_PINCAP_OUT))
1442 /* count the number of input pins that are capable to be multi-io */
1443 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1445 struct hda_gen_spec *spec = codec->spec;
1446 struct auto_pin_cfg *cfg = &spec->autocfg;
1447 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1448 unsigned int location = get_defcfg_location(defcfg);
1452 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1453 for (i = 0; i < cfg->num_inputs; i++) {
1454 if (cfg->inputs[i].type != type)
1456 if (can_be_multiio_pin(codec, location,
1457 cfg->inputs[i].pin))
1467 * When hardwired is set, try to fill ony hardwired pins, and returns
1468 * zero if any pins are filled, non-zero if nothing found.
1469 * When hardwired is off, try to fill possible input pins, and returns
1470 * the badness value.
1472 static int fill_multi_ios(struct hda_codec *codec,
1473 hda_nid_t reference_pin,
1476 struct hda_gen_spec *spec = codec->spec;
1477 struct auto_pin_cfg *cfg = &spec->autocfg;
1478 int type, i, j, num_pins, old_pins;
1479 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1480 unsigned int location = get_defcfg_location(defcfg);
1482 struct nid_path *path;
1484 old_pins = spec->multi_ios;
1488 num_pins = count_multiio_pins(codec, reference_pin);
1492 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1493 for (i = 0; i < cfg->num_inputs; i++) {
1494 hda_nid_t nid = cfg->inputs[i].pin;
1497 if (cfg->inputs[i].type != type)
1499 if (!can_be_multiio_pin(codec, location, nid))
1501 for (j = 0; j < spec->multi_ios; j++) {
1502 if (nid == spec->multi_io[j].pin)
1505 if (j < spec->multi_ios)
1509 dac = get_dac_if_single(codec, nid);
1511 dac = look_for_dac(codec, nid, false);
1516 path = snd_hda_add_new_path(codec, dac, nid,
1522 /* print_nid_path(codec, "multiio", path); */
1523 spec->multi_io[spec->multi_ios].pin = nid;
1524 spec->multi_io[spec->multi_ios].dac = dac;
1525 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1526 snd_hda_get_path_idx(codec, path);
1528 if (spec->multi_ios >= 2)
1534 badness = BAD_MULTI_IO;
1535 if (old_pins == spec->multi_ios) {
1537 return 1; /* nothing found */
1539 return badness; /* no badness if nothing found */
1541 if (!hardwired && spec->multi_ios < 2) {
1542 /* cancel newly assigned paths */
1543 spec->paths.used -= spec->multi_ios - old_pins;
1544 spec->multi_ios = old_pins;
1548 /* assign volume and mute controls */
1549 for (i = old_pins; i < spec->multi_ios; i++) {
1550 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1551 badness += assign_out_path_ctls(codec, path);
1557 /* map DACs for all pins in the list if they are single connections */
1558 static bool map_singles(struct hda_codec *codec, int outs,
1559 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1561 struct hda_gen_spec *spec = codec->spec;
1564 for (i = 0; i < outs; i++) {
1565 struct nid_path *path;
1569 dac = get_dac_if_single(codec, pins[i]);
1572 path = snd_hda_add_new_path(codec, dac, pins[i],
1574 if (!path && !i && spec->mixer_nid)
1575 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1579 /* print_nid_path(codec, "output", path); */
1580 path->active = true;
1581 path_idx[i] = snd_hda_get_path_idx(codec, path);
1587 static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1589 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1590 spec->aamix_out_paths[2];
1593 /* create a new path including aamix if available, and return its index */
1594 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1596 struct hda_gen_spec *spec = codec->spec;
1597 struct nid_path *path;
1598 hda_nid_t path_dac, dac, pin;
1600 path = snd_hda_get_path_from_idx(codec, path_idx);
1601 if (!path || !path->depth ||
1602 is_nid_contained(path, spec->mixer_nid))
1604 path_dac = path->path[0];
1605 dac = spec->private_dac_nids[0];
1606 pin = path->path[path->depth - 1];
1607 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1609 if (dac != path_dac)
1611 else if (spec->multiout.hp_out_nid[0])
1612 dac = spec->multiout.hp_out_nid[0];
1613 else if (spec->multiout.extra_out_nid[0])
1614 dac = spec->multiout.extra_out_nid[0];
1618 path = snd_hda_add_new_path(codec, dac, pin,
1623 /* print_nid_path(codec, "output-aamix", path); */
1624 path->active = false; /* unused as default */
1625 path->pin_fixed = true; /* static route */
1626 return snd_hda_get_path_idx(codec, path);
1629 /* check whether the independent HP is available with the current config */
1630 static bool indep_hp_possible(struct hda_codec *codec)
1632 struct hda_gen_spec *spec = codec->spec;
1633 struct auto_pin_cfg *cfg = &spec->autocfg;
1634 struct nid_path *path;
1637 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1638 idx = spec->out_paths[0];
1640 idx = spec->hp_paths[0];
1641 path = snd_hda_get_path_from_idx(codec, idx);
1645 /* assume no path conflicts unless aamix is involved */
1646 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1649 /* check whether output paths contain aamix */
1650 for (i = 0; i < cfg->line_outs; i++) {
1651 if (spec->out_paths[i] == idx)
1653 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1654 if (path && is_nid_contained(path, spec->mixer_nid))
1657 for (i = 0; i < cfg->speaker_outs; i++) {
1658 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1659 if (path && is_nid_contained(path, spec->mixer_nid))
1666 /* fill the empty entries in the dac array for speaker/hp with the
1667 * shared dac pointed by the paths
1669 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1670 hda_nid_t *dacs, int *path_idx)
1672 struct nid_path *path;
1675 for (i = 0; i < num_outs; i++) {
1678 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1681 dacs[i] = path->path[0];
1685 /* fill in the dac_nids table from the parsed pin configuration */
1686 static int fill_and_eval_dacs(struct hda_codec *codec,
1687 bool fill_hardwired,
1688 bool fill_mio_first)
1690 struct hda_gen_spec *spec = codec->spec;
1691 struct auto_pin_cfg *cfg = &spec->autocfg;
1692 int i, err, badness;
1694 /* set num_dacs once to full for look_for_dac() */
1695 spec->multiout.num_dacs = cfg->line_outs;
1696 spec->multiout.dac_nids = spec->private_dac_nids;
1697 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1698 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1699 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1700 spec->multi_ios = 0;
1701 snd_array_free(&spec->paths);
1703 /* clear path indices */
1704 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1705 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1706 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1707 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1708 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1709 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1710 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1711 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1715 /* fill hard-wired DACs first */
1716 if (fill_hardwired) {
1719 mapped = map_singles(codec, cfg->line_outs,
1721 spec->private_dac_nids,
1723 mapped |= map_singles(codec, cfg->hp_outs,
1725 spec->multiout.hp_out_nid,
1727 mapped |= map_singles(codec, cfg->speaker_outs,
1729 spec->multiout.extra_out_nid,
1730 spec->speaker_paths);
1731 if (!spec->no_multi_io &&
1732 fill_mio_first && cfg->line_outs == 1 &&
1733 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1734 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1741 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1742 spec->private_dac_nids, spec->out_paths,
1743 spec->main_out_badness);
1745 if (!spec->no_multi_io && fill_mio_first &&
1746 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1747 /* try to fill multi-io first */
1748 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1751 /* we don't count badness at this stage yet */
1754 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1755 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1756 spec->multiout.hp_out_nid,
1758 spec->extra_out_badness);
1763 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1764 err = try_assign_dacs(codec, cfg->speaker_outs,
1766 spec->multiout.extra_out_nid,
1767 spec->speaker_paths,
1768 spec->extra_out_badness);
1773 if (!spec->no_multi_io &&
1774 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1775 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1781 if (spec->mixer_nid) {
1782 spec->aamix_out_paths[0] =
1783 check_aamix_out_path(codec, spec->out_paths[0]);
1784 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1785 spec->aamix_out_paths[1] =
1786 check_aamix_out_path(codec, spec->hp_paths[0]);
1787 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1788 spec->aamix_out_paths[2] =
1789 check_aamix_out_path(codec, spec->speaker_paths[0]);
1792 if (!spec->no_multi_io &&
1793 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1794 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1795 spec->multi_ios = 1; /* give badness */
1797 /* re-count num_dacs and squash invalid entries */
1798 spec->multiout.num_dacs = 0;
1799 for (i = 0; i < cfg->line_outs; i++) {
1800 if (spec->private_dac_nids[i])
1801 spec->multiout.num_dacs++;
1803 memmove(spec->private_dac_nids + i,
1804 spec->private_dac_nids + i + 1,
1805 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1806 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1810 spec->ext_channel_count = spec->min_channel_count =
1811 spec->multiout.num_dacs * 2;
1813 if (spec->multi_ios == 2) {
1814 for (i = 0; i < 2; i++)
1815 spec->private_dac_nids[spec->multiout.num_dacs++] =
1816 spec->multi_io[i].dac;
1817 } else if (spec->multi_ios) {
1818 spec->multi_ios = 0;
1819 badness += BAD_MULTI_IO;
1822 if (spec->indep_hp && !indep_hp_possible(codec))
1823 badness += BAD_NO_INDEP_HP;
1825 /* re-fill the shared DAC for speaker / headphone */
1826 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1827 refill_shared_dacs(codec, cfg->hp_outs,
1828 spec->multiout.hp_out_nid,
1830 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1831 refill_shared_dacs(codec, cfg->speaker_outs,
1832 spec->multiout.extra_out_nid,
1833 spec->speaker_paths);
1838 #define DEBUG_BADNESS
1840 #ifdef DEBUG_BADNESS
1841 #define debug_badness(fmt, ...) \
1842 codec_dbg(codec, fmt, ##__VA_ARGS__)
1844 #define debug_badness(fmt, ...) \
1845 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1848 #ifdef DEBUG_BADNESS
1849 static inline void print_nid_path_idx(struct hda_codec *codec,
1850 const char *pfx, int idx)
1852 struct nid_path *path;
1854 path = snd_hda_get_path_from_idx(codec, idx);
1856 print_nid_path(codec, pfx, path);
1859 static void debug_show_configs(struct hda_codec *codec,
1860 struct auto_pin_cfg *cfg)
1862 struct hda_gen_spec *spec = codec->spec;
1863 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1866 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1867 cfg->line_out_pins[0], cfg->line_out_pins[1],
1868 cfg->line_out_pins[2], cfg->line_out_pins[3],
1869 spec->multiout.dac_nids[0],
1870 spec->multiout.dac_nids[1],
1871 spec->multiout.dac_nids[2],
1872 spec->multiout.dac_nids[3],
1873 lo_type[cfg->line_out_type]);
1874 for (i = 0; i < cfg->line_outs; i++)
1875 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1876 if (spec->multi_ios > 0)
1877 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1879 spec->multi_io[0].pin, spec->multi_io[1].pin,
1880 spec->multi_io[0].dac, spec->multi_io[1].dac);
1881 for (i = 0; i < spec->multi_ios; i++)
1882 print_nid_path_idx(codec, " mio",
1883 spec->out_paths[cfg->line_outs + i]);
1885 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1886 cfg->hp_pins[0], cfg->hp_pins[1],
1887 cfg->hp_pins[2], cfg->hp_pins[3],
1888 spec->multiout.hp_out_nid[0],
1889 spec->multiout.hp_out_nid[1],
1890 spec->multiout.hp_out_nid[2],
1891 spec->multiout.hp_out_nid[3]);
1892 for (i = 0; i < cfg->hp_outs; i++)
1893 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1894 if (cfg->speaker_outs)
1895 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1896 cfg->speaker_pins[0], cfg->speaker_pins[1],
1897 cfg->speaker_pins[2], cfg->speaker_pins[3],
1898 spec->multiout.extra_out_nid[0],
1899 spec->multiout.extra_out_nid[1],
1900 spec->multiout.extra_out_nid[2],
1901 spec->multiout.extra_out_nid[3]);
1902 for (i = 0; i < cfg->speaker_outs; i++)
1903 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1904 for (i = 0; i < 3; i++)
1905 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1908 #define debug_show_configs(codec, cfg) /* NOP */
1911 /* find all available DACs of the codec */
1912 static void fill_all_dac_nids(struct hda_codec *codec)
1914 struct hda_gen_spec *spec = codec->spec;
1917 spec->num_all_dacs = 0;
1918 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1919 for_each_hda_codec_node(nid, codec) {
1920 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1922 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1923 codec_err(codec, "Too many DACs!\n");
1926 spec->all_dacs[spec->num_all_dacs++] = nid;
1930 static int parse_output_paths(struct hda_codec *codec)
1932 struct hda_gen_spec *spec = codec->spec;
1933 struct auto_pin_cfg *cfg = &spec->autocfg;
1934 struct auto_pin_cfg *best_cfg;
1936 int best_badness = INT_MAX;
1938 bool fill_hardwired = true, fill_mio_first = true;
1939 bool best_wired = true, best_mio = true;
1940 bool hp_spk_swapped = false;
1942 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1948 badness = fill_and_eval_dacs(codec, fill_hardwired,
1954 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1955 cfg->line_out_type, fill_hardwired, fill_mio_first,
1957 debug_show_configs(codec, cfg);
1958 if (badness < best_badness) {
1959 best_badness = badness;
1961 best_wired = fill_hardwired;
1962 best_mio = fill_mio_first;
1966 fill_mio_first = !fill_mio_first;
1967 if (!fill_mio_first)
1969 fill_hardwired = !fill_hardwired;
1970 if (!fill_hardwired)
1974 hp_spk_swapped = true;
1975 if (cfg->speaker_outs > 0 &&
1976 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1977 cfg->hp_outs = cfg->line_outs;
1978 memcpy(cfg->hp_pins, cfg->line_out_pins,
1979 sizeof(cfg->hp_pins));
1980 cfg->line_outs = cfg->speaker_outs;
1981 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1982 sizeof(cfg->speaker_pins));
1983 cfg->speaker_outs = 0;
1984 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1985 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1986 fill_hardwired = true;
1989 if (cfg->hp_outs > 0 &&
1990 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1991 cfg->speaker_outs = cfg->line_outs;
1992 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1993 sizeof(cfg->speaker_pins));
1994 cfg->line_outs = cfg->hp_outs;
1995 memcpy(cfg->line_out_pins, cfg->hp_pins,
1996 sizeof(cfg->hp_pins));
1998 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1999 cfg->line_out_type = AUTO_PIN_HP_OUT;
2000 fill_hardwired = true;
2007 debug_badness("==> restoring best_cfg\n");
2009 fill_and_eval_dacs(codec, best_wired, best_mio);
2011 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2012 cfg->line_out_type, best_wired, best_mio);
2013 debug_show_configs(codec, cfg);
2015 if (cfg->line_out_pins[0]) {
2016 struct nid_path *path;
2017 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
2019 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
2020 if (spec->vmaster_nid) {
2021 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2022 HDA_OUTPUT, spec->vmaster_tlv);
2023 if (spec->dac_min_mute)
2024 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2028 /* set initial pinctl targets */
2029 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2033 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2034 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2035 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2036 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2037 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2038 set_pin_targets(codec, cfg->speaker_outs,
2039 cfg->speaker_pins, val);
2042 /* clear indep_hp flag if not available */
2043 if (spec->indep_hp && !indep_hp_possible(codec))
2050 /* add playback controls from the parsed DAC table */
2051 static int create_multi_out_ctls(struct hda_codec *codec,
2052 const struct auto_pin_cfg *cfg)
2054 struct hda_gen_spec *spec = codec->spec;
2055 int i, err, noutputs;
2057 noutputs = cfg->line_outs;
2058 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2059 noutputs += spec->multi_ios;
2061 for (i = 0; i < noutputs; i++) {
2064 struct nid_path *path;
2066 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
2070 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
2071 if (!name || !strcmp(name, "CLFE")) {
2073 err = add_vol_ctl(codec, "Center", 0, 1, path);
2076 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2080 err = add_stereo_vol(codec, name, index, path);
2085 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2086 if (!name || !strcmp(name, "CLFE")) {
2087 err = add_sw_ctl(codec, "Center", 0, 1, path);
2090 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2094 err = add_stereo_sw(codec, name, index, path);
2102 static int create_extra_out(struct hda_codec *codec, int path_idx,
2103 const char *pfx, int cidx)
2105 struct nid_path *path;
2108 path = snd_hda_get_path_from_idx(codec, path_idx);
2111 err = add_stereo_vol(codec, pfx, cidx, path);
2114 err = add_stereo_sw(codec, pfx, cidx, path);
2120 /* add playback controls for speaker and HP outputs */
2121 static int create_extra_outs(struct hda_codec *codec, int num_pins,
2122 const int *paths, const char *pfx)
2126 for (i = 0; i < num_pins; i++) {
2128 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2131 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2132 name = "Bass Speaker";
2133 else if (num_pins >= 3) {
2134 snprintf(tmp, sizeof(tmp), "%s %s",
2135 pfx, channel_name[i]);
2141 err = create_extra_out(codec, paths[i], name, idx);
2148 static int create_hp_out_ctls(struct hda_codec *codec)
2150 struct hda_gen_spec *spec = codec->spec;
2151 return create_extra_outs(codec, spec->autocfg.hp_outs,
2156 static int create_speaker_out_ctls(struct hda_codec *codec)
2158 struct hda_gen_spec *spec = codec->spec;
2159 return create_extra_outs(codec, spec->autocfg.speaker_outs,
2160 spec->speaker_paths,
2165 * independent HP controls
2168 static void call_hp_automute(struct hda_codec *codec,
2169 struct hda_jack_callback *jack);
2170 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2171 struct snd_ctl_elem_info *uinfo)
2173 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2176 static int indep_hp_get(struct snd_kcontrol *kcontrol,
2177 struct snd_ctl_elem_value *ucontrol)
2179 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2180 struct hda_gen_spec *spec = codec->spec;
2181 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2185 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2186 int nomix_path_idx, int mix_path_idx,
2189 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2190 struct snd_ctl_elem_value *ucontrol)
2192 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2193 struct hda_gen_spec *spec = codec->spec;
2194 unsigned int select = ucontrol->value.enumerated.item[0];
2197 mutex_lock(&spec->pcm_mutex);
2198 if (spec->active_streams) {
2203 if (spec->indep_hp_enabled != select) {
2205 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2206 dacp = &spec->private_dac_nids[0];
2208 dacp = &spec->multiout.hp_out_nid[0];
2210 /* update HP aamix paths in case it conflicts with indep HP */
2211 if (spec->have_aamix_ctl) {
2212 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2213 update_aamix_paths(codec, spec->aamix_mode,
2215 spec->aamix_out_paths[0],
2216 spec->autocfg.line_out_type);
2218 update_aamix_paths(codec, spec->aamix_mode,
2220 spec->aamix_out_paths[1],
2224 spec->indep_hp_enabled = select;
2225 if (spec->indep_hp_enabled)
2228 *dacp = spec->alt_dac_nid;
2230 call_hp_automute(codec, NULL);
2234 mutex_unlock(&spec->pcm_mutex);
2238 static const struct snd_kcontrol_new indep_hp_ctl = {
2239 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2240 .name = "Independent HP",
2241 .info = indep_hp_info,
2242 .get = indep_hp_get,
2243 .put = indep_hp_put,
2247 static int create_indep_hp_ctls(struct hda_codec *codec)
2249 struct hda_gen_spec *spec = codec->spec;
2252 if (!spec->indep_hp)
2254 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2255 dac = spec->multiout.dac_nids[0];
2257 dac = spec->multiout.hp_out_nid[0];
2263 spec->indep_hp_enabled = false;
2264 spec->alt_dac_nid = dac;
2265 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2271 * channel mode enum control
2274 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2275 struct snd_ctl_elem_info *uinfo)
2277 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2278 struct hda_gen_spec *spec = codec->spec;
2281 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2283 uinfo->value.enumerated.items = spec->multi_ios + 1;
2284 if (uinfo->value.enumerated.item > spec->multi_ios)
2285 uinfo->value.enumerated.item = spec->multi_ios;
2286 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2287 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2291 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2292 struct snd_ctl_elem_value *ucontrol)
2294 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2295 struct hda_gen_spec *spec = codec->spec;
2296 ucontrol->value.enumerated.item[0] =
2297 (spec->ext_channel_count - spec->min_channel_count) / 2;
2301 static inline struct nid_path *
2302 get_multiio_path(struct hda_codec *codec, int idx)
2304 struct hda_gen_spec *spec = codec->spec;
2305 return snd_hda_get_path_from_idx(codec,
2306 spec->out_paths[spec->autocfg.line_outs + idx]);
2309 static void update_automute_all(struct hda_codec *codec);
2311 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2312 * used for output paths
2314 static bool aamix_default(struct hda_gen_spec *spec)
2316 return !spec->have_aamix_ctl || spec->aamix_mode;
2319 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2321 struct hda_gen_spec *spec = codec->spec;
2322 hda_nid_t nid = spec->multi_io[idx].pin;
2323 struct nid_path *path;
2325 path = get_multiio_path(codec, idx);
2329 if (path->active == output)
2333 set_pin_target(codec, nid, PIN_OUT, true);
2334 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2335 set_pin_eapd(codec, nid, true);
2337 set_pin_eapd(codec, nid, false);
2338 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2339 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2340 path_power_down_sync(codec, path);
2343 /* update jack retasking in case it modifies any of them */
2344 update_automute_all(codec);
2349 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2350 struct snd_ctl_elem_value *ucontrol)
2352 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2353 struct hda_gen_spec *spec = codec->spec;
2356 ch = ucontrol->value.enumerated.item[0];
2357 if (ch < 0 || ch > spec->multi_ios)
2359 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2361 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2362 for (i = 0; i < spec->multi_ios; i++)
2363 set_multi_io(codec, i, i < ch);
2364 spec->multiout.max_channels = max(spec->ext_channel_count,
2365 spec->const_channel_count);
2366 if (spec->need_dac_fix)
2367 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2371 static const struct snd_kcontrol_new channel_mode_enum = {
2372 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2373 .name = "Channel Mode",
2374 .info = ch_mode_info,
2379 static int create_multi_channel_mode(struct hda_codec *codec)
2381 struct hda_gen_spec *spec = codec->spec;
2383 if (spec->multi_ios > 0) {
2384 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2391 * aamix loopback enable/disable switch
2394 #define loopback_mixing_info indep_hp_info
2396 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2397 struct snd_ctl_elem_value *ucontrol)
2399 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2400 struct hda_gen_spec *spec = codec->spec;
2401 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2405 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2406 int nomix_path_idx, int mix_path_idx,
2409 struct hda_gen_spec *spec = codec->spec;
2410 struct nid_path *nomix_path, *mix_path;
2412 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2413 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2414 if (!nomix_path || !mix_path)
2417 /* if HP aamix path is driven from a different DAC and the
2418 * independent HP mode is ON, can't turn on aamix path
2420 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2421 mix_path->path[0] != spec->alt_dac_nid)
2425 snd_hda_activate_path(codec, nomix_path, false, true);
2426 snd_hda_activate_path(codec, mix_path, true, true);
2427 path_power_down_sync(codec, nomix_path);
2429 snd_hda_activate_path(codec, mix_path, false, false);
2430 snd_hda_activate_path(codec, nomix_path, true, false);
2431 path_power_down_sync(codec, mix_path);
2435 /* re-initialize the output paths; only called from loopback_mixing_put() */
2436 static void update_output_paths(struct hda_codec *codec, int num_outs,
2439 struct hda_gen_spec *spec = codec->spec;
2440 struct nid_path *path;
2443 for (i = 0; i < num_outs; i++) {
2444 path = snd_hda_get_path_from_idx(codec, paths[i]);
2446 snd_hda_activate_path(codec, path, path->active,
2451 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2452 struct snd_ctl_elem_value *ucontrol)
2454 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2455 struct hda_gen_spec *spec = codec->spec;
2456 const struct auto_pin_cfg *cfg = &spec->autocfg;
2457 unsigned int val = ucontrol->value.enumerated.item[0];
2459 if (val == spec->aamix_mode)
2461 spec->aamix_mode = val;
2462 if (has_aamix_out_paths(spec)) {
2463 update_aamix_paths(codec, val, spec->out_paths[0],
2464 spec->aamix_out_paths[0],
2465 cfg->line_out_type);
2466 update_aamix_paths(codec, val, spec->hp_paths[0],
2467 spec->aamix_out_paths[1],
2469 update_aamix_paths(codec, val, spec->speaker_paths[0],
2470 spec->aamix_out_paths[2],
2471 AUTO_PIN_SPEAKER_OUT);
2473 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2474 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2475 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2476 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2477 update_output_paths(codec, cfg->speaker_outs,
2478 spec->speaker_paths);
2483 static const struct snd_kcontrol_new loopback_mixing_enum = {
2484 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2485 .name = "Loopback Mixing",
2486 .info = loopback_mixing_info,
2487 .get = loopback_mixing_get,
2488 .put = loopback_mixing_put,
2491 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2493 struct hda_gen_spec *spec = codec->spec;
2495 if (!spec->mixer_nid)
2497 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2499 spec->have_aamix_ctl = 1;
2504 * shared headphone/mic handling
2507 static void call_update_outputs(struct hda_codec *codec);
2509 /* for shared I/O, change the pin-control accordingly */
2510 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2512 struct hda_gen_spec *spec = codec->spec;
2517 pin = spec->hp_mic_pin;
2518 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2521 val = snd_hda_codec_get_pin_target(codec, pin);
2531 val = snd_hda_get_default_vref(codec, pin);
2532 /* if the HP pin doesn't support VREF and the codec driver gives an
2533 * alternative pin, set up the VREF on that pin instead
2535 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2536 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2537 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2538 if (vref_val != AC_PINCTL_VREF_HIZ)
2539 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2540 PIN_IN | (as_mic ? vref_val : 0));
2543 if (!spec->hp_mic_jack_modes) {
2548 set_pin_target(codec, pin, val, true);
2549 call_hp_automute(codec, NULL);
2553 /* create a shared input with the headphone out */
2554 static int create_hp_mic(struct hda_codec *codec)
2556 struct hda_gen_spec *spec = codec->spec;
2557 struct auto_pin_cfg *cfg = &spec->autocfg;
2558 unsigned int defcfg;
2561 if (!spec->hp_mic) {
2562 if (spec->suppress_hp_mic_detect)
2564 /* automatic detection: only if no input or a single internal
2565 * input pin is found, try to detect the shared hp/mic
2567 if (cfg->num_inputs > 1)
2569 else if (cfg->num_inputs == 1) {
2570 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2571 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2576 spec->hp_mic = 0; /* clear once */
2577 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2581 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2582 nid = cfg->line_out_pins[0];
2583 else if (cfg->hp_outs > 0)
2584 nid = cfg->hp_pins[0];
2588 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2589 return 0; /* no input */
2591 cfg->inputs[cfg->num_inputs].pin = nid;
2592 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2593 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2596 spec->hp_mic_pin = nid;
2597 /* we can't handle auto-mic together with HP-mic */
2598 spec->suppress_auto_mic = 1;
2599 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
2607 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2609 static const char * const out_jack_texts[] = {
2610 "Line Out", "Headphone Out",
2613 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2614 struct snd_ctl_elem_info *uinfo)
2616 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2619 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2620 struct snd_ctl_elem_value *ucontrol)
2622 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2623 hda_nid_t nid = kcontrol->private_value;
2624 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2625 ucontrol->value.enumerated.item[0] = 1;
2627 ucontrol->value.enumerated.item[0] = 0;
2631 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2632 struct snd_ctl_elem_value *ucontrol)
2634 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2635 hda_nid_t nid = kcontrol->private_value;
2638 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2639 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2641 snd_hda_set_pin_ctl_cache(codec, nid, val);
2645 static const struct snd_kcontrol_new out_jack_mode_enum = {
2646 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2647 .info = out_jack_mode_info,
2648 .get = out_jack_mode_get,
2649 .put = out_jack_mode_put,
2652 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2654 struct hda_gen_spec *spec = codec->spec;
2657 for (i = 0; i < spec->kctls.used; i++) {
2658 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2659 if (!strcmp(kctl->name, name) && kctl->index == idx)
2665 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2666 char *name, size_t name_len)
2668 struct hda_gen_spec *spec = codec->spec;
2671 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2672 strlcat(name, " Jack Mode", name_len);
2674 for (; find_kctl_name(codec, name, idx); idx++)
2678 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2680 struct hda_gen_spec *spec = codec->spec;
2681 if (spec->add_jack_modes) {
2682 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2683 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2689 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2692 struct hda_gen_spec *spec = codec->spec;
2695 for (i = 0; i < num_pins; i++) {
2696 hda_nid_t pin = pins[i];
2697 if (pin == spec->hp_mic_pin)
2699 if (get_out_jack_num_items(codec, pin) > 1) {
2700 struct snd_kcontrol_new *knew;
2701 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2702 get_jack_mode_name(codec, pin, name, sizeof(name));
2703 knew = snd_hda_gen_add_kctl(spec, name,
2704 &out_jack_mode_enum);
2707 knew->private_value = pin;
2718 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2721 static const char * const vref_texts[NUM_VREFS] = {
2722 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2723 "", "Mic 80pc Bias", "Mic 100pc Bias"
2726 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2728 unsigned int pincap;
2730 pincap = snd_hda_query_pin_caps(codec, pin);
2731 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2732 /* filter out unusual vrefs */
2733 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2737 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2738 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2740 unsigned int i, n = 0;
2742 for (i = 0; i < NUM_VREFS; i++) {
2743 if (vref_caps & (1 << i)) {
2752 /* convert back from the vref ctl index to the enum item index */
2753 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2755 unsigned int i, n = 0;
2757 for (i = 0; i < NUM_VREFS; i++) {
2760 if (vref_caps & (1 << i))
2766 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2767 struct snd_ctl_elem_info *uinfo)
2769 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2770 hda_nid_t nid = kcontrol->private_value;
2771 unsigned int vref_caps = get_vref_caps(codec, nid);
2773 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2775 /* set the right text */
2776 strcpy(uinfo->value.enumerated.name,
2777 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2781 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2782 struct snd_ctl_elem_value *ucontrol)
2784 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2785 hda_nid_t nid = kcontrol->private_value;
2786 unsigned int vref_caps = get_vref_caps(codec, nid);
2789 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2790 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2794 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2795 struct snd_ctl_elem_value *ucontrol)
2797 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2798 hda_nid_t nid = kcontrol->private_value;
2799 unsigned int vref_caps = get_vref_caps(codec, nid);
2800 unsigned int val, idx;
2802 val = snd_hda_codec_get_pin_target(codec, nid);
2803 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2804 if (idx == ucontrol->value.enumerated.item[0])
2807 val &= ~AC_PINCTL_VREFEN;
2808 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2809 snd_hda_set_pin_ctl_cache(codec, nid, val);
2813 static const struct snd_kcontrol_new in_jack_mode_enum = {
2814 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2815 .info = in_jack_mode_info,
2816 .get = in_jack_mode_get,
2817 .put = in_jack_mode_put,
2820 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2822 struct hda_gen_spec *spec = codec->spec;
2824 if (spec->add_jack_modes)
2825 nitems = hweight32(get_vref_caps(codec, pin));
2826 return nitems ? nitems : 1;
2829 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2831 struct hda_gen_spec *spec = codec->spec;
2832 struct snd_kcontrol_new *knew;
2833 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2834 unsigned int defcfg;
2836 if (pin == spec->hp_mic_pin)
2837 return 0; /* already done in create_out_jack_mode() */
2839 /* no jack mode for fixed pins */
2840 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2841 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2844 /* no multiple vref caps? */
2845 if (get_in_jack_num_items(codec, pin) <= 1)
2848 get_jack_mode_name(codec, pin, name, sizeof(name));
2849 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2852 knew->private_value = pin;
2857 * HP/mic shared jack mode
2859 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2860 struct snd_ctl_elem_info *uinfo)
2862 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2863 hda_nid_t nid = kcontrol->private_value;
2864 int out_jacks = get_out_jack_num_items(codec, nid);
2865 int in_jacks = get_in_jack_num_items(codec, nid);
2866 const char *text = NULL;
2869 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2871 uinfo->value.enumerated.items = out_jacks + in_jacks;
2872 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2873 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2874 idx = uinfo->value.enumerated.item;
2875 if (idx < out_jacks) {
2877 text = out_jack_texts[idx];
2879 text = "Headphone Out";
2883 unsigned int vref_caps = get_vref_caps(codec, nid);
2884 text = vref_texts[get_vref_idx(vref_caps, idx)];
2889 strcpy(uinfo->value.enumerated.name, text);
2893 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2895 int out_jacks = get_out_jack_num_items(codec, nid);
2896 int in_jacks = get_in_jack_num_items(codec, nid);
2897 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2900 if (val & PIN_OUT) {
2901 if (out_jacks > 1 && val == PIN_HP)
2903 } else if (val & PIN_IN) {
2906 unsigned int vref_caps = get_vref_caps(codec, nid);
2907 val &= AC_PINCTL_VREFEN;
2908 idx += cvt_from_vref_idx(vref_caps, val);
2914 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2915 struct snd_ctl_elem_value *ucontrol)
2917 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2918 hda_nid_t nid = kcontrol->private_value;
2919 ucontrol->value.enumerated.item[0] =
2920 get_cur_hp_mic_jack_mode(codec, nid);
2924 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2925 struct snd_ctl_elem_value *ucontrol)
2927 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2928 hda_nid_t nid = kcontrol->private_value;
2929 int out_jacks = get_out_jack_num_items(codec, nid);
2930 int in_jacks = get_in_jack_num_items(codec, nid);
2931 unsigned int val, oldval, idx;
2933 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2934 idx = ucontrol->value.enumerated.item[0];
2938 if (idx < out_jacks) {
2940 val = idx ? PIN_HP : PIN_OUT;
2946 unsigned int vref_caps = get_vref_caps(codec, nid);
2947 val = snd_hda_codec_get_pin_target(codec, nid);
2948 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2949 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
2951 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
2953 snd_hda_set_pin_ctl_cache(codec, nid, val);
2954 call_hp_automute(codec, NULL);
2959 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2960 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2961 .info = hp_mic_jack_mode_info,
2962 .get = hp_mic_jack_mode_get,
2963 .put = hp_mic_jack_mode_put,
2966 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2968 struct hda_gen_spec *spec = codec->spec;
2969 struct snd_kcontrol_new *knew;
2971 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2972 &hp_mic_jack_mode_enum);
2975 knew->private_value = pin;
2976 spec->hp_mic_jack_modes = 1;
2984 /* add the powersave loopback-list entry */
2985 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2987 struct hda_amp_list *list;
2989 list = snd_array_new(&spec->loopback_list);
2993 list->dir = HDA_INPUT;
2995 spec->loopback.amplist = spec->loopback_list.list;
2999 /* return true if either a volume or a mute amp is found for the given
3000 * aamix path; the amp has to be either in the mixer node or its direct leaf
3002 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3003 hda_nid_t pin, unsigned int *mix_val,
3004 unsigned int *mute_val)
3007 const hda_nid_t *list;
3010 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3014 *mix_val = *mute_val = 0;
3015 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3016 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3017 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3018 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3019 if (*mix_val && *mute_val)
3022 /* check leaf node */
3023 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3024 if (num_conns < idx)
3027 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3028 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
3029 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3030 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3031 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
3032 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3034 return *mix_val || *mute_val;
3037 /* create input playback/capture controls for the given pin */
3038 static int new_analog_input(struct hda_codec *codec, int input_idx,
3039 hda_nid_t pin, const char *ctlname, int ctlidx,
3042 struct hda_gen_spec *spec = codec->spec;
3043 struct nid_path *path;
3044 unsigned int mix_val, mute_val;
3047 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3050 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
3053 print_nid_path(codec, "loopback", path);
3054 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
3056 idx = path->idx[path->depth - 1];
3058 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
3061 path->ctls[NID_PATH_VOL_CTL] = mix_val;
3065 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
3068 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
3071 path->active = true;
3072 path->stream_enabled = true; /* no DAC/ADC involved */
3073 err = add_loopback_list(spec, mix_nid, idx);
3077 if (spec->mixer_nid != spec->mixer_merge_nid &&
3078 !spec->loopback_merge_path) {
3079 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3080 spec->mixer_merge_nid, 0);
3082 print_nid_path(codec, "loopback-merge", path);
3083 path->active = true;
3084 path->pin_fixed = true; /* static route */
3085 path->stream_enabled = true; /* no DAC/ADC involved */
3086 spec->loopback_merge_path =
3087 snd_hda_get_path_idx(codec, path);
3094 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3096 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3097 return (pincap & AC_PINCAP_IN) != 0;
3100 /* Parse the codec tree and retrieve ADCs */
3101 static int fill_adc_nids(struct hda_codec *codec)
3103 struct hda_gen_spec *spec = codec->spec;
3105 hda_nid_t *adc_nids = spec->adc_nids;
3106 int max_nums = ARRAY_SIZE(spec->adc_nids);
3109 for_each_hda_codec_node(nid, codec) {
3110 unsigned int caps = get_wcaps(codec, nid);
3111 int type = get_wcaps_type(caps);
3113 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3115 adc_nids[nums] = nid;
3116 if (++nums >= max_nums)
3119 spec->num_adc_nids = nums;
3121 /* copy the detected ADCs to all_adcs[] */
3122 spec->num_all_adcs = nums;
3123 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3128 /* filter out invalid adc_nids that don't give all active input pins;
3129 * if needed, check whether dynamic ADC-switching is available
3131 static int check_dyn_adc_switch(struct hda_codec *codec)
3133 struct hda_gen_spec *spec = codec->spec;
3134 struct hda_input_mux *imux = &spec->input_mux;
3135 unsigned int ok_bits;
3140 for (n = 0; n < spec->num_adc_nids; n++) {
3141 for (i = 0; i < imux->num_items; i++) {
3142 if (!spec->input_paths[i][n])
3145 if (i >= imux->num_items) {
3146 ok_bits |= (1 << n);
3152 /* check whether ADC-switch is possible */
3153 for (i = 0; i < imux->num_items; i++) {
3154 for (n = 0; n < spec->num_adc_nids; n++) {
3155 if (spec->input_paths[i][n]) {
3156 spec->dyn_adc_idx[i] = n;
3162 codec_dbg(codec, "enabling ADC switching\n");
3163 spec->dyn_adc_switch = 1;
3164 } else if (nums != spec->num_adc_nids) {
3165 /* shrink the invalid adcs and input paths */
3167 for (n = 0; n < spec->num_adc_nids; n++) {
3168 if (!(ok_bits & (1 << n)))
3171 spec->adc_nids[nums] = spec->adc_nids[n];
3172 for (i = 0; i < imux->num_items; i++) {
3173 invalidate_nid_path(codec,
3174 spec->input_paths[i][nums]);
3175 spec->input_paths[i][nums] =
3176 spec->input_paths[i][n];
3177 spec->input_paths[i][n] = 0;
3182 spec->num_adc_nids = nums;
3185 if (imux->num_items == 1 ||
3186 (imux->num_items == 2 && spec->hp_mic)) {
3187 codec_dbg(codec, "reducing to a single ADC\n");
3188 spec->num_adc_nids = 1; /* reduce to a single ADC */
3191 /* single index for individual volumes ctls */
3192 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3193 spec->num_adc_nids = 1;
3198 /* parse capture source paths from the given pin and create imux items */
3199 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3200 int cfg_idx, int num_adcs,
3201 const char *label, int anchor)
3203 struct hda_gen_spec *spec = codec->spec;
3204 struct hda_input_mux *imux = &spec->input_mux;
3205 int imux_idx = imux->num_items;
3206 bool imux_added = false;
3209 for (c = 0; c < num_adcs; c++) {
3210 struct nid_path *path;
3211 hda_nid_t adc = spec->adc_nids[c];
3213 if (!is_reachable_path(codec, pin, adc))
3215 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3218 print_nid_path(codec, "input", path);
3219 spec->input_paths[imux_idx][c] =
3220 snd_hda_get_path_idx(codec, path);
3223 if (spec->hp_mic_pin == pin)
3224 spec->hp_mic_mux_idx = imux->num_items;
3225 spec->imux_pins[imux->num_items] = pin;
3226 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3228 if (spec->dyn_adc_switch)
3229 spec->dyn_adc_idx[imux_idx] = c;
3237 * create playback/capture controls for input pins
3240 /* fill the label for each input at first */
3241 static int fill_input_pin_labels(struct hda_codec *codec)
3243 struct hda_gen_spec *spec = codec->spec;
3244 const struct auto_pin_cfg *cfg = &spec->autocfg;
3247 for (i = 0; i < cfg->num_inputs; i++) {
3248 hda_nid_t pin = cfg->inputs[i].pin;
3252 if (!is_input_pin(codec, pin))
3255 label = hda_get_autocfg_input_label(codec, cfg, i);
3257 for (j = i - 1; j >= 0; j--) {
3258 if (spec->input_labels[j] &&
3259 !strcmp(spec->input_labels[j], label)) {
3260 idx = spec->input_label_idxs[j] + 1;
3265 spec->input_labels[i] = label;
3266 spec->input_label_idxs[i] = idx;
3272 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3274 static int create_input_ctls(struct hda_codec *codec)
3276 struct hda_gen_spec *spec = codec->spec;
3277 const struct auto_pin_cfg *cfg = &spec->autocfg;
3278 hda_nid_t mixer = spec->mixer_nid;
3283 num_adcs = fill_adc_nids(codec);
3287 err = fill_input_pin_labels(codec);
3291 for (i = 0; i < cfg->num_inputs; i++) {
3294 pin = cfg->inputs[i].pin;
3295 if (!is_input_pin(codec, pin))
3299 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3300 val |= snd_hda_get_default_vref(codec, pin);
3301 if (pin != spec->hp_mic_pin &&
3302 !snd_hda_codec_get_pin_target(codec, pin))
3303 set_pin_target(codec, pin, val, false);
3306 if (is_reachable_path(codec, pin, mixer)) {
3307 err = new_analog_input(codec, i, pin,
3308 spec->input_labels[i],
3309 spec->input_label_idxs[i],
3316 err = parse_capture_source(codec, pin, i, num_adcs,
3317 spec->input_labels[i], -mixer);
3321 if (spec->add_jack_modes) {
3322 err = create_in_jack_mode(codec, pin);
3328 /* add stereo mix when explicitly enabled via hint */
3329 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3330 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3335 spec->suppress_auto_mic = 1;
3346 /* get the input path specified by the given adc and imux indices */
3347 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3349 struct hda_gen_spec *spec = codec->spec;
3350 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3354 if (spec->dyn_adc_switch)
3355 adc_idx = spec->dyn_adc_idx[imux_idx];
3356 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3360 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3363 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3366 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3367 struct snd_ctl_elem_info *uinfo)
3369 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3370 struct hda_gen_spec *spec = codec->spec;
3371 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3374 static int mux_enum_get(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;
3379 /* the ctls are created at once with multiple counts */
3380 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3382 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3386 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3387 struct snd_ctl_elem_value *ucontrol)
3389 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3390 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3391 return mux_select(codec, adc_idx,
3392 ucontrol->value.enumerated.item[0]);
3395 static const struct snd_kcontrol_new cap_src_temp = {
3396 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3397 .name = "Input Source",
3398 .info = mux_enum_info,
3399 .get = mux_enum_get,
3400 .put = mux_enum_put,
3404 * capture volume and capture switch ctls
3407 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3408 struct snd_ctl_elem_value *ucontrol);
3410 /* call the given amp update function for all amps in the imux list at once */
3411 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3412 struct snd_ctl_elem_value *ucontrol,
3413 put_call_t func, int type)
3415 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3416 struct hda_gen_spec *spec = codec->spec;
3417 const struct hda_input_mux *imux;
3418 struct nid_path *path;
3419 int i, adc_idx, err = 0;
3421 imux = &spec->input_mux;
3422 adc_idx = kcontrol->id.index;
3423 mutex_lock(&codec->control_mutex);
3424 for (i = 0; i < imux->num_items; i++) {
3425 path = get_input_path(codec, adc_idx, i);
3426 if (!path || !path->ctls[type])
3428 kcontrol->private_value = path->ctls[type];
3429 err = func(kcontrol, ucontrol);
3433 mutex_unlock(&codec->control_mutex);
3434 if (err >= 0 && spec->cap_sync_hook)
3435 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3439 /* capture volume ctl callbacks */
3440 #define cap_vol_info snd_hda_mixer_amp_volume_info
3441 #define cap_vol_get snd_hda_mixer_amp_volume_get
3442 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3444 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3445 struct snd_ctl_elem_value *ucontrol)
3447 return cap_put_caller(kcontrol, ucontrol,
3448 snd_hda_mixer_amp_volume_put,
3452 static const struct snd_kcontrol_new cap_vol_temp = {
3453 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3454 .name = "Capture Volume",
3455 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3456 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3457 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3458 .info = cap_vol_info,
3461 .tlv = { .c = cap_vol_tlv },
3464 /* capture switch ctl callbacks */
3465 #define cap_sw_info snd_ctl_boolean_stereo_info
3466 #define cap_sw_get snd_hda_mixer_amp_switch_get
3468 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3469 struct snd_ctl_elem_value *ucontrol)
3471 return cap_put_caller(kcontrol, ucontrol,
3472 snd_hda_mixer_amp_switch_put,
3476 static const struct snd_kcontrol_new cap_sw_temp = {
3477 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3478 .name = "Capture Switch",
3479 .info = cap_sw_info,
3484 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3489 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3490 for (depth = 0; depth < 3; depth++) {
3491 if (depth >= path->depth)
3493 i = path->depth - depth - 1;
3494 nid = path->path[i];
3495 if (!path->ctls[NID_PATH_VOL_CTL]) {
3496 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3497 path->ctls[NID_PATH_VOL_CTL] =
3498 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3499 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3500 int idx = path->idx[i];
3501 if (!depth && codec->single_adc_amp)
3503 path->ctls[NID_PATH_VOL_CTL] =
3504 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3507 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3508 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3509 path->ctls[NID_PATH_MUTE_CTL] =
3510 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3511 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3512 int idx = path->idx[i];
3513 if (!depth && codec->single_adc_amp)
3515 path->ctls[NID_PATH_MUTE_CTL] =
3516 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3523 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3525 struct hda_gen_spec *spec = codec->spec;
3526 struct auto_pin_cfg *cfg = &spec->autocfg;
3530 if (!spec->inv_dmic_split)
3532 for (i = 0; i < cfg->num_inputs; i++) {
3533 if (cfg->inputs[i].pin != nid)
3535 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3537 val = snd_hda_codec_get_pincfg(codec, nid);
3538 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3543 /* capture switch put callback for a single control with hook call */
3544 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3545 struct snd_ctl_elem_value *ucontrol)
3547 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3548 struct hda_gen_spec *spec = codec->spec;
3551 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3555 if (spec->cap_sync_hook)
3556 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3561 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3562 int idx, bool is_switch, unsigned int ctl,
3565 struct hda_gen_spec *spec = codec->spec;
3566 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3567 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3568 const char *sfx = is_switch ? "Switch" : "Volume";
3569 unsigned int chs = inv_dmic ? 1 : 3;
3570 struct snd_kcontrol_new *knew;
3576 snprintf(tmpname, sizeof(tmpname),
3577 "%s Capture %s", label, sfx);
3579 snprintf(tmpname, sizeof(tmpname),
3581 knew = add_control(spec, type, tmpname, idx,
3582 amp_val_replace_channels(ctl, chs));
3586 knew->put = cap_single_sw_put;
3590 /* Make independent right kcontrol */
3592 snprintf(tmpname, sizeof(tmpname),
3593 "Inverted %s Capture %s", label, sfx);
3595 snprintf(tmpname, sizeof(tmpname),
3596 "Inverted Capture %s", sfx);
3597 knew = add_control(spec, type, tmpname, idx,
3598 amp_val_replace_channels(ctl, 2));
3602 knew->put = cap_single_sw_put;
3606 /* create single (and simple) capture volume and switch controls */
3607 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3608 unsigned int vol_ctl, unsigned int sw_ctl,
3612 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3615 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3621 /* create bound capture volume and switch controls */
3622 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3623 unsigned int vol_ctl, unsigned int sw_ctl)
3625 struct hda_gen_spec *spec = codec->spec;
3626 struct snd_kcontrol_new *knew;
3629 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3633 knew->private_value = vol_ctl;
3634 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3637 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3641 knew->private_value = sw_ctl;
3642 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3647 /* return the vol ctl when used first in the imux list */
3648 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3650 struct nid_path *path;
3654 path = get_input_path(codec, 0, idx);
3657 ctl = path->ctls[type];
3660 for (i = 0; i < idx - 1; i++) {
3661 path = get_input_path(codec, 0, i);
3662 if (path && path->ctls[type] == ctl)
3668 /* create individual capture volume and switch controls per input */
3669 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3671 struct hda_gen_spec *spec = codec->spec;
3672 struct hda_input_mux *imux = &spec->input_mux;
3675 for (i = 0; i < imux->num_items; i++) {
3679 idx = imux->items[i].index;
3680 if (idx >= spec->autocfg.num_inputs)
3682 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3684 for (type = 0; type < 2; type++) {
3685 err = add_single_cap_ctl(codec,
3686 spec->input_labels[idx],
3687 spec->input_label_idxs[idx],
3689 get_first_cap_ctl(codec, i, type),
3698 static int create_capture_mixers(struct hda_codec *codec)
3700 struct hda_gen_spec *spec = codec->spec;
3701 struct hda_input_mux *imux = &spec->input_mux;
3702 int i, n, nums, err;
3704 if (spec->dyn_adc_switch)
3707 nums = spec->num_adc_nids;
3709 if (!spec->auto_mic && imux->num_items > 1) {
3710 struct snd_kcontrol_new *knew;
3712 name = nums > 1 ? "Input Source" : "Capture Source";
3713 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3719 for (n = 0; n < nums; n++) {
3721 bool multi_cap_vol = spec->multi_cap_vol;
3722 bool inv_dmic = false;
3726 for (i = 0; i < imux->num_items; i++) {
3727 struct nid_path *path;
3728 path = get_input_path(codec, n, i);
3731 parse_capvol_in_path(codec, path);
3733 vol = path->ctls[NID_PATH_VOL_CTL];
3734 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3736 if (!same_amp_caps(codec, vol,
3737 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3738 multi_cap_vol = true;
3741 sw = path->ctls[NID_PATH_MUTE_CTL];
3742 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3744 if (!same_amp_caps(codec, sw,
3745 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3746 multi_cap_vol = true;
3748 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3753 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3755 else if (!multi_cap_vol && !inv_dmic)
3756 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3758 err = create_multi_cap_vol_ctl(codec);
3767 * add mic boosts if needed
3770 /* check whether the given amp is feasible as a boost volume */
3771 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3776 if (!nid_has_volume(codec, nid, dir) ||
3777 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3778 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3781 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3782 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3788 /* look for a boost amp in a widget close to the pin */
3789 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3790 struct nid_path *path)
3792 unsigned int val = 0;
3796 for (depth = 0; depth < 3; depth++) {
3797 if (depth >= path->depth - 1)
3799 nid = path->path[depth];
3800 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3801 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3803 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3804 path->idx[depth])) {
3805 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3814 static int parse_mic_boost(struct hda_codec *codec)
3816 struct hda_gen_spec *spec = codec->spec;
3817 struct auto_pin_cfg *cfg = &spec->autocfg;
3818 struct hda_input_mux *imux = &spec->input_mux;
3821 if (!spec->num_adc_nids)
3824 for (i = 0; i < imux->num_items; i++) {
3825 struct nid_path *path;
3828 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3830 idx = imux->items[i].index;
3831 if (idx >= imux->num_items)
3834 /* check only line-in and mic pins */
3835 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3838 path = get_input_path(codec, 0, i);
3842 val = look_for_boost_amp(codec, path);
3846 /* create a boost control */
3847 snprintf(boost_label, sizeof(boost_label),
3848 "%s Boost Volume", spec->input_labels[idx]);
3849 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3850 spec->input_label_idxs[idx], val))
3853 path->ctls[NID_PATH_BOOST_CTL] = val;
3859 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3861 static void parse_digital(struct hda_codec *codec)
3863 struct hda_gen_spec *spec = codec->spec;
3864 struct nid_path *path;
3866 hda_nid_t dig_nid, pin;
3868 /* support multiple SPDIFs; the secondary is set up as a slave */
3870 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3871 pin = spec->autocfg.dig_out_pins[i];
3872 dig_nid = look_for_dac(codec, pin, true);
3875 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3878 print_nid_path(codec, "digout", path);
3879 path->active = true;
3880 path->pin_fixed = true; /* no jack detection */
3881 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3882 set_pin_target(codec, pin, PIN_OUT, false);
3884 spec->multiout.dig_out_nid = dig_nid;
3885 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3887 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3888 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3890 spec->slave_dig_outs[nums - 1] = dig_nid;
3895 if (spec->autocfg.dig_in_pin) {
3896 pin = spec->autocfg.dig_in_pin;
3897 for_each_hda_codec_node(dig_nid, codec) {
3898 unsigned int wcaps = get_wcaps(codec, dig_nid);
3899 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3901 if (!(wcaps & AC_WCAP_DIGITAL))
3903 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3905 print_nid_path(codec, "digin", path);
3906 path->active = true;
3907 path->pin_fixed = true; /* no jack */
3908 spec->dig_in_nid = dig_nid;
3909 spec->digin_path = snd_hda_get_path_idx(codec, path);
3910 set_pin_target(codec, pin, PIN_IN, false);
3919 * input MUX handling
3922 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3924 /* select the given imux item; either unmute exclusively or select the route */
3925 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3928 struct hda_gen_spec *spec = codec->spec;
3929 const struct hda_input_mux *imux;
3930 struct nid_path *old_path, *path;
3932 imux = &spec->input_mux;
3933 if (!imux->num_items)
3936 if (idx >= imux->num_items)
3937 idx = imux->num_items - 1;
3938 if (spec->cur_mux[adc_idx] == idx)
3941 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3944 if (old_path->active)
3945 snd_hda_activate_path(codec, old_path, false, false);
3947 spec->cur_mux[adc_idx] = idx;
3950 update_hp_mic(codec, adc_idx, false);
3952 if (spec->dyn_adc_switch)
3953 dyn_adc_pcm_resetup(codec, idx);
3955 path = get_input_path(codec, adc_idx, idx);
3960 snd_hda_activate_path(codec, path, true, false);
3961 if (spec->cap_sync_hook)
3962 spec->cap_sync_hook(codec, NULL, NULL);
3963 path_power_down_sync(codec, old_path);
3967 /* power up/down widgets in the all paths that match with the given NID
3968 * as terminals (either start- or endpoint)
3970 * returns the last changed NID, or zero if unchanged.
3972 static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
3973 int pin_state, int stream_state)
3975 struct hda_gen_spec *spec = codec->spec;
3976 hda_nid_t last, changed = 0;
3977 struct nid_path *path;
3980 for (n = 0; n < spec->paths.used; n++) {
3981 path = snd_array_elem(&spec->paths, n);
3984 if (path->path[0] == nid ||
3985 path->path[path->depth - 1] == nid) {
3986 bool pin_old = path->pin_enabled;
3987 bool stream_old = path->stream_enabled;
3990 path->pin_enabled = pin_state;
3991 if (stream_state >= 0)
3992 path->stream_enabled = stream_state;
3993 if ((!path->pin_fixed && path->pin_enabled != pin_old)
3994 || path->stream_enabled != stream_old) {
3995 last = path_power_update(codec, path, true);
4004 /* check the jack status for power control */
4005 static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4007 if (!is_jack_detectable(codec, pin))
4009 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4012 /* power up/down the paths of the given pin according to the jack state;
4013 * power = 0/1 : only power up/down if it matches with the jack state,
4014 * < 0 : force power up/down to follow the jack sate
4016 * returns the last changed NID, or zero if unchanged.
4018 static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4023 if (!codec->power_save_node)
4026 on = detect_pin_state(codec, pin);
4028 if (power >= 0 && on != power)
4030 return set_path_power(codec, pin, on, -1);
4033 static void pin_power_callback(struct hda_codec *codec,
4034 struct hda_jack_callback *jack,
4037 if (jack && jack->nid)
4038 sync_power_state_change(codec,
4039 set_pin_power_jack(codec, jack->nid, on));
4042 /* callback only doing power up -- called at first */
4043 static void pin_power_up_callback(struct hda_codec *codec,
4044 struct hda_jack_callback *jack)
4046 pin_power_callback(codec, jack, true);
4049 /* callback only doing power down -- called at last */
4050 static void pin_power_down_callback(struct hda_codec *codec,
4051 struct hda_jack_callback *jack)
4053 pin_power_callback(codec, jack, false);
4056 /* set up the power up/down callbacks */
4057 static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4058 const hda_nid_t *pins, bool on)
4061 hda_jack_callback_fn cb =
4062 on ? pin_power_up_callback : pin_power_down_callback;
4064 for (i = 0; i < num_pins && pins[i]; i++) {
4065 if (is_jack_detectable(codec, pins[i]))
4066 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4068 set_path_power(codec, pins[i], true, -1);
4072 /* enabled power callback to each available I/O pin with jack detections;
4073 * the digital I/O pins are excluded because of the unreliable detectsion
4075 static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4077 struct hda_gen_spec *spec = codec->spec;
4078 struct auto_pin_cfg *cfg = &spec->autocfg;
4081 if (!codec->power_save_node)
4083 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4084 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4085 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4086 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4087 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4088 for (i = 0; i < cfg->num_inputs; i++)
4089 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4092 /* sync path power up/down with the jack states of given pins */
4093 static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4094 const hda_nid_t *pins)
4098 for (i = 0; i < num_pins && pins[i]; i++)
4099 if (is_jack_detectable(codec, pins[i]))
4100 set_pin_power_jack(codec, pins[i], -1);
4103 /* sync path power up/down with pins; called at init and resume */
4104 static void sync_all_pin_power_ctls(struct hda_codec *codec)
4106 struct hda_gen_spec *spec = codec->spec;
4107 struct auto_pin_cfg *cfg = &spec->autocfg;
4110 if (!codec->power_save_node)
4112 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4113 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4114 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4115 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4116 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4117 for (i = 0; i < cfg->num_inputs; i++)
4118 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4121 /* add fake paths if not present yet */
4122 static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4123 int num_pins, const hda_nid_t *pins)
4125 struct hda_gen_spec *spec = codec->spec;
4126 struct nid_path *path;
4129 for (i = 0; i < num_pins; i++) {
4132 if (get_nid_path(codec, nid, pins[i], 0))
4134 path = snd_array_new(&spec->paths);
4137 memset(path, 0, sizeof(*path));
4139 path->path[0] = nid;
4140 path->path[1] = pins[i];
4141 path->active = true;
4146 /* create fake paths to all outputs from beep */
4147 static int add_fake_beep_paths(struct hda_codec *codec)
4149 struct hda_gen_spec *spec = codec->spec;
4150 struct auto_pin_cfg *cfg = &spec->autocfg;
4151 hda_nid_t nid = spec->beep_nid;
4154 if (!codec->power_save_node || !nid)
4156 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4159 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4160 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4164 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4165 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4173 /* power up/down beep widget and its output paths */
4174 static void beep_power_hook(struct hda_beep *beep, bool on)
4176 set_path_power(beep->codec, beep->nid, -1, on);
4180 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4181 * @codec: the HDA codec
4182 * @pin: NID of pin to fix
4184 int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4186 struct hda_gen_spec *spec = codec->spec;
4187 struct nid_path *path;
4189 path = snd_array_new(&spec->paths);
4192 memset(path, 0, sizeof(*path));
4194 path->path[0] = pin;
4195 path->active = true;
4196 path->pin_fixed = true;
4197 path->stream_enabled = true;
4200 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4203 * Jack detections for HP auto-mute and mic-switch
4206 /* check each pin in the given array; returns true if any of them is plugged */
4207 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4210 bool present = false;
4212 for (i = 0; i < num_pins; i++) {
4213 hda_nid_t nid = pins[i];
4216 /* don't detect pins retasked as inputs */
4217 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4219 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4225 /* standard HP/line-out auto-mute helper */
4226 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
4227 int *paths, bool mute)
4229 struct hda_gen_spec *spec = codec->spec;
4232 for (i = 0; i < num_pins; i++) {
4233 hda_nid_t nid = pins[i];
4234 unsigned int val, oldval;
4238 oldval = snd_hda_codec_get_pin_target(codec, nid);
4239 if (oldval & PIN_IN)
4240 continue; /* no mute for inputs */
4242 if (spec->auto_mute_via_amp) {
4243 struct nid_path *path;
4246 path = snd_hda_get_path_from_idx(codec, paths[i]);
4249 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4253 spec->mute_bits |= (1ULL << mute_nid);
4255 spec->mute_bits &= ~(1ULL << mute_nid);
4258 /* don't reset VREF value in case it's controlling
4259 * the amp (see alc861_fixup_asus_amp_vref_0f())
4261 if (spec->keep_vref_in_automute)
4262 val = oldval & ~PIN_HP;
4267 /* here we call update_pin_ctl() so that the pinctl is
4268 * changed without changing the pinctl target value;
4269 * the original target value will be still referred at
4270 * the init / resume again
4272 update_pin_ctl(codec, nid, val);
4275 set_pin_eapd(codec, nid, !mute);
4276 if (codec->power_save_node) {
4279 on = detect_pin_state(codec, nid);
4280 set_path_power(codec, nid, on, -1);
4286 * snd_hda_gen_update_outputs - Toggle outputs muting
4287 * @codec: the HDA codec
4289 * Update the mute status of all outputs based on the current jack states.
4291 void snd_hda_gen_update_outputs(struct hda_codec *codec)
4293 struct hda_gen_spec *spec = codec->spec;
4297 /* Control HP pins/amps depending on master_mute state;
4298 * in general, HP pins/amps control should be enabled in all cases,
4299 * but currently set only for master_mute, just to be safe
4301 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4302 paths = spec->out_paths;
4304 paths = spec->hp_paths;
4305 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
4306 spec->autocfg.hp_pins, paths, spec->master_mute);
4308 if (!spec->automute_speaker)
4311 on = spec->hp_jack_present | spec->line_jack_present;
4312 on |= spec->master_mute;
4313 spec->speaker_muted = on;
4314 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4315 paths = spec->out_paths;
4317 paths = spec->speaker_paths;
4318 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
4319 spec->autocfg.speaker_pins, paths, on);
4321 /* toggle line-out mutes if needed, too */
4322 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4323 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4324 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4326 if (!spec->automute_lo)
4329 on = spec->hp_jack_present;
4330 on |= spec->master_mute;
4331 spec->line_out_muted = on;
4332 paths = spec->out_paths;
4333 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4334 spec->autocfg.line_out_pins, paths, on);
4336 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4338 static void call_update_outputs(struct hda_codec *codec)
4340 struct hda_gen_spec *spec = codec->spec;
4341 if (spec->automute_hook)
4342 spec->automute_hook(codec);
4344 snd_hda_gen_update_outputs(codec);
4346 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4347 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4348 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4352 * snd_hda_gen_hp_automute - standard HP-automute helper
4353 * @codec: the HDA codec
4354 * @jack: jack object, NULL for the whole
4356 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4357 struct hda_jack_callback *jack)
4359 struct hda_gen_spec *spec = codec->spec;
4360 hda_nid_t *pins = spec->autocfg.hp_pins;
4361 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4363 /* No detection for the first HP jack during indep-HP mode */
4364 if (spec->indep_hp_enabled) {
4369 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4370 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4372 call_update_outputs(codec);
4374 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4377 * snd_hda_gen_line_automute - standard line-out-automute helper
4378 * @codec: the HDA codec
4379 * @jack: jack object, NULL for the whole
4381 void snd_hda_gen_line_automute(struct hda_codec *codec,
4382 struct hda_jack_callback *jack)
4384 struct hda_gen_spec *spec = codec->spec;
4386 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4388 /* check LO jack only when it's different from HP */
4389 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4392 spec->line_jack_present =
4393 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4394 spec->autocfg.line_out_pins);
4395 if (!spec->automute_speaker || !spec->detect_lo)
4397 call_update_outputs(codec);
4399 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4402 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4403 * @codec: the HDA codec
4404 * @jack: jack object, NULL for the whole
4406 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4407 struct hda_jack_callback *jack)
4409 struct hda_gen_spec *spec = codec->spec;
4412 if (!spec->auto_mic)
4415 for (i = spec->am_num_entries - 1; i > 0; i--) {
4416 hda_nid_t pin = spec->am_entry[i].pin;
4417 /* don't detect pins retasked as outputs */
4418 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4420 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4421 mux_select(codec, 0, spec->am_entry[i].idx);
4425 mux_select(codec, 0, spec->am_entry[0].idx);
4427 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4429 /* call appropriate hooks */
4430 static void call_hp_automute(struct hda_codec *codec,
4431 struct hda_jack_callback *jack)
4433 struct hda_gen_spec *spec = codec->spec;
4434 if (spec->hp_automute_hook)
4435 spec->hp_automute_hook(codec, jack);
4437 snd_hda_gen_hp_automute(codec, jack);
4440 static void call_line_automute(struct hda_codec *codec,
4441 struct hda_jack_callback *jack)
4443 struct hda_gen_spec *spec = codec->spec;
4444 if (spec->line_automute_hook)
4445 spec->line_automute_hook(codec, jack);
4447 snd_hda_gen_line_automute(codec, jack);
4450 static void call_mic_autoswitch(struct hda_codec *codec,
4451 struct hda_jack_callback *jack)
4453 struct hda_gen_spec *spec = codec->spec;
4454 if (spec->mic_autoswitch_hook)
4455 spec->mic_autoswitch_hook(codec, jack);
4457 snd_hda_gen_mic_autoswitch(codec, jack);
4460 /* update jack retasking */
4461 static void update_automute_all(struct hda_codec *codec)
4463 call_hp_automute(codec, NULL);
4464 call_line_automute(codec, NULL);
4465 call_mic_autoswitch(codec, NULL);
4469 * Auto-Mute mode mixer enum support
4471 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4472 struct snd_ctl_elem_info *uinfo)
4474 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4475 struct hda_gen_spec *spec = codec->spec;
4476 static const char * const texts3[] = {
4477 "Disabled", "Speaker Only", "Line Out+Speaker"
4480 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4481 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4482 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4485 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4486 struct snd_ctl_elem_value *ucontrol)
4488 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4489 struct hda_gen_spec *spec = codec->spec;
4490 unsigned int val = 0;
4491 if (spec->automute_speaker)
4493 if (spec->automute_lo)
4496 ucontrol->value.enumerated.item[0] = val;
4500 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4501 struct snd_ctl_elem_value *ucontrol)
4503 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4504 struct hda_gen_spec *spec = codec->spec;
4506 switch (ucontrol->value.enumerated.item[0]) {
4508 if (!spec->automute_speaker && !spec->automute_lo)
4510 spec->automute_speaker = 0;
4511 spec->automute_lo = 0;
4514 if (spec->automute_speaker_possible) {
4515 if (!spec->automute_lo && spec->automute_speaker)
4517 spec->automute_speaker = 1;
4518 spec->automute_lo = 0;
4519 } else if (spec->automute_lo_possible) {
4520 if (spec->automute_lo)
4522 spec->automute_lo = 1;
4527 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4529 if (spec->automute_speaker && spec->automute_lo)
4531 spec->automute_speaker = 1;
4532 spec->automute_lo = 1;
4537 call_update_outputs(codec);
4541 static const struct snd_kcontrol_new automute_mode_enum = {
4542 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4543 .name = "Auto-Mute Mode",
4544 .info = automute_mode_info,
4545 .get = automute_mode_get,
4546 .put = automute_mode_put,
4549 static int add_automute_mode_enum(struct hda_codec *codec)
4551 struct hda_gen_spec *spec = codec->spec;
4553 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4559 * Check the availability of HP/line-out auto-mute;
4560 * Set up appropriately if really supported
4562 static int check_auto_mute_availability(struct hda_codec *codec)
4564 struct hda_gen_spec *spec = codec->spec;
4565 struct auto_pin_cfg *cfg = &spec->autocfg;
4569 if (spec->suppress_auto_mute)
4572 if (cfg->hp_pins[0])
4574 if (cfg->line_out_pins[0])
4576 if (cfg->speaker_pins[0])
4578 if (present < 2) /* need two different output types */
4581 if (!cfg->speaker_pins[0] &&
4582 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4583 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4584 sizeof(cfg->speaker_pins));
4585 cfg->speaker_outs = cfg->line_outs;
4588 if (!cfg->hp_pins[0] &&
4589 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4590 memcpy(cfg->hp_pins, cfg->line_out_pins,
4591 sizeof(cfg->hp_pins));
4592 cfg->hp_outs = cfg->line_outs;
4595 for (i = 0; i < cfg->hp_outs; i++) {
4596 hda_nid_t nid = cfg->hp_pins[i];
4597 if (!is_jack_detectable(codec, nid))
4599 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4600 snd_hda_jack_detect_enable_callback(codec, nid,
4602 spec->detect_hp = 1;
4605 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4606 if (cfg->speaker_outs)
4607 for (i = 0; i < cfg->line_outs; i++) {
4608 hda_nid_t nid = cfg->line_out_pins[i];
4609 if (!is_jack_detectable(codec, nid))
4611 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4612 snd_hda_jack_detect_enable_callback(codec, nid,
4613 call_line_automute);
4614 spec->detect_lo = 1;
4616 spec->automute_lo_possible = spec->detect_hp;
4619 spec->automute_speaker_possible = cfg->speaker_outs &&
4620 (spec->detect_hp || spec->detect_lo);
4622 spec->automute_lo = spec->automute_lo_possible;
4623 spec->automute_speaker = spec->automute_speaker_possible;
4625 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4626 /* create a control for automute mode */
4627 err = add_automute_mode_enum(codec);
4634 /* check whether all auto-mic pins are valid; setup indices if OK */
4635 static bool auto_mic_check_imux(struct hda_codec *codec)
4637 struct hda_gen_spec *spec = codec->spec;
4638 const struct hda_input_mux *imux;
4641 imux = &spec->input_mux;
4642 for (i = 0; i < spec->am_num_entries; i++) {
4643 spec->am_entry[i].idx =
4644 find_idx_in_nid_list(spec->am_entry[i].pin,
4645 spec->imux_pins, imux->num_items);
4646 if (spec->am_entry[i].idx < 0)
4647 return false; /* no corresponding imux */
4650 /* we don't need the jack detection for the first pin */
4651 for (i = 1; i < spec->am_num_entries; i++)
4652 snd_hda_jack_detect_enable_callback(codec,
4653 spec->am_entry[i].pin,
4654 call_mic_autoswitch);
4658 static int compare_attr(const void *ap, const void *bp)
4660 const struct automic_entry *a = ap;
4661 const struct automic_entry *b = bp;
4662 return (int)(a->attr - b->attr);
4666 * Check the availability of auto-mic switch;
4667 * Set up if really supported
4669 static int check_auto_mic_availability(struct hda_codec *codec)
4671 struct hda_gen_spec *spec = codec->spec;
4672 struct auto_pin_cfg *cfg = &spec->autocfg;
4676 if (spec->suppress_auto_mic)
4681 for (i = 0; i < cfg->num_inputs; i++) {
4682 hda_nid_t nid = cfg->inputs[i].pin;
4684 attr = snd_hda_codec_get_pincfg(codec, nid);
4685 attr = snd_hda_get_input_pin_attr(attr);
4686 if (types & (1 << attr))
4687 return 0; /* already occupied */
4689 case INPUT_PIN_ATTR_INT:
4690 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4691 return 0; /* invalid type */
4693 case INPUT_PIN_ATTR_UNUSED:
4694 return 0; /* invalid entry */
4696 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4697 return 0; /* invalid type */
4698 if (!spec->line_in_auto_switch &&
4699 cfg->inputs[i].type != AUTO_PIN_MIC)
4700 return 0; /* only mic is allowed */
4701 if (!is_jack_detectable(codec, nid))
4702 return 0; /* no unsol support */
4705 if (num_pins >= MAX_AUTO_MIC_PINS)
4707 types |= (1 << attr);
4708 spec->am_entry[num_pins].pin = nid;
4709 spec->am_entry[num_pins].attr = attr;
4716 spec->am_num_entries = num_pins;
4717 /* sort the am_entry in the order of attr so that the pin with a
4718 * higher attr will be selected when the jack is plugged.
4720 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4721 compare_attr, NULL);
4723 if (!auto_mic_check_imux(codec))
4727 spec->num_adc_nids = 1;
4728 spec->cur_mux[0] = spec->am_entry[0].idx;
4729 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4730 spec->am_entry[0].pin,
4731 spec->am_entry[1].pin,
4732 spec->am_entry[2].pin);
4738 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4740 * @codec: the HDA codec
4741 * @nid: NID to evalute
4742 * @power_state: target power state
4744 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4746 unsigned int power_state)
4748 struct hda_gen_spec *spec = codec->spec;
4750 if (!spec->power_down_unused && !codec->power_save_node)
4752 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
4754 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4756 if (is_active_nid_for_any(codec, nid))
4760 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4762 /* mute all aamix inputs initially; parse up to the first leaves */
4763 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4766 const hda_nid_t *conn;
4769 nums = snd_hda_get_conn_list(codec, mix, &conn);
4770 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4771 for (i = 0; i < nums; i++) {
4773 update_amp(codec, mix, HDA_INPUT, i,
4774 0xff, HDA_AMP_MUTE);
4775 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4776 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4777 0xff, HDA_AMP_MUTE);
4782 * snd_hda_gen_stream_pm - Stream power management callback
4783 * @codec: the HDA codec
4784 * @nid: audio widget
4785 * @on: power on/off flag
4787 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
4789 void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4791 if (codec->power_save_node)
4792 set_path_power(codec, nid, -1, on);
4794 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4797 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4798 * set up the hda_gen_spec
4799 * @codec: the HDA codec
4800 * @cfg: Parsed pin configuration
4802 * return 1 if successful, 0 if the proper config is not found,
4803 * or a negative error code
4805 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4806 struct auto_pin_cfg *cfg)
4808 struct hda_gen_spec *spec = codec->spec;
4811 parse_user_hints(codec);
4813 if (spec->mixer_nid && !spec->mixer_merge_nid)
4814 spec->mixer_merge_nid = spec->mixer_nid;
4816 if (cfg != &spec->autocfg) {
4817 spec->autocfg = *cfg;
4818 cfg = &spec->autocfg;
4821 if (!spec->main_out_badness)
4822 spec->main_out_badness = &hda_main_out_badness;
4823 if (!spec->extra_out_badness)
4824 spec->extra_out_badness = &hda_extra_out_badness;
4826 fill_all_dac_nids(codec);
4828 if (!cfg->line_outs) {
4829 if (cfg->dig_outs || cfg->dig_in_pin) {
4830 spec->multiout.max_channels = 2;
4831 spec->no_analog = 1;
4834 if (!cfg->num_inputs && !cfg->dig_in_pin)
4835 return 0; /* can't find valid BIOS pin config */
4838 if (!spec->no_primary_hp &&
4839 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4840 cfg->line_outs <= cfg->hp_outs) {
4841 /* use HP as primary out */
4842 cfg->speaker_outs = cfg->line_outs;
4843 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4844 sizeof(cfg->speaker_pins));
4845 cfg->line_outs = cfg->hp_outs;
4846 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4848 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4849 cfg->line_out_type = AUTO_PIN_HP_OUT;
4852 err = parse_output_paths(codec);
4855 err = create_multi_channel_mode(codec);
4858 err = create_multi_out_ctls(codec, cfg);
4861 err = create_hp_out_ctls(codec);
4864 err = create_speaker_out_ctls(codec);
4867 err = create_indep_hp_ctls(codec);
4870 err = create_loopback_mixing_ctl(codec);
4873 err = create_hp_mic(codec);
4876 err = create_input_ctls(codec);
4880 /* add power-down pin callbacks at first */
4881 add_all_pin_power_ctls(codec, false);
4883 spec->const_channel_count = spec->ext_channel_count;
4884 /* check the multiple speaker and headphone pins */
4885 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4886 spec->const_channel_count = max(spec->const_channel_count,
4887 cfg->speaker_outs * 2);
4888 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4889 spec->const_channel_count = max(spec->const_channel_count,
4891 spec->multiout.max_channels = max(spec->ext_channel_count,
4892 spec->const_channel_count);
4894 err = check_auto_mute_availability(codec);
4898 err = check_dyn_adc_switch(codec);
4902 err = check_auto_mic_availability(codec);
4906 /* add stereo mix if available and not enabled yet */
4907 if (!spec->auto_mic && spec->mixer_nid &&
4908 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4909 spec->input_mux.num_items > 1) {
4910 err = parse_capture_source(codec, spec->mixer_nid,
4911 CFG_IDX_MIX, spec->num_all_adcs,
4918 err = create_capture_mixers(codec);
4922 err = parse_mic_boost(codec);
4926 /* create "Headphone Mic Jack Mode" if no input selection is
4927 * available (or user specifies add_jack_modes hint)
4929 if (spec->hp_mic_pin &&
4930 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4931 spec->add_jack_modes)) {
4932 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4937 if (spec->add_jack_modes) {
4938 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4939 err = create_out_jack_modes(codec, cfg->line_outs,
4940 cfg->line_out_pins);
4944 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4945 err = create_out_jack_modes(codec, cfg->hp_outs,
4952 /* add power-up pin callbacks at last */
4953 add_all_pin_power_ctls(codec, true);
4955 /* mute all aamix input initially */
4956 if (spec->mixer_nid)
4957 mute_all_mixer_nid(codec, spec->mixer_nid);
4960 parse_digital(codec);
4962 if (spec->power_down_unused || codec->power_save_node) {
4963 if (!codec->power_filter)
4964 codec->power_filter = snd_hda_gen_path_power_filter;
4965 if (!codec->patch_ops.stream_pm)
4966 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
4969 if (!spec->no_analog && spec->beep_nid) {
4970 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4973 if (codec->beep && codec->power_save_node) {
4974 err = add_fake_beep_paths(codec);
4977 codec->beep->power_hook = beep_power_hook;
4983 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
4987 * Build control elements
4990 /* slave controls for virtual master */
4991 static const char * const slave_pfxs[] = {
4992 "Front", "Surround", "Center", "LFE", "Side",
4993 "Headphone", "Speaker", "Mono", "Line Out",
4994 "CLFE", "Bass Speaker", "PCM",
4995 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4996 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4997 "Headphone Side", "Headphone+LO", "Speaker+LO",
5002 * snd_hda_gen_build_controls - Build controls from the parsed results
5003 * @codec: the HDA codec
5005 * Pass this to build_controls patch_ops.
5007 int snd_hda_gen_build_controls(struct hda_codec *codec)
5009 struct hda_gen_spec *spec = codec->spec;
5012 if (spec->kctls.used) {
5013 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5018 if (spec->multiout.dig_out_nid) {
5019 err = snd_hda_create_dig_out_ctls(codec,
5020 spec->multiout.dig_out_nid,
5021 spec->multiout.dig_out_nid,
5022 spec->pcm_rec[1]->pcm_type);
5025 if (!spec->no_analog) {
5026 err = snd_hda_create_spdif_share_sw(codec,
5030 spec->multiout.share_spdif = 1;
5033 if (spec->dig_in_nid) {
5034 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5039 /* if we have no master control, let's create it */
5040 if (!spec->no_analog && !spec->suppress_vmaster &&
5041 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
5042 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
5043 spec->vmaster_tlv, slave_pfxs,
5048 if (!spec->no_analog && !spec->suppress_vmaster &&
5049 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5050 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5053 true, &spec->vmaster_mute.sw_kctl);
5056 if (spec->vmaster_mute.hook) {
5057 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5058 spec->vmaster_mute_enum);
5059 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5063 free_kctls(spec); /* no longer needed */
5065 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5071 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
5078 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5079 struct hda_codec *codec,
5080 struct snd_pcm_substream *substream,
5083 struct hda_gen_spec *spec = codec->spec;
5084 if (spec->pcm_playback_hook)
5085 spec->pcm_playback_hook(hinfo, codec, substream, action);
5088 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5089 struct hda_codec *codec,
5090 struct snd_pcm_substream *substream,
5093 struct hda_gen_spec *spec = codec->spec;
5094 if (spec->pcm_capture_hook)
5095 spec->pcm_capture_hook(hinfo, codec, substream, action);
5099 * Analog playback callbacks
5101 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5102 struct hda_codec *codec,
5103 struct snd_pcm_substream *substream)
5105 struct hda_gen_spec *spec = codec->spec;
5108 mutex_lock(&spec->pcm_mutex);
5109 err = snd_hda_multi_out_analog_open(codec,
5110 &spec->multiout, substream,
5113 spec->active_streams |= 1 << STREAM_MULTI_OUT;
5114 call_pcm_playback_hook(hinfo, codec, substream,
5115 HDA_GEN_PCM_ACT_OPEN);
5117 mutex_unlock(&spec->pcm_mutex);
5121 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5122 struct hda_codec *codec,
5123 unsigned int stream_tag,
5124 unsigned int format,
5125 struct snd_pcm_substream *substream)
5127 struct hda_gen_spec *spec = codec->spec;
5130 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5131 stream_tag, format, substream);
5133 call_pcm_playback_hook(hinfo, codec, substream,
5134 HDA_GEN_PCM_ACT_PREPARE);
5138 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5139 struct hda_codec *codec,
5140 struct snd_pcm_substream *substream)
5142 struct hda_gen_spec *spec = codec->spec;
5145 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5147 call_pcm_playback_hook(hinfo, codec, substream,
5148 HDA_GEN_PCM_ACT_CLEANUP);
5152 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5153 struct hda_codec *codec,
5154 struct snd_pcm_substream *substream)
5156 struct hda_gen_spec *spec = codec->spec;
5157 mutex_lock(&spec->pcm_mutex);
5158 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
5159 call_pcm_playback_hook(hinfo, codec, substream,
5160 HDA_GEN_PCM_ACT_CLOSE);
5161 mutex_unlock(&spec->pcm_mutex);
5165 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5166 struct hda_codec *codec,
5167 struct snd_pcm_substream *substream)
5169 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5173 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5174 struct hda_codec *codec,
5175 unsigned int stream_tag,
5176 unsigned int format,
5177 struct snd_pcm_substream *substream)
5179 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5180 call_pcm_capture_hook(hinfo, codec, substream,
5181 HDA_GEN_PCM_ACT_PREPARE);
5185 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5186 struct hda_codec *codec,
5187 struct snd_pcm_substream *substream)
5189 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5190 call_pcm_capture_hook(hinfo, codec, substream,
5191 HDA_GEN_PCM_ACT_CLEANUP);
5195 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5196 struct hda_codec *codec,
5197 struct snd_pcm_substream *substream)
5199 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5203 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5204 struct hda_codec *codec,
5205 struct snd_pcm_substream *substream)
5207 struct hda_gen_spec *spec = codec->spec;
5210 mutex_lock(&spec->pcm_mutex);
5211 if (spec->indep_hp && !spec->indep_hp_enabled)
5214 spec->active_streams |= 1 << STREAM_INDEP_HP;
5215 call_pcm_playback_hook(hinfo, codec, substream,
5216 HDA_GEN_PCM_ACT_OPEN);
5217 mutex_unlock(&spec->pcm_mutex);
5221 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5222 struct hda_codec *codec,
5223 struct snd_pcm_substream *substream)
5225 struct hda_gen_spec *spec = codec->spec;
5226 mutex_lock(&spec->pcm_mutex);
5227 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
5228 call_pcm_playback_hook(hinfo, codec, substream,
5229 HDA_GEN_PCM_ACT_CLOSE);
5230 mutex_unlock(&spec->pcm_mutex);
5234 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5235 struct hda_codec *codec,
5236 unsigned int stream_tag,
5237 unsigned int format,
5238 struct snd_pcm_substream *substream)
5240 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5241 call_pcm_playback_hook(hinfo, codec, substream,
5242 HDA_GEN_PCM_ACT_PREPARE);
5246 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5247 struct hda_codec *codec,
5248 struct snd_pcm_substream *substream)
5250 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5251 call_pcm_playback_hook(hinfo, codec, substream,
5252 HDA_GEN_PCM_ACT_CLEANUP);
5259 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5260 struct hda_codec *codec,
5261 struct snd_pcm_substream *substream)
5263 struct hda_gen_spec *spec = codec->spec;
5264 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5267 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5268 struct hda_codec *codec,
5269 unsigned int stream_tag,
5270 unsigned int format,
5271 struct snd_pcm_substream *substream)
5273 struct hda_gen_spec *spec = codec->spec;
5274 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5275 stream_tag, format, substream);
5278 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5279 struct hda_codec *codec,
5280 struct snd_pcm_substream *substream)
5282 struct hda_gen_spec *spec = codec->spec;
5283 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5286 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5287 struct hda_codec *codec,
5288 struct snd_pcm_substream *substream)
5290 struct hda_gen_spec *spec = codec->spec;
5291 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5297 #define alt_capture_pcm_open capture_pcm_open
5298 #define alt_capture_pcm_close capture_pcm_close
5300 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5301 struct hda_codec *codec,
5302 unsigned int stream_tag,
5303 unsigned int format,
5304 struct snd_pcm_substream *substream)
5306 struct hda_gen_spec *spec = codec->spec;
5308 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5309 stream_tag, 0, format);
5310 call_pcm_capture_hook(hinfo, codec, substream,
5311 HDA_GEN_PCM_ACT_PREPARE);
5315 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5316 struct hda_codec *codec,
5317 struct snd_pcm_substream *substream)
5319 struct hda_gen_spec *spec = codec->spec;
5321 snd_hda_codec_cleanup_stream(codec,
5322 spec->adc_nids[substream->number + 1]);
5323 call_pcm_capture_hook(hinfo, codec, substream,
5324 HDA_GEN_PCM_ACT_CLEANUP);
5330 static const struct hda_pcm_stream pcm_analog_playback = {
5334 /* NID is set in build_pcms */
5336 .open = playback_pcm_open,
5337 .close = playback_pcm_close,
5338 .prepare = playback_pcm_prepare,
5339 .cleanup = playback_pcm_cleanup
5343 static const struct hda_pcm_stream pcm_analog_capture = {
5347 /* NID is set in build_pcms */
5349 .open = capture_pcm_open,
5350 .close = capture_pcm_close,
5351 .prepare = capture_pcm_prepare,
5352 .cleanup = capture_pcm_cleanup
5356 static const struct hda_pcm_stream pcm_analog_alt_playback = {
5360 /* NID is set in build_pcms */
5362 .open = alt_playback_pcm_open,
5363 .close = alt_playback_pcm_close,
5364 .prepare = alt_playback_pcm_prepare,
5365 .cleanup = alt_playback_pcm_cleanup
5369 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5370 .substreams = 2, /* can be overridden */
5373 /* NID is set in build_pcms */
5375 .open = alt_capture_pcm_open,
5376 .close = alt_capture_pcm_close,
5377 .prepare = alt_capture_pcm_prepare,
5378 .cleanup = alt_capture_pcm_cleanup
5382 static const struct hda_pcm_stream pcm_digital_playback = {
5386 /* NID is set in build_pcms */
5388 .open = dig_playback_pcm_open,
5389 .close = dig_playback_pcm_close,
5390 .prepare = dig_playback_pcm_prepare,
5391 .cleanup = dig_playback_pcm_cleanup
5395 static const struct hda_pcm_stream pcm_digital_capture = {
5399 /* NID is set in build_pcms */
5402 /* Used by build_pcms to flag that a PCM has no playback stream */
5403 static const struct hda_pcm_stream pcm_null_stream = {
5410 * dynamic changing ADC PCM streams
5412 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5414 struct hda_gen_spec *spec = codec->spec;
5415 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5417 if (spec->cur_adc && spec->cur_adc != new_adc) {
5418 /* stream is running, let's swap the current ADC */
5419 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5420 spec->cur_adc = new_adc;
5421 snd_hda_codec_setup_stream(codec, new_adc,
5422 spec->cur_adc_stream_tag, 0,
5423 spec->cur_adc_format);
5429 /* analog capture with dynamic dual-adc changes */
5430 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5431 struct hda_codec *codec,
5432 unsigned int stream_tag,
5433 unsigned int format,
5434 struct snd_pcm_substream *substream)
5436 struct hda_gen_spec *spec = codec->spec;
5437 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5438 spec->cur_adc_stream_tag = stream_tag;
5439 spec->cur_adc_format = format;
5440 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5441 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
5445 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5446 struct hda_codec *codec,
5447 struct snd_pcm_substream *substream)
5449 struct hda_gen_spec *spec = codec->spec;
5450 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5452 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
5456 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5460 .nid = 0, /* fill later */
5462 .prepare = dyn_adc_capture_pcm_prepare,
5463 .cleanup = dyn_adc_capture_pcm_cleanup
5467 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5468 const char *chip_name)
5474 strlcpy(str, chip_name, len);
5476 /* drop non-alnum chars after a space */
5477 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5478 if (!isalnum(p[1])) {
5483 strlcat(str, sfx, len);
5486 /* copy PCM stream info from @default_str, and override non-NULL entries
5487 * from @spec_str and @nid
5489 static void setup_pcm_stream(struct hda_pcm_stream *str,
5490 const struct hda_pcm_stream *default_str,
5491 const struct hda_pcm_stream *spec_str,
5494 *str = *default_str;
5498 if (spec_str->substreams)
5499 str->substreams = spec_str->substreams;
5500 if (spec_str->channels_min)
5501 str->channels_min = spec_str->channels_min;
5502 if (spec_str->channels_max)
5503 str->channels_max = spec_str->channels_max;
5504 if (spec_str->rates)
5505 str->rates = spec_str->rates;
5506 if (spec_str->formats)
5507 str->formats = spec_str->formats;
5508 if (spec_str->maxbps)
5509 str->maxbps = spec_str->maxbps;
5514 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5515 * @codec: the HDA codec
5517 * Pass this to build_pcms patch_ops.
5519 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5521 struct hda_gen_spec *spec = codec->spec;
5522 struct hda_pcm *info;
5523 bool have_multi_adcs;
5525 if (spec->no_analog)
5528 fill_pcm_stream_name(spec->stream_name_analog,
5529 sizeof(spec->stream_name_analog),
5530 " Analog", codec->core.chip_name);
5531 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5534 spec->pcm_rec[0] = info;
5536 if (spec->multiout.num_dacs > 0) {
5537 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5538 &pcm_analog_playback,
5539 spec->stream_analog_playback,
5540 spec->multiout.dac_nids[0]);
5541 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5542 spec->multiout.max_channels;
5543 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5544 spec->autocfg.line_outs == 2)
5545 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5548 if (spec->num_adc_nids) {
5549 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5550 (spec->dyn_adc_switch ?
5551 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5552 spec->stream_analog_capture,
5557 /* SPDIF for stream index #1 */
5558 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5559 fill_pcm_stream_name(spec->stream_name_digital,
5560 sizeof(spec->stream_name_digital),
5561 " Digital", codec->core.chip_name);
5562 info = snd_hda_codec_pcm_new(codec, "%s",
5563 spec->stream_name_digital);
5566 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5567 spec->pcm_rec[1] = info;
5568 if (spec->dig_out_type)
5569 info->pcm_type = spec->dig_out_type;
5571 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5572 if (spec->multiout.dig_out_nid)
5573 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5574 &pcm_digital_playback,
5575 spec->stream_digital_playback,
5576 spec->multiout.dig_out_nid);
5577 if (spec->dig_in_nid)
5578 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5579 &pcm_digital_capture,
5580 spec->stream_digital_capture,
5584 if (spec->no_analog)
5587 /* If the use of more than one ADC is requested for the current
5588 * model, configure a second analog capture-only PCM.
5590 have_multi_adcs = (spec->num_adc_nids > 1) &&
5591 !spec->dyn_adc_switch && !spec->auto_mic;
5592 /* Additional Analaog capture for index #2 */
5593 if (spec->alt_dac_nid || have_multi_adcs) {
5594 fill_pcm_stream_name(spec->stream_name_alt_analog,
5595 sizeof(spec->stream_name_alt_analog),
5596 " Alt Analog", codec->core.chip_name);
5597 info = snd_hda_codec_pcm_new(codec, "%s",
5598 spec->stream_name_alt_analog);
5601 spec->pcm_rec[2] = info;
5602 if (spec->alt_dac_nid)
5603 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5604 &pcm_analog_alt_playback,
5605 spec->stream_analog_alt_playback,
5608 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5609 &pcm_null_stream, NULL, 0);
5610 if (have_multi_adcs) {
5611 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5612 &pcm_analog_alt_capture,
5613 spec->stream_analog_alt_capture,
5615 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5616 spec->num_adc_nids - 1;
5618 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5619 &pcm_null_stream, NULL, 0);
5625 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5629 * Standard auto-parser initializations
5632 /* configure the given path as a proper output */
5633 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5635 struct nid_path *path;
5638 path = snd_hda_get_path_from_idx(codec, path_idx);
5639 if (!path || !path->depth)
5641 pin = path->path[path->depth - 1];
5642 restore_pin_ctl(codec, pin);
5643 snd_hda_activate_path(codec, path, path->active,
5644 aamix_default(codec->spec));
5645 set_pin_eapd(codec, pin, path->active);
5648 /* initialize primary output paths */
5649 static void init_multi_out(struct hda_codec *codec)
5651 struct hda_gen_spec *spec = codec->spec;
5654 for (i = 0; i < spec->autocfg.line_outs; i++)
5655 set_output_and_unmute(codec, spec->out_paths[i]);
5659 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5663 for (i = 0; i < num_outs; i++)
5664 set_output_and_unmute(codec, paths[i]);
5667 /* initialize hp and speaker paths */
5668 static void init_extra_out(struct hda_codec *codec)
5670 struct hda_gen_spec *spec = codec->spec;
5672 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5673 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5674 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5675 __init_extra_out(codec, spec->autocfg.speaker_outs,
5676 spec->speaker_paths);
5679 /* initialize multi-io paths */
5680 static void init_multi_io(struct hda_codec *codec)
5682 struct hda_gen_spec *spec = codec->spec;
5685 for (i = 0; i < spec->multi_ios; i++) {
5686 hda_nid_t pin = spec->multi_io[i].pin;
5687 struct nid_path *path;
5688 path = get_multiio_path(codec, i);
5691 if (!spec->multi_io[i].ctl_in)
5692 spec->multi_io[i].ctl_in =
5693 snd_hda_codec_get_pin_target(codec, pin);
5694 snd_hda_activate_path(codec, path, path->active,
5695 aamix_default(spec));
5699 static void init_aamix_paths(struct hda_codec *codec)
5701 struct hda_gen_spec *spec = codec->spec;
5703 if (!spec->have_aamix_ctl)
5705 if (!has_aamix_out_paths(spec))
5707 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5708 spec->aamix_out_paths[0],
5709 spec->autocfg.line_out_type);
5710 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5711 spec->aamix_out_paths[1],
5713 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5714 spec->aamix_out_paths[2],
5715 AUTO_PIN_SPEAKER_OUT);
5718 /* set up input pins and loopback paths */
5719 static void init_analog_input(struct hda_codec *codec)
5721 struct hda_gen_spec *spec = codec->spec;
5722 struct auto_pin_cfg *cfg = &spec->autocfg;
5725 for (i = 0; i < cfg->num_inputs; i++) {
5726 hda_nid_t nid = cfg->inputs[i].pin;
5727 if (is_input_pin(codec, nid))
5728 restore_pin_ctl(codec, nid);
5730 /* init loopback inputs */
5731 if (spec->mixer_nid) {
5732 resume_path_from_idx(codec, spec->loopback_paths[i]);
5733 resume_path_from_idx(codec, spec->loopback_merge_path);
5738 /* initialize ADC paths */
5739 static void init_input_src(struct hda_codec *codec)
5741 struct hda_gen_spec *spec = codec->spec;
5742 struct hda_input_mux *imux = &spec->input_mux;
5743 struct nid_path *path;
5746 if (spec->dyn_adc_switch)
5749 nums = spec->num_adc_nids;
5751 for (c = 0; c < nums; c++) {
5752 for (i = 0; i < imux->num_items; i++) {
5753 path = get_input_path(codec, c, i);
5755 bool active = path->active;
5756 if (i == spec->cur_mux[c])
5758 snd_hda_activate_path(codec, path, active, false);
5762 update_hp_mic(codec, c, true);
5765 if (spec->cap_sync_hook)
5766 spec->cap_sync_hook(codec, NULL, NULL);
5769 /* set right pin controls for digital I/O */
5770 static void init_digital(struct hda_codec *codec)
5772 struct hda_gen_spec *spec = codec->spec;
5776 for (i = 0; i < spec->autocfg.dig_outs; i++)
5777 set_output_and_unmute(codec, spec->digout_paths[i]);
5778 pin = spec->autocfg.dig_in_pin;
5780 restore_pin_ctl(codec, pin);
5781 resume_path_from_idx(codec, spec->digin_path);
5785 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5786 * invalid unsol tags by some reason
5788 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5792 for (i = 0; i < codec->init_pins.used; i++) {
5793 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5794 hda_nid_t nid = pin->nid;
5795 if (is_jack_detectable(codec, nid) &&
5796 !snd_hda_jack_tbl_get(codec, nid))
5797 snd_hda_codec_update_cache(codec, nid, 0,
5798 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5803 * snd_hda_gen_init - initialize the generic spec
5804 * @codec: the HDA codec
5806 * This can be put as patch_ops init function.
5808 int snd_hda_gen_init(struct hda_codec *codec)
5810 struct hda_gen_spec *spec = codec->spec;
5812 if (spec->init_hook)
5813 spec->init_hook(codec);
5815 snd_hda_apply_verbs(codec);
5817 init_multi_out(codec);
5818 init_extra_out(codec);
5819 init_multi_io(codec);
5820 init_aamix_paths(codec);
5821 init_analog_input(codec);
5822 init_input_src(codec);
5823 init_digital(codec);
5825 clear_unsol_on_unused_pins(codec);
5827 sync_all_pin_power_ctls(codec);
5829 /* call init functions of standard auto-mute helpers */
5830 update_automute_all(codec);
5832 regcache_sync(codec->core.regmap);
5834 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5835 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5837 hda_call_check_power_status(codec, 0x01);
5840 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
5843 * snd_hda_gen_free - free the generic spec
5844 * @codec: the HDA codec
5846 * This can be put as patch_ops free function.
5848 void snd_hda_gen_free(struct hda_codec *codec)
5850 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
5851 snd_hda_gen_spec_free(codec->spec);
5855 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
5859 * snd_hda_gen_check_power_status - check the loopback power save state
5860 * @codec: the HDA codec
5861 * @nid: NID to inspect
5863 * This can be put as patch_ops check_power_status function.
5865 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5867 struct hda_gen_spec *spec = codec->spec;
5868 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5870 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
5875 * the generic codec support
5878 static const struct hda_codec_ops generic_patch_ops = {
5879 .build_controls = snd_hda_gen_build_controls,
5880 .build_pcms = snd_hda_gen_build_pcms,
5881 .init = snd_hda_gen_init,
5882 .free = snd_hda_gen_free,
5883 .unsol_event = snd_hda_jack_unsol_event,
5885 .check_power_status = snd_hda_gen_check_power_status,
5890 * snd_hda_parse_generic_codec - Generic codec parser
5891 * @codec: the HDA codec
5893 static int snd_hda_parse_generic_codec(struct hda_codec *codec)
5895 struct hda_gen_spec *spec;
5898 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5901 snd_hda_gen_spec_init(spec);
5904 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5908 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5912 codec->patch_ops = generic_patch_ops;
5916 snd_hda_gen_free(codec);
5920 static const struct hda_device_id snd_hda_id_generic[] = {
5921 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
5924 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
5926 static struct hda_codec_driver generic_driver = {
5927 .id = snd_hda_id_generic,
5930 module_hda_codec_driver(generic_driver);
5932 MODULE_LICENSE("GPL");
5933 MODULE_DESCRIPTION("Generic HD-audio codec parser");