2 * Universal Interface for Intel High Definition Audio Codec
4 * Generic widget tree parser
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/sort.h>
27 #include <linux/delay.h>
28 #include <linux/ctype.h>
29 #include <linux/string.h>
30 #include <linux/bitops.h>
31 #include <linux/module.h>
32 #include <sound/core.h>
33 #include <sound/jack.h>
34 #include <sound/tlv.h>
35 #include "hda_codec.h"
36 #include "hda_local.h"
37 #include "hda_auto_parser.h"
40 #include "hda_generic.h"
44 * snd_hda_gen_spec_init - initialize hda_gen_spec struct
45 * @spec: hda_gen_spec object to initialize
47 * Initialize the given hda_gen_spec object.
49 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
51 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
52 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
53 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
54 mutex_init(&spec->pcm_mutex);
57 EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
60 * snd_hda_gen_add_kctl - Add a new kctl_new struct from the template
61 * @spec: hda_gen_spec object
62 * @name: name string to override the template, NULL if unchanged
63 * @temp: template for the new kctl
65 * Add a new kctl (actually snd_kcontrol_new to be instantiated later)
66 * element based on the given snd_kcontrol_new template @temp and the
67 * name string @name to the list in @spec.
68 * Returns the newly created object or NULL as error.
70 struct snd_kcontrol_new *
71 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
72 const struct snd_kcontrol_new *temp)
74 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
79 knew->name = kstrdup(name, GFP_KERNEL);
81 knew->name = kstrdup(knew->name, GFP_KERNEL);
86 EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
88 static void free_kctls(struct hda_gen_spec *spec)
90 if (spec->kctls.list) {
91 struct snd_kcontrol_new *kctl = spec->kctls.list;
93 for (i = 0; i < spec->kctls.used; i++)
96 snd_array_free(&spec->kctls);
99 static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
104 snd_array_free(&spec->paths);
105 snd_array_free(&spec->loopback_list);
111 static void parse_user_hints(struct hda_codec *codec)
113 struct hda_gen_spec *spec = codec->spec;
116 val = snd_hda_get_bool_hint(codec, "jack_detect");
118 codec->no_jack_detect = !val;
119 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
121 codec->inv_jack_detect = !!val;
122 val = snd_hda_get_bool_hint(codec, "trigger_sense");
124 codec->no_trigger_sense = !val;
125 val = snd_hda_get_bool_hint(codec, "inv_eapd");
127 codec->inv_eapd = !!val;
128 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
130 codec->pcm_format_first = !!val;
131 val = snd_hda_get_bool_hint(codec, "sticky_stream");
133 codec->no_sticky_stream = !val;
134 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
136 codec->spdif_status_reset = !!val;
137 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
139 codec->pin_amp_workaround = !!val;
140 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
142 codec->single_adc_amp = !!val;
143 val = snd_hda_get_bool_hint(codec, "power_save_node");
145 codec->power_save_node = !!val;
147 val = snd_hda_get_bool_hint(codec, "auto_mute");
149 spec->suppress_auto_mute = !val;
150 val = snd_hda_get_bool_hint(codec, "auto_mic");
152 spec->suppress_auto_mic = !val;
153 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
155 spec->line_in_auto_switch = !!val;
156 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
158 spec->auto_mute_via_amp = !!val;
159 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
161 spec->need_dac_fix = !!val;
162 val = snd_hda_get_bool_hint(codec, "primary_hp");
164 spec->no_primary_hp = !val;
165 val = snd_hda_get_bool_hint(codec, "multi_io");
167 spec->no_multi_io = !val;
168 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
170 spec->multi_cap_vol = !!val;
171 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
173 spec->inv_dmic_split = !!val;
174 val = snd_hda_get_bool_hint(codec, "indep_hp");
176 spec->indep_hp = !!val;
177 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
179 spec->add_stereo_mix_input = !!val;
180 /* the following two are just for compatibility */
181 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
183 spec->add_jack_modes = !!val;
184 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
186 spec->add_jack_modes = !!val;
187 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
189 spec->add_jack_modes = !!val;
190 val = snd_hda_get_bool_hint(codec, "power_down_unused");
192 spec->power_down_unused = !!val;
193 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
195 spec->hp_mic = !!val;
196 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
198 spec->suppress_hp_mic_detect = !val;
200 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
201 spec->mixer_nid = val;
205 * pin control value accesses
208 #define update_pin_ctl(codec, pin, val) \
209 snd_hda_codec_update_cache(codec, pin, 0, \
210 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
212 /* restore the pinctl based on the cached value */
213 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
215 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
218 /* set the pinctl target value and write it if requested */
219 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
220 unsigned int val, bool do_write)
224 val = snd_hda_correct_pin_ctl(codec, pin, val);
225 snd_hda_codec_set_pin_target(codec, pin, val);
227 update_pin_ctl(codec, pin, val);
230 /* set pinctl target values for all given pins */
231 static void set_pin_targets(struct hda_codec *codec, int num_pins,
232 hda_nid_t *pins, unsigned int val)
235 for (i = 0; i < num_pins; i++)
236 set_pin_target(codec, pins[i], val, false);
243 /* return the position of NID in the list, or -1 if not found */
244 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
247 for (i = 0; i < nums; i++)
253 /* return true if the given NID is contained in the path */
254 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
256 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
259 static struct nid_path *get_nid_path(struct hda_codec *codec,
260 hda_nid_t from_nid, hda_nid_t to_nid,
263 struct hda_gen_spec *spec = codec->spec;
266 for (i = 0; i < spec->paths.used; i++) {
267 struct nid_path *path = snd_array_elem(&spec->paths, i);
268 if (path->depth <= 0)
270 if ((!from_nid || path->path[0] == from_nid) &&
271 (!to_nid || path->path[path->depth - 1] == to_nid)) {
273 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
274 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
282 * snd_hda_get_nid_path - get the path between the given NIDs
283 * @codec: the HDA codec
284 * @from_nid: the NID where the path start from
285 * @to_nid: the NID where the path ends at
287 * Return the found nid_path object or NULL for error.
288 * Passing 0 to either @from_nid or @to_nid behaves as a wildcard.
290 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
291 hda_nid_t from_nid, hda_nid_t to_nid)
293 return get_nid_path(codec, from_nid, to_nid, 0);
295 EXPORT_SYMBOL_GPL(snd_hda_get_nid_path);
298 * snd_hda_get_path_idx - get the index number corresponding to the path
300 * @codec: the HDA codec
301 * @path: nid_path object
303 * The returned index starts from 1, i.e. the actual array index with offset 1,
304 * and zero is handled as an invalid path
306 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
308 struct hda_gen_spec *spec = codec->spec;
309 struct nid_path *array = spec->paths.list;
312 if (!spec->paths.used)
315 if (idx < 0 || idx >= spec->paths.used)
319 EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
322 * snd_hda_get_path_from_idx - get the path instance corresponding to the
324 * @codec: the HDA codec
325 * @idx: the path index
327 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
329 struct hda_gen_spec *spec = codec->spec;
331 if (idx <= 0 || idx > spec->paths.used)
333 return snd_array_elem(&spec->paths, idx - 1);
335 EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
337 /* check whether the given DAC is already found in any existing paths */
338 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
340 struct hda_gen_spec *spec = codec->spec;
343 for (i = 0; i < spec->paths.used; i++) {
344 struct nid_path *path = snd_array_elem(&spec->paths, i);
345 if (path->path[0] == nid)
351 /* check whether the given two widgets can be connected */
352 static bool is_reachable_path(struct hda_codec *codec,
353 hda_nid_t from_nid, hda_nid_t to_nid)
355 if (!from_nid || !to_nid)
357 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
360 /* nid, dir and idx */
361 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
363 /* check whether the given ctl is already assigned in any path elements */
364 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
366 struct hda_gen_spec *spec = codec->spec;
369 val &= AMP_VAL_COMPARE_MASK;
370 for (i = 0; i < spec->paths.used; i++) {
371 struct nid_path *path = snd_array_elem(&spec->paths, i);
372 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
378 /* check whether a control with the given (nid, dir, idx) was assigned */
379 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
380 int dir, int idx, int type)
382 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
383 return is_ctl_used(codec, val, type);
386 static void print_nid_path(struct hda_codec *codec,
387 const char *pfx, struct nid_path *path)
394 for (i = 0; i < path->depth; i++)
395 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
396 pos != buf ? ":" : "",
399 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
402 /* called recursively */
403 static bool __parse_nid_path(struct hda_codec *codec,
404 hda_nid_t from_nid, hda_nid_t to_nid,
405 int anchor_nid, struct nid_path *path,
408 const hda_nid_t *conn;
411 if (to_nid == anchor_nid)
412 anchor_nid = 0; /* anchor passed */
413 else if (to_nid == (hda_nid_t)(-anchor_nid))
414 return false; /* hit the exclusive nid */
416 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
417 for (i = 0; i < nums; i++) {
418 if (conn[i] != from_nid) {
419 /* special case: when from_nid is 0,
420 * try to find an empty DAC
423 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
424 is_dac_already_used(codec, conn[i]))
427 /* anchor is not requested or already passed? */
431 if (depth >= MAX_NID_PATH_DEPTH)
433 for (i = 0; i < nums; i++) {
435 type = get_wcaps_type(get_wcaps(codec, conn[i]));
436 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
439 if (__parse_nid_path(codec, from_nid, conn[i],
440 anchor_nid, path, depth + 1))
446 path->path[path->depth] = conn[i];
447 path->idx[path->depth + 1] = i;
448 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
449 path->multi[path->depth + 1] = 1;
455 * snd_hda_parse_nid_path - parse the widget path from the given nid to
457 * @codec: the HDA codec
458 * @from_nid: the NID where the path start from
459 * @to_nid: the NID where the path ends at
460 * @anchor_nid: the anchor indication
461 * @path: the path object to store the result
463 * Returns true if a matching path is found.
465 * The parsing behavior depends on parameters:
466 * when @from_nid is 0, try to find an empty DAC;
467 * when @anchor_nid is set to a positive value, only paths through the widget
468 * with the given value are evaluated.
469 * when @anchor_nid is set to a negative value, paths through the widget
470 * with the negative of given value are excluded, only other paths are chosen.
471 * when @anchor_nid is zero, no special handling about path selection.
473 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
474 hda_nid_t to_nid, int anchor_nid,
475 struct nid_path *path)
477 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
478 path->path[path->depth] = to_nid;
484 EXPORT_SYMBOL_GPL(snd_hda_parse_nid_path);
487 * snd_hda_add_new_path - parse the path between the given NIDs and
488 * add to the path list
489 * @codec: the HDA codec
490 * @from_nid: the NID where the path start from
491 * @to_nid: the NID where the path ends at
492 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
494 * If no valid path is found, returns NULL.
497 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
498 hda_nid_t to_nid, int anchor_nid)
500 struct hda_gen_spec *spec = codec->spec;
501 struct nid_path *path;
503 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
506 /* check whether the path has been already added */
507 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
511 path = snd_array_new(&spec->paths);
514 memset(path, 0, sizeof(*path));
515 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
521 EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
523 /* clear the given path as invalid so that it won't be picked up later */
524 static void invalidate_nid_path(struct hda_codec *codec, int idx)
526 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
529 memset(path, 0, sizeof(*path));
532 /* return a DAC if paired to the given pin by codec driver */
533 static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
535 struct hda_gen_spec *spec = codec->spec;
536 const hda_nid_t *list = spec->preferred_dacs;
540 for (; *list; list += 2)
546 /* look for an empty DAC slot */
547 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
550 struct hda_gen_spec *spec = codec->spec;
554 for (i = 0; i < spec->num_all_dacs; i++) {
555 hda_nid_t nid = spec->all_dacs[i];
556 if (!nid || is_dac_already_used(codec, nid))
558 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
559 if (is_digital != cap_digital)
561 if (is_reachable_path(codec, nid, pin))
567 /* replace the channels in the composed amp value with the given number */
568 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
570 val &= ~(0x3U << 16);
575 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
576 hda_nid_t nid2, int dir)
578 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
579 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
580 return (query_amp_caps(codec, nid1, dir) ==
581 query_amp_caps(codec, nid2, dir));
584 /* look for a widget suitable for assigning a mute switch in the path */
585 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
586 struct nid_path *path)
590 for (i = path->depth - 1; i >= 0; i--) {
591 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
592 return path->path[i];
593 if (i != path->depth - 1 && i != 0 &&
594 nid_has_mute(codec, path->path[i], HDA_INPUT))
595 return path->path[i];
600 /* look for a widget suitable for assigning a volume ctl in the path */
601 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
602 struct nid_path *path)
604 struct hda_gen_spec *spec = codec->spec;
607 for (i = path->depth - 1; i >= 0; i--) {
608 hda_nid_t nid = path->path[i];
609 if ((spec->out_vol_mask >> nid) & 1)
611 if (nid_has_volume(codec, nid, HDA_OUTPUT))
618 * path activation / deactivation
621 /* can have the amp-in capability? */
622 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
624 hda_nid_t nid = path->path[idx];
625 unsigned int caps = get_wcaps(codec, nid);
626 unsigned int type = get_wcaps_type(caps);
628 if (!(caps & AC_WCAP_IN_AMP))
630 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
635 /* can have the amp-out capability? */
636 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
638 hda_nid_t nid = path->path[idx];
639 unsigned int caps = get_wcaps(codec, nid);
640 unsigned int type = get_wcaps_type(caps);
642 if (!(caps & AC_WCAP_OUT_AMP))
644 if (type == AC_WID_PIN && !idx) /* only for output pins */
649 /* check whether the given (nid,dir,idx) is active */
650 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
651 unsigned int dir, unsigned int idx)
653 struct hda_gen_spec *spec = codec->spec;
654 int type = get_wcaps_type(get_wcaps(codec, nid));
657 if (nid == codec->core.afg)
660 for (n = 0; n < spec->paths.used; n++) {
661 struct nid_path *path = snd_array_elem(&spec->paths, n);
664 if (codec->power_save_node) {
665 if (!path->stream_enabled)
667 /* ignore unplugged paths except for DAC/ADC */
668 if (!(path->pin_enabled || path->pin_fixed) &&
669 type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
672 for (i = 0; i < path->depth; i++) {
673 if (path->path[i] == nid) {
674 if (dir == HDA_OUTPUT || idx == -1 ||
684 /* check whether the NID is referred by any active paths */
685 #define is_active_nid_for_any(codec, nid) \
686 is_active_nid(codec, nid, HDA_OUTPUT, -1)
688 /* get the default amp value for the target state */
689 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
690 int dir, unsigned int caps, bool enable)
692 unsigned int val = 0;
694 if (caps & AC_AMPCAP_NUM_STEPS) {
697 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
699 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
706 /* is this a stereo widget or a stereo-to-mono mix? */
707 static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
709 unsigned int wcaps = get_wcaps(codec, nid);
712 if (wcaps & AC_WCAP_STEREO)
714 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
716 if (snd_hda_get_num_conns(codec, nid) != 1)
718 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
720 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
723 /* initialize the amp value (only at the first time) */
724 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
726 unsigned int caps = query_amp_caps(codec, nid, dir);
727 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
729 if (is_stereo_amps(codec, nid, dir))
730 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
732 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
735 /* update the amp, doing in stereo or mono depending on NID */
736 static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
737 unsigned int mask, unsigned int val)
739 if (is_stereo_amps(codec, nid, dir))
740 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
743 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
747 /* calculate amp value mask we can modify;
748 * if the given amp is controlled by mixers, don't touch it
750 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
751 hda_nid_t nid, int dir, int idx,
754 unsigned int mask = 0xff;
756 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
757 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
760 if (caps & AC_AMPCAP_NUM_STEPS) {
761 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
762 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
768 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
769 int idx, int idx_to_check, bool enable)
772 unsigned int mask, val;
774 caps = query_amp_caps(codec, nid, dir);
775 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
776 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
781 update_amp(codec, nid, dir, idx, mask, val);
784 static void check_and_activate_amp(struct hda_codec *codec, hda_nid_t nid,
785 int dir, int idx, int idx_to_check,
788 /* check whether the given amp is still used by others */
789 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
791 activate_amp(codec, nid, dir, idx, idx_to_check, enable);
794 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
797 hda_nid_t nid = path->path[i];
798 init_amp(codec, nid, HDA_OUTPUT, 0);
799 check_and_activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
802 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
803 int i, bool enable, bool add_aamix)
805 struct hda_gen_spec *spec = codec->spec;
806 const hda_nid_t *conn;
809 hda_nid_t nid = path->path[i];
811 nums = snd_hda_get_conn_list(codec, nid, &conn);
812 type = get_wcaps_type(get_wcaps(codec, nid));
813 if (type == AC_WID_PIN ||
814 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
820 for (n = 0; n < nums; n++)
821 init_amp(codec, nid, HDA_INPUT, n);
823 /* here is a little bit tricky in comparison with activate_amp_out();
824 * when aa-mixer is available, we need to enable the path as well
826 for (n = 0; n < nums; n++) {
828 if (conn[n] != spec->mixer_merge_nid)
830 /* when aamix is disabled, force to off */
832 activate_amp(codec, nid, HDA_INPUT, n, n, false);
836 check_and_activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
840 /* sync power of each widget in the given path */
841 static hda_nid_t path_power_update(struct hda_codec *codec,
842 struct nid_path *path,
843 bool allow_powerdown)
845 hda_nid_t nid, changed = 0;
848 for (i = 0; i < path->depth; i++) {
850 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
852 if (nid == codec->core.afg)
854 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
858 power = snd_hda_codec_read(codec, nid, 0,
859 AC_VERB_GET_POWER_STATE, 0);
860 if (power != (state | (state << 4))) {
861 snd_hda_codec_write(codec, nid, 0,
862 AC_VERB_SET_POWER_STATE, state);
864 /* all known codecs seem to be capable to handl
865 * widgets state even in D3, so far.
866 * if any new codecs need to restore the widget
867 * states after D0 transition, call the function
871 if (state == AC_PWRST_D0)
872 snd_hdac_regmap_sync_node(&codec->core, nid);
879 /* do sync with the last power state change */
880 static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
884 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
889 * snd_hda_activate_path - activate or deactivate the given path
890 * @codec: the HDA codec
891 * @path: the path to activate/deactivate
892 * @enable: flag to activate or not
893 * @add_aamix: enable the input from aamix NID
895 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
897 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
898 bool enable, bool add_aamix)
900 struct hda_gen_spec *spec = codec->spec;
903 path->active = enable;
905 /* make sure the widget is powered up */
906 if (enable && (spec->power_down_unused || codec->power_save_node))
907 path_power_update(codec, path, codec->power_save_node);
909 for (i = path->depth - 1; i >= 0; i--) {
910 hda_nid_t nid = path->path[i];
912 if (enable && path->multi[i])
913 snd_hda_codec_update_cache(codec, nid, 0,
914 AC_VERB_SET_CONNECT_SEL,
916 if (has_amp_in(codec, path, i))
917 activate_amp_in(codec, path, i, enable, add_aamix);
918 if (has_amp_out(codec, path, i))
919 activate_amp_out(codec, path, i, enable);
922 EXPORT_SYMBOL_GPL(snd_hda_activate_path);
924 /* if the given path is inactive, put widgets into D3 (only if suitable) */
925 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
927 struct hda_gen_spec *spec = codec->spec;
929 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
931 sync_power_state_change(codec, path_power_update(codec, path, true));
934 /* turn on/off EAPD on the given pin */
935 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
937 struct hda_gen_spec *spec = codec->spec;
938 if (spec->own_eapd_ctl ||
939 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
941 if (spec->keep_eapd_on && !enable)
945 snd_hda_codec_update_cache(codec, pin, 0,
946 AC_VERB_SET_EAPD_BTLENABLE,
947 enable ? 0x02 : 0x00);
950 /* re-initialize the path specified by the given path index */
951 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
953 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
955 snd_hda_activate_path(codec, path, path->active, false);
960 * Helper functions for creating mixer ctl elements
963 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
964 struct snd_ctl_elem_value *ucontrol);
965 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
966 struct snd_ctl_elem_value *ucontrol);
973 static const struct snd_kcontrol_new control_templates[] = {
974 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
975 /* only the put callback is replaced for handling the special mute */
977 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
978 .subdevice = HDA_SUBDEV_AMP_FLAG,
979 .info = snd_hda_mixer_amp_switch_info,
980 .get = snd_hda_mixer_amp_switch_get,
981 .put = hda_gen_mixer_mute_put, /* replaced */
982 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
985 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
986 .info = snd_hda_mixer_amp_switch_info,
987 .get = snd_hda_mixer_bind_switch_get,
988 .put = hda_gen_bind_mute_put, /* replaced */
989 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
993 /* add dynamic controls from template */
994 static struct snd_kcontrol_new *
995 add_control(struct hda_gen_spec *spec, int type, const char *name,
996 int cidx, unsigned long val)
998 struct snd_kcontrol_new *knew;
1000 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
1004 if (get_amp_nid_(val))
1005 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
1006 knew->private_value = val;
1010 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
1011 const char *pfx, const char *dir,
1012 const char *sfx, int cidx, unsigned long val)
1014 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1015 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
1016 if (!add_control(spec, type, name, cidx, val))
1021 #define add_pb_vol_ctrl(spec, type, pfx, val) \
1022 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1023 #define add_pb_sw_ctrl(spec, type, pfx, val) \
1024 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1025 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1026 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1027 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1028 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1030 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1031 unsigned int chs, struct nid_path *path)
1036 val = path->ctls[NID_PATH_VOL_CTL];
1039 val = amp_val_replace_channels(val, chs);
1040 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1043 /* return the channel bits suitable for the given path->ctls[] */
1044 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1047 int chs = 1; /* mono (left only) */
1049 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1050 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1051 chs = 3; /* stereo */
1056 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1057 struct nid_path *path)
1059 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1060 return add_vol_ctl(codec, pfx, cidx, chs, path);
1063 /* create a mute-switch for the given mixer widget;
1064 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1066 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1067 unsigned int chs, struct nid_path *path)
1070 int type = HDA_CTL_WIDGET_MUTE;
1074 val = path->ctls[NID_PATH_MUTE_CTL];
1077 val = amp_val_replace_channels(val, chs);
1078 if (get_amp_direction_(val) == HDA_INPUT) {
1079 hda_nid_t nid = get_amp_nid_(val);
1080 int nums = snd_hda_get_num_conns(codec, nid);
1082 type = HDA_CTL_BIND_MUTE;
1086 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1089 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1090 int cidx, struct nid_path *path)
1092 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1093 return add_sw_ctl(codec, pfx, cidx, chs, path);
1096 /* playback mute control with the software mute bit check */
1097 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1098 struct snd_ctl_elem_value *ucontrol)
1100 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1101 struct hda_gen_spec *spec = codec->spec;
1103 if (spec->auto_mute_via_amp) {
1104 hda_nid_t nid = get_amp_nid(kcontrol);
1105 bool enabled = !((spec->mute_bits >> nid) & 1);
1106 ucontrol->value.integer.value[0] &= enabled;
1107 ucontrol->value.integer.value[1] &= enabled;
1111 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1112 struct snd_ctl_elem_value *ucontrol)
1114 sync_auto_mute_bits(kcontrol, ucontrol);
1115 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1118 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1119 struct snd_ctl_elem_value *ucontrol)
1121 sync_auto_mute_bits(kcontrol, ucontrol);
1122 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1125 /* any ctl assigned to the path with the given index? */
1126 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1128 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1129 return path && path->ctls[ctl_type];
1132 static const char * const channel_name[4] = {
1133 "Front", "Surround", "CLFE", "Side"
1136 /* give some appropriate ctl name prefix for the given line out channel */
1137 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1138 int *index, int ctl_type)
1140 struct hda_gen_spec *spec = codec->spec;
1141 struct auto_pin_cfg *cfg = &spec->autocfg;
1144 if (cfg->line_outs == 1 && !spec->multi_ios &&
1145 !cfg->hp_outs && !cfg->speaker_outs)
1146 return spec->vmaster_mute.hook ? "PCM" : "Master";
1148 /* if there is really a single DAC used in the whole output paths,
1149 * use it master (or "PCM" if a vmaster hook is present)
1151 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1152 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1153 return spec->vmaster_mute.hook ? "PCM" : "Master";
1155 /* multi-io channels */
1156 if (ch >= cfg->line_outs)
1157 return channel_name[ch];
1159 switch (cfg->line_out_type) {
1160 case AUTO_PIN_SPEAKER_OUT:
1161 /* if the primary channel vol/mute is shared with HP volume,
1162 * don't name it as Speaker
1164 if (!ch && cfg->hp_outs &&
1165 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1167 if (cfg->line_outs == 1)
1169 if (cfg->line_outs == 2)
1170 return ch ? "Bass Speaker" : "Speaker";
1172 case AUTO_PIN_HP_OUT:
1173 /* if the primary channel vol/mute is shared with spk volume,
1174 * don't name it as Headphone
1176 if (!ch && cfg->speaker_outs &&
1177 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1179 /* for multi-io case, only the primary out */
1180 if (ch && spec->multi_ios)
1184 case AUTO_PIN_LINE_OUT:
1185 /* This deals with the case where one HP or one Speaker or
1186 * one HP + one Speaker need to share the DAC with LO
1189 bool hp_lo_shared = false, spk_lo_shared = false;
1191 if (cfg->speaker_outs)
1192 spk_lo_shared = !path_has_mixer(codec,
1193 spec->speaker_paths[0], ctl_type);
1195 hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1196 if (hp_lo_shared && spk_lo_shared)
1197 return spec->vmaster_mute.hook ? "PCM" : "Master";
1199 return "Headphone+LO";
1201 return "Speaker+LO";
1205 /* for a single channel output, we don't have to name the channel */
1206 if (cfg->line_outs == 1 && !spec->multi_ios)
1209 if (ch >= ARRAY_SIZE(channel_name)) {
1214 return channel_name[ch];
1218 * Parse output paths
1221 /* badness definition */
1223 /* No primary DAC is found for the main output */
1224 BAD_NO_PRIMARY_DAC = 0x10000,
1225 /* No DAC is found for the extra output */
1226 BAD_NO_DAC = 0x4000,
1227 /* No possible multi-ios */
1228 BAD_MULTI_IO = 0x120,
1229 /* No individual DAC for extra output */
1230 BAD_NO_EXTRA_DAC = 0x102,
1231 /* No individual DAC for extra surrounds */
1232 BAD_NO_EXTRA_SURR_DAC = 0x101,
1233 /* Primary DAC shared with main surrounds */
1234 BAD_SHARED_SURROUND = 0x100,
1235 /* No independent HP possible */
1236 BAD_NO_INDEP_HP = 0x10,
1237 /* Primary DAC shared with main CLFE */
1238 BAD_SHARED_CLFE = 0x10,
1239 /* Primary DAC shared with extra surrounds */
1240 BAD_SHARED_EXTRA_SURROUND = 0x10,
1241 /* Volume widget is shared */
1242 BAD_SHARED_VOL = 0x10,
1245 /* look for widgets in the given path which are appropriate for
1246 * volume and mute controls, and assign the values to ctls[].
1248 * When no appropriate widget is found in the path, the badness value
1249 * is incremented depending on the situation. The function returns the
1250 * total badness for both volume and mute controls.
1252 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1254 struct hda_gen_spec *spec = codec->spec;
1260 return BAD_SHARED_VOL * 2;
1262 if (path->ctls[NID_PATH_VOL_CTL] ||
1263 path->ctls[NID_PATH_MUTE_CTL])
1264 return 0; /* already evaluated */
1266 nid = look_for_out_vol_nid(codec, path);
1268 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1269 if (spec->dac_min_mute)
1270 val |= HDA_AMP_VAL_MIN_MUTE;
1271 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1272 badness += BAD_SHARED_VOL;
1274 path->ctls[NID_PATH_VOL_CTL] = val;
1276 badness += BAD_SHARED_VOL;
1277 nid = look_for_out_mute_nid(codec, path);
1279 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1280 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1281 nid_has_mute(codec, nid, HDA_OUTPUT))
1282 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1284 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1285 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1286 badness += BAD_SHARED_VOL;
1288 path->ctls[NID_PATH_MUTE_CTL] = val;
1290 badness += BAD_SHARED_VOL;
1294 const struct badness_table hda_main_out_badness = {
1295 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1296 .no_dac = BAD_NO_DAC,
1297 .shared_primary = BAD_NO_PRIMARY_DAC,
1298 .shared_surr = BAD_SHARED_SURROUND,
1299 .shared_clfe = BAD_SHARED_CLFE,
1300 .shared_surr_main = BAD_SHARED_SURROUND,
1302 EXPORT_SYMBOL_GPL(hda_main_out_badness);
1304 const struct badness_table hda_extra_out_badness = {
1305 .no_primary_dac = BAD_NO_DAC,
1306 .no_dac = BAD_NO_DAC,
1307 .shared_primary = BAD_NO_EXTRA_DAC,
1308 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1309 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1310 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1312 EXPORT_SYMBOL_GPL(hda_extra_out_badness);
1314 /* get the DAC of the primary output corresponding to the given array index */
1315 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1317 struct hda_gen_spec *spec = codec->spec;
1318 struct auto_pin_cfg *cfg = &spec->autocfg;
1320 if (cfg->line_outs > idx)
1321 return spec->private_dac_nids[idx];
1322 idx -= cfg->line_outs;
1323 if (spec->multi_ios > idx)
1324 return spec->multi_io[idx].dac;
1328 /* return the DAC if it's reachable, otherwise zero */
1329 static inline hda_nid_t try_dac(struct hda_codec *codec,
1330 hda_nid_t dac, hda_nid_t pin)
1332 return is_reachable_path(codec, dac, pin) ? dac : 0;
1335 /* try to assign DACs to pins and return the resultant badness */
1336 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1337 const hda_nid_t *pins, hda_nid_t *dacs,
1339 const struct badness_table *bad)
1341 struct hda_gen_spec *spec = codec->spec;
1349 for (i = 0; i < num_outs; i++) {
1350 struct nid_path *path;
1351 hda_nid_t pin = pins[i];
1353 if (!spec->obey_preferred_dacs) {
1354 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1356 badness += assign_out_path_ctls(codec, path);
1361 dacs[i] = get_preferred_dac(codec, pin);
1363 if (is_dac_already_used(codec, dacs[i]))
1364 badness += bad->shared_primary;
1365 } else if (spec->obey_preferred_dacs) {
1366 badness += BAD_NO_PRIMARY_DAC;
1370 dacs[i] = look_for_dac(codec, pin, false);
1371 if (!dacs[i] && !i) {
1372 /* try to steal the DAC of surrounds for the front */
1373 for (j = 1; j < num_outs; j++) {
1374 if (is_reachable_path(codec, dacs[j], pin)) {
1377 invalidate_nid_path(codec, path_idx[j]);
1386 dac = try_dac(codec, get_primary_out(codec, i), pin);
1388 dac = try_dac(codec, dacs[0], pin);
1390 dac = try_dac(codec, get_primary_out(codec, i), pin);
1393 badness += bad->shared_primary;
1395 badness += bad->shared_surr;
1397 badness += bad->shared_clfe;
1398 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1399 dac = spec->private_dac_nids[0];
1400 badness += bad->shared_surr_main;
1402 badness += bad->no_primary_dac;
1404 badness += bad->no_dac;
1408 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1409 if (!path && !i && spec->mixer_nid) {
1410 /* try with aamix */
1411 path = snd_hda_add_new_path(codec, dac, pin, 0);
1415 badness += bad->no_dac;
1417 /* print_nid_path(codec, "output", path); */
1418 path->active = true;
1419 path_idx[i] = snd_hda_get_path_idx(codec, path);
1420 badness += assign_out_path_ctls(codec, path);
1427 /* return NID if the given pin has only a single connection to a certain DAC */
1428 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1430 struct hda_gen_spec *spec = codec->spec;
1432 hda_nid_t nid_found = 0;
1434 for (i = 0; i < spec->num_all_dacs; i++) {
1435 hda_nid_t nid = spec->all_dacs[i];
1436 if (!nid || is_dac_already_used(codec, nid))
1438 if (is_reachable_path(codec, nid, pin)) {
1447 /* check whether the given pin can be a multi-io pin */
1448 static bool can_be_multiio_pin(struct hda_codec *codec,
1449 unsigned int location, hda_nid_t nid)
1451 unsigned int defcfg, caps;
1453 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1454 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1456 if (location && get_defcfg_location(defcfg) != location)
1458 caps = snd_hda_query_pin_caps(codec, nid);
1459 if (!(caps & AC_PINCAP_OUT))
1464 /* count the number of input pins that are capable to be multi-io */
1465 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1467 struct hda_gen_spec *spec = codec->spec;
1468 struct auto_pin_cfg *cfg = &spec->autocfg;
1469 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1470 unsigned int location = get_defcfg_location(defcfg);
1474 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1475 for (i = 0; i < cfg->num_inputs; i++) {
1476 if (cfg->inputs[i].type != type)
1478 if (can_be_multiio_pin(codec, location,
1479 cfg->inputs[i].pin))
1489 * When hardwired is set, try to fill ony hardwired pins, and returns
1490 * zero if any pins are filled, non-zero if nothing found.
1491 * When hardwired is off, try to fill possible input pins, and returns
1492 * the badness value.
1494 static int fill_multi_ios(struct hda_codec *codec,
1495 hda_nid_t reference_pin,
1498 struct hda_gen_spec *spec = codec->spec;
1499 struct auto_pin_cfg *cfg = &spec->autocfg;
1500 int type, i, j, num_pins, old_pins;
1501 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1502 unsigned int location = get_defcfg_location(defcfg);
1504 struct nid_path *path;
1506 old_pins = spec->multi_ios;
1510 num_pins = count_multiio_pins(codec, reference_pin);
1514 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1515 for (i = 0; i < cfg->num_inputs; i++) {
1516 hda_nid_t nid = cfg->inputs[i].pin;
1519 if (cfg->inputs[i].type != type)
1521 if (!can_be_multiio_pin(codec, location, nid))
1523 for (j = 0; j < spec->multi_ios; j++) {
1524 if (nid == spec->multi_io[j].pin)
1527 if (j < spec->multi_ios)
1531 dac = get_dac_if_single(codec, nid);
1533 dac = look_for_dac(codec, nid, false);
1538 path = snd_hda_add_new_path(codec, dac, nid,
1544 /* print_nid_path(codec, "multiio", path); */
1545 spec->multi_io[spec->multi_ios].pin = nid;
1546 spec->multi_io[spec->multi_ios].dac = dac;
1547 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1548 snd_hda_get_path_idx(codec, path);
1550 if (spec->multi_ios >= 2)
1556 badness = BAD_MULTI_IO;
1557 if (old_pins == spec->multi_ios) {
1559 return 1; /* nothing found */
1561 return badness; /* no badness if nothing found */
1563 if (!hardwired && spec->multi_ios < 2) {
1564 /* cancel newly assigned paths */
1565 spec->paths.used -= spec->multi_ios - old_pins;
1566 spec->multi_ios = old_pins;
1570 /* assign volume and mute controls */
1571 for (i = old_pins; i < spec->multi_ios; i++) {
1572 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1573 badness += assign_out_path_ctls(codec, path);
1579 /* map DACs for all pins in the list if they are single connections */
1580 static bool map_singles(struct hda_codec *codec, int outs,
1581 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1583 struct hda_gen_spec *spec = codec->spec;
1586 for (i = 0; i < outs; i++) {
1587 struct nid_path *path;
1591 dac = get_dac_if_single(codec, pins[i]);
1594 path = snd_hda_add_new_path(codec, dac, pins[i],
1596 if (!path && !i && spec->mixer_nid)
1597 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1601 /* print_nid_path(codec, "output", path); */
1602 path->active = true;
1603 path_idx[i] = snd_hda_get_path_idx(codec, path);
1609 static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1611 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1612 spec->aamix_out_paths[2];
1615 /* create a new path including aamix if available, and return its index */
1616 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1618 struct hda_gen_spec *spec = codec->spec;
1619 struct nid_path *path;
1620 hda_nid_t path_dac, dac, pin;
1622 path = snd_hda_get_path_from_idx(codec, path_idx);
1623 if (!path || !path->depth ||
1624 is_nid_contained(path, spec->mixer_nid))
1626 path_dac = path->path[0];
1627 dac = spec->private_dac_nids[0];
1628 pin = path->path[path->depth - 1];
1629 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1631 if (dac != path_dac)
1633 else if (spec->multiout.hp_out_nid[0])
1634 dac = spec->multiout.hp_out_nid[0];
1635 else if (spec->multiout.extra_out_nid[0])
1636 dac = spec->multiout.extra_out_nid[0];
1640 path = snd_hda_add_new_path(codec, dac, pin,
1645 /* print_nid_path(codec, "output-aamix", path); */
1646 path->active = false; /* unused as default */
1647 path->pin_fixed = true; /* static route */
1648 return snd_hda_get_path_idx(codec, path);
1651 /* check whether the independent HP is available with the current config */
1652 static bool indep_hp_possible(struct hda_codec *codec)
1654 struct hda_gen_spec *spec = codec->spec;
1655 struct auto_pin_cfg *cfg = &spec->autocfg;
1656 struct nid_path *path;
1659 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1660 idx = spec->out_paths[0];
1662 idx = spec->hp_paths[0];
1663 path = snd_hda_get_path_from_idx(codec, idx);
1667 /* assume no path conflicts unless aamix is involved */
1668 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1671 /* check whether output paths contain aamix */
1672 for (i = 0; i < cfg->line_outs; i++) {
1673 if (spec->out_paths[i] == idx)
1675 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1676 if (path && is_nid_contained(path, spec->mixer_nid))
1679 for (i = 0; i < cfg->speaker_outs; i++) {
1680 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1681 if (path && is_nid_contained(path, spec->mixer_nid))
1688 /* fill the empty entries in the dac array for speaker/hp with the
1689 * shared dac pointed by the paths
1691 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1692 hda_nid_t *dacs, int *path_idx)
1694 struct nid_path *path;
1697 for (i = 0; i < num_outs; i++) {
1700 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1703 dacs[i] = path->path[0];
1707 /* fill in the dac_nids table from the parsed pin configuration */
1708 static int fill_and_eval_dacs(struct hda_codec *codec,
1709 bool fill_hardwired,
1710 bool fill_mio_first)
1712 struct hda_gen_spec *spec = codec->spec;
1713 struct auto_pin_cfg *cfg = &spec->autocfg;
1714 int i, err, badness;
1716 /* set num_dacs once to full for look_for_dac() */
1717 spec->multiout.num_dacs = cfg->line_outs;
1718 spec->multiout.dac_nids = spec->private_dac_nids;
1719 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1720 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1721 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1722 spec->multi_ios = 0;
1723 snd_array_free(&spec->paths);
1725 /* clear path indices */
1726 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1727 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1728 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1729 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1730 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1731 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1732 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1733 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1737 /* fill hard-wired DACs first */
1738 if (fill_hardwired) {
1741 mapped = map_singles(codec, cfg->line_outs,
1743 spec->private_dac_nids,
1745 mapped |= map_singles(codec, cfg->hp_outs,
1747 spec->multiout.hp_out_nid,
1749 mapped |= map_singles(codec, cfg->speaker_outs,
1751 spec->multiout.extra_out_nid,
1752 spec->speaker_paths);
1753 if (!spec->no_multi_io &&
1754 fill_mio_first && cfg->line_outs == 1 &&
1755 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1756 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1763 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1764 spec->private_dac_nids, spec->out_paths,
1765 spec->main_out_badness);
1767 if (!spec->no_multi_io && fill_mio_first &&
1768 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1769 /* try to fill multi-io first */
1770 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1773 /* we don't count badness at this stage yet */
1776 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1777 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1778 spec->multiout.hp_out_nid,
1780 spec->extra_out_badness);
1785 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1786 err = try_assign_dacs(codec, cfg->speaker_outs,
1788 spec->multiout.extra_out_nid,
1789 spec->speaker_paths,
1790 spec->extra_out_badness);
1795 if (!spec->no_multi_io &&
1796 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1797 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1803 if (spec->mixer_nid) {
1804 spec->aamix_out_paths[0] =
1805 check_aamix_out_path(codec, spec->out_paths[0]);
1806 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1807 spec->aamix_out_paths[1] =
1808 check_aamix_out_path(codec, spec->hp_paths[0]);
1809 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1810 spec->aamix_out_paths[2] =
1811 check_aamix_out_path(codec, spec->speaker_paths[0]);
1814 if (!spec->no_multi_io &&
1815 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1816 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1817 spec->multi_ios = 1; /* give badness */
1819 /* re-count num_dacs and squash invalid entries */
1820 spec->multiout.num_dacs = 0;
1821 for (i = 0; i < cfg->line_outs; i++) {
1822 if (spec->private_dac_nids[i])
1823 spec->multiout.num_dacs++;
1825 memmove(spec->private_dac_nids + i,
1826 spec->private_dac_nids + i + 1,
1827 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1828 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1832 spec->ext_channel_count = spec->min_channel_count =
1833 spec->multiout.num_dacs * 2;
1835 if (spec->multi_ios == 2) {
1836 for (i = 0; i < 2; i++)
1837 spec->private_dac_nids[spec->multiout.num_dacs++] =
1838 spec->multi_io[i].dac;
1839 } else if (spec->multi_ios) {
1840 spec->multi_ios = 0;
1841 badness += BAD_MULTI_IO;
1844 if (spec->indep_hp && !indep_hp_possible(codec))
1845 badness += BAD_NO_INDEP_HP;
1847 /* re-fill the shared DAC for speaker / headphone */
1848 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1849 refill_shared_dacs(codec, cfg->hp_outs,
1850 spec->multiout.hp_out_nid,
1852 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1853 refill_shared_dacs(codec, cfg->speaker_outs,
1854 spec->multiout.extra_out_nid,
1855 spec->speaker_paths);
1860 #define DEBUG_BADNESS
1862 #ifdef DEBUG_BADNESS
1863 #define debug_badness(fmt, ...) \
1864 codec_dbg(codec, fmt, ##__VA_ARGS__)
1866 #define debug_badness(fmt, ...) \
1867 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1870 #ifdef DEBUG_BADNESS
1871 static inline void print_nid_path_idx(struct hda_codec *codec,
1872 const char *pfx, int idx)
1874 struct nid_path *path;
1876 path = snd_hda_get_path_from_idx(codec, idx);
1878 print_nid_path(codec, pfx, path);
1881 static void debug_show_configs(struct hda_codec *codec,
1882 struct auto_pin_cfg *cfg)
1884 struct hda_gen_spec *spec = codec->spec;
1885 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1888 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1889 cfg->line_out_pins[0], cfg->line_out_pins[1],
1890 cfg->line_out_pins[2], cfg->line_out_pins[3],
1891 spec->multiout.dac_nids[0],
1892 spec->multiout.dac_nids[1],
1893 spec->multiout.dac_nids[2],
1894 spec->multiout.dac_nids[3],
1895 lo_type[cfg->line_out_type]);
1896 for (i = 0; i < cfg->line_outs; i++)
1897 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1898 if (spec->multi_ios > 0)
1899 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1901 spec->multi_io[0].pin, spec->multi_io[1].pin,
1902 spec->multi_io[0].dac, spec->multi_io[1].dac);
1903 for (i = 0; i < spec->multi_ios; i++)
1904 print_nid_path_idx(codec, " mio",
1905 spec->out_paths[cfg->line_outs + i]);
1907 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1908 cfg->hp_pins[0], cfg->hp_pins[1],
1909 cfg->hp_pins[2], cfg->hp_pins[3],
1910 spec->multiout.hp_out_nid[0],
1911 spec->multiout.hp_out_nid[1],
1912 spec->multiout.hp_out_nid[2],
1913 spec->multiout.hp_out_nid[3]);
1914 for (i = 0; i < cfg->hp_outs; i++)
1915 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1916 if (cfg->speaker_outs)
1917 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1918 cfg->speaker_pins[0], cfg->speaker_pins[1],
1919 cfg->speaker_pins[2], cfg->speaker_pins[3],
1920 spec->multiout.extra_out_nid[0],
1921 spec->multiout.extra_out_nid[1],
1922 spec->multiout.extra_out_nid[2],
1923 spec->multiout.extra_out_nid[3]);
1924 for (i = 0; i < cfg->speaker_outs; i++)
1925 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1926 for (i = 0; i < 3; i++)
1927 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1930 #define debug_show_configs(codec, cfg) /* NOP */
1933 /* find all available DACs of the codec */
1934 static void fill_all_dac_nids(struct hda_codec *codec)
1936 struct hda_gen_spec *spec = codec->spec;
1939 spec->num_all_dacs = 0;
1940 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1941 for_each_hda_codec_node(nid, codec) {
1942 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1944 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1945 codec_err(codec, "Too many DACs!\n");
1948 spec->all_dacs[spec->num_all_dacs++] = nid;
1952 static int parse_output_paths(struct hda_codec *codec)
1954 struct hda_gen_spec *spec = codec->spec;
1955 struct auto_pin_cfg *cfg = &spec->autocfg;
1956 struct auto_pin_cfg *best_cfg;
1958 int best_badness = INT_MAX;
1960 bool fill_hardwired = true, fill_mio_first = true;
1961 bool best_wired = true, best_mio = true;
1962 bool hp_spk_swapped = false;
1964 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1970 badness = fill_and_eval_dacs(codec, fill_hardwired,
1976 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1977 cfg->line_out_type, fill_hardwired, fill_mio_first,
1979 debug_show_configs(codec, cfg);
1980 if (badness < best_badness) {
1981 best_badness = badness;
1983 best_wired = fill_hardwired;
1984 best_mio = fill_mio_first;
1988 fill_mio_first = !fill_mio_first;
1989 if (!fill_mio_first)
1991 fill_hardwired = !fill_hardwired;
1992 if (!fill_hardwired)
1996 hp_spk_swapped = true;
1997 if (cfg->speaker_outs > 0 &&
1998 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1999 cfg->hp_outs = cfg->line_outs;
2000 memcpy(cfg->hp_pins, cfg->line_out_pins,
2001 sizeof(cfg->hp_pins));
2002 cfg->line_outs = cfg->speaker_outs;
2003 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2004 sizeof(cfg->speaker_pins));
2005 cfg->speaker_outs = 0;
2006 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2007 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2008 fill_hardwired = true;
2011 if (cfg->hp_outs > 0 &&
2012 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2013 cfg->speaker_outs = cfg->line_outs;
2014 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2015 sizeof(cfg->speaker_pins));
2016 cfg->line_outs = cfg->hp_outs;
2017 memcpy(cfg->line_out_pins, cfg->hp_pins,
2018 sizeof(cfg->hp_pins));
2020 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2021 cfg->line_out_type = AUTO_PIN_HP_OUT;
2022 fill_hardwired = true;
2029 debug_badness("==> restoring best_cfg\n");
2031 fill_and_eval_dacs(codec, best_wired, best_mio);
2033 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2034 cfg->line_out_type, best_wired, best_mio);
2035 debug_show_configs(codec, cfg);
2037 if (cfg->line_out_pins[0]) {
2038 struct nid_path *path;
2039 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
2041 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
2042 if (spec->vmaster_nid) {
2043 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2044 HDA_OUTPUT, spec->vmaster_tlv);
2045 if (spec->dac_min_mute)
2046 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2050 /* set initial pinctl targets */
2051 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2055 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2056 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2057 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2058 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2059 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2060 set_pin_targets(codec, cfg->speaker_outs,
2061 cfg->speaker_pins, val);
2064 /* clear indep_hp flag if not available */
2065 if (spec->indep_hp && !indep_hp_possible(codec))
2072 /* add playback controls from the parsed DAC table */
2073 static int create_multi_out_ctls(struct hda_codec *codec,
2074 const struct auto_pin_cfg *cfg)
2076 struct hda_gen_spec *spec = codec->spec;
2077 int i, err, noutputs;
2079 noutputs = cfg->line_outs;
2080 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2081 noutputs += spec->multi_ios;
2083 for (i = 0; i < noutputs; i++) {
2086 struct nid_path *path;
2088 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
2092 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
2093 if (!name || !strcmp(name, "CLFE")) {
2095 err = add_vol_ctl(codec, "Center", 0, 1, path);
2098 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2102 err = add_stereo_vol(codec, name, index, path);
2107 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2108 if (!name || !strcmp(name, "CLFE")) {
2109 err = add_sw_ctl(codec, "Center", 0, 1, path);
2112 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2116 err = add_stereo_sw(codec, name, index, path);
2124 static int create_extra_out(struct hda_codec *codec, int path_idx,
2125 const char *pfx, int cidx)
2127 struct nid_path *path;
2130 path = snd_hda_get_path_from_idx(codec, path_idx);
2133 err = add_stereo_vol(codec, pfx, cidx, path);
2136 err = add_stereo_sw(codec, pfx, cidx, path);
2142 /* add playback controls for speaker and HP outputs */
2143 static int create_extra_outs(struct hda_codec *codec, int num_pins,
2144 const int *paths, const char *pfx)
2148 for (i = 0; i < num_pins; i++) {
2150 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2153 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2154 name = "Bass Speaker";
2155 else if (num_pins >= 3) {
2156 snprintf(tmp, sizeof(tmp), "%s %s",
2157 pfx, channel_name[i]);
2163 err = create_extra_out(codec, paths[i], name, idx);
2170 static int create_hp_out_ctls(struct hda_codec *codec)
2172 struct hda_gen_spec *spec = codec->spec;
2173 return create_extra_outs(codec, spec->autocfg.hp_outs,
2178 static int create_speaker_out_ctls(struct hda_codec *codec)
2180 struct hda_gen_spec *spec = codec->spec;
2181 return create_extra_outs(codec, spec->autocfg.speaker_outs,
2182 spec->speaker_paths,
2187 * independent HP controls
2190 static void call_hp_automute(struct hda_codec *codec,
2191 struct hda_jack_callback *jack);
2192 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2193 struct snd_ctl_elem_info *uinfo)
2195 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2198 static int indep_hp_get(struct snd_kcontrol *kcontrol,
2199 struct snd_ctl_elem_value *ucontrol)
2201 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2202 struct hda_gen_spec *spec = codec->spec;
2203 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2207 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2208 int nomix_path_idx, int mix_path_idx,
2211 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2212 struct snd_ctl_elem_value *ucontrol)
2214 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2215 struct hda_gen_spec *spec = codec->spec;
2216 unsigned int select = ucontrol->value.enumerated.item[0];
2219 mutex_lock(&spec->pcm_mutex);
2220 if (spec->active_streams) {
2225 if (spec->indep_hp_enabled != select) {
2227 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2228 dacp = &spec->private_dac_nids[0];
2230 dacp = &spec->multiout.hp_out_nid[0];
2232 /* update HP aamix paths in case it conflicts with indep HP */
2233 if (spec->have_aamix_ctl) {
2234 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2235 update_aamix_paths(codec, spec->aamix_mode,
2237 spec->aamix_out_paths[0],
2238 spec->autocfg.line_out_type);
2240 update_aamix_paths(codec, spec->aamix_mode,
2242 spec->aamix_out_paths[1],
2246 spec->indep_hp_enabled = select;
2247 if (spec->indep_hp_enabled)
2250 *dacp = spec->alt_dac_nid;
2252 call_hp_automute(codec, NULL);
2256 mutex_unlock(&spec->pcm_mutex);
2260 static const struct snd_kcontrol_new indep_hp_ctl = {
2261 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2262 .name = "Independent HP",
2263 .info = indep_hp_info,
2264 .get = indep_hp_get,
2265 .put = indep_hp_put,
2269 static int create_indep_hp_ctls(struct hda_codec *codec)
2271 struct hda_gen_spec *spec = codec->spec;
2274 if (!spec->indep_hp)
2276 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2277 dac = spec->multiout.dac_nids[0];
2279 dac = spec->multiout.hp_out_nid[0];
2285 spec->indep_hp_enabled = false;
2286 spec->alt_dac_nid = dac;
2287 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2293 * channel mode enum control
2296 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2297 struct snd_ctl_elem_info *uinfo)
2299 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2300 struct hda_gen_spec *spec = codec->spec;
2303 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2305 uinfo->value.enumerated.items = spec->multi_ios + 1;
2306 if (uinfo->value.enumerated.item > spec->multi_ios)
2307 uinfo->value.enumerated.item = spec->multi_ios;
2308 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2309 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2313 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2314 struct snd_ctl_elem_value *ucontrol)
2316 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2317 struct hda_gen_spec *spec = codec->spec;
2318 ucontrol->value.enumerated.item[0] =
2319 (spec->ext_channel_count - spec->min_channel_count) / 2;
2323 static inline struct nid_path *
2324 get_multiio_path(struct hda_codec *codec, int idx)
2326 struct hda_gen_spec *spec = codec->spec;
2327 return snd_hda_get_path_from_idx(codec,
2328 spec->out_paths[spec->autocfg.line_outs + idx]);
2331 static void update_automute_all(struct hda_codec *codec);
2333 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2334 * used for output paths
2336 static bool aamix_default(struct hda_gen_spec *spec)
2338 return !spec->have_aamix_ctl || spec->aamix_mode;
2341 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2343 struct hda_gen_spec *spec = codec->spec;
2344 hda_nid_t nid = spec->multi_io[idx].pin;
2345 struct nid_path *path;
2347 path = get_multiio_path(codec, idx);
2351 if (path->active == output)
2355 set_pin_target(codec, nid, PIN_OUT, true);
2356 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2357 set_pin_eapd(codec, nid, true);
2359 set_pin_eapd(codec, nid, false);
2360 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2361 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2362 path_power_down_sync(codec, path);
2365 /* update jack retasking in case it modifies any of them */
2366 update_automute_all(codec);
2371 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2372 struct snd_ctl_elem_value *ucontrol)
2374 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2375 struct hda_gen_spec *spec = codec->spec;
2378 ch = ucontrol->value.enumerated.item[0];
2379 if (ch < 0 || ch > spec->multi_ios)
2381 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2383 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2384 for (i = 0; i < spec->multi_ios; i++)
2385 set_multi_io(codec, i, i < ch);
2386 spec->multiout.max_channels = max(spec->ext_channel_count,
2387 spec->const_channel_count);
2388 if (spec->need_dac_fix)
2389 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2393 static const struct snd_kcontrol_new channel_mode_enum = {
2394 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2395 .name = "Channel Mode",
2396 .info = ch_mode_info,
2401 static int create_multi_channel_mode(struct hda_codec *codec)
2403 struct hda_gen_spec *spec = codec->spec;
2405 if (spec->multi_ios > 0) {
2406 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2413 * aamix loopback enable/disable switch
2416 #define loopback_mixing_info indep_hp_info
2418 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2419 struct snd_ctl_elem_value *ucontrol)
2421 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2422 struct hda_gen_spec *spec = codec->spec;
2423 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2427 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2428 int nomix_path_idx, int mix_path_idx,
2431 struct hda_gen_spec *spec = codec->spec;
2432 struct nid_path *nomix_path, *mix_path;
2434 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2435 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2436 if (!nomix_path || !mix_path)
2439 /* if HP aamix path is driven from a different DAC and the
2440 * independent HP mode is ON, can't turn on aamix path
2442 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2443 mix_path->path[0] != spec->alt_dac_nid)
2447 snd_hda_activate_path(codec, nomix_path, false, true);
2448 snd_hda_activate_path(codec, mix_path, true, true);
2449 path_power_down_sync(codec, nomix_path);
2451 snd_hda_activate_path(codec, mix_path, false, false);
2452 snd_hda_activate_path(codec, nomix_path, true, false);
2453 path_power_down_sync(codec, mix_path);
2457 /* re-initialize the output paths; only called from loopback_mixing_put() */
2458 static void update_output_paths(struct hda_codec *codec, int num_outs,
2461 struct hda_gen_spec *spec = codec->spec;
2462 struct nid_path *path;
2465 for (i = 0; i < num_outs; i++) {
2466 path = snd_hda_get_path_from_idx(codec, paths[i]);
2468 snd_hda_activate_path(codec, path, path->active,
2473 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2474 struct snd_ctl_elem_value *ucontrol)
2476 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2477 struct hda_gen_spec *spec = codec->spec;
2478 const struct auto_pin_cfg *cfg = &spec->autocfg;
2479 unsigned int val = ucontrol->value.enumerated.item[0];
2481 if (val == spec->aamix_mode)
2483 spec->aamix_mode = val;
2484 if (has_aamix_out_paths(spec)) {
2485 update_aamix_paths(codec, val, spec->out_paths[0],
2486 spec->aamix_out_paths[0],
2487 cfg->line_out_type);
2488 update_aamix_paths(codec, val, spec->hp_paths[0],
2489 spec->aamix_out_paths[1],
2491 update_aamix_paths(codec, val, spec->speaker_paths[0],
2492 spec->aamix_out_paths[2],
2493 AUTO_PIN_SPEAKER_OUT);
2495 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2496 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2497 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2498 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2499 update_output_paths(codec, cfg->speaker_outs,
2500 spec->speaker_paths);
2505 static const struct snd_kcontrol_new loopback_mixing_enum = {
2506 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2507 .name = "Loopback Mixing",
2508 .info = loopback_mixing_info,
2509 .get = loopback_mixing_get,
2510 .put = loopback_mixing_put,
2513 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2515 struct hda_gen_spec *spec = codec->spec;
2517 if (!spec->mixer_nid)
2519 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2521 spec->have_aamix_ctl = 1;
2522 /* if no explicit aamix path is present (e.g. for Realtek codecs),
2523 * enable aamix as default -- just for compatibility
2525 spec->aamix_mode = !has_aamix_out_paths(spec);
2530 * shared headphone/mic handling
2533 static void call_update_outputs(struct hda_codec *codec);
2535 /* for shared I/O, change the pin-control accordingly */
2536 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2538 struct hda_gen_spec *spec = codec->spec;
2543 pin = spec->hp_mic_pin;
2544 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2547 val = snd_hda_codec_get_pin_target(codec, pin);
2557 val = snd_hda_get_default_vref(codec, pin);
2558 /* if the HP pin doesn't support VREF and the codec driver gives an
2559 * alternative pin, set up the VREF on that pin instead
2561 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2562 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2563 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2564 if (vref_val != AC_PINCTL_VREF_HIZ)
2565 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2566 PIN_IN | (as_mic ? vref_val : 0));
2569 if (!spec->hp_mic_jack_modes) {
2574 set_pin_target(codec, pin, val, true);
2575 call_hp_automute(codec, NULL);
2579 /* create a shared input with the headphone out */
2580 static int create_hp_mic(struct hda_codec *codec)
2582 struct hda_gen_spec *spec = codec->spec;
2583 struct auto_pin_cfg *cfg = &spec->autocfg;
2584 unsigned int defcfg;
2587 if (!spec->hp_mic) {
2588 if (spec->suppress_hp_mic_detect)
2590 /* automatic detection: only if no input or a single internal
2591 * input pin is found, try to detect the shared hp/mic
2593 if (cfg->num_inputs > 1)
2595 else if (cfg->num_inputs == 1) {
2596 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2597 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2602 spec->hp_mic = 0; /* clear once */
2603 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2607 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2608 nid = cfg->line_out_pins[0];
2609 else if (cfg->hp_outs > 0)
2610 nid = cfg->hp_pins[0];
2614 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2615 return 0; /* no input */
2617 cfg->inputs[cfg->num_inputs].pin = nid;
2618 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2619 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2622 spec->hp_mic_pin = nid;
2623 /* we can't handle auto-mic together with HP-mic */
2624 spec->suppress_auto_mic = 1;
2625 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
2633 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2635 static const char * const out_jack_texts[] = {
2636 "Line Out", "Headphone Out",
2639 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2640 struct snd_ctl_elem_info *uinfo)
2642 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2645 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2646 struct snd_ctl_elem_value *ucontrol)
2648 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2649 hda_nid_t nid = kcontrol->private_value;
2650 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2651 ucontrol->value.enumerated.item[0] = 1;
2653 ucontrol->value.enumerated.item[0] = 0;
2657 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2658 struct snd_ctl_elem_value *ucontrol)
2660 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2661 hda_nid_t nid = kcontrol->private_value;
2664 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2665 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2667 snd_hda_set_pin_ctl_cache(codec, nid, val);
2671 static const struct snd_kcontrol_new out_jack_mode_enum = {
2672 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2673 .info = out_jack_mode_info,
2674 .get = out_jack_mode_get,
2675 .put = out_jack_mode_put,
2678 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2680 struct hda_gen_spec *spec = codec->spec;
2683 for (i = 0; i < spec->kctls.used; i++) {
2684 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2685 if (!strcmp(kctl->name, name) && kctl->index == idx)
2691 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2692 char *name, size_t name_len)
2694 struct hda_gen_spec *spec = codec->spec;
2697 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2698 strlcat(name, " Jack Mode", name_len);
2700 for (; find_kctl_name(codec, name, idx); idx++)
2704 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2706 struct hda_gen_spec *spec = codec->spec;
2707 if (spec->add_jack_modes) {
2708 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2709 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2715 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2718 struct hda_gen_spec *spec = codec->spec;
2721 for (i = 0; i < num_pins; i++) {
2722 hda_nid_t pin = pins[i];
2723 if (pin == spec->hp_mic_pin)
2725 if (get_out_jack_num_items(codec, pin) > 1) {
2726 struct snd_kcontrol_new *knew;
2727 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2728 get_jack_mode_name(codec, pin, name, sizeof(name));
2729 knew = snd_hda_gen_add_kctl(spec, name,
2730 &out_jack_mode_enum);
2733 knew->private_value = pin;
2744 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2747 static const char * const vref_texts[NUM_VREFS] = {
2748 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2749 "", "Mic 80pc Bias", "Mic 100pc Bias"
2752 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2754 unsigned int pincap;
2756 pincap = snd_hda_query_pin_caps(codec, pin);
2757 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2758 /* filter out unusual vrefs */
2759 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2763 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2764 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2766 unsigned int i, n = 0;
2768 for (i = 0; i < NUM_VREFS; i++) {
2769 if (vref_caps & (1 << i)) {
2778 /* convert back from the vref ctl index to the enum item index */
2779 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2781 unsigned int i, n = 0;
2783 for (i = 0; i < NUM_VREFS; i++) {
2786 if (vref_caps & (1 << i))
2792 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2793 struct snd_ctl_elem_info *uinfo)
2795 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2796 hda_nid_t nid = kcontrol->private_value;
2797 unsigned int vref_caps = get_vref_caps(codec, nid);
2799 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2801 /* set the right text */
2802 strcpy(uinfo->value.enumerated.name,
2803 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2807 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2808 struct snd_ctl_elem_value *ucontrol)
2810 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2811 hda_nid_t nid = kcontrol->private_value;
2812 unsigned int vref_caps = get_vref_caps(codec, nid);
2815 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2816 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2820 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2821 struct snd_ctl_elem_value *ucontrol)
2823 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2824 hda_nid_t nid = kcontrol->private_value;
2825 unsigned int vref_caps = get_vref_caps(codec, nid);
2826 unsigned int val, idx;
2828 val = snd_hda_codec_get_pin_target(codec, nid);
2829 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2830 if (idx == ucontrol->value.enumerated.item[0])
2833 val &= ~AC_PINCTL_VREFEN;
2834 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2835 snd_hda_set_pin_ctl_cache(codec, nid, val);
2839 static const struct snd_kcontrol_new in_jack_mode_enum = {
2840 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2841 .info = in_jack_mode_info,
2842 .get = in_jack_mode_get,
2843 .put = in_jack_mode_put,
2846 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2848 struct hda_gen_spec *spec = codec->spec;
2850 if (spec->add_jack_modes)
2851 nitems = hweight32(get_vref_caps(codec, pin));
2852 return nitems ? nitems : 1;
2855 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2857 struct hda_gen_spec *spec = codec->spec;
2858 struct snd_kcontrol_new *knew;
2859 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2860 unsigned int defcfg;
2862 if (pin == spec->hp_mic_pin)
2863 return 0; /* already done in create_out_jack_mode() */
2865 /* no jack mode for fixed pins */
2866 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2867 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2870 /* no multiple vref caps? */
2871 if (get_in_jack_num_items(codec, pin) <= 1)
2874 get_jack_mode_name(codec, pin, name, sizeof(name));
2875 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2878 knew->private_value = pin;
2883 * HP/mic shared jack mode
2885 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2886 struct snd_ctl_elem_info *uinfo)
2888 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2889 hda_nid_t nid = kcontrol->private_value;
2890 int out_jacks = get_out_jack_num_items(codec, nid);
2891 int in_jacks = get_in_jack_num_items(codec, nid);
2892 const char *text = NULL;
2895 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2897 uinfo->value.enumerated.items = out_jacks + in_jacks;
2898 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2899 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2900 idx = uinfo->value.enumerated.item;
2901 if (idx < out_jacks) {
2903 text = out_jack_texts[idx];
2905 text = "Headphone Out";
2909 unsigned int vref_caps = get_vref_caps(codec, nid);
2910 text = vref_texts[get_vref_idx(vref_caps, idx)];
2915 strcpy(uinfo->value.enumerated.name, text);
2919 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2921 int out_jacks = get_out_jack_num_items(codec, nid);
2922 int in_jacks = get_in_jack_num_items(codec, nid);
2923 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2926 if (val & PIN_OUT) {
2927 if (out_jacks > 1 && val == PIN_HP)
2929 } else if (val & PIN_IN) {
2932 unsigned int vref_caps = get_vref_caps(codec, nid);
2933 val &= AC_PINCTL_VREFEN;
2934 idx += cvt_from_vref_idx(vref_caps, val);
2940 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2941 struct snd_ctl_elem_value *ucontrol)
2943 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2944 hda_nid_t nid = kcontrol->private_value;
2945 ucontrol->value.enumerated.item[0] =
2946 get_cur_hp_mic_jack_mode(codec, nid);
2950 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2951 struct snd_ctl_elem_value *ucontrol)
2953 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2954 hda_nid_t nid = kcontrol->private_value;
2955 int out_jacks = get_out_jack_num_items(codec, nid);
2956 int in_jacks = get_in_jack_num_items(codec, nid);
2957 unsigned int val, oldval, idx;
2959 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2960 idx = ucontrol->value.enumerated.item[0];
2964 if (idx < out_jacks) {
2966 val = idx ? PIN_HP : PIN_OUT;
2972 unsigned int vref_caps = get_vref_caps(codec, nid);
2973 val = snd_hda_codec_get_pin_target(codec, nid);
2974 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2975 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
2977 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
2979 snd_hda_set_pin_ctl_cache(codec, nid, val);
2980 call_hp_automute(codec, NULL);
2985 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2986 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2987 .info = hp_mic_jack_mode_info,
2988 .get = hp_mic_jack_mode_get,
2989 .put = hp_mic_jack_mode_put,
2992 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2994 struct hda_gen_spec *spec = codec->spec;
2995 struct snd_kcontrol_new *knew;
2997 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2998 &hp_mic_jack_mode_enum);
3001 knew->private_value = pin;
3002 spec->hp_mic_jack_modes = 1;
3010 /* add the powersave loopback-list entry */
3011 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
3013 struct hda_amp_list *list;
3015 list = snd_array_new(&spec->loopback_list);
3019 list->dir = HDA_INPUT;
3021 spec->loopback.amplist = spec->loopback_list.list;
3025 /* return true if either a volume or a mute amp is found for the given
3026 * aamix path; the amp has to be either in the mixer node or its direct leaf
3028 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3029 hda_nid_t pin, unsigned int *mix_val,
3030 unsigned int *mute_val)
3033 const hda_nid_t *list;
3036 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3040 *mix_val = *mute_val = 0;
3041 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3042 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3043 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3044 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3045 if (*mix_val && *mute_val)
3048 /* check leaf node */
3049 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3050 if (num_conns < idx)
3053 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3054 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
3055 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3056 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3057 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
3058 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3060 return *mix_val || *mute_val;
3063 /* create input playback/capture controls for the given pin */
3064 static int new_analog_input(struct hda_codec *codec, int input_idx,
3065 hda_nid_t pin, const char *ctlname, int ctlidx,
3068 struct hda_gen_spec *spec = codec->spec;
3069 struct nid_path *path;
3070 unsigned int mix_val, mute_val;
3073 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3076 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
3079 print_nid_path(codec, "loopback", path);
3080 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
3082 idx = path->idx[path->depth - 1];
3084 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
3087 path->ctls[NID_PATH_VOL_CTL] = mix_val;
3091 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
3094 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
3097 path->active = true;
3098 path->stream_enabled = true; /* no DAC/ADC involved */
3099 err = add_loopback_list(spec, mix_nid, idx);
3103 if (spec->mixer_nid != spec->mixer_merge_nid &&
3104 !spec->loopback_merge_path) {
3105 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3106 spec->mixer_merge_nid, 0);
3108 print_nid_path(codec, "loopback-merge", path);
3109 path->active = true;
3110 path->pin_fixed = true; /* static route */
3111 path->stream_enabled = true; /* no DAC/ADC involved */
3112 spec->loopback_merge_path =
3113 snd_hda_get_path_idx(codec, path);
3120 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3122 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3123 return (pincap & AC_PINCAP_IN) != 0;
3126 /* Parse the codec tree and retrieve ADCs */
3127 static int fill_adc_nids(struct hda_codec *codec)
3129 struct hda_gen_spec *spec = codec->spec;
3131 hda_nid_t *adc_nids = spec->adc_nids;
3132 int max_nums = ARRAY_SIZE(spec->adc_nids);
3135 for_each_hda_codec_node(nid, codec) {
3136 unsigned int caps = get_wcaps(codec, nid);
3137 int type = get_wcaps_type(caps);
3139 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3141 adc_nids[nums] = nid;
3142 if (++nums >= max_nums)
3145 spec->num_adc_nids = nums;
3147 /* copy the detected ADCs to all_adcs[] */
3148 spec->num_all_adcs = nums;
3149 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3154 /* filter out invalid adc_nids that don't give all active input pins;
3155 * if needed, check whether dynamic ADC-switching is available
3157 static int check_dyn_adc_switch(struct hda_codec *codec)
3159 struct hda_gen_spec *spec = codec->spec;
3160 struct hda_input_mux *imux = &spec->input_mux;
3161 unsigned int ok_bits;
3166 for (n = 0; n < spec->num_adc_nids; n++) {
3167 for (i = 0; i < imux->num_items; i++) {
3168 if (!spec->input_paths[i][n])
3171 if (i >= imux->num_items) {
3172 ok_bits |= (1 << n);
3178 /* check whether ADC-switch is possible */
3179 for (i = 0; i < imux->num_items; i++) {
3180 for (n = 0; n < spec->num_adc_nids; n++) {
3181 if (spec->input_paths[i][n]) {
3182 spec->dyn_adc_idx[i] = n;
3188 codec_dbg(codec, "enabling ADC switching\n");
3189 spec->dyn_adc_switch = 1;
3190 } else if (nums != spec->num_adc_nids) {
3191 /* shrink the invalid adcs and input paths */
3193 for (n = 0; n < spec->num_adc_nids; n++) {
3194 if (!(ok_bits & (1 << n)))
3197 spec->adc_nids[nums] = spec->adc_nids[n];
3198 for (i = 0; i < imux->num_items; i++) {
3199 invalidate_nid_path(codec,
3200 spec->input_paths[i][nums]);
3201 spec->input_paths[i][nums] =
3202 spec->input_paths[i][n];
3203 spec->input_paths[i][n] = 0;
3208 spec->num_adc_nids = nums;
3211 if (imux->num_items == 1 ||
3212 (imux->num_items == 2 && spec->hp_mic)) {
3213 codec_dbg(codec, "reducing to a single ADC\n");
3214 spec->num_adc_nids = 1; /* reduce to a single ADC */
3217 /* single index for individual volumes ctls */
3218 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3219 spec->num_adc_nids = 1;
3224 /* parse capture source paths from the given pin and create imux items */
3225 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3226 int cfg_idx, int num_adcs,
3227 const char *label, int anchor)
3229 struct hda_gen_spec *spec = codec->spec;
3230 struct hda_input_mux *imux = &spec->input_mux;
3231 int imux_idx = imux->num_items;
3232 bool imux_added = false;
3235 for (c = 0; c < num_adcs; c++) {
3236 struct nid_path *path;
3237 hda_nid_t adc = spec->adc_nids[c];
3239 if (!is_reachable_path(codec, pin, adc))
3241 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3244 print_nid_path(codec, "input", path);
3245 spec->input_paths[imux_idx][c] =
3246 snd_hda_get_path_idx(codec, path);
3249 if (spec->hp_mic_pin == pin)
3250 spec->hp_mic_mux_idx = imux->num_items;
3251 spec->imux_pins[imux->num_items] = pin;
3252 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3254 if (spec->dyn_adc_switch)
3255 spec->dyn_adc_idx[imux_idx] = c;
3263 * create playback/capture controls for input pins
3266 /* fill the label for each input at first */
3267 static int fill_input_pin_labels(struct hda_codec *codec)
3269 struct hda_gen_spec *spec = codec->spec;
3270 const struct auto_pin_cfg *cfg = &spec->autocfg;
3273 for (i = 0; i < cfg->num_inputs; i++) {
3274 hda_nid_t pin = cfg->inputs[i].pin;
3278 if (!is_input_pin(codec, pin))
3281 label = hda_get_autocfg_input_label(codec, cfg, i);
3283 for (j = i - 1; j >= 0; j--) {
3284 if (spec->input_labels[j] &&
3285 !strcmp(spec->input_labels[j], label)) {
3286 idx = spec->input_label_idxs[j] + 1;
3291 spec->input_labels[i] = label;
3292 spec->input_label_idxs[i] = idx;
3298 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3300 static int create_input_ctls(struct hda_codec *codec)
3302 struct hda_gen_spec *spec = codec->spec;
3303 const struct auto_pin_cfg *cfg = &spec->autocfg;
3304 hda_nid_t mixer = spec->mixer_nid;
3309 num_adcs = fill_adc_nids(codec);
3313 err = fill_input_pin_labels(codec);
3317 for (i = 0; i < cfg->num_inputs; i++) {
3320 pin = cfg->inputs[i].pin;
3321 if (!is_input_pin(codec, pin))
3325 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3326 val |= snd_hda_get_default_vref(codec, pin);
3327 if (pin != spec->hp_mic_pin &&
3328 !snd_hda_codec_get_pin_target(codec, pin))
3329 set_pin_target(codec, pin, val, false);
3332 if (is_reachable_path(codec, pin, mixer)) {
3333 err = new_analog_input(codec, i, pin,
3334 spec->input_labels[i],
3335 spec->input_label_idxs[i],
3342 err = parse_capture_source(codec, pin, i, num_adcs,
3343 spec->input_labels[i], -mixer);
3347 if (spec->add_jack_modes) {
3348 err = create_in_jack_mode(codec, pin);
3354 /* add stereo mix when explicitly enabled via hint */
3355 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3356 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3361 spec->suppress_auto_mic = 1;
3372 /* get the input path specified by the given adc and imux indices */
3373 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3375 struct hda_gen_spec *spec = codec->spec;
3376 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3380 if (spec->dyn_adc_switch)
3381 adc_idx = spec->dyn_adc_idx[imux_idx];
3382 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3386 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3389 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3392 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3393 struct snd_ctl_elem_info *uinfo)
3395 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3396 struct hda_gen_spec *spec = codec->spec;
3397 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3400 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3401 struct snd_ctl_elem_value *ucontrol)
3403 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3404 struct hda_gen_spec *spec = codec->spec;
3405 /* the ctls are created at once with multiple counts */
3406 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3408 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3412 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3413 struct snd_ctl_elem_value *ucontrol)
3415 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3416 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3417 return mux_select(codec, adc_idx,
3418 ucontrol->value.enumerated.item[0]);
3421 static const struct snd_kcontrol_new cap_src_temp = {
3422 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3423 .name = "Input Source",
3424 .info = mux_enum_info,
3425 .get = mux_enum_get,
3426 .put = mux_enum_put,
3430 * capture volume and capture switch ctls
3433 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3434 struct snd_ctl_elem_value *ucontrol);
3436 /* call the given amp update function for all amps in the imux list at once */
3437 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3438 struct snd_ctl_elem_value *ucontrol,
3439 put_call_t func, int type)
3441 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3442 struct hda_gen_spec *spec = codec->spec;
3443 const struct hda_input_mux *imux;
3444 struct nid_path *path;
3445 int i, adc_idx, ret, err = 0;
3447 imux = &spec->input_mux;
3448 adc_idx = kcontrol->id.index;
3449 mutex_lock(&codec->control_mutex);
3450 for (i = 0; i < imux->num_items; i++) {
3451 path = get_input_path(codec, adc_idx, i);
3452 if (!path || !path->ctls[type])
3454 kcontrol->private_value = path->ctls[type];
3455 ret = func(kcontrol, ucontrol);
3463 mutex_unlock(&codec->control_mutex);
3464 if (err >= 0 && spec->cap_sync_hook)
3465 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3469 /* capture volume ctl callbacks */
3470 #define cap_vol_info snd_hda_mixer_amp_volume_info
3471 #define cap_vol_get snd_hda_mixer_amp_volume_get
3472 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3474 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3475 struct snd_ctl_elem_value *ucontrol)
3477 return cap_put_caller(kcontrol, ucontrol,
3478 snd_hda_mixer_amp_volume_put,
3482 static const struct snd_kcontrol_new cap_vol_temp = {
3483 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3484 .name = "Capture Volume",
3485 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3486 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3487 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3488 .info = cap_vol_info,
3491 .tlv = { .c = cap_vol_tlv },
3494 /* capture switch ctl callbacks */
3495 #define cap_sw_info snd_ctl_boolean_stereo_info
3496 #define cap_sw_get snd_hda_mixer_amp_switch_get
3498 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3499 struct snd_ctl_elem_value *ucontrol)
3501 return cap_put_caller(kcontrol, ucontrol,
3502 snd_hda_mixer_amp_switch_put,
3506 static const struct snd_kcontrol_new cap_sw_temp = {
3507 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3508 .name = "Capture Switch",
3509 .info = cap_sw_info,
3514 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3519 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3520 for (depth = 0; depth < 3; depth++) {
3521 if (depth >= path->depth)
3523 i = path->depth - depth - 1;
3524 nid = path->path[i];
3525 if (!path->ctls[NID_PATH_VOL_CTL]) {
3526 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3527 path->ctls[NID_PATH_VOL_CTL] =
3528 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3529 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3530 int idx = path->idx[i];
3531 if (!depth && codec->single_adc_amp)
3533 path->ctls[NID_PATH_VOL_CTL] =
3534 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3537 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3538 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3539 path->ctls[NID_PATH_MUTE_CTL] =
3540 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3541 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3542 int idx = path->idx[i];
3543 if (!depth && codec->single_adc_amp)
3545 path->ctls[NID_PATH_MUTE_CTL] =
3546 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3553 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3555 struct hda_gen_spec *spec = codec->spec;
3556 struct auto_pin_cfg *cfg = &spec->autocfg;
3560 if (!spec->inv_dmic_split)
3562 for (i = 0; i < cfg->num_inputs; i++) {
3563 if (cfg->inputs[i].pin != nid)
3565 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3567 val = snd_hda_codec_get_pincfg(codec, nid);
3568 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3573 /* capture switch put callback for a single control with hook call */
3574 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3575 struct snd_ctl_elem_value *ucontrol)
3577 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3578 struct hda_gen_spec *spec = codec->spec;
3581 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3585 if (spec->cap_sync_hook)
3586 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3591 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3592 int idx, bool is_switch, unsigned int ctl,
3595 struct hda_gen_spec *spec = codec->spec;
3596 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3597 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3598 const char *sfx = is_switch ? "Switch" : "Volume";
3599 unsigned int chs = inv_dmic ? 1 : 3;
3600 struct snd_kcontrol_new *knew;
3606 snprintf(tmpname, sizeof(tmpname),
3607 "%s Capture %s", label, sfx);
3609 snprintf(tmpname, sizeof(tmpname),
3611 knew = add_control(spec, type, tmpname, idx,
3612 amp_val_replace_channels(ctl, chs));
3616 knew->put = cap_single_sw_put;
3620 /* Make independent right kcontrol */
3622 snprintf(tmpname, sizeof(tmpname),
3623 "Inverted %s Capture %s", label, sfx);
3625 snprintf(tmpname, sizeof(tmpname),
3626 "Inverted Capture %s", sfx);
3627 knew = add_control(spec, type, tmpname, idx,
3628 amp_val_replace_channels(ctl, 2));
3632 knew->put = cap_single_sw_put;
3636 /* create single (and simple) capture volume and switch controls */
3637 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3638 unsigned int vol_ctl, unsigned int sw_ctl,
3642 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3645 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3651 /* create bound capture volume and switch controls */
3652 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3653 unsigned int vol_ctl, unsigned int sw_ctl)
3655 struct hda_gen_spec *spec = codec->spec;
3656 struct snd_kcontrol_new *knew;
3659 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3663 knew->private_value = vol_ctl;
3664 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3667 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3671 knew->private_value = sw_ctl;
3672 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3677 /* return the vol ctl when used first in the imux list */
3678 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3680 struct nid_path *path;
3684 path = get_input_path(codec, 0, idx);
3687 ctl = path->ctls[type];
3690 for (i = 0; i < idx - 1; i++) {
3691 path = get_input_path(codec, 0, i);
3692 if (path && path->ctls[type] == ctl)
3698 /* create individual capture volume and switch controls per input */
3699 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3701 struct hda_gen_spec *spec = codec->spec;
3702 struct hda_input_mux *imux = &spec->input_mux;
3705 for (i = 0; i < imux->num_items; i++) {
3709 idx = imux->items[i].index;
3710 if (idx >= spec->autocfg.num_inputs)
3712 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3714 for (type = 0; type < 2; type++) {
3715 err = add_single_cap_ctl(codec,
3716 spec->input_labels[idx],
3717 spec->input_label_idxs[idx],
3719 get_first_cap_ctl(codec, i, type),
3728 static int create_capture_mixers(struct hda_codec *codec)
3730 struct hda_gen_spec *spec = codec->spec;
3731 struct hda_input_mux *imux = &spec->input_mux;
3732 int i, n, nums, err;
3734 if (spec->dyn_adc_switch)
3737 nums = spec->num_adc_nids;
3739 if (!spec->auto_mic && imux->num_items > 1) {
3740 struct snd_kcontrol_new *knew;
3742 name = nums > 1 ? "Input Source" : "Capture Source";
3743 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3749 for (n = 0; n < nums; n++) {
3751 bool multi_cap_vol = spec->multi_cap_vol;
3752 bool inv_dmic = false;
3756 for (i = 0; i < imux->num_items; i++) {
3757 struct nid_path *path;
3758 path = get_input_path(codec, n, i);
3761 parse_capvol_in_path(codec, path);
3763 vol = path->ctls[NID_PATH_VOL_CTL];
3764 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3766 if (!same_amp_caps(codec, vol,
3767 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3768 multi_cap_vol = true;
3771 sw = path->ctls[NID_PATH_MUTE_CTL];
3772 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3774 if (!same_amp_caps(codec, sw,
3775 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3776 multi_cap_vol = true;
3778 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3783 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3785 else if (!multi_cap_vol && !inv_dmic)
3786 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3788 err = create_multi_cap_vol_ctl(codec);
3797 * add mic boosts if needed
3800 /* check whether the given amp is feasible as a boost volume */
3801 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3806 if (!nid_has_volume(codec, nid, dir) ||
3807 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3808 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3811 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3812 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3818 /* look for a boost amp in a widget close to the pin */
3819 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3820 struct nid_path *path)
3822 unsigned int val = 0;
3826 for (depth = 0; depth < 3; depth++) {
3827 if (depth >= path->depth - 1)
3829 nid = path->path[depth];
3830 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3831 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3833 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3834 path->idx[depth])) {
3835 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3844 static int parse_mic_boost(struct hda_codec *codec)
3846 struct hda_gen_spec *spec = codec->spec;
3847 struct auto_pin_cfg *cfg = &spec->autocfg;
3848 struct hda_input_mux *imux = &spec->input_mux;
3851 if (!spec->num_adc_nids)
3854 for (i = 0; i < imux->num_items; i++) {
3855 struct nid_path *path;
3858 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3860 idx = imux->items[i].index;
3861 if (idx >= imux->num_items)
3864 /* check only line-in and mic pins */
3865 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3868 path = get_input_path(codec, 0, i);
3872 val = look_for_boost_amp(codec, path);
3876 /* create a boost control */
3877 snprintf(boost_label, sizeof(boost_label),
3878 "%s Boost Volume", spec->input_labels[idx]);
3879 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3880 spec->input_label_idxs[idx], val))
3883 path->ctls[NID_PATH_BOOST_CTL] = val;
3889 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3891 static void parse_digital(struct hda_codec *codec)
3893 struct hda_gen_spec *spec = codec->spec;
3894 struct nid_path *path;
3896 hda_nid_t dig_nid, pin;
3898 /* support multiple SPDIFs; the secondary is set up as a slave */
3900 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3901 pin = spec->autocfg.dig_out_pins[i];
3902 dig_nid = look_for_dac(codec, pin, true);
3905 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3908 print_nid_path(codec, "digout", path);
3909 path->active = true;
3910 path->pin_fixed = true; /* no jack detection */
3911 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3912 set_pin_target(codec, pin, PIN_OUT, false);
3914 spec->multiout.dig_out_nid = dig_nid;
3915 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3917 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3918 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3920 spec->slave_dig_outs[nums - 1] = dig_nid;
3925 if (spec->autocfg.dig_in_pin) {
3926 pin = spec->autocfg.dig_in_pin;
3927 for_each_hda_codec_node(dig_nid, codec) {
3928 unsigned int wcaps = get_wcaps(codec, dig_nid);
3929 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3931 if (!(wcaps & AC_WCAP_DIGITAL))
3933 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3935 print_nid_path(codec, "digin", path);
3936 path->active = true;
3937 path->pin_fixed = true; /* no jack */
3938 spec->dig_in_nid = dig_nid;
3939 spec->digin_path = snd_hda_get_path_idx(codec, path);
3940 set_pin_target(codec, pin, PIN_IN, false);
3949 * input MUX handling
3952 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3954 /* select the given imux item; either unmute exclusively or select the route */
3955 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3958 struct hda_gen_spec *spec = codec->spec;
3959 const struct hda_input_mux *imux;
3960 struct nid_path *old_path, *path;
3962 imux = &spec->input_mux;
3963 if (!imux->num_items)
3966 if (idx >= imux->num_items)
3967 idx = imux->num_items - 1;
3968 if (spec->cur_mux[adc_idx] == idx)
3971 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3974 if (old_path->active)
3975 snd_hda_activate_path(codec, old_path, false, false);
3977 spec->cur_mux[adc_idx] = idx;
3980 update_hp_mic(codec, adc_idx, false);
3982 if (spec->dyn_adc_switch)
3983 dyn_adc_pcm_resetup(codec, idx);
3985 path = get_input_path(codec, adc_idx, idx);
3990 snd_hda_activate_path(codec, path, true, false);
3991 if (spec->cap_sync_hook)
3992 spec->cap_sync_hook(codec, NULL, NULL);
3993 path_power_down_sync(codec, old_path);
3997 /* power up/down widgets in the all paths that match with the given NID
3998 * as terminals (either start- or endpoint)
4000 * returns the last changed NID, or zero if unchanged.
4002 static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
4003 int pin_state, int stream_state)
4005 struct hda_gen_spec *spec = codec->spec;
4006 hda_nid_t last, changed = 0;
4007 struct nid_path *path;
4010 for (n = 0; n < spec->paths.used; n++) {
4011 path = snd_array_elem(&spec->paths, n);
4014 if (path->path[0] == nid ||
4015 path->path[path->depth - 1] == nid) {
4016 bool pin_old = path->pin_enabled;
4017 bool stream_old = path->stream_enabled;
4020 path->pin_enabled = pin_state;
4021 if (stream_state >= 0)
4022 path->stream_enabled = stream_state;
4023 if ((!path->pin_fixed && path->pin_enabled != pin_old)
4024 || path->stream_enabled != stream_old) {
4025 last = path_power_update(codec, path, true);
4034 /* check the jack status for power control */
4035 static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4037 if (!is_jack_detectable(codec, pin))
4039 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4042 /* power up/down the paths of the given pin according to the jack state;
4043 * power = 0/1 : only power up/down if it matches with the jack state,
4044 * < 0 : force power up/down to follow the jack sate
4046 * returns the last changed NID, or zero if unchanged.
4048 static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4053 if (!codec->power_save_node)
4056 on = detect_pin_state(codec, pin);
4058 if (power >= 0 && on != power)
4060 return set_path_power(codec, pin, on, -1);
4063 static void pin_power_callback(struct hda_codec *codec,
4064 struct hda_jack_callback *jack,
4067 if (jack && jack->nid)
4068 sync_power_state_change(codec,
4069 set_pin_power_jack(codec, jack->nid, on));
4072 /* callback only doing power up -- called at first */
4073 static void pin_power_up_callback(struct hda_codec *codec,
4074 struct hda_jack_callback *jack)
4076 pin_power_callback(codec, jack, true);
4079 /* callback only doing power down -- called at last */
4080 static void pin_power_down_callback(struct hda_codec *codec,
4081 struct hda_jack_callback *jack)
4083 pin_power_callback(codec, jack, false);
4086 /* set up the power up/down callbacks */
4087 static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4088 const hda_nid_t *pins, bool on)
4091 hda_jack_callback_fn cb =
4092 on ? pin_power_up_callback : pin_power_down_callback;
4094 for (i = 0; i < num_pins && pins[i]; i++) {
4095 if (is_jack_detectable(codec, pins[i]))
4096 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4098 set_path_power(codec, pins[i], true, -1);
4102 /* enabled power callback to each available I/O pin with jack detections;
4103 * the digital I/O pins are excluded because of the unreliable detectsion
4105 static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4107 struct hda_gen_spec *spec = codec->spec;
4108 struct auto_pin_cfg *cfg = &spec->autocfg;
4111 if (!codec->power_save_node)
4113 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4114 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4115 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4116 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4117 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4118 for (i = 0; i < cfg->num_inputs; i++)
4119 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4122 /* sync path power up/down with the jack states of given pins */
4123 static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4124 const hda_nid_t *pins)
4128 for (i = 0; i < num_pins && pins[i]; i++)
4129 if (is_jack_detectable(codec, pins[i]))
4130 set_pin_power_jack(codec, pins[i], -1);
4133 /* sync path power up/down with pins; called at init and resume */
4134 static void sync_all_pin_power_ctls(struct hda_codec *codec)
4136 struct hda_gen_spec *spec = codec->spec;
4137 struct auto_pin_cfg *cfg = &spec->autocfg;
4140 if (!codec->power_save_node)
4142 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4143 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4144 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4145 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4146 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4147 for (i = 0; i < cfg->num_inputs; i++)
4148 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4151 /* add fake paths if not present yet */
4152 static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4153 int num_pins, const hda_nid_t *pins)
4155 struct hda_gen_spec *spec = codec->spec;
4156 struct nid_path *path;
4159 for (i = 0; i < num_pins; i++) {
4162 if (get_nid_path(codec, nid, pins[i], 0))
4164 path = snd_array_new(&spec->paths);
4167 memset(path, 0, sizeof(*path));
4169 path->path[0] = nid;
4170 path->path[1] = pins[i];
4171 path->active = true;
4176 /* create fake paths to all outputs from beep */
4177 static int add_fake_beep_paths(struct hda_codec *codec)
4179 struct hda_gen_spec *spec = codec->spec;
4180 struct auto_pin_cfg *cfg = &spec->autocfg;
4181 hda_nid_t nid = spec->beep_nid;
4184 if (!codec->power_save_node || !nid)
4186 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4189 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4190 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4194 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4195 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4203 /* power up/down beep widget and its output paths */
4204 static void beep_power_hook(struct hda_beep *beep, bool on)
4206 set_path_power(beep->codec, beep->nid, -1, on);
4210 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4211 * @codec: the HDA codec
4212 * @pin: NID of pin to fix
4214 int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4216 struct hda_gen_spec *spec = codec->spec;
4217 struct nid_path *path;
4219 path = snd_array_new(&spec->paths);
4222 memset(path, 0, sizeof(*path));
4224 path->path[0] = pin;
4225 path->active = true;
4226 path->pin_fixed = true;
4227 path->stream_enabled = true;
4230 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4233 * Jack detections for HP auto-mute and mic-switch
4236 /* check each pin in the given array; returns true if any of them is plugged */
4237 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4240 bool present = false;
4242 for (i = 0; i < num_pins; i++) {
4243 hda_nid_t nid = pins[i];
4246 /* don't detect pins retasked as inputs */
4247 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4249 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4255 /* standard HP/line-out auto-mute helper */
4256 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
4257 int *paths, bool mute)
4259 struct hda_gen_spec *spec = codec->spec;
4262 for (i = 0; i < num_pins; i++) {
4263 hda_nid_t nid = pins[i];
4264 unsigned int val, oldval;
4268 oldval = snd_hda_codec_get_pin_target(codec, nid);
4269 if (oldval & PIN_IN)
4270 continue; /* no mute for inputs */
4272 if (spec->auto_mute_via_amp) {
4273 struct nid_path *path;
4276 path = snd_hda_get_path_from_idx(codec, paths[i]);
4279 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4283 spec->mute_bits |= (1ULL << mute_nid);
4285 spec->mute_bits &= ~(1ULL << mute_nid);
4288 /* don't reset VREF value in case it's controlling
4289 * the amp (see alc861_fixup_asus_amp_vref_0f())
4291 if (spec->keep_vref_in_automute)
4292 val = oldval & ~PIN_HP;
4297 /* here we call update_pin_ctl() so that the pinctl is
4298 * changed without changing the pinctl target value;
4299 * the original target value will be still referred at
4300 * the init / resume again
4302 update_pin_ctl(codec, nid, val);
4305 set_pin_eapd(codec, nid, !mute);
4306 if (codec->power_save_node) {
4309 on = detect_pin_state(codec, nid);
4310 set_path_power(codec, nid, on, -1);
4316 * snd_hda_gen_update_outputs - Toggle outputs muting
4317 * @codec: the HDA codec
4319 * Update the mute status of all outputs based on the current jack states.
4321 void snd_hda_gen_update_outputs(struct hda_codec *codec)
4323 struct hda_gen_spec *spec = codec->spec;
4327 /* Control HP pins/amps depending on master_mute state;
4328 * in general, HP pins/amps control should be enabled in all cases,
4329 * but currently set only for master_mute, just to be safe
4331 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4332 paths = spec->out_paths;
4334 paths = spec->hp_paths;
4335 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
4336 spec->autocfg.hp_pins, paths, spec->master_mute);
4338 if (!spec->automute_speaker)
4341 on = spec->hp_jack_present | spec->line_jack_present;
4342 on |= spec->master_mute;
4343 spec->speaker_muted = on;
4344 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4345 paths = spec->out_paths;
4347 paths = spec->speaker_paths;
4348 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
4349 spec->autocfg.speaker_pins, paths, on);
4351 /* toggle line-out mutes if needed, too */
4352 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4353 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4354 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4356 if (!spec->automute_lo)
4359 on = spec->hp_jack_present;
4360 on |= spec->master_mute;
4361 spec->line_out_muted = on;
4362 paths = spec->out_paths;
4363 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4364 spec->autocfg.line_out_pins, paths, on);
4366 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4368 static void call_update_outputs(struct hda_codec *codec)
4370 struct hda_gen_spec *spec = codec->spec;
4371 if (spec->automute_hook)
4372 spec->automute_hook(codec);
4374 snd_hda_gen_update_outputs(codec);
4376 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4377 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4378 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4382 * snd_hda_gen_hp_automute - standard HP-automute helper
4383 * @codec: the HDA codec
4384 * @jack: jack object, NULL for the whole
4386 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4387 struct hda_jack_callback *jack)
4389 struct hda_gen_spec *spec = codec->spec;
4390 hda_nid_t *pins = spec->autocfg.hp_pins;
4391 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4393 /* No detection for the first HP jack during indep-HP mode */
4394 if (spec->indep_hp_enabled) {
4399 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4400 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4402 call_update_outputs(codec);
4404 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4407 * snd_hda_gen_line_automute - standard line-out-automute helper
4408 * @codec: the HDA codec
4409 * @jack: jack object, NULL for the whole
4411 void snd_hda_gen_line_automute(struct hda_codec *codec,
4412 struct hda_jack_callback *jack)
4414 struct hda_gen_spec *spec = codec->spec;
4416 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4418 /* check LO jack only when it's different from HP */
4419 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4422 spec->line_jack_present =
4423 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4424 spec->autocfg.line_out_pins);
4425 if (!spec->automute_speaker || !spec->detect_lo)
4427 call_update_outputs(codec);
4429 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4432 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4433 * @codec: the HDA codec
4434 * @jack: jack object, NULL for the whole
4436 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4437 struct hda_jack_callback *jack)
4439 struct hda_gen_spec *spec = codec->spec;
4442 if (!spec->auto_mic)
4445 for (i = spec->am_num_entries - 1; i > 0; i--) {
4446 hda_nid_t pin = spec->am_entry[i].pin;
4447 /* don't detect pins retasked as outputs */
4448 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4450 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4451 mux_select(codec, 0, spec->am_entry[i].idx);
4455 mux_select(codec, 0, spec->am_entry[0].idx);
4457 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4459 /* call appropriate hooks */
4460 static void call_hp_automute(struct hda_codec *codec,
4461 struct hda_jack_callback *jack)
4463 struct hda_gen_spec *spec = codec->spec;
4464 if (spec->hp_automute_hook)
4465 spec->hp_automute_hook(codec, jack);
4467 snd_hda_gen_hp_automute(codec, jack);
4470 static void call_line_automute(struct hda_codec *codec,
4471 struct hda_jack_callback *jack)
4473 struct hda_gen_spec *spec = codec->spec;
4474 if (spec->line_automute_hook)
4475 spec->line_automute_hook(codec, jack);
4477 snd_hda_gen_line_automute(codec, jack);
4480 static void call_mic_autoswitch(struct hda_codec *codec,
4481 struct hda_jack_callback *jack)
4483 struct hda_gen_spec *spec = codec->spec;
4484 if (spec->mic_autoswitch_hook)
4485 spec->mic_autoswitch_hook(codec, jack);
4487 snd_hda_gen_mic_autoswitch(codec, jack);
4490 /* update jack retasking */
4491 static void update_automute_all(struct hda_codec *codec)
4493 call_hp_automute(codec, NULL);
4494 call_line_automute(codec, NULL);
4495 call_mic_autoswitch(codec, NULL);
4499 * Auto-Mute mode mixer enum support
4501 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4502 struct snd_ctl_elem_info *uinfo)
4504 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4505 struct hda_gen_spec *spec = codec->spec;
4506 static const char * const texts3[] = {
4507 "Disabled", "Speaker Only", "Line Out+Speaker"
4510 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4511 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4512 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4515 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4516 struct snd_ctl_elem_value *ucontrol)
4518 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4519 struct hda_gen_spec *spec = codec->spec;
4520 unsigned int val = 0;
4521 if (spec->automute_speaker)
4523 if (spec->automute_lo)
4526 ucontrol->value.enumerated.item[0] = val;
4530 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4531 struct snd_ctl_elem_value *ucontrol)
4533 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4534 struct hda_gen_spec *spec = codec->spec;
4536 switch (ucontrol->value.enumerated.item[0]) {
4538 if (!spec->automute_speaker && !spec->automute_lo)
4540 spec->automute_speaker = 0;
4541 spec->automute_lo = 0;
4544 if (spec->automute_speaker_possible) {
4545 if (!spec->automute_lo && spec->automute_speaker)
4547 spec->automute_speaker = 1;
4548 spec->automute_lo = 0;
4549 } else if (spec->automute_lo_possible) {
4550 if (spec->automute_lo)
4552 spec->automute_lo = 1;
4557 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4559 if (spec->automute_speaker && spec->automute_lo)
4561 spec->automute_speaker = 1;
4562 spec->automute_lo = 1;
4567 call_update_outputs(codec);
4571 static const struct snd_kcontrol_new automute_mode_enum = {
4572 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4573 .name = "Auto-Mute Mode",
4574 .info = automute_mode_info,
4575 .get = automute_mode_get,
4576 .put = automute_mode_put,
4579 static int add_automute_mode_enum(struct hda_codec *codec)
4581 struct hda_gen_spec *spec = codec->spec;
4583 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4589 * Check the availability of HP/line-out auto-mute;
4590 * Set up appropriately if really supported
4592 static int check_auto_mute_availability(struct hda_codec *codec)
4594 struct hda_gen_spec *spec = codec->spec;
4595 struct auto_pin_cfg *cfg = &spec->autocfg;
4599 if (spec->suppress_auto_mute)
4602 if (cfg->hp_pins[0])
4604 if (cfg->line_out_pins[0])
4606 if (cfg->speaker_pins[0])
4608 if (present < 2) /* need two different output types */
4611 if (!cfg->speaker_pins[0] &&
4612 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4613 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4614 sizeof(cfg->speaker_pins));
4615 cfg->speaker_outs = cfg->line_outs;
4618 if (!cfg->hp_pins[0] &&
4619 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4620 memcpy(cfg->hp_pins, cfg->line_out_pins,
4621 sizeof(cfg->hp_pins));
4622 cfg->hp_outs = cfg->line_outs;
4625 for (i = 0; i < cfg->hp_outs; i++) {
4626 hda_nid_t nid = cfg->hp_pins[i];
4627 if (!is_jack_detectable(codec, nid))
4629 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4630 snd_hda_jack_detect_enable_callback(codec, nid,
4632 spec->detect_hp = 1;
4635 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4636 if (cfg->speaker_outs)
4637 for (i = 0; i < cfg->line_outs; i++) {
4638 hda_nid_t nid = cfg->line_out_pins[i];
4639 if (!is_jack_detectable(codec, nid))
4641 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4642 snd_hda_jack_detect_enable_callback(codec, nid,
4643 call_line_automute);
4644 spec->detect_lo = 1;
4646 spec->automute_lo_possible = spec->detect_hp;
4649 spec->automute_speaker_possible = cfg->speaker_outs &&
4650 (spec->detect_hp || spec->detect_lo);
4652 spec->automute_lo = spec->automute_lo_possible;
4653 spec->automute_speaker = spec->automute_speaker_possible;
4655 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4656 /* create a control for automute mode */
4657 err = add_automute_mode_enum(codec);
4664 /* check whether all auto-mic pins are valid; setup indices if OK */
4665 static bool auto_mic_check_imux(struct hda_codec *codec)
4667 struct hda_gen_spec *spec = codec->spec;
4668 const struct hda_input_mux *imux;
4671 imux = &spec->input_mux;
4672 for (i = 0; i < spec->am_num_entries; i++) {
4673 spec->am_entry[i].idx =
4674 find_idx_in_nid_list(spec->am_entry[i].pin,
4675 spec->imux_pins, imux->num_items);
4676 if (spec->am_entry[i].idx < 0)
4677 return false; /* no corresponding imux */
4680 /* we don't need the jack detection for the first pin */
4681 for (i = 1; i < spec->am_num_entries; i++)
4682 snd_hda_jack_detect_enable_callback(codec,
4683 spec->am_entry[i].pin,
4684 call_mic_autoswitch);
4688 static int compare_attr(const void *ap, const void *bp)
4690 const struct automic_entry *a = ap;
4691 const struct automic_entry *b = bp;
4692 return (int)(a->attr - b->attr);
4696 * Check the availability of auto-mic switch;
4697 * Set up if really supported
4699 static int check_auto_mic_availability(struct hda_codec *codec)
4701 struct hda_gen_spec *spec = codec->spec;
4702 struct auto_pin_cfg *cfg = &spec->autocfg;
4706 if (spec->suppress_auto_mic)
4711 for (i = 0; i < cfg->num_inputs; i++) {
4712 hda_nid_t nid = cfg->inputs[i].pin;
4714 attr = snd_hda_codec_get_pincfg(codec, nid);
4715 attr = snd_hda_get_input_pin_attr(attr);
4716 if (types & (1 << attr))
4717 return 0; /* already occupied */
4719 case INPUT_PIN_ATTR_INT:
4720 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4721 return 0; /* invalid type */
4723 case INPUT_PIN_ATTR_UNUSED:
4724 return 0; /* invalid entry */
4726 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4727 return 0; /* invalid type */
4728 if (!spec->line_in_auto_switch &&
4729 cfg->inputs[i].type != AUTO_PIN_MIC)
4730 return 0; /* only mic is allowed */
4731 if (!is_jack_detectable(codec, nid))
4732 return 0; /* no unsol support */
4735 if (num_pins >= MAX_AUTO_MIC_PINS)
4737 types |= (1 << attr);
4738 spec->am_entry[num_pins].pin = nid;
4739 spec->am_entry[num_pins].attr = attr;
4746 spec->am_num_entries = num_pins;
4747 /* sort the am_entry in the order of attr so that the pin with a
4748 * higher attr will be selected when the jack is plugged.
4750 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4751 compare_attr, NULL);
4753 if (!auto_mic_check_imux(codec))
4757 spec->num_adc_nids = 1;
4758 spec->cur_mux[0] = spec->am_entry[0].idx;
4759 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4760 spec->am_entry[0].pin,
4761 spec->am_entry[1].pin,
4762 spec->am_entry[2].pin);
4768 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4770 * @codec: the HDA codec
4771 * @nid: NID to evalute
4772 * @power_state: target power state
4774 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4776 unsigned int power_state)
4778 struct hda_gen_spec *spec = codec->spec;
4780 if (!spec->power_down_unused && !codec->power_save_node)
4782 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
4784 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4786 if (is_active_nid_for_any(codec, nid))
4790 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4792 /* mute all aamix inputs initially; parse up to the first leaves */
4793 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4796 const hda_nid_t *conn;
4799 nums = snd_hda_get_conn_list(codec, mix, &conn);
4800 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4801 for (i = 0; i < nums; i++) {
4803 update_amp(codec, mix, HDA_INPUT, i,
4804 0xff, HDA_AMP_MUTE);
4805 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4806 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4807 0xff, HDA_AMP_MUTE);
4812 * snd_hda_gen_stream_pm - Stream power management callback
4813 * @codec: the HDA codec
4814 * @nid: audio widget
4815 * @on: power on/off flag
4817 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
4819 void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4821 if (codec->power_save_node)
4822 set_path_power(codec, nid, -1, on);
4824 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4827 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4828 * set up the hda_gen_spec
4829 * @codec: the HDA codec
4830 * @cfg: Parsed pin configuration
4832 * return 1 if successful, 0 if the proper config is not found,
4833 * or a negative error code
4835 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4836 struct auto_pin_cfg *cfg)
4838 struct hda_gen_spec *spec = codec->spec;
4841 parse_user_hints(codec);
4843 if (spec->mixer_nid && !spec->mixer_merge_nid)
4844 spec->mixer_merge_nid = spec->mixer_nid;
4846 if (cfg != &spec->autocfg) {
4847 spec->autocfg = *cfg;
4848 cfg = &spec->autocfg;
4851 if (!spec->main_out_badness)
4852 spec->main_out_badness = &hda_main_out_badness;
4853 if (!spec->extra_out_badness)
4854 spec->extra_out_badness = &hda_extra_out_badness;
4856 fill_all_dac_nids(codec);
4858 if (!cfg->line_outs) {
4859 if (cfg->dig_outs || cfg->dig_in_pin) {
4860 spec->multiout.max_channels = 2;
4861 spec->no_analog = 1;
4864 if (!cfg->num_inputs && !cfg->dig_in_pin)
4865 return 0; /* can't find valid BIOS pin config */
4868 if (!spec->no_primary_hp &&
4869 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4870 cfg->line_outs <= cfg->hp_outs) {
4871 /* use HP as primary out */
4872 cfg->speaker_outs = cfg->line_outs;
4873 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4874 sizeof(cfg->speaker_pins));
4875 cfg->line_outs = cfg->hp_outs;
4876 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4878 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4879 cfg->line_out_type = AUTO_PIN_HP_OUT;
4882 err = parse_output_paths(codec);
4885 err = create_multi_channel_mode(codec);
4888 err = create_multi_out_ctls(codec, cfg);
4891 err = create_hp_out_ctls(codec);
4894 err = create_speaker_out_ctls(codec);
4897 err = create_indep_hp_ctls(codec);
4900 err = create_loopback_mixing_ctl(codec);
4903 err = create_hp_mic(codec);
4906 err = create_input_ctls(codec);
4910 /* add power-down pin callbacks at first */
4911 add_all_pin_power_ctls(codec, false);
4913 spec->const_channel_count = spec->ext_channel_count;
4914 /* check the multiple speaker and headphone pins */
4915 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4916 spec->const_channel_count = max(spec->const_channel_count,
4917 cfg->speaker_outs * 2);
4918 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4919 spec->const_channel_count = max(spec->const_channel_count,
4921 spec->multiout.max_channels = max(spec->ext_channel_count,
4922 spec->const_channel_count);
4924 err = check_auto_mute_availability(codec);
4928 err = check_dyn_adc_switch(codec);
4932 err = check_auto_mic_availability(codec);
4936 /* add stereo mix if available and not enabled yet */
4937 if (!spec->auto_mic && spec->mixer_nid &&
4938 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4939 spec->input_mux.num_items > 1) {
4940 err = parse_capture_source(codec, spec->mixer_nid,
4941 CFG_IDX_MIX, spec->num_all_adcs,
4948 err = create_capture_mixers(codec);
4952 err = parse_mic_boost(codec);
4956 /* create "Headphone Mic Jack Mode" if no input selection is
4957 * available (or user specifies add_jack_modes hint)
4959 if (spec->hp_mic_pin &&
4960 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4961 spec->add_jack_modes)) {
4962 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4967 if (spec->add_jack_modes) {
4968 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4969 err = create_out_jack_modes(codec, cfg->line_outs,
4970 cfg->line_out_pins);
4974 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4975 err = create_out_jack_modes(codec, cfg->hp_outs,
4982 /* add power-up pin callbacks at last */
4983 add_all_pin_power_ctls(codec, true);
4985 /* mute all aamix input initially */
4986 if (spec->mixer_nid)
4987 mute_all_mixer_nid(codec, spec->mixer_nid);
4990 parse_digital(codec);
4992 if (spec->power_down_unused || codec->power_save_node) {
4993 if (!codec->power_filter)
4994 codec->power_filter = snd_hda_gen_path_power_filter;
4995 if (!codec->patch_ops.stream_pm)
4996 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
4999 if (!spec->no_analog && spec->beep_nid) {
5000 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
5003 if (codec->beep && codec->power_save_node) {
5004 err = add_fake_beep_paths(codec);
5007 codec->beep->power_hook = beep_power_hook;
5013 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
5017 * Build control elements
5020 /* slave controls for virtual master */
5021 static const char * const slave_pfxs[] = {
5022 "Front", "Surround", "Center", "LFE", "Side",
5023 "Headphone", "Speaker", "Mono", "Line Out",
5024 "CLFE", "Bass Speaker", "PCM",
5025 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5026 "Headphone Front", "Headphone Surround", "Headphone CLFE",
5027 "Headphone Side", "Headphone+LO", "Speaker+LO",
5032 * snd_hda_gen_build_controls - Build controls from the parsed results
5033 * @codec: the HDA codec
5035 * Pass this to build_controls patch_ops.
5037 int snd_hda_gen_build_controls(struct hda_codec *codec)
5039 struct hda_gen_spec *spec = codec->spec;
5042 if (spec->kctls.used) {
5043 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5048 if (spec->multiout.dig_out_nid) {
5049 err = snd_hda_create_dig_out_ctls(codec,
5050 spec->multiout.dig_out_nid,
5051 spec->multiout.dig_out_nid,
5052 spec->pcm_rec[1]->pcm_type);
5055 if (!spec->no_analog) {
5056 err = snd_hda_create_spdif_share_sw(codec,
5060 spec->multiout.share_spdif = 1;
5063 if (spec->dig_in_nid) {
5064 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5069 /* if we have no master control, let's create it */
5070 if (!spec->no_analog &&
5071 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
5072 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
5073 spec->vmaster_tlv, slave_pfxs,
5078 if (!spec->no_analog &&
5079 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5080 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5083 true, &spec->vmaster_mute.sw_kctl);
5086 if (spec->vmaster_mute.hook) {
5087 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5088 spec->vmaster_mute_enum);
5089 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5093 free_kctls(spec); /* no longer needed */
5095 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5101 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
5108 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5109 struct hda_codec *codec,
5110 struct snd_pcm_substream *substream,
5113 struct hda_gen_spec *spec = codec->spec;
5114 if (spec->pcm_playback_hook)
5115 spec->pcm_playback_hook(hinfo, codec, substream, action);
5118 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5119 struct hda_codec *codec,
5120 struct snd_pcm_substream *substream,
5123 struct hda_gen_spec *spec = codec->spec;
5124 if (spec->pcm_capture_hook)
5125 spec->pcm_capture_hook(hinfo, codec, substream, action);
5129 * Analog playback callbacks
5131 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5132 struct hda_codec *codec,
5133 struct snd_pcm_substream *substream)
5135 struct hda_gen_spec *spec = codec->spec;
5138 mutex_lock(&spec->pcm_mutex);
5139 err = snd_hda_multi_out_analog_open(codec,
5140 &spec->multiout, substream,
5143 spec->active_streams |= 1 << STREAM_MULTI_OUT;
5144 call_pcm_playback_hook(hinfo, codec, substream,
5145 HDA_GEN_PCM_ACT_OPEN);
5147 mutex_unlock(&spec->pcm_mutex);
5151 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5152 struct hda_codec *codec,
5153 unsigned int stream_tag,
5154 unsigned int format,
5155 struct snd_pcm_substream *substream)
5157 struct hda_gen_spec *spec = codec->spec;
5160 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5161 stream_tag, format, substream);
5163 call_pcm_playback_hook(hinfo, codec, substream,
5164 HDA_GEN_PCM_ACT_PREPARE);
5168 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5169 struct hda_codec *codec,
5170 struct snd_pcm_substream *substream)
5172 struct hda_gen_spec *spec = codec->spec;
5175 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5177 call_pcm_playback_hook(hinfo, codec, substream,
5178 HDA_GEN_PCM_ACT_CLEANUP);
5182 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5183 struct hda_codec *codec,
5184 struct snd_pcm_substream *substream)
5186 struct hda_gen_spec *spec = codec->spec;
5187 mutex_lock(&spec->pcm_mutex);
5188 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
5189 call_pcm_playback_hook(hinfo, codec, substream,
5190 HDA_GEN_PCM_ACT_CLOSE);
5191 mutex_unlock(&spec->pcm_mutex);
5195 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5196 struct hda_codec *codec,
5197 struct snd_pcm_substream *substream)
5199 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5203 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5204 struct hda_codec *codec,
5205 unsigned int stream_tag,
5206 unsigned int format,
5207 struct snd_pcm_substream *substream)
5209 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5210 call_pcm_capture_hook(hinfo, codec, substream,
5211 HDA_GEN_PCM_ACT_PREPARE);
5215 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5216 struct hda_codec *codec,
5217 struct snd_pcm_substream *substream)
5219 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5220 call_pcm_capture_hook(hinfo, codec, substream,
5221 HDA_GEN_PCM_ACT_CLEANUP);
5225 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5226 struct hda_codec *codec,
5227 struct snd_pcm_substream *substream)
5229 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5233 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5234 struct hda_codec *codec,
5235 struct snd_pcm_substream *substream)
5237 struct hda_gen_spec *spec = codec->spec;
5240 mutex_lock(&spec->pcm_mutex);
5241 if (spec->indep_hp && !spec->indep_hp_enabled)
5244 spec->active_streams |= 1 << STREAM_INDEP_HP;
5245 call_pcm_playback_hook(hinfo, codec, substream,
5246 HDA_GEN_PCM_ACT_OPEN);
5247 mutex_unlock(&spec->pcm_mutex);
5251 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5252 struct hda_codec *codec,
5253 struct snd_pcm_substream *substream)
5255 struct hda_gen_spec *spec = codec->spec;
5256 mutex_lock(&spec->pcm_mutex);
5257 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
5258 call_pcm_playback_hook(hinfo, codec, substream,
5259 HDA_GEN_PCM_ACT_CLOSE);
5260 mutex_unlock(&spec->pcm_mutex);
5264 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5265 struct hda_codec *codec,
5266 unsigned int stream_tag,
5267 unsigned int format,
5268 struct snd_pcm_substream *substream)
5270 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5271 call_pcm_playback_hook(hinfo, codec, substream,
5272 HDA_GEN_PCM_ACT_PREPARE);
5276 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5277 struct hda_codec *codec,
5278 struct snd_pcm_substream *substream)
5280 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5281 call_pcm_playback_hook(hinfo, codec, substream,
5282 HDA_GEN_PCM_ACT_CLEANUP);
5289 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5290 struct hda_codec *codec,
5291 struct snd_pcm_substream *substream)
5293 struct hda_gen_spec *spec = codec->spec;
5294 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5297 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5298 struct hda_codec *codec,
5299 unsigned int stream_tag,
5300 unsigned int format,
5301 struct snd_pcm_substream *substream)
5303 struct hda_gen_spec *spec = codec->spec;
5304 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5305 stream_tag, format, substream);
5308 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5309 struct hda_codec *codec,
5310 struct snd_pcm_substream *substream)
5312 struct hda_gen_spec *spec = codec->spec;
5313 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5316 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5317 struct hda_codec *codec,
5318 struct snd_pcm_substream *substream)
5320 struct hda_gen_spec *spec = codec->spec;
5321 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5327 #define alt_capture_pcm_open capture_pcm_open
5328 #define alt_capture_pcm_close capture_pcm_close
5330 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5331 struct hda_codec *codec,
5332 unsigned int stream_tag,
5333 unsigned int format,
5334 struct snd_pcm_substream *substream)
5336 struct hda_gen_spec *spec = codec->spec;
5338 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5339 stream_tag, 0, format);
5340 call_pcm_capture_hook(hinfo, codec, substream,
5341 HDA_GEN_PCM_ACT_PREPARE);
5345 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5346 struct hda_codec *codec,
5347 struct snd_pcm_substream *substream)
5349 struct hda_gen_spec *spec = codec->spec;
5351 snd_hda_codec_cleanup_stream(codec,
5352 spec->adc_nids[substream->number + 1]);
5353 call_pcm_capture_hook(hinfo, codec, substream,
5354 HDA_GEN_PCM_ACT_CLEANUP);
5360 static const struct hda_pcm_stream pcm_analog_playback = {
5364 /* NID is set in build_pcms */
5366 .open = playback_pcm_open,
5367 .close = playback_pcm_close,
5368 .prepare = playback_pcm_prepare,
5369 .cleanup = playback_pcm_cleanup
5373 static const struct hda_pcm_stream pcm_analog_capture = {
5377 /* NID is set in build_pcms */
5379 .open = capture_pcm_open,
5380 .close = capture_pcm_close,
5381 .prepare = capture_pcm_prepare,
5382 .cleanup = capture_pcm_cleanup
5386 static const struct hda_pcm_stream pcm_analog_alt_playback = {
5390 /* NID is set in build_pcms */
5392 .open = alt_playback_pcm_open,
5393 .close = alt_playback_pcm_close,
5394 .prepare = alt_playback_pcm_prepare,
5395 .cleanup = alt_playback_pcm_cleanup
5399 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5400 .substreams = 2, /* can be overridden */
5403 /* NID is set in build_pcms */
5405 .open = alt_capture_pcm_open,
5406 .close = alt_capture_pcm_close,
5407 .prepare = alt_capture_pcm_prepare,
5408 .cleanup = alt_capture_pcm_cleanup
5412 static const struct hda_pcm_stream pcm_digital_playback = {
5416 /* NID is set in build_pcms */
5418 .open = dig_playback_pcm_open,
5419 .close = dig_playback_pcm_close,
5420 .prepare = dig_playback_pcm_prepare,
5421 .cleanup = dig_playback_pcm_cleanup
5425 static const struct hda_pcm_stream pcm_digital_capture = {
5429 /* NID is set in build_pcms */
5432 /* Used by build_pcms to flag that a PCM has no playback stream */
5433 static const struct hda_pcm_stream pcm_null_stream = {
5440 * dynamic changing ADC PCM streams
5442 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5444 struct hda_gen_spec *spec = codec->spec;
5445 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5447 if (spec->cur_adc && spec->cur_adc != new_adc) {
5448 /* stream is running, let's swap the current ADC */
5449 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5450 spec->cur_adc = new_adc;
5451 snd_hda_codec_setup_stream(codec, new_adc,
5452 spec->cur_adc_stream_tag, 0,
5453 spec->cur_adc_format);
5459 /* analog capture with dynamic dual-adc changes */
5460 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5461 struct hda_codec *codec,
5462 unsigned int stream_tag,
5463 unsigned int format,
5464 struct snd_pcm_substream *substream)
5466 struct hda_gen_spec *spec = codec->spec;
5467 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5468 spec->cur_adc_stream_tag = stream_tag;
5469 spec->cur_adc_format = format;
5470 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5474 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5475 struct hda_codec *codec,
5476 struct snd_pcm_substream *substream)
5478 struct hda_gen_spec *spec = codec->spec;
5479 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5484 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5488 .nid = 0, /* fill later */
5490 .prepare = dyn_adc_capture_pcm_prepare,
5491 .cleanup = dyn_adc_capture_pcm_cleanup
5495 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5496 const char *chip_name)
5502 strlcpy(str, chip_name, len);
5504 /* drop non-alnum chars after a space */
5505 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5506 if (!isalnum(p[1])) {
5511 strlcat(str, sfx, len);
5514 /* copy PCM stream info from @default_str, and override non-NULL entries
5515 * from @spec_str and @nid
5517 static void setup_pcm_stream(struct hda_pcm_stream *str,
5518 const struct hda_pcm_stream *default_str,
5519 const struct hda_pcm_stream *spec_str,
5522 *str = *default_str;
5526 if (spec_str->substreams)
5527 str->substreams = spec_str->substreams;
5528 if (spec_str->channels_min)
5529 str->channels_min = spec_str->channels_min;
5530 if (spec_str->channels_max)
5531 str->channels_max = spec_str->channels_max;
5532 if (spec_str->rates)
5533 str->rates = spec_str->rates;
5534 if (spec_str->formats)
5535 str->formats = spec_str->formats;
5536 if (spec_str->maxbps)
5537 str->maxbps = spec_str->maxbps;
5542 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5543 * @codec: the HDA codec
5545 * Pass this to build_pcms patch_ops.
5547 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5549 struct hda_gen_spec *spec = codec->spec;
5550 struct hda_pcm *info;
5551 bool have_multi_adcs;
5553 if (spec->no_analog)
5556 fill_pcm_stream_name(spec->stream_name_analog,
5557 sizeof(spec->stream_name_analog),
5558 " Analog", codec->core.chip_name);
5559 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5562 spec->pcm_rec[0] = info;
5564 if (spec->multiout.num_dacs > 0) {
5565 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5566 &pcm_analog_playback,
5567 spec->stream_analog_playback,
5568 spec->multiout.dac_nids[0]);
5569 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5570 spec->multiout.max_channels;
5571 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5572 spec->autocfg.line_outs == 2)
5573 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5576 if (spec->num_adc_nids) {
5577 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5578 (spec->dyn_adc_switch ?
5579 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5580 spec->stream_analog_capture,
5585 /* SPDIF for stream index #1 */
5586 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5587 fill_pcm_stream_name(spec->stream_name_digital,
5588 sizeof(spec->stream_name_digital),
5589 " Digital", codec->core.chip_name);
5590 info = snd_hda_codec_pcm_new(codec, "%s",
5591 spec->stream_name_digital);
5594 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5595 spec->pcm_rec[1] = info;
5596 if (spec->dig_out_type)
5597 info->pcm_type = spec->dig_out_type;
5599 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5600 if (spec->multiout.dig_out_nid)
5601 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5602 &pcm_digital_playback,
5603 spec->stream_digital_playback,
5604 spec->multiout.dig_out_nid);
5605 if (spec->dig_in_nid)
5606 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5607 &pcm_digital_capture,
5608 spec->stream_digital_capture,
5612 if (spec->no_analog)
5615 /* If the use of more than one ADC is requested for the current
5616 * model, configure a second analog capture-only PCM.
5618 have_multi_adcs = (spec->num_adc_nids > 1) &&
5619 !spec->dyn_adc_switch && !spec->auto_mic;
5620 /* Additional Analaog capture for index #2 */
5621 if (spec->alt_dac_nid || have_multi_adcs) {
5622 fill_pcm_stream_name(spec->stream_name_alt_analog,
5623 sizeof(spec->stream_name_alt_analog),
5624 " Alt Analog", codec->core.chip_name);
5625 info = snd_hda_codec_pcm_new(codec, "%s",
5626 spec->stream_name_alt_analog);
5629 spec->pcm_rec[2] = info;
5630 if (spec->alt_dac_nid)
5631 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5632 &pcm_analog_alt_playback,
5633 spec->stream_analog_alt_playback,
5636 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5637 &pcm_null_stream, NULL, 0);
5638 if (have_multi_adcs) {
5639 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5640 &pcm_analog_alt_capture,
5641 spec->stream_analog_alt_capture,
5643 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5644 spec->num_adc_nids - 1;
5646 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5647 &pcm_null_stream, NULL, 0);
5653 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5657 * Standard auto-parser initializations
5660 /* configure the given path as a proper output */
5661 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5663 struct nid_path *path;
5666 path = snd_hda_get_path_from_idx(codec, path_idx);
5667 if (!path || !path->depth)
5669 pin = path->path[path->depth - 1];
5670 restore_pin_ctl(codec, pin);
5671 snd_hda_activate_path(codec, path, path->active,
5672 aamix_default(codec->spec));
5673 set_pin_eapd(codec, pin, path->active);
5676 /* initialize primary output paths */
5677 static void init_multi_out(struct hda_codec *codec)
5679 struct hda_gen_spec *spec = codec->spec;
5682 for (i = 0; i < spec->autocfg.line_outs; i++)
5683 set_output_and_unmute(codec, spec->out_paths[i]);
5687 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5691 for (i = 0; i < num_outs; i++)
5692 set_output_and_unmute(codec, paths[i]);
5695 /* initialize hp and speaker paths */
5696 static void init_extra_out(struct hda_codec *codec)
5698 struct hda_gen_spec *spec = codec->spec;
5700 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5701 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5702 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5703 __init_extra_out(codec, spec->autocfg.speaker_outs,
5704 spec->speaker_paths);
5707 /* initialize multi-io paths */
5708 static void init_multi_io(struct hda_codec *codec)
5710 struct hda_gen_spec *spec = codec->spec;
5713 for (i = 0; i < spec->multi_ios; i++) {
5714 hda_nid_t pin = spec->multi_io[i].pin;
5715 struct nid_path *path;
5716 path = get_multiio_path(codec, i);
5719 if (!spec->multi_io[i].ctl_in)
5720 spec->multi_io[i].ctl_in =
5721 snd_hda_codec_get_pin_target(codec, pin);
5722 snd_hda_activate_path(codec, path, path->active,
5723 aamix_default(spec));
5727 static void init_aamix_paths(struct hda_codec *codec)
5729 struct hda_gen_spec *spec = codec->spec;
5731 if (!spec->have_aamix_ctl)
5733 if (!has_aamix_out_paths(spec))
5735 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5736 spec->aamix_out_paths[0],
5737 spec->autocfg.line_out_type);
5738 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5739 spec->aamix_out_paths[1],
5741 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5742 spec->aamix_out_paths[2],
5743 AUTO_PIN_SPEAKER_OUT);
5746 /* set up input pins and loopback paths */
5747 static void init_analog_input(struct hda_codec *codec)
5749 struct hda_gen_spec *spec = codec->spec;
5750 struct auto_pin_cfg *cfg = &spec->autocfg;
5753 for (i = 0; i < cfg->num_inputs; i++) {
5754 hda_nid_t nid = cfg->inputs[i].pin;
5755 if (is_input_pin(codec, nid))
5756 restore_pin_ctl(codec, nid);
5758 /* init loopback inputs */
5759 if (spec->mixer_nid) {
5760 resume_path_from_idx(codec, spec->loopback_paths[i]);
5761 resume_path_from_idx(codec, spec->loopback_merge_path);
5766 /* initialize ADC paths */
5767 static void init_input_src(struct hda_codec *codec)
5769 struct hda_gen_spec *spec = codec->spec;
5770 struct hda_input_mux *imux = &spec->input_mux;
5771 struct nid_path *path;
5774 if (spec->dyn_adc_switch)
5777 nums = spec->num_adc_nids;
5779 for (c = 0; c < nums; c++) {
5780 for (i = 0; i < imux->num_items; i++) {
5781 path = get_input_path(codec, c, i);
5783 bool active = path->active;
5784 if (i == spec->cur_mux[c])
5786 snd_hda_activate_path(codec, path, active, false);
5790 update_hp_mic(codec, c, true);
5793 if (spec->cap_sync_hook)
5794 spec->cap_sync_hook(codec, NULL, NULL);
5797 /* set right pin controls for digital I/O */
5798 static void init_digital(struct hda_codec *codec)
5800 struct hda_gen_spec *spec = codec->spec;
5804 for (i = 0; i < spec->autocfg.dig_outs; i++)
5805 set_output_and_unmute(codec, spec->digout_paths[i]);
5806 pin = spec->autocfg.dig_in_pin;
5808 restore_pin_ctl(codec, pin);
5809 resume_path_from_idx(codec, spec->digin_path);
5813 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5814 * invalid unsol tags by some reason
5816 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5820 for (i = 0; i < codec->init_pins.used; i++) {
5821 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5822 hda_nid_t nid = pin->nid;
5823 if (is_jack_detectable(codec, nid) &&
5824 !snd_hda_jack_tbl_get(codec, nid))
5825 snd_hda_codec_update_cache(codec, nid, 0,
5826 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5831 * snd_hda_gen_init - initialize the generic spec
5832 * @codec: the HDA codec
5834 * This can be put as patch_ops init function.
5836 int snd_hda_gen_init(struct hda_codec *codec)
5838 struct hda_gen_spec *spec = codec->spec;
5840 if (spec->init_hook)
5841 spec->init_hook(codec);
5843 if (!spec->skip_verbs)
5844 snd_hda_apply_verbs(codec);
5846 init_multi_out(codec);
5847 init_extra_out(codec);
5848 init_multi_io(codec);
5849 init_aamix_paths(codec);
5850 init_analog_input(codec);
5851 init_input_src(codec);
5852 init_digital(codec);
5854 clear_unsol_on_unused_pins(codec);
5856 sync_all_pin_power_ctls(codec);
5858 /* call init functions of standard auto-mute helpers */
5859 update_automute_all(codec);
5861 regcache_sync(codec->core.regmap);
5863 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5864 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5866 hda_call_check_power_status(codec, 0x01);
5869 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
5872 * snd_hda_gen_free - free the generic spec
5873 * @codec: the HDA codec
5875 * This can be put as patch_ops free function.
5877 void snd_hda_gen_free(struct hda_codec *codec)
5879 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
5880 snd_hda_gen_spec_free(codec->spec);
5884 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
5888 * snd_hda_gen_check_power_status - check the loopback power save state
5889 * @codec: the HDA codec
5890 * @nid: NID to inspect
5892 * This can be put as patch_ops check_power_status function.
5894 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5896 struct hda_gen_spec *spec = codec->spec;
5897 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5899 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
5904 * the generic codec support
5907 static const struct hda_codec_ops generic_patch_ops = {
5908 .build_controls = snd_hda_gen_build_controls,
5909 .build_pcms = snd_hda_gen_build_pcms,
5910 .init = snd_hda_gen_init,
5911 .free = snd_hda_gen_free,
5912 .unsol_event = snd_hda_jack_unsol_event,
5914 .check_power_status = snd_hda_gen_check_power_status,
5919 * snd_hda_parse_generic_codec - Generic codec parser
5920 * @codec: the HDA codec
5922 static int snd_hda_parse_generic_codec(struct hda_codec *codec)
5924 struct hda_gen_spec *spec;
5927 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5930 snd_hda_gen_spec_init(spec);
5933 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5937 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5941 codec->patch_ops = generic_patch_ops;
5945 snd_hda_gen_free(codec);
5949 static const struct hda_device_id snd_hda_id_generic[] = {
5950 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
5953 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
5955 static struct hda_codec_driver generic_driver = {
5956 .id = snd_hda_id_generic,
5959 module_hda_codec_driver(generic_driver);
5961 MODULE_LICENSE("GPL");
5962 MODULE_DESCRIPTION("Generic HD-audio codec parser");