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];
1003 len = snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
1004 if (snd_BUG_ON(len >= sizeof(name)))
1006 if (!add_control(spec, type, name, cidx, val))
1011 #define add_pb_vol_ctrl(spec, type, pfx, val) \
1012 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1013 #define add_pb_sw_ctrl(spec, type, pfx, val) \
1014 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1015 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1016 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1017 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1018 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1020 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1021 unsigned int chs, struct nid_path *path)
1026 val = path->ctls[NID_PATH_VOL_CTL];
1029 val = amp_val_replace_channels(val, chs);
1030 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1033 /* return the channel bits suitable for the given path->ctls[] */
1034 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1037 int chs = 1; /* mono (left only) */
1039 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1040 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1041 chs = 3; /* stereo */
1046 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1047 struct nid_path *path)
1049 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1050 return add_vol_ctl(codec, pfx, cidx, chs, path);
1053 /* create a mute-switch for the given mixer widget;
1054 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1056 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1057 unsigned int chs, struct nid_path *path)
1060 int type = HDA_CTL_WIDGET_MUTE;
1064 val = path->ctls[NID_PATH_MUTE_CTL];
1067 val = amp_val_replace_channels(val, chs);
1068 if (get_amp_direction_(val) == HDA_INPUT) {
1069 hda_nid_t nid = get_amp_nid_(val);
1070 int nums = snd_hda_get_num_conns(codec, nid);
1072 type = HDA_CTL_BIND_MUTE;
1076 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1079 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1080 int cidx, struct nid_path *path)
1082 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1083 return add_sw_ctl(codec, pfx, cidx, chs, path);
1086 /* playback mute control with the software mute bit check */
1087 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1088 struct snd_ctl_elem_value *ucontrol)
1090 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1091 struct hda_gen_spec *spec = codec->spec;
1093 if (spec->auto_mute_via_amp) {
1094 hda_nid_t nid = get_amp_nid(kcontrol);
1095 bool enabled = !((spec->mute_bits >> nid) & 1);
1096 ucontrol->value.integer.value[0] &= enabled;
1097 ucontrol->value.integer.value[1] &= enabled;
1101 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1102 struct snd_ctl_elem_value *ucontrol)
1104 sync_auto_mute_bits(kcontrol, ucontrol);
1105 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1109 * Bound mute controls
1111 #define AMP_VAL_IDX_SHIFT 19
1112 #define AMP_VAL_IDX_MASK (0x0f<<19)
1114 static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
1115 struct snd_ctl_elem_value *ucontrol)
1117 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1121 mutex_lock(&codec->control_mutex);
1122 pval = kcontrol->private_value;
1123 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1124 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1125 kcontrol->private_value = pval;
1126 mutex_unlock(&codec->control_mutex);
1130 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1131 struct snd_ctl_elem_value *ucontrol)
1133 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1135 int i, indices, err = 0, change = 0;
1137 sync_auto_mute_bits(kcontrol, ucontrol);
1139 mutex_lock(&codec->control_mutex);
1140 pval = kcontrol->private_value;
1141 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1142 for (i = 0; i < indices; i++) {
1143 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1144 (i << AMP_VAL_IDX_SHIFT);
1145 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1150 kcontrol->private_value = pval;
1151 mutex_unlock(&codec->control_mutex);
1152 return err < 0 ? err : change;
1155 /* any ctl assigned to the path with the given index? */
1156 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1158 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1159 return path && path->ctls[ctl_type];
1162 static const char * const channel_name[] = {
1163 "Front", "Surround", "CLFE", "Side", "Back",
1166 /* give some appropriate ctl name prefix for the given line out channel */
1167 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1168 int *index, int ctl_type)
1170 struct hda_gen_spec *spec = codec->spec;
1171 struct auto_pin_cfg *cfg = &spec->autocfg;
1174 if (cfg->line_outs == 1 && !spec->multi_ios &&
1175 !codec->force_pin_prefix &&
1176 !cfg->hp_outs && !cfg->speaker_outs)
1177 return spec->vmaster_mute.hook ? "PCM" : "Master";
1179 /* if there is really a single DAC used in the whole output paths,
1180 * use it master (or "PCM" if a vmaster hook is present)
1182 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1183 !codec->force_pin_prefix &&
1184 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1185 return spec->vmaster_mute.hook ? "PCM" : "Master";
1187 /* multi-io channels */
1188 if (ch >= cfg->line_outs)
1191 switch (cfg->line_out_type) {
1192 case AUTO_PIN_SPEAKER_OUT:
1193 /* if the primary channel vol/mute is shared with HP volume,
1194 * don't name it as Speaker
1196 if (!ch && cfg->hp_outs &&
1197 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1199 if (cfg->line_outs == 1)
1201 if (cfg->line_outs == 2)
1202 return ch ? "Bass Speaker" : "Speaker";
1204 case AUTO_PIN_HP_OUT:
1205 /* if the primary channel vol/mute is shared with spk volume,
1206 * don't name it as Headphone
1208 if (!ch && cfg->speaker_outs &&
1209 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1211 /* for multi-io case, only the primary out */
1212 if (ch && spec->multi_ios)
1216 case AUTO_PIN_LINE_OUT:
1217 /* This deals with the case where one HP or one Speaker or
1218 * one HP + one Speaker need to share the DAC with LO
1221 bool hp_lo_shared = false, spk_lo_shared = false;
1223 if (cfg->speaker_outs)
1224 spk_lo_shared = !path_has_mixer(codec,
1225 spec->speaker_paths[0], ctl_type);
1227 hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1228 if (hp_lo_shared && spk_lo_shared)
1229 return spec->vmaster_mute.hook ? "PCM" : "Master";
1231 return "Headphone+LO";
1233 return "Speaker+LO";
1237 /* for a single channel output, we don't have to name the channel */
1238 if (cfg->line_outs == 1 && !spec->multi_ios)
1242 if (ch >= ARRAY_SIZE(channel_name)) {
1247 return channel_name[ch];
1251 * Parse output paths
1254 /* badness definition */
1256 /* No primary DAC is found for the main output */
1257 BAD_NO_PRIMARY_DAC = 0x10000,
1258 /* No DAC is found for the extra output */
1259 BAD_NO_DAC = 0x4000,
1260 /* No possible multi-ios */
1261 BAD_MULTI_IO = 0x120,
1262 /* No individual DAC for extra output */
1263 BAD_NO_EXTRA_DAC = 0x102,
1264 /* No individual DAC for extra surrounds */
1265 BAD_NO_EXTRA_SURR_DAC = 0x101,
1266 /* Primary DAC shared with main surrounds */
1267 BAD_SHARED_SURROUND = 0x100,
1268 /* No independent HP possible */
1269 BAD_NO_INDEP_HP = 0x10,
1270 /* Primary DAC shared with main CLFE */
1271 BAD_SHARED_CLFE = 0x10,
1272 /* Primary DAC shared with extra surrounds */
1273 BAD_SHARED_EXTRA_SURROUND = 0x10,
1274 /* Volume widget is shared */
1275 BAD_SHARED_VOL = 0x10,
1278 /* look for widgets in the given path which are appropriate for
1279 * volume and mute controls, and assign the values to ctls[].
1281 * When no appropriate widget is found in the path, the badness value
1282 * is incremented depending on the situation. The function returns the
1283 * total badness for both volume and mute controls.
1285 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1287 struct hda_gen_spec *spec = codec->spec;
1293 return BAD_SHARED_VOL * 2;
1295 if (path->ctls[NID_PATH_VOL_CTL] ||
1296 path->ctls[NID_PATH_MUTE_CTL])
1297 return 0; /* already evaluated */
1299 nid = look_for_out_vol_nid(codec, path);
1301 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1302 if (spec->dac_min_mute)
1303 val |= HDA_AMP_VAL_MIN_MUTE;
1304 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1305 badness += BAD_SHARED_VOL;
1307 path->ctls[NID_PATH_VOL_CTL] = val;
1309 badness += BAD_SHARED_VOL;
1310 nid = look_for_out_mute_nid(codec, path);
1312 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1313 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1314 nid_has_mute(codec, nid, HDA_OUTPUT))
1315 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1317 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1318 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1319 badness += BAD_SHARED_VOL;
1321 path->ctls[NID_PATH_MUTE_CTL] = val;
1323 badness += BAD_SHARED_VOL;
1327 const struct badness_table hda_main_out_badness = {
1328 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1329 .no_dac = BAD_NO_DAC,
1330 .shared_primary = BAD_NO_PRIMARY_DAC,
1331 .shared_surr = BAD_SHARED_SURROUND,
1332 .shared_clfe = BAD_SHARED_CLFE,
1333 .shared_surr_main = BAD_SHARED_SURROUND,
1335 EXPORT_SYMBOL_GPL(hda_main_out_badness);
1337 const struct badness_table hda_extra_out_badness = {
1338 .no_primary_dac = BAD_NO_DAC,
1339 .no_dac = BAD_NO_DAC,
1340 .shared_primary = BAD_NO_EXTRA_DAC,
1341 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1342 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1343 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1345 EXPORT_SYMBOL_GPL(hda_extra_out_badness);
1347 /* get the DAC of the primary output corresponding to the given array index */
1348 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1350 struct hda_gen_spec *spec = codec->spec;
1351 struct auto_pin_cfg *cfg = &spec->autocfg;
1353 if (cfg->line_outs > idx)
1354 return spec->private_dac_nids[idx];
1355 idx -= cfg->line_outs;
1356 if (spec->multi_ios > idx)
1357 return spec->multi_io[idx].dac;
1361 /* return the DAC if it's reachable, otherwise zero */
1362 static inline hda_nid_t try_dac(struct hda_codec *codec,
1363 hda_nid_t dac, hda_nid_t pin)
1365 return is_reachable_path(codec, dac, pin) ? dac : 0;
1368 /* try to assign DACs to pins and return the resultant badness */
1369 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1370 const hda_nid_t *pins, hda_nid_t *dacs,
1372 const struct badness_table *bad)
1374 struct hda_gen_spec *spec = codec->spec;
1382 for (i = 0; i < num_outs; i++) {
1383 struct nid_path *path;
1384 hda_nid_t pin = pins[i];
1386 if (!spec->obey_preferred_dacs) {
1387 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1389 badness += assign_out_path_ctls(codec, path);
1394 dacs[i] = get_preferred_dac(codec, pin);
1396 if (is_dac_already_used(codec, dacs[i]))
1397 badness += bad->shared_primary;
1398 } else if (spec->obey_preferred_dacs) {
1399 badness += BAD_NO_PRIMARY_DAC;
1403 dacs[i] = look_for_dac(codec, pin, false);
1404 if (!dacs[i] && !i) {
1405 /* try to steal the DAC of surrounds for the front */
1406 for (j = 1; j < num_outs; j++) {
1407 if (is_reachable_path(codec, dacs[j], pin)) {
1410 invalidate_nid_path(codec, path_idx[j]);
1419 dac = try_dac(codec, get_primary_out(codec, i), pin);
1421 dac = try_dac(codec, dacs[0], pin);
1423 dac = try_dac(codec, get_primary_out(codec, i), pin);
1426 badness += bad->shared_primary;
1428 badness += bad->shared_surr;
1430 badness += bad->shared_clfe;
1431 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1432 dac = spec->private_dac_nids[0];
1433 badness += bad->shared_surr_main;
1435 badness += bad->no_primary_dac;
1437 badness += bad->no_dac;
1441 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1442 if (!path && !i && spec->mixer_nid) {
1443 /* try with aamix */
1444 path = snd_hda_add_new_path(codec, dac, pin, 0);
1448 badness += bad->no_dac;
1450 /* print_nid_path(codec, "output", path); */
1451 path->active = true;
1452 path_idx[i] = snd_hda_get_path_idx(codec, path);
1453 badness += assign_out_path_ctls(codec, path);
1460 /* return NID if the given pin has only a single connection to a certain DAC */
1461 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1463 struct hda_gen_spec *spec = codec->spec;
1465 hda_nid_t nid_found = 0;
1467 for (i = 0; i < spec->num_all_dacs; i++) {
1468 hda_nid_t nid = spec->all_dacs[i];
1469 if (!nid || is_dac_already_used(codec, nid))
1471 if (is_reachable_path(codec, nid, pin)) {
1480 /* check whether the given pin can be a multi-io pin */
1481 static bool can_be_multiio_pin(struct hda_codec *codec,
1482 unsigned int location, hda_nid_t nid)
1484 unsigned int defcfg, caps;
1486 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1487 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1489 if (location && get_defcfg_location(defcfg) != location)
1491 caps = snd_hda_query_pin_caps(codec, nid);
1492 if (!(caps & AC_PINCAP_OUT))
1497 /* count the number of input pins that are capable to be multi-io */
1498 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1500 struct hda_gen_spec *spec = codec->spec;
1501 struct auto_pin_cfg *cfg = &spec->autocfg;
1502 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1503 unsigned int location = get_defcfg_location(defcfg);
1507 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1508 for (i = 0; i < cfg->num_inputs; i++) {
1509 if (cfg->inputs[i].type != type)
1511 if (can_be_multiio_pin(codec, location,
1512 cfg->inputs[i].pin))
1522 * When hardwired is set, try to fill ony hardwired pins, and returns
1523 * zero if any pins are filled, non-zero if nothing found.
1524 * When hardwired is off, try to fill possible input pins, and returns
1525 * the badness value.
1527 static int fill_multi_ios(struct hda_codec *codec,
1528 hda_nid_t reference_pin,
1531 struct hda_gen_spec *spec = codec->spec;
1532 struct auto_pin_cfg *cfg = &spec->autocfg;
1533 int type, i, j, num_pins, old_pins;
1534 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1535 unsigned int location = get_defcfg_location(defcfg);
1537 struct nid_path *path;
1539 old_pins = spec->multi_ios;
1543 num_pins = count_multiio_pins(codec, reference_pin);
1547 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1548 for (i = 0; i < cfg->num_inputs; i++) {
1549 hda_nid_t nid = cfg->inputs[i].pin;
1552 if (cfg->inputs[i].type != type)
1554 if (!can_be_multiio_pin(codec, location, nid))
1556 for (j = 0; j < spec->multi_ios; j++) {
1557 if (nid == spec->multi_io[j].pin)
1560 if (j < spec->multi_ios)
1564 dac = get_dac_if_single(codec, nid);
1566 dac = look_for_dac(codec, nid, false);
1571 path = snd_hda_add_new_path(codec, dac, nid,
1577 /* print_nid_path(codec, "multiio", path); */
1578 spec->multi_io[spec->multi_ios].pin = nid;
1579 spec->multi_io[spec->multi_ios].dac = dac;
1580 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1581 snd_hda_get_path_idx(codec, path);
1583 if (spec->multi_ios >= 2)
1589 badness = BAD_MULTI_IO;
1590 if (old_pins == spec->multi_ios) {
1592 return 1; /* nothing found */
1594 return badness; /* no badness if nothing found */
1596 if (!hardwired && spec->multi_ios < 2) {
1597 /* cancel newly assigned paths */
1598 spec->paths.used -= spec->multi_ios - old_pins;
1599 spec->multi_ios = old_pins;
1603 /* assign volume and mute controls */
1604 for (i = old_pins; i < spec->multi_ios; i++) {
1605 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1606 badness += assign_out_path_ctls(codec, path);
1612 /* map DACs for all pins in the list if they are single connections */
1613 static bool map_singles(struct hda_codec *codec, int outs,
1614 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1616 struct hda_gen_spec *spec = codec->spec;
1619 for (i = 0; i < outs; i++) {
1620 struct nid_path *path;
1624 dac = get_dac_if_single(codec, pins[i]);
1627 path = snd_hda_add_new_path(codec, dac, pins[i],
1629 if (!path && !i && spec->mixer_nid)
1630 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1634 /* print_nid_path(codec, "output", path); */
1635 path->active = true;
1636 path_idx[i] = snd_hda_get_path_idx(codec, path);
1642 static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1644 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1645 spec->aamix_out_paths[2];
1648 /* create a new path including aamix if available, and return its index */
1649 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1651 struct hda_gen_spec *spec = codec->spec;
1652 struct nid_path *path;
1653 hda_nid_t path_dac, dac, pin;
1655 path = snd_hda_get_path_from_idx(codec, path_idx);
1656 if (!path || !path->depth ||
1657 is_nid_contained(path, spec->mixer_nid))
1659 path_dac = path->path[0];
1660 dac = spec->private_dac_nids[0];
1661 pin = path->path[path->depth - 1];
1662 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1664 if (dac != path_dac)
1666 else if (spec->multiout.hp_out_nid[0])
1667 dac = spec->multiout.hp_out_nid[0];
1668 else if (spec->multiout.extra_out_nid[0])
1669 dac = spec->multiout.extra_out_nid[0];
1673 path = snd_hda_add_new_path(codec, dac, pin,
1678 /* print_nid_path(codec, "output-aamix", path); */
1679 path->active = false; /* unused as default */
1680 path->pin_fixed = true; /* static route */
1681 return snd_hda_get_path_idx(codec, path);
1684 /* check whether the independent HP is available with the current config */
1685 static bool indep_hp_possible(struct hda_codec *codec)
1687 struct hda_gen_spec *spec = codec->spec;
1688 struct auto_pin_cfg *cfg = &spec->autocfg;
1689 struct nid_path *path;
1692 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1693 idx = spec->out_paths[0];
1695 idx = spec->hp_paths[0];
1696 path = snd_hda_get_path_from_idx(codec, idx);
1700 /* assume no path conflicts unless aamix is involved */
1701 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1704 /* check whether output paths contain aamix */
1705 for (i = 0; i < cfg->line_outs; i++) {
1706 if (spec->out_paths[i] == idx)
1708 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1709 if (path && is_nid_contained(path, spec->mixer_nid))
1712 for (i = 0; i < cfg->speaker_outs; i++) {
1713 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1714 if (path && is_nid_contained(path, spec->mixer_nid))
1721 /* fill the empty entries in the dac array for speaker/hp with the
1722 * shared dac pointed by the paths
1724 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1725 hda_nid_t *dacs, int *path_idx)
1727 struct nid_path *path;
1730 for (i = 0; i < num_outs; i++) {
1733 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1736 dacs[i] = path->path[0];
1740 /* fill in the dac_nids table from the parsed pin configuration */
1741 static int fill_and_eval_dacs(struct hda_codec *codec,
1742 bool fill_hardwired,
1743 bool fill_mio_first)
1745 struct hda_gen_spec *spec = codec->spec;
1746 struct auto_pin_cfg *cfg = &spec->autocfg;
1747 int i, err, badness;
1749 /* set num_dacs once to full for look_for_dac() */
1750 spec->multiout.num_dacs = cfg->line_outs;
1751 spec->multiout.dac_nids = spec->private_dac_nids;
1752 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1753 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1754 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1755 spec->multi_ios = 0;
1756 snd_array_free(&spec->paths);
1758 /* clear path indices */
1759 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1760 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1761 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1762 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1763 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1764 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1765 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1766 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1770 /* fill hard-wired DACs first */
1771 if (fill_hardwired) {
1774 mapped = map_singles(codec, cfg->line_outs,
1776 spec->private_dac_nids,
1778 mapped |= map_singles(codec, cfg->hp_outs,
1780 spec->multiout.hp_out_nid,
1782 mapped |= map_singles(codec, cfg->speaker_outs,
1784 spec->multiout.extra_out_nid,
1785 spec->speaker_paths);
1786 if (!spec->no_multi_io &&
1787 fill_mio_first && cfg->line_outs == 1 &&
1788 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1789 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1796 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1797 spec->private_dac_nids, spec->out_paths,
1798 spec->main_out_badness);
1800 if (!spec->no_multi_io && fill_mio_first &&
1801 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1802 /* try to fill multi-io first */
1803 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1806 /* we don't count badness at this stage yet */
1809 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1810 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1811 spec->multiout.hp_out_nid,
1813 spec->extra_out_badness);
1818 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1819 err = try_assign_dacs(codec, cfg->speaker_outs,
1821 spec->multiout.extra_out_nid,
1822 spec->speaker_paths,
1823 spec->extra_out_badness);
1828 if (!spec->no_multi_io &&
1829 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1830 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1836 if (spec->mixer_nid) {
1837 spec->aamix_out_paths[0] =
1838 check_aamix_out_path(codec, spec->out_paths[0]);
1839 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1840 spec->aamix_out_paths[1] =
1841 check_aamix_out_path(codec, spec->hp_paths[0]);
1842 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1843 spec->aamix_out_paths[2] =
1844 check_aamix_out_path(codec, spec->speaker_paths[0]);
1847 if (!spec->no_multi_io &&
1848 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1849 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1850 spec->multi_ios = 1; /* give badness */
1852 /* re-count num_dacs and squash invalid entries */
1853 spec->multiout.num_dacs = 0;
1854 for (i = 0; i < cfg->line_outs; i++) {
1855 if (spec->private_dac_nids[i])
1856 spec->multiout.num_dacs++;
1858 memmove(spec->private_dac_nids + i,
1859 spec->private_dac_nids + i + 1,
1860 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1861 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1865 spec->ext_channel_count = spec->min_channel_count =
1866 spec->multiout.num_dacs * 2;
1868 if (spec->multi_ios == 2) {
1869 for (i = 0; i < 2; i++)
1870 spec->private_dac_nids[spec->multiout.num_dacs++] =
1871 spec->multi_io[i].dac;
1872 } else if (spec->multi_ios) {
1873 spec->multi_ios = 0;
1874 badness += BAD_MULTI_IO;
1877 if (spec->indep_hp && !indep_hp_possible(codec))
1878 badness += BAD_NO_INDEP_HP;
1880 /* re-fill the shared DAC for speaker / headphone */
1881 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1882 refill_shared_dacs(codec, cfg->hp_outs,
1883 spec->multiout.hp_out_nid,
1885 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1886 refill_shared_dacs(codec, cfg->speaker_outs,
1887 spec->multiout.extra_out_nid,
1888 spec->speaker_paths);
1893 #define DEBUG_BADNESS
1895 #ifdef DEBUG_BADNESS
1896 #define debug_badness(fmt, ...) \
1897 codec_dbg(codec, fmt, ##__VA_ARGS__)
1899 #define debug_badness(fmt, ...) \
1900 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1903 #ifdef DEBUG_BADNESS
1904 static inline void print_nid_path_idx(struct hda_codec *codec,
1905 const char *pfx, int idx)
1907 struct nid_path *path;
1909 path = snd_hda_get_path_from_idx(codec, idx);
1911 print_nid_path(codec, pfx, path);
1914 static void debug_show_configs(struct hda_codec *codec,
1915 struct auto_pin_cfg *cfg)
1917 struct hda_gen_spec *spec = codec->spec;
1918 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1921 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1922 cfg->line_out_pins[0], cfg->line_out_pins[1],
1923 cfg->line_out_pins[2], cfg->line_out_pins[3],
1924 spec->multiout.dac_nids[0],
1925 spec->multiout.dac_nids[1],
1926 spec->multiout.dac_nids[2],
1927 spec->multiout.dac_nids[3],
1928 lo_type[cfg->line_out_type]);
1929 for (i = 0; i < cfg->line_outs; i++)
1930 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1931 if (spec->multi_ios > 0)
1932 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1934 spec->multi_io[0].pin, spec->multi_io[1].pin,
1935 spec->multi_io[0].dac, spec->multi_io[1].dac);
1936 for (i = 0; i < spec->multi_ios; i++)
1937 print_nid_path_idx(codec, " mio",
1938 spec->out_paths[cfg->line_outs + i]);
1940 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1941 cfg->hp_pins[0], cfg->hp_pins[1],
1942 cfg->hp_pins[2], cfg->hp_pins[3],
1943 spec->multiout.hp_out_nid[0],
1944 spec->multiout.hp_out_nid[1],
1945 spec->multiout.hp_out_nid[2],
1946 spec->multiout.hp_out_nid[3]);
1947 for (i = 0; i < cfg->hp_outs; i++)
1948 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1949 if (cfg->speaker_outs)
1950 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1951 cfg->speaker_pins[0], cfg->speaker_pins[1],
1952 cfg->speaker_pins[2], cfg->speaker_pins[3],
1953 spec->multiout.extra_out_nid[0],
1954 spec->multiout.extra_out_nid[1],
1955 spec->multiout.extra_out_nid[2],
1956 spec->multiout.extra_out_nid[3]);
1957 for (i = 0; i < cfg->speaker_outs; i++)
1958 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1959 for (i = 0; i < 3; i++)
1960 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1963 #define debug_show_configs(codec, cfg) /* NOP */
1966 /* find all available DACs of the codec */
1967 static void fill_all_dac_nids(struct hda_codec *codec)
1969 struct hda_gen_spec *spec = codec->spec;
1972 spec->num_all_dacs = 0;
1973 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1974 for_each_hda_codec_node(nid, codec) {
1975 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1977 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1978 codec_err(codec, "Too many DACs!\n");
1981 spec->all_dacs[spec->num_all_dacs++] = nid;
1985 static int parse_output_paths(struct hda_codec *codec)
1987 struct hda_gen_spec *spec = codec->spec;
1988 struct auto_pin_cfg *cfg = &spec->autocfg;
1989 struct auto_pin_cfg *best_cfg;
1991 int best_badness = INT_MAX;
1993 bool fill_hardwired = true, fill_mio_first = true;
1994 bool best_wired = true, best_mio = true;
1995 bool hp_spk_swapped = false;
1997 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
2003 badness = fill_and_eval_dacs(codec, fill_hardwired,
2009 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
2010 cfg->line_out_type, fill_hardwired, fill_mio_first,
2012 debug_show_configs(codec, cfg);
2013 if (badness < best_badness) {
2014 best_badness = badness;
2016 best_wired = fill_hardwired;
2017 best_mio = fill_mio_first;
2021 fill_mio_first = !fill_mio_first;
2022 if (!fill_mio_first)
2024 fill_hardwired = !fill_hardwired;
2025 if (!fill_hardwired)
2029 hp_spk_swapped = true;
2030 if (cfg->speaker_outs > 0 &&
2031 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2032 cfg->hp_outs = cfg->line_outs;
2033 memcpy(cfg->hp_pins, cfg->line_out_pins,
2034 sizeof(cfg->hp_pins));
2035 cfg->line_outs = cfg->speaker_outs;
2036 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2037 sizeof(cfg->speaker_pins));
2038 cfg->speaker_outs = 0;
2039 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2040 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2041 fill_hardwired = true;
2044 if (cfg->hp_outs > 0 &&
2045 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2046 cfg->speaker_outs = cfg->line_outs;
2047 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2048 sizeof(cfg->speaker_pins));
2049 cfg->line_outs = cfg->hp_outs;
2050 memcpy(cfg->line_out_pins, cfg->hp_pins,
2051 sizeof(cfg->hp_pins));
2053 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2054 cfg->line_out_type = AUTO_PIN_HP_OUT;
2055 fill_hardwired = true;
2062 debug_badness("==> restoring best_cfg\n");
2064 fill_and_eval_dacs(codec, best_wired, best_mio);
2066 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2067 cfg->line_out_type, best_wired, best_mio);
2068 debug_show_configs(codec, cfg);
2070 if (cfg->line_out_pins[0]) {
2071 struct nid_path *path;
2072 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
2074 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
2075 if (spec->vmaster_nid) {
2076 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2077 HDA_OUTPUT, spec->vmaster_tlv);
2078 if (spec->dac_min_mute)
2079 spec->vmaster_tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] |= TLV_DB_SCALE_MUTE;
2083 /* set initial pinctl targets */
2084 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2088 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2089 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2090 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2091 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2092 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2093 set_pin_targets(codec, cfg->speaker_outs,
2094 cfg->speaker_pins, val);
2097 /* clear indep_hp flag if not available */
2098 if (spec->indep_hp && !indep_hp_possible(codec))
2105 /* add playback controls from the parsed DAC table */
2106 static int create_multi_out_ctls(struct hda_codec *codec,
2107 const struct auto_pin_cfg *cfg)
2109 struct hda_gen_spec *spec = codec->spec;
2110 int i, err, noutputs;
2112 noutputs = cfg->line_outs;
2113 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2114 noutputs += spec->multi_ios;
2116 for (i = 0; i < noutputs; i++) {
2119 struct nid_path *path;
2121 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
2125 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
2126 if (!name || !strcmp(name, "CLFE")) {
2128 err = add_vol_ctl(codec, "Center", 0, 1, path);
2131 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2135 err = add_stereo_vol(codec, name, index, path);
2140 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2141 if (!name || !strcmp(name, "CLFE")) {
2142 err = add_sw_ctl(codec, "Center", 0, 1, path);
2145 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2149 err = add_stereo_sw(codec, name, index, path);
2157 static int create_extra_out(struct hda_codec *codec, int path_idx,
2158 const char *pfx, int cidx)
2160 struct nid_path *path;
2163 path = snd_hda_get_path_from_idx(codec, path_idx);
2166 err = add_stereo_vol(codec, pfx, cidx, path);
2169 err = add_stereo_sw(codec, pfx, cidx, path);
2175 /* add playback controls for speaker and HP outputs */
2176 static int create_extra_outs(struct hda_codec *codec, int num_pins,
2177 const int *paths, const char *pfx)
2181 for (i = 0; i < num_pins; i++) {
2183 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2186 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2187 name = "Bass Speaker";
2188 else if (num_pins >= 3) {
2189 snprintf(tmp, sizeof(tmp), "%s %s",
2190 pfx, channel_name[i]);
2196 err = create_extra_out(codec, paths[i], name, idx);
2203 static int create_hp_out_ctls(struct hda_codec *codec)
2205 struct hda_gen_spec *spec = codec->spec;
2206 return create_extra_outs(codec, spec->autocfg.hp_outs,
2211 static int create_speaker_out_ctls(struct hda_codec *codec)
2213 struct hda_gen_spec *spec = codec->spec;
2214 return create_extra_outs(codec, spec->autocfg.speaker_outs,
2215 spec->speaker_paths,
2220 * independent HP controls
2223 static void call_hp_automute(struct hda_codec *codec,
2224 struct hda_jack_callback *jack);
2225 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2226 struct snd_ctl_elem_info *uinfo)
2228 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2231 static int indep_hp_get(struct snd_kcontrol *kcontrol,
2232 struct snd_ctl_elem_value *ucontrol)
2234 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2235 struct hda_gen_spec *spec = codec->spec;
2236 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2240 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2241 int nomix_path_idx, int mix_path_idx,
2244 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2245 struct snd_ctl_elem_value *ucontrol)
2247 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2248 struct hda_gen_spec *spec = codec->spec;
2249 unsigned int select = ucontrol->value.enumerated.item[0];
2252 mutex_lock(&spec->pcm_mutex);
2253 if (spec->active_streams) {
2258 if (spec->indep_hp_enabled != select) {
2260 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2261 dacp = &spec->private_dac_nids[0];
2263 dacp = &spec->multiout.hp_out_nid[0];
2265 /* update HP aamix paths in case it conflicts with indep HP */
2266 if (spec->have_aamix_ctl) {
2267 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2268 update_aamix_paths(codec, spec->aamix_mode,
2270 spec->aamix_out_paths[0],
2271 spec->autocfg.line_out_type);
2273 update_aamix_paths(codec, spec->aamix_mode,
2275 spec->aamix_out_paths[1],
2279 spec->indep_hp_enabled = select;
2280 if (spec->indep_hp_enabled)
2283 *dacp = spec->alt_dac_nid;
2285 call_hp_automute(codec, NULL);
2289 mutex_unlock(&spec->pcm_mutex);
2293 static const struct snd_kcontrol_new indep_hp_ctl = {
2294 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2295 .name = "Independent HP",
2296 .info = indep_hp_info,
2297 .get = indep_hp_get,
2298 .put = indep_hp_put,
2302 static int create_indep_hp_ctls(struct hda_codec *codec)
2304 struct hda_gen_spec *spec = codec->spec;
2307 if (!spec->indep_hp)
2309 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2310 dac = spec->multiout.dac_nids[0];
2312 dac = spec->multiout.hp_out_nid[0];
2318 spec->indep_hp_enabled = false;
2319 spec->alt_dac_nid = dac;
2320 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2326 * channel mode enum control
2329 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2330 struct snd_ctl_elem_info *uinfo)
2332 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2333 struct hda_gen_spec *spec = codec->spec;
2336 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2338 uinfo->value.enumerated.items = spec->multi_ios + 1;
2339 if (uinfo->value.enumerated.item > spec->multi_ios)
2340 uinfo->value.enumerated.item = spec->multi_ios;
2341 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2342 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2346 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2347 struct snd_ctl_elem_value *ucontrol)
2349 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2350 struct hda_gen_spec *spec = codec->spec;
2351 ucontrol->value.enumerated.item[0] =
2352 (spec->ext_channel_count - spec->min_channel_count) / 2;
2356 static inline struct nid_path *
2357 get_multiio_path(struct hda_codec *codec, int idx)
2359 struct hda_gen_spec *spec = codec->spec;
2360 return snd_hda_get_path_from_idx(codec,
2361 spec->out_paths[spec->autocfg.line_outs + idx]);
2364 static void update_automute_all(struct hda_codec *codec);
2366 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2367 * used for output paths
2369 static bool aamix_default(struct hda_gen_spec *spec)
2371 return !spec->have_aamix_ctl || spec->aamix_mode;
2374 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2376 struct hda_gen_spec *spec = codec->spec;
2377 hda_nid_t nid = spec->multi_io[idx].pin;
2378 struct nid_path *path;
2380 path = get_multiio_path(codec, idx);
2384 if (path->active == output)
2388 set_pin_target(codec, nid, PIN_OUT, true);
2389 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2390 set_pin_eapd(codec, nid, true);
2392 set_pin_eapd(codec, nid, false);
2393 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2394 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2395 path_power_down_sync(codec, path);
2398 /* update jack retasking in case it modifies any of them */
2399 update_automute_all(codec);
2404 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2405 struct snd_ctl_elem_value *ucontrol)
2407 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2408 struct hda_gen_spec *spec = codec->spec;
2411 ch = ucontrol->value.enumerated.item[0];
2412 if (ch < 0 || ch > spec->multi_ios)
2414 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2416 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2417 for (i = 0; i < spec->multi_ios; i++)
2418 set_multi_io(codec, i, i < ch);
2419 spec->multiout.max_channels = max(spec->ext_channel_count,
2420 spec->const_channel_count);
2421 if (spec->need_dac_fix)
2422 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2426 static const struct snd_kcontrol_new channel_mode_enum = {
2427 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2428 .name = "Channel Mode",
2429 .info = ch_mode_info,
2434 static int create_multi_channel_mode(struct hda_codec *codec)
2436 struct hda_gen_spec *spec = codec->spec;
2438 if (spec->multi_ios > 0) {
2439 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2446 * aamix loopback enable/disable switch
2449 #define loopback_mixing_info indep_hp_info
2451 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2452 struct snd_ctl_elem_value *ucontrol)
2454 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2455 struct hda_gen_spec *spec = codec->spec;
2456 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2460 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2461 int nomix_path_idx, int mix_path_idx,
2464 struct hda_gen_spec *spec = codec->spec;
2465 struct nid_path *nomix_path, *mix_path;
2467 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2468 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2469 if (!nomix_path || !mix_path)
2472 /* if HP aamix path is driven from a different DAC and the
2473 * independent HP mode is ON, can't turn on aamix path
2475 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2476 mix_path->path[0] != spec->alt_dac_nid)
2480 snd_hda_activate_path(codec, nomix_path, false, true);
2481 snd_hda_activate_path(codec, mix_path, true, true);
2482 path_power_down_sync(codec, nomix_path);
2484 snd_hda_activate_path(codec, mix_path, false, false);
2485 snd_hda_activate_path(codec, nomix_path, true, false);
2486 path_power_down_sync(codec, mix_path);
2490 /* re-initialize the output paths; only called from loopback_mixing_put() */
2491 static void update_output_paths(struct hda_codec *codec, int num_outs,
2494 struct hda_gen_spec *spec = codec->spec;
2495 struct nid_path *path;
2498 for (i = 0; i < num_outs; i++) {
2499 path = snd_hda_get_path_from_idx(codec, paths[i]);
2501 snd_hda_activate_path(codec, path, path->active,
2506 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2507 struct snd_ctl_elem_value *ucontrol)
2509 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2510 struct hda_gen_spec *spec = codec->spec;
2511 const struct auto_pin_cfg *cfg = &spec->autocfg;
2512 unsigned int val = ucontrol->value.enumerated.item[0];
2514 if (val == spec->aamix_mode)
2516 spec->aamix_mode = val;
2517 if (has_aamix_out_paths(spec)) {
2518 update_aamix_paths(codec, val, spec->out_paths[0],
2519 spec->aamix_out_paths[0],
2520 cfg->line_out_type);
2521 update_aamix_paths(codec, val, spec->hp_paths[0],
2522 spec->aamix_out_paths[1],
2524 update_aamix_paths(codec, val, spec->speaker_paths[0],
2525 spec->aamix_out_paths[2],
2526 AUTO_PIN_SPEAKER_OUT);
2528 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2529 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2530 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2531 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2532 update_output_paths(codec, cfg->speaker_outs,
2533 spec->speaker_paths);
2538 static const struct snd_kcontrol_new loopback_mixing_enum = {
2539 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2540 .name = "Loopback Mixing",
2541 .info = loopback_mixing_info,
2542 .get = loopback_mixing_get,
2543 .put = loopback_mixing_put,
2546 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2548 struct hda_gen_spec *spec = codec->spec;
2550 if (!spec->mixer_nid)
2552 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2554 spec->have_aamix_ctl = 1;
2559 * shared headphone/mic handling
2562 static void call_update_outputs(struct hda_codec *codec);
2564 /* for shared I/O, change the pin-control accordingly */
2565 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2567 struct hda_gen_spec *spec = codec->spec;
2572 pin = spec->hp_mic_pin;
2573 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2576 val = snd_hda_codec_get_pin_target(codec, pin);
2586 val = snd_hda_get_default_vref(codec, pin);
2587 /* if the HP pin doesn't support VREF and the codec driver gives an
2588 * alternative pin, set up the VREF on that pin instead
2590 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2591 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2592 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2593 if (vref_val != AC_PINCTL_VREF_HIZ)
2594 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2595 PIN_IN | (as_mic ? vref_val : 0));
2598 if (!spec->hp_mic_jack_modes) {
2603 set_pin_target(codec, pin, val, true);
2604 call_hp_automute(codec, NULL);
2608 /* create a shared input with the headphone out */
2609 static int create_hp_mic(struct hda_codec *codec)
2611 struct hda_gen_spec *spec = codec->spec;
2612 struct auto_pin_cfg *cfg = &spec->autocfg;
2613 unsigned int defcfg;
2616 if (!spec->hp_mic) {
2617 if (spec->suppress_hp_mic_detect)
2619 /* automatic detection: only if no input or a single internal
2620 * input pin is found, try to detect the shared hp/mic
2622 if (cfg->num_inputs > 1)
2624 else if (cfg->num_inputs == 1) {
2625 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2626 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2631 spec->hp_mic = 0; /* clear once */
2632 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2636 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2637 nid = cfg->line_out_pins[0];
2638 else if (cfg->hp_outs > 0)
2639 nid = cfg->hp_pins[0];
2643 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2644 return 0; /* no input */
2646 cfg->inputs[cfg->num_inputs].pin = nid;
2647 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2648 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2651 spec->hp_mic_pin = nid;
2652 /* we can't handle auto-mic together with HP-mic */
2653 spec->suppress_auto_mic = 1;
2654 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
2662 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2664 static const char * const out_jack_texts[] = {
2665 "Line Out", "Headphone Out",
2668 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2669 struct snd_ctl_elem_info *uinfo)
2671 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2674 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2675 struct snd_ctl_elem_value *ucontrol)
2677 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2678 hda_nid_t nid = kcontrol->private_value;
2679 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2680 ucontrol->value.enumerated.item[0] = 1;
2682 ucontrol->value.enumerated.item[0] = 0;
2686 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2687 struct snd_ctl_elem_value *ucontrol)
2689 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2690 hda_nid_t nid = kcontrol->private_value;
2693 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2694 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2696 snd_hda_set_pin_ctl_cache(codec, nid, val);
2700 static const struct snd_kcontrol_new out_jack_mode_enum = {
2701 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2702 .info = out_jack_mode_info,
2703 .get = out_jack_mode_get,
2704 .put = out_jack_mode_put,
2707 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2709 struct hda_gen_spec *spec = codec->spec;
2710 const struct snd_kcontrol_new *kctl;
2713 snd_array_for_each(&spec->kctls, i, kctl) {
2714 if (!strcmp(kctl->name, name) && kctl->index == idx)
2720 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2721 char *name, size_t name_len)
2723 struct hda_gen_spec *spec = codec->spec;
2726 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2727 strlcat(name, " Jack Mode", name_len);
2729 for (; find_kctl_name(codec, name, idx); idx++)
2733 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2735 struct hda_gen_spec *spec = codec->spec;
2736 if (spec->add_jack_modes) {
2737 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2738 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2744 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2747 struct hda_gen_spec *spec = codec->spec;
2750 for (i = 0; i < num_pins; i++) {
2751 hda_nid_t pin = pins[i];
2752 if (pin == spec->hp_mic_pin)
2754 if (get_out_jack_num_items(codec, pin) > 1) {
2755 struct snd_kcontrol_new *knew;
2756 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2757 get_jack_mode_name(codec, pin, name, sizeof(name));
2758 knew = snd_hda_gen_add_kctl(spec, name,
2759 &out_jack_mode_enum);
2762 knew->private_value = pin;
2773 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2776 static const char * const vref_texts[NUM_VREFS] = {
2777 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2778 "", "Mic 80pc Bias", "Mic 100pc Bias"
2781 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2783 unsigned int pincap;
2785 pincap = snd_hda_query_pin_caps(codec, pin);
2786 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2787 /* filter out unusual vrefs */
2788 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2792 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2793 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2795 unsigned int i, n = 0;
2797 for (i = 0; i < NUM_VREFS; i++) {
2798 if (vref_caps & (1 << i)) {
2807 /* convert back from the vref ctl index to the enum item index */
2808 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2810 unsigned int i, n = 0;
2812 for (i = 0; i < NUM_VREFS; i++) {
2815 if (vref_caps & (1 << i))
2821 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2822 struct snd_ctl_elem_info *uinfo)
2824 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2825 hda_nid_t nid = kcontrol->private_value;
2826 unsigned int vref_caps = get_vref_caps(codec, nid);
2828 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2830 /* set the right text */
2831 strcpy(uinfo->value.enumerated.name,
2832 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2836 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2837 struct snd_ctl_elem_value *ucontrol)
2839 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2840 hda_nid_t nid = kcontrol->private_value;
2841 unsigned int vref_caps = get_vref_caps(codec, nid);
2844 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2845 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2849 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2850 struct snd_ctl_elem_value *ucontrol)
2852 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2853 hda_nid_t nid = kcontrol->private_value;
2854 unsigned int vref_caps = get_vref_caps(codec, nid);
2855 unsigned int val, idx;
2857 val = snd_hda_codec_get_pin_target(codec, nid);
2858 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2859 if (idx == ucontrol->value.enumerated.item[0])
2862 val &= ~AC_PINCTL_VREFEN;
2863 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2864 snd_hda_set_pin_ctl_cache(codec, nid, val);
2868 static const struct snd_kcontrol_new in_jack_mode_enum = {
2869 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2870 .info = in_jack_mode_info,
2871 .get = in_jack_mode_get,
2872 .put = in_jack_mode_put,
2875 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2877 struct hda_gen_spec *spec = codec->spec;
2879 if (spec->add_jack_modes)
2880 nitems = hweight32(get_vref_caps(codec, pin));
2881 return nitems ? nitems : 1;
2884 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2886 struct hda_gen_spec *spec = codec->spec;
2887 struct snd_kcontrol_new *knew;
2888 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2889 unsigned int defcfg;
2891 if (pin == spec->hp_mic_pin)
2892 return 0; /* already done in create_out_jack_mode() */
2894 /* no jack mode for fixed pins */
2895 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2896 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2899 /* no multiple vref caps? */
2900 if (get_in_jack_num_items(codec, pin) <= 1)
2903 get_jack_mode_name(codec, pin, name, sizeof(name));
2904 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2907 knew->private_value = pin;
2912 * HP/mic shared jack mode
2914 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2915 struct snd_ctl_elem_info *uinfo)
2917 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2918 hda_nid_t nid = kcontrol->private_value;
2919 int out_jacks = get_out_jack_num_items(codec, nid);
2920 int in_jacks = get_in_jack_num_items(codec, nid);
2921 const char *text = NULL;
2924 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2926 uinfo->value.enumerated.items = out_jacks + in_jacks;
2927 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2928 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2929 idx = uinfo->value.enumerated.item;
2930 if (idx < out_jacks) {
2932 text = out_jack_texts[idx];
2934 text = "Headphone Out";
2938 unsigned int vref_caps = get_vref_caps(codec, nid);
2939 text = vref_texts[get_vref_idx(vref_caps, idx)];
2944 strcpy(uinfo->value.enumerated.name, text);
2948 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2950 int out_jacks = get_out_jack_num_items(codec, nid);
2951 int in_jacks = get_in_jack_num_items(codec, nid);
2952 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2955 if (val & PIN_OUT) {
2956 if (out_jacks > 1 && val == PIN_HP)
2958 } else if (val & PIN_IN) {
2961 unsigned int vref_caps = get_vref_caps(codec, nid);
2962 val &= AC_PINCTL_VREFEN;
2963 idx += cvt_from_vref_idx(vref_caps, val);
2969 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2970 struct snd_ctl_elem_value *ucontrol)
2972 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2973 hda_nid_t nid = kcontrol->private_value;
2974 ucontrol->value.enumerated.item[0] =
2975 get_cur_hp_mic_jack_mode(codec, nid);
2979 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2980 struct snd_ctl_elem_value *ucontrol)
2982 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2983 hda_nid_t nid = kcontrol->private_value;
2984 int out_jacks = get_out_jack_num_items(codec, nid);
2985 int in_jacks = get_in_jack_num_items(codec, nid);
2986 unsigned int val, oldval, idx;
2988 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2989 idx = ucontrol->value.enumerated.item[0];
2993 if (idx < out_jacks) {
2995 val = idx ? PIN_HP : PIN_OUT;
3001 unsigned int vref_caps = get_vref_caps(codec, nid);
3002 val = snd_hda_codec_get_pin_target(codec, nid);
3003 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
3004 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
3006 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
3008 snd_hda_set_pin_ctl_cache(codec, nid, val);
3009 call_hp_automute(codec, NULL);
3014 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
3015 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3016 .info = hp_mic_jack_mode_info,
3017 .get = hp_mic_jack_mode_get,
3018 .put = hp_mic_jack_mode_put,
3021 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
3023 struct hda_gen_spec *spec = codec->spec;
3024 struct snd_kcontrol_new *knew;
3026 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
3027 &hp_mic_jack_mode_enum);
3030 knew->private_value = pin;
3031 spec->hp_mic_jack_modes = 1;
3039 /* add the powersave loopback-list entry */
3040 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
3042 struct hda_amp_list *list;
3044 list = snd_array_new(&spec->loopback_list);
3048 list->dir = HDA_INPUT;
3050 spec->loopback.amplist = spec->loopback_list.list;
3054 /* return true if either a volume or a mute amp is found for the given
3055 * aamix path; the amp has to be either in the mixer node or its direct leaf
3057 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3058 hda_nid_t pin, unsigned int *mix_val,
3059 unsigned int *mute_val)
3062 const hda_nid_t *list;
3065 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3069 *mix_val = *mute_val = 0;
3070 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3071 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3072 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3073 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3074 if (*mix_val && *mute_val)
3077 /* check leaf node */
3078 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3079 if (num_conns < idx)
3082 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3083 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
3084 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3085 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3086 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
3087 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3089 return *mix_val || *mute_val;
3092 /* create input playback/capture controls for the given pin */
3093 static int new_analog_input(struct hda_codec *codec, int input_idx,
3094 hda_nid_t pin, const char *ctlname, int ctlidx,
3097 struct hda_gen_spec *spec = codec->spec;
3098 struct nid_path *path;
3099 unsigned int mix_val, mute_val;
3102 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3105 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
3108 print_nid_path(codec, "loopback", path);
3109 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
3111 idx = path->idx[path->depth - 1];
3113 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
3116 path->ctls[NID_PATH_VOL_CTL] = mix_val;
3120 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
3123 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
3126 path->active = true;
3127 path->stream_enabled = true; /* no DAC/ADC involved */
3128 err = add_loopback_list(spec, mix_nid, idx);
3132 if (spec->mixer_nid != spec->mixer_merge_nid &&
3133 !spec->loopback_merge_path) {
3134 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3135 spec->mixer_merge_nid, 0);
3137 print_nid_path(codec, "loopback-merge", path);
3138 path->active = true;
3139 path->pin_fixed = true; /* static route */
3140 path->stream_enabled = true; /* no DAC/ADC involved */
3141 spec->loopback_merge_path =
3142 snd_hda_get_path_idx(codec, path);
3149 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3151 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3152 return (pincap & AC_PINCAP_IN) != 0;
3155 /* Parse the codec tree and retrieve ADCs */
3156 static int fill_adc_nids(struct hda_codec *codec)
3158 struct hda_gen_spec *spec = codec->spec;
3160 hda_nid_t *adc_nids = spec->adc_nids;
3161 int max_nums = ARRAY_SIZE(spec->adc_nids);
3164 for_each_hda_codec_node(nid, codec) {
3165 unsigned int caps = get_wcaps(codec, nid);
3166 int type = get_wcaps_type(caps);
3168 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3170 adc_nids[nums] = nid;
3171 if (++nums >= max_nums)
3174 spec->num_adc_nids = nums;
3176 /* copy the detected ADCs to all_adcs[] */
3177 spec->num_all_adcs = nums;
3178 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3183 /* filter out invalid adc_nids that don't give all active input pins;
3184 * if needed, check whether dynamic ADC-switching is available
3186 static int check_dyn_adc_switch(struct hda_codec *codec)
3188 struct hda_gen_spec *spec = codec->spec;
3189 struct hda_input_mux *imux = &spec->input_mux;
3190 unsigned int ok_bits;
3195 for (n = 0; n < spec->num_adc_nids; n++) {
3196 for (i = 0; i < imux->num_items; i++) {
3197 if (!spec->input_paths[i][n])
3200 if (i >= imux->num_items) {
3201 ok_bits |= (1 << n);
3207 /* check whether ADC-switch is possible */
3208 for (i = 0; i < imux->num_items; i++) {
3209 for (n = 0; n < spec->num_adc_nids; n++) {
3210 if (spec->input_paths[i][n]) {
3211 spec->dyn_adc_idx[i] = n;
3217 codec_dbg(codec, "enabling ADC switching\n");
3218 spec->dyn_adc_switch = 1;
3219 } else if (nums != spec->num_adc_nids) {
3220 /* shrink the invalid adcs and input paths */
3222 for (n = 0; n < spec->num_adc_nids; n++) {
3223 if (!(ok_bits & (1 << n)))
3226 spec->adc_nids[nums] = spec->adc_nids[n];
3227 for (i = 0; i < imux->num_items; i++) {
3228 invalidate_nid_path(codec,
3229 spec->input_paths[i][nums]);
3230 spec->input_paths[i][nums] =
3231 spec->input_paths[i][n];
3232 spec->input_paths[i][n] = 0;
3237 spec->num_adc_nids = nums;
3240 if (imux->num_items == 1 ||
3241 (imux->num_items == 2 && spec->hp_mic)) {
3242 codec_dbg(codec, "reducing to a single ADC\n");
3243 spec->num_adc_nids = 1; /* reduce to a single ADC */
3246 /* single index for individual volumes ctls */
3247 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3248 spec->num_adc_nids = 1;
3253 /* parse capture source paths from the given pin and create imux items */
3254 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3255 int cfg_idx, int num_adcs,
3256 const char *label, int anchor)
3258 struct hda_gen_spec *spec = codec->spec;
3259 struct hda_input_mux *imux = &spec->input_mux;
3260 int imux_idx = imux->num_items;
3261 bool imux_added = false;
3264 for (c = 0; c < num_adcs; c++) {
3265 struct nid_path *path;
3266 hda_nid_t adc = spec->adc_nids[c];
3268 if (!is_reachable_path(codec, pin, adc))
3270 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3273 print_nid_path(codec, "input", path);
3274 spec->input_paths[imux_idx][c] =
3275 snd_hda_get_path_idx(codec, path);
3278 if (spec->hp_mic_pin == pin)
3279 spec->hp_mic_mux_idx = imux->num_items;
3280 spec->imux_pins[imux->num_items] = pin;
3281 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3283 if (spec->dyn_adc_switch)
3284 spec->dyn_adc_idx[imux_idx] = c;
3292 * create playback/capture controls for input pins
3295 /* fill the label for each input at first */
3296 static int fill_input_pin_labels(struct hda_codec *codec)
3298 struct hda_gen_spec *spec = codec->spec;
3299 const struct auto_pin_cfg *cfg = &spec->autocfg;
3302 for (i = 0; i < cfg->num_inputs; i++) {
3303 hda_nid_t pin = cfg->inputs[i].pin;
3307 if (!is_input_pin(codec, pin))
3310 label = hda_get_autocfg_input_label(codec, cfg, i);
3312 for (j = i - 1; j >= 0; j--) {
3313 if (spec->input_labels[j] &&
3314 !strcmp(spec->input_labels[j], label)) {
3315 idx = spec->input_label_idxs[j] + 1;
3320 spec->input_labels[i] = label;
3321 spec->input_label_idxs[i] = idx;
3327 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3329 static int create_input_ctls(struct hda_codec *codec)
3331 struct hda_gen_spec *spec = codec->spec;
3332 const struct auto_pin_cfg *cfg = &spec->autocfg;
3333 hda_nid_t mixer = spec->mixer_nid;
3338 num_adcs = fill_adc_nids(codec);
3342 err = fill_input_pin_labels(codec);
3346 for (i = 0; i < cfg->num_inputs; i++) {
3349 pin = cfg->inputs[i].pin;
3350 if (!is_input_pin(codec, pin))
3354 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3355 val |= snd_hda_get_default_vref(codec, pin);
3356 if (pin != spec->hp_mic_pin &&
3357 !snd_hda_codec_get_pin_target(codec, pin))
3358 set_pin_target(codec, pin, val, false);
3361 if (is_reachable_path(codec, pin, mixer)) {
3362 err = new_analog_input(codec, i, pin,
3363 spec->input_labels[i],
3364 spec->input_label_idxs[i],
3371 err = parse_capture_source(codec, pin, i, num_adcs,
3372 spec->input_labels[i], -mixer);
3376 if (spec->add_jack_modes) {
3377 err = create_in_jack_mode(codec, pin);
3383 /* add stereo mix when explicitly enabled via hint */
3384 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3385 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3390 spec->suppress_auto_mic = 1;
3401 /* get the input path specified by the given adc and imux indices */
3402 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3404 struct hda_gen_spec *spec = codec->spec;
3405 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3409 if (spec->dyn_adc_switch)
3410 adc_idx = spec->dyn_adc_idx[imux_idx];
3411 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3415 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3418 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3421 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3422 struct snd_ctl_elem_info *uinfo)
3424 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3425 struct hda_gen_spec *spec = codec->spec;
3426 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3429 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3430 struct snd_ctl_elem_value *ucontrol)
3432 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3433 struct hda_gen_spec *spec = codec->spec;
3434 /* the ctls are created at once with multiple counts */
3435 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3437 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3441 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3442 struct snd_ctl_elem_value *ucontrol)
3444 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3445 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3446 return mux_select(codec, adc_idx,
3447 ucontrol->value.enumerated.item[0]);
3450 static const struct snd_kcontrol_new cap_src_temp = {
3451 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3452 .name = "Input Source",
3453 .info = mux_enum_info,
3454 .get = mux_enum_get,
3455 .put = mux_enum_put,
3459 * capture volume and capture switch ctls
3462 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3463 struct snd_ctl_elem_value *ucontrol);
3465 /* call the given amp update function for all amps in the imux list at once */
3466 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3467 struct snd_ctl_elem_value *ucontrol,
3468 put_call_t func, int type)
3470 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3471 struct hda_gen_spec *spec = codec->spec;
3472 const struct hda_input_mux *imux;
3473 struct nid_path *path;
3474 int i, adc_idx, ret, err = 0;
3476 imux = &spec->input_mux;
3477 adc_idx = kcontrol->id.index;
3478 mutex_lock(&codec->control_mutex);
3479 for (i = 0; i < imux->num_items; i++) {
3480 path = get_input_path(codec, adc_idx, i);
3481 if (!path || !path->ctls[type])
3483 kcontrol->private_value = path->ctls[type];
3484 ret = func(kcontrol, ucontrol);
3492 mutex_unlock(&codec->control_mutex);
3493 if (err >= 0 && spec->cap_sync_hook)
3494 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3498 /* capture volume ctl callbacks */
3499 #define cap_vol_info snd_hda_mixer_amp_volume_info
3500 #define cap_vol_get snd_hda_mixer_amp_volume_get
3501 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3503 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3504 struct snd_ctl_elem_value *ucontrol)
3506 return cap_put_caller(kcontrol, ucontrol,
3507 snd_hda_mixer_amp_volume_put,
3511 static const struct snd_kcontrol_new cap_vol_temp = {
3512 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3513 .name = "Capture Volume",
3514 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3515 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3516 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3517 .info = cap_vol_info,
3520 .tlv = { .c = cap_vol_tlv },
3523 /* capture switch ctl callbacks */
3524 #define cap_sw_info snd_ctl_boolean_stereo_info
3525 #define cap_sw_get snd_hda_mixer_amp_switch_get
3527 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3528 struct snd_ctl_elem_value *ucontrol)
3530 return cap_put_caller(kcontrol, ucontrol,
3531 snd_hda_mixer_amp_switch_put,
3535 static const struct snd_kcontrol_new cap_sw_temp = {
3536 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3537 .name = "Capture Switch",
3538 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3539 .info = cap_sw_info,
3544 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3549 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3550 for (depth = 0; depth < 3; depth++) {
3551 if (depth >= path->depth)
3553 i = path->depth - depth - 1;
3554 nid = path->path[i];
3555 if (!path->ctls[NID_PATH_VOL_CTL]) {
3556 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3557 path->ctls[NID_PATH_VOL_CTL] =
3558 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3559 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3560 int idx = path->idx[i];
3561 if (!depth && codec->single_adc_amp)
3563 path->ctls[NID_PATH_VOL_CTL] =
3564 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3567 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3568 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3569 path->ctls[NID_PATH_MUTE_CTL] =
3570 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3571 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3572 int idx = path->idx[i];
3573 if (!depth && codec->single_adc_amp)
3575 path->ctls[NID_PATH_MUTE_CTL] =
3576 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3583 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3585 struct hda_gen_spec *spec = codec->spec;
3586 struct auto_pin_cfg *cfg = &spec->autocfg;
3590 if (!spec->inv_dmic_split)
3592 for (i = 0; i < cfg->num_inputs; i++) {
3593 if (cfg->inputs[i].pin != nid)
3595 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3597 val = snd_hda_codec_get_pincfg(codec, nid);
3598 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3603 /* capture switch put callback for a single control with hook call */
3604 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3605 struct snd_ctl_elem_value *ucontrol)
3607 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3608 struct hda_gen_spec *spec = codec->spec;
3611 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3615 if (spec->cap_sync_hook)
3616 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3621 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3622 int idx, bool is_switch, unsigned int ctl,
3625 struct hda_gen_spec *spec = codec->spec;
3626 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3627 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3628 const char *sfx = is_switch ? "Switch" : "Volume";
3629 unsigned int chs = inv_dmic ? 1 : 3;
3630 struct snd_kcontrol_new *knew;
3636 snprintf(tmpname, sizeof(tmpname),
3637 "%s Capture %s", label, sfx);
3639 snprintf(tmpname, sizeof(tmpname),
3641 knew = add_control(spec, type, tmpname, idx,
3642 amp_val_replace_channels(ctl, chs));
3646 knew->put = cap_single_sw_put;
3647 if (spec->mic_mute_led)
3648 knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
3653 /* Make independent right kcontrol */
3655 snprintf(tmpname, sizeof(tmpname),
3656 "Inverted %s Capture %s", label, sfx);
3658 snprintf(tmpname, sizeof(tmpname),
3659 "Inverted Capture %s", sfx);
3660 knew = add_control(spec, type, tmpname, idx,
3661 amp_val_replace_channels(ctl, 2));
3665 knew->put = cap_single_sw_put;
3666 if (spec->mic_mute_led)
3667 knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
3672 /* create single (and simple) capture volume and switch controls */
3673 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3674 unsigned int vol_ctl, unsigned int sw_ctl,
3678 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3681 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3687 /* create bound capture volume and switch controls */
3688 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3689 unsigned int vol_ctl, unsigned int sw_ctl)
3691 struct hda_gen_spec *spec = codec->spec;
3692 struct snd_kcontrol_new *knew;
3695 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3699 knew->private_value = vol_ctl;
3700 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3703 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3707 knew->private_value = sw_ctl;
3708 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3709 if (spec->mic_mute_led)
3710 knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
3715 /* return the vol ctl when used first in the imux list */
3716 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3718 struct nid_path *path;
3722 path = get_input_path(codec, 0, idx);
3725 ctl = path->ctls[type];
3728 for (i = 0; i < idx - 1; i++) {
3729 path = get_input_path(codec, 0, i);
3730 if (path && path->ctls[type] == ctl)
3736 /* create individual capture volume and switch controls per input */
3737 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3739 struct hda_gen_spec *spec = codec->spec;
3740 struct hda_input_mux *imux = &spec->input_mux;
3743 for (i = 0; i < imux->num_items; i++) {
3747 idx = imux->items[i].index;
3748 if (idx >= spec->autocfg.num_inputs)
3750 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3752 for (type = 0; type < 2; type++) {
3753 err = add_single_cap_ctl(codec,
3754 spec->input_labels[idx],
3755 spec->input_label_idxs[idx],
3757 get_first_cap_ctl(codec, i, type),
3766 static int create_capture_mixers(struct hda_codec *codec)
3768 struct hda_gen_spec *spec = codec->spec;
3769 struct hda_input_mux *imux = &spec->input_mux;
3770 int i, n, nums, err;
3772 if (spec->dyn_adc_switch)
3775 nums = spec->num_adc_nids;
3777 if (!spec->auto_mic && imux->num_items > 1) {
3778 struct snd_kcontrol_new *knew;
3780 name = nums > 1 ? "Input Source" : "Capture Source";
3781 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3787 for (n = 0; n < nums; n++) {
3789 bool multi_cap_vol = spec->multi_cap_vol;
3790 bool inv_dmic = false;
3794 for (i = 0; i < imux->num_items; i++) {
3795 struct nid_path *path;
3796 path = get_input_path(codec, n, i);
3799 parse_capvol_in_path(codec, path);
3801 vol = path->ctls[NID_PATH_VOL_CTL];
3802 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3804 if (!same_amp_caps(codec, vol,
3805 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3806 multi_cap_vol = true;
3809 sw = path->ctls[NID_PATH_MUTE_CTL];
3810 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3812 if (!same_amp_caps(codec, sw,
3813 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3814 multi_cap_vol = true;
3816 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3821 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3823 else if (!multi_cap_vol && !inv_dmic)
3824 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3826 err = create_multi_cap_vol_ctl(codec);
3835 * add mic boosts if needed
3838 /* check whether the given amp is feasible as a boost volume */
3839 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3844 if (!nid_has_volume(codec, nid, dir) ||
3845 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3846 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3849 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3850 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3856 /* look for a boost amp in a widget close to the pin */
3857 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3858 struct nid_path *path)
3860 unsigned int val = 0;
3864 for (depth = 0; depth < 3; depth++) {
3865 if (depth >= path->depth - 1)
3867 nid = path->path[depth];
3868 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3869 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3871 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3872 path->idx[depth])) {
3873 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3882 static int parse_mic_boost(struct hda_codec *codec)
3884 struct hda_gen_spec *spec = codec->spec;
3885 struct auto_pin_cfg *cfg = &spec->autocfg;
3886 struct hda_input_mux *imux = &spec->input_mux;
3889 if (!spec->num_adc_nids)
3892 for (i = 0; i < imux->num_items; i++) {
3893 struct nid_path *path;
3896 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3898 idx = imux->items[i].index;
3899 if (idx >= imux->num_items)
3902 /* check only line-in and mic pins */
3903 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3906 path = get_input_path(codec, 0, i);
3910 val = look_for_boost_amp(codec, path);
3914 /* create a boost control */
3915 snprintf(boost_label, sizeof(boost_label),
3916 "%s Boost Volume", spec->input_labels[idx]);
3917 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3918 spec->input_label_idxs[idx], val))
3921 path->ctls[NID_PATH_BOOST_CTL] = val;
3926 #ifdef CONFIG_SND_HDA_GENERIC_LEDS
3928 * vmaster mute LED hook helpers
3931 static int create_mute_led_cdev(struct hda_codec *codec,
3932 int (*callback)(struct led_classdev *,
3933 enum led_brightness),
3936 struct hda_gen_spec *spec = codec->spec;
3937 struct led_classdev *cdev;
3938 int idx = micmute ? LED_AUDIO_MICMUTE : LED_AUDIO_MUTE;
3941 cdev = devm_kzalloc(&codec->core.dev, sizeof(*cdev), GFP_KERNEL);
3945 cdev->name = micmute ? "hda::micmute" : "hda::mute";
3946 cdev->max_brightness = 1;
3947 cdev->default_trigger = micmute ? "audio-micmute" : "audio-mute";
3948 cdev->brightness_set_blocking = callback;
3949 cdev->brightness = ledtrig_audio_get(idx);
3950 cdev->flags = LED_CORE_SUSPENDRESUME;
3952 err = led_classdev_register(&codec->core.dev, cdev);
3955 spec->led_cdevs[idx] = cdev;
3960 * snd_hda_gen_add_mute_led_cdev - Create a LED classdev and enable as vmaster mute LED
3961 * @codec: the HDA codec
3962 * @callback: the callback for LED classdev brightness_set_blocking
3964 int snd_hda_gen_add_mute_led_cdev(struct hda_codec *codec,
3965 int (*callback)(struct led_classdev *,
3966 enum led_brightness))
3968 struct hda_gen_spec *spec = codec->spec;
3972 err = create_mute_led_cdev(codec, callback, false);
3974 codec_warn(codec, "failed to create a mute LED cdev\n");
3979 if (spec->vmaster_mute.hook)
3980 codec_err(codec, "vmaster hook already present before cdev!\n");
3982 spec->vmaster_mute_led = 1;
3985 EXPORT_SYMBOL_GPL(snd_hda_gen_add_mute_led_cdev);
3988 * snd_hda_gen_add_micmute_led_cdev - Create a LED classdev and enable as mic-mute LED
3989 * @codec: the HDA codec
3990 * @callback: the callback for LED classdev brightness_set_blocking
3992 * Called from the codec drivers for offering the mic mute LED controls.
3993 * This creates a LED classdev and sets up the cap_sync_hook that is called at
3994 * each time when the capture mixer switch changes.
3996 * When NULL is passed to @callback, no classdev is created but only the
3997 * LED-trigger is set up.
3999 * Returns 0 or a negative error.
4001 int snd_hda_gen_add_micmute_led_cdev(struct hda_codec *codec,
4002 int (*callback)(struct led_classdev *,
4003 enum led_brightness))
4005 struct hda_gen_spec *spec = codec->spec;
4009 err = create_mute_led_cdev(codec, callback, true);
4011 codec_warn(codec, "failed to create a mic-mute LED cdev\n");
4016 spec->mic_mute_led = 1;
4019 EXPORT_SYMBOL_GPL(snd_hda_gen_add_micmute_led_cdev);
4020 #endif /* CONFIG_SND_HDA_GENERIC_LEDS */
4023 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
4025 static void parse_digital(struct hda_codec *codec)
4027 struct hda_gen_spec *spec = codec->spec;
4028 struct nid_path *path;
4030 hda_nid_t dig_nid, pin;
4032 /* support multiple SPDIFs; the secondary is set up as a follower */
4034 for (i = 0; i < spec->autocfg.dig_outs; i++) {
4035 pin = spec->autocfg.dig_out_pins[i];
4036 dig_nid = look_for_dac(codec, pin, true);
4039 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
4042 print_nid_path(codec, "digout", path);
4043 path->active = true;
4044 path->pin_fixed = true; /* no jack detection */
4045 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
4046 set_pin_target(codec, pin, PIN_OUT, false);
4048 spec->multiout.dig_out_nid = dig_nid;
4049 spec->dig_out_type = spec->autocfg.dig_out_type[0];
4051 spec->multiout.follower_dig_outs = spec->follower_dig_outs;
4052 if (nums >= ARRAY_SIZE(spec->follower_dig_outs) - 1)
4054 spec->follower_dig_outs[nums - 1] = dig_nid;
4059 if (spec->autocfg.dig_in_pin) {
4060 pin = spec->autocfg.dig_in_pin;
4061 for_each_hda_codec_node(dig_nid, codec) {
4062 unsigned int wcaps = get_wcaps(codec, dig_nid);
4063 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
4065 if (!(wcaps & AC_WCAP_DIGITAL))
4067 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
4069 print_nid_path(codec, "digin", path);
4070 path->active = true;
4071 path->pin_fixed = true; /* no jack */
4072 spec->dig_in_nid = dig_nid;
4073 spec->digin_path = snd_hda_get_path_idx(codec, path);
4074 set_pin_target(codec, pin, PIN_IN, false);
4083 * input MUX handling
4086 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
4088 /* select the given imux item; either unmute exclusively or select the route */
4089 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
4092 struct hda_gen_spec *spec = codec->spec;
4093 const struct hda_input_mux *imux;
4094 struct nid_path *old_path, *path;
4096 imux = &spec->input_mux;
4097 if (!imux->num_items)
4100 if (idx >= imux->num_items)
4101 idx = imux->num_items - 1;
4102 if (spec->cur_mux[adc_idx] == idx)
4105 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
4108 if (old_path->active)
4109 snd_hda_activate_path(codec, old_path, false, false);
4111 spec->cur_mux[adc_idx] = idx;
4114 update_hp_mic(codec, adc_idx, false);
4116 if (spec->dyn_adc_switch)
4117 dyn_adc_pcm_resetup(codec, idx);
4119 path = get_input_path(codec, adc_idx, idx);
4124 snd_hda_activate_path(codec, path, true, false);
4125 if (spec->cap_sync_hook)
4126 spec->cap_sync_hook(codec, NULL, NULL);
4127 path_power_down_sync(codec, old_path);
4131 /* power up/down widgets in the all paths that match with the given NID
4132 * as terminals (either start- or endpoint)
4134 * returns the last changed NID, or zero if unchanged.
4136 static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
4137 int pin_state, int stream_state)
4139 struct hda_gen_spec *spec = codec->spec;
4140 hda_nid_t last, changed = 0;
4141 struct nid_path *path;
4144 snd_array_for_each(&spec->paths, n, path) {
4147 if (path->path[0] == nid ||
4148 path->path[path->depth - 1] == nid) {
4149 bool pin_old = path->pin_enabled;
4150 bool stream_old = path->stream_enabled;
4153 path->pin_enabled = pin_state;
4154 if (stream_state >= 0)
4155 path->stream_enabled = stream_state;
4156 if ((!path->pin_fixed && path->pin_enabled != pin_old)
4157 || path->stream_enabled != stream_old) {
4158 last = path_power_update(codec, path, true);
4167 /* check the jack status for power control */
4168 static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4170 if (!is_jack_detectable(codec, pin))
4172 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4175 /* power up/down the paths of the given pin according to the jack state;
4176 * power = 0/1 : only power up/down if it matches with the jack state,
4177 * < 0 : force power up/down to follow the jack sate
4179 * returns the last changed NID, or zero if unchanged.
4181 static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4186 if (!codec->power_save_node)
4189 on = detect_pin_state(codec, pin);
4191 if (power >= 0 && on != power)
4193 return set_path_power(codec, pin, on, -1);
4196 static void pin_power_callback(struct hda_codec *codec,
4197 struct hda_jack_callback *jack,
4200 if (jack && jack->nid)
4201 sync_power_state_change(codec,
4202 set_pin_power_jack(codec, jack->nid, on));
4205 /* callback only doing power up -- called at first */
4206 static void pin_power_up_callback(struct hda_codec *codec,
4207 struct hda_jack_callback *jack)
4209 pin_power_callback(codec, jack, true);
4212 /* callback only doing power down -- called at last */
4213 static void pin_power_down_callback(struct hda_codec *codec,
4214 struct hda_jack_callback *jack)
4216 pin_power_callback(codec, jack, false);
4219 /* set up the power up/down callbacks */
4220 static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4221 const hda_nid_t *pins, bool on)
4224 hda_jack_callback_fn cb =
4225 on ? pin_power_up_callback : pin_power_down_callback;
4227 for (i = 0; i < num_pins && pins[i]; i++) {
4228 if (is_jack_detectable(codec, pins[i]))
4229 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4231 set_path_power(codec, pins[i], true, -1);
4235 /* enabled power callback to each available I/O pin with jack detections;
4236 * the digital I/O pins are excluded because of the unreliable detectsion
4238 static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4240 struct hda_gen_spec *spec = codec->spec;
4241 struct auto_pin_cfg *cfg = &spec->autocfg;
4244 if (!codec->power_save_node)
4246 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4247 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4248 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4249 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4250 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4251 for (i = 0; i < cfg->num_inputs; i++)
4252 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4255 /* sync path power up/down with the jack states of given pins */
4256 static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4257 const hda_nid_t *pins)
4261 for (i = 0; i < num_pins && pins[i]; i++)
4262 if (is_jack_detectable(codec, pins[i]))
4263 set_pin_power_jack(codec, pins[i], -1);
4266 /* sync path power up/down with pins; called at init and resume */
4267 static void sync_all_pin_power_ctls(struct hda_codec *codec)
4269 struct hda_gen_spec *spec = codec->spec;
4270 struct auto_pin_cfg *cfg = &spec->autocfg;
4273 if (!codec->power_save_node)
4275 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4276 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4277 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4278 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4279 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4280 for (i = 0; i < cfg->num_inputs; i++)
4281 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4284 /* add fake paths if not present yet */
4285 static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4286 int num_pins, const hda_nid_t *pins)
4288 struct hda_gen_spec *spec = codec->spec;
4289 struct nid_path *path;
4292 for (i = 0; i < num_pins; i++) {
4295 if (get_nid_path(codec, nid, pins[i], 0))
4297 path = snd_array_new(&spec->paths);
4300 memset(path, 0, sizeof(*path));
4302 path->path[0] = nid;
4303 path->path[1] = pins[i];
4304 path->active = true;
4309 /* create fake paths to all outputs from beep */
4310 static int add_fake_beep_paths(struct hda_codec *codec)
4312 struct hda_gen_spec *spec = codec->spec;
4313 struct auto_pin_cfg *cfg = &spec->autocfg;
4314 hda_nid_t nid = spec->beep_nid;
4317 if (!codec->power_save_node || !nid)
4319 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4322 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4323 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4327 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4328 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4336 /* power up/down beep widget and its output paths */
4337 static void beep_power_hook(struct hda_beep *beep, bool on)
4339 set_path_power(beep->codec, beep->nid, -1, on);
4343 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4344 * @codec: the HDA codec
4345 * @pin: NID of pin to fix
4347 int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4349 struct hda_gen_spec *spec = codec->spec;
4350 struct nid_path *path;
4352 path = snd_array_new(&spec->paths);
4355 memset(path, 0, sizeof(*path));
4357 path->path[0] = pin;
4358 path->active = true;
4359 path->pin_fixed = true;
4360 path->stream_enabled = true;
4363 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4366 * Jack detections for HP auto-mute and mic-switch
4369 /* check each pin in the given array; returns true if any of them is plugged */
4370 static bool detect_jacks(struct hda_codec *codec, int num_pins, const hda_nid_t *pins)
4373 bool present = false;
4375 for (i = 0; i < num_pins; i++) {
4376 hda_nid_t nid = pins[i];
4379 /* don't detect pins retasked as inputs */
4380 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4382 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4388 /* standard HP/line-out auto-mute helper */
4389 static void do_automute(struct hda_codec *codec, int num_pins, const hda_nid_t *pins,
4390 int *paths, bool mute)
4392 struct hda_gen_spec *spec = codec->spec;
4395 for (i = 0; i < num_pins; i++) {
4396 hda_nid_t nid = pins[i];
4397 unsigned int val, oldval;
4401 oldval = snd_hda_codec_get_pin_target(codec, nid);
4402 if (oldval & PIN_IN)
4403 continue; /* no mute for inputs */
4405 if (spec->auto_mute_via_amp) {
4406 struct nid_path *path;
4409 path = snd_hda_get_path_from_idx(codec, paths[i]);
4412 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4416 spec->mute_bits |= (1ULL << mute_nid);
4418 spec->mute_bits &= ~(1ULL << mute_nid);
4421 /* don't reset VREF value in case it's controlling
4422 * the amp (see alc861_fixup_asus_amp_vref_0f())
4424 if (spec->keep_vref_in_automute)
4425 val = oldval & ~PIN_HP;
4430 /* here we call update_pin_ctl() so that the pinctl is
4431 * changed without changing the pinctl target value;
4432 * the original target value will be still referred at
4433 * the init / resume again
4435 update_pin_ctl(codec, nid, val);
4438 set_pin_eapd(codec, nid, !mute);
4439 if (codec->power_save_node) {
4442 on = detect_pin_state(codec, nid);
4443 set_path_power(codec, nid, on, -1);
4449 * snd_hda_gen_update_outputs - Toggle outputs muting
4450 * @codec: the HDA codec
4452 * Update the mute status of all outputs based on the current jack states.
4454 void snd_hda_gen_update_outputs(struct hda_codec *codec)
4456 struct hda_gen_spec *spec = codec->spec;
4460 /* Control HP pins/amps depending on master_mute state;
4461 * in general, HP pins/amps control should be enabled in all cases,
4462 * but currently set only for master_mute, just to be safe
4464 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4465 paths = spec->out_paths;
4467 paths = spec->hp_paths;
4468 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
4469 spec->autocfg.hp_pins, paths, spec->master_mute);
4471 if (!spec->automute_speaker)
4474 on = spec->hp_jack_present | spec->line_jack_present;
4475 on |= spec->master_mute;
4476 spec->speaker_muted = on;
4477 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4478 paths = spec->out_paths;
4480 paths = spec->speaker_paths;
4481 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
4482 spec->autocfg.speaker_pins, paths, on);
4484 /* toggle line-out mutes if needed, too */
4485 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4486 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4487 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4489 if (!spec->automute_lo)
4492 on = spec->hp_jack_present;
4493 on |= spec->master_mute;
4494 spec->line_out_muted = on;
4495 paths = spec->out_paths;
4496 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4497 spec->autocfg.line_out_pins, paths, on);
4499 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4501 static void call_update_outputs(struct hda_codec *codec)
4503 struct hda_gen_spec *spec = codec->spec;
4504 if (spec->automute_hook)
4505 spec->automute_hook(codec);
4507 snd_hda_gen_update_outputs(codec);
4509 /* sync the whole vmaster followers to reflect the new auto-mute status */
4510 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4511 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4515 * snd_hda_gen_hp_automute - standard HP-automute helper
4516 * @codec: the HDA codec
4517 * @jack: jack object, NULL for the whole
4519 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4520 struct hda_jack_callback *jack)
4522 struct hda_gen_spec *spec = codec->spec;
4523 hda_nid_t *pins = spec->autocfg.hp_pins;
4524 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4526 /* No detection for the first HP jack during indep-HP mode */
4527 if (spec->indep_hp_enabled) {
4532 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4533 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4535 call_update_outputs(codec);
4537 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4540 * snd_hda_gen_line_automute - standard line-out-automute helper
4541 * @codec: the HDA codec
4542 * @jack: jack object, NULL for the whole
4544 void snd_hda_gen_line_automute(struct hda_codec *codec,
4545 struct hda_jack_callback *jack)
4547 struct hda_gen_spec *spec = codec->spec;
4549 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4551 /* check LO jack only when it's different from HP */
4552 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4555 spec->line_jack_present =
4556 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4557 spec->autocfg.line_out_pins);
4558 if (!spec->automute_speaker || !spec->detect_lo)
4560 call_update_outputs(codec);
4562 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4565 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4566 * @codec: the HDA codec
4567 * @jack: jack object, NULL for the whole
4569 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4570 struct hda_jack_callback *jack)
4572 struct hda_gen_spec *spec = codec->spec;
4575 if (!spec->auto_mic)
4578 for (i = spec->am_num_entries - 1; i > 0; i--) {
4579 hda_nid_t pin = spec->am_entry[i].pin;
4580 /* don't detect pins retasked as outputs */
4581 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4583 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4584 mux_select(codec, 0, spec->am_entry[i].idx);
4588 mux_select(codec, 0, spec->am_entry[0].idx);
4590 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4592 /* call appropriate hooks */
4593 static void call_hp_automute(struct hda_codec *codec,
4594 struct hda_jack_callback *jack)
4596 struct hda_gen_spec *spec = codec->spec;
4597 if (spec->hp_automute_hook)
4598 spec->hp_automute_hook(codec, jack);
4600 snd_hda_gen_hp_automute(codec, jack);
4603 static void call_line_automute(struct hda_codec *codec,
4604 struct hda_jack_callback *jack)
4606 struct hda_gen_spec *spec = codec->spec;
4607 if (spec->line_automute_hook)
4608 spec->line_automute_hook(codec, jack);
4610 snd_hda_gen_line_automute(codec, jack);
4613 static void call_mic_autoswitch(struct hda_codec *codec,
4614 struct hda_jack_callback *jack)
4616 struct hda_gen_spec *spec = codec->spec;
4617 if (spec->mic_autoswitch_hook)
4618 spec->mic_autoswitch_hook(codec, jack);
4620 snd_hda_gen_mic_autoswitch(codec, jack);
4623 /* update jack retasking */
4624 static void update_automute_all(struct hda_codec *codec)
4626 call_hp_automute(codec, NULL);
4627 call_line_automute(codec, NULL);
4628 call_mic_autoswitch(codec, NULL);
4632 * Auto-Mute mode mixer enum support
4634 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4635 struct snd_ctl_elem_info *uinfo)
4637 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4638 struct hda_gen_spec *spec = codec->spec;
4639 static const char * const texts3[] = {
4640 "Disabled", "Speaker Only", "Line Out+Speaker"
4643 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4644 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4645 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4648 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4649 struct snd_ctl_elem_value *ucontrol)
4651 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4652 struct hda_gen_spec *spec = codec->spec;
4653 unsigned int val = 0;
4654 if (spec->automute_speaker)
4656 if (spec->automute_lo)
4659 ucontrol->value.enumerated.item[0] = val;
4663 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4664 struct snd_ctl_elem_value *ucontrol)
4666 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4667 struct hda_gen_spec *spec = codec->spec;
4669 switch (ucontrol->value.enumerated.item[0]) {
4671 if (!spec->automute_speaker && !spec->automute_lo)
4673 spec->automute_speaker = 0;
4674 spec->automute_lo = 0;
4677 if (spec->automute_speaker_possible) {
4678 if (!spec->automute_lo && spec->automute_speaker)
4680 spec->automute_speaker = 1;
4681 spec->automute_lo = 0;
4682 } else if (spec->automute_lo_possible) {
4683 if (spec->automute_lo)
4685 spec->automute_lo = 1;
4690 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4692 if (spec->automute_speaker && spec->automute_lo)
4694 spec->automute_speaker = 1;
4695 spec->automute_lo = 1;
4700 call_update_outputs(codec);
4704 static const struct snd_kcontrol_new automute_mode_enum = {
4705 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4706 .name = "Auto-Mute Mode",
4707 .info = automute_mode_info,
4708 .get = automute_mode_get,
4709 .put = automute_mode_put,
4712 static int add_automute_mode_enum(struct hda_codec *codec)
4714 struct hda_gen_spec *spec = codec->spec;
4716 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4722 * Check the availability of HP/line-out auto-mute;
4723 * Set up appropriately if really supported
4725 static int check_auto_mute_availability(struct hda_codec *codec)
4727 struct hda_gen_spec *spec = codec->spec;
4728 struct auto_pin_cfg *cfg = &spec->autocfg;
4732 if (spec->suppress_auto_mute)
4735 if (cfg->hp_pins[0])
4737 if (cfg->line_out_pins[0])
4739 if (cfg->speaker_pins[0])
4741 if (present < 2) /* need two different output types */
4744 if (!cfg->speaker_pins[0] &&
4745 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4746 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4747 sizeof(cfg->speaker_pins));
4748 cfg->speaker_outs = cfg->line_outs;
4751 if (!cfg->hp_pins[0] &&
4752 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4753 memcpy(cfg->hp_pins, cfg->line_out_pins,
4754 sizeof(cfg->hp_pins));
4755 cfg->hp_outs = cfg->line_outs;
4758 for (i = 0; i < cfg->hp_outs; i++) {
4759 hda_nid_t nid = cfg->hp_pins[i];
4760 if (!is_jack_detectable(codec, nid))
4762 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4763 snd_hda_jack_detect_enable_callback(codec, nid,
4765 spec->detect_hp = 1;
4768 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4769 if (cfg->speaker_outs)
4770 for (i = 0; i < cfg->line_outs; i++) {
4771 hda_nid_t nid = cfg->line_out_pins[i];
4772 if (!is_jack_detectable(codec, nid))
4774 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4775 snd_hda_jack_detect_enable_callback(codec, nid,
4776 call_line_automute);
4777 spec->detect_lo = 1;
4779 spec->automute_lo_possible = spec->detect_hp;
4782 spec->automute_speaker_possible = cfg->speaker_outs &&
4783 (spec->detect_hp || spec->detect_lo);
4785 spec->automute_lo = spec->automute_lo_possible;
4786 spec->automute_speaker = spec->automute_speaker_possible;
4788 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4789 /* create a control for automute mode */
4790 err = add_automute_mode_enum(codec);
4797 /* check whether all auto-mic pins are valid; setup indices if OK */
4798 static bool auto_mic_check_imux(struct hda_codec *codec)
4800 struct hda_gen_spec *spec = codec->spec;
4801 const struct hda_input_mux *imux;
4804 imux = &spec->input_mux;
4805 for (i = 0; i < spec->am_num_entries; i++) {
4806 spec->am_entry[i].idx =
4807 find_idx_in_nid_list(spec->am_entry[i].pin,
4808 spec->imux_pins, imux->num_items);
4809 if (spec->am_entry[i].idx < 0)
4810 return false; /* no corresponding imux */
4813 /* we don't need the jack detection for the first pin */
4814 for (i = 1; i < spec->am_num_entries; i++)
4815 snd_hda_jack_detect_enable_callback(codec,
4816 spec->am_entry[i].pin,
4817 call_mic_autoswitch);
4821 static int compare_attr(const void *ap, const void *bp)
4823 const struct automic_entry *a = ap;
4824 const struct automic_entry *b = bp;
4825 return (int)(a->attr - b->attr);
4829 * Check the availability of auto-mic switch;
4830 * Set up if really supported
4832 static int check_auto_mic_availability(struct hda_codec *codec)
4834 struct hda_gen_spec *spec = codec->spec;
4835 struct auto_pin_cfg *cfg = &spec->autocfg;
4839 if (spec->suppress_auto_mic)
4844 for (i = 0; i < cfg->num_inputs; i++) {
4845 hda_nid_t nid = cfg->inputs[i].pin;
4847 attr = snd_hda_codec_get_pincfg(codec, nid);
4848 attr = snd_hda_get_input_pin_attr(attr);
4849 if (types & (1 << attr))
4850 return 0; /* already occupied */
4852 case INPUT_PIN_ATTR_INT:
4853 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4854 return 0; /* invalid type */
4856 case INPUT_PIN_ATTR_UNUSED:
4857 return 0; /* invalid entry */
4859 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4860 return 0; /* invalid type */
4861 if (!spec->line_in_auto_switch &&
4862 cfg->inputs[i].type != AUTO_PIN_MIC)
4863 return 0; /* only mic is allowed */
4864 if (!is_jack_detectable(codec, nid))
4865 return 0; /* no unsol support */
4868 if (num_pins >= MAX_AUTO_MIC_PINS)
4870 types |= (1 << attr);
4871 spec->am_entry[num_pins].pin = nid;
4872 spec->am_entry[num_pins].attr = attr;
4879 spec->am_num_entries = num_pins;
4880 /* sort the am_entry in the order of attr so that the pin with a
4881 * higher attr will be selected when the jack is plugged.
4883 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4884 compare_attr, NULL);
4886 if (!auto_mic_check_imux(codec))
4890 spec->num_adc_nids = 1;
4891 spec->cur_mux[0] = spec->am_entry[0].idx;
4892 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4893 spec->am_entry[0].pin,
4894 spec->am_entry[1].pin,
4895 spec->am_entry[2].pin);
4901 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4903 * @codec: the HDA codec
4904 * @nid: NID to evalute
4905 * @power_state: target power state
4907 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4909 unsigned int power_state)
4911 struct hda_gen_spec *spec = codec->spec;
4913 if (!spec->power_down_unused && !codec->power_save_node)
4915 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
4917 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4919 if (is_active_nid_for_any(codec, nid))
4923 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4925 /* mute all aamix inputs initially; parse up to the first leaves */
4926 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4929 const hda_nid_t *conn;
4932 nums = snd_hda_get_conn_list(codec, mix, &conn);
4933 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4934 for (i = 0; i < nums; i++) {
4936 update_amp(codec, mix, HDA_INPUT, i,
4937 0xff, HDA_AMP_MUTE);
4938 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4939 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4940 0xff, HDA_AMP_MUTE);
4945 * snd_hda_gen_stream_pm - Stream power management callback
4946 * @codec: the HDA codec
4947 * @nid: audio widget
4948 * @on: power on/off flag
4950 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
4952 void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4954 if (codec->power_save_node)
4955 set_path_power(codec, nid, -1, on);
4957 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4960 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4961 * set up the hda_gen_spec
4962 * @codec: the HDA codec
4963 * @cfg: Parsed pin configuration
4965 * return 1 if successful, 0 if the proper config is not found,
4966 * or a negative error code
4968 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4969 struct auto_pin_cfg *cfg)
4971 struct hda_gen_spec *spec = codec->spec;
4974 parse_user_hints(codec);
4976 if (spec->vmaster_mute_led || spec->mic_mute_led)
4977 snd_ctl_led_request();
4979 if (spec->mixer_nid && !spec->mixer_merge_nid)
4980 spec->mixer_merge_nid = spec->mixer_nid;
4982 if (cfg != &spec->autocfg) {
4983 spec->autocfg = *cfg;
4984 cfg = &spec->autocfg;
4987 if (!spec->main_out_badness)
4988 spec->main_out_badness = &hda_main_out_badness;
4989 if (!spec->extra_out_badness)
4990 spec->extra_out_badness = &hda_extra_out_badness;
4992 fill_all_dac_nids(codec);
4994 if (!cfg->line_outs) {
4995 if (cfg->dig_outs || cfg->dig_in_pin) {
4996 spec->multiout.max_channels = 2;
4997 spec->no_analog = 1;
5000 if (!cfg->num_inputs && !cfg->dig_in_pin)
5001 return 0; /* can't find valid BIOS pin config */
5004 if (!spec->no_primary_hp &&
5005 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
5006 cfg->line_outs <= cfg->hp_outs) {
5007 /* use HP as primary out */
5008 cfg->speaker_outs = cfg->line_outs;
5009 memcpy(cfg->speaker_pins, cfg->line_out_pins,
5010 sizeof(cfg->speaker_pins));
5011 cfg->line_outs = cfg->hp_outs;
5012 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
5014 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
5015 cfg->line_out_type = AUTO_PIN_HP_OUT;
5018 err = parse_output_paths(codec);
5021 err = create_multi_channel_mode(codec);
5024 err = create_multi_out_ctls(codec, cfg);
5027 err = create_hp_out_ctls(codec);
5030 err = create_speaker_out_ctls(codec);
5033 err = create_indep_hp_ctls(codec);
5036 err = create_loopback_mixing_ctl(codec);
5039 err = create_hp_mic(codec);
5042 err = create_input_ctls(codec);
5046 /* add power-down pin callbacks at first */
5047 add_all_pin_power_ctls(codec, false);
5049 spec->const_channel_count = spec->ext_channel_count;
5050 /* check the multiple speaker and headphone pins */
5051 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
5052 spec->const_channel_count = max(spec->const_channel_count,
5053 cfg->speaker_outs * 2);
5054 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
5055 spec->const_channel_count = max(spec->const_channel_count,
5057 spec->multiout.max_channels = max(spec->ext_channel_count,
5058 spec->const_channel_count);
5060 err = check_auto_mute_availability(codec);
5064 err = check_dyn_adc_switch(codec);
5068 err = check_auto_mic_availability(codec);
5072 /* add stereo mix if available and not enabled yet */
5073 if (!spec->auto_mic && spec->mixer_nid &&
5074 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
5075 spec->input_mux.num_items > 1) {
5076 err = parse_capture_source(codec, spec->mixer_nid,
5077 CFG_IDX_MIX, spec->num_all_adcs,
5084 err = create_capture_mixers(codec);
5088 err = parse_mic_boost(codec);
5092 /* create "Headphone Mic Jack Mode" if no input selection is
5093 * available (or user specifies add_jack_modes hint)
5095 if (spec->hp_mic_pin &&
5096 (spec->auto_mic || spec->input_mux.num_items == 1 ||
5097 spec->add_jack_modes)) {
5098 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
5103 if (spec->add_jack_modes) {
5104 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
5105 err = create_out_jack_modes(codec, cfg->line_outs,
5106 cfg->line_out_pins);
5110 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
5111 err = create_out_jack_modes(codec, cfg->hp_outs,
5118 /* add power-up pin callbacks at last */
5119 add_all_pin_power_ctls(codec, true);
5121 /* mute all aamix input initially */
5122 if (spec->mixer_nid)
5123 mute_all_mixer_nid(codec, spec->mixer_nid);
5126 parse_digital(codec);
5128 if (spec->power_down_unused || codec->power_save_node) {
5129 if (!codec->power_filter)
5130 codec->power_filter = snd_hda_gen_path_power_filter;
5131 if (!codec->patch_ops.stream_pm)
5132 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
5135 if (!spec->no_analog && spec->beep_nid) {
5136 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
5139 if (codec->beep && codec->power_save_node) {
5140 err = add_fake_beep_paths(codec);
5143 codec->beep->power_hook = beep_power_hook;
5149 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
5153 * Build control elements
5156 /* follower controls for virtual master */
5157 static const char * const follower_pfxs[] = {
5158 "Front", "Surround", "Center", "LFE", "Side",
5159 "Headphone", "Speaker", "Mono", "Line Out",
5160 "CLFE", "Bass Speaker", "PCM",
5161 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5162 "Headphone Front", "Headphone Surround", "Headphone CLFE",
5163 "Headphone Side", "Headphone+LO", "Speaker+LO",
5168 * snd_hda_gen_build_controls - Build controls from the parsed results
5169 * @codec: the HDA codec
5171 * Pass this to build_controls patch_ops.
5173 int snd_hda_gen_build_controls(struct hda_codec *codec)
5175 struct hda_gen_spec *spec = codec->spec;
5178 if (spec->kctls.used) {
5179 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5184 if (spec->multiout.dig_out_nid) {
5185 err = snd_hda_create_dig_out_ctls(codec,
5186 spec->multiout.dig_out_nid,
5187 spec->multiout.dig_out_nid,
5188 spec->pcm_rec[1]->pcm_type);
5191 if (!spec->no_analog) {
5192 err = snd_hda_create_spdif_share_sw(codec,
5196 spec->multiout.share_spdif = 1;
5199 if (spec->dig_in_nid) {
5200 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5205 /* if we have no master control, let's create it */
5206 if (!spec->no_analog && !spec->suppress_vmaster &&
5207 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
5208 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
5209 spec->vmaster_tlv, follower_pfxs,
5210 "Playback Volume", 0);
5214 if (!spec->no_analog && !spec->suppress_vmaster &&
5215 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5216 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5217 NULL, follower_pfxs,
5218 "Playback Switch", true,
5219 spec->vmaster_mute_led ?
5220 SNDRV_CTL_ELEM_ACCESS_SPK_LED : 0,
5221 &spec->vmaster_mute.sw_kctl);
5224 if (spec->vmaster_mute.hook) {
5225 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute);
5226 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5230 free_kctls(spec); /* no longer needed */
5232 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5238 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
5245 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5246 struct hda_codec *codec,
5247 struct snd_pcm_substream *substream,
5250 struct hda_gen_spec *spec = codec->spec;
5251 if (spec->pcm_playback_hook)
5252 spec->pcm_playback_hook(hinfo, codec, substream, action);
5255 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5256 struct hda_codec *codec,
5257 struct snd_pcm_substream *substream,
5260 struct hda_gen_spec *spec = codec->spec;
5261 if (spec->pcm_capture_hook)
5262 spec->pcm_capture_hook(hinfo, codec, substream, action);
5266 * Analog playback callbacks
5268 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5269 struct hda_codec *codec,
5270 struct snd_pcm_substream *substream)
5272 struct hda_gen_spec *spec = codec->spec;
5275 mutex_lock(&spec->pcm_mutex);
5276 err = snd_hda_multi_out_analog_open(codec,
5277 &spec->multiout, substream,
5280 spec->active_streams |= 1 << STREAM_MULTI_OUT;
5281 call_pcm_playback_hook(hinfo, codec, substream,
5282 HDA_GEN_PCM_ACT_OPEN);
5284 mutex_unlock(&spec->pcm_mutex);
5288 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5289 struct hda_codec *codec,
5290 unsigned int stream_tag,
5291 unsigned int format,
5292 struct snd_pcm_substream *substream)
5294 struct hda_gen_spec *spec = codec->spec;
5297 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5298 stream_tag, format, substream);
5300 call_pcm_playback_hook(hinfo, codec, substream,
5301 HDA_GEN_PCM_ACT_PREPARE);
5305 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5306 struct hda_codec *codec,
5307 struct snd_pcm_substream *substream)
5309 struct hda_gen_spec *spec = codec->spec;
5312 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5314 call_pcm_playback_hook(hinfo, codec, substream,
5315 HDA_GEN_PCM_ACT_CLEANUP);
5319 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5320 struct hda_codec *codec,
5321 struct snd_pcm_substream *substream)
5323 struct hda_gen_spec *spec = codec->spec;
5324 mutex_lock(&spec->pcm_mutex);
5325 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
5326 call_pcm_playback_hook(hinfo, codec, substream,
5327 HDA_GEN_PCM_ACT_CLOSE);
5328 mutex_unlock(&spec->pcm_mutex);
5332 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5333 struct hda_codec *codec,
5334 struct snd_pcm_substream *substream)
5336 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5340 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5341 struct hda_codec *codec,
5342 unsigned int stream_tag,
5343 unsigned int format,
5344 struct snd_pcm_substream *substream)
5346 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5347 call_pcm_capture_hook(hinfo, codec, substream,
5348 HDA_GEN_PCM_ACT_PREPARE);
5352 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5353 struct hda_codec *codec,
5354 struct snd_pcm_substream *substream)
5356 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5357 call_pcm_capture_hook(hinfo, codec, substream,
5358 HDA_GEN_PCM_ACT_CLEANUP);
5362 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5363 struct hda_codec *codec,
5364 struct snd_pcm_substream *substream)
5366 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5370 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5371 struct hda_codec *codec,
5372 struct snd_pcm_substream *substream)
5374 struct hda_gen_spec *spec = codec->spec;
5377 mutex_lock(&spec->pcm_mutex);
5378 if (spec->indep_hp && !spec->indep_hp_enabled)
5381 spec->active_streams |= 1 << STREAM_INDEP_HP;
5382 call_pcm_playback_hook(hinfo, codec, substream,
5383 HDA_GEN_PCM_ACT_OPEN);
5384 mutex_unlock(&spec->pcm_mutex);
5388 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5389 struct hda_codec *codec,
5390 struct snd_pcm_substream *substream)
5392 struct hda_gen_spec *spec = codec->spec;
5393 mutex_lock(&spec->pcm_mutex);
5394 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
5395 call_pcm_playback_hook(hinfo, codec, substream,
5396 HDA_GEN_PCM_ACT_CLOSE);
5397 mutex_unlock(&spec->pcm_mutex);
5401 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5402 struct hda_codec *codec,
5403 unsigned int stream_tag,
5404 unsigned int format,
5405 struct snd_pcm_substream *substream)
5407 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5408 call_pcm_playback_hook(hinfo, codec, substream,
5409 HDA_GEN_PCM_ACT_PREPARE);
5413 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5414 struct hda_codec *codec,
5415 struct snd_pcm_substream *substream)
5417 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5418 call_pcm_playback_hook(hinfo, codec, substream,
5419 HDA_GEN_PCM_ACT_CLEANUP);
5426 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5427 struct hda_codec *codec,
5428 struct snd_pcm_substream *substream)
5430 struct hda_gen_spec *spec = codec->spec;
5431 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5434 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5435 struct hda_codec *codec,
5436 unsigned int stream_tag,
5437 unsigned int format,
5438 struct snd_pcm_substream *substream)
5440 struct hda_gen_spec *spec = codec->spec;
5441 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5442 stream_tag, format, substream);
5445 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5446 struct hda_codec *codec,
5447 struct snd_pcm_substream *substream)
5449 struct hda_gen_spec *spec = codec->spec;
5450 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5453 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5454 struct hda_codec *codec,
5455 struct snd_pcm_substream *substream)
5457 struct hda_gen_spec *spec = codec->spec;
5458 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5464 #define alt_capture_pcm_open capture_pcm_open
5465 #define alt_capture_pcm_close capture_pcm_close
5467 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5468 struct hda_codec *codec,
5469 unsigned int stream_tag,
5470 unsigned int format,
5471 struct snd_pcm_substream *substream)
5473 struct hda_gen_spec *spec = codec->spec;
5475 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5476 stream_tag, 0, format);
5477 call_pcm_capture_hook(hinfo, codec, substream,
5478 HDA_GEN_PCM_ACT_PREPARE);
5482 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5483 struct hda_codec *codec,
5484 struct snd_pcm_substream *substream)
5486 struct hda_gen_spec *spec = codec->spec;
5488 snd_hda_codec_cleanup_stream(codec,
5489 spec->adc_nids[substream->number + 1]);
5490 call_pcm_capture_hook(hinfo, codec, substream,
5491 HDA_GEN_PCM_ACT_CLEANUP);
5497 static const struct hda_pcm_stream pcm_analog_playback = {
5501 /* NID is set in build_pcms */
5503 .open = playback_pcm_open,
5504 .close = playback_pcm_close,
5505 .prepare = playback_pcm_prepare,
5506 .cleanup = playback_pcm_cleanup
5510 static const struct hda_pcm_stream pcm_analog_capture = {
5514 /* NID is set in build_pcms */
5516 .open = capture_pcm_open,
5517 .close = capture_pcm_close,
5518 .prepare = capture_pcm_prepare,
5519 .cleanup = capture_pcm_cleanup
5523 static const struct hda_pcm_stream pcm_analog_alt_playback = {
5527 /* NID is set in build_pcms */
5529 .open = alt_playback_pcm_open,
5530 .close = alt_playback_pcm_close,
5531 .prepare = alt_playback_pcm_prepare,
5532 .cleanup = alt_playback_pcm_cleanup
5536 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5537 .substreams = 2, /* can be overridden */
5540 /* NID is set in build_pcms */
5542 .open = alt_capture_pcm_open,
5543 .close = alt_capture_pcm_close,
5544 .prepare = alt_capture_pcm_prepare,
5545 .cleanup = alt_capture_pcm_cleanup
5549 static const struct hda_pcm_stream pcm_digital_playback = {
5553 /* NID is set in build_pcms */
5555 .open = dig_playback_pcm_open,
5556 .close = dig_playback_pcm_close,
5557 .prepare = dig_playback_pcm_prepare,
5558 .cleanup = dig_playback_pcm_cleanup
5562 static const struct hda_pcm_stream pcm_digital_capture = {
5566 /* NID is set in build_pcms */
5569 /* Used by build_pcms to flag that a PCM has no playback stream */
5570 static const struct hda_pcm_stream pcm_null_stream = {
5577 * dynamic changing ADC PCM streams
5579 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5581 struct hda_gen_spec *spec = codec->spec;
5582 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5584 if (spec->cur_adc && spec->cur_adc != new_adc) {
5585 /* stream is running, let's swap the current ADC */
5586 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5587 spec->cur_adc = new_adc;
5588 snd_hda_codec_setup_stream(codec, new_adc,
5589 spec->cur_adc_stream_tag, 0,
5590 spec->cur_adc_format);
5596 /* analog capture with dynamic dual-adc changes */
5597 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5598 struct hda_codec *codec,
5599 unsigned int stream_tag,
5600 unsigned int format,
5601 struct snd_pcm_substream *substream)
5603 struct hda_gen_spec *spec = codec->spec;
5604 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5605 spec->cur_adc_stream_tag = stream_tag;
5606 spec->cur_adc_format = format;
5607 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5608 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
5612 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5613 struct hda_codec *codec,
5614 struct snd_pcm_substream *substream)
5616 struct hda_gen_spec *spec = codec->spec;
5617 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5619 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
5623 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5627 .nid = 0, /* fill later */
5629 .prepare = dyn_adc_capture_pcm_prepare,
5630 .cleanup = dyn_adc_capture_pcm_cleanup
5634 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5635 const char *chip_name)
5641 strscpy(str, chip_name, len);
5643 /* drop non-alnum chars after a space */
5644 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5645 if (!isalnum(p[1])) {
5650 strlcat(str, sfx, len);
5653 /* copy PCM stream info from @default_str, and override non-NULL entries
5654 * from @spec_str and @nid
5656 static void setup_pcm_stream(struct hda_pcm_stream *str,
5657 const struct hda_pcm_stream *default_str,
5658 const struct hda_pcm_stream *spec_str,
5661 *str = *default_str;
5665 if (spec_str->substreams)
5666 str->substreams = spec_str->substreams;
5667 if (spec_str->channels_min)
5668 str->channels_min = spec_str->channels_min;
5669 if (spec_str->channels_max)
5670 str->channels_max = spec_str->channels_max;
5671 if (spec_str->rates)
5672 str->rates = spec_str->rates;
5673 if (spec_str->formats)
5674 str->formats = spec_str->formats;
5675 if (spec_str->maxbps)
5676 str->maxbps = spec_str->maxbps;
5681 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5682 * @codec: the HDA codec
5684 * Pass this to build_pcms patch_ops.
5686 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5688 struct hda_gen_spec *spec = codec->spec;
5689 struct hda_pcm *info;
5690 bool have_multi_adcs;
5692 if (spec->no_analog)
5695 fill_pcm_stream_name(spec->stream_name_analog,
5696 sizeof(spec->stream_name_analog),
5697 " Analog", codec->core.chip_name);
5698 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5701 spec->pcm_rec[0] = info;
5703 if (spec->multiout.num_dacs > 0) {
5704 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5705 &pcm_analog_playback,
5706 spec->stream_analog_playback,
5707 spec->multiout.dac_nids[0]);
5708 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5709 spec->multiout.max_channels;
5710 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5711 spec->autocfg.line_outs == 2)
5712 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5715 if (spec->num_adc_nids) {
5716 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5717 (spec->dyn_adc_switch ?
5718 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5719 spec->stream_analog_capture,
5724 /* SPDIF for stream index #1 */
5725 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5726 fill_pcm_stream_name(spec->stream_name_digital,
5727 sizeof(spec->stream_name_digital),
5728 " Digital", codec->core.chip_name);
5729 info = snd_hda_codec_pcm_new(codec, "%s",
5730 spec->stream_name_digital);
5733 codec->follower_dig_outs = spec->multiout.follower_dig_outs;
5734 spec->pcm_rec[1] = info;
5735 if (spec->dig_out_type)
5736 info->pcm_type = spec->dig_out_type;
5738 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5739 if (spec->multiout.dig_out_nid)
5740 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5741 &pcm_digital_playback,
5742 spec->stream_digital_playback,
5743 spec->multiout.dig_out_nid);
5744 if (spec->dig_in_nid)
5745 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5746 &pcm_digital_capture,
5747 spec->stream_digital_capture,
5751 if (spec->no_analog)
5754 /* If the use of more than one ADC is requested for the current
5755 * model, configure a second analog capture-only PCM.
5757 have_multi_adcs = (spec->num_adc_nids > 1) &&
5758 !spec->dyn_adc_switch && !spec->auto_mic;
5759 /* Additional Analaog capture for index #2 */
5760 if (spec->alt_dac_nid || have_multi_adcs) {
5761 fill_pcm_stream_name(spec->stream_name_alt_analog,
5762 sizeof(spec->stream_name_alt_analog),
5763 " Alt Analog", codec->core.chip_name);
5764 info = snd_hda_codec_pcm_new(codec, "%s",
5765 spec->stream_name_alt_analog);
5768 spec->pcm_rec[2] = info;
5769 if (spec->alt_dac_nid)
5770 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5771 &pcm_analog_alt_playback,
5772 spec->stream_analog_alt_playback,
5775 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5776 &pcm_null_stream, NULL, 0);
5777 if (have_multi_adcs) {
5778 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5779 &pcm_analog_alt_capture,
5780 spec->stream_analog_alt_capture,
5782 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5783 spec->num_adc_nids - 1;
5785 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5786 &pcm_null_stream, NULL, 0);
5792 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5796 * Standard auto-parser initializations
5799 /* configure the given path as a proper output */
5800 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5802 struct nid_path *path;
5805 path = snd_hda_get_path_from_idx(codec, path_idx);
5806 if (!path || !path->depth)
5808 pin = path->path[path->depth - 1];
5809 restore_pin_ctl(codec, pin);
5810 snd_hda_activate_path(codec, path, path->active,
5811 aamix_default(codec->spec));
5812 set_pin_eapd(codec, pin, path->active);
5815 /* initialize primary output paths */
5816 static void init_multi_out(struct hda_codec *codec)
5818 struct hda_gen_spec *spec = codec->spec;
5821 for (i = 0; i < spec->autocfg.line_outs; i++)
5822 set_output_and_unmute(codec, spec->out_paths[i]);
5826 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5830 for (i = 0; i < num_outs; i++)
5831 set_output_and_unmute(codec, paths[i]);
5834 /* initialize hp and speaker paths */
5835 static void init_extra_out(struct hda_codec *codec)
5837 struct hda_gen_spec *spec = codec->spec;
5839 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5840 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5841 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5842 __init_extra_out(codec, spec->autocfg.speaker_outs,
5843 spec->speaker_paths);
5846 /* initialize multi-io paths */
5847 static void init_multi_io(struct hda_codec *codec)
5849 struct hda_gen_spec *spec = codec->spec;
5852 for (i = 0; i < spec->multi_ios; i++) {
5853 hda_nid_t pin = spec->multi_io[i].pin;
5854 struct nid_path *path;
5855 path = get_multiio_path(codec, i);
5858 if (!spec->multi_io[i].ctl_in)
5859 spec->multi_io[i].ctl_in =
5860 snd_hda_codec_get_pin_target(codec, pin);
5861 snd_hda_activate_path(codec, path, path->active,
5862 aamix_default(spec));
5866 static void init_aamix_paths(struct hda_codec *codec)
5868 struct hda_gen_spec *spec = codec->spec;
5870 if (!spec->have_aamix_ctl)
5872 if (!has_aamix_out_paths(spec))
5874 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5875 spec->aamix_out_paths[0],
5876 spec->autocfg.line_out_type);
5877 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5878 spec->aamix_out_paths[1],
5880 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5881 spec->aamix_out_paths[2],
5882 AUTO_PIN_SPEAKER_OUT);
5885 /* set up input pins and loopback paths */
5886 static void init_analog_input(struct hda_codec *codec)
5888 struct hda_gen_spec *spec = codec->spec;
5889 struct auto_pin_cfg *cfg = &spec->autocfg;
5892 for (i = 0; i < cfg->num_inputs; i++) {
5893 hda_nid_t nid = cfg->inputs[i].pin;
5894 if (is_input_pin(codec, nid))
5895 restore_pin_ctl(codec, nid);
5897 /* init loopback inputs */
5898 if (spec->mixer_nid) {
5899 resume_path_from_idx(codec, spec->loopback_paths[i]);
5900 resume_path_from_idx(codec, spec->loopback_merge_path);
5905 /* initialize ADC paths */
5906 static void init_input_src(struct hda_codec *codec)
5908 struct hda_gen_spec *spec = codec->spec;
5909 struct hda_input_mux *imux = &spec->input_mux;
5910 struct nid_path *path;
5913 if (spec->dyn_adc_switch)
5916 nums = spec->num_adc_nids;
5918 for (c = 0; c < nums; c++) {
5919 for (i = 0; i < imux->num_items; i++) {
5920 path = get_input_path(codec, c, i);
5922 bool active = path->active;
5923 if (i == spec->cur_mux[c])
5925 snd_hda_activate_path(codec, path, active, false);
5929 update_hp_mic(codec, c, true);
5932 if (spec->cap_sync_hook)
5933 spec->cap_sync_hook(codec, NULL, NULL);
5936 /* set right pin controls for digital I/O */
5937 static void init_digital(struct hda_codec *codec)
5939 struct hda_gen_spec *spec = codec->spec;
5943 for (i = 0; i < spec->autocfg.dig_outs; i++)
5944 set_output_and_unmute(codec, spec->digout_paths[i]);
5945 pin = spec->autocfg.dig_in_pin;
5947 restore_pin_ctl(codec, pin);
5948 resume_path_from_idx(codec, spec->digin_path);
5952 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5953 * invalid unsol tags by some reason
5955 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5957 const struct hda_pincfg *pin;
5960 snd_array_for_each(&codec->init_pins, i, pin) {
5961 hda_nid_t nid = pin->nid;
5962 if (is_jack_detectable(codec, nid) &&
5963 !snd_hda_jack_tbl_get(codec, nid))
5964 snd_hda_codec_write_cache(codec, nid, 0,
5965 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5970 * snd_hda_gen_init - initialize the generic spec
5971 * @codec: the HDA codec
5973 * This can be put as patch_ops init function.
5975 int snd_hda_gen_init(struct hda_codec *codec)
5977 struct hda_gen_spec *spec = codec->spec;
5979 if (spec->init_hook)
5980 spec->init_hook(codec);
5982 if (!spec->skip_verbs)
5983 snd_hda_apply_verbs(codec);
5985 init_multi_out(codec);
5986 init_extra_out(codec);
5987 init_multi_io(codec);
5988 init_aamix_paths(codec);
5989 init_analog_input(codec);
5990 init_input_src(codec);
5991 init_digital(codec);
5993 clear_unsol_on_unused_pins(codec);
5995 sync_all_pin_power_ctls(codec);
5997 /* call init functions of standard auto-mute helpers */
5998 update_automute_all(codec);
6000 snd_hda_regmap_sync(codec);
6002 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
6003 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
6005 hda_call_check_power_status(codec, 0x01);
6008 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
6011 * snd_hda_gen_free - free the generic spec
6012 * @codec: the HDA codec
6014 * This can be put as patch_ops free function.
6016 void snd_hda_gen_free(struct hda_codec *codec)
6018 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
6019 snd_hda_gen_spec_free(codec->spec);
6023 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
6027 * snd_hda_gen_check_power_status - check the loopback power save state
6028 * @codec: the HDA codec
6029 * @nid: NID to inspect
6031 * This can be put as patch_ops check_power_status function.
6033 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
6035 struct hda_gen_spec *spec = codec->spec;
6036 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
6038 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
6043 * the generic codec support
6046 static const struct hda_codec_ops generic_patch_ops = {
6047 .build_controls = snd_hda_gen_build_controls,
6048 .build_pcms = snd_hda_gen_build_pcms,
6049 .init = snd_hda_gen_init,
6050 .free = snd_hda_gen_free,
6051 .unsol_event = snd_hda_jack_unsol_event,
6053 .check_power_status = snd_hda_gen_check_power_status,
6058 * snd_hda_parse_generic_codec - Generic codec parser
6059 * @codec: the HDA codec
6061 static int snd_hda_parse_generic_codec(struct hda_codec *codec)
6063 struct hda_gen_spec *spec;
6066 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
6069 snd_hda_gen_spec_init(spec);
6072 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
6076 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
6080 codec->patch_ops = generic_patch_ops;
6084 snd_hda_gen_free(codec);
6088 static const struct hda_device_id snd_hda_id_generic[] = {
6089 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
6092 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
6094 static struct hda_codec_driver generic_driver = {
6095 .id = snd_hda_id_generic,
6098 module_hda_codec_driver(generic_driver);
6100 MODULE_LICENSE("GPL");
6101 MODULE_DESCRIPTION("Generic HD-audio codec parser");