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_get(struct snd_kcontrol *kcontrol,
952 struct snd_ctl_elem_value *ucontrol);
953 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
954 struct snd_ctl_elem_value *ucontrol);
961 static const struct snd_kcontrol_new control_templates[] = {
962 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
963 /* only the put callback is replaced for handling the special mute */
965 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
966 .subdevice = HDA_SUBDEV_AMP_FLAG,
967 .info = snd_hda_mixer_amp_switch_info,
968 .get = snd_hda_mixer_amp_switch_get,
969 .put = hda_gen_mixer_mute_put, /* replaced */
970 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
973 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
974 .info = snd_hda_mixer_amp_switch_info,
975 .get = hda_gen_bind_mute_get,
976 .put = hda_gen_bind_mute_put, /* replaced */
977 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
981 /* add dynamic controls from template */
982 static struct snd_kcontrol_new *
983 add_control(struct hda_gen_spec *spec, int type, const char *name,
984 int cidx, unsigned long val)
986 struct snd_kcontrol_new *knew;
988 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
992 if (get_amp_nid_(val))
993 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
994 knew->private_value = val;
998 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
999 const char *pfx, const char *dir,
1000 const char *sfx, int cidx, unsigned long val)
1002 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1003 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
1004 if (!add_control(spec, type, name, cidx, val))
1009 #define add_pb_vol_ctrl(spec, type, pfx, val) \
1010 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1011 #define add_pb_sw_ctrl(spec, type, pfx, val) \
1012 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1013 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1014 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1015 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1016 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1018 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1019 unsigned int chs, struct nid_path *path)
1024 val = path->ctls[NID_PATH_VOL_CTL];
1027 val = amp_val_replace_channels(val, chs);
1028 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1031 /* return the channel bits suitable for the given path->ctls[] */
1032 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1035 int chs = 1; /* mono (left only) */
1037 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1038 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1039 chs = 3; /* stereo */
1044 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1045 struct nid_path *path)
1047 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1048 return add_vol_ctl(codec, pfx, cidx, chs, path);
1051 /* create a mute-switch for the given mixer widget;
1052 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1054 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1055 unsigned int chs, struct nid_path *path)
1058 int type = HDA_CTL_WIDGET_MUTE;
1062 val = path->ctls[NID_PATH_MUTE_CTL];
1065 val = amp_val_replace_channels(val, chs);
1066 if (get_amp_direction_(val) == HDA_INPUT) {
1067 hda_nid_t nid = get_amp_nid_(val);
1068 int nums = snd_hda_get_num_conns(codec, nid);
1070 type = HDA_CTL_BIND_MUTE;
1074 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1077 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1078 int cidx, struct nid_path *path)
1080 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1081 return add_sw_ctl(codec, pfx, cidx, chs, path);
1084 /* playback mute control with the software mute bit check */
1085 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1086 struct snd_ctl_elem_value *ucontrol)
1088 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1089 struct hda_gen_spec *spec = codec->spec;
1091 if (spec->auto_mute_via_amp) {
1092 hda_nid_t nid = get_amp_nid(kcontrol);
1093 bool enabled = !((spec->mute_bits >> nid) & 1);
1094 ucontrol->value.integer.value[0] &= enabled;
1095 ucontrol->value.integer.value[1] &= enabled;
1099 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1100 struct snd_ctl_elem_value *ucontrol)
1102 sync_auto_mute_bits(kcontrol, ucontrol);
1103 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1107 * Bound mute controls
1109 #define AMP_VAL_IDX_SHIFT 19
1110 #define AMP_VAL_IDX_MASK (0x0f<<19)
1112 static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
1113 struct snd_ctl_elem_value *ucontrol)
1115 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1119 mutex_lock(&codec->control_mutex);
1120 pval = kcontrol->private_value;
1121 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1122 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1123 kcontrol->private_value = pval;
1124 mutex_unlock(&codec->control_mutex);
1128 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1129 struct snd_ctl_elem_value *ucontrol)
1131 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1133 int i, indices, err = 0, change = 0;
1135 sync_auto_mute_bits(kcontrol, ucontrol);
1137 mutex_lock(&codec->control_mutex);
1138 pval = kcontrol->private_value;
1139 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1140 for (i = 0; i < indices; i++) {
1141 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1142 (i << AMP_VAL_IDX_SHIFT);
1143 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1148 kcontrol->private_value = pval;
1149 mutex_unlock(&codec->control_mutex);
1150 return err < 0 ? err : change;
1153 /* any ctl assigned to the path with the given index? */
1154 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1156 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1157 return path && path->ctls[ctl_type];
1160 static const char * const channel_name[4] = {
1161 "Front", "Surround", "CLFE", "Side"
1164 /* give some appropriate ctl name prefix for the given line out channel */
1165 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1166 int *index, int ctl_type)
1168 struct hda_gen_spec *spec = codec->spec;
1169 struct auto_pin_cfg *cfg = &spec->autocfg;
1172 if (cfg->line_outs == 1 && !spec->multi_ios &&
1173 !codec->force_pin_prefix &&
1174 !cfg->hp_outs && !cfg->speaker_outs)
1175 return spec->vmaster_mute.hook ? "PCM" : "Master";
1177 /* if there is really a single DAC used in the whole output paths,
1178 * use it master (or "PCM" if a vmaster hook is present)
1180 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1181 !codec->force_pin_prefix &&
1182 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1183 return spec->vmaster_mute.hook ? "PCM" : "Master";
1185 /* multi-io channels */
1186 if (ch >= cfg->line_outs)
1187 return channel_name[ch];
1189 switch (cfg->line_out_type) {
1190 case AUTO_PIN_SPEAKER_OUT:
1191 /* if the primary channel vol/mute is shared with HP volume,
1192 * don't name it as Speaker
1194 if (!ch && cfg->hp_outs &&
1195 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1197 if (cfg->line_outs == 1)
1199 if (cfg->line_outs == 2)
1200 return ch ? "Bass Speaker" : "Speaker";
1202 case AUTO_PIN_HP_OUT:
1203 /* if the primary channel vol/mute is shared with spk volume,
1204 * don't name it as Headphone
1206 if (!ch && cfg->speaker_outs &&
1207 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1209 /* for multi-io case, only the primary out */
1210 if (ch && spec->multi_ios)
1214 case AUTO_PIN_LINE_OUT:
1215 /* This deals with the case where we have two DACs and
1216 * one LO, one HP and one Speaker */
1217 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1218 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1219 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1220 if (hp_lo_shared && spk_lo_shared)
1221 return spec->vmaster_mute.hook ? "PCM" : "Master";
1223 return "Headphone+LO";
1225 return "Speaker+LO";
1229 /* for a single channel output, we don't have to name the channel */
1230 if (cfg->line_outs == 1 && !spec->multi_ios)
1233 if (ch >= ARRAY_SIZE(channel_name)) {
1238 return channel_name[ch];
1242 * Parse output paths
1245 /* badness definition */
1247 /* No primary DAC is found for the main output */
1248 BAD_NO_PRIMARY_DAC = 0x10000,
1249 /* No DAC is found for the extra output */
1250 BAD_NO_DAC = 0x4000,
1251 /* No possible multi-ios */
1252 BAD_MULTI_IO = 0x120,
1253 /* No individual DAC for extra output */
1254 BAD_NO_EXTRA_DAC = 0x102,
1255 /* No individual DAC for extra surrounds */
1256 BAD_NO_EXTRA_SURR_DAC = 0x101,
1257 /* Primary DAC shared with main surrounds */
1258 BAD_SHARED_SURROUND = 0x100,
1259 /* No independent HP possible */
1260 BAD_NO_INDEP_HP = 0x10,
1261 /* Primary DAC shared with main CLFE */
1262 BAD_SHARED_CLFE = 0x10,
1263 /* Primary DAC shared with extra surrounds */
1264 BAD_SHARED_EXTRA_SURROUND = 0x10,
1265 /* Volume widget is shared */
1266 BAD_SHARED_VOL = 0x10,
1269 /* look for widgets in the given path which are appropriate for
1270 * volume and mute controls, and assign the values to ctls[].
1272 * When no appropriate widget is found in the path, the badness value
1273 * is incremented depending on the situation. The function returns the
1274 * total badness for both volume and mute controls.
1276 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1278 struct hda_gen_spec *spec = codec->spec;
1284 return BAD_SHARED_VOL * 2;
1286 if (path->ctls[NID_PATH_VOL_CTL] ||
1287 path->ctls[NID_PATH_MUTE_CTL])
1288 return 0; /* already evaluated */
1290 nid = look_for_out_vol_nid(codec, path);
1292 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1293 if (spec->dac_min_mute)
1294 val |= HDA_AMP_VAL_MIN_MUTE;
1295 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1296 badness += BAD_SHARED_VOL;
1298 path->ctls[NID_PATH_VOL_CTL] = val;
1300 badness += BAD_SHARED_VOL;
1301 nid = look_for_out_mute_nid(codec, path);
1303 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1304 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1305 nid_has_mute(codec, nid, HDA_OUTPUT))
1306 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1308 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1309 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1310 badness += BAD_SHARED_VOL;
1312 path->ctls[NID_PATH_MUTE_CTL] = val;
1314 badness += BAD_SHARED_VOL;
1318 const struct badness_table hda_main_out_badness = {
1319 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1320 .no_dac = BAD_NO_DAC,
1321 .shared_primary = BAD_NO_PRIMARY_DAC,
1322 .shared_surr = BAD_SHARED_SURROUND,
1323 .shared_clfe = BAD_SHARED_CLFE,
1324 .shared_surr_main = BAD_SHARED_SURROUND,
1326 EXPORT_SYMBOL_GPL(hda_main_out_badness);
1328 const struct badness_table hda_extra_out_badness = {
1329 .no_primary_dac = BAD_NO_DAC,
1330 .no_dac = BAD_NO_DAC,
1331 .shared_primary = BAD_NO_EXTRA_DAC,
1332 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1333 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1334 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1336 EXPORT_SYMBOL_GPL(hda_extra_out_badness);
1338 /* get the DAC of the primary output corresponding to the given array index */
1339 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1341 struct hda_gen_spec *spec = codec->spec;
1342 struct auto_pin_cfg *cfg = &spec->autocfg;
1344 if (cfg->line_outs > idx)
1345 return spec->private_dac_nids[idx];
1346 idx -= cfg->line_outs;
1347 if (spec->multi_ios > idx)
1348 return spec->multi_io[idx].dac;
1352 /* return the DAC if it's reachable, otherwise zero */
1353 static inline hda_nid_t try_dac(struct hda_codec *codec,
1354 hda_nid_t dac, hda_nid_t pin)
1356 return is_reachable_path(codec, dac, pin) ? dac : 0;
1359 /* try to assign DACs to pins and return the resultant badness */
1360 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1361 const hda_nid_t *pins, hda_nid_t *dacs,
1363 const struct badness_table *bad)
1365 struct hda_gen_spec *spec = codec->spec;
1373 for (i = 0; i < num_outs; i++) {
1374 struct nid_path *path;
1375 hda_nid_t pin = pins[i];
1377 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1379 badness += assign_out_path_ctls(codec, path);
1383 dacs[i] = get_preferred_dac(codec, pin);
1385 if (is_dac_already_used(codec, dacs[i]))
1386 badness += bad->shared_primary;
1390 dacs[i] = look_for_dac(codec, pin, false);
1391 if (!dacs[i] && !i) {
1392 /* try to steal the DAC of surrounds for the front */
1393 for (j = 1; j < num_outs; j++) {
1394 if (is_reachable_path(codec, dacs[j], pin)) {
1397 invalidate_nid_path(codec, path_idx[j]);
1406 dac = try_dac(codec, get_primary_out(codec, i), pin);
1408 dac = try_dac(codec, dacs[0], pin);
1410 dac = try_dac(codec, get_primary_out(codec, i), pin);
1413 badness += bad->shared_primary;
1415 badness += bad->shared_surr;
1417 badness += bad->shared_clfe;
1418 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1419 dac = spec->private_dac_nids[0];
1420 badness += bad->shared_surr_main;
1422 badness += bad->no_primary_dac;
1424 badness += bad->no_dac;
1428 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1429 if (!path && !i && spec->mixer_nid) {
1430 /* try with aamix */
1431 path = snd_hda_add_new_path(codec, dac, pin, 0);
1435 badness += bad->no_dac;
1437 /* print_nid_path(codec, "output", path); */
1438 path->active = true;
1439 path_idx[i] = snd_hda_get_path_idx(codec, path);
1440 badness += assign_out_path_ctls(codec, path);
1447 /* return NID if the given pin has only a single connection to a certain DAC */
1448 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1450 struct hda_gen_spec *spec = codec->spec;
1452 hda_nid_t nid_found = 0;
1454 for (i = 0; i < spec->num_all_dacs; i++) {
1455 hda_nid_t nid = spec->all_dacs[i];
1456 if (!nid || is_dac_already_used(codec, nid))
1458 if (is_reachable_path(codec, nid, pin)) {
1467 /* check whether the given pin can be a multi-io pin */
1468 static bool can_be_multiio_pin(struct hda_codec *codec,
1469 unsigned int location, hda_nid_t nid)
1471 unsigned int defcfg, caps;
1473 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1474 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1476 if (location && get_defcfg_location(defcfg) != location)
1478 caps = snd_hda_query_pin_caps(codec, nid);
1479 if (!(caps & AC_PINCAP_OUT))
1484 /* count the number of input pins that are capable to be multi-io */
1485 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1487 struct hda_gen_spec *spec = codec->spec;
1488 struct auto_pin_cfg *cfg = &spec->autocfg;
1489 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1490 unsigned int location = get_defcfg_location(defcfg);
1494 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1495 for (i = 0; i < cfg->num_inputs; i++) {
1496 if (cfg->inputs[i].type != type)
1498 if (can_be_multiio_pin(codec, location,
1499 cfg->inputs[i].pin))
1509 * When hardwired is set, try to fill ony hardwired pins, and returns
1510 * zero if any pins are filled, non-zero if nothing found.
1511 * When hardwired is off, try to fill possible input pins, and returns
1512 * the badness value.
1514 static int fill_multi_ios(struct hda_codec *codec,
1515 hda_nid_t reference_pin,
1518 struct hda_gen_spec *spec = codec->spec;
1519 struct auto_pin_cfg *cfg = &spec->autocfg;
1520 int type, i, j, num_pins, old_pins;
1521 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1522 unsigned int location = get_defcfg_location(defcfg);
1524 struct nid_path *path;
1526 old_pins = spec->multi_ios;
1530 num_pins = count_multiio_pins(codec, reference_pin);
1534 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1535 for (i = 0; i < cfg->num_inputs; i++) {
1536 hda_nid_t nid = cfg->inputs[i].pin;
1539 if (cfg->inputs[i].type != type)
1541 if (!can_be_multiio_pin(codec, location, nid))
1543 for (j = 0; j < spec->multi_ios; j++) {
1544 if (nid == spec->multi_io[j].pin)
1547 if (j < spec->multi_ios)
1551 dac = get_dac_if_single(codec, nid);
1553 dac = look_for_dac(codec, nid, false);
1558 path = snd_hda_add_new_path(codec, dac, nid,
1564 /* print_nid_path(codec, "multiio", path); */
1565 spec->multi_io[spec->multi_ios].pin = nid;
1566 spec->multi_io[spec->multi_ios].dac = dac;
1567 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1568 snd_hda_get_path_idx(codec, path);
1570 if (spec->multi_ios >= 2)
1576 badness = BAD_MULTI_IO;
1577 if (old_pins == spec->multi_ios) {
1579 return 1; /* nothing found */
1581 return badness; /* no badness if nothing found */
1583 if (!hardwired && spec->multi_ios < 2) {
1584 /* cancel newly assigned paths */
1585 spec->paths.used -= spec->multi_ios - old_pins;
1586 spec->multi_ios = old_pins;
1590 /* assign volume and mute controls */
1591 for (i = old_pins; i < spec->multi_ios; i++) {
1592 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1593 badness += assign_out_path_ctls(codec, path);
1599 /* map DACs for all pins in the list if they are single connections */
1600 static bool map_singles(struct hda_codec *codec, int outs,
1601 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1603 struct hda_gen_spec *spec = codec->spec;
1606 for (i = 0; i < outs; i++) {
1607 struct nid_path *path;
1611 dac = get_dac_if_single(codec, pins[i]);
1614 path = snd_hda_add_new_path(codec, dac, pins[i],
1616 if (!path && !i && spec->mixer_nid)
1617 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1621 /* print_nid_path(codec, "output", path); */
1622 path->active = true;
1623 path_idx[i] = snd_hda_get_path_idx(codec, path);
1629 static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1631 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1632 spec->aamix_out_paths[2];
1635 /* create a new path including aamix if available, and return its index */
1636 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1638 struct hda_gen_spec *spec = codec->spec;
1639 struct nid_path *path;
1640 hda_nid_t path_dac, dac, pin;
1642 path = snd_hda_get_path_from_idx(codec, path_idx);
1643 if (!path || !path->depth ||
1644 is_nid_contained(path, spec->mixer_nid))
1646 path_dac = path->path[0];
1647 dac = spec->private_dac_nids[0];
1648 pin = path->path[path->depth - 1];
1649 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1651 if (dac != path_dac)
1653 else if (spec->multiout.hp_out_nid[0])
1654 dac = spec->multiout.hp_out_nid[0];
1655 else if (spec->multiout.extra_out_nid[0])
1656 dac = spec->multiout.extra_out_nid[0];
1660 path = snd_hda_add_new_path(codec, dac, pin,
1665 /* print_nid_path(codec, "output-aamix", path); */
1666 path->active = false; /* unused as default */
1667 path->pin_fixed = true; /* static route */
1668 return snd_hda_get_path_idx(codec, path);
1671 /* check whether the independent HP is available with the current config */
1672 static bool indep_hp_possible(struct hda_codec *codec)
1674 struct hda_gen_spec *spec = codec->spec;
1675 struct auto_pin_cfg *cfg = &spec->autocfg;
1676 struct nid_path *path;
1679 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1680 idx = spec->out_paths[0];
1682 idx = spec->hp_paths[0];
1683 path = snd_hda_get_path_from_idx(codec, idx);
1687 /* assume no path conflicts unless aamix is involved */
1688 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1691 /* check whether output paths contain aamix */
1692 for (i = 0; i < cfg->line_outs; i++) {
1693 if (spec->out_paths[i] == idx)
1695 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1696 if (path && is_nid_contained(path, spec->mixer_nid))
1699 for (i = 0; i < cfg->speaker_outs; i++) {
1700 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1701 if (path && is_nid_contained(path, spec->mixer_nid))
1708 /* fill the empty entries in the dac array for speaker/hp with the
1709 * shared dac pointed by the paths
1711 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1712 hda_nid_t *dacs, int *path_idx)
1714 struct nid_path *path;
1717 for (i = 0; i < num_outs; i++) {
1720 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1723 dacs[i] = path->path[0];
1727 /* fill in the dac_nids table from the parsed pin configuration */
1728 static int fill_and_eval_dacs(struct hda_codec *codec,
1729 bool fill_hardwired,
1730 bool fill_mio_first)
1732 struct hda_gen_spec *spec = codec->spec;
1733 struct auto_pin_cfg *cfg = &spec->autocfg;
1734 int i, err, badness;
1736 /* set num_dacs once to full for look_for_dac() */
1737 spec->multiout.num_dacs = cfg->line_outs;
1738 spec->multiout.dac_nids = spec->private_dac_nids;
1739 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1740 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1741 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1742 spec->multi_ios = 0;
1743 snd_array_free(&spec->paths);
1745 /* clear path indices */
1746 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1747 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1748 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1749 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1750 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1751 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1752 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1753 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1757 /* fill hard-wired DACs first */
1758 if (fill_hardwired) {
1761 mapped = map_singles(codec, cfg->line_outs,
1763 spec->private_dac_nids,
1765 mapped |= map_singles(codec, cfg->hp_outs,
1767 spec->multiout.hp_out_nid,
1769 mapped |= map_singles(codec, cfg->speaker_outs,
1771 spec->multiout.extra_out_nid,
1772 spec->speaker_paths);
1773 if (!spec->no_multi_io &&
1774 fill_mio_first && cfg->line_outs == 1 &&
1775 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1776 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1783 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1784 spec->private_dac_nids, spec->out_paths,
1785 spec->main_out_badness);
1787 if (!spec->no_multi_io && fill_mio_first &&
1788 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1789 /* try to fill multi-io first */
1790 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1793 /* we don't count badness at this stage yet */
1796 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1797 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1798 spec->multiout.hp_out_nid,
1800 spec->extra_out_badness);
1805 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1806 err = try_assign_dacs(codec, cfg->speaker_outs,
1808 spec->multiout.extra_out_nid,
1809 spec->speaker_paths,
1810 spec->extra_out_badness);
1815 if (!spec->no_multi_io &&
1816 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1817 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1823 if (spec->mixer_nid) {
1824 spec->aamix_out_paths[0] =
1825 check_aamix_out_path(codec, spec->out_paths[0]);
1826 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1827 spec->aamix_out_paths[1] =
1828 check_aamix_out_path(codec, spec->hp_paths[0]);
1829 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1830 spec->aamix_out_paths[2] =
1831 check_aamix_out_path(codec, spec->speaker_paths[0]);
1834 if (!spec->no_multi_io &&
1835 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1836 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1837 spec->multi_ios = 1; /* give badness */
1839 /* re-count num_dacs and squash invalid entries */
1840 spec->multiout.num_dacs = 0;
1841 for (i = 0; i < cfg->line_outs; i++) {
1842 if (spec->private_dac_nids[i])
1843 spec->multiout.num_dacs++;
1845 memmove(spec->private_dac_nids + i,
1846 spec->private_dac_nids + i + 1,
1847 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1848 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1852 spec->ext_channel_count = spec->min_channel_count =
1853 spec->multiout.num_dacs * 2;
1855 if (spec->multi_ios == 2) {
1856 for (i = 0; i < 2; i++)
1857 spec->private_dac_nids[spec->multiout.num_dacs++] =
1858 spec->multi_io[i].dac;
1859 } else if (spec->multi_ios) {
1860 spec->multi_ios = 0;
1861 badness += BAD_MULTI_IO;
1864 if (spec->indep_hp && !indep_hp_possible(codec))
1865 badness += BAD_NO_INDEP_HP;
1867 /* re-fill the shared DAC for speaker / headphone */
1868 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1869 refill_shared_dacs(codec, cfg->hp_outs,
1870 spec->multiout.hp_out_nid,
1872 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1873 refill_shared_dacs(codec, cfg->speaker_outs,
1874 spec->multiout.extra_out_nid,
1875 spec->speaker_paths);
1880 #define DEBUG_BADNESS
1882 #ifdef DEBUG_BADNESS
1883 #define debug_badness(fmt, ...) \
1884 codec_dbg(codec, fmt, ##__VA_ARGS__)
1886 #define debug_badness(fmt, ...) \
1887 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1890 #ifdef DEBUG_BADNESS
1891 static inline void print_nid_path_idx(struct hda_codec *codec,
1892 const char *pfx, int idx)
1894 struct nid_path *path;
1896 path = snd_hda_get_path_from_idx(codec, idx);
1898 print_nid_path(codec, pfx, path);
1901 static void debug_show_configs(struct hda_codec *codec,
1902 struct auto_pin_cfg *cfg)
1904 struct hda_gen_spec *spec = codec->spec;
1905 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1908 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1909 cfg->line_out_pins[0], cfg->line_out_pins[1],
1910 cfg->line_out_pins[2], cfg->line_out_pins[3],
1911 spec->multiout.dac_nids[0],
1912 spec->multiout.dac_nids[1],
1913 spec->multiout.dac_nids[2],
1914 spec->multiout.dac_nids[3],
1915 lo_type[cfg->line_out_type]);
1916 for (i = 0; i < cfg->line_outs; i++)
1917 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1918 if (spec->multi_ios > 0)
1919 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1921 spec->multi_io[0].pin, spec->multi_io[1].pin,
1922 spec->multi_io[0].dac, spec->multi_io[1].dac);
1923 for (i = 0; i < spec->multi_ios; i++)
1924 print_nid_path_idx(codec, " mio",
1925 spec->out_paths[cfg->line_outs + i]);
1927 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1928 cfg->hp_pins[0], cfg->hp_pins[1],
1929 cfg->hp_pins[2], cfg->hp_pins[3],
1930 spec->multiout.hp_out_nid[0],
1931 spec->multiout.hp_out_nid[1],
1932 spec->multiout.hp_out_nid[2],
1933 spec->multiout.hp_out_nid[3]);
1934 for (i = 0; i < cfg->hp_outs; i++)
1935 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1936 if (cfg->speaker_outs)
1937 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1938 cfg->speaker_pins[0], cfg->speaker_pins[1],
1939 cfg->speaker_pins[2], cfg->speaker_pins[3],
1940 spec->multiout.extra_out_nid[0],
1941 spec->multiout.extra_out_nid[1],
1942 spec->multiout.extra_out_nid[2],
1943 spec->multiout.extra_out_nid[3]);
1944 for (i = 0; i < cfg->speaker_outs; i++)
1945 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1946 for (i = 0; i < 3; i++)
1947 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1950 #define debug_show_configs(codec, cfg) /* NOP */
1953 /* find all available DACs of the codec */
1954 static void fill_all_dac_nids(struct hda_codec *codec)
1956 struct hda_gen_spec *spec = codec->spec;
1959 spec->num_all_dacs = 0;
1960 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1961 for_each_hda_codec_node(nid, codec) {
1962 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1964 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1965 codec_err(codec, "Too many DACs!\n");
1968 spec->all_dacs[spec->num_all_dacs++] = nid;
1972 static int parse_output_paths(struct hda_codec *codec)
1974 struct hda_gen_spec *spec = codec->spec;
1975 struct auto_pin_cfg *cfg = &spec->autocfg;
1976 struct auto_pin_cfg *best_cfg;
1978 int best_badness = INT_MAX;
1980 bool fill_hardwired = true, fill_mio_first = true;
1981 bool best_wired = true, best_mio = true;
1982 bool hp_spk_swapped = false;
1984 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1990 badness = fill_and_eval_dacs(codec, fill_hardwired,
1996 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1997 cfg->line_out_type, fill_hardwired, fill_mio_first,
1999 debug_show_configs(codec, cfg);
2000 if (badness < best_badness) {
2001 best_badness = badness;
2003 best_wired = fill_hardwired;
2004 best_mio = fill_mio_first;
2008 fill_mio_first = !fill_mio_first;
2009 if (!fill_mio_first)
2011 fill_hardwired = !fill_hardwired;
2012 if (!fill_hardwired)
2016 hp_spk_swapped = true;
2017 if (cfg->speaker_outs > 0 &&
2018 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2019 cfg->hp_outs = cfg->line_outs;
2020 memcpy(cfg->hp_pins, cfg->line_out_pins,
2021 sizeof(cfg->hp_pins));
2022 cfg->line_outs = cfg->speaker_outs;
2023 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2024 sizeof(cfg->speaker_pins));
2025 cfg->speaker_outs = 0;
2026 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2027 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2028 fill_hardwired = true;
2031 if (cfg->hp_outs > 0 &&
2032 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2033 cfg->speaker_outs = cfg->line_outs;
2034 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2035 sizeof(cfg->speaker_pins));
2036 cfg->line_outs = cfg->hp_outs;
2037 memcpy(cfg->line_out_pins, cfg->hp_pins,
2038 sizeof(cfg->hp_pins));
2040 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2041 cfg->line_out_type = AUTO_PIN_HP_OUT;
2042 fill_hardwired = true;
2049 debug_badness("==> restoring best_cfg\n");
2051 fill_and_eval_dacs(codec, best_wired, best_mio);
2053 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2054 cfg->line_out_type, best_wired, best_mio);
2055 debug_show_configs(codec, cfg);
2057 if (cfg->line_out_pins[0]) {
2058 struct nid_path *path;
2059 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
2061 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
2062 if (spec->vmaster_nid) {
2063 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2064 HDA_OUTPUT, spec->vmaster_tlv);
2065 if (spec->dac_min_mute)
2066 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2070 /* set initial pinctl targets */
2071 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2075 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2076 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2077 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2078 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2079 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2080 set_pin_targets(codec, cfg->speaker_outs,
2081 cfg->speaker_pins, val);
2084 /* clear indep_hp flag if not available */
2085 if (spec->indep_hp && !indep_hp_possible(codec))
2092 /* add playback controls from the parsed DAC table */
2093 static int create_multi_out_ctls(struct hda_codec *codec,
2094 const struct auto_pin_cfg *cfg)
2096 struct hda_gen_spec *spec = codec->spec;
2097 int i, err, noutputs;
2099 noutputs = cfg->line_outs;
2100 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2101 noutputs += spec->multi_ios;
2103 for (i = 0; i < noutputs; i++) {
2106 struct nid_path *path;
2108 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
2112 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
2113 if (!name || !strcmp(name, "CLFE")) {
2115 err = add_vol_ctl(codec, "Center", 0, 1, path);
2118 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2122 err = add_stereo_vol(codec, name, index, path);
2127 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2128 if (!name || !strcmp(name, "CLFE")) {
2129 err = add_sw_ctl(codec, "Center", 0, 1, path);
2132 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2136 err = add_stereo_sw(codec, name, index, path);
2144 static int create_extra_out(struct hda_codec *codec, int path_idx,
2145 const char *pfx, int cidx)
2147 struct nid_path *path;
2150 path = snd_hda_get_path_from_idx(codec, path_idx);
2153 err = add_stereo_vol(codec, pfx, cidx, path);
2156 err = add_stereo_sw(codec, pfx, cidx, path);
2162 /* add playback controls for speaker and HP outputs */
2163 static int create_extra_outs(struct hda_codec *codec, int num_pins,
2164 const int *paths, const char *pfx)
2168 for (i = 0; i < num_pins; i++) {
2170 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2173 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2174 name = "Bass Speaker";
2175 else if (num_pins >= 3) {
2176 snprintf(tmp, sizeof(tmp), "%s %s",
2177 pfx, channel_name[i]);
2183 err = create_extra_out(codec, paths[i], name, idx);
2190 static int create_hp_out_ctls(struct hda_codec *codec)
2192 struct hda_gen_spec *spec = codec->spec;
2193 return create_extra_outs(codec, spec->autocfg.hp_outs,
2198 static int create_speaker_out_ctls(struct hda_codec *codec)
2200 struct hda_gen_spec *spec = codec->spec;
2201 return create_extra_outs(codec, spec->autocfg.speaker_outs,
2202 spec->speaker_paths,
2207 * independent HP controls
2210 static void call_hp_automute(struct hda_codec *codec,
2211 struct hda_jack_callback *jack);
2212 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2213 struct snd_ctl_elem_info *uinfo)
2215 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2218 static int indep_hp_get(struct snd_kcontrol *kcontrol,
2219 struct snd_ctl_elem_value *ucontrol)
2221 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2222 struct hda_gen_spec *spec = codec->spec;
2223 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2227 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2228 int nomix_path_idx, int mix_path_idx,
2231 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2232 struct snd_ctl_elem_value *ucontrol)
2234 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2235 struct hda_gen_spec *spec = codec->spec;
2236 unsigned int select = ucontrol->value.enumerated.item[0];
2239 mutex_lock(&spec->pcm_mutex);
2240 if (spec->active_streams) {
2245 if (spec->indep_hp_enabled != select) {
2247 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2248 dacp = &spec->private_dac_nids[0];
2250 dacp = &spec->multiout.hp_out_nid[0];
2252 /* update HP aamix paths in case it conflicts with indep HP */
2253 if (spec->have_aamix_ctl) {
2254 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2255 update_aamix_paths(codec, spec->aamix_mode,
2257 spec->aamix_out_paths[0],
2258 spec->autocfg.line_out_type);
2260 update_aamix_paths(codec, spec->aamix_mode,
2262 spec->aamix_out_paths[1],
2266 spec->indep_hp_enabled = select;
2267 if (spec->indep_hp_enabled)
2270 *dacp = spec->alt_dac_nid;
2272 call_hp_automute(codec, NULL);
2276 mutex_unlock(&spec->pcm_mutex);
2280 static const struct snd_kcontrol_new indep_hp_ctl = {
2281 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2282 .name = "Independent HP",
2283 .info = indep_hp_info,
2284 .get = indep_hp_get,
2285 .put = indep_hp_put,
2289 static int create_indep_hp_ctls(struct hda_codec *codec)
2291 struct hda_gen_spec *spec = codec->spec;
2294 if (!spec->indep_hp)
2296 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2297 dac = spec->multiout.dac_nids[0];
2299 dac = spec->multiout.hp_out_nid[0];
2305 spec->indep_hp_enabled = false;
2306 spec->alt_dac_nid = dac;
2307 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2313 * channel mode enum control
2316 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2317 struct snd_ctl_elem_info *uinfo)
2319 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2320 struct hda_gen_spec *spec = codec->spec;
2323 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2325 uinfo->value.enumerated.items = spec->multi_ios + 1;
2326 if (uinfo->value.enumerated.item > spec->multi_ios)
2327 uinfo->value.enumerated.item = spec->multi_ios;
2328 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2329 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2333 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2334 struct snd_ctl_elem_value *ucontrol)
2336 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2337 struct hda_gen_spec *spec = codec->spec;
2338 ucontrol->value.enumerated.item[0] =
2339 (spec->ext_channel_count - spec->min_channel_count) / 2;
2343 static inline struct nid_path *
2344 get_multiio_path(struct hda_codec *codec, int idx)
2346 struct hda_gen_spec *spec = codec->spec;
2347 return snd_hda_get_path_from_idx(codec,
2348 spec->out_paths[spec->autocfg.line_outs + idx]);
2351 static void update_automute_all(struct hda_codec *codec);
2353 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2354 * used for output paths
2356 static bool aamix_default(struct hda_gen_spec *spec)
2358 return !spec->have_aamix_ctl || spec->aamix_mode;
2361 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2363 struct hda_gen_spec *spec = codec->spec;
2364 hda_nid_t nid = spec->multi_io[idx].pin;
2365 struct nid_path *path;
2367 path = get_multiio_path(codec, idx);
2371 if (path->active == output)
2375 set_pin_target(codec, nid, PIN_OUT, true);
2376 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2377 set_pin_eapd(codec, nid, true);
2379 set_pin_eapd(codec, nid, false);
2380 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2381 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2382 path_power_down_sync(codec, path);
2385 /* update jack retasking in case it modifies any of them */
2386 update_automute_all(codec);
2391 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2392 struct snd_ctl_elem_value *ucontrol)
2394 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2395 struct hda_gen_spec *spec = codec->spec;
2398 ch = ucontrol->value.enumerated.item[0];
2399 if (ch < 0 || ch > spec->multi_ios)
2401 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2403 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2404 for (i = 0; i < spec->multi_ios; i++)
2405 set_multi_io(codec, i, i < ch);
2406 spec->multiout.max_channels = max(spec->ext_channel_count,
2407 spec->const_channel_count);
2408 if (spec->need_dac_fix)
2409 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2413 static const struct snd_kcontrol_new channel_mode_enum = {
2414 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2415 .name = "Channel Mode",
2416 .info = ch_mode_info,
2421 static int create_multi_channel_mode(struct hda_codec *codec)
2423 struct hda_gen_spec *spec = codec->spec;
2425 if (spec->multi_ios > 0) {
2426 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2433 * aamix loopback enable/disable switch
2436 #define loopback_mixing_info indep_hp_info
2438 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2439 struct snd_ctl_elem_value *ucontrol)
2441 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2442 struct hda_gen_spec *spec = codec->spec;
2443 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2447 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2448 int nomix_path_idx, int mix_path_idx,
2451 struct hda_gen_spec *spec = codec->spec;
2452 struct nid_path *nomix_path, *mix_path;
2454 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2455 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2456 if (!nomix_path || !mix_path)
2459 /* if HP aamix path is driven from a different DAC and the
2460 * independent HP mode is ON, can't turn on aamix path
2462 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2463 mix_path->path[0] != spec->alt_dac_nid)
2467 snd_hda_activate_path(codec, nomix_path, false, true);
2468 snd_hda_activate_path(codec, mix_path, true, true);
2469 path_power_down_sync(codec, nomix_path);
2471 snd_hda_activate_path(codec, mix_path, false, false);
2472 snd_hda_activate_path(codec, nomix_path, true, false);
2473 path_power_down_sync(codec, mix_path);
2477 /* re-initialize the output paths; only called from loopback_mixing_put() */
2478 static void update_output_paths(struct hda_codec *codec, int num_outs,
2481 struct hda_gen_spec *spec = codec->spec;
2482 struct nid_path *path;
2485 for (i = 0; i < num_outs; i++) {
2486 path = snd_hda_get_path_from_idx(codec, paths[i]);
2488 snd_hda_activate_path(codec, path, path->active,
2493 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2494 struct snd_ctl_elem_value *ucontrol)
2496 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2497 struct hda_gen_spec *spec = codec->spec;
2498 const struct auto_pin_cfg *cfg = &spec->autocfg;
2499 unsigned int val = ucontrol->value.enumerated.item[0];
2501 if (val == spec->aamix_mode)
2503 spec->aamix_mode = val;
2504 if (has_aamix_out_paths(spec)) {
2505 update_aamix_paths(codec, val, spec->out_paths[0],
2506 spec->aamix_out_paths[0],
2507 cfg->line_out_type);
2508 update_aamix_paths(codec, val, spec->hp_paths[0],
2509 spec->aamix_out_paths[1],
2511 update_aamix_paths(codec, val, spec->speaker_paths[0],
2512 spec->aamix_out_paths[2],
2513 AUTO_PIN_SPEAKER_OUT);
2515 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2516 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2517 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2518 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2519 update_output_paths(codec, cfg->speaker_outs,
2520 spec->speaker_paths);
2525 static const struct snd_kcontrol_new loopback_mixing_enum = {
2526 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2527 .name = "Loopback Mixing",
2528 .info = loopback_mixing_info,
2529 .get = loopback_mixing_get,
2530 .put = loopback_mixing_put,
2533 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2535 struct hda_gen_spec *spec = codec->spec;
2537 if (!spec->mixer_nid)
2539 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2541 spec->have_aamix_ctl = 1;
2546 * shared headphone/mic handling
2549 static void call_update_outputs(struct hda_codec *codec);
2551 /* for shared I/O, change the pin-control accordingly */
2552 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2554 struct hda_gen_spec *spec = codec->spec;
2559 pin = spec->hp_mic_pin;
2560 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2563 val = snd_hda_codec_get_pin_target(codec, pin);
2573 val = snd_hda_get_default_vref(codec, pin);
2574 /* if the HP pin doesn't support VREF and the codec driver gives an
2575 * alternative pin, set up the VREF on that pin instead
2577 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2578 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2579 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2580 if (vref_val != AC_PINCTL_VREF_HIZ)
2581 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2582 PIN_IN | (as_mic ? vref_val : 0));
2585 if (!spec->hp_mic_jack_modes) {
2590 set_pin_target(codec, pin, val, true);
2591 call_hp_automute(codec, NULL);
2595 /* create a shared input with the headphone out */
2596 static int create_hp_mic(struct hda_codec *codec)
2598 struct hda_gen_spec *spec = codec->spec;
2599 struct auto_pin_cfg *cfg = &spec->autocfg;
2600 unsigned int defcfg;
2603 if (!spec->hp_mic) {
2604 if (spec->suppress_hp_mic_detect)
2606 /* automatic detection: only if no input or a single internal
2607 * input pin is found, try to detect the shared hp/mic
2609 if (cfg->num_inputs > 1)
2611 else if (cfg->num_inputs == 1) {
2612 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2613 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2618 spec->hp_mic = 0; /* clear once */
2619 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2623 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2624 nid = cfg->line_out_pins[0];
2625 else if (cfg->hp_outs > 0)
2626 nid = cfg->hp_pins[0];
2630 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2631 return 0; /* no input */
2633 cfg->inputs[cfg->num_inputs].pin = nid;
2634 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2635 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2638 spec->hp_mic_pin = nid;
2639 /* we can't handle auto-mic together with HP-mic */
2640 spec->suppress_auto_mic = 1;
2641 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
2649 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2651 static const char * const out_jack_texts[] = {
2652 "Line Out", "Headphone Out",
2655 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2656 struct snd_ctl_elem_info *uinfo)
2658 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2661 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2662 struct snd_ctl_elem_value *ucontrol)
2664 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2665 hda_nid_t nid = kcontrol->private_value;
2666 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2667 ucontrol->value.enumerated.item[0] = 1;
2669 ucontrol->value.enumerated.item[0] = 0;
2673 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2674 struct snd_ctl_elem_value *ucontrol)
2676 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2677 hda_nid_t nid = kcontrol->private_value;
2680 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2681 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2683 snd_hda_set_pin_ctl_cache(codec, nid, val);
2687 static const struct snd_kcontrol_new out_jack_mode_enum = {
2688 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2689 .info = out_jack_mode_info,
2690 .get = out_jack_mode_get,
2691 .put = out_jack_mode_put,
2694 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2696 struct hda_gen_spec *spec = codec->spec;
2699 for (i = 0; i < spec->kctls.used; i++) {
2700 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2701 if (!strcmp(kctl->name, name) && kctl->index == idx)
2707 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2708 char *name, size_t name_len)
2710 struct hda_gen_spec *spec = codec->spec;
2713 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2714 strlcat(name, " Jack Mode", name_len);
2716 for (; find_kctl_name(codec, name, idx); idx++)
2720 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2722 struct hda_gen_spec *spec = codec->spec;
2723 if (spec->add_jack_modes) {
2724 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2725 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2731 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2734 struct hda_gen_spec *spec = codec->spec;
2737 for (i = 0; i < num_pins; i++) {
2738 hda_nid_t pin = pins[i];
2739 if (pin == spec->hp_mic_pin)
2741 if (get_out_jack_num_items(codec, pin) > 1) {
2742 struct snd_kcontrol_new *knew;
2743 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2744 get_jack_mode_name(codec, pin, name, sizeof(name));
2745 knew = snd_hda_gen_add_kctl(spec, name,
2746 &out_jack_mode_enum);
2749 knew->private_value = pin;
2760 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2763 static const char * const vref_texts[NUM_VREFS] = {
2764 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2765 "", "Mic 80pc Bias", "Mic 100pc Bias"
2768 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2770 unsigned int pincap;
2772 pincap = snd_hda_query_pin_caps(codec, pin);
2773 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2774 /* filter out unusual vrefs */
2775 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2779 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2780 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2782 unsigned int i, n = 0;
2784 for (i = 0; i < NUM_VREFS; i++) {
2785 if (vref_caps & (1 << i)) {
2794 /* convert back from the vref ctl index to the enum item index */
2795 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2797 unsigned int i, n = 0;
2799 for (i = 0; i < NUM_VREFS; i++) {
2802 if (vref_caps & (1 << i))
2808 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2809 struct snd_ctl_elem_info *uinfo)
2811 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2812 hda_nid_t nid = kcontrol->private_value;
2813 unsigned int vref_caps = get_vref_caps(codec, nid);
2815 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2817 /* set the right text */
2818 strcpy(uinfo->value.enumerated.name,
2819 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2823 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2824 struct snd_ctl_elem_value *ucontrol)
2826 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2827 hda_nid_t nid = kcontrol->private_value;
2828 unsigned int vref_caps = get_vref_caps(codec, nid);
2831 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2832 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2836 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2837 struct snd_ctl_elem_value *ucontrol)
2839 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2840 hda_nid_t nid = kcontrol->private_value;
2841 unsigned int vref_caps = get_vref_caps(codec, nid);
2842 unsigned int val, idx;
2844 val = snd_hda_codec_get_pin_target(codec, nid);
2845 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2846 if (idx == ucontrol->value.enumerated.item[0])
2849 val &= ~AC_PINCTL_VREFEN;
2850 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2851 snd_hda_set_pin_ctl_cache(codec, nid, val);
2855 static const struct snd_kcontrol_new in_jack_mode_enum = {
2856 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2857 .info = in_jack_mode_info,
2858 .get = in_jack_mode_get,
2859 .put = in_jack_mode_put,
2862 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2864 struct hda_gen_spec *spec = codec->spec;
2866 if (spec->add_jack_modes)
2867 nitems = hweight32(get_vref_caps(codec, pin));
2868 return nitems ? nitems : 1;
2871 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2873 struct hda_gen_spec *spec = codec->spec;
2874 struct snd_kcontrol_new *knew;
2875 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2876 unsigned int defcfg;
2878 if (pin == spec->hp_mic_pin)
2879 return 0; /* already done in create_out_jack_mode() */
2881 /* no jack mode for fixed pins */
2882 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2883 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2886 /* no multiple vref caps? */
2887 if (get_in_jack_num_items(codec, pin) <= 1)
2890 get_jack_mode_name(codec, pin, name, sizeof(name));
2891 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2894 knew->private_value = pin;
2899 * HP/mic shared jack mode
2901 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2902 struct snd_ctl_elem_info *uinfo)
2904 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2905 hda_nid_t nid = kcontrol->private_value;
2906 int out_jacks = get_out_jack_num_items(codec, nid);
2907 int in_jacks = get_in_jack_num_items(codec, nid);
2908 const char *text = NULL;
2911 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2913 uinfo->value.enumerated.items = out_jacks + in_jacks;
2914 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2915 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2916 idx = uinfo->value.enumerated.item;
2917 if (idx < out_jacks) {
2919 text = out_jack_texts[idx];
2921 text = "Headphone Out";
2925 unsigned int vref_caps = get_vref_caps(codec, nid);
2926 text = vref_texts[get_vref_idx(vref_caps, idx)];
2931 strcpy(uinfo->value.enumerated.name, text);
2935 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2937 int out_jacks = get_out_jack_num_items(codec, nid);
2938 int in_jacks = get_in_jack_num_items(codec, nid);
2939 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2942 if (val & PIN_OUT) {
2943 if (out_jacks > 1 && val == PIN_HP)
2945 } else if (val & PIN_IN) {
2948 unsigned int vref_caps = get_vref_caps(codec, nid);
2949 val &= AC_PINCTL_VREFEN;
2950 idx += cvt_from_vref_idx(vref_caps, val);
2956 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2957 struct snd_ctl_elem_value *ucontrol)
2959 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2960 hda_nid_t nid = kcontrol->private_value;
2961 ucontrol->value.enumerated.item[0] =
2962 get_cur_hp_mic_jack_mode(codec, nid);
2966 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2967 struct snd_ctl_elem_value *ucontrol)
2969 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2970 hda_nid_t nid = kcontrol->private_value;
2971 int out_jacks = get_out_jack_num_items(codec, nid);
2972 int in_jacks = get_in_jack_num_items(codec, nid);
2973 unsigned int val, oldval, idx;
2975 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2976 idx = ucontrol->value.enumerated.item[0];
2980 if (idx < out_jacks) {
2982 val = idx ? PIN_HP : PIN_OUT;
2988 unsigned int vref_caps = get_vref_caps(codec, nid);
2989 val = snd_hda_codec_get_pin_target(codec, nid);
2990 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2991 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
2993 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
2995 snd_hda_set_pin_ctl_cache(codec, nid, val);
2996 call_hp_automute(codec, NULL);
3001 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
3002 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3003 .info = hp_mic_jack_mode_info,
3004 .get = hp_mic_jack_mode_get,
3005 .put = hp_mic_jack_mode_put,
3008 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
3010 struct hda_gen_spec *spec = codec->spec;
3011 struct snd_kcontrol_new *knew;
3013 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
3014 &hp_mic_jack_mode_enum);
3017 knew->private_value = pin;
3018 spec->hp_mic_jack_modes = 1;
3026 /* add the powersave loopback-list entry */
3027 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
3029 struct hda_amp_list *list;
3031 list = snd_array_new(&spec->loopback_list);
3035 list->dir = HDA_INPUT;
3037 spec->loopback.amplist = spec->loopback_list.list;
3041 /* return true if either a volume or a mute amp is found for the given
3042 * aamix path; the amp has to be either in the mixer node or its direct leaf
3044 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3045 hda_nid_t pin, unsigned int *mix_val,
3046 unsigned int *mute_val)
3049 const hda_nid_t *list;
3052 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3056 *mix_val = *mute_val = 0;
3057 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3058 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3059 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3060 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3061 if (*mix_val && *mute_val)
3064 /* check leaf node */
3065 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3066 if (num_conns < idx)
3069 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3070 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
3071 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3072 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3073 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
3074 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3076 return *mix_val || *mute_val;
3079 /* create input playback/capture controls for the given pin */
3080 static int new_analog_input(struct hda_codec *codec, int input_idx,
3081 hda_nid_t pin, const char *ctlname, int ctlidx,
3084 struct hda_gen_spec *spec = codec->spec;
3085 struct nid_path *path;
3086 unsigned int mix_val, mute_val;
3089 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3092 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
3095 print_nid_path(codec, "loopback", path);
3096 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
3098 idx = path->idx[path->depth - 1];
3100 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
3103 path->ctls[NID_PATH_VOL_CTL] = mix_val;
3107 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
3110 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
3113 path->active = true;
3114 path->stream_enabled = true; /* no DAC/ADC involved */
3115 err = add_loopback_list(spec, mix_nid, idx);
3119 if (spec->mixer_nid != spec->mixer_merge_nid &&
3120 !spec->loopback_merge_path) {
3121 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3122 spec->mixer_merge_nid, 0);
3124 print_nid_path(codec, "loopback-merge", path);
3125 path->active = true;
3126 path->pin_fixed = true; /* static route */
3127 path->stream_enabled = true; /* no DAC/ADC involved */
3128 spec->loopback_merge_path =
3129 snd_hda_get_path_idx(codec, path);
3136 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3138 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3139 return (pincap & AC_PINCAP_IN) != 0;
3142 /* Parse the codec tree and retrieve ADCs */
3143 static int fill_adc_nids(struct hda_codec *codec)
3145 struct hda_gen_spec *spec = codec->spec;
3147 hda_nid_t *adc_nids = spec->adc_nids;
3148 int max_nums = ARRAY_SIZE(spec->adc_nids);
3151 for_each_hda_codec_node(nid, codec) {
3152 unsigned int caps = get_wcaps(codec, nid);
3153 int type = get_wcaps_type(caps);
3155 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3157 adc_nids[nums] = nid;
3158 if (++nums >= max_nums)
3161 spec->num_adc_nids = nums;
3163 /* copy the detected ADCs to all_adcs[] */
3164 spec->num_all_adcs = nums;
3165 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3170 /* filter out invalid adc_nids that don't give all active input pins;
3171 * if needed, check whether dynamic ADC-switching is available
3173 static int check_dyn_adc_switch(struct hda_codec *codec)
3175 struct hda_gen_spec *spec = codec->spec;
3176 struct hda_input_mux *imux = &spec->input_mux;
3177 unsigned int ok_bits;
3182 for (n = 0; n < spec->num_adc_nids; n++) {
3183 for (i = 0; i < imux->num_items; i++) {
3184 if (!spec->input_paths[i][n])
3187 if (i >= imux->num_items) {
3188 ok_bits |= (1 << n);
3194 /* check whether ADC-switch is possible */
3195 for (i = 0; i < imux->num_items; i++) {
3196 for (n = 0; n < spec->num_adc_nids; n++) {
3197 if (spec->input_paths[i][n]) {
3198 spec->dyn_adc_idx[i] = n;
3204 codec_dbg(codec, "enabling ADC switching\n");
3205 spec->dyn_adc_switch = 1;
3206 } else if (nums != spec->num_adc_nids) {
3207 /* shrink the invalid adcs and input paths */
3209 for (n = 0; n < spec->num_adc_nids; n++) {
3210 if (!(ok_bits & (1 << n)))
3213 spec->adc_nids[nums] = spec->adc_nids[n];
3214 for (i = 0; i < imux->num_items; i++) {
3215 invalidate_nid_path(codec,
3216 spec->input_paths[i][nums]);
3217 spec->input_paths[i][nums] =
3218 spec->input_paths[i][n];
3219 spec->input_paths[i][n] = 0;
3224 spec->num_adc_nids = nums;
3227 if (imux->num_items == 1 ||
3228 (imux->num_items == 2 && spec->hp_mic)) {
3229 codec_dbg(codec, "reducing to a single ADC\n");
3230 spec->num_adc_nids = 1; /* reduce to a single ADC */
3233 /* single index for individual volumes ctls */
3234 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3235 spec->num_adc_nids = 1;
3240 /* parse capture source paths from the given pin and create imux items */
3241 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3242 int cfg_idx, int num_adcs,
3243 const char *label, int anchor)
3245 struct hda_gen_spec *spec = codec->spec;
3246 struct hda_input_mux *imux = &spec->input_mux;
3247 int imux_idx = imux->num_items;
3248 bool imux_added = false;
3251 for (c = 0; c < num_adcs; c++) {
3252 struct nid_path *path;
3253 hda_nid_t adc = spec->adc_nids[c];
3255 if (!is_reachable_path(codec, pin, adc))
3257 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3260 print_nid_path(codec, "input", path);
3261 spec->input_paths[imux_idx][c] =
3262 snd_hda_get_path_idx(codec, path);
3265 if (spec->hp_mic_pin == pin)
3266 spec->hp_mic_mux_idx = imux->num_items;
3267 spec->imux_pins[imux->num_items] = pin;
3268 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3270 if (spec->dyn_adc_switch)
3271 spec->dyn_adc_idx[imux_idx] = c;
3279 * create playback/capture controls for input pins
3282 /* fill the label for each input at first */
3283 static int fill_input_pin_labels(struct hda_codec *codec)
3285 struct hda_gen_spec *spec = codec->spec;
3286 const struct auto_pin_cfg *cfg = &spec->autocfg;
3289 for (i = 0; i < cfg->num_inputs; i++) {
3290 hda_nid_t pin = cfg->inputs[i].pin;
3294 if (!is_input_pin(codec, pin))
3297 label = hda_get_autocfg_input_label(codec, cfg, i);
3299 for (j = i - 1; j >= 0; j--) {
3300 if (spec->input_labels[j] &&
3301 !strcmp(spec->input_labels[j], label)) {
3302 idx = spec->input_label_idxs[j] + 1;
3307 spec->input_labels[i] = label;
3308 spec->input_label_idxs[i] = idx;
3314 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3316 static int create_input_ctls(struct hda_codec *codec)
3318 struct hda_gen_spec *spec = codec->spec;
3319 const struct auto_pin_cfg *cfg = &spec->autocfg;
3320 hda_nid_t mixer = spec->mixer_nid;
3325 num_adcs = fill_adc_nids(codec);
3329 err = fill_input_pin_labels(codec);
3333 for (i = 0; i < cfg->num_inputs; i++) {
3336 pin = cfg->inputs[i].pin;
3337 if (!is_input_pin(codec, pin))
3341 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3342 val |= snd_hda_get_default_vref(codec, pin);
3343 if (pin != spec->hp_mic_pin &&
3344 !snd_hda_codec_get_pin_target(codec, pin))
3345 set_pin_target(codec, pin, val, false);
3348 if (is_reachable_path(codec, pin, mixer)) {
3349 err = new_analog_input(codec, i, pin,
3350 spec->input_labels[i],
3351 spec->input_label_idxs[i],
3358 err = parse_capture_source(codec, pin, i, num_adcs,
3359 spec->input_labels[i], -mixer);
3363 if (spec->add_jack_modes) {
3364 err = create_in_jack_mode(codec, pin);
3370 /* add stereo mix when explicitly enabled via hint */
3371 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3372 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3377 spec->suppress_auto_mic = 1;
3388 /* get the input path specified by the given adc and imux indices */
3389 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3391 struct hda_gen_spec *spec = codec->spec;
3392 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3396 if (spec->dyn_adc_switch)
3397 adc_idx = spec->dyn_adc_idx[imux_idx];
3398 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3402 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3405 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3408 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3409 struct snd_ctl_elem_info *uinfo)
3411 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3412 struct hda_gen_spec *spec = codec->spec;
3413 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3416 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3417 struct snd_ctl_elem_value *ucontrol)
3419 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3420 struct hda_gen_spec *spec = codec->spec;
3421 /* the ctls are created at once with multiple counts */
3422 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3424 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3428 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3429 struct snd_ctl_elem_value *ucontrol)
3431 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3432 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3433 return mux_select(codec, adc_idx,
3434 ucontrol->value.enumerated.item[0]);
3437 static const struct snd_kcontrol_new cap_src_temp = {
3438 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3439 .name = "Input Source",
3440 .info = mux_enum_info,
3441 .get = mux_enum_get,
3442 .put = mux_enum_put,
3446 * capture volume and capture switch ctls
3449 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3450 struct snd_ctl_elem_value *ucontrol);
3452 /* call the given amp update function for all amps in the imux list at once */
3453 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3454 struct snd_ctl_elem_value *ucontrol,
3455 put_call_t func, int type)
3457 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3458 struct hda_gen_spec *spec = codec->spec;
3459 const struct hda_input_mux *imux;
3460 struct nid_path *path;
3461 int i, adc_idx, err = 0;
3463 imux = &spec->input_mux;
3464 adc_idx = kcontrol->id.index;
3465 mutex_lock(&codec->control_mutex);
3466 for (i = 0; i < imux->num_items; i++) {
3467 path = get_input_path(codec, adc_idx, i);
3468 if (!path || !path->ctls[type])
3470 kcontrol->private_value = path->ctls[type];
3471 err = func(kcontrol, ucontrol);
3475 mutex_unlock(&codec->control_mutex);
3476 if (err >= 0 && spec->cap_sync_hook)
3477 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3481 /* capture volume ctl callbacks */
3482 #define cap_vol_info snd_hda_mixer_amp_volume_info
3483 #define cap_vol_get snd_hda_mixer_amp_volume_get
3484 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3486 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3487 struct snd_ctl_elem_value *ucontrol)
3489 return cap_put_caller(kcontrol, ucontrol,
3490 snd_hda_mixer_amp_volume_put,
3494 static const struct snd_kcontrol_new cap_vol_temp = {
3495 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3496 .name = "Capture Volume",
3497 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3498 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3499 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3500 .info = cap_vol_info,
3503 .tlv = { .c = cap_vol_tlv },
3506 /* capture switch ctl callbacks */
3507 #define cap_sw_info snd_ctl_boolean_stereo_info
3508 #define cap_sw_get snd_hda_mixer_amp_switch_get
3510 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3511 struct snd_ctl_elem_value *ucontrol)
3513 return cap_put_caller(kcontrol, ucontrol,
3514 snd_hda_mixer_amp_switch_put,
3518 static const struct snd_kcontrol_new cap_sw_temp = {
3519 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3520 .name = "Capture Switch",
3521 .info = cap_sw_info,
3526 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3531 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3532 for (depth = 0; depth < 3; depth++) {
3533 if (depth >= path->depth)
3535 i = path->depth - depth - 1;
3536 nid = path->path[i];
3537 if (!path->ctls[NID_PATH_VOL_CTL]) {
3538 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3539 path->ctls[NID_PATH_VOL_CTL] =
3540 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3541 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3542 int idx = path->idx[i];
3543 if (!depth && codec->single_adc_amp)
3545 path->ctls[NID_PATH_VOL_CTL] =
3546 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3549 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3550 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3551 path->ctls[NID_PATH_MUTE_CTL] =
3552 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3553 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3554 int idx = path->idx[i];
3555 if (!depth && codec->single_adc_amp)
3557 path->ctls[NID_PATH_MUTE_CTL] =
3558 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3565 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3567 struct hda_gen_spec *spec = codec->spec;
3568 struct auto_pin_cfg *cfg = &spec->autocfg;
3572 if (!spec->inv_dmic_split)
3574 for (i = 0; i < cfg->num_inputs; i++) {
3575 if (cfg->inputs[i].pin != nid)
3577 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3579 val = snd_hda_codec_get_pincfg(codec, nid);
3580 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3585 /* capture switch put callback for a single control with hook call */
3586 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3587 struct snd_ctl_elem_value *ucontrol)
3589 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3590 struct hda_gen_spec *spec = codec->spec;
3593 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3597 if (spec->cap_sync_hook)
3598 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3603 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3604 int idx, bool is_switch, unsigned int ctl,
3607 struct hda_gen_spec *spec = codec->spec;
3608 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3609 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3610 const char *sfx = is_switch ? "Switch" : "Volume";
3611 unsigned int chs = inv_dmic ? 1 : 3;
3612 struct snd_kcontrol_new *knew;
3618 snprintf(tmpname, sizeof(tmpname),
3619 "%s Capture %s", label, sfx);
3621 snprintf(tmpname, sizeof(tmpname),
3623 knew = add_control(spec, type, tmpname, idx,
3624 amp_val_replace_channels(ctl, chs));
3628 knew->put = cap_single_sw_put;
3632 /* Make independent right kcontrol */
3634 snprintf(tmpname, sizeof(tmpname),
3635 "Inverted %s Capture %s", label, sfx);
3637 snprintf(tmpname, sizeof(tmpname),
3638 "Inverted Capture %s", sfx);
3639 knew = add_control(spec, type, tmpname, idx,
3640 amp_val_replace_channels(ctl, 2));
3644 knew->put = cap_single_sw_put;
3648 /* create single (and simple) capture volume and switch controls */
3649 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3650 unsigned int vol_ctl, unsigned int sw_ctl,
3654 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3657 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3663 /* create bound capture volume and switch controls */
3664 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3665 unsigned int vol_ctl, unsigned int sw_ctl)
3667 struct hda_gen_spec *spec = codec->spec;
3668 struct snd_kcontrol_new *knew;
3671 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3675 knew->private_value = vol_ctl;
3676 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3679 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3683 knew->private_value = sw_ctl;
3684 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3689 /* return the vol ctl when used first in the imux list */
3690 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3692 struct nid_path *path;
3696 path = get_input_path(codec, 0, idx);
3699 ctl = path->ctls[type];
3702 for (i = 0; i < idx - 1; i++) {
3703 path = get_input_path(codec, 0, i);
3704 if (path && path->ctls[type] == ctl)
3710 /* create individual capture volume and switch controls per input */
3711 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3713 struct hda_gen_spec *spec = codec->spec;
3714 struct hda_input_mux *imux = &spec->input_mux;
3717 for (i = 0; i < imux->num_items; i++) {
3721 idx = imux->items[i].index;
3722 if (idx >= spec->autocfg.num_inputs)
3724 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3726 for (type = 0; type < 2; type++) {
3727 err = add_single_cap_ctl(codec,
3728 spec->input_labels[idx],
3729 spec->input_label_idxs[idx],
3731 get_first_cap_ctl(codec, i, type),
3740 static int create_capture_mixers(struct hda_codec *codec)
3742 struct hda_gen_spec *spec = codec->spec;
3743 struct hda_input_mux *imux = &spec->input_mux;
3744 int i, n, nums, err;
3746 if (spec->dyn_adc_switch)
3749 nums = spec->num_adc_nids;
3751 if (!spec->auto_mic && imux->num_items > 1) {
3752 struct snd_kcontrol_new *knew;
3754 name = nums > 1 ? "Input Source" : "Capture Source";
3755 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3761 for (n = 0; n < nums; n++) {
3763 bool multi_cap_vol = spec->multi_cap_vol;
3764 bool inv_dmic = false;
3768 for (i = 0; i < imux->num_items; i++) {
3769 struct nid_path *path;
3770 path = get_input_path(codec, n, i);
3773 parse_capvol_in_path(codec, path);
3775 vol = path->ctls[NID_PATH_VOL_CTL];
3776 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3778 if (!same_amp_caps(codec, vol,
3779 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3780 multi_cap_vol = true;
3783 sw = path->ctls[NID_PATH_MUTE_CTL];
3784 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3786 if (!same_amp_caps(codec, sw,
3787 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3788 multi_cap_vol = true;
3790 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3795 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3797 else if (!multi_cap_vol && !inv_dmic)
3798 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3800 err = create_multi_cap_vol_ctl(codec);
3809 * add mic boosts if needed
3812 /* check whether the given amp is feasible as a boost volume */
3813 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3818 if (!nid_has_volume(codec, nid, dir) ||
3819 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3820 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3823 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3824 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3830 /* look for a boost amp in a widget close to the pin */
3831 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3832 struct nid_path *path)
3834 unsigned int val = 0;
3838 for (depth = 0; depth < 3; depth++) {
3839 if (depth >= path->depth - 1)
3841 nid = path->path[depth];
3842 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3843 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3845 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3846 path->idx[depth])) {
3847 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3856 static int parse_mic_boost(struct hda_codec *codec)
3858 struct hda_gen_spec *spec = codec->spec;
3859 struct auto_pin_cfg *cfg = &spec->autocfg;
3860 struct hda_input_mux *imux = &spec->input_mux;
3863 if (!spec->num_adc_nids)
3866 for (i = 0; i < imux->num_items; i++) {
3867 struct nid_path *path;
3870 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3872 idx = imux->items[i].index;
3873 if (idx >= imux->num_items)
3876 /* check only line-in and mic pins */
3877 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3880 path = get_input_path(codec, 0, i);
3884 val = look_for_boost_amp(codec, path);
3888 /* create a boost control */
3889 snprintf(boost_label, sizeof(boost_label),
3890 "%s Boost Volume", spec->input_labels[idx]);
3891 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3892 spec->input_label_idxs[idx], val))
3895 path->ctls[NID_PATH_BOOST_CTL] = val;
3901 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3903 static void parse_digital(struct hda_codec *codec)
3905 struct hda_gen_spec *spec = codec->spec;
3906 struct nid_path *path;
3908 hda_nid_t dig_nid, pin;
3910 /* support multiple SPDIFs; the secondary is set up as a slave */
3912 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3913 pin = spec->autocfg.dig_out_pins[i];
3914 dig_nid = look_for_dac(codec, pin, true);
3917 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3920 print_nid_path(codec, "digout", path);
3921 path->active = true;
3922 path->pin_fixed = true; /* no jack detection */
3923 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3924 set_pin_target(codec, pin, PIN_OUT, false);
3926 spec->multiout.dig_out_nid = dig_nid;
3927 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3929 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3930 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3932 spec->slave_dig_outs[nums - 1] = dig_nid;
3937 if (spec->autocfg.dig_in_pin) {
3938 pin = spec->autocfg.dig_in_pin;
3939 for_each_hda_codec_node(dig_nid, codec) {
3940 unsigned int wcaps = get_wcaps(codec, dig_nid);
3941 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3943 if (!(wcaps & AC_WCAP_DIGITAL))
3945 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3947 print_nid_path(codec, "digin", path);
3948 path->active = true;
3949 path->pin_fixed = true; /* no jack */
3950 spec->dig_in_nid = dig_nid;
3951 spec->digin_path = snd_hda_get_path_idx(codec, path);
3952 set_pin_target(codec, pin, PIN_IN, false);
3961 * input MUX handling
3964 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3966 /* select the given imux item; either unmute exclusively or select the route */
3967 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3970 struct hda_gen_spec *spec = codec->spec;
3971 const struct hda_input_mux *imux;
3972 struct nid_path *old_path, *path;
3974 imux = &spec->input_mux;
3975 if (!imux->num_items)
3978 if (idx >= imux->num_items)
3979 idx = imux->num_items - 1;
3980 if (spec->cur_mux[adc_idx] == idx)
3983 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3986 if (old_path->active)
3987 snd_hda_activate_path(codec, old_path, false, false);
3989 spec->cur_mux[adc_idx] = idx;
3992 update_hp_mic(codec, adc_idx, false);
3994 if (spec->dyn_adc_switch)
3995 dyn_adc_pcm_resetup(codec, idx);
3997 path = get_input_path(codec, adc_idx, idx);
4002 snd_hda_activate_path(codec, path, true, false);
4003 if (spec->cap_sync_hook)
4004 spec->cap_sync_hook(codec, NULL, NULL);
4005 path_power_down_sync(codec, old_path);
4009 /* power up/down widgets in the all paths that match with the given NID
4010 * as terminals (either start- or endpoint)
4012 * returns the last changed NID, or zero if unchanged.
4014 static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
4015 int pin_state, int stream_state)
4017 struct hda_gen_spec *spec = codec->spec;
4018 hda_nid_t last, changed = 0;
4019 struct nid_path *path;
4022 for (n = 0; n < spec->paths.used; n++) {
4023 path = snd_array_elem(&spec->paths, n);
4026 if (path->path[0] == nid ||
4027 path->path[path->depth - 1] == nid) {
4028 bool pin_old = path->pin_enabled;
4029 bool stream_old = path->stream_enabled;
4032 path->pin_enabled = pin_state;
4033 if (stream_state >= 0)
4034 path->stream_enabled = stream_state;
4035 if ((!path->pin_fixed && path->pin_enabled != pin_old)
4036 || path->stream_enabled != stream_old) {
4037 last = path_power_update(codec, path, true);
4046 /* check the jack status for power control */
4047 static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4049 if (!is_jack_detectable(codec, pin))
4051 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4054 /* power up/down the paths of the given pin according to the jack state;
4055 * power = 0/1 : only power up/down if it matches with the jack state,
4056 * < 0 : force power up/down to follow the jack sate
4058 * returns the last changed NID, or zero if unchanged.
4060 static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4065 if (!codec->power_save_node)
4068 on = detect_pin_state(codec, pin);
4070 if (power >= 0 && on != power)
4072 return set_path_power(codec, pin, on, -1);
4075 static void pin_power_callback(struct hda_codec *codec,
4076 struct hda_jack_callback *jack,
4079 if (jack && jack->nid)
4080 sync_power_state_change(codec,
4081 set_pin_power_jack(codec, jack->nid, on));
4084 /* callback only doing power up -- called at first */
4085 static void pin_power_up_callback(struct hda_codec *codec,
4086 struct hda_jack_callback *jack)
4088 pin_power_callback(codec, jack, true);
4091 /* callback only doing power down -- called at last */
4092 static void pin_power_down_callback(struct hda_codec *codec,
4093 struct hda_jack_callback *jack)
4095 pin_power_callback(codec, jack, false);
4098 /* set up the power up/down callbacks */
4099 static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4100 const hda_nid_t *pins, bool on)
4103 hda_jack_callback_fn cb =
4104 on ? pin_power_up_callback : pin_power_down_callback;
4106 for (i = 0; i < num_pins && pins[i]; i++) {
4107 if (is_jack_detectable(codec, pins[i]))
4108 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4110 set_path_power(codec, pins[i], true, -1);
4114 /* enabled power callback to each available I/O pin with jack detections;
4115 * the digital I/O pins are excluded because of the unreliable detectsion
4117 static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4119 struct hda_gen_spec *spec = codec->spec;
4120 struct auto_pin_cfg *cfg = &spec->autocfg;
4123 if (!codec->power_save_node)
4125 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4126 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4127 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4128 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4129 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4130 for (i = 0; i < cfg->num_inputs; i++)
4131 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4134 /* sync path power up/down with the jack states of given pins */
4135 static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4136 const hda_nid_t *pins)
4140 for (i = 0; i < num_pins && pins[i]; i++)
4141 if (is_jack_detectable(codec, pins[i]))
4142 set_pin_power_jack(codec, pins[i], -1);
4145 /* sync path power up/down with pins; called at init and resume */
4146 static void sync_all_pin_power_ctls(struct hda_codec *codec)
4148 struct hda_gen_spec *spec = codec->spec;
4149 struct auto_pin_cfg *cfg = &spec->autocfg;
4152 if (!codec->power_save_node)
4154 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4155 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4156 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4157 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4158 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4159 for (i = 0; i < cfg->num_inputs; i++)
4160 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4163 /* add fake paths if not present yet */
4164 static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4165 int num_pins, const hda_nid_t *pins)
4167 struct hda_gen_spec *spec = codec->spec;
4168 struct nid_path *path;
4171 for (i = 0; i < num_pins; i++) {
4174 if (get_nid_path(codec, nid, pins[i], 0))
4176 path = snd_array_new(&spec->paths);
4179 memset(path, 0, sizeof(*path));
4181 path->path[0] = nid;
4182 path->path[1] = pins[i];
4183 path->active = true;
4188 /* create fake paths to all outputs from beep */
4189 static int add_fake_beep_paths(struct hda_codec *codec)
4191 struct hda_gen_spec *spec = codec->spec;
4192 struct auto_pin_cfg *cfg = &spec->autocfg;
4193 hda_nid_t nid = spec->beep_nid;
4196 if (!codec->power_save_node || !nid)
4198 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4201 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4202 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4206 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4207 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4215 /* power up/down beep widget and its output paths */
4216 static void beep_power_hook(struct hda_beep *beep, bool on)
4218 set_path_power(beep->codec, beep->nid, -1, on);
4222 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4223 * @codec: the HDA codec
4224 * @pin: NID of pin to fix
4226 int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4228 struct hda_gen_spec *spec = codec->spec;
4229 struct nid_path *path;
4231 path = snd_array_new(&spec->paths);
4234 memset(path, 0, sizeof(*path));
4236 path->path[0] = pin;
4237 path->active = true;
4238 path->pin_fixed = true;
4239 path->stream_enabled = true;
4242 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4245 * Jack detections for HP auto-mute and mic-switch
4248 /* check each pin in the given array; returns true if any of them is plugged */
4249 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4252 bool present = false;
4254 for (i = 0; i < num_pins; i++) {
4255 hda_nid_t nid = pins[i];
4258 /* don't detect pins retasked as inputs */
4259 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4261 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4267 /* standard HP/line-out auto-mute helper */
4268 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
4269 int *paths, bool mute)
4271 struct hda_gen_spec *spec = codec->spec;
4274 for (i = 0; i < num_pins; i++) {
4275 hda_nid_t nid = pins[i];
4276 unsigned int val, oldval;
4280 oldval = snd_hda_codec_get_pin_target(codec, nid);
4281 if (oldval & PIN_IN)
4282 continue; /* no mute for inputs */
4284 if (spec->auto_mute_via_amp) {
4285 struct nid_path *path;
4288 path = snd_hda_get_path_from_idx(codec, paths[i]);
4291 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4295 spec->mute_bits |= (1ULL << mute_nid);
4297 spec->mute_bits &= ~(1ULL << mute_nid);
4300 /* don't reset VREF value in case it's controlling
4301 * the amp (see alc861_fixup_asus_amp_vref_0f())
4303 if (spec->keep_vref_in_automute)
4304 val = oldval & ~PIN_HP;
4309 /* here we call update_pin_ctl() so that the pinctl is
4310 * changed without changing the pinctl target value;
4311 * the original target value will be still referred at
4312 * the init / resume again
4314 update_pin_ctl(codec, nid, val);
4317 set_pin_eapd(codec, nid, !mute);
4318 if (codec->power_save_node) {
4321 on = detect_pin_state(codec, nid);
4322 set_path_power(codec, nid, on, -1);
4328 * snd_hda_gen_update_outputs - Toggle outputs muting
4329 * @codec: the HDA codec
4331 * Update the mute status of all outputs based on the current jack states.
4333 void snd_hda_gen_update_outputs(struct hda_codec *codec)
4335 struct hda_gen_spec *spec = codec->spec;
4339 /* Control HP pins/amps depending on master_mute state;
4340 * in general, HP pins/amps control should be enabled in all cases,
4341 * but currently set only for master_mute, just to be safe
4343 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4344 paths = spec->out_paths;
4346 paths = spec->hp_paths;
4347 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
4348 spec->autocfg.hp_pins, paths, spec->master_mute);
4350 if (!spec->automute_speaker)
4353 on = spec->hp_jack_present | spec->line_jack_present;
4354 on |= spec->master_mute;
4355 spec->speaker_muted = on;
4356 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4357 paths = spec->out_paths;
4359 paths = spec->speaker_paths;
4360 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
4361 spec->autocfg.speaker_pins, paths, on);
4363 /* toggle line-out mutes if needed, too */
4364 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4365 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4366 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4368 if (!spec->automute_lo)
4371 on = spec->hp_jack_present;
4372 on |= spec->master_mute;
4373 spec->line_out_muted = on;
4374 paths = spec->out_paths;
4375 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4376 spec->autocfg.line_out_pins, paths, on);
4378 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4380 static void call_update_outputs(struct hda_codec *codec)
4382 struct hda_gen_spec *spec = codec->spec;
4383 if (spec->automute_hook)
4384 spec->automute_hook(codec);
4386 snd_hda_gen_update_outputs(codec);
4388 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4389 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4390 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4394 * snd_hda_gen_hp_automute - standard HP-automute helper
4395 * @codec: the HDA codec
4396 * @jack: jack object, NULL for the whole
4398 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4399 struct hda_jack_callback *jack)
4401 struct hda_gen_spec *spec = codec->spec;
4402 hda_nid_t *pins = spec->autocfg.hp_pins;
4403 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4405 /* No detection for the first HP jack during indep-HP mode */
4406 if (spec->indep_hp_enabled) {
4411 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4412 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4414 call_update_outputs(codec);
4416 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4419 * snd_hda_gen_line_automute - standard line-out-automute helper
4420 * @codec: the HDA codec
4421 * @jack: jack object, NULL for the whole
4423 void snd_hda_gen_line_automute(struct hda_codec *codec,
4424 struct hda_jack_callback *jack)
4426 struct hda_gen_spec *spec = codec->spec;
4428 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4430 /* check LO jack only when it's different from HP */
4431 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4434 spec->line_jack_present =
4435 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4436 spec->autocfg.line_out_pins);
4437 if (!spec->automute_speaker || !spec->detect_lo)
4439 call_update_outputs(codec);
4441 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4444 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4445 * @codec: the HDA codec
4446 * @jack: jack object, NULL for the whole
4448 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4449 struct hda_jack_callback *jack)
4451 struct hda_gen_spec *spec = codec->spec;
4454 if (!spec->auto_mic)
4457 for (i = spec->am_num_entries - 1; i > 0; i--) {
4458 hda_nid_t pin = spec->am_entry[i].pin;
4459 /* don't detect pins retasked as outputs */
4460 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4462 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4463 mux_select(codec, 0, spec->am_entry[i].idx);
4467 mux_select(codec, 0, spec->am_entry[0].idx);
4469 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4471 /* call appropriate hooks */
4472 static void call_hp_automute(struct hda_codec *codec,
4473 struct hda_jack_callback *jack)
4475 struct hda_gen_spec *spec = codec->spec;
4476 if (spec->hp_automute_hook)
4477 spec->hp_automute_hook(codec, jack);
4479 snd_hda_gen_hp_automute(codec, jack);
4482 static void call_line_automute(struct hda_codec *codec,
4483 struct hda_jack_callback *jack)
4485 struct hda_gen_spec *spec = codec->spec;
4486 if (spec->line_automute_hook)
4487 spec->line_automute_hook(codec, jack);
4489 snd_hda_gen_line_automute(codec, jack);
4492 static void call_mic_autoswitch(struct hda_codec *codec,
4493 struct hda_jack_callback *jack)
4495 struct hda_gen_spec *spec = codec->spec;
4496 if (spec->mic_autoswitch_hook)
4497 spec->mic_autoswitch_hook(codec, jack);
4499 snd_hda_gen_mic_autoswitch(codec, jack);
4502 /* update jack retasking */
4503 static void update_automute_all(struct hda_codec *codec)
4505 call_hp_automute(codec, NULL);
4506 call_line_automute(codec, NULL);
4507 call_mic_autoswitch(codec, NULL);
4511 * Auto-Mute mode mixer enum support
4513 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4514 struct snd_ctl_elem_info *uinfo)
4516 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4517 struct hda_gen_spec *spec = codec->spec;
4518 static const char * const texts3[] = {
4519 "Disabled", "Speaker Only", "Line Out+Speaker"
4522 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4523 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4524 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4527 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4528 struct snd_ctl_elem_value *ucontrol)
4530 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4531 struct hda_gen_spec *spec = codec->spec;
4532 unsigned int val = 0;
4533 if (spec->automute_speaker)
4535 if (spec->automute_lo)
4538 ucontrol->value.enumerated.item[0] = val;
4542 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4543 struct snd_ctl_elem_value *ucontrol)
4545 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4546 struct hda_gen_spec *spec = codec->spec;
4548 switch (ucontrol->value.enumerated.item[0]) {
4550 if (!spec->automute_speaker && !spec->automute_lo)
4552 spec->automute_speaker = 0;
4553 spec->automute_lo = 0;
4556 if (spec->automute_speaker_possible) {
4557 if (!spec->automute_lo && spec->automute_speaker)
4559 spec->automute_speaker = 1;
4560 spec->automute_lo = 0;
4561 } else if (spec->automute_lo_possible) {
4562 if (spec->automute_lo)
4564 spec->automute_lo = 1;
4569 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4571 if (spec->automute_speaker && spec->automute_lo)
4573 spec->automute_speaker = 1;
4574 spec->automute_lo = 1;
4579 call_update_outputs(codec);
4583 static const struct snd_kcontrol_new automute_mode_enum = {
4584 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4585 .name = "Auto-Mute Mode",
4586 .info = automute_mode_info,
4587 .get = automute_mode_get,
4588 .put = automute_mode_put,
4591 static int add_automute_mode_enum(struct hda_codec *codec)
4593 struct hda_gen_spec *spec = codec->spec;
4595 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4601 * Check the availability of HP/line-out auto-mute;
4602 * Set up appropriately if really supported
4604 static int check_auto_mute_availability(struct hda_codec *codec)
4606 struct hda_gen_spec *spec = codec->spec;
4607 struct auto_pin_cfg *cfg = &spec->autocfg;
4611 if (spec->suppress_auto_mute)
4614 if (cfg->hp_pins[0])
4616 if (cfg->line_out_pins[0])
4618 if (cfg->speaker_pins[0])
4620 if (present < 2) /* need two different output types */
4623 if (!cfg->speaker_pins[0] &&
4624 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4625 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4626 sizeof(cfg->speaker_pins));
4627 cfg->speaker_outs = cfg->line_outs;
4630 if (!cfg->hp_pins[0] &&
4631 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4632 memcpy(cfg->hp_pins, cfg->line_out_pins,
4633 sizeof(cfg->hp_pins));
4634 cfg->hp_outs = cfg->line_outs;
4637 for (i = 0; i < cfg->hp_outs; i++) {
4638 hda_nid_t nid = cfg->hp_pins[i];
4639 if (!is_jack_detectable(codec, nid))
4641 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4642 snd_hda_jack_detect_enable_callback(codec, nid,
4644 spec->detect_hp = 1;
4647 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4648 if (cfg->speaker_outs)
4649 for (i = 0; i < cfg->line_outs; i++) {
4650 hda_nid_t nid = cfg->line_out_pins[i];
4651 if (!is_jack_detectable(codec, nid))
4653 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4654 snd_hda_jack_detect_enable_callback(codec, nid,
4655 call_line_automute);
4656 spec->detect_lo = 1;
4658 spec->automute_lo_possible = spec->detect_hp;
4661 spec->automute_speaker_possible = cfg->speaker_outs &&
4662 (spec->detect_hp || spec->detect_lo);
4664 spec->automute_lo = spec->automute_lo_possible;
4665 spec->automute_speaker = spec->automute_speaker_possible;
4667 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4668 /* create a control for automute mode */
4669 err = add_automute_mode_enum(codec);
4676 /* check whether all auto-mic pins are valid; setup indices if OK */
4677 static bool auto_mic_check_imux(struct hda_codec *codec)
4679 struct hda_gen_spec *spec = codec->spec;
4680 const struct hda_input_mux *imux;
4683 imux = &spec->input_mux;
4684 for (i = 0; i < spec->am_num_entries; i++) {
4685 spec->am_entry[i].idx =
4686 find_idx_in_nid_list(spec->am_entry[i].pin,
4687 spec->imux_pins, imux->num_items);
4688 if (spec->am_entry[i].idx < 0)
4689 return false; /* no corresponding imux */
4692 /* we don't need the jack detection for the first pin */
4693 for (i = 1; i < spec->am_num_entries; i++)
4694 snd_hda_jack_detect_enable_callback(codec,
4695 spec->am_entry[i].pin,
4696 call_mic_autoswitch);
4700 static int compare_attr(const void *ap, const void *bp)
4702 const struct automic_entry *a = ap;
4703 const struct automic_entry *b = bp;
4704 return (int)(a->attr - b->attr);
4708 * Check the availability of auto-mic switch;
4709 * Set up if really supported
4711 static int check_auto_mic_availability(struct hda_codec *codec)
4713 struct hda_gen_spec *spec = codec->spec;
4714 struct auto_pin_cfg *cfg = &spec->autocfg;
4718 if (spec->suppress_auto_mic)
4723 for (i = 0; i < cfg->num_inputs; i++) {
4724 hda_nid_t nid = cfg->inputs[i].pin;
4726 attr = snd_hda_codec_get_pincfg(codec, nid);
4727 attr = snd_hda_get_input_pin_attr(attr);
4728 if (types & (1 << attr))
4729 return 0; /* already occupied */
4731 case INPUT_PIN_ATTR_INT:
4732 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4733 return 0; /* invalid type */
4735 case INPUT_PIN_ATTR_UNUSED:
4736 return 0; /* invalid entry */
4738 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4739 return 0; /* invalid type */
4740 if (!spec->line_in_auto_switch &&
4741 cfg->inputs[i].type != AUTO_PIN_MIC)
4742 return 0; /* only mic is allowed */
4743 if (!is_jack_detectable(codec, nid))
4744 return 0; /* no unsol support */
4747 if (num_pins >= MAX_AUTO_MIC_PINS)
4749 types |= (1 << attr);
4750 spec->am_entry[num_pins].pin = nid;
4751 spec->am_entry[num_pins].attr = attr;
4758 spec->am_num_entries = num_pins;
4759 /* sort the am_entry in the order of attr so that the pin with a
4760 * higher attr will be selected when the jack is plugged.
4762 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4763 compare_attr, NULL);
4765 if (!auto_mic_check_imux(codec))
4769 spec->num_adc_nids = 1;
4770 spec->cur_mux[0] = spec->am_entry[0].idx;
4771 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4772 spec->am_entry[0].pin,
4773 spec->am_entry[1].pin,
4774 spec->am_entry[2].pin);
4780 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4782 * @codec: the HDA codec
4783 * @nid: NID to evalute
4784 * @power_state: target power state
4786 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4788 unsigned int power_state)
4790 struct hda_gen_spec *spec = codec->spec;
4792 if (!spec->power_down_unused && !codec->power_save_node)
4794 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
4796 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4798 if (is_active_nid_for_any(codec, nid))
4802 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4804 /* mute all aamix inputs initially; parse up to the first leaves */
4805 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4808 const hda_nid_t *conn;
4811 nums = snd_hda_get_conn_list(codec, mix, &conn);
4812 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4813 for (i = 0; i < nums; i++) {
4815 update_amp(codec, mix, HDA_INPUT, i,
4816 0xff, HDA_AMP_MUTE);
4817 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4818 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4819 0xff, HDA_AMP_MUTE);
4824 * snd_hda_gen_stream_pm - Stream power management callback
4825 * @codec: the HDA codec
4826 * @nid: audio widget
4827 * @on: power on/off flag
4829 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
4831 void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4833 if (codec->power_save_node)
4834 set_path_power(codec, nid, -1, on);
4836 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4839 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4840 * set up the hda_gen_spec
4841 * @codec: the HDA codec
4842 * @cfg: Parsed pin configuration
4844 * return 1 if successful, 0 if the proper config is not found,
4845 * or a negative error code
4847 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4848 struct auto_pin_cfg *cfg)
4850 struct hda_gen_spec *spec = codec->spec;
4853 parse_user_hints(codec);
4855 if (spec->mixer_nid && !spec->mixer_merge_nid)
4856 spec->mixer_merge_nid = spec->mixer_nid;
4858 if (cfg != &spec->autocfg) {
4859 spec->autocfg = *cfg;
4860 cfg = &spec->autocfg;
4863 if (!spec->main_out_badness)
4864 spec->main_out_badness = &hda_main_out_badness;
4865 if (!spec->extra_out_badness)
4866 spec->extra_out_badness = &hda_extra_out_badness;
4868 fill_all_dac_nids(codec);
4870 if (!cfg->line_outs) {
4871 if (cfg->dig_outs || cfg->dig_in_pin) {
4872 spec->multiout.max_channels = 2;
4873 spec->no_analog = 1;
4876 if (!cfg->num_inputs && !cfg->dig_in_pin)
4877 return 0; /* can't find valid BIOS pin config */
4880 if (!spec->no_primary_hp &&
4881 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4882 cfg->line_outs <= cfg->hp_outs) {
4883 /* use HP as primary out */
4884 cfg->speaker_outs = cfg->line_outs;
4885 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4886 sizeof(cfg->speaker_pins));
4887 cfg->line_outs = cfg->hp_outs;
4888 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4890 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4891 cfg->line_out_type = AUTO_PIN_HP_OUT;
4894 err = parse_output_paths(codec);
4897 err = create_multi_channel_mode(codec);
4900 err = create_multi_out_ctls(codec, cfg);
4903 err = create_hp_out_ctls(codec);
4906 err = create_speaker_out_ctls(codec);
4909 err = create_indep_hp_ctls(codec);
4912 err = create_loopback_mixing_ctl(codec);
4915 err = create_hp_mic(codec);
4918 err = create_input_ctls(codec);
4922 /* add power-down pin callbacks at first */
4923 add_all_pin_power_ctls(codec, false);
4925 spec->const_channel_count = spec->ext_channel_count;
4926 /* check the multiple speaker and headphone pins */
4927 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4928 spec->const_channel_count = max(spec->const_channel_count,
4929 cfg->speaker_outs * 2);
4930 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4931 spec->const_channel_count = max(spec->const_channel_count,
4933 spec->multiout.max_channels = max(spec->ext_channel_count,
4934 spec->const_channel_count);
4936 err = check_auto_mute_availability(codec);
4940 err = check_dyn_adc_switch(codec);
4944 err = check_auto_mic_availability(codec);
4948 /* add stereo mix if available and not enabled yet */
4949 if (!spec->auto_mic && spec->mixer_nid &&
4950 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4951 spec->input_mux.num_items > 1) {
4952 err = parse_capture_source(codec, spec->mixer_nid,
4953 CFG_IDX_MIX, spec->num_all_adcs,
4960 err = create_capture_mixers(codec);
4964 err = parse_mic_boost(codec);
4968 /* create "Headphone Mic Jack Mode" if no input selection is
4969 * available (or user specifies add_jack_modes hint)
4971 if (spec->hp_mic_pin &&
4972 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4973 spec->add_jack_modes)) {
4974 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4979 if (spec->add_jack_modes) {
4980 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4981 err = create_out_jack_modes(codec, cfg->line_outs,
4982 cfg->line_out_pins);
4986 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4987 err = create_out_jack_modes(codec, cfg->hp_outs,
4994 /* add power-up pin callbacks at last */
4995 add_all_pin_power_ctls(codec, true);
4997 /* mute all aamix input initially */
4998 if (spec->mixer_nid)
4999 mute_all_mixer_nid(codec, spec->mixer_nid);
5002 parse_digital(codec);
5004 if (spec->power_down_unused || codec->power_save_node) {
5005 if (!codec->power_filter)
5006 codec->power_filter = snd_hda_gen_path_power_filter;
5007 if (!codec->patch_ops.stream_pm)
5008 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
5011 if (!spec->no_analog && spec->beep_nid) {
5012 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
5015 if (codec->beep && codec->power_save_node) {
5016 err = add_fake_beep_paths(codec);
5019 codec->beep->power_hook = beep_power_hook;
5025 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
5029 * Build control elements
5032 /* slave controls for virtual master */
5033 static const char * const slave_pfxs[] = {
5034 "Front", "Surround", "Center", "LFE", "Side",
5035 "Headphone", "Speaker", "Mono", "Line Out",
5036 "CLFE", "Bass Speaker", "PCM",
5037 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5038 "Headphone Front", "Headphone Surround", "Headphone CLFE",
5039 "Headphone Side", "Headphone+LO", "Speaker+LO",
5044 * snd_hda_gen_build_controls - Build controls from the parsed results
5045 * @codec: the HDA codec
5047 * Pass this to build_controls patch_ops.
5049 int snd_hda_gen_build_controls(struct hda_codec *codec)
5051 struct hda_gen_spec *spec = codec->spec;
5054 if (spec->kctls.used) {
5055 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5060 if (spec->multiout.dig_out_nid) {
5061 err = snd_hda_create_dig_out_ctls(codec,
5062 spec->multiout.dig_out_nid,
5063 spec->multiout.dig_out_nid,
5064 spec->pcm_rec[1]->pcm_type);
5067 if (!spec->no_analog) {
5068 err = snd_hda_create_spdif_share_sw(codec,
5072 spec->multiout.share_spdif = 1;
5075 if (spec->dig_in_nid) {
5076 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5081 /* if we have no master control, let's create it */
5082 if (!spec->no_analog && !spec->suppress_vmaster &&
5083 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
5084 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
5085 spec->vmaster_tlv, slave_pfxs,
5090 if (!spec->no_analog && !spec->suppress_vmaster &&
5091 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5092 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5095 true, &spec->vmaster_mute.sw_kctl);
5098 if (spec->vmaster_mute.hook) {
5099 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5100 spec->vmaster_mute_enum);
5101 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5105 free_kctls(spec); /* no longer needed */
5107 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5113 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
5120 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5121 struct hda_codec *codec,
5122 struct snd_pcm_substream *substream,
5125 struct hda_gen_spec *spec = codec->spec;
5126 if (spec->pcm_playback_hook)
5127 spec->pcm_playback_hook(hinfo, codec, substream, action);
5130 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5131 struct hda_codec *codec,
5132 struct snd_pcm_substream *substream,
5135 struct hda_gen_spec *spec = codec->spec;
5136 if (spec->pcm_capture_hook)
5137 spec->pcm_capture_hook(hinfo, codec, substream, action);
5141 * Analog playback callbacks
5143 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5144 struct hda_codec *codec,
5145 struct snd_pcm_substream *substream)
5147 struct hda_gen_spec *spec = codec->spec;
5150 mutex_lock(&spec->pcm_mutex);
5151 err = snd_hda_multi_out_analog_open(codec,
5152 &spec->multiout, substream,
5155 spec->active_streams |= 1 << STREAM_MULTI_OUT;
5156 call_pcm_playback_hook(hinfo, codec, substream,
5157 HDA_GEN_PCM_ACT_OPEN);
5159 mutex_unlock(&spec->pcm_mutex);
5163 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5164 struct hda_codec *codec,
5165 unsigned int stream_tag,
5166 unsigned int format,
5167 struct snd_pcm_substream *substream)
5169 struct hda_gen_spec *spec = codec->spec;
5172 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5173 stream_tag, format, substream);
5175 call_pcm_playback_hook(hinfo, codec, substream,
5176 HDA_GEN_PCM_ACT_PREPARE);
5180 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5181 struct hda_codec *codec,
5182 struct snd_pcm_substream *substream)
5184 struct hda_gen_spec *spec = codec->spec;
5187 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5189 call_pcm_playback_hook(hinfo, codec, substream,
5190 HDA_GEN_PCM_ACT_CLEANUP);
5194 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5195 struct hda_codec *codec,
5196 struct snd_pcm_substream *substream)
5198 struct hda_gen_spec *spec = codec->spec;
5199 mutex_lock(&spec->pcm_mutex);
5200 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
5201 call_pcm_playback_hook(hinfo, codec, substream,
5202 HDA_GEN_PCM_ACT_CLOSE);
5203 mutex_unlock(&spec->pcm_mutex);
5207 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5208 struct hda_codec *codec,
5209 struct snd_pcm_substream *substream)
5211 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5215 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5216 struct hda_codec *codec,
5217 unsigned int stream_tag,
5218 unsigned int format,
5219 struct snd_pcm_substream *substream)
5221 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5222 call_pcm_capture_hook(hinfo, codec, substream,
5223 HDA_GEN_PCM_ACT_PREPARE);
5227 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5228 struct hda_codec *codec,
5229 struct snd_pcm_substream *substream)
5231 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5232 call_pcm_capture_hook(hinfo, codec, substream,
5233 HDA_GEN_PCM_ACT_CLEANUP);
5237 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5238 struct hda_codec *codec,
5239 struct snd_pcm_substream *substream)
5241 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5245 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5246 struct hda_codec *codec,
5247 struct snd_pcm_substream *substream)
5249 struct hda_gen_spec *spec = codec->spec;
5252 mutex_lock(&spec->pcm_mutex);
5253 if (spec->indep_hp && !spec->indep_hp_enabled)
5256 spec->active_streams |= 1 << STREAM_INDEP_HP;
5257 call_pcm_playback_hook(hinfo, codec, substream,
5258 HDA_GEN_PCM_ACT_OPEN);
5259 mutex_unlock(&spec->pcm_mutex);
5263 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5264 struct hda_codec *codec,
5265 struct snd_pcm_substream *substream)
5267 struct hda_gen_spec *spec = codec->spec;
5268 mutex_lock(&spec->pcm_mutex);
5269 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
5270 call_pcm_playback_hook(hinfo, codec, substream,
5271 HDA_GEN_PCM_ACT_CLOSE);
5272 mutex_unlock(&spec->pcm_mutex);
5276 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5277 struct hda_codec *codec,
5278 unsigned int stream_tag,
5279 unsigned int format,
5280 struct snd_pcm_substream *substream)
5282 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5283 call_pcm_playback_hook(hinfo, codec, substream,
5284 HDA_GEN_PCM_ACT_PREPARE);
5288 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5289 struct hda_codec *codec,
5290 struct snd_pcm_substream *substream)
5292 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5293 call_pcm_playback_hook(hinfo, codec, substream,
5294 HDA_GEN_PCM_ACT_CLEANUP);
5301 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5302 struct hda_codec *codec,
5303 struct snd_pcm_substream *substream)
5305 struct hda_gen_spec *spec = codec->spec;
5306 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5309 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5310 struct hda_codec *codec,
5311 unsigned int stream_tag,
5312 unsigned int format,
5313 struct snd_pcm_substream *substream)
5315 struct hda_gen_spec *spec = codec->spec;
5316 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5317 stream_tag, format, substream);
5320 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5321 struct hda_codec *codec,
5322 struct snd_pcm_substream *substream)
5324 struct hda_gen_spec *spec = codec->spec;
5325 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5328 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5329 struct hda_codec *codec,
5330 struct snd_pcm_substream *substream)
5332 struct hda_gen_spec *spec = codec->spec;
5333 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5339 #define alt_capture_pcm_open capture_pcm_open
5340 #define alt_capture_pcm_close capture_pcm_close
5342 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5343 struct hda_codec *codec,
5344 unsigned int stream_tag,
5345 unsigned int format,
5346 struct snd_pcm_substream *substream)
5348 struct hda_gen_spec *spec = codec->spec;
5350 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5351 stream_tag, 0, format);
5352 call_pcm_capture_hook(hinfo, codec, substream,
5353 HDA_GEN_PCM_ACT_PREPARE);
5357 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5358 struct hda_codec *codec,
5359 struct snd_pcm_substream *substream)
5361 struct hda_gen_spec *spec = codec->spec;
5363 snd_hda_codec_cleanup_stream(codec,
5364 spec->adc_nids[substream->number + 1]);
5365 call_pcm_capture_hook(hinfo, codec, substream,
5366 HDA_GEN_PCM_ACT_CLEANUP);
5372 static const struct hda_pcm_stream pcm_analog_playback = {
5376 /* NID is set in build_pcms */
5378 .open = playback_pcm_open,
5379 .close = playback_pcm_close,
5380 .prepare = playback_pcm_prepare,
5381 .cleanup = playback_pcm_cleanup
5385 static const struct hda_pcm_stream pcm_analog_capture = {
5389 /* NID is set in build_pcms */
5391 .open = capture_pcm_open,
5392 .close = capture_pcm_close,
5393 .prepare = capture_pcm_prepare,
5394 .cleanup = capture_pcm_cleanup
5398 static const struct hda_pcm_stream pcm_analog_alt_playback = {
5402 /* NID is set in build_pcms */
5404 .open = alt_playback_pcm_open,
5405 .close = alt_playback_pcm_close,
5406 .prepare = alt_playback_pcm_prepare,
5407 .cleanup = alt_playback_pcm_cleanup
5411 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5412 .substreams = 2, /* can be overridden */
5415 /* NID is set in build_pcms */
5417 .open = alt_capture_pcm_open,
5418 .close = alt_capture_pcm_close,
5419 .prepare = alt_capture_pcm_prepare,
5420 .cleanup = alt_capture_pcm_cleanup
5424 static const struct hda_pcm_stream pcm_digital_playback = {
5428 /* NID is set in build_pcms */
5430 .open = dig_playback_pcm_open,
5431 .close = dig_playback_pcm_close,
5432 .prepare = dig_playback_pcm_prepare,
5433 .cleanup = dig_playback_pcm_cleanup
5437 static const struct hda_pcm_stream pcm_digital_capture = {
5441 /* NID is set in build_pcms */
5444 /* Used by build_pcms to flag that a PCM has no playback stream */
5445 static const struct hda_pcm_stream pcm_null_stream = {
5452 * dynamic changing ADC PCM streams
5454 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5456 struct hda_gen_spec *spec = codec->spec;
5457 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5459 if (spec->cur_adc && spec->cur_adc != new_adc) {
5460 /* stream is running, let's swap the current ADC */
5461 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5462 spec->cur_adc = new_adc;
5463 snd_hda_codec_setup_stream(codec, new_adc,
5464 spec->cur_adc_stream_tag, 0,
5465 spec->cur_adc_format);
5471 /* analog capture with dynamic dual-adc changes */
5472 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5473 struct hda_codec *codec,
5474 unsigned int stream_tag,
5475 unsigned int format,
5476 struct snd_pcm_substream *substream)
5478 struct hda_gen_spec *spec = codec->spec;
5479 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5480 spec->cur_adc_stream_tag = stream_tag;
5481 spec->cur_adc_format = format;
5482 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5483 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
5487 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5488 struct hda_codec *codec,
5489 struct snd_pcm_substream *substream)
5491 struct hda_gen_spec *spec = codec->spec;
5492 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5494 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
5498 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5502 .nid = 0, /* fill later */
5504 .prepare = dyn_adc_capture_pcm_prepare,
5505 .cleanup = dyn_adc_capture_pcm_cleanup
5509 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5510 const char *chip_name)
5516 strlcpy(str, chip_name, len);
5518 /* drop non-alnum chars after a space */
5519 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5520 if (!isalnum(p[1])) {
5525 strlcat(str, sfx, len);
5528 /* copy PCM stream info from @default_str, and override non-NULL entries
5529 * from @spec_str and @nid
5531 static void setup_pcm_stream(struct hda_pcm_stream *str,
5532 const struct hda_pcm_stream *default_str,
5533 const struct hda_pcm_stream *spec_str,
5536 *str = *default_str;
5540 if (spec_str->substreams)
5541 str->substreams = spec_str->substreams;
5542 if (spec_str->channels_min)
5543 str->channels_min = spec_str->channels_min;
5544 if (spec_str->channels_max)
5545 str->channels_max = spec_str->channels_max;
5546 if (spec_str->rates)
5547 str->rates = spec_str->rates;
5548 if (spec_str->formats)
5549 str->formats = spec_str->formats;
5550 if (spec_str->maxbps)
5551 str->maxbps = spec_str->maxbps;
5556 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5557 * @codec: the HDA codec
5559 * Pass this to build_pcms patch_ops.
5561 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5563 struct hda_gen_spec *spec = codec->spec;
5564 struct hda_pcm *info;
5565 bool have_multi_adcs;
5567 if (spec->no_analog)
5570 fill_pcm_stream_name(spec->stream_name_analog,
5571 sizeof(spec->stream_name_analog),
5572 " Analog", codec->core.chip_name);
5573 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5576 spec->pcm_rec[0] = info;
5578 if (spec->multiout.num_dacs > 0) {
5579 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5580 &pcm_analog_playback,
5581 spec->stream_analog_playback,
5582 spec->multiout.dac_nids[0]);
5583 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5584 spec->multiout.max_channels;
5585 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5586 spec->autocfg.line_outs == 2)
5587 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5590 if (spec->num_adc_nids) {
5591 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5592 (spec->dyn_adc_switch ?
5593 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5594 spec->stream_analog_capture,
5599 /* SPDIF for stream index #1 */
5600 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5601 fill_pcm_stream_name(spec->stream_name_digital,
5602 sizeof(spec->stream_name_digital),
5603 " Digital", codec->core.chip_name);
5604 info = snd_hda_codec_pcm_new(codec, "%s",
5605 spec->stream_name_digital);
5608 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5609 spec->pcm_rec[1] = info;
5610 if (spec->dig_out_type)
5611 info->pcm_type = spec->dig_out_type;
5613 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5614 if (spec->multiout.dig_out_nid)
5615 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5616 &pcm_digital_playback,
5617 spec->stream_digital_playback,
5618 spec->multiout.dig_out_nid);
5619 if (spec->dig_in_nid)
5620 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5621 &pcm_digital_capture,
5622 spec->stream_digital_capture,
5626 if (spec->no_analog)
5629 /* If the use of more than one ADC is requested for the current
5630 * model, configure a second analog capture-only PCM.
5632 have_multi_adcs = (spec->num_adc_nids > 1) &&
5633 !spec->dyn_adc_switch && !spec->auto_mic;
5634 /* Additional Analaog capture for index #2 */
5635 if (spec->alt_dac_nid || have_multi_adcs) {
5636 fill_pcm_stream_name(spec->stream_name_alt_analog,
5637 sizeof(spec->stream_name_alt_analog),
5638 " Alt Analog", codec->core.chip_name);
5639 info = snd_hda_codec_pcm_new(codec, "%s",
5640 spec->stream_name_alt_analog);
5643 spec->pcm_rec[2] = info;
5644 if (spec->alt_dac_nid)
5645 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5646 &pcm_analog_alt_playback,
5647 spec->stream_analog_alt_playback,
5650 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5651 &pcm_null_stream, NULL, 0);
5652 if (have_multi_adcs) {
5653 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5654 &pcm_analog_alt_capture,
5655 spec->stream_analog_alt_capture,
5657 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5658 spec->num_adc_nids - 1;
5660 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5661 &pcm_null_stream, NULL, 0);
5667 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5671 * Standard auto-parser initializations
5674 /* configure the given path as a proper output */
5675 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5677 struct nid_path *path;
5680 path = snd_hda_get_path_from_idx(codec, path_idx);
5681 if (!path || !path->depth)
5683 pin = path->path[path->depth - 1];
5684 restore_pin_ctl(codec, pin);
5685 snd_hda_activate_path(codec, path, path->active,
5686 aamix_default(codec->spec));
5687 set_pin_eapd(codec, pin, path->active);
5690 /* initialize primary output paths */
5691 static void init_multi_out(struct hda_codec *codec)
5693 struct hda_gen_spec *spec = codec->spec;
5696 for (i = 0; i < spec->autocfg.line_outs; i++)
5697 set_output_and_unmute(codec, spec->out_paths[i]);
5701 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5705 for (i = 0; i < num_outs; i++)
5706 set_output_and_unmute(codec, paths[i]);
5709 /* initialize hp and speaker paths */
5710 static void init_extra_out(struct hda_codec *codec)
5712 struct hda_gen_spec *spec = codec->spec;
5714 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5715 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5716 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5717 __init_extra_out(codec, spec->autocfg.speaker_outs,
5718 spec->speaker_paths);
5721 /* initialize multi-io paths */
5722 static void init_multi_io(struct hda_codec *codec)
5724 struct hda_gen_spec *spec = codec->spec;
5727 for (i = 0; i < spec->multi_ios; i++) {
5728 hda_nid_t pin = spec->multi_io[i].pin;
5729 struct nid_path *path;
5730 path = get_multiio_path(codec, i);
5733 if (!spec->multi_io[i].ctl_in)
5734 spec->multi_io[i].ctl_in =
5735 snd_hda_codec_get_pin_target(codec, pin);
5736 snd_hda_activate_path(codec, path, path->active,
5737 aamix_default(spec));
5741 static void init_aamix_paths(struct hda_codec *codec)
5743 struct hda_gen_spec *spec = codec->spec;
5745 if (!spec->have_aamix_ctl)
5747 if (!has_aamix_out_paths(spec))
5749 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5750 spec->aamix_out_paths[0],
5751 spec->autocfg.line_out_type);
5752 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5753 spec->aamix_out_paths[1],
5755 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5756 spec->aamix_out_paths[2],
5757 AUTO_PIN_SPEAKER_OUT);
5760 /* set up input pins and loopback paths */
5761 static void init_analog_input(struct hda_codec *codec)
5763 struct hda_gen_spec *spec = codec->spec;
5764 struct auto_pin_cfg *cfg = &spec->autocfg;
5767 for (i = 0; i < cfg->num_inputs; i++) {
5768 hda_nid_t nid = cfg->inputs[i].pin;
5769 if (is_input_pin(codec, nid))
5770 restore_pin_ctl(codec, nid);
5772 /* init loopback inputs */
5773 if (spec->mixer_nid) {
5774 resume_path_from_idx(codec, spec->loopback_paths[i]);
5775 resume_path_from_idx(codec, spec->loopback_merge_path);
5780 /* initialize ADC paths */
5781 static void init_input_src(struct hda_codec *codec)
5783 struct hda_gen_spec *spec = codec->spec;
5784 struct hda_input_mux *imux = &spec->input_mux;
5785 struct nid_path *path;
5788 if (spec->dyn_adc_switch)
5791 nums = spec->num_adc_nids;
5793 for (c = 0; c < nums; c++) {
5794 for (i = 0; i < imux->num_items; i++) {
5795 path = get_input_path(codec, c, i);
5797 bool active = path->active;
5798 if (i == spec->cur_mux[c])
5800 snd_hda_activate_path(codec, path, active, false);
5804 update_hp_mic(codec, c, true);
5807 if (spec->cap_sync_hook)
5808 spec->cap_sync_hook(codec, NULL, NULL);
5811 /* set right pin controls for digital I/O */
5812 static void init_digital(struct hda_codec *codec)
5814 struct hda_gen_spec *spec = codec->spec;
5818 for (i = 0; i < spec->autocfg.dig_outs; i++)
5819 set_output_and_unmute(codec, spec->digout_paths[i]);
5820 pin = spec->autocfg.dig_in_pin;
5822 restore_pin_ctl(codec, pin);
5823 resume_path_from_idx(codec, spec->digin_path);
5827 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5828 * invalid unsol tags by some reason
5830 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5834 for (i = 0; i < codec->init_pins.used; i++) {
5835 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5836 hda_nid_t nid = pin->nid;
5837 if (is_jack_detectable(codec, nid) &&
5838 !snd_hda_jack_tbl_get(codec, nid))
5839 snd_hda_codec_update_cache(codec, nid, 0,
5840 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5845 * snd_hda_gen_init - initialize the generic spec
5846 * @codec: the HDA codec
5848 * This can be put as patch_ops init function.
5850 int snd_hda_gen_init(struct hda_codec *codec)
5852 struct hda_gen_spec *spec = codec->spec;
5854 if (spec->init_hook)
5855 spec->init_hook(codec);
5857 snd_hda_apply_verbs(codec);
5859 init_multi_out(codec);
5860 init_extra_out(codec);
5861 init_multi_io(codec);
5862 init_aamix_paths(codec);
5863 init_analog_input(codec);
5864 init_input_src(codec);
5865 init_digital(codec);
5867 clear_unsol_on_unused_pins(codec);
5869 sync_all_pin_power_ctls(codec);
5871 /* call init functions of standard auto-mute helpers */
5872 update_automute_all(codec);
5874 regcache_sync(codec->core.regmap);
5876 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5877 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5879 hda_call_check_power_status(codec, 0x01);
5882 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
5885 * snd_hda_gen_free - free the generic spec
5886 * @codec: the HDA codec
5888 * This can be put as patch_ops free function.
5890 void snd_hda_gen_free(struct hda_codec *codec)
5892 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
5893 snd_hda_gen_spec_free(codec->spec);
5897 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
5901 * snd_hda_gen_check_power_status - check the loopback power save state
5902 * @codec: the HDA codec
5903 * @nid: NID to inspect
5905 * This can be put as patch_ops check_power_status function.
5907 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5909 struct hda_gen_spec *spec = codec->spec;
5910 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5912 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
5917 * the generic codec support
5920 static const struct hda_codec_ops generic_patch_ops = {
5921 .build_controls = snd_hda_gen_build_controls,
5922 .build_pcms = snd_hda_gen_build_pcms,
5923 .init = snd_hda_gen_init,
5924 .free = snd_hda_gen_free,
5925 .unsol_event = snd_hda_jack_unsol_event,
5927 .check_power_status = snd_hda_gen_check_power_status,
5932 * snd_hda_parse_generic_codec - Generic codec parser
5933 * @codec: the HDA codec
5935 static int snd_hda_parse_generic_codec(struct hda_codec *codec)
5937 struct hda_gen_spec *spec;
5940 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5943 snd_hda_gen_spec_init(spec);
5946 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5950 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5954 codec->patch_ops = generic_patch_ops;
5958 snd_hda_gen_free(codec);
5962 static const struct hda_device_id snd_hda_id_generic[] = {
5963 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
5966 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
5968 static struct hda_codec_driver generic_driver = {
5969 .id = snd_hda_id_generic,
5972 module_hda_codec_driver(generic_driver);
5974 MODULE_LICENSE("GPL");
5975 MODULE_DESCRIPTION("Generic HD-audio codec parser");