2 * Universal Interface for Intel High Definition Audio Codec
4 * Generic widget tree parser
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/sort.h>
27 #include <linux/delay.h>
28 #include <linux/ctype.h>
29 #include <linux/string.h>
30 #include <linux/bitops.h>
31 #include <linux/module.h>
32 #include <sound/core.h>
33 #include <sound/jack.h>
34 #include <sound/tlv.h>
35 #include "hda_codec.h"
36 #include "hda_local.h"
37 #include "hda_auto_parser.h"
40 #include "hda_generic.h"
44 * snd_hda_gen_spec_init - initialize hda_gen_spec struct
45 * @spec: hda_gen_spec object to initialize
47 * Initialize the given hda_gen_spec object.
49 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
51 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
52 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
53 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
54 mutex_init(&spec->pcm_mutex);
57 EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init);
60 * snd_hda_gen_add_kctl - Add a new kctl_new struct from the template
61 * @spec: hda_gen_spec object
62 * @name: name string to override the template, NULL if unchanged
63 * @temp: template for the new kctl
65 * Add a new kctl (actually snd_kcontrol_new to be instantiated later)
66 * element based on the given snd_kcontrol_new template @temp and the
67 * name string @name to the list in @spec.
68 * Returns the newly created object or NULL as error.
70 struct snd_kcontrol_new *
71 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
72 const struct snd_kcontrol_new *temp)
74 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
79 knew->name = kstrdup(name, GFP_KERNEL);
81 knew->name = kstrdup(knew->name, GFP_KERNEL);
86 EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl);
88 static void free_kctls(struct hda_gen_spec *spec)
90 if (spec->kctls.list) {
91 struct snd_kcontrol_new *kctl = spec->kctls.list;
93 for (i = 0; i < spec->kctls.used; i++)
96 snd_array_free(&spec->kctls);
99 static void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
104 snd_array_free(&spec->paths);
105 snd_array_free(&spec->loopback_list);
111 static void parse_user_hints(struct hda_codec *codec)
113 struct hda_gen_spec *spec = codec->spec;
116 val = snd_hda_get_bool_hint(codec, "jack_detect");
118 codec->no_jack_detect = !val;
119 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
121 codec->inv_jack_detect = !!val;
122 val = snd_hda_get_bool_hint(codec, "trigger_sense");
124 codec->no_trigger_sense = !val;
125 val = snd_hda_get_bool_hint(codec, "inv_eapd");
127 codec->inv_eapd = !!val;
128 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
130 codec->pcm_format_first = !!val;
131 val = snd_hda_get_bool_hint(codec, "sticky_stream");
133 codec->no_sticky_stream = !val;
134 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
136 codec->spdif_status_reset = !!val;
137 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
139 codec->pin_amp_workaround = !!val;
140 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
142 codec->single_adc_amp = !!val;
143 val = snd_hda_get_bool_hint(codec, "power_save_node");
145 codec->power_save_node = !!val;
147 val = snd_hda_get_bool_hint(codec, "auto_mute");
149 spec->suppress_auto_mute = !val;
150 val = snd_hda_get_bool_hint(codec, "auto_mic");
152 spec->suppress_auto_mic = !val;
153 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
155 spec->line_in_auto_switch = !!val;
156 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
158 spec->auto_mute_via_amp = !!val;
159 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
161 spec->need_dac_fix = !!val;
162 val = snd_hda_get_bool_hint(codec, "primary_hp");
164 spec->no_primary_hp = !val;
165 val = snd_hda_get_bool_hint(codec, "multi_io");
167 spec->no_multi_io = !val;
168 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
170 spec->multi_cap_vol = !!val;
171 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
173 spec->inv_dmic_split = !!val;
174 val = snd_hda_get_bool_hint(codec, "indep_hp");
176 spec->indep_hp = !!val;
177 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
179 spec->add_stereo_mix_input = !!val;
180 /* the following two are just for compatibility */
181 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
183 spec->add_jack_modes = !!val;
184 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
186 spec->add_jack_modes = !!val;
187 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
189 spec->add_jack_modes = !!val;
190 val = snd_hda_get_bool_hint(codec, "power_down_unused");
192 spec->power_down_unused = !!val;
193 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
195 spec->hp_mic = !!val;
196 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
198 spec->suppress_hp_mic_detect = !val;
199 val = snd_hda_get_bool_hint(codec, "vmaster");
201 spec->suppress_vmaster = !val;
203 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
204 spec->mixer_nid = val;
208 * pin control value accesses
211 #define update_pin_ctl(codec, pin, val) \
212 snd_hda_codec_update_cache(codec, pin, 0, \
213 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
215 /* restore the pinctl based on the cached value */
216 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
218 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
221 /* set the pinctl target value and write it if requested */
222 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
223 unsigned int val, bool do_write)
227 val = snd_hda_correct_pin_ctl(codec, pin, val);
228 snd_hda_codec_set_pin_target(codec, pin, val);
230 update_pin_ctl(codec, pin, val);
233 /* set pinctl target values for all given pins */
234 static void set_pin_targets(struct hda_codec *codec, int num_pins,
235 hda_nid_t *pins, unsigned int val)
238 for (i = 0; i < num_pins; i++)
239 set_pin_target(codec, pins[i], val, false);
246 /* return the position of NID in the list, or -1 if not found */
247 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
250 for (i = 0; i < nums; i++)
256 /* return true if the given NID is contained in the path */
257 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
259 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
262 static struct nid_path *get_nid_path(struct hda_codec *codec,
263 hda_nid_t from_nid, hda_nid_t to_nid,
266 struct hda_gen_spec *spec = codec->spec;
269 for (i = 0; i < spec->paths.used; i++) {
270 struct nid_path *path = snd_array_elem(&spec->paths, i);
271 if (path->depth <= 0)
273 if ((!from_nid || path->path[0] == from_nid) &&
274 (!to_nid || path->path[path->depth - 1] == to_nid)) {
276 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
277 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
285 * snd_hda_get_path_idx - get the index number corresponding to the path
287 * @codec: the HDA codec
288 * @path: nid_path object
290 * The returned index starts from 1, i.e. the actual array index with offset 1,
291 * and zero is handled as an invalid path
293 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
295 struct hda_gen_spec *spec = codec->spec;
296 struct nid_path *array = spec->paths.list;
299 if (!spec->paths.used)
302 if (idx < 0 || idx >= spec->paths.used)
306 EXPORT_SYMBOL_GPL(snd_hda_get_path_idx);
309 * snd_hda_get_path_from_idx - get the path instance corresponding to the
311 * @codec: the HDA codec
312 * @idx: the path index
314 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
316 struct hda_gen_spec *spec = codec->spec;
318 if (idx <= 0 || idx > spec->paths.used)
320 return snd_array_elem(&spec->paths, idx - 1);
322 EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx);
324 /* check whether the given DAC is already found in any existing paths */
325 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
327 struct hda_gen_spec *spec = codec->spec;
330 for (i = 0; i < spec->paths.used; i++) {
331 struct nid_path *path = snd_array_elem(&spec->paths, i);
332 if (path->path[0] == nid)
338 /* check whether the given two widgets can be connected */
339 static bool is_reachable_path(struct hda_codec *codec,
340 hda_nid_t from_nid, hda_nid_t to_nid)
342 if (!from_nid || !to_nid)
344 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
347 /* nid, dir and idx */
348 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
350 /* check whether the given ctl is already assigned in any path elements */
351 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
353 struct hda_gen_spec *spec = codec->spec;
356 val &= AMP_VAL_COMPARE_MASK;
357 for (i = 0; i < spec->paths.used; i++) {
358 struct nid_path *path = snd_array_elem(&spec->paths, i);
359 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
365 /* check whether a control with the given (nid, dir, idx) was assigned */
366 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
367 int dir, int idx, int type)
369 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
370 return is_ctl_used(codec, val, type);
373 static void print_nid_path(struct hda_codec *codec,
374 const char *pfx, struct nid_path *path)
381 for (i = 0; i < path->depth; i++)
382 pos += scnprintf(pos, sizeof(buf) - (pos - buf), "%s%02x",
383 pos != buf ? ":" : "",
386 codec_dbg(codec, "%s path: depth=%d '%s'\n", pfx, path->depth, buf);
389 /* called recursively */
390 static bool __parse_nid_path(struct hda_codec *codec,
391 hda_nid_t from_nid, hda_nid_t to_nid,
392 int anchor_nid, struct nid_path *path,
395 const hda_nid_t *conn;
398 if (to_nid == anchor_nid)
399 anchor_nid = 0; /* anchor passed */
400 else if (to_nid == (hda_nid_t)(-anchor_nid))
401 return false; /* hit the exclusive nid */
403 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
404 for (i = 0; i < nums; i++) {
405 if (conn[i] != from_nid) {
406 /* special case: when from_nid is 0,
407 * try to find an empty DAC
410 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
411 is_dac_already_used(codec, conn[i]))
414 /* anchor is not requested or already passed? */
418 if (depth >= MAX_NID_PATH_DEPTH)
420 for (i = 0; i < nums; i++) {
422 type = get_wcaps_type(get_wcaps(codec, conn[i]));
423 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
426 if (__parse_nid_path(codec, from_nid, conn[i],
427 anchor_nid, path, depth + 1))
433 path->path[path->depth] = conn[i];
434 path->idx[path->depth + 1] = i;
435 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
436 path->multi[path->depth + 1] = 1;
442 * snd_hda_parse_nid_path - parse the widget path from the given nid to
444 * @codec: the HDA codec
445 * @from_nid: the NID where the path start from
446 * @to_nid: the NID where the path ends at
447 * @anchor_nid: the anchor indication
448 * @path: the path object to store the result
450 * Returns true if a matching path is found.
452 * The parsing behavior depends on parameters:
453 * when @from_nid is 0, try to find an empty DAC;
454 * when @anchor_nid is set to a positive value, only paths through the widget
455 * with the given value are evaluated.
456 * when @anchor_nid is set to a negative value, paths through the widget
457 * with the negative of given value are excluded, only other paths are chosen.
458 * when @anchor_nid is zero, no special handling about path selection.
460 static bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
461 hda_nid_t to_nid, int anchor_nid,
462 struct nid_path *path)
464 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
465 path->path[path->depth] = to_nid;
473 * snd_hda_add_new_path - parse the path between the given NIDs and
474 * add to the path list
475 * @codec: the HDA codec
476 * @from_nid: the NID where the path start from
477 * @to_nid: the NID where the path ends at
478 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
480 * If no valid path is found, returns NULL.
483 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
484 hda_nid_t to_nid, int anchor_nid)
486 struct hda_gen_spec *spec = codec->spec;
487 struct nid_path *path;
489 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
492 /* check whether the path has been already added */
493 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
497 path = snd_array_new(&spec->paths);
500 memset(path, 0, sizeof(*path));
501 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
507 EXPORT_SYMBOL_GPL(snd_hda_add_new_path);
509 /* clear the given path as invalid so that it won't be picked up later */
510 static void invalidate_nid_path(struct hda_codec *codec, int idx)
512 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
515 memset(path, 0, sizeof(*path));
518 /* return a DAC if paired to the given pin by codec driver */
519 static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
521 struct hda_gen_spec *spec = codec->spec;
522 const hda_nid_t *list = spec->preferred_dacs;
526 for (; *list; list += 2)
532 /* look for an empty DAC slot */
533 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
536 struct hda_gen_spec *spec = codec->spec;
540 for (i = 0; i < spec->num_all_dacs; i++) {
541 hda_nid_t nid = spec->all_dacs[i];
542 if (!nid || is_dac_already_used(codec, nid))
544 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
545 if (is_digital != cap_digital)
547 if (is_reachable_path(codec, nid, pin))
553 /* replace the channels in the composed amp value with the given number */
554 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
556 val &= ~(0x3U << 16);
561 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
562 hda_nid_t nid2, int dir)
564 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
565 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
566 return (query_amp_caps(codec, nid1, dir) ==
567 query_amp_caps(codec, nid2, dir));
570 /* look for a widget suitable for assigning a mute switch in the path */
571 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
572 struct nid_path *path)
576 for (i = path->depth - 1; i >= 0; i--) {
577 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
578 return path->path[i];
579 if (i != path->depth - 1 && i != 0 &&
580 nid_has_mute(codec, path->path[i], HDA_INPUT))
581 return path->path[i];
586 /* look for a widget suitable for assigning a volume ctl in the path */
587 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
588 struct nid_path *path)
590 struct hda_gen_spec *spec = codec->spec;
593 for (i = path->depth - 1; i >= 0; i--) {
594 hda_nid_t nid = path->path[i];
595 if ((spec->out_vol_mask >> nid) & 1)
597 if (nid_has_volume(codec, nid, HDA_OUTPUT))
604 * path activation / deactivation
607 /* can have the amp-in capability? */
608 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
610 hda_nid_t nid = path->path[idx];
611 unsigned int caps = get_wcaps(codec, nid);
612 unsigned int type = get_wcaps_type(caps);
614 if (!(caps & AC_WCAP_IN_AMP))
616 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
621 /* can have the amp-out capability? */
622 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
624 hda_nid_t nid = path->path[idx];
625 unsigned int caps = get_wcaps(codec, nid);
626 unsigned int type = get_wcaps_type(caps);
628 if (!(caps & AC_WCAP_OUT_AMP))
630 if (type == AC_WID_PIN && !idx) /* only for output pins */
635 /* check whether the given (nid,dir,idx) is active */
636 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
637 unsigned int dir, unsigned int idx)
639 struct hda_gen_spec *spec = codec->spec;
640 int type = get_wcaps_type(get_wcaps(codec, nid));
643 if (nid == codec->core.afg)
646 for (n = 0; n < spec->paths.used; n++) {
647 struct nid_path *path = snd_array_elem(&spec->paths, n);
650 if (codec->power_save_node) {
651 if (!path->stream_enabled)
653 /* ignore unplugged paths except for DAC/ADC */
654 if (!(path->pin_enabled || path->pin_fixed) &&
655 type != AC_WID_AUD_OUT && type != AC_WID_AUD_IN)
658 for (i = 0; i < path->depth; i++) {
659 if (path->path[i] == nid) {
660 if (dir == HDA_OUTPUT || idx == -1 ||
670 /* check whether the NID is referred by any active paths */
671 #define is_active_nid_for_any(codec, nid) \
672 is_active_nid(codec, nid, HDA_OUTPUT, -1)
674 /* get the default amp value for the target state */
675 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
676 int dir, unsigned int caps, bool enable)
678 unsigned int val = 0;
680 if (caps & AC_AMPCAP_NUM_STEPS) {
683 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
685 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
692 /* is this a stereo widget or a stereo-to-mono mix? */
693 static bool is_stereo_amps(struct hda_codec *codec, hda_nid_t nid, int dir)
695 unsigned int wcaps = get_wcaps(codec, nid);
698 if (wcaps & AC_WCAP_STEREO)
700 if (dir != HDA_INPUT || get_wcaps_type(wcaps) != AC_WID_AUD_MIX)
702 if (snd_hda_get_num_conns(codec, nid) != 1)
704 if (snd_hda_get_connections(codec, nid, &conn, 1) < 0)
706 return !!(get_wcaps(codec, conn) & AC_WCAP_STEREO);
709 /* initialize the amp value (only at the first time) */
710 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
712 unsigned int caps = query_amp_caps(codec, nid, dir);
713 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
715 if (is_stereo_amps(codec, nid, dir))
716 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
718 snd_hda_codec_amp_init(codec, nid, 0, dir, idx, 0xff, val);
721 /* update the amp, doing in stereo or mono depending on NID */
722 static int update_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx,
723 unsigned int mask, unsigned int val)
725 if (is_stereo_amps(codec, nid, dir))
726 return snd_hda_codec_amp_stereo(codec, nid, dir, idx,
729 return snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
733 /* calculate amp value mask we can modify;
734 * if the given amp is controlled by mixers, don't touch it
736 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
737 hda_nid_t nid, int dir, int idx,
740 unsigned int mask = 0xff;
742 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
743 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
746 if (caps & AC_AMPCAP_NUM_STEPS) {
747 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
748 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
754 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
755 int idx, int idx_to_check, bool enable)
758 unsigned int mask, val;
760 caps = query_amp_caps(codec, nid, dir);
761 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
762 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
767 update_amp(codec, nid, dir, idx, mask, val);
770 static void check_and_activate_amp(struct hda_codec *codec, hda_nid_t nid,
771 int dir, int idx, int idx_to_check,
774 /* check whether the given amp is still used by others */
775 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
777 activate_amp(codec, nid, dir, idx, idx_to_check, enable);
780 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
783 hda_nid_t nid = path->path[i];
784 init_amp(codec, nid, HDA_OUTPUT, 0);
785 check_and_activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
788 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
789 int i, bool enable, bool add_aamix)
791 struct hda_gen_spec *spec = codec->spec;
792 const hda_nid_t *conn;
795 hda_nid_t nid = path->path[i];
797 nums = snd_hda_get_conn_list(codec, nid, &conn);
798 type = get_wcaps_type(get_wcaps(codec, nid));
799 if (type == AC_WID_PIN ||
800 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
806 for (n = 0; n < nums; n++)
807 init_amp(codec, nid, HDA_INPUT, n);
809 /* here is a little bit tricky in comparison with activate_amp_out();
810 * when aa-mixer is available, we need to enable the path as well
812 for (n = 0; n < nums; n++) {
814 if (conn[n] != spec->mixer_merge_nid)
816 /* when aamix is disabled, force to off */
818 activate_amp(codec, nid, HDA_INPUT, n, n, false);
822 check_and_activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
826 /* sync power of each widget in the given path */
827 static hda_nid_t path_power_update(struct hda_codec *codec,
828 struct nid_path *path,
829 bool allow_powerdown)
831 hda_nid_t nid, changed = 0;
834 for (i = 0; i < path->depth; i++) {
836 if (!(get_wcaps(codec, nid) & AC_WCAP_POWER))
838 if (nid == codec->core.afg)
840 if (!allow_powerdown || is_active_nid_for_any(codec, nid))
844 power = snd_hda_codec_read(codec, nid, 0,
845 AC_VERB_GET_POWER_STATE, 0);
846 if (power != (state | (state << 4))) {
847 snd_hda_codec_write(codec, nid, 0,
848 AC_VERB_SET_POWER_STATE, state);
850 /* all known codecs seem to be capable to handl
851 * widgets state even in D3, so far.
852 * if any new codecs need to restore the widget
853 * states after D0 transition, call the function
857 if (state == AC_PWRST_D0)
858 snd_hdac_regmap_sync_node(&codec->core, nid);
865 /* do sync with the last power state change */
866 static void sync_power_state_change(struct hda_codec *codec, hda_nid_t nid)
870 snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
875 * snd_hda_activate_path - activate or deactivate the given path
876 * @codec: the HDA codec
877 * @path: the path to activate/deactivate
878 * @enable: flag to activate or not
879 * @add_aamix: enable the input from aamix NID
881 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
883 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
884 bool enable, bool add_aamix)
886 struct hda_gen_spec *spec = codec->spec;
889 path->active = enable;
891 /* make sure the widget is powered up */
892 if (enable && (spec->power_down_unused || codec->power_save_node))
893 path_power_update(codec, path, codec->power_save_node);
895 for (i = path->depth - 1; i >= 0; i--) {
896 hda_nid_t nid = path->path[i];
898 if (enable && path->multi[i])
899 snd_hda_codec_update_cache(codec, nid, 0,
900 AC_VERB_SET_CONNECT_SEL,
902 if (has_amp_in(codec, path, i))
903 activate_amp_in(codec, path, i, enable, add_aamix);
904 if (has_amp_out(codec, path, i))
905 activate_amp_out(codec, path, i, enable);
908 EXPORT_SYMBOL_GPL(snd_hda_activate_path);
910 /* if the given path is inactive, put widgets into D3 (only if suitable) */
911 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
913 struct hda_gen_spec *spec = codec->spec;
915 if (!(spec->power_down_unused || codec->power_save_node) || path->active)
917 sync_power_state_change(codec, path_power_update(codec, path, true));
920 /* turn on/off EAPD on the given pin */
921 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
923 struct hda_gen_spec *spec = codec->spec;
924 if (spec->own_eapd_ctl ||
925 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
927 if (spec->keep_eapd_on && !enable)
931 snd_hda_codec_update_cache(codec, pin, 0,
932 AC_VERB_SET_EAPD_BTLENABLE,
933 enable ? 0x02 : 0x00);
936 /* re-initialize the path specified by the given path index */
937 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
939 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
941 snd_hda_activate_path(codec, path, path->active, false);
946 * Helper functions for creating mixer ctl elements
949 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
950 struct snd_ctl_elem_value *ucontrol);
951 static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
952 struct snd_ctl_elem_value *ucontrol);
953 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
954 struct snd_ctl_elem_value *ucontrol);
961 static const struct snd_kcontrol_new control_templates[] = {
962 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
963 /* only the put callback is replaced for handling the special mute */
965 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
966 .subdevice = HDA_SUBDEV_AMP_FLAG,
967 .info = snd_hda_mixer_amp_switch_info,
968 .get = snd_hda_mixer_amp_switch_get,
969 .put = hda_gen_mixer_mute_put, /* replaced */
970 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
973 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
974 .info = snd_hda_mixer_amp_switch_info,
975 .get = hda_gen_bind_mute_get,
976 .put = hda_gen_bind_mute_put, /* replaced */
977 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
981 /* add dynamic controls from template */
982 static struct snd_kcontrol_new *
983 add_control(struct hda_gen_spec *spec, int type, const char *name,
984 int cidx, unsigned long val)
986 struct snd_kcontrol_new *knew;
988 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
992 if (get_amp_nid_(val))
993 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
994 knew->private_value = val;
998 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
999 const char *pfx, const char *dir,
1000 const char *sfx, int cidx, unsigned long val)
1002 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1003 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
1004 if (!add_control(spec, type, name, cidx, val))
1009 #define add_pb_vol_ctrl(spec, type, pfx, val) \
1010 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1011 #define add_pb_sw_ctrl(spec, type, pfx, val) \
1012 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1013 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1014 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1015 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1016 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1018 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1019 unsigned int chs, struct nid_path *path)
1024 val = path->ctls[NID_PATH_VOL_CTL];
1027 val = amp_val_replace_channels(val, chs);
1028 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
1031 /* return the channel bits suitable for the given path->ctls[] */
1032 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
1035 int chs = 1; /* mono (left only) */
1037 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
1038 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
1039 chs = 3; /* stereo */
1044 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
1045 struct nid_path *path)
1047 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
1048 return add_vol_ctl(codec, pfx, cidx, chs, path);
1051 /* create a mute-switch for the given mixer widget;
1052 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1054 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
1055 unsigned int chs, struct nid_path *path)
1058 int type = HDA_CTL_WIDGET_MUTE;
1062 val = path->ctls[NID_PATH_MUTE_CTL];
1065 val = amp_val_replace_channels(val, chs);
1066 if (get_amp_direction_(val) == HDA_INPUT) {
1067 hda_nid_t nid = get_amp_nid_(val);
1068 int nums = snd_hda_get_num_conns(codec, nid);
1070 type = HDA_CTL_BIND_MUTE;
1074 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
1077 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
1078 int cidx, struct nid_path *path)
1080 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
1081 return add_sw_ctl(codec, pfx, cidx, chs, path);
1084 /* playback mute control with the software mute bit check */
1085 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
1086 struct snd_ctl_elem_value *ucontrol)
1088 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1089 struct hda_gen_spec *spec = codec->spec;
1091 if (spec->auto_mute_via_amp) {
1092 hda_nid_t nid = get_amp_nid(kcontrol);
1093 bool enabled = !((spec->mute_bits >> nid) & 1);
1094 ucontrol->value.integer.value[0] &= enabled;
1095 ucontrol->value.integer.value[1] &= enabled;
1099 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
1100 struct snd_ctl_elem_value *ucontrol)
1102 sync_auto_mute_bits(kcontrol, ucontrol);
1103 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1107 * Bound mute controls
1109 #define AMP_VAL_IDX_SHIFT 19
1110 #define AMP_VAL_IDX_MASK (0x0f<<19)
1112 static int hda_gen_bind_mute_get(struct snd_kcontrol *kcontrol,
1113 struct snd_ctl_elem_value *ucontrol)
1115 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1119 mutex_lock(&codec->control_mutex);
1120 pval = kcontrol->private_value;
1121 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1122 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1123 kcontrol->private_value = pval;
1124 mutex_unlock(&codec->control_mutex);
1128 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
1129 struct snd_ctl_elem_value *ucontrol)
1131 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1133 int i, indices, err = 0, change = 0;
1135 sync_auto_mute_bits(kcontrol, ucontrol);
1137 mutex_lock(&codec->control_mutex);
1138 pval = kcontrol->private_value;
1139 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1140 for (i = 0; i < indices; i++) {
1141 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1142 (i << AMP_VAL_IDX_SHIFT);
1143 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1148 kcontrol->private_value = pval;
1149 mutex_unlock(&codec->control_mutex);
1150 return err < 0 ? err : change;
1153 /* any ctl assigned to the path with the given index? */
1154 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
1156 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
1157 return path && path->ctls[ctl_type];
1160 static const char * const channel_name[4] = {
1161 "Front", "Surround", "CLFE", "Side"
1164 /* give some appropriate ctl name prefix for the given line out channel */
1165 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
1166 int *index, int ctl_type)
1168 struct hda_gen_spec *spec = codec->spec;
1169 struct auto_pin_cfg *cfg = &spec->autocfg;
1172 if (cfg->line_outs == 1 && !spec->multi_ios &&
1173 !codec->force_pin_prefix &&
1174 !cfg->hp_outs && !cfg->speaker_outs)
1175 return spec->vmaster_mute.hook ? "PCM" : "Master";
1177 /* if there is really a single DAC used in the whole output paths,
1178 * use it master (or "PCM" if a vmaster hook is present)
1180 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1181 !codec->force_pin_prefix &&
1182 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1183 return spec->vmaster_mute.hook ? "PCM" : "Master";
1185 /* multi-io channels */
1186 if (ch >= cfg->line_outs)
1187 return channel_name[ch];
1189 switch (cfg->line_out_type) {
1190 case AUTO_PIN_SPEAKER_OUT:
1191 /* if the primary channel vol/mute is shared with HP volume,
1192 * don't name it as Speaker
1194 if (!ch && cfg->hp_outs &&
1195 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1197 if (cfg->line_outs == 1)
1199 if (cfg->line_outs == 2)
1200 return ch ? "Bass Speaker" : "Speaker";
1202 case AUTO_PIN_HP_OUT:
1203 /* if the primary channel vol/mute is shared with spk volume,
1204 * don't name it as Headphone
1206 if (!ch && cfg->speaker_outs &&
1207 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1209 /* for multi-io case, only the primary out */
1210 if (ch && spec->multi_ios)
1214 case AUTO_PIN_LINE_OUT:
1215 /* This deals with the case where one HP or one Speaker or
1216 * one HP + one Speaker need to share the DAC with LO
1219 bool hp_lo_shared = false, spk_lo_shared = false;
1221 if (cfg->speaker_outs)
1222 spk_lo_shared = !path_has_mixer(codec,
1223 spec->speaker_paths[0], ctl_type);
1225 hp_lo_shared = !path_has_mixer(codec, spec->hp_paths[0], ctl_type);
1226 if (hp_lo_shared && spk_lo_shared)
1227 return spec->vmaster_mute.hook ? "PCM" : "Master";
1229 return "Headphone+LO";
1231 return "Speaker+LO";
1235 /* for a single channel output, we don't have to name the channel */
1236 if (cfg->line_outs == 1 && !spec->multi_ios)
1239 if (ch >= ARRAY_SIZE(channel_name)) {
1244 return channel_name[ch];
1248 * Parse output paths
1251 /* badness definition */
1253 /* No primary DAC is found for the main output */
1254 BAD_NO_PRIMARY_DAC = 0x10000,
1255 /* No DAC is found for the extra output */
1256 BAD_NO_DAC = 0x4000,
1257 /* No possible multi-ios */
1258 BAD_MULTI_IO = 0x120,
1259 /* No individual DAC for extra output */
1260 BAD_NO_EXTRA_DAC = 0x102,
1261 /* No individual DAC for extra surrounds */
1262 BAD_NO_EXTRA_SURR_DAC = 0x101,
1263 /* Primary DAC shared with main surrounds */
1264 BAD_SHARED_SURROUND = 0x100,
1265 /* No independent HP possible */
1266 BAD_NO_INDEP_HP = 0x10,
1267 /* Primary DAC shared with main CLFE */
1268 BAD_SHARED_CLFE = 0x10,
1269 /* Primary DAC shared with extra surrounds */
1270 BAD_SHARED_EXTRA_SURROUND = 0x10,
1271 /* Volume widget is shared */
1272 BAD_SHARED_VOL = 0x10,
1275 /* look for widgets in the given path which are appropriate for
1276 * volume and mute controls, and assign the values to ctls[].
1278 * When no appropriate widget is found in the path, the badness value
1279 * is incremented depending on the situation. The function returns the
1280 * total badness for both volume and mute controls.
1282 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1284 struct hda_gen_spec *spec = codec->spec;
1290 return BAD_SHARED_VOL * 2;
1292 if (path->ctls[NID_PATH_VOL_CTL] ||
1293 path->ctls[NID_PATH_MUTE_CTL])
1294 return 0; /* already evaluated */
1296 nid = look_for_out_vol_nid(codec, path);
1298 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1299 if (spec->dac_min_mute)
1300 val |= HDA_AMP_VAL_MIN_MUTE;
1301 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1302 badness += BAD_SHARED_VOL;
1304 path->ctls[NID_PATH_VOL_CTL] = val;
1306 badness += BAD_SHARED_VOL;
1307 nid = look_for_out_mute_nid(codec, path);
1309 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1310 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1311 nid_has_mute(codec, nid, HDA_OUTPUT))
1312 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1314 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1315 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1316 badness += BAD_SHARED_VOL;
1318 path->ctls[NID_PATH_MUTE_CTL] = val;
1320 badness += BAD_SHARED_VOL;
1324 const struct badness_table hda_main_out_badness = {
1325 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1326 .no_dac = BAD_NO_DAC,
1327 .shared_primary = BAD_NO_PRIMARY_DAC,
1328 .shared_surr = BAD_SHARED_SURROUND,
1329 .shared_clfe = BAD_SHARED_CLFE,
1330 .shared_surr_main = BAD_SHARED_SURROUND,
1332 EXPORT_SYMBOL_GPL(hda_main_out_badness);
1334 const struct badness_table hda_extra_out_badness = {
1335 .no_primary_dac = BAD_NO_DAC,
1336 .no_dac = BAD_NO_DAC,
1337 .shared_primary = BAD_NO_EXTRA_DAC,
1338 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1339 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1340 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1342 EXPORT_SYMBOL_GPL(hda_extra_out_badness);
1344 /* get the DAC of the primary output corresponding to the given array index */
1345 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1347 struct hda_gen_spec *spec = codec->spec;
1348 struct auto_pin_cfg *cfg = &spec->autocfg;
1350 if (cfg->line_outs > idx)
1351 return spec->private_dac_nids[idx];
1352 idx -= cfg->line_outs;
1353 if (spec->multi_ios > idx)
1354 return spec->multi_io[idx].dac;
1358 /* return the DAC if it's reachable, otherwise zero */
1359 static inline hda_nid_t try_dac(struct hda_codec *codec,
1360 hda_nid_t dac, hda_nid_t pin)
1362 return is_reachable_path(codec, dac, pin) ? dac : 0;
1365 /* try to assign DACs to pins and return the resultant badness */
1366 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1367 const hda_nid_t *pins, hda_nid_t *dacs,
1369 const struct badness_table *bad)
1371 struct hda_gen_spec *spec = codec->spec;
1379 for (i = 0; i < num_outs; i++) {
1380 struct nid_path *path;
1381 hda_nid_t pin = pins[i];
1383 if (!spec->obey_preferred_dacs) {
1384 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1386 badness += assign_out_path_ctls(codec, path);
1391 dacs[i] = get_preferred_dac(codec, pin);
1393 if (is_dac_already_used(codec, dacs[i]))
1394 badness += bad->shared_primary;
1395 } else if (spec->obey_preferred_dacs) {
1396 badness += BAD_NO_PRIMARY_DAC;
1400 dacs[i] = look_for_dac(codec, pin, false);
1401 if (!dacs[i] && !i) {
1402 /* try to steal the DAC of surrounds for the front */
1403 for (j = 1; j < num_outs; j++) {
1404 if (is_reachable_path(codec, dacs[j], pin)) {
1407 invalidate_nid_path(codec, path_idx[j]);
1416 dac = try_dac(codec, get_primary_out(codec, i), pin);
1418 dac = try_dac(codec, dacs[0], pin);
1420 dac = try_dac(codec, get_primary_out(codec, i), pin);
1423 badness += bad->shared_primary;
1425 badness += bad->shared_surr;
1427 badness += bad->shared_clfe;
1428 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1429 dac = spec->private_dac_nids[0];
1430 badness += bad->shared_surr_main;
1432 badness += bad->no_primary_dac;
1434 badness += bad->no_dac;
1438 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1439 if (!path && !i && spec->mixer_nid) {
1440 /* try with aamix */
1441 path = snd_hda_add_new_path(codec, dac, pin, 0);
1445 badness += bad->no_dac;
1447 /* print_nid_path(codec, "output", path); */
1448 path->active = true;
1449 path_idx[i] = snd_hda_get_path_idx(codec, path);
1450 badness += assign_out_path_ctls(codec, path);
1457 /* return NID if the given pin has only a single connection to a certain DAC */
1458 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1460 struct hda_gen_spec *spec = codec->spec;
1462 hda_nid_t nid_found = 0;
1464 for (i = 0; i < spec->num_all_dacs; i++) {
1465 hda_nid_t nid = spec->all_dacs[i];
1466 if (!nid || is_dac_already_used(codec, nid))
1468 if (is_reachable_path(codec, nid, pin)) {
1477 /* check whether the given pin can be a multi-io pin */
1478 static bool can_be_multiio_pin(struct hda_codec *codec,
1479 unsigned int location, hda_nid_t nid)
1481 unsigned int defcfg, caps;
1483 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1484 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1486 if (location && get_defcfg_location(defcfg) != location)
1488 caps = snd_hda_query_pin_caps(codec, nid);
1489 if (!(caps & AC_PINCAP_OUT))
1494 /* count the number of input pins that are capable to be multi-io */
1495 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1497 struct hda_gen_spec *spec = codec->spec;
1498 struct auto_pin_cfg *cfg = &spec->autocfg;
1499 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1500 unsigned int location = get_defcfg_location(defcfg);
1504 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1505 for (i = 0; i < cfg->num_inputs; i++) {
1506 if (cfg->inputs[i].type != type)
1508 if (can_be_multiio_pin(codec, location,
1509 cfg->inputs[i].pin))
1519 * When hardwired is set, try to fill ony hardwired pins, and returns
1520 * zero if any pins are filled, non-zero if nothing found.
1521 * When hardwired is off, try to fill possible input pins, and returns
1522 * the badness value.
1524 static int fill_multi_ios(struct hda_codec *codec,
1525 hda_nid_t reference_pin,
1528 struct hda_gen_spec *spec = codec->spec;
1529 struct auto_pin_cfg *cfg = &spec->autocfg;
1530 int type, i, j, num_pins, old_pins;
1531 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1532 unsigned int location = get_defcfg_location(defcfg);
1534 struct nid_path *path;
1536 old_pins = spec->multi_ios;
1540 num_pins = count_multiio_pins(codec, reference_pin);
1544 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1545 for (i = 0; i < cfg->num_inputs; i++) {
1546 hda_nid_t nid = cfg->inputs[i].pin;
1549 if (cfg->inputs[i].type != type)
1551 if (!can_be_multiio_pin(codec, location, nid))
1553 for (j = 0; j < spec->multi_ios; j++) {
1554 if (nid == spec->multi_io[j].pin)
1557 if (j < spec->multi_ios)
1561 dac = get_dac_if_single(codec, nid);
1563 dac = look_for_dac(codec, nid, false);
1568 path = snd_hda_add_new_path(codec, dac, nid,
1574 /* print_nid_path(codec, "multiio", path); */
1575 spec->multi_io[spec->multi_ios].pin = nid;
1576 spec->multi_io[spec->multi_ios].dac = dac;
1577 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1578 snd_hda_get_path_idx(codec, path);
1580 if (spec->multi_ios >= 2)
1586 badness = BAD_MULTI_IO;
1587 if (old_pins == spec->multi_ios) {
1589 return 1; /* nothing found */
1591 return badness; /* no badness if nothing found */
1593 if (!hardwired && spec->multi_ios < 2) {
1594 /* cancel newly assigned paths */
1595 spec->paths.used -= spec->multi_ios - old_pins;
1596 spec->multi_ios = old_pins;
1600 /* assign volume and mute controls */
1601 for (i = old_pins; i < spec->multi_ios; i++) {
1602 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1603 badness += assign_out_path_ctls(codec, path);
1609 /* map DACs for all pins in the list if they are single connections */
1610 static bool map_singles(struct hda_codec *codec, int outs,
1611 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1613 struct hda_gen_spec *spec = codec->spec;
1616 for (i = 0; i < outs; i++) {
1617 struct nid_path *path;
1621 dac = get_dac_if_single(codec, pins[i]);
1624 path = snd_hda_add_new_path(codec, dac, pins[i],
1626 if (!path && !i && spec->mixer_nid)
1627 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1631 /* print_nid_path(codec, "output", path); */
1632 path->active = true;
1633 path_idx[i] = snd_hda_get_path_idx(codec, path);
1639 static inline bool has_aamix_out_paths(struct hda_gen_spec *spec)
1641 return spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1642 spec->aamix_out_paths[2];
1645 /* create a new path including aamix if available, and return its index */
1646 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1648 struct hda_gen_spec *spec = codec->spec;
1649 struct nid_path *path;
1650 hda_nid_t path_dac, dac, pin;
1652 path = snd_hda_get_path_from_idx(codec, path_idx);
1653 if (!path || !path->depth ||
1654 is_nid_contained(path, spec->mixer_nid))
1656 path_dac = path->path[0];
1657 dac = spec->private_dac_nids[0];
1658 pin = path->path[path->depth - 1];
1659 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1661 if (dac != path_dac)
1663 else if (spec->multiout.hp_out_nid[0])
1664 dac = spec->multiout.hp_out_nid[0];
1665 else if (spec->multiout.extra_out_nid[0])
1666 dac = spec->multiout.extra_out_nid[0];
1670 path = snd_hda_add_new_path(codec, dac, pin,
1675 /* print_nid_path(codec, "output-aamix", path); */
1676 path->active = false; /* unused as default */
1677 path->pin_fixed = true; /* static route */
1678 return snd_hda_get_path_idx(codec, path);
1681 /* check whether the independent HP is available with the current config */
1682 static bool indep_hp_possible(struct hda_codec *codec)
1684 struct hda_gen_spec *spec = codec->spec;
1685 struct auto_pin_cfg *cfg = &spec->autocfg;
1686 struct nid_path *path;
1689 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1690 idx = spec->out_paths[0];
1692 idx = spec->hp_paths[0];
1693 path = snd_hda_get_path_from_idx(codec, idx);
1697 /* assume no path conflicts unless aamix is involved */
1698 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1701 /* check whether output paths contain aamix */
1702 for (i = 0; i < cfg->line_outs; i++) {
1703 if (spec->out_paths[i] == idx)
1705 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1706 if (path && is_nid_contained(path, spec->mixer_nid))
1709 for (i = 0; i < cfg->speaker_outs; i++) {
1710 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1711 if (path && is_nid_contained(path, spec->mixer_nid))
1718 /* fill the empty entries in the dac array for speaker/hp with the
1719 * shared dac pointed by the paths
1721 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1722 hda_nid_t *dacs, int *path_idx)
1724 struct nid_path *path;
1727 for (i = 0; i < num_outs; i++) {
1730 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1733 dacs[i] = path->path[0];
1737 /* fill in the dac_nids table from the parsed pin configuration */
1738 static int fill_and_eval_dacs(struct hda_codec *codec,
1739 bool fill_hardwired,
1740 bool fill_mio_first)
1742 struct hda_gen_spec *spec = codec->spec;
1743 struct auto_pin_cfg *cfg = &spec->autocfg;
1744 int i, err, badness;
1746 /* set num_dacs once to full for look_for_dac() */
1747 spec->multiout.num_dacs = cfg->line_outs;
1748 spec->multiout.dac_nids = spec->private_dac_nids;
1749 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1750 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1751 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1752 spec->multi_ios = 0;
1753 snd_array_free(&spec->paths);
1755 /* clear path indices */
1756 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1757 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1758 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1759 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1760 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1761 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1762 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1763 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1767 /* fill hard-wired DACs first */
1768 if (fill_hardwired) {
1771 mapped = map_singles(codec, cfg->line_outs,
1773 spec->private_dac_nids,
1775 mapped |= map_singles(codec, cfg->hp_outs,
1777 spec->multiout.hp_out_nid,
1779 mapped |= map_singles(codec, cfg->speaker_outs,
1781 spec->multiout.extra_out_nid,
1782 spec->speaker_paths);
1783 if (!spec->no_multi_io &&
1784 fill_mio_first && cfg->line_outs == 1 &&
1785 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1786 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1793 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1794 spec->private_dac_nids, spec->out_paths,
1795 spec->main_out_badness);
1797 if (!spec->no_multi_io && fill_mio_first &&
1798 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1799 /* try to fill multi-io first */
1800 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1803 /* we don't count badness at this stage yet */
1806 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1807 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1808 spec->multiout.hp_out_nid,
1810 spec->extra_out_badness);
1815 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1816 err = try_assign_dacs(codec, cfg->speaker_outs,
1818 spec->multiout.extra_out_nid,
1819 spec->speaker_paths,
1820 spec->extra_out_badness);
1825 if (!spec->no_multi_io &&
1826 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1827 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1833 if (spec->mixer_nid) {
1834 spec->aamix_out_paths[0] =
1835 check_aamix_out_path(codec, spec->out_paths[0]);
1836 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1837 spec->aamix_out_paths[1] =
1838 check_aamix_out_path(codec, spec->hp_paths[0]);
1839 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1840 spec->aamix_out_paths[2] =
1841 check_aamix_out_path(codec, spec->speaker_paths[0]);
1844 if (!spec->no_multi_io &&
1845 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1846 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1847 spec->multi_ios = 1; /* give badness */
1849 /* re-count num_dacs and squash invalid entries */
1850 spec->multiout.num_dacs = 0;
1851 for (i = 0; i < cfg->line_outs; i++) {
1852 if (spec->private_dac_nids[i])
1853 spec->multiout.num_dacs++;
1855 memmove(spec->private_dac_nids + i,
1856 spec->private_dac_nids + i + 1,
1857 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1858 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1862 spec->ext_channel_count = spec->min_channel_count =
1863 spec->multiout.num_dacs * 2;
1865 if (spec->multi_ios == 2) {
1866 for (i = 0; i < 2; i++)
1867 spec->private_dac_nids[spec->multiout.num_dacs++] =
1868 spec->multi_io[i].dac;
1869 } else if (spec->multi_ios) {
1870 spec->multi_ios = 0;
1871 badness += BAD_MULTI_IO;
1874 if (spec->indep_hp && !indep_hp_possible(codec))
1875 badness += BAD_NO_INDEP_HP;
1877 /* re-fill the shared DAC for speaker / headphone */
1878 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1879 refill_shared_dacs(codec, cfg->hp_outs,
1880 spec->multiout.hp_out_nid,
1882 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1883 refill_shared_dacs(codec, cfg->speaker_outs,
1884 spec->multiout.extra_out_nid,
1885 spec->speaker_paths);
1890 #define DEBUG_BADNESS
1892 #ifdef DEBUG_BADNESS
1893 #define debug_badness(fmt, ...) \
1894 codec_dbg(codec, fmt, ##__VA_ARGS__)
1896 #define debug_badness(fmt, ...) \
1897 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1900 #ifdef DEBUG_BADNESS
1901 static inline void print_nid_path_idx(struct hda_codec *codec,
1902 const char *pfx, int idx)
1904 struct nid_path *path;
1906 path = snd_hda_get_path_from_idx(codec, idx);
1908 print_nid_path(codec, pfx, path);
1911 static void debug_show_configs(struct hda_codec *codec,
1912 struct auto_pin_cfg *cfg)
1914 struct hda_gen_spec *spec = codec->spec;
1915 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1918 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1919 cfg->line_out_pins[0], cfg->line_out_pins[1],
1920 cfg->line_out_pins[2], cfg->line_out_pins[3],
1921 spec->multiout.dac_nids[0],
1922 spec->multiout.dac_nids[1],
1923 spec->multiout.dac_nids[2],
1924 spec->multiout.dac_nids[3],
1925 lo_type[cfg->line_out_type]);
1926 for (i = 0; i < cfg->line_outs; i++)
1927 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1928 if (spec->multi_ios > 0)
1929 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1931 spec->multi_io[0].pin, spec->multi_io[1].pin,
1932 spec->multi_io[0].dac, spec->multi_io[1].dac);
1933 for (i = 0; i < spec->multi_ios; i++)
1934 print_nid_path_idx(codec, " mio",
1935 spec->out_paths[cfg->line_outs + i]);
1937 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1938 cfg->hp_pins[0], cfg->hp_pins[1],
1939 cfg->hp_pins[2], cfg->hp_pins[3],
1940 spec->multiout.hp_out_nid[0],
1941 spec->multiout.hp_out_nid[1],
1942 spec->multiout.hp_out_nid[2],
1943 spec->multiout.hp_out_nid[3]);
1944 for (i = 0; i < cfg->hp_outs; i++)
1945 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1946 if (cfg->speaker_outs)
1947 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1948 cfg->speaker_pins[0], cfg->speaker_pins[1],
1949 cfg->speaker_pins[2], cfg->speaker_pins[3],
1950 spec->multiout.extra_out_nid[0],
1951 spec->multiout.extra_out_nid[1],
1952 spec->multiout.extra_out_nid[2],
1953 spec->multiout.extra_out_nid[3]);
1954 for (i = 0; i < cfg->speaker_outs; i++)
1955 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1956 for (i = 0; i < 3; i++)
1957 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1960 #define debug_show_configs(codec, cfg) /* NOP */
1963 /* find all available DACs of the codec */
1964 static void fill_all_dac_nids(struct hda_codec *codec)
1966 struct hda_gen_spec *spec = codec->spec;
1969 spec->num_all_dacs = 0;
1970 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1971 for_each_hda_codec_node(nid, codec) {
1972 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1974 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1975 codec_err(codec, "Too many DACs!\n");
1978 spec->all_dacs[spec->num_all_dacs++] = nid;
1982 static int parse_output_paths(struct hda_codec *codec)
1984 struct hda_gen_spec *spec = codec->spec;
1985 struct auto_pin_cfg *cfg = &spec->autocfg;
1986 struct auto_pin_cfg *best_cfg;
1988 int best_badness = INT_MAX;
1990 bool fill_hardwired = true, fill_mio_first = true;
1991 bool best_wired = true, best_mio = true;
1992 bool hp_spk_swapped = false;
1994 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
2000 badness = fill_and_eval_dacs(codec, fill_hardwired,
2006 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
2007 cfg->line_out_type, fill_hardwired, fill_mio_first,
2009 debug_show_configs(codec, cfg);
2010 if (badness < best_badness) {
2011 best_badness = badness;
2013 best_wired = fill_hardwired;
2014 best_mio = fill_mio_first;
2018 fill_mio_first = !fill_mio_first;
2019 if (!fill_mio_first)
2021 fill_hardwired = !fill_hardwired;
2022 if (!fill_hardwired)
2026 hp_spk_swapped = true;
2027 if (cfg->speaker_outs > 0 &&
2028 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2029 cfg->hp_outs = cfg->line_outs;
2030 memcpy(cfg->hp_pins, cfg->line_out_pins,
2031 sizeof(cfg->hp_pins));
2032 cfg->line_outs = cfg->speaker_outs;
2033 memcpy(cfg->line_out_pins, cfg->speaker_pins,
2034 sizeof(cfg->speaker_pins));
2035 cfg->speaker_outs = 0;
2036 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
2037 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
2038 fill_hardwired = true;
2041 if (cfg->hp_outs > 0 &&
2042 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2043 cfg->speaker_outs = cfg->line_outs;
2044 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2045 sizeof(cfg->speaker_pins));
2046 cfg->line_outs = cfg->hp_outs;
2047 memcpy(cfg->line_out_pins, cfg->hp_pins,
2048 sizeof(cfg->hp_pins));
2050 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2051 cfg->line_out_type = AUTO_PIN_HP_OUT;
2052 fill_hardwired = true;
2059 debug_badness("==> restoring best_cfg\n");
2061 fill_and_eval_dacs(codec, best_wired, best_mio);
2063 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2064 cfg->line_out_type, best_wired, best_mio);
2065 debug_show_configs(codec, cfg);
2067 if (cfg->line_out_pins[0]) {
2068 struct nid_path *path;
2069 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
2071 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
2072 if (spec->vmaster_nid) {
2073 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2074 HDA_OUTPUT, spec->vmaster_tlv);
2075 if (spec->dac_min_mute)
2076 spec->vmaster_tlv[3] |= TLV_DB_SCALE_MUTE;
2080 /* set initial pinctl targets */
2081 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
2085 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
2086 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2087 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
2088 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
2089 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
2090 set_pin_targets(codec, cfg->speaker_outs,
2091 cfg->speaker_pins, val);
2094 /* clear indep_hp flag if not available */
2095 if (spec->indep_hp && !indep_hp_possible(codec))
2102 /* add playback controls from the parsed DAC table */
2103 static int create_multi_out_ctls(struct hda_codec *codec,
2104 const struct auto_pin_cfg *cfg)
2106 struct hda_gen_spec *spec = codec->spec;
2107 int i, err, noutputs;
2109 noutputs = cfg->line_outs;
2110 if (spec->multi_ios > 0 && cfg->line_outs < 3)
2111 noutputs += spec->multi_ios;
2113 for (i = 0; i < noutputs; i++) {
2116 struct nid_path *path;
2118 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
2122 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
2123 if (!name || !strcmp(name, "CLFE")) {
2125 err = add_vol_ctl(codec, "Center", 0, 1, path);
2128 err = add_vol_ctl(codec, "LFE", 0, 2, path);
2132 err = add_stereo_vol(codec, name, index, path);
2137 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
2138 if (!name || !strcmp(name, "CLFE")) {
2139 err = add_sw_ctl(codec, "Center", 0, 1, path);
2142 err = add_sw_ctl(codec, "LFE", 0, 2, path);
2146 err = add_stereo_sw(codec, name, index, path);
2154 static int create_extra_out(struct hda_codec *codec, int path_idx,
2155 const char *pfx, int cidx)
2157 struct nid_path *path;
2160 path = snd_hda_get_path_from_idx(codec, path_idx);
2163 err = add_stereo_vol(codec, pfx, cidx, path);
2166 err = add_stereo_sw(codec, pfx, cidx, path);
2172 /* add playback controls for speaker and HP outputs */
2173 static int create_extra_outs(struct hda_codec *codec, int num_pins,
2174 const int *paths, const char *pfx)
2178 for (i = 0; i < num_pins; i++) {
2180 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2183 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
2184 name = "Bass Speaker";
2185 else if (num_pins >= 3) {
2186 snprintf(tmp, sizeof(tmp), "%s %s",
2187 pfx, channel_name[i]);
2193 err = create_extra_out(codec, paths[i], name, idx);
2200 static int create_hp_out_ctls(struct hda_codec *codec)
2202 struct hda_gen_spec *spec = codec->spec;
2203 return create_extra_outs(codec, spec->autocfg.hp_outs,
2208 static int create_speaker_out_ctls(struct hda_codec *codec)
2210 struct hda_gen_spec *spec = codec->spec;
2211 return create_extra_outs(codec, spec->autocfg.speaker_outs,
2212 spec->speaker_paths,
2217 * independent HP controls
2220 static void call_hp_automute(struct hda_codec *codec,
2221 struct hda_jack_callback *jack);
2222 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2223 struct snd_ctl_elem_info *uinfo)
2225 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2228 static int indep_hp_get(struct snd_kcontrol *kcontrol,
2229 struct snd_ctl_elem_value *ucontrol)
2231 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2232 struct hda_gen_spec *spec = codec->spec;
2233 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2237 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2238 int nomix_path_idx, int mix_path_idx,
2241 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2242 struct snd_ctl_elem_value *ucontrol)
2244 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2245 struct hda_gen_spec *spec = codec->spec;
2246 unsigned int select = ucontrol->value.enumerated.item[0];
2249 mutex_lock(&spec->pcm_mutex);
2250 if (spec->active_streams) {
2255 if (spec->indep_hp_enabled != select) {
2257 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2258 dacp = &spec->private_dac_nids[0];
2260 dacp = &spec->multiout.hp_out_nid[0];
2262 /* update HP aamix paths in case it conflicts with indep HP */
2263 if (spec->have_aamix_ctl) {
2264 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2265 update_aamix_paths(codec, spec->aamix_mode,
2267 spec->aamix_out_paths[0],
2268 spec->autocfg.line_out_type);
2270 update_aamix_paths(codec, spec->aamix_mode,
2272 spec->aamix_out_paths[1],
2276 spec->indep_hp_enabled = select;
2277 if (spec->indep_hp_enabled)
2280 *dacp = spec->alt_dac_nid;
2282 call_hp_automute(codec, NULL);
2286 mutex_unlock(&spec->pcm_mutex);
2290 static const struct snd_kcontrol_new indep_hp_ctl = {
2291 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2292 .name = "Independent HP",
2293 .info = indep_hp_info,
2294 .get = indep_hp_get,
2295 .put = indep_hp_put,
2299 static int create_indep_hp_ctls(struct hda_codec *codec)
2301 struct hda_gen_spec *spec = codec->spec;
2304 if (!spec->indep_hp)
2306 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2307 dac = spec->multiout.dac_nids[0];
2309 dac = spec->multiout.hp_out_nid[0];
2315 spec->indep_hp_enabled = false;
2316 spec->alt_dac_nid = dac;
2317 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2323 * channel mode enum control
2326 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2327 struct snd_ctl_elem_info *uinfo)
2329 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2330 struct hda_gen_spec *spec = codec->spec;
2333 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2335 uinfo->value.enumerated.items = spec->multi_ios + 1;
2336 if (uinfo->value.enumerated.item > spec->multi_ios)
2337 uinfo->value.enumerated.item = spec->multi_ios;
2338 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2339 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2343 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2344 struct snd_ctl_elem_value *ucontrol)
2346 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2347 struct hda_gen_spec *spec = codec->spec;
2348 ucontrol->value.enumerated.item[0] =
2349 (spec->ext_channel_count - spec->min_channel_count) / 2;
2353 static inline struct nid_path *
2354 get_multiio_path(struct hda_codec *codec, int idx)
2356 struct hda_gen_spec *spec = codec->spec;
2357 return snd_hda_get_path_from_idx(codec,
2358 spec->out_paths[spec->autocfg.line_outs + idx]);
2361 static void update_automute_all(struct hda_codec *codec);
2363 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2364 * used for output paths
2366 static bool aamix_default(struct hda_gen_spec *spec)
2368 return !spec->have_aamix_ctl || spec->aamix_mode;
2371 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2373 struct hda_gen_spec *spec = codec->spec;
2374 hda_nid_t nid = spec->multi_io[idx].pin;
2375 struct nid_path *path;
2377 path = get_multiio_path(codec, idx);
2381 if (path->active == output)
2385 set_pin_target(codec, nid, PIN_OUT, true);
2386 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2387 set_pin_eapd(codec, nid, true);
2389 set_pin_eapd(codec, nid, false);
2390 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2391 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2392 path_power_down_sync(codec, path);
2395 /* update jack retasking in case it modifies any of them */
2396 update_automute_all(codec);
2401 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2402 struct snd_ctl_elem_value *ucontrol)
2404 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2405 struct hda_gen_spec *spec = codec->spec;
2408 ch = ucontrol->value.enumerated.item[0];
2409 if (ch < 0 || ch > spec->multi_ios)
2411 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2413 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2414 for (i = 0; i < spec->multi_ios; i++)
2415 set_multi_io(codec, i, i < ch);
2416 spec->multiout.max_channels = max(spec->ext_channel_count,
2417 spec->const_channel_count);
2418 if (spec->need_dac_fix)
2419 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2423 static const struct snd_kcontrol_new channel_mode_enum = {
2424 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2425 .name = "Channel Mode",
2426 .info = ch_mode_info,
2431 static int create_multi_channel_mode(struct hda_codec *codec)
2433 struct hda_gen_spec *spec = codec->spec;
2435 if (spec->multi_ios > 0) {
2436 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2443 * aamix loopback enable/disable switch
2446 #define loopback_mixing_info indep_hp_info
2448 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2449 struct snd_ctl_elem_value *ucontrol)
2451 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2452 struct hda_gen_spec *spec = codec->spec;
2453 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2457 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2458 int nomix_path_idx, int mix_path_idx,
2461 struct hda_gen_spec *spec = codec->spec;
2462 struct nid_path *nomix_path, *mix_path;
2464 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2465 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2466 if (!nomix_path || !mix_path)
2469 /* if HP aamix path is driven from a different DAC and the
2470 * independent HP mode is ON, can't turn on aamix path
2472 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2473 mix_path->path[0] != spec->alt_dac_nid)
2477 snd_hda_activate_path(codec, nomix_path, false, true);
2478 snd_hda_activate_path(codec, mix_path, true, true);
2479 path_power_down_sync(codec, nomix_path);
2481 snd_hda_activate_path(codec, mix_path, false, false);
2482 snd_hda_activate_path(codec, nomix_path, true, false);
2483 path_power_down_sync(codec, mix_path);
2487 /* re-initialize the output paths; only called from loopback_mixing_put() */
2488 static void update_output_paths(struct hda_codec *codec, int num_outs,
2491 struct hda_gen_spec *spec = codec->spec;
2492 struct nid_path *path;
2495 for (i = 0; i < num_outs; i++) {
2496 path = snd_hda_get_path_from_idx(codec, paths[i]);
2498 snd_hda_activate_path(codec, path, path->active,
2503 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2504 struct snd_ctl_elem_value *ucontrol)
2506 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2507 struct hda_gen_spec *spec = codec->spec;
2508 const struct auto_pin_cfg *cfg = &spec->autocfg;
2509 unsigned int val = ucontrol->value.enumerated.item[0];
2511 if (val == spec->aamix_mode)
2513 spec->aamix_mode = val;
2514 if (has_aamix_out_paths(spec)) {
2515 update_aamix_paths(codec, val, spec->out_paths[0],
2516 spec->aamix_out_paths[0],
2517 cfg->line_out_type);
2518 update_aamix_paths(codec, val, spec->hp_paths[0],
2519 spec->aamix_out_paths[1],
2521 update_aamix_paths(codec, val, spec->speaker_paths[0],
2522 spec->aamix_out_paths[2],
2523 AUTO_PIN_SPEAKER_OUT);
2525 update_output_paths(codec, cfg->line_outs, spec->out_paths);
2526 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
2527 update_output_paths(codec, cfg->hp_outs, spec->hp_paths);
2528 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
2529 update_output_paths(codec, cfg->speaker_outs,
2530 spec->speaker_paths);
2535 static const struct snd_kcontrol_new loopback_mixing_enum = {
2536 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2537 .name = "Loopback Mixing",
2538 .info = loopback_mixing_info,
2539 .get = loopback_mixing_get,
2540 .put = loopback_mixing_put,
2543 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2545 struct hda_gen_spec *spec = codec->spec;
2547 if (!spec->mixer_nid)
2549 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2551 spec->have_aamix_ctl = 1;
2556 * shared headphone/mic handling
2559 static void call_update_outputs(struct hda_codec *codec);
2561 /* for shared I/O, change the pin-control accordingly */
2562 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2564 struct hda_gen_spec *spec = codec->spec;
2569 pin = spec->hp_mic_pin;
2570 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2573 val = snd_hda_codec_get_pin_target(codec, pin);
2583 val = snd_hda_get_default_vref(codec, pin);
2584 /* if the HP pin doesn't support VREF and the codec driver gives an
2585 * alternative pin, set up the VREF on that pin instead
2587 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2588 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2589 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2590 if (vref_val != AC_PINCTL_VREF_HIZ)
2591 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2592 PIN_IN | (as_mic ? vref_val : 0));
2595 if (!spec->hp_mic_jack_modes) {
2600 set_pin_target(codec, pin, val, true);
2601 call_hp_automute(codec, NULL);
2605 /* create a shared input with the headphone out */
2606 static int create_hp_mic(struct hda_codec *codec)
2608 struct hda_gen_spec *spec = codec->spec;
2609 struct auto_pin_cfg *cfg = &spec->autocfg;
2610 unsigned int defcfg;
2613 if (!spec->hp_mic) {
2614 if (spec->suppress_hp_mic_detect)
2616 /* automatic detection: only if no input or a single internal
2617 * input pin is found, try to detect the shared hp/mic
2619 if (cfg->num_inputs > 1)
2621 else if (cfg->num_inputs == 1) {
2622 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2623 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2628 spec->hp_mic = 0; /* clear once */
2629 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2633 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2634 nid = cfg->line_out_pins[0];
2635 else if (cfg->hp_outs > 0)
2636 nid = cfg->hp_pins[0];
2640 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2641 return 0; /* no input */
2643 cfg->inputs[cfg->num_inputs].pin = nid;
2644 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2645 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2648 spec->hp_mic_pin = nid;
2649 /* we can't handle auto-mic together with HP-mic */
2650 spec->suppress_auto_mic = 1;
2651 codec_dbg(codec, "Enable shared I/O jack on NID 0x%x\n", nid);
2659 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2661 static const char * const out_jack_texts[] = {
2662 "Line Out", "Headphone Out",
2665 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2666 struct snd_ctl_elem_info *uinfo)
2668 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2671 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2672 struct snd_ctl_elem_value *ucontrol)
2674 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2675 hda_nid_t nid = kcontrol->private_value;
2676 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2677 ucontrol->value.enumerated.item[0] = 1;
2679 ucontrol->value.enumerated.item[0] = 0;
2683 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2684 struct snd_ctl_elem_value *ucontrol)
2686 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2687 hda_nid_t nid = kcontrol->private_value;
2690 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2691 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2693 snd_hda_set_pin_ctl_cache(codec, nid, val);
2697 static const struct snd_kcontrol_new out_jack_mode_enum = {
2698 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2699 .info = out_jack_mode_info,
2700 .get = out_jack_mode_get,
2701 .put = out_jack_mode_put,
2704 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2706 struct hda_gen_spec *spec = codec->spec;
2709 for (i = 0; i < spec->kctls.used; i++) {
2710 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2711 if (!strcmp(kctl->name, name) && kctl->index == idx)
2717 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2718 char *name, size_t name_len)
2720 struct hda_gen_spec *spec = codec->spec;
2723 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2724 strlcat(name, " Jack Mode", name_len);
2726 for (; find_kctl_name(codec, name, idx); idx++)
2730 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2732 struct hda_gen_spec *spec = codec->spec;
2733 if (spec->add_jack_modes) {
2734 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2735 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2741 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2744 struct hda_gen_spec *spec = codec->spec;
2747 for (i = 0; i < num_pins; i++) {
2748 hda_nid_t pin = pins[i];
2749 if (pin == spec->hp_mic_pin)
2751 if (get_out_jack_num_items(codec, pin) > 1) {
2752 struct snd_kcontrol_new *knew;
2753 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2754 get_jack_mode_name(codec, pin, name, sizeof(name));
2755 knew = snd_hda_gen_add_kctl(spec, name,
2756 &out_jack_mode_enum);
2759 knew->private_value = pin;
2770 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2773 static const char * const vref_texts[NUM_VREFS] = {
2774 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2775 "", "Mic 80pc Bias", "Mic 100pc Bias"
2778 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2780 unsigned int pincap;
2782 pincap = snd_hda_query_pin_caps(codec, pin);
2783 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2784 /* filter out unusual vrefs */
2785 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2789 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2790 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2792 unsigned int i, n = 0;
2794 for (i = 0; i < NUM_VREFS; i++) {
2795 if (vref_caps & (1 << i)) {
2804 /* convert back from the vref ctl index to the enum item index */
2805 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2807 unsigned int i, n = 0;
2809 for (i = 0; i < NUM_VREFS; i++) {
2812 if (vref_caps & (1 << i))
2818 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2819 struct snd_ctl_elem_info *uinfo)
2821 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2822 hda_nid_t nid = kcontrol->private_value;
2823 unsigned int vref_caps = get_vref_caps(codec, nid);
2825 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2827 /* set the right text */
2828 strcpy(uinfo->value.enumerated.name,
2829 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2833 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2834 struct snd_ctl_elem_value *ucontrol)
2836 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2837 hda_nid_t nid = kcontrol->private_value;
2838 unsigned int vref_caps = get_vref_caps(codec, nid);
2841 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2842 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2846 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2847 struct snd_ctl_elem_value *ucontrol)
2849 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2850 hda_nid_t nid = kcontrol->private_value;
2851 unsigned int vref_caps = get_vref_caps(codec, nid);
2852 unsigned int val, idx;
2854 val = snd_hda_codec_get_pin_target(codec, nid);
2855 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2856 if (idx == ucontrol->value.enumerated.item[0])
2859 val &= ~AC_PINCTL_VREFEN;
2860 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2861 snd_hda_set_pin_ctl_cache(codec, nid, val);
2865 static const struct snd_kcontrol_new in_jack_mode_enum = {
2866 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2867 .info = in_jack_mode_info,
2868 .get = in_jack_mode_get,
2869 .put = in_jack_mode_put,
2872 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2874 struct hda_gen_spec *spec = codec->spec;
2876 if (spec->add_jack_modes)
2877 nitems = hweight32(get_vref_caps(codec, pin));
2878 return nitems ? nitems : 1;
2881 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2883 struct hda_gen_spec *spec = codec->spec;
2884 struct snd_kcontrol_new *knew;
2885 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2886 unsigned int defcfg;
2888 if (pin == spec->hp_mic_pin)
2889 return 0; /* already done in create_out_jack_mode() */
2891 /* no jack mode for fixed pins */
2892 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2893 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2896 /* no multiple vref caps? */
2897 if (get_in_jack_num_items(codec, pin) <= 1)
2900 get_jack_mode_name(codec, pin, name, sizeof(name));
2901 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2904 knew->private_value = pin;
2909 * HP/mic shared jack mode
2911 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2912 struct snd_ctl_elem_info *uinfo)
2914 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2915 hda_nid_t nid = kcontrol->private_value;
2916 int out_jacks = get_out_jack_num_items(codec, nid);
2917 int in_jacks = get_in_jack_num_items(codec, nid);
2918 const char *text = NULL;
2921 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2923 uinfo->value.enumerated.items = out_jacks + in_jacks;
2924 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2925 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2926 idx = uinfo->value.enumerated.item;
2927 if (idx < out_jacks) {
2929 text = out_jack_texts[idx];
2931 text = "Headphone Out";
2935 unsigned int vref_caps = get_vref_caps(codec, nid);
2936 text = vref_texts[get_vref_idx(vref_caps, idx)];
2941 strcpy(uinfo->value.enumerated.name, text);
2945 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2947 int out_jacks = get_out_jack_num_items(codec, nid);
2948 int in_jacks = get_in_jack_num_items(codec, nid);
2949 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2952 if (val & PIN_OUT) {
2953 if (out_jacks > 1 && val == PIN_HP)
2955 } else if (val & PIN_IN) {
2958 unsigned int vref_caps = get_vref_caps(codec, nid);
2959 val &= AC_PINCTL_VREFEN;
2960 idx += cvt_from_vref_idx(vref_caps, val);
2966 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2967 struct snd_ctl_elem_value *ucontrol)
2969 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2970 hda_nid_t nid = kcontrol->private_value;
2971 ucontrol->value.enumerated.item[0] =
2972 get_cur_hp_mic_jack_mode(codec, nid);
2976 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2977 struct snd_ctl_elem_value *ucontrol)
2979 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2980 hda_nid_t nid = kcontrol->private_value;
2981 int out_jacks = get_out_jack_num_items(codec, nid);
2982 int in_jacks = get_in_jack_num_items(codec, nid);
2983 unsigned int val, oldval, idx;
2985 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2986 idx = ucontrol->value.enumerated.item[0];
2990 if (idx < out_jacks) {
2992 val = idx ? PIN_HP : PIN_OUT;
2998 unsigned int vref_caps = get_vref_caps(codec, nid);
2999 val = snd_hda_codec_get_pin_target(codec, nid);
3000 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
3001 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
3003 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
3005 snd_hda_set_pin_ctl_cache(codec, nid, val);
3006 call_hp_automute(codec, NULL);
3011 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
3012 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3013 .info = hp_mic_jack_mode_info,
3014 .get = hp_mic_jack_mode_get,
3015 .put = hp_mic_jack_mode_put,
3018 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
3020 struct hda_gen_spec *spec = codec->spec;
3021 struct snd_kcontrol_new *knew;
3023 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
3024 &hp_mic_jack_mode_enum);
3027 knew->private_value = pin;
3028 spec->hp_mic_jack_modes = 1;
3036 /* add the powersave loopback-list entry */
3037 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
3039 struct hda_amp_list *list;
3041 list = snd_array_new(&spec->loopback_list);
3045 list->dir = HDA_INPUT;
3047 spec->loopback.amplist = spec->loopback_list.list;
3051 /* return true if either a volume or a mute amp is found for the given
3052 * aamix path; the amp has to be either in the mixer node or its direct leaf
3054 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
3055 hda_nid_t pin, unsigned int *mix_val,
3056 unsigned int *mute_val)
3059 const hda_nid_t *list;
3062 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
3066 *mix_val = *mute_val = 0;
3067 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
3068 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3069 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
3070 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
3071 if (*mix_val && *mute_val)
3074 /* check leaf node */
3075 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
3076 if (num_conns < idx)
3079 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT) &&
3080 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_VOL_CTL))
3081 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3082 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT) &&
3083 !is_ctl_associated(codec, nid, HDA_OUTPUT, 0, NID_PATH_MUTE_CTL))
3084 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3086 return *mix_val || *mute_val;
3089 /* create input playback/capture controls for the given pin */
3090 static int new_analog_input(struct hda_codec *codec, int input_idx,
3091 hda_nid_t pin, const char *ctlname, int ctlidx,
3094 struct hda_gen_spec *spec = codec->spec;
3095 struct nid_path *path;
3096 unsigned int mix_val, mute_val;
3099 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
3102 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
3105 print_nid_path(codec, "loopback", path);
3106 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
3108 idx = path->idx[path->depth - 1];
3110 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
3113 path->ctls[NID_PATH_VOL_CTL] = mix_val;
3117 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
3120 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
3123 path->active = true;
3124 path->stream_enabled = true; /* no DAC/ADC involved */
3125 err = add_loopback_list(spec, mix_nid, idx);
3129 if (spec->mixer_nid != spec->mixer_merge_nid &&
3130 !spec->loopback_merge_path) {
3131 path = snd_hda_add_new_path(codec, spec->mixer_nid,
3132 spec->mixer_merge_nid, 0);
3134 print_nid_path(codec, "loopback-merge", path);
3135 path->active = true;
3136 path->pin_fixed = true; /* static route */
3137 path->stream_enabled = true; /* no DAC/ADC involved */
3138 spec->loopback_merge_path =
3139 snd_hda_get_path_idx(codec, path);
3146 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
3148 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
3149 return (pincap & AC_PINCAP_IN) != 0;
3152 /* Parse the codec tree and retrieve ADCs */
3153 static int fill_adc_nids(struct hda_codec *codec)
3155 struct hda_gen_spec *spec = codec->spec;
3157 hda_nid_t *adc_nids = spec->adc_nids;
3158 int max_nums = ARRAY_SIZE(spec->adc_nids);
3161 for_each_hda_codec_node(nid, codec) {
3162 unsigned int caps = get_wcaps(codec, nid);
3163 int type = get_wcaps_type(caps);
3165 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
3167 adc_nids[nums] = nid;
3168 if (++nums >= max_nums)
3171 spec->num_adc_nids = nums;
3173 /* copy the detected ADCs to all_adcs[] */
3174 spec->num_all_adcs = nums;
3175 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
3180 /* filter out invalid adc_nids that don't give all active input pins;
3181 * if needed, check whether dynamic ADC-switching is available
3183 static int check_dyn_adc_switch(struct hda_codec *codec)
3185 struct hda_gen_spec *spec = codec->spec;
3186 struct hda_input_mux *imux = &spec->input_mux;
3187 unsigned int ok_bits;
3192 for (n = 0; n < spec->num_adc_nids; n++) {
3193 for (i = 0; i < imux->num_items; i++) {
3194 if (!spec->input_paths[i][n])
3197 if (i >= imux->num_items) {
3198 ok_bits |= (1 << n);
3204 /* check whether ADC-switch is possible */
3205 for (i = 0; i < imux->num_items; i++) {
3206 for (n = 0; n < spec->num_adc_nids; n++) {
3207 if (spec->input_paths[i][n]) {
3208 spec->dyn_adc_idx[i] = n;
3214 codec_dbg(codec, "enabling ADC switching\n");
3215 spec->dyn_adc_switch = 1;
3216 } else if (nums != spec->num_adc_nids) {
3217 /* shrink the invalid adcs and input paths */
3219 for (n = 0; n < spec->num_adc_nids; n++) {
3220 if (!(ok_bits & (1 << n)))
3223 spec->adc_nids[nums] = spec->adc_nids[n];
3224 for (i = 0; i < imux->num_items; i++) {
3225 invalidate_nid_path(codec,
3226 spec->input_paths[i][nums]);
3227 spec->input_paths[i][nums] =
3228 spec->input_paths[i][n];
3229 spec->input_paths[i][n] = 0;
3234 spec->num_adc_nids = nums;
3237 if (imux->num_items == 1 ||
3238 (imux->num_items == 2 && spec->hp_mic)) {
3239 codec_dbg(codec, "reducing to a single ADC\n");
3240 spec->num_adc_nids = 1; /* reduce to a single ADC */
3243 /* single index for individual volumes ctls */
3244 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
3245 spec->num_adc_nids = 1;
3250 /* parse capture source paths from the given pin and create imux items */
3251 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
3252 int cfg_idx, int num_adcs,
3253 const char *label, int anchor)
3255 struct hda_gen_spec *spec = codec->spec;
3256 struct hda_input_mux *imux = &spec->input_mux;
3257 int imux_idx = imux->num_items;
3258 bool imux_added = false;
3261 for (c = 0; c < num_adcs; c++) {
3262 struct nid_path *path;
3263 hda_nid_t adc = spec->adc_nids[c];
3265 if (!is_reachable_path(codec, pin, adc))
3267 path = snd_hda_add_new_path(codec, pin, adc, anchor);
3270 print_nid_path(codec, "input", path);
3271 spec->input_paths[imux_idx][c] =
3272 snd_hda_get_path_idx(codec, path);
3275 if (spec->hp_mic_pin == pin)
3276 spec->hp_mic_mux_idx = imux->num_items;
3277 spec->imux_pins[imux->num_items] = pin;
3278 snd_hda_add_imux_item(codec, imux, label, cfg_idx, NULL);
3280 if (spec->dyn_adc_switch)
3281 spec->dyn_adc_idx[imux_idx] = c;
3289 * create playback/capture controls for input pins
3292 /* fill the label for each input at first */
3293 static int fill_input_pin_labels(struct hda_codec *codec)
3295 struct hda_gen_spec *spec = codec->spec;
3296 const struct auto_pin_cfg *cfg = &spec->autocfg;
3299 for (i = 0; i < cfg->num_inputs; i++) {
3300 hda_nid_t pin = cfg->inputs[i].pin;
3304 if (!is_input_pin(codec, pin))
3307 label = hda_get_autocfg_input_label(codec, cfg, i);
3309 for (j = i - 1; j >= 0; j--) {
3310 if (spec->input_labels[j] &&
3311 !strcmp(spec->input_labels[j], label)) {
3312 idx = spec->input_label_idxs[j] + 1;
3317 spec->input_labels[i] = label;
3318 spec->input_label_idxs[i] = idx;
3324 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3326 static int create_input_ctls(struct hda_codec *codec)
3328 struct hda_gen_spec *spec = codec->spec;
3329 const struct auto_pin_cfg *cfg = &spec->autocfg;
3330 hda_nid_t mixer = spec->mixer_nid;
3335 num_adcs = fill_adc_nids(codec);
3339 err = fill_input_pin_labels(codec);
3343 for (i = 0; i < cfg->num_inputs; i++) {
3346 pin = cfg->inputs[i].pin;
3347 if (!is_input_pin(codec, pin))
3351 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3352 val |= snd_hda_get_default_vref(codec, pin);
3353 if (pin != spec->hp_mic_pin &&
3354 !snd_hda_codec_get_pin_target(codec, pin))
3355 set_pin_target(codec, pin, val, false);
3358 if (is_reachable_path(codec, pin, mixer)) {
3359 err = new_analog_input(codec, i, pin,
3360 spec->input_labels[i],
3361 spec->input_label_idxs[i],
3368 err = parse_capture_source(codec, pin, i, num_adcs,
3369 spec->input_labels[i], -mixer);
3373 if (spec->add_jack_modes) {
3374 err = create_in_jack_mode(codec, pin);
3380 /* add stereo mix when explicitly enabled via hint */
3381 if (mixer && spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_ENABLE) {
3382 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3387 spec->suppress_auto_mic = 1;
3398 /* get the input path specified by the given adc and imux indices */
3399 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3401 struct hda_gen_spec *spec = codec->spec;
3402 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3406 if (spec->dyn_adc_switch)
3407 adc_idx = spec->dyn_adc_idx[imux_idx];
3408 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3412 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3415 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3418 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3419 struct snd_ctl_elem_info *uinfo)
3421 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3422 struct hda_gen_spec *spec = codec->spec;
3423 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3426 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3427 struct snd_ctl_elem_value *ucontrol)
3429 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3430 struct hda_gen_spec *spec = codec->spec;
3431 /* the ctls are created at once with multiple counts */
3432 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3434 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3438 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3439 struct snd_ctl_elem_value *ucontrol)
3441 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3442 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3443 return mux_select(codec, adc_idx,
3444 ucontrol->value.enumerated.item[0]);
3447 static const struct snd_kcontrol_new cap_src_temp = {
3448 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3449 .name = "Input Source",
3450 .info = mux_enum_info,
3451 .get = mux_enum_get,
3452 .put = mux_enum_put,
3456 * capture volume and capture switch ctls
3459 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3460 struct snd_ctl_elem_value *ucontrol);
3462 /* call the given amp update function for all amps in the imux list at once */
3463 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3464 struct snd_ctl_elem_value *ucontrol,
3465 put_call_t func, int type)
3467 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3468 struct hda_gen_spec *spec = codec->spec;
3469 const struct hda_input_mux *imux;
3470 struct nid_path *path;
3471 int i, adc_idx, ret, err = 0;
3473 imux = &spec->input_mux;
3474 adc_idx = kcontrol->id.index;
3475 mutex_lock(&codec->control_mutex);
3476 for (i = 0; i < imux->num_items; i++) {
3477 path = get_input_path(codec, adc_idx, i);
3478 if (!path || !path->ctls[type])
3480 kcontrol->private_value = path->ctls[type];
3481 ret = func(kcontrol, ucontrol);
3489 mutex_unlock(&codec->control_mutex);
3490 if (err >= 0 && spec->cap_sync_hook)
3491 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3495 /* capture volume ctl callbacks */
3496 #define cap_vol_info snd_hda_mixer_amp_volume_info
3497 #define cap_vol_get snd_hda_mixer_amp_volume_get
3498 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3500 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3501 struct snd_ctl_elem_value *ucontrol)
3503 return cap_put_caller(kcontrol, ucontrol,
3504 snd_hda_mixer_amp_volume_put,
3508 static const struct snd_kcontrol_new cap_vol_temp = {
3509 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3510 .name = "Capture Volume",
3511 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3512 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3513 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3514 .info = cap_vol_info,
3517 .tlv = { .c = cap_vol_tlv },
3520 /* capture switch ctl callbacks */
3521 #define cap_sw_info snd_ctl_boolean_stereo_info
3522 #define cap_sw_get snd_hda_mixer_amp_switch_get
3524 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3525 struct snd_ctl_elem_value *ucontrol)
3527 return cap_put_caller(kcontrol, ucontrol,
3528 snd_hda_mixer_amp_switch_put,
3532 static const struct snd_kcontrol_new cap_sw_temp = {
3533 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3534 .name = "Capture Switch",
3535 .info = cap_sw_info,
3540 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3545 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3546 for (depth = 0; depth < 3; depth++) {
3547 if (depth >= path->depth)
3549 i = path->depth - depth - 1;
3550 nid = path->path[i];
3551 if (!path->ctls[NID_PATH_VOL_CTL]) {
3552 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3553 path->ctls[NID_PATH_VOL_CTL] =
3554 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3555 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3556 int idx = path->idx[i];
3557 if (!depth && codec->single_adc_amp)
3559 path->ctls[NID_PATH_VOL_CTL] =
3560 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3563 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3564 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3565 path->ctls[NID_PATH_MUTE_CTL] =
3566 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3567 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3568 int idx = path->idx[i];
3569 if (!depth && codec->single_adc_amp)
3571 path->ctls[NID_PATH_MUTE_CTL] =
3572 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3579 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3581 struct hda_gen_spec *spec = codec->spec;
3582 struct auto_pin_cfg *cfg = &spec->autocfg;
3586 if (!spec->inv_dmic_split)
3588 for (i = 0; i < cfg->num_inputs; i++) {
3589 if (cfg->inputs[i].pin != nid)
3591 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3593 val = snd_hda_codec_get_pincfg(codec, nid);
3594 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3599 /* capture switch put callback for a single control with hook call */
3600 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3601 struct snd_ctl_elem_value *ucontrol)
3603 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3604 struct hda_gen_spec *spec = codec->spec;
3607 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3611 if (spec->cap_sync_hook)
3612 spec->cap_sync_hook(codec, kcontrol, ucontrol);
3617 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3618 int idx, bool is_switch, unsigned int ctl,
3621 struct hda_gen_spec *spec = codec->spec;
3622 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3623 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3624 const char *sfx = is_switch ? "Switch" : "Volume";
3625 unsigned int chs = inv_dmic ? 1 : 3;
3626 struct snd_kcontrol_new *knew;
3632 snprintf(tmpname, sizeof(tmpname),
3633 "%s Capture %s", label, sfx);
3635 snprintf(tmpname, sizeof(tmpname),
3637 knew = add_control(spec, type, tmpname, idx,
3638 amp_val_replace_channels(ctl, chs));
3642 knew->put = cap_single_sw_put;
3646 /* Make independent right kcontrol */
3648 snprintf(tmpname, sizeof(tmpname),
3649 "Inverted %s Capture %s", label, sfx);
3651 snprintf(tmpname, sizeof(tmpname),
3652 "Inverted Capture %s", sfx);
3653 knew = add_control(spec, type, tmpname, idx,
3654 amp_val_replace_channels(ctl, 2));
3658 knew->put = cap_single_sw_put;
3662 /* create single (and simple) capture volume and switch controls */
3663 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3664 unsigned int vol_ctl, unsigned int sw_ctl,
3668 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3671 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3677 /* create bound capture volume and switch controls */
3678 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3679 unsigned int vol_ctl, unsigned int sw_ctl)
3681 struct hda_gen_spec *spec = codec->spec;
3682 struct snd_kcontrol_new *knew;
3685 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3689 knew->private_value = vol_ctl;
3690 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3693 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3697 knew->private_value = sw_ctl;
3698 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3703 /* return the vol ctl when used first in the imux list */
3704 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3706 struct nid_path *path;
3710 path = get_input_path(codec, 0, idx);
3713 ctl = path->ctls[type];
3716 for (i = 0; i < idx - 1; i++) {
3717 path = get_input_path(codec, 0, i);
3718 if (path && path->ctls[type] == ctl)
3724 /* create individual capture volume and switch controls per input */
3725 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3727 struct hda_gen_spec *spec = codec->spec;
3728 struct hda_input_mux *imux = &spec->input_mux;
3731 for (i = 0; i < imux->num_items; i++) {
3735 idx = imux->items[i].index;
3736 if (idx >= spec->autocfg.num_inputs)
3738 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3740 for (type = 0; type < 2; type++) {
3741 err = add_single_cap_ctl(codec,
3742 spec->input_labels[idx],
3743 spec->input_label_idxs[idx],
3745 get_first_cap_ctl(codec, i, type),
3754 static int create_capture_mixers(struct hda_codec *codec)
3756 struct hda_gen_spec *spec = codec->spec;
3757 struct hda_input_mux *imux = &spec->input_mux;
3758 int i, n, nums, err;
3760 if (spec->dyn_adc_switch)
3763 nums = spec->num_adc_nids;
3765 if (!spec->auto_mic && imux->num_items > 1) {
3766 struct snd_kcontrol_new *knew;
3768 name = nums > 1 ? "Input Source" : "Capture Source";
3769 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3775 for (n = 0; n < nums; n++) {
3777 bool multi_cap_vol = spec->multi_cap_vol;
3778 bool inv_dmic = false;
3782 for (i = 0; i < imux->num_items; i++) {
3783 struct nid_path *path;
3784 path = get_input_path(codec, n, i);
3787 parse_capvol_in_path(codec, path);
3789 vol = path->ctls[NID_PATH_VOL_CTL];
3790 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3792 if (!same_amp_caps(codec, vol,
3793 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3794 multi_cap_vol = true;
3797 sw = path->ctls[NID_PATH_MUTE_CTL];
3798 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3800 if (!same_amp_caps(codec, sw,
3801 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3802 multi_cap_vol = true;
3804 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3809 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3811 else if (!multi_cap_vol && !inv_dmic)
3812 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3814 err = create_multi_cap_vol_ctl(codec);
3823 * add mic boosts if needed
3826 /* check whether the given amp is feasible as a boost volume */
3827 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3832 if (!nid_has_volume(codec, nid, dir) ||
3833 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3834 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3837 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3838 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3844 /* look for a boost amp in a widget close to the pin */
3845 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3846 struct nid_path *path)
3848 unsigned int val = 0;
3852 for (depth = 0; depth < 3; depth++) {
3853 if (depth >= path->depth - 1)
3855 nid = path->path[depth];
3856 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3857 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3859 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3860 path->idx[depth])) {
3861 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3870 static int parse_mic_boost(struct hda_codec *codec)
3872 struct hda_gen_spec *spec = codec->spec;
3873 struct auto_pin_cfg *cfg = &spec->autocfg;
3874 struct hda_input_mux *imux = &spec->input_mux;
3877 if (!spec->num_adc_nids)
3880 for (i = 0; i < imux->num_items; i++) {
3881 struct nid_path *path;
3884 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3886 idx = imux->items[i].index;
3887 if (idx >= imux->num_items)
3890 /* check only line-in and mic pins */
3891 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3894 path = get_input_path(codec, 0, i);
3898 val = look_for_boost_amp(codec, path);
3902 /* create a boost control */
3903 snprintf(boost_label, sizeof(boost_label),
3904 "%s Boost Volume", spec->input_labels[idx]);
3905 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3906 spec->input_label_idxs[idx], val))
3909 path->ctls[NID_PATH_BOOST_CTL] = val;
3915 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3917 static void parse_digital(struct hda_codec *codec)
3919 struct hda_gen_spec *spec = codec->spec;
3920 struct nid_path *path;
3922 hda_nid_t dig_nid, pin;
3924 /* support multiple SPDIFs; the secondary is set up as a slave */
3926 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3927 pin = spec->autocfg.dig_out_pins[i];
3928 dig_nid = look_for_dac(codec, pin, true);
3931 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3934 print_nid_path(codec, "digout", path);
3935 path->active = true;
3936 path->pin_fixed = true; /* no jack detection */
3937 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3938 set_pin_target(codec, pin, PIN_OUT, false);
3940 spec->multiout.dig_out_nid = dig_nid;
3941 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3943 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3944 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3946 spec->slave_dig_outs[nums - 1] = dig_nid;
3951 if (spec->autocfg.dig_in_pin) {
3952 pin = spec->autocfg.dig_in_pin;
3953 for_each_hda_codec_node(dig_nid, codec) {
3954 unsigned int wcaps = get_wcaps(codec, dig_nid);
3955 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3957 if (!(wcaps & AC_WCAP_DIGITAL))
3959 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3961 print_nid_path(codec, "digin", path);
3962 path->active = true;
3963 path->pin_fixed = true; /* no jack */
3964 spec->dig_in_nid = dig_nid;
3965 spec->digin_path = snd_hda_get_path_idx(codec, path);
3966 set_pin_target(codec, pin, PIN_IN, false);
3975 * input MUX handling
3978 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3980 /* select the given imux item; either unmute exclusively or select the route */
3981 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3984 struct hda_gen_spec *spec = codec->spec;
3985 const struct hda_input_mux *imux;
3986 struct nid_path *old_path, *path;
3988 imux = &spec->input_mux;
3989 if (!imux->num_items)
3992 if (idx >= imux->num_items)
3993 idx = imux->num_items - 1;
3994 if (spec->cur_mux[adc_idx] == idx)
3997 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
4000 if (old_path->active)
4001 snd_hda_activate_path(codec, old_path, false, false);
4003 spec->cur_mux[adc_idx] = idx;
4006 update_hp_mic(codec, adc_idx, false);
4008 if (spec->dyn_adc_switch)
4009 dyn_adc_pcm_resetup(codec, idx);
4011 path = get_input_path(codec, adc_idx, idx);
4016 snd_hda_activate_path(codec, path, true, false);
4017 if (spec->cap_sync_hook)
4018 spec->cap_sync_hook(codec, NULL, NULL);
4019 path_power_down_sync(codec, old_path);
4023 /* power up/down widgets in the all paths that match with the given NID
4024 * as terminals (either start- or endpoint)
4026 * returns the last changed NID, or zero if unchanged.
4028 static hda_nid_t set_path_power(struct hda_codec *codec, hda_nid_t nid,
4029 int pin_state, int stream_state)
4031 struct hda_gen_spec *spec = codec->spec;
4032 hda_nid_t last, changed = 0;
4033 struct nid_path *path;
4036 for (n = 0; n < spec->paths.used; n++) {
4037 path = snd_array_elem(&spec->paths, n);
4040 if (path->path[0] == nid ||
4041 path->path[path->depth - 1] == nid) {
4042 bool pin_old = path->pin_enabled;
4043 bool stream_old = path->stream_enabled;
4046 path->pin_enabled = pin_state;
4047 if (stream_state >= 0)
4048 path->stream_enabled = stream_state;
4049 if ((!path->pin_fixed && path->pin_enabled != pin_old)
4050 || path->stream_enabled != stream_old) {
4051 last = path_power_update(codec, path, true);
4060 /* check the jack status for power control */
4061 static bool detect_pin_state(struct hda_codec *codec, hda_nid_t pin)
4063 if (!is_jack_detectable(codec, pin))
4065 return snd_hda_jack_detect_state(codec, pin) != HDA_JACK_NOT_PRESENT;
4068 /* power up/down the paths of the given pin according to the jack state;
4069 * power = 0/1 : only power up/down if it matches with the jack state,
4070 * < 0 : force power up/down to follow the jack sate
4072 * returns the last changed NID, or zero if unchanged.
4074 static hda_nid_t set_pin_power_jack(struct hda_codec *codec, hda_nid_t pin,
4079 if (!codec->power_save_node)
4082 on = detect_pin_state(codec, pin);
4084 if (power >= 0 && on != power)
4086 return set_path_power(codec, pin, on, -1);
4089 static void pin_power_callback(struct hda_codec *codec,
4090 struct hda_jack_callback *jack,
4093 if (jack && jack->nid)
4094 sync_power_state_change(codec,
4095 set_pin_power_jack(codec, jack->nid, on));
4098 /* callback only doing power up -- called at first */
4099 static void pin_power_up_callback(struct hda_codec *codec,
4100 struct hda_jack_callback *jack)
4102 pin_power_callback(codec, jack, true);
4105 /* callback only doing power down -- called at last */
4106 static void pin_power_down_callback(struct hda_codec *codec,
4107 struct hda_jack_callback *jack)
4109 pin_power_callback(codec, jack, false);
4112 /* set up the power up/down callbacks */
4113 static void add_pin_power_ctls(struct hda_codec *codec, int num_pins,
4114 const hda_nid_t *pins, bool on)
4117 hda_jack_callback_fn cb =
4118 on ? pin_power_up_callback : pin_power_down_callback;
4120 for (i = 0; i < num_pins && pins[i]; i++) {
4121 if (is_jack_detectable(codec, pins[i]))
4122 snd_hda_jack_detect_enable_callback(codec, pins[i], cb);
4124 set_path_power(codec, pins[i], true, -1);
4128 /* enabled power callback to each available I/O pin with jack detections;
4129 * the digital I/O pins are excluded because of the unreliable detectsion
4131 static void add_all_pin_power_ctls(struct hda_codec *codec, bool on)
4133 struct hda_gen_spec *spec = codec->spec;
4134 struct auto_pin_cfg *cfg = &spec->autocfg;
4137 if (!codec->power_save_node)
4139 add_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins, on);
4140 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4141 add_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins, on);
4142 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4143 add_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins, on);
4144 for (i = 0; i < cfg->num_inputs; i++)
4145 add_pin_power_ctls(codec, 1, &cfg->inputs[i].pin, on);
4148 /* sync path power up/down with the jack states of given pins */
4149 static void sync_pin_power_ctls(struct hda_codec *codec, int num_pins,
4150 const hda_nid_t *pins)
4154 for (i = 0; i < num_pins && pins[i]; i++)
4155 if (is_jack_detectable(codec, pins[i]))
4156 set_pin_power_jack(codec, pins[i], -1);
4159 /* sync path power up/down with pins; called at init and resume */
4160 static void sync_all_pin_power_ctls(struct hda_codec *codec)
4162 struct hda_gen_spec *spec = codec->spec;
4163 struct auto_pin_cfg *cfg = &spec->autocfg;
4166 if (!codec->power_save_node)
4168 sync_pin_power_ctls(codec, cfg->line_outs, cfg->line_out_pins);
4169 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4170 sync_pin_power_ctls(codec, cfg->hp_outs, cfg->hp_pins);
4171 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4172 sync_pin_power_ctls(codec, cfg->speaker_outs, cfg->speaker_pins);
4173 for (i = 0; i < cfg->num_inputs; i++)
4174 sync_pin_power_ctls(codec, 1, &cfg->inputs[i].pin);
4177 /* add fake paths if not present yet */
4178 static int add_fake_paths(struct hda_codec *codec, hda_nid_t nid,
4179 int num_pins, const hda_nid_t *pins)
4181 struct hda_gen_spec *spec = codec->spec;
4182 struct nid_path *path;
4185 for (i = 0; i < num_pins; i++) {
4188 if (get_nid_path(codec, nid, pins[i], 0))
4190 path = snd_array_new(&spec->paths);
4193 memset(path, 0, sizeof(*path));
4195 path->path[0] = nid;
4196 path->path[1] = pins[i];
4197 path->active = true;
4202 /* create fake paths to all outputs from beep */
4203 static int add_fake_beep_paths(struct hda_codec *codec)
4205 struct hda_gen_spec *spec = codec->spec;
4206 struct auto_pin_cfg *cfg = &spec->autocfg;
4207 hda_nid_t nid = spec->beep_nid;
4210 if (!codec->power_save_node || !nid)
4212 err = add_fake_paths(codec, nid, cfg->line_outs, cfg->line_out_pins);
4215 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4216 err = add_fake_paths(codec, nid, cfg->hp_outs, cfg->hp_pins);
4220 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4221 err = add_fake_paths(codec, nid, cfg->speaker_outs,
4229 /* power up/down beep widget and its output paths */
4230 static void beep_power_hook(struct hda_beep *beep, bool on)
4232 set_path_power(beep->codec, beep->nid, -1, on);
4236 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4237 * @codec: the HDA codec
4238 * @pin: NID of pin to fix
4240 int snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin)
4242 struct hda_gen_spec *spec = codec->spec;
4243 struct nid_path *path;
4245 path = snd_array_new(&spec->paths);
4248 memset(path, 0, sizeof(*path));
4250 path->path[0] = pin;
4251 path->active = true;
4252 path->pin_fixed = true;
4253 path->stream_enabled = true;
4256 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power);
4259 * Jack detections for HP auto-mute and mic-switch
4262 /* check each pin in the given array; returns true if any of them is plugged */
4263 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
4266 bool present = false;
4268 for (i = 0; i < num_pins; i++) {
4269 hda_nid_t nid = pins[i];
4272 /* don't detect pins retasked as inputs */
4273 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
4275 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
4281 /* standard HP/line-out auto-mute helper */
4282 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
4283 int *paths, bool mute)
4285 struct hda_gen_spec *spec = codec->spec;
4288 for (i = 0; i < num_pins; i++) {
4289 hda_nid_t nid = pins[i];
4290 unsigned int val, oldval;
4294 oldval = snd_hda_codec_get_pin_target(codec, nid);
4295 if (oldval & PIN_IN)
4296 continue; /* no mute for inputs */
4298 if (spec->auto_mute_via_amp) {
4299 struct nid_path *path;
4302 path = snd_hda_get_path_from_idx(codec, paths[i]);
4305 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
4309 spec->mute_bits |= (1ULL << mute_nid);
4311 spec->mute_bits &= ~(1ULL << mute_nid);
4314 /* don't reset VREF value in case it's controlling
4315 * the amp (see alc861_fixup_asus_amp_vref_0f())
4317 if (spec->keep_vref_in_automute)
4318 val = oldval & ~PIN_HP;
4323 /* here we call update_pin_ctl() so that the pinctl is
4324 * changed without changing the pinctl target value;
4325 * the original target value will be still referred at
4326 * the init / resume again
4328 update_pin_ctl(codec, nid, val);
4331 set_pin_eapd(codec, nid, !mute);
4332 if (codec->power_save_node) {
4335 on = detect_pin_state(codec, nid);
4336 set_path_power(codec, nid, on, -1);
4342 * snd_hda_gen_update_outputs - Toggle outputs muting
4343 * @codec: the HDA codec
4345 * Update the mute status of all outputs based on the current jack states.
4347 void snd_hda_gen_update_outputs(struct hda_codec *codec)
4349 struct hda_gen_spec *spec = codec->spec;
4353 /* Control HP pins/amps depending on master_mute state;
4354 * in general, HP pins/amps control should be enabled in all cases,
4355 * but currently set only for master_mute, just to be safe
4357 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
4358 paths = spec->out_paths;
4360 paths = spec->hp_paths;
4361 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
4362 spec->autocfg.hp_pins, paths, spec->master_mute);
4364 if (!spec->automute_speaker)
4367 on = spec->hp_jack_present | spec->line_jack_present;
4368 on |= spec->master_mute;
4369 spec->speaker_muted = on;
4370 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4371 paths = spec->out_paths;
4373 paths = spec->speaker_paths;
4374 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
4375 spec->autocfg.speaker_pins, paths, on);
4377 /* toggle line-out mutes if needed, too */
4378 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4379 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
4380 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
4382 if (!spec->automute_lo)
4385 on = spec->hp_jack_present;
4386 on |= spec->master_mute;
4387 spec->line_out_muted = on;
4388 paths = spec->out_paths;
4389 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4390 spec->autocfg.line_out_pins, paths, on);
4392 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs);
4394 static void call_update_outputs(struct hda_codec *codec)
4396 struct hda_gen_spec *spec = codec->spec;
4397 if (spec->automute_hook)
4398 spec->automute_hook(codec);
4400 snd_hda_gen_update_outputs(codec);
4402 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4403 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
4404 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
4408 * snd_hda_gen_hp_automute - standard HP-automute helper
4409 * @codec: the HDA codec
4410 * @jack: jack object, NULL for the whole
4412 void snd_hda_gen_hp_automute(struct hda_codec *codec,
4413 struct hda_jack_callback *jack)
4415 struct hda_gen_spec *spec = codec->spec;
4416 hda_nid_t *pins = spec->autocfg.hp_pins;
4417 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
4419 /* No detection for the first HP jack during indep-HP mode */
4420 if (spec->indep_hp_enabled) {
4425 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
4426 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
4428 call_update_outputs(codec);
4430 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute);
4433 * snd_hda_gen_line_automute - standard line-out-automute helper
4434 * @codec: the HDA codec
4435 * @jack: jack object, NULL for the whole
4437 void snd_hda_gen_line_automute(struct hda_codec *codec,
4438 struct hda_jack_callback *jack)
4440 struct hda_gen_spec *spec = codec->spec;
4442 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
4444 /* check LO jack only when it's different from HP */
4445 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
4448 spec->line_jack_present =
4449 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
4450 spec->autocfg.line_out_pins);
4451 if (!spec->automute_speaker || !spec->detect_lo)
4453 call_update_outputs(codec);
4455 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute);
4458 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4459 * @codec: the HDA codec
4460 * @jack: jack object, NULL for the whole
4462 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
4463 struct hda_jack_callback *jack)
4465 struct hda_gen_spec *spec = codec->spec;
4468 if (!spec->auto_mic)
4471 for (i = spec->am_num_entries - 1; i > 0; i--) {
4472 hda_nid_t pin = spec->am_entry[i].pin;
4473 /* don't detect pins retasked as outputs */
4474 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
4476 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
4477 mux_select(codec, 0, spec->am_entry[i].idx);
4481 mux_select(codec, 0, spec->am_entry[0].idx);
4483 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch);
4485 /* call appropriate hooks */
4486 static void call_hp_automute(struct hda_codec *codec,
4487 struct hda_jack_callback *jack)
4489 struct hda_gen_spec *spec = codec->spec;
4490 if (spec->hp_automute_hook)
4491 spec->hp_automute_hook(codec, jack);
4493 snd_hda_gen_hp_automute(codec, jack);
4496 static void call_line_automute(struct hda_codec *codec,
4497 struct hda_jack_callback *jack)
4499 struct hda_gen_spec *spec = codec->spec;
4500 if (spec->line_automute_hook)
4501 spec->line_automute_hook(codec, jack);
4503 snd_hda_gen_line_automute(codec, jack);
4506 static void call_mic_autoswitch(struct hda_codec *codec,
4507 struct hda_jack_callback *jack)
4509 struct hda_gen_spec *spec = codec->spec;
4510 if (spec->mic_autoswitch_hook)
4511 spec->mic_autoswitch_hook(codec, jack);
4513 snd_hda_gen_mic_autoswitch(codec, jack);
4516 /* update jack retasking */
4517 static void update_automute_all(struct hda_codec *codec)
4519 call_hp_automute(codec, NULL);
4520 call_line_automute(codec, NULL);
4521 call_mic_autoswitch(codec, NULL);
4525 * Auto-Mute mode mixer enum support
4527 static int automute_mode_info(struct snd_kcontrol *kcontrol,
4528 struct snd_ctl_elem_info *uinfo)
4530 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4531 struct hda_gen_spec *spec = codec->spec;
4532 static const char * const texts3[] = {
4533 "Disabled", "Speaker Only", "Line Out+Speaker"
4536 if (spec->automute_speaker_possible && spec->automute_lo_possible)
4537 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
4538 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4541 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4542 struct snd_ctl_elem_value *ucontrol)
4544 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4545 struct hda_gen_spec *spec = codec->spec;
4546 unsigned int val = 0;
4547 if (spec->automute_speaker)
4549 if (spec->automute_lo)
4552 ucontrol->value.enumerated.item[0] = val;
4556 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4557 struct snd_ctl_elem_value *ucontrol)
4559 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4560 struct hda_gen_spec *spec = codec->spec;
4562 switch (ucontrol->value.enumerated.item[0]) {
4564 if (!spec->automute_speaker && !spec->automute_lo)
4566 spec->automute_speaker = 0;
4567 spec->automute_lo = 0;
4570 if (spec->automute_speaker_possible) {
4571 if (!spec->automute_lo && spec->automute_speaker)
4573 spec->automute_speaker = 1;
4574 spec->automute_lo = 0;
4575 } else if (spec->automute_lo_possible) {
4576 if (spec->automute_lo)
4578 spec->automute_lo = 1;
4583 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4585 if (spec->automute_speaker && spec->automute_lo)
4587 spec->automute_speaker = 1;
4588 spec->automute_lo = 1;
4593 call_update_outputs(codec);
4597 static const struct snd_kcontrol_new automute_mode_enum = {
4598 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4599 .name = "Auto-Mute Mode",
4600 .info = automute_mode_info,
4601 .get = automute_mode_get,
4602 .put = automute_mode_put,
4605 static int add_automute_mode_enum(struct hda_codec *codec)
4607 struct hda_gen_spec *spec = codec->spec;
4609 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4615 * Check the availability of HP/line-out auto-mute;
4616 * Set up appropriately if really supported
4618 static int check_auto_mute_availability(struct hda_codec *codec)
4620 struct hda_gen_spec *spec = codec->spec;
4621 struct auto_pin_cfg *cfg = &spec->autocfg;
4625 if (spec->suppress_auto_mute)
4628 if (cfg->hp_pins[0])
4630 if (cfg->line_out_pins[0])
4632 if (cfg->speaker_pins[0])
4634 if (present < 2) /* need two different output types */
4637 if (!cfg->speaker_pins[0] &&
4638 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4639 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4640 sizeof(cfg->speaker_pins));
4641 cfg->speaker_outs = cfg->line_outs;
4644 if (!cfg->hp_pins[0] &&
4645 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4646 memcpy(cfg->hp_pins, cfg->line_out_pins,
4647 sizeof(cfg->hp_pins));
4648 cfg->hp_outs = cfg->line_outs;
4651 for (i = 0; i < cfg->hp_outs; i++) {
4652 hda_nid_t nid = cfg->hp_pins[i];
4653 if (!is_jack_detectable(codec, nid))
4655 codec_dbg(codec, "Enable HP auto-muting on NID 0x%x\n", nid);
4656 snd_hda_jack_detect_enable_callback(codec, nid,
4658 spec->detect_hp = 1;
4661 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4662 if (cfg->speaker_outs)
4663 for (i = 0; i < cfg->line_outs; i++) {
4664 hda_nid_t nid = cfg->line_out_pins[i];
4665 if (!is_jack_detectable(codec, nid))
4667 codec_dbg(codec, "Enable Line-Out auto-muting on NID 0x%x\n", nid);
4668 snd_hda_jack_detect_enable_callback(codec, nid,
4669 call_line_automute);
4670 spec->detect_lo = 1;
4672 spec->automute_lo_possible = spec->detect_hp;
4675 spec->automute_speaker_possible = cfg->speaker_outs &&
4676 (spec->detect_hp || spec->detect_lo);
4678 spec->automute_lo = spec->automute_lo_possible;
4679 spec->automute_speaker = spec->automute_speaker_possible;
4681 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4682 /* create a control for automute mode */
4683 err = add_automute_mode_enum(codec);
4690 /* check whether all auto-mic pins are valid; setup indices if OK */
4691 static bool auto_mic_check_imux(struct hda_codec *codec)
4693 struct hda_gen_spec *spec = codec->spec;
4694 const struct hda_input_mux *imux;
4697 imux = &spec->input_mux;
4698 for (i = 0; i < spec->am_num_entries; i++) {
4699 spec->am_entry[i].idx =
4700 find_idx_in_nid_list(spec->am_entry[i].pin,
4701 spec->imux_pins, imux->num_items);
4702 if (spec->am_entry[i].idx < 0)
4703 return false; /* no corresponding imux */
4706 /* we don't need the jack detection for the first pin */
4707 for (i = 1; i < spec->am_num_entries; i++)
4708 snd_hda_jack_detect_enable_callback(codec,
4709 spec->am_entry[i].pin,
4710 call_mic_autoswitch);
4714 static int compare_attr(const void *ap, const void *bp)
4716 const struct automic_entry *a = ap;
4717 const struct automic_entry *b = bp;
4718 return (int)(a->attr - b->attr);
4722 * Check the availability of auto-mic switch;
4723 * Set up if really supported
4725 static int check_auto_mic_availability(struct hda_codec *codec)
4727 struct hda_gen_spec *spec = codec->spec;
4728 struct auto_pin_cfg *cfg = &spec->autocfg;
4732 if (spec->suppress_auto_mic)
4737 for (i = 0; i < cfg->num_inputs; i++) {
4738 hda_nid_t nid = cfg->inputs[i].pin;
4740 attr = snd_hda_codec_get_pincfg(codec, nid);
4741 attr = snd_hda_get_input_pin_attr(attr);
4742 if (types & (1 << attr))
4743 return 0; /* already occupied */
4745 case INPUT_PIN_ATTR_INT:
4746 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4747 return 0; /* invalid type */
4749 case INPUT_PIN_ATTR_UNUSED:
4750 return 0; /* invalid entry */
4752 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4753 return 0; /* invalid type */
4754 if (!spec->line_in_auto_switch &&
4755 cfg->inputs[i].type != AUTO_PIN_MIC)
4756 return 0; /* only mic is allowed */
4757 if (!is_jack_detectable(codec, nid))
4758 return 0; /* no unsol support */
4761 if (num_pins >= MAX_AUTO_MIC_PINS)
4763 types |= (1 << attr);
4764 spec->am_entry[num_pins].pin = nid;
4765 spec->am_entry[num_pins].attr = attr;
4772 spec->am_num_entries = num_pins;
4773 /* sort the am_entry in the order of attr so that the pin with a
4774 * higher attr will be selected when the jack is plugged.
4776 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4777 compare_attr, NULL);
4779 if (!auto_mic_check_imux(codec))
4783 spec->num_adc_nids = 1;
4784 spec->cur_mux[0] = spec->am_entry[0].idx;
4785 codec_dbg(codec, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4786 spec->am_entry[0].pin,
4787 spec->am_entry[1].pin,
4788 spec->am_entry[2].pin);
4794 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4796 * @codec: the HDA codec
4797 * @nid: NID to evalute
4798 * @power_state: target power state
4800 unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4802 unsigned int power_state)
4804 struct hda_gen_spec *spec = codec->spec;
4806 if (!spec->power_down_unused && !codec->power_save_node)
4808 if (power_state != AC_PWRST_D0 || nid == codec->core.afg)
4810 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4812 if (is_active_nid_for_any(codec, nid))
4816 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter);
4818 /* mute all aamix inputs initially; parse up to the first leaves */
4819 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4822 const hda_nid_t *conn;
4825 nums = snd_hda_get_conn_list(codec, mix, &conn);
4826 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4827 for (i = 0; i < nums; i++) {
4829 update_amp(codec, mix, HDA_INPUT, i,
4830 0xff, HDA_AMP_MUTE);
4831 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4832 update_amp(codec, conn[i], HDA_OUTPUT, 0,
4833 0xff, HDA_AMP_MUTE);
4838 * snd_hda_gen_stream_pm - Stream power management callback
4839 * @codec: the HDA codec
4840 * @nid: audio widget
4841 * @on: power on/off flag
4843 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
4845 void snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on)
4847 if (codec->power_save_node)
4848 set_path_power(codec, nid, -1, on);
4850 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm);
4853 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4854 * set up the hda_gen_spec
4855 * @codec: the HDA codec
4856 * @cfg: Parsed pin configuration
4858 * return 1 if successful, 0 if the proper config is not found,
4859 * or a negative error code
4861 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4862 struct auto_pin_cfg *cfg)
4864 struct hda_gen_spec *spec = codec->spec;
4867 parse_user_hints(codec);
4869 if (spec->mixer_nid && !spec->mixer_merge_nid)
4870 spec->mixer_merge_nid = spec->mixer_nid;
4872 if (cfg != &spec->autocfg) {
4873 spec->autocfg = *cfg;
4874 cfg = &spec->autocfg;
4877 if (!spec->main_out_badness)
4878 spec->main_out_badness = &hda_main_out_badness;
4879 if (!spec->extra_out_badness)
4880 spec->extra_out_badness = &hda_extra_out_badness;
4882 fill_all_dac_nids(codec);
4884 if (!cfg->line_outs) {
4885 if (cfg->dig_outs || cfg->dig_in_pin) {
4886 spec->multiout.max_channels = 2;
4887 spec->no_analog = 1;
4890 if (!cfg->num_inputs && !cfg->dig_in_pin)
4891 return 0; /* can't find valid BIOS pin config */
4894 if (!spec->no_primary_hp &&
4895 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4896 cfg->line_outs <= cfg->hp_outs) {
4897 /* use HP as primary out */
4898 cfg->speaker_outs = cfg->line_outs;
4899 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4900 sizeof(cfg->speaker_pins));
4901 cfg->line_outs = cfg->hp_outs;
4902 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4904 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4905 cfg->line_out_type = AUTO_PIN_HP_OUT;
4908 err = parse_output_paths(codec);
4911 err = create_multi_channel_mode(codec);
4914 err = create_multi_out_ctls(codec, cfg);
4917 err = create_hp_out_ctls(codec);
4920 err = create_speaker_out_ctls(codec);
4923 err = create_indep_hp_ctls(codec);
4926 err = create_loopback_mixing_ctl(codec);
4929 err = create_hp_mic(codec);
4932 err = create_input_ctls(codec);
4936 /* add power-down pin callbacks at first */
4937 add_all_pin_power_ctls(codec, false);
4939 spec->const_channel_count = spec->ext_channel_count;
4940 /* check the multiple speaker and headphone pins */
4941 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4942 spec->const_channel_count = max(spec->const_channel_count,
4943 cfg->speaker_outs * 2);
4944 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4945 spec->const_channel_count = max(spec->const_channel_count,
4947 spec->multiout.max_channels = max(spec->ext_channel_count,
4948 spec->const_channel_count);
4950 err = check_auto_mute_availability(codec);
4954 err = check_dyn_adc_switch(codec);
4958 err = check_auto_mic_availability(codec);
4962 /* add stereo mix if available and not enabled yet */
4963 if (!spec->auto_mic && spec->mixer_nid &&
4964 spec->add_stereo_mix_input == HDA_HINT_STEREO_MIX_AUTO &&
4965 spec->input_mux.num_items > 1) {
4966 err = parse_capture_source(codec, spec->mixer_nid,
4967 CFG_IDX_MIX, spec->num_all_adcs,
4974 err = create_capture_mixers(codec);
4978 err = parse_mic_boost(codec);
4982 /* create "Headphone Mic Jack Mode" if no input selection is
4983 * available (or user specifies add_jack_modes hint)
4985 if (spec->hp_mic_pin &&
4986 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4987 spec->add_jack_modes)) {
4988 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4993 if (spec->add_jack_modes) {
4994 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4995 err = create_out_jack_modes(codec, cfg->line_outs,
4996 cfg->line_out_pins);
5000 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
5001 err = create_out_jack_modes(codec, cfg->hp_outs,
5008 /* add power-up pin callbacks at last */
5009 add_all_pin_power_ctls(codec, true);
5011 /* mute all aamix input initially */
5012 if (spec->mixer_nid)
5013 mute_all_mixer_nid(codec, spec->mixer_nid);
5016 parse_digital(codec);
5018 if (spec->power_down_unused || codec->power_save_node) {
5019 if (!codec->power_filter)
5020 codec->power_filter = snd_hda_gen_path_power_filter;
5021 if (!codec->patch_ops.stream_pm)
5022 codec->patch_ops.stream_pm = snd_hda_gen_stream_pm;
5025 if (!spec->no_analog && spec->beep_nid) {
5026 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
5029 if (codec->beep && codec->power_save_node) {
5030 err = add_fake_beep_paths(codec);
5033 codec->beep->power_hook = beep_power_hook;
5039 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config);
5043 * Build control elements
5046 /* slave controls for virtual master */
5047 static const char * const slave_pfxs[] = {
5048 "Front", "Surround", "Center", "LFE", "Side",
5049 "Headphone", "Speaker", "Mono", "Line Out",
5050 "CLFE", "Bass Speaker", "PCM",
5051 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5052 "Headphone Front", "Headphone Surround", "Headphone CLFE",
5053 "Headphone Side", "Headphone+LO", "Speaker+LO",
5058 * snd_hda_gen_build_controls - Build controls from the parsed results
5059 * @codec: the HDA codec
5061 * Pass this to build_controls patch_ops.
5063 int snd_hda_gen_build_controls(struct hda_codec *codec)
5065 struct hda_gen_spec *spec = codec->spec;
5068 if (spec->kctls.used) {
5069 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
5074 if (spec->multiout.dig_out_nid) {
5075 err = snd_hda_create_dig_out_ctls(codec,
5076 spec->multiout.dig_out_nid,
5077 spec->multiout.dig_out_nid,
5078 spec->pcm_rec[1]->pcm_type);
5081 if (!spec->no_analog) {
5082 err = snd_hda_create_spdif_share_sw(codec,
5086 spec->multiout.share_spdif = 1;
5089 if (spec->dig_in_nid) {
5090 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
5095 /* if we have no master control, let's create it */
5096 if (!spec->no_analog && !spec->suppress_vmaster &&
5097 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
5098 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
5099 spec->vmaster_tlv, slave_pfxs,
5104 if (!spec->no_analog && !spec->suppress_vmaster &&
5105 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
5106 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
5109 true, &spec->vmaster_mute.sw_kctl);
5112 if (spec->vmaster_mute.hook) {
5113 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
5114 spec->vmaster_mute_enum);
5115 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5119 free_kctls(spec); /* no longer needed */
5121 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
5127 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls);
5134 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
5135 struct hda_codec *codec,
5136 struct snd_pcm_substream *substream,
5139 struct hda_gen_spec *spec = codec->spec;
5140 if (spec->pcm_playback_hook)
5141 spec->pcm_playback_hook(hinfo, codec, substream, action);
5144 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
5145 struct hda_codec *codec,
5146 struct snd_pcm_substream *substream,
5149 struct hda_gen_spec *spec = codec->spec;
5150 if (spec->pcm_capture_hook)
5151 spec->pcm_capture_hook(hinfo, codec, substream, action);
5155 * Analog playback callbacks
5157 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
5158 struct hda_codec *codec,
5159 struct snd_pcm_substream *substream)
5161 struct hda_gen_spec *spec = codec->spec;
5164 mutex_lock(&spec->pcm_mutex);
5165 err = snd_hda_multi_out_analog_open(codec,
5166 &spec->multiout, substream,
5169 spec->active_streams |= 1 << STREAM_MULTI_OUT;
5170 call_pcm_playback_hook(hinfo, codec, substream,
5171 HDA_GEN_PCM_ACT_OPEN);
5173 mutex_unlock(&spec->pcm_mutex);
5177 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5178 struct hda_codec *codec,
5179 unsigned int stream_tag,
5180 unsigned int format,
5181 struct snd_pcm_substream *substream)
5183 struct hda_gen_spec *spec = codec->spec;
5186 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
5187 stream_tag, format, substream);
5189 call_pcm_playback_hook(hinfo, codec, substream,
5190 HDA_GEN_PCM_ACT_PREPARE);
5194 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5195 struct hda_codec *codec,
5196 struct snd_pcm_substream *substream)
5198 struct hda_gen_spec *spec = codec->spec;
5201 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
5203 call_pcm_playback_hook(hinfo, codec, substream,
5204 HDA_GEN_PCM_ACT_CLEANUP);
5208 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
5209 struct hda_codec *codec,
5210 struct snd_pcm_substream *substream)
5212 struct hda_gen_spec *spec = codec->spec;
5213 mutex_lock(&spec->pcm_mutex);
5214 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
5215 call_pcm_playback_hook(hinfo, codec, substream,
5216 HDA_GEN_PCM_ACT_CLOSE);
5217 mutex_unlock(&spec->pcm_mutex);
5221 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
5222 struct hda_codec *codec,
5223 struct snd_pcm_substream *substream)
5225 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
5229 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5230 struct hda_codec *codec,
5231 unsigned int stream_tag,
5232 unsigned int format,
5233 struct snd_pcm_substream *substream)
5235 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5236 call_pcm_capture_hook(hinfo, codec, substream,
5237 HDA_GEN_PCM_ACT_PREPARE);
5241 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5242 struct hda_codec *codec,
5243 struct snd_pcm_substream *substream)
5245 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5246 call_pcm_capture_hook(hinfo, codec, substream,
5247 HDA_GEN_PCM_ACT_CLEANUP);
5251 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
5252 struct hda_codec *codec,
5253 struct snd_pcm_substream *substream)
5255 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
5259 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
5260 struct hda_codec *codec,
5261 struct snd_pcm_substream *substream)
5263 struct hda_gen_spec *spec = codec->spec;
5266 mutex_lock(&spec->pcm_mutex);
5267 if (spec->indep_hp && !spec->indep_hp_enabled)
5270 spec->active_streams |= 1 << STREAM_INDEP_HP;
5271 call_pcm_playback_hook(hinfo, codec, substream,
5272 HDA_GEN_PCM_ACT_OPEN);
5273 mutex_unlock(&spec->pcm_mutex);
5277 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
5278 struct hda_codec *codec,
5279 struct snd_pcm_substream *substream)
5281 struct hda_gen_spec *spec = codec->spec;
5282 mutex_lock(&spec->pcm_mutex);
5283 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
5284 call_pcm_playback_hook(hinfo, codec, substream,
5285 HDA_GEN_PCM_ACT_CLOSE);
5286 mutex_unlock(&spec->pcm_mutex);
5290 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5291 struct hda_codec *codec,
5292 unsigned int stream_tag,
5293 unsigned int format,
5294 struct snd_pcm_substream *substream)
5296 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
5297 call_pcm_playback_hook(hinfo, codec, substream,
5298 HDA_GEN_PCM_ACT_PREPARE);
5302 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5303 struct hda_codec *codec,
5304 struct snd_pcm_substream *substream)
5306 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
5307 call_pcm_playback_hook(hinfo, codec, substream,
5308 HDA_GEN_PCM_ACT_CLEANUP);
5315 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
5316 struct hda_codec *codec,
5317 struct snd_pcm_substream *substream)
5319 struct hda_gen_spec *spec = codec->spec;
5320 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
5323 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
5324 struct hda_codec *codec,
5325 unsigned int stream_tag,
5326 unsigned int format,
5327 struct snd_pcm_substream *substream)
5329 struct hda_gen_spec *spec = codec->spec;
5330 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
5331 stream_tag, format, substream);
5334 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
5335 struct hda_codec *codec,
5336 struct snd_pcm_substream *substream)
5338 struct hda_gen_spec *spec = codec->spec;
5339 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
5342 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
5343 struct hda_codec *codec,
5344 struct snd_pcm_substream *substream)
5346 struct hda_gen_spec *spec = codec->spec;
5347 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
5353 #define alt_capture_pcm_open capture_pcm_open
5354 #define alt_capture_pcm_close capture_pcm_close
5356 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5357 struct hda_codec *codec,
5358 unsigned int stream_tag,
5359 unsigned int format,
5360 struct snd_pcm_substream *substream)
5362 struct hda_gen_spec *spec = codec->spec;
5364 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
5365 stream_tag, 0, format);
5366 call_pcm_capture_hook(hinfo, codec, substream,
5367 HDA_GEN_PCM_ACT_PREPARE);
5371 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5372 struct hda_codec *codec,
5373 struct snd_pcm_substream *substream)
5375 struct hda_gen_spec *spec = codec->spec;
5377 snd_hda_codec_cleanup_stream(codec,
5378 spec->adc_nids[substream->number + 1]);
5379 call_pcm_capture_hook(hinfo, codec, substream,
5380 HDA_GEN_PCM_ACT_CLEANUP);
5386 static const struct hda_pcm_stream pcm_analog_playback = {
5390 /* NID is set in build_pcms */
5392 .open = playback_pcm_open,
5393 .close = playback_pcm_close,
5394 .prepare = playback_pcm_prepare,
5395 .cleanup = playback_pcm_cleanup
5399 static const struct hda_pcm_stream pcm_analog_capture = {
5403 /* NID is set in build_pcms */
5405 .open = capture_pcm_open,
5406 .close = capture_pcm_close,
5407 .prepare = capture_pcm_prepare,
5408 .cleanup = capture_pcm_cleanup
5412 static const struct hda_pcm_stream pcm_analog_alt_playback = {
5416 /* NID is set in build_pcms */
5418 .open = alt_playback_pcm_open,
5419 .close = alt_playback_pcm_close,
5420 .prepare = alt_playback_pcm_prepare,
5421 .cleanup = alt_playback_pcm_cleanup
5425 static const struct hda_pcm_stream pcm_analog_alt_capture = {
5426 .substreams = 2, /* can be overridden */
5429 /* NID is set in build_pcms */
5431 .open = alt_capture_pcm_open,
5432 .close = alt_capture_pcm_close,
5433 .prepare = alt_capture_pcm_prepare,
5434 .cleanup = alt_capture_pcm_cleanup
5438 static const struct hda_pcm_stream pcm_digital_playback = {
5442 /* NID is set in build_pcms */
5444 .open = dig_playback_pcm_open,
5445 .close = dig_playback_pcm_close,
5446 .prepare = dig_playback_pcm_prepare,
5447 .cleanup = dig_playback_pcm_cleanup
5451 static const struct hda_pcm_stream pcm_digital_capture = {
5455 /* NID is set in build_pcms */
5458 /* Used by build_pcms to flag that a PCM has no playback stream */
5459 static const struct hda_pcm_stream pcm_null_stream = {
5466 * dynamic changing ADC PCM streams
5468 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
5470 struct hda_gen_spec *spec = codec->spec;
5471 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
5473 if (spec->cur_adc && spec->cur_adc != new_adc) {
5474 /* stream is running, let's swap the current ADC */
5475 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
5476 spec->cur_adc = new_adc;
5477 snd_hda_codec_setup_stream(codec, new_adc,
5478 spec->cur_adc_stream_tag, 0,
5479 spec->cur_adc_format);
5485 /* analog capture with dynamic dual-adc changes */
5486 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
5487 struct hda_codec *codec,
5488 unsigned int stream_tag,
5489 unsigned int format,
5490 struct snd_pcm_substream *substream)
5492 struct hda_gen_spec *spec = codec->spec;
5493 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
5494 spec->cur_adc_stream_tag = stream_tag;
5495 spec->cur_adc_format = format;
5496 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
5497 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_PREPARE);
5501 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
5502 struct hda_codec *codec,
5503 struct snd_pcm_substream *substream)
5505 struct hda_gen_spec *spec = codec->spec;
5506 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
5508 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLEANUP);
5512 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
5516 .nid = 0, /* fill later */
5518 .prepare = dyn_adc_capture_pcm_prepare,
5519 .cleanup = dyn_adc_capture_pcm_cleanup
5523 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
5524 const char *chip_name)
5530 strlcpy(str, chip_name, len);
5532 /* drop non-alnum chars after a space */
5533 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
5534 if (!isalnum(p[1])) {
5539 strlcat(str, sfx, len);
5542 /* copy PCM stream info from @default_str, and override non-NULL entries
5543 * from @spec_str and @nid
5545 static void setup_pcm_stream(struct hda_pcm_stream *str,
5546 const struct hda_pcm_stream *default_str,
5547 const struct hda_pcm_stream *spec_str,
5550 *str = *default_str;
5554 if (spec_str->substreams)
5555 str->substreams = spec_str->substreams;
5556 if (spec_str->channels_min)
5557 str->channels_min = spec_str->channels_min;
5558 if (spec_str->channels_max)
5559 str->channels_max = spec_str->channels_max;
5560 if (spec_str->rates)
5561 str->rates = spec_str->rates;
5562 if (spec_str->formats)
5563 str->formats = spec_str->formats;
5564 if (spec_str->maxbps)
5565 str->maxbps = spec_str->maxbps;
5570 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5571 * @codec: the HDA codec
5573 * Pass this to build_pcms patch_ops.
5575 int snd_hda_gen_build_pcms(struct hda_codec *codec)
5577 struct hda_gen_spec *spec = codec->spec;
5578 struct hda_pcm *info;
5579 bool have_multi_adcs;
5581 if (spec->no_analog)
5584 fill_pcm_stream_name(spec->stream_name_analog,
5585 sizeof(spec->stream_name_analog),
5586 " Analog", codec->core.chip_name);
5587 info = snd_hda_codec_pcm_new(codec, "%s", spec->stream_name_analog);
5590 spec->pcm_rec[0] = info;
5592 if (spec->multiout.num_dacs > 0) {
5593 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5594 &pcm_analog_playback,
5595 spec->stream_analog_playback,
5596 spec->multiout.dac_nids[0]);
5597 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
5598 spec->multiout.max_channels;
5599 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
5600 spec->autocfg.line_outs == 2)
5601 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
5604 if (spec->num_adc_nids) {
5605 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5606 (spec->dyn_adc_switch ?
5607 &dyn_adc_pcm_analog_capture : &pcm_analog_capture),
5608 spec->stream_analog_capture,
5613 /* SPDIF for stream index #1 */
5614 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
5615 fill_pcm_stream_name(spec->stream_name_digital,
5616 sizeof(spec->stream_name_digital),
5617 " Digital", codec->core.chip_name);
5618 info = snd_hda_codec_pcm_new(codec, "%s",
5619 spec->stream_name_digital);
5622 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
5623 spec->pcm_rec[1] = info;
5624 if (spec->dig_out_type)
5625 info->pcm_type = spec->dig_out_type;
5627 info->pcm_type = HDA_PCM_TYPE_SPDIF;
5628 if (spec->multiout.dig_out_nid)
5629 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5630 &pcm_digital_playback,
5631 spec->stream_digital_playback,
5632 spec->multiout.dig_out_nid);
5633 if (spec->dig_in_nid)
5634 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5635 &pcm_digital_capture,
5636 spec->stream_digital_capture,
5640 if (spec->no_analog)
5643 /* If the use of more than one ADC is requested for the current
5644 * model, configure a second analog capture-only PCM.
5646 have_multi_adcs = (spec->num_adc_nids > 1) &&
5647 !spec->dyn_adc_switch && !spec->auto_mic;
5648 /* Additional Analaog capture for index #2 */
5649 if (spec->alt_dac_nid || have_multi_adcs) {
5650 fill_pcm_stream_name(spec->stream_name_alt_analog,
5651 sizeof(spec->stream_name_alt_analog),
5652 " Alt Analog", codec->core.chip_name);
5653 info = snd_hda_codec_pcm_new(codec, "%s",
5654 spec->stream_name_alt_analog);
5657 spec->pcm_rec[2] = info;
5658 if (spec->alt_dac_nid)
5659 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5660 &pcm_analog_alt_playback,
5661 spec->stream_analog_alt_playback,
5664 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_PLAYBACK],
5665 &pcm_null_stream, NULL, 0);
5666 if (have_multi_adcs) {
5667 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5668 &pcm_analog_alt_capture,
5669 spec->stream_analog_alt_capture,
5671 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5672 spec->num_adc_nids - 1;
5674 setup_pcm_stream(&info->stream[SNDRV_PCM_STREAM_CAPTURE],
5675 &pcm_null_stream, NULL, 0);
5681 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms);
5685 * Standard auto-parser initializations
5688 /* configure the given path as a proper output */
5689 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5691 struct nid_path *path;
5694 path = snd_hda_get_path_from_idx(codec, path_idx);
5695 if (!path || !path->depth)
5697 pin = path->path[path->depth - 1];
5698 restore_pin_ctl(codec, pin);
5699 snd_hda_activate_path(codec, path, path->active,
5700 aamix_default(codec->spec));
5701 set_pin_eapd(codec, pin, path->active);
5704 /* initialize primary output paths */
5705 static void init_multi_out(struct hda_codec *codec)
5707 struct hda_gen_spec *spec = codec->spec;
5710 for (i = 0; i < spec->autocfg.line_outs; i++)
5711 set_output_and_unmute(codec, spec->out_paths[i]);
5715 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5719 for (i = 0; i < num_outs; i++)
5720 set_output_and_unmute(codec, paths[i]);
5723 /* initialize hp and speaker paths */
5724 static void init_extra_out(struct hda_codec *codec)
5726 struct hda_gen_spec *spec = codec->spec;
5728 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5729 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5730 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5731 __init_extra_out(codec, spec->autocfg.speaker_outs,
5732 spec->speaker_paths);
5735 /* initialize multi-io paths */
5736 static void init_multi_io(struct hda_codec *codec)
5738 struct hda_gen_spec *spec = codec->spec;
5741 for (i = 0; i < spec->multi_ios; i++) {
5742 hda_nid_t pin = spec->multi_io[i].pin;
5743 struct nid_path *path;
5744 path = get_multiio_path(codec, i);
5747 if (!spec->multi_io[i].ctl_in)
5748 spec->multi_io[i].ctl_in =
5749 snd_hda_codec_get_pin_target(codec, pin);
5750 snd_hda_activate_path(codec, path, path->active,
5751 aamix_default(spec));
5755 static void init_aamix_paths(struct hda_codec *codec)
5757 struct hda_gen_spec *spec = codec->spec;
5759 if (!spec->have_aamix_ctl)
5761 if (!has_aamix_out_paths(spec))
5763 update_aamix_paths(codec, spec->aamix_mode, spec->out_paths[0],
5764 spec->aamix_out_paths[0],
5765 spec->autocfg.line_out_type);
5766 update_aamix_paths(codec, spec->aamix_mode, spec->hp_paths[0],
5767 spec->aamix_out_paths[1],
5769 update_aamix_paths(codec, spec->aamix_mode, spec->speaker_paths[0],
5770 spec->aamix_out_paths[2],
5771 AUTO_PIN_SPEAKER_OUT);
5774 /* set up input pins and loopback paths */
5775 static void init_analog_input(struct hda_codec *codec)
5777 struct hda_gen_spec *spec = codec->spec;
5778 struct auto_pin_cfg *cfg = &spec->autocfg;
5781 for (i = 0; i < cfg->num_inputs; i++) {
5782 hda_nid_t nid = cfg->inputs[i].pin;
5783 if (is_input_pin(codec, nid))
5784 restore_pin_ctl(codec, nid);
5786 /* init loopback inputs */
5787 if (spec->mixer_nid) {
5788 resume_path_from_idx(codec, spec->loopback_paths[i]);
5789 resume_path_from_idx(codec, spec->loopback_merge_path);
5794 /* initialize ADC paths */
5795 static void init_input_src(struct hda_codec *codec)
5797 struct hda_gen_spec *spec = codec->spec;
5798 struct hda_input_mux *imux = &spec->input_mux;
5799 struct nid_path *path;
5802 if (spec->dyn_adc_switch)
5805 nums = spec->num_adc_nids;
5807 for (c = 0; c < nums; c++) {
5808 for (i = 0; i < imux->num_items; i++) {
5809 path = get_input_path(codec, c, i);
5811 bool active = path->active;
5812 if (i == spec->cur_mux[c])
5814 snd_hda_activate_path(codec, path, active, false);
5818 update_hp_mic(codec, c, true);
5821 if (spec->cap_sync_hook)
5822 spec->cap_sync_hook(codec, NULL, NULL);
5825 /* set right pin controls for digital I/O */
5826 static void init_digital(struct hda_codec *codec)
5828 struct hda_gen_spec *spec = codec->spec;
5832 for (i = 0; i < spec->autocfg.dig_outs; i++)
5833 set_output_and_unmute(codec, spec->digout_paths[i]);
5834 pin = spec->autocfg.dig_in_pin;
5836 restore_pin_ctl(codec, pin);
5837 resume_path_from_idx(codec, spec->digin_path);
5841 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5842 * invalid unsol tags by some reason
5844 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5848 for (i = 0; i < codec->init_pins.used; i++) {
5849 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5850 hda_nid_t nid = pin->nid;
5851 if (is_jack_detectable(codec, nid) &&
5852 !snd_hda_jack_tbl_get(codec, nid))
5853 snd_hda_codec_update_cache(codec, nid, 0,
5854 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5859 * snd_hda_gen_init - initialize the generic spec
5860 * @codec: the HDA codec
5862 * This can be put as patch_ops init function.
5864 int snd_hda_gen_init(struct hda_codec *codec)
5866 struct hda_gen_spec *spec = codec->spec;
5868 if (spec->init_hook)
5869 spec->init_hook(codec);
5871 if (!spec->skip_verbs)
5872 snd_hda_apply_verbs(codec);
5874 init_multi_out(codec);
5875 init_extra_out(codec);
5876 init_multi_io(codec);
5877 init_aamix_paths(codec);
5878 init_analog_input(codec);
5879 init_input_src(codec);
5880 init_digital(codec);
5882 clear_unsol_on_unused_pins(codec);
5884 sync_all_pin_power_ctls(codec);
5886 /* call init functions of standard auto-mute helpers */
5887 update_automute_all(codec);
5889 regcache_sync(codec->core.regmap);
5891 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5892 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5894 hda_call_check_power_status(codec, 0x01);
5897 EXPORT_SYMBOL_GPL(snd_hda_gen_init);
5900 * snd_hda_gen_free - free the generic spec
5901 * @codec: the HDA codec
5903 * This can be put as patch_ops free function.
5905 void snd_hda_gen_free(struct hda_codec *codec)
5907 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_FREE);
5908 snd_hda_gen_spec_free(codec->spec);
5912 EXPORT_SYMBOL_GPL(snd_hda_gen_free);
5915 * snd_hda_gen_reboot_notify - Make codec enter D3 before rebooting
5916 * @codec: the HDA codec
5918 * This can be put as patch_ops reboot_notify function.
5920 void snd_hda_gen_reboot_notify(struct hda_codec *codec)
5922 /* Make the codec enter D3 to avoid spurious noises from the internal
5923 * speaker during (and after) reboot
5925 snd_hda_codec_set_power_to_all(codec, codec->core.afg, AC_PWRST_D3);
5926 snd_hda_codec_write(codec, codec->core.afg, 0,
5927 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
5930 EXPORT_SYMBOL_GPL(snd_hda_gen_reboot_notify);
5934 * snd_hda_gen_check_power_status - check the loopback power save state
5935 * @codec: the HDA codec
5936 * @nid: NID to inspect
5938 * This can be put as patch_ops check_power_status function.
5940 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5942 struct hda_gen_spec *spec = codec->spec;
5943 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5945 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status);
5950 * the generic codec support
5953 static const struct hda_codec_ops generic_patch_ops = {
5954 .build_controls = snd_hda_gen_build_controls,
5955 .build_pcms = snd_hda_gen_build_pcms,
5956 .init = snd_hda_gen_init,
5957 .free = snd_hda_gen_free,
5958 .unsol_event = snd_hda_jack_unsol_event,
5959 .reboot_notify = snd_hda_gen_reboot_notify,
5961 .check_power_status = snd_hda_gen_check_power_status,
5966 * snd_hda_parse_generic_codec - Generic codec parser
5967 * @codec: the HDA codec
5969 static int snd_hda_parse_generic_codec(struct hda_codec *codec)
5971 struct hda_gen_spec *spec;
5974 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5977 snd_hda_gen_spec_init(spec);
5980 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5984 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5988 codec->patch_ops = generic_patch_ops;
5992 snd_hda_gen_free(codec);
5996 static const struct hda_device_id snd_hda_id_generic[] = {
5997 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC, "Generic", snd_hda_parse_generic_codec),
6000 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_generic);
6002 static struct hda_codec_driver generic_driver = {
6003 .id = snd_hda_id_generic,
6006 module_hda_codec_driver(generic_driver);
6008 MODULE_LICENSE("GPL");
6009 MODULE_DESCRIPTION("Generic HD-audio codec parser");