2 * Universal Interface for Intel High Definition Audio Codec
4 * Generic widget tree parser
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/sort.h>
27 #include <linux/ctype.h>
28 #include <linux/string.h>
29 #include <sound/core.h>
30 #include <sound/jack.h>
31 #include "hda_codec.h"
32 #include "hda_local.h"
33 #include "hda_auto_parser.h"
35 #include "hda_generic.h"
38 /* initialize hda_gen_spec struct */
39 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
41 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
42 snd_array_init(&spec->bind_ctls, sizeof(struct hda_bind_ctls *), 8);
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 static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
80 struct hda_ctl_ops *ops)
82 struct hda_gen_spec *spec = codec->spec;
83 struct hda_bind_ctls **ctlp, *ctl;
84 ctlp = snd_array_new(&spec->bind_ctls);
87 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
94 static void free_bind_ctls(struct hda_gen_spec *spec)
96 if (spec->bind_ctls.list) {
97 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
99 for (i = 0; i < spec->bind_ctls.used; i++)
102 snd_array_free(&spec->bind_ctls);
105 void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
110 free_bind_ctls(spec);
111 snd_array_free(&spec->paths);
113 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
119 static struct nid_path *get_nid_path(struct hda_codec *codec,
120 hda_nid_t from_nid, hda_nid_t to_nid,
123 struct hda_gen_spec *spec = codec->spec;
126 for (i = 0; i < spec->paths.used; i++) {
127 struct nid_path *path = snd_array_elem(&spec->paths, i);
128 if (path->depth <= 0)
130 if ((!from_nid || path->path[0] == from_nid) &&
131 (!to_nid || path->path[path->depth - 1] == to_nid)) {
132 if (with_aa_mix == HDA_PARSE_ALL ||
133 path->with_aa_mix == with_aa_mix)
140 /* get the path between the given NIDs;
141 * passing 0 to either @pin or @dac behaves as a wildcard
143 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
144 hda_nid_t from_nid, hda_nid_t to_nid)
146 return get_nid_path(codec, from_nid, to_nid, HDA_PARSE_ALL);
148 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
150 /* get the index number corresponding to the path instance;
151 * the index starts from 1, for easier checking the invalid value
153 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
155 struct hda_gen_spec *spec = codec->spec;
156 struct nid_path *array = spec->paths.list;
159 if (!spec->paths.used)
162 if (idx < 0 || idx >= spec->paths.used)
167 /* get the path instance corresponding to the given index number */
168 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
170 struct hda_gen_spec *spec = codec->spec;
172 if (idx <= 0 || idx > spec->paths.used)
174 return snd_array_elem(&spec->paths, idx - 1);
177 /* check whether the given DAC is already found in any existing paths */
178 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
180 struct hda_gen_spec *spec = codec->spec;
183 for (i = 0; i < spec->paths.used; i++) {
184 struct nid_path *path = snd_array_elem(&spec->paths, i);
185 if (path->path[0] == nid)
191 /* check whether the given two widgets can be connected */
192 static bool is_reachable_path(struct hda_codec *codec,
193 hda_nid_t from_nid, hda_nid_t to_nid)
195 if (!from_nid || !to_nid)
197 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
200 /* nid, dir and idx */
201 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
203 /* check whether the given ctl is already assigned in any path elements */
204 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
206 struct hda_gen_spec *spec = codec->spec;
209 val &= AMP_VAL_COMPARE_MASK;
210 for (i = 0; i < spec->paths.used; i++) {
211 struct nid_path *path = snd_array_elem(&spec->paths, i);
212 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
218 /* check whether a control with the given (nid, dir, idx) was assigned */
219 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
222 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
223 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
224 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
227 static void print_nid_path(const char *pfx, struct nid_path *path)
234 for (i = 0; i < path->depth; i++) {
236 sprintf(tmp, ":%02x", path->path[i]);
237 strlcat(buf, tmp, sizeof(buf));
239 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
242 /* called recursively */
243 static bool __parse_nid_path(struct hda_codec *codec,
244 hda_nid_t from_nid, hda_nid_t to_nid,
245 int with_aa_mix, struct nid_path *path, int depth)
247 struct hda_gen_spec *spec = codec->spec;
248 const hda_nid_t *conn;
251 if (to_nid == spec->mixer_nid) {
252 if (with_aa_mix == HDA_PARSE_NO_AAMIX)
254 with_aa_mix = HDA_PARSE_ALL; /* mark aa-mix is included */
257 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
258 for (i = 0; i < nums; i++) {
259 if (conn[i] != from_nid) {
260 /* special case: when from_nid is 0,
261 * try to find an empty DAC
264 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
265 is_dac_already_used(codec, conn[i]))
268 /* aa-mix is requested but not included? */
269 if (!(spec->mixer_nid && with_aa_mix == HDA_PARSE_ONLY_AAMIX))
272 if (depth >= MAX_NID_PATH_DEPTH)
274 for (i = 0; i < nums; i++) {
276 type = get_wcaps_type(get_wcaps(codec, conn[i]));
277 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
280 if (__parse_nid_path(codec, from_nid, conn[i],
281 with_aa_mix, path, depth + 1))
287 path->path[path->depth] = conn[i];
288 if (conn[i] == spec->mixer_nid)
289 path->with_aa_mix = true;
290 path->idx[path->depth + 1] = i;
291 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
292 path->multi[path->depth + 1] = 1;
297 /* parse the widget path from the given nid to the target nid;
298 * when @from_nid is 0, try to find an empty DAC;
299 * when @with_aa_mix is HDA_PARSE_NO_AAMIX, paths with spec->mixer_nid are
300 * excluded, only the paths that don't go through the mixer will be chosen.
301 * when @with_aa_mix is HDA_PARSE_ONLY_AAMIX, only the paths going through
302 * spec->mixer_nid will be chosen.
303 * when @with_aa_mix is HDA_PARSE_ALL, no special handling about mixer widget.
305 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
306 hda_nid_t to_nid, int with_aa_mix,
307 struct nid_path *path)
309 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
310 path->path[path->depth] = to_nid;
316 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
319 * parse the path between the given NIDs and add to the path list.
320 * if no valid path is found, return NULL
323 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
324 hda_nid_t to_nid, int with_aa_mix)
326 struct hda_gen_spec *spec = codec->spec;
327 struct nid_path *path;
329 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
332 /* check whether the path has been already added */
333 path = get_nid_path(codec, from_nid, to_nid, with_aa_mix);
337 path = snd_array_new(&spec->paths);
340 memset(path, 0, sizeof(*path));
341 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
347 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
349 /* look for an empty DAC slot */
350 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
353 struct hda_gen_spec *spec = codec->spec;
357 for (i = 0; i < spec->num_all_dacs; i++) {
358 hda_nid_t nid = spec->all_dacs[i];
359 if (!nid || is_dac_already_used(codec, nid))
361 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
362 if (is_digital != cap_digital)
364 if (is_reachable_path(codec, nid, pin))
370 /* replace the channels in the composed amp value with the given number */
371 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
373 val &= ~(0x3U << 16);
378 /* check whether the widget has the given amp capability for the direction */
379 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
380 int dir, unsigned int bits)
384 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
385 if (query_amp_caps(codec, nid, dir) & bits)
390 #define nid_has_mute(codec, nid, dir) \
391 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
392 #define nid_has_volume(codec, nid, dir) \
393 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
395 /* look for a widget suitable for assigning a mute switch in the path */
396 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
397 struct nid_path *path)
401 for (i = path->depth - 1; i >= 0; i--) {
402 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
403 return path->path[i];
404 if (i != path->depth - 1 && i != 0 &&
405 nid_has_mute(codec, path->path[i], HDA_INPUT))
406 return path->path[i];
411 /* look for a widget suitable for assigning a volume ctl in the path */
412 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
413 struct nid_path *path)
417 for (i = path->depth - 1; i >= 0; i--) {
418 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
419 return path->path[i];
425 * path activation / deactivation
428 /* can have the amp-in capability? */
429 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
431 hda_nid_t nid = path->path[idx];
432 unsigned int caps = get_wcaps(codec, nid);
433 unsigned int type = get_wcaps_type(caps);
435 if (!(caps & AC_WCAP_IN_AMP))
437 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
442 /* can have the amp-out capability? */
443 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
445 hda_nid_t nid = path->path[idx];
446 unsigned int caps = get_wcaps(codec, nid);
447 unsigned int type = get_wcaps_type(caps);
449 if (!(caps & AC_WCAP_OUT_AMP))
451 if (type == AC_WID_PIN && !idx) /* only for output pins */
456 /* check whether the given (nid,dir,idx) is active */
457 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
458 unsigned int idx, unsigned int dir)
460 struct hda_gen_spec *spec = codec->spec;
463 for (n = 0; n < spec->paths.used; n++) {
464 struct nid_path *path = snd_array_elem(&spec->paths, n);
467 for (i = 0; i < path->depth; i++) {
468 if (path->path[i] == nid) {
469 if (dir == HDA_OUTPUT || path->idx[i] == idx)
478 /* get the default amp value for the target state */
479 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
480 int dir, bool enable)
483 unsigned int val = 0;
485 caps = query_amp_caps(codec, nid, dir);
486 if (caps & AC_AMPCAP_NUM_STEPS) {
489 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
491 if (caps & AC_AMPCAP_MUTE) {
498 /* initialize the amp value (only at the first time) */
499 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
501 int val = get_amp_val_to_activate(codec, nid, dir, false);
502 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
505 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
506 int idx, bool enable)
509 if (is_ctl_associated(codec, nid, dir, idx) ||
510 (!enable && is_active_nid(codec, nid, dir, idx)))
512 val = get_amp_val_to_activate(codec, nid, dir, enable);
513 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
516 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
519 hda_nid_t nid = path->path[i];
520 init_amp(codec, nid, HDA_OUTPUT, 0);
521 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
524 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
525 int i, bool enable, bool add_aamix)
527 struct hda_gen_spec *spec = codec->spec;
528 const hda_nid_t *conn;
531 hda_nid_t nid = path->path[i];
533 nums = snd_hda_get_conn_list(codec, nid, &conn);
534 type = get_wcaps_type(get_wcaps(codec, nid));
535 if (type == AC_WID_PIN ||
536 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
542 for (n = 0; n < nums; n++)
543 init_amp(codec, nid, HDA_INPUT, n);
545 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
548 /* here is a little bit tricky in comparison with activate_amp_out();
549 * when aa-mixer is available, we need to enable the path as well
551 for (n = 0; n < nums; n++) {
552 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
554 activate_amp(codec, nid, HDA_INPUT, n, enable);
558 /* activate or deactivate the given path
559 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
561 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
562 bool enable, bool add_aamix)
567 path->active = false;
569 for (i = path->depth - 1; i >= 0; i--) {
570 if (enable && path->multi[i])
571 snd_hda_codec_write_cache(codec, path->path[i], 0,
572 AC_VERB_SET_CONNECT_SEL,
574 if (has_amp_in(codec, path, i))
575 activate_amp_in(codec, path, i, enable, add_aamix);
576 if (has_amp_out(codec, path, i))
577 activate_amp_out(codec, path, i, enable);
583 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
585 /* turn on/off EAPD on the given pin */
586 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
588 struct hda_gen_spec *spec = codec->spec;
589 if (spec->own_eapd_ctl ||
590 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
594 snd_hda_codec_update_cache(codec, pin, 0,
595 AC_VERB_SET_EAPD_BTLENABLE,
596 enable ? 0x02 : 0x00);
601 * Helper functions for creating mixer ctl elements
611 static const struct snd_kcontrol_new control_templates[] = {
612 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
613 HDA_CODEC_MUTE(NULL, 0, 0, 0),
614 HDA_BIND_MUTE(NULL, 0, 0, 0),
615 HDA_BIND_VOL(NULL, 0),
616 HDA_BIND_SW(NULL, 0),
619 /* add dynamic controls from template */
620 static int add_control(struct hda_gen_spec *spec, int type, const char *name,
621 int cidx, unsigned long val)
623 struct snd_kcontrol_new *knew;
625 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
629 if (get_amp_nid_(val))
630 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
631 knew->private_value = val;
635 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
636 const char *pfx, const char *dir,
637 const char *sfx, int cidx, unsigned long val)
640 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
641 return add_control(spec, type, name, cidx, val);
644 #define add_pb_vol_ctrl(spec, type, pfx, val) \
645 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
646 #define add_pb_sw_ctrl(spec, type, pfx, val) \
647 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
648 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
649 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
650 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
651 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
653 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
654 unsigned int chs, struct nid_path *path)
659 val = path->ctls[NID_PATH_VOL_CTL];
662 val = amp_val_replace_channels(val, chs);
663 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
666 /* return the channel bits suitable for the given path->ctls[] */
667 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
670 int chs = 1; /* mono (left only) */
672 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
673 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
674 chs = 3; /* stereo */
679 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
680 struct nid_path *path)
682 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
683 return add_vol_ctl(codec, pfx, cidx, chs, path);
686 /* create a mute-switch for the given mixer widget;
687 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
689 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
690 unsigned int chs, struct nid_path *path)
693 int type = HDA_CTL_WIDGET_MUTE;
697 val = path->ctls[NID_PATH_MUTE_CTL];
700 val = amp_val_replace_channels(val, chs);
701 if (get_amp_direction_(val) == HDA_INPUT) {
702 hda_nid_t nid = get_amp_nid_(val);
703 int nums = snd_hda_get_num_conns(codec, nid);
705 type = HDA_CTL_BIND_MUTE;
709 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
712 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
713 int cidx, struct nid_path *path)
715 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
716 return add_sw_ctl(codec, pfx, cidx, chs, path);
719 static const char * const channel_name[4] = {
720 "Front", "Surround", "CLFE", "Side"
723 /* give some appropriate ctl name prefix for the given line out channel */
724 static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
725 bool can_be_master, int *index)
727 struct auto_pin_cfg *cfg = &spec->autocfg;
730 if (cfg->line_outs == 1 && !spec->multi_ios &&
731 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
732 return spec->vmaster_mute.hook ? "PCM" : "Master";
734 /* if there is really a single DAC used in the whole output paths,
735 * use it master (or "PCM" if a vmaster hook is present)
737 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
738 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
739 return spec->vmaster_mute.hook ? "PCM" : "Master";
741 switch (cfg->line_out_type) {
742 case AUTO_PIN_SPEAKER_OUT:
743 if (cfg->line_outs == 1)
745 if (cfg->line_outs == 2)
746 return ch ? "Bass Speaker" : "Speaker";
748 case AUTO_PIN_HP_OUT:
749 /* for multi-io case, only the primary out */
750 if (ch && spec->multi_ios)
755 if (cfg->line_outs == 1 && !spec->multi_ios)
759 if (ch >= ARRAY_SIZE(channel_name)) {
764 return channel_name[ch];
771 /* badness definition */
773 /* No primary DAC is found for the main output */
774 BAD_NO_PRIMARY_DAC = 0x10000,
775 /* No DAC is found for the extra output */
777 /* No possible multi-ios */
778 BAD_MULTI_IO = 0x103,
779 /* No individual DAC for extra output */
780 BAD_NO_EXTRA_DAC = 0x102,
781 /* No individual DAC for extra surrounds */
782 BAD_NO_EXTRA_SURR_DAC = 0x101,
783 /* Primary DAC shared with main surrounds */
784 BAD_SHARED_SURROUND = 0x100,
785 /* Primary DAC shared with main CLFE */
786 BAD_SHARED_CLFE = 0x10,
787 /* Primary DAC shared with extra surrounds */
788 BAD_SHARED_EXTRA_SURROUND = 0x10,
789 /* Volume widget is shared */
790 BAD_SHARED_VOL = 0x10,
793 /* look for widgets in the path between the given NIDs appropriate for
794 * volume and mute controls, and assign the values to ctls[].
796 * When no appropriate widget is found in the path, the badness value
797 * is incremented depending on the situation. The function returns the
798 * total badness for both volume and mute controls.
800 static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
803 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
809 return BAD_SHARED_VOL * 2;
810 nid = look_for_out_vol_nid(codec, path);
812 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
813 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
814 badness += BAD_SHARED_VOL;
816 path->ctls[NID_PATH_VOL_CTL] = val;
818 badness += BAD_SHARED_VOL;
819 nid = look_for_out_mute_nid(codec, path);
821 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
822 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
823 nid_has_mute(codec, nid, HDA_OUTPUT))
824 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
826 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
827 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
828 badness += BAD_SHARED_VOL;
830 path->ctls[NID_PATH_MUTE_CTL] = val;
832 badness += BAD_SHARED_VOL;
836 struct badness_table {
837 int no_primary_dac; /* no primary DAC */
838 int no_dac; /* no secondary DACs */
839 int shared_primary; /* primary DAC is shared with main output */
840 int shared_surr; /* secondary DAC shared with main or primary */
841 int shared_clfe; /* third DAC shared with main or primary */
842 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
845 static struct badness_table main_out_badness = {
846 .no_primary_dac = BAD_NO_PRIMARY_DAC,
847 .no_dac = BAD_NO_DAC,
848 .shared_primary = BAD_NO_PRIMARY_DAC,
849 .shared_surr = BAD_SHARED_SURROUND,
850 .shared_clfe = BAD_SHARED_CLFE,
851 .shared_surr_main = BAD_SHARED_SURROUND,
854 static struct badness_table extra_out_badness = {
855 .no_primary_dac = BAD_NO_DAC,
856 .no_dac = BAD_NO_DAC,
857 .shared_primary = BAD_NO_EXTRA_DAC,
858 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
859 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
860 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
863 /* try to assign DACs to pins and return the resultant badness */
864 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
865 const hda_nid_t *pins, hda_nid_t *dacs,
867 const struct badness_table *bad)
869 struct hda_gen_spec *spec = codec->spec;
870 struct auto_pin_cfg *cfg = &spec->autocfg;
878 for (i = 0; i < num_outs; i++) {
879 struct nid_path *path;
880 hda_nid_t pin = pins[i];
883 badness += assign_out_path_ctls(codec, pin, dacs[i]);
887 dacs[i] = look_for_dac(codec, pin, false);
888 if (!dacs[i] && !i) {
889 for (j = 1; j < num_outs; j++) {
890 if (is_reachable_path(codec, dacs[j], pin)) {
900 if (is_reachable_path(codec, dacs[0], pin))
902 else if (cfg->line_outs > i &&
903 is_reachable_path(codec, spec->private_dac_nids[i], pin))
904 dac = spec->private_dac_nids[i];
907 badness += bad->shared_primary;
909 badness += bad->shared_surr;
911 badness += bad->shared_clfe;
912 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
913 dac = spec->private_dac_nids[0];
914 badness += bad->shared_surr_main;
916 badness += bad->no_primary_dac;
918 badness += bad->no_dac;
920 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_NO_AAMIX);
921 if (!path && i > 0 && spec->mixer_nid) {
923 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_ALL);
928 print_nid_path("output", path);
930 path_idx[i] = snd_hda_get_path_idx(codec, path);
933 badness += assign_out_path_ctls(codec, pin, dac);
939 /* return NID if the given pin has only a single connection to a certain DAC */
940 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
942 struct hda_gen_spec *spec = codec->spec;
944 hda_nid_t nid_found = 0;
946 for (i = 0; i < spec->num_all_dacs; i++) {
947 hda_nid_t nid = spec->all_dacs[i];
948 if (!nid || is_dac_already_used(codec, nid))
950 if (is_reachable_path(codec, nid, pin)) {
959 /* check whether the given pin can be a multi-io pin */
960 static bool can_be_multiio_pin(struct hda_codec *codec,
961 unsigned int location, hda_nid_t nid)
963 unsigned int defcfg, caps;
965 defcfg = snd_hda_codec_get_pincfg(codec, nid);
966 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
968 if (location && get_defcfg_location(defcfg) != location)
970 caps = snd_hda_query_pin_caps(codec, nid);
971 if (!(caps & AC_PINCAP_OUT))
976 /* count the number of input pins that are capable to be multi-io */
977 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
979 struct hda_gen_spec *spec = codec->spec;
980 struct auto_pin_cfg *cfg = &spec->autocfg;
981 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
982 unsigned int location = get_defcfg_location(defcfg);
986 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
987 for (i = 0; i < cfg->num_inputs; i++) {
988 if (cfg->inputs[i].type != type)
990 if (can_be_multiio_pin(codec, location,
1001 * When hardwired is set, try to fill ony hardwired pins, and returns
1002 * zero if any pins are filled, non-zero if nothing found.
1003 * When hardwired is off, try to fill possible input pins, and returns
1004 * the badness value.
1006 static int fill_multi_ios(struct hda_codec *codec,
1007 hda_nid_t reference_pin,
1010 struct hda_gen_spec *spec = codec->spec;
1011 struct auto_pin_cfg *cfg = &spec->autocfg;
1012 int type, i, j, num_pins, old_pins;
1013 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1014 unsigned int location = get_defcfg_location(defcfg);
1017 old_pins = spec->multi_ios;
1021 num_pins = count_multiio_pins(codec, reference_pin);
1025 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1026 for (i = 0; i < cfg->num_inputs; i++) {
1027 struct nid_path *path;
1028 hda_nid_t nid = cfg->inputs[i].pin;
1031 if (cfg->inputs[i].type != type)
1033 if (!can_be_multiio_pin(codec, location, nid))
1035 for (j = 0; j < spec->multi_ios; j++) {
1036 if (nid == spec->multi_io[j].pin)
1039 if (j < spec->multi_ios)
1043 dac = get_dac_if_single(codec, nid);
1045 dac = look_for_dac(codec, nid, false);
1050 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
1055 print_nid_path("multiio", path);
1056 spec->multi_io[spec->multi_ios].pin = nid;
1057 spec->multi_io[spec->multi_ios].dac = dac;
1058 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1059 snd_hda_get_path_idx(codec, path);
1061 if (spec->multi_ios >= 2)
1067 badness = BAD_MULTI_IO;
1068 if (old_pins == spec->multi_ios) {
1070 return 1; /* nothing found */
1072 return badness; /* no badness if nothing found */
1074 if (!hardwired && spec->multi_ios < 2) {
1075 /* cancel newly assigned paths */
1076 spec->paths.used -= spec->multi_ios - old_pins;
1077 spec->multi_ios = old_pins;
1081 /* assign volume and mute controls */
1082 for (i = old_pins; i < spec->multi_ios; i++)
1083 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1084 spec->multi_io[i].dac);
1089 /* map DACs for all pins in the list if they are single connections */
1090 static bool map_singles(struct hda_codec *codec, int outs,
1091 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1093 struct hda_gen_spec *spec = codec->spec;
1096 for (i = 0; i < outs; i++) {
1097 struct nid_path *path;
1101 dac = get_dac_if_single(codec, pins[i]);
1104 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
1105 if (!path && i > 0 && spec->mixer_nid)
1106 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
1110 print_nid_path("output", path);
1111 path->active = true;
1112 path_idx[i] = snd_hda_get_path_idx(codec, path);
1118 /* fill in the dac_nids table from the parsed pin configuration */
1119 static int fill_and_eval_dacs(struct hda_codec *codec,
1120 bool fill_hardwired,
1121 bool fill_mio_first)
1123 struct hda_gen_spec *spec = codec->spec;
1124 struct auto_pin_cfg *cfg = &spec->autocfg;
1125 int i, err, badness;
1127 /* set num_dacs once to full for look_for_dac() */
1128 spec->multiout.num_dacs = cfg->line_outs;
1129 spec->multiout.dac_nids = spec->private_dac_nids;
1130 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1131 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1132 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1133 spec->multi_ios = 0;
1134 snd_array_free(&spec->paths);
1137 /* fill hard-wired DACs first */
1138 if (fill_hardwired) {
1141 mapped = map_singles(codec, cfg->line_outs,
1143 spec->private_dac_nids,
1145 mapped |= map_singles(codec, cfg->hp_outs,
1147 spec->multiout.hp_out_nid,
1149 mapped |= map_singles(codec, cfg->speaker_outs,
1151 spec->multiout.extra_out_nid,
1152 spec->speaker_paths);
1153 if (fill_mio_first && cfg->line_outs == 1 &&
1154 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1155 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1162 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1163 spec->private_dac_nids, spec->out_paths,
1166 /* re-count num_dacs and squash invalid entries */
1167 spec->multiout.num_dacs = 0;
1168 for (i = 0; i < cfg->line_outs; i++) {
1169 if (spec->private_dac_nids[i])
1170 spec->multiout.num_dacs++;
1172 memmove(spec->private_dac_nids + i,
1173 spec->private_dac_nids + i + 1,
1174 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1175 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1179 if (fill_mio_first &&
1180 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1181 /* try to fill multi-io first */
1182 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1185 /* we don't count badness at this stage yet */
1188 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1189 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1190 spec->multiout.hp_out_nid,
1192 &extra_out_badness);
1197 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1198 err = try_assign_dacs(codec, cfg->speaker_outs,
1200 spec->multiout.extra_out_nid,
1201 spec->speaker_paths,
1202 &extra_out_badness);
1207 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1208 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1214 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1215 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1216 spec->multi_ios = 1; /* give badness */
1218 if (spec->multi_ios == 2) {
1219 for (i = 0; i < 2; i++)
1220 spec->private_dac_nids[spec->multiout.num_dacs++] =
1221 spec->multi_io[i].dac;
1222 spec->ext_channel_count = 2;
1223 } else if (spec->multi_ios) {
1224 spec->multi_ios = 0;
1225 badness += BAD_MULTI_IO;
1231 #define DEBUG_BADNESS
1233 #ifdef DEBUG_BADNESS
1234 #define debug_badness snd_printdd
1236 #define debug_badness(...)
1239 static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1241 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1242 cfg->line_out_pins[0], cfg->line_out_pins[1],
1243 cfg->line_out_pins[2], cfg->line_out_pins[3],
1244 spec->multiout.dac_nids[0],
1245 spec->multiout.dac_nids[1],
1246 spec->multiout.dac_nids[2],
1247 spec->multiout.dac_nids[3]);
1248 if (spec->multi_ios > 0)
1249 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1251 spec->multi_io[0].pin, spec->multi_io[1].pin,
1252 spec->multi_io[0].dac, spec->multi_io[1].dac);
1253 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1254 cfg->hp_pins[0], cfg->hp_pins[1],
1255 cfg->hp_pins[2], cfg->hp_pins[3],
1256 spec->multiout.hp_out_nid[0],
1257 spec->multiout.hp_out_nid[1],
1258 spec->multiout.hp_out_nid[2],
1259 spec->multiout.hp_out_nid[3]);
1260 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1261 cfg->speaker_pins[0], cfg->speaker_pins[1],
1262 cfg->speaker_pins[2], cfg->speaker_pins[3],
1263 spec->multiout.extra_out_nid[0],
1264 spec->multiout.extra_out_nid[1],
1265 spec->multiout.extra_out_nid[2],
1266 spec->multiout.extra_out_nid[3]);
1269 /* find all available DACs of the codec */
1270 static void fill_all_dac_nids(struct hda_codec *codec)
1272 struct hda_gen_spec *spec = codec->spec;
1274 hda_nid_t nid = codec->start_nid;
1276 spec->num_all_dacs = 0;
1277 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1278 for (i = 0; i < codec->num_nodes; i++, nid++) {
1279 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1281 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1282 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1285 spec->all_dacs[spec->num_all_dacs++] = nid;
1289 static int parse_output_paths(struct hda_codec *codec)
1291 struct hda_gen_spec *spec = codec->spec;
1292 struct auto_pin_cfg *cfg = &spec->autocfg;
1293 struct auto_pin_cfg *best_cfg;
1294 int best_badness = INT_MAX;
1296 bool fill_hardwired = true, fill_mio_first = true;
1297 bool best_wired = true, best_mio = true;
1298 bool hp_spk_swapped = false;
1300 fill_all_dac_nids(codec);
1302 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1308 badness = fill_and_eval_dacs(codec, fill_hardwired,
1314 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1315 cfg->line_out_type, fill_hardwired, fill_mio_first,
1317 debug_show_configs(spec, cfg);
1318 if (badness < best_badness) {
1319 best_badness = badness;
1321 best_wired = fill_hardwired;
1322 best_mio = fill_mio_first;
1326 fill_mio_first = !fill_mio_first;
1327 if (!fill_mio_first)
1329 fill_hardwired = !fill_hardwired;
1330 if (!fill_hardwired)
1334 hp_spk_swapped = true;
1335 if (cfg->speaker_outs > 0 &&
1336 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1337 cfg->hp_outs = cfg->line_outs;
1338 memcpy(cfg->hp_pins, cfg->line_out_pins,
1339 sizeof(cfg->hp_pins));
1340 cfg->line_outs = cfg->speaker_outs;
1341 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1342 sizeof(cfg->speaker_pins));
1343 cfg->speaker_outs = 0;
1344 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1345 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1346 fill_hardwired = true;
1349 if (cfg->hp_outs > 0 &&
1350 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1351 cfg->speaker_outs = cfg->line_outs;
1352 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1353 sizeof(cfg->speaker_pins));
1354 cfg->line_outs = cfg->hp_outs;
1355 memcpy(cfg->line_out_pins, cfg->hp_pins,
1356 sizeof(cfg->hp_pins));
1358 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1359 cfg->line_out_type = AUTO_PIN_HP_OUT;
1360 fill_hardwired = true;
1367 debug_badness("==> restoring best_cfg\n");
1369 fill_and_eval_dacs(codec, best_wired, best_mio);
1371 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1372 cfg->line_out_type, best_wired, best_mio);
1373 debug_show_configs(spec, cfg);
1375 if (cfg->line_out_pins[0]) {
1376 struct nid_path *path;
1377 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1379 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1386 /* add playback controls from the parsed DAC table */
1387 static int create_multi_out_ctls(struct hda_codec *codec,
1388 const struct auto_pin_cfg *cfg)
1390 struct hda_gen_spec *spec = codec->spec;
1391 int i, err, noutputs;
1393 noutputs = cfg->line_outs;
1394 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1395 noutputs += spec->multi_ios;
1397 for (i = 0; i < noutputs; i++) {
1401 struct nid_path *path;
1403 dac = spec->multiout.dac_nids[i];
1406 if (i >= cfg->line_outs) {
1408 name = channel_name[i];
1410 name = get_line_out_pfx(spec, i, true, &index);
1413 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1416 if (!name || !strcmp(name, "CLFE")) {
1418 err = add_vol_ctl(codec, "Center", 0, 1, path);
1421 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1424 err = add_sw_ctl(codec, "Center", 0, 1, path);
1427 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1431 err = add_stereo_vol(codec, name, index, path);
1434 err = add_stereo_sw(codec, name, index, path);
1442 static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1443 hda_nid_t dac, int path_idx,
1444 const char *pfx, int cidx)
1446 struct nid_path *path;
1449 path = snd_hda_get_path_from_idx(codec, path_idx);
1452 /* bind volume control will be created in the case of dac = 0 */
1454 err = add_stereo_vol(codec, pfx, cidx, path);
1458 err = add_stereo_sw(codec, pfx, cidx, path);
1464 /* add playback controls for speaker and HP outputs */
1465 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1466 const hda_nid_t *pins, const hda_nid_t *dacs,
1467 const int *paths, const char *pfx)
1469 struct hda_gen_spec *spec = codec->spec;
1470 struct hda_bind_ctls *ctl;
1474 if (!num_pins || !pins[0])
1477 if (num_pins == 1) {
1478 hda_nid_t dac = *dacs;
1480 dac = spec->multiout.dac_nids[0];
1481 return create_extra_out(codec, *pins, dac, paths[0], pfx, 0);
1484 for (i = 0; i < num_pins; i++) {
1486 if (dacs[num_pins - 1])
1487 dac = dacs[i]; /* with individual volumes */
1490 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1491 err = create_extra_out(codec, pins[i], dac, paths[i],
1493 } else if (num_pins >= 3) {
1494 snprintf(name, sizeof(name), "%s %s",
1495 pfx, channel_name[i]);
1496 err = create_extra_out(codec, pins[i], dac, paths[i],
1499 err = create_extra_out(codec, pins[i], dac, paths[i],
1505 if (dacs[num_pins - 1])
1508 /* Let's create a bind-controls for volumes */
1509 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1513 for (i = 0; i < num_pins; i++) {
1515 struct nid_path *path;
1516 if (!pins[i] || !dacs[i])
1518 path = snd_hda_get_path_from_idx(codec, paths[i]);
1521 vol = look_for_out_vol_nid(codec, path);
1524 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1527 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1528 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1535 static int create_hp_out_ctls(struct hda_codec *codec)
1537 struct hda_gen_spec *spec = codec->spec;
1538 return create_extra_outs(codec, spec->autocfg.hp_outs,
1539 spec->autocfg.hp_pins,
1540 spec->multiout.hp_out_nid,
1545 static int create_speaker_out_ctls(struct hda_codec *codec)
1547 struct hda_gen_spec *spec = codec->spec;
1548 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1549 spec->autocfg.speaker_pins,
1550 spec->multiout.extra_out_nid,
1551 spec->speaker_paths,
1556 * independent HP controls
1559 static int indep_hp_info(struct snd_kcontrol *kcontrol,
1560 struct snd_ctl_elem_info *uinfo)
1562 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1565 static int indep_hp_get(struct snd_kcontrol *kcontrol,
1566 struct snd_ctl_elem_value *ucontrol)
1568 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1569 struct hda_gen_spec *spec = codec->spec;
1570 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1574 static int indep_hp_put(struct snd_kcontrol *kcontrol,
1575 struct snd_ctl_elem_value *ucontrol)
1577 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1578 struct hda_gen_spec *spec = codec->spec;
1579 unsigned int select = ucontrol->value.enumerated.item[0];
1582 mutex_lock(&spec->pcm_mutex);
1583 if (spec->active_streams) {
1588 if (spec->indep_hp_enabled != select) {
1589 spec->indep_hp_enabled = select;
1590 if (spec->indep_hp_enabled)
1591 spec->multiout.hp_out_nid[0] = 0;
1593 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1597 mutex_unlock(&spec->pcm_mutex);
1601 static const struct snd_kcontrol_new indep_hp_ctl = {
1602 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1603 .name = "Independent HP",
1604 .info = indep_hp_info,
1605 .get = indep_hp_get,
1606 .put = indep_hp_put,
1610 static int create_indep_hp_ctls(struct hda_codec *codec)
1612 struct hda_gen_spec *spec = codec->spec;
1614 if (!spec->indep_hp)
1616 if (!spec->multiout.hp_out_nid[0]) {
1621 spec->indep_hp_enabled = false;
1622 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1623 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1629 * channel mode enum control
1632 static int ch_mode_info(struct snd_kcontrol *kcontrol,
1633 struct snd_ctl_elem_info *uinfo)
1635 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1636 struct hda_gen_spec *spec = codec->spec;
1638 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1640 uinfo->value.enumerated.items = spec->multi_ios + 1;
1641 if (uinfo->value.enumerated.item > spec->multi_ios)
1642 uinfo->value.enumerated.item = spec->multi_ios;
1643 sprintf(uinfo->value.enumerated.name, "%dch",
1644 (uinfo->value.enumerated.item + 1) * 2);
1648 static int ch_mode_get(struct snd_kcontrol *kcontrol,
1649 struct snd_ctl_elem_value *ucontrol)
1651 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1652 struct hda_gen_spec *spec = codec->spec;
1653 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1657 static inline struct nid_path *
1658 get_multiio_path(struct hda_codec *codec, int idx)
1660 struct hda_gen_spec *spec = codec->spec;
1661 return snd_hda_get_path_from_idx(codec,
1662 spec->out_paths[spec->autocfg.line_outs + idx]);
1665 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1667 struct hda_gen_spec *spec = codec->spec;
1668 hda_nid_t nid = spec->multi_io[idx].pin;
1669 struct nid_path *path;
1671 path = get_multiio_path(codec, idx);
1675 if (path->active == output)
1679 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1680 snd_hda_activate_path(codec, path, true, true);
1681 set_pin_eapd(codec, nid, true);
1683 set_pin_eapd(codec, nid, false);
1684 snd_hda_activate_path(codec, path, false, true);
1685 snd_hda_set_pin_ctl_cache(codec, nid,
1686 spec->multi_io[idx].ctl_in);
1691 static int ch_mode_put(struct snd_kcontrol *kcontrol,
1692 struct snd_ctl_elem_value *ucontrol)
1694 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1695 struct hda_gen_spec *spec = codec->spec;
1698 ch = ucontrol->value.enumerated.item[0];
1699 if (ch < 0 || ch > spec->multi_ios)
1701 if (ch == (spec->ext_channel_count - 1) / 2)
1703 spec->ext_channel_count = (ch + 1) * 2;
1704 for (i = 0; i < spec->multi_ios; i++)
1705 set_multi_io(codec, i, i < ch);
1706 spec->multiout.max_channels = max(spec->ext_channel_count,
1707 spec->const_channel_count);
1708 if (spec->need_dac_fix)
1709 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1713 static const struct snd_kcontrol_new channel_mode_enum = {
1714 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1715 .name = "Channel Mode",
1716 .info = ch_mode_info,
1721 static int create_multi_channel_mode(struct hda_codec *codec)
1723 struct hda_gen_spec *spec = codec->spec;
1725 if (spec->multi_ios > 0) {
1726 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
1733 * shared headphone/mic handling
1736 static void call_update_outputs(struct hda_codec *codec);
1738 /* for shared I/O, change the pin-control accordingly */
1739 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1741 struct hda_gen_spec *spec = codec->spec;
1743 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1744 /* NOTE: this assumes that there are only two inputs, the
1745 * first is the real internal mic and the second is HP/mic jack.
1748 val = snd_hda_get_default_vref(codec, pin);
1750 /* This pin does not have vref caps - let's enable vref on pin 0x18
1751 instead, as suggested by Realtek */
1752 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1753 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1754 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1755 if (vref_val != AC_PINCTL_VREF_HIZ)
1756 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1757 PIN_IN | (set_as_mic ? vref_val : 0));
1760 val = set_as_mic ? val | PIN_IN : PIN_HP;
1761 snd_hda_set_pin_ctl_cache(codec, pin, val);
1763 spec->automute_speaker = !set_as_mic;
1764 call_update_outputs(codec);
1767 /* create a shared input with the headphone out */
1768 static int create_shared_input(struct hda_codec *codec)
1770 struct hda_gen_spec *spec = codec->spec;
1771 struct auto_pin_cfg *cfg = &spec->autocfg;
1772 unsigned int defcfg;
1775 /* only one internal input pin? */
1776 if (cfg->num_inputs != 1)
1778 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1779 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1782 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1783 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1784 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1785 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1787 return 0; /* both not available */
1789 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1790 return 0; /* no input */
1792 cfg->inputs[1].pin = nid;
1793 cfg->inputs[1].type = AUTO_PIN_MIC;
1794 cfg->num_inputs = 2;
1795 spec->shared_mic_hp = 1;
1796 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1806 /* add the powersave loopback-list entry */
1807 static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1809 struct hda_amp_list *list;
1811 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1813 list = spec->loopback_list + spec->num_loopbacks;
1815 list->dir = HDA_INPUT;
1817 spec->num_loopbacks++;
1818 spec->loopback.amplist = spec->loopback_list;
1821 #define add_loopback_list(spec, mix, idx) /* NOP */
1824 /* create input playback/capture controls for the given pin */
1825 static int new_analog_input(struct hda_codec *codec, int input_idx,
1826 hda_nid_t pin, const char *ctlname, int ctlidx,
1829 struct hda_gen_spec *spec = codec->spec;
1830 struct nid_path *path;
1834 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1835 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1836 return 0; /* no need for analog loopback */
1838 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
1841 print_nid_path("loopback", path);
1842 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
1844 idx = path->idx[path->depth - 1];
1845 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1846 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1847 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
1850 path->ctls[NID_PATH_VOL_CTL] = val;
1853 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1854 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1855 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
1858 path->ctls[NID_PATH_MUTE_CTL] = val;
1861 path->active = true;
1862 add_loopback_list(spec, mix_nid, idx);
1866 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
1868 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1869 return (pincap & AC_PINCAP_IN) != 0;
1872 /* Parse the codec tree and retrieve ADCs */
1873 static int fill_adc_nids(struct hda_codec *codec)
1875 struct hda_gen_spec *spec = codec->spec;
1877 hda_nid_t *adc_nids = spec->adc_nids;
1878 int max_nums = ARRAY_SIZE(spec->adc_nids);
1881 nid = codec->start_nid;
1882 for (i = 0; i < codec->num_nodes; i++, nid++) {
1883 unsigned int caps = get_wcaps(codec, nid);
1884 int type = get_wcaps_type(caps);
1886 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1888 adc_nids[nums] = nid;
1889 if (++nums >= max_nums)
1892 spec->num_adc_nids = nums;
1896 /* filter out invalid adc_nids that don't give all active input pins;
1897 * if needed, check whether dynamic ADC-switching is available
1899 static int check_dyn_adc_switch(struct hda_codec *codec)
1901 struct hda_gen_spec *spec = codec->spec;
1902 struct hda_input_mux *imux = &spec->input_mux;
1903 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1909 for (n = 0; n < spec->num_adc_nids; n++) {
1910 adc = spec->adc_nids[n];
1911 for (i = 0; i < imux->num_items; i++) {
1912 pin = spec->imux_pins[i];
1913 if (!is_reachable_path(codec, pin, adc))
1916 if (i >= imux->num_items)
1917 adc_nids[nums++] = adc;
1921 if (spec->shared_mic_hp) {
1922 spec->shared_mic_hp = 0;
1923 imux->num_items = 1;
1927 /* check whether ADC-switch is possible */
1928 for (i = 0; i < imux->num_items; i++) {
1929 pin = spec->imux_pins[i];
1930 for (n = 0; n < spec->num_adc_nids; n++) {
1931 adc = spec->adc_nids[n];
1932 if (is_reachable_path(codec, pin, adc)) {
1933 spec->dyn_adc_idx[i] = n;
1939 snd_printdd("hda-codec: enabling ADC switching\n");
1940 spec->dyn_adc_switch = 1;
1941 } else if (nums != spec->num_adc_nids) {
1942 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1943 spec->num_adc_nids = nums;
1946 if (imux->num_items == 1 || spec->shared_mic_hp) {
1947 snd_printdd("hda-codec: reducing to a single ADC\n");
1948 spec->num_adc_nids = 1; /* reduce to a single ADC */
1951 /* single index for individual volumes ctls */
1952 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1953 spec->num_adc_nids = 1;
1959 * create playback/capture controls for input pins
1961 static int create_input_ctls(struct hda_codec *codec)
1963 struct hda_gen_spec *spec = codec->spec;
1964 const struct auto_pin_cfg *cfg = &spec->autocfg;
1965 hda_nid_t mixer = spec->mixer_nid;
1966 struct hda_input_mux *imux = &spec->input_mux;
1968 int i, c, err, type_idx = 0;
1969 const char *prev_label = NULL;
1971 num_adcs = fill_adc_nids(codec);
1975 for (i = 0; i < cfg->num_inputs; i++) {
1980 pin = cfg->inputs[i].pin;
1981 if (!is_input_pin(codec, pin))
1984 label = hda_get_autocfg_input_label(codec, cfg, i);
1985 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1986 label = "Headphone Mic";
1987 if (prev_label && !strcmp(label, prev_label))
1994 if (is_reachable_path(codec, pin, mixer)) {
1995 err = new_analog_input(codec, i, pin,
1996 label, type_idx, mixer);
2003 for (c = 0; c < num_adcs; c++) {
2004 struct nid_path *path;
2005 hda_nid_t adc = spec->adc_nids[c];
2007 if (!is_reachable_path(codec, pin, adc))
2009 path = snd_array_new(&spec->paths);
2012 memset(path, 0, sizeof(*path));
2013 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
2015 "invalid input path 0x%x -> 0x%x\n",
2020 print_nid_path("input", path);
2023 spec->imux_pins[imux->num_items] = pin;
2024 snd_hda_add_imux_item(imux, label,
2025 imux->num_items, NULL);
2039 /* get the ADC NID corresponding to the given index */
2040 static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
2042 struct hda_gen_spec *spec = codec->spec;
2043 if (spec->dyn_adc_switch)
2044 adc_idx = spec->dyn_adc_idx[imux_idx];
2045 return spec->adc_nids[adc_idx];
2048 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2051 static int mux_enum_info(struct snd_kcontrol *kcontrol,
2052 struct snd_ctl_elem_info *uinfo)
2054 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2055 struct hda_gen_spec *spec = codec->spec;
2056 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2059 static int mux_enum_get(struct snd_kcontrol *kcontrol,
2060 struct snd_ctl_elem_value *ucontrol)
2062 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2063 struct hda_gen_spec *spec = codec->spec;
2064 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2066 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2070 static int mux_enum_put(struct snd_kcontrol *kcontrol,
2071 struct snd_ctl_elem_value *ucontrol)
2073 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2074 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2075 return mux_select(codec, adc_idx,
2076 ucontrol->value.enumerated.item[0]);
2079 static const struct snd_kcontrol_new cap_src_temp = {
2080 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2081 .name = "Input Source",
2082 .info = mux_enum_info,
2083 .get = mux_enum_get,
2084 .put = mux_enum_put,
2088 * capture volume and capture switch ctls
2091 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2092 struct snd_ctl_elem_value *ucontrol);
2094 /* call the given amp update function for all amps in the imux list at once */
2095 static int cap_put_caller(struct snd_kcontrol *kcontrol,
2096 struct snd_ctl_elem_value *ucontrol,
2097 put_call_t func, int type)
2099 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2100 struct hda_gen_spec *spec = codec->spec;
2101 const struct hda_input_mux *imux;
2102 struct nid_path *path;
2103 int i, adc_idx, err = 0;
2105 imux = &spec->input_mux;
2106 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2107 mutex_lock(&codec->control_mutex);
2108 /* we use the cache-only update at first since multiple input paths
2109 * may shared the same amp; by updating only caches, the redundant
2110 * writes to hardware can be reduced.
2112 codec->cached_write = 1;
2113 for (i = 0; i < imux->num_items; i++) {
2114 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2115 get_adc_nid(codec, adc_idx, i));
2116 if (!path->ctls[type])
2118 kcontrol->private_value = path->ctls[type];
2119 err = func(kcontrol, ucontrol);
2124 codec->cached_write = 0;
2125 mutex_unlock(&codec->control_mutex);
2126 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
2127 if (err >= 0 && spec->cap_sync_hook)
2128 spec->cap_sync_hook(codec);
2132 /* capture volume ctl callbacks */
2133 #define cap_vol_info snd_hda_mixer_amp_volume_info
2134 #define cap_vol_get snd_hda_mixer_amp_volume_get
2135 #define cap_vol_tlv snd_hda_mixer_amp_tlv
2137 static int cap_vol_put(struct snd_kcontrol *kcontrol,
2138 struct snd_ctl_elem_value *ucontrol)
2140 return cap_put_caller(kcontrol, ucontrol,
2141 snd_hda_mixer_amp_volume_put,
2145 static const struct snd_kcontrol_new cap_vol_temp = {
2146 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2147 .name = "Capture Volume",
2148 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2149 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2150 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2151 .info = cap_vol_info,
2154 .tlv = { .c = cap_vol_tlv },
2157 /* capture switch ctl callbacks */
2158 #define cap_sw_info snd_ctl_boolean_stereo_info
2159 #define cap_sw_get snd_hda_mixer_amp_switch_get
2161 static int cap_sw_put(struct snd_kcontrol *kcontrol,
2162 struct snd_ctl_elem_value *ucontrol)
2164 return cap_put_caller(kcontrol, ucontrol,
2165 snd_hda_mixer_amp_switch_put,
2169 static const struct snd_kcontrol_new cap_sw_temp = {
2170 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2171 .name = "Capture Switch",
2172 .info = cap_sw_info,
2177 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2182 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2183 for (depth = 0; depth < 3; depth++) {
2184 if (depth >= path->depth)
2186 i = path->depth - depth - 1;
2187 nid = path->path[i];
2188 if (!path->ctls[NID_PATH_VOL_CTL]) {
2189 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2190 path->ctls[NID_PATH_VOL_CTL] =
2191 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2192 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2193 int idx = path->idx[i];
2194 if (!depth && codec->single_adc_amp)
2196 path->ctls[NID_PATH_VOL_CTL] =
2197 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2200 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2201 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2202 path->ctls[NID_PATH_MUTE_CTL] =
2203 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2204 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2205 int idx = path->idx[i];
2206 if (!depth && codec->single_adc_amp)
2208 path->ctls[NID_PATH_MUTE_CTL] =
2209 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2216 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2218 struct hda_gen_spec *spec = codec->spec;
2219 struct auto_pin_cfg *cfg = &spec->autocfg;
2223 if (!spec->inv_dmic_split)
2225 for (i = 0; i < cfg->num_inputs; i++) {
2226 if (cfg->inputs[i].pin != nid)
2228 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2230 val = snd_hda_codec_get_pincfg(codec, nid);
2231 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2236 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2237 int idx, bool is_switch, unsigned int ctl,
2240 struct hda_gen_spec *spec = codec->spec;
2242 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2243 const char *sfx = is_switch ? "Switch" : "Volume";
2244 unsigned int chs = inv_dmic ? 1 : 3;
2251 snprintf(tmpname, sizeof(tmpname),
2252 "%s Capture %s", label, sfx);
2254 snprintf(tmpname, sizeof(tmpname),
2256 err = add_control(spec, type, tmpname, idx,
2257 amp_val_replace_channels(ctl, chs));
2258 if (err < 0 || !inv_dmic)
2261 /* Make independent right kcontrol */
2263 snprintf(tmpname, sizeof(tmpname),
2264 "Inverted %s Capture %s", label, sfx);
2266 snprintf(tmpname, sizeof(tmpname),
2267 "Inverted Capture %s", sfx);
2268 return add_control(spec, type, tmpname, idx,
2269 amp_val_replace_channels(ctl, 2));
2272 /* create single (and simple) capture volume and switch controls */
2273 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2274 unsigned int vol_ctl, unsigned int sw_ctl,
2278 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2281 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2287 /* create bound capture volume and switch controls */
2288 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2289 unsigned int vol_ctl, unsigned int sw_ctl)
2291 struct hda_gen_spec *spec = codec->spec;
2292 struct snd_kcontrol_new *knew;
2295 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
2299 knew->private_value = vol_ctl;
2300 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2303 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
2307 knew->private_value = sw_ctl;
2308 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2313 /* return the vol ctl when used first in the imux list */
2314 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2316 struct hda_gen_spec *spec = codec->spec;
2317 struct nid_path *path;
2321 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2322 get_adc_nid(codec, 0, idx));
2325 ctl = path->ctls[type];
2328 for (i = 0; i < idx - 1; i++) {
2329 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2330 get_adc_nid(codec, 0, i));
2331 if (path && path->ctls[type] == ctl)
2337 /* create individual capture volume and switch controls per input */
2338 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2340 struct hda_gen_spec *spec = codec->spec;
2341 struct hda_input_mux *imux = &spec->input_mux;
2342 int i, err, type, type_idx = 0;
2343 const char *prev_label = NULL;
2345 for (i = 0; i < imux->num_items; i++) {
2348 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2349 if (prev_label && !strcmp(label, prev_label))
2354 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2356 for (type = 0; type < 2; type++) {
2357 err = add_single_cap_ctl(codec, label, type_idx, type,
2358 get_first_cap_ctl(codec, i, type),
2367 static int create_capture_mixers(struct hda_codec *codec)
2369 struct hda_gen_spec *spec = codec->spec;
2370 struct hda_input_mux *imux = &spec->input_mux;
2371 int i, n, nums, err;
2373 if (spec->dyn_adc_switch)
2376 nums = spec->num_adc_nids;
2378 if (!spec->auto_mic && imux->num_items > 1) {
2379 struct snd_kcontrol_new *knew;
2381 name = nums > 1 ? "Input Source" : "Capture Source";
2382 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
2388 for (n = 0; n < nums; n++) {
2390 bool inv_dmic = false;
2394 for (i = 0; i < imux->num_items; i++) {
2395 struct nid_path *path;
2396 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2397 get_adc_nid(codec, n, i));
2400 parse_capvol_in_path(codec, path);
2402 vol = path->ctls[NID_PATH_VOL_CTL];
2403 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2406 sw = path->ctls[NID_PATH_MUTE_CTL];
2407 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2409 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2414 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2416 else if (!spec->multi_cap_vol)
2417 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2419 err = create_multi_cap_vol_ctl(codec);
2428 * add mic boosts if needed
2430 static int parse_mic_boost(struct hda_codec *codec)
2432 struct hda_gen_spec *spec = codec->spec;
2433 struct auto_pin_cfg *cfg = &spec->autocfg;
2437 const char *prev_label = NULL;
2439 for (i = 0; i < cfg->num_inputs; i++) {
2440 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2442 nid = cfg->inputs[i].pin;
2443 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2445 char boost_label[32];
2446 struct nid_path *path;
2449 label = hda_get_autocfg_input_label(codec, cfg, i);
2450 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2451 label = "Headphone Mic";
2452 if (prev_label && !strcmp(label, prev_label))
2458 snprintf(boost_label, sizeof(boost_label),
2459 "%s Boost Volume", label);
2460 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2461 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2462 boost_label, type_idx, val);
2466 path = snd_hda_get_nid_path(codec, nid, 0);
2468 path->ctls[NID_PATH_BOOST_CTL] = val;
2475 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2477 static void parse_digital(struct hda_codec *codec)
2479 struct hda_gen_spec *spec = codec->spec;
2480 struct nid_path *path;
2484 /* support multiple SPDIFs; the secondary is set up as a slave */
2486 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2487 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2488 dig_nid = look_for_dac(codec, pin, true);
2491 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
2494 print_nid_path("digout", path);
2495 path->active = true;
2496 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
2498 spec->multiout.dig_out_nid = dig_nid;
2499 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2501 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2502 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2504 spec->slave_dig_outs[nums - 1] = dig_nid;
2509 if (spec->autocfg.dig_in_pin) {
2510 dig_nid = codec->start_nid;
2511 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2512 unsigned int wcaps = get_wcaps(codec, dig_nid);
2513 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2515 if (!(wcaps & AC_WCAP_DIGITAL))
2517 path = snd_hda_add_new_path(codec,
2518 spec->autocfg.dig_in_pin,
2519 dig_nid, HDA_PARSE_ALL);
2521 print_nid_path("digin", path);
2522 path->active = true;
2523 spec->dig_in_nid = dig_nid;
2524 spec->digin_path = snd_hda_get_path_idx(codec, path);
2533 * input MUX handling
2536 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2538 /* select the given imux item; either unmute exclusively or select the route */
2539 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2542 struct hda_gen_spec *spec = codec->spec;
2543 const struct hda_input_mux *imux;
2544 struct nid_path *path;
2546 imux = &spec->input_mux;
2547 if (!imux->num_items)
2550 if (idx >= imux->num_items)
2551 idx = imux->num_items - 1;
2552 if (spec->cur_mux[adc_idx] == idx)
2555 path = snd_hda_get_nid_path(codec,
2556 spec->imux_pins[spec->cur_mux[adc_idx]],
2557 spec->adc_nids[adc_idx]);
2561 snd_hda_activate_path(codec, path, false, false);
2563 spec->cur_mux[adc_idx] = idx;
2565 if (spec->shared_mic_hp)
2566 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2568 if (spec->dyn_adc_switch)
2569 dyn_adc_pcm_resetup(codec, idx);
2571 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2572 get_adc_nid(codec, adc_idx, idx));
2577 snd_hda_activate_path(codec, path, true, false);
2578 if (spec->cap_sync_hook)
2579 spec->cap_sync_hook(codec);
2585 * Jack detections for HP auto-mute and mic-switch
2588 /* check each pin in the given array; returns true if any of them is plugged */
2589 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2593 for (i = 0; i < num_pins; i++) {
2594 hda_nid_t nid = pins[i];
2597 present |= snd_hda_jack_detect(codec, nid);
2602 /* standard HP/line-out auto-mute helper */
2603 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2604 bool mute, bool hp_out)
2606 struct hda_gen_spec *spec = codec->spec;
2607 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2610 for (i = 0; i < num_pins; i++) {
2611 hda_nid_t nid = pins[i];
2615 /* don't reset VREF value in case it's controlling
2616 * the amp (see alc861_fixup_asus_amp_vref_0f())
2618 if (spec->keep_vref_in_automute) {
2619 val = snd_hda_codec_read(codec, nid, 0,
2620 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2625 snd_hda_set_pin_ctl_cache(codec, nid, val);
2626 set_pin_eapd(codec, nid, !mute);
2630 /* Toggle outputs muting */
2631 void snd_hda_gen_update_outputs(struct hda_codec *codec)
2633 struct hda_gen_spec *spec = codec->spec;
2636 /* Control HP pins/amps depending on master_mute state;
2637 * in general, HP pins/amps control should be enabled in all cases,
2638 * but currently set only for master_mute, just to be safe
2640 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2641 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2642 spec->autocfg.hp_pins, spec->master_mute, true);
2644 if (!spec->automute_speaker)
2647 on = spec->hp_jack_present | spec->line_jack_present;
2648 on |= spec->master_mute;
2649 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2650 spec->autocfg.speaker_pins, on, false);
2652 /* toggle line-out mutes if needed, too */
2653 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2654 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2655 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2657 if (!spec->automute_lo)
2660 on = spec->hp_jack_present;
2661 on |= spec->master_mute;
2662 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2663 spec->autocfg.line_out_pins, on, false);
2665 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
2667 static void call_update_outputs(struct hda_codec *codec)
2669 struct hda_gen_spec *spec = codec->spec;
2670 if (spec->automute_hook)
2671 spec->automute_hook(codec);
2673 snd_hda_gen_update_outputs(codec);
2676 /* standard HP-automute helper */
2677 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2679 struct hda_gen_spec *spec = codec->spec;
2681 spec->hp_jack_present =
2682 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2683 spec->autocfg.hp_pins);
2684 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2686 call_update_outputs(codec);
2688 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
2690 /* standard line-out-automute helper */
2691 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2693 struct hda_gen_spec *spec = codec->spec;
2695 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2697 /* check LO jack only when it's different from HP */
2698 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2701 spec->line_jack_present =
2702 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2703 spec->autocfg.line_out_pins);
2704 if (!spec->automute_speaker || !spec->detect_lo)
2706 call_update_outputs(codec);
2708 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
2710 /* standard mic auto-switch helper */
2711 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
2713 struct hda_gen_spec *spec = codec->spec;
2716 if (!spec->auto_mic)
2719 for (i = spec->am_num_entries - 1; i > 0; i--) {
2720 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2721 mux_select(codec, 0, spec->am_entry[i].idx);
2725 mux_select(codec, 0, spec->am_entry[0].idx);
2727 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
2730 * Auto-Mute mode mixer enum support
2732 static int automute_mode_info(struct snd_kcontrol *kcontrol,
2733 struct snd_ctl_elem_info *uinfo)
2735 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2736 struct hda_gen_spec *spec = codec->spec;
2737 static const char * const texts3[] = {
2738 "Disabled", "Speaker Only", "Line Out+Speaker"
2741 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2742 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2743 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2746 static int automute_mode_get(struct snd_kcontrol *kcontrol,
2747 struct snd_ctl_elem_value *ucontrol)
2749 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2750 struct hda_gen_spec *spec = codec->spec;
2751 unsigned int val = 0;
2752 if (spec->automute_speaker)
2754 if (spec->automute_lo)
2757 ucontrol->value.enumerated.item[0] = val;
2761 static int automute_mode_put(struct snd_kcontrol *kcontrol,
2762 struct snd_ctl_elem_value *ucontrol)
2764 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2765 struct hda_gen_spec *spec = codec->spec;
2767 switch (ucontrol->value.enumerated.item[0]) {
2769 if (!spec->automute_speaker && !spec->automute_lo)
2771 spec->automute_speaker = 0;
2772 spec->automute_lo = 0;
2775 if (spec->automute_speaker_possible) {
2776 if (!spec->automute_lo && spec->automute_speaker)
2778 spec->automute_speaker = 1;
2779 spec->automute_lo = 0;
2780 } else if (spec->automute_lo_possible) {
2781 if (spec->automute_lo)
2783 spec->automute_lo = 1;
2788 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2790 if (spec->automute_speaker && spec->automute_lo)
2792 spec->automute_speaker = 1;
2793 spec->automute_lo = 1;
2798 call_update_outputs(codec);
2802 static const struct snd_kcontrol_new automute_mode_enum = {
2803 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2804 .name = "Auto-Mute Mode",
2805 .info = automute_mode_info,
2806 .get = automute_mode_get,
2807 .put = automute_mode_put,
2810 static int add_automute_mode_enum(struct hda_codec *codec)
2812 struct hda_gen_spec *spec = codec->spec;
2814 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
2820 * Check the availability of HP/line-out auto-mute;
2821 * Set up appropriately if really supported
2823 static int check_auto_mute_availability(struct hda_codec *codec)
2825 struct hda_gen_spec *spec = codec->spec;
2826 struct auto_pin_cfg *cfg = &spec->autocfg;
2830 if (cfg->hp_pins[0])
2832 if (cfg->line_out_pins[0])
2834 if (cfg->speaker_pins[0])
2836 if (present < 2) /* need two different output types */
2839 if (!cfg->speaker_pins[0] &&
2840 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2841 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2842 sizeof(cfg->speaker_pins));
2843 cfg->speaker_outs = cfg->line_outs;
2846 if (!cfg->hp_pins[0] &&
2847 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2848 memcpy(cfg->hp_pins, cfg->line_out_pins,
2849 sizeof(cfg->hp_pins));
2850 cfg->hp_outs = cfg->line_outs;
2853 for (i = 0; i < cfg->hp_outs; i++) {
2854 hda_nid_t nid = cfg->hp_pins[i];
2855 if (!is_jack_detectable(codec, nid))
2857 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2859 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
2860 spec->hp_automute_hook ?
2861 spec->hp_automute_hook :
2862 snd_hda_gen_hp_automute);
2863 spec->detect_hp = 1;
2866 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2867 if (cfg->speaker_outs)
2868 for (i = 0; i < cfg->line_outs; i++) {
2869 hda_nid_t nid = cfg->line_out_pins[i];
2870 if (!is_jack_detectable(codec, nid))
2872 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2873 snd_hda_jack_detect_enable_callback(codec, nid,
2874 HDA_GEN_FRONT_EVENT,
2875 spec->line_automute_hook ?
2876 spec->line_automute_hook :
2877 snd_hda_gen_line_automute);
2878 spec->detect_lo = 1;
2880 spec->automute_lo_possible = spec->detect_hp;
2883 spec->automute_speaker_possible = cfg->speaker_outs &&
2884 (spec->detect_hp || spec->detect_lo);
2886 spec->automute_lo = spec->automute_lo_possible;
2887 spec->automute_speaker = spec->automute_speaker_possible;
2889 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2890 /* create a control for automute mode */
2891 err = add_automute_mode_enum(codec);
2898 /* return the position of NID in the list, or -1 if not found */
2899 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2902 for (i = 0; i < nums; i++)
2908 /* check whether all auto-mic pins are valid; setup indices if OK */
2909 static bool auto_mic_check_imux(struct hda_codec *codec)
2911 struct hda_gen_spec *spec = codec->spec;
2912 const struct hda_input_mux *imux;
2915 imux = &spec->input_mux;
2916 for (i = 0; i < spec->am_num_entries; i++) {
2917 spec->am_entry[i].idx =
2918 find_idx_in_nid_list(spec->am_entry[i].pin,
2919 spec->imux_pins, imux->num_items);
2920 if (spec->am_entry[i].idx < 0)
2921 return false; /* no corresponding imux */
2924 /* we don't need the jack detection for the first pin */
2925 for (i = 1; i < spec->am_num_entries; i++)
2926 snd_hda_jack_detect_enable_callback(codec,
2927 spec->am_entry[i].pin,
2929 spec->mic_autoswitch_hook ?
2930 spec->mic_autoswitch_hook :
2931 snd_hda_gen_mic_autoswitch);
2935 static int compare_attr(const void *ap, const void *bp)
2937 const struct automic_entry *a = ap;
2938 const struct automic_entry *b = bp;
2939 return (int)(a->attr - b->attr);
2943 * Check the availability of auto-mic switch;
2944 * Set up if really supported
2946 static int check_auto_mic_availability(struct hda_codec *codec)
2948 struct hda_gen_spec *spec = codec->spec;
2949 struct auto_pin_cfg *cfg = &spec->autocfg;
2955 for (i = 0; i < cfg->num_inputs; i++) {
2956 hda_nid_t nid = cfg->inputs[i].pin;
2958 attr = snd_hda_codec_get_pincfg(codec, nid);
2959 attr = snd_hda_get_input_pin_attr(attr);
2960 if (types & (1 << attr))
2961 return 0; /* already occupied */
2963 case INPUT_PIN_ATTR_INT:
2964 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2965 return 0; /* invalid type */
2967 case INPUT_PIN_ATTR_UNUSED:
2968 return 0; /* invalid entry */
2970 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2971 return 0; /* invalid type */
2972 if (!spec->line_in_auto_switch &&
2973 cfg->inputs[i].type != AUTO_PIN_MIC)
2974 return 0; /* only mic is allowed */
2975 if (!is_jack_detectable(codec, nid))
2976 return 0; /* no unsol support */
2979 if (num_pins >= MAX_AUTO_MIC_PINS)
2981 types |= (1 << attr);
2982 spec->am_entry[num_pins].pin = nid;
2983 spec->am_entry[num_pins].attr = attr;
2990 spec->am_num_entries = num_pins;
2991 /* sort the am_entry in the order of attr so that the pin with a
2992 * higher attr will be selected when the jack is plugged.
2994 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2995 compare_attr, NULL);
2997 if (!auto_mic_check_imux(codec))
3001 spec->num_adc_nids = 1;
3002 spec->cur_mux[0] = spec->am_entry[0].idx;
3003 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3004 spec->am_entry[0].pin,
3005 spec->am_entry[1].pin,
3006 spec->am_entry[2].pin);
3013 * Parse the given BIOS configuration and set up the hda_gen_spec
3015 * return 1 if successful, 0 if the proper config is not found,
3016 * or a negative error code
3018 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
3019 struct auto_pin_cfg *cfg)
3021 struct hda_gen_spec *spec = codec->spec;
3024 if (cfg != &spec->autocfg) {
3025 spec->autocfg = *cfg;
3026 cfg = &spec->autocfg;
3029 if (!cfg->line_outs) {
3030 if (cfg->dig_outs || cfg->dig_in_pin) {
3031 spec->multiout.max_channels = 2;
3032 spec->no_analog = 1;
3035 return 0; /* can't find valid BIOS pin config */
3038 if (!spec->no_primary_hp &&
3039 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3040 cfg->line_outs <= cfg->hp_outs) {
3041 /* use HP as primary out */
3042 cfg->speaker_outs = cfg->line_outs;
3043 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3044 sizeof(cfg->speaker_pins));
3045 cfg->line_outs = cfg->hp_outs;
3046 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3048 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3049 cfg->line_out_type = AUTO_PIN_HP_OUT;
3052 err = parse_output_paths(codec);
3055 err = create_multi_channel_mode(codec);
3058 err = create_multi_out_ctls(codec, cfg);
3061 err = create_hp_out_ctls(codec);
3064 err = create_speaker_out_ctls(codec);
3067 err = create_indep_hp_ctls(codec);
3070 err = create_shared_input(codec);
3073 err = create_input_ctls(codec);
3077 /* check the multiple speaker pins */
3078 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
3079 spec->const_channel_count = cfg->line_outs * 2;
3081 spec->const_channel_count = cfg->speaker_outs * 2;
3083 if (spec->multi_ios > 0)
3084 spec->multiout.max_channels = max(spec->ext_channel_count,
3085 spec->const_channel_count);
3087 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3089 err = check_auto_mute_availability(codec);
3093 err = check_dyn_adc_switch(codec);
3097 if (!spec->shared_mic_hp) {
3098 err = check_auto_mic_availability(codec);
3103 err = create_capture_mixers(codec);
3107 err = parse_mic_boost(codec);
3112 parse_digital(codec);
3116 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
3120 * Build control elements
3123 /* slave controls for virtual master */
3124 static const char * const slave_pfxs[] = {
3125 "Front", "Surround", "Center", "LFE", "Side",
3126 "Headphone", "Speaker", "Mono", "Line Out",
3127 "CLFE", "Bass Speaker", "PCM",
3131 int snd_hda_gen_build_controls(struct hda_codec *codec)
3133 struct hda_gen_spec *spec = codec->spec;
3136 if (spec->kctls.used) {
3137 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3142 if (spec->multiout.dig_out_nid) {
3143 err = snd_hda_create_dig_out_ctls(codec,
3144 spec->multiout.dig_out_nid,
3145 spec->multiout.dig_out_nid,
3146 spec->pcm_rec[1].pcm_type);
3149 if (!spec->no_analog) {
3150 err = snd_hda_create_spdif_share_sw(codec,
3154 spec->multiout.share_spdif = 1;
3157 if (spec->dig_in_nid) {
3158 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3163 /* if we have no master control, let's create it */
3164 if (!spec->no_analog &&
3165 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3166 unsigned int vmaster_tlv[4];
3167 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3168 HDA_OUTPUT, vmaster_tlv);
3169 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3170 vmaster_tlv, slave_pfxs,
3175 if (!spec->no_analog &&
3176 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3177 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3180 true, &spec->vmaster_mute.sw_kctl);
3183 if (spec->vmaster_mute.hook)
3184 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3185 spec->vmaster_mute_enum);
3188 free_kctls(spec); /* no longer needed */
3190 if (spec->shared_mic_hp) {
3192 int nid = spec->autocfg.inputs[1].pin;
3193 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3196 err = snd_hda_jack_detect_enable(codec, nid, 0);
3201 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3207 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3215 * Analog playback callbacks
3217 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3218 struct hda_codec *codec,
3219 struct snd_pcm_substream *substream)
3221 struct hda_gen_spec *spec = codec->spec;
3224 mutex_lock(&spec->pcm_mutex);
3225 err = snd_hda_multi_out_analog_open(codec,
3226 &spec->multiout, substream,
3229 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3230 mutex_unlock(&spec->pcm_mutex);
3234 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3235 struct hda_codec *codec,
3236 unsigned int stream_tag,
3237 unsigned int format,
3238 struct snd_pcm_substream *substream)
3240 struct hda_gen_spec *spec = codec->spec;
3241 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3242 stream_tag, format, substream);
3245 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3246 struct hda_codec *codec,
3247 struct snd_pcm_substream *substream)
3249 struct hda_gen_spec *spec = codec->spec;
3250 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3253 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3254 struct hda_codec *codec,
3255 struct snd_pcm_substream *substream)
3257 struct hda_gen_spec *spec = codec->spec;
3258 mutex_lock(&spec->pcm_mutex);
3259 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3260 mutex_unlock(&spec->pcm_mutex);
3264 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3265 struct hda_codec *codec,
3266 struct snd_pcm_substream *substream)
3268 struct hda_gen_spec *spec = codec->spec;
3271 mutex_lock(&spec->pcm_mutex);
3272 if (!spec->indep_hp_enabled)
3275 spec->active_streams |= 1 << STREAM_INDEP_HP;
3276 mutex_unlock(&spec->pcm_mutex);
3280 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3281 struct hda_codec *codec,
3282 struct snd_pcm_substream *substream)
3284 struct hda_gen_spec *spec = codec->spec;
3285 mutex_lock(&spec->pcm_mutex);
3286 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3287 mutex_unlock(&spec->pcm_mutex);
3294 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3295 struct hda_codec *codec,
3296 struct snd_pcm_substream *substream)
3298 struct hda_gen_spec *spec = codec->spec;
3299 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3302 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3303 struct hda_codec *codec,
3304 unsigned int stream_tag,
3305 unsigned int format,
3306 struct snd_pcm_substream *substream)
3308 struct hda_gen_spec *spec = codec->spec;
3309 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3310 stream_tag, format, substream);
3313 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3314 struct hda_codec *codec,
3315 struct snd_pcm_substream *substream)
3317 struct hda_gen_spec *spec = codec->spec;
3318 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3321 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3322 struct hda_codec *codec,
3323 struct snd_pcm_substream *substream)
3325 struct hda_gen_spec *spec = codec->spec;
3326 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3332 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3333 struct hda_codec *codec,
3334 unsigned int stream_tag,
3335 unsigned int format,
3336 struct snd_pcm_substream *substream)
3338 struct hda_gen_spec *spec = codec->spec;
3340 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
3341 stream_tag, 0, format);
3345 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3346 struct hda_codec *codec,
3347 struct snd_pcm_substream *substream)
3349 struct hda_gen_spec *spec = codec->spec;
3351 snd_hda_codec_cleanup_stream(codec,
3352 spec->adc_nids[substream->number + 1]);
3358 static const struct hda_pcm_stream pcm_analog_playback = {
3362 /* NID is set in build_pcms */
3364 .open = playback_pcm_open,
3365 .close = playback_pcm_close,
3366 .prepare = playback_pcm_prepare,
3367 .cleanup = playback_pcm_cleanup
3371 static const struct hda_pcm_stream pcm_analog_capture = {
3375 /* NID is set in build_pcms */
3378 static const struct hda_pcm_stream pcm_analog_alt_playback = {
3382 /* NID is set in build_pcms */
3384 .open = alt_playback_pcm_open,
3385 .close = alt_playback_pcm_close
3389 static const struct hda_pcm_stream pcm_analog_alt_capture = {
3390 .substreams = 2, /* can be overridden */
3393 /* NID is set in build_pcms */
3395 .prepare = alt_capture_pcm_prepare,
3396 .cleanup = alt_capture_pcm_cleanup
3400 static const struct hda_pcm_stream pcm_digital_playback = {
3404 /* NID is set in build_pcms */
3406 .open = dig_playback_pcm_open,
3407 .close = dig_playback_pcm_close,
3408 .prepare = dig_playback_pcm_prepare,
3409 .cleanup = dig_playback_pcm_cleanup
3413 static const struct hda_pcm_stream pcm_digital_capture = {
3417 /* NID is set in build_pcms */
3420 /* Used by build_pcms to flag that a PCM has no playback stream */
3421 static const struct hda_pcm_stream pcm_null_stream = {
3428 * dynamic changing ADC PCM streams
3430 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3432 struct hda_gen_spec *spec = codec->spec;
3433 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3435 if (spec->cur_adc && spec->cur_adc != new_adc) {
3436 /* stream is running, let's swap the current ADC */
3437 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3438 spec->cur_adc = new_adc;
3439 snd_hda_codec_setup_stream(codec, new_adc,
3440 spec->cur_adc_stream_tag, 0,
3441 spec->cur_adc_format);
3447 /* analog capture with dynamic dual-adc changes */
3448 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3449 struct hda_codec *codec,
3450 unsigned int stream_tag,
3451 unsigned int format,
3452 struct snd_pcm_substream *substream)
3454 struct hda_gen_spec *spec = codec->spec;
3455 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3456 spec->cur_adc_stream_tag = stream_tag;
3457 spec->cur_adc_format = format;
3458 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3462 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3463 struct hda_codec *codec,
3464 struct snd_pcm_substream *substream)
3466 struct hda_gen_spec *spec = codec->spec;
3467 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3472 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3476 .nid = 0, /* fill later */
3478 .prepare = dyn_adc_capture_pcm_prepare,
3479 .cleanup = dyn_adc_capture_pcm_cleanup
3483 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3484 const char *chip_name)
3490 strlcpy(str, chip_name, len);
3492 /* drop non-alnum chars after a space */
3493 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3494 if (!isalnum(p[1])) {
3499 strlcat(str, sfx, len);
3502 /* build PCM streams based on the parsed results */
3503 int snd_hda_gen_build_pcms(struct hda_codec *codec)
3505 struct hda_gen_spec *spec = codec->spec;
3506 struct hda_pcm *info = spec->pcm_rec;
3507 const struct hda_pcm_stream *p;
3508 bool have_multi_adcs;
3510 codec->num_pcms = 1;
3511 codec->pcm_info = info;
3513 if (spec->no_analog)
3516 fill_pcm_stream_name(spec->stream_name_analog,
3517 sizeof(spec->stream_name_analog),
3518 " Analog", codec->chip_name);
3519 info->name = spec->stream_name_analog;
3521 if (spec->multiout.num_dacs > 0) {
3522 p = spec->stream_analog_playback;
3524 p = &pcm_analog_playback;
3525 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3526 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3527 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3528 spec->multiout.max_channels;
3529 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3530 spec->autocfg.line_outs == 2)
3531 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3534 if (spec->num_adc_nids) {
3535 p = spec->stream_analog_capture;
3537 if (spec->dyn_adc_switch)
3538 p = &dyn_adc_pcm_analog_capture;
3540 p = &pcm_analog_capture;
3542 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3543 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3547 /* SPDIF for stream index #1 */
3548 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
3549 fill_pcm_stream_name(spec->stream_name_digital,
3550 sizeof(spec->stream_name_digital),
3551 " Digital", codec->chip_name);
3552 codec->num_pcms = 2;
3553 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3554 info = spec->pcm_rec + 1;
3555 info->name = spec->stream_name_digital;
3556 if (spec->dig_out_type)
3557 info->pcm_type = spec->dig_out_type;
3559 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3560 if (spec->multiout.dig_out_nid) {
3561 p = spec->stream_digital_playback;
3563 p = &pcm_digital_playback;
3564 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3565 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3567 if (spec->dig_in_nid) {
3568 p = spec->stream_digital_capture;
3570 p = &pcm_digital_capture;
3571 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3572 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3576 if (spec->no_analog)
3579 /* If the use of more than one ADC is requested for the current
3580 * model, configure a second analog capture-only PCM.
3582 have_multi_adcs = (spec->num_adc_nids > 1) &&
3583 !spec->dyn_adc_switch && !spec->auto_mic;
3584 /* Additional Analaog capture for index #2 */
3585 if (spec->alt_dac_nid || have_multi_adcs) {
3586 codec->num_pcms = 3;
3587 info = spec->pcm_rec + 2;
3588 info->name = spec->stream_name_analog;
3589 if (spec->alt_dac_nid) {
3590 p = spec->stream_analog_alt_playback;
3592 p = &pcm_analog_alt_playback;
3593 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3594 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3597 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3599 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3601 if (have_multi_adcs) {
3602 p = spec->stream_analog_alt_capture;
3604 p = &pcm_analog_alt_capture;
3605 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3606 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3608 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3609 spec->num_adc_nids - 1;
3611 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3613 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3619 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3623 * Standard auto-parser initializations
3626 /* configure the path from the given dac to the pin as the proper output */
3627 static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3628 int pin_type, int path_idx)
3630 struct nid_path *path;
3632 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3633 path = snd_hda_get_path_from_idx(codec, path_idx);
3636 snd_hda_activate_path(codec, path, path->active, true);
3637 set_pin_eapd(codec, pin, path->active);
3640 /* initialize primary output paths */
3641 static void init_multi_out(struct hda_codec *codec)
3643 struct hda_gen_spec *spec = codec->spec;
3648 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3653 for (i = 0; i < spec->autocfg.line_outs; i++) {
3654 nid = spec->autocfg.line_out_pins[i];
3656 set_output_and_unmute(codec, nid, pin_type,
3657 spec->out_paths[i]);
3662 static void __init_extra_out(struct hda_codec *codec, int num_outs,
3663 hda_nid_t *pins, int *paths, int type)
3668 for (i = 0; i < num_outs; i++) {
3672 set_output_and_unmute(codec, pin, type, paths[i]);
3676 /* initialize hp and speaker paths */
3677 static void init_extra_out(struct hda_codec *codec)
3679 struct hda_gen_spec *spec = codec->spec;
3681 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3682 __init_extra_out(codec, spec->autocfg.hp_outs,
3683 spec->autocfg.hp_pins,
3684 spec->hp_paths, PIN_HP);
3685 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3686 __init_extra_out(codec, spec->autocfg.speaker_outs,
3687 spec->autocfg.speaker_pins,
3688 spec->speaker_paths, PIN_OUT);
3691 /* initialize multi-io paths */
3692 static void init_multi_io(struct hda_codec *codec)
3694 struct hda_gen_spec *spec = codec->spec;
3697 for (i = 0; i < spec->multi_ios; i++) {
3698 hda_nid_t pin = spec->multi_io[i].pin;
3699 struct nid_path *path;
3700 path = get_multiio_path(codec, i);
3703 if (!spec->multi_io[i].ctl_in)
3704 spec->multi_io[i].ctl_in =
3705 snd_hda_codec_update_cache(codec, pin, 0,
3706 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3707 snd_hda_activate_path(codec, path, path->active, true);
3711 /* set up the input pin config, depending on the given auto-pin type */
3712 static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3715 unsigned int val = PIN_IN;
3716 if (auto_pin_type == AUTO_PIN_MIC)
3717 val |= snd_hda_get_default_vref(codec, nid);
3718 snd_hda_set_pin_ctl_cache(codec, nid, val);
3721 /* set up input pins and loopback paths */
3722 static void init_analog_input(struct hda_codec *codec)
3724 struct hda_gen_spec *spec = codec->spec;
3725 struct auto_pin_cfg *cfg = &spec->autocfg;
3728 for (i = 0; i < cfg->num_inputs; i++) {
3729 hda_nid_t nid = cfg->inputs[i].pin;
3730 if (is_input_pin(codec, nid))
3731 set_input_pin(codec, nid, cfg->inputs[i].type);
3733 /* init loopback inputs */
3734 if (spec->mixer_nid) {
3735 struct nid_path *path;
3736 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
3738 snd_hda_activate_path(codec, path,
3739 path->active, false);
3744 /* initialize ADC paths */
3745 static void init_input_src(struct hda_codec *codec)
3747 struct hda_gen_spec *spec = codec->spec;
3748 struct hda_input_mux *imux = &spec->input_mux;
3749 struct nid_path *path;
3752 if (spec->dyn_adc_switch)
3755 nums = spec->num_adc_nids;
3757 for (c = 0; c < nums; c++) {
3758 for (i = 0; i < imux->num_items; i++) {
3759 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3760 get_adc_nid(codec, c, i));
3762 bool active = path->active;
3763 if (i == spec->cur_mux[c])
3765 snd_hda_activate_path(codec, path, active, false);
3770 if (spec->shared_mic_hp)
3771 update_shared_mic_hp(codec, spec->cur_mux[0]);
3773 if (spec->cap_sync_hook)
3774 spec->cap_sync_hook(codec);
3777 /* set right pin controls for digital I/O */
3778 static void init_digital(struct hda_codec *codec)
3780 struct hda_gen_spec *spec = codec->spec;
3784 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3785 pin = spec->autocfg.dig_out_pins[i];
3788 set_output_and_unmute(codec, pin, PIN_OUT,
3789 spec->digout_paths[i]);
3791 pin = spec->autocfg.dig_in_pin;
3793 struct nid_path *path;
3794 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
3795 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
3797 snd_hda_activate_path(codec, path, path->active, false);
3801 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3802 * invalid unsol tags by some reason
3804 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3808 for (i = 0; i < codec->init_pins.used; i++) {
3809 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3810 hda_nid_t nid = pin->nid;
3811 if (is_jack_detectable(codec, nid) &&
3812 !snd_hda_jack_tbl_get(codec, nid))
3813 snd_hda_codec_update_cache(codec, nid, 0,
3814 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3818 int snd_hda_gen_init(struct hda_codec *codec)
3820 struct hda_gen_spec *spec = codec->spec;
3822 if (spec->init_hook)
3823 spec->init_hook(codec);
3825 snd_hda_apply_verbs(codec);
3827 codec->cached_write = 1;
3829 init_multi_out(codec);
3830 init_extra_out(codec);
3831 init_multi_io(codec);
3832 init_analog_input(codec);
3833 init_input_src(codec);
3834 init_digital(codec);
3836 clear_unsol_on_unused_pins(codec);
3838 /* call init functions of standard auto-mute helpers */
3839 snd_hda_gen_hp_automute(codec, NULL);
3840 snd_hda_gen_line_automute(codec, NULL);
3841 snd_hda_gen_mic_autoswitch(codec, NULL);
3843 snd_hda_codec_flush_amp_cache(codec);
3844 snd_hda_codec_flush_cmd_cache(codec);
3846 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3847 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3849 hda_call_check_power_status(codec, 0x01);
3852 EXPORT_SYMBOL(snd_hda_gen_init);
3856 * the generic codec support
3860 static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3862 struct hda_gen_spec *spec = codec->spec;
3863 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3867 static void generic_free(struct hda_codec *codec)
3869 snd_hda_gen_spec_free(codec->spec);
3874 static const struct hda_codec_ops generic_patch_ops = {
3875 .build_controls = snd_hda_gen_build_controls,
3876 .build_pcms = snd_hda_gen_build_pcms,
3877 .init = snd_hda_gen_init,
3878 .free = generic_free,
3879 .unsol_event = snd_hda_jack_unsol_event,
3881 .check_power_status = generic_check_power_status,
3885 int snd_hda_parse_generic_codec(struct hda_codec *codec)
3887 struct hda_gen_spec *spec;
3890 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3893 snd_hda_gen_spec_init(spec);
3896 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3900 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
3904 codec->patch_ops = generic_patch_ops;
3908 generic_free(codec);
3911 EXPORT_SYMBOL(snd_hda_parse_generic_codec);