1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Universal Interface for Intel High Definition Audio Codec
5 * Generic widget tree parser
7 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/export.h>
13 #include <linux/sort.h>
14 #include <linux/delay.h>
15 #include <linux/ctype.h>
16 #include <linux/string.h>
17 #include <linux/bitops.h>
18 #include <linux/module.h>
19 #include <linux/leds.h>
20 #include <sound/core.h>
21 #include <sound/jack.h>
22 #include <sound/tlv.h>
23 #include <sound/hda_codec.h>
24 #include "hda_local.h"
25 #include "hda_auto_parser.h"
28 #include "hda_generic.h"
32 * snd_hda_gen_spec_init - initialize hda_gen_spec struct
33 * @spec: hda_gen_spec object to initialize
35 * Initialize the given hda_gen_spec object.
37 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
39 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
40 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
41 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
42 mutex_init(&spec->pcm_mutex);
45 EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
48 * snd_hda_gen_add_kctl - Add a new kctl_new struct from the template
49 * @spec: hda_gen_spec object
50 * @name: name string to override the template, NULL if unchanged
51 * @temp: template for the new kctl
53 * Add a new kctl (actually snd_kcontrol_new to be instantiated later)
54 * element based on the given snd_kcontrol_new template @temp and the
55 * name string @name to the list in @spec.
56 * Returns the newly created object or NULL as error.
58 struct snd_kcontrol_new *
59 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
60 const struct snd_kcontrol_new *temp)
62 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
67 knew->name = kstrdup(name, GFP_KERNEL);
69 knew->name = kstrdup(knew->name, GFP_KERNEL);
74 EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
76 static void free_kctls(struct hda_gen_spec *spec)
78 if (spec->kctls.list) {
79 struct snd_kcontrol_new *kctl = spec->kctls.list;
81 for (i = 0; i < spec->kctls.used; i++)
84 snd_array_free(&spec->kctls);
87 static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
92 snd_array_free(&spec->paths);
93 snd_array_free(&spec->loopback_list);
94 #ifdef CONFIG_SND_HDA_GENERIC_LEDS
95 if (spec->led_cdevs[LED_AUDIO_MUTE])
96 led_classdev_unregister(spec->led_cdevs[LED_AUDIO_MUTE]);
97 if (spec->led_cdevs[LED_AUDIO_MICMUTE])
98 led_classdev_unregister(spec->led_cdevs[LED_AUDIO_MICMUTE]);
105 static void parse_user_hints(struct hda_codec *codec)
107 struct hda_gen_spec *spec = codec->spec;
110 val = snd_hda_get_bool_hint(codec, "jack_detect");
112 codec->no_jack_detect = !val;
113 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
115 codec->inv_jack_detect = !!val;
116 val = snd_hda_get_bool_hint(codec, "trigger_sense");
118 codec->no_trigger_sense = !val;
119 val = snd_hda_get_bool_hint(codec, "inv_eapd");
121 codec->inv_eapd = !!val;
122 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
124 codec->pcm_format_first = !!val;
125 val = snd_hda_get_bool_hint(codec, "sticky_stream");
127 codec->no_sticky_stream = !val;
128 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
130 codec->spdif_status_reset = !!val;
131 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
133 codec->pin_amp_workaround = !!val;
134 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
136 codec->single_adc_amp = !!val;
137 val = snd_hda_get_bool_hint(codec, "power_save_node");
139 codec->power_save_node = !!val;
141 val = snd_hda_get_bool_hint(codec, "auto_mute");
143 spec->suppress_auto_mute = !val;
144 val = snd_hda_get_bool_hint(codec, "auto_mic");
146 spec->suppress_auto_mic = !val;
147 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
149 spec->line_in_auto_switch = !!val;
150 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
152 spec->auto_mute_via_amp = !!val;
153 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
155 spec->need_dac_fix = !!val;
156 val = snd_hda_get_bool_hint(codec, "primary_hp");
158 spec->no_primary_hp = !val;
159 val = snd_hda_get_bool_hint(codec, "multi_io");
161 spec->no_multi_io = !val;
162 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
164 spec->multi_cap_vol = !!val;
165 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
167 spec->inv_dmic_split = !!val;
168 val = snd_hda_get_bool_hint(codec, "indep_hp");
170 spec->indep_hp = !!val;
171 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
173 spec->add_stereo_mix_input = !!val;
174 /* the following two are just for compatibility */
175 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
177 spec->add_jack_modes = !!val;
178 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
180 spec->add_jack_modes = !!val;
181 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
183 spec->add_jack_modes = !!val;
184 val = snd_hda_get_bool_hint(codec, "power_down_unused");
186 spec->power_down_unused = !!val;
187 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
189 spec->hp_mic = !!val;
190 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
192 spec->suppress_hp_mic_detect = !val;
193 val = snd_hda_get_bool_hint(codec, "vmaster");
195 spec->suppress_vmaster = !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_write_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;
261 struct nid_path *path;
264 snd_array_for_each(&spec->paths, i, path) {
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_path_idx - get the index number corresponding to the path
281 * @codec: the HDA codec
282 * @path: nid_path object
284 * The returned index starts from 1, i.e. the actual array index with offset 1,
285 * and zero is handled as an invalid path
287 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
289 struct hda_gen_spec *spec = codec->spec;
290 struct nid_path *array = spec->paths.list;
293 if (!spec->paths.used)
296 if (idx < 0 || idx >= spec->paths.used)
300 EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
303 * snd_hda_get_path_from_idx - get the path instance corresponding to the
305 * @codec: the HDA codec
306 * @idx: the path index
308 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
310 struct hda_gen_spec *spec = codec->spec;
312 if (idx <= 0 || idx > spec->paths.used)
314 return snd_array_elem(&spec->paths, idx - 1);
316 EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
318 /* check whether the given DAC is already found in any existing paths */
319 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
321 struct hda_gen_spec *spec = codec->spec;
322 const struct nid_path *path;
325 snd_array_for_each(&spec->paths, i, path) {
326 if (path->path[0] == nid)
332 /* check whether the given two widgets can be connected */
333 static bool is_reachable_path(struct hda_codec *codec,
334 hda_nid_t from_nid, hda_nid_t to_nid)
336 if (!from_nid || !to_nid)
338 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
341 /* nid, dir and idx */
342 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
344 /* check whether the given ctl is already assigned in any path elements */
345 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
347 struct hda_gen_spec *spec = codec->spec;
348 const struct nid_path *path;
351 val &= AMP_VAL_COMPARE_MASK;
352 snd_array_for_each(&spec->paths, i, path) {
353 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
359 /* check whether a control with the given (nid, dir, idx) was assigned */
360 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
361 int dir, int idx, int type)
363 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
364 return is_ctl_used(codec, val, type);
367 static void print_nid_path(struct hda_codec *codec,
368 const char *pfx, struct nid_path *path)
375 for (i = 0; i < path->depth; i++)
376 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
377 pos != buf ? ":" : "",
380 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
383 /* called recursively */
384 static bool __parse_nid_path(struct hda_codec *codec,
385 hda_nid_t from_nid, hda_nid_t to_nid,
386 int anchor_nid, struct nid_path *path,
389 const hda_nid_t *conn;
392 if (to_nid == anchor_nid)
393 anchor_nid = 0; /* anchor passed */
394 else if (to_nid == (hda_nid_t)(-anchor_nid))
395 return false; /* hit the exclusive nid */
397 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
398 for (i = 0; i < nums; i++) {
399 if (conn[i] != from_nid) {
400 /* special case: when from_nid is 0,
401 * try to find an empty DAC
404 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
405 is_dac_already_used(codec, conn[i]))
408 /* anchor is not requested or already passed? */
412 if (depth >= MAX_NID_PATH_DEPTH)
414 for (i = 0; i < nums; i++) {
416 type = get_wcaps_type(get_wcaps(codec, conn[i]));
417 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
420 if (__parse_nid_path(codec, from_nid, conn[i],
421 anchor_nid, path, depth + 1))
427 path->path[path->depth] = conn[i];
428 path->idx[path->depth + 1] = i;
429 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
430 path->multi[path->depth + 1] = 1;
436 * snd_hda_parse_nid_path - parse the widget path from the given nid to
438 * @codec: the HDA codec
439 * @from_nid: the NID where the path start from
440 * @to_nid: the NID where the path ends at
441 * @anchor_nid: the anchor indication
442 * @path: the path object to store the result
444 * Returns true if a matching path is found.
446 * The parsing behavior depends on parameters:
447 * when @from_nid is 0, try to find an empty DAC;
448 * when @anchor_nid is set to a positive value, only paths through the widget
449 * with the given value are evaluated.
450 * when @anchor_nid is set to a negative value, paths through the widget
451 * with the negative of given value are excluded, only other paths are chosen.
452 * when @anchor_nid is zero, no special handling about path selection.
454 static bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
455 hda_nid_t to_nid, int anchor_nid,
456 struct nid_path *path)
458 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
459 path->path[path->depth] = to_nid;
467 * snd_hda_add_new_path - parse the path between the given NIDs and
468 * add to the path list
469 * @codec: the HDA codec
470 * @from_nid: the NID where the path start from
471 * @to_nid: the NID where the path ends at
472 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
474 * If no valid path is found, returns NULL.
477 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
478 hda_nid_t to_nid, int anchor_nid)
480 struct hda_gen_spec *spec = codec->spec;
481 struct nid_path *path;
483 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
486 /* check whether the path has been already added */
487 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
491 path = snd_array_new(&spec->paths);
494 memset(path, 0, sizeof(*path));
495 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
501 EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
503 /* clear the given path as invalid so that it won't be picked up later */
504 static void invalidate_nid_path(struct hda_codec *codec, int idx)
506 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
509 memset(path, 0, sizeof(*path));
512 /* return a DAC if paired to the given pin by codec driver */
513 static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
515 struct hda_gen_spec *spec = codec->spec;
516 const hda_nid_t *list = spec->preferred_dacs;
520 for (; *list; list += 2)
526 /* look for an empty DAC slot */
527 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
530 struct hda_gen_spec *spec = codec->spec;
534 for (i = 0; i < spec->num_all_dacs; i++) {
535 hda_nid_t nid = spec->all_dacs[i];
536 if (!nid || is_dac_already_used(codec, nid))
538 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
539 if (is_digital != cap_digital)
541 if (is_reachable_path(codec, nid, pin))
547 /* replace the channels in the composed amp value with the given number */
548 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
550 val &= ~(0x3U << 16);
555 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
556 hda_nid_t nid2, int dir)
558 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
559 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
560 return (query_amp_caps(codec, nid1, dir) ==
561 query_amp_caps(codec, nid2, dir));
564 /* look for a widget suitable for assigning a mute switch in the path */
565 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
566 struct nid_path *path)
570 for (i = path->depth - 1; i >= 0; i--) {
571 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
572 return path->path[i];
573 if (i != path->depth - 1 && i != 0 &&
574 nid_has_mute(codec, path->path[i], HDA_INPUT))
575 return path->path[i];
580 /* look for a widget suitable for assigning a volume ctl in the path */
581 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
582 struct nid_path *path)
584 struct hda_gen_spec *spec = codec->spec;
587 for (i = path->depth - 1; i >= 0; i--) {
588 hda_nid_t nid = path->path[i];
589 if ((spec->out_vol_mask >> nid) & 1)
591 if (nid_has_volume(codec, nid, HDA_OUTPUT))
598 * path activation / deactivation
601 /* can have the amp-in capability? */
602 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
604 hda_nid_t nid = path->path[idx];
605 unsigned int caps = get_wcaps(codec, nid);
606 unsigned int type = get_wcaps_type(caps);
608 if (!(caps & AC_WCAP_IN_AMP))
610 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
615 /* can have the amp-out capability? */
616 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
618 hda_nid_t nid = path->path[idx];
619 unsigned int caps = get_wcaps(codec, nid);
620 unsigned int type = get_wcaps_type(caps);
622 if (!(caps & AC_WCAP_OUT_AMP))
624 if (type == AC_WID_PIN && !idx) /* only for output pins */
629 /* check whether the given (nid,dir,idx) is active */
630 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
631 unsigned int dir, unsigned int idx)
633 struct hda_gen_spec *spec = codec->spec;
634 int type = get_wcaps_type(get_wcaps(codec, nid));
635 const struct nid_path *path;
638 if (nid == codec->core.afg)
641 snd_array_for_each(&spec->paths, n, path) {
644 if (codec->power_save_node) {
645 if (!path->stream_enabled)
647 /* ignore unplugged paths except for DAC/ADC */
648 if (!(path->pin_enabled || path->pin_fixed) &&
649 type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
652 for (i = 0; i < path->depth; i++) {
653 if (path->path[i] == nid) {
654 if (dir == HDA_OUTPUT || idx == -1 ||
664 /* check whether the NID is referred by any active paths */
665 #define is_active_nid_for_any(codec, nid) \
666 is_active_nid(codec, nid, HDA_OUTPUT, -1)
668 /* get the default amp value for the target state */
669 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
670 int dir, unsigned int caps, bool enable)
672 unsigned int val = 0;
674 if (caps & AC_AMPCAP_NUM_STEPS) {
677 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
679 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
686 /* is this a stereo widget or a stereo-to-mono mix? */
687 static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
689 unsigned int wcaps = get_wcaps(codec, nid);
692 if (wcaps & AC_WCAP_STEREO)
694 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
696 if (snd_hda_get_num_conns(codec, nid) != 1)
698 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
700 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
703 /* initialize the amp value (only at the first time) */
704 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
706 unsigned int caps = query_amp_caps(codec, nid, dir);
707 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
709 if (is_stereo_amps(codec, nid, dir))
710 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
712 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
715 /* update the amp, doing in stereo or mono depending on NID */
716 static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
717 unsigned int mask, unsigned int val)
719 if (is_stereo_amps(codec, nid, dir))
720 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
723 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
727 /* calculate amp value mask we can modify;
728 * if the given amp is controlled by mixers, don't touch it
730 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
731 hda_nid_t nid, int dir, int idx,
734 unsigned int mask = 0xff;
736 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
737 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
740 if (caps & AC_AMPCAP_NUM_STEPS) {
741 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
742 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
748 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
749 int idx, int idx_to_check, bool enable)
752 unsigned int mask, val;
754 caps = query_amp_caps(codec, nid, dir);
755 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
756 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
761 update_amp(codec, nid, dir, idx, mask, val);
764 static void check_and_activate_amp(struct hda_codec *codec, hda_nid_t nid,
765 int dir, int idx, int idx_to_check,
768 /* check whether the given amp is still used by others */
769 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
771 activate_amp(codec, nid, dir, idx, idx_to_check, enable);
774 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
777 hda_nid_t nid = path->path[i];
778 init_amp(codec, nid, HDA_OUTPUT, 0);
779 check_and_activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
782 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
783 int i, bool enable, bool add_aamix)
785 struct hda_gen_spec *spec = codec->spec;
786 const hda_nid_t *conn;
789 hda_nid_t nid = path->path[i];
791 nums = snd_hda_get_conn_list(codec, nid, &conn);
794 type = get_wcaps_type(get_wcaps(codec, nid));
795 if (type == AC_WID_PIN ||
796 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
802 for (n = 0; n < nums; n++)
803 init_amp(codec, nid, HDA_INPUT, n);
805 /* here is a little bit tricky in comparison with activate_amp_out();
806 * when aa-mixer is available, we need to enable the path as well
808 for (n = 0; n < nums; n++) {
810 if (conn[n] != spec->mixer_merge_nid)
812 /* when aamix is disabled, force to off */
814 activate_amp(codec, nid, HDA_INPUT, n, n, false);
818 check_and_activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
822 /* sync power of each widget in the given path */
823 static hda_nid_t path_power_update(struct hda_codec *codec,
824 struct nid_path *path,
825 bool allow_powerdown)
827 hda_nid_t nid, changed = 0;
830 for (i = 0; i < path->depth; i++) {
832 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
834 if (nid == codec->core.afg)
836 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
840 power = snd_hda_codec_read(codec, nid, 0,
841 AC_VERB_GET_POWER_STATE, 0);
842 if (power != (state | (state << 4))) {
843 snd_hda_codec_write(codec, nid, 0,
844 AC_VERB_SET_POWER_STATE, state);
846 /* all known codecs seem to be capable to handl
847 * widgets state even in D3, so far.
848 * if any new codecs need to restore the widget
849 * states after D0 transition, call the function
853 if (state == AC_PWRST_D0)
854 snd_hdac_regmap_sync_node(&codec->core, nid);
861 /* do sync with the last power state change */
862 static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
866 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
871 * snd_hda_activate_path - activate or deactivate the given path
872 * @codec: the HDA codec
873 * @path: the path to activate/deactivate
874 * @enable: flag to activate or not
875 * @add_aamix: enable the input from aamix NID
877 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
879 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
880 bool enable, bool add_aamix)
882 struct hda_gen_spec *spec = codec->spec;
885 path->active = enable;
887 /* make sure the widget is powered up */
888 if (enable && (spec->power_down_unused || codec->power_save_node))
889 path_power_update(codec, path, codec->power_save_node);
891 for (i = path->depth - 1; i >= 0; i--) {
892 hda_nid_t nid = path->path[i];
894 if (enable && path->multi[i])
895 snd_hda_codec_write_cache(codec, nid, 0,
896 AC_VERB_SET_CONNECT_SEL,
898 if (has_amp_in(codec, path, i))
899 activate_amp_in(codec, path, i, enable, add_aamix);
900 if (has_amp_out(codec, path, i))
901 activate_amp_out(codec, path, i, enable);
904 EXPORT_SYMBOL_GPL(snd_hda_activate_path);
906 /* if the given path is inactive, put widgets into D3 (only if suitable) */
907 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
909 struct hda_gen_spec *spec = codec->spec;
911 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
913 sync_power_state_change(codec, path_power_update(codec, path, true));
916 /* turn on/off EAPD on the given pin */
917 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
919 struct hda_gen_spec *spec = codec->spec;
920 if (spec->own_eapd_ctl ||
921 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
923 if (spec->keep_eapd_on && !enable)
927 snd_hda_codec_write_cache(codec, pin, 0,
928 AC_VERB_SET_EAPD_BTLENABLE,
929 enable ? 0x02 : 0x00);
932 /* re-initialize the path specified by the given path index */
933 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
935 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
937 snd_hda_activate_path(codec, path, path->active, false);
942 * Helper functions for creating mixer ctl elements
945 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
946 struct snd_ctl_elem_value *ucontrol);
947 static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
948 struct snd_ctl_elem_value *ucontrol);
949 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
950 struct snd_ctl_elem_value *ucontrol);
957 static const struct snd_kcontrol_new control_templates[] = {
958 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
959 /* only the put callback is replaced for handling the special mute */
961 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
962 .subdevice = HDA_SUBDEV_AMP_FLAG,
963 .info = snd_hda_mixer_amp_switch_info,
964 .get = snd_hda_mixer_amp_switch_get,
965 .put = hda_gen_mixer_mute_put, /* replaced */
966 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
969 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
970 .info = snd_hda_mixer_amp_switch_info,
971 .get = hda_gen_bind_mute_get,
972 .put = hda_gen_bind_mute_put, /* replaced */
973 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
977 /* add dynamic controls from template */
978 static struct snd_kcontrol_new *
979 add_control(struct hda_gen_spec *spec, int type, const char *name,
980 int cidx, unsigned long val)
982 struct snd_kcontrol_new *knew;
984 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
988 if (get_amp_nid_(val))
989 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
990 if (knew->access == 0)
991 knew->access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
992 knew->private_value = val;
996 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
997 const char *pfx, const char *dir,
998 const char *sfx, int cidx, unsigned long val)
1000 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1001 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
1002 if (!add_control(spec, type, name, cidx, val))
1007 #define add_pb_vol_ctrl(spec, type, pfx, val) \
1008 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1009 #define add_pb_sw_ctrl(spec, type, pfx, val) \
1010 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1011 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1012 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1013 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1014 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1016 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1017 unsigned int chs, struct nid_path *path)
1022 val = path->ctls[NID_PATH_VOL_CTL];
1025 val = amp_val_replace_channels(val, chs);
1026 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1029 /* return the channel bits suitable for the given path->ctls[] */
1030 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1033 int chs = 1; /* mono (left only) */
1035 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1036 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1037 chs = 3; /* stereo */
1042 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1043 struct nid_path *path)
1045 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1046 return add_vol_ctl(codec, pfx, cidx, chs, path);
1049 /* create a mute-switch for the given mixer widget;
1050 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1052 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1053 unsigned int chs, struct nid_path *path)
1056 int type = HDA_CTL_WIDGET_MUTE;
1060 val = path->ctls[NID_PATH_MUTE_CTL];
1063 val = amp_val_replace_channels(val, chs);
1064 if (get_amp_direction_(val) == HDA_INPUT) {
1065 hda_nid_t nid = get_amp_nid_(val);
1066 int nums = snd_hda_get_num_conns(codec, nid);
1068 type = HDA_CTL_BIND_MUTE;
1072 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1075 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1076 int cidx, struct nid_path *path)
1078 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1079 return add_sw_ctl(codec, pfx, cidx, chs, path);
1082 /* playback mute control with the software mute bit check */
1083 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1084 struct snd_ctl_elem_value *ucontrol)
1086 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1087 struct hda_gen_spec *spec = codec->spec;
1089 if (spec->auto_mute_via_amp) {
1090 hda_nid_t nid = get_amp_nid(kcontrol);
1091 bool enabled = !((spec->mute_bits >> nid) & 1);
1092 ucontrol->value.integer.value[0] &= enabled;
1093 ucontrol->value.integer.value[1] &= enabled;
1097 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1098 struct snd_ctl_elem_value *ucontrol)
1100 sync_auto_mute_bits(kcontrol, ucontrol);
1101 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1105 * Bound mute controls
1107 #define AMP_VAL_IDX_SHIFT 19
1108 #define AMP_VAL_IDX_MASK (0x0f<<19)
1110 static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
1111 struct snd_ctl_elem_value *ucontrol)
1113 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1117 mutex_lock(&codec->control_mutex);
1118 pval = kcontrol->private_value;
1119 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1120 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1121 kcontrol->private_value = pval;
1122 mutex_unlock(&codec->control_mutex);
1126 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1127 struct snd_ctl_elem_value *ucontrol)
1129 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1131 int i, indices, err = 0, change = 0;
1133 sync_auto_mute_bits(kcontrol, ucontrol);
1135 mutex_lock(&codec->control_mutex);
1136 pval = kcontrol->private_value;
1137 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1138 for (i = 0; i < indices; i++) {
1139 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1140 (i << AMP_VAL_IDX_SHIFT);
1141 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1146 kcontrol->private_value = pval;
1147 mutex_unlock(&codec->control_mutex);
1148 return err < 0 ? err : change;
1151 /* any ctl assigned to the path with the given index? */
1152 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1154 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1155 return path && path->ctls[ctl_type];
1158 static const char * const channel_name[4] = {
1159 "Front", "Surround", "CLFE", "Side"
1162 /* give some appropriate ctl name prefix for the given line out channel */
1163 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1164 int *index, int ctl_type)
1166 struct hda_gen_spec *spec = codec->spec;
1167 struct auto_pin_cfg *cfg = &spec->autocfg;
1170 if (cfg->line_outs == 1 && !spec->multi_ios &&
1171 !codec->force_pin_prefix &&
1172 !cfg->hp_outs && !cfg->speaker_outs)
1173 return spec->vmaster_mute.hook ? "PCM" : "Master";
1175 /* if there is really a single DAC used in the whole output paths,
1176 * use it master (or "PCM" if a vmaster hook is present)
1178 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1179 !codec->force_pin_prefix &&
1180 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1181 return spec->vmaster_mute.hook ? "PCM" : "Master";
1183 /* multi-io channels */
1184 if (ch >= cfg->line_outs)
1185 return channel_name[ch];
1187 switch (cfg->line_out_type) {
1188 case AUTO_PIN_SPEAKER_OUT:
1189 /* if the primary channel vol/mute is shared with HP volume,
1190 * don't name it as Speaker
1192 if (!ch && cfg->hp_outs &&
1193 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1195 if (cfg->line_outs == 1)
1197 if (cfg->line_outs == 2)
1198 return ch ? "Bass Speaker" : "Speaker";
1200 case AUTO_PIN_HP_OUT:
1201 /* if the primary channel vol/mute is shared with spk volume,
1202 * don't name it as Headphone
1204 if (!ch && cfg->speaker_outs &&
1205 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1207 /* for multi-io case, only the primary out */
1208 if (ch && spec->multi_ios)
1212 case AUTO_PIN_LINE_OUT:
1213 /* This deals with the case where one HP or one Speaker or
1214 * one HP + one Speaker need to share the DAC with LO
1217 bool hp_lo_shared = false, spk_lo_shared = false;
1219 if (cfg->speaker_outs)
1220 spk_lo_shared = !path_has_mixer(codec,
1221 spec->speaker_paths[0], ctl_type);
1223 hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1224 if (hp_lo_shared && spk_lo_shared)
1225 return spec->vmaster_mute.hook ? "PCM" : "Master";
1227 return "Headphone+LO";
1229 return "Speaker+LO";
1233 /* for a single channel output, we don't have to name the channel */
1234 if (cfg->line_outs == 1 && !spec->multi_ios)
1237 if (ch >= ARRAY_SIZE(channel_name)) {
1242 return channel_name[ch];
1246 * Parse output paths
1249 /* badness definition */
1251 /* No primary DAC is found for the main output */
1252 BAD_NO_PRIMARY_DAC = 0x10000,
1253 /* No DAC is found for the extra output */
1254 BAD_NO_DAC = 0x4000,
1255 /* No possible multi-ios */
1256 BAD_MULTI_IO = 0x120,
1257 /* No individual DAC for extra output */
1258 BAD_NO_EXTRA_DAC = 0x102,
1259 /* No individual DAC for extra surrounds */
1260 BAD_NO_EXTRA_SURR_DAC = 0x101,
1261 /* Primary DAC shared with main surrounds */
1262 BAD_SHARED_SURROUND = 0x100,
1263 /* No independent HP possible */
1264 BAD_NO_INDEP_HP = 0x10,
1265 /* Primary DAC shared with main CLFE */
1266 BAD_SHARED_CLFE = 0x10,
1267 /* Primary DAC shared with extra surrounds */
1268 BAD_SHARED_EXTRA_SURROUND = 0x10,
1269 /* Volume widget is shared */
1270 BAD_SHARED_VOL = 0x10,
1273 /* look for widgets in the given path which are appropriate for
1274 * volume and mute controls, and assign the values to ctls[].
1276 * When no appropriate widget is found in the path, the badness value
1277 * is incremented depending on the situation. The function returns the
1278 * total badness for both volume and mute controls.
1280 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1282 struct hda_gen_spec *spec = codec->spec;
1288 return BAD_SHARED_VOL * 2;
1290 if (path->ctls[NID_PATH_VOL_CTL] ||
1291 path->ctls[NID_PATH_MUTE_CTL])
1292 return 0; /* already evaluated */
1294 nid = look_for_out_vol_nid(codec, path);
1296 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1297 if (spec->dac_min_mute)
1298 val |= HDA_AMP_VAL_MIN_MUTE;
1299 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1300 badness += BAD_SHARED_VOL;
1302 path->ctls[NID_PATH_VOL_CTL] = val;
1304 badness += BAD_SHARED_VOL;
1305 nid = look_for_out_mute_nid(codec, path);
1307 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1308 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1309 nid_has_mute(codec, nid, HDA_OUTPUT))
1310 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1312 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1313 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1314 badness += BAD_SHARED_VOL;
1316 path->ctls[NID_PATH_MUTE_CTL] = val;
1318 badness += BAD_SHARED_VOL;
1322 const struct badness_table hda_main_out_badness = {
1323 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1324 .no_dac = BAD_NO_DAC,
1325 .shared_primary = BAD_NO_PRIMARY_DAC,
1326 .shared_surr = BAD_SHARED_SURROUND,
1327 .shared_clfe = BAD_SHARED_CLFE,
1328 .shared_surr_main = BAD_SHARED_SURROUND,
1330 EXPORT_SYMBOL_GPL(hda_main_out_badness);
1332 const struct badness_table hda_extra_out_badness = {
1333 .no_primary_dac = BAD_NO_DAC,
1334 .no_dac = BAD_NO_DAC,
1335 .shared_primary = BAD_NO_EXTRA_DAC,
1336 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1337 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1338 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1340 EXPORT_SYMBOL_GPL(hda_extra_out_badness);
1342 /* get the DAC of the primary output corresponding to the given array index */
1343 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1345 struct hda_gen_spec *spec = codec->spec;
1346 struct auto_pin_cfg *cfg = &spec->autocfg;
1348 if (cfg->line_outs > idx)
1349 return spec->private_dac_nids[idx];
1350 idx -= cfg->line_outs;
1351 if (spec->multi_ios > idx)
1352 return spec->multi_io[idx].dac;
1356 /* return the DAC if it's reachable, otherwise zero */
1357 static inline hda_nid_t try_dac(struct hda_codec *codec,
1358 hda_nid_t dac, hda_nid_t pin)
1360 return is_reachable_path(codec, dac, pin) ? dac : 0;
1363 /* try to assign DACs to pins and return the resultant badness */
1364 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1365 const hda_nid_t *pins, hda_nid_t *dacs,
1367 const struct badness_table *bad)
1369 struct hda_gen_spec *spec = codec->spec;
1377 for (i = 0; i < num_outs; i++) {
1378 struct nid_path *path;
1379 hda_nid_t pin = pins[i];
1381 if (!spec->obey_preferred_dacs) {
1382 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1384 badness += assign_out_path_ctls(codec, path);
1389 dacs[i] = get_preferred_dac(codec, pin);
1391 if (is_dac_already_used(codec, dacs[i]))
1392 badness += bad->shared_primary;
1393 } else if (spec->obey_preferred_dacs) {
1394 badness += BAD_NO_PRIMARY_DAC;
1398 dacs[i] = look_for_dac(codec, pin, false);
1399 if (!dacs[i] && !i) {
1400 /* try to steal the DAC of surrounds for the front */
1401 for (j = 1; j < num_outs; j++) {
1402 if (is_reachable_path(codec, dacs[j], pin)) {
1405 invalidate_nid_path(codec, path_idx[j]);
1414 dac = try_dac(codec, get_primary_out(codec, i), pin);
1416 dac = try_dac(codec, dacs[0], pin);
1418 dac = try_dac(codec, get_primary_out(codec, i), pin);
1421 badness += bad->shared_primary;
1423 badness += bad->shared_surr;
1425 badness += bad->shared_clfe;
1426 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1427 dac = spec->private_dac_nids[0];
1428 badness += bad->shared_surr_main;
1430 badness += bad->no_primary_dac;
1432 badness += bad->no_dac;
1436 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1437 if (!path && !i && spec->mixer_nid) {
1438 /* try with aamix */
1439 path = snd_hda_add_new_path(codec, dac, pin, 0);
1443 badness += bad->no_dac;
1445 /* print_nid_path(codec, "output", path); */
1446 path->active = true;
1447 path_idx[i] = snd_hda_get_path_idx(codec, path);
1448 badness += assign_out_path_ctls(codec, path);
1455 /* return NID if the given pin has only a single connection to a certain DAC */
1456 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1458 struct hda_gen_spec *spec = codec->spec;
1460 hda_nid_t nid_found = 0;
1462 for (i = 0; i < spec->num_all_dacs; i++) {
1463 hda_nid_t nid = spec->all_dacs[i];
1464 if (!nid || is_dac_already_used(codec, nid))
1466 if (is_reachable_path(codec, nid, pin)) {
1475 /* check whether the given pin can be a multi-io pin */
1476 static bool can_be_multiio_pin(struct hda_codec *codec,
1477 unsigned int location, hda_nid_t nid)
1479 unsigned int defcfg, caps;
1481 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1482 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1484 if (location && get_defcfg_location(defcfg) != location)
1486 caps = snd_hda_query_pin_caps(codec, nid);
1487 if (!(caps & AC_PINCAP_OUT))
1492 /* count the number of input pins that are capable to be multi-io */
1493 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1495 struct hda_gen_spec *spec = codec->spec;
1496 struct auto_pin_cfg *cfg = &spec->autocfg;
1497 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1498 unsigned int location = get_defcfg_location(defcfg);
1502 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1503 for (i = 0; i < cfg->num_inputs; i++) {
1504 if (cfg->inputs[i].type != type)
1506 if (can_be_multiio_pin(codec, location,
1507 cfg->inputs[i].pin))
1517 * When hardwired is set, try to fill ony hardwired pins, and returns
1518 * zero if any pins are filled, non-zero if nothing found.
1519 * When hardwired is off, try to fill possible input pins, and returns
1520 * the badness value.
1522 static int fill_multi_ios(struct hda_codec *codec,
1523 hda_nid_t reference_pin,
1526 struct hda_gen_spec *spec = codec->spec;
1527 struct auto_pin_cfg *cfg = &spec->autocfg;
1528 int type, i, j, num_pins, old_pins;
1529 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1530 unsigned int location = get_defcfg_location(defcfg);
1532 struct nid_path *path;
1534 old_pins = spec->multi_ios;
1538 num_pins = count_multiio_pins(codec, reference_pin);
1542 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1543 for (i = 0; i < cfg->num_inputs; i++) {
1544 hda_nid_t nid = cfg->inputs[i].pin;
1547 if (cfg->inputs[i].type != type)
1549 if (!can_be_multiio_pin(codec, location, nid))
1551 for (j = 0; j < spec->multi_ios; j++) {
1552 if (nid == spec->multi_io[j].pin)
1555 if (j < spec->multi_ios)
1559 dac = get_dac_if_single(codec, nid);
1561 dac = look_for_dac(codec, nid, false);
1566 path = snd_hda_add_new_path(codec, dac, nid,
1572 /* print_nid_path(codec, "multiio", path); */
1573 spec->multi_io[spec->multi_ios].pin = nid;
1574 spec->multi_io[spec->multi_ios].dac = dac;
1575 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1576 snd_hda_get_path_idx(codec, path);
1578 if (spec->multi_ios >= 2)
1584 badness = BAD_MULTI_IO;
1585 if (old_pins == spec->multi_ios) {
1587 return 1; /* nothing found */
1589 return badness; /* no badness if nothing found */
1591 if (!hardwired && spec->multi_ios < 2) {
1592 /* cancel newly assigned paths */
1593 spec->paths.used -= spec->multi_ios - old_pins;
1594 spec->multi_ios = old_pins;
1598 /* assign volume and mute controls */
1599 for (i = old_pins; i < spec->multi_ios; i++) {
1600 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1601 badness += assign_out_path_ctls(codec, path);
1607 /* map DACs for all pins in the list if they are single connections */
1608 static bool map_singles(struct hda_codec *codec, int outs,
1609 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1611 struct hda_gen_spec *spec = codec->spec;
1614 for (i = 0; i < outs; i++) {
1615 struct nid_path *path;
1619 dac = get_dac_if_single(codec, pins[i]);
1622 path = snd_hda_add_new_path(codec, dac, pins[i],
1624 if (!path && !i && spec->mixer_nid)
1625 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1629 /* print_nid_path(codec, "output", path); */
1630 path->active = true;
1631 path_idx[i] = snd_hda_get_path_idx(codec, path);
1637 static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1639 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1640 spec->aamix_out_paths[2];
1643 /* create a new path including aamix if available, and return its index */
1644 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1646 struct hda_gen_spec *spec = codec->spec;
1647 struct nid_path *path;
1648 hda_nid_t path_dac, dac, pin;
1650 path = snd_hda_get_path_from_idx(codec, path_idx);
1651 if (!path || !path->depth ||
1652 is_nid_contained(path, spec->mixer_nid))
1654 path_dac = path->path[0];
1655 dac = spec->private_dac_nids[0];
1656 pin = path->path[path->depth - 1];
1657 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1659 if (dac != path_dac)
1661 else if (spec->multiout.hp_out_nid[0])
1662 dac = spec->multiout.hp_out_nid[0];
1663 else if (spec->multiout.extra_out_nid[0])
1664 dac = spec->multiout.extra_out_nid[0];
1668 path = snd_hda_add_new_path(codec, dac, pin,
1673 /* print_nid_path(codec, "output-aamix", path); */
1674 path->active = false; /* unused as default */
1675 path->pin_fixed = true; /* static route */
1676 return snd_hda_get_path_idx(codec, path);
1679 /* check whether the independent HP is available with the current config */
1680 static bool indep_hp_possible(struct hda_codec *codec)
1682 struct hda_gen_spec *spec = codec->spec;
1683 struct auto_pin_cfg *cfg = &spec->autocfg;
1684 struct nid_path *path;
1687 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1688 idx = spec->out_paths[0];
1690 idx = spec->hp_paths[0];
1691 path = snd_hda_get_path_from_idx(codec, idx);
1695 /* assume no path conflicts unless aamix is involved */
1696 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1699 /* check whether output paths contain aamix */
1700 for (i = 0; i < cfg->line_outs; i++) {
1701 if (spec->out_paths[i] == idx)
1703 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1704 if (path && is_nid_contained(path, spec->mixer_nid))
1707 for (i = 0; i < cfg->speaker_outs; i++) {
1708 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1709 if (path && is_nid_contained(path, spec->mixer_nid))
1716 /* fill the empty entries in the dac array for speaker/hp with the
1717 * shared dac pointed by the paths
1719 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1720 hda_nid_t *dacs, int *path_idx)
1722 struct nid_path *path;
1725 for (i = 0; i < num_outs; i++) {
1728 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1731 dacs[i] = path->path[0];
1735 /* fill in the dac_nids table from the parsed pin configuration */
1736 static int fill_and_eval_dacs(struct hda_codec *codec,
1737 bool fill_hardwired,
1738 bool fill_mio_first)
1740 struct hda_gen_spec *spec = codec->spec;
1741 struct auto_pin_cfg *cfg = &spec->autocfg;
1742 int i, err, badness;
1744 /* set num_dacs once to full for look_for_dac() */
1745 spec->multiout.num_dacs = cfg->line_outs;
1746 spec->multiout.dac_nids = spec->private_dac_nids;
1747 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1748 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1749 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1750 spec->multi_ios = 0;
1751 snd_array_free(&spec->paths);
1753 /* clear path indices */
1754 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1755 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1756 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1757 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1758 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1759 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1760 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1761 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1765 /* fill hard-wired DACs first */
1766 if (fill_hardwired) {
1769 mapped = map_singles(codec, cfg->line_outs,
1771 spec->private_dac_nids,
1773 mapped |= map_singles(codec, cfg->hp_outs,
1775 spec->multiout.hp_out_nid,
1777 mapped |= map_singles(codec, cfg->speaker_outs,
1779 spec->multiout.extra_out_nid,
1780 spec->speaker_paths);
1781 if (!spec->no_multi_io &&
1782 fill_mio_first && cfg->line_outs == 1 &&
1783 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1784 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1791 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1792 spec->private_dac_nids, spec->out_paths,
1793 spec->main_out_badness);
1795 if (!spec->no_multi_io && fill_mio_first &&
1796 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1797 /* try to fill multi-io first */
1798 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1801 /* we don't count badness at this stage yet */
1804 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1805 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1806 spec->multiout.hp_out_nid,
1808 spec->extra_out_badness);
1813 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1814 err = try_assign_dacs(codec, cfg->speaker_outs,
1816 spec->multiout.extra_out_nid,
1817 spec->speaker_paths,
1818 spec->extra_out_badness);
1823 if (!spec->no_multi_io &&
1824 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1825 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1831 if (spec->mixer_nid) {
1832 spec->aamix_out_paths[0] =
1833 check_aamix_out_path(codec, spec->out_paths[0]);
1834 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1835 spec->aamix_out_paths[1] =
1836 check_aamix_out_path(codec, spec->hp_paths[0]);
1837 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1838 spec->aamix_out_paths[2] =
1839 check_aamix_out_path(codec, spec->speaker_paths[0]);
1842 if (!spec->no_multi_io &&
1843 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1844 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1845 spec->multi_ios = 1; /* give badness */
1847 /* re-count num_dacs and squash invalid entries */
1848 spec->multiout.num_dacs = 0;
1849 for (i = 0; i < cfg->line_outs; i++) {
1850 if (spec->private_dac_nids[i])
1851 spec->multiout.num_dacs++;
1853 memmove(spec->private_dac_nids + i,
1854 spec->private_dac_nids + i + 1,
1855 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1856 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1860 spec->ext_channel_count = spec->min_channel_count =
1861 spec->multiout.num_dacs * 2;
1863 if (spec->multi_ios == 2) {
1864 for (i = 0; i < 2; i++)
1865 spec->private_dac_nids[spec->multiout.num_dacs++] =
1866 spec->multi_io[i].dac;
1867 } else if (spec->multi_ios) {
1868 spec->multi_ios = 0;
1869 badness += BAD_MULTI_IO;
1872 if (spec->indep_hp && !indep_hp_possible(codec))
1873 badness += BAD_NO_INDEP_HP;
1875 /* re-fill the shared DAC for speaker / headphone */
1876 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1877 refill_shared_dacs(codec, cfg->hp_outs,
1878 spec->multiout.hp_out_nid,
1880 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1881 refill_shared_dacs(codec, cfg->speaker_outs,
1882 spec->multiout.extra_out_nid,
1883 spec->speaker_paths);
1888 #define DEBUG_BADNESS
1890 #ifdef DEBUG_BADNESS
1891 #define debug_badness(fmt, ...) \
1892 codec_dbg(codec, fmt, ##__VA_ARGS__)
1894 #define debug_badness(fmt, ...) \
1895 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1898 #ifdef DEBUG_BADNESS
1899 static inline void print_nid_path_idx(struct hda_codec *codec,
1900 const char *pfx, int idx)
1902 struct nid_path *path;
1904 path = snd_hda_get_path_from_idx(codec, idx);
1906 print_nid_path(codec, pfx, path);
1909 static void debug_show_configs(struct hda_codec *codec,
1910 struct auto_pin_cfg *cfg)
1912 struct hda_gen_spec *spec = codec->spec;
1913 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1916 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1917 cfg->line_out_pins[0], cfg->line_out_pins[1],
1918 cfg->line_out_pins[2], cfg->line_out_pins[3],
1919 spec->multiout.dac_nids[0],
1920 spec->multiout.dac_nids[1],
1921 spec->multiout.dac_nids[2],
1922 spec->multiout.dac_nids[3],
1923 lo_type[cfg->line_out_type]);
1924 for (i = 0; i < cfg->line_outs; i++)
1925 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1926 if (spec->multi_ios > 0)
1927 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1929 spec->multi_io[0].pin, spec->multi_io[1].pin,
1930 spec->multi_io[0].dac, spec->multi_io[1].dac);
1931 for (i = 0; i < spec->multi_ios; i++)
1932 print_nid_path_idx(codec, " mio",
1933 spec->out_paths[cfg->line_outs + i]);
1935 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1936 cfg->hp_pins[0], cfg->hp_pins[1],
1937 cfg->hp_pins[2], cfg->hp_pins[3],
1938 spec->multiout.hp_out_nid[0],
1939 spec->multiout.hp_out_nid[1],
1940 spec->multiout.hp_out_nid[2],
1941 spec->multiout.hp_out_nid[3]);
1942 for (i = 0; i < cfg->hp_outs; i++)
1943 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1944 if (cfg->speaker_outs)
1945 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1946 cfg->speaker_pins[0], cfg->speaker_pins[1],
1947 cfg->speaker_pins[2], cfg->speaker_pins[3],
1948 spec->multiout.extra_out_nid[0],
1949 spec->multiout.extra_out_nid[1],
1950 spec->multiout.extra_out_nid[2],
1951 spec->multiout.extra_out_nid[3]);
1952 for (i = 0; i < cfg->speaker_outs; i++)
1953 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1954 for (i = 0; i < 3; i++)
1955 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1958 #define debug_show_configs(codec, cfg) /* NOP */
1961 /* find all available DACs of the codec */
1962 static void fill_all_dac_nids(struct hda_codec *codec)
1964 struct hda_gen_spec *spec = codec->spec;
1967 spec->num_all_dacs = 0;
1968 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1969 for_each_hda_codec_node(nid, codec) {
1970 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1972 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1973 codec_err(codec, "Too many DACs!\n");
1976 spec->all_dacs[spec->num_all_dacs++] = nid;
1980 static int parse_output_paths(struct hda_codec *codec)
1982 struct hda_gen_spec *spec = codec->spec;
1983 struct auto_pin_cfg *cfg = &spec->autocfg;
1984 struct auto_pin_cfg *best_cfg;
1986 int best_badness = INT_MAX;
1988 bool fill_hardwired = true, fill_mio_first = true;
1989 bool best_wired = true, best_mio = true;
1990 bool hp_spk_swapped = false;
1992 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1998 badness = fill_and_eval_dacs(codec, fill_hardwired,
2004 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
2005 cfg->line_out_type, fill_hardwired, fill_mio_first,
2007 debug_show_configs(codec, cfg);
2008 if (badness < best_badness) {
2009 best_badness = badness;
2011 best_wired = fill_hardwired;
2012 best_mio = fill_mio_first;
2016 fill_mio_first = !fill_mio_first;
2017 if (!fill_mio_first)
2019 fill_hardwired = !fill_hardwired;
2020 if (!fill_hardwired)
2024 hp_spk_swapped = true;
2025 if (cfg->speaker_outs > 0 &&
2026 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2027 cfg->hp_outs = cfg->line_outs;
2028 memcpy(cfg->hp_pins, cfg->line_out_pins,
2029 sizeof(cfg->hp_pins));
2030 cfg->line_outs = cfg->speaker_outs;
2031 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2032 sizeof(cfg->speaker_pins));
2033 cfg->speaker_outs = 0;
2034 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2035 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2036 fill_hardwired = true;
2039 if (cfg->hp_outs > 0 &&
2040 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2041 cfg->speaker_outs = cfg->line_outs;
2042 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2043 sizeof(cfg->speaker_pins));
2044 cfg->line_outs = cfg->hp_outs;
2045 memcpy(cfg->line_out_pins, cfg->hp_pins,
2046 sizeof(cfg->hp_pins));
2048 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2049 cfg->line_out_type = AUTO_PIN_HP_OUT;
2050 fill_hardwired = true;
2057 debug_badness("==> restoring best_cfg\n");
2059 fill_and_eval_dacs(codec, best_wired, best_mio);
2061 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2062 cfg->line_out_type, best_wired, best_mio);
2063 debug_show_configs(codec, cfg);
2065 if (cfg->line_out_pins[0]) {
2066 struct nid_path *path;
2067 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
2069 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
2070 if (spec->vmaster_nid) {
2071 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2072 HDA_OUTPUT, spec->vmaster_tlv);
2073 if (spec->dac_min_mute)
2074 spec->vmaster_tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] |= TLV_DB_SCALE_MUTE;
2078 /* set initial pinctl targets */
2079 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2083 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2084 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2085 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2086 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2087 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2088 set_pin_targets(codec, cfg->speaker_outs,
2089 cfg->speaker_pins, val);
2092 /* clear indep_hp flag if not available */
2093 if (spec->indep_hp && !indep_hp_possible(codec))
2100 /* add playback controls from the parsed DAC table */
2101 static int create_multi_out_ctls(struct hda_codec *codec,
2102 const struct auto_pin_cfg *cfg)
2104 struct hda_gen_spec *spec = codec->spec;
2105 int i, err, noutputs;
2107 noutputs = cfg->line_outs;
2108 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2109 noutputs += spec->multi_ios;
2111 for (i = 0; i < noutputs; i++) {
2114 struct nid_path *path;
2116 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
2120 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
2121 if (!name || !strcmp(name, "CLFE")) {
2123 err = add_vol_ctl(codec, "Center", 0, 1, path);
2126 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2130 err = add_stereo_vol(codec, name, index, path);
2135 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2136 if (!name || !strcmp(name, "CLFE")) {
2137 err = add_sw_ctl(codec, "Center", 0, 1, path);
2140 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2144 err = add_stereo_sw(codec, name, index, path);
2152 static int create_extra_out(struct hda_codec *codec, int path_idx,
2153 const char *pfx, int cidx)
2155 struct nid_path *path;
2158 path = snd_hda_get_path_from_idx(codec, path_idx);
2161 err = add_stereo_vol(codec, pfx, cidx, path);
2164 err = add_stereo_sw(codec, pfx, cidx, path);
2170 /* add playback controls for speaker and HP outputs */
2171 static int create_extra_outs(struct hda_codec *codec, int num_pins,
2172 const int *paths, const char *pfx)
2176 for (i = 0; i < num_pins; i++) {
2178 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2181 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2182 name = "Bass Speaker";
2183 else if (num_pins >= 3) {
2184 snprintf(tmp, sizeof(tmp), "%s %s",
2185 pfx, channel_name[i]);
2191 err = create_extra_out(codec, paths[i], name, idx);
2198 static int create_hp_out_ctls(struct hda_codec *codec)
2200 struct hda_gen_spec *spec = codec->spec;
2201 return create_extra_outs(codec, spec->autocfg.hp_outs,
2206 static int create_speaker_out_ctls(struct hda_codec *codec)
2208 struct hda_gen_spec *spec = codec->spec;
2209 return create_extra_outs(codec, spec->autocfg.speaker_outs,
2210 spec->speaker_paths,
2215 * independent HP controls
2218 static void call_hp_automute(struct hda_codec *codec,
2219 struct hda_jack_callback *jack);
2220 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2221 struct snd_ctl_elem_info *uinfo)
2223 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2226 static int indep_hp_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] = spec->indep_hp_enabled;
2235 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2236 int nomix_path_idx, int mix_path_idx,
2239 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2240 struct snd_ctl_elem_value *ucontrol)
2242 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2243 struct hda_gen_spec *spec = codec->spec;
2244 unsigned int select = ucontrol->value.enumerated.item[0];
2247 mutex_lock(&spec->pcm_mutex);
2248 if (spec->active_streams) {
2253 if (spec->indep_hp_enabled != select) {
2255 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2256 dacp = &spec->private_dac_nids[0];
2258 dacp = &spec->multiout.hp_out_nid[0];
2260 /* update HP aamix paths in case it conflicts with indep HP */
2261 if (spec->have_aamix_ctl) {
2262 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2263 update_aamix_paths(codec, spec->aamix_mode,
2265 spec->aamix_out_paths[0],
2266 spec->autocfg.line_out_type);
2268 update_aamix_paths(codec, spec->aamix_mode,
2270 spec->aamix_out_paths[1],
2274 spec->indep_hp_enabled = select;
2275 if (spec->indep_hp_enabled)
2278 *dacp = spec->alt_dac_nid;
2280 call_hp_automute(codec, NULL);
2284 mutex_unlock(&spec->pcm_mutex);
2288 static const struct snd_kcontrol_new indep_hp_ctl = {
2289 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2290 .name = "Independent HP",
2291 .info = indep_hp_info,
2292 .get = indep_hp_get,
2293 .put = indep_hp_put,
2297 static int create_indep_hp_ctls(struct hda_codec *codec)
2299 struct hda_gen_spec *spec = codec->spec;
2302 if (!spec->indep_hp)
2304 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2305 dac = spec->multiout.dac_nids[0];
2307 dac = spec->multiout.hp_out_nid[0];
2313 spec->indep_hp_enabled = false;
2314 spec->alt_dac_nid = dac;
2315 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2321 * channel mode enum control
2324 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2325 struct snd_ctl_elem_info *uinfo)
2327 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2328 struct hda_gen_spec *spec = codec->spec;
2331 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2333 uinfo->value.enumerated.items = spec->multi_ios + 1;
2334 if (uinfo->value.enumerated.item > spec->multi_ios)
2335 uinfo->value.enumerated.item = spec->multi_ios;
2336 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2337 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2341 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2342 struct snd_ctl_elem_value *ucontrol)
2344 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2345 struct hda_gen_spec *spec = codec->spec;
2346 ucontrol->value.enumerated.item[0] =
2347 (spec->ext_channel_count - spec->min_channel_count) / 2;
2351 static inline struct nid_path *
2352 get_multiio_path(struct hda_codec *codec, int idx)
2354 struct hda_gen_spec *spec = codec->spec;
2355 return snd_hda_get_path_from_idx(codec,
2356 spec->out_paths[spec->autocfg.line_outs + idx]);
2359 static void update_automute_all(struct hda_codec *codec);
2361 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2362 * used for output paths
2364 static bool aamix_default(struct hda_gen_spec *spec)
2366 return !spec->have_aamix_ctl || spec->aamix_mode;
2369 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2371 struct hda_gen_spec *spec = codec->spec;
2372 hda_nid_t nid = spec->multi_io[idx].pin;
2373 struct nid_path *path;
2375 path = get_multiio_path(codec, idx);
2379 if (path->active == output)
2383 set_pin_target(codec, nid, PIN_OUT, true);
2384 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2385 set_pin_eapd(codec, nid, true);
2387 set_pin_eapd(codec, nid, false);
2388 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2389 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2390 path_power_down_sync(codec, path);
2393 /* update jack retasking in case it modifies any of them */
2394 update_automute_all(codec);
2399 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2400 struct snd_ctl_elem_value *ucontrol)
2402 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2403 struct hda_gen_spec *spec = codec->spec;
2406 ch = ucontrol->value.enumerated.item[0];
2407 if (ch < 0 || ch > spec->multi_ios)
2409 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2411 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2412 for (i = 0; i < spec->multi_ios; i++)
2413 set_multi_io(codec, i, i < ch);
2414 spec->multiout.max_channels = max(spec->ext_channel_count,
2415 spec->const_channel_count);
2416 if (spec->need_dac_fix)
2417 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2421 static const struct snd_kcontrol_new channel_mode_enum = {
2422 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2423 .name = "Channel Mode",
2424 .info = ch_mode_info,
2429 static int create_multi_channel_mode(struct hda_codec *codec)
2431 struct hda_gen_spec *spec = codec->spec;
2433 if (spec->multi_ios > 0) {
2434 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2441 * aamix loopback enable/disable switch
2444 #define loopback_mixing_info indep_hp_info
2446 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2447 struct snd_ctl_elem_value *ucontrol)
2449 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2450 struct hda_gen_spec *spec = codec->spec;
2451 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2455 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2456 int nomix_path_idx, int mix_path_idx,
2459 struct hda_gen_spec *spec = codec->spec;
2460 struct nid_path *nomix_path, *mix_path;
2462 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2463 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2464 if (!nomix_path || !mix_path)
2467 /* if HP aamix path is driven from a different DAC and the
2468 * independent HP mode is ON, can't turn on aamix path
2470 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2471 mix_path->path[0] != spec->alt_dac_nid)
2475 snd_hda_activate_path(codec, nomix_path, false, true);
2476 snd_hda_activate_path(codec, mix_path, true, true);
2477 path_power_down_sync(codec, nomix_path);
2479 snd_hda_activate_path(codec, mix_path, false, false);
2480 snd_hda_activate_path(codec, nomix_path, true, false);
2481 path_power_down_sync(codec, mix_path);
2485 /* re-initialize the output paths; only called from loopback_mixing_put() */
2486 static void update_output_paths(struct hda_codec *codec, int num_outs,
2489 struct hda_gen_spec *spec = codec->spec;
2490 struct nid_path *path;
2493 for (i = 0; i < num_outs; i++) {
2494 path = snd_hda_get_path_from_idx(codec, paths[i]);
2496 snd_hda_activate_path(codec, path, path->active,
2501 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2502 struct snd_ctl_elem_value *ucontrol)
2504 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2505 struct hda_gen_spec *spec = codec->spec;
2506 const struct auto_pin_cfg *cfg = &spec->autocfg;
2507 unsigned int val = ucontrol->value.enumerated.item[0];
2509 if (val == spec->aamix_mode)
2511 spec->aamix_mode = val;
2512 if (has_aamix_out_paths(spec)) {
2513 update_aamix_paths(codec, val, spec->out_paths[0],
2514 spec->aamix_out_paths[0],
2515 cfg->line_out_type);
2516 update_aamix_paths(codec, val, spec->hp_paths[0],
2517 spec->aamix_out_paths[1],
2519 update_aamix_paths(codec, val, spec->speaker_paths[0],
2520 spec->aamix_out_paths[2],
2521 AUTO_PIN_SPEAKER_OUT);
2523 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2524 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2525 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2526 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2527 update_output_paths(codec, cfg->speaker_outs,
2528 spec->speaker_paths);
2533 static const struct snd_kcontrol_new loopback_mixing_enum = {
2534 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2535 .name = "Loopback Mixing",
2536 .info = loopback_mixing_info,
2537 .get = loopback_mixing_get,
2538 .put = loopback_mixing_put,
2541 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2543 struct hda_gen_spec *spec = codec->spec;
2545 if (!spec->mixer_nid)
2547 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2549 spec->have_aamix_ctl = 1;
2554 * shared headphone/mic handling
2557 static void call_update_outputs(struct hda_codec *codec);
2559 /* for shared I/O, change the pin-control accordingly */
2560 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2562 struct hda_gen_spec *spec = codec->spec;
2567 pin = spec->hp_mic_pin;
2568 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2571 val = snd_hda_codec_get_pin_target(codec, pin);
2581 val = snd_hda_get_default_vref(codec, pin);
2582 /* if the HP pin doesn't support VREF and the codec driver gives an
2583 * alternative pin, set up the VREF on that pin instead
2585 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2586 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2587 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2588 if (vref_val != AC_PINCTL_VREF_HIZ)
2589 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2590 PIN_IN | (as_mic ? vref_val : 0));
2593 if (!spec->hp_mic_jack_modes) {
2598 set_pin_target(codec, pin, val, true);
2599 call_hp_automute(codec, NULL);
2603 /* create a shared input with the headphone out */
2604 static int create_hp_mic(struct hda_codec *codec)
2606 struct hda_gen_spec *spec = codec->spec;
2607 struct auto_pin_cfg *cfg = &spec->autocfg;
2608 unsigned int defcfg;
2611 if (!spec->hp_mic) {
2612 if (spec->suppress_hp_mic_detect)
2614 /* automatic detection: only if no input or a single internal
2615 * input pin is found, try to detect the shared hp/mic
2617 if (cfg->num_inputs > 1)
2619 else if (cfg->num_inputs == 1) {
2620 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2621 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2626 spec->hp_mic = 0; /* clear once */
2627 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2631 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2632 nid = cfg->line_out_pins[0];
2633 else if (cfg->hp_outs > 0)
2634 nid = cfg->hp_pins[0];
2638 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2639 return 0; /* no input */
2641 cfg->inputs[cfg->num_inputs].pin = nid;
2642 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2643 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2646 spec->hp_mic_pin = nid;
2647 /* we can't handle auto-mic together with HP-mic */
2648 spec->suppress_auto_mic = 1;
2649 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
2657 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2659 static const char * const out_jack_texts[] = {
2660 "Line Out", "Headphone Out",
2663 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2664 struct snd_ctl_elem_info *uinfo)
2666 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2669 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2670 struct snd_ctl_elem_value *ucontrol)
2672 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2673 hda_nid_t nid = kcontrol->private_value;
2674 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2675 ucontrol->value.enumerated.item[0] = 1;
2677 ucontrol->value.enumerated.item[0] = 0;
2681 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2682 struct snd_ctl_elem_value *ucontrol)
2684 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2685 hda_nid_t nid = kcontrol->private_value;
2688 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2689 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2691 snd_hda_set_pin_ctl_cache(codec, nid, val);
2695 static const struct snd_kcontrol_new out_jack_mode_enum = {
2696 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2697 .info = out_jack_mode_info,
2698 .get = out_jack_mode_get,
2699 .put = out_jack_mode_put,
2702 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2704 struct hda_gen_spec *spec = codec->spec;
2705 const struct snd_kcontrol_new *kctl;
2708 snd_array_for_each(&spec->kctls, i, kctl) {
2709 if (!strcmp(kctl->name, name) && kctl->index == idx)
2715 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2716 char *name, size_t name_len)
2718 struct hda_gen_spec *spec = codec->spec;
2721 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2722 strlcat(name, " Jack Mode", name_len);
2724 for (; find_kctl_name(codec, name, idx); idx++)
2728 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2730 struct hda_gen_spec *spec = codec->spec;
2731 if (spec->add_jack_modes) {
2732 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2733 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2739 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2742 struct hda_gen_spec *spec = codec->spec;
2745 for (i = 0; i < num_pins; i++) {
2746 hda_nid_t pin = pins[i];
2747 if (pin == spec->hp_mic_pin)
2749 if (get_out_jack_num_items(codec, pin) > 1) {
2750 struct snd_kcontrol_new *knew;
2751 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2752 get_jack_mode_name(codec, pin, name, sizeof(name));
2753 knew = snd_hda_gen_add_kctl(spec, name,
2754 &out_jack_mode_enum);
2757 knew->private_value = pin;
2768 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2771 static const char * const vref_texts[NUM_VREFS] = {
2772 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2773 "", "Mic 80pc Bias", "Mic 100pc Bias"
2776 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2778 unsigned int pincap;
2780 pincap = snd_hda_query_pin_caps(codec, pin);
2781 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2782 /* filter out unusual vrefs */
2783 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2787 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2788 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2790 unsigned int i, n = 0;
2792 for (i = 0; i < NUM_VREFS; i++) {
2793 if (vref_caps & (1 << i)) {
2802 /* convert back from the vref ctl index to the enum item index */
2803 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2805 unsigned int i, n = 0;
2807 for (i = 0; i < NUM_VREFS; i++) {
2810 if (vref_caps & (1 << i))
2816 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2817 struct snd_ctl_elem_info *uinfo)
2819 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2820 hda_nid_t nid = kcontrol->private_value;
2821 unsigned int vref_caps = get_vref_caps(codec, nid);
2823 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2825 /* set the right text */
2826 strcpy(uinfo->value.enumerated.name,
2827 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2831 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2832 struct snd_ctl_elem_value *ucontrol)
2834 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2835 hda_nid_t nid = kcontrol->private_value;
2836 unsigned int vref_caps = get_vref_caps(codec, nid);
2839 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2840 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2844 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2845 struct snd_ctl_elem_value *ucontrol)
2847 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2848 hda_nid_t nid = kcontrol->private_value;
2849 unsigned int vref_caps = get_vref_caps(codec, nid);
2850 unsigned int val, idx;
2852 val = snd_hda_codec_get_pin_target(codec, nid);
2853 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2854 if (idx == ucontrol->value.enumerated.item[0])
2857 val &= ~AC_PINCTL_VREFEN;
2858 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2859 snd_hda_set_pin_ctl_cache(codec, nid, val);
2863 static const struct snd_kcontrol_new in_jack_mode_enum = {
2864 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2865 .info = in_jack_mode_info,
2866 .get = in_jack_mode_get,
2867 .put = in_jack_mode_put,
2870 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2872 struct hda_gen_spec *spec = codec->spec;
2874 if (spec->add_jack_modes)
2875 nitems = hweight32(get_vref_caps(codec, pin));
2876 return nitems ? nitems : 1;
2879 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2881 struct hda_gen_spec *spec = codec->spec;
2882 struct snd_kcontrol_new *knew;
2883 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2884 unsigned int defcfg;
2886 if (pin == spec->hp_mic_pin)
2887 return 0; /* already done in create_out_jack_mode() */
2889 /* no jack mode for fixed pins */
2890 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2891 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2894 /* no multiple vref caps? */
2895 if (get_in_jack_num_items(codec, pin) <= 1)
2898 get_jack_mode_name(codec, pin, name, sizeof(name));
2899 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2902 knew->private_value = pin;
2907 * HP/mic shared jack mode
2909 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2910 struct snd_ctl_elem_info *uinfo)
2912 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2913 hda_nid_t nid = kcontrol->private_value;
2914 int out_jacks = get_out_jack_num_items(codec, nid);
2915 int in_jacks = get_in_jack_num_items(codec, nid);
2916 const char *text = NULL;
2919 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2921 uinfo->value.enumerated.items = out_jacks + in_jacks;
2922 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2923 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2924 idx = uinfo->value.enumerated.item;
2925 if (idx < out_jacks) {
2927 text = out_jack_texts[idx];
2929 text = "Headphone Out";
2933 unsigned int vref_caps = get_vref_caps(codec, nid);
2934 text = vref_texts[get_vref_idx(vref_caps, idx)];
2939 strcpy(uinfo->value.enumerated.name, text);
2943 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2945 int out_jacks = get_out_jack_num_items(codec, nid);
2946 int in_jacks = get_in_jack_num_items(codec, nid);
2947 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2950 if (val & PIN_OUT) {
2951 if (out_jacks > 1 && val == PIN_HP)
2953 } else if (val & PIN_IN) {
2956 unsigned int vref_caps = get_vref_caps(codec, nid);
2957 val &= AC_PINCTL_VREFEN;
2958 idx += cvt_from_vref_idx(vref_caps, val);
2964 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2965 struct snd_ctl_elem_value *ucontrol)
2967 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2968 hda_nid_t nid = kcontrol->private_value;
2969 ucontrol->value.enumerated.item[0] =
2970 get_cur_hp_mic_jack_mode(codec, nid);
2974 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2975 struct snd_ctl_elem_value *ucontrol)
2977 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2978 hda_nid_t nid = kcontrol->private_value;
2979 int out_jacks = get_out_jack_num_items(codec, nid);
2980 int in_jacks = get_in_jack_num_items(codec, nid);
2981 unsigned int val, oldval, idx;
2983 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2984 idx = ucontrol->value.enumerated.item[0];
2988 if (idx < out_jacks) {
2990 val = idx ? PIN_HP : PIN_OUT;
2996 unsigned int vref_caps = get_vref_caps(codec, nid);
2997 val = snd_hda_codec_get_pin_target(codec, nid);
2998 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2999 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
3001 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
3003 snd_hda_set_pin_ctl_cache(codec, nid, val);
3004 call_hp_automute(codec, NULL);
3009 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
3010 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3011 .info = hp_mic_jack_mode_info,
3012 .get = hp_mic_jack_mode_get,
3013 .put = hp_mic_jack_mode_put,
3016 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
3018 struct hda_gen_spec *spec = codec->spec;
3019 struct snd_kcontrol_new *knew;
3021 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
3022 &hp_mic_jack_mode_enum);
3025 knew->private_value = pin;
3026 spec->hp_mic_jack_modes = 1;
3034 /* add the powersave loopback-list entry */
3035 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
3037 struct hda_amp_list *list;
3039 list = snd_array_new(&spec->loopback_list);
3043 list->dir = HDA_INPUT;
3045 spec->loopback.amplist = spec->loopback_list.list;
3049 /* return true if either a volume or a mute amp is found for the given
3050 * aamix path; the amp has to be either in the mixer node or its direct leaf
3052 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3053 hda_nid_t pin, unsigned int *mix_val,
3054 unsigned int *mute_val)
3057 const hda_nid_t *list;
3060 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3064 *mix_val = *mute_val = 0;
3065 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3066 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3067 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3068 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3069 if (*mix_val && *mute_val)
3072 /* check leaf node */
3073 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3074 if (num_conns < idx)
3077 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3078 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
3079 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3080 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3081 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
3082 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3084 return *mix_val || *mute_val;
3087 /* create input playback/capture controls for the given pin */
3088 static int new_analog_input(struct hda_codec *codec, int input_idx,
3089 hda_nid_t pin, const char *ctlname, int ctlidx,
3092 struct hda_gen_spec *spec = codec->spec;
3093 struct nid_path *path;
3094 unsigned int mix_val, mute_val;
3097 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3100 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
3103 print_nid_path(codec, "loopback", path);
3104 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
3106 idx = path->idx[path->depth - 1];
3108 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
3111 path->ctls[NID_PATH_VOL_CTL] = mix_val;
3115 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
3118 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
3121 path->active = true;
3122 path->stream_enabled = true; /* no DAC/ADC involved */
3123 err = add_loopback_list(spec, mix_nid, idx);
3127 if (spec->mixer_nid != spec->mixer_merge_nid &&
3128 !spec->loopback_merge_path) {
3129 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3130 spec->mixer_merge_nid, 0);
3132 print_nid_path(codec, "loopback-merge", path);
3133 path->active = true;
3134 path->pin_fixed = true; /* static route */
3135 path->stream_enabled = true; /* no DAC/ADC involved */
3136 spec->loopback_merge_path =
3137 snd_hda_get_path_idx(codec, path);
3144 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3146 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3147 return (pincap & AC_PINCAP_IN) != 0;
3150 /* Parse the codec tree and retrieve ADCs */
3151 static int fill_adc_nids(struct hda_codec *codec)
3153 struct hda_gen_spec *spec = codec->spec;
3155 hda_nid_t *adc_nids = spec->adc_nids;
3156 int max_nums = ARRAY_SIZE(spec->adc_nids);
3159 for_each_hda_codec_node(nid, codec) {
3160 unsigned int caps = get_wcaps(codec, nid);
3161 int type = get_wcaps_type(caps);
3163 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3165 adc_nids[nums] = nid;
3166 if (++nums >= max_nums)
3169 spec->num_adc_nids = nums;
3171 /* copy the detected ADCs to all_adcs[] */
3172 spec->num_all_adcs = nums;
3173 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3178 /* filter out invalid adc_nids that don't give all active input pins;
3179 * if needed, check whether dynamic ADC-switching is available
3181 static int check_dyn_adc_switch(struct hda_codec *codec)
3183 struct hda_gen_spec *spec = codec->spec;
3184 struct hda_input_mux *imux = &spec->input_mux;
3185 unsigned int ok_bits;
3190 for (n = 0; n < spec->num_adc_nids; n++) {
3191 for (i = 0; i < imux->num_items; i++) {
3192 if (!spec->input_paths[i][n])
3195 if (i >= imux->num_items) {
3196 ok_bits |= (1 << n);
3202 /* check whether ADC-switch is possible */
3203 for (i = 0; i < imux->num_items; i++) {
3204 for (n = 0; n < spec->num_adc_nids; n++) {
3205 if (spec->input_paths[i][n]) {
3206 spec->dyn_adc_idx[i] = n;
3212 codec_dbg(codec, "enabling ADC switching\n");
3213 spec->dyn_adc_switch = 1;
3214 } else if (nums != spec->num_adc_nids) {
3215 /* shrink the invalid adcs and input paths */
3217 for (n = 0; n < spec->num_adc_nids; n++) {
3218 if (!(ok_bits & (1 << n)))
3221 spec->adc_nids[nums] = spec->adc_nids[n];
3222 for (i = 0; i < imux->num_items; i++) {
3223 invalidate_nid_path(codec,
3224 spec->input_paths[i][nums]);
3225 spec->input_paths[i][nums] =
3226 spec->input_paths[i][n];
3227 spec->input_paths[i][n] = 0;
3232 spec->num_adc_nids = nums;
3235 if (imux->num_items == 1 ||
3236 (imux->num_items == 2 && spec->hp_mic)) {
3237 codec_dbg(codec, "reducing to a single ADC\n");
3238 spec->num_adc_nids = 1; /* reduce to a single ADC */
3241 /* single index for individual volumes ctls */
3242 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3243 spec->num_adc_nids = 1;
3248 /* parse capture source paths from the given pin and create imux items */
3249 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3250 int cfg_idx, int num_adcs,
3251 const char *label, int anchor)
3253 struct hda_gen_spec *spec = codec->spec;
3254 struct hda_input_mux *imux = &spec->input_mux;
3255 int imux_idx = imux->num_items;
3256 bool imux_added = false;
3259 for (c = 0; c < num_adcs; c++) {
3260 struct nid_path *path;
3261 hda_nid_t adc = spec->adc_nids[c];
3263 if (!is_reachable_path(codec, pin, adc))
3265 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3268 print_nid_path(codec, "input", path);
3269 spec->input_paths[imux_idx][c] =
3270 snd_hda_get_path_idx(codec, path);
3273 if (spec->hp_mic_pin == pin)
3274 spec->hp_mic_mux_idx = imux->num_items;
3275 spec->imux_pins[imux->num_items] = pin;
3276 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3278 if (spec->dyn_adc_switch)
3279 spec->dyn_adc_idx[imux_idx] = c;
3287 * create playback/capture controls for input pins
3290 /* fill the label for each input at first */
3291 static int fill_input_pin_labels(struct hda_codec *codec)
3293 struct hda_gen_spec *spec = codec->spec;
3294 const struct auto_pin_cfg *cfg = &spec->autocfg;
3297 for (i = 0; i < cfg->num_inputs; i++) {
3298 hda_nid_t pin = cfg->inputs[i].pin;
3302 if (!is_input_pin(codec, pin))
3305 label = hda_get_autocfg_input_label(codec, cfg, i);
3307 for (j = i - 1; j >= 0; j--) {
3308 if (spec->input_labels[j] &&
3309 !strcmp(spec->input_labels[j], label)) {
3310 idx = spec->input_label_idxs[j] + 1;
3315 spec->input_labels[i] = label;
3316 spec->input_label_idxs[i] = idx;
3322 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3324 static int create_input_ctls(struct hda_codec *codec)
3326 struct hda_gen_spec *spec = codec->spec;
3327 const struct auto_pin_cfg *cfg = &spec->autocfg;
3328 hda_nid_t mixer = spec->mixer_nid;
3333 num_adcs = fill_adc_nids(codec);
3337 err = fill_input_pin_labels(codec);
3341 for (i = 0; i < cfg->num_inputs; i++) {
3344 pin = cfg->inputs[i].pin;
3345 if (!is_input_pin(codec, pin))
3349 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3350 val |= snd_hda_get_default_vref(codec, pin);
3351 if (pin != spec->hp_mic_pin &&
3352 !snd_hda_codec_get_pin_target(codec, pin))
3353 set_pin_target(codec, pin, val, false);
3356 if (is_reachable_path(codec, pin, mixer)) {
3357 err = new_analog_input(codec, i, pin,
3358 spec->input_labels[i],
3359 spec->input_label_idxs[i],
3366 err = parse_capture_source(codec, pin, i, num_adcs,
3367 spec->input_labels[i], -mixer);
3371 if (spec->add_jack_modes) {
3372 err = create_in_jack_mode(codec, pin);
3378 /* add stereo mix when explicitly enabled via hint */
3379 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3380 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3385 spec->suppress_auto_mic = 1;
3396 /* get the input path specified by the given adc and imux indices */
3397 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3399 struct hda_gen_spec *spec = codec->spec;
3400 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3404 if (spec->dyn_adc_switch)
3405 adc_idx = spec->dyn_adc_idx[imux_idx];
3406 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3410 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3413 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3416 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3417 struct snd_ctl_elem_info *uinfo)
3419 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3420 struct hda_gen_spec *spec = codec->spec;
3421 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3424 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3425 struct snd_ctl_elem_value *ucontrol)
3427 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3428 struct hda_gen_spec *spec = codec->spec;
3429 /* the ctls are created at once with multiple counts */
3430 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3432 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3436 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3437 struct snd_ctl_elem_value *ucontrol)
3439 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3440 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3441 return mux_select(codec, adc_idx,
3442 ucontrol->value.enumerated.item[0]);
3445 static const struct snd_kcontrol_new cap_src_temp = {
3446 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3447 .name = "Input Source",
3448 .info = mux_enum_info,
3449 .get = mux_enum_get,
3450 .put = mux_enum_put,
3454 * capture volume and capture switch ctls
3457 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3458 struct snd_ctl_elem_value *ucontrol);
3460 /* call the given amp update function for all amps in the imux list at once */
3461 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3462 struct snd_ctl_elem_value *ucontrol,
3463 put_call_t func, int type)
3465 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3466 struct hda_gen_spec *spec = codec->spec;
3467 const struct hda_input_mux *imux;
3468 struct nid_path *path;
3469 int i, adc_idx, ret, err = 0;
3471 imux = &spec->input_mux;
3472 adc_idx = kcontrol->id.index;
3473 mutex_lock(&codec->control_mutex);
3474 for (i = 0; i < imux->num_items; i++) {
3475 path = get_input_path(codec, adc_idx, i);
3476 if (!path || !path->ctls[type])
3478 kcontrol->private_value = path->ctls[type];
3479 ret = func(kcontrol, ucontrol);
3487 mutex_unlock(&codec->control_mutex);
3488 if (err >= 0 && spec->cap_sync_hook)
3489 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3493 /* capture volume ctl callbacks */
3494 #define cap_vol_info snd_hda_mixer_amp_volume_info
3495 #define cap_vol_get snd_hda_mixer_amp_volume_get
3496 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3498 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3499 struct snd_ctl_elem_value *ucontrol)
3501 return cap_put_caller(kcontrol, ucontrol,
3502 snd_hda_mixer_amp_volume_put,
3506 static const struct snd_kcontrol_new cap_vol_temp = {
3507 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3508 .name = "Capture Volume",
3509 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3510 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3511 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3512 .info = cap_vol_info,
3515 .tlv = { .c = cap_vol_tlv },
3518 /* capture switch ctl callbacks */
3519 #define cap_sw_info snd_ctl_boolean_stereo_info
3520 #define cap_sw_get snd_hda_mixer_amp_switch_get
3522 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3523 struct snd_ctl_elem_value *ucontrol)
3525 return cap_put_caller(kcontrol, ucontrol,
3526 snd_hda_mixer_amp_switch_put,
3530 static const struct snd_kcontrol_new cap_sw_temp = {
3531 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3532 .name = "Capture Switch",
3533 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3534 .info = cap_sw_info,
3539 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3544 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3545 for (depth = 0; depth < 3; depth++) {
3546 if (depth >= path->depth)
3548 i = path->depth - depth - 1;
3549 nid = path->path[i];
3550 if (!path->ctls[NID_PATH_VOL_CTL]) {
3551 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3552 path->ctls[NID_PATH_VOL_CTL] =
3553 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3554 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3555 int idx = path->idx[i];
3556 if (!depth && codec->single_adc_amp)
3558 path->ctls[NID_PATH_VOL_CTL] =
3559 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3562 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3563 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3564 path->ctls[NID_PATH_MUTE_CTL] =
3565 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3566 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3567 int idx = path->idx[i];
3568 if (!depth && codec->single_adc_amp)
3570 path->ctls[NID_PATH_MUTE_CTL] =
3571 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3578 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3580 struct hda_gen_spec *spec = codec->spec;
3581 struct auto_pin_cfg *cfg = &spec->autocfg;
3585 if (!spec->inv_dmic_split)
3587 for (i = 0; i < cfg->num_inputs; i++) {
3588 if (cfg->inputs[i].pin != nid)
3590 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3592 val = snd_hda_codec_get_pincfg(codec, nid);
3593 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3598 /* capture switch put callback for a single control with hook call */
3599 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3600 struct snd_ctl_elem_value *ucontrol)
3602 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3603 struct hda_gen_spec *spec = codec->spec;
3606 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3610 if (spec->cap_sync_hook)
3611 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3616 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3617 int idx, bool is_switch, unsigned int ctl,
3620 struct hda_gen_spec *spec = codec->spec;
3621 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3622 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3623 const char *sfx = is_switch ? "Switch" : "Volume";
3624 unsigned int chs = inv_dmic ? 1 : 3;
3625 struct snd_kcontrol_new *knew;
3631 snprintf(tmpname, sizeof(tmpname),
3632 "%s Capture %s", label, sfx);
3634 snprintf(tmpname, sizeof(tmpname),
3636 knew = add_control(spec, type, tmpname, idx,
3637 amp_val_replace_channels(ctl, chs));
3641 knew->put = cap_single_sw_put;
3642 if (spec->mic_mute_led)
3643 knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
3648 /* Make independent right kcontrol */
3650 snprintf(tmpname, sizeof(tmpname),
3651 "Inverted %s Capture %s", label, sfx);
3653 snprintf(tmpname, sizeof(tmpname),
3654 "Inverted Capture %s", sfx);
3655 knew = add_control(spec, type, tmpname, idx,
3656 amp_val_replace_channels(ctl, 2));
3660 knew->put = cap_single_sw_put;
3661 if (spec->mic_mute_led)
3662 knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
3667 /* create single (and simple) capture volume and switch controls */
3668 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3669 unsigned int vol_ctl, unsigned int sw_ctl,
3673 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3676 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3682 /* create bound capture volume and switch controls */
3683 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3684 unsigned int vol_ctl, unsigned int sw_ctl)
3686 struct hda_gen_spec *spec = codec->spec;
3687 struct snd_kcontrol_new *knew;
3690 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3694 knew->private_value = vol_ctl;
3695 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3698 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3702 knew->private_value = sw_ctl;
3703 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3704 if (spec->mic_mute_led)
3705 knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
3710 /* return the vol ctl when used first in the imux list */
3711 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3713 struct nid_path *path;
3717 path = get_input_path(codec, 0, idx);
3720 ctl = path->ctls[type];
3723 for (i = 0; i < idx - 1; i++) {
3724 path = get_input_path(codec, 0, i);
3725 if (path && path->ctls[type] == ctl)
3731 /* create individual capture volume and switch controls per input */
3732 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3734 struct hda_gen_spec *spec = codec->spec;
3735 struct hda_input_mux *imux = &spec->input_mux;
3738 for (i = 0; i < imux->num_items; i++) {
3742 idx = imux->items[i].index;
3743 if (idx >= spec->autocfg.num_inputs)
3745 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3747 for (type = 0; type < 2; type++) {
3748 err = add_single_cap_ctl(codec,
3749 spec->input_labels[idx],
3750 spec->input_label_idxs[idx],
3752 get_first_cap_ctl(codec, i, type),
3761 static int create_capture_mixers(struct hda_codec *codec)
3763 struct hda_gen_spec *spec = codec->spec;
3764 struct hda_input_mux *imux = &spec->input_mux;
3765 int i, n, nums, err;
3767 if (spec->dyn_adc_switch)
3770 nums = spec->num_adc_nids;
3772 if (!spec->auto_mic && imux->num_items > 1) {
3773 struct snd_kcontrol_new *knew;
3775 name = nums > 1 ? "Input Source" : "Capture Source";
3776 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3782 for (n = 0; n < nums; n++) {
3784 bool multi_cap_vol = spec->multi_cap_vol;
3785 bool inv_dmic = false;
3789 for (i = 0; i < imux->num_items; i++) {
3790 struct nid_path *path;
3791 path = get_input_path(codec, n, i);
3794 parse_capvol_in_path(codec, path);
3796 vol = path->ctls[NID_PATH_VOL_CTL];
3797 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3799 if (!same_amp_caps(codec, vol,
3800 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3801 multi_cap_vol = true;
3804 sw = path->ctls[NID_PATH_MUTE_CTL];
3805 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3807 if (!same_amp_caps(codec, sw,
3808 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3809 multi_cap_vol = true;
3811 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3816 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3818 else if (!multi_cap_vol && !inv_dmic)
3819 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3821 err = create_multi_cap_vol_ctl(codec);
3830 * add mic boosts if needed
3833 /* check whether the given amp is feasible as a boost volume */
3834 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3839 if (!nid_has_volume(codec, nid, dir) ||
3840 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3841 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3844 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3845 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3851 /* look for a boost amp in a widget close to the pin */
3852 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3853 struct nid_path *path)
3855 unsigned int val = 0;
3859 for (depth = 0; depth < 3; depth++) {
3860 if (depth >= path->depth - 1)
3862 nid = path->path[depth];
3863 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3864 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3866 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3867 path->idx[depth])) {
3868 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3877 static int parse_mic_boost(struct hda_codec *codec)
3879 struct hda_gen_spec *spec = codec->spec;
3880 struct auto_pin_cfg *cfg = &spec->autocfg;
3881 struct hda_input_mux *imux = &spec->input_mux;
3884 if (!spec->num_adc_nids)
3887 for (i = 0; i < imux->num_items; i++) {
3888 struct nid_path *path;
3891 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3893 idx = imux->items[i].index;
3894 if (idx >= imux->num_items)
3897 /* check only line-in and mic pins */
3898 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3901 path = get_input_path(codec, 0, i);
3905 val = look_for_boost_amp(codec, path);
3909 /* create a boost control */
3910 snprintf(boost_label, sizeof(boost_label),
3911 "%s Boost Volume", spec->input_labels[idx]);
3912 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3913 spec->input_label_idxs[idx], val))
3916 path->ctls[NID_PATH_BOOST_CTL] = val;
3921 #ifdef CONFIG_SND_HDA_GENERIC_LEDS
3923 * vmaster mute LED hook helpers
3926 static int create_mute_led_cdev(struct hda_codec *codec,
3927 int (*callback)(struct led_classdev *,
3928 enum led_brightness),
3931 struct hda_gen_spec *spec = codec->spec;
3932 struct led_classdev *cdev;
3933 int idx = micmute ? LED_AUDIO_MICMUTE : LED_AUDIO_MUTE;
3936 cdev = devm_kzalloc(&codec->core.dev, sizeof(*cdev), GFP_KERNEL);
3940 cdev->name = micmute ? "hda::micmute" : "hda::mute";
3941 cdev->max_brightness = 1;
3942 cdev->default_trigger = micmute ? "audio-micmute" : "audio-mute";
3943 cdev->brightness_set_blocking = callback;
3944 cdev->brightness = ledtrig_audio_get(idx);
3945 cdev->flags = LED_CORE_SUSPENDRESUME;
3947 err = led_classdev_register(&codec->core.dev, cdev);
3950 spec->led_cdevs[idx] = cdev;
3955 * snd_hda_gen_add_mute_led_cdev - Create a LED classdev and enable as vmaster mute LED
3956 * @codec: the HDA codec
3957 * @callback: the callback for LED classdev brightness_set_blocking
3959 int snd_hda_gen_add_mute_led_cdev(struct hda_codec *codec,
3960 int (*callback)(struct led_classdev *,
3961 enum led_brightness))
3963 struct hda_gen_spec *spec = codec->spec;
3967 err = create_mute_led_cdev(codec, callback, false);
3969 codec_warn(codec, "failed to create a mute LED cdev\n");
3974 if (spec->vmaster_mute.hook)
3975 codec_err(codec, "vmaster hook already present before cdev!\n");
3977 spec->vmaster_mute_led = 1;
3980 EXPORT_SYMBOL_GPL(snd_hda_gen_add_mute_led_cdev);
3983 * snd_hda_gen_add_micmute_led_cdev - Create a LED classdev and enable as mic-mute LED
3984 * @codec: the HDA codec
3985 * @callback: the callback for LED classdev brightness_set_blocking
3987 * Called from the codec drivers for offering the mic mute LED controls.
3988 * This creates a LED classdev and sets up the cap_sync_hook that is called at
3989 * each time when the capture mixer switch changes.
3991 * When NULL is passed to @callback, no classdev is created but only the
3992 * LED-trigger is set up.
3994 * Returns 0 or a negative error.
3996 int snd_hda_gen_add_micmute_led_cdev(struct hda_codec *codec,
3997 int (*callback)(struct led_classdev *,
3998 enum led_brightness))
4000 struct hda_gen_spec *spec = codec->spec;
4004 err = create_mute_led_cdev(codec, callback, true);
4006 codec_warn(codec, "failed to create a mic-mute LED cdev\n");
4011 spec->mic_mute_led = 1;
4014 EXPORT_SYMBOL_GPL(snd_hda_gen_add_micmute_led_cdev);
4015 #endif /* CONFIG_SND_HDA_GENERIC_LEDS */
4018 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
4020 static void parse_digital(struct hda_codec *codec)
4022 struct hda_gen_spec *spec = codec->spec;
4023 struct nid_path *path;
4025 hda_nid_t dig_nid, pin;
4027 /* support multiple SPDIFs; the secondary is set up as a follower */
4029 for (i = 0; i < spec->autocfg.dig_outs; i++) {
4030 pin = spec->autocfg.dig_out_pins[i];
4031 dig_nid = look_for_dac(codec, pin, true);
4034 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
4037 print_nid_path(codec, "digout", path);
4038 path->active = true;
4039 path->pin_fixed = true; /* no jack detection */
4040 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
4041 set_pin_target(codec, pin, PIN_OUT, false);
4043 spec->multiout.dig_out_nid = dig_nid;
4044 spec->dig_out_type = spec->autocfg.dig_out_type[0];
4046 spec->multiout.follower_dig_outs = spec->follower_dig_outs;
4047 if (nums >= ARRAY_SIZE(spec->follower_dig_outs) - 1)
4049 spec->follower_dig_outs[nums - 1] = dig_nid;
4054 if (spec->autocfg.dig_in_pin) {
4055 pin = spec->autocfg.dig_in_pin;
4056 for_each_hda_codec_node(dig_nid, codec) {
4057 unsigned int wcaps = get_wcaps(codec, dig_nid);
4058 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
4060 if (!(wcaps & AC_WCAP_DIGITAL))
4062 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
4064 print_nid_path(codec, "digin", path);
4065 path->active = true;
4066 path->pin_fixed = true; /* no jack */
4067 spec->dig_in_nid = dig_nid;
4068 spec->digin_path = snd_hda_get_path_idx(codec, path);
4069 set_pin_target(codec, pin, PIN_IN, false);
4078 * input MUX handling
4081 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
4083 /* select the given imux item; either unmute exclusively or select the route */
4084 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
4087 struct hda_gen_spec *spec = codec->spec;
4088 const struct hda_input_mux *imux;
4089 struct nid_path *old_path, *path;
4091 imux = &spec->input_mux;
4092 if (!imux->num_items)
4095 if (idx >= imux->num_items)
4096 idx = imux->num_items - 1;
4097 if (spec->cur_mux[adc_idx] == idx)
4100 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
4103 if (old_path->active)
4104 snd_hda_activate_path(codec, old_path, false, false);
4106 spec->cur_mux[adc_idx] = idx;
4109 update_hp_mic(codec, adc_idx, false);
4111 if (spec->dyn_adc_switch)
4112 dyn_adc_pcm_resetup(codec, idx);
4114 path = get_input_path(codec, adc_idx, idx);
4119 snd_hda_activate_path(codec, path, true, false);
4120 if (spec->cap_sync_hook)
4121 spec->cap_sync_hook(codec, NULL, NULL);
4122 path_power_down_sync(codec, old_path);
4126 /* power up/down widgets in the all paths that match with the given NID
4127 * as terminals (either start- or endpoint)
4129 * returns the last changed NID, or zero if unchanged.
4131 static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
4132 int pin_state, int stream_state)
4134 struct hda_gen_spec *spec = codec->spec;
4135 hda_nid_t last, changed = 0;
4136 struct nid_path *path;
4139 snd_array_for_each(&spec->paths, n, path) {
4142 if (path->path[0] == nid ||
4143 path->path[path->depth - 1] == nid) {
4144 bool pin_old = path->pin_enabled;
4145 bool stream_old = path->stream_enabled;
4148 path->pin_enabled = pin_state;
4149 if (stream_state >= 0)
4150 path->stream_enabled = stream_state;
4151 if ((!path->pin_fixed && path->pin_enabled != pin_old)
4152 || path->stream_enabled != stream_old) {
4153 last = path_power_update(codec, path, true);
4162 /* check the jack status for power control */
4163 static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4165 if (!is_jack_detectable(codec, pin))
4167 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4170 /* power up/down the paths of the given pin according to the jack state;
4171 * power = 0/1 : only power up/down if it matches with the jack state,
4172 * < 0 : force power up/down to follow the jack sate
4174 * returns the last changed NID, or zero if unchanged.
4176 static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4181 if (!codec->power_save_node)
4184 on = detect_pin_state(codec, pin);
4186 if (power >= 0 && on != power)
4188 return set_path_power(codec, pin, on, -1);
4191 static void pin_power_callback(struct hda_codec *codec,
4192 struct hda_jack_callback *jack,
4195 if (jack && jack->nid)
4196 sync_power_state_change(codec,
4197 set_pin_power_jack(codec, jack->nid, on));
4200 /* callback only doing power up -- called at first */
4201 static void pin_power_up_callback(struct hda_codec *codec,
4202 struct hda_jack_callback *jack)
4204 pin_power_callback(codec, jack, true);
4207 /* callback only doing power down -- called at last */
4208 static void pin_power_down_callback(struct hda_codec *codec,
4209 struct hda_jack_callback *jack)
4211 pin_power_callback(codec, jack, false);
4214 /* set up the power up/down callbacks */
4215 static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4216 const hda_nid_t *pins, bool on)
4219 hda_jack_callback_fn cb =
4220 on ? pin_power_up_callback : pin_power_down_callback;
4222 for (i = 0; i < num_pins && pins[i]; i++) {
4223 if (is_jack_detectable(codec, pins[i]))
4224 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4226 set_path_power(codec, pins[i], true, -1);
4230 /* enabled power callback to each available I/O pin with jack detections;
4231 * the digital I/O pins are excluded because of the unreliable detectsion
4233 static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4235 struct hda_gen_spec *spec = codec->spec;
4236 struct auto_pin_cfg *cfg = &spec->autocfg;
4239 if (!codec->power_save_node)
4241 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4242 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4243 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4244 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4245 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4246 for (i = 0; i < cfg->num_inputs; i++)
4247 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4250 /* sync path power up/down with the jack states of given pins */
4251 static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4252 const hda_nid_t *pins)
4256 for (i = 0; i < num_pins && pins[i]; i++)
4257 if (is_jack_detectable(codec, pins[i]))
4258 set_pin_power_jack(codec, pins[i], -1);
4261 /* sync path power up/down with pins; called at init and resume */
4262 static void sync_all_pin_power_ctls(struct hda_codec *codec)
4264 struct hda_gen_spec *spec = codec->spec;
4265 struct auto_pin_cfg *cfg = &spec->autocfg;
4268 if (!codec->power_save_node)
4270 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4271 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4272 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4273 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4274 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4275 for (i = 0; i < cfg->num_inputs; i++)
4276 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4279 /* add fake paths if not present yet */
4280 static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4281 int num_pins, const hda_nid_t *pins)
4283 struct hda_gen_spec *spec = codec->spec;
4284 struct nid_path *path;
4287 for (i = 0; i < num_pins; i++) {
4290 if (get_nid_path(codec, nid, pins[i], 0))
4292 path = snd_array_new(&spec->paths);
4295 memset(path, 0, sizeof(*path));
4297 path->path[0] = nid;
4298 path->path[1] = pins[i];
4299 path->active = true;
4304 /* create fake paths to all outputs from beep */
4305 static int add_fake_beep_paths(struct hda_codec *codec)
4307 struct hda_gen_spec *spec = codec->spec;
4308 struct auto_pin_cfg *cfg = &spec->autocfg;
4309 hda_nid_t nid = spec->beep_nid;
4312 if (!codec->power_save_node || !nid)
4314 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4317 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4318 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4322 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4323 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4331 /* power up/down beep widget and its output paths */
4332 static void beep_power_hook(struct hda_beep *beep, bool on)
4334 set_path_power(beep->codec, beep->nid, -1, on);
4338 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4339 * @codec: the HDA codec
4340 * @pin: NID of pin to fix
4342 int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4344 struct hda_gen_spec *spec = codec->spec;
4345 struct nid_path *path;
4347 path = snd_array_new(&spec->paths);
4350 memset(path, 0, sizeof(*path));
4352 path->path[0] = pin;
4353 path->active = true;
4354 path->pin_fixed = true;
4355 path->stream_enabled = true;
4358 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4361 * Jack detections for HP auto-mute and mic-switch
4364 /* check each pin in the given array; returns true if any of them is plugged */
4365 static bool detect_jacks(struct hda_codec *codec, int num_pins, const hda_nid_t *pins)
4368 bool present = false;
4370 for (i = 0; i < num_pins; i++) {
4371 hda_nid_t nid = pins[i];
4374 /* don't detect pins retasked as inputs */
4375 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4377 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4383 /* standard HP/line-out auto-mute helper */
4384 static void do_automute(struct hda_codec *codec, int num_pins, const hda_nid_t *pins,
4385 int *paths, bool mute)
4387 struct hda_gen_spec *spec = codec->spec;
4390 for (i = 0; i < num_pins; i++) {
4391 hda_nid_t nid = pins[i];
4392 unsigned int val, oldval;
4396 oldval = snd_hda_codec_get_pin_target(codec, nid);
4397 if (oldval & PIN_IN)
4398 continue; /* no mute for inputs */
4400 if (spec->auto_mute_via_amp) {
4401 struct nid_path *path;
4404 path = snd_hda_get_path_from_idx(codec, paths[i]);
4407 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4411 spec->mute_bits |= (1ULL << mute_nid);
4413 spec->mute_bits &= ~(1ULL << mute_nid);
4416 /* don't reset VREF value in case it's controlling
4417 * the amp (see alc861_fixup_asus_amp_vref_0f())
4419 if (spec->keep_vref_in_automute)
4420 val = oldval & ~PIN_HP;
4425 /* here we call update_pin_ctl() so that the pinctl is
4426 * changed without changing the pinctl target value;
4427 * the original target value will be still referred at
4428 * the init / resume again
4430 update_pin_ctl(codec, nid, val);
4433 set_pin_eapd(codec, nid, !mute);
4434 if (codec->power_save_node) {
4437 on = detect_pin_state(codec, nid);
4438 set_path_power(codec, nid, on, -1);
4444 * snd_hda_gen_update_outputs - Toggle outputs muting
4445 * @codec: the HDA codec
4447 * Update the mute status of all outputs based on the current jack states.
4449 void snd_hda_gen_update_outputs(struct hda_codec *codec)
4451 struct hda_gen_spec *spec = codec->spec;
4455 /* Control HP pins/amps depending on master_mute state;
4456 * in general, HP pins/amps control should be enabled in all cases,
4457 * but currently set only for master_mute, just to be safe
4459 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4460 paths = spec->out_paths;
4462 paths = spec->hp_paths;
4463 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
4464 spec->autocfg.hp_pins, paths, spec->master_mute);
4466 if (!spec->automute_speaker)
4469 on = spec->hp_jack_present | spec->line_jack_present;
4470 on |= spec->master_mute;
4471 spec->speaker_muted = on;
4472 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4473 paths = spec->out_paths;
4475 paths = spec->speaker_paths;
4476 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
4477 spec->autocfg.speaker_pins, paths, on);
4479 /* toggle line-out mutes if needed, too */
4480 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4481 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4482 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4484 if (!spec->automute_lo)
4487 on = spec->hp_jack_present;
4488 on |= spec->master_mute;
4489 spec->line_out_muted = on;
4490 paths = spec->out_paths;
4491 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4492 spec->autocfg.line_out_pins, paths, on);
4494 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4496 static void call_update_outputs(struct hda_codec *codec)
4498 struct hda_gen_spec *spec = codec->spec;
4499 if (spec->automute_hook)
4500 spec->automute_hook(codec);
4502 snd_hda_gen_update_outputs(codec);
4504 /* sync the whole vmaster followers to reflect the new auto-mute status */
4505 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4506 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4510 * snd_hda_gen_hp_automute - standard HP-automute helper
4511 * @codec: the HDA codec
4512 * @jack: jack object, NULL for the whole
4514 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4515 struct hda_jack_callback *jack)
4517 struct hda_gen_spec *spec = codec->spec;
4518 hda_nid_t *pins = spec->autocfg.hp_pins;
4519 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4521 /* No detection for the first HP jack during indep-HP mode */
4522 if (spec->indep_hp_enabled) {
4527 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4528 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4530 call_update_outputs(codec);
4532 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4535 * snd_hda_gen_line_automute - standard line-out-automute helper
4536 * @codec: the HDA codec
4537 * @jack: jack object, NULL for the whole
4539 void snd_hda_gen_line_automute(struct hda_codec *codec,
4540 struct hda_jack_callback *jack)
4542 struct hda_gen_spec *spec = codec->spec;
4544 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4546 /* check LO jack only when it's different from HP */
4547 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4550 spec->line_jack_present =
4551 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4552 spec->autocfg.line_out_pins);
4553 if (!spec->automute_speaker || !spec->detect_lo)
4555 call_update_outputs(codec);
4557 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4560 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4561 * @codec: the HDA codec
4562 * @jack: jack object, NULL for the whole
4564 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4565 struct hda_jack_callback *jack)
4567 struct hda_gen_spec *spec = codec->spec;
4570 if (!spec->auto_mic)
4573 for (i = spec->am_num_entries - 1; i > 0; i--) {
4574 hda_nid_t pin = spec->am_entry[i].pin;
4575 /* don't detect pins retasked as outputs */
4576 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4578 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4579 mux_select(codec, 0, spec->am_entry[i].idx);
4583 mux_select(codec, 0, spec->am_entry[0].idx);
4585 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4587 /* call appropriate hooks */
4588 static void call_hp_automute(struct hda_codec *codec,
4589 struct hda_jack_callback *jack)
4591 struct hda_gen_spec *spec = codec->spec;
4592 if (spec->hp_automute_hook)
4593 spec->hp_automute_hook(codec, jack);
4595 snd_hda_gen_hp_automute(codec, jack);
4598 static void call_line_automute(struct hda_codec *codec,
4599 struct hda_jack_callback *jack)
4601 struct hda_gen_spec *spec = codec->spec;
4602 if (spec->line_automute_hook)
4603 spec->line_automute_hook(codec, jack);
4605 snd_hda_gen_line_automute(codec, jack);
4608 static void call_mic_autoswitch(struct hda_codec *codec,
4609 struct hda_jack_callback *jack)
4611 struct hda_gen_spec *spec = codec->spec;
4612 if (spec->mic_autoswitch_hook)
4613 spec->mic_autoswitch_hook(codec, jack);
4615 snd_hda_gen_mic_autoswitch(codec, jack);
4618 /* update jack retasking */
4619 static void update_automute_all(struct hda_codec *codec)
4621 call_hp_automute(codec, NULL);
4622 call_line_automute(codec, NULL);
4623 call_mic_autoswitch(codec, NULL);
4627 * Auto-Mute mode mixer enum support
4629 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4630 struct snd_ctl_elem_info *uinfo)
4632 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4633 struct hda_gen_spec *spec = codec->spec;
4634 static const char * const texts3[] = {
4635 "Disabled", "Speaker Only", "Line Out+Speaker"
4638 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4639 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4640 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4643 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4644 struct snd_ctl_elem_value *ucontrol)
4646 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4647 struct hda_gen_spec *spec = codec->spec;
4648 unsigned int val = 0;
4649 if (spec->automute_speaker)
4651 if (spec->automute_lo)
4654 ucontrol->value.enumerated.item[0] = val;
4658 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4659 struct snd_ctl_elem_value *ucontrol)
4661 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4662 struct hda_gen_spec *spec = codec->spec;
4664 switch (ucontrol->value.enumerated.item[0]) {
4666 if (!spec->automute_speaker && !spec->automute_lo)
4668 spec->automute_speaker = 0;
4669 spec->automute_lo = 0;
4672 if (spec->automute_speaker_possible) {
4673 if (!spec->automute_lo && spec->automute_speaker)
4675 spec->automute_speaker = 1;
4676 spec->automute_lo = 0;
4677 } else if (spec->automute_lo_possible) {
4678 if (spec->automute_lo)
4680 spec->automute_lo = 1;
4685 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4687 if (spec->automute_speaker && spec->automute_lo)
4689 spec->automute_speaker = 1;
4690 spec->automute_lo = 1;
4695 call_update_outputs(codec);
4699 static const struct snd_kcontrol_new automute_mode_enum = {
4700 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4701 .name = "Auto-Mute Mode",
4702 .info = automute_mode_info,
4703 .get = automute_mode_get,
4704 .put = automute_mode_put,
4707 static int add_automute_mode_enum(struct hda_codec *codec)
4709 struct hda_gen_spec *spec = codec->spec;
4711 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4717 * Check the availability of HP/line-out auto-mute;
4718 * Set up appropriately if really supported
4720 static int check_auto_mute_availability(struct hda_codec *codec)
4722 struct hda_gen_spec *spec = codec->spec;
4723 struct auto_pin_cfg *cfg = &spec->autocfg;
4727 if (spec->suppress_auto_mute)
4730 if (cfg->hp_pins[0])
4732 if (cfg->line_out_pins[0])
4734 if (cfg->speaker_pins[0])
4736 if (present < 2) /* need two different output types */
4739 if (!cfg->speaker_pins[0] &&
4740 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4741 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4742 sizeof(cfg->speaker_pins));
4743 cfg->speaker_outs = cfg->line_outs;
4746 if (!cfg->hp_pins[0] &&
4747 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4748 memcpy(cfg->hp_pins, cfg->line_out_pins,
4749 sizeof(cfg->hp_pins));
4750 cfg->hp_outs = cfg->line_outs;
4753 for (i = 0; i < cfg->hp_outs; i++) {
4754 hda_nid_t nid = cfg->hp_pins[i];
4755 if (!is_jack_detectable(codec, nid))
4757 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4758 snd_hda_jack_detect_enable_callback(codec, nid,
4760 spec->detect_hp = 1;
4763 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4764 if (cfg->speaker_outs)
4765 for (i = 0; i < cfg->line_outs; i++) {
4766 hda_nid_t nid = cfg->line_out_pins[i];
4767 if (!is_jack_detectable(codec, nid))
4769 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4770 snd_hda_jack_detect_enable_callback(codec, nid,
4771 call_line_automute);
4772 spec->detect_lo = 1;
4774 spec->automute_lo_possible = spec->detect_hp;
4777 spec->automute_speaker_possible = cfg->speaker_outs &&
4778 (spec->detect_hp || spec->detect_lo);
4780 spec->automute_lo = spec->automute_lo_possible;
4781 spec->automute_speaker = spec->automute_speaker_possible;
4783 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4784 /* create a control for automute mode */
4785 err = add_automute_mode_enum(codec);
4792 /* check whether all auto-mic pins are valid; setup indices if OK */
4793 static bool auto_mic_check_imux(struct hda_codec *codec)
4795 struct hda_gen_spec *spec = codec->spec;
4796 const struct hda_input_mux *imux;
4799 imux = &spec->input_mux;
4800 for (i = 0; i < spec->am_num_entries; i++) {
4801 spec->am_entry[i].idx =
4802 find_idx_in_nid_list(spec->am_entry[i].pin,
4803 spec->imux_pins, imux->num_items);
4804 if (spec->am_entry[i].idx < 0)
4805 return false; /* no corresponding imux */
4808 /* we don't need the jack detection for the first pin */
4809 for (i = 1; i < spec->am_num_entries; i++)
4810 snd_hda_jack_detect_enable_callback(codec,
4811 spec->am_entry[i].pin,
4812 call_mic_autoswitch);
4816 static int compare_attr(const void *ap, const void *bp)
4818 const struct automic_entry *a = ap;
4819 const struct automic_entry *b = bp;
4820 return (int)(a->attr - b->attr);
4824 * Check the availability of auto-mic switch;
4825 * Set up if really supported
4827 static int check_auto_mic_availability(struct hda_codec *codec)
4829 struct hda_gen_spec *spec = codec->spec;
4830 struct auto_pin_cfg *cfg = &spec->autocfg;
4834 if (spec->suppress_auto_mic)
4839 for (i = 0; i < cfg->num_inputs; i++) {
4840 hda_nid_t nid = cfg->inputs[i].pin;
4842 attr = snd_hda_codec_get_pincfg(codec, nid);
4843 attr = snd_hda_get_input_pin_attr(attr);
4844 if (types & (1 << attr))
4845 return 0; /* already occupied */
4847 case INPUT_PIN_ATTR_INT:
4848 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4849 return 0; /* invalid type */
4851 case INPUT_PIN_ATTR_UNUSED:
4852 return 0; /* invalid entry */
4854 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4855 return 0; /* invalid type */
4856 if (!spec->line_in_auto_switch &&
4857 cfg->inputs[i].type != AUTO_PIN_MIC)
4858 return 0; /* only mic is allowed */
4859 if (!is_jack_detectable(codec, nid))
4860 return 0; /* no unsol support */
4863 if (num_pins >= MAX_AUTO_MIC_PINS)
4865 types |= (1 << attr);
4866 spec->am_entry[num_pins].pin = nid;
4867 spec->am_entry[num_pins].attr = attr;
4874 spec->am_num_entries = num_pins;
4875 /* sort the am_entry in the order of attr so that the pin with a
4876 * higher attr will be selected when the jack is plugged.
4878 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4879 compare_attr, NULL);
4881 if (!auto_mic_check_imux(codec))
4885 spec->num_adc_nids = 1;
4886 spec->cur_mux[0] = spec->am_entry[0].idx;
4887 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4888 spec->am_entry[0].pin,
4889 spec->am_entry[1].pin,
4890 spec->am_entry[2].pin);
4896 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4898 * @codec: the HDA codec
4899 * @nid: NID to evalute
4900 * @power_state: target power state
4902 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4904 unsigned int power_state)
4906 struct hda_gen_spec *spec = codec->spec;
4908 if (!spec->power_down_unused && !codec->power_save_node)
4910 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
4912 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4914 if (is_active_nid_for_any(codec, nid))
4918 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4920 /* mute all aamix inputs initially; parse up to the first leaves */
4921 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4924 const hda_nid_t *conn;
4927 nums = snd_hda_get_conn_list(codec, mix, &conn);
4928 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4929 for (i = 0; i < nums; i++) {
4931 update_amp(codec, mix, HDA_INPUT, i,
4932 0xff, HDA_AMP_MUTE);
4933 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4934 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4935 0xff, HDA_AMP_MUTE);
4940 * snd_hda_gen_stream_pm - Stream power management callback
4941 * @codec: the HDA codec
4942 * @nid: audio widget
4943 * @on: power on/off flag
4945 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
4947 void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4949 if (codec->power_save_node)
4950 set_path_power(codec, nid, -1, on);
4952 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4955 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4956 * set up the hda_gen_spec
4957 * @codec: the HDA codec
4958 * @cfg: Parsed pin configuration
4960 * return 1 if successful, 0 if the proper config is not found,
4961 * or a negative error code
4963 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4964 struct auto_pin_cfg *cfg)
4966 struct hda_gen_spec *spec = codec->spec;
4969 parse_user_hints(codec);
4971 if (spec->vmaster_mute_led || spec->mic_mute_led)
4972 snd_ctl_led_request();
4974 if (spec->mixer_nid && !spec->mixer_merge_nid)
4975 spec->mixer_merge_nid = spec->mixer_nid;
4977 if (cfg != &spec->autocfg) {
4978 spec->autocfg = *cfg;
4979 cfg = &spec->autocfg;
4982 if (!spec->main_out_badness)
4983 spec->main_out_badness = &hda_main_out_badness;
4984 if (!spec->extra_out_badness)
4985 spec->extra_out_badness = &hda_extra_out_badness;
4987 fill_all_dac_nids(codec);
4989 if (!cfg->line_outs) {
4990 if (cfg->dig_outs || cfg->dig_in_pin) {
4991 spec->multiout.max_channels = 2;
4992 spec->no_analog = 1;
4995 if (!cfg->num_inputs && !cfg->dig_in_pin)
4996 return 0; /* can't find valid BIOS pin config */
4999 if (!spec->no_primary_hp &&
5000 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
5001 cfg->line_outs <= cfg->hp_outs) {
5002 /* use HP as primary out */
5003 cfg->speaker_outs = cfg->line_outs;
5004 memcpy(cfg->speaker_pins, cfg->line_out_pins,
5005 sizeof(cfg->speaker_pins));
5006 cfg->line_outs = cfg->hp_outs;
5007 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
5009 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
5010 cfg->line_out_type = AUTO_PIN_HP_OUT;
5013 err = parse_output_paths(codec);
5016 err = create_multi_channel_mode(codec);
5019 err = create_multi_out_ctls(codec, cfg);
5022 err = create_hp_out_ctls(codec);
5025 err = create_speaker_out_ctls(codec);
5028 err = create_indep_hp_ctls(codec);
5031 err = create_loopback_mixing_ctl(codec);
5034 err = create_hp_mic(codec);
5037 err = create_input_ctls(codec);
5041 /* add power-down pin callbacks at first */
5042 add_all_pin_power_ctls(codec, false);
5044 spec->const_channel_count = spec->ext_channel_count;
5045 /* check the multiple speaker and headphone pins */
5046 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
5047 spec->const_channel_count = max(spec->const_channel_count,
5048 cfg->speaker_outs * 2);
5049 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
5050 spec->const_channel_count = max(spec->const_channel_count,
5052 spec->multiout.max_channels = max(spec->ext_channel_count,
5053 spec->const_channel_count);
5055 err = check_auto_mute_availability(codec);
5059 err = check_dyn_adc_switch(codec);
5063 err = check_auto_mic_availability(codec);
5067 /* add stereo mix if available and not enabled yet */
5068 if (!spec->auto_mic && spec->mixer_nid &&
5069 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
5070 spec->input_mux.num_items > 1) {
5071 err = parse_capture_source(codec, spec->mixer_nid,
5072 CFG_IDX_MIX, spec->num_all_adcs,
5079 err = create_capture_mixers(codec);
5083 err = parse_mic_boost(codec);
5087 /* create "Headphone Mic Jack Mode" if no input selection is
5088 * available (or user specifies add_jack_modes hint)
5090 if (spec->hp_mic_pin &&
5091 (spec->auto_mic || spec->input_mux.num_items == 1 ||
5092 spec->add_jack_modes)) {
5093 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
5098 if (spec->add_jack_modes) {
5099 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
5100 err = create_out_jack_modes(codec, cfg->line_outs,
5101 cfg->line_out_pins);
5105 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
5106 err = create_out_jack_modes(codec, cfg->hp_outs,
5113 /* add power-up pin callbacks at last */
5114 add_all_pin_power_ctls(codec, true);
5116 /* mute all aamix input initially */
5117 if (spec->mixer_nid)
5118 mute_all_mixer_nid(codec, spec->mixer_nid);
5121 parse_digital(codec);
5123 if (spec->power_down_unused || codec->power_save_node) {
5124 if (!codec->power_filter)
5125 codec->power_filter = snd_hda_gen_path_power_filter;
5126 if (!codec->patch_ops.stream_pm)
5127 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
5130 if (!spec->no_analog && spec->beep_nid) {
5131 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
5134 if (codec->beep && codec->power_save_node) {
5135 err = add_fake_beep_paths(codec);
5138 codec->beep->power_hook = beep_power_hook;
5144 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
5148 * Build control elements
5151 /* follower controls for virtual master */
5152 static const char * const follower_pfxs[] = {
5153 "Front", "Surround", "Center", "LFE", "Side",
5154 "Headphone", "Speaker", "Mono", "Line Out",
5155 "CLFE", "Bass Speaker", "PCM",
5156 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5157 "Headphone Front", "Headphone Surround", "Headphone CLFE",
5158 "Headphone Side", "Headphone+LO", "Speaker+LO",
5163 * snd_hda_gen_build_controls - Build controls from the parsed results
5164 * @codec: the HDA codec
5166 * Pass this to build_controls patch_ops.
5168 int snd_hda_gen_build_controls(struct hda_codec *codec)
5170 struct hda_gen_spec *spec = codec->spec;
5173 if (spec->kctls.used) {
5174 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5179 if (spec->multiout.dig_out_nid) {
5180 err = snd_hda_create_dig_out_ctls(codec,
5181 spec->multiout.dig_out_nid,
5182 spec->multiout.dig_out_nid,
5183 spec->pcm_rec[1]->pcm_type);
5186 if (!spec->no_analog) {
5187 err = snd_hda_create_spdif_share_sw(codec,
5191 spec->multiout.share_spdif = 1;
5194 if (spec->dig_in_nid) {
5195 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5200 /* if we have no master control, let's create it */
5201 if (!spec->no_analog && !spec->suppress_vmaster &&
5202 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
5203 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
5204 spec->vmaster_tlv, follower_pfxs,
5205 "Playback Volume", 0);
5209 if (!spec->no_analog && !spec->suppress_vmaster &&
5210 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5211 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5212 NULL, follower_pfxs,
5213 "Playback Switch", true,
5214 spec->vmaster_mute_led ?
5215 SNDRV_CTL_ELEM_ACCESS_SPK_LED : 0,
5216 &spec->vmaster_mute.sw_kctl);
5219 if (spec->vmaster_mute.hook) {
5220 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute);
5221 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5225 free_kctls(spec); /* no longer needed */
5227 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5233 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
5240 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5241 struct hda_codec *codec,
5242 struct snd_pcm_substream *substream,
5245 struct hda_gen_spec *spec = codec->spec;
5246 if (spec->pcm_playback_hook)
5247 spec->pcm_playback_hook(hinfo, codec, substream, action);
5250 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5251 struct hda_codec *codec,
5252 struct snd_pcm_substream *substream,
5255 struct hda_gen_spec *spec = codec->spec;
5256 if (spec->pcm_capture_hook)
5257 spec->pcm_capture_hook(hinfo, codec, substream, action);
5261 * Analog playback callbacks
5263 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5264 struct hda_codec *codec,
5265 struct snd_pcm_substream *substream)
5267 struct hda_gen_spec *spec = codec->spec;
5270 mutex_lock(&spec->pcm_mutex);
5271 err = snd_hda_multi_out_analog_open(codec,
5272 &spec->multiout, substream,
5275 spec->active_streams |= 1 << STREAM_MULTI_OUT;
5276 call_pcm_playback_hook(hinfo, codec, substream,
5277 HDA_GEN_PCM_ACT_OPEN);
5279 mutex_unlock(&spec->pcm_mutex);
5283 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5284 struct hda_codec *codec,
5285 unsigned int stream_tag,
5286 unsigned int format,
5287 struct snd_pcm_substream *substream)
5289 struct hda_gen_spec *spec = codec->spec;
5292 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5293 stream_tag, format, substream);
5295 call_pcm_playback_hook(hinfo, codec, substream,
5296 HDA_GEN_PCM_ACT_PREPARE);
5300 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5301 struct hda_codec *codec,
5302 struct snd_pcm_substream *substream)
5304 struct hda_gen_spec *spec = codec->spec;
5307 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5309 call_pcm_playback_hook(hinfo, codec, substream,
5310 HDA_GEN_PCM_ACT_CLEANUP);
5314 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5315 struct hda_codec *codec,
5316 struct snd_pcm_substream *substream)
5318 struct hda_gen_spec *spec = codec->spec;
5319 mutex_lock(&spec->pcm_mutex);
5320 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
5321 call_pcm_playback_hook(hinfo, codec, substream,
5322 HDA_GEN_PCM_ACT_CLOSE);
5323 mutex_unlock(&spec->pcm_mutex);
5327 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5328 struct hda_codec *codec,
5329 struct snd_pcm_substream *substream)
5331 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5335 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5336 struct hda_codec *codec,
5337 unsigned int stream_tag,
5338 unsigned int format,
5339 struct snd_pcm_substream *substream)
5341 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5342 call_pcm_capture_hook(hinfo, codec, substream,
5343 HDA_GEN_PCM_ACT_PREPARE);
5347 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5348 struct hda_codec *codec,
5349 struct snd_pcm_substream *substream)
5351 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5352 call_pcm_capture_hook(hinfo, codec, substream,
5353 HDA_GEN_PCM_ACT_CLEANUP);
5357 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5358 struct hda_codec *codec,
5359 struct snd_pcm_substream *substream)
5361 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5365 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5366 struct hda_codec *codec,
5367 struct snd_pcm_substream *substream)
5369 struct hda_gen_spec *spec = codec->spec;
5372 mutex_lock(&spec->pcm_mutex);
5373 if (spec->indep_hp && !spec->indep_hp_enabled)
5376 spec->active_streams |= 1 << STREAM_INDEP_HP;
5377 call_pcm_playback_hook(hinfo, codec, substream,
5378 HDA_GEN_PCM_ACT_OPEN);
5379 mutex_unlock(&spec->pcm_mutex);
5383 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5384 struct hda_codec *codec,
5385 struct snd_pcm_substream *substream)
5387 struct hda_gen_spec *spec = codec->spec;
5388 mutex_lock(&spec->pcm_mutex);
5389 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
5390 call_pcm_playback_hook(hinfo, codec, substream,
5391 HDA_GEN_PCM_ACT_CLOSE);
5392 mutex_unlock(&spec->pcm_mutex);
5396 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5397 struct hda_codec *codec,
5398 unsigned int stream_tag,
5399 unsigned int format,
5400 struct snd_pcm_substream *substream)
5402 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5403 call_pcm_playback_hook(hinfo, codec, substream,
5404 HDA_GEN_PCM_ACT_PREPARE);
5408 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5409 struct hda_codec *codec,
5410 struct snd_pcm_substream *substream)
5412 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5413 call_pcm_playback_hook(hinfo, codec, substream,
5414 HDA_GEN_PCM_ACT_CLEANUP);
5421 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5422 struct hda_codec *codec,
5423 struct snd_pcm_substream *substream)
5425 struct hda_gen_spec *spec = codec->spec;
5426 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5429 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5430 struct hda_codec *codec,
5431 unsigned int stream_tag,
5432 unsigned int format,
5433 struct snd_pcm_substream *substream)
5435 struct hda_gen_spec *spec = codec->spec;
5436 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5437 stream_tag, format, substream);
5440 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5441 struct hda_codec *codec,
5442 struct snd_pcm_substream *substream)
5444 struct hda_gen_spec *spec = codec->spec;
5445 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5448 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5449 struct hda_codec *codec,
5450 struct snd_pcm_substream *substream)
5452 struct hda_gen_spec *spec = codec->spec;
5453 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5459 #define alt_capture_pcm_open capture_pcm_open
5460 #define alt_capture_pcm_close capture_pcm_close
5462 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5463 struct hda_codec *codec,
5464 unsigned int stream_tag,
5465 unsigned int format,
5466 struct snd_pcm_substream *substream)
5468 struct hda_gen_spec *spec = codec->spec;
5470 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5471 stream_tag, 0, format);
5472 call_pcm_capture_hook(hinfo, codec, substream,
5473 HDA_GEN_PCM_ACT_PREPARE);
5477 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5478 struct hda_codec *codec,
5479 struct snd_pcm_substream *substream)
5481 struct hda_gen_spec *spec = codec->spec;
5483 snd_hda_codec_cleanup_stream(codec,
5484 spec->adc_nids[substream->number + 1]);
5485 call_pcm_capture_hook(hinfo, codec, substream,
5486 HDA_GEN_PCM_ACT_CLEANUP);
5492 static const struct hda_pcm_stream pcm_analog_playback = {
5496 /* NID is set in build_pcms */
5498 .open = playback_pcm_open,
5499 .close = playback_pcm_close,
5500 .prepare = playback_pcm_prepare,
5501 .cleanup = playback_pcm_cleanup
5505 static const struct hda_pcm_stream pcm_analog_capture = {
5509 /* NID is set in build_pcms */
5511 .open = capture_pcm_open,
5512 .close = capture_pcm_close,
5513 .prepare = capture_pcm_prepare,
5514 .cleanup = capture_pcm_cleanup
5518 static const struct hda_pcm_stream pcm_analog_alt_playback = {
5522 /* NID is set in build_pcms */
5524 .open = alt_playback_pcm_open,
5525 .close = alt_playback_pcm_close,
5526 .prepare = alt_playback_pcm_prepare,
5527 .cleanup = alt_playback_pcm_cleanup
5531 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5532 .substreams = 2, /* can be overridden */
5535 /* NID is set in build_pcms */
5537 .open = alt_capture_pcm_open,
5538 .close = alt_capture_pcm_close,
5539 .prepare = alt_capture_pcm_prepare,
5540 .cleanup = alt_capture_pcm_cleanup
5544 static const struct hda_pcm_stream pcm_digital_playback = {
5548 /* NID is set in build_pcms */
5550 .open = dig_playback_pcm_open,
5551 .close = dig_playback_pcm_close,
5552 .prepare = dig_playback_pcm_prepare,
5553 .cleanup = dig_playback_pcm_cleanup
5557 static const struct hda_pcm_stream pcm_digital_capture = {
5561 /* NID is set in build_pcms */
5564 /* Used by build_pcms to flag that a PCM has no playback stream */
5565 static const struct hda_pcm_stream pcm_null_stream = {
5572 * dynamic changing ADC PCM streams
5574 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5576 struct hda_gen_spec *spec = codec->spec;
5577 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5579 if (spec->cur_adc && spec->cur_adc != new_adc) {
5580 /* stream is running, let's swap the current ADC */
5581 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5582 spec->cur_adc = new_adc;
5583 snd_hda_codec_setup_stream(codec, new_adc,
5584 spec->cur_adc_stream_tag, 0,
5585 spec->cur_adc_format);
5591 /* analog capture with dynamic dual-adc changes */
5592 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5593 struct hda_codec *codec,
5594 unsigned int stream_tag,
5595 unsigned int format,
5596 struct snd_pcm_substream *substream)
5598 struct hda_gen_spec *spec = codec->spec;
5599 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5600 spec->cur_adc_stream_tag = stream_tag;
5601 spec->cur_adc_format = format;
5602 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5603 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
5607 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5608 struct hda_codec *codec,
5609 struct snd_pcm_substream *substream)
5611 struct hda_gen_spec *spec = codec->spec;
5612 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5614 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
5618 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5622 .nid = 0, /* fill later */
5624 .prepare = dyn_adc_capture_pcm_prepare,
5625 .cleanup = dyn_adc_capture_pcm_cleanup
5629 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5630 const char *chip_name)
5636 strscpy(str, chip_name, len);
5638 /* drop non-alnum chars after a space */
5639 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5640 if (!isalnum(p[1])) {
5645 strlcat(str, sfx, len);
5648 /* copy PCM stream info from @default_str, and override non-NULL entries
5649 * from @spec_str and @nid
5651 static void setup_pcm_stream(struct hda_pcm_stream *str,
5652 const struct hda_pcm_stream *default_str,
5653 const struct hda_pcm_stream *spec_str,
5656 *str = *default_str;
5660 if (spec_str->substreams)
5661 str->substreams = spec_str->substreams;
5662 if (spec_str->channels_min)
5663 str->channels_min = spec_str->channels_min;
5664 if (spec_str->channels_max)
5665 str->channels_max = spec_str->channels_max;
5666 if (spec_str->rates)
5667 str->rates = spec_str->rates;
5668 if (spec_str->formats)
5669 str->formats = spec_str->formats;
5670 if (spec_str->maxbps)
5671 str->maxbps = spec_str->maxbps;
5676 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5677 * @codec: the HDA codec
5679 * Pass this to build_pcms patch_ops.
5681 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5683 struct hda_gen_spec *spec = codec->spec;
5684 struct hda_pcm *info;
5685 bool have_multi_adcs;
5687 if (spec->no_analog)
5690 fill_pcm_stream_name(spec->stream_name_analog,
5691 sizeof(spec->stream_name_analog),
5692 " Analog", codec->core.chip_name);
5693 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5696 spec->pcm_rec[0] = info;
5698 if (spec->multiout.num_dacs > 0) {
5699 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5700 &pcm_analog_playback,
5701 spec->stream_analog_playback,
5702 spec->multiout.dac_nids[0]);
5703 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5704 spec->multiout.max_channels;
5705 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5706 spec->autocfg.line_outs == 2)
5707 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5710 if (spec->num_adc_nids) {
5711 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5712 (spec->dyn_adc_switch ?
5713 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5714 spec->stream_analog_capture,
5719 /* SPDIF for stream index #1 */
5720 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5721 fill_pcm_stream_name(spec->stream_name_digital,
5722 sizeof(spec->stream_name_digital),
5723 " Digital", codec->core.chip_name);
5724 info = snd_hda_codec_pcm_new(codec, "%s",
5725 spec->stream_name_digital);
5728 codec->follower_dig_outs = spec->multiout.follower_dig_outs;
5729 spec->pcm_rec[1] = info;
5730 if (spec->dig_out_type)
5731 info->pcm_type = spec->dig_out_type;
5733 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5734 if (spec->multiout.dig_out_nid)
5735 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5736 &pcm_digital_playback,
5737 spec->stream_digital_playback,
5738 spec->multiout.dig_out_nid);
5739 if (spec->dig_in_nid)
5740 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5741 &pcm_digital_capture,
5742 spec->stream_digital_capture,
5746 if (spec->no_analog)
5749 /* If the use of more than one ADC is requested for the current
5750 * model, configure a second analog capture-only PCM.
5752 have_multi_adcs = (spec->num_adc_nids > 1) &&
5753 !spec->dyn_adc_switch && !spec->auto_mic;
5754 /* Additional Analaog capture for index #2 */
5755 if (spec->alt_dac_nid || have_multi_adcs) {
5756 fill_pcm_stream_name(spec->stream_name_alt_analog,
5757 sizeof(spec->stream_name_alt_analog),
5758 " Alt Analog", codec->core.chip_name);
5759 info = snd_hda_codec_pcm_new(codec, "%s",
5760 spec->stream_name_alt_analog);
5763 spec->pcm_rec[2] = info;
5764 if (spec->alt_dac_nid)
5765 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5766 &pcm_analog_alt_playback,
5767 spec->stream_analog_alt_playback,
5770 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5771 &pcm_null_stream, NULL, 0);
5772 if (have_multi_adcs) {
5773 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5774 &pcm_analog_alt_capture,
5775 spec->stream_analog_alt_capture,
5777 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5778 spec->num_adc_nids - 1;
5780 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5781 &pcm_null_stream, NULL, 0);
5787 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5791 * Standard auto-parser initializations
5794 /* configure the given path as a proper output */
5795 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5797 struct nid_path *path;
5800 path = snd_hda_get_path_from_idx(codec, path_idx);
5801 if (!path || !path->depth)
5803 pin = path->path[path->depth - 1];
5804 restore_pin_ctl(codec, pin);
5805 snd_hda_activate_path(codec, path, path->active,
5806 aamix_default(codec->spec));
5807 set_pin_eapd(codec, pin, path->active);
5810 /* initialize primary output paths */
5811 static void init_multi_out(struct hda_codec *codec)
5813 struct hda_gen_spec *spec = codec->spec;
5816 for (i = 0; i < spec->autocfg.line_outs; i++)
5817 set_output_and_unmute(codec, spec->out_paths[i]);
5821 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5825 for (i = 0; i < num_outs; i++)
5826 set_output_and_unmute(codec, paths[i]);
5829 /* initialize hp and speaker paths */
5830 static void init_extra_out(struct hda_codec *codec)
5832 struct hda_gen_spec *spec = codec->spec;
5834 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5835 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5836 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5837 __init_extra_out(codec, spec->autocfg.speaker_outs,
5838 spec->speaker_paths);
5841 /* initialize multi-io paths */
5842 static void init_multi_io(struct hda_codec *codec)
5844 struct hda_gen_spec *spec = codec->spec;
5847 for (i = 0; i < spec->multi_ios; i++) {
5848 hda_nid_t pin = spec->multi_io[i].pin;
5849 struct nid_path *path;
5850 path = get_multiio_path(codec, i);
5853 if (!spec->multi_io[i].ctl_in)
5854 spec->multi_io[i].ctl_in =
5855 snd_hda_codec_get_pin_target(codec, pin);
5856 snd_hda_activate_path(codec, path, path->active,
5857 aamix_default(spec));
5861 static void init_aamix_paths(struct hda_codec *codec)
5863 struct hda_gen_spec *spec = codec->spec;
5865 if (!spec->have_aamix_ctl)
5867 if (!has_aamix_out_paths(spec))
5869 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5870 spec->aamix_out_paths[0],
5871 spec->autocfg.line_out_type);
5872 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5873 spec->aamix_out_paths[1],
5875 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5876 spec->aamix_out_paths[2],
5877 AUTO_PIN_SPEAKER_OUT);
5880 /* set up input pins and loopback paths */
5881 static void init_analog_input(struct hda_codec *codec)
5883 struct hda_gen_spec *spec = codec->spec;
5884 struct auto_pin_cfg *cfg = &spec->autocfg;
5887 for (i = 0; i < cfg->num_inputs; i++) {
5888 hda_nid_t nid = cfg->inputs[i].pin;
5889 if (is_input_pin(codec, nid))
5890 restore_pin_ctl(codec, nid);
5892 /* init loopback inputs */
5893 if (spec->mixer_nid) {
5894 resume_path_from_idx(codec, spec->loopback_paths[i]);
5895 resume_path_from_idx(codec, spec->loopback_merge_path);
5900 /* initialize ADC paths */
5901 static void init_input_src(struct hda_codec *codec)
5903 struct hda_gen_spec *spec = codec->spec;
5904 struct hda_input_mux *imux = &spec->input_mux;
5905 struct nid_path *path;
5908 if (spec->dyn_adc_switch)
5911 nums = spec->num_adc_nids;
5913 for (c = 0; c < nums; c++) {
5914 for (i = 0; i < imux->num_items; i++) {
5915 path = get_input_path(codec, c, i);
5917 bool active = path->active;
5918 if (i == spec->cur_mux[c])
5920 snd_hda_activate_path(codec, path, active, false);
5924 update_hp_mic(codec, c, true);
5927 if (spec->cap_sync_hook)
5928 spec->cap_sync_hook(codec, NULL, NULL);
5931 /* set right pin controls for digital I/O */
5932 static void init_digital(struct hda_codec *codec)
5934 struct hda_gen_spec *spec = codec->spec;
5938 for (i = 0; i < spec->autocfg.dig_outs; i++)
5939 set_output_and_unmute(codec, spec->digout_paths[i]);
5940 pin = spec->autocfg.dig_in_pin;
5942 restore_pin_ctl(codec, pin);
5943 resume_path_from_idx(codec, spec->digin_path);
5947 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5948 * invalid unsol tags by some reason
5950 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5952 const struct hda_pincfg *pin;
5955 snd_array_for_each(&codec->init_pins, i, pin) {
5956 hda_nid_t nid = pin->nid;
5957 if (is_jack_detectable(codec, nid) &&
5958 !snd_hda_jack_tbl_get(codec, nid))
5959 snd_hda_codec_write_cache(codec, nid, 0,
5960 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5965 * snd_hda_gen_init - initialize the generic spec
5966 * @codec: the HDA codec
5968 * This can be put as patch_ops init function.
5970 int snd_hda_gen_init(struct hda_codec *codec)
5972 struct hda_gen_spec *spec = codec->spec;
5974 if (spec->init_hook)
5975 spec->init_hook(codec);
5977 if (!spec->skip_verbs)
5978 snd_hda_apply_verbs(codec);
5980 init_multi_out(codec);
5981 init_extra_out(codec);
5982 init_multi_io(codec);
5983 init_aamix_paths(codec);
5984 init_analog_input(codec);
5985 init_input_src(codec);
5986 init_digital(codec);
5988 clear_unsol_on_unused_pins(codec);
5990 sync_all_pin_power_ctls(codec);
5992 /* call init functions of standard auto-mute helpers */
5993 update_automute_all(codec);
5995 snd_hda_regmap_sync(codec);
5997 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5998 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
6000 hda_call_check_power_status(codec, 0x01);
6003 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
6006 * snd_hda_gen_free - free the generic spec
6007 * @codec: the HDA codec
6009 * This can be put as patch_ops free function.
6011 void snd_hda_gen_free(struct hda_codec *codec)
6013 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
6014 snd_hda_gen_spec_free(codec->spec);
6018 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
6022 * snd_hda_gen_check_power_status - check the loopback power save state
6023 * @codec: the HDA codec
6024 * @nid: NID to inspect
6026 * This can be put as patch_ops check_power_status function.
6028 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
6030 struct hda_gen_spec *spec = codec->spec;
6031 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
6033 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
6038 * the generic codec support
6041 static const struct hda_codec_ops generic_patch_ops = {
6042 .build_controls = snd_hda_gen_build_controls,
6043 .build_pcms = snd_hda_gen_build_pcms,
6044 .init = snd_hda_gen_init,
6045 .free = snd_hda_gen_free,
6046 .unsol_event = snd_hda_jack_unsol_event,
6048 .check_power_status = snd_hda_gen_check_power_status,
6053 * snd_hda_parse_generic_codec - Generic codec parser
6054 * @codec: the HDA codec
6056 static int snd_hda_parse_generic_codec(struct hda_codec *codec)
6058 struct hda_gen_spec *spec;
6061 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
6064 snd_hda_gen_spec_init(spec);
6067 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
6071 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
6075 codec->patch_ops = generic_patch_ops;
6079 snd_hda_gen_free(codec);
6083 static const struct hda_device_id snd_hda_id_generic[] = {
6084 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
6087 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
6089 static struct hda_codec_driver generic_driver = {
6090 .id = snd_hda_id_generic,
6093 module_hda_codec_driver(generic_driver);
6095 MODULE_LICENSE("GPL");
6096 MODULE_DESCRIPTION("Generic HD-audio codec parser");