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 struct snd_kcontrol_new *
47 snd_hda_gen_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);
62 EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
64 static void free_kctls(struct hda_gen_spec *spec)
66 if (spec->kctls.list) {
67 struct snd_kcontrol_new *kctl = spec->kctls.list;
69 for (i = 0; i < spec->kctls.used; i++)
72 snd_array_free(&spec->kctls);
75 static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
77 struct hda_ctl_ops *ops)
79 struct hda_gen_spec *spec = codec->spec;
80 struct hda_bind_ctls **ctlp, *ctl;
81 ctlp = snd_array_new(&spec->bind_ctls);
84 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
91 static void free_bind_ctls(struct hda_gen_spec *spec)
93 if (spec->bind_ctls.list) {
94 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
96 for (i = 0; i < spec->bind_ctls.used; i++)
99 snd_array_free(&spec->bind_ctls);
102 void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
107 free_bind_ctls(spec);
108 snd_array_free(&spec->paths);
110 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
116 /* get the path between the given NIDs;
117 * passing 0 to either @pin or @dac behaves as a wildcard
119 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
120 hda_nid_t from_nid, hda_nid_t to_nid)
122 struct hda_gen_spec *spec = codec->spec;
125 for (i = 0; i < spec->paths.used; i++) {
126 struct nid_path *path = snd_array_elem(&spec->paths, i);
127 if (path->depth <= 0)
129 if ((!from_nid || path->path[0] == from_nid) &&
130 (!to_nid || path->path[path->depth - 1] == to_nid))
135 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
137 /* check whether the given DAC is already found in any existing paths */
138 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
140 struct hda_gen_spec *spec = codec->spec;
143 for (i = 0; i < spec->paths.used; i++) {
144 struct nid_path *path = snd_array_elem(&spec->paths, i);
145 if (path->path[0] == nid)
151 /* check whether the given two widgets can be connected */
152 static bool is_reachable_path(struct hda_codec *codec,
153 hda_nid_t from_nid, hda_nid_t to_nid)
155 if (!from_nid || !to_nid)
157 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
160 /* nid, dir and idx */
161 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
163 /* check whether the given ctl is already assigned in any path elements */
164 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
166 struct hda_gen_spec *spec = codec->spec;
169 val &= AMP_VAL_COMPARE_MASK;
170 for (i = 0; i < spec->paths.used; i++) {
171 struct nid_path *path = snd_array_elem(&spec->paths, i);
172 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
178 /* check whether a control with the given (nid, dir, idx) was assigned */
179 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
182 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
183 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
184 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
187 /* called recursively */
188 static bool __parse_nid_path(struct hda_codec *codec,
189 hda_nid_t from_nid, hda_nid_t to_nid,
190 int with_aa_mix, struct nid_path *path, int depth)
192 struct hda_gen_spec *spec = codec->spec;
196 if (to_nid == spec->mixer_nid) {
199 with_aa_mix = 2; /* mark aa-mix is included */
202 nums = snd_hda_get_connections(codec, to_nid, conn, ARRAY_SIZE(conn));
203 for (i = 0; i < nums; i++) {
204 if (conn[i] != from_nid) {
205 /* special case: when from_nid is 0,
206 * try to find an empty DAC
209 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
210 is_dac_already_used(codec, conn[i]))
213 /* aa-mix is requested but not included? */
214 if (!(spec->mixer_nid && with_aa_mix == 1))
217 if (depth >= MAX_NID_PATH_DEPTH)
219 for (i = 0; i < nums; i++) {
221 type = get_wcaps_type(get_wcaps(codec, conn[i]));
222 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
225 if (__parse_nid_path(codec, from_nid, conn[i],
226 with_aa_mix, path, depth + 1))
232 path->path[path->depth] = conn[i];
233 path->idx[path->depth + 1] = i;
234 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
235 path->multi[path->depth + 1] = 1;
240 /* parse the widget path from the given nid to the target nid;
241 * when @from_nid is 0, try to find an empty DAC;
242 * when @with_aa_mix is 0, paths with spec->mixer_nid are excluded.
243 * when @with_aa_mix is 1, paths without spec->mixer_nid are excluded.
244 * when @with_aa_mix is 2, no special handling about spec->mixer_nid.
246 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
247 hda_nid_t to_nid, int with_aa_mix,
248 struct nid_path *path)
250 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
251 path->path[path->depth] = to_nid;
254 snd_printdd("path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
255 path->depth, path->path[0], path->path[1],
256 path->path[2], path->path[3], path->path[4]);
262 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
265 * parse the path between the given NIDs and add to the path list.
266 * if no valid path is found, return NULL
269 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
270 hda_nid_t to_nid, int with_aa_mix)
272 struct hda_gen_spec *spec = codec->spec;
273 struct nid_path *path;
275 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
278 path = snd_array_new(&spec->paths);
281 memset(path, 0, sizeof(*path));
282 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
288 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
290 /* look for an empty DAC slot */
291 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
294 struct hda_gen_spec *spec = codec->spec;
298 for (i = 0; i < spec->num_all_dacs; i++) {
299 hda_nid_t nid = spec->all_dacs[i];
300 if (!nid || is_dac_already_used(codec, nid))
302 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
303 if (is_digital != cap_digital)
305 if (is_reachable_path(codec, nid, pin))
311 /* replace the channels in the composed amp value with the given number */
312 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
314 val &= ~(0x3U << 16);
319 /* check whether the widget has the given amp capability for the direction */
320 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
321 int dir, unsigned int bits)
325 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
326 if (query_amp_caps(codec, nid, dir) & bits)
331 #define nid_has_mute(codec, nid, dir) \
332 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
333 #define nid_has_volume(codec, nid, dir) \
334 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
336 /* look for a widget suitable for assigning a mute switch in the path */
337 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
338 struct nid_path *path)
342 for (i = path->depth - 1; i >= 0; i--) {
343 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
344 return path->path[i];
345 if (i != path->depth - 1 && i != 0 &&
346 nid_has_mute(codec, path->path[i], HDA_INPUT))
347 return path->path[i];
352 /* look for a widget suitable for assigning a volume ctl in the path */
353 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
354 struct nid_path *path)
358 for (i = path->depth - 1; i >= 0; i--) {
359 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
360 return path->path[i];
366 * path activation / deactivation
369 /* can have the amp-in capability? */
370 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
372 hda_nid_t nid = path->path[idx];
373 unsigned int caps = get_wcaps(codec, nid);
374 unsigned int type = get_wcaps_type(caps);
376 if (!(caps & AC_WCAP_IN_AMP))
378 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
383 /* can have the amp-out capability? */
384 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
386 hda_nid_t nid = path->path[idx];
387 unsigned int caps = get_wcaps(codec, nid);
388 unsigned int type = get_wcaps_type(caps);
390 if (!(caps & AC_WCAP_OUT_AMP))
392 if (type == AC_WID_PIN && !idx) /* only for output pins */
397 /* check whether the given (nid,dir,idx) is active */
398 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
399 unsigned int idx, unsigned int dir)
401 struct hda_gen_spec *spec = codec->spec;
404 for (n = 0; n < spec->paths.used; n++) {
405 struct nid_path *path = snd_array_elem(&spec->paths, n);
408 for (i = 0; i < path->depth; i++) {
409 if (path->path[i] == nid) {
410 if (dir == HDA_OUTPUT || path->idx[i] == idx)
419 /* get the default amp value for the target state */
420 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
421 int dir, bool enable)
424 unsigned int val = 0;
426 caps = query_amp_caps(codec, nid, dir);
427 if (caps & AC_AMPCAP_NUM_STEPS) {
430 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
432 if (caps & AC_AMPCAP_MUTE) {
439 /* initialize the amp value (only at the first time) */
440 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
442 int val = get_amp_val_to_activate(codec, nid, dir, false);
443 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
446 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
447 int idx, bool enable)
450 if (is_ctl_associated(codec, nid, dir, idx) ||
451 is_active_nid(codec, nid, dir, idx))
453 val = get_amp_val_to_activate(codec, nid, dir, enable);
454 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
457 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
460 hda_nid_t nid = path->path[i];
461 init_amp(codec, nid, HDA_OUTPUT, 0);
462 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
465 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
466 int i, bool enable, bool add_aamix)
468 struct hda_gen_spec *spec = codec->spec;
472 hda_nid_t nid = path->path[i];
474 nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
475 type = get_wcaps_type(get_wcaps(codec, nid));
476 if (type == AC_WID_PIN ||
477 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
483 for (n = 0; n < nums; n++)
484 init_amp(codec, nid, HDA_INPUT, n);
486 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
489 /* here is a little bit tricky in comparison with activate_amp_out();
490 * when aa-mixer is available, we need to enable the path as well
492 for (n = 0; n < nums; n++) {
493 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
495 activate_amp(codec, nid, HDA_INPUT, n, enable);
499 /* activate or deactivate the given path
500 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
502 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
503 bool enable, bool add_aamix)
508 path->active = false;
510 for (i = path->depth - 1; i >= 0; i--) {
511 if (enable && path->multi[i])
512 snd_hda_codec_write_cache(codec, path->path[i], 0,
513 AC_VERB_SET_CONNECT_SEL,
515 if (has_amp_in(codec, path, i))
516 activate_amp_in(codec, path, i, enable, add_aamix);
517 if (has_amp_out(codec, path, i))
518 activate_amp_out(codec, path, i, enable);
524 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
528 * Helper functions for creating mixer ctl elements
538 static const struct snd_kcontrol_new control_templates[] = {
539 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
540 HDA_CODEC_MUTE(NULL, 0, 0, 0),
541 HDA_BIND_MUTE(NULL, 0, 0, 0),
542 HDA_BIND_VOL(NULL, 0),
543 HDA_BIND_SW(NULL, 0),
546 /* add dynamic controls from template */
547 static int add_control(struct hda_gen_spec *spec, int type, const char *name,
548 int cidx, unsigned long val)
550 struct snd_kcontrol_new *knew;
552 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
556 if (get_amp_nid_(val))
557 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
558 knew->private_value = val;
562 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
563 const char *pfx, const char *dir,
564 const char *sfx, int cidx, unsigned long val)
567 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
568 return add_control(spec, type, name, cidx, val);
571 #define add_pb_vol_ctrl(spec, type, pfx, val) \
572 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
573 #define add_pb_sw_ctrl(spec, type, pfx, val) \
574 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
575 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
576 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
577 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
578 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
580 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
581 unsigned int chs, struct nid_path *path)
586 val = path->ctls[NID_PATH_VOL_CTL];
589 val = amp_val_replace_channels(val, chs);
590 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
593 /* return the channel bits suitable for the given path->ctls[] */
594 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
597 int chs = 1; /* mono (left only) */
599 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
600 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
601 chs = 3; /* stereo */
606 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
607 struct nid_path *path)
609 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
610 return add_vol_ctl(codec, pfx, cidx, chs, path);
613 /* create a mute-switch for the given mixer widget;
614 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
616 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
617 unsigned int chs, struct nid_path *path)
620 int type = HDA_CTL_WIDGET_MUTE;
624 val = path->ctls[NID_PATH_MUTE_CTL];
627 val = amp_val_replace_channels(val, chs);
628 if (get_amp_direction_(val) == HDA_INPUT) {
629 hda_nid_t nid = get_amp_nid_(val);
630 int nums = snd_hda_get_num_conns(codec, nid);
632 type = HDA_CTL_BIND_MUTE;
636 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
639 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
640 int cidx, struct nid_path *path)
642 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
643 return add_sw_ctl(codec, pfx, cidx, chs, path);
646 static const char * const channel_name[4] = {
647 "Front", "Surround", "CLFE", "Side"
650 /* give some appropriate ctl name prefix for the given line out channel */
651 static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
652 bool can_be_master, int *index)
654 struct auto_pin_cfg *cfg = &spec->autocfg;
657 if (cfg->line_outs == 1 && !spec->multi_ios &&
658 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
659 return spec->vmaster_mute.hook ? "PCM" : "Master";
661 /* if there is really a single DAC used in the whole output paths,
662 * use it master (or "PCM" if a vmaster hook is present)
664 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
665 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
666 return spec->vmaster_mute.hook ? "PCM" : "Master";
668 switch (cfg->line_out_type) {
669 case AUTO_PIN_SPEAKER_OUT:
670 if (cfg->line_outs == 1)
672 if (cfg->line_outs == 2)
673 return ch ? "Bass Speaker" : "Speaker";
675 case AUTO_PIN_HP_OUT:
676 /* for multi-io case, only the primary out */
677 if (ch && spec->multi_ios)
682 if (cfg->line_outs == 1 && !spec->multi_ios)
686 if (ch >= ARRAY_SIZE(channel_name)) {
691 return channel_name[ch];
698 /* badness definition */
700 /* No primary DAC is found for the main output */
701 BAD_NO_PRIMARY_DAC = 0x10000,
702 /* No DAC is found for the extra output */
704 /* No possible multi-ios */
705 BAD_MULTI_IO = 0x103,
706 /* No individual DAC for extra output */
707 BAD_NO_EXTRA_DAC = 0x102,
708 /* No individual DAC for extra surrounds */
709 BAD_NO_EXTRA_SURR_DAC = 0x101,
710 /* Primary DAC shared with main surrounds */
711 BAD_SHARED_SURROUND = 0x100,
712 /* Primary DAC shared with main CLFE */
713 BAD_SHARED_CLFE = 0x10,
714 /* Primary DAC shared with extra surrounds */
715 BAD_SHARED_EXTRA_SURROUND = 0x10,
716 /* Volume widget is shared */
717 BAD_SHARED_VOL = 0x10,
720 /* look for widgets in the path between the given NIDs appropriate for
721 * volume and mute controls, and assign the values to ctls[].
723 * When no appropriate widget is found in the path, the badness value
724 * is incremented depending on the situation. The function returns the
725 * total badness for both volume and mute controls.
727 static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
730 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
736 return BAD_SHARED_VOL * 2;
737 nid = look_for_out_vol_nid(codec, path);
739 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
740 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
741 badness += BAD_SHARED_VOL;
743 path->ctls[NID_PATH_VOL_CTL] = val;
745 badness += BAD_SHARED_VOL;
746 nid = look_for_out_mute_nid(codec, path);
748 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
749 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
750 nid_has_mute(codec, nid, HDA_OUTPUT))
751 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
753 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
754 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
755 badness += BAD_SHARED_VOL;
757 path->ctls[NID_PATH_MUTE_CTL] = val;
759 badness += BAD_SHARED_VOL;
763 struct badness_table {
764 int no_primary_dac; /* no primary DAC */
765 int no_dac; /* no secondary DACs */
766 int shared_primary; /* primary DAC is shared with main output */
767 int shared_surr; /* secondary DAC shared with main or primary */
768 int shared_clfe; /* third DAC shared with main or primary */
769 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
772 static struct badness_table main_out_badness = {
773 .no_primary_dac = BAD_NO_PRIMARY_DAC,
774 .no_dac = BAD_NO_DAC,
775 .shared_primary = BAD_NO_PRIMARY_DAC,
776 .shared_surr = BAD_SHARED_SURROUND,
777 .shared_clfe = BAD_SHARED_CLFE,
778 .shared_surr_main = BAD_SHARED_SURROUND,
781 static struct badness_table extra_out_badness = {
782 .no_primary_dac = BAD_NO_DAC,
783 .no_dac = BAD_NO_DAC,
784 .shared_primary = BAD_NO_EXTRA_DAC,
785 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
786 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
787 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
790 /* try to assign DACs to pins and return the resultant badness */
791 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
792 const hda_nid_t *pins, hda_nid_t *dacs,
793 const struct badness_table *bad)
795 struct hda_gen_spec *spec = codec->spec;
796 struct auto_pin_cfg *cfg = &spec->autocfg;
804 for (i = 0; i < num_outs; i++) {
805 hda_nid_t pin = pins[i];
807 dacs[i] = look_for_dac(codec, pin, false);
808 if (!dacs[i] && !i) {
809 for (j = 1; j < num_outs; j++) {
810 if (is_reachable_path(codec, dacs[j], pin)) {
819 if (is_reachable_path(codec, dacs[0], pin))
821 else if (cfg->line_outs > i &&
822 is_reachable_path(codec, spec->private_dac_nids[i], pin))
823 dac = spec->private_dac_nids[i];
826 badness += bad->shared_primary;
828 badness += bad->shared_surr;
830 badness += bad->shared_clfe;
831 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
832 dac = spec->private_dac_nids[0];
833 badness += bad->shared_surr_main;
835 badness += bad->no_primary_dac;
837 badness += bad->no_dac;
839 if (!snd_hda_add_new_path(codec, dac, pin, 0))
842 badness += assign_out_path_ctls(codec, pin, dac);
848 /* return NID if the given pin has only a single connection to a certain DAC */
849 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
851 struct hda_gen_spec *spec = codec->spec;
853 hda_nid_t nid_found = 0;
855 for (i = 0; i < spec->num_all_dacs; i++) {
856 hda_nid_t nid = spec->all_dacs[i];
857 if (!nid || is_dac_already_used(codec, nid))
859 if (is_reachable_path(codec, nid, pin)) {
868 /* check whether the given pin can be a multi-io pin */
869 static bool can_be_multiio_pin(struct hda_codec *codec,
870 unsigned int location, hda_nid_t nid)
872 unsigned int defcfg, caps;
874 defcfg = snd_hda_codec_get_pincfg(codec, nid);
875 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
877 if (location && get_defcfg_location(defcfg) != location)
879 caps = snd_hda_query_pin_caps(codec, nid);
880 if (!(caps & AC_PINCAP_OUT))
888 * When hardwired is set, try to fill ony hardwired pins, and returns
889 * zero if any pins are filled, non-zero if nothing found.
890 * When hardwired is off, try to fill possible input pins, and returns
893 static int fill_multi_ios(struct hda_codec *codec,
894 hda_nid_t reference_pin,
895 bool hardwired, int offset)
897 struct hda_gen_spec *spec = codec->spec;
898 struct auto_pin_cfg *cfg = &spec->autocfg;
899 int type, i, j, dacs, num_pins, old_pins;
900 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
901 unsigned int location = get_defcfg_location(defcfg);
904 old_pins = spec->multi_ios;
909 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
910 for (i = 0; i < cfg->num_inputs; i++) {
911 if (cfg->inputs[i].type != type)
913 if (can_be_multiio_pin(codec, location,
921 dacs = spec->multiout.num_dacs;
922 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
923 for (i = 0; i < cfg->num_inputs; i++) {
924 hda_nid_t nid = cfg->inputs[i].pin;
927 if (cfg->inputs[i].type != type)
929 if (!can_be_multiio_pin(codec, location, nid))
931 for (j = 0; j < spec->multi_ios; j++) {
932 if (nid == spec->multi_io[j].pin)
935 if (j < spec->multi_ios)
938 if (offset && offset + spec->multi_ios < dacs) {
939 dac = spec->private_dac_nids[offset + spec->multi_ios];
940 if (!is_reachable_path(codec, dac, nid))
944 dac = get_dac_if_single(codec, nid);
946 dac = look_for_dac(codec, nid, false);
951 if (!snd_hda_add_new_path(codec, dac, nid, 0)) {
955 spec->multi_io[spec->multi_ios].pin = nid;
956 spec->multi_io[spec->multi_ios].dac = dac;
958 if (spec->multi_ios >= 2)
964 badness = BAD_MULTI_IO;
965 if (old_pins == spec->multi_ios) {
967 return 1; /* nothing found */
969 return badness; /* no badness if nothing found */
971 if (!hardwired && spec->multi_ios < 2) {
972 /* cancel newly assigned paths */
973 spec->paths.used -= spec->multi_ios - old_pins;
974 spec->multi_ios = old_pins;
978 /* assign volume and mute controls */
979 for (i = old_pins; i < spec->multi_ios; i++)
980 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
981 spec->multi_io[i].dac);
986 /* map DACs for all pins in the list if they are single connections */
987 static bool map_singles(struct hda_codec *codec, int outs,
988 const hda_nid_t *pins, hda_nid_t *dacs)
992 for (i = 0; i < outs; i++) {
996 dac = get_dac_if_single(codec, pins[i]);
999 if (snd_hda_add_new_path(codec, dac, pins[i], 0)) {
1007 /* fill in the dac_nids table from the parsed pin configuration */
1008 static int fill_and_eval_dacs(struct hda_codec *codec,
1009 bool fill_hardwired,
1010 bool fill_mio_first)
1012 struct hda_gen_spec *spec = codec->spec;
1013 struct auto_pin_cfg *cfg = &spec->autocfg;
1014 int i, err, badness;
1016 /* set num_dacs once to full for look_for_dac() */
1017 spec->multiout.num_dacs = cfg->line_outs;
1018 spec->multiout.dac_nids = spec->private_dac_nids;
1019 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1020 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1021 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1022 spec->multi_ios = 0;
1023 snd_array_free(&spec->paths);
1026 /* fill hard-wired DACs first */
1027 if (fill_hardwired) {
1030 mapped = map_singles(codec, cfg->line_outs,
1032 spec->private_dac_nids);
1033 mapped |= map_singles(codec, cfg->hp_outs,
1035 spec->multiout.hp_out_nid);
1036 mapped |= map_singles(codec, cfg->speaker_outs,
1038 spec->multiout.extra_out_nid);
1039 if (fill_mio_first && cfg->line_outs == 1 &&
1040 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1041 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1048 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1049 spec->private_dac_nids,
1052 /* re-count num_dacs and squash invalid entries */
1053 spec->multiout.num_dacs = 0;
1054 for (i = 0; i < cfg->line_outs; i++) {
1055 if (spec->private_dac_nids[i])
1056 spec->multiout.num_dacs++;
1058 memmove(spec->private_dac_nids + i,
1059 spec->private_dac_nids + i + 1,
1060 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1061 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1065 if (fill_mio_first &&
1066 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1067 /* try to fill multi-io first */
1068 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1071 /* we don't count badness at this stage yet */
1074 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1075 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1076 spec->multiout.hp_out_nid,
1077 &extra_out_badness);
1082 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1083 err = try_assign_dacs(codec, cfg->speaker_outs,
1085 spec->multiout.extra_out_nid,
1086 &extra_out_badness);
1091 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1092 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1097 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1098 /* try multi-ios with HP + inputs */
1100 if (cfg->line_outs >= 3)
1102 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1108 if (spec->multi_ios == 2) {
1109 for (i = 0; i < 2; i++)
1110 spec->private_dac_nids[spec->multiout.num_dacs++] =
1111 spec->multi_io[i].dac;
1112 spec->ext_channel_count = 2;
1113 } else if (spec->multi_ios) {
1114 spec->multi_ios = 0;
1115 badness += BAD_MULTI_IO;
1121 #define DEBUG_BADNESS
1123 #ifdef DEBUG_BADNESS
1124 #define debug_badness snd_printdd
1126 #define debug_badness(...)
1129 static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1131 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1132 cfg->line_out_pins[0], cfg->line_out_pins[1],
1133 cfg->line_out_pins[2], cfg->line_out_pins[2],
1134 spec->multiout.dac_nids[0],
1135 spec->multiout.dac_nids[1],
1136 spec->multiout.dac_nids[2],
1137 spec->multiout.dac_nids[3]);
1138 if (spec->multi_ios > 0)
1139 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1141 spec->multi_io[0].pin, spec->multi_io[1].pin,
1142 spec->multi_io[0].dac, spec->multi_io[1].dac);
1143 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1144 cfg->hp_pins[0], cfg->hp_pins[1],
1145 cfg->hp_pins[2], cfg->hp_pins[2],
1146 spec->multiout.hp_out_nid[0],
1147 spec->multiout.hp_out_nid[1],
1148 spec->multiout.hp_out_nid[2],
1149 spec->multiout.hp_out_nid[3]);
1150 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1151 cfg->speaker_pins[0], cfg->speaker_pins[1],
1152 cfg->speaker_pins[2], cfg->speaker_pins[3],
1153 spec->multiout.extra_out_nid[0],
1154 spec->multiout.extra_out_nid[1],
1155 spec->multiout.extra_out_nid[2],
1156 spec->multiout.extra_out_nid[3]);
1159 /* find all available DACs of the codec */
1160 static void fill_all_dac_nids(struct hda_codec *codec)
1162 struct hda_gen_spec *spec = codec->spec;
1164 hda_nid_t nid = codec->start_nid;
1166 spec->num_all_dacs = 0;
1167 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1168 for (i = 0; i < codec->num_nodes; i++, nid++) {
1169 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1171 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1172 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1175 spec->all_dacs[spec->num_all_dacs++] = nid;
1179 static int parse_output_paths(struct hda_codec *codec)
1181 struct hda_gen_spec *spec = codec->spec;
1182 struct auto_pin_cfg *cfg = &spec->autocfg;
1183 struct auto_pin_cfg *best_cfg;
1184 int best_badness = INT_MAX;
1186 bool fill_hardwired = true, fill_mio_first = true;
1187 bool best_wired = true, best_mio = true;
1188 bool hp_spk_swapped = false;
1190 fill_all_dac_nids(codec);
1192 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1198 badness = fill_and_eval_dacs(codec, fill_hardwired,
1204 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1205 cfg->line_out_type, fill_hardwired, fill_mio_first,
1207 debug_show_configs(spec, cfg);
1208 if (badness < best_badness) {
1209 best_badness = badness;
1211 best_wired = fill_hardwired;
1212 best_mio = fill_mio_first;
1216 fill_mio_first = !fill_mio_first;
1217 if (!fill_mio_first)
1219 fill_hardwired = !fill_hardwired;
1220 if (!fill_hardwired)
1224 hp_spk_swapped = true;
1225 if (cfg->speaker_outs > 0 &&
1226 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1227 cfg->hp_outs = cfg->line_outs;
1228 memcpy(cfg->hp_pins, cfg->line_out_pins,
1229 sizeof(cfg->hp_pins));
1230 cfg->line_outs = cfg->speaker_outs;
1231 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1232 sizeof(cfg->speaker_pins));
1233 cfg->speaker_outs = 0;
1234 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1235 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1236 fill_hardwired = true;
1239 if (cfg->hp_outs > 0 &&
1240 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1241 cfg->speaker_outs = cfg->line_outs;
1242 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1243 sizeof(cfg->speaker_pins));
1244 cfg->line_outs = cfg->hp_outs;
1245 memcpy(cfg->line_out_pins, cfg->hp_pins,
1246 sizeof(cfg->hp_pins));
1248 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1249 cfg->line_out_type = AUTO_PIN_HP_OUT;
1250 fill_hardwired = true;
1258 fill_and_eval_dacs(codec, best_wired, best_mio);
1260 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1261 cfg->line_out_type, best_wired, best_mio);
1262 debug_show_configs(spec, cfg);
1264 if (cfg->line_out_pins[0]) {
1265 struct nid_path *path;
1266 path = snd_hda_get_nid_path(codec,
1267 spec->multiout.dac_nids[0],
1268 cfg->line_out_pins[0]);
1270 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1277 /* add playback controls from the parsed DAC table */
1278 static int create_multi_out_ctls(struct hda_codec *codec,
1279 const struct auto_pin_cfg *cfg)
1281 struct hda_gen_spec *spec = codec->spec;
1282 int i, err, noutputs;
1284 noutputs = cfg->line_outs;
1285 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1286 noutputs += spec->multi_ios;
1288 for (i = 0; i < noutputs; i++) {
1292 struct nid_path *path;
1294 dac = spec->multiout.dac_nids[i];
1297 if (i >= cfg->line_outs) {
1298 pin = spec->multi_io[i - 1].pin;
1300 name = channel_name[i];
1302 pin = cfg->line_out_pins[i];
1303 name = get_line_out_pfx(spec, i, true, &index);
1306 path = snd_hda_get_nid_path(codec, dac, pin);
1309 if (!name || !strcmp(name, "CLFE")) {
1311 err = add_vol_ctl(codec, "Center", 0, 1, path);
1314 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1317 err = add_sw_ctl(codec, "Center", 0, 1, path);
1320 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1324 err = add_stereo_vol(codec, name, index, path);
1327 err = add_stereo_sw(codec, name, index, path);
1335 static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1336 hda_nid_t dac, const char *pfx, int cidx)
1338 struct nid_path *path;
1341 path = snd_hda_get_nid_path(codec, dac, pin);
1344 /* bind volume control will be created in the case of dac = 0 */
1346 err = add_stereo_vol(codec, pfx, cidx, path);
1350 err = add_stereo_sw(codec, pfx, cidx, path);
1356 /* add playback controls for speaker and HP outputs */
1357 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1358 const hda_nid_t *pins, const hda_nid_t *dacs,
1361 struct hda_gen_spec *spec = codec->spec;
1362 struct hda_bind_ctls *ctl;
1366 if (!num_pins || !pins[0])
1369 if (num_pins == 1) {
1370 hda_nid_t dac = *dacs;
1372 dac = spec->multiout.dac_nids[0];
1373 return create_extra_out(codec, *pins, dac, pfx, 0);
1376 for (i = 0; i < num_pins; i++) {
1378 if (dacs[num_pins - 1])
1379 dac = dacs[i]; /* with individual volumes */
1382 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1383 err = create_extra_out(codec, pins[i], dac,
1385 } else if (num_pins >= 3) {
1386 snprintf(name, sizeof(name), "%s %s",
1387 pfx, channel_name[i]);
1388 err = create_extra_out(codec, pins[i], dac, name, 0);
1390 err = create_extra_out(codec, pins[i], dac, pfx, i);
1395 if (dacs[num_pins - 1])
1398 /* Let's create a bind-controls for volumes */
1399 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1403 for (i = 0; i < num_pins; i++) {
1405 struct nid_path *path;
1406 if (!pins[i] || !dacs[i])
1408 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1411 vol = look_for_out_vol_nid(codec, path);
1414 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1417 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1418 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1425 static int create_hp_out_ctls(struct hda_codec *codec)
1427 struct hda_gen_spec *spec = codec->spec;
1428 return create_extra_outs(codec, spec->autocfg.hp_outs,
1429 spec->autocfg.hp_pins,
1430 spec->multiout.hp_out_nid,
1434 static int create_speaker_out_ctls(struct hda_codec *codec)
1436 struct hda_gen_spec *spec = codec->spec;
1437 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1438 spec->autocfg.speaker_pins,
1439 spec->multiout.extra_out_nid,
1444 * channel mode enum control
1447 static int ch_mode_info(struct snd_kcontrol *kcontrol,
1448 struct snd_ctl_elem_info *uinfo)
1450 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1451 struct hda_gen_spec *spec = codec->spec;
1453 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1455 uinfo->value.enumerated.items = spec->multi_ios + 1;
1456 if (uinfo->value.enumerated.item > spec->multi_ios)
1457 uinfo->value.enumerated.item = spec->multi_ios;
1458 sprintf(uinfo->value.enumerated.name, "%dch",
1459 (uinfo->value.enumerated.item + 1) * 2);
1463 static int ch_mode_get(struct snd_kcontrol *kcontrol,
1464 struct snd_ctl_elem_value *ucontrol)
1466 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1467 struct hda_gen_spec *spec = codec->spec;
1468 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1472 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1474 struct hda_gen_spec *spec = codec->spec;
1475 hda_nid_t nid = spec->multi_io[idx].pin;
1476 struct nid_path *path;
1478 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1482 if (path->active == output)
1486 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1487 snd_hda_activate_path(codec, path, true, true);
1489 snd_hda_activate_path(codec, path, false, true);
1490 snd_hda_set_pin_ctl_cache(codec, nid,
1491 spec->multi_io[idx].ctl_in);
1496 static int ch_mode_put(struct snd_kcontrol *kcontrol,
1497 struct snd_ctl_elem_value *ucontrol)
1499 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1500 struct hda_gen_spec *spec = codec->spec;
1503 ch = ucontrol->value.enumerated.item[0];
1504 if (ch < 0 || ch > spec->multi_ios)
1506 if (ch == (spec->ext_channel_count - 1) / 2)
1508 spec->ext_channel_count = (ch + 1) * 2;
1509 for (i = 0; i < spec->multi_ios; i++)
1510 set_multi_io(codec, i, i < ch);
1511 spec->multiout.max_channels = max(spec->ext_channel_count,
1512 spec->const_channel_count);
1513 if (spec->need_dac_fix)
1514 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1518 static const struct snd_kcontrol_new channel_mode_enum = {
1519 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1520 .name = "Channel Mode",
1521 .info = ch_mode_info,
1526 static int create_multi_channel_mode(struct hda_codec *codec)
1528 struct hda_gen_spec *spec = codec->spec;
1530 if (spec->multi_ios > 0) {
1531 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
1538 * shared headphone/mic handling
1541 static void call_update_outputs(struct hda_codec *codec);
1543 /* for shared I/O, change the pin-control accordingly */
1544 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1546 struct hda_gen_spec *spec = codec->spec;
1548 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1549 /* NOTE: this assumes that there are only two inputs, the
1550 * first is the real internal mic and the second is HP/mic jack.
1553 val = snd_hda_get_default_vref(codec, pin);
1555 /* This pin does not have vref caps - let's enable vref on pin 0x18
1556 instead, as suggested by Realtek */
1557 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1558 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1559 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1560 if (vref_val != AC_PINCTL_VREF_HIZ)
1561 snd_hda_set_pin_ctl(codec, vref_pin, PIN_IN | (set_as_mic ? vref_val : 0));
1564 val = set_as_mic ? val | PIN_IN : PIN_HP;
1565 snd_hda_set_pin_ctl(codec, pin, val);
1567 spec->automute_speaker = !set_as_mic;
1568 call_update_outputs(codec);
1571 /* create a shared input with the headphone out */
1572 static int create_shared_input(struct hda_codec *codec)
1574 struct hda_gen_spec *spec = codec->spec;
1575 struct auto_pin_cfg *cfg = &spec->autocfg;
1576 unsigned int defcfg;
1579 /* only one internal input pin? */
1580 if (cfg->num_inputs != 1)
1582 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1583 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1586 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1587 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1588 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1589 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1591 return 0; /* both not available */
1593 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1594 return 0; /* no input */
1596 cfg->inputs[1].pin = nid;
1597 cfg->inputs[1].type = AUTO_PIN_MIC;
1598 cfg->num_inputs = 2;
1599 spec->shared_mic_hp = 1;
1600 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1610 /* add the powersave loopback-list entry */
1611 static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1613 struct hda_amp_list *list;
1615 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1617 list = spec->loopback_list + spec->num_loopbacks;
1619 list->dir = HDA_INPUT;
1621 spec->num_loopbacks++;
1622 spec->loopback.amplist = spec->loopback_list;
1625 #define add_loopback_list(spec, mix, idx) /* NOP */
1628 /* create input playback/capture controls for the given pin */
1629 static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1630 const char *ctlname, int ctlidx,
1633 struct hda_gen_spec *spec = codec->spec;
1634 struct nid_path *path;
1638 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1639 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1640 return 0; /* no need for analog loopback */
1642 path = snd_hda_add_new_path(codec, pin, mix_nid, 2);
1646 idx = path->idx[path->depth - 1];
1647 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1648 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1649 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
1652 path->ctls[NID_PATH_VOL_CTL] = val;
1655 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1656 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1657 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
1660 path->ctls[NID_PATH_MUTE_CTL] = val;
1663 path->active = true;
1664 add_loopback_list(spec, mix_nid, idx);
1668 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
1670 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1671 return (pincap & AC_PINCAP_IN) != 0;
1674 /* Parse the codec tree and retrieve ADCs */
1675 static int fill_adc_nids(struct hda_codec *codec)
1677 struct hda_gen_spec *spec = codec->spec;
1679 hda_nid_t *adc_nids = spec->adc_nids;
1680 int max_nums = ARRAY_SIZE(spec->adc_nids);
1683 nid = codec->start_nid;
1684 for (i = 0; i < codec->num_nodes; i++, nid++) {
1685 unsigned int caps = get_wcaps(codec, nid);
1686 int type = get_wcaps_type(caps);
1688 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1690 adc_nids[nums] = nid;
1691 if (++nums >= max_nums)
1694 spec->num_adc_nids = nums;
1698 /* filter out invalid adc_nids that don't give all active input pins;
1699 * if needed, check whether dynamic ADC-switching is available
1701 static int check_dyn_adc_switch(struct hda_codec *codec)
1703 struct hda_gen_spec *spec = codec->spec;
1704 struct hda_input_mux *imux = &spec->input_mux;
1705 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1711 for (n = 0; n < spec->num_adc_nids; n++) {
1712 adc = spec->adc_nids[n];
1713 for (i = 0; i < imux->num_items; i++) {
1714 pin = spec->imux_pins[i];
1715 if (!is_reachable_path(codec, pin, adc))
1718 if (i >= imux->num_items)
1719 adc_nids[nums++] = adc;
1723 if (spec->shared_mic_hp) {
1724 spec->shared_mic_hp = 0;
1725 imux->num_items = 1;
1729 /* check whether ADC-switch is possible */
1730 for (i = 0; i < imux->num_items; i++) {
1731 pin = spec->imux_pins[i];
1732 for (n = 0; n < spec->num_adc_nids; n++) {
1733 adc = spec->adc_nids[n];
1734 if (is_reachable_path(codec, pin, adc)) {
1735 spec->dyn_adc_idx[i] = n;
1741 snd_printdd("hda-codec: enabling ADC switching\n");
1742 spec->dyn_adc_switch = 1;
1743 } else if (nums != spec->num_adc_nids) {
1744 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1745 spec->num_adc_nids = nums;
1748 if (imux->num_items == 1 || spec->shared_mic_hp) {
1749 snd_printdd("hda-codec: reducing to a single ADC\n");
1750 spec->num_adc_nids = 1; /* reduce to a single ADC */
1753 /* single index for individual volumes ctls */
1754 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1755 spec->num_adc_nids = 1;
1761 * create playback/capture controls for input pins
1763 static int create_input_ctls(struct hda_codec *codec)
1765 struct hda_gen_spec *spec = codec->spec;
1766 const struct auto_pin_cfg *cfg = &spec->autocfg;
1767 hda_nid_t mixer = spec->mixer_nid;
1768 struct hda_input_mux *imux = &spec->input_mux;
1770 int i, c, err, type_idx = 0;
1771 const char *prev_label = NULL;
1773 num_adcs = fill_adc_nids(codec);
1777 for (i = 0; i < cfg->num_inputs; i++) {
1782 pin = cfg->inputs[i].pin;
1783 if (!is_input_pin(codec, pin))
1786 label = hda_get_autocfg_input_label(codec, cfg, i);
1787 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1788 label = "Headphone Mic";
1789 if (prev_label && !strcmp(label, prev_label))
1796 if (is_reachable_path(codec, pin, mixer)) {
1797 err = new_analog_input(codec, pin,
1798 label, type_idx, mixer);
1805 for (c = 0; c < num_adcs; c++) {
1806 struct nid_path *path;
1807 hda_nid_t adc = spec->adc_nids[c];
1809 if (!is_reachable_path(codec, pin, adc))
1811 path = snd_array_new(&spec->paths);
1814 memset(path, 0, sizeof(*path));
1815 if (!snd_hda_parse_nid_path(codec, pin, adc, 2, path)) {
1817 "invalid input path 0x%x -> 0x%x\n",
1824 spec->imux_pins[imux->num_items] = pin;
1825 snd_hda_add_imux_item(imux, label,
1826 imux->num_items, NULL);
1840 /* get the ADC NID corresponding to the given index */
1841 static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1843 struct hda_gen_spec *spec = codec->spec;
1844 if (spec->dyn_adc_switch)
1845 adc_idx = spec->dyn_adc_idx[imux_idx];
1846 return spec->adc_nids[adc_idx];
1849 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
1852 static int mux_enum_info(struct snd_kcontrol *kcontrol,
1853 struct snd_ctl_elem_info *uinfo)
1855 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1856 struct hda_gen_spec *spec = codec->spec;
1857 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1860 static int mux_enum_get(struct snd_kcontrol *kcontrol,
1861 struct snd_ctl_elem_value *ucontrol)
1863 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1864 struct hda_gen_spec *spec = codec->spec;
1865 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1867 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1871 static int mux_enum_put(struct snd_kcontrol *kcontrol,
1872 struct snd_ctl_elem_value *ucontrol)
1874 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1875 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1876 return mux_select(codec, adc_idx,
1877 ucontrol->value.enumerated.item[0]);
1881 * capture volume and capture switch ctls
1884 static const struct snd_kcontrol_new cap_src_temp = {
1885 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1886 .name = "Input Source",
1887 .info = mux_enum_info,
1888 .get = mux_enum_get,
1889 .put = mux_enum_put,
1892 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
1893 struct snd_ctl_elem_value *ucontrol);
1895 static int cap_put_caller(struct snd_kcontrol *kcontrol,
1896 struct snd_ctl_elem_value *ucontrol,
1897 put_call_t func, int type)
1899 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1900 struct hda_gen_spec *spec = codec->spec;
1901 const struct hda_input_mux *imux;
1902 struct nid_path *path;
1903 int i, adc_idx, err = 0;
1905 imux = &spec->input_mux;
1906 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1907 mutex_lock(&codec->control_mutex);
1908 codec->cached_write = 1;
1909 for (i = 0; i < imux->num_items; i++) {
1910 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
1911 get_adc_nid(codec, adc_idx, i));
1912 if (!path->ctls[type])
1914 kcontrol->private_value = path->ctls[type];
1915 err = func(kcontrol, ucontrol);
1920 codec->cached_write = 0;
1921 mutex_unlock(&codec->control_mutex);
1922 if (err >= 0 && spec->cap_sync_hook)
1923 spec->cap_sync_hook(codec);
1927 /* capture volume ctl callbacks */
1928 #define cap_vol_info snd_hda_mixer_amp_volume_info
1929 #define cap_vol_get snd_hda_mixer_amp_volume_get
1930 #define cap_vol_tlv snd_hda_mixer_amp_tlv
1932 static int cap_vol_put(struct snd_kcontrol *kcontrol,
1933 struct snd_ctl_elem_value *ucontrol)
1935 return cap_put_caller(kcontrol, ucontrol,
1936 snd_hda_mixer_amp_volume_put,
1940 static const struct snd_kcontrol_new cap_vol_temp = {
1941 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1942 .name = "Capture Volume",
1943 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1944 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1945 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
1946 .info = cap_vol_info,
1949 .tlv = { .c = cap_vol_tlv },
1952 /* capture switch ctl callbacks */
1953 #define cap_sw_info snd_ctl_boolean_stereo_info
1954 #define cap_sw_get snd_hda_mixer_amp_switch_get
1956 static int cap_sw_put(struct snd_kcontrol *kcontrol,
1957 struct snd_ctl_elem_value *ucontrol)
1959 return cap_put_caller(kcontrol, ucontrol,
1960 snd_hda_mixer_amp_switch_put,
1964 static const struct snd_kcontrol_new cap_sw_temp = {
1965 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1966 .name = "Capture Switch",
1967 .info = cap_sw_info,
1972 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
1977 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
1978 for (depth = 0; depth < 3; depth++) {
1979 if (depth >= path->depth)
1981 i = path->depth - depth - 1;
1982 nid = path->path[i];
1983 if (!path->ctls[NID_PATH_VOL_CTL]) {
1984 if (nid_has_volume(codec, nid, HDA_OUTPUT))
1985 path->ctls[NID_PATH_VOL_CTL] =
1986 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1987 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
1988 int idx = path->idx[i];
1989 if (!depth && codec->single_adc_amp)
1991 path->ctls[NID_PATH_VOL_CTL] =
1992 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
1995 if (!path->ctls[NID_PATH_MUTE_CTL]) {
1996 if (nid_has_mute(codec, nid, HDA_OUTPUT))
1997 path->ctls[NID_PATH_MUTE_CTL] =
1998 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1999 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2000 int idx = path->idx[i];
2001 if (!depth && codec->single_adc_amp)
2003 path->ctls[NID_PATH_MUTE_CTL] =
2004 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2011 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2013 struct hda_gen_spec *spec = codec->spec;
2014 struct auto_pin_cfg *cfg = &spec->autocfg;
2018 if (!spec->inv_dmic_split)
2020 for (i = 0; i < cfg->num_inputs; i++) {
2021 if (cfg->inputs[i].pin != nid)
2023 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2025 val = snd_hda_codec_get_pincfg(codec, nid);
2026 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2031 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2032 int idx, bool is_switch, unsigned int ctl,
2035 struct hda_gen_spec *spec = codec->spec;
2037 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2038 const char *sfx = is_switch ? "Switch" : "Volume";
2039 unsigned int chs = inv_dmic ? 1 : 3;
2046 snprintf(tmpname, sizeof(tmpname),
2047 "%s Capture %s", label, sfx);
2049 snprintf(tmpname, sizeof(tmpname),
2051 err = add_control(spec, type, tmpname, idx,
2052 amp_val_replace_channels(ctl, chs));
2053 if (err < 0 || !inv_dmic)
2056 /* Make independent right kcontrol */
2058 snprintf(tmpname, sizeof(tmpname),
2059 "Inverted %s Capture %s", label, sfx);
2061 snprintf(tmpname, sizeof(tmpname),
2062 "Inverted Capture %s", sfx);
2063 return add_control(spec, type, tmpname, idx,
2064 amp_val_replace_channels(ctl, 2));
2067 /* create single (and simple) capture volume and switch controls */
2068 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2069 unsigned int vol_ctl, unsigned int sw_ctl,
2073 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2076 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2082 /* create bound capture volume and switch controls */
2083 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2084 unsigned int vol_ctl, unsigned int sw_ctl)
2086 struct hda_gen_spec *spec = codec->spec;
2087 struct snd_kcontrol_new *knew;
2090 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
2094 knew->private_value = vol_ctl;
2095 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2098 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
2102 knew->private_value = sw_ctl;
2103 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2108 /* return the vol ctl when used first in the imux list */
2109 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2111 struct hda_gen_spec *spec = codec->spec;
2112 struct nid_path *path;
2116 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2117 get_adc_nid(codec, 0, idx));
2120 ctl = path->ctls[type];
2123 for (i = 0; i < idx - 1; i++) {
2124 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2125 get_adc_nid(codec, 0, i));
2126 if (path && path->ctls[type] == ctl)
2132 /* create individual capture volume and switch controls per input */
2133 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2135 struct hda_gen_spec *spec = codec->spec;
2136 struct hda_input_mux *imux = &spec->input_mux;
2137 int i, err, type, type_idx = 0;
2138 const char *prev_label = NULL;
2140 for (i = 0; i < imux->num_items; i++) {
2143 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2144 if (prev_label && !strcmp(label, prev_label))
2149 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2151 for (type = 0; type < 2; type++) {
2152 err = add_single_cap_ctl(codec, label, type_idx, type,
2153 get_first_cap_ctl(codec, i, type),
2162 static int create_capture_mixers(struct hda_codec *codec)
2164 struct hda_gen_spec *spec = codec->spec;
2165 struct hda_input_mux *imux = &spec->input_mux;
2166 int i, n, nums, err;
2168 if (spec->dyn_adc_switch)
2171 nums = spec->num_adc_nids;
2173 if (!spec->auto_mic && imux->num_items > 1) {
2174 struct snd_kcontrol_new *knew;
2175 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_src_temp);
2181 for (n = 0; n < nums; n++) {
2183 bool inv_dmic = false;
2187 for (i = 0; i < imux->num_items; i++) {
2188 struct nid_path *path;
2189 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2190 get_adc_nid(codec, n, i));
2193 parse_capvol_in_path(codec, path);
2195 vol = path->ctls[NID_PATH_VOL_CTL];
2196 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2199 sw = path->ctls[NID_PATH_MUTE_CTL];
2200 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2202 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2207 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2209 else if (!spec->multi_cap_vol)
2210 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2212 err = create_multi_cap_vol_ctl(codec);
2221 * add mic boosts if needed
2223 static int parse_mic_boost(struct hda_codec *codec)
2225 struct hda_gen_spec *spec = codec->spec;
2226 struct auto_pin_cfg *cfg = &spec->autocfg;
2230 const char *prev_label = NULL;
2232 for (i = 0; i < cfg->num_inputs; i++) {
2233 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2235 nid = cfg->inputs[i].pin;
2236 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2238 char boost_label[32];
2239 struct nid_path *path;
2242 label = hda_get_autocfg_input_label(codec, cfg, i);
2243 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2244 label = "Headphone Mic";
2245 if (prev_label && !strcmp(label, prev_label))
2251 snprintf(boost_label, sizeof(boost_label),
2252 "%s Boost Volume", label);
2253 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2254 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2255 boost_label, type_idx, val);
2259 path = snd_hda_get_nid_path(codec, nid, 0);
2261 path->ctls[NID_PATH_BOOST_CTL] = val;
2268 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2270 static void parse_digital(struct hda_codec *codec)
2272 struct hda_gen_spec *spec = codec->spec;
2276 /* support multiple SPDIFs; the secondary is set up as a slave */
2278 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2279 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2280 dig_nid = look_for_dac(codec, pin, true);
2283 if (!snd_hda_add_new_path(codec, dig_nid, pin, 2))
2286 spec->multiout.dig_out_nid = dig_nid;
2287 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2289 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2290 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2292 spec->slave_dig_outs[nums - 1] = dig_nid;
2297 if (spec->autocfg.dig_in_pin) {
2298 dig_nid = codec->start_nid;
2299 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2300 struct nid_path *path;
2301 unsigned int wcaps = get_wcaps(codec, dig_nid);
2302 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2304 if (!(wcaps & AC_WCAP_DIGITAL))
2306 path = snd_hda_add_new_path(codec,
2307 spec->autocfg.dig_in_pin,
2310 path->active = true;
2311 spec->dig_in_nid = dig_nid;
2320 * input MUX handling
2323 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2325 /* select the given imux item; either unmute exclusively or select the route */
2326 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2329 struct hda_gen_spec *spec = codec->spec;
2330 const struct hda_input_mux *imux;
2331 struct nid_path *path;
2333 imux = &spec->input_mux;
2334 if (!imux->num_items)
2337 if (idx >= imux->num_items)
2338 idx = imux->num_items - 1;
2339 if (spec->cur_mux[adc_idx] == idx)
2342 path = snd_hda_get_nid_path(codec,
2343 spec->imux_pins[spec->cur_mux[adc_idx]],
2344 spec->adc_nids[adc_idx]);
2348 snd_hda_activate_path(codec, path, false, false);
2350 spec->cur_mux[adc_idx] = idx;
2352 if (spec->shared_mic_hp)
2353 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2355 if (spec->dyn_adc_switch)
2356 dyn_adc_pcm_resetup(codec, idx);
2358 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2359 get_adc_nid(codec, adc_idx, idx));
2364 snd_hda_activate_path(codec, path, true, false);
2365 if (spec->cap_sync_hook)
2366 spec->cap_sync_hook(codec);
2372 * Jack detections for HP auto-mute and mic-switch
2375 /* check each pin in the given array; returns true if any of them is plugged */
2376 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2380 for (i = 0; i < num_pins; i++) {
2381 hda_nid_t nid = pins[i];
2384 present |= snd_hda_jack_detect(codec, nid);
2389 /* standard HP/line-out auto-mute helper */
2390 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2391 bool mute, bool hp_out)
2393 struct hda_gen_spec *spec = codec->spec;
2394 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2397 for (i = 0; i < num_pins; i++) {
2398 hda_nid_t nid = pins[i];
2402 /* don't reset VREF value in case it's controlling
2403 * the amp (see alc861_fixup_asus_amp_vref_0f())
2405 if (spec->keep_vref_in_automute) {
2406 val = snd_hda_codec_read(codec, nid, 0,
2407 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2412 snd_hda_set_pin_ctl(codec, nid, val);
2416 /* Toggle outputs muting */
2417 static void update_outputs(struct hda_codec *codec)
2419 struct hda_gen_spec *spec = codec->spec;
2422 /* Control HP pins/amps depending on master_mute state;
2423 * in general, HP pins/amps control should be enabled in all cases,
2424 * but currently set only for master_mute, just to be safe
2426 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2427 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2428 spec->autocfg.hp_pins, spec->master_mute, true);
2430 if (!spec->automute_speaker)
2433 on = spec->hp_jack_present | spec->line_jack_present;
2434 on |= spec->master_mute;
2435 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2436 spec->autocfg.speaker_pins, on, false);
2438 /* toggle line-out mutes if needed, too */
2439 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2440 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2441 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2443 if (!spec->automute_lo)
2446 on = spec->hp_jack_present;
2447 on |= spec->master_mute;
2448 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2449 spec->autocfg.line_out_pins, on, false);
2452 static void call_update_outputs(struct hda_codec *codec)
2454 struct hda_gen_spec *spec = codec->spec;
2455 if (spec->automute_hook)
2456 spec->automute_hook(codec);
2458 update_outputs(codec);
2461 /* standard HP-automute helper */
2462 static void hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2464 struct hda_gen_spec *spec = codec->spec;
2466 spec->hp_jack_present =
2467 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2468 spec->autocfg.hp_pins);
2469 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2471 call_update_outputs(codec);
2474 /* standard line-out-automute helper */
2475 static void line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2477 struct hda_gen_spec *spec = codec->spec;
2479 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2481 /* check LO jack only when it's different from HP */
2482 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2485 spec->line_jack_present =
2486 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2487 spec->autocfg.line_out_pins);
2488 if (!spec->automute_speaker || !spec->detect_lo)
2490 call_update_outputs(codec);
2493 /* standard mic auto-switch helper */
2494 static void mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
2496 struct hda_gen_spec *spec = codec->spec;
2499 if (!spec->auto_mic)
2502 for (i = spec->am_num_entries - 1; i > 0; i--) {
2503 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2504 mux_select(codec, 0, spec->am_entry[i].idx);
2508 mux_select(codec, 0, spec->am_entry[0].idx);
2512 * Auto-Mute mode mixer enum support
2514 static int automute_mode_info(struct snd_kcontrol *kcontrol,
2515 struct snd_ctl_elem_info *uinfo)
2517 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2518 struct hda_gen_spec *spec = codec->spec;
2519 static const char * const texts3[] = {
2520 "Disabled", "Speaker Only", "Line Out+Speaker"
2523 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2524 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2525 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2528 static int automute_mode_get(struct snd_kcontrol *kcontrol,
2529 struct snd_ctl_elem_value *ucontrol)
2531 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2532 struct hda_gen_spec *spec = codec->spec;
2533 unsigned int val = 0;
2534 if (spec->automute_speaker)
2536 if (spec->automute_lo)
2539 ucontrol->value.enumerated.item[0] = val;
2543 static int automute_mode_put(struct snd_kcontrol *kcontrol,
2544 struct snd_ctl_elem_value *ucontrol)
2546 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2547 struct hda_gen_spec *spec = codec->spec;
2549 switch (ucontrol->value.enumerated.item[0]) {
2551 if (!spec->automute_speaker && !spec->automute_lo)
2553 spec->automute_speaker = 0;
2554 spec->automute_lo = 0;
2557 if (spec->automute_speaker_possible) {
2558 if (!spec->automute_lo && spec->automute_speaker)
2560 spec->automute_speaker = 1;
2561 spec->automute_lo = 0;
2562 } else if (spec->automute_lo_possible) {
2563 if (spec->automute_lo)
2565 spec->automute_lo = 1;
2570 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2572 if (spec->automute_speaker && spec->automute_lo)
2574 spec->automute_speaker = 1;
2575 spec->automute_lo = 1;
2580 call_update_outputs(codec);
2584 static const struct snd_kcontrol_new automute_mode_enum = {
2585 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2586 .name = "Auto-Mute Mode",
2587 .info = automute_mode_info,
2588 .get = automute_mode_get,
2589 .put = automute_mode_put,
2592 static int add_automute_mode_enum(struct hda_codec *codec)
2594 struct hda_gen_spec *spec = codec->spec;
2596 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
2602 * Check the availability of HP/line-out auto-mute;
2603 * Set up appropriately if really supported
2605 static int check_auto_mute_availability(struct hda_codec *codec)
2607 struct hda_gen_spec *spec = codec->spec;
2608 struct auto_pin_cfg *cfg = &spec->autocfg;
2612 if (cfg->hp_pins[0])
2614 if (cfg->line_out_pins[0])
2616 if (cfg->speaker_pins[0])
2618 if (present < 2) /* need two different output types */
2621 if (!cfg->speaker_pins[0] &&
2622 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2623 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2624 sizeof(cfg->speaker_pins));
2625 cfg->speaker_outs = cfg->line_outs;
2628 if (!cfg->hp_pins[0] &&
2629 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2630 memcpy(cfg->hp_pins, cfg->line_out_pins,
2631 sizeof(cfg->hp_pins));
2632 cfg->hp_outs = cfg->line_outs;
2635 for (i = 0; i < cfg->hp_outs; i++) {
2636 hda_nid_t nid = cfg->hp_pins[i];
2637 if (!is_jack_detectable(codec, nid))
2639 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2641 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
2643 spec->detect_hp = 1;
2646 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2647 if (cfg->speaker_outs)
2648 for (i = 0; i < cfg->line_outs; i++) {
2649 hda_nid_t nid = cfg->line_out_pins[i];
2650 if (!is_jack_detectable(codec, nid))
2652 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2653 snd_hda_jack_detect_enable_callback(codec, nid,
2654 HDA_GEN_FRONT_EVENT,
2656 spec->detect_lo = 1;
2658 spec->automute_lo_possible = spec->detect_hp;
2661 spec->automute_speaker_possible = cfg->speaker_outs &&
2662 (spec->detect_hp || spec->detect_lo);
2664 spec->automute_lo = spec->automute_lo_possible;
2665 spec->automute_speaker = spec->automute_speaker_possible;
2667 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2668 /* create a control for automute mode */
2669 err = add_automute_mode_enum(codec);
2676 /* return the position of NID in the list, or -1 if not found */
2677 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2680 for (i = 0; i < nums; i++)
2686 /* check whether all auto-mic pins are valid; setup indices if OK */
2687 static bool auto_mic_check_imux(struct hda_codec *codec)
2689 struct hda_gen_spec *spec = codec->spec;
2690 const struct hda_input_mux *imux;
2693 imux = &spec->input_mux;
2694 for (i = 0; i < spec->am_num_entries; i++) {
2695 spec->am_entry[i].idx =
2696 find_idx_in_nid_list(spec->am_entry[i].pin,
2697 spec->imux_pins, imux->num_items);
2698 if (spec->am_entry[i].idx < 0)
2699 return false; /* no corresponding imux */
2702 /* we don't need the jack detection for the first pin */
2703 for (i = 1; i < spec->am_num_entries; i++)
2704 snd_hda_jack_detect_enable_callback(codec,
2705 spec->am_entry[i].pin,
2711 static int compare_attr(const void *ap, const void *bp)
2713 const struct automic_entry *a = ap;
2714 const struct automic_entry *b = bp;
2715 return (int)(a->attr - b->attr);
2719 * Check the availability of auto-mic switch;
2720 * Set up if really supported
2722 static int check_auto_mic_availability(struct hda_codec *codec)
2724 struct hda_gen_spec *spec = codec->spec;
2725 struct auto_pin_cfg *cfg = &spec->autocfg;
2731 for (i = 0; i < cfg->num_inputs; i++) {
2732 hda_nid_t nid = cfg->inputs[i].pin;
2734 attr = snd_hda_codec_get_pincfg(codec, nid);
2735 attr = snd_hda_get_input_pin_attr(attr);
2736 if (types & (1 << attr))
2737 return 0; /* already occupied */
2739 case INPUT_PIN_ATTR_INT:
2740 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2741 return 0; /* invalid type */
2743 case INPUT_PIN_ATTR_UNUSED:
2744 return 0; /* invalid entry */
2746 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2747 return 0; /* invalid type */
2748 if (!spec->line_in_auto_switch &&
2749 cfg->inputs[i].type != AUTO_PIN_MIC)
2750 return 0; /* only mic is allowed */
2751 if (!is_jack_detectable(codec, nid))
2752 return 0; /* no unsol support */
2755 if (num_pins >= MAX_AUTO_MIC_PINS)
2757 types |= (1 << attr);
2758 spec->am_entry[num_pins].pin = nid;
2759 spec->am_entry[num_pins].attr = attr;
2766 spec->am_num_entries = num_pins;
2767 /* sort the am_entry in the order of attr so that the pin with a
2768 * higher attr will be selected when the jack is plugged.
2770 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2771 compare_attr, NULL);
2773 if (!auto_mic_check_imux(codec))
2777 spec->num_adc_nids = 1;
2778 spec->cur_mux[0] = spec->am_entry[0].idx;
2779 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2780 spec->am_entry[0].pin,
2781 spec->am_entry[1].pin,
2782 spec->am_entry[2].pin);
2789 * Parse the given BIOS configuration and set up the hda_gen_spec
2791 * return 1 if successful, 0 if the proper config is not found,
2792 * or a negative error code
2794 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
2795 struct auto_pin_cfg *cfg)
2797 struct hda_gen_spec *spec = codec->spec;
2800 if (cfg != &spec->autocfg) {
2801 spec->autocfg = *cfg;
2802 cfg = &spec->autocfg;
2805 if (!cfg->line_outs) {
2806 if (cfg->dig_outs || cfg->dig_in_pin) {
2807 spec->multiout.max_channels = 2;
2808 spec->no_analog = 1;
2811 return 0; /* can't find valid BIOS pin config */
2814 if (!spec->no_primary_hp &&
2815 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2816 cfg->line_outs <= cfg->hp_outs) {
2817 /* use HP as primary out */
2818 cfg->speaker_outs = cfg->line_outs;
2819 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2820 sizeof(cfg->speaker_pins));
2821 cfg->line_outs = cfg->hp_outs;
2822 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2824 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2825 cfg->line_out_type = AUTO_PIN_HP_OUT;
2828 err = parse_output_paths(codec);
2831 err = create_multi_channel_mode(codec);
2834 err = create_multi_out_ctls(codec, cfg);
2837 err = create_hp_out_ctls(codec);
2840 err = create_speaker_out_ctls(codec);
2843 err = create_shared_input(codec);
2846 err = create_input_ctls(codec);
2850 /* check the multiple speaker pins */
2851 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2852 spec->const_channel_count = cfg->line_outs * 2;
2854 spec->const_channel_count = cfg->speaker_outs * 2;
2856 if (spec->multi_ios > 0)
2857 spec->multiout.max_channels = max(spec->ext_channel_count,
2858 spec->const_channel_count);
2860 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2862 err = check_auto_mute_availability(codec);
2866 err = check_dyn_adc_switch(codec);
2870 if (!spec->shared_mic_hp) {
2871 err = check_auto_mic_availability(codec);
2876 err = create_capture_mixers(codec);
2880 err = parse_mic_boost(codec);
2885 parse_digital(codec);
2889 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
2893 * Build control elements
2896 /* slave controls for virtual master */
2897 static const char * const slave_pfxs[] = {
2898 "Front", "Surround", "Center", "LFE", "Side",
2899 "Headphone", "Speaker", "Mono", "Line Out",
2900 "CLFE", "Bass Speaker", "PCM",
2904 int snd_hda_gen_build_controls(struct hda_codec *codec)
2906 struct hda_gen_spec *spec = codec->spec;
2909 if (spec->kctls.used) {
2910 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
2915 if (spec->multiout.dig_out_nid) {
2916 err = snd_hda_create_dig_out_ctls(codec,
2917 spec->multiout.dig_out_nid,
2918 spec->multiout.dig_out_nid,
2919 spec->pcm_rec[1].pcm_type);
2922 if (!spec->no_analog) {
2923 err = snd_hda_create_spdif_share_sw(codec,
2927 spec->multiout.share_spdif = 1;
2930 if (spec->dig_in_nid) {
2931 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
2936 /* if we have no master control, let's create it */
2937 if (!spec->no_analog &&
2938 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
2939 unsigned int vmaster_tlv[4];
2940 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2941 HDA_OUTPUT, vmaster_tlv);
2942 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
2943 vmaster_tlv, slave_pfxs,
2948 if (!spec->no_analog &&
2949 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
2950 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
2953 true, &spec->vmaster_mute.sw_kctl);
2956 if (spec->vmaster_mute.hook)
2957 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute, true);
2960 free_kctls(spec); /* no longer needed */
2962 if (spec->shared_mic_hp) {
2964 int nid = spec->autocfg.inputs[1].pin;
2965 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
2968 err = snd_hda_jack_detect_enable(codec, nid, 0);
2973 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
2979 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
2987 * Analog playback callbacks
2989 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
2990 struct hda_codec *codec,
2991 struct snd_pcm_substream *substream)
2993 struct hda_gen_spec *spec = codec->spec;
2994 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
2998 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2999 struct hda_codec *codec,
3000 unsigned int stream_tag,
3001 unsigned int format,
3002 struct snd_pcm_substream *substream)
3004 struct hda_gen_spec *spec = codec->spec;
3005 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3006 stream_tag, format, substream);
3009 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3010 struct hda_codec *codec,
3011 struct snd_pcm_substream *substream)
3013 struct hda_gen_spec *spec = codec->spec;
3014 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3020 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3021 struct hda_codec *codec,
3022 struct snd_pcm_substream *substream)
3024 struct hda_gen_spec *spec = codec->spec;
3025 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3028 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3029 struct hda_codec *codec,
3030 unsigned int stream_tag,
3031 unsigned int format,
3032 struct snd_pcm_substream *substream)
3034 struct hda_gen_spec *spec = codec->spec;
3035 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3036 stream_tag, format, substream);
3039 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3040 struct hda_codec *codec,
3041 struct snd_pcm_substream *substream)
3043 struct hda_gen_spec *spec = codec->spec;
3044 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3047 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3048 struct hda_codec *codec,
3049 struct snd_pcm_substream *substream)
3051 struct hda_gen_spec *spec = codec->spec;
3052 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3058 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3059 struct hda_codec *codec,
3060 unsigned int stream_tag,
3061 unsigned int format,
3062 struct snd_pcm_substream *substream)
3064 struct hda_gen_spec *spec = codec->spec;
3066 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
3067 stream_tag, 0, format);
3071 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3072 struct hda_codec *codec,
3073 struct snd_pcm_substream *substream)
3075 struct hda_gen_spec *spec = codec->spec;
3077 snd_hda_codec_cleanup_stream(codec,
3078 spec->adc_nids[substream->number + 1]);
3084 static const struct hda_pcm_stream pcm_analog_playback = {
3088 /* NID is set in build_pcms */
3090 .open = playback_pcm_open,
3091 .prepare = playback_pcm_prepare,
3092 .cleanup = playback_pcm_cleanup
3096 static const struct hda_pcm_stream pcm_analog_capture = {
3100 /* NID is set in build_pcms */
3103 static const struct hda_pcm_stream pcm_analog_alt_playback = {
3107 /* NID is set in build_pcms */
3110 static const struct hda_pcm_stream pcm_analog_alt_capture = {
3111 .substreams = 2, /* can be overridden */
3114 /* NID is set in build_pcms */
3116 .prepare = alt_capture_pcm_prepare,
3117 .cleanup = alt_capture_pcm_cleanup
3121 static const struct hda_pcm_stream pcm_digital_playback = {
3125 /* NID is set in build_pcms */
3127 .open = dig_playback_pcm_open,
3128 .close = dig_playback_pcm_close,
3129 .prepare = dig_playback_pcm_prepare,
3130 .cleanup = dig_playback_pcm_cleanup
3134 static const struct hda_pcm_stream pcm_digital_capture = {
3138 /* NID is set in build_pcms */
3141 /* Used by build_pcms to flag that a PCM has no playback stream */
3142 static const struct hda_pcm_stream pcm_null_stream = {
3149 * dynamic changing ADC PCM streams
3151 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3153 struct hda_gen_spec *spec = codec->spec;
3154 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3156 if (spec->cur_adc && spec->cur_adc != new_adc) {
3157 /* stream is running, let's swap the current ADC */
3158 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3159 spec->cur_adc = new_adc;
3160 snd_hda_codec_setup_stream(codec, new_adc,
3161 spec->cur_adc_stream_tag, 0,
3162 spec->cur_adc_format);
3168 /* analog capture with dynamic dual-adc changes */
3169 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3170 struct hda_codec *codec,
3171 unsigned int stream_tag,
3172 unsigned int format,
3173 struct snd_pcm_substream *substream)
3175 struct hda_gen_spec *spec = codec->spec;
3176 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3177 spec->cur_adc_stream_tag = stream_tag;
3178 spec->cur_adc_format = format;
3179 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3183 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3184 struct hda_codec *codec,
3185 struct snd_pcm_substream *substream)
3187 struct hda_gen_spec *spec = codec->spec;
3188 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3193 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3197 .nid = 0, /* fill later */
3199 .prepare = dyn_adc_capture_pcm_prepare,
3200 .cleanup = dyn_adc_capture_pcm_cleanup
3204 /* build PCM streams based on the parsed results */
3205 int snd_hda_gen_build_pcms(struct hda_codec *codec)
3207 struct hda_gen_spec *spec = codec->spec;
3208 struct hda_pcm *info = spec->pcm_rec;
3209 const struct hda_pcm_stream *p;
3210 bool have_multi_adcs;
3213 codec->num_pcms = 1;
3214 codec->pcm_info = info;
3216 if (spec->no_analog)
3219 snprintf(spec->stream_name_analog, sizeof(spec->stream_name_analog),
3220 "%s Analog", codec->chip_name);
3221 info->name = spec->stream_name_analog;
3223 if (spec->multiout.num_dacs > 0) {
3224 p = spec->stream_analog_playback;
3226 p = &pcm_analog_playback;
3227 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3228 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3229 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3230 spec->multiout.max_channels;
3231 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3232 spec->autocfg.line_outs == 2)
3233 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3236 if (spec->num_adc_nids) {
3237 p = spec->stream_analog_capture;
3239 if (spec->dyn_adc_switch)
3240 p = &dyn_adc_pcm_analog_capture;
3242 p = &pcm_analog_capture;
3244 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3245 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3248 if (spec->channel_mode) {
3249 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = 0;
3250 for (i = 0; i < spec->num_channel_mode; i++) {
3251 if (spec->channel_mode[i].channels > info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max) {
3252 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = spec->channel_mode[i].channels;
3258 /* SPDIF for stream index #1 */
3259 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
3260 snprintf(spec->stream_name_digital,
3261 sizeof(spec->stream_name_digital),
3262 "%s Digital", codec->chip_name);
3263 codec->num_pcms = 2;
3264 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3265 info = spec->pcm_rec + 1;
3266 info->name = spec->stream_name_digital;
3267 if (spec->dig_out_type)
3268 info->pcm_type = spec->dig_out_type;
3270 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3271 if (spec->multiout.dig_out_nid) {
3272 p = spec->stream_digital_playback;
3274 p = &pcm_digital_playback;
3275 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3276 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3278 if (spec->dig_in_nid) {
3279 p = spec->stream_digital_capture;
3281 p = &pcm_digital_capture;
3282 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3283 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3287 if (spec->no_analog)
3290 /* If the use of more than one ADC is requested for the current
3291 * model, configure a second analog capture-only PCM.
3293 have_multi_adcs = (spec->num_adc_nids > 1) &&
3294 !spec->dyn_adc_switch && !spec->auto_mic;
3295 /* Additional Analaog capture for index #2 */
3296 if (spec->alt_dac_nid || have_multi_adcs) {
3297 codec->num_pcms = 3;
3298 info = spec->pcm_rec + 2;
3299 info->name = spec->stream_name_analog;
3300 if (spec->alt_dac_nid) {
3301 p = spec->stream_analog_alt_playback;
3303 p = &pcm_analog_alt_playback;
3304 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3305 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3308 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3310 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3312 if (have_multi_adcs) {
3313 p = spec->stream_analog_alt_capture;
3315 p = &pcm_analog_alt_capture;
3316 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3317 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3319 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3320 spec->num_adc_nids - 1;
3322 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3324 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3330 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3334 * Standard auto-parser initializations
3337 /* configure the path from the given dac to the pin as the proper output */
3338 static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3339 int pin_type, hda_nid_t dac)
3341 struct hda_gen_spec *spec = codec->spec;
3342 struct nid_path *path;
3344 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3345 path = snd_hda_get_nid_path(codec, dac, pin);
3350 snd_hda_activate_path(codec, path, true, true);
3352 if (!spec->own_eapd_ctl &&
3353 (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
3354 snd_hda_codec_update_cache(codec, pin, 0,
3355 AC_VERB_SET_EAPD_BTLENABLE, 0x02);
3358 /* initialize primary output paths */
3359 static void init_multi_out(struct hda_codec *codec)
3361 struct hda_gen_spec *spec = codec->spec;
3365 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3370 for (i = 0; i <= HDA_SIDE; i++) {
3371 hda_nid_t nid = spec->autocfg.line_out_pins[i];
3373 set_output_and_unmute(codec, nid, pin_type,
3374 spec->multiout.dac_nids[i]);
3379 /* initialize hp and speaker paths */
3380 static void init_extra_out(struct hda_codec *codec)
3382 struct hda_gen_spec *spec = codec->spec;
3386 for (i = 0; i < spec->autocfg.hp_outs; i++) {
3387 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3389 pin = spec->autocfg.hp_pins[i];
3392 dac = spec->multiout.hp_out_nid[i];
3394 if (i > 0 && spec->multiout.hp_out_nid[0])
3395 dac = spec->multiout.hp_out_nid[0];
3397 dac = spec->multiout.dac_nids[0];
3399 set_output_and_unmute(codec, pin, PIN_HP, dac);
3401 for (i = 0; i < spec->autocfg.speaker_outs; i++) {
3402 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3404 pin = spec->autocfg.speaker_pins[i];
3407 dac = spec->multiout.extra_out_nid[i];
3409 if (i > 0 && spec->multiout.extra_out_nid[0])
3410 dac = spec->multiout.extra_out_nid[0];
3412 dac = spec->multiout.dac_nids[0];
3414 set_output_and_unmute(codec, pin, PIN_OUT, dac);
3418 /* initialize multi-io paths */
3419 static void init_multi_io(struct hda_codec *codec)
3421 struct hda_gen_spec *spec = codec->spec;
3424 for (i = 0; i < spec->multi_ios; i++) {
3425 hda_nid_t pin = spec->multi_io[i].pin;
3426 struct nid_path *path;
3427 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3430 if (!spec->multi_io[i].ctl_in)
3431 spec->multi_io[i].ctl_in =
3432 snd_hda_codec_update_cache(codec, pin, 0,
3433 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3434 snd_hda_activate_path(codec, path, path->active, true);
3438 /* set up the input pin config, depending on the given auto-pin type */
3439 static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3442 unsigned int val = PIN_IN;
3443 if (auto_pin_type == AUTO_PIN_MIC)
3444 val |= snd_hda_get_default_vref(codec, nid);
3445 snd_hda_set_pin_ctl(codec, nid, val);
3448 /* set up input pins and loopback paths */
3449 static void init_analog_input(struct hda_codec *codec)
3451 struct hda_gen_spec *spec = codec->spec;
3452 struct auto_pin_cfg *cfg = &spec->autocfg;
3455 for (i = 0; i < cfg->num_inputs; i++) {
3456 hda_nid_t nid = cfg->inputs[i].pin;
3457 if (is_input_pin(codec, nid))
3458 set_input_pin(codec, nid, cfg->inputs[i].type);
3460 /* init loopback inputs */
3461 if (spec->mixer_nid) {
3462 struct nid_path *path;
3463 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3465 snd_hda_activate_path(codec, path,
3466 path->active, false);
3471 /* initialize ADC paths */
3472 static void init_input_src(struct hda_codec *codec)
3474 struct hda_gen_spec *spec = codec->spec;
3475 struct hda_input_mux *imux = &spec->input_mux;
3476 struct nid_path *path;
3479 if (spec->dyn_adc_switch)
3482 nums = spec->num_adc_nids;
3484 for (c = 0; c < nums; c++) {
3485 for (i = 0; i < imux->num_items; i++) {
3486 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3487 get_adc_nid(codec, c, i));
3489 bool active = path->active;
3490 if (i == spec->cur_mux[c])
3492 snd_hda_activate_path(codec, path, active, false);
3497 if (spec->shared_mic_hp)
3498 update_shared_mic_hp(codec, spec->cur_mux[0]);
3500 if (spec->cap_sync_hook)
3501 spec->cap_sync_hook(codec);
3504 /* set right pin controls for digital I/O */
3505 static void init_digital(struct hda_codec *codec)
3507 struct hda_gen_spec *spec = codec->spec;
3511 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3512 pin = spec->autocfg.dig_out_pins[i];
3515 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3517 pin = spec->autocfg.dig_in_pin;
3519 snd_hda_set_pin_ctl(codec, pin, PIN_IN);
3522 int snd_hda_gen_init(struct hda_codec *codec)
3524 struct hda_gen_spec *spec = codec->spec;
3526 if (spec->init_hook)
3527 spec->init_hook(codec);
3529 snd_hda_apply_verbs(codec);
3531 init_multi_out(codec);
3532 init_extra_out(codec);
3533 init_multi_io(codec);
3534 init_analog_input(codec);
3535 init_input_src(codec);
3536 init_digital(codec);
3538 /* call init functions of standard auto-mute helpers */
3539 hp_automute(codec, NULL);
3540 line_automute(codec, NULL);
3541 mic_autoswitch(codec, NULL);
3543 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3544 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3546 hda_call_check_power_status(codec, 0x01);
3549 EXPORT_SYMBOL(snd_hda_gen_init);
3553 * the generic codec support
3557 static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3559 struct hda_gen_spec *spec = codec->spec;
3560 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3564 static void generic_free(struct hda_codec *codec)
3566 snd_hda_gen_spec_free(codec->spec);
3571 static const struct hda_codec_ops generic_patch_ops = {
3572 .build_controls = snd_hda_gen_build_controls,
3573 .build_pcms = snd_hda_gen_build_pcms,
3574 .init = snd_hda_gen_init,
3575 .free = generic_free,
3576 .unsol_event = snd_hda_jack_unsol_event,
3578 .check_power_status = generic_check_power_status,
3582 int snd_hda_parse_generic_codec(struct hda_codec *codec)
3584 struct hda_gen_spec *spec;
3587 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3590 snd_hda_gen_spec_init(spec);
3593 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3597 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
3601 codec->patch_ops = generic_patch_ops;
3605 generic_free(codec);
3608 EXPORT_SYMBOL(snd_hda_parse_generic_codec);