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;
200 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
201 spec->mixer_nid = val;
205 * pin control value accesses
208 #define update_pin_ctl(codec, pin, val) \
209 snd_hda_codec_update_cache(codec, pin, 0, \
210 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
212 /* restore the pinctl based on the cached value */
213 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
215 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
218 /* set the pinctl target value and write it if requested */
219 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
220 unsigned int val, bool do_write)
224 val = snd_hda_correct_pin_ctl(codec, pin, val);
225 snd_hda_codec_set_pin_target(codec, pin, val);
227 update_pin_ctl(codec, pin, val);
230 /* set pinctl target values for all given pins */
231 static void set_pin_targets(struct hda_codec *codec, int num_pins,
232 hda_nid_t *pins, unsigned int val)
235 for (i = 0; i < num_pins; i++)
236 set_pin_target(codec, pins[i], val, false);
243 /* return the position of NID in the list, or -1 if not found */
244 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
247 for (i = 0; i < nums; i++)
253 /* return true if the given NID is contained in the path */
254 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
256 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
259 static struct nid_path *get_nid_path(struct hda_codec *codec,
260 hda_nid_t from_nid, hda_nid_t to_nid,
263 struct hda_gen_spec *spec = codec->spec;
266 for (i = 0; i < spec->paths.used; i++) {
267 struct nid_path *path = snd_array_elem(&spec->paths, i);
268 if (path->depth <= 0)
270 if ((!from_nid || path->path[0] == from_nid) &&
271 (!to_nid || path->path[path->depth - 1] == to_nid)) {
273 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
274 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
282 * snd_hda_get_nid_path - get the path between the given NIDs
283 * @codec: the HDA codec
284 * @from_nid: the NID where the path start from
285 * @to_nid: the NID where the path ends at
287 * Return the found nid_path object or NULL for error.
288 * Passing 0 to either @from_nid or @to_nid behaves as a wildcard.
290 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
291 hda_nid_t from_nid, hda_nid_t to_nid)
293 return get_nid_path(codec, from_nid, to_nid, 0);
295 EXPORT_SYMBOL_GPL(snd_hda_get_nid_path);
298 * snd_hda_get_path_idx - get the index number corresponding to the path
300 * @codec: the HDA codec
301 * @path: nid_path object
303 * The returned index starts from 1, i.e. the actual array index with offset 1,
304 * and zero is handled as an invalid path
306 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
308 struct hda_gen_spec *spec = codec->spec;
309 struct nid_path *array = spec->paths.list;
312 if (!spec->paths.used)
315 if (idx < 0 || idx >= spec->paths.used)
319 EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
322 * snd_hda_get_path_from_idx - get the path instance corresponding to the
324 * @codec: the HDA codec
325 * @idx: the path index
327 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
329 struct hda_gen_spec *spec = codec->spec;
331 if (idx <= 0 || idx > spec->paths.used)
333 return snd_array_elem(&spec->paths, idx - 1);
335 EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
337 /* check whether the given DAC is already found in any existing paths */
338 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
340 struct hda_gen_spec *spec = codec->spec;
343 for (i = 0; i < spec->paths.used; i++) {
344 struct nid_path *path = snd_array_elem(&spec->paths, i);
345 if (path->path[0] == nid)
351 /* check whether the given two widgets can be connected */
352 static bool is_reachable_path(struct hda_codec *codec,
353 hda_nid_t from_nid, hda_nid_t to_nid)
355 if (!from_nid || !to_nid)
357 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
360 /* nid, dir and idx */
361 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
363 /* check whether the given ctl is already assigned in any path elements */
364 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
366 struct hda_gen_spec *spec = codec->spec;
369 val &= AMP_VAL_COMPARE_MASK;
370 for (i = 0; i < spec->paths.used; i++) {
371 struct nid_path *path = snd_array_elem(&spec->paths, i);
372 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
378 /* check whether a control with the given (nid, dir, idx) was assigned */
379 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
380 int dir, int idx, int type)
382 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
383 return is_ctl_used(codec, val, type);
386 static void print_nid_path(struct hda_codec *codec,
387 const char *pfx, struct nid_path *path)
394 for (i = 0; i < path->depth; i++)
395 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
396 pos != buf ? ":" : "",
399 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
402 /* called recursively */
403 static bool __parse_nid_path(struct hda_codec *codec,
404 hda_nid_t from_nid, hda_nid_t to_nid,
405 int anchor_nid, struct nid_path *path,
408 const hda_nid_t *conn;
411 if (to_nid == anchor_nid)
412 anchor_nid = 0; /* anchor passed */
413 else if (to_nid == (hda_nid_t)(-anchor_nid))
414 return false; /* hit the exclusive nid */
416 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
417 for (i = 0; i < nums; i++) {
418 if (conn[i] != from_nid) {
419 /* special case: when from_nid is 0,
420 * try to find an empty DAC
423 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
424 is_dac_already_used(codec, conn[i]))
427 /* anchor is not requested or already passed? */
431 if (depth >= MAX_NID_PATH_DEPTH)
433 for (i = 0; i < nums; i++) {
435 type = get_wcaps_type(get_wcaps(codec, conn[i]));
436 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
439 if (__parse_nid_path(codec, from_nid, conn[i],
440 anchor_nid, path, depth + 1))
446 path->path[path->depth] = conn[i];
447 path->idx[path->depth + 1] = i;
448 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
449 path->multi[path->depth + 1] = 1;
455 * snd_hda_parse_nid_path - parse the widget path from the given nid to
457 * @codec: the HDA codec
458 * @from_nid: the NID where the path start from
459 * @to_nid: the NID where the path ends at
460 * @anchor_nid: the anchor indication
461 * @path: the path object to store the result
463 * Returns true if a matching path is found.
465 * The parsing behavior depends on parameters:
466 * when @from_nid is 0, try to find an empty DAC;
467 * when @anchor_nid is set to a positive value, only paths through the widget
468 * with the given value are evaluated.
469 * when @anchor_nid is set to a negative value, paths through the widget
470 * with the negative of given value are excluded, only other paths are chosen.
471 * when @anchor_nid is zero, no special handling about path selection.
473 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
474 hda_nid_t to_nid, int anchor_nid,
475 struct nid_path *path)
477 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
478 path->path[path->depth] = to_nid;
484 EXPORT_SYMBOL_GPL(snd_hda_parse_nid_path);
487 * snd_hda_add_new_path - parse the path between the given NIDs and
488 * add to the path list
489 * @codec: the HDA codec
490 * @from_nid: the NID where the path start from
491 * @to_nid: the NID where the path ends at
492 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
494 * If no valid path is found, returns NULL.
497 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
498 hda_nid_t to_nid, int anchor_nid)
500 struct hda_gen_spec *spec = codec->spec;
501 struct nid_path *path;
503 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
506 /* check whether the path has been already added */
507 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
511 path = snd_array_new(&spec->paths);
514 memset(path, 0, sizeof(*path));
515 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
521 EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
523 /* clear the given path as invalid so that it won't be picked up later */
524 static void invalidate_nid_path(struct hda_codec *codec, int idx)
526 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
529 memset(path, 0, sizeof(*path));
532 /* return a DAC if paired to the given pin by codec driver */
533 static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
535 struct hda_gen_spec *spec = codec->spec;
536 const hda_nid_t *list = spec->preferred_dacs;
540 for (; *list; list += 2)
546 /* look for an empty DAC slot */
547 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
550 struct hda_gen_spec *spec = codec->spec;
554 for (i = 0; i < spec->num_all_dacs; i++) {
555 hda_nid_t nid = spec->all_dacs[i];
556 if (!nid || is_dac_already_used(codec, nid))
558 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
559 if (is_digital != cap_digital)
561 if (is_reachable_path(codec, nid, pin))
567 /* replace the channels in the composed amp value with the given number */
568 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
570 val &= ~(0x3U << 16);
575 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
576 hda_nid_t nid2, int dir)
578 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
579 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
580 return (query_amp_caps(codec, nid1, dir) ==
581 query_amp_caps(codec, nid2, dir));
584 /* look for a widget suitable for assigning a mute switch in the path */
585 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
586 struct nid_path *path)
590 for (i = path->depth - 1; i >= 0; i--) {
591 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
592 return path->path[i];
593 if (i != path->depth - 1 && i != 0 &&
594 nid_has_mute(codec, path->path[i], HDA_INPUT))
595 return path->path[i];
600 /* look for a widget suitable for assigning a volume ctl in the path */
601 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
602 struct nid_path *path)
604 struct hda_gen_spec *spec = codec->spec;
607 for (i = path->depth - 1; i >= 0; i--) {
608 hda_nid_t nid = path->path[i];
609 if ((spec->out_vol_mask >> nid) & 1)
611 if (nid_has_volume(codec, nid, HDA_OUTPUT))
618 * path activation / deactivation
621 /* can have the amp-in capability? */
622 static bool has_amp_in(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_IN_AMP))
630 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
635 /* can have the amp-out capability? */
636 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
638 hda_nid_t nid = path->path[idx];
639 unsigned int caps = get_wcaps(codec, nid);
640 unsigned int type = get_wcaps_type(caps);
642 if (!(caps & AC_WCAP_OUT_AMP))
644 if (type == AC_WID_PIN && !idx) /* only for output pins */
649 /* check whether the given (nid,dir,idx) is active */
650 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
651 unsigned int dir, unsigned int idx)
653 struct hda_gen_spec *spec = codec->spec;
654 int type = get_wcaps_type(get_wcaps(codec, nid));
657 if (nid == codec->core.afg)
660 for (n = 0; n < spec->paths.used; n++) {
661 struct nid_path *path = snd_array_elem(&spec->paths, n);
664 if (codec->power_save_node) {
665 if (!path->stream_enabled)
667 /* ignore unplugged paths except for DAC/ADC */
668 if (!(path->pin_enabled || path->pin_fixed) &&
669 type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
672 for (i = 0; i < path->depth; i++) {
673 if (path->path[i] == nid) {
674 if (dir == HDA_OUTPUT || idx == -1 ||
684 /* check whether the NID is referred by any active paths */
685 #define is_active_nid_for_any(codec, nid) \
686 is_active_nid(codec, nid, HDA_OUTPUT, -1)
688 /* get the default amp value for the target state */
689 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
690 int dir, unsigned int caps, bool enable)
692 unsigned int val = 0;
694 if (caps & AC_AMPCAP_NUM_STEPS) {
697 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
699 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
706 /* is this a stereo widget or a stereo-to-mono mix? */
707 static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
709 unsigned int wcaps = get_wcaps(codec, nid);
712 if (wcaps & AC_WCAP_STEREO)
714 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
716 if (snd_hda_get_num_conns(codec, nid) != 1)
718 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
720 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
723 /* initialize the amp value (only at the first time) */
724 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
726 unsigned int caps = query_amp_caps(codec, nid, dir);
727 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
729 if (is_stereo_amps(codec, nid, dir))
730 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
732 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
735 /* update the amp, doing in stereo or mono depending on NID */
736 static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
737 unsigned int mask, unsigned int val)
739 if (is_stereo_amps(codec, nid, dir))
740 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
743 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
747 /* calculate amp value mask we can modify;
748 * if the given amp is controlled by mixers, don't touch it
750 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
751 hda_nid_t nid, int dir, int idx,
754 unsigned int mask = 0xff;
756 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
757 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
760 if (caps & AC_AMPCAP_NUM_STEPS) {
761 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
762 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
768 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
769 int idx, int idx_to_check, bool enable)
772 unsigned int mask, val;
774 caps = query_amp_caps(codec, nid, dir);
775 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
776 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
781 update_amp(codec, nid, dir, idx, mask, val);
784 static void check_and_activate_amp(struct hda_codec *codec, hda_nid_t nid,
785 int dir, int idx, int idx_to_check,
788 /* check whether the given amp is still used by others */
789 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
791 activate_amp(codec, nid, dir, idx, idx_to_check, enable);
794 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
797 hda_nid_t nid = path->path[i];
798 init_amp(codec, nid, HDA_OUTPUT, 0);
799 check_and_activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
802 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
803 int i, bool enable, bool add_aamix)
805 struct hda_gen_spec *spec = codec->spec;
806 const hda_nid_t *conn;
809 hda_nid_t nid = path->path[i];
811 nums = snd_hda_get_conn_list(codec, nid, &conn);
812 type = get_wcaps_type(get_wcaps(codec, nid));
813 if (type == AC_WID_PIN ||
814 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
820 for (n = 0; n < nums; n++)
821 init_amp(codec, nid, HDA_INPUT, n);
823 /* here is a little bit tricky in comparison with activate_amp_out();
824 * when aa-mixer is available, we need to enable the path as well
826 for (n = 0; n < nums; n++) {
828 if (conn[n] != spec->mixer_merge_nid)
830 /* when aamix is disabled, force to off */
832 activate_amp(codec, nid, HDA_INPUT, n, n, false);
836 check_and_activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
840 /* sync power of each widget in the the given path */
841 static hda_nid_t path_power_update(struct hda_codec *codec,
842 struct nid_path *path,
843 bool allow_powerdown)
845 hda_nid_t nid, changed = 0;
848 for (i = 0; i < path->depth; i++) {
850 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
852 if (nid == codec->core.afg)
854 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
858 power = snd_hda_codec_read(codec, nid, 0,
859 AC_VERB_GET_POWER_STATE, 0);
860 if (power != (state | (state << 4))) {
861 snd_hda_codec_write(codec, nid, 0,
862 AC_VERB_SET_POWER_STATE, state);
864 /* all known codecs seem to be capable to handl
865 * widgets state even in D3, so far.
866 * if any new codecs need to restore the widget
867 * states after D0 transition, call the function
871 if (state == AC_PWRST_D0)
872 snd_hdac_regmap_sync_node(&codec->core, nid);
879 /* do sync with the last power state change */
880 static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
884 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
889 * snd_hda_activate_path - activate or deactivate the given path
890 * @codec: the HDA codec
891 * @path: the path to activate/deactivate
892 * @enable: flag to activate or not
893 * @add_aamix: enable the input from aamix NID
895 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
897 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
898 bool enable, bool add_aamix)
900 struct hda_gen_spec *spec = codec->spec;
903 path->active = enable;
905 /* make sure the widget is powered up */
906 if (enable && (spec->power_down_unused || codec->power_save_node))
907 path_power_update(codec, path, codec->power_save_node);
909 for (i = path->depth - 1; i >= 0; i--) {
910 hda_nid_t nid = path->path[i];
912 if (enable && path->multi[i])
913 snd_hda_codec_update_cache(codec, nid, 0,
914 AC_VERB_SET_CONNECT_SEL,
916 if (has_amp_in(codec, path, i))
917 activate_amp_in(codec, path, i, enable, add_aamix);
918 if (has_amp_out(codec, path, i))
919 activate_amp_out(codec, path, i, enable);
922 EXPORT_SYMBOL_GPL(snd_hda_activate_path);
924 /* if the given path is inactive, put widgets into D3 (only if suitable) */
925 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
927 struct hda_gen_spec *spec = codec->spec;
929 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
931 sync_power_state_change(codec, path_power_update(codec, path, true));
934 /* turn on/off EAPD on the given pin */
935 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
937 struct hda_gen_spec *spec = codec->spec;
938 if (spec->own_eapd_ctl ||
939 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
941 if (spec->keep_eapd_on && !enable)
945 snd_hda_codec_update_cache(codec, pin, 0,
946 AC_VERB_SET_EAPD_BTLENABLE,
947 enable ? 0x02 : 0x00);
950 /* re-initialize the path specified by the given path index */
951 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
953 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
955 snd_hda_activate_path(codec, path, path->active, false);
960 * Helper functions for creating mixer ctl elements
963 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
964 struct snd_ctl_elem_value *ucontrol);
965 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
966 struct snd_ctl_elem_value *ucontrol);
973 static const struct snd_kcontrol_new control_templates[] = {
974 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
975 /* only the put callback is replaced for handling the special mute */
977 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
978 .subdevice = HDA_SUBDEV_AMP_FLAG,
979 .info = snd_hda_mixer_amp_switch_info,
980 .get = snd_hda_mixer_amp_switch_get,
981 .put = hda_gen_mixer_mute_put, /* replaced */
982 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
985 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
986 .info = snd_hda_mixer_amp_switch_info,
987 .get = snd_hda_mixer_bind_switch_get,
988 .put = hda_gen_bind_mute_put, /* replaced */
989 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
993 /* add dynamic controls from template */
994 static struct snd_kcontrol_new *
995 add_control(struct hda_gen_spec *spec, int type, const char *name,
996 int cidx, unsigned long val)
998 struct snd_kcontrol_new *knew;
1000 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
1004 if (get_amp_nid_(val))
1005 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
1006 knew->private_value = val;
1010 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
1011 const char *pfx, const char *dir,
1012 const char *sfx, int cidx, unsigned long val)
1014 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1015 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
1016 if (!add_control(spec, type, name, cidx, val))
1021 #define add_pb_vol_ctrl(spec, type, pfx, val) \
1022 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1023 #define add_pb_sw_ctrl(spec, type, pfx, val) \
1024 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1025 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1026 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1027 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1028 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1030 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1031 unsigned int chs, struct nid_path *path)
1036 val = path->ctls[NID_PATH_VOL_CTL];
1039 val = amp_val_replace_channels(val, chs);
1040 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1043 /* return the channel bits suitable for the given path->ctls[] */
1044 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1047 int chs = 1; /* mono (left only) */
1049 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1050 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1051 chs = 3; /* stereo */
1056 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1057 struct nid_path *path)
1059 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1060 return add_vol_ctl(codec, pfx, cidx, chs, path);
1063 /* create a mute-switch for the given mixer widget;
1064 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1066 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1067 unsigned int chs, struct nid_path *path)
1070 int type = HDA_CTL_WIDGET_MUTE;
1074 val = path->ctls[NID_PATH_MUTE_CTL];
1077 val = amp_val_replace_channels(val, chs);
1078 if (get_amp_direction_(val) == HDA_INPUT) {
1079 hda_nid_t nid = get_amp_nid_(val);
1080 int nums = snd_hda_get_num_conns(codec, nid);
1082 type = HDA_CTL_BIND_MUTE;
1086 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1089 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1090 int cidx, struct nid_path *path)
1092 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1093 return add_sw_ctl(codec, pfx, cidx, chs, path);
1096 /* playback mute control with the software mute bit check */
1097 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1098 struct snd_ctl_elem_value *ucontrol)
1100 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1101 struct hda_gen_spec *spec = codec->spec;
1103 if (spec->auto_mute_via_amp) {
1104 hda_nid_t nid = get_amp_nid(kcontrol);
1105 bool enabled = !((spec->mute_bits >> nid) & 1);
1106 ucontrol->value.integer.value[0] &= enabled;
1107 ucontrol->value.integer.value[1] &= enabled;
1111 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1112 struct snd_ctl_elem_value *ucontrol)
1114 sync_auto_mute_bits(kcontrol, ucontrol);
1115 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1118 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1119 struct snd_ctl_elem_value *ucontrol)
1121 sync_auto_mute_bits(kcontrol, ucontrol);
1122 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1125 /* any ctl assigned to the path with the given index? */
1126 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1128 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1129 return path && path->ctls[ctl_type];
1132 static const char * const channel_name[4] = {
1133 "Front", "Surround", "CLFE", "Side"
1136 /* give some appropriate ctl name prefix for the given line out channel */
1137 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1138 int *index, int ctl_type)
1140 struct hda_gen_spec *spec = codec->spec;
1141 struct auto_pin_cfg *cfg = &spec->autocfg;
1144 if (cfg->line_outs == 1 && !spec->multi_ios &&
1145 !cfg->hp_outs && !cfg->speaker_outs)
1146 return spec->vmaster_mute.hook ? "PCM" : "Master";
1148 /* if there is really a single DAC used in the whole output paths,
1149 * use it master (or "PCM" if a vmaster hook is present)
1151 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1152 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1153 return spec->vmaster_mute.hook ? "PCM" : "Master";
1155 /* multi-io channels */
1156 if (ch >= cfg->line_outs)
1157 return channel_name[ch];
1159 switch (cfg->line_out_type) {
1160 case AUTO_PIN_SPEAKER_OUT:
1161 /* if the primary channel vol/mute is shared with HP volume,
1162 * don't name it as Speaker
1164 if (!ch && cfg->hp_outs &&
1165 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1167 if (cfg->line_outs == 1)
1169 if (cfg->line_outs == 2)
1170 return ch ? "Bass Speaker" : "Speaker";
1172 case AUTO_PIN_HP_OUT:
1173 /* if the primary channel vol/mute is shared with spk volume,
1174 * don't name it as Headphone
1176 if (!ch && cfg->speaker_outs &&
1177 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1179 /* for multi-io case, only the primary out */
1180 if (ch && spec->multi_ios)
1184 case AUTO_PIN_LINE_OUT:
1185 /* This deals with the case where we have two DACs and
1186 * one LO, one HP and one Speaker */
1187 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1188 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1189 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1190 if (hp_lo_shared && spk_lo_shared)
1191 return spec->vmaster_mute.hook ? "PCM" : "Master";
1193 return "Headphone+LO";
1195 return "Speaker+LO";
1199 /* for a single channel output, we don't have to name the channel */
1200 if (cfg->line_outs == 1 && !spec->multi_ios)
1203 if (ch >= ARRAY_SIZE(channel_name)) {
1208 return channel_name[ch];
1212 * Parse output paths
1215 /* badness definition */
1217 /* No primary DAC is found for the main output */
1218 BAD_NO_PRIMARY_DAC = 0x10000,
1219 /* No DAC is found for the extra output */
1220 BAD_NO_DAC = 0x4000,
1221 /* No possible multi-ios */
1222 BAD_MULTI_IO = 0x120,
1223 /* No individual DAC for extra output */
1224 BAD_NO_EXTRA_DAC = 0x102,
1225 /* No individual DAC for extra surrounds */
1226 BAD_NO_EXTRA_SURR_DAC = 0x101,
1227 /* Primary DAC shared with main surrounds */
1228 BAD_SHARED_SURROUND = 0x100,
1229 /* No independent HP possible */
1230 BAD_NO_INDEP_HP = 0x10,
1231 /* Primary DAC shared with main CLFE */
1232 BAD_SHARED_CLFE = 0x10,
1233 /* Primary DAC shared with extra surrounds */
1234 BAD_SHARED_EXTRA_SURROUND = 0x10,
1235 /* Volume widget is shared */
1236 BAD_SHARED_VOL = 0x10,
1239 /* look for widgets in the given path which are appropriate for
1240 * volume and mute controls, and assign the values to ctls[].
1242 * When no appropriate widget is found in the path, the badness value
1243 * is incremented depending on the situation. The function returns the
1244 * total badness for both volume and mute controls.
1246 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1248 struct hda_gen_spec *spec = codec->spec;
1254 return BAD_SHARED_VOL * 2;
1256 if (path->ctls[NID_PATH_VOL_CTL] ||
1257 path->ctls[NID_PATH_MUTE_CTL])
1258 return 0; /* already evaluated */
1260 nid = look_for_out_vol_nid(codec, path);
1262 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1263 if (spec->dac_min_mute)
1264 val |= HDA_AMP_VAL_MIN_MUTE;
1265 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1266 badness += BAD_SHARED_VOL;
1268 path->ctls[NID_PATH_VOL_CTL] = val;
1270 badness += BAD_SHARED_VOL;
1271 nid = look_for_out_mute_nid(codec, path);
1273 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1274 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1275 nid_has_mute(codec, nid, HDA_OUTPUT))
1276 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1278 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1279 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1280 badness += BAD_SHARED_VOL;
1282 path->ctls[NID_PATH_MUTE_CTL] = val;
1284 badness += BAD_SHARED_VOL;
1288 const struct badness_table hda_main_out_badness = {
1289 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1290 .no_dac = BAD_NO_DAC,
1291 .shared_primary = BAD_NO_PRIMARY_DAC,
1292 .shared_surr = BAD_SHARED_SURROUND,
1293 .shared_clfe = BAD_SHARED_CLFE,
1294 .shared_surr_main = BAD_SHARED_SURROUND,
1296 EXPORT_SYMBOL_GPL(hda_main_out_badness);
1298 const struct badness_table hda_extra_out_badness = {
1299 .no_primary_dac = BAD_NO_DAC,
1300 .no_dac = BAD_NO_DAC,
1301 .shared_primary = BAD_NO_EXTRA_DAC,
1302 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1303 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1304 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1306 EXPORT_SYMBOL_GPL(hda_extra_out_badness);
1308 /* get the DAC of the primary output corresponding to the given array index */
1309 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1311 struct hda_gen_spec *spec = codec->spec;
1312 struct auto_pin_cfg *cfg = &spec->autocfg;
1314 if (cfg->line_outs > idx)
1315 return spec->private_dac_nids[idx];
1316 idx -= cfg->line_outs;
1317 if (spec->multi_ios > idx)
1318 return spec->multi_io[idx].dac;
1322 /* return the DAC if it's reachable, otherwise zero */
1323 static inline hda_nid_t try_dac(struct hda_codec *codec,
1324 hda_nid_t dac, hda_nid_t pin)
1326 return is_reachable_path(codec, dac, pin) ? dac : 0;
1329 /* try to assign DACs to pins and return the resultant badness */
1330 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1331 const hda_nid_t *pins, hda_nid_t *dacs,
1333 const struct badness_table *bad)
1335 struct hda_gen_spec *spec = codec->spec;
1343 for (i = 0; i < num_outs; i++) {
1344 struct nid_path *path;
1345 hda_nid_t pin = pins[i];
1347 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1349 badness += assign_out_path_ctls(codec, path);
1353 dacs[i] = get_preferred_dac(codec, pin);
1355 if (is_dac_already_used(codec, dacs[i]))
1356 badness += bad->shared_primary;
1360 dacs[i] = look_for_dac(codec, pin, false);
1361 if (!dacs[i] && !i) {
1362 /* try to steal the DAC of surrounds for the front */
1363 for (j = 1; j < num_outs; j++) {
1364 if (is_reachable_path(codec, dacs[j], pin)) {
1367 invalidate_nid_path(codec, path_idx[j]);
1376 dac = try_dac(codec, get_primary_out(codec, i), pin);
1378 dac = try_dac(codec, dacs[0], pin);
1380 dac = try_dac(codec, get_primary_out(codec, i), pin);
1383 badness += bad->shared_primary;
1385 badness += bad->shared_surr;
1387 badness += bad->shared_clfe;
1388 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1389 dac = spec->private_dac_nids[0];
1390 badness += bad->shared_surr_main;
1392 badness += bad->no_primary_dac;
1394 badness += bad->no_dac;
1398 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1399 if (!path && !i && spec->mixer_nid) {
1400 /* try with aamix */
1401 path = snd_hda_add_new_path(codec, dac, pin, 0);
1405 badness += bad->no_dac;
1407 /* print_nid_path(codec, "output", path); */
1408 path->active = true;
1409 path_idx[i] = snd_hda_get_path_idx(codec, path);
1410 badness += assign_out_path_ctls(codec, path);
1417 /* return NID if the given pin has only a single connection to a certain DAC */
1418 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1420 struct hda_gen_spec *spec = codec->spec;
1422 hda_nid_t nid_found = 0;
1424 for (i = 0; i < spec->num_all_dacs; i++) {
1425 hda_nid_t nid = spec->all_dacs[i];
1426 if (!nid || is_dac_already_used(codec, nid))
1428 if (is_reachable_path(codec, nid, pin)) {
1437 /* check whether the given pin can be a multi-io pin */
1438 static bool can_be_multiio_pin(struct hda_codec *codec,
1439 unsigned int location, hda_nid_t nid)
1441 unsigned int defcfg, caps;
1443 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1444 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1446 if (location && get_defcfg_location(defcfg) != location)
1448 caps = snd_hda_query_pin_caps(codec, nid);
1449 if (!(caps & AC_PINCAP_OUT))
1454 /* count the number of input pins that are capable to be multi-io */
1455 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1457 struct hda_gen_spec *spec = codec->spec;
1458 struct auto_pin_cfg *cfg = &spec->autocfg;
1459 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1460 unsigned int location = get_defcfg_location(defcfg);
1464 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1465 for (i = 0; i < cfg->num_inputs; i++) {
1466 if (cfg->inputs[i].type != type)
1468 if (can_be_multiio_pin(codec, location,
1469 cfg->inputs[i].pin))
1479 * When hardwired is set, try to fill ony hardwired pins, and returns
1480 * zero if any pins are filled, non-zero if nothing found.
1481 * When hardwired is off, try to fill possible input pins, and returns
1482 * the badness value.
1484 static int fill_multi_ios(struct hda_codec *codec,
1485 hda_nid_t reference_pin,
1488 struct hda_gen_spec *spec = codec->spec;
1489 struct auto_pin_cfg *cfg = &spec->autocfg;
1490 int type, i, j, num_pins, old_pins;
1491 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1492 unsigned int location = get_defcfg_location(defcfg);
1494 struct nid_path *path;
1496 old_pins = spec->multi_ios;
1500 num_pins = count_multiio_pins(codec, reference_pin);
1504 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1505 for (i = 0; i < cfg->num_inputs; i++) {
1506 hda_nid_t nid = cfg->inputs[i].pin;
1509 if (cfg->inputs[i].type != type)
1511 if (!can_be_multiio_pin(codec, location, nid))
1513 for (j = 0; j < spec->multi_ios; j++) {
1514 if (nid == spec->multi_io[j].pin)
1517 if (j < spec->multi_ios)
1521 dac = get_dac_if_single(codec, nid);
1523 dac = look_for_dac(codec, nid, false);
1528 path = snd_hda_add_new_path(codec, dac, nid,
1534 /* print_nid_path(codec, "multiio", path); */
1535 spec->multi_io[spec->multi_ios].pin = nid;
1536 spec->multi_io[spec->multi_ios].dac = dac;
1537 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1538 snd_hda_get_path_idx(codec, path);
1540 if (spec->multi_ios >= 2)
1546 badness = BAD_MULTI_IO;
1547 if (old_pins == spec->multi_ios) {
1549 return 1; /* nothing found */
1551 return badness; /* no badness if nothing found */
1553 if (!hardwired && spec->multi_ios < 2) {
1554 /* cancel newly assigned paths */
1555 spec->paths.used -= spec->multi_ios - old_pins;
1556 spec->multi_ios = old_pins;
1560 /* assign volume and mute controls */
1561 for (i = old_pins; i < spec->multi_ios; i++) {
1562 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1563 badness += assign_out_path_ctls(codec, path);
1569 /* map DACs for all pins in the list if they are single connections */
1570 static bool map_singles(struct hda_codec *codec, int outs,
1571 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1573 struct hda_gen_spec *spec = codec->spec;
1576 for (i = 0; i < outs; i++) {
1577 struct nid_path *path;
1581 dac = get_dac_if_single(codec, pins[i]);
1584 path = snd_hda_add_new_path(codec, dac, pins[i],
1586 if (!path && !i && spec->mixer_nid)
1587 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1591 /* print_nid_path(codec, "output", path); */
1592 path->active = true;
1593 path_idx[i] = snd_hda_get_path_idx(codec, path);
1599 static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1601 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1602 spec->aamix_out_paths[2];
1605 /* create a new path including aamix if available, and return its index */
1606 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1608 struct hda_gen_spec *spec = codec->spec;
1609 struct nid_path *path;
1610 hda_nid_t path_dac, dac, pin;
1612 path = snd_hda_get_path_from_idx(codec, path_idx);
1613 if (!path || !path->depth ||
1614 is_nid_contained(path, spec->mixer_nid))
1616 path_dac = path->path[0];
1617 dac = spec->private_dac_nids[0];
1618 pin = path->path[path->depth - 1];
1619 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1621 if (dac != path_dac)
1623 else if (spec->multiout.hp_out_nid[0])
1624 dac = spec->multiout.hp_out_nid[0];
1625 else if (spec->multiout.extra_out_nid[0])
1626 dac = spec->multiout.extra_out_nid[0];
1630 path = snd_hda_add_new_path(codec, dac, pin,
1635 /* print_nid_path(codec, "output-aamix", path); */
1636 path->active = false; /* unused as default */
1637 path->pin_fixed = true; /* static route */
1638 return snd_hda_get_path_idx(codec, path);
1641 /* check whether the independent HP is available with the current config */
1642 static bool indep_hp_possible(struct hda_codec *codec)
1644 struct hda_gen_spec *spec = codec->spec;
1645 struct auto_pin_cfg *cfg = &spec->autocfg;
1646 struct nid_path *path;
1649 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1650 idx = spec->out_paths[0];
1652 idx = spec->hp_paths[0];
1653 path = snd_hda_get_path_from_idx(codec, idx);
1657 /* assume no path conflicts unless aamix is involved */
1658 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1661 /* check whether output paths contain aamix */
1662 for (i = 0; i < cfg->line_outs; i++) {
1663 if (spec->out_paths[i] == idx)
1665 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1666 if (path && is_nid_contained(path, spec->mixer_nid))
1669 for (i = 0; i < cfg->speaker_outs; i++) {
1670 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1671 if (path && is_nid_contained(path, spec->mixer_nid))
1678 /* fill the empty entries in the dac array for speaker/hp with the
1679 * shared dac pointed by the paths
1681 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1682 hda_nid_t *dacs, int *path_idx)
1684 struct nid_path *path;
1687 for (i = 0; i < num_outs; i++) {
1690 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1693 dacs[i] = path->path[0];
1697 /* fill in the dac_nids table from the parsed pin configuration */
1698 static int fill_and_eval_dacs(struct hda_codec *codec,
1699 bool fill_hardwired,
1700 bool fill_mio_first)
1702 struct hda_gen_spec *spec = codec->spec;
1703 struct auto_pin_cfg *cfg = &spec->autocfg;
1704 int i, err, badness;
1706 /* set num_dacs once to full for look_for_dac() */
1707 spec->multiout.num_dacs = cfg->line_outs;
1708 spec->multiout.dac_nids = spec->private_dac_nids;
1709 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1710 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1711 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1712 spec->multi_ios = 0;
1713 snd_array_free(&spec->paths);
1715 /* clear path indices */
1716 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1717 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1718 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1719 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1720 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1721 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1722 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1723 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1727 /* fill hard-wired DACs first */
1728 if (fill_hardwired) {
1731 mapped = map_singles(codec, cfg->line_outs,
1733 spec->private_dac_nids,
1735 mapped |= map_singles(codec, cfg->hp_outs,
1737 spec->multiout.hp_out_nid,
1739 mapped |= map_singles(codec, cfg->speaker_outs,
1741 spec->multiout.extra_out_nid,
1742 spec->speaker_paths);
1743 if (!spec->no_multi_io &&
1744 fill_mio_first && cfg->line_outs == 1 &&
1745 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1746 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1753 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1754 spec->private_dac_nids, spec->out_paths,
1755 spec->main_out_badness);
1757 if (!spec->no_multi_io && fill_mio_first &&
1758 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1759 /* try to fill multi-io first */
1760 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1763 /* we don't count badness at this stage yet */
1766 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1767 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1768 spec->multiout.hp_out_nid,
1770 spec->extra_out_badness);
1775 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1776 err = try_assign_dacs(codec, cfg->speaker_outs,
1778 spec->multiout.extra_out_nid,
1779 spec->speaker_paths,
1780 spec->extra_out_badness);
1785 if (!spec->no_multi_io &&
1786 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1787 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1793 if (spec->mixer_nid) {
1794 spec->aamix_out_paths[0] =
1795 check_aamix_out_path(codec, spec->out_paths[0]);
1796 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1797 spec->aamix_out_paths[1] =
1798 check_aamix_out_path(codec, spec->hp_paths[0]);
1799 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1800 spec->aamix_out_paths[2] =
1801 check_aamix_out_path(codec, spec->speaker_paths[0]);
1804 if (!spec->no_multi_io &&
1805 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1806 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1807 spec->multi_ios = 1; /* give badness */
1809 /* re-count num_dacs and squash invalid entries */
1810 spec->multiout.num_dacs = 0;
1811 for (i = 0; i < cfg->line_outs; i++) {
1812 if (spec->private_dac_nids[i])
1813 spec->multiout.num_dacs++;
1815 memmove(spec->private_dac_nids + i,
1816 spec->private_dac_nids + i + 1,
1817 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1818 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1822 spec->ext_channel_count = spec->min_channel_count =
1823 spec->multiout.num_dacs * 2;
1825 if (spec->multi_ios == 2) {
1826 for (i = 0; i < 2; i++)
1827 spec->private_dac_nids[spec->multiout.num_dacs++] =
1828 spec->multi_io[i].dac;
1829 } else if (spec->multi_ios) {
1830 spec->multi_ios = 0;
1831 badness += BAD_MULTI_IO;
1834 if (spec->indep_hp && !indep_hp_possible(codec))
1835 badness += BAD_NO_INDEP_HP;
1837 /* re-fill the shared DAC for speaker / headphone */
1838 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1839 refill_shared_dacs(codec, cfg->hp_outs,
1840 spec->multiout.hp_out_nid,
1842 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1843 refill_shared_dacs(codec, cfg->speaker_outs,
1844 spec->multiout.extra_out_nid,
1845 spec->speaker_paths);
1850 #define DEBUG_BADNESS
1852 #ifdef DEBUG_BADNESS
1853 #define debug_badness(fmt, ...) \
1854 codec_dbg(codec, fmt, ##__VA_ARGS__)
1856 #define debug_badness(fmt, ...) \
1857 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1860 #ifdef DEBUG_BADNESS
1861 static inline void print_nid_path_idx(struct hda_codec *codec,
1862 const char *pfx, int idx)
1864 struct nid_path *path;
1866 path = snd_hda_get_path_from_idx(codec, idx);
1868 print_nid_path(codec, pfx, path);
1871 static void debug_show_configs(struct hda_codec *codec,
1872 struct auto_pin_cfg *cfg)
1874 struct hda_gen_spec *spec = codec->spec;
1875 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1878 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1879 cfg->line_out_pins[0], cfg->line_out_pins[1],
1880 cfg->line_out_pins[2], cfg->line_out_pins[3],
1881 spec->multiout.dac_nids[0],
1882 spec->multiout.dac_nids[1],
1883 spec->multiout.dac_nids[2],
1884 spec->multiout.dac_nids[3],
1885 lo_type[cfg->line_out_type]);
1886 for (i = 0; i < cfg->line_outs; i++)
1887 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1888 if (spec->multi_ios > 0)
1889 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1891 spec->multi_io[0].pin, spec->multi_io[1].pin,
1892 spec->multi_io[0].dac, spec->multi_io[1].dac);
1893 for (i = 0; i < spec->multi_ios; i++)
1894 print_nid_path_idx(codec, " mio",
1895 spec->out_paths[cfg->line_outs + i]);
1897 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1898 cfg->hp_pins[0], cfg->hp_pins[1],
1899 cfg->hp_pins[2], cfg->hp_pins[3],
1900 spec->multiout.hp_out_nid[0],
1901 spec->multiout.hp_out_nid[1],
1902 spec->multiout.hp_out_nid[2],
1903 spec->multiout.hp_out_nid[3]);
1904 for (i = 0; i < cfg->hp_outs; i++)
1905 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1906 if (cfg->speaker_outs)
1907 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1908 cfg->speaker_pins[0], cfg->speaker_pins[1],
1909 cfg->speaker_pins[2], cfg->speaker_pins[3],
1910 spec->multiout.extra_out_nid[0],
1911 spec->multiout.extra_out_nid[1],
1912 spec->multiout.extra_out_nid[2],
1913 spec->multiout.extra_out_nid[3]);
1914 for (i = 0; i < cfg->speaker_outs; i++)
1915 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1916 for (i = 0; i < 3; i++)
1917 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1920 #define debug_show_configs(codec, cfg) /* NOP */
1923 /* find all available DACs of the codec */
1924 static void fill_all_dac_nids(struct hda_codec *codec)
1926 struct hda_gen_spec *spec = codec->spec;
1929 spec->num_all_dacs = 0;
1930 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1931 for_each_hda_codec_node(nid, codec) {
1932 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1934 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1935 codec_err(codec, "Too many DACs!\n");
1938 spec->all_dacs[spec->num_all_dacs++] = nid;
1942 static int parse_output_paths(struct hda_codec *codec)
1944 struct hda_gen_spec *spec = codec->spec;
1945 struct auto_pin_cfg *cfg = &spec->autocfg;
1946 struct auto_pin_cfg *best_cfg;
1948 int best_badness = INT_MAX;
1950 bool fill_hardwired = true, fill_mio_first = true;
1951 bool best_wired = true, best_mio = true;
1952 bool hp_spk_swapped = false;
1954 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1960 badness = fill_and_eval_dacs(codec, fill_hardwired,
1966 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1967 cfg->line_out_type, fill_hardwired, fill_mio_first,
1969 debug_show_configs(codec, cfg);
1970 if (badness < best_badness) {
1971 best_badness = badness;
1973 best_wired = fill_hardwired;
1974 best_mio = fill_mio_first;
1978 fill_mio_first = !fill_mio_first;
1979 if (!fill_mio_first)
1981 fill_hardwired = !fill_hardwired;
1982 if (!fill_hardwired)
1986 hp_spk_swapped = true;
1987 if (cfg->speaker_outs > 0 &&
1988 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1989 cfg->hp_outs = cfg->line_outs;
1990 memcpy(cfg->hp_pins, cfg->line_out_pins,
1991 sizeof(cfg->hp_pins));
1992 cfg->line_outs = cfg->speaker_outs;
1993 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1994 sizeof(cfg->speaker_pins));
1995 cfg->speaker_outs = 0;
1996 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1997 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1998 fill_hardwired = true;
2001 if (cfg->hp_outs > 0 &&
2002 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2003 cfg->speaker_outs = cfg->line_outs;
2004 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2005 sizeof(cfg->speaker_pins));
2006 cfg->line_outs = cfg->hp_outs;
2007 memcpy(cfg->line_out_pins, cfg->hp_pins,
2008 sizeof(cfg->hp_pins));
2010 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2011 cfg->line_out_type = AUTO_PIN_HP_OUT;
2012 fill_hardwired = true;
2019 debug_badness("==> restoring best_cfg\n");
2021 fill_and_eval_dacs(codec, best_wired, best_mio);
2023 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2024 cfg->line_out_type, best_wired, best_mio);
2025 debug_show_configs(codec, cfg);
2027 if (cfg->line_out_pins[0]) {
2028 struct nid_path *path;
2029 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
2031 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
2032 if (spec->vmaster_nid) {
2033 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2034 HDA_OUTPUT, spec->vmaster_tlv);
2035 if (spec->dac_min_mute)
2036 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2040 /* set initial pinctl targets */
2041 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2045 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2046 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2047 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2048 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2049 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2050 set_pin_targets(codec, cfg->speaker_outs,
2051 cfg->speaker_pins, val);
2054 /* clear indep_hp flag if not available */
2055 if (spec->indep_hp && !indep_hp_possible(codec))
2062 /* add playback controls from the parsed DAC table */
2063 static int create_multi_out_ctls(struct hda_codec *codec,
2064 const struct auto_pin_cfg *cfg)
2066 struct hda_gen_spec *spec = codec->spec;
2067 int i, err, noutputs;
2069 noutputs = cfg->line_outs;
2070 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2071 noutputs += spec->multi_ios;
2073 for (i = 0; i < noutputs; i++) {
2076 struct nid_path *path;
2078 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
2082 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
2083 if (!name || !strcmp(name, "CLFE")) {
2085 err = add_vol_ctl(codec, "Center", 0, 1, path);
2088 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2092 err = add_stereo_vol(codec, name, index, path);
2097 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2098 if (!name || !strcmp(name, "CLFE")) {
2099 err = add_sw_ctl(codec, "Center", 0, 1, path);
2102 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2106 err = add_stereo_sw(codec, name, index, path);
2114 static int create_extra_out(struct hda_codec *codec, int path_idx,
2115 const char *pfx, int cidx)
2117 struct nid_path *path;
2120 path = snd_hda_get_path_from_idx(codec, path_idx);
2123 err = add_stereo_vol(codec, pfx, cidx, path);
2126 err = add_stereo_sw(codec, pfx, cidx, path);
2132 /* add playback controls for speaker and HP outputs */
2133 static int create_extra_outs(struct hda_codec *codec, int num_pins,
2134 const int *paths, const char *pfx)
2138 for (i = 0; i < num_pins; i++) {
2140 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2143 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2144 name = "Bass Speaker";
2145 else if (num_pins >= 3) {
2146 snprintf(tmp, sizeof(tmp), "%s %s",
2147 pfx, channel_name[i]);
2153 err = create_extra_out(codec, paths[i], name, idx);
2160 static int create_hp_out_ctls(struct hda_codec *codec)
2162 struct hda_gen_spec *spec = codec->spec;
2163 return create_extra_outs(codec, spec->autocfg.hp_outs,
2168 static int create_speaker_out_ctls(struct hda_codec *codec)
2170 struct hda_gen_spec *spec = codec->spec;
2171 return create_extra_outs(codec, spec->autocfg.speaker_outs,
2172 spec->speaker_paths,
2177 * independent HP controls
2180 static void call_hp_automute(struct hda_codec *codec,
2181 struct hda_jack_callback *jack);
2182 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2183 struct snd_ctl_elem_info *uinfo)
2185 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2188 static int indep_hp_get(struct snd_kcontrol *kcontrol,
2189 struct snd_ctl_elem_value *ucontrol)
2191 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2192 struct hda_gen_spec *spec = codec->spec;
2193 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2197 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2198 int nomix_path_idx, int mix_path_idx,
2201 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2202 struct snd_ctl_elem_value *ucontrol)
2204 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2205 struct hda_gen_spec *spec = codec->spec;
2206 unsigned int select = ucontrol->value.enumerated.item[0];
2209 mutex_lock(&spec->pcm_mutex);
2210 if (spec->active_streams) {
2215 if (spec->indep_hp_enabled != select) {
2217 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2218 dacp = &spec->private_dac_nids[0];
2220 dacp = &spec->multiout.hp_out_nid[0];
2222 /* update HP aamix paths in case it conflicts with indep HP */
2223 if (spec->have_aamix_ctl) {
2224 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2225 update_aamix_paths(codec, spec->aamix_mode,
2227 spec->aamix_out_paths[0],
2228 spec->autocfg.line_out_type);
2230 update_aamix_paths(codec, spec->aamix_mode,
2232 spec->aamix_out_paths[1],
2236 spec->indep_hp_enabled = select;
2237 if (spec->indep_hp_enabled)
2240 *dacp = spec->alt_dac_nid;
2242 call_hp_automute(codec, NULL);
2246 mutex_unlock(&spec->pcm_mutex);
2250 static const struct snd_kcontrol_new indep_hp_ctl = {
2251 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2252 .name = "Independent HP",
2253 .info = indep_hp_info,
2254 .get = indep_hp_get,
2255 .put = indep_hp_put,
2259 static int create_indep_hp_ctls(struct hda_codec *codec)
2261 struct hda_gen_spec *spec = codec->spec;
2264 if (!spec->indep_hp)
2266 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2267 dac = spec->multiout.dac_nids[0];
2269 dac = spec->multiout.hp_out_nid[0];
2275 spec->indep_hp_enabled = false;
2276 spec->alt_dac_nid = dac;
2277 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2283 * channel mode enum control
2286 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2287 struct snd_ctl_elem_info *uinfo)
2289 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2290 struct hda_gen_spec *spec = codec->spec;
2293 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2295 uinfo->value.enumerated.items = spec->multi_ios + 1;
2296 if (uinfo->value.enumerated.item > spec->multi_ios)
2297 uinfo->value.enumerated.item = spec->multi_ios;
2298 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2299 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2303 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2304 struct snd_ctl_elem_value *ucontrol)
2306 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2307 struct hda_gen_spec *spec = codec->spec;
2308 ucontrol->value.enumerated.item[0] =
2309 (spec->ext_channel_count - spec->min_channel_count) / 2;
2313 static inline struct nid_path *
2314 get_multiio_path(struct hda_codec *codec, int idx)
2316 struct hda_gen_spec *spec = codec->spec;
2317 return snd_hda_get_path_from_idx(codec,
2318 spec->out_paths[spec->autocfg.line_outs + idx]);
2321 static void update_automute_all(struct hda_codec *codec);
2323 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2324 * used for output paths
2326 static bool aamix_default(struct hda_gen_spec *spec)
2328 return !spec->have_aamix_ctl || spec->aamix_mode;
2331 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2333 struct hda_gen_spec *spec = codec->spec;
2334 hda_nid_t nid = spec->multi_io[idx].pin;
2335 struct nid_path *path;
2337 path = get_multiio_path(codec, idx);
2341 if (path->active == output)
2345 set_pin_target(codec, nid, PIN_OUT, true);
2346 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2347 set_pin_eapd(codec, nid, true);
2349 set_pin_eapd(codec, nid, false);
2350 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2351 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2352 path_power_down_sync(codec, path);
2355 /* update jack retasking in case it modifies any of them */
2356 update_automute_all(codec);
2361 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2362 struct snd_ctl_elem_value *ucontrol)
2364 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2365 struct hda_gen_spec *spec = codec->spec;
2368 ch = ucontrol->value.enumerated.item[0];
2369 if (ch < 0 || ch > spec->multi_ios)
2371 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2373 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2374 for (i = 0; i < spec->multi_ios; i++)
2375 set_multi_io(codec, i, i < ch);
2376 spec->multiout.max_channels = max(spec->ext_channel_count,
2377 spec->const_channel_count);
2378 if (spec->need_dac_fix)
2379 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2383 static const struct snd_kcontrol_new channel_mode_enum = {
2384 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2385 .name = "Channel Mode",
2386 .info = ch_mode_info,
2391 static int create_multi_channel_mode(struct hda_codec *codec)
2393 struct hda_gen_spec *spec = codec->spec;
2395 if (spec->multi_ios > 0) {
2396 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2403 * aamix loopback enable/disable switch
2406 #define loopback_mixing_info indep_hp_info
2408 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2409 struct snd_ctl_elem_value *ucontrol)
2411 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2412 struct hda_gen_spec *spec = codec->spec;
2413 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2417 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2418 int nomix_path_idx, int mix_path_idx,
2421 struct hda_gen_spec *spec = codec->spec;
2422 struct nid_path *nomix_path, *mix_path;
2424 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2425 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2426 if (!nomix_path || !mix_path)
2429 /* if HP aamix path is driven from a different DAC and the
2430 * independent HP mode is ON, can't turn on aamix path
2432 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2433 mix_path->path[0] != spec->alt_dac_nid)
2437 snd_hda_activate_path(codec, nomix_path, false, true);
2438 snd_hda_activate_path(codec, mix_path, true, true);
2439 path_power_down_sync(codec, nomix_path);
2441 snd_hda_activate_path(codec, mix_path, false, false);
2442 snd_hda_activate_path(codec, nomix_path, true, false);
2443 path_power_down_sync(codec, mix_path);
2447 /* re-initialize the output paths; only called from loopback_mixing_put() */
2448 static void update_output_paths(struct hda_codec *codec, int num_outs,
2451 struct hda_gen_spec *spec = codec->spec;
2452 struct nid_path *path;
2455 for (i = 0; i < num_outs; i++) {
2456 path = snd_hda_get_path_from_idx(codec, paths[i]);
2458 snd_hda_activate_path(codec, path, path->active,
2463 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2464 struct snd_ctl_elem_value *ucontrol)
2466 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2467 struct hda_gen_spec *spec = codec->spec;
2468 const struct auto_pin_cfg *cfg = &spec->autocfg;
2469 unsigned int val = ucontrol->value.enumerated.item[0];
2471 if (val == spec->aamix_mode)
2473 spec->aamix_mode = val;
2474 if (has_aamix_out_paths(spec)) {
2475 update_aamix_paths(codec, val, spec->out_paths[0],
2476 spec->aamix_out_paths[0],
2477 cfg->line_out_type);
2478 update_aamix_paths(codec, val, spec->hp_paths[0],
2479 spec->aamix_out_paths[1],
2481 update_aamix_paths(codec, val, spec->speaker_paths[0],
2482 spec->aamix_out_paths[2],
2483 AUTO_PIN_SPEAKER_OUT);
2485 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2486 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2487 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2488 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2489 update_output_paths(codec, cfg->speaker_outs,
2490 spec->speaker_paths);
2495 static const struct snd_kcontrol_new loopback_mixing_enum = {
2496 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2497 .name = "Loopback Mixing",
2498 .info = loopback_mixing_info,
2499 .get = loopback_mixing_get,
2500 .put = loopback_mixing_put,
2503 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2505 struct hda_gen_spec *spec = codec->spec;
2507 if (!spec->mixer_nid)
2509 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2511 spec->have_aamix_ctl = 1;
2512 /* if no explicit aamix path is present (e.g. for Realtek codecs),
2513 * enable aamix as default -- just for compatibility
2515 spec->aamix_mode = !has_aamix_out_paths(spec);
2520 * shared headphone/mic handling
2523 static void call_update_outputs(struct hda_codec *codec);
2525 /* for shared I/O, change the pin-control accordingly */
2526 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2528 struct hda_gen_spec *spec = codec->spec;
2533 pin = spec->hp_mic_pin;
2534 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2537 val = snd_hda_codec_get_pin_target(codec, pin);
2547 val = snd_hda_get_default_vref(codec, pin);
2548 /* if the HP pin doesn't support VREF and the codec driver gives an
2549 * alternative pin, set up the VREF on that pin instead
2551 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2552 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2553 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2554 if (vref_val != AC_PINCTL_VREF_HIZ)
2555 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2556 PIN_IN | (as_mic ? vref_val : 0));
2559 if (!spec->hp_mic_jack_modes) {
2564 set_pin_target(codec, pin, val, true);
2565 call_hp_automute(codec, NULL);
2569 /* create a shared input with the headphone out */
2570 static int create_hp_mic(struct hda_codec *codec)
2572 struct hda_gen_spec *spec = codec->spec;
2573 struct auto_pin_cfg *cfg = &spec->autocfg;
2574 unsigned int defcfg;
2577 if (!spec->hp_mic) {
2578 if (spec->suppress_hp_mic_detect)
2580 /* automatic detection: only if no input or a single internal
2581 * input pin is found, try to detect the shared hp/mic
2583 if (cfg->num_inputs > 1)
2585 else if (cfg->num_inputs == 1) {
2586 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2587 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2592 spec->hp_mic = 0; /* clear once */
2593 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2597 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2598 nid = cfg->line_out_pins[0];
2599 else if (cfg->hp_outs > 0)
2600 nid = cfg->hp_pins[0];
2604 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2605 return 0; /* no input */
2607 cfg->inputs[cfg->num_inputs].pin = nid;
2608 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2609 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2612 spec->hp_mic_pin = nid;
2613 /* we can't handle auto-mic together with HP-mic */
2614 spec->suppress_auto_mic = 1;
2615 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
2623 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2625 static const char * const out_jack_texts[] = {
2626 "Line Out", "Headphone Out",
2629 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2630 struct snd_ctl_elem_info *uinfo)
2632 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2635 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2636 struct snd_ctl_elem_value *ucontrol)
2638 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2639 hda_nid_t nid = kcontrol->private_value;
2640 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2641 ucontrol->value.enumerated.item[0] = 1;
2643 ucontrol->value.enumerated.item[0] = 0;
2647 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2648 struct snd_ctl_elem_value *ucontrol)
2650 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2651 hda_nid_t nid = kcontrol->private_value;
2654 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2655 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2657 snd_hda_set_pin_ctl_cache(codec, nid, val);
2661 static const struct snd_kcontrol_new out_jack_mode_enum = {
2662 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2663 .info = out_jack_mode_info,
2664 .get = out_jack_mode_get,
2665 .put = out_jack_mode_put,
2668 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2670 struct hda_gen_spec *spec = codec->spec;
2673 for (i = 0; i < spec->kctls.used; i++) {
2674 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2675 if (!strcmp(kctl->name, name) && kctl->index == idx)
2681 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2682 char *name, size_t name_len)
2684 struct hda_gen_spec *spec = codec->spec;
2687 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2688 strlcat(name, " Jack Mode", name_len);
2690 for (; find_kctl_name(codec, name, idx); idx++)
2694 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2696 struct hda_gen_spec *spec = codec->spec;
2697 if (spec->add_jack_modes) {
2698 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2699 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2705 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2708 struct hda_gen_spec *spec = codec->spec;
2711 for (i = 0; i < num_pins; i++) {
2712 hda_nid_t pin = pins[i];
2713 if (pin == spec->hp_mic_pin)
2715 if (get_out_jack_num_items(codec, pin) > 1) {
2716 struct snd_kcontrol_new *knew;
2717 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2718 get_jack_mode_name(codec, pin, name, sizeof(name));
2719 knew = snd_hda_gen_add_kctl(spec, name,
2720 &out_jack_mode_enum);
2723 knew->private_value = pin;
2734 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2737 static const char * const vref_texts[NUM_VREFS] = {
2738 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2739 "", "Mic 80pc Bias", "Mic 100pc Bias"
2742 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2744 unsigned int pincap;
2746 pincap = snd_hda_query_pin_caps(codec, pin);
2747 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2748 /* filter out unusual vrefs */
2749 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2753 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2754 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2756 unsigned int i, n = 0;
2758 for (i = 0; i < NUM_VREFS; i++) {
2759 if (vref_caps & (1 << i)) {
2768 /* convert back from the vref ctl index to the enum item index */
2769 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2771 unsigned int i, n = 0;
2773 for (i = 0; i < NUM_VREFS; i++) {
2776 if (vref_caps & (1 << i))
2782 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2783 struct snd_ctl_elem_info *uinfo)
2785 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2786 hda_nid_t nid = kcontrol->private_value;
2787 unsigned int vref_caps = get_vref_caps(codec, nid);
2789 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2791 /* set the right text */
2792 strcpy(uinfo->value.enumerated.name,
2793 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2797 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2798 struct snd_ctl_elem_value *ucontrol)
2800 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2801 hda_nid_t nid = kcontrol->private_value;
2802 unsigned int vref_caps = get_vref_caps(codec, nid);
2805 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2806 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2810 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2811 struct snd_ctl_elem_value *ucontrol)
2813 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2814 hda_nid_t nid = kcontrol->private_value;
2815 unsigned int vref_caps = get_vref_caps(codec, nid);
2816 unsigned int val, idx;
2818 val = snd_hda_codec_get_pin_target(codec, nid);
2819 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2820 if (idx == ucontrol->value.enumerated.item[0])
2823 val &= ~AC_PINCTL_VREFEN;
2824 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2825 snd_hda_set_pin_ctl_cache(codec, nid, val);
2829 static const struct snd_kcontrol_new in_jack_mode_enum = {
2830 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2831 .info = in_jack_mode_info,
2832 .get = in_jack_mode_get,
2833 .put = in_jack_mode_put,
2836 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2838 struct hda_gen_spec *spec = codec->spec;
2840 if (spec->add_jack_modes)
2841 nitems = hweight32(get_vref_caps(codec, pin));
2842 return nitems ? nitems : 1;
2845 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2847 struct hda_gen_spec *spec = codec->spec;
2848 struct snd_kcontrol_new *knew;
2849 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2850 unsigned int defcfg;
2852 if (pin == spec->hp_mic_pin)
2853 return 0; /* already done in create_out_jack_mode() */
2855 /* no jack mode for fixed pins */
2856 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2857 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2860 /* no multiple vref caps? */
2861 if (get_in_jack_num_items(codec, pin) <= 1)
2864 get_jack_mode_name(codec, pin, name, sizeof(name));
2865 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2868 knew->private_value = pin;
2873 * HP/mic shared jack mode
2875 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2876 struct snd_ctl_elem_info *uinfo)
2878 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2879 hda_nid_t nid = kcontrol->private_value;
2880 int out_jacks = get_out_jack_num_items(codec, nid);
2881 int in_jacks = get_in_jack_num_items(codec, nid);
2882 const char *text = NULL;
2885 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2887 uinfo->value.enumerated.items = out_jacks + in_jacks;
2888 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2889 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2890 idx = uinfo->value.enumerated.item;
2891 if (idx < out_jacks) {
2893 text = out_jack_texts[idx];
2895 text = "Headphone Out";
2899 unsigned int vref_caps = get_vref_caps(codec, nid);
2900 text = vref_texts[get_vref_idx(vref_caps, idx)];
2905 strcpy(uinfo->value.enumerated.name, text);
2909 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2911 int out_jacks = get_out_jack_num_items(codec, nid);
2912 int in_jacks = get_in_jack_num_items(codec, nid);
2913 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2916 if (val & PIN_OUT) {
2917 if (out_jacks > 1 && val == PIN_HP)
2919 } else if (val & PIN_IN) {
2922 unsigned int vref_caps = get_vref_caps(codec, nid);
2923 val &= AC_PINCTL_VREFEN;
2924 idx += cvt_from_vref_idx(vref_caps, val);
2930 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2931 struct snd_ctl_elem_value *ucontrol)
2933 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2934 hda_nid_t nid = kcontrol->private_value;
2935 ucontrol->value.enumerated.item[0] =
2936 get_cur_hp_mic_jack_mode(codec, nid);
2940 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2941 struct snd_ctl_elem_value *ucontrol)
2943 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2944 hda_nid_t nid = kcontrol->private_value;
2945 int out_jacks = get_out_jack_num_items(codec, nid);
2946 int in_jacks = get_in_jack_num_items(codec, nid);
2947 unsigned int val, oldval, idx;
2949 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2950 idx = ucontrol->value.enumerated.item[0];
2954 if (idx < out_jacks) {
2956 val = idx ? PIN_HP : PIN_OUT;
2962 unsigned int vref_caps = get_vref_caps(codec, nid);
2963 val = snd_hda_codec_get_pin_target(codec, nid);
2964 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2965 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
2967 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
2969 snd_hda_set_pin_ctl_cache(codec, nid, val);
2970 call_hp_automute(codec, NULL);
2975 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2976 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2977 .info = hp_mic_jack_mode_info,
2978 .get = hp_mic_jack_mode_get,
2979 .put = hp_mic_jack_mode_put,
2982 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2984 struct hda_gen_spec *spec = codec->spec;
2985 struct snd_kcontrol_new *knew;
2987 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2988 &hp_mic_jack_mode_enum);
2991 knew->private_value = pin;
2992 spec->hp_mic_jack_modes = 1;
3000 /* add the powersave loopback-list entry */
3001 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
3003 struct hda_amp_list *list;
3005 list = snd_array_new(&spec->loopback_list);
3009 list->dir = HDA_INPUT;
3011 spec->loopback.amplist = spec->loopback_list.list;
3015 /* return true if either a volume or a mute amp is found for the given
3016 * aamix path; the amp has to be either in the mixer node or its direct leaf
3018 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3019 hda_nid_t pin, unsigned int *mix_val,
3020 unsigned int *mute_val)
3023 const hda_nid_t *list;
3026 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3030 *mix_val = *mute_val = 0;
3031 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3032 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3033 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3034 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3035 if (*mix_val && *mute_val)
3038 /* check leaf node */
3039 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3040 if (num_conns < idx)
3043 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3044 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
3045 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3046 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3047 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
3048 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3050 return *mix_val || *mute_val;
3053 /* create input playback/capture controls for the given pin */
3054 static int new_analog_input(struct hda_codec *codec, int input_idx,
3055 hda_nid_t pin, const char *ctlname, int ctlidx,
3058 struct hda_gen_spec *spec = codec->spec;
3059 struct nid_path *path;
3060 unsigned int mix_val, mute_val;
3063 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3066 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
3069 print_nid_path(codec, "loopback", path);
3070 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
3072 idx = path->idx[path->depth - 1];
3074 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
3077 path->ctls[NID_PATH_VOL_CTL] = mix_val;
3081 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
3084 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
3087 path->active = true;
3088 path->stream_enabled = true; /* no DAC/ADC involved */
3089 err = add_loopback_list(spec, mix_nid, idx);
3093 if (spec->mixer_nid != spec->mixer_merge_nid &&
3094 !spec->loopback_merge_path) {
3095 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3096 spec->mixer_merge_nid, 0);
3098 print_nid_path(codec, "loopback-merge", path);
3099 path->active = true;
3100 path->pin_fixed = true; /* static route */
3101 path->stream_enabled = true; /* no DAC/ADC involved */
3102 spec->loopback_merge_path =
3103 snd_hda_get_path_idx(codec, path);
3110 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3112 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3113 return (pincap & AC_PINCAP_IN) != 0;
3116 /* Parse the codec tree and retrieve ADCs */
3117 static int fill_adc_nids(struct hda_codec *codec)
3119 struct hda_gen_spec *spec = codec->spec;
3121 hda_nid_t *adc_nids = spec->adc_nids;
3122 int max_nums = ARRAY_SIZE(spec->adc_nids);
3125 for_each_hda_codec_node(nid, codec) {
3126 unsigned int caps = get_wcaps(codec, nid);
3127 int type = get_wcaps_type(caps);
3129 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3131 adc_nids[nums] = nid;
3132 if (++nums >= max_nums)
3135 spec->num_adc_nids = nums;
3137 /* copy the detected ADCs to all_adcs[] */
3138 spec->num_all_adcs = nums;
3139 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3144 /* filter out invalid adc_nids that don't give all active input pins;
3145 * if needed, check whether dynamic ADC-switching is available
3147 static int check_dyn_adc_switch(struct hda_codec *codec)
3149 struct hda_gen_spec *spec = codec->spec;
3150 struct hda_input_mux *imux = &spec->input_mux;
3151 unsigned int ok_bits;
3156 for (n = 0; n < spec->num_adc_nids; n++) {
3157 for (i = 0; i < imux->num_items; i++) {
3158 if (!spec->input_paths[i][n])
3161 if (i >= imux->num_items) {
3162 ok_bits |= (1 << n);
3168 /* check whether ADC-switch is possible */
3169 for (i = 0; i < imux->num_items; i++) {
3170 for (n = 0; n < spec->num_adc_nids; n++) {
3171 if (spec->input_paths[i][n]) {
3172 spec->dyn_adc_idx[i] = n;
3178 codec_dbg(codec, "enabling ADC switching\n");
3179 spec->dyn_adc_switch = 1;
3180 } else if (nums != spec->num_adc_nids) {
3181 /* shrink the invalid adcs and input paths */
3183 for (n = 0; n < spec->num_adc_nids; n++) {
3184 if (!(ok_bits & (1 << n)))
3187 spec->adc_nids[nums] = spec->adc_nids[n];
3188 for (i = 0; i < imux->num_items; i++) {
3189 invalidate_nid_path(codec,
3190 spec->input_paths[i][nums]);
3191 spec->input_paths[i][nums] =
3192 spec->input_paths[i][n];
3197 spec->num_adc_nids = nums;
3200 if (imux->num_items == 1 ||
3201 (imux->num_items == 2 && spec->hp_mic)) {
3202 codec_dbg(codec, "reducing to a single ADC\n");
3203 spec->num_adc_nids = 1; /* reduce to a single ADC */
3206 /* single index for individual volumes ctls */
3207 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3208 spec->num_adc_nids = 1;
3213 /* parse capture source paths from the given pin and create imux items */
3214 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3215 int cfg_idx, int num_adcs,
3216 const char *label, int anchor)
3218 struct hda_gen_spec *spec = codec->spec;
3219 struct hda_input_mux *imux = &spec->input_mux;
3220 int imux_idx = imux->num_items;
3221 bool imux_added = false;
3224 for (c = 0; c < num_adcs; c++) {
3225 struct nid_path *path;
3226 hda_nid_t adc = spec->adc_nids[c];
3228 if (!is_reachable_path(codec, pin, adc))
3230 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3233 print_nid_path(codec, "input", path);
3234 spec->input_paths[imux_idx][c] =
3235 snd_hda_get_path_idx(codec, path);
3238 if (spec->hp_mic_pin == pin)
3239 spec->hp_mic_mux_idx = imux->num_items;
3240 spec->imux_pins[imux->num_items] = pin;
3241 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3243 if (spec->dyn_adc_switch)
3244 spec->dyn_adc_idx[imux_idx] = c;
3252 * create playback/capture controls for input pins
3255 /* fill the label for each input at first */
3256 static int fill_input_pin_labels(struct hda_codec *codec)
3258 struct hda_gen_spec *spec = codec->spec;
3259 const struct auto_pin_cfg *cfg = &spec->autocfg;
3262 for (i = 0; i < cfg->num_inputs; i++) {
3263 hda_nid_t pin = cfg->inputs[i].pin;
3267 if (!is_input_pin(codec, pin))
3270 label = hda_get_autocfg_input_label(codec, cfg, i);
3272 for (j = i - 1; j >= 0; j--) {
3273 if (spec->input_labels[j] &&
3274 !strcmp(spec->input_labels[j], label)) {
3275 idx = spec->input_label_idxs[j] + 1;
3280 spec->input_labels[i] = label;
3281 spec->input_label_idxs[i] = idx;
3287 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3289 static int create_input_ctls(struct hda_codec *codec)
3291 struct hda_gen_spec *spec = codec->spec;
3292 const struct auto_pin_cfg *cfg = &spec->autocfg;
3293 hda_nid_t mixer = spec->mixer_nid;
3298 num_adcs = fill_adc_nids(codec);
3302 err = fill_input_pin_labels(codec);
3306 for (i = 0; i < cfg->num_inputs; i++) {
3309 pin = cfg->inputs[i].pin;
3310 if (!is_input_pin(codec, pin))
3314 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3315 val |= snd_hda_get_default_vref(codec, pin);
3316 if (pin != spec->hp_mic_pin &&
3317 !snd_hda_codec_get_pin_target(codec, pin))
3318 set_pin_target(codec, pin, val, false);
3321 if (is_reachable_path(codec, pin, mixer)) {
3322 err = new_analog_input(codec, i, pin,
3323 spec->input_labels[i],
3324 spec->input_label_idxs[i],
3331 err = parse_capture_source(codec, pin, i, num_adcs,
3332 spec->input_labels[i], -mixer);
3336 if (spec->add_jack_modes) {
3337 err = create_in_jack_mode(codec, pin);
3343 /* add stereo mix when explicitly enabled via hint */
3344 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3345 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3350 spec->suppress_auto_mic = 1;
3361 /* get the input path specified by the given adc and imux indices */
3362 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3364 struct hda_gen_spec *spec = codec->spec;
3365 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3369 if (spec->dyn_adc_switch)
3370 adc_idx = spec->dyn_adc_idx[imux_idx];
3371 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3375 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3378 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3381 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3382 struct snd_ctl_elem_info *uinfo)
3384 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3385 struct hda_gen_spec *spec = codec->spec;
3386 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3389 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3390 struct snd_ctl_elem_value *ucontrol)
3392 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3393 struct hda_gen_spec *spec = codec->spec;
3394 /* the ctls are created at once with multiple counts */
3395 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3397 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3401 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3402 struct snd_ctl_elem_value *ucontrol)
3404 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3405 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3406 return mux_select(codec, adc_idx,
3407 ucontrol->value.enumerated.item[0]);
3410 static const struct snd_kcontrol_new cap_src_temp = {
3411 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3412 .name = "Input Source",
3413 .info = mux_enum_info,
3414 .get = mux_enum_get,
3415 .put = mux_enum_put,
3419 * capture volume and capture switch ctls
3422 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3423 struct snd_ctl_elem_value *ucontrol);
3425 /* call the given amp update function for all amps in the imux list at once */
3426 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3427 struct snd_ctl_elem_value *ucontrol,
3428 put_call_t func, int type)
3430 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3431 struct hda_gen_spec *spec = codec->spec;
3432 const struct hda_input_mux *imux;
3433 struct nid_path *path;
3434 int i, adc_idx, err = 0;
3436 imux = &spec->input_mux;
3437 adc_idx = kcontrol->id.index;
3438 mutex_lock(&codec->control_mutex);
3439 for (i = 0; i < imux->num_items; i++) {
3440 path = get_input_path(codec, adc_idx, i);
3441 if (!path || !path->ctls[type])
3443 kcontrol->private_value = path->ctls[type];
3444 err = func(kcontrol, ucontrol);
3448 mutex_unlock(&codec->control_mutex);
3449 if (err >= 0 && spec->cap_sync_hook)
3450 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3454 /* capture volume ctl callbacks */
3455 #define cap_vol_info snd_hda_mixer_amp_volume_info
3456 #define cap_vol_get snd_hda_mixer_amp_volume_get
3457 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3459 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3460 struct snd_ctl_elem_value *ucontrol)
3462 return cap_put_caller(kcontrol, ucontrol,
3463 snd_hda_mixer_amp_volume_put,
3467 static const struct snd_kcontrol_new cap_vol_temp = {
3468 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3469 .name = "Capture Volume",
3470 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3471 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3472 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3473 .info = cap_vol_info,
3476 .tlv = { .c = cap_vol_tlv },
3479 /* capture switch ctl callbacks */
3480 #define cap_sw_info snd_ctl_boolean_stereo_info
3481 #define cap_sw_get snd_hda_mixer_amp_switch_get
3483 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3484 struct snd_ctl_elem_value *ucontrol)
3486 return cap_put_caller(kcontrol, ucontrol,
3487 snd_hda_mixer_amp_switch_put,
3491 static const struct snd_kcontrol_new cap_sw_temp = {
3492 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3493 .name = "Capture Switch",
3494 .info = cap_sw_info,
3499 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3504 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3505 for (depth = 0; depth < 3; depth++) {
3506 if (depth >= path->depth)
3508 i = path->depth - depth - 1;
3509 nid = path->path[i];
3510 if (!path->ctls[NID_PATH_VOL_CTL]) {
3511 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3512 path->ctls[NID_PATH_VOL_CTL] =
3513 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3514 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3515 int idx = path->idx[i];
3516 if (!depth && codec->single_adc_amp)
3518 path->ctls[NID_PATH_VOL_CTL] =
3519 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3522 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3523 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3524 path->ctls[NID_PATH_MUTE_CTL] =
3525 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3526 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3527 int idx = path->idx[i];
3528 if (!depth && codec->single_adc_amp)
3530 path->ctls[NID_PATH_MUTE_CTL] =
3531 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3538 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3540 struct hda_gen_spec *spec = codec->spec;
3541 struct auto_pin_cfg *cfg = &spec->autocfg;
3545 if (!spec->inv_dmic_split)
3547 for (i = 0; i < cfg->num_inputs; i++) {
3548 if (cfg->inputs[i].pin != nid)
3550 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3552 val = snd_hda_codec_get_pincfg(codec, nid);
3553 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3558 /* capture switch put callback for a single control with hook call */
3559 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3560 struct snd_ctl_elem_value *ucontrol)
3562 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3563 struct hda_gen_spec *spec = codec->spec;
3566 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3570 if (spec->cap_sync_hook)
3571 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3576 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3577 int idx, bool is_switch, unsigned int ctl,
3580 struct hda_gen_spec *spec = codec->spec;
3581 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3582 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3583 const char *sfx = is_switch ? "Switch" : "Volume";
3584 unsigned int chs = inv_dmic ? 1 : 3;
3585 struct snd_kcontrol_new *knew;
3591 snprintf(tmpname, sizeof(tmpname),
3592 "%s Capture %s", label, sfx);
3594 snprintf(tmpname, sizeof(tmpname),
3596 knew = add_control(spec, type, tmpname, idx,
3597 amp_val_replace_channels(ctl, chs));
3601 knew->put = cap_single_sw_put;
3605 /* Make independent right kcontrol */
3607 snprintf(tmpname, sizeof(tmpname),
3608 "Inverted %s Capture %s", label, sfx);
3610 snprintf(tmpname, sizeof(tmpname),
3611 "Inverted Capture %s", sfx);
3612 knew = add_control(spec, type, tmpname, idx,
3613 amp_val_replace_channels(ctl, 2));
3617 knew->put = cap_single_sw_put;
3621 /* create single (and simple) capture volume and switch controls */
3622 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3623 unsigned int vol_ctl, unsigned int sw_ctl,
3627 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3630 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3636 /* create bound capture volume and switch controls */
3637 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3638 unsigned int vol_ctl, unsigned int sw_ctl)
3640 struct hda_gen_spec *spec = codec->spec;
3641 struct snd_kcontrol_new *knew;
3644 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3648 knew->private_value = vol_ctl;
3649 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3652 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3656 knew->private_value = sw_ctl;
3657 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3662 /* return the vol ctl when used first in the imux list */
3663 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3665 struct nid_path *path;
3669 path = get_input_path(codec, 0, idx);
3672 ctl = path->ctls[type];
3675 for (i = 0; i < idx - 1; i++) {
3676 path = get_input_path(codec, 0, i);
3677 if (path && path->ctls[type] == ctl)
3683 /* create individual capture volume and switch controls per input */
3684 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3686 struct hda_gen_spec *spec = codec->spec;
3687 struct hda_input_mux *imux = &spec->input_mux;
3690 for (i = 0; i < imux->num_items; i++) {
3694 idx = imux->items[i].index;
3695 if (idx >= spec->autocfg.num_inputs)
3697 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3699 for (type = 0; type < 2; type++) {
3700 err = add_single_cap_ctl(codec,
3701 spec->input_labels[idx],
3702 spec->input_label_idxs[idx],
3704 get_first_cap_ctl(codec, i, type),
3713 static int create_capture_mixers(struct hda_codec *codec)
3715 struct hda_gen_spec *spec = codec->spec;
3716 struct hda_input_mux *imux = &spec->input_mux;
3717 int i, n, nums, err;
3719 if (spec->dyn_adc_switch)
3722 nums = spec->num_adc_nids;
3724 if (!spec->auto_mic && imux->num_items > 1) {
3725 struct snd_kcontrol_new *knew;
3727 name = nums > 1 ? "Input Source" : "Capture Source";
3728 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3734 for (n = 0; n < nums; n++) {
3736 bool multi_cap_vol = spec->multi_cap_vol;
3737 bool inv_dmic = false;
3741 for (i = 0; i < imux->num_items; i++) {
3742 struct nid_path *path;
3743 path = get_input_path(codec, n, i);
3746 parse_capvol_in_path(codec, path);
3748 vol = path->ctls[NID_PATH_VOL_CTL];
3749 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3751 if (!same_amp_caps(codec, vol,
3752 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3753 multi_cap_vol = true;
3756 sw = path->ctls[NID_PATH_MUTE_CTL];
3757 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3759 if (!same_amp_caps(codec, sw,
3760 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3761 multi_cap_vol = true;
3763 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3768 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3770 else if (!multi_cap_vol && !inv_dmic)
3771 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3773 err = create_multi_cap_vol_ctl(codec);
3782 * add mic boosts if needed
3785 /* check whether the given amp is feasible as a boost volume */
3786 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3791 if (!nid_has_volume(codec, nid, dir) ||
3792 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3793 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3796 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3797 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3803 /* look for a boost amp in a widget close to the pin */
3804 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3805 struct nid_path *path)
3807 unsigned int val = 0;
3811 for (depth = 0; depth < 3; depth++) {
3812 if (depth >= path->depth - 1)
3814 nid = path->path[depth];
3815 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3816 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3818 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3819 path->idx[depth])) {
3820 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3829 static int parse_mic_boost(struct hda_codec *codec)
3831 struct hda_gen_spec *spec = codec->spec;
3832 struct auto_pin_cfg *cfg = &spec->autocfg;
3833 struct hda_input_mux *imux = &spec->input_mux;
3836 if (!spec->num_adc_nids)
3839 for (i = 0; i < imux->num_items; i++) {
3840 struct nid_path *path;
3843 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3845 idx = imux->items[i].index;
3846 if (idx >= imux->num_items)
3849 /* check only line-in and mic pins */
3850 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3853 path = get_input_path(codec, 0, i);
3857 val = look_for_boost_amp(codec, path);
3861 /* create a boost control */
3862 snprintf(boost_label, sizeof(boost_label),
3863 "%s Boost Volume", spec->input_labels[idx]);
3864 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3865 spec->input_label_idxs[idx], val))
3868 path->ctls[NID_PATH_BOOST_CTL] = val;
3874 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3876 static void parse_digital(struct hda_codec *codec)
3878 struct hda_gen_spec *spec = codec->spec;
3879 struct nid_path *path;
3881 hda_nid_t dig_nid, pin;
3883 /* support multiple SPDIFs; the secondary is set up as a slave */
3885 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3886 pin = spec->autocfg.dig_out_pins[i];
3887 dig_nid = look_for_dac(codec, pin, true);
3890 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3893 print_nid_path(codec, "digout", path);
3894 path->active = true;
3895 path->pin_fixed = true; /* no jack detection */
3896 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3897 set_pin_target(codec, pin, PIN_OUT, false);
3899 spec->multiout.dig_out_nid = dig_nid;
3900 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3902 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3903 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3905 spec->slave_dig_outs[nums - 1] = dig_nid;
3910 if (spec->autocfg.dig_in_pin) {
3911 pin = spec->autocfg.dig_in_pin;
3912 for_each_hda_codec_node(dig_nid, codec) {
3913 unsigned int wcaps = get_wcaps(codec, dig_nid);
3914 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3916 if (!(wcaps & AC_WCAP_DIGITAL))
3918 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3920 print_nid_path(codec, "digin", path);
3921 path->active = true;
3922 path->pin_fixed = true; /* no jack */
3923 spec->dig_in_nid = dig_nid;
3924 spec->digin_path = snd_hda_get_path_idx(codec, path);
3925 set_pin_target(codec, pin, PIN_IN, false);
3934 * input MUX handling
3937 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3939 /* select the given imux item; either unmute exclusively or select the route */
3940 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3943 struct hda_gen_spec *spec = codec->spec;
3944 const struct hda_input_mux *imux;
3945 struct nid_path *old_path, *path;
3947 imux = &spec->input_mux;
3948 if (!imux->num_items)
3951 if (idx >= imux->num_items)
3952 idx = imux->num_items - 1;
3953 if (spec->cur_mux[adc_idx] == idx)
3956 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3959 if (old_path->active)
3960 snd_hda_activate_path(codec, old_path, false, false);
3962 spec->cur_mux[adc_idx] = idx;
3965 update_hp_mic(codec, adc_idx, false);
3967 if (spec->dyn_adc_switch)
3968 dyn_adc_pcm_resetup(codec, idx);
3970 path = get_input_path(codec, adc_idx, idx);
3975 snd_hda_activate_path(codec, path, true, false);
3976 if (spec->cap_sync_hook)
3977 spec->cap_sync_hook(codec, NULL, NULL);
3978 path_power_down_sync(codec, old_path);
3982 /* power up/down widgets in the all paths that match with the given NID
3983 * as terminals (either start- or endpoint)
3985 * returns the last changed NID, or zero if unchanged.
3987 static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
3988 int pin_state, int stream_state)
3990 struct hda_gen_spec *spec = codec->spec;
3991 hda_nid_t last, changed = 0;
3992 struct nid_path *path;
3995 for (n = 0; n < spec->paths.used; n++) {
3996 path = snd_array_elem(&spec->paths, n);
3999 if (path->path[0] == nid ||
4000 path->path[path->depth - 1] == nid) {
4001 bool pin_old = path->pin_enabled;
4002 bool stream_old = path->stream_enabled;
4005 path->pin_enabled = pin_state;
4006 if (stream_state >= 0)
4007 path->stream_enabled = stream_state;
4008 if ((!path->pin_fixed && path->pin_enabled != pin_old)
4009 || path->stream_enabled != stream_old) {
4010 last = path_power_update(codec, path, true);
4019 /* check the jack status for power control */
4020 static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4022 if (!is_jack_detectable(codec, pin))
4024 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4027 /* power up/down the paths of the given pin according to the jack state;
4028 * power = 0/1 : only power up/down if it matches with the jack state,
4029 * < 0 : force power up/down to follow the jack sate
4031 * returns the last changed NID, or zero if unchanged.
4033 static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4038 if (!codec->power_save_node)
4041 on = detect_pin_state(codec, pin);
4043 if (power >= 0 && on != power)
4045 return set_path_power(codec, pin, on, -1);
4048 static void pin_power_callback(struct hda_codec *codec,
4049 struct hda_jack_callback *jack,
4052 if (jack && jack->nid)
4053 sync_power_state_change(codec,
4054 set_pin_power_jack(codec, jack->nid, on));
4057 /* callback only doing power up -- called at first */
4058 static void pin_power_up_callback(struct hda_codec *codec,
4059 struct hda_jack_callback *jack)
4061 pin_power_callback(codec, jack, true);
4064 /* callback only doing power down -- called at last */
4065 static void pin_power_down_callback(struct hda_codec *codec,
4066 struct hda_jack_callback *jack)
4068 pin_power_callback(codec, jack, false);
4071 /* set up the power up/down callbacks */
4072 static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4073 const hda_nid_t *pins, bool on)
4076 hda_jack_callback_fn cb =
4077 on ? pin_power_up_callback : pin_power_down_callback;
4079 for (i = 0; i < num_pins && pins[i]; i++) {
4080 if (is_jack_detectable(codec, pins[i]))
4081 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4083 set_path_power(codec, pins[i], true, -1);
4087 /* enabled power callback to each available I/O pin with jack detections;
4088 * the digital I/O pins are excluded because of the unreliable detectsion
4090 static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4092 struct hda_gen_spec *spec = codec->spec;
4093 struct auto_pin_cfg *cfg = &spec->autocfg;
4096 if (!codec->power_save_node)
4098 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4099 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4100 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4101 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4102 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4103 for (i = 0; i < cfg->num_inputs; i++)
4104 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4107 /* sync path power up/down with the jack states of given pins */
4108 static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4109 const hda_nid_t *pins)
4113 for (i = 0; i < num_pins && pins[i]; i++)
4114 if (is_jack_detectable(codec, pins[i]))
4115 set_pin_power_jack(codec, pins[i], -1);
4118 /* sync path power up/down with pins; called at init and resume */
4119 static void sync_all_pin_power_ctls(struct hda_codec *codec)
4121 struct hda_gen_spec *spec = codec->spec;
4122 struct auto_pin_cfg *cfg = &spec->autocfg;
4125 if (!codec->power_save_node)
4127 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4128 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4129 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4130 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4131 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4132 for (i = 0; i < cfg->num_inputs; i++)
4133 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4136 /* add fake paths if not present yet */
4137 static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4138 int num_pins, const hda_nid_t *pins)
4140 struct hda_gen_spec *spec = codec->spec;
4141 struct nid_path *path;
4144 for (i = 0; i < num_pins; i++) {
4147 if (get_nid_path(codec, nid, pins[i], 0))
4149 path = snd_array_new(&spec->paths);
4152 memset(path, 0, sizeof(*path));
4154 path->path[0] = nid;
4155 path->path[1] = pins[i];
4156 path->active = true;
4161 /* create fake paths to all outputs from beep */
4162 static int add_fake_beep_paths(struct hda_codec *codec)
4164 struct hda_gen_spec *spec = codec->spec;
4165 struct auto_pin_cfg *cfg = &spec->autocfg;
4166 hda_nid_t nid = spec->beep_nid;
4169 if (!codec->power_save_node || !nid)
4171 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4174 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4175 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4179 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4180 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4188 /* power up/down beep widget and its output paths */
4189 static void beep_power_hook(struct hda_beep *beep, bool on)
4191 set_path_power(beep->codec, beep->nid, -1, on);
4195 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4196 * @codec: the HDA codec
4197 * @pin: NID of pin to fix
4199 int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4201 struct hda_gen_spec *spec = codec->spec;
4202 struct nid_path *path;
4204 path = snd_array_new(&spec->paths);
4207 memset(path, 0, sizeof(*path));
4209 path->path[0] = pin;
4210 path->active = true;
4211 path->pin_fixed = true;
4212 path->stream_enabled = true;
4215 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4218 * Jack detections for HP auto-mute and mic-switch
4221 /* check each pin in the given array; returns true if any of them is plugged */
4222 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4225 bool present = false;
4227 for (i = 0; i < num_pins; i++) {
4228 hda_nid_t nid = pins[i];
4231 /* don't detect pins retasked as inputs */
4232 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4234 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4240 /* standard HP/line-out auto-mute helper */
4241 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
4242 int *paths, bool mute)
4244 struct hda_gen_spec *spec = codec->spec;
4247 for (i = 0; i < num_pins; i++) {
4248 hda_nid_t nid = pins[i];
4249 unsigned int val, oldval;
4253 oldval = snd_hda_codec_get_pin_target(codec, nid);
4254 if (oldval & PIN_IN)
4255 continue; /* no mute for inputs */
4257 if (spec->auto_mute_via_amp) {
4258 struct nid_path *path;
4261 path = snd_hda_get_path_from_idx(codec, paths[i]);
4264 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4268 spec->mute_bits |= (1ULL << mute_nid);
4270 spec->mute_bits &= ~(1ULL << mute_nid);
4273 /* don't reset VREF value in case it's controlling
4274 * the amp (see alc861_fixup_asus_amp_vref_0f())
4276 if (spec->keep_vref_in_automute)
4277 val = oldval & ~PIN_HP;
4282 /* here we call update_pin_ctl() so that the pinctl is
4283 * changed without changing the pinctl target value;
4284 * the original target value will be still referred at
4285 * the init / resume again
4287 update_pin_ctl(codec, nid, val);
4290 set_pin_eapd(codec, nid, !mute);
4291 if (codec->power_save_node) {
4294 on = detect_pin_state(codec, nid);
4295 set_path_power(codec, nid, on, -1);
4301 * snd_hda_gen_update_outputs - Toggle outputs muting
4302 * @codec: the HDA codec
4304 * Update the mute status of all outputs based on the current jack states.
4306 void snd_hda_gen_update_outputs(struct hda_codec *codec)
4308 struct hda_gen_spec *spec = codec->spec;
4312 /* Control HP pins/amps depending on master_mute state;
4313 * in general, HP pins/amps control should be enabled in all cases,
4314 * but currently set only for master_mute, just to be safe
4316 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4317 paths = spec->out_paths;
4319 paths = spec->hp_paths;
4320 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
4321 spec->autocfg.hp_pins, paths, spec->master_mute);
4323 if (!spec->automute_speaker)
4326 on = spec->hp_jack_present | spec->line_jack_present;
4327 on |= spec->master_mute;
4328 spec->speaker_muted = on;
4329 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4330 paths = spec->out_paths;
4332 paths = spec->speaker_paths;
4333 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
4334 spec->autocfg.speaker_pins, paths, on);
4336 /* toggle line-out mutes if needed, too */
4337 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4338 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4339 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4341 if (!spec->automute_lo)
4344 on = spec->hp_jack_present;
4345 on |= spec->master_mute;
4346 spec->line_out_muted = on;
4347 paths = spec->out_paths;
4348 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4349 spec->autocfg.line_out_pins, paths, on);
4351 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4353 static void call_update_outputs(struct hda_codec *codec)
4355 struct hda_gen_spec *spec = codec->spec;
4356 if (spec->automute_hook)
4357 spec->automute_hook(codec);
4359 snd_hda_gen_update_outputs(codec);
4361 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4362 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4363 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4367 * snd_hda_gen_hp_automute - standard HP-automute helper
4368 * @codec: the HDA codec
4369 * @jack: jack object, NULL for the whole
4371 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4372 struct hda_jack_callback *jack)
4374 struct hda_gen_spec *spec = codec->spec;
4375 hda_nid_t *pins = spec->autocfg.hp_pins;
4376 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4378 /* No detection for the first HP jack during indep-HP mode */
4379 if (spec->indep_hp_enabled) {
4384 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4385 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4387 call_update_outputs(codec);
4389 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4392 * snd_hda_gen_line_automute - standard line-out-automute helper
4393 * @codec: the HDA codec
4394 * @jack: jack object, NULL for the whole
4396 void snd_hda_gen_line_automute(struct hda_codec *codec,
4397 struct hda_jack_callback *jack)
4399 struct hda_gen_spec *spec = codec->spec;
4401 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4403 /* check LO jack only when it's different from HP */
4404 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4407 spec->line_jack_present =
4408 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4409 spec->autocfg.line_out_pins);
4410 if (!spec->automute_speaker || !spec->detect_lo)
4412 call_update_outputs(codec);
4414 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4417 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4418 * @codec: the HDA codec
4419 * @jack: jack object, NULL for the whole
4421 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4422 struct hda_jack_callback *jack)
4424 struct hda_gen_spec *spec = codec->spec;
4427 if (!spec->auto_mic)
4430 for (i = spec->am_num_entries - 1; i > 0; i--) {
4431 hda_nid_t pin = spec->am_entry[i].pin;
4432 /* don't detect pins retasked as outputs */
4433 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4435 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4436 mux_select(codec, 0, spec->am_entry[i].idx);
4440 mux_select(codec, 0, spec->am_entry[0].idx);
4442 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4444 /* call appropriate hooks */
4445 static void call_hp_automute(struct hda_codec *codec,
4446 struct hda_jack_callback *jack)
4448 struct hda_gen_spec *spec = codec->spec;
4449 if (spec->hp_automute_hook)
4450 spec->hp_automute_hook(codec, jack);
4452 snd_hda_gen_hp_automute(codec, jack);
4455 static void call_line_automute(struct hda_codec *codec,
4456 struct hda_jack_callback *jack)
4458 struct hda_gen_spec *spec = codec->spec;
4459 if (spec->line_automute_hook)
4460 spec->line_automute_hook(codec, jack);
4462 snd_hda_gen_line_automute(codec, jack);
4465 static void call_mic_autoswitch(struct hda_codec *codec,
4466 struct hda_jack_callback *jack)
4468 struct hda_gen_spec *spec = codec->spec;
4469 if (spec->mic_autoswitch_hook)
4470 spec->mic_autoswitch_hook(codec, jack);
4472 snd_hda_gen_mic_autoswitch(codec, jack);
4475 /* update jack retasking */
4476 static void update_automute_all(struct hda_codec *codec)
4478 call_hp_automute(codec, NULL);
4479 call_line_automute(codec, NULL);
4480 call_mic_autoswitch(codec, NULL);
4484 * Auto-Mute mode mixer enum support
4486 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4487 struct snd_ctl_elem_info *uinfo)
4489 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4490 struct hda_gen_spec *spec = codec->spec;
4491 static const char * const texts3[] = {
4492 "Disabled", "Speaker Only", "Line Out+Speaker"
4495 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4496 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4497 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4500 static int automute_mode_get(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;
4505 unsigned int val = 0;
4506 if (spec->automute_speaker)
4508 if (spec->automute_lo)
4511 ucontrol->value.enumerated.item[0] = val;
4515 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4516 struct snd_ctl_elem_value *ucontrol)
4518 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4519 struct hda_gen_spec *spec = codec->spec;
4521 switch (ucontrol->value.enumerated.item[0]) {
4523 if (!spec->automute_speaker && !spec->automute_lo)
4525 spec->automute_speaker = 0;
4526 spec->automute_lo = 0;
4529 if (spec->automute_speaker_possible) {
4530 if (!spec->automute_lo && spec->automute_speaker)
4532 spec->automute_speaker = 1;
4533 spec->automute_lo = 0;
4534 } else if (spec->automute_lo_possible) {
4535 if (spec->automute_lo)
4537 spec->automute_lo = 1;
4542 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4544 if (spec->automute_speaker && spec->automute_lo)
4546 spec->automute_speaker = 1;
4547 spec->automute_lo = 1;
4552 call_update_outputs(codec);
4556 static const struct snd_kcontrol_new automute_mode_enum = {
4557 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4558 .name = "Auto-Mute Mode",
4559 .info = automute_mode_info,
4560 .get = automute_mode_get,
4561 .put = automute_mode_put,
4564 static int add_automute_mode_enum(struct hda_codec *codec)
4566 struct hda_gen_spec *spec = codec->spec;
4568 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4574 * Check the availability of HP/line-out auto-mute;
4575 * Set up appropriately if really supported
4577 static int check_auto_mute_availability(struct hda_codec *codec)
4579 struct hda_gen_spec *spec = codec->spec;
4580 struct auto_pin_cfg *cfg = &spec->autocfg;
4584 if (spec->suppress_auto_mute)
4587 if (cfg->hp_pins[0])
4589 if (cfg->line_out_pins[0])
4591 if (cfg->speaker_pins[0])
4593 if (present < 2) /* need two different output types */
4596 if (!cfg->speaker_pins[0] &&
4597 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4598 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4599 sizeof(cfg->speaker_pins));
4600 cfg->speaker_outs = cfg->line_outs;
4603 if (!cfg->hp_pins[0] &&
4604 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4605 memcpy(cfg->hp_pins, cfg->line_out_pins,
4606 sizeof(cfg->hp_pins));
4607 cfg->hp_outs = cfg->line_outs;
4610 for (i = 0; i < cfg->hp_outs; i++) {
4611 hda_nid_t nid = cfg->hp_pins[i];
4612 if (!is_jack_detectable(codec, nid))
4614 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4615 snd_hda_jack_detect_enable_callback(codec, nid,
4617 spec->detect_hp = 1;
4620 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4621 if (cfg->speaker_outs)
4622 for (i = 0; i < cfg->line_outs; i++) {
4623 hda_nid_t nid = cfg->line_out_pins[i];
4624 if (!is_jack_detectable(codec, nid))
4626 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4627 snd_hda_jack_detect_enable_callback(codec, nid,
4628 call_line_automute);
4629 spec->detect_lo = 1;
4631 spec->automute_lo_possible = spec->detect_hp;
4634 spec->automute_speaker_possible = cfg->speaker_outs &&
4635 (spec->detect_hp || spec->detect_lo);
4637 spec->automute_lo = spec->automute_lo_possible;
4638 spec->automute_speaker = spec->automute_speaker_possible;
4640 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4641 /* create a control for automute mode */
4642 err = add_automute_mode_enum(codec);
4649 /* check whether all auto-mic pins are valid; setup indices if OK */
4650 static bool auto_mic_check_imux(struct hda_codec *codec)
4652 struct hda_gen_spec *spec = codec->spec;
4653 const struct hda_input_mux *imux;
4656 imux = &spec->input_mux;
4657 for (i = 0; i < spec->am_num_entries; i++) {
4658 spec->am_entry[i].idx =
4659 find_idx_in_nid_list(spec->am_entry[i].pin,
4660 spec->imux_pins, imux->num_items);
4661 if (spec->am_entry[i].idx < 0)
4662 return false; /* no corresponding imux */
4665 /* we don't need the jack detection for the first pin */
4666 for (i = 1; i < spec->am_num_entries; i++)
4667 snd_hda_jack_detect_enable_callback(codec,
4668 spec->am_entry[i].pin,
4669 call_mic_autoswitch);
4673 static int compare_attr(const void *ap, const void *bp)
4675 const struct automic_entry *a = ap;
4676 const struct automic_entry *b = bp;
4677 return (int)(a->attr - b->attr);
4681 * Check the availability of auto-mic switch;
4682 * Set up if really supported
4684 static int check_auto_mic_availability(struct hda_codec *codec)
4686 struct hda_gen_spec *spec = codec->spec;
4687 struct auto_pin_cfg *cfg = &spec->autocfg;
4691 if (spec->suppress_auto_mic)
4696 for (i = 0; i < cfg->num_inputs; i++) {
4697 hda_nid_t nid = cfg->inputs[i].pin;
4699 attr = snd_hda_codec_get_pincfg(codec, nid);
4700 attr = snd_hda_get_input_pin_attr(attr);
4701 if (types & (1 << attr))
4702 return 0; /* already occupied */
4704 case INPUT_PIN_ATTR_INT:
4705 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4706 return 0; /* invalid type */
4708 case INPUT_PIN_ATTR_UNUSED:
4709 return 0; /* invalid entry */
4711 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4712 return 0; /* invalid type */
4713 if (!spec->line_in_auto_switch &&
4714 cfg->inputs[i].type != AUTO_PIN_MIC)
4715 return 0; /* only mic is allowed */
4716 if (!is_jack_detectable(codec, nid))
4717 return 0; /* no unsol support */
4720 if (num_pins >= MAX_AUTO_MIC_PINS)
4722 types |= (1 << attr);
4723 spec->am_entry[num_pins].pin = nid;
4724 spec->am_entry[num_pins].attr = attr;
4731 spec->am_num_entries = num_pins;
4732 /* sort the am_entry in the order of attr so that the pin with a
4733 * higher attr will be selected when the jack is plugged.
4735 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4736 compare_attr, NULL);
4738 if (!auto_mic_check_imux(codec))
4742 spec->num_adc_nids = 1;
4743 spec->cur_mux[0] = spec->am_entry[0].idx;
4744 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4745 spec->am_entry[0].pin,
4746 spec->am_entry[1].pin,
4747 spec->am_entry[2].pin);
4753 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4755 * @codec: the HDA codec
4756 * @nid: NID to evalute
4757 * @power_state: target power state
4759 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4761 unsigned int power_state)
4763 struct hda_gen_spec *spec = codec->spec;
4765 if (!spec->power_down_unused && !codec->power_save_node)
4767 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
4769 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4771 if (is_active_nid_for_any(codec, nid))
4775 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4777 /* mute all aamix inputs initially; parse up to the first leaves */
4778 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4781 const hda_nid_t *conn;
4784 nums = snd_hda_get_conn_list(codec, mix, &conn);
4785 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4786 for (i = 0; i < nums; i++) {
4788 update_amp(codec, mix, HDA_INPUT, i,
4789 0xff, HDA_AMP_MUTE);
4790 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4791 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4792 0xff, HDA_AMP_MUTE);
4797 * snd_hda_gen_stream_pm - Stream power management callback
4798 * @codec: the HDA codec
4799 * @nid: audio widget
4800 * @on: power on/off flag
4802 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
4804 void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4806 if (codec->power_save_node)
4807 set_path_power(codec, nid, -1, on);
4809 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4812 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4813 * set up the hda_gen_spec
4814 * @codec: the HDA codec
4815 * @cfg: Parsed pin configuration
4817 * return 1 if successful, 0 if the proper config is not found,
4818 * or a negative error code
4820 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4821 struct auto_pin_cfg *cfg)
4823 struct hda_gen_spec *spec = codec->spec;
4826 parse_user_hints(codec);
4828 if (spec->mixer_nid && !spec->mixer_merge_nid)
4829 spec->mixer_merge_nid = spec->mixer_nid;
4831 if (cfg != &spec->autocfg) {
4832 spec->autocfg = *cfg;
4833 cfg = &spec->autocfg;
4836 if (!spec->main_out_badness)
4837 spec->main_out_badness = &hda_main_out_badness;
4838 if (!spec->extra_out_badness)
4839 spec->extra_out_badness = &hda_extra_out_badness;
4841 fill_all_dac_nids(codec);
4843 if (!cfg->line_outs) {
4844 if (cfg->dig_outs || cfg->dig_in_pin) {
4845 spec->multiout.max_channels = 2;
4846 spec->no_analog = 1;
4849 if (!cfg->num_inputs && !cfg->dig_in_pin)
4850 return 0; /* can't find valid BIOS pin config */
4853 if (!spec->no_primary_hp &&
4854 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4855 cfg->line_outs <= cfg->hp_outs) {
4856 /* use HP as primary out */
4857 cfg->speaker_outs = cfg->line_outs;
4858 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4859 sizeof(cfg->speaker_pins));
4860 cfg->line_outs = cfg->hp_outs;
4861 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4863 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4864 cfg->line_out_type = AUTO_PIN_HP_OUT;
4867 err = parse_output_paths(codec);
4870 err = create_multi_channel_mode(codec);
4873 err = create_multi_out_ctls(codec, cfg);
4876 err = create_hp_out_ctls(codec);
4879 err = create_speaker_out_ctls(codec);
4882 err = create_indep_hp_ctls(codec);
4885 err = create_loopback_mixing_ctl(codec);
4888 err = create_hp_mic(codec);
4891 err = create_input_ctls(codec);
4895 /* add power-down pin callbacks at first */
4896 add_all_pin_power_ctls(codec, false);
4898 spec->const_channel_count = spec->ext_channel_count;
4899 /* check the multiple speaker and headphone pins */
4900 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4901 spec->const_channel_count = max(spec->const_channel_count,
4902 cfg->speaker_outs * 2);
4903 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4904 spec->const_channel_count = max(spec->const_channel_count,
4906 spec->multiout.max_channels = max(spec->ext_channel_count,
4907 spec->const_channel_count);
4909 err = check_auto_mute_availability(codec);
4913 err = check_dyn_adc_switch(codec);
4917 err = check_auto_mic_availability(codec);
4921 /* add stereo mix if available and not enabled yet */
4922 if (!spec->auto_mic && spec->mixer_nid &&
4923 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4924 spec->input_mux.num_items > 1) {
4925 err = parse_capture_source(codec, spec->mixer_nid,
4926 CFG_IDX_MIX, spec->num_all_adcs,
4933 err = create_capture_mixers(codec);
4937 err = parse_mic_boost(codec);
4941 /* create "Headphone Mic Jack Mode" if no input selection is
4942 * available (or user specifies add_jack_modes hint)
4944 if (spec->hp_mic_pin &&
4945 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4946 spec->add_jack_modes)) {
4947 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4952 if (spec->add_jack_modes) {
4953 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4954 err = create_out_jack_modes(codec, cfg->line_outs,
4955 cfg->line_out_pins);
4959 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4960 err = create_out_jack_modes(codec, cfg->hp_outs,
4967 /* add power-up pin callbacks at last */
4968 add_all_pin_power_ctls(codec, true);
4970 /* mute all aamix input initially */
4971 if (spec->mixer_nid)
4972 mute_all_mixer_nid(codec, spec->mixer_nid);
4975 parse_digital(codec);
4977 if (spec->power_down_unused || codec->power_save_node) {
4978 if (!codec->power_filter)
4979 codec->power_filter = snd_hda_gen_path_power_filter;
4980 if (!codec->patch_ops.stream_pm)
4981 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
4984 if (!spec->no_analog && spec->beep_nid) {
4985 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4988 if (codec->beep && codec->power_save_node) {
4989 err = add_fake_beep_paths(codec);
4992 codec->beep->power_hook = beep_power_hook;
4998 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
5002 * Build control elements
5005 /* slave controls for virtual master */
5006 static const char * const slave_pfxs[] = {
5007 "Front", "Surround", "Center", "LFE", "Side",
5008 "Headphone", "Speaker", "Mono", "Line Out",
5009 "CLFE", "Bass Speaker", "PCM",
5010 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5011 "Headphone Front", "Headphone Surround", "Headphone CLFE",
5012 "Headphone Side", "Headphone+LO", "Speaker+LO",
5017 * snd_hda_gen_build_controls - Build controls from the parsed results
5018 * @codec: the HDA codec
5020 * Pass this to build_controls patch_ops.
5022 int snd_hda_gen_build_controls(struct hda_codec *codec)
5024 struct hda_gen_spec *spec = codec->spec;
5027 if (spec->kctls.used) {
5028 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5033 if (spec->multiout.dig_out_nid) {
5034 err = snd_hda_create_dig_out_ctls(codec,
5035 spec->multiout.dig_out_nid,
5036 spec->multiout.dig_out_nid,
5037 spec->pcm_rec[1]->pcm_type);
5040 if (!spec->no_analog) {
5041 err = snd_hda_create_spdif_share_sw(codec,
5045 spec->multiout.share_spdif = 1;
5048 if (spec->dig_in_nid) {
5049 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5054 /* if we have no master control, let's create it */
5055 if (!spec->no_analog &&
5056 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
5057 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
5058 spec->vmaster_tlv, slave_pfxs,
5063 if (!spec->no_analog &&
5064 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5065 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5068 true, &spec->vmaster_mute.sw_kctl);
5071 if (spec->vmaster_mute.hook) {
5072 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5073 spec->vmaster_mute_enum);
5074 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5078 free_kctls(spec); /* no longer needed */
5080 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5086 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
5093 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5094 struct hda_codec *codec,
5095 struct snd_pcm_substream *substream,
5098 struct hda_gen_spec *spec = codec->spec;
5099 if (spec->pcm_playback_hook)
5100 spec->pcm_playback_hook(hinfo, codec, substream, action);
5103 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5104 struct hda_codec *codec,
5105 struct snd_pcm_substream *substream,
5108 struct hda_gen_spec *spec = codec->spec;
5109 if (spec->pcm_capture_hook)
5110 spec->pcm_capture_hook(hinfo, codec, substream, action);
5114 * Analog playback callbacks
5116 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5117 struct hda_codec *codec,
5118 struct snd_pcm_substream *substream)
5120 struct hda_gen_spec *spec = codec->spec;
5123 mutex_lock(&spec->pcm_mutex);
5124 err = snd_hda_multi_out_analog_open(codec,
5125 &spec->multiout, substream,
5128 spec->active_streams |= 1 << STREAM_MULTI_OUT;
5129 call_pcm_playback_hook(hinfo, codec, substream,
5130 HDA_GEN_PCM_ACT_OPEN);
5132 mutex_unlock(&spec->pcm_mutex);
5136 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5137 struct hda_codec *codec,
5138 unsigned int stream_tag,
5139 unsigned int format,
5140 struct snd_pcm_substream *substream)
5142 struct hda_gen_spec *spec = codec->spec;
5145 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5146 stream_tag, format, substream);
5148 call_pcm_playback_hook(hinfo, codec, substream,
5149 HDA_GEN_PCM_ACT_PREPARE);
5153 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5154 struct hda_codec *codec,
5155 struct snd_pcm_substream *substream)
5157 struct hda_gen_spec *spec = codec->spec;
5160 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5162 call_pcm_playback_hook(hinfo, codec, substream,
5163 HDA_GEN_PCM_ACT_CLEANUP);
5167 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5168 struct hda_codec *codec,
5169 struct snd_pcm_substream *substream)
5171 struct hda_gen_spec *spec = codec->spec;
5172 mutex_lock(&spec->pcm_mutex);
5173 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
5174 call_pcm_playback_hook(hinfo, codec, substream,
5175 HDA_GEN_PCM_ACT_CLOSE);
5176 mutex_unlock(&spec->pcm_mutex);
5180 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5181 struct hda_codec *codec,
5182 struct snd_pcm_substream *substream)
5184 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5188 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5189 struct hda_codec *codec,
5190 unsigned int stream_tag,
5191 unsigned int format,
5192 struct snd_pcm_substream *substream)
5194 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5195 call_pcm_capture_hook(hinfo, codec, substream,
5196 HDA_GEN_PCM_ACT_PREPARE);
5200 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5201 struct hda_codec *codec,
5202 struct snd_pcm_substream *substream)
5204 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5205 call_pcm_capture_hook(hinfo, codec, substream,
5206 HDA_GEN_PCM_ACT_CLEANUP);
5210 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5211 struct hda_codec *codec,
5212 struct snd_pcm_substream *substream)
5214 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5218 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5219 struct hda_codec *codec,
5220 struct snd_pcm_substream *substream)
5222 struct hda_gen_spec *spec = codec->spec;
5225 mutex_lock(&spec->pcm_mutex);
5226 if (spec->indep_hp && !spec->indep_hp_enabled)
5229 spec->active_streams |= 1 << STREAM_INDEP_HP;
5230 call_pcm_playback_hook(hinfo, codec, substream,
5231 HDA_GEN_PCM_ACT_OPEN);
5232 mutex_unlock(&spec->pcm_mutex);
5236 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5237 struct hda_codec *codec,
5238 struct snd_pcm_substream *substream)
5240 struct hda_gen_spec *spec = codec->spec;
5241 mutex_lock(&spec->pcm_mutex);
5242 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
5243 call_pcm_playback_hook(hinfo, codec, substream,
5244 HDA_GEN_PCM_ACT_CLOSE);
5245 mutex_unlock(&spec->pcm_mutex);
5249 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5250 struct hda_codec *codec,
5251 unsigned int stream_tag,
5252 unsigned int format,
5253 struct snd_pcm_substream *substream)
5255 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5256 call_pcm_playback_hook(hinfo, codec, substream,
5257 HDA_GEN_PCM_ACT_PREPARE);
5261 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5262 struct hda_codec *codec,
5263 struct snd_pcm_substream *substream)
5265 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5266 call_pcm_playback_hook(hinfo, codec, substream,
5267 HDA_GEN_PCM_ACT_CLEANUP);
5274 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5275 struct hda_codec *codec,
5276 struct snd_pcm_substream *substream)
5278 struct hda_gen_spec *spec = codec->spec;
5279 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5282 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5283 struct hda_codec *codec,
5284 unsigned int stream_tag,
5285 unsigned int format,
5286 struct snd_pcm_substream *substream)
5288 struct hda_gen_spec *spec = codec->spec;
5289 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5290 stream_tag, format, substream);
5293 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5294 struct hda_codec *codec,
5295 struct snd_pcm_substream *substream)
5297 struct hda_gen_spec *spec = codec->spec;
5298 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5301 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5302 struct hda_codec *codec,
5303 struct snd_pcm_substream *substream)
5305 struct hda_gen_spec *spec = codec->spec;
5306 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5312 #define alt_capture_pcm_open capture_pcm_open
5313 #define alt_capture_pcm_close capture_pcm_close
5315 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5316 struct hda_codec *codec,
5317 unsigned int stream_tag,
5318 unsigned int format,
5319 struct snd_pcm_substream *substream)
5321 struct hda_gen_spec *spec = codec->spec;
5323 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5324 stream_tag, 0, format);
5325 call_pcm_capture_hook(hinfo, codec, substream,
5326 HDA_GEN_PCM_ACT_PREPARE);
5330 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5331 struct hda_codec *codec,
5332 struct snd_pcm_substream *substream)
5334 struct hda_gen_spec *spec = codec->spec;
5336 snd_hda_codec_cleanup_stream(codec,
5337 spec->adc_nids[substream->number + 1]);
5338 call_pcm_capture_hook(hinfo, codec, substream,
5339 HDA_GEN_PCM_ACT_CLEANUP);
5345 static const struct hda_pcm_stream pcm_analog_playback = {
5349 /* NID is set in build_pcms */
5351 .open = playback_pcm_open,
5352 .close = playback_pcm_close,
5353 .prepare = playback_pcm_prepare,
5354 .cleanup = playback_pcm_cleanup
5358 static const struct hda_pcm_stream pcm_analog_capture = {
5362 /* NID is set in build_pcms */
5364 .open = capture_pcm_open,
5365 .close = capture_pcm_close,
5366 .prepare = capture_pcm_prepare,
5367 .cleanup = capture_pcm_cleanup
5371 static const struct hda_pcm_stream pcm_analog_alt_playback = {
5375 /* NID is set in build_pcms */
5377 .open = alt_playback_pcm_open,
5378 .close = alt_playback_pcm_close,
5379 .prepare = alt_playback_pcm_prepare,
5380 .cleanup = alt_playback_pcm_cleanup
5384 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5385 .substreams = 2, /* can be overridden */
5388 /* NID is set in build_pcms */
5390 .open = alt_capture_pcm_open,
5391 .close = alt_capture_pcm_close,
5392 .prepare = alt_capture_pcm_prepare,
5393 .cleanup = alt_capture_pcm_cleanup
5397 static const struct hda_pcm_stream pcm_digital_playback = {
5401 /* NID is set in build_pcms */
5403 .open = dig_playback_pcm_open,
5404 .close = dig_playback_pcm_close,
5405 .prepare = dig_playback_pcm_prepare,
5406 .cleanup = dig_playback_pcm_cleanup
5410 static const struct hda_pcm_stream pcm_digital_capture = {
5414 /* NID is set in build_pcms */
5417 /* Used by build_pcms to flag that a PCM has no playback stream */
5418 static const struct hda_pcm_stream pcm_null_stream = {
5425 * dynamic changing ADC PCM streams
5427 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5429 struct hda_gen_spec *spec = codec->spec;
5430 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5432 if (spec->cur_adc && spec->cur_adc != new_adc) {
5433 /* stream is running, let's swap the current ADC */
5434 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5435 spec->cur_adc = new_adc;
5436 snd_hda_codec_setup_stream(codec, new_adc,
5437 spec->cur_adc_stream_tag, 0,
5438 spec->cur_adc_format);
5444 /* analog capture with dynamic dual-adc changes */
5445 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5446 struct hda_codec *codec,
5447 unsigned int stream_tag,
5448 unsigned int format,
5449 struct snd_pcm_substream *substream)
5451 struct hda_gen_spec *spec = codec->spec;
5452 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5453 spec->cur_adc_stream_tag = stream_tag;
5454 spec->cur_adc_format = format;
5455 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5459 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5460 struct hda_codec *codec,
5461 struct snd_pcm_substream *substream)
5463 struct hda_gen_spec *spec = codec->spec;
5464 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5469 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5473 .nid = 0, /* fill later */
5475 .prepare = dyn_adc_capture_pcm_prepare,
5476 .cleanup = dyn_adc_capture_pcm_cleanup
5480 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5481 const char *chip_name)
5487 strlcpy(str, chip_name, len);
5489 /* drop non-alnum chars after a space */
5490 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5491 if (!isalnum(p[1])) {
5496 strlcat(str, sfx, len);
5499 /* copy PCM stream info from @default_str, and override non-NULL entries
5500 * from @spec_str and @nid
5502 static void setup_pcm_stream(struct hda_pcm_stream *str,
5503 const struct hda_pcm_stream *default_str,
5504 const struct hda_pcm_stream *spec_str,
5507 *str = *default_str;
5511 if (spec_str->substreams)
5512 str->substreams = spec_str->substreams;
5513 if (spec_str->channels_min)
5514 str->channels_min = spec_str->channels_min;
5515 if (spec_str->channels_max)
5516 str->channels_max = spec_str->channels_max;
5517 if (spec_str->rates)
5518 str->rates = spec_str->rates;
5519 if (spec_str->formats)
5520 str->formats = spec_str->formats;
5521 if (spec_str->maxbps)
5522 str->maxbps = spec_str->maxbps;
5527 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5528 * @codec: the HDA codec
5530 * Pass this to build_pcms patch_ops.
5532 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5534 struct hda_gen_spec *spec = codec->spec;
5535 struct hda_pcm *info;
5536 bool have_multi_adcs;
5538 if (spec->no_analog)
5541 fill_pcm_stream_name(spec->stream_name_analog,
5542 sizeof(spec->stream_name_analog),
5543 " Analog", codec->core.chip_name);
5544 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5547 spec->pcm_rec[0] = info;
5549 if (spec->multiout.num_dacs > 0) {
5550 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5551 &pcm_analog_playback,
5552 spec->stream_analog_playback,
5553 spec->multiout.dac_nids[0]);
5554 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5555 spec->multiout.max_channels;
5556 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5557 spec->autocfg.line_outs == 2)
5558 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5561 if (spec->num_adc_nids) {
5562 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5563 (spec->dyn_adc_switch ?
5564 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5565 spec->stream_analog_capture,
5570 /* SPDIF for stream index #1 */
5571 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5572 fill_pcm_stream_name(spec->stream_name_digital,
5573 sizeof(spec->stream_name_digital),
5574 " Digital", codec->core.chip_name);
5575 info = snd_hda_codec_pcm_new(codec, "%s",
5576 spec->stream_name_digital);
5579 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5580 spec->pcm_rec[1] = info;
5581 if (spec->dig_out_type)
5582 info->pcm_type = spec->dig_out_type;
5584 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5585 if (spec->multiout.dig_out_nid)
5586 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5587 &pcm_digital_playback,
5588 spec->stream_digital_playback,
5589 spec->multiout.dig_out_nid);
5590 if (spec->dig_in_nid)
5591 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5592 &pcm_digital_capture,
5593 spec->stream_digital_capture,
5597 if (spec->no_analog)
5600 /* If the use of more than one ADC is requested for the current
5601 * model, configure a second analog capture-only PCM.
5603 have_multi_adcs = (spec->num_adc_nids > 1) &&
5604 !spec->dyn_adc_switch && !spec->auto_mic;
5605 /* Additional Analaog capture for index #2 */
5606 if (spec->alt_dac_nid || have_multi_adcs) {
5607 fill_pcm_stream_name(spec->stream_name_alt_analog,
5608 sizeof(spec->stream_name_alt_analog),
5609 " Alt Analog", codec->core.chip_name);
5610 info = snd_hda_codec_pcm_new(codec, "%s",
5611 spec->stream_name_alt_analog);
5614 spec->pcm_rec[2] = info;
5615 if (spec->alt_dac_nid)
5616 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5617 &pcm_analog_alt_playback,
5618 spec->stream_analog_alt_playback,
5621 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5622 &pcm_null_stream, NULL, 0);
5623 if (have_multi_adcs) {
5624 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5625 &pcm_analog_alt_capture,
5626 spec->stream_analog_alt_capture,
5628 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5629 spec->num_adc_nids - 1;
5631 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5632 &pcm_null_stream, NULL, 0);
5638 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5642 * Standard auto-parser initializations
5645 /* configure the given path as a proper output */
5646 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5648 struct nid_path *path;
5651 path = snd_hda_get_path_from_idx(codec, path_idx);
5652 if (!path || !path->depth)
5654 pin = path->path[path->depth - 1];
5655 restore_pin_ctl(codec, pin);
5656 snd_hda_activate_path(codec, path, path->active,
5657 aamix_default(codec->spec));
5658 set_pin_eapd(codec, pin, path->active);
5661 /* initialize primary output paths */
5662 static void init_multi_out(struct hda_codec *codec)
5664 struct hda_gen_spec *spec = codec->spec;
5667 for (i = 0; i < spec->autocfg.line_outs; i++)
5668 set_output_and_unmute(codec, spec->out_paths[i]);
5672 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5676 for (i = 0; i < num_outs; i++)
5677 set_output_and_unmute(codec, paths[i]);
5680 /* initialize hp and speaker paths */
5681 static void init_extra_out(struct hda_codec *codec)
5683 struct hda_gen_spec *spec = codec->spec;
5685 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5686 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5687 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5688 __init_extra_out(codec, spec->autocfg.speaker_outs,
5689 spec->speaker_paths);
5692 /* initialize multi-io paths */
5693 static void init_multi_io(struct hda_codec *codec)
5695 struct hda_gen_spec *spec = codec->spec;
5698 for (i = 0; i < spec->multi_ios; i++) {
5699 hda_nid_t pin = spec->multi_io[i].pin;
5700 struct nid_path *path;
5701 path = get_multiio_path(codec, i);
5704 if (!spec->multi_io[i].ctl_in)
5705 spec->multi_io[i].ctl_in =
5706 snd_hda_codec_get_pin_target(codec, pin);
5707 snd_hda_activate_path(codec, path, path->active,
5708 aamix_default(spec));
5712 static void init_aamix_paths(struct hda_codec *codec)
5714 struct hda_gen_spec *spec = codec->spec;
5716 if (!spec->have_aamix_ctl)
5718 if (!has_aamix_out_paths(spec))
5720 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5721 spec->aamix_out_paths[0],
5722 spec->autocfg.line_out_type);
5723 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5724 spec->aamix_out_paths[1],
5726 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5727 spec->aamix_out_paths[2],
5728 AUTO_PIN_SPEAKER_OUT);
5731 /* set up input pins and loopback paths */
5732 static void init_analog_input(struct hda_codec *codec)
5734 struct hda_gen_spec *spec = codec->spec;
5735 struct auto_pin_cfg *cfg = &spec->autocfg;
5738 for (i = 0; i < cfg->num_inputs; i++) {
5739 hda_nid_t nid = cfg->inputs[i].pin;
5740 if (is_input_pin(codec, nid))
5741 restore_pin_ctl(codec, nid);
5743 /* init loopback inputs */
5744 if (spec->mixer_nid) {
5745 resume_path_from_idx(codec, spec->loopback_paths[i]);
5746 resume_path_from_idx(codec, spec->loopback_merge_path);
5751 /* initialize ADC paths */
5752 static void init_input_src(struct hda_codec *codec)
5754 struct hda_gen_spec *spec = codec->spec;
5755 struct hda_input_mux *imux = &spec->input_mux;
5756 struct nid_path *path;
5759 if (spec->dyn_adc_switch)
5762 nums = spec->num_adc_nids;
5764 for (c = 0; c < nums; c++) {
5765 for (i = 0; i < imux->num_items; i++) {
5766 path = get_input_path(codec, c, i);
5768 bool active = path->active;
5769 if (i == spec->cur_mux[c])
5771 snd_hda_activate_path(codec, path, active, false);
5775 update_hp_mic(codec, c, true);
5778 if (spec->cap_sync_hook)
5779 spec->cap_sync_hook(codec, NULL, NULL);
5782 /* set right pin controls for digital I/O */
5783 static void init_digital(struct hda_codec *codec)
5785 struct hda_gen_spec *spec = codec->spec;
5789 for (i = 0; i < spec->autocfg.dig_outs; i++)
5790 set_output_and_unmute(codec, spec->digout_paths[i]);
5791 pin = spec->autocfg.dig_in_pin;
5793 restore_pin_ctl(codec, pin);
5794 resume_path_from_idx(codec, spec->digin_path);
5798 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5799 * invalid unsol tags by some reason
5801 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5805 for (i = 0; i < codec->init_pins.used; i++) {
5806 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5807 hda_nid_t nid = pin->nid;
5808 if (is_jack_detectable(codec, nid) &&
5809 !snd_hda_jack_tbl_get(codec, nid))
5810 snd_hda_codec_update_cache(codec, nid, 0,
5811 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5816 * snd_hda_gen_init - initialize the generic spec
5817 * @codec: the HDA codec
5819 * This can be put as patch_ops init function.
5821 int snd_hda_gen_init(struct hda_codec *codec)
5823 struct hda_gen_spec *spec = codec->spec;
5825 if (spec->init_hook)
5826 spec->init_hook(codec);
5828 snd_hda_apply_verbs(codec);
5830 init_multi_out(codec);
5831 init_extra_out(codec);
5832 init_multi_io(codec);
5833 init_aamix_paths(codec);
5834 init_analog_input(codec);
5835 init_input_src(codec);
5836 init_digital(codec);
5838 clear_unsol_on_unused_pins(codec);
5840 sync_all_pin_power_ctls(codec);
5842 /* call init functions of standard auto-mute helpers */
5843 update_automute_all(codec);
5845 regcache_sync(codec->core.regmap);
5847 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5848 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5850 hda_call_check_power_status(codec, 0x01);
5853 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
5856 * snd_hda_gen_free - free the generic spec
5857 * @codec: the HDA codec
5859 * This can be put as patch_ops free function.
5861 void snd_hda_gen_free(struct hda_codec *codec)
5863 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
5864 snd_hda_gen_spec_free(codec->spec);
5868 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
5872 * snd_hda_gen_check_power_status - check the loopback power save state
5873 * @codec: the HDA codec
5874 * @nid: NID to inspect
5876 * This can be put as patch_ops check_power_status function.
5878 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5880 struct hda_gen_spec *spec = codec->spec;
5881 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5883 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
5888 * the generic codec support
5891 static const struct hda_codec_ops generic_patch_ops = {
5892 .build_controls = snd_hda_gen_build_controls,
5893 .build_pcms = snd_hda_gen_build_pcms,
5894 .init = snd_hda_gen_init,
5895 .free = snd_hda_gen_free,
5896 .unsol_event = snd_hda_jack_unsol_event,
5898 .check_power_status = snd_hda_gen_check_power_status,
5903 * snd_hda_parse_generic_codec - Generic codec parser
5904 * @codec: the HDA codec
5906 static int snd_hda_parse_generic_codec(struct hda_codec *codec)
5908 struct hda_gen_spec *spec;
5911 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5914 snd_hda_gen_spec_init(spec);
5917 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5921 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5925 codec->patch_ops = generic_patch_ops;
5929 snd_hda_gen_free(codec);
5933 static const struct hda_device_id snd_hda_id_generic[] = {
5934 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
5937 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
5939 static struct hda_codec_driver generic_driver = {
5940 .id = snd_hda_id_generic,
5943 module_hda_codec_driver(generic_driver);
5945 MODULE_LICENSE("GPL");
5946 MODULE_DESCRIPTION("Generic HD-audio codec parser");