1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Universal Interface for Intel High Definition Audio Codec
5 * Generic widget tree parser
7 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/export.h>
13 #include <linux/sort.h>
14 #include <linux/delay.h>
15 #include <linux/ctype.h>
16 #include <linux/string.h>
17 #include <linux/bitops.h>
18 #include <linux/module.h>
19 #include <linux/leds.h>
20 #include <sound/core.h>
21 #include <sound/jack.h>
22 #include <sound/tlv.h>
23 #include <sound/hda_codec.h>
24 #include "hda_local.h"
25 #include "hda_auto_parser.h"
28 #include "hda_generic.h"
32 * snd_hda_gen_spec_init - initialize hda_gen_spec struct
33 * @spec: hda_gen_spec object to initialize
35 * Initialize the given hda_gen_spec object.
37 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
39 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
40 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
41 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
42 mutex_init(&spec->pcm_mutex);
45 EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
48 * snd_hda_gen_add_kctl - Add a new kctl_new struct from the template
49 * @spec: hda_gen_spec object
50 * @name: name string to override the template, NULL if unchanged
51 * @temp: template for the new kctl
53 * Add a new kctl (actually snd_kcontrol_new to be instantiated later)
54 * element based on the given snd_kcontrol_new template @temp and the
55 * name string @name to the list in @spec.
56 * Returns the newly created object or NULL as error.
58 struct snd_kcontrol_new *
59 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
60 const struct snd_kcontrol_new *temp)
62 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
67 knew->name = kstrdup(name, GFP_KERNEL);
69 knew->name = kstrdup(knew->name, GFP_KERNEL);
74 EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
76 static void free_kctls(struct hda_gen_spec *spec)
78 if (spec->kctls.list) {
79 struct snd_kcontrol_new *kctl = spec->kctls.list;
81 for (i = 0; i < spec->kctls.used; i++)
84 snd_array_free(&spec->kctls);
87 static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
92 snd_array_free(&spec->paths);
93 snd_array_free(&spec->loopback_list);
94 #ifdef CONFIG_SND_HDA_GENERIC_LEDS
95 if (spec->led_cdevs[LED_AUDIO_MUTE])
96 led_classdev_unregister(spec->led_cdevs[LED_AUDIO_MUTE]);
97 if (spec->led_cdevs[LED_AUDIO_MICMUTE])
98 led_classdev_unregister(spec->led_cdevs[LED_AUDIO_MICMUTE]);
105 static void parse_user_hints(struct hda_codec *codec)
107 struct hda_gen_spec *spec = codec->spec;
110 val = snd_hda_get_bool_hint(codec, "jack_detect");
112 codec->no_jack_detect = !val;
113 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
115 codec->inv_jack_detect = !!val;
116 val = snd_hda_get_bool_hint(codec, "trigger_sense");
118 codec->no_trigger_sense = !val;
119 val = snd_hda_get_bool_hint(codec, "inv_eapd");
121 codec->inv_eapd = !!val;
122 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
124 codec->pcm_format_first = !!val;
125 val = snd_hda_get_bool_hint(codec, "sticky_stream");
127 codec->no_sticky_stream = !val;
128 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
130 codec->spdif_status_reset = !!val;
131 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
133 codec->pin_amp_workaround = !!val;
134 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
136 codec->single_adc_amp = !!val;
137 val = snd_hda_get_bool_hint(codec, "power_save_node");
139 codec->power_save_node = !!val;
141 val = snd_hda_get_bool_hint(codec, "auto_mute");
143 spec->suppress_auto_mute = !val;
144 val = snd_hda_get_bool_hint(codec, "auto_mic");
146 spec->suppress_auto_mic = !val;
147 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
149 spec->line_in_auto_switch = !!val;
150 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
152 spec->auto_mute_via_amp = !!val;
153 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
155 spec->need_dac_fix = !!val;
156 val = snd_hda_get_bool_hint(codec, "primary_hp");
158 spec->no_primary_hp = !val;
159 val = snd_hda_get_bool_hint(codec, "multi_io");
161 spec->no_multi_io = !val;
162 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
164 spec->multi_cap_vol = !!val;
165 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
167 spec->inv_dmic_split = !!val;
168 val = snd_hda_get_bool_hint(codec, "indep_hp");
170 spec->indep_hp = !!val;
171 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
173 spec->add_stereo_mix_input = !!val;
174 /* the following two are just for compatibility */
175 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
177 spec->add_jack_modes = !!val;
178 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
180 spec->add_jack_modes = !!val;
181 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
183 spec->add_jack_modes = !!val;
184 val = snd_hda_get_bool_hint(codec, "power_down_unused");
186 spec->power_down_unused = !!val;
187 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
189 spec->hp_mic = !!val;
190 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
192 spec->suppress_hp_mic_detect = !val;
193 val = snd_hda_get_bool_hint(codec, "vmaster");
195 spec->suppress_vmaster = !val;
197 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
198 spec->mixer_nid = val;
202 * pin control value accesses
205 #define update_pin_ctl(codec, pin, val) \
206 snd_hda_codec_write_cache(codec, pin, 0, \
207 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
209 /* restore the pinctl based on the cached value */
210 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
212 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
215 /* set the pinctl target value and write it if requested */
216 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
217 unsigned int val, bool do_write)
221 val = snd_hda_correct_pin_ctl(codec, pin, val);
222 snd_hda_codec_set_pin_target(codec, pin, val);
224 update_pin_ctl(codec, pin, val);
227 /* set pinctl target values for all given pins */
228 static void set_pin_targets(struct hda_codec *codec, int num_pins,
229 hda_nid_t *pins, unsigned int val)
232 for (i = 0; i < num_pins; i++)
233 set_pin_target(codec, pins[i], val, false);
240 /* return the position of NID in the list, or -1 if not found */
241 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
244 for (i = 0; i < nums; i++)
250 /* return true if the given NID is contained in the path */
251 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
253 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
256 static struct nid_path *get_nid_path(struct hda_codec *codec,
257 hda_nid_t from_nid, hda_nid_t to_nid,
260 struct hda_gen_spec *spec = codec->spec;
261 struct nid_path *path;
264 snd_array_for_each(&spec->paths, i, path) {
265 if (path->depth <= 0)
267 if ((!from_nid || path->path[0] == from_nid) &&
268 (!to_nid || path->path[path->depth - 1] == to_nid)) {
270 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
271 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
279 * snd_hda_get_path_idx - get the index number corresponding to the path
281 * @codec: the HDA codec
282 * @path: nid_path object
284 * The returned index starts from 1, i.e. the actual array index with offset 1,
285 * and zero is handled as an invalid path
287 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
289 struct hda_gen_spec *spec = codec->spec;
290 struct nid_path *array = spec->paths.list;
293 if (!spec->paths.used)
296 if (idx < 0 || idx >= spec->paths.used)
300 EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
303 * snd_hda_get_path_from_idx - get the path instance corresponding to the
305 * @codec: the HDA codec
306 * @idx: the path index
308 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
310 struct hda_gen_spec *spec = codec->spec;
312 if (idx <= 0 || idx > spec->paths.used)
314 return snd_array_elem(&spec->paths, idx - 1);
316 EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
318 /* check whether the given DAC is already found in any existing paths */
319 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
321 struct hda_gen_spec *spec = codec->spec;
322 const struct nid_path *path;
325 snd_array_for_each(&spec->paths, i, path) {
326 if (path->path[0] == nid)
332 /* check whether the given two widgets can be connected */
333 static bool is_reachable_path(struct hda_codec *codec,
334 hda_nid_t from_nid, hda_nid_t to_nid)
336 if (!from_nid || !to_nid)
338 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
341 /* nid, dir and idx */
342 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
344 /* check whether the given ctl is already assigned in any path elements */
345 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
347 struct hda_gen_spec *spec = codec->spec;
348 const struct nid_path *path;
351 val &= AMP_VAL_COMPARE_MASK;
352 snd_array_for_each(&spec->paths, i, path) {
353 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
359 /* check whether a control with the given (nid, dir, idx) was assigned */
360 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
361 int dir, int idx, int type)
363 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
364 return is_ctl_used(codec, val, type);
367 static void print_nid_path(struct hda_codec *codec,
368 const char *pfx, struct nid_path *path)
375 for (i = 0; i < path->depth; i++)
376 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
377 pos != buf ? ":" : "",
380 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
383 /* called recursively */
384 static bool __parse_nid_path(struct hda_codec *codec,
385 hda_nid_t from_nid, hda_nid_t to_nid,
386 int anchor_nid, struct nid_path *path,
389 const hda_nid_t *conn;
392 if (to_nid == anchor_nid)
393 anchor_nid = 0; /* anchor passed */
394 else if (to_nid == (hda_nid_t)(-anchor_nid))
395 return false; /* hit the exclusive nid */
397 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
398 for (i = 0; i < nums; i++) {
399 if (conn[i] != from_nid) {
400 /* special case: when from_nid is 0,
401 * try to find an empty DAC
404 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
405 is_dac_already_used(codec, conn[i]))
408 /* anchor is not requested or already passed? */
412 if (depth >= MAX_NID_PATH_DEPTH)
414 for (i = 0; i < nums; i++) {
416 type = get_wcaps_type(get_wcaps(codec, conn[i]));
417 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
420 if (__parse_nid_path(codec, from_nid, conn[i],
421 anchor_nid, path, depth + 1))
427 path->path[path->depth] = conn[i];
428 path->idx[path->depth + 1] = i;
429 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
430 path->multi[path->depth + 1] = 1;
436 * snd_hda_parse_nid_path - parse the widget path from the given nid to
438 * @codec: the HDA codec
439 * @from_nid: the NID where the path start from
440 * @to_nid: the NID where the path ends at
441 * @anchor_nid: the anchor indication
442 * @path: the path object to store the result
444 * Returns true if a matching path is found.
446 * The parsing behavior depends on parameters:
447 * when @from_nid is 0, try to find an empty DAC;
448 * when @anchor_nid is set to a positive value, only paths through the widget
449 * with the given value are evaluated.
450 * when @anchor_nid is set to a negative value, paths through the widget
451 * with the negative of given value are excluded, only other paths are chosen.
452 * when @anchor_nid is zero, no special handling about path selection.
454 static bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
455 hda_nid_t to_nid, int anchor_nid,
456 struct nid_path *path)
458 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
459 path->path[path->depth] = to_nid;
467 * snd_hda_add_new_path - parse the path between the given NIDs and
468 * add to the path list
469 * @codec: the HDA codec
470 * @from_nid: the NID where the path start from
471 * @to_nid: the NID where the path ends at
472 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
474 * If no valid path is found, returns NULL.
477 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
478 hda_nid_t to_nid, int anchor_nid)
480 struct hda_gen_spec *spec = codec->spec;
481 struct nid_path *path;
483 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
486 /* check whether the path has been already added */
487 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
491 path = snd_array_new(&spec->paths);
494 memset(path, 0, sizeof(*path));
495 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
501 EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
503 /* clear the given path as invalid so that it won't be picked up later */
504 static void invalidate_nid_path(struct hda_codec *codec, int idx)
506 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
509 memset(path, 0, sizeof(*path));
512 /* return a DAC if paired to the given pin by codec driver */
513 static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
515 struct hda_gen_spec *spec = codec->spec;
516 const hda_nid_t *list = spec->preferred_dacs;
520 for (; *list; list += 2)
526 /* look for an empty DAC slot */
527 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
530 struct hda_gen_spec *spec = codec->spec;
534 for (i = 0; i < spec->num_all_dacs; i++) {
535 hda_nid_t nid = spec->all_dacs[i];
536 if (!nid || is_dac_already_used(codec, nid))
538 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
539 if (is_digital != cap_digital)
541 if (is_reachable_path(codec, nid, pin))
547 /* replace the channels in the composed amp value with the given number */
548 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
550 val &= ~(0x3U << 16);
555 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
556 hda_nid_t nid2, int dir)
558 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
559 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
560 return (query_amp_caps(codec, nid1, dir) ==
561 query_amp_caps(codec, nid2, dir));
564 /* look for a widget suitable for assigning a mute switch in the path */
565 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
566 struct nid_path *path)
570 for (i = path->depth - 1; i >= 0; i--) {
571 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
572 return path->path[i];
573 if (i != path->depth - 1 && i != 0 &&
574 nid_has_mute(codec, path->path[i], HDA_INPUT))
575 return path->path[i];
580 /* look for a widget suitable for assigning a volume ctl in the path */
581 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
582 struct nid_path *path)
584 struct hda_gen_spec *spec = codec->spec;
587 for (i = path->depth - 1; i >= 0; i--) {
588 hda_nid_t nid = path->path[i];
589 if ((spec->out_vol_mask >> nid) & 1)
591 if (nid_has_volume(codec, nid, HDA_OUTPUT))
598 * path activation / deactivation
601 /* can have the amp-in capability? */
602 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
604 hda_nid_t nid = path->path[idx];
605 unsigned int caps = get_wcaps(codec, nid);
606 unsigned int type = get_wcaps_type(caps);
608 if (!(caps & AC_WCAP_IN_AMP))
610 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
615 /* can have the amp-out capability? */
616 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
618 hda_nid_t nid = path->path[idx];
619 unsigned int caps = get_wcaps(codec, nid);
620 unsigned int type = get_wcaps_type(caps);
622 if (!(caps & AC_WCAP_OUT_AMP))
624 if (type == AC_WID_PIN && !idx) /* only for output pins */
629 /* check whether the given (nid,dir,idx) is active */
630 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
631 unsigned int dir, unsigned int idx)
633 struct hda_gen_spec *spec = codec->spec;
634 int type = get_wcaps_type(get_wcaps(codec, nid));
635 const struct nid_path *path;
638 if (nid == codec->core.afg)
641 snd_array_for_each(&spec->paths, n, path) {
644 if (codec->power_save_node) {
645 if (!path->stream_enabled)
647 /* ignore unplugged paths except for DAC/ADC */
648 if (!(path->pin_enabled || path->pin_fixed) &&
649 type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
652 for (i = 0; i < path->depth; i++) {
653 if (path->path[i] == nid) {
654 if (dir == HDA_OUTPUT || idx == -1 ||
664 /* check whether the NID is referred by any active paths */
665 #define is_active_nid_for_any(codec, nid) \
666 is_active_nid(codec, nid, HDA_OUTPUT, -1)
668 /* get the default amp value for the target state */
669 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
670 int dir, unsigned int caps, bool enable)
672 unsigned int val = 0;
674 if (caps & AC_AMPCAP_NUM_STEPS) {
677 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
679 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
686 /* is this a stereo widget or a stereo-to-mono mix? */
687 static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
689 unsigned int wcaps = get_wcaps(codec, nid);
692 if (wcaps & AC_WCAP_STEREO)
694 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
696 if (snd_hda_get_num_conns(codec, nid) != 1)
698 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
700 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
703 /* initialize the amp value (only at the first time) */
704 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
706 unsigned int caps = query_amp_caps(codec, nid, dir);
707 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
709 if (is_stereo_amps(codec, nid, dir))
710 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
712 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
715 /* update the amp, doing in stereo or mono depending on NID */
716 static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
717 unsigned int mask, unsigned int val)
719 if (is_stereo_amps(codec, nid, dir))
720 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
723 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
727 /* calculate amp value mask we can modify;
728 * if the given amp is controlled by mixers, don't touch it
730 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
731 hda_nid_t nid, int dir, int idx,
734 unsigned int mask = 0xff;
736 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
737 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
740 if (caps & AC_AMPCAP_NUM_STEPS) {
741 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
742 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
748 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
749 int idx, int idx_to_check, bool enable)
752 unsigned int mask, val;
754 caps = query_amp_caps(codec, nid, dir);
755 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
756 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
761 update_amp(codec, nid, dir, idx, mask, val);
764 static void check_and_activate_amp(struct hda_codec *codec, hda_nid_t nid,
765 int dir, int idx, int idx_to_check,
768 /* check whether the given amp is still used by others */
769 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
771 activate_amp(codec, nid, dir, idx, idx_to_check, enable);
774 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
777 hda_nid_t nid = path->path[i];
778 init_amp(codec, nid, HDA_OUTPUT, 0);
779 check_and_activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
782 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
783 int i, bool enable, bool add_aamix)
785 struct hda_gen_spec *spec = codec->spec;
786 const hda_nid_t *conn;
789 hda_nid_t nid = path->path[i];
791 nums = snd_hda_get_conn_list(codec, nid, &conn);
794 type = get_wcaps_type(get_wcaps(codec, nid));
795 if (type == AC_WID_PIN ||
796 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
802 for (n = 0; n < nums; n++)
803 init_amp(codec, nid, HDA_INPUT, n);
805 /* here is a little bit tricky in comparison with activate_amp_out();
806 * when aa-mixer is available, we need to enable the path as well
808 for (n = 0; n < nums; n++) {
810 if (conn[n] != spec->mixer_merge_nid)
812 /* when aamix is disabled, force to off */
814 activate_amp(codec, nid, HDA_INPUT, n, n, false);
818 check_and_activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
822 /* sync power of each widget in the given path */
823 static hda_nid_t path_power_update(struct hda_codec *codec,
824 struct nid_path *path,
825 bool allow_powerdown)
827 hda_nid_t nid, changed = 0;
830 for (i = 0; i < path->depth; i++) {
832 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
834 if (nid == codec->core.afg)
836 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
840 power = snd_hda_codec_read(codec, nid, 0,
841 AC_VERB_GET_POWER_STATE, 0);
842 if (power != (state | (state << 4))) {
843 snd_hda_codec_write(codec, nid, 0,
844 AC_VERB_SET_POWER_STATE, state);
846 /* all known codecs seem to be capable to handl
847 * widgets state even in D3, so far.
848 * if any new codecs need to restore the widget
849 * states after D0 transition, call the function
853 if (state == AC_PWRST_D0)
854 snd_hdac_regmap_sync_node(&codec->core, nid);
861 /* do sync with the last power state change */
862 static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
866 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
871 * snd_hda_activate_path - activate or deactivate the given path
872 * @codec: the HDA codec
873 * @path: the path to activate/deactivate
874 * @enable: flag to activate or not
875 * @add_aamix: enable the input from aamix NID
877 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
879 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
880 bool enable, bool add_aamix)
882 struct hda_gen_spec *spec = codec->spec;
885 path->active = enable;
887 /* make sure the widget is powered up */
888 if (enable && (spec->power_down_unused || codec->power_save_node))
889 path_power_update(codec, path, codec->power_save_node);
891 for (i = path->depth - 1; i >= 0; i--) {
892 hda_nid_t nid = path->path[i];
894 if (enable && path->multi[i])
895 snd_hda_codec_write_cache(codec, nid, 0,
896 AC_VERB_SET_CONNECT_SEL,
898 if (has_amp_in(codec, path, i))
899 activate_amp_in(codec, path, i, enable, add_aamix);
900 if (has_amp_out(codec, path, i))
901 activate_amp_out(codec, path, i, enable);
904 EXPORT_SYMBOL_GPL(snd_hda_activate_path);
906 /* if the given path is inactive, put widgets into D3 (only if suitable) */
907 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
909 struct hda_gen_spec *spec = codec->spec;
911 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
913 sync_power_state_change(codec, path_power_update(codec, path, true));
916 /* turn on/off EAPD on the given pin */
917 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
919 struct hda_gen_spec *spec = codec->spec;
920 if (spec->own_eapd_ctl ||
921 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
923 if (spec->keep_eapd_on && !enable)
927 snd_hda_codec_write_cache(codec, pin, 0,
928 AC_VERB_SET_EAPD_BTLENABLE,
929 enable ? 0x02 : 0x00);
932 /* re-initialize the path specified by the given path index */
933 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
935 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
937 snd_hda_activate_path(codec, path, path->active, false);
942 * Helper functions for creating mixer ctl elements
945 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
946 struct snd_ctl_elem_value *ucontrol);
947 static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
948 struct snd_ctl_elem_value *ucontrol);
949 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
950 struct snd_ctl_elem_value *ucontrol);
957 static const struct snd_kcontrol_new control_templates[] = {
958 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
959 /* only the put callback is replaced for handling the special mute */
961 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
962 .subdevice = HDA_SUBDEV_AMP_FLAG,
963 .info = snd_hda_mixer_amp_switch_info,
964 .get = snd_hda_mixer_amp_switch_get,
965 .put = hda_gen_mixer_mute_put, /* replaced */
966 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
969 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
970 .info = snd_hda_mixer_amp_switch_info,
971 .get = hda_gen_bind_mute_get,
972 .put = hda_gen_bind_mute_put, /* replaced */
973 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
977 /* add dynamic controls from template */
978 static struct snd_kcontrol_new *
979 add_control(struct hda_gen_spec *spec, int type, const char *name,
980 int cidx, unsigned long val)
982 struct snd_kcontrol_new *knew;
984 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
988 if (get_amp_nid_(val))
989 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
990 if (knew->access == 0)
991 knew->access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
992 knew->private_value = val;
996 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
997 const char *pfx, const char *dir,
998 const char *sfx, int cidx, unsigned long val)
1000 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1001 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
1002 if (!add_control(spec, type, name, cidx, val))
1007 #define add_pb_vol_ctrl(spec, type, pfx, val) \
1008 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1009 #define add_pb_sw_ctrl(spec, type, pfx, val) \
1010 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1011 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1012 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1013 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1014 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1016 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1017 unsigned int chs, struct nid_path *path)
1022 val = path->ctls[NID_PATH_VOL_CTL];
1025 val = amp_val_replace_channels(val, chs);
1026 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1029 /* return the channel bits suitable for the given path->ctls[] */
1030 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1033 int chs = 1; /* mono (left only) */
1035 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1036 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1037 chs = 3; /* stereo */
1042 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1043 struct nid_path *path)
1045 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1046 return add_vol_ctl(codec, pfx, cidx, chs, path);
1049 /* create a mute-switch for the given mixer widget;
1050 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1052 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1053 unsigned int chs, struct nid_path *path)
1056 int type = HDA_CTL_WIDGET_MUTE;
1060 val = path->ctls[NID_PATH_MUTE_CTL];
1063 val = amp_val_replace_channels(val, chs);
1064 if (get_amp_direction_(val) == HDA_INPUT) {
1065 hda_nid_t nid = get_amp_nid_(val);
1066 int nums = snd_hda_get_num_conns(codec, nid);
1068 type = HDA_CTL_BIND_MUTE;
1072 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1075 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1076 int cidx, struct nid_path *path)
1078 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1079 return add_sw_ctl(codec, pfx, cidx, chs, path);
1082 /* playback mute control with the software mute bit check */
1083 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1084 struct snd_ctl_elem_value *ucontrol)
1086 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1087 struct hda_gen_spec *spec = codec->spec;
1089 if (spec->auto_mute_via_amp) {
1090 hda_nid_t nid = get_amp_nid(kcontrol);
1091 bool enabled = !((spec->mute_bits >> nid) & 1);
1092 ucontrol->value.integer.value[0] &= enabled;
1093 ucontrol->value.integer.value[1] &= enabled;
1097 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1098 struct snd_ctl_elem_value *ucontrol)
1100 sync_auto_mute_bits(kcontrol, ucontrol);
1101 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1105 * Bound mute controls
1107 #define AMP_VAL_IDX_SHIFT 19
1108 #define AMP_VAL_IDX_MASK (0x0f<<19)
1110 static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
1111 struct snd_ctl_elem_value *ucontrol)
1113 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1117 mutex_lock(&codec->control_mutex);
1118 pval = kcontrol->private_value;
1119 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1120 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1121 kcontrol->private_value = pval;
1122 mutex_unlock(&codec->control_mutex);
1126 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1127 struct snd_ctl_elem_value *ucontrol)
1129 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1131 int i, indices, err = 0, change = 0;
1133 sync_auto_mute_bits(kcontrol, ucontrol);
1135 mutex_lock(&codec->control_mutex);
1136 pval = kcontrol->private_value;
1137 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1138 for (i = 0; i < indices; i++) {
1139 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1140 (i << AMP_VAL_IDX_SHIFT);
1141 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1146 kcontrol->private_value = pval;
1147 mutex_unlock(&codec->control_mutex);
1148 return err < 0 ? err : change;
1151 /* any ctl assigned to the path with the given index? */
1152 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1154 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1155 return path && path->ctls[ctl_type];
1158 static const char * const channel_name[] = {
1159 "Front", "Surround", "CLFE", "Side", "Back",
1162 /* give some appropriate ctl name prefix for the given line out channel */
1163 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1164 int *index, int ctl_type)
1166 struct hda_gen_spec *spec = codec->spec;
1167 struct auto_pin_cfg *cfg = &spec->autocfg;
1170 if (cfg->line_outs == 1 && !spec->multi_ios &&
1171 !codec->force_pin_prefix &&
1172 !cfg->hp_outs && !cfg->speaker_outs)
1173 return spec->vmaster_mute.hook ? "PCM" : "Master";
1175 /* if there is really a single DAC used in the whole output paths,
1176 * use it master (or "PCM" if a vmaster hook is present)
1178 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1179 !codec->force_pin_prefix &&
1180 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1181 return spec->vmaster_mute.hook ? "PCM" : "Master";
1183 /* multi-io channels */
1184 if (ch >= cfg->line_outs)
1187 switch (cfg->line_out_type) {
1188 case AUTO_PIN_SPEAKER_OUT:
1189 /* if the primary channel vol/mute is shared with HP volume,
1190 * don't name it as Speaker
1192 if (!ch && cfg->hp_outs &&
1193 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1195 if (cfg->line_outs == 1)
1197 if (cfg->line_outs == 2)
1198 return ch ? "Bass Speaker" : "Speaker";
1200 case AUTO_PIN_HP_OUT:
1201 /* if the primary channel vol/mute is shared with spk volume,
1202 * don't name it as Headphone
1204 if (!ch && cfg->speaker_outs &&
1205 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1207 /* for multi-io case, only the primary out */
1208 if (ch && spec->multi_ios)
1212 case AUTO_PIN_LINE_OUT:
1213 /* This deals with the case where one HP or one Speaker or
1214 * one HP + one Speaker need to share the DAC with LO
1217 bool hp_lo_shared = false, spk_lo_shared = false;
1219 if (cfg->speaker_outs)
1220 spk_lo_shared = !path_has_mixer(codec,
1221 spec->speaker_paths[0], ctl_type);
1223 hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1224 if (hp_lo_shared && spk_lo_shared)
1225 return spec->vmaster_mute.hook ? "PCM" : "Master";
1227 return "Headphone+LO";
1229 return "Speaker+LO";
1233 /* for a single channel output, we don't have to name the channel */
1234 if (cfg->line_outs == 1 && !spec->multi_ios)
1238 if (ch >= ARRAY_SIZE(channel_name)) {
1243 return channel_name[ch];
1247 * Parse output paths
1250 /* badness definition */
1252 /* No primary DAC is found for the main output */
1253 BAD_NO_PRIMARY_DAC = 0x10000,
1254 /* No DAC is found for the extra output */
1255 BAD_NO_DAC = 0x4000,
1256 /* No possible multi-ios */
1257 BAD_MULTI_IO = 0x120,
1258 /* No individual DAC for extra output */
1259 BAD_NO_EXTRA_DAC = 0x102,
1260 /* No individual DAC for extra surrounds */
1261 BAD_NO_EXTRA_SURR_DAC = 0x101,
1262 /* Primary DAC shared with main surrounds */
1263 BAD_SHARED_SURROUND = 0x100,
1264 /* No independent HP possible */
1265 BAD_NO_INDEP_HP = 0x10,
1266 /* Primary DAC shared with main CLFE */
1267 BAD_SHARED_CLFE = 0x10,
1268 /* Primary DAC shared with extra surrounds */
1269 BAD_SHARED_EXTRA_SURROUND = 0x10,
1270 /* Volume widget is shared */
1271 BAD_SHARED_VOL = 0x10,
1274 /* look for widgets in the given path which are appropriate for
1275 * volume and mute controls, and assign the values to ctls[].
1277 * When no appropriate widget is found in the path, the badness value
1278 * is incremented depending on the situation. The function returns the
1279 * total badness for both volume and mute controls.
1281 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1283 struct hda_gen_spec *spec = codec->spec;
1289 return BAD_SHARED_VOL * 2;
1291 if (path->ctls[NID_PATH_VOL_CTL] ||
1292 path->ctls[NID_PATH_MUTE_CTL])
1293 return 0; /* already evaluated */
1295 nid = look_for_out_vol_nid(codec, path);
1297 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1298 if (spec->dac_min_mute)
1299 val |= HDA_AMP_VAL_MIN_MUTE;
1300 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1301 badness += BAD_SHARED_VOL;
1303 path->ctls[NID_PATH_VOL_CTL] = val;
1305 badness += BAD_SHARED_VOL;
1306 nid = look_for_out_mute_nid(codec, path);
1308 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1309 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1310 nid_has_mute(codec, nid, HDA_OUTPUT))
1311 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1313 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1314 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1315 badness += BAD_SHARED_VOL;
1317 path->ctls[NID_PATH_MUTE_CTL] = val;
1319 badness += BAD_SHARED_VOL;
1323 const struct badness_table hda_main_out_badness = {
1324 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1325 .no_dac = BAD_NO_DAC,
1326 .shared_primary = BAD_NO_PRIMARY_DAC,
1327 .shared_surr = BAD_SHARED_SURROUND,
1328 .shared_clfe = BAD_SHARED_CLFE,
1329 .shared_surr_main = BAD_SHARED_SURROUND,
1331 EXPORT_SYMBOL_GPL(hda_main_out_badness);
1333 const struct badness_table hda_extra_out_badness = {
1334 .no_primary_dac = BAD_NO_DAC,
1335 .no_dac = BAD_NO_DAC,
1336 .shared_primary = BAD_NO_EXTRA_DAC,
1337 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1338 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1339 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1341 EXPORT_SYMBOL_GPL(hda_extra_out_badness);
1343 /* get the DAC of the primary output corresponding to the given array index */
1344 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1346 struct hda_gen_spec *spec = codec->spec;
1347 struct auto_pin_cfg *cfg = &spec->autocfg;
1349 if (cfg->line_outs > idx)
1350 return spec->private_dac_nids[idx];
1351 idx -= cfg->line_outs;
1352 if (spec->multi_ios > idx)
1353 return spec->multi_io[idx].dac;
1357 /* return the DAC if it's reachable, otherwise zero */
1358 static inline hda_nid_t try_dac(struct hda_codec *codec,
1359 hda_nid_t dac, hda_nid_t pin)
1361 return is_reachable_path(codec, dac, pin) ? dac : 0;
1364 /* try to assign DACs to pins and return the resultant badness */
1365 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1366 const hda_nid_t *pins, hda_nid_t *dacs,
1368 const struct badness_table *bad)
1370 struct hda_gen_spec *spec = codec->spec;
1378 for (i = 0; i < num_outs; i++) {
1379 struct nid_path *path;
1380 hda_nid_t pin = pins[i];
1382 if (!spec->obey_preferred_dacs) {
1383 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1385 badness += assign_out_path_ctls(codec, path);
1390 dacs[i] = get_preferred_dac(codec, pin);
1392 if (is_dac_already_used(codec, dacs[i]))
1393 badness += bad->shared_primary;
1394 } else if (spec->obey_preferred_dacs) {
1395 badness += BAD_NO_PRIMARY_DAC;
1399 dacs[i] = look_for_dac(codec, pin, false);
1400 if (!dacs[i] && !i) {
1401 /* try to steal the DAC of surrounds for the front */
1402 for (j = 1; j < num_outs; j++) {
1403 if (is_reachable_path(codec, dacs[j], pin)) {
1406 invalidate_nid_path(codec, path_idx[j]);
1415 dac = try_dac(codec, get_primary_out(codec, i), pin);
1417 dac = try_dac(codec, dacs[0], pin);
1419 dac = try_dac(codec, get_primary_out(codec, i), pin);
1422 badness += bad->shared_primary;
1424 badness += bad->shared_surr;
1426 badness += bad->shared_clfe;
1427 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1428 dac = spec->private_dac_nids[0];
1429 badness += bad->shared_surr_main;
1431 badness += bad->no_primary_dac;
1433 badness += bad->no_dac;
1437 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1438 if (!path && !i && spec->mixer_nid) {
1439 /* try with aamix */
1440 path = snd_hda_add_new_path(codec, dac, pin, 0);
1444 badness += bad->no_dac;
1446 /* print_nid_path(codec, "output", path); */
1447 path->active = true;
1448 path_idx[i] = snd_hda_get_path_idx(codec, path);
1449 badness += assign_out_path_ctls(codec, path);
1456 /* return NID if the given pin has only a single connection to a certain DAC */
1457 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1459 struct hda_gen_spec *spec = codec->spec;
1461 hda_nid_t nid_found = 0;
1463 for (i = 0; i < spec->num_all_dacs; i++) {
1464 hda_nid_t nid = spec->all_dacs[i];
1465 if (!nid || is_dac_already_used(codec, nid))
1467 if (is_reachable_path(codec, nid, pin)) {
1476 /* check whether the given pin can be a multi-io pin */
1477 static bool can_be_multiio_pin(struct hda_codec *codec,
1478 unsigned int location, hda_nid_t nid)
1480 unsigned int defcfg, caps;
1482 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1483 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1485 if (location && get_defcfg_location(defcfg) != location)
1487 caps = snd_hda_query_pin_caps(codec, nid);
1488 if (!(caps & AC_PINCAP_OUT))
1493 /* count the number of input pins that are capable to be multi-io */
1494 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1496 struct hda_gen_spec *spec = codec->spec;
1497 struct auto_pin_cfg *cfg = &spec->autocfg;
1498 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1499 unsigned int location = get_defcfg_location(defcfg);
1503 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1504 for (i = 0; i < cfg->num_inputs; i++) {
1505 if (cfg->inputs[i].type != type)
1507 if (can_be_multiio_pin(codec, location,
1508 cfg->inputs[i].pin))
1518 * When hardwired is set, try to fill ony hardwired pins, and returns
1519 * zero if any pins are filled, non-zero if nothing found.
1520 * When hardwired is off, try to fill possible input pins, and returns
1521 * the badness value.
1523 static int fill_multi_ios(struct hda_codec *codec,
1524 hda_nid_t reference_pin,
1527 struct hda_gen_spec *spec = codec->spec;
1528 struct auto_pin_cfg *cfg = &spec->autocfg;
1529 int type, i, j, num_pins, old_pins;
1530 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1531 unsigned int location = get_defcfg_location(defcfg);
1533 struct nid_path *path;
1535 old_pins = spec->multi_ios;
1539 num_pins = count_multiio_pins(codec, reference_pin);
1543 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1544 for (i = 0; i < cfg->num_inputs; i++) {
1545 hda_nid_t nid = cfg->inputs[i].pin;
1548 if (cfg->inputs[i].type != type)
1550 if (!can_be_multiio_pin(codec, location, nid))
1552 for (j = 0; j < spec->multi_ios; j++) {
1553 if (nid == spec->multi_io[j].pin)
1556 if (j < spec->multi_ios)
1560 dac = get_dac_if_single(codec, nid);
1562 dac = look_for_dac(codec, nid, false);
1567 path = snd_hda_add_new_path(codec, dac, nid,
1573 /* print_nid_path(codec, "multiio", path); */
1574 spec->multi_io[spec->multi_ios].pin = nid;
1575 spec->multi_io[spec->multi_ios].dac = dac;
1576 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1577 snd_hda_get_path_idx(codec, path);
1579 if (spec->multi_ios >= 2)
1585 badness = BAD_MULTI_IO;
1586 if (old_pins == spec->multi_ios) {
1588 return 1; /* nothing found */
1590 return badness; /* no badness if nothing found */
1592 if (!hardwired && spec->multi_ios < 2) {
1593 /* cancel newly assigned paths */
1594 spec->paths.used -= spec->multi_ios - old_pins;
1595 spec->multi_ios = old_pins;
1599 /* assign volume and mute controls */
1600 for (i = old_pins; i < spec->multi_ios; i++) {
1601 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1602 badness += assign_out_path_ctls(codec, path);
1608 /* map DACs for all pins in the list if they are single connections */
1609 static bool map_singles(struct hda_codec *codec, int outs,
1610 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1612 struct hda_gen_spec *spec = codec->spec;
1615 for (i = 0; i < outs; i++) {
1616 struct nid_path *path;
1620 dac = get_dac_if_single(codec, pins[i]);
1623 path = snd_hda_add_new_path(codec, dac, pins[i],
1625 if (!path && !i && spec->mixer_nid)
1626 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1630 /* print_nid_path(codec, "output", path); */
1631 path->active = true;
1632 path_idx[i] = snd_hda_get_path_idx(codec, path);
1638 static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1640 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1641 spec->aamix_out_paths[2];
1644 /* create a new path including aamix if available, and return its index */
1645 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1647 struct hda_gen_spec *spec = codec->spec;
1648 struct nid_path *path;
1649 hda_nid_t path_dac, dac, pin;
1651 path = snd_hda_get_path_from_idx(codec, path_idx);
1652 if (!path || !path->depth ||
1653 is_nid_contained(path, spec->mixer_nid))
1655 path_dac = path->path[0];
1656 dac = spec->private_dac_nids[0];
1657 pin = path->path[path->depth - 1];
1658 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1660 if (dac != path_dac)
1662 else if (spec->multiout.hp_out_nid[0])
1663 dac = spec->multiout.hp_out_nid[0];
1664 else if (spec->multiout.extra_out_nid[0])
1665 dac = spec->multiout.extra_out_nid[0];
1669 path = snd_hda_add_new_path(codec, dac, pin,
1674 /* print_nid_path(codec, "output-aamix", path); */
1675 path->active = false; /* unused as default */
1676 path->pin_fixed = true; /* static route */
1677 return snd_hda_get_path_idx(codec, path);
1680 /* check whether the independent HP is available with the current config */
1681 static bool indep_hp_possible(struct hda_codec *codec)
1683 struct hda_gen_spec *spec = codec->spec;
1684 struct auto_pin_cfg *cfg = &spec->autocfg;
1685 struct nid_path *path;
1688 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1689 idx = spec->out_paths[0];
1691 idx = spec->hp_paths[0];
1692 path = snd_hda_get_path_from_idx(codec, idx);
1696 /* assume no path conflicts unless aamix is involved */
1697 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1700 /* check whether output paths contain aamix */
1701 for (i = 0; i < cfg->line_outs; i++) {
1702 if (spec->out_paths[i] == idx)
1704 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1705 if (path && is_nid_contained(path, spec->mixer_nid))
1708 for (i = 0; i < cfg->speaker_outs; i++) {
1709 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1710 if (path && is_nid_contained(path, spec->mixer_nid))
1717 /* fill the empty entries in the dac array for speaker/hp with the
1718 * shared dac pointed by the paths
1720 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1721 hda_nid_t *dacs, int *path_idx)
1723 struct nid_path *path;
1726 for (i = 0; i < num_outs; i++) {
1729 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1732 dacs[i] = path->path[0];
1736 /* fill in the dac_nids table from the parsed pin configuration */
1737 static int fill_and_eval_dacs(struct hda_codec *codec,
1738 bool fill_hardwired,
1739 bool fill_mio_first)
1741 struct hda_gen_spec *spec = codec->spec;
1742 struct auto_pin_cfg *cfg = &spec->autocfg;
1743 int i, err, badness;
1745 /* set num_dacs once to full for look_for_dac() */
1746 spec->multiout.num_dacs = cfg->line_outs;
1747 spec->multiout.dac_nids = spec->private_dac_nids;
1748 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1749 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1750 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1751 spec->multi_ios = 0;
1752 snd_array_free(&spec->paths);
1754 /* clear path indices */
1755 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1756 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1757 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1758 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1759 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1760 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1761 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1762 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1766 /* fill hard-wired DACs first */
1767 if (fill_hardwired) {
1770 mapped = map_singles(codec, cfg->line_outs,
1772 spec->private_dac_nids,
1774 mapped |= map_singles(codec, cfg->hp_outs,
1776 spec->multiout.hp_out_nid,
1778 mapped |= map_singles(codec, cfg->speaker_outs,
1780 spec->multiout.extra_out_nid,
1781 spec->speaker_paths);
1782 if (!spec->no_multi_io &&
1783 fill_mio_first && cfg->line_outs == 1 &&
1784 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1785 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1792 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1793 spec->private_dac_nids, spec->out_paths,
1794 spec->main_out_badness);
1796 if (!spec->no_multi_io && fill_mio_first &&
1797 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1798 /* try to fill multi-io first */
1799 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1802 /* we don't count badness at this stage yet */
1805 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1806 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1807 spec->multiout.hp_out_nid,
1809 spec->extra_out_badness);
1814 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1815 err = try_assign_dacs(codec, cfg->speaker_outs,
1817 spec->multiout.extra_out_nid,
1818 spec->speaker_paths,
1819 spec->extra_out_badness);
1824 if (!spec->no_multi_io &&
1825 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1826 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1832 if (spec->mixer_nid) {
1833 spec->aamix_out_paths[0] =
1834 check_aamix_out_path(codec, spec->out_paths[0]);
1835 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1836 spec->aamix_out_paths[1] =
1837 check_aamix_out_path(codec, spec->hp_paths[0]);
1838 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1839 spec->aamix_out_paths[2] =
1840 check_aamix_out_path(codec, spec->speaker_paths[0]);
1843 if (!spec->no_multi_io &&
1844 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1845 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1846 spec->multi_ios = 1; /* give badness */
1848 /* re-count num_dacs and squash invalid entries */
1849 spec->multiout.num_dacs = 0;
1850 for (i = 0; i < cfg->line_outs; i++) {
1851 if (spec->private_dac_nids[i])
1852 spec->multiout.num_dacs++;
1854 memmove(spec->private_dac_nids + i,
1855 spec->private_dac_nids + i + 1,
1856 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1857 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1861 spec->ext_channel_count = spec->min_channel_count =
1862 spec->multiout.num_dacs * 2;
1864 if (spec->multi_ios == 2) {
1865 for (i = 0; i < 2; i++)
1866 spec->private_dac_nids[spec->multiout.num_dacs++] =
1867 spec->multi_io[i].dac;
1868 } else if (spec->multi_ios) {
1869 spec->multi_ios = 0;
1870 badness += BAD_MULTI_IO;
1873 if (spec->indep_hp && !indep_hp_possible(codec))
1874 badness += BAD_NO_INDEP_HP;
1876 /* re-fill the shared DAC for speaker / headphone */
1877 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1878 refill_shared_dacs(codec, cfg->hp_outs,
1879 spec->multiout.hp_out_nid,
1881 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1882 refill_shared_dacs(codec, cfg->speaker_outs,
1883 spec->multiout.extra_out_nid,
1884 spec->speaker_paths);
1889 #define DEBUG_BADNESS
1891 #ifdef DEBUG_BADNESS
1892 #define debug_badness(fmt, ...) \
1893 codec_dbg(codec, fmt, ##__VA_ARGS__)
1895 #define debug_badness(fmt, ...) \
1896 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1899 #ifdef DEBUG_BADNESS
1900 static inline void print_nid_path_idx(struct hda_codec *codec,
1901 const char *pfx, int idx)
1903 struct nid_path *path;
1905 path = snd_hda_get_path_from_idx(codec, idx);
1907 print_nid_path(codec, pfx, path);
1910 static void debug_show_configs(struct hda_codec *codec,
1911 struct auto_pin_cfg *cfg)
1913 struct hda_gen_spec *spec = codec->spec;
1914 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1917 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1918 cfg->line_out_pins[0], cfg->line_out_pins[1],
1919 cfg->line_out_pins[2], cfg->line_out_pins[3],
1920 spec->multiout.dac_nids[0],
1921 spec->multiout.dac_nids[1],
1922 spec->multiout.dac_nids[2],
1923 spec->multiout.dac_nids[3],
1924 lo_type[cfg->line_out_type]);
1925 for (i = 0; i < cfg->line_outs; i++)
1926 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1927 if (spec->multi_ios > 0)
1928 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1930 spec->multi_io[0].pin, spec->multi_io[1].pin,
1931 spec->multi_io[0].dac, spec->multi_io[1].dac);
1932 for (i = 0; i < spec->multi_ios; i++)
1933 print_nid_path_idx(codec, " mio",
1934 spec->out_paths[cfg->line_outs + i]);
1936 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1937 cfg->hp_pins[0], cfg->hp_pins[1],
1938 cfg->hp_pins[2], cfg->hp_pins[3],
1939 spec->multiout.hp_out_nid[0],
1940 spec->multiout.hp_out_nid[1],
1941 spec->multiout.hp_out_nid[2],
1942 spec->multiout.hp_out_nid[3]);
1943 for (i = 0; i < cfg->hp_outs; i++)
1944 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1945 if (cfg->speaker_outs)
1946 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1947 cfg->speaker_pins[0], cfg->speaker_pins[1],
1948 cfg->speaker_pins[2], cfg->speaker_pins[3],
1949 spec->multiout.extra_out_nid[0],
1950 spec->multiout.extra_out_nid[1],
1951 spec->multiout.extra_out_nid[2],
1952 spec->multiout.extra_out_nid[3]);
1953 for (i = 0; i < cfg->speaker_outs; i++)
1954 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1955 for (i = 0; i < 3; i++)
1956 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1959 #define debug_show_configs(codec, cfg) /* NOP */
1962 /* find all available DACs of the codec */
1963 static void fill_all_dac_nids(struct hda_codec *codec)
1965 struct hda_gen_spec *spec = codec->spec;
1968 spec->num_all_dacs = 0;
1969 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1970 for_each_hda_codec_node(nid, codec) {
1971 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1973 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1974 codec_err(codec, "Too many DACs!\n");
1977 spec->all_dacs[spec->num_all_dacs++] = nid;
1981 static int parse_output_paths(struct hda_codec *codec)
1983 struct hda_gen_spec *spec = codec->spec;
1984 struct auto_pin_cfg *cfg = &spec->autocfg;
1985 struct auto_pin_cfg *best_cfg;
1987 int best_badness = INT_MAX;
1989 bool fill_hardwired = true, fill_mio_first = true;
1990 bool best_wired = true, best_mio = true;
1991 bool hp_spk_swapped = false;
1993 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1999 badness = fill_and_eval_dacs(codec, fill_hardwired,
2005 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
2006 cfg->line_out_type, fill_hardwired, fill_mio_first,
2008 debug_show_configs(codec, cfg);
2009 if (badness < best_badness) {
2010 best_badness = badness;
2012 best_wired = fill_hardwired;
2013 best_mio = fill_mio_first;
2017 fill_mio_first = !fill_mio_first;
2018 if (!fill_mio_first)
2020 fill_hardwired = !fill_hardwired;
2021 if (!fill_hardwired)
2025 hp_spk_swapped = true;
2026 if (cfg->speaker_outs > 0 &&
2027 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2028 cfg->hp_outs = cfg->line_outs;
2029 memcpy(cfg->hp_pins, cfg->line_out_pins,
2030 sizeof(cfg->hp_pins));
2031 cfg->line_outs = cfg->speaker_outs;
2032 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2033 sizeof(cfg->speaker_pins));
2034 cfg->speaker_outs = 0;
2035 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2036 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2037 fill_hardwired = true;
2040 if (cfg->hp_outs > 0 &&
2041 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2042 cfg->speaker_outs = cfg->line_outs;
2043 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2044 sizeof(cfg->speaker_pins));
2045 cfg->line_outs = cfg->hp_outs;
2046 memcpy(cfg->line_out_pins, cfg->hp_pins,
2047 sizeof(cfg->hp_pins));
2049 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2050 cfg->line_out_type = AUTO_PIN_HP_OUT;
2051 fill_hardwired = true;
2058 debug_badness("==> restoring best_cfg\n");
2060 fill_and_eval_dacs(codec, best_wired, best_mio);
2062 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2063 cfg->line_out_type, best_wired, best_mio);
2064 debug_show_configs(codec, cfg);
2066 if (cfg->line_out_pins[0]) {
2067 struct nid_path *path;
2068 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
2070 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
2071 if (spec->vmaster_nid) {
2072 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2073 HDA_OUTPUT, spec->vmaster_tlv);
2074 if (spec->dac_min_mute)
2075 spec->vmaster_tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] |= TLV_DB_SCALE_MUTE;
2079 /* set initial pinctl targets */
2080 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2084 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2085 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2086 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2087 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2088 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2089 set_pin_targets(codec, cfg->speaker_outs,
2090 cfg->speaker_pins, val);
2093 /* clear indep_hp flag if not available */
2094 if (spec->indep_hp && !indep_hp_possible(codec))
2101 /* add playback controls from the parsed DAC table */
2102 static int create_multi_out_ctls(struct hda_codec *codec,
2103 const struct auto_pin_cfg *cfg)
2105 struct hda_gen_spec *spec = codec->spec;
2106 int i, err, noutputs;
2108 noutputs = cfg->line_outs;
2109 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2110 noutputs += spec->multi_ios;
2112 for (i = 0; i < noutputs; i++) {
2115 struct nid_path *path;
2117 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
2121 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
2122 if (!name || !strcmp(name, "CLFE")) {
2124 err = add_vol_ctl(codec, "Center", 0, 1, path);
2127 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2131 err = add_stereo_vol(codec, name, index, path);
2136 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2137 if (!name || !strcmp(name, "CLFE")) {
2138 err = add_sw_ctl(codec, "Center", 0, 1, path);
2141 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2145 err = add_stereo_sw(codec, name, index, path);
2153 static int create_extra_out(struct hda_codec *codec, int path_idx,
2154 const char *pfx, int cidx)
2156 struct nid_path *path;
2159 path = snd_hda_get_path_from_idx(codec, path_idx);
2162 err = add_stereo_vol(codec, pfx, cidx, path);
2165 err = add_stereo_sw(codec, pfx, cidx, path);
2171 /* add playback controls for speaker and HP outputs */
2172 static int create_extra_outs(struct hda_codec *codec, int num_pins,
2173 const int *paths, const char *pfx)
2177 for (i = 0; i < num_pins; i++) {
2179 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2182 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2183 name = "Bass Speaker";
2184 else if (num_pins >= 3) {
2185 snprintf(tmp, sizeof(tmp), "%s %s",
2186 pfx, channel_name[i]);
2192 err = create_extra_out(codec, paths[i], name, idx);
2199 static int create_hp_out_ctls(struct hda_codec *codec)
2201 struct hda_gen_spec *spec = codec->spec;
2202 return create_extra_outs(codec, spec->autocfg.hp_outs,
2207 static int create_speaker_out_ctls(struct hda_codec *codec)
2209 struct hda_gen_spec *spec = codec->spec;
2210 return create_extra_outs(codec, spec->autocfg.speaker_outs,
2211 spec->speaker_paths,
2216 * independent HP controls
2219 static void call_hp_automute(struct hda_codec *codec,
2220 struct hda_jack_callback *jack);
2221 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2222 struct snd_ctl_elem_info *uinfo)
2224 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2227 static int indep_hp_get(struct snd_kcontrol *kcontrol,
2228 struct snd_ctl_elem_value *ucontrol)
2230 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2231 struct hda_gen_spec *spec = codec->spec;
2232 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2236 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2237 int nomix_path_idx, int mix_path_idx,
2240 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2241 struct snd_ctl_elem_value *ucontrol)
2243 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2244 struct hda_gen_spec *spec = codec->spec;
2245 unsigned int select = ucontrol->value.enumerated.item[0];
2248 mutex_lock(&spec->pcm_mutex);
2249 if (spec->active_streams) {
2254 if (spec->indep_hp_enabled != select) {
2256 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2257 dacp = &spec->private_dac_nids[0];
2259 dacp = &spec->multiout.hp_out_nid[0];
2261 /* update HP aamix paths in case it conflicts with indep HP */
2262 if (spec->have_aamix_ctl) {
2263 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2264 update_aamix_paths(codec, spec->aamix_mode,
2266 spec->aamix_out_paths[0],
2267 spec->autocfg.line_out_type);
2269 update_aamix_paths(codec, spec->aamix_mode,
2271 spec->aamix_out_paths[1],
2275 spec->indep_hp_enabled = select;
2276 if (spec->indep_hp_enabled)
2279 *dacp = spec->alt_dac_nid;
2281 call_hp_automute(codec, NULL);
2285 mutex_unlock(&spec->pcm_mutex);
2289 static const struct snd_kcontrol_new indep_hp_ctl = {
2290 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2291 .name = "Independent HP",
2292 .info = indep_hp_info,
2293 .get = indep_hp_get,
2294 .put = indep_hp_put,
2298 static int create_indep_hp_ctls(struct hda_codec *codec)
2300 struct hda_gen_spec *spec = codec->spec;
2303 if (!spec->indep_hp)
2305 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2306 dac = spec->multiout.dac_nids[0];
2308 dac = spec->multiout.hp_out_nid[0];
2314 spec->indep_hp_enabled = false;
2315 spec->alt_dac_nid = dac;
2316 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2322 * channel mode enum control
2325 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2326 struct snd_ctl_elem_info *uinfo)
2328 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2329 struct hda_gen_spec *spec = codec->spec;
2332 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2334 uinfo->value.enumerated.items = spec->multi_ios + 1;
2335 if (uinfo->value.enumerated.item > spec->multi_ios)
2336 uinfo->value.enumerated.item = spec->multi_ios;
2337 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2338 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2342 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2343 struct snd_ctl_elem_value *ucontrol)
2345 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2346 struct hda_gen_spec *spec = codec->spec;
2347 ucontrol->value.enumerated.item[0] =
2348 (spec->ext_channel_count - spec->min_channel_count) / 2;
2352 static inline struct nid_path *
2353 get_multiio_path(struct hda_codec *codec, int idx)
2355 struct hda_gen_spec *spec = codec->spec;
2356 return snd_hda_get_path_from_idx(codec,
2357 spec->out_paths[spec->autocfg.line_outs + idx]);
2360 static void update_automute_all(struct hda_codec *codec);
2362 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2363 * used for output paths
2365 static bool aamix_default(struct hda_gen_spec *spec)
2367 return !spec->have_aamix_ctl || spec->aamix_mode;
2370 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2372 struct hda_gen_spec *spec = codec->spec;
2373 hda_nid_t nid = spec->multi_io[idx].pin;
2374 struct nid_path *path;
2376 path = get_multiio_path(codec, idx);
2380 if (path->active == output)
2384 set_pin_target(codec, nid, PIN_OUT, true);
2385 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2386 set_pin_eapd(codec, nid, true);
2388 set_pin_eapd(codec, nid, false);
2389 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2390 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2391 path_power_down_sync(codec, path);
2394 /* update jack retasking in case it modifies any of them */
2395 update_automute_all(codec);
2400 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2401 struct snd_ctl_elem_value *ucontrol)
2403 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2404 struct hda_gen_spec *spec = codec->spec;
2407 ch = ucontrol->value.enumerated.item[0];
2408 if (ch < 0 || ch > spec->multi_ios)
2410 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2412 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2413 for (i = 0; i < spec->multi_ios; i++)
2414 set_multi_io(codec, i, i < ch);
2415 spec->multiout.max_channels = max(spec->ext_channel_count,
2416 spec->const_channel_count);
2417 if (spec->need_dac_fix)
2418 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2422 static const struct snd_kcontrol_new channel_mode_enum = {
2423 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2424 .name = "Channel Mode",
2425 .info = ch_mode_info,
2430 static int create_multi_channel_mode(struct hda_codec *codec)
2432 struct hda_gen_spec *spec = codec->spec;
2434 if (spec->multi_ios > 0) {
2435 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2442 * aamix loopback enable/disable switch
2445 #define loopback_mixing_info indep_hp_info
2447 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2448 struct snd_ctl_elem_value *ucontrol)
2450 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2451 struct hda_gen_spec *spec = codec->spec;
2452 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2456 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2457 int nomix_path_idx, int mix_path_idx,
2460 struct hda_gen_spec *spec = codec->spec;
2461 struct nid_path *nomix_path, *mix_path;
2463 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2464 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2465 if (!nomix_path || !mix_path)
2468 /* if HP aamix path is driven from a different DAC and the
2469 * independent HP mode is ON, can't turn on aamix path
2471 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2472 mix_path->path[0] != spec->alt_dac_nid)
2476 snd_hda_activate_path(codec, nomix_path, false, true);
2477 snd_hda_activate_path(codec, mix_path, true, true);
2478 path_power_down_sync(codec, nomix_path);
2480 snd_hda_activate_path(codec, mix_path, false, false);
2481 snd_hda_activate_path(codec, nomix_path, true, false);
2482 path_power_down_sync(codec, mix_path);
2486 /* re-initialize the output paths; only called from loopback_mixing_put() */
2487 static void update_output_paths(struct hda_codec *codec, int num_outs,
2490 struct hda_gen_spec *spec = codec->spec;
2491 struct nid_path *path;
2494 for (i = 0; i < num_outs; i++) {
2495 path = snd_hda_get_path_from_idx(codec, paths[i]);
2497 snd_hda_activate_path(codec, path, path->active,
2502 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2503 struct snd_ctl_elem_value *ucontrol)
2505 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2506 struct hda_gen_spec *spec = codec->spec;
2507 const struct auto_pin_cfg *cfg = &spec->autocfg;
2508 unsigned int val = ucontrol->value.enumerated.item[0];
2510 if (val == spec->aamix_mode)
2512 spec->aamix_mode = val;
2513 if (has_aamix_out_paths(spec)) {
2514 update_aamix_paths(codec, val, spec->out_paths[0],
2515 spec->aamix_out_paths[0],
2516 cfg->line_out_type);
2517 update_aamix_paths(codec, val, spec->hp_paths[0],
2518 spec->aamix_out_paths[1],
2520 update_aamix_paths(codec, val, spec->speaker_paths[0],
2521 spec->aamix_out_paths[2],
2522 AUTO_PIN_SPEAKER_OUT);
2524 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2525 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2526 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2527 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2528 update_output_paths(codec, cfg->speaker_outs,
2529 spec->speaker_paths);
2534 static const struct snd_kcontrol_new loopback_mixing_enum = {
2535 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2536 .name = "Loopback Mixing",
2537 .info = loopback_mixing_info,
2538 .get = loopback_mixing_get,
2539 .put = loopback_mixing_put,
2542 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2544 struct hda_gen_spec *spec = codec->spec;
2546 if (!spec->mixer_nid)
2548 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2550 spec->have_aamix_ctl = 1;
2555 * shared headphone/mic handling
2558 static void call_update_outputs(struct hda_codec *codec);
2560 /* for shared I/O, change the pin-control accordingly */
2561 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2563 struct hda_gen_spec *spec = codec->spec;
2568 pin = spec->hp_mic_pin;
2569 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2572 val = snd_hda_codec_get_pin_target(codec, pin);
2582 val = snd_hda_get_default_vref(codec, pin);
2583 /* if the HP pin doesn't support VREF and the codec driver gives an
2584 * alternative pin, set up the VREF on that pin instead
2586 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2587 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2588 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2589 if (vref_val != AC_PINCTL_VREF_HIZ)
2590 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2591 PIN_IN | (as_mic ? vref_val : 0));
2594 if (!spec->hp_mic_jack_modes) {
2599 set_pin_target(codec, pin, val, true);
2600 call_hp_automute(codec, NULL);
2604 /* create a shared input with the headphone out */
2605 static int create_hp_mic(struct hda_codec *codec)
2607 struct hda_gen_spec *spec = codec->spec;
2608 struct auto_pin_cfg *cfg = &spec->autocfg;
2609 unsigned int defcfg;
2612 if (!spec->hp_mic) {
2613 if (spec->suppress_hp_mic_detect)
2615 /* automatic detection: only if no input or a single internal
2616 * input pin is found, try to detect the shared hp/mic
2618 if (cfg->num_inputs > 1)
2620 else if (cfg->num_inputs == 1) {
2621 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2622 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2627 spec->hp_mic = 0; /* clear once */
2628 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2632 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2633 nid = cfg->line_out_pins[0];
2634 else if (cfg->hp_outs > 0)
2635 nid = cfg->hp_pins[0];
2639 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2640 return 0; /* no input */
2642 cfg->inputs[cfg->num_inputs].pin = nid;
2643 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2644 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2647 spec->hp_mic_pin = nid;
2648 /* we can't handle auto-mic together with HP-mic */
2649 spec->suppress_auto_mic = 1;
2650 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
2658 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2660 static const char * const out_jack_texts[] = {
2661 "Line Out", "Headphone Out",
2664 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2665 struct snd_ctl_elem_info *uinfo)
2667 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2670 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2671 struct snd_ctl_elem_value *ucontrol)
2673 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2674 hda_nid_t nid = kcontrol->private_value;
2675 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2676 ucontrol->value.enumerated.item[0] = 1;
2678 ucontrol->value.enumerated.item[0] = 0;
2682 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2683 struct snd_ctl_elem_value *ucontrol)
2685 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2686 hda_nid_t nid = kcontrol->private_value;
2689 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2690 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2692 snd_hda_set_pin_ctl_cache(codec, nid, val);
2696 static const struct snd_kcontrol_new out_jack_mode_enum = {
2697 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2698 .info = out_jack_mode_info,
2699 .get = out_jack_mode_get,
2700 .put = out_jack_mode_put,
2703 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2705 struct hda_gen_spec *spec = codec->spec;
2706 const struct snd_kcontrol_new *kctl;
2709 snd_array_for_each(&spec->kctls, i, kctl) {
2710 if (!strcmp(kctl->name, name) && kctl->index == idx)
2716 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2717 char *name, size_t name_len)
2719 struct hda_gen_spec *spec = codec->spec;
2722 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2723 strlcat(name, " Jack Mode", name_len);
2725 for (; find_kctl_name(codec, name, idx); idx++)
2729 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2731 struct hda_gen_spec *spec = codec->spec;
2732 if (spec->add_jack_modes) {
2733 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2734 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2740 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2743 struct hda_gen_spec *spec = codec->spec;
2746 for (i = 0; i < num_pins; i++) {
2747 hda_nid_t pin = pins[i];
2748 if (pin == spec->hp_mic_pin)
2750 if (get_out_jack_num_items(codec, pin) > 1) {
2751 struct snd_kcontrol_new *knew;
2752 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2753 get_jack_mode_name(codec, pin, name, sizeof(name));
2754 knew = snd_hda_gen_add_kctl(spec, name,
2755 &out_jack_mode_enum);
2758 knew->private_value = pin;
2769 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2772 static const char * const vref_texts[NUM_VREFS] = {
2773 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2774 "", "Mic 80pc Bias", "Mic 100pc Bias"
2777 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2779 unsigned int pincap;
2781 pincap = snd_hda_query_pin_caps(codec, pin);
2782 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2783 /* filter out unusual vrefs */
2784 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2788 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2789 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2791 unsigned int i, n = 0;
2793 for (i = 0; i < NUM_VREFS; i++) {
2794 if (vref_caps & (1 << i)) {
2803 /* convert back from the vref ctl index to the enum item index */
2804 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2806 unsigned int i, n = 0;
2808 for (i = 0; i < NUM_VREFS; i++) {
2811 if (vref_caps & (1 << i))
2817 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2818 struct snd_ctl_elem_info *uinfo)
2820 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2821 hda_nid_t nid = kcontrol->private_value;
2822 unsigned int vref_caps = get_vref_caps(codec, nid);
2824 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2826 /* set the right text */
2827 strcpy(uinfo->value.enumerated.name,
2828 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2832 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2833 struct snd_ctl_elem_value *ucontrol)
2835 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2836 hda_nid_t nid = kcontrol->private_value;
2837 unsigned int vref_caps = get_vref_caps(codec, nid);
2840 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2841 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2845 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2846 struct snd_ctl_elem_value *ucontrol)
2848 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2849 hda_nid_t nid = kcontrol->private_value;
2850 unsigned int vref_caps = get_vref_caps(codec, nid);
2851 unsigned int val, idx;
2853 val = snd_hda_codec_get_pin_target(codec, nid);
2854 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2855 if (idx == ucontrol->value.enumerated.item[0])
2858 val &= ~AC_PINCTL_VREFEN;
2859 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2860 snd_hda_set_pin_ctl_cache(codec, nid, val);
2864 static const struct snd_kcontrol_new in_jack_mode_enum = {
2865 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2866 .info = in_jack_mode_info,
2867 .get = in_jack_mode_get,
2868 .put = in_jack_mode_put,
2871 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2873 struct hda_gen_spec *spec = codec->spec;
2875 if (spec->add_jack_modes)
2876 nitems = hweight32(get_vref_caps(codec, pin));
2877 return nitems ? nitems : 1;
2880 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2882 struct hda_gen_spec *spec = codec->spec;
2883 struct snd_kcontrol_new *knew;
2884 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2885 unsigned int defcfg;
2887 if (pin == spec->hp_mic_pin)
2888 return 0; /* already done in create_out_jack_mode() */
2890 /* no jack mode for fixed pins */
2891 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2892 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2895 /* no multiple vref caps? */
2896 if (get_in_jack_num_items(codec, pin) <= 1)
2899 get_jack_mode_name(codec, pin, name, sizeof(name));
2900 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2903 knew->private_value = pin;
2908 * HP/mic shared jack mode
2910 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2911 struct snd_ctl_elem_info *uinfo)
2913 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2914 hda_nid_t nid = kcontrol->private_value;
2915 int out_jacks = get_out_jack_num_items(codec, nid);
2916 int in_jacks = get_in_jack_num_items(codec, nid);
2917 const char *text = NULL;
2920 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2922 uinfo->value.enumerated.items = out_jacks + in_jacks;
2923 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2924 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2925 idx = uinfo->value.enumerated.item;
2926 if (idx < out_jacks) {
2928 text = out_jack_texts[idx];
2930 text = "Headphone Out";
2934 unsigned int vref_caps = get_vref_caps(codec, nid);
2935 text = vref_texts[get_vref_idx(vref_caps, idx)];
2940 strcpy(uinfo->value.enumerated.name, text);
2944 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2946 int out_jacks = get_out_jack_num_items(codec, nid);
2947 int in_jacks = get_in_jack_num_items(codec, nid);
2948 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2951 if (val & PIN_OUT) {
2952 if (out_jacks > 1 && val == PIN_HP)
2954 } else if (val & PIN_IN) {
2957 unsigned int vref_caps = get_vref_caps(codec, nid);
2958 val &= AC_PINCTL_VREFEN;
2959 idx += cvt_from_vref_idx(vref_caps, val);
2965 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2966 struct snd_ctl_elem_value *ucontrol)
2968 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2969 hda_nid_t nid = kcontrol->private_value;
2970 ucontrol->value.enumerated.item[0] =
2971 get_cur_hp_mic_jack_mode(codec, nid);
2975 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2976 struct snd_ctl_elem_value *ucontrol)
2978 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2979 hda_nid_t nid = kcontrol->private_value;
2980 int out_jacks = get_out_jack_num_items(codec, nid);
2981 int in_jacks = get_in_jack_num_items(codec, nid);
2982 unsigned int val, oldval, idx;
2984 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2985 idx = ucontrol->value.enumerated.item[0];
2989 if (idx < out_jacks) {
2991 val = idx ? PIN_HP : PIN_OUT;
2997 unsigned int vref_caps = get_vref_caps(codec, nid);
2998 val = snd_hda_codec_get_pin_target(codec, nid);
2999 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
3000 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
3002 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
3004 snd_hda_set_pin_ctl_cache(codec, nid, val);
3005 call_hp_automute(codec, NULL);
3010 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
3011 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3012 .info = hp_mic_jack_mode_info,
3013 .get = hp_mic_jack_mode_get,
3014 .put = hp_mic_jack_mode_put,
3017 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
3019 struct hda_gen_spec *spec = codec->spec;
3020 struct snd_kcontrol_new *knew;
3022 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
3023 &hp_mic_jack_mode_enum);
3026 knew->private_value = pin;
3027 spec->hp_mic_jack_modes = 1;
3035 /* add the powersave loopback-list entry */
3036 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
3038 struct hda_amp_list *list;
3040 list = snd_array_new(&spec->loopback_list);
3044 list->dir = HDA_INPUT;
3046 spec->loopback.amplist = spec->loopback_list.list;
3050 /* return true if either a volume or a mute amp is found for the given
3051 * aamix path; the amp has to be either in the mixer node or its direct leaf
3053 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3054 hda_nid_t pin, unsigned int *mix_val,
3055 unsigned int *mute_val)
3058 const hda_nid_t *list;
3061 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3065 *mix_val = *mute_val = 0;
3066 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3067 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3068 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3069 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3070 if (*mix_val && *mute_val)
3073 /* check leaf node */
3074 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3075 if (num_conns < idx)
3078 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3079 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
3080 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3081 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3082 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
3083 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3085 return *mix_val || *mute_val;
3088 /* create input playback/capture controls for the given pin */
3089 static int new_analog_input(struct hda_codec *codec, int input_idx,
3090 hda_nid_t pin, const char *ctlname, int ctlidx,
3093 struct hda_gen_spec *spec = codec->spec;
3094 struct nid_path *path;
3095 unsigned int mix_val, mute_val;
3098 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3101 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
3104 print_nid_path(codec, "loopback", path);
3105 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
3107 idx = path->idx[path->depth - 1];
3109 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
3112 path->ctls[NID_PATH_VOL_CTL] = mix_val;
3116 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
3119 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
3122 path->active = true;
3123 path->stream_enabled = true; /* no DAC/ADC involved */
3124 err = add_loopback_list(spec, mix_nid, idx);
3128 if (spec->mixer_nid != spec->mixer_merge_nid &&
3129 !spec->loopback_merge_path) {
3130 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3131 spec->mixer_merge_nid, 0);
3133 print_nid_path(codec, "loopback-merge", path);
3134 path->active = true;
3135 path->pin_fixed = true; /* static route */
3136 path->stream_enabled = true; /* no DAC/ADC involved */
3137 spec->loopback_merge_path =
3138 snd_hda_get_path_idx(codec, path);
3145 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3147 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3148 return (pincap & AC_PINCAP_IN) != 0;
3151 /* Parse the codec tree and retrieve ADCs */
3152 static int fill_adc_nids(struct hda_codec *codec)
3154 struct hda_gen_spec *spec = codec->spec;
3156 hda_nid_t *adc_nids = spec->adc_nids;
3157 int max_nums = ARRAY_SIZE(spec->adc_nids);
3160 for_each_hda_codec_node(nid, codec) {
3161 unsigned int caps = get_wcaps(codec, nid);
3162 int type = get_wcaps_type(caps);
3164 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3166 adc_nids[nums] = nid;
3167 if (++nums >= max_nums)
3170 spec->num_adc_nids = nums;
3172 /* copy the detected ADCs to all_adcs[] */
3173 spec->num_all_adcs = nums;
3174 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3179 /* filter out invalid adc_nids that don't give all active input pins;
3180 * if needed, check whether dynamic ADC-switching is available
3182 static int check_dyn_adc_switch(struct hda_codec *codec)
3184 struct hda_gen_spec *spec = codec->spec;
3185 struct hda_input_mux *imux = &spec->input_mux;
3186 unsigned int ok_bits;
3191 for (n = 0; n < spec->num_adc_nids; n++) {
3192 for (i = 0; i < imux->num_items; i++) {
3193 if (!spec->input_paths[i][n])
3196 if (i >= imux->num_items) {
3197 ok_bits |= (1 << n);
3203 /* check whether ADC-switch is possible */
3204 for (i = 0; i < imux->num_items; i++) {
3205 for (n = 0; n < spec->num_adc_nids; n++) {
3206 if (spec->input_paths[i][n]) {
3207 spec->dyn_adc_idx[i] = n;
3213 codec_dbg(codec, "enabling ADC switching\n");
3214 spec->dyn_adc_switch = 1;
3215 } else if (nums != spec->num_adc_nids) {
3216 /* shrink the invalid adcs and input paths */
3218 for (n = 0; n < spec->num_adc_nids; n++) {
3219 if (!(ok_bits & (1 << n)))
3222 spec->adc_nids[nums] = spec->adc_nids[n];
3223 for (i = 0; i < imux->num_items; i++) {
3224 invalidate_nid_path(codec,
3225 spec->input_paths[i][nums]);
3226 spec->input_paths[i][nums] =
3227 spec->input_paths[i][n];
3228 spec->input_paths[i][n] = 0;
3233 spec->num_adc_nids = nums;
3236 if (imux->num_items == 1 ||
3237 (imux->num_items == 2 && spec->hp_mic)) {
3238 codec_dbg(codec, "reducing to a single ADC\n");
3239 spec->num_adc_nids = 1; /* reduce to a single ADC */
3242 /* single index for individual volumes ctls */
3243 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3244 spec->num_adc_nids = 1;
3249 /* parse capture source paths from the given pin and create imux items */
3250 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3251 int cfg_idx, int num_adcs,
3252 const char *label, int anchor)
3254 struct hda_gen_spec *spec = codec->spec;
3255 struct hda_input_mux *imux = &spec->input_mux;
3256 int imux_idx = imux->num_items;
3257 bool imux_added = false;
3260 for (c = 0; c < num_adcs; c++) {
3261 struct nid_path *path;
3262 hda_nid_t adc = spec->adc_nids[c];
3264 if (!is_reachable_path(codec, pin, adc))
3266 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3269 print_nid_path(codec, "input", path);
3270 spec->input_paths[imux_idx][c] =
3271 snd_hda_get_path_idx(codec, path);
3274 if (spec->hp_mic_pin == pin)
3275 spec->hp_mic_mux_idx = imux->num_items;
3276 spec->imux_pins[imux->num_items] = pin;
3277 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3279 if (spec->dyn_adc_switch)
3280 spec->dyn_adc_idx[imux_idx] = c;
3288 * create playback/capture controls for input pins
3291 /* fill the label for each input at first */
3292 static int fill_input_pin_labels(struct hda_codec *codec)
3294 struct hda_gen_spec *spec = codec->spec;
3295 const struct auto_pin_cfg *cfg = &spec->autocfg;
3298 for (i = 0; i < cfg->num_inputs; i++) {
3299 hda_nid_t pin = cfg->inputs[i].pin;
3303 if (!is_input_pin(codec, pin))
3306 label = hda_get_autocfg_input_label(codec, cfg, i);
3308 for (j = i - 1; j >= 0; j--) {
3309 if (spec->input_labels[j] &&
3310 !strcmp(spec->input_labels[j], label)) {
3311 idx = spec->input_label_idxs[j] + 1;
3316 spec->input_labels[i] = label;
3317 spec->input_label_idxs[i] = idx;
3323 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3325 static int create_input_ctls(struct hda_codec *codec)
3327 struct hda_gen_spec *spec = codec->spec;
3328 const struct auto_pin_cfg *cfg = &spec->autocfg;
3329 hda_nid_t mixer = spec->mixer_nid;
3334 num_adcs = fill_adc_nids(codec);
3338 err = fill_input_pin_labels(codec);
3342 for (i = 0; i < cfg->num_inputs; i++) {
3345 pin = cfg->inputs[i].pin;
3346 if (!is_input_pin(codec, pin))
3350 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3351 val |= snd_hda_get_default_vref(codec, pin);
3352 if (pin != spec->hp_mic_pin &&
3353 !snd_hda_codec_get_pin_target(codec, pin))
3354 set_pin_target(codec, pin, val, false);
3357 if (is_reachable_path(codec, pin, mixer)) {
3358 err = new_analog_input(codec, i, pin,
3359 spec->input_labels[i],
3360 spec->input_label_idxs[i],
3367 err = parse_capture_source(codec, pin, i, num_adcs,
3368 spec->input_labels[i], -mixer);
3372 if (spec->add_jack_modes) {
3373 err = create_in_jack_mode(codec, pin);
3379 /* add stereo mix when explicitly enabled via hint */
3380 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3381 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3386 spec->suppress_auto_mic = 1;
3397 /* get the input path specified by the given adc and imux indices */
3398 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3400 struct hda_gen_spec *spec = codec->spec;
3401 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3405 if (spec->dyn_adc_switch)
3406 adc_idx = spec->dyn_adc_idx[imux_idx];
3407 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3411 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3414 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3417 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3418 struct snd_ctl_elem_info *uinfo)
3420 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3421 struct hda_gen_spec *spec = codec->spec;
3422 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3425 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3426 struct snd_ctl_elem_value *ucontrol)
3428 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3429 struct hda_gen_spec *spec = codec->spec;
3430 /* the ctls are created at once with multiple counts */
3431 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3433 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3437 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3438 struct snd_ctl_elem_value *ucontrol)
3440 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3441 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3442 return mux_select(codec, adc_idx,
3443 ucontrol->value.enumerated.item[0]);
3446 static const struct snd_kcontrol_new cap_src_temp = {
3447 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3448 .name = "Input Source",
3449 .info = mux_enum_info,
3450 .get = mux_enum_get,
3451 .put = mux_enum_put,
3455 * capture volume and capture switch ctls
3458 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3459 struct snd_ctl_elem_value *ucontrol);
3461 /* call the given amp update function for all amps in the imux list at once */
3462 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3463 struct snd_ctl_elem_value *ucontrol,
3464 put_call_t func, int type)
3466 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3467 struct hda_gen_spec *spec = codec->spec;
3468 const struct hda_input_mux *imux;
3469 struct nid_path *path;
3470 int i, adc_idx, ret, err = 0;
3472 imux = &spec->input_mux;
3473 adc_idx = kcontrol->id.index;
3474 mutex_lock(&codec->control_mutex);
3475 for (i = 0; i < imux->num_items; i++) {
3476 path = get_input_path(codec, adc_idx, i);
3477 if (!path || !path->ctls[type])
3479 kcontrol->private_value = path->ctls[type];
3480 ret = func(kcontrol, ucontrol);
3488 mutex_unlock(&codec->control_mutex);
3489 if (err >= 0 && spec->cap_sync_hook)
3490 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3494 /* capture volume ctl callbacks */
3495 #define cap_vol_info snd_hda_mixer_amp_volume_info
3496 #define cap_vol_get snd_hda_mixer_amp_volume_get
3497 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3499 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3500 struct snd_ctl_elem_value *ucontrol)
3502 return cap_put_caller(kcontrol, ucontrol,
3503 snd_hda_mixer_amp_volume_put,
3507 static const struct snd_kcontrol_new cap_vol_temp = {
3508 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3509 .name = "Capture Volume",
3510 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3511 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3512 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3513 .info = cap_vol_info,
3516 .tlv = { .c = cap_vol_tlv },
3519 /* capture switch ctl callbacks */
3520 #define cap_sw_info snd_ctl_boolean_stereo_info
3521 #define cap_sw_get snd_hda_mixer_amp_switch_get
3523 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3524 struct snd_ctl_elem_value *ucontrol)
3526 return cap_put_caller(kcontrol, ucontrol,
3527 snd_hda_mixer_amp_switch_put,
3531 static const struct snd_kcontrol_new cap_sw_temp = {
3532 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3533 .name = "Capture Switch",
3534 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
3535 .info = cap_sw_info,
3540 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3545 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3546 for (depth = 0; depth < 3; depth++) {
3547 if (depth >= path->depth)
3549 i = path->depth - depth - 1;
3550 nid = path->path[i];
3551 if (!path->ctls[NID_PATH_VOL_CTL]) {
3552 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3553 path->ctls[NID_PATH_VOL_CTL] =
3554 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3555 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3556 int idx = path->idx[i];
3557 if (!depth && codec->single_adc_amp)
3559 path->ctls[NID_PATH_VOL_CTL] =
3560 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3563 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3564 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3565 path->ctls[NID_PATH_MUTE_CTL] =
3566 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3567 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3568 int idx = path->idx[i];
3569 if (!depth && codec->single_adc_amp)
3571 path->ctls[NID_PATH_MUTE_CTL] =
3572 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3579 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3581 struct hda_gen_spec *spec = codec->spec;
3582 struct auto_pin_cfg *cfg = &spec->autocfg;
3586 if (!spec->inv_dmic_split)
3588 for (i = 0; i < cfg->num_inputs; i++) {
3589 if (cfg->inputs[i].pin != nid)
3591 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3593 val = snd_hda_codec_get_pincfg(codec, nid);
3594 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3599 /* capture switch put callback for a single control with hook call */
3600 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3601 struct snd_ctl_elem_value *ucontrol)
3603 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3604 struct hda_gen_spec *spec = codec->spec;
3607 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3611 if (spec->cap_sync_hook)
3612 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3617 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3618 int idx, bool is_switch, unsigned int ctl,
3621 struct hda_gen_spec *spec = codec->spec;
3622 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3623 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3624 const char *sfx = is_switch ? "Switch" : "Volume";
3625 unsigned int chs = inv_dmic ? 1 : 3;
3626 struct snd_kcontrol_new *knew;
3632 snprintf(tmpname, sizeof(tmpname),
3633 "%s Capture %s", label, sfx);
3635 snprintf(tmpname, sizeof(tmpname),
3637 knew = add_control(spec, type, tmpname, idx,
3638 amp_val_replace_channels(ctl, chs));
3642 knew->put = cap_single_sw_put;
3643 if (spec->mic_mute_led)
3644 knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
3649 /* Make independent right kcontrol */
3651 snprintf(tmpname, sizeof(tmpname),
3652 "Inverted %s Capture %s", label, sfx);
3654 snprintf(tmpname, sizeof(tmpname),
3655 "Inverted Capture %s", sfx);
3656 knew = add_control(spec, type, tmpname, idx,
3657 amp_val_replace_channels(ctl, 2));
3661 knew->put = cap_single_sw_put;
3662 if (spec->mic_mute_led)
3663 knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
3668 /* create single (and simple) capture volume and switch controls */
3669 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3670 unsigned int vol_ctl, unsigned int sw_ctl,
3674 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3677 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3683 /* create bound capture volume and switch controls */
3684 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3685 unsigned int vol_ctl, unsigned int sw_ctl)
3687 struct hda_gen_spec *spec = codec->spec;
3688 struct snd_kcontrol_new *knew;
3691 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3695 knew->private_value = vol_ctl;
3696 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3699 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3703 knew->private_value = sw_ctl;
3704 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3705 if (spec->mic_mute_led)
3706 knew->access |= SNDRV_CTL_ELEM_ACCESS_MIC_LED;
3711 /* return the vol ctl when used first in the imux list */
3712 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3714 struct nid_path *path;
3718 path = get_input_path(codec, 0, idx);
3721 ctl = path->ctls[type];
3724 for (i = 0; i < idx - 1; i++) {
3725 path = get_input_path(codec, 0, i);
3726 if (path && path->ctls[type] == ctl)
3732 /* create individual capture volume and switch controls per input */
3733 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3735 struct hda_gen_spec *spec = codec->spec;
3736 struct hda_input_mux *imux = &spec->input_mux;
3739 for (i = 0; i < imux->num_items; i++) {
3743 idx = imux->items[i].index;
3744 if (idx >= spec->autocfg.num_inputs)
3746 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3748 for (type = 0; type < 2; type++) {
3749 err = add_single_cap_ctl(codec,
3750 spec->input_labels[idx],
3751 spec->input_label_idxs[idx],
3753 get_first_cap_ctl(codec, i, type),
3762 static int create_capture_mixers(struct hda_codec *codec)
3764 struct hda_gen_spec *spec = codec->spec;
3765 struct hda_input_mux *imux = &spec->input_mux;
3766 int i, n, nums, err;
3768 if (spec->dyn_adc_switch)
3771 nums = spec->num_adc_nids;
3773 if (!spec->auto_mic && imux->num_items > 1) {
3774 struct snd_kcontrol_new *knew;
3776 name = nums > 1 ? "Input Source" : "Capture Source";
3777 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3783 for (n = 0; n < nums; n++) {
3785 bool multi_cap_vol = spec->multi_cap_vol;
3786 bool inv_dmic = false;
3790 for (i = 0; i < imux->num_items; i++) {
3791 struct nid_path *path;
3792 path = get_input_path(codec, n, i);
3795 parse_capvol_in_path(codec, path);
3797 vol = path->ctls[NID_PATH_VOL_CTL];
3798 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3800 if (!same_amp_caps(codec, vol,
3801 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3802 multi_cap_vol = true;
3805 sw = path->ctls[NID_PATH_MUTE_CTL];
3806 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3808 if (!same_amp_caps(codec, sw,
3809 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3810 multi_cap_vol = true;
3812 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3817 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3819 else if (!multi_cap_vol && !inv_dmic)
3820 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3822 err = create_multi_cap_vol_ctl(codec);
3831 * add mic boosts if needed
3834 /* check whether the given amp is feasible as a boost volume */
3835 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3840 if (!nid_has_volume(codec, nid, dir) ||
3841 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3842 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3845 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3846 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3852 /* look for a boost amp in a widget close to the pin */
3853 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3854 struct nid_path *path)
3856 unsigned int val = 0;
3860 for (depth = 0; depth < 3; depth++) {
3861 if (depth >= path->depth - 1)
3863 nid = path->path[depth];
3864 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3865 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3867 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3868 path->idx[depth])) {
3869 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3878 static int parse_mic_boost(struct hda_codec *codec)
3880 struct hda_gen_spec *spec = codec->spec;
3881 struct auto_pin_cfg *cfg = &spec->autocfg;
3882 struct hda_input_mux *imux = &spec->input_mux;
3885 if (!spec->num_adc_nids)
3888 for (i = 0; i < imux->num_items; i++) {
3889 struct nid_path *path;
3892 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3894 idx = imux->items[i].index;
3895 if (idx >= imux->num_items)
3898 /* check only line-in and mic pins */
3899 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3902 path = get_input_path(codec, 0, i);
3906 val = look_for_boost_amp(codec, path);
3910 /* create a boost control */
3911 snprintf(boost_label, sizeof(boost_label),
3912 "%s Boost Volume", spec->input_labels[idx]);
3913 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3914 spec->input_label_idxs[idx], val))
3917 path->ctls[NID_PATH_BOOST_CTL] = val;
3922 #ifdef CONFIG_SND_HDA_GENERIC_LEDS
3924 * vmaster mute LED hook helpers
3927 static int create_mute_led_cdev(struct hda_codec *codec,
3928 int (*callback)(struct led_classdev *,
3929 enum led_brightness),
3932 struct hda_gen_spec *spec = codec->spec;
3933 struct led_classdev *cdev;
3934 int idx = micmute ? LED_AUDIO_MICMUTE : LED_AUDIO_MUTE;
3937 cdev = devm_kzalloc(&codec->core.dev, sizeof(*cdev), GFP_KERNEL);
3941 cdev->name = micmute ? "hda::micmute" : "hda::mute";
3942 cdev->max_brightness = 1;
3943 cdev->default_trigger = micmute ? "audio-micmute" : "audio-mute";
3944 cdev->brightness_set_blocking = callback;
3945 cdev->brightness = ledtrig_audio_get(idx);
3946 cdev->flags = LED_CORE_SUSPENDRESUME;
3948 err = led_classdev_register(&codec->core.dev, cdev);
3951 spec->led_cdevs[idx] = cdev;
3956 * snd_hda_gen_add_mute_led_cdev - Create a LED classdev and enable as vmaster mute LED
3957 * @codec: the HDA codec
3958 * @callback: the callback for LED classdev brightness_set_blocking
3960 int snd_hda_gen_add_mute_led_cdev(struct hda_codec *codec,
3961 int (*callback)(struct led_classdev *,
3962 enum led_brightness))
3964 struct hda_gen_spec *spec = codec->spec;
3968 err = create_mute_led_cdev(codec, callback, false);
3970 codec_warn(codec, "failed to create a mute LED cdev\n");
3975 if (spec->vmaster_mute.hook)
3976 codec_err(codec, "vmaster hook already present before cdev!\n");
3978 spec->vmaster_mute_led = 1;
3981 EXPORT_SYMBOL_GPL(snd_hda_gen_add_mute_led_cdev);
3984 * snd_hda_gen_add_micmute_led_cdev - Create a LED classdev and enable as mic-mute LED
3985 * @codec: the HDA codec
3986 * @callback: the callback for LED classdev brightness_set_blocking
3988 * Called from the codec drivers for offering the mic mute LED controls.
3989 * This creates a LED classdev and sets up the cap_sync_hook that is called at
3990 * each time when the capture mixer switch changes.
3992 * When NULL is passed to @callback, no classdev is created but only the
3993 * LED-trigger is set up.
3995 * Returns 0 or a negative error.
3997 int snd_hda_gen_add_micmute_led_cdev(struct hda_codec *codec,
3998 int (*callback)(struct led_classdev *,
3999 enum led_brightness))
4001 struct hda_gen_spec *spec = codec->spec;
4005 err = create_mute_led_cdev(codec, callback, true);
4007 codec_warn(codec, "failed to create a mic-mute LED cdev\n");
4012 spec->mic_mute_led = 1;
4015 EXPORT_SYMBOL_GPL(snd_hda_gen_add_micmute_led_cdev);
4016 #endif /* CONFIG_SND_HDA_GENERIC_LEDS */
4019 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
4021 static void parse_digital(struct hda_codec *codec)
4023 struct hda_gen_spec *spec = codec->spec;
4024 struct nid_path *path;
4026 hda_nid_t dig_nid, pin;
4028 /* support multiple SPDIFs; the secondary is set up as a follower */
4030 for (i = 0; i < spec->autocfg.dig_outs; i++) {
4031 pin = spec->autocfg.dig_out_pins[i];
4032 dig_nid = look_for_dac(codec, pin, true);
4035 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
4038 print_nid_path(codec, "digout", path);
4039 path->active = true;
4040 path->pin_fixed = true; /* no jack detection */
4041 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
4042 set_pin_target(codec, pin, PIN_OUT, false);
4044 spec->multiout.dig_out_nid = dig_nid;
4045 spec->dig_out_type = spec->autocfg.dig_out_type[0];
4047 spec->multiout.follower_dig_outs = spec->follower_dig_outs;
4048 if (nums >= ARRAY_SIZE(spec->follower_dig_outs) - 1)
4050 spec->follower_dig_outs[nums - 1] = dig_nid;
4055 if (spec->autocfg.dig_in_pin) {
4056 pin = spec->autocfg.dig_in_pin;
4057 for_each_hda_codec_node(dig_nid, codec) {
4058 unsigned int wcaps = get_wcaps(codec, dig_nid);
4059 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
4061 if (!(wcaps & AC_WCAP_DIGITAL))
4063 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
4065 print_nid_path(codec, "digin", path);
4066 path->active = true;
4067 path->pin_fixed = true; /* no jack */
4068 spec->dig_in_nid = dig_nid;
4069 spec->digin_path = snd_hda_get_path_idx(codec, path);
4070 set_pin_target(codec, pin, PIN_IN, false);
4079 * input MUX handling
4082 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
4084 /* select the given imux item; either unmute exclusively or select the route */
4085 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
4088 struct hda_gen_spec *spec = codec->spec;
4089 const struct hda_input_mux *imux;
4090 struct nid_path *old_path, *path;
4092 imux = &spec->input_mux;
4093 if (!imux->num_items)
4096 if (idx >= imux->num_items)
4097 idx = imux->num_items - 1;
4098 if (spec->cur_mux[adc_idx] == idx)
4101 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
4104 if (old_path->active)
4105 snd_hda_activate_path(codec, old_path, false, false);
4107 spec->cur_mux[adc_idx] = idx;
4110 update_hp_mic(codec, adc_idx, false);
4112 if (spec->dyn_adc_switch)
4113 dyn_adc_pcm_resetup(codec, idx);
4115 path = get_input_path(codec, adc_idx, idx);
4120 snd_hda_activate_path(codec, path, true, false);
4121 if (spec->cap_sync_hook)
4122 spec->cap_sync_hook(codec, NULL, NULL);
4123 path_power_down_sync(codec, old_path);
4127 /* power up/down widgets in the all paths that match with the given NID
4128 * as terminals (either start- or endpoint)
4130 * returns the last changed NID, or zero if unchanged.
4132 static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
4133 int pin_state, int stream_state)
4135 struct hda_gen_spec *spec = codec->spec;
4136 hda_nid_t last, changed = 0;
4137 struct nid_path *path;
4140 snd_array_for_each(&spec->paths, n, path) {
4143 if (path->path[0] == nid ||
4144 path->path[path->depth - 1] == nid) {
4145 bool pin_old = path->pin_enabled;
4146 bool stream_old = path->stream_enabled;
4149 path->pin_enabled = pin_state;
4150 if (stream_state >= 0)
4151 path->stream_enabled = stream_state;
4152 if ((!path->pin_fixed && path->pin_enabled != pin_old)
4153 || path->stream_enabled != stream_old) {
4154 last = path_power_update(codec, path, true);
4163 /* check the jack status for power control */
4164 static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4166 if (!is_jack_detectable(codec, pin))
4168 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4171 /* power up/down the paths of the given pin according to the jack state;
4172 * power = 0/1 : only power up/down if it matches with the jack state,
4173 * < 0 : force power up/down to follow the jack sate
4175 * returns the last changed NID, or zero if unchanged.
4177 static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4182 if (!codec->power_save_node)
4185 on = detect_pin_state(codec, pin);
4187 if (power >= 0 && on != power)
4189 return set_path_power(codec, pin, on, -1);
4192 static void pin_power_callback(struct hda_codec *codec,
4193 struct hda_jack_callback *jack,
4196 if (jack && jack->nid)
4197 sync_power_state_change(codec,
4198 set_pin_power_jack(codec, jack->nid, on));
4201 /* callback only doing power up -- called at first */
4202 static void pin_power_up_callback(struct hda_codec *codec,
4203 struct hda_jack_callback *jack)
4205 pin_power_callback(codec, jack, true);
4208 /* callback only doing power down -- called at last */
4209 static void pin_power_down_callback(struct hda_codec *codec,
4210 struct hda_jack_callback *jack)
4212 pin_power_callback(codec, jack, false);
4215 /* set up the power up/down callbacks */
4216 static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4217 const hda_nid_t *pins, bool on)
4220 hda_jack_callback_fn cb =
4221 on ? pin_power_up_callback : pin_power_down_callback;
4223 for (i = 0; i < num_pins && pins[i]; i++) {
4224 if (is_jack_detectable(codec, pins[i]))
4225 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4227 set_path_power(codec, pins[i], true, -1);
4231 /* enabled power callback to each available I/O pin with jack detections;
4232 * the digital I/O pins are excluded because of the unreliable detectsion
4234 static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4236 struct hda_gen_spec *spec = codec->spec;
4237 struct auto_pin_cfg *cfg = &spec->autocfg;
4240 if (!codec->power_save_node)
4242 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4243 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4244 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4245 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4246 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4247 for (i = 0; i < cfg->num_inputs; i++)
4248 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4251 /* sync path power up/down with the jack states of given pins */
4252 static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4253 const hda_nid_t *pins)
4257 for (i = 0; i < num_pins && pins[i]; i++)
4258 if (is_jack_detectable(codec, pins[i]))
4259 set_pin_power_jack(codec, pins[i], -1);
4262 /* sync path power up/down with pins; called at init and resume */
4263 static void sync_all_pin_power_ctls(struct hda_codec *codec)
4265 struct hda_gen_spec *spec = codec->spec;
4266 struct auto_pin_cfg *cfg = &spec->autocfg;
4269 if (!codec->power_save_node)
4271 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4272 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4273 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4274 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4275 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4276 for (i = 0; i < cfg->num_inputs; i++)
4277 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4280 /* add fake paths if not present yet */
4281 static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4282 int num_pins, const hda_nid_t *pins)
4284 struct hda_gen_spec *spec = codec->spec;
4285 struct nid_path *path;
4288 for (i = 0; i < num_pins; i++) {
4291 if (get_nid_path(codec, nid, pins[i], 0))
4293 path = snd_array_new(&spec->paths);
4296 memset(path, 0, sizeof(*path));
4298 path->path[0] = nid;
4299 path->path[1] = pins[i];
4300 path->active = true;
4305 /* create fake paths to all outputs from beep */
4306 static int add_fake_beep_paths(struct hda_codec *codec)
4308 struct hda_gen_spec *spec = codec->spec;
4309 struct auto_pin_cfg *cfg = &spec->autocfg;
4310 hda_nid_t nid = spec->beep_nid;
4313 if (!codec->power_save_node || !nid)
4315 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4318 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4319 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4323 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4324 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4332 /* power up/down beep widget and its output paths */
4333 static void beep_power_hook(struct hda_beep *beep, bool on)
4335 set_path_power(beep->codec, beep->nid, -1, on);
4339 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4340 * @codec: the HDA codec
4341 * @pin: NID of pin to fix
4343 int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4345 struct hda_gen_spec *spec = codec->spec;
4346 struct nid_path *path;
4348 path = snd_array_new(&spec->paths);
4351 memset(path, 0, sizeof(*path));
4353 path->path[0] = pin;
4354 path->active = true;
4355 path->pin_fixed = true;
4356 path->stream_enabled = true;
4359 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4362 * Jack detections for HP auto-mute and mic-switch
4365 /* check each pin in the given array; returns true if any of them is plugged */
4366 static bool detect_jacks(struct hda_codec *codec, int num_pins, const hda_nid_t *pins)
4369 bool present = false;
4371 for (i = 0; i < num_pins; i++) {
4372 hda_nid_t nid = pins[i];
4375 /* don't detect pins retasked as inputs */
4376 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4378 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4384 /* standard HP/line-out auto-mute helper */
4385 static void do_automute(struct hda_codec *codec, int num_pins, const hda_nid_t *pins,
4386 int *paths, bool mute)
4388 struct hda_gen_spec *spec = codec->spec;
4391 for (i = 0; i < num_pins; i++) {
4392 hda_nid_t nid = pins[i];
4393 unsigned int val, oldval;
4397 oldval = snd_hda_codec_get_pin_target(codec, nid);
4398 if (oldval & PIN_IN)
4399 continue; /* no mute for inputs */
4401 if (spec->auto_mute_via_amp) {
4402 struct nid_path *path;
4405 path = snd_hda_get_path_from_idx(codec, paths[i]);
4408 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4412 spec->mute_bits |= (1ULL << mute_nid);
4414 spec->mute_bits &= ~(1ULL << mute_nid);
4417 /* don't reset VREF value in case it's controlling
4418 * the amp (see alc861_fixup_asus_amp_vref_0f())
4420 if (spec->keep_vref_in_automute)
4421 val = oldval & ~PIN_HP;
4426 /* here we call update_pin_ctl() so that the pinctl is
4427 * changed without changing the pinctl target value;
4428 * the original target value will be still referred at
4429 * the init / resume again
4431 update_pin_ctl(codec, nid, val);
4434 set_pin_eapd(codec, nid, !mute);
4435 if (codec->power_save_node) {
4438 on = detect_pin_state(codec, nid);
4439 set_path_power(codec, nid, on, -1);
4445 * snd_hda_gen_update_outputs - Toggle outputs muting
4446 * @codec: the HDA codec
4448 * Update the mute status of all outputs based on the current jack states.
4450 void snd_hda_gen_update_outputs(struct hda_codec *codec)
4452 struct hda_gen_spec *spec = codec->spec;
4456 /* Control HP pins/amps depending on master_mute state;
4457 * in general, HP pins/amps control should be enabled in all cases,
4458 * but currently set only for master_mute, just to be safe
4460 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4461 paths = spec->out_paths;
4463 paths = spec->hp_paths;
4464 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
4465 spec->autocfg.hp_pins, paths, spec->master_mute);
4467 if (!spec->automute_speaker)
4470 on = spec->hp_jack_present | spec->line_jack_present;
4471 on |= spec->master_mute;
4472 spec->speaker_muted = on;
4473 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4474 paths = spec->out_paths;
4476 paths = spec->speaker_paths;
4477 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
4478 spec->autocfg.speaker_pins, paths, on);
4480 /* toggle line-out mutes if needed, too */
4481 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4482 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4483 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4485 if (!spec->automute_lo)
4488 on = spec->hp_jack_present;
4489 on |= spec->master_mute;
4490 spec->line_out_muted = on;
4491 paths = spec->out_paths;
4492 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4493 spec->autocfg.line_out_pins, paths, on);
4495 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4497 static void call_update_outputs(struct hda_codec *codec)
4499 struct hda_gen_spec *spec = codec->spec;
4500 if (spec->automute_hook)
4501 spec->automute_hook(codec);
4503 snd_hda_gen_update_outputs(codec);
4505 /* sync the whole vmaster followers to reflect the new auto-mute status */
4506 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4507 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4511 * snd_hda_gen_hp_automute - standard HP-automute helper
4512 * @codec: the HDA codec
4513 * @jack: jack object, NULL for the whole
4515 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4516 struct hda_jack_callback *jack)
4518 struct hda_gen_spec *spec = codec->spec;
4519 hda_nid_t *pins = spec->autocfg.hp_pins;
4520 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4522 /* No detection for the first HP jack during indep-HP mode */
4523 if (spec->indep_hp_enabled) {
4528 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4529 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4531 call_update_outputs(codec);
4533 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4536 * snd_hda_gen_line_automute - standard line-out-automute helper
4537 * @codec: the HDA codec
4538 * @jack: jack object, NULL for the whole
4540 void snd_hda_gen_line_automute(struct hda_codec *codec,
4541 struct hda_jack_callback *jack)
4543 struct hda_gen_spec *spec = codec->spec;
4545 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4547 /* check LO jack only when it's different from HP */
4548 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4551 spec->line_jack_present =
4552 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4553 spec->autocfg.line_out_pins);
4554 if (!spec->automute_speaker || !spec->detect_lo)
4556 call_update_outputs(codec);
4558 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4561 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4562 * @codec: the HDA codec
4563 * @jack: jack object, NULL for the whole
4565 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4566 struct hda_jack_callback *jack)
4568 struct hda_gen_spec *spec = codec->spec;
4571 if (!spec->auto_mic)
4574 for (i = spec->am_num_entries - 1; i > 0; i--) {
4575 hda_nid_t pin = spec->am_entry[i].pin;
4576 /* don't detect pins retasked as outputs */
4577 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4579 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4580 mux_select(codec, 0, spec->am_entry[i].idx);
4584 mux_select(codec, 0, spec->am_entry[0].idx);
4586 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4588 /* call appropriate hooks */
4589 static void call_hp_automute(struct hda_codec *codec,
4590 struct hda_jack_callback *jack)
4592 struct hda_gen_spec *spec = codec->spec;
4593 if (spec->hp_automute_hook)
4594 spec->hp_automute_hook(codec, jack);
4596 snd_hda_gen_hp_automute(codec, jack);
4599 static void call_line_automute(struct hda_codec *codec,
4600 struct hda_jack_callback *jack)
4602 struct hda_gen_spec *spec = codec->spec;
4603 if (spec->line_automute_hook)
4604 spec->line_automute_hook(codec, jack);
4606 snd_hda_gen_line_automute(codec, jack);
4609 static void call_mic_autoswitch(struct hda_codec *codec,
4610 struct hda_jack_callback *jack)
4612 struct hda_gen_spec *spec = codec->spec;
4613 if (spec->mic_autoswitch_hook)
4614 spec->mic_autoswitch_hook(codec, jack);
4616 snd_hda_gen_mic_autoswitch(codec, jack);
4619 /* update jack retasking */
4620 static void update_automute_all(struct hda_codec *codec)
4622 call_hp_automute(codec, NULL);
4623 call_line_automute(codec, NULL);
4624 call_mic_autoswitch(codec, NULL);
4628 * Auto-Mute mode mixer enum support
4630 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4631 struct snd_ctl_elem_info *uinfo)
4633 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4634 struct hda_gen_spec *spec = codec->spec;
4635 static const char * const texts3[] = {
4636 "Disabled", "Speaker Only", "Line Out+Speaker"
4639 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4640 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4641 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4644 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4645 struct snd_ctl_elem_value *ucontrol)
4647 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4648 struct hda_gen_spec *spec = codec->spec;
4649 unsigned int val = 0;
4650 if (spec->automute_speaker)
4652 if (spec->automute_lo)
4655 ucontrol->value.enumerated.item[0] = val;
4659 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4660 struct snd_ctl_elem_value *ucontrol)
4662 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4663 struct hda_gen_spec *spec = codec->spec;
4665 switch (ucontrol->value.enumerated.item[0]) {
4667 if (!spec->automute_speaker && !spec->automute_lo)
4669 spec->automute_speaker = 0;
4670 spec->automute_lo = 0;
4673 if (spec->automute_speaker_possible) {
4674 if (!spec->automute_lo && spec->automute_speaker)
4676 spec->automute_speaker = 1;
4677 spec->automute_lo = 0;
4678 } else if (spec->automute_lo_possible) {
4679 if (spec->automute_lo)
4681 spec->automute_lo = 1;
4686 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4688 if (spec->automute_speaker && spec->automute_lo)
4690 spec->automute_speaker = 1;
4691 spec->automute_lo = 1;
4696 call_update_outputs(codec);
4700 static const struct snd_kcontrol_new automute_mode_enum = {
4701 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4702 .name = "Auto-Mute Mode",
4703 .info = automute_mode_info,
4704 .get = automute_mode_get,
4705 .put = automute_mode_put,
4708 static int add_automute_mode_enum(struct hda_codec *codec)
4710 struct hda_gen_spec *spec = codec->spec;
4712 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4718 * Check the availability of HP/line-out auto-mute;
4719 * Set up appropriately if really supported
4721 static int check_auto_mute_availability(struct hda_codec *codec)
4723 struct hda_gen_spec *spec = codec->spec;
4724 struct auto_pin_cfg *cfg = &spec->autocfg;
4728 if (spec->suppress_auto_mute)
4731 if (cfg->hp_pins[0])
4733 if (cfg->line_out_pins[0])
4735 if (cfg->speaker_pins[0])
4737 if (present < 2) /* need two different output types */
4740 if (!cfg->speaker_pins[0] &&
4741 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4742 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4743 sizeof(cfg->speaker_pins));
4744 cfg->speaker_outs = cfg->line_outs;
4747 if (!cfg->hp_pins[0] &&
4748 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4749 memcpy(cfg->hp_pins, cfg->line_out_pins,
4750 sizeof(cfg->hp_pins));
4751 cfg->hp_outs = cfg->line_outs;
4754 for (i = 0; i < cfg->hp_outs; i++) {
4755 hda_nid_t nid = cfg->hp_pins[i];
4756 if (!is_jack_detectable(codec, nid))
4758 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4759 snd_hda_jack_detect_enable_callback(codec, nid,
4761 spec->detect_hp = 1;
4764 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4765 if (cfg->speaker_outs)
4766 for (i = 0; i < cfg->line_outs; i++) {
4767 hda_nid_t nid = cfg->line_out_pins[i];
4768 if (!is_jack_detectable(codec, nid))
4770 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4771 snd_hda_jack_detect_enable_callback(codec, nid,
4772 call_line_automute);
4773 spec->detect_lo = 1;
4775 spec->automute_lo_possible = spec->detect_hp;
4778 spec->automute_speaker_possible = cfg->speaker_outs &&
4779 (spec->detect_hp || spec->detect_lo);
4781 spec->automute_lo = spec->automute_lo_possible;
4782 spec->automute_speaker = spec->automute_speaker_possible;
4784 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4785 /* create a control for automute mode */
4786 err = add_automute_mode_enum(codec);
4793 /* check whether all auto-mic pins are valid; setup indices if OK */
4794 static bool auto_mic_check_imux(struct hda_codec *codec)
4796 struct hda_gen_spec *spec = codec->spec;
4797 const struct hda_input_mux *imux;
4800 imux = &spec->input_mux;
4801 for (i = 0; i < spec->am_num_entries; i++) {
4802 spec->am_entry[i].idx =
4803 find_idx_in_nid_list(spec->am_entry[i].pin,
4804 spec->imux_pins, imux->num_items);
4805 if (spec->am_entry[i].idx < 0)
4806 return false; /* no corresponding imux */
4809 /* we don't need the jack detection for the first pin */
4810 for (i = 1; i < spec->am_num_entries; i++)
4811 snd_hda_jack_detect_enable_callback(codec,
4812 spec->am_entry[i].pin,
4813 call_mic_autoswitch);
4817 static int compare_attr(const void *ap, const void *bp)
4819 const struct automic_entry *a = ap;
4820 const struct automic_entry *b = bp;
4821 return (int)(a->attr - b->attr);
4825 * Check the availability of auto-mic switch;
4826 * Set up if really supported
4828 static int check_auto_mic_availability(struct hda_codec *codec)
4830 struct hda_gen_spec *spec = codec->spec;
4831 struct auto_pin_cfg *cfg = &spec->autocfg;
4835 if (spec->suppress_auto_mic)
4840 for (i = 0; i < cfg->num_inputs; i++) {
4841 hda_nid_t nid = cfg->inputs[i].pin;
4843 attr = snd_hda_codec_get_pincfg(codec, nid);
4844 attr = snd_hda_get_input_pin_attr(attr);
4845 if (types & (1 << attr))
4846 return 0; /* already occupied */
4848 case INPUT_PIN_ATTR_INT:
4849 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4850 return 0; /* invalid type */
4852 case INPUT_PIN_ATTR_UNUSED:
4853 return 0; /* invalid entry */
4855 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4856 return 0; /* invalid type */
4857 if (!spec->line_in_auto_switch &&
4858 cfg->inputs[i].type != AUTO_PIN_MIC)
4859 return 0; /* only mic is allowed */
4860 if (!is_jack_detectable(codec, nid))
4861 return 0; /* no unsol support */
4864 if (num_pins >= MAX_AUTO_MIC_PINS)
4866 types |= (1 << attr);
4867 spec->am_entry[num_pins].pin = nid;
4868 spec->am_entry[num_pins].attr = attr;
4875 spec->am_num_entries = num_pins;
4876 /* sort the am_entry in the order of attr so that the pin with a
4877 * higher attr will be selected when the jack is plugged.
4879 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4880 compare_attr, NULL);
4882 if (!auto_mic_check_imux(codec))
4886 spec->num_adc_nids = 1;
4887 spec->cur_mux[0] = spec->am_entry[0].idx;
4888 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4889 spec->am_entry[0].pin,
4890 spec->am_entry[1].pin,
4891 spec->am_entry[2].pin);
4897 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4899 * @codec: the HDA codec
4900 * @nid: NID to evalute
4901 * @power_state: target power state
4903 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4905 unsigned int power_state)
4907 struct hda_gen_spec *spec = codec->spec;
4909 if (!spec->power_down_unused && !codec->power_save_node)
4911 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
4913 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4915 if (is_active_nid_for_any(codec, nid))
4919 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4921 /* mute all aamix inputs initially; parse up to the first leaves */
4922 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4925 const hda_nid_t *conn;
4928 nums = snd_hda_get_conn_list(codec, mix, &conn);
4929 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4930 for (i = 0; i < nums; i++) {
4932 update_amp(codec, mix, HDA_INPUT, i,
4933 0xff, HDA_AMP_MUTE);
4934 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4935 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4936 0xff, HDA_AMP_MUTE);
4941 * snd_hda_gen_stream_pm - Stream power management callback
4942 * @codec: the HDA codec
4943 * @nid: audio widget
4944 * @on: power on/off flag
4946 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
4948 void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4950 if (codec->power_save_node)
4951 set_path_power(codec, nid, -1, on);
4953 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4956 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4957 * set up the hda_gen_spec
4958 * @codec: the HDA codec
4959 * @cfg: Parsed pin configuration
4961 * return 1 if successful, 0 if the proper config is not found,
4962 * or a negative error code
4964 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4965 struct auto_pin_cfg *cfg)
4967 struct hda_gen_spec *spec = codec->spec;
4970 parse_user_hints(codec);
4972 if (spec->vmaster_mute_led || spec->mic_mute_led)
4973 snd_ctl_led_request();
4975 if (spec->mixer_nid && !spec->mixer_merge_nid)
4976 spec->mixer_merge_nid = spec->mixer_nid;
4978 if (cfg != &spec->autocfg) {
4979 spec->autocfg = *cfg;
4980 cfg = &spec->autocfg;
4983 if (!spec->main_out_badness)
4984 spec->main_out_badness = &hda_main_out_badness;
4985 if (!spec->extra_out_badness)
4986 spec->extra_out_badness = &hda_extra_out_badness;
4988 fill_all_dac_nids(codec);
4990 if (!cfg->line_outs) {
4991 if (cfg->dig_outs || cfg->dig_in_pin) {
4992 spec->multiout.max_channels = 2;
4993 spec->no_analog = 1;
4996 if (!cfg->num_inputs && !cfg->dig_in_pin)
4997 return 0; /* can't find valid BIOS pin config */
5000 if (!spec->no_primary_hp &&
5001 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
5002 cfg->line_outs <= cfg->hp_outs) {
5003 /* use HP as primary out */
5004 cfg->speaker_outs = cfg->line_outs;
5005 memcpy(cfg->speaker_pins, cfg->line_out_pins,
5006 sizeof(cfg->speaker_pins));
5007 cfg->line_outs = cfg->hp_outs;
5008 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
5010 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
5011 cfg->line_out_type = AUTO_PIN_HP_OUT;
5014 err = parse_output_paths(codec);
5017 err = create_multi_channel_mode(codec);
5020 err = create_multi_out_ctls(codec, cfg);
5023 err = create_hp_out_ctls(codec);
5026 err = create_speaker_out_ctls(codec);
5029 err = create_indep_hp_ctls(codec);
5032 err = create_loopback_mixing_ctl(codec);
5035 err = create_hp_mic(codec);
5038 err = create_input_ctls(codec);
5042 /* add power-down pin callbacks at first */
5043 add_all_pin_power_ctls(codec, false);
5045 spec->const_channel_count = spec->ext_channel_count;
5046 /* check the multiple speaker and headphone pins */
5047 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
5048 spec->const_channel_count = max(spec->const_channel_count,
5049 cfg->speaker_outs * 2);
5050 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
5051 spec->const_channel_count = max(spec->const_channel_count,
5053 spec->multiout.max_channels = max(spec->ext_channel_count,
5054 spec->const_channel_count);
5056 err = check_auto_mute_availability(codec);
5060 err = check_dyn_adc_switch(codec);
5064 err = check_auto_mic_availability(codec);
5068 /* add stereo mix if available and not enabled yet */
5069 if (!spec->auto_mic && spec->mixer_nid &&
5070 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
5071 spec->input_mux.num_items > 1) {
5072 err = parse_capture_source(codec, spec->mixer_nid,
5073 CFG_IDX_MIX, spec->num_all_adcs,
5080 err = create_capture_mixers(codec);
5084 err = parse_mic_boost(codec);
5088 /* create "Headphone Mic Jack Mode" if no input selection is
5089 * available (or user specifies add_jack_modes hint)
5091 if (spec->hp_mic_pin &&
5092 (spec->auto_mic || spec->input_mux.num_items == 1 ||
5093 spec->add_jack_modes)) {
5094 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
5099 if (spec->add_jack_modes) {
5100 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
5101 err = create_out_jack_modes(codec, cfg->line_outs,
5102 cfg->line_out_pins);
5106 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
5107 err = create_out_jack_modes(codec, cfg->hp_outs,
5114 /* add power-up pin callbacks at last */
5115 add_all_pin_power_ctls(codec, true);
5117 /* mute all aamix input initially */
5118 if (spec->mixer_nid)
5119 mute_all_mixer_nid(codec, spec->mixer_nid);
5122 parse_digital(codec);
5124 if (spec->power_down_unused || codec->power_save_node) {
5125 if (!codec->power_filter)
5126 codec->power_filter = snd_hda_gen_path_power_filter;
5127 if (!codec->patch_ops.stream_pm)
5128 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
5131 if (!spec->no_analog && spec->beep_nid) {
5132 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
5135 if (codec->beep && codec->power_save_node) {
5136 err = add_fake_beep_paths(codec);
5139 codec->beep->power_hook = beep_power_hook;
5145 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
5149 * Build control elements
5152 /* follower controls for virtual master */
5153 static const char * const follower_pfxs[] = {
5154 "Front", "Surround", "Center", "LFE", "Side",
5155 "Headphone", "Speaker", "Mono", "Line Out",
5156 "CLFE", "Bass Speaker", "PCM",
5157 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5158 "Headphone Front", "Headphone Surround", "Headphone CLFE",
5159 "Headphone Side", "Headphone+LO", "Speaker+LO",
5164 * snd_hda_gen_build_controls - Build controls from the parsed results
5165 * @codec: the HDA codec
5167 * Pass this to build_controls patch_ops.
5169 int snd_hda_gen_build_controls(struct hda_codec *codec)
5171 struct hda_gen_spec *spec = codec->spec;
5174 if (spec->kctls.used) {
5175 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5180 if (spec->multiout.dig_out_nid) {
5181 err = snd_hda_create_dig_out_ctls(codec,
5182 spec->multiout.dig_out_nid,
5183 spec->multiout.dig_out_nid,
5184 spec->pcm_rec[1]->pcm_type);
5187 if (!spec->no_analog) {
5188 err = snd_hda_create_spdif_share_sw(codec,
5192 spec->multiout.share_spdif = 1;
5195 if (spec->dig_in_nid) {
5196 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5201 /* if we have no master control, let's create it */
5202 if (!spec->no_analog && !spec->suppress_vmaster &&
5203 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
5204 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
5205 spec->vmaster_tlv, follower_pfxs,
5206 "Playback Volume", 0);
5210 if (!spec->no_analog && !spec->suppress_vmaster &&
5211 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5212 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5213 NULL, follower_pfxs,
5214 "Playback Switch", true,
5215 spec->vmaster_mute_led ?
5216 SNDRV_CTL_ELEM_ACCESS_SPK_LED : 0,
5217 &spec->vmaster_mute.sw_kctl);
5220 if (spec->vmaster_mute.hook) {
5221 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute);
5222 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5226 free_kctls(spec); /* no longer needed */
5228 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5234 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
5241 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5242 struct hda_codec *codec,
5243 struct snd_pcm_substream *substream,
5246 struct hda_gen_spec *spec = codec->spec;
5247 if (spec->pcm_playback_hook)
5248 spec->pcm_playback_hook(hinfo, codec, substream, action);
5251 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5252 struct hda_codec *codec,
5253 struct snd_pcm_substream *substream,
5256 struct hda_gen_spec *spec = codec->spec;
5257 if (spec->pcm_capture_hook)
5258 spec->pcm_capture_hook(hinfo, codec, substream, action);
5262 * Analog playback callbacks
5264 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5265 struct hda_codec *codec,
5266 struct snd_pcm_substream *substream)
5268 struct hda_gen_spec *spec = codec->spec;
5271 mutex_lock(&spec->pcm_mutex);
5272 err = snd_hda_multi_out_analog_open(codec,
5273 &spec->multiout, substream,
5276 spec->active_streams |= 1 << STREAM_MULTI_OUT;
5277 call_pcm_playback_hook(hinfo, codec, substream,
5278 HDA_GEN_PCM_ACT_OPEN);
5280 mutex_unlock(&spec->pcm_mutex);
5284 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5285 struct hda_codec *codec,
5286 unsigned int stream_tag,
5287 unsigned int format,
5288 struct snd_pcm_substream *substream)
5290 struct hda_gen_spec *spec = codec->spec;
5293 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5294 stream_tag, format, substream);
5296 call_pcm_playback_hook(hinfo, codec, substream,
5297 HDA_GEN_PCM_ACT_PREPARE);
5301 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5302 struct hda_codec *codec,
5303 struct snd_pcm_substream *substream)
5305 struct hda_gen_spec *spec = codec->spec;
5308 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5310 call_pcm_playback_hook(hinfo, codec, substream,
5311 HDA_GEN_PCM_ACT_CLEANUP);
5315 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5316 struct hda_codec *codec,
5317 struct snd_pcm_substream *substream)
5319 struct hda_gen_spec *spec = codec->spec;
5320 mutex_lock(&spec->pcm_mutex);
5321 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
5322 call_pcm_playback_hook(hinfo, codec, substream,
5323 HDA_GEN_PCM_ACT_CLOSE);
5324 mutex_unlock(&spec->pcm_mutex);
5328 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5329 struct hda_codec *codec,
5330 struct snd_pcm_substream *substream)
5332 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5336 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5337 struct hda_codec *codec,
5338 unsigned int stream_tag,
5339 unsigned int format,
5340 struct snd_pcm_substream *substream)
5342 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5343 call_pcm_capture_hook(hinfo, codec, substream,
5344 HDA_GEN_PCM_ACT_PREPARE);
5348 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5349 struct hda_codec *codec,
5350 struct snd_pcm_substream *substream)
5352 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5353 call_pcm_capture_hook(hinfo, codec, substream,
5354 HDA_GEN_PCM_ACT_CLEANUP);
5358 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5359 struct hda_codec *codec,
5360 struct snd_pcm_substream *substream)
5362 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5366 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5367 struct hda_codec *codec,
5368 struct snd_pcm_substream *substream)
5370 struct hda_gen_spec *spec = codec->spec;
5373 mutex_lock(&spec->pcm_mutex);
5374 if (spec->indep_hp && !spec->indep_hp_enabled)
5377 spec->active_streams |= 1 << STREAM_INDEP_HP;
5378 call_pcm_playback_hook(hinfo, codec, substream,
5379 HDA_GEN_PCM_ACT_OPEN);
5380 mutex_unlock(&spec->pcm_mutex);
5384 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5385 struct hda_codec *codec,
5386 struct snd_pcm_substream *substream)
5388 struct hda_gen_spec *spec = codec->spec;
5389 mutex_lock(&spec->pcm_mutex);
5390 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
5391 call_pcm_playback_hook(hinfo, codec, substream,
5392 HDA_GEN_PCM_ACT_CLOSE);
5393 mutex_unlock(&spec->pcm_mutex);
5397 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5398 struct hda_codec *codec,
5399 unsigned int stream_tag,
5400 unsigned int format,
5401 struct snd_pcm_substream *substream)
5403 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5404 call_pcm_playback_hook(hinfo, codec, substream,
5405 HDA_GEN_PCM_ACT_PREPARE);
5409 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5410 struct hda_codec *codec,
5411 struct snd_pcm_substream *substream)
5413 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5414 call_pcm_playback_hook(hinfo, codec, substream,
5415 HDA_GEN_PCM_ACT_CLEANUP);
5422 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5423 struct hda_codec *codec,
5424 struct snd_pcm_substream *substream)
5426 struct hda_gen_spec *spec = codec->spec;
5427 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5430 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5431 struct hda_codec *codec,
5432 unsigned int stream_tag,
5433 unsigned int format,
5434 struct snd_pcm_substream *substream)
5436 struct hda_gen_spec *spec = codec->spec;
5437 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5438 stream_tag, format, substream);
5441 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5442 struct hda_codec *codec,
5443 struct snd_pcm_substream *substream)
5445 struct hda_gen_spec *spec = codec->spec;
5446 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5449 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5450 struct hda_codec *codec,
5451 struct snd_pcm_substream *substream)
5453 struct hda_gen_spec *spec = codec->spec;
5454 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5460 #define alt_capture_pcm_open capture_pcm_open
5461 #define alt_capture_pcm_close capture_pcm_close
5463 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5464 struct hda_codec *codec,
5465 unsigned int stream_tag,
5466 unsigned int format,
5467 struct snd_pcm_substream *substream)
5469 struct hda_gen_spec *spec = codec->spec;
5471 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5472 stream_tag, 0, format);
5473 call_pcm_capture_hook(hinfo, codec, substream,
5474 HDA_GEN_PCM_ACT_PREPARE);
5478 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5479 struct hda_codec *codec,
5480 struct snd_pcm_substream *substream)
5482 struct hda_gen_spec *spec = codec->spec;
5484 snd_hda_codec_cleanup_stream(codec,
5485 spec->adc_nids[substream->number + 1]);
5486 call_pcm_capture_hook(hinfo, codec, substream,
5487 HDA_GEN_PCM_ACT_CLEANUP);
5493 static const struct hda_pcm_stream pcm_analog_playback = {
5497 /* NID is set in build_pcms */
5499 .open = playback_pcm_open,
5500 .close = playback_pcm_close,
5501 .prepare = playback_pcm_prepare,
5502 .cleanup = playback_pcm_cleanup
5506 static const struct hda_pcm_stream pcm_analog_capture = {
5510 /* NID is set in build_pcms */
5512 .open = capture_pcm_open,
5513 .close = capture_pcm_close,
5514 .prepare = capture_pcm_prepare,
5515 .cleanup = capture_pcm_cleanup
5519 static const struct hda_pcm_stream pcm_analog_alt_playback = {
5523 /* NID is set in build_pcms */
5525 .open = alt_playback_pcm_open,
5526 .close = alt_playback_pcm_close,
5527 .prepare = alt_playback_pcm_prepare,
5528 .cleanup = alt_playback_pcm_cleanup
5532 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5533 .substreams = 2, /* can be overridden */
5536 /* NID is set in build_pcms */
5538 .open = alt_capture_pcm_open,
5539 .close = alt_capture_pcm_close,
5540 .prepare = alt_capture_pcm_prepare,
5541 .cleanup = alt_capture_pcm_cleanup
5545 static const struct hda_pcm_stream pcm_digital_playback = {
5549 /* NID is set in build_pcms */
5551 .open = dig_playback_pcm_open,
5552 .close = dig_playback_pcm_close,
5553 .prepare = dig_playback_pcm_prepare,
5554 .cleanup = dig_playback_pcm_cleanup
5558 static const struct hda_pcm_stream pcm_digital_capture = {
5562 /* NID is set in build_pcms */
5565 /* Used by build_pcms to flag that a PCM has no playback stream */
5566 static const struct hda_pcm_stream pcm_null_stream = {
5573 * dynamic changing ADC PCM streams
5575 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5577 struct hda_gen_spec *spec = codec->spec;
5578 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5580 if (spec->cur_adc && spec->cur_adc != new_adc) {
5581 /* stream is running, let's swap the current ADC */
5582 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5583 spec->cur_adc = new_adc;
5584 snd_hda_codec_setup_stream(codec, new_adc,
5585 spec->cur_adc_stream_tag, 0,
5586 spec->cur_adc_format);
5592 /* analog capture with dynamic dual-adc changes */
5593 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5594 struct hda_codec *codec,
5595 unsigned int stream_tag,
5596 unsigned int format,
5597 struct snd_pcm_substream *substream)
5599 struct hda_gen_spec *spec = codec->spec;
5600 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5601 spec->cur_adc_stream_tag = stream_tag;
5602 spec->cur_adc_format = format;
5603 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5604 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
5608 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5609 struct hda_codec *codec,
5610 struct snd_pcm_substream *substream)
5612 struct hda_gen_spec *spec = codec->spec;
5613 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5615 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
5619 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5623 .nid = 0, /* fill later */
5625 .prepare = dyn_adc_capture_pcm_prepare,
5626 .cleanup = dyn_adc_capture_pcm_cleanup
5630 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5631 const char *chip_name)
5637 strscpy(str, chip_name, len);
5639 /* drop non-alnum chars after a space */
5640 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5641 if (!isalnum(p[1])) {
5646 strlcat(str, sfx, len);
5649 /* copy PCM stream info from @default_str, and override non-NULL entries
5650 * from @spec_str and @nid
5652 static void setup_pcm_stream(struct hda_pcm_stream *str,
5653 const struct hda_pcm_stream *default_str,
5654 const struct hda_pcm_stream *spec_str,
5657 *str = *default_str;
5661 if (spec_str->substreams)
5662 str->substreams = spec_str->substreams;
5663 if (spec_str->channels_min)
5664 str->channels_min = spec_str->channels_min;
5665 if (spec_str->channels_max)
5666 str->channels_max = spec_str->channels_max;
5667 if (spec_str->rates)
5668 str->rates = spec_str->rates;
5669 if (spec_str->formats)
5670 str->formats = spec_str->formats;
5671 if (spec_str->maxbps)
5672 str->maxbps = spec_str->maxbps;
5677 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5678 * @codec: the HDA codec
5680 * Pass this to build_pcms patch_ops.
5682 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5684 struct hda_gen_spec *spec = codec->spec;
5685 struct hda_pcm *info;
5686 bool have_multi_adcs;
5688 if (spec->no_analog)
5691 fill_pcm_stream_name(spec->stream_name_analog,
5692 sizeof(spec->stream_name_analog),
5693 " Analog", codec->core.chip_name);
5694 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5697 spec->pcm_rec[0] = info;
5699 if (spec->multiout.num_dacs > 0) {
5700 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5701 &pcm_analog_playback,
5702 spec->stream_analog_playback,
5703 spec->multiout.dac_nids[0]);
5704 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5705 spec->multiout.max_channels;
5706 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5707 spec->autocfg.line_outs == 2)
5708 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5711 if (spec->num_adc_nids) {
5712 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5713 (spec->dyn_adc_switch ?
5714 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5715 spec->stream_analog_capture,
5720 /* SPDIF for stream index #1 */
5721 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5722 fill_pcm_stream_name(spec->stream_name_digital,
5723 sizeof(spec->stream_name_digital),
5724 " Digital", codec->core.chip_name);
5725 info = snd_hda_codec_pcm_new(codec, "%s",
5726 spec->stream_name_digital);
5729 codec->follower_dig_outs = spec->multiout.follower_dig_outs;
5730 spec->pcm_rec[1] = info;
5731 if (spec->dig_out_type)
5732 info->pcm_type = spec->dig_out_type;
5734 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5735 if (spec->multiout.dig_out_nid)
5736 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5737 &pcm_digital_playback,
5738 spec->stream_digital_playback,
5739 spec->multiout.dig_out_nid);
5740 if (spec->dig_in_nid)
5741 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5742 &pcm_digital_capture,
5743 spec->stream_digital_capture,
5747 if (spec->no_analog)
5750 /* If the use of more than one ADC is requested for the current
5751 * model, configure a second analog capture-only PCM.
5753 have_multi_adcs = (spec->num_adc_nids > 1) &&
5754 !spec->dyn_adc_switch && !spec->auto_mic;
5755 /* Additional Analaog capture for index #2 */
5756 if (spec->alt_dac_nid || have_multi_adcs) {
5757 fill_pcm_stream_name(spec->stream_name_alt_analog,
5758 sizeof(spec->stream_name_alt_analog),
5759 " Alt Analog", codec->core.chip_name);
5760 info = snd_hda_codec_pcm_new(codec, "%s",
5761 spec->stream_name_alt_analog);
5764 spec->pcm_rec[2] = info;
5765 if (spec->alt_dac_nid)
5766 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5767 &pcm_analog_alt_playback,
5768 spec->stream_analog_alt_playback,
5771 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5772 &pcm_null_stream, NULL, 0);
5773 if (have_multi_adcs) {
5774 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5775 &pcm_analog_alt_capture,
5776 spec->stream_analog_alt_capture,
5778 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5779 spec->num_adc_nids - 1;
5781 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5782 &pcm_null_stream, NULL, 0);
5788 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5792 * Standard auto-parser initializations
5795 /* configure the given path as a proper output */
5796 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5798 struct nid_path *path;
5801 path = snd_hda_get_path_from_idx(codec, path_idx);
5802 if (!path || !path->depth)
5804 pin = path->path[path->depth - 1];
5805 restore_pin_ctl(codec, pin);
5806 snd_hda_activate_path(codec, path, path->active,
5807 aamix_default(codec->spec));
5808 set_pin_eapd(codec, pin, path->active);
5811 /* initialize primary output paths */
5812 static void init_multi_out(struct hda_codec *codec)
5814 struct hda_gen_spec *spec = codec->spec;
5817 for (i = 0; i < spec->autocfg.line_outs; i++)
5818 set_output_and_unmute(codec, spec->out_paths[i]);
5822 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5826 for (i = 0; i < num_outs; i++)
5827 set_output_and_unmute(codec, paths[i]);
5830 /* initialize hp and speaker paths */
5831 static void init_extra_out(struct hda_codec *codec)
5833 struct hda_gen_spec *spec = codec->spec;
5835 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5836 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5837 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5838 __init_extra_out(codec, spec->autocfg.speaker_outs,
5839 spec->speaker_paths);
5842 /* initialize multi-io paths */
5843 static void init_multi_io(struct hda_codec *codec)
5845 struct hda_gen_spec *spec = codec->spec;
5848 for (i = 0; i < spec->multi_ios; i++) {
5849 hda_nid_t pin = spec->multi_io[i].pin;
5850 struct nid_path *path;
5851 path = get_multiio_path(codec, i);
5854 if (!spec->multi_io[i].ctl_in)
5855 spec->multi_io[i].ctl_in =
5856 snd_hda_codec_get_pin_target(codec, pin);
5857 snd_hda_activate_path(codec, path, path->active,
5858 aamix_default(spec));
5862 static void init_aamix_paths(struct hda_codec *codec)
5864 struct hda_gen_spec *spec = codec->spec;
5866 if (!spec->have_aamix_ctl)
5868 if (!has_aamix_out_paths(spec))
5870 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5871 spec->aamix_out_paths[0],
5872 spec->autocfg.line_out_type);
5873 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5874 spec->aamix_out_paths[1],
5876 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5877 spec->aamix_out_paths[2],
5878 AUTO_PIN_SPEAKER_OUT);
5881 /* set up input pins and loopback paths */
5882 static void init_analog_input(struct hda_codec *codec)
5884 struct hda_gen_spec *spec = codec->spec;
5885 struct auto_pin_cfg *cfg = &spec->autocfg;
5888 for (i = 0; i < cfg->num_inputs; i++) {
5889 hda_nid_t nid = cfg->inputs[i].pin;
5890 if (is_input_pin(codec, nid))
5891 restore_pin_ctl(codec, nid);
5893 /* init loopback inputs */
5894 if (spec->mixer_nid) {
5895 resume_path_from_idx(codec, spec->loopback_paths[i]);
5896 resume_path_from_idx(codec, spec->loopback_merge_path);
5901 /* initialize ADC paths */
5902 static void init_input_src(struct hda_codec *codec)
5904 struct hda_gen_spec *spec = codec->spec;
5905 struct hda_input_mux *imux = &spec->input_mux;
5906 struct nid_path *path;
5909 if (spec->dyn_adc_switch)
5912 nums = spec->num_adc_nids;
5914 for (c = 0; c < nums; c++) {
5915 for (i = 0; i < imux->num_items; i++) {
5916 path = get_input_path(codec, c, i);
5918 bool active = path->active;
5919 if (i == spec->cur_mux[c])
5921 snd_hda_activate_path(codec, path, active, false);
5925 update_hp_mic(codec, c, true);
5928 if (spec->cap_sync_hook)
5929 spec->cap_sync_hook(codec, NULL, NULL);
5932 /* set right pin controls for digital I/O */
5933 static void init_digital(struct hda_codec *codec)
5935 struct hda_gen_spec *spec = codec->spec;
5939 for (i = 0; i < spec->autocfg.dig_outs; i++)
5940 set_output_and_unmute(codec, spec->digout_paths[i]);
5941 pin = spec->autocfg.dig_in_pin;
5943 restore_pin_ctl(codec, pin);
5944 resume_path_from_idx(codec, spec->digin_path);
5948 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5949 * invalid unsol tags by some reason
5951 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5953 const struct hda_pincfg *pin;
5956 snd_array_for_each(&codec->init_pins, i, pin) {
5957 hda_nid_t nid = pin->nid;
5958 if (is_jack_detectable(codec, nid) &&
5959 !snd_hda_jack_tbl_get(codec, nid))
5960 snd_hda_codec_write_cache(codec, nid, 0,
5961 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5966 * snd_hda_gen_init - initialize the generic spec
5967 * @codec: the HDA codec
5969 * This can be put as patch_ops init function.
5971 int snd_hda_gen_init(struct hda_codec *codec)
5973 struct hda_gen_spec *spec = codec->spec;
5975 if (spec->init_hook)
5976 spec->init_hook(codec);
5978 if (!spec->skip_verbs)
5979 snd_hda_apply_verbs(codec);
5981 init_multi_out(codec);
5982 init_extra_out(codec);
5983 init_multi_io(codec);
5984 init_aamix_paths(codec);
5985 init_analog_input(codec);
5986 init_input_src(codec);
5987 init_digital(codec);
5989 clear_unsol_on_unused_pins(codec);
5991 sync_all_pin_power_ctls(codec);
5993 /* call init functions of standard auto-mute helpers */
5994 update_automute_all(codec);
5996 snd_hda_regmap_sync(codec);
5998 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5999 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
6001 hda_call_check_power_status(codec, 0x01);
6004 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
6007 * snd_hda_gen_free - free the generic spec
6008 * @codec: the HDA codec
6010 * This can be put as patch_ops free function.
6012 void snd_hda_gen_free(struct hda_codec *codec)
6014 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
6015 snd_hda_gen_spec_free(codec->spec);
6019 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
6023 * snd_hda_gen_check_power_status - check the loopback power save state
6024 * @codec: the HDA codec
6025 * @nid: NID to inspect
6027 * This can be put as patch_ops check_power_status function.
6029 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
6031 struct hda_gen_spec *spec = codec->spec;
6032 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
6034 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
6039 * the generic codec support
6042 static const struct hda_codec_ops generic_patch_ops = {
6043 .build_controls = snd_hda_gen_build_controls,
6044 .build_pcms = snd_hda_gen_build_pcms,
6045 .init = snd_hda_gen_init,
6046 .free = snd_hda_gen_free,
6047 .unsol_event = snd_hda_jack_unsol_event,
6049 .check_power_status = snd_hda_gen_check_power_status,
6054 * snd_hda_parse_generic_codec - Generic codec parser
6055 * @codec: the HDA codec
6057 static int snd_hda_parse_generic_codec(struct hda_codec *codec)
6059 struct hda_gen_spec *spec;
6062 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
6065 snd_hda_gen_spec_init(spec);
6068 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
6072 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
6076 codec->patch_ops = generic_patch_ops;
6080 snd_hda_gen_free(codec);
6084 static const struct hda_device_id snd_hda_id_generic[] = {
6085 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
6088 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
6090 static struct hda_codec_driver generic_driver = {
6091 .id = snd_hda_id_generic,
6094 module_hda_codec_driver(generic_driver);
6096 MODULE_LICENSE("GPL");
6097 MODULE_DESCRIPTION("Generic HD-audio codec parser");