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 /* return a DAC if paired to the given pin by codec driver */
472 static hda_nid_t get_preferred_dac(struct hda_codec *codec, hda_nid_t pin)
474 struct hda_gen_spec *spec = codec->spec;
475 const hda_nid_t *list = spec->preferred_dacs;
479 for (; *list; list += 2)
485 /* look for an empty DAC slot */
486 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
489 struct hda_gen_spec *spec = codec->spec;
493 for (i = 0; i < spec->num_all_dacs; i++) {
494 hda_nid_t nid = spec->all_dacs[i];
495 if (!nid || is_dac_already_used(codec, nid))
497 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
498 if (is_digital != cap_digital)
500 if (is_reachable_path(codec, nid, pin))
506 /* replace the channels in the composed amp value with the given number */
507 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
509 val &= ~(0x3U << 16);
514 /* check whether the widget has the given amp capability for the direction */
515 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
516 int dir, unsigned int bits)
520 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
521 if (query_amp_caps(codec, nid, dir) & bits)
526 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
527 hda_nid_t nid2, int dir)
529 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
530 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
531 return (query_amp_caps(codec, nid1, dir) ==
532 query_amp_caps(codec, nid2, dir));
535 #define nid_has_mute(codec, nid, dir) \
536 check_amp_caps(codec, nid, dir, (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE))
537 #define nid_has_volume(codec, nid, dir) \
538 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
540 /* look for a widget suitable for assigning a mute switch in the path */
541 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
542 struct nid_path *path)
546 for (i = path->depth - 1; i >= 0; i--) {
547 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
548 return path->path[i];
549 if (i != path->depth - 1 && i != 0 &&
550 nid_has_mute(codec, path->path[i], HDA_INPUT))
551 return path->path[i];
556 /* look for a widget suitable for assigning a volume ctl in the path */
557 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
558 struct nid_path *path)
562 for (i = path->depth - 1; i >= 0; i--) {
563 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
564 return path->path[i];
570 * path activation / deactivation
573 /* can have the amp-in capability? */
574 static bool has_amp_in(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_IN_AMP))
582 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
587 /* can have the amp-out capability? */
588 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
590 hda_nid_t nid = path->path[idx];
591 unsigned int caps = get_wcaps(codec, nid);
592 unsigned int type = get_wcaps_type(caps);
594 if (!(caps & AC_WCAP_OUT_AMP))
596 if (type == AC_WID_PIN && !idx) /* only for output pins */
601 /* check whether the given (nid,dir,idx) is active */
602 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
603 unsigned int dir, unsigned int idx)
605 struct hda_gen_spec *spec = codec->spec;
608 for (n = 0; n < spec->paths.used; n++) {
609 struct nid_path *path = snd_array_elem(&spec->paths, n);
612 for (i = 0; i < path->depth; i++) {
613 if (path->path[i] == nid) {
614 if (dir == HDA_OUTPUT || path->idx[i] == idx)
623 /* check whether the NID is referred by any active paths */
624 #define is_active_nid_for_any(codec, nid) \
625 is_active_nid(codec, nid, HDA_OUTPUT, 0)
627 /* get the default amp value for the target state */
628 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
629 int dir, unsigned int caps, bool enable)
631 unsigned int val = 0;
633 if (caps & AC_AMPCAP_NUM_STEPS) {
636 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
638 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
645 /* initialize the amp value (only at the first time) */
646 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
648 unsigned int caps = query_amp_caps(codec, nid, dir);
649 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
650 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
653 /* calculate amp value mask we can modify;
654 * if the given amp is controlled by mixers, don't touch it
656 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
657 hda_nid_t nid, int dir, int idx,
660 unsigned int mask = 0xff;
662 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
663 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
666 if (caps & AC_AMPCAP_NUM_STEPS) {
667 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
668 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
674 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
675 int idx, int idx_to_check, bool enable)
678 unsigned int mask, val;
680 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
683 caps = query_amp_caps(codec, nid, dir);
684 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
685 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
690 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
693 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
696 hda_nid_t nid = path->path[i];
697 init_amp(codec, nid, HDA_OUTPUT, 0);
698 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
701 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
702 int i, bool enable, bool add_aamix)
704 struct hda_gen_spec *spec = codec->spec;
705 const hda_nid_t *conn;
708 hda_nid_t nid = path->path[i];
710 nums = snd_hda_get_conn_list(codec, nid, &conn);
711 type = get_wcaps_type(get_wcaps(codec, nid));
712 if (type == AC_WID_PIN ||
713 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
719 for (n = 0; n < nums; n++)
720 init_amp(codec, nid, HDA_INPUT, n);
722 /* here is a little bit tricky in comparison with activate_amp_out();
723 * when aa-mixer is available, we need to enable the path as well
725 for (n = 0; n < nums; n++) {
726 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
728 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
732 /* activate or deactivate the given path
733 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
735 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
736 bool enable, bool add_aamix)
738 struct hda_gen_spec *spec = codec->spec;
742 path->active = false;
744 for (i = path->depth - 1; i >= 0; i--) {
745 hda_nid_t nid = path->path[i];
746 if (enable && spec->power_down_unused) {
747 /* make sure the widget is powered up */
748 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
749 snd_hda_codec_write(codec, nid, 0,
750 AC_VERB_SET_POWER_STATE,
753 if (enable && path->multi[i])
754 snd_hda_codec_write_cache(codec, nid, 0,
755 AC_VERB_SET_CONNECT_SEL,
757 if (has_amp_in(codec, path, i))
758 activate_amp_in(codec, path, i, enable, add_aamix);
759 if (has_amp_out(codec, path, i))
760 activate_amp_out(codec, path, i, enable);
766 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
768 /* if the given path is inactive, put widgets into D3 (only if suitable) */
769 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
771 struct hda_gen_spec *spec = codec->spec;
772 bool changed = false;
775 if (!spec->power_down_unused || path->active)
778 for (i = 0; i < path->depth; i++) {
779 hda_nid_t nid = path->path[i];
780 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
781 !is_active_nid_for_any(codec, nid)) {
782 snd_hda_codec_write(codec, nid, 0,
783 AC_VERB_SET_POWER_STATE,
791 snd_hda_codec_read(codec, path->path[0], 0,
792 AC_VERB_GET_POWER_STATE, 0);
796 /* turn on/off EAPD on the given pin */
797 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
799 struct hda_gen_spec *spec = codec->spec;
800 if (spec->own_eapd_ctl ||
801 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
803 if (spec->keep_eapd_on && !enable)
807 snd_hda_codec_update_cache(codec, pin, 0,
808 AC_VERB_SET_EAPD_BTLENABLE,
809 enable ? 0x02 : 0x00);
812 /* re-initialize the path specified by the given path index */
813 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
815 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
817 snd_hda_activate_path(codec, path, path->active, false);
822 * Helper functions for creating mixer ctl elements
830 static const struct snd_kcontrol_new control_templates[] = {
831 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
832 HDA_CODEC_MUTE(NULL, 0, 0, 0),
833 HDA_BIND_MUTE(NULL, 0, 0, 0),
836 /* add dynamic controls from template */
837 static struct snd_kcontrol_new *
838 add_control(struct hda_gen_spec *spec, int type, const char *name,
839 int cidx, unsigned long val)
841 struct snd_kcontrol_new *knew;
843 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
847 if (get_amp_nid_(val))
848 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
849 knew->private_value = val;
853 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
854 const char *pfx, const char *dir,
855 const char *sfx, int cidx, unsigned long val)
858 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
859 if (!add_control(spec, type, name, cidx, val))
864 #define add_pb_vol_ctrl(spec, type, pfx, val) \
865 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
866 #define add_pb_sw_ctrl(spec, type, pfx, val) \
867 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
868 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
869 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
870 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
871 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
873 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
874 unsigned int chs, struct nid_path *path)
879 val = path->ctls[NID_PATH_VOL_CTL];
882 val = amp_val_replace_channels(val, chs);
883 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
886 /* return the channel bits suitable for the given path->ctls[] */
887 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
890 int chs = 1; /* mono (left only) */
892 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
893 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
894 chs = 3; /* stereo */
899 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
900 struct nid_path *path)
902 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
903 return add_vol_ctl(codec, pfx, cidx, chs, path);
906 /* create a mute-switch for the given mixer widget;
907 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
909 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
910 unsigned int chs, struct nid_path *path)
913 int type = HDA_CTL_WIDGET_MUTE;
917 val = path->ctls[NID_PATH_MUTE_CTL];
920 val = amp_val_replace_channels(val, chs);
921 if (get_amp_direction_(val) == HDA_INPUT) {
922 hda_nid_t nid = get_amp_nid_(val);
923 int nums = snd_hda_get_num_conns(codec, nid);
925 type = HDA_CTL_BIND_MUTE;
929 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
932 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
933 int cidx, struct nid_path *path)
935 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
936 return add_sw_ctl(codec, pfx, cidx, chs, path);
939 /* any ctl assigned to the path with the given index? */
940 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
942 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
943 return path && path->ctls[ctl_type];
946 static const char * const channel_name[4] = {
947 "Front", "Surround", "CLFE", "Side"
950 /* give some appropriate ctl name prefix for the given line out channel */
951 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
952 int *index, int ctl_type)
954 struct hda_gen_spec *spec = codec->spec;
955 struct auto_pin_cfg *cfg = &spec->autocfg;
958 if (cfg->line_outs == 1 && !spec->multi_ios &&
959 !cfg->hp_outs && !cfg->speaker_outs)
960 return spec->vmaster_mute.hook ? "PCM" : "Master";
962 /* if there is really a single DAC used in the whole output paths,
963 * use it master (or "PCM" if a vmaster hook is present)
965 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
966 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
967 return spec->vmaster_mute.hook ? "PCM" : "Master";
969 /* multi-io channels */
970 if (ch >= cfg->line_outs)
971 return channel_name[ch];
973 switch (cfg->line_out_type) {
974 case AUTO_PIN_SPEAKER_OUT:
975 /* if the primary channel vol/mute is shared with HP volume,
976 * don't name it as Speaker
978 if (!ch && cfg->hp_outs &&
979 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
981 if (cfg->line_outs == 1)
983 if (cfg->line_outs == 2)
984 return ch ? "Bass Speaker" : "Speaker";
986 case AUTO_PIN_HP_OUT:
987 /* if the primary channel vol/mute is shared with spk volume,
988 * don't name it as Headphone
990 if (!ch && cfg->speaker_outs &&
991 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
993 /* for multi-io case, only the primary out */
994 if (ch && spec->multi_ios)
1000 /* for a single channel output, we don't have to name the channel */
1001 if (cfg->line_outs == 1 && !spec->multi_ios)
1004 if (ch >= ARRAY_SIZE(channel_name)) {
1009 return channel_name[ch];
1013 * Parse output paths
1016 /* badness definition */
1018 /* No primary DAC is found for the main output */
1019 BAD_NO_PRIMARY_DAC = 0x10000,
1020 /* No DAC is found for the extra output */
1021 BAD_NO_DAC = 0x4000,
1022 /* No possible multi-ios */
1023 BAD_MULTI_IO = 0x120,
1024 /* No individual DAC for extra output */
1025 BAD_NO_EXTRA_DAC = 0x102,
1026 /* No individual DAC for extra surrounds */
1027 BAD_NO_EXTRA_SURR_DAC = 0x101,
1028 /* Primary DAC shared with main surrounds */
1029 BAD_SHARED_SURROUND = 0x100,
1030 /* No independent HP possible */
1031 BAD_NO_INDEP_HP = 0x10,
1032 /* Primary DAC shared with main CLFE */
1033 BAD_SHARED_CLFE = 0x10,
1034 /* Primary DAC shared with extra surrounds */
1035 BAD_SHARED_EXTRA_SURROUND = 0x10,
1036 /* Volume widget is shared */
1037 BAD_SHARED_VOL = 0x10,
1040 /* look for widgets in the given path which are appropriate for
1041 * volume and mute controls, and assign the values to ctls[].
1043 * When no appropriate widget is found in the path, the badness value
1044 * is incremented depending on the situation. The function returns the
1045 * total badness for both volume and mute controls.
1047 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1054 return BAD_SHARED_VOL * 2;
1056 if (path->ctls[NID_PATH_VOL_CTL] ||
1057 path->ctls[NID_PATH_MUTE_CTL])
1058 return 0; /* already evaluated */
1060 nid = look_for_out_vol_nid(codec, path);
1062 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1063 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1064 badness += BAD_SHARED_VOL;
1066 path->ctls[NID_PATH_VOL_CTL] = val;
1068 badness += BAD_SHARED_VOL;
1069 nid = look_for_out_mute_nid(codec, path);
1071 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1072 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1073 nid_has_mute(codec, nid, HDA_OUTPUT))
1074 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1076 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1077 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1078 badness += BAD_SHARED_VOL;
1080 path->ctls[NID_PATH_MUTE_CTL] = val;
1082 badness += BAD_SHARED_VOL;
1086 const struct badness_table hda_main_out_badness = {
1087 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1088 .no_dac = BAD_NO_DAC,
1089 .shared_primary = BAD_NO_PRIMARY_DAC,
1090 .shared_surr = BAD_SHARED_SURROUND,
1091 .shared_clfe = BAD_SHARED_CLFE,
1092 .shared_surr_main = BAD_SHARED_SURROUND,
1094 EXPORT_SYMBOL_HDA(hda_main_out_badness);
1096 const struct badness_table hda_extra_out_badness = {
1097 .no_primary_dac = BAD_NO_DAC,
1098 .no_dac = BAD_NO_DAC,
1099 .shared_primary = BAD_NO_EXTRA_DAC,
1100 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1101 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1102 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1104 EXPORT_SYMBOL_HDA(hda_extra_out_badness);
1106 /* get the DAC of the primary output corresponding to the given array index */
1107 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1109 struct hda_gen_spec *spec = codec->spec;
1110 struct auto_pin_cfg *cfg = &spec->autocfg;
1112 if (cfg->line_outs > idx)
1113 return spec->private_dac_nids[idx];
1114 idx -= cfg->line_outs;
1115 if (spec->multi_ios > idx)
1116 return spec->multi_io[idx].dac;
1120 /* return the DAC if it's reachable, otherwise zero */
1121 static inline hda_nid_t try_dac(struct hda_codec *codec,
1122 hda_nid_t dac, hda_nid_t pin)
1124 return is_reachable_path(codec, dac, pin) ? dac : 0;
1127 /* try to assign DACs to pins and return the resultant badness */
1128 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1129 const hda_nid_t *pins, hda_nid_t *dacs,
1131 const struct badness_table *bad)
1133 struct hda_gen_spec *spec = codec->spec;
1141 for (i = 0; i < num_outs; i++) {
1142 struct nid_path *path;
1143 hda_nid_t pin = pins[i];
1145 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1147 badness += assign_out_path_ctls(codec, path);
1151 dacs[i] = get_preferred_dac(codec, pin);
1153 if (is_dac_already_used(codec, dacs[i]))
1154 badness += bad->shared_primary;
1158 dacs[i] = look_for_dac(codec, pin, false);
1159 if (!dacs[i] && !i) {
1160 /* try to steal the DAC of surrounds for the front */
1161 for (j = 1; j < num_outs; j++) {
1162 if (is_reachable_path(codec, dacs[j], pin)) {
1165 invalidate_nid_path(codec, path_idx[j]);
1174 dac = try_dac(codec, get_primary_out(codec, i), pin);
1176 dac = try_dac(codec, dacs[0], pin);
1178 dac = try_dac(codec, get_primary_out(codec, i), pin);
1181 badness += bad->shared_primary;
1183 badness += bad->shared_surr;
1185 badness += bad->shared_clfe;
1186 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1187 dac = spec->private_dac_nids[0];
1188 badness += bad->shared_surr_main;
1190 badness += bad->no_primary_dac;
1192 badness += bad->no_dac;
1196 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1197 if (!path && !i && spec->mixer_nid) {
1198 /* try with aamix */
1199 path = snd_hda_add_new_path(codec, dac, pin, 0);
1203 badness += bad->no_dac;
1205 /* print_nid_path("output", path); */
1206 path->active = true;
1207 path_idx[i] = snd_hda_get_path_idx(codec, path);
1208 badness += assign_out_path_ctls(codec, path);
1215 /* return NID if the given pin has only a single connection to a certain DAC */
1216 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1218 struct hda_gen_spec *spec = codec->spec;
1220 hda_nid_t nid_found = 0;
1222 for (i = 0; i < spec->num_all_dacs; i++) {
1223 hda_nid_t nid = spec->all_dacs[i];
1224 if (!nid || is_dac_already_used(codec, nid))
1226 if (is_reachable_path(codec, nid, pin)) {
1235 /* check whether the given pin can be a multi-io pin */
1236 static bool can_be_multiio_pin(struct hda_codec *codec,
1237 unsigned int location, hda_nid_t nid)
1239 unsigned int defcfg, caps;
1241 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1242 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1244 if (location && get_defcfg_location(defcfg) != location)
1246 caps = snd_hda_query_pin_caps(codec, nid);
1247 if (!(caps & AC_PINCAP_OUT))
1252 /* count the number of input pins that are capable to be multi-io */
1253 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1255 struct hda_gen_spec *spec = codec->spec;
1256 struct auto_pin_cfg *cfg = &spec->autocfg;
1257 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1258 unsigned int location = get_defcfg_location(defcfg);
1262 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1263 for (i = 0; i < cfg->num_inputs; i++) {
1264 if (cfg->inputs[i].type != type)
1266 if (can_be_multiio_pin(codec, location,
1267 cfg->inputs[i].pin))
1277 * When hardwired is set, try to fill ony hardwired pins, and returns
1278 * zero if any pins are filled, non-zero if nothing found.
1279 * When hardwired is off, try to fill possible input pins, and returns
1280 * the badness value.
1282 static int fill_multi_ios(struct hda_codec *codec,
1283 hda_nid_t reference_pin,
1286 struct hda_gen_spec *spec = codec->spec;
1287 struct auto_pin_cfg *cfg = &spec->autocfg;
1288 int type, i, j, num_pins, old_pins;
1289 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1290 unsigned int location = get_defcfg_location(defcfg);
1292 struct nid_path *path;
1294 old_pins = spec->multi_ios;
1298 num_pins = count_multiio_pins(codec, reference_pin);
1302 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1303 for (i = 0; i < cfg->num_inputs; i++) {
1304 hda_nid_t nid = cfg->inputs[i].pin;
1307 if (cfg->inputs[i].type != type)
1309 if (!can_be_multiio_pin(codec, location, nid))
1311 for (j = 0; j < spec->multi_ios; j++) {
1312 if (nid == spec->multi_io[j].pin)
1315 if (j < spec->multi_ios)
1319 dac = get_dac_if_single(codec, nid);
1321 dac = look_for_dac(codec, nid, false);
1326 path = snd_hda_add_new_path(codec, dac, nid,
1332 /* print_nid_path("multiio", path); */
1333 spec->multi_io[spec->multi_ios].pin = nid;
1334 spec->multi_io[spec->multi_ios].dac = dac;
1335 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1336 snd_hda_get_path_idx(codec, path);
1338 if (spec->multi_ios >= 2)
1344 badness = BAD_MULTI_IO;
1345 if (old_pins == spec->multi_ios) {
1347 return 1; /* nothing found */
1349 return badness; /* no badness if nothing found */
1351 if (!hardwired && spec->multi_ios < 2) {
1352 /* cancel newly assigned paths */
1353 spec->paths.used -= spec->multi_ios - old_pins;
1354 spec->multi_ios = old_pins;
1358 /* assign volume and mute controls */
1359 for (i = old_pins; i < spec->multi_ios; i++) {
1360 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1361 badness += assign_out_path_ctls(codec, path);
1367 /* map DACs for all pins in the list if they are single connections */
1368 static bool map_singles(struct hda_codec *codec, int outs,
1369 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1371 struct hda_gen_spec *spec = codec->spec;
1374 for (i = 0; i < outs; i++) {
1375 struct nid_path *path;
1379 dac = get_dac_if_single(codec, pins[i]);
1382 path = snd_hda_add_new_path(codec, dac, pins[i],
1384 if (!path && !i && spec->mixer_nid)
1385 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1389 /* print_nid_path("output", path); */
1390 path->active = true;
1391 path_idx[i] = snd_hda_get_path_idx(codec, path);
1397 /* create a new path including aamix if available, and return its index */
1398 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1400 struct hda_gen_spec *spec = codec->spec;
1401 struct nid_path *path;
1402 hda_nid_t path_dac, dac, pin;
1404 path = snd_hda_get_path_from_idx(codec, path_idx);
1405 if (!path || !path->depth ||
1406 is_nid_contained(path, spec->mixer_nid))
1408 path_dac = path->path[0];
1409 dac = spec->private_dac_nids[0];
1410 pin = path->path[path->depth - 1];
1411 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1413 if (dac != path_dac)
1415 else if (spec->multiout.hp_out_nid[0])
1416 dac = spec->multiout.hp_out_nid[0];
1417 else if (spec->multiout.extra_out_nid[0])
1418 dac = spec->multiout.extra_out_nid[0];
1422 path = snd_hda_add_new_path(codec, dac, pin,
1427 /* print_nid_path("output-aamix", path); */
1428 path->active = false; /* unused as default */
1429 return snd_hda_get_path_idx(codec, path);
1432 /* check whether the independent HP is available with the current config */
1433 static bool indep_hp_possible(struct hda_codec *codec)
1435 struct hda_gen_spec *spec = codec->spec;
1436 struct auto_pin_cfg *cfg = &spec->autocfg;
1437 struct nid_path *path;
1440 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1441 idx = spec->out_paths[0];
1443 idx = spec->hp_paths[0];
1444 path = snd_hda_get_path_from_idx(codec, idx);
1448 /* assume no path conflicts unless aamix is involved */
1449 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1452 /* check whether output paths contain aamix */
1453 for (i = 0; i < cfg->line_outs; i++) {
1454 if (spec->out_paths[i] == idx)
1456 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1457 if (path && is_nid_contained(path, spec->mixer_nid))
1460 for (i = 0; i < cfg->speaker_outs; i++) {
1461 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1462 if (path && is_nid_contained(path, spec->mixer_nid))
1469 /* fill the empty entries in the dac array for speaker/hp with the
1470 * shared dac pointed by the paths
1472 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1473 hda_nid_t *dacs, int *path_idx)
1475 struct nid_path *path;
1478 for (i = 0; i < num_outs; i++) {
1481 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1484 dacs[i] = path->path[0];
1488 /* fill in the dac_nids table from the parsed pin configuration */
1489 static int fill_and_eval_dacs(struct hda_codec *codec,
1490 bool fill_hardwired,
1491 bool fill_mio_first)
1493 struct hda_gen_spec *spec = codec->spec;
1494 struct auto_pin_cfg *cfg = &spec->autocfg;
1495 int i, err, badness;
1497 /* set num_dacs once to full for look_for_dac() */
1498 spec->multiout.num_dacs = cfg->line_outs;
1499 spec->multiout.dac_nids = spec->private_dac_nids;
1500 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1501 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1502 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1503 spec->multi_ios = 0;
1504 snd_array_free(&spec->paths);
1506 /* clear path indices */
1507 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1508 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1509 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1510 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1511 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1512 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1513 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1514 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1518 /* fill hard-wired DACs first */
1519 if (fill_hardwired) {
1522 mapped = map_singles(codec, cfg->line_outs,
1524 spec->private_dac_nids,
1526 mapped |= map_singles(codec, cfg->hp_outs,
1528 spec->multiout.hp_out_nid,
1530 mapped |= map_singles(codec, cfg->speaker_outs,
1532 spec->multiout.extra_out_nid,
1533 spec->speaker_paths);
1534 if (fill_mio_first && cfg->line_outs == 1 &&
1535 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1536 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1543 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1544 spec->private_dac_nids, spec->out_paths,
1545 spec->main_out_badness);
1547 if (fill_mio_first &&
1548 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1549 /* try to fill multi-io first */
1550 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1553 /* we don't count badness at this stage yet */
1556 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1557 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1558 spec->multiout.hp_out_nid,
1560 spec->extra_out_badness);
1565 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1566 err = try_assign_dacs(codec, cfg->speaker_outs,
1568 spec->multiout.extra_out_nid,
1569 spec->speaker_paths,
1570 spec->extra_out_badness);
1575 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1576 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1582 if (spec->mixer_nid) {
1583 spec->aamix_out_paths[0] =
1584 check_aamix_out_path(codec, spec->out_paths[0]);
1585 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1586 spec->aamix_out_paths[1] =
1587 check_aamix_out_path(codec, spec->hp_paths[0]);
1588 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1589 spec->aamix_out_paths[2] =
1590 check_aamix_out_path(codec, spec->speaker_paths[0]);
1593 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1594 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1595 spec->multi_ios = 1; /* give badness */
1597 /* re-count num_dacs and squash invalid entries */
1598 spec->multiout.num_dacs = 0;
1599 for (i = 0; i < cfg->line_outs; i++) {
1600 if (spec->private_dac_nids[i])
1601 spec->multiout.num_dacs++;
1603 memmove(spec->private_dac_nids + i,
1604 spec->private_dac_nids + i + 1,
1605 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1606 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1610 spec->ext_channel_count = spec->min_channel_count =
1611 spec->multiout.num_dacs * 2;
1613 if (spec->multi_ios == 2) {
1614 for (i = 0; i < 2; i++)
1615 spec->private_dac_nids[spec->multiout.num_dacs++] =
1616 spec->multi_io[i].dac;
1617 } else if (spec->multi_ios) {
1618 spec->multi_ios = 0;
1619 badness += BAD_MULTI_IO;
1622 if (spec->indep_hp && !indep_hp_possible(codec))
1623 badness += BAD_NO_INDEP_HP;
1625 /* re-fill the shared DAC for speaker / headphone */
1626 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1627 refill_shared_dacs(codec, cfg->hp_outs,
1628 spec->multiout.hp_out_nid,
1630 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1631 refill_shared_dacs(codec, cfg->speaker_outs,
1632 spec->multiout.extra_out_nid,
1633 spec->speaker_paths);
1638 #define DEBUG_BADNESS
1640 #ifdef DEBUG_BADNESS
1641 #define debug_badness snd_printdd
1643 #define debug_badness(...)
1646 #ifdef DEBUG_BADNESS
1647 static inline void print_nid_path_idx(struct hda_codec *codec,
1648 const char *pfx, int idx)
1650 struct nid_path *path;
1652 path = snd_hda_get_path_from_idx(codec, idx);
1654 print_nid_path(pfx, path);
1657 static void debug_show_configs(struct hda_codec *codec,
1658 struct auto_pin_cfg *cfg)
1660 struct hda_gen_spec *spec = codec->spec;
1661 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1664 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1665 cfg->line_out_pins[0], cfg->line_out_pins[1],
1666 cfg->line_out_pins[2], cfg->line_out_pins[3],
1667 spec->multiout.dac_nids[0],
1668 spec->multiout.dac_nids[1],
1669 spec->multiout.dac_nids[2],
1670 spec->multiout.dac_nids[3],
1671 lo_type[cfg->line_out_type]);
1672 for (i = 0; i < cfg->line_outs; i++)
1673 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1674 if (spec->multi_ios > 0)
1675 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1677 spec->multi_io[0].pin, spec->multi_io[1].pin,
1678 spec->multi_io[0].dac, spec->multi_io[1].dac);
1679 for (i = 0; i < spec->multi_ios; i++)
1680 print_nid_path_idx(codec, " mio",
1681 spec->out_paths[cfg->line_outs + i]);
1683 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1684 cfg->hp_pins[0], cfg->hp_pins[1],
1685 cfg->hp_pins[2], cfg->hp_pins[3],
1686 spec->multiout.hp_out_nid[0],
1687 spec->multiout.hp_out_nid[1],
1688 spec->multiout.hp_out_nid[2],
1689 spec->multiout.hp_out_nid[3]);
1690 for (i = 0; i < cfg->hp_outs; i++)
1691 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1692 if (cfg->speaker_outs)
1693 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1694 cfg->speaker_pins[0], cfg->speaker_pins[1],
1695 cfg->speaker_pins[2], cfg->speaker_pins[3],
1696 spec->multiout.extra_out_nid[0],
1697 spec->multiout.extra_out_nid[1],
1698 spec->multiout.extra_out_nid[2],
1699 spec->multiout.extra_out_nid[3]);
1700 for (i = 0; i < cfg->speaker_outs; i++)
1701 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1702 for (i = 0; i < 3; i++)
1703 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1706 #define debug_show_configs(codec, cfg) /* NOP */
1709 /* find all available DACs of the codec */
1710 static void fill_all_dac_nids(struct hda_codec *codec)
1712 struct hda_gen_spec *spec = codec->spec;
1714 hda_nid_t nid = codec->start_nid;
1716 spec->num_all_dacs = 0;
1717 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1718 for (i = 0; i < codec->num_nodes; i++, nid++) {
1719 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1721 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1722 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1725 spec->all_dacs[spec->num_all_dacs++] = nid;
1729 static int parse_output_paths(struct hda_codec *codec)
1731 struct hda_gen_spec *spec = codec->spec;
1732 struct auto_pin_cfg *cfg = &spec->autocfg;
1733 struct auto_pin_cfg *best_cfg;
1735 int best_badness = INT_MAX;
1737 bool fill_hardwired = true, fill_mio_first = true;
1738 bool best_wired = true, best_mio = true;
1739 bool hp_spk_swapped = false;
1741 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1747 badness = fill_and_eval_dacs(codec, fill_hardwired,
1753 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1754 cfg->line_out_type, fill_hardwired, fill_mio_first,
1756 debug_show_configs(codec, cfg);
1757 if (badness < best_badness) {
1758 best_badness = badness;
1760 best_wired = fill_hardwired;
1761 best_mio = fill_mio_first;
1765 fill_mio_first = !fill_mio_first;
1766 if (!fill_mio_first)
1768 fill_hardwired = !fill_hardwired;
1769 if (!fill_hardwired)
1773 hp_spk_swapped = true;
1774 if (cfg->speaker_outs > 0 &&
1775 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1776 cfg->hp_outs = cfg->line_outs;
1777 memcpy(cfg->hp_pins, cfg->line_out_pins,
1778 sizeof(cfg->hp_pins));
1779 cfg->line_outs = cfg->speaker_outs;
1780 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1781 sizeof(cfg->speaker_pins));
1782 cfg->speaker_outs = 0;
1783 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1784 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1785 fill_hardwired = true;
1788 if (cfg->hp_outs > 0 &&
1789 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1790 cfg->speaker_outs = cfg->line_outs;
1791 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1792 sizeof(cfg->speaker_pins));
1793 cfg->line_outs = cfg->hp_outs;
1794 memcpy(cfg->line_out_pins, cfg->hp_pins,
1795 sizeof(cfg->hp_pins));
1797 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1798 cfg->line_out_type = AUTO_PIN_HP_OUT;
1799 fill_hardwired = true;
1806 debug_badness("==> restoring best_cfg\n");
1808 fill_and_eval_dacs(codec, best_wired, best_mio);
1810 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1811 cfg->line_out_type, best_wired, best_mio);
1812 debug_show_configs(codec, cfg);
1814 if (cfg->line_out_pins[0]) {
1815 struct nid_path *path;
1816 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1818 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1819 if (spec->vmaster_nid)
1820 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1821 HDA_OUTPUT, spec->vmaster_tlv);
1824 /* set initial pinctl targets */
1825 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1829 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1830 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1831 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1832 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1833 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1834 set_pin_targets(codec, cfg->speaker_outs,
1835 cfg->speaker_pins, val);
1838 /* clear indep_hp flag if not available */
1839 if (spec->indep_hp && !indep_hp_possible(codec))
1846 /* add playback controls from the parsed DAC table */
1847 static int create_multi_out_ctls(struct hda_codec *codec,
1848 const struct auto_pin_cfg *cfg)
1850 struct hda_gen_spec *spec = codec->spec;
1851 int i, err, noutputs;
1853 noutputs = cfg->line_outs;
1854 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1855 noutputs += spec->multi_ios;
1857 for (i = 0; i < noutputs; i++) {
1860 struct nid_path *path;
1862 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1866 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
1867 if (!name || !strcmp(name, "CLFE")) {
1869 err = add_vol_ctl(codec, "Center", 0, 1, path);
1872 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1876 err = add_stereo_vol(codec, name, index, path);
1881 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1882 if (!name || !strcmp(name, "CLFE")) {
1883 err = add_sw_ctl(codec, "Center", 0, 1, path);
1886 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1890 err = add_stereo_sw(codec, name, index, path);
1898 static int create_extra_out(struct hda_codec *codec, int path_idx,
1899 const char *pfx, int cidx)
1901 struct nid_path *path;
1904 path = snd_hda_get_path_from_idx(codec, path_idx);
1907 err = add_stereo_vol(codec, pfx, cidx, path);
1910 err = add_stereo_sw(codec, pfx, cidx, path);
1916 /* add playback controls for speaker and HP outputs */
1917 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1918 const int *paths, const char *pfx)
1922 for (i = 0; i < num_pins; i++) {
1927 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1928 name = "Bass Speaker";
1929 else if (num_pins >= 3) {
1930 snprintf(tmp, sizeof(tmp), "%s %s",
1931 pfx, channel_name[i]);
1937 err = create_extra_out(codec, paths[i], name, idx);
1944 static int create_hp_out_ctls(struct hda_codec *codec)
1946 struct hda_gen_spec *spec = codec->spec;
1947 return create_extra_outs(codec, spec->autocfg.hp_outs,
1952 static int create_speaker_out_ctls(struct hda_codec *codec)
1954 struct hda_gen_spec *spec = codec->spec;
1955 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1956 spec->speaker_paths,
1961 * independent HP controls
1964 static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack);
1965 static int indep_hp_info(struct snd_kcontrol *kcontrol,
1966 struct snd_ctl_elem_info *uinfo)
1968 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1971 static int indep_hp_get(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 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1980 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1981 int nomix_path_idx, int mix_path_idx,
1984 static int indep_hp_put(struct snd_kcontrol *kcontrol,
1985 struct snd_ctl_elem_value *ucontrol)
1987 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1988 struct hda_gen_spec *spec = codec->spec;
1989 unsigned int select = ucontrol->value.enumerated.item[0];
1992 mutex_lock(&spec->pcm_mutex);
1993 if (spec->active_streams) {
1998 if (spec->indep_hp_enabled != select) {
2000 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2001 dacp = &spec->private_dac_nids[0];
2003 dacp = &spec->multiout.hp_out_nid[0];
2005 /* update HP aamix paths in case it conflicts with indep HP */
2006 if (spec->have_aamix_ctl) {
2007 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2008 update_aamix_paths(codec, spec->aamix_mode,
2010 spec->aamix_out_paths[0],
2011 spec->autocfg.line_out_type);
2013 update_aamix_paths(codec, spec->aamix_mode,
2015 spec->aamix_out_paths[1],
2019 spec->indep_hp_enabled = select;
2020 if (spec->indep_hp_enabled)
2023 *dacp = spec->alt_dac_nid;
2025 call_hp_automute(codec, NULL);
2029 mutex_unlock(&spec->pcm_mutex);
2033 static const struct snd_kcontrol_new indep_hp_ctl = {
2034 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2035 .name = "Independent HP",
2036 .info = indep_hp_info,
2037 .get = indep_hp_get,
2038 .put = indep_hp_put,
2042 static int create_indep_hp_ctls(struct hda_codec *codec)
2044 struct hda_gen_spec *spec = codec->spec;
2047 if (!spec->indep_hp)
2049 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2050 dac = spec->multiout.dac_nids[0];
2052 dac = spec->multiout.hp_out_nid[0];
2058 spec->indep_hp_enabled = false;
2059 spec->alt_dac_nid = dac;
2060 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2066 * channel mode enum control
2069 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2070 struct snd_ctl_elem_info *uinfo)
2072 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2073 struct hda_gen_spec *spec = codec->spec;
2076 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2078 uinfo->value.enumerated.items = spec->multi_ios + 1;
2079 if (uinfo->value.enumerated.item > spec->multi_ios)
2080 uinfo->value.enumerated.item = spec->multi_ios;
2081 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2082 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2086 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2087 struct snd_ctl_elem_value *ucontrol)
2089 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2090 struct hda_gen_spec *spec = codec->spec;
2091 ucontrol->value.enumerated.item[0] =
2092 (spec->ext_channel_count - spec->min_channel_count) / 2;
2096 static inline struct nid_path *
2097 get_multiio_path(struct hda_codec *codec, int idx)
2099 struct hda_gen_spec *spec = codec->spec;
2100 return snd_hda_get_path_from_idx(codec,
2101 spec->out_paths[spec->autocfg.line_outs + idx]);
2104 static void update_automute_all(struct hda_codec *codec);
2106 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2107 * used for output paths
2109 static bool aamix_default(struct hda_gen_spec *spec)
2111 return !spec->have_aamix_ctl || spec->aamix_mode;
2114 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2116 struct hda_gen_spec *spec = codec->spec;
2117 hda_nid_t nid = spec->multi_io[idx].pin;
2118 struct nid_path *path;
2120 path = get_multiio_path(codec, idx);
2124 if (path->active == output)
2128 set_pin_target(codec, nid, PIN_OUT, true);
2129 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2130 set_pin_eapd(codec, nid, true);
2132 set_pin_eapd(codec, nid, false);
2133 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2134 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2135 path_power_down_sync(codec, path);
2138 /* update jack retasking in case it modifies any of them */
2139 update_automute_all(codec);
2144 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2145 struct snd_ctl_elem_value *ucontrol)
2147 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2148 struct hda_gen_spec *spec = codec->spec;
2151 ch = ucontrol->value.enumerated.item[0];
2152 if (ch < 0 || ch > spec->multi_ios)
2154 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2156 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2157 for (i = 0; i < spec->multi_ios; i++)
2158 set_multi_io(codec, i, i < ch);
2159 spec->multiout.max_channels = max(spec->ext_channel_count,
2160 spec->const_channel_count);
2161 if (spec->need_dac_fix)
2162 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2166 static const struct snd_kcontrol_new channel_mode_enum = {
2167 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2168 .name = "Channel Mode",
2169 .info = ch_mode_info,
2174 static int create_multi_channel_mode(struct hda_codec *codec)
2176 struct hda_gen_spec *spec = codec->spec;
2178 if (spec->multi_ios > 0) {
2179 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2186 * aamix loopback enable/disable switch
2189 #define loopback_mixing_info indep_hp_info
2191 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2192 struct snd_ctl_elem_value *ucontrol)
2194 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2195 struct hda_gen_spec *spec = codec->spec;
2196 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2200 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2201 int nomix_path_idx, int mix_path_idx,
2204 struct hda_gen_spec *spec = codec->spec;
2205 struct nid_path *nomix_path, *mix_path;
2207 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2208 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2209 if (!nomix_path || !mix_path)
2212 /* if HP aamix path is driven from a different DAC and the
2213 * independent HP mode is ON, can't turn on aamix path
2215 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2216 mix_path->path[0] != spec->alt_dac_nid)
2220 snd_hda_activate_path(codec, nomix_path, false, true);
2221 snd_hda_activate_path(codec, mix_path, true, true);
2222 path_power_down_sync(codec, nomix_path);
2224 snd_hda_activate_path(codec, mix_path, false, false);
2225 snd_hda_activate_path(codec, nomix_path, true, false);
2226 path_power_down_sync(codec, mix_path);
2230 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2231 struct snd_ctl_elem_value *ucontrol)
2233 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2234 struct hda_gen_spec *spec = codec->spec;
2235 unsigned int val = ucontrol->value.enumerated.item[0];
2237 if (val == spec->aamix_mode)
2239 spec->aamix_mode = val;
2240 update_aamix_paths(codec, val, spec->out_paths[0],
2241 spec->aamix_out_paths[0],
2242 spec->autocfg.line_out_type);
2243 update_aamix_paths(codec, val, spec->hp_paths[0],
2244 spec->aamix_out_paths[1],
2246 update_aamix_paths(codec, val, spec->speaker_paths[0],
2247 spec->aamix_out_paths[2],
2248 AUTO_PIN_SPEAKER_OUT);
2252 static const struct snd_kcontrol_new loopback_mixing_enum = {
2253 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2254 .name = "Loopback Mixing",
2255 .info = loopback_mixing_info,
2256 .get = loopback_mixing_get,
2257 .put = loopback_mixing_put,
2260 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2262 struct hda_gen_spec *spec = codec->spec;
2264 if (!spec->mixer_nid)
2266 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2267 spec->aamix_out_paths[2]))
2269 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2271 spec->have_aamix_ctl = 1;
2276 * shared headphone/mic handling
2279 static void call_update_outputs(struct hda_codec *codec);
2281 /* for shared I/O, change the pin-control accordingly */
2282 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2284 struct hda_gen_spec *spec = codec->spec;
2289 pin = spec->hp_mic_pin;
2290 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2293 val = snd_hda_codec_get_pin_target(codec, pin);
2303 val = snd_hda_get_default_vref(codec, pin);
2304 /* if the HP pin doesn't support VREF and the codec driver gives an
2305 * alternative pin, set up the VREF on that pin instead
2307 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2308 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2309 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2310 if (vref_val != AC_PINCTL_VREF_HIZ)
2311 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2312 PIN_IN | (as_mic ? vref_val : 0));
2315 if (!spec->hp_mic_jack_modes) {
2320 set_pin_target(codec, pin, val, true);
2321 call_hp_automute(codec, NULL);
2325 /* create a shared input with the headphone out */
2326 static int create_hp_mic(struct hda_codec *codec)
2328 struct hda_gen_spec *spec = codec->spec;
2329 struct auto_pin_cfg *cfg = &spec->autocfg;
2330 unsigned int defcfg;
2333 if (!spec->hp_mic) {
2334 if (spec->suppress_hp_mic_detect)
2336 /* automatic detection: only if no input or a single internal
2337 * input pin is found, try to detect the shared hp/mic
2339 if (cfg->num_inputs > 1)
2341 else if (cfg->num_inputs == 1) {
2342 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2343 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2348 spec->hp_mic = 0; /* clear once */
2349 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2353 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2354 nid = cfg->line_out_pins[0];
2355 else if (cfg->hp_outs > 0)
2356 nid = cfg->hp_pins[0];
2360 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2361 return 0; /* no input */
2363 cfg->inputs[cfg->num_inputs].pin = nid;
2364 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2365 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2368 spec->hp_mic_pin = nid;
2369 /* we can't handle auto-mic together with HP-mic */
2370 spec->suppress_auto_mic = 1;
2371 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2379 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2381 static const char * const out_jack_texts[] = {
2382 "Line Out", "Headphone Out",
2385 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2386 struct snd_ctl_elem_info *uinfo)
2388 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2391 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2392 struct snd_ctl_elem_value *ucontrol)
2394 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2395 hda_nid_t nid = kcontrol->private_value;
2396 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2397 ucontrol->value.enumerated.item[0] = 1;
2399 ucontrol->value.enumerated.item[0] = 0;
2403 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2404 struct snd_ctl_elem_value *ucontrol)
2406 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2407 hda_nid_t nid = kcontrol->private_value;
2410 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2411 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2413 snd_hda_set_pin_ctl_cache(codec, nid, val);
2417 static const struct snd_kcontrol_new out_jack_mode_enum = {
2418 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2419 .info = out_jack_mode_info,
2420 .get = out_jack_mode_get,
2421 .put = out_jack_mode_put,
2424 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2426 struct hda_gen_spec *spec = codec->spec;
2429 for (i = 0; i < spec->kctls.used; i++) {
2430 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2431 if (!strcmp(kctl->name, name) && kctl->index == idx)
2437 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2438 char *name, size_t name_len)
2440 struct hda_gen_spec *spec = codec->spec;
2443 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2444 strlcat(name, " Jack Mode", name_len);
2446 for (; find_kctl_name(codec, name, idx); idx++)
2450 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2452 struct hda_gen_spec *spec = codec->spec;
2453 if (spec->add_jack_modes) {
2454 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2455 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2461 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2464 struct hda_gen_spec *spec = codec->spec;
2467 for (i = 0; i < num_pins; i++) {
2468 hda_nid_t pin = pins[i];
2469 if (pin == spec->hp_mic_pin)
2471 if (get_out_jack_num_items(codec, pin) > 1) {
2472 struct snd_kcontrol_new *knew;
2474 get_jack_mode_name(codec, pin, name, sizeof(name));
2475 knew = snd_hda_gen_add_kctl(spec, name,
2476 &out_jack_mode_enum);
2479 knew->private_value = pin;
2490 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2493 static const char * const vref_texts[NUM_VREFS] = {
2494 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2495 "", "Mic 80pc Bias", "Mic 100pc Bias"
2498 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2500 unsigned int pincap;
2502 pincap = snd_hda_query_pin_caps(codec, pin);
2503 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2504 /* filter out unusual vrefs */
2505 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2509 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2510 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2512 unsigned int i, n = 0;
2514 for (i = 0; i < NUM_VREFS; i++) {
2515 if (vref_caps & (1 << i)) {
2524 /* convert back from the vref ctl index to the enum item index */
2525 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2527 unsigned int i, n = 0;
2529 for (i = 0; i < NUM_VREFS; i++) {
2532 if (vref_caps & (1 << i))
2538 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2539 struct snd_ctl_elem_info *uinfo)
2541 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2542 hda_nid_t nid = kcontrol->private_value;
2543 unsigned int vref_caps = get_vref_caps(codec, nid);
2545 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2547 /* set the right text */
2548 strcpy(uinfo->value.enumerated.name,
2549 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2553 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2554 struct snd_ctl_elem_value *ucontrol)
2556 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2557 hda_nid_t nid = kcontrol->private_value;
2558 unsigned int vref_caps = get_vref_caps(codec, nid);
2561 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2562 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2566 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2567 struct snd_ctl_elem_value *ucontrol)
2569 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2570 hda_nid_t nid = kcontrol->private_value;
2571 unsigned int vref_caps = get_vref_caps(codec, nid);
2572 unsigned int val, idx;
2574 val = snd_hda_codec_get_pin_target(codec, nid);
2575 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2576 if (idx == ucontrol->value.enumerated.item[0])
2579 val &= ~AC_PINCTL_VREFEN;
2580 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2581 snd_hda_set_pin_ctl_cache(codec, nid, val);
2585 static const struct snd_kcontrol_new in_jack_mode_enum = {
2586 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2587 .info = in_jack_mode_info,
2588 .get = in_jack_mode_get,
2589 .put = in_jack_mode_put,
2592 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2594 struct hda_gen_spec *spec = codec->spec;
2596 if (spec->add_jack_modes)
2597 nitems = hweight32(get_vref_caps(codec, pin));
2598 return nitems ? nitems : 1;
2601 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2603 struct hda_gen_spec *spec = codec->spec;
2604 struct snd_kcontrol_new *knew;
2606 unsigned int defcfg;
2608 if (pin == spec->hp_mic_pin)
2609 return 0; /* already done in create_out_jack_mode() */
2611 /* no jack mode for fixed pins */
2612 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2613 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2616 /* no multiple vref caps? */
2617 if (get_in_jack_num_items(codec, pin) <= 1)
2620 get_jack_mode_name(codec, pin, name, sizeof(name));
2621 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2624 knew->private_value = pin;
2629 * HP/mic shared jack mode
2631 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2632 struct snd_ctl_elem_info *uinfo)
2634 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2635 hda_nid_t nid = kcontrol->private_value;
2636 int out_jacks = get_out_jack_num_items(codec, nid);
2637 int in_jacks = get_in_jack_num_items(codec, nid);
2638 const char *text = NULL;
2641 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2643 uinfo->value.enumerated.items = out_jacks + in_jacks;
2644 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2645 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2646 idx = uinfo->value.enumerated.item;
2647 if (idx < out_jacks) {
2649 text = out_jack_texts[idx];
2651 text = "Headphone Out";
2655 unsigned int vref_caps = get_vref_caps(codec, nid);
2656 text = vref_texts[get_vref_idx(vref_caps, idx)];
2661 strcpy(uinfo->value.enumerated.name, text);
2665 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2667 int out_jacks = get_out_jack_num_items(codec, nid);
2668 int in_jacks = get_in_jack_num_items(codec, nid);
2669 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2672 if (val & PIN_OUT) {
2673 if (out_jacks > 1 && val == PIN_HP)
2675 } else if (val & PIN_IN) {
2678 unsigned int vref_caps = get_vref_caps(codec, nid);
2679 val &= AC_PINCTL_VREFEN;
2680 idx += cvt_from_vref_idx(vref_caps, val);
2686 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2687 struct snd_ctl_elem_value *ucontrol)
2689 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2690 hda_nid_t nid = kcontrol->private_value;
2691 ucontrol->value.enumerated.item[0] =
2692 get_cur_hp_mic_jack_mode(codec, nid);
2696 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2697 struct snd_ctl_elem_value *ucontrol)
2699 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2700 hda_nid_t nid = kcontrol->private_value;
2701 int out_jacks = get_out_jack_num_items(codec, nid);
2702 int in_jacks = get_in_jack_num_items(codec, nid);
2703 unsigned int val, oldval, idx;
2705 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2706 idx = ucontrol->value.enumerated.item[0];
2710 if (idx < out_jacks) {
2712 val = idx ? PIN_HP : PIN_OUT;
2718 unsigned int vref_caps = get_vref_caps(codec, nid);
2719 val = snd_hda_codec_get_pin_target(codec, nid);
2720 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2721 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
2723 val = snd_hda_get_default_vref(codec, nid) | PIN_IN;
2725 snd_hda_set_pin_ctl_cache(codec, nid, val);
2726 call_hp_automute(codec, NULL);
2731 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2732 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2733 .info = hp_mic_jack_mode_info,
2734 .get = hp_mic_jack_mode_get,
2735 .put = hp_mic_jack_mode_put,
2738 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2740 struct hda_gen_spec *spec = codec->spec;
2741 struct snd_kcontrol_new *knew;
2743 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2744 &hp_mic_jack_mode_enum);
2747 knew->private_value = pin;
2748 spec->hp_mic_jack_modes = 1;
2756 /* add the powersave loopback-list entry */
2757 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2759 struct hda_amp_list *list;
2761 list = snd_array_new(&spec->loopback_list);
2765 list->dir = HDA_INPUT;
2767 spec->loopback.amplist = spec->loopback_list.list;
2771 /* return true if either a volume or a mute amp is found for the given
2772 * aamix path; the amp has to be either in the mixer node or its direct leaf
2774 static bool look_for_mix_leaf_ctls(struct hda_codec *codec, hda_nid_t mix_nid,
2775 hda_nid_t pin, unsigned int *mix_val,
2776 unsigned int *mute_val)
2779 const hda_nid_t *list;
2782 idx = snd_hda_get_conn_index(codec, mix_nid, pin, true);
2786 *mix_val = *mute_val = 0;
2787 if (nid_has_volume(codec, mix_nid, HDA_INPUT))
2788 *mix_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2789 if (nid_has_mute(codec, mix_nid, HDA_INPUT))
2790 *mute_val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2791 if (*mix_val && *mute_val)
2794 /* check leaf node */
2795 num_conns = snd_hda_get_conn_list(codec, mix_nid, &list);
2796 if (num_conns < idx)
2799 if (!*mix_val && nid_has_volume(codec, nid, HDA_OUTPUT))
2800 *mix_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2801 if (!*mute_val && nid_has_mute(codec, nid, HDA_OUTPUT))
2802 *mute_val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2804 return *mix_val || *mute_val;
2807 /* create input playback/capture controls for the given pin */
2808 static int new_analog_input(struct hda_codec *codec, int input_idx,
2809 hda_nid_t pin, const char *ctlname, int ctlidx,
2812 struct hda_gen_spec *spec = codec->spec;
2813 struct nid_path *path;
2814 unsigned int mix_val, mute_val;
2817 if (!look_for_mix_leaf_ctls(codec, mix_nid, pin, &mix_val, &mute_val))
2820 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
2823 print_nid_path("loopback", path);
2824 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
2826 idx = path->idx[path->depth - 1];
2828 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, mix_val);
2831 path->ctls[NID_PATH_VOL_CTL] = mix_val;
2835 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, mute_val);
2838 path->ctls[NID_PATH_MUTE_CTL] = mute_val;
2841 path->active = true;
2842 err = add_loopback_list(spec, mix_nid, idx);
2846 if (spec->mixer_nid != spec->mixer_merge_nid &&
2847 !spec->loopback_merge_path) {
2848 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2849 spec->mixer_merge_nid, 0);
2851 print_nid_path("loopback-merge", path);
2852 path->active = true;
2853 spec->loopback_merge_path =
2854 snd_hda_get_path_idx(codec, path);
2861 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2863 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2864 return (pincap & AC_PINCAP_IN) != 0;
2867 /* Parse the codec tree and retrieve ADCs */
2868 static int fill_adc_nids(struct hda_codec *codec)
2870 struct hda_gen_spec *spec = codec->spec;
2872 hda_nid_t *adc_nids = spec->adc_nids;
2873 int max_nums = ARRAY_SIZE(spec->adc_nids);
2876 nid = codec->start_nid;
2877 for (i = 0; i < codec->num_nodes; i++, nid++) {
2878 unsigned int caps = get_wcaps(codec, nid);
2879 int type = get_wcaps_type(caps);
2881 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2883 adc_nids[nums] = nid;
2884 if (++nums >= max_nums)
2887 spec->num_adc_nids = nums;
2889 /* copy the detected ADCs to all_adcs[] */
2890 spec->num_all_adcs = nums;
2891 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2896 /* filter out invalid adc_nids that don't give all active input pins;
2897 * if needed, check whether dynamic ADC-switching is available
2899 static int check_dyn_adc_switch(struct hda_codec *codec)
2901 struct hda_gen_spec *spec = codec->spec;
2902 struct hda_input_mux *imux = &spec->input_mux;
2903 unsigned int ok_bits;
2908 for (n = 0; n < spec->num_adc_nids; n++) {
2909 for (i = 0; i < imux->num_items; i++) {
2910 if (!spec->input_paths[i][n])
2913 if (i >= imux->num_items) {
2914 ok_bits |= (1 << n);
2920 /* check whether ADC-switch is possible */
2921 for (i = 0; i < imux->num_items; i++) {
2922 for (n = 0; n < spec->num_adc_nids; n++) {
2923 if (spec->input_paths[i][n]) {
2924 spec->dyn_adc_idx[i] = n;
2930 snd_printdd("hda-codec: enabling ADC switching\n");
2931 spec->dyn_adc_switch = 1;
2932 } else if (nums != spec->num_adc_nids) {
2933 /* shrink the invalid adcs and input paths */
2935 for (n = 0; n < spec->num_adc_nids; n++) {
2936 if (!(ok_bits & (1 << n)))
2939 spec->adc_nids[nums] = spec->adc_nids[n];
2940 for (i = 0; i < imux->num_items; i++) {
2941 invalidate_nid_path(codec,
2942 spec->input_paths[i][nums]);
2943 spec->input_paths[i][nums] =
2944 spec->input_paths[i][n];
2949 spec->num_adc_nids = nums;
2952 if (imux->num_items == 1 ||
2953 (imux->num_items == 2 && spec->hp_mic)) {
2954 snd_printdd("hda-codec: reducing to a single ADC\n");
2955 spec->num_adc_nids = 1; /* reduce to a single ADC */
2958 /* single index for individual volumes ctls */
2959 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2960 spec->num_adc_nids = 1;
2965 /* parse capture source paths from the given pin and create imux items */
2966 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2967 int cfg_idx, int num_adcs,
2968 const char *label, int anchor)
2970 struct hda_gen_spec *spec = codec->spec;
2971 struct hda_input_mux *imux = &spec->input_mux;
2972 int imux_idx = imux->num_items;
2973 bool imux_added = false;
2976 for (c = 0; c < num_adcs; c++) {
2977 struct nid_path *path;
2978 hda_nid_t adc = spec->adc_nids[c];
2980 if (!is_reachable_path(codec, pin, adc))
2982 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2985 print_nid_path("input", path);
2986 spec->input_paths[imux_idx][c] =
2987 snd_hda_get_path_idx(codec, path);
2990 if (spec->hp_mic_pin == pin)
2991 spec->hp_mic_mux_idx = imux->num_items;
2992 spec->imux_pins[imux->num_items] = pin;
2993 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
3002 * create playback/capture controls for input pins
3005 /* fill the label for each input at first */
3006 static int fill_input_pin_labels(struct hda_codec *codec)
3008 struct hda_gen_spec *spec = codec->spec;
3009 const struct auto_pin_cfg *cfg = &spec->autocfg;
3012 for (i = 0; i < cfg->num_inputs; i++) {
3013 hda_nid_t pin = cfg->inputs[i].pin;
3017 if (!is_input_pin(codec, pin))
3020 label = hda_get_autocfg_input_label(codec, cfg, i);
3022 for (j = i - 1; j >= 0; j--) {
3023 if (spec->input_labels[j] &&
3024 !strcmp(spec->input_labels[j], label)) {
3025 idx = spec->input_label_idxs[j] + 1;
3030 spec->input_labels[i] = label;
3031 spec->input_label_idxs[i] = idx;
3037 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3039 static int create_input_ctls(struct hda_codec *codec)
3041 struct hda_gen_spec *spec = codec->spec;
3042 const struct auto_pin_cfg *cfg = &spec->autocfg;
3043 hda_nid_t mixer = spec->mixer_nid;
3048 num_adcs = fill_adc_nids(codec);
3052 err = fill_input_pin_labels(codec);
3056 for (i = 0; i < cfg->num_inputs; i++) {
3059 pin = cfg->inputs[i].pin;
3060 if (!is_input_pin(codec, pin))
3064 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3065 val |= snd_hda_get_default_vref(codec, pin);
3066 if (pin != spec->hp_mic_pin)
3067 set_pin_target(codec, pin, val, false);
3070 if (is_reachable_path(codec, pin, mixer)) {
3071 err = new_analog_input(codec, i, pin,
3072 spec->input_labels[i],
3073 spec->input_label_idxs[i],
3080 err = parse_capture_source(codec, pin, i, num_adcs,
3081 spec->input_labels[i], -mixer);
3085 if (spec->add_jack_modes) {
3086 err = create_in_jack_mode(codec, pin);
3092 if (mixer && spec->add_stereo_mix_input) {
3093 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3107 /* get the input path specified by the given adc and imux indices */
3108 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3110 struct hda_gen_spec *spec = codec->spec;
3111 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3115 if (spec->dyn_adc_switch)
3116 adc_idx = spec->dyn_adc_idx[imux_idx];
3117 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3121 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3124 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3127 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3128 struct snd_ctl_elem_info *uinfo)
3130 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3131 struct hda_gen_spec *spec = codec->spec;
3132 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3135 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3136 struct snd_ctl_elem_value *ucontrol)
3138 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3139 struct hda_gen_spec *spec = codec->spec;
3140 /* the ctls are created at once with multiple counts */
3141 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3143 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3147 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3148 struct snd_ctl_elem_value *ucontrol)
3150 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3151 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3152 return mux_select(codec, adc_idx,
3153 ucontrol->value.enumerated.item[0]);
3156 static const struct snd_kcontrol_new cap_src_temp = {
3157 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3158 .name = "Input Source",
3159 .info = mux_enum_info,
3160 .get = mux_enum_get,
3161 .put = mux_enum_put,
3165 * capture volume and capture switch ctls
3168 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3169 struct snd_ctl_elem_value *ucontrol);
3171 /* call the given amp update function for all amps in the imux list at once */
3172 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3173 struct snd_ctl_elem_value *ucontrol,
3174 put_call_t func, int type)
3176 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3177 struct hda_gen_spec *spec = codec->spec;
3178 const struct hda_input_mux *imux;
3179 struct nid_path *path;
3180 int i, adc_idx, err = 0;
3182 imux = &spec->input_mux;
3183 adc_idx = kcontrol->id.index;
3184 mutex_lock(&codec->control_mutex);
3185 /* we use the cache-only update at first since multiple input paths
3186 * may shared the same amp; by updating only caches, the redundant
3187 * writes to hardware can be reduced.
3189 codec->cached_write = 1;
3190 for (i = 0; i < imux->num_items; i++) {
3191 path = get_input_path(codec, adc_idx, i);
3192 if (!path || !path->ctls[type])
3194 kcontrol->private_value = path->ctls[type];
3195 err = func(kcontrol, ucontrol);
3200 codec->cached_write = 0;
3201 mutex_unlock(&codec->control_mutex);
3202 snd_hda_codec_flush_cache(codec); /* flush the updates */
3203 if (err >= 0 && spec->cap_sync_hook)
3204 spec->cap_sync_hook(codec, ucontrol);
3208 /* capture volume ctl callbacks */
3209 #define cap_vol_info snd_hda_mixer_amp_volume_info
3210 #define cap_vol_get snd_hda_mixer_amp_volume_get
3211 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3213 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3214 struct snd_ctl_elem_value *ucontrol)
3216 return cap_put_caller(kcontrol, ucontrol,
3217 snd_hda_mixer_amp_volume_put,
3221 static const struct snd_kcontrol_new cap_vol_temp = {
3222 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3223 .name = "Capture Volume",
3224 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3225 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3226 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3227 .info = cap_vol_info,
3230 .tlv = { .c = cap_vol_tlv },
3233 /* capture switch ctl callbacks */
3234 #define cap_sw_info snd_ctl_boolean_stereo_info
3235 #define cap_sw_get snd_hda_mixer_amp_switch_get
3237 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3238 struct snd_ctl_elem_value *ucontrol)
3240 return cap_put_caller(kcontrol, ucontrol,
3241 snd_hda_mixer_amp_switch_put,
3245 static const struct snd_kcontrol_new cap_sw_temp = {
3246 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3247 .name = "Capture Switch",
3248 .info = cap_sw_info,
3253 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3258 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3259 for (depth = 0; depth < 3; depth++) {
3260 if (depth >= path->depth)
3262 i = path->depth - depth - 1;
3263 nid = path->path[i];
3264 if (!path->ctls[NID_PATH_VOL_CTL]) {
3265 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3266 path->ctls[NID_PATH_VOL_CTL] =
3267 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3268 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3269 int idx = path->idx[i];
3270 if (!depth && codec->single_adc_amp)
3272 path->ctls[NID_PATH_VOL_CTL] =
3273 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3276 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3277 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3278 path->ctls[NID_PATH_MUTE_CTL] =
3279 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3280 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3281 int idx = path->idx[i];
3282 if (!depth && codec->single_adc_amp)
3284 path->ctls[NID_PATH_MUTE_CTL] =
3285 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3292 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3294 struct hda_gen_spec *spec = codec->spec;
3295 struct auto_pin_cfg *cfg = &spec->autocfg;
3299 if (!spec->inv_dmic_split)
3301 for (i = 0; i < cfg->num_inputs; i++) {
3302 if (cfg->inputs[i].pin != nid)
3304 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3306 val = snd_hda_codec_get_pincfg(codec, nid);
3307 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3312 /* capture switch put callback for a single control with hook call */
3313 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3314 struct snd_ctl_elem_value *ucontrol)
3316 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3317 struct hda_gen_spec *spec = codec->spec;
3320 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3324 if (spec->cap_sync_hook)
3325 spec->cap_sync_hook(codec, ucontrol);
3330 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3331 int idx, bool is_switch, unsigned int ctl,
3334 struct hda_gen_spec *spec = codec->spec;
3336 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3337 const char *sfx = is_switch ? "Switch" : "Volume";
3338 unsigned int chs = inv_dmic ? 1 : 3;
3339 struct snd_kcontrol_new *knew;
3345 snprintf(tmpname, sizeof(tmpname),
3346 "%s Capture %s", label, sfx);
3348 snprintf(tmpname, sizeof(tmpname),
3350 knew = add_control(spec, type, tmpname, idx,
3351 amp_val_replace_channels(ctl, chs));
3355 knew->put = cap_single_sw_put;
3359 /* Make independent right kcontrol */
3361 snprintf(tmpname, sizeof(tmpname),
3362 "Inverted %s Capture %s", label, sfx);
3364 snprintf(tmpname, sizeof(tmpname),
3365 "Inverted Capture %s", sfx);
3366 knew = add_control(spec, type, tmpname, idx,
3367 amp_val_replace_channels(ctl, 2));
3371 knew->put = cap_single_sw_put;
3375 /* create single (and simple) capture volume and switch controls */
3376 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3377 unsigned int vol_ctl, unsigned int sw_ctl,
3381 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3384 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3390 /* create bound capture volume and switch controls */
3391 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3392 unsigned int vol_ctl, unsigned int sw_ctl)
3394 struct hda_gen_spec *spec = codec->spec;
3395 struct snd_kcontrol_new *knew;
3398 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3402 knew->private_value = vol_ctl;
3403 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3406 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3410 knew->private_value = sw_ctl;
3411 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3416 /* return the vol ctl when used first in the imux list */
3417 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3419 struct nid_path *path;
3423 path = get_input_path(codec, 0, idx);
3426 ctl = path->ctls[type];
3429 for (i = 0; i < idx - 1; i++) {
3430 path = get_input_path(codec, 0, i);
3431 if (path && path->ctls[type] == ctl)
3437 /* create individual capture volume and switch controls per input */
3438 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3440 struct hda_gen_spec *spec = codec->spec;
3441 struct hda_input_mux *imux = &spec->input_mux;
3444 for (i = 0; i < imux->num_items; i++) {
3448 idx = imux->items[i].index;
3449 if (idx >= spec->autocfg.num_inputs)
3451 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3453 for (type = 0; type < 2; type++) {
3454 err = add_single_cap_ctl(codec,
3455 spec->input_labels[idx],
3456 spec->input_label_idxs[idx],
3458 get_first_cap_ctl(codec, i, type),
3467 static int create_capture_mixers(struct hda_codec *codec)
3469 struct hda_gen_spec *spec = codec->spec;
3470 struct hda_input_mux *imux = &spec->input_mux;
3471 int i, n, nums, err;
3473 if (spec->dyn_adc_switch)
3476 nums = spec->num_adc_nids;
3478 if (!spec->auto_mic && imux->num_items > 1) {
3479 struct snd_kcontrol_new *knew;
3481 name = nums > 1 ? "Input Source" : "Capture Source";
3482 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3488 for (n = 0; n < nums; n++) {
3490 bool multi_cap_vol = spec->multi_cap_vol;
3491 bool inv_dmic = false;
3495 for (i = 0; i < imux->num_items; i++) {
3496 struct nid_path *path;
3497 path = get_input_path(codec, n, i);
3500 parse_capvol_in_path(codec, path);
3502 vol = path->ctls[NID_PATH_VOL_CTL];
3503 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3505 if (!same_amp_caps(codec, vol,
3506 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3507 multi_cap_vol = true;
3510 sw = path->ctls[NID_PATH_MUTE_CTL];
3511 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3513 if (!same_amp_caps(codec, sw,
3514 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3515 multi_cap_vol = true;
3517 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3522 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3524 else if (!multi_cap_vol && !inv_dmic)
3525 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3527 err = create_multi_cap_vol_ctl(codec);
3536 * add mic boosts if needed
3539 /* check whether the given amp is feasible as a boost volume */
3540 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3545 if (!nid_has_volume(codec, nid, dir) ||
3546 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3547 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3550 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3551 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3557 /* look for a boost amp in a widget close to the pin */
3558 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3559 struct nid_path *path)
3561 unsigned int val = 0;
3565 for (depth = 0; depth < 3; depth++) {
3566 if (depth >= path->depth - 1)
3568 nid = path->path[depth];
3569 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3570 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3572 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3573 path->idx[depth])) {
3574 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3583 static int parse_mic_boost(struct hda_codec *codec)
3585 struct hda_gen_spec *spec = codec->spec;
3586 struct auto_pin_cfg *cfg = &spec->autocfg;
3587 struct hda_input_mux *imux = &spec->input_mux;
3590 if (!spec->num_adc_nids)
3593 for (i = 0; i < imux->num_items; i++) {
3594 struct nid_path *path;
3597 char boost_label[44];
3599 idx = imux->items[i].index;
3600 if (idx >= imux->num_items)
3603 /* check only line-in and mic pins */
3604 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3607 path = get_input_path(codec, 0, i);
3611 val = look_for_boost_amp(codec, path);
3615 /* create a boost control */
3616 snprintf(boost_label, sizeof(boost_label),
3617 "%s Boost Volume", spec->input_labels[idx]);
3618 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3619 spec->input_label_idxs[idx], val))
3622 path->ctls[NID_PATH_BOOST_CTL] = val;
3628 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3630 static void parse_digital(struct hda_codec *codec)
3632 struct hda_gen_spec *spec = codec->spec;
3633 struct nid_path *path;
3635 hda_nid_t dig_nid, pin;
3637 /* support multiple SPDIFs; the secondary is set up as a slave */
3639 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3640 pin = spec->autocfg.dig_out_pins[i];
3641 dig_nid = look_for_dac(codec, pin, true);
3644 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3647 print_nid_path("digout", path);
3648 path->active = true;
3649 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3650 set_pin_target(codec, pin, PIN_OUT, false);
3652 spec->multiout.dig_out_nid = dig_nid;
3653 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3655 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3656 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3658 spec->slave_dig_outs[nums - 1] = dig_nid;
3663 if (spec->autocfg.dig_in_pin) {
3664 pin = spec->autocfg.dig_in_pin;
3665 dig_nid = codec->start_nid;
3666 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3667 unsigned int wcaps = get_wcaps(codec, dig_nid);
3668 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3670 if (!(wcaps & AC_WCAP_DIGITAL))
3672 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3674 print_nid_path("digin", path);
3675 path->active = true;
3676 spec->dig_in_nid = dig_nid;
3677 spec->digin_path = snd_hda_get_path_idx(codec, path);
3678 set_pin_target(codec, pin, PIN_IN, false);
3687 * input MUX handling
3690 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3692 /* select the given imux item; either unmute exclusively or select the route */
3693 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3696 struct hda_gen_spec *spec = codec->spec;
3697 const struct hda_input_mux *imux;
3698 struct nid_path *old_path, *path;
3700 imux = &spec->input_mux;
3701 if (!imux->num_items)
3704 if (idx >= imux->num_items)
3705 idx = imux->num_items - 1;
3706 if (spec->cur_mux[adc_idx] == idx)
3709 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3712 if (old_path->active)
3713 snd_hda_activate_path(codec, old_path, false, false);
3715 spec->cur_mux[adc_idx] = idx;
3718 update_hp_mic(codec, adc_idx, false);
3720 if (spec->dyn_adc_switch)
3721 dyn_adc_pcm_resetup(codec, idx);
3723 path = get_input_path(codec, adc_idx, idx);
3728 snd_hda_activate_path(codec, path, true, false);
3729 if (spec->cap_sync_hook)
3730 spec->cap_sync_hook(codec, NULL);
3731 path_power_down_sync(codec, old_path);
3737 * Jack detections for HP auto-mute and mic-switch
3740 /* check each pin in the given array; returns true if any of them is plugged */
3741 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3745 for (i = 0; i < num_pins; i++) {
3746 hda_nid_t nid = pins[i];
3749 /* don't detect pins retasked as inputs */
3750 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3752 present |= snd_hda_jack_detect(codec, nid);
3757 /* standard HP/line-out auto-mute helper */
3758 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
3761 struct hda_gen_spec *spec = codec->spec;
3764 for (i = 0; i < num_pins; i++) {
3765 hda_nid_t nid = pins[i];
3766 unsigned int val, oldval;
3769 oldval = snd_hda_codec_get_pin_target(codec, nid);
3770 if (oldval & PIN_IN)
3771 continue; /* no mute for inputs */
3772 /* don't reset VREF value in case it's controlling
3773 * the amp (see alc861_fixup_asus_amp_vref_0f())
3775 if (spec->keep_vref_in_automute)
3776 val = oldval & ~PIN_HP;
3781 /* here we call update_pin_ctl() so that the pinctl is changed
3782 * without changing the pinctl target value;
3783 * the original target value will be still referred at the
3784 * init / resume again
3786 update_pin_ctl(codec, nid, val);
3787 set_pin_eapd(codec, nid, !mute);
3791 /* Toggle outputs muting */
3792 void snd_hda_gen_update_outputs(struct hda_codec *codec)
3794 struct hda_gen_spec *spec = codec->spec;
3797 /* Control HP pins/amps depending on master_mute state;
3798 * in general, HP pins/amps control should be enabled in all cases,
3799 * but currently set only for master_mute, just to be safe
3801 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3802 spec->autocfg.hp_pins, spec->master_mute);
3804 if (!spec->automute_speaker)
3807 on = spec->hp_jack_present | spec->line_jack_present;
3808 on |= spec->master_mute;
3809 spec->speaker_muted = on;
3810 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
3811 spec->autocfg.speaker_pins, on);
3813 /* toggle line-out mutes if needed, too */
3814 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3815 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3816 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3818 if (!spec->automute_lo)
3821 on = spec->hp_jack_present;
3822 on |= spec->master_mute;
3823 spec->line_out_muted = on;
3824 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3825 spec->autocfg.line_out_pins, on);
3827 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
3829 static void call_update_outputs(struct hda_codec *codec)
3831 struct hda_gen_spec *spec = codec->spec;
3832 if (spec->automute_hook)
3833 spec->automute_hook(codec);
3835 snd_hda_gen_update_outputs(codec);
3838 /* standard HP-automute helper */
3839 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3841 struct hda_gen_spec *spec = codec->spec;
3842 hda_nid_t *pins = spec->autocfg.hp_pins;
3843 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
3845 /* No detection for the first HP jack during indep-HP mode */
3846 if (spec->indep_hp_enabled) {
3851 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
3852 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3854 call_update_outputs(codec);
3856 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
3858 /* standard line-out-automute helper */
3859 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3861 struct hda_gen_spec *spec = codec->spec;
3863 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3865 /* check LO jack only when it's different from HP */
3866 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3869 spec->line_jack_present =
3870 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3871 spec->autocfg.line_out_pins);
3872 if (!spec->automute_speaker || !spec->detect_lo)
3874 call_update_outputs(codec);
3876 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
3878 /* standard mic auto-switch helper */
3879 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
3881 struct hda_gen_spec *spec = codec->spec;
3884 if (!spec->auto_mic)
3887 for (i = spec->am_num_entries - 1; i > 0; i--) {
3888 hda_nid_t pin = spec->am_entry[i].pin;
3889 /* don't detect pins retasked as outputs */
3890 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3892 if (snd_hda_jack_detect(codec, pin)) {
3893 mux_select(codec, 0, spec->am_entry[i].idx);
3897 mux_select(codec, 0, spec->am_entry[0].idx);
3899 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
3901 /* call appropriate hooks */
3902 static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3904 struct hda_gen_spec *spec = codec->spec;
3905 if (spec->hp_automute_hook)
3906 spec->hp_automute_hook(codec, jack);
3908 snd_hda_gen_hp_automute(codec, jack);
3911 static void call_line_automute(struct hda_codec *codec,
3912 struct hda_jack_tbl *jack)
3914 struct hda_gen_spec *spec = codec->spec;
3915 if (spec->line_automute_hook)
3916 spec->line_automute_hook(codec, jack);
3918 snd_hda_gen_line_automute(codec, jack);
3921 static void call_mic_autoswitch(struct hda_codec *codec,
3922 struct hda_jack_tbl *jack)
3924 struct hda_gen_spec *spec = codec->spec;
3925 if (spec->mic_autoswitch_hook)
3926 spec->mic_autoswitch_hook(codec, jack);
3928 snd_hda_gen_mic_autoswitch(codec, jack);
3931 /* update jack retasking */
3932 static void update_automute_all(struct hda_codec *codec)
3934 call_hp_automute(codec, NULL);
3935 call_line_automute(codec, NULL);
3936 call_mic_autoswitch(codec, NULL);
3940 * Auto-Mute mode mixer enum support
3942 static int automute_mode_info(struct snd_kcontrol *kcontrol,
3943 struct snd_ctl_elem_info *uinfo)
3945 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3946 struct hda_gen_spec *spec = codec->spec;
3947 static const char * const texts3[] = {
3948 "Disabled", "Speaker Only", "Line Out+Speaker"
3951 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3952 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3953 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3956 static int automute_mode_get(struct snd_kcontrol *kcontrol,
3957 struct snd_ctl_elem_value *ucontrol)
3959 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3960 struct hda_gen_spec *spec = codec->spec;
3961 unsigned int val = 0;
3962 if (spec->automute_speaker)
3964 if (spec->automute_lo)
3967 ucontrol->value.enumerated.item[0] = val;
3971 static int automute_mode_put(struct snd_kcontrol *kcontrol,
3972 struct snd_ctl_elem_value *ucontrol)
3974 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3975 struct hda_gen_spec *spec = codec->spec;
3977 switch (ucontrol->value.enumerated.item[0]) {
3979 if (!spec->automute_speaker && !spec->automute_lo)
3981 spec->automute_speaker = 0;
3982 spec->automute_lo = 0;
3985 if (spec->automute_speaker_possible) {
3986 if (!spec->automute_lo && spec->automute_speaker)
3988 spec->automute_speaker = 1;
3989 spec->automute_lo = 0;
3990 } else if (spec->automute_lo_possible) {
3991 if (spec->automute_lo)
3993 spec->automute_lo = 1;
3998 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4000 if (spec->automute_speaker && spec->automute_lo)
4002 spec->automute_speaker = 1;
4003 spec->automute_lo = 1;
4008 call_update_outputs(codec);
4012 static const struct snd_kcontrol_new automute_mode_enum = {
4013 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4014 .name = "Auto-Mute Mode",
4015 .info = automute_mode_info,
4016 .get = automute_mode_get,
4017 .put = automute_mode_put,
4020 static int add_automute_mode_enum(struct hda_codec *codec)
4022 struct hda_gen_spec *spec = codec->spec;
4024 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4030 * Check the availability of HP/line-out auto-mute;
4031 * Set up appropriately if really supported
4033 static int check_auto_mute_availability(struct hda_codec *codec)
4035 struct hda_gen_spec *spec = codec->spec;
4036 struct auto_pin_cfg *cfg = &spec->autocfg;
4040 if (spec->suppress_auto_mute)
4043 if (cfg->hp_pins[0])
4045 if (cfg->line_out_pins[0])
4047 if (cfg->speaker_pins[0])
4049 if (present < 2) /* need two different output types */
4052 if (!cfg->speaker_pins[0] &&
4053 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4054 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4055 sizeof(cfg->speaker_pins));
4056 cfg->speaker_outs = cfg->line_outs;
4059 if (!cfg->hp_pins[0] &&
4060 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4061 memcpy(cfg->hp_pins, cfg->line_out_pins,
4062 sizeof(cfg->hp_pins));
4063 cfg->hp_outs = cfg->line_outs;
4066 for (i = 0; i < cfg->hp_outs; i++) {
4067 hda_nid_t nid = cfg->hp_pins[i];
4068 if (!is_jack_detectable(codec, nid))
4070 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
4072 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
4074 spec->detect_hp = 1;
4077 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4078 if (cfg->speaker_outs)
4079 for (i = 0; i < cfg->line_outs; i++) {
4080 hda_nid_t nid = cfg->line_out_pins[i];
4081 if (!is_jack_detectable(codec, nid))
4083 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
4084 snd_hda_jack_detect_enable_callback(codec, nid,
4085 HDA_GEN_FRONT_EVENT,
4086 call_line_automute);
4087 spec->detect_lo = 1;
4089 spec->automute_lo_possible = spec->detect_hp;
4092 spec->automute_speaker_possible = cfg->speaker_outs &&
4093 (spec->detect_hp || spec->detect_lo);
4095 spec->automute_lo = spec->automute_lo_possible;
4096 spec->automute_speaker = spec->automute_speaker_possible;
4098 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4099 /* create a control for automute mode */
4100 err = add_automute_mode_enum(codec);
4107 /* check whether all auto-mic pins are valid; setup indices if OK */
4108 static bool auto_mic_check_imux(struct hda_codec *codec)
4110 struct hda_gen_spec *spec = codec->spec;
4111 const struct hda_input_mux *imux;
4114 imux = &spec->input_mux;
4115 for (i = 0; i < spec->am_num_entries; i++) {
4116 spec->am_entry[i].idx =
4117 find_idx_in_nid_list(spec->am_entry[i].pin,
4118 spec->imux_pins, imux->num_items);
4119 if (spec->am_entry[i].idx < 0)
4120 return false; /* no corresponding imux */
4123 /* we don't need the jack detection for the first pin */
4124 for (i = 1; i < spec->am_num_entries; i++)
4125 snd_hda_jack_detect_enable_callback(codec,
4126 spec->am_entry[i].pin,
4128 call_mic_autoswitch);
4132 static int compare_attr(const void *ap, const void *bp)
4134 const struct automic_entry *a = ap;
4135 const struct automic_entry *b = bp;
4136 return (int)(a->attr - b->attr);
4140 * Check the availability of auto-mic switch;
4141 * Set up if really supported
4143 static int check_auto_mic_availability(struct hda_codec *codec)
4145 struct hda_gen_spec *spec = codec->spec;
4146 struct auto_pin_cfg *cfg = &spec->autocfg;
4150 if (spec->suppress_auto_mic)
4155 for (i = 0; i < cfg->num_inputs; i++) {
4156 hda_nid_t nid = cfg->inputs[i].pin;
4158 attr = snd_hda_codec_get_pincfg(codec, nid);
4159 attr = snd_hda_get_input_pin_attr(attr);
4160 if (types & (1 << attr))
4161 return 0; /* already occupied */
4163 case INPUT_PIN_ATTR_INT:
4164 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4165 return 0; /* invalid type */
4167 case INPUT_PIN_ATTR_UNUSED:
4168 return 0; /* invalid entry */
4170 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4171 return 0; /* invalid type */
4172 if (!spec->line_in_auto_switch &&
4173 cfg->inputs[i].type != AUTO_PIN_MIC)
4174 return 0; /* only mic is allowed */
4175 if (!is_jack_detectable(codec, nid))
4176 return 0; /* no unsol support */
4179 if (num_pins >= MAX_AUTO_MIC_PINS)
4181 types |= (1 << attr);
4182 spec->am_entry[num_pins].pin = nid;
4183 spec->am_entry[num_pins].attr = attr;
4190 spec->am_num_entries = num_pins;
4191 /* sort the am_entry in the order of attr so that the pin with a
4192 * higher attr will be selected when the jack is plugged.
4194 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4195 compare_attr, NULL);
4197 if (!auto_mic_check_imux(codec))
4201 spec->num_adc_nids = 1;
4202 spec->cur_mux[0] = spec->am_entry[0].idx;
4203 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4204 spec->am_entry[0].pin,
4205 spec->am_entry[1].pin,
4206 spec->am_entry[2].pin);
4211 /* power_filter hook; make inactive widgets into power down */
4212 static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4214 unsigned int power_state)
4216 if (power_state != AC_PWRST_D0)
4218 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4220 if (is_active_nid_for_any(codec, nid))
4225 /* mute all aamix inputs initially; parse up to the first leaves */
4226 static void mute_all_mixer_nid(struct hda_codec *codec, hda_nid_t mix)
4229 const hda_nid_t *conn;
4232 nums = snd_hda_get_conn_list(codec, mix, &conn);
4233 has_amp = nid_has_mute(codec, mix, HDA_INPUT);
4234 for (i = 0; i < nums; i++) {
4236 snd_hda_codec_amp_stereo(codec, mix,
4238 0xff, HDA_AMP_MUTE);
4239 else if (nid_has_volume(codec, conn[i], HDA_OUTPUT))
4240 snd_hda_codec_amp_stereo(codec, conn[i],
4242 0xff, HDA_AMP_MUTE);
4247 * Parse the given BIOS configuration and set up the hda_gen_spec
4249 * return 1 if successful, 0 if the proper config is not found,
4250 * or a negative error code
4252 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4253 struct auto_pin_cfg *cfg)
4255 struct hda_gen_spec *spec = codec->spec;
4258 parse_user_hints(codec);
4260 if (spec->mixer_nid && !spec->mixer_merge_nid)
4261 spec->mixer_merge_nid = spec->mixer_nid;
4263 if (cfg != &spec->autocfg) {
4264 spec->autocfg = *cfg;
4265 cfg = &spec->autocfg;
4268 if (!spec->main_out_badness)
4269 spec->main_out_badness = &hda_main_out_badness;
4270 if (!spec->extra_out_badness)
4271 spec->extra_out_badness = &hda_extra_out_badness;
4273 fill_all_dac_nids(codec);
4275 if (!cfg->line_outs) {
4276 if (cfg->dig_outs || cfg->dig_in_pin) {
4277 spec->multiout.max_channels = 2;
4278 spec->no_analog = 1;
4281 return 0; /* can't find valid BIOS pin config */
4284 if (!spec->no_primary_hp &&
4285 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4286 cfg->line_outs <= cfg->hp_outs) {
4287 /* use HP as primary out */
4288 cfg->speaker_outs = cfg->line_outs;
4289 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4290 sizeof(cfg->speaker_pins));
4291 cfg->line_outs = cfg->hp_outs;
4292 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4294 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4295 cfg->line_out_type = AUTO_PIN_HP_OUT;
4298 err = parse_output_paths(codec);
4301 err = create_multi_channel_mode(codec);
4304 err = create_multi_out_ctls(codec, cfg);
4307 err = create_hp_out_ctls(codec);
4310 err = create_speaker_out_ctls(codec);
4313 err = create_indep_hp_ctls(codec);
4316 err = create_loopback_mixing_ctl(codec);
4319 err = create_hp_mic(codec);
4322 err = create_input_ctls(codec);
4326 spec->const_channel_count = spec->ext_channel_count;
4327 /* check the multiple speaker and headphone pins */
4328 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4329 spec->const_channel_count = max(spec->const_channel_count,
4330 cfg->speaker_outs * 2);
4331 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4332 spec->const_channel_count = max(spec->const_channel_count,
4334 spec->multiout.max_channels = max(spec->ext_channel_count,
4335 spec->const_channel_count);
4337 err = check_auto_mute_availability(codec);
4341 err = check_dyn_adc_switch(codec);
4345 err = check_auto_mic_availability(codec);
4349 err = create_capture_mixers(codec);
4353 err = parse_mic_boost(codec);
4357 /* create "Headphone Mic Jack Mode" if no input selection is
4358 * available (or user specifies add_jack_modes hint)
4360 if (spec->hp_mic_pin &&
4361 (spec->auto_mic || spec->input_mux.num_items == 1 ||
4362 spec->add_jack_modes)) {
4363 err = create_hp_mic_jack_mode(codec, spec->hp_mic_pin);
4368 if (spec->add_jack_modes) {
4369 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4370 err = create_out_jack_modes(codec, cfg->line_outs,
4371 cfg->line_out_pins);
4375 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4376 err = create_out_jack_modes(codec, cfg->hp_outs,
4383 /* mute all aamix input initially */
4384 if (spec->mixer_nid)
4385 mute_all_mixer_nid(codec, spec->mixer_nid);
4388 parse_digital(codec);
4390 if (spec->power_down_unused)
4391 codec->power_filter = snd_hda_gen_path_power_filter;
4393 if (!spec->no_analog && spec->beep_nid) {
4394 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4401 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
4405 * Build control elements
4408 /* slave controls for virtual master */
4409 static const char * const slave_pfxs[] = {
4410 "Front", "Surround", "Center", "LFE", "Side",
4411 "Headphone", "Speaker", "Mono", "Line Out",
4412 "CLFE", "Bass Speaker", "PCM",
4413 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4414 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4419 int snd_hda_gen_build_controls(struct hda_codec *codec)
4421 struct hda_gen_spec *spec = codec->spec;
4424 if (spec->kctls.used) {
4425 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4430 if (spec->multiout.dig_out_nid) {
4431 err = snd_hda_create_dig_out_ctls(codec,
4432 spec->multiout.dig_out_nid,
4433 spec->multiout.dig_out_nid,
4434 spec->pcm_rec[1].pcm_type);
4437 if (!spec->no_analog) {
4438 err = snd_hda_create_spdif_share_sw(codec,
4442 spec->multiout.share_spdif = 1;
4445 if (spec->dig_in_nid) {
4446 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4451 /* if we have no master control, let's create it */
4452 if (!spec->no_analog &&
4453 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
4454 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
4455 spec->vmaster_tlv, slave_pfxs,
4460 if (!spec->no_analog &&
4461 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4462 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4465 true, &spec->vmaster_mute.sw_kctl);
4468 if (spec->vmaster_mute.hook) {
4469 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4470 spec->vmaster_mute_enum);
4471 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4475 free_kctls(spec); /* no longer needed */
4477 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4483 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4490 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4491 struct hda_codec *codec,
4492 struct snd_pcm_substream *substream,
4495 struct hda_gen_spec *spec = codec->spec;
4496 if (spec->pcm_playback_hook)
4497 spec->pcm_playback_hook(hinfo, codec, substream, action);
4500 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4501 struct hda_codec *codec,
4502 struct snd_pcm_substream *substream,
4505 struct hda_gen_spec *spec = codec->spec;
4506 if (spec->pcm_capture_hook)
4507 spec->pcm_capture_hook(hinfo, codec, substream, action);
4511 * Analog playback callbacks
4513 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4514 struct hda_codec *codec,
4515 struct snd_pcm_substream *substream)
4517 struct hda_gen_spec *spec = codec->spec;
4520 mutex_lock(&spec->pcm_mutex);
4521 err = snd_hda_multi_out_analog_open(codec,
4522 &spec->multiout, substream,
4525 spec->active_streams |= 1 << STREAM_MULTI_OUT;
4526 call_pcm_playback_hook(hinfo, codec, substream,
4527 HDA_GEN_PCM_ACT_OPEN);
4529 mutex_unlock(&spec->pcm_mutex);
4533 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4534 struct hda_codec *codec,
4535 unsigned int stream_tag,
4536 unsigned int format,
4537 struct snd_pcm_substream *substream)
4539 struct hda_gen_spec *spec = codec->spec;
4542 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4543 stream_tag, format, substream);
4545 call_pcm_playback_hook(hinfo, codec, substream,
4546 HDA_GEN_PCM_ACT_PREPARE);
4550 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4551 struct hda_codec *codec,
4552 struct snd_pcm_substream *substream)
4554 struct hda_gen_spec *spec = codec->spec;
4557 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4559 call_pcm_playback_hook(hinfo, codec, substream,
4560 HDA_GEN_PCM_ACT_CLEANUP);
4564 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4565 struct hda_codec *codec,
4566 struct snd_pcm_substream *substream)
4568 struct hda_gen_spec *spec = codec->spec;
4569 mutex_lock(&spec->pcm_mutex);
4570 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
4571 call_pcm_playback_hook(hinfo, codec, substream,
4572 HDA_GEN_PCM_ACT_CLOSE);
4573 mutex_unlock(&spec->pcm_mutex);
4577 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4578 struct hda_codec *codec,
4579 struct snd_pcm_substream *substream)
4581 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4585 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4586 struct hda_codec *codec,
4587 unsigned int stream_tag,
4588 unsigned int format,
4589 struct snd_pcm_substream *substream)
4591 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4592 call_pcm_capture_hook(hinfo, codec, substream,
4593 HDA_GEN_PCM_ACT_PREPARE);
4597 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4598 struct hda_codec *codec,
4599 struct snd_pcm_substream *substream)
4601 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4602 call_pcm_capture_hook(hinfo, codec, substream,
4603 HDA_GEN_PCM_ACT_CLEANUP);
4607 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4608 struct hda_codec *codec,
4609 struct snd_pcm_substream *substream)
4611 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4615 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4616 struct hda_codec *codec,
4617 struct snd_pcm_substream *substream)
4619 struct hda_gen_spec *spec = codec->spec;
4622 mutex_lock(&spec->pcm_mutex);
4623 if (!spec->indep_hp_enabled)
4626 spec->active_streams |= 1 << STREAM_INDEP_HP;
4627 call_pcm_playback_hook(hinfo, codec, substream,
4628 HDA_GEN_PCM_ACT_OPEN);
4629 mutex_unlock(&spec->pcm_mutex);
4633 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4634 struct hda_codec *codec,
4635 struct snd_pcm_substream *substream)
4637 struct hda_gen_spec *spec = codec->spec;
4638 mutex_lock(&spec->pcm_mutex);
4639 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
4640 call_pcm_playback_hook(hinfo, codec, substream,
4641 HDA_GEN_PCM_ACT_CLOSE);
4642 mutex_unlock(&spec->pcm_mutex);
4646 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4647 struct hda_codec *codec,
4648 unsigned int stream_tag,
4649 unsigned int format,
4650 struct snd_pcm_substream *substream)
4652 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4653 call_pcm_playback_hook(hinfo, codec, substream,
4654 HDA_GEN_PCM_ACT_PREPARE);
4658 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4659 struct hda_codec *codec,
4660 struct snd_pcm_substream *substream)
4662 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4663 call_pcm_playback_hook(hinfo, codec, substream,
4664 HDA_GEN_PCM_ACT_CLEANUP);
4671 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4672 struct hda_codec *codec,
4673 struct snd_pcm_substream *substream)
4675 struct hda_gen_spec *spec = codec->spec;
4676 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4679 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4680 struct hda_codec *codec,
4681 unsigned int stream_tag,
4682 unsigned int format,
4683 struct snd_pcm_substream *substream)
4685 struct hda_gen_spec *spec = codec->spec;
4686 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4687 stream_tag, format, substream);
4690 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4691 struct hda_codec *codec,
4692 struct snd_pcm_substream *substream)
4694 struct hda_gen_spec *spec = codec->spec;
4695 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4698 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4699 struct hda_codec *codec,
4700 struct snd_pcm_substream *substream)
4702 struct hda_gen_spec *spec = codec->spec;
4703 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4709 #define alt_capture_pcm_open capture_pcm_open
4710 #define alt_capture_pcm_close capture_pcm_close
4712 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4713 struct hda_codec *codec,
4714 unsigned int stream_tag,
4715 unsigned int format,
4716 struct snd_pcm_substream *substream)
4718 struct hda_gen_spec *spec = codec->spec;
4720 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
4721 stream_tag, 0, format);
4722 call_pcm_capture_hook(hinfo, codec, substream,
4723 HDA_GEN_PCM_ACT_PREPARE);
4727 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4728 struct hda_codec *codec,
4729 struct snd_pcm_substream *substream)
4731 struct hda_gen_spec *spec = codec->spec;
4733 snd_hda_codec_cleanup_stream(codec,
4734 spec->adc_nids[substream->number + 1]);
4735 call_pcm_capture_hook(hinfo, codec, substream,
4736 HDA_GEN_PCM_ACT_CLEANUP);
4742 static const struct hda_pcm_stream pcm_analog_playback = {
4746 /* NID is set in build_pcms */
4748 .open = playback_pcm_open,
4749 .close = playback_pcm_close,
4750 .prepare = playback_pcm_prepare,
4751 .cleanup = playback_pcm_cleanup
4755 static const struct hda_pcm_stream pcm_analog_capture = {
4759 /* NID is set in build_pcms */
4761 .open = capture_pcm_open,
4762 .close = capture_pcm_close,
4763 .prepare = capture_pcm_prepare,
4764 .cleanup = capture_pcm_cleanup
4768 static const struct hda_pcm_stream pcm_analog_alt_playback = {
4772 /* NID is set in build_pcms */
4774 .open = alt_playback_pcm_open,
4775 .close = alt_playback_pcm_close,
4776 .prepare = alt_playback_pcm_prepare,
4777 .cleanup = alt_playback_pcm_cleanup
4781 static const struct hda_pcm_stream pcm_analog_alt_capture = {
4782 .substreams = 2, /* can be overridden */
4785 /* NID is set in build_pcms */
4787 .open = alt_capture_pcm_open,
4788 .close = alt_capture_pcm_close,
4789 .prepare = alt_capture_pcm_prepare,
4790 .cleanup = alt_capture_pcm_cleanup
4794 static const struct hda_pcm_stream pcm_digital_playback = {
4798 /* NID is set in build_pcms */
4800 .open = dig_playback_pcm_open,
4801 .close = dig_playback_pcm_close,
4802 .prepare = dig_playback_pcm_prepare,
4803 .cleanup = dig_playback_pcm_cleanup
4807 static const struct hda_pcm_stream pcm_digital_capture = {
4811 /* NID is set in build_pcms */
4814 /* Used by build_pcms to flag that a PCM has no playback stream */
4815 static const struct hda_pcm_stream pcm_null_stream = {
4822 * dynamic changing ADC PCM streams
4824 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4826 struct hda_gen_spec *spec = codec->spec;
4827 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4829 if (spec->cur_adc && spec->cur_adc != new_adc) {
4830 /* stream is running, let's swap the current ADC */
4831 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4832 spec->cur_adc = new_adc;
4833 snd_hda_codec_setup_stream(codec, new_adc,
4834 spec->cur_adc_stream_tag, 0,
4835 spec->cur_adc_format);
4841 /* analog capture with dynamic dual-adc changes */
4842 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4843 struct hda_codec *codec,
4844 unsigned int stream_tag,
4845 unsigned int format,
4846 struct snd_pcm_substream *substream)
4848 struct hda_gen_spec *spec = codec->spec;
4849 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4850 spec->cur_adc_stream_tag = stream_tag;
4851 spec->cur_adc_format = format;
4852 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4856 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4857 struct hda_codec *codec,
4858 struct snd_pcm_substream *substream)
4860 struct hda_gen_spec *spec = codec->spec;
4861 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4866 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4870 .nid = 0, /* fill later */
4872 .prepare = dyn_adc_capture_pcm_prepare,
4873 .cleanup = dyn_adc_capture_pcm_cleanup
4877 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4878 const char *chip_name)
4884 strlcpy(str, chip_name, len);
4886 /* drop non-alnum chars after a space */
4887 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4888 if (!isalnum(p[1])) {
4893 strlcat(str, sfx, len);
4896 /* build PCM streams based on the parsed results */
4897 int snd_hda_gen_build_pcms(struct hda_codec *codec)
4899 struct hda_gen_spec *spec = codec->spec;
4900 struct hda_pcm *info = spec->pcm_rec;
4901 const struct hda_pcm_stream *p;
4902 bool have_multi_adcs;
4904 codec->num_pcms = 1;
4905 codec->pcm_info = info;
4907 if (spec->no_analog)
4910 fill_pcm_stream_name(spec->stream_name_analog,
4911 sizeof(spec->stream_name_analog),
4912 " Analog", codec->chip_name);
4913 info->name = spec->stream_name_analog;
4915 if (spec->multiout.num_dacs > 0) {
4916 p = spec->stream_analog_playback;
4918 p = &pcm_analog_playback;
4919 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4920 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4921 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4922 spec->multiout.max_channels;
4923 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4924 spec->autocfg.line_outs == 2)
4925 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4928 if (spec->num_adc_nids) {
4929 p = spec->stream_analog_capture;
4931 if (spec->dyn_adc_switch)
4932 p = &dyn_adc_pcm_analog_capture;
4934 p = &pcm_analog_capture;
4936 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4937 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4941 /* SPDIF for stream index #1 */
4942 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
4943 fill_pcm_stream_name(spec->stream_name_digital,
4944 sizeof(spec->stream_name_digital),
4945 " Digital", codec->chip_name);
4946 codec->num_pcms = 2;
4947 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4948 info = spec->pcm_rec + 1;
4949 info->name = spec->stream_name_digital;
4950 if (spec->dig_out_type)
4951 info->pcm_type = spec->dig_out_type;
4953 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4954 if (spec->multiout.dig_out_nid) {
4955 p = spec->stream_digital_playback;
4957 p = &pcm_digital_playback;
4958 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4959 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4961 if (spec->dig_in_nid) {
4962 p = spec->stream_digital_capture;
4964 p = &pcm_digital_capture;
4965 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4966 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4970 if (spec->no_analog)
4973 /* If the use of more than one ADC is requested for the current
4974 * model, configure a second analog capture-only PCM.
4976 have_multi_adcs = (spec->num_adc_nids > 1) &&
4977 !spec->dyn_adc_switch && !spec->auto_mic;
4978 /* Additional Analaog capture for index #2 */
4979 if (spec->alt_dac_nid || have_multi_adcs) {
4980 fill_pcm_stream_name(spec->stream_name_alt_analog,
4981 sizeof(spec->stream_name_alt_analog),
4982 " Alt Analog", codec->chip_name);
4983 codec->num_pcms = 3;
4984 info = spec->pcm_rec + 2;
4985 info->name = spec->stream_name_alt_analog;
4986 if (spec->alt_dac_nid) {
4987 p = spec->stream_analog_alt_playback;
4989 p = &pcm_analog_alt_playback;
4990 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4991 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4994 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4996 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4998 if (have_multi_adcs) {
4999 p = spec->stream_analog_alt_capture;
5001 p = &pcm_analog_alt_capture;
5002 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5003 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
5005 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5006 spec->num_adc_nids - 1;
5008 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
5010 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
5016 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
5020 * Standard auto-parser initializations
5023 /* configure the given path as a proper output */
5024 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5026 struct nid_path *path;
5029 path = snd_hda_get_path_from_idx(codec, path_idx);
5030 if (!path || !path->depth)
5032 pin = path->path[path->depth - 1];
5033 restore_pin_ctl(codec, pin);
5034 snd_hda_activate_path(codec, path, path->active,
5035 aamix_default(codec->spec));
5036 set_pin_eapd(codec, pin, path->active);
5039 /* initialize primary output paths */
5040 static void init_multi_out(struct hda_codec *codec)
5042 struct hda_gen_spec *spec = codec->spec;
5045 for (i = 0; i < spec->autocfg.line_outs; i++)
5046 set_output_and_unmute(codec, spec->out_paths[i]);
5050 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5054 for (i = 0; i < num_outs; i++)
5055 set_output_and_unmute(codec, paths[i]);
5058 /* initialize hp and speaker paths */
5059 static void init_extra_out(struct hda_codec *codec)
5061 struct hda_gen_spec *spec = codec->spec;
5063 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5064 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5065 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5066 __init_extra_out(codec, spec->autocfg.speaker_outs,
5067 spec->speaker_paths);
5070 /* initialize multi-io paths */
5071 static void init_multi_io(struct hda_codec *codec)
5073 struct hda_gen_spec *spec = codec->spec;
5076 for (i = 0; i < spec->multi_ios; i++) {
5077 hda_nid_t pin = spec->multi_io[i].pin;
5078 struct nid_path *path;
5079 path = get_multiio_path(codec, i);
5082 if (!spec->multi_io[i].ctl_in)
5083 spec->multi_io[i].ctl_in =
5084 snd_hda_codec_get_pin_target(codec, pin);
5085 snd_hda_activate_path(codec, path, path->active,
5086 aamix_default(spec));
5090 /* set up input pins and loopback paths */
5091 static void init_analog_input(struct hda_codec *codec)
5093 struct hda_gen_spec *spec = codec->spec;
5094 struct auto_pin_cfg *cfg = &spec->autocfg;
5097 for (i = 0; i < cfg->num_inputs; i++) {
5098 hda_nid_t nid = cfg->inputs[i].pin;
5099 if (is_input_pin(codec, nid))
5100 restore_pin_ctl(codec, nid);
5102 /* init loopback inputs */
5103 if (spec->mixer_nid) {
5104 resume_path_from_idx(codec, spec->loopback_paths[i]);
5105 resume_path_from_idx(codec, spec->loopback_merge_path);
5110 /* initialize ADC paths */
5111 static void init_input_src(struct hda_codec *codec)
5113 struct hda_gen_spec *spec = codec->spec;
5114 struct hda_input_mux *imux = &spec->input_mux;
5115 struct nid_path *path;
5118 if (spec->dyn_adc_switch)
5121 nums = spec->num_adc_nids;
5123 for (c = 0; c < nums; c++) {
5124 for (i = 0; i < imux->num_items; i++) {
5125 path = get_input_path(codec, c, i);
5127 bool active = path->active;
5128 if (i == spec->cur_mux[c])
5130 snd_hda_activate_path(codec, path, active, false);
5134 update_hp_mic(codec, c, true);
5137 if (spec->cap_sync_hook)
5138 spec->cap_sync_hook(codec, NULL);
5141 /* set right pin controls for digital I/O */
5142 static void init_digital(struct hda_codec *codec)
5144 struct hda_gen_spec *spec = codec->spec;
5148 for (i = 0; i < spec->autocfg.dig_outs; i++)
5149 set_output_and_unmute(codec, spec->digout_paths[i]);
5150 pin = spec->autocfg.dig_in_pin;
5152 restore_pin_ctl(codec, pin);
5153 resume_path_from_idx(codec, spec->digin_path);
5157 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5158 * invalid unsol tags by some reason
5160 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5164 for (i = 0; i < codec->init_pins.used; i++) {
5165 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5166 hda_nid_t nid = pin->nid;
5167 if (is_jack_detectable(codec, nid) &&
5168 !snd_hda_jack_tbl_get(codec, nid))
5169 snd_hda_codec_update_cache(codec, nid, 0,
5170 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5175 * initialize the generic spec;
5176 * this can be put as patch_ops.init function
5178 int snd_hda_gen_init(struct hda_codec *codec)
5180 struct hda_gen_spec *spec = codec->spec;
5182 if (spec->init_hook)
5183 spec->init_hook(codec);
5185 snd_hda_apply_verbs(codec);
5187 codec->cached_write = 1;
5189 init_multi_out(codec);
5190 init_extra_out(codec);
5191 init_multi_io(codec);
5192 init_analog_input(codec);
5193 init_input_src(codec);
5194 init_digital(codec);
5196 clear_unsol_on_unused_pins(codec);
5198 /* call init functions of standard auto-mute helpers */
5199 update_automute_all(codec);
5201 snd_hda_codec_flush_cache(codec);
5203 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5204 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5206 hda_call_check_power_status(codec, 0x01);
5209 EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5212 * free the generic spec;
5213 * this can be put as patch_ops.free function
5215 void snd_hda_gen_free(struct hda_codec *codec)
5217 snd_hda_detach_beep_device(codec);
5218 snd_hda_gen_spec_free(codec->spec);
5222 EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5226 * check the loopback power save state;
5227 * this can be put as patch_ops.check_power_status function
5229 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5231 struct hda_gen_spec *spec = codec->spec;
5232 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5234 EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5239 * the generic codec support
5242 static const struct hda_codec_ops generic_patch_ops = {
5243 .build_controls = snd_hda_gen_build_controls,
5244 .build_pcms = snd_hda_gen_build_pcms,
5245 .init = snd_hda_gen_init,
5246 .free = snd_hda_gen_free,
5247 .unsol_event = snd_hda_jack_unsol_event,
5249 .check_power_status = snd_hda_gen_check_power_status,
5253 int snd_hda_parse_generic_codec(struct hda_codec *codec)
5255 struct hda_gen_spec *spec;
5258 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5261 snd_hda_gen_spec_init(spec);
5264 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5268 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5272 codec->patch_ops = generic_patch_ops;
5276 snd_hda_gen_free(codec);
5279 EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);