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_path_idx - get the index number corresponding to the path
284 * @codec: the HDA codec
285 * @path: nid_path object
287 * The returned index starts from 1, i.e. the actual array index with offset 1,
288 * and zero is handled as an invalid path
290 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
292 struct hda_gen_spec *spec = codec->spec;
293 struct nid_path *array = spec->paths.list;
296 if (!spec->paths.used)
299 if (idx < 0 || idx >= spec->paths.used)
303 EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
306 * snd_hda_get_path_from_idx - get the path instance corresponding to the
308 * @codec: the HDA codec
309 * @idx: the path index
311 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
313 struct hda_gen_spec *spec = codec->spec;
315 if (idx <= 0 || idx > spec->paths.used)
317 return snd_array_elem(&spec->paths, idx - 1);
319 EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
321 /* check whether the given DAC is already found in any existing paths */
322 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
324 struct hda_gen_spec *spec = codec->spec;
327 for (i = 0; i < spec->paths.used; i++) {
328 struct nid_path *path = snd_array_elem(&spec->paths, i);
329 if (path->path[0] == nid)
335 /* check whether the given two widgets can be connected */
336 static bool is_reachable_path(struct hda_codec *codec,
337 hda_nid_t from_nid, hda_nid_t to_nid)
339 if (!from_nid || !to_nid)
341 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
344 /* nid, dir and idx */
345 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
347 /* check whether the given ctl is already assigned in any path elements */
348 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
350 struct hda_gen_spec *spec = codec->spec;
353 val &= AMP_VAL_COMPARE_MASK;
354 for (i = 0; i < spec->paths.used; i++) {
355 struct nid_path *path = snd_array_elem(&spec->paths, i);
356 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
362 /* check whether a control with the given (nid, dir, idx) was assigned */
363 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
364 int dir, int idx, int type)
366 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
367 return is_ctl_used(codec, val, type);
370 static void print_nid_path(struct hda_codec *codec,
371 const char *pfx, struct nid_path *path)
378 for (i = 0; i < path->depth; i++)
379 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
380 pos != buf ? ":" : "",
383 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
386 /* called recursively */
387 static bool __parse_nid_path(struct hda_codec *codec,
388 hda_nid_t from_nid, hda_nid_t to_nid,
389 int anchor_nid, struct nid_path *path,
392 const hda_nid_t *conn;
395 if (to_nid == anchor_nid)
396 anchor_nid = 0; /* anchor passed */
397 else if (to_nid == (hda_nid_t)(-anchor_nid))
398 return false; /* hit the exclusive nid */
400 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
401 for (i = 0; i < nums; i++) {
402 if (conn[i] != from_nid) {
403 /* special case: when from_nid is 0,
404 * try to find an empty DAC
407 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
408 is_dac_already_used(codec, conn[i]))
411 /* anchor is not requested or already passed? */
415 if (depth >= MAX_NID_PATH_DEPTH)
417 for (i = 0; i < nums; i++) {
419 type = get_wcaps_type(get_wcaps(codec, conn[i]));
420 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
423 if (__parse_nid_path(codec, from_nid, conn[i],
424 anchor_nid, path, depth + 1))
430 path->path[path->depth] = conn[i];
431 path->idx[path->depth + 1] = i;
432 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
433 path->multi[path->depth + 1] = 1;
439 * snd_hda_parse_nid_path - parse the widget path from the given nid to
441 * @codec: the HDA codec
442 * @from_nid: the NID where the path start from
443 * @to_nid: the NID where the path ends at
444 * @anchor_nid: the anchor indication
445 * @path: the path object to store the result
447 * Returns true if a matching path is found.
449 * The parsing behavior depends on parameters:
450 * when @from_nid is 0, try to find an empty DAC;
451 * when @anchor_nid is set to a positive value, only paths through the widget
452 * with the given value are evaluated.
453 * when @anchor_nid is set to a negative value, paths through the widget
454 * with the negative of given value are excluded, only other paths are chosen.
455 * when @anchor_nid is zero, no special handling about path selection.
457 static bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
458 hda_nid_t to_nid, int anchor_nid,
459 struct nid_path *path)
461 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
462 path->path[path->depth] = to_nid;
470 * snd_hda_add_new_path - parse the path between the given NIDs and
471 * add to the path list
472 * @codec: the HDA codec
473 * @from_nid: the NID where the path start from
474 * @to_nid: the NID where the path ends at
475 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
477 * If no valid path is found, returns NULL.
480 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
481 hda_nid_t to_nid, int anchor_nid)
483 struct hda_gen_spec *spec = codec->spec;
484 struct nid_path *path;
486 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
489 /* check whether the path has been already added */
490 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
494 path = snd_array_new(&spec->paths);
497 memset(path, 0, sizeof(*path));
498 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
504 EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
506 /* clear the given path as invalid so that it won't be picked up later */
507 static void invalidate_nid_path(struct hda_codec *codec, int idx)
509 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
512 memset(path, 0, sizeof(*path));
515 /* return a DAC if paired to the given pin by codec driver */
516 static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
518 struct hda_gen_spec *spec = codec->spec;
519 const hda_nid_t *list = spec->preferred_dacs;
523 for (; *list; list += 2)
529 /* look for an empty DAC slot */
530 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
533 struct hda_gen_spec *spec = codec->spec;
537 for (i = 0; i < spec->num_all_dacs; i++) {
538 hda_nid_t nid = spec->all_dacs[i];
539 if (!nid || is_dac_already_used(codec, nid))
541 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
542 if (is_digital != cap_digital)
544 if (is_reachable_path(codec, nid, pin))
550 /* replace the channels in the composed amp value with the given number */
551 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
553 val &= ~(0x3U << 16);
558 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
559 hda_nid_t nid2, int dir)
561 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
562 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
563 return (query_amp_caps(codec, nid1, dir) ==
564 query_amp_caps(codec, nid2, dir));
567 /* look for a widget suitable for assigning a mute switch in the path */
568 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
569 struct nid_path *path)
573 for (i = path->depth - 1; i >= 0; i--) {
574 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
575 return path->path[i];
576 if (i != path->depth - 1 && i != 0 &&
577 nid_has_mute(codec, path->path[i], HDA_INPUT))
578 return path->path[i];
583 /* look for a widget suitable for assigning a volume ctl in the path */
584 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
585 struct nid_path *path)
587 struct hda_gen_spec *spec = codec->spec;
590 for (i = path->depth - 1; i >= 0; i--) {
591 hda_nid_t nid = path->path[i];
592 if ((spec->out_vol_mask >> nid) & 1)
594 if (nid_has_volume(codec, nid, HDA_OUTPUT))
601 * path activation / deactivation
604 /* can have the amp-in capability? */
605 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
607 hda_nid_t nid = path->path[idx];
608 unsigned int caps = get_wcaps(codec, nid);
609 unsigned int type = get_wcaps_type(caps);
611 if (!(caps & AC_WCAP_IN_AMP))
613 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
618 /* can have the amp-out capability? */
619 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
621 hda_nid_t nid = path->path[idx];
622 unsigned int caps = get_wcaps(codec, nid);
623 unsigned int type = get_wcaps_type(caps);
625 if (!(caps & AC_WCAP_OUT_AMP))
627 if (type == AC_WID_PIN && !idx) /* only for output pins */
632 /* check whether the given (nid,dir,idx) is active */
633 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
634 unsigned int dir, unsigned int idx)
636 struct hda_gen_spec *spec = codec->spec;
637 int type = get_wcaps_type(get_wcaps(codec, nid));
640 if (nid == codec->core.afg)
643 for (n = 0; n < spec->paths.used; n++) {
644 struct nid_path *path = snd_array_elem(&spec->paths, n);
647 if (codec->power_save_node) {
648 if (!path->stream_enabled)
650 /* ignore unplugged paths except for DAC/ADC */
651 if (!(path->pin_enabled || path->pin_fixed) &&
652 type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
655 for (i = 0; i < path->depth; i++) {
656 if (path->path[i] == nid) {
657 if (dir == HDA_OUTPUT || idx == -1 ||
667 /* check whether the NID is referred by any active paths */
668 #define is_active_nid_for_any(codec, nid) \
669 is_active_nid(codec, nid, HDA_OUTPUT, -1)
671 /* get the default amp value for the target state */
672 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
673 int dir, unsigned int caps, bool enable)
675 unsigned int val = 0;
677 if (caps & AC_AMPCAP_NUM_STEPS) {
680 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
682 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
689 /* is this a stereo widget or a stereo-to-mono mix? */
690 static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
692 unsigned int wcaps = get_wcaps(codec, nid);
695 if (wcaps & AC_WCAP_STEREO)
697 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
699 if (snd_hda_get_num_conns(codec, nid) != 1)
701 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
703 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
706 /* initialize the amp value (only at the first time) */
707 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
709 unsigned int caps = query_amp_caps(codec, nid, dir);
710 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
712 if (is_stereo_amps(codec, nid, dir))
713 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
715 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
718 /* update the amp, doing in stereo or mono depending on NID */
719 static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
720 unsigned int mask, unsigned int val)
722 if (is_stereo_amps(codec, nid, dir))
723 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
726 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
730 /* calculate amp value mask we can modify;
731 * if the given amp is controlled by mixers, don't touch it
733 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
734 hda_nid_t nid, int dir, int idx,
737 unsigned int mask = 0xff;
739 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
740 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
743 if (caps & AC_AMPCAP_NUM_STEPS) {
744 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
745 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
751 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
752 int idx, int idx_to_check, bool enable)
755 unsigned int mask, val;
757 caps = query_amp_caps(codec, nid, dir);
758 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
759 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
764 update_amp(codec, nid, dir, idx, mask, val);
767 static void check_and_activate_amp(struct hda_codec *codec, hda_nid_t nid,
768 int dir, int idx, int idx_to_check,
771 /* check whether the given amp is still used by others */
772 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
774 activate_amp(codec, nid, dir, idx, idx_to_check, enable);
777 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
780 hda_nid_t nid = path->path[i];
781 init_amp(codec, nid, HDA_OUTPUT, 0);
782 check_and_activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
785 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
786 int i, bool enable, bool add_aamix)
788 struct hda_gen_spec *spec = codec->spec;
789 const hda_nid_t *conn;
792 hda_nid_t nid = path->path[i];
794 nums = snd_hda_get_conn_list(codec, nid, &conn);
795 type = get_wcaps_type(get_wcaps(codec, nid));
796 if (type == AC_WID_PIN ||
797 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
803 for (n = 0; n < nums; n++)
804 init_amp(codec, nid, HDA_INPUT, n);
806 /* here is a little bit tricky in comparison with activate_amp_out();
807 * when aa-mixer is available, we need to enable the path as well
809 for (n = 0; n < nums; n++) {
811 if (conn[n] != spec->mixer_merge_nid)
813 /* when aamix is disabled, force to off */
815 activate_amp(codec, nid, HDA_INPUT, n, n, false);
819 check_and_activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
823 /* sync power of each widget in the the given path */
824 static hda_nid_t path_power_update(struct hda_codec *codec,
825 struct nid_path *path,
826 bool allow_powerdown)
828 hda_nid_t nid, changed = 0;
831 for (i = 0; i < path->depth; i++) {
833 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
835 if (nid == codec->core.afg)
837 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
841 power = snd_hda_codec_read(codec, nid, 0,
842 AC_VERB_GET_POWER_STATE, 0);
843 if (power != (state | (state << 4))) {
844 snd_hda_codec_write(codec, nid, 0,
845 AC_VERB_SET_POWER_STATE, state);
847 /* all known codecs seem to be capable to handl
848 * widgets state even in D3, so far.
849 * if any new codecs need to restore the widget
850 * states after D0 transition, call the function
854 if (state == AC_PWRST_D0)
855 snd_hdac_regmap_sync_node(&codec->core, nid);
862 /* do sync with the last power state change */
863 static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
867 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
872 * snd_hda_activate_path - activate or deactivate the given path
873 * @codec: the HDA codec
874 * @path: the path to activate/deactivate
875 * @enable: flag to activate or not
876 * @add_aamix: enable the input from aamix NID
878 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
880 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
881 bool enable, bool add_aamix)
883 struct hda_gen_spec *spec = codec->spec;
886 path->active = enable;
888 /* make sure the widget is powered up */
889 if (enable && (spec->power_down_unused || codec->power_save_node))
890 path_power_update(codec, path, codec->power_save_node);
892 for (i = path->depth - 1; i >= 0; i--) {
893 hda_nid_t nid = path->path[i];
895 if (enable && path->multi[i])
896 snd_hda_codec_update_cache(codec, nid, 0,
897 AC_VERB_SET_CONNECT_SEL,
899 if (has_amp_in(codec, path, i))
900 activate_amp_in(codec, path, i, enable, add_aamix);
901 if (has_amp_out(codec, path, i))
902 activate_amp_out(codec, path, i, enable);
905 EXPORT_SYMBOL_GPL(snd_hda_activate_path);
907 /* if the given path is inactive, put widgets into D3 (only if suitable) */
908 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
910 struct hda_gen_spec *spec = codec->spec;
912 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
914 sync_power_state_change(codec, path_power_update(codec, path, true));
917 /* turn on/off EAPD on the given pin */
918 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
920 struct hda_gen_spec *spec = codec->spec;
921 if (spec->own_eapd_ctl ||
922 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
924 if (spec->keep_eapd_on && !enable)
928 snd_hda_codec_update_cache(codec, pin, 0,
929 AC_VERB_SET_EAPD_BTLENABLE,
930 enable ? 0x02 : 0x00);
933 /* re-initialize the path specified by the given path index */
934 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
936 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
938 snd_hda_activate_path(codec, path, path->active, false);
943 * Helper functions for creating mixer ctl elements
946 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
947 struct snd_ctl_elem_value *ucontrol);
948 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
949 struct snd_ctl_elem_value *ucontrol);
956 static const struct snd_kcontrol_new control_templates[] = {
957 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
958 /* only the put callback is replaced for handling the special mute */
960 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
961 .subdevice = HDA_SUBDEV_AMP_FLAG,
962 .info = snd_hda_mixer_amp_switch_info,
963 .get = snd_hda_mixer_amp_switch_get,
964 .put = hda_gen_mixer_mute_put, /* replaced */
965 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
968 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
969 .info = snd_hda_mixer_amp_switch_info,
970 .get = snd_hda_mixer_bind_switch_get,
971 .put = hda_gen_bind_mute_put, /* replaced */
972 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
976 /* add dynamic controls from template */
977 static struct snd_kcontrol_new *
978 add_control(struct hda_gen_spec *spec, int type, const char *name,
979 int cidx, unsigned long val)
981 struct snd_kcontrol_new *knew;
983 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
987 if (get_amp_nid_(val))
988 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
989 knew->private_value = val;
993 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
994 const char *pfx, const char *dir,
995 const char *sfx, int cidx, unsigned long val)
997 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
998 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
999 if (!add_control(spec, type, name, cidx, val))
1004 #define add_pb_vol_ctrl(spec, type, pfx, val) \
1005 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1006 #define add_pb_sw_ctrl(spec, type, pfx, val) \
1007 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1008 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1009 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1010 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1011 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1013 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1014 unsigned int chs, struct nid_path *path)
1019 val = path->ctls[NID_PATH_VOL_CTL];
1022 val = amp_val_replace_channels(val, chs);
1023 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1026 /* return the channel bits suitable for the given path->ctls[] */
1027 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1030 int chs = 1; /* mono (left only) */
1032 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1033 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1034 chs = 3; /* stereo */
1039 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1040 struct nid_path *path)
1042 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1043 return add_vol_ctl(codec, pfx, cidx, chs, path);
1046 /* create a mute-switch for the given mixer widget;
1047 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1049 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1050 unsigned int chs, struct nid_path *path)
1053 int type = HDA_CTL_WIDGET_MUTE;
1057 val = path->ctls[NID_PATH_MUTE_CTL];
1060 val = amp_val_replace_channels(val, chs);
1061 if (get_amp_direction_(val) == HDA_INPUT) {
1062 hda_nid_t nid = get_amp_nid_(val);
1063 int nums = snd_hda_get_num_conns(codec, nid);
1065 type = HDA_CTL_BIND_MUTE;
1069 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1072 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1073 int cidx, struct nid_path *path)
1075 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1076 return add_sw_ctl(codec, pfx, cidx, chs, path);
1079 /* playback mute control with the software mute bit check */
1080 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1081 struct snd_ctl_elem_value *ucontrol)
1083 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1084 struct hda_gen_spec *spec = codec->spec;
1086 if (spec->auto_mute_via_amp) {
1087 hda_nid_t nid = get_amp_nid(kcontrol);
1088 bool enabled = !((spec->mute_bits >> nid) & 1);
1089 ucontrol->value.integer.value[0] &= enabled;
1090 ucontrol->value.integer.value[1] &= enabled;
1094 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1095 struct snd_ctl_elem_value *ucontrol)
1097 sync_auto_mute_bits(kcontrol, ucontrol);
1098 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1101 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1102 struct snd_ctl_elem_value *ucontrol)
1104 sync_auto_mute_bits(kcontrol, ucontrol);
1105 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1108 /* any ctl assigned to the path with the given index? */
1109 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1111 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1112 return path && path->ctls[ctl_type];
1115 static const char * const channel_name[4] = {
1116 "Front", "Surround", "CLFE", "Side"
1119 /* give some appropriate ctl name prefix for the given line out channel */
1120 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1121 int *index, int ctl_type)
1123 struct hda_gen_spec *spec = codec->spec;
1124 struct auto_pin_cfg *cfg = &spec->autocfg;
1127 if (cfg->line_outs == 1 && !spec->multi_ios &&
1128 !cfg->hp_outs && !cfg->speaker_outs)
1129 return spec->vmaster_mute.hook ? "PCM" : "Master";
1131 /* if there is really a single DAC used in the whole output paths,
1132 * use it master (or "PCM" if a vmaster hook is present)
1134 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1135 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1136 return spec->vmaster_mute.hook ? "PCM" : "Master";
1138 /* multi-io channels */
1139 if (ch >= cfg->line_outs)
1140 return channel_name[ch];
1142 switch (cfg->line_out_type) {
1143 case AUTO_PIN_SPEAKER_OUT:
1144 /* if the primary channel vol/mute is shared with HP volume,
1145 * don't name it as Speaker
1147 if (!ch && cfg->hp_outs &&
1148 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1150 if (cfg->line_outs == 1)
1152 if (cfg->line_outs == 2)
1153 return ch ? "Bass Speaker" : "Speaker";
1155 case AUTO_PIN_HP_OUT:
1156 /* if the primary channel vol/mute is shared with spk volume,
1157 * don't name it as Headphone
1159 if (!ch && cfg->speaker_outs &&
1160 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1162 /* for multi-io case, only the primary out */
1163 if (ch && spec->multi_ios)
1167 case AUTO_PIN_LINE_OUT:
1168 /* This deals with the case where we have two DACs and
1169 * one LO, one HP and one Speaker */
1170 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1171 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1172 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1173 if (hp_lo_shared && spk_lo_shared)
1174 return spec->vmaster_mute.hook ? "PCM" : "Master";
1176 return "Headphone+LO";
1178 return "Speaker+LO";
1182 /* for a single channel output, we don't have to name the channel */
1183 if (cfg->line_outs == 1 && !spec->multi_ios)
1186 if (ch >= ARRAY_SIZE(channel_name)) {
1191 return channel_name[ch];
1195 * Parse output paths
1198 /* badness definition */
1200 /* No primary DAC is found for the main output */
1201 BAD_NO_PRIMARY_DAC = 0x10000,
1202 /* No DAC is found for the extra output */
1203 BAD_NO_DAC = 0x4000,
1204 /* No possible multi-ios */
1205 BAD_MULTI_IO = 0x120,
1206 /* No individual DAC for extra output */
1207 BAD_NO_EXTRA_DAC = 0x102,
1208 /* No individual DAC for extra surrounds */
1209 BAD_NO_EXTRA_SURR_DAC = 0x101,
1210 /* Primary DAC shared with main surrounds */
1211 BAD_SHARED_SURROUND = 0x100,
1212 /* No independent HP possible */
1213 BAD_NO_INDEP_HP = 0x10,
1214 /* Primary DAC shared with main CLFE */
1215 BAD_SHARED_CLFE = 0x10,
1216 /* Primary DAC shared with extra surrounds */
1217 BAD_SHARED_EXTRA_SURROUND = 0x10,
1218 /* Volume widget is shared */
1219 BAD_SHARED_VOL = 0x10,
1222 /* look for widgets in the given path which are appropriate for
1223 * volume and mute controls, and assign the values to ctls[].
1225 * When no appropriate widget is found in the path, the badness value
1226 * is incremented depending on the situation. The function returns the
1227 * total badness for both volume and mute controls.
1229 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1231 struct hda_gen_spec *spec = codec->spec;
1237 return BAD_SHARED_VOL * 2;
1239 if (path->ctls[NID_PATH_VOL_CTL] ||
1240 path->ctls[NID_PATH_MUTE_CTL])
1241 return 0; /* already evaluated */
1243 nid = look_for_out_vol_nid(codec, path);
1245 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1246 if (spec->dac_min_mute)
1247 val |= HDA_AMP_VAL_MIN_MUTE;
1248 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1249 badness += BAD_SHARED_VOL;
1251 path->ctls[NID_PATH_VOL_CTL] = val;
1253 badness += BAD_SHARED_VOL;
1254 nid = look_for_out_mute_nid(codec, path);
1256 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1257 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1258 nid_has_mute(codec, nid, HDA_OUTPUT))
1259 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1261 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1262 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1263 badness += BAD_SHARED_VOL;
1265 path->ctls[NID_PATH_MUTE_CTL] = val;
1267 badness += BAD_SHARED_VOL;
1271 const struct badness_table hda_main_out_badness = {
1272 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1273 .no_dac = BAD_NO_DAC,
1274 .shared_primary = BAD_NO_PRIMARY_DAC,
1275 .shared_surr = BAD_SHARED_SURROUND,
1276 .shared_clfe = BAD_SHARED_CLFE,
1277 .shared_surr_main = BAD_SHARED_SURROUND,
1279 EXPORT_SYMBOL_GPL(hda_main_out_badness);
1281 const struct badness_table hda_extra_out_badness = {
1282 .no_primary_dac = BAD_NO_DAC,
1283 .no_dac = BAD_NO_DAC,
1284 .shared_primary = BAD_NO_EXTRA_DAC,
1285 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1286 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1287 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1289 EXPORT_SYMBOL_GPL(hda_extra_out_badness);
1291 /* get the DAC of the primary output corresponding to the given array index */
1292 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1294 struct hda_gen_spec *spec = codec->spec;
1295 struct auto_pin_cfg *cfg = &spec->autocfg;
1297 if (cfg->line_outs > idx)
1298 return spec->private_dac_nids[idx];
1299 idx -= cfg->line_outs;
1300 if (spec->multi_ios > idx)
1301 return spec->multi_io[idx].dac;
1305 /* return the DAC if it's reachable, otherwise zero */
1306 static inline hda_nid_t try_dac(struct hda_codec *codec,
1307 hda_nid_t dac, hda_nid_t pin)
1309 return is_reachable_path(codec, dac, pin) ? dac : 0;
1312 /* try to assign DACs to pins and return the resultant badness */
1313 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1314 const hda_nid_t *pins, hda_nid_t *dacs,
1316 const struct badness_table *bad)
1318 struct hda_gen_spec *spec = codec->spec;
1326 for (i = 0; i < num_outs; i++) {
1327 struct nid_path *path;
1328 hda_nid_t pin = pins[i];
1330 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1332 badness += assign_out_path_ctls(codec, path);
1336 dacs[i] = get_preferred_dac(codec, pin);
1338 if (is_dac_already_used(codec, dacs[i]))
1339 badness += bad->shared_primary;
1343 dacs[i] = look_for_dac(codec, pin, false);
1344 if (!dacs[i] && !i) {
1345 /* try to steal the DAC of surrounds for the front */
1346 for (j = 1; j < num_outs; j++) {
1347 if (is_reachable_path(codec, dacs[j], pin)) {
1350 invalidate_nid_path(codec, path_idx[j]);
1359 dac = try_dac(codec, get_primary_out(codec, i), pin);
1361 dac = try_dac(codec, dacs[0], pin);
1363 dac = try_dac(codec, get_primary_out(codec, i), pin);
1366 badness += bad->shared_primary;
1368 badness += bad->shared_surr;
1370 badness += bad->shared_clfe;
1371 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1372 dac = spec->private_dac_nids[0];
1373 badness += bad->shared_surr_main;
1375 badness += bad->no_primary_dac;
1377 badness += bad->no_dac;
1381 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1382 if (!path && !i && spec->mixer_nid) {
1383 /* try with aamix */
1384 path = snd_hda_add_new_path(codec, dac, pin, 0);
1388 badness += bad->no_dac;
1390 /* print_nid_path(codec, "output", path); */
1391 path->active = true;
1392 path_idx[i] = snd_hda_get_path_idx(codec, path);
1393 badness += assign_out_path_ctls(codec, path);
1400 /* return NID if the given pin has only a single connection to a certain DAC */
1401 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1403 struct hda_gen_spec *spec = codec->spec;
1405 hda_nid_t nid_found = 0;
1407 for (i = 0; i < spec->num_all_dacs; i++) {
1408 hda_nid_t nid = spec->all_dacs[i];
1409 if (!nid || is_dac_already_used(codec, nid))
1411 if (is_reachable_path(codec, nid, pin)) {
1420 /* check whether the given pin can be a multi-io pin */
1421 static bool can_be_multiio_pin(struct hda_codec *codec,
1422 unsigned int location, hda_nid_t nid)
1424 unsigned int defcfg, caps;
1426 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1427 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1429 if (location && get_defcfg_location(defcfg) != location)
1431 caps = snd_hda_query_pin_caps(codec, nid);
1432 if (!(caps & AC_PINCAP_OUT))
1437 /* count the number of input pins that are capable to be multi-io */
1438 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1440 struct hda_gen_spec *spec = codec->spec;
1441 struct auto_pin_cfg *cfg = &spec->autocfg;
1442 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1443 unsigned int location = get_defcfg_location(defcfg);
1447 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1448 for (i = 0; i < cfg->num_inputs; i++) {
1449 if (cfg->inputs[i].type != type)
1451 if (can_be_multiio_pin(codec, location,
1452 cfg->inputs[i].pin))
1462 * When hardwired is set, try to fill ony hardwired pins, and returns
1463 * zero if any pins are filled, non-zero if nothing found.
1464 * When hardwired is off, try to fill possible input pins, and returns
1465 * the badness value.
1467 static int fill_multi_ios(struct hda_codec *codec,
1468 hda_nid_t reference_pin,
1471 struct hda_gen_spec *spec = codec->spec;
1472 struct auto_pin_cfg *cfg = &spec->autocfg;
1473 int type, i, j, num_pins, old_pins;
1474 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1475 unsigned int location = get_defcfg_location(defcfg);
1477 struct nid_path *path;
1479 old_pins = spec->multi_ios;
1483 num_pins = count_multiio_pins(codec, reference_pin);
1487 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1488 for (i = 0; i < cfg->num_inputs; i++) {
1489 hda_nid_t nid = cfg->inputs[i].pin;
1492 if (cfg->inputs[i].type != type)
1494 if (!can_be_multiio_pin(codec, location, nid))
1496 for (j = 0; j < spec->multi_ios; j++) {
1497 if (nid == spec->multi_io[j].pin)
1500 if (j < spec->multi_ios)
1504 dac = get_dac_if_single(codec, nid);
1506 dac = look_for_dac(codec, nid, false);
1511 path = snd_hda_add_new_path(codec, dac, nid,
1517 /* print_nid_path(codec, "multiio", path); */
1518 spec->multi_io[spec->multi_ios].pin = nid;
1519 spec->multi_io[spec->multi_ios].dac = dac;
1520 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1521 snd_hda_get_path_idx(codec, path);
1523 if (spec->multi_ios >= 2)
1529 badness = BAD_MULTI_IO;
1530 if (old_pins == spec->multi_ios) {
1532 return 1; /* nothing found */
1534 return badness; /* no badness if nothing found */
1536 if (!hardwired && spec->multi_ios < 2) {
1537 /* cancel newly assigned paths */
1538 spec->paths.used -= spec->multi_ios - old_pins;
1539 spec->multi_ios = old_pins;
1543 /* assign volume and mute controls */
1544 for (i = old_pins; i < spec->multi_ios; i++) {
1545 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1546 badness += assign_out_path_ctls(codec, path);
1552 /* map DACs for all pins in the list if they are single connections */
1553 static bool map_singles(struct hda_codec *codec, int outs,
1554 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1556 struct hda_gen_spec *spec = codec->spec;
1559 for (i = 0; i < outs; i++) {
1560 struct nid_path *path;
1564 dac = get_dac_if_single(codec, pins[i]);
1567 path = snd_hda_add_new_path(codec, dac, pins[i],
1569 if (!path && !i && spec->mixer_nid)
1570 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1574 /* print_nid_path(codec, "output", path); */
1575 path->active = true;
1576 path_idx[i] = snd_hda_get_path_idx(codec, path);
1582 static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1584 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1585 spec->aamix_out_paths[2];
1588 /* create a new path including aamix if available, and return its index */
1589 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1591 struct hda_gen_spec *spec = codec->spec;
1592 struct nid_path *path;
1593 hda_nid_t path_dac, dac, pin;
1595 path = snd_hda_get_path_from_idx(codec, path_idx);
1596 if (!path || !path->depth ||
1597 is_nid_contained(path, spec->mixer_nid))
1599 path_dac = path->path[0];
1600 dac = spec->private_dac_nids[0];
1601 pin = path->path[path->depth - 1];
1602 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1604 if (dac != path_dac)
1606 else if (spec->multiout.hp_out_nid[0])
1607 dac = spec->multiout.hp_out_nid[0];
1608 else if (spec->multiout.extra_out_nid[0])
1609 dac = spec->multiout.extra_out_nid[0];
1613 path = snd_hda_add_new_path(codec, dac, pin,
1618 /* print_nid_path(codec, "output-aamix", path); */
1619 path->active = false; /* unused as default */
1620 path->pin_fixed = true; /* static route */
1621 return snd_hda_get_path_idx(codec, path);
1624 /* check whether the independent HP is available with the current config */
1625 static bool indep_hp_possible(struct hda_codec *codec)
1627 struct hda_gen_spec *spec = codec->spec;
1628 struct auto_pin_cfg *cfg = &spec->autocfg;
1629 struct nid_path *path;
1632 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1633 idx = spec->out_paths[0];
1635 idx = spec->hp_paths[0];
1636 path = snd_hda_get_path_from_idx(codec, idx);
1640 /* assume no path conflicts unless aamix is involved */
1641 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1644 /* check whether output paths contain aamix */
1645 for (i = 0; i < cfg->line_outs; i++) {
1646 if (spec->out_paths[i] == idx)
1648 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1649 if (path && is_nid_contained(path, spec->mixer_nid))
1652 for (i = 0; i < cfg->speaker_outs; i++) {
1653 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1654 if (path && is_nid_contained(path, spec->mixer_nid))
1661 /* fill the empty entries in the dac array for speaker/hp with the
1662 * shared dac pointed by the paths
1664 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1665 hda_nid_t *dacs, int *path_idx)
1667 struct nid_path *path;
1670 for (i = 0; i < num_outs; i++) {
1673 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1676 dacs[i] = path->path[0];
1680 /* fill in the dac_nids table from the parsed pin configuration */
1681 static int fill_and_eval_dacs(struct hda_codec *codec,
1682 bool fill_hardwired,
1683 bool fill_mio_first)
1685 struct hda_gen_spec *spec = codec->spec;
1686 struct auto_pin_cfg *cfg = &spec->autocfg;
1687 int i, err, badness;
1689 /* set num_dacs once to full for look_for_dac() */
1690 spec->multiout.num_dacs = cfg->line_outs;
1691 spec->multiout.dac_nids = spec->private_dac_nids;
1692 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1693 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1694 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1695 spec->multi_ios = 0;
1696 snd_array_free(&spec->paths);
1698 /* clear path indices */
1699 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1700 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1701 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1702 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1703 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1704 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1705 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1706 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1710 /* fill hard-wired DACs first */
1711 if (fill_hardwired) {
1714 mapped = map_singles(codec, cfg->line_outs,
1716 spec->private_dac_nids,
1718 mapped |= map_singles(codec, cfg->hp_outs,
1720 spec->multiout.hp_out_nid,
1722 mapped |= map_singles(codec, cfg->speaker_outs,
1724 spec->multiout.extra_out_nid,
1725 spec->speaker_paths);
1726 if (!spec->no_multi_io &&
1727 fill_mio_first && cfg->line_outs == 1 &&
1728 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1729 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1736 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1737 spec->private_dac_nids, spec->out_paths,
1738 spec->main_out_badness);
1740 if (!spec->no_multi_io && fill_mio_first &&
1741 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1742 /* try to fill multi-io first */
1743 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1746 /* we don't count badness at this stage yet */
1749 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1750 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1751 spec->multiout.hp_out_nid,
1753 spec->extra_out_badness);
1758 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1759 err = try_assign_dacs(codec, cfg->speaker_outs,
1761 spec->multiout.extra_out_nid,
1762 spec->speaker_paths,
1763 spec->extra_out_badness);
1768 if (!spec->no_multi_io &&
1769 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1770 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1776 if (spec->mixer_nid) {
1777 spec->aamix_out_paths[0] =
1778 check_aamix_out_path(codec, spec->out_paths[0]);
1779 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1780 spec->aamix_out_paths[1] =
1781 check_aamix_out_path(codec, spec->hp_paths[0]);
1782 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1783 spec->aamix_out_paths[2] =
1784 check_aamix_out_path(codec, spec->speaker_paths[0]);
1787 if (!spec->no_multi_io &&
1788 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1789 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1790 spec->multi_ios = 1; /* give badness */
1792 /* re-count num_dacs and squash invalid entries */
1793 spec->multiout.num_dacs = 0;
1794 for (i = 0; i < cfg->line_outs; i++) {
1795 if (spec->private_dac_nids[i])
1796 spec->multiout.num_dacs++;
1798 memmove(spec->private_dac_nids + i,
1799 spec->private_dac_nids + i + 1,
1800 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1801 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1805 spec->ext_channel_count = spec->min_channel_count =
1806 spec->multiout.num_dacs * 2;
1808 if (spec->multi_ios == 2) {
1809 for (i = 0; i < 2; i++)
1810 spec->private_dac_nids[spec->multiout.num_dacs++] =
1811 spec->multi_io[i].dac;
1812 } else if (spec->multi_ios) {
1813 spec->multi_ios = 0;
1814 badness += BAD_MULTI_IO;
1817 if (spec->indep_hp && !indep_hp_possible(codec))
1818 badness += BAD_NO_INDEP_HP;
1820 /* re-fill the shared DAC for speaker / headphone */
1821 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1822 refill_shared_dacs(codec, cfg->hp_outs,
1823 spec->multiout.hp_out_nid,
1825 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1826 refill_shared_dacs(codec, cfg->speaker_outs,
1827 spec->multiout.extra_out_nid,
1828 spec->speaker_paths);
1833 #define DEBUG_BADNESS
1835 #ifdef DEBUG_BADNESS
1836 #define debug_badness(fmt, ...) \
1837 codec_dbg(codec, fmt, ##__VA_ARGS__)
1839 #define debug_badness(fmt, ...) \
1840 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1843 #ifdef DEBUG_BADNESS
1844 static inline void print_nid_path_idx(struct hda_codec *codec,
1845 const char *pfx, int idx)
1847 struct nid_path *path;
1849 path = snd_hda_get_path_from_idx(codec, idx);
1851 print_nid_path(codec, pfx, path);
1854 static void debug_show_configs(struct hda_codec *codec,
1855 struct auto_pin_cfg *cfg)
1857 struct hda_gen_spec *spec = codec->spec;
1858 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1861 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1862 cfg->line_out_pins[0], cfg->line_out_pins[1],
1863 cfg->line_out_pins[2], cfg->line_out_pins[3],
1864 spec->multiout.dac_nids[0],
1865 spec->multiout.dac_nids[1],
1866 spec->multiout.dac_nids[2],
1867 spec->multiout.dac_nids[3],
1868 lo_type[cfg->line_out_type]);
1869 for (i = 0; i < cfg->line_outs; i++)
1870 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1871 if (spec->multi_ios > 0)
1872 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1874 spec->multi_io[0].pin, spec->multi_io[1].pin,
1875 spec->multi_io[0].dac, spec->multi_io[1].dac);
1876 for (i = 0; i < spec->multi_ios; i++)
1877 print_nid_path_idx(codec, " mio",
1878 spec->out_paths[cfg->line_outs + i]);
1880 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1881 cfg->hp_pins[0], cfg->hp_pins[1],
1882 cfg->hp_pins[2], cfg->hp_pins[3],
1883 spec->multiout.hp_out_nid[0],
1884 spec->multiout.hp_out_nid[1],
1885 spec->multiout.hp_out_nid[2],
1886 spec->multiout.hp_out_nid[3]);
1887 for (i = 0; i < cfg->hp_outs; i++)
1888 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1889 if (cfg->speaker_outs)
1890 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1891 cfg->speaker_pins[0], cfg->speaker_pins[1],
1892 cfg->speaker_pins[2], cfg->speaker_pins[3],
1893 spec->multiout.extra_out_nid[0],
1894 spec->multiout.extra_out_nid[1],
1895 spec->multiout.extra_out_nid[2],
1896 spec->multiout.extra_out_nid[3]);
1897 for (i = 0; i < cfg->speaker_outs; i++)
1898 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1899 for (i = 0; i < 3; i++)
1900 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1903 #define debug_show_configs(codec, cfg) /* NOP */
1906 /* find all available DACs of the codec */
1907 static void fill_all_dac_nids(struct hda_codec *codec)
1909 struct hda_gen_spec *spec = codec->spec;
1912 spec->num_all_dacs = 0;
1913 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1914 for_each_hda_codec_node(nid, codec) {
1915 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1917 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1918 codec_err(codec, "Too many DACs!\n");
1921 spec->all_dacs[spec->num_all_dacs++] = nid;
1925 static int parse_output_paths(struct hda_codec *codec)
1927 struct hda_gen_spec *spec = codec->spec;
1928 struct auto_pin_cfg *cfg = &spec->autocfg;
1929 struct auto_pin_cfg *best_cfg;
1931 int best_badness = INT_MAX;
1933 bool fill_hardwired = true, fill_mio_first = true;
1934 bool best_wired = true, best_mio = true;
1935 bool hp_spk_swapped = false;
1937 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1943 badness = fill_and_eval_dacs(codec, fill_hardwired,
1949 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1950 cfg->line_out_type, fill_hardwired, fill_mio_first,
1952 debug_show_configs(codec, cfg);
1953 if (badness < best_badness) {
1954 best_badness = badness;
1956 best_wired = fill_hardwired;
1957 best_mio = fill_mio_first;
1961 fill_mio_first = !fill_mio_first;
1962 if (!fill_mio_first)
1964 fill_hardwired = !fill_hardwired;
1965 if (!fill_hardwired)
1969 hp_spk_swapped = true;
1970 if (cfg->speaker_outs > 0 &&
1971 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1972 cfg->hp_outs = cfg->line_outs;
1973 memcpy(cfg->hp_pins, cfg->line_out_pins,
1974 sizeof(cfg->hp_pins));
1975 cfg->line_outs = cfg->speaker_outs;
1976 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1977 sizeof(cfg->speaker_pins));
1978 cfg->speaker_outs = 0;
1979 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1980 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1981 fill_hardwired = true;
1984 if (cfg->hp_outs > 0 &&
1985 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1986 cfg->speaker_outs = cfg->line_outs;
1987 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1988 sizeof(cfg->speaker_pins));
1989 cfg->line_outs = cfg->hp_outs;
1990 memcpy(cfg->line_out_pins, cfg->hp_pins,
1991 sizeof(cfg->hp_pins));
1993 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1994 cfg->line_out_type = AUTO_PIN_HP_OUT;
1995 fill_hardwired = true;
2002 debug_badness("==> restoring best_cfg\n");
2004 fill_and_eval_dacs(codec, best_wired, best_mio);
2006 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2007 cfg->line_out_type, best_wired, best_mio);
2008 debug_show_configs(codec, cfg);
2010 if (cfg->line_out_pins[0]) {
2011 struct nid_path *path;
2012 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
2014 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
2015 if (spec->vmaster_nid) {
2016 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2017 HDA_OUTPUT, spec->vmaster_tlv);
2018 if (spec->dac_min_mute)
2019 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2023 /* set initial pinctl targets */
2024 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2028 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2029 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2030 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2031 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2032 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2033 set_pin_targets(codec, cfg->speaker_outs,
2034 cfg->speaker_pins, val);
2037 /* clear indep_hp flag if not available */
2038 if (spec->indep_hp && !indep_hp_possible(codec))
2045 /* add playback controls from the parsed DAC table */
2046 static int create_multi_out_ctls(struct hda_codec *codec,
2047 const struct auto_pin_cfg *cfg)
2049 struct hda_gen_spec *spec = codec->spec;
2050 int i, err, noutputs;
2052 noutputs = cfg->line_outs;
2053 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2054 noutputs += spec->multi_ios;
2056 for (i = 0; i < noutputs; i++) {
2059 struct nid_path *path;
2061 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
2065 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
2066 if (!name || !strcmp(name, "CLFE")) {
2068 err = add_vol_ctl(codec, "Center", 0, 1, path);
2071 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2075 err = add_stereo_vol(codec, name, index, path);
2080 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2081 if (!name || !strcmp(name, "CLFE")) {
2082 err = add_sw_ctl(codec, "Center", 0, 1, path);
2085 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2089 err = add_stereo_sw(codec, name, index, path);
2097 static int create_extra_out(struct hda_codec *codec, int path_idx,
2098 const char *pfx, int cidx)
2100 struct nid_path *path;
2103 path = snd_hda_get_path_from_idx(codec, path_idx);
2106 err = add_stereo_vol(codec, pfx, cidx, path);
2109 err = add_stereo_sw(codec, pfx, cidx, path);
2115 /* add playback controls for speaker and HP outputs */
2116 static int create_extra_outs(struct hda_codec *codec, int num_pins,
2117 const int *paths, const char *pfx)
2121 for (i = 0; i < num_pins; i++) {
2123 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2126 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2127 name = "Bass Speaker";
2128 else if (num_pins >= 3) {
2129 snprintf(tmp, sizeof(tmp), "%s %s",
2130 pfx, channel_name[i]);
2136 err = create_extra_out(codec, paths[i], name, idx);
2143 static int create_hp_out_ctls(struct hda_codec *codec)
2145 struct hda_gen_spec *spec = codec->spec;
2146 return create_extra_outs(codec, spec->autocfg.hp_outs,
2151 static int create_speaker_out_ctls(struct hda_codec *codec)
2153 struct hda_gen_spec *spec = codec->spec;
2154 return create_extra_outs(codec, spec->autocfg.speaker_outs,
2155 spec->speaker_paths,
2160 * independent HP controls
2163 static void call_hp_automute(struct hda_codec *codec,
2164 struct hda_jack_callback *jack);
2165 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2166 struct snd_ctl_elem_info *uinfo)
2168 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2171 static int indep_hp_get(struct snd_kcontrol *kcontrol,
2172 struct snd_ctl_elem_value *ucontrol)
2174 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2175 struct hda_gen_spec *spec = codec->spec;
2176 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2180 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2181 int nomix_path_idx, int mix_path_idx,
2184 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2185 struct snd_ctl_elem_value *ucontrol)
2187 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2188 struct hda_gen_spec *spec = codec->spec;
2189 unsigned int select = ucontrol->value.enumerated.item[0];
2192 mutex_lock(&spec->pcm_mutex);
2193 if (spec->active_streams) {
2198 if (spec->indep_hp_enabled != select) {
2200 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2201 dacp = &spec->private_dac_nids[0];
2203 dacp = &spec->multiout.hp_out_nid[0];
2205 /* update HP aamix paths in case it conflicts with indep HP */
2206 if (spec->have_aamix_ctl) {
2207 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2208 update_aamix_paths(codec, spec->aamix_mode,
2210 spec->aamix_out_paths[0],
2211 spec->autocfg.line_out_type);
2213 update_aamix_paths(codec, spec->aamix_mode,
2215 spec->aamix_out_paths[1],
2219 spec->indep_hp_enabled = select;
2220 if (spec->indep_hp_enabled)
2223 *dacp = spec->alt_dac_nid;
2225 call_hp_automute(codec, NULL);
2229 mutex_unlock(&spec->pcm_mutex);
2233 static const struct snd_kcontrol_new indep_hp_ctl = {
2234 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2235 .name = "Independent HP",
2236 .info = indep_hp_info,
2237 .get = indep_hp_get,
2238 .put = indep_hp_put,
2242 static int create_indep_hp_ctls(struct hda_codec *codec)
2244 struct hda_gen_spec *spec = codec->spec;
2247 if (!spec->indep_hp)
2249 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2250 dac = spec->multiout.dac_nids[0];
2252 dac = spec->multiout.hp_out_nid[0];
2258 spec->indep_hp_enabled = false;
2259 spec->alt_dac_nid = dac;
2260 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2266 * channel mode enum control
2269 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2270 struct snd_ctl_elem_info *uinfo)
2272 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2273 struct hda_gen_spec *spec = codec->spec;
2276 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2278 uinfo->value.enumerated.items = spec->multi_ios + 1;
2279 if (uinfo->value.enumerated.item > spec->multi_ios)
2280 uinfo->value.enumerated.item = spec->multi_ios;
2281 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2282 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2286 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2287 struct snd_ctl_elem_value *ucontrol)
2289 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2290 struct hda_gen_spec *spec = codec->spec;
2291 ucontrol->value.enumerated.item[0] =
2292 (spec->ext_channel_count - spec->min_channel_count) / 2;
2296 static inline struct nid_path *
2297 get_multiio_path(struct hda_codec *codec, int idx)
2299 struct hda_gen_spec *spec = codec->spec;
2300 return snd_hda_get_path_from_idx(codec,
2301 spec->out_paths[spec->autocfg.line_outs + idx]);
2304 static void update_automute_all(struct hda_codec *codec);
2306 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2307 * used for output paths
2309 static bool aamix_default(struct hda_gen_spec *spec)
2311 return !spec->have_aamix_ctl || spec->aamix_mode;
2314 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2316 struct hda_gen_spec *spec = codec->spec;
2317 hda_nid_t nid = spec->multi_io[idx].pin;
2318 struct nid_path *path;
2320 path = get_multiio_path(codec, idx);
2324 if (path->active == output)
2328 set_pin_target(codec, nid, PIN_OUT, true);
2329 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2330 set_pin_eapd(codec, nid, true);
2332 set_pin_eapd(codec, nid, false);
2333 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2334 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2335 path_power_down_sync(codec, path);
2338 /* update jack retasking in case it modifies any of them */
2339 update_automute_all(codec);
2344 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2345 struct snd_ctl_elem_value *ucontrol)
2347 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2348 struct hda_gen_spec *spec = codec->spec;
2351 ch = ucontrol->value.enumerated.item[0];
2352 if (ch < 0 || ch > spec->multi_ios)
2354 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2356 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2357 for (i = 0; i < spec->multi_ios; i++)
2358 set_multi_io(codec, i, i < ch);
2359 spec->multiout.max_channels = max(spec->ext_channel_count,
2360 spec->const_channel_count);
2361 if (spec->need_dac_fix)
2362 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2366 static const struct snd_kcontrol_new channel_mode_enum = {
2367 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2368 .name = "Channel Mode",
2369 .info = ch_mode_info,
2374 static int create_multi_channel_mode(struct hda_codec *codec)
2376 struct hda_gen_spec *spec = codec->spec;
2378 if (spec->multi_ios > 0) {
2379 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2386 * aamix loopback enable/disable switch
2389 #define loopback_mixing_info indep_hp_info
2391 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2392 struct snd_ctl_elem_value *ucontrol)
2394 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2395 struct hda_gen_spec *spec = codec->spec;
2396 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2400 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2401 int nomix_path_idx, int mix_path_idx,
2404 struct hda_gen_spec *spec = codec->spec;
2405 struct nid_path *nomix_path, *mix_path;
2407 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2408 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2409 if (!nomix_path || !mix_path)
2412 /* if HP aamix path is driven from a different DAC and the
2413 * independent HP mode is ON, can't turn on aamix path
2415 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2416 mix_path->path[0] != spec->alt_dac_nid)
2420 snd_hda_activate_path(codec, nomix_path, false, true);
2421 snd_hda_activate_path(codec, mix_path, true, true);
2422 path_power_down_sync(codec, nomix_path);
2424 snd_hda_activate_path(codec, mix_path, false, false);
2425 snd_hda_activate_path(codec, nomix_path, true, false);
2426 path_power_down_sync(codec, mix_path);
2430 /* re-initialize the output paths; only called from loopback_mixing_put() */
2431 static void update_output_paths(struct hda_codec *codec, int num_outs,
2434 struct hda_gen_spec *spec = codec->spec;
2435 struct nid_path *path;
2438 for (i = 0; i < num_outs; i++) {
2439 path = snd_hda_get_path_from_idx(codec, paths[i]);
2441 snd_hda_activate_path(codec, path, path->active,
2446 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2447 struct snd_ctl_elem_value *ucontrol)
2449 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2450 struct hda_gen_spec *spec = codec->spec;
2451 const struct auto_pin_cfg *cfg = &spec->autocfg;
2452 unsigned int val = ucontrol->value.enumerated.item[0];
2454 if (val == spec->aamix_mode)
2456 spec->aamix_mode = val;
2457 if (has_aamix_out_paths(spec)) {
2458 update_aamix_paths(codec, val, spec->out_paths[0],
2459 spec->aamix_out_paths[0],
2460 cfg->line_out_type);
2461 update_aamix_paths(codec, val, spec->hp_paths[0],
2462 spec->aamix_out_paths[1],
2464 update_aamix_paths(codec, val, spec->speaker_paths[0],
2465 spec->aamix_out_paths[2],
2466 AUTO_PIN_SPEAKER_OUT);
2468 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2469 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2470 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2471 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2472 update_output_paths(codec, cfg->speaker_outs,
2473 spec->speaker_paths);
2478 static const struct snd_kcontrol_new loopback_mixing_enum = {
2479 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2480 .name = "Loopback Mixing",
2481 .info = loopback_mixing_info,
2482 .get = loopback_mixing_get,
2483 .put = loopback_mixing_put,
2486 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2488 struct hda_gen_spec *spec = codec->spec;
2490 if (!spec->mixer_nid)
2492 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2494 spec->have_aamix_ctl = 1;
2495 /* if no explicit aamix path is present (e.g. for Realtek codecs),
2496 * enable aamix as default -- just for compatibility
2498 spec->aamix_mode = !has_aamix_out_paths(spec);
2503 * shared headphone/mic handling
2506 static void call_update_outputs(struct hda_codec *codec);
2508 /* for shared I/O, change the pin-control accordingly */
2509 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2511 struct hda_gen_spec *spec = codec->spec;
2516 pin = spec->hp_mic_pin;
2517 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2520 val = snd_hda_codec_get_pin_target(codec, pin);
2530 val = snd_hda_get_default_vref(codec, pin);
2531 /* if the HP pin doesn't support VREF and the codec driver gives an
2532 * alternative pin, set up the VREF on that pin instead
2534 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2535 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2536 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2537 if (vref_val != AC_PINCTL_VREF_HIZ)
2538 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2539 PIN_IN | (as_mic ? vref_val : 0));
2542 if (!spec->hp_mic_jack_modes) {
2547 set_pin_target(codec, pin, val, true);
2548 call_hp_automute(codec, NULL);
2552 /* create a shared input with the headphone out */
2553 static int create_hp_mic(struct hda_codec *codec)
2555 struct hda_gen_spec *spec = codec->spec;
2556 struct auto_pin_cfg *cfg = &spec->autocfg;
2557 unsigned int defcfg;
2560 if (!spec->hp_mic) {
2561 if (spec->suppress_hp_mic_detect)
2563 /* automatic detection: only if no input or a single internal
2564 * input pin is found, try to detect the shared hp/mic
2566 if (cfg->num_inputs > 1)
2568 else if (cfg->num_inputs == 1) {
2569 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2570 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2575 spec->hp_mic = 0; /* clear once */
2576 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2580 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2581 nid = cfg->line_out_pins[0];
2582 else if (cfg->hp_outs > 0)
2583 nid = cfg->hp_pins[0];
2587 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2588 return 0; /* no input */
2590 cfg->inputs[cfg->num_inputs].pin = nid;
2591 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2592 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2595 spec->hp_mic_pin = nid;
2596 /* we can't handle auto-mic together with HP-mic */
2597 spec->suppress_auto_mic = 1;
2598 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
2606 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2608 static const char * const out_jack_texts[] = {
2609 "Line Out", "Headphone Out",
2612 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2613 struct snd_ctl_elem_info *uinfo)
2615 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2618 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2619 struct snd_ctl_elem_value *ucontrol)
2621 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2622 hda_nid_t nid = kcontrol->private_value;
2623 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2624 ucontrol->value.enumerated.item[0] = 1;
2626 ucontrol->value.enumerated.item[0] = 0;
2630 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2631 struct snd_ctl_elem_value *ucontrol)
2633 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2634 hda_nid_t nid = kcontrol->private_value;
2637 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2638 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2640 snd_hda_set_pin_ctl_cache(codec, nid, val);
2644 static const struct snd_kcontrol_new out_jack_mode_enum = {
2645 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2646 .info = out_jack_mode_info,
2647 .get = out_jack_mode_get,
2648 .put = out_jack_mode_put,
2651 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2653 struct hda_gen_spec *spec = codec->spec;
2656 for (i = 0; i < spec->kctls.used; i++) {
2657 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2658 if (!strcmp(kctl->name, name) && kctl->index == idx)
2664 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2665 char *name, size_t name_len)
2667 struct hda_gen_spec *spec = codec->spec;
2670 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2671 strlcat(name, " Jack Mode", name_len);
2673 for (; find_kctl_name(codec, name, idx); idx++)
2677 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2679 struct hda_gen_spec *spec = codec->spec;
2680 if (spec->add_jack_modes) {
2681 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2682 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2688 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2691 struct hda_gen_spec *spec = codec->spec;
2694 for (i = 0; i < num_pins; i++) {
2695 hda_nid_t pin = pins[i];
2696 if (pin == spec->hp_mic_pin)
2698 if (get_out_jack_num_items(codec, pin) > 1) {
2699 struct snd_kcontrol_new *knew;
2700 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2701 get_jack_mode_name(codec, pin, name, sizeof(name));
2702 knew = snd_hda_gen_add_kctl(spec, name,
2703 &out_jack_mode_enum);
2706 knew->private_value = pin;
2717 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2720 static const char * const vref_texts[NUM_VREFS] = {
2721 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2722 "", "Mic 80pc Bias", "Mic 100pc Bias"
2725 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2727 unsigned int pincap;
2729 pincap = snd_hda_query_pin_caps(codec, pin);
2730 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2731 /* filter out unusual vrefs */
2732 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2736 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2737 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2739 unsigned int i, n = 0;
2741 for (i = 0; i < NUM_VREFS; i++) {
2742 if (vref_caps & (1 << i)) {
2751 /* convert back from the vref ctl index to the enum item index */
2752 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2754 unsigned int i, n = 0;
2756 for (i = 0; i < NUM_VREFS; i++) {
2759 if (vref_caps & (1 << i))
2765 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2766 struct snd_ctl_elem_info *uinfo)
2768 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2769 hda_nid_t nid = kcontrol->private_value;
2770 unsigned int vref_caps = get_vref_caps(codec, nid);
2772 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2774 /* set the right text */
2775 strcpy(uinfo->value.enumerated.name,
2776 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2780 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2781 struct snd_ctl_elem_value *ucontrol)
2783 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2784 hda_nid_t nid = kcontrol->private_value;
2785 unsigned int vref_caps = get_vref_caps(codec, nid);
2788 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2789 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2793 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2794 struct snd_ctl_elem_value *ucontrol)
2796 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2797 hda_nid_t nid = kcontrol->private_value;
2798 unsigned int vref_caps = get_vref_caps(codec, nid);
2799 unsigned int val, idx;
2801 val = snd_hda_codec_get_pin_target(codec, nid);
2802 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2803 if (idx == ucontrol->value.enumerated.item[0])
2806 val &= ~AC_PINCTL_VREFEN;
2807 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2808 snd_hda_set_pin_ctl_cache(codec, nid, val);
2812 static const struct snd_kcontrol_new in_jack_mode_enum = {
2813 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2814 .info = in_jack_mode_info,
2815 .get = in_jack_mode_get,
2816 .put = in_jack_mode_put,
2819 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2821 struct hda_gen_spec *spec = codec->spec;
2823 if (spec->add_jack_modes)
2824 nitems = hweight32(get_vref_caps(codec, pin));
2825 return nitems ? nitems : 1;
2828 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2830 struct hda_gen_spec *spec = codec->spec;
2831 struct snd_kcontrol_new *knew;
2832 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2833 unsigned int defcfg;
2835 if (pin == spec->hp_mic_pin)
2836 return 0; /* already done in create_out_jack_mode() */
2838 /* no jack mode for fixed pins */
2839 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2840 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2843 /* no multiple vref caps? */
2844 if (get_in_jack_num_items(codec, pin) <= 1)
2847 get_jack_mode_name(codec, pin, name, sizeof(name));
2848 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2851 knew->private_value = pin;
2856 * HP/mic shared jack mode
2858 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2859 struct snd_ctl_elem_info *uinfo)
2861 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2862 hda_nid_t nid = kcontrol->private_value;
2863 int out_jacks = get_out_jack_num_items(codec, nid);
2864 int in_jacks = get_in_jack_num_items(codec, nid);
2865 const char *text = NULL;
2868 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2870 uinfo->value.enumerated.items = out_jacks + in_jacks;
2871 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2872 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2873 idx = uinfo->value.enumerated.item;
2874 if (idx < out_jacks) {
2876 text = out_jack_texts[idx];
2878 text = "Headphone Out";
2882 unsigned int vref_caps = get_vref_caps(codec, nid);
2883 text = vref_texts[get_vref_idx(vref_caps, idx)];
2888 strcpy(uinfo->value.enumerated.name, text);
2892 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2894 int out_jacks = get_out_jack_num_items(codec, nid);
2895 int in_jacks = get_in_jack_num_items(codec, nid);
2896 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2899 if (val & PIN_OUT) {
2900 if (out_jacks > 1 && val == PIN_HP)
2902 } else if (val & PIN_IN) {
2905 unsigned int vref_caps = get_vref_caps(codec, nid);
2906 val &= AC_PINCTL_VREFEN;
2907 idx += cvt_from_vref_idx(vref_caps, val);
2913 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2914 struct snd_ctl_elem_value *ucontrol)
2916 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2917 hda_nid_t nid = kcontrol->private_value;
2918 ucontrol->value.enumerated.item[0] =
2919 get_cur_hp_mic_jack_mode(codec, nid);
2923 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2924 struct snd_ctl_elem_value *ucontrol)
2926 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2927 hda_nid_t nid = kcontrol->private_value;
2928 int out_jacks = get_out_jack_num_items(codec, nid);
2929 int in_jacks = get_in_jack_num_items(codec, nid);
2930 unsigned int val, oldval, idx;
2932 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2933 idx = ucontrol->value.enumerated.item[0];
2937 if (idx < out_jacks) {
2939 val = idx ? PIN_HP : PIN_OUT;
2945 unsigned int vref_caps = get_vref_caps(codec, nid);
2946 val = snd_hda_codec_get_pin_target(codec, nid);
2947 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2948 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
2950 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
2952 snd_hda_set_pin_ctl_cache(codec, nid, val);
2953 call_hp_automute(codec, NULL);
2958 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2959 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2960 .info = hp_mic_jack_mode_info,
2961 .get = hp_mic_jack_mode_get,
2962 .put = hp_mic_jack_mode_put,
2965 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2967 struct hda_gen_spec *spec = codec->spec;
2968 struct snd_kcontrol_new *knew;
2970 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2971 &hp_mic_jack_mode_enum);
2974 knew->private_value = pin;
2975 spec->hp_mic_jack_modes = 1;
2983 /* add the powersave loopback-list entry */
2984 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2986 struct hda_amp_list *list;
2988 list = snd_array_new(&spec->loopback_list);
2992 list->dir = HDA_INPUT;
2994 spec->loopback.amplist = spec->loopback_list.list;
2998 /* return true if either a volume or a mute amp is found for the given
2999 * aamix path; the amp has to be either in the mixer node or its direct leaf
3001 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3002 hda_nid_t pin, unsigned int *mix_val,
3003 unsigned int *mute_val)
3006 const hda_nid_t *list;
3009 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3013 *mix_val = *mute_val = 0;
3014 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3015 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3016 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3017 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3018 if (*mix_val && *mute_val)
3021 /* check leaf node */
3022 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3023 if (num_conns < idx)
3026 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3027 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
3028 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3029 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3030 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
3031 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3033 return *mix_val || *mute_val;
3036 /* create input playback/capture controls for the given pin */
3037 static int new_analog_input(struct hda_codec *codec, int input_idx,
3038 hda_nid_t pin, const char *ctlname, int ctlidx,
3041 struct hda_gen_spec *spec = codec->spec;
3042 struct nid_path *path;
3043 unsigned int mix_val, mute_val;
3046 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3049 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
3052 print_nid_path(codec, "loopback", path);
3053 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
3055 idx = path->idx[path->depth - 1];
3057 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
3060 path->ctls[NID_PATH_VOL_CTL] = mix_val;
3064 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
3067 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
3070 path->active = true;
3071 path->stream_enabled = true; /* no DAC/ADC involved */
3072 err = add_loopback_list(spec, mix_nid, idx);
3076 if (spec->mixer_nid != spec->mixer_merge_nid &&
3077 !spec->loopback_merge_path) {
3078 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3079 spec->mixer_merge_nid, 0);
3081 print_nid_path(codec, "loopback-merge", path);
3082 path->active = true;
3083 path->pin_fixed = true; /* static route */
3084 path->stream_enabled = true; /* no DAC/ADC involved */
3085 spec->loopback_merge_path =
3086 snd_hda_get_path_idx(codec, path);
3093 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3095 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3096 return (pincap & AC_PINCAP_IN) != 0;
3099 /* Parse the codec tree and retrieve ADCs */
3100 static int fill_adc_nids(struct hda_codec *codec)
3102 struct hda_gen_spec *spec = codec->spec;
3104 hda_nid_t *adc_nids = spec->adc_nids;
3105 int max_nums = ARRAY_SIZE(spec->adc_nids);
3108 for_each_hda_codec_node(nid, codec) {
3109 unsigned int caps = get_wcaps(codec, nid);
3110 int type = get_wcaps_type(caps);
3112 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3114 adc_nids[nums] = nid;
3115 if (++nums >= max_nums)
3118 spec->num_adc_nids = nums;
3120 /* copy the detected ADCs to all_adcs[] */
3121 spec->num_all_adcs = nums;
3122 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3127 /* filter out invalid adc_nids that don't give all active input pins;
3128 * if needed, check whether dynamic ADC-switching is available
3130 static int check_dyn_adc_switch(struct hda_codec *codec)
3132 struct hda_gen_spec *spec = codec->spec;
3133 struct hda_input_mux *imux = &spec->input_mux;
3134 unsigned int ok_bits;
3139 for (n = 0; n < spec->num_adc_nids; n++) {
3140 for (i = 0; i < imux->num_items; i++) {
3141 if (!spec->input_paths[i][n])
3144 if (i >= imux->num_items) {
3145 ok_bits |= (1 << n);
3151 /* check whether ADC-switch is possible */
3152 for (i = 0; i < imux->num_items; i++) {
3153 for (n = 0; n < spec->num_adc_nids; n++) {
3154 if (spec->input_paths[i][n]) {
3155 spec->dyn_adc_idx[i] = n;
3161 codec_dbg(codec, "enabling ADC switching\n");
3162 spec->dyn_adc_switch = 1;
3163 } else if (nums != spec->num_adc_nids) {
3164 /* shrink the invalid adcs and input paths */
3166 for (n = 0; n < spec->num_adc_nids; n++) {
3167 if (!(ok_bits & (1 << n)))
3170 spec->adc_nids[nums] = spec->adc_nids[n];
3171 for (i = 0; i < imux->num_items; i++) {
3172 invalidate_nid_path(codec,
3173 spec->input_paths[i][nums]);
3174 spec->input_paths[i][nums] =
3175 spec->input_paths[i][n];
3180 spec->num_adc_nids = nums;
3183 if (imux->num_items == 1 ||
3184 (imux->num_items == 2 && spec->hp_mic)) {
3185 codec_dbg(codec, "reducing to a single ADC\n");
3186 spec->num_adc_nids = 1; /* reduce to a single ADC */
3189 /* single index for individual volumes ctls */
3190 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3191 spec->num_adc_nids = 1;
3196 /* parse capture source paths from the given pin and create imux items */
3197 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3198 int cfg_idx, int num_adcs,
3199 const char *label, int anchor)
3201 struct hda_gen_spec *spec = codec->spec;
3202 struct hda_input_mux *imux = &spec->input_mux;
3203 int imux_idx = imux->num_items;
3204 bool imux_added = false;
3207 for (c = 0; c < num_adcs; c++) {
3208 struct nid_path *path;
3209 hda_nid_t adc = spec->adc_nids[c];
3211 if (!is_reachable_path(codec, pin, adc))
3213 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3216 print_nid_path(codec, "input", path);
3217 spec->input_paths[imux_idx][c] =
3218 snd_hda_get_path_idx(codec, path);
3221 if (spec->hp_mic_pin == pin)
3222 spec->hp_mic_mux_idx = imux->num_items;
3223 spec->imux_pins[imux->num_items] = pin;
3224 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3226 if (spec->dyn_adc_switch)
3227 spec->dyn_adc_idx[imux_idx] = c;
3235 * create playback/capture controls for input pins
3238 /* fill the label for each input at first */
3239 static int fill_input_pin_labels(struct hda_codec *codec)
3241 struct hda_gen_spec *spec = codec->spec;
3242 const struct auto_pin_cfg *cfg = &spec->autocfg;
3245 for (i = 0; i < cfg->num_inputs; i++) {
3246 hda_nid_t pin = cfg->inputs[i].pin;
3250 if (!is_input_pin(codec, pin))
3253 label = hda_get_autocfg_input_label(codec, cfg, i);
3255 for (j = i - 1; j >= 0; j--) {
3256 if (spec->input_labels[j] &&
3257 !strcmp(spec->input_labels[j], label)) {
3258 idx = spec->input_label_idxs[j] + 1;
3263 spec->input_labels[i] = label;
3264 spec->input_label_idxs[i] = idx;
3270 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3272 static int create_input_ctls(struct hda_codec *codec)
3274 struct hda_gen_spec *spec = codec->spec;
3275 const struct auto_pin_cfg *cfg = &spec->autocfg;
3276 hda_nid_t mixer = spec->mixer_nid;
3281 num_adcs = fill_adc_nids(codec);
3285 err = fill_input_pin_labels(codec);
3289 for (i = 0; i < cfg->num_inputs; i++) {
3292 pin = cfg->inputs[i].pin;
3293 if (!is_input_pin(codec, pin))
3297 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3298 val |= snd_hda_get_default_vref(codec, pin);
3299 if (pin != spec->hp_mic_pin &&
3300 !snd_hda_codec_get_pin_target(codec, pin))
3301 set_pin_target(codec, pin, val, false);
3304 if (is_reachable_path(codec, pin, mixer)) {
3305 err = new_analog_input(codec, i, pin,
3306 spec->input_labels[i],
3307 spec->input_label_idxs[i],
3314 err = parse_capture_source(codec, pin, i, num_adcs,
3315 spec->input_labels[i], -mixer);
3319 if (spec->add_jack_modes) {
3320 err = create_in_jack_mode(codec, pin);
3326 /* add stereo mix when explicitly enabled via hint */
3327 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3328 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3333 spec->suppress_auto_mic = 1;
3344 /* get the input path specified by the given adc and imux indices */
3345 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3347 struct hda_gen_spec *spec = codec->spec;
3348 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3352 if (spec->dyn_adc_switch)
3353 adc_idx = spec->dyn_adc_idx[imux_idx];
3354 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3358 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3361 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3364 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3365 struct snd_ctl_elem_info *uinfo)
3367 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3368 struct hda_gen_spec *spec = codec->spec;
3369 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3372 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3373 struct snd_ctl_elem_value *ucontrol)
3375 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3376 struct hda_gen_spec *spec = codec->spec;
3377 /* the ctls are created at once with multiple counts */
3378 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3380 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3384 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3385 struct snd_ctl_elem_value *ucontrol)
3387 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3388 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3389 return mux_select(codec, adc_idx,
3390 ucontrol->value.enumerated.item[0]);
3393 static const struct snd_kcontrol_new cap_src_temp = {
3394 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3395 .name = "Input Source",
3396 .info = mux_enum_info,
3397 .get = mux_enum_get,
3398 .put = mux_enum_put,
3402 * capture volume and capture switch ctls
3405 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3406 struct snd_ctl_elem_value *ucontrol);
3408 /* call the given amp update function for all amps in the imux list at once */
3409 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3410 struct snd_ctl_elem_value *ucontrol,
3411 put_call_t func, int type)
3413 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3414 struct hda_gen_spec *spec = codec->spec;
3415 const struct hda_input_mux *imux;
3416 struct nid_path *path;
3417 int i, adc_idx, err = 0;
3419 imux = &spec->input_mux;
3420 adc_idx = kcontrol->id.index;
3421 mutex_lock(&codec->control_mutex);
3422 for (i = 0; i < imux->num_items; i++) {
3423 path = get_input_path(codec, adc_idx, i);
3424 if (!path || !path->ctls[type])
3426 kcontrol->private_value = path->ctls[type];
3427 err = func(kcontrol, ucontrol);
3431 mutex_unlock(&codec->control_mutex);
3432 if (err >= 0 && spec->cap_sync_hook)
3433 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3437 /* capture volume ctl callbacks */
3438 #define cap_vol_info snd_hda_mixer_amp_volume_info
3439 #define cap_vol_get snd_hda_mixer_amp_volume_get
3440 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3442 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3443 struct snd_ctl_elem_value *ucontrol)
3445 return cap_put_caller(kcontrol, ucontrol,
3446 snd_hda_mixer_amp_volume_put,
3450 static const struct snd_kcontrol_new cap_vol_temp = {
3451 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3452 .name = "Capture Volume",
3453 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3454 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3455 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3456 .info = cap_vol_info,
3459 .tlv = { .c = cap_vol_tlv },
3462 /* capture switch ctl callbacks */
3463 #define cap_sw_info snd_ctl_boolean_stereo_info
3464 #define cap_sw_get snd_hda_mixer_amp_switch_get
3466 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3467 struct snd_ctl_elem_value *ucontrol)
3469 return cap_put_caller(kcontrol, ucontrol,
3470 snd_hda_mixer_amp_switch_put,
3474 static const struct snd_kcontrol_new cap_sw_temp = {
3475 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3476 .name = "Capture Switch",
3477 .info = cap_sw_info,
3482 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3487 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3488 for (depth = 0; depth < 3; depth++) {
3489 if (depth >= path->depth)
3491 i = path->depth - depth - 1;
3492 nid = path->path[i];
3493 if (!path->ctls[NID_PATH_VOL_CTL]) {
3494 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3495 path->ctls[NID_PATH_VOL_CTL] =
3496 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3497 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3498 int idx = path->idx[i];
3499 if (!depth && codec->single_adc_amp)
3501 path->ctls[NID_PATH_VOL_CTL] =
3502 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3505 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3506 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3507 path->ctls[NID_PATH_MUTE_CTL] =
3508 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3509 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3510 int idx = path->idx[i];
3511 if (!depth && codec->single_adc_amp)
3513 path->ctls[NID_PATH_MUTE_CTL] =
3514 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3521 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3523 struct hda_gen_spec *spec = codec->spec;
3524 struct auto_pin_cfg *cfg = &spec->autocfg;
3528 if (!spec->inv_dmic_split)
3530 for (i = 0; i < cfg->num_inputs; i++) {
3531 if (cfg->inputs[i].pin != nid)
3533 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3535 val = snd_hda_codec_get_pincfg(codec, nid);
3536 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3541 /* capture switch put callback for a single control with hook call */
3542 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3543 struct snd_ctl_elem_value *ucontrol)
3545 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3546 struct hda_gen_spec *spec = codec->spec;
3549 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3553 if (spec->cap_sync_hook)
3554 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3559 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3560 int idx, bool is_switch, unsigned int ctl,
3563 struct hda_gen_spec *spec = codec->spec;
3564 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3565 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3566 const char *sfx = is_switch ? "Switch" : "Volume";
3567 unsigned int chs = inv_dmic ? 1 : 3;
3568 struct snd_kcontrol_new *knew;
3574 snprintf(tmpname, sizeof(tmpname),
3575 "%s Capture %s", label, sfx);
3577 snprintf(tmpname, sizeof(tmpname),
3579 knew = add_control(spec, type, tmpname, idx,
3580 amp_val_replace_channels(ctl, chs));
3584 knew->put = cap_single_sw_put;
3588 /* Make independent right kcontrol */
3590 snprintf(tmpname, sizeof(tmpname),
3591 "Inverted %s Capture %s", label, sfx);
3593 snprintf(tmpname, sizeof(tmpname),
3594 "Inverted Capture %s", sfx);
3595 knew = add_control(spec, type, tmpname, idx,
3596 amp_val_replace_channels(ctl, 2));
3600 knew->put = cap_single_sw_put;
3604 /* create single (and simple) capture volume and switch controls */
3605 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3606 unsigned int vol_ctl, unsigned int sw_ctl,
3610 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3613 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3619 /* create bound capture volume and switch controls */
3620 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3621 unsigned int vol_ctl, unsigned int sw_ctl)
3623 struct hda_gen_spec *spec = codec->spec;
3624 struct snd_kcontrol_new *knew;
3627 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3631 knew->private_value = vol_ctl;
3632 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3635 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3639 knew->private_value = sw_ctl;
3640 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3645 /* return the vol ctl when used first in the imux list */
3646 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3648 struct nid_path *path;
3652 path = get_input_path(codec, 0, idx);
3655 ctl = path->ctls[type];
3658 for (i = 0; i < idx - 1; i++) {
3659 path = get_input_path(codec, 0, i);
3660 if (path && path->ctls[type] == ctl)
3666 /* create individual capture volume and switch controls per input */
3667 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3669 struct hda_gen_spec *spec = codec->spec;
3670 struct hda_input_mux *imux = &spec->input_mux;
3673 for (i = 0; i < imux->num_items; i++) {
3677 idx = imux->items[i].index;
3678 if (idx >= spec->autocfg.num_inputs)
3680 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3682 for (type = 0; type < 2; type++) {
3683 err = add_single_cap_ctl(codec,
3684 spec->input_labels[idx],
3685 spec->input_label_idxs[idx],
3687 get_first_cap_ctl(codec, i, type),
3696 static int create_capture_mixers(struct hda_codec *codec)
3698 struct hda_gen_spec *spec = codec->spec;
3699 struct hda_input_mux *imux = &spec->input_mux;
3700 int i, n, nums, err;
3702 if (spec->dyn_adc_switch)
3705 nums = spec->num_adc_nids;
3707 if (!spec->auto_mic && imux->num_items > 1) {
3708 struct snd_kcontrol_new *knew;
3710 name = nums > 1 ? "Input Source" : "Capture Source";
3711 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3717 for (n = 0; n < nums; n++) {
3719 bool multi_cap_vol = spec->multi_cap_vol;
3720 bool inv_dmic = false;
3724 for (i = 0; i < imux->num_items; i++) {
3725 struct nid_path *path;
3726 path = get_input_path(codec, n, i);
3729 parse_capvol_in_path(codec, path);
3731 vol = path->ctls[NID_PATH_VOL_CTL];
3732 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3734 if (!same_amp_caps(codec, vol,
3735 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3736 multi_cap_vol = true;
3739 sw = path->ctls[NID_PATH_MUTE_CTL];
3740 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3742 if (!same_amp_caps(codec, sw,
3743 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3744 multi_cap_vol = true;
3746 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3751 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3753 else if (!multi_cap_vol && !inv_dmic)
3754 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3756 err = create_multi_cap_vol_ctl(codec);
3765 * add mic boosts if needed
3768 /* check whether the given amp is feasible as a boost volume */
3769 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3774 if (!nid_has_volume(codec, nid, dir) ||
3775 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3776 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3779 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3780 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3786 /* look for a boost amp in a widget close to the pin */
3787 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3788 struct nid_path *path)
3790 unsigned int val = 0;
3794 for (depth = 0; depth < 3; depth++) {
3795 if (depth >= path->depth - 1)
3797 nid = path->path[depth];
3798 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3799 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3801 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3802 path->idx[depth])) {
3803 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3812 static int parse_mic_boost(struct hda_codec *codec)
3814 struct hda_gen_spec *spec = codec->spec;
3815 struct auto_pin_cfg *cfg = &spec->autocfg;
3816 struct hda_input_mux *imux = &spec->input_mux;
3819 if (!spec->num_adc_nids)
3822 for (i = 0; i < imux->num_items; i++) {
3823 struct nid_path *path;
3826 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3828 idx = imux->items[i].index;
3829 if (idx >= imux->num_items)
3832 /* check only line-in and mic pins */
3833 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3836 path = get_input_path(codec, 0, i);
3840 val = look_for_boost_amp(codec, path);
3844 /* create a boost control */
3845 snprintf(boost_label, sizeof(boost_label),
3846 "%s Boost Volume", spec->input_labels[idx]);
3847 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3848 spec->input_label_idxs[idx], val))
3851 path->ctls[NID_PATH_BOOST_CTL] = val;
3857 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3859 static void parse_digital(struct hda_codec *codec)
3861 struct hda_gen_spec *spec = codec->spec;
3862 struct nid_path *path;
3864 hda_nid_t dig_nid, pin;
3866 /* support multiple SPDIFs; the secondary is set up as a slave */
3868 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3869 pin = spec->autocfg.dig_out_pins[i];
3870 dig_nid = look_for_dac(codec, pin, true);
3873 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3876 print_nid_path(codec, "digout", path);
3877 path->active = true;
3878 path->pin_fixed = true; /* no jack detection */
3879 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3880 set_pin_target(codec, pin, PIN_OUT, false);
3882 spec->multiout.dig_out_nid = dig_nid;
3883 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3885 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3886 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3888 spec->slave_dig_outs[nums - 1] = dig_nid;
3893 if (spec->autocfg.dig_in_pin) {
3894 pin = spec->autocfg.dig_in_pin;
3895 for_each_hda_codec_node(dig_nid, codec) {
3896 unsigned int wcaps = get_wcaps(codec, dig_nid);
3897 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3899 if (!(wcaps & AC_WCAP_DIGITAL))
3901 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3903 print_nid_path(codec, "digin", path);
3904 path->active = true;
3905 path->pin_fixed = true; /* no jack */
3906 spec->dig_in_nid = dig_nid;
3907 spec->digin_path = snd_hda_get_path_idx(codec, path);
3908 set_pin_target(codec, pin, PIN_IN, false);
3917 * input MUX handling
3920 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3922 /* select the given imux item; either unmute exclusively or select the route */
3923 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3926 struct hda_gen_spec *spec = codec->spec;
3927 const struct hda_input_mux *imux;
3928 struct nid_path *old_path, *path;
3930 imux = &spec->input_mux;
3931 if (!imux->num_items)
3934 if (idx >= imux->num_items)
3935 idx = imux->num_items - 1;
3936 if (spec->cur_mux[adc_idx] == idx)
3939 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3942 if (old_path->active)
3943 snd_hda_activate_path(codec, old_path, false, false);
3945 spec->cur_mux[adc_idx] = idx;
3948 update_hp_mic(codec, adc_idx, false);
3950 if (spec->dyn_adc_switch)
3951 dyn_adc_pcm_resetup(codec, idx);
3953 path = get_input_path(codec, adc_idx, idx);
3958 snd_hda_activate_path(codec, path, true, false);
3959 if (spec->cap_sync_hook)
3960 spec->cap_sync_hook(codec, NULL, NULL);
3961 path_power_down_sync(codec, old_path);
3965 /* power up/down widgets in the all paths that match with the given NID
3966 * as terminals (either start- or endpoint)
3968 * returns the last changed NID, or zero if unchanged.
3970 static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
3971 int pin_state, int stream_state)
3973 struct hda_gen_spec *spec = codec->spec;
3974 hda_nid_t last, changed = 0;
3975 struct nid_path *path;
3978 for (n = 0; n < spec->paths.used; n++) {
3979 path = snd_array_elem(&spec->paths, n);
3982 if (path->path[0] == nid ||
3983 path->path[path->depth - 1] == nid) {
3984 bool pin_old = path->pin_enabled;
3985 bool stream_old = path->stream_enabled;
3988 path->pin_enabled = pin_state;
3989 if (stream_state >= 0)
3990 path->stream_enabled = stream_state;
3991 if ((!path->pin_fixed && path->pin_enabled != pin_old)
3992 || path->stream_enabled != stream_old) {
3993 last = path_power_update(codec, path, true);
4002 /* check the jack status for power control */
4003 static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4005 if (!is_jack_detectable(codec, pin))
4007 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4010 /* power up/down the paths of the given pin according to the jack state;
4011 * power = 0/1 : only power up/down if it matches with the jack state,
4012 * < 0 : force power up/down to follow the jack sate
4014 * returns the last changed NID, or zero if unchanged.
4016 static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4021 if (!codec->power_save_node)
4024 on = detect_pin_state(codec, pin);
4026 if (power >= 0 && on != power)
4028 return set_path_power(codec, pin, on, -1);
4031 static void pin_power_callback(struct hda_codec *codec,
4032 struct hda_jack_callback *jack,
4035 if (jack && jack->nid)
4036 sync_power_state_change(codec,
4037 set_pin_power_jack(codec, jack->nid, on));
4040 /* callback only doing power up -- called at first */
4041 static void pin_power_up_callback(struct hda_codec *codec,
4042 struct hda_jack_callback *jack)
4044 pin_power_callback(codec, jack, true);
4047 /* callback only doing power down -- called at last */
4048 static void pin_power_down_callback(struct hda_codec *codec,
4049 struct hda_jack_callback *jack)
4051 pin_power_callback(codec, jack, false);
4054 /* set up the power up/down callbacks */
4055 static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4056 const hda_nid_t *pins, bool on)
4059 hda_jack_callback_fn cb =
4060 on ? pin_power_up_callback : pin_power_down_callback;
4062 for (i = 0; i < num_pins && pins[i]; i++) {
4063 if (is_jack_detectable(codec, pins[i]))
4064 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4066 set_path_power(codec, pins[i], true, -1);
4070 /* enabled power callback to each available I/O pin with jack detections;
4071 * the digital I/O pins are excluded because of the unreliable detectsion
4073 static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4075 struct hda_gen_spec *spec = codec->spec;
4076 struct auto_pin_cfg *cfg = &spec->autocfg;
4079 if (!codec->power_save_node)
4081 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4082 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4083 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4084 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4085 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4086 for (i = 0; i < cfg->num_inputs; i++)
4087 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4090 /* sync path power up/down with the jack states of given pins */
4091 static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4092 const hda_nid_t *pins)
4096 for (i = 0; i < num_pins && pins[i]; i++)
4097 if (is_jack_detectable(codec, pins[i]))
4098 set_pin_power_jack(codec, pins[i], -1);
4101 /* sync path power up/down with pins; called at init and resume */
4102 static void sync_all_pin_power_ctls(struct hda_codec *codec)
4104 struct hda_gen_spec *spec = codec->spec;
4105 struct auto_pin_cfg *cfg = &spec->autocfg;
4108 if (!codec->power_save_node)
4110 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4111 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4112 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4113 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4114 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4115 for (i = 0; i < cfg->num_inputs; i++)
4116 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4119 /* add fake paths if not present yet */
4120 static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4121 int num_pins, const hda_nid_t *pins)
4123 struct hda_gen_spec *spec = codec->spec;
4124 struct nid_path *path;
4127 for (i = 0; i < num_pins; i++) {
4130 if (get_nid_path(codec, nid, pins[i], 0))
4132 path = snd_array_new(&spec->paths);
4135 memset(path, 0, sizeof(*path));
4137 path->path[0] = nid;
4138 path->path[1] = pins[i];
4139 path->active = true;
4144 /* create fake paths to all outputs from beep */
4145 static int add_fake_beep_paths(struct hda_codec *codec)
4147 struct hda_gen_spec *spec = codec->spec;
4148 struct auto_pin_cfg *cfg = &spec->autocfg;
4149 hda_nid_t nid = spec->beep_nid;
4152 if (!codec->power_save_node || !nid)
4154 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4157 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4158 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4162 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4163 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4171 /* power up/down beep widget and its output paths */
4172 static void beep_power_hook(struct hda_beep *beep, bool on)
4174 set_path_power(beep->codec, beep->nid, -1, on);
4178 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4179 * @codec: the HDA codec
4180 * @pin: NID of pin to fix
4182 int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4184 struct hda_gen_spec *spec = codec->spec;
4185 struct nid_path *path;
4187 path = snd_array_new(&spec->paths);
4190 memset(path, 0, sizeof(*path));
4192 path->path[0] = pin;
4193 path->active = true;
4194 path->pin_fixed = true;
4195 path->stream_enabled = true;
4198 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4201 * Jack detections for HP auto-mute and mic-switch
4204 /* check each pin in the given array; returns true if any of them is plugged */
4205 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4208 bool present = false;
4210 for (i = 0; i < num_pins; i++) {
4211 hda_nid_t nid = pins[i];
4214 /* don't detect pins retasked as inputs */
4215 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4217 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4223 /* standard HP/line-out auto-mute helper */
4224 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
4225 int *paths, bool mute)
4227 struct hda_gen_spec *spec = codec->spec;
4230 for (i = 0; i < num_pins; i++) {
4231 hda_nid_t nid = pins[i];
4232 unsigned int val, oldval;
4236 oldval = snd_hda_codec_get_pin_target(codec, nid);
4237 if (oldval & PIN_IN)
4238 continue; /* no mute for inputs */
4240 if (spec->auto_mute_via_amp) {
4241 struct nid_path *path;
4244 path = snd_hda_get_path_from_idx(codec, paths[i]);
4247 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4251 spec->mute_bits |= (1ULL << mute_nid);
4253 spec->mute_bits &= ~(1ULL << mute_nid);
4256 /* don't reset VREF value in case it's controlling
4257 * the amp (see alc861_fixup_asus_amp_vref_0f())
4259 if (spec->keep_vref_in_automute)
4260 val = oldval & ~PIN_HP;
4265 /* here we call update_pin_ctl() so that the pinctl is
4266 * changed without changing the pinctl target value;
4267 * the original target value will be still referred at
4268 * the init / resume again
4270 update_pin_ctl(codec, nid, val);
4273 set_pin_eapd(codec, nid, !mute);
4274 if (codec->power_save_node) {
4277 on = detect_pin_state(codec, nid);
4278 set_path_power(codec, nid, on, -1);
4284 * snd_hda_gen_update_outputs - Toggle outputs muting
4285 * @codec: the HDA codec
4287 * Update the mute status of all outputs based on the current jack states.
4289 void snd_hda_gen_update_outputs(struct hda_codec *codec)
4291 struct hda_gen_spec *spec = codec->spec;
4295 /* Control HP pins/amps depending on master_mute state;
4296 * in general, HP pins/amps control should be enabled in all cases,
4297 * but currently set only for master_mute, just to be safe
4299 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4300 paths = spec->out_paths;
4302 paths = spec->hp_paths;
4303 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
4304 spec->autocfg.hp_pins, paths, spec->master_mute);
4306 if (!spec->automute_speaker)
4309 on = spec->hp_jack_present | spec->line_jack_present;
4310 on |= spec->master_mute;
4311 spec->speaker_muted = on;
4312 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4313 paths = spec->out_paths;
4315 paths = spec->speaker_paths;
4316 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
4317 spec->autocfg.speaker_pins, paths, on);
4319 /* toggle line-out mutes if needed, too */
4320 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4321 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4322 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4324 if (!spec->automute_lo)
4327 on = spec->hp_jack_present;
4328 on |= spec->master_mute;
4329 spec->line_out_muted = on;
4330 paths = spec->out_paths;
4331 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4332 spec->autocfg.line_out_pins, paths, on);
4334 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4336 static void call_update_outputs(struct hda_codec *codec)
4338 struct hda_gen_spec *spec = codec->spec;
4339 if (spec->automute_hook)
4340 spec->automute_hook(codec);
4342 snd_hda_gen_update_outputs(codec);
4344 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4345 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4346 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4350 * snd_hda_gen_hp_automute - standard HP-automute helper
4351 * @codec: the HDA codec
4352 * @jack: jack object, NULL for the whole
4354 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4355 struct hda_jack_callback *jack)
4357 struct hda_gen_spec *spec = codec->spec;
4358 hda_nid_t *pins = spec->autocfg.hp_pins;
4359 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4361 /* No detection for the first HP jack during indep-HP mode */
4362 if (spec->indep_hp_enabled) {
4367 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4368 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4370 call_update_outputs(codec);
4372 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4375 * snd_hda_gen_line_automute - standard line-out-automute helper
4376 * @codec: the HDA codec
4377 * @jack: jack object, NULL for the whole
4379 void snd_hda_gen_line_automute(struct hda_codec *codec,
4380 struct hda_jack_callback *jack)
4382 struct hda_gen_spec *spec = codec->spec;
4384 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4386 /* check LO jack only when it's different from HP */
4387 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4390 spec->line_jack_present =
4391 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4392 spec->autocfg.line_out_pins);
4393 if (!spec->automute_speaker || !spec->detect_lo)
4395 call_update_outputs(codec);
4397 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4400 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4401 * @codec: the HDA codec
4402 * @jack: jack object, NULL for the whole
4404 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4405 struct hda_jack_callback *jack)
4407 struct hda_gen_spec *spec = codec->spec;
4410 if (!spec->auto_mic)
4413 for (i = spec->am_num_entries - 1; i > 0; i--) {
4414 hda_nid_t pin = spec->am_entry[i].pin;
4415 /* don't detect pins retasked as outputs */
4416 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4418 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4419 mux_select(codec, 0, spec->am_entry[i].idx);
4423 mux_select(codec, 0, spec->am_entry[0].idx);
4425 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4427 /* call appropriate hooks */
4428 static void call_hp_automute(struct hda_codec *codec,
4429 struct hda_jack_callback *jack)
4431 struct hda_gen_spec *spec = codec->spec;
4432 if (spec->hp_automute_hook)
4433 spec->hp_automute_hook(codec, jack);
4435 snd_hda_gen_hp_automute(codec, jack);
4438 static void call_line_automute(struct hda_codec *codec,
4439 struct hda_jack_callback *jack)
4441 struct hda_gen_spec *spec = codec->spec;
4442 if (spec->line_automute_hook)
4443 spec->line_automute_hook(codec, jack);
4445 snd_hda_gen_line_automute(codec, jack);
4448 static void call_mic_autoswitch(struct hda_codec *codec,
4449 struct hda_jack_callback *jack)
4451 struct hda_gen_spec *spec = codec->spec;
4452 if (spec->mic_autoswitch_hook)
4453 spec->mic_autoswitch_hook(codec, jack);
4455 snd_hda_gen_mic_autoswitch(codec, jack);
4458 /* update jack retasking */
4459 static void update_automute_all(struct hda_codec *codec)
4461 call_hp_automute(codec, NULL);
4462 call_line_automute(codec, NULL);
4463 call_mic_autoswitch(codec, NULL);
4467 * Auto-Mute mode mixer enum support
4469 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4470 struct snd_ctl_elem_info *uinfo)
4472 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4473 struct hda_gen_spec *spec = codec->spec;
4474 static const char * const texts3[] = {
4475 "Disabled", "Speaker Only", "Line Out+Speaker"
4478 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4479 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4480 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4483 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4484 struct snd_ctl_elem_value *ucontrol)
4486 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4487 struct hda_gen_spec *spec = codec->spec;
4488 unsigned int val = 0;
4489 if (spec->automute_speaker)
4491 if (spec->automute_lo)
4494 ucontrol->value.enumerated.item[0] = val;
4498 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4499 struct snd_ctl_elem_value *ucontrol)
4501 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4502 struct hda_gen_spec *spec = codec->spec;
4504 switch (ucontrol->value.enumerated.item[0]) {
4506 if (!spec->automute_speaker && !spec->automute_lo)
4508 spec->automute_speaker = 0;
4509 spec->automute_lo = 0;
4512 if (spec->automute_speaker_possible) {
4513 if (!spec->automute_lo && spec->automute_speaker)
4515 spec->automute_speaker = 1;
4516 spec->automute_lo = 0;
4517 } else if (spec->automute_lo_possible) {
4518 if (spec->automute_lo)
4520 spec->automute_lo = 1;
4525 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4527 if (spec->automute_speaker && spec->automute_lo)
4529 spec->automute_speaker = 1;
4530 spec->automute_lo = 1;
4535 call_update_outputs(codec);
4539 static const struct snd_kcontrol_new automute_mode_enum = {
4540 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4541 .name = "Auto-Mute Mode",
4542 .info = automute_mode_info,
4543 .get = automute_mode_get,
4544 .put = automute_mode_put,
4547 static int add_automute_mode_enum(struct hda_codec *codec)
4549 struct hda_gen_spec *spec = codec->spec;
4551 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4557 * Check the availability of HP/line-out auto-mute;
4558 * Set up appropriately if really supported
4560 static int check_auto_mute_availability(struct hda_codec *codec)
4562 struct hda_gen_spec *spec = codec->spec;
4563 struct auto_pin_cfg *cfg = &spec->autocfg;
4567 if (spec->suppress_auto_mute)
4570 if (cfg->hp_pins[0])
4572 if (cfg->line_out_pins[0])
4574 if (cfg->speaker_pins[0])
4576 if (present < 2) /* need two different output types */
4579 if (!cfg->speaker_pins[0] &&
4580 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4581 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4582 sizeof(cfg->speaker_pins));
4583 cfg->speaker_outs = cfg->line_outs;
4586 if (!cfg->hp_pins[0] &&
4587 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4588 memcpy(cfg->hp_pins, cfg->line_out_pins,
4589 sizeof(cfg->hp_pins));
4590 cfg->hp_outs = cfg->line_outs;
4593 for (i = 0; i < cfg->hp_outs; i++) {
4594 hda_nid_t nid = cfg->hp_pins[i];
4595 if (!is_jack_detectable(codec, nid))
4597 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4598 snd_hda_jack_detect_enable_callback(codec, nid,
4600 spec->detect_hp = 1;
4603 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4604 if (cfg->speaker_outs)
4605 for (i = 0; i < cfg->line_outs; i++) {
4606 hda_nid_t nid = cfg->line_out_pins[i];
4607 if (!is_jack_detectable(codec, nid))
4609 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4610 snd_hda_jack_detect_enable_callback(codec, nid,
4611 call_line_automute);
4612 spec->detect_lo = 1;
4614 spec->automute_lo_possible = spec->detect_hp;
4617 spec->automute_speaker_possible = cfg->speaker_outs &&
4618 (spec->detect_hp || spec->detect_lo);
4620 spec->automute_lo = spec->automute_lo_possible;
4621 spec->automute_speaker = spec->automute_speaker_possible;
4623 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4624 /* create a control for automute mode */
4625 err = add_automute_mode_enum(codec);
4632 /* check whether all auto-mic pins are valid; setup indices if OK */
4633 static bool auto_mic_check_imux(struct hda_codec *codec)
4635 struct hda_gen_spec *spec = codec->spec;
4636 const struct hda_input_mux *imux;
4639 imux = &spec->input_mux;
4640 for (i = 0; i < spec->am_num_entries; i++) {
4641 spec->am_entry[i].idx =
4642 find_idx_in_nid_list(spec->am_entry[i].pin,
4643 spec->imux_pins, imux->num_items);
4644 if (spec->am_entry[i].idx < 0)
4645 return false; /* no corresponding imux */
4648 /* we don't need the jack detection for the first pin */
4649 for (i = 1; i < spec->am_num_entries; i++)
4650 snd_hda_jack_detect_enable_callback(codec,
4651 spec->am_entry[i].pin,
4652 call_mic_autoswitch);
4656 static int compare_attr(const void *ap, const void *bp)
4658 const struct automic_entry *a = ap;
4659 const struct automic_entry *b = bp;
4660 return (int)(a->attr - b->attr);
4664 * Check the availability of auto-mic switch;
4665 * Set up if really supported
4667 static int check_auto_mic_availability(struct hda_codec *codec)
4669 struct hda_gen_spec *spec = codec->spec;
4670 struct auto_pin_cfg *cfg = &spec->autocfg;
4674 if (spec->suppress_auto_mic)
4679 for (i = 0; i < cfg->num_inputs; i++) {
4680 hda_nid_t nid = cfg->inputs[i].pin;
4682 attr = snd_hda_codec_get_pincfg(codec, nid);
4683 attr = snd_hda_get_input_pin_attr(attr);
4684 if (types & (1 << attr))
4685 return 0; /* already occupied */
4687 case INPUT_PIN_ATTR_INT:
4688 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4689 return 0; /* invalid type */
4691 case INPUT_PIN_ATTR_UNUSED:
4692 return 0; /* invalid entry */
4694 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4695 return 0; /* invalid type */
4696 if (!spec->line_in_auto_switch &&
4697 cfg->inputs[i].type != AUTO_PIN_MIC)
4698 return 0; /* only mic is allowed */
4699 if (!is_jack_detectable(codec, nid))
4700 return 0; /* no unsol support */
4703 if (num_pins >= MAX_AUTO_MIC_PINS)
4705 types |= (1 << attr);
4706 spec->am_entry[num_pins].pin = nid;
4707 spec->am_entry[num_pins].attr = attr;
4714 spec->am_num_entries = num_pins;
4715 /* sort the am_entry in the order of attr so that the pin with a
4716 * higher attr will be selected when the jack is plugged.
4718 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4719 compare_attr, NULL);
4721 if (!auto_mic_check_imux(codec))
4725 spec->num_adc_nids = 1;
4726 spec->cur_mux[0] = spec->am_entry[0].idx;
4727 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4728 spec->am_entry[0].pin,
4729 spec->am_entry[1].pin,
4730 spec->am_entry[2].pin);
4736 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4738 * @codec: the HDA codec
4739 * @nid: NID to evalute
4740 * @power_state: target power state
4742 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4744 unsigned int power_state)
4746 struct hda_gen_spec *spec = codec->spec;
4748 if (!spec->power_down_unused && !codec->power_save_node)
4750 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
4752 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4754 if (is_active_nid_for_any(codec, nid))
4758 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4760 /* mute all aamix inputs initially; parse up to the first leaves */
4761 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4764 const hda_nid_t *conn;
4767 nums = snd_hda_get_conn_list(codec, mix, &conn);
4768 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4769 for (i = 0; i < nums; i++) {
4771 update_amp(codec, mix, HDA_INPUT, i,
4772 0xff, HDA_AMP_MUTE);
4773 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4774 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4775 0xff, HDA_AMP_MUTE);
4780 * snd_hda_gen_stream_pm - Stream power management callback
4781 * @codec: the HDA codec
4782 * @nid: audio widget
4783 * @on: power on/off flag
4785 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
4787 void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4789 if (codec->power_save_node)
4790 set_path_power(codec, nid, -1, on);
4792 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4795 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4796 * set up the hda_gen_spec
4797 * @codec: the HDA codec
4798 * @cfg: Parsed pin configuration
4800 * return 1 if successful, 0 if the proper config is not found,
4801 * or a negative error code
4803 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4804 struct auto_pin_cfg *cfg)
4806 struct hda_gen_spec *spec = codec->spec;
4809 parse_user_hints(codec);
4811 if (spec->mixer_nid && !spec->mixer_merge_nid)
4812 spec->mixer_merge_nid = spec->mixer_nid;
4814 if (cfg != &spec->autocfg) {
4815 spec->autocfg = *cfg;
4816 cfg = &spec->autocfg;
4819 if (!spec->main_out_badness)
4820 spec->main_out_badness = &hda_main_out_badness;
4821 if (!spec->extra_out_badness)
4822 spec->extra_out_badness = &hda_extra_out_badness;
4824 fill_all_dac_nids(codec);
4826 if (!cfg->line_outs) {
4827 if (cfg->dig_outs || cfg->dig_in_pin) {
4828 spec->multiout.max_channels = 2;
4829 spec->no_analog = 1;
4832 if (!cfg->num_inputs && !cfg->dig_in_pin)
4833 return 0; /* can't find valid BIOS pin config */
4836 if (!spec->no_primary_hp &&
4837 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4838 cfg->line_outs <= cfg->hp_outs) {
4839 /* use HP as primary out */
4840 cfg->speaker_outs = cfg->line_outs;
4841 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4842 sizeof(cfg->speaker_pins));
4843 cfg->line_outs = cfg->hp_outs;
4844 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4846 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4847 cfg->line_out_type = AUTO_PIN_HP_OUT;
4850 err = parse_output_paths(codec);
4853 err = create_multi_channel_mode(codec);
4856 err = create_multi_out_ctls(codec, cfg);
4859 err = create_hp_out_ctls(codec);
4862 err = create_speaker_out_ctls(codec);
4865 err = create_indep_hp_ctls(codec);
4868 err = create_loopback_mixing_ctl(codec);
4871 err = create_hp_mic(codec);
4874 err = create_input_ctls(codec);
4878 /* add power-down pin callbacks at first */
4879 add_all_pin_power_ctls(codec, false);
4881 spec->const_channel_count = spec->ext_channel_count;
4882 /* check the multiple speaker and headphone pins */
4883 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4884 spec->const_channel_count = max(spec->const_channel_count,
4885 cfg->speaker_outs * 2);
4886 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4887 spec->const_channel_count = max(spec->const_channel_count,
4889 spec->multiout.max_channels = max(spec->ext_channel_count,
4890 spec->const_channel_count);
4892 err = check_auto_mute_availability(codec);
4896 err = check_dyn_adc_switch(codec);
4900 err = check_auto_mic_availability(codec);
4904 /* add stereo mix if available and not enabled yet */
4905 if (!spec->auto_mic && spec->mixer_nid &&
4906 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4907 spec->input_mux.num_items > 1) {
4908 err = parse_capture_source(codec, spec->mixer_nid,
4909 CFG_IDX_MIX, spec->num_all_adcs,
4916 err = create_capture_mixers(codec);
4920 err = parse_mic_boost(codec);
4924 /* create "Headphone Mic Jack Mode" if no input selection is
4925 * available (or user specifies add_jack_modes hint)
4927 if (spec->hp_mic_pin &&
4928 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4929 spec->add_jack_modes)) {
4930 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4935 if (spec->add_jack_modes) {
4936 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4937 err = create_out_jack_modes(codec, cfg->line_outs,
4938 cfg->line_out_pins);
4942 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4943 err = create_out_jack_modes(codec, cfg->hp_outs,
4950 /* add power-up pin callbacks at last */
4951 add_all_pin_power_ctls(codec, true);
4953 /* mute all aamix input initially */
4954 if (spec->mixer_nid)
4955 mute_all_mixer_nid(codec, spec->mixer_nid);
4958 parse_digital(codec);
4960 if (spec->power_down_unused || codec->power_save_node) {
4961 if (!codec->power_filter)
4962 codec->power_filter = snd_hda_gen_path_power_filter;
4963 if (!codec->patch_ops.stream_pm)
4964 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
4967 if (!spec->no_analog && spec->beep_nid) {
4968 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4971 if (codec->beep && codec->power_save_node) {
4972 err = add_fake_beep_paths(codec);
4975 codec->beep->power_hook = beep_power_hook;
4981 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
4985 * Build control elements
4988 /* slave controls for virtual master */
4989 static const char * const slave_pfxs[] = {
4990 "Front", "Surround", "Center", "LFE", "Side",
4991 "Headphone", "Speaker", "Mono", "Line Out",
4992 "CLFE", "Bass Speaker", "PCM",
4993 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4994 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4995 "Headphone Side", "Headphone+LO", "Speaker+LO",
5000 * snd_hda_gen_build_controls - Build controls from the parsed results
5001 * @codec: the HDA codec
5003 * Pass this to build_controls patch_ops.
5005 int snd_hda_gen_build_controls(struct hda_codec *codec)
5007 struct hda_gen_spec *spec = codec->spec;
5010 if (spec->kctls.used) {
5011 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5016 if (spec->multiout.dig_out_nid) {
5017 err = snd_hda_create_dig_out_ctls(codec,
5018 spec->multiout.dig_out_nid,
5019 spec->multiout.dig_out_nid,
5020 spec->pcm_rec[1]->pcm_type);
5023 if (!spec->no_analog) {
5024 err = snd_hda_create_spdif_share_sw(codec,
5028 spec->multiout.share_spdif = 1;
5031 if (spec->dig_in_nid) {
5032 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5037 /* if we have no master control, let's create it */
5038 if (!spec->no_analog &&
5039 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
5040 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
5041 spec->vmaster_tlv, slave_pfxs,
5046 if (!spec->no_analog &&
5047 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5048 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5051 true, &spec->vmaster_mute.sw_kctl);
5054 if (spec->vmaster_mute.hook) {
5055 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5056 spec->vmaster_mute_enum);
5057 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5061 free_kctls(spec); /* no longer needed */
5063 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5069 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
5076 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5077 struct hda_codec *codec,
5078 struct snd_pcm_substream *substream,
5081 struct hda_gen_spec *spec = codec->spec;
5082 if (spec->pcm_playback_hook)
5083 spec->pcm_playback_hook(hinfo, codec, substream, action);
5086 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5087 struct hda_codec *codec,
5088 struct snd_pcm_substream *substream,
5091 struct hda_gen_spec *spec = codec->spec;
5092 if (spec->pcm_capture_hook)
5093 spec->pcm_capture_hook(hinfo, codec, substream, action);
5097 * Analog playback callbacks
5099 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5100 struct hda_codec *codec,
5101 struct snd_pcm_substream *substream)
5103 struct hda_gen_spec *spec = codec->spec;
5106 mutex_lock(&spec->pcm_mutex);
5107 err = snd_hda_multi_out_analog_open(codec,
5108 &spec->multiout, substream,
5111 spec->active_streams |= 1 << STREAM_MULTI_OUT;
5112 call_pcm_playback_hook(hinfo, codec, substream,
5113 HDA_GEN_PCM_ACT_OPEN);
5115 mutex_unlock(&spec->pcm_mutex);
5119 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5120 struct hda_codec *codec,
5121 unsigned int stream_tag,
5122 unsigned int format,
5123 struct snd_pcm_substream *substream)
5125 struct hda_gen_spec *spec = codec->spec;
5128 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5129 stream_tag, format, substream);
5131 call_pcm_playback_hook(hinfo, codec, substream,
5132 HDA_GEN_PCM_ACT_PREPARE);
5136 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5137 struct hda_codec *codec,
5138 struct snd_pcm_substream *substream)
5140 struct hda_gen_spec *spec = codec->spec;
5143 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5145 call_pcm_playback_hook(hinfo, codec, substream,
5146 HDA_GEN_PCM_ACT_CLEANUP);
5150 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5151 struct hda_codec *codec,
5152 struct snd_pcm_substream *substream)
5154 struct hda_gen_spec *spec = codec->spec;
5155 mutex_lock(&spec->pcm_mutex);
5156 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
5157 call_pcm_playback_hook(hinfo, codec, substream,
5158 HDA_GEN_PCM_ACT_CLOSE);
5159 mutex_unlock(&spec->pcm_mutex);
5163 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5164 struct hda_codec *codec,
5165 struct snd_pcm_substream *substream)
5167 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5171 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5172 struct hda_codec *codec,
5173 unsigned int stream_tag,
5174 unsigned int format,
5175 struct snd_pcm_substream *substream)
5177 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5178 call_pcm_capture_hook(hinfo, codec, substream,
5179 HDA_GEN_PCM_ACT_PREPARE);
5183 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5184 struct hda_codec *codec,
5185 struct snd_pcm_substream *substream)
5187 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5188 call_pcm_capture_hook(hinfo, codec, substream,
5189 HDA_GEN_PCM_ACT_CLEANUP);
5193 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5194 struct hda_codec *codec,
5195 struct snd_pcm_substream *substream)
5197 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5201 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5202 struct hda_codec *codec,
5203 struct snd_pcm_substream *substream)
5205 struct hda_gen_spec *spec = codec->spec;
5208 mutex_lock(&spec->pcm_mutex);
5209 if (spec->indep_hp && !spec->indep_hp_enabled)
5212 spec->active_streams |= 1 << STREAM_INDEP_HP;
5213 call_pcm_playback_hook(hinfo, codec, substream,
5214 HDA_GEN_PCM_ACT_OPEN);
5215 mutex_unlock(&spec->pcm_mutex);
5219 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5220 struct hda_codec *codec,
5221 struct snd_pcm_substream *substream)
5223 struct hda_gen_spec *spec = codec->spec;
5224 mutex_lock(&spec->pcm_mutex);
5225 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
5226 call_pcm_playback_hook(hinfo, codec, substream,
5227 HDA_GEN_PCM_ACT_CLOSE);
5228 mutex_unlock(&spec->pcm_mutex);
5232 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5233 struct hda_codec *codec,
5234 unsigned int stream_tag,
5235 unsigned int format,
5236 struct snd_pcm_substream *substream)
5238 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5239 call_pcm_playback_hook(hinfo, codec, substream,
5240 HDA_GEN_PCM_ACT_PREPARE);
5244 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5245 struct hda_codec *codec,
5246 struct snd_pcm_substream *substream)
5248 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5249 call_pcm_playback_hook(hinfo, codec, substream,
5250 HDA_GEN_PCM_ACT_CLEANUP);
5257 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5258 struct hda_codec *codec,
5259 struct snd_pcm_substream *substream)
5261 struct hda_gen_spec *spec = codec->spec;
5262 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5265 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5266 struct hda_codec *codec,
5267 unsigned int stream_tag,
5268 unsigned int format,
5269 struct snd_pcm_substream *substream)
5271 struct hda_gen_spec *spec = codec->spec;
5272 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5273 stream_tag, format, substream);
5276 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5277 struct hda_codec *codec,
5278 struct snd_pcm_substream *substream)
5280 struct hda_gen_spec *spec = codec->spec;
5281 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5284 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5285 struct hda_codec *codec,
5286 struct snd_pcm_substream *substream)
5288 struct hda_gen_spec *spec = codec->spec;
5289 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5295 #define alt_capture_pcm_open capture_pcm_open
5296 #define alt_capture_pcm_close capture_pcm_close
5298 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5299 struct hda_codec *codec,
5300 unsigned int stream_tag,
5301 unsigned int format,
5302 struct snd_pcm_substream *substream)
5304 struct hda_gen_spec *spec = codec->spec;
5306 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5307 stream_tag, 0, format);
5308 call_pcm_capture_hook(hinfo, codec, substream,
5309 HDA_GEN_PCM_ACT_PREPARE);
5313 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5314 struct hda_codec *codec,
5315 struct snd_pcm_substream *substream)
5317 struct hda_gen_spec *spec = codec->spec;
5319 snd_hda_codec_cleanup_stream(codec,
5320 spec->adc_nids[substream->number + 1]);
5321 call_pcm_capture_hook(hinfo, codec, substream,
5322 HDA_GEN_PCM_ACT_CLEANUP);
5328 static const struct hda_pcm_stream pcm_analog_playback = {
5332 /* NID is set in build_pcms */
5334 .open = playback_pcm_open,
5335 .close = playback_pcm_close,
5336 .prepare = playback_pcm_prepare,
5337 .cleanup = playback_pcm_cleanup
5341 static const struct hda_pcm_stream pcm_analog_capture = {
5345 /* NID is set in build_pcms */
5347 .open = capture_pcm_open,
5348 .close = capture_pcm_close,
5349 .prepare = capture_pcm_prepare,
5350 .cleanup = capture_pcm_cleanup
5354 static const struct hda_pcm_stream pcm_analog_alt_playback = {
5358 /* NID is set in build_pcms */
5360 .open = alt_playback_pcm_open,
5361 .close = alt_playback_pcm_close,
5362 .prepare = alt_playback_pcm_prepare,
5363 .cleanup = alt_playback_pcm_cleanup
5367 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5368 .substreams = 2, /* can be overridden */
5371 /* NID is set in build_pcms */
5373 .open = alt_capture_pcm_open,
5374 .close = alt_capture_pcm_close,
5375 .prepare = alt_capture_pcm_prepare,
5376 .cleanup = alt_capture_pcm_cleanup
5380 static const struct hda_pcm_stream pcm_digital_playback = {
5384 /* NID is set in build_pcms */
5386 .open = dig_playback_pcm_open,
5387 .close = dig_playback_pcm_close,
5388 .prepare = dig_playback_pcm_prepare,
5389 .cleanup = dig_playback_pcm_cleanup
5393 static const struct hda_pcm_stream pcm_digital_capture = {
5397 /* NID is set in build_pcms */
5400 /* Used by build_pcms to flag that a PCM has no playback stream */
5401 static const struct hda_pcm_stream pcm_null_stream = {
5408 * dynamic changing ADC PCM streams
5410 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5412 struct hda_gen_spec *spec = codec->spec;
5413 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5415 if (spec->cur_adc && spec->cur_adc != new_adc) {
5416 /* stream is running, let's swap the current ADC */
5417 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5418 spec->cur_adc = new_adc;
5419 snd_hda_codec_setup_stream(codec, new_adc,
5420 spec->cur_adc_stream_tag, 0,
5421 spec->cur_adc_format);
5427 /* analog capture with dynamic dual-adc changes */
5428 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5429 struct hda_codec *codec,
5430 unsigned int stream_tag,
5431 unsigned int format,
5432 struct snd_pcm_substream *substream)
5434 struct hda_gen_spec *spec = codec->spec;
5435 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5436 spec->cur_adc_stream_tag = stream_tag;
5437 spec->cur_adc_format = format;
5438 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5439 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
5443 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5444 struct hda_codec *codec,
5445 struct snd_pcm_substream *substream)
5447 struct hda_gen_spec *spec = codec->spec;
5448 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5450 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
5454 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5458 .nid = 0, /* fill later */
5460 .prepare = dyn_adc_capture_pcm_prepare,
5461 .cleanup = dyn_adc_capture_pcm_cleanup
5465 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5466 const char *chip_name)
5472 strlcpy(str, chip_name, len);
5474 /* drop non-alnum chars after a space */
5475 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5476 if (!isalnum(p[1])) {
5481 strlcat(str, sfx, len);
5484 /* copy PCM stream info from @default_str, and override non-NULL entries
5485 * from @spec_str and @nid
5487 static void setup_pcm_stream(struct hda_pcm_stream *str,
5488 const struct hda_pcm_stream *default_str,
5489 const struct hda_pcm_stream *spec_str,
5492 *str = *default_str;
5496 if (spec_str->substreams)
5497 str->substreams = spec_str->substreams;
5498 if (spec_str->channels_min)
5499 str->channels_min = spec_str->channels_min;
5500 if (spec_str->channels_max)
5501 str->channels_max = spec_str->channels_max;
5502 if (spec_str->rates)
5503 str->rates = spec_str->rates;
5504 if (spec_str->formats)
5505 str->formats = spec_str->formats;
5506 if (spec_str->maxbps)
5507 str->maxbps = spec_str->maxbps;
5512 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5513 * @codec: the HDA codec
5515 * Pass this to build_pcms patch_ops.
5517 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5519 struct hda_gen_spec *spec = codec->spec;
5520 struct hda_pcm *info;
5521 bool have_multi_adcs;
5523 if (spec->no_analog)
5526 fill_pcm_stream_name(spec->stream_name_analog,
5527 sizeof(spec->stream_name_analog),
5528 " Analog", codec->core.chip_name);
5529 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5532 spec->pcm_rec[0] = info;
5534 if (spec->multiout.num_dacs > 0) {
5535 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5536 &pcm_analog_playback,
5537 spec->stream_analog_playback,
5538 spec->multiout.dac_nids[0]);
5539 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5540 spec->multiout.max_channels;
5541 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5542 spec->autocfg.line_outs == 2)
5543 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5546 if (spec->num_adc_nids) {
5547 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5548 (spec->dyn_adc_switch ?
5549 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5550 spec->stream_analog_capture,
5555 /* SPDIF for stream index #1 */
5556 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5557 fill_pcm_stream_name(spec->stream_name_digital,
5558 sizeof(spec->stream_name_digital),
5559 " Digital", codec->core.chip_name);
5560 info = snd_hda_codec_pcm_new(codec, "%s",
5561 spec->stream_name_digital);
5564 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5565 spec->pcm_rec[1] = info;
5566 if (spec->dig_out_type)
5567 info->pcm_type = spec->dig_out_type;
5569 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5570 if (spec->multiout.dig_out_nid)
5571 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5572 &pcm_digital_playback,
5573 spec->stream_digital_playback,
5574 spec->multiout.dig_out_nid);
5575 if (spec->dig_in_nid)
5576 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5577 &pcm_digital_capture,
5578 spec->stream_digital_capture,
5582 if (spec->no_analog)
5585 /* If the use of more than one ADC is requested for the current
5586 * model, configure a second analog capture-only PCM.
5588 have_multi_adcs = (spec->num_adc_nids > 1) &&
5589 !spec->dyn_adc_switch && !spec->auto_mic;
5590 /* Additional Analaog capture for index #2 */
5591 if (spec->alt_dac_nid || have_multi_adcs) {
5592 fill_pcm_stream_name(spec->stream_name_alt_analog,
5593 sizeof(spec->stream_name_alt_analog),
5594 " Alt Analog", codec->core.chip_name);
5595 info = snd_hda_codec_pcm_new(codec, "%s",
5596 spec->stream_name_alt_analog);
5599 spec->pcm_rec[2] = info;
5600 if (spec->alt_dac_nid)
5601 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5602 &pcm_analog_alt_playback,
5603 spec->stream_analog_alt_playback,
5606 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5607 &pcm_null_stream, NULL, 0);
5608 if (have_multi_adcs) {
5609 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5610 &pcm_analog_alt_capture,
5611 spec->stream_analog_alt_capture,
5613 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5614 spec->num_adc_nids - 1;
5616 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5617 &pcm_null_stream, NULL, 0);
5623 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5627 * Standard auto-parser initializations
5630 /* configure the given path as a proper output */
5631 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5633 struct nid_path *path;
5636 path = snd_hda_get_path_from_idx(codec, path_idx);
5637 if (!path || !path->depth)
5639 pin = path->path[path->depth - 1];
5640 restore_pin_ctl(codec, pin);
5641 snd_hda_activate_path(codec, path, path->active,
5642 aamix_default(codec->spec));
5643 set_pin_eapd(codec, pin, path->active);
5646 /* initialize primary output paths */
5647 static void init_multi_out(struct hda_codec *codec)
5649 struct hda_gen_spec *spec = codec->spec;
5652 for (i = 0; i < spec->autocfg.line_outs; i++)
5653 set_output_and_unmute(codec, spec->out_paths[i]);
5657 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5661 for (i = 0; i < num_outs; i++)
5662 set_output_and_unmute(codec, paths[i]);
5665 /* initialize hp and speaker paths */
5666 static void init_extra_out(struct hda_codec *codec)
5668 struct hda_gen_spec *spec = codec->spec;
5670 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5671 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5672 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5673 __init_extra_out(codec, spec->autocfg.speaker_outs,
5674 spec->speaker_paths);
5677 /* initialize multi-io paths */
5678 static void init_multi_io(struct hda_codec *codec)
5680 struct hda_gen_spec *spec = codec->spec;
5683 for (i = 0; i < spec->multi_ios; i++) {
5684 hda_nid_t pin = spec->multi_io[i].pin;
5685 struct nid_path *path;
5686 path = get_multiio_path(codec, i);
5689 if (!spec->multi_io[i].ctl_in)
5690 spec->multi_io[i].ctl_in =
5691 snd_hda_codec_get_pin_target(codec, pin);
5692 snd_hda_activate_path(codec, path, path->active,
5693 aamix_default(spec));
5697 static void init_aamix_paths(struct hda_codec *codec)
5699 struct hda_gen_spec *spec = codec->spec;
5701 if (!spec->have_aamix_ctl)
5703 if (!has_aamix_out_paths(spec))
5705 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5706 spec->aamix_out_paths[0],
5707 spec->autocfg.line_out_type);
5708 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5709 spec->aamix_out_paths[1],
5711 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5712 spec->aamix_out_paths[2],
5713 AUTO_PIN_SPEAKER_OUT);
5716 /* set up input pins and loopback paths */
5717 static void init_analog_input(struct hda_codec *codec)
5719 struct hda_gen_spec *spec = codec->spec;
5720 struct auto_pin_cfg *cfg = &spec->autocfg;
5723 for (i = 0; i < cfg->num_inputs; i++) {
5724 hda_nid_t nid = cfg->inputs[i].pin;
5725 if (is_input_pin(codec, nid))
5726 restore_pin_ctl(codec, nid);
5728 /* init loopback inputs */
5729 if (spec->mixer_nid) {
5730 resume_path_from_idx(codec, spec->loopback_paths[i]);
5731 resume_path_from_idx(codec, spec->loopback_merge_path);
5736 /* initialize ADC paths */
5737 static void init_input_src(struct hda_codec *codec)
5739 struct hda_gen_spec *spec = codec->spec;
5740 struct hda_input_mux *imux = &spec->input_mux;
5741 struct nid_path *path;
5744 if (spec->dyn_adc_switch)
5747 nums = spec->num_adc_nids;
5749 for (c = 0; c < nums; c++) {
5750 for (i = 0; i < imux->num_items; i++) {
5751 path = get_input_path(codec, c, i);
5753 bool active = path->active;
5754 if (i == spec->cur_mux[c])
5756 snd_hda_activate_path(codec, path, active, false);
5760 update_hp_mic(codec, c, true);
5763 if (spec->cap_sync_hook)
5764 spec->cap_sync_hook(codec, NULL, NULL);
5767 /* set right pin controls for digital I/O */
5768 static void init_digital(struct hda_codec *codec)
5770 struct hda_gen_spec *spec = codec->spec;
5774 for (i = 0; i < spec->autocfg.dig_outs; i++)
5775 set_output_and_unmute(codec, spec->digout_paths[i]);
5776 pin = spec->autocfg.dig_in_pin;
5778 restore_pin_ctl(codec, pin);
5779 resume_path_from_idx(codec, spec->digin_path);
5783 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5784 * invalid unsol tags by some reason
5786 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5790 for (i = 0; i < codec->init_pins.used; i++) {
5791 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5792 hda_nid_t nid = pin->nid;
5793 if (is_jack_detectable(codec, nid) &&
5794 !snd_hda_jack_tbl_get(codec, nid))
5795 snd_hda_codec_update_cache(codec, nid, 0,
5796 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5801 * snd_hda_gen_init - initialize the generic spec
5802 * @codec: the HDA codec
5804 * This can be put as patch_ops init function.
5806 int snd_hda_gen_init(struct hda_codec *codec)
5808 struct hda_gen_spec *spec = codec->spec;
5810 if (spec->init_hook)
5811 spec->init_hook(codec);
5813 snd_hda_apply_verbs(codec);
5815 init_multi_out(codec);
5816 init_extra_out(codec);
5817 init_multi_io(codec);
5818 init_aamix_paths(codec);
5819 init_analog_input(codec);
5820 init_input_src(codec);
5821 init_digital(codec);
5823 clear_unsol_on_unused_pins(codec);
5825 sync_all_pin_power_ctls(codec);
5827 /* call init functions of standard auto-mute helpers */
5828 update_automute_all(codec);
5830 regcache_sync(codec->core.regmap);
5832 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5833 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5835 hda_call_check_power_status(codec, 0x01);
5838 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
5841 * snd_hda_gen_free - free the generic spec
5842 * @codec: the HDA codec
5844 * This can be put as patch_ops free function.
5846 void snd_hda_gen_free(struct hda_codec *codec)
5848 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
5849 snd_hda_gen_spec_free(codec->spec);
5853 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
5857 * snd_hda_gen_check_power_status - check the loopback power save state
5858 * @codec: the HDA codec
5859 * @nid: NID to inspect
5861 * This can be put as patch_ops check_power_status function.
5863 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5865 struct hda_gen_spec *spec = codec->spec;
5866 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5868 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
5873 * the generic codec support
5876 static const struct hda_codec_ops generic_patch_ops = {
5877 .build_controls = snd_hda_gen_build_controls,
5878 .build_pcms = snd_hda_gen_build_pcms,
5879 .init = snd_hda_gen_init,
5880 .free = snd_hda_gen_free,
5881 .unsol_event = snd_hda_jack_unsol_event,
5883 .check_power_status = snd_hda_gen_check_power_status,
5888 * snd_hda_parse_generic_codec - Generic codec parser
5889 * @codec: the HDA codec
5891 static int snd_hda_parse_generic_codec(struct hda_codec *codec)
5893 struct hda_gen_spec *spec;
5896 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5899 snd_hda_gen_spec_init(spec);
5902 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5906 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5910 codec->patch_ops = generic_patch_ops;
5914 snd_hda_gen_free(codec);
5918 static const struct hda_device_id snd_hda_id_generic[] = {
5919 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
5922 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
5924 static struct hda_codec_driver generic_driver = {
5925 .id = snd_hda_id_generic,
5928 module_hda_codec_driver(generic_driver);
5930 MODULE_LICENSE("GPL");
5931 MODULE_DESCRIPTION("Generic HD-audio codec parser");