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 <linux/bitops.h>
30 #include <sound/core.h>
31 #include <sound/jack.h>
32 #include "hda_codec.h"
33 #include "hda_local.h"
34 #include "hda_auto_parser.h"
36 #include "hda_generic.h"
39 /* initialize hda_gen_spec struct */
40 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
42 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
43 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
44 mutex_init(&spec->pcm_mutex);
47 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
49 struct snd_kcontrol_new *
50 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
51 const struct snd_kcontrol_new *temp)
53 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
58 knew->name = kstrdup(name, GFP_KERNEL);
60 knew->name = kstrdup(knew->name, GFP_KERNEL);
65 EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
67 static void free_kctls(struct hda_gen_spec *spec)
69 if (spec->kctls.list) {
70 struct snd_kcontrol_new *kctl = spec->kctls.list;
72 for (i = 0; i < spec->kctls.used; i++)
75 snd_array_free(&spec->kctls);
78 void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
83 snd_array_free(&spec->paths);
85 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
90 static void parse_user_hints(struct hda_codec *codec)
92 struct hda_gen_spec *spec = codec->spec;
95 val = snd_hda_get_bool_hint(codec, "jack_detect");
97 codec->no_jack_detect = !val;
98 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
100 codec->inv_jack_detect = !!val;
101 val = snd_hda_get_bool_hint(codec, "trigger_sense");
103 codec->no_trigger_sense = !val;
104 val = snd_hda_get_bool_hint(codec, "inv_eapd");
106 codec->inv_eapd = !!val;
107 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
109 codec->pcm_format_first = !!val;
110 val = snd_hda_get_bool_hint(codec, "sticky_stream");
112 codec->no_sticky_stream = !val;
113 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
115 codec->spdif_status_reset = !!val;
116 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
118 codec->pin_amp_workaround = !!val;
119 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
121 codec->single_adc_amp = !!val;
123 val = snd_hda_get_bool_hint(codec, "auto_mute");
125 spec->suppress_auto_mute = !val;
126 val = snd_hda_get_bool_hint(codec, "auto_mic");
128 spec->suppress_auto_mic = !val;
129 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
131 spec->line_in_auto_switch = !!val;
132 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
134 spec->need_dac_fix = !!val;
135 val = snd_hda_get_bool_hint(codec, "primary_hp");
137 spec->no_primary_hp = !val;
138 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
140 spec->multi_cap_vol = !!val;
141 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
143 spec->inv_dmic_split = !!val;
144 val = snd_hda_get_bool_hint(codec, "indep_hp");
146 spec->indep_hp = !!val;
147 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
149 spec->add_stereo_mix_input = !!val;
150 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
152 spec->add_out_jack_modes = !!val;
153 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
155 spec->add_in_jack_modes = !!val;
157 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
158 spec->mixer_nid = val;
162 * pin control value accesses
165 #define update_pin_ctl(codec, pin, val) \
166 snd_hda_codec_update_cache(codec, pin, 0, \
167 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
169 /* restore the pinctl based on the cached value */
170 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
172 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
175 /* set the pinctl target value and write it if requested */
176 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
177 unsigned int val, bool do_write)
181 val = snd_hda_correct_pin_ctl(codec, pin, val);
182 snd_hda_codec_set_pin_target(codec, pin, val);
184 update_pin_ctl(codec, pin, val);
187 /* set pinctl target values for all given pins */
188 static void set_pin_targets(struct hda_codec *codec, int num_pins,
189 hda_nid_t *pins, unsigned int val)
192 for (i = 0; i < num_pins; i++)
193 set_pin_target(codec, pins[i], val, false);
200 /* return the position of NID in the list, or -1 if not found */
201 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
204 for (i = 0; i < nums; i++)
210 /* return true if the given NID is contained in the path */
211 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
213 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
216 static struct nid_path *get_nid_path(struct hda_codec *codec,
217 hda_nid_t from_nid, hda_nid_t to_nid,
220 struct hda_gen_spec *spec = codec->spec;
223 for (i = 0; i < spec->paths.used; i++) {
224 struct nid_path *path = snd_array_elem(&spec->paths, i);
225 if (path->depth <= 0)
227 if ((!from_nid || path->path[0] == from_nid) &&
228 (!to_nid || path->path[path->depth - 1] == to_nid)) {
230 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
231 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
238 /* get the path between the given NIDs;
239 * passing 0 to either @pin or @dac behaves as a wildcard
241 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
242 hda_nid_t from_nid, hda_nid_t to_nid)
244 return get_nid_path(codec, from_nid, to_nid, 0);
246 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
248 /* get the index number corresponding to the path instance;
249 * the index starts from 1, for easier checking the invalid value
251 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
253 struct hda_gen_spec *spec = codec->spec;
254 struct nid_path *array = spec->paths.list;
257 if (!spec->paths.used)
260 if (idx < 0 || idx >= spec->paths.used)
265 /* get the path instance corresponding to the given index number */
266 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
268 struct hda_gen_spec *spec = codec->spec;
270 if (idx <= 0 || idx > spec->paths.used)
272 return snd_array_elem(&spec->paths, idx - 1);
275 /* check whether the given DAC is already found in any existing paths */
276 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
278 struct hda_gen_spec *spec = codec->spec;
281 for (i = 0; i < spec->paths.used; i++) {
282 struct nid_path *path = snd_array_elem(&spec->paths, i);
283 if (path->path[0] == nid)
289 /* check whether the given two widgets can be connected */
290 static bool is_reachable_path(struct hda_codec *codec,
291 hda_nid_t from_nid, hda_nid_t to_nid)
293 if (!from_nid || !to_nid)
295 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
298 /* nid, dir and idx */
299 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
301 /* check whether the given ctl is already assigned in any path elements */
302 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
304 struct hda_gen_spec *spec = codec->spec;
307 val &= AMP_VAL_COMPARE_MASK;
308 for (i = 0; i < spec->paths.used; i++) {
309 struct nid_path *path = snd_array_elem(&spec->paths, i);
310 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
316 /* check whether a control with the given (nid, dir, idx) was assigned */
317 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
320 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
321 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
322 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
325 static void print_nid_path(const char *pfx, struct nid_path *path)
332 for (i = 0; i < path->depth; i++) {
334 sprintf(tmp, ":%02x", path->path[i]);
335 strlcat(buf, tmp, sizeof(buf));
337 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
340 /* called recursively */
341 static bool __parse_nid_path(struct hda_codec *codec,
342 hda_nid_t from_nid, hda_nid_t to_nid,
343 int anchor_nid, struct nid_path *path,
346 const hda_nid_t *conn;
349 if (to_nid == anchor_nid)
350 anchor_nid = 0; /* anchor passed */
351 else if (to_nid == (hda_nid_t)(-anchor_nid))
352 return false; /* hit the exclusive nid */
354 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
355 for (i = 0; i < nums; i++) {
356 if (conn[i] != from_nid) {
357 /* special case: when from_nid is 0,
358 * try to find an empty DAC
361 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
362 is_dac_already_used(codec, conn[i]))
365 /* anchor is not requested or already passed? */
369 if (depth >= MAX_NID_PATH_DEPTH)
371 for (i = 0; i < nums; i++) {
373 type = get_wcaps_type(get_wcaps(codec, conn[i]));
374 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
377 if (__parse_nid_path(codec, from_nid, conn[i],
378 anchor_nid, path, depth + 1))
384 path->path[path->depth] = conn[i];
385 path->idx[path->depth + 1] = i;
386 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
387 path->multi[path->depth + 1] = 1;
392 /* parse the widget path from the given nid to the target nid;
393 * when @from_nid is 0, try to find an empty DAC;
394 * when @anchor_nid is set to a positive value, only paths through the widget
395 * with the given value are evaluated.
396 * when @anchor_nid is set to a negative value, paths through the widget
397 * with the negative of given value are excluded, only other paths are chosen.
398 * when @anchor_nid is zero, no special handling about path selection.
400 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
401 hda_nid_t to_nid, int anchor_nid,
402 struct nid_path *path)
404 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
405 path->path[path->depth] = to_nid;
411 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
414 * parse the path between the given NIDs and add to the path list.
415 * if no valid path is found, return NULL
418 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
419 hda_nid_t to_nid, int anchor_nid)
421 struct hda_gen_spec *spec = codec->spec;
422 struct nid_path *path;
424 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
427 /* check whether the path has been already added */
428 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
432 path = snd_array_new(&spec->paths);
435 memset(path, 0, sizeof(*path));
436 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
442 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
444 /* clear the given path as invalid so that it won't be picked up later */
445 static void invalidate_nid_path(struct hda_codec *codec, int idx)
447 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
450 memset(path, 0, sizeof(*path));
453 /* look for an empty DAC slot */
454 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
457 struct hda_gen_spec *spec = codec->spec;
461 for (i = 0; i < spec->num_all_dacs; i++) {
462 hda_nid_t nid = spec->all_dacs[i];
463 if (!nid || is_dac_already_used(codec, nid))
465 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
466 if (is_digital != cap_digital)
468 if (is_reachable_path(codec, nid, pin))
474 /* replace the channels in the composed amp value with the given number */
475 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
477 val &= ~(0x3U << 16);
482 /* check whether the widget has the given amp capability for the direction */
483 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
484 int dir, unsigned int bits)
488 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
489 if (query_amp_caps(codec, nid, dir) & bits)
494 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
495 hda_nid_t nid2, int dir)
497 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
498 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
499 return (query_amp_caps(codec, nid1, dir) ==
500 query_amp_caps(codec, nid2, dir));
503 #define nid_has_mute(codec, nid, dir) \
504 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
505 #define nid_has_volume(codec, nid, dir) \
506 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
508 /* look for a widget suitable for assigning a mute switch in the path */
509 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
510 struct nid_path *path)
514 for (i = path->depth - 1; i >= 0; i--) {
515 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
516 return path->path[i];
517 if (i != path->depth - 1 && i != 0 &&
518 nid_has_mute(codec, path->path[i], HDA_INPUT))
519 return path->path[i];
524 /* look for a widget suitable for assigning a volume ctl in the path */
525 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
526 struct nid_path *path)
530 for (i = path->depth - 1; i >= 0; i--) {
531 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
532 return path->path[i];
538 * path activation / deactivation
541 /* can have the amp-in capability? */
542 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
544 hda_nid_t nid = path->path[idx];
545 unsigned int caps = get_wcaps(codec, nid);
546 unsigned int type = get_wcaps_type(caps);
548 if (!(caps & AC_WCAP_IN_AMP))
550 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
555 /* can have the amp-out capability? */
556 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
558 hda_nid_t nid = path->path[idx];
559 unsigned int caps = get_wcaps(codec, nid);
560 unsigned int type = get_wcaps_type(caps);
562 if (!(caps & AC_WCAP_OUT_AMP))
564 if (type == AC_WID_PIN && !idx) /* only for output pins */
569 /* check whether the given (nid,dir,idx) is active */
570 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
571 unsigned int idx, unsigned int dir)
573 struct hda_gen_spec *spec = codec->spec;
576 for (n = 0; n < spec->paths.used; n++) {
577 struct nid_path *path = snd_array_elem(&spec->paths, n);
580 for (i = 0; i < path->depth; i++) {
581 if (path->path[i] == nid) {
582 if (dir == HDA_OUTPUT || path->idx[i] == idx)
591 /* get the default amp value for the target state */
592 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
593 int dir, bool enable)
596 unsigned int val = 0;
598 caps = query_amp_caps(codec, nid, dir);
599 if (caps & AC_AMPCAP_NUM_STEPS) {
602 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
604 if (caps & AC_AMPCAP_MUTE) {
611 /* initialize the amp value (only at the first time) */
612 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
614 int val = get_amp_val_to_activate(codec, nid, dir, false);
615 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
618 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
619 int idx, bool enable)
622 if (is_ctl_associated(codec, nid, dir, idx) ||
623 (!enable && is_active_nid(codec, nid, dir, idx)))
625 val = get_amp_val_to_activate(codec, nid, dir, enable);
626 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
629 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
632 hda_nid_t nid = path->path[i];
633 init_amp(codec, nid, HDA_OUTPUT, 0);
634 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
637 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
638 int i, bool enable, bool add_aamix)
640 struct hda_gen_spec *spec = codec->spec;
641 const hda_nid_t *conn;
644 hda_nid_t nid = path->path[i];
646 nums = snd_hda_get_conn_list(codec, nid, &conn);
647 type = get_wcaps_type(get_wcaps(codec, nid));
648 if (type == AC_WID_PIN ||
649 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
655 for (n = 0; n < nums; n++)
656 init_amp(codec, nid, HDA_INPUT, n);
658 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
661 /* here is a little bit tricky in comparison with activate_amp_out();
662 * when aa-mixer is available, we need to enable the path as well
664 for (n = 0; n < nums; n++) {
665 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
667 activate_amp(codec, nid, HDA_INPUT, n, enable);
671 /* activate or deactivate the given path
672 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
674 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
675 bool enable, bool add_aamix)
680 path->active = false;
682 for (i = path->depth - 1; i >= 0; i--) {
683 if (enable && path->multi[i])
684 snd_hda_codec_write_cache(codec, path->path[i], 0,
685 AC_VERB_SET_CONNECT_SEL,
687 if (has_amp_in(codec, path, i))
688 activate_amp_in(codec, path, i, enable, add_aamix);
689 if (has_amp_out(codec, path, i))
690 activate_amp_out(codec, path, i, enable);
696 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
698 /* turn on/off EAPD on the given pin */
699 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
701 struct hda_gen_spec *spec = codec->spec;
702 if (spec->own_eapd_ctl ||
703 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
707 snd_hda_codec_update_cache(codec, pin, 0,
708 AC_VERB_SET_EAPD_BTLENABLE,
709 enable ? 0x02 : 0x00);
714 * Helper functions for creating mixer ctl elements
722 static const struct snd_kcontrol_new control_templates[] = {
723 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
724 HDA_CODEC_MUTE(NULL, 0, 0, 0),
725 HDA_BIND_MUTE(NULL, 0, 0, 0),
728 /* add dynamic controls from template */
729 static int add_control(struct hda_gen_spec *spec, int type, const char *name,
730 int cidx, unsigned long val)
732 struct snd_kcontrol_new *knew;
734 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
738 if (get_amp_nid_(val))
739 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
740 knew->private_value = val;
744 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
745 const char *pfx, const char *dir,
746 const char *sfx, int cidx, unsigned long val)
749 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
750 return add_control(spec, type, name, cidx, val);
753 #define add_pb_vol_ctrl(spec, type, pfx, val) \
754 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
755 #define add_pb_sw_ctrl(spec, type, pfx, val) \
756 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
757 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
758 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
759 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
760 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
762 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
763 unsigned int chs, struct nid_path *path)
768 val = path->ctls[NID_PATH_VOL_CTL];
771 val = amp_val_replace_channels(val, chs);
772 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
775 /* return the channel bits suitable for the given path->ctls[] */
776 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
779 int chs = 1; /* mono (left only) */
781 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
782 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
783 chs = 3; /* stereo */
788 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
789 struct nid_path *path)
791 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
792 return add_vol_ctl(codec, pfx, cidx, chs, path);
795 /* create a mute-switch for the given mixer widget;
796 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
798 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
799 unsigned int chs, struct nid_path *path)
802 int type = HDA_CTL_WIDGET_MUTE;
806 val = path->ctls[NID_PATH_MUTE_CTL];
809 val = amp_val_replace_channels(val, chs);
810 if (get_amp_direction_(val) == HDA_INPUT) {
811 hda_nid_t nid = get_amp_nid_(val);
812 int nums = snd_hda_get_num_conns(codec, nid);
814 type = HDA_CTL_BIND_MUTE;
818 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
821 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
822 int cidx, struct nid_path *path)
824 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
825 return add_sw_ctl(codec, pfx, cidx, chs, path);
828 /* any ctl assigned to the path with the given index? */
829 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
831 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
832 return path && path->ctls[ctl_type];
835 static const char * const channel_name[4] = {
836 "Front", "Surround", "CLFE", "Side"
839 /* give some appropriate ctl name prefix for the given line out channel */
840 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
841 int *index, int ctl_type)
843 struct hda_gen_spec *spec = codec->spec;
844 struct auto_pin_cfg *cfg = &spec->autocfg;
847 if (cfg->line_outs == 1 && !spec->multi_ios &&
848 !cfg->hp_outs && !cfg->speaker_outs)
849 return spec->vmaster_mute.hook ? "PCM" : "Master";
851 /* if there is really a single DAC used in the whole output paths,
852 * use it master (or "PCM" if a vmaster hook is present)
854 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
855 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
856 return spec->vmaster_mute.hook ? "PCM" : "Master";
858 /* multi-io channels */
859 if (ch >= cfg->line_outs)
860 return channel_name[ch];
862 switch (cfg->line_out_type) {
863 case AUTO_PIN_SPEAKER_OUT:
864 /* if the primary channel vol/mute is shared with HP volume,
865 * don't name it as Speaker
867 if (!ch && cfg->hp_outs &&
868 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
870 if (cfg->line_outs == 1)
872 if (cfg->line_outs == 2)
873 return ch ? "Bass Speaker" : "Speaker";
875 case AUTO_PIN_HP_OUT:
876 /* if the primary channel vol/mute is shared with spk volume,
877 * don't name it as Headphone
879 if (!ch && cfg->speaker_outs &&
880 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
882 /* for multi-io case, only the primary out */
883 if (ch && spec->multi_ios)
889 /* for a single channel output, we don't have to name the channel */
890 if (cfg->line_outs == 1 && !spec->multi_ios)
893 if (ch >= ARRAY_SIZE(channel_name)) {
898 return channel_name[ch];
905 /* badness definition */
907 /* No primary DAC is found for the main output */
908 BAD_NO_PRIMARY_DAC = 0x10000,
909 /* No DAC is found for the extra output */
911 /* No possible multi-ios */
912 BAD_MULTI_IO = 0x103,
913 /* No individual DAC for extra output */
914 BAD_NO_EXTRA_DAC = 0x102,
915 /* No individual DAC for extra surrounds */
916 BAD_NO_EXTRA_SURR_DAC = 0x101,
917 /* Primary DAC shared with main surrounds */
918 BAD_SHARED_SURROUND = 0x100,
919 /* Primary DAC shared with main CLFE */
920 BAD_SHARED_CLFE = 0x10,
921 /* Primary DAC shared with extra surrounds */
922 BAD_SHARED_EXTRA_SURROUND = 0x10,
923 /* Volume widget is shared */
924 BAD_SHARED_VOL = 0x10,
927 /* look for widgets in the given path which are appropriate for
928 * volume and mute controls, and assign the values to ctls[].
930 * When no appropriate widget is found in the path, the badness value
931 * is incremented depending on the situation. The function returns the
932 * total badness for both volume and mute controls.
934 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
941 return BAD_SHARED_VOL * 2;
943 if (path->ctls[NID_PATH_VOL_CTL] ||
944 path->ctls[NID_PATH_MUTE_CTL])
945 return 0; /* already evaluated */
947 nid = look_for_out_vol_nid(codec, path);
949 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
950 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
951 badness += BAD_SHARED_VOL;
953 path->ctls[NID_PATH_VOL_CTL] = val;
955 badness += BAD_SHARED_VOL;
956 nid = look_for_out_mute_nid(codec, path);
958 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
959 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
960 nid_has_mute(codec, nid, HDA_OUTPUT))
961 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
963 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
964 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
965 badness += BAD_SHARED_VOL;
967 path->ctls[NID_PATH_MUTE_CTL] = val;
969 badness += BAD_SHARED_VOL;
973 struct badness_table {
974 int no_primary_dac; /* no primary DAC */
975 int no_dac; /* no secondary DACs */
976 int shared_primary; /* primary DAC is shared with main output */
977 int shared_surr; /* secondary DAC shared with main or primary */
978 int shared_clfe; /* third DAC shared with main or primary */
979 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
982 static struct badness_table main_out_badness = {
983 .no_primary_dac = BAD_NO_PRIMARY_DAC,
984 .no_dac = BAD_NO_DAC,
985 .shared_primary = BAD_NO_PRIMARY_DAC,
986 .shared_surr = BAD_SHARED_SURROUND,
987 .shared_clfe = BAD_SHARED_CLFE,
988 .shared_surr_main = BAD_SHARED_SURROUND,
991 static struct badness_table extra_out_badness = {
992 .no_primary_dac = BAD_NO_DAC,
993 .no_dac = BAD_NO_DAC,
994 .shared_primary = BAD_NO_EXTRA_DAC,
995 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
996 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
997 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1000 /* get the DAC of the primary output corresponding to the given array index */
1001 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1003 struct hda_gen_spec *spec = codec->spec;
1004 struct auto_pin_cfg *cfg = &spec->autocfg;
1006 if (cfg->line_outs > idx)
1007 return spec->private_dac_nids[idx];
1008 idx -= cfg->line_outs;
1009 if (spec->multi_ios > idx)
1010 return spec->multi_io[idx].dac;
1014 /* return the DAC if it's reachable, otherwise zero */
1015 static inline hda_nid_t try_dac(struct hda_codec *codec,
1016 hda_nid_t dac, hda_nid_t pin)
1018 return is_reachable_path(codec, dac, pin) ? dac : 0;
1021 /* try to assign DACs to pins and return the resultant badness */
1022 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1023 const hda_nid_t *pins, hda_nid_t *dacs,
1025 const struct badness_table *bad)
1027 struct hda_gen_spec *spec = codec->spec;
1035 for (i = 0; i < num_outs; i++) {
1036 struct nid_path *path;
1037 hda_nid_t pin = pins[i];
1039 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1041 badness += assign_out_path_ctls(codec, path);
1045 dacs[i] = look_for_dac(codec, pin, false);
1046 if (!dacs[i] && !i) {
1047 /* try to steal the DAC of surrounds for the front */
1048 for (j = 1; j < num_outs; j++) {
1049 if (is_reachable_path(codec, dacs[j], pin)) {
1052 invalidate_nid_path(codec, path_idx[j]);
1061 dac = try_dac(codec, get_primary_out(codec, i), pin);
1063 dac = try_dac(codec, dacs[0], pin);
1065 dac = try_dac(codec, get_primary_out(codec, i), pin);
1068 badness += bad->shared_primary;
1070 badness += bad->shared_surr;
1072 badness += bad->shared_clfe;
1073 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1074 dac = spec->private_dac_nids[0];
1075 badness += bad->shared_surr_main;
1077 badness += bad->no_primary_dac;
1079 badness += bad->no_dac;
1081 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1082 if (!path && !i && spec->mixer_nid) {
1083 /* try with aamix */
1084 path = snd_hda_add_new_path(codec, dac, pin, 0);
1089 print_nid_path("output", path);
1090 path->active = true;
1091 path_idx[i] = snd_hda_get_path_idx(codec, path);
1092 badness += assign_out_path_ctls(codec, path);
1099 /* return NID if the given pin has only a single connection to a certain DAC */
1100 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1102 struct hda_gen_spec *spec = codec->spec;
1104 hda_nid_t nid_found = 0;
1106 for (i = 0; i < spec->num_all_dacs; i++) {
1107 hda_nid_t nid = spec->all_dacs[i];
1108 if (!nid || is_dac_already_used(codec, nid))
1110 if (is_reachable_path(codec, nid, pin)) {
1119 /* check whether the given pin can be a multi-io pin */
1120 static bool can_be_multiio_pin(struct hda_codec *codec,
1121 unsigned int location, hda_nid_t nid)
1123 unsigned int defcfg, caps;
1125 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1126 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1128 if (location && get_defcfg_location(defcfg) != location)
1130 caps = snd_hda_query_pin_caps(codec, nid);
1131 if (!(caps & AC_PINCAP_OUT))
1136 /* count the number of input pins that are capable to be multi-io */
1137 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1139 struct hda_gen_spec *spec = codec->spec;
1140 struct auto_pin_cfg *cfg = &spec->autocfg;
1141 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1142 unsigned int location = get_defcfg_location(defcfg);
1146 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1147 for (i = 0; i < cfg->num_inputs; i++) {
1148 if (cfg->inputs[i].type != type)
1150 if (can_be_multiio_pin(codec, location,
1151 cfg->inputs[i].pin))
1161 * When hardwired is set, try to fill ony hardwired pins, and returns
1162 * zero if any pins are filled, non-zero if nothing found.
1163 * When hardwired is off, try to fill possible input pins, and returns
1164 * the badness value.
1166 static int fill_multi_ios(struct hda_codec *codec,
1167 hda_nid_t reference_pin,
1170 struct hda_gen_spec *spec = codec->spec;
1171 struct auto_pin_cfg *cfg = &spec->autocfg;
1172 int type, i, j, num_pins, old_pins;
1173 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1174 unsigned int location = get_defcfg_location(defcfg);
1176 struct nid_path *path;
1178 old_pins = spec->multi_ios;
1182 num_pins = count_multiio_pins(codec, reference_pin);
1186 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1187 for (i = 0; i < cfg->num_inputs; i++) {
1188 hda_nid_t nid = cfg->inputs[i].pin;
1191 if (cfg->inputs[i].type != type)
1193 if (!can_be_multiio_pin(codec, location, nid))
1195 for (j = 0; j < spec->multi_ios; j++) {
1196 if (nid == spec->multi_io[j].pin)
1199 if (j < spec->multi_ios)
1203 dac = get_dac_if_single(codec, nid);
1205 dac = look_for_dac(codec, nid, false);
1210 path = snd_hda_add_new_path(codec, dac, nid,
1216 print_nid_path("multiio", path);
1217 spec->multi_io[spec->multi_ios].pin = nid;
1218 spec->multi_io[spec->multi_ios].dac = dac;
1219 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1220 snd_hda_get_path_idx(codec, path);
1222 if (spec->multi_ios >= 2)
1228 badness = BAD_MULTI_IO;
1229 if (old_pins == spec->multi_ios) {
1231 return 1; /* nothing found */
1233 return badness; /* no badness if nothing found */
1235 if (!hardwired && spec->multi_ios < 2) {
1236 /* cancel newly assigned paths */
1237 spec->paths.used -= spec->multi_ios - old_pins;
1238 spec->multi_ios = old_pins;
1242 /* assign volume and mute controls */
1243 for (i = old_pins; i < spec->multi_ios; i++) {
1244 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1245 badness += assign_out_path_ctls(codec, path);
1251 /* map DACs for all pins in the list if they are single connections */
1252 static bool map_singles(struct hda_codec *codec, int outs,
1253 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1255 struct hda_gen_spec *spec = codec->spec;
1258 for (i = 0; i < outs; i++) {
1259 struct nid_path *path;
1263 dac = get_dac_if_single(codec, pins[i]);
1266 path = snd_hda_add_new_path(codec, dac, pins[i],
1268 if (!path && !i && spec->mixer_nid)
1269 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1273 print_nid_path("output", path);
1274 path->active = true;
1275 path_idx[i] = snd_hda_get_path_idx(codec, path);
1281 /* create a new path including aamix if available, and return its index */
1282 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1284 struct hda_gen_spec *spec = codec->spec;
1285 struct nid_path *path;
1287 path = snd_hda_get_path_from_idx(codec, path_idx);
1288 if (!path || !path->depth ||
1289 is_nid_contained(path, spec->mixer_nid))
1291 path = snd_hda_add_new_path(codec, path->path[0],
1292 path->path[path->depth - 1],
1296 print_nid_path("output-aamix", path);
1297 path->active = false; /* unused as default */
1298 return snd_hda_get_path_idx(codec, path);
1301 /* fill the empty entries in the dac array for speaker/hp with the
1302 * shared dac pointed by the paths
1304 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1305 hda_nid_t *dacs, int *path_idx)
1307 struct nid_path *path;
1310 for (i = 0; i < num_outs; i++) {
1313 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1316 dacs[i] = path->path[0];
1320 /* fill in the dac_nids table from the parsed pin configuration */
1321 static int fill_and_eval_dacs(struct hda_codec *codec,
1322 bool fill_hardwired,
1323 bool fill_mio_first)
1325 struct hda_gen_spec *spec = codec->spec;
1326 struct auto_pin_cfg *cfg = &spec->autocfg;
1327 int i, err, badness;
1330 /* set num_dacs once to full for look_for_dac() */
1331 spec->multiout.num_dacs = cfg->line_outs;
1332 spec->multiout.dac_nids = spec->private_dac_nids;
1333 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1334 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1335 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1336 spec->multi_ios = 0;
1337 snd_array_free(&spec->paths);
1339 /* clear path indices */
1340 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1341 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1342 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1343 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1344 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1345 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1346 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1347 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1351 /* fill hard-wired DACs first */
1352 if (fill_hardwired) {
1355 mapped = map_singles(codec, cfg->line_outs,
1357 spec->private_dac_nids,
1359 mapped |= map_singles(codec, cfg->hp_outs,
1361 spec->multiout.hp_out_nid,
1363 mapped |= map_singles(codec, cfg->speaker_outs,
1365 spec->multiout.extra_out_nid,
1366 spec->speaker_paths);
1367 if (fill_mio_first && cfg->line_outs == 1 &&
1368 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1369 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1376 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1377 spec->private_dac_nids, spec->out_paths,
1380 if (fill_mio_first &&
1381 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1382 /* try to fill multi-io first */
1383 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1386 /* we don't count badness at this stage yet */
1389 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1390 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1391 spec->multiout.hp_out_nid,
1393 &extra_out_badness);
1398 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1399 err = try_assign_dacs(codec, cfg->speaker_outs,
1401 spec->multiout.extra_out_nid,
1402 spec->speaker_paths,
1403 &extra_out_badness);
1408 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1409 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1415 if (spec->mixer_nid) {
1416 spec->aamix_out_paths[0] =
1417 check_aamix_out_path(codec, spec->out_paths[0]);
1418 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1419 spec->aamix_out_paths[1] =
1420 check_aamix_out_path(codec, spec->hp_paths[0]);
1421 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1422 spec->aamix_out_paths[2] =
1423 check_aamix_out_path(codec, spec->speaker_paths[0]);
1426 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1427 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1428 spec->multi_ios = 1; /* give badness */
1430 /* re-count num_dacs and squash invalid entries */
1431 spec->multiout.num_dacs = 0;
1432 for (i = 0; i < cfg->line_outs; i++) {
1433 if (spec->private_dac_nids[i])
1434 spec->multiout.num_dacs++;
1436 memmove(spec->private_dac_nids + i,
1437 spec->private_dac_nids + i + 1,
1438 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1439 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1443 spec->ext_channel_count = spec->min_channel_count =
1444 spec->multiout.num_dacs * 2;
1446 if (spec->multi_ios == 2) {
1447 for (i = 0; i < 2; i++)
1448 spec->private_dac_nids[spec->multiout.num_dacs++] =
1449 spec->multi_io[i].dac;
1450 } else if (spec->multi_ios) {
1451 spec->multi_ios = 0;
1452 badness += BAD_MULTI_IO;
1455 /* re-fill the shared DAC for speaker / headphone */
1456 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1457 refill_shared_dacs(codec, cfg->hp_outs,
1458 spec->multiout.hp_out_nid,
1460 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1461 refill_shared_dacs(codec, cfg->speaker_outs,
1462 spec->multiout.extra_out_nid,
1463 spec->speaker_paths);
1465 /* set initial pinctl targets */
1466 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1470 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1471 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1472 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1473 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1474 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1475 set_pin_targets(codec, cfg->speaker_outs,
1476 cfg->speaker_pins, val);
1482 #define DEBUG_BADNESS
1484 #ifdef DEBUG_BADNESS
1485 #define debug_badness snd_printdd
1487 #define debug_badness(...)
1490 static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1492 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1493 cfg->line_out_pins[0], cfg->line_out_pins[1],
1494 cfg->line_out_pins[2], cfg->line_out_pins[3],
1495 spec->multiout.dac_nids[0],
1496 spec->multiout.dac_nids[1],
1497 spec->multiout.dac_nids[2],
1498 spec->multiout.dac_nids[3]);
1499 if (spec->multi_ios > 0)
1500 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1502 spec->multi_io[0].pin, spec->multi_io[1].pin,
1503 spec->multi_io[0].dac, spec->multi_io[1].dac);
1504 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1505 cfg->hp_pins[0], cfg->hp_pins[1],
1506 cfg->hp_pins[2], cfg->hp_pins[3],
1507 spec->multiout.hp_out_nid[0],
1508 spec->multiout.hp_out_nid[1],
1509 spec->multiout.hp_out_nid[2],
1510 spec->multiout.hp_out_nid[3]);
1511 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1512 cfg->speaker_pins[0], cfg->speaker_pins[1],
1513 cfg->speaker_pins[2], cfg->speaker_pins[3],
1514 spec->multiout.extra_out_nid[0],
1515 spec->multiout.extra_out_nid[1],
1516 spec->multiout.extra_out_nid[2],
1517 spec->multiout.extra_out_nid[3]);
1520 /* find all available DACs of the codec */
1521 static void fill_all_dac_nids(struct hda_codec *codec)
1523 struct hda_gen_spec *spec = codec->spec;
1525 hda_nid_t nid = codec->start_nid;
1527 spec->num_all_dacs = 0;
1528 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1529 for (i = 0; i < codec->num_nodes; i++, nid++) {
1530 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1532 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1533 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1536 spec->all_dacs[spec->num_all_dacs++] = nid;
1540 static int parse_output_paths(struct hda_codec *codec)
1542 struct hda_gen_spec *spec = codec->spec;
1543 struct auto_pin_cfg *cfg = &spec->autocfg;
1544 struct auto_pin_cfg *best_cfg;
1545 int best_badness = INT_MAX;
1547 bool fill_hardwired = true, fill_mio_first = true;
1548 bool best_wired = true, best_mio = true;
1549 bool hp_spk_swapped = false;
1551 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1557 badness = fill_and_eval_dacs(codec, fill_hardwired,
1563 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1564 cfg->line_out_type, fill_hardwired, fill_mio_first,
1566 debug_show_configs(spec, cfg);
1567 if (badness < best_badness) {
1568 best_badness = badness;
1570 best_wired = fill_hardwired;
1571 best_mio = fill_mio_first;
1575 fill_mio_first = !fill_mio_first;
1576 if (!fill_mio_first)
1578 fill_hardwired = !fill_hardwired;
1579 if (!fill_hardwired)
1583 hp_spk_swapped = true;
1584 if (cfg->speaker_outs > 0 &&
1585 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1586 cfg->hp_outs = cfg->line_outs;
1587 memcpy(cfg->hp_pins, cfg->line_out_pins,
1588 sizeof(cfg->hp_pins));
1589 cfg->line_outs = cfg->speaker_outs;
1590 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1591 sizeof(cfg->speaker_pins));
1592 cfg->speaker_outs = 0;
1593 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1594 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1595 fill_hardwired = true;
1598 if (cfg->hp_outs > 0 &&
1599 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1600 cfg->speaker_outs = cfg->line_outs;
1601 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1602 sizeof(cfg->speaker_pins));
1603 cfg->line_outs = cfg->hp_outs;
1604 memcpy(cfg->line_out_pins, cfg->hp_pins,
1605 sizeof(cfg->hp_pins));
1607 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1608 cfg->line_out_type = AUTO_PIN_HP_OUT;
1609 fill_hardwired = true;
1616 debug_badness("==> restoring best_cfg\n");
1618 fill_and_eval_dacs(codec, best_wired, best_mio);
1620 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1621 cfg->line_out_type, best_wired, best_mio);
1622 debug_show_configs(spec, cfg);
1624 if (cfg->line_out_pins[0]) {
1625 struct nid_path *path;
1626 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1628 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1629 if (spec->vmaster_nid)
1630 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1631 HDA_OUTPUT, spec->vmaster_tlv);
1638 /* add playback controls from the parsed DAC table */
1639 static int create_multi_out_ctls(struct hda_codec *codec,
1640 const struct auto_pin_cfg *cfg)
1642 struct hda_gen_spec *spec = codec->spec;
1643 int i, err, noutputs;
1645 noutputs = cfg->line_outs;
1646 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1647 noutputs += spec->multi_ios;
1649 for (i = 0; i < noutputs; i++) {
1652 struct nid_path *path;
1654 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1658 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
1659 if (!name || !strcmp(name, "CLFE")) {
1661 err = add_vol_ctl(codec, "Center", 0, 1, path);
1664 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1668 err = add_stereo_vol(codec, name, index, path);
1673 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1674 if (!name || !strcmp(name, "CLFE")) {
1675 err = add_sw_ctl(codec, "Center", 0, 1, path);
1678 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1682 err = add_stereo_sw(codec, name, index, path);
1690 static int create_extra_out(struct hda_codec *codec, int path_idx,
1691 const char *pfx, int cidx)
1693 struct nid_path *path;
1696 path = snd_hda_get_path_from_idx(codec, path_idx);
1699 err = add_stereo_vol(codec, pfx, cidx, path);
1702 err = add_stereo_sw(codec, pfx, cidx, path);
1708 /* add playback controls for speaker and HP outputs */
1709 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1710 const int *paths, const char *pfx)
1714 for (i = 0; i < num_pins; i++) {
1719 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1720 name = "Bass Speaker";
1721 else if (num_pins >= 3) {
1722 snprintf(tmp, sizeof(tmp), "%s %s",
1723 pfx, channel_name[i]);
1729 err = create_extra_out(codec, paths[i], name, idx);
1736 static int create_hp_out_ctls(struct hda_codec *codec)
1738 struct hda_gen_spec *spec = codec->spec;
1739 return create_extra_outs(codec, spec->autocfg.hp_outs,
1744 static int create_speaker_out_ctls(struct hda_codec *codec)
1746 struct hda_gen_spec *spec = codec->spec;
1747 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1748 spec->speaker_paths,
1753 * independent HP controls
1756 static int indep_hp_info(struct snd_kcontrol *kcontrol,
1757 struct snd_ctl_elem_info *uinfo)
1759 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1762 static int indep_hp_get(struct snd_kcontrol *kcontrol,
1763 struct snd_ctl_elem_value *ucontrol)
1765 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1766 struct hda_gen_spec *spec = codec->spec;
1767 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1771 static int indep_hp_put(struct snd_kcontrol *kcontrol,
1772 struct snd_ctl_elem_value *ucontrol)
1774 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1775 struct hda_gen_spec *spec = codec->spec;
1776 unsigned int select = ucontrol->value.enumerated.item[0];
1779 mutex_lock(&spec->pcm_mutex);
1780 if (spec->active_streams) {
1785 if (spec->indep_hp_enabled != select) {
1786 spec->indep_hp_enabled = select;
1787 if (spec->indep_hp_enabled)
1788 spec->multiout.hp_out_nid[0] = 0;
1790 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1794 mutex_unlock(&spec->pcm_mutex);
1798 static const struct snd_kcontrol_new indep_hp_ctl = {
1799 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1800 .name = "Independent HP",
1801 .info = indep_hp_info,
1802 .get = indep_hp_get,
1803 .put = indep_hp_put,
1807 static int create_indep_hp_ctls(struct hda_codec *codec)
1809 struct hda_gen_spec *spec = codec->spec;
1811 if (!spec->indep_hp)
1813 if (!spec->multiout.hp_out_nid[0]) {
1818 spec->indep_hp_enabled = false;
1819 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1820 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1826 * channel mode enum control
1829 static int ch_mode_info(struct snd_kcontrol *kcontrol,
1830 struct snd_ctl_elem_info *uinfo)
1832 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1833 struct hda_gen_spec *spec = codec->spec;
1836 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1838 uinfo->value.enumerated.items = spec->multi_ios + 1;
1839 if (uinfo->value.enumerated.item > spec->multi_ios)
1840 uinfo->value.enumerated.item = spec->multi_ios;
1841 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1842 sprintf(uinfo->value.enumerated.name, "%dch", chs);
1846 static int ch_mode_get(struct snd_kcontrol *kcontrol,
1847 struct snd_ctl_elem_value *ucontrol)
1849 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1850 struct hda_gen_spec *spec = codec->spec;
1851 ucontrol->value.enumerated.item[0] =
1852 (spec->ext_channel_count - spec->min_channel_count) / 2;
1856 static inline struct nid_path *
1857 get_multiio_path(struct hda_codec *codec, int idx)
1859 struct hda_gen_spec *spec = codec->spec;
1860 return snd_hda_get_path_from_idx(codec,
1861 spec->out_paths[spec->autocfg.line_outs + idx]);
1864 static void update_automute_all(struct hda_codec *codec);
1866 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1868 struct hda_gen_spec *spec = codec->spec;
1869 hda_nid_t nid = spec->multi_io[idx].pin;
1870 struct nid_path *path;
1872 path = get_multiio_path(codec, idx);
1876 if (path->active == output)
1880 set_pin_target(codec, nid, PIN_OUT, true);
1881 snd_hda_activate_path(codec, path, true, true);
1882 set_pin_eapd(codec, nid, true);
1884 set_pin_eapd(codec, nid, false);
1885 snd_hda_activate_path(codec, path, false, true);
1886 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
1889 /* update jack retasking in case it modifies any of them */
1890 update_automute_all(codec);
1895 static int ch_mode_put(struct snd_kcontrol *kcontrol,
1896 struct snd_ctl_elem_value *ucontrol)
1898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1899 struct hda_gen_spec *spec = codec->spec;
1902 ch = ucontrol->value.enumerated.item[0];
1903 if (ch < 0 || ch > spec->multi_ios)
1905 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
1907 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
1908 for (i = 0; i < spec->multi_ios; i++)
1909 set_multi_io(codec, i, i < ch);
1910 spec->multiout.max_channels = max(spec->ext_channel_count,
1911 spec->const_channel_count);
1912 if (spec->need_dac_fix)
1913 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1917 static const struct snd_kcontrol_new channel_mode_enum = {
1918 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1919 .name = "Channel Mode",
1920 .info = ch_mode_info,
1925 static int create_multi_channel_mode(struct hda_codec *codec)
1927 struct hda_gen_spec *spec = codec->spec;
1929 if (spec->multi_ios > 0) {
1930 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
1937 * aamix loopback enable/disable switch
1940 #define loopback_mixing_info indep_hp_info
1942 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1943 struct snd_ctl_elem_value *ucontrol)
1945 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1946 struct hda_gen_spec *spec = codec->spec;
1947 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1951 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1952 int nomix_path_idx, int mix_path_idx)
1954 struct nid_path *nomix_path, *mix_path;
1956 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1957 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1958 if (!nomix_path || !mix_path)
1961 snd_hda_activate_path(codec, nomix_path, false, true);
1962 snd_hda_activate_path(codec, mix_path, true, true);
1964 snd_hda_activate_path(codec, mix_path, false, true);
1965 snd_hda_activate_path(codec, nomix_path, true, true);
1969 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1970 struct snd_ctl_elem_value *ucontrol)
1972 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1973 struct hda_gen_spec *spec = codec->spec;
1974 unsigned int val = ucontrol->value.enumerated.item[0];
1976 if (val == spec->aamix_mode)
1978 spec->aamix_mode = val;
1979 update_aamix_paths(codec, val, spec->out_paths[0],
1980 spec->aamix_out_paths[0]);
1981 update_aamix_paths(codec, val, spec->hp_paths[0],
1982 spec->aamix_out_paths[1]);
1983 update_aamix_paths(codec, val, spec->speaker_paths[0],
1984 spec->aamix_out_paths[2]);
1988 static const struct snd_kcontrol_new loopback_mixing_enum = {
1989 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1990 .name = "Loopback Mixing",
1991 .info = loopback_mixing_info,
1992 .get = loopback_mixing_get,
1993 .put = loopback_mixing_put,
1996 static int create_loopback_mixing_ctl(struct hda_codec *codec)
1998 struct hda_gen_spec *spec = codec->spec;
2000 if (!spec->mixer_nid)
2002 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2003 spec->aamix_out_paths[2]))
2005 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2011 * shared headphone/mic handling
2014 static void call_update_outputs(struct hda_codec *codec);
2016 /* for shared I/O, change the pin-control accordingly */
2017 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
2019 struct hda_gen_spec *spec = codec->spec;
2021 hda_nid_t pin = spec->autocfg.inputs[1].pin;
2022 /* NOTE: this assumes that there are only two inputs, the
2023 * first is the real internal mic and the second is HP/mic jack.
2026 val = snd_hda_get_default_vref(codec, pin);
2028 /* This pin does not have vref caps - let's enable vref on pin 0x18
2029 instead, as suggested by Realtek */
2030 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2031 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2032 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2033 if (vref_val != AC_PINCTL_VREF_HIZ)
2034 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2035 PIN_IN | (set_as_mic ? vref_val : 0));
2038 val = set_as_mic ? val | PIN_IN : PIN_HP;
2039 set_pin_target(codec, pin, val, true);
2041 spec->automute_speaker = !set_as_mic;
2042 call_update_outputs(codec);
2045 /* create a shared input with the headphone out */
2046 static int create_shared_input(struct hda_codec *codec)
2048 struct hda_gen_spec *spec = codec->spec;
2049 struct auto_pin_cfg *cfg = &spec->autocfg;
2050 unsigned int defcfg;
2053 /* only one internal input pin? */
2054 if (cfg->num_inputs != 1)
2056 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2057 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2060 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2061 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2062 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2063 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2065 return 0; /* both not available */
2067 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2068 return 0; /* no input */
2070 cfg->inputs[1].pin = nid;
2071 cfg->inputs[1].type = AUTO_PIN_MIC;
2072 cfg->num_inputs = 2;
2073 spec->shared_mic_hp = 1;
2074 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2081 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2082 struct snd_ctl_elem_info *uinfo)
2084 static const char * const texts[] = {
2085 "Line Out", "Headphone Out",
2087 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2090 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2091 struct snd_ctl_elem_value *ucontrol)
2093 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2094 hda_nid_t nid = kcontrol->private_value;
2095 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2096 ucontrol->value.enumerated.item[0] = 1;
2098 ucontrol->value.enumerated.item[0] = 0;
2102 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2103 struct snd_ctl_elem_value *ucontrol)
2105 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2106 hda_nid_t nid = kcontrol->private_value;
2109 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2110 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2112 snd_hda_set_pin_ctl_cache(codec, nid, val);
2116 static const struct snd_kcontrol_new out_jack_mode_enum = {
2117 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2118 .info = out_jack_mode_info,
2119 .get = out_jack_mode_get,
2120 .put = out_jack_mode_put,
2123 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2125 struct hda_gen_spec *spec = codec->spec;
2128 for (i = 0; i < spec->kctls.used; i++) {
2129 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2130 if (!strcmp(kctl->name, name) && kctl->index == idx)
2136 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2137 char *name, size_t name_len)
2139 struct hda_gen_spec *spec = codec->spec;
2142 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2143 strlcat(name, " Jack Mode", name_len);
2145 for (; find_kctl_name(codec, name, idx); idx++)
2149 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2152 struct hda_gen_spec *spec = codec->spec;
2155 for (i = 0; i < num_pins; i++) {
2156 hda_nid_t pin = pins[i];
2157 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2158 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2159 struct snd_kcontrol_new *knew;
2161 get_jack_mode_name(codec, pin, name, sizeof(name));
2162 knew = snd_hda_gen_add_kctl(spec, name,
2163 &out_jack_mode_enum);
2166 knew->private_value = pin;
2177 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2180 static const char * const vref_texts[NUM_VREFS] = {
2181 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2182 "", "Mic 80pc Bias", "Mic 100pc Bias"
2185 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2187 unsigned int pincap;
2189 pincap = snd_hda_query_pin_caps(codec, pin);
2190 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2191 /* filter out unusual vrefs */
2192 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2196 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2197 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2199 unsigned int i, n = 0;
2201 for (i = 0; i < NUM_VREFS; i++) {
2202 if (vref_caps & (1 << i)) {
2211 /* convert back from the vref ctl index to the enum item index */
2212 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2214 unsigned int i, n = 0;
2216 for (i = 0; i < NUM_VREFS; i++) {
2219 if (vref_caps & (1 << i))
2225 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2226 struct snd_ctl_elem_info *uinfo)
2228 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2229 hda_nid_t nid = kcontrol->private_value;
2230 unsigned int vref_caps = get_vref_caps(codec, nid);
2232 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2234 /* set the right text */
2235 strcpy(uinfo->value.enumerated.name,
2236 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2240 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2241 struct snd_ctl_elem_value *ucontrol)
2243 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2244 hda_nid_t nid = kcontrol->private_value;
2245 unsigned int vref_caps = get_vref_caps(codec, nid);
2248 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2249 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2253 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2254 struct snd_ctl_elem_value *ucontrol)
2256 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2257 hda_nid_t nid = kcontrol->private_value;
2258 unsigned int vref_caps = get_vref_caps(codec, nid);
2259 unsigned int val, idx;
2261 val = snd_hda_codec_get_pin_target(codec, nid);
2262 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2263 if (idx == ucontrol->value.enumerated.item[0])
2266 val &= ~AC_PINCTL_VREFEN;
2267 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2268 snd_hda_set_pin_ctl_cache(codec, nid, val);
2272 static const struct snd_kcontrol_new in_jack_mode_enum = {
2273 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2274 .info = in_jack_mode_info,
2275 .get = in_jack_mode_get,
2276 .put = in_jack_mode_put,
2279 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2281 struct hda_gen_spec *spec = codec->spec;
2282 unsigned int defcfg;
2283 struct snd_kcontrol_new *knew;
2286 /* no jack mode for fixed pins */
2287 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2288 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2291 /* no multiple vref caps? */
2292 if (hweight32(get_vref_caps(codec, pin)) <= 1)
2295 get_jack_mode_name(codec, pin, name, sizeof(name));
2296 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2299 knew->private_value = pin;
2309 /* add the powersave loopback-list entry */
2310 static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2312 struct hda_amp_list *list;
2314 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2316 list = spec->loopback_list + spec->num_loopbacks;
2318 list->dir = HDA_INPUT;
2320 spec->num_loopbacks++;
2321 spec->loopback.amplist = spec->loopback_list;
2324 #define add_loopback_list(spec, mix, idx) /* NOP */
2327 /* create input playback/capture controls for the given pin */
2328 static int new_analog_input(struct hda_codec *codec, int input_idx,
2329 hda_nid_t pin, const char *ctlname, int ctlidx,
2332 struct hda_gen_spec *spec = codec->spec;
2333 struct nid_path *path;
2337 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2338 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2339 return 0; /* no need for analog loopback */
2341 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
2344 print_nid_path("loopback", path);
2345 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
2347 idx = path->idx[path->depth - 1];
2348 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2349 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2350 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
2353 path->ctls[NID_PATH_VOL_CTL] = val;
2356 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2357 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2358 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
2361 path->ctls[NID_PATH_MUTE_CTL] = val;
2364 path->active = true;
2365 add_loopback_list(spec, mix_nid, idx);
2369 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2371 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2372 return (pincap & AC_PINCAP_IN) != 0;
2375 /* Parse the codec tree and retrieve ADCs */
2376 static int fill_adc_nids(struct hda_codec *codec)
2378 struct hda_gen_spec *spec = codec->spec;
2380 hda_nid_t *adc_nids = spec->adc_nids;
2381 int max_nums = ARRAY_SIZE(spec->adc_nids);
2384 nid = codec->start_nid;
2385 for (i = 0; i < codec->num_nodes; i++, nid++) {
2386 unsigned int caps = get_wcaps(codec, nid);
2387 int type = get_wcaps_type(caps);
2389 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2391 adc_nids[nums] = nid;
2392 if (++nums >= max_nums)
2395 spec->num_adc_nids = nums;
2397 /* copy the detected ADCs to all_adcs[] */
2398 spec->num_all_adcs = nums;
2399 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2404 /* filter out invalid adc_nids that don't give all active input pins;
2405 * if needed, check whether dynamic ADC-switching is available
2407 static int check_dyn_adc_switch(struct hda_codec *codec)
2409 struct hda_gen_spec *spec = codec->spec;
2410 struct hda_input_mux *imux = &spec->input_mux;
2411 unsigned int ok_bits;
2417 for (n = 0; n < spec->num_adc_nids; n++) {
2418 for (i = 0; i < imux->num_items; i++) {
2419 if (!spec->input_paths[i][n])
2422 if (i >= imux->num_items) {
2423 ok_bits |= (1 << n);
2429 if (spec->shared_mic_hp) {
2430 spec->shared_mic_hp = 0;
2431 imux->num_items = 1;
2435 /* check whether ADC-switch is possible */
2436 for (i = 0; i < imux->num_items; i++) {
2437 for (n = 0; n < spec->num_adc_nids; n++) {
2438 if (spec->input_paths[i][n]) {
2439 spec->dyn_adc_idx[i] = n;
2445 snd_printdd("hda-codec: enabling ADC switching\n");
2446 spec->dyn_adc_switch = 1;
2447 } else if (nums != spec->num_adc_nids) {
2448 /* shrink the invalid adcs and input paths */
2450 for (n = 0; n < spec->num_adc_nids; n++) {
2451 if (!(ok_bits & (1 << n)))
2454 spec->adc_nids[nums] = spec->adc_nids[n];
2455 for (i = 0; i < imux->num_items; i++) {
2456 invalidate_nid_path(codec,
2457 spec->input_paths[i][nums]);
2458 spec->input_paths[i][nums] =
2459 spec->input_paths[i][n];
2464 spec->num_adc_nids = nums;
2467 if (imux->num_items == 1 || spec->shared_mic_hp) {
2468 snd_printdd("hda-codec: reducing to a single ADC\n");
2469 spec->num_adc_nids = 1; /* reduce to a single ADC */
2472 /* single index for individual volumes ctls */
2473 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2474 spec->num_adc_nids = 1;
2479 /* parse capture source paths from the given pin and create imux items */
2480 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2481 int num_adcs, const char *label, int anchor)
2483 struct hda_gen_spec *spec = codec->spec;
2484 struct hda_input_mux *imux = &spec->input_mux;
2485 int imux_idx = imux->num_items;
2486 bool imux_added = false;
2489 for (c = 0; c < num_adcs; c++) {
2490 struct nid_path *path;
2491 hda_nid_t adc = spec->adc_nids[c];
2493 if (!is_reachable_path(codec, pin, adc))
2495 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2498 print_nid_path("input", path);
2499 spec->input_paths[imux_idx][c] =
2500 snd_hda_get_path_idx(codec, path);
2503 spec->imux_pins[imux->num_items] = pin;
2504 snd_hda_add_imux_item(imux, label,
2505 imux->num_items, NULL);
2514 * create playback/capture controls for input pins
2516 static int create_input_ctls(struct hda_codec *codec)
2518 struct hda_gen_spec *spec = codec->spec;
2519 const struct auto_pin_cfg *cfg = &spec->autocfg;
2520 hda_nid_t mixer = spec->mixer_nid;
2522 int i, err, type_idx = 0;
2523 const char *prev_label = NULL;
2526 num_adcs = fill_adc_nids(codec);
2530 for (i = 0; i < cfg->num_inputs; i++) {
2534 pin = cfg->inputs[i].pin;
2535 if (!is_input_pin(codec, pin))
2538 label = hda_get_autocfg_input_label(codec, cfg, i);
2539 if (prev_label && !strcmp(label, prev_label))
2546 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2547 val |= snd_hda_get_default_vref(codec, pin);
2548 set_pin_target(codec, pin, val, false);
2551 if (is_reachable_path(codec, pin, mixer)) {
2552 err = new_analog_input(codec, i, pin,
2553 label, type_idx, mixer);
2559 err = parse_capture_source(codec, pin, num_adcs, label, -mixer);
2563 if (spec->add_in_jack_modes) {
2564 err = create_in_jack_mode(codec, pin);
2570 if (mixer && spec->add_stereo_mix_input) {
2571 err = parse_capture_source(codec, mixer, num_adcs,
2585 /* get the input path specified by the given adc and imux indices */
2586 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
2588 struct hda_gen_spec *spec = codec->spec;
2589 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2593 if (spec->dyn_adc_switch)
2594 adc_idx = spec->dyn_adc_idx[imux_idx];
2595 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_OUTS) {
2599 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
2602 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2605 static int mux_enum_info(struct snd_kcontrol *kcontrol,
2606 struct snd_ctl_elem_info *uinfo)
2608 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2609 struct hda_gen_spec *spec = codec->spec;
2610 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2613 static int mux_enum_get(struct snd_kcontrol *kcontrol,
2614 struct snd_ctl_elem_value *ucontrol)
2616 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2617 struct hda_gen_spec *spec = codec->spec;
2618 unsigned int adc_idx = kcontrol->id.index;
2620 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2624 static int mux_enum_put(struct snd_kcontrol *kcontrol,
2625 struct snd_ctl_elem_value *ucontrol)
2627 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2628 unsigned int adc_idx = kcontrol->id.index;
2629 return mux_select(codec, adc_idx,
2630 ucontrol->value.enumerated.item[0]);
2633 static const struct snd_kcontrol_new cap_src_temp = {
2634 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2635 .name = "Input Source",
2636 .info = mux_enum_info,
2637 .get = mux_enum_get,
2638 .put = mux_enum_put,
2642 * capture volume and capture switch ctls
2645 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2646 struct snd_ctl_elem_value *ucontrol);
2648 /* call the given amp update function for all amps in the imux list at once */
2649 static int cap_put_caller(struct snd_kcontrol *kcontrol,
2650 struct snd_ctl_elem_value *ucontrol,
2651 put_call_t func, int type)
2653 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2654 struct hda_gen_spec *spec = codec->spec;
2655 const struct hda_input_mux *imux;
2656 struct nid_path *path;
2657 int i, adc_idx, err = 0;
2659 imux = &spec->input_mux;
2660 adc_idx = kcontrol->id.index;
2661 mutex_lock(&codec->control_mutex);
2662 /* we use the cache-only update at first since multiple input paths
2663 * may shared the same amp; by updating only caches, the redundant
2664 * writes to hardware can be reduced.
2666 codec->cached_write = 1;
2667 for (i = 0; i < imux->num_items; i++) {
2668 path = get_input_path(codec, adc_idx, i);
2669 if (!path || !path->ctls[type])
2671 kcontrol->private_value = path->ctls[type];
2672 err = func(kcontrol, ucontrol);
2677 codec->cached_write = 0;
2678 mutex_unlock(&codec->control_mutex);
2679 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
2680 if (err >= 0 && spec->cap_sync_hook)
2681 spec->cap_sync_hook(codec);
2685 /* capture volume ctl callbacks */
2686 #define cap_vol_info snd_hda_mixer_amp_volume_info
2687 #define cap_vol_get snd_hda_mixer_amp_volume_get
2688 #define cap_vol_tlv snd_hda_mixer_amp_tlv
2690 static int cap_vol_put(struct snd_kcontrol *kcontrol,
2691 struct snd_ctl_elem_value *ucontrol)
2693 return cap_put_caller(kcontrol, ucontrol,
2694 snd_hda_mixer_amp_volume_put,
2698 static const struct snd_kcontrol_new cap_vol_temp = {
2699 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2700 .name = "Capture Volume",
2701 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2702 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2703 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2704 .info = cap_vol_info,
2707 .tlv = { .c = cap_vol_tlv },
2710 /* capture switch ctl callbacks */
2711 #define cap_sw_info snd_ctl_boolean_stereo_info
2712 #define cap_sw_get snd_hda_mixer_amp_switch_get
2714 static int cap_sw_put(struct snd_kcontrol *kcontrol,
2715 struct snd_ctl_elem_value *ucontrol)
2717 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2718 struct hda_gen_spec *spec = codec->spec;
2721 ret = cap_put_caller(kcontrol, ucontrol,
2722 snd_hda_mixer_amp_switch_put,
2727 if (spec->capture_switch_hook) {
2728 bool enable = (ucontrol->value.integer.value[0] ||
2729 ucontrol->value.integer.value[1]);
2730 spec->capture_switch_hook(codec, enable);
2736 static const struct snd_kcontrol_new cap_sw_temp = {
2737 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2738 .name = "Capture Switch",
2739 .info = cap_sw_info,
2744 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2749 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2750 for (depth = 0; depth < 3; depth++) {
2751 if (depth >= path->depth)
2753 i = path->depth - depth - 1;
2754 nid = path->path[i];
2755 if (!path->ctls[NID_PATH_VOL_CTL]) {
2756 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2757 path->ctls[NID_PATH_VOL_CTL] =
2758 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2759 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2760 int idx = path->idx[i];
2761 if (!depth && codec->single_adc_amp)
2763 path->ctls[NID_PATH_VOL_CTL] =
2764 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2767 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2768 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2769 path->ctls[NID_PATH_MUTE_CTL] =
2770 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2771 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2772 int idx = path->idx[i];
2773 if (!depth && codec->single_adc_amp)
2775 path->ctls[NID_PATH_MUTE_CTL] =
2776 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2783 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2785 struct hda_gen_spec *spec = codec->spec;
2786 struct auto_pin_cfg *cfg = &spec->autocfg;
2790 if (!spec->inv_dmic_split)
2792 for (i = 0; i < cfg->num_inputs; i++) {
2793 if (cfg->inputs[i].pin != nid)
2795 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2797 val = snd_hda_codec_get_pincfg(codec, nid);
2798 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2803 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2804 int idx, bool is_switch, unsigned int ctl,
2807 struct hda_gen_spec *spec = codec->spec;
2809 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2810 const char *sfx = is_switch ? "Switch" : "Volume";
2811 unsigned int chs = inv_dmic ? 1 : 3;
2818 snprintf(tmpname, sizeof(tmpname),
2819 "%s Capture %s", label, sfx);
2821 snprintf(tmpname, sizeof(tmpname),
2823 err = add_control(spec, type, tmpname, idx,
2824 amp_val_replace_channels(ctl, chs));
2825 if (err < 0 || !inv_dmic)
2828 /* Make independent right kcontrol */
2830 snprintf(tmpname, sizeof(tmpname),
2831 "Inverted %s Capture %s", label, sfx);
2833 snprintf(tmpname, sizeof(tmpname),
2834 "Inverted Capture %s", sfx);
2835 return add_control(spec, type, tmpname, idx,
2836 amp_val_replace_channels(ctl, 2));
2839 /* create single (and simple) capture volume and switch controls */
2840 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2841 unsigned int vol_ctl, unsigned int sw_ctl,
2845 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2848 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2854 /* create bound capture volume and switch controls */
2855 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2856 unsigned int vol_ctl, unsigned int sw_ctl)
2858 struct hda_gen_spec *spec = codec->spec;
2859 struct snd_kcontrol_new *knew;
2862 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
2866 knew->private_value = vol_ctl;
2867 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2870 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
2874 knew->private_value = sw_ctl;
2875 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2880 /* return the vol ctl when used first in the imux list */
2881 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2883 struct nid_path *path;
2887 path = get_input_path(codec, 0, idx);
2890 ctl = path->ctls[type];
2893 for (i = 0; i < idx - 1; i++) {
2894 path = get_input_path(codec, 0, i);
2895 if (path && path->ctls[type] == ctl)
2901 /* create individual capture volume and switch controls per input */
2902 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2904 struct hda_gen_spec *spec = codec->spec;
2905 struct hda_input_mux *imux = &spec->input_mux;
2906 int i, err, type, type_idx = 0;
2907 const char *prev_label = NULL;
2909 for (i = 0; i < imux->num_items; i++) {
2912 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2913 if (prev_label && !strcmp(label, prev_label))
2918 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2920 for (type = 0; type < 2; type++) {
2921 err = add_single_cap_ctl(codec, label, type_idx, type,
2922 get_first_cap_ctl(codec, i, type),
2931 static int create_capture_mixers(struct hda_codec *codec)
2933 struct hda_gen_spec *spec = codec->spec;
2934 struct hda_input_mux *imux = &spec->input_mux;
2935 int i, n, nums, err;
2937 if (spec->dyn_adc_switch)
2940 nums = spec->num_adc_nids;
2942 if (!spec->auto_mic && imux->num_items > 1) {
2943 struct snd_kcontrol_new *knew;
2945 name = nums > 1 ? "Input Source" : "Capture Source";
2946 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
2952 for (n = 0; n < nums; n++) {
2954 bool multi_cap_vol = spec->multi_cap_vol;
2955 bool inv_dmic = false;
2959 for (i = 0; i < imux->num_items; i++) {
2960 struct nid_path *path;
2961 path = get_input_path(codec, n, i);
2964 parse_capvol_in_path(codec, path);
2966 vol = path->ctls[NID_PATH_VOL_CTL];
2967 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
2969 if (!same_amp_caps(codec, vol,
2970 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
2971 multi_cap_vol = true;
2974 sw = path->ctls[NID_PATH_MUTE_CTL];
2975 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
2977 if (!same_amp_caps(codec, sw,
2978 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
2979 multi_cap_vol = true;
2981 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2986 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2988 else if (!multi_cap_vol)
2989 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2991 err = create_multi_cap_vol_ctl(codec);
3000 * add mic boosts if needed
3002 static int parse_mic_boost(struct hda_codec *codec)
3004 struct hda_gen_spec *spec = codec->spec;
3005 struct auto_pin_cfg *cfg = &spec->autocfg;
3009 const char *prev_label = NULL;
3011 for (i = 0; i < cfg->num_inputs; i++) {
3012 if (cfg->inputs[i].type > AUTO_PIN_MIC)
3014 nid = cfg->inputs[i].pin;
3015 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
3017 char boost_label[44];
3018 struct nid_path *path;
3021 if (!nid_has_volume(codec, nid, HDA_INPUT))
3024 label = hda_get_autocfg_input_label(codec, cfg, i);
3025 if (prev_label && !strcmp(label, prev_label))
3031 snprintf(boost_label, sizeof(boost_label),
3032 "%s Boost Volume", label);
3033 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
3034 err = add_control(spec, HDA_CTL_WIDGET_VOL,
3035 boost_label, type_idx, val);
3039 path = snd_hda_get_nid_path(codec, nid, 0);
3041 path->ctls[NID_PATH_BOOST_CTL] = val;
3048 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3050 static void parse_digital(struct hda_codec *codec)
3052 struct hda_gen_spec *spec = codec->spec;
3053 struct nid_path *path;
3055 hda_nid_t dig_nid, pin;
3057 /* support multiple SPDIFs; the secondary is set up as a slave */
3059 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3060 pin = spec->autocfg.dig_out_pins[i];
3061 dig_nid = look_for_dac(codec, pin, true);
3064 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3067 print_nid_path("digout", path);
3068 path->active = true;
3069 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3070 set_pin_target(codec, pin, PIN_OUT, false);
3072 spec->multiout.dig_out_nid = dig_nid;
3073 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3075 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3076 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3078 spec->slave_dig_outs[nums - 1] = dig_nid;
3083 if (spec->autocfg.dig_in_pin) {
3084 pin = spec->autocfg.dig_in_pin;
3085 dig_nid = codec->start_nid;
3086 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3087 unsigned int wcaps = get_wcaps(codec, dig_nid);
3088 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3090 if (!(wcaps & AC_WCAP_DIGITAL))
3092 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3094 print_nid_path("digin", path);
3095 path->active = true;
3096 spec->dig_in_nid = dig_nid;
3097 spec->digin_path = snd_hda_get_path_idx(codec, path);
3098 set_pin_target(codec, pin, PIN_IN, false);
3107 * input MUX handling
3110 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3112 /* select the given imux item; either unmute exclusively or select the route */
3113 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3116 struct hda_gen_spec *spec = codec->spec;
3117 const struct hda_input_mux *imux;
3118 struct nid_path *path;
3120 imux = &spec->input_mux;
3121 if (!imux->num_items)
3124 if (idx >= imux->num_items)
3125 idx = imux->num_items - 1;
3126 if (spec->cur_mux[adc_idx] == idx)
3129 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3133 snd_hda_activate_path(codec, path, false, false);
3135 spec->cur_mux[adc_idx] = idx;
3137 if (spec->shared_mic_hp)
3138 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3140 if (spec->dyn_adc_switch)
3141 dyn_adc_pcm_resetup(codec, idx);
3143 path = get_input_path(codec, adc_idx, idx);
3148 snd_hda_activate_path(codec, path, true, false);
3149 if (spec->cap_sync_hook)
3150 spec->cap_sync_hook(codec);
3156 * Jack detections for HP auto-mute and mic-switch
3159 /* check each pin in the given array; returns true if any of them is plugged */
3160 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3164 for (i = 0; i < num_pins; i++) {
3165 hda_nid_t nid = pins[i];
3168 /* don't detect pins retasked as inputs */
3169 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3171 present |= snd_hda_jack_detect(codec, nid);
3176 /* standard HP/line-out auto-mute helper */
3177 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
3180 struct hda_gen_spec *spec = codec->spec;
3183 for (i = 0; i < num_pins; i++) {
3184 hda_nid_t nid = pins[i];
3188 /* don't reset VREF value in case it's controlling
3189 * the amp (see alc861_fixup_asus_amp_vref_0f())
3191 if (spec->keep_vref_in_automute)
3192 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3196 val |= snd_hda_codec_get_pin_target(codec, nid);
3197 /* here we call update_pin_ctl() so that the pinctl is changed
3198 * without changing the pinctl target value;
3199 * the original target value will be still referred at the
3200 * init / resume again
3202 update_pin_ctl(codec, nid, val);
3203 set_pin_eapd(codec, nid, !mute);
3207 /* Toggle outputs muting */
3208 void snd_hda_gen_update_outputs(struct hda_codec *codec)
3210 struct hda_gen_spec *spec = codec->spec;
3213 /* Control HP pins/amps depending on master_mute state;
3214 * in general, HP pins/amps control should be enabled in all cases,
3215 * but currently set only for master_mute, just to be safe
3217 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3218 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3219 spec->autocfg.hp_pins, spec->master_mute);
3221 if (!spec->automute_speaker)
3224 on = spec->hp_jack_present | spec->line_jack_present;
3225 on |= spec->master_mute;
3226 spec->speaker_muted = on;
3227 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
3228 spec->autocfg.speaker_pins, on);
3230 /* toggle line-out mutes if needed, too */
3231 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3232 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3233 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3235 if (!spec->automute_lo)
3238 on = spec->hp_jack_present;
3239 on |= spec->master_mute;
3240 spec->line_out_muted = on;
3241 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3242 spec->autocfg.line_out_pins, on);
3244 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
3246 static void call_update_outputs(struct hda_codec *codec)
3248 struct hda_gen_spec *spec = codec->spec;
3249 if (spec->automute_hook)
3250 spec->automute_hook(codec);
3252 snd_hda_gen_update_outputs(codec);
3255 /* standard HP-automute helper */
3256 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3258 struct hda_gen_spec *spec = codec->spec;
3260 spec->hp_jack_present =
3261 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3262 spec->autocfg.hp_pins);
3263 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3265 call_update_outputs(codec);
3267 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
3269 /* standard line-out-automute helper */
3270 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3272 struct hda_gen_spec *spec = codec->spec;
3274 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3276 /* check LO jack only when it's different from HP */
3277 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3280 spec->line_jack_present =
3281 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3282 spec->autocfg.line_out_pins);
3283 if (!spec->automute_speaker || !spec->detect_lo)
3285 call_update_outputs(codec);
3287 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
3289 /* standard mic auto-switch helper */
3290 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
3292 struct hda_gen_spec *spec = codec->spec;
3295 if (!spec->auto_mic)
3298 for (i = spec->am_num_entries - 1; i > 0; i--) {
3299 hda_nid_t pin = spec->am_entry[i].pin;
3300 /* don't detect pins retasked as outputs */
3301 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3303 if (snd_hda_jack_detect(codec, pin)) {
3304 mux_select(codec, 0, spec->am_entry[i].idx);
3308 mux_select(codec, 0, spec->am_entry[0].idx);
3310 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
3312 /* update jack retasking */
3313 static void update_automute_all(struct hda_codec *codec)
3315 struct hda_gen_spec *spec = codec->spec;
3317 if (spec->hp_automute_hook)
3318 spec->hp_automute_hook(codec, NULL);
3320 snd_hda_gen_hp_automute(codec, NULL);
3321 if (spec->line_automute_hook)
3322 spec->line_automute_hook(codec, NULL);
3324 snd_hda_gen_line_automute(codec, NULL);
3325 if (spec->mic_autoswitch_hook)
3326 spec->mic_autoswitch_hook(codec, NULL);
3328 snd_hda_gen_mic_autoswitch(codec, NULL);
3332 * Auto-Mute mode mixer enum support
3334 static int automute_mode_info(struct snd_kcontrol *kcontrol,
3335 struct snd_ctl_elem_info *uinfo)
3337 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3338 struct hda_gen_spec *spec = codec->spec;
3339 static const char * const texts3[] = {
3340 "Disabled", "Speaker Only", "Line Out+Speaker"
3343 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3344 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3345 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3348 static int automute_mode_get(struct snd_kcontrol *kcontrol,
3349 struct snd_ctl_elem_value *ucontrol)
3351 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3352 struct hda_gen_spec *spec = codec->spec;
3353 unsigned int val = 0;
3354 if (spec->automute_speaker)
3356 if (spec->automute_lo)
3359 ucontrol->value.enumerated.item[0] = val;
3363 static int automute_mode_put(struct snd_kcontrol *kcontrol,
3364 struct snd_ctl_elem_value *ucontrol)
3366 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3367 struct hda_gen_spec *spec = codec->spec;
3369 switch (ucontrol->value.enumerated.item[0]) {
3371 if (!spec->automute_speaker && !spec->automute_lo)
3373 spec->automute_speaker = 0;
3374 spec->automute_lo = 0;
3377 if (spec->automute_speaker_possible) {
3378 if (!spec->automute_lo && spec->automute_speaker)
3380 spec->automute_speaker = 1;
3381 spec->automute_lo = 0;
3382 } else if (spec->automute_lo_possible) {
3383 if (spec->automute_lo)
3385 spec->automute_lo = 1;
3390 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3392 if (spec->automute_speaker && spec->automute_lo)
3394 spec->automute_speaker = 1;
3395 spec->automute_lo = 1;
3400 call_update_outputs(codec);
3404 static const struct snd_kcontrol_new automute_mode_enum = {
3405 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3406 .name = "Auto-Mute Mode",
3407 .info = automute_mode_info,
3408 .get = automute_mode_get,
3409 .put = automute_mode_put,
3412 static int add_automute_mode_enum(struct hda_codec *codec)
3414 struct hda_gen_spec *spec = codec->spec;
3416 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
3422 * Check the availability of HP/line-out auto-mute;
3423 * Set up appropriately if really supported
3425 static int check_auto_mute_availability(struct hda_codec *codec)
3427 struct hda_gen_spec *spec = codec->spec;
3428 struct auto_pin_cfg *cfg = &spec->autocfg;
3432 if (spec->suppress_auto_mute)
3435 if (cfg->hp_pins[0])
3437 if (cfg->line_out_pins[0])
3439 if (cfg->speaker_pins[0])
3441 if (present < 2) /* need two different output types */
3444 if (!cfg->speaker_pins[0] &&
3445 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3446 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3447 sizeof(cfg->speaker_pins));
3448 cfg->speaker_outs = cfg->line_outs;
3451 if (!cfg->hp_pins[0] &&
3452 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3453 memcpy(cfg->hp_pins, cfg->line_out_pins,
3454 sizeof(cfg->hp_pins));
3455 cfg->hp_outs = cfg->line_outs;
3458 for (i = 0; i < cfg->hp_outs; i++) {
3459 hda_nid_t nid = cfg->hp_pins[i];
3460 if (!is_jack_detectable(codec, nid))
3462 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3464 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
3465 spec->hp_automute_hook ?
3466 spec->hp_automute_hook :
3467 snd_hda_gen_hp_automute);
3468 spec->detect_hp = 1;
3471 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3472 if (cfg->speaker_outs)
3473 for (i = 0; i < cfg->line_outs; i++) {
3474 hda_nid_t nid = cfg->line_out_pins[i];
3475 if (!is_jack_detectable(codec, nid))
3477 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3478 snd_hda_jack_detect_enable_callback(codec, nid,
3479 HDA_GEN_FRONT_EVENT,
3480 spec->line_automute_hook ?
3481 spec->line_automute_hook :
3482 snd_hda_gen_line_automute);
3483 spec->detect_lo = 1;
3485 spec->automute_lo_possible = spec->detect_hp;
3488 spec->automute_speaker_possible = cfg->speaker_outs &&
3489 (spec->detect_hp || spec->detect_lo);
3491 spec->automute_lo = spec->automute_lo_possible;
3492 spec->automute_speaker = spec->automute_speaker_possible;
3494 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3495 /* create a control for automute mode */
3496 err = add_automute_mode_enum(codec);
3503 /* check whether all auto-mic pins are valid; setup indices if OK */
3504 static bool auto_mic_check_imux(struct hda_codec *codec)
3506 struct hda_gen_spec *spec = codec->spec;
3507 const struct hda_input_mux *imux;
3510 imux = &spec->input_mux;
3511 for (i = 0; i < spec->am_num_entries; i++) {
3512 spec->am_entry[i].idx =
3513 find_idx_in_nid_list(spec->am_entry[i].pin,
3514 spec->imux_pins, imux->num_items);
3515 if (spec->am_entry[i].idx < 0)
3516 return false; /* no corresponding imux */
3519 /* we don't need the jack detection for the first pin */
3520 for (i = 1; i < spec->am_num_entries; i++)
3521 snd_hda_jack_detect_enable_callback(codec,
3522 spec->am_entry[i].pin,
3524 spec->mic_autoswitch_hook ?
3525 spec->mic_autoswitch_hook :
3526 snd_hda_gen_mic_autoswitch);
3530 static int compare_attr(const void *ap, const void *bp)
3532 const struct automic_entry *a = ap;
3533 const struct automic_entry *b = bp;
3534 return (int)(a->attr - b->attr);
3538 * Check the availability of auto-mic switch;
3539 * Set up if really supported
3541 static int check_auto_mic_availability(struct hda_codec *codec)
3543 struct hda_gen_spec *spec = codec->spec;
3544 struct auto_pin_cfg *cfg = &spec->autocfg;
3548 if (spec->suppress_auto_mic)
3553 for (i = 0; i < cfg->num_inputs; i++) {
3554 hda_nid_t nid = cfg->inputs[i].pin;
3556 attr = snd_hda_codec_get_pincfg(codec, nid);
3557 attr = snd_hda_get_input_pin_attr(attr);
3558 if (types & (1 << attr))
3559 return 0; /* already occupied */
3561 case INPUT_PIN_ATTR_INT:
3562 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3563 return 0; /* invalid type */
3565 case INPUT_PIN_ATTR_UNUSED:
3566 return 0; /* invalid entry */
3568 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3569 return 0; /* invalid type */
3570 if (!spec->line_in_auto_switch &&
3571 cfg->inputs[i].type != AUTO_PIN_MIC)
3572 return 0; /* only mic is allowed */
3573 if (!is_jack_detectable(codec, nid))
3574 return 0; /* no unsol support */
3577 if (num_pins >= MAX_AUTO_MIC_PINS)
3579 types |= (1 << attr);
3580 spec->am_entry[num_pins].pin = nid;
3581 spec->am_entry[num_pins].attr = attr;
3588 spec->am_num_entries = num_pins;
3589 /* sort the am_entry in the order of attr so that the pin with a
3590 * higher attr will be selected when the jack is plugged.
3592 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3593 compare_attr, NULL);
3595 if (!auto_mic_check_imux(codec))
3599 spec->num_adc_nids = 1;
3600 spec->cur_mux[0] = spec->am_entry[0].idx;
3601 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3602 spec->am_entry[0].pin,
3603 spec->am_entry[1].pin,
3604 spec->am_entry[2].pin);
3611 * Parse the given BIOS configuration and set up the hda_gen_spec
3613 * return 1 if successful, 0 if the proper config is not found,
3614 * or a negative error code
3616 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
3617 struct auto_pin_cfg *cfg)
3619 struct hda_gen_spec *spec = codec->spec;
3622 parse_user_hints(codec);
3624 if (cfg != &spec->autocfg) {
3625 spec->autocfg = *cfg;
3626 cfg = &spec->autocfg;
3629 fill_all_dac_nids(codec);
3631 if (!cfg->line_outs) {
3632 if (cfg->dig_outs || cfg->dig_in_pin) {
3633 spec->multiout.max_channels = 2;
3634 spec->no_analog = 1;
3637 return 0; /* can't find valid BIOS pin config */
3640 if (!spec->no_primary_hp &&
3641 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3642 cfg->line_outs <= cfg->hp_outs) {
3643 /* use HP as primary out */
3644 cfg->speaker_outs = cfg->line_outs;
3645 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3646 sizeof(cfg->speaker_pins));
3647 cfg->line_outs = cfg->hp_outs;
3648 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3650 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3651 cfg->line_out_type = AUTO_PIN_HP_OUT;
3654 err = parse_output_paths(codec);
3657 err = create_multi_channel_mode(codec);
3660 err = create_multi_out_ctls(codec, cfg);
3663 err = create_hp_out_ctls(codec);
3666 err = create_speaker_out_ctls(codec);
3669 err = create_indep_hp_ctls(codec);
3672 err = create_loopback_mixing_ctl(codec);
3675 err = create_shared_input(codec);
3678 err = create_input_ctls(codec);
3682 spec->const_channel_count = spec->ext_channel_count;
3683 /* check the multiple speaker and headphone pins */
3684 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3685 spec->const_channel_count = max(spec->const_channel_count,
3686 cfg->speaker_outs * 2);
3687 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3688 spec->const_channel_count = max(spec->const_channel_count,
3690 spec->multiout.max_channels = max(spec->ext_channel_count,
3691 spec->const_channel_count);
3693 err = check_auto_mute_availability(codec);
3697 err = check_dyn_adc_switch(codec);
3701 if (!spec->shared_mic_hp) {
3702 err = check_auto_mic_availability(codec);
3707 err = create_capture_mixers(codec);
3711 err = parse_mic_boost(codec);
3715 if (spec->add_out_jack_modes) {
3716 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3717 err = create_out_jack_modes(codec, cfg->line_outs,
3718 cfg->line_out_pins);
3722 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3723 err = create_out_jack_modes(codec, cfg->hp_outs,
3731 parse_digital(codec);
3735 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
3739 * Build control elements
3742 /* slave controls for virtual master */
3743 static const char * const slave_pfxs[] = {
3744 "Front", "Surround", "Center", "LFE", "Side",
3745 "Headphone", "Speaker", "Mono", "Line Out",
3746 "CLFE", "Bass Speaker", "PCM",
3747 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3748 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3753 int snd_hda_gen_build_controls(struct hda_codec *codec)
3755 struct hda_gen_spec *spec = codec->spec;
3758 if (spec->kctls.used) {
3759 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3764 if (spec->multiout.dig_out_nid) {
3765 err = snd_hda_create_dig_out_ctls(codec,
3766 spec->multiout.dig_out_nid,
3767 spec->multiout.dig_out_nid,
3768 spec->pcm_rec[1].pcm_type);
3771 if (!spec->no_analog) {
3772 err = snd_hda_create_spdif_share_sw(codec,
3776 spec->multiout.share_spdif = 1;
3779 if (spec->dig_in_nid) {
3780 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3785 /* if we have no master control, let's create it */
3786 if (!spec->no_analog &&
3787 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3788 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3789 spec->vmaster_tlv, slave_pfxs,
3794 if (!spec->no_analog &&
3795 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3796 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3799 true, &spec->vmaster_mute.sw_kctl);
3802 if (spec->vmaster_mute.hook)
3803 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3804 spec->vmaster_mute_enum);
3807 free_kctls(spec); /* no longer needed */
3809 if (spec->shared_mic_hp) {
3811 int nid = spec->autocfg.inputs[1].pin;
3812 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3815 err = snd_hda_jack_detect_enable(codec, nid, 0);
3820 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3826 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3833 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3834 struct hda_codec *codec,
3835 struct snd_pcm_substream *substream,
3838 struct hda_gen_spec *spec = codec->spec;
3839 if (spec->pcm_playback_hook)
3840 spec->pcm_playback_hook(hinfo, codec, substream, action);
3843 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
3844 struct hda_codec *codec,
3845 struct snd_pcm_substream *substream,
3848 struct hda_gen_spec *spec = codec->spec;
3849 if (spec->pcm_capture_hook)
3850 spec->pcm_capture_hook(hinfo, codec, substream, action);
3854 * Analog playback callbacks
3856 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3857 struct hda_codec *codec,
3858 struct snd_pcm_substream *substream)
3860 struct hda_gen_spec *spec = codec->spec;
3863 mutex_lock(&spec->pcm_mutex);
3864 err = snd_hda_multi_out_analog_open(codec,
3865 &spec->multiout, substream,
3868 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3869 call_pcm_playback_hook(hinfo, codec, substream,
3870 HDA_GEN_PCM_ACT_OPEN);
3872 mutex_unlock(&spec->pcm_mutex);
3876 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3877 struct hda_codec *codec,
3878 unsigned int stream_tag,
3879 unsigned int format,
3880 struct snd_pcm_substream *substream)
3882 struct hda_gen_spec *spec = codec->spec;
3885 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3886 stream_tag, format, substream);
3888 call_pcm_playback_hook(hinfo, codec, substream,
3889 HDA_GEN_PCM_ACT_PREPARE);
3893 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3894 struct hda_codec *codec,
3895 struct snd_pcm_substream *substream)
3897 struct hda_gen_spec *spec = codec->spec;
3900 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3902 call_pcm_playback_hook(hinfo, codec, substream,
3903 HDA_GEN_PCM_ACT_CLEANUP);
3907 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3908 struct hda_codec *codec,
3909 struct snd_pcm_substream *substream)
3911 struct hda_gen_spec *spec = codec->spec;
3912 mutex_lock(&spec->pcm_mutex);
3913 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3914 call_pcm_playback_hook(hinfo, codec, substream,
3915 HDA_GEN_PCM_ACT_CLOSE);
3916 mutex_unlock(&spec->pcm_mutex);
3920 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
3921 struct hda_codec *codec,
3922 struct snd_pcm_substream *substream)
3924 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
3928 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3929 struct hda_codec *codec,
3930 unsigned int stream_tag,
3931 unsigned int format,
3932 struct snd_pcm_substream *substream)
3934 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3935 call_pcm_capture_hook(hinfo, codec, substream,
3936 HDA_GEN_PCM_ACT_PREPARE);
3940 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3941 struct hda_codec *codec,
3942 struct snd_pcm_substream *substream)
3944 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3945 call_pcm_capture_hook(hinfo, codec, substream,
3946 HDA_GEN_PCM_ACT_CLEANUP);
3950 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
3951 struct hda_codec *codec,
3952 struct snd_pcm_substream *substream)
3954 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
3958 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3959 struct hda_codec *codec,
3960 struct snd_pcm_substream *substream)
3962 struct hda_gen_spec *spec = codec->spec;
3965 mutex_lock(&spec->pcm_mutex);
3966 if (!spec->indep_hp_enabled)
3969 spec->active_streams |= 1 << STREAM_INDEP_HP;
3970 call_pcm_playback_hook(hinfo, codec, substream,
3971 HDA_GEN_PCM_ACT_OPEN);
3972 mutex_unlock(&spec->pcm_mutex);
3976 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3977 struct hda_codec *codec,
3978 struct snd_pcm_substream *substream)
3980 struct hda_gen_spec *spec = codec->spec;
3981 mutex_lock(&spec->pcm_mutex);
3982 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3983 call_pcm_playback_hook(hinfo, codec, substream,
3984 HDA_GEN_PCM_ACT_CLOSE);
3985 mutex_unlock(&spec->pcm_mutex);
3989 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3990 struct hda_codec *codec,
3991 unsigned int stream_tag,
3992 unsigned int format,
3993 struct snd_pcm_substream *substream)
3995 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3996 call_pcm_playback_hook(hinfo, codec, substream,
3997 HDA_GEN_PCM_ACT_PREPARE);
4001 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4002 struct hda_codec *codec,
4003 struct snd_pcm_substream *substream)
4005 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4006 call_pcm_playback_hook(hinfo, codec, substream,
4007 HDA_GEN_PCM_ACT_CLEANUP);
4014 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4015 struct hda_codec *codec,
4016 struct snd_pcm_substream *substream)
4018 struct hda_gen_spec *spec = codec->spec;
4019 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4022 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4023 struct hda_codec *codec,
4024 unsigned int stream_tag,
4025 unsigned int format,
4026 struct snd_pcm_substream *substream)
4028 struct hda_gen_spec *spec = codec->spec;
4029 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4030 stream_tag, format, substream);
4033 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4034 struct hda_codec *codec,
4035 struct snd_pcm_substream *substream)
4037 struct hda_gen_spec *spec = codec->spec;
4038 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4041 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4042 struct hda_codec *codec,
4043 struct snd_pcm_substream *substream)
4045 struct hda_gen_spec *spec = codec->spec;
4046 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4052 #define alt_capture_pcm_open capture_pcm_open
4053 #define alt_capture_pcm_close capture_pcm_close
4055 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4056 struct hda_codec *codec,
4057 unsigned int stream_tag,
4058 unsigned int format,
4059 struct snd_pcm_substream *substream)
4061 struct hda_gen_spec *spec = codec->spec;
4063 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
4064 stream_tag, 0, format);
4065 call_pcm_capture_hook(hinfo, codec, substream,
4066 HDA_GEN_PCM_ACT_PREPARE);
4070 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4071 struct hda_codec *codec,
4072 struct snd_pcm_substream *substream)
4074 struct hda_gen_spec *spec = codec->spec;
4076 snd_hda_codec_cleanup_stream(codec,
4077 spec->adc_nids[substream->number + 1]);
4078 call_pcm_capture_hook(hinfo, codec, substream,
4079 HDA_GEN_PCM_ACT_CLEANUP);
4085 static const struct hda_pcm_stream pcm_analog_playback = {
4089 /* NID is set in build_pcms */
4091 .open = playback_pcm_open,
4092 .close = playback_pcm_close,
4093 .prepare = playback_pcm_prepare,
4094 .cleanup = playback_pcm_cleanup
4098 static const struct hda_pcm_stream pcm_analog_capture = {
4102 /* NID is set in build_pcms */
4104 .open = capture_pcm_open,
4105 .close = capture_pcm_close,
4106 .prepare = capture_pcm_prepare,
4107 .cleanup = capture_pcm_cleanup
4111 static const struct hda_pcm_stream pcm_analog_alt_playback = {
4115 /* NID is set in build_pcms */
4117 .open = alt_playback_pcm_open,
4118 .close = alt_playback_pcm_close,
4119 .prepare = alt_playback_pcm_prepare,
4120 .cleanup = alt_playback_pcm_cleanup
4124 static const struct hda_pcm_stream pcm_analog_alt_capture = {
4125 .substreams = 2, /* can be overridden */
4128 /* NID is set in build_pcms */
4130 .open = alt_capture_pcm_open,
4131 .close = alt_capture_pcm_close,
4132 .prepare = alt_capture_pcm_prepare,
4133 .cleanup = alt_capture_pcm_cleanup
4137 static const struct hda_pcm_stream pcm_digital_playback = {
4141 /* NID is set in build_pcms */
4143 .open = dig_playback_pcm_open,
4144 .close = dig_playback_pcm_close,
4145 .prepare = dig_playback_pcm_prepare,
4146 .cleanup = dig_playback_pcm_cleanup
4150 static const struct hda_pcm_stream pcm_digital_capture = {
4154 /* NID is set in build_pcms */
4157 /* Used by build_pcms to flag that a PCM has no playback stream */
4158 static const struct hda_pcm_stream pcm_null_stream = {
4165 * dynamic changing ADC PCM streams
4167 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4169 struct hda_gen_spec *spec = codec->spec;
4170 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4172 if (spec->cur_adc && spec->cur_adc != new_adc) {
4173 /* stream is running, let's swap the current ADC */
4174 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4175 spec->cur_adc = new_adc;
4176 snd_hda_codec_setup_stream(codec, new_adc,
4177 spec->cur_adc_stream_tag, 0,
4178 spec->cur_adc_format);
4184 /* analog capture with dynamic dual-adc changes */
4185 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4186 struct hda_codec *codec,
4187 unsigned int stream_tag,
4188 unsigned int format,
4189 struct snd_pcm_substream *substream)
4191 struct hda_gen_spec *spec = codec->spec;
4192 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4193 spec->cur_adc_stream_tag = stream_tag;
4194 spec->cur_adc_format = format;
4195 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4199 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4200 struct hda_codec *codec,
4201 struct snd_pcm_substream *substream)
4203 struct hda_gen_spec *spec = codec->spec;
4204 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4209 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4213 .nid = 0, /* fill later */
4215 .prepare = dyn_adc_capture_pcm_prepare,
4216 .cleanup = dyn_adc_capture_pcm_cleanup
4220 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4221 const char *chip_name)
4227 strlcpy(str, chip_name, len);
4229 /* drop non-alnum chars after a space */
4230 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4231 if (!isalnum(p[1])) {
4236 strlcat(str, sfx, len);
4239 /* build PCM streams based on the parsed results */
4240 int snd_hda_gen_build_pcms(struct hda_codec *codec)
4242 struct hda_gen_spec *spec = codec->spec;
4243 struct hda_pcm *info = spec->pcm_rec;
4244 const struct hda_pcm_stream *p;
4245 bool have_multi_adcs;
4247 codec->num_pcms = 1;
4248 codec->pcm_info = info;
4250 if (spec->no_analog)
4253 fill_pcm_stream_name(spec->stream_name_analog,
4254 sizeof(spec->stream_name_analog),
4255 " Analog", codec->chip_name);
4256 info->name = spec->stream_name_analog;
4258 if (spec->multiout.num_dacs > 0) {
4259 p = spec->stream_analog_playback;
4261 p = &pcm_analog_playback;
4262 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4263 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4264 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4265 spec->multiout.max_channels;
4266 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4267 spec->autocfg.line_outs == 2)
4268 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4271 if (spec->num_adc_nids) {
4272 p = spec->stream_analog_capture;
4274 if (spec->dyn_adc_switch)
4275 p = &dyn_adc_pcm_analog_capture;
4277 p = &pcm_analog_capture;
4279 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4280 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4284 /* SPDIF for stream index #1 */
4285 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
4286 fill_pcm_stream_name(spec->stream_name_digital,
4287 sizeof(spec->stream_name_digital),
4288 " Digital", codec->chip_name);
4289 codec->num_pcms = 2;
4290 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4291 info = spec->pcm_rec + 1;
4292 info->name = spec->stream_name_digital;
4293 if (spec->dig_out_type)
4294 info->pcm_type = spec->dig_out_type;
4296 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4297 if (spec->multiout.dig_out_nid) {
4298 p = spec->stream_digital_playback;
4300 p = &pcm_digital_playback;
4301 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4302 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4304 if (spec->dig_in_nid) {
4305 p = spec->stream_digital_capture;
4307 p = &pcm_digital_capture;
4308 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4309 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4313 if (spec->no_analog)
4316 /* If the use of more than one ADC is requested for the current
4317 * model, configure a second analog capture-only PCM.
4319 have_multi_adcs = (spec->num_adc_nids > 1) &&
4320 !spec->dyn_adc_switch && !spec->auto_mic;
4321 /* Additional Analaog capture for index #2 */
4322 if (spec->alt_dac_nid || have_multi_adcs) {
4323 codec->num_pcms = 3;
4324 info = spec->pcm_rec + 2;
4325 info->name = spec->stream_name_analog;
4326 if (spec->alt_dac_nid) {
4327 p = spec->stream_analog_alt_playback;
4329 p = &pcm_analog_alt_playback;
4330 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4331 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4334 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4336 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4338 if (have_multi_adcs) {
4339 p = spec->stream_analog_alt_capture;
4341 p = &pcm_analog_alt_capture;
4342 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4343 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4345 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4346 spec->num_adc_nids - 1;
4348 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4350 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4356 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4360 * Standard auto-parser initializations
4363 /* configure the given path as a proper output */
4364 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
4366 struct nid_path *path;
4369 path = snd_hda_get_path_from_idx(codec, path_idx);
4370 if (!path || !path->depth)
4372 pin = path->path[path->depth - 1];
4373 restore_pin_ctl(codec, pin);
4374 snd_hda_activate_path(codec, path, path->active, true);
4375 set_pin_eapd(codec, pin, path->active);
4378 /* initialize primary output paths */
4379 static void init_multi_out(struct hda_codec *codec)
4381 struct hda_gen_spec *spec = codec->spec;
4384 for (i = 0; i < spec->autocfg.line_outs; i++)
4385 set_output_and_unmute(codec, spec->out_paths[i]);
4389 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
4393 for (i = 0; i < num_outs; i++)
4394 set_output_and_unmute(codec, paths[i]);
4397 /* initialize hp and speaker paths */
4398 static void init_extra_out(struct hda_codec *codec)
4400 struct hda_gen_spec *spec = codec->spec;
4402 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
4403 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
4404 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4405 __init_extra_out(codec, spec->autocfg.speaker_outs,
4406 spec->speaker_paths);
4409 /* initialize multi-io paths */
4410 static void init_multi_io(struct hda_codec *codec)
4412 struct hda_gen_spec *spec = codec->spec;
4415 for (i = 0; i < spec->multi_ios; i++) {
4416 hda_nid_t pin = spec->multi_io[i].pin;
4417 struct nid_path *path;
4418 path = get_multiio_path(codec, i);
4421 if (!spec->multi_io[i].ctl_in)
4422 spec->multi_io[i].ctl_in =
4423 snd_hda_codec_get_pin_target(codec, pin);
4424 snd_hda_activate_path(codec, path, path->active, true);
4428 /* set up input pins and loopback paths */
4429 static void init_analog_input(struct hda_codec *codec)
4431 struct hda_gen_spec *spec = codec->spec;
4432 struct auto_pin_cfg *cfg = &spec->autocfg;
4435 for (i = 0; i < cfg->num_inputs; i++) {
4436 hda_nid_t nid = cfg->inputs[i].pin;
4437 if (is_input_pin(codec, nid))
4438 restore_pin_ctl(codec, nid);
4440 /* init loopback inputs */
4441 if (spec->mixer_nid) {
4442 struct nid_path *path;
4443 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
4445 snd_hda_activate_path(codec, path,
4446 path->active, false);
4451 /* initialize ADC paths */
4452 static void init_input_src(struct hda_codec *codec)
4454 struct hda_gen_spec *spec = codec->spec;
4455 struct hda_input_mux *imux = &spec->input_mux;
4456 struct nid_path *path;
4459 if (spec->dyn_adc_switch)
4462 nums = spec->num_adc_nids;
4464 for (c = 0; c < nums; c++) {
4465 for (i = 0; i < imux->num_items; i++) {
4466 path = get_input_path(codec, c, i);
4468 bool active = path->active;
4469 if (i == spec->cur_mux[c])
4471 snd_hda_activate_path(codec, path, active, false);
4476 if (spec->shared_mic_hp)
4477 update_shared_mic_hp(codec, spec->cur_mux[0]);
4479 if (spec->cap_sync_hook)
4480 spec->cap_sync_hook(codec);
4483 /* set right pin controls for digital I/O */
4484 static void init_digital(struct hda_codec *codec)
4486 struct hda_gen_spec *spec = codec->spec;
4490 for (i = 0; i < spec->autocfg.dig_outs; i++)
4491 set_output_and_unmute(codec, spec->digout_paths[i]);
4492 pin = spec->autocfg.dig_in_pin;
4494 struct nid_path *path;
4495 restore_pin_ctl(codec, pin);
4496 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4498 snd_hda_activate_path(codec, path, path->active, false);
4502 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4503 * invalid unsol tags by some reason
4505 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4509 for (i = 0; i < codec->init_pins.used; i++) {
4510 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4511 hda_nid_t nid = pin->nid;
4512 if (is_jack_detectable(codec, nid) &&
4513 !snd_hda_jack_tbl_get(codec, nid))
4514 snd_hda_codec_update_cache(codec, nid, 0,
4515 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4520 * initialize the generic spec;
4521 * this can be put as patch_ops.init function
4523 int snd_hda_gen_init(struct hda_codec *codec)
4525 struct hda_gen_spec *spec = codec->spec;
4527 if (spec->init_hook)
4528 spec->init_hook(codec);
4530 snd_hda_apply_verbs(codec);
4532 codec->cached_write = 1;
4534 init_multi_out(codec);
4535 init_extra_out(codec);
4536 init_multi_io(codec);
4537 init_analog_input(codec);
4538 init_input_src(codec);
4539 init_digital(codec);
4541 clear_unsol_on_unused_pins(codec);
4543 /* call init functions of standard auto-mute helpers */
4544 update_automute_all(codec);
4546 snd_hda_codec_flush_amp_cache(codec);
4547 snd_hda_codec_flush_cmd_cache(codec);
4549 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4550 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4552 hda_call_check_power_status(codec, 0x01);
4555 EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4558 * free the generic spec;
4559 * this can be put as patch_ops.free function
4561 void snd_hda_gen_free(struct hda_codec *codec)
4563 snd_hda_gen_spec_free(codec->spec);
4567 EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4571 * check the loopback power save state;
4572 * this can be put as patch_ops.check_power_status function
4574 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4576 struct hda_gen_spec *spec = codec->spec;
4577 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4579 EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4584 * the generic codec support
4587 static const struct hda_codec_ops generic_patch_ops = {
4588 .build_controls = snd_hda_gen_build_controls,
4589 .build_pcms = snd_hda_gen_build_pcms,
4590 .init = snd_hda_gen_init,
4591 .free = snd_hda_gen_free,
4592 .unsol_event = snd_hda_jack_unsol_event,
4594 .check_power_status = snd_hda_gen_check_power_status,
4598 int snd_hda_parse_generic_codec(struct hda_codec *codec)
4600 struct hda_gen_spec *spec;
4603 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4606 snd_hda_gen_spec_init(spec);
4609 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4613 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
4617 codec->patch_ops = generic_patch_ops;
4621 snd_hda_gen_free(codec);
4624 EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);