2 * Universal Interface for Intel High Definition Audio Codec
4 * Generic widget tree parser
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/sort.h>
27 #include <linux/delay.h>
28 #include <linux/ctype.h>
29 #include <linux/string.h>
30 #include <linux/bitops.h>
31 #include <sound/core.h>
32 #include <sound/jack.h>
33 #include "hda_codec.h"
34 #include "hda_local.h"
35 #include "hda_auto_parser.h"
38 #include "hda_generic.h"
41 /* initialize hda_gen_spec struct */
42 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
44 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
45 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
46 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
47 mutex_init(&spec->pcm_mutex);
50 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
52 struct snd_kcontrol_new *
53 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
54 const struct snd_kcontrol_new *temp)
56 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
61 knew->name = kstrdup(name, GFP_KERNEL);
63 knew->name = kstrdup(knew->name, GFP_KERNEL);
68 EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
70 static void free_kctls(struct hda_gen_spec *spec)
72 if (spec->kctls.list) {
73 struct snd_kcontrol_new *kctl = spec->kctls.list;
75 for (i = 0; i < spec->kctls.used; i++)
78 snd_array_free(&spec->kctls);
81 void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
86 snd_array_free(&spec->paths);
87 snd_array_free(&spec->loopback_list);
89 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
94 static void parse_user_hints(struct hda_codec *codec)
96 struct hda_gen_spec *spec = codec->spec;
99 val = snd_hda_get_bool_hint(codec, "jack_detect");
101 codec->no_jack_detect = !val;
102 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
104 codec->inv_jack_detect = !!val;
105 val = snd_hda_get_bool_hint(codec, "trigger_sense");
107 codec->no_trigger_sense = !val;
108 val = snd_hda_get_bool_hint(codec, "inv_eapd");
110 codec->inv_eapd = !!val;
111 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
113 codec->pcm_format_first = !!val;
114 val = snd_hda_get_bool_hint(codec, "sticky_stream");
116 codec->no_sticky_stream = !val;
117 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
119 codec->spdif_status_reset = !!val;
120 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
122 codec->pin_amp_workaround = !!val;
123 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
125 codec->single_adc_amp = !!val;
127 val = snd_hda_get_bool_hint(codec, "auto_mute");
129 spec->suppress_auto_mute = !val;
130 val = snd_hda_get_bool_hint(codec, "auto_mic");
132 spec->suppress_auto_mic = !val;
133 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
135 spec->line_in_auto_switch = !!val;
136 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
138 spec->need_dac_fix = !!val;
139 val = snd_hda_get_bool_hint(codec, "primary_hp");
141 spec->no_primary_hp = !val;
142 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
144 spec->multi_cap_vol = !!val;
145 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
147 spec->inv_dmic_split = !!val;
148 val = snd_hda_get_bool_hint(codec, "indep_hp");
150 spec->indep_hp = !!val;
151 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
153 spec->add_stereo_mix_input = !!val;
154 /* the following two are just for compatibility */
155 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
157 spec->add_jack_modes = !!val;
158 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
160 spec->add_jack_modes = !!val;
161 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
163 spec->add_jack_modes = !!val;
164 val = snd_hda_get_bool_hint(codec, "power_down_unused");
166 spec->power_down_unused = !!val;
167 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
169 spec->hp_mic = !!val;
170 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
172 spec->suppress_hp_mic_detect = !val;
174 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
175 spec->mixer_nid = val;
179 * pin control value accesses
182 #define update_pin_ctl(codec, pin, val) \
183 snd_hda_codec_update_cache(codec, pin, 0, \
184 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
186 /* restore the pinctl based on the cached value */
187 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
189 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
192 /* set the pinctl target value and write it if requested */
193 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
194 unsigned int val, bool do_write)
198 val = snd_hda_correct_pin_ctl(codec, pin, val);
199 snd_hda_codec_set_pin_target(codec, pin, val);
201 update_pin_ctl(codec, pin, val);
204 /* set pinctl target values for all given pins */
205 static void set_pin_targets(struct hda_codec *codec, int num_pins,
206 hda_nid_t *pins, unsigned int val)
209 for (i = 0; i < num_pins; i++)
210 set_pin_target(codec, pins[i], val, false);
217 /* return the position of NID in the list, or -1 if not found */
218 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
221 for (i = 0; i < nums; i++)
227 /* return true if the given NID is contained in the path */
228 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
230 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
233 static struct nid_path *get_nid_path(struct hda_codec *codec,
234 hda_nid_t from_nid, hda_nid_t to_nid,
237 struct hda_gen_spec *spec = codec->spec;
240 for (i = 0; i < spec->paths.used; i++) {
241 struct nid_path *path = snd_array_elem(&spec->paths, i);
242 if (path->depth <= 0)
244 if ((!from_nid || path->path[0] == from_nid) &&
245 (!to_nid || path->path[path->depth - 1] == to_nid)) {
247 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
248 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
255 /* get the path between the given NIDs;
256 * passing 0 to either @pin or @dac behaves as a wildcard
258 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
259 hda_nid_t from_nid, hda_nid_t to_nid)
261 return get_nid_path(codec, from_nid, to_nid, 0);
263 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
265 /* get the index number corresponding to the path instance;
266 * the index starts from 1, for easier checking the invalid value
268 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
270 struct hda_gen_spec *spec = codec->spec;
271 struct nid_path *array = spec->paths.list;
274 if (!spec->paths.used)
277 if (idx < 0 || idx >= spec->paths.used)
281 EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
283 /* get the path instance corresponding to the given index number */
284 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
286 struct hda_gen_spec *spec = codec->spec;
288 if (idx <= 0 || idx > spec->paths.used)
290 return snd_array_elem(&spec->paths, idx - 1);
292 EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
294 /* check whether the given DAC is already found in any existing paths */
295 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
297 struct hda_gen_spec *spec = codec->spec;
300 for (i = 0; i < spec->paths.used; i++) {
301 struct nid_path *path = snd_array_elem(&spec->paths, i);
302 if (path->path[0] == nid)
308 /* check whether the given two widgets can be connected */
309 static bool is_reachable_path(struct hda_codec *codec,
310 hda_nid_t from_nid, hda_nid_t to_nid)
312 if (!from_nid || !to_nid)
314 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
317 /* nid, dir and idx */
318 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
320 /* check whether the given ctl is already assigned in any path elements */
321 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
323 struct hda_gen_spec *spec = codec->spec;
326 val &= AMP_VAL_COMPARE_MASK;
327 for (i = 0; i < spec->paths.used; i++) {
328 struct nid_path *path = snd_array_elem(&spec->paths, i);
329 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
335 /* check whether a control with the given (nid, dir, idx) was assigned */
336 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
337 int dir, int idx, int type)
339 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
340 return is_ctl_used(codec, val, type);
343 static void print_nid_path(const char *pfx, struct nid_path *path)
350 for (i = 0; i < path->depth; i++) {
352 sprintf(tmp, ":%02x", path->path[i]);
353 strlcat(buf, tmp, sizeof(buf));
355 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
358 /* called recursively */
359 static bool __parse_nid_path(struct hda_codec *codec,
360 hda_nid_t from_nid, hda_nid_t to_nid,
361 int anchor_nid, struct nid_path *path,
364 const hda_nid_t *conn;
367 if (to_nid == anchor_nid)
368 anchor_nid = 0; /* anchor passed */
369 else if (to_nid == (hda_nid_t)(-anchor_nid))
370 return false; /* hit the exclusive nid */
372 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
373 for (i = 0; i < nums; i++) {
374 if (conn[i] != from_nid) {
375 /* special case: when from_nid is 0,
376 * try to find an empty DAC
379 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
380 is_dac_already_used(codec, conn[i]))
383 /* anchor is not requested or already passed? */
387 if (depth >= MAX_NID_PATH_DEPTH)
389 for (i = 0; i < nums; i++) {
391 type = get_wcaps_type(get_wcaps(codec, conn[i]));
392 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
395 if (__parse_nid_path(codec, from_nid, conn[i],
396 anchor_nid, path, depth + 1))
402 path->path[path->depth] = conn[i];
403 path->idx[path->depth + 1] = i;
404 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
405 path->multi[path->depth + 1] = 1;
410 /* parse the widget path from the given nid to the target nid;
411 * when @from_nid is 0, try to find an empty DAC;
412 * when @anchor_nid is set to a positive value, only paths through the widget
413 * with the given value are evaluated.
414 * when @anchor_nid is set to a negative value, paths through the widget
415 * with the negative of given value are excluded, only other paths are chosen.
416 * when @anchor_nid is zero, no special handling about path selection.
418 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
419 hda_nid_t to_nid, int anchor_nid,
420 struct nid_path *path)
422 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
423 path->path[path->depth] = to_nid;
429 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
432 * parse the path between the given NIDs and add to the path list.
433 * if no valid path is found, return NULL
436 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
437 hda_nid_t to_nid, int anchor_nid)
439 struct hda_gen_spec *spec = codec->spec;
440 struct nid_path *path;
442 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
445 /* check whether the path has been already added */
446 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
450 path = snd_array_new(&spec->paths);
453 memset(path, 0, sizeof(*path));
454 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
460 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
462 /* clear the given path as invalid so that it won't be picked up later */
463 static void invalidate_nid_path(struct hda_codec *codec, int idx)
465 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
468 memset(path, 0, sizeof(*path));
471 /* look for an empty DAC slot */
472 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
475 struct hda_gen_spec *spec = codec->spec;
479 for (i = 0; i < spec->num_all_dacs; i++) {
480 hda_nid_t nid = spec->all_dacs[i];
481 if (!nid || is_dac_already_used(codec, nid))
483 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
484 if (is_digital != cap_digital)
486 if (is_reachable_path(codec, nid, pin))
492 /* replace the channels in the composed amp value with the given number */
493 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
495 val &= ~(0x3U << 16);
500 /* check whether the widget has the given amp capability for the direction */
501 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
502 int dir, unsigned int bits)
506 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
507 if (query_amp_caps(codec, nid, dir) & bits)
512 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
513 hda_nid_t nid2, int dir)
515 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
516 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
517 return (query_amp_caps(codec, nid1, dir) ==
518 query_amp_caps(codec, nid2, dir));
521 #define nid_has_mute(codec, nid, dir) \
522 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
523 #define nid_has_volume(codec, nid, dir) \
524 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
526 /* look for a widget suitable for assigning a mute switch in the path */
527 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
528 struct nid_path *path)
532 for (i = path->depth - 1; i >= 0; i--) {
533 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
534 return path->path[i];
535 if (i != path->depth - 1 && i != 0 &&
536 nid_has_mute(codec, path->path[i], HDA_INPUT))
537 return path->path[i];
542 /* look for a widget suitable for assigning a volume ctl in the path */
543 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
544 struct nid_path *path)
548 for (i = path->depth - 1; i >= 0; i--) {
549 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
550 return path->path[i];
556 * path activation / deactivation
559 /* can have the amp-in capability? */
560 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
562 hda_nid_t nid = path->path[idx];
563 unsigned int caps = get_wcaps(codec, nid);
564 unsigned int type = get_wcaps_type(caps);
566 if (!(caps & AC_WCAP_IN_AMP))
568 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
573 /* can have the amp-out capability? */
574 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
576 hda_nid_t nid = path->path[idx];
577 unsigned int caps = get_wcaps(codec, nid);
578 unsigned int type = get_wcaps_type(caps);
580 if (!(caps & AC_WCAP_OUT_AMP))
582 if (type == AC_WID_PIN && !idx) /* only for output pins */
587 /* check whether the given (nid,dir,idx) is active */
588 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
589 unsigned int dir, unsigned int idx)
591 struct hda_gen_spec *spec = codec->spec;
594 for (n = 0; n < spec->paths.used; n++) {
595 struct nid_path *path = snd_array_elem(&spec->paths, n);
598 for (i = 0; i < path->depth; i++) {
599 if (path->path[i] == nid) {
600 if (dir == HDA_OUTPUT || path->idx[i] == idx)
609 /* get the default amp value for the target state */
610 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
611 int dir, unsigned int caps, bool enable)
613 unsigned int val = 0;
615 if (caps & AC_AMPCAP_NUM_STEPS) {
618 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
620 if (caps & AC_AMPCAP_MUTE) {
627 /* initialize the amp value (only at the first time) */
628 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
630 unsigned int caps = query_amp_caps(codec, nid, dir);
631 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
632 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
635 /* calculate amp value mask we can modify;
636 * if the given amp is controlled by mixers, don't touch it
638 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
639 hda_nid_t nid, int dir, int idx,
642 unsigned int mask = 0xff;
644 if (caps & AC_AMPCAP_MUTE) {
645 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
648 if (caps & AC_AMPCAP_NUM_STEPS) {
649 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
650 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
656 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
657 int idx, int idx_to_check, bool enable)
660 unsigned int mask, val;
662 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
665 caps = query_amp_caps(codec, nid, dir);
666 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
667 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
672 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
675 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
678 hda_nid_t nid = path->path[i];
679 init_amp(codec, nid, HDA_OUTPUT, 0);
680 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
683 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
684 int i, bool enable, bool add_aamix)
686 struct hda_gen_spec *spec = codec->spec;
687 const hda_nid_t *conn;
690 hda_nid_t nid = path->path[i];
692 nums = snd_hda_get_conn_list(codec, nid, &conn);
693 type = get_wcaps_type(get_wcaps(codec, nid));
694 if (type == AC_WID_PIN ||
695 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
701 for (n = 0; n < nums; n++)
702 init_amp(codec, nid, HDA_INPUT, n);
704 /* here is a little bit tricky in comparison with activate_amp_out();
705 * when aa-mixer is available, we need to enable the path as well
707 for (n = 0; n < nums; n++) {
708 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
710 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
714 /* activate or deactivate the given path
715 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
717 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
718 bool enable, bool add_aamix)
720 struct hda_gen_spec *spec = codec->spec;
724 path->active = false;
726 for (i = path->depth - 1; i >= 0; i--) {
727 hda_nid_t nid = path->path[i];
728 if (enable && spec->power_down_unused) {
729 /* make sure the widget is powered up */
730 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
731 snd_hda_codec_write(codec, nid, 0,
732 AC_VERB_SET_POWER_STATE,
735 if (enable && path->multi[i])
736 snd_hda_codec_write_cache(codec, nid, 0,
737 AC_VERB_SET_CONNECT_SEL,
739 if (has_amp_in(codec, path, i))
740 activate_amp_in(codec, path, i, enable, add_aamix);
741 if (has_amp_out(codec, path, i))
742 activate_amp_out(codec, path, i, enable);
748 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
750 /* if the given path is inactive, put widgets into D3 (only if suitable) */
751 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
753 struct hda_gen_spec *spec = codec->spec;
754 bool changed = false;
757 if (!spec->power_down_unused || path->active)
760 for (i = 0; i < path->depth; i++) {
761 hda_nid_t nid = path->path[i];
762 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3)) {
763 snd_hda_codec_write(codec, nid, 0,
764 AC_VERB_SET_POWER_STATE,
772 snd_hda_codec_read(codec, path->path[0], 0,
773 AC_VERB_GET_POWER_STATE, 0);
777 /* turn on/off EAPD on the given pin */
778 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
780 struct hda_gen_spec *spec = codec->spec;
781 if (spec->own_eapd_ctl ||
782 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
786 snd_hda_codec_update_cache(codec, pin, 0,
787 AC_VERB_SET_EAPD_BTLENABLE,
788 enable ? 0x02 : 0x00);
791 /* re-initialize the path specified by the given path index */
792 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
794 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
796 snd_hda_activate_path(codec, path, path->active, false);
801 * Helper functions for creating mixer ctl elements
809 static const struct snd_kcontrol_new control_templates[] = {
810 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
811 HDA_CODEC_MUTE(NULL, 0, 0, 0),
812 HDA_BIND_MUTE(NULL, 0, 0, 0),
815 /* add dynamic controls from template */
816 static struct snd_kcontrol_new *
817 add_control(struct hda_gen_spec *spec, int type, const char *name,
818 int cidx, unsigned long val)
820 struct snd_kcontrol_new *knew;
822 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
826 if (get_amp_nid_(val))
827 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
828 knew->private_value = val;
832 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
833 const char *pfx, const char *dir,
834 const char *sfx, int cidx, unsigned long val)
837 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
838 if (!add_control(spec, type, name, cidx, val))
843 #define add_pb_vol_ctrl(spec, type, pfx, val) \
844 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
845 #define add_pb_sw_ctrl(spec, type, pfx, val) \
846 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
847 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
848 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
849 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
850 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
852 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
853 unsigned int chs, struct nid_path *path)
858 val = path->ctls[NID_PATH_VOL_CTL];
861 val = amp_val_replace_channels(val, chs);
862 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
865 /* return the channel bits suitable for the given path->ctls[] */
866 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
869 int chs = 1; /* mono (left only) */
871 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
872 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
873 chs = 3; /* stereo */
878 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
879 struct nid_path *path)
881 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
882 return add_vol_ctl(codec, pfx, cidx, chs, path);
885 /* create a mute-switch for the given mixer widget;
886 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
888 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
889 unsigned int chs, struct nid_path *path)
892 int type = HDA_CTL_WIDGET_MUTE;
896 val = path->ctls[NID_PATH_MUTE_CTL];
899 val = amp_val_replace_channels(val, chs);
900 if (get_amp_direction_(val) == HDA_INPUT) {
901 hda_nid_t nid = get_amp_nid_(val);
902 int nums = snd_hda_get_num_conns(codec, nid);
904 type = HDA_CTL_BIND_MUTE;
908 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
911 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
912 int cidx, struct nid_path *path)
914 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
915 return add_sw_ctl(codec, pfx, cidx, chs, path);
918 /* any ctl assigned to the path with the given index? */
919 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
921 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
922 return path && path->ctls[ctl_type];
925 static const char * const channel_name[4] = {
926 "Front", "Surround", "CLFE", "Side"
929 /* give some appropriate ctl name prefix for the given line out channel */
930 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
931 int *index, int ctl_type)
933 struct hda_gen_spec *spec = codec->spec;
934 struct auto_pin_cfg *cfg = &spec->autocfg;
937 if (cfg->line_outs == 1 && !spec->multi_ios &&
938 !cfg->hp_outs && !cfg->speaker_outs)
939 return spec->vmaster_mute.hook ? "PCM" : "Master";
941 /* if there is really a single DAC used in the whole output paths,
942 * use it master (or "PCM" if a vmaster hook is present)
944 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
945 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
946 return spec->vmaster_mute.hook ? "PCM" : "Master";
948 /* multi-io channels */
949 if (ch >= cfg->line_outs)
950 return channel_name[ch];
952 switch (cfg->line_out_type) {
953 case AUTO_PIN_SPEAKER_OUT:
954 /* if the primary channel vol/mute is shared with HP volume,
955 * don't name it as Speaker
957 if (!ch && cfg->hp_outs &&
958 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
960 if (cfg->line_outs == 1)
962 if (cfg->line_outs == 2)
963 return ch ? "Bass Speaker" : "Speaker";
965 case AUTO_PIN_HP_OUT:
966 /* if the primary channel vol/mute is shared with spk volume,
967 * don't name it as Headphone
969 if (!ch && cfg->speaker_outs &&
970 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
972 /* for multi-io case, only the primary out */
973 if (ch && spec->multi_ios)
979 /* for a single channel output, we don't have to name the channel */
980 if (cfg->line_outs == 1 && !spec->multi_ios)
983 if (ch >= ARRAY_SIZE(channel_name)) {
988 return channel_name[ch];
995 /* badness definition */
997 /* No primary DAC is found for the main output */
998 BAD_NO_PRIMARY_DAC = 0x10000,
999 /* No DAC is found for the extra output */
1000 BAD_NO_DAC = 0x4000,
1001 /* No possible multi-ios */
1002 BAD_MULTI_IO = 0x120,
1003 /* No individual DAC for extra output */
1004 BAD_NO_EXTRA_DAC = 0x102,
1005 /* No individual DAC for extra surrounds */
1006 BAD_NO_EXTRA_SURR_DAC = 0x101,
1007 /* Primary DAC shared with main surrounds */
1008 BAD_SHARED_SURROUND = 0x100,
1009 /* No independent HP possible */
1010 BAD_NO_INDEP_HP = 0x10,
1011 /* Primary DAC shared with main CLFE */
1012 BAD_SHARED_CLFE = 0x10,
1013 /* Primary DAC shared with extra surrounds */
1014 BAD_SHARED_EXTRA_SURROUND = 0x10,
1015 /* Volume widget is shared */
1016 BAD_SHARED_VOL = 0x10,
1019 /* look for widgets in the given path which are appropriate for
1020 * volume and mute controls, and assign the values to ctls[].
1022 * When no appropriate widget is found in the path, the badness value
1023 * is incremented depending on the situation. The function returns the
1024 * total badness for both volume and mute controls.
1026 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1033 return BAD_SHARED_VOL * 2;
1035 if (path->ctls[NID_PATH_VOL_CTL] ||
1036 path->ctls[NID_PATH_MUTE_CTL])
1037 return 0; /* already evaluated */
1039 nid = look_for_out_vol_nid(codec, path);
1041 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1042 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1043 badness += BAD_SHARED_VOL;
1045 path->ctls[NID_PATH_VOL_CTL] = val;
1047 badness += BAD_SHARED_VOL;
1048 nid = look_for_out_mute_nid(codec, path);
1050 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1051 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1052 nid_has_mute(codec, nid, HDA_OUTPUT))
1053 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1055 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1056 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1057 badness += BAD_SHARED_VOL;
1059 path->ctls[NID_PATH_MUTE_CTL] = val;
1061 badness += BAD_SHARED_VOL;
1065 const struct badness_table hda_main_out_badness = {
1066 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1067 .no_dac = BAD_NO_DAC,
1068 .shared_primary = BAD_NO_PRIMARY_DAC,
1069 .shared_surr = BAD_SHARED_SURROUND,
1070 .shared_clfe = BAD_SHARED_CLFE,
1071 .shared_surr_main = BAD_SHARED_SURROUND,
1073 EXPORT_SYMBOL_HDA(hda_main_out_badness);
1075 const struct badness_table hda_extra_out_badness = {
1076 .no_primary_dac = BAD_NO_DAC,
1077 .no_dac = BAD_NO_DAC,
1078 .shared_primary = BAD_NO_EXTRA_DAC,
1079 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1080 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1081 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1083 EXPORT_SYMBOL_HDA(hda_extra_out_badness);
1085 /* get the DAC of the primary output corresponding to the given array index */
1086 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1088 struct hda_gen_spec *spec = codec->spec;
1089 struct auto_pin_cfg *cfg = &spec->autocfg;
1091 if (cfg->line_outs > idx)
1092 return spec->private_dac_nids[idx];
1093 idx -= cfg->line_outs;
1094 if (spec->multi_ios > idx)
1095 return spec->multi_io[idx].dac;
1099 /* return the DAC if it's reachable, otherwise zero */
1100 static inline hda_nid_t try_dac(struct hda_codec *codec,
1101 hda_nid_t dac, hda_nid_t pin)
1103 return is_reachable_path(codec, dac, pin) ? dac : 0;
1106 /* try to assign DACs to pins and return the resultant badness */
1107 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1108 const hda_nid_t *pins, hda_nid_t *dacs,
1110 const struct badness_table *bad)
1112 struct hda_gen_spec *spec = codec->spec;
1120 for (i = 0; i < num_outs; i++) {
1121 struct nid_path *path;
1122 hda_nid_t pin = pins[i];
1124 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1126 badness += assign_out_path_ctls(codec, path);
1130 dacs[i] = look_for_dac(codec, pin, false);
1131 if (!dacs[i] && !i) {
1132 /* try to steal the DAC of surrounds for the front */
1133 for (j = 1; j < num_outs; j++) {
1134 if (is_reachable_path(codec, dacs[j], pin)) {
1137 invalidate_nid_path(codec, path_idx[j]);
1146 dac = try_dac(codec, get_primary_out(codec, i), pin);
1148 dac = try_dac(codec, dacs[0], pin);
1150 dac = try_dac(codec, get_primary_out(codec, i), pin);
1153 badness += bad->shared_primary;
1155 badness += bad->shared_surr;
1157 badness += bad->shared_clfe;
1158 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1159 dac = spec->private_dac_nids[0];
1160 badness += bad->shared_surr_main;
1162 badness += bad->no_primary_dac;
1164 badness += bad->no_dac;
1168 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1169 if (!path && !i && spec->mixer_nid) {
1170 /* try with aamix */
1171 path = snd_hda_add_new_path(codec, dac, pin, 0);
1175 badness += bad->no_dac;
1177 /* print_nid_path("output", path); */
1178 path->active = true;
1179 path_idx[i] = snd_hda_get_path_idx(codec, path);
1180 badness += assign_out_path_ctls(codec, path);
1187 /* return NID if the given pin has only a single connection to a certain DAC */
1188 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1190 struct hda_gen_spec *spec = codec->spec;
1192 hda_nid_t nid_found = 0;
1194 for (i = 0; i < spec->num_all_dacs; i++) {
1195 hda_nid_t nid = spec->all_dacs[i];
1196 if (!nid || is_dac_already_used(codec, nid))
1198 if (is_reachable_path(codec, nid, pin)) {
1207 /* check whether the given pin can be a multi-io pin */
1208 static bool can_be_multiio_pin(struct hda_codec *codec,
1209 unsigned int location, hda_nid_t nid)
1211 unsigned int defcfg, caps;
1213 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1214 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1216 if (location && get_defcfg_location(defcfg) != location)
1218 caps = snd_hda_query_pin_caps(codec, nid);
1219 if (!(caps & AC_PINCAP_OUT))
1224 /* count the number of input pins that are capable to be multi-io */
1225 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1227 struct hda_gen_spec *spec = codec->spec;
1228 struct auto_pin_cfg *cfg = &spec->autocfg;
1229 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1230 unsigned int location = get_defcfg_location(defcfg);
1234 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1235 for (i = 0; i < cfg->num_inputs; i++) {
1236 if (cfg->inputs[i].type != type)
1238 if (can_be_multiio_pin(codec, location,
1239 cfg->inputs[i].pin))
1249 * When hardwired is set, try to fill ony hardwired pins, and returns
1250 * zero if any pins are filled, non-zero if nothing found.
1251 * When hardwired is off, try to fill possible input pins, and returns
1252 * the badness value.
1254 static int fill_multi_ios(struct hda_codec *codec,
1255 hda_nid_t reference_pin,
1258 struct hda_gen_spec *spec = codec->spec;
1259 struct auto_pin_cfg *cfg = &spec->autocfg;
1260 int type, i, j, num_pins, old_pins;
1261 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1262 unsigned int location = get_defcfg_location(defcfg);
1264 struct nid_path *path;
1266 old_pins = spec->multi_ios;
1270 num_pins = count_multiio_pins(codec, reference_pin);
1274 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1275 for (i = 0; i < cfg->num_inputs; i++) {
1276 hda_nid_t nid = cfg->inputs[i].pin;
1279 if (cfg->inputs[i].type != type)
1281 if (!can_be_multiio_pin(codec, location, nid))
1283 for (j = 0; j < spec->multi_ios; j++) {
1284 if (nid == spec->multi_io[j].pin)
1287 if (j < spec->multi_ios)
1291 dac = get_dac_if_single(codec, nid);
1293 dac = look_for_dac(codec, nid, false);
1298 path = snd_hda_add_new_path(codec, dac, nid,
1304 /* print_nid_path("multiio", path); */
1305 spec->multi_io[spec->multi_ios].pin = nid;
1306 spec->multi_io[spec->multi_ios].dac = dac;
1307 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1308 snd_hda_get_path_idx(codec, path);
1310 if (spec->multi_ios >= 2)
1316 badness = BAD_MULTI_IO;
1317 if (old_pins == spec->multi_ios) {
1319 return 1; /* nothing found */
1321 return badness; /* no badness if nothing found */
1323 if (!hardwired && spec->multi_ios < 2) {
1324 /* cancel newly assigned paths */
1325 spec->paths.used -= spec->multi_ios - old_pins;
1326 spec->multi_ios = old_pins;
1330 /* assign volume and mute controls */
1331 for (i = old_pins; i < spec->multi_ios; i++) {
1332 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1333 badness += assign_out_path_ctls(codec, path);
1339 /* map DACs for all pins in the list if they are single connections */
1340 static bool map_singles(struct hda_codec *codec, int outs,
1341 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1343 struct hda_gen_spec *spec = codec->spec;
1346 for (i = 0; i < outs; i++) {
1347 struct nid_path *path;
1351 dac = get_dac_if_single(codec, pins[i]);
1354 path = snd_hda_add_new_path(codec, dac, pins[i],
1356 if (!path && !i && spec->mixer_nid)
1357 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1361 /* print_nid_path("output", path); */
1362 path->active = true;
1363 path_idx[i] = snd_hda_get_path_idx(codec, path);
1369 /* create a new path including aamix if available, and return its index */
1370 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1372 struct hda_gen_spec *spec = codec->spec;
1373 struct nid_path *path;
1374 hda_nid_t path_dac, dac, pin;
1376 path = snd_hda_get_path_from_idx(codec, path_idx);
1377 if (!path || !path->depth ||
1378 is_nid_contained(path, spec->mixer_nid))
1380 path_dac = path->path[0];
1381 dac = spec->private_dac_nids[0];
1382 pin = path->path[path->depth - 1];
1383 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1385 if (dac != path_dac)
1387 else if (spec->multiout.hp_out_nid[0])
1388 dac = spec->multiout.hp_out_nid[0];
1389 else if (spec->multiout.extra_out_nid[0])
1390 dac = spec->multiout.extra_out_nid[0];
1394 path = snd_hda_add_new_path(codec, dac, pin,
1399 /* print_nid_path("output-aamix", path); */
1400 path->active = false; /* unused as default */
1401 return snd_hda_get_path_idx(codec, path);
1404 /* check whether the independent HP is available with the current config */
1405 static bool indep_hp_possible(struct hda_codec *codec)
1407 struct hda_gen_spec *spec = codec->spec;
1408 struct auto_pin_cfg *cfg = &spec->autocfg;
1409 struct nid_path *path;
1412 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1413 idx = spec->out_paths[0];
1415 idx = spec->hp_paths[0];
1416 path = snd_hda_get_path_from_idx(codec, idx);
1420 /* assume no path conflicts unless aamix is involved */
1421 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1424 /* check whether output paths contain aamix */
1425 for (i = 0; i < cfg->line_outs; i++) {
1426 if (spec->out_paths[i] == idx)
1428 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1429 if (path && is_nid_contained(path, spec->mixer_nid))
1432 for (i = 0; i < cfg->speaker_outs; i++) {
1433 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1434 if (path && is_nid_contained(path, spec->mixer_nid))
1441 /* fill the empty entries in the dac array for speaker/hp with the
1442 * shared dac pointed by the paths
1444 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1445 hda_nid_t *dacs, int *path_idx)
1447 struct nid_path *path;
1450 for (i = 0; i < num_outs; i++) {
1453 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1456 dacs[i] = path->path[0];
1460 /* fill in the dac_nids table from the parsed pin configuration */
1461 static int fill_and_eval_dacs(struct hda_codec *codec,
1462 bool fill_hardwired,
1463 bool fill_mio_first)
1465 struct hda_gen_spec *spec = codec->spec;
1466 struct auto_pin_cfg *cfg = &spec->autocfg;
1467 int i, err, badness;
1469 /* set num_dacs once to full for look_for_dac() */
1470 spec->multiout.num_dacs = cfg->line_outs;
1471 spec->multiout.dac_nids = spec->private_dac_nids;
1472 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1473 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1474 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1475 spec->multi_ios = 0;
1476 snd_array_free(&spec->paths);
1478 /* clear path indices */
1479 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1480 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1481 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1482 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1483 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1484 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1485 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1486 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1490 /* fill hard-wired DACs first */
1491 if (fill_hardwired) {
1494 mapped = map_singles(codec, cfg->line_outs,
1496 spec->private_dac_nids,
1498 mapped |= map_singles(codec, cfg->hp_outs,
1500 spec->multiout.hp_out_nid,
1502 mapped |= map_singles(codec, cfg->speaker_outs,
1504 spec->multiout.extra_out_nid,
1505 spec->speaker_paths);
1506 if (fill_mio_first && cfg->line_outs == 1 &&
1507 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1508 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1515 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1516 spec->private_dac_nids, spec->out_paths,
1517 spec->main_out_badness);
1519 if (fill_mio_first &&
1520 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1521 /* try to fill multi-io first */
1522 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1525 /* we don't count badness at this stage yet */
1528 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1529 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1530 spec->multiout.hp_out_nid,
1532 spec->extra_out_badness);
1537 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1538 err = try_assign_dacs(codec, cfg->speaker_outs,
1540 spec->multiout.extra_out_nid,
1541 spec->speaker_paths,
1542 spec->extra_out_badness);
1547 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1548 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1554 if (spec->mixer_nid) {
1555 spec->aamix_out_paths[0] =
1556 check_aamix_out_path(codec, spec->out_paths[0]);
1557 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1558 spec->aamix_out_paths[1] =
1559 check_aamix_out_path(codec, spec->hp_paths[0]);
1560 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1561 spec->aamix_out_paths[2] =
1562 check_aamix_out_path(codec, spec->speaker_paths[0]);
1565 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1566 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1567 spec->multi_ios = 1; /* give badness */
1569 /* re-count num_dacs and squash invalid entries */
1570 spec->multiout.num_dacs = 0;
1571 for (i = 0; i < cfg->line_outs; i++) {
1572 if (spec->private_dac_nids[i])
1573 spec->multiout.num_dacs++;
1575 memmove(spec->private_dac_nids + i,
1576 spec->private_dac_nids + i + 1,
1577 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1578 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1582 spec->ext_channel_count = spec->min_channel_count =
1583 spec->multiout.num_dacs * 2;
1585 if (spec->multi_ios == 2) {
1586 for (i = 0; i < 2; i++)
1587 spec->private_dac_nids[spec->multiout.num_dacs++] =
1588 spec->multi_io[i].dac;
1589 } else if (spec->multi_ios) {
1590 spec->multi_ios = 0;
1591 badness += BAD_MULTI_IO;
1594 if (spec->indep_hp && !indep_hp_possible(codec))
1595 badness += BAD_NO_INDEP_HP;
1597 /* re-fill the shared DAC for speaker / headphone */
1598 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1599 refill_shared_dacs(codec, cfg->hp_outs,
1600 spec->multiout.hp_out_nid,
1602 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1603 refill_shared_dacs(codec, cfg->speaker_outs,
1604 spec->multiout.extra_out_nid,
1605 spec->speaker_paths);
1610 #define DEBUG_BADNESS
1612 #ifdef DEBUG_BADNESS
1613 #define debug_badness snd_printdd
1615 #define debug_badness(...)
1618 #ifdef DEBUG_BADNESS
1619 static inline void print_nid_path_idx(struct hda_codec *codec,
1620 const char *pfx, int idx)
1622 struct nid_path *path;
1624 path = snd_hda_get_path_from_idx(codec, idx);
1626 print_nid_path(pfx, path);
1629 static void debug_show_configs(struct hda_codec *codec,
1630 struct auto_pin_cfg *cfg)
1632 struct hda_gen_spec *spec = codec->spec;
1633 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1636 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1637 cfg->line_out_pins[0], cfg->line_out_pins[1],
1638 cfg->line_out_pins[2], cfg->line_out_pins[3],
1639 spec->multiout.dac_nids[0],
1640 spec->multiout.dac_nids[1],
1641 spec->multiout.dac_nids[2],
1642 spec->multiout.dac_nids[3],
1643 lo_type[cfg->line_out_type]);
1644 for (i = 0; i < cfg->line_outs; i++)
1645 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1646 if (spec->multi_ios > 0)
1647 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1649 spec->multi_io[0].pin, spec->multi_io[1].pin,
1650 spec->multi_io[0].dac, spec->multi_io[1].dac);
1651 for (i = 0; i < spec->multi_ios; i++)
1652 print_nid_path_idx(codec, " mio",
1653 spec->out_paths[cfg->line_outs + i]);
1655 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1656 cfg->hp_pins[0], cfg->hp_pins[1],
1657 cfg->hp_pins[2], cfg->hp_pins[3],
1658 spec->multiout.hp_out_nid[0],
1659 spec->multiout.hp_out_nid[1],
1660 spec->multiout.hp_out_nid[2],
1661 spec->multiout.hp_out_nid[3]);
1662 for (i = 0; i < cfg->hp_outs; i++)
1663 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1664 if (cfg->speaker_outs)
1665 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1666 cfg->speaker_pins[0], cfg->speaker_pins[1],
1667 cfg->speaker_pins[2], cfg->speaker_pins[3],
1668 spec->multiout.extra_out_nid[0],
1669 spec->multiout.extra_out_nid[1],
1670 spec->multiout.extra_out_nid[2],
1671 spec->multiout.extra_out_nid[3]);
1672 for (i = 0; i < cfg->speaker_outs; i++)
1673 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1674 for (i = 0; i < 3; i++)
1675 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1678 #define debug_show_configs(codec, cfg) /* NOP */
1681 /* find all available DACs of the codec */
1682 static void fill_all_dac_nids(struct hda_codec *codec)
1684 struct hda_gen_spec *spec = codec->spec;
1686 hda_nid_t nid = codec->start_nid;
1688 spec->num_all_dacs = 0;
1689 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1690 for (i = 0; i < codec->num_nodes; i++, nid++) {
1691 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1693 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1694 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1697 spec->all_dacs[spec->num_all_dacs++] = nid;
1701 static int parse_output_paths(struct hda_codec *codec)
1703 struct hda_gen_spec *spec = codec->spec;
1704 struct auto_pin_cfg *cfg = &spec->autocfg;
1705 struct auto_pin_cfg *best_cfg;
1707 int best_badness = INT_MAX;
1709 bool fill_hardwired = true, fill_mio_first = true;
1710 bool best_wired = true, best_mio = true;
1711 bool hp_spk_swapped = false;
1713 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1719 badness = fill_and_eval_dacs(codec, fill_hardwired,
1725 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1726 cfg->line_out_type, fill_hardwired, fill_mio_first,
1728 debug_show_configs(codec, cfg);
1729 if (badness < best_badness) {
1730 best_badness = badness;
1732 best_wired = fill_hardwired;
1733 best_mio = fill_mio_first;
1737 fill_mio_first = !fill_mio_first;
1738 if (!fill_mio_first)
1740 fill_hardwired = !fill_hardwired;
1741 if (!fill_hardwired)
1745 hp_spk_swapped = true;
1746 if (cfg->speaker_outs > 0 &&
1747 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1748 cfg->hp_outs = cfg->line_outs;
1749 memcpy(cfg->hp_pins, cfg->line_out_pins,
1750 sizeof(cfg->hp_pins));
1751 cfg->line_outs = cfg->speaker_outs;
1752 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1753 sizeof(cfg->speaker_pins));
1754 cfg->speaker_outs = 0;
1755 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1756 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1757 fill_hardwired = true;
1760 if (cfg->hp_outs > 0 &&
1761 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1762 cfg->speaker_outs = cfg->line_outs;
1763 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1764 sizeof(cfg->speaker_pins));
1765 cfg->line_outs = cfg->hp_outs;
1766 memcpy(cfg->line_out_pins, cfg->hp_pins,
1767 sizeof(cfg->hp_pins));
1769 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1770 cfg->line_out_type = AUTO_PIN_HP_OUT;
1771 fill_hardwired = true;
1778 debug_badness("==> restoring best_cfg\n");
1780 fill_and_eval_dacs(codec, best_wired, best_mio);
1782 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1783 cfg->line_out_type, best_wired, best_mio);
1784 debug_show_configs(codec, cfg);
1786 if (cfg->line_out_pins[0]) {
1787 struct nid_path *path;
1788 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1790 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1791 if (spec->vmaster_nid)
1792 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1793 HDA_OUTPUT, spec->vmaster_tlv);
1796 /* set initial pinctl targets */
1797 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1801 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1802 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1803 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1804 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1805 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1806 set_pin_targets(codec, cfg->speaker_outs,
1807 cfg->speaker_pins, val);
1810 /* clear indep_hp flag if not available */
1811 if (spec->indep_hp && !indep_hp_possible(codec))
1818 /* add playback controls from the parsed DAC table */
1819 static int create_multi_out_ctls(struct hda_codec *codec,
1820 const struct auto_pin_cfg *cfg)
1822 struct hda_gen_spec *spec = codec->spec;
1823 int i, err, noutputs;
1825 noutputs = cfg->line_outs;
1826 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1827 noutputs += spec->multi_ios;
1829 for (i = 0; i < noutputs; i++) {
1832 struct nid_path *path;
1834 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1838 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
1839 if (!name || !strcmp(name, "CLFE")) {
1841 err = add_vol_ctl(codec, "Center", 0, 1, path);
1844 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1848 err = add_stereo_vol(codec, name, index, path);
1853 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1854 if (!name || !strcmp(name, "CLFE")) {
1855 err = add_sw_ctl(codec, "Center", 0, 1, path);
1858 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1862 err = add_stereo_sw(codec, name, index, path);
1870 static int create_extra_out(struct hda_codec *codec, int path_idx,
1871 const char *pfx, int cidx)
1873 struct nid_path *path;
1876 path = snd_hda_get_path_from_idx(codec, path_idx);
1879 err = add_stereo_vol(codec, pfx, cidx, path);
1882 err = add_stereo_sw(codec, pfx, cidx, path);
1888 /* add playback controls for speaker and HP outputs */
1889 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1890 const int *paths, const char *pfx)
1894 for (i = 0; i < num_pins; i++) {
1899 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1900 name = "Bass Speaker";
1901 else if (num_pins >= 3) {
1902 snprintf(tmp, sizeof(tmp), "%s %s",
1903 pfx, channel_name[i]);
1909 err = create_extra_out(codec, paths[i], name, idx);
1916 static int create_hp_out_ctls(struct hda_codec *codec)
1918 struct hda_gen_spec *spec = codec->spec;
1919 return create_extra_outs(codec, spec->autocfg.hp_outs,
1924 static int create_speaker_out_ctls(struct hda_codec *codec)
1926 struct hda_gen_spec *spec = codec->spec;
1927 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1928 spec->speaker_paths,
1933 * independent HP controls
1936 /* update HP auto-mute state too */
1937 static void update_hp_automute_hook(struct hda_codec *codec)
1939 struct hda_gen_spec *spec = codec->spec;
1941 if (spec->hp_automute_hook)
1942 spec->hp_automute_hook(codec, NULL);
1944 snd_hda_gen_hp_automute(codec, NULL);
1947 static int indep_hp_info(struct snd_kcontrol *kcontrol,
1948 struct snd_ctl_elem_info *uinfo)
1950 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1953 static int indep_hp_get(struct snd_kcontrol *kcontrol,
1954 struct snd_ctl_elem_value *ucontrol)
1956 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1957 struct hda_gen_spec *spec = codec->spec;
1958 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1962 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1963 int nomix_path_idx, int mix_path_idx,
1966 static int indep_hp_put(struct snd_kcontrol *kcontrol,
1967 struct snd_ctl_elem_value *ucontrol)
1969 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1970 struct hda_gen_spec *spec = codec->spec;
1971 unsigned int select = ucontrol->value.enumerated.item[0];
1974 mutex_lock(&spec->pcm_mutex);
1975 if (spec->active_streams) {
1980 if (spec->indep_hp_enabled != select) {
1982 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1983 dacp = &spec->private_dac_nids[0];
1985 dacp = &spec->multiout.hp_out_nid[0];
1987 /* update HP aamix paths in case it conflicts with indep HP */
1988 if (spec->have_aamix_ctl) {
1989 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1990 update_aamix_paths(codec, spec->aamix_mode,
1992 spec->aamix_out_paths[0],
1993 spec->autocfg.line_out_type);
1995 update_aamix_paths(codec, spec->aamix_mode,
1997 spec->aamix_out_paths[1],
2001 spec->indep_hp_enabled = select;
2002 if (spec->indep_hp_enabled)
2005 *dacp = spec->alt_dac_nid;
2007 update_hp_automute_hook(codec);
2011 mutex_unlock(&spec->pcm_mutex);
2015 static const struct snd_kcontrol_new indep_hp_ctl = {
2016 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2017 .name = "Independent HP",
2018 .info = indep_hp_info,
2019 .get = indep_hp_get,
2020 .put = indep_hp_put,
2024 static int create_indep_hp_ctls(struct hda_codec *codec)
2026 struct hda_gen_spec *spec = codec->spec;
2029 if (!spec->indep_hp)
2031 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2032 dac = spec->multiout.dac_nids[0];
2034 dac = spec->multiout.hp_out_nid[0];
2040 spec->indep_hp_enabled = false;
2041 spec->alt_dac_nid = dac;
2042 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2048 * channel mode enum control
2051 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2052 struct snd_ctl_elem_info *uinfo)
2054 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2055 struct hda_gen_spec *spec = codec->spec;
2058 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2060 uinfo->value.enumerated.items = spec->multi_ios + 1;
2061 if (uinfo->value.enumerated.item > spec->multi_ios)
2062 uinfo->value.enumerated.item = spec->multi_ios;
2063 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2064 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2068 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2069 struct snd_ctl_elem_value *ucontrol)
2071 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2072 struct hda_gen_spec *spec = codec->spec;
2073 ucontrol->value.enumerated.item[0] =
2074 (spec->ext_channel_count - spec->min_channel_count) / 2;
2078 static inline struct nid_path *
2079 get_multiio_path(struct hda_codec *codec, int idx)
2081 struct hda_gen_spec *spec = codec->spec;
2082 return snd_hda_get_path_from_idx(codec,
2083 spec->out_paths[spec->autocfg.line_outs + idx]);
2086 static void update_automute_all(struct hda_codec *codec);
2088 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2089 * used for output paths
2091 static bool aamix_default(struct hda_gen_spec *spec)
2093 return !spec->have_aamix_ctl || spec->aamix_mode;
2096 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2098 struct hda_gen_spec *spec = codec->spec;
2099 hda_nid_t nid = spec->multi_io[idx].pin;
2100 struct nid_path *path;
2102 path = get_multiio_path(codec, idx);
2106 if (path->active == output)
2110 set_pin_target(codec, nid, PIN_OUT, true);
2111 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2112 set_pin_eapd(codec, nid, true);
2114 set_pin_eapd(codec, nid, false);
2115 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2116 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2117 path_power_down_sync(codec, path);
2120 /* update jack retasking in case it modifies any of them */
2121 update_automute_all(codec);
2126 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2127 struct snd_ctl_elem_value *ucontrol)
2129 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2130 struct hda_gen_spec *spec = codec->spec;
2133 ch = ucontrol->value.enumerated.item[0];
2134 if (ch < 0 || ch > spec->multi_ios)
2136 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2138 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2139 for (i = 0; i < spec->multi_ios; i++)
2140 set_multi_io(codec, i, i < ch);
2141 spec->multiout.max_channels = max(spec->ext_channel_count,
2142 spec->const_channel_count);
2143 if (spec->need_dac_fix)
2144 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2148 static const struct snd_kcontrol_new channel_mode_enum = {
2149 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2150 .name = "Channel Mode",
2151 .info = ch_mode_info,
2156 static int create_multi_channel_mode(struct hda_codec *codec)
2158 struct hda_gen_spec *spec = codec->spec;
2160 if (spec->multi_ios > 0) {
2161 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2168 * aamix loopback enable/disable switch
2171 #define loopback_mixing_info indep_hp_info
2173 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2174 struct snd_ctl_elem_value *ucontrol)
2176 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2177 struct hda_gen_spec *spec = codec->spec;
2178 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2182 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2183 int nomix_path_idx, int mix_path_idx,
2186 struct hda_gen_spec *spec = codec->spec;
2187 struct nid_path *nomix_path, *mix_path;
2189 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2190 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2191 if (!nomix_path || !mix_path)
2194 /* if HP aamix path is driven from a different DAC and the
2195 * independent HP mode is ON, can't turn on aamix path
2197 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2198 mix_path->path[0] != spec->alt_dac_nid)
2202 snd_hda_activate_path(codec, nomix_path, false, true);
2203 snd_hda_activate_path(codec, mix_path, true, true);
2204 path_power_down_sync(codec, nomix_path);
2206 snd_hda_activate_path(codec, mix_path, false, false);
2207 snd_hda_activate_path(codec, nomix_path, true, false);
2208 path_power_down_sync(codec, mix_path);
2212 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2213 struct snd_ctl_elem_value *ucontrol)
2215 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2216 struct hda_gen_spec *spec = codec->spec;
2217 unsigned int val = ucontrol->value.enumerated.item[0];
2219 if (val == spec->aamix_mode)
2221 spec->aamix_mode = val;
2222 update_aamix_paths(codec, val, spec->out_paths[0],
2223 spec->aamix_out_paths[0],
2224 spec->autocfg.line_out_type);
2225 update_aamix_paths(codec, val, spec->hp_paths[0],
2226 spec->aamix_out_paths[1],
2228 update_aamix_paths(codec, val, spec->speaker_paths[0],
2229 spec->aamix_out_paths[2],
2230 AUTO_PIN_SPEAKER_OUT);
2234 static const struct snd_kcontrol_new loopback_mixing_enum = {
2235 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2236 .name = "Loopback Mixing",
2237 .info = loopback_mixing_info,
2238 .get = loopback_mixing_get,
2239 .put = loopback_mixing_put,
2242 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2244 struct hda_gen_spec *spec = codec->spec;
2246 if (!spec->mixer_nid)
2248 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2249 spec->aamix_out_paths[2]))
2251 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2253 spec->have_aamix_ctl = 1;
2258 * shared headphone/mic handling
2261 static void call_update_outputs(struct hda_codec *codec);
2263 /* for shared I/O, change the pin-control accordingly */
2264 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2266 struct hda_gen_spec *spec = codec->spec;
2271 pin = spec->hp_mic_pin;
2272 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2275 val = snd_hda_codec_get_pin_target(codec, pin);
2285 val = snd_hda_get_default_vref(codec, pin);
2286 /* if the HP pin doesn't support VREF and the codec driver gives an
2287 * alternative pin, set up the VREF on that pin instead
2289 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2290 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2291 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2292 if (vref_val != AC_PINCTL_VREF_HIZ)
2293 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2294 PIN_IN | (as_mic ? vref_val : 0));
2297 if (!spec->hp_mic_jack_modes) {
2302 set_pin_target(codec, pin, val, true);
2303 update_hp_automute_hook(codec);
2307 /* create a shared input with the headphone out */
2308 static int create_hp_mic(struct hda_codec *codec)
2310 struct hda_gen_spec *spec = codec->spec;
2311 struct auto_pin_cfg *cfg = &spec->autocfg;
2312 unsigned int defcfg;
2315 if (!spec->hp_mic) {
2316 if (spec->suppress_hp_mic_detect)
2318 /* automatic detection: only if no input or a single internal
2319 * input pin is found, try to detect the shared hp/mic
2321 if (cfg->num_inputs > 1)
2323 else if (cfg->num_inputs == 1) {
2324 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2325 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2330 spec->hp_mic = 0; /* clear once */
2331 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2335 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2336 nid = cfg->line_out_pins[0];
2337 else if (cfg->hp_outs > 0)
2338 nid = cfg->hp_pins[0];
2342 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2343 return 0; /* no input */
2345 cfg->inputs[cfg->num_inputs].pin = nid;
2346 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2347 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2350 spec->hp_mic_pin = nid;
2351 /* we can't handle auto-mic together with HP-mic */
2352 spec->suppress_auto_mic = 1;
2353 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2361 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2363 static const char * const out_jack_texts[] = {
2364 "Line Out", "Headphone Out",
2367 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2368 struct snd_ctl_elem_info *uinfo)
2370 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2373 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2374 struct snd_ctl_elem_value *ucontrol)
2376 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2377 hda_nid_t nid = kcontrol->private_value;
2378 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2379 ucontrol->value.enumerated.item[0] = 1;
2381 ucontrol->value.enumerated.item[0] = 0;
2385 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2386 struct snd_ctl_elem_value *ucontrol)
2388 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2389 hda_nid_t nid = kcontrol->private_value;
2392 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2393 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2395 snd_hda_set_pin_ctl_cache(codec, nid, val);
2399 static const struct snd_kcontrol_new out_jack_mode_enum = {
2400 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2401 .info = out_jack_mode_info,
2402 .get = out_jack_mode_get,
2403 .put = out_jack_mode_put,
2406 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2408 struct hda_gen_spec *spec = codec->spec;
2411 for (i = 0; i < spec->kctls.used; i++) {
2412 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2413 if (!strcmp(kctl->name, name) && kctl->index == idx)
2419 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2420 char *name, size_t name_len)
2422 struct hda_gen_spec *spec = codec->spec;
2425 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2426 strlcat(name, " Jack Mode", name_len);
2428 for (; find_kctl_name(codec, name, idx); idx++)
2432 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2434 struct hda_gen_spec *spec = codec->spec;
2435 if (spec->add_jack_modes) {
2436 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2437 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2443 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2446 struct hda_gen_spec *spec = codec->spec;
2449 for (i = 0; i < num_pins; i++) {
2450 hda_nid_t pin = pins[i];
2451 if (pin == spec->hp_mic_pin) {
2452 int ret = create_hp_mic_jack_mode(codec, pin);
2457 if (get_out_jack_num_items(codec, pin) > 1) {
2458 struct snd_kcontrol_new *knew;
2460 get_jack_mode_name(codec, pin, name, sizeof(name));
2461 knew = snd_hda_gen_add_kctl(spec, name,
2462 &out_jack_mode_enum);
2465 knew->private_value = pin;
2476 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2479 static const char * const vref_texts[NUM_VREFS] = {
2480 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2481 "", "Mic 80pc Bias", "Mic 100pc Bias"
2484 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2486 unsigned int pincap;
2488 pincap = snd_hda_query_pin_caps(codec, pin);
2489 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2490 /* filter out unusual vrefs */
2491 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2495 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2496 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2498 unsigned int i, n = 0;
2500 for (i = 0; i < NUM_VREFS; i++) {
2501 if (vref_caps & (1 << i)) {
2510 /* convert back from the vref ctl index to the enum item index */
2511 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2513 unsigned int i, n = 0;
2515 for (i = 0; i < NUM_VREFS; i++) {
2518 if (vref_caps & (1 << i))
2524 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2525 struct snd_ctl_elem_info *uinfo)
2527 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2528 hda_nid_t nid = kcontrol->private_value;
2529 unsigned int vref_caps = get_vref_caps(codec, nid);
2531 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2533 /* set the right text */
2534 strcpy(uinfo->value.enumerated.name,
2535 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2539 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2540 struct snd_ctl_elem_value *ucontrol)
2542 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2543 hda_nid_t nid = kcontrol->private_value;
2544 unsigned int vref_caps = get_vref_caps(codec, nid);
2547 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2548 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2552 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2553 struct snd_ctl_elem_value *ucontrol)
2555 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2556 hda_nid_t nid = kcontrol->private_value;
2557 unsigned int vref_caps = get_vref_caps(codec, nid);
2558 unsigned int val, idx;
2560 val = snd_hda_codec_get_pin_target(codec, nid);
2561 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2562 if (idx == ucontrol->value.enumerated.item[0])
2565 val &= ~AC_PINCTL_VREFEN;
2566 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2567 snd_hda_set_pin_ctl_cache(codec, nid, val);
2571 static const struct snd_kcontrol_new in_jack_mode_enum = {
2572 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2573 .info = in_jack_mode_info,
2574 .get = in_jack_mode_get,
2575 .put = in_jack_mode_put,
2578 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2580 struct hda_gen_spec *spec = codec->spec;
2582 if (spec->add_jack_modes)
2583 nitems = hweight32(get_vref_caps(codec, pin));
2584 return nitems ? nitems : 1;
2587 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2589 struct hda_gen_spec *spec = codec->spec;
2590 struct snd_kcontrol_new *knew;
2592 unsigned int defcfg;
2594 if (pin == spec->hp_mic_pin)
2595 return 0; /* already done in create_out_jack_mode() */
2597 /* no jack mode for fixed pins */
2598 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2599 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2602 /* no multiple vref caps? */
2603 if (get_in_jack_num_items(codec, pin) <= 1)
2606 get_jack_mode_name(codec, pin, name, sizeof(name));
2607 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2610 knew->private_value = pin;
2615 * HP/mic shared jack mode
2617 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2618 struct snd_ctl_elem_info *uinfo)
2620 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2621 hda_nid_t nid = kcontrol->private_value;
2622 int out_jacks = get_out_jack_num_items(codec, nid);
2623 int in_jacks = get_in_jack_num_items(codec, nid);
2624 const char *text = NULL;
2627 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2629 uinfo->value.enumerated.items = out_jacks + in_jacks;
2630 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2631 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2632 idx = uinfo->value.enumerated.item;
2633 if (idx < out_jacks) {
2635 text = out_jack_texts[idx];
2637 text = "Headphone Out";
2641 unsigned int vref_caps = get_vref_caps(codec, nid);
2642 text = vref_texts[get_vref_idx(vref_caps, idx)];
2647 strcpy(uinfo->value.enumerated.name, text);
2651 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2653 int out_jacks = get_out_jack_num_items(codec, nid);
2654 int in_jacks = get_in_jack_num_items(codec, nid);
2655 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2658 if (val & PIN_OUT) {
2659 if (out_jacks > 1 && val == PIN_HP)
2661 } else if (val & PIN_IN) {
2664 unsigned int vref_caps = get_vref_caps(codec, nid);
2665 val &= AC_PINCTL_VREFEN;
2666 idx += cvt_from_vref_idx(vref_caps, val);
2672 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2673 struct snd_ctl_elem_value *ucontrol)
2675 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2676 hda_nid_t nid = kcontrol->private_value;
2677 ucontrol->value.enumerated.item[0] =
2678 get_cur_hp_mic_jack_mode(codec, nid);
2682 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2683 struct snd_ctl_elem_value *ucontrol)
2685 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2686 hda_nid_t nid = kcontrol->private_value;
2687 int out_jacks = get_out_jack_num_items(codec, nid);
2688 int in_jacks = get_in_jack_num_items(codec, nid);
2689 unsigned int val, oldval, idx;
2691 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2692 idx = ucontrol->value.enumerated.item[0];
2696 if (idx < out_jacks) {
2698 val = idx ? PIN_HP : PIN_OUT;
2704 unsigned int vref_caps = get_vref_caps(codec, nid);
2705 val = snd_hda_codec_get_pin_target(codec, nid);
2706 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2707 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
2709 val = snd_hda_get_default_vref(codec, nid);
2711 snd_hda_set_pin_ctl_cache(codec, nid, val);
2712 update_hp_automute_hook(codec);
2717 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2718 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2719 .info = hp_mic_jack_mode_info,
2720 .get = hp_mic_jack_mode_get,
2721 .put = hp_mic_jack_mode_put,
2724 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2726 struct hda_gen_spec *spec = codec->spec;
2727 struct snd_kcontrol_new *knew;
2729 if (get_out_jack_num_items(codec, pin) <= 1 &&
2730 get_in_jack_num_items(codec, pin) <= 1)
2731 return 0; /* no need */
2732 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2733 &hp_mic_jack_mode_enum);
2736 knew->private_value = pin;
2737 spec->hp_mic_jack_modes = 1;
2745 /* add the powersave loopback-list entry */
2746 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2748 struct hda_amp_list *list;
2750 list = snd_array_new(&spec->loopback_list);
2754 list->dir = HDA_INPUT;
2756 spec->loopback.amplist = spec->loopback_list.list;
2760 /* create input playback/capture controls for the given pin */
2761 static int new_analog_input(struct hda_codec *codec, int input_idx,
2762 hda_nid_t pin, const char *ctlname, int ctlidx,
2765 struct hda_gen_spec *spec = codec->spec;
2766 struct nid_path *path;
2770 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2771 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2772 return 0; /* no need for analog loopback */
2774 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
2777 print_nid_path("loopback", path);
2778 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
2780 idx = path->idx[path->depth - 1];
2781 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2782 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2783 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
2786 path->ctls[NID_PATH_VOL_CTL] = val;
2789 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2790 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2791 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
2794 path->ctls[NID_PATH_MUTE_CTL] = val;
2797 path->active = true;
2798 err = add_loopback_list(spec, mix_nid, idx);
2802 if (spec->mixer_nid != spec->mixer_merge_nid &&
2803 !spec->loopback_merge_path) {
2804 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2805 spec->mixer_merge_nid, 0);
2807 print_nid_path("loopback-merge", path);
2808 path->active = true;
2809 spec->loopback_merge_path =
2810 snd_hda_get_path_idx(codec, path);
2817 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2819 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2820 return (pincap & AC_PINCAP_IN) != 0;
2823 /* Parse the codec tree and retrieve ADCs */
2824 static int fill_adc_nids(struct hda_codec *codec)
2826 struct hda_gen_spec *spec = codec->spec;
2828 hda_nid_t *adc_nids = spec->adc_nids;
2829 int max_nums = ARRAY_SIZE(spec->adc_nids);
2832 nid = codec->start_nid;
2833 for (i = 0; i < codec->num_nodes; i++, nid++) {
2834 unsigned int caps = get_wcaps(codec, nid);
2835 int type = get_wcaps_type(caps);
2837 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2839 adc_nids[nums] = nid;
2840 if (++nums >= max_nums)
2843 spec->num_adc_nids = nums;
2845 /* copy the detected ADCs to all_adcs[] */
2846 spec->num_all_adcs = nums;
2847 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2852 /* filter out invalid adc_nids that don't give all active input pins;
2853 * if needed, check whether dynamic ADC-switching is available
2855 static int check_dyn_adc_switch(struct hda_codec *codec)
2857 struct hda_gen_spec *spec = codec->spec;
2858 struct hda_input_mux *imux = &spec->input_mux;
2859 unsigned int ok_bits;
2864 for (n = 0; n < spec->num_adc_nids; n++) {
2865 for (i = 0; i < imux->num_items; i++) {
2866 if (!spec->input_paths[i][n])
2869 if (i >= imux->num_items) {
2870 ok_bits |= (1 << n);
2876 /* check whether ADC-switch is possible */
2877 for (i = 0; i < imux->num_items; i++) {
2878 for (n = 0; n < spec->num_adc_nids; n++) {
2879 if (spec->input_paths[i][n]) {
2880 spec->dyn_adc_idx[i] = n;
2886 snd_printdd("hda-codec: enabling ADC switching\n");
2887 spec->dyn_adc_switch = 1;
2888 } else if (nums != spec->num_adc_nids) {
2889 /* shrink the invalid adcs and input paths */
2891 for (n = 0; n < spec->num_adc_nids; n++) {
2892 if (!(ok_bits & (1 << n)))
2895 spec->adc_nids[nums] = spec->adc_nids[n];
2896 for (i = 0; i < imux->num_items; i++) {
2897 invalidate_nid_path(codec,
2898 spec->input_paths[i][nums]);
2899 spec->input_paths[i][nums] =
2900 spec->input_paths[i][n];
2905 spec->num_adc_nids = nums;
2908 if (imux->num_items == 1 ||
2909 (imux->num_items == 2 && spec->hp_mic)) {
2910 snd_printdd("hda-codec: reducing to a single ADC\n");
2911 spec->num_adc_nids = 1; /* reduce to a single ADC */
2914 /* single index for individual volumes ctls */
2915 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2916 spec->num_adc_nids = 1;
2921 /* parse capture source paths from the given pin and create imux items */
2922 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2923 int cfg_idx, int num_adcs,
2924 const char *label, int anchor)
2926 struct hda_gen_spec *spec = codec->spec;
2927 struct hda_input_mux *imux = &spec->input_mux;
2928 int imux_idx = imux->num_items;
2929 bool imux_added = false;
2932 for (c = 0; c < num_adcs; c++) {
2933 struct nid_path *path;
2934 hda_nid_t adc = spec->adc_nids[c];
2936 if (!is_reachable_path(codec, pin, adc))
2938 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2941 print_nid_path("input", path);
2942 spec->input_paths[imux_idx][c] =
2943 snd_hda_get_path_idx(codec, path);
2946 if (spec->hp_mic_pin == pin)
2947 spec->hp_mic_mux_idx = imux->num_items;
2948 spec->imux_pins[imux->num_items] = pin;
2949 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
2958 * create playback/capture controls for input pins
2961 /* fill the label for each input at first */
2962 static int fill_input_pin_labels(struct hda_codec *codec)
2964 struct hda_gen_spec *spec = codec->spec;
2965 const struct auto_pin_cfg *cfg = &spec->autocfg;
2968 for (i = 0; i < cfg->num_inputs; i++) {
2969 hda_nid_t pin = cfg->inputs[i].pin;
2973 if (!is_input_pin(codec, pin))
2976 label = hda_get_autocfg_input_label(codec, cfg, i);
2978 for (j = i - 1; j >= 0; j--) {
2979 if (spec->input_labels[j] &&
2980 !strcmp(spec->input_labels[j], label)) {
2981 idx = spec->input_label_idxs[j] + 1;
2986 spec->input_labels[i] = label;
2987 spec->input_label_idxs[i] = idx;
2993 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2995 static int create_input_ctls(struct hda_codec *codec)
2997 struct hda_gen_spec *spec = codec->spec;
2998 const struct auto_pin_cfg *cfg = &spec->autocfg;
2999 hda_nid_t mixer = spec->mixer_nid;
3004 num_adcs = fill_adc_nids(codec);
3008 err = fill_input_pin_labels(codec);
3012 for (i = 0; i < cfg->num_inputs; i++) {
3015 pin = cfg->inputs[i].pin;
3016 if (!is_input_pin(codec, pin))
3020 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3021 val |= snd_hda_get_default_vref(codec, pin);
3022 if (pin != spec->hp_mic_pin)
3023 set_pin_target(codec, pin, val, false);
3026 if (is_reachable_path(codec, pin, mixer)) {
3027 err = new_analog_input(codec, i, pin,
3028 spec->input_labels[i],
3029 spec->input_label_idxs[i],
3036 err = parse_capture_source(codec, pin, i, num_adcs,
3037 spec->input_labels[i], -mixer);
3041 if (spec->add_jack_modes) {
3042 err = create_in_jack_mode(codec, pin);
3048 if (mixer && spec->add_stereo_mix_input) {
3049 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3063 /* get the input path specified by the given adc and imux indices */
3064 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3066 struct hda_gen_spec *spec = codec->spec;
3067 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3071 if (spec->dyn_adc_switch)
3072 adc_idx = spec->dyn_adc_idx[imux_idx];
3073 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3077 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3080 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3083 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3084 struct snd_ctl_elem_info *uinfo)
3086 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3087 struct hda_gen_spec *spec = codec->spec;
3088 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3091 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3092 struct snd_ctl_elem_value *ucontrol)
3094 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3095 struct hda_gen_spec *spec = codec->spec;
3096 /* the ctls are created at once with multiple counts */
3097 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3099 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3103 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3104 struct snd_ctl_elem_value *ucontrol)
3106 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3107 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3108 return mux_select(codec, adc_idx,
3109 ucontrol->value.enumerated.item[0]);
3112 static const struct snd_kcontrol_new cap_src_temp = {
3113 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3114 .name = "Input Source",
3115 .info = mux_enum_info,
3116 .get = mux_enum_get,
3117 .put = mux_enum_put,
3121 * capture volume and capture switch ctls
3124 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3125 struct snd_ctl_elem_value *ucontrol);
3127 /* call the given amp update function for all amps in the imux list at once */
3128 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3129 struct snd_ctl_elem_value *ucontrol,
3130 put_call_t func, int type)
3132 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3133 struct hda_gen_spec *spec = codec->spec;
3134 const struct hda_input_mux *imux;
3135 struct nid_path *path;
3136 int i, adc_idx, err = 0;
3138 imux = &spec->input_mux;
3139 adc_idx = kcontrol->id.index;
3140 mutex_lock(&codec->control_mutex);
3141 /* we use the cache-only update at first since multiple input paths
3142 * may shared the same amp; by updating only caches, the redundant
3143 * writes to hardware can be reduced.
3145 codec->cached_write = 1;
3146 for (i = 0; i < imux->num_items; i++) {
3147 path = get_input_path(codec, adc_idx, i);
3148 if (!path || !path->ctls[type])
3150 kcontrol->private_value = path->ctls[type];
3151 err = func(kcontrol, ucontrol);
3156 codec->cached_write = 0;
3157 mutex_unlock(&codec->control_mutex);
3158 snd_hda_codec_flush_cache(codec); /* flush the updates */
3159 if (err >= 0 && spec->cap_sync_hook)
3160 spec->cap_sync_hook(codec, ucontrol);
3164 /* capture volume ctl callbacks */
3165 #define cap_vol_info snd_hda_mixer_amp_volume_info
3166 #define cap_vol_get snd_hda_mixer_amp_volume_get
3167 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3169 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3170 struct snd_ctl_elem_value *ucontrol)
3172 return cap_put_caller(kcontrol, ucontrol,
3173 snd_hda_mixer_amp_volume_put,
3177 static const struct snd_kcontrol_new cap_vol_temp = {
3178 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3179 .name = "Capture Volume",
3180 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3181 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3182 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3183 .info = cap_vol_info,
3186 .tlv = { .c = cap_vol_tlv },
3189 /* capture switch ctl callbacks */
3190 #define cap_sw_info snd_ctl_boolean_stereo_info
3191 #define cap_sw_get snd_hda_mixer_amp_switch_get
3193 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3194 struct snd_ctl_elem_value *ucontrol)
3196 return cap_put_caller(kcontrol, ucontrol,
3197 snd_hda_mixer_amp_switch_put,
3201 static const struct snd_kcontrol_new cap_sw_temp = {
3202 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3203 .name = "Capture Switch",
3204 .info = cap_sw_info,
3209 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3214 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3215 for (depth = 0; depth < 3; depth++) {
3216 if (depth >= path->depth)
3218 i = path->depth - depth - 1;
3219 nid = path->path[i];
3220 if (!path->ctls[NID_PATH_VOL_CTL]) {
3221 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3222 path->ctls[NID_PATH_VOL_CTL] =
3223 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3224 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3225 int idx = path->idx[i];
3226 if (!depth && codec->single_adc_amp)
3228 path->ctls[NID_PATH_VOL_CTL] =
3229 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3232 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3233 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3234 path->ctls[NID_PATH_MUTE_CTL] =
3235 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3236 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3237 int idx = path->idx[i];
3238 if (!depth && codec->single_adc_amp)
3240 path->ctls[NID_PATH_MUTE_CTL] =
3241 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3248 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3250 struct hda_gen_spec *spec = codec->spec;
3251 struct auto_pin_cfg *cfg = &spec->autocfg;
3255 if (!spec->inv_dmic_split)
3257 for (i = 0; i < cfg->num_inputs; i++) {
3258 if (cfg->inputs[i].pin != nid)
3260 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3262 val = snd_hda_codec_get_pincfg(codec, nid);
3263 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3268 /* capture switch put callback for a single control with hook call */
3269 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3270 struct snd_ctl_elem_value *ucontrol)
3272 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3273 struct hda_gen_spec *spec = codec->spec;
3276 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3280 if (spec->cap_sync_hook)
3281 spec->cap_sync_hook(codec, ucontrol);
3286 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3287 int idx, bool is_switch, unsigned int ctl,
3290 struct hda_gen_spec *spec = codec->spec;
3292 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3293 const char *sfx = is_switch ? "Switch" : "Volume";
3294 unsigned int chs = inv_dmic ? 1 : 3;
3295 struct snd_kcontrol_new *knew;
3301 snprintf(tmpname, sizeof(tmpname),
3302 "%s Capture %s", label, sfx);
3304 snprintf(tmpname, sizeof(tmpname),
3306 knew = add_control(spec, type, tmpname, idx,
3307 amp_val_replace_channels(ctl, chs));
3311 knew->put = cap_single_sw_put;
3315 /* Make independent right kcontrol */
3317 snprintf(tmpname, sizeof(tmpname),
3318 "Inverted %s Capture %s", label, sfx);
3320 snprintf(tmpname, sizeof(tmpname),
3321 "Inverted Capture %s", sfx);
3322 knew = add_control(spec, type, tmpname, idx,
3323 amp_val_replace_channels(ctl, 2));
3327 knew->put = cap_single_sw_put;
3331 /* create single (and simple) capture volume and switch controls */
3332 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3333 unsigned int vol_ctl, unsigned int sw_ctl,
3337 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3340 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3346 /* create bound capture volume and switch controls */
3347 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3348 unsigned int vol_ctl, unsigned int sw_ctl)
3350 struct hda_gen_spec *spec = codec->spec;
3351 struct snd_kcontrol_new *knew;
3354 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3358 knew->private_value = vol_ctl;
3359 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3362 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3366 knew->private_value = sw_ctl;
3367 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3372 /* return the vol ctl when used first in the imux list */
3373 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3375 struct nid_path *path;
3379 path = get_input_path(codec, 0, idx);
3382 ctl = path->ctls[type];
3385 for (i = 0; i < idx - 1; i++) {
3386 path = get_input_path(codec, 0, i);
3387 if (path && path->ctls[type] == ctl)
3393 /* create individual capture volume and switch controls per input */
3394 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3396 struct hda_gen_spec *spec = codec->spec;
3397 struct hda_input_mux *imux = &spec->input_mux;
3400 for (i = 0; i < imux->num_items; i++) {
3404 idx = imux->items[i].index;
3405 if (idx >= spec->autocfg.num_inputs)
3407 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3409 for (type = 0; type < 2; type++) {
3410 err = add_single_cap_ctl(codec,
3411 spec->input_labels[idx],
3412 spec->input_label_idxs[idx],
3414 get_first_cap_ctl(codec, i, type),
3423 static int create_capture_mixers(struct hda_codec *codec)
3425 struct hda_gen_spec *spec = codec->spec;
3426 struct hda_input_mux *imux = &spec->input_mux;
3427 int i, n, nums, err;
3429 if (spec->dyn_adc_switch)
3432 nums = spec->num_adc_nids;
3434 if (!spec->auto_mic && imux->num_items > 1) {
3435 struct snd_kcontrol_new *knew;
3437 name = nums > 1 ? "Input Source" : "Capture Source";
3438 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3444 for (n = 0; n < nums; n++) {
3446 bool multi_cap_vol = spec->multi_cap_vol;
3447 bool inv_dmic = false;
3451 for (i = 0; i < imux->num_items; i++) {
3452 struct nid_path *path;
3453 path = get_input_path(codec, n, i);
3456 parse_capvol_in_path(codec, path);
3458 vol = path->ctls[NID_PATH_VOL_CTL];
3459 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3461 if (!same_amp_caps(codec, vol,
3462 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3463 multi_cap_vol = true;
3466 sw = path->ctls[NID_PATH_MUTE_CTL];
3467 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3469 if (!same_amp_caps(codec, sw,
3470 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3471 multi_cap_vol = true;
3473 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3478 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3480 else if (!multi_cap_vol)
3481 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3483 err = create_multi_cap_vol_ctl(codec);
3492 * add mic boosts if needed
3495 /* check whether the given amp is feasible as a boost volume */
3496 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3501 if (!nid_has_volume(codec, nid, dir) ||
3502 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3503 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3506 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3507 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3513 /* look for a boost amp in a widget close to the pin */
3514 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3515 struct nid_path *path)
3517 unsigned int val = 0;
3521 for (depth = 0; depth < 3; depth++) {
3522 if (depth >= path->depth - 1)
3524 nid = path->path[depth];
3525 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3526 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3528 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3529 path->idx[depth])) {
3530 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3539 static int parse_mic_boost(struct hda_codec *codec)
3541 struct hda_gen_spec *spec = codec->spec;
3542 struct auto_pin_cfg *cfg = &spec->autocfg;
3543 struct hda_input_mux *imux = &spec->input_mux;
3546 if (!spec->num_adc_nids)
3549 for (i = 0; i < imux->num_items; i++) {
3550 struct nid_path *path;
3553 char boost_label[44];
3555 idx = imux->items[i].index;
3556 if (idx >= imux->num_items)
3559 /* check only line-in and mic pins */
3560 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3563 path = get_input_path(codec, 0, i);
3567 val = look_for_boost_amp(codec, path);
3571 /* create a boost control */
3572 snprintf(boost_label, sizeof(boost_label),
3573 "%s Boost Volume", spec->input_labels[idx]);
3574 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3575 spec->input_label_idxs[idx], val))
3578 path->ctls[NID_PATH_BOOST_CTL] = val;
3584 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3586 static void parse_digital(struct hda_codec *codec)
3588 struct hda_gen_spec *spec = codec->spec;
3589 struct nid_path *path;
3591 hda_nid_t dig_nid, pin;
3593 /* support multiple SPDIFs; the secondary is set up as a slave */
3595 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3596 pin = spec->autocfg.dig_out_pins[i];
3597 dig_nid = look_for_dac(codec, pin, true);
3600 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3603 print_nid_path("digout", path);
3604 path->active = true;
3605 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3606 set_pin_target(codec, pin, PIN_OUT, false);
3608 spec->multiout.dig_out_nid = dig_nid;
3609 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3611 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3612 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3614 spec->slave_dig_outs[nums - 1] = dig_nid;
3619 if (spec->autocfg.dig_in_pin) {
3620 pin = spec->autocfg.dig_in_pin;
3621 dig_nid = codec->start_nid;
3622 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3623 unsigned int wcaps = get_wcaps(codec, dig_nid);
3624 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3626 if (!(wcaps & AC_WCAP_DIGITAL))
3628 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3630 print_nid_path("digin", path);
3631 path->active = true;
3632 spec->dig_in_nid = dig_nid;
3633 spec->digin_path = snd_hda_get_path_idx(codec, path);
3634 set_pin_target(codec, pin, PIN_IN, false);
3643 * input MUX handling
3646 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3648 /* select the given imux item; either unmute exclusively or select the route */
3649 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3652 struct hda_gen_spec *spec = codec->spec;
3653 const struct hda_input_mux *imux;
3654 struct nid_path *old_path, *path;
3656 imux = &spec->input_mux;
3657 if (!imux->num_items)
3660 if (idx >= imux->num_items)
3661 idx = imux->num_items - 1;
3662 if (spec->cur_mux[adc_idx] == idx)
3665 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3668 if (old_path->active)
3669 snd_hda_activate_path(codec, old_path, false, false);
3671 spec->cur_mux[adc_idx] = idx;
3674 update_hp_mic(codec, adc_idx, false);
3676 if (spec->dyn_adc_switch)
3677 dyn_adc_pcm_resetup(codec, idx);
3679 path = get_input_path(codec, adc_idx, idx);
3684 snd_hda_activate_path(codec, path, true, false);
3685 if (spec->cap_sync_hook)
3686 spec->cap_sync_hook(codec, NULL);
3687 path_power_down_sync(codec, old_path);
3693 * Jack detections for HP auto-mute and mic-switch
3696 /* check each pin in the given array; returns true if any of them is plugged */
3697 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3701 for (i = 0; i < num_pins; i++) {
3702 hda_nid_t nid = pins[i];
3705 /* don't detect pins retasked as inputs */
3706 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3708 present |= snd_hda_jack_detect(codec, nid);
3713 /* standard HP/line-out auto-mute helper */
3714 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
3717 struct hda_gen_spec *spec = codec->spec;
3720 for (i = 0; i < num_pins; i++) {
3721 hda_nid_t nid = pins[i];
3722 unsigned int val, oldval;
3725 oldval = snd_hda_codec_get_pin_target(codec, nid);
3726 if (oldval & PIN_IN)
3727 continue; /* no mute for inputs */
3728 /* don't reset VREF value in case it's controlling
3729 * the amp (see alc861_fixup_asus_amp_vref_0f())
3731 if (spec->keep_vref_in_automute)
3732 val = oldval & ~PIN_HP;
3737 /* here we call update_pin_ctl() so that the pinctl is changed
3738 * without changing the pinctl target value;
3739 * the original target value will be still referred at the
3740 * init / resume again
3742 update_pin_ctl(codec, nid, val);
3743 set_pin_eapd(codec, nid, !mute);
3747 /* Toggle outputs muting */
3748 void snd_hda_gen_update_outputs(struct hda_codec *codec)
3750 struct hda_gen_spec *spec = codec->spec;
3753 /* Control HP pins/amps depending on master_mute state;
3754 * in general, HP pins/amps control should be enabled in all cases,
3755 * but currently set only for master_mute, just to be safe
3757 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3758 spec->autocfg.hp_pins, spec->master_mute);
3760 if (!spec->automute_speaker)
3763 on = spec->hp_jack_present | spec->line_jack_present;
3764 on |= spec->master_mute;
3765 spec->speaker_muted = on;
3766 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
3767 spec->autocfg.speaker_pins, on);
3769 /* toggle line-out mutes if needed, too */
3770 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3771 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3772 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3774 if (!spec->automute_lo)
3777 on = spec->hp_jack_present;
3778 on |= spec->master_mute;
3779 spec->line_out_muted = on;
3780 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3781 spec->autocfg.line_out_pins, on);
3783 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
3785 static void call_update_outputs(struct hda_codec *codec)
3787 struct hda_gen_spec *spec = codec->spec;
3788 if (spec->automute_hook)
3789 spec->automute_hook(codec);
3791 snd_hda_gen_update_outputs(codec);
3794 /* standard HP-automute helper */
3795 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3797 struct hda_gen_spec *spec = codec->spec;
3798 hda_nid_t *pins = spec->autocfg.hp_pins;
3799 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
3801 /* No detection for the first HP jack during indep-HP mode */
3802 if (spec->indep_hp_enabled) {
3807 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
3808 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3810 call_update_outputs(codec);
3812 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
3814 /* standard line-out-automute helper */
3815 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3817 struct hda_gen_spec *spec = codec->spec;
3819 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3821 /* check LO jack only when it's different from HP */
3822 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3825 spec->line_jack_present =
3826 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3827 spec->autocfg.line_out_pins);
3828 if (!spec->automute_speaker || !spec->detect_lo)
3830 call_update_outputs(codec);
3832 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
3834 /* standard mic auto-switch helper */
3835 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
3837 struct hda_gen_spec *spec = codec->spec;
3840 if (!spec->auto_mic)
3843 for (i = spec->am_num_entries - 1; i > 0; i--) {
3844 hda_nid_t pin = spec->am_entry[i].pin;
3845 /* don't detect pins retasked as outputs */
3846 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3848 if (snd_hda_jack_detect(codec, pin)) {
3849 mux_select(codec, 0, spec->am_entry[i].idx);
3853 mux_select(codec, 0, spec->am_entry[0].idx);
3855 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
3857 /* update jack retasking */
3858 static void update_automute_all(struct hda_codec *codec)
3860 struct hda_gen_spec *spec = codec->spec;
3862 update_hp_automute_hook(codec);
3863 if (spec->line_automute_hook)
3864 spec->line_automute_hook(codec, NULL);
3866 snd_hda_gen_line_automute(codec, NULL);
3867 if (spec->mic_autoswitch_hook)
3868 spec->mic_autoswitch_hook(codec, NULL);
3870 snd_hda_gen_mic_autoswitch(codec, NULL);
3874 * Auto-Mute mode mixer enum support
3876 static int automute_mode_info(struct snd_kcontrol *kcontrol,
3877 struct snd_ctl_elem_info *uinfo)
3879 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3880 struct hda_gen_spec *spec = codec->spec;
3881 static const char * const texts3[] = {
3882 "Disabled", "Speaker Only", "Line Out+Speaker"
3885 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3886 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3887 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3890 static int automute_mode_get(struct snd_kcontrol *kcontrol,
3891 struct snd_ctl_elem_value *ucontrol)
3893 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3894 struct hda_gen_spec *spec = codec->spec;
3895 unsigned int val = 0;
3896 if (spec->automute_speaker)
3898 if (spec->automute_lo)
3901 ucontrol->value.enumerated.item[0] = val;
3905 static int automute_mode_put(struct snd_kcontrol *kcontrol,
3906 struct snd_ctl_elem_value *ucontrol)
3908 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3909 struct hda_gen_spec *spec = codec->spec;
3911 switch (ucontrol->value.enumerated.item[0]) {
3913 if (!spec->automute_speaker && !spec->automute_lo)
3915 spec->automute_speaker = 0;
3916 spec->automute_lo = 0;
3919 if (spec->automute_speaker_possible) {
3920 if (!spec->automute_lo && spec->automute_speaker)
3922 spec->automute_speaker = 1;
3923 spec->automute_lo = 0;
3924 } else if (spec->automute_lo_possible) {
3925 if (spec->automute_lo)
3927 spec->automute_lo = 1;
3932 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3934 if (spec->automute_speaker && spec->automute_lo)
3936 spec->automute_speaker = 1;
3937 spec->automute_lo = 1;
3942 call_update_outputs(codec);
3946 static const struct snd_kcontrol_new automute_mode_enum = {
3947 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3948 .name = "Auto-Mute Mode",
3949 .info = automute_mode_info,
3950 .get = automute_mode_get,
3951 .put = automute_mode_put,
3954 static int add_automute_mode_enum(struct hda_codec *codec)
3956 struct hda_gen_spec *spec = codec->spec;
3958 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
3964 * Check the availability of HP/line-out auto-mute;
3965 * Set up appropriately if really supported
3967 static int check_auto_mute_availability(struct hda_codec *codec)
3969 struct hda_gen_spec *spec = codec->spec;
3970 struct auto_pin_cfg *cfg = &spec->autocfg;
3974 if (spec->suppress_auto_mute)
3977 if (cfg->hp_pins[0])
3979 if (cfg->line_out_pins[0])
3981 if (cfg->speaker_pins[0])
3983 if (present < 2) /* need two different output types */
3986 if (!cfg->speaker_pins[0] &&
3987 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3988 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3989 sizeof(cfg->speaker_pins));
3990 cfg->speaker_outs = cfg->line_outs;
3993 if (!cfg->hp_pins[0] &&
3994 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3995 memcpy(cfg->hp_pins, cfg->line_out_pins,
3996 sizeof(cfg->hp_pins));
3997 cfg->hp_outs = cfg->line_outs;
4000 for (i = 0; i < cfg->hp_outs; i++) {
4001 hda_nid_t nid = cfg->hp_pins[i];
4002 if (!is_jack_detectable(codec, nid))
4004 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
4006 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
4007 spec->hp_automute_hook ?
4008 spec->hp_automute_hook :
4009 snd_hda_gen_hp_automute);
4010 spec->detect_hp = 1;
4013 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4014 if (cfg->speaker_outs)
4015 for (i = 0; i < cfg->line_outs; i++) {
4016 hda_nid_t nid = cfg->line_out_pins[i];
4017 if (!is_jack_detectable(codec, nid))
4019 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
4020 snd_hda_jack_detect_enable_callback(codec, nid,
4021 HDA_GEN_FRONT_EVENT,
4022 spec->line_automute_hook ?
4023 spec->line_automute_hook :
4024 snd_hda_gen_line_automute);
4025 spec->detect_lo = 1;
4027 spec->automute_lo_possible = spec->detect_hp;
4030 spec->automute_speaker_possible = cfg->speaker_outs &&
4031 (spec->detect_hp || spec->detect_lo);
4033 spec->automute_lo = spec->automute_lo_possible;
4034 spec->automute_speaker = spec->automute_speaker_possible;
4036 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4037 /* create a control for automute mode */
4038 err = add_automute_mode_enum(codec);
4045 /* check whether all auto-mic pins are valid; setup indices if OK */
4046 static bool auto_mic_check_imux(struct hda_codec *codec)
4048 struct hda_gen_spec *spec = codec->spec;
4049 const struct hda_input_mux *imux;
4052 imux = &spec->input_mux;
4053 for (i = 0; i < spec->am_num_entries; i++) {
4054 spec->am_entry[i].idx =
4055 find_idx_in_nid_list(spec->am_entry[i].pin,
4056 spec->imux_pins, imux->num_items);
4057 if (spec->am_entry[i].idx < 0)
4058 return false; /* no corresponding imux */
4061 /* we don't need the jack detection for the first pin */
4062 for (i = 1; i < spec->am_num_entries; i++)
4063 snd_hda_jack_detect_enable_callback(codec,
4064 spec->am_entry[i].pin,
4066 spec->mic_autoswitch_hook ?
4067 spec->mic_autoswitch_hook :
4068 snd_hda_gen_mic_autoswitch);
4072 static int compare_attr(const void *ap, const void *bp)
4074 const struct automic_entry *a = ap;
4075 const struct automic_entry *b = bp;
4076 return (int)(a->attr - b->attr);
4080 * Check the availability of auto-mic switch;
4081 * Set up if really supported
4083 static int check_auto_mic_availability(struct hda_codec *codec)
4085 struct hda_gen_spec *spec = codec->spec;
4086 struct auto_pin_cfg *cfg = &spec->autocfg;
4090 if (spec->suppress_auto_mic)
4095 for (i = 0; i < cfg->num_inputs; i++) {
4096 hda_nid_t nid = cfg->inputs[i].pin;
4098 attr = snd_hda_codec_get_pincfg(codec, nid);
4099 attr = snd_hda_get_input_pin_attr(attr);
4100 if (types & (1 << attr))
4101 return 0; /* already occupied */
4103 case INPUT_PIN_ATTR_INT:
4104 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4105 return 0; /* invalid type */
4107 case INPUT_PIN_ATTR_UNUSED:
4108 return 0; /* invalid entry */
4110 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4111 return 0; /* invalid type */
4112 if (!spec->line_in_auto_switch &&
4113 cfg->inputs[i].type != AUTO_PIN_MIC)
4114 return 0; /* only mic is allowed */
4115 if (!is_jack_detectable(codec, nid))
4116 return 0; /* no unsol support */
4119 if (num_pins >= MAX_AUTO_MIC_PINS)
4121 types |= (1 << attr);
4122 spec->am_entry[num_pins].pin = nid;
4123 spec->am_entry[num_pins].attr = attr;
4130 spec->am_num_entries = num_pins;
4131 /* sort the am_entry in the order of attr so that the pin with a
4132 * higher attr will be selected when the jack is plugged.
4134 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4135 compare_attr, NULL);
4137 if (!auto_mic_check_imux(codec))
4141 spec->num_adc_nids = 1;
4142 spec->cur_mux[0] = spec->am_entry[0].idx;
4143 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4144 spec->am_entry[0].pin,
4145 spec->am_entry[1].pin,
4146 spec->am_entry[2].pin);
4151 /* power_filter hook; make inactive widgets into power down */
4152 static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4154 unsigned int power_state)
4156 if (power_state != AC_PWRST_D0)
4158 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4160 if (is_active_nid(codec, nid, HDA_OUTPUT, 0))
4167 * Parse the given BIOS configuration and set up the hda_gen_spec
4169 * return 1 if successful, 0 if the proper config is not found,
4170 * or a negative error code
4172 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4173 struct auto_pin_cfg *cfg)
4175 struct hda_gen_spec *spec = codec->spec;
4178 parse_user_hints(codec);
4180 if (spec->mixer_nid && !spec->mixer_merge_nid)
4181 spec->mixer_merge_nid = spec->mixer_nid;
4183 if (cfg != &spec->autocfg) {
4184 spec->autocfg = *cfg;
4185 cfg = &spec->autocfg;
4188 if (!spec->main_out_badness)
4189 spec->main_out_badness = &hda_main_out_badness;
4190 if (!spec->extra_out_badness)
4191 spec->extra_out_badness = &hda_extra_out_badness;
4193 fill_all_dac_nids(codec);
4195 if (!cfg->line_outs) {
4196 if (cfg->dig_outs || cfg->dig_in_pin) {
4197 spec->multiout.max_channels = 2;
4198 spec->no_analog = 1;
4201 return 0; /* can't find valid BIOS pin config */
4204 if (!spec->no_primary_hp &&
4205 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4206 cfg->line_outs <= cfg->hp_outs) {
4207 /* use HP as primary out */
4208 cfg->speaker_outs = cfg->line_outs;
4209 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4210 sizeof(cfg->speaker_pins));
4211 cfg->line_outs = cfg->hp_outs;
4212 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4214 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4215 cfg->line_out_type = AUTO_PIN_HP_OUT;
4218 err = parse_output_paths(codec);
4221 err = create_multi_channel_mode(codec);
4224 err = create_multi_out_ctls(codec, cfg);
4227 err = create_hp_out_ctls(codec);
4230 err = create_speaker_out_ctls(codec);
4233 err = create_indep_hp_ctls(codec);
4236 err = create_loopback_mixing_ctl(codec);
4239 err = create_hp_mic(codec);
4242 err = create_input_ctls(codec);
4246 spec->const_channel_count = spec->ext_channel_count;
4247 /* check the multiple speaker and headphone pins */
4248 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4249 spec->const_channel_count = max(spec->const_channel_count,
4250 cfg->speaker_outs * 2);
4251 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4252 spec->const_channel_count = max(spec->const_channel_count,
4254 spec->multiout.max_channels = max(spec->ext_channel_count,
4255 spec->const_channel_count);
4257 err = check_auto_mute_availability(codec);
4261 err = check_dyn_adc_switch(codec);
4265 err = check_auto_mic_availability(codec);
4269 err = create_capture_mixers(codec);
4273 err = parse_mic_boost(codec);
4277 if (spec->add_jack_modes) {
4278 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4279 err = create_out_jack_modes(codec, cfg->line_outs,
4280 cfg->line_out_pins);
4284 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4285 err = create_out_jack_modes(codec, cfg->hp_outs,
4293 parse_digital(codec);
4295 if (spec->power_down_unused)
4296 codec->power_filter = snd_hda_gen_path_power_filter;
4298 if (!spec->no_analog && spec->beep_nid) {
4299 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4306 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
4310 * Build control elements
4313 /* slave controls for virtual master */
4314 static const char * const slave_pfxs[] = {
4315 "Front", "Surround", "Center", "LFE", "Side",
4316 "Headphone", "Speaker", "Mono", "Line Out",
4317 "CLFE", "Bass Speaker", "PCM",
4318 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4319 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4324 int snd_hda_gen_build_controls(struct hda_codec *codec)
4326 struct hda_gen_spec *spec = codec->spec;
4329 if (spec->kctls.used) {
4330 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4335 if (spec->multiout.dig_out_nid) {
4336 err = snd_hda_create_dig_out_ctls(codec,
4337 spec->multiout.dig_out_nid,
4338 spec->multiout.dig_out_nid,
4339 spec->pcm_rec[1].pcm_type);
4342 if (!spec->no_analog) {
4343 err = snd_hda_create_spdif_share_sw(codec,
4347 spec->multiout.share_spdif = 1;
4350 if (spec->dig_in_nid) {
4351 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4356 /* if we have no master control, let's create it */
4357 if (!spec->no_analog &&
4358 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
4359 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
4360 spec->vmaster_tlv, slave_pfxs,
4365 if (!spec->no_analog &&
4366 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4367 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4370 true, &spec->vmaster_mute.sw_kctl);
4373 if (spec->vmaster_mute.hook)
4374 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4375 spec->vmaster_mute_enum);
4378 free_kctls(spec); /* no longer needed */
4380 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4386 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4393 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4394 struct hda_codec *codec,
4395 struct snd_pcm_substream *substream,
4398 struct hda_gen_spec *spec = codec->spec;
4399 if (spec->pcm_playback_hook)
4400 spec->pcm_playback_hook(hinfo, codec, substream, action);
4403 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4404 struct hda_codec *codec,
4405 struct snd_pcm_substream *substream,
4408 struct hda_gen_spec *spec = codec->spec;
4409 if (spec->pcm_capture_hook)
4410 spec->pcm_capture_hook(hinfo, codec, substream, action);
4414 * Analog playback callbacks
4416 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4417 struct hda_codec *codec,
4418 struct snd_pcm_substream *substream)
4420 struct hda_gen_spec *spec = codec->spec;
4423 mutex_lock(&spec->pcm_mutex);
4424 err = snd_hda_multi_out_analog_open(codec,
4425 &spec->multiout, substream,
4428 spec->active_streams |= 1 << STREAM_MULTI_OUT;
4429 call_pcm_playback_hook(hinfo, codec, substream,
4430 HDA_GEN_PCM_ACT_OPEN);
4432 mutex_unlock(&spec->pcm_mutex);
4436 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4437 struct hda_codec *codec,
4438 unsigned int stream_tag,
4439 unsigned int format,
4440 struct snd_pcm_substream *substream)
4442 struct hda_gen_spec *spec = codec->spec;
4445 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4446 stream_tag, format, substream);
4448 call_pcm_playback_hook(hinfo, codec, substream,
4449 HDA_GEN_PCM_ACT_PREPARE);
4453 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4454 struct hda_codec *codec,
4455 struct snd_pcm_substream *substream)
4457 struct hda_gen_spec *spec = codec->spec;
4460 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4462 call_pcm_playback_hook(hinfo, codec, substream,
4463 HDA_GEN_PCM_ACT_CLEANUP);
4467 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4468 struct hda_codec *codec,
4469 struct snd_pcm_substream *substream)
4471 struct hda_gen_spec *spec = codec->spec;
4472 mutex_lock(&spec->pcm_mutex);
4473 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
4474 call_pcm_playback_hook(hinfo, codec, substream,
4475 HDA_GEN_PCM_ACT_CLOSE);
4476 mutex_unlock(&spec->pcm_mutex);
4480 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4481 struct hda_codec *codec,
4482 struct snd_pcm_substream *substream)
4484 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4488 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4489 struct hda_codec *codec,
4490 unsigned int stream_tag,
4491 unsigned int format,
4492 struct snd_pcm_substream *substream)
4494 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4495 call_pcm_capture_hook(hinfo, codec, substream,
4496 HDA_GEN_PCM_ACT_PREPARE);
4500 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4501 struct hda_codec *codec,
4502 struct snd_pcm_substream *substream)
4504 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4505 call_pcm_capture_hook(hinfo, codec, substream,
4506 HDA_GEN_PCM_ACT_CLEANUP);
4510 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4511 struct hda_codec *codec,
4512 struct snd_pcm_substream *substream)
4514 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4518 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4519 struct hda_codec *codec,
4520 struct snd_pcm_substream *substream)
4522 struct hda_gen_spec *spec = codec->spec;
4525 mutex_lock(&spec->pcm_mutex);
4526 if (!spec->indep_hp_enabled)
4529 spec->active_streams |= 1 << STREAM_INDEP_HP;
4530 call_pcm_playback_hook(hinfo, codec, substream,
4531 HDA_GEN_PCM_ACT_OPEN);
4532 mutex_unlock(&spec->pcm_mutex);
4536 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4537 struct hda_codec *codec,
4538 struct snd_pcm_substream *substream)
4540 struct hda_gen_spec *spec = codec->spec;
4541 mutex_lock(&spec->pcm_mutex);
4542 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
4543 call_pcm_playback_hook(hinfo, codec, substream,
4544 HDA_GEN_PCM_ACT_CLOSE);
4545 mutex_unlock(&spec->pcm_mutex);
4549 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4550 struct hda_codec *codec,
4551 unsigned int stream_tag,
4552 unsigned int format,
4553 struct snd_pcm_substream *substream)
4555 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4556 call_pcm_playback_hook(hinfo, codec, substream,
4557 HDA_GEN_PCM_ACT_PREPARE);
4561 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4562 struct hda_codec *codec,
4563 struct snd_pcm_substream *substream)
4565 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4566 call_pcm_playback_hook(hinfo, codec, substream,
4567 HDA_GEN_PCM_ACT_CLEANUP);
4574 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4575 struct hda_codec *codec,
4576 struct snd_pcm_substream *substream)
4578 struct hda_gen_spec *spec = codec->spec;
4579 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4582 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4583 struct hda_codec *codec,
4584 unsigned int stream_tag,
4585 unsigned int format,
4586 struct snd_pcm_substream *substream)
4588 struct hda_gen_spec *spec = codec->spec;
4589 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4590 stream_tag, format, substream);
4593 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4594 struct hda_codec *codec,
4595 struct snd_pcm_substream *substream)
4597 struct hda_gen_spec *spec = codec->spec;
4598 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4601 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4602 struct hda_codec *codec,
4603 struct snd_pcm_substream *substream)
4605 struct hda_gen_spec *spec = codec->spec;
4606 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4612 #define alt_capture_pcm_open capture_pcm_open
4613 #define alt_capture_pcm_close capture_pcm_close
4615 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4616 struct hda_codec *codec,
4617 unsigned int stream_tag,
4618 unsigned int format,
4619 struct snd_pcm_substream *substream)
4621 struct hda_gen_spec *spec = codec->spec;
4623 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
4624 stream_tag, 0, format);
4625 call_pcm_capture_hook(hinfo, codec, substream,
4626 HDA_GEN_PCM_ACT_PREPARE);
4630 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4631 struct hda_codec *codec,
4632 struct snd_pcm_substream *substream)
4634 struct hda_gen_spec *spec = codec->spec;
4636 snd_hda_codec_cleanup_stream(codec,
4637 spec->adc_nids[substream->number + 1]);
4638 call_pcm_capture_hook(hinfo, codec, substream,
4639 HDA_GEN_PCM_ACT_CLEANUP);
4645 static const struct hda_pcm_stream pcm_analog_playback = {
4649 /* NID is set in build_pcms */
4651 .open = playback_pcm_open,
4652 .close = playback_pcm_close,
4653 .prepare = playback_pcm_prepare,
4654 .cleanup = playback_pcm_cleanup
4658 static const struct hda_pcm_stream pcm_analog_capture = {
4662 /* NID is set in build_pcms */
4664 .open = capture_pcm_open,
4665 .close = capture_pcm_close,
4666 .prepare = capture_pcm_prepare,
4667 .cleanup = capture_pcm_cleanup
4671 static const struct hda_pcm_stream pcm_analog_alt_playback = {
4675 /* NID is set in build_pcms */
4677 .open = alt_playback_pcm_open,
4678 .close = alt_playback_pcm_close,
4679 .prepare = alt_playback_pcm_prepare,
4680 .cleanup = alt_playback_pcm_cleanup
4684 static const struct hda_pcm_stream pcm_analog_alt_capture = {
4685 .substreams = 2, /* can be overridden */
4688 /* NID is set in build_pcms */
4690 .open = alt_capture_pcm_open,
4691 .close = alt_capture_pcm_close,
4692 .prepare = alt_capture_pcm_prepare,
4693 .cleanup = alt_capture_pcm_cleanup
4697 static const struct hda_pcm_stream pcm_digital_playback = {
4701 /* NID is set in build_pcms */
4703 .open = dig_playback_pcm_open,
4704 .close = dig_playback_pcm_close,
4705 .prepare = dig_playback_pcm_prepare,
4706 .cleanup = dig_playback_pcm_cleanup
4710 static const struct hda_pcm_stream pcm_digital_capture = {
4714 /* NID is set in build_pcms */
4717 /* Used by build_pcms to flag that a PCM has no playback stream */
4718 static const struct hda_pcm_stream pcm_null_stream = {
4725 * dynamic changing ADC PCM streams
4727 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4729 struct hda_gen_spec *spec = codec->spec;
4730 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4732 if (spec->cur_adc && spec->cur_adc != new_adc) {
4733 /* stream is running, let's swap the current ADC */
4734 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4735 spec->cur_adc = new_adc;
4736 snd_hda_codec_setup_stream(codec, new_adc,
4737 spec->cur_adc_stream_tag, 0,
4738 spec->cur_adc_format);
4744 /* analog capture with dynamic dual-adc changes */
4745 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4746 struct hda_codec *codec,
4747 unsigned int stream_tag,
4748 unsigned int format,
4749 struct snd_pcm_substream *substream)
4751 struct hda_gen_spec *spec = codec->spec;
4752 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4753 spec->cur_adc_stream_tag = stream_tag;
4754 spec->cur_adc_format = format;
4755 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4759 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4760 struct hda_codec *codec,
4761 struct snd_pcm_substream *substream)
4763 struct hda_gen_spec *spec = codec->spec;
4764 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4769 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4773 .nid = 0, /* fill later */
4775 .prepare = dyn_adc_capture_pcm_prepare,
4776 .cleanup = dyn_adc_capture_pcm_cleanup
4780 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4781 const char *chip_name)
4787 strlcpy(str, chip_name, len);
4789 /* drop non-alnum chars after a space */
4790 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4791 if (!isalnum(p[1])) {
4796 strlcat(str, sfx, len);
4799 /* build PCM streams based on the parsed results */
4800 int snd_hda_gen_build_pcms(struct hda_codec *codec)
4802 struct hda_gen_spec *spec = codec->spec;
4803 struct hda_pcm *info = spec->pcm_rec;
4804 const struct hda_pcm_stream *p;
4805 bool have_multi_adcs;
4807 codec->num_pcms = 1;
4808 codec->pcm_info = info;
4810 if (spec->no_analog)
4813 fill_pcm_stream_name(spec->stream_name_analog,
4814 sizeof(spec->stream_name_analog),
4815 " Analog", codec->chip_name);
4816 info->name = spec->stream_name_analog;
4818 if (spec->multiout.num_dacs > 0) {
4819 p = spec->stream_analog_playback;
4821 p = &pcm_analog_playback;
4822 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4823 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4824 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4825 spec->multiout.max_channels;
4826 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4827 spec->autocfg.line_outs == 2)
4828 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4831 if (spec->num_adc_nids) {
4832 p = spec->stream_analog_capture;
4834 if (spec->dyn_adc_switch)
4835 p = &dyn_adc_pcm_analog_capture;
4837 p = &pcm_analog_capture;
4839 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4840 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4844 /* SPDIF for stream index #1 */
4845 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
4846 fill_pcm_stream_name(spec->stream_name_digital,
4847 sizeof(spec->stream_name_digital),
4848 " Digital", codec->chip_name);
4849 codec->num_pcms = 2;
4850 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4851 info = spec->pcm_rec + 1;
4852 info->name = spec->stream_name_digital;
4853 if (spec->dig_out_type)
4854 info->pcm_type = spec->dig_out_type;
4856 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4857 if (spec->multiout.dig_out_nid) {
4858 p = spec->stream_digital_playback;
4860 p = &pcm_digital_playback;
4861 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4862 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4864 if (spec->dig_in_nid) {
4865 p = spec->stream_digital_capture;
4867 p = &pcm_digital_capture;
4868 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4869 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4873 if (spec->no_analog)
4876 /* If the use of more than one ADC is requested for the current
4877 * model, configure a second analog capture-only PCM.
4879 have_multi_adcs = (spec->num_adc_nids > 1) &&
4880 !spec->dyn_adc_switch && !spec->auto_mic;
4881 /* Additional Analaog capture for index #2 */
4882 if (spec->alt_dac_nid || have_multi_adcs) {
4883 fill_pcm_stream_name(spec->stream_name_alt_analog,
4884 sizeof(spec->stream_name_alt_analog),
4885 " Alt Analog", codec->chip_name);
4886 codec->num_pcms = 3;
4887 info = spec->pcm_rec + 2;
4888 info->name = spec->stream_name_alt_analog;
4889 if (spec->alt_dac_nid) {
4890 p = spec->stream_analog_alt_playback;
4892 p = &pcm_analog_alt_playback;
4893 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4894 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4897 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4899 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4901 if (have_multi_adcs) {
4902 p = spec->stream_analog_alt_capture;
4904 p = &pcm_analog_alt_capture;
4905 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4906 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4908 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4909 spec->num_adc_nids - 1;
4911 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4913 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4919 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4923 * Standard auto-parser initializations
4926 /* configure the given path as a proper output */
4927 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
4929 struct nid_path *path;
4932 path = snd_hda_get_path_from_idx(codec, path_idx);
4933 if (!path || !path->depth)
4935 pin = path->path[path->depth - 1];
4936 restore_pin_ctl(codec, pin);
4937 snd_hda_activate_path(codec, path, path->active,
4938 aamix_default(codec->spec));
4939 set_pin_eapd(codec, pin, path->active);
4942 /* initialize primary output paths */
4943 static void init_multi_out(struct hda_codec *codec)
4945 struct hda_gen_spec *spec = codec->spec;
4948 for (i = 0; i < spec->autocfg.line_outs; i++)
4949 set_output_and_unmute(codec, spec->out_paths[i]);
4953 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
4957 for (i = 0; i < num_outs; i++)
4958 set_output_and_unmute(codec, paths[i]);
4961 /* initialize hp and speaker paths */
4962 static void init_extra_out(struct hda_codec *codec)
4964 struct hda_gen_spec *spec = codec->spec;
4966 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
4967 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
4968 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4969 __init_extra_out(codec, spec->autocfg.speaker_outs,
4970 spec->speaker_paths);
4973 /* initialize multi-io paths */
4974 static void init_multi_io(struct hda_codec *codec)
4976 struct hda_gen_spec *spec = codec->spec;
4979 for (i = 0; i < spec->multi_ios; i++) {
4980 hda_nid_t pin = spec->multi_io[i].pin;
4981 struct nid_path *path;
4982 path = get_multiio_path(codec, i);
4985 if (!spec->multi_io[i].ctl_in)
4986 spec->multi_io[i].ctl_in =
4987 snd_hda_codec_get_pin_target(codec, pin);
4988 snd_hda_activate_path(codec, path, path->active,
4989 aamix_default(spec));
4993 /* set up input pins and loopback paths */
4994 static void init_analog_input(struct hda_codec *codec)
4996 struct hda_gen_spec *spec = codec->spec;
4997 struct auto_pin_cfg *cfg = &spec->autocfg;
5000 for (i = 0; i < cfg->num_inputs; i++) {
5001 hda_nid_t nid = cfg->inputs[i].pin;
5002 if (is_input_pin(codec, nid))
5003 restore_pin_ctl(codec, nid);
5005 /* init loopback inputs */
5006 if (spec->mixer_nid) {
5007 resume_path_from_idx(codec, spec->loopback_paths[i]);
5008 resume_path_from_idx(codec, spec->loopback_merge_path);
5013 /* initialize ADC paths */
5014 static void init_input_src(struct hda_codec *codec)
5016 struct hda_gen_spec *spec = codec->spec;
5017 struct hda_input_mux *imux = &spec->input_mux;
5018 struct nid_path *path;
5021 if (spec->dyn_adc_switch)
5024 nums = spec->num_adc_nids;
5026 for (c = 0; c < nums; c++) {
5027 for (i = 0; i < imux->num_items; i++) {
5028 path = get_input_path(codec, c, i);
5030 bool active = path->active;
5031 if (i == spec->cur_mux[c])
5033 snd_hda_activate_path(codec, path, active, false);
5037 update_hp_mic(codec, c, true);
5040 if (spec->cap_sync_hook)
5041 spec->cap_sync_hook(codec, NULL);
5044 /* set right pin controls for digital I/O */
5045 static void init_digital(struct hda_codec *codec)
5047 struct hda_gen_spec *spec = codec->spec;
5051 for (i = 0; i < spec->autocfg.dig_outs; i++)
5052 set_output_and_unmute(codec, spec->digout_paths[i]);
5053 pin = spec->autocfg.dig_in_pin;
5055 restore_pin_ctl(codec, pin);
5056 resume_path_from_idx(codec, spec->digin_path);
5060 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5061 * invalid unsol tags by some reason
5063 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5067 for (i = 0; i < codec->init_pins.used; i++) {
5068 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5069 hda_nid_t nid = pin->nid;
5070 if (is_jack_detectable(codec, nid) &&
5071 !snd_hda_jack_tbl_get(codec, nid))
5072 snd_hda_codec_update_cache(codec, nid, 0,
5073 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5078 * initialize the generic spec;
5079 * this can be put as patch_ops.init function
5081 int snd_hda_gen_init(struct hda_codec *codec)
5083 struct hda_gen_spec *spec = codec->spec;
5085 if (spec->init_hook)
5086 spec->init_hook(codec);
5088 snd_hda_apply_verbs(codec);
5090 codec->cached_write = 1;
5092 init_multi_out(codec);
5093 init_extra_out(codec);
5094 init_multi_io(codec);
5095 init_analog_input(codec);
5096 init_input_src(codec);
5097 init_digital(codec);
5099 clear_unsol_on_unused_pins(codec);
5101 /* call init functions of standard auto-mute helpers */
5102 update_automute_all(codec);
5104 snd_hda_codec_flush_cache(codec);
5106 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5107 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5109 hda_call_check_power_status(codec, 0x01);
5112 EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5115 * free the generic spec;
5116 * this can be put as patch_ops.free function
5118 void snd_hda_gen_free(struct hda_codec *codec)
5120 snd_hda_detach_beep_device(codec);
5121 snd_hda_gen_spec_free(codec->spec);
5125 EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5129 * check the loopback power save state;
5130 * this can be put as patch_ops.check_power_status function
5132 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5134 struct hda_gen_spec *spec = codec->spec;
5135 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5137 EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5142 * the generic codec support
5145 static const struct hda_codec_ops generic_patch_ops = {
5146 .build_controls = snd_hda_gen_build_controls,
5147 .build_pcms = snd_hda_gen_build_pcms,
5148 .init = snd_hda_gen_init,
5149 .free = snd_hda_gen_free,
5150 .unsol_event = snd_hda_jack_unsol_event,
5152 .check_power_status = snd_hda_gen_check_power_status,
5156 int snd_hda_parse_generic_codec(struct hda_codec *codec)
5158 struct hda_gen_spec *spec;
5161 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5164 snd_hda_gen_spec_init(spec);
5167 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5171 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5175 codec->patch_ops = generic_patch_ops;
5179 snd_hda_gen_free(codec);
5182 EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);