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 /* initialize the amp value (only at the first time) */
691 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
693 unsigned int caps = query_amp_caps(codec, nid, dir);
694 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
696 if (get_wcaps(codec, nid) & AC_WCAP_STEREO)
697 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
699 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
702 /* update the amp, doing in stereo or mono depending on NID */
703 static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
704 unsigned int mask, unsigned int val)
706 if (get_wcaps(codec, nid) & AC_WCAP_STEREO)
707 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
710 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
714 /* calculate amp value mask we can modify;
715 * if the given amp is controlled by mixers, don't touch it
717 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
718 hda_nid_t nid, int dir, int idx,
721 unsigned int mask = 0xff;
723 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
724 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
727 if (caps & AC_AMPCAP_NUM_STEPS) {
728 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
729 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
735 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
736 int idx, int idx_to_check, bool enable)
739 unsigned int mask, val;
741 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
744 caps = query_amp_caps(codec, nid, dir);
745 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
746 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
751 update_amp(codec, nid, dir, idx, mask, val);
754 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
757 hda_nid_t nid = path->path[i];
758 init_amp(codec, nid, HDA_OUTPUT, 0);
759 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
762 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
763 int i, bool enable, bool add_aamix)
765 struct hda_gen_spec *spec = codec->spec;
766 const hda_nid_t *conn;
769 hda_nid_t nid = path->path[i];
771 nums = snd_hda_get_conn_list(codec, nid, &conn);
772 type = get_wcaps_type(get_wcaps(codec, nid));
773 if (type == AC_WID_PIN ||
774 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
780 for (n = 0; n < nums; n++)
781 init_amp(codec, nid, HDA_INPUT, n);
783 /* here is a little bit tricky in comparison with activate_amp_out();
784 * when aa-mixer is available, we need to enable the path as well
786 for (n = 0; n < nums; n++) {
787 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
789 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
794 * snd_hda_activate_path - activate or deactivate the given path
795 * @codec: the HDA codec
796 * @path: the path to activate/deactivate
797 * @enable: flag to activate or not
798 * @add_aamix: enable the input from aamix NID
800 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
802 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
803 bool enable, bool add_aamix)
805 struct hda_gen_spec *spec = codec->spec;
809 path->active = false;
811 for (i = path->depth - 1; i >= 0; i--) {
812 hda_nid_t nid = path->path[i];
813 if (enable && spec->power_down_unused) {
814 /* make sure the widget is powered up */
815 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
816 snd_hda_codec_write(codec, nid, 0,
817 AC_VERB_SET_POWER_STATE,
820 if (enable && path->multi[i])
821 snd_hda_codec_update_cache(codec, nid, 0,
822 AC_VERB_SET_CONNECT_SEL,
824 if (has_amp_in(codec, path, i))
825 activate_amp_in(codec, path, i, enable, add_aamix);
826 if (has_amp_out(codec, path, i))
827 activate_amp_out(codec, path, i, enable);
833 EXPORT_SYMBOL_GPL(snd_hda_activate_path);
835 /* if the given path is inactive, put widgets into D3 (only if suitable) */
836 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
838 struct hda_gen_spec *spec = codec->spec;
839 bool changed = false;
842 if (!spec->power_down_unused || path->active)
845 for (i = 0; i < path->depth; i++) {
846 hda_nid_t nid = path->path[i];
847 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
848 !is_active_nid_for_any(codec, nid)) {
849 snd_hda_codec_write(codec, nid, 0,
850 AC_VERB_SET_POWER_STATE,
858 snd_hda_codec_read(codec, path->path[0], 0,
859 AC_VERB_GET_POWER_STATE, 0);
863 /* turn on/off EAPD on the given pin */
864 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
866 struct hda_gen_spec *spec = codec->spec;
867 if (spec->own_eapd_ctl ||
868 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
870 if (spec->keep_eapd_on && !enable)
874 snd_hda_codec_update_cache(codec, pin, 0,
875 AC_VERB_SET_EAPD_BTLENABLE,
876 enable ? 0x02 : 0x00);
879 /* re-initialize the path specified by the given path index */
880 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
882 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
884 snd_hda_activate_path(codec, path, path->active, false);
889 * Helper functions for creating mixer ctl elements
892 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
893 struct snd_ctl_elem_value *ucontrol);
894 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
895 struct snd_ctl_elem_value *ucontrol);
902 static const struct snd_kcontrol_new control_templates[] = {
903 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
904 /* only the put callback is replaced for handling the special mute */
906 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
907 .subdevice = HDA_SUBDEV_AMP_FLAG,
908 .info = snd_hda_mixer_amp_switch_info,
909 .get = snd_hda_mixer_amp_switch_get,
910 .put = hda_gen_mixer_mute_put, /* replaced */
911 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
914 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
915 .info = snd_hda_mixer_amp_switch_info,
916 .get = snd_hda_mixer_bind_switch_get,
917 .put = hda_gen_bind_mute_put, /* replaced */
918 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
922 /* add dynamic controls from template */
923 static struct snd_kcontrol_new *
924 add_control(struct hda_gen_spec *spec, int type, const char *name,
925 int cidx, unsigned long val)
927 struct snd_kcontrol_new *knew;
929 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
933 if (get_amp_nid_(val))
934 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
935 knew->private_value = val;
939 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
940 const char *pfx, const char *dir,
941 const char *sfx, int cidx, unsigned long val)
943 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
944 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
945 if (!add_control(spec, type, name, cidx, val))
950 #define add_pb_vol_ctrl(spec, type, pfx, val) \
951 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
952 #define add_pb_sw_ctrl(spec, type, pfx, val) \
953 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
954 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
955 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
956 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
957 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
959 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
960 unsigned int chs, struct nid_path *path)
965 val = path->ctls[NID_PATH_VOL_CTL];
968 val = amp_val_replace_channels(val, chs);
969 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
972 /* return the channel bits suitable for the given path->ctls[] */
973 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
976 int chs = 1; /* mono (left only) */
978 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
979 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
980 chs = 3; /* stereo */
985 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
986 struct nid_path *path)
988 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
989 return add_vol_ctl(codec, pfx, cidx, chs, path);
992 /* create a mute-switch for the given mixer widget;
993 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
995 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
996 unsigned int chs, struct nid_path *path)
999 int type = HDA_CTL_WIDGET_MUTE;
1003 val = path->ctls[NID_PATH_MUTE_CTL];
1006 val = amp_val_replace_channels(val, chs);
1007 if (get_amp_direction_(val) == HDA_INPUT) {
1008 hda_nid_t nid = get_amp_nid_(val);
1009 int nums = snd_hda_get_num_conns(codec, nid);
1011 type = HDA_CTL_BIND_MUTE;
1015 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1018 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1019 int cidx, struct nid_path *path)
1021 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1022 return add_sw_ctl(codec, pfx, cidx, chs, path);
1025 /* playback mute control with the software mute bit check */
1026 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1027 struct snd_ctl_elem_value *ucontrol)
1029 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1030 struct hda_gen_spec *spec = codec->spec;
1032 if (spec->auto_mute_via_amp) {
1033 hda_nid_t nid = get_amp_nid(kcontrol);
1034 bool enabled = !((spec->mute_bits >> nid) & 1);
1035 ucontrol->value.integer.value[0] &= enabled;
1036 ucontrol->value.integer.value[1] &= enabled;
1040 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1041 struct snd_ctl_elem_value *ucontrol)
1043 sync_auto_mute_bits(kcontrol, ucontrol);
1044 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1047 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1048 struct snd_ctl_elem_value *ucontrol)
1050 sync_auto_mute_bits(kcontrol, ucontrol);
1051 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
1054 /* any ctl assigned to the path with the given index? */
1055 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1057 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1058 return path && path->ctls[ctl_type];
1061 static const char * const channel_name[4] = {
1062 "Front", "Surround", "CLFE", "Side"
1065 /* give some appropriate ctl name prefix for the given line out channel */
1066 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1067 int *index, int ctl_type)
1069 struct hda_gen_spec *spec = codec->spec;
1070 struct auto_pin_cfg *cfg = &spec->autocfg;
1073 if (cfg->line_outs == 1 && !spec->multi_ios &&
1074 !cfg->hp_outs && !cfg->speaker_outs)
1075 return spec->vmaster_mute.hook ? "PCM" : "Master";
1077 /* if there is really a single DAC used in the whole output paths,
1078 * use it master (or "PCM" if a vmaster hook is present)
1080 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1081 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1082 return spec->vmaster_mute.hook ? "PCM" : "Master";
1084 /* multi-io channels */
1085 if (ch >= cfg->line_outs)
1086 return channel_name[ch];
1088 switch (cfg->line_out_type) {
1089 case AUTO_PIN_SPEAKER_OUT:
1090 /* if the primary channel vol/mute is shared with HP volume,
1091 * don't name it as Speaker
1093 if (!ch && cfg->hp_outs &&
1094 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1096 if (cfg->line_outs == 1)
1098 if (cfg->line_outs == 2)
1099 return ch ? "Bass Speaker" : "Speaker";
1101 case AUTO_PIN_HP_OUT:
1102 /* if the primary channel vol/mute is shared with spk volume,
1103 * don't name it as Headphone
1105 if (!ch && cfg->speaker_outs &&
1106 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1108 /* for multi-io case, only the primary out */
1109 if (ch && spec->multi_ios)
1113 case AUTO_PIN_LINE_OUT:
1114 /* This deals with the case where we have two DACs and
1115 * one LO, one HP and one Speaker */
1116 if (!ch && cfg->speaker_outs && cfg->hp_outs) {
1117 bool hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1118 bool spk_lo_shared = !path_has_mixer(codec, spec->speaker_paths[0], ctl_type);
1119 if (hp_lo_shared && spk_lo_shared)
1120 return spec->vmaster_mute.hook ? "PCM" : "Master";
1122 return "Headphone+LO";
1124 return "Speaker+LO";
1128 /* for a single channel output, we don't have to name the channel */
1129 if (cfg->line_outs == 1 && !spec->multi_ios)
1132 if (ch >= ARRAY_SIZE(channel_name)) {
1137 return channel_name[ch];
1141 * Parse output paths
1144 /* badness definition */
1146 /* No primary DAC is found for the main output */
1147 BAD_NO_PRIMARY_DAC = 0x10000,
1148 /* No DAC is found for the extra output */
1149 BAD_NO_DAC = 0x4000,
1150 /* No possible multi-ios */
1151 BAD_MULTI_IO = 0x120,
1152 /* No individual DAC for extra output */
1153 BAD_NO_EXTRA_DAC = 0x102,
1154 /* No individual DAC for extra surrounds */
1155 BAD_NO_EXTRA_SURR_DAC = 0x101,
1156 /* Primary DAC shared with main surrounds */
1157 BAD_SHARED_SURROUND = 0x100,
1158 /* No independent HP possible */
1159 BAD_NO_INDEP_HP = 0x10,
1160 /* Primary DAC shared with main CLFE */
1161 BAD_SHARED_CLFE = 0x10,
1162 /* Primary DAC shared with extra surrounds */
1163 BAD_SHARED_EXTRA_SURROUND = 0x10,
1164 /* Volume widget is shared */
1165 BAD_SHARED_VOL = 0x10,
1168 /* look for widgets in the given path which are appropriate for
1169 * volume and mute controls, and assign the values to ctls[].
1171 * When no appropriate widget is found in the path, the badness value
1172 * is incremented depending on the situation. The function returns the
1173 * total badness for both volume and mute controls.
1175 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1177 struct hda_gen_spec *spec = codec->spec;
1183 return BAD_SHARED_VOL * 2;
1185 if (path->ctls[NID_PATH_VOL_CTL] ||
1186 path->ctls[NID_PATH_MUTE_CTL])
1187 return 0; /* already evaluated */
1189 nid = look_for_out_vol_nid(codec, path);
1191 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1192 if (spec->dac_min_mute)
1193 val |= HDA_AMP_VAL_MIN_MUTE;
1194 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1195 badness += BAD_SHARED_VOL;
1197 path->ctls[NID_PATH_VOL_CTL] = val;
1199 badness += BAD_SHARED_VOL;
1200 nid = look_for_out_mute_nid(codec, path);
1202 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1203 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1204 nid_has_mute(codec, nid, HDA_OUTPUT))
1205 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1207 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1208 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1209 badness += BAD_SHARED_VOL;
1211 path->ctls[NID_PATH_MUTE_CTL] = val;
1213 badness += BAD_SHARED_VOL;
1217 const struct badness_table hda_main_out_badness = {
1218 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1219 .no_dac = BAD_NO_DAC,
1220 .shared_primary = BAD_NO_PRIMARY_DAC,
1221 .shared_surr = BAD_SHARED_SURROUND,
1222 .shared_clfe = BAD_SHARED_CLFE,
1223 .shared_surr_main = BAD_SHARED_SURROUND,
1225 EXPORT_SYMBOL_GPL(hda_main_out_badness);
1227 const struct badness_table hda_extra_out_badness = {
1228 .no_primary_dac = BAD_NO_DAC,
1229 .no_dac = BAD_NO_DAC,
1230 .shared_primary = BAD_NO_EXTRA_DAC,
1231 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1232 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1233 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1235 EXPORT_SYMBOL_GPL(hda_extra_out_badness);
1237 /* get the DAC of the primary output corresponding to the given array index */
1238 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1240 struct hda_gen_spec *spec = codec->spec;
1241 struct auto_pin_cfg *cfg = &spec->autocfg;
1243 if (cfg->line_outs > idx)
1244 return spec->private_dac_nids[idx];
1245 idx -= cfg->line_outs;
1246 if (spec->multi_ios > idx)
1247 return spec->multi_io[idx].dac;
1251 /* return the DAC if it's reachable, otherwise zero */
1252 static inline hda_nid_t try_dac(struct hda_codec *codec,
1253 hda_nid_t dac, hda_nid_t pin)
1255 return is_reachable_path(codec, dac, pin) ? dac : 0;
1258 /* try to assign DACs to pins and return the resultant badness */
1259 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1260 const hda_nid_t *pins, hda_nid_t *dacs,
1262 const struct badness_table *bad)
1264 struct hda_gen_spec *spec = codec->spec;
1272 for (i = 0; i < num_outs; i++) {
1273 struct nid_path *path;
1274 hda_nid_t pin = pins[i];
1276 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1278 badness += assign_out_path_ctls(codec, path);
1282 dacs[i] = get_preferred_dac(codec, pin);
1284 if (is_dac_already_used(codec, dacs[i]))
1285 badness += bad->shared_primary;
1289 dacs[i] = look_for_dac(codec, pin, false);
1290 if (!dacs[i] && !i) {
1291 /* try to steal the DAC of surrounds for the front */
1292 for (j = 1; j < num_outs; j++) {
1293 if (is_reachable_path(codec, dacs[j], pin)) {
1296 invalidate_nid_path(codec, path_idx[j]);
1305 dac = try_dac(codec, get_primary_out(codec, i), pin);
1307 dac = try_dac(codec, dacs[0], pin);
1309 dac = try_dac(codec, get_primary_out(codec, i), pin);
1312 badness += bad->shared_primary;
1314 badness += bad->shared_surr;
1316 badness += bad->shared_clfe;
1317 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1318 dac = spec->private_dac_nids[0];
1319 badness += bad->shared_surr_main;
1321 badness += bad->no_primary_dac;
1323 badness += bad->no_dac;
1327 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1328 if (!path && !i && spec->mixer_nid) {
1329 /* try with aamix */
1330 path = snd_hda_add_new_path(codec, dac, pin, 0);
1334 badness += bad->no_dac;
1336 /* print_nid_path(codec, "output", path); */
1337 path->active = true;
1338 path_idx[i] = snd_hda_get_path_idx(codec, path);
1339 badness += assign_out_path_ctls(codec, path);
1346 /* return NID if the given pin has only a single connection to a certain DAC */
1347 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1349 struct hda_gen_spec *spec = codec->spec;
1351 hda_nid_t nid_found = 0;
1353 for (i = 0; i < spec->num_all_dacs; i++) {
1354 hda_nid_t nid = spec->all_dacs[i];
1355 if (!nid || is_dac_already_used(codec, nid))
1357 if (is_reachable_path(codec, nid, pin)) {
1366 /* check whether the given pin can be a multi-io pin */
1367 static bool can_be_multiio_pin(struct hda_codec *codec,
1368 unsigned int location, hda_nid_t nid)
1370 unsigned int defcfg, caps;
1372 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1373 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1375 if (location && get_defcfg_location(defcfg) != location)
1377 caps = snd_hda_query_pin_caps(codec, nid);
1378 if (!(caps & AC_PINCAP_OUT))
1383 /* count the number of input pins that are capable to be multi-io */
1384 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1386 struct hda_gen_spec *spec = codec->spec;
1387 struct auto_pin_cfg *cfg = &spec->autocfg;
1388 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1389 unsigned int location = get_defcfg_location(defcfg);
1393 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1394 for (i = 0; i < cfg->num_inputs; i++) {
1395 if (cfg->inputs[i].type != type)
1397 if (can_be_multiio_pin(codec, location,
1398 cfg->inputs[i].pin))
1408 * When hardwired is set, try to fill ony hardwired pins, and returns
1409 * zero if any pins are filled, non-zero if nothing found.
1410 * When hardwired is off, try to fill possible input pins, and returns
1411 * the badness value.
1413 static int fill_multi_ios(struct hda_codec *codec,
1414 hda_nid_t reference_pin,
1417 struct hda_gen_spec *spec = codec->spec;
1418 struct auto_pin_cfg *cfg = &spec->autocfg;
1419 int type, i, j, num_pins, old_pins;
1420 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1421 unsigned int location = get_defcfg_location(defcfg);
1423 struct nid_path *path;
1425 old_pins = spec->multi_ios;
1429 num_pins = count_multiio_pins(codec, reference_pin);
1433 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1434 for (i = 0; i < cfg->num_inputs; i++) {
1435 hda_nid_t nid = cfg->inputs[i].pin;
1438 if (cfg->inputs[i].type != type)
1440 if (!can_be_multiio_pin(codec, location, nid))
1442 for (j = 0; j < spec->multi_ios; j++) {
1443 if (nid == spec->multi_io[j].pin)
1446 if (j < spec->multi_ios)
1450 dac = get_dac_if_single(codec, nid);
1452 dac = look_for_dac(codec, nid, false);
1457 path = snd_hda_add_new_path(codec, dac, nid,
1463 /* print_nid_path(codec, "multiio", path); */
1464 spec->multi_io[spec->multi_ios].pin = nid;
1465 spec->multi_io[spec->multi_ios].dac = dac;
1466 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1467 snd_hda_get_path_idx(codec, path);
1469 if (spec->multi_ios >= 2)
1475 badness = BAD_MULTI_IO;
1476 if (old_pins == spec->multi_ios) {
1478 return 1; /* nothing found */
1480 return badness; /* no badness if nothing found */
1482 if (!hardwired && spec->multi_ios < 2) {
1483 /* cancel newly assigned paths */
1484 spec->paths.used -= spec->multi_ios - old_pins;
1485 spec->multi_ios = old_pins;
1489 /* assign volume and mute controls */
1490 for (i = old_pins; i < spec->multi_ios; i++) {
1491 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1492 badness += assign_out_path_ctls(codec, path);
1498 /* map DACs for all pins in the list if they are single connections */
1499 static bool map_singles(struct hda_codec *codec, int outs,
1500 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1502 struct hda_gen_spec *spec = codec->spec;
1505 for (i = 0; i < outs; i++) {
1506 struct nid_path *path;
1510 dac = get_dac_if_single(codec, pins[i]);
1513 path = snd_hda_add_new_path(codec, dac, pins[i],
1515 if (!path && !i && spec->mixer_nid)
1516 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1520 /* print_nid_path(codec, "output", path); */
1521 path->active = true;
1522 path_idx[i] = snd_hda_get_path_idx(codec, path);
1528 /* create a new path including aamix if available, and return its index */
1529 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1531 struct hda_gen_spec *spec = codec->spec;
1532 struct nid_path *path;
1533 hda_nid_t path_dac, dac, pin;
1535 path = snd_hda_get_path_from_idx(codec, path_idx);
1536 if (!path || !path->depth ||
1537 is_nid_contained(path, spec->mixer_nid))
1539 path_dac = path->path[0];
1540 dac = spec->private_dac_nids[0];
1541 pin = path->path[path->depth - 1];
1542 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1544 if (dac != path_dac)
1546 else if (spec->multiout.hp_out_nid[0])
1547 dac = spec->multiout.hp_out_nid[0];
1548 else if (spec->multiout.extra_out_nid[0])
1549 dac = spec->multiout.extra_out_nid[0];
1553 path = snd_hda_add_new_path(codec, dac, pin,
1558 /* print_nid_path(codec, "output-aamix", path); */
1559 path->active = false; /* unused as default */
1560 return snd_hda_get_path_idx(codec, path);
1563 /* check whether the independent HP is available with the current config */
1564 static bool indep_hp_possible(struct hda_codec *codec)
1566 struct hda_gen_spec *spec = codec->spec;
1567 struct auto_pin_cfg *cfg = &spec->autocfg;
1568 struct nid_path *path;
1571 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1572 idx = spec->out_paths[0];
1574 idx = spec->hp_paths[0];
1575 path = snd_hda_get_path_from_idx(codec, idx);
1579 /* assume no path conflicts unless aamix is involved */
1580 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1583 /* check whether output paths contain aamix */
1584 for (i = 0; i < cfg->line_outs; i++) {
1585 if (spec->out_paths[i] == idx)
1587 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1588 if (path && is_nid_contained(path, spec->mixer_nid))
1591 for (i = 0; i < cfg->speaker_outs; i++) {
1592 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1593 if (path && is_nid_contained(path, spec->mixer_nid))
1600 /* fill the empty entries in the dac array for speaker/hp with the
1601 * shared dac pointed by the paths
1603 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1604 hda_nid_t *dacs, int *path_idx)
1606 struct nid_path *path;
1609 for (i = 0; i < num_outs; i++) {
1612 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1615 dacs[i] = path->path[0];
1619 /* fill in the dac_nids table from the parsed pin configuration */
1620 static int fill_and_eval_dacs(struct hda_codec *codec,
1621 bool fill_hardwired,
1622 bool fill_mio_first)
1624 struct hda_gen_spec *spec = codec->spec;
1625 struct auto_pin_cfg *cfg = &spec->autocfg;
1626 int i, err, badness;
1628 /* set num_dacs once to full for look_for_dac() */
1629 spec->multiout.num_dacs = cfg->line_outs;
1630 spec->multiout.dac_nids = spec->private_dac_nids;
1631 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1632 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1633 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1634 spec->multi_ios = 0;
1635 snd_array_free(&spec->paths);
1637 /* clear path indices */
1638 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1639 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1640 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1641 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1642 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1643 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1644 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1645 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1649 /* fill hard-wired DACs first */
1650 if (fill_hardwired) {
1653 mapped = map_singles(codec, cfg->line_outs,
1655 spec->private_dac_nids,
1657 mapped |= map_singles(codec, cfg->hp_outs,
1659 spec->multiout.hp_out_nid,
1661 mapped |= map_singles(codec, cfg->speaker_outs,
1663 spec->multiout.extra_out_nid,
1664 spec->speaker_paths);
1665 if (!spec->no_multi_io &&
1666 fill_mio_first && cfg->line_outs == 1 &&
1667 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1668 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1675 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1676 spec->private_dac_nids, spec->out_paths,
1677 spec->main_out_badness);
1679 if (!spec->no_multi_io && fill_mio_first &&
1680 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1681 /* try to fill multi-io first */
1682 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1685 /* we don't count badness at this stage yet */
1688 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1689 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1690 spec->multiout.hp_out_nid,
1692 spec->extra_out_badness);
1697 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1698 err = try_assign_dacs(codec, cfg->speaker_outs,
1700 spec->multiout.extra_out_nid,
1701 spec->speaker_paths,
1702 spec->extra_out_badness);
1707 if (!spec->no_multi_io &&
1708 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1709 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1715 if (spec->mixer_nid) {
1716 spec->aamix_out_paths[0] =
1717 check_aamix_out_path(codec, spec->out_paths[0]);
1718 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1719 spec->aamix_out_paths[1] =
1720 check_aamix_out_path(codec, spec->hp_paths[0]);
1721 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1722 spec->aamix_out_paths[2] =
1723 check_aamix_out_path(codec, spec->speaker_paths[0]);
1726 if (!spec->no_multi_io &&
1727 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1728 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1729 spec->multi_ios = 1; /* give badness */
1731 /* re-count num_dacs and squash invalid entries */
1732 spec->multiout.num_dacs = 0;
1733 for (i = 0; i < cfg->line_outs; i++) {
1734 if (spec->private_dac_nids[i])
1735 spec->multiout.num_dacs++;
1737 memmove(spec->private_dac_nids + i,
1738 spec->private_dac_nids + i + 1,
1739 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1740 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1744 spec->ext_channel_count = spec->min_channel_count =
1745 spec->multiout.num_dacs * 2;
1747 if (spec->multi_ios == 2) {
1748 for (i = 0; i < 2; i++)
1749 spec->private_dac_nids[spec->multiout.num_dacs++] =
1750 spec->multi_io[i].dac;
1751 } else if (spec->multi_ios) {
1752 spec->multi_ios = 0;
1753 badness += BAD_MULTI_IO;
1756 if (spec->indep_hp && !indep_hp_possible(codec))
1757 badness += BAD_NO_INDEP_HP;
1759 /* re-fill the shared DAC for speaker / headphone */
1760 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1761 refill_shared_dacs(codec, cfg->hp_outs,
1762 spec->multiout.hp_out_nid,
1764 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1765 refill_shared_dacs(codec, cfg->speaker_outs,
1766 spec->multiout.extra_out_nid,
1767 spec->speaker_paths);
1772 #define DEBUG_BADNESS
1774 #ifdef DEBUG_BADNESS
1775 #define debug_badness(fmt, ...) \
1776 codec_dbg(codec, fmt, ##__VA_ARGS__)
1778 #define debug_badness(fmt, ...) \
1779 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1782 #ifdef DEBUG_BADNESS
1783 static inline void print_nid_path_idx(struct hda_codec *codec,
1784 const char *pfx, int idx)
1786 struct nid_path *path;
1788 path = snd_hda_get_path_from_idx(codec, idx);
1790 print_nid_path(codec, pfx, path);
1793 static void debug_show_configs(struct hda_codec *codec,
1794 struct auto_pin_cfg *cfg)
1796 struct hda_gen_spec *spec = codec->spec;
1797 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1800 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1801 cfg->line_out_pins[0], cfg->line_out_pins[1],
1802 cfg->line_out_pins[2], cfg->line_out_pins[3],
1803 spec->multiout.dac_nids[0],
1804 spec->multiout.dac_nids[1],
1805 spec->multiout.dac_nids[2],
1806 spec->multiout.dac_nids[3],
1807 lo_type[cfg->line_out_type]);
1808 for (i = 0; i < cfg->line_outs; i++)
1809 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1810 if (spec->multi_ios > 0)
1811 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1813 spec->multi_io[0].pin, spec->multi_io[1].pin,
1814 spec->multi_io[0].dac, spec->multi_io[1].dac);
1815 for (i = 0; i < spec->multi_ios; i++)
1816 print_nid_path_idx(codec, " mio",
1817 spec->out_paths[cfg->line_outs + i]);
1819 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1820 cfg->hp_pins[0], cfg->hp_pins[1],
1821 cfg->hp_pins[2], cfg->hp_pins[3],
1822 spec->multiout.hp_out_nid[0],
1823 spec->multiout.hp_out_nid[1],
1824 spec->multiout.hp_out_nid[2],
1825 spec->multiout.hp_out_nid[3]);
1826 for (i = 0; i < cfg->hp_outs; i++)
1827 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1828 if (cfg->speaker_outs)
1829 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1830 cfg->speaker_pins[0], cfg->speaker_pins[1],
1831 cfg->speaker_pins[2], cfg->speaker_pins[3],
1832 spec->multiout.extra_out_nid[0],
1833 spec->multiout.extra_out_nid[1],
1834 spec->multiout.extra_out_nid[2],
1835 spec->multiout.extra_out_nid[3]);
1836 for (i = 0; i < cfg->speaker_outs; i++)
1837 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1838 for (i = 0; i < 3; i++)
1839 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1842 #define debug_show_configs(codec, cfg) /* NOP */
1845 /* find all available DACs of the codec */
1846 static void fill_all_dac_nids(struct hda_codec *codec)
1848 struct hda_gen_spec *spec = codec->spec;
1850 hda_nid_t nid = codec->start_nid;
1852 spec->num_all_dacs = 0;
1853 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1854 for (i = 0; i < codec->num_nodes; i++, nid++) {
1855 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1857 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1858 codec_err(codec, "Too many DACs!\n");
1861 spec->all_dacs[spec->num_all_dacs++] = nid;
1865 static int parse_output_paths(struct hda_codec *codec)
1867 struct hda_gen_spec *spec = codec->spec;
1868 struct auto_pin_cfg *cfg = &spec->autocfg;
1869 struct auto_pin_cfg *best_cfg;
1871 int best_badness = INT_MAX;
1873 bool fill_hardwired = true, fill_mio_first = true;
1874 bool best_wired = true, best_mio = true;
1875 bool hp_spk_swapped = false;
1877 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1883 badness = fill_and_eval_dacs(codec, fill_hardwired,
1889 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1890 cfg->line_out_type, fill_hardwired, fill_mio_first,
1892 debug_show_configs(codec, cfg);
1893 if (badness < best_badness) {
1894 best_badness = badness;
1896 best_wired = fill_hardwired;
1897 best_mio = fill_mio_first;
1901 fill_mio_first = !fill_mio_first;
1902 if (!fill_mio_first)
1904 fill_hardwired = !fill_hardwired;
1905 if (!fill_hardwired)
1909 hp_spk_swapped = true;
1910 if (cfg->speaker_outs > 0 &&
1911 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1912 cfg->hp_outs = cfg->line_outs;
1913 memcpy(cfg->hp_pins, cfg->line_out_pins,
1914 sizeof(cfg->hp_pins));
1915 cfg->line_outs = cfg->speaker_outs;
1916 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1917 sizeof(cfg->speaker_pins));
1918 cfg->speaker_outs = 0;
1919 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1920 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1921 fill_hardwired = true;
1924 if (cfg->hp_outs > 0 &&
1925 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1926 cfg->speaker_outs = cfg->line_outs;
1927 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1928 sizeof(cfg->speaker_pins));
1929 cfg->line_outs = cfg->hp_outs;
1930 memcpy(cfg->line_out_pins, cfg->hp_pins,
1931 sizeof(cfg->hp_pins));
1933 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1934 cfg->line_out_type = AUTO_PIN_HP_OUT;
1935 fill_hardwired = true;
1942 debug_badness("==> restoring best_cfg\n");
1944 fill_and_eval_dacs(codec, best_wired, best_mio);
1946 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1947 cfg->line_out_type, best_wired, best_mio);
1948 debug_show_configs(codec, cfg);
1950 if (cfg->line_out_pins[0]) {
1951 struct nid_path *path;
1952 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1954 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1955 if (spec->vmaster_nid) {
1956 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1957 HDA_OUTPUT, spec->vmaster_tlv);
1958 if (spec->dac_min_mute)
1959 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
1963 /* set initial pinctl targets */
1964 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1968 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1969 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1970 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1971 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1972 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1973 set_pin_targets(codec, cfg->speaker_outs,
1974 cfg->speaker_pins, val);
1977 /* clear indep_hp flag if not available */
1978 if (spec->indep_hp && !indep_hp_possible(codec))
1985 /* add playback controls from the parsed DAC table */
1986 static int create_multi_out_ctls(struct hda_codec *codec,
1987 const struct auto_pin_cfg *cfg)
1989 struct hda_gen_spec *spec = codec->spec;
1990 int i, err, noutputs;
1992 noutputs = cfg->line_outs;
1993 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1994 noutputs += spec->multi_ios;
1996 for (i = 0; i < noutputs; i++) {
1999 struct nid_path *path;
2001 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
2005 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
2006 if (!name || !strcmp(name, "CLFE")) {
2008 err = add_vol_ctl(codec, "Center", 0, 1, path);
2011 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2015 err = add_stereo_vol(codec, name, index, path);
2020 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2021 if (!name || !strcmp(name, "CLFE")) {
2022 err = add_sw_ctl(codec, "Center", 0, 1, path);
2025 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2029 err = add_stereo_sw(codec, name, index, path);
2037 static int create_extra_out(struct hda_codec *codec, int path_idx,
2038 const char *pfx, int cidx)
2040 struct nid_path *path;
2043 path = snd_hda_get_path_from_idx(codec, path_idx);
2046 err = add_stereo_vol(codec, pfx, cidx, path);
2049 err = add_stereo_sw(codec, pfx, cidx, path);
2055 /* add playback controls for speaker and HP outputs */
2056 static int create_extra_outs(struct hda_codec *codec, int num_pins,
2057 const int *paths, const char *pfx)
2061 for (i = 0; i < num_pins; i++) {
2063 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2066 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2067 name = "Bass Speaker";
2068 else if (num_pins >= 3) {
2069 snprintf(tmp, sizeof(tmp), "%s %s",
2070 pfx, channel_name[i]);
2076 err = create_extra_out(codec, paths[i], name, idx);
2083 static int create_hp_out_ctls(struct hda_codec *codec)
2085 struct hda_gen_spec *spec = codec->spec;
2086 return create_extra_outs(codec, spec->autocfg.hp_outs,
2091 static int create_speaker_out_ctls(struct hda_codec *codec)
2093 struct hda_gen_spec *spec = codec->spec;
2094 return create_extra_outs(codec, spec->autocfg.speaker_outs,
2095 spec->speaker_paths,
2100 * independent HP controls
2103 static void call_hp_automute(struct hda_codec *codec,
2104 struct hda_jack_callback *jack);
2105 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2106 struct snd_ctl_elem_info *uinfo)
2108 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2111 static int indep_hp_get(struct snd_kcontrol *kcontrol,
2112 struct snd_ctl_elem_value *ucontrol)
2114 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2115 struct hda_gen_spec *spec = codec->spec;
2116 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2120 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2121 int nomix_path_idx, int mix_path_idx,
2124 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2125 struct snd_ctl_elem_value *ucontrol)
2127 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2128 struct hda_gen_spec *spec = codec->spec;
2129 unsigned int select = ucontrol->value.enumerated.item[0];
2132 mutex_lock(&spec->pcm_mutex);
2133 if (spec->active_streams) {
2138 if (spec->indep_hp_enabled != select) {
2140 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2141 dacp = &spec->private_dac_nids[0];
2143 dacp = &spec->multiout.hp_out_nid[0];
2145 /* update HP aamix paths in case it conflicts with indep HP */
2146 if (spec->have_aamix_ctl) {
2147 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2148 update_aamix_paths(codec, spec->aamix_mode,
2150 spec->aamix_out_paths[0],
2151 spec->autocfg.line_out_type);
2153 update_aamix_paths(codec, spec->aamix_mode,
2155 spec->aamix_out_paths[1],
2159 spec->indep_hp_enabled = select;
2160 if (spec->indep_hp_enabled)
2163 *dacp = spec->alt_dac_nid;
2165 call_hp_automute(codec, NULL);
2169 mutex_unlock(&spec->pcm_mutex);
2173 static const struct snd_kcontrol_new indep_hp_ctl = {
2174 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2175 .name = "Independent HP",
2176 .info = indep_hp_info,
2177 .get = indep_hp_get,
2178 .put = indep_hp_put,
2182 static int create_indep_hp_ctls(struct hda_codec *codec)
2184 struct hda_gen_spec *spec = codec->spec;
2187 if (!spec->indep_hp)
2189 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2190 dac = spec->multiout.dac_nids[0];
2192 dac = spec->multiout.hp_out_nid[0];
2198 spec->indep_hp_enabled = false;
2199 spec->alt_dac_nid = dac;
2200 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2206 * channel mode enum control
2209 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2210 struct snd_ctl_elem_info *uinfo)
2212 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2213 struct hda_gen_spec *spec = codec->spec;
2216 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2218 uinfo->value.enumerated.items = spec->multi_ios + 1;
2219 if (uinfo->value.enumerated.item > spec->multi_ios)
2220 uinfo->value.enumerated.item = spec->multi_ios;
2221 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2222 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2226 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2227 struct snd_ctl_elem_value *ucontrol)
2229 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2230 struct hda_gen_spec *spec = codec->spec;
2231 ucontrol->value.enumerated.item[0] =
2232 (spec->ext_channel_count - spec->min_channel_count) / 2;
2236 static inline struct nid_path *
2237 get_multiio_path(struct hda_codec *codec, int idx)
2239 struct hda_gen_spec *spec = codec->spec;
2240 return snd_hda_get_path_from_idx(codec,
2241 spec->out_paths[spec->autocfg.line_outs + idx]);
2244 static void update_automute_all(struct hda_codec *codec);
2246 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2247 * used for output paths
2249 static bool aamix_default(struct hda_gen_spec *spec)
2251 return !spec->have_aamix_ctl || spec->aamix_mode;
2254 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2256 struct hda_gen_spec *spec = codec->spec;
2257 hda_nid_t nid = spec->multi_io[idx].pin;
2258 struct nid_path *path;
2260 path = get_multiio_path(codec, idx);
2264 if (path->active == output)
2268 set_pin_target(codec, nid, PIN_OUT, true);
2269 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2270 set_pin_eapd(codec, nid, true);
2272 set_pin_eapd(codec, nid, false);
2273 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2274 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2275 path_power_down_sync(codec, path);
2278 /* update jack retasking in case it modifies any of them */
2279 update_automute_all(codec);
2284 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2285 struct snd_ctl_elem_value *ucontrol)
2287 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2288 struct hda_gen_spec *spec = codec->spec;
2291 ch = ucontrol->value.enumerated.item[0];
2292 if (ch < 0 || ch > spec->multi_ios)
2294 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2296 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2297 for (i = 0; i < spec->multi_ios; i++)
2298 set_multi_io(codec, i, i < ch);
2299 spec->multiout.max_channels = max(spec->ext_channel_count,
2300 spec->const_channel_count);
2301 if (spec->need_dac_fix)
2302 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2306 static const struct snd_kcontrol_new channel_mode_enum = {
2307 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2308 .name = "Channel Mode",
2309 .info = ch_mode_info,
2314 static int create_multi_channel_mode(struct hda_codec *codec)
2316 struct hda_gen_spec *spec = codec->spec;
2318 if (spec->multi_ios > 0) {
2319 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2326 * aamix loopback enable/disable switch
2329 #define loopback_mixing_info indep_hp_info
2331 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2332 struct snd_ctl_elem_value *ucontrol)
2334 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2335 struct hda_gen_spec *spec = codec->spec;
2336 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2340 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2341 int nomix_path_idx, int mix_path_idx,
2344 struct hda_gen_spec *spec = codec->spec;
2345 struct nid_path *nomix_path, *mix_path;
2347 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2348 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2349 if (!nomix_path || !mix_path)
2352 /* if HP aamix path is driven from a different DAC and the
2353 * independent HP mode is ON, can't turn on aamix path
2355 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2356 mix_path->path[0] != spec->alt_dac_nid)
2360 snd_hda_activate_path(codec, nomix_path, false, true);
2361 snd_hda_activate_path(codec, mix_path, true, true);
2362 path_power_down_sync(codec, nomix_path);
2364 snd_hda_activate_path(codec, mix_path, false, false);
2365 snd_hda_activate_path(codec, nomix_path, true, false);
2366 path_power_down_sync(codec, mix_path);
2370 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2371 struct snd_ctl_elem_value *ucontrol)
2373 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2374 struct hda_gen_spec *spec = codec->spec;
2375 unsigned int val = ucontrol->value.enumerated.item[0];
2377 if (val == spec->aamix_mode)
2379 spec->aamix_mode = val;
2380 update_aamix_paths(codec, val, spec->out_paths[0],
2381 spec->aamix_out_paths[0],
2382 spec->autocfg.line_out_type);
2383 update_aamix_paths(codec, val, spec->hp_paths[0],
2384 spec->aamix_out_paths[1],
2386 update_aamix_paths(codec, val, spec->speaker_paths[0],
2387 spec->aamix_out_paths[2],
2388 AUTO_PIN_SPEAKER_OUT);
2392 static const struct snd_kcontrol_new loopback_mixing_enum = {
2393 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2394 .name = "Loopback Mixing",
2395 .info = loopback_mixing_info,
2396 .get = loopback_mixing_get,
2397 .put = loopback_mixing_put,
2400 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2402 struct hda_gen_spec *spec = codec->spec;
2404 if (!spec->mixer_nid)
2406 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2407 spec->aamix_out_paths[2]))
2409 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2411 spec->have_aamix_ctl = 1;
2416 * shared headphone/mic handling
2419 static void call_update_outputs(struct hda_codec *codec);
2421 /* for shared I/O, change the pin-control accordingly */
2422 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2424 struct hda_gen_spec *spec = codec->spec;
2429 pin = spec->hp_mic_pin;
2430 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2433 val = snd_hda_codec_get_pin_target(codec, pin);
2443 val = snd_hda_get_default_vref(codec, pin);
2444 /* if the HP pin doesn't support VREF and the codec driver gives an
2445 * alternative pin, set up the VREF on that pin instead
2447 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2448 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2449 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2450 if (vref_val != AC_PINCTL_VREF_HIZ)
2451 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2452 PIN_IN | (as_mic ? vref_val : 0));
2455 if (!spec->hp_mic_jack_modes) {
2460 set_pin_target(codec, pin, val, true);
2461 call_hp_automute(codec, NULL);
2465 /* create a shared input with the headphone out */
2466 static int create_hp_mic(struct hda_codec *codec)
2468 struct hda_gen_spec *spec = codec->spec;
2469 struct auto_pin_cfg *cfg = &spec->autocfg;
2470 unsigned int defcfg;
2473 if (!spec->hp_mic) {
2474 if (spec->suppress_hp_mic_detect)
2476 /* automatic detection: only if no input or a single internal
2477 * input pin is found, try to detect the shared hp/mic
2479 if (cfg->num_inputs > 1)
2481 else if (cfg->num_inputs == 1) {
2482 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2483 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2488 spec->hp_mic = 0; /* clear once */
2489 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2493 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2494 nid = cfg->line_out_pins[0];
2495 else if (cfg->hp_outs > 0)
2496 nid = cfg->hp_pins[0];
2500 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2501 return 0; /* no input */
2503 cfg->inputs[cfg->num_inputs].pin = nid;
2504 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2505 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2508 spec->hp_mic_pin = nid;
2509 /* we can't handle auto-mic together with HP-mic */
2510 spec->suppress_auto_mic = 1;
2511 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
2519 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2521 static const char * const out_jack_texts[] = {
2522 "Line Out", "Headphone Out",
2525 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2526 struct snd_ctl_elem_info *uinfo)
2528 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2531 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2532 struct snd_ctl_elem_value *ucontrol)
2534 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2535 hda_nid_t nid = kcontrol->private_value;
2536 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2537 ucontrol->value.enumerated.item[0] = 1;
2539 ucontrol->value.enumerated.item[0] = 0;
2543 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2544 struct snd_ctl_elem_value *ucontrol)
2546 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2547 hda_nid_t nid = kcontrol->private_value;
2550 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2551 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2553 snd_hda_set_pin_ctl_cache(codec, nid, val);
2557 static const struct snd_kcontrol_new out_jack_mode_enum = {
2558 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2559 .info = out_jack_mode_info,
2560 .get = out_jack_mode_get,
2561 .put = out_jack_mode_put,
2564 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2566 struct hda_gen_spec *spec = codec->spec;
2569 for (i = 0; i < spec->kctls.used; i++) {
2570 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2571 if (!strcmp(kctl->name, name) && kctl->index == idx)
2577 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2578 char *name, size_t name_len)
2580 struct hda_gen_spec *spec = codec->spec;
2583 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2584 strlcat(name, " Jack Mode", name_len);
2586 for (; find_kctl_name(codec, name, idx); idx++)
2590 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2592 struct hda_gen_spec *spec = codec->spec;
2593 if (spec->add_jack_modes) {
2594 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2595 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2601 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2604 struct hda_gen_spec *spec = codec->spec;
2607 for (i = 0; i < num_pins; i++) {
2608 hda_nid_t pin = pins[i];
2609 if (pin == spec->hp_mic_pin)
2611 if (get_out_jack_num_items(codec, pin) > 1) {
2612 struct snd_kcontrol_new *knew;
2613 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2614 get_jack_mode_name(codec, pin, name, sizeof(name));
2615 knew = snd_hda_gen_add_kctl(spec, name,
2616 &out_jack_mode_enum);
2619 knew->private_value = pin;
2630 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2633 static const char * const vref_texts[NUM_VREFS] = {
2634 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2635 "", "Mic 80pc Bias", "Mic 100pc Bias"
2638 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2640 unsigned int pincap;
2642 pincap = snd_hda_query_pin_caps(codec, pin);
2643 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2644 /* filter out unusual vrefs */
2645 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2649 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2650 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2652 unsigned int i, n = 0;
2654 for (i = 0; i < NUM_VREFS; i++) {
2655 if (vref_caps & (1 << i)) {
2664 /* convert back from the vref ctl index to the enum item index */
2665 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2667 unsigned int i, n = 0;
2669 for (i = 0; i < NUM_VREFS; i++) {
2672 if (vref_caps & (1 << i))
2678 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2679 struct snd_ctl_elem_info *uinfo)
2681 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2682 hda_nid_t nid = kcontrol->private_value;
2683 unsigned int vref_caps = get_vref_caps(codec, nid);
2685 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2687 /* set the right text */
2688 strcpy(uinfo->value.enumerated.name,
2689 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2693 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2694 struct snd_ctl_elem_value *ucontrol)
2696 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2697 hda_nid_t nid = kcontrol->private_value;
2698 unsigned int vref_caps = get_vref_caps(codec, nid);
2701 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2702 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2706 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2707 struct snd_ctl_elem_value *ucontrol)
2709 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2710 hda_nid_t nid = kcontrol->private_value;
2711 unsigned int vref_caps = get_vref_caps(codec, nid);
2712 unsigned int val, idx;
2714 val = snd_hda_codec_get_pin_target(codec, nid);
2715 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2716 if (idx == ucontrol->value.enumerated.item[0])
2719 val &= ~AC_PINCTL_VREFEN;
2720 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2721 snd_hda_set_pin_ctl_cache(codec, nid, val);
2725 static const struct snd_kcontrol_new in_jack_mode_enum = {
2726 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2727 .info = in_jack_mode_info,
2728 .get = in_jack_mode_get,
2729 .put = in_jack_mode_put,
2732 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2734 struct hda_gen_spec *spec = codec->spec;
2736 if (spec->add_jack_modes)
2737 nitems = hweight32(get_vref_caps(codec, pin));
2738 return nitems ? nitems : 1;
2741 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2743 struct hda_gen_spec *spec = codec->spec;
2744 struct snd_kcontrol_new *knew;
2745 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2746 unsigned int defcfg;
2748 if (pin == spec->hp_mic_pin)
2749 return 0; /* already done in create_out_jack_mode() */
2751 /* no jack mode for fixed pins */
2752 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2753 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2756 /* no multiple vref caps? */
2757 if (get_in_jack_num_items(codec, pin) <= 1)
2760 get_jack_mode_name(codec, pin, name, sizeof(name));
2761 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2764 knew->private_value = pin;
2769 * HP/mic shared jack mode
2771 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2772 struct snd_ctl_elem_info *uinfo)
2774 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2775 hda_nid_t nid = kcontrol->private_value;
2776 int out_jacks = get_out_jack_num_items(codec, nid);
2777 int in_jacks = get_in_jack_num_items(codec, nid);
2778 const char *text = NULL;
2781 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2783 uinfo->value.enumerated.items = out_jacks + in_jacks;
2784 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2785 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2786 idx = uinfo->value.enumerated.item;
2787 if (idx < out_jacks) {
2789 text = out_jack_texts[idx];
2791 text = "Headphone Out";
2795 unsigned int vref_caps = get_vref_caps(codec, nid);
2796 text = vref_texts[get_vref_idx(vref_caps, idx)];
2801 strcpy(uinfo->value.enumerated.name, text);
2805 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2807 int out_jacks = get_out_jack_num_items(codec, nid);
2808 int in_jacks = get_in_jack_num_items(codec, nid);
2809 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2812 if (val & PIN_OUT) {
2813 if (out_jacks > 1 && val == PIN_HP)
2815 } else if (val & PIN_IN) {
2818 unsigned int vref_caps = get_vref_caps(codec, nid);
2819 val &= AC_PINCTL_VREFEN;
2820 idx += cvt_from_vref_idx(vref_caps, val);
2826 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2827 struct snd_ctl_elem_value *ucontrol)
2829 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2830 hda_nid_t nid = kcontrol->private_value;
2831 ucontrol->value.enumerated.item[0] =
2832 get_cur_hp_mic_jack_mode(codec, nid);
2836 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2837 struct snd_ctl_elem_value *ucontrol)
2839 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2840 hda_nid_t nid = kcontrol->private_value;
2841 int out_jacks = get_out_jack_num_items(codec, nid);
2842 int in_jacks = get_in_jack_num_items(codec, nid);
2843 unsigned int val, oldval, idx;
2845 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2846 idx = ucontrol->value.enumerated.item[0];
2850 if (idx < out_jacks) {
2852 val = idx ? PIN_HP : PIN_OUT;
2858 unsigned int vref_caps = get_vref_caps(codec, nid);
2859 val = snd_hda_codec_get_pin_target(codec, nid);
2860 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2861 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
2863 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
2865 snd_hda_set_pin_ctl_cache(codec, nid, val);
2866 call_hp_automute(codec, NULL);
2871 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2872 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2873 .info = hp_mic_jack_mode_info,
2874 .get = hp_mic_jack_mode_get,
2875 .put = hp_mic_jack_mode_put,
2878 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2880 struct hda_gen_spec *spec = codec->spec;
2881 struct snd_kcontrol_new *knew;
2883 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2884 &hp_mic_jack_mode_enum);
2887 knew->private_value = pin;
2888 spec->hp_mic_jack_modes = 1;
2896 /* add the powersave loopback-list entry */
2897 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2899 struct hda_amp_list *list;
2901 list = snd_array_new(&spec->loopback_list);
2905 list->dir = HDA_INPUT;
2907 spec->loopback.amplist = spec->loopback_list.list;
2911 /* return true if either a volume or a mute amp is found for the given
2912 * aamix path; the amp has to be either in the mixer node or its direct leaf
2914 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2915 hda_nid_t pin, unsigned int *mix_val,
2916 unsigned int *mute_val)
2919 const hda_nid_t *list;
2922 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2926 *mix_val = *mute_val = 0;
2927 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2928 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2929 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2930 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2931 if (*mix_val && *mute_val)
2934 /* check leaf node */
2935 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2936 if (num_conns < idx)
2939 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
2940 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
2941 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2942 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
2943 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
2944 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2946 return *mix_val || *mute_val;
2949 /* create input playback/capture controls for the given pin */
2950 static int new_analog_input(struct hda_codec *codec, int input_idx,
2951 hda_nid_t pin, const char *ctlname, int ctlidx,
2954 struct hda_gen_spec *spec = codec->spec;
2955 struct nid_path *path;
2956 unsigned int mix_val, mute_val;
2959 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
2962 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
2965 print_nid_path(codec, "loopback", path);
2966 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
2968 idx = path->idx[path->depth - 1];
2970 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
2973 path->ctls[NID_PATH_VOL_CTL] = mix_val;
2977 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
2980 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
2983 path->active = true;
2984 err = add_loopback_list(spec, mix_nid, idx);
2988 if (spec->mixer_nid != spec->mixer_merge_nid &&
2989 !spec->loopback_merge_path) {
2990 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2991 spec->mixer_merge_nid, 0);
2993 print_nid_path(codec, "loopback-merge", path);
2994 path->active = true;
2995 spec->loopback_merge_path =
2996 snd_hda_get_path_idx(codec, path);
3003 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3005 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3006 return (pincap & AC_PINCAP_IN) != 0;
3009 /* Parse the codec tree and retrieve ADCs */
3010 static int fill_adc_nids(struct hda_codec *codec)
3012 struct hda_gen_spec *spec = codec->spec;
3014 hda_nid_t *adc_nids = spec->adc_nids;
3015 int max_nums = ARRAY_SIZE(spec->adc_nids);
3018 nid = codec->start_nid;
3019 for (i = 0; i < codec->num_nodes; i++, nid++) {
3020 unsigned int caps = get_wcaps(codec, nid);
3021 int type = get_wcaps_type(caps);
3023 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3025 adc_nids[nums] = nid;
3026 if (++nums >= max_nums)
3029 spec->num_adc_nids = nums;
3031 /* copy the detected ADCs to all_adcs[] */
3032 spec->num_all_adcs = nums;
3033 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3038 /* filter out invalid adc_nids that don't give all active input pins;
3039 * if needed, check whether dynamic ADC-switching is available
3041 static int check_dyn_adc_switch(struct hda_codec *codec)
3043 struct hda_gen_spec *spec = codec->spec;
3044 struct hda_input_mux *imux = &spec->input_mux;
3045 unsigned int ok_bits;
3050 for (n = 0; n < spec->num_adc_nids; n++) {
3051 for (i = 0; i < imux->num_items; i++) {
3052 if (!spec->input_paths[i][n])
3055 if (i >= imux->num_items) {
3056 ok_bits |= (1 << n);
3062 /* check whether ADC-switch is possible */
3063 for (i = 0; i < imux->num_items; i++) {
3064 for (n = 0; n < spec->num_adc_nids; n++) {
3065 if (spec->input_paths[i][n]) {
3066 spec->dyn_adc_idx[i] = n;
3072 codec_dbg(codec, "enabling ADC switching\n");
3073 spec->dyn_adc_switch = 1;
3074 } else if (nums != spec->num_adc_nids) {
3075 /* shrink the invalid adcs and input paths */
3077 for (n = 0; n < spec->num_adc_nids; n++) {
3078 if (!(ok_bits & (1 << n)))
3081 spec->adc_nids[nums] = spec->adc_nids[n];
3082 for (i = 0; i < imux->num_items; i++) {
3083 invalidate_nid_path(codec,
3084 spec->input_paths[i][nums]);
3085 spec->input_paths[i][nums] =
3086 spec->input_paths[i][n];
3091 spec->num_adc_nids = nums;
3094 if (imux->num_items == 1 ||
3095 (imux->num_items == 2 && spec->hp_mic)) {
3096 codec_dbg(codec, "reducing to a single ADC\n");
3097 spec->num_adc_nids = 1; /* reduce to a single ADC */
3100 /* single index for individual volumes ctls */
3101 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3102 spec->num_adc_nids = 1;
3107 /* parse capture source paths from the given pin and create imux items */
3108 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3109 int cfg_idx, int num_adcs,
3110 const char *label, int anchor)
3112 struct hda_gen_spec *spec = codec->spec;
3113 struct hda_input_mux *imux = &spec->input_mux;
3114 int imux_idx = imux->num_items;
3115 bool imux_added = false;
3118 for (c = 0; c < num_adcs; c++) {
3119 struct nid_path *path;
3120 hda_nid_t adc = spec->adc_nids[c];
3122 if (!is_reachable_path(codec, pin, adc))
3124 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3127 print_nid_path(codec, "input", path);
3128 spec->input_paths[imux_idx][c] =
3129 snd_hda_get_path_idx(codec, path);
3132 if (spec->hp_mic_pin == pin)
3133 spec->hp_mic_mux_idx = imux->num_items;
3134 spec->imux_pins[imux->num_items] = pin;
3135 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3137 if (spec->dyn_adc_switch)
3138 spec->dyn_adc_idx[imux_idx] = c;
3146 * create playback/capture controls for input pins
3149 /* fill the label for each input at first */
3150 static int fill_input_pin_labels(struct hda_codec *codec)
3152 struct hda_gen_spec *spec = codec->spec;
3153 const struct auto_pin_cfg *cfg = &spec->autocfg;
3156 for (i = 0; i < cfg->num_inputs; i++) {
3157 hda_nid_t pin = cfg->inputs[i].pin;
3161 if (!is_input_pin(codec, pin))
3164 label = hda_get_autocfg_input_label(codec, cfg, i);
3166 for (j = i - 1; j >= 0; j--) {
3167 if (spec->input_labels[j] &&
3168 !strcmp(spec->input_labels[j], label)) {
3169 idx = spec->input_label_idxs[j] + 1;
3174 spec->input_labels[i] = label;
3175 spec->input_label_idxs[i] = idx;
3181 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3183 static int create_input_ctls(struct hda_codec *codec)
3185 struct hda_gen_spec *spec = codec->spec;
3186 const struct auto_pin_cfg *cfg = &spec->autocfg;
3187 hda_nid_t mixer = spec->mixer_nid;
3192 num_adcs = fill_adc_nids(codec);
3196 err = fill_input_pin_labels(codec);
3200 for (i = 0; i < cfg->num_inputs; i++) {
3203 pin = cfg->inputs[i].pin;
3204 if (!is_input_pin(codec, pin))
3208 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3209 val |= snd_hda_get_default_vref(codec, pin);
3210 if (pin != spec->hp_mic_pin)
3211 set_pin_target(codec, pin, val, false);
3214 if (is_reachable_path(codec, pin, mixer)) {
3215 err = new_analog_input(codec, i, pin,
3216 spec->input_labels[i],
3217 spec->input_label_idxs[i],
3224 err = parse_capture_source(codec, pin, i, num_adcs,
3225 spec->input_labels[i], -mixer);
3229 if (spec->add_jack_modes) {
3230 err = create_in_jack_mode(codec, pin);
3236 /* add stereo mix when explicitly enabled via hint */
3237 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3238 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3243 spec->suppress_auto_mic = 1;
3254 /* get the input path specified by the given adc and imux indices */
3255 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3257 struct hda_gen_spec *spec = codec->spec;
3258 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3262 if (spec->dyn_adc_switch)
3263 adc_idx = spec->dyn_adc_idx[imux_idx];
3264 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3268 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3271 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3274 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3275 struct snd_ctl_elem_info *uinfo)
3277 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3278 struct hda_gen_spec *spec = codec->spec;
3279 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3282 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3283 struct snd_ctl_elem_value *ucontrol)
3285 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3286 struct hda_gen_spec *spec = codec->spec;
3287 /* the ctls are created at once with multiple counts */
3288 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3290 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3294 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3295 struct snd_ctl_elem_value *ucontrol)
3297 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3298 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3299 return mux_select(codec, adc_idx,
3300 ucontrol->value.enumerated.item[0]);
3303 static const struct snd_kcontrol_new cap_src_temp = {
3304 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3305 .name = "Input Source",
3306 .info = mux_enum_info,
3307 .get = mux_enum_get,
3308 .put = mux_enum_put,
3312 * capture volume and capture switch ctls
3315 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3316 struct snd_ctl_elem_value *ucontrol);
3318 /* call the given amp update function for all amps in the imux list at once */
3319 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3320 struct snd_ctl_elem_value *ucontrol,
3321 put_call_t func, int type)
3323 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3324 struct hda_gen_spec *spec = codec->spec;
3325 const struct hda_input_mux *imux;
3326 struct nid_path *path;
3327 int i, adc_idx, err = 0;
3329 imux = &spec->input_mux;
3330 adc_idx = kcontrol->id.index;
3331 mutex_lock(&codec->control_mutex);
3332 /* we use the cache-only update at first since multiple input paths
3333 * may shared the same amp; by updating only caches, the redundant
3334 * writes to hardware can be reduced.
3336 codec->cached_write = 1;
3337 for (i = 0; i < imux->num_items; i++) {
3338 path = get_input_path(codec, adc_idx, i);
3339 if (!path || !path->ctls[type])
3341 kcontrol->private_value = path->ctls[type];
3342 err = func(kcontrol, ucontrol);
3347 codec->cached_write = 0;
3348 mutex_unlock(&codec->control_mutex);
3349 snd_hda_codec_flush_cache(codec); /* flush the updates */
3350 if (err >= 0 && spec->cap_sync_hook)
3351 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3355 /* capture volume ctl callbacks */
3356 #define cap_vol_info snd_hda_mixer_amp_volume_info
3357 #define cap_vol_get snd_hda_mixer_amp_volume_get
3358 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3360 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3361 struct snd_ctl_elem_value *ucontrol)
3363 return cap_put_caller(kcontrol, ucontrol,
3364 snd_hda_mixer_amp_volume_put,
3368 static const struct snd_kcontrol_new cap_vol_temp = {
3369 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3370 .name = "Capture Volume",
3371 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3372 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3373 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3374 .info = cap_vol_info,
3377 .tlv = { .c = cap_vol_tlv },
3380 /* capture switch ctl callbacks */
3381 #define cap_sw_info snd_ctl_boolean_stereo_info
3382 #define cap_sw_get snd_hda_mixer_amp_switch_get
3384 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3385 struct snd_ctl_elem_value *ucontrol)
3387 return cap_put_caller(kcontrol, ucontrol,
3388 snd_hda_mixer_amp_switch_put,
3392 static const struct snd_kcontrol_new cap_sw_temp = {
3393 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3394 .name = "Capture Switch",
3395 .info = cap_sw_info,
3400 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3405 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3406 for (depth = 0; depth < 3; depth++) {
3407 if (depth >= path->depth)
3409 i = path->depth - depth - 1;
3410 nid = path->path[i];
3411 if (!path->ctls[NID_PATH_VOL_CTL]) {
3412 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3413 path->ctls[NID_PATH_VOL_CTL] =
3414 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3415 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3416 int idx = path->idx[i];
3417 if (!depth && codec->single_adc_amp)
3419 path->ctls[NID_PATH_VOL_CTL] =
3420 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3423 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3424 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3425 path->ctls[NID_PATH_MUTE_CTL] =
3426 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3427 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3428 int idx = path->idx[i];
3429 if (!depth && codec->single_adc_amp)
3431 path->ctls[NID_PATH_MUTE_CTL] =
3432 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3439 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3441 struct hda_gen_spec *spec = codec->spec;
3442 struct auto_pin_cfg *cfg = &spec->autocfg;
3446 if (!spec->inv_dmic_split)
3448 for (i = 0; i < cfg->num_inputs; i++) {
3449 if (cfg->inputs[i].pin != nid)
3451 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3453 val = snd_hda_codec_get_pincfg(codec, nid);
3454 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3459 /* capture switch put callback for a single control with hook call */
3460 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3461 struct snd_ctl_elem_value *ucontrol)
3463 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3464 struct hda_gen_spec *spec = codec->spec;
3467 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3471 if (spec->cap_sync_hook)
3472 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3477 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3478 int idx, bool is_switch, unsigned int ctl,
3481 struct hda_gen_spec *spec = codec->spec;
3482 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3483 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3484 const char *sfx = is_switch ? "Switch" : "Volume";
3485 unsigned int chs = inv_dmic ? 1 : 3;
3486 struct snd_kcontrol_new *knew;
3492 snprintf(tmpname, sizeof(tmpname),
3493 "%s Capture %s", label, sfx);
3495 snprintf(tmpname, sizeof(tmpname),
3497 knew = add_control(spec, type, tmpname, idx,
3498 amp_val_replace_channels(ctl, chs));
3502 knew->put = cap_single_sw_put;
3506 /* Make independent right kcontrol */
3508 snprintf(tmpname, sizeof(tmpname),
3509 "Inverted %s Capture %s", label, sfx);
3511 snprintf(tmpname, sizeof(tmpname),
3512 "Inverted Capture %s", sfx);
3513 knew = add_control(spec, type, tmpname, idx,
3514 amp_val_replace_channels(ctl, 2));
3518 knew->put = cap_single_sw_put;
3522 /* create single (and simple) capture volume and switch controls */
3523 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3524 unsigned int vol_ctl, unsigned int sw_ctl,
3528 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3531 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3537 /* create bound capture volume and switch controls */
3538 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3539 unsigned int vol_ctl, unsigned int sw_ctl)
3541 struct hda_gen_spec *spec = codec->spec;
3542 struct snd_kcontrol_new *knew;
3545 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3549 knew->private_value = vol_ctl;
3550 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3553 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3557 knew->private_value = sw_ctl;
3558 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3563 /* return the vol ctl when used first in the imux list */
3564 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3566 struct nid_path *path;
3570 path = get_input_path(codec, 0, idx);
3573 ctl = path->ctls[type];
3576 for (i = 0; i < idx - 1; i++) {
3577 path = get_input_path(codec, 0, i);
3578 if (path && path->ctls[type] == ctl)
3584 /* create individual capture volume and switch controls per input */
3585 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3587 struct hda_gen_spec *spec = codec->spec;
3588 struct hda_input_mux *imux = &spec->input_mux;
3591 for (i = 0; i < imux->num_items; i++) {
3595 idx = imux->items[i].index;
3596 if (idx >= spec->autocfg.num_inputs)
3598 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3600 for (type = 0; type < 2; type++) {
3601 err = add_single_cap_ctl(codec,
3602 spec->input_labels[idx],
3603 spec->input_label_idxs[idx],
3605 get_first_cap_ctl(codec, i, type),
3614 static int create_capture_mixers(struct hda_codec *codec)
3616 struct hda_gen_spec *spec = codec->spec;
3617 struct hda_input_mux *imux = &spec->input_mux;
3618 int i, n, nums, err;
3620 if (spec->dyn_adc_switch)
3623 nums = spec->num_adc_nids;
3625 if (!spec->auto_mic && imux->num_items > 1) {
3626 struct snd_kcontrol_new *knew;
3628 name = nums > 1 ? "Input Source" : "Capture Source";
3629 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3635 for (n = 0; n < nums; n++) {
3637 bool multi_cap_vol = spec->multi_cap_vol;
3638 bool inv_dmic = false;
3642 for (i = 0; i < imux->num_items; i++) {
3643 struct nid_path *path;
3644 path = get_input_path(codec, n, i);
3647 parse_capvol_in_path(codec, path);
3649 vol = path->ctls[NID_PATH_VOL_CTL];
3650 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3652 if (!same_amp_caps(codec, vol,
3653 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3654 multi_cap_vol = true;
3657 sw = path->ctls[NID_PATH_MUTE_CTL];
3658 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3660 if (!same_amp_caps(codec, sw,
3661 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3662 multi_cap_vol = true;
3664 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3669 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3671 else if (!multi_cap_vol && !inv_dmic)
3672 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3674 err = create_multi_cap_vol_ctl(codec);
3683 * add mic boosts if needed
3686 /* check whether the given amp is feasible as a boost volume */
3687 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3692 if (!nid_has_volume(codec, nid, dir) ||
3693 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3694 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3697 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3698 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3704 /* look for a boost amp in a widget close to the pin */
3705 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3706 struct nid_path *path)
3708 unsigned int val = 0;
3712 for (depth = 0; depth < 3; depth++) {
3713 if (depth >= path->depth - 1)
3715 nid = path->path[depth];
3716 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3717 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3719 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3720 path->idx[depth])) {
3721 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3730 static int parse_mic_boost(struct hda_codec *codec)
3732 struct hda_gen_spec *spec = codec->spec;
3733 struct auto_pin_cfg *cfg = &spec->autocfg;
3734 struct hda_input_mux *imux = &spec->input_mux;
3737 if (!spec->num_adc_nids)
3740 for (i = 0; i < imux->num_items; i++) {
3741 struct nid_path *path;
3744 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3746 idx = imux->items[i].index;
3747 if (idx >= imux->num_items)
3750 /* check only line-in and mic pins */
3751 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3754 path = get_input_path(codec, 0, i);
3758 val = look_for_boost_amp(codec, path);
3762 /* create a boost control */
3763 snprintf(boost_label, sizeof(boost_label),
3764 "%s Boost Volume", spec->input_labels[idx]);
3765 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3766 spec->input_label_idxs[idx], val))
3769 path->ctls[NID_PATH_BOOST_CTL] = val;
3775 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3777 static void parse_digital(struct hda_codec *codec)
3779 struct hda_gen_spec *spec = codec->spec;
3780 struct nid_path *path;
3782 hda_nid_t dig_nid, pin;
3784 /* support multiple SPDIFs; the secondary is set up as a slave */
3786 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3787 pin = spec->autocfg.dig_out_pins[i];
3788 dig_nid = look_for_dac(codec, pin, true);
3791 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3794 print_nid_path(codec, "digout", path);
3795 path->active = true;
3796 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3797 set_pin_target(codec, pin, PIN_OUT, false);
3799 spec->multiout.dig_out_nid = dig_nid;
3800 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3802 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3803 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3805 spec->slave_dig_outs[nums - 1] = dig_nid;
3810 if (spec->autocfg.dig_in_pin) {
3811 pin = spec->autocfg.dig_in_pin;
3812 dig_nid = codec->start_nid;
3813 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3814 unsigned int wcaps = get_wcaps(codec, dig_nid);
3815 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3817 if (!(wcaps & AC_WCAP_DIGITAL))
3819 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3821 print_nid_path(codec, "digin", path);
3822 path->active = true;
3823 spec->dig_in_nid = dig_nid;
3824 spec->digin_path = snd_hda_get_path_idx(codec, path);
3825 set_pin_target(codec, pin, PIN_IN, false);
3834 * input MUX handling
3837 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3839 /* select the given imux item; either unmute exclusively or select the route */
3840 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3843 struct hda_gen_spec *spec = codec->spec;
3844 const struct hda_input_mux *imux;
3845 struct nid_path *old_path, *path;
3847 imux = &spec->input_mux;
3848 if (!imux->num_items)
3851 if (idx >= imux->num_items)
3852 idx = imux->num_items - 1;
3853 if (spec->cur_mux[adc_idx] == idx)
3856 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3859 if (old_path->active)
3860 snd_hda_activate_path(codec, old_path, false, false);
3862 spec->cur_mux[adc_idx] = idx;
3865 update_hp_mic(codec, adc_idx, false);
3867 if (spec->dyn_adc_switch)
3868 dyn_adc_pcm_resetup(codec, idx);
3870 path = get_input_path(codec, adc_idx, idx);
3875 snd_hda_activate_path(codec, path, true, false);
3876 if (spec->cap_sync_hook)
3877 spec->cap_sync_hook(codec, NULL, NULL);
3878 path_power_down_sync(codec, old_path);
3884 * Jack detections for HP auto-mute and mic-switch
3887 /* check each pin in the given array; returns true if any of them is plugged */
3888 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3891 bool present = false;
3893 for (i = 0; i < num_pins; i++) {
3894 hda_nid_t nid = pins[i];
3897 /* don't detect pins retasked as inputs */
3898 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3900 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3906 /* standard HP/line-out auto-mute helper */
3907 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
3908 int *paths, bool mute)
3910 struct hda_gen_spec *spec = codec->spec;
3913 for (i = 0; i < num_pins; i++) {
3914 hda_nid_t nid = pins[i];
3915 unsigned int val, oldval;
3919 if (spec->auto_mute_via_amp) {
3920 struct nid_path *path;
3923 path = snd_hda_get_path_from_idx(codec, paths[i]);
3926 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
3930 spec->mute_bits |= (1ULL << mute_nid);
3932 spec->mute_bits &= ~(1ULL << mute_nid);
3933 set_pin_eapd(codec, nid, !mute);
3937 oldval = snd_hda_codec_get_pin_target(codec, nid);
3938 if (oldval & PIN_IN)
3939 continue; /* no mute for inputs */
3940 /* don't reset VREF value in case it's controlling
3941 * the amp (see alc861_fixup_asus_amp_vref_0f())
3943 if (spec->keep_vref_in_automute)
3944 val = oldval & ~PIN_HP;
3949 /* here we call update_pin_ctl() so that the pinctl is changed
3950 * without changing the pinctl target value;
3951 * the original target value will be still referred at the
3952 * init / resume again
3954 update_pin_ctl(codec, nid, val);
3955 set_pin_eapd(codec, nid, !mute);
3960 * snd_hda_gen_update_outputs - Toggle outputs muting
3961 * @codec: the HDA codec
3963 * Update the mute status of all outputs based on the current jack states.
3965 void snd_hda_gen_update_outputs(struct hda_codec *codec)
3967 struct hda_gen_spec *spec = codec->spec;
3971 /* Control HP pins/amps depending on master_mute state;
3972 * in general, HP pins/amps control should be enabled in all cases,
3973 * but currently set only for master_mute, just to be safe
3975 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3976 paths = spec->out_paths;
3978 paths = spec->hp_paths;
3979 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3980 spec->autocfg.hp_pins, paths, spec->master_mute);
3982 if (!spec->automute_speaker)
3985 on = spec->hp_jack_present | spec->line_jack_present;
3986 on |= spec->master_mute;
3987 spec->speaker_muted = on;
3988 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3989 paths = spec->out_paths;
3991 paths = spec->speaker_paths;
3992 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
3993 spec->autocfg.speaker_pins, paths, on);
3995 /* toggle line-out mutes if needed, too */
3996 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3997 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3998 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4000 if (!spec->automute_lo)
4003 on = spec->hp_jack_present;
4004 on |= spec->master_mute;
4005 spec->line_out_muted = on;
4006 paths = spec->out_paths;
4007 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4008 spec->autocfg.line_out_pins, paths, on);
4010 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4012 static void call_update_outputs(struct hda_codec *codec)
4014 struct hda_gen_spec *spec = codec->spec;
4015 if (spec->automute_hook)
4016 spec->automute_hook(codec);
4018 snd_hda_gen_update_outputs(codec);
4020 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4021 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4022 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4026 * snd_hda_gen_hp_automute - standard HP-automute helper
4027 * @codec: the HDA codec
4028 * @jack: jack object, NULL for the whole
4030 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4031 struct hda_jack_callback *jack)
4033 struct hda_gen_spec *spec = codec->spec;
4034 hda_nid_t *pins = spec->autocfg.hp_pins;
4035 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4037 /* No detection for the first HP jack during indep-HP mode */
4038 if (spec->indep_hp_enabled) {
4043 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4044 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4046 call_update_outputs(codec);
4048 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4051 * snd_hda_gen_line_automute - standard line-out-automute helper
4052 * @codec: the HDA codec
4053 * @jack: jack object, NULL for the whole
4055 void snd_hda_gen_line_automute(struct hda_codec *codec,
4056 struct hda_jack_callback *jack)
4058 struct hda_gen_spec *spec = codec->spec;
4060 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4062 /* check LO jack only when it's different from HP */
4063 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4066 spec->line_jack_present =
4067 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4068 spec->autocfg.line_out_pins);
4069 if (!spec->automute_speaker || !spec->detect_lo)
4071 call_update_outputs(codec);
4073 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4076 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4077 * @codec: the HDA codec
4078 * @jack: jack object, NULL for the whole
4080 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4081 struct hda_jack_callback *jack)
4083 struct hda_gen_spec *spec = codec->spec;
4086 if (!spec->auto_mic)
4089 for (i = spec->am_num_entries - 1; i > 0; i--) {
4090 hda_nid_t pin = spec->am_entry[i].pin;
4091 /* don't detect pins retasked as outputs */
4092 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4094 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4095 mux_select(codec, 0, spec->am_entry[i].idx);
4099 mux_select(codec, 0, spec->am_entry[0].idx);
4101 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4103 /* call appropriate hooks */
4104 static void call_hp_automute(struct hda_codec *codec,
4105 struct hda_jack_callback *jack)
4107 struct hda_gen_spec *spec = codec->spec;
4108 if (spec->hp_automute_hook)
4109 spec->hp_automute_hook(codec, jack);
4111 snd_hda_gen_hp_automute(codec, jack);
4114 static void call_line_automute(struct hda_codec *codec,
4115 struct hda_jack_callback *jack)
4117 struct hda_gen_spec *spec = codec->spec;
4118 if (spec->line_automute_hook)
4119 spec->line_automute_hook(codec, jack);
4121 snd_hda_gen_line_automute(codec, jack);
4124 static void call_mic_autoswitch(struct hda_codec *codec,
4125 struct hda_jack_callback *jack)
4127 struct hda_gen_spec *spec = codec->spec;
4128 if (spec->mic_autoswitch_hook)
4129 spec->mic_autoswitch_hook(codec, jack);
4131 snd_hda_gen_mic_autoswitch(codec, jack);
4134 /* update jack retasking */
4135 static void update_automute_all(struct hda_codec *codec)
4137 call_hp_automute(codec, NULL);
4138 call_line_automute(codec, NULL);
4139 call_mic_autoswitch(codec, NULL);
4143 * Auto-Mute mode mixer enum support
4145 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4146 struct snd_ctl_elem_info *uinfo)
4148 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4149 struct hda_gen_spec *spec = codec->spec;
4150 static const char * const texts3[] = {
4151 "Disabled", "Speaker Only", "Line Out+Speaker"
4154 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4155 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4156 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4159 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4160 struct snd_ctl_elem_value *ucontrol)
4162 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4163 struct hda_gen_spec *spec = codec->spec;
4164 unsigned int val = 0;
4165 if (spec->automute_speaker)
4167 if (spec->automute_lo)
4170 ucontrol->value.enumerated.item[0] = val;
4174 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4175 struct snd_ctl_elem_value *ucontrol)
4177 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4178 struct hda_gen_spec *spec = codec->spec;
4180 switch (ucontrol->value.enumerated.item[0]) {
4182 if (!spec->automute_speaker && !spec->automute_lo)
4184 spec->automute_speaker = 0;
4185 spec->automute_lo = 0;
4188 if (spec->automute_speaker_possible) {
4189 if (!spec->automute_lo && spec->automute_speaker)
4191 spec->automute_speaker = 1;
4192 spec->automute_lo = 0;
4193 } else if (spec->automute_lo_possible) {
4194 if (spec->automute_lo)
4196 spec->automute_lo = 1;
4201 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4203 if (spec->automute_speaker && spec->automute_lo)
4205 spec->automute_speaker = 1;
4206 spec->automute_lo = 1;
4211 call_update_outputs(codec);
4215 static const struct snd_kcontrol_new automute_mode_enum = {
4216 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4217 .name = "Auto-Mute Mode",
4218 .info = automute_mode_info,
4219 .get = automute_mode_get,
4220 .put = automute_mode_put,
4223 static int add_automute_mode_enum(struct hda_codec *codec)
4225 struct hda_gen_spec *spec = codec->spec;
4227 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4233 * Check the availability of HP/line-out auto-mute;
4234 * Set up appropriately if really supported
4236 static int check_auto_mute_availability(struct hda_codec *codec)
4238 struct hda_gen_spec *spec = codec->spec;
4239 struct auto_pin_cfg *cfg = &spec->autocfg;
4243 if (spec->suppress_auto_mute)
4246 if (cfg->hp_pins[0])
4248 if (cfg->line_out_pins[0])
4250 if (cfg->speaker_pins[0])
4252 if (present < 2) /* need two different output types */
4255 if (!cfg->speaker_pins[0] &&
4256 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4257 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4258 sizeof(cfg->speaker_pins));
4259 cfg->speaker_outs = cfg->line_outs;
4262 if (!cfg->hp_pins[0] &&
4263 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4264 memcpy(cfg->hp_pins, cfg->line_out_pins,
4265 sizeof(cfg->hp_pins));
4266 cfg->hp_outs = cfg->line_outs;
4269 for (i = 0; i < cfg->hp_outs; i++) {
4270 hda_nid_t nid = cfg->hp_pins[i];
4271 if (!is_jack_detectable(codec, nid))
4273 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4274 snd_hda_jack_detect_enable_callback(codec, nid,
4276 spec->detect_hp = 1;
4279 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4280 if (cfg->speaker_outs)
4281 for (i = 0; i < cfg->line_outs; i++) {
4282 hda_nid_t nid = cfg->line_out_pins[i];
4283 if (!is_jack_detectable(codec, nid))
4285 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4286 snd_hda_jack_detect_enable_callback(codec, nid,
4287 call_line_automute);
4288 spec->detect_lo = 1;
4290 spec->automute_lo_possible = spec->detect_hp;
4293 spec->automute_speaker_possible = cfg->speaker_outs &&
4294 (spec->detect_hp || spec->detect_lo);
4296 spec->automute_lo = spec->automute_lo_possible;
4297 spec->automute_speaker = spec->automute_speaker_possible;
4299 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4300 /* create a control for automute mode */
4301 err = add_automute_mode_enum(codec);
4308 /* check whether all auto-mic pins are valid; setup indices if OK */
4309 static bool auto_mic_check_imux(struct hda_codec *codec)
4311 struct hda_gen_spec *spec = codec->spec;
4312 const struct hda_input_mux *imux;
4315 imux = &spec->input_mux;
4316 for (i = 0; i < spec->am_num_entries; i++) {
4317 spec->am_entry[i].idx =
4318 find_idx_in_nid_list(spec->am_entry[i].pin,
4319 spec->imux_pins, imux->num_items);
4320 if (spec->am_entry[i].idx < 0)
4321 return false; /* no corresponding imux */
4324 /* we don't need the jack detection for the first pin */
4325 for (i = 1; i < spec->am_num_entries; i++)
4326 snd_hda_jack_detect_enable_callback(codec,
4327 spec->am_entry[i].pin,
4328 call_mic_autoswitch);
4332 static int compare_attr(const void *ap, const void *bp)
4334 const struct automic_entry *a = ap;
4335 const struct automic_entry *b = bp;
4336 return (int)(a->attr - b->attr);
4340 * Check the availability of auto-mic switch;
4341 * Set up if really supported
4343 static int check_auto_mic_availability(struct hda_codec *codec)
4345 struct hda_gen_spec *spec = codec->spec;
4346 struct auto_pin_cfg *cfg = &spec->autocfg;
4350 if (spec->suppress_auto_mic)
4355 for (i = 0; i < cfg->num_inputs; i++) {
4356 hda_nid_t nid = cfg->inputs[i].pin;
4358 attr = snd_hda_codec_get_pincfg(codec, nid);
4359 attr = snd_hda_get_input_pin_attr(attr);
4360 if (types & (1 << attr))
4361 return 0; /* already occupied */
4363 case INPUT_PIN_ATTR_INT:
4364 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4365 return 0; /* invalid type */
4367 case INPUT_PIN_ATTR_UNUSED:
4368 return 0; /* invalid entry */
4370 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4371 return 0; /* invalid type */
4372 if (!spec->line_in_auto_switch &&
4373 cfg->inputs[i].type != AUTO_PIN_MIC)
4374 return 0; /* only mic is allowed */
4375 if (!is_jack_detectable(codec, nid))
4376 return 0; /* no unsol support */
4379 if (num_pins >= MAX_AUTO_MIC_PINS)
4381 types |= (1 << attr);
4382 spec->am_entry[num_pins].pin = nid;
4383 spec->am_entry[num_pins].attr = attr;
4390 spec->am_num_entries = num_pins;
4391 /* sort the am_entry in the order of attr so that the pin with a
4392 * higher attr will be selected when the jack is plugged.
4394 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4395 compare_attr, NULL);
4397 if (!auto_mic_check_imux(codec))
4401 spec->num_adc_nids = 1;
4402 spec->cur_mux[0] = spec->am_entry[0].idx;
4403 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4404 spec->am_entry[0].pin,
4405 spec->am_entry[1].pin,
4406 spec->am_entry[2].pin);
4412 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4414 * @codec: the HDA codec
4415 * @nid: NID to evalute
4416 * @power_state: target power state
4418 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4420 unsigned int power_state)
4422 if (power_state != AC_PWRST_D0 || nid == codec->afg)
4424 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4426 if (is_active_nid_for_any(codec, nid))
4430 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4432 /* mute all aamix inputs initially; parse up to the first leaves */
4433 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4436 const hda_nid_t *conn;
4439 nums = snd_hda_get_conn_list(codec, mix, &conn);
4440 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4441 for (i = 0; i < nums; i++) {
4443 update_amp(codec, mix, HDA_INPUT, i,
4444 0xff, HDA_AMP_MUTE);
4445 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4446 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4447 0xff, HDA_AMP_MUTE);
4452 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4453 * set up the hda_gen_spec
4454 * @codec: the HDA codec
4455 * @cfg: Parsed pin configuration
4457 * return 1 if successful, 0 if the proper config is not found,
4458 * or a negative error code
4460 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4461 struct auto_pin_cfg *cfg)
4463 struct hda_gen_spec *spec = codec->spec;
4466 parse_user_hints(codec);
4468 if (spec->mixer_nid && !spec->mixer_merge_nid)
4469 spec->mixer_merge_nid = spec->mixer_nid;
4471 if (cfg != &spec->autocfg) {
4472 spec->autocfg = *cfg;
4473 cfg = &spec->autocfg;
4476 if (!spec->main_out_badness)
4477 spec->main_out_badness = &hda_main_out_badness;
4478 if (!spec->extra_out_badness)
4479 spec->extra_out_badness = &hda_extra_out_badness;
4481 fill_all_dac_nids(codec);
4483 if (!cfg->line_outs) {
4484 if (cfg->dig_outs || cfg->dig_in_pin) {
4485 spec->multiout.max_channels = 2;
4486 spec->no_analog = 1;
4489 if (!cfg->num_inputs && !cfg->dig_in_pin)
4490 return 0; /* can't find valid BIOS pin config */
4493 if (!spec->no_primary_hp &&
4494 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4495 cfg->line_outs <= cfg->hp_outs) {
4496 /* use HP as primary out */
4497 cfg->speaker_outs = cfg->line_outs;
4498 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4499 sizeof(cfg->speaker_pins));
4500 cfg->line_outs = cfg->hp_outs;
4501 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4503 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4504 cfg->line_out_type = AUTO_PIN_HP_OUT;
4507 err = parse_output_paths(codec);
4510 err = create_multi_channel_mode(codec);
4513 err = create_multi_out_ctls(codec, cfg);
4516 err = create_hp_out_ctls(codec);
4519 err = create_speaker_out_ctls(codec);
4522 err = create_indep_hp_ctls(codec);
4525 err = create_loopback_mixing_ctl(codec);
4528 err = create_hp_mic(codec);
4531 err = create_input_ctls(codec);
4535 spec->const_channel_count = spec->ext_channel_count;
4536 /* check the multiple speaker and headphone pins */
4537 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4538 spec->const_channel_count = max(spec->const_channel_count,
4539 cfg->speaker_outs * 2);
4540 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4541 spec->const_channel_count = max(spec->const_channel_count,
4543 spec->multiout.max_channels = max(spec->ext_channel_count,
4544 spec->const_channel_count);
4546 err = check_auto_mute_availability(codec);
4550 err = check_dyn_adc_switch(codec);
4554 err = check_auto_mic_availability(codec);
4558 /* add stereo mix if available and not enabled yet */
4559 if (!spec->auto_mic && spec->mixer_nid &&
4560 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4561 spec->input_mux.num_items > 1) {
4562 err = parse_capture_source(codec, spec->mixer_nid,
4563 CFG_IDX_MIX, spec->num_all_adcs,
4570 err = create_capture_mixers(codec);
4574 err = parse_mic_boost(codec);
4578 /* create "Headphone Mic Jack Mode" if no input selection is
4579 * available (or user specifies add_jack_modes hint)
4581 if (spec->hp_mic_pin &&
4582 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4583 spec->add_jack_modes)) {
4584 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4589 if (spec->add_jack_modes) {
4590 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4591 err = create_out_jack_modes(codec, cfg->line_outs,
4592 cfg->line_out_pins);
4596 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4597 err = create_out_jack_modes(codec, cfg->hp_outs,
4604 /* mute all aamix input initially */
4605 if (spec->mixer_nid)
4606 mute_all_mixer_nid(codec, spec->mixer_nid);
4609 parse_digital(codec);
4611 if (spec->power_down_unused)
4612 codec->power_filter = snd_hda_gen_path_power_filter;
4614 if (!spec->no_analog && spec->beep_nid) {
4615 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4622 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
4626 * Build control elements
4629 /* slave controls for virtual master */
4630 static const char * const slave_pfxs[] = {
4631 "Front", "Surround", "Center", "LFE", "Side",
4632 "Headphone", "Speaker", "Mono", "Line Out",
4633 "CLFE", "Bass Speaker", "PCM",
4634 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4635 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4636 "Headphone Side", "Headphone+LO", "Speaker+LO",
4641 * snd_hda_gen_build_controls - Build controls from the parsed results
4642 * @codec: the HDA codec
4644 * Pass this to build_controls patch_ops.
4646 int snd_hda_gen_build_controls(struct hda_codec *codec)
4648 struct hda_gen_spec *spec = codec->spec;
4651 if (spec->kctls.used) {
4652 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4657 if (spec->multiout.dig_out_nid) {
4658 err = snd_hda_create_dig_out_ctls(codec,
4659 spec->multiout.dig_out_nid,
4660 spec->multiout.dig_out_nid,
4661 spec->pcm_rec[1].pcm_type);
4664 if (!spec->no_analog) {
4665 err = snd_hda_create_spdif_share_sw(codec,
4669 spec->multiout.share_spdif = 1;
4672 if (spec->dig_in_nid) {
4673 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4678 /* if we have no master control, let's create it */
4679 if (!spec->no_analog &&
4680 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
4681 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
4682 spec->vmaster_tlv, slave_pfxs,
4687 if (!spec->no_analog &&
4688 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4689 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4692 true, &spec->vmaster_mute.sw_kctl);
4695 if (spec->vmaster_mute.hook) {
4696 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4697 spec->vmaster_mute_enum);
4698 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4702 free_kctls(spec); /* no longer needed */
4704 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4710 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
4717 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4718 struct hda_codec *codec,
4719 struct snd_pcm_substream *substream,
4722 struct hda_gen_spec *spec = codec->spec;
4723 if (spec->pcm_playback_hook)
4724 spec->pcm_playback_hook(hinfo, codec, substream, action);
4727 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4728 struct hda_codec *codec,
4729 struct snd_pcm_substream *substream,
4732 struct hda_gen_spec *spec = codec->spec;
4733 if (spec->pcm_capture_hook)
4734 spec->pcm_capture_hook(hinfo, codec, substream, action);
4738 * Analog playback callbacks
4740 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4741 struct hda_codec *codec,
4742 struct snd_pcm_substream *substream)
4744 struct hda_gen_spec *spec = codec->spec;
4747 mutex_lock(&spec->pcm_mutex);
4748 err = snd_hda_multi_out_analog_open(codec,
4749 &spec->multiout, substream,
4752 spec->active_streams |= 1 << STREAM_MULTI_OUT;
4753 call_pcm_playback_hook(hinfo, codec, substream,
4754 HDA_GEN_PCM_ACT_OPEN);
4756 mutex_unlock(&spec->pcm_mutex);
4760 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4761 struct hda_codec *codec,
4762 unsigned int stream_tag,
4763 unsigned int format,
4764 struct snd_pcm_substream *substream)
4766 struct hda_gen_spec *spec = codec->spec;
4769 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4770 stream_tag, format, substream);
4772 call_pcm_playback_hook(hinfo, codec, substream,
4773 HDA_GEN_PCM_ACT_PREPARE);
4777 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4778 struct hda_codec *codec,
4779 struct snd_pcm_substream *substream)
4781 struct hda_gen_spec *spec = codec->spec;
4784 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4786 call_pcm_playback_hook(hinfo, codec, substream,
4787 HDA_GEN_PCM_ACT_CLEANUP);
4791 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4792 struct hda_codec *codec,
4793 struct snd_pcm_substream *substream)
4795 struct hda_gen_spec *spec = codec->spec;
4796 mutex_lock(&spec->pcm_mutex);
4797 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
4798 call_pcm_playback_hook(hinfo, codec, substream,
4799 HDA_GEN_PCM_ACT_CLOSE);
4800 mutex_unlock(&spec->pcm_mutex);
4804 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4805 struct hda_codec *codec,
4806 struct snd_pcm_substream *substream)
4808 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4812 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4813 struct hda_codec *codec,
4814 unsigned int stream_tag,
4815 unsigned int format,
4816 struct snd_pcm_substream *substream)
4818 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4819 call_pcm_capture_hook(hinfo, codec, substream,
4820 HDA_GEN_PCM_ACT_PREPARE);
4824 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4825 struct hda_codec *codec,
4826 struct snd_pcm_substream *substream)
4828 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4829 call_pcm_capture_hook(hinfo, codec, substream,
4830 HDA_GEN_PCM_ACT_CLEANUP);
4834 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4835 struct hda_codec *codec,
4836 struct snd_pcm_substream *substream)
4838 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4842 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4843 struct hda_codec *codec,
4844 struct snd_pcm_substream *substream)
4846 struct hda_gen_spec *spec = codec->spec;
4849 mutex_lock(&spec->pcm_mutex);
4850 if (!spec->indep_hp_enabled)
4853 spec->active_streams |= 1 << STREAM_INDEP_HP;
4854 call_pcm_playback_hook(hinfo, codec, substream,
4855 HDA_GEN_PCM_ACT_OPEN);
4856 mutex_unlock(&spec->pcm_mutex);
4860 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4861 struct hda_codec *codec,
4862 struct snd_pcm_substream *substream)
4864 struct hda_gen_spec *spec = codec->spec;
4865 mutex_lock(&spec->pcm_mutex);
4866 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
4867 call_pcm_playback_hook(hinfo, codec, substream,
4868 HDA_GEN_PCM_ACT_CLOSE);
4869 mutex_unlock(&spec->pcm_mutex);
4873 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4874 struct hda_codec *codec,
4875 unsigned int stream_tag,
4876 unsigned int format,
4877 struct snd_pcm_substream *substream)
4879 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4880 call_pcm_playback_hook(hinfo, codec, substream,
4881 HDA_GEN_PCM_ACT_PREPARE);
4885 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4886 struct hda_codec *codec,
4887 struct snd_pcm_substream *substream)
4889 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4890 call_pcm_playback_hook(hinfo, codec, substream,
4891 HDA_GEN_PCM_ACT_CLEANUP);
4898 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4899 struct hda_codec *codec,
4900 struct snd_pcm_substream *substream)
4902 struct hda_gen_spec *spec = codec->spec;
4903 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4906 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4907 struct hda_codec *codec,
4908 unsigned int stream_tag,
4909 unsigned int format,
4910 struct snd_pcm_substream *substream)
4912 struct hda_gen_spec *spec = codec->spec;
4913 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4914 stream_tag, format, substream);
4917 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4918 struct hda_codec *codec,
4919 struct snd_pcm_substream *substream)
4921 struct hda_gen_spec *spec = codec->spec;
4922 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4925 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4926 struct hda_codec *codec,
4927 struct snd_pcm_substream *substream)
4929 struct hda_gen_spec *spec = codec->spec;
4930 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4936 #define alt_capture_pcm_open capture_pcm_open
4937 #define alt_capture_pcm_close capture_pcm_close
4939 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4940 struct hda_codec *codec,
4941 unsigned int stream_tag,
4942 unsigned int format,
4943 struct snd_pcm_substream *substream)
4945 struct hda_gen_spec *spec = codec->spec;
4947 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
4948 stream_tag, 0, format);
4949 call_pcm_capture_hook(hinfo, codec, substream,
4950 HDA_GEN_PCM_ACT_PREPARE);
4954 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4955 struct hda_codec *codec,
4956 struct snd_pcm_substream *substream)
4958 struct hda_gen_spec *spec = codec->spec;
4960 snd_hda_codec_cleanup_stream(codec,
4961 spec->adc_nids[substream->number + 1]);
4962 call_pcm_capture_hook(hinfo, codec, substream,
4963 HDA_GEN_PCM_ACT_CLEANUP);
4969 static const struct hda_pcm_stream pcm_analog_playback = {
4973 /* NID is set in build_pcms */
4975 .open = playback_pcm_open,
4976 .close = playback_pcm_close,
4977 .prepare = playback_pcm_prepare,
4978 .cleanup = playback_pcm_cleanup
4982 static const struct hda_pcm_stream pcm_analog_capture = {
4986 /* NID is set in build_pcms */
4988 .open = capture_pcm_open,
4989 .close = capture_pcm_close,
4990 .prepare = capture_pcm_prepare,
4991 .cleanup = capture_pcm_cleanup
4995 static const struct hda_pcm_stream pcm_analog_alt_playback = {
4999 /* NID is set in build_pcms */
5001 .open = alt_playback_pcm_open,
5002 .close = alt_playback_pcm_close,
5003 .prepare = alt_playback_pcm_prepare,
5004 .cleanup = alt_playback_pcm_cleanup
5008 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5009 .substreams = 2, /* can be overridden */
5012 /* NID is set in build_pcms */
5014 .open = alt_capture_pcm_open,
5015 .close = alt_capture_pcm_close,
5016 .prepare = alt_capture_pcm_prepare,
5017 .cleanup = alt_capture_pcm_cleanup
5021 static const struct hda_pcm_stream pcm_digital_playback = {
5025 /* NID is set in build_pcms */
5027 .open = dig_playback_pcm_open,
5028 .close = dig_playback_pcm_close,
5029 .prepare = dig_playback_pcm_prepare,
5030 .cleanup = dig_playback_pcm_cleanup
5034 static const struct hda_pcm_stream pcm_digital_capture = {
5038 /* NID is set in build_pcms */
5041 /* Used by build_pcms to flag that a PCM has no playback stream */
5042 static const struct hda_pcm_stream pcm_null_stream = {
5049 * dynamic changing ADC PCM streams
5051 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5053 struct hda_gen_spec *spec = codec->spec;
5054 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5056 if (spec->cur_adc && spec->cur_adc != new_adc) {
5057 /* stream is running, let's swap the current ADC */
5058 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5059 spec->cur_adc = new_adc;
5060 snd_hda_codec_setup_stream(codec, new_adc,
5061 spec->cur_adc_stream_tag, 0,
5062 spec->cur_adc_format);
5068 /* analog capture with dynamic dual-adc changes */
5069 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5070 struct hda_codec *codec,
5071 unsigned int stream_tag,
5072 unsigned int format,
5073 struct snd_pcm_substream *substream)
5075 struct hda_gen_spec *spec = codec->spec;
5076 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5077 spec->cur_adc_stream_tag = stream_tag;
5078 spec->cur_adc_format = format;
5079 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5083 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5084 struct hda_codec *codec,
5085 struct snd_pcm_substream *substream)
5087 struct hda_gen_spec *spec = codec->spec;
5088 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5093 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5097 .nid = 0, /* fill later */
5099 .prepare = dyn_adc_capture_pcm_prepare,
5100 .cleanup = dyn_adc_capture_pcm_cleanup
5104 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5105 const char *chip_name)
5111 strlcpy(str, chip_name, len);
5113 /* drop non-alnum chars after a space */
5114 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5115 if (!isalnum(p[1])) {
5120 strlcat(str, sfx, len);
5124 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5125 * @codec: the HDA codec
5127 * Pass this to build_pcms patch_ops.
5129 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5131 struct hda_gen_spec *spec = codec->spec;
5132 struct hda_pcm *info = spec->pcm_rec;
5133 const struct hda_pcm_stream *p;
5134 bool have_multi_adcs;
5136 codec->num_pcms = 1;
5137 codec->pcm_info = info;
5139 if (spec->no_analog)
5142 fill_pcm_stream_name(spec->stream_name_analog,
5143 sizeof(spec->stream_name_analog),
5144 " Analog", codec->chip_name);
5145 info->name = spec->stream_name_analog;
5147 if (spec->multiout.num_dacs > 0) {
5148 p = spec->stream_analog_playback;
5150 p = &pcm_analog_playback;
5151 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5152 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
5153 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5154 spec->multiout.max_channels;
5155 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5156 spec->autocfg.line_outs == 2)
5157 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5160 if (spec->num_adc_nids) {
5161 p = spec->stream_analog_capture;
5163 if (spec->dyn_adc_switch)
5164 p = &dyn_adc_pcm_analog_capture;
5166 p = &pcm_analog_capture;
5168 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5169 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
5173 /* SPDIF for stream index #1 */
5174 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5175 fill_pcm_stream_name(spec->stream_name_digital,
5176 sizeof(spec->stream_name_digital),
5177 " Digital", codec->chip_name);
5178 codec->num_pcms = 2;
5179 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5180 info = spec->pcm_rec + 1;
5181 info->name = spec->stream_name_digital;
5182 if (spec->dig_out_type)
5183 info->pcm_type = spec->dig_out_type;
5185 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5186 if (spec->multiout.dig_out_nid) {
5187 p = spec->stream_digital_playback;
5189 p = &pcm_digital_playback;
5190 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5191 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
5193 if (spec->dig_in_nid) {
5194 p = spec->stream_digital_capture;
5196 p = &pcm_digital_capture;
5197 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5198 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
5202 if (spec->no_analog)
5205 /* If the use of more than one ADC is requested for the current
5206 * model, configure a second analog capture-only PCM.
5208 have_multi_adcs = (spec->num_adc_nids > 1) &&
5209 !spec->dyn_adc_switch && !spec->auto_mic;
5210 /* Additional Analaog capture for index #2 */
5211 if (spec->alt_dac_nid || have_multi_adcs) {
5212 fill_pcm_stream_name(spec->stream_name_alt_analog,
5213 sizeof(spec->stream_name_alt_analog),
5214 " Alt Analog", codec->chip_name);
5215 codec->num_pcms = 3;
5216 info = spec->pcm_rec + 2;
5217 info->name = spec->stream_name_alt_analog;
5218 if (spec->alt_dac_nid) {
5219 p = spec->stream_analog_alt_playback;
5221 p = &pcm_analog_alt_playback;
5222 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
5223 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
5226 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
5228 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
5230 if (have_multi_adcs) {
5231 p = spec->stream_analog_alt_capture;
5233 p = &pcm_analog_alt_capture;
5234 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5235 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
5237 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5238 spec->num_adc_nids - 1;
5240 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
5242 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
5248 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5252 * Standard auto-parser initializations
5255 /* configure the given path as a proper output */
5256 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5258 struct nid_path *path;
5261 path = snd_hda_get_path_from_idx(codec, path_idx);
5262 if (!path || !path->depth)
5264 pin = path->path[path->depth - 1];
5265 restore_pin_ctl(codec, pin);
5266 snd_hda_activate_path(codec, path, path->active,
5267 aamix_default(codec->spec));
5268 set_pin_eapd(codec, pin, path->active);
5271 /* initialize primary output paths */
5272 static void init_multi_out(struct hda_codec *codec)
5274 struct hda_gen_spec *spec = codec->spec;
5277 for (i = 0; i < spec->autocfg.line_outs; i++)
5278 set_output_and_unmute(codec, spec->out_paths[i]);
5282 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5286 for (i = 0; i < num_outs; i++)
5287 set_output_and_unmute(codec, paths[i]);
5290 /* initialize hp and speaker paths */
5291 static void init_extra_out(struct hda_codec *codec)
5293 struct hda_gen_spec *spec = codec->spec;
5295 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5296 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5297 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5298 __init_extra_out(codec, spec->autocfg.speaker_outs,
5299 spec->speaker_paths);
5302 /* initialize multi-io paths */
5303 static void init_multi_io(struct hda_codec *codec)
5305 struct hda_gen_spec *spec = codec->spec;
5308 for (i = 0; i < spec->multi_ios; i++) {
5309 hda_nid_t pin = spec->multi_io[i].pin;
5310 struct nid_path *path;
5311 path = get_multiio_path(codec, i);
5314 if (!spec->multi_io[i].ctl_in)
5315 spec->multi_io[i].ctl_in =
5316 snd_hda_codec_get_pin_target(codec, pin);
5317 snd_hda_activate_path(codec, path, path->active,
5318 aamix_default(spec));
5322 static void init_aamix_paths(struct hda_codec *codec)
5324 struct hda_gen_spec *spec = codec->spec;
5326 if (!spec->have_aamix_ctl)
5328 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5329 spec->aamix_out_paths[0],
5330 spec->autocfg.line_out_type);
5331 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5332 spec->aamix_out_paths[1],
5334 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5335 spec->aamix_out_paths[2],
5336 AUTO_PIN_SPEAKER_OUT);
5339 /* set up input pins and loopback paths */
5340 static void init_analog_input(struct hda_codec *codec)
5342 struct hda_gen_spec *spec = codec->spec;
5343 struct auto_pin_cfg *cfg = &spec->autocfg;
5346 for (i = 0; i < cfg->num_inputs; i++) {
5347 hda_nid_t nid = cfg->inputs[i].pin;
5348 if (is_input_pin(codec, nid))
5349 restore_pin_ctl(codec, nid);
5351 /* init loopback inputs */
5352 if (spec->mixer_nid) {
5353 resume_path_from_idx(codec, spec->loopback_paths[i]);
5354 resume_path_from_idx(codec, spec->loopback_merge_path);
5359 /* initialize ADC paths */
5360 static void init_input_src(struct hda_codec *codec)
5362 struct hda_gen_spec *spec = codec->spec;
5363 struct hda_input_mux *imux = &spec->input_mux;
5364 struct nid_path *path;
5367 if (spec->dyn_adc_switch)
5370 nums = spec->num_adc_nids;
5372 for (c = 0; c < nums; c++) {
5373 for (i = 0; i < imux->num_items; i++) {
5374 path = get_input_path(codec, c, i);
5376 bool active = path->active;
5377 if (i == spec->cur_mux[c])
5379 snd_hda_activate_path(codec, path, active, false);
5383 update_hp_mic(codec, c, true);
5386 if (spec->cap_sync_hook)
5387 spec->cap_sync_hook(codec, NULL, NULL);
5390 /* set right pin controls for digital I/O */
5391 static void init_digital(struct hda_codec *codec)
5393 struct hda_gen_spec *spec = codec->spec;
5397 for (i = 0; i < spec->autocfg.dig_outs; i++)
5398 set_output_and_unmute(codec, spec->digout_paths[i]);
5399 pin = spec->autocfg.dig_in_pin;
5401 restore_pin_ctl(codec, pin);
5402 resume_path_from_idx(codec, spec->digin_path);
5406 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5407 * invalid unsol tags by some reason
5409 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5413 for (i = 0; i < codec->init_pins.used; i++) {
5414 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5415 hda_nid_t nid = pin->nid;
5416 if (is_jack_detectable(codec, nid) &&
5417 !snd_hda_jack_tbl_get(codec, nid))
5418 snd_hda_codec_update_cache(codec, nid, 0,
5419 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5424 * snd_hda_gen_init - initialize the generic spec
5425 * @codec: the HDA codec
5427 * This can be put as patch_ops init function.
5429 int snd_hda_gen_init(struct hda_codec *codec)
5431 struct hda_gen_spec *spec = codec->spec;
5433 if (spec->init_hook)
5434 spec->init_hook(codec);
5436 snd_hda_apply_verbs(codec);
5438 codec->cached_write = 1;
5440 init_multi_out(codec);
5441 init_extra_out(codec);
5442 init_multi_io(codec);
5443 init_aamix_paths(codec);
5444 init_analog_input(codec);
5445 init_input_src(codec);
5446 init_digital(codec);
5448 clear_unsol_on_unused_pins(codec);
5450 /* call init functions of standard auto-mute helpers */
5451 update_automute_all(codec);
5453 snd_hda_codec_flush_cache(codec);
5455 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5456 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5458 hda_call_check_power_status(codec, 0x01);
5461 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
5464 * snd_hda_gen_free - free the generic spec
5465 * @codec: the HDA codec
5467 * This can be put as patch_ops free function.
5469 void snd_hda_gen_free(struct hda_codec *codec)
5471 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
5472 snd_hda_gen_spec_free(codec->spec);
5476 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
5480 * snd_hda_gen_check_power_status - check the loopback power save state
5481 * @codec: the HDA codec
5482 * @nid: NID to inspect
5484 * This can be put as patch_ops check_power_status function.
5486 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5488 struct hda_gen_spec *spec = codec->spec;
5489 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5491 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
5496 * the generic codec support
5499 static const struct hda_codec_ops generic_patch_ops = {
5500 .build_controls = snd_hda_gen_build_controls,
5501 .build_pcms = snd_hda_gen_build_pcms,
5502 .init = snd_hda_gen_init,
5503 .free = snd_hda_gen_free,
5504 .unsol_event = snd_hda_jack_unsol_event,
5506 .check_power_status = snd_hda_gen_check_power_status,
5511 * snd_hda_parse_generic_codec - Generic codec parser
5512 * @codec: the HDA codec
5514 * This should be called from the HDA codec core.
5516 int snd_hda_parse_generic_codec(struct hda_codec *codec)
5518 struct hda_gen_spec *spec;
5521 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5524 snd_hda_gen_spec_init(spec);
5527 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5531 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5535 codec->patch_ops = generic_patch_ops;
5539 snd_hda_gen_free(codec);
5542 EXPORT_SYMBOL_GPL(snd_hda_parse_generic_codec);
5544 MODULE_LICENSE("GPL");
5545 MODULE_DESCRIPTION("Generic HD-audio codec parser");