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/ctype.h>
28 #include <linux/string.h>
29 #include <sound/core.h>
30 #include <sound/jack.h>
31 #include "hda_codec.h"
32 #include "hda_local.h"
33 #include "hda_auto_parser.h"
35 #include "hda_generic.h"
38 /* initialize hda_gen_spec struct */
39 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
41 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
42 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
43 mutex_init(&spec->pcm_mutex);
46 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
48 struct snd_kcontrol_new *
49 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
50 const struct snd_kcontrol_new *temp)
52 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
57 knew->name = kstrdup(name, GFP_KERNEL);
59 knew->name = kstrdup(knew->name, GFP_KERNEL);
64 EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
66 static void free_kctls(struct hda_gen_spec *spec)
68 if (spec->kctls.list) {
69 struct snd_kcontrol_new *kctl = spec->kctls.list;
71 for (i = 0; i < spec->kctls.used; i++)
74 snd_array_free(&spec->kctls);
77 void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
82 snd_array_free(&spec->paths);
84 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
89 static void parse_user_hints(struct hda_codec *codec)
91 struct hda_gen_spec *spec = codec->spec;
94 val = snd_hda_get_bool_hint(codec, "jack_detect");
96 codec->no_jack_detect = !val;
97 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
99 codec->inv_jack_detect = !!val;
100 val = snd_hda_get_bool_hint(codec, "trigger_sense");
102 codec->no_trigger_sense = !val;
103 val = snd_hda_get_bool_hint(codec, "inv_eapd");
105 codec->inv_eapd = !!val;
106 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
108 codec->pcm_format_first = !!val;
109 val = snd_hda_get_bool_hint(codec, "sticky_stream");
111 codec->no_sticky_stream = !val;
112 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
114 codec->spdif_status_reset = !!val;
115 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
117 codec->pin_amp_workaround = !!val;
118 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
120 codec->single_adc_amp = !!val;
122 val = snd_hda_get_bool_hint(codec, "auto_mic");
124 spec->suppress_auto_mic = !val;
125 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
127 spec->line_in_auto_switch = !!val;
128 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
130 spec->need_dac_fix = !!val;
131 val = snd_hda_get_bool_hint(codec, "primary_hp");
133 spec->no_primary_hp = !val;
134 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
136 spec->multi_cap_vol = !!val;
137 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
139 spec->inv_dmic_split = !!val;
140 val = snd_hda_get_bool_hint(codec, "indep_hp");
142 spec->indep_hp = !!val;
143 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
145 spec->add_stereo_mix_input = !!val;
146 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
148 spec->add_out_jack_modes = !!val;
150 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
151 spec->mixer_nid = val;
155 * pin control value accesses
158 #define update_pin_ctl(codec, pin, val) \
159 snd_hda_codec_update_cache(codec, pin, 0, \
160 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
162 /* restore the pinctl based on the cached value */
163 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
165 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
168 /* set the pinctl target value and write it if requested */
169 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
170 unsigned int val, bool do_write)
174 val = snd_hda_correct_pin_ctl(codec, pin, val);
175 snd_hda_codec_set_pin_target(codec, pin, val);
177 update_pin_ctl(codec, pin, val);
180 /* set pinctl target values for all given pins */
181 static void set_pin_targets(struct hda_codec *codec, int num_pins,
182 hda_nid_t *pins, unsigned int val)
185 for (i = 0; i < num_pins; i++)
186 set_pin_target(codec, pins[i], val, false);
193 /* return the position of NID in the list, or -1 if not found */
194 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
197 for (i = 0; i < nums; i++)
203 /* return true if the given NID is contained in the path */
204 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
206 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
209 static struct nid_path *get_nid_path(struct hda_codec *codec,
210 hda_nid_t from_nid, hda_nid_t to_nid,
213 struct hda_gen_spec *spec = codec->spec;
216 for (i = 0; i < spec->paths.used; i++) {
217 struct nid_path *path = snd_array_elem(&spec->paths, i);
218 if (path->depth <= 0)
220 if ((!from_nid || path->path[0] == from_nid) &&
221 (!to_nid || path->path[path->depth - 1] == to_nid)) {
223 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
224 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
231 /* get the path between the given NIDs;
232 * passing 0 to either @pin or @dac behaves as a wildcard
234 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
235 hda_nid_t from_nid, hda_nid_t to_nid)
237 return get_nid_path(codec, from_nid, to_nid, 0);
239 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
241 /* get the index number corresponding to the path instance;
242 * the index starts from 1, for easier checking the invalid value
244 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
246 struct hda_gen_spec *spec = codec->spec;
247 struct nid_path *array = spec->paths.list;
250 if (!spec->paths.used)
253 if (idx < 0 || idx >= spec->paths.used)
258 /* get the path instance corresponding to the given index number */
259 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
261 struct hda_gen_spec *spec = codec->spec;
263 if (idx <= 0 || idx > spec->paths.used)
265 return snd_array_elem(&spec->paths, idx - 1);
268 /* check whether the given DAC is already found in any existing paths */
269 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
271 struct hda_gen_spec *spec = codec->spec;
274 for (i = 0; i < spec->paths.used; i++) {
275 struct nid_path *path = snd_array_elem(&spec->paths, i);
276 if (path->path[0] == nid)
282 /* check whether the given two widgets can be connected */
283 static bool is_reachable_path(struct hda_codec *codec,
284 hda_nid_t from_nid, hda_nid_t to_nid)
286 if (!from_nid || !to_nid)
288 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
291 /* nid, dir and idx */
292 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
294 /* check whether the given ctl is already assigned in any path elements */
295 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
297 struct hda_gen_spec *spec = codec->spec;
300 val &= AMP_VAL_COMPARE_MASK;
301 for (i = 0; i < spec->paths.used; i++) {
302 struct nid_path *path = snd_array_elem(&spec->paths, i);
303 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
309 /* check whether a control with the given (nid, dir, idx) was assigned */
310 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
313 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
314 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
315 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
318 static void print_nid_path(const char *pfx, struct nid_path *path)
325 for (i = 0; i < path->depth; i++) {
327 sprintf(tmp, ":%02x", path->path[i]);
328 strlcat(buf, tmp, sizeof(buf));
330 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
333 /* called recursively */
334 static bool __parse_nid_path(struct hda_codec *codec,
335 hda_nid_t from_nid, hda_nid_t to_nid,
336 int anchor_nid, struct nid_path *path,
339 const hda_nid_t *conn;
342 if (to_nid == anchor_nid)
343 anchor_nid = 0; /* anchor passed */
344 else if (to_nid == (hda_nid_t)(-anchor_nid))
345 return false; /* hit the exclusive nid */
347 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
348 for (i = 0; i < nums; i++) {
349 if (conn[i] != from_nid) {
350 /* special case: when from_nid is 0,
351 * try to find an empty DAC
354 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
355 is_dac_already_used(codec, conn[i]))
358 /* anchor is not requested or already passed? */
362 if (depth >= MAX_NID_PATH_DEPTH)
364 for (i = 0; i < nums; i++) {
366 type = get_wcaps_type(get_wcaps(codec, conn[i]));
367 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
370 if (__parse_nid_path(codec, from_nid, conn[i],
371 anchor_nid, path, depth + 1))
377 path->path[path->depth] = conn[i];
378 path->idx[path->depth + 1] = i;
379 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
380 path->multi[path->depth + 1] = 1;
385 /* parse the widget path from the given nid to the target nid;
386 * when @from_nid is 0, try to find an empty DAC;
387 * when @anchor_nid is set to a positive value, only paths through the widget
388 * with the given value are evaluated.
389 * when @anchor_nid is set to a negative value, paths through the widget
390 * with the negative of given value are excluded, only other paths are chosen.
391 * when @anchor_nid is zero, no special handling about path selection.
393 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
394 hda_nid_t to_nid, int anchor_nid,
395 struct nid_path *path)
397 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
398 path->path[path->depth] = to_nid;
404 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
407 * parse the path between the given NIDs and add to the path list.
408 * if no valid path is found, return NULL
411 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
412 hda_nid_t to_nid, int anchor_nid)
414 struct hda_gen_spec *spec = codec->spec;
415 struct nid_path *path;
417 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
420 /* check whether the path has been already added */
421 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
425 path = snd_array_new(&spec->paths);
428 memset(path, 0, sizeof(*path));
429 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
435 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
437 /* clear the given path as invalid so that it won't be picked up later */
438 static void invalidate_nid_path(struct hda_codec *codec, int idx)
440 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
443 memset(path, 0, sizeof(*path));
446 /* look for an empty DAC slot */
447 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
450 struct hda_gen_spec *spec = codec->spec;
454 for (i = 0; i < spec->num_all_dacs; i++) {
455 hda_nid_t nid = spec->all_dacs[i];
456 if (!nid || is_dac_already_used(codec, nid))
458 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
459 if (is_digital != cap_digital)
461 if (is_reachable_path(codec, nid, pin))
467 /* replace the channels in the composed amp value with the given number */
468 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
470 val &= ~(0x3U << 16);
475 /* check whether the widget has the given amp capability for the direction */
476 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
477 int dir, unsigned int bits)
481 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
482 if (query_amp_caps(codec, nid, dir) & bits)
487 #define nid_has_mute(codec, nid, dir) \
488 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
489 #define nid_has_volume(codec, nid, dir) \
490 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
492 /* look for a widget suitable for assigning a mute switch in the path */
493 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
494 struct nid_path *path)
498 for (i = path->depth - 1; i >= 0; i--) {
499 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
500 return path->path[i];
501 if (i != path->depth - 1 && i != 0 &&
502 nid_has_mute(codec, path->path[i], HDA_INPUT))
503 return path->path[i];
508 /* look for a widget suitable for assigning a volume ctl in the path */
509 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
510 struct nid_path *path)
514 for (i = path->depth - 1; i >= 0; i--) {
515 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
516 return path->path[i];
522 * path activation / deactivation
525 /* can have the amp-in capability? */
526 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
528 hda_nid_t nid = path->path[idx];
529 unsigned int caps = get_wcaps(codec, nid);
530 unsigned int type = get_wcaps_type(caps);
532 if (!(caps & AC_WCAP_IN_AMP))
534 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
539 /* can have the amp-out capability? */
540 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
542 hda_nid_t nid = path->path[idx];
543 unsigned int caps = get_wcaps(codec, nid);
544 unsigned int type = get_wcaps_type(caps);
546 if (!(caps & AC_WCAP_OUT_AMP))
548 if (type == AC_WID_PIN && !idx) /* only for output pins */
553 /* check whether the given (nid,dir,idx) is active */
554 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
555 unsigned int idx, unsigned int dir)
557 struct hda_gen_spec *spec = codec->spec;
560 for (n = 0; n < spec->paths.used; n++) {
561 struct nid_path *path = snd_array_elem(&spec->paths, n);
564 for (i = 0; i < path->depth; i++) {
565 if (path->path[i] == nid) {
566 if (dir == HDA_OUTPUT || path->idx[i] == idx)
575 /* get the default amp value for the target state */
576 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
577 int dir, bool enable)
580 unsigned int val = 0;
582 caps = query_amp_caps(codec, nid, dir);
583 if (caps & AC_AMPCAP_NUM_STEPS) {
586 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
588 if (caps & AC_AMPCAP_MUTE) {
595 /* initialize the amp value (only at the first time) */
596 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
598 int val = get_amp_val_to_activate(codec, nid, dir, false);
599 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
602 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
603 int idx, bool enable)
606 if (is_ctl_associated(codec, nid, dir, idx) ||
607 (!enable && is_active_nid(codec, nid, dir, idx)))
609 val = get_amp_val_to_activate(codec, nid, dir, enable);
610 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
613 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
616 hda_nid_t nid = path->path[i];
617 init_amp(codec, nid, HDA_OUTPUT, 0);
618 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
621 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
622 int i, bool enable, bool add_aamix)
624 struct hda_gen_spec *spec = codec->spec;
625 const hda_nid_t *conn;
628 hda_nid_t nid = path->path[i];
630 nums = snd_hda_get_conn_list(codec, nid, &conn);
631 type = get_wcaps_type(get_wcaps(codec, nid));
632 if (type == AC_WID_PIN ||
633 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
639 for (n = 0; n < nums; n++)
640 init_amp(codec, nid, HDA_INPUT, n);
642 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
645 /* here is a little bit tricky in comparison with activate_amp_out();
646 * when aa-mixer is available, we need to enable the path as well
648 for (n = 0; n < nums; n++) {
649 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
651 activate_amp(codec, nid, HDA_INPUT, n, enable);
655 /* activate or deactivate the given path
656 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
658 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
659 bool enable, bool add_aamix)
664 path->active = false;
666 for (i = path->depth - 1; i >= 0; i--) {
667 if (enable && path->multi[i])
668 snd_hda_codec_write_cache(codec, path->path[i], 0,
669 AC_VERB_SET_CONNECT_SEL,
671 if (has_amp_in(codec, path, i))
672 activate_amp_in(codec, path, i, enable, add_aamix);
673 if (has_amp_out(codec, path, i))
674 activate_amp_out(codec, path, i, enable);
680 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
682 /* turn on/off EAPD on the given pin */
683 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
685 struct hda_gen_spec *spec = codec->spec;
686 if (spec->own_eapd_ctl ||
687 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
691 snd_hda_codec_update_cache(codec, pin, 0,
692 AC_VERB_SET_EAPD_BTLENABLE,
693 enable ? 0x02 : 0x00);
698 * Helper functions for creating mixer ctl elements
706 static const struct snd_kcontrol_new control_templates[] = {
707 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
708 HDA_CODEC_MUTE(NULL, 0, 0, 0),
709 HDA_BIND_MUTE(NULL, 0, 0, 0),
712 /* add dynamic controls from template */
713 static int add_control(struct hda_gen_spec *spec, int type, const char *name,
714 int cidx, unsigned long val)
716 struct snd_kcontrol_new *knew;
718 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
722 if (get_amp_nid_(val))
723 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
724 knew->private_value = val;
728 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
729 const char *pfx, const char *dir,
730 const char *sfx, int cidx, unsigned long val)
733 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
734 return add_control(spec, type, name, cidx, val);
737 #define add_pb_vol_ctrl(spec, type, pfx, val) \
738 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
739 #define add_pb_sw_ctrl(spec, type, pfx, val) \
740 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
741 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
742 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
743 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
744 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
746 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
747 unsigned int chs, struct nid_path *path)
752 val = path->ctls[NID_PATH_VOL_CTL];
755 val = amp_val_replace_channels(val, chs);
756 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
759 /* return the channel bits suitable for the given path->ctls[] */
760 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
763 int chs = 1; /* mono (left only) */
765 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
766 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
767 chs = 3; /* stereo */
772 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
773 struct nid_path *path)
775 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
776 return add_vol_ctl(codec, pfx, cidx, chs, path);
779 /* create a mute-switch for the given mixer widget;
780 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
782 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
783 unsigned int chs, struct nid_path *path)
786 int type = HDA_CTL_WIDGET_MUTE;
790 val = path->ctls[NID_PATH_MUTE_CTL];
793 val = amp_val_replace_channels(val, chs);
794 if (get_amp_direction_(val) == HDA_INPUT) {
795 hda_nid_t nid = get_amp_nid_(val);
796 int nums = snd_hda_get_num_conns(codec, nid);
798 type = HDA_CTL_BIND_MUTE;
802 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
805 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
806 int cidx, struct nid_path *path)
808 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
809 return add_sw_ctl(codec, pfx, cidx, chs, path);
812 static const char * const channel_name[4] = {
813 "Front", "Surround", "CLFE", "Side"
816 /* give some appropriate ctl name prefix for the given line out channel */
817 static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
818 bool can_be_master, int *index)
820 struct auto_pin_cfg *cfg = &spec->autocfg;
823 if (cfg->line_outs == 1 && !spec->multi_ios &&
824 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
825 return spec->vmaster_mute.hook ? "PCM" : "Master";
827 /* if there is really a single DAC used in the whole output paths,
828 * use it master (or "PCM" if a vmaster hook is present)
830 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
831 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
832 return spec->vmaster_mute.hook ? "PCM" : "Master";
834 switch (cfg->line_out_type) {
835 case AUTO_PIN_SPEAKER_OUT:
836 if (cfg->line_outs == 1)
838 if (cfg->line_outs == 2)
839 return ch ? "Bass Speaker" : "Speaker";
841 case AUTO_PIN_HP_OUT:
842 /* for multi-io case, only the primary out */
843 if (ch && spec->multi_ios)
848 if (cfg->line_outs == 1 && !spec->multi_ios)
852 if (ch >= ARRAY_SIZE(channel_name)) {
857 return channel_name[ch];
864 /* badness definition */
866 /* No primary DAC is found for the main output */
867 BAD_NO_PRIMARY_DAC = 0x10000,
868 /* No DAC is found for the extra output */
870 /* No possible multi-ios */
871 BAD_MULTI_IO = 0x103,
872 /* No individual DAC for extra output */
873 BAD_NO_EXTRA_DAC = 0x102,
874 /* No individual DAC for extra surrounds */
875 BAD_NO_EXTRA_SURR_DAC = 0x101,
876 /* Primary DAC shared with main surrounds */
877 BAD_SHARED_SURROUND = 0x100,
878 /* Primary DAC shared with main CLFE */
879 BAD_SHARED_CLFE = 0x10,
880 /* Primary DAC shared with extra surrounds */
881 BAD_SHARED_EXTRA_SURROUND = 0x10,
882 /* Volume widget is shared */
883 BAD_SHARED_VOL = 0x10,
886 /* look for widgets in the given path which are appropriate for
887 * volume and mute controls, and assign the values to ctls[].
889 * When no appropriate widget is found in the path, the badness value
890 * is incremented depending on the situation. The function returns the
891 * total badness for both volume and mute controls.
893 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
900 return BAD_SHARED_VOL * 2;
902 if (path->ctls[NID_PATH_VOL_CTL] ||
903 path->ctls[NID_PATH_MUTE_CTL])
904 return 0; /* already evaluated */
906 nid = look_for_out_vol_nid(codec, path);
908 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
909 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
910 badness += BAD_SHARED_VOL;
912 path->ctls[NID_PATH_VOL_CTL] = val;
914 badness += BAD_SHARED_VOL;
915 nid = look_for_out_mute_nid(codec, path);
917 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
918 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
919 nid_has_mute(codec, nid, HDA_OUTPUT))
920 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
922 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
923 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
924 badness += BAD_SHARED_VOL;
926 path->ctls[NID_PATH_MUTE_CTL] = val;
928 badness += BAD_SHARED_VOL;
932 struct badness_table {
933 int no_primary_dac; /* no primary DAC */
934 int no_dac; /* no secondary DACs */
935 int shared_primary; /* primary DAC is shared with main output */
936 int shared_surr; /* secondary DAC shared with main or primary */
937 int shared_clfe; /* third DAC shared with main or primary */
938 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
941 static struct badness_table main_out_badness = {
942 .no_primary_dac = BAD_NO_PRIMARY_DAC,
943 .no_dac = BAD_NO_DAC,
944 .shared_primary = BAD_NO_PRIMARY_DAC,
945 .shared_surr = BAD_SHARED_SURROUND,
946 .shared_clfe = BAD_SHARED_CLFE,
947 .shared_surr_main = BAD_SHARED_SURROUND,
950 static struct badness_table extra_out_badness = {
951 .no_primary_dac = BAD_NO_DAC,
952 .no_dac = BAD_NO_DAC,
953 .shared_primary = BAD_NO_EXTRA_DAC,
954 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
955 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
956 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
959 /* get the DAC of the primary output corresponding to the given array index */
960 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
962 struct hda_gen_spec *spec = codec->spec;
963 struct auto_pin_cfg *cfg = &spec->autocfg;
965 if (cfg->line_outs > idx)
966 return spec->private_dac_nids[idx];
967 idx -= cfg->line_outs;
968 if (spec->multi_ios > idx)
969 return spec->multi_io[idx].dac;
973 /* return the DAC if it's reachable, otherwise zero */
974 static inline hda_nid_t try_dac(struct hda_codec *codec,
975 hda_nid_t dac, hda_nid_t pin)
977 return is_reachable_path(codec, dac, pin) ? dac : 0;
980 /* try to assign DACs to pins and return the resultant badness */
981 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
982 const hda_nid_t *pins, hda_nid_t *dacs,
984 const struct badness_table *bad)
986 struct hda_gen_spec *spec = codec->spec;
994 for (i = 0; i < num_outs; i++) {
995 struct nid_path *path;
996 hda_nid_t pin = pins[i];
998 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1000 badness += assign_out_path_ctls(codec, path);
1004 dacs[i] = look_for_dac(codec, pin, false);
1005 if (!dacs[i] && !i) {
1006 /* try to steal the DAC of surrounds for the front */
1007 for (j = 1; j < num_outs; j++) {
1008 if (is_reachable_path(codec, dacs[j], pin)) {
1011 invalidate_nid_path(codec, path_idx[j]);
1020 dac = try_dac(codec, get_primary_out(codec, i), pin);
1022 dac = try_dac(codec, dacs[0], pin);
1024 dac = try_dac(codec, get_primary_out(codec, i), pin);
1027 badness += bad->shared_primary;
1029 badness += bad->shared_surr;
1031 badness += bad->shared_clfe;
1032 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1033 dac = spec->private_dac_nids[0];
1034 badness += bad->shared_surr_main;
1036 badness += bad->no_primary_dac;
1038 badness += bad->no_dac;
1040 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1041 if (!path && !i && spec->mixer_nid) {
1042 /* try with aamix */
1043 path = snd_hda_add_new_path(codec, dac, pin, 0);
1048 print_nid_path("output", path);
1049 path->active = true;
1050 path_idx[i] = snd_hda_get_path_idx(codec, path);
1051 badness += assign_out_path_ctls(codec, path);
1058 /* return NID if the given pin has only a single connection to a certain DAC */
1059 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1061 struct hda_gen_spec *spec = codec->spec;
1063 hda_nid_t nid_found = 0;
1065 for (i = 0; i < spec->num_all_dacs; i++) {
1066 hda_nid_t nid = spec->all_dacs[i];
1067 if (!nid || is_dac_already_used(codec, nid))
1069 if (is_reachable_path(codec, nid, pin)) {
1078 /* check whether the given pin can be a multi-io pin */
1079 static bool can_be_multiio_pin(struct hda_codec *codec,
1080 unsigned int location, hda_nid_t nid)
1082 unsigned int defcfg, caps;
1084 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1085 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1087 if (location && get_defcfg_location(defcfg) != location)
1089 caps = snd_hda_query_pin_caps(codec, nid);
1090 if (!(caps & AC_PINCAP_OUT))
1095 /* count the number of input pins that are capable to be multi-io */
1096 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1098 struct hda_gen_spec *spec = codec->spec;
1099 struct auto_pin_cfg *cfg = &spec->autocfg;
1100 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1101 unsigned int location = get_defcfg_location(defcfg);
1105 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1106 for (i = 0; i < cfg->num_inputs; i++) {
1107 if (cfg->inputs[i].type != type)
1109 if (can_be_multiio_pin(codec, location,
1110 cfg->inputs[i].pin))
1120 * When hardwired is set, try to fill ony hardwired pins, and returns
1121 * zero if any pins are filled, non-zero if nothing found.
1122 * When hardwired is off, try to fill possible input pins, and returns
1123 * the badness value.
1125 static int fill_multi_ios(struct hda_codec *codec,
1126 hda_nid_t reference_pin,
1129 struct hda_gen_spec *spec = codec->spec;
1130 struct auto_pin_cfg *cfg = &spec->autocfg;
1131 int type, i, j, num_pins, old_pins;
1132 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1133 unsigned int location = get_defcfg_location(defcfg);
1135 struct nid_path *path;
1137 old_pins = spec->multi_ios;
1141 num_pins = count_multiio_pins(codec, reference_pin);
1145 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1146 for (i = 0; i < cfg->num_inputs; i++) {
1147 hda_nid_t nid = cfg->inputs[i].pin;
1150 if (cfg->inputs[i].type != type)
1152 if (!can_be_multiio_pin(codec, location, nid))
1154 for (j = 0; j < spec->multi_ios; j++) {
1155 if (nid == spec->multi_io[j].pin)
1158 if (j < spec->multi_ios)
1162 dac = get_dac_if_single(codec, nid);
1164 dac = look_for_dac(codec, nid, false);
1169 path = snd_hda_add_new_path(codec, dac, nid,
1175 print_nid_path("multiio", path);
1176 spec->multi_io[spec->multi_ios].pin = nid;
1177 spec->multi_io[spec->multi_ios].dac = dac;
1178 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1179 snd_hda_get_path_idx(codec, path);
1181 if (spec->multi_ios >= 2)
1187 badness = BAD_MULTI_IO;
1188 if (old_pins == spec->multi_ios) {
1190 return 1; /* nothing found */
1192 return badness; /* no badness if nothing found */
1194 if (!hardwired && spec->multi_ios < 2) {
1195 /* cancel newly assigned paths */
1196 spec->paths.used -= spec->multi_ios - old_pins;
1197 spec->multi_ios = old_pins;
1201 /* assign volume and mute controls */
1202 for (i = old_pins; i < spec->multi_ios; i++) {
1203 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1204 badness += assign_out_path_ctls(codec, path);
1210 /* map DACs for all pins in the list if they are single connections */
1211 static bool map_singles(struct hda_codec *codec, int outs,
1212 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1214 struct hda_gen_spec *spec = codec->spec;
1217 for (i = 0; i < outs; i++) {
1218 struct nid_path *path;
1222 dac = get_dac_if_single(codec, pins[i]);
1225 path = snd_hda_add_new_path(codec, dac, pins[i],
1227 if (!path && !i && spec->mixer_nid)
1228 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1232 print_nid_path("output", path);
1233 path->active = true;
1234 path_idx[i] = snd_hda_get_path_idx(codec, path);
1240 /* create a new path including aamix if available, and return its index */
1241 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1243 struct hda_gen_spec *spec = codec->spec;
1244 struct nid_path *path;
1246 path = snd_hda_get_path_from_idx(codec, path_idx);
1247 if (!path || !path->depth ||
1248 is_nid_contained(path, spec->mixer_nid))
1250 path = snd_hda_add_new_path(codec, path->path[0],
1251 path->path[path->depth - 1],
1255 print_nid_path("output-aamix", path);
1256 path->active = false; /* unused as default */
1257 return snd_hda_get_path_idx(codec, path);
1260 /* fill the empty entries in the dac array for speaker/hp with the
1261 * shared dac pointed by the paths
1263 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1264 hda_nid_t *dacs, int *path_idx)
1266 struct nid_path *path;
1269 for (i = 0; i < num_outs; i++) {
1272 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1275 dacs[i] = path->path[0];
1279 /* fill in the dac_nids table from the parsed pin configuration */
1280 static int fill_and_eval_dacs(struct hda_codec *codec,
1281 bool fill_hardwired,
1282 bool fill_mio_first)
1284 struct hda_gen_spec *spec = codec->spec;
1285 struct auto_pin_cfg *cfg = &spec->autocfg;
1286 int i, err, badness;
1289 /* set num_dacs once to full for look_for_dac() */
1290 spec->multiout.num_dacs = cfg->line_outs;
1291 spec->multiout.dac_nids = spec->private_dac_nids;
1292 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1293 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1294 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1295 spec->multi_ios = 0;
1296 snd_array_free(&spec->paths);
1298 /* clear path indices */
1299 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1300 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1301 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1302 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1303 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1304 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1305 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1306 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1310 /* fill hard-wired DACs first */
1311 if (fill_hardwired) {
1314 mapped = map_singles(codec, cfg->line_outs,
1316 spec->private_dac_nids,
1318 mapped |= map_singles(codec, cfg->hp_outs,
1320 spec->multiout.hp_out_nid,
1322 mapped |= map_singles(codec, cfg->speaker_outs,
1324 spec->multiout.extra_out_nid,
1325 spec->speaker_paths);
1326 if (fill_mio_first && cfg->line_outs == 1 &&
1327 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1328 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1335 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1336 spec->private_dac_nids, spec->out_paths,
1339 if (fill_mio_first &&
1340 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1341 /* try to fill multi-io first */
1342 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1345 /* we don't count badness at this stage yet */
1348 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1349 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1350 spec->multiout.hp_out_nid,
1352 &extra_out_badness);
1357 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1358 err = try_assign_dacs(codec, cfg->speaker_outs,
1360 spec->multiout.extra_out_nid,
1361 spec->speaker_paths,
1362 &extra_out_badness);
1367 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1368 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1374 if (spec->mixer_nid) {
1375 spec->aamix_out_paths[0] =
1376 check_aamix_out_path(codec, spec->out_paths[0]);
1377 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1378 spec->aamix_out_paths[1] =
1379 check_aamix_out_path(codec, spec->hp_paths[0]);
1380 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1381 spec->aamix_out_paths[2] =
1382 check_aamix_out_path(codec, spec->speaker_paths[0]);
1385 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1386 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1387 spec->multi_ios = 1; /* give badness */
1389 /* re-count num_dacs and squash invalid entries */
1390 spec->multiout.num_dacs = 0;
1391 for (i = 0; i < cfg->line_outs; i++) {
1392 if (spec->private_dac_nids[i])
1393 spec->multiout.num_dacs++;
1395 memmove(spec->private_dac_nids + i,
1396 spec->private_dac_nids + i + 1,
1397 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1398 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1402 spec->ext_channel_count = spec->min_channel_count =
1403 spec->multiout.num_dacs;
1405 if (spec->multi_ios == 2) {
1406 for (i = 0; i < 2; i++)
1407 spec->private_dac_nids[spec->multiout.num_dacs++] =
1408 spec->multi_io[i].dac;
1409 } else if (spec->multi_ios) {
1410 spec->multi_ios = 0;
1411 badness += BAD_MULTI_IO;
1414 /* re-fill the shared DAC for speaker / headphone */
1415 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1416 refill_shared_dacs(codec, cfg->hp_outs,
1417 spec->multiout.hp_out_nid,
1419 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1420 refill_shared_dacs(codec, cfg->speaker_outs,
1421 spec->multiout.extra_out_nid,
1422 spec->speaker_paths);
1424 /* set initial pinctl targets */
1425 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1429 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1430 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1431 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1432 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1433 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1434 set_pin_targets(codec, cfg->speaker_outs,
1435 cfg->speaker_pins, val);
1441 #define DEBUG_BADNESS
1443 #ifdef DEBUG_BADNESS
1444 #define debug_badness snd_printdd
1446 #define debug_badness(...)
1449 static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1451 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1452 cfg->line_out_pins[0], cfg->line_out_pins[1],
1453 cfg->line_out_pins[2], cfg->line_out_pins[3],
1454 spec->multiout.dac_nids[0],
1455 spec->multiout.dac_nids[1],
1456 spec->multiout.dac_nids[2],
1457 spec->multiout.dac_nids[3]);
1458 if (spec->multi_ios > 0)
1459 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1461 spec->multi_io[0].pin, spec->multi_io[1].pin,
1462 spec->multi_io[0].dac, spec->multi_io[1].dac);
1463 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1464 cfg->hp_pins[0], cfg->hp_pins[1],
1465 cfg->hp_pins[2], cfg->hp_pins[3],
1466 spec->multiout.hp_out_nid[0],
1467 spec->multiout.hp_out_nid[1],
1468 spec->multiout.hp_out_nid[2],
1469 spec->multiout.hp_out_nid[3]);
1470 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1471 cfg->speaker_pins[0], cfg->speaker_pins[1],
1472 cfg->speaker_pins[2], cfg->speaker_pins[3],
1473 spec->multiout.extra_out_nid[0],
1474 spec->multiout.extra_out_nid[1],
1475 spec->multiout.extra_out_nid[2],
1476 spec->multiout.extra_out_nid[3]);
1479 /* find all available DACs of the codec */
1480 static void fill_all_dac_nids(struct hda_codec *codec)
1482 struct hda_gen_spec *spec = codec->spec;
1484 hda_nid_t nid = codec->start_nid;
1486 spec->num_all_dacs = 0;
1487 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1488 for (i = 0; i < codec->num_nodes; i++, nid++) {
1489 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1491 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1492 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1495 spec->all_dacs[spec->num_all_dacs++] = nid;
1499 static int parse_output_paths(struct hda_codec *codec)
1501 struct hda_gen_spec *spec = codec->spec;
1502 struct auto_pin_cfg *cfg = &spec->autocfg;
1503 struct auto_pin_cfg *best_cfg;
1504 int best_badness = INT_MAX;
1506 bool fill_hardwired = true, fill_mio_first = true;
1507 bool best_wired = true, best_mio = true;
1508 bool hp_spk_swapped = false;
1510 fill_all_dac_nids(codec);
1512 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1518 badness = fill_and_eval_dacs(codec, fill_hardwired,
1524 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1525 cfg->line_out_type, fill_hardwired, fill_mio_first,
1527 debug_show_configs(spec, cfg);
1528 if (badness < best_badness) {
1529 best_badness = badness;
1531 best_wired = fill_hardwired;
1532 best_mio = fill_mio_first;
1536 fill_mio_first = !fill_mio_first;
1537 if (!fill_mio_first)
1539 fill_hardwired = !fill_hardwired;
1540 if (!fill_hardwired)
1544 hp_spk_swapped = true;
1545 if (cfg->speaker_outs > 0 &&
1546 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1547 cfg->hp_outs = cfg->line_outs;
1548 memcpy(cfg->hp_pins, cfg->line_out_pins,
1549 sizeof(cfg->hp_pins));
1550 cfg->line_outs = cfg->speaker_outs;
1551 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1552 sizeof(cfg->speaker_pins));
1553 cfg->speaker_outs = 0;
1554 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1555 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1556 fill_hardwired = true;
1559 if (cfg->hp_outs > 0 &&
1560 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1561 cfg->speaker_outs = cfg->line_outs;
1562 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1563 sizeof(cfg->speaker_pins));
1564 cfg->line_outs = cfg->hp_outs;
1565 memcpy(cfg->line_out_pins, cfg->hp_pins,
1566 sizeof(cfg->hp_pins));
1568 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1569 cfg->line_out_type = AUTO_PIN_HP_OUT;
1570 fill_hardwired = true;
1577 debug_badness("==> restoring best_cfg\n");
1579 fill_and_eval_dacs(codec, best_wired, best_mio);
1581 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1582 cfg->line_out_type, best_wired, best_mio);
1583 debug_show_configs(spec, cfg);
1585 if (cfg->line_out_pins[0]) {
1586 struct nid_path *path;
1587 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1589 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1596 /* add playback controls from the parsed DAC table */
1597 static int create_multi_out_ctls(struct hda_codec *codec,
1598 const struct auto_pin_cfg *cfg)
1600 struct hda_gen_spec *spec = codec->spec;
1601 int i, err, noutputs;
1603 noutputs = cfg->line_outs;
1604 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1605 noutputs += spec->multi_ios;
1607 for (i = 0; i < noutputs; i++) {
1610 struct nid_path *path;
1612 if (i >= cfg->line_outs) {
1614 name = channel_name[i];
1616 name = get_line_out_pfx(spec, i, true, &index);
1619 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1622 if (!name || !strcmp(name, "CLFE")) {
1624 err = add_vol_ctl(codec, "Center", 0, 1, path);
1627 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1630 err = add_sw_ctl(codec, "Center", 0, 1, path);
1633 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1637 err = add_stereo_vol(codec, name, index, path);
1640 err = add_stereo_sw(codec, name, index, path);
1648 static int create_extra_out(struct hda_codec *codec, int path_idx,
1649 const char *pfx, int cidx)
1651 struct nid_path *path;
1654 path = snd_hda_get_path_from_idx(codec, path_idx);
1657 err = add_stereo_vol(codec, pfx, cidx, path);
1660 err = add_stereo_sw(codec, pfx, cidx, path);
1666 /* add playback controls for speaker and HP outputs */
1667 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1668 const int *paths, const char *pfx)
1672 for (i = 0; i < num_pins; i++) {
1677 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1678 name = "Bass Speaker";
1679 else if (num_pins >= 3) {
1680 snprintf(tmp, sizeof(tmp), "%s %s",
1681 pfx, channel_name[i]);
1687 err = create_extra_out(codec, paths[i], name, idx);
1694 static int create_hp_out_ctls(struct hda_codec *codec)
1696 struct hda_gen_spec *spec = codec->spec;
1697 return create_extra_outs(codec, spec->autocfg.hp_outs,
1702 static int create_speaker_out_ctls(struct hda_codec *codec)
1704 struct hda_gen_spec *spec = codec->spec;
1705 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1706 spec->speaker_paths,
1711 * independent HP controls
1714 static int indep_hp_info(struct snd_kcontrol *kcontrol,
1715 struct snd_ctl_elem_info *uinfo)
1717 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1720 static int indep_hp_get(struct snd_kcontrol *kcontrol,
1721 struct snd_ctl_elem_value *ucontrol)
1723 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1724 struct hda_gen_spec *spec = codec->spec;
1725 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1729 static int indep_hp_put(struct snd_kcontrol *kcontrol,
1730 struct snd_ctl_elem_value *ucontrol)
1732 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1733 struct hda_gen_spec *spec = codec->spec;
1734 unsigned int select = ucontrol->value.enumerated.item[0];
1737 mutex_lock(&spec->pcm_mutex);
1738 if (spec->active_streams) {
1743 if (spec->indep_hp_enabled != select) {
1744 spec->indep_hp_enabled = select;
1745 if (spec->indep_hp_enabled)
1746 spec->multiout.hp_out_nid[0] = 0;
1748 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1752 mutex_unlock(&spec->pcm_mutex);
1756 static const struct snd_kcontrol_new indep_hp_ctl = {
1757 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1758 .name = "Independent HP",
1759 .info = indep_hp_info,
1760 .get = indep_hp_get,
1761 .put = indep_hp_put,
1765 static int create_indep_hp_ctls(struct hda_codec *codec)
1767 struct hda_gen_spec *spec = codec->spec;
1769 if (!spec->indep_hp)
1771 if (!spec->multiout.hp_out_nid[0]) {
1776 spec->indep_hp_enabled = false;
1777 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1778 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1784 * channel mode enum control
1787 static int ch_mode_info(struct snd_kcontrol *kcontrol,
1788 struct snd_ctl_elem_info *uinfo)
1790 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1791 struct hda_gen_spec *spec = codec->spec;
1794 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1796 uinfo->value.enumerated.items = spec->multi_ios + 1;
1797 if (uinfo->value.enumerated.item > spec->multi_ios)
1798 uinfo->value.enumerated.item = spec->multi_ios;
1799 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1800 sprintf(uinfo->value.enumerated.name, "%dch", chs);
1804 static int ch_mode_get(struct snd_kcontrol *kcontrol,
1805 struct snd_ctl_elem_value *ucontrol)
1807 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1808 struct hda_gen_spec *spec = codec->spec;
1809 ucontrol->value.enumerated.item[0] =
1810 (spec->ext_channel_count - spec->min_channel_count) / 2;
1814 static inline struct nid_path *
1815 get_multiio_path(struct hda_codec *codec, int idx)
1817 struct hda_gen_spec *spec = codec->spec;
1818 return snd_hda_get_path_from_idx(codec,
1819 spec->out_paths[spec->autocfg.line_outs + idx]);
1822 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1824 struct hda_gen_spec *spec = codec->spec;
1825 hda_nid_t nid = spec->multi_io[idx].pin;
1826 struct nid_path *path;
1828 path = get_multiio_path(codec, idx);
1832 if (path->active == output)
1836 set_pin_target(codec, nid, PIN_OUT, true);
1837 snd_hda_activate_path(codec, path, true, true);
1838 set_pin_eapd(codec, nid, true);
1840 set_pin_eapd(codec, nid, false);
1841 snd_hda_activate_path(codec, path, false, true);
1842 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
1845 /* update jack retasking in case it modifies any of them */
1846 snd_hda_gen_hp_automute(codec, NULL);
1847 snd_hda_gen_line_automute(codec, NULL);
1848 snd_hda_gen_mic_autoswitch(codec, NULL);
1853 static int ch_mode_put(struct snd_kcontrol *kcontrol,
1854 struct snd_ctl_elem_value *ucontrol)
1856 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1857 struct hda_gen_spec *spec = codec->spec;
1860 ch = ucontrol->value.enumerated.item[0];
1861 if (ch < 0 || ch > spec->multi_ios)
1863 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
1865 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
1866 for (i = 0; i < spec->multi_ios; i++)
1867 set_multi_io(codec, i, i < ch);
1868 spec->multiout.max_channels = max(spec->ext_channel_count,
1869 spec->const_channel_count);
1870 if (spec->need_dac_fix)
1871 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1875 static const struct snd_kcontrol_new channel_mode_enum = {
1876 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1877 .name = "Channel Mode",
1878 .info = ch_mode_info,
1883 static int create_multi_channel_mode(struct hda_codec *codec)
1885 struct hda_gen_spec *spec = codec->spec;
1887 if (spec->multi_ios > 0) {
1888 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
1895 * aamix loopback enable/disable switch
1898 #define loopback_mixing_info indep_hp_info
1900 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1901 struct snd_ctl_elem_value *ucontrol)
1903 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1904 struct hda_gen_spec *spec = codec->spec;
1905 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1909 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1910 int nomix_path_idx, int mix_path_idx)
1912 struct nid_path *nomix_path, *mix_path;
1914 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1915 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1916 if (!nomix_path || !mix_path)
1919 snd_hda_activate_path(codec, nomix_path, false, true);
1920 snd_hda_activate_path(codec, mix_path, true, true);
1922 snd_hda_activate_path(codec, mix_path, false, true);
1923 snd_hda_activate_path(codec, nomix_path, true, true);
1927 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1928 struct snd_ctl_elem_value *ucontrol)
1930 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1931 struct hda_gen_spec *spec = codec->spec;
1932 unsigned int val = ucontrol->value.enumerated.item[0];
1934 if (val == spec->aamix_mode)
1936 spec->aamix_mode = val;
1937 update_aamix_paths(codec, val, spec->out_paths[0],
1938 spec->aamix_out_paths[0]);
1939 update_aamix_paths(codec, val, spec->hp_paths[0],
1940 spec->aamix_out_paths[1]);
1941 update_aamix_paths(codec, val, spec->speaker_paths[0],
1942 spec->aamix_out_paths[2]);
1946 static const struct snd_kcontrol_new loopback_mixing_enum = {
1947 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1948 .name = "Loopback Mixing",
1949 .info = loopback_mixing_info,
1950 .get = loopback_mixing_get,
1951 .put = loopback_mixing_put,
1954 static int create_loopback_mixing_ctl(struct hda_codec *codec)
1956 struct hda_gen_spec *spec = codec->spec;
1958 if (!spec->mixer_nid)
1960 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1961 spec->aamix_out_paths[2]))
1963 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1969 * shared headphone/mic handling
1972 static void call_update_outputs(struct hda_codec *codec);
1974 /* for shared I/O, change the pin-control accordingly */
1975 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1977 struct hda_gen_spec *spec = codec->spec;
1979 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1980 /* NOTE: this assumes that there are only two inputs, the
1981 * first is the real internal mic and the second is HP/mic jack.
1984 val = snd_hda_get_default_vref(codec, pin);
1986 /* This pin does not have vref caps - let's enable vref on pin 0x18
1987 instead, as suggested by Realtek */
1988 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1989 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1990 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1991 if (vref_val != AC_PINCTL_VREF_HIZ)
1992 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1993 PIN_IN | (set_as_mic ? vref_val : 0));
1996 val = set_as_mic ? val | PIN_IN : PIN_HP;
1997 set_pin_target(codec, pin, val, true);
1999 spec->automute_speaker = !set_as_mic;
2000 call_update_outputs(codec);
2003 /* create a shared input with the headphone out */
2004 static int create_shared_input(struct hda_codec *codec)
2006 struct hda_gen_spec *spec = codec->spec;
2007 struct auto_pin_cfg *cfg = &spec->autocfg;
2008 unsigned int defcfg;
2011 /* only one internal input pin? */
2012 if (cfg->num_inputs != 1)
2014 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2015 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2018 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2019 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2020 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2021 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2023 return 0; /* both not available */
2025 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2026 return 0; /* no input */
2028 cfg->inputs[1].pin = nid;
2029 cfg->inputs[1].type = AUTO_PIN_MIC;
2030 cfg->num_inputs = 2;
2031 spec->shared_mic_hp = 1;
2032 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2039 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2040 struct snd_ctl_elem_info *uinfo)
2042 static const char * const texts[] = {
2043 "Line Out", "Headphone Out",
2045 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2048 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2049 struct snd_ctl_elem_value *ucontrol)
2051 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2052 hda_nid_t nid = kcontrol->private_value;
2053 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2054 ucontrol->value.enumerated.item[0] = 1;
2056 ucontrol->value.enumerated.item[0] = 0;
2060 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2061 struct snd_ctl_elem_value *ucontrol)
2063 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2064 hda_nid_t nid = kcontrol->private_value;
2067 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2068 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2070 snd_hda_set_pin_ctl_cache(codec, nid, val);
2074 static const struct snd_kcontrol_new out_jack_mode_enum = {
2075 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2076 .info = out_jack_mode_info,
2077 .get = out_jack_mode_get,
2078 .put = out_jack_mode_put,
2081 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2083 struct hda_gen_spec *spec = codec->spec;
2086 for (i = 0; i < spec->kctls.used; i++) {
2087 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2088 if (!strcmp(kctl->name, name) && kctl->index == idx)
2094 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2095 char *name, size_t name_len)
2097 struct hda_gen_spec *spec = codec->spec;
2100 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2101 strlcat(name, " Jack Mode", name_len);
2103 for (; find_kctl_name(codec, name, idx); idx++)
2107 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2110 struct hda_gen_spec *spec = codec->spec;
2113 for (i = 0; i < num_pins; i++) {
2114 hda_nid_t pin = pins[i];
2115 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2116 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2117 struct snd_kcontrol_new *knew;
2119 get_jack_mode_name(codec, pin, name, sizeof(name));
2120 knew = snd_hda_gen_add_kctl(spec, name,
2121 &out_jack_mode_enum);
2124 knew->private_value = pin;
2137 /* add the powersave loopback-list entry */
2138 static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2140 struct hda_amp_list *list;
2142 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2144 list = spec->loopback_list + spec->num_loopbacks;
2146 list->dir = HDA_INPUT;
2148 spec->num_loopbacks++;
2149 spec->loopback.amplist = spec->loopback_list;
2152 #define add_loopback_list(spec, mix, idx) /* NOP */
2155 /* create input playback/capture controls for the given pin */
2156 static int new_analog_input(struct hda_codec *codec, int input_idx,
2157 hda_nid_t pin, const char *ctlname, int ctlidx,
2160 struct hda_gen_spec *spec = codec->spec;
2161 struct nid_path *path;
2165 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2166 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2167 return 0; /* no need for analog loopback */
2169 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
2172 print_nid_path("loopback", path);
2173 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
2175 idx = path->idx[path->depth - 1];
2176 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2177 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2178 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
2181 path->ctls[NID_PATH_VOL_CTL] = val;
2184 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2185 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2186 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
2189 path->ctls[NID_PATH_MUTE_CTL] = val;
2192 path->active = true;
2193 add_loopback_list(spec, mix_nid, idx);
2197 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2199 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2200 return (pincap & AC_PINCAP_IN) != 0;
2203 /* Parse the codec tree and retrieve ADCs */
2204 static int fill_adc_nids(struct hda_codec *codec)
2206 struct hda_gen_spec *spec = codec->spec;
2208 hda_nid_t *adc_nids = spec->adc_nids;
2209 int max_nums = ARRAY_SIZE(spec->adc_nids);
2212 nid = codec->start_nid;
2213 for (i = 0; i < codec->num_nodes; i++, nid++) {
2214 unsigned int caps = get_wcaps(codec, nid);
2215 int type = get_wcaps_type(caps);
2217 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2219 adc_nids[nums] = nid;
2220 if (++nums >= max_nums)
2223 spec->num_adc_nids = nums;
2227 /* filter out invalid adc_nids that don't give all active input pins;
2228 * if needed, check whether dynamic ADC-switching is available
2230 static int check_dyn_adc_switch(struct hda_codec *codec)
2232 struct hda_gen_spec *spec = codec->spec;
2233 struct hda_input_mux *imux = &spec->input_mux;
2234 unsigned int ok_bits;
2240 for (n = 0; n < spec->num_adc_nids; n++) {
2241 for (i = 0; i < imux->num_items; i++) {
2242 if (!spec->input_paths[i][n])
2245 if (i >= imux->num_items) {
2246 ok_bits |= (1 << n);
2252 if (spec->shared_mic_hp) {
2253 spec->shared_mic_hp = 0;
2254 imux->num_items = 1;
2258 /* check whether ADC-switch is possible */
2259 for (i = 0; i < imux->num_items; i++) {
2260 for (n = 0; n < spec->num_adc_nids; n++) {
2261 if (spec->input_paths[i][n]) {
2262 spec->dyn_adc_idx[i] = n;
2268 snd_printdd("hda-codec: enabling ADC switching\n");
2269 spec->dyn_adc_switch = 1;
2270 } else if (nums != spec->num_adc_nids) {
2271 /* shrink the invalid adcs and input paths */
2273 for (n = 0; n < spec->num_adc_nids; n++) {
2274 if (!(ok_bits & (1 << n)))
2277 spec->adc_nids[nums] = spec->adc_nids[n];
2278 for (i = 0; i < imux->num_items; i++) {
2279 invalidate_nid_path(codec,
2280 spec->input_paths[i][nums]);
2281 spec->input_paths[i][nums] =
2282 spec->input_paths[i][n];
2287 spec->num_adc_nids = nums;
2290 if (imux->num_items == 1 || spec->shared_mic_hp) {
2291 snd_printdd("hda-codec: reducing to a single ADC\n");
2292 spec->num_adc_nids = 1; /* reduce to a single ADC */
2295 /* single index for individual volumes ctls */
2296 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2297 spec->num_adc_nids = 1;
2302 /* parse capture source paths from the given pin and create imux items */
2303 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2304 int num_adcs, const char *label, int anchor)
2306 struct hda_gen_spec *spec = codec->spec;
2307 struct hda_input_mux *imux = &spec->input_mux;
2308 int imux_idx = imux->num_items;
2309 bool imux_added = false;
2312 for (c = 0; c < num_adcs; c++) {
2313 struct nid_path *path;
2314 hda_nid_t adc = spec->adc_nids[c];
2316 if (!is_reachable_path(codec, pin, adc))
2318 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2321 print_nid_path("input", path);
2322 spec->input_paths[imux_idx][c] =
2323 snd_hda_get_path_idx(codec, path);
2326 spec->imux_pins[imux->num_items] = pin;
2327 snd_hda_add_imux_item(imux, label,
2328 imux->num_items, NULL);
2337 * create playback/capture controls for input pins
2339 static int create_input_ctls(struct hda_codec *codec)
2341 struct hda_gen_spec *spec = codec->spec;
2342 const struct auto_pin_cfg *cfg = &spec->autocfg;
2343 hda_nid_t mixer = spec->mixer_nid;
2345 int i, err, type_idx = 0;
2346 const char *prev_label = NULL;
2349 num_adcs = fill_adc_nids(codec);
2353 for (i = 0; i < cfg->num_inputs; i++) {
2357 pin = cfg->inputs[i].pin;
2358 if (!is_input_pin(codec, pin))
2361 label = hda_get_autocfg_input_label(codec, cfg, i);
2362 if (prev_label && !strcmp(label, prev_label))
2369 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2370 val |= snd_hda_get_default_vref(codec, pin);
2371 set_pin_target(codec, pin, val, false);
2374 if (is_reachable_path(codec, pin, mixer)) {
2375 err = new_analog_input(codec, i, pin,
2376 label, type_idx, mixer);
2382 err = parse_capture_source(codec, pin, num_adcs, label, -mixer);
2387 if (mixer && spec->add_stereo_mix_input) {
2388 err = parse_capture_source(codec, mixer, num_adcs,
2402 /* get the input path specified by the given adc and imux indices */
2403 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
2405 struct hda_gen_spec *spec = codec->spec;
2406 if (spec->dyn_adc_switch)
2407 adc_idx = spec->dyn_adc_idx[imux_idx];
2408 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
2411 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2414 static int mux_enum_info(struct snd_kcontrol *kcontrol,
2415 struct snd_ctl_elem_info *uinfo)
2417 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2418 struct hda_gen_spec *spec = codec->spec;
2419 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2422 static int mux_enum_get(struct snd_kcontrol *kcontrol,
2423 struct snd_ctl_elem_value *ucontrol)
2425 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2426 struct hda_gen_spec *spec = codec->spec;
2427 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2429 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2433 static int mux_enum_put(struct snd_kcontrol *kcontrol,
2434 struct snd_ctl_elem_value *ucontrol)
2436 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2437 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2438 return mux_select(codec, adc_idx,
2439 ucontrol->value.enumerated.item[0]);
2442 static const struct snd_kcontrol_new cap_src_temp = {
2443 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2444 .name = "Input Source",
2445 .info = mux_enum_info,
2446 .get = mux_enum_get,
2447 .put = mux_enum_put,
2451 * capture volume and capture switch ctls
2454 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2455 struct snd_ctl_elem_value *ucontrol);
2457 /* call the given amp update function for all amps in the imux list at once */
2458 static int cap_put_caller(struct snd_kcontrol *kcontrol,
2459 struct snd_ctl_elem_value *ucontrol,
2460 put_call_t func, int type)
2462 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2463 struct hda_gen_spec *spec = codec->spec;
2464 const struct hda_input_mux *imux;
2465 struct nid_path *path;
2466 int i, adc_idx, err = 0;
2468 imux = &spec->input_mux;
2469 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2470 mutex_lock(&codec->control_mutex);
2471 /* we use the cache-only update at first since multiple input paths
2472 * may shared the same amp; by updating only caches, the redundant
2473 * writes to hardware can be reduced.
2475 codec->cached_write = 1;
2476 for (i = 0; i < imux->num_items; i++) {
2477 path = get_input_path(codec, adc_idx, i);
2478 if (!path || !path->ctls[type])
2480 kcontrol->private_value = path->ctls[type];
2481 err = func(kcontrol, ucontrol);
2486 codec->cached_write = 0;
2487 mutex_unlock(&codec->control_mutex);
2488 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
2489 if (err >= 0 && spec->cap_sync_hook)
2490 spec->cap_sync_hook(codec);
2494 /* capture volume ctl callbacks */
2495 #define cap_vol_info snd_hda_mixer_amp_volume_info
2496 #define cap_vol_get snd_hda_mixer_amp_volume_get
2497 #define cap_vol_tlv snd_hda_mixer_amp_tlv
2499 static int cap_vol_put(struct snd_kcontrol *kcontrol,
2500 struct snd_ctl_elem_value *ucontrol)
2502 return cap_put_caller(kcontrol, ucontrol,
2503 snd_hda_mixer_amp_volume_put,
2507 static const struct snd_kcontrol_new cap_vol_temp = {
2508 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2509 .name = "Capture Volume",
2510 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2511 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2512 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2513 .info = cap_vol_info,
2516 .tlv = { .c = cap_vol_tlv },
2519 /* capture switch ctl callbacks */
2520 #define cap_sw_info snd_ctl_boolean_stereo_info
2521 #define cap_sw_get snd_hda_mixer_amp_switch_get
2523 static int cap_sw_put(struct snd_kcontrol *kcontrol,
2524 struct snd_ctl_elem_value *ucontrol)
2526 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2527 struct hda_gen_spec *spec = codec->spec;
2530 ret = cap_put_caller(kcontrol, ucontrol,
2531 snd_hda_mixer_amp_switch_put,
2536 if (spec->capture_switch_hook) {
2537 bool enable = (ucontrol->value.integer.value[0] ||
2538 ucontrol->value.integer.value[1]);
2539 spec->capture_switch_hook(codec, enable);
2545 static const struct snd_kcontrol_new cap_sw_temp = {
2546 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2547 .name = "Capture Switch",
2548 .info = cap_sw_info,
2553 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2558 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2559 for (depth = 0; depth < 3; depth++) {
2560 if (depth >= path->depth)
2562 i = path->depth - depth - 1;
2563 nid = path->path[i];
2564 if (!path->ctls[NID_PATH_VOL_CTL]) {
2565 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2566 path->ctls[NID_PATH_VOL_CTL] =
2567 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2568 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2569 int idx = path->idx[i];
2570 if (!depth && codec->single_adc_amp)
2572 path->ctls[NID_PATH_VOL_CTL] =
2573 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2576 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2577 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2578 path->ctls[NID_PATH_MUTE_CTL] =
2579 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2580 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2581 int idx = path->idx[i];
2582 if (!depth && codec->single_adc_amp)
2584 path->ctls[NID_PATH_MUTE_CTL] =
2585 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2592 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2594 struct hda_gen_spec *spec = codec->spec;
2595 struct auto_pin_cfg *cfg = &spec->autocfg;
2599 if (!spec->inv_dmic_split)
2601 for (i = 0; i < cfg->num_inputs; i++) {
2602 if (cfg->inputs[i].pin != nid)
2604 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2606 val = snd_hda_codec_get_pincfg(codec, nid);
2607 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2612 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2613 int idx, bool is_switch, unsigned int ctl,
2616 struct hda_gen_spec *spec = codec->spec;
2618 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2619 const char *sfx = is_switch ? "Switch" : "Volume";
2620 unsigned int chs = inv_dmic ? 1 : 3;
2627 snprintf(tmpname, sizeof(tmpname),
2628 "%s Capture %s", label, sfx);
2630 snprintf(tmpname, sizeof(tmpname),
2632 err = add_control(spec, type, tmpname, idx,
2633 amp_val_replace_channels(ctl, chs));
2634 if (err < 0 || !inv_dmic)
2637 /* Make independent right kcontrol */
2639 snprintf(tmpname, sizeof(tmpname),
2640 "Inverted %s Capture %s", label, sfx);
2642 snprintf(tmpname, sizeof(tmpname),
2643 "Inverted Capture %s", sfx);
2644 return add_control(spec, type, tmpname, idx,
2645 amp_val_replace_channels(ctl, 2));
2648 /* create single (and simple) capture volume and switch controls */
2649 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2650 unsigned int vol_ctl, unsigned int sw_ctl,
2654 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2657 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2663 /* create bound capture volume and switch controls */
2664 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2665 unsigned int vol_ctl, unsigned int sw_ctl)
2667 struct hda_gen_spec *spec = codec->spec;
2668 struct snd_kcontrol_new *knew;
2671 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
2675 knew->private_value = vol_ctl;
2676 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2679 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
2683 knew->private_value = sw_ctl;
2684 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2689 /* return the vol ctl when used first in the imux list */
2690 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2692 struct nid_path *path;
2696 path = get_input_path(codec, 0, idx);
2699 ctl = path->ctls[type];
2702 for (i = 0; i < idx - 1; i++) {
2703 path = get_input_path(codec, 0, i);
2704 if (path && path->ctls[type] == ctl)
2710 /* create individual capture volume and switch controls per input */
2711 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2713 struct hda_gen_spec *spec = codec->spec;
2714 struct hda_input_mux *imux = &spec->input_mux;
2715 int i, err, type, type_idx = 0;
2716 const char *prev_label = NULL;
2718 for (i = 0; i < imux->num_items; i++) {
2721 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2722 if (prev_label && !strcmp(label, prev_label))
2727 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2729 for (type = 0; type < 2; type++) {
2730 err = add_single_cap_ctl(codec, label, type_idx, type,
2731 get_first_cap_ctl(codec, i, type),
2740 static int create_capture_mixers(struct hda_codec *codec)
2742 struct hda_gen_spec *spec = codec->spec;
2743 struct hda_input_mux *imux = &spec->input_mux;
2744 int i, n, nums, err;
2746 if (spec->dyn_adc_switch)
2749 nums = spec->num_adc_nids;
2751 if (!spec->auto_mic && imux->num_items > 1) {
2752 struct snd_kcontrol_new *knew;
2754 name = nums > 1 ? "Input Source" : "Capture Source";
2755 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
2761 for (n = 0; n < nums; n++) {
2763 bool inv_dmic = false;
2767 for (i = 0; i < imux->num_items; i++) {
2768 struct nid_path *path;
2769 path = get_input_path(codec, n, i);
2772 parse_capvol_in_path(codec, path);
2774 vol = path->ctls[NID_PATH_VOL_CTL];
2775 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2778 sw = path->ctls[NID_PATH_MUTE_CTL];
2779 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2781 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2786 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2788 else if (!spec->multi_cap_vol)
2789 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2791 err = create_multi_cap_vol_ctl(codec);
2800 * add mic boosts if needed
2802 static int parse_mic_boost(struct hda_codec *codec)
2804 struct hda_gen_spec *spec = codec->spec;
2805 struct auto_pin_cfg *cfg = &spec->autocfg;
2809 const char *prev_label = NULL;
2811 for (i = 0; i < cfg->num_inputs; i++) {
2812 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2814 nid = cfg->inputs[i].pin;
2815 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2817 char boost_label[44];
2818 struct nid_path *path;
2821 label = hda_get_autocfg_input_label(codec, cfg, i);
2822 if (prev_label && !strcmp(label, prev_label))
2828 snprintf(boost_label, sizeof(boost_label),
2829 "%s Boost Volume", label);
2830 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2831 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2832 boost_label, type_idx, val);
2836 path = snd_hda_get_nid_path(codec, nid, 0);
2838 path->ctls[NID_PATH_BOOST_CTL] = val;
2845 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2847 static void parse_digital(struct hda_codec *codec)
2849 struct hda_gen_spec *spec = codec->spec;
2850 struct nid_path *path;
2852 hda_nid_t dig_nid, pin;
2854 /* support multiple SPDIFs; the secondary is set up as a slave */
2856 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2857 pin = spec->autocfg.dig_out_pins[i];
2858 dig_nid = look_for_dac(codec, pin, true);
2861 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
2864 print_nid_path("digout", path);
2865 path->active = true;
2866 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
2867 set_pin_target(codec, pin, PIN_OUT, false);
2869 spec->multiout.dig_out_nid = dig_nid;
2870 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2872 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2873 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2875 spec->slave_dig_outs[nums - 1] = dig_nid;
2880 if (spec->autocfg.dig_in_pin) {
2881 pin = spec->autocfg.dig_in_pin;
2882 dig_nid = codec->start_nid;
2883 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2884 unsigned int wcaps = get_wcaps(codec, dig_nid);
2885 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2887 if (!(wcaps & AC_WCAP_DIGITAL))
2889 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
2891 print_nid_path("digin", path);
2892 path->active = true;
2893 spec->dig_in_nid = dig_nid;
2894 spec->digin_path = snd_hda_get_path_idx(codec, path);
2895 set_pin_target(codec, pin, PIN_IN, false);
2904 * input MUX handling
2907 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2909 /* select the given imux item; either unmute exclusively or select the route */
2910 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2913 struct hda_gen_spec *spec = codec->spec;
2914 const struct hda_input_mux *imux;
2915 struct nid_path *path;
2917 imux = &spec->input_mux;
2918 if (!imux->num_items)
2921 if (idx >= imux->num_items)
2922 idx = imux->num_items - 1;
2923 if (spec->cur_mux[adc_idx] == idx)
2926 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
2930 snd_hda_activate_path(codec, path, false, false);
2932 spec->cur_mux[adc_idx] = idx;
2934 if (spec->shared_mic_hp)
2935 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2937 if (spec->dyn_adc_switch)
2938 dyn_adc_pcm_resetup(codec, idx);
2940 path = get_input_path(codec, adc_idx, idx);
2945 snd_hda_activate_path(codec, path, true, false);
2946 if (spec->cap_sync_hook)
2947 spec->cap_sync_hook(codec);
2953 * Jack detections for HP auto-mute and mic-switch
2956 /* check each pin in the given array; returns true if any of them is plugged */
2957 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2961 for (i = 0; i < num_pins; i++) {
2962 hda_nid_t nid = pins[i];
2965 /* don't detect pins retasked as inputs */
2966 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
2968 present |= snd_hda_jack_detect(codec, nid);
2973 /* standard HP/line-out auto-mute helper */
2974 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2977 struct hda_gen_spec *spec = codec->spec;
2980 for (i = 0; i < num_pins; i++) {
2981 hda_nid_t nid = pins[i];
2985 /* don't reset VREF value in case it's controlling
2986 * the amp (see alc861_fixup_asus_amp_vref_0f())
2988 if (spec->keep_vref_in_automute)
2989 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
2993 val |= snd_hda_codec_get_pin_target(codec, nid);
2994 /* here we call update_pin_ctl() so that the pinctl is changed
2995 * without changing the pinctl target value;
2996 * the original target value will be still referred at the
2997 * init / resume again
2999 update_pin_ctl(codec, nid, val);
3000 set_pin_eapd(codec, nid, !mute);
3004 /* Toggle outputs muting */
3005 void snd_hda_gen_update_outputs(struct hda_codec *codec)
3007 struct hda_gen_spec *spec = codec->spec;
3010 /* Control HP pins/amps depending on master_mute state;
3011 * in general, HP pins/amps control should be enabled in all cases,
3012 * but currently set only for master_mute, just to be safe
3014 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3015 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3016 spec->autocfg.hp_pins, spec->master_mute);
3018 if (!spec->automute_speaker)
3021 on = spec->hp_jack_present | spec->line_jack_present;
3022 on |= spec->master_mute;
3023 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
3024 spec->autocfg.speaker_pins, on);
3026 /* toggle line-out mutes if needed, too */
3027 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3028 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3029 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3031 if (!spec->automute_lo)
3034 on = spec->hp_jack_present;
3035 on |= spec->master_mute;
3036 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3037 spec->autocfg.line_out_pins, on);
3039 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
3041 static void call_update_outputs(struct hda_codec *codec)
3043 struct hda_gen_spec *spec = codec->spec;
3044 if (spec->automute_hook)
3045 spec->automute_hook(codec);
3047 snd_hda_gen_update_outputs(codec);
3050 /* standard HP-automute helper */
3051 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3053 struct hda_gen_spec *spec = codec->spec;
3055 spec->hp_jack_present =
3056 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3057 spec->autocfg.hp_pins);
3058 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3060 call_update_outputs(codec);
3062 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
3064 /* standard line-out-automute helper */
3065 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3067 struct hda_gen_spec *spec = codec->spec;
3069 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3071 /* check LO jack only when it's different from HP */
3072 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3075 spec->line_jack_present =
3076 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3077 spec->autocfg.line_out_pins);
3078 if (!spec->automute_speaker || !spec->detect_lo)
3080 call_update_outputs(codec);
3082 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
3084 /* standard mic auto-switch helper */
3085 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
3087 struct hda_gen_spec *spec = codec->spec;
3090 if (!spec->auto_mic)
3093 for (i = spec->am_num_entries - 1; i > 0; i--) {
3094 hda_nid_t pin = spec->am_entry[i].pin;
3095 /* don't detect pins retasked as outputs */
3096 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3098 if (snd_hda_jack_detect(codec, pin)) {
3099 mux_select(codec, 0, spec->am_entry[i].idx);
3103 mux_select(codec, 0, spec->am_entry[0].idx);
3105 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
3108 * Auto-Mute mode mixer enum support
3110 static int automute_mode_info(struct snd_kcontrol *kcontrol,
3111 struct snd_ctl_elem_info *uinfo)
3113 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3114 struct hda_gen_spec *spec = codec->spec;
3115 static const char * const texts3[] = {
3116 "Disabled", "Speaker Only", "Line Out+Speaker"
3119 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3120 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3121 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3124 static int automute_mode_get(struct snd_kcontrol *kcontrol,
3125 struct snd_ctl_elem_value *ucontrol)
3127 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3128 struct hda_gen_spec *spec = codec->spec;
3129 unsigned int val = 0;
3130 if (spec->automute_speaker)
3132 if (spec->automute_lo)
3135 ucontrol->value.enumerated.item[0] = val;
3139 static int automute_mode_put(struct snd_kcontrol *kcontrol,
3140 struct snd_ctl_elem_value *ucontrol)
3142 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3143 struct hda_gen_spec *spec = codec->spec;
3145 switch (ucontrol->value.enumerated.item[0]) {
3147 if (!spec->automute_speaker && !spec->automute_lo)
3149 spec->automute_speaker = 0;
3150 spec->automute_lo = 0;
3153 if (spec->automute_speaker_possible) {
3154 if (!spec->automute_lo && spec->automute_speaker)
3156 spec->automute_speaker = 1;
3157 spec->automute_lo = 0;
3158 } else if (spec->automute_lo_possible) {
3159 if (spec->automute_lo)
3161 spec->automute_lo = 1;
3166 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3168 if (spec->automute_speaker && spec->automute_lo)
3170 spec->automute_speaker = 1;
3171 spec->automute_lo = 1;
3176 call_update_outputs(codec);
3180 static const struct snd_kcontrol_new automute_mode_enum = {
3181 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3182 .name = "Auto-Mute Mode",
3183 .info = automute_mode_info,
3184 .get = automute_mode_get,
3185 .put = automute_mode_put,
3188 static int add_automute_mode_enum(struct hda_codec *codec)
3190 struct hda_gen_spec *spec = codec->spec;
3192 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
3198 * Check the availability of HP/line-out auto-mute;
3199 * Set up appropriately if really supported
3201 static int check_auto_mute_availability(struct hda_codec *codec)
3203 struct hda_gen_spec *spec = codec->spec;
3204 struct auto_pin_cfg *cfg = &spec->autocfg;
3208 if (cfg->hp_pins[0])
3210 if (cfg->line_out_pins[0])
3212 if (cfg->speaker_pins[0])
3214 if (present < 2) /* need two different output types */
3217 if (!cfg->speaker_pins[0] &&
3218 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3219 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3220 sizeof(cfg->speaker_pins));
3221 cfg->speaker_outs = cfg->line_outs;
3224 if (!cfg->hp_pins[0] &&
3225 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3226 memcpy(cfg->hp_pins, cfg->line_out_pins,
3227 sizeof(cfg->hp_pins));
3228 cfg->hp_outs = cfg->line_outs;
3231 for (i = 0; i < cfg->hp_outs; i++) {
3232 hda_nid_t nid = cfg->hp_pins[i];
3233 if (!is_jack_detectable(codec, nid))
3235 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3237 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
3238 spec->hp_automute_hook ?
3239 spec->hp_automute_hook :
3240 snd_hda_gen_hp_automute);
3241 spec->detect_hp = 1;
3244 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3245 if (cfg->speaker_outs)
3246 for (i = 0; i < cfg->line_outs; i++) {
3247 hda_nid_t nid = cfg->line_out_pins[i];
3248 if (!is_jack_detectable(codec, nid))
3250 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3251 snd_hda_jack_detect_enable_callback(codec, nid,
3252 HDA_GEN_FRONT_EVENT,
3253 spec->line_automute_hook ?
3254 spec->line_automute_hook :
3255 snd_hda_gen_line_automute);
3256 spec->detect_lo = 1;
3258 spec->automute_lo_possible = spec->detect_hp;
3261 spec->automute_speaker_possible = cfg->speaker_outs &&
3262 (spec->detect_hp || spec->detect_lo);
3264 spec->automute_lo = spec->automute_lo_possible;
3265 spec->automute_speaker = spec->automute_speaker_possible;
3267 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3268 /* create a control for automute mode */
3269 err = add_automute_mode_enum(codec);
3276 /* check whether all auto-mic pins are valid; setup indices if OK */
3277 static bool auto_mic_check_imux(struct hda_codec *codec)
3279 struct hda_gen_spec *spec = codec->spec;
3280 const struct hda_input_mux *imux;
3283 imux = &spec->input_mux;
3284 for (i = 0; i < spec->am_num_entries; i++) {
3285 spec->am_entry[i].idx =
3286 find_idx_in_nid_list(spec->am_entry[i].pin,
3287 spec->imux_pins, imux->num_items);
3288 if (spec->am_entry[i].idx < 0)
3289 return false; /* no corresponding imux */
3292 /* we don't need the jack detection for the first pin */
3293 for (i = 1; i < spec->am_num_entries; i++)
3294 snd_hda_jack_detect_enable_callback(codec,
3295 spec->am_entry[i].pin,
3297 spec->mic_autoswitch_hook ?
3298 spec->mic_autoswitch_hook :
3299 snd_hda_gen_mic_autoswitch);
3303 static int compare_attr(const void *ap, const void *bp)
3305 const struct automic_entry *a = ap;
3306 const struct automic_entry *b = bp;
3307 return (int)(a->attr - b->attr);
3311 * Check the availability of auto-mic switch;
3312 * Set up if really supported
3314 static int check_auto_mic_availability(struct hda_codec *codec)
3316 struct hda_gen_spec *spec = codec->spec;
3317 struct auto_pin_cfg *cfg = &spec->autocfg;
3321 if (spec->suppress_auto_mic)
3326 for (i = 0; i < cfg->num_inputs; i++) {
3327 hda_nid_t nid = cfg->inputs[i].pin;
3329 attr = snd_hda_codec_get_pincfg(codec, nid);
3330 attr = snd_hda_get_input_pin_attr(attr);
3331 if (types & (1 << attr))
3332 return 0; /* already occupied */
3334 case INPUT_PIN_ATTR_INT:
3335 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3336 return 0; /* invalid type */
3338 case INPUT_PIN_ATTR_UNUSED:
3339 return 0; /* invalid entry */
3341 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3342 return 0; /* invalid type */
3343 if (!spec->line_in_auto_switch &&
3344 cfg->inputs[i].type != AUTO_PIN_MIC)
3345 return 0; /* only mic is allowed */
3346 if (!is_jack_detectable(codec, nid))
3347 return 0; /* no unsol support */
3350 if (num_pins >= MAX_AUTO_MIC_PINS)
3352 types |= (1 << attr);
3353 spec->am_entry[num_pins].pin = nid;
3354 spec->am_entry[num_pins].attr = attr;
3361 spec->am_num_entries = num_pins;
3362 /* sort the am_entry in the order of attr so that the pin with a
3363 * higher attr will be selected when the jack is plugged.
3365 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3366 compare_attr, NULL);
3368 if (!auto_mic_check_imux(codec))
3372 spec->num_adc_nids = 1;
3373 spec->cur_mux[0] = spec->am_entry[0].idx;
3374 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3375 spec->am_entry[0].pin,
3376 spec->am_entry[1].pin,
3377 spec->am_entry[2].pin);
3384 * Parse the given BIOS configuration and set up the hda_gen_spec
3386 * return 1 if successful, 0 if the proper config is not found,
3387 * or a negative error code
3389 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
3390 struct auto_pin_cfg *cfg)
3392 struct hda_gen_spec *spec = codec->spec;
3395 parse_user_hints(codec);
3397 if (cfg != &spec->autocfg) {
3398 spec->autocfg = *cfg;
3399 cfg = &spec->autocfg;
3402 if (!cfg->line_outs) {
3403 if (cfg->dig_outs || cfg->dig_in_pin) {
3404 spec->multiout.max_channels = 2;
3405 spec->no_analog = 1;
3408 return 0; /* can't find valid BIOS pin config */
3411 if (!spec->no_primary_hp &&
3412 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3413 cfg->line_outs <= cfg->hp_outs) {
3414 /* use HP as primary out */
3415 cfg->speaker_outs = cfg->line_outs;
3416 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3417 sizeof(cfg->speaker_pins));
3418 cfg->line_outs = cfg->hp_outs;
3419 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3421 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3422 cfg->line_out_type = AUTO_PIN_HP_OUT;
3425 err = parse_output_paths(codec);
3428 err = create_multi_channel_mode(codec);
3431 err = create_multi_out_ctls(codec, cfg);
3434 err = create_hp_out_ctls(codec);
3437 err = create_speaker_out_ctls(codec);
3440 err = create_indep_hp_ctls(codec);
3443 err = create_loopback_mixing_ctl(codec);
3446 err = create_shared_input(codec);
3449 err = create_input_ctls(codec);
3453 spec->const_channel_count = spec->ext_channel_count;
3454 /* check the multiple speaker and headphone pins */
3455 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3456 spec->const_channel_count = max(spec->const_channel_count,
3457 cfg->speaker_outs * 2);
3458 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3459 spec->const_channel_count = max(spec->const_channel_count,
3461 spec->multiout.max_channels = max(spec->ext_channel_count,
3462 spec->const_channel_count);
3464 err = check_auto_mute_availability(codec);
3468 err = check_dyn_adc_switch(codec);
3472 if (!spec->shared_mic_hp) {
3473 err = check_auto_mic_availability(codec);
3478 err = create_capture_mixers(codec);
3482 err = parse_mic_boost(codec);
3486 if (spec->add_out_jack_modes) {
3487 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3488 err = create_out_jack_modes(codec, cfg->line_outs,
3489 cfg->line_out_pins);
3493 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3494 err = create_out_jack_modes(codec, cfg->hp_outs,
3502 parse_digital(codec);
3506 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
3510 * Build control elements
3513 /* slave controls for virtual master */
3514 static const char * const slave_pfxs[] = {
3515 "Front", "Surround", "Center", "LFE", "Side",
3516 "Headphone", "Speaker", "Mono", "Line Out",
3517 "CLFE", "Bass Speaker", "PCM",
3518 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3519 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3524 int snd_hda_gen_build_controls(struct hda_codec *codec)
3526 struct hda_gen_spec *spec = codec->spec;
3529 if (spec->kctls.used) {
3530 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3535 if (spec->multiout.dig_out_nid) {
3536 err = snd_hda_create_dig_out_ctls(codec,
3537 spec->multiout.dig_out_nid,
3538 spec->multiout.dig_out_nid,
3539 spec->pcm_rec[1].pcm_type);
3542 if (!spec->no_analog) {
3543 err = snd_hda_create_spdif_share_sw(codec,
3547 spec->multiout.share_spdif = 1;
3550 if (spec->dig_in_nid) {
3551 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3556 /* if we have no master control, let's create it */
3557 if (!spec->no_analog &&
3558 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3559 unsigned int vmaster_tlv[4];
3560 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3561 HDA_OUTPUT, vmaster_tlv);
3562 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3563 vmaster_tlv, slave_pfxs,
3568 if (!spec->no_analog &&
3569 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3570 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3573 true, &spec->vmaster_mute.sw_kctl);
3576 if (spec->vmaster_mute.hook)
3577 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3578 spec->vmaster_mute_enum);
3581 free_kctls(spec); /* no longer needed */
3583 if (spec->shared_mic_hp) {
3585 int nid = spec->autocfg.inputs[1].pin;
3586 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3589 err = snd_hda_jack_detect_enable(codec, nid, 0);
3594 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3600 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3607 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3608 struct hda_codec *codec,
3609 struct snd_pcm_substream *substream,
3612 struct hda_gen_spec *spec = codec->spec;
3613 if (spec->pcm_playback_hook)
3614 spec->pcm_playback_hook(hinfo, codec, substream, action);
3618 * Analog playback callbacks
3620 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3621 struct hda_codec *codec,
3622 struct snd_pcm_substream *substream)
3624 struct hda_gen_spec *spec = codec->spec;
3627 mutex_lock(&spec->pcm_mutex);
3628 err = snd_hda_multi_out_analog_open(codec,
3629 &spec->multiout, substream,
3632 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3633 call_pcm_playback_hook(hinfo, codec, substream,
3634 HDA_GEN_PCM_ACT_OPEN);
3636 mutex_unlock(&spec->pcm_mutex);
3640 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3641 struct hda_codec *codec,
3642 unsigned int stream_tag,
3643 unsigned int format,
3644 struct snd_pcm_substream *substream)
3646 struct hda_gen_spec *spec = codec->spec;
3649 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3650 stream_tag, format, substream);
3652 call_pcm_playback_hook(hinfo, codec, substream,
3653 HDA_GEN_PCM_ACT_PREPARE);
3657 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3658 struct hda_codec *codec,
3659 struct snd_pcm_substream *substream)
3661 struct hda_gen_spec *spec = codec->spec;
3664 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3666 call_pcm_playback_hook(hinfo, codec, substream,
3667 HDA_GEN_PCM_ACT_CLEANUP);
3671 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3672 struct hda_codec *codec,
3673 struct snd_pcm_substream *substream)
3675 struct hda_gen_spec *spec = codec->spec;
3676 mutex_lock(&spec->pcm_mutex);
3677 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3678 call_pcm_playback_hook(hinfo, codec, substream,
3679 HDA_GEN_PCM_ACT_CLOSE);
3680 mutex_unlock(&spec->pcm_mutex);
3684 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3685 struct hda_codec *codec,
3686 struct snd_pcm_substream *substream)
3688 struct hda_gen_spec *spec = codec->spec;
3691 mutex_lock(&spec->pcm_mutex);
3692 if (!spec->indep_hp_enabled)
3695 spec->active_streams |= 1 << STREAM_INDEP_HP;
3696 call_pcm_playback_hook(hinfo, codec, substream,
3697 HDA_GEN_PCM_ACT_OPEN);
3698 mutex_unlock(&spec->pcm_mutex);
3702 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3703 struct hda_codec *codec,
3704 struct snd_pcm_substream *substream)
3706 struct hda_gen_spec *spec = codec->spec;
3707 mutex_lock(&spec->pcm_mutex);
3708 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3709 call_pcm_playback_hook(hinfo, codec, substream,
3710 HDA_GEN_PCM_ACT_CLOSE);
3711 mutex_unlock(&spec->pcm_mutex);
3715 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3716 struct hda_codec *codec,
3717 unsigned int stream_tag,
3718 unsigned int format,
3719 struct snd_pcm_substream *substream)
3721 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3722 call_pcm_playback_hook(hinfo, codec, substream,
3723 HDA_GEN_PCM_ACT_PREPARE);
3727 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3728 struct hda_codec *codec,
3729 struct snd_pcm_substream *substream)
3731 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3732 call_pcm_playback_hook(hinfo, codec, substream,
3733 HDA_GEN_PCM_ACT_CLEANUP);
3740 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3741 struct hda_codec *codec,
3742 struct snd_pcm_substream *substream)
3744 struct hda_gen_spec *spec = codec->spec;
3745 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3748 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3749 struct hda_codec *codec,
3750 unsigned int stream_tag,
3751 unsigned int format,
3752 struct snd_pcm_substream *substream)
3754 struct hda_gen_spec *spec = codec->spec;
3755 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3756 stream_tag, format, substream);
3759 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3760 struct hda_codec *codec,
3761 struct snd_pcm_substream *substream)
3763 struct hda_gen_spec *spec = codec->spec;
3764 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3767 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3768 struct hda_codec *codec,
3769 struct snd_pcm_substream *substream)
3771 struct hda_gen_spec *spec = codec->spec;
3772 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3778 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3779 struct hda_codec *codec,
3780 unsigned int stream_tag,
3781 unsigned int format,
3782 struct snd_pcm_substream *substream)
3784 struct hda_gen_spec *spec = codec->spec;
3786 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
3787 stream_tag, 0, format);
3791 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3792 struct hda_codec *codec,
3793 struct snd_pcm_substream *substream)
3795 struct hda_gen_spec *spec = codec->spec;
3797 snd_hda_codec_cleanup_stream(codec,
3798 spec->adc_nids[substream->number + 1]);
3804 static const struct hda_pcm_stream pcm_analog_playback = {
3808 /* NID is set in build_pcms */
3810 .open = playback_pcm_open,
3811 .close = playback_pcm_close,
3812 .prepare = playback_pcm_prepare,
3813 .cleanup = playback_pcm_cleanup
3817 static const struct hda_pcm_stream pcm_analog_capture = {
3821 /* NID is set in build_pcms */
3824 static const struct hda_pcm_stream pcm_analog_alt_playback = {
3828 /* NID is set in build_pcms */
3830 .open = alt_playback_pcm_open,
3831 .close = alt_playback_pcm_close,
3832 .prepare = alt_playback_pcm_prepare,
3833 .cleanup = alt_playback_pcm_cleanup
3837 static const struct hda_pcm_stream pcm_analog_alt_capture = {
3838 .substreams = 2, /* can be overridden */
3841 /* NID is set in build_pcms */
3843 .prepare = alt_capture_pcm_prepare,
3844 .cleanup = alt_capture_pcm_cleanup
3848 static const struct hda_pcm_stream pcm_digital_playback = {
3852 /* NID is set in build_pcms */
3854 .open = dig_playback_pcm_open,
3855 .close = dig_playback_pcm_close,
3856 .prepare = dig_playback_pcm_prepare,
3857 .cleanup = dig_playback_pcm_cleanup
3861 static const struct hda_pcm_stream pcm_digital_capture = {
3865 /* NID is set in build_pcms */
3868 /* Used by build_pcms to flag that a PCM has no playback stream */
3869 static const struct hda_pcm_stream pcm_null_stream = {
3876 * dynamic changing ADC PCM streams
3878 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3880 struct hda_gen_spec *spec = codec->spec;
3881 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3883 if (spec->cur_adc && spec->cur_adc != new_adc) {
3884 /* stream is running, let's swap the current ADC */
3885 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3886 spec->cur_adc = new_adc;
3887 snd_hda_codec_setup_stream(codec, new_adc,
3888 spec->cur_adc_stream_tag, 0,
3889 spec->cur_adc_format);
3895 /* analog capture with dynamic dual-adc changes */
3896 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3897 struct hda_codec *codec,
3898 unsigned int stream_tag,
3899 unsigned int format,
3900 struct snd_pcm_substream *substream)
3902 struct hda_gen_spec *spec = codec->spec;
3903 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3904 spec->cur_adc_stream_tag = stream_tag;
3905 spec->cur_adc_format = format;
3906 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3910 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3911 struct hda_codec *codec,
3912 struct snd_pcm_substream *substream)
3914 struct hda_gen_spec *spec = codec->spec;
3915 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3920 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3924 .nid = 0, /* fill later */
3926 .prepare = dyn_adc_capture_pcm_prepare,
3927 .cleanup = dyn_adc_capture_pcm_cleanup
3931 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3932 const char *chip_name)
3938 strlcpy(str, chip_name, len);
3940 /* drop non-alnum chars after a space */
3941 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3942 if (!isalnum(p[1])) {
3947 strlcat(str, sfx, len);
3950 /* build PCM streams based on the parsed results */
3951 int snd_hda_gen_build_pcms(struct hda_codec *codec)
3953 struct hda_gen_spec *spec = codec->spec;
3954 struct hda_pcm *info = spec->pcm_rec;
3955 const struct hda_pcm_stream *p;
3956 bool have_multi_adcs;
3958 codec->num_pcms = 1;
3959 codec->pcm_info = info;
3961 if (spec->no_analog)
3964 fill_pcm_stream_name(spec->stream_name_analog,
3965 sizeof(spec->stream_name_analog),
3966 " Analog", codec->chip_name);
3967 info->name = spec->stream_name_analog;
3969 if (spec->multiout.num_dacs > 0) {
3970 p = spec->stream_analog_playback;
3972 p = &pcm_analog_playback;
3973 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3974 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3975 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3976 spec->multiout.max_channels;
3977 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3978 spec->autocfg.line_outs == 2)
3979 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3982 if (spec->num_adc_nids) {
3983 p = spec->stream_analog_capture;
3985 if (spec->dyn_adc_switch)
3986 p = &dyn_adc_pcm_analog_capture;
3988 p = &pcm_analog_capture;
3990 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3991 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3995 /* SPDIF for stream index #1 */
3996 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
3997 fill_pcm_stream_name(spec->stream_name_digital,
3998 sizeof(spec->stream_name_digital),
3999 " Digital", codec->chip_name);
4000 codec->num_pcms = 2;
4001 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4002 info = spec->pcm_rec + 1;
4003 info->name = spec->stream_name_digital;
4004 if (spec->dig_out_type)
4005 info->pcm_type = spec->dig_out_type;
4007 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4008 if (spec->multiout.dig_out_nid) {
4009 p = spec->stream_digital_playback;
4011 p = &pcm_digital_playback;
4012 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4013 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4015 if (spec->dig_in_nid) {
4016 p = spec->stream_digital_capture;
4018 p = &pcm_digital_capture;
4019 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4020 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4024 if (spec->no_analog)
4027 /* If the use of more than one ADC is requested for the current
4028 * model, configure a second analog capture-only PCM.
4030 have_multi_adcs = (spec->num_adc_nids > 1) &&
4031 !spec->dyn_adc_switch && !spec->auto_mic;
4032 /* Additional Analaog capture for index #2 */
4033 if (spec->alt_dac_nid || have_multi_adcs) {
4034 codec->num_pcms = 3;
4035 info = spec->pcm_rec + 2;
4036 info->name = spec->stream_name_analog;
4037 if (spec->alt_dac_nid) {
4038 p = spec->stream_analog_alt_playback;
4040 p = &pcm_analog_alt_playback;
4041 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4042 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4045 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4047 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4049 if (have_multi_adcs) {
4050 p = spec->stream_analog_alt_capture;
4052 p = &pcm_analog_alt_capture;
4053 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4054 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4056 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4057 spec->num_adc_nids - 1;
4059 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4061 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4067 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4071 * Standard auto-parser initializations
4074 /* configure the given path as a proper output */
4075 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
4077 struct nid_path *path;
4080 path = snd_hda_get_path_from_idx(codec, path_idx);
4081 if (!path || !path->depth)
4083 pin = path->path[path->depth - 1];
4084 restore_pin_ctl(codec, pin);
4085 snd_hda_activate_path(codec, path, path->active, true);
4086 set_pin_eapd(codec, pin, path->active);
4089 /* initialize primary output paths */
4090 static void init_multi_out(struct hda_codec *codec)
4092 struct hda_gen_spec *spec = codec->spec;
4095 for (i = 0; i < spec->autocfg.line_outs; i++)
4096 set_output_and_unmute(codec, spec->out_paths[i]);
4100 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
4104 for (i = 0; i < num_outs; i++)
4105 set_output_and_unmute(codec, paths[i]);
4108 /* initialize hp and speaker paths */
4109 static void init_extra_out(struct hda_codec *codec)
4111 struct hda_gen_spec *spec = codec->spec;
4113 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
4114 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
4115 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4116 __init_extra_out(codec, spec->autocfg.speaker_outs,
4117 spec->speaker_paths);
4120 /* initialize multi-io paths */
4121 static void init_multi_io(struct hda_codec *codec)
4123 struct hda_gen_spec *spec = codec->spec;
4126 for (i = 0; i < spec->multi_ios; i++) {
4127 hda_nid_t pin = spec->multi_io[i].pin;
4128 struct nid_path *path;
4129 path = get_multiio_path(codec, i);
4132 if (!spec->multi_io[i].ctl_in)
4133 spec->multi_io[i].ctl_in =
4134 snd_hda_codec_get_pin_target(codec, pin);
4135 snd_hda_activate_path(codec, path, path->active, true);
4139 /* set up input pins and loopback paths */
4140 static void init_analog_input(struct hda_codec *codec)
4142 struct hda_gen_spec *spec = codec->spec;
4143 struct auto_pin_cfg *cfg = &spec->autocfg;
4146 for (i = 0; i < cfg->num_inputs; i++) {
4147 hda_nid_t nid = cfg->inputs[i].pin;
4148 if (is_input_pin(codec, nid))
4149 restore_pin_ctl(codec, nid);
4151 /* init loopback inputs */
4152 if (spec->mixer_nid) {
4153 struct nid_path *path;
4154 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
4156 snd_hda_activate_path(codec, path,
4157 path->active, false);
4162 /* initialize ADC paths */
4163 static void init_input_src(struct hda_codec *codec)
4165 struct hda_gen_spec *spec = codec->spec;
4166 struct hda_input_mux *imux = &spec->input_mux;
4167 struct nid_path *path;
4170 if (spec->dyn_adc_switch)
4173 nums = spec->num_adc_nids;
4175 for (c = 0; c < nums; c++) {
4176 for (i = 0; i < imux->num_items; i++) {
4177 path = get_input_path(codec, c, i);
4179 bool active = path->active;
4180 if (i == spec->cur_mux[c])
4182 snd_hda_activate_path(codec, path, active, false);
4187 if (spec->shared_mic_hp)
4188 update_shared_mic_hp(codec, spec->cur_mux[0]);
4190 if (spec->cap_sync_hook)
4191 spec->cap_sync_hook(codec);
4194 /* set right pin controls for digital I/O */
4195 static void init_digital(struct hda_codec *codec)
4197 struct hda_gen_spec *spec = codec->spec;
4201 for (i = 0; i < spec->autocfg.dig_outs; i++)
4202 set_output_and_unmute(codec, spec->digout_paths[i]);
4203 pin = spec->autocfg.dig_in_pin;
4205 struct nid_path *path;
4206 restore_pin_ctl(codec, pin);
4207 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4209 snd_hda_activate_path(codec, path, path->active, false);
4213 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4214 * invalid unsol tags by some reason
4216 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4220 for (i = 0; i < codec->init_pins.used; i++) {
4221 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4222 hda_nid_t nid = pin->nid;
4223 if (is_jack_detectable(codec, nid) &&
4224 !snd_hda_jack_tbl_get(codec, nid))
4225 snd_hda_codec_update_cache(codec, nid, 0,
4226 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4231 * initialize the generic spec;
4232 * this can be put as patch_ops.init function
4234 int snd_hda_gen_init(struct hda_codec *codec)
4236 struct hda_gen_spec *spec = codec->spec;
4238 if (spec->init_hook)
4239 spec->init_hook(codec);
4241 snd_hda_apply_verbs(codec);
4243 codec->cached_write = 1;
4245 init_multi_out(codec);
4246 init_extra_out(codec);
4247 init_multi_io(codec);
4248 init_analog_input(codec);
4249 init_input_src(codec);
4250 init_digital(codec);
4252 clear_unsol_on_unused_pins(codec);
4254 /* call init functions of standard auto-mute helpers */
4255 snd_hda_gen_hp_automute(codec, NULL);
4256 snd_hda_gen_line_automute(codec, NULL);
4257 snd_hda_gen_mic_autoswitch(codec, NULL);
4259 snd_hda_codec_flush_amp_cache(codec);
4260 snd_hda_codec_flush_cmd_cache(codec);
4262 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4263 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4265 hda_call_check_power_status(codec, 0x01);
4268 EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4271 * free the generic spec;
4272 * this can be put as patch_ops.free function
4274 void snd_hda_gen_free(struct hda_codec *codec)
4276 snd_hda_gen_spec_free(codec->spec);
4280 EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4284 * check the loopback power save state;
4285 * this can be put as patch_ops.check_power_status function
4287 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4289 struct hda_gen_spec *spec = codec->spec;
4290 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4292 EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4297 * the generic codec support
4300 static const struct hda_codec_ops generic_patch_ops = {
4301 .build_controls = snd_hda_gen_build_controls,
4302 .build_pcms = snd_hda_gen_build_pcms,
4303 .init = snd_hda_gen_init,
4304 .free = snd_hda_gen_free,
4305 .unsol_event = snd_hda_jack_unsol_event,
4307 .check_power_status = snd_hda_gen_check_power_status,
4311 int snd_hda_parse_generic_codec(struct hda_codec *codec)
4313 struct hda_gen_spec *spec;
4316 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4319 snd_hda_gen_spec_init(spec);
4322 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4326 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
4330 codec->patch_ops = generic_patch_ops;
4334 snd_hda_gen_free(codec);
4337 EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);