2 * Universal Interface for Intel High Definition Audio Codec
4 * Generic widget tree parser
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/sort.h>
27 #include <linux/delay.h>
28 #include <linux/ctype.h>
29 #include <linux/string.h>
30 #include <linux/bitops.h>
31 #include <linux/module.h>
32 #include <sound/core.h>
33 #include <sound/jack.h>
34 #include <sound/tlv.h>
35 #include "hda_codec.h"
36 #include "hda_local.h"
37 #include "hda_auto_parser.h"
40 #include "hda_generic.h"
44 * snd_hda_gen_spec_init - initialize hda_gen_spec struct
45 * @spec: hda_gen_spec object to initialize
47 * Initialize the given hda_gen_spec object.
49 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
51 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
52 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
53 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
54 mutex_init(&spec->pcm_mutex);
57 EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
60 * snd_hda_gen_add_kctl - Add a new kctl_new struct from the template
61 * @spec: hda_gen_spec object
62 * @name: name string to override the template, NULL if unchanged
63 * @temp: template for the new kctl
65 * Add a new kctl (actually snd_kcontrol_new to be instantiated later)
66 * element based on the given snd_kcontrol_new template @temp and the
67 * name string @name to the list in @spec.
68 * Returns the newly created object or NULL as error.
70 struct snd_kcontrol_new *
71 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
72 const struct snd_kcontrol_new *temp)
74 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
79 knew->name = kstrdup(name, GFP_KERNEL);
81 knew->name = kstrdup(knew->name, GFP_KERNEL);
86 EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
88 static void free_kctls(struct hda_gen_spec *spec)
90 if (spec->kctls.list) {
91 struct snd_kcontrol_new *kctl = spec->kctls.list;
93 for (i = 0; i < spec->kctls.used; i++)
96 snd_array_free(&spec->kctls);
99 static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
104 snd_array_free(&spec->paths);
105 snd_array_free(&spec->loopback_list);
111 static void parse_user_hints(struct hda_codec *codec)
113 struct hda_gen_spec *spec = codec->spec;
116 val = snd_hda_get_bool_hint(codec, "jack_detect");
118 codec->no_jack_detect = !val;
119 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
121 codec->inv_jack_detect = !!val;
122 val = snd_hda_get_bool_hint(codec, "trigger_sense");
124 codec->no_trigger_sense = !val;
125 val = snd_hda_get_bool_hint(codec, "inv_eapd");
127 codec->inv_eapd = !!val;
128 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
130 codec->pcm_format_first = !!val;
131 val = snd_hda_get_bool_hint(codec, "sticky_stream");
133 codec->no_sticky_stream = !val;
134 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
136 codec->spdif_status_reset = !!val;
137 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
139 codec->pin_amp_workaround = !!val;
140 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
142 codec->single_adc_amp = !!val;
143 val = snd_hda_get_bool_hint(codec, "power_save_node");
145 codec->power_save_node = !!val;
147 val = snd_hda_get_bool_hint(codec, "auto_mute");
149 spec->suppress_auto_mute = !val;
150 val = snd_hda_get_bool_hint(codec, "auto_mic");
152 spec->suppress_auto_mic = !val;
153 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
155 spec->line_in_auto_switch = !!val;
156 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
158 spec->auto_mute_via_amp = !!val;
159 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
161 spec->need_dac_fix = !!val;
162 val = snd_hda_get_bool_hint(codec, "primary_hp");
164 spec->no_primary_hp = !val;
165 val = snd_hda_get_bool_hint(codec, "multi_io");
167 spec->no_multi_io = !val;
168 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
170 spec->multi_cap_vol = !!val;
171 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
173 spec->inv_dmic_split = !!val;
174 val = snd_hda_get_bool_hint(codec, "indep_hp");
176 spec->indep_hp = !!val;
177 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
179 spec->add_stereo_mix_input = !!val;
180 /* the following two are just for compatibility */
181 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
183 spec->add_jack_modes = !!val;
184 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
186 spec->add_jack_modes = !!val;
187 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
189 spec->add_jack_modes = !!val;
190 val = snd_hda_get_bool_hint(codec, "power_down_unused");
192 spec->power_down_unused = !!val;
193 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
195 spec->hp_mic = !!val;
196 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
198 spec->suppress_hp_mic_detect = !val;
199 val = snd_hda_get_bool_hint(codec, "vmaster");
201 spec->suppress_vmaster = !val;
203 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
204 spec->mixer_nid = val;
208 * pin control value accesses
211 #define update_pin_ctl(codec, pin, val) \
212 snd_hda_codec_update_cache(codec, pin, 0, \
213 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
215 /* restore the pinctl based on the cached value */
216 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
218 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
221 /* set the pinctl target value and write it if requested */
222 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
223 unsigned int val, bool do_write)
227 val = snd_hda_correct_pin_ctl(codec, pin, val);
228 snd_hda_codec_set_pin_target(codec, pin, val);
230 update_pin_ctl(codec, pin, val);
233 /* set pinctl target values for all given pins */
234 static void set_pin_targets(struct hda_codec *codec, int num_pins,
235 hda_nid_t *pins, unsigned int val)
238 for (i = 0; i < num_pins; i++)
239 set_pin_target(codec, pins[i], val, false);
246 /* return the position of NID in the list, or -1 if not found */
247 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
250 for (i = 0; i < nums; i++)
256 /* return true if the given NID is contained in the path */
257 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
259 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
262 static struct nid_path *get_nid_path(struct hda_codec *codec,
263 hda_nid_t from_nid, hda_nid_t to_nid,
266 struct hda_gen_spec *spec = codec->spec;
269 for (i = 0; i < spec->paths.used; i++) {
270 struct nid_path *path = snd_array_elem(&spec->paths, i);
271 if (path->depth <= 0)
273 if ((!from_nid || path->path[0] == from_nid) &&
274 (!to_nid || path->path[path->depth - 1] == to_nid)) {
276 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
277 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
285 * snd_hda_get_path_idx - get the index number corresponding to the path
287 * @codec: the HDA codec
288 * @path: nid_path object
290 * The returned index starts from 1, i.e. the actual array index with offset 1,
291 * and zero is handled as an invalid path
293 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
295 struct hda_gen_spec *spec = codec->spec;
296 struct nid_path *array = spec->paths.list;
299 if (!spec->paths.used)
302 if (idx < 0 || idx >= spec->paths.used)
306 EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
309 * snd_hda_get_path_from_idx - get the path instance corresponding to the
311 * @codec: the HDA codec
312 * @idx: the path index
314 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
316 struct hda_gen_spec *spec = codec->spec;
318 if (idx <= 0 || idx > spec->paths.used)
320 return snd_array_elem(&spec->paths, idx - 1);
322 EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
324 /* check whether the given DAC is already found in any existing paths */
325 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
327 struct hda_gen_spec *spec = codec->spec;
330 for (i = 0; i < spec->paths.used; i++) {
331 struct nid_path *path = snd_array_elem(&spec->paths, i);
332 if (path->path[0] == nid)
338 /* check whether the given two widgets can be connected */
339 static bool is_reachable_path(struct hda_codec *codec,
340 hda_nid_t from_nid, hda_nid_t to_nid)
342 if (!from_nid || !to_nid)
344 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
347 /* nid, dir and idx */
348 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
350 /* check whether the given ctl is already assigned in any path elements */
351 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
353 struct hda_gen_spec *spec = codec->spec;
356 val &= AMP_VAL_COMPARE_MASK;
357 for (i = 0; i < spec->paths.used; i++) {
358 struct nid_path *path = snd_array_elem(&spec->paths, i);
359 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
365 /* check whether a control with the given (nid, dir, idx) was assigned */
366 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
367 int dir, int idx, int type)
369 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
370 return is_ctl_used(codec, val, type);
373 static void print_nid_path(struct hda_codec *codec,
374 const char *pfx, struct nid_path *path)
381 for (i = 0; i < path->depth; i++)
382 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
383 pos != buf ? ":" : "",
386 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
389 /* called recursively */
390 static bool __parse_nid_path(struct hda_codec *codec,
391 hda_nid_t from_nid, hda_nid_t to_nid,
392 int anchor_nid, struct nid_path *path,
395 const hda_nid_t *conn;
398 if (to_nid == anchor_nid)
399 anchor_nid = 0; /* anchor passed */
400 else if (to_nid == (hda_nid_t)(-anchor_nid))
401 return false; /* hit the exclusive nid */
403 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
404 for (i = 0; i < nums; i++) {
405 if (conn[i] != from_nid) {
406 /* special case: when from_nid is 0,
407 * try to find an empty DAC
410 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
411 is_dac_already_used(codec, conn[i]))
414 /* anchor is not requested or already passed? */
418 if (depth >= MAX_NID_PATH_DEPTH)
420 for (i = 0; i < nums; i++) {
422 type = get_wcaps_type(get_wcaps(codec, conn[i]));
423 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
426 if (__parse_nid_path(codec, from_nid, conn[i],
427 anchor_nid, path, depth + 1))
433 path->path[path->depth] = conn[i];
434 path->idx[path->depth + 1] = i;
435 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
436 path->multi[path->depth + 1] = 1;
442 * snd_hda_parse_nid_path - parse the widget path from the given nid to
444 * @codec: the HDA codec
445 * @from_nid: the NID where the path start from
446 * @to_nid: the NID where the path ends at
447 * @anchor_nid: the anchor indication
448 * @path: the path object to store the result
450 * Returns true if a matching path is found.
452 * The parsing behavior depends on parameters:
453 * when @from_nid is 0, try to find an empty DAC;
454 * when @anchor_nid is set to a positive value, only paths through the widget
455 * with the given value are evaluated.
456 * when @anchor_nid is set to a negative value, paths through the widget
457 * with the negative of given value are excluded, only other paths are chosen.
458 * when @anchor_nid is zero, no special handling about path selection.
460 static bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
461 hda_nid_t to_nid, int anchor_nid,
462 struct nid_path *path)
464 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
465 path->path[path->depth] = to_nid;
473 * snd_hda_add_new_path - parse the path between the given NIDs and
474 * add to the path list
475 * @codec: the HDA codec
476 * @from_nid: the NID where the path start from
477 * @to_nid: the NID where the path ends at
478 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
480 * If no valid path is found, returns NULL.
483 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
484 hda_nid_t to_nid, int anchor_nid)
486 struct hda_gen_spec *spec = codec->spec;
487 struct nid_path *path;
489 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
492 /* check whether the path has been already added */
493 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
497 path = snd_array_new(&spec->paths);
500 memset(path, 0, sizeof(*path));
501 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
507 EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
509 /* clear the given path as invalid so that it won't be picked up later */
510 static void invalidate_nid_path(struct hda_codec *codec, int idx)
512 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
515 memset(path, 0, sizeof(*path));
518 /* return a DAC if paired to the given pin by codec driver */
519 static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
521 struct hda_gen_spec *spec = codec->spec;
522 const hda_nid_t *list = spec->preferred_dacs;
526 for (; *list; list += 2)
532 /* look for an empty DAC slot */
533 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
536 struct hda_gen_spec *spec = codec->spec;
540 for (i = 0; i < spec->num_all_dacs; i++) {
541 hda_nid_t nid = spec->all_dacs[i];
542 if (!nid || is_dac_already_used(codec, nid))
544 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
545 if (is_digital != cap_digital)
547 if (is_reachable_path(codec, nid, pin))
553 /* replace the channels in the composed amp value with the given number */
554 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
556 val &= ~(0x3U << 16);
561 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
562 hda_nid_t nid2, int dir)
564 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
565 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
566 return (query_amp_caps(codec, nid1, dir) ==
567 query_amp_caps(codec, nid2, dir));
570 /* look for a widget suitable for assigning a mute switch in the path */
571 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
572 struct nid_path *path)
576 for (i = path->depth - 1; i >= 0; i--) {
577 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
578 return path->path[i];
579 if (i != path->depth - 1 && i != 0 &&
580 nid_has_mute(codec, path->path[i], HDA_INPUT))
581 return path->path[i];
586 /* look for a widget suitable for assigning a volume ctl in the path */
587 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
588 struct nid_path *path)
590 struct hda_gen_spec *spec = codec->spec;
593 for (i = path->depth - 1; i >= 0; i--) {
594 hda_nid_t nid = path->path[i];
595 if ((spec->out_vol_mask >> nid) & 1)
597 if (nid_has_volume(codec, nid, HDA_OUTPUT))
604 * path activation / deactivation
607 /* can have the amp-in capability? */
608 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
610 hda_nid_t nid = path->path[idx];
611 unsigned int caps = get_wcaps(codec, nid);
612 unsigned int type = get_wcaps_type(caps);
614 if (!(caps & AC_WCAP_IN_AMP))
616 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
621 /* can have the amp-out capability? */
622 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
624 hda_nid_t nid = path->path[idx];
625 unsigned int caps = get_wcaps(codec, nid);
626 unsigned int type = get_wcaps_type(caps);
628 if (!(caps & AC_WCAP_OUT_AMP))
630 if (type == AC_WID_PIN && !idx) /* only for output pins */
635 /* check whether the given (nid,dir,idx) is active */
636 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
637 unsigned int dir, unsigned int idx)
639 struct hda_gen_spec *spec = codec->spec;
640 int type = get_wcaps_type(get_wcaps(codec, nid));
643 if (nid == codec->core.afg)
646 for (n = 0; n < spec->paths.used; n++) {
647 struct nid_path *path = snd_array_elem(&spec->paths, n);
650 if (codec->power_save_node) {
651 if (!path->stream_enabled)
653 /* ignore unplugged paths except for DAC/ADC */
654 if (!(path->pin_enabled || path->pin_fixed) &&
655 type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
658 for (i = 0; i < path->depth; i++) {
659 if (path->path[i] == nid) {
660 if (dir == HDA_OUTPUT || idx == -1 ||
670 /* check whether the NID is referred by any active paths */
671 #define is_active_nid_for_any(codec, nid) \
672 is_active_nid(codec, nid, HDA_OUTPUT, -1)
674 /* get the default amp value for the target state */
675 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
676 int dir, unsigned int caps, bool enable)
678 unsigned int val = 0;
680 if (caps & AC_AMPCAP_NUM_STEPS) {
683 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
685 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
692 /* is this a stereo widget or a stereo-to-mono mix? */
693 static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
695 unsigned int wcaps = get_wcaps(codec, nid);
698 if (wcaps & AC_WCAP_STEREO)
700 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
702 if (snd_hda_get_num_conns(codec, nid) != 1)
704 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
706 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
709 /* initialize the amp value (only at the first time) */
710 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
712 unsigned int caps = query_amp_caps(codec, nid, dir);
713 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
715 if (is_stereo_amps(codec, nid, dir))
716 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
718 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
721 /* update the amp, doing in stereo or mono depending on NID */
722 static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
723 unsigned int mask, unsigned int val)
725 if (is_stereo_amps(codec, nid, dir))
726 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
729 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
733 /* calculate amp value mask we can modify;
734 * if the given amp is controlled by mixers, don't touch it
736 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
737 hda_nid_t nid, int dir, int idx,
740 unsigned int mask = 0xff;
742 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
743 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
746 if (caps & AC_AMPCAP_NUM_STEPS) {
747 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
748 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
754 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
755 int idx, int idx_to_check, bool enable)
758 unsigned int mask, val;
760 caps = query_amp_caps(codec, nid, dir);
761 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
762 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
767 update_amp(codec, nid, dir, idx, mask, val);
770 static void check_and_activate_amp(struct hda_codec *codec, hda_nid_t nid,
771 int dir, int idx, int idx_to_check,
774 /* check whether the given amp is still used by others */
775 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
777 activate_amp(codec, nid, dir, idx, idx_to_check, enable);
780 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
783 hda_nid_t nid = path->path[i];
784 init_amp(codec, nid, HDA_OUTPUT, 0);
785 check_and_activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
788 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
789 int i, bool enable, bool add_aamix)
791 struct hda_gen_spec *spec = codec->spec;
792 const hda_nid_t *conn;
795 hda_nid_t nid = path->path[i];
797 nums = snd_hda_get_conn_list(codec, nid, &conn);
798 type = get_wcaps_type(get_wcaps(codec, nid));
799 if (type == AC_WID_PIN ||
800 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
806 for (n = 0; n < nums; n++)
807 init_amp(codec, nid, HDA_INPUT, n);
809 /* here is a little bit tricky in comparison with activate_amp_out();
810 * when aa-mixer is available, we need to enable the path as well
812 for (n = 0; n < nums; n++) {
814 if (conn[n] != spec->mixer_merge_nid)
816 /* when aamix is disabled, force to off */
818 activate_amp(codec, nid, HDA_INPUT, n, n, false);
822 check_and_activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
826 /* sync power of each widget in the the given path */
827 static hda_nid_t path_power_update(struct hda_codec *codec,
828 struct nid_path *path,
829 bool allow_powerdown)
831 hda_nid_t nid, changed = 0;
834 for (i = 0; i < path->depth; i++) {
836 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
838 if (nid == codec->core.afg)
840 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
844 power = snd_hda_codec_read(codec, nid, 0,
845 AC_VERB_GET_POWER_STATE, 0);
846 if (power != (state | (state << 4))) {
847 snd_hda_codec_write(codec, nid, 0,
848 AC_VERB_SET_POWER_STATE, state);
850 /* all known codecs seem to be capable to handl
851 * widgets state even in D3, so far.
852 * if any new codecs need to restore the widget
853 * states after D0 transition, call the function
857 if (state == AC_PWRST_D0)
858 snd_hdac_regmap_sync_node(&codec->core, nid);
865 /* do sync with the last power state change */
866 static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
870 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
875 * snd_hda_activate_path - activate or deactivate the given path
876 * @codec: the HDA codec
877 * @path: the path to activate/deactivate
878 * @enable: flag to activate or not
879 * @add_aamix: enable the input from aamix NID
881 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
883 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
884 bool enable, bool add_aamix)
886 struct hda_gen_spec *spec = codec->spec;
889 path->active = enable;
891 /* make sure the widget is powered up */
892 if (enable && (spec->power_down_unused || codec->power_save_node))
893 path_power_update(codec, path, codec->power_save_node);
895 for (i = path->depth - 1; i >= 0; i--) {
896 hda_nid_t nid = path->path[i];
898 if (enable && path->multi[i])
899 snd_hda_codec_update_cache(codec, nid, 0,
900 AC_VERB_SET_CONNECT_SEL,
902 if (has_amp_in(codec, path, i))
903 activate_amp_in(codec, path, i, enable, add_aamix);
904 if (has_amp_out(codec, path, i))
905 activate_amp_out(codec, path, i, enable);
908 EXPORT_SYMBOL_GPL(snd_hda_activate_path);
910 /* if the given path is inactive, put widgets into D3 (only if suitable) */
911 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
913 struct hda_gen_spec *spec = codec->spec;
915 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
917 sync_power_state_change(codec, path_power_update(codec, path, true));
920 /* turn on/off EAPD on the given pin */
921 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
923 struct hda_gen_spec *spec = codec->spec;
924 if (spec->own_eapd_ctl ||
925 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
927 if (spec->keep_eapd_on && !enable)
931 snd_hda_codec_update_cache(codec, pin, 0,
932 AC_VERB_SET_EAPD_BTLENABLE,
933 enable ? 0x02 : 0x00);
936 /* re-initialize the path specified by the given path index */
937 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
939 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
941 snd_hda_activate_path(codec, path, path->active, false);
946 * Helper functions for creating mixer ctl elements
949 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
950 struct snd_ctl_elem_value *ucontrol);
951 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
952 struct snd_ctl_elem_value *ucontrol);
959 static const struct snd_kcontrol_new control_templates[] = {
960 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
961 /* only the put callback is replaced for handling the special mute */
963 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
964 .subdevice = HDA_SUBDEV_AMP_FLAG,
965 .info = snd_hda_mixer_amp_switch_info,
966 .get = snd_hda_mixer_amp_switch_get,
967 .put = hda_gen_mixer_mute_put, /* replaced */
968 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
971 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
972 .info = snd_hda_mixer_amp_switch_info,
973 .get = snd_hda_mixer_bind_switch_get,
974 .put = hda_gen_bind_mute_put, /* replaced */
975 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
979 /* add dynamic controls from template */
980 static struct snd_kcontrol_new *
981 add_control(struct hda_gen_spec *spec, int type, const char *name,
982 int cidx, unsigned long val)
984 struct snd_kcontrol_new *knew;
986 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
990 if (get_amp_nid_(val))
991 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
992 knew->private_value = val;
996 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
997 const char *pfx, const char *dir,
998 const char *sfx, int cidx, unsigned long val)
1000 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1001 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
1002 if (!add_control(spec, type, name, cidx, val))
1007 #define add_pb_vol_ctrl(spec, type, pfx, val) \
1008 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1009 #define add_pb_sw_ctrl(spec, type, pfx, val) \
1010 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1011 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1012 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1013 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1014 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1016 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1017 unsigned int chs, struct nid_path *path)
1022 val = path->ctls[NID_PATH_VOL_CTL];
1025 val = amp_val_replace_channels(val, chs);
1026 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1029 /* return the channel bits suitable for the given path->ctls[] */
1030 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1033 int chs = 1; /* mono (left only) */
1035 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1036 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1037 chs = 3; /* stereo */
1042 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1043 struct nid_path *path)
1045 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1046 return add_vol_ctl(codec, pfx, cidx, chs, path);
1049 /* create a mute-switch for the given mixer widget;
1050 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1052 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1053 unsigned int chs, struct nid_path *path)
1056 int type = HDA_CTL_WIDGET_MUTE;
1060 val = path->ctls[NID_PATH_MUTE_CTL];
1063 val = amp_val_replace_channels(val, chs);
1064 if (get_amp_direction_(val) == HDA_INPUT) {
1065 hda_nid_t nid = get_amp_nid_(val);
1066 int nums = snd_hda_get_num_conns(codec, nid);
1068 type = HDA_CTL_BIND_MUTE;
1072 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1075 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1076 int cidx, struct nid_path *path)
1078 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1079 return add_sw_ctl(codec, pfx, cidx, chs, path);
1082 /* playback mute control with the software mute bit check */
1083 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1084 struct snd_ctl_elem_value *ucontrol)
1086 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1087 struct hda_gen_spec *spec = codec->spec;
1089 if (spec->auto_mute_via_amp) {
1090 hda_nid_t nid = get_amp_nid(kcontrol);
1091 bool enabled = !((spec->mute_bits >> nid) & 1);
1092 ucontrol->value.integer.value[0] &= enabled;
1093 ucontrol->value.integer.value[1] &= enabled;
1097 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1098 struct snd_ctl_elem_value *ucontrol)
1100 sync_auto_mute_bits(kcontrol, ucontrol);
1101 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1104 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1105 struct snd_ctl_elem_value *ucontrol)
1107 sync_auto_mute_bits(kcontrol, ucontrol);
1108 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1111 /* any ctl assigned to the path with the given index? */
1112 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1114 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1115 return path && path->ctls[ctl_type];
1118 static const char * const channel_name[4] = {
1119 "Front", "Surround", "CLFE", "Side"
1122 /* give some appropriate ctl name prefix for the given line out channel */
1123 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1124 int *index, int ctl_type)
1126 struct hda_gen_spec *spec = codec->spec;
1127 struct auto_pin_cfg *cfg = &spec->autocfg;
1130 if (cfg->line_outs == 1 && !spec->multi_ios &&
1131 !codec->force_pin_prefix &&
1132 !cfg->hp_outs && !cfg->speaker_outs)
1133 return spec->vmaster_mute.hook ? "PCM" : "Master";
1135 /* if there is really a single DAC used in the whole output paths,
1136 * use it master (or "PCM" if a vmaster hook is present)
1138 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1139 !codec->force_pin_prefix &&
1140 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1141 return spec->vmaster_mute.hook ? "PCM" : "Master";
1143 /* multi-io channels */
1144 if (ch >= cfg->line_outs)
1145 return channel_name[ch];
1147 switch (cfg->line_out_type) {
1148 case AUTO_PIN_SPEAKER_OUT:
1149 /* if the primary channel vol/mute is shared with HP volume,
1150 * don't name it as Speaker
1152 if (!ch && cfg->hp_outs &&
1153 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1155 if (cfg->line_outs == 1)
1157 if (cfg->line_outs == 2)
1158 return ch ? "Bass Speaker" : "Speaker";
1160 case AUTO_PIN_HP_OUT:
1161 /* if the primary channel vol/mute is shared with spk volume,
1162 * don't name it as Headphone
1164 if (!ch && cfg->speaker_outs &&
1165 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1167 /* for multi-io case, only the primary out */
1168 if (ch && spec->multi_ios)
1172 case AUTO_PIN_LINE_OUT:
1173 /* This deals with the case where we have two DACs and
1174 * one LO, one HP and one Speaker */
1175 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1176 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1177 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1178 if (hp_lo_shared && spk_lo_shared)
1179 return spec->vmaster_mute.hook ? "PCM" : "Master";
1181 return "Headphone+LO";
1183 return "Speaker+LO";
1187 /* for a single channel output, we don't have to name the channel */
1188 if (cfg->line_outs == 1 && !spec->multi_ios)
1191 if (ch >= ARRAY_SIZE(channel_name)) {
1196 return channel_name[ch];
1200 * Parse output paths
1203 /* badness definition */
1205 /* No primary DAC is found for the main output */
1206 BAD_NO_PRIMARY_DAC = 0x10000,
1207 /* No DAC is found for the extra output */
1208 BAD_NO_DAC = 0x4000,
1209 /* No possible multi-ios */
1210 BAD_MULTI_IO = 0x120,
1211 /* No individual DAC for extra output */
1212 BAD_NO_EXTRA_DAC = 0x102,
1213 /* No individual DAC for extra surrounds */
1214 BAD_NO_EXTRA_SURR_DAC = 0x101,
1215 /* Primary DAC shared with main surrounds */
1216 BAD_SHARED_SURROUND = 0x100,
1217 /* No independent HP possible */
1218 BAD_NO_INDEP_HP = 0x10,
1219 /* Primary DAC shared with main CLFE */
1220 BAD_SHARED_CLFE = 0x10,
1221 /* Primary DAC shared with extra surrounds */
1222 BAD_SHARED_EXTRA_SURROUND = 0x10,
1223 /* Volume widget is shared */
1224 BAD_SHARED_VOL = 0x10,
1227 /* look for widgets in the given path which are appropriate for
1228 * volume and mute controls, and assign the values to ctls[].
1230 * When no appropriate widget is found in the path, the badness value
1231 * is incremented depending on the situation. The function returns the
1232 * total badness for both volume and mute controls.
1234 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1236 struct hda_gen_spec *spec = codec->spec;
1242 return BAD_SHARED_VOL * 2;
1244 if (path->ctls[NID_PATH_VOL_CTL] ||
1245 path->ctls[NID_PATH_MUTE_CTL])
1246 return 0; /* already evaluated */
1248 nid = look_for_out_vol_nid(codec, path);
1250 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1251 if (spec->dac_min_mute)
1252 val |= HDA_AMP_VAL_MIN_MUTE;
1253 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1254 badness += BAD_SHARED_VOL;
1256 path->ctls[NID_PATH_VOL_CTL] = val;
1258 badness += BAD_SHARED_VOL;
1259 nid = look_for_out_mute_nid(codec, path);
1261 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1262 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1263 nid_has_mute(codec, nid, HDA_OUTPUT))
1264 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1266 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1267 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1268 badness += BAD_SHARED_VOL;
1270 path->ctls[NID_PATH_MUTE_CTL] = val;
1272 badness += BAD_SHARED_VOL;
1276 const struct badness_table hda_main_out_badness = {
1277 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1278 .no_dac = BAD_NO_DAC,
1279 .shared_primary = BAD_NO_PRIMARY_DAC,
1280 .shared_surr = BAD_SHARED_SURROUND,
1281 .shared_clfe = BAD_SHARED_CLFE,
1282 .shared_surr_main = BAD_SHARED_SURROUND,
1284 EXPORT_SYMBOL_GPL(hda_main_out_badness);
1286 const struct badness_table hda_extra_out_badness = {
1287 .no_primary_dac = BAD_NO_DAC,
1288 .no_dac = BAD_NO_DAC,
1289 .shared_primary = BAD_NO_EXTRA_DAC,
1290 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1291 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1292 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1294 EXPORT_SYMBOL_GPL(hda_extra_out_badness);
1296 /* get the DAC of the primary output corresponding to the given array index */
1297 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1299 struct hda_gen_spec *spec = codec->spec;
1300 struct auto_pin_cfg *cfg = &spec->autocfg;
1302 if (cfg->line_outs > idx)
1303 return spec->private_dac_nids[idx];
1304 idx -= cfg->line_outs;
1305 if (spec->multi_ios > idx)
1306 return spec->multi_io[idx].dac;
1310 /* return the DAC if it's reachable, otherwise zero */
1311 static inline hda_nid_t try_dac(struct hda_codec *codec,
1312 hda_nid_t dac, hda_nid_t pin)
1314 return is_reachable_path(codec, dac, pin) ? dac : 0;
1317 /* try to assign DACs to pins and return the resultant badness */
1318 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1319 const hda_nid_t *pins, hda_nid_t *dacs,
1321 const struct badness_table *bad)
1323 struct hda_gen_spec *spec = codec->spec;
1331 for (i = 0; i < num_outs; i++) {
1332 struct nid_path *path;
1333 hda_nid_t pin = pins[i];
1335 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1337 badness += assign_out_path_ctls(codec, path);
1341 dacs[i] = get_preferred_dac(codec, pin);
1343 if (is_dac_already_used(codec, dacs[i]))
1344 badness += bad->shared_primary;
1348 dacs[i] = look_for_dac(codec, pin, false);
1349 if (!dacs[i] && !i) {
1350 /* try to steal the DAC of surrounds for the front */
1351 for (j = 1; j < num_outs; j++) {
1352 if (is_reachable_path(codec, dacs[j], pin)) {
1355 invalidate_nid_path(codec, path_idx[j]);
1364 dac = try_dac(codec, get_primary_out(codec, i), pin);
1366 dac = try_dac(codec, dacs[0], pin);
1368 dac = try_dac(codec, get_primary_out(codec, i), pin);
1371 badness += bad->shared_primary;
1373 badness += bad->shared_surr;
1375 badness += bad->shared_clfe;
1376 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1377 dac = spec->private_dac_nids[0];
1378 badness += bad->shared_surr_main;
1380 badness += bad->no_primary_dac;
1382 badness += bad->no_dac;
1386 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1387 if (!path && !i && spec->mixer_nid) {
1388 /* try with aamix */
1389 path = snd_hda_add_new_path(codec, dac, pin, 0);
1393 badness += bad->no_dac;
1395 /* print_nid_path(codec, "output", path); */
1396 path->active = true;
1397 path_idx[i] = snd_hda_get_path_idx(codec, path);
1398 badness += assign_out_path_ctls(codec, path);
1405 /* return NID if the given pin has only a single connection to a certain DAC */
1406 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1408 struct hda_gen_spec *spec = codec->spec;
1410 hda_nid_t nid_found = 0;
1412 for (i = 0; i < spec->num_all_dacs; i++) {
1413 hda_nid_t nid = spec->all_dacs[i];
1414 if (!nid || is_dac_already_used(codec, nid))
1416 if (is_reachable_path(codec, nid, pin)) {
1425 /* check whether the given pin can be a multi-io pin */
1426 static bool can_be_multiio_pin(struct hda_codec *codec,
1427 unsigned int location, hda_nid_t nid)
1429 unsigned int defcfg, caps;
1431 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1432 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1434 if (location && get_defcfg_location(defcfg) != location)
1436 caps = snd_hda_query_pin_caps(codec, nid);
1437 if (!(caps & AC_PINCAP_OUT))
1442 /* count the number of input pins that are capable to be multi-io */
1443 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1445 struct hda_gen_spec *spec = codec->spec;
1446 struct auto_pin_cfg *cfg = &spec->autocfg;
1447 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1448 unsigned int location = get_defcfg_location(defcfg);
1452 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1453 for (i = 0; i < cfg->num_inputs; i++) {
1454 if (cfg->inputs[i].type != type)
1456 if (can_be_multiio_pin(codec, location,
1457 cfg->inputs[i].pin))
1467 * When hardwired is set, try to fill ony hardwired pins, and returns
1468 * zero if any pins are filled, non-zero if nothing found.
1469 * When hardwired is off, try to fill possible input pins, and returns
1470 * the badness value.
1472 static int fill_multi_ios(struct hda_codec *codec,
1473 hda_nid_t reference_pin,
1476 struct hda_gen_spec *spec = codec->spec;
1477 struct auto_pin_cfg *cfg = &spec->autocfg;
1478 int type, i, j, num_pins, old_pins;
1479 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1480 unsigned int location = get_defcfg_location(defcfg);
1482 struct nid_path *path;
1484 old_pins = spec->multi_ios;
1488 num_pins = count_multiio_pins(codec, reference_pin);
1492 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1493 for (i = 0; i < cfg->num_inputs; i++) {
1494 hda_nid_t nid = cfg->inputs[i].pin;
1497 if (cfg->inputs[i].type != type)
1499 if (!can_be_multiio_pin(codec, location, nid))
1501 for (j = 0; j < spec->multi_ios; j++) {
1502 if (nid == spec->multi_io[j].pin)
1505 if (j < spec->multi_ios)
1509 dac = get_dac_if_single(codec, nid);
1511 dac = look_for_dac(codec, nid, false);
1516 path = snd_hda_add_new_path(codec, dac, nid,
1522 /* print_nid_path(codec, "multiio", path); */
1523 spec->multi_io[spec->multi_ios].pin = nid;
1524 spec->multi_io[spec->multi_ios].dac = dac;
1525 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1526 snd_hda_get_path_idx(codec, path);
1528 if (spec->multi_ios >= 2)
1534 badness = BAD_MULTI_IO;
1535 if (old_pins == spec->multi_ios) {
1537 return 1; /* nothing found */
1539 return badness; /* no badness if nothing found */
1541 if (!hardwired && spec->multi_ios < 2) {
1542 /* cancel newly assigned paths */
1543 spec->paths.used -= spec->multi_ios - old_pins;
1544 spec->multi_ios = old_pins;
1548 /* assign volume and mute controls */
1549 for (i = old_pins; i < spec->multi_ios; i++) {
1550 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1551 badness += assign_out_path_ctls(codec, path);
1557 /* map DACs for all pins in the list if they are single connections */
1558 static bool map_singles(struct hda_codec *codec, int outs,
1559 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1561 struct hda_gen_spec *spec = codec->spec;
1564 for (i = 0; i < outs; i++) {
1565 struct nid_path *path;
1569 dac = get_dac_if_single(codec, pins[i]);
1572 path = snd_hda_add_new_path(codec, dac, pins[i],
1574 if (!path && !i && spec->mixer_nid)
1575 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1579 /* print_nid_path(codec, "output", path); */
1580 path->active = true;
1581 path_idx[i] = snd_hda_get_path_idx(codec, path);
1587 static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1589 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1590 spec->aamix_out_paths[2];
1593 /* create a new path including aamix if available, and return its index */
1594 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1596 struct hda_gen_spec *spec = codec->spec;
1597 struct nid_path *path;
1598 hda_nid_t path_dac, dac, pin;
1600 path = snd_hda_get_path_from_idx(codec, path_idx);
1601 if (!path || !path->depth ||
1602 is_nid_contained(path, spec->mixer_nid))
1604 path_dac = path->path[0];
1605 dac = spec->private_dac_nids[0];
1606 pin = path->path[path->depth - 1];
1607 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1609 if (dac != path_dac)
1611 else if (spec->multiout.hp_out_nid[0])
1612 dac = spec->multiout.hp_out_nid[0];
1613 else if (spec->multiout.extra_out_nid[0])
1614 dac = spec->multiout.extra_out_nid[0];
1618 path = snd_hda_add_new_path(codec, dac, pin,
1623 /* print_nid_path(codec, "output-aamix", path); */
1624 path->active = false; /* unused as default */
1625 path->pin_fixed = true; /* static route */
1626 return snd_hda_get_path_idx(codec, path);
1629 /* check whether the independent HP is available with the current config */
1630 static bool indep_hp_possible(struct hda_codec *codec)
1632 struct hda_gen_spec *spec = codec->spec;
1633 struct auto_pin_cfg *cfg = &spec->autocfg;
1634 struct nid_path *path;
1637 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1638 idx = spec->out_paths[0];
1640 idx = spec->hp_paths[0];
1641 path = snd_hda_get_path_from_idx(codec, idx);
1645 /* assume no path conflicts unless aamix is involved */
1646 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1649 /* check whether output paths contain aamix */
1650 for (i = 0; i < cfg->line_outs; i++) {
1651 if (spec->out_paths[i] == idx)
1653 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1654 if (path && is_nid_contained(path, spec->mixer_nid))
1657 for (i = 0; i < cfg->speaker_outs; i++) {
1658 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1659 if (path && is_nid_contained(path, spec->mixer_nid))
1666 /* fill the empty entries in the dac array for speaker/hp with the
1667 * shared dac pointed by the paths
1669 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1670 hda_nid_t *dacs, int *path_idx)
1672 struct nid_path *path;
1675 for (i = 0; i < num_outs; i++) {
1678 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1681 dacs[i] = path->path[0];
1685 /* fill in the dac_nids table from the parsed pin configuration */
1686 static int fill_and_eval_dacs(struct hda_codec *codec,
1687 bool fill_hardwired,
1688 bool fill_mio_first)
1690 struct hda_gen_spec *spec = codec->spec;
1691 struct auto_pin_cfg *cfg = &spec->autocfg;
1692 int i, err, badness;
1694 /* set num_dacs once to full for look_for_dac() */
1695 spec->multiout.num_dacs = cfg->line_outs;
1696 spec->multiout.dac_nids = spec->private_dac_nids;
1697 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1698 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1699 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1700 spec->multi_ios = 0;
1701 snd_array_free(&spec->paths);
1703 /* clear path indices */
1704 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1705 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1706 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1707 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1708 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1709 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1710 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1711 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1715 /* fill hard-wired DACs first */
1716 if (fill_hardwired) {
1719 mapped = map_singles(codec, cfg->line_outs,
1721 spec->private_dac_nids,
1723 mapped |= map_singles(codec, cfg->hp_outs,
1725 spec->multiout.hp_out_nid,
1727 mapped |= map_singles(codec, cfg->speaker_outs,
1729 spec->multiout.extra_out_nid,
1730 spec->speaker_paths);
1731 if (!spec->no_multi_io &&
1732 fill_mio_first && cfg->line_outs == 1 &&
1733 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1734 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1741 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1742 spec->private_dac_nids, spec->out_paths,
1743 spec->main_out_badness);
1745 if (!spec->no_multi_io && fill_mio_first &&
1746 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1747 /* try to fill multi-io first */
1748 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1751 /* we don't count badness at this stage yet */
1754 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1755 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1756 spec->multiout.hp_out_nid,
1758 spec->extra_out_badness);
1763 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1764 err = try_assign_dacs(codec, cfg->speaker_outs,
1766 spec->multiout.extra_out_nid,
1767 spec->speaker_paths,
1768 spec->extra_out_badness);
1773 if (!spec->no_multi_io &&
1774 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1775 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1781 if (spec->mixer_nid) {
1782 spec->aamix_out_paths[0] =
1783 check_aamix_out_path(codec, spec->out_paths[0]);
1784 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1785 spec->aamix_out_paths[1] =
1786 check_aamix_out_path(codec, spec->hp_paths[0]);
1787 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1788 spec->aamix_out_paths[2] =
1789 check_aamix_out_path(codec, spec->speaker_paths[0]);
1792 if (!spec->no_multi_io &&
1793 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1794 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1795 spec->multi_ios = 1; /* give badness */
1797 /* re-count num_dacs and squash invalid entries */
1798 spec->multiout.num_dacs = 0;
1799 for (i = 0; i < cfg->line_outs; i++) {
1800 if (spec->private_dac_nids[i])
1801 spec->multiout.num_dacs++;
1803 memmove(spec->private_dac_nids + i,
1804 spec->private_dac_nids + i + 1,
1805 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1806 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1810 spec->ext_channel_count = spec->min_channel_count =
1811 spec->multiout.num_dacs * 2;
1813 if (spec->multi_ios == 2) {
1814 for (i = 0; i < 2; i++)
1815 spec->private_dac_nids[spec->multiout.num_dacs++] =
1816 spec->multi_io[i].dac;
1817 } else if (spec->multi_ios) {
1818 spec->multi_ios = 0;
1819 badness += BAD_MULTI_IO;
1822 if (spec->indep_hp && !indep_hp_possible(codec))
1823 badness += BAD_NO_INDEP_HP;
1825 /* re-fill the shared DAC for speaker / headphone */
1826 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1827 refill_shared_dacs(codec, cfg->hp_outs,
1828 spec->multiout.hp_out_nid,
1830 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1831 refill_shared_dacs(codec, cfg->speaker_outs,
1832 spec->multiout.extra_out_nid,
1833 spec->speaker_paths);
1838 #define DEBUG_BADNESS
1840 #ifdef DEBUG_BADNESS
1841 #define debug_badness(fmt, ...) \
1842 codec_dbg(codec, fmt, ##__VA_ARGS__)
1844 #define debug_badness(fmt, ...) \
1845 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1848 #ifdef DEBUG_BADNESS
1849 static inline void print_nid_path_idx(struct hda_codec *codec,
1850 const char *pfx, int idx)
1852 struct nid_path *path;
1854 path = snd_hda_get_path_from_idx(codec, idx);
1856 print_nid_path(codec, pfx, path);
1859 static void debug_show_configs(struct hda_codec *codec,
1860 struct auto_pin_cfg *cfg)
1862 struct hda_gen_spec *spec = codec->spec;
1863 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1866 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1867 cfg->line_out_pins[0], cfg->line_out_pins[1],
1868 cfg->line_out_pins[2], cfg->line_out_pins[3],
1869 spec->multiout.dac_nids[0],
1870 spec->multiout.dac_nids[1],
1871 spec->multiout.dac_nids[2],
1872 spec->multiout.dac_nids[3],
1873 lo_type[cfg->line_out_type]);
1874 for (i = 0; i < cfg->line_outs; i++)
1875 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1876 if (spec->multi_ios > 0)
1877 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1879 spec->multi_io[0].pin, spec->multi_io[1].pin,
1880 spec->multi_io[0].dac, spec->multi_io[1].dac);
1881 for (i = 0; i < spec->multi_ios; i++)
1882 print_nid_path_idx(codec, " mio",
1883 spec->out_paths[cfg->line_outs + i]);
1885 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1886 cfg->hp_pins[0], cfg->hp_pins[1],
1887 cfg->hp_pins[2], cfg->hp_pins[3],
1888 spec->multiout.hp_out_nid[0],
1889 spec->multiout.hp_out_nid[1],
1890 spec->multiout.hp_out_nid[2],
1891 spec->multiout.hp_out_nid[3]);
1892 for (i = 0; i < cfg->hp_outs; i++)
1893 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1894 if (cfg->speaker_outs)
1895 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1896 cfg->speaker_pins[0], cfg->speaker_pins[1],
1897 cfg->speaker_pins[2], cfg->speaker_pins[3],
1898 spec->multiout.extra_out_nid[0],
1899 spec->multiout.extra_out_nid[1],
1900 spec->multiout.extra_out_nid[2],
1901 spec->multiout.extra_out_nid[3]);
1902 for (i = 0; i < cfg->speaker_outs; i++)
1903 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1904 for (i = 0; i < 3; i++)
1905 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1908 #define debug_show_configs(codec, cfg) /* NOP */
1911 /* find all available DACs of the codec */
1912 static void fill_all_dac_nids(struct hda_codec *codec)
1914 struct hda_gen_spec *spec = codec->spec;
1917 spec->num_all_dacs = 0;
1918 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1919 for_each_hda_codec_node(nid, codec) {
1920 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1922 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1923 codec_err(codec, "Too many DACs!\n");
1926 spec->all_dacs[spec->num_all_dacs++] = nid;
1930 static int parse_output_paths(struct hda_codec *codec)
1932 struct hda_gen_spec *spec = codec->spec;
1933 struct auto_pin_cfg *cfg = &spec->autocfg;
1934 struct auto_pin_cfg *best_cfg;
1936 int best_badness = INT_MAX;
1938 bool fill_hardwired = true, fill_mio_first = true;
1939 bool best_wired = true, best_mio = true;
1940 bool hp_spk_swapped = false;
1942 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1948 badness = fill_and_eval_dacs(codec, fill_hardwired,
1954 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1955 cfg->line_out_type, fill_hardwired, fill_mio_first,
1957 debug_show_configs(codec, cfg);
1958 if (badness < best_badness) {
1959 best_badness = badness;
1961 best_wired = fill_hardwired;
1962 best_mio = fill_mio_first;
1966 fill_mio_first = !fill_mio_first;
1967 if (!fill_mio_first)
1969 fill_hardwired = !fill_hardwired;
1970 if (!fill_hardwired)
1974 hp_spk_swapped = true;
1975 if (cfg->speaker_outs > 0 &&
1976 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1977 cfg->hp_outs = cfg->line_outs;
1978 memcpy(cfg->hp_pins, cfg->line_out_pins,
1979 sizeof(cfg->hp_pins));
1980 cfg->line_outs = cfg->speaker_outs;
1981 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1982 sizeof(cfg->speaker_pins));
1983 cfg->speaker_outs = 0;
1984 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1985 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1986 fill_hardwired = true;
1989 if (cfg->hp_outs > 0 &&
1990 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1991 cfg->speaker_outs = cfg->line_outs;
1992 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1993 sizeof(cfg->speaker_pins));
1994 cfg->line_outs = cfg->hp_outs;
1995 memcpy(cfg->line_out_pins, cfg->hp_pins,
1996 sizeof(cfg->hp_pins));
1998 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1999 cfg->line_out_type = AUTO_PIN_HP_OUT;
2000 fill_hardwired = true;
2007 debug_badness("==> restoring best_cfg\n");
2009 fill_and_eval_dacs(codec, best_wired, best_mio);
2011 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2012 cfg->line_out_type, best_wired, best_mio);
2013 debug_show_configs(codec, cfg);
2015 if (cfg->line_out_pins[0]) {
2016 struct nid_path *path;
2017 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
2019 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
2020 if (spec->vmaster_nid) {
2021 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2022 HDA_OUTPUT, spec->vmaster_tlv);
2023 if (spec->dac_min_mute)
2024 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2028 /* set initial pinctl targets */
2029 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2033 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2034 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2035 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2036 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2037 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2038 set_pin_targets(codec, cfg->speaker_outs,
2039 cfg->speaker_pins, val);
2042 /* clear indep_hp flag if not available */
2043 if (spec->indep_hp && !indep_hp_possible(codec))
2050 /* add playback controls from the parsed DAC table */
2051 static int create_multi_out_ctls(struct hda_codec *codec,
2052 const struct auto_pin_cfg *cfg)
2054 struct hda_gen_spec *spec = codec->spec;
2055 int i, err, noutputs;
2057 noutputs = cfg->line_outs;
2058 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2059 noutputs += spec->multi_ios;
2061 for (i = 0; i < noutputs; i++) {
2064 struct nid_path *path;
2066 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
2070 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
2071 if (!name || !strcmp(name, "CLFE")) {
2073 err = add_vol_ctl(codec, "Center", 0, 1, path);
2076 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2080 err = add_stereo_vol(codec, name, index, path);
2085 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2086 if (!name || !strcmp(name, "CLFE")) {
2087 err = add_sw_ctl(codec, "Center", 0, 1, path);
2090 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2094 err = add_stereo_sw(codec, name, index, path);
2102 static int create_extra_out(struct hda_codec *codec, int path_idx,
2103 const char *pfx, int cidx)
2105 struct nid_path *path;
2108 path = snd_hda_get_path_from_idx(codec, path_idx);
2111 err = add_stereo_vol(codec, pfx, cidx, path);
2114 err = add_stereo_sw(codec, pfx, cidx, path);
2120 /* add playback controls for speaker and HP outputs */
2121 static int create_extra_outs(struct hda_codec *codec, int num_pins,
2122 const int *paths, const char *pfx)
2126 for (i = 0; i < num_pins; i++) {
2128 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2131 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2132 name = "Bass Speaker";
2133 else if (num_pins >= 3) {
2134 snprintf(tmp, sizeof(tmp), "%s %s",
2135 pfx, channel_name[i]);
2141 err = create_extra_out(codec, paths[i], name, idx);
2148 static int create_hp_out_ctls(struct hda_codec *codec)
2150 struct hda_gen_spec *spec = codec->spec;
2151 return create_extra_outs(codec, spec->autocfg.hp_outs,
2156 static int create_speaker_out_ctls(struct hda_codec *codec)
2158 struct hda_gen_spec *spec = codec->spec;
2159 return create_extra_outs(codec, spec->autocfg.speaker_outs,
2160 spec->speaker_paths,
2165 * independent HP controls
2168 static void call_hp_automute(struct hda_codec *codec,
2169 struct hda_jack_callback *jack);
2170 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2171 struct snd_ctl_elem_info *uinfo)
2173 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2176 static int indep_hp_get(struct snd_kcontrol *kcontrol,
2177 struct snd_ctl_elem_value *ucontrol)
2179 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2180 struct hda_gen_spec *spec = codec->spec;
2181 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2185 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2186 int nomix_path_idx, int mix_path_idx,
2189 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2190 struct snd_ctl_elem_value *ucontrol)
2192 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2193 struct hda_gen_spec *spec = codec->spec;
2194 unsigned int select = ucontrol->value.enumerated.item[0];
2197 mutex_lock(&spec->pcm_mutex);
2198 if (spec->active_streams) {
2203 if (spec->indep_hp_enabled != select) {
2205 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2206 dacp = &spec->private_dac_nids[0];
2208 dacp = &spec->multiout.hp_out_nid[0];
2210 /* update HP aamix paths in case it conflicts with indep HP */
2211 if (spec->have_aamix_ctl) {
2212 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2213 update_aamix_paths(codec, spec->aamix_mode,
2215 spec->aamix_out_paths[0],
2216 spec->autocfg.line_out_type);
2218 update_aamix_paths(codec, spec->aamix_mode,
2220 spec->aamix_out_paths[1],
2224 spec->indep_hp_enabled = select;
2225 if (spec->indep_hp_enabled)
2228 *dacp = spec->alt_dac_nid;
2230 call_hp_automute(codec, NULL);
2234 mutex_unlock(&spec->pcm_mutex);
2238 static const struct snd_kcontrol_new indep_hp_ctl = {
2239 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2240 .name = "Independent HP",
2241 .info = indep_hp_info,
2242 .get = indep_hp_get,
2243 .put = indep_hp_put,
2247 static int create_indep_hp_ctls(struct hda_codec *codec)
2249 struct hda_gen_spec *spec = codec->spec;
2252 if (!spec->indep_hp)
2254 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2255 dac = spec->multiout.dac_nids[0];
2257 dac = spec->multiout.hp_out_nid[0];
2263 spec->indep_hp_enabled = false;
2264 spec->alt_dac_nid = dac;
2265 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2271 * channel mode enum control
2274 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2275 struct snd_ctl_elem_info *uinfo)
2277 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2278 struct hda_gen_spec *spec = codec->spec;
2281 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2283 uinfo->value.enumerated.items = spec->multi_ios + 1;
2284 if (uinfo->value.enumerated.item > spec->multi_ios)
2285 uinfo->value.enumerated.item = spec->multi_ios;
2286 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2287 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2291 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2292 struct snd_ctl_elem_value *ucontrol)
2294 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2295 struct hda_gen_spec *spec = codec->spec;
2296 ucontrol->value.enumerated.item[0] =
2297 (spec->ext_channel_count - spec->min_channel_count) / 2;
2301 static inline struct nid_path *
2302 get_multiio_path(struct hda_codec *codec, int idx)
2304 struct hda_gen_spec *spec = codec->spec;
2305 return snd_hda_get_path_from_idx(codec,
2306 spec->out_paths[spec->autocfg.line_outs + idx]);
2309 static void update_automute_all(struct hda_codec *codec);
2311 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2312 * used for output paths
2314 static bool aamix_default(struct hda_gen_spec *spec)
2316 return !spec->have_aamix_ctl || spec->aamix_mode;
2319 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2321 struct hda_gen_spec *spec = codec->spec;
2322 hda_nid_t nid = spec->multi_io[idx].pin;
2323 struct nid_path *path;
2325 path = get_multiio_path(codec, idx);
2329 if (path->active == output)
2333 set_pin_target(codec, nid, PIN_OUT, true);
2334 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2335 set_pin_eapd(codec, nid, true);
2337 set_pin_eapd(codec, nid, false);
2338 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2339 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2340 path_power_down_sync(codec, path);
2343 /* update jack retasking in case it modifies any of them */
2344 update_automute_all(codec);
2349 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2350 struct snd_ctl_elem_value *ucontrol)
2352 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2353 struct hda_gen_spec *spec = codec->spec;
2356 ch = ucontrol->value.enumerated.item[0];
2357 if (ch < 0 || ch > spec->multi_ios)
2359 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2361 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2362 for (i = 0; i < spec->multi_ios; i++)
2363 set_multi_io(codec, i, i < ch);
2364 spec->multiout.max_channels = max(spec->ext_channel_count,
2365 spec->const_channel_count);
2366 if (spec->need_dac_fix)
2367 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2371 static const struct snd_kcontrol_new channel_mode_enum = {
2372 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2373 .name = "Channel Mode",
2374 .info = ch_mode_info,
2379 static int create_multi_channel_mode(struct hda_codec *codec)
2381 struct hda_gen_spec *spec = codec->spec;
2383 if (spec->multi_ios > 0) {
2384 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2391 * aamix loopback enable/disable switch
2394 #define loopback_mixing_info indep_hp_info
2396 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2397 struct snd_ctl_elem_value *ucontrol)
2399 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2400 struct hda_gen_spec *spec = codec->spec;
2401 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2405 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2406 int nomix_path_idx, int mix_path_idx,
2409 struct hda_gen_spec *spec = codec->spec;
2410 struct nid_path *nomix_path, *mix_path;
2412 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2413 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2414 if (!nomix_path || !mix_path)
2417 /* if HP aamix path is driven from a different DAC and the
2418 * independent HP mode is ON, can't turn on aamix path
2420 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2421 mix_path->path[0] != spec->alt_dac_nid)
2425 snd_hda_activate_path(codec, nomix_path, false, true);
2426 snd_hda_activate_path(codec, mix_path, true, true);
2427 path_power_down_sync(codec, nomix_path);
2429 snd_hda_activate_path(codec, mix_path, false, false);
2430 snd_hda_activate_path(codec, nomix_path, true, false);
2431 path_power_down_sync(codec, mix_path);
2435 /* re-initialize the output paths; only called from loopback_mixing_put() */
2436 static void update_output_paths(struct hda_codec *codec, int num_outs,
2439 struct hda_gen_spec *spec = codec->spec;
2440 struct nid_path *path;
2443 for (i = 0; i < num_outs; i++) {
2444 path = snd_hda_get_path_from_idx(codec, paths[i]);
2446 snd_hda_activate_path(codec, path, path->active,
2451 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2452 struct snd_ctl_elem_value *ucontrol)
2454 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2455 struct hda_gen_spec *spec = codec->spec;
2456 const struct auto_pin_cfg *cfg = &spec->autocfg;
2457 unsigned int val = ucontrol->value.enumerated.item[0];
2459 if (val == spec->aamix_mode)
2461 spec->aamix_mode = val;
2462 if (has_aamix_out_paths(spec)) {
2463 update_aamix_paths(codec, val, spec->out_paths[0],
2464 spec->aamix_out_paths[0],
2465 cfg->line_out_type);
2466 update_aamix_paths(codec, val, spec->hp_paths[0],
2467 spec->aamix_out_paths[1],
2469 update_aamix_paths(codec, val, spec->speaker_paths[0],
2470 spec->aamix_out_paths[2],
2471 AUTO_PIN_SPEAKER_OUT);
2473 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2474 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2475 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2476 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2477 update_output_paths(codec, cfg->speaker_outs,
2478 spec->speaker_paths);
2483 static const struct snd_kcontrol_new loopback_mixing_enum = {
2484 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2485 .name = "Loopback Mixing",
2486 .info = loopback_mixing_info,
2487 .get = loopback_mixing_get,
2488 .put = loopback_mixing_put,
2491 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2493 struct hda_gen_spec *spec = codec->spec;
2495 if (!spec->mixer_nid)
2497 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2499 spec->have_aamix_ctl = 1;
2504 * shared headphone/mic handling
2507 static void call_update_outputs(struct hda_codec *codec);
2509 /* for shared I/O, change the pin-control accordingly */
2510 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2512 struct hda_gen_spec *spec = codec->spec;
2517 pin = spec->hp_mic_pin;
2518 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2521 val = snd_hda_codec_get_pin_target(codec, pin);
2531 val = snd_hda_get_default_vref(codec, pin);
2532 /* if the HP pin doesn't support VREF and the codec driver gives an
2533 * alternative pin, set up the VREF on that pin instead
2535 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2536 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2537 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2538 if (vref_val != AC_PINCTL_VREF_HIZ)
2539 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2540 PIN_IN | (as_mic ? vref_val : 0));
2543 if (!spec->hp_mic_jack_modes) {
2548 set_pin_target(codec, pin, val, true);
2549 call_hp_automute(codec, NULL);
2553 /* create a shared input with the headphone out */
2554 static int create_hp_mic(struct hda_codec *codec)
2556 struct hda_gen_spec *spec = codec->spec;
2557 struct auto_pin_cfg *cfg = &spec->autocfg;
2558 unsigned int defcfg;
2561 if (!spec->hp_mic) {
2562 if (spec->suppress_hp_mic_detect)
2564 /* automatic detection: only if no input or a single internal
2565 * input pin is found, try to detect the shared hp/mic
2567 if (cfg->num_inputs > 1)
2569 else if (cfg->num_inputs == 1) {
2570 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2571 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2576 spec->hp_mic = 0; /* clear once */
2577 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2581 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2582 nid = cfg->line_out_pins[0];
2583 else if (cfg->hp_outs > 0)
2584 nid = cfg->hp_pins[0];
2588 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2589 return 0; /* no input */
2591 cfg->inputs[cfg->num_inputs].pin = nid;
2592 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2593 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2596 spec->hp_mic_pin = nid;
2597 /* we can't handle auto-mic together with HP-mic */
2598 spec->suppress_auto_mic = 1;
2599 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
2607 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2609 static const char * const out_jack_texts[] = {
2610 "Line Out", "Headphone Out",
2613 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2614 struct snd_ctl_elem_info *uinfo)
2616 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2619 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2620 struct snd_ctl_elem_value *ucontrol)
2622 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2623 hda_nid_t nid = kcontrol->private_value;
2624 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2625 ucontrol->value.enumerated.item[0] = 1;
2627 ucontrol->value.enumerated.item[0] = 0;
2631 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2632 struct snd_ctl_elem_value *ucontrol)
2634 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2635 hda_nid_t nid = kcontrol->private_value;
2638 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2639 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2641 snd_hda_set_pin_ctl_cache(codec, nid, val);
2645 static const struct snd_kcontrol_new out_jack_mode_enum = {
2646 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2647 .info = out_jack_mode_info,
2648 .get = out_jack_mode_get,
2649 .put = out_jack_mode_put,
2652 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2654 struct hda_gen_spec *spec = codec->spec;
2657 for (i = 0; i < spec->kctls.used; i++) {
2658 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2659 if (!strcmp(kctl->name, name) && kctl->index == idx)
2665 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2666 char *name, size_t name_len)
2668 struct hda_gen_spec *spec = codec->spec;
2671 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2672 strlcat(name, " Jack Mode", name_len);
2674 for (; find_kctl_name(codec, name, idx); idx++)
2678 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2680 struct hda_gen_spec *spec = codec->spec;
2681 if (spec->add_jack_modes) {
2682 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2683 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2689 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2692 struct hda_gen_spec *spec = codec->spec;
2695 for (i = 0; i < num_pins; i++) {
2696 hda_nid_t pin = pins[i];
2697 if (pin == spec->hp_mic_pin)
2699 if (get_out_jack_num_items(codec, pin) > 1) {
2700 struct snd_kcontrol_new *knew;
2701 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2702 get_jack_mode_name(codec, pin, name, sizeof(name));
2703 knew = snd_hda_gen_add_kctl(spec, name,
2704 &out_jack_mode_enum);
2707 knew->private_value = pin;
2718 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2721 static const char * const vref_texts[NUM_VREFS] = {
2722 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2723 "", "Mic 80pc Bias", "Mic 100pc Bias"
2726 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2728 unsigned int pincap;
2730 pincap = snd_hda_query_pin_caps(codec, pin);
2731 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2732 /* filter out unusual vrefs */
2733 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2737 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2738 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2740 unsigned int i, n = 0;
2742 for (i = 0; i < NUM_VREFS; i++) {
2743 if (vref_caps & (1 << i)) {
2752 /* convert back from the vref ctl index to the enum item index */
2753 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2755 unsigned int i, n = 0;
2757 for (i = 0; i < NUM_VREFS; i++) {
2760 if (vref_caps & (1 << i))
2766 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2767 struct snd_ctl_elem_info *uinfo)
2769 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2770 hda_nid_t nid = kcontrol->private_value;
2771 unsigned int vref_caps = get_vref_caps(codec, nid);
2773 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2775 /* set the right text */
2776 strcpy(uinfo->value.enumerated.name,
2777 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2781 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2782 struct snd_ctl_elem_value *ucontrol)
2784 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2785 hda_nid_t nid = kcontrol->private_value;
2786 unsigned int vref_caps = get_vref_caps(codec, nid);
2789 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2790 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2794 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2795 struct snd_ctl_elem_value *ucontrol)
2797 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2798 hda_nid_t nid = kcontrol->private_value;
2799 unsigned int vref_caps = get_vref_caps(codec, nid);
2800 unsigned int val, idx;
2802 val = snd_hda_codec_get_pin_target(codec, nid);
2803 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2804 if (idx == ucontrol->value.enumerated.item[0])
2807 val &= ~AC_PINCTL_VREFEN;
2808 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2809 snd_hda_set_pin_ctl_cache(codec, nid, val);
2813 static const struct snd_kcontrol_new in_jack_mode_enum = {
2814 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2815 .info = in_jack_mode_info,
2816 .get = in_jack_mode_get,
2817 .put = in_jack_mode_put,
2820 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2822 struct hda_gen_spec *spec = codec->spec;
2824 if (spec->add_jack_modes)
2825 nitems = hweight32(get_vref_caps(codec, pin));
2826 return nitems ? nitems : 1;
2829 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2831 struct hda_gen_spec *spec = codec->spec;
2832 struct snd_kcontrol_new *knew;
2833 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2834 unsigned int defcfg;
2836 if (pin == spec->hp_mic_pin)
2837 return 0; /* already done in create_out_jack_mode() */
2839 /* no jack mode for fixed pins */
2840 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2841 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2844 /* no multiple vref caps? */
2845 if (get_in_jack_num_items(codec, pin) <= 1)
2848 get_jack_mode_name(codec, pin, name, sizeof(name));
2849 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2852 knew->private_value = pin;
2857 * HP/mic shared jack mode
2859 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2860 struct snd_ctl_elem_info *uinfo)
2862 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2863 hda_nid_t nid = kcontrol->private_value;
2864 int out_jacks = get_out_jack_num_items(codec, nid);
2865 int in_jacks = get_in_jack_num_items(codec, nid);
2866 const char *text = NULL;
2869 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2871 uinfo->value.enumerated.items = out_jacks + in_jacks;
2872 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2873 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2874 idx = uinfo->value.enumerated.item;
2875 if (idx < out_jacks) {
2877 text = out_jack_texts[idx];
2879 text = "Headphone Out";
2883 unsigned int vref_caps = get_vref_caps(codec, nid);
2884 text = vref_texts[get_vref_idx(vref_caps, idx)];
2889 strcpy(uinfo->value.enumerated.name, text);
2893 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2895 int out_jacks = get_out_jack_num_items(codec, nid);
2896 int in_jacks = get_in_jack_num_items(codec, nid);
2897 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2900 if (val & PIN_OUT) {
2901 if (out_jacks > 1 && val == PIN_HP)
2903 } else if (val & PIN_IN) {
2906 unsigned int vref_caps = get_vref_caps(codec, nid);
2907 val &= AC_PINCTL_VREFEN;
2908 idx += cvt_from_vref_idx(vref_caps, val);
2914 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2915 struct snd_ctl_elem_value *ucontrol)
2917 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2918 hda_nid_t nid = kcontrol->private_value;
2919 ucontrol->value.enumerated.item[0] =
2920 get_cur_hp_mic_jack_mode(codec, nid);
2924 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2925 struct snd_ctl_elem_value *ucontrol)
2927 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2928 hda_nid_t nid = kcontrol->private_value;
2929 int out_jacks = get_out_jack_num_items(codec, nid);
2930 int in_jacks = get_in_jack_num_items(codec, nid);
2931 unsigned int val, oldval, idx;
2933 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2934 idx = ucontrol->value.enumerated.item[0];
2938 if (idx < out_jacks) {
2940 val = idx ? PIN_HP : PIN_OUT;
2946 unsigned int vref_caps = get_vref_caps(codec, nid);
2947 val = snd_hda_codec_get_pin_target(codec, nid);
2948 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2949 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
2951 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
2953 snd_hda_set_pin_ctl_cache(codec, nid, val);
2954 call_hp_automute(codec, NULL);
2959 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2960 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2961 .info = hp_mic_jack_mode_info,
2962 .get = hp_mic_jack_mode_get,
2963 .put = hp_mic_jack_mode_put,
2966 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2968 struct hda_gen_spec *spec = codec->spec;
2969 struct snd_kcontrol_new *knew;
2971 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2972 &hp_mic_jack_mode_enum);
2975 knew->private_value = pin;
2976 spec->hp_mic_jack_modes = 1;
2984 /* add the powersave loopback-list entry */
2985 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2987 struct hda_amp_list *list;
2989 list = snd_array_new(&spec->loopback_list);
2993 list->dir = HDA_INPUT;
2995 spec->loopback.amplist = spec->loopback_list.list;
2999 /* return true if either a volume or a mute amp is found for the given
3000 * aamix path; the amp has to be either in the mixer node or its direct leaf
3002 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3003 hda_nid_t pin, unsigned int *mix_val,
3004 unsigned int *mute_val)
3007 const hda_nid_t *list;
3010 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3014 *mix_val = *mute_val = 0;
3015 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3016 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3017 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3018 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3019 if (*mix_val && *mute_val)
3022 /* check leaf node */
3023 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3024 if (num_conns < idx)
3027 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3028 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
3029 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3030 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3031 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
3032 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3034 return *mix_val || *mute_val;
3037 /* create input playback/capture controls for the given pin */
3038 static int new_analog_input(struct hda_codec *codec, int input_idx,
3039 hda_nid_t pin, const char *ctlname, int ctlidx,
3042 struct hda_gen_spec *spec = codec->spec;
3043 struct nid_path *path;
3044 unsigned int mix_val, mute_val;
3047 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3050 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
3053 print_nid_path(codec, "loopback", path);
3054 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
3056 idx = path->idx[path->depth - 1];
3058 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
3061 path->ctls[NID_PATH_VOL_CTL] = mix_val;
3065 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
3068 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
3071 path->active = true;
3072 path->stream_enabled = true; /* no DAC/ADC involved */
3073 err = add_loopback_list(spec, mix_nid, idx);
3077 if (spec->mixer_nid != spec->mixer_merge_nid &&
3078 !spec->loopback_merge_path) {
3079 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3080 spec->mixer_merge_nid, 0);
3082 print_nid_path(codec, "loopback-merge", path);
3083 path->active = true;
3084 path->pin_fixed = true; /* static route */
3085 path->stream_enabled = true; /* no DAC/ADC involved */
3086 spec->loopback_merge_path =
3087 snd_hda_get_path_idx(codec, path);
3094 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3096 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3097 return (pincap & AC_PINCAP_IN) != 0;
3100 /* Parse the codec tree and retrieve ADCs */
3101 static int fill_adc_nids(struct hda_codec *codec)
3103 struct hda_gen_spec *spec = codec->spec;
3105 hda_nid_t *adc_nids = spec->adc_nids;
3106 int max_nums = ARRAY_SIZE(spec->adc_nids);
3109 for_each_hda_codec_node(nid, codec) {
3110 unsigned int caps = get_wcaps(codec, nid);
3111 int type = get_wcaps_type(caps);
3113 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3115 adc_nids[nums] = nid;
3116 if (++nums >= max_nums)
3119 spec->num_adc_nids = nums;
3121 /* copy the detected ADCs to all_adcs[] */
3122 spec->num_all_adcs = nums;
3123 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3128 /* filter out invalid adc_nids that don't give all active input pins;
3129 * if needed, check whether dynamic ADC-switching is available
3131 static int check_dyn_adc_switch(struct hda_codec *codec)
3133 struct hda_gen_spec *spec = codec->spec;
3134 struct hda_input_mux *imux = &spec->input_mux;
3135 unsigned int ok_bits;
3140 for (n = 0; n < spec->num_adc_nids; n++) {
3141 for (i = 0; i < imux->num_items; i++) {
3142 if (!spec->input_paths[i][n])
3145 if (i >= imux->num_items) {
3146 ok_bits |= (1 << n);
3152 /* check whether ADC-switch is possible */
3153 for (i = 0; i < imux->num_items; i++) {
3154 for (n = 0; n < spec->num_adc_nids; n++) {
3155 if (spec->input_paths[i][n]) {
3156 spec->dyn_adc_idx[i] = n;
3162 codec_dbg(codec, "enabling ADC switching\n");
3163 spec->dyn_adc_switch = 1;
3164 } else if (nums != spec->num_adc_nids) {
3165 /* shrink the invalid adcs and input paths */
3167 for (n = 0; n < spec->num_adc_nids; n++) {
3168 if (!(ok_bits & (1 << n)))
3171 spec->adc_nids[nums] = spec->adc_nids[n];
3172 for (i = 0; i < imux->num_items; i++) {
3173 invalidate_nid_path(codec,
3174 spec->input_paths[i][nums]);
3175 spec->input_paths[i][nums] =
3176 spec->input_paths[i][n];
3181 spec->num_adc_nids = nums;
3184 if (imux->num_items == 1 ||
3185 (imux->num_items == 2 && spec->hp_mic)) {
3186 codec_dbg(codec, "reducing to a single ADC\n");
3187 spec->num_adc_nids = 1; /* reduce to a single ADC */
3190 /* single index for individual volumes ctls */
3191 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3192 spec->num_adc_nids = 1;
3197 /* parse capture source paths from the given pin and create imux items */
3198 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3199 int cfg_idx, int num_adcs,
3200 const char *label, int anchor)
3202 struct hda_gen_spec *spec = codec->spec;
3203 struct hda_input_mux *imux = &spec->input_mux;
3204 int imux_idx = imux->num_items;
3205 bool imux_added = false;
3208 for (c = 0; c < num_adcs; c++) {
3209 struct nid_path *path;
3210 hda_nid_t adc = spec->adc_nids[c];
3212 if (!is_reachable_path(codec, pin, adc))
3214 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3217 print_nid_path(codec, "input", path);
3218 spec->input_paths[imux_idx][c] =
3219 snd_hda_get_path_idx(codec, path);
3222 if (spec->hp_mic_pin == pin)
3223 spec->hp_mic_mux_idx = imux->num_items;
3224 spec->imux_pins[imux->num_items] = pin;
3225 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3227 if (spec->dyn_adc_switch)
3228 spec->dyn_adc_idx[imux_idx] = c;
3236 * create playback/capture controls for input pins
3239 /* fill the label for each input at first */
3240 static int fill_input_pin_labels(struct hda_codec *codec)
3242 struct hda_gen_spec *spec = codec->spec;
3243 const struct auto_pin_cfg *cfg = &spec->autocfg;
3246 for (i = 0; i < cfg->num_inputs; i++) {
3247 hda_nid_t pin = cfg->inputs[i].pin;
3251 if (!is_input_pin(codec, pin))
3254 label = hda_get_autocfg_input_label(codec, cfg, i);
3256 for (j = i - 1; j >= 0; j--) {
3257 if (spec->input_labels[j] &&
3258 !strcmp(spec->input_labels[j], label)) {
3259 idx = spec->input_label_idxs[j] + 1;
3264 spec->input_labels[i] = label;
3265 spec->input_label_idxs[i] = idx;
3271 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3273 static int create_input_ctls(struct hda_codec *codec)
3275 struct hda_gen_spec *spec = codec->spec;
3276 const struct auto_pin_cfg *cfg = &spec->autocfg;
3277 hda_nid_t mixer = spec->mixer_nid;
3282 num_adcs = fill_adc_nids(codec);
3286 err = fill_input_pin_labels(codec);
3290 for (i = 0; i < cfg->num_inputs; i++) {
3293 pin = cfg->inputs[i].pin;
3294 if (!is_input_pin(codec, pin))
3298 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3299 val |= snd_hda_get_default_vref(codec, pin);
3300 if (pin != spec->hp_mic_pin &&
3301 !snd_hda_codec_get_pin_target(codec, pin))
3302 set_pin_target(codec, pin, val, false);
3305 if (is_reachable_path(codec, pin, mixer)) {
3306 err = new_analog_input(codec, i, pin,
3307 spec->input_labels[i],
3308 spec->input_label_idxs[i],
3315 err = parse_capture_source(codec, pin, i, num_adcs,
3316 spec->input_labels[i], -mixer);
3320 if (spec->add_jack_modes) {
3321 err = create_in_jack_mode(codec, pin);
3327 /* add stereo mix when explicitly enabled via hint */
3328 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3329 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3334 spec->suppress_auto_mic = 1;
3345 /* get the input path specified by the given adc and imux indices */
3346 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3348 struct hda_gen_spec *spec = codec->spec;
3349 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3353 if (spec->dyn_adc_switch)
3354 adc_idx = spec->dyn_adc_idx[imux_idx];
3355 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3359 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3362 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3365 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3366 struct snd_ctl_elem_info *uinfo)
3368 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3369 struct hda_gen_spec *spec = codec->spec;
3370 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3373 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3374 struct snd_ctl_elem_value *ucontrol)
3376 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3377 struct hda_gen_spec *spec = codec->spec;
3378 /* the ctls are created at once with multiple counts */
3379 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3381 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3385 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3386 struct snd_ctl_elem_value *ucontrol)
3388 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3389 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3390 return mux_select(codec, adc_idx,
3391 ucontrol->value.enumerated.item[0]);
3394 static const struct snd_kcontrol_new cap_src_temp = {
3395 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3396 .name = "Input Source",
3397 .info = mux_enum_info,
3398 .get = mux_enum_get,
3399 .put = mux_enum_put,
3403 * capture volume and capture switch ctls
3406 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3407 struct snd_ctl_elem_value *ucontrol);
3409 /* call the given amp update function for all amps in the imux list at once */
3410 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3411 struct snd_ctl_elem_value *ucontrol,
3412 put_call_t func, int type)
3414 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3415 struct hda_gen_spec *spec = codec->spec;
3416 const struct hda_input_mux *imux;
3417 struct nid_path *path;
3418 int i, adc_idx, err = 0;
3420 imux = &spec->input_mux;
3421 adc_idx = kcontrol->id.index;
3422 mutex_lock(&codec->control_mutex);
3423 for (i = 0; i < imux->num_items; i++) {
3424 path = get_input_path(codec, adc_idx, i);
3425 if (!path || !path->ctls[type])
3427 kcontrol->private_value = path->ctls[type];
3428 err = func(kcontrol, ucontrol);
3432 mutex_unlock(&codec->control_mutex);
3433 if (err >= 0 && spec->cap_sync_hook)
3434 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3438 /* capture volume ctl callbacks */
3439 #define cap_vol_info snd_hda_mixer_amp_volume_info
3440 #define cap_vol_get snd_hda_mixer_amp_volume_get
3441 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3443 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3444 struct snd_ctl_elem_value *ucontrol)
3446 return cap_put_caller(kcontrol, ucontrol,
3447 snd_hda_mixer_amp_volume_put,
3451 static const struct snd_kcontrol_new cap_vol_temp = {
3452 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3453 .name = "Capture Volume",
3454 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3455 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3456 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3457 .info = cap_vol_info,
3460 .tlv = { .c = cap_vol_tlv },
3463 /* capture switch ctl callbacks */
3464 #define cap_sw_info snd_ctl_boolean_stereo_info
3465 #define cap_sw_get snd_hda_mixer_amp_switch_get
3467 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3468 struct snd_ctl_elem_value *ucontrol)
3470 return cap_put_caller(kcontrol, ucontrol,
3471 snd_hda_mixer_amp_switch_put,
3475 static const struct snd_kcontrol_new cap_sw_temp = {
3476 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3477 .name = "Capture Switch",
3478 .info = cap_sw_info,
3483 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3488 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3489 for (depth = 0; depth < 3; depth++) {
3490 if (depth >= path->depth)
3492 i = path->depth - depth - 1;
3493 nid = path->path[i];
3494 if (!path->ctls[NID_PATH_VOL_CTL]) {
3495 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3496 path->ctls[NID_PATH_VOL_CTL] =
3497 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3498 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3499 int idx = path->idx[i];
3500 if (!depth && codec->single_adc_amp)
3502 path->ctls[NID_PATH_VOL_CTL] =
3503 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3506 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3507 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3508 path->ctls[NID_PATH_MUTE_CTL] =
3509 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3510 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3511 int idx = path->idx[i];
3512 if (!depth && codec->single_adc_amp)
3514 path->ctls[NID_PATH_MUTE_CTL] =
3515 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3522 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3524 struct hda_gen_spec *spec = codec->spec;
3525 struct auto_pin_cfg *cfg = &spec->autocfg;
3529 if (!spec->inv_dmic_split)
3531 for (i = 0; i < cfg->num_inputs; i++) {
3532 if (cfg->inputs[i].pin != nid)
3534 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3536 val = snd_hda_codec_get_pincfg(codec, nid);
3537 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3542 /* capture switch put callback for a single control with hook call */
3543 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3544 struct snd_ctl_elem_value *ucontrol)
3546 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3547 struct hda_gen_spec *spec = codec->spec;
3550 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3554 if (spec->cap_sync_hook)
3555 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3560 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3561 int idx, bool is_switch, unsigned int ctl,
3564 struct hda_gen_spec *spec = codec->spec;
3565 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3566 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3567 const char *sfx = is_switch ? "Switch" : "Volume";
3568 unsigned int chs = inv_dmic ? 1 : 3;
3569 struct snd_kcontrol_new *knew;
3575 snprintf(tmpname, sizeof(tmpname),
3576 "%s Capture %s", label, sfx);
3578 snprintf(tmpname, sizeof(tmpname),
3580 knew = add_control(spec, type, tmpname, idx,
3581 amp_val_replace_channels(ctl, chs));
3585 knew->put = cap_single_sw_put;
3589 /* Make independent right kcontrol */
3591 snprintf(tmpname, sizeof(tmpname),
3592 "Inverted %s Capture %s", label, sfx);
3594 snprintf(tmpname, sizeof(tmpname),
3595 "Inverted Capture %s", sfx);
3596 knew = add_control(spec, type, tmpname, idx,
3597 amp_val_replace_channels(ctl, 2));
3601 knew->put = cap_single_sw_put;
3605 /* create single (and simple) capture volume and switch controls */
3606 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3607 unsigned int vol_ctl, unsigned int sw_ctl,
3611 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3614 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3620 /* create bound capture volume and switch controls */
3621 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3622 unsigned int vol_ctl, unsigned int sw_ctl)
3624 struct hda_gen_spec *spec = codec->spec;
3625 struct snd_kcontrol_new *knew;
3628 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3632 knew->private_value = vol_ctl;
3633 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3636 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3640 knew->private_value = sw_ctl;
3641 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3646 /* return the vol ctl when used first in the imux list */
3647 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3649 struct nid_path *path;
3653 path = get_input_path(codec, 0, idx);
3656 ctl = path->ctls[type];
3659 for (i = 0; i < idx - 1; i++) {
3660 path = get_input_path(codec, 0, i);
3661 if (path && path->ctls[type] == ctl)
3667 /* create individual capture volume and switch controls per input */
3668 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3670 struct hda_gen_spec *spec = codec->spec;
3671 struct hda_input_mux *imux = &spec->input_mux;
3674 for (i = 0; i < imux->num_items; i++) {
3678 idx = imux->items[i].index;
3679 if (idx >= spec->autocfg.num_inputs)
3681 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3683 for (type = 0; type < 2; type++) {
3684 err = add_single_cap_ctl(codec,
3685 spec->input_labels[idx],
3686 spec->input_label_idxs[idx],
3688 get_first_cap_ctl(codec, i, type),
3697 static int create_capture_mixers(struct hda_codec *codec)
3699 struct hda_gen_spec *spec = codec->spec;
3700 struct hda_input_mux *imux = &spec->input_mux;
3701 int i, n, nums, err;
3703 if (spec->dyn_adc_switch)
3706 nums = spec->num_adc_nids;
3708 if (!spec->auto_mic && imux->num_items > 1) {
3709 struct snd_kcontrol_new *knew;
3711 name = nums > 1 ? "Input Source" : "Capture Source";
3712 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3718 for (n = 0; n < nums; n++) {
3720 bool multi_cap_vol = spec->multi_cap_vol;
3721 bool inv_dmic = false;
3725 for (i = 0; i < imux->num_items; i++) {
3726 struct nid_path *path;
3727 path = get_input_path(codec, n, i);
3730 parse_capvol_in_path(codec, path);
3732 vol = path->ctls[NID_PATH_VOL_CTL];
3733 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3735 if (!same_amp_caps(codec, vol,
3736 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3737 multi_cap_vol = true;
3740 sw = path->ctls[NID_PATH_MUTE_CTL];
3741 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3743 if (!same_amp_caps(codec, sw,
3744 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3745 multi_cap_vol = true;
3747 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3752 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3754 else if (!multi_cap_vol && !inv_dmic)
3755 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3757 err = create_multi_cap_vol_ctl(codec);
3766 * add mic boosts if needed
3769 /* check whether the given amp is feasible as a boost volume */
3770 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3775 if (!nid_has_volume(codec, nid, dir) ||
3776 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3777 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3780 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3781 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3787 /* look for a boost amp in a widget close to the pin */
3788 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3789 struct nid_path *path)
3791 unsigned int val = 0;
3795 for (depth = 0; depth < 3; depth++) {
3796 if (depth >= path->depth - 1)
3798 nid = path->path[depth];
3799 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3800 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3802 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3803 path->idx[depth])) {
3804 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3813 static int parse_mic_boost(struct hda_codec *codec)
3815 struct hda_gen_spec *spec = codec->spec;
3816 struct auto_pin_cfg *cfg = &spec->autocfg;
3817 struct hda_input_mux *imux = &spec->input_mux;
3820 if (!spec->num_adc_nids)
3823 for (i = 0; i < imux->num_items; i++) {
3824 struct nid_path *path;
3827 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3829 idx = imux->items[i].index;
3830 if (idx >= imux->num_items)
3833 /* check only line-in and mic pins */
3834 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3837 path = get_input_path(codec, 0, i);
3841 val = look_for_boost_amp(codec, path);
3845 /* create a boost control */
3846 snprintf(boost_label, sizeof(boost_label),
3847 "%s Boost Volume", spec->input_labels[idx]);
3848 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3849 spec->input_label_idxs[idx], val))
3852 path->ctls[NID_PATH_BOOST_CTL] = val;
3858 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3860 static void parse_digital(struct hda_codec *codec)
3862 struct hda_gen_spec *spec = codec->spec;
3863 struct nid_path *path;
3865 hda_nid_t dig_nid, pin;
3867 /* support multiple SPDIFs; the secondary is set up as a slave */
3869 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3870 pin = spec->autocfg.dig_out_pins[i];
3871 dig_nid = look_for_dac(codec, pin, true);
3874 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3877 print_nid_path(codec, "digout", path);
3878 path->active = true;
3879 path->pin_fixed = true; /* no jack detection */
3880 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3881 set_pin_target(codec, pin, PIN_OUT, false);
3883 spec->multiout.dig_out_nid = dig_nid;
3884 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3886 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3887 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3889 spec->slave_dig_outs[nums - 1] = dig_nid;
3894 if (spec->autocfg.dig_in_pin) {
3895 pin = spec->autocfg.dig_in_pin;
3896 for_each_hda_codec_node(dig_nid, codec) {
3897 unsigned int wcaps = get_wcaps(codec, dig_nid);
3898 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3900 if (!(wcaps & AC_WCAP_DIGITAL))
3902 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3904 print_nid_path(codec, "digin", path);
3905 path->active = true;
3906 path->pin_fixed = true; /* no jack */
3907 spec->dig_in_nid = dig_nid;
3908 spec->digin_path = snd_hda_get_path_idx(codec, path);
3909 set_pin_target(codec, pin, PIN_IN, false);
3918 * input MUX handling
3921 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3923 /* select the given imux item; either unmute exclusively or select the route */
3924 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3927 struct hda_gen_spec *spec = codec->spec;
3928 const struct hda_input_mux *imux;
3929 struct nid_path *old_path, *path;
3931 imux = &spec->input_mux;
3932 if (!imux->num_items)
3935 if (idx >= imux->num_items)
3936 idx = imux->num_items - 1;
3937 if (spec->cur_mux[adc_idx] == idx)
3940 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3943 if (old_path->active)
3944 snd_hda_activate_path(codec, old_path, false, false);
3946 spec->cur_mux[adc_idx] = idx;
3949 update_hp_mic(codec, adc_idx, false);
3951 if (spec->dyn_adc_switch)
3952 dyn_adc_pcm_resetup(codec, idx);
3954 path = get_input_path(codec, adc_idx, idx);
3959 snd_hda_activate_path(codec, path, true, false);
3960 if (spec->cap_sync_hook)
3961 spec->cap_sync_hook(codec, NULL, NULL);
3962 path_power_down_sync(codec, old_path);
3966 /* power up/down widgets in the all paths that match with the given NID
3967 * as terminals (either start- or endpoint)
3969 * returns the last changed NID, or zero if unchanged.
3971 static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
3972 int pin_state, int stream_state)
3974 struct hda_gen_spec *spec = codec->spec;
3975 hda_nid_t last, changed = 0;
3976 struct nid_path *path;
3979 for (n = 0; n < spec->paths.used; n++) {
3980 path = snd_array_elem(&spec->paths, n);
3983 if (path->path[0] == nid ||
3984 path->path[path->depth - 1] == nid) {
3985 bool pin_old = path->pin_enabled;
3986 bool stream_old = path->stream_enabled;
3989 path->pin_enabled = pin_state;
3990 if (stream_state >= 0)
3991 path->stream_enabled = stream_state;
3992 if ((!path->pin_fixed && path->pin_enabled != pin_old)
3993 || path->stream_enabled != stream_old) {
3994 last = path_power_update(codec, path, true);
4003 /* check the jack status for power control */
4004 static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4006 if (!is_jack_detectable(codec, pin))
4008 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4011 /* power up/down the paths of the given pin according to the jack state;
4012 * power = 0/1 : only power up/down if it matches with the jack state,
4013 * < 0 : force power up/down to follow the jack sate
4015 * returns the last changed NID, or zero if unchanged.
4017 static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4022 if (!codec->power_save_node)
4025 on = detect_pin_state(codec, pin);
4027 if (power >= 0 && on != power)
4029 return set_path_power(codec, pin, on, -1);
4032 static void pin_power_callback(struct hda_codec *codec,
4033 struct hda_jack_callback *jack,
4036 if (jack && jack->nid)
4037 sync_power_state_change(codec,
4038 set_pin_power_jack(codec, jack->nid, on));
4041 /* callback only doing power up -- called at first */
4042 static void pin_power_up_callback(struct hda_codec *codec,
4043 struct hda_jack_callback *jack)
4045 pin_power_callback(codec, jack, true);
4048 /* callback only doing power down -- called at last */
4049 static void pin_power_down_callback(struct hda_codec *codec,
4050 struct hda_jack_callback *jack)
4052 pin_power_callback(codec, jack, false);
4055 /* set up the power up/down callbacks */
4056 static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4057 const hda_nid_t *pins, bool on)
4060 hda_jack_callback_fn cb =
4061 on ? pin_power_up_callback : pin_power_down_callback;
4063 for (i = 0; i < num_pins && pins[i]; i++) {
4064 if (is_jack_detectable(codec, pins[i]))
4065 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4067 set_path_power(codec, pins[i], true, -1);
4071 /* enabled power callback to each available I/O pin with jack detections;
4072 * the digital I/O pins are excluded because of the unreliable detectsion
4074 static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4076 struct hda_gen_spec *spec = codec->spec;
4077 struct auto_pin_cfg *cfg = &spec->autocfg;
4080 if (!codec->power_save_node)
4082 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4083 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4084 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4085 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4086 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4087 for (i = 0; i < cfg->num_inputs; i++)
4088 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4091 /* sync path power up/down with the jack states of given pins */
4092 static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4093 const hda_nid_t *pins)
4097 for (i = 0; i < num_pins && pins[i]; i++)
4098 if (is_jack_detectable(codec, pins[i]))
4099 set_pin_power_jack(codec, pins[i], -1);
4102 /* sync path power up/down with pins; called at init and resume */
4103 static void sync_all_pin_power_ctls(struct hda_codec *codec)
4105 struct hda_gen_spec *spec = codec->spec;
4106 struct auto_pin_cfg *cfg = &spec->autocfg;
4109 if (!codec->power_save_node)
4111 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4112 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4113 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4114 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4115 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4116 for (i = 0; i < cfg->num_inputs; i++)
4117 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4120 /* add fake paths if not present yet */
4121 static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4122 int num_pins, const hda_nid_t *pins)
4124 struct hda_gen_spec *spec = codec->spec;
4125 struct nid_path *path;
4128 for (i = 0; i < num_pins; i++) {
4131 if (get_nid_path(codec, nid, pins[i], 0))
4133 path = snd_array_new(&spec->paths);
4136 memset(path, 0, sizeof(*path));
4138 path->path[0] = nid;
4139 path->path[1] = pins[i];
4140 path->active = true;
4145 /* create fake paths to all outputs from beep */
4146 static int add_fake_beep_paths(struct hda_codec *codec)
4148 struct hda_gen_spec *spec = codec->spec;
4149 struct auto_pin_cfg *cfg = &spec->autocfg;
4150 hda_nid_t nid = spec->beep_nid;
4153 if (!codec->power_save_node || !nid)
4155 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4158 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4159 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4163 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4164 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4172 /* power up/down beep widget and its output paths */
4173 static void beep_power_hook(struct hda_beep *beep, bool on)
4175 set_path_power(beep->codec, beep->nid, -1, on);
4179 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4180 * @codec: the HDA codec
4181 * @pin: NID of pin to fix
4183 int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4185 struct hda_gen_spec *spec = codec->spec;
4186 struct nid_path *path;
4188 path = snd_array_new(&spec->paths);
4191 memset(path, 0, sizeof(*path));
4193 path->path[0] = pin;
4194 path->active = true;
4195 path->pin_fixed = true;
4196 path->stream_enabled = true;
4199 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4202 * Jack detections for HP auto-mute and mic-switch
4205 /* check each pin in the given array; returns true if any of them is plugged */
4206 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4209 bool present = false;
4211 for (i = 0; i < num_pins; i++) {
4212 hda_nid_t nid = pins[i];
4215 /* don't detect pins retasked as inputs */
4216 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4218 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4224 /* standard HP/line-out auto-mute helper */
4225 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
4226 int *paths, bool mute)
4228 struct hda_gen_spec *spec = codec->spec;
4231 for (i = 0; i < num_pins; i++) {
4232 hda_nid_t nid = pins[i];
4233 unsigned int val, oldval;
4237 oldval = snd_hda_codec_get_pin_target(codec, nid);
4238 if (oldval & PIN_IN)
4239 continue; /* no mute for inputs */
4241 if (spec->auto_mute_via_amp) {
4242 struct nid_path *path;
4245 path = snd_hda_get_path_from_idx(codec, paths[i]);
4248 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4252 spec->mute_bits |= (1ULL << mute_nid);
4254 spec->mute_bits &= ~(1ULL << mute_nid);
4257 /* don't reset VREF value in case it's controlling
4258 * the amp (see alc861_fixup_asus_amp_vref_0f())
4260 if (spec->keep_vref_in_automute)
4261 val = oldval & ~PIN_HP;
4266 /* here we call update_pin_ctl() so that the pinctl is
4267 * changed without changing the pinctl target value;
4268 * the original target value will be still referred at
4269 * the init / resume again
4271 update_pin_ctl(codec, nid, val);
4274 set_pin_eapd(codec, nid, !mute);
4275 if (codec->power_save_node) {
4278 on = detect_pin_state(codec, nid);
4279 set_path_power(codec, nid, on, -1);
4285 * snd_hda_gen_update_outputs - Toggle outputs muting
4286 * @codec: the HDA codec
4288 * Update the mute status of all outputs based on the current jack states.
4290 void snd_hda_gen_update_outputs(struct hda_codec *codec)
4292 struct hda_gen_spec *spec = codec->spec;
4296 /* Control HP pins/amps depending on master_mute state;
4297 * in general, HP pins/amps control should be enabled in all cases,
4298 * but currently set only for master_mute, just to be safe
4300 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4301 paths = spec->out_paths;
4303 paths = spec->hp_paths;
4304 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
4305 spec->autocfg.hp_pins, paths, spec->master_mute);
4307 if (!spec->automute_speaker)
4310 on = spec->hp_jack_present | spec->line_jack_present;
4311 on |= spec->master_mute;
4312 spec->speaker_muted = on;
4313 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4314 paths = spec->out_paths;
4316 paths = spec->speaker_paths;
4317 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
4318 spec->autocfg.speaker_pins, paths, on);
4320 /* toggle line-out mutes if needed, too */
4321 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4322 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4323 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4325 if (!spec->automute_lo)
4328 on = spec->hp_jack_present;
4329 on |= spec->master_mute;
4330 spec->line_out_muted = on;
4331 paths = spec->out_paths;
4332 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4333 spec->autocfg.line_out_pins, paths, on);
4335 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4337 static void call_update_outputs(struct hda_codec *codec)
4339 struct hda_gen_spec *spec = codec->spec;
4340 if (spec->automute_hook)
4341 spec->automute_hook(codec);
4343 snd_hda_gen_update_outputs(codec);
4345 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4346 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4347 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4351 * snd_hda_gen_hp_automute - standard HP-automute helper
4352 * @codec: the HDA codec
4353 * @jack: jack object, NULL for the whole
4355 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4356 struct hda_jack_callback *jack)
4358 struct hda_gen_spec *spec = codec->spec;
4359 hda_nid_t *pins = spec->autocfg.hp_pins;
4360 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4362 /* No detection for the first HP jack during indep-HP mode */
4363 if (spec->indep_hp_enabled) {
4368 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4369 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4371 call_update_outputs(codec);
4373 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4376 * snd_hda_gen_line_automute - standard line-out-automute helper
4377 * @codec: the HDA codec
4378 * @jack: jack object, NULL for the whole
4380 void snd_hda_gen_line_automute(struct hda_codec *codec,
4381 struct hda_jack_callback *jack)
4383 struct hda_gen_spec *spec = codec->spec;
4385 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4387 /* check LO jack only when it's different from HP */
4388 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4391 spec->line_jack_present =
4392 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4393 spec->autocfg.line_out_pins);
4394 if (!spec->automute_speaker || !spec->detect_lo)
4396 call_update_outputs(codec);
4398 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4401 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4402 * @codec: the HDA codec
4403 * @jack: jack object, NULL for the whole
4405 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4406 struct hda_jack_callback *jack)
4408 struct hda_gen_spec *spec = codec->spec;
4411 if (!spec->auto_mic)
4414 for (i = spec->am_num_entries - 1; i > 0; i--) {
4415 hda_nid_t pin = spec->am_entry[i].pin;
4416 /* don't detect pins retasked as outputs */
4417 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4419 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4420 mux_select(codec, 0, spec->am_entry[i].idx);
4424 mux_select(codec, 0, spec->am_entry[0].idx);
4426 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4428 /* call appropriate hooks */
4429 static void call_hp_automute(struct hda_codec *codec,
4430 struct hda_jack_callback *jack)
4432 struct hda_gen_spec *spec = codec->spec;
4433 if (spec->hp_automute_hook)
4434 spec->hp_automute_hook(codec, jack);
4436 snd_hda_gen_hp_automute(codec, jack);
4439 static void call_line_automute(struct hda_codec *codec,
4440 struct hda_jack_callback *jack)
4442 struct hda_gen_spec *spec = codec->spec;
4443 if (spec->line_automute_hook)
4444 spec->line_automute_hook(codec, jack);
4446 snd_hda_gen_line_automute(codec, jack);
4449 static void call_mic_autoswitch(struct hda_codec *codec,
4450 struct hda_jack_callback *jack)
4452 struct hda_gen_spec *spec = codec->spec;
4453 if (spec->mic_autoswitch_hook)
4454 spec->mic_autoswitch_hook(codec, jack);
4456 snd_hda_gen_mic_autoswitch(codec, jack);
4459 /* update jack retasking */
4460 static void update_automute_all(struct hda_codec *codec)
4462 call_hp_automute(codec, NULL);
4463 call_line_automute(codec, NULL);
4464 call_mic_autoswitch(codec, NULL);
4468 * Auto-Mute mode mixer enum support
4470 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4471 struct snd_ctl_elem_info *uinfo)
4473 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4474 struct hda_gen_spec *spec = codec->spec;
4475 static const char * const texts3[] = {
4476 "Disabled", "Speaker Only", "Line Out+Speaker"
4479 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4480 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4481 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4484 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4485 struct snd_ctl_elem_value *ucontrol)
4487 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4488 struct hda_gen_spec *spec = codec->spec;
4489 unsigned int val = 0;
4490 if (spec->automute_speaker)
4492 if (spec->automute_lo)
4495 ucontrol->value.enumerated.item[0] = val;
4499 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4500 struct snd_ctl_elem_value *ucontrol)
4502 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4503 struct hda_gen_spec *spec = codec->spec;
4505 switch (ucontrol->value.enumerated.item[0]) {
4507 if (!spec->automute_speaker && !spec->automute_lo)
4509 spec->automute_speaker = 0;
4510 spec->automute_lo = 0;
4513 if (spec->automute_speaker_possible) {
4514 if (!spec->automute_lo && spec->automute_speaker)
4516 spec->automute_speaker = 1;
4517 spec->automute_lo = 0;
4518 } else if (spec->automute_lo_possible) {
4519 if (spec->automute_lo)
4521 spec->automute_lo = 1;
4526 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4528 if (spec->automute_speaker && spec->automute_lo)
4530 spec->automute_speaker = 1;
4531 spec->automute_lo = 1;
4536 call_update_outputs(codec);
4540 static const struct snd_kcontrol_new automute_mode_enum = {
4541 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4542 .name = "Auto-Mute Mode",
4543 .info = automute_mode_info,
4544 .get = automute_mode_get,
4545 .put = automute_mode_put,
4548 static int add_automute_mode_enum(struct hda_codec *codec)
4550 struct hda_gen_spec *spec = codec->spec;
4552 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4558 * Check the availability of HP/line-out auto-mute;
4559 * Set up appropriately if really supported
4561 static int check_auto_mute_availability(struct hda_codec *codec)
4563 struct hda_gen_spec *spec = codec->spec;
4564 struct auto_pin_cfg *cfg = &spec->autocfg;
4568 if (spec->suppress_auto_mute)
4571 if (cfg->hp_pins[0])
4573 if (cfg->line_out_pins[0])
4575 if (cfg->speaker_pins[0])
4577 if (present < 2) /* need two different output types */
4580 if (!cfg->speaker_pins[0] &&
4581 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4582 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4583 sizeof(cfg->speaker_pins));
4584 cfg->speaker_outs = cfg->line_outs;
4587 if (!cfg->hp_pins[0] &&
4588 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4589 memcpy(cfg->hp_pins, cfg->line_out_pins,
4590 sizeof(cfg->hp_pins));
4591 cfg->hp_outs = cfg->line_outs;
4594 for (i = 0; i < cfg->hp_outs; i++) {
4595 hda_nid_t nid = cfg->hp_pins[i];
4596 if (!is_jack_detectable(codec, nid))
4598 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4599 snd_hda_jack_detect_enable_callback(codec, nid,
4601 spec->detect_hp = 1;
4604 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4605 if (cfg->speaker_outs)
4606 for (i = 0; i < cfg->line_outs; i++) {
4607 hda_nid_t nid = cfg->line_out_pins[i];
4608 if (!is_jack_detectable(codec, nid))
4610 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4611 snd_hda_jack_detect_enable_callback(codec, nid,
4612 call_line_automute);
4613 spec->detect_lo = 1;
4615 spec->automute_lo_possible = spec->detect_hp;
4618 spec->automute_speaker_possible = cfg->speaker_outs &&
4619 (spec->detect_hp || spec->detect_lo);
4621 spec->automute_lo = spec->automute_lo_possible;
4622 spec->automute_speaker = spec->automute_speaker_possible;
4624 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4625 /* create a control for automute mode */
4626 err = add_automute_mode_enum(codec);
4633 /* check whether all auto-mic pins are valid; setup indices if OK */
4634 static bool auto_mic_check_imux(struct hda_codec *codec)
4636 struct hda_gen_spec *spec = codec->spec;
4637 const struct hda_input_mux *imux;
4640 imux = &spec->input_mux;
4641 for (i = 0; i < spec->am_num_entries; i++) {
4642 spec->am_entry[i].idx =
4643 find_idx_in_nid_list(spec->am_entry[i].pin,
4644 spec->imux_pins, imux->num_items);
4645 if (spec->am_entry[i].idx < 0)
4646 return false; /* no corresponding imux */
4649 /* we don't need the jack detection for the first pin */
4650 for (i = 1; i < spec->am_num_entries; i++)
4651 snd_hda_jack_detect_enable_callback(codec,
4652 spec->am_entry[i].pin,
4653 call_mic_autoswitch);
4657 static int compare_attr(const void *ap, const void *bp)
4659 const struct automic_entry *a = ap;
4660 const struct automic_entry *b = bp;
4661 return (int)(a->attr - b->attr);
4665 * Check the availability of auto-mic switch;
4666 * Set up if really supported
4668 static int check_auto_mic_availability(struct hda_codec *codec)
4670 struct hda_gen_spec *spec = codec->spec;
4671 struct auto_pin_cfg *cfg = &spec->autocfg;
4675 if (spec->suppress_auto_mic)
4680 for (i = 0; i < cfg->num_inputs; i++) {
4681 hda_nid_t nid = cfg->inputs[i].pin;
4683 attr = snd_hda_codec_get_pincfg(codec, nid);
4684 attr = snd_hda_get_input_pin_attr(attr);
4685 if (types & (1 << attr))
4686 return 0; /* already occupied */
4688 case INPUT_PIN_ATTR_INT:
4689 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4690 return 0; /* invalid type */
4692 case INPUT_PIN_ATTR_UNUSED:
4693 return 0; /* invalid entry */
4695 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4696 return 0; /* invalid type */
4697 if (!spec->line_in_auto_switch &&
4698 cfg->inputs[i].type != AUTO_PIN_MIC)
4699 return 0; /* only mic is allowed */
4700 if (!is_jack_detectable(codec, nid))
4701 return 0; /* no unsol support */
4704 if (num_pins >= MAX_AUTO_MIC_PINS)
4706 types |= (1 << attr);
4707 spec->am_entry[num_pins].pin = nid;
4708 spec->am_entry[num_pins].attr = attr;
4715 spec->am_num_entries = num_pins;
4716 /* sort the am_entry in the order of attr so that the pin with a
4717 * higher attr will be selected when the jack is plugged.
4719 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4720 compare_attr, NULL);
4722 if (!auto_mic_check_imux(codec))
4726 spec->num_adc_nids = 1;
4727 spec->cur_mux[0] = spec->am_entry[0].idx;
4728 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4729 spec->am_entry[0].pin,
4730 spec->am_entry[1].pin,
4731 spec->am_entry[2].pin);
4737 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4739 * @codec: the HDA codec
4740 * @nid: NID to evalute
4741 * @power_state: target power state
4743 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4745 unsigned int power_state)
4747 struct hda_gen_spec *spec = codec->spec;
4749 if (!spec->power_down_unused && !codec->power_save_node)
4751 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
4753 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4755 if (is_active_nid_for_any(codec, nid))
4759 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4761 /* mute all aamix inputs initially; parse up to the first leaves */
4762 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4765 const hda_nid_t *conn;
4768 nums = snd_hda_get_conn_list(codec, mix, &conn);
4769 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4770 for (i = 0; i < nums; i++) {
4772 update_amp(codec, mix, HDA_INPUT, i,
4773 0xff, HDA_AMP_MUTE);
4774 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4775 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4776 0xff, HDA_AMP_MUTE);
4781 * snd_hda_gen_stream_pm - Stream power management callback
4782 * @codec: the HDA codec
4783 * @nid: audio widget
4784 * @on: power on/off flag
4786 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
4788 void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4790 if (codec->power_save_node)
4791 set_path_power(codec, nid, -1, on);
4793 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4796 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4797 * set up the hda_gen_spec
4798 * @codec: the HDA codec
4799 * @cfg: Parsed pin configuration
4801 * return 1 if successful, 0 if the proper config is not found,
4802 * or a negative error code
4804 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4805 struct auto_pin_cfg *cfg)
4807 struct hda_gen_spec *spec = codec->spec;
4810 parse_user_hints(codec);
4812 if (spec->mixer_nid && !spec->mixer_merge_nid)
4813 spec->mixer_merge_nid = spec->mixer_nid;
4815 if (cfg != &spec->autocfg) {
4816 spec->autocfg = *cfg;
4817 cfg = &spec->autocfg;
4820 if (!spec->main_out_badness)
4821 spec->main_out_badness = &hda_main_out_badness;
4822 if (!spec->extra_out_badness)
4823 spec->extra_out_badness = &hda_extra_out_badness;
4825 fill_all_dac_nids(codec);
4827 if (!cfg->line_outs) {
4828 if (cfg->dig_outs || cfg->dig_in_pin) {
4829 spec->multiout.max_channels = 2;
4830 spec->no_analog = 1;
4833 if (!cfg->num_inputs && !cfg->dig_in_pin)
4834 return 0; /* can't find valid BIOS pin config */
4837 if (!spec->no_primary_hp &&
4838 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4839 cfg->line_outs <= cfg->hp_outs) {
4840 /* use HP as primary out */
4841 cfg->speaker_outs = cfg->line_outs;
4842 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4843 sizeof(cfg->speaker_pins));
4844 cfg->line_outs = cfg->hp_outs;
4845 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4847 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4848 cfg->line_out_type = AUTO_PIN_HP_OUT;
4851 err = parse_output_paths(codec);
4854 err = create_multi_channel_mode(codec);
4857 err = create_multi_out_ctls(codec, cfg);
4860 err = create_hp_out_ctls(codec);
4863 err = create_speaker_out_ctls(codec);
4866 err = create_indep_hp_ctls(codec);
4869 err = create_loopback_mixing_ctl(codec);
4872 err = create_hp_mic(codec);
4875 err = create_input_ctls(codec);
4879 /* add power-down pin callbacks at first */
4880 add_all_pin_power_ctls(codec, false);
4882 spec->const_channel_count = spec->ext_channel_count;
4883 /* check the multiple speaker and headphone pins */
4884 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4885 spec->const_channel_count = max(spec->const_channel_count,
4886 cfg->speaker_outs * 2);
4887 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4888 spec->const_channel_count = max(spec->const_channel_count,
4890 spec->multiout.max_channels = max(spec->ext_channel_count,
4891 spec->const_channel_count);
4893 err = check_auto_mute_availability(codec);
4897 err = check_dyn_adc_switch(codec);
4901 err = check_auto_mic_availability(codec);
4905 /* add stereo mix if available and not enabled yet */
4906 if (!spec->auto_mic && spec->mixer_nid &&
4907 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4908 spec->input_mux.num_items > 1) {
4909 err = parse_capture_source(codec, spec->mixer_nid,
4910 CFG_IDX_MIX, spec->num_all_adcs,
4917 err = create_capture_mixers(codec);
4921 err = parse_mic_boost(codec);
4925 /* create "Headphone Mic Jack Mode" if no input selection is
4926 * available (or user specifies add_jack_modes hint)
4928 if (spec->hp_mic_pin &&
4929 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4930 spec->add_jack_modes)) {
4931 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4936 if (spec->add_jack_modes) {
4937 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4938 err = create_out_jack_modes(codec, cfg->line_outs,
4939 cfg->line_out_pins);
4943 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4944 err = create_out_jack_modes(codec, cfg->hp_outs,
4951 /* add power-up pin callbacks at last */
4952 add_all_pin_power_ctls(codec, true);
4954 /* mute all aamix input initially */
4955 if (spec->mixer_nid)
4956 mute_all_mixer_nid(codec, spec->mixer_nid);
4959 parse_digital(codec);
4961 if (spec->power_down_unused || codec->power_save_node) {
4962 if (!codec->power_filter)
4963 codec->power_filter = snd_hda_gen_path_power_filter;
4964 if (!codec->patch_ops.stream_pm)
4965 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
4968 if (!spec->no_analog && spec->beep_nid) {
4969 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4972 if (codec->beep && codec->power_save_node) {
4973 err = add_fake_beep_paths(codec);
4976 codec->beep->power_hook = beep_power_hook;
4982 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
4986 * Build control elements
4989 /* slave controls for virtual master */
4990 static const char * const slave_pfxs[] = {
4991 "Front", "Surround", "Center", "LFE", "Side",
4992 "Headphone", "Speaker", "Mono", "Line Out",
4993 "CLFE", "Bass Speaker", "PCM",
4994 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4995 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4996 "Headphone Side", "Headphone+LO", "Speaker+LO",
5001 * snd_hda_gen_build_controls - Build controls from the parsed results
5002 * @codec: the HDA codec
5004 * Pass this to build_controls patch_ops.
5006 int snd_hda_gen_build_controls(struct hda_codec *codec)
5008 struct hda_gen_spec *spec = codec->spec;
5011 if (spec->kctls.used) {
5012 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5017 if (spec->multiout.dig_out_nid) {
5018 err = snd_hda_create_dig_out_ctls(codec,
5019 spec->multiout.dig_out_nid,
5020 spec->multiout.dig_out_nid,
5021 spec->pcm_rec[1]->pcm_type);
5024 if (!spec->no_analog) {
5025 err = snd_hda_create_spdif_share_sw(codec,
5029 spec->multiout.share_spdif = 1;
5032 if (spec->dig_in_nid) {
5033 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5038 /* if we have no master control, let's create it */
5039 if (!spec->no_analog && !spec->suppress_vmaster &&
5040 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
5041 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
5042 spec->vmaster_tlv, slave_pfxs,
5047 if (!spec->no_analog && !spec->suppress_vmaster &&
5048 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5049 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5052 true, &spec->vmaster_mute.sw_kctl);
5055 if (spec->vmaster_mute.hook) {
5056 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5057 spec->vmaster_mute_enum);
5058 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5062 free_kctls(spec); /* no longer needed */
5064 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5070 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
5077 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5078 struct hda_codec *codec,
5079 struct snd_pcm_substream *substream,
5082 struct hda_gen_spec *spec = codec->spec;
5083 if (spec->pcm_playback_hook)
5084 spec->pcm_playback_hook(hinfo, codec, substream, action);
5087 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5088 struct hda_codec *codec,
5089 struct snd_pcm_substream *substream,
5092 struct hda_gen_spec *spec = codec->spec;
5093 if (spec->pcm_capture_hook)
5094 spec->pcm_capture_hook(hinfo, codec, substream, action);
5098 * Analog playback callbacks
5100 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5101 struct hda_codec *codec,
5102 struct snd_pcm_substream *substream)
5104 struct hda_gen_spec *spec = codec->spec;
5107 mutex_lock(&spec->pcm_mutex);
5108 err = snd_hda_multi_out_analog_open(codec,
5109 &spec->multiout, substream,
5112 spec->active_streams |= 1 << STREAM_MULTI_OUT;
5113 call_pcm_playback_hook(hinfo, codec, substream,
5114 HDA_GEN_PCM_ACT_OPEN);
5116 mutex_unlock(&spec->pcm_mutex);
5120 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5121 struct hda_codec *codec,
5122 unsigned int stream_tag,
5123 unsigned int format,
5124 struct snd_pcm_substream *substream)
5126 struct hda_gen_spec *spec = codec->spec;
5129 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5130 stream_tag, format, substream);
5132 call_pcm_playback_hook(hinfo, codec, substream,
5133 HDA_GEN_PCM_ACT_PREPARE);
5137 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5138 struct hda_codec *codec,
5139 struct snd_pcm_substream *substream)
5141 struct hda_gen_spec *spec = codec->spec;
5144 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5146 call_pcm_playback_hook(hinfo, codec, substream,
5147 HDA_GEN_PCM_ACT_CLEANUP);
5151 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5152 struct hda_codec *codec,
5153 struct snd_pcm_substream *substream)
5155 struct hda_gen_spec *spec = codec->spec;
5156 mutex_lock(&spec->pcm_mutex);
5157 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
5158 call_pcm_playback_hook(hinfo, codec, substream,
5159 HDA_GEN_PCM_ACT_CLOSE);
5160 mutex_unlock(&spec->pcm_mutex);
5164 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5165 struct hda_codec *codec,
5166 struct snd_pcm_substream *substream)
5168 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5172 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5173 struct hda_codec *codec,
5174 unsigned int stream_tag,
5175 unsigned int format,
5176 struct snd_pcm_substream *substream)
5178 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5179 call_pcm_capture_hook(hinfo, codec, substream,
5180 HDA_GEN_PCM_ACT_PREPARE);
5184 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5185 struct hda_codec *codec,
5186 struct snd_pcm_substream *substream)
5188 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5189 call_pcm_capture_hook(hinfo, codec, substream,
5190 HDA_GEN_PCM_ACT_CLEANUP);
5194 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5195 struct hda_codec *codec,
5196 struct snd_pcm_substream *substream)
5198 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5202 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5203 struct hda_codec *codec,
5204 struct snd_pcm_substream *substream)
5206 struct hda_gen_spec *spec = codec->spec;
5209 mutex_lock(&spec->pcm_mutex);
5210 if (spec->indep_hp && !spec->indep_hp_enabled)
5213 spec->active_streams |= 1 << STREAM_INDEP_HP;
5214 call_pcm_playback_hook(hinfo, codec, substream,
5215 HDA_GEN_PCM_ACT_OPEN);
5216 mutex_unlock(&spec->pcm_mutex);
5220 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5221 struct hda_codec *codec,
5222 struct snd_pcm_substream *substream)
5224 struct hda_gen_spec *spec = codec->spec;
5225 mutex_lock(&spec->pcm_mutex);
5226 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
5227 call_pcm_playback_hook(hinfo, codec, substream,
5228 HDA_GEN_PCM_ACT_CLOSE);
5229 mutex_unlock(&spec->pcm_mutex);
5233 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5234 struct hda_codec *codec,
5235 unsigned int stream_tag,
5236 unsigned int format,
5237 struct snd_pcm_substream *substream)
5239 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5240 call_pcm_playback_hook(hinfo, codec, substream,
5241 HDA_GEN_PCM_ACT_PREPARE);
5245 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5246 struct hda_codec *codec,
5247 struct snd_pcm_substream *substream)
5249 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5250 call_pcm_playback_hook(hinfo, codec, substream,
5251 HDA_GEN_PCM_ACT_CLEANUP);
5258 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5259 struct hda_codec *codec,
5260 struct snd_pcm_substream *substream)
5262 struct hda_gen_spec *spec = codec->spec;
5263 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5266 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5267 struct hda_codec *codec,
5268 unsigned int stream_tag,
5269 unsigned int format,
5270 struct snd_pcm_substream *substream)
5272 struct hda_gen_spec *spec = codec->spec;
5273 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5274 stream_tag, format, substream);
5277 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5278 struct hda_codec *codec,
5279 struct snd_pcm_substream *substream)
5281 struct hda_gen_spec *spec = codec->spec;
5282 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5285 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5286 struct hda_codec *codec,
5287 struct snd_pcm_substream *substream)
5289 struct hda_gen_spec *spec = codec->spec;
5290 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5296 #define alt_capture_pcm_open capture_pcm_open
5297 #define alt_capture_pcm_close capture_pcm_close
5299 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5300 struct hda_codec *codec,
5301 unsigned int stream_tag,
5302 unsigned int format,
5303 struct snd_pcm_substream *substream)
5305 struct hda_gen_spec *spec = codec->spec;
5307 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5308 stream_tag, 0, format);
5309 call_pcm_capture_hook(hinfo, codec, substream,
5310 HDA_GEN_PCM_ACT_PREPARE);
5314 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5315 struct hda_codec *codec,
5316 struct snd_pcm_substream *substream)
5318 struct hda_gen_spec *spec = codec->spec;
5320 snd_hda_codec_cleanup_stream(codec,
5321 spec->adc_nids[substream->number + 1]);
5322 call_pcm_capture_hook(hinfo, codec, substream,
5323 HDA_GEN_PCM_ACT_CLEANUP);
5329 static const struct hda_pcm_stream pcm_analog_playback = {
5333 /* NID is set in build_pcms */
5335 .open = playback_pcm_open,
5336 .close = playback_pcm_close,
5337 .prepare = playback_pcm_prepare,
5338 .cleanup = playback_pcm_cleanup
5342 static const struct hda_pcm_stream pcm_analog_capture = {
5346 /* NID is set in build_pcms */
5348 .open = capture_pcm_open,
5349 .close = capture_pcm_close,
5350 .prepare = capture_pcm_prepare,
5351 .cleanup = capture_pcm_cleanup
5355 static const struct hda_pcm_stream pcm_analog_alt_playback = {
5359 /* NID is set in build_pcms */
5361 .open = alt_playback_pcm_open,
5362 .close = alt_playback_pcm_close,
5363 .prepare = alt_playback_pcm_prepare,
5364 .cleanup = alt_playback_pcm_cleanup
5368 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5369 .substreams = 2, /* can be overridden */
5372 /* NID is set in build_pcms */
5374 .open = alt_capture_pcm_open,
5375 .close = alt_capture_pcm_close,
5376 .prepare = alt_capture_pcm_prepare,
5377 .cleanup = alt_capture_pcm_cleanup
5381 static const struct hda_pcm_stream pcm_digital_playback = {
5385 /* NID is set in build_pcms */
5387 .open = dig_playback_pcm_open,
5388 .close = dig_playback_pcm_close,
5389 .prepare = dig_playback_pcm_prepare,
5390 .cleanup = dig_playback_pcm_cleanup
5394 static const struct hda_pcm_stream pcm_digital_capture = {
5398 /* NID is set in build_pcms */
5401 /* Used by build_pcms to flag that a PCM has no playback stream */
5402 static const struct hda_pcm_stream pcm_null_stream = {
5409 * dynamic changing ADC PCM streams
5411 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5413 struct hda_gen_spec *spec = codec->spec;
5414 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5416 if (spec->cur_adc && spec->cur_adc != new_adc) {
5417 /* stream is running, let's swap the current ADC */
5418 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5419 spec->cur_adc = new_adc;
5420 snd_hda_codec_setup_stream(codec, new_adc,
5421 spec->cur_adc_stream_tag, 0,
5422 spec->cur_adc_format);
5428 /* analog capture with dynamic dual-adc changes */
5429 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5430 struct hda_codec *codec,
5431 unsigned int stream_tag,
5432 unsigned int format,
5433 struct snd_pcm_substream *substream)
5435 struct hda_gen_spec *spec = codec->spec;
5436 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5437 spec->cur_adc_stream_tag = stream_tag;
5438 spec->cur_adc_format = format;
5439 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5440 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
5444 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5445 struct hda_codec *codec,
5446 struct snd_pcm_substream *substream)
5448 struct hda_gen_spec *spec = codec->spec;
5449 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5451 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
5455 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5459 .nid = 0, /* fill later */
5461 .prepare = dyn_adc_capture_pcm_prepare,
5462 .cleanup = dyn_adc_capture_pcm_cleanup
5466 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5467 const char *chip_name)
5473 strlcpy(str, chip_name, len);
5475 /* drop non-alnum chars after a space */
5476 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5477 if (!isalnum(p[1])) {
5482 strlcat(str, sfx, len);
5485 /* copy PCM stream info from @default_str, and override non-NULL entries
5486 * from @spec_str and @nid
5488 static void setup_pcm_stream(struct hda_pcm_stream *str,
5489 const struct hda_pcm_stream *default_str,
5490 const struct hda_pcm_stream *spec_str,
5493 *str = *default_str;
5497 if (spec_str->substreams)
5498 str->substreams = spec_str->substreams;
5499 if (spec_str->channels_min)
5500 str->channels_min = spec_str->channels_min;
5501 if (spec_str->channels_max)
5502 str->channels_max = spec_str->channels_max;
5503 if (spec_str->rates)
5504 str->rates = spec_str->rates;
5505 if (spec_str->formats)
5506 str->formats = spec_str->formats;
5507 if (spec_str->maxbps)
5508 str->maxbps = spec_str->maxbps;
5513 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5514 * @codec: the HDA codec
5516 * Pass this to build_pcms patch_ops.
5518 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5520 struct hda_gen_spec *spec = codec->spec;
5521 struct hda_pcm *info;
5522 bool have_multi_adcs;
5524 if (spec->no_analog)
5527 fill_pcm_stream_name(spec->stream_name_analog,
5528 sizeof(spec->stream_name_analog),
5529 " Analog", codec->core.chip_name);
5530 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5533 spec->pcm_rec[0] = info;
5535 if (spec->multiout.num_dacs > 0) {
5536 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5537 &pcm_analog_playback,
5538 spec->stream_analog_playback,
5539 spec->multiout.dac_nids[0]);
5540 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5541 spec->multiout.max_channels;
5542 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5543 spec->autocfg.line_outs == 2)
5544 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5547 if (spec->num_adc_nids) {
5548 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5549 (spec->dyn_adc_switch ?
5550 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5551 spec->stream_analog_capture,
5556 /* SPDIF for stream index #1 */
5557 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5558 fill_pcm_stream_name(spec->stream_name_digital,
5559 sizeof(spec->stream_name_digital),
5560 " Digital", codec->core.chip_name);
5561 info = snd_hda_codec_pcm_new(codec, "%s",
5562 spec->stream_name_digital);
5565 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5566 spec->pcm_rec[1] = info;
5567 if (spec->dig_out_type)
5568 info->pcm_type = spec->dig_out_type;
5570 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5571 if (spec->multiout.dig_out_nid)
5572 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5573 &pcm_digital_playback,
5574 spec->stream_digital_playback,
5575 spec->multiout.dig_out_nid);
5576 if (spec->dig_in_nid)
5577 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5578 &pcm_digital_capture,
5579 spec->stream_digital_capture,
5583 if (spec->no_analog)
5586 /* If the use of more than one ADC is requested for the current
5587 * model, configure a second analog capture-only PCM.
5589 have_multi_adcs = (spec->num_adc_nids > 1) &&
5590 !spec->dyn_adc_switch && !spec->auto_mic;
5591 /* Additional Analaog capture for index #2 */
5592 if (spec->alt_dac_nid || have_multi_adcs) {
5593 fill_pcm_stream_name(spec->stream_name_alt_analog,
5594 sizeof(spec->stream_name_alt_analog),
5595 " Alt Analog", codec->core.chip_name);
5596 info = snd_hda_codec_pcm_new(codec, "%s",
5597 spec->stream_name_alt_analog);
5600 spec->pcm_rec[2] = info;
5601 if (spec->alt_dac_nid)
5602 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5603 &pcm_analog_alt_playback,
5604 spec->stream_analog_alt_playback,
5607 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5608 &pcm_null_stream, NULL, 0);
5609 if (have_multi_adcs) {
5610 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5611 &pcm_analog_alt_capture,
5612 spec->stream_analog_alt_capture,
5614 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5615 spec->num_adc_nids - 1;
5617 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5618 &pcm_null_stream, NULL, 0);
5624 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5628 * Standard auto-parser initializations
5631 /* configure the given path as a proper output */
5632 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5634 struct nid_path *path;
5637 path = snd_hda_get_path_from_idx(codec, path_idx);
5638 if (!path || !path->depth)
5640 pin = path->path[path->depth - 1];
5641 restore_pin_ctl(codec, pin);
5642 snd_hda_activate_path(codec, path, path->active,
5643 aamix_default(codec->spec));
5644 set_pin_eapd(codec, pin, path->active);
5647 /* initialize primary output paths */
5648 static void init_multi_out(struct hda_codec *codec)
5650 struct hda_gen_spec *spec = codec->spec;
5653 for (i = 0; i < spec->autocfg.line_outs; i++)
5654 set_output_and_unmute(codec, spec->out_paths[i]);
5658 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5662 for (i = 0; i < num_outs; i++)
5663 set_output_and_unmute(codec, paths[i]);
5666 /* initialize hp and speaker paths */
5667 static void init_extra_out(struct hda_codec *codec)
5669 struct hda_gen_spec *spec = codec->spec;
5671 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5672 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5673 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5674 __init_extra_out(codec, spec->autocfg.speaker_outs,
5675 spec->speaker_paths);
5678 /* initialize multi-io paths */
5679 static void init_multi_io(struct hda_codec *codec)
5681 struct hda_gen_spec *spec = codec->spec;
5684 for (i = 0; i < spec->multi_ios; i++) {
5685 hda_nid_t pin = spec->multi_io[i].pin;
5686 struct nid_path *path;
5687 path = get_multiio_path(codec, i);
5690 if (!spec->multi_io[i].ctl_in)
5691 spec->multi_io[i].ctl_in =
5692 snd_hda_codec_get_pin_target(codec, pin);
5693 snd_hda_activate_path(codec, path, path->active,
5694 aamix_default(spec));
5698 static void init_aamix_paths(struct hda_codec *codec)
5700 struct hda_gen_spec *spec = codec->spec;
5702 if (!spec->have_aamix_ctl)
5704 if (!has_aamix_out_paths(spec))
5706 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5707 spec->aamix_out_paths[0],
5708 spec->autocfg.line_out_type);
5709 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5710 spec->aamix_out_paths[1],
5712 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5713 spec->aamix_out_paths[2],
5714 AUTO_PIN_SPEAKER_OUT);
5717 /* set up input pins and loopback paths */
5718 static void init_analog_input(struct hda_codec *codec)
5720 struct hda_gen_spec *spec = codec->spec;
5721 struct auto_pin_cfg *cfg = &spec->autocfg;
5724 for (i = 0; i < cfg->num_inputs; i++) {
5725 hda_nid_t nid = cfg->inputs[i].pin;
5726 if (is_input_pin(codec, nid))
5727 restore_pin_ctl(codec, nid);
5729 /* init loopback inputs */
5730 if (spec->mixer_nid) {
5731 resume_path_from_idx(codec, spec->loopback_paths[i]);
5732 resume_path_from_idx(codec, spec->loopback_merge_path);
5737 /* initialize ADC paths */
5738 static void init_input_src(struct hda_codec *codec)
5740 struct hda_gen_spec *spec = codec->spec;
5741 struct hda_input_mux *imux = &spec->input_mux;
5742 struct nid_path *path;
5745 if (spec->dyn_adc_switch)
5748 nums = spec->num_adc_nids;
5750 for (c = 0; c < nums; c++) {
5751 for (i = 0; i < imux->num_items; i++) {
5752 path = get_input_path(codec, c, i);
5754 bool active = path->active;
5755 if (i == spec->cur_mux[c])
5757 snd_hda_activate_path(codec, path, active, false);
5761 update_hp_mic(codec, c, true);
5764 if (spec->cap_sync_hook)
5765 spec->cap_sync_hook(codec, NULL, NULL);
5768 /* set right pin controls for digital I/O */
5769 static void init_digital(struct hda_codec *codec)
5771 struct hda_gen_spec *spec = codec->spec;
5775 for (i = 0; i < spec->autocfg.dig_outs; i++)
5776 set_output_and_unmute(codec, spec->digout_paths[i]);
5777 pin = spec->autocfg.dig_in_pin;
5779 restore_pin_ctl(codec, pin);
5780 resume_path_from_idx(codec, spec->digin_path);
5784 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5785 * invalid unsol tags by some reason
5787 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5791 for (i = 0; i < codec->init_pins.used; i++) {
5792 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5793 hda_nid_t nid = pin->nid;
5794 if (is_jack_detectable(codec, nid) &&
5795 !snd_hda_jack_tbl_get(codec, nid))
5796 snd_hda_codec_update_cache(codec, nid, 0,
5797 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5802 * snd_hda_gen_init - initialize the generic spec
5803 * @codec: the HDA codec
5805 * This can be put as patch_ops init function.
5807 int snd_hda_gen_init(struct hda_codec *codec)
5809 struct hda_gen_spec *spec = codec->spec;
5811 if (spec->init_hook)
5812 spec->init_hook(codec);
5814 snd_hda_apply_verbs(codec);
5816 init_multi_out(codec);
5817 init_extra_out(codec);
5818 init_multi_io(codec);
5819 init_aamix_paths(codec);
5820 init_analog_input(codec);
5821 init_input_src(codec);
5822 init_digital(codec);
5824 clear_unsol_on_unused_pins(codec);
5826 sync_all_pin_power_ctls(codec);
5828 /* call init functions of standard auto-mute helpers */
5829 update_automute_all(codec);
5831 regcache_sync(codec->core.regmap);
5833 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5834 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5836 hda_call_check_power_status(codec, 0x01);
5839 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
5842 * snd_hda_gen_free - free the generic spec
5843 * @codec: the HDA codec
5845 * This can be put as patch_ops free function.
5847 void snd_hda_gen_free(struct hda_codec *codec)
5849 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
5850 snd_hda_gen_spec_free(codec->spec);
5854 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
5858 * snd_hda_gen_check_power_status - check the loopback power save state
5859 * @codec: the HDA codec
5860 * @nid: NID to inspect
5862 * This can be put as patch_ops check_power_status function.
5864 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5866 struct hda_gen_spec *spec = codec->spec;
5867 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5869 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
5874 * the generic codec support
5877 static const struct hda_codec_ops generic_patch_ops = {
5878 .build_controls = snd_hda_gen_build_controls,
5879 .build_pcms = snd_hda_gen_build_pcms,
5880 .init = snd_hda_gen_init,
5881 .free = snd_hda_gen_free,
5882 .unsol_event = snd_hda_jack_unsol_event,
5884 .check_power_status = snd_hda_gen_check_power_status,
5889 * snd_hda_parse_generic_codec - Generic codec parser
5890 * @codec: the HDA codec
5892 static int snd_hda_parse_generic_codec(struct hda_codec *codec)
5894 struct hda_gen_spec *spec;
5897 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5900 snd_hda_gen_spec_init(spec);
5903 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5907 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5911 codec->patch_ops = generic_patch_ops;
5915 snd_hda_gen_free(codec);
5919 static const struct hda_device_id snd_hda_id_generic[] = {
5920 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
5923 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
5925 static struct hda_codec_driver generic_driver = {
5926 .id = snd_hda_id_generic,
5929 module_hda_codec_driver(generic_driver);
5931 MODULE_LICENSE("GPL");
5932 MODULE_DESCRIPTION("Generic HD-audio codec parser");