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 <sound/core.h>
32 #include <sound/jack.h>
33 #include "hda_codec.h"
34 #include "hda_local.h"
35 #include "hda_auto_parser.h"
38 #include "hda_generic.h"
41 /* initialize hda_gen_spec struct */
42 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
44 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
45 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
46 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
47 mutex_init(&spec->pcm_mutex);
50 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
52 struct snd_kcontrol_new *
53 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
54 const struct snd_kcontrol_new *temp)
56 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
61 knew->name = kstrdup(name, GFP_KERNEL);
63 knew->name = kstrdup(knew->name, GFP_KERNEL);
68 EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
70 static void free_kctls(struct hda_gen_spec *spec)
72 if (spec->kctls.list) {
73 struct snd_kcontrol_new *kctl = spec->kctls.list;
75 for (i = 0; i < spec->kctls.used; i++)
78 snd_array_free(&spec->kctls);
81 void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
86 snd_array_free(&spec->paths);
87 snd_array_free(&spec->loopback_list);
89 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
94 static void parse_user_hints(struct hda_codec *codec)
96 struct hda_gen_spec *spec = codec->spec;
99 val = snd_hda_get_bool_hint(codec, "jack_detect");
101 codec->no_jack_detect = !val;
102 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
104 codec->inv_jack_detect = !!val;
105 val = snd_hda_get_bool_hint(codec, "trigger_sense");
107 codec->no_trigger_sense = !val;
108 val = snd_hda_get_bool_hint(codec, "inv_eapd");
110 codec->inv_eapd = !!val;
111 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
113 codec->pcm_format_first = !!val;
114 val = snd_hda_get_bool_hint(codec, "sticky_stream");
116 codec->no_sticky_stream = !val;
117 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
119 codec->spdif_status_reset = !!val;
120 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
122 codec->pin_amp_workaround = !!val;
123 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
125 codec->single_adc_amp = !!val;
127 val = snd_hda_get_bool_hint(codec, "auto_mute");
129 spec->suppress_auto_mute = !val;
130 val = snd_hda_get_bool_hint(codec, "auto_mic");
132 spec->suppress_auto_mic = !val;
133 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
135 spec->line_in_auto_switch = !!val;
136 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
138 spec->need_dac_fix = !!val;
139 val = snd_hda_get_bool_hint(codec, "primary_hp");
141 spec->no_primary_hp = !val;
142 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
144 spec->multi_cap_vol = !!val;
145 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
147 spec->inv_dmic_split = !!val;
148 val = snd_hda_get_bool_hint(codec, "indep_hp");
150 spec->indep_hp = !!val;
151 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
153 spec->add_stereo_mix_input = !!val;
154 /* the following two are just for compatibility */
155 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
157 spec->add_jack_modes = !!val;
158 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
160 spec->add_jack_modes = !!val;
161 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
163 spec->add_jack_modes = !!val;
164 val = snd_hda_get_bool_hint(codec, "power_down_unused");
166 spec->power_down_unused = !!val;
167 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
169 spec->hp_mic = !!val;
170 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
172 spec->suppress_hp_mic_detect = !val;
174 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
175 spec->mixer_nid = val;
179 * pin control value accesses
182 #define update_pin_ctl(codec, pin, val) \
183 snd_hda_codec_update_cache(codec, pin, 0, \
184 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
186 /* restore the pinctl based on the cached value */
187 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
189 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
192 /* set the pinctl target value and write it if requested */
193 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
194 unsigned int val, bool do_write)
198 val = snd_hda_correct_pin_ctl(codec, pin, val);
199 snd_hda_codec_set_pin_target(codec, pin, val);
201 update_pin_ctl(codec, pin, val);
204 /* set pinctl target values for all given pins */
205 static void set_pin_targets(struct hda_codec *codec, int num_pins,
206 hda_nid_t *pins, unsigned int val)
209 for (i = 0; i < num_pins; i++)
210 set_pin_target(codec, pins[i], val, false);
217 /* return the position of NID in the list, or -1 if not found */
218 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
221 for (i = 0; i < nums; i++)
227 /* return true if the given NID is contained in the path */
228 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
230 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
233 static struct nid_path *get_nid_path(struct hda_codec *codec,
234 hda_nid_t from_nid, hda_nid_t to_nid,
237 struct hda_gen_spec *spec = codec->spec;
240 for (i = 0; i < spec->paths.used; i++) {
241 struct nid_path *path = snd_array_elem(&spec->paths, i);
242 if (path->depth <= 0)
244 if ((!from_nid || path->path[0] == from_nid) &&
245 (!to_nid || path->path[path->depth - 1] == to_nid)) {
247 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
248 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
255 /* get the path between the given NIDs;
256 * passing 0 to either @pin or @dac behaves as a wildcard
258 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
259 hda_nid_t from_nid, hda_nid_t to_nid)
261 return get_nid_path(codec, from_nid, to_nid, 0);
263 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
265 /* get the index number corresponding to the path instance;
266 * the index starts from 1, for easier checking the invalid value
268 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
270 struct hda_gen_spec *spec = codec->spec;
271 struct nid_path *array = spec->paths.list;
274 if (!spec->paths.used)
277 if (idx < 0 || idx >= spec->paths.used)
281 EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
283 /* get the path instance corresponding to the given index number */
284 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
286 struct hda_gen_spec *spec = codec->spec;
288 if (idx <= 0 || idx > spec->paths.used)
290 return snd_array_elem(&spec->paths, idx - 1);
292 EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
294 /* check whether the given DAC is already found in any existing paths */
295 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
297 struct hda_gen_spec *spec = codec->spec;
300 for (i = 0; i < spec->paths.used; i++) {
301 struct nid_path *path = snd_array_elem(&spec->paths, i);
302 if (path->path[0] == nid)
308 /* check whether the given two widgets can be connected */
309 static bool is_reachable_path(struct hda_codec *codec,
310 hda_nid_t from_nid, hda_nid_t to_nid)
312 if (!from_nid || !to_nid)
314 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
317 /* nid, dir and idx */
318 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
320 /* check whether the given ctl is already assigned in any path elements */
321 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
323 struct hda_gen_spec *spec = codec->spec;
326 val &= AMP_VAL_COMPARE_MASK;
327 for (i = 0; i < spec->paths.used; i++) {
328 struct nid_path *path = snd_array_elem(&spec->paths, i);
329 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
335 /* check whether a control with the given (nid, dir, idx) was assigned */
336 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
337 int dir, int idx, int type)
339 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
340 return is_ctl_used(codec, val, type);
343 static void print_nid_path(const char *pfx, struct nid_path *path)
350 for (i = 0; i < path->depth; i++) {
352 sprintf(tmp, ":%02x", path->path[i]);
353 strlcat(buf, tmp, sizeof(buf));
355 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
358 /* called recursively */
359 static bool __parse_nid_path(struct hda_codec *codec,
360 hda_nid_t from_nid, hda_nid_t to_nid,
361 int anchor_nid, struct nid_path *path,
364 const hda_nid_t *conn;
367 if (to_nid == anchor_nid)
368 anchor_nid = 0; /* anchor passed */
369 else if (to_nid == (hda_nid_t)(-anchor_nid))
370 return false; /* hit the exclusive nid */
372 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
373 for (i = 0; i < nums; i++) {
374 if (conn[i] != from_nid) {
375 /* special case: when from_nid is 0,
376 * try to find an empty DAC
379 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
380 is_dac_already_used(codec, conn[i]))
383 /* anchor is not requested or already passed? */
387 if (depth >= MAX_NID_PATH_DEPTH)
389 for (i = 0; i < nums; i++) {
391 type = get_wcaps_type(get_wcaps(codec, conn[i]));
392 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
395 if (__parse_nid_path(codec, from_nid, conn[i],
396 anchor_nid, path, depth + 1))
402 path->path[path->depth] = conn[i];
403 path->idx[path->depth + 1] = i;
404 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
405 path->multi[path->depth + 1] = 1;
410 /* parse the widget path from the given nid to the target nid;
411 * when @from_nid is 0, try to find an empty DAC;
412 * when @anchor_nid is set to a positive value, only paths through the widget
413 * with the given value are evaluated.
414 * when @anchor_nid is set to a negative value, paths through the widget
415 * with the negative of given value are excluded, only other paths are chosen.
416 * when @anchor_nid is zero, no special handling about path selection.
418 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
419 hda_nid_t to_nid, int anchor_nid,
420 struct nid_path *path)
422 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
423 path->path[path->depth] = to_nid;
429 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
432 * parse the path between the given NIDs and add to the path list.
433 * if no valid path is found, return NULL
436 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
437 hda_nid_t to_nid, int anchor_nid)
439 struct hda_gen_spec *spec = codec->spec;
440 struct nid_path *path;
442 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
445 /* check whether the path has been already added */
446 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
450 path = snd_array_new(&spec->paths);
453 memset(path, 0, sizeof(*path));
454 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
460 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
462 /* clear the given path as invalid so that it won't be picked up later */
463 static void invalidate_nid_path(struct hda_codec *codec, int idx)
465 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
468 memset(path, 0, sizeof(*path));
471 /* look for an empty DAC slot */
472 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
475 struct hda_gen_spec *spec = codec->spec;
479 for (i = 0; i < spec->num_all_dacs; i++) {
480 hda_nid_t nid = spec->all_dacs[i];
481 if (!nid || is_dac_already_used(codec, nid))
483 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
484 if (is_digital != cap_digital)
486 if (is_reachable_path(codec, nid, pin))
492 /* replace the channels in the composed amp value with the given number */
493 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
495 val &= ~(0x3U << 16);
500 /* check whether the widget has the given amp capability for the direction */
501 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
502 int dir, unsigned int bits)
506 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
507 if (query_amp_caps(codec, nid, dir) & bits)
512 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
513 hda_nid_t nid2, int dir)
515 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
516 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
517 return (query_amp_caps(codec, nid1, dir) ==
518 query_amp_caps(codec, nid2, dir));
521 #define nid_has_mute(codec, nid, dir) \
522 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
523 #define nid_has_volume(codec, nid, dir) \
524 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
526 /* look for a widget suitable for assigning a mute switch in the path */
527 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
528 struct nid_path *path)
532 for (i = path->depth - 1; i >= 0; i--) {
533 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
534 return path->path[i];
535 if (i != path->depth - 1 && i != 0 &&
536 nid_has_mute(codec, path->path[i], HDA_INPUT))
537 return path->path[i];
542 /* look for a widget suitable for assigning a volume ctl in the path */
543 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
544 struct nid_path *path)
548 for (i = path->depth - 1; i >= 0; i--) {
549 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
550 return path->path[i];
556 * path activation / deactivation
559 /* can have the amp-in capability? */
560 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
562 hda_nid_t nid = path->path[idx];
563 unsigned int caps = get_wcaps(codec, nid);
564 unsigned int type = get_wcaps_type(caps);
566 if (!(caps & AC_WCAP_IN_AMP))
568 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
573 /* can have the amp-out capability? */
574 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
576 hda_nid_t nid = path->path[idx];
577 unsigned int caps = get_wcaps(codec, nid);
578 unsigned int type = get_wcaps_type(caps);
580 if (!(caps & AC_WCAP_OUT_AMP))
582 if (type == AC_WID_PIN && !idx) /* only for output pins */
587 /* check whether the given (nid,dir,idx) is active */
588 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
589 unsigned int dir, unsigned int idx)
591 struct hda_gen_spec *spec = codec->spec;
594 for (n = 0; n < spec->paths.used; n++) {
595 struct nid_path *path = snd_array_elem(&spec->paths, n);
598 for (i = 0; i < path->depth; i++) {
599 if (path->path[i] == nid) {
600 if (dir == HDA_OUTPUT || path->idx[i] == idx)
609 /* check whether the NID is referred by any active paths */
610 #define is_active_nid_for_any(codec, nid) \
611 is_active_nid(codec, nid, HDA_OUTPUT, 0)
613 /* get the default amp value for the target state */
614 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
615 int dir, unsigned int caps, bool enable)
617 unsigned int val = 0;
619 if (caps & AC_AMPCAP_NUM_STEPS) {
622 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
624 if (caps & AC_AMPCAP_MUTE) {
631 /* initialize the amp value (only at the first time) */
632 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
634 unsigned int caps = query_amp_caps(codec, nid, dir);
635 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
636 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
639 /* calculate amp value mask we can modify;
640 * if the given amp is controlled by mixers, don't touch it
642 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
643 hda_nid_t nid, int dir, int idx,
646 unsigned int mask = 0xff;
648 if (caps & AC_AMPCAP_MUTE) {
649 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
652 if (caps & AC_AMPCAP_NUM_STEPS) {
653 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
654 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
660 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
661 int idx, int idx_to_check, bool enable)
664 unsigned int mask, val;
666 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
669 caps = query_amp_caps(codec, nid, dir);
670 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
671 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
676 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
679 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
682 hda_nid_t nid = path->path[i];
683 init_amp(codec, nid, HDA_OUTPUT, 0);
684 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
687 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
688 int i, bool enable, bool add_aamix)
690 struct hda_gen_spec *spec = codec->spec;
691 const hda_nid_t *conn;
694 hda_nid_t nid = path->path[i];
696 nums = snd_hda_get_conn_list(codec, nid, &conn);
697 type = get_wcaps_type(get_wcaps(codec, nid));
698 if (type == AC_WID_PIN ||
699 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
705 for (n = 0; n < nums; n++)
706 init_amp(codec, nid, HDA_INPUT, n);
708 /* here is a little bit tricky in comparison with activate_amp_out();
709 * when aa-mixer is available, we need to enable the path as well
711 for (n = 0; n < nums; n++) {
712 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
714 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
718 /* activate or deactivate the given path
719 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
721 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
722 bool enable, bool add_aamix)
724 struct hda_gen_spec *spec = codec->spec;
728 path->active = false;
730 for (i = path->depth - 1; i >= 0; i--) {
731 hda_nid_t nid = path->path[i];
732 if (enable && spec->power_down_unused) {
733 /* make sure the widget is powered up */
734 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
735 snd_hda_codec_write(codec, nid, 0,
736 AC_VERB_SET_POWER_STATE,
739 if (enable && path->multi[i])
740 snd_hda_codec_write_cache(codec, nid, 0,
741 AC_VERB_SET_CONNECT_SEL,
743 if (has_amp_in(codec, path, i))
744 activate_amp_in(codec, path, i, enable, add_aamix);
745 if (has_amp_out(codec, path, i))
746 activate_amp_out(codec, path, i, enable);
752 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
754 /* if the given path is inactive, put widgets into D3 (only if suitable) */
755 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
757 struct hda_gen_spec *spec = codec->spec;
758 bool changed = false;
761 if (!spec->power_down_unused || path->active)
764 for (i = 0; i < path->depth; i++) {
765 hda_nid_t nid = path->path[i];
766 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
767 !is_active_nid_for_any(codec, nid)) {
768 snd_hda_codec_write(codec, nid, 0,
769 AC_VERB_SET_POWER_STATE,
777 snd_hda_codec_read(codec, path->path[0], 0,
778 AC_VERB_GET_POWER_STATE, 0);
782 /* turn on/off EAPD on the given pin */
783 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
785 struct hda_gen_spec *spec = codec->spec;
786 if (spec->own_eapd_ctl ||
787 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
791 snd_hda_codec_update_cache(codec, pin, 0,
792 AC_VERB_SET_EAPD_BTLENABLE,
793 enable ? 0x02 : 0x00);
796 /* re-initialize the path specified by the given path index */
797 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
799 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
801 snd_hda_activate_path(codec, path, path->active, false);
806 * Helper functions for creating mixer ctl elements
814 static const struct snd_kcontrol_new control_templates[] = {
815 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
816 HDA_CODEC_MUTE(NULL, 0, 0, 0),
817 HDA_BIND_MUTE(NULL, 0, 0, 0),
820 /* add dynamic controls from template */
821 static struct snd_kcontrol_new *
822 add_control(struct hda_gen_spec *spec, int type, const char *name,
823 int cidx, unsigned long val)
825 struct snd_kcontrol_new *knew;
827 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
831 if (get_amp_nid_(val))
832 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
833 knew->private_value = val;
837 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
838 const char *pfx, const char *dir,
839 const char *sfx, int cidx, unsigned long val)
842 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
843 if (!add_control(spec, type, name, cidx, val))
848 #define add_pb_vol_ctrl(spec, type, pfx, val) \
849 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
850 #define add_pb_sw_ctrl(spec, type, pfx, val) \
851 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
852 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
853 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
854 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
855 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
857 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
858 unsigned int chs, struct nid_path *path)
863 val = path->ctls[NID_PATH_VOL_CTL];
866 val = amp_val_replace_channels(val, chs);
867 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
870 /* return the channel bits suitable for the given path->ctls[] */
871 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
874 int chs = 1; /* mono (left only) */
876 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
877 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
878 chs = 3; /* stereo */
883 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
884 struct nid_path *path)
886 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
887 return add_vol_ctl(codec, pfx, cidx, chs, path);
890 /* create a mute-switch for the given mixer widget;
891 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
893 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
894 unsigned int chs, struct nid_path *path)
897 int type = HDA_CTL_WIDGET_MUTE;
901 val = path->ctls[NID_PATH_MUTE_CTL];
904 val = amp_val_replace_channels(val, chs);
905 if (get_amp_direction_(val) == HDA_INPUT) {
906 hda_nid_t nid = get_amp_nid_(val);
907 int nums = snd_hda_get_num_conns(codec, nid);
909 type = HDA_CTL_BIND_MUTE;
913 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
916 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
917 int cidx, struct nid_path *path)
919 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
920 return add_sw_ctl(codec, pfx, cidx, chs, path);
923 /* any ctl assigned to the path with the given index? */
924 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
926 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
927 return path && path->ctls[ctl_type];
930 static const char * const channel_name[4] = {
931 "Front", "Surround", "CLFE", "Side"
934 /* give some appropriate ctl name prefix for the given line out channel */
935 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
936 int *index, int ctl_type)
938 struct hda_gen_spec *spec = codec->spec;
939 struct auto_pin_cfg *cfg = &spec->autocfg;
942 if (cfg->line_outs == 1 && !spec->multi_ios &&
943 !cfg->hp_outs && !cfg->speaker_outs)
944 return spec->vmaster_mute.hook ? "PCM" : "Master";
946 /* if there is really a single DAC used in the whole output paths,
947 * use it master (or "PCM" if a vmaster hook is present)
949 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
950 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
951 return spec->vmaster_mute.hook ? "PCM" : "Master";
953 /* multi-io channels */
954 if (ch >= cfg->line_outs)
955 return channel_name[ch];
957 switch (cfg->line_out_type) {
958 case AUTO_PIN_SPEAKER_OUT:
959 /* if the primary channel vol/mute is shared with HP volume,
960 * don't name it as Speaker
962 if (!ch && cfg->hp_outs &&
963 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
965 if (cfg->line_outs == 1)
967 if (cfg->line_outs == 2)
968 return ch ? "Bass Speaker" : "Speaker";
970 case AUTO_PIN_HP_OUT:
971 /* if the primary channel vol/mute is shared with spk volume,
972 * don't name it as Headphone
974 if (!ch && cfg->speaker_outs &&
975 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
977 /* for multi-io case, only the primary out */
978 if (ch && spec->multi_ios)
984 /* for a single channel output, we don't have to name the channel */
985 if (cfg->line_outs == 1 && !spec->multi_ios)
988 if (ch >= ARRAY_SIZE(channel_name)) {
993 return channel_name[ch];
1000 /* badness definition */
1002 /* No primary DAC is found for the main output */
1003 BAD_NO_PRIMARY_DAC = 0x10000,
1004 /* No DAC is found for the extra output */
1005 BAD_NO_DAC = 0x4000,
1006 /* No possible multi-ios */
1007 BAD_MULTI_IO = 0x120,
1008 /* No individual DAC for extra output */
1009 BAD_NO_EXTRA_DAC = 0x102,
1010 /* No individual DAC for extra surrounds */
1011 BAD_NO_EXTRA_SURR_DAC = 0x101,
1012 /* Primary DAC shared with main surrounds */
1013 BAD_SHARED_SURROUND = 0x100,
1014 /* No independent HP possible */
1015 BAD_NO_INDEP_HP = 0x10,
1016 /* Primary DAC shared with main CLFE */
1017 BAD_SHARED_CLFE = 0x10,
1018 /* Primary DAC shared with extra surrounds */
1019 BAD_SHARED_EXTRA_SURROUND = 0x10,
1020 /* Volume widget is shared */
1021 BAD_SHARED_VOL = 0x10,
1024 /* look for widgets in the given path which are appropriate for
1025 * volume and mute controls, and assign the values to ctls[].
1027 * When no appropriate widget is found in the path, the badness value
1028 * is incremented depending on the situation. The function returns the
1029 * total badness for both volume and mute controls.
1031 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1038 return BAD_SHARED_VOL * 2;
1040 if (path->ctls[NID_PATH_VOL_CTL] ||
1041 path->ctls[NID_PATH_MUTE_CTL])
1042 return 0; /* already evaluated */
1044 nid = look_for_out_vol_nid(codec, path);
1046 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1047 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1048 badness += BAD_SHARED_VOL;
1050 path->ctls[NID_PATH_VOL_CTL] = val;
1052 badness += BAD_SHARED_VOL;
1053 nid = look_for_out_mute_nid(codec, path);
1055 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1056 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1057 nid_has_mute(codec, nid, HDA_OUTPUT))
1058 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1060 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1061 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1062 badness += BAD_SHARED_VOL;
1064 path->ctls[NID_PATH_MUTE_CTL] = val;
1066 badness += BAD_SHARED_VOL;
1070 const struct badness_table hda_main_out_badness = {
1071 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1072 .no_dac = BAD_NO_DAC,
1073 .shared_primary = BAD_NO_PRIMARY_DAC,
1074 .shared_surr = BAD_SHARED_SURROUND,
1075 .shared_clfe = BAD_SHARED_CLFE,
1076 .shared_surr_main = BAD_SHARED_SURROUND,
1078 EXPORT_SYMBOL_HDA(hda_main_out_badness);
1080 const struct badness_table hda_extra_out_badness = {
1081 .no_primary_dac = BAD_NO_DAC,
1082 .no_dac = BAD_NO_DAC,
1083 .shared_primary = BAD_NO_EXTRA_DAC,
1084 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1085 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1086 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1088 EXPORT_SYMBOL_HDA(hda_extra_out_badness);
1090 /* get the DAC of the primary output corresponding to the given array index */
1091 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1093 struct hda_gen_spec *spec = codec->spec;
1094 struct auto_pin_cfg *cfg = &spec->autocfg;
1096 if (cfg->line_outs > idx)
1097 return spec->private_dac_nids[idx];
1098 idx -= cfg->line_outs;
1099 if (spec->multi_ios > idx)
1100 return spec->multi_io[idx].dac;
1104 /* return the DAC if it's reachable, otherwise zero */
1105 static inline hda_nid_t try_dac(struct hda_codec *codec,
1106 hda_nid_t dac, hda_nid_t pin)
1108 return is_reachable_path(codec, dac, pin) ? dac : 0;
1111 /* try to assign DACs to pins and return the resultant badness */
1112 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1113 const hda_nid_t *pins, hda_nid_t *dacs,
1115 const struct badness_table *bad)
1117 struct hda_gen_spec *spec = codec->spec;
1125 for (i = 0; i < num_outs; i++) {
1126 struct nid_path *path;
1127 hda_nid_t pin = pins[i];
1129 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1131 badness += assign_out_path_ctls(codec, path);
1135 dacs[i] = look_for_dac(codec, pin, false);
1136 if (!dacs[i] && !i) {
1137 /* try to steal the DAC of surrounds for the front */
1138 for (j = 1; j < num_outs; j++) {
1139 if (is_reachable_path(codec, dacs[j], pin)) {
1142 invalidate_nid_path(codec, path_idx[j]);
1151 dac = try_dac(codec, get_primary_out(codec, i), pin);
1153 dac = try_dac(codec, dacs[0], pin);
1155 dac = try_dac(codec, get_primary_out(codec, i), pin);
1158 badness += bad->shared_primary;
1160 badness += bad->shared_surr;
1162 badness += bad->shared_clfe;
1163 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1164 dac = spec->private_dac_nids[0];
1165 badness += bad->shared_surr_main;
1167 badness += bad->no_primary_dac;
1169 badness += bad->no_dac;
1173 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1174 if (!path && !i && spec->mixer_nid) {
1175 /* try with aamix */
1176 path = snd_hda_add_new_path(codec, dac, pin, 0);
1180 badness += bad->no_dac;
1182 /* print_nid_path("output", path); */
1183 path->active = true;
1184 path_idx[i] = snd_hda_get_path_idx(codec, path);
1185 badness += assign_out_path_ctls(codec, path);
1192 /* return NID if the given pin has only a single connection to a certain DAC */
1193 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1195 struct hda_gen_spec *spec = codec->spec;
1197 hda_nid_t nid_found = 0;
1199 for (i = 0; i < spec->num_all_dacs; i++) {
1200 hda_nid_t nid = spec->all_dacs[i];
1201 if (!nid || is_dac_already_used(codec, nid))
1203 if (is_reachable_path(codec, nid, pin)) {
1212 /* check whether the given pin can be a multi-io pin */
1213 static bool can_be_multiio_pin(struct hda_codec *codec,
1214 unsigned int location, hda_nid_t nid)
1216 unsigned int defcfg, caps;
1218 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1219 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1221 if (location && get_defcfg_location(defcfg) != location)
1223 caps = snd_hda_query_pin_caps(codec, nid);
1224 if (!(caps & AC_PINCAP_OUT))
1229 /* count the number of input pins that are capable to be multi-io */
1230 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1232 struct hda_gen_spec *spec = codec->spec;
1233 struct auto_pin_cfg *cfg = &spec->autocfg;
1234 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1235 unsigned int location = get_defcfg_location(defcfg);
1239 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1240 for (i = 0; i < cfg->num_inputs; i++) {
1241 if (cfg->inputs[i].type != type)
1243 if (can_be_multiio_pin(codec, location,
1244 cfg->inputs[i].pin))
1254 * When hardwired is set, try to fill ony hardwired pins, and returns
1255 * zero if any pins are filled, non-zero if nothing found.
1256 * When hardwired is off, try to fill possible input pins, and returns
1257 * the badness value.
1259 static int fill_multi_ios(struct hda_codec *codec,
1260 hda_nid_t reference_pin,
1263 struct hda_gen_spec *spec = codec->spec;
1264 struct auto_pin_cfg *cfg = &spec->autocfg;
1265 int type, i, j, num_pins, old_pins;
1266 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1267 unsigned int location = get_defcfg_location(defcfg);
1269 struct nid_path *path;
1271 old_pins = spec->multi_ios;
1275 num_pins = count_multiio_pins(codec, reference_pin);
1279 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1280 for (i = 0; i < cfg->num_inputs; i++) {
1281 hda_nid_t nid = cfg->inputs[i].pin;
1284 if (cfg->inputs[i].type != type)
1286 if (!can_be_multiio_pin(codec, location, nid))
1288 for (j = 0; j < spec->multi_ios; j++) {
1289 if (nid == spec->multi_io[j].pin)
1292 if (j < spec->multi_ios)
1296 dac = get_dac_if_single(codec, nid);
1298 dac = look_for_dac(codec, nid, false);
1303 path = snd_hda_add_new_path(codec, dac, nid,
1309 /* print_nid_path("multiio", path); */
1310 spec->multi_io[spec->multi_ios].pin = nid;
1311 spec->multi_io[spec->multi_ios].dac = dac;
1312 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1313 snd_hda_get_path_idx(codec, path);
1315 if (spec->multi_ios >= 2)
1321 badness = BAD_MULTI_IO;
1322 if (old_pins == spec->multi_ios) {
1324 return 1; /* nothing found */
1326 return badness; /* no badness if nothing found */
1328 if (!hardwired && spec->multi_ios < 2) {
1329 /* cancel newly assigned paths */
1330 spec->paths.used -= spec->multi_ios - old_pins;
1331 spec->multi_ios = old_pins;
1335 /* assign volume and mute controls */
1336 for (i = old_pins; i < spec->multi_ios; i++) {
1337 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1338 badness += assign_out_path_ctls(codec, path);
1344 /* map DACs for all pins in the list if they are single connections */
1345 static bool map_singles(struct hda_codec *codec, int outs,
1346 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1348 struct hda_gen_spec *spec = codec->spec;
1351 for (i = 0; i < outs; i++) {
1352 struct nid_path *path;
1356 dac = get_dac_if_single(codec, pins[i]);
1359 path = snd_hda_add_new_path(codec, dac, pins[i],
1361 if (!path && !i && spec->mixer_nid)
1362 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1366 /* print_nid_path("output", path); */
1367 path->active = true;
1368 path_idx[i] = snd_hda_get_path_idx(codec, path);
1374 /* create a new path including aamix if available, and return its index */
1375 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1377 struct hda_gen_spec *spec = codec->spec;
1378 struct nid_path *path;
1379 hda_nid_t path_dac, dac, pin;
1381 path = snd_hda_get_path_from_idx(codec, path_idx);
1382 if (!path || !path->depth ||
1383 is_nid_contained(path, spec->mixer_nid))
1385 path_dac = path->path[0];
1386 dac = spec->private_dac_nids[0];
1387 pin = path->path[path->depth - 1];
1388 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1390 if (dac != path_dac)
1392 else if (spec->multiout.hp_out_nid[0])
1393 dac = spec->multiout.hp_out_nid[0];
1394 else if (spec->multiout.extra_out_nid[0])
1395 dac = spec->multiout.extra_out_nid[0];
1399 path = snd_hda_add_new_path(codec, dac, pin,
1404 /* print_nid_path("output-aamix", path); */
1405 path->active = false; /* unused as default */
1406 return snd_hda_get_path_idx(codec, path);
1409 /* check whether the independent HP is available with the current config */
1410 static bool indep_hp_possible(struct hda_codec *codec)
1412 struct hda_gen_spec *spec = codec->spec;
1413 struct auto_pin_cfg *cfg = &spec->autocfg;
1414 struct nid_path *path;
1417 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1418 idx = spec->out_paths[0];
1420 idx = spec->hp_paths[0];
1421 path = snd_hda_get_path_from_idx(codec, idx);
1425 /* assume no path conflicts unless aamix is involved */
1426 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1429 /* check whether output paths contain aamix */
1430 for (i = 0; i < cfg->line_outs; i++) {
1431 if (spec->out_paths[i] == idx)
1433 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1434 if (path && is_nid_contained(path, spec->mixer_nid))
1437 for (i = 0; i < cfg->speaker_outs; i++) {
1438 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1439 if (path && is_nid_contained(path, spec->mixer_nid))
1446 /* fill the empty entries in the dac array for speaker/hp with the
1447 * shared dac pointed by the paths
1449 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1450 hda_nid_t *dacs, int *path_idx)
1452 struct nid_path *path;
1455 for (i = 0; i < num_outs; i++) {
1458 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1461 dacs[i] = path->path[0];
1465 /* fill in the dac_nids table from the parsed pin configuration */
1466 static int fill_and_eval_dacs(struct hda_codec *codec,
1467 bool fill_hardwired,
1468 bool fill_mio_first)
1470 struct hda_gen_spec *spec = codec->spec;
1471 struct auto_pin_cfg *cfg = &spec->autocfg;
1472 int i, err, badness;
1474 /* set num_dacs once to full for look_for_dac() */
1475 spec->multiout.num_dacs = cfg->line_outs;
1476 spec->multiout.dac_nids = spec->private_dac_nids;
1477 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1478 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1479 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1480 spec->multi_ios = 0;
1481 snd_array_free(&spec->paths);
1483 /* clear path indices */
1484 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1485 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1486 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1487 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1488 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1489 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1490 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1491 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1495 /* fill hard-wired DACs first */
1496 if (fill_hardwired) {
1499 mapped = map_singles(codec, cfg->line_outs,
1501 spec->private_dac_nids,
1503 mapped |= map_singles(codec, cfg->hp_outs,
1505 spec->multiout.hp_out_nid,
1507 mapped |= map_singles(codec, cfg->speaker_outs,
1509 spec->multiout.extra_out_nid,
1510 spec->speaker_paths);
1511 if (fill_mio_first && cfg->line_outs == 1 &&
1512 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1513 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1520 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1521 spec->private_dac_nids, spec->out_paths,
1522 spec->main_out_badness);
1524 if (fill_mio_first &&
1525 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1526 /* try to fill multi-io first */
1527 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1530 /* we don't count badness at this stage yet */
1533 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1534 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1535 spec->multiout.hp_out_nid,
1537 spec->extra_out_badness);
1542 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1543 err = try_assign_dacs(codec, cfg->speaker_outs,
1545 spec->multiout.extra_out_nid,
1546 spec->speaker_paths,
1547 spec->extra_out_badness);
1552 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1553 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1559 if (spec->mixer_nid) {
1560 spec->aamix_out_paths[0] =
1561 check_aamix_out_path(codec, spec->out_paths[0]);
1562 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1563 spec->aamix_out_paths[1] =
1564 check_aamix_out_path(codec, spec->hp_paths[0]);
1565 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1566 spec->aamix_out_paths[2] =
1567 check_aamix_out_path(codec, spec->speaker_paths[0]);
1570 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1571 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1572 spec->multi_ios = 1; /* give badness */
1574 /* re-count num_dacs and squash invalid entries */
1575 spec->multiout.num_dacs = 0;
1576 for (i = 0; i < cfg->line_outs; i++) {
1577 if (spec->private_dac_nids[i])
1578 spec->multiout.num_dacs++;
1580 memmove(spec->private_dac_nids + i,
1581 spec->private_dac_nids + i + 1,
1582 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1583 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1587 spec->ext_channel_count = spec->min_channel_count =
1588 spec->multiout.num_dacs * 2;
1590 if (spec->multi_ios == 2) {
1591 for (i = 0; i < 2; i++)
1592 spec->private_dac_nids[spec->multiout.num_dacs++] =
1593 spec->multi_io[i].dac;
1594 } else if (spec->multi_ios) {
1595 spec->multi_ios = 0;
1596 badness += BAD_MULTI_IO;
1599 if (spec->indep_hp && !indep_hp_possible(codec))
1600 badness += BAD_NO_INDEP_HP;
1602 /* re-fill the shared DAC for speaker / headphone */
1603 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1604 refill_shared_dacs(codec, cfg->hp_outs,
1605 spec->multiout.hp_out_nid,
1607 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1608 refill_shared_dacs(codec, cfg->speaker_outs,
1609 spec->multiout.extra_out_nid,
1610 spec->speaker_paths);
1615 #define DEBUG_BADNESS
1617 #ifdef DEBUG_BADNESS
1618 #define debug_badness snd_printdd
1620 #define debug_badness(...)
1623 #ifdef DEBUG_BADNESS
1624 static inline void print_nid_path_idx(struct hda_codec *codec,
1625 const char *pfx, int idx)
1627 struct nid_path *path;
1629 path = snd_hda_get_path_from_idx(codec, idx);
1631 print_nid_path(pfx, path);
1634 static void debug_show_configs(struct hda_codec *codec,
1635 struct auto_pin_cfg *cfg)
1637 struct hda_gen_spec *spec = codec->spec;
1638 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1641 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1642 cfg->line_out_pins[0], cfg->line_out_pins[1],
1643 cfg->line_out_pins[2], cfg->line_out_pins[3],
1644 spec->multiout.dac_nids[0],
1645 spec->multiout.dac_nids[1],
1646 spec->multiout.dac_nids[2],
1647 spec->multiout.dac_nids[3],
1648 lo_type[cfg->line_out_type]);
1649 for (i = 0; i < cfg->line_outs; i++)
1650 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1651 if (spec->multi_ios > 0)
1652 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1654 spec->multi_io[0].pin, spec->multi_io[1].pin,
1655 spec->multi_io[0].dac, spec->multi_io[1].dac);
1656 for (i = 0; i < spec->multi_ios; i++)
1657 print_nid_path_idx(codec, " mio",
1658 spec->out_paths[cfg->line_outs + i]);
1660 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1661 cfg->hp_pins[0], cfg->hp_pins[1],
1662 cfg->hp_pins[2], cfg->hp_pins[3],
1663 spec->multiout.hp_out_nid[0],
1664 spec->multiout.hp_out_nid[1],
1665 spec->multiout.hp_out_nid[2],
1666 spec->multiout.hp_out_nid[3]);
1667 for (i = 0; i < cfg->hp_outs; i++)
1668 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1669 if (cfg->speaker_outs)
1670 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1671 cfg->speaker_pins[0], cfg->speaker_pins[1],
1672 cfg->speaker_pins[2], cfg->speaker_pins[3],
1673 spec->multiout.extra_out_nid[0],
1674 spec->multiout.extra_out_nid[1],
1675 spec->multiout.extra_out_nid[2],
1676 spec->multiout.extra_out_nid[3]);
1677 for (i = 0; i < cfg->speaker_outs; i++)
1678 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1679 for (i = 0; i < 3; i++)
1680 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1683 #define debug_show_configs(codec, cfg) /* NOP */
1686 /* find all available DACs of the codec */
1687 static void fill_all_dac_nids(struct hda_codec *codec)
1689 struct hda_gen_spec *spec = codec->spec;
1691 hda_nid_t nid = codec->start_nid;
1693 spec->num_all_dacs = 0;
1694 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1695 for (i = 0; i < codec->num_nodes; i++, nid++) {
1696 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1698 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1699 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1702 spec->all_dacs[spec->num_all_dacs++] = nid;
1706 static int parse_output_paths(struct hda_codec *codec)
1708 struct hda_gen_spec *spec = codec->spec;
1709 struct auto_pin_cfg *cfg = &spec->autocfg;
1710 struct auto_pin_cfg *best_cfg;
1712 int best_badness = INT_MAX;
1714 bool fill_hardwired = true, fill_mio_first = true;
1715 bool best_wired = true, best_mio = true;
1716 bool hp_spk_swapped = false;
1718 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1724 badness = fill_and_eval_dacs(codec, fill_hardwired,
1730 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1731 cfg->line_out_type, fill_hardwired, fill_mio_first,
1733 debug_show_configs(codec, cfg);
1734 if (badness < best_badness) {
1735 best_badness = badness;
1737 best_wired = fill_hardwired;
1738 best_mio = fill_mio_first;
1742 fill_mio_first = !fill_mio_first;
1743 if (!fill_mio_first)
1745 fill_hardwired = !fill_hardwired;
1746 if (!fill_hardwired)
1750 hp_spk_swapped = true;
1751 if (cfg->speaker_outs > 0 &&
1752 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1753 cfg->hp_outs = cfg->line_outs;
1754 memcpy(cfg->hp_pins, cfg->line_out_pins,
1755 sizeof(cfg->hp_pins));
1756 cfg->line_outs = cfg->speaker_outs;
1757 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1758 sizeof(cfg->speaker_pins));
1759 cfg->speaker_outs = 0;
1760 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1761 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1762 fill_hardwired = true;
1765 if (cfg->hp_outs > 0 &&
1766 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1767 cfg->speaker_outs = cfg->line_outs;
1768 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1769 sizeof(cfg->speaker_pins));
1770 cfg->line_outs = cfg->hp_outs;
1771 memcpy(cfg->line_out_pins, cfg->hp_pins,
1772 sizeof(cfg->hp_pins));
1774 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1775 cfg->line_out_type = AUTO_PIN_HP_OUT;
1776 fill_hardwired = true;
1783 debug_badness("==> restoring best_cfg\n");
1785 fill_and_eval_dacs(codec, best_wired, best_mio);
1787 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1788 cfg->line_out_type, best_wired, best_mio);
1789 debug_show_configs(codec, cfg);
1791 if (cfg->line_out_pins[0]) {
1792 struct nid_path *path;
1793 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1795 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1796 if (spec->vmaster_nid)
1797 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1798 HDA_OUTPUT, spec->vmaster_tlv);
1801 /* set initial pinctl targets */
1802 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1806 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1807 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1808 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1809 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1810 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1811 set_pin_targets(codec, cfg->speaker_outs,
1812 cfg->speaker_pins, val);
1815 /* clear indep_hp flag if not available */
1816 if (spec->indep_hp && !indep_hp_possible(codec))
1823 /* add playback controls from the parsed DAC table */
1824 static int create_multi_out_ctls(struct hda_codec *codec,
1825 const struct auto_pin_cfg *cfg)
1827 struct hda_gen_spec *spec = codec->spec;
1828 int i, err, noutputs;
1830 noutputs = cfg->line_outs;
1831 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1832 noutputs += spec->multi_ios;
1834 for (i = 0; i < noutputs; i++) {
1837 struct nid_path *path;
1839 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1843 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
1844 if (!name || !strcmp(name, "CLFE")) {
1846 err = add_vol_ctl(codec, "Center", 0, 1, path);
1849 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1853 err = add_stereo_vol(codec, name, index, path);
1858 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1859 if (!name || !strcmp(name, "CLFE")) {
1860 err = add_sw_ctl(codec, "Center", 0, 1, path);
1863 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1867 err = add_stereo_sw(codec, name, index, path);
1875 static int create_extra_out(struct hda_codec *codec, int path_idx,
1876 const char *pfx, int cidx)
1878 struct nid_path *path;
1881 path = snd_hda_get_path_from_idx(codec, path_idx);
1884 err = add_stereo_vol(codec, pfx, cidx, path);
1887 err = add_stereo_sw(codec, pfx, cidx, path);
1893 /* add playback controls for speaker and HP outputs */
1894 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1895 const int *paths, const char *pfx)
1899 for (i = 0; i < num_pins; i++) {
1904 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1905 name = "Bass Speaker";
1906 else if (num_pins >= 3) {
1907 snprintf(tmp, sizeof(tmp), "%s %s",
1908 pfx, channel_name[i]);
1914 err = create_extra_out(codec, paths[i], name, idx);
1921 static int create_hp_out_ctls(struct hda_codec *codec)
1923 struct hda_gen_spec *spec = codec->spec;
1924 return create_extra_outs(codec, spec->autocfg.hp_outs,
1929 static int create_speaker_out_ctls(struct hda_codec *codec)
1931 struct hda_gen_spec *spec = codec->spec;
1932 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1933 spec->speaker_paths,
1938 * independent HP controls
1941 /* update HP auto-mute state too */
1942 static void update_hp_automute_hook(struct hda_codec *codec)
1944 struct hda_gen_spec *spec = codec->spec;
1946 if (spec->hp_automute_hook)
1947 spec->hp_automute_hook(codec, NULL);
1949 snd_hda_gen_hp_automute(codec, NULL);
1952 static int indep_hp_info(struct snd_kcontrol *kcontrol,
1953 struct snd_ctl_elem_info *uinfo)
1955 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1958 static int indep_hp_get(struct snd_kcontrol *kcontrol,
1959 struct snd_ctl_elem_value *ucontrol)
1961 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1962 struct hda_gen_spec *spec = codec->spec;
1963 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1967 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1968 int nomix_path_idx, int mix_path_idx,
1971 static int indep_hp_put(struct snd_kcontrol *kcontrol,
1972 struct snd_ctl_elem_value *ucontrol)
1974 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1975 struct hda_gen_spec *spec = codec->spec;
1976 unsigned int select = ucontrol->value.enumerated.item[0];
1979 mutex_lock(&spec->pcm_mutex);
1980 if (spec->active_streams) {
1985 if (spec->indep_hp_enabled != select) {
1987 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1988 dacp = &spec->private_dac_nids[0];
1990 dacp = &spec->multiout.hp_out_nid[0];
1992 /* update HP aamix paths in case it conflicts with indep HP */
1993 if (spec->have_aamix_ctl) {
1994 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1995 update_aamix_paths(codec, spec->aamix_mode,
1997 spec->aamix_out_paths[0],
1998 spec->autocfg.line_out_type);
2000 update_aamix_paths(codec, spec->aamix_mode,
2002 spec->aamix_out_paths[1],
2006 spec->indep_hp_enabled = select;
2007 if (spec->indep_hp_enabled)
2010 *dacp = spec->alt_dac_nid;
2012 update_hp_automute_hook(codec);
2016 mutex_unlock(&spec->pcm_mutex);
2020 static const struct snd_kcontrol_new indep_hp_ctl = {
2021 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2022 .name = "Independent HP",
2023 .info = indep_hp_info,
2024 .get = indep_hp_get,
2025 .put = indep_hp_put,
2029 static int create_indep_hp_ctls(struct hda_codec *codec)
2031 struct hda_gen_spec *spec = codec->spec;
2034 if (!spec->indep_hp)
2036 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2037 dac = spec->multiout.dac_nids[0];
2039 dac = spec->multiout.hp_out_nid[0];
2045 spec->indep_hp_enabled = false;
2046 spec->alt_dac_nid = dac;
2047 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2053 * channel mode enum control
2056 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2057 struct snd_ctl_elem_info *uinfo)
2059 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2060 struct hda_gen_spec *spec = codec->spec;
2063 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2065 uinfo->value.enumerated.items = spec->multi_ios + 1;
2066 if (uinfo->value.enumerated.item > spec->multi_ios)
2067 uinfo->value.enumerated.item = spec->multi_ios;
2068 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2069 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2073 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2074 struct snd_ctl_elem_value *ucontrol)
2076 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2077 struct hda_gen_spec *spec = codec->spec;
2078 ucontrol->value.enumerated.item[0] =
2079 (spec->ext_channel_count - spec->min_channel_count) / 2;
2083 static inline struct nid_path *
2084 get_multiio_path(struct hda_codec *codec, int idx)
2086 struct hda_gen_spec *spec = codec->spec;
2087 return snd_hda_get_path_from_idx(codec,
2088 spec->out_paths[spec->autocfg.line_outs + idx]);
2091 static void update_automute_all(struct hda_codec *codec);
2093 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2094 * used for output paths
2096 static bool aamix_default(struct hda_gen_spec *spec)
2098 return !spec->have_aamix_ctl || spec->aamix_mode;
2101 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2103 struct hda_gen_spec *spec = codec->spec;
2104 hda_nid_t nid = spec->multi_io[idx].pin;
2105 struct nid_path *path;
2107 path = get_multiio_path(codec, idx);
2111 if (path->active == output)
2115 set_pin_target(codec, nid, PIN_OUT, true);
2116 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2117 set_pin_eapd(codec, nid, true);
2119 set_pin_eapd(codec, nid, false);
2120 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2121 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2122 path_power_down_sync(codec, path);
2125 /* update jack retasking in case it modifies any of them */
2126 update_automute_all(codec);
2131 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2132 struct snd_ctl_elem_value *ucontrol)
2134 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2135 struct hda_gen_spec *spec = codec->spec;
2138 ch = ucontrol->value.enumerated.item[0];
2139 if (ch < 0 || ch > spec->multi_ios)
2141 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2143 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2144 for (i = 0; i < spec->multi_ios; i++)
2145 set_multi_io(codec, i, i < ch);
2146 spec->multiout.max_channels = max(spec->ext_channel_count,
2147 spec->const_channel_count);
2148 if (spec->need_dac_fix)
2149 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2153 static const struct snd_kcontrol_new channel_mode_enum = {
2154 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2155 .name = "Channel Mode",
2156 .info = ch_mode_info,
2161 static int create_multi_channel_mode(struct hda_codec *codec)
2163 struct hda_gen_spec *spec = codec->spec;
2165 if (spec->multi_ios > 0) {
2166 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2173 * aamix loopback enable/disable switch
2176 #define loopback_mixing_info indep_hp_info
2178 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2179 struct snd_ctl_elem_value *ucontrol)
2181 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2182 struct hda_gen_spec *spec = codec->spec;
2183 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2187 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2188 int nomix_path_idx, int mix_path_idx,
2191 struct hda_gen_spec *spec = codec->spec;
2192 struct nid_path *nomix_path, *mix_path;
2194 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2195 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2196 if (!nomix_path || !mix_path)
2199 /* if HP aamix path is driven from a different DAC and the
2200 * independent HP mode is ON, can't turn on aamix path
2202 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2203 mix_path->path[0] != spec->alt_dac_nid)
2207 snd_hda_activate_path(codec, nomix_path, false, true);
2208 snd_hda_activate_path(codec, mix_path, true, true);
2209 path_power_down_sync(codec, nomix_path);
2211 snd_hda_activate_path(codec, mix_path, false, false);
2212 snd_hda_activate_path(codec, nomix_path, true, false);
2213 path_power_down_sync(codec, mix_path);
2217 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2218 struct snd_ctl_elem_value *ucontrol)
2220 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2221 struct hda_gen_spec *spec = codec->spec;
2222 unsigned int val = ucontrol->value.enumerated.item[0];
2224 if (val == spec->aamix_mode)
2226 spec->aamix_mode = val;
2227 update_aamix_paths(codec, val, spec->out_paths[0],
2228 spec->aamix_out_paths[0],
2229 spec->autocfg.line_out_type);
2230 update_aamix_paths(codec, val, spec->hp_paths[0],
2231 spec->aamix_out_paths[1],
2233 update_aamix_paths(codec, val, spec->speaker_paths[0],
2234 spec->aamix_out_paths[2],
2235 AUTO_PIN_SPEAKER_OUT);
2239 static const struct snd_kcontrol_new loopback_mixing_enum = {
2240 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2241 .name = "Loopback Mixing",
2242 .info = loopback_mixing_info,
2243 .get = loopback_mixing_get,
2244 .put = loopback_mixing_put,
2247 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2249 struct hda_gen_spec *spec = codec->spec;
2251 if (!spec->mixer_nid)
2253 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2254 spec->aamix_out_paths[2]))
2256 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2258 spec->have_aamix_ctl = 1;
2263 * shared headphone/mic handling
2266 static void call_update_outputs(struct hda_codec *codec);
2268 /* for shared I/O, change the pin-control accordingly */
2269 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2271 struct hda_gen_spec *spec = codec->spec;
2276 pin = spec->hp_mic_pin;
2277 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2280 val = snd_hda_codec_get_pin_target(codec, pin);
2290 val = snd_hda_get_default_vref(codec, pin);
2291 /* if the HP pin doesn't support VREF and the codec driver gives an
2292 * alternative pin, set up the VREF on that pin instead
2294 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2295 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2296 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2297 if (vref_val != AC_PINCTL_VREF_HIZ)
2298 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2299 PIN_IN | (as_mic ? vref_val : 0));
2302 if (!spec->hp_mic_jack_modes) {
2307 set_pin_target(codec, pin, val, true);
2308 update_hp_automute_hook(codec);
2312 /* create a shared input with the headphone out */
2313 static int create_hp_mic(struct hda_codec *codec)
2315 struct hda_gen_spec *spec = codec->spec;
2316 struct auto_pin_cfg *cfg = &spec->autocfg;
2317 unsigned int defcfg;
2320 if (!spec->hp_mic) {
2321 if (spec->suppress_hp_mic_detect)
2323 /* automatic detection: only if no input or a single internal
2324 * input pin is found, try to detect the shared hp/mic
2326 if (cfg->num_inputs > 1)
2328 else if (cfg->num_inputs == 1) {
2329 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2330 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2335 spec->hp_mic = 0; /* clear once */
2336 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2340 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2341 nid = cfg->line_out_pins[0];
2342 else if (cfg->hp_outs > 0)
2343 nid = cfg->hp_pins[0];
2347 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2348 return 0; /* no input */
2350 cfg->inputs[cfg->num_inputs].pin = nid;
2351 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2352 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2355 spec->hp_mic_pin = nid;
2356 /* we can't handle auto-mic together with HP-mic */
2357 spec->suppress_auto_mic = 1;
2358 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2366 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2368 static const char * const out_jack_texts[] = {
2369 "Line Out", "Headphone Out",
2372 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2373 struct snd_ctl_elem_info *uinfo)
2375 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2378 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2379 struct snd_ctl_elem_value *ucontrol)
2381 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2382 hda_nid_t nid = kcontrol->private_value;
2383 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2384 ucontrol->value.enumerated.item[0] = 1;
2386 ucontrol->value.enumerated.item[0] = 0;
2390 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2391 struct snd_ctl_elem_value *ucontrol)
2393 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2394 hda_nid_t nid = kcontrol->private_value;
2397 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2398 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2400 snd_hda_set_pin_ctl_cache(codec, nid, val);
2404 static const struct snd_kcontrol_new out_jack_mode_enum = {
2405 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2406 .info = out_jack_mode_info,
2407 .get = out_jack_mode_get,
2408 .put = out_jack_mode_put,
2411 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2413 struct hda_gen_spec *spec = codec->spec;
2416 for (i = 0; i < spec->kctls.used; i++) {
2417 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2418 if (!strcmp(kctl->name, name) && kctl->index == idx)
2424 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2425 char *name, size_t name_len)
2427 struct hda_gen_spec *spec = codec->spec;
2430 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2431 strlcat(name, " Jack Mode", name_len);
2433 for (; find_kctl_name(codec, name, idx); idx++)
2437 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2439 struct hda_gen_spec *spec = codec->spec;
2440 if (spec->add_jack_modes) {
2441 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2442 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2448 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2451 struct hda_gen_spec *spec = codec->spec;
2454 for (i = 0; i < num_pins; i++) {
2455 hda_nid_t pin = pins[i];
2456 if (pin == spec->hp_mic_pin) {
2457 int ret = create_hp_mic_jack_mode(codec, pin);
2462 if (get_out_jack_num_items(codec, pin) > 1) {
2463 struct snd_kcontrol_new *knew;
2465 get_jack_mode_name(codec, pin, name, sizeof(name));
2466 knew = snd_hda_gen_add_kctl(spec, name,
2467 &out_jack_mode_enum);
2470 knew->private_value = pin;
2481 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2484 static const char * const vref_texts[NUM_VREFS] = {
2485 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2486 "", "Mic 80pc Bias", "Mic 100pc Bias"
2489 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2491 unsigned int pincap;
2493 pincap = snd_hda_query_pin_caps(codec, pin);
2494 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2495 /* filter out unusual vrefs */
2496 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2500 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2501 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2503 unsigned int i, n = 0;
2505 for (i = 0; i < NUM_VREFS; i++) {
2506 if (vref_caps & (1 << i)) {
2515 /* convert back from the vref ctl index to the enum item index */
2516 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2518 unsigned int i, n = 0;
2520 for (i = 0; i < NUM_VREFS; i++) {
2523 if (vref_caps & (1 << i))
2529 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2530 struct snd_ctl_elem_info *uinfo)
2532 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2533 hda_nid_t nid = kcontrol->private_value;
2534 unsigned int vref_caps = get_vref_caps(codec, nid);
2536 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2538 /* set the right text */
2539 strcpy(uinfo->value.enumerated.name,
2540 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2544 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2545 struct snd_ctl_elem_value *ucontrol)
2547 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2548 hda_nid_t nid = kcontrol->private_value;
2549 unsigned int vref_caps = get_vref_caps(codec, nid);
2552 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2553 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2557 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2558 struct snd_ctl_elem_value *ucontrol)
2560 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2561 hda_nid_t nid = kcontrol->private_value;
2562 unsigned int vref_caps = get_vref_caps(codec, nid);
2563 unsigned int val, idx;
2565 val = snd_hda_codec_get_pin_target(codec, nid);
2566 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2567 if (idx == ucontrol->value.enumerated.item[0])
2570 val &= ~AC_PINCTL_VREFEN;
2571 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2572 snd_hda_set_pin_ctl_cache(codec, nid, val);
2576 static const struct snd_kcontrol_new in_jack_mode_enum = {
2577 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2578 .info = in_jack_mode_info,
2579 .get = in_jack_mode_get,
2580 .put = in_jack_mode_put,
2583 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2585 struct hda_gen_spec *spec = codec->spec;
2587 if (spec->add_jack_modes)
2588 nitems = hweight32(get_vref_caps(codec, pin));
2589 return nitems ? nitems : 1;
2592 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2594 struct hda_gen_spec *spec = codec->spec;
2595 struct snd_kcontrol_new *knew;
2597 unsigned int defcfg;
2599 if (pin == spec->hp_mic_pin)
2600 return 0; /* already done in create_out_jack_mode() */
2602 /* no jack mode for fixed pins */
2603 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2604 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2607 /* no multiple vref caps? */
2608 if (get_in_jack_num_items(codec, pin) <= 1)
2611 get_jack_mode_name(codec, pin, name, sizeof(name));
2612 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2615 knew->private_value = pin;
2620 * HP/mic shared jack mode
2622 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2623 struct snd_ctl_elem_info *uinfo)
2625 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2626 hda_nid_t nid = kcontrol->private_value;
2627 int out_jacks = get_out_jack_num_items(codec, nid);
2628 int in_jacks = get_in_jack_num_items(codec, nid);
2629 const char *text = NULL;
2632 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2634 uinfo->value.enumerated.items = out_jacks + in_jacks;
2635 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2636 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2637 idx = uinfo->value.enumerated.item;
2638 if (idx < out_jacks) {
2640 text = out_jack_texts[idx];
2642 text = "Headphone Out";
2646 unsigned int vref_caps = get_vref_caps(codec, nid);
2647 text = vref_texts[get_vref_idx(vref_caps, idx)];
2652 strcpy(uinfo->value.enumerated.name, text);
2656 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2658 int out_jacks = get_out_jack_num_items(codec, nid);
2659 int in_jacks = get_in_jack_num_items(codec, nid);
2660 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2663 if (val & PIN_OUT) {
2664 if (out_jacks > 1 && val == PIN_HP)
2666 } else if (val & PIN_IN) {
2669 unsigned int vref_caps = get_vref_caps(codec, nid);
2670 val &= AC_PINCTL_VREFEN;
2671 idx += cvt_from_vref_idx(vref_caps, val);
2677 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2678 struct snd_ctl_elem_value *ucontrol)
2680 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2681 hda_nid_t nid = kcontrol->private_value;
2682 ucontrol->value.enumerated.item[0] =
2683 get_cur_hp_mic_jack_mode(codec, nid);
2687 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2688 struct snd_ctl_elem_value *ucontrol)
2690 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2691 hda_nid_t nid = kcontrol->private_value;
2692 int out_jacks = get_out_jack_num_items(codec, nid);
2693 int in_jacks = get_in_jack_num_items(codec, nid);
2694 unsigned int val, oldval, idx;
2696 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2697 idx = ucontrol->value.enumerated.item[0];
2701 if (idx < out_jacks) {
2703 val = idx ? PIN_HP : PIN_OUT;
2709 unsigned int vref_caps = get_vref_caps(codec, nid);
2710 val = snd_hda_codec_get_pin_target(codec, nid);
2711 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2712 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
2714 val = snd_hda_get_default_vref(codec, nid);
2716 snd_hda_set_pin_ctl_cache(codec, nid, val);
2717 update_hp_automute_hook(codec);
2722 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2723 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2724 .info = hp_mic_jack_mode_info,
2725 .get = hp_mic_jack_mode_get,
2726 .put = hp_mic_jack_mode_put,
2729 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2731 struct hda_gen_spec *spec = codec->spec;
2732 struct snd_kcontrol_new *knew;
2734 if (get_out_jack_num_items(codec, pin) <= 1 &&
2735 get_in_jack_num_items(codec, pin) <= 1)
2736 return 0; /* no need */
2737 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2738 &hp_mic_jack_mode_enum);
2741 knew->private_value = pin;
2742 spec->hp_mic_jack_modes = 1;
2750 /* add the powersave loopback-list entry */
2751 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2753 struct hda_amp_list *list;
2755 list = snd_array_new(&spec->loopback_list);
2759 list->dir = HDA_INPUT;
2761 spec->loopback.amplist = spec->loopback_list.list;
2765 /* create input playback/capture controls for the given pin */
2766 static int new_analog_input(struct hda_codec *codec, int input_idx,
2767 hda_nid_t pin, const char *ctlname, int ctlidx,
2770 struct hda_gen_spec *spec = codec->spec;
2771 struct nid_path *path;
2775 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2776 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2777 return 0; /* no need for analog loopback */
2779 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
2782 print_nid_path("loopback", path);
2783 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
2785 idx = path->idx[path->depth - 1];
2786 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2787 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2788 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
2791 path->ctls[NID_PATH_VOL_CTL] = val;
2794 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2795 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2796 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
2799 path->ctls[NID_PATH_MUTE_CTL] = val;
2802 path->active = true;
2803 err = add_loopback_list(spec, mix_nid, idx);
2807 if (spec->mixer_nid != spec->mixer_merge_nid &&
2808 !spec->loopback_merge_path) {
2809 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2810 spec->mixer_merge_nid, 0);
2812 print_nid_path("loopback-merge", path);
2813 path->active = true;
2814 spec->loopback_merge_path =
2815 snd_hda_get_path_idx(codec, path);
2822 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2824 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2825 return (pincap & AC_PINCAP_IN) != 0;
2828 /* Parse the codec tree and retrieve ADCs */
2829 static int fill_adc_nids(struct hda_codec *codec)
2831 struct hda_gen_spec *spec = codec->spec;
2833 hda_nid_t *adc_nids = spec->adc_nids;
2834 int max_nums = ARRAY_SIZE(spec->adc_nids);
2837 nid = codec->start_nid;
2838 for (i = 0; i < codec->num_nodes; i++, nid++) {
2839 unsigned int caps = get_wcaps(codec, nid);
2840 int type = get_wcaps_type(caps);
2842 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2844 adc_nids[nums] = nid;
2845 if (++nums >= max_nums)
2848 spec->num_adc_nids = nums;
2850 /* copy the detected ADCs to all_adcs[] */
2851 spec->num_all_adcs = nums;
2852 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2857 /* filter out invalid adc_nids that don't give all active input pins;
2858 * if needed, check whether dynamic ADC-switching is available
2860 static int check_dyn_adc_switch(struct hda_codec *codec)
2862 struct hda_gen_spec *spec = codec->spec;
2863 struct hda_input_mux *imux = &spec->input_mux;
2864 unsigned int ok_bits;
2869 for (n = 0; n < spec->num_adc_nids; n++) {
2870 for (i = 0; i < imux->num_items; i++) {
2871 if (!spec->input_paths[i][n])
2874 if (i >= imux->num_items) {
2875 ok_bits |= (1 << n);
2881 /* check whether ADC-switch is possible */
2882 for (i = 0; i < imux->num_items; i++) {
2883 for (n = 0; n < spec->num_adc_nids; n++) {
2884 if (spec->input_paths[i][n]) {
2885 spec->dyn_adc_idx[i] = n;
2891 snd_printdd("hda-codec: enabling ADC switching\n");
2892 spec->dyn_adc_switch = 1;
2893 } else if (nums != spec->num_adc_nids) {
2894 /* shrink the invalid adcs and input paths */
2896 for (n = 0; n < spec->num_adc_nids; n++) {
2897 if (!(ok_bits & (1 << n)))
2900 spec->adc_nids[nums] = spec->adc_nids[n];
2901 for (i = 0; i < imux->num_items; i++) {
2902 invalidate_nid_path(codec,
2903 spec->input_paths[i][nums]);
2904 spec->input_paths[i][nums] =
2905 spec->input_paths[i][n];
2910 spec->num_adc_nids = nums;
2913 if (imux->num_items == 1 ||
2914 (imux->num_items == 2 && spec->hp_mic)) {
2915 snd_printdd("hda-codec: reducing to a single ADC\n");
2916 spec->num_adc_nids = 1; /* reduce to a single ADC */
2919 /* single index for individual volumes ctls */
2920 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2921 spec->num_adc_nids = 1;
2926 /* parse capture source paths from the given pin and create imux items */
2927 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2928 int cfg_idx, int num_adcs,
2929 const char *label, int anchor)
2931 struct hda_gen_spec *spec = codec->spec;
2932 struct hda_input_mux *imux = &spec->input_mux;
2933 int imux_idx = imux->num_items;
2934 bool imux_added = false;
2937 for (c = 0; c < num_adcs; c++) {
2938 struct nid_path *path;
2939 hda_nid_t adc = spec->adc_nids[c];
2941 if (!is_reachable_path(codec, pin, adc))
2943 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2946 print_nid_path("input", path);
2947 spec->input_paths[imux_idx][c] =
2948 snd_hda_get_path_idx(codec, path);
2951 if (spec->hp_mic_pin == pin)
2952 spec->hp_mic_mux_idx = imux->num_items;
2953 spec->imux_pins[imux->num_items] = pin;
2954 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
2963 * create playback/capture controls for input pins
2966 /* fill the label for each input at first */
2967 static int fill_input_pin_labels(struct hda_codec *codec)
2969 struct hda_gen_spec *spec = codec->spec;
2970 const struct auto_pin_cfg *cfg = &spec->autocfg;
2973 for (i = 0; i < cfg->num_inputs; i++) {
2974 hda_nid_t pin = cfg->inputs[i].pin;
2978 if (!is_input_pin(codec, pin))
2981 label = hda_get_autocfg_input_label(codec, cfg, i);
2983 for (j = i - 1; j >= 0; j--) {
2984 if (spec->input_labels[j] &&
2985 !strcmp(spec->input_labels[j], label)) {
2986 idx = spec->input_label_idxs[j] + 1;
2991 spec->input_labels[i] = label;
2992 spec->input_label_idxs[i] = idx;
2998 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3000 static int create_input_ctls(struct hda_codec *codec)
3002 struct hda_gen_spec *spec = codec->spec;
3003 const struct auto_pin_cfg *cfg = &spec->autocfg;
3004 hda_nid_t mixer = spec->mixer_nid;
3009 num_adcs = fill_adc_nids(codec);
3013 err = fill_input_pin_labels(codec);
3017 for (i = 0; i < cfg->num_inputs; i++) {
3020 pin = cfg->inputs[i].pin;
3021 if (!is_input_pin(codec, pin))
3025 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3026 val |= snd_hda_get_default_vref(codec, pin);
3027 if (pin != spec->hp_mic_pin)
3028 set_pin_target(codec, pin, val, false);
3031 if (is_reachable_path(codec, pin, mixer)) {
3032 err = new_analog_input(codec, i, pin,
3033 spec->input_labels[i],
3034 spec->input_label_idxs[i],
3041 err = parse_capture_source(codec, pin, i, num_adcs,
3042 spec->input_labels[i], -mixer);
3046 if (spec->add_jack_modes) {
3047 err = create_in_jack_mode(codec, pin);
3053 if (mixer && spec->add_stereo_mix_input) {
3054 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3068 /* get the input path specified by the given adc and imux indices */
3069 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3071 struct hda_gen_spec *spec = codec->spec;
3072 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3076 if (spec->dyn_adc_switch)
3077 adc_idx = spec->dyn_adc_idx[imux_idx];
3078 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3082 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3085 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3088 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3089 struct snd_ctl_elem_info *uinfo)
3091 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3092 struct hda_gen_spec *spec = codec->spec;
3093 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3096 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3097 struct snd_ctl_elem_value *ucontrol)
3099 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3100 struct hda_gen_spec *spec = codec->spec;
3101 /* the ctls are created at once with multiple counts */
3102 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3104 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3108 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3109 struct snd_ctl_elem_value *ucontrol)
3111 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3112 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3113 return mux_select(codec, adc_idx,
3114 ucontrol->value.enumerated.item[0]);
3117 static const struct snd_kcontrol_new cap_src_temp = {
3118 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3119 .name = "Input Source",
3120 .info = mux_enum_info,
3121 .get = mux_enum_get,
3122 .put = mux_enum_put,
3126 * capture volume and capture switch ctls
3129 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3130 struct snd_ctl_elem_value *ucontrol);
3132 /* call the given amp update function for all amps in the imux list at once */
3133 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3134 struct snd_ctl_elem_value *ucontrol,
3135 put_call_t func, int type)
3137 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3138 struct hda_gen_spec *spec = codec->spec;
3139 const struct hda_input_mux *imux;
3140 struct nid_path *path;
3141 int i, adc_idx, err = 0;
3143 imux = &spec->input_mux;
3144 adc_idx = kcontrol->id.index;
3145 mutex_lock(&codec->control_mutex);
3146 /* we use the cache-only update at first since multiple input paths
3147 * may shared the same amp; by updating only caches, the redundant
3148 * writes to hardware can be reduced.
3150 codec->cached_write = 1;
3151 for (i = 0; i < imux->num_items; i++) {
3152 path = get_input_path(codec, adc_idx, i);
3153 if (!path || !path->ctls[type])
3155 kcontrol->private_value = path->ctls[type];
3156 err = func(kcontrol, ucontrol);
3161 codec->cached_write = 0;
3162 mutex_unlock(&codec->control_mutex);
3163 snd_hda_codec_flush_cache(codec); /* flush the updates */
3164 if (err >= 0 && spec->cap_sync_hook)
3165 spec->cap_sync_hook(codec, ucontrol);
3169 /* capture volume ctl callbacks */
3170 #define cap_vol_info snd_hda_mixer_amp_volume_info
3171 #define cap_vol_get snd_hda_mixer_amp_volume_get
3172 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3174 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3175 struct snd_ctl_elem_value *ucontrol)
3177 return cap_put_caller(kcontrol, ucontrol,
3178 snd_hda_mixer_amp_volume_put,
3182 static const struct snd_kcontrol_new cap_vol_temp = {
3183 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3184 .name = "Capture Volume",
3185 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3186 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3187 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3188 .info = cap_vol_info,
3191 .tlv = { .c = cap_vol_tlv },
3194 /* capture switch ctl callbacks */
3195 #define cap_sw_info snd_ctl_boolean_stereo_info
3196 #define cap_sw_get snd_hda_mixer_amp_switch_get
3198 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3199 struct snd_ctl_elem_value *ucontrol)
3201 return cap_put_caller(kcontrol, ucontrol,
3202 snd_hda_mixer_amp_switch_put,
3206 static const struct snd_kcontrol_new cap_sw_temp = {
3207 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3208 .name = "Capture Switch",
3209 .info = cap_sw_info,
3214 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3219 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3220 for (depth = 0; depth < 3; depth++) {
3221 if (depth >= path->depth)
3223 i = path->depth - depth - 1;
3224 nid = path->path[i];
3225 if (!path->ctls[NID_PATH_VOL_CTL]) {
3226 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3227 path->ctls[NID_PATH_VOL_CTL] =
3228 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3229 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3230 int idx = path->idx[i];
3231 if (!depth && codec->single_adc_amp)
3233 path->ctls[NID_PATH_VOL_CTL] =
3234 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3237 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3238 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3239 path->ctls[NID_PATH_MUTE_CTL] =
3240 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3241 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3242 int idx = path->idx[i];
3243 if (!depth && codec->single_adc_amp)
3245 path->ctls[NID_PATH_MUTE_CTL] =
3246 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3253 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3255 struct hda_gen_spec *spec = codec->spec;
3256 struct auto_pin_cfg *cfg = &spec->autocfg;
3260 if (!spec->inv_dmic_split)
3262 for (i = 0; i < cfg->num_inputs; i++) {
3263 if (cfg->inputs[i].pin != nid)
3265 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3267 val = snd_hda_codec_get_pincfg(codec, nid);
3268 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3273 /* capture switch put callback for a single control with hook call */
3274 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3275 struct snd_ctl_elem_value *ucontrol)
3277 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3278 struct hda_gen_spec *spec = codec->spec;
3281 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3285 if (spec->cap_sync_hook)
3286 spec->cap_sync_hook(codec, ucontrol);
3291 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3292 int idx, bool is_switch, unsigned int ctl,
3295 struct hda_gen_spec *spec = codec->spec;
3297 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3298 const char *sfx = is_switch ? "Switch" : "Volume";
3299 unsigned int chs = inv_dmic ? 1 : 3;
3300 struct snd_kcontrol_new *knew;
3306 snprintf(tmpname, sizeof(tmpname),
3307 "%s Capture %s", label, sfx);
3309 snprintf(tmpname, sizeof(tmpname),
3311 knew = add_control(spec, type, tmpname, idx,
3312 amp_val_replace_channels(ctl, chs));
3316 knew->put = cap_single_sw_put;
3320 /* Make independent right kcontrol */
3322 snprintf(tmpname, sizeof(tmpname),
3323 "Inverted %s Capture %s", label, sfx);
3325 snprintf(tmpname, sizeof(tmpname),
3326 "Inverted Capture %s", sfx);
3327 knew = add_control(spec, type, tmpname, idx,
3328 amp_val_replace_channels(ctl, 2));
3332 knew->put = cap_single_sw_put;
3336 /* create single (and simple) capture volume and switch controls */
3337 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3338 unsigned int vol_ctl, unsigned int sw_ctl,
3342 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3345 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3351 /* create bound capture volume and switch controls */
3352 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3353 unsigned int vol_ctl, unsigned int sw_ctl)
3355 struct hda_gen_spec *spec = codec->spec;
3356 struct snd_kcontrol_new *knew;
3359 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3363 knew->private_value = vol_ctl;
3364 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3367 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3371 knew->private_value = sw_ctl;
3372 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3377 /* return the vol ctl when used first in the imux list */
3378 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3380 struct nid_path *path;
3384 path = get_input_path(codec, 0, idx);
3387 ctl = path->ctls[type];
3390 for (i = 0; i < idx - 1; i++) {
3391 path = get_input_path(codec, 0, i);
3392 if (path && path->ctls[type] == ctl)
3398 /* create individual capture volume and switch controls per input */
3399 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3401 struct hda_gen_spec *spec = codec->spec;
3402 struct hda_input_mux *imux = &spec->input_mux;
3405 for (i = 0; i < imux->num_items; i++) {
3409 idx = imux->items[i].index;
3410 if (idx >= spec->autocfg.num_inputs)
3412 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3414 for (type = 0; type < 2; type++) {
3415 err = add_single_cap_ctl(codec,
3416 spec->input_labels[idx],
3417 spec->input_label_idxs[idx],
3419 get_first_cap_ctl(codec, i, type),
3428 static int create_capture_mixers(struct hda_codec *codec)
3430 struct hda_gen_spec *spec = codec->spec;
3431 struct hda_input_mux *imux = &spec->input_mux;
3432 int i, n, nums, err;
3434 if (spec->dyn_adc_switch)
3437 nums = spec->num_adc_nids;
3439 if (!spec->auto_mic && imux->num_items > 1) {
3440 struct snd_kcontrol_new *knew;
3442 name = nums > 1 ? "Input Source" : "Capture Source";
3443 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3449 for (n = 0; n < nums; n++) {
3451 bool multi_cap_vol = spec->multi_cap_vol;
3452 bool inv_dmic = false;
3456 for (i = 0; i < imux->num_items; i++) {
3457 struct nid_path *path;
3458 path = get_input_path(codec, n, i);
3461 parse_capvol_in_path(codec, path);
3463 vol = path->ctls[NID_PATH_VOL_CTL];
3464 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3466 if (!same_amp_caps(codec, vol,
3467 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3468 multi_cap_vol = true;
3471 sw = path->ctls[NID_PATH_MUTE_CTL];
3472 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3474 if (!same_amp_caps(codec, sw,
3475 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3476 multi_cap_vol = true;
3478 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3483 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3485 else if (!multi_cap_vol)
3486 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3488 err = create_multi_cap_vol_ctl(codec);
3497 * add mic boosts if needed
3500 /* check whether the given amp is feasible as a boost volume */
3501 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3506 if (!nid_has_volume(codec, nid, dir) ||
3507 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3508 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3511 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3512 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3518 /* look for a boost amp in a widget close to the pin */
3519 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3520 struct nid_path *path)
3522 unsigned int val = 0;
3526 for (depth = 0; depth < 3; depth++) {
3527 if (depth >= path->depth - 1)
3529 nid = path->path[depth];
3530 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3531 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3533 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3534 path->idx[depth])) {
3535 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3544 static int parse_mic_boost(struct hda_codec *codec)
3546 struct hda_gen_spec *spec = codec->spec;
3547 struct auto_pin_cfg *cfg = &spec->autocfg;
3548 struct hda_input_mux *imux = &spec->input_mux;
3551 if (!spec->num_adc_nids)
3554 for (i = 0; i < imux->num_items; i++) {
3555 struct nid_path *path;
3558 char boost_label[44];
3560 idx = imux->items[i].index;
3561 if (idx >= imux->num_items)
3564 /* check only line-in and mic pins */
3565 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3568 path = get_input_path(codec, 0, i);
3572 val = look_for_boost_amp(codec, path);
3576 /* create a boost control */
3577 snprintf(boost_label, sizeof(boost_label),
3578 "%s Boost Volume", spec->input_labels[idx]);
3579 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3580 spec->input_label_idxs[idx], val))
3583 path->ctls[NID_PATH_BOOST_CTL] = val;
3589 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3591 static void parse_digital(struct hda_codec *codec)
3593 struct hda_gen_spec *spec = codec->spec;
3594 struct nid_path *path;
3596 hda_nid_t dig_nid, pin;
3598 /* support multiple SPDIFs; the secondary is set up as a slave */
3600 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3601 pin = spec->autocfg.dig_out_pins[i];
3602 dig_nid = look_for_dac(codec, pin, true);
3605 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3608 print_nid_path("digout", path);
3609 path->active = true;
3610 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3611 set_pin_target(codec, pin, PIN_OUT, false);
3613 spec->multiout.dig_out_nid = dig_nid;
3614 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3616 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3617 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3619 spec->slave_dig_outs[nums - 1] = dig_nid;
3624 if (spec->autocfg.dig_in_pin) {
3625 pin = spec->autocfg.dig_in_pin;
3626 dig_nid = codec->start_nid;
3627 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3628 unsigned int wcaps = get_wcaps(codec, dig_nid);
3629 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3631 if (!(wcaps & AC_WCAP_DIGITAL))
3633 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3635 print_nid_path("digin", path);
3636 path->active = true;
3637 spec->dig_in_nid = dig_nid;
3638 spec->digin_path = snd_hda_get_path_idx(codec, path);
3639 set_pin_target(codec, pin, PIN_IN, false);
3648 * input MUX handling
3651 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3653 /* select the given imux item; either unmute exclusively or select the route */
3654 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3657 struct hda_gen_spec *spec = codec->spec;
3658 const struct hda_input_mux *imux;
3659 struct nid_path *old_path, *path;
3661 imux = &spec->input_mux;
3662 if (!imux->num_items)
3665 if (idx >= imux->num_items)
3666 idx = imux->num_items - 1;
3667 if (spec->cur_mux[adc_idx] == idx)
3670 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3673 if (old_path->active)
3674 snd_hda_activate_path(codec, old_path, false, false);
3676 spec->cur_mux[adc_idx] = idx;
3679 update_hp_mic(codec, adc_idx, false);
3681 if (spec->dyn_adc_switch)
3682 dyn_adc_pcm_resetup(codec, idx);
3684 path = get_input_path(codec, adc_idx, idx);
3689 snd_hda_activate_path(codec, path, true, false);
3690 if (spec->cap_sync_hook)
3691 spec->cap_sync_hook(codec, NULL);
3692 path_power_down_sync(codec, old_path);
3698 * Jack detections for HP auto-mute and mic-switch
3701 /* check each pin in the given array; returns true if any of them is plugged */
3702 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3706 for (i = 0; i < num_pins; i++) {
3707 hda_nid_t nid = pins[i];
3710 /* don't detect pins retasked as inputs */
3711 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3713 present |= snd_hda_jack_detect(codec, nid);
3718 /* standard HP/line-out auto-mute helper */
3719 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
3722 struct hda_gen_spec *spec = codec->spec;
3725 for (i = 0; i < num_pins; i++) {
3726 hda_nid_t nid = pins[i];
3727 unsigned int val, oldval;
3730 oldval = snd_hda_codec_get_pin_target(codec, nid);
3731 if (oldval & PIN_IN)
3732 continue; /* no mute for inputs */
3733 /* don't reset VREF value in case it's controlling
3734 * the amp (see alc861_fixup_asus_amp_vref_0f())
3736 if (spec->keep_vref_in_automute)
3737 val = oldval & ~PIN_HP;
3742 /* here we call update_pin_ctl() so that the pinctl is changed
3743 * without changing the pinctl target value;
3744 * the original target value will be still referred at the
3745 * init / resume again
3747 update_pin_ctl(codec, nid, val);
3748 set_pin_eapd(codec, nid, !mute);
3752 /* Toggle outputs muting */
3753 void snd_hda_gen_update_outputs(struct hda_codec *codec)
3755 struct hda_gen_spec *spec = codec->spec;
3758 /* Control HP pins/amps depending on master_mute state;
3759 * in general, HP pins/amps control should be enabled in all cases,
3760 * but currently set only for master_mute, just to be safe
3762 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3763 spec->autocfg.hp_pins, spec->master_mute);
3765 if (!spec->automute_speaker)
3768 on = spec->hp_jack_present | spec->line_jack_present;
3769 on |= spec->master_mute;
3770 spec->speaker_muted = on;
3771 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
3772 spec->autocfg.speaker_pins, on);
3774 /* toggle line-out mutes if needed, too */
3775 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3776 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3777 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3779 if (!spec->automute_lo)
3782 on = spec->hp_jack_present;
3783 on |= spec->master_mute;
3784 spec->line_out_muted = on;
3785 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3786 spec->autocfg.line_out_pins, on);
3788 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
3790 static void call_update_outputs(struct hda_codec *codec)
3792 struct hda_gen_spec *spec = codec->spec;
3793 if (spec->automute_hook)
3794 spec->automute_hook(codec);
3796 snd_hda_gen_update_outputs(codec);
3799 /* standard HP-automute helper */
3800 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3802 struct hda_gen_spec *spec = codec->spec;
3803 hda_nid_t *pins = spec->autocfg.hp_pins;
3804 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
3806 /* No detection for the first HP jack during indep-HP mode */
3807 if (spec->indep_hp_enabled) {
3812 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
3813 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3815 call_update_outputs(codec);
3817 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
3819 /* standard line-out-automute helper */
3820 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3822 struct hda_gen_spec *spec = codec->spec;
3824 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3826 /* check LO jack only when it's different from HP */
3827 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3830 spec->line_jack_present =
3831 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3832 spec->autocfg.line_out_pins);
3833 if (!spec->automute_speaker || !spec->detect_lo)
3835 call_update_outputs(codec);
3837 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
3839 /* standard mic auto-switch helper */
3840 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
3842 struct hda_gen_spec *spec = codec->spec;
3845 if (!spec->auto_mic)
3848 for (i = spec->am_num_entries - 1; i > 0; i--) {
3849 hda_nid_t pin = spec->am_entry[i].pin;
3850 /* don't detect pins retasked as outputs */
3851 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3853 if (snd_hda_jack_detect(codec, pin)) {
3854 mux_select(codec, 0, spec->am_entry[i].idx);
3858 mux_select(codec, 0, spec->am_entry[0].idx);
3860 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
3862 /* update jack retasking */
3863 static void update_automute_all(struct hda_codec *codec)
3865 struct hda_gen_spec *spec = codec->spec;
3867 update_hp_automute_hook(codec);
3868 if (spec->line_automute_hook)
3869 spec->line_automute_hook(codec, NULL);
3871 snd_hda_gen_line_automute(codec, NULL);
3872 if (spec->mic_autoswitch_hook)
3873 spec->mic_autoswitch_hook(codec, NULL);
3875 snd_hda_gen_mic_autoswitch(codec, NULL);
3879 * Auto-Mute mode mixer enum support
3881 static int automute_mode_info(struct snd_kcontrol *kcontrol,
3882 struct snd_ctl_elem_info *uinfo)
3884 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3885 struct hda_gen_spec *spec = codec->spec;
3886 static const char * const texts3[] = {
3887 "Disabled", "Speaker Only", "Line Out+Speaker"
3890 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3891 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3892 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3895 static int automute_mode_get(struct snd_kcontrol *kcontrol,
3896 struct snd_ctl_elem_value *ucontrol)
3898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3899 struct hda_gen_spec *spec = codec->spec;
3900 unsigned int val = 0;
3901 if (spec->automute_speaker)
3903 if (spec->automute_lo)
3906 ucontrol->value.enumerated.item[0] = val;
3910 static int automute_mode_put(struct snd_kcontrol *kcontrol,
3911 struct snd_ctl_elem_value *ucontrol)
3913 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3914 struct hda_gen_spec *spec = codec->spec;
3916 switch (ucontrol->value.enumerated.item[0]) {
3918 if (!spec->automute_speaker && !spec->automute_lo)
3920 spec->automute_speaker = 0;
3921 spec->automute_lo = 0;
3924 if (spec->automute_speaker_possible) {
3925 if (!spec->automute_lo && spec->automute_speaker)
3927 spec->automute_speaker = 1;
3928 spec->automute_lo = 0;
3929 } else if (spec->automute_lo_possible) {
3930 if (spec->automute_lo)
3932 spec->automute_lo = 1;
3937 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3939 if (spec->automute_speaker && spec->automute_lo)
3941 spec->automute_speaker = 1;
3942 spec->automute_lo = 1;
3947 call_update_outputs(codec);
3951 static const struct snd_kcontrol_new automute_mode_enum = {
3952 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3953 .name = "Auto-Mute Mode",
3954 .info = automute_mode_info,
3955 .get = automute_mode_get,
3956 .put = automute_mode_put,
3959 static int add_automute_mode_enum(struct hda_codec *codec)
3961 struct hda_gen_spec *spec = codec->spec;
3963 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
3969 * Check the availability of HP/line-out auto-mute;
3970 * Set up appropriately if really supported
3972 static int check_auto_mute_availability(struct hda_codec *codec)
3974 struct hda_gen_spec *spec = codec->spec;
3975 struct auto_pin_cfg *cfg = &spec->autocfg;
3979 if (spec->suppress_auto_mute)
3982 if (cfg->hp_pins[0])
3984 if (cfg->line_out_pins[0])
3986 if (cfg->speaker_pins[0])
3988 if (present < 2) /* need two different output types */
3991 if (!cfg->speaker_pins[0] &&
3992 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3993 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3994 sizeof(cfg->speaker_pins));
3995 cfg->speaker_outs = cfg->line_outs;
3998 if (!cfg->hp_pins[0] &&
3999 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4000 memcpy(cfg->hp_pins, cfg->line_out_pins,
4001 sizeof(cfg->hp_pins));
4002 cfg->hp_outs = cfg->line_outs;
4005 for (i = 0; i < cfg->hp_outs; i++) {
4006 hda_nid_t nid = cfg->hp_pins[i];
4007 if (!is_jack_detectable(codec, nid))
4009 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
4011 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
4012 spec->hp_automute_hook ?
4013 spec->hp_automute_hook :
4014 snd_hda_gen_hp_automute);
4015 spec->detect_hp = 1;
4018 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4019 if (cfg->speaker_outs)
4020 for (i = 0; i < cfg->line_outs; i++) {
4021 hda_nid_t nid = cfg->line_out_pins[i];
4022 if (!is_jack_detectable(codec, nid))
4024 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
4025 snd_hda_jack_detect_enable_callback(codec, nid,
4026 HDA_GEN_FRONT_EVENT,
4027 spec->line_automute_hook ?
4028 spec->line_automute_hook :
4029 snd_hda_gen_line_automute);
4030 spec->detect_lo = 1;
4032 spec->automute_lo_possible = spec->detect_hp;
4035 spec->automute_speaker_possible = cfg->speaker_outs &&
4036 (spec->detect_hp || spec->detect_lo);
4038 spec->automute_lo = spec->automute_lo_possible;
4039 spec->automute_speaker = spec->automute_speaker_possible;
4041 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4042 /* create a control for automute mode */
4043 err = add_automute_mode_enum(codec);
4050 /* check whether all auto-mic pins are valid; setup indices if OK */
4051 static bool auto_mic_check_imux(struct hda_codec *codec)
4053 struct hda_gen_spec *spec = codec->spec;
4054 const struct hda_input_mux *imux;
4057 imux = &spec->input_mux;
4058 for (i = 0; i < spec->am_num_entries; i++) {
4059 spec->am_entry[i].idx =
4060 find_idx_in_nid_list(spec->am_entry[i].pin,
4061 spec->imux_pins, imux->num_items);
4062 if (spec->am_entry[i].idx < 0)
4063 return false; /* no corresponding imux */
4066 /* we don't need the jack detection for the first pin */
4067 for (i = 1; i < spec->am_num_entries; i++)
4068 snd_hda_jack_detect_enable_callback(codec,
4069 spec->am_entry[i].pin,
4071 spec->mic_autoswitch_hook ?
4072 spec->mic_autoswitch_hook :
4073 snd_hda_gen_mic_autoswitch);
4077 static int compare_attr(const void *ap, const void *bp)
4079 const struct automic_entry *a = ap;
4080 const struct automic_entry *b = bp;
4081 return (int)(a->attr - b->attr);
4085 * Check the availability of auto-mic switch;
4086 * Set up if really supported
4088 static int check_auto_mic_availability(struct hda_codec *codec)
4090 struct hda_gen_spec *spec = codec->spec;
4091 struct auto_pin_cfg *cfg = &spec->autocfg;
4095 if (spec->suppress_auto_mic)
4100 for (i = 0; i < cfg->num_inputs; i++) {
4101 hda_nid_t nid = cfg->inputs[i].pin;
4103 attr = snd_hda_codec_get_pincfg(codec, nid);
4104 attr = snd_hda_get_input_pin_attr(attr);
4105 if (types & (1 << attr))
4106 return 0; /* already occupied */
4108 case INPUT_PIN_ATTR_INT:
4109 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4110 return 0; /* invalid type */
4112 case INPUT_PIN_ATTR_UNUSED:
4113 return 0; /* invalid entry */
4115 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4116 return 0; /* invalid type */
4117 if (!spec->line_in_auto_switch &&
4118 cfg->inputs[i].type != AUTO_PIN_MIC)
4119 return 0; /* only mic is allowed */
4120 if (!is_jack_detectable(codec, nid))
4121 return 0; /* no unsol support */
4124 if (num_pins >= MAX_AUTO_MIC_PINS)
4126 types |= (1 << attr);
4127 spec->am_entry[num_pins].pin = nid;
4128 spec->am_entry[num_pins].attr = attr;
4135 spec->am_num_entries = num_pins;
4136 /* sort the am_entry in the order of attr so that the pin with a
4137 * higher attr will be selected when the jack is plugged.
4139 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4140 compare_attr, NULL);
4142 if (!auto_mic_check_imux(codec))
4146 spec->num_adc_nids = 1;
4147 spec->cur_mux[0] = spec->am_entry[0].idx;
4148 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4149 spec->am_entry[0].pin,
4150 spec->am_entry[1].pin,
4151 spec->am_entry[2].pin);
4156 /* power_filter hook; make inactive widgets into power down */
4157 static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4159 unsigned int power_state)
4161 if (power_state != AC_PWRST_D0)
4163 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4165 if (is_active_nid_for_any(codec, nid))
4172 * Parse the given BIOS configuration and set up the hda_gen_spec
4174 * return 1 if successful, 0 if the proper config is not found,
4175 * or a negative error code
4177 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4178 struct auto_pin_cfg *cfg)
4180 struct hda_gen_spec *spec = codec->spec;
4183 parse_user_hints(codec);
4185 if (spec->mixer_nid && !spec->mixer_merge_nid)
4186 spec->mixer_merge_nid = spec->mixer_nid;
4188 if (cfg != &spec->autocfg) {
4189 spec->autocfg = *cfg;
4190 cfg = &spec->autocfg;
4193 if (!spec->main_out_badness)
4194 spec->main_out_badness = &hda_main_out_badness;
4195 if (!spec->extra_out_badness)
4196 spec->extra_out_badness = &hda_extra_out_badness;
4198 fill_all_dac_nids(codec);
4200 if (!cfg->line_outs) {
4201 if (cfg->dig_outs || cfg->dig_in_pin) {
4202 spec->multiout.max_channels = 2;
4203 spec->no_analog = 1;
4206 return 0; /* can't find valid BIOS pin config */
4209 if (!spec->no_primary_hp &&
4210 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4211 cfg->line_outs <= cfg->hp_outs) {
4212 /* use HP as primary out */
4213 cfg->speaker_outs = cfg->line_outs;
4214 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4215 sizeof(cfg->speaker_pins));
4216 cfg->line_outs = cfg->hp_outs;
4217 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4219 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4220 cfg->line_out_type = AUTO_PIN_HP_OUT;
4223 err = parse_output_paths(codec);
4226 err = create_multi_channel_mode(codec);
4229 err = create_multi_out_ctls(codec, cfg);
4232 err = create_hp_out_ctls(codec);
4235 err = create_speaker_out_ctls(codec);
4238 err = create_indep_hp_ctls(codec);
4241 err = create_loopback_mixing_ctl(codec);
4244 err = create_hp_mic(codec);
4247 err = create_input_ctls(codec);
4251 spec->const_channel_count = spec->ext_channel_count;
4252 /* check the multiple speaker and headphone pins */
4253 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4254 spec->const_channel_count = max(spec->const_channel_count,
4255 cfg->speaker_outs * 2);
4256 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4257 spec->const_channel_count = max(spec->const_channel_count,
4259 spec->multiout.max_channels = max(spec->ext_channel_count,
4260 spec->const_channel_count);
4262 err = check_auto_mute_availability(codec);
4266 err = check_dyn_adc_switch(codec);
4270 err = check_auto_mic_availability(codec);
4274 err = create_capture_mixers(codec);
4278 err = parse_mic_boost(codec);
4282 if (spec->add_jack_modes) {
4283 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4284 err = create_out_jack_modes(codec, cfg->line_outs,
4285 cfg->line_out_pins);
4289 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4290 err = create_out_jack_modes(codec, cfg->hp_outs,
4298 parse_digital(codec);
4300 if (spec->power_down_unused)
4301 codec->power_filter = snd_hda_gen_path_power_filter;
4303 if (!spec->no_analog && spec->beep_nid) {
4304 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4311 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
4315 * Build control elements
4318 /* slave controls for virtual master */
4319 static const char * const slave_pfxs[] = {
4320 "Front", "Surround", "Center", "LFE", "Side",
4321 "Headphone", "Speaker", "Mono", "Line Out",
4322 "CLFE", "Bass Speaker", "PCM",
4323 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4324 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4329 int snd_hda_gen_build_controls(struct hda_codec *codec)
4331 struct hda_gen_spec *spec = codec->spec;
4334 if (spec->kctls.used) {
4335 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4340 if (spec->multiout.dig_out_nid) {
4341 err = snd_hda_create_dig_out_ctls(codec,
4342 spec->multiout.dig_out_nid,
4343 spec->multiout.dig_out_nid,
4344 spec->pcm_rec[1].pcm_type);
4347 if (!spec->no_analog) {
4348 err = snd_hda_create_spdif_share_sw(codec,
4352 spec->multiout.share_spdif = 1;
4355 if (spec->dig_in_nid) {
4356 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4361 /* if we have no master control, let's create it */
4362 if (!spec->no_analog &&
4363 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
4364 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
4365 spec->vmaster_tlv, slave_pfxs,
4370 if (!spec->no_analog &&
4371 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4372 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4375 true, &spec->vmaster_mute.sw_kctl);
4378 if (spec->vmaster_mute.hook)
4379 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4380 spec->vmaster_mute_enum);
4383 free_kctls(spec); /* no longer needed */
4385 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4391 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4398 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4399 struct hda_codec *codec,
4400 struct snd_pcm_substream *substream,
4403 struct hda_gen_spec *spec = codec->spec;
4404 if (spec->pcm_playback_hook)
4405 spec->pcm_playback_hook(hinfo, codec, substream, action);
4408 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4409 struct hda_codec *codec,
4410 struct snd_pcm_substream *substream,
4413 struct hda_gen_spec *spec = codec->spec;
4414 if (spec->pcm_capture_hook)
4415 spec->pcm_capture_hook(hinfo, codec, substream, action);
4419 * Analog playback callbacks
4421 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4422 struct hda_codec *codec,
4423 struct snd_pcm_substream *substream)
4425 struct hda_gen_spec *spec = codec->spec;
4428 mutex_lock(&spec->pcm_mutex);
4429 err = snd_hda_multi_out_analog_open(codec,
4430 &spec->multiout, substream,
4433 spec->active_streams |= 1 << STREAM_MULTI_OUT;
4434 call_pcm_playback_hook(hinfo, codec, substream,
4435 HDA_GEN_PCM_ACT_OPEN);
4437 mutex_unlock(&spec->pcm_mutex);
4441 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4442 struct hda_codec *codec,
4443 unsigned int stream_tag,
4444 unsigned int format,
4445 struct snd_pcm_substream *substream)
4447 struct hda_gen_spec *spec = codec->spec;
4450 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4451 stream_tag, format, substream);
4453 call_pcm_playback_hook(hinfo, codec, substream,
4454 HDA_GEN_PCM_ACT_PREPARE);
4458 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4459 struct hda_codec *codec,
4460 struct snd_pcm_substream *substream)
4462 struct hda_gen_spec *spec = codec->spec;
4465 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4467 call_pcm_playback_hook(hinfo, codec, substream,
4468 HDA_GEN_PCM_ACT_CLEANUP);
4472 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4473 struct hda_codec *codec,
4474 struct snd_pcm_substream *substream)
4476 struct hda_gen_spec *spec = codec->spec;
4477 mutex_lock(&spec->pcm_mutex);
4478 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
4479 call_pcm_playback_hook(hinfo, codec, substream,
4480 HDA_GEN_PCM_ACT_CLOSE);
4481 mutex_unlock(&spec->pcm_mutex);
4485 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4486 struct hda_codec *codec,
4487 struct snd_pcm_substream *substream)
4489 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4493 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4494 struct hda_codec *codec,
4495 unsigned int stream_tag,
4496 unsigned int format,
4497 struct snd_pcm_substream *substream)
4499 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4500 call_pcm_capture_hook(hinfo, codec, substream,
4501 HDA_GEN_PCM_ACT_PREPARE);
4505 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4506 struct hda_codec *codec,
4507 struct snd_pcm_substream *substream)
4509 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4510 call_pcm_capture_hook(hinfo, codec, substream,
4511 HDA_GEN_PCM_ACT_CLEANUP);
4515 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4516 struct hda_codec *codec,
4517 struct snd_pcm_substream *substream)
4519 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4523 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4524 struct hda_codec *codec,
4525 struct snd_pcm_substream *substream)
4527 struct hda_gen_spec *spec = codec->spec;
4530 mutex_lock(&spec->pcm_mutex);
4531 if (!spec->indep_hp_enabled)
4534 spec->active_streams |= 1 << STREAM_INDEP_HP;
4535 call_pcm_playback_hook(hinfo, codec, substream,
4536 HDA_GEN_PCM_ACT_OPEN);
4537 mutex_unlock(&spec->pcm_mutex);
4541 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4542 struct hda_codec *codec,
4543 struct snd_pcm_substream *substream)
4545 struct hda_gen_spec *spec = codec->spec;
4546 mutex_lock(&spec->pcm_mutex);
4547 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
4548 call_pcm_playback_hook(hinfo, codec, substream,
4549 HDA_GEN_PCM_ACT_CLOSE);
4550 mutex_unlock(&spec->pcm_mutex);
4554 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4555 struct hda_codec *codec,
4556 unsigned int stream_tag,
4557 unsigned int format,
4558 struct snd_pcm_substream *substream)
4560 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4561 call_pcm_playback_hook(hinfo, codec, substream,
4562 HDA_GEN_PCM_ACT_PREPARE);
4566 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4567 struct hda_codec *codec,
4568 struct snd_pcm_substream *substream)
4570 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4571 call_pcm_playback_hook(hinfo, codec, substream,
4572 HDA_GEN_PCM_ACT_CLEANUP);
4579 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4580 struct hda_codec *codec,
4581 struct snd_pcm_substream *substream)
4583 struct hda_gen_spec *spec = codec->spec;
4584 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4587 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4588 struct hda_codec *codec,
4589 unsigned int stream_tag,
4590 unsigned int format,
4591 struct snd_pcm_substream *substream)
4593 struct hda_gen_spec *spec = codec->spec;
4594 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4595 stream_tag, format, substream);
4598 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4599 struct hda_codec *codec,
4600 struct snd_pcm_substream *substream)
4602 struct hda_gen_spec *spec = codec->spec;
4603 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4606 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4607 struct hda_codec *codec,
4608 struct snd_pcm_substream *substream)
4610 struct hda_gen_spec *spec = codec->spec;
4611 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4617 #define alt_capture_pcm_open capture_pcm_open
4618 #define alt_capture_pcm_close capture_pcm_close
4620 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4621 struct hda_codec *codec,
4622 unsigned int stream_tag,
4623 unsigned int format,
4624 struct snd_pcm_substream *substream)
4626 struct hda_gen_spec *spec = codec->spec;
4628 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
4629 stream_tag, 0, format);
4630 call_pcm_capture_hook(hinfo, codec, substream,
4631 HDA_GEN_PCM_ACT_PREPARE);
4635 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4636 struct hda_codec *codec,
4637 struct snd_pcm_substream *substream)
4639 struct hda_gen_spec *spec = codec->spec;
4641 snd_hda_codec_cleanup_stream(codec,
4642 spec->adc_nids[substream->number + 1]);
4643 call_pcm_capture_hook(hinfo, codec, substream,
4644 HDA_GEN_PCM_ACT_CLEANUP);
4650 static const struct hda_pcm_stream pcm_analog_playback = {
4654 /* NID is set in build_pcms */
4656 .open = playback_pcm_open,
4657 .close = playback_pcm_close,
4658 .prepare = playback_pcm_prepare,
4659 .cleanup = playback_pcm_cleanup
4663 static const struct hda_pcm_stream pcm_analog_capture = {
4667 /* NID is set in build_pcms */
4669 .open = capture_pcm_open,
4670 .close = capture_pcm_close,
4671 .prepare = capture_pcm_prepare,
4672 .cleanup = capture_pcm_cleanup
4676 static const struct hda_pcm_stream pcm_analog_alt_playback = {
4680 /* NID is set in build_pcms */
4682 .open = alt_playback_pcm_open,
4683 .close = alt_playback_pcm_close,
4684 .prepare = alt_playback_pcm_prepare,
4685 .cleanup = alt_playback_pcm_cleanup
4689 static const struct hda_pcm_stream pcm_analog_alt_capture = {
4690 .substreams = 2, /* can be overridden */
4693 /* NID is set in build_pcms */
4695 .open = alt_capture_pcm_open,
4696 .close = alt_capture_pcm_close,
4697 .prepare = alt_capture_pcm_prepare,
4698 .cleanup = alt_capture_pcm_cleanup
4702 static const struct hda_pcm_stream pcm_digital_playback = {
4706 /* NID is set in build_pcms */
4708 .open = dig_playback_pcm_open,
4709 .close = dig_playback_pcm_close,
4710 .prepare = dig_playback_pcm_prepare,
4711 .cleanup = dig_playback_pcm_cleanup
4715 static const struct hda_pcm_stream pcm_digital_capture = {
4719 /* NID is set in build_pcms */
4722 /* Used by build_pcms to flag that a PCM has no playback stream */
4723 static const struct hda_pcm_stream pcm_null_stream = {
4730 * dynamic changing ADC PCM streams
4732 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4734 struct hda_gen_spec *spec = codec->spec;
4735 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4737 if (spec->cur_adc && spec->cur_adc != new_adc) {
4738 /* stream is running, let's swap the current ADC */
4739 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4740 spec->cur_adc = new_adc;
4741 snd_hda_codec_setup_stream(codec, new_adc,
4742 spec->cur_adc_stream_tag, 0,
4743 spec->cur_adc_format);
4749 /* analog capture with dynamic dual-adc changes */
4750 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4751 struct hda_codec *codec,
4752 unsigned int stream_tag,
4753 unsigned int format,
4754 struct snd_pcm_substream *substream)
4756 struct hda_gen_spec *spec = codec->spec;
4757 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4758 spec->cur_adc_stream_tag = stream_tag;
4759 spec->cur_adc_format = format;
4760 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4764 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4765 struct hda_codec *codec,
4766 struct snd_pcm_substream *substream)
4768 struct hda_gen_spec *spec = codec->spec;
4769 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4774 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4778 .nid = 0, /* fill later */
4780 .prepare = dyn_adc_capture_pcm_prepare,
4781 .cleanup = dyn_adc_capture_pcm_cleanup
4785 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4786 const char *chip_name)
4792 strlcpy(str, chip_name, len);
4794 /* drop non-alnum chars after a space */
4795 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4796 if (!isalnum(p[1])) {
4801 strlcat(str, sfx, len);
4804 /* build PCM streams based on the parsed results */
4805 int snd_hda_gen_build_pcms(struct hda_codec *codec)
4807 struct hda_gen_spec *spec = codec->spec;
4808 struct hda_pcm *info = spec->pcm_rec;
4809 const struct hda_pcm_stream *p;
4810 bool have_multi_adcs;
4812 codec->num_pcms = 1;
4813 codec->pcm_info = info;
4815 if (spec->no_analog)
4818 fill_pcm_stream_name(spec->stream_name_analog,
4819 sizeof(spec->stream_name_analog),
4820 " Analog", codec->chip_name);
4821 info->name = spec->stream_name_analog;
4823 if (spec->multiout.num_dacs > 0) {
4824 p = spec->stream_analog_playback;
4826 p = &pcm_analog_playback;
4827 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4828 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4829 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4830 spec->multiout.max_channels;
4831 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4832 spec->autocfg.line_outs == 2)
4833 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4836 if (spec->num_adc_nids) {
4837 p = spec->stream_analog_capture;
4839 if (spec->dyn_adc_switch)
4840 p = &dyn_adc_pcm_analog_capture;
4842 p = &pcm_analog_capture;
4844 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4845 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4849 /* SPDIF for stream index #1 */
4850 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
4851 fill_pcm_stream_name(spec->stream_name_digital,
4852 sizeof(spec->stream_name_digital),
4853 " Digital", codec->chip_name);
4854 codec->num_pcms = 2;
4855 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4856 info = spec->pcm_rec + 1;
4857 info->name = spec->stream_name_digital;
4858 if (spec->dig_out_type)
4859 info->pcm_type = spec->dig_out_type;
4861 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4862 if (spec->multiout.dig_out_nid) {
4863 p = spec->stream_digital_playback;
4865 p = &pcm_digital_playback;
4866 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4867 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4869 if (spec->dig_in_nid) {
4870 p = spec->stream_digital_capture;
4872 p = &pcm_digital_capture;
4873 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4874 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4878 if (spec->no_analog)
4881 /* If the use of more than one ADC is requested for the current
4882 * model, configure a second analog capture-only PCM.
4884 have_multi_adcs = (spec->num_adc_nids > 1) &&
4885 !spec->dyn_adc_switch && !spec->auto_mic;
4886 /* Additional Analaog capture for index #2 */
4887 if (spec->alt_dac_nid || have_multi_adcs) {
4888 fill_pcm_stream_name(spec->stream_name_alt_analog,
4889 sizeof(spec->stream_name_alt_analog),
4890 " Alt Analog", codec->chip_name);
4891 codec->num_pcms = 3;
4892 info = spec->pcm_rec + 2;
4893 info->name = spec->stream_name_alt_analog;
4894 if (spec->alt_dac_nid) {
4895 p = spec->stream_analog_alt_playback;
4897 p = &pcm_analog_alt_playback;
4898 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4899 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4902 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4904 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4906 if (have_multi_adcs) {
4907 p = spec->stream_analog_alt_capture;
4909 p = &pcm_analog_alt_capture;
4910 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4911 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4913 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4914 spec->num_adc_nids - 1;
4916 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4918 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4924 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4928 * Standard auto-parser initializations
4931 /* configure the given path as a proper output */
4932 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
4934 struct nid_path *path;
4937 path = snd_hda_get_path_from_idx(codec, path_idx);
4938 if (!path || !path->depth)
4940 pin = path->path[path->depth - 1];
4941 restore_pin_ctl(codec, pin);
4942 snd_hda_activate_path(codec, path, path->active,
4943 aamix_default(codec->spec));
4944 set_pin_eapd(codec, pin, path->active);
4947 /* initialize primary output paths */
4948 static void init_multi_out(struct hda_codec *codec)
4950 struct hda_gen_spec *spec = codec->spec;
4953 for (i = 0; i < spec->autocfg.line_outs; i++)
4954 set_output_and_unmute(codec, spec->out_paths[i]);
4958 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
4962 for (i = 0; i < num_outs; i++)
4963 set_output_and_unmute(codec, paths[i]);
4966 /* initialize hp and speaker paths */
4967 static void init_extra_out(struct hda_codec *codec)
4969 struct hda_gen_spec *spec = codec->spec;
4971 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
4972 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
4973 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4974 __init_extra_out(codec, spec->autocfg.speaker_outs,
4975 spec->speaker_paths);
4978 /* initialize multi-io paths */
4979 static void init_multi_io(struct hda_codec *codec)
4981 struct hda_gen_spec *spec = codec->spec;
4984 for (i = 0; i < spec->multi_ios; i++) {
4985 hda_nid_t pin = spec->multi_io[i].pin;
4986 struct nid_path *path;
4987 path = get_multiio_path(codec, i);
4990 if (!spec->multi_io[i].ctl_in)
4991 spec->multi_io[i].ctl_in =
4992 snd_hda_codec_get_pin_target(codec, pin);
4993 snd_hda_activate_path(codec, path, path->active,
4994 aamix_default(spec));
4998 /* set up input pins and loopback paths */
4999 static void init_analog_input(struct hda_codec *codec)
5001 struct hda_gen_spec *spec = codec->spec;
5002 struct auto_pin_cfg *cfg = &spec->autocfg;
5005 for (i = 0; i < cfg->num_inputs; i++) {
5006 hda_nid_t nid = cfg->inputs[i].pin;
5007 if (is_input_pin(codec, nid))
5008 restore_pin_ctl(codec, nid);
5010 /* init loopback inputs */
5011 if (spec->mixer_nid) {
5012 resume_path_from_idx(codec, spec->loopback_paths[i]);
5013 resume_path_from_idx(codec, spec->loopback_merge_path);
5018 /* initialize ADC paths */
5019 static void init_input_src(struct hda_codec *codec)
5021 struct hda_gen_spec *spec = codec->spec;
5022 struct hda_input_mux *imux = &spec->input_mux;
5023 struct nid_path *path;
5026 if (spec->dyn_adc_switch)
5029 nums = spec->num_adc_nids;
5031 for (c = 0; c < nums; c++) {
5032 for (i = 0; i < imux->num_items; i++) {
5033 path = get_input_path(codec, c, i);
5035 bool active = path->active;
5036 if (i == spec->cur_mux[c])
5038 snd_hda_activate_path(codec, path, active, false);
5042 update_hp_mic(codec, c, true);
5045 if (spec->cap_sync_hook)
5046 spec->cap_sync_hook(codec, NULL);
5049 /* set right pin controls for digital I/O */
5050 static void init_digital(struct hda_codec *codec)
5052 struct hda_gen_spec *spec = codec->spec;
5056 for (i = 0; i < spec->autocfg.dig_outs; i++)
5057 set_output_and_unmute(codec, spec->digout_paths[i]);
5058 pin = spec->autocfg.dig_in_pin;
5060 restore_pin_ctl(codec, pin);
5061 resume_path_from_idx(codec, spec->digin_path);
5065 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5066 * invalid unsol tags by some reason
5068 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5072 for (i = 0; i < codec->init_pins.used; i++) {
5073 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5074 hda_nid_t nid = pin->nid;
5075 if (is_jack_detectable(codec, nid) &&
5076 !snd_hda_jack_tbl_get(codec, nid))
5077 snd_hda_codec_update_cache(codec, nid, 0,
5078 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5083 * initialize the generic spec;
5084 * this can be put as patch_ops.init function
5086 int snd_hda_gen_init(struct hda_codec *codec)
5088 struct hda_gen_spec *spec = codec->spec;
5090 if (spec->init_hook)
5091 spec->init_hook(codec);
5093 snd_hda_apply_verbs(codec);
5095 codec->cached_write = 1;
5097 init_multi_out(codec);
5098 init_extra_out(codec);
5099 init_multi_io(codec);
5100 init_analog_input(codec);
5101 init_input_src(codec);
5102 init_digital(codec);
5104 clear_unsol_on_unused_pins(codec);
5106 /* call init functions of standard auto-mute helpers */
5107 update_automute_all(codec);
5109 snd_hda_codec_flush_cache(codec);
5111 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5112 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5114 hda_call_check_power_status(codec, 0x01);
5117 EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5120 * free the generic spec;
5121 * this can be put as patch_ops.free function
5123 void snd_hda_gen_free(struct hda_codec *codec)
5125 snd_hda_detach_beep_device(codec);
5126 snd_hda_gen_spec_free(codec->spec);
5130 EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5134 * check the loopback power save state;
5135 * this can be put as patch_ops.check_power_status function
5137 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5139 struct hda_gen_spec *spec = codec->spec;
5140 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5142 EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5147 * the generic codec support
5150 static const struct hda_codec_ops generic_patch_ops = {
5151 .build_controls = snd_hda_gen_build_controls,
5152 .build_pcms = snd_hda_gen_build_pcms,
5153 .init = snd_hda_gen_init,
5154 .free = snd_hda_gen_free,
5155 .unsol_event = snd_hda_jack_unsol_event,
5157 .check_power_status = snd_hda_gen_check_power_status,
5161 int snd_hda_parse_generic_codec(struct hda_codec *codec)
5163 struct hda_gen_spec *spec;
5166 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5169 snd_hda_gen_spec_init(spec);
5172 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5176 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5180 codec->patch_ops = generic_patch_ops;
5184 snd_hda_gen_free(codec);
5187 EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);