2 * Universal Interface for Intel High Definition Audio Codec
4 * Generic widget tree parser
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/sort.h>
27 #include <linux/ctype.h>
28 #include <linux/string.h>
29 #include <sound/core.h>
30 #include <sound/jack.h>
31 #include "hda_codec.h"
32 #include "hda_local.h"
33 #include "hda_auto_parser.h"
35 #include "hda_generic.h"
38 /* initialize hda_gen_spec struct */
39 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
41 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
42 snd_array_init(&spec->bind_ctls, sizeof(struct hda_bind_ctls *), 8);
43 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
46 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
48 struct snd_kcontrol_new *
49 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
50 const struct snd_kcontrol_new *temp)
52 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
57 knew->name = kstrdup(name, GFP_KERNEL);
59 knew->name = kstrdup(knew->name, GFP_KERNEL);
64 EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
66 static void free_kctls(struct hda_gen_spec *spec)
68 if (spec->kctls.list) {
69 struct snd_kcontrol_new *kctl = spec->kctls.list;
71 for (i = 0; i < spec->kctls.used; i++)
74 snd_array_free(&spec->kctls);
77 static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
79 struct hda_ctl_ops *ops)
81 struct hda_gen_spec *spec = codec->spec;
82 struct hda_bind_ctls **ctlp, *ctl;
83 ctlp = snd_array_new(&spec->bind_ctls);
86 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
93 static void free_bind_ctls(struct hda_gen_spec *spec)
95 if (spec->bind_ctls.list) {
96 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
98 for (i = 0; i < spec->bind_ctls.used; i++)
101 snd_array_free(&spec->bind_ctls);
104 void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
109 free_bind_ctls(spec);
110 snd_array_free(&spec->paths);
112 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
118 /* get the path between the given NIDs;
119 * passing 0 to either @pin or @dac behaves as a wildcard
121 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
122 hda_nid_t from_nid, hda_nid_t to_nid)
124 struct hda_gen_spec *spec = codec->spec;
127 for (i = 0; i < spec->paths.used; i++) {
128 struct nid_path *path = snd_array_elem(&spec->paths, i);
129 if (path->depth <= 0)
131 if ((!from_nid || path->path[0] == from_nid) &&
132 (!to_nid || path->path[path->depth - 1] == to_nid))
137 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
139 /* check whether the given DAC is already found in any existing paths */
140 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
142 struct hda_gen_spec *spec = codec->spec;
145 for (i = 0; i < spec->paths.used; i++) {
146 struct nid_path *path = snd_array_elem(&spec->paths, i);
147 if (path->path[0] == nid)
153 /* check whether the given two widgets can be connected */
154 static bool is_reachable_path(struct hda_codec *codec,
155 hda_nid_t from_nid, hda_nid_t to_nid)
157 if (!from_nid || !to_nid)
159 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
162 /* nid, dir and idx */
163 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
165 /* check whether the given ctl is already assigned in any path elements */
166 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
168 struct hda_gen_spec *spec = codec->spec;
171 val &= AMP_VAL_COMPARE_MASK;
172 for (i = 0; i < spec->paths.used; i++) {
173 struct nid_path *path = snd_array_elem(&spec->paths, i);
174 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
180 /* check whether a control with the given (nid, dir, idx) was assigned */
181 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
184 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
185 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
186 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
189 /* called recursively */
190 static bool __parse_nid_path(struct hda_codec *codec,
191 hda_nid_t from_nid, hda_nid_t to_nid,
192 int with_aa_mix, struct nid_path *path, int depth)
194 struct hda_gen_spec *spec = codec->spec;
198 if (to_nid == spec->mixer_nid) {
201 with_aa_mix = 2; /* mark aa-mix is included */
204 nums = snd_hda_get_connections(codec, to_nid, conn, ARRAY_SIZE(conn));
205 for (i = 0; i < nums; i++) {
206 if (conn[i] != from_nid) {
207 /* special case: when from_nid is 0,
208 * try to find an empty DAC
211 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
212 is_dac_already_used(codec, conn[i]))
215 /* aa-mix is requested but not included? */
216 if (!(spec->mixer_nid && with_aa_mix == 1))
219 if (depth >= MAX_NID_PATH_DEPTH)
221 for (i = 0; i < nums; i++) {
223 type = get_wcaps_type(get_wcaps(codec, conn[i]));
224 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
227 if (__parse_nid_path(codec, from_nid, conn[i],
228 with_aa_mix, path, depth + 1))
234 path->path[path->depth] = conn[i];
235 path->idx[path->depth + 1] = i;
236 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
237 path->multi[path->depth + 1] = 1;
242 /* parse the widget path from the given nid to the target nid;
243 * when @from_nid is 0, try to find an empty DAC;
244 * when @with_aa_mix is 0, paths with spec->mixer_nid are excluded.
245 * when @with_aa_mix is 1, paths without spec->mixer_nid are excluded.
246 * when @with_aa_mix is 2, no special handling about spec->mixer_nid.
248 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
249 hda_nid_t to_nid, int with_aa_mix,
250 struct nid_path *path)
252 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
253 path->path[path->depth] = to_nid;
256 snd_printdd("path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
257 path->depth, path->path[0], path->path[1],
258 path->path[2], path->path[3], path->path[4]);
264 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
267 * parse the path between the given NIDs and add to the path list.
268 * if no valid path is found, return NULL
271 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
272 hda_nid_t to_nid, int with_aa_mix)
274 struct hda_gen_spec *spec = codec->spec;
275 struct nid_path *path;
277 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
280 path = snd_array_new(&spec->paths);
283 memset(path, 0, sizeof(*path));
284 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
290 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
292 /* look for an empty DAC slot */
293 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
296 struct hda_gen_spec *spec = codec->spec;
300 for (i = 0; i < spec->num_all_dacs; i++) {
301 hda_nid_t nid = spec->all_dacs[i];
302 if (!nid || is_dac_already_used(codec, nid))
304 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
305 if (is_digital != cap_digital)
307 if (is_reachable_path(codec, nid, pin))
313 /* replace the channels in the composed amp value with the given number */
314 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
316 val &= ~(0x3U << 16);
321 /* check whether the widget has the given amp capability for the direction */
322 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
323 int dir, unsigned int bits)
327 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
328 if (query_amp_caps(codec, nid, dir) & bits)
333 #define nid_has_mute(codec, nid, dir) \
334 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
335 #define nid_has_volume(codec, nid, dir) \
336 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
338 /* look for a widget suitable for assigning a mute switch in the path */
339 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
340 struct nid_path *path)
344 for (i = path->depth - 1; i >= 0; i--) {
345 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
346 return path->path[i];
347 if (i != path->depth - 1 && i != 0 &&
348 nid_has_mute(codec, path->path[i], HDA_INPUT))
349 return path->path[i];
354 /* look for a widget suitable for assigning a volume ctl in the path */
355 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
356 struct nid_path *path)
360 for (i = path->depth - 1; i >= 0; i--) {
361 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
362 return path->path[i];
368 * path activation / deactivation
371 /* can have the amp-in capability? */
372 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
374 hda_nid_t nid = path->path[idx];
375 unsigned int caps = get_wcaps(codec, nid);
376 unsigned int type = get_wcaps_type(caps);
378 if (!(caps & AC_WCAP_IN_AMP))
380 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
385 /* can have the amp-out capability? */
386 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
388 hda_nid_t nid = path->path[idx];
389 unsigned int caps = get_wcaps(codec, nid);
390 unsigned int type = get_wcaps_type(caps);
392 if (!(caps & AC_WCAP_OUT_AMP))
394 if (type == AC_WID_PIN && !idx) /* only for output pins */
399 /* check whether the given (nid,dir,idx) is active */
400 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
401 unsigned int idx, unsigned int dir)
403 struct hda_gen_spec *spec = codec->spec;
406 for (n = 0; n < spec->paths.used; n++) {
407 struct nid_path *path = snd_array_elem(&spec->paths, n);
410 for (i = 0; i < path->depth; i++) {
411 if (path->path[i] == nid) {
412 if (dir == HDA_OUTPUT || path->idx[i] == idx)
421 /* get the default amp value for the target state */
422 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
423 int dir, bool enable)
426 unsigned int val = 0;
428 caps = query_amp_caps(codec, nid, dir);
429 if (caps & AC_AMPCAP_NUM_STEPS) {
432 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
434 if (caps & AC_AMPCAP_MUTE) {
441 /* initialize the amp value (only at the first time) */
442 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
444 int val = get_amp_val_to_activate(codec, nid, dir, false);
445 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
448 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
449 int idx, bool enable)
452 if (is_ctl_associated(codec, nid, dir, idx) ||
453 is_active_nid(codec, nid, dir, idx))
455 val = get_amp_val_to_activate(codec, nid, dir, enable);
456 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
459 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
462 hda_nid_t nid = path->path[i];
463 init_amp(codec, nid, HDA_OUTPUT, 0);
464 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
467 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
468 int i, bool enable, bool add_aamix)
470 struct hda_gen_spec *spec = codec->spec;
474 hda_nid_t nid = path->path[i];
476 nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
477 type = get_wcaps_type(get_wcaps(codec, nid));
478 if (type == AC_WID_PIN ||
479 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
485 for (n = 0; n < nums; n++)
486 init_amp(codec, nid, HDA_INPUT, n);
488 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
491 /* here is a little bit tricky in comparison with activate_amp_out();
492 * when aa-mixer is available, we need to enable the path as well
494 for (n = 0; n < nums; n++) {
495 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
497 activate_amp(codec, nid, HDA_INPUT, n, enable);
501 /* activate or deactivate the given path
502 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
504 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
505 bool enable, bool add_aamix)
510 path->active = false;
512 for (i = path->depth - 1; i >= 0; i--) {
513 if (enable && path->multi[i])
514 snd_hda_codec_write_cache(codec, path->path[i], 0,
515 AC_VERB_SET_CONNECT_SEL,
517 if (has_amp_in(codec, path, i))
518 activate_amp_in(codec, path, i, enable, add_aamix);
519 if (has_amp_out(codec, path, i))
520 activate_amp_out(codec, path, i, enable);
526 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
528 /* turn on/off EAPD on the given pin */
529 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
531 struct hda_gen_spec *spec = codec->spec;
532 if (spec->own_eapd_ctl ||
533 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
535 snd_hda_codec_update_cache(codec, pin, 0,
536 AC_VERB_SET_EAPD_BTLENABLE,
537 enable ? 0x02 : 0x00);
542 * Helper functions for creating mixer ctl elements
552 static const struct snd_kcontrol_new control_templates[] = {
553 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
554 HDA_CODEC_MUTE(NULL, 0, 0, 0),
555 HDA_BIND_MUTE(NULL, 0, 0, 0),
556 HDA_BIND_VOL(NULL, 0),
557 HDA_BIND_SW(NULL, 0),
560 /* add dynamic controls from template */
561 static int add_control(struct hda_gen_spec *spec, int type, const char *name,
562 int cidx, unsigned long val)
564 struct snd_kcontrol_new *knew;
566 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
570 if (get_amp_nid_(val))
571 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
572 knew->private_value = val;
576 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
577 const char *pfx, const char *dir,
578 const char *sfx, int cidx, unsigned long val)
581 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
582 return add_control(spec, type, name, cidx, val);
585 #define add_pb_vol_ctrl(spec, type, pfx, val) \
586 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
587 #define add_pb_sw_ctrl(spec, type, pfx, val) \
588 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
589 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
590 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
591 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
592 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
594 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
595 unsigned int chs, struct nid_path *path)
600 val = path->ctls[NID_PATH_VOL_CTL];
603 val = amp_val_replace_channels(val, chs);
604 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
607 /* return the channel bits suitable for the given path->ctls[] */
608 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
611 int chs = 1; /* mono (left only) */
613 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
614 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
615 chs = 3; /* stereo */
620 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
621 struct nid_path *path)
623 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
624 return add_vol_ctl(codec, pfx, cidx, chs, path);
627 /* create a mute-switch for the given mixer widget;
628 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
630 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
631 unsigned int chs, struct nid_path *path)
634 int type = HDA_CTL_WIDGET_MUTE;
638 val = path->ctls[NID_PATH_MUTE_CTL];
641 val = amp_val_replace_channels(val, chs);
642 if (get_amp_direction_(val) == HDA_INPUT) {
643 hda_nid_t nid = get_amp_nid_(val);
644 int nums = snd_hda_get_num_conns(codec, nid);
646 type = HDA_CTL_BIND_MUTE;
650 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
653 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
654 int cidx, struct nid_path *path)
656 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
657 return add_sw_ctl(codec, pfx, cidx, chs, path);
660 static const char * const channel_name[4] = {
661 "Front", "Surround", "CLFE", "Side"
664 /* give some appropriate ctl name prefix for the given line out channel */
665 static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
666 bool can_be_master, int *index)
668 struct auto_pin_cfg *cfg = &spec->autocfg;
671 if (cfg->line_outs == 1 && !spec->multi_ios &&
672 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
673 return spec->vmaster_mute.hook ? "PCM" : "Master";
675 /* if there is really a single DAC used in the whole output paths,
676 * use it master (or "PCM" if a vmaster hook is present)
678 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
679 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
680 return spec->vmaster_mute.hook ? "PCM" : "Master";
682 switch (cfg->line_out_type) {
683 case AUTO_PIN_SPEAKER_OUT:
684 if (cfg->line_outs == 1)
686 if (cfg->line_outs == 2)
687 return ch ? "Bass Speaker" : "Speaker";
689 case AUTO_PIN_HP_OUT:
690 /* for multi-io case, only the primary out */
691 if (ch && spec->multi_ios)
696 if (cfg->line_outs == 1 && !spec->multi_ios)
700 if (ch >= ARRAY_SIZE(channel_name)) {
705 return channel_name[ch];
712 /* badness definition */
714 /* No primary DAC is found for the main output */
715 BAD_NO_PRIMARY_DAC = 0x10000,
716 /* No DAC is found for the extra output */
718 /* No possible multi-ios */
719 BAD_MULTI_IO = 0x103,
720 /* No individual DAC for extra output */
721 BAD_NO_EXTRA_DAC = 0x102,
722 /* No individual DAC for extra surrounds */
723 BAD_NO_EXTRA_SURR_DAC = 0x101,
724 /* Primary DAC shared with main surrounds */
725 BAD_SHARED_SURROUND = 0x100,
726 /* Primary DAC shared with main CLFE */
727 BAD_SHARED_CLFE = 0x10,
728 /* Primary DAC shared with extra surrounds */
729 BAD_SHARED_EXTRA_SURROUND = 0x10,
730 /* Volume widget is shared */
731 BAD_SHARED_VOL = 0x10,
734 /* look for widgets in the path between the given NIDs appropriate for
735 * volume and mute controls, and assign the values to ctls[].
737 * When no appropriate widget is found in the path, the badness value
738 * is incremented depending on the situation. The function returns the
739 * total badness for both volume and mute controls.
741 static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
744 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
750 return BAD_SHARED_VOL * 2;
751 nid = look_for_out_vol_nid(codec, path);
753 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
754 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
755 badness += BAD_SHARED_VOL;
757 path->ctls[NID_PATH_VOL_CTL] = val;
759 badness += BAD_SHARED_VOL;
760 nid = look_for_out_mute_nid(codec, path);
762 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
763 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
764 nid_has_mute(codec, nid, HDA_OUTPUT))
765 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
767 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
768 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
769 badness += BAD_SHARED_VOL;
771 path->ctls[NID_PATH_MUTE_CTL] = val;
773 badness += BAD_SHARED_VOL;
777 struct badness_table {
778 int no_primary_dac; /* no primary DAC */
779 int no_dac; /* no secondary DACs */
780 int shared_primary; /* primary DAC is shared with main output */
781 int shared_surr; /* secondary DAC shared with main or primary */
782 int shared_clfe; /* third DAC shared with main or primary */
783 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
786 static struct badness_table main_out_badness = {
787 .no_primary_dac = BAD_NO_PRIMARY_DAC,
788 .no_dac = BAD_NO_DAC,
789 .shared_primary = BAD_NO_PRIMARY_DAC,
790 .shared_surr = BAD_SHARED_SURROUND,
791 .shared_clfe = BAD_SHARED_CLFE,
792 .shared_surr_main = BAD_SHARED_SURROUND,
795 static struct badness_table extra_out_badness = {
796 .no_primary_dac = BAD_NO_DAC,
797 .no_dac = BAD_NO_DAC,
798 .shared_primary = BAD_NO_EXTRA_DAC,
799 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
800 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
801 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
804 /* try to assign DACs to pins and return the resultant badness */
805 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
806 const hda_nid_t *pins, hda_nid_t *dacs,
807 const struct badness_table *bad)
809 struct hda_gen_spec *spec = codec->spec;
810 struct auto_pin_cfg *cfg = &spec->autocfg;
818 for (i = 0; i < num_outs; i++) {
819 hda_nid_t pin = pins[i];
821 dacs[i] = look_for_dac(codec, pin, false);
822 if (!dacs[i] && !i) {
823 for (j = 1; j < num_outs; j++) {
824 if (is_reachable_path(codec, dacs[j], pin)) {
833 if (is_reachable_path(codec, dacs[0], pin))
835 else if (cfg->line_outs > i &&
836 is_reachable_path(codec, spec->private_dac_nids[i], pin))
837 dac = spec->private_dac_nids[i];
840 badness += bad->shared_primary;
842 badness += bad->shared_surr;
844 badness += bad->shared_clfe;
845 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
846 dac = spec->private_dac_nids[0];
847 badness += bad->shared_surr_main;
849 badness += bad->no_primary_dac;
851 badness += bad->no_dac;
853 if (!snd_hda_add_new_path(codec, dac, pin, 0))
856 badness += assign_out_path_ctls(codec, pin, dac);
862 /* return NID if the given pin has only a single connection to a certain DAC */
863 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
865 struct hda_gen_spec *spec = codec->spec;
867 hda_nid_t nid_found = 0;
869 for (i = 0; i < spec->num_all_dacs; i++) {
870 hda_nid_t nid = spec->all_dacs[i];
871 if (!nid || is_dac_already_used(codec, nid))
873 if (is_reachable_path(codec, nid, pin)) {
882 /* check whether the given pin can be a multi-io pin */
883 static bool can_be_multiio_pin(struct hda_codec *codec,
884 unsigned int location, hda_nid_t nid)
886 unsigned int defcfg, caps;
888 defcfg = snd_hda_codec_get_pincfg(codec, nid);
889 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
891 if (location && get_defcfg_location(defcfg) != location)
893 caps = snd_hda_query_pin_caps(codec, nid);
894 if (!(caps & AC_PINCAP_OUT))
902 * When hardwired is set, try to fill ony hardwired pins, and returns
903 * zero if any pins are filled, non-zero if nothing found.
904 * When hardwired is off, try to fill possible input pins, and returns
907 static int fill_multi_ios(struct hda_codec *codec,
908 hda_nid_t reference_pin,
909 bool hardwired, int offset)
911 struct hda_gen_spec *spec = codec->spec;
912 struct auto_pin_cfg *cfg = &spec->autocfg;
913 int type, i, j, dacs, num_pins, old_pins;
914 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
915 unsigned int location = get_defcfg_location(defcfg);
918 old_pins = spec->multi_ios;
923 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
924 for (i = 0; i < cfg->num_inputs; i++) {
925 if (cfg->inputs[i].type != type)
927 if (can_be_multiio_pin(codec, location,
935 dacs = spec->multiout.num_dacs;
936 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
937 for (i = 0; i < cfg->num_inputs; i++) {
938 hda_nid_t nid = cfg->inputs[i].pin;
941 if (cfg->inputs[i].type != type)
943 if (!can_be_multiio_pin(codec, location, nid))
945 for (j = 0; j < spec->multi_ios; j++) {
946 if (nid == spec->multi_io[j].pin)
949 if (j < spec->multi_ios)
952 if (offset && offset + spec->multi_ios < dacs) {
953 dac = spec->private_dac_nids[offset + spec->multi_ios];
954 if (!is_reachable_path(codec, dac, nid))
958 dac = get_dac_if_single(codec, nid);
960 dac = look_for_dac(codec, nid, false);
965 if (!snd_hda_add_new_path(codec, dac, nid, 0)) {
969 spec->multi_io[spec->multi_ios].pin = nid;
970 spec->multi_io[spec->multi_ios].dac = dac;
972 if (spec->multi_ios >= 2)
978 badness = BAD_MULTI_IO;
979 if (old_pins == spec->multi_ios) {
981 return 1; /* nothing found */
983 return badness; /* no badness if nothing found */
985 if (!hardwired && spec->multi_ios < 2) {
986 /* cancel newly assigned paths */
987 spec->paths.used -= spec->multi_ios - old_pins;
988 spec->multi_ios = old_pins;
992 /* assign volume and mute controls */
993 for (i = old_pins; i < spec->multi_ios; i++)
994 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
995 spec->multi_io[i].dac);
1000 /* map DACs for all pins in the list if they are single connections */
1001 static bool map_singles(struct hda_codec *codec, int outs,
1002 const hda_nid_t *pins, hda_nid_t *dacs)
1006 for (i = 0; i < outs; i++) {
1010 dac = get_dac_if_single(codec, pins[i]);
1013 if (snd_hda_add_new_path(codec, dac, pins[i], 0)) {
1021 /* fill in the dac_nids table from the parsed pin configuration */
1022 static int fill_and_eval_dacs(struct hda_codec *codec,
1023 bool fill_hardwired,
1024 bool fill_mio_first)
1026 struct hda_gen_spec *spec = codec->spec;
1027 struct auto_pin_cfg *cfg = &spec->autocfg;
1028 int i, err, badness;
1030 /* set num_dacs once to full for look_for_dac() */
1031 spec->multiout.num_dacs = cfg->line_outs;
1032 spec->multiout.dac_nids = spec->private_dac_nids;
1033 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1034 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1035 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1036 spec->multi_ios = 0;
1037 snd_array_free(&spec->paths);
1040 /* fill hard-wired DACs first */
1041 if (fill_hardwired) {
1044 mapped = map_singles(codec, cfg->line_outs,
1046 spec->private_dac_nids);
1047 mapped |= map_singles(codec, cfg->hp_outs,
1049 spec->multiout.hp_out_nid);
1050 mapped |= map_singles(codec, cfg->speaker_outs,
1052 spec->multiout.extra_out_nid);
1053 if (fill_mio_first && cfg->line_outs == 1 &&
1054 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1055 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1062 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1063 spec->private_dac_nids,
1066 /* re-count num_dacs and squash invalid entries */
1067 spec->multiout.num_dacs = 0;
1068 for (i = 0; i < cfg->line_outs; i++) {
1069 if (spec->private_dac_nids[i])
1070 spec->multiout.num_dacs++;
1072 memmove(spec->private_dac_nids + i,
1073 spec->private_dac_nids + i + 1,
1074 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1075 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1079 if (fill_mio_first &&
1080 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1081 /* try to fill multi-io first */
1082 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1085 /* we don't count badness at this stage yet */
1088 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1089 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1090 spec->multiout.hp_out_nid,
1091 &extra_out_badness);
1096 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1097 err = try_assign_dacs(codec, cfg->speaker_outs,
1099 spec->multiout.extra_out_nid,
1100 &extra_out_badness);
1105 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1106 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1111 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1112 /* try multi-ios with HP + inputs */
1114 if (cfg->line_outs >= 3)
1116 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1122 if (spec->multi_ios == 2) {
1123 for (i = 0; i < 2; i++)
1124 spec->private_dac_nids[spec->multiout.num_dacs++] =
1125 spec->multi_io[i].dac;
1126 spec->ext_channel_count = 2;
1127 } else if (spec->multi_ios) {
1128 spec->multi_ios = 0;
1129 badness += BAD_MULTI_IO;
1135 #define DEBUG_BADNESS
1137 #ifdef DEBUG_BADNESS
1138 #define debug_badness snd_printdd
1140 #define debug_badness(...)
1143 static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1145 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1146 cfg->line_out_pins[0], cfg->line_out_pins[1],
1147 cfg->line_out_pins[2], cfg->line_out_pins[2],
1148 spec->multiout.dac_nids[0],
1149 spec->multiout.dac_nids[1],
1150 spec->multiout.dac_nids[2],
1151 spec->multiout.dac_nids[3]);
1152 if (spec->multi_ios > 0)
1153 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1155 spec->multi_io[0].pin, spec->multi_io[1].pin,
1156 spec->multi_io[0].dac, spec->multi_io[1].dac);
1157 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1158 cfg->hp_pins[0], cfg->hp_pins[1],
1159 cfg->hp_pins[2], cfg->hp_pins[2],
1160 spec->multiout.hp_out_nid[0],
1161 spec->multiout.hp_out_nid[1],
1162 spec->multiout.hp_out_nid[2],
1163 spec->multiout.hp_out_nid[3]);
1164 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1165 cfg->speaker_pins[0], cfg->speaker_pins[1],
1166 cfg->speaker_pins[2], cfg->speaker_pins[3],
1167 spec->multiout.extra_out_nid[0],
1168 spec->multiout.extra_out_nid[1],
1169 spec->multiout.extra_out_nid[2],
1170 spec->multiout.extra_out_nid[3]);
1173 /* find all available DACs of the codec */
1174 static void fill_all_dac_nids(struct hda_codec *codec)
1176 struct hda_gen_spec *spec = codec->spec;
1178 hda_nid_t nid = codec->start_nid;
1180 spec->num_all_dacs = 0;
1181 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1182 for (i = 0; i < codec->num_nodes; i++, nid++) {
1183 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1185 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1186 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1189 spec->all_dacs[spec->num_all_dacs++] = nid;
1193 static int parse_output_paths(struct hda_codec *codec)
1195 struct hda_gen_spec *spec = codec->spec;
1196 struct auto_pin_cfg *cfg = &spec->autocfg;
1197 struct auto_pin_cfg *best_cfg;
1198 int best_badness = INT_MAX;
1200 bool fill_hardwired = true, fill_mio_first = true;
1201 bool best_wired = true, best_mio = true;
1202 bool hp_spk_swapped = false;
1204 fill_all_dac_nids(codec);
1206 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1212 badness = fill_and_eval_dacs(codec, fill_hardwired,
1218 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1219 cfg->line_out_type, fill_hardwired, fill_mio_first,
1221 debug_show_configs(spec, cfg);
1222 if (badness < best_badness) {
1223 best_badness = badness;
1225 best_wired = fill_hardwired;
1226 best_mio = fill_mio_first;
1230 fill_mio_first = !fill_mio_first;
1231 if (!fill_mio_first)
1233 fill_hardwired = !fill_hardwired;
1234 if (!fill_hardwired)
1238 hp_spk_swapped = true;
1239 if (cfg->speaker_outs > 0 &&
1240 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1241 cfg->hp_outs = cfg->line_outs;
1242 memcpy(cfg->hp_pins, cfg->line_out_pins,
1243 sizeof(cfg->hp_pins));
1244 cfg->line_outs = cfg->speaker_outs;
1245 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1246 sizeof(cfg->speaker_pins));
1247 cfg->speaker_outs = 0;
1248 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1249 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1250 fill_hardwired = true;
1253 if (cfg->hp_outs > 0 &&
1254 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1255 cfg->speaker_outs = cfg->line_outs;
1256 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1257 sizeof(cfg->speaker_pins));
1258 cfg->line_outs = cfg->hp_outs;
1259 memcpy(cfg->line_out_pins, cfg->hp_pins,
1260 sizeof(cfg->hp_pins));
1262 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1263 cfg->line_out_type = AUTO_PIN_HP_OUT;
1264 fill_hardwired = true;
1272 fill_and_eval_dacs(codec, best_wired, best_mio);
1274 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1275 cfg->line_out_type, best_wired, best_mio);
1276 debug_show_configs(spec, cfg);
1278 if (cfg->line_out_pins[0]) {
1279 struct nid_path *path;
1280 path = snd_hda_get_nid_path(codec,
1281 spec->multiout.dac_nids[0],
1282 cfg->line_out_pins[0]);
1284 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1291 /* add playback controls from the parsed DAC table */
1292 static int create_multi_out_ctls(struct hda_codec *codec,
1293 const struct auto_pin_cfg *cfg)
1295 struct hda_gen_spec *spec = codec->spec;
1296 int i, err, noutputs;
1298 noutputs = cfg->line_outs;
1299 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1300 noutputs += spec->multi_ios;
1302 for (i = 0; i < noutputs; i++) {
1306 struct nid_path *path;
1308 dac = spec->multiout.dac_nids[i];
1311 if (i >= cfg->line_outs) {
1312 pin = spec->multi_io[i - 1].pin;
1314 name = channel_name[i];
1316 pin = cfg->line_out_pins[i];
1317 name = get_line_out_pfx(spec, i, true, &index);
1320 path = snd_hda_get_nid_path(codec, dac, pin);
1323 if (!name || !strcmp(name, "CLFE")) {
1325 err = add_vol_ctl(codec, "Center", 0, 1, path);
1328 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1331 err = add_sw_ctl(codec, "Center", 0, 1, path);
1334 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1338 err = add_stereo_vol(codec, name, index, path);
1341 err = add_stereo_sw(codec, name, index, path);
1349 static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1350 hda_nid_t dac, const char *pfx, int cidx)
1352 struct nid_path *path;
1355 path = snd_hda_get_nid_path(codec, dac, pin);
1358 /* bind volume control will be created in the case of dac = 0 */
1360 err = add_stereo_vol(codec, pfx, cidx, path);
1364 err = add_stereo_sw(codec, pfx, cidx, path);
1370 /* add playback controls for speaker and HP outputs */
1371 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1372 const hda_nid_t *pins, const hda_nid_t *dacs,
1375 struct hda_gen_spec *spec = codec->spec;
1376 struct hda_bind_ctls *ctl;
1380 if (!num_pins || !pins[0])
1383 if (num_pins == 1) {
1384 hda_nid_t dac = *dacs;
1386 dac = spec->multiout.dac_nids[0];
1387 return create_extra_out(codec, *pins, dac, pfx, 0);
1390 for (i = 0; i < num_pins; i++) {
1392 if (dacs[num_pins - 1])
1393 dac = dacs[i]; /* with individual volumes */
1396 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1397 err = create_extra_out(codec, pins[i], dac,
1399 } else if (num_pins >= 3) {
1400 snprintf(name, sizeof(name), "%s %s",
1401 pfx, channel_name[i]);
1402 err = create_extra_out(codec, pins[i], dac, name, 0);
1404 err = create_extra_out(codec, pins[i], dac, pfx, i);
1409 if (dacs[num_pins - 1])
1412 /* Let's create a bind-controls for volumes */
1413 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1417 for (i = 0; i < num_pins; i++) {
1419 struct nid_path *path;
1420 if (!pins[i] || !dacs[i])
1422 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1425 vol = look_for_out_vol_nid(codec, path);
1428 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1431 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1432 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1439 static int create_hp_out_ctls(struct hda_codec *codec)
1441 struct hda_gen_spec *spec = codec->spec;
1442 return create_extra_outs(codec, spec->autocfg.hp_outs,
1443 spec->autocfg.hp_pins,
1444 spec->multiout.hp_out_nid,
1448 static int create_speaker_out_ctls(struct hda_codec *codec)
1450 struct hda_gen_spec *spec = codec->spec;
1451 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1452 spec->autocfg.speaker_pins,
1453 spec->multiout.extra_out_nid,
1458 * channel mode enum control
1461 static int ch_mode_info(struct snd_kcontrol *kcontrol,
1462 struct snd_ctl_elem_info *uinfo)
1464 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1465 struct hda_gen_spec *spec = codec->spec;
1467 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1469 uinfo->value.enumerated.items = spec->multi_ios + 1;
1470 if (uinfo->value.enumerated.item > spec->multi_ios)
1471 uinfo->value.enumerated.item = spec->multi_ios;
1472 sprintf(uinfo->value.enumerated.name, "%dch",
1473 (uinfo->value.enumerated.item + 1) * 2);
1477 static int ch_mode_get(struct snd_kcontrol *kcontrol,
1478 struct snd_ctl_elem_value *ucontrol)
1480 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1481 struct hda_gen_spec *spec = codec->spec;
1482 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1486 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1488 struct hda_gen_spec *spec = codec->spec;
1489 hda_nid_t nid = spec->multi_io[idx].pin;
1490 struct nid_path *path;
1492 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1496 if (path->active == output)
1500 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1501 snd_hda_activate_path(codec, path, true, true);
1502 set_pin_eapd(codec, nid, true);
1504 set_pin_eapd(codec, nid, false);
1505 snd_hda_activate_path(codec, path, false, true);
1506 snd_hda_set_pin_ctl_cache(codec, nid,
1507 spec->multi_io[idx].ctl_in);
1512 static int ch_mode_put(struct snd_kcontrol *kcontrol,
1513 struct snd_ctl_elem_value *ucontrol)
1515 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1516 struct hda_gen_spec *spec = codec->spec;
1519 ch = ucontrol->value.enumerated.item[0];
1520 if (ch < 0 || ch > spec->multi_ios)
1522 if (ch == (spec->ext_channel_count - 1) / 2)
1524 spec->ext_channel_count = (ch + 1) * 2;
1525 for (i = 0; i < spec->multi_ios; i++)
1526 set_multi_io(codec, i, i < ch);
1527 spec->multiout.max_channels = max(spec->ext_channel_count,
1528 spec->const_channel_count);
1529 if (spec->need_dac_fix)
1530 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1534 static const struct snd_kcontrol_new channel_mode_enum = {
1535 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1536 .name = "Channel Mode",
1537 .info = ch_mode_info,
1542 static int create_multi_channel_mode(struct hda_codec *codec)
1544 struct hda_gen_spec *spec = codec->spec;
1546 if (spec->multi_ios > 0) {
1547 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
1554 * shared headphone/mic handling
1557 static void call_update_outputs(struct hda_codec *codec);
1559 /* for shared I/O, change the pin-control accordingly */
1560 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1562 struct hda_gen_spec *spec = codec->spec;
1564 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1565 /* NOTE: this assumes that there are only two inputs, the
1566 * first is the real internal mic and the second is HP/mic jack.
1569 val = snd_hda_get_default_vref(codec, pin);
1571 /* This pin does not have vref caps - let's enable vref on pin 0x18
1572 instead, as suggested by Realtek */
1573 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1574 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1575 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1576 if (vref_val != AC_PINCTL_VREF_HIZ)
1577 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1578 PIN_IN | (set_as_mic ? vref_val : 0));
1581 val = set_as_mic ? val | PIN_IN : PIN_HP;
1582 snd_hda_set_pin_ctl_cache(codec, pin, val);
1584 spec->automute_speaker = !set_as_mic;
1585 call_update_outputs(codec);
1588 /* create a shared input with the headphone out */
1589 static int create_shared_input(struct hda_codec *codec)
1591 struct hda_gen_spec *spec = codec->spec;
1592 struct auto_pin_cfg *cfg = &spec->autocfg;
1593 unsigned int defcfg;
1596 /* only one internal input pin? */
1597 if (cfg->num_inputs != 1)
1599 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1600 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1603 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1604 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1605 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1606 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1608 return 0; /* both not available */
1610 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1611 return 0; /* no input */
1613 cfg->inputs[1].pin = nid;
1614 cfg->inputs[1].type = AUTO_PIN_MIC;
1615 cfg->num_inputs = 2;
1616 spec->shared_mic_hp = 1;
1617 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1627 /* add the powersave loopback-list entry */
1628 static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1630 struct hda_amp_list *list;
1632 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1634 list = spec->loopback_list + spec->num_loopbacks;
1636 list->dir = HDA_INPUT;
1638 spec->num_loopbacks++;
1639 spec->loopback.amplist = spec->loopback_list;
1642 #define add_loopback_list(spec, mix, idx) /* NOP */
1645 /* create input playback/capture controls for the given pin */
1646 static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1647 const char *ctlname, int ctlidx,
1650 struct hda_gen_spec *spec = codec->spec;
1651 struct nid_path *path;
1655 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1656 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1657 return 0; /* no need for analog loopback */
1659 path = snd_hda_add_new_path(codec, pin, mix_nid, 2);
1663 idx = path->idx[path->depth - 1];
1664 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1665 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1666 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
1669 path->ctls[NID_PATH_VOL_CTL] = val;
1672 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1673 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1674 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
1677 path->ctls[NID_PATH_MUTE_CTL] = val;
1680 path->active = true;
1681 add_loopback_list(spec, mix_nid, idx);
1685 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
1687 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1688 return (pincap & AC_PINCAP_IN) != 0;
1691 /* Parse the codec tree and retrieve ADCs */
1692 static int fill_adc_nids(struct hda_codec *codec)
1694 struct hda_gen_spec *spec = codec->spec;
1696 hda_nid_t *adc_nids = spec->adc_nids;
1697 int max_nums = ARRAY_SIZE(spec->adc_nids);
1700 nid = codec->start_nid;
1701 for (i = 0; i < codec->num_nodes; i++, nid++) {
1702 unsigned int caps = get_wcaps(codec, nid);
1703 int type = get_wcaps_type(caps);
1705 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1707 adc_nids[nums] = nid;
1708 if (++nums >= max_nums)
1711 spec->num_adc_nids = nums;
1715 /* filter out invalid adc_nids that don't give all active input pins;
1716 * if needed, check whether dynamic ADC-switching is available
1718 static int check_dyn_adc_switch(struct hda_codec *codec)
1720 struct hda_gen_spec *spec = codec->spec;
1721 struct hda_input_mux *imux = &spec->input_mux;
1722 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1728 for (n = 0; n < spec->num_adc_nids; n++) {
1729 adc = spec->adc_nids[n];
1730 for (i = 0; i < imux->num_items; i++) {
1731 pin = spec->imux_pins[i];
1732 if (!is_reachable_path(codec, pin, adc))
1735 if (i >= imux->num_items)
1736 adc_nids[nums++] = adc;
1740 if (spec->shared_mic_hp) {
1741 spec->shared_mic_hp = 0;
1742 imux->num_items = 1;
1746 /* check whether ADC-switch is possible */
1747 for (i = 0; i < imux->num_items; i++) {
1748 pin = spec->imux_pins[i];
1749 for (n = 0; n < spec->num_adc_nids; n++) {
1750 adc = spec->adc_nids[n];
1751 if (is_reachable_path(codec, pin, adc)) {
1752 spec->dyn_adc_idx[i] = n;
1758 snd_printdd("hda-codec: enabling ADC switching\n");
1759 spec->dyn_adc_switch = 1;
1760 } else if (nums != spec->num_adc_nids) {
1761 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1762 spec->num_adc_nids = nums;
1765 if (imux->num_items == 1 || spec->shared_mic_hp) {
1766 snd_printdd("hda-codec: reducing to a single ADC\n");
1767 spec->num_adc_nids = 1; /* reduce to a single ADC */
1770 /* single index for individual volumes ctls */
1771 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1772 spec->num_adc_nids = 1;
1778 * create playback/capture controls for input pins
1780 static int create_input_ctls(struct hda_codec *codec)
1782 struct hda_gen_spec *spec = codec->spec;
1783 const struct auto_pin_cfg *cfg = &spec->autocfg;
1784 hda_nid_t mixer = spec->mixer_nid;
1785 struct hda_input_mux *imux = &spec->input_mux;
1787 int i, c, err, type_idx = 0;
1788 const char *prev_label = NULL;
1790 num_adcs = fill_adc_nids(codec);
1794 for (i = 0; i < cfg->num_inputs; i++) {
1799 pin = cfg->inputs[i].pin;
1800 if (!is_input_pin(codec, pin))
1803 label = hda_get_autocfg_input_label(codec, cfg, i);
1804 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1805 label = "Headphone Mic";
1806 if (prev_label && !strcmp(label, prev_label))
1813 if (is_reachable_path(codec, pin, mixer)) {
1814 err = new_analog_input(codec, pin,
1815 label, type_idx, mixer);
1822 for (c = 0; c < num_adcs; c++) {
1823 struct nid_path *path;
1824 hda_nid_t adc = spec->adc_nids[c];
1826 if (!is_reachable_path(codec, pin, adc))
1828 path = snd_array_new(&spec->paths);
1831 memset(path, 0, sizeof(*path));
1832 if (!snd_hda_parse_nid_path(codec, pin, adc, 2, path)) {
1834 "invalid input path 0x%x -> 0x%x\n",
1841 spec->imux_pins[imux->num_items] = pin;
1842 snd_hda_add_imux_item(imux, label,
1843 imux->num_items, NULL);
1857 /* get the ADC NID corresponding to the given index */
1858 static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1860 struct hda_gen_spec *spec = codec->spec;
1861 if (spec->dyn_adc_switch)
1862 adc_idx = spec->dyn_adc_idx[imux_idx];
1863 return spec->adc_nids[adc_idx];
1866 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
1869 static int mux_enum_info(struct snd_kcontrol *kcontrol,
1870 struct snd_ctl_elem_info *uinfo)
1872 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1873 struct hda_gen_spec *spec = codec->spec;
1874 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1877 static int mux_enum_get(struct snd_kcontrol *kcontrol,
1878 struct snd_ctl_elem_value *ucontrol)
1880 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1881 struct hda_gen_spec *spec = codec->spec;
1882 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1884 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1888 static int mux_enum_put(struct snd_kcontrol *kcontrol,
1889 struct snd_ctl_elem_value *ucontrol)
1891 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1892 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1893 return mux_select(codec, adc_idx,
1894 ucontrol->value.enumerated.item[0]);
1897 static const struct snd_kcontrol_new cap_src_temp = {
1898 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1899 .name = "Input Source",
1900 .info = mux_enum_info,
1901 .get = mux_enum_get,
1902 .put = mux_enum_put,
1906 * capture volume and capture switch ctls
1909 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
1910 struct snd_ctl_elem_value *ucontrol);
1912 /* call the given amp update function for all amps in the imux list at once */
1913 static int cap_put_caller(struct snd_kcontrol *kcontrol,
1914 struct snd_ctl_elem_value *ucontrol,
1915 put_call_t func, int type)
1917 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1918 struct hda_gen_spec *spec = codec->spec;
1919 const struct hda_input_mux *imux;
1920 struct nid_path *path;
1921 int i, adc_idx, err = 0;
1923 imux = &spec->input_mux;
1924 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1925 mutex_lock(&codec->control_mutex);
1926 /* we use the cache-only update at first since multiple input paths
1927 * may shared the same amp; by updating only caches, the redundant
1928 * writes to hardware can be reduced.
1930 codec->cached_write = 1;
1931 for (i = 0; i < imux->num_items; i++) {
1932 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
1933 get_adc_nid(codec, adc_idx, i));
1934 if (!path->ctls[type])
1936 kcontrol->private_value = path->ctls[type];
1937 err = func(kcontrol, ucontrol);
1942 codec->cached_write = 0;
1943 mutex_unlock(&codec->control_mutex);
1944 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
1945 if (err >= 0 && spec->cap_sync_hook)
1946 spec->cap_sync_hook(codec);
1950 /* capture volume ctl callbacks */
1951 #define cap_vol_info snd_hda_mixer_amp_volume_info
1952 #define cap_vol_get snd_hda_mixer_amp_volume_get
1953 #define cap_vol_tlv snd_hda_mixer_amp_tlv
1955 static int cap_vol_put(struct snd_kcontrol *kcontrol,
1956 struct snd_ctl_elem_value *ucontrol)
1958 return cap_put_caller(kcontrol, ucontrol,
1959 snd_hda_mixer_amp_volume_put,
1963 static const struct snd_kcontrol_new cap_vol_temp = {
1964 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1965 .name = "Capture Volume",
1966 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1967 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
1968 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
1969 .info = cap_vol_info,
1972 .tlv = { .c = cap_vol_tlv },
1975 /* capture switch ctl callbacks */
1976 #define cap_sw_info snd_ctl_boolean_stereo_info
1977 #define cap_sw_get snd_hda_mixer_amp_switch_get
1979 static int cap_sw_put(struct snd_kcontrol *kcontrol,
1980 struct snd_ctl_elem_value *ucontrol)
1982 return cap_put_caller(kcontrol, ucontrol,
1983 snd_hda_mixer_amp_switch_put,
1987 static const struct snd_kcontrol_new cap_sw_temp = {
1988 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1989 .name = "Capture Switch",
1990 .info = cap_sw_info,
1995 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2000 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2001 for (depth = 0; depth < 3; depth++) {
2002 if (depth >= path->depth)
2004 i = path->depth - depth - 1;
2005 nid = path->path[i];
2006 if (!path->ctls[NID_PATH_VOL_CTL]) {
2007 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2008 path->ctls[NID_PATH_VOL_CTL] =
2009 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2010 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2011 int idx = path->idx[i];
2012 if (!depth && codec->single_adc_amp)
2014 path->ctls[NID_PATH_VOL_CTL] =
2015 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2018 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2019 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2020 path->ctls[NID_PATH_MUTE_CTL] =
2021 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2022 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2023 int idx = path->idx[i];
2024 if (!depth && codec->single_adc_amp)
2026 path->ctls[NID_PATH_MUTE_CTL] =
2027 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2034 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2036 struct hda_gen_spec *spec = codec->spec;
2037 struct auto_pin_cfg *cfg = &spec->autocfg;
2041 if (!spec->inv_dmic_split)
2043 for (i = 0; i < cfg->num_inputs; i++) {
2044 if (cfg->inputs[i].pin != nid)
2046 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2048 val = snd_hda_codec_get_pincfg(codec, nid);
2049 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2054 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2055 int idx, bool is_switch, unsigned int ctl,
2058 struct hda_gen_spec *spec = codec->spec;
2060 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2061 const char *sfx = is_switch ? "Switch" : "Volume";
2062 unsigned int chs = inv_dmic ? 1 : 3;
2069 snprintf(tmpname, sizeof(tmpname),
2070 "%s Capture %s", label, sfx);
2072 snprintf(tmpname, sizeof(tmpname),
2074 err = add_control(spec, type, tmpname, idx,
2075 amp_val_replace_channels(ctl, chs));
2076 if (err < 0 || !inv_dmic)
2079 /* Make independent right kcontrol */
2081 snprintf(tmpname, sizeof(tmpname),
2082 "Inverted %s Capture %s", label, sfx);
2084 snprintf(tmpname, sizeof(tmpname),
2085 "Inverted Capture %s", sfx);
2086 return add_control(spec, type, tmpname, idx,
2087 amp_val_replace_channels(ctl, 2));
2090 /* create single (and simple) capture volume and switch controls */
2091 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2092 unsigned int vol_ctl, unsigned int sw_ctl,
2096 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2099 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2105 /* create bound capture volume and switch controls */
2106 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2107 unsigned int vol_ctl, unsigned int sw_ctl)
2109 struct hda_gen_spec *spec = codec->spec;
2110 struct snd_kcontrol_new *knew;
2113 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
2117 knew->private_value = vol_ctl;
2118 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2121 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
2125 knew->private_value = sw_ctl;
2126 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2131 /* return the vol ctl when used first in the imux list */
2132 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2134 struct hda_gen_spec *spec = codec->spec;
2135 struct nid_path *path;
2139 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2140 get_adc_nid(codec, 0, idx));
2143 ctl = path->ctls[type];
2146 for (i = 0; i < idx - 1; i++) {
2147 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2148 get_adc_nid(codec, 0, i));
2149 if (path && path->ctls[type] == ctl)
2155 /* create individual capture volume and switch controls per input */
2156 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2158 struct hda_gen_spec *spec = codec->spec;
2159 struct hda_input_mux *imux = &spec->input_mux;
2160 int i, err, type, type_idx = 0;
2161 const char *prev_label = NULL;
2163 for (i = 0; i < imux->num_items; i++) {
2166 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2167 if (prev_label && !strcmp(label, prev_label))
2172 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2174 for (type = 0; type < 2; type++) {
2175 err = add_single_cap_ctl(codec, label, type_idx, type,
2176 get_first_cap_ctl(codec, i, type),
2185 static int create_capture_mixers(struct hda_codec *codec)
2187 struct hda_gen_spec *spec = codec->spec;
2188 struct hda_input_mux *imux = &spec->input_mux;
2189 int i, n, nums, err;
2191 if (spec->dyn_adc_switch)
2194 nums = spec->num_adc_nids;
2196 if (!spec->auto_mic && imux->num_items > 1) {
2197 struct snd_kcontrol_new *knew;
2199 name = nums > 1 ? "Input Source" : "Capture Source";
2200 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
2206 for (n = 0; n < nums; n++) {
2208 bool inv_dmic = false;
2212 for (i = 0; i < imux->num_items; i++) {
2213 struct nid_path *path;
2214 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2215 get_adc_nid(codec, n, i));
2218 parse_capvol_in_path(codec, path);
2220 vol = path->ctls[NID_PATH_VOL_CTL];
2221 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2224 sw = path->ctls[NID_PATH_MUTE_CTL];
2225 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2227 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2232 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2234 else if (!spec->multi_cap_vol)
2235 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2237 err = create_multi_cap_vol_ctl(codec);
2246 * add mic boosts if needed
2248 static int parse_mic_boost(struct hda_codec *codec)
2250 struct hda_gen_spec *spec = codec->spec;
2251 struct auto_pin_cfg *cfg = &spec->autocfg;
2255 const char *prev_label = NULL;
2257 for (i = 0; i < cfg->num_inputs; i++) {
2258 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2260 nid = cfg->inputs[i].pin;
2261 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2263 char boost_label[32];
2264 struct nid_path *path;
2267 label = hda_get_autocfg_input_label(codec, cfg, i);
2268 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2269 label = "Headphone Mic";
2270 if (prev_label && !strcmp(label, prev_label))
2276 snprintf(boost_label, sizeof(boost_label),
2277 "%s Boost Volume", label);
2278 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2279 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2280 boost_label, type_idx, val);
2284 path = snd_hda_get_nid_path(codec, nid, 0);
2286 path->ctls[NID_PATH_BOOST_CTL] = val;
2293 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2295 static void parse_digital(struct hda_codec *codec)
2297 struct hda_gen_spec *spec = codec->spec;
2301 /* support multiple SPDIFs; the secondary is set up as a slave */
2303 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2304 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2305 dig_nid = look_for_dac(codec, pin, true);
2308 if (!snd_hda_add_new_path(codec, dig_nid, pin, 2))
2311 spec->multiout.dig_out_nid = dig_nid;
2312 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2314 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2315 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2317 spec->slave_dig_outs[nums - 1] = dig_nid;
2322 if (spec->autocfg.dig_in_pin) {
2323 dig_nid = codec->start_nid;
2324 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2325 struct nid_path *path;
2326 unsigned int wcaps = get_wcaps(codec, dig_nid);
2327 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2329 if (!(wcaps & AC_WCAP_DIGITAL))
2331 path = snd_hda_add_new_path(codec,
2332 spec->autocfg.dig_in_pin,
2335 path->active = true;
2336 spec->dig_in_nid = dig_nid;
2345 * input MUX handling
2348 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2350 /* select the given imux item; either unmute exclusively or select the route */
2351 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2354 struct hda_gen_spec *spec = codec->spec;
2355 const struct hda_input_mux *imux;
2356 struct nid_path *path;
2358 imux = &spec->input_mux;
2359 if (!imux->num_items)
2362 if (idx >= imux->num_items)
2363 idx = imux->num_items - 1;
2364 if (spec->cur_mux[adc_idx] == idx)
2367 path = snd_hda_get_nid_path(codec,
2368 spec->imux_pins[spec->cur_mux[adc_idx]],
2369 spec->adc_nids[adc_idx]);
2373 snd_hda_activate_path(codec, path, false, false);
2375 spec->cur_mux[adc_idx] = idx;
2377 if (spec->shared_mic_hp)
2378 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2380 if (spec->dyn_adc_switch)
2381 dyn_adc_pcm_resetup(codec, idx);
2383 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2384 get_adc_nid(codec, adc_idx, idx));
2389 snd_hda_activate_path(codec, path, true, false);
2390 if (spec->cap_sync_hook)
2391 spec->cap_sync_hook(codec);
2397 * Jack detections for HP auto-mute and mic-switch
2400 /* check each pin in the given array; returns true if any of them is plugged */
2401 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2405 for (i = 0; i < num_pins; i++) {
2406 hda_nid_t nid = pins[i];
2409 present |= snd_hda_jack_detect(codec, nid);
2414 /* standard HP/line-out auto-mute helper */
2415 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2416 bool mute, bool hp_out)
2418 struct hda_gen_spec *spec = codec->spec;
2419 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2422 for (i = 0; i < num_pins; i++) {
2423 hda_nid_t nid = pins[i];
2427 /* don't reset VREF value in case it's controlling
2428 * the amp (see alc861_fixup_asus_amp_vref_0f())
2430 if (spec->keep_vref_in_automute) {
2431 val = snd_hda_codec_read(codec, nid, 0,
2432 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2437 snd_hda_set_pin_ctl_cache(codec, nid, val);
2438 set_pin_eapd(codec, nid, !mute);
2442 /* Toggle outputs muting */
2443 void snd_hda_gen_update_outputs(struct hda_codec *codec)
2445 struct hda_gen_spec *spec = codec->spec;
2448 /* Control HP pins/amps depending on master_mute state;
2449 * in general, HP pins/amps control should be enabled in all cases,
2450 * but currently set only for master_mute, just to be safe
2452 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2453 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2454 spec->autocfg.hp_pins, spec->master_mute, true);
2456 if (!spec->automute_speaker)
2459 on = spec->hp_jack_present | spec->line_jack_present;
2460 on |= spec->master_mute;
2461 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2462 spec->autocfg.speaker_pins, on, false);
2464 /* toggle line-out mutes if needed, too */
2465 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2466 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2467 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2469 if (!spec->automute_lo)
2472 on = spec->hp_jack_present;
2473 on |= spec->master_mute;
2474 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2475 spec->autocfg.line_out_pins, on, false);
2477 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
2479 static void call_update_outputs(struct hda_codec *codec)
2481 struct hda_gen_spec *spec = codec->spec;
2482 if (spec->automute_hook)
2483 spec->automute_hook(codec);
2485 snd_hda_gen_update_outputs(codec);
2488 /* standard HP-automute helper */
2489 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2491 struct hda_gen_spec *spec = codec->spec;
2493 spec->hp_jack_present =
2494 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2495 spec->autocfg.hp_pins);
2496 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2498 call_update_outputs(codec);
2500 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
2502 /* standard line-out-automute helper */
2503 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2505 struct hda_gen_spec *spec = codec->spec;
2507 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2509 /* check LO jack only when it's different from HP */
2510 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2513 spec->line_jack_present =
2514 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2515 spec->autocfg.line_out_pins);
2516 if (!spec->automute_speaker || !spec->detect_lo)
2518 call_update_outputs(codec);
2520 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
2522 /* standard mic auto-switch helper */
2523 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
2525 struct hda_gen_spec *spec = codec->spec;
2528 if (!spec->auto_mic)
2531 for (i = spec->am_num_entries - 1; i > 0; i--) {
2532 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2533 mux_select(codec, 0, spec->am_entry[i].idx);
2537 mux_select(codec, 0, spec->am_entry[0].idx);
2539 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
2542 * Auto-Mute mode mixer enum support
2544 static int automute_mode_info(struct snd_kcontrol *kcontrol,
2545 struct snd_ctl_elem_info *uinfo)
2547 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2548 struct hda_gen_spec *spec = codec->spec;
2549 static const char * const texts3[] = {
2550 "Disabled", "Speaker Only", "Line Out+Speaker"
2553 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2554 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2555 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2558 static int automute_mode_get(struct snd_kcontrol *kcontrol,
2559 struct snd_ctl_elem_value *ucontrol)
2561 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2562 struct hda_gen_spec *spec = codec->spec;
2563 unsigned int val = 0;
2564 if (spec->automute_speaker)
2566 if (spec->automute_lo)
2569 ucontrol->value.enumerated.item[0] = val;
2573 static int automute_mode_put(struct snd_kcontrol *kcontrol,
2574 struct snd_ctl_elem_value *ucontrol)
2576 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2577 struct hda_gen_spec *spec = codec->spec;
2579 switch (ucontrol->value.enumerated.item[0]) {
2581 if (!spec->automute_speaker && !spec->automute_lo)
2583 spec->automute_speaker = 0;
2584 spec->automute_lo = 0;
2587 if (spec->automute_speaker_possible) {
2588 if (!spec->automute_lo && spec->automute_speaker)
2590 spec->automute_speaker = 1;
2591 spec->automute_lo = 0;
2592 } else if (spec->automute_lo_possible) {
2593 if (spec->automute_lo)
2595 spec->automute_lo = 1;
2600 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2602 if (spec->automute_speaker && spec->automute_lo)
2604 spec->automute_speaker = 1;
2605 spec->automute_lo = 1;
2610 call_update_outputs(codec);
2614 static const struct snd_kcontrol_new automute_mode_enum = {
2615 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2616 .name = "Auto-Mute Mode",
2617 .info = automute_mode_info,
2618 .get = automute_mode_get,
2619 .put = automute_mode_put,
2622 static int add_automute_mode_enum(struct hda_codec *codec)
2624 struct hda_gen_spec *spec = codec->spec;
2626 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
2632 * Check the availability of HP/line-out auto-mute;
2633 * Set up appropriately if really supported
2635 static int check_auto_mute_availability(struct hda_codec *codec)
2637 struct hda_gen_spec *spec = codec->spec;
2638 struct auto_pin_cfg *cfg = &spec->autocfg;
2642 if (cfg->hp_pins[0])
2644 if (cfg->line_out_pins[0])
2646 if (cfg->speaker_pins[0])
2648 if (present < 2) /* need two different output types */
2651 if (!cfg->speaker_pins[0] &&
2652 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2653 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2654 sizeof(cfg->speaker_pins));
2655 cfg->speaker_outs = cfg->line_outs;
2658 if (!cfg->hp_pins[0] &&
2659 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2660 memcpy(cfg->hp_pins, cfg->line_out_pins,
2661 sizeof(cfg->hp_pins));
2662 cfg->hp_outs = cfg->line_outs;
2665 for (i = 0; i < cfg->hp_outs; i++) {
2666 hda_nid_t nid = cfg->hp_pins[i];
2667 if (!is_jack_detectable(codec, nid))
2669 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2671 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
2672 snd_hda_gen_hp_automute);
2673 spec->detect_hp = 1;
2676 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2677 if (cfg->speaker_outs)
2678 for (i = 0; i < cfg->line_outs; i++) {
2679 hda_nid_t nid = cfg->line_out_pins[i];
2680 if (!is_jack_detectable(codec, nid))
2682 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2683 snd_hda_jack_detect_enable_callback(codec, nid,
2684 HDA_GEN_FRONT_EVENT,
2685 snd_hda_gen_line_automute);
2686 spec->detect_lo = 1;
2688 spec->automute_lo_possible = spec->detect_hp;
2691 spec->automute_speaker_possible = cfg->speaker_outs &&
2692 (spec->detect_hp || spec->detect_lo);
2694 spec->automute_lo = spec->automute_lo_possible;
2695 spec->automute_speaker = spec->automute_speaker_possible;
2697 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2698 /* create a control for automute mode */
2699 err = add_automute_mode_enum(codec);
2706 /* return the position of NID in the list, or -1 if not found */
2707 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2710 for (i = 0; i < nums; i++)
2716 /* check whether all auto-mic pins are valid; setup indices if OK */
2717 static bool auto_mic_check_imux(struct hda_codec *codec)
2719 struct hda_gen_spec *spec = codec->spec;
2720 const struct hda_input_mux *imux;
2723 imux = &spec->input_mux;
2724 for (i = 0; i < spec->am_num_entries; i++) {
2725 spec->am_entry[i].idx =
2726 find_idx_in_nid_list(spec->am_entry[i].pin,
2727 spec->imux_pins, imux->num_items);
2728 if (spec->am_entry[i].idx < 0)
2729 return false; /* no corresponding imux */
2732 /* we don't need the jack detection for the first pin */
2733 for (i = 1; i < spec->am_num_entries; i++)
2734 snd_hda_jack_detect_enable_callback(codec,
2735 spec->am_entry[i].pin,
2737 snd_hda_gen_mic_autoswitch);
2741 static int compare_attr(const void *ap, const void *bp)
2743 const struct automic_entry *a = ap;
2744 const struct automic_entry *b = bp;
2745 return (int)(a->attr - b->attr);
2749 * Check the availability of auto-mic switch;
2750 * Set up if really supported
2752 static int check_auto_mic_availability(struct hda_codec *codec)
2754 struct hda_gen_spec *spec = codec->spec;
2755 struct auto_pin_cfg *cfg = &spec->autocfg;
2761 for (i = 0; i < cfg->num_inputs; i++) {
2762 hda_nid_t nid = cfg->inputs[i].pin;
2764 attr = snd_hda_codec_get_pincfg(codec, nid);
2765 attr = snd_hda_get_input_pin_attr(attr);
2766 if (types & (1 << attr))
2767 return 0; /* already occupied */
2769 case INPUT_PIN_ATTR_INT:
2770 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2771 return 0; /* invalid type */
2773 case INPUT_PIN_ATTR_UNUSED:
2774 return 0; /* invalid entry */
2776 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2777 return 0; /* invalid type */
2778 if (!spec->line_in_auto_switch &&
2779 cfg->inputs[i].type != AUTO_PIN_MIC)
2780 return 0; /* only mic is allowed */
2781 if (!is_jack_detectable(codec, nid))
2782 return 0; /* no unsol support */
2785 if (num_pins >= MAX_AUTO_MIC_PINS)
2787 types |= (1 << attr);
2788 spec->am_entry[num_pins].pin = nid;
2789 spec->am_entry[num_pins].attr = attr;
2796 spec->am_num_entries = num_pins;
2797 /* sort the am_entry in the order of attr so that the pin with a
2798 * higher attr will be selected when the jack is plugged.
2800 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2801 compare_attr, NULL);
2803 if (!auto_mic_check_imux(codec))
2807 spec->num_adc_nids = 1;
2808 spec->cur_mux[0] = spec->am_entry[0].idx;
2809 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2810 spec->am_entry[0].pin,
2811 spec->am_entry[1].pin,
2812 spec->am_entry[2].pin);
2819 * Parse the given BIOS configuration and set up the hda_gen_spec
2821 * return 1 if successful, 0 if the proper config is not found,
2822 * or a negative error code
2824 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
2825 struct auto_pin_cfg *cfg)
2827 struct hda_gen_spec *spec = codec->spec;
2830 if (cfg != &spec->autocfg) {
2831 spec->autocfg = *cfg;
2832 cfg = &spec->autocfg;
2835 if (!cfg->line_outs) {
2836 if (cfg->dig_outs || cfg->dig_in_pin) {
2837 spec->multiout.max_channels = 2;
2838 spec->no_analog = 1;
2841 return 0; /* can't find valid BIOS pin config */
2844 if (!spec->no_primary_hp &&
2845 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2846 cfg->line_outs <= cfg->hp_outs) {
2847 /* use HP as primary out */
2848 cfg->speaker_outs = cfg->line_outs;
2849 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2850 sizeof(cfg->speaker_pins));
2851 cfg->line_outs = cfg->hp_outs;
2852 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2854 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2855 cfg->line_out_type = AUTO_PIN_HP_OUT;
2858 err = parse_output_paths(codec);
2861 err = create_multi_channel_mode(codec);
2864 err = create_multi_out_ctls(codec, cfg);
2867 err = create_hp_out_ctls(codec);
2870 err = create_speaker_out_ctls(codec);
2873 err = create_shared_input(codec);
2876 err = create_input_ctls(codec);
2880 /* check the multiple speaker pins */
2881 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2882 spec->const_channel_count = cfg->line_outs * 2;
2884 spec->const_channel_count = cfg->speaker_outs * 2;
2886 if (spec->multi_ios > 0)
2887 spec->multiout.max_channels = max(spec->ext_channel_count,
2888 spec->const_channel_count);
2890 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2892 err = check_auto_mute_availability(codec);
2896 err = check_dyn_adc_switch(codec);
2900 if (!spec->shared_mic_hp) {
2901 err = check_auto_mic_availability(codec);
2906 err = create_capture_mixers(codec);
2910 err = parse_mic_boost(codec);
2915 parse_digital(codec);
2919 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
2923 * Build control elements
2926 /* slave controls for virtual master */
2927 static const char * const slave_pfxs[] = {
2928 "Front", "Surround", "Center", "LFE", "Side",
2929 "Headphone", "Speaker", "Mono", "Line Out",
2930 "CLFE", "Bass Speaker", "PCM",
2934 int snd_hda_gen_build_controls(struct hda_codec *codec)
2936 struct hda_gen_spec *spec = codec->spec;
2939 if (spec->kctls.used) {
2940 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
2945 if (spec->multiout.dig_out_nid) {
2946 err = snd_hda_create_dig_out_ctls(codec,
2947 spec->multiout.dig_out_nid,
2948 spec->multiout.dig_out_nid,
2949 spec->pcm_rec[1].pcm_type);
2952 if (!spec->no_analog) {
2953 err = snd_hda_create_spdif_share_sw(codec,
2957 spec->multiout.share_spdif = 1;
2960 if (spec->dig_in_nid) {
2961 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
2966 /* if we have no master control, let's create it */
2967 if (!spec->no_analog &&
2968 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
2969 unsigned int vmaster_tlv[4];
2970 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
2971 HDA_OUTPUT, vmaster_tlv);
2972 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
2973 vmaster_tlv, slave_pfxs,
2978 if (!spec->no_analog &&
2979 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
2980 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
2983 true, &spec->vmaster_mute.sw_kctl);
2986 if (spec->vmaster_mute.hook)
2987 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
2988 spec->vmaster_mute_enum);
2991 free_kctls(spec); /* no longer needed */
2993 if (spec->shared_mic_hp) {
2995 int nid = spec->autocfg.inputs[1].pin;
2996 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
2999 err = snd_hda_jack_detect_enable(codec, nid, 0);
3004 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3010 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3018 * Analog playback callbacks
3020 static int 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_analog_open(codec, &spec->multiout, substream,
3029 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3030 struct hda_codec *codec,
3031 unsigned int stream_tag,
3032 unsigned int format,
3033 struct snd_pcm_substream *substream)
3035 struct hda_gen_spec *spec = codec->spec;
3036 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3037 stream_tag, format, substream);
3040 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3041 struct hda_codec *codec,
3042 struct snd_pcm_substream *substream)
3044 struct hda_gen_spec *spec = codec->spec;
3045 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3051 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3052 struct hda_codec *codec,
3053 struct snd_pcm_substream *substream)
3055 struct hda_gen_spec *spec = codec->spec;
3056 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3059 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3060 struct hda_codec *codec,
3061 unsigned int stream_tag,
3062 unsigned int format,
3063 struct snd_pcm_substream *substream)
3065 struct hda_gen_spec *spec = codec->spec;
3066 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3067 stream_tag, format, substream);
3070 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3071 struct hda_codec *codec,
3072 struct snd_pcm_substream *substream)
3074 struct hda_gen_spec *spec = codec->spec;
3075 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3078 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3079 struct hda_codec *codec,
3080 struct snd_pcm_substream *substream)
3082 struct hda_gen_spec *spec = codec->spec;
3083 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3089 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3090 struct hda_codec *codec,
3091 unsigned int stream_tag,
3092 unsigned int format,
3093 struct snd_pcm_substream *substream)
3095 struct hda_gen_spec *spec = codec->spec;
3097 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
3098 stream_tag, 0, format);
3102 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3103 struct hda_codec *codec,
3104 struct snd_pcm_substream *substream)
3106 struct hda_gen_spec *spec = codec->spec;
3108 snd_hda_codec_cleanup_stream(codec,
3109 spec->adc_nids[substream->number + 1]);
3115 static const struct hda_pcm_stream pcm_analog_playback = {
3119 /* NID is set in build_pcms */
3121 .open = playback_pcm_open,
3122 .prepare = playback_pcm_prepare,
3123 .cleanup = playback_pcm_cleanup
3127 static const struct hda_pcm_stream pcm_analog_capture = {
3131 /* NID is set in build_pcms */
3134 static const struct hda_pcm_stream pcm_analog_alt_playback = {
3138 /* NID is set in build_pcms */
3141 static const struct hda_pcm_stream pcm_analog_alt_capture = {
3142 .substreams = 2, /* can be overridden */
3145 /* NID is set in build_pcms */
3147 .prepare = alt_capture_pcm_prepare,
3148 .cleanup = alt_capture_pcm_cleanup
3152 static const struct hda_pcm_stream pcm_digital_playback = {
3156 /* NID is set in build_pcms */
3158 .open = dig_playback_pcm_open,
3159 .close = dig_playback_pcm_close,
3160 .prepare = dig_playback_pcm_prepare,
3161 .cleanup = dig_playback_pcm_cleanup
3165 static const struct hda_pcm_stream pcm_digital_capture = {
3169 /* NID is set in build_pcms */
3172 /* Used by build_pcms to flag that a PCM has no playback stream */
3173 static const struct hda_pcm_stream pcm_null_stream = {
3180 * dynamic changing ADC PCM streams
3182 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3184 struct hda_gen_spec *spec = codec->spec;
3185 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3187 if (spec->cur_adc && spec->cur_adc != new_adc) {
3188 /* stream is running, let's swap the current ADC */
3189 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3190 spec->cur_adc = new_adc;
3191 snd_hda_codec_setup_stream(codec, new_adc,
3192 spec->cur_adc_stream_tag, 0,
3193 spec->cur_adc_format);
3199 /* analog capture with dynamic dual-adc changes */
3200 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3201 struct hda_codec *codec,
3202 unsigned int stream_tag,
3203 unsigned int format,
3204 struct snd_pcm_substream *substream)
3206 struct hda_gen_spec *spec = codec->spec;
3207 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3208 spec->cur_adc_stream_tag = stream_tag;
3209 spec->cur_adc_format = format;
3210 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3214 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3215 struct hda_codec *codec,
3216 struct snd_pcm_substream *substream)
3218 struct hda_gen_spec *spec = codec->spec;
3219 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3224 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3228 .nid = 0, /* fill later */
3230 .prepare = dyn_adc_capture_pcm_prepare,
3231 .cleanup = dyn_adc_capture_pcm_cleanup
3235 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3236 const char *chip_name)
3242 strlcpy(str, chip_name, len);
3244 /* drop non-alnum chars after a space */
3245 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3246 if (!isalnum(p[1])) {
3251 strlcat(str, sfx, len);
3254 /* build PCM streams based on the parsed results */
3255 int snd_hda_gen_build_pcms(struct hda_codec *codec)
3257 struct hda_gen_spec *spec = codec->spec;
3258 struct hda_pcm *info = spec->pcm_rec;
3259 const struct hda_pcm_stream *p;
3260 bool have_multi_adcs;
3262 codec->num_pcms = 1;
3263 codec->pcm_info = info;
3265 if (spec->no_analog)
3268 fill_pcm_stream_name(spec->stream_name_analog,
3269 sizeof(spec->stream_name_analog),
3270 " Analog", codec->chip_name);
3271 info->name = spec->stream_name_analog;
3273 if (spec->multiout.num_dacs > 0) {
3274 p = spec->stream_analog_playback;
3276 p = &pcm_analog_playback;
3277 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3278 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3279 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3280 spec->multiout.max_channels;
3281 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3282 spec->autocfg.line_outs == 2)
3283 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3286 if (spec->num_adc_nids) {
3287 p = spec->stream_analog_capture;
3289 if (spec->dyn_adc_switch)
3290 p = &dyn_adc_pcm_analog_capture;
3292 p = &pcm_analog_capture;
3294 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3295 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3299 /* SPDIF for stream index #1 */
3300 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
3301 fill_pcm_stream_name(spec->stream_name_digital,
3302 sizeof(spec->stream_name_digital),
3303 " Digital", codec->chip_name);
3304 codec->num_pcms = 2;
3305 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3306 info = spec->pcm_rec + 1;
3307 info->name = spec->stream_name_digital;
3308 if (spec->dig_out_type)
3309 info->pcm_type = spec->dig_out_type;
3311 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3312 if (spec->multiout.dig_out_nid) {
3313 p = spec->stream_digital_playback;
3315 p = &pcm_digital_playback;
3316 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3317 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3319 if (spec->dig_in_nid) {
3320 p = spec->stream_digital_capture;
3322 p = &pcm_digital_capture;
3323 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3324 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3328 if (spec->no_analog)
3331 /* If the use of more than one ADC is requested for the current
3332 * model, configure a second analog capture-only PCM.
3334 have_multi_adcs = (spec->num_adc_nids > 1) &&
3335 !spec->dyn_adc_switch && !spec->auto_mic;
3336 /* Additional Analaog capture for index #2 */
3337 if (spec->alt_dac_nid || have_multi_adcs) {
3338 codec->num_pcms = 3;
3339 info = spec->pcm_rec + 2;
3340 info->name = spec->stream_name_analog;
3341 if (spec->alt_dac_nid) {
3342 p = spec->stream_analog_alt_playback;
3344 p = &pcm_analog_alt_playback;
3345 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3346 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3349 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3351 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3353 if (have_multi_adcs) {
3354 p = spec->stream_analog_alt_capture;
3356 p = &pcm_analog_alt_capture;
3357 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3358 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3360 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3361 spec->num_adc_nids - 1;
3363 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3365 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3371 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3375 * Standard auto-parser initializations
3378 /* configure the path from the given dac to the pin as the proper output */
3379 static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3380 int pin_type, hda_nid_t dac)
3382 struct nid_path *path;
3384 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3385 path = snd_hda_get_nid_path(codec, dac, pin);
3390 snd_hda_activate_path(codec, path, true, true);
3391 set_pin_eapd(codec, pin, true);
3394 /* initialize primary output paths */
3395 static void init_multi_out(struct hda_codec *codec)
3397 struct hda_gen_spec *spec = codec->spec;
3402 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3407 for (i = 0; i < spec->autocfg.line_outs; i++) {
3408 nid = spec->autocfg.line_out_pins[i];
3410 dac = spec->multiout.dac_nids[i];
3412 dac = spec->multiout.dac_nids[0];
3413 set_output_and_unmute(codec, nid, pin_type, dac);
3419 static void __init_extra_out(struct hda_codec *codec, int num_outs,
3420 hda_nid_t *pins, hda_nid_t *dacs, int type)
3422 struct hda_gen_spec *spec = codec->spec;
3426 for (i = 0; i < num_outs; i++) {
3432 if (i > 0 && dacs[0])
3435 dac = spec->multiout.dac_nids[0];
3437 set_output_and_unmute(codec, pin, type, dac);
3441 /* initialize hp and speaker paths */
3442 static void init_extra_out(struct hda_codec *codec)
3444 struct hda_gen_spec *spec = codec->spec;
3446 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3447 __init_extra_out(codec, spec->autocfg.hp_outs,
3448 spec->autocfg.hp_pins,
3449 spec->multiout.hp_out_nid, PIN_HP);
3450 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3451 __init_extra_out(codec, spec->autocfg.speaker_outs,
3452 spec->autocfg.speaker_pins,
3453 spec->multiout.extra_out_nid, PIN_OUT);
3456 /* initialize multi-io paths */
3457 static void init_multi_io(struct hda_codec *codec)
3459 struct hda_gen_spec *spec = codec->spec;
3462 for (i = 0; i < spec->multi_ios; i++) {
3463 hda_nid_t pin = spec->multi_io[i].pin;
3464 struct nid_path *path;
3465 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3468 if (!spec->multi_io[i].ctl_in)
3469 spec->multi_io[i].ctl_in =
3470 snd_hda_codec_update_cache(codec, pin, 0,
3471 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3472 snd_hda_activate_path(codec, path, path->active, true);
3476 /* set up the input pin config, depending on the given auto-pin type */
3477 static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3480 unsigned int val = PIN_IN;
3481 if (auto_pin_type == AUTO_PIN_MIC)
3482 val |= snd_hda_get_default_vref(codec, nid);
3483 snd_hda_set_pin_ctl_cache(codec, nid, val);
3486 /* set up input pins and loopback paths */
3487 static void init_analog_input(struct hda_codec *codec)
3489 struct hda_gen_spec *spec = codec->spec;
3490 struct auto_pin_cfg *cfg = &spec->autocfg;
3493 for (i = 0; i < cfg->num_inputs; i++) {
3494 hda_nid_t nid = cfg->inputs[i].pin;
3495 if (is_input_pin(codec, nid))
3496 set_input_pin(codec, nid, cfg->inputs[i].type);
3498 /* init loopback inputs */
3499 if (spec->mixer_nid) {
3500 struct nid_path *path;
3501 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3503 snd_hda_activate_path(codec, path,
3504 path->active, false);
3509 /* initialize ADC paths */
3510 static void init_input_src(struct hda_codec *codec)
3512 struct hda_gen_spec *spec = codec->spec;
3513 struct hda_input_mux *imux = &spec->input_mux;
3514 struct nid_path *path;
3517 if (spec->dyn_adc_switch)
3520 nums = spec->num_adc_nids;
3522 for (c = 0; c < nums; c++) {
3523 for (i = 0; i < imux->num_items; i++) {
3524 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3525 get_adc_nid(codec, c, i));
3527 bool active = path->active;
3528 if (i == spec->cur_mux[c])
3530 snd_hda_activate_path(codec, path, active, false);
3535 if (spec->shared_mic_hp)
3536 update_shared_mic_hp(codec, spec->cur_mux[0]);
3538 if (spec->cap_sync_hook)
3539 spec->cap_sync_hook(codec);
3542 /* set right pin controls for digital I/O */
3543 static void init_digital(struct hda_codec *codec)
3545 struct hda_gen_spec *spec = codec->spec;
3549 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3550 pin = spec->autocfg.dig_out_pins[i];
3553 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3555 pin = spec->autocfg.dig_in_pin;
3557 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
3560 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3561 * invalid unsol tags by some reason
3563 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3567 for (i = 0; i < codec->init_pins.used; i++) {
3568 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3569 hda_nid_t nid = pin->nid;
3570 if (is_jack_detectable(codec, nid) &&
3571 !snd_hda_jack_tbl_get(codec, nid))
3572 snd_hda_codec_update_cache(codec, nid, 0,
3573 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3577 int snd_hda_gen_init(struct hda_codec *codec)
3579 struct hda_gen_spec *spec = codec->spec;
3581 if (spec->init_hook)
3582 spec->init_hook(codec);
3584 snd_hda_apply_verbs(codec);
3586 codec->cached_write = 1;
3588 init_multi_out(codec);
3589 init_extra_out(codec);
3590 init_multi_io(codec);
3591 init_analog_input(codec);
3592 init_input_src(codec);
3593 init_digital(codec);
3595 clear_unsol_on_unused_pins(codec);
3597 /* call init functions of standard auto-mute helpers */
3598 snd_hda_gen_hp_automute(codec, NULL);
3599 snd_hda_gen_line_automute(codec, NULL);
3600 snd_hda_gen_mic_autoswitch(codec, NULL);
3602 snd_hda_codec_flush_amp_cache(codec);
3603 snd_hda_codec_flush_cmd_cache(codec);
3605 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3606 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3608 hda_call_check_power_status(codec, 0x01);
3611 EXPORT_SYMBOL(snd_hda_gen_init);
3615 * the generic codec support
3619 static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3621 struct hda_gen_spec *spec = codec->spec;
3622 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3626 static void generic_free(struct hda_codec *codec)
3628 snd_hda_gen_spec_free(codec->spec);
3633 static const struct hda_codec_ops generic_patch_ops = {
3634 .build_controls = snd_hda_gen_build_controls,
3635 .build_pcms = snd_hda_gen_build_pcms,
3636 .init = snd_hda_gen_init,
3637 .free = generic_free,
3638 .unsol_event = snd_hda_jack_unsol_event,
3640 .check_power_status = generic_check_power_status,
3644 int snd_hda_parse_generic_codec(struct hda_codec *codec)
3646 struct hda_gen_spec *spec;
3649 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3652 snd_hda_gen_spec_init(spec);
3655 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3659 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
3663 codec->patch_ops = generic_patch_ops;
3667 generic_free(codec);
3670 EXPORT_SYMBOL(snd_hda_parse_generic_codec);