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 static void print_nid_path(const char *pfx, struct nid_path *path)
196 for (i = 0; i < path->depth; i++) {
198 sprintf(tmp, ":%02x", path->path[i]);
199 strlcat(buf, tmp, sizeof(buf));
201 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
204 /* called recursively */
205 static bool __parse_nid_path(struct hda_codec *codec,
206 hda_nid_t from_nid, hda_nid_t to_nid,
207 int with_aa_mix, struct nid_path *path, int depth)
209 struct hda_gen_spec *spec = codec->spec;
213 if (to_nid == spec->mixer_nid) {
214 if (with_aa_mix == HDA_PARSE_NO_AAMIX)
216 with_aa_mix = HDA_PARSE_ALL; /* mark aa-mix is included */
219 nums = snd_hda_get_connections(codec, to_nid, conn, ARRAY_SIZE(conn));
220 for (i = 0; i < nums; i++) {
221 if (conn[i] != from_nid) {
222 /* special case: when from_nid is 0,
223 * try to find an empty DAC
226 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
227 is_dac_already_used(codec, conn[i]))
230 /* aa-mix is requested but not included? */
231 if (!(spec->mixer_nid && with_aa_mix == HDA_PARSE_ONLY_AAMIX))
234 if (depth >= MAX_NID_PATH_DEPTH)
236 for (i = 0; i < nums; i++) {
238 type = get_wcaps_type(get_wcaps(codec, conn[i]));
239 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
242 if (__parse_nid_path(codec, from_nid, conn[i],
243 with_aa_mix, path, depth + 1))
249 path->path[path->depth] = conn[i];
250 path->idx[path->depth + 1] = i;
251 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
252 path->multi[path->depth + 1] = 1;
257 /* parse the widget path from the given nid to the target nid;
258 * when @from_nid is 0, try to find an empty DAC;
259 * when @with_aa_mix is HDA_PARSE_NO_AAMIX, paths with spec->mixer_nid are
260 * excluded, only the paths that don't go through the mixer will be chosen.
261 * when @with_aa_mix is HDA_PARSE_ONLY_AAMIX, only the paths going through
262 * spec->mixer_nid will be chosen.
263 * when @with_aa_mix is HDA_PARSE_ALL, no special handling about mixer widget.
265 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
266 hda_nid_t to_nid, int with_aa_mix,
267 struct nid_path *path)
269 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
270 path->path[path->depth] = to_nid;
276 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
279 * parse the path between the given NIDs and add to the path list.
280 * if no valid path is found, return NULL
283 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
284 hda_nid_t to_nid, int with_aa_mix)
286 struct hda_gen_spec *spec = codec->spec;
287 struct nid_path *path;
289 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
292 path = snd_array_new(&spec->paths);
295 memset(path, 0, sizeof(*path));
296 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
302 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
304 /* look for an empty DAC slot */
305 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
308 struct hda_gen_spec *spec = codec->spec;
312 for (i = 0; i < spec->num_all_dacs; i++) {
313 hda_nid_t nid = spec->all_dacs[i];
314 if (!nid || is_dac_already_used(codec, nid))
316 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
317 if (is_digital != cap_digital)
319 if (is_reachable_path(codec, nid, pin))
325 /* replace the channels in the composed amp value with the given number */
326 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
328 val &= ~(0x3U << 16);
333 /* check whether the widget has the given amp capability for the direction */
334 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
335 int dir, unsigned int bits)
339 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
340 if (query_amp_caps(codec, nid, dir) & bits)
345 #define nid_has_mute(codec, nid, dir) \
346 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
347 #define nid_has_volume(codec, nid, dir) \
348 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
350 /* look for a widget suitable for assigning a mute switch in the path */
351 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
352 struct nid_path *path)
356 for (i = path->depth - 1; i >= 0; i--) {
357 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
358 return path->path[i];
359 if (i != path->depth - 1 && i != 0 &&
360 nid_has_mute(codec, path->path[i], HDA_INPUT))
361 return path->path[i];
366 /* look for a widget suitable for assigning a volume ctl in the path */
367 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
368 struct nid_path *path)
372 for (i = path->depth - 1; i >= 0; i--) {
373 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
374 return path->path[i];
380 * path activation / deactivation
383 /* can have the amp-in capability? */
384 static bool has_amp_in(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_IN_AMP))
392 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
397 /* can have the amp-out capability? */
398 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
400 hda_nid_t nid = path->path[idx];
401 unsigned int caps = get_wcaps(codec, nid);
402 unsigned int type = get_wcaps_type(caps);
404 if (!(caps & AC_WCAP_OUT_AMP))
406 if (type == AC_WID_PIN && !idx) /* only for output pins */
411 /* check whether the given (nid,dir,idx) is active */
412 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
413 unsigned int idx, unsigned int dir)
415 struct hda_gen_spec *spec = codec->spec;
418 for (n = 0; n < spec->paths.used; n++) {
419 struct nid_path *path = snd_array_elem(&spec->paths, n);
422 for (i = 0; i < path->depth; i++) {
423 if (path->path[i] == nid) {
424 if (dir == HDA_OUTPUT || path->idx[i] == idx)
433 /* get the default amp value for the target state */
434 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
435 int dir, bool enable)
438 unsigned int val = 0;
440 caps = query_amp_caps(codec, nid, dir);
441 if (caps & AC_AMPCAP_NUM_STEPS) {
444 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
446 if (caps & AC_AMPCAP_MUTE) {
453 /* initialize the amp value (only at the first time) */
454 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
456 int val = get_amp_val_to_activate(codec, nid, dir, false);
457 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
460 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
461 int idx, bool enable)
464 if (is_ctl_associated(codec, nid, dir, idx) ||
465 is_active_nid(codec, nid, dir, idx))
467 val = get_amp_val_to_activate(codec, nid, dir, enable);
468 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
471 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
474 hda_nid_t nid = path->path[i];
475 init_amp(codec, nid, HDA_OUTPUT, 0);
476 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
479 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
480 int i, bool enable, bool add_aamix)
482 struct hda_gen_spec *spec = codec->spec;
486 hda_nid_t nid = path->path[i];
488 nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
489 type = get_wcaps_type(get_wcaps(codec, nid));
490 if (type == AC_WID_PIN ||
491 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
497 for (n = 0; n < nums; n++)
498 init_amp(codec, nid, HDA_INPUT, n);
500 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
503 /* here is a little bit tricky in comparison with activate_amp_out();
504 * when aa-mixer is available, we need to enable the path as well
506 for (n = 0; n < nums; n++) {
507 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
509 activate_amp(codec, nid, HDA_INPUT, n, enable);
513 /* activate or deactivate the given path
514 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
516 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
517 bool enable, bool add_aamix)
522 path->active = false;
524 for (i = path->depth - 1; i >= 0; i--) {
525 if (enable && path->multi[i])
526 snd_hda_codec_write_cache(codec, path->path[i], 0,
527 AC_VERB_SET_CONNECT_SEL,
529 if (has_amp_in(codec, path, i))
530 activate_amp_in(codec, path, i, enable, add_aamix);
531 if (has_amp_out(codec, path, i))
532 activate_amp_out(codec, path, i, enable);
538 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
540 /* turn on/off EAPD on the given pin */
541 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
543 struct hda_gen_spec *spec = codec->spec;
544 if (spec->own_eapd_ctl ||
545 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
547 snd_hda_codec_update_cache(codec, pin, 0,
548 AC_VERB_SET_EAPD_BTLENABLE,
549 enable ? 0x02 : 0x00);
554 * Helper functions for creating mixer ctl elements
564 static const struct snd_kcontrol_new control_templates[] = {
565 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
566 HDA_CODEC_MUTE(NULL, 0, 0, 0),
567 HDA_BIND_MUTE(NULL, 0, 0, 0),
568 HDA_BIND_VOL(NULL, 0),
569 HDA_BIND_SW(NULL, 0),
572 /* add dynamic controls from template */
573 static int add_control(struct hda_gen_spec *spec, int type, const char *name,
574 int cidx, unsigned long val)
576 struct snd_kcontrol_new *knew;
578 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
582 if (get_amp_nid_(val))
583 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
584 knew->private_value = val;
588 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
589 const char *pfx, const char *dir,
590 const char *sfx, int cidx, unsigned long val)
593 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
594 return add_control(spec, type, name, cidx, val);
597 #define add_pb_vol_ctrl(spec, type, pfx, val) \
598 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
599 #define add_pb_sw_ctrl(spec, type, pfx, val) \
600 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
601 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
602 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
603 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
604 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
606 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
607 unsigned int chs, struct nid_path *path)
612 val = path->ctls[NID_PATH_VOL_CTL];
615 val = amp_val_replace_channels(val, chs);
616 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
619 /* return the channel bits suitable for the given path->ctls[] */
620 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
623 int chs = 1; /* mono (left only) */
625 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
626 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
627 chs = 3; /* stereo */
632 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
633 struct nid_path *path)
635 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
636 return add_vol_ctl(codec, pfx, cidx, chs, path);
639 /* create a mute-switch for the given mixer widget;
640 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
642 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
643 unsigned int chs, struct nid_path *path)
646 int type = HDA_CTL_WIDGET_MUTE;
650 val = path->ctls[NID_PATH_MUTE_CTL];
653 val = amp_val_replace_channels(val, chs);
654 if (get_amp_direction_(val) == HDA_INPUT) {
655 hda_nid_t nid = get_amp_nid_(val);
656 int nums = snd_hda_get_num_conns(codec, nid);
658 type = HDA_CTL_BIND_MUTE;
662 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
665 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
666 int cidx, struct nid_path *path)
668 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
669 return add_sw_ctl(codec, pfx, cidx, chs, path);
672 static const char * const channel_name[4] = {
673 "Front", "Surround", "CLFE", "Side"
676 /* give some appropriate ctl name prefix for the given line out channel */
677 static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
678 bool can_be_master, int *index)
680 struct auto_pin_cfg *cfg = &spec->autocfg;
683 if (cfg->line_outs == 1 && !spec->multi_ios &&
684 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
685 return spec->vmaster_mute.hook ? "PCM" : "Master";
687 /* if there is really a single DAC used in the whole output paths,
688 * use it master (or "PCM" if a vmaster hook is present)
690 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
691 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
692 return spec->vmaster_mute.hook ? "PCM" : "Master";
694 switch (cfg->line_out_type) {
695 case AUTO_PIN_SPEAKER_OUT:
696 if (cfg->line_outs == 1)
698 if (cfg->line_outs == 2)
699 return ch ? "Bass Speaker" : "Speaker";
701 case AUTO_PIN_HP_OUT:
702 /* for multi-io case, only the primary out */
703 if (ch && spec->multi_ios)
708 if (cfg->line_outs == 1 && !spec->multi_ios)
712 if (ch >= ARRAY_SIZE(channel_name)) {
717 return channel_name[ch];
724 /* badness definition */
726 /* No primary DAC is found for the main output */
727 BAD_NO_PRIMARY_DAC = 0x10000,
728 /* No DAC is found for the extra output */
730 /* No possible multi-ios */
731 BAD_MULTI_IO = 0x103,
732 /* No individual DAC for extra output */
733 BAD_NO_EXTRA_DAC = 0x102,
734 /* No individual DAC for extra surrounds */
735 BAD_NO_EXTRA_SURR_DAC = 0x101,
736 /* Primary DAC shared with main surrounds */
737 BAD_SHARED_SURROUND = 0x100,
738 /* Primary DAC shared with main CLFE */
739 BAD_SHARED_CLFE = 0x10,
740 /* Primary DAC shared with extra surrounds */
741 BAD_SHARED_EXTRA_SURROUND = 0x10,
742 /* Volume widget is shared */
743 BAD_SHARED_VOL = 0x10,
746 /* look for widgets in the path between the given NIDs appropriate for
747 * volume and mute controls, and assign the values to ctls[].
749 * When no appropriate widget is found in the path, the badness value
750 * is incremented depending on the situation. The function returns the
751 * total badness for both volume and mute controls.
753 static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
756 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
762 return BAD_SHARED_VOL * 2;
763 nid = look_for_out_vol_nid(codec, path);
765 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
766 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
767 badness += BAD_SHARED_VOL;
769 path->ctls[NID_PATH_VOL_CTL] = val;
771 badness += BAD_SHARED_VOL;
772 nid = look_for_out_mute_nid(codec, path);
774 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
775 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
776 nid_has_mute(codec, nid, HDA_OUTPUT))
777 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
779 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
780 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
781 badness += BAD_SHARED_VOL;
783 path->ctls[NID_PATH_MUTE_CTL] = val;
785 badness += BAD_SHARED_VOL;
789 struct badness_table {
790 int no_primary_dac; /* no primary DAC */
791 int no_dac; /* no secondary DACs */
792 int shared_primary; /* primary DAC is shared with main output */
793 int shared_surr; /* secondary DAC shared with main or primary */
794 int shared_clfe; /* third DAC shared with main or primary */
795 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
798 static struct badness_table main_out_badness = {
799 .no_primary_dac = BAD_NO_PRIMARY_DAC,
800 .no_dac = BAD_NO_DAC,
801 .shared_primary = BAD_NO_PRIMARY_DAC,
802 .shared_surr = BAD_SHARED_SURROUND,
803 .shared_clfe = BAD_SHARED_CLFE,
804 .shared_surr_main = BAD_SHARED_SURROUND,
807 static struct badness_table extra_out_badness = {
808 .no_primary_dac = BAD_NO_DAC,
809 .no_dac = BAD_NO_DAC,
810 .shared_primary = BAD_NO_EXTRA_DAC,
811 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
812 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
813 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
816 /* try to assign DACs to pins and return the resultant badness */
817 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
818 const hda_nid_t *pins, hda_nid_t *dacs,
819 const struct badness_table *bad)
821 struct hda_gen_spec *spec = codec->spec;
822 struct auto_pin_cfg *cfg = &spec->autocfg;
830 for (i = 0; i < num_outs; i++) {
831 struct nid_path *path;
832 hda_nid_t pin = pins[i];
834 dacs[i] = look_for_dac(codec, pin, false);
835 if (!dacs[i] && !i) {
836 for (j = 1; j < num_outs; j++) {
837 if (is_reachable_path(codec, dacs[j], pin)) {
846 if (is_reachable_path(codec, dacs[0], pin))
848 else if (cfg->line_outs > i &&
849 is_reachable_path(codec, spec->private_dac_nids[i], pin))
850 dac = spec->private_dac_nids[i];
853 badness += bad->shared_primary;
855 badness += bad->shared_surr;
857 badness += bad->shared_clfe;
858 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
859 dac = spec->private_dac_nids[0];
860 badness += bad->shared_surr_main;
862 badness += bad->no_primary_dac;
864 badness += bad->no_dac;
866 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_NO_AAMIX);
867 if (!path && i > 0 && spec->mixer_nid) {
869 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_ALL);
874 print_nid_path("output", path);
876 badness += assign_out_path_ctls(codec, pin, dac);
882 /* return NID if the given pin has only a single connection to a certain DAC */
883 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
885 struct hda_gen_spec *spec = codec->spec;
887 hda_nid_t nid_found = 0;
889 for (i = 0; i < spec->num_all_dacs; i++) {
890 hda_nid_t nid = spec->all_dacs[i];
891 if (!nid || is_dac_already_used(codec, nid))
893 if (is_reachable_path(codec, nid, pin)) {
902 /* check whether the given pin can be a multi-io pin */
903 static bool can_be_multiio_pin(struct hda_codec *codec,
904 unsigned int location, hda_nid_t nid)
906 unsigned int defcfg, caps;
908 defcfg = snd_hda_codec_get_pincfg(codec, nid);
909 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
911 if (location && get_defcfg_location(defcfg) != location)
913 caps = snd_hda_query_pin_caps(codec, nid);
914 if (!(caps & AC_PINCAP_OUT))
922 * When hardwired is set, try to fill ony hardwired pins, and returns
923 * zero if any pins are filled, non-zero if nothing found.
924 * When hardwired is off, try to fill possible input pins, and returns
927 static int fill_multi_ios(struct hda_codec *codec,
928 hda_nid_t reference_pin,
929 bool hardwired, int offset)
931 struct hda_gen_spec *spec = codec->spec;
932 struct auto_pin_cfg *cfg = &spec->autocfg;
933 int type, i, j, dacs, num_pins, old_pins;
934 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
935 unsigned int location = get_defcfg_location(defcfg);
938 old_pins = spec->multi_ios;
943 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
944 for (i = 0; i < cfg->num_inputs; i++) {
945 if (cfg->inputs[i].type != type)
947 if (can_be_multiio_pin(codec, location,
955 dacs = spec->multiout.num_dacs;
956 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
957 for (i = 0; i < cfg->num_inputs; i++) {
958 struct nid_path *path;
959 hda_nid_t nid = cfg->inputs[i].pin;
962 if (cfg->inputs[i].type != type)
964 if (!can_be_multiio_pin(codec, location, nid))
966 for (j = 0; j < spec->multi_ios; j++) {
967 if (nid == spec->multi_io[j].pin)
970 if (j < spec->multi_ios)
973 if (offset && offset + spec->multi_ios < dacs) {
974 dac = spec->private_dac_nids[offset + spec->multi_ios];
975 if (!is_reachable_path(codec, dac, nid))
979 dac = get_dac_if_single(codec, nid);
981 dac = look_for_dac(codec, nid, false);
986 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
991 print_nid_path("multiio", path);
992 spec->multi_io[spec->multi_ios].pin = nid;
993 spec->multi_io[spec->multi_ios].dac = dac;
995 if (spec->multi_ios >= 2)
1001 badness = BAD_MULTI_IO;
1002 if (old_pins == spec->multi_ios) {
1004 return 1; /* nothing found */
1006 return badness; /* no badness if nothing found */
1008 if (!hardwired && spec->multi_ios < 2) {
1009 /* cancel newly assigned paths */
1010 spec->paths.used -= spec->multi_ios - old_pins;
1011 spec->multi_ios = old_pins;
1015 /* assign volume and mute controls */
1016 for (i = old_pins; i < spec->multi_ios; i++)
1017 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1018 spec->multi_io[i].dac);
1023 /* map DACs for all pins in the list if they are single connections */
1024 static bool map_singles(struct hda_codec *codec, int outs,
1025 const hda_nid_t *pins, hda_nid_t *dacs)
1027 struct hda_gen_spec *spec = codec->spec;
1030 for (i = 0; i < outs; i++) {
1031 struct nid_path *path;
1035 dac = get_dac_if_single(codec, pins[i]);
1038 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
1039 if (!path && i > 0 && spec->mixer_nid)
1040 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
1044 print_nid_path("output", path);
1050 /* fill in the dac_nids table from the parsed pin configuration */
1051 static int fill_and_eval_dacs(struct hda_codec *codec,
1052 bool fill_hardwired,
1053 bool fill_mio_first)
1055 struct hda_gen_spec *spec = codec->spec;
1056 struct auto_pin_cfg *cfg = &spec->autocfg;
1057 int i, err, badness;
1059 /* set num_dacs once to full for look_for_dac() */
1060 spec->multiout.num_dacs = cfg->line_outs;
1061 spec->multiout.dac_nids = spec->private_dac_nids;
1062 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1063 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1064 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1065 spec->multi_ios = 0;
1066 snd_array_free(&spec->paths);
1069 /* fill hard-wired DACs first */
1070 if (fill_hardwired) {
1073 mapped = map_singles(codec, cfg->line_outs,
1075 spec->private_dac_nids);
1076 mapped |= map_singles(codec, cfg->hp_outs,
1078 spec->multiout.hp_out_nid);
1079 mapped |= map_singles(codec, cfg->speaker_outs,
1081 spec->multiout.extra_out_nid);
1082 if (fill_mio_first && cfg->line_outs == 1 &&
1083 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1084 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1091 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1092 spec->private_dac_nids,
1095 /* re-count num_dacs and squash invalid entries */
1096 spec->multiout.num_dacs = 0;
1097 for (i = 0; i < cfg->line_outs; i++) {
1098 if (spec->private_dac_nids[i])
1099 spec->multiout.num_dacs++;
1101 memmove(spec->private_dac_nids + i,
1102 spec->private_dac_nids + i + 1,
1103 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1104 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1108 if (fill_mio_first &&
1109 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1110 /* try to fill multi-io first */
1111 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1114 /* we don't count badness at this stage yet */
1117 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1118 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1119 spec->multiout.hp_out_nid,
1120 &extra_out_badness);
1125 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1126 err = try_assign_dacs(codec, cfg->speaker_outs,
1128 spec->multiout.extra_out_nid,
1129 &extra_out_badness);
1134 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1135 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1140 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1141 /* try multi-ios with HP + inputs */
1143 if (cfg->line_outs >= 3)
1145 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1151 if (spec->multi_ios == 2) {
1152 for (i = 0; i < 2; i++)
1153 spec->private_dac_nids[spec->multiout.num_dacs++] =
1154 spec->multi_io[i].dac;
1155 spec->ext_channel_count = 2;
1156 } else if (spec->multi_ios) {
1157 spec->multi_ios = 0;
1158 badness += BAD_MULTI_IO;
1164 #define DEBUG_BADNESS
1166 #ifdef DEBUG_BADNESS
1167 #define debug_badness snd_printdd
1169 #define debug_badness(...)
1172 static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1174 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1175 cfg->line_out_pins[0], cfg->line_out_pins[1],
1176 cfg->line_out_pins[2], cfg->line_out_pins[3],
1177 spec->multiout.dac_nids[0],
1178 spec->multiout.dac_nids[1],
1179 spec->multiout.dac_nids[2],
1180 spec->multiout.dac_nids[3]);
1181 if (spec->multi_ios > 0)
1182 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1184 spec->multi_io[0].pin, spec->multi_io[1].pin,
1185 spec->multi_io[0].dac, spec->multi_io[1].dac);
1186 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1187 cfg->hp_pins[0], cfg->hp_pins[1],
1188 cfg->hp_pins[2], cfg->hp_pins[3],
1189 spec->multiout.hp_out_nid[0],
1190 spec->multiout.hp_out_nid[1],
1191 spec->multiout.hp_out_nid[2],
1192 spec->multiout.hp_out_nid[3]);
1193 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1194 cfg->speaker_pins[0], cfg->speaker_pins[1],
1195 cfg->speaker_pins[2], cfg->speaker_pins[3],
1196 spec->multiout.extra_out_nid[0],
1197 spec->multiout.extra_out_nid[1],
1198 spec->multiout.extra_out_nid[2],
1199 spec->multiout.extra_out_nid[3]);
1202 /* find all available DACs of the codec */
1203 static void fill_all_dac_nids(struct hda_codec *codec)
1205 struct hda_gen_spec *spec = codec->spec;
1207 hda_nid_t nid = codec->start_nid;
1209 spec->num_all_dacs = 0;
1210 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1211 for (i = 0; i < codec->num_nodes; i++, nid++) {
1212 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1214 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1215 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1218 spec->all_dacs[spec->num_all_dacs++] = nid;
1222 static int parse_output_paths(struct hda_codec *codec)
1224 struct hda_gen_spec *spec = codec->spec;
1225 struct auto_pin_cfg *cfg = &spec->autocfg;
1226 struct auto_pin_cfg *best_cfg;
1227 int best_badness = INT_MAX;
1229 bool fill_hardwired = true, fill_mio_first = true;
1230 bool best_wired = true, best_mio = true;
1231 bool hp_spk_swapped = false;
1233 fill_all_dac_nids(codec);
1235 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1241 badness = fill_and_eval_dacs(codec, fill_hardwired,
1247 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1248 cfg->line_out_type, fill_hardwired, fill_mio_first,
1250 debug_show_configs(spec, cfg);
1251 if (badness < best_badness) {
1252 best_badness = badness;
1254 best_wired = fill_hardwired;
1255 best_mio = fill_mio_first;
1259 fill_mio_first = !fill_mio_first;
1260 if (!fill_mio_first)
1262 fill_hardwired = !fill_hardwired;
1263 if (!fill_hardwired)
1267 hp_spk_swapped = true;
1268 if (cfg->speaker_outs > 0 &&
1269 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1270 cfg->hp_outs = cfg->line_outs;
1271 memcpy(cfg->hp_pins, cfg->line_out_pins,
1272 sizeof(cfg->hp_pins));
1273 cfg->line_outs = cfg->speaker_outs;
1274 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1275 sizeof(cfg->speaker_pins));
1276 cfg->speaker_outs = 0;
1277 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1278 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1279 fill_hardwired = true;
1282 if (cfg->hp_outs > 0 &&
1283 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1284 cfg->speaker_outs = cfg->line_outs;
1285 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1286 sizeof(cfg->speaker_pins));
1287 cfg->line_outs = cfg->hp_outs;
1288 memcpy(cfg->line_out_pins, cfg->hp_pins,
1289 sizeof(cfg->hp_pins));
1291 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1292 cfg->line_out_type = AUTO_PIN_HP_OUT;
1293 fill_hardwired = true;
1300 debug_badness("==> restoring best_cfg\n");
1302 fill_and_eval_dacs(codec, best_wired, best_mio);
1304 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1305 cfg->line_out_type, best_wired, best_mio);
1306 debug_show_configs(spec, cfg);
1308 if (cfg->line_out_pins[0]) {
1309 struct nid_path *path;
1310 path = snd_hda_get_nid_path(codec,
1311 spec->multiout.dac_nids[0],
1312 cfg->line_out_pins[0]);
1314 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1321 /* add playback controls from the parsed DAC table */
1322 static int create_multi_out_ctls(struct hda_codec *codec,
1323 const struct auto_pin_cfg *cfg)
1325 struct hda_gen_spec *spec = codec->spec;
1326 int i, err, noutputs;
1328 noutputs = cfg->line_outs;
1329 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1330 noutputs += spec->multi_ios;
1332 for (i = 0; i < noutputs; i++) {
1336 struct nid_path *path;
1338 dac = spec->multiout.dac_nids[i];
1341 if (i >= cfg->line_outs) {
1342 pin = spec->multi_io[i - 1].pin;
1344 name = channel_name[i];
1346 pin = cfg->line_out_pins[i];
1347 name = get_line_out_pfx(spec, i, true, &index);
1350 path = snd_hda_get_nid_path(codec, dac, pin);
1353 if (!name || !strcmp(name, "CLFE")) {
1355 err = add_vol_ctl(codec, "Center", 0, 1, path);
1358 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1361 err = add_sw_ctl(codec, "Center", 0, 1, path);
1364 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1368 err = add_stereo_vol(codec, name, index, path);
1371 err = add_stereo_sw(codec, name, index, path);
1379 static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1380 hda_nid_t dac, const char *pfx, int cidx)
1382 struct nid_path *path;
1385 path = snd_hda_get_nid_path(codec, dac, pin);
1388 /* bind volume control will be created in the case of dac = 0 */
1390 err = add_stereo_vol(codec, pfx, cidx, path);
1394 err = add_stereo_sw(codec, pfx, cidx, path);
1400 /* add playback controls for speaker and HP outputs */
1401 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1402 const hda_nid_t *pins, const hda_nid_t *dacs,
1405 struct hda_gen_spec *spec = codec->spec;
1406 struct hda_bind_ctls *ctl;
1410 if (!num_pins || !pins[0])
1413 if (num_pins == 1) {
1414 hda_nid_t dac = *dacs;
1416 dac = spec->multiout.dac_nids[0];
1417 return create_extra_out(codec, *pins, dac, pfx, 0);
1420 for (i = 0; i < num_pins; i++) {
1422 if (dacs[num_pins - 1])
1423 dac = dacs[i]; /* with individual volumes */
1426 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1427 err = create_extra_out(codec, pins[i], dac,
1429 } else if (num_pins >= 3) {
1430 snprintf(name, sizeof(name), "%s %s",
1431 pfx, channel_name[i]);
1432 err = create_extra_out(codec, pins[i], dac, name, 0);
1434 err = create_extra_out(codec, pins[i], dac, pfx, i);
1439 if (dacs[num_pins - 1])
1442 /* Let's create a bind-controls for volumes */
1443 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1447 for (i = 0; i < num_pins; i++) {
1449 struct nid_path *path;
1450 if (!pins[i] || !dacs[i])
1452 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1455 vol = look_for_out_vol_nid(codec, path);
1458 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1461 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1462 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1469 static int create_hp_out_ctls(struct hda_codec *codec)
1471 struct hda_gen_spec *spec = codec->spec;
1472 return create_extra_outs(codec, spec->autocfg.hp_outs,
1473 spec->autocfg.hp_pins,
1474 spec->multiout.hp_out_nid,
1478 static int create_speaker_out_ctls(struct hda_codec *codec)
1480 struct hda_gen_spec *spec = codec->spec;
1481 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1482 spec->autocfg.speaker_pins,
1483 spec->multiout.extra_out_nid,
1488 * channel mode enum control
1491 static int ch_mode_info(struct snd_kcontrol *kcontrol,
1492 struct snd_ctl_elem_info *uinfo)
1494 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1495 struct hda_gen_spec *spec = codec->spec;
1497 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1499 uinfo->value.enumerated.items = spec->multi_ios + 1;
1500 if (uinfo->value.enumerated.item > spec->multi_ios)
1501 uinfo->value.enumerated.item = spec->multi_ios;
1502 sprintf(uinfo->value.enumerated.name, "%dch",
1503 (uinfo->value.enumerated.item + 1) * 2);
1507 static int ch_mode_get(struct snd_kcontrol *kcontrol,
1508 struct snd_ctl_elem_value *ucontrol)
1510 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1511 struct hda_gen_spec *spec = codec->spec;
1512 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1516 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1518 struct hda_gen_spec *spec = codec->spec;
1519 hda_nid_t nid = spec->multi_io[idx].pin;
1520 struct nid_path *path;
1522 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1526 if (path->active == output)
1530 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1531 snd_hda_activate_path(codec, path, true, true);
1532 set_pin_eapd(codec, nid, true);
1534 set_pin_eapd(codec, nid, false);
1535 snd_hda_activate_path(codec, path, false, true);
1536 snd_hda_set_pin_ctl_cache(codec, nid,
1537 spec->multi_io[idx].ctl_in);
1542 static int ch_mode_put(struct snd_kcontrol *kcontrol,
1543 struct snd_ctl_elem_value *ucontrol)
1545 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1546 struct hda_gen_spec *spec = codec->spec;
1549 ch = ucontrol->value.enumerated.item[0];
1550 if (ch < 0 || ch > spec->multi_ios)
1552 if (ch == (spec->ext_channel_count - 1) / 2)
1554 spec->ext_channel_count = (ch + 1) * 2;
1555 for (i = 0; i < spec->multi_ios; i++)
1556 set_multi_io(codec, i, i < ch);
1557 spec->multiout.max_channels = max(spec->ext_channel_count,
1558 spec->const_channel_count);
1559 if (spec->need_dac_fix)
1560 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1564 static const struct snd_kcontrol_new channel_mode_enum = {
1565 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1566 .name = "Channel Mode",
1567 .info = ch_mode_info,
1572 static int create_multi_channel_mode(struct hda_codec *codec)
1574 struct hda_gen_spec *spec = codec->spec;
1576 if (spec->multi_ios > 0) {
1577 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
1584 * shared headphone/mic handling
1587 static void call_update_outputs(struct hda_codec *codec);
1589 /* for shared I/O, change the pin-control accordingly */
1590 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1592 struct hda_gen_spec *spec = codec->spec;
1594 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1595 /* NOTE: this assumes that there are only two inputs, the
1596 * first is the real internal mic and the second is HP/mic jack.
1599 val = snd_hda_get_default_vref(codec, pin);
1601 /* This pin does not have vref caps - let's enable vref on pin 0x18
1602 instead, as suggested by Realtek */
1603 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1604 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1605 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1606 if (vref_val != AC_PINCTL_VREF_HIZ)
1607 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1608 PIN_IN | (set_as_mic ? vref_val : 0));
1611 val = set_as_mic ? val | PIN_IN : PIN_HP;
1612 snd_hda_set_pin_ctl_cache(codec, pin, val);
1614 spec->automute_speaker = !set_as_mic;
1615 call_update_outputs(codec);
1618 /* create a shared input with the headphone out */
1619 static int create_shared_input(struct hda_codec *codec)
1621 struct hda_gen_spec *spec = codec->spec;
1622 struct auto_pin_cfg *cfg = &spec->autocfg;
1623 unsigned int defcfg;
1626 /* only one internal input pin? */
1627 if (cfg->num_inputs != 1)
1629 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1630 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1633 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1634 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1635 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1636 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1638 return 0; /* both not available */
1640 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1641 return 0; /* no input */
1643 cfg->inputs[1].pin = nid;
1644 cfg->inputs[1].type = AUTO_PIN_MIC;
1645 cfg->num_inputs = 2;
1646 spec->shared_mic_hp = 1;
1647 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1657 /* add the powersave loopback-list entry */
1658 static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1660 struct hda_amp_list *list;
1662 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1664 list = spec->loopback_list + spec->num_loopbacks;
1666 list->dir = HDA_INPUT;
1668 spec->num_loopbacks++;
1669 spec->loopback.amplist = spec->loopback_list;
1672 #define add_loopback_list(spec, mix, idx) /* NOP */
1675 /* create input playback/capture controls for the given pin */
1676 static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1677 const char *ctlname, int ctlidx,
1680 struct hda_gen_spec *spec = codec->spec;
1681 struct nid_path *path;
1685 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1686 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1687 return 0; /* no need for analog loopback */
1689 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
1692 print_nid_path("loopback", path);
1694 idx = path->idx[path->depth - 1];
1695 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1696 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1697 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
1700 path->ctls[NID_PATH_VOL_CTL] = val;
1703 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1704 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1705 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
1708 path->ctls[NID_PATH_MUTE_CTL] = val;
1711 path->active = true;
1712 add_loopback_list(spec, mix_nid, idx);
1716 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
1718 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1719 return (pincap & AC_PINCAP_IN) != 0;
1722 /* Parse the codec tree and retrieve ADCs */
1723 static int fill_adc_nids(struct hda_codec *codec)
1725 struct hda_gen_spec *spec = codec->spec;
1727 hda_nid_t *adc_nids = spec->adc_nids;
1728 int max_nums = ARRAY_SIZE(spec->adc_nids);
1731 nid = codec->start_nid;
1732 for (i = 0; i < codec->num_nodes; i++, nid++) {
1733 unsigned int caps = get_wcaps(codec, nid);
1734 int type = get_wcaps_type(caps);
1736 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1738 adc_nids[nums] = nid;
1739 if (++nums >= max_nums)
1742 spec->num_adc_nids = nums;
1746 /* filter out invalid adc_nids that don't give all active input pins;
1747 * if needed, check whether dynamic ADC-switching is available
1749 static int check_dyn_adc_switch(struct hda_codec *codec)
1751 struct hda_gen_spec *spec = codec->spec;
1752 struct hda_input_mux *imux = &spec->input_mux;
1753 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1759 for (n = 0; n < spec->num_adc_nids; n++) {
1760 adc = spec->adc_nids[n];
1761 for (i = 0; i < imux->num_items; i++) {
1762 pin = spec->imux_pins[i];
1763 if (!is_reachable_path(codec, pin, adc))
1766 if (i >= imux->num_items)
1767 adc_nids[nums++] = adc;
1771 if (spec->shared_mic_hp) {
1772 spec->shared_mic_hp = 0;
1773 imux->num_items = 1;
1777 /* check whether ADC-switch is possible */
1778 for (i = 0; i < imux->num_items; i++) {
1779 pin = spec->imux_pins[i];
1780 for (n = 0; n < spec->num_adc_nids; n++) {
1781 adc = spec->adc_nids[n];
1782 if (is_reachable_path(codec, pin, adc)) {
1783 spec->dyn_adc_idx[i] = n;
1789 snd_printdd("hda-codec: enabling ADC switching\n");
1790 spec->dyn_adc_switch = 1;
1791 } else if (nums != spec->num_adc_nids) {
1792 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1793 spec->num_adc_nids = nums;
1796 if (imux->num_items == 1 || spec->shared_mic_hp) {
1797 snd_printdd("hda-codec: reducing to a single ADC\n");
1798 spec->num_adc_nids = 1; /* reduce to a single ADC */
1801 /* single index for individual volumes ctls */
1802 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1803 spec->num_adc_nids = 1;
1809 * create playback/capture controls for input pins
1811 static int create_input_ctls(struct hda_codec *codec)
1813 struct hda_gen_spec *spec = codec->spec;
1814 const struct auto_pin_cfg *cfg = &spec->autocfg;
1815 hda_nid_t mixer = spec->mixer_nid;
1816 struct hda_input_mux *imux = &spec->input_mux;
1818 int i, c, err, type_idx = 0;
1819 const char *prev_label = NULL;
1821 num_adcs = fill_adc_nids(codec);
1825 for (i = 0; i < cfg->num_inputs; i++) {
1830 pin = cfg->inputs[i].pin;
1831 if (!is_input_pin(codec, pin))
1834 label = hda_get_autocfg_input_label(codec, cfg, i);
1835 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1836 label = "Headphone Mic";
1837 if (prev_label && !strcmp(label, prev_label))
1844 if (is_reachable_path(codec, pin, mixer)) {
1845 err = new_analog_input(codec, pin,
1846 label, type_idx, mixer);
1853 for (c = 0; c < num_adcs; c++) {
1854 struct nid_path *path;
1855 hda_nid_t adc = spec->adc_nids[c];
1857 if (!is_reachable_path(codec, pin, adc))
1859 path = snd_array_new(&spec->paths);
1862 memset(path, 0, sizeof(*path));
1863 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
1865 "invalid input path 0x%x -> 0x%x\n",
1870 print_nid_path("input", path);
1873 spec->imux_pins[imux->num_items] = pin;
1874 snd_hda_add_imux_item(imux, label,
1875 imux->num_items, NULL);
1889 /* get the ADC NID corresponding to the given index */
1890 static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1892 struct hda_gen_spec *spec = codec->spec;
1893 if (spec->dyn_adc_switch)
1894 adc_idx = spec->dyn_adc_idx[imux_idx];
1895 return spec->adc_nids[adc_idx];
1898 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
1901 static int mux_enum_info(struct snd_kcontrol *kcontrol,
1902 struct snd_ctl_elem_info *uinfo)
1904 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1905 struct hda_gen_spec *spec = codec->spec;
1906 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1909 static int mux_enum_get(struct snd_kcontrol *kcontrol,
1910 struct snd_ctl_elem_value *ucontrol)
1912 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1913 struct hda_gen_spec *spec = codec->spec;
1914 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1916 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1920 static int mux_enum_put(struct snd_kcontrol *kcontrol,
1921 struct snd_ctl_elem_value *ucontrol)
1923 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1924 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1925 return mux_select(codec, adc_idx,
1926 ucontrol->value.enumerated.item[0]);
1929 static const struct snd_kcontrol_new cap_src_temp = {
1930 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1931 .name = "Input Source",
1932 .info = mux_enum_info,
1933 .get = mux_enum_get,
1934 .put = mux_enum_put,
1938 * capture volume and capture switch ctls
1941 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
1942 struct snd_ctl_elem_value *ucontrol);
1944 /* call the given amp update function for all amps in the imux list at once */
1945 static int cap_put_caller(struct snd_kcontrol *kcontrol,
1946 struct snd_ctl_elem_value *ucontrol,
1947 put_call_t func, int type)
1949 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1950 struct hda_gen_spec *spec = codec->spec;
1951 const struct hda_input_mux *imux;
1952 struct nid_path *path;
1953 int i, adc_idx, err = 0;
1955 imux = &spec->input_mux;
1956 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1957 mutex_lock(&codec->control_mutex);
1958 /* we use the cache-only update at first since multiple input paths
1959 * may shared the same amp; by updating only caches, the redundant
1960 * writes to hardware can be reduced.
1962 codec->cached_write = 1;
1963 for (i = 0; i < imux->num_items; i++) {
1964 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
1965 get_adc_nid(codec, adc_idx, i));
1966 if (!path->ctls[type])
1968 kcontrol->private_value = path->ctls[type];
1969 err = func(kcontrol, ucontrol);
1974 codec->cached_write = 0;
1975 mutex_unlock(&codec->control_mutex);
1976 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
1977 if (err >= 0 && spec->cap_sync_hook)
1978 spec->cap_sync_hook(codec);
1982 /* capture volume ctl callbacks */
1983 #define cap_vol_info snd_hda_mixer_amp_volume_info
1984 #define cap_vol_get snd_hda_mixer_amp_volume_get
1985 #define cap_vol_tlv snd_hda_mixer_amp_tlv
1987 static int cap_vol_put(struct snd_kcontrol *kcontrol,
1988 struct snd_ctl_elem_value *ucontrol)
1990 return cap_put_caller(kcontrol, ucontrol,
1991 snd_hda_mixer_amp_volume_put,
1995 static const struct snd_kcontrol_new cap_vol_temp = {
1996 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1997 .name = "Capture Volume",
1998 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
1999 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2000 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2001 .info = cap_vol_info,
2004 .tlv = { .c = cap_vol_tlv },
2007 /* capture switch ctl callbacks */
2008 #define cap_sw_info snd_ctl_boolean_stereo_info
2009 #define cap_sw_get snd_hda_mixer_amp_switch_get
2011 static int cap_sw_put(struct snd_kcontrol *kcontrol,
2012 struct snd_ctl_elem_value *ucontrol)
2014 return cap_put_caller(kcontrol, ucontrol,
2015 snd_hda_mixer_amp_switch_put,
2019 static const struct snd_kcontrol_new cap_sw_temp = {
2020 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2021 .name = "Capture Switch",
2022 .info = cap_sw_info,
2027 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2032 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2033 for (depth = 0; depth < 3; depth++) {
2034 if (depth >= path->depth)
2036 i = path->depth - depth - 1;
2037 nid = path->path[i];
2038 if (!path->ctls[NID_PATH_VOL_CTL]) {
2039 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2040 path->ctls[NID_PATH_VOL_CTL] =
2041 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2042 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2043 int idx = path->idx[i];
2044 if (!depth && codec->single_adc_amp)
2046 path->ctls[NID_PATH_VOL_CTL] =
2047 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2050 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2051 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2052 path->ctls[NID_PATH_MUTE_CTL] =
2053 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2054 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2055 int idx = path->idx[i];
2056 if (!depth && codec->single_adc_amp)
2058 path->ctls[NID_PATH_MUTE_CTL] =
2059 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2066 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2068 struct hda_gen_spec *spec = codec->spec;
2069 struct auto_pin_cfg *cfg = &spec->autocfg;
2073 if (!spec->inv_dmic_split)
2075 for (i = 0; i < cfg->num_inputs; i++) {
2076 if (cfg->inputs[i].pin != nid)
2078 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2080 val = snd_hda_codec_get_pincfg(codec, nid);
2081 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2086 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2087 int idx, bool is_switch, unsigned int ctl,
2090 struct hda_gen_spec *spec = codec->spec;
2092 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2093 const char *sfx = is_switch ? "Switch" : "Volume";
2094 unsigned int chs = inv_dmic ? 1 : 3;
2101 snprintf(tmpname, sizeof(tmpname),
2102 "%s Capture %s", label, sfx);
2104 snprintf(tmpname, sizeof(tmpname),
2106 err = add_control(spec, type, tmpname, idx,
2107 amp_val_replace_channels(ctl, chs));
2108 if (err < 0 || !inv_dmic)
2111 /* Make independent right kcontrol */
2113 snprintf(tmpname, sizeof(tmpname),
2114 "Inverted %s Capture %s", label, sfx);
2116 snprintf(tmpname, sizeof(tmpname),
2117 "Inverted Capture %s", sfx);
2118 return add_control(spec, type, tmpname, idx,
2119 amp_val_replace_channels(ctl, 2));
2122 /* create single (and simple) capture volume and switch controls */
2123 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2124 unsigned int vol_ctl, unsigned int sw_ctl,
2128 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2131 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2137 /* create bound capture volume and switch controls */
2138 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2139 unsigned int vol_ctl, unsigned int sw_ctl)
2141 struct hda_gen_spec *spec = codec->spec;
2142 struct snd_kcontrol_new *knew;
2145 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
2149 knew->private_value = vol_ctl;
2150 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2153 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
2157 knew->private_value = sw_ctl;
2158 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2163 /* return the vol ctl when used first in the imux list */
2164 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2166 struct hda_gen_spec *spec = codec->spec;
2167 struct nid_path *path;
2171 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2172 get_adc_nid(codec, 0, idx));
2175 ctl = path->ctls[type];
2178 for (i = 0; i < idx - 1; i++) {
2179 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2180 get_adc_nid(codec, 0, i));
2181 if (path && path->ctls[type] == ctl)
2187 /* create individual capture volume and switch controls per input */
2188 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2190 struct hda_gen_spec *spec = codec->spec;
2191 struct hda_input_mux *imux = &spec->input_mux;
2192 int i, err, type, type_idx = 0;
2193 const char *prev_label = NULL;
2195 for (i = 0; i < imux->num_items; i++) {
2198 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2199 if (prev_label && !strcmp(label, prev_label))
2204 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2206 for (type = 0; type < 2; type++) {
2207 err = add_single_cap_ctl(codec, label, type_idx, type,
2208 get_first_cap_ctl(codec, i, type),
2217 static int create_capture_mixers(struct hda_codec *codec)
2219 struct hda_gen_spec *spec = codec->spec;
2220 struct hda_input_mux *imux = &spec->input_mux;
2221 int i, n, nums, err;
2223 if (spec->dyn_adc_switch)
2226 nums = spec->num_adc_nids;
2228 if (!spec->auto_mic && imux->num_items > 1) {
2229 struct snd_kcontrol_new *knew;
2231 name = nums > 1 ? "Input Source" : "Capture Source";
2232 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
2238 for (n = 0; n < nums; n++) {
2240 bool inv_dmic = false;
2244 for (i = 0; i < imux->num_items; i++) {
2245 struct nid_path *path;
2246 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2247 get_adc_nid(codec, n, i));
2250 parse_capvol_in_path(codec, path);
2252 vol = path->ctls[NID_PATH_VOL_CTL];
2253 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2256 sw = path->ctls[NID_PATH_MUTE_CTL];
2257 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2259 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2264 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2266 else if (!spec->multi_cap_vol)
2267 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2269 err = create_multi_cap_vol_ctl(codec);
2278 * add mic boosts if needed
2280 static int parse_mic_boost(struct hda_codec *codec)
2282 struct hda_gen_spec *spec = codec->spec;
2283 struct auto_pin_cfg *cfg = &spec->autocfg;
2287 const char *prev_label = NULL;
2289 for (i = 0; i < cfg->num_inputs; i++) {
2290 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2292 nid = cfg->inputs[i].pin;
2293 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2295 char boost_label[32];
2296 struct nid_path *path;
2299 label = hda_get_autocfg_input_label(codec, cfg, i);
2300 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2301 label = "Headphone Mic";
2302 if (prev_label && !strcmp(label, prev_label))
2308 snprintf(boost_label, sizeof(boost_label),
2309 "%s Boost Volume", label);
2310 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2311 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2312 boost_label, type_idx, val);
2316 path = snd_hda_get_nid_path(codec, nid, 0);
2318 path->ctls[NID_PATH_BOOST_CTL] = val;
2325 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2327 static void parse_digital(struct hda_codec *codec)
2329 struct hda_gen_spec *spec = codec->spec;
2330 struct nid_path *path;
2334 /* support multiple SPDIFs; the secondary is set up as a slave */
2336 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2337 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2338 dig_nid = look_for_dac(codec, pin, true);
2341 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
2344 print_nid_path("digout", path);
2346 spec->multiout.dig_out_nid = dig_nid;
2347 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2349 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2350 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2352 spec->slave_dig_outs[nums - 1] = dig_nid;
2357 if (spec->autocfg.dig_in_pin) {
2358 dig_nid = codec->start_nid;
2359 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2360 unsigned int wcaps = get_wcaps(codec, dig_nid);
2361 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2363 if (!(wcaps & AC_WCAP_DIGITAL))
2365 path = snd_hda_add_new_path(codec,
2366 spec->autocfg.dig_in_pin,
2367 dig_nid, HDA_PARSE_ALL);
2369 print_nid_path("digin", path);
2370 path->active = true;
2371 spec->dig_in_nid = dig_nid;
2380 * input MUX handling
2383 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2385 /* select the given imux item; either unmute exclusively or select the route */
2386 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2389 struct hda_gen_spec *spec = codec->spec;
2390 const struct hda_input_mux *imux;
2391 struct nid_path *path;
2393 imux = &spec->input_mux;
2394 if (!imux->num_items)
2397 if (idx >= imux->num_items)
2398 idx = imux->num_items - 1;
2399 if (spec->cur_mux[adc_idx] == idx)
2402 path = snd_hda_get_nid_path(codec,
2403 spec->imux_pins[spec->cur_mux[adc_idx]],
2404 spec->adc_nids[adc_idx]);
2408 snd_hda_activate_path(codec, path, false, false);
2410 spec->cur_mux[adc_idx] = idx;
2412 if (spec->shared_mic_hp)
2413 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2415 if (spec->dyn_adc_switch)
2416 dyn_adc_pcm_resetup(codec, idx);
2418 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2419 get_adc_nid(codec, adc_idx, idx));
2424 snd_hda_activate_path(codec, path, true, false);
2425 if (spec->cap_sync_hook)
2426 spec->cap_sync_hook(codec);
2432 * Jack detections for HP auto-mute and mic-switch
2435 /* check each pin in the given array; returns true if any of them is plugged */
2436 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2440 for (i = 0; i < num_pins; i++) {
2441 hda_nid_t nid = pins[i];
2444 present |= snd_hda_jack_detect(codec, nid);
2449 /* standard HP/line-out auto-mute helper */
2450 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2451 bool mute, bool hp_out)
2453 struct hda_gen_spec *spec = codec->spec;
2454 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2457 for (i = 0; i < num_pins; i++) {
2458 hda_nid_t nid = pins[i];
2462 /* don't reset VREF value in case it's controlling
2463 * the amp (see alc861_fixup_asus_amp_vref_0f())
2465 if (spec->keep_vref_in_automute) {
2466 val = snd_hda_codec_read(codec, nid, 0,
2467 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2472 snd_hda_set_pin_ctl_cache(codec, nid, val);
2473 set_pin_eapd(codec, nid, !mute);
2477 /* Toggle outputs muting */
2478 void snd_hda_gen_update_outputs(struct hda_codec *codec)
2480 struct hda_gen_spec *spec = codec->spec;
2483 /* Control HP pins/amps depending on master_mute state;
2484 * in general, HP pins/amps control should be enabled in all cases,
2485 * but currently set only for master_mute, just to be safe
2487 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2488 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2489 spec->autocfg.hp_pins, spec->master_mute, true);
2491 if (!spec->automute_speaker)
2494 on = spec->hp_jack_present | spec->line_jack_present;
2495 on |= spec->master_mute;
2496 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2497 spec->autocfg.speaker_pins, on, false);
2499 /* toggle line-out mutes if needed, too */
2500 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2501 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2502 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2504 if (!spec->automute_lo)
2507 on = spec->hp_jack_present;
2508 on |= spec->master_mute;
2509 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2510 spec->autocfg.line_out_pins, on, false);
2512 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
2514 static void call_update_outputs(struct hda_codec *codec)
2516 struct hda_gen_spec *spec = codec->spec;
2517 if (spec->automute_hook)
2518 spec->automute_hook(codec);
2520 snd_hda_gen_update_outputs(codec);
2523 /* standard HP-automute helper */
2524 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2526 struct hda_gen_spec *spec = codec->spec;
2528 spec->hp_jack_present =
2529 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2530 spec->autocfg.hp_pins);
2531 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2533 call_update_outputs(codec);
2535 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
2537 /* standard line-out-automute helper */
2538 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2540 struct hda_gen_spec *spec = codec->spec;
2542 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2544 /* check LO jack only when it's different from HP */
2545 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2548 spec->line_jack_present =
2549 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2550 spec->autocfg.line_out_pins);
2551 if (!spec->automute_speaker || !spec->detect_lo)
2553 call_update_outputs(codec);
2555 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
2557 /* standard mic auto-switch helper */
2558 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
2560 struct hda_gen_spec *spec = codec->spec;
2563 if (!spec->auto_mic)
2566 for (i = spec->am_num_entries - 1; i > 0; i--) {
2567 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2568 mux_select(codec, 0, spec->am_entry[i].idx);
2572 mux_select(codec, 0, spec->am_entry[0].idx);
2574 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
2577 * Auto-Mute mode mixer enum support
2579 static int automute_mode_info(struct snd_kcontrol *kcontrol,
2580 struct snd_ctl_elem_info *uinfo)
2582 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2583 struct hda_gen_spec *spec = codec->spec;
2584 static const char * const texts3[] = {
2585 "Disabled", "Speaker Only", "Line Out+Speaker"
2588 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2589 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2590 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2593 static int automute_mode_get(struct snd_kcontrol *kcontrol,
2594 struct snd_ctl_elem_value *ucontrol)
2596 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2597 struct hda_gen_spec *spec = codec->spec;
2598 unsigned int val = 0;
2599 if (spec->automute_speaker)
2601 if (spec->automute_lo)
2604 ucontrol->value.enumerated.item[0] = val;
2608 static int automute_mode_put(struct snd_kcontrol *kcontrol,
2609 struct snd_ctl_elem_value *ucontrol)
2611 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2612 struct hda_gen_spec *spec = codec->spec;
2614 switch (ucontrol->value.enumerated.item[0]) {
2616 if (!spec->automute_speaker && !spec->automute_lo)
2618 spec->automute_speaker = 0;
2619 spec->automute_lo = 0;
2622 if (spec->automute_speaker_possible) {
2623 if (!spec->automute_lo && spec->automute_speaker)
2625 spec->automute_speaker = 1;
2626 spec->automute_lo = 0;
2627 } else if (spec->automute_lo_possible) {
2628 if (spec->automute_lo)
2630 spec->automute_lo = 1;
2635 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2637 if (spec->automute_speaker && spec->automute_lo)
2639 spec->automute_speaker = 1;
2640 spec->automute_lo = 1;
2645 call_update_outputs(codec);
2649 static const struct snd_kcontrol_new automute_mode_enum = {
2650 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2651 .name = "Auto-Mute Mode",
2652 .info = automute_mode_info,
2653 .get = automute_mode_get,
2654 .put = automute_mode_put,
2657 static int add_automute_mode_enum(struct hda_codec *codec)
2659 struct hda_gen_spec *spec = codec->spec;
2661 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
2667 * Check the availability of HP/line-out auto-mute;
2668 * Set up appropriately if really supported
2670 static int check_auto_mute_availability(struct hda_codec *codec)
2672 struct hda_gen_spec *spec = codec->spec;
2673 struct auto_pin_cfg *cfg = &spec->autocfg;
2677 if (cfg->hp_pins[0])
2679 if (cfg->line_out_pins[0])
2681 if (cfg->speaker_pins[0])
2683 if (present < 2) /* need two different output types */
2686 if (!cfg->speaker_pins[0] &&
2687 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2688 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2689 sizeof(cfg->speaker_pins));
2690 cfg->speaker_outs = cfg->line_outs;
2693 if (!cfg->hp_pins[0] &&
2694 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2695 memcpy(cfg->hp_pins, cfg->line_out_pins,
2696 sizeof(cfg->hp_pins));
2697 cfg->hp_outs = cfg->line_outs;
2700 for (i = 0; i < cfg->hp_outs; i++) {
2701 hda_nid_t nid = cfg->hp_pins[i];
2702 if (!is_jack_detectable(codec, nid))
2704 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2706 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
2707 snd_hda_gen_hp_automute);
2708 spec->detect_hp = 1;
2711 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2712 if (cfg->speaker_outs)
2713 for (i = 0; i < cfg->line_outs; i++) {
2714 hda_nid_t nid = cfg->line_out_pins[i];
2715 if (!is_jack_detectable(codec, nid))
2717 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2718 snd_hda_jack_detect_enable_callback(codec, nid,
2719 HDA_GEN_FRONT_EVENT,
2720 snd_hda_gen_line_automute);
2721 spec->detect_lo = 1;
2723 spec->automute_lo_possible = spec->detect_hp;
2726 spec->automute_speaker_possible = cfg->speaker_outs &&
2727 (spec->detect_hp || spec->detect_lo);
2729 spec->automute_lo = spec->automute_lo_possible;
2730 spec->automute_speaker = spec->automute_speaker_possible;
2732 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2733 /* create a control for automute mode */
2734 err = add_automute_mode_enum(codec);
2741 /* return the position of NID in the list, or -1 if not found */
2742 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2745 for (i = 0; i < nums; i++)
2751 /* check whether all auto-mic pins are valid; setup indices if OK */
2752 static bool auto_mic_check_imux(struct hda_codec *codec)
2754 struct hda_gen_spec *spec = codec->spec;
2755 const struct hda_input_mux *imux;
2758 imux = &spec->input_mux;
2759 for (i = 0; i < spec->am_num_entries; i++) {
2760 spec->am_entry[i].idx =
2761 find_idx_in_nid_list(spec->am_entry[i].pin,
2762 spec->imux_pins, imux->num_items);
2763 if (spec->am_entry[i].idx < 0)
2764 return false; /* no corresponding imux */
2767 /* we don't need the jack detection for the first pin */
2768 for (i = 1; i < spec->am_num_entries; i++)
2769 snd_hda_jack_detect_enable_callback(codec,
2770 spec->am_entry[i].pin,
2772 snd_hda_gen_mic_autoswitch);
2776 static int compare_attr(const void *ap, const void *bp)
2778 const struct automic_entry *a = ap;
2779 const struct automic_entry *b = bp;
2780 return (int)(a->attr - b->attr);
2784 * Check the availability of auto-mic switch;
2785 * Set up if really supported
2787 static int check_auto_mic_availability(struct hda_codec *codec)
2789 struct hda_gen_spec *spec = codec->spec;
2790 struct auto_pin_cfg *cfg = &spec->autocfg;
2796 for (i = 0; i < cfg->num_inputs; i++) {
2797 hda_nid_t nid = cfg->inputs[i].pin;
2799 attr = snd_hda_codec_get_pincfg(codec, nid);
2800 attr = snd_hda_get_input_pin_attr(attr);
2801 if (types & (1 << attr))
2802 return 0; /* already occupied */
2804 case INPUT_PIN_ATTR_INT:
2805 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2806 return 0; /* invalid type */
2808 case INPUT_PIN_ATTR_UNUSED:
2809 return 0; /* invalid entry */
2811 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2812 return 0; /* invalid type */
2813 if (!spec->line_in_auto_switch &&
2814 cfg->inputs[i].type != AUTO_PIN_MIC)
2815 return 0; /* only mic is allowed */
2816 if (!is_jack_detectable(codec, nid))
2817 return 0; /* no unsol support */
2820 if (num_pins >= MAX_AUTO_MIC_PINS)
2822 types |= (1 << attr);
2823 spec->am_entry[num_pins].pin = nid;
2824 spec->am_entry[num_pins].attr = attr;
2831 spec->am_num_entries = num_pins;
2832 /* sort the am_entry in the order of attr so that the pin with a
2833 * higher attr will be selected when the jack is plugged.
2835 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2836 compare_attr, NULL);
2838 if (!auto_mic_check_imux(codec))
2842 spec->num_adc_nids = 1;
2843 spec->cur_mux[0] = spec->am_entry[0].idx;
2844 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2845 spec->am_entry[0].pin,
2846 spec->am_entry[1].pin,
2847 spec->am_entry[2].pin);
2854 * Parse the given BIOS configuration and set up the hda_gen_spec
2856 * return 1 if successful, 0 if the proper config is not found,
2857 * or a negative error code
2859 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
2860 struct auto_pin_cfg *cfg)
2862 struct hda_gen_spec *spec = codec->spec;
2865 if (cfg != &spec->autocfg) {
2866 spec->autocfg = *cfg;
2867 cfg = &spec->autocfg;
2870 if (!cfg->line_outs) {
2871 if (cfg->dig_outs || cfg->dig_in_pin) {
2872 spec->multiout.max_channels = 2;
2873 spec->no_analog = 1;
2876 return 0; /* can't find valid BIOS pin config */
2879 if (!spec->no_primary_hp &&
2880 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2881 cfg->line_outs <= cfg->hp_outs) {
2882 /* use HP as primary out */
2883 cfg->speaker_outs = cfg->line_outs;
2884 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2885 sizeof(cfg->speaker_pins));
2886 cfg->line_outs = cfg->hp_outs;
2887 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2889 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2890 cfg->line_out_type = AUTO_PIN_HP_OUT;
2893 err = parse_output_paths(codec);
2896 err = create_multi_channel_mode(codec);
2899 err = create_multi_out_ctls(codec, cfg);
2902 err = create_hp_out_ctls(codec);
2905 err = create_speaker_out_ctls(codec);
2908 err = create_shared_input(codec);
2911 err = create_input_ctls(codec);
2915 /* check the multiple speaker pins */
2916 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2917 spec->const_channel_count = cfg->line_outs * 2;
2919 spec->const_channel_count = cfg->speaker_outs * 2;
2921 if (spec->multi_ios > 0)
2922 spec->multiout.max_channels = max(spec->ext_channel_count,
2923 spec->const_channel_count);
2925 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2927 err = check_auto_mute_availability(codec);
2931 err = check_dyn_adc_switch(codec);
2935 if (!spec->shared_mic_hp) {
2936 err = check_auto_mic_availability(codec);
2941 err = create_capture_mixers(codec);
2945 err = parse_mic_boost(codec);
2950 parse_digital(codec);
2954 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
2958 * Build control elements
2961 /* slave controls for virtual master */
2962 static const char * const slave_pfxs[] = {
2963 "Front", "Surround", "Center", "LFE", "Side",
2964 "Headphone", "Speaker", "Mono", "Line Out",
2965 "CLFE", "Bass Speaker", "PCM",
2969 int snd_hda_gen_build_controls(struct hda_codec *codec)
2971 struct hda_gen_spec *spec = codec->spec;
2974 if (spec->kctls.used) {
2975 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
2980 if (spec->multiout.dig_out_nid) {
2981 err = snd_hda_create_dig_out_ctls(codec,
2982 spec->multiout.dig_out_nid,
2983 spec->multiout.dig_out_nid,
2984 spec->pcm_rec[1].pcm_type);
2987 if (!spec->no_analog) {
2988 err = snd_hda_create_spdif_share_sw(codec,
2992 spec->multiout.share_spdif = 1;
2995 if (spec->dig_in_nid) {
2996 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3001 /* if we have no master control, let's create it */
3002 if (!spec->no_analog &&
3003 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3004 unsigned int vmaster_tlv[4];
3005 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3006 HDA_OUTPUT, vmaster_tlv);
3007 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3008 vmaster_tlv, slave_pfxs,
3013 if (!spec->no_analog &&
3014 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3015 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3018 true, &spec->vmaster_mute.sw_kctl);
3021 if (spec->vmaster_mute.hook)
3022 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3023 spec->vmaster_mute_enum);
3026 free_kctls(spec); /* no longer needed */
3028 if (spec->shared_mic_hp) {
3030 int nid = spec->autocfg.inputs[1].pin;
3031 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3034 err = snd_hda_jack_detect_enable(codec, nid, 0);
3039 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3045 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3053 * Analog playback callbacks
3055 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3056 struct hda_codec *codec,
3057 struct snd_pcm_substream *substream)
3059 struct hda_gen_spec *spec = codec->spec;
3060 return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
3064 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3065 struct hda_codec *codec,
3066 unsigned int stream_tag,
3067 unsigned int format,
3068 struct snd_pcm_substream *substream)
3070 struct hda_gen_spec *spec = codec->spec;
3071 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3072 stream_tag, format, substream);
3075 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3076 struct hda_codec *codec,
3077 struct snd_pcm_substream *substream)
3079 struct hda_gen_spec *spec = codec->spec;
3080 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3086 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3087 struct hda_codec *codec,
3088 struct snd_pcm_substream *substream)
3090 struct hda_gen_spec *spec = codec->spec;
3091 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3094 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3095 struct hda_codec *codec,
3096 unsigned int stream_tag,
3097 unsigned int format,
3098 struct snd_pcm_substream *substream)
3100 struct hda_gen_spec *spec = codec->spec;
3101 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3102 stream_tag, format, substream);
3105 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3106 struct hda_codec *codec,
3107 struct snd_pcm_substream *substream)
3109 struct hda_gen_spec *spec = codec->spec;
3110 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3113 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3114 struct hda_codec *codec,
3115 struct snd_pcm_substream *substream)
3117 struct hda_gen_spec *spec = codec->spec;
3118 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3124 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3125 struct hda_codec *codec,
3126 unsigned int stream_tag,
3127 unsigned int format,
3128 struct snd_pcm_substream *substream)
3130 struct hda_gen_spec *spec = codec->spec;
3132 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
3133 stream_tag, 0, format);
3137 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3138 struct hda_codec *codec,
3139 struct snd_pcm_substream *substream)
3141 struct hda_gen_spec *spec = codec->spec;
3143 snd_hda_codec_cleanup_stream(codec,
3144 spec->adc_nids[substream->number + 1]);
3150 static const struct hda_pcm_stream pcm_analog_playback = {
3154 /* NID is set in build_pcms */
3156 .open = playback_pcm_open,
3157 .prepare = playback_pcm_prepare,
3158 .cleanup = playback_pcm_cleanup
3162 static const struct hda_pcm_stream pcm_analog_capture = {
3166 /* NID is set in build_pcms */
3169 static const struct hda_pcm_stream pcm_analog_alt_playback = {
3173 /* NID is set in build_pcms */
3176 static const struct hda_pcm_stream pcm_analog_alt_capture = {
3177 .substreams = 2, /* can be overridden */
3180 /* NID is set in build_pcms */
3182 .prepare = alt_capture_pcm_prepare,
3183 .cleanup = alt_capture_pcm_cleanup
3187 static const struct hda_pcm_stream pcm_digital_playback = {
3191 /* NID is set in build_pcms */
3193 .open = dig_playback_pcm_open,
3194 .close = dig_playback_pcm_close,
3195 .prepare = dig_playback_pcm_prepare,
3196 .cleanup = dig_playback_pcm_cleanup
3200 static const struct hda_pcm_stream pcm_digital_capture = {
3204 /* NID is set in build_pcms */
3207 /* Used by build_pcms to flag that a PCM has no playback stream */
3208 static const struct hda_pcm_stream pcm_null_stream = {
3215 * dynamic changing ADC PCM streams
3217 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3219 struct hda_gen_spec *spec = codec->spec;
3220 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3222 if (spec->cur_adc && spec->cur_adc != new_adc) {
3223 /* stream is running, let's swap the current ADC */
3224 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3225 spec->cur_adc = new_adc;
3226 snd_hda_codec_setup_stream(codec, new_adc,
3227 spec->cur_adc_stream_tag, 0,
3228 spec->cur_adc_format);
3234 /* analog capture with dynamic dual-adc changes */
3235 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3236 struct hda_codec *codec,
3237 unsigned int stream_tag,
3238 unsigned int format,
3239 struct snd_pcm_substream *substream)
3241 struct hda_gen_spec *spec = codec->spec;
3242 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3243 spec->cur_adc_stream_tag = stream_tag;
3244 spec->cur_adc_format = format;
3245 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3249 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3250 struct hda_codec *codec,
3251 struct snd_pcm_substream *substream)
3253 struct hda_gen_spec *spec = codec->spec;
3254 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3259 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3263 .nid = 0, /* fill later */
3265 .prepare = dyn_adc_capture_pcm_prepare,
3266 .cleanup = dyn_adc_capture_pcm_cleanup
3270 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3271 const char *chip_name)
3277 strlcpy(str, chip_name, len);
3279 /* drop non-alnum chars after a space */
3280 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3281 if (!isalnum(p[1])) {
3286 strlcat(str, sfx, len);
3289 /* build PCM streams based on the parsed results */
3290 int snd_hda_gen_build_pcms(struct hda_codec *codec)
3292 struct hda_gen_spec *spec = codec->spec;
3293 struct hda_pcm *info = spec->pcm_rec;
3294 const struct hda_pcm_stream *p;
3295 bool have_multi_adcs;
3297 codec->num_pcms = 1;
3298 codec->pcm_info = info;
3300 if (spec->no_analog)
3303 fill_pcm_stream_name(spec->stream_name_analog,
3304 sizeof(spec->stream_name_analog),
3305 " Analog", codec->chip_name);
3306 info->name = spec->stream_name_analog;
3308 if (spec->multiout.num_dacs > 0) {
3309 p = spec->stream_analog_playback;
3311 p = &pcm_analog_playback;
3312 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3313 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3314 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3315 spec->multiout.max_channels;
3316 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3317 spec->autocfg.line_outs == 2)
3318 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3321 if (spec->num_adc_nids) {
3322 p = spec->stream_analog_capture;
3324 if (spec->dyn_adc_switch)
3325 p = &dyn_adc_pcm_analog_capture;
3327 p = &pcm_analog_capture;
3329 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3330 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3334 /* SPDIF for stream index #1 */
3335 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
3336 fill_pcm_stream_name(spec->stream_name_digital,
3337 sizeof(spec->stream_name_digital),
3338 " Digital", codec->chip_name);
3339 codec->num_pcms = 2;
3340 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3341 info = spec->pcm_rec + 1;
3342 info->name = spec->stream_name_digital;
3343 if (spec->dig_out_type)
3344 info->pcm_type = spec->dig_out_type;
3346 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3347 if (spec->multiout.dig_out_nid) {
3348 p = spec->stream_digital_playback;
3350 p = &pcm_digital_playback;
3351 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3352 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3354 if (spec->dig_in_nid) {
3355 p = spec->stream_digital_capture;
3357 p = &pcm_digital_capture;
3358 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3359 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3363 if (spec->no_analog)
3366 /* If the use of more than one ADC is requested for the current
3367 * model, configure a second analog capture-only PCM.
3369 have_multi_adcs = (spec->num_adc_nids > 1) &&
3370 !spec->dyn_adc_switch && !spec->auto_mic;
3371 /* Additional Analaog capture for index #2 */
3372 if (spec->alt_dac_nid || have_multi_adcs) {
3373 codec->num_pcms = 3;
3374 info = spec->pcm_rec + 2;
3375 info->name = spec->stream_name_analog;
3376 if (spec->alt_dac_nid) {
3377 p = spec->stream_analog_alt_playback;
3379 p = &pcm_analog_alt_playback;
3380 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3381 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3384 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3386 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3388 if (have_multi_adcs) {
3389 p = spec->stream_analog_alt_capture;
3391 p = &pcm_analog_alt_capture;
3392 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3393 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3395 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3396 spec->num_adc_nids - 1;
3398 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3400 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3406 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3410 * Standard auto-parser initializations
3413 /* configure the path from the given dac to the pin as the proper output */
3414 static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3415 int pin_type, hda_nid_t dac)
3417 struct nid_path *path;
3419 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3420 path = snd_hda_get_nid_path(codec, dac, pin);
3425 snd_hda_activate_path(codec, path, true, true);
3426 set_pin_eapd(codec, pin, true);
3429 /* initialize primary output paths */
3430 static void init_multi_out(struct hda_codec *codec)
3432 struct hda_gen_spec *spec = codec->spec;
3437 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3442 for (i = 0; i < spec->autocfg.line_outs; i++) {
3443 nid = spec->autocfg.line_out_pins[i];
3445 dac = spec->multiout.dac_nids[i];
3447 dac = spec->multiout.dac_nids[0];
3448 set_output_and_unmute(codec, nid, pin_type, dac);
3454 static void __init_extra_out(struct hda_codec *codec, int num_outs,
3455 hda_nid_t *pins, hda_nid_t *dacs, int type)
3457 struct hda_gen_spec *spec = codec->spec;
3461 for (i = 0; i < num_outs; i++) {
3467 if (i > 0 && dacs[0])
3470 dac = spec->multiout.dac_nids[0];
3472 set_output_and_unmute(codec, pin, type, dac);
3476 /* initialize hp and speaker paths */
3477 static void init_extra_out(struct hda_codec *codec)
3479 struct hda_gen_spec *spec = codec->spec;
3481 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3482 __init_extra_out(codec, spec->autocfg.hp_outs,
3483 spec->autocfg.hp_pins,
3484 spec->multiout.hp_out_nid, PIN_HP);
3485 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3486 __init_extra_out(codec, spec->autocfg.speaker_outs,
3487 spec->autocfg.speaker_pins,
3488 spec->multiout.extra_out_nid, PIN_OUT);
3491 /* initialize multi-io paths */
3492 static void init_multi_io(struct hda_codec *codec)
3494 struct hda_gen_spec *spec = codec->spec;
3497 for (i = 0; i < spec->multi_ios; i++) {
3498 hda_nid_t pin = spec->multi_io[i].pin;
3499 struct nid_path *path;
3500 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3503 if (!spec->multi_io[i].ctl_in)
3504 spec->multi_io[i].ctl_in =
3505 snd_hda_codec_update_cache(codec, pin, 0,
3506 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3507 snd_hda_activate_path(codec, path, path->active, true);
3511 /* set up the input pin config, depending on the given auto-pin type */
3512 static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3515 unsigned int val = PIN_IN;
3516 if (auto_pin_type == AUTO_PIN_MIC)
3517 val |= snd_hda_get_default_vref(codec, nid);
3518 snd_hda_set_pin_ctl_cache(codec, nid, val);
3521 /* set up input pins and loopback paths */
3522 static void init_analog_input(struct hda_codec *codec)
3524 struct hda_gen_spec *spec = codec->spec;
3525 struct auto_pin_cfg *cfg = &spec->autocfg;
3528 for (i = 0; i < cfg->num_inputs; i++) {
3529 hda_nid_t nid = cfg->inputs[i].pin;
3530 if (is_input_pin(codec, nid))
3531 set_input_pin(codec, nid, cfg->inputs[i].type);
3533 /* init loopback inputs */
3534 if (spec->mixer_nid) {
3535 struct nid_path *path;
3536 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3538 snd_hda_activate_path(codec, path,
3539 path->active, false);
3544 /* initialize ADC paths */
3545 static void init_input_src(struct hda_codec *codec)
3547 struct hda_gen_spec *spec = codec->spec;
3548 struct hda_input_mux *imux = &spec->input_mux;
3549 struct nid_path *path;
3552 if (spec->dyn_adc_switch)
3555 nums = spec->num_adc_nids;
3557 for (c = 0; c < nums; c++) {
3558 for (i = 0; i < imux->num_items; i++) {
3559 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3560 get_adc_nid(codec, c, i));
3562 bool active = path->active;
3563 if (i == spec->cur_mux[c])
3565 snd_hda_activate_path(codec, path, active, false);
3570 if (spec->shared_mic_hp)
3571 update_shared_mic_hp(codec, spec->cur_mux[0]);
3573 if (spec->cap_sync_hook)
3574 spec->cap_sync_hook(codec);
3577 /* set right pin controls for digital I/O */
3578 static void init_digital(struct hda_codec *codec)
3580 struct hda_gen_spec *spec = codec->spec;
3584 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3585 pin = spec->autocfg.dig_out_pins[i];
3588 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3590 pin = spec->autocfg.dig_in_pin;
3592 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
3595 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3596 * invalid unsol tags by some reason
3598 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3602 for (i = 0; i < codec->init_pins.used; i++) {
3603 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3604 hda_nid_t nid = pin->nid;
3605 if (is_jack_detectable(codec, nid) &&
3606 !snd_hda_jack_tbl_get(codec, nid))
3607 snd_hda_codec_update_cache(codec, nid, 0,
3608 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3612 int snd_hda_gen_init(struct hda_codec *codec)
3614 struct hda_gen_spec *spec = codec->spec;
3616 if (spec->init_hook)
3617 spec->init_hook(codec);
3619 snd_hda_apply_verbs(codec);
3621 codec->cached_write = 1;
3623 init_multi_out(codec);
3624 init_extra_out(codec);
3625 init_multi_io(codec);
3626 init_analog_input(codec);
3627 init_input_src(codec);
3628 init_digital(codec);
3630 clear_unsol_on_unused_pins(codec);
3632 /* call init functions of standard auto-mute helpers */
3633 snd_hda_gen_hp_automute(codec, NULL);
3634 snd_hda_gen_line_automute(codec, NULL);
3635 snd_hda_gen_mic_autoswitch(codec, NULL);
3637 snd_hda_codec_flush_amp_cache(codec);
3638 snd_hda_codec_flush_cmd_cache(codec);
3640 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3641 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3643 hda_call_check_power_status(codec, 0x01);
3646 EXPORT_SYMBOL(snd_hda_gen_init);
3650 * the generic codec support
3654 static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3656 struct hda_gen_spec *spec = codec->spec;
3657 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3661 static void generic_free(struct hda_codec *codec)
3663 snd_hda_gen_spec_free(codec->spec);
3668 static const struct hda_codec_ops generic_patch_ops = {
3669 .build_controls = snd_hda_gen_build_controls,
3670 .build_pcms = snd_hda_gen_build_pcms,
3671 .init = snd_hda_gen_init,
3672 .free = generic_free,
3673 .unsol_event = snd_hda_jack_unsol_event,
3675 .check_power_status = generic_check_power_status,
3679 int snd_hda_parse_generic_codec(struct hda_codec *codec)
3681 struct hda_gen_spec *spec;
3684 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3687 snd_hda_gen_spec_init(spec);
3690 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3694 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
3698 codec->patch_ops = generic_patch_ops;
3702 generic_free(codec);
3705 EXPORT_SYMBOL(snd_hda_parse_generic_codec);