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_write_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;
267 struct nid_path *path;
270 snd_array_for_each(&spec->paths, i, path) {
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;
328 const struct nid_path *path;
331 snd_array_for_each(&spec->paths, i, path) {
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;
354 const struct nid_path *path;
357 val &= AMP_VAL_COMPARE_MASK;
358 snd_array_for_each(&spec->paths, i, path) {
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));
641 const struct nid_path *path;
644 if (nid == codec->core.afg)
647 snd_array_for_each(&spec->paths, n, path) {
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);
800 type = get_wcaps_type(get_wcaps(codec, nid));
801 if (type == AC_WID_PIN ||
802 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
808 for (n = 0; n < nums; n++)
809 init_amp(codec, nid, HDA_INPUT, n);
811 /* here is a little bit tricky in comparison with activate_amp_out();
812 * when aa-mixer is available, we need to enable the path as well
814 for (n = 0; n < nums; n++) {
816 if (conn[n] != spec->mixer_merge_nid)
818 /* when aamix is disabled, force to off */
820 activate_amp(codec, nid, HDA_INPUT, n, n, false);
824 check_and_activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
828 /* sync power of each widget in the given path */
829 static hda_nid_t path_power_update(struct hda_codec *codec,
830 struct nid_path *path,
831 bool allow_powerdown)
833 hda_nid_t nid, changed = 0;
836 for (i = 0; i < path->depth; i++) {
838 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
840 if (nid == codec->core.afg)
842 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
846 power = snd_hda_codec_read(codec, nid, 0,
847 AC_VERB_GET_POWER_STATE, 0);
848 if (power != (state | (state << 4))) {
849 snd_hda_codec_write(codec, nid, 0,
850 AC_VERB_SET_POWER_STATE, state);
852 /* all known codecs seem to be capable to handl
853 * widgets state even in D3, so far.
854 * if any new codecs need to restore the widget
855 * states after D0 transition, call the function
859 if (state == AC_PWRST_D0)
860 snd_hdac_regmap_sync_node(&codec->core, nid);
867 /* do sync with the last power state change */
868 static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
872 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
877 * snd_hda_activate_path - activate or deactivate the given path
878 * @codec: the HDA codec
879 * @path: the path to activate/deactivate
880 * @enable: flag to activate or not
881 * @add_aamix: enable the input from aamix NID
883 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
885 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
886 bool enable, bool add_aamix)
888 struct hda_gen_spec *spec = codec->spec;
891 path->active = enable;
893 /* make sure the widget is powered up */
894 if (enable && (spec->power_down_unused || codec->power_save_node))
895 path_power_update(codec, path, codec->power_save_node);
897 for (i = path->depth - 1; i >= 0; i--) {
898 hda_nid_t nid = path->path[i];
900 if (enable && path->multi[i])
901 snd_hda_codec_write_cache(codec, nid, 0,
902 AC_VERB_SET_CONNECT_SEL,
904 if (has_amp_in(codec, path, i))
905 activate_amp_in(codec, path, i, enable, add_aamix);
906 if (has_amp_out(codec, path, i))
907 activate_amp_out(codec, path, i, enable);
910 EXPORT_SYMBOL_GPL(snd_hda_activate_path);
912 /* if the given path is inactive, put widgets into D3 (only if suitable) */
913 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
915 struct hda_gen_spec *spec = codec->spec;
917 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
919 sync_power_state_change(codec, path_power_update(codec, path, true));
922 /* turn on/off EAPD on the given pin */
923 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
925 struct hda_gen_spec *spec = codec->spec;
926 if (spec->own_eapd_ctl ||
927 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
929 if (spec->keep_eapd_on && !enable)
933 snd_hda_codec_write_cache(codec, pin, 0,
934 AC_VERB_SET_EAPD_BTLENABLE,
935 enable ? 0x02 : 0x00);
938 /* re-initialize the path specified by the given path index */
939 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
941 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
943 snd_hda_activate_path(codec, path, path->active, false);
948 * Helper functions for creating mixer ctl elements
951 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
952 struct snd_ctl_elem_value *ucontrol);
953 static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
954 struct snd_ctl_elem_value *ucontrol);
955 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
956 struct snd_ctl_elem_value *ucontrol);
963 static const struct snd_kcontrol_new control_templates[] = {
964 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
965 /* only the put callback is replaced for handling the special mute */
967 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
968 .subdevice = HDA_SUBDEV_AMP_FLAG,
969 .info = snd_hda_mixer_amp_switch_info,
970 .get = snd_hda_mixer_amp_switch_get,
971 .put = hda_gen_mixer_mute_put, /* replaced */
972 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
975 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
976 .info = snd_hda_mixer_amp_switch_info,
977 .get = hda_gen_bind_mute_get,
978 .put = hda_gen_bind_mute_put, /* replaced */
979 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
983 /* add dynamic controls from template */
984 static struct snd_kcontrol_new *
985 add_control(struct hda_gen_spec *spec, int type, const char *name,
986 int cidx, unsigned long val)
988 struct snd_kcontrol_new *knew;
990 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
994 if (get_amp_nid_(val))
995 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
996 knew->private_value = val;
1000 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
1001 const char *pfx, const char *dir,
1002 const char *sfx, int cidx, unsigned long val)
1004 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1005 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
1006 if (!add_control(spec, type, name, cidx, val))
1011 #define add_pb_vol_ctrl(spec, type, pfx, val) \
1012 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1013 #define add_pb_sw_ctrl(spec, type, pfx, val) \
1014 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1015 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1016 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1017 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1018 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1020 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1021 unsigned int chs, struct nid_path *path)
1026 val = path->ctls[NID_PATH_VOL_CTL];
1029 val = amp_val_replace_channels(val, chs);
1030 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1033 /* return the channel bits suitable for the given path->ctls[] */
1034 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1037 int chs = 1; /* mono (left only) */
1039 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1040 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1041 chs = 3; /* stereo */
1046 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1047 struct nid_path *path)
1049 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1050 return add_vol_ctl(codec, pfx, cidx, chs, path);
1053 /* create a mute-switch for the given mixer widget;
1054 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1056 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1057 unsigned int chs, struct nid_path *path)
1060 int type = HDA_CTL_WIDGET_MUTE;
1064 val = path->ctls[NID_PATH_MUTE_CTL];
1067 val = amp_val_replace_channels(val, chs);
1068 if (get_amp_direction_(val) == HDA_INPUT) {
1069 hda_nid_t nid = get_amp_nid_(val);
1070 int nums = snd_hda_get_num_conns(codec, nid);
1072 type = HDA_CTL_BIND_MUTE;
1076 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1079 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1080 int cidx, struct nid_path *path)
1082 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1083 return add_sw_ctl(codec, pfx, cidx, chs, path);
1086 /* playback mute control with the software mute bit check */
1087 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1088 struct snd_ctl_elem_value *ucontrol)
1090 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1091 struct hda_gen_spec *spec = codec->spec;
1093 if (spec->auto_mute_via_amp) {
1094 hda_nid_t nid = get_amp_nid(kcontrol);
1095 bool enabled = !((spec->mute_bits >> nid) & 1);
1096 ucontrol->value.integer.value[0] &= enabled;
1097 ucontrol->value.integer.value[1] &= enabled;
1101 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1102 struct snd_ctl_elem_value *ucontrol)
1104 sync_auto_mute_bits(kcontrol, ucontrol);
1105 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1109 * Bound mute controls
1111 #define AMP_VAL_IDX_SHIFT 19
1112 #define AMP_VAL_IDX_MASK (0x0f<<19)
1114 static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
1115 struct snd_ctl_elem_value *ucontrol)
1117 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1121 mutex_lock(&codec->control_mutex);
1122 pval = kcontrol->private_value;
1123 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1124 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1125 kcontrol->private_value = pval;
1126 mutex_unlock(&codec->control_mutex);
1130 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1131 struct snd_ctl_elem_value *ucontrol)
1133 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1135 int i, indices, err = 0, change = 0;
1137 sync_auto_mute_bits(kcontrol, ucontrol);
1139 mutex_lock(&codec->control_mutex);
1140 pval = kcontrol->private_value;
1141 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1142 for (i = 0; i < indices; i++) {
1143 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1144 (i << AMP_VAL_IDX_SHIFT);
1145 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1150 kcontrol->private_value = pval;
1151 mutex_unlock(&codec->control_mutex);
1152 return err < 0 ? err : change;
1155 /* any ctl assigned to the path with the given index? */
1156 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1158 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1159 return path && path->ctls[ctl_type];
1162 static const char * const channel_name[4] = {
1163 "Front", "Surround", "CLFE", "Side"
1166 /* give some appropriate ctl name prefix for the given line out channel */
1167 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1168 int *index, int ctl_type)
1170 struct hda_gen_spec *spec = codec->spec;
1171 struct auto_pin_cfg *cfg = &spec->autocfg;
1174 if (cfg->line_outs == 1 && !spec->multi_ios &&
1175 !codec->force_pin_prefix &&
1176 !cfg->hp_outs && !cfg->speaker_outs)
1177 return spec->vmaster_mute.hook ? "PCM" : "Master";
1179 /* if there is really a single DAC used in the whole output paths,
1180 * use it master (or "PCM" if a vmaster hook is present)
1182 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1183 !codec->force_pin_prefix &&
1184 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1185 return spec->vmaster_mute.hook ? "PCM" : "Master";
1187 /* multi-io channels */
1188 if (ch >= cfg->line_outs)
1189 return channel_name[ch];
1191 switch (cfg->line_out_type) {
1192 case AUTO_PIN_SPEAKER_OUT:
1193 /* if the primary channel vol/mute is shared with HP volume,
1194 * don't name it as Speaker
1196 if (!ch && cfg->hp_outs &&
1197 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1199 if (cfg->line_outs == 1)
1201 if (cfg->line_outs == 2)
1202 return ch ? "Bass Speaker" : "Speaker";
1204 case AUTO_PIN_HP_OUT:
1205 /* if the primary channel vol/mute is shared with spk volume,
1206 * don't name it as Headphone
1208 if (!ch && cfg->speaker_outs &&
1209 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1211 /* for multi-io case, only the primary out */
1212 if (ch && spec->multi_ios)
1216 case AUTO_PIN_LINE_OUT:
1217 /* This deals with the case where one HP or one Speaker or
1218 * one HP + one Speaker need to share the DAC with LO
1221 bool hp_lo_shared = false, spk_lo_shared = false;
1223 if (cfg->speaker_outs)
1224 spk_lo_shared = !path_has_mixer(codec,
1225 spec->speaker_paths[0], ctl_type);
1227 hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1228 if (hp_lo_shared && spk_lo_shared)
1229 return spec->vmaster_mute.hook ? "PCM" : "Master";
1231 return "Headphone+LO";
1233 return "Speaker+LO";
1237 /* for a single channel output, we don't have to name the channel */
1238 if (cfg->line_outs == 1 && !spec->multi_ios)
1241 if (ch >= ARRAY_SIZE(channel_name)) {
1246 return channel_name[ch];
1250 * Parse output paths
1253 /* badness definition */
1255 /* No primary DAC is found for the main output */
1256 BAD_NO_PRIMARY_DAC = 0x10000,
1257 /* No DAC is found for the extra output */
1258 BAD_NO_DAC = 0x4000,
1259 /* No possible multi-ios */
1260 BAD_MULTI_IO = 0x120,
1261 /* No individual DAC for extra output */
1262 BAD_NO_EXTRA_DAC = 0x102,
1263 /* No individual DAC for extra surrounds */
1264 BAD_NO_EXTRA_SURR_DAC = 0x101,
1265 /* Primary DAC shared with main surrounds */
1266 BAD_SHARED_SURROUND = 0x100,
1267 /* No independent HP possible */
1268 BAD_NO_INDEP_HP = 0x10,
1269 /* Primary DAC shared with main CLFE */
1270 BAD_SHARED_CLFE = 0x10,
1271 /* Primary DAC shared with extra surrounds */
1272 BAD_SHARED_EXTRA_SURROUND = 0x10,
1273 /* Volume widget is shared */
1274 BAD_SHARED_VOL = 0x10,
1277 /* look for widgets in the given path which are appropriate for
1278 * volume and mute controls, and assign the values to ctls[].
1280 * When no appropriate widget is found in the path, the badness value
1281 * is incremented depending on the situation. The function returns the
1282 * total badness for both volume and mute controls.
1284 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1286 struct hda_gen_spec *spec = codec->spec;
1292 return BAD_SHARED_VOL * 2;
1294 if (path->ctls[NID_PATH_VOL_CTL] ||
1295 path->ctls[NID_PATH_MUTE_CTL])
1296 return 0; /* already evaluated */
1298 nid = look_for_out_vol_nid(codec, path);
1300 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1301 if (spec->dac_min_mute)
1302 val |= HDA_AMP_VAL_MIN_MUTE;
1303 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1304 badness += BAD_SHARED_VOL;
1306 path->ctls[NID_PATH_VOL_CTL] = val;
1308 badness += BAD_SHARED_VOL;
1309 nid = look_for_out_mute_nid(codec, path);
1311 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1312 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1313 nid_has_mute(codec, nid, HDA_OUTPUT))
1314 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1316 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1317 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1318 badness += BAD_SHARED_VOL;
1320 path->ctls[NID_PATH_MUTE_CTL] = val;
1322 badness += BAD_SHARED_VOL;
1326 const struct badness_table hda_main_out_badness = {
1327 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1328 .no_dac = BAD_NO_DAC,
1329 .shared_primary = BAD_NO_PRIMARY_DAC,
1330 .shared_surr = BAD_SHARED_SURROUND,
1331 .shared_clfe = BAD_SHARED_CLFE,
1332 .shared_surr_main = BAD_SHARED_SURROUND,
1334 EXPORT_SYMBOL_GPL(hda_main_out_badness);
1336 const struct badness_table hda_extra_out_badness = {
1337 .no_primary_dac = BAD_NO_DAC,
1338 .no_dac = BAD_NO_DAC,
1339 .shared_primary = BAD_NO_EXTRA_DAC,
1340 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1341 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1342 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1344 EXPORT_SYMBOL_GPL(hda_extra_out_badness);
1346 /* get the DAC of the primary output corresponding to the given array index */
1347 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1349 struct hda_gen_spec *spec = codec->spec;
1350 struct auto_pin_cfg *cfg = &spec->autocfg;
1352 if (cfg->line_outs > idx)
1353 return spec->private_dac_nids[idx];
1354 idx -= cfg->line_outs;
1355 if (spec->multi_ios > idx)
1356 return spec->multi_io[idx].dac;
1360 /* return the DAC if it's reachable, otherwise zero */
1361 static inline hda_nid_t try_dac(struct hda_codec *codec,
1362 hda_nid_t dac, hda_nid_t pin)
1364 return is_reachable_path(codec, dac, pin) ? dac : 0;
1367 /* try to assign DACs to pins and return the resultant badness */
1368 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1369 const hda_nid_t *pins, hda_nid_t *dacs,
1371 const struct badness_table *bad)
1373 struct hda_gen_spec *spec = codec->spec;
1381 for (i = 0; i < num_outs; i++) {
1382 struct nid_path *path;
1383 hda_nid_t pin = pins[i];
1385 if (!spec->obey_preferred_dacs) {
1386 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1388 badness += assign_out_path_ctls(codec, path);
1393 dacs[i] = get_preferred_dac(codec, pin);
1395 if (is_dac_already_used(codec, dacs[i]))
1396 badness += bad->shared_primary;
1397 } else if (spec->obey_preferred_dacs) {
1398 badness += BAD_NO_PRIMARY_DAC;
1402 dacs[i] = look_for_dac(codec, pin, false);
1403 if (!dacs[i] && !i) {
1404 /* try to steal the DAC of surrounds for the front */
1405 for (j = 1; j < num_outs; j++) {
1406 if (is_reachable_path(codec, dacs[j], pin)) {
1409 invalidate_nid_path(codec, path_idx[j]);
1418 dac = try_dac(codec, get_primary_out(codec, i), pin);
1420 dac = try_dac(codec, dacs[0], pin);
1422 dac = try_dac(codec, get_primary_out(codec, i), pin);
1425 badness += bad->shared_primary;
1427 badness += bad->shared_surr;
1429 badness += bad->shared_clfe;
1430 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1431 dac = spec->private_dac_nids[0];
1432 badness += bad->shared_surr_main;
1434 badness += bad->no_primary_dac;
1436 badness += bad->no_dac;
1440 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1441 if (!path && !i && spec->mixer_nid) {
1442 /* try with aamix */
1443 path = snd_hda_add_new_path(codec, dac, pin, 0);
1447 badness += bad->no_dac;
1449 /* print_nid_path(codec, "output", path); */
1450 path->active = true;
1451 path_idx[i] = snd_hda_get_path_idx(codec, path);
1452 badness += assign_out_path_ctls(codec, path);
1459 /* return NID if the given pin has only a single connection to a certain DAC */
1460 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1462 struct hda_gen_spec *spec = codec->spec;
1464 hda_nid_t nid_found = 0;
1466 for (i = 0; i < spec->num_all_dacs; i++) {
1467 hda_nid_t nid = spec->all_dacs[i];
1468 if (!nid || is_dac_already_used(codec, nid))
1470 if (is_reachable_path(codec, nid, pin)) {
1479 /* check whether the given pin can be a multi-io pin */
1480 static bool can_be_multiio_pin(struct hda_codec *codec,
1481 unsigned int location, hda_nid_t nid)
1483 unsigned int defcfg, caps;
1485 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1486 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1488 if (location && get_defcfg_location(defcfg) != location)
1490 caps = snd_hda_query_pin_caps(codec, nid);
1491 if (!(caps & AC_PINCAP_OUT))
1496 /* count the number of input pins that are capable to be multi-io */
1497 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1499 struct hda_gen_spec *spec = codec->spec;
1500 struct auto_pin_cfg *cfg = &spec->autocfg;
1501 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1502 unsigned int location = get_defcfg_location(defcfg);
1506 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1507 for (i = 0; i < cfg->num_inputs; i++) {
1508 if (cfg->inputs[i].type != type)
1510 if (can_be_multiio_pin(codec, location,
1511 cfg->inputs[i].pin))
1521 * When hardwired is set, try to fill ony hardwired pins, and returns
1522 * zero if any pins are filled, non-zero if nothing found.
1523 * When hardwired is off, try to fill possible input pins, and returns
1524 * the badness value.
1526 static int fill_multi_ios(struct hda_codec *codec,
1527 hda_nid_t reference_pin,
1530 struct hda_gen_spec *spec = codec->spec;
1531 struct auto_pin_cfg *cfg = &spec->autocfg;
1532 int type, i, j, num_pins, old_pins;
1533 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1534 unsigned int location = get_defcfg_location(defcfg);
1536 struct nid_path *path;
1538 old_pins = spec->multi_ios;
1542 num_pins = count_multiio_pins(codec, reference_pin);
1546 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1547 for (i = 0; i < cfg->num_inputs; i++) {
1548 hda_nid_t nid = cfg->inputs[i].pin;
1551 if (cfg->inputs[i].type != type)
1553 if (!can_be_multiio_pin(codec, location, nid))
1555 for (j = 0; j < spec->multi_ios; j++) {
1556 if (nid == spec->multi_io[j].pin)
1559 if (j < spec->multi_ios)
1563 dac = get_dac_if_single(codec, nid);
1565 dac = look_for_dac(codec, nid, false);
1570 path = snd_hda_add_new_path(codec, dac, nid,
1576 /* print_nid_path(codec, "multiio", path); */
1577 spec->multi_io[spec->multi_ios].pin = nid;
1578 spec->multi_io[spec->multi_ios].dac = dac;
1579 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1580 snd_hda_get_path_idx(codec, path);
1582 if (spec->multi_ios >= 2)
1588 badness = BAD_MULTI_IO;
1589 if (old_pins == spec->multi_ios) {
1591 return 1; /* nothing found */
1593 return badness; /* no badness if nothing found */
1595 if (!hardwired && spec->multi_ios < 2) {
1596 /* cancel newly assigned paths */
1597 spec->paths.used -= spec->multi_ios - old_pins;
1598 spec->multi_ios = old_pins;
1602 /* assign volume and mute controls */
1603 for (i = old_pins; i < spec->multi_ios; i++) {
1604 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1605 badness += assign_out_path_ctls(codec, path);
1611 /* map DACs for all pins in the list if they are single connections */
1612 static bool map_singles(struct hda_codec *codec, int outs,
1613 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1615 struct hda_gen_spec *spec = codec->spec;
1618 for (i = 0; i < outs; i++) {
1619 struct nid_path *path;
1623 dac = get_dac_if_single(codec, pins[i]);
1626 path = snd_hda_add_new_path(codec, dac, pins[i],
1628 if (!path && !i && spec->mixer_nid)
1629 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1633 /* print_nid_path(codec, "output", path); */
1634 path->active = true;
1635 path_idx[i] = snd_hda_get_path_idx(codec, path);
1641 static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1643 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1644 spec->aamix_out_paths[2];
1647 /* create a new path including aamix if available, and return its index */
1648 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1650 struct hda_gen_spec *spec = codec->spec;
1651 struct nid_path *path;
1652 hda_nid_t path_dac, dac, pin;
1654 path = snd_hda_get_path_from_idx(codec, path_idx);
1655 if (!path || !path->depth ||
1656 is_nid_contained(path, spec->mixer_nid))
1658 path_dac = path->path[0];
1659 dac = spec->private_dac_nids[0];
1660 pin = path->path[path->depth - 1];
1661 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1663 if (dac != path_dac)
1665 else if (spec->multiout.hp_out_nid[0])
1666 dac = spec->multiout.hp_out_nid[0];
1667 else if (spec->multiout.extra_out_nid[0])
1668 dac = spec->multiout.extra_out_nid[0];
1672 path = snd_hda_add_new_path(codec, dac, pin,
1677 /* print_nid_path(codec, "output-aamix", path); */
1678 path->active = false; /* unused as default */
1679 path->pin_fixed = true; /* static route */
1680 return snd_hda_get_path_idx(codec, path);
1683 /* check whether the independent HP is available with the current config */
1684 static bool indep_hp_possible(struct hda_codec *codec)
1686 struct hda_gen_spec *spec = codec->spec;
1687 struct auto_pin_cfg *cfg = &spec->autocfg;
1688 struct nid_path *path;
1691 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1692 idx = spec->out_paths[0];
1694 idx = spec->hp_paths[0];
1695 path = snd_hda_get_path_from_idx(codec, idx);
1699 /* assume no path conflicts unless aamix is involved */
1700 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1703 /* check whether output paths contain aamix */
1704 for (i = 0; i < cfg->line_outs; i++) {
1705 if (spec->out_paths[i] == idx)
1707 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1708 if (path && is_nid_contained(path, spec->mixer_nid))
1711 for (i = 0; i < cfg->speaker_outs; i++) {
1712 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1713 if (path && is_nid_contained(path, spec->mixer_nid))
1720 /* fill the empty entries in the dac array for speaker/hp with the
1721 * shared dac pointed by the paths
1723 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1724 hda_nid_t *dacs, int *path_idx)
1726 struct nid_path *path;
1729 for (i = 0; i < num_outs; i++) {
1732 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1735 dacs[i] = path->path[0];
1739 /* fill in the dac_nids table from the parsed pin configuration */
1740 static int fill_and_eval_dacs(struct hda_codec *codec,
1741 bool fill_hardwired,
1742 bool fill_mio_first)
1744 struct hda_gen_spec *spec = codec->spec;
1745 struct auto_pin_cfg *cfg = &spec->autocfg;
1746 int i, err, badness;
1748 /* set num_dacs once to full for look_for_dac() */
1749 spec->multiout.num_dacs = cfg->line_outs;
1750 spec->multiout.dac_nids = spec->private_dac_nids;
1751 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1752 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1753 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1754 spec->multi_ios = 0;
1755 snd_array_free(&spec->paths);
1757 /* clear path indices */
1758 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1759 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1760 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1761 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1762 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1763 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1764 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1765 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1769 /* fill hard-wired DACs first */
1770 if (fill_hardwired) {
1773 mapped = map_singles(codec, cfg->line_outs,
1775 spec->private_dac_nids,
1777 mapped |= map_singles(codec, cfg->hp_outs,
1779 spec->multiout.hp_out_nid,
1781 mapped |= map_singles(codec, cfg->speaker_outs,
1783 spec->multiout.extra_out_nid,
1784 spec->speaker_paths);
1785 if (!spec->no_multi_io &&
1786 fill_mio_first && cfg->line_outs == 1 &&
1787 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1788 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1795 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1796 spec->private_dac_nids, spec->out_paths,
1797 spec->main_out_badness);
1799 if (!spec->no_multi_io && fill_mio_first &&
1800 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1801 /* try to fill multi-io first */
1802 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1805 /* we don't count badness at this stage yet */
1808 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1809 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1810 spec->multiout.hp_out_nid,
1812 spec->extra_out_badness);
1817 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1818 err = try_assign_dacs(codec, cfg->speaker_outs,
1820 spec->multiout.extra_out_nid,
1821 spec->speaker_paths,
1822 spec->extra_out_badness);
1827 if (!spec->no_multi_io &&
1828 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1829 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1835 if (spec->mixer_nid) {
1836 spec->aamix_out_paths[0] =
1837 check_aamix_out_path(codec, spec->out_paths[0]);
1838 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1839 spec->aamix_out_paths[1] =
1840 check_aamix_out_path(codec, spec->hp_paths[0]);
1841 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1842 spec->aamix_out_paths[2] =
1843 check_aamix_out_path(codec, spec->speaker_paths[0]);
1846 if (!spec->no_multi_io &&
1847 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1848 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1849 spec->multi_ios = 1; /* give badness */
1851 /* re-count num_dacs and squash invalid entries */
1852 spec->multiout.num_dacs = 0;
1853 for (i = 0; i < cfg->line_outs; i++) {
1854 if (spec->private_dac_nids[i])
1855 spec->multiout.num_dacs++;
1857 memmove(spec->private_dac_nids + i,
1858 spec->private_dac_nids + i + 1,
1859 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1860 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1864 spec->ext_channel_count = spec->min_channel_count =
1865 spec->multiout.num_dacs * 2;
1867 if (spec->multi_ios == 2) {
1868 for (i = 0; i < 2; i++)
1869 spec->private_dac_nids[spec->multiout.num_dacs++] =
1870 spec->multi_io[i].dac;
1871 } else if (spec->multi_ios) {
1872 spec->multi_ios = 0;
1873 badness += BAD_MULTI_IO;
1876 if (spec->indep_hp && !indep_hp_possible(codec))
1877 badness += BAD_NO_INDEP_HP;
1879 /* re-fill the shared DAC for speaker / headphone */
1880 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1881 refill_shared_dacs(codec, cfg->hp_outs,
1882 spec->multiout.hp_out_nid,
1884 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1885 refill_shared_dacs(codec, cfg->speaker_outs,
1886 spec->multiout.extra_out_nid,
1887 spec->speaker_paths);
1892 #define DEBUG_BADNESS
1894 #ifdef DEBUG_BADNESS
1895 #define debug_badness(fmt, ...) \
1896 codec_dbg(codec, fmt, ##__VA_ARGS__)
1898 #define debug_badness(fmt, ...) \
1899 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1902 #ifdef DEBUG_BADNESS
1903 static inline void print_nid_path_idx(struct hda_codec *codec,
1904 const char *pfx, int idx)
1906 struct nid_path *path;
1908 path = snd_hda_get_path_from_idx(codec, idx);
1910 print_nid_path(codec, pfx, path);
1913 static void debug_show_configs(struct hda_codec *codec,
1914 struct auto_pin_cfg *cfg)
1916 struct hda_gen_spec *spec = codec->spec;
1917 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1920 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1921 cfg->line_out_pins[0], cfg->line_out_pins[1],
1922 cfg->line_out_pins[2], cfg->line_out_pins[3],
1923 spec->multiout.dac_nids[0],
1924 spec->multiout.dac_nids[1],
1925 spec->multiout.dac_nids[2],
1926 spec->multiout.dac_nids[3],
1927 lo_type[cfg->line_out_type]);
1928 for (i = 0; i < cfg->line_outs; i++)
1929 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1930 if (spec->multi_ios > 0)
1931 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1933 spec->multi_io[0].pin, spec->multi_io[1].pin,
1934 spec->multi_io[0].dac, spec->multi_io[1].dac);
1935 for (i = 0; i < spec->multi_ios; i++)
1936 print_nid_path_idx(codec, " mio",
1937 spec->out_paths[cfg->line_outs + i]);
1939 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1940 cfg->hp_pins[0], cfg->hp_pins[1],
1941 cfg->hp_pins[2], cfg->hp_pins[3],
1942 spec->multiout.hp_out_nid[0],
1943 spec->multiout.hp_out_nid[1],
1944 spec->multiout.hp_out_nid[2],
1945 spec->multiout.hp_out_nid[3]);
1946 for (i = 0; i < cfg->hp_outs; i++)
1947 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1948 if (cfg->speaker_outs)
1949 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1950 cfg->speaker_pins[0], cfg->speaker_pins[1],
1951 cfg->speaker_pins[2], cfg->speaker_pins[3],
1952 spec->multiout.extra_out_nid[0],
1953 spec->multiout.extra_out_nid[1],
1954 spec->multiout.extra_out_nid[2],
1955 spec->multiout.extra_out_nid[3]);
1956 for (i = 0; i < cfg->speaker_outs; i++)
1957 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1958 for (i = 0; i < 3; i++)
1959 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1962 #define debug_show_configs(codec, cfg) /* NOP */
1965 /* find all available DACs of the codec */
1966 static void fill_all_dac_nids(struct hda_codec *codec)
1968 struct hda_gen_spec *spec = codec->spec;
1971 spec->num_all_dacs = 0;
1972 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1973 for_each_hda_codec_node(nid, codec) {
1974 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1976 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1977 codec_err(codec, "Too many DACs!\n");
1980 spec->all_dacs[spec->num_all_dacs++] = nid;
1984 static int parse_output_paths(struct hda_codec *codec)
1986 struct hda_gen_spec *spec = codec->spec;
1987 struct auto_pin_cfg *cfg = &spec->autocfg;
1988 struct auto_pin_cfg *best_cfg;
1990 int best_badness = INT_MAX;
1992 bool fill_hardwired = true, fill_mio_first = true;
1993 bool best_wired = true, best_mio = true;
1994 bool hp_spk_swapped = false;
1996 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
2002 badness = fill_and_eval_dacs(codec, fill_hardwired,
2008 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
2009 cfg->line_out_type, fill_hardwired, fill_mio_first,
2011 debug_show_configs(codec, cfg);
2012 if (badness < best_badness) {
2013 best_badness = badness;
2015 best_wired = fill_hardwired;
2016 best_mio = fill_mio_first;
2020 fill_mio_first = !fill_mio_first;
2021 if (!fill_mio_first)
2023 fill_hardwired = !fill_hardwired;
2024 if (!fill_hardwired)
2028 hp_spk_swapped = true;
2029 if (cfg->speaker_outs > 0 &&
2030 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2031 cfg->hp_outs = cfg->line_outs;
2032 memcpy(cfg->hp_pins, cfg->line_out_pins,
2033 sizeof(cfg->hp_pins));
2034 cfg->line_outs = cfg->speaker_outs;
2035 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2036 sizeof(cfg->speaker_pins));
2037 cfg->speaker_outs = 0;
2038 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2039 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2040 fill_hardwired = true;
2043 if (cfg->hp_outs > 0 &&
2044 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2045 cfg->speaker_outs = cfg->line_outs;
2046 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2047 sizeof(cfg->speaker_pins));
2048 cfg->line_outs = cfg->hp_outs;
2049 memcpy(cfg->line_out_pins, cfg->hp_pins,
2050 sizeof(cfg->hp_pins));
2052 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2053 cfg->line_out_type = AUTO_PIN_HP_OUT;
2054 fill_hardwired = true;
2061 debug_badness("==> restoring best_cfg\n");
2063 fill_and_eval_dacs(codec, best_wired, best_mio);
2065 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2066 cfg->line_out_type, best_wired, best_mio);
2067 debug_show_configs(codec, cfg);
2069 if (cfg->line_out_pins[0]) {
2070 struct nid_path *path;
2071 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
2073 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
2074 if (spec->vmaster_nid) {
2075 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2076 HDA_OUTPUT, spec->vmaster_tlv);
2077 if (spec->dac_min_mute)
2078 spec->vmaster_tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] |= TLV_DB_SCALE_MUTE;
2082 /* set initial pinctl targets */
2083 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2087 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2088 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2089 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2090 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2091 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2092 set_pin_targets(codec, cfg->speaker_outs,
2093 cfg->speaker_pins, val);
2096 /* clear indep_hp flag if not available */
2097 if (spec->indep_hp && !indep_hp_possible(codec))
2104 /* add playback controls from the parsed DAC table */
2105 static int create_multi_out_ctls(struct hda_codec *codec,
2106 const struct auto_pin_cfg *cfg)
2108 struct hda_gen_spec *spec = codec->spec;
2109 int i, err, noutputs;
2111 noutputs = cfg->line_outs;
2112 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2113 noutputs += spec->multi_ios;
2115 for (i = 0; i < noutputs; i++) {
2118 struct nid_path *path;
2120 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
2124 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
2125 if (!name || !strcmp(name, "CLFE")) {
2127 err = add_vol_ctl(codec, "Center", 0, 1, path);
2130 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2134 err = add_stereo_vol(codec, name, index, path);
2139 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2140 if (!name || !strcmp(name, "CLFE")) {
2141 err = add_sw_ctl(codec, "Center", 0, 1, path);
2144 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2148 err = add_stereo_sw(codec, name, index, path);
2156 static int create_extra_out(struct hda_codec *codec, int path_idx,
2157 const char *pfx, int cidx)
2159 struct nid_path *path;
2162 path = snd_hda_get_path_from_idx(codec, path_idx);
2165 err = add_stereo_vol(codec, pfx, cidx, path);
2168 err = add_stereo_sw(codec, pfx, cidx, path);
2174 /* add playback controls for speaker and HP outputs */
2175 static int create_extra_outs(struct hda_codec *codec, int num_pins,
2176 const int *paths, const char *pfx)
2180 for (i = 0; i < num_pins; i++) {
2182 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2185 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2186 name = "Bass Speaker";
2187 else if (num_pins >= 3) {
2188 snprintf(tmp, sizeof(tmp), "%s %s",
2189 pfx, channel_name[i]);
2195 err = create_extra_out(codec, paths[i], name, idx);
2202 static int create_hp_out_ctls(struct hda_codec *codec)
2204 struct hda_gen_spec *spec = codec->spec;
2205 return create_extra_outs(codec, spec->autocfg.hp_outs,
2210 static int create_speaker_out_ctls(struct hda_codec *codec)
2212 struct hda_gen_spec *spec = codec->spec;
2213 return create_extra_outs(codec, spec->autocfg.speaker_outs,
2214 spec->speaker_paths,
2219 * independent HP controls
2222 static void call_hp_automute(struct hda_codec *codec,
2223 struct hda_jack_callback *jack);
2224 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2225 struct snd_ctl_elem_info *uinfo)
2227 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2230 static int indep_hp_get(struct snd_kcontrol *kcontrol,
2231 struct snd_ctl_elem_value *ucontrol)
2233 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2234 struct hda_gen_spec *spec = codec->spec;
2235 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2239 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2240 int nomix_path_idx, int mix_path_idx,
2243 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2244 struct snd_ctl_elem_value *ucontrol)
2246 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2247 struct hda_gen_spec *spec = codec->spec;
2248 unsigned int select = ucontrol->value.enumerated.item[0];
2251 mutex_lock(&spec->pcm_mutex);
2252 if (spec->active_streams) {
2257 if (spec->indep_hp_enabled != select) {
2259 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2260 dacp = &spec->private_dac_nids[0];
2262 dacp = &spec->multiout.hp_out_nid[0];
2264 /* update HP aamix paths in case it conflicts with indep HP */
2265 if (spec->have_aamix_ctl) {
2266 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2267 update_aamix_paths(codec, spec->aamix_mode,
2269 spec->aamix_out_paths[0],
2270 spec->autocfg.line_out_type);
2272 update_aamix_paths(codec, spec->aamix_mode,
2274 spec->aamix_out_paths[1],
2278 spec->indep_hp_enabled = select;
2279 if (spec->indep_hp_enabled)
2282 *dacp = spec->alt_dac_nid;
2284 call_hp_automute(codec, NULL);
2288 mutex_unlock(&spec->pcm_mutex);
2292 static const struct snd_kcontrol_new indep_hp_ctl = {
2293 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2294 .name = "Independent HP",
2295 .info = indep_hp_info,
2296 .get = indep_hp_get,
2297 .put = indep_hp_put,
2301 static int create_indep_hp_ctls(struct hda_codec *codec)
2303 struct hda_gen_spec *spec = codec->spec;
2306 if (!spec->indep_hp)
2308 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2309 dac = spec->multiout.dac_nids[0];
2311 dac = spec->multiout.hp_out_nid[0];
2317 spec->indep_hp_enabled = false;
2318 spec->alt_dac_nid = dac;
2319 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2325 * channel mode enum control
2328 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2329 struct snd_ctl_elem_info *uinfo)
2331 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2332 struct hda_gen_spec *spec = codec->spec;
2335 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2337 uinfo->value.enumerated.items = spec->multi_ios + 1;
2338 if (uinfo->value.enumerated.item > spec->multi_ios)
2339 uinfo->value.enumerated.item = spec->multi_ios;
2340 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2341 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2345 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2346 struct snd_ctl_elem_value *ucontrol)
2348 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2349 struct hda_gen_spec *spec = codec->spec;
2350 ucontrol->value.enumerated.item[0] =
2351 (spec->ext_channel_count - spec->min_channel_count) / 2;
2355 static inline struct nid_path *
2356 get_multiio_path(struct hda_codec *codec, int idx)
2358 struct hda_gen_spec *spec = codec->spec;
2359 return snd_hda_get_path_from_idx(codec,
2360 spec->out_paths[spec->autocfg.line_outs + idx]);
2363 static void update_automute_all(struct hda_codec *codec);
2365 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2366 * used for output paths
2368 static bool aamix_default(struct hda_gen_spec *spec)
2370 return !spec->have_aamix_ctl || spec->aamix_mode;
2373 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2375 struct hda_gen_spec *spec = codec->spec;
2376 hda_nid_t nid = spec->multi_io[idx].pin;
2377 struct nid_path *path;
2379 path = get_multiio_path(codec, idx);
2383 if (path->active == output)
2387 set_pin_target(codec, nid, PIN_OUT, true);
2388 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2389 set_pin_eapd(codec, nid, true);
2391 set_pin_eapd(codec, nid, false);
2392 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2393 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2394 path_power_down_sync(codec, path);
2397 /* update jack retasking in case it modifies any of them */
2398 update_automute_all(codec);
2403 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2404 struct snd_ctl_elem_value *ucontrol)
2406 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2407 struct hda_gen_spec *spec = codec->spec;
2410 ch = ucontrol->value.enumerated.item[0];
2411 if (ch < 0 || ch > spec->multi_ios)
2413 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2415 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2416 for (i = 0; i < spec->multi_ios; i++)
2417 set_multi_io(codec, i, i < ch);
2418 spec->multiout.max_channels = max(spec->ext_channel_count,
2419 spec->const_channel_count);
2420 if (spec->need_dac_fix)
2421 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2425 static const struct snd_kcontrol_new channel_mode_enum = {
2426 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2427 .name = "Channel Mode",
2428 .info = ch_mode_info,
2433 static int create_multi_channel_mode(struct hda_codec *codec)
2435 struct hda_gen_spec *spec = codec->spec;
2437 if (spec->multi_ios > 0) {
2438 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2445 * aamix loopback enable/disable switch
2448 #define loopback_mixing_info indep_hp_info
2450 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2451 struct snd_ctl_elem_value *ucontrol)
2453 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2454 struct hda_gen_spec *spec = codec->spec;
2455 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2459 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2460 int nomix_path_idx, int mix_path_idx,
2463 struct hda_gen_spec *spec = codec->spec;
2464 struct nid_path *nomix_path, *mix_path;
2466 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2467 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2468 if (!nomix_path || !mix_path)
2471 /* if HP aamix path is driven from a different DAC and the
2472 * independent HP mode is ON, can't turn on aamix path
2474 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2475 mix_path->path[0] != spec->alt_dac_nid)
2479 snd_hda_activate_path(codec, nomix_path, false, true);
2480 snd_hda_activate_path(codec, mix_path, true, true);
2481 path_power_down_sync(codec, nomix_path);
2483 snd_hda_activate_path(codec, mix_path, false, false);
2484 snd_hda_activate_path(codec, nomix_path, true, false);
2485 path_power_down_sync(codec, mix_path);
2489 /* re-initialize the output paths; only called from loopback_mixing_put() */
2490 static void update_output_paths(struct hda_codec *codec, int num_outs,
2493 struct hda_gen_spec *spec = codec->spec;
2494 struct nid_path *path;
2497 for (i = 0; i < num_outs; i++) {
2498 path = snd_hda_get_path_from_idx(codec, paths[i]);
2500 snd_hda_activate_path(codec, path, path->active,
2505 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2506 struct snd_ctl_elem_value *ucontrol)
2508 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2509 struct hda_gen_spec *spec = codec->spec;
2510 const struct auto_pin_cfg *cfg = &spec->autocfg;
2511 unsigned int val = ucontrol->value.enumerated.item[0];
2513 if (val == spec->aamix_mode)
2515 spec->aamix_mode = val;
2516 if (has_aamix_out_paths(spec)) {
2517 update_aamix_paths(codec, val, spec->out_paths[0],
2518 spec->aamix_out_paths[0],
2519 cfg->line_out_type);
2520 update_aamix_paths(codec, val, spec->hp_paths[0],
2521 spec->aamix_out_paths[1],
2523 update_aamix_paths(codec, val, spec->speaker_paths[0],
2524 spec->aamix_out_paths[2],
2525 AUTO_PIN_SPEAKER_OUT);
2527 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2528 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2529 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2530 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2531 update_output_paths(codec, cfg->speaker_outs,
2532 spec->speaker_paths);
2537 static const struct snd_kcontrol_new loopback_mixing_enum = {
2538 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2539 .name = "Loopback Mixing",
2540 .info = loopback_mixing_info,
2541 .get = loopback_mixing_get,
2542 .put = loopback_mixing_put,
2545 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2547 struct hda_gen_spec *spec = codec->spec;
2549 if (!spec->mixer_nid)
2551 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2553 spec->have_aamix_ctl = 1;
2558 * shared headphone/mic handling
2561 static void call_update_outputs(struct hda_codec *codec);
2563 /* for shared I/O, change the pin-control accordingly */
2564 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2566 struct hda_gen_spec *spec = codec->spec;
2571 pin = spec->hp_mic_pin;
2572 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2575 val = snd_hda_codec_get_pin_target(codec, pin);
2585 val = snd_hda_get_default_vref(codec, pin);
2586 /* if the HP pin doesn't support VREF and the codec driver gives an
2587 * alternative pin, set up the VREF on that pin instead
2589 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2590 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2591 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2592 if (vref_val != AC_PINCTL_VREF_HIZ)
2593 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2594 PIN_IN | (as_mic ? vref_val : 0));
2597 if (!spec->hp_mic_jack_modes) {
2602 set_pin_target(codec, pin, val, true);
2603 call_hp_automute(codec, NULL);
2607 /* create a shared input with the headphone out */
2608 static int create_hp_mic(struct hda_codec *codec)
2610 struct hda_gen_spec *spec = codec->spec;
2611 struct auto_pin_cfg *cfg = &spec->autocfg;
2612 unsigned int defcfg;
2615 if (!spec->hp_mic) {
2616 if (spec->suppress_hp_mic_detect)
2618 /* automatic detection: only if no input or a single internal
2619 * input pin is found, try to detect the shared hp/mic
2621 if (cfg->num_inputs > 1)
2623 else if (cfg->num_inputs == 1) {
2624 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2625 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2630 spec->hp_mic = 0; /* clear once */
2631 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2635 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2636 nid = cfg->line_out_pins[0];
2637 else if (cfg->hp_outs > 0)
2638 nid = cfg->hp_pins[0];
2642 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2643 return 0; /* no input */
2645 cfg->inputs[cfg->num_inputs].pin = nid;
2646 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2647 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2650 spec->hp_mic_pin = nid;
2651 /* we can't handle auto-mic together with HP-mic */
2652 spec->suppress_auto_mic = 1;
2653 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
2661 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2663 static const char * const out_jack_texts[] = {
2664 "Line Out", "Headphone Out",
2667 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2668 struct snd_ctl_elem_info *uinfo)
2670 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2673 static int out_jack_mode_get(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;
2678 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2679 ucontrol->value.enumerated.item[0] = 1;
2681 ucontrol->value.enumerated.item[0] = 0;
2685 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2686 struct snd_ctl_elem_value *ucontrol)
2688 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2689 hda_nid_t nid = kcontrol->private_value;
2692 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2693 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2695 snd_hda_set_pin_ctl_cache(codec, nid, val);
2699 static const struct snd_kcontrol_new out_jack_mode_enum = {
2700 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2701 .info = out_jack_mode_info,
2702 .get = out_jack_mode_get,
2703 .put = out_jack_mode_put,
2706 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2708 struct hda_gen_spec *spec = codec->spec;
2709 const struct snd_kcontrol_new *kctl;
2712 snd_array_for_each(&spec->kctls, i, kctl) {
2713 if (!strcmp(kctl->name, name) && kctl->index == idx)
2719 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2720 char *name, size_t name_len)
2722 struct hda_gen_spec *spec = codec->spec;
2725 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2726 strlcat(name, " Jack Mode", name_len);
2728 for (; find_kctl_name(codec, name, idx); idx++)
2732 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2734 struct hda_gen_spec *spec = codec->spec;
2735 if (spec->add_jack_modes) {
2736 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2737 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2743 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2746 struct hda_gen_spec *spec = codec->spec;
2749 for (i = 0; i < num_pins; i++) {
2750 hda_nid_t pin = pins[i];
2751 if (pin == spec->hp_mic_pin)
2753 if (get_out_jack_num_items(codec, pin) > 1) {
2754 struct snd_kcontrol_new *knew;
2755 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2756 get_jack_mode_name(codec, pin, name, sizeof(name));
2757 knew = snd_hda_gen_add_kctl(spec, name,
2758 &out_jack_mode_enum);
2761 knew->private_value = pin;
2772 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2775 static const char * const vref_texts[NUM_VREFS] = {
2776 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2777 "", "Mic 80pc Bias", "Mic 100pc Bias"
2780 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2782 unsigned int pincap;
2784 pincap = snd_hda_query_pin_caps(codec, pin);
2785 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2786 /* filter out unusual vrefs */
2787 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2791 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2792 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2794 unsigned int i, n = 0;
2796 for (i = 0; i < NUM_VREFS; i++) {
2797 if (vref_caps & (1 << i)) {
2806 /* convert back from the vref ctl index to the enum item index */
2807 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2809 unsigned int i, n = 0;
2811 for (i = 0; i < NUM_VREFS; i++) {
2814 if (vref_caps & (1 << i))
2820 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2821 struct snd_ctl_elem_info *uinfo)
2823 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2824 hda_nid_t nid = kcontrol->private_value;
2825 unsigned int vref_caps = get_vref_caps(codec, nid);
2827 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2829 /* set the right text */
2830 strcpy(uinfo->value.enumerated.name,
2831 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2835 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2836 struct snd_ctl_elem_value *ucontrol)
2838 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2839 hda_nid_t nid = kcontrol->private_value;
2840 unsigned int vref_caps = get_vref_caps(codec, nid);
2843 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2844 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2848 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2849 struct snd_ctl_elem_value *ucontrol)
2851 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2852 hda_nid_t nid = kcontrol->private_value;
2853 unsigned int vref_caps = get_vref_caps(codec, nid);
2854 unsigned int val, idx;
2856 val = snd_hda_codec_get_pin_target(codec, nid);
2857 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2858 if (idx == ucontrol->value.enumerated.item[0])
2861 val &= ~AC_PINCTL_VREFEN;
2862 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2863 snd_hda_set_pin_ctl_cache(codec, nid, val);
2867 static const struct snd_kcontrol_new in_jack_mode_enum = {
2868 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2869 .info = in_jack_mode_info,
2870 .get = in_jack_mode_get,
2871 .put = in_jack_mode_put,
2874 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2876 struct hda_gen_spec *spec = codec->spec;
2878 if (spec->add_jack_modes)
2879 nitems = hweight32(get_vref_caps(codec, pin));
2880 return nitems ? nitems : 1;
2883 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2885 struct hda_gen_spec *spec = codec->spec;
2886 struct snd_kcontrol_new *knew;
2887 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2888 unsigned int defcfg;
2890 if (pin == spec->hp_mic_pin)
2891 return 0; /* already done in create_out_jack_mode() */
2893 /* no jack mode for fixed pins */
2894 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2895 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2898 /* no multiple vref caps? */
2899 if (get_in_jack_num_items(codec, pin) <= 1)
2902 get_jack_mode_name(codec, pin, name, sizeof(name));
2903 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2906 knew->private_value = pin;
2911 * HP/mic shared jack mode
2913 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2914 struct snd_ctl_elem_info *uinfo)
2916 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2917 hda_nid_t nid = kcontrol->private_value;
2918 int out_jacks = get_out_jack_num_items(codec, nid);
2919 int in_jacks = get_in_jack_num_items(codec, nid);
2920 const char *text = NULL;
2923 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2925 uinfo->value.enumerated.items = out_jacks + in_jacks;
2926 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2927 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2928 idx = uinfo->value.enumerated.item;
2929 if (idx < out_jacks) {
2931 text = out_jack_texts[idx];
2933 text = "Headphone Out";
2937 unsigned int vref_caps = get_vref_caps(codec, nid);
2938 text = vref_texts[get_vref_idx(vref_caps, idx)];
2943 strcpy(uinfo->value.enumerated.name, text);
2947 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2949 int out_jacks = get_out_jack_num_items(codec, nid);
2950 int in_jacks = get_in_jack_num_items(codec, nid);
2951 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2954 if (val & PIN_OUT) {
2955 if (out_jacks > 1 && val == PIN_HP)
2957 } else if (val & PIN_IN) {
2960 unsigned int vref_caps = get_vref_caps(codec, nid);
2961 val &= AC_PINCTL_VREFEN;
2962 idx += cvt_from_vref_idx(vref_caps, val);
2968 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2969 struct snd_ctl_elem_value *ucontrol)
2971 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2972 hda_nid_t nid = kcontrol->private_value;
2973 ucontrol->value.enumerated.item[0] =
2974 get_cur_hp_mic_jack_mode(codec, nid);
2978 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2979 struct snd_ctl_elem_value *ucontrol)
2981 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2982 hda_nid_t nid = kcontrol->private_value;
2983 int out_jacks = get_out_jack_num_items(codec, nid);
2984 int in_jacks = get_in_jack_num_items(codec, nid);
2985 unsigned int val, oldval, idx;
2987 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2988 idx = ucontrol->value.enumerated.item[0];
2992 if (idx < out_jacks) {
2994 val = idx ? PIN_HP : PIN_OUT;
3000 unsigned int vref_caps = get_vref_caps(codec, nid);
3001 val = snd_hda_codec_get_pin_target(codec, nid);
3002 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
3003 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
3005 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
3007 snd_hda_set_pin_ctl_cache(codec, nid, val);
3008 call_hp_automute(codec, NULL);
3013 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
3014 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3015 .info = hp_mic_jack_mode_info,
3016 .get = hp_mic_jack_mode_get,
3017 .put = hp_mic_jack_mode_put,
3020 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
3022 struct hda_gen_spec *spec = codec->spec;
3023 struct snd_kcontrol_new *knew;
3025 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
3026 &hp_mic_jack_mode_enum);
3029 knew->private_value = pin;
3030 spec->hp_mic_jack_modes = 1;
3038 /* add the powersave loopback-list entry */
3039 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
3041 struct hda_amp_list *list;
3043 list = snd_array_new(&spec->loopback_list);
3047 list->dir = HDA_INPUT;
3049 spec->loopback.amplist = spec->loopback_list.list;
3053 /* return true if either a volume or a mute amp is found for the given
3054 * aamix path; the amp has to be either in the mixer node or its direct leaf
3056 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3057 hda_nid_t pin, unsigned int *mix_val,
3058 unsigned int *mute_val)
3061 const hda_nid_t *list;
3064 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3068 *mix_val = *mute_val = 0;
3069 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3070 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3071 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3072 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3073 if (*mix_val && *mute_val)
3076 /* check leaf node */
3077 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3078 if (num_conns < idx)
3081 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3082 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
3083 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3084 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3085 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
3086 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3088 return *mix_val || *mute_val;
3091 /* create input playback/capture controls for the given pin */
3092 static int new_analog_input(struct hda_codec *codec, int input_idx,
3093 hda_nid_t pin, const char *ctlname, int ctlidx,
3096 struct hda_gen_spec *spec = codec->spec;
3097 struct nid_path *path;
3098 unsigned int mix_val, mute_val;
3101 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3104 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
3107 print_nid_path(codec, "loopback", path);
3108 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
3110 idx = path->idx[path->depth - 1];
3112 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
3115 path->ctls[NID_PATH_VOL_CTL] = mix_val;
3119 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
3122 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
3125 path->active = true;
3126 path->stream_enabled = true; /* no DAC/ADC involved */
3127 err = add_loopback_list(spec, mix_nid, idx);
3131 if (spec->mixer_nid != spec->mixer_merge_nid &&
3132 !spec->loopback_merge_path) {
3133 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3134 spec->mixer_merge_nid, 0);
3136 print_nid_path(codec, "loopback-merge", path);
3137 path->active = true;
3138 path->pin_fixed = true; /* static route */
3139 path->stream_enabled = true; /* no DAC/ADC involved */
3140 spec->loopback_merge_path =
3141 snd_hda_get_path_idx(codec, path);
3148 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3150 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3151 return (pincap & AC_PINCAP_IN) != 0;
3154 /* Parse the codec tree and retrieve ADCs */
3155 static int fill_adc_nids(struct hda_codec *codec)
3157 struct hda_gen_spec *spec = codec->spec;
3159 hda_nid_t *adc_nids = spec->adc_nids;
3160 int max_nums = ARRAY_SIZE(spec->adc_nids);
3163 for_each_hda_codec_node(nid, codec) {
3164 unsigned int caps = get_wcaps(codec, nid);
3165 int type = get_wcaps_type(caps);
3167 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3169 adc_nids[nums] = nid;
3170 if (++nums >= max_nums)
3173 spec->num_adc_nids = nums;
3175 /* copy the detected ADCs to all_adcs[] */
3176 spec->num_all_adcs = nums;
3177 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3182 /* filter out invalid adc_nids that don't give all active input pins;
3183 * if needed, check whether dynamic ADC-switching is available
3185 static int check_dyn_adc_switch(struct hda_codec *codec)
3187 struct hda_gen_spec *spec = codec->spec;
3188 struct hda_input_mux *imux = &spec->input_mux;
3189 unsigned int ok_bits;
3194 for (n = 0; n < spec->num_adc_nids; n++) {
3195 for (i = 0; i < imux->num_items; i++) {
3196 if (!spec->input_paths[i][n])
3199 if (i >= imux->num_items) {
3200 ok_bits |= (1 << n);
3206 /* check whether ADC-switch is possible */
3207 for (i = 0; i < imux->num_items; i++) {
3208 for (n = 0; n < spec->num_adc_nids; n++) {
3209 if (spec->input_paths[i][n]) {
3210 spec->dyn_adc_idx[i] = n;
3216 codec_dbg(codec, "enabling ADC switching\n");
3217 spec->dyn_adc_switch = 1;
3218 } else if (nums != spec->num_adc_nids) {
3219 /* shrink the invalid adcs and input paths */
3221 for (n = 0; n < spec->num_adc_nids; n++) {
3222 if (!(ok_bits & (1 << n)))
3225 spec->adc_nids[nums] = spec->adc_nids[n];
3226 for (i = 0; i < imux->num_items; i++) {
3227 invalidate_nid_path(codec,
3228 spec->input_paths[i][nums]);
3229 spec->input_paths[i][nums] =
3230 spec->input_paths[i][n];
3231 spec->input_paths[i][n] = 0;
3236 spec->num_adc_nids = nums;
3239 if (imux->num_items == 1 ||
3240 (imux->num_items == 2 && spec->hp_mic)) {
3241 codec_dbg(codec, "reducing to a single ADC\n");
3242 spec->num_adc_nids = 1; /* reduce to a single ADC */
3245 /* single index for individual volumes ctls */
3246 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3247 spec->num_adc_nids = 1;
3252 /* parse capture source paths from the given pin and create imux items */
3253 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3254 int cfg_idx, int num_adcs,
3255 const char *label, int anchor)
3257 struct hda_gen_spec *spec = codec->spec;
3258 struct hda_input_mux *imux = &spec->input_mux;
3259 int imux_idx = imux->num_items;
3260 bool imux_added = false;
3263 for (c = 0; c < num_adcs; c++) {
3264 struct nid_path *path;
3265 hda_nid_t adc = spec->adc_nids[c];
3267 if (!is_reachable_path(codec, pin, adc))
3269 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3272 print_nid_path(codec, "input", path);
3273 spec->input_paths[imux_idx][c] =
3274 snd_hda_get_path_idx(codec, path);
3277 if (spec->hp_mic_pin == pin)
3278 spec->hp_mic_mux_idx = imux->num_items;
3279 spec->imux_pins[imux->num_items] = pin;
3280 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3282 if (spec->dyn_adc_switch)
3283 spec->dyn_adc_idx[imux_idx] = c;
3291 * create playback/capture controls for input pins
3294 /* fill the label for each input at first */
3295 static int fill_input_pin_labels(struct hda_codec *codec)
3297 struct hda_gen_spec *spec = codec->spec;
3298 const struct auto_pin_cfg *cfg = &spec->autocfg;
3301 for (i = 0; i < cfg->num_inputs; i++) {
3302 hda_nid_t pin = cfg->inputs[i].pin;
3306 if (!is_input_pin(codec, pin))
3309 label = hda_get_autocfg_input_label(codec, cfg, i);
3311 for (j = i - 1; j >= 0; j--) {
3312 if (spec->input_labels[j] &&
3313 !strcmp(spec->input_labels[j], label)) {
3314 idx = spec->input_label_idxs[j] + 1;
3319 spec->input_labels[i] = label;
3320 spec->input_label_idxs[i] = idx;
3326 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3328 static int create_input_ctls(struct hda_codec *codec)
3330 struct hda_gen_spec *spec = codec->spec;
3331 const struct auto_pin_cfg *cfg = &spec->autocfg;
3332 hda_nid_t mixer = spec->mixer_nid;
3337 num_adcs = fill_adc_nids(codec);
3341 err = fill_input_pin_labels(codec);
3345 for (i = 0; i < cfg->num_inputs; i++) {
3348 pin = cfg->inputs[i].pin;
3349 if (!is_input_pin(codec, pin))
3353 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3354 val |= snd_hda_get_default_vref(codec, pin);
3355 if (pin != spec->hp_mic_pin &&
3356 !snd_hda_codec_get_pin_target(codec, pin))
3357 set_pin_target(codec, pin, val, false);
3360 if (is_reachable_path(codec, pin, mixer)) {
3361 err = new_analog_input(codec, i, pin,
3362 spec->input_labels[i],
3363 spec->input_label_idxs[i],
3370 err = parse_capture_source(codec, pin, i, num_adcs,
3371 spec->input_labels[i], -mixer);
3375 if (spec->add_jack_modes) {
3376 err = create_in_jack_mode(codec, pin);
3382 /* add stereo mix when explicitly enabled via hint */
3383 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3384 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3389 spec->suppress_auto_mic = 1;
3400 /* get the input path specified by the given adc and imux indices */
3401 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3403 struct hda_gen_spec *spec = codec->spec;
3404 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3408 if (spec->dyn_adc_switch)
3409 adc_idx = spec->dyn_adc_idx[imux_idx];
3410 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3414 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3417 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3420 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3421 struct snd_ctl_elem_info *uinfo)
3423 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3424 struct hda_gen_spec *spec = codec->spec;
3425 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3428 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3429 struct snd_ctl_elem_value *ucontrol)
3431 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3432 struct hda_gen_spec *spec = codec->spec;
3433 /* the ctls are created at once with multiple counts */
3434 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3436 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3440 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3441 struct snd_ctl_elem_value *ucontrol)
3443 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3444 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3445 return mux_select(codec, adc_idx,
3446 ucontrol->value.enumerated.item[0]);
3449 static const struct snd_kcontrol_new cap_src_temp = {
3450 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3451 .name = "Input Source",
3452 .info = mux_enum_info,
3453 .get = mux_enum_get,
3454 .put = mux_enum_put,
3458 * capture volume and capture switch ctls
3461 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3462 struct snd_ctl_elem_value *ucontrol);
3464 /* call the given amp update function for all amps in the imux list at once */
3465 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3466 struct snd_ctl_elem_value *ucontrol,
3467 put_call_t func, int type)
3469 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3470 struct hda_gen_spec *spec = codec->spec;
3471 const struct hda_input_mux *imux;
3472 struct nid_path *path;
3473 int i, adc_idx, ret, err = 0;
3475 imux = &spec->input_mux;
3476 adc_idx = kcontrol->id.index;
3477 mutex_lock(&codec->control_mutex);
3478 for (i = 0; i < imux->num_items; i++) {
3479 path = get_input_path(codec, adc_idx, i);
3480 if (!path || !path->ctls[type])
3482 kcontrol->private_value = path->ctls[type];
3483 ret = func(kcontrol, ucontrol);
3491 mutex_unlock(&codec->control_mutex);
3492 if (err >= 0 && spec->cap_sync_hook)
3493 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3497 /* capture volume ctl callbacks */
3498 #define cap_vol_info snd_hda_mixer_amp_volume_info
3499 #define cap_vol_get snd_hda_mixer_amp_volume_get
3500 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3502 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3503 struct snd_ctl_elem_value *ucontrol)
3505 return cap_put_caller(kcontrol, ucontrol,
3506 snd_hda_mixer_amp_volume_put,
3510 static const struct snd_kcontrol_new cap_vol_temp = {
3511 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3512 .name = "Capture Volume",
3513 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3514 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3515 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3516 .info = cap_vol_info,
3519 .tlv = { .c = cap_vol_tlv },
3522 /* capture switch ctl callbacks */
3523 #define cap_sw_info snd_ctl_boolean_stereo_info
3524 #define cap_sw_get snd_hda_mixer_amp_switch_get
3526 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3527 struct snd_ctl_elem_value *ucontrol)
3529 return cap_put_caller(kcontrol, ucontrol,
3530 snd_hda_mixer_amp_switch_put,
3534 static const struct snd_kcontrol_new cap_sw_temp = {
3535 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3536 .name = "Capture Switch",
3537 .info = cap_sw_info,
3542 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3547 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3548 for (depth = 0; depth < 3; depth++) {
3549 if (depth >= path->depth)
3551 i = path->depth - depth - 1;
3552 nid = path->path[i];
3553 if (!path->ctls[NID_PATH_VOL_CTL]) {
3554 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3555 path->ctls[NID_PATH_VOL_CTL] =
3556 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3557 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3558 int idx = path->idx[i];
3559 if (!depth && codec->single_adc_amp)
3561 path->ctls[NID_PATH_VOL_CTL] =
3562 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3565 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3566 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3567 path->ctls[NID_PATH_MUTE_CTL] =
3568 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3569 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3570 int idx = path->idx[i];
3571 if (!depth && codec->single_adc_amp)
3573 path->ctls[NID_PATH_MUTE_CTL] =
3574 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3581 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3583 struct hda_gen_spec *spec = codec->spec;
3584 struct auto_pin_cfg *cfg = &spec->autocfg;
3588 if (!spec->inv_dmic_split)
3590 for (i = 0; i < cfg->num_inputs; i++) {
3591 if (cfg->inputs[i].pin != nid)
3593 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3595 val = snd_hda_codec_get_pincfg(codec, nid);
3596 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3601 /* capture switch put callback for a single control with hook call */
3602 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3603 struct snd_ctl_elem_value *ucontrol)
3605 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3606 struct hda_gen_spec *spec = codec->spec;
3609 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3613 if (spec->cap_sync_hook)
3614 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3619 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3620 int idx, bool is_switch, unsigned int ctl,
3623 struct hda_gen_spec *spec = codec->spec;
3624 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3625 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3626 const char *sfx = is_switch ? "Switch" : "Volume";
3627 unsigned int chs = inv_dmic ? 1 : 3;
3628 struct snd_kcontrol_new *knew;
3634 snprintf(tmpname, sizeof(tmpname),
3635 "%s Capture %s", label, sfx);
3637 snprintf(tmpname, sizeof(tmpname),
3639 knew = add_control(spec, type, tmpname, idx,
3640 amp_val_replace_channels(ctl, chs));
3644 knew->put = cap_single_sw_put;
3648 /* Make independent right kcontrol */
3650 snprintf(tmpname, sizeof(tmpname),
3651 "Inverted %s Capture %s", label, sfx);
3653 snprintf(tmpname, sizeof(tmpname),
3654 "Inverted Capture %s", sfx);
3655 knew = add_control(spec, type, tmpname, idx,
3656 amp_val_replace_channels(ctl, 2));
3660 knew->put = cap_single_sw_put;
3664 /* create single (and simple) capture volume and switch controls */
3665 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3666 unsigned int vol_ctl, unsigned int sw_ctl,
3670 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3673 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3679 /* create bound capture volume and switch controls */
3680 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3681 unsigned int vol_ctl, unsigned int sw_ctl)
3683 struct hda_gen_spec *spec = codec->spec;
3684 struct snd_kcontrol_new *knew;
3687 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3691 knew->private_value = vol_ctl;
3692 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3695 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3699 knew->private_value = sw_ctl;
3700 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3705 /* return the vol ctl when used first in the imux list */
3706 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3708 struct nid_path *path;
3712 path = get_input_path(codec, 0, idx);
3715 ctl = path->ctls[type];
3718 for (i = 0; i < idx - 1; i++) {
3719 path = get_input_path(codec, 0, i);
3720 if (path && path->ctls[type] == ctl)
3726 /* create individual capture volume and switch controls per input */
3727 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3729 struct hda_gen_spec *spec = codec->spec;
3730 struct hda_input_mux *imux = &spec->input_mux;
3733 for (i = 0; i < imux->num_items; i++) {
3737 idx = imux->items[i].index;
3738 if (idx >= spec->autocfg.num_inputs)
3740 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3742 for (type = 0; type < 2; type++) {
3743 err = add_single_cap_ctl(codec,
3744 spec->input_labels[idx],
3745 spec->input_label_idxs[idx],
3747 get_first_cap_ctl(codec, i, type),
3756 static int create_capture_mixers(struct hda_codec *codec)
3758 struct hda_gen_spec *spec = codec->spec;
3759 struct hda_input_mux *imux = &spec->input_mux;
3760 int i, n, nums, err;
3762 if (spec->dyn_adc_switch)
3765 nums = spec->num_adc_nids;
3767 if (!spec->auto_mic && imux->num_items > 1) {
3768 struct snd_kcontrol_new *knew;
3770 name = nums > 1 ? "Input Source" : "Capture Source";
3771 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3777 for (n = 0; n < nums; n++) {
3779 bool multi_cap_vol = spec->multi_cap_vol;
3780 bool inv_dmic = false;
3784 for (i = 0; i < imux->num_items; i++) {
3785 struct nid_path *path;
3786 path = get_input_path(codec, n, i);
3789 parse_capvol_in_path(codec, path);
3791 vol = path->ctls[NID_PATH_VOL_CTL];
3792 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3794 if (!same_amp_caps(codec, vol,
3795 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3796 multi_cap_vol = true;
3799 sw = path->ctls[NID_PATH_MUTE_CTL];
3800 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3802 if (!same_amp_caps(codec, sw,
3803 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3804 multi_cap_vol = true;
3806 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3811 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3813 else if (!multi_cap_vol && !inv_dmic)
3814 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3816 err = create_multi_cap_vol_ctl(codec);
3825 * add mic boosts if needed
3828 /* check whether the given amp is feasible as a boost volume */
3829 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3834 if (!nid_has_volume(codec, nid, dir) ||
3835 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3836 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3839 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3840 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3846 /* look for a boost amp in a widget close to the pin */
3847 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3848 struct nid_path *path)
3850 unsigned int val = 0;
3854 for (depth = 0; depth < 3; depth++) {
3855 if (depth >= path->depth - 1)
3857 nid = path->path[depth];
3858 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3859 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3861 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3862 path->idx[depth])) {
3863 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3872 static int parse_mic_boost(struct hda_codec *codec)
3874 struct hda_gen_spec *spec = codec->spec;
3875 struct auto_pin_cfg *cfg = &spec->autocfg;
3876 struct hda_input_mux *imux = &spec->input_mux;
3879 if (!spec->num_adc_nids)
3882 for (i = 0; i < imux->num_items; i++) {
3883 struct nid_path *path;
3886 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3888 idx = imux->items[i].index;
3889 if (idx >= imux->num_items)
3892 /* check only line-in and mic pins */
3893 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3896 path = get_input_path(codec, 0, i);
3900 val = look_for_boost_amp(codec, path);
3904 /* create a boost control */
3905 snprintf(boost_label, sizeof(boost_label),
3906 "%s Boost Volume", spec->input_labels[idx]);
3907 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3908 spec->input_label_idxs[idx], val))
3911 path->ctls[NID_PATH_BOOST_CTL] = val;
3917 * mic mute LED hook helpers
3922 MICMUTE_LED_FOLLOW_CAPTURE,
3923 MICMUTE_LED_FOLLOW_MUTE,
3926 static void call_micmute_led_update(struct hda_codec *codec)
3928 struct hda_gen_spec *spec = codec->spec;
3931 switch (spec->micmute_led.led_mode) {
3932 case MICMUTE_LED_ON:
3935 case MICMUTE_LED_OFF:
3938 case MICMUTE_LED_FOLLOW_CAPTURE:
3939 val = !!spec->micmute_led.capture;
3941 case MICMUTE_LED_FOLLOW_MUTE:
3943 val = !spec->micmute_led.capture;
3947 if (val == spec->micmute_led.led_value)
3949 spec->micmute_led.led_value = val;
3950 if (spec->micmute_led.update)
3951 spec->micmute_led.update(codec);
3954 static void update_micmute_led(struct hda_codec *codec,
3955 struct snd_kcontrol *kcontrol,
3956 struct snd_ctl_elem_value *ucontrol)
3958 struct hda_gen_spec *spec = codec->spec;
3961 if (spec->micmute_led.old_hook)
3962 spec->micmute_led.old_hook(codec, kcontrol, ucontrol);
3966 mask = 1U << snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3967 if (!strcmp("Capture Switch", ucontrol->id.name)) {
3968 /* TODO: How do I verify if it's a mono or stereo here? */
3969 if (ucontrol->value.integer.value[0] ||
3970 ucontrol->value.integer.value[1])
3971 spec->micmute_led.capture |= mask;
3973 spec->micmute_led.capture &= ~mask;
3974 call_micmute_led_update(codec);
3978 static int micmute_led_mode_info(struct snd_kcontrol *kcontrol,
3979 struct snd_ctl_elem_info *uinfo)
3981 static const char * const texts[] = {
3982 "On", "Off", "Follow Capture", "Follow Mute",
3985 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
3988 static int micmute_led_mode_get(struct snd_kcontrol *kcontrol,
3989 struct snd_ctl_elem_value *ucontrol)
3991 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3992 struct hda_gen_spec *spec = codec->spec;
3994 ucontrol->value.enumerated.item[0] = spec->micmute_led.led_mode;
3998 static int micmute_led_mode_put(struct snd_kcontrol *kcontrol,
3999 struct snd_ctl_elem_value *ucontrol)
4001 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4002 struct hda_gen_spec *spec = codec->spec;
4005 mode = ucontrol->value.enumerated.item[0];
4006 if (mode > MICMUTE_LED_FOLLOW_MUTE)
4007 mode = MICMUTE_LED_FOLLOW_MUTE;
4008 if (mode == spec->micmute_led.led_mode)
4010 spec->micmute_led.led_mode = mode;
4011 call_micmute_led_update(codec);
4015 static const struct snd_kcontrol_new micmute_led_mode_ctl = {
4016 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4017 .name = "Mic Mute-LED Mode",
4018 .info = micmute_led_mode_info,
4019 .get = micmute_led_mode_get,
4020 .put = micmute_led_mode_put,
4024 * snd_hda_gen_add_micmute_led - helper for setting up mic mute LED hook
4025 * @codec: the HDA codec
4026 * @hook: the callback for updating LED
4028 * Called from the codec drivers for offering the mic mute LED controls.
4029 * When established, it sets up cap_sync_hook and triggers the callback at
4030 * each time when the capture mixer switch changes. The callback is supposed
4031 * to update the LED accordingly.
4033 * Returns 0 if the hook is established or a negative error code.
4035 int snd_hda_gen_add_micmute_led(struct hda_codec *codec,
4036 void (*hook)(struct hda_codec *))
4038 struct hda_gen_spec *spec = codec->spec;
4040 spec->micmute_led.led_mode = MICMUTE_LED_FOLLOW_MUTE;
4041 spec->micmute_led.capture = 0;
4042 spec->micmute_led.led_value = -1;
4043 spec->micmute_led.old_hook = spec->cap_sync_hook;
4044 spec->micmute_led.update = hook;
4045 spec->cap_sync_hook = update_micmute_led;
4046 if (!snd_hda_gen_add_kctl(spec, NULL, &micmute_led_mode_ctl))
4050 EXPORT_SYMBOL_GPL(snd_hda_gen_add_micmute_led);
4053 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
4055 static void parse_digital(struct hda_codec *codec)
4057 struct hda_gen_spec *spec = codec->spec;
4058 struct nid_path *path;
4060 hda_nid_t dig_nid, pin;
4062 /* support multiple SPDIFs; the secondary is set up as a slave */
4064 for (i = 0; i < spec->autocfg.dig_outs; i++) {
4065 pin = spec->autocfg.dig_out_pins[i];
4066 dig_nid = look_for_dac(codec, pin, true);
4069 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
4072 print_nid_path(codec, "digout", path);
4073 path->active = true;
4074 path->pin_fixed = true; /* no jack detection */
4075 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
4076 set_pin_target(codec, pin, PIN_OUT, false);
4078 spec->multiout.dig_out_nid = dig_nid;
4079 spec->dig_out_type = spec->autocfg.dig_out_type[0];
4081 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
4082 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
4084 spec->slave_dig_outs[nums - 1] = dig_nid;
4089 if (spec->autocfg.dig_in_pin) {
4090 pin = spec->autocfg.dig_in_pin;
4091 for_each_hda_codec_node(dig_nid, codec) {
4092 unsigned int wcaps = get_wcaps(codec, dig_nid);
4093 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
4095 if (!(wcaps & AC_WCAP_DIGITAL))
4097 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
4099 print_nid_path(codec, "digin", path);
4100 path->active = true;
4101 path->pin_fixed = true; /* no jack */
4102 spec->dig_in_nid = dig_nid;
4103 spec->digin_path = snd_hda_get_path_idx(codec, path);
4104 set_pin_target(codec, pin, PIN_IN, false);
4113 * input MUX handling
4116 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
4118 /* select the given imux item; either unmute exclusively or select the route */
4119 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
4122 struct hda_gen_spec *spec = codec->spec;
4123 const struct hda_input_mux *imux;
4124 struct nid_path *old_path, *path;
4126 imux = &spec->input_mux;
4127 if (!imux->num_items)
4130 if (idx >= imux->num_items)
4131 idx = imux->num_items - 1;
4132 if (spec->cur_mux[adc_idx] == idx)
4135 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
4138 if (old_path->active)
4139 snd_hda_activate_path(codec, old_path, false, false);
4141 spec->cur_mux[adc_idx] = idx;
4144 update_hp_mic(codec, adc_idx, false);
4146 if (spec->dyn_adc_switch)
4147 dyn_adc_pcm_resetup(codec, idx);
4149 path = get_input_path(codec, adc_idx, idx);
4154 snd_hda_activate_path(codec, path, true, false);
4155 if (spec->cap_sync_hook)
4156 spec->cap_sync_hook(codec, NULL, NULL);
4157 path_power_down_sync(codec, old_path);
4161 /* power up/down widgets in the all paths that match with the given NID
4162 * as terminals (either start- or endpoint)
4164 * returns the last changed NID, or zero if unchanged.
4166 static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
4167 int pin_state, int stream_state)
4169 struct hda_gen_spec *spec = codec->spec;
4170 hda_nid_t last, changed = 0;
4171 struct nid_path *path;
4174 snd_array_for_each(&spec->paths, n, path) {
4177 if (path->path[0] == nid ||
4178 path->path[path->depth - 1] == nid) {
4179 bool pin_old = path->pin_enabled;
4180 bool stream_old = path->stream_enabled;
4183 path->pin_enabled = pin_state;
4184 if (stream_state >= 0)
4185 path->stream_enabled = stream_state;
4186 if ((!path->pin_fixed && path->pin_enabled != pin_old)
4187 || path->stream_enabled != stream_old) {
4188 last = path_power_update(codec, path, true);
4197 /* check the jack status for power control */
4198 static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4200 if (!is_jack_detectable(codec, pin))
4202 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4205 /* power up/down the paths of the given pin according to the jack state;
4206 * power = 0/1 : only power up/down if it matches with the jack state,
4207 * < 0 : force power up/down to follow the jack sate
4209 * returns the last changed NID, or zero if unchanged.
4211 static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4216 if (!codec->power_save_node)
4219 on = detect_pin_state(codec, pin);
4221 if (power >= 0 && on != power)
4223 return set_path_power(codec, pin, on, -1);
4226 static void pin_power_callback(struct hda_codec *codec,
4227 struct hda_jack_callback *jack,
4230 if (jack && jack->nid)
4231 sync_power_state_change(codec,
4232 set_pin_power_jack(codec, jack->nid, on));
4235 /* callback only doing power up -- called at first */
4236 static void pin_power_up_callback(struct hda_codec *codec,
4237 struct hda_jack_callback *jack)
4239 pin_power_callback(codec, jack, true);
4242 /* callback only doing power down -- called at last */
4243 static void pin_power_down_callback(struct hda_codec *codec,
4244 struct hda_jack_callback *jack)
4246 pin_power_callback(codec, jack, false);
4249 /* set up the power up/down callbacks */
4250 static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4251 const hda_nid_t *pins, bool on)
4254 hda_jack_callback_fn cb =
4255 on ? pin_power_up_callback : pin_power_down_callback;
4257 for (i = 0; i < num_pins && pins[i]; i++) {
4258 if (is_jack_detectable(codec, pins[i]))
4259 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4261 set_path_power(codec, pins[i], true, -1);
4265 /* enabled power callback to each available I/O pin with jack detections;
4266 * the digital I/O pins are excluded because of the unreliable detectsion
4268 static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4270 struct hda_gen_spec *spec = codec->spec;
4271 struct auto_pin_cfg *cfg = &spec->autocfg;
4274 if (!codec->power_save_node)
4276 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4277 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4278 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4279 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4280 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4281 for (i = 0; i < cfg->num_inputs; i++)
4282 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4285 /* sync path power up/down with the jack states of given pins */
4286 static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4287 const hda_nid_t *pins)
4291 for (i = 0; i < num_pins && pins[i]; i++)
4292 if (is_jack_detectable(codec, pins[i]))
4293 set_pin_power_jack(codec, pins[i], -1);
4296 /* sync path power up/down with pins; called at init and resume */
4297 static void sync_all_pin_power_ctls(struct hda_codec *codec)
4299 struct hda_gen_spec *spec = codec->spec;
4300 struct auto_pin_cfg *cfg = &spec->autocfg;
4303 if (!codec->power_save_node)
4305 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4306 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4307 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4308 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4309 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4310 for (i = 0; i < cfg->num_inputs; i++)
4311 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4314 /* add fake paths if not present yet */
4315 static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4316 int num_pins, const hda_nid_t *pins)
4318 struct hda_gen_spec *spec = codec->spec;
4319 struct nid_path *path;
4322 for (i = 0; i < num_pins; i++) {
4325 if (get_nid_path(codec, nid, pins[i], 0))
4327 path = snd_array_new(&spec->paths);
4330 memset(path, 0, sizeof(*path));
4332 path->path[0] = nid;
4333 path->path[1] = pins[i];
4334 path->active = true;
4339 /* create fake paths to all outputs from beep */
4340 static int add_fake_beep_paths(struct hda_codec *codec)
4342 struct hda_gen_spec *spec = codec->spec;
4343 struct auto_pin_cfg *cfg = &spec->autocfg;
4344 hda_nid_t nid = spec->beep_nid;
4347 if (!codec->power_save_node || !nid)
4349 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4352 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4353 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4357 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4358 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4366 /* power up/down beep widget and its output paths */
4367 static void beep_power_hook(struct hda_beep *beep, bool on)
4369 set_path_power(beep->codec, beep->nid, -1, on);
4373 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4374 * @codec: the HDA codec
4375 * @pin: NID of pin to fix
4377 int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4379 struct hda_gen_spec *spec = codec->spec;
4380 struct nid_path *path;
4382 path = snd_array_new(&spec->paths);
4385 memset(path, 0, sizeof(*path));
4387 path->path[0] = pin;
4388 path->active = true;
4389 path->pin_fixed = true;
4390 path->stream_enabled = true;
4393 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4396 * Jack detections for HP auto-mute and mic-switch
4399 /* check each pin in the given array; returns true if any of them is plugged */
4400 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4403 bool present = false;
4405 for (i = 0; i < num_pins; i++) {
4406 hda_nid_t nid = pins[i];
4409 /* don't detect pins retasked as inputs */
4410 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4412 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4418 /* standard HP/line-out auto-mute helper */
4419 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
4420 int *paths, bool mute)
4422 struct hda_gen_spec *spec = codec->spec;
4425 for (i = 0; i < num_pins; i++) {
4426 hda_nid_t nid = pins[i];
4427 unsigned int val, oldval;
4431 oldval = snd_hda_codec_get_pin_target(codec, nid);
4432 if (oldval & PIN_IN)
4433 continue; /* no mute for inputs */
4435 if (spec->auto_mute_via_amp) {
4436 struct nid_path *path;
4439 path = snd_hda_get_path_from_idx(codec, paths[i]);
4442 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4446 spec->mute_bits |= (1ULL << mute_nid);
4448 spec->mute_bits &= ~(1ULL << mute_nid);
4451 /* don't reset VREF value in case it's controlling
4452 * the amp (see alc861_fixup_asus_amp_vref_0f())
4454 if (spec->keep_vref_in_automute)
4455 val = oldval & ~PIN_HP;
4460 /* here we call update_pin_ctl() so that the pinctl is
4461 * changed without changing the pinctl target value;
4462 * the original target value will be still referred at
4463 * the init / resume again
4465 update_pin_ctl(codec, nid, val);
4468 set_pin_eapd(codec, nid, !mute);
4469 if (codec->power_save_node) {
4472 on = detect_pin_state(codec, nid);
4473 set_path_power(codec, nid, on, -1);
4479 * snd_hda_gen_update_outputs - Toggle outputs muting
4480 * @codec: the HDA codec
4482 * Update the mute status of all outputs based on the current jack states.
4484 void snd_hda_gen_update_outputs(struct hda_codec *codec)
4486 struct hda_gen_spec *spec = codec->spec;
4490 /* Control HP pins/amps depending on master_mute state;
4491 * in general, HP pins/amps control should be enabled in all cases,
4492 * but currently set only for master_mute, just to be safe
4494 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4495 paths = spec->out_paths;
4497 paths = spec->hp_paths;
4498 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
4499 spec->autocfg.hp_pins, paths, spec->master_mute);
4501 if (!spec->automute_speaker)
4504 on = spec->hp_jack_present | spec->line_jack_present;
4505 on |= spec->master_mute;
4506 spec->speaker_muted = on;
4507 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4508 paths = spec->out_paths;
4510 paths = spec->speaker_paths;
4511 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
4512 spec->autocfg.speaker_pins, paths, on);
4514 /* toggle line-out mutes if needed, too */
4515 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4516 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4517 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4519 if (!spec->automute_lo)
4522 on = spec->hp_jack_present;
4523 on |= spec->master_mute;
4524 spec->line_out_muted = on;
4525 paths = spec->out_paths;
4526 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4527 spec->autocfg.line_out_pins, paths, on);
4529 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4531 static void call_update_outputs(struct hda_codec *codec)
4533 struct hda_gen_spec *spec = codec->spec;
4534 if (spec->automute_hook)
4535 spec->automute_hook(codec);
4537 snd_hda_gen_update_outputs(codec);
4539 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4540 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4541 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4545 * snd_hda_gen_hp_automute - standard HP-automute helper
4546 * @codec: the HDA codec
4547 * @jack: jack object, NULL for the whole
4549 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4550 struct hda_jack_callback *jack)
4552 struct hda_gen_spec *spec = codec->spec;
4553 hda_nid_t *pins = spec->autocfg.hp_pins;
4554 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4556 /* No detection for the first HP jack during indep-HP mode */
4557 if (spec->indep_hp_enabled) {
4562 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4563 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4565 call_update_outputs(codec);
4567 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4570 * snd_hda_gen_line_automute - standard line-out-automute helper
4571 * @codec: the HDA codec
4572 * @jack: jack object, NULL for the whole
4574 void snd_hda_gen_line_automute(struct hda_codec *codec,
4575 struct hda_jack_callback *jack)
4577 struct hda_gen_spec *spec = codec->spec;
4579 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4581 /* check LO jack only when it's different from HP */
4582 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4585 spec->line_jack_present =
4586 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4587 spec->autocfg.line_out_pins);
4588 if (!spec->automute_speaker || !spec->detect_lo)
4590 call_update_outputs(codec);
4592 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4595 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4596 * @codec: the HDA codec
4597 * @jack: jack object, NULL for the whole
4599 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4600 struct hda_jack_callback *jack)
4602 struct hda_gen_spec *spec = codec->spec;
4605 if (!spec->auto_mic)
4608 for (i = spec->am_num_entries - 1; i > 0; i--) {
4609 hda_nid_t pin = spec->am_entry[i].pin;
4610 /* don't detect pins retasked as outputs */
4611 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4613 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4614 mux_select(codec, 0, spec->am_entry[i].idx);
4618 mux_select(codec, 0, spec->am_entry[0].idx);
4620 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4622 /* call appropriate hooks */
4623 static void call_hp_automute(struct hda_codec *codec,
4624 struct hda_jack_callback *jack)
4626 struct hda_gen_spec *spec = codec->spec;
4627 if (spec->hp_automute_hook)
4628 spec->hp_automute_hook(codec, jack);
4630 snd_hda_gen_hp_automute(codec, jack);
4633 static void call_line_automute(struct hda_codec *codec,
4634 struct hda_jack_callback *jack)
4636 struct hda_gen_spec *spec = codec->spec;
4637 if (spec->line_automute_hook)
4638 spec->line_automute_hook(codec, jack);
4640 snd_hda_gen_line_automute(codec, jack);
4643 static void call_mic_autoswitch(struct hda_codec *codec,
4644 struct hda_jack_callback *jack)
4646 struct hda_gen_spec *spec = codec->spec;
4647 if (spec->mic_autoswitch_hook)
4648 spec->mic_autoswitch_hook(codec, jack);
4650 snd_hda_gen_mic_autoswitch(codec, jack);
4653 /* update jack retasking */
4654 static void update_automute_all(struct hda_codec *codec)
4656 call_hp_automute(codec, NULL);
4657 call_line_automute(codec, NULL);
4658 call_mic_autoswitch(codec, NULL);
4662 * Auto-Mute mode mixer enum support
4664 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4665 struct snd_ctl_elem_info *uinfo)
4667 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4668 struct hda_gen_spec *spec = codec->spec;
4669 static const char * const texts3[] = {
4670 "Disabled", "Speaker Only", "Line Out+Speaker"
4673 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4674 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4675 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4678 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4679 struct snd_ctl_elem_value *ucontrol)
4681 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4682 struct hda_gen_spec *spec = codec->spec;
4683 unsigned int val = 0;
4684 if (spec->automute_speaker)
4686 if (spec->automute_lo)
4689 ucontrol->value.enumerated.item[0] = val;
4693 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4694 struct snd_ctl_elem_value *ucontrol)
4696 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4697 struct hda_gen_spec *spec = codec->spec;
4699 switch (ucontrol->value.enumerated.item[0]) {
4701 if (!spec->automute_speaker && !spec->automute_lo)
4703 spec->automute_speaker = 0;
4704 spec->automute_lo = 0;
4707 if (spec->automute_speaker_possible) {
4708 if (!spec->automute_lo && spec->automute_speaker)
4710 spec->automute_speaker = 1;
4711 spec->automute_lo = 0;
4712 } else if (spec->automute_lo_possible) {
4713 if (spec->automute_lo)
4715 spec->automute_lo = 1;
4720 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4722 if (spec->automute_speaker && spec->automute_lo)
4724 spec->automute_speaker = 1;
4725 spec->automute_lo = 1;
4730 call_update_outputs(codec);
4734 static const struct snd_kcontrol_new automute_mode_enum = {
4735 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4736 .name = "Auto-Mute Mode",
4737 .info = automute_mode_info,
4738 .get = automute_mode_get,
4739 .put = automute_mode_put,
4742 static int add_automute_mode_enum(struct hda_codec *codec)
4744 struct hda_gen_spec *spec = codec->spec;
4746 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4752 * Check the availability of HP/line-out auto-mute;
4753 * Set up appropriately if really supported
4755 static int check_auto_mute_availability(struct hda_codec *codec)
4757 struct hda_gen_spec *spec = codec->spec;
4758 struct auto_pin_cfg *cfg = &spec->autocfg;
4762 if (spec->suppress_auto_mute)
4765 if (cfg->hp_pins[0])
4767 if (cfg->line_out_pins[0])
4769 if (cfg->speaker_pins[0])
4771 if (present < 2) /* need two different output types */
4774 if (!cfg->speaker_pins[0] &&
4775 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4776 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4777 sizeof(cfg->speaker_pins));
4778 cfg->speaker_outs = cfg->line_outs;
4781 if (!cfg->hp_pins[0] &&
4782 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4783 memcpy(cfg->hp_pins, cfg->line_out_pins,
4784 sizeof(cfg->hp_pins));
4785 cfg->hp_outs = cfg->line_outs;
4788 for (i = 0; i < cfg->hp_outs; i++) {
4789 hda_nid_t nid = cfg->hp_pins[i];
4790 if (!is_jack_detectable(codec, nid))
4792 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4793 snd_hda_jack_detect_enable_callback(codec, nid,
4795 spec->detect_hp = 1;
4798 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4799 if (cfg->speaker_outs)
4800 for (i = 0; i < cfg->line_outs; i++) {
4801 hda_nid_t nid = cfg->line_out_pins[i];
4802 if (!is_jack_detectable(codec, nid))
4804 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4805 snd_hda_jack_detect_enable_callback(codec, nid,
4806 call_line_automute);
4807 spec->detect_lo = 1;
4809 spec->automute_lo_possible = spec->detect_hp;
4812 spec->automute_speaker_possible = cfg->speaker_outs &&
4813 (spec->detect_hp || spec->detect_lo);
4815 spec->automute_lo = spec->automute_lo_possible;
4816 spec->automute_speaker = spec->automute_speaker_possible;
4818 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4819 /* create a control for automute mode */
4820 err = add_automute_mode_enum(codec);
4827 /* check whether all auto-mic pins are valid; setup indices if OK */
4828 static bool auto_mic_check_imux(struct hda_codec *codec)
4830 struct hda_gen_spec *spec = codec->spec;
4831 const struct hda_input_mux *imux;
4834 imux = &spec->input_mux;
4835 for (i = 0; i < spec->am_num_entries; i++) {
4836 spec->am_entry[i].idx =
4837 find_idx_in_nid_list(spec->am_entry[i].pin,
4838 spec->imux_pins, imux->num_items);
4839 if (spec->am_entry[i].idx < 0)
4840 return false; /* no corresponding imux */
4843 /* we don't need the jack detection for the first pin */
4844 for (i = 1; i < spec->am_num_entries; i++)
4845 snd_hda_jack_detect_enable_callback(codec,
4846 spec->am_entry[i].pin,
4847 call_mic_autoswitch);
4851 static int compare_attr(const void *ap, const void *bp)
4853 const struct automic_entry *a = ap;
4854 const struct automic_entry *b = bp;
4855 return (int)(a->attr - b->attr);
4859 * Check the availability of auto-mic switch;
4860 * Set up if really supported
4862 static int check_auto_mic_availability(struct hda_codec *codec)
4864 struct hda_gen_spec *spec = codec->spec;
4865 struct auto_pin_cfg *cfg = &spec->autocfg;
4869 if (spec->suppress_auto_mic)
4874 for (i = 0; i < cfg->num_inputs; i++) {
4875 hda_nid_t nid = cfg->inputs[i].pin;
4877 attr = snd_hda_codec_get_pincfg(codec, nid);
4878 attr = snd_hda_get_input_pin_attr(attr);
4879 if (types & (1 << attr))
4880 return 0; /* already occupied */
4882 case INPUT_PIN_ATTR_INT:
4883 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4884 return 0; /* invalid type */
4886 case INPUT_PIN_ATTR_UNUSED:
4887 return 0; /* invalid entry */
4889 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4890 return 0; /* invalid type */
4891 if (!spec->line_in_auto_switch &&
4892 cfg->inputs[i].type != AUTO_PIN_MIC)
4893 return 0; /* only mic is allowed */
4894 if (!is_jack_detectable(codec, nid))
4895 return 0; /* no unsol support */
4898 if (num_pins >= MAX_AUTO_MIC_PINS)
4900 types |= (1 << attr);
4901 spec->am_entry[num_pins].pin = nid;
4902 spec->am_entry[num_pins].attr = attr;
4909 spec->am_num_entries = num_pins;
4910 /* sort the am_entry in the order of attr so that the pin with a
4911 * higher attr will be selected when the jack is plugged.
4913 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4914 compare_attr, NULL);
4916 if (!auto_mic_check_imux(codec))
4920 spec->num_adc_nids = 1;
4921 spec->cur_mux[0] = spec->am_entry[0].idx;
4922 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4923 spec->am_entry[0].pin,
4924 spec->am_entry[1].pin,
4925 spec->am_entry[2].pin);
4931 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4933 * @codec: the HDA codec
4934 * @nid: NID to evalute
4935 * @power_state: target power state
4937 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4939 unsigned int power_state)
4941 struct hda_gen_spec *spec = codec->spec;
4943 if (!spec->power_down_unused && !codec->power_save_node)
4945 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
4947 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4949 if (is_active_nid_for_any(codec, nid))
4953 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4955 /* mute all aamix inputs initially; parse up to the first leaves */
4956 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4959 const hda_nid_t *conn;
4962 nums = snd_hda_get_conn_list(codec, mix, &conn);
4963 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4964 for (i = 0; i < nums; i++) {
4966 update_amp(codec, mix, HDA_INPUT, i,
4967 0xff, HDA_AMP_MUTE);
4968 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4969 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4970 0xff, HDA_AMP_MUTE);
4975 * snd_hda_gen_stream_pm - Stream power management callback
4976 * @codec: the HDA codec
4977 * @nid: audio widget
4978 * @on: power on/off flag
4980 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
4982 void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4984 if (codec->power_save_node)
4985 set_path_power(codec, nid, -1, on);
4987 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4990 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4991 * set up the hda_gen_spec
4992 * @codec: the HDA codec
4993 * @cfg: Parsed pin configuration
4995 * return 1 if successful, 0 if the proper config is not found,
4996 * or a negative error code
4998 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4999 struct auto_pin_cfg *cfg)
5001 struct hda_gen_spec *spec = codec->spec;
5004 parse_user_hints(codec);
5006 if (spec->mixer_nid && !spec->mixer_merge_nid)
5007 spec->mixer_merge_nid = spec->mixer_nid;
5009 if (cfg != &spec->autocfg) {
5010 spec->autocfg = *cfg;
5011 cfg = &spec->autocfg;
5014 if (!spec->main_out_badness)
5015 spec->main_out_badness = &hda_main_out_badness;
5016 if (!spec->extra_out_badness)
5017 spec->extra_out_badness = &hda_extra_out_badness;
5019 fill_all_dac_nids(codec);
5021 if (!cfg->line_outs) {
5022 if (cfg->dig_outs || cfg->dig_in_pin) {
5023 spec->multiout.max_channels = 2;
5024 spec->no_analog = 1;
5027 if (!cfg->num_inputs && !cfg->dig_in_pin)
5028 return 0; /* can't find valid BIOS pin config */
5031 if (!spec->no_primary_hp &&
5032 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
5033 cfg->line_outs <= cfg->hp_outs) {
5034 /* use HP as primary out */
5035 cfg->speaker_outs = cfg->line_outs;
5036 memcpy(cfg->speaker_pins, cfg->line_out_pins,
5037 sizeof(cfg->speaker_pins));
5038 cfg->line_outs = cfg->hp_outs;
5039 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
5041 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
5042 cfg->line_out_type = AUTO_PIN_HP_OUT;
5045 err = parse_output_paths(codec);
5048 err = create_multi_channel_mode(codec);
5051 err = create_multi_out_ctls(codec, cfg);
5054 err = create_hp_out_ctls(codec);
5057 err = create_speaker_out_ctls(codec);
5060 err = create_indep_hp_ctls(codec);
5063 err = create_loopback_mixing_ctl(codec);
5066 err = create_hp_mic(codec);
5069 err = create_input_ctls(codec);
5073 /* add power-down pin callbacks at first */
5074 add_all_pin_power_ctls(codec, false);
5076 spec->const_channel_count = spec->ext_channel_count;
5077 /* check the multiple speaker and headphone pins */
5078 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
5079 spec->const_channel_count = max(spec->const_channel_count,
5080 cfg->speaker_outs * 2);
5081 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
5082 spec->const_channel_count = max(spec->const_channel_count,
5084 spec->multiout.max_channels = max(spec->ext_channel_count,
5085 spec->const_channel_count);
5087 err = check_auto_mute_availability(codec);
5091 err = check_dyn_adc_switch(codec);
5095 err = check_auto_mic_availability(codec);
5099 /* add stereo mix if available and not enabled yet */
5100 if (!spec->auto_mic && spec->mixer_nid &&
5101 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
5102 spec->input_mux.num_items > 1) {
5103 err = parse_capture_source(codec, spec->mixer_nid,
5104 CFG_IDX_MIX, spec->num_all_adcs,
5111 err = create_capture_mixers(codec);
5115 err = parse_mic_boost(codec);
5119 /* create "Headphone Mic Jack Mode" if no input selection is
5120 * available (or user specifies add_jack_modes hint)
5122 if (spec->hp_mic_pin &&
5123 (spec->auto_mic || spec->input_mux.num_items == 1 ||
5124 spec->add_jack_modes)) {
5125 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
5130 if (spec->add_jack_modes) {
5131 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
5132 err = create_out_jack_modes(codec, cfg->line_outs,
5133 cfg->line_out_pins);
5137 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
5138 err = create_out_jack_modes(codec, cfg->hp_outs,
5145 /* add power-up pin callbacks at last */
5146 add_all_pin_power_ctls(codec, true);
5148 /* mute all aamix input initially */
5149 if (spec->mixer_nid)
5150 mute_all_mixer_nid(codec, spec->mixer_nid);
5153 parse_digital(codec);
5155 if (spec->power_down_unused || codec->power_save_node) {
5156 if (!codec->power_filter)
5157 codec->power_filter = snd_hda_gen_path_power_filter;
5158 if (!codec->patch_ops.stream_pm)
5159 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
5162 if (!spec->no_analog && spec->beep_nid) {
5163 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
5166 if (codec->beep && codec->power_save_node) {
5167 err = add_fake_beep_paths(codec);
5170 codec->beep->power_hook = beep_power_hook;
5176 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
5180 * Build control elements
5183 /* slave controls for virtual master */
5184 static const char * const slave_pfxs[] = {
5185 "Front", "Surround", "Center", "LFE", "Side",
5186 "Headphone", "Speaker", "Mono", "Line Out",
5187 "CLFE", "Bass Speaker", "PCM",
5188 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5189 "Headphone Front", "Headphone Surround", "Headphone CLFE",
5190 "Headphone Side", "Headphone+LO", "Speaker+LO",
5195 * snd_hda_gen_build_controls - Build controls from the parsed results
5196 * @codec: the HDA codec
5198 * Pass this to build_controls patch_ops.
5200 int snd_hda_gen_build_controls(struct hda_codec *codec)
5202 struct hda_gen_spec *spec = codec->spec;
5205 if (spec->kctls.used) {
5206 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5211 if (spec->multiout.dig_out_nid) {
5212 err = snd_hda_create_dig_out_ctls(codec,
5213 spec->multiout.dig_out_nid,
5214 spec->multiout.dig_out_nid,
5215 spec->pcm_rec[1]->pcm_type);
5218 if (!spec->no_analog) {
5219 err = snd_hda_create_spdif_share_sw(codec,
5223 spec->multiout.share_spdif = 1;
5226 if (spec->dig_in_nid) {
5227 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5232 /* if we have no master control, let's create it */
5233 if (!spec->no_analog && !spec->suppress_vmaster &&
5234 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
5235 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
5236 spec->vmaster_tlv, slave_pfxs,
5241 if (!spec->no_analog && !spec->suppress_vmaster &&
5242 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5243 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5246 true, &spec->vmaster_mute.sw_kctl);
5249 if (spec->vmaster_mute.hook) {
5250 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5251 spec->vmaster_mute_enum);
5252 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5256 free_kctls(spec); /* no longer needed */
5258 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5264 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
5271 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5272 struct hda_codec *codec,
5273 struct snd_pcm_substream *substream,
5276 struct hda_gen_spec *spec = codec->spec;
5277 if (spec->pcm_playback_hook)
5278 spec->pcm_playback_hook(hinfo, codec, substream, action);
5281 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5282 struct hda_codec *codec,
5283 struct snd_pcm_substream *substream,
5286 struct hda_gen_spec *spec = codec->spec;
5287 if (spec->pcm_capture_hook)
5288 spec->pcm_capture_hook(hinfo, codec, substream, action);
5292 * Analog playback callbacks
5294 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5295 struct hda_codec *codec,
5296 struct snd_pcm_substream *substream)
5298 struct hda_gen_spec *spec = codec->spec;
5301 mutex_lock(&spec->pcm_mutex);
5302 err = snd_hda_multi_out_analog_open(codec,
5303 &spec->multiout, substream,
5306 spec->active_streams |= 1 << STREAM_MULTI_OUT;
5307 call_pcm_playback_hook(hinfo, codec, substream,
5308 HDA_GEN_PCM_ACT_OPEN);
5310 mutex_unlock(&spec->pcm_mutex);
5314 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5315 struct hda_codec *codec,
5316 unsigned int stream_tag,
5317 unsigned int format,
5318 struct snd_pcm_substream *substream)
5320 struct hda_gen_spec *spec = codec->spec;
5323 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5324 stream_tag, format, substream);
5326 call_pcm_playback_hook(hinfo, codec, substream,
5327 HDA_GEN_PCM_ACT_PREPARE);
5331 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5332 struct hda_codec *codec,
5333 struct snd_pcm_substream *substream)
5335 struct hda_gen_spec *spec = codec->spec;
5338 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5340 call_pcm_playback_hook(hinfo, codec, substream,
5341 HDA_GEN_PCM_ACT_CLEANUP);
5345 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5346 struct hda_codec *codec,
5347 struct snd_pcm_substream *substream)
5349 struct hda_gen_spec *spec = codec->spec;
5350 mutex_lock(&spec->pcm_mutex);
5351 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
5352 call_pcm_playback_hook(hinfo, codec, substream,
5353 HDA_GEN_PCM_ACT_CLOSE);
5354 mutex_unlock(&spec->pcm_mutex);
5358 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5359 struct hda_codec *codec,
5360 struct snd_pcm_substream *substream)
5362 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5366 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5367 struct hda_codec *codec,
5368 unsigned int stream_tag,
5369 unsigned int format,
5370 struct snd_pcm_substream *substream)
5372 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5373 call_pcm_capture_hook(hinfo, codec, substream,
5374 HDA_GEN_PCM_ACT_PREPARE);
5378 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5379 struct hda_codec *codec,
5380 struct snd_pcm_substream *substream)
5382 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5383 call_pcm_capture_hook(hinfo, codec, substream,
5384 HDA_GEN_PCM_ACT_CLEANUP);
5388 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5389 struct hda_codec *codec,
5390 struct snd_pcm_substream *substream)
5392 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5396 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5397 struct hda_codec *codec,
5398 struct snd_pcm_substream *substream)
5400 struct hda_gen_spec *spec = codec->spec;
5403 mutex_lock(&spec->pcm_mutex);
5404 if (spec->indep_hp && !spec->indep_hp_enabled)
5407 spec->active_streams |= 1 << STREAM_INDEP_HP;
5408 call_pcm_playback_hook(hinfo, codec, substream,
5409 HDA_GEN_PCM_ACT_OPEN);
5410 mutex_unlock(&spec->pcm_mutex);
5414 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5415 struct hda_codec *codec,
5416 struct snd_pcm_substream *substream)
5418 struct hda_gen_spec *spec = codec->spec;
5419 mutex_lock(&spec->pcm_mutex);
5420 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
5421 call_pcm_playback_hook(hinfo, codec, substream,
5422 HDA_GEN_PCM_ACT_CLOSE);
5423 mutex_unlock(&spec->pcm_mutex);
5427 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5428 struct hda_codec *codec,
5429 unsigned int stream_tag,
5430 unsigned int format,
5431 struct snd_pcm_substream *substream)
5433 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5434 call_pcm_playback_hook(hinfo, codec, substream,
5435 HDA_GEN_PCM_ACT_PREPARE);
5439 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5440 struct hda_codec *codec,
5441 struct snd_pcm_substream *substream)
5443 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5444 call_pcm_playback_hook(hinfo, codec, substream,
5445 HDA_GEN_PCM_ACT_CLEANUP);
5452 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5453 struct hda_codec *codec,
5454 struct snd_pcm_substream *substream)
5456 struct hda_gen_spec *spec = codec->spec;
5457 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5460 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5461 struct hda_codec *codec,
5462 unsigned int stream_tag,
5463 unsigned int format,
5464 struct snd_pcm_substream *substream)
5466 struct hda_gen_spec *spec = codec->spec;
5467 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5468 stream_tag, format, substream);
5471 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5472 struct hda_codec *codec,
5473 struct snd_pcm_substream *substream)
5475 struct hda_gen_spec *spec = codec->spec;
5476 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5479 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5480 struct hda_codec *codec,
5481 struct snd_pcm_substream *substream)
5483 struct hda_gen_spec *spec = codec->spec;
5484 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5490 #define alt_capture_pcm_open capture_pcm_open
5491 #define alt_capture_pcm_close capture_pcm_close
5493 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5494 struct hda_codec *codec,
5495 unsigned int stream_tag,
5496 unsigned int format,
5497 struct snd_pcm_substream *substream)
5499 struct hda_gen_spec *spec = codec->spec;
5501 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5502 stream_tag, 0, format);
5503 call_pcm_capture_hook(hinfo, codec, substream,
5504 HDA_GEN_PCM_ACT_PREPARE);
5508 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5509 struct hda_codec *codec,
5510 struct snd_pcm_substream *substream)
5512 struct hda_gen_spec *spec = codec->spec;
5514 snd_hda_codec_cleanup_stream(codec,
5515 spec->adc_nids[substream->number + 1]);
5516 call_pcm_capture_hook(hinfo, codec, substream,
5517 HDA_GEN_PCM_ACT_CLEANUP);
5523 static const struct hda_pcm_stream pcm_analog_playback = {
5527 /* NID is set in build_pcms */
5529 .open = playback_pcm_open,
5530 .close = playback_pcm_close,
5531 .prepare = playback_pcm_prepare,
5532 .cleanup = playback_pcm_cleanup
5536 static const struct hda_pcm_stream pcm_analog_capture = {
5540 /* NID is set in build_pcms */
5542 .open = capture_pcm_open,
5543 .close = capture_pcm_close,
5544 .prepare = capture_pcm_prepare,
5545 .cleanup = capture_pcm_cleanup
5549 static const struct hda_pcm_stream pcm_analog_alt_playback = {
5553 /* NID is set in build_pcms */
5555 .open = alt_playback_pcm_open,
5556 .close = alt_playback_pcm_close,
5557 .prepare = alt_playback_pcm_prepare,
5558 .cleanup = alt_playback_pcm_cleanup
5562 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5563 .substreams = 2, /* can be overridden */
5566 /* NID is set in build_pcms */
5568 .open = alt_capture_pcm_open,
5569 .close = alt_capture_pcm_close,
5570 .prepare = alt_capture_pcm_prepare,
5571 .cleanup = alt_capture_pcm_cleanup
5575 static const struct hda_pcm_stream pcm_digital_playback = {
5579 /* NID is set in build_pcms */
5581 .open = dig_playback_pcm_open,
5582 .close = dig_playback_pcm_close,
5583 .prepare = dig_playback_pcm_prepare,
5584 .cleanup = dig_playback_pcm_cleanup
5588 static const struct hda_pcm_stream pcm_digital_capture = {
5592 /* NID is set in build_pcms */
5595 /* Used by build_pcms to flag that a PCM has no playback stream */
5596 static const struct hda_pcm_stream pcm_null_stream = {
5603 * dynamic changing ADC PCM streams
5605 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5607 struct hda_gen_spec *spec = codec->spec;
5608 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5610 if (spec->cur_adc && spec->cur_adc != new_adc) {
5611 /* stream is running, let's swap the current ADC */
5612 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5613 spec->cur_adc = new_adc;
5614 snd_hda_codec_setup_stream(codec, new_adc,
5615 spec->cur_adc_stream_tag, 0,
5616 spec->cur_adc_format);
5622 /* analog capture with dynamic dual-adc changes */
5623 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5624 struct hda_codec *codec,
5625 unsigned int stream_tag,
5626 unsigned int format,
5627 struct snd_pcm_substream *substream)
5629 struct hda_gen_spec *spec = codec->spec;
5630 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5631 spec->cur_adc_stream_tag = stream_tag;
5632 spec->cur_adc_format = format;
5633 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5634 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
5638 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5639 struct hda_codec *codec,
5640 struct snd_pcm_substream *substream)
5642 struct hda_gen_spec *spec = codec->spec;
5643 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5645 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
5649 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5653 .nid = 0, /* fill later */
5655 .prepare = dyn_adc_capture_pcm_prepare,
5656 .cleanup = dyn_adc_capture_pcm_cleanup
5660 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5661 const char *chip_name)
5667 strlcpy(str, chip_name, len);
5669 /* drop non-alnum chars after a space */
5670 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5671 if (!isalnum(p[1])) {
5676 strlcat(str, sfx, len);
5679 /* copy PCM stream info from @default_str, and override non-NULL entries
5680 * from @spec_str and @nid
5682 static void setup_pcm_stream(struct hda_pcm_stream *str,
5683 const struct hda_pcm_stream *default_str,
5684 const struct hda_pcm_stream *spec_str,
5687 *str = *default_str;
5691 if (spec_str->substreams)
5692 str->substreams = spec_str->substreams;
5693 if (spec_str->channels_min)
5694 str->channels_min = spec_str->channels_min;
5695 if (spec_str->channels_max)
5696 str->channels_max = spec_str->channels_max;
5697 if (spec_str->rates)
5698 str->rates = spec_str->rates;
5699 if (spec_str->formats)
5700 str->formats = spec_str->formats;
5701 if (spec_str->maxbps)
5702 str->maxbps = spec_str->maxbps;
5707 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5708 * @codec: the HDA codec
5710 * Pass this to build_pcms patch_ops.
5712 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5714 struct hda_gen_spec *spec = codec->spec;
5715 struct hda_pcm *info;
5716 bool have_multi_adcs;
5718 if (spec->no_analog)
5721 fill_pcm_stream_name(spec->stream_name_analog,
5722 sizeof(spec->stream_name_analog),
5723 " Analog", codec->core.chip_name);
5724 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5727 spec->pcm_rec[0] = info;
5729 if (spec->multiout.num_dacs > 0) {
5730 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5731 &pcm_analog_playback,
5732 spec->stream_analog_playback,
5733 spec->multiout.dac_nids[0]);
5734 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5735 spec->multiout.max_channels;
5736 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5737 spec->autocfg.line_outs == 2)
5738 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5741 if (spec->num_adc_nids) {
5742 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5743 (spec->dyn_adc_switch ?
5744 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5745 spec->stream_analog_capture,
5750 /* SPDIF for stream index #1 */
5751 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5752 fill_pcm_stream_name(spec->stream_name_digital,
5753 sizeof(spec->stream_name_digital),
5754 " Digital", codec->core.chip_name);
5755 info = snd_hda_codec_pcm_new(codec, "%s",
5756 spec->stream_name_digital);
5759 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5760 spec->pcm_rec[1] = info;
5761 if (spec->dig_out_type)
5762 info->pcm_type = spec->dig_out_type;
5764 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5765 if (spec->multiout.dig_out_nid)
5766 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5767 &pcm_digital_playback,
5768 spec->stream_digital_playback,
5769 spec->multiout.dig_out_nid);
5770 if (spec->dig_in_nid)
5771 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5772 &pcm_digital_capture,
5773 spec->stream_digital_capture,
5777 if (spec->no_analog)
5780 /* If the use of more than one ADC is requested for the current
5781 * model, configure a second analog capture-only PCM.
5783 have_multi_adcs = (spec->num_adc_nids > 1) &&
5784 !spec->dyn_adc_switch && !spec->auto_mic;
5785 /* Additional Analaog capture for index #2 */
5786 if (spec->alt_dac_nid || have_multi_adcs) {
5787 fill_pcm_stream_name(spec->stream_name_alt_analog,
5788 sizeof(spec->stream_name_alt_analog),
5789 " Alt Analog", codec->core.chip_name);
5790 info = snd_hda_codec_pcm_new(codec, "%s",
5791 spec->stream_name_alt_analog);
5794 spec->pcm_rec[2] = info;
5795 if (spec->alt_dac_nid)
5796 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5797 &pcm_analog_alt_playback,
5798 spec->stream_analog_alt_playback,
5801 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5802 &pcm_null_stream, NULL, 0);
5803 if (have_multi_adcs) {
5804 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5805 &pcm_analog_alt_capture,
5806 spec->stream_analog_alt_capture,
5808 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5809 spec->num_adc_nids - 1;
5811 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5812 &pcm_null_stream, NULL, 0);
5818 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5822 * Standard auto-parser initializations
5825 /* configure the given path as a proper output */
5826 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5828 struct nid_path *path;
5831 path = snd_hda_get_path_from_idx(codec, path_idx);
5832 if (!path || !path->depth)
5834 pin = path->path[path->depth - 1];
5835 restore_pin_ctl(codec, pin);
5836 snd_hda_activate_path(codec, path, path->active,
5837 aamix_default(codec->spec));
5838 set_pin_eapd(codec, pin, path->active);
5841 /* initialize primary output paths */
5842 static void init_multi_out(struct hda_codec *codec)
5844 struct hda_gen_spec *spec = codec->spec;
5847 for (i = 0; i < spec->autocfg.line_outs; i++)
5848 set_output_and_unmute(codec, spec->out_paths[i]);
5852 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5856 for (i = 0; i < num_outs; i++)
5857 set_output_and_unmute(codec, paths[i]);
5860 /* initialize hp and speaker paths */
5861 static void init_extra_out(struct hda_codec *codec)
5863 struct hda_gen_spec *spec = codec->spec;
5865 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5866 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5867 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5868 __init_extra_out(codec, spec->autocfg.speaker_outs,
5869 spec->speaker_paths);
5872 /* initialize multi-io paths */
5873 static void init_multi_io(struct hda_codec *codec)
5875 struct hda_gen_spec *spec = codec->spec;
5878 for (i = 0; i < spec->multi_ios; i++) {
5879 hda_nid_t pin = spec->multi_io[i].pin;
5880 struct nid_path *path;
5881 path = get_multiio_path(codec, i);
5884 if (!spec->multi_io[i].ctl_in)
5885 spec->multi_io[i].ctl_in =
5886 snd_hda_codec_get_pin_target(codec, pin);
5887 snd_hda_activate_path(codec, path, path->active,
5888 aamix_default(spec));
5892 static void init_aamix_paths(struct hda_codec *codec)
5894 struct hda_gen_spec *spec = codec->spec;
5896 if (!spec->have_aamix_ctl)
5898 if (!has_aamix_out_paths(spec))
5900 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5901 spec->aamix_out_paths[0],
5902 spec->autocfg.line_out_type);
5903 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5904 spec->aamix_out_paths[1],
5906 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5907 spec->aamix_out_paths[2],
5908 AUTO_PIN_SPEAKER_OUT);
5911 /* set up input pins and loopback paths */
5912 static void init_analog_input(struct hda_codec *codec)
5914 struct hda_gen_spec *spec = codec->spec;
5915 struct auto_pin_cfg *cfg = &spec->autocfg;
5918 for (i = 0; i < cfg->num_inputs; i++) {
5919 hda_nid_t nid = cfg->inputs[i].pin;
5920 if (is_input_pin(codec, nid))
5921 restore_pin_ctl(codec, nid);
5923 /* init loopback inputs */
5924 if (spec->mixer_nid) {
5925 resume_path_from_idx(codec, spec->loopback_paths[i]);
5926 resume_path_from_idx(codec, spec->loopback_merge_path);
5931 /* initialize ADC paths */
5932 static void init_input_src(struct hda_codec *codec)
5934 struct hda_gen_spec *spec = codec->spec;
5935 struct hda_input_mux *imux = &spec->input_mux;
5936 struct nid_path *path;
5939 if (spec->dyn_adc_switch)
5942 nums = spec->num_adc_nids;
5944 for (c = 0; c < nums; c++) {
5945 for (i = 0; i < imux->num_items; i++) {
5946 path = get_input_path(codec, c, i);
5948 bool active = path->active;
5949 if (i == spec->cur_mux[c])
5951 snd_hda_activate_path(codec, path, active, false);
5955 update_hp_mic(codec, c, true);
5958 if (spec->cap_sync_hook)
5959 spec->cap_sync_hook(codec, NULL, NULL);
5962 /* set right pin controls for digital I/O */
5963 static void init_digital(struct hda_codec *codec)
5965 struct hda_gen_spec *spec = codec->spec;
5969 for (i = 0; i < spec->autocfg.dig_outs; i++)
5970 set_output_and_unmute(codec, spec->digout_paths[i]);
5971 pin = spec->autocfg.dig_in_pin;
5973 restore_pin_ctl(codec, pin);
5974 resume_path_from_idx(codec, spec->digin_path);
5978 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5979 * invalid unsol tags by some reason
5981 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5983 const struct hda_pincfg *pin;
5986 snd_array_for_each(&codec->init_pins, i, pin) {
5987 hda_nid_t nid = pin->nid;
5988 if (is_jack_detectable(codec, nid) &&
5989 !snd_hda_jack_tbl_get(codec, nid))
5990 snd_hda_codec_write_cache(codec, nid, 0,
5991 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5996 * snd_hda_gen_init - initialize the generic spec
5997 * @codec: the HDA codec
5999 * This can be put as patch_ops init function.
6001 int snd_hda_gen_init(struct hda_codec *codec)
6003 struct hda_gen_spec *spec = codec->spec;
6005 if (spec->init_hook)
6006 spec->init_hook(codec);
6008 if (!spec->skip_verbs)
6009 snd_hda_apply_verbs(codec);
6011 init_multi_out(codec);
6012 init_extra_out(codec);
6013 init_multi_io(codec);
6014 init_aamix_paths(codec);
6015 init_analog_input(codec);
6016 init_input_src(codec);
6017 init_digital(codec);
6019 clear_unsol_on_unused_pins(codec);
6021 sync_all_pin_power_ctls(codec);
6023 /* call init functions of standard auto-mute helpers */
6024 update_automute_all(codec);
6026 regcache_sync(codec->core.regmap);
6028 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
6029 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
6031 hda_call_check_power_status(codec, 0x01);
6034 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
6037 * snd_hda_gen_free - free the generic spec
6038 * @codec: the HDA codec
6040 * This can be put as patch_ops free function.
6042 void snd_hda_gen_free(struct hda_codec *codec)
6044 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
6045 snd_hda_gen_spec_free(codec->spec);
6049 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
6052 * snd_hda_gen_reboot_notify - Make codec enter D3 before rebooting
6053 * @codec: the HDA codec
6055 * This can be put as patch_ops reboot_notify function.
6057 void snd_hda_gen_reboot_notify(struct hda_codec *codec)
6059 /* Make the codec enter D3 to avoid spurious noises from the internal
6060 * speaker during (and after) reboot
6062 snd_hda_codec_set_power_to_all(codec, codec->core.afg, AC_PWRST_D3);
6063 snd_hda_codec_write(codec, codec->core.afg, 0,
6064 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6067 EXPORT_SYMBOL_GPL(snd_hda_gen_reboot_notify);
6071 * snd_hda_gen_check_power_status - check the loopback power save state
6072 * @codec: the HDA codec
6073 * @nid: NID to inspect
6075 * This can be put as patch_ops check_power_status function.
6077 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
6079 struct hda_gen_spec *spec = codec->spec;
6080 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
6082 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
6087 * the generic codec support
6090 static const struct hda_codec_ops generic_patch_ops = {
6091 .build_controls = snd_hda_gen_build_controls,
6092 .build_pcms = snd_hda_gen_build_pcms,
6093 .init = snd_hda_gen_init,
6094 .free = snd_hda_gen_free,
6095 .unsol_event = snd_hda_jack_unsol_event,
6096 .reboot_notify = snd_hda_gen_reboot_notify,
6098 .check_power_status = snd_hda_gen_check_power_status,
6103 * snd_hda_parse_generic_codec - Generic codec parser
6104 * @codec: the HDA codec
6106 static int snd_hda_parse_generic_codec(struct hda_codec *codec)
6108 struct hda_gen_spec *spec;
6111 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
6114 snd_hda_gen_spec_init(spec);
6117 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
6121 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
6125 codec->patch_ops = generic_patch_ops;
6129 snd_hda_gen_free(codec);
6133 static const struct hda_device_id snd_hda_id_generic[] = {
6134 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
6137 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
6139 static struct hda_codec_driver generic_driver = {
6140 .id = snd_hda_id_generic,
6143 module_hda_codec_driver(generic_driver);
6145 MODULE_LICENSE("GPL");
6146 MODULE_DESCRIPTION("Generic HD-audio codec parser");