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 knew->private_value = val;
994 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
995 const char *pfx, const char *dir,
996 const char *sfx, int cidx, unsigned long val)
998 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
999 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
1000 if (!add_control(spec, type, name, cidx, val))
1005 #define add_pb_vol_ctrl(spec, type, pfx, val) \
1006 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1007 #define add_pb_sw_ctrl(spec, type, pfx, val) \
1008 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1009 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1010 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1011 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1012 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1014 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1015 unsigned int chs, struct nid_path *path)
1020 val = path->ctls[NID_PATH_VOL_CTL];
1023 val = amp_val_replace_channels(val, chs);
1024 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1027 /* return the channel bits suitable for the given path->ctls[] */
1028 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1031 int chs = 1; /* mono (left only) */
1033 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1034 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1035 chs = 3; /* stereo */
1040 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1041 struct nid_path *path)
1043 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1044 return add_vol_ctl(codec, pfx, cidx, chs, path);
1047 /* create a mute-switch for the given mixer widget;
1048 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1050 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1051 unsigned int chs, struct nid_path *path)
1054 int type = HDA_CTL_WIDGET_MUTE;
1058 val = path->ctls[NID_PATH_MUTE_CTL];
1061 val = amp_val_replace_channels(val, chs);
1062 if (get_amp_direction_(val) == HDA_INPUT) {
1063 hda_nid_t nid = get_amp_nid_(val);
1064 int nums = snd_hda_get_num_conns(codec, nid);
1066 type = HDA_CTL_BIND_MUTE;
1070 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1073 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1074 int cidx, struct nid_path *path)
1076 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1077 return add_sw_ctl(codec, pfx, cidx, chs, path);
1080 /* playback mute control with the software mute bit check */
1081 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1082 struct snd_ctl_elem_value *ucontrol)
1084 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1085 struct hda_gen_spec *spec = codec->spec;
1087 if (spec->auto_mute_via_amp) {
1088 hda_nid_t nid = get_amp_nid(kcontrol);
1089 bool enabled = !((spec->mute_bits >> nid) & 1);
1090 ucontrol->value.integer.value[0] &= enabled;
1091 ucontrol->value.integer.value[1] &= enabled;
1095 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1096 struct snd_ctl_elem_value *ucontrol)
1098 sync_auto_mute_bits(kcontrol, ucontrol);
1099 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1103 * Bound mute controls
1105 #define AMP_VAL_IDX_SHIFT 19
1106 #define AMP_VAL_IDX_MASK (0x0f<<19)
1108 static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
1109 struct snd_ctl_elem_value *ucontrol)
1111 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1115 mutex_lock(&codec->control_mutex);
1116 pval = kcontrol->private_value;
1117 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1118 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1119 kcontrol->private_value = pval;
1120 mutex_unlock(&codec->control_mutex);
1124 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1125 struct snd_ctl_elem_value *ucontrol)
1127 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1129 int i, indices, err = 0, change = 0;
1131 sync_auto_mute_bits(kcontrol, ucontrol);
1133 mutex_lock(&codec->control_mutex);
1134 pval = kcontrol->private_value;
1135 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1136 for (i = 0; i < indices; i++) {
1137 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1138 (i << AMP_VAL_IDX_SHIFT);
1139 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1144 kcontrol->private_value = pval;
1145 mutex_unlock(&codec->control_mutex);
1146 return err < 0 ? err : change;
1149 /* any ctl assigned to the path with the given index? */
1150 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1152 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1153 return path && path->ctls[ctl_type];
1156 static const char * const channel_name[] = {
1157 "Front", "Surround", "CLFE", "Side", "Back",
1160 /* give some appropriate ctl name prefix for the given line out channel */
1161 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1162 int *index, int ctl_type)
1164 struct hda_gen_spec *spec = codec->spec;
1165 struct auto_pin_cfg *cfg = &spec->autocfg;
1168 if (cfg->line_outs == 1 && !spec->multi_ios &&
1169 !codec->force_pin_prefix &&
1170 !cfg->hp_outs && !cfg->speaker_outs)
1171 return spec->vmaster_mute.hook ? "PCM" : "Master";
1173 /* if there is really a single DAC used in the whole output paths,
1174 * use it master (or "PCM" if a vmaster hook is present)
1176 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1177 !codec->force_pin_prefix &&
1178 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1179 return spec->vmaster_mute.hook ? "PCM" : "Master";
1181 /* multi-io channels */
1182 if (ch >= cfg->line_outs)
1185 switch (cfg->line_out_type) {
1186 case AUTO_PIN_SPEAKER_OUT:
1187 /* if the primary channel vol/mute is shared with HP volume,
1188 * don't name it as Speaker
1190 if (!ch && cfg->hp_outs &&
1191 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1193 if (cfg->line_outs == 1)
1195 if (cfg->line_outs == 2)
1196 return ch ? "Bass Speaker" : "Speaker";
1198 case AUTO_PIN_HP_OUT:
1199 /* if the primary channel vol/mute is shared with spk volume,
1200 * don't name it as Headphone
1202 if (!ch && cfg->speaker_outs &&
1203 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1205 /* for multi-io case, only the primary out */
1206 if (ch && spec->multi_ios)
1210 case AUTO_PIN_LINE_OUT:
1211 /* This deals with the case where one HP or one Speaker or
1212 * one HP + one Speaker need to share the DAC with LO
1215 bool hp_lo_shared = false, spk_lo_shared = false;
1217 if (cfg->speaker_outs)
1218 spk_lo_shared = !path_has_mixer(codec,
1219 spec->speaker_paths[0], ctl_type);
1221 hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1222 if (hp_lo_shared && spk_lo_shared)
1223 return spec->vmaster_mute.hook ? "PCM" : "Master";
1225 return "Headphone+LO";
1227 return "Speaker+LO";
1231 /* for a single channel output, we don't have to name the channel */
1232 if (cfg->line_outs == 1 && !spec->multi_ios)
1236 if (ch >= ARRAY_SIZE(channel_name)) {
1241 return channel_name[ch];
1245 * Parse output paths
1248 /* badness definition */
1250 /* No primary DAC is found for the main output */
1251 BAD_NO_PRIMARY_DAC = 0x10000,
1252 /* No DAC is found for the extra output */
1253 BAD_NO_DAC = 0x4000,
1254 /* No possible multi-ios */
1255 BAD_MULTI_IO = 0x120,
1256 /* No individual DAC for extra output */
1257 BAD_NO_EXTRA_DAC = 0x102,
1258 /* No individual DAC for extra surrounds */
1259 BAD_NO_EXTRA_SURR_DAC = 0x101,
1260 /* Primary DAC shared with main surrounds */
1261 BAD_SHARED_SURROUND = 0x100,
1262 /* No independent HP possible */
1263 BAD_NO_INDEP_HP = 0x10,
1264 /* Primary DAC shared with main CLFE */
1265 BAD_SHARED_CLFE = 0x10,
1266 /* Primary DAC shared with extra surrounds */
1267 BAD_SHARED_EXTRA_SURROUND = 0x10,
1268 /* Volume widget is shared */
1269 BAD_SHARED_VOL = 0x10,
1272 /* look for widgets in the given path which are appropriate for
1273 * volume and mute controls, and assign the values to ctls[].
1275 * When no appropriate widget is found in the path, the badness value
1276 * is incremented depending on the situation. The function returns the
1277 * total badness for both volume and mute controls.
1279 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1281 struct hda_gen_spec *spec = codec->spec;
1287 return BAD_SHARED_VOL * 2;
1289 if (path->ctls[NID_PATH_VOL_CTL] ||
1290 path->ctls[NID_PATH_MUTE_CTL])
1291 return 0; /* already evaluated */
1293 nid = look_for_out_vol_nid(codec, path);
1295 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1296 if (spec->dac_min_mute)
1297 val |= HDA_AMP_VAL_MIN_MUTE;
1298 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1299 badness += BAD_SHARED_VOL;
1301 path->ctls[NID_PATH_VOL_CTL] = val;
1303 badness += BAD_SHARED_VOL;
1304 nid = look_for_out_mute_nid(codec, path);
1306 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1307 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1308 nid_has_mute(codec, nid, HDA_OUTPUT))
1309 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1311 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1312 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1313 badness += BAD_SHARED_VOL;
1315 path->ctls[NID_PATH_MUTE_CTL] = val;
1317 badness += BAD_SHARED_VOL;
1321 const struct badness_table hda_main_out_badness = {
1322 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1323 .no_dac = BAD_NO_DAC,
1324 .shared_primary = BAD_NO_PRIMARY_DAC,
1325 .shared_surr = BAD_SHARED_SURROUND,
1326 .shared_clfe = BAD_SHARED_CLFE,
1327 .shared_surr_main = BAD_SHARED_SURROUND,
1329 EXPORT_SYMBOL_GPL(hda_main_out_badness);
1331 const struct badness_table hda_extra_out_badness = {
1332 .no_primary_dac = BAD_NO_DAC,
1333 .no_dac = BAD_NO_DAC,
1334 .shared_primary = BAD_NO_EXTRA_DAC,
1335 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1336 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1337 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1339 EXPORT_SYMBOL_GPL(hda_extra_out_badness);
1341 /* get the DAC of the primary output corresponding to the given array index */
1342 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1344 struct hda_gen_spec *spec = codec->spec;
1345 struct auto_pin_cfg *cfg = &spec->autocfg;
1347 if (cfg->line_outs > idx)
1348 return spec->private_dac_nids[idx];
1349 idx -= cfg->line_outs;
1350 if (spec->multi_ios > idx)
1351 return spec->multi_io[idx].dac;
1355 /* return the DAC if it's reachable, otherwise zero */
1356 static inline hda_nid_t try_dac(struct hda_codec *codec,
1357 hda_nid_t dac, hda_nid_t pin)
1359 return is_reachable_path(codec, dac, pin) ? dac : 0;
1362 /* try to assign DACs to pins and return the resultant badness */
1363 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1364 const hda_nid_t *pins, hda_nid_t *dacs,
1366 const struct badness_table *bad)
1368 struct hda_gen_spec *spec = codec->spec;
1376 for (i = 0; i < num_outs; i++) {
1377 struct nid_path *path;
1378 hda_nid_t pin = pins[i];
1380 if (!spec->obey_preferred_dacs) {
1381 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1383 badness += assign_out_path_ctls(codec, path);
1388 dacs[i] = get_preferred_dac(codec, pin);
1390 if (is_dac_already_used(codec, dacs[i]))
1391 badness += bad->shared_primary;
1392 } else if (spec->obey_preferred_dacs) {
1393 badness += BAD_NO_PRIMARY_DAC;
1397 dacs[i] = look_for_dac(codec, pin, false);
1398 if (!dacs[i] && !i) {
1399 /* try to steal the DAC of surrounds for the front */
1400 for (j = 1; j < num_outs; j++) {
1401 if (is_reachable_path(codec, dacs[j], pin)) {
1404 invalidate_nid_path(codec, path_idx[j]);
1413 dac = try_dac(codec, get_primary_out(codec, i), pin);
1415 dac = try_dac(codec, dacs[0], pin);
1417 dac = try_dac(codec, get_primary_out(codec, i), pin);
1420 badness += bad->shared_primary;
1422 badness += bad->shared_surr;
1424 badness += bad->shared_clfe;
1425 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1426 dac = spec->private_dac_nids[0];
1427 badness += bad->shared_surr_main;
1429 badness += bad->no_primary_dac;
1431 badness += bad->no_dac;
1435 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1436 if (!path && !i && spec->mixer_nid) {
1437 /* try with aamix */
1438 path = snd_hda_add_new_path(codec, dac, pin, 0);
1442 badness += bad->no_dac;
1444 /* print_nid_path(codec, "output", path); */
1445 path->active = true;
1446 path_idx[i] = snd_hda_get_path_idx(codec, path);
1447 badness += assign_out_path_ctls(codec, path);
1454 /* return NID if the given pin has only a single connection to a certain DAC */
1455 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1457 struct hda_gen_spec *spec = codec->spec;
1459 hda_nid_t nid_found = 0;
1461 for (i = 0; i < spec->num_all_dacs; i++) {
1462 hda_nid_t nid = spec->all_dacs[i];
1463 if (!nid || is_dac_already_used(codec, nid))
1465 if (is_reachable_path(codec, nid, pin)) {
1474 /* check whether the given pin can be a multi-io pin */
1475 static bool can_be_multiio_pin(struct hda_codec *codec,
1476 unsigned int location, hda_nid_t nid)
1478 unsigned int defcfg, caps;
1480 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1481 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1483 if (location && get_defcfg_location(defcfg) != location)
1485 caps = snd_hda_query_pin_caps(codec, nid);
1486 if (!(caps & AC_PINCAP_OUT))
1491 /* count the number of input pins that are capable to be multi-io */
1492 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1494 struct hda_gen_spec *spec = codec->spec;
1495 struct auto_pin_cfg *cfg = &spec->autocfg;
1496 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1497 unsigned int location = get_defcfg_location(defcfg);
1501 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1502 for (i = 0; i < cfg->num_inputs; i++) {
1503 if (cfg->inputs[i].type != type)
1505 if (can_be_multiio_pin(codec, location,
1506 cfg->inputs[i].pin))
1516 * When hardwired is set, try to fill ony hardwired pins, and returns
1517 * zero if any pins are filled, non-zero if nothing found.
1518 * When hardwired is off, try to fill possible input pins, and returns
1519 * the badness value.
1521 static int fill_multi_ios(struct hda_codec *codec,
1522 hda_nid_t reference_pin,
1525 struct hda_gen_spec *spec = codec->spec;
1526 struct auto_pin_cfg *cfg = &spec->autocfg;
1527 int type, i, j, num_pins, old_pins;
1528 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1529 unsigned int location = get_defcfg_location(defcfg);
1531 struct nid_path *path;
1533 old_pins = spec->multi_ios;
1537 num_pins = count_multiio_pins(codec, reference_pin);
1541 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1542 for (i = 0; i < cfg->num_inputs; i++) {
1543 hda_nid_t nid = cfg->inputs[i].pin;
1546 if (cfg->inputs[i].type != type)
1548 if (!can_be_multiio_pin(codec, location, nid))
1550 for (j = 0; j < spec->multi_ios; j++) {
1551 if (nid == spec->multi_io[j].pin)
1554 if (j < spec->multi_ios)
1558 dac = get_dac_if_single(codec, nid);
1560 dac = look_for_dac(codec, nid, false);
1565 path = snd_hda_add_new_path(codec, dac, nid,
1571 /* print_nid_path(codec, "multiio", path); */
1572 spec->multi_io[spec->multi_ios].pin = nid;
1573 spec->multi_io[spec->multi_ios].dac = dac;
1574 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1575 snd_hda_get_path_idx(codec, path);
1577 if (spec->multi_ios >= 2)
1583 badness = BAD_MULTI_IO;
1584 if (old_pins == spec->multi_ios) {
1586 return 1; /* nothing found */
1588 return badness; /* no badness if nothing found */
1590 if (!hardwired && spec->multi_ios < 2) {
1591 /* cancel newly assigned paths */
1592 spec->paths.used -= spec->multi_ios - old_pins;
1593 spec->multi_ios = old_pins;
1597 /* assign volume and mute controls */
1598 for (i = old_pins; i < spec->multi_ios; i++) {
1599 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1600 badness += assign_out_path_ctls(codec, path);
1606 /* map DACs for all pins in the list if they are single connections */
1607 static bool map_singles(struct hda_codec *codec, int outs,
1608 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1610 struct hda_gen_spec *spec = codec->spec;
1613 for (i = 0; i < outs; i++) {
1614 struct nid_path *path;
1618 dac = get_dac_if_single(codec, pins[i]);
1621 path = snd_hda_add_new_path(codec, dac, pins[i],
1623 if (!path && !i && spec->mixer_nid)
1624 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1628 /* print_nid_path(codec, "output", path); */
1629 path->active = true;
1630 path_idx[i] = snd_hda_get_path_idx(codec, path);
1636 static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1638 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1639 spec->aamix_out_paths[2];
1642 /* create a new path including aamix if available, and return its index */
1643 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1645 struct hda_gen_spec *spec = codec->spec;
1646 struct nid_path *path;
1647 hda_nid_t path_dac, dac, pin;
1649 path = snd_hda_get_path_from_idx(codec, path_idx);
1650 if (!path || !path->depth ||
1651 is_nid_contained(path, spec->mixer_nid))
1653 path_dac = path->path[0];
1654 dac = spec->private_dac_nids[0];
1655 pin = path->path[path->depth - 1];
1656 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1658 if (dac != path_dac)
1660 else if (spec->multiout.hp_out_nid[0])
1661 dac = spec->multiout.hp_out_nid[0];
1662 else if (spec->multiout.extra_out_nid[0])
1663 dac = spec->multiout.extra_out_nid[0];
1667 path = snd_hda_add_new_path(codec, dac, pin,
1672 /* print_nid_path(codec, "output-aamix", path); */
1673 path->active = false; /* unused as default */
1674 path->pin_fixed = true; /* static route */
1675 return snd_hda_get_path_idx(codec, path);
1678 /* check whether the independent HP is available with the current config */
1679 static bool indep_hp_possible(struct hda_codec *codec)
1681 struct hda_gen_spec *spec = codec->spec;
1682 struct auto_pin_cfg *cfg = &spec->autocfg;
1683 struct nid_path *path;
1686 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1687 idx = spec->out_paths[0];
1689 idx = spec->hp_paths[0];
1690 path = snd_hda_get_path_from_idx(codec, idx);
1694 /* assume no path conflicts unless aamix is involved */
1695 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1698 /* check whether output paths contain aamix */
1699 for (i = 0; i < cfg->line_outs; i++) {
1700 if (spec->out_paths[i] == idx)
1702 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1703 if (path && is_nid_contained(path, spec->mixer_nid))
1706 for (i = 0; i < cfg->speaker_outs; i++) {
1707 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1708 if (path && is_nid_contained(path, spec->mixer_nid))
1715 /* fill the empty entries in the dac array for speaker/hp with the
1716 * shared dac pointed by the paths
1718 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1719 hda_nid_t *dacs, int *path_idx)
1721 struct nid_path *path;
1724 for (i = 0; i < num_outs; i++) {
1727 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1730 dacs[i] = path->path[0];
1734 /* fill in the dac_nids table from the parsed pin configuration */
1735 static int fill_and_eval_dacs(struct hda_codec *codec,
1736 bool fill_hardwired,
1737 bool fill_mio_first)
1739 struct hda_gen_spec *spec = codec->spec;
1740 struct auto_pin_cfg *cfg = &spec->autocfg;
1741 int i, err, badness;
1743 /* set num_dacs once to full for look_for_dac() */
1744 spec->multiout.num_dacs = cfg->line_outs;
1745 spec->multiout.dac_nids = spec->private_dac_nids;
1746 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1747 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1748 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1749 spec->multi_ios = 0;
1750 snd_array_free(&spec->paths);
1752 /* clear path indices */
1753 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1754 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1755 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1756 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1757 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1758 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1759 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1760 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1764 /* fill hard-wired DACs first */
1765 if (fill_hardwired) {
1768 mapped = map_singles(codec, cfg->line_outs,
1770 spec->private_dac_nids,
1772 mapped |= map_singles(codec, cfg->hp_outs,
1774 spec->multiout.hp_out_nid,
1776 mapped |= map_singles(codec, cfg->speaker_outs,
1778 spec->multiout.extra_out_nid,
1779 spec->speaker_paths);
1780 if (!spec->no_multi_io &&
1781 fill_mio_first && cfg->line_outs == 1 &&
1782 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1783 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1790 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1791 spec->private_dac_nids, spec->out_paths,
1792 spec->main_out_badness);
1794 if (!spec->no_multi_io && fill_mio_first &&
1795 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1796 /* try to fill multi-io first */
1797 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1800 /* we don't count badness at this stage yet */
1803 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1804 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1805 spec->multiout.hp_out_nid,
1807 spec->extra_out_badness);
1812 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1813 err = try_assign_dacs(codec, cfg->speaker_outs,
1815 spec->multiout.extra_out_nid,
1816 spec->speaker_paths,
1817 spec->extra_out_badness);
1822 if (!spec->no_multi_io &&
1823 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1824 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1830 if (spec->mixer_nid) {
1831 spec->aamix_out_paths[0] =
1832 check_aamix_out_path(codec, spec->out_paths[0]);
1833 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1834 spec->aamix_out_paths[1] =
1835 check_aamix_out_path(codec, spec->hp_paths[0]);
1836 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1837 spec->aamix_out_paths[2] =
1838 check_aamix_out_path(codec, spec->speaker_paths[0]);
1841 if (!spec->no_multi_io &&
1842 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1843 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1844 spec->multi_ios = 1; /* give badness */
1846 /* re-count num_dacs and squash invalid entries */
1847 spec->multiout.num_dacs = 0;
1848 for (i = 0; i < cfg->line_outs; i++) {
1849 if (spec->private_dac_nids[i])
1850 spec->multiout.num_dacs++;
1852 memmove(spec->private_dac_nids + i,
1853 spec->private_dac_nids + i + 1,
1854 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1855 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1859 spec->ext_channel_count = spec->min_channel_count =
1860 spec->multiout.num_dacs * 2;
1862 if (spec->multi_ios == 2) {
1863 for (i = 0; i < 2; i++)
1864 spec->private_dac_nids[spec->multiout.num_dacs++] =
1865 spec->multi_io[i].dac;
1866 } else if (spec->multi_ios) {
1867 spec->multi_ios = 0;
1868 badness += BAD_MULTI_IO;
1871 if (spec->indep_hp && !indep_hp_possible(codec))
1872 badness += BAD_NO_INDEP_HP;
1874 /* re-fill the shared DAC for speaker / headphone */
1875 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1876 refill_shared_dacs(codec, cfg->hp_outs,
1877 spec->multiout.hp_out_nid,
1879 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1880 refill_shared_dacs(codec, cfg->speaker_outs,
1881 spec->multiout.extra_out_nid,
1882 spec->speaker_paths);
1887 #define DEBUG_BADNESS
1889 #ifdef DEBUG_BADNESS
1890 #define debug_badness(fmt, ...) \
1891 codec_dbg(codec, fmt, ##__VA_ARGS__)
1893 #define debug_badness(fmt, ...) \
1894 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1897 #ifdef DEBUG_BADNESS
1898 static inline void print_nid_path_idx(struct hda_codec *codec,
1899 const char *pfx, int idx)
1901 struct nid_path *path;
1903 path = snd_hda_get_path_from_idx(codec, idx);
1905 print_nid_path(codec, pfx, path);
1908 static void debug_show_configs(struct hda_codec *codec,
1909 struct auto_pin_cfg *cfg)
1911 struct hda_gen_spec *spec = codec->spec;
1912 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1915 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1916 cfg->line_out_pins[0], cfg->line_out_pins[1],
1917 cfg->line_out_pins[2], cfg->line_out_pins[3],
1918 spec->multiout.dac_nids[0],
1919 spec->multiout.dac_nids[1],
1920 spec->multiout.dac_nids[2],
1921 spec->multiout.dac_nids[3],
1922 lo_type[cfg->line_out_type]);
1923 for (i = 0; i < cfg->line_outs; i++)
1924 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1925 if (spec->multi_ios > 0)
1926 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1928 spec->multi_io[0].pin, spec->multi_io[1].pin,
1929 spec->multi_io[0].dac, spec->multi_io[1].dac);
1930 for (i = 0; i < spec->multi_ios; i++)
1931 print_nid_path_idx(codec, " mio",
1932 spec->out_paths[cfg->line_outs + i]);
1934 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1935 cfg->hp_pins[0], cfg->hp_pins[1],
1936 cfg->hp_pins[2], cfg->hp_pins[3],
1937 spec->multiout.hp_out_nid[0],
1938 spec->multiout.hp_out_nid[1],
1939 spec->multiout.hp_out_nid[2],
1940 spec->multiout.hp_out_nid[3]);
1941 for (i = 0; i < cfg->hp_outs; i++)
1942 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1943 if (cfg->speaker_outs)
1944 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1945 cfg->speaker_pins[0], cfg->speaker_pins[1],
1946 cfg->speaker_pins[2], cfg->speaker_pins[3],
1947 spec->multiout.extra_out_nid[0],
1948 spec->multiout.extra_out_nid[1],
1949 spec->multiout.extra_out_nid[2],
1950 spec->multiout.extra_out_nid[3]);
1951 for (i = 0; i < cfg->speaker_outs; i++)
1952 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1953 for (i = 0; i < 3; i++)
1954 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1957 #define debug_show_configs(codec, cfg) /* NOP */
1960 /* find all available DACs of the codec */
1961 static void fill_all_dac_nids(struct hda_codec *codec)
1963 struct hda_gen_spec *spec = codec->spec;
1966 spec->num_all_dacs = 0;
1967 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1968 for_each_hda_codec_node(nid, codec) {
1969 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1971 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1972 codec_err(codec, "Too many DACs!\n");
1975 spec->all_dacs[spec->num_all_dacs++] = nid;
1979 static int parse_output_paths(struct hda_codec *codec)
1981 struct hda_gen_spec *spec = codec->spec;
1982 struct auto_pin_cfg *cfg = &spec->autocfg;
1983 struct auto_pin_cfg *best_cfg;
1985 int best_badness = INT_MAX;
1987 bool fill_hardwired = true, fill_mio_first = true;
1988 bool best_wired = true, best_mio = true;
1989 bool hp_spk_swapped = false;
1991 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1997 badness = fill_and_eval_dacs(codec, fill_hardwired,
2003 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
2004 cfg->line_out_type, fill_hardwired, fill_mio_first,
2006 debug_show_configs(codec, cfg);
2007 if (badness < best_badness) {
2008 best_badness = badness;
2010 best_wired = fill_hardwired;
2011 best_mio = fill_mio_first;
2015 fill_mio_first = !fill_mio_first;
2016 if (!fill_mio_first)
2018 fill_hardwired = !fill_hardwired;
2019 if (!fill_hardwired)
2023 hp_spk_swapped = true;
2024 if (cfg->speaker_outs > 0 &&
2025 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2026 cfg->hp_outs = cfg->line_outs;
2027 memcpy(cfg->hp_pins, cfg->line_out_pins,
2028 sizeof(cfg->hp_pins));
2029 cfg->line_outs = cfg->speaker_outs;
2030 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2031 sizeof(cfg->speaker_pins));
2032 cfg->speaker_outs = 0;
2033 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2034 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2035 fill_hardwired = true;
2038 if (cfg->hp_outs > 0 &&
2039 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2040 cfg->speaker_outs = cfg->line_outs;
2041 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2042 sizeof(cfg->speaker_pins));
2043 cfg->line_outs = cfg->hp_outs;
2044 memcpy(cfg->line_out_pins, cfg->hp_pins,
2045 sizeof(cfg->hp_pins));
2047 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2048 cfg->line_out_type = AUTO_PIN_HP_OUT;
2049 fill_hardwired = true;
2056 debug_badness("==> restoring best_cfg\n");
2058 fill_and_eval_dacs(codec, best_wired, best_mio);
2060 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2061 cfg->line_out_type, best_wired, best_mio);
2062 debug_show_configs(codec, cfg);
2064 if (cfg->line_out_pins[0]) {
2065 struct nid_path *path;
2066 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
2068 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
2069 if (spec->vmaster_nid) {
2070 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2071 HDA_OUTPUT, spec->vmaster_tlv);
2072 if (spec->dac_min_mute)
2073 spec->vmaster_tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] |= TLV_DB_SCALE_MUTE;
2077 /* set initial pinctl targets */
2078 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2082 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2083 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2084 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2085 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2086 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2087 set_pin_targets(codec, cfg->speaker_outs,
2088 cfg->speaker_pins, val);
2091 /* clear indep_hp flag if not available */
2092 if (spec->indep_hp && !indep_hp_possible(codec))
2099 /* add playback controls from the parsed DAC table */
2100 static int create_multi_out_ctls(struct hda_codec *codec,
2101 const struct auto_pin_cfg *cfg)
2103 struct hda_gen_spec *spec = codec->spec;
2104 int i, err, noutputs;
2106 noutputs = cfg->line_outs;
2107 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2108 noutputs += spec->multi_ios;
2110 for (i = 0; i < noutputs; i++) {
2113 struct nid_path *path;
2115 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
2119 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
2120 if (!name || !strcmp(name, "CLFE")) {
2122 err = add_vol_ctl(codec, "Center", 0, 1, path);
2125 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2129 err = add_stereo_vol(codec, name, index, path);
2134 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2135 if (!name || !strcmp(name, "CLFE")) {
2136 err = add_sw_ctl(codec, "Center", 0, 1, path);
2139 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2143 err = add_stereo_sw(codec, name, index, path);
2151 static int create_extra_out(struct hda_codec *codec, int path_idx,
2152 const char *pfx, int cidx)
2154 struct nid_path *path;
2157 path = snd_hda_get_path_from_idx(codec, path_idx);
2160 err = add_stereo_vol(codec, pfx, cidx, path);
2163 err = add_stereo_sw(codec, pfx, cidx, path);
2169 /* add playback controls for speaker and HP outputs */
2170 static int create_extra_outs(struct hda_codec *codec, int num_pins,
2171 const int *paths, const char *pfx)
2175 for (i = 0; i < num_pins; i++) {
2177 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2180 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2181 name = "Bass Speaker";
2182 else if (num_pins >= 3) {
2183 snprintf(tmp, sizeof(tmp), "%s %s",
2184 pfx, channel_name[i]);
2190 err = create_extra_out(codec, paths[i], name, idx);
2197 static int create_hp_out_ctls(struct hda_codec *codec)
2199 struct hda_gen_spec *spec = codec->spec;
2200 return create_extra_outs(codec, spec->autocfg.hp_outs,
2205 static int create_speaker_out_ctls(struct hda_codec *codec)
2207 struct hda_gen_spec *spec = codec->spec;
2208 return create_extra_outs(codec, spec->autocfg.speaker_outs,
2209 spec->speaker_paths,
2214 * independent HP controls
2217 static void call_hp_automute(struct hda_codec *codec,
2218 struct hda_jack_callback *jack);
2219 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2220 struct snd_ctl_elem_info *uinfo)
2222 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2225 static int indep_hp_get(struct snd_kcontrol *kcontrol,
2226 struct snd_ctl_elem_value *ucontrol)
2228 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2229 struct hda_gen_spec *spec = codec->spec;
2230 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2234 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2235 int nomix_path_idx, int mix_path_idx,
2238 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2239 struct snd_ctl_elem_value *ucontrol)
2241 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2242 struct hda_gen_spec *spec = codec->spec;
2243 unsigned int select = ucontrol->value.enumerated.item[0];
2246 mutex_lock(&spec->pcm_mutex);
2247 if (spec->active_streams) {
2252 if (spec->indep_hp_enabled != select) {
2254 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2255 dacp = &spec->private_dac_nids[0];
2257 dacp = &spec->multiout.hp_out_nid[0];
2259 /* update HP aamix paths in case it conflicts with indep HP */
2260 if (spec->have_aamix_ctl) {
2261 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2262 update_aamix_paths(codec, spec->aamix_mode,
2264 spec->aamix_out_paths[0],
2265 spec->autocfg.line_out_type);
2267 update_aamix_paths(codec, spec->aamix_mode,
2269 spec->aamix_out_paths[1],
2273 spec->indep_hp_enabled = select;
2274 if (spec->indep_hp_enabled)
2277 *dacp = spec->alt_dac_nid;
2279 call_hp_automute(codec, NULL);
2283 mutex_unlock(&spec->pcm_mutex);
2287 static const struct snd_kcontrol_new indep_hp_ctl = {
2288 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2289 .name = "Independent HP",
2290 .info = indep_hp_info,
2291 .get = indep_hp_get,
2292 .put = indep_hp_put,
2296 static int create_indep_hp_ctls(struct hda_codec *codec)
2298 struct hda_gen_spec *spec = codec->spec;
2301 if (!spec->indep_hp)
2303 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2304 dac = spec->multiout.dac_nids[0];
2306 dac = spec->multiout.hp_out_nid[0];
2312 spec->indep_hp_enabled = false;
2313 spec->alt_dac_nid = dac;
2314 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2320 * channel mode enum control
2323 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2324 struct snd_ctl_elem_info *uinfo)
2326 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2327 struct hda_gen_spec *spec = codec->spec;
2330 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2332 uinfo->value.enumerated.items = spec->multi_ios + 1;
2333 if (uinfo->value.enumerated.item > spec->multi_ios)
2334 uinfo->value.enumerated.item = spec->multi_ios;
2335 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2336 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2340 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2341 struct snd_ctl_elem_value *ucontrol)
2343 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2344 struct hda_gen_spec *spec = codec->spec;
2345 ucontrol->value.enumerated.item[0] =
2346 (spec->ext_channel_count - spec->min_channel_count) / 2;
2350 static inline struct nid_path *
2351 get_multiio_path(struct hda_codec *codec, int idx)
2353 struct hda_gen_spec *spec = codec->spec;
2354 return snd_hda_get_path_from_idx(codec,
2355 spec->out_paths[spec->autocfg.line_outs + idx]);
2358 static void update_automute_all(struct hda_codec *codec);
2360 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2361 * used for output paths
2363 static bool aamix_default(struct hda_gen_spec *spec)
2365 return !spec->have_aamix_ctl || spec->aamix_mode;
2368 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2370 struct hda_gen_spec *spec = codec->spec;
2371 hda_nid_t nid = spec->multi_io[idx].pin;
2372 struct nid_path *path;
2374 path = get_multiio_path(codec, idx);
2378 if (path->active == output)
2382 set_pin_target(codec, nid, PIN_OUT, true);
2383 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2384 set_pin_eapd(codec, nid, true);
2386 set_pin_eapd(codec, nid, false);
2387 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2388 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2389 path_power_down_sync(codec, path);
2392 /* update jack retasking in case it modifies any of them */
2393 update_automute_all(codec);
2398 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2399 struct snd_ctl_elem_value *ucontrol)
2401 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2402 struct hda_gen_spec *spec = codec->spec;
2405 ch = ucontrol->value.enumerated.item[0];
2406 if (ch < 0 || ch > spec->multi_ios)
2408 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2410 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2411 for (i = 0; i < spec->multi_ios; i++)
2412 set_multi_io(codec, i, i < ch);
2413 spec->multiout.max_channels = max(spec->ext_channel_count,
2414 spec->const_channel_count);
2415 if (spec->need_dac_fix)
2416 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2420 static const struct snd_kcontrol_new channel_mode_enum = {
2421 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2422 .name = "Channel Mode",
2423 .info = ch_mode_info,
2428 static int create_multi_channel_mode(struct hda_codec *codec)
2430 struct hda_gen_spec *spec = codec->spec;
2432 if (spec->multi_ios > 0) {
2433 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2440 * aamix loopback enable/disable switch
2443 #define loopback_mixing_info indep_hp_info
2445 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2446 struct snd_ctl_elem_value *ucontrol)
2448 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2449 struct hda_gen_spec *spec = codec->spec;
2450 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2454 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2455 int nomix_path_idx, int mix_path_idx,
2458 struct hda_gen_spec *spec = codec->spec;
2459 struct nid_path *nomix_path, *mix_path;
2461 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2462 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2463 if (!nomix_path || !mix_path)
2466 /* if HP aamix path is driven from a different DAC and the
2467 * independent HP mode is ON, can't turn on aamix path
2469 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2470 mix_path->path[0] != spec->alt_dac_nid)
2474 snd_hda_activate_path(codec, nomix_path, false, true);
2475 snd_hda_activate_path(codec, mix_path, true, true);
2476 path_power_down_sync(codec, nomix_path);
2478 snd_hda_activate_path(codec, mix_path, false, false);
2479 snd_hda_activate_path(codec, nomix_path, true, false);
2480 path_power_down_sync(codec, mix_path);
2484 /* re-initialize the output paths; only called from loopback_mixing_put() */
2485 static void update_output_paths(struct hda_codec *codec, int num_outs,
2488 struct hda_gen_spec *spec = codec->spec;
2489 struct nid_path *path;
2492 for (i = 0; i < num_outs; i++) {
2493 path = snd_hda_get_path_from_idx(codec, paths[i]);
2495 snd_hda_activate_path(codec, path, path->active,
2500 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2501 struct snd_ctl_elem_value *ucontrol)
2503 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2504 struct hda_gen_spec *spec = codec->spec;
2505 const struct auto_pin_cfg *cfg = &spec->autocfg;
2506 unsigned int val = ucontrol->value.enumerated.item[0];
2508 if (val == spec->aamix_mode)
2510 spec->aamix_mode = val;
2511 if (has_aamix_out_paths(spec)) {
2512 update_aamix_paths(codec, val, spec->out_paths[0],
2513 spec->aamix_out_paths[0],
2514 cfg->line_out_type);
2515 update_aamix_paths(codec, val, spec->hp_paths[0],
2516 spec->aamix_out_paths[1],
2518 update_aamix_paths(codec, val, spec->speaker_paths[0],
2519 spec->aamix_out_paths[2],
2520 AUTO_PIN_SPEAKER_OUT);
2522 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2523 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2524 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2525 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2526 update_output_paths(codec, cfg->speaker_outs,
2527 spec->speaker_paths);
2532 static const struct snd_kcontrol_new loopback_mixing_enum = {
2533 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2534 .name = "Loopback Mixing",
2535 .info = loopback_mixing_info,
2536 .get = loopback_mixing_get,
2537 .put = loopback_mixing_put,
2540 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2542 struct hda_gen_spec *spec = codec->spec;
2544 if (!spec->mixer_nid)
2546 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2548 spec->have_aamix_ctl = 1;
2553 * shared headphone/mic handling
2556 static void call_update_outputs(struct hda_codec *codec);
2558 /* for shared I/O, change the pin-control accordingly */
2559 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2561 struct hda_gen_spec *spec = codec->spec;
2566 pin = spec->hp_mic_pin;
2567 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2570 val = snd_hda_codec_get_pin_target(codec, pin);
2580 val = snd_hda_get_default_vref(codec, pin);
2581 /* if the HP pin doesn't support VREF and the codec driver gives an
2582 * alternative pin, set up the VREF on that pin instead
2584 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2585 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2586 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2587 if (vref_val != AC_PINCTL_VREF_HIZ)
2588 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2589 PIN_IN | (as_mic ? vref_val : 0));
2592 if (!spec->hp_mic_jack_modes) {
2597 set_pin_target(codec, pin, val, true);
2598 call_hp_automute(codec, NULL);
2602 /* create a shared input with the headphone out */
2603 static int create_hp_mic(struct hda_codec *codec)
2605 struct hda_gen_spec *spec = codec->spec;
2606 struct auto_pin_cfg *cfg = &spec->autocfg;
2607 unsigned int defcfg;
2610 if (!spec->hp_mic) {
2611 if (spec->suppress_hp_mic_detect)
2613 /* automatic detection: only if no input or a single internal
2614 * input pin is found, try to detect the shared hp/mic
2616 if (cfg->num_inputs > 1)
2618 else if (cfg->num_inputs == 1) {
2619 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2620 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2625 spec->hp_mic = 0; /* clear once */
2626 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2630 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2631 nid = cfg->line_out_pins[0];
2632 else if (cfg->hp_outs > 0)
2633 nid = cfg->hp_pins[0];
2637 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2638 return 0; /* no input */
2640 cfg->inputs[cfg->num_inputs].pin = nid;
2641 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2642 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2645 spec->hp_mic_pin = nid;
2646 /* we can't handle auto-mic together with HP-mic */
2647 spec->suppress_auto_mic = 1;
2648 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
2656 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2658 static const char * const out_jack_texts[] = {
2659 "Line Out", "Headphone Out",
2662 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2663 struct snd_ctl_elem_info *uinfo)
2665 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2668 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2669 struct snd_ctl_elem_value *ucontrol)
2671 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2672 hda_nid_t nid = kcontrol->private_value;
2673 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2674 ucontrol->value.enumerated.item[0] = 1;
2676 ucontrol->value.enumerated.item[0] = 0;
2680 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2681 struct snd_ctl_elem_value *ucontrol)
2683 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2684 hda_nid_t nid = kcontrol->private_value;
2687 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2688 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2690 snd_hda_set_pin_ctl_cache(codec, nid, val);
2694 static const struct snd_kcontrol_new out_jack_mode_enum = {
2695 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2696 .info = out_jack_mode_info,
2697 .get = out_jack_mode_get,
2698 .put = out_jack_mode_put,
2701 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2703 struct hda_gen_spec *spec = codec->spec;
2704 const struct snd_kcontrol_new *kctl;
2707 snd_array_for_each(&spec->kctls, i, kctl) {
2708 if (!strcmp(kctl->name, name) && kctl->index == idx)
2714 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2715 char *name, size_t name_len)
2717 struct hda_gen_spec *spec = codec->spec;
2720 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2721 strlcat(name, " Jack Mode", name_len);
2723 for (; find_kctl_name(codec, name, idx); idx++)
2727 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2729 struct hda_gen_spec *spec = codec->spec;
2730 if (spec->add_jack_modes) {
2731 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2732 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2738 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2741 struct hda_gen_spec *spec = codec->spec;
2744 for (i = 0; i < num_pins; i++) {
2745 hda_nid_t pin = pins[i];
2746 if (pin == spec->hp_mic_pin)
2748 if (get_out_jack_num_items(codec, pin) > 1) {
2749 struct snd_kcontrol_new *knew;
2750 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2751 get_jack_mode_name(codec, pin, name, sizeof(name));
2752 knew = snd_hda_gen_add_kctl(spec, name,
2753 &out_jack_mode_enum);
2756 knew->private_value = pin;
2767 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2770 static const char * const vref_texts[NUM_VREFS] = {
2771 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2772 "", "Mic 80pc Bias", "Mic 100pc Bias"
2775 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2777 unsigned int pincap;
2779 pincap = snd_hda_query_pin_caps(codec, pin);
2780 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2781 /* filter out unusual vrefs */
2782 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2786 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2787 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2789 unsigned int i, n = 0;
2791 for (i = 0; i < NUM_VREFS; i++) {
2792 if (vref_caps & (1 << i)) {
2801 /* convert back from the vref ctl index to the enum item index */
2802 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2804 unsigned int i, n = 0;
2806 for (i = 0; i < NUM_VREFS; i++) {
2809 if (vref_caps & (1 << i))
2815 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2816 struct snd_ctl_elem_info *uinfo)
2818 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2819 hda_nid_t nid = kcontrol->private_value;
2820 unsigned int vref_caps = get_vref_caps(codec, nid);
2822 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2824 /* set the right text */
2825 strcpy(uinfo->value.enumerated.name,
2826 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2830 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2831 struct snd_ctl_elem_value *ucontrol)
2833 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2834 hda_nid_t nid = kcontrol->private_value;
2835 unsigned int vref_caps = get_vref_caps(codec, nid);
2838 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2839 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2843 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2844 struct snd_ctl_elem_value *ucontrol)
2846 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2847 hda_nid_t nid = kcontrol->private_value;
2848 unsigned int vref_caps = get_vref_caps(codec, nid);
2849 unsigned int val, idx;
2851 val = snd_hda_codec_get_pin_target(codec, nid);
2852 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2853 if (idx == ucontrol->value.enumerated.item[0])
2856 val &= ~AC_PINCTL_VREFEN;
2857 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2858 snd_hda_set_pin_ctl_cache(codec, nid, val);
2862 static const struct snd_kcontrol_new in_jack_mode_enum = {
2863 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2864 .info = in_jack_mode_info,
2865 .get = in_jack_mode_get,
2866 .put = in_jack_mode_put,
2869 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2871 struct hda_gen_spec *spec = codec->spec;
2873 if (spec->add_jack_modes)
2874 nitems = hweight32(get_vref_caps(codec, pin));
2875 return nitems ? nitems : 1;
2878 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2880 struct hda_gen_spec *spec = codec->spec;
2881 struct snd_kcontrol_new *knew;
2882 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2883 unsigned int defcfg;
2885 if (pin == spec->hp_mic_pin)
2886 return 0; /* already done in create_out_jack_mode() */
2888 /* no jack mode for fixed pins */
2889 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2890 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2893 /* no multiple vref caps? */
2894 if (get_in_jack_num_items(codec, pin) <= 1)
2897 get_jack_mode_name(codec, pin, name, sizeof(name));
2898 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2901 knew->private_value = pin;
2906 * HP/mic shared jack mode
2908 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2909 struct snd_ctl_elem_info *uinfo)
2911 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2912 hda_nid_t nid = kcontrol->private_value;
2913 int out_jacks = get_out_jack_num_items(codec, nid);
2914 int in_jacks = get_in_jack_num_items(codec, nid);
2915 const char *text = NULL;
2918 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2920 uinfo->value.enumerated.items = out_jacks + in_jacks;
2921 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2922 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2923 idx = uinfo->value.enumerated.item;
2924 if (idx < out_jacks) {
2926 text = out_jack_texts[idx];
2928 text = "Headphone Out";
2932 unsigned int vref_caps = get_vref_caps(codec, nid);
2933 text = vref_texts[get_vref_idx(vref_caps, idx)];
2938 strcpy(uinfo->value.enumerated.name, text);
2942 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2944 int out_jacks = get_out_jack_num_items(codec, nid);
2945 int in_jacks = get_in_jack_num_items(codec, nid);
2946 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2949 if (val & PIN_OUT) {
2950 if (out_jacks > 1 && val == PIN_HP)
2952 } else if (val & PIN_IN) {
2955 unsigned int vref_caps = get_vref_caps(codec, nid);
2956 val &= AC_PINCTL_VREFEN;
2957 idx += cvt_from_vref_idx(vref_caps, val);
2963 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2964 struct snd_ctl_elem_value *ucontrol)
2966 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2967 hda_nid_t nid = kcontrol->private_value;
2968 ucontrol->value.enumerated.item[0] =
2969 get_cur_hp_mic_jack_mode(codec, nid);
2973 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2974 struct snd_ctl_elem_value *ucontrol)
2976 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2977 hda_nid_t nid = kcontrol->private_value;
2978 int out_jacks = get_out_jack_num_items(codec, nid);
2979 int in_jacks = get_in_jack_num_items(codec, nid);
2980 unsigned int val, oldval, idx;
2982 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2983 idx = ucontrol->value.enumerated.item[0];
2987 if (idx < out_jacks) {
2989 val = idx ? PIN_HP : PIN_OUT;
2995 unsigned int vref_caps = get_vref_caps(codec, nid);
2996 val = snd_hda_codec_get_pin_target(codec, nid);
2997 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2998 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
3000 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
3002 snd_hda_set_pin_ctl_cache(codec, nid, val);
3003 call_hp_automute(codec, NULL);
3008 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
3009 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3010 .info = hp_mic_jack_mode_info,
3011 .get = hp_mic_jack_mode_get,
3012 .put = hp_mic_jack_mode_put,
3015 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
3017 struct hda_gen_spec *spec = codec->spec;
3018 struct snd_kcontrol_new *knew;
3020 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
3021 &hp_mic_jack_mode_enum);
3024 knew->private_value = pin;
3025 spec->hp_mic_jack_modes = 1;
3033 /* add the powersave loopback-list entry */
3034 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
3036 struct hda_amp_list *list;
3038 list = snd_array_new(&spec->loopback_list);
3042 list->dir = HDA_INPUT;
3044 spec->loopback.amplist = spec->loopback_list.list;
3048 /* return true if either a volume or a mute amp is found for the given
3049 * aamix path; the amp has to be either in the mixer node or its direct leaf
3051 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3052 hda_nid_t pin, unsigned int *mix_val,
3053 unsigned int *mute_val)
3056 const hda_nid_t *list;
3059 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3063 *mix_val = *mute_val = 0;
3064 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3065 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3066 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3067 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3068 if (*mix_val && *mute_val)
3071 /* check leaf node */
3072 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3073 if (num_conns < idx)
3076 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3077 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
3078 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3079 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3080 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
3081 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3083 return *mix_val || *mute_val;
3086 /* create input playback/capture controls for the given pin */
3087 static int new_analog_input(struct hda_codec *codec, int input_idx,
3088 hda_nid_t pin, const char *ctlname, int ctlidx,
3091 struct hda_gen_spec *spec = codec->spec;
3092 struct nid_path *path;
3093 unsigned int mix_val, mute_val;
3096 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3099 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
3102 print_nid_path(codec, "loopback", path);
3103 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
3105 idx = path->idx[path->depth - 1];
3107 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
3110 path->ctls[NID_PATH_VOL_CTL] = mix_val;
3114 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
3117 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
3120 path->active = true;
3121 path->stream_enabled = true; /* no DAC/ADC involved */
3122 err = add_loopback_list(spec, mix_nid, idx);
3126 if (spec->mixer_nid != spec->mixer_merge_nid &&
3127 !spec->loopback_merge_path) {
3128 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3129 spec->mixer_merge_nid, 0);
3131 print_nid_path(codec, "loopback-merge", path);
3132 path->active = true;
3133 path->pin_fixed = true; /* static route */
3134 path->stream_enabled = true; /* no DAC/ADC involved */
3135 spec->loopback_merge_path =
3136 snd_hda_get_path_idx(codec, path);
3143 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3145 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3146 return (pincap & AC_PINCAP_IN) != 0;
3149 /* Parse the codec tree and retrieve ADCs */
3150 static int fill_adc_nids(struct hda_codec *codec)
3152 struct hda_gen_spec *spec = codec->spec;
3154 hda_nid_t *adc_nids = spec->adc_nids;
3155 int max_nums = ARRAY_SIZE(spec->adc_nids);
3158 for_each_hda_codec_node(nid, codec) {
3159 unsigned int caps = get_wcaps(codec, nid);
3160 int type = get_wcaps_type(caps);
3162 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3164 adc_nids[nums] = nid;
3165 if (++nums >= max_nums)
3168 spec->num_adc_nids = nums;
3170 /* copy the detected ADCs to all_adcs[] */
3171 spec->num_all_adcs = nums;
3172 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3177 /* filter out invalid adc_nids that don't give all active input pins;
3178 * if needed, check whether dynamic ADC-switching is available
3180 static int check_dyn_adc_switch(struct hda_codec *codec)
3182 struct hda_gen_spec *spec = codec->spec;
3183 struct hda_input_mux *imux = &spec->input_mux;
3184 unsigned int ok_bits;
3189 for (n = 0; n < spec->num_adc_nids; n++) {
3190 for (i = 0; i < imux->num_items; i++) {
3191 if (!spec->input_paths[i][n])
3194 if (i >= imux->num_items) {
3195 ok_bits |= (1 << n);
3201 /* check whether ADC-switch is possible */
3202 for (i = 0; i < imux->num_items; i++) {
3203 for (n = 0; n < spec->num_adc_nids; n++) {
3204 if (spec->input_paths[i][n]) {
3205 spec->dyn_adc_idx[i] = n;
3211 codec_dbg(codec, "enabling ADC switching\n");
3212 spec->dyn_adc_switch = 1;
3213 } else if (nums != spec->num_adc_nids) {
3214 /* shrink the invalid adcs and input paths */
3216 for (n = 0; n < spec->num_adc_nids; n++) {
3217 if (!(ok_bits & (1 << n)))
3220 spec->adc_nids[nums] = spec->adc_nids[n];
3221 for (i = 0; i < imux->num_items; i++) {
3222 invalidate_nid_path(codec,
3223 spec->input_paths[i][nums]);
3224 spec->input_paths[i][nums] =
3225 spec->input_paths[i][n];
3226 spec->input_paths[i][n] = 0;
3231 spec->num_adc_nids = nums;
3234 if (imux->num_items == 1 ||
3235 (imux->num_items == 2 && spec->hp_mic)) {
3236 codec_dbg(codec, "reducing to a single ADC\n");
3237 spec->num_adc_nids = 1; /* reduce to a single ADC */
3240 /* single index for individual volumes ctls */
3241 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3242 spec->num_adc_nids = 1;
3247 /* parse capture source paths from the given pin and create imux items */
3248 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3249 int cfg_idx, int num_adcs,
3250 const char *label, int anchor)
3252 struct hda_gen_spec *spec = codec->spec;
3253 struct hda_input_mux *imux = &spec->input_mux;
3254 int imux_idx = imux->num_items;
3255 bool imux_added = false;
3258 for (c = 0; c < num_adcs; c++) {
3259 struct nid_path *path;
3260 hda_nid_t adc = spec->adc_nids[c];
3262 if (!is_reachable_path(codec, pin, adc))
3264 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3267 print_nid_path(codec, "input", path);
3268 spec->input_paths[imux_idx][c] =
3269 snd_hda_get_path_idx(codec, path);
3272 if (spec->hp_mic_pin == pin)
3273 spec->hp_mic_mux_idx = imux->num_items;
3274 spec->imux_pins[imux->num_items] = pin;
3275 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3277 if (spec->dyn_adc_switch)
3278 spec->dyn_adc_idx[imux_idx] = c;
3286 * create playback/capture controls for input pins
3289 /* fill the label for each input at first */
3290 static int fill_input_pin_labels(struct hda_codec *codec)
3292 struct hda_gen_spec *spec = codec->spec;
3293 const struct auto_pin_cfg *cfg = &spec->autocfg;
3296 for (i = 0; i < cfg->num_inputs; i++) {
3297 hda_nid_t pin = cfg->inputs[i].pin;
3301 if (!is_input_pin(codec, pin))
3304 label = hda_get_autocfg_input_label(codec, cfg, i);
3306 for (j = i - 1; j >= 0; j--) {
3307 if (spec->input_labels[j] &&
3308 !strcmp(spec->input_labels[j], label)) {
3309 idx = spec->input_label_idxs[j] + 1;
3314 spec->input_labels[i] = label;
3315 spec->input_label_idxs[i] = idx;
3321 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3323 static int create_input_ctls(struct hda_codec *codec)
3325 struct hda_gen_spec *spec = codec->spec;
3326 const struct auto_pin_cfg *cfg = &spec->autocfg;
3327 hda_nid_t mixer = spec->mixer_nid;
3332 num_adcs = fill_adc_nids(codec);
3336 err = fill_input_pin_labels(codec);
3340 for (i = 0; i < cfg->num_inputs; i++) {
3343 pin = cfg->inputs[i].pin;
3344 if (!is_input_pin(codec, pin))
3348 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3349 val |= snd_hda_get_default_vref(codec, pin);
3350 if (pin != spec->hp_mic_pin &&
3351 !snd_hda_codec_get_pin_target(codec, pin))
3352 set_pin_target(codec, pin, val, false);
3355 if (is_reachable_path(codec, pin, mixer)) {
3356 err = new_analog_input(codec, i, pin,
3357 spec->input_labels[i],
3358 spec->input_label_idxs[i],
3365 err = parse_capture_source(codec, pin, i, num_adcs,
3366 spec->input_labels[i], -mixer);
3370 if (spec->add_jack_modes) {
3371 err = create_in_jack_mode(codec, pin);
3377 /* add stereo mix when explicitly enabled via hint */
3378 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3379 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3384 spec->suppress_auto_mic = 1;
3395 /* get the input path specified by the given adc and imux indices */
3396 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3398 struct hda_gen_spec *spec = codec->spec;
3399 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3403 if (spec->dyn_adc_switch)
3404 adc_idx = spec->dyn_adc_idx[imux_idx];
3405 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3409 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3412 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3415 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3416 struct snd_ctl_elem_info *uinfo)
3418 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3419 struct hda_gen_spec *spec = codec->spec;
3420 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3423 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3424 struct snd_ctl_elem_value *ucontrol)
3426 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3427 struct hda_gen_spec *spec = codec->spec;
3428 /* the ctls are created at once with multiple counts */
3429 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3431 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3435 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3436 struct snd_ctl_elem_value *ucontrol)
3438 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3439 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3440 return mux_select(codec, adc_idx,
3441 ucontrol->value.enumerated.item[0]);
3444 static const struct snd_kcontrol_new cap_src_temp = {
3445 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3446 .name = "Input Source",
3447 .info = mux_enum_info,
3448 .get = mux_enum_get,
3449 .put = mux_enum_put,
3453 * capture volume and capture switch ctls
3456 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3457 struct snd_ctl_elem_value *ucontrol);
3459 /* call the given amp update function for all amps in the imux list at once */
3460 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3461 struct snd_ctl_elem_value *ucontrol,
3462 put_call_t func, int type)
3464 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3465 struct hda_gen_spec *spec = codec->spec;
3466 const struct hda_input_mux *imux;
3467 struct nid_path *path;
3468 int i, adc_idx, ret, err = 0;
3470 imux = &spec->input_mux;
3471 adc_idx = kcontrol->id.index;
3472 mutex_lock(&codec->control_mutex);
3473 for (i = 0; i < imux->num_items; i++) {
3474 path = get_input_path(codec, adc_idx, i);
3475 if (!path || !path->ctls[type])
3477 kcontrol->private_value = path->ctls[type];
3478 ret = func(kcontrol, ucontrol);
3486 mutex_unlock(&codec->control_mutex);
3487 if (err >= 0 && spec->cap_sync_hook)
3488 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3492 /* capture volume ctl callbacks */
3493 #define cap_vol_info snd_hda_mixer_amp_volume_info
3494 #define cap_vol_get snd_hda_mixer_amp_volume_get
3495 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3497 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3498 struct snd_ctl_elem_value *ucontrol)
3500 return cap_put_caller(kcontrol, ucontrol,
3501 snd_hda_mixer_amp_volume_put,
3505 static const struct snd_kcontrol_new cap_vol_temp = {
3506 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3507 .name = "Capture Volume",
3508 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3509 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3510 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3511 .info = cap_vol_info,
3514 .tlv = { .c = cap_vol_tlv },
3517 /* capture switch ctl callbacks */
3518 #define cap_sw_info snd_ctl_boolean_stereo_info
3519 #define cap_sw_get snd_hda_mixer_amp_switch_get
3521 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3522 struct snd_ctl_elem_value *ucontrol)
3524 return cap_put_caller(kcontrol, ucontrol,
3525 snd_hda_mixer_amp_switch_put,
3529 static const struct snd_kcontrol_new cap_sw_temp = {
3530 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3531 .name = "Capture Switch",
3532 .info = cap_sw_info,
3537 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3542 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3543 for (depth = 0; depth < 3; depth++) {
3544 if (depth >= path->depth)
3546 i = path->depth - depth - 1;
3547 nid = path->path[i];
3548 if (!path->ctls[NID_PATH_VOL_CTL]) {
3549 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3550 path->ctls[NID_PATH_VOL_CTL] =
3551 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3552 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3553 int idx = path->idx[i];
3554 if (!depth && codec->single_adc_amp)
3556 path->ctls[NID_PATH_VOL_CTL] =
3557 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3560 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3561 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3562 path->ctls[NID_PATH_MUTE_CTL] =
3563 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3564 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3565 int idx = path->idx[i];
3566 if (!depth && codec->single_adc_amp)
3568 path->ctls[NID_PATH_MUTE_CTL] =
3569 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3576 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3578 struct hda_gen_spec *spec = codec->spec;
3579 struct auto_pin_cfg *cfg = &spec->autocfg;
3583 if (!spec->inv_dmic_split)
3585 for (i = 0; i < cfg->num_inputs; i++) {
3586 if (cfg->inputs[i].pin != nid)
3588 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3590 val = snd_hda_codec_get_pincfg(codec, nid);
3591 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3596 /* capture switch put callback for a single control with hook call */
3597 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3598 struct snd_ctl_elem_value *ucontrol)
3600 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3601 struct hda_gen_spec *spec = codec->spec;
3604 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3608 if (spec->cap_sync_hook)
3609 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3614 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3615 int idx, bool is_switch, unsigned int ctl,
3618 struct hda_gen_spec *spec = codec->spec;
3619 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3620 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3621 const char *sfx = is_switch ? "Switch" : "Volume";
3622 unsigned int chs = inv_dmic ? 1 : 3;
3623 struct snd_kcontrol_new *knew;
3629 snprintf(tmpname, sizeof(tmpname),
3630 "%s Capture %s", label, sfx);
3632 snprintf(tmpname, sizeof(tmpname),
3634 knew = add_control(spec, type, tmpname, idx,
3635 amp_val_replace_channels(ctl, chs));
3639 knew->put = cap_single_sw_put;
3643 /* Make independent right kcontrol */
3645 snprintf(tmpname, sizeof(tmpname),
3646 "Inverted %s Capture %s", label, sfx);
3648 snprintf(tmpname, sizeof(tmpname),
3649 "Inverted Capture %s", sfx);
3650 knew = add_control(spec, type, tmpname, idx,
3651 amp_val_replace_channels(ctl, 2));
3655 knew->put = cap_single_sw_put;
3659 /* create single (and simple) capture volume and switch controls */
3660 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3661 unsigned int vol_ctl, unsigned int sw_ctl,
3665 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3668 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3674 /* create bound capture volume and switch controls */
3675 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3676 unsigned int vol_ctl, unsigned int sw_ctl)
3678 struct hda_gen_spec *spec = codec->spec;
3679 struct snd_kcontrol_new *knew;
3682 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3686 knew->private_value = vol_ctl;
3687 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3690 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3694 knew->private_value = sw_ctl;
3695 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3700 /* return the vol ctl when used first in the imux list */
3701 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3703 struct nid_path *path;
3707 path = get_input_path(codec, 0, idx);
3710 ctl = path->ctls[type];
3713 for (i = 0; i < idx - 1; i++) {
3714 path = get_input_path(codec, 0, i);
3715 if (path && path->ctls[type] == ctl)
3721 /* create individual capture volume and switch controls per input */
3722 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3724 struct hda_gen_spec *spec = codec->spec;
3725 struct hda_input_mux *imux = &spec->input_mux;
3728 for (i = 0; i < imux->num_items; i++) {
3732 idx = imux->items[i].index;
3733 if (idx >= spec->autocfg.num_inputs)
3735 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3737 for (type = 0; type < 2; type++) {
3738 err = add_single_cap_ctl(codec,
3739 spec->input_labels[idx],
3740 spec->input_label_idxs[idx],
3742 get_first_cap_ctl(codec, i, type),
3751 static int create_capture_mixers(struct hda_codec *codec)
3753 struct hda_gen_spec *spec = codec->spec;
3754 struct hda_input_mux *imux = &spec->input_mux;
3755 int i, n, nums, err;
3757 if (spec->dyn_adc_switch)
3760 nums = spec->num_adc_nids;
3762 if (!spec->auto_mic && imux->num_items > 1) {
3763 struct snd_kcontrol_new *knew;
3765 name = nums > 1 ? "Input Source" : "Capture Source";
3766 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3772 for (n = 0; n < nums; n++) {
3774 bool multi_cap_vol = spec->multi_cap_vol;
3775 bool inv_dmic = false;
3779 for (i = 0; i < imux->num_items; i++) {
3780 struct nid_path *path;
3781 path = get_input_path(codec, n, i);
3784 parse_capvol_in_path(codec, path);
3786 vol = path->ctls[NID_PATH_VOL_CTL];
3787 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3789 if (!same_amp_caps(codec, vol,
3790 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3791 multi_cap_vol = true;
3794 sw = path->ctls[NID_PATH_MUTE_CTL];
3795 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3797 if (!same_amp_caps(codec, sw,
3798 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3799 multi_cap_vol = true;
3801 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3806 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3808 else if (!multi_cap_vol && !inv_dmic)
3809 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3811 err = create_multi_cap_vol_ctl(codec);
3820 * add mic boosts if needed
3823 /* check whether the given amp is feasible as a boost volume */
3824 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3829 if (!nid_has_volume(codec, nid, dir) ||
3830 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3831 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3834 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3835 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3841 /* look for a boost amp in a widget close to the pin */
3842 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3843 struct nid_path *path)
3845 unsigned int val = 0;
3849 for (depth = 0; depth < 3; depth++) {
3850 if (depth >= path->depth - 1)
3852 nid = path->path[depth];
3853 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3854 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3856 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3857 path->idx[depth])) {
3858 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3867 static int parse_mic_boost(struct hda_codec *codec)
3869 struct hda_gen_spec *spec = codec->spec;
3870 struct auto_pin_cfg *cfg = &spec->autocfg;
3871 struct hda_input_mux *imux = &spec->input_mux;
3874 if (!spec->num_adc_nids)
3877 for (i = 0; i < imux->num_items; i++) {
3878 struct nid_path *path;
3881 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3883 idx = imux->items[i].index;
3884 if (idx >= imux->num_items)
3887 /* check only line-in and mic pins */
3888 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3891 path = get_input_path(codec, 0, i);
3895 val = look_for_boost_amp(codec, path);
3899 /* create a boost control */
3900 snprintf(boost_label, sizeof(boost_label),
3901 "%s Boost Volume", spec->input_labels[idx]);
3902 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3903 spec->input_label_idxs[idx], val))
3906 path->ctls[NID_PATH_BOOST_CTL] = val;
3911 #ifdef CONFIG_SND_HDA_GENERIC_LEDS
3913 * vmaster mute LED hook helpers
3916 static int create_mute_led_cdev(struct hda_codec *codec,
3917 int (*callback)(struct led_classdev *,
3918 enum led_brightness),
3921 struct hda_gen_spec *spec = codec->spec;
3922 struct led_classdev *cdev;
3923 int idx = micmute ? LED_AUDIO_MICMUTE : LED_AUDIO_MUTE;
3926 cdev = devm_kzalloc(&codec->core.dev, sizeof(*cdev), GFP_KERNEL);
3930 cdev->name = micmute ? "hda::micmute" : "hda::mute";
3931 cdev->max_brightness = 1;
3932 cdev->default_trigger = micmute ? "audio-micmute" : "audio-mute";
3933 cdev->brightness_set_blocking = callback;
3934 cdev->brightness = ledtrig_audio_get(idx);
3935 cdev->flags = LED_CORE_SUSPENDRESUME;
3937 err = led_classdev_register(&codec->core.dev, cdev);
3940 spec->led_cdevs[idx] = cdev;
3944 static void vmaster_update_mute_led(void *private_data, int enabled)
3946 ledtrig_audio_set(LED_AUDIO_MUTE, enabled ? LED_OFF : LED_ON);
3950 * snd_dha_gen_add_mute_led_cdev - Create a LED classdev and enable as vmaster mute LED
3951 * @codec: the HDA codec
3952 * @callback: the callback for LED classdev brightness_set_blocking
3954 int snd_hda_gen_add_mute_led_cdev(struct hda_codec *codec,
3955 int (*callback)(struct led_classdev *,
3956 enum led_brightness))
3958 struct hda_gen_spec *spec = codec->spec;
3962 err = create_mute_led_cdev(codec, callback, false);
3964 codec_warn(codec, "failed to create a mute LED cdev\n");
3969 if (spec->vmaster_mute.hook)
3970 codec_err(codec, "vmaster hook already present before cdev!\n");
3972 spec->vmaster_mute.hook = vmaster_update_mute_led;
3973 spec->vmaster_mute_enum = 1;
3976 EXPORT_SYMBOL_GPL(snd_hda_gen_add_mute_led_cdev);
3979 * mic mute LED hook helpers
3984 MICMUTE_LED_FOLLOW_CAPTURE,
3985 MICMUTE_LED_FOLLOW_MUTE,
3988 static void call_micmute_led_update(struct hda_codec *codec)
3990 struct hda_gen_spec *spec = codec->spec;
3993 switch (spec->micmute_led.led_mode) {
3994 case MICMUTE_LED_ON:
3997 case MICMUTE_LED_OFF:
4000 case MICMUTE_LED_FOLLOW_CAPTURE:
4001 val = !!spec->micmute_led.capture;
4003 case MICMUTE_LED_FOLLOW_MUTE:
4005 val = !spec->micmute_led.capture;
4009 if (val == spec->micmute_led.led_value)
4011 spec->micmute_led.led_value = val;
4012 ledtrig_audio_set(LED_AUDIO_MICMUTE,
4013 spec->micmute_led.led_value ? LED_ON : LED_OFF);
4016 static void update_micmute_led(struct hda_codec *codec,
4017 struct snd_kcontrol *kcontrol,
4018 struct snd_ctl_elem_value *ucontrol)
4020 struct hda_gen_spec *spec = codec->spec;
4023 if (spec->micmute_led.old_hook)
4024 spec->micmute_led.old_hook(codec, kcontrol, ucontrol);
4028 mask = 1U << snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
4029 if (!strcmp("Capture Switch", ucontrol->id.name)) {
4030 /* TODO: How do I verify if it's a mono or stereo here? */
4031 if (ucontrol->value.integer.value[0] ||
4032 ucontrol->value.integer.value[1])
4033 spec->micmute_led.capture |= mask;
4035 spec->micmute_led.capture &= ~mask;
4036 call_micmute_led_update(codec);
4040 static int micmute_led_mode_info(struct snd_kcontrol *kcontrol,
4041 struct snd_ctl_elem_info *uinfo)
4043 static const char * const texts[] = {
4044 "On", "Off", "Follow Capture", "Follow Mute",
4047 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(texts), texts);
4050 static int micmute_led_mode_get(struct snd_kcontrol *kcontrol,
4051 struct snd_ctl_elem_value *ucontrol)
4053 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4054 struct hda_gen_spec *spec = codec->spec;
4056 ucontrol->value.enumerated.item[0] = spec->micmute_led.led_mode;
4060 static int micmute_led_mode_put(struct snd_kcontrol *kcontrol,
4061 struct snd_ctl_elem_value *ucontrol)
4063 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4064 struct hda_gen_spec *spec = codec->spec;
4067 mode = ucontrol->value.enumerated.item[0];
4068 if (mode > MICMUTE_LED_FOLLOW_MUTE)
4069 mode = MICMUTE_LED_FOLLOW_MUTE;
4070 if (mode == spec->micmute_led.led_mode)
4072 spec->micmute_led.led_mode = mode;
4073 call_micmute_led_update(codec);
4077 static const struct snd_kcontrol_new micmute_led_mode_ctl = {
4078 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4079 .name = "Mic Mute-LED Mode",
4080 .info = micmute_led_mode_info,
4081 .get = micmute_led_mode_get,
4082 .put = micmute_led_mode_put,
4085 /* Set up the capture sync hook for controlling the mic-mute LED */
4086 static int add_micmute_led_hook(struct hda_codec *codec)
4088 struct hda_gen_spec *spec = codec->spec;
4090 spec->micmute_led.led_mode = MICMUTE_LED_FOLLOW_MUTE;
4091 spec->micmute_led.capture = 0;
4092 spec->micmute_led.led_value = -1;
4093 spec->micmute_led.old_hook = spec->cap_sync_hook;
4094 spec->cap_sync_hook = update_micmute_led;
4095 if (!snd_hda_gen_add_kctl(spec, NULL, &micmute_led_mode_ctl))
4101 * snd_dha_gen_add_micmute_led_cdev - Create a LED classdev and enable as mic-mute LED
4102 * @codec: the HDA codec
4103 * @callback: the callback for LED classdev brightness_set_blocking
4105 * Called from the codec drivers for offering the mic mute LED controls.
4106 * This creates a LED classdev and sets up the cap_sync_hook that is called at
4107 * each time when the capture mixer switch changes.
4109 * When NULL is passed to @callback, no classdev is created but only the
4110 * LED-trigger is set up.
4112 * Returns 0 or a negative error.
4114 int snd_hda_gen_add_micmute_led_cdev(struct hda_codec *codec,
4115 int (*callback)(struct led_classdev *,
4116 enum led_brightness))
4121 err = create_mute_led_cdev(codec, callback, true);
4123 codec_warn(codec, "failed to create a mic-mute LED cdev\n");
4128 return add_micmute_led_hook(codec);
4130 EXPORT_SYMBOL_GPL(snd_hda_gen_add_micmute_led_cdev);
4131 #endif /* CONFIG_SND_HDA_GENERIC_LEDS */
4134 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
4136 static void parse_digital(struct hda_codec *codec)
4138 struct hda_gen_spec *spec = codec->spec;
4139 struct nid_path *path;
4141 hda_nid_t dig_nid, pin;
4143 /* support multiple SPDIFs; the secondary is set up as a follower */
4145 for (i = 0; i < spec->autocfg.dig_outs; i++) {
4146 pin = spec->autocfg.dig_out_pins[i];
4147 dig_nid = look_for_dac(codec, pin, true);
4150 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
4153 print_nid_path(codec, "digout", path);
4154 path->active = true;
4155 path->pin_fixed = true; /* no jack detection */
4156 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
4157 set_pin_target(codec, pin, PIN_OUT, false);
4159 spec->multiout.dig_out_nid = dig_nid;
4160 spec->dig_out_type = spec->autocfg.dig_out_type[0];
4162 spec->multiout.follower_dig_outs = spec->follower_dig_outs;
4163 if (nums >= ARRAY_SIZE(spec->follower_dig_outs) - 1)
4165 spec->follower_dig_outs[nums - 1] = dig_nid;
4170 if (spec->autocfg.dig_in_pin) {
4171 pin = spec->autocfg.dig_in_pin;
4172 for_each_hda_codec_node(dig_nid, codec) {
4173 unsigned int wcaps = get_wcaps(codec, dig_nid);
4174 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
4176 if (!(wcaps & AC_WCAP_DIGITAL))
4178 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
4180 print_nid_path(codec, "digin", path);
4181 path->active = true;
4182 path->pin_fixed = true; /* no jack */
4183 spec->dig_in_nid = dig_nid;
4184 spec->digin_path = snd_hda_get_path_idx(codec, path);
4185 set_pin_target(codec, pin, PIN_IN, false);
4194 * input MUX handling
4197 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
4199 /* select the given imux item; either unmute exclusively or select the route */
4200 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
4203 struct hda_gen_spec *spec = codec->spec;
4204 const struct hda_input_mux *imux;
4205 struct nid_path *old_path, *path;
4207 imux = &spec->input_mux;
4208 if (!imux->num_items)
4211 if (idx >= imux->num_items)
4212 idx = imux->num_items - 1;
4213 if (spec->cur_mux[adc_idx] == idx)
4216 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
4219 if (old_path->active)
4220 snd_hda_activate_path(codec, old_path, false, false);
4222 spec->cur_mux[adc_idx] = idx;
4225 update_hp_mic(codec, adc_idx, false);
4227 if (spec->dyn_adc_switch)
4228 dyn_adc_pcm_resetup(codec, idx);
4230 path = get_input_path(codec, adc_idx, idx);
4235 snd_hda_activate_path(codec, path, true, false);
4236 if (spec->cap_sync_hook)
4237 spec->cap_sync_hook(codec, NULL, NULL);
4238 path_power_down_sync(codec, old_path);
4242 /* power up/down widgets in the all paths that match with the given NID
4243 * as terminals (either start- or endpoint)
4245 * returns the last changed NID, or zero if unchanged.
4247 static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
4248 int pin_state, int stream_state)
4250 struct hda_gen_spec *spec = codec->spec;
4251 hda_nid_t last, changed = 0;
4252 struct nid_path *path;
4255 snd_array_for_each(&spec->paths, n, path) {
4258 if (path->path[0] == nid ||
4259 path->path[path->depth - 1] == nid) {
4260 bool pin_old = path->pin_enabled;
4261 bool stream_old = path->stream_enabled;
4264 path->pin_enabled = pin_state;
4265 if (stream_state >= 0)
4266 path->stream_enabled = stream_state;
4267 if ((!path->pin_fixed && path->pin_enabled != pin_old)
4268 || path->stream_enabled != stream_old) {
4269 last = path_power_update(codec, path, true);
4278 /* check the jack status for power control */
4279 static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4281 if (!is_jack_detectable(codec, pin))
4283 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4286 /* power up/down the paths of the given pin according to the jack state;
4287 * power = 0/1 : only power up/down if it matches with the jack state,
4288 * < 0 : force power up/down to follow the jack sate
4290 * returns the last changed NID, or zero if unchanged.
4292 static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4297 if (!codec->power_save_node)
4300 on = detect_pin_state(codec, pin);
4302 if (power >= 0 && on != power)
4304 return set_path_power(codec, pin, on, -1);
4307 static void pin_power_callback(struct hda_codec *codec,
4308 struct hda_jack_callback *jack,
4311 if (jack && jack->nid)
4312 sync_power_state_change(codec,
4313 set_pin_power_jack(codec, jack->nid, on));
4316 /* callback only doing power up -- called at first */
4317 static void pin_power_up_callback(struct hda_codec *codec,
4318 struct hda_jack_callback *jack)
4320 pin_power_callback(codec, jack, true);
4323 /* callback only doing power down -- called at last */
4324 static void pin_power_down_callback(struct hda_codec *codec,
4325 struct hda_jack_callback *jack)
4327 pin_power_callback(codec, jack, false);
4330 /* set up the power up/down callbacks */
4331 static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4332 const hda_nid_t *pins, bool on)
4335 hda_jack_callback_fn cb =
4336 on ? pin_power_up_callback : pin_power_down_callback;
4338 for (i = 0; i < num_pins && pins[i]; i++) {
4339 if (is_jack_detectable(codec, pins[i]))
4340 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4342 set_path_power(codec, pins[i], true, -1);
4346 /* enabled power callback to each available I/O pin with jack detections;
4347 * the digital I/O pins are excluded because of the unreliable detectsion
4349 static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4351 struct hda_gen_spec *spec = codec->spec;
4352 struct auto_pin_cfg *cfg = &spec->autocfg;
4355 if (!codec->power_save_node)
4357 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4358 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4359 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4360 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4361 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4362 for (i = 0; i < cfg->num_inputs; i++)
4363 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4366 /* sync path power up/down with the jack states of given pins */
4367 static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4368 const hda_nid_t *pins)
4372 for (i = 0; i < num_pins && pins[i]; i++)
4373 if (is_jack_detectable(codec, pins[i]))
4374 set_pin_power_jack(codec, pins[i], -1);
4377 /* sync path power up/down with pins; called at init and resume */
4378 static void sync_all_pin_power_ctls(struct hda_codec *codec)
4380 struct hda_gen_spec *spec = codec->spec;
4381 struct auto_pin_cfg *cfg = &spec->autocfg;
4384 if (!codec->power_save_node)
4386 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4387 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4388 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4389 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4390 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4391 for (i = 0; i < cfg->num_inputs; i++)
4392 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4395 /* add fake paths if not present yet */
4396 static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4397 int num_pins, const hda_nid_t *pins)
4399 struct hda_gen_spec *spec = codec->spec;
4400 struct nid_path *path;
4403 for (i = 0; i < num_pins; i++) {
4406 if (get_nid_path(codec, nid, pins[i], 0))
4408 path = snd_array_new(&spec->paths);
4411 memset(path, 0, sizeof(*path));
4413 path->path[0] = nid;
4414 path->path[1] = pins[i];
4415 path->active = true;
4420 /* create fake paths to all outputs from beep */
4421 static int add_fake_beep_paths(struct hda_codec *codec)
4423 struct hda_gen_spec *spec = codec->spec;
4424 struct auto_pin_cfg *cfg = &spec->autocfg;
4425 hda_nid_t nid = spec->beep_nid;
4428 if (!codec->power_save_node || !nid)
4430 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4433 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4434 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4438 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4439 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4447 /* power up/down beep widget and its output paths */
4448 static void beep_power_hook(struct hda_beep *beep, bool on)
4450 set_path_power(beep->codec, beep->nid, -1, on);
4454 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4455 * @codec: the HDA codec
4456 * @pin: NID of pin to fix
4458 int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4460 struct hda_gen_spec *spec = codec->spec;
4461 struct nid_path *path;
4463 path = snd_array_new(&spec->paths);
4466 memset(path, 0, sizeof(*path));
4468 path->path[0] = pin;
4469 path->active = true;
4470 path->pin_fixed = true;
4471 path->stream_enabled = true;
4474 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4477 * Jack detections for HP auto-mute and mic-switch
4480 /* check each pin in the given array; returns true if any of them is plugged */
4481 static bool detect_jacks(struct hda_codec *codec, int num_pins, const hda_nid_t *pins)
4484 bool present = false;
4486 for (i = 0; i < num_pins; i++) {
4487 hda_nid_t nid = pins[i];
4490 /* don't detect pins retasked as inputs */
4491 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4493 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4499 /* standard HP/line-out auto-mute helper */
4500 static void do_automute(struct hda_codec *codec, int num_pins, const hda_nid_t *pins,
4501 int *paths, bool mute)
4503 struct hda_gen_spec *spec = codec->spec;
4506 for (i = 0; i < num_pins; i++) {
4507 hda_nid_t nid = pins[i];
4508 unsigned int val, oldval;
4512 oldval = snd_hda_codec_get_pin_target(codec, nid);
4513 if (oldval & PIN_IN)
4514 continue; /* no mute for inputs */
4516 if (spec->auto_mute_via_amp) {
4517 struct nid_path *path;
4520 path = snd_hda_get_path_from_idx(codec, paths[i]);
4523 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4527 spec->mute_bits |= (1ULL << mute_nid);
4529 spec->mute_bits &= ~(1ULL << mute_nid);
4532 /* don't reset VREF value in case it's controlling
4533 * the amp (see alc861_fixup_asus_amp_vref_0f())
4535 if (spec->keep_vref_in_automute)
4536 val = oldval & ~PIN_HP;
4541 /* here we call update_pin_ctl() so that the pinctl is
4542 * changed without changing the pinctl target value;
4543 * the original target value will be still referred at
4544 * the init / resume again
4546 update_pin_ctl(codec, nid, val);
4549 set_pin_eapd(codec, nid, !mute);
4550 if (codec->power_save_node) {
4553 on = detect_pin_state(codec, nid);
4554 set_path_power(codec, nid, on, -1);
4560 * snd_hda_gen_update_outputs - Toggle outputs muting
4561 * @codec: the HDA codec
4563 * Update the mute status of all outputs based on the current jack states.
4565 void snd_hda_gen_update_outputs(struct hda_codec *codec)
4567 struct hda_gen_spec *spec = codec->spec;
4571 /* Control HP pins/amps depending on master_mute state;
4572 * in general, HP pins/amps control should be enabled in all cases,
4573 * but currently set only for master_mute, just to be safe
4575 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4576 paths = spec->out_paths;
4578 paths = spec->hp_paths;
4579 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
4580 spec->autocfg.hp_pins, paths, spec->master_mute);
4582 if (!spec->automute_speaker)
4585 on = spec->hp_jack_present | spec->line_jack_present;
4586 on |= spec->master_mute;
4587 spec->speaker_muted = on;
4588 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4589 paths = spec->out_paths;
4591 paths = spec->speaker_paths;
4592 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
4593 spec->autocfg.speaker_pins, paths, on);
4595 /* toggle line-out mutes if needed, too */
4596 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4597 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4598 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4600 if (!spec->automute_lo)
4603 on = spec->hp_jack_present;
4604 on |= spec->master_mute;
4605 spec->line_out_muted = on;
4606 paths = spec->out_paths;
4607 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4608 spec->autocfg.line_out_pins, paths, on);
4610 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4612 static void call_update_outputs(struct hda_codec *codec)
4614 struct hda_gen_spec *spec = codec->spec;
4615 if (spec->automute_hook)
4616 spec->automute_hook(codec);
4618 snd_hda_gen_update_outputs(codec);
4620 /* sync the whole vmaster followers to reflect the new auto-mute status */
4621 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4622 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4626 * snd_hda_gen_hp_automute - standard HP-automute helper
4627 * @codec: the HDA codec
4628 * @jack: jack object, NULL for the whole
4630 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4631 struct hda_jack_callback *jack)
4633 struct hda_gen_spec *spec = codec->spec;
4634 hda_nid_t *pins = spec->autocfg.hp_pins;
4635 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4637 /* No detection for the first HP jack during indep-HP mode */
4638 if (spec->indep_hp_enabled) {
4643 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4644 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4646 call_update_outputs(codec);
4648 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4651 * snd_hda_gen_line_automute - standard line-out-automute helper
4652 * @codec: the HDA codec
4653 * @jack: jack object, NULL for the whole
4655 void snd_hda_gen_line_automute(struct hda_codec *codec,
4656 struct hda_jack_callback *jack)
4658 struct hda_gen_spec *spec = codec->spec;
4660 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4662 /* check LO jack only when it's different from HP */
4663 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4666 spec->line_jack_present =
4667 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4668 spec->autocfg.line_out_pins);
4669 if (!spec->automute_speaker || !spec->detect_lo)
4671 call_update_outputs(codec);
4673 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4676 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4677 * @codec: the HDA codec
4678 * @jack: jack object, NULL for the whole
4680 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4681 struct hda_jack_callback *jack)
4683 struct hda_gen_spec *spec = codec->spec;
4686 if (!spec->auto_mic)
4689 for (i = spec->am_num_entries - 1; i > 0; i--) {
4690 hda_nid_t pin = spec->am_entry[i].pin;
4691 /* don't detect pins retasked as outputs */
4692 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4694 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4695 mux_select(codec, 0, spec->am_entry[i].idx);
4699 mux_select(codec, 0, spec->am_entry[0].idx);
4701 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4703 /* call appropriate hooks */
4704 static void call_hp_automute(struct hda_codec *codec,
4705 struct hda_jack_callback *jack)
4707 struct hda_gen_spec *spec = codec->spec;
4708 if (spec->hp_automute_hook)
4709 spec->hp_automute_hook(codec, jack);
4711 snd_hda_gen_hp_automute(codec, jack);
4714 static void call_line_automute(struct hda_codec *codec,
4715 struct hda_jack_callback *jack)
4717 struct hda_gen_spec *spec = codec->spec;
4718 if (spec->line_automute_hook)
4719 spec->line_automute_hook(codec, jack);
4721 snd_hda_gen_line_automute(codec, jack);
4724 static void call_mic_autoswitch(struct hda_codec *codec,
4725 struct hda_jack_callback *jack)
4727 struct hda_gen_spec *spec = codec->spec;
4728 if (spec->mic_autoswitch_hook)
4729 spec->mic_autoswitch_hook(codec, jack);
4731 snd_hda_gen_mic_autoswitch(codec, jack);
4734 /* update jack retasking */
4735 static void update_automute_all(struct hda_codec *codec)
4737 call_hp_automute(codec, NULL);
4738 call_line_automute(codec, NULL);
4739 call_mic_autoswitch(codec, NULL);
4743 * Auto-Mute mode mixer enum support
4745 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4746 struct snd_ctl_elem_info *uinfo)
4748 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4749 struct hda_gen_spec *spec = codec->spec;
4750 static const char * const texts3[] = {
4751 "Disabled", "Speaker Only", "Line Out+Speaker"
4754 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4755 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4756 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4759 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4760 struct snd_ctl_elem_value *ucontrol)
4762 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4763 struct hda_gen_spec *spec = codec->spec;
4764 unsigned int val = 0;
4765 if (spec->automute_speaker)
4767 if (spec->automute_lo)
4770 ucontrol->value.enumerated.item[0] = val;
4774 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4775 struct snd_ctl_elem_value *ucontrol)
4777 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4778 struct hda_gen_spec *spec = codec->spec;
4780 switch (ucontrol->value.enumerated.item[0]) {
4782 if (!spec->automute_speaker && !spec->automute_lo)
4784 spec->automute_speaker = 0;
4785 spec->automute_lo = 0;
4788 if (spec->automute_speaker_possible) {
4789 if (!spec->automute_lo && spec->automute_speaker)
4791 spec->automute_speaker = 1;
4792 spec->automute_lo = 0;
4793 } else if (spec->automute_lo_possible) {
4794 if (spec->automute_lo)
4796 spec->automute_lo = 1;
4801 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4803 if (spec->automute_speaker && spec->automute_lo)
4805 spec->automute_speaker = 1;
4806 spec->automute_lo = 1;
4811 call_update_outputs(codec);
4815 static const struct snd_kcontrol_new automute_mode_enum = {
4816 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4817 .name = "Auto-Mute Mode",
4818 .info = automute_mode_info,
4819 .get = automute_mode_get,
4820 .put = automute_mode_put,
4823 static int add_automute_mode_enum(struct hda_codec *codec)
4825 struct hda_gen_spec *spec = codec->spec;
4827 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4833 * Check the availability of HP/line-out auto-mute;
4834 * Set up appropriately if really supported
4836 static int check_auto_mute_availability(struct hda_codec *codec)
4838 struct hda_gen_spec *spec = codec->spec;
4839 struct auto_pin_cfg *cfg = &spec->autocfg;
4843 if (spec->suppress_auto_mute)
4846 if (cfg->hp_pins[0])
4848 if (cfg->line_out_pins[0])
4850 if (cfg->speaker_pins[0])
4852 if (present < 2) /* need two different output types */
4855 if (!cfg->speaker_pins[0] &&
4856 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4857 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4858 sizeof(cfg->speaker_pins));
4859 cfg->speaker_outs = cfg->line_outs;
4862 if (!cfg->hp_pins[0] &&
4863 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4864 memcpy(cfg->hp_pins, cfg->line_out_pins,
4865 sizeof(cfg->hp_pins));
4866 cfg->hp_outs = cfg->line_outs;
4869 for (i = 0; i < cfg->hp_outs; i++) {
4870 hda_nid_t nid = cfg->hp_pins[i];
4871 if (!is_jack_detectable(codec, nid))
4873 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4874 snd_hda_jack_detect_enable_callback(codec, nid,
4876 spec->detect_hp = 1;
4879 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4880 if (cfg->speaker_outs)
4881 for (i = 0; i < cfg->line_outs; i++) {
4882 hda_nid_t nid = cfg->line_out_pins[i];
4883 if (!is_jack_detectable(codec, nid))
4885 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4886 snd_hda_jack_detect_enable_callback(codec, nid,
4887 call_line_automute);
4888 spec->detect_lo = 1;
4890 spec->automute_lo_possible = spec->detect_hp;
4893 spec->automute_speaker_possible = cfg->speaker_outs &&
4894 (spec->detect_hp || spec->detect_lo);
4896 spec->automute_lo = spec->automute_lo_possible;
4897 spec->automute_speaker = spec->automute_speaker_possible;
4899 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4900 /* create a control for automute mode */
4901 err = add_automute_mode_enum(codec);
4908 /* check whether all auto-mic pins are valid; setup indices if OK */
4909 static bool auto_mic_check_imux(struct hda_codec *codec)
4911 struct hda_gen_spec *spec = codec->spec;
4912 const struct hda_input_mux *imux;
4915 imux = &spec->input_mux;
4916 for (i = 0; i < spec->am_num_entries; i++) {
4917 spec->am_entry[i].idx =
4918 find_idx_in_nid_list(spec->am_entry[i].pin,
4919 spec->imux_pins, imux->num_items);
4920 if (spec->am_entry[i].idx < 0)
4921 return false; /* no corresponding imux */
4924 /* we don't need the jack detection for the first pin */
4925 for (i = 1; i < spec->am_num_entries; i++)
4926 snd_hda_jack_detect_enable_callback(codec,
4927 spec->am_entry[i].pin,
4928 call_mic_autoswitch);
4932 static int compare_attr(const void *ap, const void *bp)
4934 const struct automic_entry *a = ap;
4935 const struct automic_entry *b = bp;
4936 return (int)(a->attr - b->attr);
4940 * Check the availability of auto-mic switch;
4941 * Set up if really supported
4943 static int check_auto_mic_availability(struct hda_codec *codec)
4945 struct hda_gen_spec *spec = codec->spec;
4946 struct auto_pin_cfg *cfg = &spec->autocfg;
4950 if (spec->suppress_auto_mic)
4955 for (i = 0; i < cfg->num_inputs; i++) {
4956 hda_nid_t nid = cfg->inputs[i].pin;
4958 attr = snd_hda_codec_get_pincfg(codec, nid);
4959 attr = snd_hda_get_input_pin_attr(attr);
4960 if (types & (1 << attr))
4961 return 0; /* already occupied */
4963 case INPUT_PIN_ATTR_INT:
4964 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4965 return 0; /* invalid type */
4967 case INPUT_PIN_ATTR_UNUSED:
4968 return 0; /* invalid entry */
4970 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4971 return 0; /* invalid type */
4972 if (!spec->line_in_auto_switch &&
4973 cfg->inputs[i].type != AUTO_PIN_MIC)
4974 return 0; /* only mic is allowed */
4975 if (!is_jack_detectable(codec, nid))
4976 return 0; /* no unsol support */
4979 if (num_pins >= MAX_AUTO_MIC_PINS)
4981 types |= (1 << attr);
4982 spec->am_entry[num_pins].pin = nid;
4983 spec->am_entry[num_pins].attr = attr;
4990 spec->am_num_entries = num_pins;
4991 /* sort the am_entry in the order of attr so that the pin with a
4992 * higher attr will be selected when the jack is plugged.
4994 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4995 compare_attr, NULL);
4997 if (!auto_mic_check_imux(codec))
5001 spec->num_adc_nids = 1;
5002 spec->cur_mux[0] = spec->am_entry[0].idx;
5003 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
5004 spec->am_entry[0].pin,
5005 spec->am_entry[1].pin,
5006 spec->am_entry[2].pin);
5012 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
5014 * @codec: the HDA codec
5015 * @nid: NID to evalute
5016 * @power_state: target power state
5018 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
5020 unsigned int power_state)
5022 struct hda_gen_spec *spec = codec->spec;
5024 if (!spec->power_down_unused && !codec->power_save_node)
5026 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
5028 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
5030 if (is_active_nid_for_any(codec, nid))
5034 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
5036 /* mute all aamix inputs initially; parse up to the first leaves */
5037 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
5040 const hda_nid_t *conn;
5043 nums = snd_hda_get_conn_list(codec, mix, &conn);
5044 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
5045 for (i = 0; i < nums; i++) {
5047 update_amp(codec, mix, HDA_INPUT, i,
5048 0xff, HDA_AMP_MUTE);
5049 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
5050 update_amp(codec, conn[i], HDA_OUTPUT, 0,
5051 0xff, HDA_AMP_MUTE);
5056 * snd_hda_gen_stream_pm - Stream power management callback
5057 * @codec: the HDA codec
5058 * @nid: audio widget
5059 * @on: power on/off flag
5061 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
5063 void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
5065 if (codec->power_save_node)
5066 set_path_power(codec, nid, -1, on);
5068 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
5071 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
5072 * set up the hda_gen_spec
5073 * @codec: the HDA codec
5074 * @cfg: Parsed pin configuration
5076 * return 1 if successful, 0 if the proper config is not found,
5077 * or a negative error code
5079 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
5080 struct auto_pin_cfg *cfg)
5082 struct hda_gen_spec *spec = codec->spec;
5085 parse_user_hints(codec);
5087 if (spec->mixer_nid && !spec->mixer_merge_nid)
5088 spec->mixer_merge_nid = spec->mixer_nid;
5090 if (cfg != &spec->autocfg) {
5091 spec->autocfg = *cfg;
5092 cfg = &spec->autocfg;
5095 if (!spec->main_out_badness)
5096 spec->main_out_badness = &hda_main_out_badness;
5097 if (!spec->extra_out_badness)
5098 spec->extra_out_badness = &hda_extra_out_badness;
5100 fill_all_dac_nids(codec);
5102 if (!cfg->line_outs) {
5103 if (cfg->dig_outs || cfg->dig_in_pin) {
5104 spec->multiout.max_channels = 2;
5105 spec->no_analog = 1;
5108 if (!cfg->num_inputs && !cfg->dig_in_pin)
5109 return 0; /* can't find valid BIOS pin config */
5112 if (!spec->no_primary_hp &&
5113 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
5114 cfg->line_outs <= cfg->hp_outs) {
5115 /* use HP as primary out */
5116 cfg->speaker_outs = cfg->line_outs;
5117 memcpy(cfg->speaker_pins, cfg->line_out_pins,
5118 sizeof(cfg->speaker_pins));
5119 cfg->line_outs = cfg->hp_outs;
5120 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
5122 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
5123 cfg->line_out_type = AUTO_PIN_HP_OUT;
5126 err = parse_output_paths(codec);
5129 err = create_multi_channel_mode(codec);
5132 err = create_multi_out_ctls(codec, cfg);
5135 err = create_hp_out_ctls(codec);
5138 err = create_speaker_out_ctls(codec);
5141 err = create_indep_hp_ctls(codec);
5144 err = create_loopback_mixing_ctl(codec);
5147 err = create_hp_mic(codec);
5150 err = create_input_ctls(codec);
5154 /* add power-down pin callbacks at first */
5155 add_all_pin_power_ctls(codec, false);
5157 spec->const_channel_count = spec->ext_channel_count;
5158 /* check the multiple speaker and headphone pins */
5159 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
5160 spec->const_channel_count = max(spec->const_channel_count,
5161 cfg->speaker_outs * 2);
5162 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
5163 spec->const_channel_count = max(spec->const_channel_count,
5165 spec->multiout.max_channels = max(spec->ext_channel_count,
5166 spec->const_channel_count);
5168 err = check_auto_mute_availability(codec);
5172 err = check_dyn_adc_switch(codec);
5176 err = check_auto_mic_availability(codec);
5180 /* add stereo mix if available and not enabled yet */
5181 if (!spec->auto_mic && spec->mixer_nid &&
5182 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
5183 spec->input_mux.num_items > 1) {
5184 err = parse_capture_source(codec, spec->mixer_nid,
5185 CFG_IDX_MIX, spec->num_all_adcs,
5192 err = create_capture_mixers(codec);
5196 err = parse_mic_boost(codec);
5200 /* create "Headphone Mic Jack Mode" if no input selection is
5201 * available (or user specifies add_jack_modes hint)
5203 if (spec->hp_mic_pin &&
5204 (spec->auto_mic || spec->input_mux.num_items == 1 ||
5205 spec->add_jack_modes)) {
5206 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
5211 if (spec->add_jack_modes) {
5212 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
5213 err = create_out_jack_modes(codec, cfg->line_outs,
5214 cfg->line_out_pins);
5218 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
5219 err = create_out_jack_modes(codec, cfg->hp_outs,
5226 /* add power-up pin callbacks at last */
5227 add_all_pin_power_ctls(codec, true);
5229 /* mute all aamix input initially */
5230 if (spec->mixer_nid)
5231 mute_all_mixer_nid(codec, spec->mixer_nid);
5234 parse_digital(codec);
5236 if (spec->power_down_unused || codec->power_save_node) {
5237 if (!codec->power_filter)
5238 codec->power_filter = snd_hda_gen_path_power_filter;
5239 if (!codec->patch_ops.stream_pm)
5240 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
5243 if (!spec->no_analog && spec->beep_nid) {
5244 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
5247 if (codec->beep && codec->power_save_node) {
5248 err = add_fake_beep_paths(codec);
5251 codec->beep->power_hook = beep_power_hook;
5257 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
5261 * Build control elements
5264 /* follower controls for virtual master */
5265 static const char * const follower_pfxs[] = {
5266 "Front", "Surround", "Center", "LFE", "Side",
5267 "Headphone", "Speaker", "Mono", "Line Out",
5268 "CLFE", "Bass Speaker", "PCM",
5269 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5270 "Headphone Front", "Headphone Surround", "Headphone CLFE",
5271 "Headphone Side", "Headphone+LO", "Speaker+LO",
5276 * snd_hda_gen_build_controls - Build controls from the parsed results
5277 * @codec: the HDA codec
5279 * Pass this to build_controls patch_ops.
5281 int snd_hda_gen_build_controls(struct hda_codec *codec)
5283 struct hda_gen_spec *spec = codec->spec;
5286 if (spec->kctls.used) {
5287 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5292 if (spec->multiout.dig_out_nid) {
5293 err = snd_hda_create_dig_out_ctls(codec,
5294 spec->multiout.dig_out_nid,
5295 spec->multiout.dig_out_nid,
5296 spec->pcm_rec[1]->pcm_type);
5299 if (!spec->no_analog) {
5300 err = snd_hda_create_spdif_share_sw(codec,
5304 spec->multiout.share_spdif = 1;
5307 if (spec->dig_in_nid) {
5308 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5313 /* if we have no master control, let's create it */
5314 if (!spec->no_analog && !spec->suppress_vmaster &&
5315 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
5316 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
5317 spec->vmaster_tlv, follower_pfxs,
5322 if (!spec->no_analog && !spec->suppress_vmaster &&
5323 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5324 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5325 NULL, follower_pfxs,
5327 true, &spec->vmaster_mute.sw_kctl);
5330 if (spec->vmaster_mute.hook) {
5331 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5332 spec->vmaster_mute_enum);
5333 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5337 free_kctls(spec); /* no longer needed */
5339 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5345 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
5352 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5353 struct hda_codec *codec,
5354 struct snd_pcm_substream *substream,
5357 struct hda_gen_spec *spec = codec->spec;
5358 if (spec->pcm_playback_hook)
5359 spec->pcm_playback_hook(hinfo, codec, substream, action);
5362 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5363 struct hda_codec *codec,
5364 struct snd_pcm_substream *substream,
5367 struct hda_gen_spec *spec = codec->spec;
5368 if (spec->pcm_capture_hook)
5369 spec->pcm_capture_hook(hinfo, codec, substream, action);
5373 * Analog playback callbacks
5375 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5376 struct hda_codec *codec,
5377 struct snd_pcm_substream *substream)
5379 struct hda_gen_spec *spec = codec->spec;
5382 mutex_lock(&spec->pcm_mutex);
5383 err = snd_hda_multi_out_analog_open(codec,
5384 &spec->multiout, substream,
5387 spec->active_streams |= 1 << STREAM_MULTI_OUT;
5388 call_pcm_playback_hook(hinfo, codec, substream,
5389 HDA_GEN_PCM_ACT_OPEN);
5391 mutex_unlock(&spec->pcm_mutex);
5395 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5396 struct hda_codec *codec,
5397 unsigned int stream_tag,
5398 unsigned int format,
5399 struct snd_pcm_substream *substream)
5401 struct hda_gen_spec *spec = codec->spec;
5404 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5405 stream_tag, format, substream);
5407 call_pcm_playback_hook(hinfo, codec, substream,
5408 HDA_GEN_PCM_ACT_PREPARE);
5412 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5413 struct hda_codec *codec,
5414 struct snd_pcm_substream *substream)
5416 struct hda_gen_spec *spec = codec->spec;
5419 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5421 call_pcm_playback_hook(hinfo, codec, substream,
5422 HDA_GEN_PCM_ACT_CLEANUP);
5426 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5427 struct hda_codec *codec,
5428 struct snd_pcm_substream *substream)
5430 struct hda_gen_spec *spec = codec->spec;
5431 mutex_lock(&spec->pcm_mutex);
5432 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
5433 call_pcm_playback_hook(hinfo, codec, substream,
5434 HDA_GEN_PCM_ACT_CLOSE);
5435 mutex_unlock(&spec->pcm_mutex);
5439 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5440 struct hda_codec *codec,
5441 struct snd_pcm_substream *substream)
5443 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5447 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5448 struct hda_codec *codec,
5449 unsigned int stream_tag,
5450 unsigned int format,
5451 struct snd_pcm_substream *substream)
5453 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5454 call_pcm_capture_hook(hinfo, codec, substream,
5455 HDA_GEN_PCM_ACT_PREPARE);
5459 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5460 struct hda_codec *codec,
5461 struct snd_pcm_substream *substream)
5463 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5464 call_pcm_capture_hook(hinfo, codec, substream,
5465 HDA_GEN_PCM_ACT_CLEANUP);
5469 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5470 struct hda_codec *codec,
5471 struct snd_pcm_substream *substream)
5473 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5477 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5478 struct hda_codec *codec,
5479 struct snd_pcm_substream *substream)
5481 struct hda_gen_spec *spec = codec->spec;
5484 mutex_lock(&spec->pcm_mutex);
5485 if (spec->indep_hp && !spec->indep_hp_enabled)
5488 spec->active_streams |= 1 << STREAM_INDEP_HP;
5489 call_pcm_playback_hook(hinfo, codec, substream,
5490 HDA_GEN_PCM_ACT_OPEN);
5491 mutex_unlock(&spec->pcm_mutex);
5495 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5496 struct hda_codec *codec,
5497 struct snd_pcm_substream *substream)
5499 struct hda_gen_spec *spec = codec->spec;
5500 mutex_lock(&spec->pcm_mutex);
5501 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
5502 call_pcm_playback_hook(hinfo, codec, substream,
5503 HDA_GEN_PCM_ACT_CLOSE);
5504 mutex_unlock(&spec->pcm_mutex);
5508 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5509 struct hda_codec *codec,
5510 unsigned int stream_tag,
5511 unsigned int format,
5512 struct snd_pcm_substream *substream)
5514 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5515 call_pcm_playback_hook(hinfo, codec, substream,
5516 HDA_GEN_PCM_ACT_PREPARE);
5520 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5521 struct hda_codec *codec,
5522 struct snd_pcm_substream *substream)
5524 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5525 call_pcm_playback_hook(hinfo, codec, substream,
5526 HDA_GEN_PCM_ACT_CLEANUP);
5533 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5534 struct hda_codec *codec,
5535 struct snd_pcm_substream *substream)
5537 struct hda_gen_spec *spec = codec->spec;
5538 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5541 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5542 struct hda_codec *codec,
5543 unsigned int stream_tag,
5544 unsigned int format,
5545 struct snd_pcm_substream *substream)
5547 struct hda_gen_spec *spec = codec->spec;
5548 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5549 stream_tag, format, substream);
5552 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5553 struct hda_codec *codec,
5554 struct snd_pcm_substream *substream)
5556 struct hda_gen_spec *spec = codec->spec;
5557 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5560 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5561 struct hda_codec *codec,
5562 struct snd_pcm_substream *substream)
5564 struct hda_gen_spec *spec = codec->spec;
5565 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5571 #define alt_capture_pcm_open capture_pcm_open
5572 #define alt_capture_pcm_close capture_pcm_close
5574 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5575 struct hda_codec *codec,
5576 unsigned int stream_tag,
5577 unsigned int format,
5578 struct snd_pcm_substream *substream)
5580 struct hda_gen_spec *spec = codec->spec;
5582 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5583 stream_tag, 0, format);
5584 call_pcm_capture_hook(hinfo, codec, substream,
5585 HDA_GEN_PCM_ACT_PREPARE);
5589 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5590 struct hda_codec *codec,
5591 struct snd_pcm_substream *substream)
5593 struct hda_gen_spec *spec = codec->spec;
5595 snd_hda_codec_cleanup_stream(codec,
5596 spec->adc_nids[substream->number + 1]);
5597 call_pcm_capture_hook(hinfo, codec, substream,
5598 HDA_GEN_PCM_ACT_CLEANUP);
5604 static const struct hda_pcm_stream pcm_analog_playback = {
5608 /* NID is set in build_pcms */
5610 .open = playback_pcm_open,
5611 .close = playback_pcm_close,
5612 .prepare = playback_pcm_prepare,
5613 .cleanup = playback_pcm_cleanup
5617 static const struct hda_pcm_stream pcm_analog_capture = {
5621 /* NID is set in build_pcms */
5623 .open = capture_pcm_open,
5624 .close = capture_pcm_close,
5625 .prepare = capture_pcm_prepare,
5626 .cleanup = capture_pcm_cleanup
5630 static const struct hda_pcm_stream pcm_analog_alt_playback = {
5634 /* NID is set in build_pcms */
5636 .open = alt_playback_pcm_open,
5637 .close = alt_playback_pcm_close,
5638 .prepare = alt_playback_pcm_prepare,
5639 .cleanup = alt_playback_pcm_cleanup
5643 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5644 .substreams = 2, /* can be overridden */
5647 /* NID is set in build_pcms */
5649 .open = alt_capture_pcm_open,
5650 .close = alt_capture_pcm_close,
5651 .prepare = alt_capture_pcm_prepare,
5652 .cleanup = alt_capture_pcm_cleanup
5656 static const struct hda_pcm_stream pcm_digital_playback = {
5660 /* NID is set in build_pcms */
5662 .open = dig_playback_pcm_open,
5663 .close = dig_playback_pcm_close,
5664 .prepare = dig_playback_pcm_prepare,
5665 .cleanup = dig_playback_pcm_cleanup
5669 static const struct hda_pcm_stream pcm_digital_capture = {
5673 /* NID is set in build_pcms */
5676 /* Used by build_pcms to flag that a PCM has no playback stream */
5677 static const struct hda_pcm_stream pcm_null_stream = {
5684 * dynamic changing ADC PCM streams
5686 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5688 struct hda_gen_spec *spec = codec->spec;
5689 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5691 if (spec->cur_adc && spec->cur_adc != new_adc) {
5692 /* stream is running, let's swap the current ADC */
5693 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5694 spec->cur_adc = new_adc;
5695 snd_hda_codec_setup_stream(codec, new_adc,
5696 spec->cur_adc_stream_tag, 0,
5697 spec->cur_adc_format);
5703 /* analog capture with dynamic dual-adc changes */
5704 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5705 struct hda_codec *codec,
5706 unsigned int stream_tag,
5707 unsigned int format,
5708 struct snd_pcm_substream *substream)
5710 struct hda_gen_spec *spec = codec->spec;
5711 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5712 spec->cur_adc_stream_tag = stream_tag;
5713 spec->cur_adc_format = format;
5714 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5715 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
5719 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5720 struct hda_codec *codec,
5721 struct snd_pcm_substream *substream)
5723 struct hda_gen_spec *spec = codec->spec;
5724 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5726 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
5730 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5734 .nid = 0, /* fill later */
5736 .prepare = dyn_adc_capture_pcm_prepare,
5737 .cleanup = dyn_adc_capture_pcm_cleanup
5741 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5742 const char *chip_name)
5748 strlcpy(str, chip_name, len);
5750 /* drop non-alnum chars after a space */
5751 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5752 if (!isalnum(p[1])) {
5757 strlcat(str, sfx, len);
5760 /* copy PCM stream info from @default_str, and override non-NULL entries
5761 * from @spec_str and @nid
5763 static void setup_pcm_stream(struct hda_pcm_stream *str,
5764 const struct hda_pcm_stream *default_str,
5765 const struct hda_pcm_stream *spec_str,
5768 *str = *default_str;
5772 if (spec_str->substreams)
5773 str->substreams = spec_str->substreams;
5774 if (spec_str->channels_min)
5775 str->channels_min = spec_str->channels_min;
5776 if (spec_str->channels_max)
5777 str->channels_max = spec_str->channels_max;
5778 if (spec_str->rates)
5779 str->rates = spec_str->rates;
5780 if (spec_str->formats)
5781 str->formats = spec_str->formats;
5782 if (spec_str->maxbps)
5783 str->maxbps = spec_str->maxbps;
5788 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5789 * @codec: the HDA codec
5791 * Pass this to build_pcms patch_ops.
5793 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5795 struct hda_gen_spec *spec = codec->spec;
5796 struct hda_pcm *info;
5797 bool have_multi_adcs;
5799 if (spec->no_analog)
5802 fill_pcm_stream_name(spec->stream_name_analog,
5803 sizeof(spec->stream_name_analog),
5804 " Analog", codec->core.chip_name);
5805 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5808 spec->pcm_rec[0] = info;
5810 if (spec->multiout.num_dacs > 0) {
5811 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5812 &pcm_analog_playback,
5813 spec->stream_analog_playback,
5814 spec->multiout.dac_nids[0]);
5815 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5816 spec->multiout.max_channels;
5817 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5818 spec->autocfg.line_outs == 2)
5819 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5822 if (spec->num_adc_nids) {
5823 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5824 (spec->dyn_adc_switch ?
5825 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5826 spec->stream_analog_capture,
5831 /* SPDIF for stream index #1 */
5832 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5833 fill_pcm_stream_name(spec->stream_name_digital,
5834 sizeof(spec->stream_name_digital),
5835 " Digital", codec->core.chip_name);
5836 info = snd_hda_codec_pcm_new(codec, "%s",
5837 spec->stream_name_digital);
5840 codec->follower_dig_outs = spec->multiout.follower_dig_outs;
5841 spec->pcm_rec[1] = info;
5842 if (spec->dig_out_type)
5843 info->pcm_type = spec->dig_out_type;
5845 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5846 if (spec->multiout.dig_out_nid)
5847 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5848 &pcm_digital_playback,
5849 spec->stream_digital_playback,
5850 spec->multiout.dig_out_nid);
5851 if (spec->dig_in_nid)
5852 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5853 &pcm_digital_capture,
5854 spec->stream_digital_capture,
5858 if (spec->no_analog)
5861 /* If the use of more than one ADC is requested for the current
5862 * model, configure a second analog capture-only PCM.
5864 have_multi_adcs = (spec->num_adc_nids > 1) &&
5865 !spec->dyn_adc_switch && !spec->auto_mic;
5866 /* Additional Analaog capture for index #2 */
5867 if (spec->alt_dac_nid || have_multi_adcs) {
5868 fill_pcm_stream_name(spec->stream_name_alt_analog,
5869 sizeof(spec->stream_name_alt_analog),
5870 " Alt Analog", codec->core.chip_name);
5871 info = snd_hda_codec_pcm_new(codec, "%s",
5872 spec->stream_name_alt_analog);
5875 spec->pcm_rec[2] = info;
5876 if (spec->alt_dac_nid)
5877 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5878 &pcm_analog_alt_playback,
5879 spec->stream_analog_alt_playback,
5882 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5883 &pcm_null_stream, NULL, 0);
5884 if (have_multi_adcs) {
5885 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5886 &pcm_analog_alt_capture,
5887 spec->stream_analog_alt_capture,
5889 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5890 spec->num_adc_nids - 1;
5892 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5893 &pcm_null_stream, NULL, 0);
5899 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5903 * Standard auto-parser initializations
5906 /* configure the given path as a proper output */
5907 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5909 struct nid_path *path;
5912 path = snd_hda_get_path_from_idx(codec, path_idx);
5913 if (!path || !path->depth)
5915 pin = path->path[path->depth - 1];
5916 restore_pin_ctl(codec, pin);
5917 snd_hda_activate_path(codec, path, path->active,
5918 aamix_default(codec->spec));
5919 set_pin_eapd(codec, pin, path->active);
5922 /* initialize primary output paths */
5923 static void init_multi_out(struct hda_codec *codec)
5925 struct hda_gen_spec *spec = codec->spec;
5928 for (i = 0; i < spec->autocfg.line_outs; i++)
5929 set_output_and_unmute(codec, spec->out_paths[i]);
5933 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5937 for (i = 0; i < num_outs; i++)
5938 set_output_and_unmute(codec, paths[i]);
5941 /* initialize hp and speaker paths */
5942 static void init_extra_out(struct hda_codec *codec)
5944 struct hda_gen_spec *spec = codec->spec;
5946 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5947 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5948 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5949 __init_extra_out(codec, spec->autocfg.speaker_outs,
5950 spec->speaker_paths);
5953 /* initialize multi-io paths */
5954 static void init_multi_io(struct hda_codec *codec)
5956 struct hda_gen_spec *spec = codec->spec;
5959 for (i = 0; i < spec->multi_ios; i++) {
5960 hda_nid_t pin = spec->multi_io[i].pin;
5961 struct nid_path *path;
5962 path = get_multiio_path(codec, i);
5965 if (!spec->multi_io[i].ctl_in)
5966 spec->multi_io[i].ctl_in =
5967 snd_hda_codec_get_pin_target(codec, pin);
5968 snd_hda_activate_path(codec, path, path->active,
5969 aamix_default(spec));
5973 static void init_aamix_paths(struct hda_codec *codec)
5975 struct hda_gen_spec *spec = codec->spec;
5977 if (!spec->have_aamix_ctl)
5979 if (!has_aamix_out_paths(spec))
5981 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5982 spec->aamix_out_paths[0],
5983 spec->autocfg.line_out_type);
5984 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5985 spec->aamix_out_paths[1],
5987 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5988 spec->aamix_out_paths[2],
5989 AUTO_PIN_SPEAKER_OUT);
5992 /* set up input pins and loopback paths */
5993 static void init_analog_input(struct hda_codec *codec)
5995 struct hda_gen_spec *spec = codec->spec;
5996 struct auto_pin_cfg *cfg = &spec->autocfg;
5999 for (i = 0; i < cfg->num_inputs; i++) {
6000 hda_nid_t nid = cfg->inputs[i].pin;
6001 if (is_input_pin(codec, nid))
6002 restore_pin_ctl(codec, nid);
6004 /* init loopback inputs */
6005 if (spec->mixer_nid) {
6006 resume_path_from_idx(codec, spec->loopback_paths[i]);
6007 resume_path_from_idx(codec, spec->loopback_merge_path);
6012 /* initialize ADC paths */
6013 static void init_input_src(struct hda_codec *codec)
6015 struct hda_gen_spec *spec = codec->spec;
6016 struct hda_input_mux *imux = &spec->input_mux;
6017 struct nid_path *path;
6020 if (spec->dyn_adc_switch)
6023 nums = spec->num_adc_nids;
6025 for (c = 0; c < nums; c++) {
6026 for (i = 0; i < imux->num_items; i++) {
6027 path = get_input_path(codec, c, i);
6029 bool active = path->active;
6030 if (i == spec->cur_mux[c])
6032 snd_hda_activate_path(codec, path, active, false);
6036 update_hp_mic(codec, c, true);
6039 if (spec->cap_sync_hook)
6040 spec->cap_sync_hook(codec, NULL, NULL);
6043 /* set right pin controls for digital I/O */
6044 static void init_digital(struct hda_codec *codec)
6046 struct hda_gen_spec *spec = codec->spec;
6050 for (i = 0; i < spec->autocfg.dig_outs; i++)
6051 set_output_and_unmute(codec, spec->digout_paths[i]);
6052 pin = spec->autocfg.dig_in_pin;
6054 restore_pin_ctl(codec, pin);
6055 resume_path_from_idx(codec, spec->digin_path);
6059 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
6060 * invalid unsol tags by some reason
6062 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
6064 const struct hda_pincfg *pin;
6067 snd_array_for_each(&codec->init_pins, i, pin) {
6068 hda_nid_t nid = pin->nid;
6069 if (is_jack_detectable(codec, nid) &&
6070 !snd_hda_jack_tbl_get(codec, nid))
6071 snd_hda_codec_write_cache(codec, nid, 0,
6072 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
6077 * snd_hda_gen_init - initialize the generic spec
6078 * @codec: the HDA codec
6080 * This can be put as patch_ops init function.
6082 int snd_hda_gen_init(struct hda_codec *codec)
6084 struct hda_gen_spec *spec = codec->spec;
6086 if (spec->init_hook)
6087 spec->init_hook(codec);
6089 if (!spec->skip_verbs)
6090 snd_hda_apply_verbs(codec);
6092 init_multi_out(codec);
6093 init_extra_out(codec);
6094 init_multi_io(codec);
6095 init_aamix_paths(codec);
6096 init_analog_input(codec);
6097 init_input_src(codec);
6098 init_digital(codec);
6100 clear_unsol_on_unused_pins(codec);
6102 sync_all_pin_power_ctls(codec);
6104 /* call init functions of standard auto-mute helpers */
6105 update_automute_all(codec);
6107 snd_hda_regmap_sync(codec);
6109 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
6110 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
6112 hda_call_check_power_status(codec, 0x01);
6115 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
6118 * snd_hda_gen_free - free the generic spec
6119 * @codec: the HDA codec
6121 * This can be put as patch_ops free function.
6123 void snd_hda_gen_free(struct hda_codec *codec)
6125 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
6126 snd_hda_gen_spec_free(codec->spec);
6130 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
6133 * snd_hda_gen_reboot_notify - Make codec enter D3 before rebooting
6134 * @codec: the HDA codec
6136 * This can be put as patch_ops reboot_notify function.
6138 void snd_hda_gen_reboot_notify(struct hda_codec *codec)
6140 /* Make the codec enter D3 to avoid spurious noises from the internal
6141 * speaker during (and after) reboot
6143 snd_hda_codec_set_power_to_all(codec, codec->core.afg, AC_PWRST_D3);
6144 snd_hda_codec_write(codec, codec->core.afg, 0,
6145 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
6148 EXPORT_SYMBOL_GPL(snd_hda_gen_reboot_notify);
6152 * snd_hda_gen_check_power_status - check the loopback power save state
6153 * @codec: the HDA codec
6154 * @nid: NID to inspect
6156 * This can be put as patch_ops check_power_status function.
6158 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
6160 struct hda_gen_spec *spec = codec->spec;
6161 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
6163 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
6168 * the generic codec support
6171 static const struct hda_codec_ops generic_patch_ops = {
6172 .build_controls = snd_hda_gen_build_controls,
6173 .build_pcms = snd_hda_gen_build_pcms,
6174 .init = snd_hda_gen_init,
6175 .free = snd_hda_gen_free,
6176 .unsol_event = snd_hda_jack_unsol_event,
6177 .reboot_notify = snd_hda_gen_reboot_notify,
6179 .check_power_status = snd_hda_gen_check_power_status,
6184 * snd_hda_parse_generic_codec - Generic codec parser
6185 * @codec: the HDA codec
6187 static int snd_hda_parse_generic_codec(struct hda_codec *codec)
6189 struct hda_gen_spec *spec;
6192 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
6195 snd_hda_gen_spec_init(spec);
6198 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
6202 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
6206 codec->patch_ops = generic_patch_ops;
6210 snd_hda_gen_free(codec);
6214 static const struct hda_device_id snd_hda_id_generic[] = {
6215 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
6218 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
6220 static struct hda_codec_driver generic_driver = {
6221 .id = snd_hda_id_generic,
6224 module_hda_codec_driver(generic_driver);
6226 MODULE_LICENSE("GPL");
6227 MODULE_DESCRIPTION("Generic HD-audio codec parser");