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;
144 val = snd_hda_get_bool_hint(codec, "auto_mute");
146 spec->suppress_auto_mute = !val;
147 val = snd_hda_get_bool_hint(codec, "auto_mic");
149 spec->suppress_auto_mic = !val;
150 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
152 spec->line_in_auto_switch = !!val;
153 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
155 spec->auto_mute_via_amp = !!val;
156 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
158 spec->need_dac_fix = !!val;
159 val = snd_hda_get_bool_hint(codec, "primary_hp");
161 spec->no_primary_hp = !val;
162 val = snd_hda_get_bool_hint(codec, "multi_io");
164 spec->no_multi_io = !val;
165 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
167 spec->multi_cap_vol = !!val;
168 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
170 spec->inv_dmic_split = !!val;
171 val = snd_hda_get_bool_hint(codec, "indep_hp");
173 spec->indep_hp = !!val;
174 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
176 spec->add_stereo_mix_input = !!val;
177 /* the following two are just for compatibility */
178 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
180 spec->add_jack_modes = !!val;
181 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
183 spec->add_jack_modes = !!val;
184 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
186 spec->add_jack_modes = !!val;
187 val = snd_hda_get_bool_hint(codec, "power_down_unused");
189 spec->power_down_unused = !!val;
190 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
192 spec->hp_mic = !!val;
193 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
195 spec->suppress_hp_mic_detect = !val;
197 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
198 spec->mixer_nid = val;
202 * pin control value accesses
205 #define update_pin_ctl(codec, pin, val) \
206 snd_hda_codec_update_cache(codec, pin, 0, \
207 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
209 /* restore the pinctl based on the cached value */
210 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
212 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
215 /* set the pinctl target value and write it if requested */
216 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
217 unsigned int val, bool do_write)
221 val = snd_hda_correct_pin_ctl(codec, pin, val);
222 snd_hda_codec_set_pin_target(codec, pin, val);
224 update_pin_ctl(codec, pin, val);
227 /* set pinctl target values for all given pins */
228 static void set_pin_targets(struct hda_codec *codec, int num_pins,
229 hda_nid_t *pins, unsigned int val)
232 for (i = 0; i < num_pins; i++)
233 set_pin_target(codec, pins[i], val, false);
240 /* return the position of NID in the list, or -1 if not found */
241 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
244 for (i = 0; i < nums; i++)
250 /* return true if the given NID is contained in the path */
251 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
253 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
256 static struct nid_path *get_nid_path(struct hda_codec *codec,
257 hda_nid_t from_nid, hda_nid_t to_nid,
260 struct hda_gen_spec *spec = codec->spec;
263 for (i = 0; i < spec->paths.used; i++) {
264 struct nid_path *path = snd_array_elem(&spec->paths, i);
265 if (path->depth <= 0)
267 if ((!from_nid || path->path[0] == from_nid) &&
268 (!to_nid || path->path[path->depth - 1] == to_nid)) {
270 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
271 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
279 * snd_hda_get_nid_path - get the path between the given NIDs
280 * @codec: the HDA codec
281 * @from_nid: the NID where the path start from
282 * @to_nid: the NID where the path ends at
284 * Return the found nid_path object or NULL for error.
285 * Passing 0 to either @from_nid or @to_nid behaves as a wildcard.
287 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
288 hda_nid_t from_nid, hda_nid_t to_nid)
290 return get_nid_path(codec, from_nid, to_nid, 0);
292 EXPORT_SYMBOL_GPL(snd_hda_get_nid_path);
295 * snd_hda_get_path_idx - get the index number corresponding to the path
297 * @codec: the HDA codec
298 * @path: nid_path object
300 * The returned index starts from 1, i.e. the actual array index with offset 1,
301 * and zero is handled as an invalid path
303 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
305 struct hda_gen_spec *spec = codec->spec;
306 struct nid_path *array = spec->paths.list;
309 if (!spec->paths.used)
312 if (idx < 0 || idx >= spec->paths.used)
316 EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
319 * snd_hda_get_path_from_idx - get the path instance corresponding to the
321 * @codec: the HDA codec
322 * @idx: the path index
324 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
326 struct hda_gen_spec *spec = codec->spec;
328 if (idx <= 0 || idx > spec->paths.used)
330 return snd_array_elem(&spec->paths, idx - 1);
332 EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
334 /* check whether the given DAC is already found in any existing paths */
335 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
337 struct hda_gen_spec *spec = codec->spec;
340 for (i = 0; i < spec->paths.used; i++) {
341 struct nid_path *path = snd_array_elem(&spec->paths, i);
342 if (path->path[0] == nid)
348 /* check whether the given two widgets can be connected */
349 static bool is_reachable_path(struct hda_codec *codec,
350 hda_nid_t from_nid, hda_nid_t to_nid)
352 if (!from_nid || !to_nid)
354 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
357 /* nid, dir and idx */
358 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
360 /* check whether the given ctl is already assigned in any path elements */
361 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
363 struct hda_gen_spec *spec = codec->spec;
366 val &= AMP_VAL_COMPARE_MASK;
367 for (i = 0; i < spec->paths.used; i++) {
368 struct nid_path *path = snd_array_elem(&spec->paths, i);
369 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
375 /* check whether a control with the given (nid, dir, idx) was assigned */
376 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
377 int dir, int idx, int type)
379 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
380 return is_ctl_used(codec, val, type);
383 static void print_nid_path(struct hda_codec *codec,
384 const char *pfx, struct nid_path *path)
391 for (i = 0; i < path->depth; i++)
392 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
393 pos != buf ? ":" : "",
396 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
399 /* called recursively */
400 static bool __parse_nid_path(struct hda_codec *codec,
401 hda_nid_t from_nid, hda_nid_t to_nid,
402 int anchor_nid, struct nid_path *path,
405 const hda_nid_t *conn;
408 if (to_nid == anchor_nid)
409 anchor_nid = 0; /* anchor passed */
410 else if (to_nid == (hda_nid_t)(-anchor_nid))
411 return false; /* hit the exclusive nid */
413 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
414 for (i = 0; i < nums; i++) {
415 if (conn[i] != from_nid) {
416 /* special case: when from_nid is 0,
417 * try to find an empty DAC
420 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
421 is_dac_already_used(codec, conn[i]))
424 /* anchor is not requested or already passed? */
428 if (depth >= MAX_NID_PATH_DEPTH)
430 for (i = 0; i < nums; i++) {
432 type = get_wcaps_type(get_wcaps(codec, conn[i]));
433 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
436 if (__parse_nid_path(codec, from_nid, conn[i],
437 anchor_nid, path, depth + 1))
443 path->path[path->depth] = conn[i];
444 path->idx[path->depth + 1] = i;
445 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
446 path->multi[path->depth + 1] = 1;
452 * snd_hda_parse_nid_path - parse the widget path from the given nid to
454 * @codec: the HDA codec
455 * @from_nid: the NID where the path start from
456 * @to_nid: the NID where the path ends at
457 * @anchor_nid: the anchor indication
458 * @path: the path object to store the result
460 * Returns true if a matching path is found.
462 * The parsing behavior depends on parameters:
463 * when @from_nid is 0, try to find an empty DAC;
464 * when @anchor_nid is set to a positive value, only paths through the widget
465 * with the given value are evaluated.
466 * when @anchor_nid is set to a negative value, paths through the widget
467 * with the negative of given value are excluded, only other paths are chosen.
468 * when @anchor_nid is zero, no special handling about path selection.
470 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
471 hda_nid_t to_nid, int anchor_nid,
472 struct nid_path *path)
474 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
475 path->path[path->depth] = to_nid;
481 EXPORT_SYMBOL_GPL(snd_hda_parse_nid_path);
484 * snd_hda_add_new_path - parse the path between the given NIDs and
485 * add to the path list
486 * @codec: the HDA codec
487 * @from_nid: the NID where the path start from
488 * @to_nid: the NID where the path ends at
489 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
491 * If no valid path is found, returns NULL.
494 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
495 hda_nid_t to_nid, int anchor_nid)
497 struct hda_gen_spec *spec = codec->spec;
498 struct nid_path *path;
500 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
503 /* check whether the path has been already added */
504 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
508 path = snd_array_new(&spec->paths);
511 memset(path, 0, sizeof(*path));
512 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
518 EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
520 /* clear the given path as invalid so that it won't be picked up later */
521 static void invalidate_nid_path(struct hda_codec *codec, int idx)
523 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
526 memset(path, 0, sizeof(*path));
529 /* return a DAC if paired to the given pin by codec driver */
530 static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
532 struct hda_gen_spec *spec = codec->spec;
533 const hda_nid_t *list = spec->preferred_dacs;
537 for (; *list; list += 2)
543 /* look for an empty DAC slot */
544 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
547 struct hda_gen_spec *spec = codec->spec;
551 for (i = 0; i < spec->num_all_dacs; i++) {
552 hda_nid_t nid = spec->all_dacs[i];
553 if (!nid || is_dac_already_used(codec, nid))
555 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
556 if (is_digital != cap_digital)
558 if (is_reachable_path(codec, nid, pin))
564 /* replace the channels in the composed amp value with the given number */
565 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
567 val &= ~(0x3U << 16);
572 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
573 hda_nid_t nid2, int dir)
575 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
576 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
577 return (query_amp_caps(codec, nid1, dir) ==
578 query_amp_caps(codec, nid2, dir));
581 /* look for a widget suitable for assigning a mute switch in the path */
582 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
583 struct nid_path *path)
587 for (i = path->depth - 1; i >= 0; i--) {
588 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
589 return path->path[i];
590 if (i != path->depth - 1 && i != 0 &&
591 nid_has_mute(codec, path->path[i], HDA_INPUT))
592 return path->path[i];
597 /* look for a widget suitable for assigning a volume ctl in the path */
598 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
599 struct nid_path *path)
601 struct hda_gen_spec *spec = codec->spec;
604 for (i = path->depth - 1; i >= 0; i--) {
605 hda_nid_t nid = path->path[i];
606 if ((spec->out_vol_mask >> nid) & 1)
608 if (nid_has_volume(codec, nid, HDA_OUTPUT))
615 * path activation / deactivation
618 /* can have the amp-in capability? */
619 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
621 hda_nid_t nid = path->path[idx];
622 unsigned int caps = get_wcaps(codec, nid);
623 unsigned int type = get_wcaps_type(caps);
625 if (!(caps & AC_WCAP_IN_AMP))
627 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
632 /* can have the amp-out capability? */
633 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
635 hda_nid_t nid = path->path[idx];
636 unsigned int caps = get_wcaps(codec, nid);
637 unsigned int type = get_wcaps_type(caps);
639 if (!(caps & AC_WCAP_OUT_AMP))
641 if (type == AC_WID_PIN && !idx) /* only for output pins */
646 /* check whether the given (nid,dir,idx) is active */
647 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
648 unsigned int dir, unsigned int idx)
650 struct hda_gen_spec *spec = codec->spec;
653 for (n = 0; n < spec->paths.used; n++) {
654 struct nid_path *path = snd_array_elem(&spec->paths, n);
657 for (i = 0; i < path->depth; i++) {
658 if (path->path[i] == nid) {
659 if (dir == HDA_OUTPUT || path->idx[i] == idx)
668 /* check whether the NID is referred by any active paths */
669 #define is_active_nid_for_any(codec, nid) \
670 is_active_nid(codec, nid, HDA_OUTPUT, 0)
672 /* get the default amp value for the target state */
673 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
674 int dir, unsigned int caps, bool enable)
676 unsigned int val = 0;
678 if (caps & AC_AMPCAP_NUM_STEPS) {
681 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
683 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
690 /* is this a stereo widget or a stereo-to-mono mix? */
691 static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
693 unsigned int wcaps = get_wcaps(codec, nid);
696 if (wcaps & AC_WCAP_STEREO)
698 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
700 if (snd_hda_get_num_conns(codec, nid) != 1)
702 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
704 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
707 /* initialize the amp value (only at the first time) */
708 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
710 unsigned int caps = query_amp_caps(codec, nid, dir);
711 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
713 if (is_stereo_amps(codec, nid, dir))
714 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
716 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
719 /* update the amp, doing in stereo or mono depending on NID */
720 static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
721 unsigned int mask, unsigned int val)
723 if (is_stereo_amps(codec, nid, dir))
724 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
727 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
731 /* calculate amp value mask we can modify;
732 * if the given amp is controlled by mixers, don't touch it
734 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
735 hda_nid_t nid, int dir, int idx,
738 unsigned int mask = 0xff;
740 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
741 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
744 if (caps & AC_AMPCAP_NUM_STEPS) {
745 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
746 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
752 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
753 int idx, int idx_to_check, bool enable)
756 unsigned int mask, val;
758 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
761 caps = query_amp_caps(codec, nid, dir);
762 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
763 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
768 update_amp(codec, nid, dir, idx, mask, val);
771 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
774 hda_nid_t nid = path->path[i];
775 init_amp(codec, nid, HDA_OUTPUT, 0);
776 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
779 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
780 int i, bool enable, bool add_aamix)
782 struct hda_gen_spec *spec = codec->spec;
783 const hda_nid_t *conn;
786 hda_nid_t nid = path->path[i];
788 nums = snd_hda_get_conn_list(codec, nid, &conn);
789 type = get_wcaps_type(get_wcaps(codec, nid));
790 if (type == AC_WID_PIN ||
791 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
797 for (n = 0; n < nums; n++)
798 init_amp(codec, nid, HDA_INPUT, n);
800 /* here is a little bit tricky in comparison with activate_amp_out();
801 * when aa-mixer is available, we need to enable the path as well
803 for (n = 0; n < nums; n++) {
804 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
806 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
811 * snd_hda_activate_path - activate or deactivate the given path
812 * @codec: the HDA codec
813 * @path: the path to activate/deactivate
814 * @enable: flag to activate or not
815 * @add_aamix: enable the input from aamix NID
817 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
819 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
820 bool enable, bool add_aamix)
822 struct hda_gen_spec *spec = codec->spec;
826 path->active = false;
828 for (i = path->depth - 1; i >= 0; i--) {
829 hda_nid_t nid = path->path[i];
830 if (enable && spec->power_down_unused) {
831 /* make sure the widget is powered up */
832 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
833 snd_hda_codec_write(codec, nid, 0,
834 AC_VERB_SET_POWER_STATE,
837 if (enable && path->multi[i])
838 snd_hda_codec_update_cache(codec, nid, 0,
839 AC_VERB_SET_CONNECT_SEL,
841 if (has_amp_in(codec, path, i))
842 activate_amp_in(codec, path, i, enable, add_aamix);
843 if (has_amp_out(codec, path, i))
844 activate_amp_out(codec, path, i, enable);
850 EXPORT_SYMBOL_GPL(snd_hda_activate_path);
852 /* if the given path is inactive, put widgets into D3 (only if suitable) */
853 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
855 struct hda_gen_spec *spec = codec->spec;
856 bool changed = false;
859 if (!spec->power_down_unused || path->active)
862 for (i = 0; i < path->depth; i++) {
863 hda_nid_t nid = path->path[i];
864 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
865 !is_active_nid_for_any(codec, nid)) {
866 snd_hda_codec_write(codec, nid, 0,
867 AC_VERB_SET_POWER_STATE,
875 snd_hda_codec_read(codec, path->path[0], 0,
876 AC_VERB_GET_POWER_STATE, 0);
880 /* turn on/off EAPD on the given pin */
881 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
883 struct hda_gen_spec *spec = codec->spec;
884 if (spec->own_eapd_ctl ||
885 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
887 if (spec->keep_eapd_on && !enable)
891 snd_hda_codec_update_cache(codec, pin, 0,
892 AC_VERB_SET_EAPD_BTLENABLE,
893 enable ? 0x02 : 0x00);
896 /* re-initialize the path specified by the given path index */
897 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
899 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
901 snd_hda_activate_path(codec, path, path->active, false);
906 * Helper functions for creating mixer ctl elements
909 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
910 struct snd_ctl_elem_value *ucontrol);
911 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
912 struct snd_ctl_elem_value *ucontrol);
919 static const struct snd_kcontrol_new control_templates[] = {
920 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
921 /* only the put callback is replaced for handling the special mute */
923 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
924 .subdevice = HDA_SUBDEV_AMP_FLAG,
925 .info = snd_hda_mixer_amp_switch_info,
926 .get = snd_hda_mixer_amp_switch_get,
927 .put = hda_gen_mixer_mute_put, /* replaced */
928 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
931 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
932 .info = snd_hda_mixer_amp_switch_info,
933 .get = snd_hda_mixer_bind_switch_get,
934 .put = hda_gen_bind_mute_put, /* replaced */
935 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
939 /* add dynamic controls from template */
940 static struct snd_kcontrol_new *
941 add_control(struct hda_gen_spec *spec, int type, const char *name,
942 int cidx, unsigned long val)
944 struct snd_kcontrol_new *knew;
946 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
950 if (get_amp_nid_(val))
951 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
952 knew->private_value = val;
956 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
957 const char *pfx, const char *dir,
958 const char *sfx, int cidx, unsigned long val)
960 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
961 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
962 if (!add_control(spec, type, name, cidx, val))
967 #define add_pb_vol_ctrl(spec, type, pfx, val) \
968 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
969 #define add_pb_sw_ctrl(spec, type, pfx, val) \
970 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
971 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
972 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
973 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
974 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
976 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
977 unsigned int chs, struct nid_path *path)
982 val = path->ctls[NID_PATH_VOL_CTL];
985 val = amp_val_replace_channels(val, chs);
986 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
989 /* return the channel bits suitable for the given path->ctls[] */
990 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
993 int chs = 1; /* mono (left only) */
995 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
996 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
997 chs = 3; /* stereo */
1002 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1003 struct nid_path *path)
1005 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1006 return add_vol_ctl(codec, pfx, cidx, chs, path);
1009 /* create a mute-switch for the given mixer widget;
1010 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1012 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1013 unsigned int chs, struct nid_path *path)
1016 int type = HDA_CTL_WIDGET_MUTE;
1020 val = path->ctls[NID_PATH_MUTE_CTL];
1023 val = amp_val_replace_channels(val, chs);
1024 if (get_amp_direction_(val) == HDA_INPUT) {
1025 hda_nid_t nid = get_amp_nid_(val);
1026 int nums = snd_hda_get_num_conns(codec, nid);
1028 type = HDA_CTL_BIND_MUTE;
1032 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1035 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1036 int cidx, struct nid_path *path)
1038 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1039 return add_sw_ctl(codec, pfx, cidx, chs, path);
1042 /* playback mute control with the software mute bit check */
1043 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1044 struct snd_ctl_elem_value *ucontrol)
1046 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1047 struct hda_gen_spec *spec = codec->spec;
1049 if (spec->auto_mute_via_amp) {
1050 hda_nid_t nid = get_amp_nid(kcontrol);
1051 bool enabled = !((spec->mute_bits >> nid) & 1);
1052 ucontrol->value.integer.value[0] &= enabled;
1053 ucontrol->value.integer.value[1] &= enabled;
1057 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1058 struct snd_ctl_elem_value *ucontrol)
1060 sync_auto_mute_bits(kcontrol, ucontrol);
1061 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1064 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1065 struct snd_ctl_elem_value *ucontrol)
1067 sync_auto_mute_bits(kcontrol, ucontrol);
1068 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1071 /* any ctl assigned to the path with the given index? */
1072 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1074 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1075 return path && path->ctls[ctl_type];
1078 static const char * const channel_name[4] = {
1079 "Front", "Surround", "CLFE", "Side"
1082 /* give some appropriate ctl name prefix for the given line out channel */
1083 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1084 int *index, int ctl_type)
1086 struct hda_gen_spec *spec = codec->spec;
1087 struct auto_pin_cfg *cfg = &spec->autocfg;
1090 if (cfg->line_outs == 1 && !spec->multi_ios &&
1091 !cfg->hp_outs && !cfg->speaker_outs)
1092 return spec->vmaster_mute.hook ? "PCM" : "Master";
1094 /* if there is really a single DAC used in the whole output paths,
1095 * use it master (or "PCM" if a vmaster hook is present)
1097 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1098 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1099 return spec->vmaster_mute.hook ? "PCM" : "Master";
1101 /* multi-io channels */
1102 if (ch >= cfg->line_outs)
1103 return channel_name[ch];
1105 switch (cfg->line_out_type) {
1106 case AUTO_PIN_SPEAKER_OUT:
1107 /* if the primary channel vol/mute is shared with HP volume,
1108 * don't name it as Speaker
1110 if (!ch && cfg->hp_outs &&
1111 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1113 if (cfg->line_outs == 1)
1115 if (cfg->line_outs == 2)
1116 return ch ? "Bass Speaker" : "Speaker";
1118 case AUTO_PIN_HP_OUT:
1119 /* if the primary channel vol/mute is shared with spk volume,
1120 * don't name it as Headphone
1122 if (!ch && cfg->speaker_outs &&
1123 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1125 /* for multi-io case, only the primary out */
1126 if (ch && spec->multi_ios)
1130 case AUTO_PIN_LINE_OUT:
1131 /* This deals with the case where we have two DACs and
1132 * one LO, one HP and one Speaker */
1133 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1134 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1135 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1136 if (hp_lo_shared && spk_lo_shared)
1137 return spec->vmaster_mute.hook ? "PCM" : "Master";
1139 return "Headphone+LO";
1141 return "Speaker+LO";
1145 /* for a single channel output, we don't have to name the channel */
1146 if (cfg->line_outs == 1 && !spec->multi_ios)
1149 if (ch >= ARRAY_SIZE(channel_name)) {
1154 return channel_name[ch];
1158 * Parse output paths
1161 /* badness definition */
1163 /* No primary DAC is found for the main output */
1164 BAD_NO_PRIMARY_DAC = 0x10000,
1165 /* No DAC is found for the extra output */
1166 BAD_NO_DAC = 0x4000,
1167 /* No possible multi-ios */
1168 BAD_MULTI_IO = 0x120,
1169 /* No individual DAC for extra output */
1170 BAD_NO_EXTRA_DAC = 0x102,
1171 /* No individual DAC for extra surrounds */
1172 BAD_NO_EXTRA_SURR_DAC = 0x101,
1173 /* Primary DAC shared with main surrounds */
1174 BAD_SHARED_SURROUND = 0x100,
1175 /* No independent HP possible */
1176 BAD_NO_INDEP_HP = 0x10,
1177 /* Primary DAC shared with main CLFE */
1178 BAD_SHARED_CLFE = 0x10,
1179 /* Primary DAC shared with extra surrounds */
1180 BAD_SHARED_EXTRA_SURROUND = 0x10,
1181 /* Volume widget is shared */
1182 BAD_SHARED_VOL = 0x10,
1185 /* look for widgets in the given path which are appropriate for
1186 * volume and mute controls, and assign the values to ctls[].
1188 * When no appropriate widget is found in the path, the badness value
1189 * is incremented depending on the situation. The function returns the
1190 * total badness for both volume and mute controls.
1192 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1194 struct hda_gen_spec *spec = codec->spec;
1200 return BAD_SHARED_VOL * 2;
1202 if (path->ctls[NID_PATH_VOL_CTL] ||
1203 path->ctls[NID_PATH_MUTE_CTL])
1204 return 0; /* already evaluated */
1206 nid = look_for_out_vol_nid(codec, path);
1208 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1209 if (spec->dac_min_mute)
1210 val |= HDA_AMP_VAL_MIN_MUTE;
1211 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1212 badness += BAD_SHARED_VOL;
1214 path->ctls[NID_PATH_VOL_CTL] = val;
1216 badness += BAD_SHARED_VOL;
1217 nid = look_for_out_mute_nid(codec, path);
1219 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1220 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1221 nid_has_mute(codec, nid, HDA_OUTPUT))
1222 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1224 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1225 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1226 badness += BAD_SHARED_VOL;
1228 path->ctls[NID_PATH_MUTE_CTL] = val;
1230 badness += BAD_SHARED_VOL;
1234 const struct badness_table hda_main_out_badness = {
1235 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1236 .no_dac = BAD_NO_DAC,
1237 .shared_primary = BAD_NO_PRIMARY_DAC,
1238 .shared_surr = BAD_SHARED_SURROUND,
1239 .shared_clfe = BAD_SHARED_CLFE,
1240 .shared_surr_main = BAD_SHARED_SURROUND,
1242 EXPORT_SYMBOL_GPL(hda_main_out_badness);
1244 const struct badness_table hda_extra_out_badness = {
1245 .no_primary_dac = BAD_NO_DAC,
1246 .no_dac = BAD_NO_DAC,
1247 .shared_primary = BAD_NO_EXTRA_DAC,
1248 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1249 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1250 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1252 EXPORT_SYMBOL_GPL(hda_extra_out_badness);
1254 /* get the DAC of the primary output corresponding to the given array index */
1255 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1257 struct hda_gen_spec *spec = codec->spec;
1258 struct auto_pin_cfg *cfg = &spec->autocfg;
1260 if (cfg->line_outs > idx)
1261 return spec->private_dac_nids[idx];
1262 idx -= cfg->line_outs;
1263 if (spec->multi_ios > idx)
1264 return spec->multi_io[idx].dac;
1268 /* return the DAC if it's reachable, otherwise zero */
1269 static inline hda_nid_t try_dac(struct hda_codec *codec,
1270 hda_nid_t dac, hda_nid_t pin)
1272 return is_reachable_path(codec, dac, pin) ? dac : 0;
1275 /* try to assign DACs to pins and return the resultant badness */
1276 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1277 const hda_nid_t *pins, hda_nid_t *dacs,
1279 const struct badness_table *bad)
1281 struct hda_gen_spec *spec = codec->spec;
1289 for (i = 0; i < num_outs; i++) {
1290 struct nid_path *path;
1291 hda_nid_t pin = pins[i];
1293 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1295 badness += assign_out_path_ctls(codec, path);
1299 dacs[i] = get_preferred_dac(codec, pin);
1301 if (is_dac_already_used(codec, dacs[i]))
1302 badness += bad->shared_primary;
1306 dacs[i] = look_for_dac(codec, pin, false);
1307 if (!dacs[i] && !i) {
1308 /* try to steal the DAC of surrounds for the front */
1309 for (j = 1; j < num_outs; j++) {
1310 if (is_reachable_path(codec, dacs[j], pin)) {
1313 invalidate_nid_path(codec, path_idx[j]);
1322 dac = try_dac(codec, get_primary_out(codec, i), pin);
1324 dac = try_dac(codec, dacs[0], pin);
1326 dac = try_dac(codec, get_primary_out(codec, i), pin);
1329 badness += bad->shared_primary;
1331 badness += bad->shared_surr;
1333 badness += bad->shared_clfe;
1334 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1335 dac = spec->private_dac_nids[0];
1336 badness += bad->shared_surr_main;
1338 badness += bad->no_primary_dac;
1340 badness += bad->no_dac;
1344 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1345 if (!path && !i && spec->mixer_nid) {
1346 /* try with aamix */
1347 path = snd_hda_add_new_path(codec, dac, pin, 0);
1351 badness += bad->no_dac;
1353 /* print_nid_path(codec, "output", path); */
1354 path->active = true;
1355 path_idx[i] = snd_hda_get_path_idx(codec, path);
1356 badness += assign_out_path_ctls(codec, path);
1363 /* return NID if the given pin has only a single connection to a certain DAC */
1364 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1366 struct hda_gen_spec *spec = codec->spec;
1368 hda_nid_t nid_found = 0;
1370 for (i = 0; i < spec->num_all_dacs; i++) {
1371 hda_nid_t nid = spec->all_dacs[i];
1372 if (!nid || is_dac_already_used(codec, nid))
1374 if (is_reachable_path(codec, nid, pin)) {
1383 /* check whether the given pin can be a multi-io pin */
1384 static bool can_be_multiio_pin(struct hda_codec *codec,
1385 unsigned int location, hda_nid_t nid)
1387 unsigned int defcfg, caps;
1389 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1390 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1392 if (location && get_defcfg_location(defcfg) != location)
1394 caps = snd_hda_query_pin_caps(codec, nid);
1395 if (!(caps & AC_PINCAP_OUT))
1400 /* count the number of input pins that are capable to be multi-io */
1401 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1403 struct hda_gen_spec *spec = codec->spec;
1404 struct auto_pin_cfg *cfg = &spec->autocfg;
1405 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1406 unsigned int location = get_defcfg_location(defcfg);
1410 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1411 for (i = 0; i < cfg->num_inputs; i++) {
1412 if (cfg->inputs[i].type != type)
1414 if (can_be_multiio_pin(codec, location,
1415 cfg->inputs[i].pin))
1425 * When hardwired is set, try to fill ony hardwired pins, and returns
1426 * zero if any pins are filled, non-zero if nothing found.
1427 * When hardwired is off, try to fill possible input pins, and returns
1428 * the badness value.
1430 static int fill_multi_ios(struct hda_codec *codec,
1431 hda_nid_t reference_pin,
1434 struct hda_gen_spec *spec = codec->spec;
1435 struct auto_pin_cfg *cfg = &spec->autocfg;
1436 int type, i, j, num_pins, old_pins;
1437 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1438 unsigned int location = get_defcfg_location(defcfg);
1440 struct nid_path *path;
1442 old_pins = spec->multi_ios;
1446 num_pins = count_multiio_pins(codec, reference_pin);
1450 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1451 for (i = 0; i < cfg->num_inputs; i++) {
1452 hda_nid_t nid = cfg->inputs[i].pin;
1455 if (cfg->inputs[i].type != type)
1457 if (!can_be_multiio_pin(codec, location, nid))
1459 for (j = 0; j < spec->multi_ios; j++) {
1460 if (nid == spec->multi_io[j].pin)
1463 if (j < spec->multi_ios)
1467 dac = get_dac_if_single(codec, nid);
1469 dac = look_for_dac(codec, nid, false);
1474 path = snd_hda_add_new_path(codec, dac, nid,
1480 /* print_nid_path(codec, "multiio", path); */
1481 spec->multi_io[spec->multi_ios].pin = nid;
1482 spec->multi_io[spec->multi_ios].dac = dac;
1483 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1484 snd_hda_get_path_idx(codec, path);
1486 if (spec->multi_ios >= 2)
1492 badness = BAD_MULTI_IO;
1493 if (old_pins == spec->multi_ios) {
1495 return 1; /* nothing found */
1497 return badness; /* no badness if nothing found */
1499 if (!hardwired && spec->multi_ios < 2) {
1500 /* cancel newly assigned paths */
1501 spec->paths.used -= spec->multi_ios - old_pins;
1502 spec->multi_ios = old_pins;
1506 /* assign volume and mute controls */
1507 for (i = old_pins; i < spec->multi_ios; i++) {
1508 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1509 badness += assign_out_path_ctls(codec, path);
1515 /* map DACs for all pins in the list if they are single connections */
1516 static bool map_singles(struct hda_codec *codec, int outs,
1517 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1519 struct hda_gen_spec *spec = codec->spec;
1522 for (i = 0; i < outs; i++) {
1523 struct nid_path *path;
1527 dac = get_dac_if_single(codec, pins[i]);
1530 path = snd_hda_add_new_path(codec, dac, pins[i],
1532 if (!path && !i && spec->mixer_nid)
1533 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1537 /* print_nid_path(codec, "output", path); */
1538 path->active = true;
1539 path_idx[i] = snd_hda_get_path_idx(codec, path);
1545 /* create a new path including aamix if available, and return its index */
1546 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1548 struct hda_gen_spec *spec = codec->spec;
1549 struct nid_path *path;
1550 hda_nid_t path_dac, dac, pin;
1552 path = snd_hda_get_path_from_idx(codec, path_idx);
1553 if (!path || !path->depth ||
1554 is_nid_contained(path, spec->mixer_nid))
1556 path_dac = path->path[0];
1557 dac = spec->private_dac_nids[0];
1558 pin = path->path[path->depth - 1];
1559 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1561 if (dac != path_dac)
1563 else if (spec->multiout.hp_out_nid[0])
1564 dac = spec->multiout.hp_out_nid[0];
1565 else if (spec->multiout.extra_out_nid[0])
1566 dac = spec->multiout.extra_out_nid[0];
1570 path = snd_hda_add_new_path(codec, dac, pin,
1575 /* print_nid_path(codec, "output-aamix", path); */
1576 path->active = false; /* unused as default */
1577 return snd_hda_get_path_idx(codec, path);
1580 /* check whether the independent HP is available with the current config */
1581 static bool indep_hp_possible(struct hda_codec *codec)
1583 struct hda_gen_spec *spec = codec->spec;
1584 struct auto_pin_cfg *cfg = &spec->autocfg;
1585 struct nid_path *path;
1588 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1589 idx = spec->out_paths[0];
1591 idx = spec->hp_paths[0];
1592 path = snd_hda_get_path_from_idx(codec, idx);
1596 /* assume no path conflicts unless aamix is involved */
1597 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1600 /* check whether output paths contain aamix */
1601 for (i = 0; i < cfg->line_outs; i++) {
1602 if (spec->out_paths[i] == idx)
1604 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1605 if (path && is_nid_contained(path, spec->mixer_nid))
1608 for (i = 0; i < cfg->speaker_outs; i++) {
1609 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1610 if (path && is_nid_contained(path, spec->mixer_nid))
1617 /* fill the empty entries in the dac array for speaker/hp with the
1618 * shared dac pointed by the paths
1620 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1621 hda_nid_t *dacs, int *path_idx)
1623 struct nid_path *path;
1626 for (i = 0; i < num_outs; i++) {
1629 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1632 dacs[i] = path->path[0];
1636 /* fill in the dac_nids table from the parsed pin configuration */
1637 static int fill_and_eval_dacs(struct hda_codec *codec,
1638 bool fill_hardwired,
1639 bool fill_mio_first)
1641 struct hda_gen_spec *spec = codec->spec;
1642 struct auto_pin_cfg *cfg = &spec->autocfg;
1643 int i, err, badness;
1645 /* set num_dacs once to full for look_for_dac() */
1646 spec->multiout.num_dacs = cfg->line_outs;
1647 spec->multiout.dac_nids = spec->private_dac_nids;
1648 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1649 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1650 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1651 spec->multi_ios = 0;
1652 snd_array_free(&spec->paths);
1654 /* clear path indices */
1655 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1656 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1657 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1658 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1659 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1660 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1661 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1662 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1666 /* fill hard-wired DACs first */
1667 if (fill_hardwired) {
1670 mapped = map_singles(codec, cfg->line_outs,
1672 spec->private_dac_nids,
1674 mapped |= map_singles(codec, cfg->hp_outs,
1676 spec->multiout.hp_out_nid,
1678 mapped |= map_singles(codec, cfg->speaker_outs,
1680 spec->multiout.extra_out_nid,
1681 spec->speaker_paths);
1682 if (!spec->no_multi_io &&
1683 fill_mio_first && cfg->line_outs == 1 &&
1684 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1685 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1692 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1693 spec->private_dac_nids, spec->out_paths,
1694 spec->main_out_badness);
1696 if (!spec->no_multi_io && fill_mio_first &&
1697 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1698 /* try to fill multi-io first */
1699 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1702 /* we don't count badness at this stage yet */
1705 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1706 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1707 spec->multiout.hp_out_nid,
1709 spec->extra_out_badness);
1714 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1715 err = try_assign_dacs(codec, cfg->speaker_outs,
1717 spec->multiout.extra_out_nid,
1718 spec->speaker_paths,
1719 spec->extra_out_badness);
1724 if (!spec->no_multi_io &&
1725 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1726 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1732 if (spec->mixer_nid) {
1733 spec->aamix_out_paths[0] =
1734 check_aamix_out_path(codec, spec->out_paths[0]);
1735 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1736 spec->aamix_out_paths[1] =
1737 check_aamix_out_path(codec, spec->hp_paths[0]);
1738 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1739 spec->aamix_out_paths[2] =
1740 check_aamix_out_path(codec, spec->speaker_paths[0]);
1743 if (!spec->no_multi_io &&
1744 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1745 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1746 spec->multi_ios = 1; /* give badness */
1748 /* re-count num_dacs and squash invalid entries */
1749 spec->multiout.num_dacs = 0;
1750 for (i = 0; i < cfg->line_outs; i++) {
1751 if (spec->private_dac_nids[i])
1752 spec->multiout.num_dacs++;
1754 memmove(spec->private_dac_nids + i,
1755 spec->private_dac_nids + i + 1,
1756 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1757 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1761 spec->ext_channel_count = spec->min_channel_count =
1762 spec->multiout.num_dacs * 2;
1764 if (spec->multi_ios == 2) {
1765 for (i = 0; i < 2; i++)
1766 spec->private_dac_nids[spec->multiout.num_dacs++] =
1767 spec->multi_io[i].dac;
1768 } else if (spec->multi_ios) {
1769 spec->multi_ios = 0;
1770 badness += BAD_MULTI_IO;
1773 if (spec->indep_hp && !indep_hp_possible(codec))
1774 badness += BAD_NO_INDEP_HP;
1776 /* re-fill the shared DAC for speaker / headphone */
1777 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1778 refill_shared_dacs(codec, cfg->hp_outs,
1779 spec->multiout.hp_out_nid,
1781 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1782 refill_shared_dacs(codec, cfg->speaker_outs,
1783 spec->multiout.extra_out_nid,
1784 spec->speaker_paths);
1789 #define DEBUG_BADNESS
1791 #ifdef DEBUG_BADNESS
1792 #define debug_badness(fmt, ...) \
1793 codec_dbg(codec, fmt, ##__VA_ARGS__)
1795 #define debug_badness(fmt, ...) \
1796 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1799 #ifdef DEBUG_BADNESS
1800 static inline void print_nid_path_idx(struct hda_codec *codec,
1801 const char *pfx, int idx)
1803 struct nid_path *path;
1805 path = snd_hda_get_path_from_idx(codec, idx);
1807 print_nid_path(codec, pfx, path);
1810 static void debug_show_configs(struct hda_codec *codec,
1811 struct auto_pin_cfg *cfg)
1813 struct hda_gen_spec *spec = codec->spec;
1814 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1817 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1818 cfg->line_out_pins[0], cfg->line_out_pins[1],
1819 cfg->line_out_pins[2], cfg->line_out_pins[3],
1820 spec->multiout.dac_nids[0],
1821 spec->multiout.dac_nids[1],
1822 spec->multiout.dac_nids[2],
1823 spec->multiout.dac_nids[3],
1824 lo_type[cfg->line_out_type]);
1825 for (i = 0; i < cfg->line_outs; i++)
1826 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1827 if (spec->multi_ios > 0)
1828 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1830 spec->multi_io[0].pin, spec->multi_io[1].pin,
1831 spec->multi_io[0].dac, spec->multi_io[1].dac);
1832 for (i = 0; i < spec->multi_ios; i++)
1833 print_nid_path_idx(codec, " mio",
1834 spec->out_paths[cfg->line_outs + i]);
1836 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1837 cfg->hp_pins[0], cfg->hp_pins[1],
1838 cfg->hp_pins[2], cfg->hp_pins[3],
1839 spec->multiout.hp_out_nid[0],
1840 spec->multiout.hp_out_nid[1],
1841 spec->multiout.hp_out_nid[2],
1842 spec->multiout.hp_out_nid[3]);
1843 for (i = 0; i < cfg->hp_outs; i++)
1844 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1845 if (cfg->speaker_outs)
1846 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1847 cfg->speaker_pins[0], cfg->speaker_pins[1],
1848 cfg->speaker_pins[2], cfg->speaker_pins[3],
1849 spec->multiout.extra_out_nid[0],
1850 spec->multiout.extra_out_nid[1],
1851 spec->multiout.extra_out_nid[2],
1852 spec->multiout.extra_out_nid[3]);
1853 for (i = 0; i < cfg->speaker_outs; i++)
1854 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1855 for (i = 0; i < 3; i++)
1856 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1859 #define debug_show_configs(codec, cfg) /* NOP */
1862 /* find all available DACs of the codec */
1863 static void fill_all_dac_nids(struct hda_codec *codec)
1865 struct hda_gen_spec *spec = codec->spec;
1867 hda_nid_t nid = codec->start_nid;
1869 spec->num_all_dacs = 0;
1870 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1871 for (i = 0; i < codec->num_nodes; i++, nid++) {
1872 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1874 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1875 codec_err(codec, "Too many DACs!\n");
1878 spec->all_dacs[spec->num_all_dacs++] = nid;
1882 static int parse_output_paths(struct hda_codec *codec)
1884 struct hda_gen_spec *spec = codec->spec;
1885 struct auto_pin_cfg *cfg = &spec->autocfg;
1886 struct auto_pin_cfg *best_cfg;
1888 int best_badness = INT_MAX;
1890 bool fill_hardwired = true, fill_mio_first = true;
1891 bool best_wired = true, best_mio = true;
1892 bool hp_spk_swapped = false;
1894 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1900 badness = fill_and_eval_dacs(codec, fill_hardwired,
1906 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1907 cfg->line_out_type, fill_hardwired, fill_mio_first,
1909 debug_show_configs(codec, cfg);
1910 if (badness < best_badness) {
1911 best_badness = badness;
1913 best_wired = fill_hardwired;
1914 best_mio = fill_mio_first;
1918 fill_mio_first = !fill_mio_first;
1919 if (!fill_mio_first)
1921 fill_hardwired = !fill_hardwired;
1922 if (!fill_hardwired)
1926 hp_spk_swapped = true;
1927 if (cfg->speaker_outs > 0 &&
1928 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1929 cfg->hp_outs = cfg->line_outs;
1930 memcpy(cfg->hp_pins, cfg->line_out_pins,
1931 sizeof(cfg->hp_pins));
1932 cfg->line_outs = cfg->speaker_outs;
1933 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1934 sizeof(cfg->speaker_pins));
1935 cfg->speaker_outs = 0;
1936 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1937 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1938 fill_hardwired = true;
1941 if (cfg->hp_outs > 0 &&
1942 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1943 cfg->speaker_outs = cfg->line_outs;
1944 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1945 sizeof(cfg->speaker_pins));
1946 cfg->line_outs = cfg->hp_outs;
1947 memcpy(cfg->line_out_pins, cfg->hp_pins,
1948 sizeof(cfg->hp_pins));
1950 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1951 cfg->line_out_type = AUTO_PIN_HP_OUT;
1952 fill_hardwired = true;
1959 debug_badness("==> restoring best_cfg\n");
1961 fill_and_eval_dacs(codec, best_wired, best_mio);
1963 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1964 cfg->line_out_type, best_wired, best_mio);
1965 debug_show_configs(codec, cfg);
1967 if (cfg->line_out_pins[0]) {
1968 struct nid_path *path;
1969 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1971 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1972 if (spec->vmaster_nid) {
1973 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1974 HDA_OUTPUT, spec->vmaster_tlv);
1975 if (spec->dac_min_mute)
1976 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
1980 /* set initial pinctl targets */
1981 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1985 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1986 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1987 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1988 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1989 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1990 set_pin_targets(codec, cfg->speaker_outs,
1991 cfg->speaker_pins, val);
1994 /* clear indep_hp flag if not available */
1995 if (spec->indep_hp && !indep_hp_possible(codec))
2002 /* add playback controls from the parsed DAC table */
2003 static int create_multi_out_ctls(struct hda_codec *codec,
2004 const struct auto_pin_cfg *cfg)
2006 struct hda_gen_spec *spec = codec->spec;
2007 int i, err, noutputs;
2009 noutputs = cfg->line_outs;
2010 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2011 noutputs += spec->multi_ios;
2013 for (i = 0; i < noutputs; i++) {
2016 struct nid_path *path;
2018 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
2022 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
2023 if (!name || !strcmp(name, "CLFE")) {
2025 err = add_vol_ctl(codec, "Center", 0, 1, path);
2028 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2032 err = add_stereo_vol(codec, name, index, path);
2037 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2038 if (!name || !strcmp(name, "CLFE")) {
2039 err = add_sw_ctl(codec, "Center", 0, 1, path);
2042 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2046 err = add_stereo_sw(codec, name, index, path);
2054 static int create_extra_out(struct hda_codec *codec, int path_idx,
2055 const char *pfx, int cidx)
2057 struct nid_path *path;
2060 path = snd_hda_get_path_from_idx(codec, path_idx);
2063 err = add_stereo_vol(codec, pfx, cidx, path);
2066 err = add_stereo_sw(codec, pfx, cidx, path);
2072 /* add playback controls for speaker and HP outputs */
2073 static int create_extra_outs(struct hda_codec *codec, int num_pins,
2074 const int *paths, const char *pfx)
2078 for (i = 0; i < num_pins; i++) {
2080 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2083 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2084 name = "Bass Speaker";
2085 else if (num_pins >= 3) {
2086 snprintf(tmp, sizeof(tmp), "%s %s",
2087 pfx, channel_name[i]);
2093 err = create_extra_out(codec, paths[i], name, idx);
2100 static int create_hp_out_ctls(struct hda_codec *codec)
2102 struct hda_gen_spec *spec = codec->spec;
2103 return create_extra_outs(codec, spec->autocfg.hp_outs,
2108 static int create_speaker_out_ctls(struct hda_codec *codec)
2110 struct hda_gen_spec *spec = codec->spec;
2111 return create_extra_outs(codec, spec->autocfg.speaker_outs,
2112 spec->speaker_paths,
2117 * independent HP controls
2120 static void call_hp_automute(struct hda_codec *codec,
2121 struct hda_jack_callback *jack);
2122 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2123 struct snd_ctl_elem_info *uinfo)
2125 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2128 static int indep_hp_get(struct snd_kcontrol *kcontrol,
2129 struct snd_ctl_elem_value *ucontrol)
2131 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2132 struct hda_gen_spec *spec = codec->spec;
2133 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2137 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2138 int nomix_path_idx, int mix_path_idx,
2141 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2142 struct snd_ctl_elem_value *ucontrol)
2144 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2145 struct hda_gen_spec *spec = codec->spec;
2146 unsigned int select = ucontrol->value.enumerated.item[0];
2149 mutex_lock(&spec->pcm_mutex);
2150 if (spec->active_streams) {
2155 if (spec->indep_hp_enabled != select) {
2157 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2158 dacp = &spec->private_dac_nids[0];
2160 dacp = &spec->multiout.hp_out_nid[0];
2162 /* update HP aamix paths in case it conflicts with indep HP */
2163 if (spec->have_aamix_ctl) {
2164 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2165 update_aamix_paths(codec, spec->aamix_mode,
2167 spec->aamix_out_paths[0],
2168 spec->autocfg.line_out_type);
2170 update_aamix_paths(codec, spec->aamix_mode,
2172 spec->aamix_out_paths[1],
2176 spec->indep_hp_enabled = select;
2177 if (spec->indep_hp_enabled)
2180 *dacp = spec->alt_dac_nid;
2182 call_hp_automute(codec, NULL);
2186 mutex_unlock(&spec->pcm_mutex);
2190 static const struct snd_kcontrol_new indep_hp_ctl = {
2191 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2192 .name = "Independent HP",
2193 .info = indep_hp_info,
2194 .get = indep_hp_get,
2195 .put = indep_hp_put,
2199 static int create_indep_hp_ctls(struct hda_codec *codec)
2201 struct hda_gen_spec *spec = codec->spec;
2204 if (!spec->indep_hp)
2206 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2207 dac = spec->multiout.dac_nids[0];
2209 dac = spec->multiout.hp_out_nid[0];
2215 spec->indep_hp_enabled = false;
2216 spec->alt_dac_nid = dac;
2217 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2223 * channel mode enum control
2226 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2227 struct snd_ctl_elem_info *uinfo)
2229 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2230 struct hda_gen_spec *spec = codec->spec;
2233 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2235 uinfo->value.enumerated.items = spec->multi_ios + 1;
2236 if (uinfo->value.enumerated.item > spec->multi_ios)
2237 uinfo->value.enumerated.item = spec->multi_ios;
2238 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2239 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2243 static int ch_mode_get(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 ucontrol->value.enumerated.item[0] =
2249 (spec->ext_channel_count - spec->min_channel_count) / 2;
2253 static inline struct nid_path *
2254 get_multiio_path(struct hda_codec *codec, int idx)
2256 struct hda_gen_spec *spec = codec->spec;
2257 return snd_hda_get_path_from_idx(codec,
2258 spec->out_paths[spec->autocfg.line_outs + idx]);
2261 static void update_automute_all(struct hda_codec *codec);
2263 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2264 * used for output paths
2266 static bool aamix_default(struct hda_gen_spec *spec)
2268 return !spec->have_aamix_ctl || spec->aamix_mode;
2271 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2273 struct hda_gen_spec *spec = codec->spec;
2274 hda_nid_t nid = spec->multi_io[idx].pin;
2275 struct nid_path *path;
2277 path = get_multiio_path(codec, idx);
2281 if (path->active == output)
2285 set_pin_target(codec, nid, PIN_OUT, true);
2286 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2287 set_pin_eapd(codec, nid, true);
2289 set_pin_eapd(codec, nid, false);
2290 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2291 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2292 path_power_down_sync(codec, path);
2295 /* update jack retasking in case it modifies any of them */
2296 update_automute_all(codec);
2301 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2302 struct snd_ctl_elem_value *ucontrol)
2304 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2305 struct hda_gen_spec *spec = codec->spec;
2308 ch = ucontrol->value.enumerated.item[0];
2309 if (ch < 0 || ch > spec->multi_ios)
2311 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2313 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2314 for (i = 0; i < spec->multi_ios; i++)
2315 set_multi_io(codec, i, i < ch);
2316 spec->multiout.max_channels = max(spec->ext_channel_count,
2317 spec->const_channel_count);
2318 if (spec->need_dac_fix)
2319 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2323 static const struct snd_kcontrol_new channel_mode_enum = {
2324 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2325 .name = "Channel Mode",
2326 .info = ch_mode_info,
2331 static int create_multi_channel_mode(struct hda_codec *codec)
2333 struct hda_gen_spec *spec = codec->spec;
2335 if (spec->multi_ios > 0) {
2336 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2343 * aamix loopback enable/disable switch
2346 #define loopback_mixing_info indep_hp_info
2348 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2349 struct snd_ctl_elem_value *ucontrol)
2351 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2352 struct hda_gen_spec *spec = codec->spec;
2353 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2357 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2358 int nomix_path_idx, int mix_path_idx,
2361 struct hda_gen_spec *spec = codec->spec;
2362 struct nid_path *nomix_path, *mix_path;
2364 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2365 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2366 if (!nomix_path || !mix_path)
2369 /* if HP aamix path is driven from a different DAC and the
2370 * independent HP mode is ON, can't turn on aamix path
2372 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2373 mix_path->path[0] != spec->alt_dac_nid)
2377 snd_hda_activate_path(codec, nomix_path, false, true);
2378 snd_hda_activate_path(codec, mix_path, true, true);
2379 path_power_down_sync(codec, nomix_path);
2381 snd_hda_activate_path(codec, mix_path, false, false);
2382 snd_hda_activate_path(codec, nomix_path, true, false);
2383 path_power_down_sync(codec, mix_path);
2387 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2388 struct snd_ctl_elem_value *ucontrol)
2390 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2391 struct hda_gen_spec *spec = codec->spec;
2392 unsigned int val = ucontrol->value.enumerated.item[0];
2394 if (val == spec->aamix_mode)
2396 spec->aamix_mode = val;
2397 update_aamix_paths(codec, val, spec->out_paths[0],
2398 spec->aamix_out_paths[0],
2399 spec->autocfg.line_out_type);
2400 update_aamix_paths(codec, val, spec->hp_paths[0],
2401 spec->aamix_out_paths[1],
2403 update_aamix_paths(codec, val, spec->speaker_paths[0],
2404 spec->aamix_out_paths[2],
2405 AUTO_PIN_SPEAKER_OUT);
2409 static const struct snd_kcontrol_new loopback_mixing_enum = {
2410 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2411 .name = "Loopback Mixing",
2412 .info = loopback_mixing_info,
2413 .get = loopback_mixing_get,
2414 .put = loopback_mixing_put,
2417 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2419 struct hda_gen_spec *spec = codec->spec;
2421 if (!spec->mixer_nid)
2423 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2424 spec->aamix_out_paths[2]))
2426 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2428 spec->have_aamix_ctl = 1;
2433 * shared headphone/mic handling
2436 static void call_update_outputs(struct hda_codec *codec);
2438 /* for shared I/O, change the pin-control accordingly */
2439 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2441 struct hda_gen_spec *spec = codec->spec;
2446 pin = spec->hp_mic_pin;
2447 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2450 val = snd_hda_codec_get_pin_target(codec, pin);
2460 val = snd_hda_get_default_vref(codec, pin);
2461 /* if the HP pin doesn't support VREF and the codec driver gives an
2462 * alternative pin, set up the VREF on that pin instead
2464 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2465 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2466 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2467 if (vref_val != AC_PINCTL_VREF_HIZ)
2468 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2469 PIN_IN | (as_mic ? vref_val : 0));
2472 if (!spec->hp_mic_jack_modes) {
2477 set_pin_target(codec, pin, val, true);
2478 call_hp_automute(codec, NULL);
2482 /* create a shared input with the headphone out */
2483 static int create_hp_mic(struct hda_codec *codec)
2485 struct hda_gen_spec *spec = codec->spec;
2486 struct auto_pin_cfg *cfg = &spec->autocfg;
2487 unsigned int defcfg;
2490 if (!spec->hp_mic) {
2491 if (spec->suppress_hp_mic_detect)
2493 /* automatic detection: only if no input or a single internal
2494 * input pin is found, try to detect the shared hp/mic
2496 if (cfg->num_inputs > 1)
2498 else if (cfg->num_inputs == 1) {
2499 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2500 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2505 spec->hp_mic = 0; /* clear once */
2506 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2510 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2511 nid = cfg->line_out_pins[0];
2512 else if (cfg->hp_outs > 0)
2513 nid = cfg->hp_pins[0];
2517 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2518 return 0; /* no input */
2520 cfg->inputs[cfg->num_inputs].pin = nid;
2521 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2522 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2525 spec->hp_mic_pin = nid;
2526 /* we can't handle auto-mic together with HP-mic */
2527 spec->suppress_auto_mic = 1;
2528 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
2536 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2538 static const char * const out_jack_texts[] = {
2539 "Line Out", "Headphone Out",
2542 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2543 struct snd_ctl_elem_info *uinfo)
2545 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2548 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2549 struct snd_ctl_elem_value *ucontrol)
2551 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2552 hda_nid_t nid = kcontrol->private_value;
2553 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2554 ucontrol->value.enumerated.item[0] = 1;
2556 ucontrol->value.enumerated.item[0] = 0;
2560 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2561 struct snd_ctl_elem_value *ucontrol)
2563 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2564 hda_nid_t nid = kcontrol->private_value;
2567 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2568 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2570 snd_hda_set_pin_ctl_cache(codec, nid, val);
2574 static const struct snd_kcontrol_new out_jack_mode_enum = {
2575 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2576 .info = out_jack_mode_info,
2577 .get = out_jack_mode_get,
2578 .put = out_jack_mode_put,
2581 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2583 struct hda_gen_spec *spec = codec->spec;
2586 for (i = 0; i < spec->kctls.used; i++) {
2587 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2588 if (!strcmp(kctl->name, name) && kctl->index == idx)
2594 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2595 char *name, size_t name_len)
2597 struct hda_gen_spec *spec = codec->spec;
2600 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2601 strlcat(name, " Jack Mode", name_len);
2603 for (; find_kctl_name(codec, name, idx); idx++)
2607 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2609 struct hda_gen_spec *spec = codec->spec;
2610 if (spec->add_jack_modes) {
2611 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2612 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2618 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2621 struct hda_gen_spec *spec = codec->spec;
2624 for (i = 0; i < num_pins; i++) {
2625 hda_nid_t pin = pins[i];
2626 if (pin == spec->hp_mic_pin)
2628 if (get_out_jack_num_items(codec, pin) > 1) {
2629 struct snd_kcontrol_new *knew;
2630 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2631 get_jack_mode_name(codec, pin, name, sizeof(name));
2632 knew = snd_hda_gen_add_kctl(spec, name,
2633 &out_jack_mode_enum);
2636 knew->private_value = pin;
2647 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2650 static const char * const vref_texts[NUM_VREFS] = {
2651 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2652 "", "Mic 80pc Bias", "Mic 100pc Bias"
2655 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2657 unsigned int pincap;
2659 pincap = snd_hda_query_pin_caps(codec, pin);
2660 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2661 /* filter out unusual vrefs */
2662 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2666 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2667 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2669 unsigned int i, n = 0;
2671 for (i = 0; i < NUM_VREFS; i++) {
2672 if (vref_caps & (1 << i)) {
2681 /* convert back from the vref ctl index to the enum item index */
2682 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2684 unsigned int i, n = 0;
2686 for (i = 0; i < NUM_VREFS; i++) {
2689 if (vref_caps & (1 << i))
2695 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2696 struct snd_ctl_elem_info *uinfo)
2698 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2699 hda_nid_t nid = kcontrol->private_value;
2700 unsigned int vref_caps = get_vref_caps(codec, nid);
2702 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2704 /* set the right text */
2705 strcpy(uinfo->value.enumerated.name,
2706 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2710 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2711 struct snd_ctl_elem_value *ucontrol)
2713 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2714 hda_nid_t nid = kcontrol->private_value;
2715 unsigned int vref_caps = get_vref_caps(codec, nid);
2718 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2719 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2723 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2724 struct snd_ctl_elem_value *ucontrol)
2726 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2727 hda_nid_t nid = kcontrol->private_value;
2728 unsigned int vref_caps = get_vref_caps(codec, nid);
2729 unsigned int val, idx;
2731 val = snd_hda_codec_get_pin_target(codec, nid);
2732 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2733 if (idx == ucontrol->value.enumerated.item[0])
2736 val &= ~AC_PINCTL_VREFEN;
2737 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2738 snd_hda_set_pin_ctl_cache(codec, nid, val);
2742 static const struct snd_kcontrol_new in_jack_mode_enum = {
2743 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2744 .info = in_jack_mode_info,
2745 .get = in_jack_mode_get,
2746 .put = in_jack_mode_put,
2749 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2751 struct hda_gen_spec *spec = codec->spec;
2753 if (spec->add_jack_modes)
2754 nitems = hweight32(get_vref_caps(codec, pin));
2755 return nitems ? nitems : 1;
2758 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2760 struct hda_gen_spec *spec = codec->spec;
2761 struct snd_kcontrol_new *knew;
2762 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2763 unsigned int defcfg;
2765 if (pin == spec->hp_mic_pin)
2766 return 0; /* already done in create_out_jack_mode() */
2768 /* no jack mode for fixed pins */
2769 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2770 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2773 /* no multiple vref caps? */
2774 if (get_in_jack_num_items(codec, pin) <= 1)
2777 get_jack_mode_name(codec, pin, name, sizeof(name));
2778 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2781 knew->private_value = pin;
2786 * HP/mic shared jack mode
2788 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2789 struct snd_ctl_elem_info *uinfo)
2791 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2792 hda_nid_t nid = kcontrol->private_value;
2793 int out_jacks = get_out_jack_num_items(codec, nid);
2794 int in_jacks = get_in_jack_num_items(codec, nid);
2795 const char *text = NULL;
2798 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2800 uinfo->value.enumerated.items = out_jacks + in_jacks;
2801 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2802 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2803 idx = uinfo->value.enumerated.item;
2804 if (idx < out_jacks) {
2806 text = out_jack_texts[idx];
2808 text = "Headphone Out";
2812 unsigned int vref_caps = get_vref_caps(codec, nid);
2813 text = vref_texts[get_vref_idx(vref_caps, idx)];
2818 strcpy(uinfo->value.enumerated.name, text);
2822 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2824 int out_jacks = get_out_jack_num_items(codec, nid);
2825 int in_jacks = get_in_jack_num_items(codec, nid);
2826 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2829 if (val & PIN_OUT) {
2830 if (out_jacks > 1 && val == PIN_HP)
2832 } else if (val & PIN_IN) {
2835 unsigned int vref_caps = get_vref_caps(codec, nid);
2836 val &= AC_PINCTL_VREFEN;
2837 idx += cvt_from_vref_idx(vref_caps, val);
2843 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2844 struct snd_ctl_elem_value *ucontrol)
2846 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2847 hda_nid_t nid = kcontrol->private_value;
2848 ucontrol->value.enumerated.item[0] =
2849 get_cur_hp_mic_jack_mode(codec, nid);
2853 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
2856 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2857 hda_nid_t nid = kcontrol->private_value;
2858 int out_jacks = get_out_jack_num_items(codec, nid);
2859 int in_jacks = get_in_jack_num_items(codec, nid);
2860 unsigned int val, oldval, idx;
2862 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2863 idx = ucontrol->value.enumerated.item[0];
2867 if (idx < out_jacks) {
2869 val = idx ? PIN_HP : PIN_OUT;
2875 unsigned int vref_caps = get_vref_caps(codec, nid);
2876 val = snd_hda_codec_get_pin_target(codec, nid);
2877 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2878 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
2880 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
2882 snd_hda_set_pin_ctl_cache(codec, nid, val);
2883 call_hp_automute(codec, NULL);
2888 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2889 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2890 .info = hp_mic_jack_mode_info,
2891 .get = hp_mic_jack_mode_get,
2892 .put = hp_mic_jack_mode_put,
2895 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2897 struct hda_gen_spec *spec = codec->spec;
2898 struct snd_kcontrol_new *knew;
2900 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2901 &hp_mic_jack_mode_enum);
2904 knew->private_value = pin;
2905 spec->hp_mic_jack_modes = 1;
2913 /* add the powersave loopback-list entry */
2914 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2916 struct hda_amp_list *list;
2918 list = snd_array_new(&spec->loopback_list);
2922 list->dir = HDA_INPUT;
2924 spec->loopback.amplist = spec->loopback_list.list;
2928 /* return true if either a volume or a mute amp is found for the given
2929 * aamix path; the amp has to be either in the mixer node or its direct leaf
2931 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2932 hda_nid_t pin, unsigned int *mix_val,
2933 unsigned int *mute_val)
2936 const hda_nid_t *list;
2939 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2943 *mix_val = *mute_val = 0;
2944 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2945 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2946 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2947 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2948 if (*mix_val && *mute_val)
2951 /* check leaf node */
2952 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2953 if (num_conns < idx)
2956 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2957 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
2958 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2959 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2960 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
2961 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2963 return *mix_val || *mute_val;
2966 /* create input playback/capture controls for the given pin */
2967 static int new_analog_input(struct hda_codec *codec, int input_idx,
2968 hda_nid_t pin, const char *ctlname, int ctlidx,
2971 struct hda_gen_spec *spec = codec->spec;
2972 struct nid_path *path;
2973 unsigned int mix_val, mute_val;
2976 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
2979 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
2982 print_nid_path(codec, "loopback", path);
2983 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
2985 idx = path->idx[path->depth - 1];
2987 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
2990 path->ctls[NID_PATH_VOL_CTL] = mix_val;
2994 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
2997 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
3000 path->active = true;
3001 err = add_loopback_list(spec, mix_nid, idx);
3005 if (spec->mixer_nid != spec->mixer_merge_nid &&
3006 !spec->loopback_merge_path) {
3007 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3008 spec->mixer_merge_nid, 0);
3010 print_nid_path(codec, "loopback-merge", path);
3011 path->active = true;
3012 spec->loopback_merge_path =
3013 snd_hda_get_path_idx(codec, path);
3020 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3022 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3023 return (pincap & AC_PINCAP_IN) != 0;
3026 /* Parse the codec tree and retrieve ADCs */
3027 static int fill_adc_nids(struct hda_codec *codec)
3029 struct hda_gen_spec *spec = codec->spec;
3031 hda_nid_t *adc_nids = spec->adc_nids;
3032 int max_nums = ARRAY_SIZE(spec->adc_nids);
3035 nid = codec->start_nid;
3036 for (i = 0; i < codec->num_nodes; i++, nid++) {
3037 unsigned int caps = get_wcaps(codec, nid);
3038 int type = get_wcaps_type(caps);
3040 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3042 adc_nids[nums] = nid;
3043 if (++nums >= max_nums)
3046 spec->num_adc_nids = nums;
3048 /* copy the detected ADCs to all_adcs[] */
3049 spec->num_all_adcs = nums;
3050 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3055 /* filter out invalid adc_nids that don't give all active input pins;
3056 * if needed, check whether dynamic ADC-switching is available
3058 static int check_dyn_adc_switch(struct hda_codec *codec)
3060 struct hda_gen_spec *spec = codec->spec;
3061 struct hda_input_mux *imux = &spec->input_mux;
3062 unsigned int ok_bits;
3067 for (n = 0; n < spec->num_adc_nids; n++) {
3068 for (i = 0; i < imux->num_items; i++) {
3069 if (!spec->input_paths[i][n])
3072 if (i >= imux->num_items) {
3073 ok_bits |= (1 << n);
3079 /* check whether ADC-switch is possible */
3080 for (i = 0; i < imux->num_items; i++) {
3081 for (n = 0; n < spec->num_adc_nids; n++) {
3082 if (spec->input_paths[i][n]) {
3083 spec->dyn_adc_idx[i] = n;
3089 codec_dbg(codec, "enabling ADC switching\n");
3090 spec->dyn_adc_switch = 1;
3091 } else if (nums != spec->num_adc_nids) {
3092 /* shrink the invalid adcs and input paths */
3094 for (n = 0; n < spec->num_adc_nids; n++) {
3095 if (!(ok_bits & (1 << n)))
3098 spec->adc_nids[nums] = spec->adc_nids[n];
3099 for (i = 0; i < imux->num_items; i++) {
3100 invalidate_nid_path(codec,
3101 spec->input_paths[i][nums]);
3102 spec->input_paths[i][nums] =
3103 spec->input_paths[i][n];
3108 spec->num_adc_nids = nums;
3111 if (imux->num_items == 1 ||
3112 (imux->num_items == 2 && spec->hp_mic)) {
3113 codec_dbg(codec, "reducing to a single ADC\n");
3114 spec->num_adc_nids = 1; /* reduce to a single ADC */
3117 /* single index for individual volumes ctls */
3118 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3119 spec->num_adc_nids = 1;
3124 /* parse capture source paths from the given pin and create imux items */
3125 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3126 int cfg_idx, int num_adcs,
3127 const char *label, int anchor)
3129 struct hda_gen_spec *spec = codec->spec;
3130 struct hda_input_mux *imux = &spec->input_mux;
3131 int imux_idx = imux->num_items;
3132 bool imux_added = false;
3135 for (c = 0; c < num_adcs; c++) {
3136 struct nid_path *path;
3137 hda_nid_t adc = spec->adc_nids[c];
3139 if (!is_reachable_path(codec, pin, adc))
3141 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3144 print_nid_path(codec, "input", path);
3145 spec->input_paths[imux_idx][c] =
3146 snd_hda_get_path_idx(codec, path);
3149 if (spec->hp_mic_pin == pin)
3150 spec->hp_mic_mux_idx = imux->num_items;
3151 spec->imux_pins[imux->num_items] = pin;
3152 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3154 if (spec->dyn_adc_switch)
3155 spec->dyn_adc_idx[imux_idx] = c;
3163 * create playback/capture controls for input pins
3166 /* fill the label for each input at first */
3167 static int fill_input_pin_labels(struct hda_codec *codec)
3169 struct hda_gen_spec *spec = codec->spec;
3170 const struct auto_pin_cfg *cfg = &spec->autocfg;
3173 for (i = 0; i < cfg->num_inputs; i++) {
3174 hda_nid_t pin = cfg->inputs[i].pin;
3178 if (!is_input_pin(codec, pin))
3181 label = hda_get_autocfg_input_label(codec, cfg, i);
3183 for (j = i - 1; j >= 0; j--) {
3184 if (spec->input_labels[j] &&
3185 !strcmp(spec->input_labels[j], label)) {
3186 idx = spec->input_label_idxs[j] + 1;
3191 spec->input_labels[i] = label;
3192 spec->input_label_idxs[i] = idx;
3198 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3200 static int create_input_ctls(struct hda_codec *codec)
3202 struct hda_gen_spec *spec = codec->spec;
3203 const struct auto_pin_cfg *cfg = &spec->autocfg;
3204 hda_nid_t mixer = spec->mixer_nid;
3209 num_adcs = fill_adc_nids(codec);
3213 err = fill_input_pin_labels(codec);
3217 for (i = 0; i < cfg->num_inputs; i++) {
3220 pin = cfg->inputs[i].pin;
3221 if (!is_input_pin(codec, pin))
3225 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3226 val |= snd_hda_get_default_vref(codec, pin);
3227 if (pin != spec->hp_mic_pin)
3228 set_pin_target(codec, pin, val, false);
3231 if (is_reachable_path(codec, pin, mixer)) {
3232 err = new_analog_input(codec, i, pin,
3233 spec->input_labels[i],
3234 spec->input_label_idxs[i],
3241 err = parse_capture_source(codec, pin, i, num_adcs,
3242 spec->input_labels[i], -mixer);
3246 if (spec->add_jack_modes) {
3247 err = create_in_jack_mode(codec, pin);
3253 /* add stereo mix when explicitly enabled via hint */
3254 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3255 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3260 spec->suppress_auto_mic = 1;
3271 /* get the input path specified by the given adc and imux indices */
3272 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3274 struct hda_gen_spec *spec = codec->spec;
3275 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3279 if (spec->dyn_adc_switch)
3280 adc_idx = spec->dyn_adc_idx[imux_idx];
3281 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3285 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3288 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3291 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3292 struct snd_ctl_elem_info *uinfo)
3294 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3295 struct hda_gen_spec *spec = codec->spec;
3296 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3299 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3300 struct snd_ctl_elem_value *ucontrol)
3302 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3303 struct hda_gen_spec *spec = codec->spec;
3304 /* the ctls are created at once with multiple counts */
3305 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3307 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3311 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3312 struct snd_ctl_elem_value *ucontrol)
3314 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3315 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3316 return mux_select(codec, adc_idx,
3317 ucontrol->value.enumerated.item[0]);
3320 static const struct snd_kcontrol_new cap_src_temp = {
3321 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3322 .name = "Input Source",
3323 .info = mux_enum_info,
3324 .get = mux_enum_get,
3325 .put = mux_enum_put,
3329 * capture volume and capture switch ctls
3332 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3333 struct snd_ctl_elem_value *ucontrol);
3335 /* call the given amp update function for all amps in the imux list at once */
3336 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3337 struct snd_ctl_elem_value *ucontrol,
3338 put_call_t func, int type)
3340 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3341 struct hda_gen_spec *spec = codec->spec;
3342 const struct hda_input_mux *imux;
3343 struct nid_path *path;
3344 int i, adc_idx, err = 0;
3346 imux = &spec->input_mux;
3347 adc_idx = kcontrol->id.index;
3348 mutex_lock(&codec->control_mutex);
3349 /* we use the cache-only update at first since multiple input paths
3350 * may shared the same amp; by updating only caches, the redundant
3351 * writes to hardware can be reduced.
3353 codec->cached_write = 1;
3354 for (i = 0; i < imux->num_items; i++) {
3355 path = get_input_path(codec, adc_idx, i);
3356 if (!path || !path->ctls[type])
3358 kcontrol->private_value = path->ctls[type];
3359 err = func(kcontrol, ucontrol);
3364 codec->cached_write = 0;
3365 mutex_unlock(&codec->control_mutex);
3366 snd_hda_codec_flush_cache(codec); /* flush the updates */
3367 if (err >= 0 && spec->cap_sync_hook)
3368 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3372 /* capture volume ctl callbacks */
3373 #define cap_vol_info snd_hda_mixer_amp_volume_info
3374 #define cap_vol_get snd_hda_mixer_amp_volume_get
3375 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3377 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3378 struct snd_ctl_elem_value *ucontrol)
3380 return cap_put_caller(kcontrol, ucontrol,
3381 snd_hda_mixer_amp_volume_put,
3385 static const struct snd_kcontrol_new cap_vol_temp = {
3386 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3387 .name = "Capture Volume",
3388 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3389 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3390 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3391 .info = cap_vol_info,
3394 .tlv = { .c = cap_vol_tlv },
3397 /* capture switch ctl callbacks */
3398 #define cap_sw_info snd_ctl_boolean_stereo_info
3399 #define cap_sw_get snd_hda_mixer_amp_switch_get
3401 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3402 struct snd_ctl_elem_value *ucontrol)
3404 return cap_put_caller(kcontrol, ucontrol,
3405 snd_hda_mixer_amp_switch_put,
3409 static const struct snd_kcontrol_new cap_sw_temp = {
3410 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3411 .name = "Capture Switch",
3412 .info = cap_sw_info,
3417 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3422 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3423 for (depth = 0; depth < 3; depth++) {
3424 if (depth >= path->depth)
3426 i = path->depth - depth - 1;
3427 nid = path->path[i];
3428 if (!path->ctls[NID_PATH_VOL_CTL]) {
3429 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3430 path->ctls[NID_PATH_VOL_CTL] =
3431 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3432 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3433 int idx = path->idx[i];
3434 if (!depth && codec->single_adc_amp)
3436 path->ctls[NID_PATH_VOL_CTL] =
3437 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3440 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3441 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3442 path->ctls[NID_PATH_MUTE_CTL] =
3443 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3444 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3445 int idx = path->idx[i];
3446 if (!depth && codec->single_adc_amp)
3448 path->ctls[NID_PATH_MUTE_CTL] =
3449 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3456 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3458 struct hda_gen_spec *spec = codec->spec;
3459 struct auto_pin_cfg *cfg = &spec->autocfg;
3463 if (!spec->inv_dmic_split)
3465 for (i = 0; i < cfg->num_inputs; i++) {
3466 if (cfg->inputs[i].pin != nid)
3468 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3470 val = snd_hda_codec_get_pincfg(codec, nid);
3471 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3476 /* capture switch put callback for a single control with hook call */
3477 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3478 struct snd_ctl_elem_value *ucontrol)
3480 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3481 struct hda_gen_spec *spec = codec->spec;
3484 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3488 if (spec->cap_sync_hook)
3489 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3494 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3495 int idx, bool is_switch, unsigned int ctl,
3498 struct hda_gen_spec *spec = codec->spec;
3499 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3500 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3501 const char *sfx = is_switch ? "Switch" : "Volume";
3502 unsigned int chs = inv_dmic ? 1 : 3;
3503 struct snd_kcontrol_new *knew;
3509 snprintf(tmpname, sizeof(tmpname),
3510 "%s Capture %s", label, sfx);
3512 snprintf(tmpname, sizeof(tmpname),
3514 knew = add_control(spec, type, tmpname, idx,
3515 amp_val_replace_channels(ctl, chs));
3519 knew->put = cap_single_sw_put;
3523 /* Make independent right kcontrol */
3525 snprintf(tmpname, sizeof(tmpname),
3526 "Inverted %s Capture %s", label, sfx);
3528 snprintf(tmpname, sizeof(tmpname),
3529 "Inverted Capture %s", sfx);
3530 knew = add_control(spec, type, tmpname, idx,
3531 amp_val_replace_channels(ctl, 2));
3535 knew->put = cap_single_sw_put;
3539 /* create single (and simple) capture volume and switch controls */
3540 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3541 unsigned int vol_ctl, unsigned int sw_ctl,
3545 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3548 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3554 /* create bound capture volume and switch controls */
3555 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3556 unsigned int vol_ctl, unsigned int sw_ctl)
3558 struct hda_gen_spec *spec = codec->spec;
3559 struct snd_kcontrol_new *knew;
3562 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3566 knew->private_value = vol_ctl;
3567 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3570 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3574 knew->private_value = sw_ctl;
3575 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3580 /* return the vol ctl when used first in the imux list */
3581 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3583 struct nid_path *path;
3587 path = get_input_path(codec, 0, idx);
3590 ctl = path->ctls[type];
3593 for (i = 0; i < idx - 1; i++) {
3594 path = get_input_path(codec, 0, i);
3595 if (path && path->ctls[type] == ctl)
3601 /* create individual capture volume and switch controls per input */
3602 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3604 struct hda_gen_spec *spec = codec->spec;
3605 struct hda_input_mux *imux = &spec->input_mux;
3608 for (i = 0; i < imux->num_items; i++) {
3612 idx = imux->items[i].index;
3613 if (idx >= spec->autocfg.num_inputs)
3615 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3617 for (type = 0; type < 2; type++) {
3618 err = add_single_cap_ctl(codec,
3619 spec->input_labels[idx],
3620 spec->input_label_idxs[idx],
3622 get_first_cap_ctl(codec, i, type),
3631 static int create_capture_mixers(struct hda_codec *codec)
3633 struct hda_gen_spec *spec = codec->spec;
3634 struct hda_input_mux *imux = &spec->input_mux;
3635 int i, n, nums, err;
3637 if (spec->dyn_adc_switch)
3640 nums = spec->num_adc_nids;
3642 if (!spec->auto_mic && imux->num_items > 1) {
3643 struct snd_kcontrol_new *knew;
3645 name = nums > 1 ? "Input Source" : "Capture Source";
3646 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3652 for (n = 0; n < nums; n++) {
3654 bool multi_cap_vol = spec->multi_cap_vol;
3655 bool inv_dmic = false;
3659 for (i = 0; i < imux->num_items; i++) {
3660 struct nid_path *path;
3661 path = get_input_path(codec, n, i);
3664 parse_capvol_in_path(codec, path);
3666 vol = path->ctls[NID_PATH_VOL_CTL];
3667 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3669 if (!same_amp_caps(codec, vol,
3670 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3671 multi_cap_vol = true;
3674 sw = path->ctls[NID_PATH_MUTE_CTL];
3675 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3677 if (!same_amp_caps(codec, sw,
3678 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3679 multi_cap_vol = true;
3681 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3686 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3688 else if (!multi_cap_vol && !inv_dmic)
3689 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3691 err = create_multi_cap_vol_ctl(codec);
3700 * add mic boosts if needed
3703 /* check whether the given amp is feasible as a boost volume */
3704 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3709 if (!nid_has_volume(codec, nid, dir) ||
3710 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3711 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3714 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3715 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3721 /* look for a boost amp in a widget close to the pin */
3722 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3723 struct nid_path *path)
3725 unsigned int val = 0;
3729 for (depth = 0; depth < 3; depth++) {
3730 if (depth >= path->depth - 1)
3732 nid = path->path[depth];
3733 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3734 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3736 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3737 path->idx[depth])) {
3738 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3747 static int parse_mic_boost(struct hda_codec *codec)
3749 struct hda_gen_spec *spec = codec->spec;
3750 struct auto_pin_cfg *cfg = &spec->autocfg;
3751 struct hda_input_mux *imux = &spec->input_mux;
3754 if (!spec->num_adc_nids)
3757 for (i = 0; i < imux->num_items; i++) {
3758 struct nid_path *path;
3761 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3763 idx = imux->items[i].index;
3764 if (idx >= imux->num_items)
3767 /* check only line-in and mic pins */
3768 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3771 path = get_input_path(codec, 0, i);
3775 val = look_for_boost_amp(codec, path);
3779 /* create a boost control */
3780 snprintf(boost_label, sizeof(boost_label),
3781 "%s Boost Volume", spec->input_labels[idx]);
3782 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3783 spec->input_label_idxs[idx], val))
3786 path->ctls[NID_PATH_BOOST_CTL] = val;
3792 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3794 static void parse_digital(struct hda_codec *codec)
3796 struct hda_gen_spec *spec = codec->spec;
3797 struct nid_path *path;
3799 hda_nid_t dig_nid, pin;
3801 /* support multiple SPDIFs; the secondary is set up as a slave */
3803 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3804 pin = spec->autocfg.dig_out_pins[i];
3805 dig_nid = look_for_dac(codec, pin, true);
3808 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3811 print_nid_path(codec, "digout", path);
3812 path->active = true;
3813 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3814 set_pin_target(codec, pin, PIN_OUT, false);
3816 spec->multiout.dig_out_nid = dig_nid;
3817 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3819 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3820 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3822 spec->slave_dig_outs[nums - 1] = dig_nid;
3827 if (spec->autocfg.dig_in_pin) {
3828 pin = spec->autocfg.dig_in_pin;
3829 dig_nid = codec->start_nid;
3830 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3831 unsigned int wcaps = get_wcaps(codec, dig_nid);
3832 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3834 if (!(wcaps & AC_WCAP_DIGITAL))
3836 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3838 print_nid_path(codec, "digin", path);
3839 path->active = true;
3840 spec->dig_in_nid = dig_nid;
3841 spec->digin_path = snd_hda_get_path_idx(codec, path);
3842 set_pin_target(codec, pin, PIN_IN, false);
3851 * input MUX handling
3854 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3856 /* select the given imux item; either unmute exclusively or select the route */
3857 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3860 struct hda_gen_spec *spec = codec->spec;
3861 const struct hda_input_mux *imux;
3862 struct nid_path *old_path, *path;
3864 imux = &spec->input_mux;
3865 if (!imux->num_items)
3868 if (idx >= imux->num_items)
3869 idx = imux->num_items - 1;
3870 if (spec->cur_mux[adc_idx] == idx)
3873 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3876 if (old_path->active)
3877 snd_hda_activate_path(codec, old_path, false, false);
3879 spec->cur_mux[adc_idx] = idx;
3882 update_hp_mic(codec, adc_idx, false);
3884 if (spec->dyn_adc_switch)
3885 dyn_adc_pcm_resetup(codec, idx);
3887 path = get_input_path(codec, adc_idx, idx);
3892 snd_hda_activate_path(codec, path, true, false);
3893 if (spec->cap_sync_hook)
3894 spec->cap_sync_hook(codec, NULL, NULL);
3895 path_power_down_sync(codec, old_path);
3901 * Jack detections for HP auto-mute and mic-switch
3904 /* check each pin in the given array; returns true if any of them is plugged */
3905 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3908 bool present = false;
3910 for (i = 0; i < num_pins; i++) {
3911 hda_nid_t nid = pins[i];
3914 /* don't detect pins retasked as inputs */
3915 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3917 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3923 /* standard HP/line-out auto-mute helper */
3924 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
3925 int *paths, bool mute)
3927 struct hda_gen_spec *spec = codec->spec;
3930 for (i = 0; i < num_pins; i++) {
3931 hda_nid_t nid = pins[i];
3932 unsigned int val, oldval;
3936 if (spec->auto_mute_via_amp) {
3937 struct nid_path *path;
3940 path = snd_hda_get_path_from_idx(codec, paths[i]);
3943 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
3947 spec->mute_bits |= (1ULL << mute_nid);
3949 spec->mute_bits &= ~(1ULL << mute_nid);
3950 set_pin_eapd(codec, nid, !mute);
3954 oldval = snd_hda_codec_get_pin_target(codec, nid);
3955 if (oldval & PIN_IN)
3956 continue; /* no mute for inputs */
3957 /* don't reset VREF value in case it's controlling
3958 * the amp (see alc861_fixup_asus_amp_vref_0f())
3960 if (spec->keep_vref_in_automute)
3961 val = oldval & ~PIN_HP;
3966 /* here we call update_pin_ctl() so that the pinctl is changed
3967 * without changing the pinctl target value;
3968 * the original target value will be still referred at the
3969 * init / resume again
3971 update_pin_ctl(codec, nid, val);
3972 set_pin_eapd(codec, nid, !mute);
3977 * snd_hda_gen_update_outputs - Toggle outputs muting
3978 * @codec: the HDA codec
3980 * Update the mute status of all outputs based on the current jack states.
3982 void snd_hda_gen_update_outputs(struct hda_codec *codec)
3984 struct hda_gen_spec *spec = codec->spec;
3988 /* Control HP pins/amps depending on master_mute state;
3989 * in general, HP pins/amps control should be enabled in all cases,
3990 * but currently set only for master_mute, just to be safe
3992 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3993 paths = spec->out_paths;
3995 paths = spec->hp_paths;
3996 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3997 spec->autocfg.hp_pins, paths, spec->master_mute);
3999 if (!spec->automute_speaker)
4002 on = spec->hp_jack_present | spec->line_jack_present;
4003 on |= spec->master_mute;
4004 spec->speaker_muted = on;
4005 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4006 paths = spec->out_paths;
4008 paths = spec->speaker_paths;
4009 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
4010 spec->autocfg.speaker_pins, paths, on);
4012 /* toggle line-out mutes if needed, too */
4013 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4014 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4015 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4017 if (!spec->automute_lo)
4020 on = spec->hp_jack_present;
4021 on |= spec->master_mute;
4022 spec->line_out_muted = on;
4023 paths = spec->out_paths;
4024 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4025 spec->autocfg.line_out_pins, paths, on);
4027 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4029 static void call_update_outputs(struct hda_codec *codec)
4031 struct hda_gen_spec *spec = codec->spec;
4032 if (spec->automute_hook)
4033 spec->automute_hook(codec);
4035 snd_hda_gen_update_outputs(codec);
4037 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4038 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4039 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4043 * snd_hda_gen_hp_automute - standard HP-automute helper
4044 * @codec: the HDA codec
4045 * @jack: jack object, NULL for the whole
4047 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4048 struct hda_jack_callback *jack)
4050 struct hda_gen_spec *spec = codec->spec;
4051 hda_nid_t *pins = spec->autocfg.hp_pins;
4052 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4054 /* No detection for the first HP jack during indep-HP mode */
4055 if (spec->indep_hp_enabled) {
4060 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4061 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4063 call_update_outputs(codec);
4065 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4068 * snd_hda_gen_line_automute - standard line-out-automute helper
4069 * @codec: the HDA codec
4070 * @jack: jack object, NULL for the whole
4072 void snd_hda_gen_line_automute(struct hda_codec *codec,
4073 struct hda_jack_callback *jack)
4075 struct hda_gen_spec *spec = codec->spec;
4077 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4079 /* check LO jack only when it's different from HP */
4080 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4083 spec->line_jack_present =
4084 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4085 spec->autocfg.line_out_pins);
4086 if (!spec->automute_speaker || !spec->detect_lo)
4088 call_update_outputs(codec);
4090 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4093 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4094 * @codec: the HDA codec
4095 * @jack: jack object, NULL for the whole
4097 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4098 struct hda_jack_callback *jack)
4100 struct hda_gen_spec *spec = codec->spec;
4103 if (!spec->auto_mic)
4106 for (i = spec->am_num_entries - 1; i > 0; i--) {
4107 hda_nid_t pin = spec->am_entry[i].pin;
4108 /* don't detect pins retasked as outputs */
4109 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4111 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4112 mux_select(codec, 0, spec->am_entry[i].idx);
4116 mux_select(codec, 0, spec->am_entry[0].idx);
4118 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4120 /* call appropriate hooks */
4121 static void call_hp_automute(struct hda_codec *codec,
4122 struct hda_jack_callback *jack)
4124 struct hda_gen_spec *spec = codec->spec;
4125 if (spec->hp_automute_hook)
4126 spec->hp_automute_hook(codec, jack);
4128 snd_hda_gen_hp_automute(codec, jack);
4131 static void call_line_automute(struct hda_codec *codec,
4132 struct hda_jack_callback *jack)
4134 struct hda_gen_spec *spec = codec->spec;
4135 if (spec->line_automute_hook)
4136 spec->line_automute_hook(codec, jack);
4138 snd_hda_gen_line_automute(codec, jack);
4141 static void call_mic_autoswitch(struct hda_codec *codec,
4142 struct hda_jack_callback *jack)
4144 struct hda_gen_spec *spec = codec->spec;
4145 if (spec->mic_autoswitch_hook)
4146 spec->mic_autoswitch_hook(codec, jack);
4148 snd_hda_gen_mic_autoswitch(codec, jack);
4151 /* update jack retasking */
4152 static void update_automute_all(struct hda_codec *codec)
4154 call_hp_automute(codec, NULL);
4155 call_line_automute(codec, NULL);
4156 call_mic_autoswitch(codec, NULL);
4160 * Auto-Mute mode mixer enum support
4162 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4163 struct snd_ctl_elem_info *uinfo)
4165 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4166 struct hda_gen_spec *spec = codec->spec;
4167 static const char * const texts3[] = {
4168 "Disabled", "Speaker Only", "Line Out+Speaker"
4171 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4172 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4173 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4176 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4177 struct snd_ctl_elem_value *ucontrol)
4179 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4180 struct hda_gen_spec *spec = codec->spec;
4181 unsigned int val = 0;
4182 if (spec->automute_speaker)
4184 if (spec->automute_lo)
4187 ucontrol->value.enumerated.item[0] = val;
4191 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4192 struct snd_ctl_elem_value *ucontrol)
4194 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4195 struct hda_gen_spec *spec = codec->spec;
4197 switch (ucontrol->value.enumerated.item[0]) {
4199 if (!spec->automute_speaker && !spec->automute_lo)
4201 spec->automute_speaker = 0;
4202 spec->automute_lo = 0;
4205 if (spec->automute_speaker_possible) {
4206 if (!spec->automute_lo && spec->automute_speaker)
4208 spec->automute_speaker = 1;
4209 spec->automute_lo = 0;
4210 } else if (spec->automute_lo_possible) {
4211 if (spec->automute_lo)
4213 spec->automute_lo = 1;
4218 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4220 if (spec->automute_speaker && spec->automute_lo)
4222 spec->automute_speaker = 1;
4223 spec->automute_lo = 1;
4228 call_update_outputs(codec);
4232 static const struct snd_kcontrol_new automute_mode_enum = {
4233 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4234 .name = "Auto-Mute Mode",
4235 .info = automute_mode_info,
4236 .get = automute_mode_get,
4237 .put = automute_mode_put,
4240 static int add_automute_mode_enum(struct hda_codec *codec)
4242 struct hda_gen_spec *spec = codec->spec;
4244 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4250 * Check the availability of HP/line-out auto-mute;
4251 * Set up appropriately if really supported
4253 static int check_auto_mute_availability(struct hda_codec *codec)
4255 struct hda_gen_spec *spec = codec->spec;
4256 struct auto_pin_cfg *cfg = &spec->autocfg;
4260 if (spec->suppress_auto_mute)
4263 if (cfg->hp_pins[0])
4265 if (cfg->line_out_pins[0])
4267 if (cfg->speaker_pins[0])
4269 if (present < 2) /* need two different output types */
4272 if (!cfg->speaker_pins[0] &&
4273 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4274 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4275 sizeof(cfg->speaker_pins));
4276 cfg->speaker_outs = cfg->line_outs;
4279 if (!cfg->hp_pins[0] &&
4280 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4281 memcpy(cfg->hp_pins, cfg->line_out_pins,
4282 sizeof(cfg->hp_pins));
4283 cfg->hp_outs = cfg->line_outs;
4286 for (i = 0; i < cfg->hp_outs; i++) {
4287 hda_nid_t nid = cfg->hp_pins[i];
4288 if (!is_jack_detectable(codec, nid))
4290 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4291 snd_hda_jack_detect_enable_callback(codec, nid,
4293 spec->detect_hp = 1;
4296 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4297 if (cfg->speaker_outs)
4298 for (i = 0; i < cfg->line_outs; i++) {
4299 hda_nid_t nid = cfg->line_out_pins[i];
4300 if (!is_jack_detectable(codec, nid))
4302 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4303 snd_hda_jack_detect_enable_callback(codec, nid,
4304 call_line_automute);
4305 spec->detect_lo = 1;
4307 spec->automute_lo_possible = spec->detect_hp;
4310 spec->automute_speaker_possible = cfg->speaker_outs &&
4311 (spec->detect_hp || spec->detect_lo);
4313 spec->automute_lo = spec->automute_lo_possible;
4314 spec->automute_speaker = spec->automute_speaker_possible;
4316 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4317 /* create a control for automute mode */
4318 err = add_automute_mode_enum(codec);
4325 /* check whether all auto-mic pins are valid; setup indices if OK */
4326 static bool auto_mic_check_imux(struct hda_codec *codec)
4328 struct hda_gen_spec *spec = codec->spec;
4329 const struct hda_input_mux *imux;
4332 imux = &spec->input_mux;
4333 for (i = 0; i < spec->am_num_entries; i++) {
4334 spec->am_entry[i].idx =
4335 find_idx_in_nid_list(spec->am_entry[i].pin,
4336 spec->imux_pins, imux->num_items);
4337 if (spec->am_entry[i].idx < 0)
4338 return false; /* no corresponding imux */
4341 /* we don't need the jack detection for the first pin */
4342 for (i = 1; i < spec->am_num_entries; i++)
4343 snd_hda_jack_detect_enable_callback(codec,
4344 spec->am_entry[i].pin,
4345 call_mic_autoswitch);
4349 static int compare_attr(const void *ap, const void *bp)
4351 const struct automic_entry *a = ap;
4352 const struct automic_entry *b = bp;
4353 return (int)(a->attr - b->attr);
4357 * Check the availability of auto-mic switch;
4358 * Set up if really supported
4360 static int check_auto_mic_availability(struct hda_codec *codec)
4362 struct hda_gen_spec *spec = codec->spec;
4363 struct auto_pin_cfg *cfg = &spec->autocfg;
4367 if (spec->suppress_auto_mic)
4372 for (i = 0; i < cfg->num_inputs; i++) {
4373 hda_nid_t nid = cfg->inputs[i].pin;
4375 attr = snd_hda_codec_get_pincfg(codec, nid);
4376 attr = snd_hda_get_input_pin_attr(attr);
4377 if (types & (1 << attr))
4378 return 0; /* already occupied */
4380 case INPUT_PIN_ATTR_INT:
4381 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4382 return 0; /* invalid type */
4384 case INPUT_PIN_ATTR_UNUSED:
4385 return 0; /* invalid entry */
4387 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4388 return 0; /* invalid type */
4389 if (!spec->line_in_auto_switch &&
4390 cfg->inputs[i].type != AUTO_PIN_MIC)
4391 return 0; /* only mic is allowed */
4392 if (!is_jack_detectable(codec, nid))
4393 return 0; /* no unsol support */
4396 if (num_pins >= MAX_AUTO_MIC_PINS)
4398 types |= (1 << attr);
4399 spec->am_entry[num_pins].pin = nid;
4400 spec->am_entry[num_pins].attr = attr;
4407 spec->am_num_entries = num_pins;
4408 /* sort the am_entry in the order of attr so that the pin with a
4409 * higher attr will be selected when the jack is plugged.
4411 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4412 compare_attr, NULL);
4414 if (!auto_mic_check_imux(codec))
4418 spec->num_adc_nids = 1;
4419 spec->cur_mux[0] = spec->am_entry[0].idx;
4420 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4421 spec->am_entry[0].pin,
4422 spec->am_entry[1].pin,
4423 spec->am_entry[2].pin);
4429 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4431 * @codec: the HDA codec
4432 * @nid: NID to evalute
4433 * @power_state: target power state
4435 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4437 unsigned int power_state)
4439 if (power_state != AC_PWRST_D0 || nid == codec->afg)
4441 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4443 if (is_active_nid_for_any(codec, nid))
4447 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4449 /* mute all aamix inputs initially; parse up to the first leaves */
4450 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4453 const hda_nid_t *conn;
4456 nums = snd_hda_get_conn_list(codec, mix, &conn);
4457 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4458 for (i = 0; i < nums; i++) {
4460 update_amp(codec, mix, HDA_INPUT, i,
4461 0xff, HDA_AMP_MUTE);
4462 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4463 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4464 0xff, HDA_AMP_MUTE);
4469 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4470 * set up the hda_gen_spec
4471 * @codec: the HDA codec
4472 * @cfg: Parsed pin configuration
4474 * return 1 if successful, 0 if the proper config is not found,
4475 * or a negative error code
4477 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4478 struct auto_pin_cfg *cfg)
4480 struct hda_gen_spec *spec = codec->spec;
4483 parse_user_hints(codec);
4485 if (spec->mixer_nid && !spec->mixer_merge_nid)
4486 spec->mixer_merge_nid = spec->mixer_nid;
4488 if (cfg != &spec->autocfg) {
4489 spec->autocfg = *cfg;
4490 cfg = &spec->autocfg;
4493 if (!spec->main_out_badness)
4494 spec->main_out_badness = &hda_main_out_badness;
4495 if (!spec->extra_out_badness)
4496 spec->extra_out_badness = &hda_extra_out_badness;
4498 fill_all_dac_nids(codec);
4500 if (!cfg->line_outs) {
4501 if (cfg->dig_outs || cfg->dig_in_pin) {
4502 spec->multiout.max_channels = 2;
4503 spec->no_analog = 1;
4506 if (!cfg->num_inputs && !cfg->dig_in_pin)
4507 return 0; /* can't find valid BIOS pin config */
4510 if (!spec->no_primary_hp &&
4511 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4512 cfg->line_outs <= cfg->hp_outs) {
4513 /* use HP as primary out */
4514 cfg->speaker_outs = cfg->line_outs;
4515 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4516 sizeof(cfg->speaker_pins));
4517 cfg->line_outs = cfg->hp_outs;
4518 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4520 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4521 cfg->line_out_type = AUTO_PIN_HP_OUT;
4524 err = parse_output_paths(codec);
4527 err = create_multi_channel_mode(codec);
4530 err = create_multi_out_ctls(codec, cfg);
4533 err = create_hp_out_ctls(codec);
4536 err = create_speaker_out_ctls(codec);
4539 err = create_indep_hp_ctls(codec);
4542 err = create_loopback_mixing_ctl(codec);
4545 err = create_hp_mic(codec);
4548 err = create_input_ctls(codec);
4552 spec->const_channel_count = spec->ext_channel_count;
4553 /* check the multiple speaker and headphone pins */
4554 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4555 spec->const_channel_count = max(spec->const_channel_count,
4556 cfg->speaker_outs * 2);
4557 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4558 spec->const_channel_count = max(spec->const_channel_count,
4560 spec->multiout.max_channels = max(spec->ext_channel_count,
4561 spec->const_channel_count);
4563 err = check_auto_mute_availability(codec);
4567 err = check_dyn_adc_switch(codec);
4571 err = check_auto_mic_availability(codec);
4575 /* add stereo mix if available and not enabled yet */
4576 if (!spec->auto_mic && spec->mixer_nid &&
4577 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4578 spec->input_mux.num_items > 1) {
4579 err = parse_capture_source(codec, spec->mixer_nid,
4580 CFG_IDX_MIX, spec->num_all_adcs,
4587 err = create_capture_mixers(codec);
4591 err = parse_mic_boost(codec);
4595 /* create "Headphone Mic Jack Mode" if no input selection is
4596 * available (or user specifies add_jack_modes hint)
4598 if (spec->hp_mic_pin &&
4599 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4600 spec->add_jack_modes)) {
4601 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4606 if (spec->add_jack_modes) {
4607 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4608 err = create_out_jack_modes(codec, cfg->line_outs,
4609 cfg->line_out_pins);
4613 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4614 err = create_out_jack_modes(codec, cfg->hp_outs,
4621 /* mute all aamix input initially */
4622 if (spec->mixer_nid)
4623 mute_all_mixer_nid(codec, spec->mixer_nid);
4626 parse_digital(codec);
4628 if (spec->power_down_unused)
4629 codec->power_filter = snd_hda_gen_path_power_filter;
4631 if (!spec->no_analog && spec->beep_nid) {
4632 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4639 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
4643 * Build control elements
4646 /* slave controls for virtual master */
4647 static const char * const slave_pfxs[] = {
4648 "Front", "Surround", "Center", "LFE", "Side",
4649 "Headphone", "Speaker", "Mono", "Line Out",
4650 "CLFE", "Bass Speaker", "PCM",
4651 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4652 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4653 "Headphone Side", "Headphone+LO", "Speaker+LO",
4658 * snd_hda_gen_build_controls - Build controls from the parsed results
4659 * @codec: the HDA codec
4661 * Pass this to build_controls patch_ops.
4663 int snd_hda_gen_build_controls(struct hda_codec *codec)
4665 struct hda_gen_spec *spec = codec->spec;
4668 if (spec->kctls.used) {
4669 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4674 if (spec->multiout.dig_out_nid) {
4675 err = snd_hda_create_dig_out_ctls(codec,
4676 spec->multiout.dig_out_nid,
4677 spec->multiout.dig_out_nid,
4678 spec->pcm_rec[1].pcm_type);
4681 if (!spec->no_analog) {
4682 err = snd_hda_create_spdif_share_sw(codec,
4686 spec->multiout.share_spdif = 1;
4689 if (spec->dig_in_nid) {
4690 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4695 /* if we have no master control, let's create it */
4696 if (!spec->no_analog &&
4697 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
4698 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
4699 spec->vmaster_tlv, slave_pfxs,
4704 if (!spec->no_analog &&
4705 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4706 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4709 true, &spec->vmaster_mute.sw_kctl);
4712 if (spec->vmaster_mute.hook) {
4713 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4714 spec->vmaster_mute_enum);
4715 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4719 free_kctls(spec); /* no longer needed */
4721 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4727 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
4734 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4735 struct hda_codec *codec,
4736 struct snd_pcm_substream *substream,
4739 struct hda_gen_spec *spec = codec->spec;
4740 if (spec->pcm_playback_hook)
4741 spec->pcm_playback_hook(hinfo, codec, substream, action);
4744 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4745 struct hda_codec *codec,
4746 struct snd_pcm_substream *substream,
4749 struct hda_gen_spec *spec = codec->spec;
4750 if (spec->pcm_capture_hook)
4751 spec->pcm_capture_hook(hinfo, codec, substream, action);
4755 * Analog playback callbacks
4757 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4758 struct hda_codec *codec,
4759 struct snd_pcm_substream *substream)
4761 struct hda_gen_spec *spec = codec->spec;
4764 mutex_lock(&spec->pcm_mutex);
4765 err = snd_hda_multi_out_analog_open(codec,
4766 &spec->multiout, substream,
4769 spec->active_streams |= 1 << STREAM_MULTI_OUT;
4770 call_pcm_playback_hook(hinfo, codec, substream,
4771 HDA_GEN_PCM_ACT_OPEN);
4773 mutex_unlock(&spec->pcm_mutex);
4777 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4778 struct hda_codec *codec,
4779 unsigned int stream_tag,
4780 unsigned int format,
4781 struct snd_pcm_substream *substream)
4783 struct hda_gen_spec *spec = codec->spec;
4786 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4787 stream_tag, format, substream);
4789 call_pcm_playback_hook(hinfo, codec, substream,
4790 HDA_GEN_PCM_ACT_PREPARE);
4794 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4795 struct hda_codec *codec,
4796 struct snd_pcm_substream *substream)
4798 struct hda_gen_spec *spec = codec->spec;
4801 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4803 call_pcm_playback_hook(hinfo, codec, substream,
4804 HDA_GEN_PCM_ACT_CLEANUP);
4808 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4809 struct hda_codec *codec,
4810 struct snd_pcm_substream *substream)
4812 struct hda_gen_spec *spec = codec->spec;
4813 mutex_lock(&spec->pcm_mutex);
4814 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
4815 call_pcm_playback_hook(hinfo, codec, substream,
4816 HDA_GEN_PCM_ACT_CLOSE);
4817 mutex_unlock(&spec->pcm_mutex);
4821 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4822 struct hda_codec *codec,
4823 struct snd_pcm_substream *substream)
4825 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4829 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4830 struct hda_codec *codec,
4831 unsigned int stream_tag,
4832 unsigned int format,
4833 struct snd_pcm_substream *substream)
4835 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4836 call_pcm_capture_hook(hinfo, codec, substream,
4837 HDA_GEN_PCM_ACT_PREPARE);
4841 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4842 struct hda_codec *codec,
4843 struct snd_pcm_substream *substream)
4845 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4846 call_pcm_capture_hook(hinfo, codec, substream,
4847 HDA_GEN_PCM_ACT_CLEANUP);
4851 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4852 struct hda_codec *codec,
4853 struct snd_pcm_substream *substream)
4855 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4859 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4860 struct hda_codec *codec,
4861 struct snd_pcm_substream *substream)
4863 struct hda_gen_spec *spec = codec->spec;
4866 mutex_lock(&spec->pcm_mutex);
4867 if (!spec->indep_hp_enabled)
4870 spec->active_streams |= 1 << STREAM_INDEP_HP;
4871 call_pcm_playback_hook(hinfo, codec, substream,
4872 HDA_GEN_PCM_ACT_OPEN);
4873 mutex_unlock(&spec->pcm_mutex);
4877 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4878 struct hda_codec *codec,
4879 struct snd_pcm_substream *substream)
4881 struct hda_gen_spec *spec = codec->spec;
4882 mutex_lock(&spec->pcm_mutex);
4883 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
4884 call_pcm_playback_hook(hinfo, codec, substream,
4885 HDA_GEN_PCM_ACT_CLOSE);
4886 mutex_unlock(&spec->pcm_mutex);
4890 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4891 struct hda_codec *codec,
4892 unsigned int stream_tag,
4893 unsigned int format,
4894 struct snd_pcm_substream *substream)
4896 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4897 call_pcm_playback_hook(hinfo, codec, substream,
4898 HDA_GEN_PCM_ACT_PREPARE);
4902 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4903 struct hda_codec *codec,
4904 struct snd_pcm_substream *substream)
4906 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4907 call_pcm_playback_hook(hinfo, codec, substream,
4908 HDA_GEN_PCM_ACT_CLEANUP);
4915 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4916 struct hda_codec *codec,
4917 struct snd_pcm_substream *substream)
4919 struct hda_gen_spec *spec = codec->spec;
4920 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4923 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4924 struct hda_codec *codec,
4925 unsigned int stream_tag,
4926 unsigned int format,
4927 struct snd_pcm_substream *substream)
4929 struct hda_gen_spec *spec = codec->spec;
4930 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4931 stream_tag, format, substream);
4934 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4935 struct hda_codec *codec,
4936 struct snd_pcm_substream *substream)
4938 struct hda_gen_spec *spec = codec->spec;
4939 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4942 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4943 struct hda_codec *codec,
4944 struct snd_pcm_substream *substream)
4946 struct hda_gen_spec *spec = codec->spec;
4947 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4953 #define alt_capture_pcm_open capture_pcm_open
4954 #define alt_capture_pcm_close capture_pcm_close
4956 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4957 struct hda_codec *codec,
4958 unsigned int stream_tag,
4959 unsigned int format,
4960 struct snd_pcm_substream *substream)
4962 struct hda_gen_spec *spec = codec->spec;
4964 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
4965 stream_tag, 0, format);
4966 call_pcm_capture_hook(hinfo, codec, substream,
4967 HDA_GEN_PCM_ACT_PREPARE);
4971 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4972 struct hda_codec *codec,
4973 struct snd_pcm_substream *substream)
4975 struct hda_gen_spec *spec = codec->spec;
4977 snd_hda_codec_cleanup_stream(codec,
4978 spec->adc_nids[substream->number + 1]);
4979 call_pcm_capture_hook(hinfo, codec, substream,
4980 HDA_GEN_PCM_ACT_CLEANUP);
4986 static const struct hda_pcm_stream pcm_analog_playback = {
4990 /* NID is set in build_pcms */
4992 .open = playback_pcm_open,
4993 .close = playback_pcm_close,
4994 .prepare = playback_pcm_prepare,
4995 .cleanup = playback_pcm_cleanup
4999 static const struct hda_pcm_stream pcm_analog_capture = {
5003 /* NID is set in build_pcms */
5005 .open = capture_pcm_open,
5006 .close = capture_pcm_close,
5007 .prepare = capture_pcm_prepare,
5008 .cleanup = capture_pcm_cleanup
5012 static const struct hda_pcm_stream pcm_analog_alt_playback = {
5016 /* NID is set in build_pcms */
5018 .open = alt_playback_pcm_open,
5019 .close = alt_playback_pcm_close,
5020 .prepare = alt_playback_pcm_prepare,
5021 .cleanup = alt_playback_pcm_cleanup
5025 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5026 .substreams = 2, /* can be overridden */
5029 /* NID is set in build_pcms */
5031 .open = alt_capture_pcm_open,
5032 .close = alt_capture_pcm_close,
5033 .prepare = alt_capture_pcm_prepare,
5034 .cleanup = alt_capture_pcm_cleanup
5038 static const struct hda_pcm_stream pcm_digital_playback = {
5042 /* NID is set in build_pcms */
5044 .open = dig_playback_pcm_open,
5045 .close = dig_playback_pcm_close,
5046 .prepare = dig_playback_pcm_prepare,
5047 .cleanup = dig_playback_pcm_cleanup
5051 static const struct hda_pcm_stream pcm_digital_capture = {
5055 /* NID is set in build_pcms */
5058 /* Used by build_pcms to flag that a PCM has no playback stream */
5059 static const struct hda_pcm_stream pcm_null_stream = {
5066 * dynamic changing ADC PCM streams
5068 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5070 struct hda_gen_spec *spec = codec->spec;
5071 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5073 if (spec->cur_adc && spec->cur_adc != new_adc) {
5074 /* stream is running, let's swap the current ADC */
5075 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5076 spec->cur_adc = new_adc;
5077 snd_hda_codec_setup_stream(codec, new_adc,
5078 spec->cur_adc_stream_tag, 0,
5079 spec->cur_adc_format);
5085 /* analog capture with dynamic dual-adc changes */
5086 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5087 struct hda_codec *codec,
5088 unsigned int stream_tag,
5089 unsigned int format,
5090 struct snd_pcm_substream *substream)
5092 struct hda_gen_spec *spec = codec->spec;
5093 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5094 spec->cur_adc_stream_tag = stream_tag;
5095 spec->cur_adc_format = format;
5096 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5100 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5101 struct hda_codec *codec,
5102 struct snd_pcm_substream *substream)
5104 struct hda_gen_spec *spec = codec->spec;
5105 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5110 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5114 .nid = 0, /* fill later */
5116 .prepare = dyn_adc_capture_pcm_prepare,
5117 .cleanup = dyn_adc_capture_pcm_cleanup
5121 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5122 const char *chip_name)
5128 strlcpy(str, chip_name, len);
5130 /* drop non-alnum chars after a space */
5131 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5132 if (!isalnum(p[1])) {
5137 strlcat(str, sfx, len);
5141 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5142 * @codec: the HDA codec
5144 * Pass this to build_pcms patch_ops.
5146 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5148 struct hda_gen_spec *spec = codec->spec;
5149 struct hda_pcm *info = spec->pcm_rec;
5150 const struct hda_pcm_stream *p;
5151 bool have_multi_adcs;
5153 codec->num_pcms = 1;
5154 codec->pcm_info = info;
5156 if (spec->no_analog)
5159 fill_pcm_stream_name(spec->stream_name_analog,
5160 sizeof(spec->stream_name_analog),
5161 " Analog", codec->chip_name);
5162 info->name = spec->stream_name_analog;
5164 if (spec->multiout.num_dacs > 0) {
5165 p = spec->stream_analog_playback;
5167 p = &pcm_analog_playback;
5168 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5169 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
5170 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5171 spec->multiout.max_channels;
5172 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5173 spec->autocfg.line_outs == 2)
5174 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5177 if (spec->num_adc_nids) {
5178 p = spec->stream_analog_capture;
5180 if (spec->dyn_adc_switch)
5181 p = &dyn_adc_pcm_analog_capture;
5183 p = &pcm_analog_capture;
5185 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5186 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
5190 /* SPDIF for stream index #1 */
5191 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5192 fill_pcm_stream_name(spec->stream_name_digital,
5193 sizeof(spec->stream_name_digital),
5194 " Digital", codec->chip_name);
5195 codec->num_pcms = 2;
5196 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5197 info = spec->pcm_rec + 1;
5198 info->name = spec->stream_name_digital;
5199 if (spec->dig_out_type)
5200 info->pcm_type = spec->dig_out_type;
5202 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5203 if (spec->multiout.dig_out_nid) {
5204 p = spec->stream_digital_playback;
5206 p = &pcm_digital_playback;
5207 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5208 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
5210 if (spec->dig_in_nid) {
5211 p = spec->stream_digital_capture;
5213 p = &pcm_digital_capture;
5214 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5215 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
5219 if (spec->no_analog)
5222 /* If the use of more than one ADC is requested for the current
5223 * model, configure a second analog capture-only PCM.
5225 have_multi_adcs = (spec->num_adc_nids > 1) &&
5226 !spec->dyn_adc_switch && !spec->auto_mic;
5227 /* Additional Analaog capture for index #2 */
5228 if (spec->alt_dac_nid || have_multi_adcs) {
5229 fill_pcm_stream_name(spec->stream_name_alt_analog,
5230 sizeof(spec->stream_name_alt_analog),
5231 " Alt Analog", codec->chip_name);
5232 codec->num_pcms = 3;
5233 info = spec->pcm_rec + 2;
5234 info->name = spec->stream_name_alt_analog;
5235 if (spec->alt_dac_nid) {
5236 p = spec->stream_analog_alt_playback;
5238 p = &pcm_analog_alt_playback;
5239 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5240 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
5243 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
5245 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
5247 if (have_multi_adcs) {
5248 p = spec->stream_analog_alt_capture;
5250 p = &pcm_analog_alt_capture;
5251 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5252 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
5254 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5255 spec->num_adc_nids - 1;
5257 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
5259 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
5265 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5269 * Standard auto-parser initializations
5272 /* configure the given path as a proper output */
5273 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5275 struct nid_path *path;
5278 path = snd_hda_get_path_from_idx(codec, path_idx);
5279 if (!path || !path->depth)
5281 pin = path->path[path->depth - 1];
5282 restore_pin_ctl(codec, pin);
5283 snd_hda_activate_path(codec, path, path->active,
5284 aamix_default(codec->spec));
5285 set_pin_eapd(codec, pin, path->active);
5288 /* initialize primary output paths */
5289 static void init_multi_out(struct hda_codec *codec)
5291 struct hda_gen_spec *spec = codec->spec;
5294 for (i = 0; i < spec->autocfg.line_outs; i++)
5295 set_output_and_unmute(codec, spec->out_paths[i]);
5299 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5303 for (i = 0; i < num_outs; i++)
5304 set_output_and_unmute(codec, paths[i]);
5307 /* initialize hp and speaker paths */
5308 static void init_extra_out(struct hda_codec *codec)
5310 struct hda_gen_spec *spec = codec->spec;
5312 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5313 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5314 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5315 __init_extra_out(codec, spec->autocfg.speaker_outs,
5316 spec->speaker_paths);
5319 /* initialize multi-io paths */
5320 static void init_multi_io(struct hda_codec *codec)
5322 struct hda_gen_spec *spec = codec->spec;
5325 for (i = 0; i < spec->multi_ios; i++) {
5326 hda_nid_t pin = spec->multi_io[i].pin;
5327 struct nid_path *path;
5328 path = get_multiio_path(codec, i);
5331 if (!spec->multi_io[i].ctl_in)
5332 spec->multi_io[i].ctl_in =
5333 snd_hda_codec_get_pin_target(codec, pin);
5334 snd_hda_activate_path(codec, path, path->active,
5335 aamix_default(spec));
5339 static void init_aamix_paths(struct hda_codec *codec)
5341 struct hda_gen_spec *spec = codec->spec;
5343 if (!spec->have_aamix_ctl)
5345 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5346 spec->aamix_out_paths[0],
5347 spec->autocfg.line_out_type);
5348 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5349 spec->aamix_out_paths[1],
5351 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5352 spec->aamix_out_paths[2],
5353 AUTO_PIN_SPEAKER_OUT);
5356 /* set up input pins and loopback paths */
5357 static void init_analog_input(struct hda_codec *codec)
5359 struct hda_gen_spec *spec = codec->spec;
5360 struct auto_pin_cfg *cfg = &spec->autocfg;
5363 for (i = 0; i < cfg->num_inputs; i++) {
5364 hda_nid_t nid = cfg->inputs[i].pin;
5365 if (is_input_pin(codec, nid))
5366 restore_pin_ctl(codec, nid);
5368 /* init loopback inputs */
5369 if (spec->mixer_nid) {
5370 resume_path_from_idx(codec, spec->loopback_paths[i]);
5371 resume_path_from_idx(codec, spec->loopback_merge_path);
5376 /* initialize ADC paths */
5377 static void init_input_src(struct hda_codec *codec)
5379 struct hda_gen_spec *spec = codec->spec;
5380 struct hda_input_mux *imux = &spec->input_mux;
5381 struct nid_path *path;
5384 if (spec->dyn_adc_switch)
5387 nums = spec->num_adc_nids;
5389 for (c = 0; c < nums; c++) {
5390 for (i = 0; i < imux->num_items; i++) {
5391 path = get_input_path(codec, c, i);
5393 bool active = path->active;
5394 if (i == spec->cur_mux[c])
5396 snd_hda_activate_path(codec, path, active, false);
5400 update_hp_mic(codec, c, true);
5403 if (spec->cap_sync_hook)
5404 spec->cap_sync_hook(codec, NULL, NULL);
5407 /* set right pin controls for digital I/O */
5408 static void init_digital(struct hda_codec *codec)
5410 struct hda_gen_spec *spec = codec->spec;
5414 for (i = 0; i < spec->autocfg.dig_outs; i++)
5415 set_output_and_unmute(codec, spec->digout_paths[i]);
5416 pin = spec->autocfg.dig_in_pin;
5418 restore_pin_ctl(codec, pin);
5419 resume_path_from_idx(codec, spec->digin_path);
5423 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5424 * invalid unsol tags by some reason
5426 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5430 for (i = 0; i < codec->init_pins.used; i++) {
5431 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5432 hda_nid_t nid = pin->nid;
5433 if (is_jack_detectable(codec, nid) &&
5434 !snd_hda_jack_tbl_get(codec, nid))
5435 snd_hda_codec_update_cache(codec, nid, 0,
5436 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5441 * snd_hda_gen_init - initialize the generic spec
5442 * @codec: the HDA codec
5444 * This can be put as patch_ops init function.
5446 int snd_hda_gen_init(struct hda_codec *codec)
5448 struct hda_gen_spec *spec = codec->spec;
5450 if (spec->init_hook)
5451 spec->init_hook(codec);
5453 snd_hda_apply_verbs(codec);
5455 codec->cached_write = 1;
5457 init_multi_out(codec);
5458 init_extra_out(codec);
5459 init_multi_io(codec);
5460 init_aamix_paths(codec);
5461 init_analog_input(codec);
5462 init_input_src(codec);
5463 init_digital(codec);
5465 clear_unsol_on_unused_pins(codec);
5467 /* call init functions of standard auto-mute helpers */
5468 update_automute_all(codec);
5470 snd_hda_codec_flush_cache(codec);
5472 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5473 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5475 hda_call_check_power_status(codec, 0x01);
5478 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
5481 * snd_hda_gen_free - free the generic spec
5482 * @codec: the HDA codec
5484 * This can be put as patch_ops free function.
5486 void snd_hda_gen_free(struct hda_codec *codec)
5488 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
5489 snd_hda_gen_spec_free(codec->spec);
5493 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
5497 * snd_hda_gen_check_power_status - check the loopback power save state
5498 * @codec: the HDA codec
5499 * @nid: NID to inspect
5501 * This can be put as patch_ops check_power_status function.
5503 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5505 struct hda_gen_spec *spec = codec->spec;
5506 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5508 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
5513 * the generic codec support
5516 static const struct hda_codec_ops generic_patch_ops = {
5517 .build_controls = snd_hda_gen_build_controls,
5518 .build_pcms = snd_hda_gen_build_pcms,
5519 .init = snd_hda_gen_init,
5520 .free = snd_hda_gen_free,
5521 .unsol_event = snd_hda_jack_unsol_event,
5523 .check_power_status = snd_hda_gen_check_power_status,
5528 * snd_hda_parse_generic_codec - Generic codec parser
5529 * @codec: the HDA codec
5531 * This should be called from the HDA codec core.
5533 int snd_hda_parse_generic_codec(struct hda_codec *codec)
5535 struct hda_gen_spec *spec;
5538 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5541 snd_hda_gen_spec_init(spec);
5544 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5548 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5552 codec->patch_ops = generic_patch_ops;
5556 snd_hda_gen_free(codec);
5559 EXPORT_SYMBOL_GPL(snd_hda_parse_generic_codec);
5561 MODULE_LICENSE("GPL");
5562 MODULE_DESCRIPTION("Generic HD-audio codec parser");