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 <sound/core.h>
28 #include <sound/jack.h>
29 #include "hda_codec.h"
30 #include "hda_local.h"
31 #include "hda_auto_parser.h"
33 #include "hda_generic.h"
36 /* initialize hda_gen_spec struct */
37 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
39 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
40 snd_array_init(&spec->bind_ctls, sizeof(struct hda_bind_ctls *), 8);
41 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
44 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
46 static struct snd_kcontrol_new *
47 add_kctl(struct hda_gen_spec *spec, const char *name,
48 const struct snd_kcontrol_new *temp)
50 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
55 knew->name = kstrdup(name, GFP_KERNEL);
57 knew->name = kstrdup(knew->name, GFP_KERNEL);
63 static void free_kctls(struct hda_gen_spec *spec)
65 if (spec->kctls.list) {
66 struct snd_kcontrol_new *kctl = spec->kctls.list;
68 for (i = 0; i < spec->kctls.used; i++)
71 snd_array_free(&spec->kctls);
74 static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
76 struct hda_ctl_ops *ops)
78 struct hda_gen_spec *spec = codec->spec;
79 struct hda_bind_ctls **ctlp, *ctl;
80 ctlp = snd_array_new(&spec->bind_ctls);
83 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
90 static void free_bind_ctls(struct hda_gen_spec *spec)
92 if (spec->bind_ctls.list) {
93 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
95 for (i = 0; i < spec->bind_ctls.used; i++)
98 snd_array_free(&spec->bind_ctls);
101 void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
106 free_bind_ctls(spec);
107 snd_array_free(&spec->paths);
109 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
115 /* get the path between the given NIDs;
116 * passing 0 to either @pin or @dac behaves as a wildcard
118 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
119 hda_nid_t from_nid, hda_nid_t to_nid)
121 struct hda_gen_spec *spec = codec->spec;
124 for (i = 0; i < spec->paths.used; i++) {
125 struct nid_path *path = snd_array_elem(&spec->paths, i);
126 if (path->depth <= 0)
128 if ((!from_nid || path->path[0] == from_nid) &&
129 (!to_nid || path->path[path->depth - 1] == to_nid))
134 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
136 /* check whether the given DAC is already found in any existing paths */
137 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
139 struct hda_gen_spec *spec = codec->spec;
142 for (i = 0; i < spec->paths.used; i++) {
143 struct nid_path *path = snd_array_elem(&spec->paths, i);
144 if (path->path[0] == nid)
150 /* check whether the given two widgets can be connected */
151 static bool is_reachable_path(struct hda_codec *codec,
152 hda_nid_t from_nid, hda_nid_t to_nid)
154 if (!from_nid || !to_nid)
156 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
159 /* nid, dir and idx */
160 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
162 /* check whether the given ctl is already assigned in any path elements */
163 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
165 struct hda_gen_spec *spec = codec->spec;
168 val &= AMP_VAL_COMPARE_MASK;
169 for (i = 0; i < spec->paths.used; i++) {
170 struct nid_path *path = snd_array_elem(&spec->paths, i);
171 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
177 /* check whether a control with the given (nid, dir, idx) was assigned */
178 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
181 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
182 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
183 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
186 /* called recursively */
187 static bool __parse_nid_path(struct hda_codec *codec,
188 hda_nid_t from_nid, hda_nid_t to_nid,
189 int with_aa_mix, struct nid_path *path, int depth)
191 struct hda_gen_spec *spec = codec->spec;
195 if (to_nid == spec->mixer_nid) {
198 with_aa_mix = 2; /* mark aa-mix is included */
201 nums = snd_hda_get_connections(codec, to_nid, conn, ARRAY_SIZE(conn));
202 for (i = 0; i < nums; i++) {
203 if (conn[i] != from_nid) {
204 /* special case: when from_nid is 0,
205 * try to find an empty DAC
208 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
209 is_dac_already_used(codec, conn[i]))
212 /* aa-mix is requested but not included? */
213 if (!(spec->mixer_nid && with_aa_mix == 1))
216 if (depth >= MAX_NID_PATH_DEPTH)
218 for (i = 0; i < nums; i++) {
220 type = get_wcaps_type(get_wcaps(codec, conn[i]));
221 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
224 if (__parse_nid_path(codec, from_nid, conn[i],
225 with_aa_mix, path, depth + 1))
231 path->path[path->depth] = conn[i];
232 path->idx[path->depth + 1] = i;
233 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
234 path->multi[path->depth + 1] = 1;
239 /* parse the widget path from the given nid to the target nid;
240 * when @from_nid is 0, try to find an empty DAC;
241 * when @with_aa_mix is 0, paths with spec->mixer_nid are excluded.
242 * when @with_aa_mix is 1, paths without spec->mixer_nid are excluded.
243 * when @with_aa_mix is 2, no special handling about spec->mixer_nid.
245 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
246 hda_nid_t to_nid, int with_aa_mix,
247 struct nid_path *path)
249 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
250 path->path[path->depth] = to_nid;
253 snd_printdd("path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
254 path->depth, path->path[0], path->path[1],
255 path->path[2], path->path[3], path->path[4]);
261 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
264 * parse the path between the given NIDs and add to the path list.
265 * if no valid path is found, return NULL
268 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
269 hda_nid_t to_nid, int with_aa_mix)
271 struct hda_gen_spec *spec = codec->spec;
272 struct nid_path *path;
274 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
277 path = snd_array_new(&spec->paths);
280 memset(path, 0, sizeof(*path));
281 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
287 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
289 /* look for an empty DAC slot */
290 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
293 struct hda_gen_spec *spec = codec->spec;
297 for (i = 0; i < spec->num_all_dacs; i++) {
298 hda_nid_t nid = spec->all_dacs[i];
299 if (!nid || is_dac_already_used(codec, nid))
301 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
302 if (is_digital != cap_digital)
304 if (is_reachable_path(codec, nid, pin))
310 /* replace the channels in the composed amp value with the given number */
311 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
313 val &= ~(0x3U << 16);
318 /* check whether the widget has the given amp capability for the direction */
319 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
320 int dir, unsigned int bits)
324 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
325 if (query_amp_caps(codec, nid, dir) & bits)
330 #define nid_has_mute(codec, nid, dir) \
331 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
332 #define nid_has_volume(codec, nid, dir) \
333 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
335 /* look for a widget suitable for assigning a mute switch in the path */
336 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
337 struct nid_path *path)
341 for (i = path->depth - 1; i >= 0; i--) {
342 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
343 return path->path[i];
344 if (i != path->depth - 1 && i != 0 &&
345 nid_has_mute(codec, path->path[i], HDA_INPUT))
346 return path->path[i];
351 /* look for a widget suitable for assigning a volume ctl in the path */
352 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
353 struct nid_path *path)
357 for (i = path->depth - 1; i >= 0; i--) {
358 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
359 return path->path[i];
365 * path activation / deactivation
368 /* can have the amp-in capability? */
369 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
371 hda_nid_t nid = path->path[idx];
372 unsigned int caps = get_wcaps(codec, nid);
373 unsigned int type = get_wcaps_type(caps);
375 if (!(caps & AC_WCAP_IN_AMP))
377 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
382 /* can have the amp-out capability? */
383 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
385 hda_nid_t nid = path->path[idx];
386 unsigned int caps = get_wcaps(codec, nid);
387 unsigned int type = get_wcaps_type(caps);
389 if (!(caps & AC_WCAP_OUT_AMP))
391 if (type == AC_WID_PIN && !idx) /* only for output pins */
396 /* check whether the given (nid,dir,idx) is active */
397 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
398 unsigned int idx, unsigned int dir)
400 struct hda_gen_spec *spec = codec->spec;
403 for (n = 0; n < spec->paths.used; n++) {
404 struct nid_path *path = snd_array_elem(&spec->paths, n);
407 for (i = 0; i < path->depth; i++) {
408 if (path->path[i] == nid) {
409 if (dir == HDA_OUTPUT || path->idx[i] == idx)
418 /* get the default amp value for the target state */
419 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
420 int dir, bool enable)
423 unsigned int val = 0;
425 caps = query_amp_caps(codec, nid, dir);
426 if (caps & AC_AMPCAP_NUM_STEPS) {
429 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
431 if (caps & AC_AMPCAP_MUTE) {
438 /* initialize the amp value (only at the first time) */
439 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
441 int val = get_amp_val_to_activate(codec, nid, dir, false);
442 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
445 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
446 int idx, bool enable)
449 if (is_ctl_associated(codec, nid, dir, idx) ||
450 is_active_nid(codec, nid, dir, idx))
452 val = get_amp_val_to_activate(codec, nid, dir, enable);
453 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
456 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
459 hda_nid_t nid = path->path[i];
460 init_amp(codec, nid, HDA_OUTPUT, 0);
461 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
464 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
465 int i, bool enable, bool add_aamix)
467 struct hda_gen_spec *spec = codec->spec;
471 hda_nid_t nid = path->path[i];
473 nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
474 type = get_wcaps_type(get_wcaps(codec, nid));
475 if (type == AC_WID_PIN ||
476 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
482 for (n = 0; n < nums; n++)
483 init_amp(codec, nid, HDA_INPUT, n);
485 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
488 /* here is a little bit tricky in comparison with activate_amp_out();
489 * when aa-mixer is available, we need to enable the path as well
491 for (n = 0; n < nums; n++) {
492 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
494 activate_amp(codec, nid, HDA_INPUT, n, enable);
498 /* activate or deactivate the given path
499 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
501 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
502 bool enable, bool add_aamix)
507 path->active = false;
509 for (i = path->depth - 1; i >= 0; i--) {
510 if (enable && path->multi[i])
511 snd_hda_codec_write_cache(codec, path->path[i], 0,
512 AC_VERB_SET_CONNECT_SEL,
514 if (has_amp_in(codec, path, i))
515 activate_amp_in(codec, path, i, enable, add_aamix);
516 if (has_amp_out(codec, path, i))
517 activate_amp_out(codec, path, i, enable);
523 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
527 * Helper functions for creating mixer ctl elements
537 static const struct snd_kcontrol_new control_templates[] = {
538 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
539 HDA_CODEC_MUTE(NULL, 0, 0, 0),
540 HDA_BIND_MUTE(NULL, 0, 0, 0),
541 HDA_BIND_VOL(NULL, 0),
542 HDA_BIND_SW(NULL, 0),
545 /* add dynamic controls from template */
546 static int add_control(struct hda_gen_spec *spec, int type, const char *name,
547 int cidx, unsigned long val)
549 struct snd_kcontrol_new *knew;
551 knew = add_kctl(spec, name, &control_templates[type]);
555 if (get_amp_nid_(val))
556 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
557 knew->private_value = val;
561 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
562 const char *pfx, const char *dir,
563 const char *sfx, int cidx, unsigned long val)
566 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
567 return add_control(spec, type, name, cidx, val);
570 #define add_pb_vol_ctrl(spec, type, pfx, val) \
571 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
572 #define add_pb_sw_ctrl(spec, type, pfx, val) \
573 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
574 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
575 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
576 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
577 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
579 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
580 unsigned int chs, struct nid_path *path)
585 val = path->ctls[NID_PATH_VOL_CTL];
588 val = amp_val_replace_channels(val, chs);
589 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
592 /* return the channel bits suitable for the given path->ctls[] */
593 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
596 int chs = 1; /* mono (left only) */
598 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
599 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
600 chs = 3; /* stereo */
605 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
606 struct nid_path *path)
608 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
609 return add_vol_ctl(codec, pfx, cidx, chs, path);
612 /* create a mute-switch for the given mixer widget;
613 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
615 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
616 unsigned int chs, struct nid_path *path)
619 int type = HDA_CTL_WIDGET_MUTE;
623 val = path->ctls[NID_PATH_MUTE_CTL];
626 val = amp_val_replace_channels(val, chs);
627 if (get_amp_direction_(val) == HDA_INPUT) {
628 hda_nid_t nid = get_amp_nid_(val);
629 int nums = snd_hda_get_num_conns(codec, nid);
631 type = HDA_CTL_BIND_MUTE;
635 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
638 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
639 int cidx, struct nid_path *path)
641 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
642 return add_sw_ctl(codec, pfx, cidx, chs, path);
645 static const char * const channel_name[4] = {
646 "Front", "Surround", "CLFE", "Side"
649 /* give some appropriate ctl name prefix for the given line out channel */
650 static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
651 bool can_be_master, int *index)
653 struct auto_pin_cfg *cfg = &spec->autocfg;
656 if (cfg->line_outs == 1 && !spec->multi_ios &&
657 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
658 return spec->vmaster_mute.hook ? "PCM" : "Master";
660 /* if there is really a single DAC used in the whole output paths,
661 * use it master (or "PCM" if a vmaster hook is present)
663 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
664 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
665 return spec->vmaster_mute.hook ? "PCM" : "Master";
667 switch (cfg->line_out_type) {
668 case AUTO_PIN_SPEAKER_OUT:
669 if (cfg->line_outs == 1)
671 if (cfg->line_outs == 2)
672 return ch ? "Bass Speaker" : "Speaker";
674 case AUTO_PIN_HP_OUT:
675 /* for multi-io case, only the primary out */
676 if (ch && spec->multi_ios)
681 if (cfg->line_outs == 1 && !spec->multi_ios)
685 if (ch >= ARRAY_SIZE(channel_name)) {
690 return channel_name[ch];
697 /* badness definition */
699 /* No primary DAC is found for the main output */
700 BAD_NO_PRIMARY_DAC = 0x10000,
701 /* No DAC is found for the extra output */
703 /* No possible multi-ios */
704 BAD_MULTI_IO = 0x103,
705 /* No individual DAC for extra output */
706 BAD_NO_EXTRA_DAC = 0x102,
707 /* No individual DAC for extra surrounds */
708 BAD_NO_EXTRA_SURR_DAC = 0x101,
709 /* Primary DAC shared with main surrounds */
710 BAD_SHARED_SURROUND = 0x100,
711 /* Primary DAC shared with main CLFE */
712 BAD_SHARED_CLFE = 0x10,
713 /* Primary DAC shared with extra surrounds */
714 BAD_SHARED_EXTRA_SURROUND = 0x10,
715 /* Volume widget is shared */
716 BAD_SHARED_VOL = 0x10,
719 /* look for widgets in the path between the given NIDs appropriate for
720 * volume and mute controls, and assign the values to ctls[].
722 * When no appropriate widget is found in the path, the badness value
723 * is incremented depending on the situation. The function returns the
724 * total badness for both volume and mute controls.
726 static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
729 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
735 return BAD_SHARED_VOL * 2;
736 nid = look_for_out_vol_nid(codec, path);
738 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
739 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
740 badness += BAD_SHARED_VOL;
742 path->ctls[NID_PATH_VOL_CTL] = val;
744 badness += BAD_SHARED_VOL;
745 nid = look_for_out_mute_nid(codec, path);
747 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
748 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
749 nid_has_mute(codec, nid, HDA_OUTPUT))
750 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
752 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
753 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
754 badness += BAD_SHARED_VOL;
756 path->ctls[NID_PATH_MUTE_CTL] = val;
758 badness += BAD_SHARED_VOL;
762 struct badness_table {
763 int no_primary_dac; /* no primary DAC */
764 int no_dac; /* no secondary DACs */
765 int shared_primary; /* primary DAC is shared with main output */
766 int shared_surr; /* secondary DAC shared with main or primary */
767 int shared_clfe; /* third DAC shared with main or primary */
768 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
771 static struct badness_table main_out_badness = {
772 .no_primary_dac = BAD_NO_PRIMARY_DAC,
773 .no_dac = BAD_NO_DAC,
774 .shared_primary = BAD_NO_PRIMARY_DAC,
775 .shared_surr = BAD_SHARED_SURROUND,
776 .shared_clfe = BAD_SHARED_CLFE,
777 .shared_surr_main = BAD_SHARED_SURROUND,
780 static struct badness_table extra_out_badness = {
781 .no_primary_dac = BAD_NO_DAC,
782 .no_dac = BAD_NO_DAC,
783 .shared_primary = BAD_NO_EXTRA_DAC,
784 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
785 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
786 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
789 /* try to assign DACs to pins and return the resultant badness */
790 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
791 const hda_nid_t *pins, hda_nid_t *dacs,
792 const struct badness_table *bad)
794 struct hda_gen_spec *spec = codec->spec;
795 struct auto_pin_cfg *cfg = &spec->autocfg;
803 for (i = 0; i < num_outs; i++) {
804 hda_nid_t pin = pins[i];
806 dacs[i] = look_for_dac(codec, pin, false);
807 if (!dacs[i] && !i) {
808 for (j = 1; j < num_outs; j++) {
809 if (is_reachable_path(codec, dacs[j], pin)) {
818 if (is_reachable_path(codec, dacs[0], pin))
820 else if (cfg->line_outs > i &&
821 is_reachable_path(codec, spec->private_dac_nids[i], pin))
822 dac = spec->private_dac_nids[i];
825 badness += bad->shared_primary;
827 badness += bad->shared_surr;
829 badness += bad->shared_clfe;
830 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
831 dac = spec->private_dac_nids[0];
832 badness += bad->shared_surr_main;
834 badness += bad->no_primary_dac;
836 badness += bad->no_dac;
838 if (!snd_hda_add_new_path(codec, dac, pin, 0))
841 badness += assign_out_path_ctls(codec, pin, dac);
847 /* return NID if the given pin has only a single connection to a certain DAC */
848 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
850 struct hda_gen_spec *spec = codec->spec;
852 hda_nid_t nid_found = 0;
854 for (i = 0; i < spec->num_all_dacs; i++) {
855 hda_nid_t nid = spec->all_dacs[i];
856 if (!nid || is_dac_already_used(codec, nid))
858 if (is_reachable_path(codec, nid, pin)) {
867 /* check whether the given pin can be a multi-io pin */
868 static bool can_be_multiio_pin(struct hda_codec *codec,
869 unsigned int location, hda_nid_t nid)
871 unsigned int defcfg, caps;
873 defcfg = snd_hda_codec_get_pincfg(codec, nid);
874 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
876 if (location && get_defcfg_location(defcfg) != location)
878 caps = snd_hda_query_pin_caps(codec, nid);
879 if (!(caps & AC_PINCAP_OUT))
887 * When hardwired is set, try to fill ony hardwired pins, and returns
888 * zero if any pins are filled, non-zero if nothing found.
889 * When hardwired is off, try to fill possible input pins, and returns
892 static int fill_multi_ios(struct hda_codec *codec,
893 hda_nid_t reference_pin,
894 bool hardwired, int offset)
896 struct hda_gen_spec *spec = codec->spec;
897 struct auto_pin_cfg *cfg = &spec->autocfg;
898 int type, i, j, dacs, num_pins, old_pins;
899 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
900 unsigned int location = get_defcfg_location(defcfg);
903 old_pins = spec->multi_ios;
908 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
909 for (i = 0; i < cfg->num_inputs; i++) {
910 if (cfg->inputs[i].type != type)
912 if (can_be_multiio_pin(codec, location,
920 dacs = spec->multiout.num_dacs;
921 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
922 for (i = 0; i < cfg->num_inputs; i++) {
923 hda_nid_t nid = cfg->inputs[i].pin;
926 if (cfg->inputs[i].type != type)
928 if (!can_be_multiio_pin(codec, location, nid))
930 for (j = 0; j < spec->multi_ios; j++) {
931 if (nid == spec->multi_io[j].pin)
934 if (j < spec->multi_ios)
937 if (offset && offset + spec->multi_ios < dacs) {
938 dac = spec->private_dac_nids[offset + spec->multi_ios];
939 if (!is_reachable_path(codec, dac, nid))
943 dac = get_dac_if_single(codec, nid);
945 dac = look_for_dac(codec, nid, false);
950 if (!snd_hda_add_new_path(codec, dac, nid, 0)) {
954 spec->multi_io[spec->multi_ios].pin = nid;
955 spec->multi_io[spec->multi_ios].dac = dac;
957 if (spec->multi_ios >= 2)
963 badness = BAD_MULTI_IO;
964 if (old_pins == spec->multi_ios) {
966 return 1; /* nothing found */
968 return badness; /* no badness if nothing found */
970 if (!hardwired && spec->multi_ios < 2) {
971 /* cancel newly assigned paths */
972 spec->paths.used -= spec->multi_ios - old_pins;
973 spec->multi_ios = old_pins;
977 /* assign volume and mute controls */
978 for (i = old_pins; i < spec->multi_ios; i++)
979 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
980 spec->multi_io[i].dac);
985 /* map DACs for all pins in the list if they are single connections */
986 static bool map_singles(struct hda_codec *codec, int outs,
987 const hda_nid_t *pins, hda_nid_t *dacs)
991 for (i = 0; i < outs; i++) {
995 dac = get_dac_if_single(codec, pins[i]);
998 if (snd_hda_add_new_path(codec, dac, pins[i], 0)) {
1006 /* fill in the dac_nids table from the parsed pin configuration */
1007 static int fill_and_eval_dacs(struct hda_codec *codec,
1008 bool fill_hardwired,
1009 bool fill_mio_first)
1011 struct hda_gen_spec *spec = codec->spec;
1012 struct auto_pin_cfg *cfg = &spec->autocfg;
1013 int i, err, badness;
1015 /* set num_dacs once to full for look_for_dac() */
1016 spec->multiout.num_dacs = cfg->line_outs;
1017 spec->multiout.dac_nids = spec->private_dac_nids;
1018 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1019 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1020 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1021 spec->multi_ios = 0;
1022 snd_array_free(&spec->paths);
1025 /* fill hard-wired DACs first */
1026 if (fill_hardwired) {
1029 mapped = map_singles(codec, cfg->line_outs,
1031 spec->private_dac_nids);
1032 mapped |= map_singles(codec, cfg->hp_outs,
1034 spec->multiout.hp_out_nid);
1035 mapped |= map_singles(codec, cfg->speaker_outs,
1037 spec->multiout.extra_out_nid);
1038 if (fill_mio_first && cfg->line_outs == 1 &&
1039 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1040 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1047 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1048 spec->private_dac_nids,
1051 /* re-count num_dacs and squash invalid entries */
1052 spec->multiout.num_dacs = 0;
1053 for (i = 0; i < cfg->line_outs; i++) {
1054 if (spec->private_dac_nids[i])
1055 spec->multiout.num_dacs++;
1057 memmove(spec->private_dac_nids + i,
1058 spec->private_dac_nids + i + 1,
1059 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1060 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1064 if (fill_mio_first &&
1065 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1066 /* try to fill multi-io first */
1067 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1070 /* we don't count badness at this stage yet */
1073 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1074 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1075 spec->multiout.hp_out_nid,
1076 &extra_out_badness);
1081 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1082 err = try_assign_dacs(codec, cfg->speaker_outs,
1084 spec->multiout.extra_out_nid,
1085 &extra_out_badness);
1090 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1091 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1096 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1097 /* try multi-ios with HP + inputs */
1099 if (cfg->line_outs >= 3)
1101 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1107 if (spec->multi_ios == 2) {
1108 for (i = 0; i < 2; i++)
1109 spec->private_dac_nids[spec->multiout.num_dacs++] =
1110 spec->multi_io[i].dac;
1111 spec->ext_channel_count = 2;
1112 } else if (spec->multi_ios) {
1113 spec->multi_ios = 0;
1114 badness += BAD_MULTI_IO;
1120 #define DEBUG_BADNESS
1122 #ifdef DEBUG_BADNESS
1123 #define debug_badness snd_printdd
1125 #define debug_badness(...)
1128 static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1130 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1131 cfg->line_out_pins[0], cfg->line_out_pins[1],
1132 cfg->line_out_pins[2], cfg->line_out_pins[2],
1133 spec->multiout.dac_nids[0],
1134 spec->multiout.dac_nids[1],
1135 spec->multiout.dac_nids[2],
1136 spec->multiout.dac_nids[3]);
1137 if (spec->multi_ios > 0)
1138 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1140 spec->multi_io[0].pin, spec->multi_io[1].pin,
1141 spec->multi_io[0].dac, spec->multi_io[1].dac);
1142 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1143 cfg->hp_pins[0], cfg->hp_pins[1],
1144 cfg->hp_pins[2], cfg->hp_pins[2],
1145 spec->multiout.hp_out_nid[0],
1146 spec->multiout.hp_out_nid[1],
1147 spec->multiout.hp_out_nid[2],
1148 spec->multiout.hp_out_nid[3]);
1149 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1150 cfg->speaker_pins[0], cfg->speaker_pins[1],
1151 cfg->speaker_pins[2], cfg->speaker_pins[3],
1152 spec->multiout.extra_out_nid[0],
1153 spec->multiout.extra_out_nid[1],
1154 spec->multiout.extra_out_nid[2],
1155 spec->multiout.extra_out_nid[3]);
1158 /* find all available DACs of the codec */
1159 static void fill_all_dac_nids(struct hda_codec *codec)
1161 struct hda_gen_spec *spec = codec->spec;
1163 hda_nid_t nid = codec->start_nid;
1165 spec->num_all_dacs = 0;
1166 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1167 for (i = 0; i < codec->num_nodes; i++, nid++) {
1168 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1170 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1171 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1174 spec->all_dacs[spec->num_all_dacs++] = nid;
1178 static int parse_output_paths(struct hda_codec *codec)
1180 struct hda_gen_spec *spec = codec->spec;
1181 struct auto_pin_cfg *cfg = &spec->autocfg;
1182 struct auto_pin_cfg *best_cfg;
1183 int best_badness = INT_MAX;
1185 bool fill_hardwired = true, fill_mio_first = true;
1186 bool best_wired = true, best_mio = true;
1187 bool hp_spk_swapped = false;
1189 fill_all_dac_nids(codec);
1191 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1197 badness = fill_and_eval_dacs(codec, fill_hardwired,
1203 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1204 cfg->line_out_type, fill_hardwired, fill_mio_first,
1206 debug_show_configs(spec, cfg);
1207 if (badness < best_badness) {
1208 best_badness = badness;
1210 best_wired = fill_hardwired;
1211 best_mio = fill_mio_first;
1215 fill_mio_first = !fill_mio_first;
1216 if (!fill_mio_first)
1218 fill_hardwired = !fill_hardwired;
1219 if (!fill_hardwired)
1223 hp_spk_swapped = true;
1224 if (cfg->speaker_outs > 0 &&
1225 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1226 cfg->hp_outs = cfg->line_outs;
1227 memcpy(cfg->hp_pins, cfg->line_out_pins,
1228 sizeof(cfg->hp_pins));
1229 cfg->line_outs = cfg->speaker_outs;
1230 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1231 sizeof(cfg->speaker_pins));
1232 cfg->speaker_outs = 0;
1233 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1234 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1235 fill_hardwired = true;
1238 if (cfg->hp_outs > 0 &&
1239 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1240 cfg->speaker_outs = cfg->line_outs;
1241 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1242 sizeof(cfg->speaker_pins));
1243 cfg->line_outs = cfg->hp_outs;
1244 memcpy(cfg->line_out_pins, cfg->hp_pins,
1245 sizeof(cfg->hp_pins));
1247 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1248 cfg->line_out_type = AUTO_PIN_HP_OUT;
1249 fill_hardwired = true;
1257 fill_and_eval_dacs(codec, best_wired, best_mio);
1259 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1260 cfg->line_out_type, best_wired, best_mio);
1261 debug_show_configs(spec, cfg);
1263 if (cfg->line_out_pins[0]) {
1264 struct nid_path *path;
1265 path = snd_hda_get_nid_path(codec,
1266 spec->multiout.dac_nids[0],
1267 cfg->line_out_pins[0]);
1269 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1276 /* add playback controls from the parsed DAC table */
1277 static int create_multi_out_ctls(struct hda_codec *codec,
1278 const struct auto_pin_cfg *cfg)
1280 struct hda_gen_spec *spec = codec->spec;
1281 int i, err, noutputs;
1283 noutputs = cfg->line_outs;
1284 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1285 noutputs += spec->multi_ios;
1287 for (i = 0; i < noutputs; i++) {
1291 struct nid_path *path;
1293 dac = spec->multiout.dac_nids[i];
1296 if (i >= cfg->line_outs) {
1297 pin = spec->multi_io[i - 1].pin;
1299 name = channel_name[i];
1301 pin = cfg->line_out_pins[i];
1302 name = get_line_out_pfx(spec, i, true, &index);
1305 path = snd_hda_get_nid_path(codec, dac, pin);
1308 if (!name || !strcmp(name, "CLFE")) {
1310 err = add_vol_ctl(codec, "Center", 0, 1, path);
1313 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1316 err = add_sw_ctl(codec, "Center", 0, 1, path);
1319 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1323 err = add_stereo_vol(codec, name, index, path);
1326 err = add_stereo_sw(codec, name, index, path);
1334 static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1335 hda_nid_t dac, const char *pfx, int cidx)
1337 struct nid_path *path;
1340 path = snd_hda_get_nid_path(codec, dac, pin);
1343 /* bind volume control will be created in the case of dac = 0 */
1345 err = add_stereo_vol(codec, pfx, cidx, path);
1349 err = add_stereo_sw(codec, pfx, cidx, path);
1355 /* add playback controls for speaker and HP outputs */
1356 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1357 const hda_nid_t *pins, const hda_nid_t *dacs,
1360 struct hda_gen_spec *spec = codec->spec;
1361 struct hda_bind_ctls *ctl;
1365 if (!num_pins || !pins[0])
1368 if (num_pins == 1) {
1369 hda_nid_t dac = *dacs;
1371 dac = spec->multiout.dac_nids[0];
1372 return create_extra_out(codec, *pins, dac, pfx, 0);
1375 for (i = 0; i < num_pins; i++) {
1377 if (dacs[num_pins - 1])
1378 dac = dacs[i]; /* with individual volumes */
1381 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1382 err = create_extra_out(codec, pins[i], dac,
1384 } else if (num_pins >= 3) {
1385 snprintf(name, sizeof(name), "%s %s",
1386 pfx, channel_name[i]);
1387 err = create_extra_out(codec, pins[i], dac, name, 0);
1389 err = create_extra_out(codec, pins[i], dac, pfx, i);
1394 if (dacs[num_pins - 1])
1397 /* Let's create a bind-controls for volumes */
1398 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1402 for (i = 0; i < num_pins; i++) {
1404 struct nid_path *path;
1405 if (!pins[i] || !dacs[i])
1407 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1410 vol = look_for_out_vol_nid(codec, path);
1413 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1416 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1417 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1424 static int create_hp_out_ctls(struct hda_codec *codec)
1426 struct hda_gen_spec *spec = codec->spec;
1427 return create_extra_outs(codec, spec->autocfg.hp_outs,
1428 spec->autocfg.hp_pins,
1429 spec->multiout.hp_out_nid,
1433 static int create_speaker_out_ctls(struct hda_codec *codec)
1435 struct hda_gen_spec *spec = codec->spec;
1436 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1437 spec->autocfg.speaker_pins,
1438 spec->multiout.extra_out_nid,
1443 * channel mode enum control
1446 static int ch_mode_info(struct snd_kcontrol *kcontrol,
1447 struct snd_ctl_elem_info *uinfo)
1449 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1450 struct hda_gen_spec *spec = codec->spec;
1452 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1454 uinfo->value.enumerated.items = spec->multi_ios + 1;
1455 if (uinfo->value.enumerated.item > spec->multi_ios)
1456 uinfo->value.enumerated.item = spec->multi_ios;
1457 sprintf(uinfo->value.enumerated.name, "%dch",
1458 (uinfo->value.enumerated.item + 1) * 2);
1462 static int ch_mode_get(struct snd_kcontrol *kcontrol,
1463 struct snd_ctl_elem_value *ucontrol)
1465 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1466 struct hda_gen_spec *spec = codec->spec;
1467 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1471 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1473 struct hda_gen_spec *spec = codec->spec;
1474 hda_nid_t nid = spec->multi_io[idx].pin;
1475 struct nid_path *path;
1477 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1481 if (path->active == output)
1485 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1486 snd_hda_activate_path(codec, path, true, true);
1488 snd_hda_activate_path(codec, path, false, true);
1489 snd_hda_set_pin_ctl_cache(codec, nid,
1490 spec->multi_io[idx].ctl_in);
1495 static int ch_mode_put(struct snd_kcontrol *kcontrol,
1496 struct snd_ctl_elem_value *ucontrol)
1498 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1499 struct hda_gen_spec *spec = codec->spec;
1502 ch = ucontrol->value.enumerated.item[0];
1503 if (ch < 0 || ch > spec->multi_ios)
1505 if (ch == (spec->ext_channel_count - 1) / 2)
1507 spec->ext_channel_count = (ch + 1) * 2;
1508 for (i = 0; i < spec->multi_ios; i++)
1509 set_multi_io(codec, i, i < ch);
1510 spec->multiout.max_channels = max(spec->ext_channel_count,
1511 spec->const_channel_count);
1512 if (spec->need_dac_fix)
1513 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1517 static const struct snd_kcontrol_new channel_mode_enum = {
1518 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1519 .name = "Channel Mode",
1520 .info = ch_mode_info,
1525 static int create_multi_channel_mode(struct hda_codec *codec)
1527 struct hda_gen_spec *spec = codec->spec;
1529 if (spec->multi_ios > 0) {
1530 if (!add_kctl(spec, NULL, &channel_mode_enum))
1537 * shared headphone/mic handling
1540 static void call_update_outputs(struct hda_codec *codec);
1542 /* for shared I/O, change the pin-control accordingly */
1543 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1545 struct hda_gen_spec *spec = codec->spec;
1547 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1548 /* NOTE: this assumes that there are only two inputs, the
1549 * first is the real internal mic and the second is HP/mic jack.
1552 val = snd_hda_get_default_vref(codec, pin);
1554 /* This pin does not have vref caps - let's enable vref on pin 0x18
1555 instead, as suggested by Realtek */
1556 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1557 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1558 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1559 if (vref_val != AC_PINCTL_VREF_HIZ)
1560 snd_hda_set_pin_ctl(codec, vref_pin, PIN_IN | (set_as_mic ? vref_val : 0));
1563 val = set_as_mic ? val | PIN_IN : PIN_HP;
1564 snd_hda_set_pin_ctl(codec, pin, val);
1566 spec->automute_speaker = !set_as_mic;
1567 call_update_outputs(codec);
1570 /* create a shared input with the headphone out */
1571 static int create_shared_input(struct hda_codec *codec)
1573 struct hda_gen_spec *spec = codec->spec;
1574 struct auto_pin_cfg *cfg = &spec->autocfg;
1575 unsigned int defcfg;
1578 /* only one internal input pin? */
1579 if (cfg->num_inputs != 1)
1581 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1582 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1585 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1586 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1587 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1588 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1590 return 0; /* both not available */
1592 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1593 return 0; /* no input */
1595 cfg->inputs[1].pin = nid;
1596 cfg->inputs[1].type = AUTO_PIN_MIC;
1597 cfg->num_inputs = 2;
1598 spec->shared_mic_hp = 1;
1599 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1609 /* add the powersave loopback-list entry */
1610 static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1612 struct hda_amp_list *list;
1614 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1616 list = spec->loopback_list + spec->num_loopbacks;
1618 list->dir = HDA_INPUT;
1620 spec->num_loopbacks++;
1621 spec->loopback.amplist = spec->loopback_list;
1624 #define add_loopback_list(spec, mix, idx) /* NOP */
1627 /* create input playback/capture controls for the given pin */
1628 static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1629 const char *ctlname, int ctlidx,
1632 struct hda_gen_spec *spec = codec->spec;
1633 struct nid_path *path;
1637 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1638 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1639 return 0; /* no need for analog loopback */
1641 path = snd_hda_add_new_path(codec, pin, mix_nid, 2);
1645 idx = path->idx[path->depth - 1];
1646 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1647 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1648 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
1651 path->ctls[NID_PATH_VOL_CTL] = val;
1654 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1655 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1656 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
1659 path->ctls[NID_PATH_MUTE_CTL] = val;
1662 path->active = true;
1663 add_loopback_list(spec, mix_nid, idx);
1667 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
1669 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1670 return (pincap & AC_PINCAP_IN) != 0;
1673 /* Parse the codec tree and retrieve ADCs */
1674 static int fill_adc_nids(struct hda_codec *codec)
1676 struct hda_gen_spec *spec = codec->spec;
1678 hda_nid_t *adc_nids = spec->adc_nids;
1679 int max_nums = ARRAY_SIZE(spec->adc_nids);
1682 nid = codec->start_nid;
1683 for (i = 0; i < codec->num_nodes; i++, nid++) {
1684 unsigned int caps = get_wcaps(codec, nid);
1685 int type = get_wcaps_type(caps);
1687 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1689 adc_nids[nums] = nid;
1690 if (++nums >= max_nums)
1693 spec->num_adc_nids = nums;
1697 /* filter out invalid adc_nids that don't give all active input pins;
1698 * if needed, check whether dynamic ADC-switching is available
1700 static int check_dyn_adc_switch(struct hda_codec *codec)
1702 struct hda_gen_spec *spec = codec->spec;
1703 struct hda_input_mux *imux = &spec->input_mux;
1704 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1710 for (n = 0; n < spec->num_adc_nids; n++) {
1711 adc = spec->adc_nids[n];
1712 for (i = 0; i < imux->num_items; i++) {
1713 pin = spec->imux_pins[i];
1714 if (!is_reachable_path(codec, pin, adc))
1717 if (i >= imux->num_items)
1718 adc_nids[nums++] = adc;
1722 if (spec->shared_mic_hp) {
1723 spec->shared_mic_hp = 0;
1724 imux->num_items = 1;
1728 /* check whether ADC-switch is possible */
1729 for (i = 0; i < imux->num_items; i++) {
1730 pin = spec->imux_pins[i];
1731 for (n = 0; n < spec->num_adc_nids; n++) {
1732 adc = spec->adc_nids[n];
1733 if (is_reachable_path(codec, pin, adc)) {
1734 spec->dyn_adc_idx[i] = n;
1740 snd_printdd("hda-codec: enabling ADC switching\n");
1741 spec->dyn_adc_switch = 1;
1742 } else if (nums != spec->num_adc_nids) {
1743 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1744 spec->num_adc_nids = nums;
1747 if (imux->num_items == 1 || spec->shared_mic_hp) {
1748 snd_printdd("hda-codec: reducing to a single ADC\n");
1749 spec->num_adc_nids = 1; /* reduce to a single ADC */
1752 /* single index for individual volumes ctls */
1753 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1754 spec->num_adc_nids = 1;
1760 * create playback/capture controls for input pins
1762 static int create_input_ctls(struct hda_codec *codec)
1764 struct hda_gen_spec *spec = codec->spec;
1765 const struct auto_pin_cfg *cfg = &spec->autocfg;
1766 hda_nid_t mixer = spec->mixer_nid;
1767 struct hda_input_mux *imux = &spec->input_mux;
1769 int i, c, err, type_idx = 0;
1770 const char *prev_label = NULL;
1772 num_adcs = fill_adc_nids(codec);
1776 for (i = 0; i < cfg->num_inputs; i++) {
1781 pin = cfg->inputs[i].pin;
1782 if (!is_input_pin(codec, pin))
1785 label = hda_get_autocfg_input_label(codec, cfg, i);
1786 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1787 label = "Headphone Mic";
1788 if (prev_label && !strcmp(label, prev_label))
1795 if (is_reachable_path(codec, pin, mixer)) {
1796 err = new_analog_input(codec, pin,
1797 label, type_idx, mixer);
1804 for (c = 0; c < num_adcs; c++) {
1805 struct nid_path *path;
1806 hda_nid_t adc = spec->adc_nids[c];
1808 if (!is_reachable_path(codec, pin, adc))
1810 path = snd_array_new(&spec->paths);
1813 memset(path, 0, sizeof(*path));
1814 if (!snd_hda_parse_nid_path(codec, pin, adc, 2, path)) {
1816 "invalid input path 0x%x -> 0x%x\n",
1823 spec->imux_pins[imux->num_items] = pin;
1824 snd_hda_add_imux_item(imux, label,
1825 imux->num_items, NULL);
1839 /* get the ADC NID corresponding to the given index */
1840 static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1842 struct hda_gen_spec *spec = codec->spec;
1843 if (spec->dyn_adc_switch)
1844 adc_idx = spec->dyn_adc_idx[imux_idx];
1845 return spec->adc_nids[adc_idx];
1848 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
1851 static int mux_enum_info(struct snd_kcontrol *kcontrol,
1852 struct snd_ctl_elem_info *uinfo)
1854 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1855 struct hda_gen_spec *spec = codec->spec;
1856 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1859 static int mux_enum_get(struct snd_kcontrol *kcontrol,
1860 struct snd_ctl_elem_value *ucontrol)
1862 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1863 struct hda_gen_spec *spec = codec->spec;
1864 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1866 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1870 static int mux_enum_put(struct snd_kcontrol *kcontrol,
1871 struct snd_ctl_elem_value *ucontrol)
1873 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1874 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1875 return mux_select(codec, adc_idx,
1876 ucontrol->value.enumerated.item[0]);
1880 * capture volume and capture switch ctls
1883 static const struct snd_kcontrol_new cap_src_temp = {
1884 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1885 .name = "Input Source",
1886 .info = mux_enum_info,
1887 .get = mux_enum_get,
1888 .put = mux_enum_put,
1891 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
1892 struct snd_ctl_elem_value *ucontrol);
1894 static int cap_put_caller(struct snd_kcontrol *kcontrol,
1895 struct snd_ctl_elem_value *ucontrol,
1896 put_call_t func, int type)
1898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1899 struct hda_gen_spec *spec = codec->spec;
1900 const struct hda_input_mux *imux;
1901 struct nid_path *path;
1902 int i, adc_idx, err = 0;
1904 imux = &spec->input_mux;
1905 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1906 mutex_lock(&codec->control_mutex);
1907 codec->cached_write = 1;
1908 for (i = 0; i < imux->num_items; i++) {
1909 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
1910 get_adc_nid(codec, adc_idx, i));
1911 if (!path->ctls[type])
1913 kcontrol->private_value = path->ctls[type];
1914 err = func(kcontrol, ucontrol);
1919 codec->cached_write = 0;
1920 mutex_unlock(&codec->control_mutex);
1921 if (err >= 0 && spec->cap_sync_hook)
1922 spec->cap_sync_hook(codec);
1926 /* capture volume ctl callbacks */
1927 #define cap_vol_info snd_hda_mixer_amp_volume_info
1928 #define cap_vol_get snd_hda_mixer_amp_volume_get
1929 #define cap_vol_tlv snd_hda_mixer_amp_tlv
1931 static int cap_vol_put(struct snd_kcontrol *kcontrol,
1932 struct snd_ctl_elem_value *ucontrol)
1934 return cap_put_caller(kcontrol, ucontrol,
1935 snd_hda_mixer_amp_volume_put,
1939 static const struct snd_kcontrol_new cap_vol_temp = {
1940 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1941 .name = "Capture Volume",
1942 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1943 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1944 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
1945 .info = cap_vol_info,
1948 .tlv = { .c = cap_vol_tlv },
1951 /* capture switch ctl callbacks */
1952 #define cap_sw_info snd_ctl_boolean_stereo_info
1953 #define cap_sw_get snd_hda_mixer_amp_switch_get
1955 static int cap_sw_put(struct snd_kcontrol *kcontrol,
1956 struct snd_ctl_elem_value *ucontrol)
1958 return cap_put_caller(kcontrol, ucontrol,
1959 snd_hda_mixer_amp_switch_put,
1963 static const struct snd_kcontrol_new cap_sw_temp = {
1964 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1965 .name = "Capture Switch",
1966 .info = cap_sw_info,
1971 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
1976 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
1977 for (depth = 0; depth < 3; depth++) {
1978 if (depth >= path->depth)
1980 i = path->depth - depth - 1;
1981 nid = path->path[i];
1982 if (!path->ctls[NID_PATH_VOL_CTL]) {
1983 if (nid_has_volume(codec, nid, HDA_OUTPUT))
1984 path->ctls[NID_PATH_VOL_CTL] =
1985 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1986 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
1987 int idx = path->idx[i];
1988 if (!depth && codec->single_adc_amp)
1990 path->ctls[NID_PATH_VOL_CTL] =
1991 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
1994 if (!path->ctls[NID_PATH_MUTE_CTL]) {
1995 if (nid_has_mute(codec, nid, HDA_OUTPUT))
1996 path->ctls[NID_PATH_MUTE_CTL] =
1997 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1998 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
1999 int idx = path->idx[i];
2000 if (!depth && codec->single_adc_amp)
2002 path->ctls[NID_PATH_MUTE_CTL] =
2003 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2010 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2012 struct hda_gen_spec *spec = codec->spec;
2013 struct auto_pin_cfg *cfg = &spec->autocfg;
2017 if (!spec->inv_dmic_split)
2019 for (i = 0; i < cfg->num_inputs; i++) {
2020 if (cfg->inputs[i].pin != nid)
2022 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2024 val = snd_hda_codec_get_pincfg(codec, nid);
2025 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2030 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2031 int idx, bool is_switch, unsigned int ctl,
2034 struct hda_gen_spec *spec = codec->spec;
2036 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2037 const char *sfx = is_switch ? "Switch" : "Volume";
2038 unsigned int chs = inv_dmic ? 1 : 3;
2045 snprintf(tmpname, sizeof(tmpname),
2046 "%s Capture %s", label, sfx);
2048 snprintf(tmpname, sizeof(tmpname),
2050 err = add_control(spec, type, tmpname, idx,
2051 amp_val_replace_channels(ctl, chs));
2052 if (err < 0 || !inv_dmic)
2055 /* Make independent right kcontrol */
2057 snprintf(tmpname, sizeof(tmpname),
2058 "Inverted %s Capture %s", label, sfx);
2060 snprintf(tmpname, sizeof(tmpname),
2061 "Inverted Capture %s", sfx);
2062 return add_control(spec, type, tmpname, idx,
2063 amp_val_replace_channels(ctl, 2));
2066 /* create single (and simple) capture volume and switch controls */
2067 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2068 unsigned int vol_ctl, unsigned int sw_ctl,
2072 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2075 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2081 /* create bound capture volume and switch controls */
2082 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2083 unsigned int vol_ctl, unsigned int sw_ctl)
2085 struct hda_gen_spec *spec = codec->spec;
2086 struct snd_kcontrol_new *knew;
2089 knew = add_kctl(spec, NULL, &cap_vol_temp);
2093 knew->private_value = vol_ctl;
2094 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2097 knew = add_kctl(spec, NULL, &cap_sw_temp);
2101 knew->private_value = sw_ctl;
2102 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2107 /* return the vol ctl when used first in the imux list */
2108 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2110 struct hda_gen_spec *spec = codec->spec;
2111 struct nid_path *path;
2115 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2116 get_adc_nid(codec, 0, idx));
2119 ctl = path->ctls[type];
2122 for (i = 0; i < idx - 1; i++) {
2123 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2124 get_adc_nid(codec, 0, i));
2125 if (path && path->ctls[type] == ctl)
2131 /* create individual capture volume and switch controls per input */
2132 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2134 struct hda_gen_spec *spec = codec->spec;
2135 struct hda_input_mux *imux = &spec->input_mux;
2136 int i, err, type, type_idx = 0;
2137 const char *prev_label = NULL;
2139 for (i = 0; i < imux->num_items; i++) {
2142 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2143 if (prev_label && !strcmp(label, prev_label))
2148 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2150 for (type = 0; type < 2; type++) {
2151 err = add_single_cap_ctl(codec, label, type_idx, type,
2152 get_first_cap_ctl(codec, i, type),
2161 static int create_capture_mixers(struct hda_codec *codec)
2163 struct hda_gen_spec *spec = codec->spec;
2164 struct hda_input_mux *imux = &spec->input_mux;
2165 int i, n, nums, err;
2167 if (spec->dyn_adc_switch)
2170 nums = spec->num_adc_nids;
2172 if (!spec->auto_mic && imux->num_items > 1) {
2173 struct snd_kcontrol_new *knew;
2174 knew = add_kctl(spec, NULL, &cap_src_temp);
2180 for (n = 0; n < nums; n++) {
2182 bool inv_dmic = false;
2186 for (i = 0; i < imux->num_items; i++) {
2187 struct nid_path *path;
2188 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2189 get_adc_nid(codec, n, i));
2192 parse_capvol_in_path(codec, path);
2194 vol = path->ctls[NID_PATH_VOL_CTL];
2195 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2198 sw = path->ctls[NID_PATH_MUTE_CTL];
2199 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2201 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2206 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2208 else if (!spec->multi_cap_vol)
2209 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2211 err = create_multi_cap_vol_ctl(codec);
2220 * add mic boosts if needed
2222 static int parse_mic_boost(struct hda_codec *codec)
2224 struct hda_gen_spec *spec = codec->spec;
2225 struct auto_pin_cfg *cfg = &spec->autocfg;
2229 const char *prev_label = NULL;
2231 for (i = 0; i < cfg->num_inputs; i++) {
2232 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2234 nid = cfg->inputs[i].pin;
2235 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2237 char boost_label[32];
2238 struct nid_path *path;
2241 label = hda_get_autocfg_input_label(codec, cfg, i);
2242 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2243 label = "Headphone Mic";
2244 if (prev_label && !strcmp(label, prev_label))
2250 snprintf(boost_label, sizeof(boost_label),
2251 "%s Boost Volume", label);
2252 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2253 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2254 boost_label, type_idx, val);
2258 path = snd_hda_get_nid_path(codec, nid, 0);
2260 path->ctls[NID_PATH_BOOST_CTL] = val;
2267 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2269 static void parse_digital(struct hda_codec *codec)
2271 struct hda_gen_spec *spec = codec->spec;
2275 /* support multiple SPDIFs; the secondary is set up as a slave */
2277 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2278 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2279 dig_nid = look_for_dac(codec, pin, true);
2282 if (!snd_hda_add_new_path(codec, dig_nid, pin, 2))
2285 spec->multiout.dig_out_nid = dig_nid;
2286 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2288 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2289 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2291 spec->slave_dig_outs[nums - 1] = dig_nid;
2296 if (spec->autocfg.dig_in_pin) {
2297 dig_nid = codec->start_nid;
2298 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2299 struct nid_path *path;
2300 unsigned int wcaps = get_wcaps(codec, dig_nid);
2301 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2303 if (!(wcaps & AC_WCAP_DIGITAL))
2305 path = snd_hda_add_new_path(codec,
2306 spec->autocfg.dig_in_pin,
2309 path->active = true;
2310 spec->dig_in_nid = dig_nid;
2319 * input MUX handling
2322 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2324 /* select the given imux item; either unmute exclusively or select the route */
2325 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2328 struct hda_gen_spec *spec = codec->spec;
2329 const struct hda_input_mux *imux;
2330 struct nid_path *path;
2332 imux = &spec->input_mux;
2333 if (!imux->num_items)
2336 if (idx >= imux->num_items)
2337 idx = imux->num_items - 1;
2338 if (spec->cur_mux[adc_idx] == idx)
2341 path = snd_hda_get_nid_path(codec,
2342 spec->imux_pins[spec->cur_mux[adc_idx]],
2343 spec->adc_nids[adc_idx]);
2347 snd_hda_activate_path(codec, path, false, false);
2349 spec->cur_mux[adc_idx] = idx;
2351 if (spec->shared_mic_hp)
2352 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2354 if (spec->dyn_adc_switch)
2355 dyn_adc_pcm_resetup(codec, idx);
2357 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2358 get_adc_nid(codec, adc_idx, idx));
2363 snd_hda_activate_path(codec, path, true, false);
2364 if (spec->cap_sync_hook)
2365 spec->cap_sync_hook(codec);
2371 * Jack detections for HP auto-mute and mic-switch
2374 /* check each pin in the given array; returns true if any of them is plugged */
2375 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2379 for (i = 0; i < num_pins; i++) {
2380 hda_nid_t nid = pins[i];
2383 present |= snd_hda_jack_detect(codec, nid);
2388 /* standard HP/line-out auto-mute helper */
2389 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2390 bool mute, bool hp_out)
2392 struct hda_gen_spec *spec = codec->spec;
2393 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2396 for (i = 0; i < num_pins; i++) {
2397 hda_nid_t nid = pins[i];
2401 /* don't reset VREF value in case it's controlling
2402 * the amp (see alc861_fixup_asus_amp_vref_0f())
2404 if (spec->keep_vref_in_automute) {
2405 val = snd_hda_codec_read(codec, nid, 0,
2406 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2411 snd_hda_set_pin_ctl(codec, nid, val);
2415 /* Toggle outputs muting */
2416 static void update_outputs(struct hda_codec *codec)
2418 struct hda_gen_spec *spec = codec->spec;
2421 /* Control HP pins/amps depending on master_mute state;
2422 * in general, HP pins/amps control should be enabled in all cases,
2423 * but currently set only for master_mute, just to be safe
2425 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2426 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2427 spec->autocfg.hp_pins, spec->master_mute, true);
2429 if (!spec->automute_speaker)
2432 on = spec->hp_jack_present | spec->line_jack_present;
2433 on |= spec->master_mute;
2434 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2435 spec->autocfg.speaker_pins, on, false);
2437 /* toggle line-out mutes if needed, too */
2438 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2439 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2440 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2442 if (!spec->automute_lo)
2445 on = spec->hp_jack_present;
2446 on |= spec->master_mute;
2447 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2448 spec->autocfg.line_out_pins, on, false);
2451 static void call_update_outputs(struct hda_codec *codec)
2453 struct hda_gen_spec *spec = codec->spec;
2454 if (spec->automute_hook)
2455 spec->automute_hook(codec);
2457 update_outputs(codec);
2460 /* standard HP-automute helper */
2461 static void hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2463 struct hda_gen_spec *spec = codec->spec;
2465 spec->hp_jack_present =
2466 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2467 spec->autocfg.hp_pins);
2468 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2470 call_update_outputs(codec);
2473 /* standard line-out-automute helper */
2474 static void line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2476 struct hda_gen_spec *spec = codec->spec;
2478 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2480 /* check LO jack only when it's different from HP */
2481 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2484 spec->line_jack_present =
2485 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2486 spec->autocfg.line_out_pins);
2487 if (!spec->automute_speaker || !spec->detect_lo)
2489 call_update_outputs(codec);
2492 /* standard mic auto-switch helper */
2493 static void mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
2495 struct hda_gen_spec *spec = codec->spec;
2498 if (!spec->auto_mic)
2501 for (i = spec->am_num_entries - 1; i > 0; i--) {
2502 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2503 mux_select(codec, 0, spec->am_entry[i].idx);
2507 mux_select(codec, 0, spec->am_entry[0].idx);
2511 * Auto-Mute mode mixer enum support
2513 static int automute_mode_info(struct snd_kcontrol *kcontrol,
2514 struct snd_ctl_elem_info *uinfo)
2516 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2517 struct hda_gen_spec *spec = codec->spec;
2518 static const char * const texts3[] = {
2519 "Disabled", "Speaker Only", "Line Out+Speaker"
2522 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2523 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2524 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2527 static int automute_mode_get(struct snd_kcontrol *kcontrol,
2528 struct snd_ctl_elem_value *ucontrol)
2530 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2531 struct hda_gen_spec *spec = codec->spec;
2532 unsigned int val = 0;
2533 if (spec->automute_speaker)
2535 if (spec->automute_lo)
2538 ucontrol->value.enumerated.item[0] = val;
2542 static int automute_mode_put(struct snd_kcontrol *kcontrol,
2543 struct snd_ctl_elem_value *ucontrol)
2545 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2546 struct hda_gen_spec *spec = codec->spec;
2548 switch (ucontrol->value.enumerated.item[0]) {
2550 if (!spec->automute_speaker && !spec->automute_lo)
2552 spec->automute_speaker = 0;
2553 spec->automute_lo = 0;
2556 if (spec->automute_speaker_possible) {
2557 if (!spec->automute_lo && spec->automute_speaker)
2559 spec->automute_speaker = 1;
2560 spec->automute_lo = 0;
2561 } else if (spec->automute_lo_possible) {
2562 if (spec->automute_lo)
2564 spec->automute_lo = 1;
2569 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2571 if (spec->automute_speaker && spec->automute_lo)
2573 spec->automute_speaker = 1;
2574 spec->automute_lo = 1;
2579 call_update_outputs(codec);
2583 static const struct snd_kcontrol_new automute_mode_enum = {
2584 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2585 .name = "Auto-Mute Mode",
2586 .info = automute_mode_info,
2587 .get = automute_mode_get,
2588 .put = automute_mode_put,
2591 static int add_automute_mode_enum(struct hda_codec *codec)
2593 struct hda_gen_spec *spec = codec->spec;
2595 if (!add_kctl(spec, NULL, &automute_mode_enum))
2601 * Check the availability of HP/line-out auto-mute;
2602 * Set up appropriately if really supported
2604 static int check_auto_mute_availability(struct hda_codec *codec)
2606 struct hda_gen_spec *spec = codec->spec;
2607 struct auto_pin_cfg *cfg = &spec->autocfg;
2611 if (cfg->hp_pins[0])
2613 if (cfg->line_out_pins[0])
2615 if (cfg->speaker_pins[0])
2617 if (present < 2) /* need two different output types */
2620 if (!cfg->speaker_pins[0] &&
2621 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2622 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2623 sizeof(cfg->speaker_pins));
2624 cfg->speaker_outs = cfg->line_outs;
2627 if (!cfg->hp_pins[0] &&
2628 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2629 memcpy(cfg->hp_pins, cfg->line_out_pins,
2630 sizeof(cfg->hp_pins));
2631 cfg->hp_outs = cfg->line_outs;
2634 for (i = 0; i < cfg->hp_outs; i++) {
2635 hda_nid_t nid = cfg->hp_pins[i];
2636 if (!is_jack_detectable(codec, nid))
2638 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2640 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
2642 spec->detect_hp = 1;
2645 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2646 if (cfg->speaker_outs)
2647 for (i = 0; i < cfg->line_outs; i++) {
2648 hda_nid_t nid = cfg->line_out_pins[i];
2649 if (!is_jack_detectable(codec, nid))
2651 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2652 snd_hda_jack_detect_enable_callback(codec, nid,
2653 HDA_GEN_FRONT_EVENT,
2655 spec->detect_lo = 1;
2657 spec->automute_lo_possible = spec->detect_hp;
2660 spec->automute_speaker_possible = cfg->speaker_outs &&
2661 (spec->detect_hp || spec->detect_lo);
2663 spec->automute_lo = spec->automute_lo_possible;
2664 spec->automute_speaker = spec->automute_speaker_possible;
2666 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2667 /* create a control for automute mode */
2668 err = add_automute_mode_enum(codec);
2675 /* return the position of NID in the list, or -1 if not found */
2676 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2679 for (i = 0; i < nums; i++)
2685 /* check whether all auto-mic pins are valid; setup indices if OK */
2686 static bool auto_mic_check_imux(struct hda_codec *codec)
2688 struct hda_gen_spec *spec = codec->spec;
2689 const struct hda_input_mux *imux;
2692 imux = &spec->input_mux;
2693 for (i = 0; i < spec->am_num_entries; i++) {
2694 spec->am_entry[i].idx =
2695 find_idx_in_nid_list(spec->am_entry[i].pin,
2696 spec->imux_pins, imux->num_items);
2697 if (spec->am_entry[i].idx < 0)
2698 return false; /* no corresponding imux */
2701 /* we don't need the jack detection for the first pin */
2702 for (i = 1; i < spec->am_num_entries; i++)
2703 snd_hda_jack_detect_enable_callback(codec,
2704 spec->am_entry[i].pin,
2710 static int compare_attr(const void *ap, const void *bp)
2712 const struct automic_entry *a = ap;
2713 const struct automic_entry *b = bp;
2714 return (int)(a->attr - b->attr);
2718 * Check the availability of auto-mic switch;
2719 * Set up if really supported
2721 static int check_auto_mic_availability(struct hda_codec *codec)
2723 struct hda_gen_spec *spec = codec->spec;
2724 struct auto_pin_cfg *cfg = &spec->autocfg;
2730 for (i = 0; i < cfg->num_inputs; i++) {
2731 hda_nid_t nid = cfg->inputs[i].pin;
2733 attr = snd_hda_codec_get_pincfg(codec, nid);
2734 attr = snd_hda_get_input_pin_attr(attr);
2735 if (types & (1 << attr))
2736 return 0; /* already occupied */
2738 case INPUT_PIN_ATTR_INT:
2739 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2740 return 0; /* invalid type */
2742 case INPUT_PIN_ATTR_UNUSED:
2743 return 0; /* invalid entry */
2745 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2746 return 0; /* invalid type */
2747 if (!spec->line_in_auto_switch &&
2748 cfg->inputs[i].type != AUTO_PIN_MIC)
2749 return 0; /* only mic is allowed */
2750 if (!is_jack_detectable(codec, nid))
2751 return 0; /* no unsol support */
2754 if (num_pins >= MAX_AUTO_MIC_PINS)
2756 types |= (1 << attr);
2757 spec->am_entry[num_pins].pin = nid;
2758 spec->am_entry[num_pins].attr = attr;
2765 spec->am_num_entries = num_pins;
2766 /* sort the am_entry in the order of attr so that the pin with a
2767 * higher attr will be selected when the jack is plugged.
2769 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2770 compare_attr, NULL);
2772 if (!auto_mic_check_imux(codec))
2776 spec->num_adc_nids = 1;
2777 spec->cur_mux[0] = spec->am_entry[0].idx;
2778 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2779 spec->am_entry[0].pin,
2780 spec->am_entry[1].pin,
2781 spec->am_entry[2].pin);
2787 /* parse the BIOS configuration and set up the hda_gen_spec */
2788 /* return 1 if successful, 0 if the proper config is not found,
2789 * or a negative error code
2791 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
2792 const hda_nid_t *ignore_nids)
2794 struct hda_gen_spec *spec = codec->spec;
2795 struct auto_pin_cfg *cfg = &spec->autocfg;
2798 err = snd_hda_parse_pin_defcfg(codec, cfg, ignore_nids,
2802 if (!cfg->line_outs) {
2803 if (cfg->dig_outs || cfg->dig_in_pin) {
2804 spec->multiout.max_channels = 2;
2805 spec->no_analog = 1;
2808 return 0; /* can't find valid BIOS pin config */
2811 if (!spec->no_primary_hp &&
2812 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2813 cfg->line_outs <= cfg->hp_outs) {
2814 /* use HP as primary out */
2815 cfg->speaker_outs = cfg->line_outs;
2816 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2817 sizeof(cfg->speaker_pins));
2818 cfg->line_outs = cfg->hp_outs;
2819 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2821 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2822 cfg->line_out_type = AUTO_PIN_HP_OUT;
2825 err = parse_output_paths(codec);
2828 err = create_multi_channel_mode(codec);
2831 err = create_multi_out_ctls(codec, cfg);
2834 err = create_hp_out_ctls(codec);
2837 err = create_speaker_out_ctls(codec);
2840 err = create_shared_input(codec);
2843 err = create_input_ctls(codec);
2847 /* check the multiple speaker pins */
2848 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2849 spec->const_channel_count = cfg->line_outs * 2;
2851 spec->const_channel_count = cfg->speaker_outs * 2;
2853 if (spec->multi_ios > 0)
2854 spec->multiout.max_channels = max(spec->ext_channel_count,
2855 spec->const_channel_count);
2857 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2859 err = check_auto_mute_availability(codec);
2863 err = check_dyn_adc_switch(codec);
2867 if (!spec->shared_mic_hp) {
2868 err = check_auto_mic_availability(codec);
2873 err = create_capture_mixers(codec);
2877 err = parse_mic_boost(codec);
2882 parse_digital(codec);
2886 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
2890 * Build control elements
2893 /* slave controls for virtual master */
2894 static const char * const slave_pfxs[] = {
2895 "Front", "Surround", "Center", "LFE", "Side",
2896 "Headphone", "Speaker", "Mono", "Line Out",
2897 "CLFE", "Bass Speaker", "PCM",
2901 int snd_hda_gen_build_controls(struct hda_codec *codec)
2903 struct hda_gen_spec *spec = codec->spec;
2906 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
2910 if (spec->multiout.dig_out_nid) {
2911 err = snd_hda_create_dig_out_ctls(codec,
2912 spec->multiout.dig_out_nid,
2913 spec->multiout.dig_out_nid,
2914 spec->pcm_rec[1].pcm_type);
2917 if (!spec->no_analog) {
2918 err = snd_hda_create_spdif_share_sw(codec,
2922 spec->multiout.share_spdif = 1;
2925 if (spec->dig_in_nid) {
2926 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
2931 /* if we have no master control, let's create it */
2932 if (!spec->no_analog &&
2933 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
2934 unsigned int vmaster_tlv[4];
2935 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2936 HDA_OUTPUT, vmaster_tlv);
2937 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
2938 vmaster_tlv, slave_pfxs,
2943 if (!spec->no_analog &&
2944 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
2945 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
2948 true, &spec->vmaster_mute.sw_kctl);
2951 if (spec->vmaster_mute.hook)
2952 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute, true);
2955 free_kctls(spec); /* no longer needed */
2957 if (spec->shared_mic_hp) {
2959 int nid = spec->autocfg.inputs[1].pin;
2960 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
2963 err = snd_hda_jack_detect_enable(codec, nid, 0);
2968 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
2974 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
2982 * Analog playback callbacks
2984 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
2985 struct hda_codec *codec,
2986 struct snd_pcm_substream *substream)
2988 struct hda_gen_spec *spec = codec->spec;
2989 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
2993 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2994 struct hda_codec *codec,
2995 unsigned int stream_tag,
2996 unsigned int format,
2997 struct snd_pcm_substream *substream)
2999 struct hda_gen_spec *spec = codec->spec;
3000 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3001 stream_tag, format, substream);
3004 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3005 struct hda_codec *codec,
3006 struct snd_pcm_substream *substream)
3008 struct hda_gen_spec *spec = codec->spec;
3009 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3015 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3016 struct hda_codec *codec,
3017 struct snd_pcm_substream *substream)
3019 struct hda_gen_spec *spec = codec->spec;
3020 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3023 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3024 struct hda_codec *codec,
3025 unsigned int stream_tag,
3026 unsigned int format,
3027 struct snd_pcm_substream *substream)
3029 struct hda_gen_spec *spec = codec->spec;
3030 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3031 stream_tag, format, substream);
3034 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3035 struct hda_codec *codec,
3036 struct snd_pcm_substream *substream)
3038 struct hda_gen_spec *spec = codec->spec;
3039 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3042 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3043 struct hda_codec *codec,
3044 struct snd_pcm_substream *substream)
3046 struct hda_gen_spec *spec = codec->spec;
3047 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3053 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3054 struct hda_codec *codec,
3055 unsigned int stream_tag,
3056 unsigned int format,
3057 struct snd_pcm_substream *substream)
3059 struct hda_gen_spec *spec = codec->spec;
3061 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
3062 stream_tag, 0, format);
3066 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3067 struct hda_codec *codec,
3068 struct snd_pcm_substream *substream)
3070 struct hda_gen_spec *spec = codec->spec;
3072 snd_hda_codec_cleanup_stream(codec,
3073 spec->adc_nids[substream->number + 1]);
3079 static const struct hda_pcm_stream pcm_analog_playback = {
3083 /* NID is set in build_pcms */
3085 .open = playback_pcm_open,
3086 .prepare = playback_pcm_prepare,
3087 .cleanup = playback_pcm_cleanup
3091 static const struct hda_pcm_stream pcm_analog_capture = {
3095 /* NID is set in build_pcms */
3098 static const struct hda_pcm_stream pcm_analog_alt_playback = {
3102 /* NID is set in build_pcms */
3105 static const struct hda_pcm_stream pcm_analog_alt_capture = {
3106 .substreams = 2, /* can be overridden */
3109 /* NID is set in build_pcms */
3111 .prepare = alt_capture_pcm_prepare,
3112 .cleanup = alt_capture_pcm_cleanup
3116 static const struct hda_pcm_stream pcm_digital_playback = {
3120 /* NID is set in build_pcms */
3122 .open = dig_playback_pcm_open,
3123 .close = dig_playback_pcm_close,
3124 .prepare = dig_playback_pcm_prepare,
3125 .cleanup = dig_playback_pcm_cleanup
3129 static const struct hda_pcm_stream pcm_digital_capture = {
3133 /* NID is set in build_pcms */
3136 /* Used by build_pcms to flag that a PCM has no playback stream */
3137 static const struct hda_pcm_stream pcm_null_stream = {
3144 * dynamic changing ADC PCM streams
3146 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3148 struct hda_gen_spec *spec = codec->spec;
3149 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3151 if (spec->cur_adc && spec->cur_adc != new_adc) {
3152 /* stream is running, let's swap the current ADC */
3153 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3154 spec->cur_adc = new_adc;
3155 snd_hda_codec_setup_stream(codec, new_adc,
3156 spec->cur_adc_stream_tag, 0,
3157 spec->cur_adc_format);
3163 /* analog capture with dynamic dual-adc changes */
3164 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3165 struct hda_codec *codec,
3166 unsigned int stream_tag,
3167 unsigned int format,
3168 struct snd_pcm_substream *substream)
3170 struct hda_gen_spec *spec = codec->spec;
3171 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3172 spec->cur_adc_stream_tag = stream_tag;
3173 spec->cur_adc_format = format;
3174 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3178 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3179 struct hda_codec *codec,
3180 struct snd_pcm_substream *substream)
3182 struct hda_gen_spec *spec = codec->spec;
3183 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3188 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3192 .nid = 0, /* fill later */
3194 .prepare = dyn_adc_capture_pcm_prepare,
3195 .cleanup = dyn_adc_capture_pcm_cleanup
3199 /* build PCM streams based on the parsed results */
3200 int snd_hda_gen_build_pcms(struct hda_codec *codec)
3202 struct hda_gen_spec *spec = codec->spec;
3203 struct hda_pcm *info = spec->pcm_rec;
3204 const struct hda_pcm_stream *p;
3205 bool have_multi_adcs;
3208 codec->num_pcms = 1;
3209 codec->pcm_info = info;
3211 if (spec->no_analog)
3214 snprintf(spec->stream_name_analog, sizeof(spec->stream_name_analog),
3215 "%s Analog", codec->chip_name);
3216 info->name = spec->stream_name_analog;
3218 if (spec->multiout.num_dacs > 0) {
3219 p = spec->stream_analog_playback;
3221 p = &pcm_analog_playback;
3222 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3223 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3224 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3225 spec->multiout.max_channels;
3226 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3227 spec->autocfg.line_outs == 2)
3228 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3231 if (spec->num_adc_nids) {
3232 p = spec->stream_analog_capture;
3234 if (spec->dyn_adc_switch)
3235 p = &dyn_adc_pcm_analog_capture;
3237 p = &pcm_analog_capture;
3239 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3240 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3243 if (spec->channel_mode) {
3244 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = 0;
3245 for (i = 0; i < spec->num_channel_mode; i++) {
3246 if (spec->channel_mode[i].channels > info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max) {
3247 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->channel_mode[i].channels;
3253 /* SPDIF for stream index #1 */
3254 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
3255 snprintf(spec->stream_name_digital,
3256 sizeof(spec->stream_name_digital),
3257 "%s Digital", codec->chip_name);
3258 codec->num_pcms = 2;
3259 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3260 info = spec->pcm_rec + 1;
3261 info->name = spec->stream_name_digital;
3262 if (spec->dig_out_type)
3263 info->pcm_type = spec->dig_out_type;
3265 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3266 if (spec->multiout.dig_out_nid) {
3267 p = spec->stream_digital_playback;
3269 p = &pcm_digital_playback;
3270 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3271 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3273 if (spec->dig_in_nid) {
3274 p = spec->stream_digital_capture;
3276 p = &pcm_digital_capture;
3277 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3278 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3282 if (spec->no_analog)
3285 /* If the use of more than one ADC is requested for the current
3286 * model, configure a second analog capture-only PCM.
3288 have_multi_adcs = (spec->num_adc_nids > 1) &&
3289 !spec->dyn_adc_switch && !spec->auto_mic;
3290 /* Additional Analaog capture for index #2 */
3291 if (spec->alt_dac_nid || have_multi_adcs) {
3292 codec->num_pcms = 3;
3293 info = spec->pcm_rec + 2;
3294 info->name = spec->stream_name_analog;
3295 if (spec->alt_dac_nid) {
3296 p = spec->stream_analog_alt_playback;
3298 p = &pcm_analog_alt_playback;
3299 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3300 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3303 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3305 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3307 if (have_multi_adcs) {
3308 p = spec->stream_analog_alt_capture;
3310 p = &pcm_analog_alt_capture;
3311 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3312 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3314 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3315 spec->num_adc_nids - 1;
3317 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3319 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3325 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3329 * Standard auto-parser initializations
3332 /* configure the path from the given dac to the pin as the proper output */
3333 static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3334 int pin_type, hda_nid_t dac)
3336 struct hda_gen_spec *spec = codec->spec;
3337 struct nid_path *path;
3339 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3340 path = snd_hda_get_nid_path(codec, dac, pin);
3345 snd_hda_activate_path(codec, path, true, true);
3347 if (!spec->own_eapd_ctl &&
3348 (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
3349 snd_hda_codec_update_cache(codec, pin, 0,
3350 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
3353 /* initialize primary output paths */
3354 static void init_multi_out(struct hda_codec *codec)
3356 struct hda_gen_spec *spec = codec->spec;
3360 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3365 for (i = 0; i <= HDA_SIDE; i++) {
3366 hda_nid_t nid = spec->autocfg.line_out_pins[i];
3368 set_output_and_unmute(codec, nid, pin_type,
3369 spec->multiout.dac_nids[i]);
3374 /* initialize hp and speaker paths */
3375 static void init_extra_out(struct hda_codec *codec)
3377 struct hda_gen_spec *spec = codec->spec;
3381 for (i = 0; i < spec->autocfg.hp_outs; i++) {
3382 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3384 pin = spec->autocfg.hp_pins[i];
3387 dac = spec->multiout.hp_out_nid[i];
3389 if (i > 0 && spec->multiout.hp_out_nid[0])
3390 dac = spec->multiout.hp_out_nid[0];
3392 dac = spec->multiout.dac_nids[0];
3394 set_output_and_unmute(codec, pin, PIN_HP, dac);
3396 for (i = 0; i < spec->autocfg.speaker_outs; i++) {
3397 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3399 pin = spec->autocfg.speaker_pins[i];
3402 dac = spec->multiout.extra_out_nid[i];
3404 if (i > 0 && spec->multiout.extra_out_nid[0])
3405 dac = spec->multiout.extra_out_nid[0];
3407 dac = spec->multiout.dac_nids[0];
3409 set_output_and_unmute(codec, pin, PIN_OUT, dac);
3413 /* initialize multi-io paths */
3414 static void init_multi_io(struct hda_codec *codec)
3416 struct hda_gen_spec *spec = codec->spec;
3419 for (i = 0; i < spec->multi_ios; i++) {
3420 hda_nid_t pin = spec->multi_io[i].pin;
3421 struct nid_path *path;
3422 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3425 if (!spec->multi_io[i].ctl_in)
3426 spec->multi_io[i].ctl_in =
3427 snd_hda_codec_update_cache(codec, pin, 0,
3428 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3429 snd_hda_activate_path(codec, path, path->active, true);
3433 /* set up the input pin config, depending on the given auto-pin type */
3434 static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3437 unsigned int val = PIN_IN;
3438 if (auto_pin_type == AUTO_PIN_MIC)
3439 val |= snd_hda_get_default_vref(codec, nid);
3440 snd_hda_set_pin_ctl(codec, nid, val);
3443 /* set up input pins and loopback paths */
3444 static void init_analog_input(struct hda_codec *codec)
3446 struct hda_gen_spec *spec = codec->spec;
3447 struct auto_pin_cfg *cfg = &spec->autocfg;
3450 for (i = 0; i < cfg->num_inputs; i++) {
3451 hda_nid_t nid = cfg->inputs[i].pin;
3452 if (is_input_pin(codec, nid))
3453 set_input_pin(codec, nid, cfg->inputs[i].type);
3455 /* init loopback inputs */
3456 if (spec->mixer_nid) {
3457 struct nid_path *path;
3458 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3460 snd_hda_activate_path(codec, path,
3461 path->active, false);
3466 /* initialize ADC paths */
3467 static void init_input_src(struct hda_codec *codec)
3469 struct hda_gen_spec *spec = codec->spec;
3470 struct hda_input_mux *imux = &spec->input_mux;
3471 struct nid_path *path;
3474 if (spec->dyn_adc_switch)
3477 nums = spec->num_adc_nids;
3479 for (c = 0; c < nums; c++) {
3480 for (i = 0; i < imux->num_items; i++) {
3481 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3482 get_adc_nid(codec, c, i));
3484 bool active = path->active;
3485 if (i == spec->cur_mux[c])
3487 snd_hda_activate_path(codec, path, active, false);
3492 if (spec->shared_mic_hp)
3493 update_shared_mic_hp(codec, spec->cur_mux[0]);
3495 if (spec->cap_sync_hook)
3496 spec->cap_sync_hook(codec);
3499 /* set right pin controls for digital I/O */
3500 static void init_digital(struct hda_codec *codec)
3502 struct hda_gen_spec *spec = codec->spec;
3506 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3507 pin = spec->autocfg.dig_out_pins[i];
3510 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3512 pin = spec->autocfg.dig_in_pin;
3514 snd_hda_set_pin_ctl(codec, pin, PIN_IN);
3517 int snd_hda_gen_init(struct hda_codec *codec)
3519 struct hda_gen_spec *spec = codec->spec;
3521 if (spec->init_hook)
3522 spec->init_hook(codec);
3524 snd_hda_apply_verbs(codec);
3526 init_multi_out(codec);
3527 init_extra_out(codec);
3528 init_multi_io(codec);
3529 init_analog_input(codec);
3530 init_input_src(codec);
3531 init_digital(codec);
3533 /* call init functions of standard auto-mute helpers */
3534 hp_automute(codec, NULL);
3535 line_automute(codec, NULL);
3536 mic_autoswitch(codec, NULL);
3538 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3539 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3541 hda_call_check_power_status(codec, 0x01);
3544 EXPORT_SYMBOL(snd_hda_gen_init);
3548 * the generic codec support
3552 static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3554 struct hda_gen_spec *spec = codec->spec;
3555 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3559 static void generic_free(struct hda_codec *codec)
3561 snd_hda_gen_spec_free(codec->spec);
3566 static const struct hda_codec_ops generic_patch_ops = {
3567 .build_controls = snd_hda_gen_build_controls,
3568 .build_pcms = snd_hda_gen_build_pcms,
3569 .init = snd_hda_gen_init,
3570 .free = generic_free,
3571 .unsol_event = snd_hda_jack_unsol_event,
3573 .check_power_status = generic_check_power_status,
3577 int snd_hda_parse_generic_codec(struct hda_codec *codec)
3579 struct hda_gen_spec *spec;
3582 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3585 snd_hda_gen_spec_init(spec);
3588 err = snd_hda_gen_parse_auto_config(codec, NULL);
3592 codec->patch_ops = generic_patch_ops;
3596 generic_free(codec);
3599 EXPORT_SYMBOL(snd_hda_parse_generic_codec);