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 || path->idx[i] == idx)
683 /* check whether the NID is referred by any active paths */
684 #define is_active_nid_for_any(codec, nid) \
685 is_active_nid(codec, nid, HDA_OUTPUT, 0)
687 /* get the default amp value for the target state */
688 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
689 int dir, unsigned int caps, bool enable)
691 unsigned int val = 0;
693 if (caps & AC_AMPCAP_NUM_STEPS) {
696 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
698 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
705 /* is this a stereo widget or a stereo-to-mono mix? */
706 static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
708 unsigned int wcaps = get_wcaps(codec, nid);
711 if (wcaps & AC_WCAP_STEREO)
713 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
715 if (snd_hda_get_num_conns(codec, nid) != 1)
717 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
719 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
722 /* initialize the amp value (only at the first time) */
723 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
725 unsigned int caps = query_amp_caps(codec, nid, dir);
726 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
728 if (is_stereo_amps(codec, nid, dir))
729 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
731 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
734 /* update the amp, doing in stereo or mono depending on NID */
735 static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
736 unsigned int mask, unsigned int val)
738 if (is_stereo_amps(codec, nid, dir))
739 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
742 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
746 /* calculate amp value mask we can modify;
747 * if the given amp is controlled by mixers, don't touch it
749 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
750 hda_nid_t nid, int dir, int idx,
753 unsigned int mask = 0xff;
755 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
756 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
759 if (caps & AC_AMPCAP_NUM_STEPS) {
760 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
761 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
767 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
768 int idx, int idx_to_check, bool enable)
771 unsigned int mask, val;
773 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
776 caps = query_amp_caps(codec, nid, dir);
777 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
778 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
783 update_amp(codec, nid, dir, idx, mask, val);
786 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
789 hda_nid_t nid = path->path[i];
790 init_amp(codec, nid, HDA_OUTPUT, 0);
791 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
794 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
795 int i, bool enable, bool add_aamix)
797 struct hda_gen_spec *spec = codec->spec;
798 const hda_nid_t *conn;
801 hda_nid_t nid = path->path[i];
803 nums = snd_hda_get_conn_list(codec, nid, &conn);
804 type = get_wcaps_type(get_wcaps(codec, nid));
805 if (type == AC_WID_PIN ||
806 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
812 for (n = 0; n < nums; n++)
813 init_amp(codec, nid, HDA_INPUT, n);
815 /* here is a little bit tricky in comparison with activate_amp_out();
816 * when aa-mixer is available, we need to enable the path as well
818 for (n = 0; n < nums; n++) {
819 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
821 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
825 /* sync power of each widget in the the given path */
826 static hda_nid_t path_power_update(struct hda_codec *codec,
827 struct nid_path *path,
828 bool allow_powerdown)
830 hda_nid_t nid, changed = 0;
833 for (i = 0; i < path->depth; i++) {
835 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
837 if (nid == codec->core.afg)
839 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
843 if (!snd_hda_check_power_state(codec, nid, state)) {
844 snd_hda_codec_write(codec, nid, 0,
845 AC_VERB_SET_POWER_STATE, state);
847 if (state == AC_PWRST_D0)
848 snd_hdac_regmap_sync_node(&codec->core, nid);
854 /* do sync with the last power state change */
855 static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
859 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
864 * snd_hda_activate_path - activate or deactivate the given path
865 * @codec: the HDA codec
866 * @path: the path to activate/deactivate
867 * @enable: flag to activate or not
868 * @add_aamix: enable the input from aamix NID
870 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
872 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
873 bool enable, bool add_aamix)
875 struct hda_gen_spec *spec = codec->spec;
879 path->active = false;
881 /* make sure the widget is powered up */
882 if (enable && (spec->power_down_unused || codec->power_save_node))
883 path_power_update(codec, path, codec->power_save_node);
885 for (i = path->depth - 1; i >= 0; i--) {
886 hda_nid_t nid = path->path[i];
888 if (enable && path->multi[i])
889 snd_hda_codec_update_cache(codec, nid, 0,
890 AC_VERB_SET_CONNECT_SEL,
892 if (has_amp_in(codec, path, i))
893 activate_amp_in(codec, path, i, enable, add_aamix);
894 if (has_amp_out(codec, path, i))
895 activate_amp_out(codec, path, i, enable);
901 EXPORT_SYMBOL_GPL(snd_hda_activate_path);
903 /* if the given path is inactive, put widgets into D3 (only if suitable) */
904 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
906 struct hda_gen_spec *spec = codec->spec;
908 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
910 sync_power_state_change(codec, path_power_update(codec, path, true));
913 /* turn on/off EAPD on the given pin */
914 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
916 struct hda_gen_spec *spec = codec->spec;
917 if (spec->own_eapd_ctl ||
918 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
920 if (spec->keep_eapd_on && !enable)
924 snd_hda_codec_update_cache(codec, pin, 0,
925 AC_VERB_SET_EAPD_BTLENABLE,
926 enable ? 0x02 : 0x00);
929 /* re-initialize the path specified by the given path index */
930 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
932 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
934 snd_hda_activate_path(codec, path, path->active, false);
939 * Helper functions for creating mixer ctl elements
942 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
943 struct snd_ctl_elem_value *ucontrol);
944 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
945 struct snd_ctl_elem_value *ucontrol);
952 static const struct snd_kcontrol_new control_templates[] = {
953 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
954 /* only the put callback is replaced for handling the special mute */
956 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
957 .subdevice = HDA_SUBDEV_AMP_FLAG,
958 .info = snd_hda_mixer_amp_switch_info,
959 .get = snd_hda_mixer_amp_switch_get,
960 .put = hda_gen_mixer_mute_put, /* replaced */
961 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
964 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
965 .info = snd_hda_mixer_amp_switch_info,
966 .get = snd_hda_mixer_bind_switch_get,
967 .put = hda_gen_bind_mute_put, /* replaced */
968 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
972 /* add dynamic controls from template */
973 static struct snd_kcontrol_new *
974 add_control(struct hda_gen_spec *spec, int type, const char *name,
975 int cidx, unsigned long val)
977 struct snd_kcontrol_new *knew;
979 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
983 if (get_amp_nid_(val))
984 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
985 knew->private_value = val;
989 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
990 const char *pfx, const char *dir,
991 const char *sfx, int cidx, unsigned long val)
993 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
994 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
995 if (!add_control(spec, type, name, cidx, val))
1000 #define add_pb_vol_ctrl(spec, type, pfx, val) \
1001 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1002 #define add_pb_sw_ctrl(spec, type, pfx, val) \
1003 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1004 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1005 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1006 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1007 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1009 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1010 unsigned int chs, struct nid_path *path)
1015 val = path->ctls[NID_PATH_VOL_CTL];
1018 val = amp_val_replace_channels(val, chs);
1019 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1022 /* return the channel bits suitable for the given path->ctls[] */
1023 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1026 int chs = 1; /* mono (left only) */
1028 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1029 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1030 chs = 3; /* stereo */
1035 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1036 struct nid_path *path)
1038 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1039 return add_vol_ctl(codec, pfx, cidx, chs, path);
1042 /* create a mute-switch for the given mixer widget;
1043 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1045 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1046 unsigned int chs, struct nid_path *path)
1049 int type = HDA_CTL_WIDGET_MUTE;
1053 val = path->ctls[NID_PATH_MUTE_CTL];
1056 val = amp_val_replace_channels(val, chs);
1057 if (get_amp_direction_(val) == HDA_INPUT) {
1058 hda_nid_t nid = get_amp_nid_(val);
1059 int nums = snd_hda_get_num_conns(codec, nid);
1061 type = HDA_CTL_BIND_MUTE;
1065 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1068 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1069 int cidx, struct nid_path *path)
1071 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1072 return add_sw_ctl(codec, pfx, cidx, chs, path);
1075 /* playback mute control with the software mute bit check */
1076 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1077 struct snd_ctl_elem_value *ucontrol)
1079 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1080 struct hda_gen_spec *spec = codec->spec;
1082 if (spec->auto_mute_via_amp) {
1083 hda_nid_t nid = get_amp_nid(kcontrol);
1084 bool enabled = !((spec->mute_bits >> nid) & 1);
1085 ucontrol->value.integer.value[0] &= enabled;
1086 ucontrol->value.integer.value[1] &= enabled;
1090 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1091 struct snd_ctl_elem_value *ucontrol)
1093 sync_auto_mute_bits(kcontrol, ucontrol);
1094 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1097 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1098 struct snd_ctl_elem_value *ucontrol)
1100 sync_auto_mute_bits(kcontrol, ucontrol);
1101 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1104 /* any ctl assigned to the path with the given index? */
1105 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1107 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1108 return path && path->ctls[ctl_type];
1111 static const char * const channel_name[4] = {
1112 "Front", "Surround", "CLFE", "Side"
1115 /* give some appropriate ctl name prefix for the given line out channel */
1116 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1117 int *index, int ctl_type)
1119 struct hda_gen_spec *spec = codec->spec;
1120 struct auto_pin_cfg *cfg = &spec->autocfg;
1123 if (cfg->line_outs == 1 && !spec->multi_ios &&
1124 !cfg->hp_outs && !cfg->speaker_outs)
1125 return spec->vmaster_mute.hook ? "PCM" : "Master";
1127 /* if there is really a single DAC used in the whole output paths,
1128 * use it master (or "PCM" if a vmaster hook is present)
1130 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1131 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1132 return spec->vmaster_mute.hook ? "PCM" : "Master";
1134 /* multi-io channels */
1135 if (ch >= cfg->line_outs)
1136 return channel_name[ch];
1138 switch (cfg->line_out_type) {
1139 case AUTO_PIN_SPEAKER_OUT:
1140 /* if the primary channel vol/mute is shared with HP volume,
1141 * don't name it as Speaker
1143 if (!ch && cfg->hp_outs &&
1144 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1146 if (cfg->line_outs == 1)
1148 if (cfg->line_outs == 2)
1149 return ch ? "Bass Speaker" : "Speaker";
1151 case AUTO_PIN_HP_OUT:
1152 /* if the primary channel vol/mute is shared with spk volume,
1153 * don't name it as Headphone
1155 if (!ch && cfg->speaker_outs &&
1156 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1158 /* for multi-io case, only the primary out */
1159 if (ch && spec->multi_ios)
1163 case AUTO_PIN_LINE_OUT:
1164 /* This deals with the case where we have two DACs and
1165 * one LO, one HP and one Speaker */
1166 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1167 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1168 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1169 if (hp_lo_shared && spk_lo_shared)
1170 return spec->vmaster_mute.hook ? "PCM" : "Master";
1172 return "Headphone+LO";
1174 return "Speaker+LO";
1178 /* for a single channel output, we don't have to name the channel */
1179 if (cfg->line_outs == 1 && !spec->multi_ios)
1182 if (ch >= ARRAY_SIZE(channel_name)) {
1187 return channel_name[ch];
1191 * Parse output paths
1194 /* badness definition */
1196 /* No primary DAC is found for the main output */
1197 BAD_NO_PRIMARY_DAC = 0x10000,
1198 /* No DAC is found for the extra output */
1199 BAD_NO_DAC = 0x4000,
1200 /* No possible multi-ios */
1201 BAD_MULTI_IO = 0x120,
1202 /* No individual DAC for extra output */
1203 BAD_NO_EXTRA_DAC = 0x102,
1204 /* No individual DAC for extra surrounds */
1205 BAD_NO_EXTRA_SURR_DAC = 0x101,
1206 /* Primary DAC shared with main surrounds */
1207 BAD_SHARED_SURROUND = 0x100,
1208 /* No independent HP possible */
1209 BAD_NO_INDEP_HP = 0x10,
1210 /* Primary DAC shared with main CLFE */
1211 BAD_SHARED_CLFE = 0x10,
1212 /* Primary DAC shared with extra surrounds */
1213 BAD_SHARED_EXTRA_SURROUND = 0x10,
1214 /* Volume widget is shared */
1215 BAD_SHARED_VOL = 0x10,
1218 /* look for widgets in the given path which are appropriate for
1219 * volume and mute controls, and assign the values to ctls[].
1221 * When no appropriate widget is found in the path, the badness value
1222 * is incremented depending on the situation. The function returns the
1223 * total badness for both volume and mute controls.
1225 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1227 struct hda_gen_spec *spec = codec->spec;
1233 return BAD_SHARED_VOL * 2;
1235 if (path->ctls[NID_PATH_VOL_CTL] ||
1236 path->ctls[NID_PATH_MUTE_CTL])
1237 return 0; /* already evaluated */
1239 nid = look_for_out_vol_nid(codec, path);
1241 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1242 if (spec->dac_min_mute)
1243 val |= HDA_AMP_VAL_MIN_MUTE;
1244 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1245 badness += BAD_SHARED_VOL;
1247 path->ctls[NID_PATH_VOL_CTL] = val;
1249 badness += BAD_SHARED_VOL;
1250 nid = look_for_out_mute_nid(codec, path);
1252 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1253 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1254 nid_has_mute(codec, nid, HDA_OUTPUT))
1255 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1257 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1258 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1259 badness += BAD_SHARED_VOL;
1261 path->ctls[NID_PATH_MUTE_CTL] = val;
1263 badness += BAD_SHARED_VOL;
1267 const struct badness_table hda_main_out_badness = {
1268 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1269 .no_dac = BAD_NO_DAC,
1270 .shared_primary = BAD_NO_PRIMARY_DAC,
1271 .shared_surr = BAD_SHARED_SURROUND,
1272 .shared_clfe = BAD_SHARED_CLFE,
1273 .shared_surr_main = BAD_SHARED_SURROUND,
1275 EXPORT_SYMBOL_GPL(hda_main_out_badness);
1277 const struct badness_table hda_extra_out_badness = {
1278 .no_primary_dac = BAD_NO_DAC,
1279 .no_dac = BAD_NO_DAC,
1280 .shared_primary = BAD_NO_EXTRA_DAC,
1281 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1282 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1283 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1285 EXPORT_SYMBOL_GPL(hda_extra_out_badness);
1287 /* get the DAC of the primary output corresponding to the given array index */
1288 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1290 struct hda_gen_spec *spec = codec->spec;
1291 struct auto_pin_cfg *cfg = &spec->autocfg;
1293 if (cfg->line_outs > idx)
1294 return spec->private_dac_nids[idx];
1295 idx -= cfg->line_outs;
1296 if (spec->multi_ios > idx)
1297 return spec->multi_io[idx].dac;
1301 /* return the DAC if it's reachable, otherwise zero */
1302 static inline hda_nid_t try_dac(struct hda_codec *codec,
1303 hda_nid_t dac, hda_nid_t pin)
1305 return is_reachable_path(codec, dac, pin) ? dac : 0;
1308 /* try to assign DACs to pins and return the resultant badness */
1309 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1310 const hda_nid_t *pins, hda_nid_t *dacs,
1312 const struct badness_table *bad)
1314 struct hda_gen_spec *spec = codec->spec;
1322 for (i = 0; i < num_outs; i++) {
1323 struct nid_path *path;
1324 hda_nid_t pin = pins[i];
1326 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1328 badness += assign_out_path_ctls(codec, path);
1332 dacs[i] = get_preferred_dac(codec, pin);
1334 if (is_dac_already_used(codec, dacs[i]))
1335 badness += bad->shared_primary;
1339 dacs[i] = look_for_dac(codec, pin, false);
1340 if (!dacs[i] && !i) {
1341 /* try to steal the DAC of surrounds for the front */
1342 for (j = 1; j < num_outs; j++) {
1343 if (is_reachable_path(codec, dacs[j], pin)) {
1346 invalidate_nid_path(codec, path_idx[j]);
1355 dac = try_dac(codec, get_primary_out(codec, i), pin);
1357 dac = try_dac(codec, dacs[0], pin);
1359 dac = try_dac(codec, get_primary_out(codec, i), pin);
1362 badness += bad->shared_primary;
1364 badness += bad->shared_surr;
1366 badness += bad->shared_clfe;
1367 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1368 dac = spec->private_dac_nids[0];
1369 badness += bad->shared_surr_main;
1371 badness += bad->no_primary_dac;
1373 badness += bad->no_dac;
1377 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1378 if (!path && !i && spec->mixer_nid) {
1379 /* try with aamix */
1380 path = snd_hda_add_new_path(codec, dac, pin, 0);
1384 badness += bad->no_dac;
1386 /* print_nid_path(codec, "output", path); */
1387 path->active = true;
1388 path_idx[i] = snd_hda_get_path_idx(codec, path);
1389 badness += assign_out_path_ctls(codec, path);
1396 /* return NID if the given pin has only a single connection to a certain DAC */
1397 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1399 struct hda_gen_spec *spec = codec->spec;
1401 hda_nid_t nid_found = 0;
1403 for (i = 0; i < spec->num_all_dacs; i++) {
1404 hda_nid_t nid = spec->all_dacs[i];
1405 if (!nid || is_dac_already_used(codec, nid))
1407 if (is_reachable_path(codec, nid, pin)) {
1416 /* check whether the given pin can be a multi-io pin */
1417 static bool can_be_multiio_pin(struct hda_codec *codec,
1418 unsigned int location, hda_nid_t nid)
1420 unsigned int defcfg, caps;
1422 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1423 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1425 if (location && get_defcfg_location(defcfg) != location)
1427 caps = snd_hda_query_pin_caps(codec, nid);
1428 if (!(caps & AC_PINCAP_OUT))
1433 /* count the number of input pins that are capable to be multi-io */
1434 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1436 struct hda_gen_spec *spec = codec->spec;
1437 struct auto_pin_cfg *cfg = &spec->autocfg;
1438 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1439 unsigned int location = get_defcfg_location(defcfg);
1443 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1444 for (i = 0; i < cfg->num_inputs; i++) {
1445 if (cfg->inputs[i].type != type)
1447 if (can_be_multiio_pin(codec, location,
1448 cfg->inputs[i].pin))
1458 * When hardwired is set, try to fill ony hardwired pins, and returns
1459 * zero if any pins are filled, non-zero if nothing found.
1460 * When hardwired is off, try to fill possible input pins, and returns
1461 * the badness value.
1463 static int fill_multi_ios(struct hda_codec *codec,
1464 hda_nid_t reference_pin,
1467 struct hda_gen_spec *spec = codec->spec;
1468 struct auto_pin_cfg *cfg = &spec->autocfg;
1469 int type, i, j, num_pins, old_pins;
1470 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1471 unsigned int location = get_defcfg_location(defcfg);
1473 struct nid_path *path;
1475 old_pins = spec->multi_ios;
1479 num_pins = count_multiio_pins(codec, reference_pin);
1483 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1484 for (i = 0; i < cfg->num_inputs; i++) {
1485 hda_nid_t nid = cfg->inputs[i].pin;
1488 if (cfg->inputs[i].type != type)
1490 if (!can_be_multiio_pin(codec, location, nid))
1492 for (j = 0; j < spec->multi_ios; j++) {
1493 if (nid == spec->multi_io[j].pin)
1496 if (j < spec->multi_ios)
1500 dac = get_dac_if_single(codec, nid);
1502 dac = look_for_dac(codec, nid, false);
1507 path = snd_hda_add_new_path(codec, dac, nid,
1513 /* print_nid_path(codec, "multiio", path); */
1514 spec->multi_io[spec->multi_ios].pin = nid;
1515 spec->multi_io[spec->multi_ios].dac = dac;
1516 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1517 snd_hda_get_path_idx(codec, path);
1519 if (spec->multi_ios >= 2)
1525 badness = BAD_MULTI_IO;
1526 if (old_pins == spec->multi_ios) {
1528 return 1; /* nothing found */
1530 return badness; /* no badness if nothing found */
1532 if (!hardwired && spec->multi_ios < 2) {
1533 /* cancel newly assigned paths */
1534 spec->paths.used -= spec->multi_ios - old_pins;
1535 spec->multi_ios = old_pins;
1539 /* assign volume and mute controls */
1540 for (i = old_pins; i < spec->multi_ios; i++) {
1541 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1542 badness += assign_out_path_ctls(codec, path);
1548 /* map DACs for all pins in the list if they are single connections */
1549 static bool map_singles(struct hda_codec *codec, int outs,
1550 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1552 struct hda_gen_spec *spec = codec->spec;
1555 for (i = 0; i < outs; i++) {
1556 struct nid_path *path;
1560 dac = get_dac_if_single(codec, pins[i]);
1563 path = snd_hda_add_new_path(codec, dac, pins[i],
1565 if (!path && !i && spec->mixer_nid)
1566 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1570 /* print_nid_path(codec, "output", path); */
1571 path->active = true;
1572 path_idx[i] = snd_hda_get_path_idx(codec, path);
1578 /* create a new path including aamix if available, and return its index */
1579 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1581 struct hda_gen_spec *spec = codec->spec;
1582 struct nid_path *path;
1583 hda_nid_t path_dac, dac, pin;
1585 path = snd_hda_get_path_from_idx(codec, path_idx);
1586 if (!path || !path->depth ||
1587 is_nid_contained(path, spec->mixer_nid))
1589 path_dac = path->path[0];
1590 dac = spec->private_dac_nids[0];
1591 pin = path->path[path->depth - 1];
1592 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1594 if (dac != path_dac)
1596 else if (spec->multiout.hp_out_nid[0])
1597 dac = spec->multiout.hp_out_nid[0];
1598 else if (spec->multiout.extra_out_nid[0])
1599 dac = spec->multiout.extra_out_nid[0];
1603 path = snd_hda_add_new_path(codec, dac, pin,
1608 /* print_nid_path(codec, "output-aamix", path); */
1609 path->active = false; /* unused as default */
1610 path->pin_fixed = true; /* static route */
1611 return snd_hda_get_path_idx(codec, path);
1614 /* check whether the independent HP is available with the current config */
1615 static bool indep_hp_possible(struct hda_codec *codec)
1617 struct hda_gen_spec *spec = codec->spec;
1618 struct auto_pin_cfg *cfg = &spec->autocfg;
1619 struct nid_path *path;
1622 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1623 idx = spec->out_paths[0];
1625 idx = spec->hp_paths[0];
1626 path = snd_hda_get_path_from_idx(codec, idx);
1630 /* assume no path conflicts unless aamix is involved */
1631 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1634 /* check whether output paths contain aamix */
1635 for (i = 0; i < cfg->line_outs; i++) {
1636 if (spec->out_paths[i] == idx)
1638 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1639 if (path && is_nid_contained(path, spec->mixer_nid))
1642 for (i = 0; i < cfg->speaker_outs; i++) {
1643 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1644 if (path && is_nid_contained(path, spec->mixer_nid))
1651 /* fill the empty entries in the dac array for speaker/hp with the
1652 * shared dac pointed by the paths
1654 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1655 hda_nid_t *dacs, int *path_idx)
1657 struct nid_path *path;
1660 for (i = 0; i < num_outs; i++) {
1663 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1666 dacs[i] = path->path[0];
1670 /* fill in the dac_nids table from the parsed pin configuration */
1671 static int fill_and_eval_dacs(struct hda_codec *codec,
1672 bool fill_hardwired,
1673 bool fill_mio_first)
1675 struct hda_gen_spec *spec = codec->spec;
1676 struct auto_pin_cfg *cfg = &spec->autocfg;
1677 int i, err, badness;
1679 /* set num_dacs once to full for look_for_dac() */
1680 spec->multiout.num_dacs = cfg->line_outs;
1681 spec->multiout.dac_nids = spec->private_dac_nids;
1682 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1683 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1684 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1685 spec->multi_ios = 0;
1686 snd_array_free(&spec->paths);
1688 /* clear path indices */
1689 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1690 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1691 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1692 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1693 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1694 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1695 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1696 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1700 /* fill hard-wired DACs first */
1701 if (fill_hardwired) {
1704 mapped = map_singles(codec, cfg->line_outs,
1706 spec->private_dac_nids,
1708 mapped |= map_singles(codec, cfg->hp_outs,
1710 spec->multiout.hp_out_nid,
1712 mapped |= map_singles(codec, cfg->speaker_outs,
1714 spec->multiout.extra_out_nid,
1715 spec->speaker_paths);
1716 if (!spec->no_multi_io &&
1717 fill_mio_first && cfg->line_outs == 1 &&
1718 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1719 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1726 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1727 spec->private_dac_nids, spec->out_paths,
1728 spec->main_out_badness);
1730 if (!spec->no_multi_io && fill_mio_first &&
1731 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1732 /* try to fill multi-io first */
1733 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1736 /* we don't count badness at this stage yet */
1739 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1740 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1741 spec->multiout.hp_out_nid,
1743 spec->extra_out_badness);
1748 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1749 err = try_assign_dacs(codec, cfg->speaker_outs,
1751 spec->multiout.extra_out_nid,
1752 spec->speaker_paths,
1753 spec->extra_out_badness);
1758 if (!spec->no_multi_io &&
1759 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1760 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1766 if (spec->mixer_nid) {
1767 spec->aamix_out_paths[0] =
1768 check_aamix_out_path(codec, spec->out_paths[0]);
1769 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1770 spec->aamix_out_paths[1] =
1771 check_aamix_out_path(codec, spec->hp_paths[0]);
1772 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1773 spec->aamix_out_paths[2] =
1774 check_aamix_out_path(codec, spec->speaker_paths[0]);
1777 if (!spec->no_multi_io &&
1778 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1779 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1780 spec->multi_ios = 1; /* give badness */
1782 /* re-count num_dacs and squash invalid entries */
1783 spec->multiout.num_dacs = 0;
1784 for (i = 0; i < cfg->line_outs; i++) {
1785 if (spec->private_dac_nids[i])
1786 spec->multiout.num_dacs++;
1788 memmove(spec->private_dac_nids + i,
1789 spec->private_dac_nids + i + 1,
1790 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1791 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1795 spec->ext_channel_count = spec->min_channel_count =
1796 spec->multiout.num_dacs * 2;
1798 if (spec->multi_ios == 2) {
1799 for (i = 0; i < 2; i++)
1800 spec->private_dac_nids[spec->multiout.num_dacs++] =
1801 spec->multi_io[i].dac;
1802 } else if (spec->multi_ios) {
1803 spec->multi_ios = 0;
1804 badness += BAD_MULTI_IO;
1807 if (spec->indep_hp && !indep_hp_possible(codec))
1808 badness += BAD_NO_INDEP_HP;
1810 /* re-fill the shared DAC for speaker / headphone */
1811 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1812 refill_shared_dacs(codec, cfg->hp_outs,
1813 spec->multiout.hp_out_nid,
1815 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1816 refill_shared_dacs(codec, cfg->speaker_outs,
1817 spec->multiout.extra_out_nid,
1818 spec->speaker_paths);
1823 #define DEBUG_BADNESS
1825 #ifdef DEBUG_BADNESS
1826 #define debug_badness(fmt, ...) \
1827 codec_dbg(codec, fmt, ##__VA_ARGS__)
1829 #define debug_badness(fmt, ...) \
1830 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1833 #ifdef DEBUG_BADNESS
1834 static inline void print_nid_path_idx(struct hda_codec *codec,
1835 const char *pfx, int idx)
1837 struct nid_path *path;
1839 path = snd_hda_get_path_from_idx(codec, idx);
1841 print_nid_path(codec, pfx, path);
1844 static void debug_show_configs(struct hda_codec *codec,
1845 struct auto_pin_cfg *cfg)
1847 struct hda_gen_spec *spec = codec->spec;
1848 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1851 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1852 cfg->line_out_pins[0], cfg->line_out_pins[1],
1853 cfg->line_out_pins[2], cfg->line_out_pins[3],
1854 spec->multiout.dac_nids[0],
1855 spec->multiout.dac_nids[1],
1856 spec->multiout.dac_nids[2],
1857 spec->multiout.dac_nids[3],
1858 lo_type[cfg->line_out_type]);
1859 for (i = 0; i < cfg->line_outs; i++)
1860 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1861 if (spec->multi_ios > 0)
1862 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1864 spec->multi_io[0].pin, spec->multi_io[1].pin,
1865 spec->multi_io[0].dac, spec->multi_io[1].dac);
1866 for (i = 0; i < spec->multi_ios; i++)
1867 print_nid_path_idx(codec, " mio",
1868 spec->out_paths[cfg->line_outs + i]);
1870 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1871 cfg->hp_pins[0], cfg->hp_pins[1],
1872 cfg->hp_pins[2], cfg->hp_pins[3],
1873 spec->multiout.hp_out_nid[0],
1874 spec->multiout.hp_out_nid[1],
1875 spec->multiout.hp_out_nid[2],
1876 spec->multiout.hp_out_nid[3]);
1877 for (i = 0; i < cfg->hp_outs; i++)
1878 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1879 if (cfg->speaker_outs)
1880 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1881 cfg->speaker_pins[0], cfg->speaker_pins[1],
1882 cfg->speaker_pins[2], cfg->speaker_pins[3],
1883 spec->multiout.extra_out_nid[0],
1884 spec->multiout.extra_out_nid[1],
1885 spec->multiout.extra_out_nid[2],
1886 spec->multiout.extra_out_nid[3]);
1887 for (i = 0; i < cfg->speaker_outs; i++)
1888 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1889 for (i = 0; i < 3; i++)
1890 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1893 #define debug_show_configs(codec, cfg) /* NOP */
1896 /* find all available DACs of the codec */
1897 static void fill_all_dac_nids(struct hda_codec *codec)
1899 struct hda_gen_spec *spec = codec->spec;
1902 spec->num_all_dacs = 0;
1903 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1904 for_each_hda_codec_node(nid, codec) {
1905 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1907 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1908 codec_err(codec, "Too many DACs!\n");
1911 spec->all_dacs[spec->num_all_dacs++] = nid;
1915 static int parse_output_paths(struct hda_codec *codec)
1917 struct hda_gen_spec *spec = codec->spec;
1918 struct auto_pin_cfg *cfg = &spec->autocfg;
1919 struct auto_pin_cfg *best_cfg;
1921 int best_badness = INT_MAX;
1923 bool fill_hardwired = true, fill_mio_first = true;
1924 bool best_wired = true, best_mio = true;
1925 bool hp_spk_swapped = false;
1927 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1933 badness = fill_and_eval_dacs(codec, fill_hardwired,
1939 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1940 cfg->line_out_type, fill_hardwired, fill_mio_first,
1942 debug_show_configs(codec, cfg);
1943 if (badness < best_badness) {
1944 best_badness = badness;
1946 best_wired = fill_hardwired;
1947 best_mio = fill_mio_first;
1951 fill_mio_first = !fill_mio_first;
1952 if (!fill_mio_first)
1954 fill_hardwired = !fill_hardwired;
1955 if (!fill_hardwired)
1959 hp_spk_swapped = true;
1960 if (cfg->speaker_outs > 0 &&
1961 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1962 cfg->hp_outs = cfg->line_outs;
1963 memcpy(cfg->hp_pins, cfg->line_out_pins,
1964 sizeof(cfg->hp_pins));
1965 cfg->line_outs = cfg->speaker_outs;
1966 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1967 sizeof(cfg->speaker_pins));
1968 cfg->speaker_outs = 0;
1969 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1970 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1971 fill_hardwired = true;
1974 if (cfg->hp_outs > 0 &&
1975 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1976 cfg->speaker_outs = cfg->line_outs;
1977 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1978 sizeof(cfg->speaker_pins));
1979 cfg->line_outs = cfg->hp_outs;
1980 memcpy(cfg->line_out_pins, cfg->hp_pins,
1981 sizeof(cfg->hp_pins));
1983 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1984 cfg->line_out_type = AUTO_PIN_HP_OUT;
1985 fill_hardwired = true;
1992 debug_badness("==> restoring best_cfg\n");
1994 fill_and_eval_dacs(codec, best_wired, best_mio);
1996 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1997 cfg->line_out_type, best_wired, best_mio);
1998 debug_show_configs(codec, cfg);
2000 if (cfg->line_out_pins[0]) {
2001 struct nid_path *path;
2002 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
2004 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
2005 if (spec->vmaster_nid) {
2006 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2007 HDA_OUTPUT, spec->vmaster_tlv);
2008 if (spec->dac_min_mute)
2009 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2013 /* set initial pinctl targets */
2014 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2018 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2019 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2020 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2021 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2022 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2023 set_pin_targets(codec, cfg->speaker_outs,
2024 cfg->speaker_pins, val);
2027 /* clear indep_hp flag if not available */
2028 if (spec->indep_hp && !indep_hp_possible(codec))
2035 /* add playback controls from the parsed DAC table */
2036 static int create_multi_out_ctls(struct hda_codec *codec,
2037 const struct auto_pin_cfg *cfg)
2039 struct hda_gen_spec *spec = codec->spec;
2040 int i, err, noutputs;
2042 noutputs = cfg->line_outs;
2043 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2044 noutputs += spec->multi_ios;
2046 for (i = 0; i < noutputs; i++) {
2049 struct nid_path *path;
2051 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
2055 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
2056 if (!name || !strcmp(name, "CLFE")) {
2058 err = add_vol_ctl(codec, "Center", 0, 1, path);
2061 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2065 err = add_stereo_vol(codec, name, index, path);
2070 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2071 if (!name || !strcmp(name, "CLFE")) {
2072 err = add_sw_ctl(codec, "Center", 0, 1, path);
2075 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2079 err = add_stereo_sw(codec, name, index, path);
2087 static int create_extra_out(struct hda_codec *codec, int path_idx,
2088 const char *pfx, int cidx)
2090 struct nid_path *path;
2093 path = snd_hda_get_path_from_idx(codec, path_idx);
2096 err = add_stereo_vol(codec, pfx, cidx, path);
2099 err = add_stereo_sw(codec, pfx, cidx, path);
2105 /* add playback controls for speaker and HP outputs */
2106 static int create_extra_outs(struct hda_codec *codec, int num_pins,
2107 const int *paths, const char *pfx)
2111 for (i = 0; i < num_pins; i++) {
2113 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2116 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2117 name = "Bass Speaker";
2118 else if (num_pins >= 3) {
2119 snprintf(tmp, sizeof(tmp), "%s %s",
2120 pfx, channel_name[i]);
2126 err = create_extra_out(codec, paths[i], name, idx);
2133 static int create_hp_out_ctls(struct hda_codec *codec)
2135 struct hda_gen_spec *spec = codec->spec;
2136 return create_extra_outs(codec, spec->autocfg.hp_outs,
2141 static int create_speaker_out_ctls(struct hda_codec *codec)
2143 struct hda_gen_spec *spec = codec->spec;
2144 return create_extra_outs(codec, spec->autocfg.speaker_outs,
2145 spec->speaker_paths,
2150 * independent HP controls
2153 static void call_hp_automute(struct hda_codec *codec,
2154 struct hda_jack_callback *jack);
2155 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2156 struct snd_ctl_elem_info *uinfo)
2158 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2161 static int indep_hp_get(struct snd_kcontrol *kcontrol,
2162 struct snd_ctl_elem_value *ucontrol)
2164 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2165 struct hda_gen_spec *spec = codec->spec;
2166 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2170 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2171 int nomix_path_idx, int mix_path_idx,
2174 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2175 struct snd_ctl_elem_value *ucontrol)
2177 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2178 struct hda_gen_spec *spec = codec->spec;
2179 unsigned int select = ucontrol->value.enumerated.item[0];
2182 mutex_lock(&spec->pcm_mutex);
2183 if (spec->active_streams) {
2188 if (spec->indep_hp_enabled != select) {
2190 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2191 dacp = &spec->private_dac_nids[0];
2193 dacp = &spec->multiout.hp_out_nid[0];
2195 /* update HP aamix paths in case it conflicts with indep HP */
2196 if (spec->have_aamix_ctl) {
2197 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2198 update_aamix_paths(codec, spec->aamix_mode,
2200 spec->aamix_out_paths[0],
2201 spec->autocfg.line_out_type);
2203 update_aamix_paths(codec, spec->aamix_mode,
2205 spec->aamix_out_paths[1],
2209 spec->indep_hp_enabled = select;
2210 if (spec->indep_hp_enabled)
2213 *dacp = spec->alt_dac_nid;
2215 call_hp_automute(codec, NULL);
2219 mutex_unlock(&spec->pcm_mutex);
2223 static const struct snd_kcontrol_new indep_hp_ctl = {
2224 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2225 .name = "Independent HP",
2226 .info = indep_hp_info,
2227 .get = indep_hp_get,
2228 .put = indep_hp_put,
2232 static int create_indep_hp_ctls(struct hda_codec *codec)
2234 struct hda_gen_spec *spec = codec->spec;
2237 if (!spec->indep_hp)
2239 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2240 dac = spec->multiout.dac_nids[0];
2242 dac = spec->multiout.hp_out_nid[0];
2248 spec->indep_hp_enabled = false;
2249 spec->alt_dac_nid = dac;
2250 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2256 * channel mode enum control
2259 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2260 struct snd_ctl_elem_info *uinfo)
2262 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2263 struct hda_gen_spec *spec = codec->spec;
2266 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2268 uinfo->value.enumerated.items = spec->multi_ios + 1;
2269 if (uinfo->value.enumerated.item > spec->multi_ios)
2270 uinfo->value.enumerated.item = spec->multi_ios;
2271 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2272 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2276 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2277 struct snd_ctl_elem_value *ucontrol)
2279 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2280 struct hda_gen_spec *spec = codec->spec;
2281 ucontrol->value.enumerated.item[0] =
2282 (spec->ext_channel_count - spec->min_channel_count) / 2;
2286 static inline struct nid_path *
2287 get_multiio_path(struct hda_codec *codec, int idx)
2289 struct hda_gen_spec *spec = codec->spec;
2290 return snd_hda_get_path_from_idx(codec,
2291 spec->out_paths[spec->autocfg.line_outs + idx]);
2294 static void update_automute_all(struct hda_codec *codec);
2296 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2297 * used for output paths
2299 static bool aamix_default(struct hda_gen_spec *spec)
2301 return !spec->have_aamix_ctl || spec->aamix_mode;
2304 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2306 struct hda_gen_spec *spec = codec->spec;
2307 hda_nid_t nid = spec->multi_io[idx].pin;
2308 struct nid_path *path;
2310 path = get_multiio_path(codec, idx);
2314 if (path->active == output)
2318 set_pin_target(codec, nid, PIN_OUT, true);
2319 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2320 set_pin_eapd(codec, nid, true);
2322 set_pin_eapd(codec, nid, false);
2323 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2324 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2325 path_power_down_sync(codec, path);
2328 /* update jack retasking in case it modifies any of them */
2329 update_automute_all(codec);
2334 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2335 struct snd_ctl_elem_value *ucontrol)
2337 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2338 struct hda_gen_spec *spec = codec->spec;
2341 ch = ucontrol->value.enumerated.item[0];
2342 if (ch < 0 || ch > spec->multi_ios)
2344 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2346 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2347 for (i = 0; i < spec->multi_ios; i++)
2348 set_multi_io(codec, i, i < ch);
2349 spec->multiout.max_channels = max(spec->ext_channel_count,
2350 spec->const_channel_count);
2351 if (spec->need_dac_fix)
2352 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2356 static const struct snd_kcontrol_new channel_mode_enum = {
2357 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2358 .name = "Channel Mode",
2359 .info = ch_mode_info,
2364 static int create_multi_channel_mode(struct hda_codec *codec)
2366 struct hda_gen_spec *spec = codec->spec;
2368 if (spec->multi_ios > 0) {
2369 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2376 * aamix loopback enable/disable switch
2379 #define loopback_mixing_info indep_hp_info
2381 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2382 struct snd_ctl_elem_value *ucontrol)
2384 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2385 struct hda_gen_spec *spec = codec->spec;
2386 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2390 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2391 int nomix_path_idx, int mix_path_idx,
2394 struct hda_gen_spec *spec = codec->spec;
2395 struct nid_path *nomix_path, *mix_path;
2397 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2398 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2399 if (!nomix_path || !mix_path)
2402 /* if HP aamix path is driven from a different DAC and the
2403 * independent HP mode is ON, can't turn on aamix path
2405 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2406 mix_path->path[0] != spec->alt_dac_nid)
2410 snd_hda_activate_path(codec, nomix_path, false, true);
2411 snd_hda_activate_path(codec, mix_path, true, true);
2412 path_power_down_sync(codec, nomix_path);
2414 snd_hda_activate_path(codec, mix_path, false, false);
2415 snd_hda_activate_path(codec, nomix_path, true, false);
2416 path_power_down_sync(codec, mix_path);
2420 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2421 struct snd_ctl_elem_value *ucontrol)
2423 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2424 struct hda_gen_spec *spec = codec->spec;
2425 unsigned int val = ucontrol->value.enumerated.item[0];
2427 if (val == spec->aamix_mode)
2429 spec->aamix_mode = val;
2430 update_aamix_paths(codec, val, spec->out_paths[0],
2431 spec->aamix_out_paths[0],
2432 spec->autocfg.line_out_type);
2433 update_aamix_paths(codec, val, spec->hp_paths[0],
2434 spec->aamix_out_paths[1],
2436 update_aamix_paths(codec, val, spec->speaker_paths[0],
2437 spec->aamix_out_paths[2],
2438 AUTO_PIN_SPEAKER_OUT);
2442 static const struct snd_kcontrol_new loopback_mixing_enum = {
2443 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2444 .name = "Loopback Mixing",
2445 .info = loopback_mixing_info,
2446 .get = loopback_mixing_get,
2447 .put = loopback_mixing_put,
2450 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2452 struct hda_gen_spec *spec = codec->spec;
2454 if (!spec->mixer_nid)
2456 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2457 spec->aamix_out_paths[2]))
2459 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2461 spec->have_aamix_ctl = 1;
2466 * shared headphone/mic handling
2469 static void call_update_outputs(struct hda_codec *codec);
2471 /* for shared I/O, change the pin-control accordingly */
2472 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2474 struct hda_gen_spec *spec = codec->spec;
2479 pin = spec->hp_mic_pin;
2480 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2483 val = snd_hda_codec_get_pin_target(codec, pin);
2493 val = snd_hda_get_default_vref(codec, pin);
2494 /* if the HP pin doesn't support VREF and the codec driver gives an
2495 * alternative pin, set up the VREF on that pin instead
2497 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2498 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2499 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2500 if (vref_val != AC_PINCTL_VREF_HIZ)
2501 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2502 PIN_IN | (as_mic ? vref_val : 0));
2505 if (!spec->hp_mic_jack_modes) {
2510 set_pin_target(codec, pin, val, true);
2511 call_hp_automute(codec, NULL);
2515 /* create a shared input with the headphone out */
2516 static int create_hp_mic(struct hda_codec *codec)
2518 struct hda_gen_spec *spec = codec->spec;
2519 struct auto_pin_cfg *cfg = &spec->autocfg;
2520 unsigned int defcfg;
2523 if (!spec->hp_mic) {
2524 if (spec->suppress_hp_mic_detect)
2526 /* automatic detection: only if no input or a single internal
2527 * input pin is found, try to detect the shared hp/mic
2529 if (cfg->num_inputs > 1)
2531 else if (cfg->num_inputs == 1) {
2532 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2533 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2538 spec->hp_mic = 0; /* clear once */
2539 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2543 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2544 nid = cfg->line_out_pins[0];
2545 else if (cfg->hp_outs > 0)
2546 nid = cfg->hp_pins[0];
2550 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2551 return 0; /* no input */
2553 cfg->inputs[cfg->num_inputs].pin = nid;
2554 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2555 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2558 spec->hp_mic_pin = nid;
2559 /* we can't handle auto-mic together with HP-mic */
2560 spec->suppress_auto_mic = 1;
2561 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
2569 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2571 static const char * const out_jack_texts[] = {
2572 "Line Out", "Headphone Out",
2575 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2576 struct snd_ctl_elem_info *uinfo)
2578 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2581 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2582 struct snd_ctl_elem_value *ucontrol)
2584 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2585 hda_nid_t nid = kcontrol->private_value;
2586 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2587 ucontrol->value.enumerated.item[0] = 1;
2589 ucontrol->value.enumerated.item[0] = 0;
2593 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2594 struct snd_ctl_elem_value *ucontrol)
2596 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2597 hda_nid_t nid = kcontrol->private_value;
2600 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2601 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2603 snd_hda_set_pin_ctl_cache(codec, nid, val);
2607 static const struct snd_kcontrol_new out_jack_mode_enum = {
2608 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2609 .info = out_jack_mode_info,
2610 .get = out_jack_mode_get,
2611 .put = out_jack_mode_put,
2614 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2616 struct hda_gen_spec *spec = codec->spec;
2619 for (i = 0; i < spec->kctls.used; i++) {
2620 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2621 if (!strcmp(kctl->name, name) && kctl->index == idx)
2627 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2628 char *name, size_t name_len)
2630 struct hda_gen_spec *spec = codec->spec;
2633 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2634 strlcat(name, " Jack Mode", name_len);
2636 for (; find_kctl_name(codec, name, idx); idx++)
2640 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2642 struct hda_gen_spec *spec = codec->spec;
2643 if (spec->add_jack_modes) {
2644 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2645 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2651 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2654 struct hda_gen_spec *spec = codec->spec;
2657 for (i = 0; i < num_pins; i++) {
2658 hda_nid_t pin = pins[i];
2659 if (pin == spec->hp_mic_pin)
2661 if (get_out_jack_num_items(codec, pin) > 1) {
2662 struct snd_kcontrol_new *knew;
2663 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2664 get_jack_mode_name(codec, pin, name, sizeof(name));
2665 knew = snd_hda_gen_add_kctl(spec, name,
2666 &out_jack_mode_enum);
2669 knew->private_value = pin;
2680 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2683 static const char * const vref_texts[NUM_VREFS] = {
2684 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2685 "", "Mic 80pc Bias", "Mic 100pc Bias"
2688 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2690 unsigned int pincap;
2692 pincap = snd_hda_query_pin_caps(codec, pin);
2693 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2694 /* filter out unusual vrefs */
2695 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2699 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2700 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2702 unsigned int i, n = 0;
2704 for (i = 0; i < NUM_VREFS; i++) {
2705 if (vref_caps & (1 << i)) {
2714 /* convert back from the vref ctl index to the enum item index */
2715 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2717 unsigned int i, n = 0;
2719 for (i = 0; i < NUM_VREFS; i++) {
2722 if (vref_caps & (1 << i))
2728 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2729 struct snd_ctl_elem_info *uinfo)
2731 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2732 hda_nid_t nid = kcontrol->private_value;
2733 unsigned int vref_caps = get_vref_caps(codec, nid);
2735 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2737 /* set the right text */
2738 strcpy(uinfo->value.enumerated.name,
2739 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2743 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2744 struct snd_ctl_elem_value *ucontrol)
2746 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2747 hda_nid_t nid = kcontrol->private_value;
2748 unsigned int vref_caps = get_vref_caps(codec, nid);
2751 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2752 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2756 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2757 struct snd_ctl_elem_value *ucontrol)
2759 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2760 hda_nid_t nid = kcontrol->private_value;
2761 unsigned int vref_caps = get_vref_caps(codec, nid);
2762 unsigned int val, idx;
2764 val = snd_hda_codec_get_pin_target(codec, nid);
2765 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2766 if (idx == ucontrol->value.enumerated.item[0])
2769 val &= ~AC_PINCTL_VREFEN;
2770 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2771 snd_hda_set_pin_ctl_cache(codec, nid, val);
2775 static const struct snd_kcontrol_new in_jack_mode_enum = {
2776 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2777 .info = in_jack_mode_info,
2778 .get = in_jack_mode_get,
2779 .put = in_jack_mode_put,
2782 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2784 struct hda_gen_spec *spec = codec->spec;
2786 if (spec->add_jack_modes)
2787 nitems = hweight32(get_vref_caps(codec, pin));
2788 return nitems ? nitems : 1;
2791 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2793 struct hda_gen_spec *spec = codec->spec;
2794 struct snd_kcontrol_new *knew;
2795 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2796 unsigned int defcfg;
2798 if (pin == spec->hp_mic_pin)
2799 return 0; /* already done in create_out_jack_mode() */
2801 /* no jack mode for fixed pins */
2802 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2803 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2806 /* no multiple vref caps? */
2807 if (get_in_jack_num_items(codec, pin) <= 1)
2810 get_jack_mode_name(codec, pin, name, sizeof(name));
2811 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2814 knew->private_value = pin;
2819 * HP/mic shared jack mode
2821 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2822 struct snd_ctl_elem_info *uinfo)
2824 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2825 hda_nid_t nid = kcontrol->private_value;
2826 int out_jacks = get_out_jack_num_items(codec, nid);
2827 int in_jacks = get_in_jack_num_items(codec, nid);
2828 const char *text = NULL;
2831 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2833 uinfo->value.enumerated.items = out_jacks + in_jacks;
2834 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2835 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2836 idx = uinfo->value.enumerated.item;
2837 if (idx < out_jacks) {
2839 text = out_jack_texts[idx];
2841 text = "Headphone Out";
2845 unsigned int vref_caps = get_vref_caps(codec, nid);
2846 text = vref_texts[get_vref_idx(vref_caps, idx)];
2851 strcpy(uinfo->value.enumerated.name, text);
2855 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2857 int out_jacks = get_out_jack_num_items(codec, nid);
2858 int in_jacks = get_in_jack_num_items(codec, nid);
2859 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2862 if (val & PIN_OUT) {
2863 if (out_jacks > 1 && val == PIN_HP)
2865 } else if (val & PIN_IN) {
2868 unsigned int vref_caps = get_vref_caps(codec, nid);
2869 val &= AC_PINCTL_VREFEN;
2870 idx += cvt_from_vref_idx(vref_caps, val);
2876 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2877 struct snd_ctl_elem_value *ucontrol)
2879 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2880 hda_nid_t nid = kcontrol->private_value;
2881 ucontrol->value.enumerated.item[0] =
2882 get_cur_hp_mic_jack_mode(codec, nid);
2886 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2887 struct snd_ctl_elem_value *ucontrol)
2889 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2890 hda_nid_t nid = kcontrol->private_value;
2891 int out_jacks = get_out_jack_num_items(codec, nid);
2892 int in_jacks = get_in_jack_num_items(codec, nid);
2893 unsigned int val, oldval, idx;
2895 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2896 idx = ucontrol->value.enumerated.item[0];
2900 if (idx < out_jacks) {
2902 val = idx ? PIN_HP : PIN_OUT;
2908 unsigned int vref_caps = get_vref_caps(codec, nid);
2909 val = snd_hda_codec_get_pin_target(codec, nid);
2910 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2911 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
2913 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
2915 snd_hda_set_pin_ctl_cache(codec, nid, val);
2916 call_hp_automute(codec, NULL);
2921 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2922 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2923 .info = hp_mic_jack_mode_info,
2924 .get = hp_mic_jack_mode_get,
2925 .put = hp_mic_jack_mode_put,
2928 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2930 struct hda_gen_spec *spec = codec->spec;
2931 struct snd_kcontrol_new *knew;
2933 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2934 &hp_mic_jack_mode_enum);
2937 knew->private_value = pin;
2938 spec->hp_mic_jack_modes = 1;
2946 /* add the powersave loopback-list entry */
2947 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2949 struct hda_amp_list *list;
2951 list = snd_array_new(&spec->loopback_list);
2955 list->dir = HDA_INPUT;
2957 spec->loopback.amplist = spec->loopback_list.list;
2961 /* return true if either a volume or a mute amp is found for the given
2962 * aamix path; the amp has to be either in the mixer node or its direct leaf
2964 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2965 hda_nid_t pin, unsigned int *mix_val,
2966 unsigned int *mute_val)
2969 const hda_nid_t *list;
2972 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2976 *mix_val = *mute_val = 0;
2977 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2978 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2979 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2980 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2981 if (*mix_val && *mute_val)
2984 /* check leaf node */
2985 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2986 if (num_conns < idx)
2989 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2990 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
2991 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2992 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2993 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
2994 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2996 return *mix_val || *mute_val;
2999 /* create input playback/capture controls for the given pin */
3000 static int new_analog_input(struct hda_codec *codec, int input_idx,
3001 hda_nid_t pin, const char *ctlname, int ctlidx,
3004 struct hda_gen_spec *spec = codec->spec;
3005 struct nid_path *path;
3006 unsigned int mix_val, mute_val;
3009 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3012 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
3015 print_nid_path(codec, "loopback", path);
3016 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
3018 idx = path->idx[path->depth - 1];
3020 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
3023 path->ctls[NID_PATH_VOL_CTL] = mix_val;
3027 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
3030 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
3033 path->active = true;
3034 path->stream_enabled = true; /* no DAC/ADC involved */
3035 err = add_loopback_list(spec, mix_nid, idx);
3039 if (spec->mixer_nid != spec->mixer_merge_nid &&
3040 !spec->loopback_merge_path) {
3041 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3042 spec->mixer_merge_nid, 0);
3044 print_nid_path(codec, "loopback-merge", path);
3045 path->active = true;
3046 path->pin_fixed = true; /* static route */
3047 path->stream_enabled = true; /* no DAC/ADC involved */
3048 spec->loopback_merge_path =
3049 snd_hda_get_path_idx(codec, path);
3056 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3058 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3059 return (pincap & AC_PINCAP_IN) != 0;
3062 /* Parse the codec tree and retrieve ADCs */
3063 static int fill_adc_nids(struct hda_codec *codec)
3065 struct hda_gen_spec *spec = codec->spec;
3067 hda_nid_t *adc_nids = spec->adc_nids;
3068 int max_nums = ARRAY_SIZE(spec->adc_nids);
3071 for_each_hda_codec_node(nid, codec) {
3072 unsigned int caps = get_wcaps(codec, nid);
3073 int type = get_wcaps_type(caps);
3075 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3077 adc_nids[nums] = nid;
3078 if (++nums >= max_nums)
3081 spec->num_adc_nids = nums;
3083 /* copy the detected ADCs to all_adcs[] */
3084 spec->num_all_adcs = nums;
3085 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3090 /* filter out invalid adc_nids that don't give all active input pins;
3091 * if needed, check whether dynamic ADC-switching is available
3093 static int check_dyn_adc_switch(struct hda_codec *codec)
3095 struct hda_gen_spec *spec = codec->spec;
3096 struct hda_input_mux *imux = &spec->input_mux;
3097 unsigned int ok_bits;
3102 for (n = 0; n < spec->num_adc_nids; n++) {
3103 for (i = 0; i < imux->num_items; i++) {
3104 if (!spec->input_paths[i][n])
3107 if (i >= imux->num_items) {
3108 ok_bits |= (1 << n);
3114 /* check whether ADC-switch is possible */
3115 for (i = 0; i < imux->num_items; i++) {
3116 for (n = 0; n < spec->num_adc_nids; n++) {
3117 if (spec->input_paths[i][n]) {
3118 spec->dyn_adc_idx[i] = n;
3124 codec_dbg(codec, "enabling ADC switching\n");
3125 spec->dyn_adc_switch = 1;
3126 } else if (nums != spec->num_adc_nids) {
3127 /* shrink the invalid adcs and input paths */
3129 for (n = 0; n < spec->num_adc_nids; n++) {
3130 if (!(ok_bits & (1 << n)))
3133 spec->adc_nids[nums] = spec->adc_nids[n];
3134 for (i = 0; i < imux->num_items; i++) {
3135 invalidate_nid_path(codec,
3136 spec->input_paths[i][nums]);
3137 spec->input_paths[i][nums] =
3138 spec->input_paths[i][n];
3143 spec->num_adc_nids = nums;
3146 if (imux->num_items == 1 ||
3147 (imux->num_items == 2 && spec->hp_mic)) {
3148 codec_dbg(codec, "reducing to a single ADC\n");
3149 spec->num_adc_nids = 1; /* reduce to a single ADC */
3152 /* single index for individual volumes ctls */
3153 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3154 spec->num_adc_nids = 1;
3159 /* parse capture source paths from the given pin and create imux items */
3160 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3161 int cfg_idx, int num_adcs,
3162 const char *label, int anchor)
3164 struct hda_gen_spec *spec = codec->spec;
3165 struct hda_input_mux *imux = &spec->input_mux;
3166 int imux_idx = imux->num_items;
3167 bool imux_added = false;
3170 for (c = 0; c < num_adcs; c++) {
3171 struct nid_path *path;
3172 hda_nid_t adc = spec->adc_nids[c];
3174 if (!is_reachable_path(codec, pin, adc))
3176 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3179 print_nid_path(codec, "input", path);
3180 spec->input_paths[imux_idx][c] =
3181 snd_hda_get_path_idx(codec, path);
3184 if (spec->hp_mic_pin == pin)
3185 spec->hp_mic_mux_idx = imux->num_items;
3186 spec->imux_pins[imux->num_items] = pin;
3187 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3189 if (spec->dyn_adc_switch)
3190 spec->dyn_adc_idx[imux_idx] = c;
3198 * create playback/capture controls for input pins
3201 /* fill the label for each input at first */
3202 static int fill_input_pin_labels(struct hda_codec *codec)
3204 struct hda_gen_spec *spec = codec->spec;
3205 const struct auto_pin_cfg *cfg = &spec->autocfg;
3208 for (i = 0; i < cfg->num_inputs; i++) {
3209 hda_nid_t pin = cfg->inputs[i].pin;
3213 if (!is_input_pin(codec, pin))
3216 label = hda_get_autocfg_input_label(codec, cfg, i);
3218 for (j = i - 1; j >= 0; j--) {
3219 if (spec->input_labels[j] &&
3220 !strcmp(spec->input_labels[j], label)) {
3221 idx = spec->input_label_idxs[j] + 1;
3226 spec->input_labels[i] = label;
3227 spec->input_label_idxs[i] = idx;
3233 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3235 static int create_input_ctls(struct hda_codec *codec)
3237 struct hda_gen_spec *spec = codec->spec;
3238 const struct auto_pin_cfg *cfg = &spec->autocfg;
3239 hda_nid_t mixer = spec->mixer_nid;
3244 num_adcs = fill_adc_nids(codec);
3248 err = fill_input_pin_labels(codec);
3252 for (i = 0; i < cfg->num_inputs; i++) {
3255 pin = cfg->inputs[i].pin;
3256 if (!is_input_pin(codec, pin))
3260 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3261 val |= snd_hda_get_default_vref(codec, pin);
3262 if (pin != spec->hp_mic_pin)
3263 set_pin_target(codec, pin, val, false);
3266 if (is_reachable_path(codec, pin, mixer)) {
3267 err = new_analog_input(codec, i, pin,
3268 spec->input_labels[i],
3269 spec->input_label_idxs[i],
3276 err = parse_capture_source(codec, pin, i, num_adcs,
3277 spec->input_labels[i], -mixer);
3281 if (spec->add_jack_modes) {
3282 err = create_in_jack_mode(codec, pin);
3288 /* add stereo mix when explicitly enabled via hint */
3289 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3290 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3295 spec->suppress_auto_mic = 1;
3306 /* get the input path specified by the given adc and imux indices */
3307 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3309 struct hda_gen_spec *spec = codec->spec;
3310 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3314 if (spec->dyn_adc_switch)
3315 adc_idx = spec->dyn_adc_idx[imux_idx];
3316 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3320 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3323 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3326 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3327 struct snd_ctl_elem_info *uinfo)
3329 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3330 struct hda_gen_spec *spec = codec->spec;
3331 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3334 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3335 struct snd_ctl_elem_value *ucontrol)
3337 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3338 struct hda_gen_spec *spec = codec->spec;
3339 /* the ctls are created at once with multiple counts */
3340 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3342 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3346 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3347 struct snd_ctl_elem_value *ucontrol)
3349 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3350 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3351 return mux_select(codec, adc_idx,
3352 ucontrol->value.enumerated.item[0]);
3355 static const struct snd_kcontrol_new cap_src_temp = {
3356 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3357 .name = "Input Source",
3358 .info = mux_enum_info,
3359 .get = mux_enum_get,
3360 .put = mux_enum_put,
3364 * capture volume and capture switch ctls
3367 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3368 struct snd_ctl_elem_value *ucontrol);
3370 /* call the given amp update function for all amps in the imux list at once */
3371 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3372 struct snd_ctl_elem_value *ucontrol,
3373 put_call_t func, int type)
3375 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3376 struct hda_gen_spec *spec = codec->spec;
3377 const struct hda_input_mux *imux;
3378 struct nid_path *path;
3379 int i, adc_idx, err = 0;
3381 imux = &spec->input_mux;
3382 adc_idx = kcontrol->id.index;
3383 mutex_lock(&codec->control_mutex);
3384 for (i = 0; i < imux->num_items; i++) {
3385 path = get_input_path(codec, adc_idx, i);
3386 if (!path || !path->ctls[type])
3388 kcontrol->private_value = path->ctls[type];
3389 err = func(kcontrol, ucontrol);
3393 mutex_unlock(&codec->control_mutex);
3394 if (err >= 0 && spec->cap_sync_hook)
3395 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3399 /* capture volume ctl callbacks */
3400 #define cap_vol_info snd_hda_mixer_amp_volume_info
3401 #define cap_vol_get snd_hda_mixer_amp_volume_get
3402 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3404 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3405 struct snd_ctl_elem_value *ucontrol)
3407 return cap_put_caller(kcontrol, ucontrol,
3408 snd_hda_mixer_amp_volume_put,
3412 static const struct snd_kcontrol_new cap_vol_temp = {
3413 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3414 .name = "Capture Volume",
3415 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3416 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3417 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3418 .info = cap_vol_info,
3421 .tlv = { .c = cap_vol_tlv },
3424 /* capture switch ctl callbacks */
3425 #define cap_sw_info snd_ctl_boolean_stereo_info
3426 #define cap_sw_get snd_hda_mixer_amp_switch_get
3428 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3429 struct snd_ctl_elem_value *ucontrol)
3431 return cap_put_caller(kcontrol, ucontrol,
3432 snd_hda_mixer_amp_switch_put,
3436 static const struct snd_kcontrol_new cap_sw_temp = {
3437 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3438 .name = "Capture Switch",
3439 .info = cap_sw_info,
3444 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3449 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3450 for (depth = 0; depth < 3; depth++) {
3451 if (depth >= path->depth)
3453 i = path->depth - depth - 1;
3454 nid = path->path[i];
3455 if (!path->ctls[NID_PATH_VOL_CTL]) {
3456 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3457 path->ctls[NID_PATH_VOL_CTL] =
3458 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3459 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3460 int idx = path->idx[i];
3461 if (!depth && codec->single_adc_amp)
3463 path->ctls[NID_PATH_VOL_CTL] =
3464 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3467 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3468 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3469 path->ctls[NID_PATH_MUTE_CTL] =
3470 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3471 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3472 int idx = path->idx[i];
3473 if (!depth && codec->single_adc_amp)
3475 path->ctls[NID_PATH_MUTE_CTL] =
3476 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3483 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3485 struct hda_gen_spec *spec = codec->spec;
3486 struct auto_pin_cfg *cfg = &spec->autocfg;
3490 if (!spec->inv_dmic_split)
3492 for (i = 0; i < cfg->num_inputs; i++) {
3493 if (cfg->inputs[i].pin != nid)
3495 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3497 val = snd_hda_codec_get_pincfg(codec, nid);
3498 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3503 /* capture switch put callback for a single control with hook call */
3504 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3505 struct snd_ctl_elem_value *ucontrol)
3507 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3508 struct hda_gen_spec *spec = codec->spec;
3511 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3515 if (spec->cap_sync_hook)
3516 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3521 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3522 int idx, bool is_switch, unsigned int ctl,
3525 struct hda_gen_spec *spec = codec->spec;
3526 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3527 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3528 const char *sfx = is_switch ? "Switch" : "Volume";
3529 unsigned int chs = inv_dmic ? 1 : 3;
3530 struct snd_kcontrol_new *knew;
3536 snprintf(tmpname, sizeof(tmpname),
3537 "%s Capture %s", label, sfx);
3539 snprintf(tmpname, sizeof(tmpname),
3541 knew = add_control(spec, type, tmpname, idx,
3542 amp_val_replace_channels(ctl, chs));
3546 knew->put = cap_single_sw_put;
3550 /* Make independent right kcontrol */
3552 snprintf(tmpname, sizeof(tmpname),
3553 "Inverted %s Capture %s", label, sfx);
3555 snprintf(tmpname, sizeof(tmpname),
3556 "Inverted Capture %s", sfx);
3557 knew = add_control(spec, type, tmpname, idx,
3558 amp_val_replace_channels(ctl, 2));
3562 knew->put = cap_single_sw_put;
3566 /* create single (and simple) capture volume and switch controls */
3567 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3568 unsigned int vol_ctl, unsigned int sw_ctl,
3572 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3575 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3581 /* create bound capture volume and switch controls */
3582 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3583 unsigned int vol_ctl, unsigned int sw_ctl)
3585 struct hda_gen_spec *spec = codec->spec;
3586 struct snd_kcontrol_new *knew;
3589 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3593 knew->private_value = vol_ctl;
3594 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3597 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3601 knew->private_value = sw_ctl;
3602 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3607 /* return the vol ctl when used first in the imux list */
3608 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3610 struct nid_path *path;
3614 path = get_input_path(codec, 0, idx);
3617 ctl = path->ctls[type];
3620 for (i = 0; i < idx - 1; i++) {
3621 path = get_input_path(codec, 0, i);
3622 if (path && path->ctls[type] == ctl)
3628 /* create individual capture volume and switch controls per input */
3629 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3631 struct hda_gen_spec *spec = codec->spec;
3632 struct hda_input_mux *imux = &spec->input_mux;
3635 for (i = 0; i < imux->num_items; i++) {
3639 idx = imux->items[i].index;
3640 if (idx >= spec->autocfg.num_inputs)
3642 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3644 for (type = 0; type < 2; type++) {
3645 err = add_single_cap_ctl(codec,
3646 spec->input_labels[idx],
3647 spec->input_label_idxs[idx],
3649 get_first_cap_ctl(codec, i, type),
3658 static int create_capture_mixers(struct hda_codec *codec)
3660 struct hda_gen_spec *spec = codec->spec;
3661 struct hda_input_mux *imux = &spec->input_mux;
3662 int i, n, nums, err;
3664 if (spec->dyn_adc_switch)
3667 nums = spec->num_adc_nids;
3669 if (!spec->auto_mic && imux->num_items > 1) {
3670 struct snd_kcontrol_new *knew;
3672 name = nums > 1 ? "Input Source" : "Capture Source";
3673 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3679 for (n = 0; n < nums; n++) {
3681 bool multi_cap_vol = spec->multi_cap_vol;
3682 bool inv_dmic = false;
3686 for (i = 0; i < imux->num_items; i++) {
3687 struct nid_path *path;
3688 path = get_input_path(codec, n, i);
3691 parse_capvol_in_path(codec, path);
3693 vol = path->ctls[NID_PATH_VOL_CTL];
3694 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3696 if (!same_amp_caps(codec, vol,
3697 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3698 multi_cap_vol = true;
3701 sw = path->ctls[NID_PATH_MUTE_CTL];
3702 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3704 if (!same_amp_caps(codec, sw,
3705 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3706 multi_cap_vol = true;
3708 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3713 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3715 else if (!multi_cap_vol && !inv_dmic)
3716 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3718 err = create_multi_cap_vol_ctl(codec);
3727 * add mic boosts if needed
3730 /* check whether the given amp is feasible as a boost volume */
3731 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3736 if (!nid_has_volume(codec, nid, dir) ||
3737 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3738 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3741 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3742 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3748 /* look for a boost amp in a widget close to the pin */
3749 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3750 struct nid_path *path)
3752 unsigned int val = 0;
3756 for (depth = 0; depth < 3; depth++) {
3757 if (depth >= path->depth - 1)
3759 nid = path->path[depth];
3760 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3761 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3763 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3764 path->idx[depth])) {
3765 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3774 static int parse_mic_boost(struct hda_codec *codec)
3776 struct hda_gen_spec *spec = codec->spec;
3777 struct auto_pin_cfg *cfg = &spec->autocfg;
3778 struct hda_input_mux *imux = &spec->input_mux;
3781 if (!spec->num_adc_nids)
3784 for (i = 0; i < imux->num_items; i++) {
3785 struct nid_path *path;
3788 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3790 idx = imux->items[i].index;
3791 if (idx >= imux->num_items)
3794 /* check only line-in and mic pins */
3795 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3798 path = get_input_path(codec, 0, i);
3802 val = look_for_boost_amp(codec, path);
3806 /* create a boost control */
3807 snprintf(boost_label, sizeof(boost_label),
3808 "%s Boost Volume", spec->input_labels[idx]);
3809 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3810 spec->input_label_idxs[idx], val))
3813 path->ctls[NID_PATH_BOOST_CTL] = val;
3819 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3821 static void parse_digital(struct hda_codec *codec)
3823 struct hda_gen_spec *spec = codec->spec;
3824 struct nid_path *path;
3826 hda_nid_t dig_nid, pin;
3828 /* support multiple SPDIFs; the secondary is set up as a slave */
3830 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3831 pin = spec->autocfg.dig_out_pins[i];
3832 dig_nid = look_for_dac(codec, pin, true);
3835 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3838 print_nid_path(codec, "digout", path);
3839 path->active = true;
3840 path->pin_fixed = true; /* no jack detection */
3841 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3842 set_pin_target(codec, pin, PIN_OUT, false);
3844 spec->multiout.dig_out_nid = dig_nid;
3845 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3847 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3848 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3850 spec->slave_dig_outs[nums - 1] = dig_nid;
3855 if (spec->autocfg.dig_in_pin) {
3856 pin = spec->autocfg.dig_in_pin;
3857 for_each_hda_codec_node(dig_nid, codec) {
3858 unsigned int wcaps = get_wcaps(codec, dig_nid);
3859 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3861 if (!(wcaps & AC_WCAP_DIGITAL))
3863 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3865 print_nid_path(codec, "digin", path);
3866 path->active = true;
3867 path->pin_fixed = true; /* no jack */
3868 spec->dig_in_nid = dig_nid;
3869 spec->digin_path = snd_hda_get_path_idx(codec, path);
3870 set_pin_target(codec, pin, PIN_IN, false);
3879 * input MUX handling
3882 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3884 /* select the given imux item; either unmute exclusively or select the route */
3885 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3888 struct hda_gen_spec *spec = codec->spec;
3889 const struct hda_input_mux *imux;
3890 struct nid_path *old_path, *path;
3892 imux = &spec->input_mux;
3893 if (!imux->num_items)
3896 if (idx >= imux->num_items)
3897 idx = imux->num_items - 1;
3898 if (spec->cur_mux[adc_idx] == idx)
3901 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3904 if (old_path->active)
3905 snd_hda_activate_path(codec, old_path, false, false);
3907 spec->cur_mux[adc_idx] = idx;
3910 update_hp_mic(codec, adc_idx, false);
3912 if (spec->dyn_adc_switch)
3913 dyn_adc_pcm_resetup(codec, idx);
3915 path = get_input_path(codec, adc_idx, idx);
3920 snd_hda_activate_path(codec, path, true, false);
3921 if (spec->cap_sync_hook)
3922 spec->cap_sync_hook(codec, NULL, NULL);
3923 path_power_down_sync(codec, old_path);
3927 /* power up/down widgets in the all paths that match with the given NID
3928 * as terminals (either start- or endpoint)
3930 * returns the last changed NID, or zero if unchanged.
3932 static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
3933 int pin_state, int stream_state)
3935 struct hda_gen_spec *spec = codec->spec;
3936 hda_nid_t last, changed = 0;
3937 struct nid_path *path;
3940 for (n = 0; n < spec->paths.used; n++) {
3941 path = snd_array_elem(&spec->paths, n);
3942 if (path->path[0] == nid ||
3943 path->path[path->depth - 1] == nid) {
3944 bool pin_old = path->pin_enabled;
3945 bool stream_old = path->stream_enabled;
3948 path->pin_enabled = pin_state;
3949 if (stream_state >= 0)
3950 path->stream_enabled = stream_state;
3951 if ((!path->pin_fixed && path->pin_enabled != pin_old)
3952 || path->stream_enabled != stream_old) {
3953 last = path_power_update(codec, path, true);
3962 /* check the jack status for power control */
3963 static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
3965 if (!is_jack_detectable(codec, pin))
3967 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
3970 /* power up/down the paths of the given pin according to the jack state;
3971 * power = 0/1 : only power up/down if it matches with the jack state,
3972 * < 0 : force power up/down to follow the jack sate
3974 * returns the last changed NID, or zero if unchanged.
3976 static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
3981 if (!codec->power_save_node)
3984 on = detect_pin_state(codec, pin);
3986 if (power >= 0 && on != power)
3988 return set_path_power(codec, pin, on, -1);
3991 static void pin_power_callback(struct hda_codec *codec,
3992 struct hda_jack_callback *jack,
3995 if (jack && jack->tbl->nid)
3996 sync_power_state_change(codec,
3997 set_pin_power_jack(codec, jack->tbl->nid, on));
4000 /* callback only doing power up -- called at first */
4001 static void pin_power_up_callback(struct hda_codec *codec,
4002 struct hda_jack_callback *jack)
4004 pin_power_callback(codec, jack, true);
4007 /* callback only doing power down -- called at last */
4008 static void pin_power_down_callback(struct hda_codec *codec,
4009 struct hda_jack_callback *jack)
4011 pin_power_callback(codec, jack, false);
4014 /* set up the power up/down callbacks */
4015 static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4016 const hda_nid_t *pins, bool on)
4019 hda_jack_callback_fn cb =
4020 on ? pin_power_up_callback : pin_power_down_callback;
4022 for (i = 0; i < num_pins && pins[i]; i++) {
4023 if (is_jack_detectable(codec, pins[i]))
4024 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4026 set_path_power(codec, pins[i], true, -1);
4030 /* enabled power callback to each available I/O pin with jack detections;
4031 * the digital I/O pins are excluded because of the unreliable detectsion
4033 static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4035 struct hda_gen_spec *spec = codec->spec;
4036 struct auto_pin_cfg *cfg = &spec->autocfg;
4039 if (!codec->power_save_node)
4041 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4042 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4043 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4044 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4045 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4046 for (i = 0; i < cfg->num_inputs; i++)
4047 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4050 /* sync path power up/down with the jack states of given pins */
4051 static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4052 const hda_nid_t *pins)
4056 for (i = 0; i < num_pins && pins[i]; i++)
4057 if (is_jack_detectable(codec, pins[i]))
4058 set_pin_power_jack(codec, pins[i], -1);
4061 /* sync path power up/down with pins; called at init and resume */
4062 static void sync_all_pin_power_ctls(struct hda_codec *codec)
4064 struct hda_gen_spec *spec = codec->spec;
4065 struct auto_pin_cfg *cfg = &spec->autocfg;
4068 if (!codec->power_save_node)
4070 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4071 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4072 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4073 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4074 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4075 for (i = 0; i < cfg->num_inputs; i++)
4076 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4079 /* add fake paths if not present yet */
4080 static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4081 int num_pins, const hda_nid_t *pins)
4083 struct hda_gen_spec *spec = codec->spec;
4084 struct nid_path *path;
4087 for (i = 0; i < num_pins; i++) {
4090 if (get_nid_path(codec, nid, pins[i], 0))
4092 path = snd_array_new(&spec->paths);
4095 memset(path, 0, sizeof(*path));
4097 path->path[0] = nid;
4098 path->path[1] = pins[i];
4099 path->active = true;
4104 /* create fake paths to all outputs from beep */
4105 static int add_fake_beep_paths(struct hda_codec *codec)
4107 struct hda_gen_spec *spec = codec->spec;
4108 struct auto_pin_cfg *cfg = &spec->autocfg;
4109 hda_nid_t nid = spec->beep_nid;
4112 if (!codec->power_save_node || !nid)
4114 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4117 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4118 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4122 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4123 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4131 /* power up/down beep widget and its output paths */
4132 static void beep_power_hook(struct hda_beep *beep, bool on)
4134 set_path_power(beep->codec, beep->nid, -1, on);
4138 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4139 * @codec: the HDA codec
4140 * @pin: NID of pin to fix
4142 int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4144 struct hda_gen_spec *spec = codec->spec;
4145 struct nid_path *path;
4147 path = snd_array_new(&spec->paths);
4150 memset(path, 0, sizeof(*path));
4152 path->path[0] = pin;
4153 path->active = true;
4154 path->pin_fixed = true;
4155 path->stream_enabled = true;
4158 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4161 * Jack detections for HP auto-mute and mic-switch
4164 /* check each pin in the given array; returns true if any of them is plugged */
4165 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4168 bool present = false;
4170 for (i = 0; i < num_pins; i++) {
4171 hda_nid_t nid = pins[i];
4174 /* don't detect pins retasked as inputs */
4175 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4177 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4183 /* standard HP/line-out auto-mute helper */
4184 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
4185 int *paths, bool mute)
4187 struct hda_gen_spec *spec = codec->spec;
4190 for (i = 0; i < num_pins; i++) {
4191 hda_nid_t nid = pins[i];
4192 unsigned int val, oldval;
4196 oldval = snd_hda_codec_get_pin_target(codec, nid);
4197 if (oldval & PIN_IN)
4198 continue; /* no mute for inputs */
4200 if (spec->auto_mute_via_amp) {
4201 struct nid_path *path;
4204 path = snd_hda_get_path_from_idx(codec, paths[i]);
4207 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4211 spec->mute_bits |= (1ULL << mute_nid);
4213 spec->mute_bits &= ~(1ULL << mute_nid);
4216 /* don't reset VREF value in case it's controlling
4217 * the amp (see alc861_fixup_asus_amp_vref_0f())
4219 if (spec->keep_vref_in_automute)
4220 val = oldval & ~PIN_HP;
4225 /* here we call update_pin_ctl() so that the pinctl is
4226 * changed without changing the pinctl target value;
4227 * the original target value will be still referred at
4228 * the init / resume again
4230 update_pin_ctl(codec, nid, val);
4233 set_pin_eapd(codec, nid, !mute);
4234 if (codec->power_save_node) {
4237 on = detect_pin_state(codec, nid);
4238 set_path_power(codec, nid, on, -1);
4244 * snd_hda_gen_update_outputs - Toggle outputs muting
4245 * @codec: the HDA codec
4247 * Update the mute status of all outputs based on the current jack states.
4249 void snd_hda_gen_update_outputs(struct hda_codec *codec)
4251 struct hda_gen_spec *spec = codec->spec;
4255 /* Control HP pins/amps depending on master_mute state;
4256 * in general, HP pins/amps control should be enabled in all cases,
4257 * but currently set only for master_mute, just to be safe
4259 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4260 paths = spec->out_paths;
4262 paths = spec->hp_paths;
4263 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
4264 spec->autocfg.hp_pins, paths, spec->master_mute);
4266 if (!spec->automute_speaker)
4269 on = spec->hp_jack_present | spec->line_jack_present;
4270 on |= spec->master_mute;
4271 spec->speaker_muted = on;
4272 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4273 paths = spec->out_paths;
4275 paths = spec->speaker_paths;
4276 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
4277 spec->autocfg.speaker_pins, paths, on);
4279 /* toggle line-out mutes if needed, too */
4280 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4281 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4282 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4284 if (!spec->automute_lo)
4287 on = spec->hp_jack_present;
4288 on |= spec->master_mute;
4289 spec->line_out_muted = on;
4290 paths = spec->out_paths;
4291 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4292 spec->autocfg.line_out_pins, paths, on);
4294 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4296 static void call_update_outputs(struct hda_codec *codec)
4298 struct hda_gen_spec *spec = codec->spec;
4299 if (spec->automute_hook)
4300 spec->automute_hook(codec);
4302 snd_hda_gen_update_outputs(codec);
4304 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4305 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4306 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4310 * snd_hda_gen_hp_automute - standard HP-automute helper
4311 * @codec: the HDA codec
4312 * @jack: jack object, NULL for the whole
4314 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4315 struct hda_jack_callback *jack)
4317 struct hda_gen_spec *spec = codec->spec;
4318 hda_nid_t *pins = spec->autocfg.hp_pins;
4319 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4321 /* No detection for the first HP jack during indep-HP mode */
4322 if (spec->indep_hp_enabled) {
4327 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4328 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4330 call_update_outputs(codec);
4332 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4335 * snd_hda_gen_line_automute - standard line-out-automute helper
4336 * @codec: the HDA codec
4337 * @jack: jack object, NULL for the whole
4339 void snd_hda_gen_line_automute(struct hda_codec *codec,
4340 struct hda_jack_callback *jack)
4342 struct hda_gen_spec *spec = codec->spec;
4344 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4346 /* check LO jack only when it's different from HP */
4347 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4350 spec->line_jack_present =
4351 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4352 spec->autocfg.line_out_pins);
4353 if (!spec->automute_speaker || !spec->detect_lo)
4355 call_update_outputs(codec);
4357 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4360 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4361 * @codec: the HDA codec
4362 * @jack: jack object, NULL for the whole
4364 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4365 struct hda_jack_callback *jack)
4367 struct hda_gen_spec *spec = codec->spec;
4370 if (!spec->auto_mic)
4373 for (i = spec->am_num_entries - 1; i > 0; i--) {
4374 hda_nid_t pin = spec->am_entry[i].pin;
4375 /* don't detect pins retasked as outputs */
4376 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4378 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4379 mux_select(codec, 0, spec->am_entry[i].idx);
4383 mux_select(codec, 0, spec->am_entry[0].idx);
4385 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4387 /* call appropriate hooks */
4388 static void call_hp_automute(struct hda_codec *codec,
4389 struct hda_jack_callback *jack)
4391 struct hda_gen_spec *spec = codec->spec;
4392 if (spec->hp_automute_hook)
4393 spec->hp_automute_hook(codec, jack);
4395 snd_hda_gen_hp_automute(codec, jack);
4398 static void call_line_automute(struct hda_codec *codec,
4399 struct hda_jack_callback *jack)
4401 struct hda_gen_spec *spec = codec->spec;
4402 if (spec->line_automute_hook)
4403 spec->line_automute_hook(codec, jack);
4405 snd_hda_gen_line_automute(codec, jack);
4408 static void call_mic_autoswitch(struct hda_codec *codec,
4409 struct hda_jack_callback *jack)
4411 struct hda_gen_spec *spec = codec->spec;
4412 if (spec->mic_autoswitch_hook)
4413 spec->mic_autoswitch_hook(codec, jack);
4415 snd_hda_gen_mic_autoswitch(codec, jack);
4418 /* update jack retasking */
4419 static void update_automute_all(struct hda_codec *codec)
4421 call_hp_automute(codec, NULL);
4422 call_line_automute(codec, NULL);
4423 call_mic_autoswitch(codec, NULL);
4427 * Auto-Mute mode mixer enum support
4429 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4430 struct snd_ctl_elem_info *uinfo)
4432 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4433 struct hda_gen_spec *spec = codec->spec;
4434 static const char * const texts3[] = {
4435 "Disabled", "Speaker Only", "Line Out+Speaker"
4438 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4439 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4440 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4443 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4444 struct snd_ctl_elem_value *ucontrol)
4446 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4447 struct hda_gen_spec *spec = codec->spec;
4448 unsigned int val = 0;
4449 if (spec->automute_speaker)
4451 if (spec->automute_lo)
4454 ucontrol->value.enumerated.item[0] = val;
4458 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4459 struct snd_ctl_elem_value *ucontrol)
4461 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4462 struct hda_gen_spec *spec = codec->spec;
4464 switch (ucontrol->value.enumerated.item[0]) {
4466 if (!spec->automute_speaker && !spec->automute_lo)
4468 spec->automute_speaker = 0;
4469 spec->automute_lo = 0;
4472 if (spec->automute_speaker_possible) {
4473 if (!spec->automute_lo && spec->automute_speaker)
4475 spec->automute_speaker = 1;
4476 spec->automute_lo = 0;
4477 } else if (spec->automute_lo_possible) {
4478 if (spec->automute_lo)
4480 spec->automute_lo = 1;
4485 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4487 if (spec->automute_speaker && spec->automute_lo)
4489 spec->automute_speaker = 1;
4490 spec->automute_lo = 1;
4495 call_update_outputs(codec);
4499 static const struct snd_kcontrol_new automute_mode_enum = {
4500 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4501 .name = "Auto-Mute Mode",
4502 .info = automute_mode_info,
4503 .get = automute_mode_get,
4504 .put = automute_mode_put,
4507 static int add_automute_mode_enum(struct hda_codec *codec)
4509 struct hda_gen_spec *spec = codec->spec;
4511 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4517 * Check the availability of HP/line-out auto-mute;
4518 * Set up appropriately if really supported
4520 static int check_auto_mute_availability(struct hda_codec *codec)
4522 struct hda_gen_spec *spec = codec->spec;
4523 struct auto_pin_cfg *cfg = &spec->autocfg;
4527 if (spec->suppress_auto_mute)
4530 if (cfg->hp_pins[0])
4532 if (cfg->line_out_pins[0])
4534 if (cfg->speaker_pins[0])
4536 if (present < 2) /* need two different output types */
4539 if (!cfg->speaker_pins[0] &&
4540 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4541 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4542 sizeof(cfg->speaker_pins));
4543 cfg->speaker_outs = cfg->line_outs;
4546 if (!cfg->hp_pins[0] &&
4547 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4548 memcpy(cfg->hp_pins, cfg->line_out_pins,
4549 sizeof(cfg->hp_pins));
4550 cfg->hp_outs = cfg->line_outs;
4553 for (i = 0; i < cfg->hp_outs; i++) {
4554 hda_nid_t nid = cfg->hp_pins[i];
4555 if (!is_jack_detectable(codec, nid))
4557 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4558 snd_hda_jack_detect_enable_callback(codec, nid,
4560 spec->detect_hp = 1;
4563 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4564 if (cfg->speaker_outs)
4565 for (i = 0; i < cfg->line_outs; i++) {
4566 hda_nid_t nid = cfg->line_out_pins[i];
4567 if (!is_jack_detectable(codec, nid))
4569 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4570 snd_hda_jack_detect_enable_callback(codec, nid,
4571 call_line_automute);
4572 spec->detect_lo = 1;
4574 spec->automute_lo_possible = spec->detect_hp;
4577 spec->automute_speaker_possible = cfg->speaker_outs &&
4578 (spec->detect_hp || spec->detect_lo);
4580 spec->automute_lo = spec->automute_lo_possible;
4581 spec->automute_speaker = spec->automute_speaker_possible;
4583 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4584 /* create a control for automute mode */
4585 err = add_automute_mode_enum(codec);
4592 /* check whether all auto-mic pins are valid; setup indices if OK */
4593 static bool auto_mic_check_imux(struct hda_codec *codec)
4595 struct hda_gen_spec *spec = codec->spec;
4596 const struct hda_input_mux *imux;
4599 imux = &spec->input_mux;
4600 for (i = 0; i < spec->am_num_entries; i++) {
4601 spec->am_entry[i].idx =
4602 find_idx_in_nid_list(spec->am_entry[i].pin,
4603 spec->imux_pins, imux->num_items);
4604 if (spec->am_entry[i].idx < 0)
4605 return false; /* no corresponding imux */
4608 /* we don't need the jack detection for the first pin */
4609 for (i = 1; i < spec->am_num_entries; i++)
4610 snd_hda_jack_detect_enable_callback(codec,
4611 spec->am_entry[i].pin,
4612 call_mic_autoswitch);
4616 static int compare_attr(const void *ap, const void *bp)
4618 const struct automic_entry *a = ap;
4619 const struct automic_entry *b = bp;
4620 return (int)(a->attr - b->attr);
4624 * Check the availability of auto-mic switch;
4625 * Set up if really supported
4627 static int check_auto_mic_availability(struct hda_codec *codec)
4629 struct hda_gen_spec *spec = codec->spec;
4630 struct auto_pin_cfg *cfg = &spec->autocfg;
4634 if (spec->suppress_auto_mic)
4639 for (i = 0; i < cfg->num_inputs; i++) {
4640 hda_nid_t nid = cfg->inputs[i].pin;
4642 attr = snd_hda_codec_get_pincfg(codec, nid);
4643 attr = snd_hda_get_input_pin_attr(attr);
4644 if (types & (1 << attr))
4645 return 0; /* already occupied */
4647 case INPUT_PIN_ATTR_INT:
4648 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4649 return 0; /* invalid type */
4651 case INPUT_PIN_ATTR_UNUSED:
4652 return 0; /* invalid entry */
4654 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4655 return 0; /* invalid type */
4656 if (!spec->line_in_auto_switch &&
4657 cfg->inputs[i].type != AUTO_PIN_MIC)
4658 return 0; /* only mic is allowed */
4659 if (!is_jack_detectable(codec, nid))
4660 return 0; /* no unsol support */
4663 if (num_pins >= MAX_AUTO_MIC_PINS)
4665 types |= (1 << attr);
4666 spec->am_entry[num_pins].pin = nid;
4667 spec->am_entry[num_pins].attr = attr;
4674 spec->am_num_entries = num_pins;
4675 /* sort the am_entry in the order of attr so that the pin with a
4676 * higher attr will be selected when the jack is plugged.
4678 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4679 compare_attr, NULL);
4681 if (!auto_mic_check_imux(codec))
4685 spec->num_adc_nids = 1;
4686 spec->cur_mux[0] = spec->am_entry[0].idx;
4687 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4688 spec->am_entry[0].pin,
4689 spec->am_entry[1].pin,
4690 spec->am_entry[2].pin);
4696 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4698 * @codec: the HDA codec
4699 * @nid: NID to evalute
4700 * @power_state: target power state
4702 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4704 unsigned int power_state)
4706 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
4708 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4710 if (is_active_nid_for_any(codec, nid))
4714 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4716 /* mute all aamix inputs initially; parse up to the first leaves */
4717 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4720 const hda_nid_t *conn;
4723 nums = snd_hda_get_conn_list(codec, mix, &conn);
4724 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4725 for (i = 0; i < nums; i++) {
4727 update_amp(codec, mix, HDA_INPUT, i,
4728 0xff, HDA_AMP_MUTE);
4729 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4730 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4731 0xff, HDA_AMP_MUTE);
4736 * snd_hda_gen_stream_pm - Stream power management callback
4737 * @codec: the HDA codec
4738 * @nid: audio widget
4739 * @on: power on/off flag
4741 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
4743 void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4745 if (codec->power_save_node)
4746 set_path_power(codec, nid, -1, on);
4748 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4751 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4752 * set up the hda_gen_spec
4753 * @codec: the HDA codec
4754 * @cfg: Parsed pin configuration
4756 * return 1 if successful, 0 if the proper config is not found,
4757 * or a negative error code
4759 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4760 struct auto_pin_cfg *cfg)
4762 struct hda_gen_spec *spec = codec->spec;
4765 parse_user_hints(codec);
4767 if (spec->mixer_nid && !spec->mixer_merge_nid)
4768 spec->mixer_merge_nid = spec->mixer_nid;
4770 if (cfg != &spec->autocfg) {
4771 spec->autocfg = *cfg;
4772 cfg = &spec->autocfg;
4775 if (!spec->main_out_badness)
4776 spec->main_out_badness = &hda_main_out_badness;
4777 if (!spec->extra_out_badness)
4778 spec->extra_out_badness = &hda_extra_out_badness;
4780 fill_all_dac_nids(codec);
4782 if (!cfg->line_outs) {
4783 if (cfg->dig_outs || cfg->dig_in_pin) {
4784 spec->multiout.max_channels = 2;
4785 spec->no_analog = 1;
4788 if (!cfg->num_inputs && !cfg->dig_in_pin)
4789 return 0; /* can't find valid BIOS pin config */
4792 if (!spec->no_primary_hp &&
4793 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4794 cfg->line_outs <= cfg->hp_outs) {
4795 /* use HP as primary out */
4796 cfg->speaker_outs = cfg->line_outs;
4797 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4798 sizeof(cfg->speaker_pins));
4799 cfg->line_outs = cfg->hp_outs;
4800 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4802 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4803 cfg->line_out_type = AUTO_PIN_HP_OUT;
4806 err = parse_output_paths(codec);
4809 err = create_multi_channel_mode(codec);
4812 err = create_multi_out_ctls(codec, cfg);
4815 err = create_hp_out_ctls(codec);
4818 err = create_speaker_out_ctls(codec);
4821 err = create_indep_hp_ctls(codec);
4824 err = create_loopback_mixing_ctl(codec);
4827 err = create_hp_mic(codec);
4830 err = create_input_ctls(codec);
4834 /* add power-down pin callbacks at first */
4835 add_all_pin_power_ctls(codec, false);
4837 spec->const_channel_count = spec->ext_channel_count;
4838 /* check the multiple speaker and headphone pins */
4839 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4840 spec->const_channel_count = max(spec->const_channel_count,
4841 cfg->speaker_outs * 2);
4842 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4843 spec->const_channel_count = max(spec->const_channel_count,
4845 spec->multiout.max_channels = max(spec->ext_channel_count,
4846 spec->const_channel_count);
4848 err = check_auto_mute_availability(codec);
4852 err = check_dyn_adc_switch(codec);
4856 err = check_auto_mic_availability(codec);
4860 /* add stereo mix if available and not enabled yet */
4861 if (!spec->auto_mic && spec->mixer_nid &&
4862 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4863 spec->input_mux.num_items > 1) {
4864 err = parse_capture_source(codec, spec->mixer_nid,
4865 CFG_IDX_MIX, spec->num_all_adcs,
4872 err = create_capture_mixers(codec);
4876 err = parse_mic_boost(codec);
4880 /* create "Headphone Mic Jack Mode" if no input selection is
4881 * available (or user specifies add_jack_modes hint)
4883 if (spec->hp_mic_pin &&
4884 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4885 spec->add_jack_modes)) {
4886 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4891 if (spec->add_jack_modes) {
4892 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4893 err = create_out_jack_modes(codec, cfg->line_outs,
4894 cfg->line_out_pins);
4898 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4899 err = create_out_jack_modes(codec, cfg->hp_outs,
4906 /* add power-up pin callbacks at last */
4907 add_all_pin_power_ctls(codec, true);
4909 /* mute all aamix input initially */
4910 if (spec->mixer_nid)
4911 mute_all_mixer_nid(codec, spec->mixer_nid);
4914 parse_digital(codec);
4916 if (spec->power_down_unused || codec->power_save_node)
4917 codec->power_filter = snd_hda_gen_path_power_filter;
4919 if (!spec->no_analog && spec->beep_nid) {
4920 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4923 if (codec->beep && codec->power_save_node) {
4924 err = add_fake_beep_paths(codec);
4927 codec->beep->power_hook = beep_power_hook;
4933 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
4937 * Build control elements
4940 /* slave controls for virtual master */
4941 static const char * const slave_pfxs[] = {
4942 "Front", "Surround", "Center", "LFE", "Side",
4943 "Headphone", "Speaker", "Mono", "Line Out",
4944 "CLFE", "Bass Speaker", "PCM",
4945 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4946 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4947 "Headphone Side", "Headphone+LO", "Speaker+LO",
4952 * snd_hda_gen_build_controls - Build controls from the parsed results
4953 * @codec: the HDA codec
4955 * Pass this to build_controls patch_ops.
4957 int snd_hda_gen_build_controls(struct hda_codec *codec)
4959 struct hda_gen_spec *spec = codec->spec;
4962 if (spec->kctls.used) {
4963 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4968 if (spec->multiout.dig_out_nid) {
4969 err = snd_hda_create_dig_out_ctls(codec,
4970 spec->multiout.dig_out_nid,
4971 spec->multiout.dig_out_nid,
4972 spec->pcm_rec[1]->pcm_type);
4975 if (!spec->no_analog) {
4976 err = snd_hda_create_spdif_share_sw(codec,
4980 spec->multiout.share_spdif = 1;
4983 if (spec->dig_in_nid) {
4984 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4989 /* if we have no master control, let's create it */
4990 if (!spec->no_analog &&
4991 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
4992 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
4993 spec->vmaster_tlv, slave_pfxs,
4998 if (!spec->no_analog &&
4999 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5000 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5003 true, &spec->vmaster_mute.sw_kctl);
5006 if (spec->vmaster_mute.hook) {
5007 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5008 spec->vmaster_mute_enum);
5009 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5013 free_kctls(spec); /* no longer needed */
5015 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5021 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
5028 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5029 struct hda_codec *codec,
5030 struct snd_pcm_substream *substream,
5033 struct hda_gen_spec *spec = codec->spec;
5034 if (spec->pcm_playback_hook)
5035 spec->pcm_playback_hook(hinfo, codec, substream, action);
5038 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5039 struct hda_codec *codec,
5040 struct snd_pcm_substream *substream,
5043 struct hda_gen_spec *spec = codec->spec;
5044 if (spec->pcm_capture_hook)
5045 spec->pcm_capture_hook(hinfo, codec, substream, action);
5049 * Analog playback callbacks
5051 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5052 struct hda_codec *codec,
5053 struct snd_pcm_substream *substream)
5055 struct hda_gen_spec *spec = codec->spec;
5058 mutex_lock(&spec->pcm_mutex);
5059 err = snd_hda_multi_out_analog_open(codec,
5060 &spec->multiout, substream,
5063 spec->active_streams |= 1 << STREAM_MULTI_OUT;
5064 call_pcm_playback_hook(hinfo, codec, substream,
5065 HDA_GEN_PCM_ACT_OPEN);
5067 mutex_unlock(&spec->pcm_mutex);
5071 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5072 struct hda_codec *codec,
5073 unsigned int stream_tag,
5074 unsigned int format,
5075 struct snd_pcm_substream *substream)
5077 struct hda_gen_spec *spec = codec->spec;
5080 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5081 stream_tag, format, substream);
5083 call_pcm_playback_hook(hinfo, codec, substream,
5084 HDA_GEN_PCM_ACT_PREPARE);
5088 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5089 struct hda_codec *codec,
5090 struct snd_pcm_substream *substream)
5092 struct hda_gen_spec *spec = codec->spec;
5095 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5097 call_pcm_playback_hook(hinfo, codec, substream,
5098 HDA_GEN_PCM_ACT_CLEANUP);
5102 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5103 struct hda_codec *codec,
5104 struct snd_pcm_substream *substream)
5106 struct hda_gen_spec *spec = codec->spec;
5107 mutex_lock(&spec->pcm_mutex);
5108 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
5109 call_pcm_playback_hook(hinfo, codec, substream,
5110 HDA_GEN_PCM_ACT_CLOSE);
5111 mutex_unlock(&spec->pcm_mutex);
5115 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5116 struct hda_codec *codec,
5117 struct snd_pcm_substream *substream)
5119 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5123 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5124 struct hda_codec *codec,
5125 unsigned int stream_tag,
5126 unsigned int format,
5127 struct snd_pcm_substream *substream)
5129 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5130 call_pcm_capture_hook(hinfo, codec, substream,
5131 HDA_GEN_PCM_ACT_PREPARE);
5135 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5136 struct hda_codec *codec,
5137 struct snd_pcm_substream *substream)
5139 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5140 call_pcm_capture_hook(hinfo, codec, substream,
5141 HDA_GEN_PCM_ACT_CLEANUP);
5145 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5146 struct hda_codec *codec,
5147 struct snd_pcm_substream *substream)
5149 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5153 static int alt_playback_pcm_open(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 mutex_lock(&spec->pcm_mutex);
5161 if (!spec->indep_hp_enabled)
5164 spec->active_streams |= 1 << STREAM_INDEP_HP;
5165 call_pcm_playback_hook(hinfo, codec, substream,
5166 HDA_GEN_PCM_ACT_OPEN);
5167 mutex_unlock(&spec->pcm_mutex);
5171 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5172 struct hda_codec *codec,
5173 struct snd_pcm_substream *substream)
5175 struct hda_gen_spec *spec = codec->spec;
5176 mutex_lock(&spec->pcm_mutex);
5177 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
5178 call_pcm_playback_hook(hinfo, codec, substream,
5179 HDA_GEN_PCM_ACT_CLOSE);
5180 mutex_unlock(&spec->pcm_mutex);
5184 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5185 struct hda_codec *codec,
5186 unsigned int stream_tag,
5187 unsigned int format,
5188 struct snd_pcm_substream *substream)
5190 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5191 call_pcm_playback_hook(hinfo, codec, substream,
5192 HDA_GEN_PCM_ACT_PREPARE);
5196 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5197 struct hda_codec *codec,
5198 struct snd_pcm_substream *substream)
5200 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5201 call_pcm_playback_hook(hinfo, codec, substream,
5202 HDA_GEN_PCM_ACT_CLEANUP);
5209 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5210 struct hda_codec *codec,
5211 struct snd_pcm_substream *substream)
5213 struct hda_gen_spec *spec = codec->spec;
5214 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5217 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5218 struct hda_codec *codec,
5219 unsigned int stream_tag,
5220 unsigned int format,
5221 struct snd_pcm_substream *substream)
5223 struct hda_gen_spec *spec = codec->spec;
5224 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5225 stream_tag, format, substream);
5228 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5229 struct hda_codec *codec,
5230 struct snd_pcm_substream *substream)
5232 struct hda_gen_spec *spec = codec->spec;
5233 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5236 static int dig_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 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5247 #define alt_capture_pcm_open capture_pcm_open
5248 #define alt_capture_pcm_close capture_pcm_close
5250 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5251 struct hda_codec *codec,
5252 unsigned int stream_tag,
5253 unsigned int format,
5254 struct snd_pcm_substream *substream)
5256 struct hda_gen_spec *spec = codec->spec;
5258 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5259 stream_tag, 0, format);
5260 call_pcm_capture_hook(hinfo, codec, substream,
5261 HDA_GEN_PCM_ACT_PREPARE);
5265 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5266 struct hda_codec *codec,
5267 struct snd_pcm_substream *substream)
5269 struct hda_gen_spec *spec = codec->spec;
5271 snd_hda_codec_cleanup_stream(codec,
5272 spec->adc_nids[substream->number + 1]);
5273 call_pcm_capture_hook(hinfo, codec, substream,
5274 HDA_GEN_PCM_ACT_CLEANUP);
5280 static const struct hda_pcm_stream pcm_analog_playback = {
5284 /* NID is set in build_pcms */
5286 .open = playback_pcm_open,
5287 .close = playback_pcm_close,
5288 .prepare = playback_pcm_prepare,
5289 .cleanup = playback_pcm_cleanup
5293 static const struct hda_pcm_stream pcm_analog_capture = {
5297 /* NID is set in build_pcms */
5299 .open = capture_pcm_open,
5300 .close = capture_pcm_close,
5301 .prepare = capture_pcm_prepare,
5302 .cleanup = capture_pcm_cleanup
5306 static const struct hda_pcm_stream pcm_analog_alt_playback = {
5310 /* NID is set in build_pcms */
5312 .open = alt_playback_pcm_open,
5313 .close = alt_playback_pcm_close,
5314 .prepare = alt_playback_pcm_prepare,
5315 .cleanup = alt_playback_pcm_cleanup
5319 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5320 .substreams = 2, /* can be overridden */
5323 /* NID is set in build_pcms */
5325 .open = alt_capture_pcm_open,
5326 .close = alt_capture_pcm_close,
5327 .prepare = alt_capture_pcm_prepare,
5328 .cleanup = alt_capture_pcm_cleanup
5332 static const struct hda_pcm_stream pcm_digital_playback = {
5336 /* NID is set in build_pcms */
5338 .open = dig_playback_pcm_open,
5339 .close = dig_playback_pcm_close,
5340 .prepare = dig_playback_pcm_prepare,
5341 .cleanup = dig_playback_pcm_cleanup
5345 static const struct hda_pcm_stream pcm_digital_capture = {
5349 /* NID is set in build_pcms */
5352 /* Used by build_pcms to flag that a PCM has no playback stream */
5353 static const struct hda_pcm_stream pcm_null_stream = {
5360 * dynamic changing ADC PCM streams
5362 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5364 struct hda_gen_spec *spec = codec->spec;
5365 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5367 if (spec->cur_adc && spec->cur_adc != new_adc) {
5368 /* stream is running, let's swap the current ADC */
5369 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5370 spec->cur_adc = new_adc;
5371 snd_hda_codec_setup_stream(codec, new_adc,
5372 spec->cur_adc_stream_tag, 0,
5373 spec->cur_adc_format);
5379 /* analog capture with dynamic dual-adc changes */
5380 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5381 struct hda_codec *codec,
5382 unsigned int stream_tag,
5383 unsigned int format,
5384 struct snd_pcm_substream *substream)
5386 struct hda_gen_spec *spec = codec->spec;
5387 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5388 spec->cur_adc_stream_tag = stream_tag;
5389 spec->cur_adc_format = format;
5390 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5394 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5395 struct hda_codec *codec,
5396 struct snd_pcm_substream *substream)
5398 struct hda_gen_spec *spec = codec->spec;
5399 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5404 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5408 .nid = 0, /* fill later */
5410 .prepare = dyn_adc_capture_pcm_prepare,
5411 .cleanup = dyn_adc_capture_pcm_cleanup
5415 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5416 const char *chip_name)
5422 strlcpy(str, chip_name, len);
5424 /* drop non-alnum chars after a space */
5425 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5426 if (!isalnum(p[1])) {
5431 strlcat(str, sfx, len);
5434 /* copy PCM stream info from @default_str, and override non-NULL entries
5435 * from @spec_str and @nid
5437 static void setup_pcm_stream(struct hda_pcm_stream *str,
5438 const struct hda_pcm_stream *default_str,
5439 const struct hda_pcm_stream *spec_str,
5442 *str = *default_str;
5446 if (spec_str->substreams)
5447 str->substreams = spec_str->substreams;
5448 if (spec_str->channels_min)
5449 str->channels_min = spec_str->channels_min;
5450 if (spec_str->channels_max)
5451 str->channels_max = spec_str->channels_max;
5452 if (spec_str->rates)
5453 str->rates = spec_str->rates;
5454 if (spec_str->formats)
5455 str->formats = spec_str->formats;
5456 if (spec_str->maxbps)
5457 str->maxbps = spec_str->maxbps;
5462 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5463 * @codec: the HDA codec
5465 * Pass this to build_pcms patch_ops.
5467 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5469 struct hda_gen_spec *spec = codec->spec;
5470 struct hda_pcm *info;
5471 bool have_multi_adcs;
5473 if (spec->no_analog)
5476 fill_pcm_stream_name(spec->stream_name_analog,
5477 sizeof(spec->stream_name_analog),
5478 " Analog", codec->core.chip_name);
5479 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5482 spec->pcm_rec[0] = info;
5484 if (spec->multiout.num_dacs > 0) {
5485 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5486 &pcm_analog_playback,
5487 spec->stream_analog_playback,
5488 spec->multiout.dac_nids[0]);
5489 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5490 spec->multiout.max_channels;
5491 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5492 spec->autocfg.line_outs == 2)
5493 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5496 if (spec->num_adc_nids) {
5497 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5498 (spec->dyn_adc_switch ?
5499 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5500 spec->stream_analog_capture,
5505 /* SPDIF for stream index #1 */
5506 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5507 fill_pcm_stream_name(spec->stream_name_digital,
5508 sizeof(spec->stream_name_digital),
5509 " Digital", codec->core.chip_name);
5510 info = snd_hda_codec_pcm_new(codec, "%s",
5511 spec->stream_name_digital);
5514 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5515 spec->pcm_rec[1] = info;
5516 if (spec->dig_out_type)
5517 info->pcm_type = spec->dig_out_type;
5519 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5520 if (spec->multiout.dig_out_nid)
5521 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5522 &pcm_digital_playback,
5523 spec->stream_digital_playback,
5524 spec->multiout.dig_out_nid);
5525 if (spec->dig_in_nid)
5526 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5527 &pcm_digital_capture,
5528 spec->stream_digital_capture,
5532 if (spec->no_analog)
5535 /* If the use of more than one ADC is requested for the current
5536 * model, configure a second analog capture-only PCM.
5538 have_multi_adcs = (spec->num_adc_nids > 1) &&
5539 !spec->dyn_adc_switch && !spec->auto_mic;
5540 /* Additional Analaog capture for index #2 */
5541 if (spec->alt_dac_nid || have_multi_adcs) {
5542 fill_pcm_stream_name(spec->stream_name_alt_analog,
5543 sizeof(spec->stream_name_alt_analog),
5544 " Alt Analog", codec->core.chip_name);
5545 info = snd_hda_codec_pcm_new(codec, "%s",
5546 spec->stream_name_alt_analog);
5549 spec->pcm_rec[2] = info;
5550 if (spec->alt_dac_nid)
5551 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5552 &pcm_analog_alt_playback,
5553 spec->stream_analog_alt_playback,
5556 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5557 &pcm_null_stream, NULL, 0);
5558 if (have_multi_adcs) {
5559 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5560 &pcm_analog_alt_capture,
5561 spec->stream_analog_alt_capture,
5563 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5564 spec->num_adc_nids - 1;
5566 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5567 &pcm_null_stream, NULL, 0);
5573 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5577 * Standard auto-parser initializations
5580 /* configure the given path as a proper output */
5581 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5583 struct nid_path *path;
5586 path = snd_hda_get_path_from_idx(codec, path_idx);
5587 if (!path || !path->depth)
5589 pin = path->path[path->depth - 1];
5590 restore_pin_ctl(codec, pin);
5591 snd_hda_activate_path(codec, path, path->active,
5592 aamix_default(codec->spec));
5593 set_pin_eapd(codec, pin, path->active);
5596 /* initialize primary output paths */
5597 static void init_multi_out(struct hda_codec *codec)
5599 struct hda_gen_spec *spec = codec->spec;
5602 for (i = 0; i < spec->autocfg.line_outs; i++)
5603 set_output_and_unmute(codec, spec->out_paths[i]);
5607 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5611 for (i = 0; i < num_outs; i++)
5612 set_output_and_unmute(codec, paths[i]);
5615 /* initialize hp and speaker paths */
5616 static void init_extra_out(struct hda_codec *codec)
5618 struct hda_gen_spec *spec = codec->spec;
5620 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5621 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5622 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5623 __init_extra_out(codec, spec->autocfg.speaker_outs,
5624 spec->speaker_paths);
5627 /* initialize multi-io paths */
5628 static void init_multi_io(struct hda_codec *codec)
5630 struct hda_gen_spec *spec = codec->spec;
5633 for (i = 0; i < spec->multi_ios; i++) {
5634 hda_nid_t pin = spec->multi_io[i].pin;
5635 struct nid_path *path;
5636 path = get_multiio_path(codec, i);
5639 if (!spec->multi_io[i].ctl_in)
5640 spec->multi_io[i].ctl_in =
5641 snd_hda_codec_get_pin_target(codec, pin);
5642 snd_hda_activate_path(codec, path, path->active,
5643 aamix_default(spec));
5647 static void init_aamix_paths(struct hda_codec *codec)
5649 struct hda_gen_spec *spec = codec->spec;
5651 if (!spec->have_aamix_ctl)
5653 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5654 spec->aamix_out_paths[0],
5655 spec->autocfg.line_out_type);
5656 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5657 spec->aamix_out_paths[1],
5659 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5660 spec->aamix_out_paths[2],
5661 AUTO_PIN_SPEAKER_OUT);
5664 /* set up input pins and loopback paths */
5665 static void init_analog_input(struct hda_codec *codec)
5667 struct hda_gen_spec *spec = codec->spec;
5668 struct auto_pin_cfg *cfg = &spec->autocfg;
5671 for (i = 0; i < cfg->num_inputs; i++) {
5672 hda_nid_t nid = cfg->inputs[i].pin;
5673 if (is_input_pin(codec, nid))
5674 restore_pin_ctl(codec, nid);
5676 /* init loopback inputs */
5677 if (spec->mixer_nid) {
5678 resume_path_from_idx(codec, spec->loopback_paths[i]);
5679 resume_path_from_idx(codec, spec->loopback_merge_path);
5684 /* initialize ADC paths */
5685 static void init_input_src(struct hda_codec *codec)
5687 struct hda_gen_spec *spec = codec->spec;
5688 struct hda_input_mux *imux = &spec->input_mux;
5689 struct nid_path *path;
5692 if (spec->dyn_adc_switch)
5695 nums = spec->num_adc_nids;
5697 for (c = 0; c < nums; c++) {
5698 for (i = 0; i < imux->num_items; i++) {
5699 path = get_input_path(codec, c, i);
5701 bool active = path->active;
5702 if (i == spec->cur_mux[c])
5704 snd_hda_activate_path(codec, path, active, false);
5708 update_hp_mic(codec, c, true);
5711 if (spec->cap_sync_hook)
5712 spec->cap_sync_hook(codec, NULL, NULL);
5715 /* set right pin controls for digital I/O */
5716 static void init_digital(struct hda_codec *codec)
5718 struct hda_gen_spec *spec = codec->spec;
5722 for (i = 0; i < spec->autocfg.dig_outs; i++)
5723 set_output_and_unmute(codec, spec->digout_paths[i]);
5724 pin = spec->autocfg.dig_in_pin;
5726 restore_pin_ctl(codec, pin);
5727 resume_path_from_idx(codec, spec->digin_path);
5731 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5732 * invalid unsol tags by some reason
5734 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5738 for (i = 0; i < codec->init_pins.used; i++) {
5739 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5740 hda_nid_t nid = pin->nid;
5741 if (is_jack_detectable(codec, nid) &&
5742 !snd_hda_jack_tbl_get(codec, nid))
5743 snd_hda_codec_update_cache(codec, nid, 0,
5744 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5749 * snd_hda_gen_init - initialize the generic spec
5750 * @codec: the HDA codec
5752 * This can be put as patch_ops init function.
5754 int snd_hda_gen_init(struct hda_codec *codec)
5756 struct hda_gen_spec *spec = codec->spec;
5758 if (spec->init_hook)
5759 spec->init_hook(codec);
5761 snd_hda_apply_verbs(codec);
5763 init_multi_out(codec);
5764 init_extra_out(codec);
5765 init_multi_io(codec);
5766 init_aamix_paths(codec);
5767 init_analog_input(codec);
5768 init_input_src(codec);
5769 init_digital(codec);
5771 clear_unsol_on_unused_pins(codec);
5773 sync_all_pin_power_ctls(codec);
5775 /* call init functions of standard auto-mute helpers */
5776 update_automute_all(codec);
5778 regcache_sync(codec->core.regmap);
5780 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5781 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5783 hda_call_check_power_status(codec, 0x01);
5786 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
5789 * snd_hda_gen_free - free the generic spec
5790 * @codec: the HDA codec
5792 * This can be put as patch_ops free function.
5794 void snd_hda_gen_free(struct hda_codec *codec)
5796 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
5797 snd_hda_gen_spec_free(codec->spec);
5801 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
5805 * snd_hda_gen_check_power_status - check the loopback power save state
5806 * @codec: the HDA codec
5807 * @nid: NID to inspect
5809 * This can be put as patch_ops check_power_status function.
5811 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5813 struct hda_gen_spec *spec = codec->spec;
5814 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5816 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
5821 * the generic codec support
5824 static const struct hda_codec_ops generic_patch_ops = {
5825 .build_controls = snd_hda_gen_build_controls,
5826 .build_pcms = snd_hda_gen_build_pcms,
5827 .init = snd_hda_gen_init,
5828 .free = snd_hda_gen_free,
5829 .unsol_event = snd_hda_jack_unsol_event,
5831 .check_power_status = snd_hda_gen_check_power_status,
5836 * snd_hda_parse_generic_codec - Generic codec parser
5837 * @codec: the HDA codec
5839 static int snd_hda_parse_generic_codec(struct hda_codec *codec)
5841 struct hda_gen_spec *spec;
5844 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5847 snd_hda_gen_spec_init(spec);
5850 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5854 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5858 codec->patch_ops = generic_patch_ops;
5862 snd_hda_gen_free(codec);
5866 static const struct hda_codec_preset snd_hda_preset_generic[] = {
5867 { .id = HDA_CODEC_ID_GENERIC, .patch = snd_hda_parse_generic_codec },
5871 static struct hda_codec_driver generic_driver = {
5872 .preset = snd_hda_preset_generic,
5875 module_hda_codec_driver(generic_driver);
5877 MODULE_LICENSE("GPL");
5878 MODULE_DESCRIPTION("Generic HD-audio codec parser");