2 * Universal Interface for Intel High Definition Audio Codec
4 * Generic widget tree parser
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/sort.h>
27 #include <linux/ctype.h>
28 #include <linux/string.h>
29 #include <sound/core.h>
30 #include <sound/jack.h>
31 #include "hda_codec.h"
32 #include "hda_local.h"
33 #include "hda_auto_parser.h"
35 #include "hda_generic.h"
38 /* initialize hda_gen_spec struct */
39 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
41 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
42 snd_array_init(&spec->bind_ctls, sizeof(struct hda_bind_ctls *), 8);
43 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
44 mutex_init(&spec->pcm_mutex);
47 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
49 struct snd_kcontrol_new *
50 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
51 const struct snd_kcontrol_new *temp)
53 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
58 knew->name = kstrdup(name, GFP_KERNEL);
60 knew->name = kstrdup(knew->name, GFP_KERNEL);
65 EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
67 static void free_kctls(struct hda_gen_spec *spec)
69 if (spec->kctls.list) {
70 struct snd_kcontrol_new *kctl = spec->kctls.list;
72 for (i = 0; i < spec->kctls.used; i++)
75 snd_array_free(&spec->kctls);
78 static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
80 struct hda_ctl_ops *ops)
82 struct hda_gen_spec *spec = codec->spec;
83 struct hda_bind_ctls **ctlp, *ctl;
84 ctlp = snd_array_new(&spec->bind_ctls);
87 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
94 static void free_bind_ctls(struct hda_gen_spec *spec)
96 if (spec->bind_ctls.list) {
97 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
99 for (i = 0; i < spec->bind_ctls.used; i++)
102 snd_array_free(&spec->bind_ctls);
105 void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
110 free_bind_ctls(spec);
111 snd_array_free(&spec->paths);
113 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
119 /* get the path between the given NIDs;
120 * passing 0 to either @pin or @dac behaves as a wildcard
122 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
123 hda_nid_t from_nid, hda_nid_t to_nid)
125 struct hda_gen_spec *spec = codec->spec;
128 for (i = 0; i < spec->paths.used; i++) {
129 struct nid_path *path = snd_array_elem(&spec->paths, i);
130 if (path->depth <= 0)
132 if ((!from_nid || path->path[0] == from_nid) &&
133 (!to_nid || path->path[path->depth - 1] == to_nid))
138 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
140 /* check whether the given DAC is already found in any existing paths */
141 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
143 struct hda_gen_spec *spec = codec->spec;
146 for (i = 0; i < spec->paths.used; i++) {
147 struct nid_path *path = snd_array_elem(&spec->paths, i);
148 if (path->path[0] == nid)
154 /* check whether the given two widgets can be connected */
155 static bool is_reachable_path(struct hda_codec *codec,
156 hda_nid_t from_nid, hda_nid_t to_nid)
158 if (!from_nid || !to_nid)
160 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
163 /* nid, dir and idx */
164 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
166 /* check whether the given ctl is already assigned in any path elements */
167 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
169 struct hda_gen_spec *spec = codec->spec;
172 val &= AMP_VAL_COMPARE_MASK;
173 for (i = 0; i < spec->paths.used; i++) {
174 struct nid_path *path = snd_array_elem(&spec->paths, i);
175 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
181 /* check whether a control with the given (nid, dir, idx) was assigned */
182 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
185 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
186 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
187 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
190 static void print_nid_path(const char *pfx, struct nid_path *path)
197 for (i = 0; i < path->depth; i++) {
199 sprintf(tmp, ":%02x", path->path[i]);
200 strlcat(buf, tmp, sizeof(buf));
202 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
205 /* called recursively */
206 static bool __parse_nid_path(struct hda_codec *codec,
207 hda_nid_t from_nid, hda_nid_t to_nid,
208 int with_aa_mix, struct nid_path *path, int depth)
210 struct hda_gen_spec *spec = codec->spec;
211 const hda_nid_t *conn;
214 if (to_nid == spec->mixer_nid) {
215 if (with_aa_mix == HDA_PARSE_NO_AAMIX)
217 with_aa_mix = HDA_PARSE_ALL; /* mark aa-mix is included */
220 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
221 for (i = 0; i < nums; i++) {
222 if (conn[i] != from_nid) {
223 /* special case: when from_nid is 0,
224 * try to find an empty DAC
227 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
228 is_dac_already_used(codec, conn[i]))
231 /* aa-mix is requested but not included? */
232 if (!(spec->mixer_nid && with_aa_mix == HDA_PARSE_ONLY_AAMIX))
235 if (depth >= MAX_NID_PATH_DEPTH)
237 for (i = 0; i < nums; i++) {
239 type = get_wcaps_type(get_wcaps(codec, conn[i]));
240 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
243 if (__parse_nid_path(codec, from_nid, conn[i],
244 with_aa_mix, path, depth + 1))
250 path->path[path->depth] = conn[i];
251 path->idx[path->depth + 1] = i;
252 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
253 path->multi[path->depth + 1] = 1;
258 /* parse the widget path from the given nid to the target nid;
259 * when @from_nid is 0, try to find an empty DAC;
260 * when @with_aa_mix is HDA_PARSE_NO_AAMIX, paths with spec->mixer_nid are
261 * excluded, only the paths that don't go through the mixer will be chosen.
262 * when @with_aa_mix is HDA_PARSE_ONLY_AAMIX, only the paths going through
263 * spec->mixer_nid will be chosen.
264 * when @with_aa_mix is HDA_PARSE_ALL, no special handling about mixer widget.
266 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
267 hda_nid_t to_nid, int with_aa_mix,
268 struct nid_path *path)
270 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
271 path->path[path->depth] = to_nid;
277 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
280 * parse the path between the given NIDs and add to the path list.
281 * if no valid path is found, return NULL
284 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
285 hda_nid_t to_nid, int with_aa_mix)
287 struct hda_gen_spec *spec = codec->spec;
288 struct nid_path *path;
290 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
293 path = snd_array_new(&spec->paths);
296 memset(path, 0, sizeof(*path));
297 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
303 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
305 /* look for an empty DAC slot */
306 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
309 struct hda_gen_spec *spec = codec->spec;
313 for (i = 0; i < spec->num_all_dacs; i++) {
314 hda_nid_t nid = spec->all_dacs[i];
315 if (!nid || is_dac_already_used(codec, nid))
317 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
318 if (is_digital != cap_digital)
320 if (is_reachable_path(codec, nid, pin))
326 /* replace the channels in the composed amp value with the given number */
327 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
329 val &= ~(0x3U << 16);
334 /* check whether the widget has the given amp capability for the direction */
335 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
336 int dir, unsigned int bits)
340 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
341 if (query_amp_caps(codec, nid, dir) & bits)
346 #define nid_has_mute(codec, nid, dir) \
347 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
348 #define nid_has_volume(codec, nid, dir) \
349 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
351 /* look for a widget suitable for assigning a mute switch in the path */
352 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
353 struct nid_path *path)
357 for (i = path->depth - 1; i >= 0; i--) {
358 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
359 return path->path[i];
360 if (i != path->depth - 1 && i != 0 &&
361 nid_has_mute(codec, path->path[i], HDA_INPUT))
362 return path->path[i];
367 /* look for a widget suitable for assigning a volume ctl in the path */
368 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
369 struct nid_path *path)
373 for (i = path->depth - 1; i >= 0; i--) {
374 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
375 return path->path[i];
381 * path activation / deactivation
384 /* can have the amp-in capability? */
385 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
387 hda_nid_t nid = path->path[idx];
388 unsigned int caps = get_wcaps(codec, nid);
389 unsigned int type = get_wcaps_type(caps);
391 if (!(caps & AC_WCAP_IN_AMP))
393 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
398 /* can have the amp-out capability? */
399 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
401 hda_nid_t nid = path->path[idx];
402 unsigned int caps = get_wcaps(codec, nid);
403 unsigned int type = get_wcaps_type(caps);
405 if (!(caps & AC_WCAP_OUT_AMP))
407 if (type == AC_WID_PIN && !idx) /* only for output pins */
412 /* check whether the given (nid,dir,idx) is active */
413 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
414 unsigned int idx, unsigned int dir)
416 struct hda_gen_spec *spec = codec->spec;
419 for (n = 0; n < spec->paths.used; n++) {
420 struct nid_path *path = snd_array_elem(&spec->paths, n);
423 for (i = 0; i < path->depth; i++) {
424 if (path->path[i] == nid) {
425 if (dir == HDA_OUTPUT || path->idx[i] == idx)
434 /* get the default amp value for the target state */
435 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
436 int dir, bool enable)
439 unsigned int val = 0;
441 caps = query_amp_caps(codec, nid, dir);
442 if (caps & AC_AMPCAP_NUM_STEPS) {
445 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
447 if (caps & AC_AMPCAP_MUTE) {
454 /* initialize the amp value (only at the first time) */
455 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
457 int val = get_amp_val_to_activate(codec, nid, dir, false);
458 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
461 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
462 int idx, bool enable)
465 if (is_ctl_associated(codec, nid, dir, idx) ||
466 is_active_nid(codec, nid, dir, idx))
468 val = get_amp_val_to_activate(codec, nid, dir, enable);
469 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
472 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
475 hda_nid_t nid = path->path[i];
476 init_amp(codec, nid, HDA_OUTPUT, 0);
477 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
480 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
481 int i, bool enable, bool add_aamix)
483 struct hda_gen_spec *spec = codec->spec;
484 const hda_nid_t *conn;
487 hda_nid_t nid = path->path[i];
489 nums = snd_hda_get_conn_list(codec, nid, &conn);
490 type = get_wcaps_type(get_wcaps(codec, nid));
491 if (type == AC_WID_PIN ||
492 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
498 for (n = 0; n < nums; n++)
499 init_amp(codec, nid, HDA_INPUT, n);
501 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
504 /* here is a little bit tricky in comparison with activate_amp_out();
505 * when aa-mixer is available, we need to enable the path as well
507 for (n = 0; n < nums; n++) {
508 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
510 activate_amp(codec, nid, HDA_INPUT, n, enable);
514 /* activate or deactivate the given path
515 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
517 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
518 bool enable, bool add_aamix)
523 path->active = false;
525 for (i = path->depth - 1; i >= 0; i--) {
526 if (enable && path->multi[i])
527 snd_hda_codec_write_cache(codec, path->path[i], 0,
528 AC_VERB_SET_CONNECT_SEL,
530 if (has_amp_in(codec, path, i))
531 activate_amp_in(codec, path, i, enable, add_aamix);
532 if (has_amp_out(codec, path, i))
533 activate_amp_out(codec, path, i, enable);
539 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
541 /* turn on/off EAPD on the given pin */
542 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
544 struct hda_gen_spec *spec = codec->spec;
545 if (spec->own_eapd_ctl ||
546 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
550 snd_hda_codec_update_cache(codec, pin, 0,
551 AC_VERB_SET_EAPD_BTLENABLE,
552 enable ? 0x02 : 0x00);
557 * Helper functions for creating mixer ctl elements
567 static const struct snd_kcontrol_new control_templates[] = {
568 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
569 HDA_CODEC_MUTE(NULL, 0, 0, 0),
570 HDA_BIND_MUTE(NULL, 0, 0, 0),
571 HDA_BIND_VOL(NULL, 0),
572 HDA_BIND_SW(NULL, 0),
575 /* add dynamic controls from template */
576 static int add_control(struct hda_gen_spec *spec, int type, const char *name,
577 int cidx, unsigned long val)
579 struct snd_kcontrol_new *knew;
581 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
585 if (get_amp_nid_(val))
586 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
587 knew->private_value = val;
591 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
592 const char *pfx, const char *dir,
593 const char *sfx, int cidx, unsigned long val)
596 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
597 return add_control(spec, type, name, cidx, val);
600 #define add_pb_vol_ctrl(spec, type, pfx, val) \
601 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
602 #define add_pb_sw_ctrl(spec, type, pfx, val) \
603 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
604 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
605 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
606 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
607 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
609 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
610 unsigned int chs, struct nid_path *path)
615 val = path->ctls[NID_PATH_VOL_CTL];
618 val = amp_val_replace_channels(val, chs);
619 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
622 /* return the channel bits suitable for the given path->ctls[] */
623 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
626 int chs = 1; /* mono (left only) */
628 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
629 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
630 chs = 3; /* stereo */
635 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
636 struct nid_path *path)
638 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
639 return add_vol_ctl(codec, pfx, cidx, chs, path);
642 /* create a mute-switch for the given mixer widget;
643 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
645 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
646 unsigned int chs, struct nid_path *path)
649 int type = HDA_CTL_WIDGET_MUTE;
653 val = path->ctls[NID_PATH_MUTE_CTL];
656 val = amp_val_replace_channels(val, chs);
657 if (get_amp_direction_(val) == HDA_INPUT) {
658 hda_nid_t nid = get_amp_nid_(val);
659 int nums = snd_hda_get_num_conns(codec, nid);
661 type = HDA_CTL_BIND_MUTE;
665 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
668 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
669 int cidx, struct nid_path *path)
671 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
672 return add_sw_ctl(codec, pfx, cidx, chs, path);
675 static const char * const channel_name[4] = {
676 "Front", "Surround", "CLFE", "Side"
679 /* give some appropriate ctl name prefix for the given line out channel */
680 static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
681 bool can_be_master, int *index)
683 struct auto_pin_cfg *cfg = &spec->autocfg;
686 if (cfg->line_outs == 1 && !spec->multi_ios &&
687 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
688 return spec->vmaster_mute.hook ? "PCM" : "Master";
690 /* if there is really a single DAC used in the whole output paths,
691 * use it master (or "PCM" if a vmaster hook is present)
693 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
694 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
695 return spec->vmaster_mute.hook ? "PCM" : "Master";
697 switch (cfg->line_out_type) {
698 case AUTO_PIN_SPEAKER_OUT:
699 if (cfg->line_outs == 1)
701 if (cfg->line_outs == 2)
702 return ch ? "Bass Speaker" : "Speaker";
704 case AUTO_PIN_HP_OUT:
705 /* for multi-io case, only the primary out */
706 if (ch && spec->multi_ios)
711 if (cfg->line_outs == 1 && !spec->multi_ios)
715 if (ch >= ARRAY_SIZE(channel_name)) {
720 return channel_name[ch];
727 /* badness definition */
729 /* No primary DAC is found for the main output */
730 BAD_NO_PRIMARY_DAC = 0x10000,
731 /* No DAC is found for the extra output */
733 /* No possible multi-ios */
734 BAD_MULTI_IO = 0x103,
735 /* No individual DAC for extra output */
736 BAD_NO_EXTRA_DAC = 0x102,
737 /* No individual DAC for extra surrounds */
738 BAD_NO_EXTRA_SURR_DAC = 0x101,
739 /* Primary DAC shared with main surrounds */
740 BAD_SHARED_SURROUND = 0x100,
741 /* Primary DAC shared with main CLFE */
742 BAD_SHARED_CLFE = 0x10,
743 /* Primary DAC shared with extra surrounds */
744 BAD_SHARED_EXTRA_SURROUND = 0x10,
745 /* Volume widget is shared */
746 BAD_SHARED_VOL = 0x10,
749 /* look for widgets in the path between the given NIDs appropriate for
750 * volume and mute controls, and assign the values to ctls[].
752 * When no appropriate widget is found in the path, the badness value
753 * is incremented depending on the situation. The function returns the
754 * total badness for both volume and mute controls.
756 static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
759 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
765 return BAD_SHARED_VOL * 2;
766 nid = look_for_out_vol_nid(codec, path);
768 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
769 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
770 badness += BAD_SHARED_VOL;
772 path->ctls[NID_PATH_VOL_CTL] = val;
774 badness += BAD_SHARED_VOL;
775 nid = look_for_out_mute_nid(codec, path);
777 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
778 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
779 nid_has_mute(codec, nid, HDA_OUTPUT))
780 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
782 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
783 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
784 badness += BAD_SHARED_VOL;
786 path->ctls[NID_PATH_MUTE_CTL] = val;
788 badness += BAD_SHARED_VOL;
792 struct badness_table {
793 int no_primary_dac; /* no primary DAC */
794 int no_dac; /* no secondary DACs */
795 int shared_primary; /* primary DAC is shared with main output */
796 int shared_surr; /* secondary DAC shared with main or primary */
797 int shared_clfe; /* third DAC shared with main or primary */
798 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
801 static struct badness_table main_out_badness = {
802 .no_primary_dac = BAD_NO_PRIMARY_DAC,
803 .no_dac = BAD_NO_DAC,
804 .shared_primary = BAD_NO_PRIMARY_DAC,
805 .shared_surr = BAD_SHARED_SURROUND,
806 .shared_clfe = BAD_SHARED_CLFE,
807 .shared_surr_main = BAD_SHARED_SURROUND,
810 static struct badness_table extra_out_badness = {
811 .no_primary_dac = BAD_NO_DAC,
812 .no_dac = BAD_NO_DAC,
813 .shared_primary = BAD_NO_EXTRA_DAC,
814 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
815 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
816 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
819 /* try to assign DACs to pins and return the resultant badness */
820 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
821 const hda_nid_t *pins, hda_nid_t *dacs,
822 const struct badness_table *bad)
824 struct hda_gen_spec *spec = codec->spec;
825 struct auto_pin_cfg *cfg = &spec->autocfg;
833 for (i = 0; i < num_outs; i++) {
834 struct nid_path *path;
835 hda_nid_t pin = pins[i];
837 dacs[i] = look_for_dac(codec, pin, false);
838 if (!dacs[i] && !i) {
839 for (j = 1; j < num_outs; j++) {
840 if (is_reachable_path(codec, dacs[j], pin)) {
849 if (is_reachable_path(codec, dacs[0], pin))
851 else if (cfg->line_outs > i &&
852 is_reachable_path(codec, spec->private_dac_nids[i], pin))
853 dac = spec->private_dac_nids[i];
856 badness += bad->shared_primary;
858 badness += bad->shared_surr;
860 badness += bad->shared_clfe;
861 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
862 dac = spec->private_dac_nids[0];
863 badness += bad->shared_surr_main;
865 badness += bad->no_primary_dac;
867 badness += bad->no_dac;
869 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_NO_AAMIX);
870 if (!path && i > 0 && spec->mixer_nid) {
872 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_ALL);
877 print_nid_path("output", path);
879 badness += assign_out_path_ctls(codec, pin, dac);
885 /* return NID if the given pin has only a single connection to a certain DAC */
886 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
888 struct hda_gen_spec *spec = codec->spec;
890 hda_nid_t nid_found = 0;
892 for (i = 0; i < spec->num_all_dacs; i++) {
893 hda_nid_t nid = spec->all_dacs[i];
894 if (!nid || is_dac_already_used(codec, nid))
896 if (is_reachable_path(codec, nid, pin)) {
905 /* check whether the given pin can be a multi-io pin */
906 static bool can_be_multiio_pin(struct hda_codec *codec,
907 unsigned int location, hda_nid_t nid)
909 unsigned int defcfg, caps;
911 defcfg = snd_hda_codec_get_pincfg(codec, nid);
912 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
914 if (location && get_defcfg_location(defcfg) != location)
916 caps = snd_hda_query_pin_caps(codec, nid);
917 if (!(caps & AC_PINCAP_OUT))
925 * When hardwired is set, try to fill ony hardwired pins, and returns
926 * zero if any pins are filled, non-zero if nothing found.
927 * When hardwired is off, try to fill possible input pins, and returns
930 static int fill_multi_ios(struct hda_codec *codec,
931 hda_nid_t reference_pin,
932 bool hardwired, int offset)
934 struct hda_gen_spec *spec = codec->spec;
935 struct auto_pin_cfg *cfg = &spec->autocfg;
936 int type, i, j, dacs, num_pins, old_pins;
937 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
938 unsigned int location = get_defcfg_location(defcfg);
941 old_pins = spec->multi_ios;
946 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
947 for (i = 0; i < cfg->num_inputs; i++) {
948 if (cfg->inputs[i].type != type)
950 if (can_be_multiio_pin(codec, location,
958 dacs = spec->multiout.num_dacs;
959 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
960 for (i = 0; i < cfg->num_inputs; i++) {
961 struct nid_path *path;
962 hda_nid_t nid = cfg->inputs[i].pin;
965 if (cfg->inputs[i].type != type)
967 if (!can_be_multiio_pin(codec, location, nid))
969 for (j = 0; j < spec->multi_ios; j++) {
970 if (nid == spec->multi_io[j].pin)
973 if (j < spec->multi_ios)
976 if (offset && offset + spec->multi_ios < dacs) {
977 dac = spec->private_dac_nids[offset + spec->multi_ios];
978 if (!is_reachable_path(codec, dac, nid))
982 dac = get_dac_if_single(codec, nid);
984 dac = look_for_dac(codec, nid, false);
989 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
994 print_nid_path("multiio", path);
995 spec->multi_io[spec->multi_ios].pin = nid;
996 spec->multi_io[spec->multi_ios].dac = dac;
998 if (spec->multi_ios >= 2)
1004 badness = BAD_MULTI_IO;
1005 if (old_pins == spec->multi_ios) {
1007 return 1; /* nothing found */
1009 return badness; /* no badness if nothing found */
1011 if (!hardwired && spec->multi_ios < 2) {
1012 /* cancel newly assigned paths */
1013 spec->paths.used -= spec->multi_ios - old_pins;
1014 spec->multi_ios = old_pins;
1018 /* assign volume and mute controls */
1019 for (i = old_pins; i < spec->multi_ios; i++)
1020 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1021 spec->multi_io[i].dac);
1026 /* map DACs for all pins in the list if they are single connections */
1027 static bool map_singles(struct hda_codec *codec, int outs,
1028 const hda_nid_t *pins, hda_nid_t *dacs)
1030 struct hda_gen_spec *spec = codec->spec;
1033 for (i = 0; i < outs; i++) {
1034 struct nid_path *path;
1038 dac = get_dac_if_single(codec, pins[i]);
1041 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
1042 if (!path && i > 0 && spec->mixer_nid)
1043 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
1047 print_nid_path("output", path);
1053 /* fill in the dac_nids table from the parsed pin configuration */
1054 static int fill_and_eval_dacs(struct hda_codec *codec,
1055 bool fill_hardwired,
1056 bool fill_mio_first)
1058 struct hda_gen_spec *spec = codec->spec;
1059 struct auto_pin_cfg *cfg = &spec->autocfg;
1060 int i, err, badness;
1062 /* set num_dacs once to full for look_for_dac() */
1063 spec->multiout.num_dacs = cfg->line_outs;
1064 spec->multiout.dac_nids = spec->private_dac_nids;
1065 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1066 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1067 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1068 spec->multi_ios = 0;
1069 snd_array_free(&spec->paths);
1072 /* fill hard-wired DACs first */
1073 if (fill_hardwired) {
1076 mapped = map_singles(codec, cfg->line_outs,
1078 spec->private_dac_nids);
1079 mapped |= map_singles(codec, cfg->hp_outs,
1081 spec->multiout.hp_out_nid);
1082 mapped |= map_singles(codec, cfg->speaker_outs,
1084 spec->multiout.extra_out_nid);
1085 if (fill_mio_first && cfg->line_outs == 1 &&
1086 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1087 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1094 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1095 spec->private_dac_nids,
1098 /* re-count num_dacs and squash invalid entries */
1099 spec->multiout.num_dacs = 0;
1100 for (i = 0; i < cfg->line_outs; i++) {
1101 if (spec->private_dac_nids[i])
1102 spec->multiout.num_dacs++;
1104 memmove(spec->private_dac_nids + i,
1105 spec->private_dac_nids + i + 1,
1106 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1107 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1111 if (fill_mio_first &&
1112 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1113 /* try to fill multi-io first */
1114 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1117 /* we don't count badness at this stage yet */
1120 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1121 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1122 spec->multiout.hp_out_nid,
1123 &extra_out_badness);
1128 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1129 err = try_assign_dacs(codec, cfg->speaker_outs,
1131 spec->multiout.extra_out_nid,
1132 &extra_out_badness);
1137 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1138 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1143 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1144 /* try multi-ios with HP + inputs */
1146 if (cfg->line_outs >= 3)
1148 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1154 if (spec->multi_ios == 2) {
1155 for (i = 0; i < 2; i++)
1156 spec->private_dac_nids[spec->multiout.num_dacs++] =
1157 spec->multi_io[i].dac;
1158 spec->ext_channel_count = 2;
1159 } else if (spec->multi_ios) {
1160 spec->multi_ios = 0;
1161 badness += BAD_MULTI_IO;
1167 #define DEBUG_BADNESS
1169 #ifdef DEBUG_BADNESS
1170 #define debug_badness snd_printdd
1172 #define debug_badness(...)
1175 static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1177 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1178 cfg->line_out_pins[0], cfg->line_out_pins[1],
1179 cfg->line_out_pins[2], cfg->line_out_pins[3],
1180 spec->multiout.dac_nids[0],
1181 spec->multiout.dac_nids[1],
1182 spec->multiout.dac_nids[2],
1183 spec->multiout.dac_nids[3]);
1184 if (spec->multi_ios > 0)
1185 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1187 spec->multi_io[0].pin, spec->multi_io[1].pin,
1188 spec->multi_io[0].dac, spec->multi_io[1].dac);
1189 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1190 cfg->hp_pins[0], cfg->hp_pins[1],
1191 cfg->hp_pins[2], cfg->hp_pins[3],
1192 spec->multiout.hp_out_nid[0],
1193 spec->multiout.hp_out_nid[1],
1194 spec->multiout.hp_out_nid[2],
1195 spec->multiout.hp_out_nid[3]);
1196 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1197 cfg->speaker_pins[0], cfg->speaker_pins[1],
1198 cfg->speaker_pins[2], cfg->speaker_pins[3],
1199 spec->multiout.extra_out_nid[0],
1200 spec->multiout.extra_out_nid[1],
1201 spec->multiout.extra_out_nid[2],
1202 spec->multiout.extra_out_nid[3]);
1205 /* find all available DACs of the codec */
1206 static void fill_all_dac_nids(struct hda_codec *codec)
1208 struct hda_gen_spec *spec = codec->spec;
1210 hda_nid_t nid = codec->start_nid;
1212 spec->num_all_dacs = 0;
1213 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1214 for (i = 0; i < codec->num_nodes; i++, nid++) {
1215 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1217 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1218 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1221 spec->all_dacs[spec->num_all_dacs++] = nid;
1225 static int parse_output_paths(struct hda_codec *codec)
1227 struct hda_gen_spec *spec = codec->spec;
1228 struct auto_pin_cfg *cfg = &spec->autocfg;
1229 struct auto_pin_cfg *best_cfg;
1230 int best_badness = INT_MAX;
1232 bool fill_hardwired = true, fill_mio_first = true;
1233 bool best_wired = true, best_mio = true;
1234 bool hp_spk_swapped = false;
1236 fill_all_dac_nids(codec);
1238 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1244 badness = fill_and_eval_dacs(codec, fill_hardwired,
1250 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1251 cfg->line_out_type, fill_hardwired, fill_mio_first,
1253 debug_show_configs(spec, cfg);
1254 if (badness < best_badness) {
1255 best_badness = badness;
1257 best_wired = fill_hardwired;
1258 best_mio = fill_mio_first;
1262 fill_mio_first = !fill_mio_first;
1263 if (!fill_mio_first)
1265 fill_hardwired = !fill_hardwired;
1266 if (!fill_hardwired)
1270 hp_spk_swapped = true;
1271 if (cfg->speaker_outs > 0 &&
1272 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1273 cfg->hp_outs = cfg->line_outs;
1274 memcpy(cfg->hp_pins, cfg->line_out_pins,
1275 sizeof(cfg->hp_pins));
1276 cfg->line_outs = cfg->speaker_outs;
1277 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1278 sizeof(cfg->speaker_pins));
1279 cfg->speaker_outs = 0;
1280 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1281 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1282 fill_hardwired = true;
1285 if (cfg->hp_outs > 0 &&
1286 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1287 cfg->speaker_outs = cfg->line_outs;
1288 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1289 sizeof(cfg->speaker_pins));
1290 cfg->line_outs = cfg->hp_outs;
1291 memcpy(cfg->line_out_pins, cfg->hp_pins,
1292 sizeof(cfg->hp_pins));
1294 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1295 cfg->line_out_type = AUTO_PIN_HP_OUT;
1296 fill_hardwired = true;
1303 debug_badness("==> restoring best_cfg\n");
1305 fill_and_eval_dacs(codec, best_wired, best_mio);
1307 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1308 cfg->line_out_type, best_wired, best_mio);
1309 debug_show_configs(spec, cfg);
1311 if (cfg->line_out_pins[0]) {
1312 struct nid_path *path;
1313 path = snd_hda_get_nid_path(codec,
1314 spec->multiout.dac_nids[0],
1315 cfg->line_out_pins[0]);
1317 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1324 /* add playback controls from the parsed DAC table */
1325 static int create_multi_out_ctls(struct hda_codec *codec,
1326 const struct auto_pin_cfg *cfg)
1328 struct hda_gen_spec *spec = codec->spec;
1329 int i, err, noutputs;
1331 noutputs = cfg->line_outs;
1332 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1333 noutputs += spec->multi_ios;
1335 for (i = 0; i < noutputs; i++) {
1339 struct nid_path *path;
1341 dac = spec->multiout.dac_nids[i];
1344 if (i >= cfg->line_outs) {
1345 pin = spec->multi_io[i - 1].pin;
1347 name = channel_name[i];
1349 pin = cfg->line_out_pins[i];
1350 name = get_line_out_pfx(spec, i, true, &index);
1353 path = snd_hda_get_nid_path(codec, dac, pin);
1356 if (!name || !strcmp(name, "CLFE")) {
1358 err = add_vol_ctl(codec, "Center", 0, 1, path);
1361 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1364 err = add_sw_ctl(codec, "Center", 0, 1, path);
1367 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1371 err = add_stereo_vol(codec, name, index, path);
1374 err = add_stereo_sw(codec, name, index, path);
1382 static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1383 hda_nid_t dac, const char *pfx, int cidx)
1385 struct nid_path *path;
1388 path = snd_hda_get_nid_path(codec, dac, pin);
1391 /* bind volume control will be created in the case of dac = 0 */
1393 err = add_stereo_vol(codec, pfx, cidx, path);
1397 err = add_stereo_sw(codec, pfx, cidx, path);
1403 /* add playback controls for speaker and HP outputs */
1404 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1405 const hda_nid_t *pins, const hda_nid_t *dacs,
1408 struct hda_gen_spec *spec = codec->spec;
1409 struct hda_bind_ctls *ctl;
1413 if (!num_pins || !pins[0])
1416 if (num_pins == 1) {
1417 hda_nid_t dac = *dacs;
1419 dac = spec->multiout.dac_nids[0];
1420 return create_extra_out(codec, *pins, dac, pfx, 0);
1423 for (i = 0; i < num_pins; i++) {
1425 if (dacs[num_pins - 1])
1426 dac = dacs[i]; /* with individual volumes */
1429 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1430 err = create_extra_out(codec, pins[i], dac,
1432 } else if (num_pins >= 3) {
1433 snprintf(name, sizeof(name), "%s %s",
1434 pfx, channel_name[i]);
1435 err = create_extra_out(codec, pins[i], dac, name, 0);
1437 err = create_extra_out(codec, pins[i], dac, pfx, i);
1442 if (dacs[num_pins - 1])
1445 /* Let's create a bind-controls for volumes */
1446 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1450 for (i = 0; i < num_pins; i++) {
1452 struct nid_path *path;
1453 if (!pins[i] || !dacs[i])
1455 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1458 vol = look_for_out_vol_nid(codec, path);
1461 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1464 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1465 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1472 static int create_hp_out_ctls(struct hda_codec *codec)
1474 struct hda_gen_spec *spec = codec->spec;
1475 return create_extra_outs(codec, spec->autocfg.hp_outs,
1476 spec->autocfg.hp_pins,
1477 spec->multiout.hp_out_nid,
1481 static int create_speaker_out_ctls(struct hda_codec *codec)
1483 struct hda_gen_spec *spec = codec->spec;
1484 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1485 spec->autocfg.speaker_pins,
1486 spec->multiout.extra_out_nid,
1491 * independent HP controls
1494 static int indep_hp_info(struct snd_kcontrol *kcontrol,
1495 struct snd_ctl_elem_info *uinfo)
1497 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1500 static int indep_hp_get(struct snd_kcontrol *kcontrol,
1501 struct snd_ctl_elem_value *ucontrol)
1503 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1504 struct hda_gen_spec *spec = codec->spec;
1505 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1509 static int indep_hp_put(struct snd_kcontrol *kcontrol,
1510 struct snd_ctl_elem_value *ucontrol)
1512 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1513 struct hda_gen_spec *spec = codec->spec;
1514 unsigned int select = ucontrol->value.enumerated.item[0];
1517 mutex_lock(&spec->pcm_mutex);
1518 if (spec->active_streams) {
1523 if (spec->indep_hp_enabled != select) {
1524 spec->indep_hp_enabled = select;
1525 if (spec->indep_hp_enabled)
1526 spec->multiout.hp_out_nid[0] = 0;
1528 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1532 mutex_unlock(&spec->pcm_mutex);
1536 static const struct snd_kcontrol_new indep_hp_ctl = {
1537 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1538 .name = "Independent HP",
1539 .info = indep_hp_info,
1540 .get = indep_hp_get,
1541 .put = indep_hp_put,
1545 static int create_indep_hp_ctls(struct hda_codec *codec)
1547 struct hda_gen_spec *spec = codec->spec;
1549 if (!spec->indep_hp)
1551 if (!spec->multiout.hp_out_nid[0]) {
1556 spec->indep_hp_enabled = false;
1557 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1558 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1564 * channel mode enum control
1567 static int ch_mode_info(struct snd_kcontrol *kcontrol,
1568 struct snd_ctl_elem_info *uinfo)
1570 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1571 struct hda_gen_spec *spec = codec->spec;
1573 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1575 uinfo->value.enumerated.items = spec->multi_ios + 1;
1576 if (uinfo->value.enumerated.item > spec->multi_ios)
1577 uinfo->value.enumerated.item = spec->multi_ios;
1578 sprintf(uinfo->value.enumerated.name, "%dch",
1579 (uinfo->value.enumerated.item + 1) * 2);
1583 static int ch_mode_get(struct snd_kcontrol *kcontrol,
1584 struct snd_ctl_elem_value *ucontrol)
1586 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1587 struct hda_gen_spec *spec = codec->spec;
1588 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1592 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1594 struct hda_gen_spec *spec = codec->spec;
1595 hda_nid_t nid = spec->multi_io[idx].pin;
1596 struct nid_path *path;
1598 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1602 if (path->active == output)
1606 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1607 snd_hda_activate_path(codec, path, true, true);
1608 set_pin_eapd(codec, nid, true);
1610 set_pin_eapd(codec, nid, false);
1611 snd_hda_activate_path(codec, path, false, true);
1612 snd_hda_set_pin_ctl_cache(codec, nid,
1613 spec->multi_io[idx].ctl_in);
1618 static int ch_mode_put(struct snd_kcontrol *kcontrol,
1619 struct snd_ctl_elem_value *ucontrol)
1621 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1622 struct hda_gen_spec *spec = codec->spec;
1625 ch = ucontrol->value.enumerated.item[0];
1626 if (ch < 0 || ch > spec->multi_ios)
1628 if (ch == (spec->ext_channel_count - 1) / 2)
1630 spec->ext_channel_count = (ch + 1) * 2;
1631 for (i = 0; i < spec->multi_ios; i++)
1632 set_multi_io(codec, i, i < ch);
1633 spec->multiout.max_channels = max(spec->ext_channel_count,
1634 spec->const_channel_count);
1635 if (spec->need_dac_fix)
1636 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1640 static const struct snd_kcontrol_new channel_mode_enum = {
1641 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1642 .name = "Channel Mode",
1643 .info = ch_mode_info,
1648 static int create_multi_channel_mode(struct hda_codec *codec)
1650 struct hda_gen_spec *spec = codec->spec;
1652 if (spec->multi_ios > 0) {
1653 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
1660 * shared headphone/mic handling
1663 static void call_update_outputs(struct hda_codec *codec);
1665 /* for shared I/O, change the pin-control accordingly */
1666 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1668 struct hda_gen_spec *spec = codec->spec;
1670 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1671 /* NOTE: this assumes that there are only two inputs, the
1672 * first is the real internal mic and the second is HP/mic jack.
1675 val = snd_hda_get_default_vref(codec, pin);
1677 /* This pin does not have vref caps - let's enable vref on pin 0x18
1678 instead, as suggested by Realtek */
1679 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1680 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1681 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1682 if (vref_val != AC_PINCTL_VREF_HIZ)
1683 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1684 PIN_IN | (set_as_mic ? vref_val : 0));
1687 val = set_as_mic ? val | PIN_IN : PIN_HP;
1688 snd_hda_set_pin_ctl_cache(codec, pin, val);
1690 spec->automute_speaker = !set_as_mic;
1691 call_update_outputs(codec);
1694 /* create a shared input with the headphone out */
1695 static int create_shared_input(struct hda_codec *codec)
1697 struct hda_gen_spec *spec = codec->spec;
1698 struct auto_pin_cfg *cfg = &spec->autocfg;
1699 unsigned int defcfg;
1702 /* only one internal input pin? */
1703 if (cfg->num_inputs != 1)
1705 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1706 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1709 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1710 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1711 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1712 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1714 return 0; /* both not available */
1716 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1717 return 0; /* no input */
1719 cfg->inputs[1].pin = nid;
1720 cfg->inputs[1].type = AUTO_PIN_MIC;
1721 cfg->num_inputs = 2;
1722 spec->shared_mic_hp = 1;
1723 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1733 /* add the powersave loopback-list entry */
1734 static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1736 struct hda_amp_list *list;
1738 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1740 list = spec->loopback_list + spec->num_loopbacks;
1742 list->dir = HDA_INPUT;
1744 spec->num_loopbacks++;
1745 spec->loopback.amplist = spec->loopback_list;
1748 #define add_loopback_list(spec, mix, idx) /* NOP */
1751 /* create input playback/capture controls for the given pin */
1752 static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1753 const char *ctlname, int ctlidx,
1756 struct hda_gen_spec *spec = codec->spec;
1757 struct nid_path *path;
1761 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1762 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1763 return 0; /* no need for analog loopback */
1765 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
1768 print_nid_path("loopback", path);
1770 idx = path->idx[path->depth - 1];
1771 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1772 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1773 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
1776 path->ctls[NID_PATH_VOL_CTL] = val;
1779 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1780 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1781 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
1784 path->ctls[NID_PATH_MUTE_CTL] = val;
1787 path->active = true;
1788 add_loopback_list(spec, mix_nid, idx);
1792 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
1794 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1795 return (pincap & AC_PINCAP_IN) != 0;
1798 /* Parse the codec tree and retrieve ADCs */
1799 static int fill_adc_nids(struct hda_codec *codec)
1801 struct hda_gen_spec *spec = codec->spec;
1803 hda_nid_t *adc_nids = spec->adc_nids;
1804 int max_nums = ARRAY_SIZE(spec->adc_nids);
1807 nid = codec->start_nid;
1808 for (i = 0; i < codec->num_nodes; i++, nid++) {
1809 unsigned int caps = get_wcaps(codec, nid);
1810 int type = get_wcaps_type(caps);
1812 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1814 adc_nids[nums] = nid;
1815 if (++nums >= max_nums)
1818 spec->num_adc_nids = nums;
1822 /* filter out invalid adc_nids that don't give all active input pins;
1823 * if needed, check whether dynamic ADC-switching is available
1825 static int check_dyn_adc_switch(struct hda_codec *codec)
1827 struct hda_gen_spec *spec = codec->spec;
1828 struct hda_input_mux *imux = &spec->input_mux;
1829 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1835 for (n = 0; n < spec->num_adc_nids; n++) {
1836 adc = spec->adc_nids[n];
1837 for (i = 0; i < imux->num_items; i++) {
1838 pin = spec->imux_pins[i];
1839 if (!is_reachable_path(codec, pin, adc))
1842 if (i >= imux->num_items)
1843 adc_nids[nums++] = adc;
1847 if (spec->shared_mic_hp) {
1848 spec->shared_mic_hp = 0;
1849 imux->num_items = 1;
1853 /* check whether ADC-switch is possible */
1854 for (i = 0; i < imux->num_items; i++) {
1855 pin = spec->imux_pins[i];
1856 for (n = 0; n < spec->num_adc_nids; n++) {
1857 adc = spec->adc_nids[n];
1858 if (is_reachable_path(codec, pin, adc)) {
1859 spec->dyn_adc_idx[i] = n;
1865 snd_printdd("hda-codec: enabling ADC switching\n");
1866 spec->dyn_adc_switch = 1;
1867 } else if (nums != spec->num_adc_nids) {
1868 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1869 spec->num_adc_nids = nums;
1872 if (imux->num_items == 1 || spec->shared_mic_hp) {
1873 snd_printdd("hda-codec: reducing to a single ADC\n");
1874 spec->num_adc_nids = 1; /* reduce to a single ADC */
1877 /* single index for individual volumes ctls */
1878 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1879 spec->num_adc_nids = 1;
1885 * create playback/capture controls for input pins
1887 static int create_input_ctls(struct hda_codec *codec)
1889 struct hda_gen_spec *spec = codec->spec;
1890 const struct auto_pin_cfg *cfg = &spec->autocfg;
1891 hda_nid_t mixer = spec->mixer_nid;
1892 struct hda_input_mux *imux = &spec->input_mux;
1894 int i, c, err, type_idx = 0;
1895 const char *prev_label = NULL;
1897 num_adcs = fill_adc_nids(codec);
1901 for (i = 0; i < cfg->num_inputs; i++) {
1906 pin = cfg->inputs[i].pin;
1907 if (!is_input_pin(codec, pin))
1910 label = hda_get_autocfg_input_label(codec, cfg, i);
1911 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1912 label = "Headphone Mic";
1913 if (prev_label && !strcmp(label, prev_label))
1920 if (is_reachable_path(codec, pin, mixer)) {
1921 err = new_analog_input(codec, pin,
1922 label, type_idx, mixer);
1929 for (c = 0; c < num_adcs; c++) {
1930 struct nid_path *path;
1931 hda_nid_t adc = spec->adc_nids[c];
1933 if (!is_reachable_path(codec, pin, adc))
1935 path = snd_array_new(&spec->paths);
1938 memset(path, 0, sizeof(*path));
1939 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
1941 "invalid input path 0x%x -> 0x%x\n",
1946 print_nid_path("input", path);
1949 spec->imux_pins[imux->num_items] = pin;
1950 snd_hda_add_imux_item(imux, label,
1951 imux->num_items, NULL);
1965 /* get the ADC NID corresponding to the given index */
1966 static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1968 struct hda_gen_spec *spec = codec->spec;
1969 if (spec->dyn_adc_switch)
1970 adc_idx = spec->dyn_adc_idx[imux_idx];
1971 return spec->adc_nids[adc_idx];
1974 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
1977 static int mux_enum_info(struct snd_kcontrol *kcontrol,
1978 struct snd_ctl_elem_info *uinfo)
1980 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1981 struct hda_gen_spec *spec = codec->spec;
1982 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1985 static int mux_enum_get(struct snd_kcontrol *kcontrol,
1986 struct snd_ctl_elem_value *ucontrol)
1988 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1989 struct hda_gen_spec *spec = codec->spec;
1990 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1992 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1996 static int mux_enum_put(struct snd_kcontrol *kcontrol,
1997 struct snd_ctl_elem_value *ucontrol)
1999 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2000 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2001 return mux_select(codec, adc_idx,
2002 ucontrol->value.enumerated.item[0]);
2005 static const struct snd_kcontrol_new cap_src_temp = {
2006 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2007 .name = "Input Source",
2008 .info = mux_enum_info,
2009 .get = mux_enum_get,
2010 .put = mux_enum_put,
2014 * capture volume and capture switch ctls
2017 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2018 struct snd_ctl_elem_value *ucontrol);
2020 /* call the given amp update function for all amps in the imux list at once */
2021 static int cap_put_caller(struct snd_kcontrol *kcontrol,
2022 struct snd_ctl_elem_value *ucontrol,
2023 put_call_t func, int type)
2025 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2026 struct hda_gen_spec *spec = codec->spec;
2027 const struct hda_input_mux *imux;
2028 struct nid_path *path;
2029 int i, adc_idx, err = 0;
2031 imux = &spec->input_mux;
2032 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2033 mutex_lock(&codec->control_mutex);
2034 /* we use the cache-only update at first since multiple input paths
2035 * may shared the same amp; by updating only caches, the redundant
2036 * writes to hardware can be reduced.
2038 codec->cached_write = 1;
2039 for (i = 0; i < imux->num_items; i++) {
2040 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2041 get_adc_nid(codec, adc_idx, i));
2042 if (!path->ctls[type])
2044 kcontrol->private_value = path->ctls[type];
2045 err = func(kcontrol, ucontrol);
2050 codec->cached_write = 0;
2051 mutex_unlock(&codec->control_mutex);
2052 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
2053 if (err >= 0 && spec->cap_sync_hook)
2054 spec->cap_sync_hook(codec);
2058 /* capture volume ctl callbacks */
2059 #define cap_vol_info snd_hda_mixer_amp_volume_info
2060 #define cap_vol_get snd_hda_mixer_amp_volume_get
2061 #define cap_vol_tlv snd_hda_mixer_amp_tlv
2063 static int cap_vol_put(struct snd_kcontrol *kcontrol,
2064 struct snd_ctl_elem_value *ucontrol)
2066 return cap_put_caller(kcontrol, ucontrol,
2067 snd_hda_mixer_amp_volume_put,
2071 static const struct snd_kcontrol_new cap_vol_temp = {
2072 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2073 .name = "Capture Volume",
2074 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2075 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2076 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2077 .info = cap_vol_info,
2080 .tlv = { .c = cap_vol_tlv },
2083 /* capture switch ctl callbacks */
2084 #define cap_sw_info snd_ctl_boolean_stereo_info
2085 #define cap_sw_get snd_hda_mixer_amp_switch_get
2087 static int cap_sw_put(struct snd_kcontrol *kcontrol,
2088 struct snd_ctl_elem_value *ucontrol)
2090 return cap_put_caller(kcontrol, ucontrol,
2091 snd_hda_mixer_amp_switch_put,
2095 static const struct snd_kcontrol_new cap_sw_temp = {
2096 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2097 .name = "Capture Switch",
2098 .info = cap_sw_info,
2103 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2108 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2109 for (depth = 0; depth < 3; depth++) {
2110 if (depth >= path->depth)
2112 i = path->depth - depth - 1;
2113 nid = path->path[i];
2114 if (!path->ctls[NID_PATH_VOL_CTL]) {
2115 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2116 path->ctls[NID_PATH_VOL_CTL] =
2117 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2118 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2119 int idx = path->idx[i];
2120 if (!depth && codec->single_adc_amp)
2122 path->ctls[NID_PATH_VOL_CTL] =
2123 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2126 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2127 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2128 path->ctls[NID_PATH_MUTE_CTL] =
2129 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2130 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2131 int idx = path->idx[i];
2132 if (!depth && codec->single_adc_amp)
2134 path->ctls[NID_PATH_MUTE_CTL] =
2135 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2142 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2144 struct hda_gen_spec *spec = codec->spec;
2145 struct auto_pin_cfg *cfg = &spec->autocfg;
2149 if (!spec->inv_dmic_split)
2151 for (i = 0; i < cfg->num_inputs; i++) {
2152 if (cfg->inputs[i].pin != nid)
2154 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2156 val = snd_hda_codec_get_pincfg(codec, nid);
2157 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2162 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2163 int idx, bool is_switch, unsigned int ctl,
2166 struct hda_gen_spec *spec = codec->spec;
2168 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2169 const char *sfx = is_switch ? "Switch" : "Volume";
2170 unsigned int chs = inv_dmic ? 1 : 3;
2177 snprintf(tmpname, sizeof(tmpname),
2178 "%s Capture %s", label, sfx);
2180 snprintf(tmpname, sizeof(tmpname),
2182 err = add_control(spec, type, tmpname, idx,
2183 amp_val_replace_channels(ctl, chs));
2184 if (err < 0 || !inv_dmic)
2187 /* Make independent right kcontrol */
2189 snprintf(tmpname, sizeof(tmpname),
2190 "Inverted %s Capture %s", label, sfx);
2192 snprintf(tmpname, sizeof(tmpname),
2193 "Inverted Capture %s", sfx);
2194 return add_control(spec, type, tmpname, idx,
2195 amp_val_replace_channels(ctl, 2));
2198 /* create single (and simple) capture volume and switch controls */
2199 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2200 unsigned int vol_ctl, unsigned int sw_ctl,
2204 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2207 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2213 /* create bound capture volume and switch controls */
2214 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2215 unsigned int vol_ctl, unsigned int sw_ctl)
2217 struct hda_gen_spec *spec = codec->spec;
2218 struct snd_kcontrol_new *knew;
2221 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
2225 knew->private_value = vol_ctl;
2226 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2229 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
2233 knew->private_value = sw_ctl;
2234 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2239 /* return the vol ctl when used first in the imux list */
2240 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2242 struct hda_gen_spec *spec = codec->spec;
2243 struct nid_path *path;
2247 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2248 get_adc_nid(codec, 0, idx));
2251 ctl = path->ctls[type];
2254 for (i = 0; i < idx - 1; i++) {
2255 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2256 get_adc_nid(codec, 0, i));
2257 if (path && path->ctls[type] == ctl)
2263 /* create individual capture volume and switch controls per input */
2264 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2266 struct hda_gen_spec *spec = codec->spec;
2267 struct hda_input_mux *imux = &spec->input_mux;
2268 int i, err, type, type_idx = 0;
2269 const char *prev_label = NULL;
2271 for (i = 0; i < imux->num_items; i++) {
2274 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2275 if (prev_label && !strcmp(label, prev_label))
2280 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2282 for (type = 0; type < 2; type++) {
2283 err = add_single_cap_ctl(codec, label, type_idx, type,
2284 get_first_cap_ctl(codec, i, type),
2293 static int create_capture_mixers(struct hda_codec *codec)
2295 struct hda_gen_spec *spec = codec->spec;
2296 struct hda_input_mux *imux = &spec->input_mux;
2297 int i, n, nums, err;
2299 if (spec->dyn_adc_switch)
2302 nums = spec->num_adc_nids;
2304 if (!spec->auto_mic && imux->num_items > 1) {
2305 struct snd_kcontrol_new *knew;
2307 name = nums > 1 ? "Input Source" : "Capture Source";
2308 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
2314 for (n = 0; n < nums; n++) {
2316 bool inv_dmic = false;
2320 for (i = 0; i < imux->num_items; i++) {
2321 struct nid_path *path;
2322 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2323 get_adc_nid(codec, n, i));
2326 parse_capvol_in_path(codec, path);
2328 vol = path->ctls[NID_PATH_VOL_CTL];
2329 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2332 sw = path->ctls[NID_PATH_MUTE_CTL];
2333 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2335 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2340 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2342 else if (!spec->multi_cap_vol)
2343 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2345 err = create_multi_cap_vol_ctl(codec);
2354 * add mic boosts if needed
2356 static int parse_mic_boost(struct hda_codec *codec)
2358 struct hda_gen_spec *spec = codec->spec;
2359 struct auto_pin_cfg *cfg = &spec->autocfg;
2363 const char *prev_label = NULL;
2365 for (i = 0; i < cfg->num_inputs; i++) {
2366 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2368 nid = cfg->inputs[i].pin;
2369 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2371 char boost_label[32];
2372 struct nid_path *path;
2375 label = hda_get_autocfg_input_label(codec, cfg, i);
2376 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2377 label = "Headphone Mic";
2378 if (prev_label && !strcmp(label, prev_label))
2384 snprintf(boost_label, sizeof(boost_label),
2385 "%s Boost Volume", label);
2386 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2387 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2388 boost_label, type_idx, val);
2392 path = snd_hda_get_nid_path(codec, nid, 0);
2394 path->ctls[NID_PATH_BOOST_CTL] = val;
2401 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2403 static void parse_digital(struct hda_codec *codec)
2405 struct hda_gen_spec *spec = codec->spec;
2406 struct nid_path *path;
2410 /* support multiple SPDIFs; the secondary is set up as a slave */
2412 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2413 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2414 dig_nid = look_for_dac(codec, pin, true);
2417 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
2420 print_nid_path("digout", path);
2422 spec->multiout.dig_out_nid = dig_nid;
2423 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2425 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2426 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2428 spec->slave_dig_outs[nums - 1] = dig_nid;
2433 if (spec->autocfg.dig_in_pin) {
2434 dig_nid = codec->start_nid;
2435 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2436 unsigned int wcaps = get_wcaps(codec, dig_nid);
2437 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2439 if (!(wcaps & AC_WCAP_DIGITAL))
2441 path = snd_hda_add_new_path(codec,
2442 spec->autocfg.dig_in_pin,
2443 dig_nid, HDA_PARSE_ALL);
2445 print_nid_path("digin", path);
2446 path->active = true;
2447 spec->dig_in_nid = dig_nid;
2456 * input MUX handling
2459 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2461 /* select the given imux item; either unmute exclusively or select the route */
2462 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2465 struct hda_gen_spec *spec = codec->spec;
2466 const struct hda_input_mux *imux;
2467 struct nid_path *path;
2469 imux = &spec->input_mux;
2470 if (!imux->num_items)
2473 if (idx >= imux->num_items)
2474 idx = imux->num_items - 1;
2475 if (spec->cur_mux[adc_idx] == idx)
2478 path = snd_hda_get_nid_path(codec,
2479 spec->imux_pins[spec->cur_mux[adc_idx]],
2480 spec->adc_nids[adc_idx]);
2484 snd_hda_activate_path(codec, path, false, false);
2486 spec->cur_mux[adc_idx] = idx;
2488 if (spec->shared_mic_hp)
2489 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2491 if (spec->dyn_adc_switch)
2492 dyn_adc_pcm_resetup(codec, idx);
2494 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2495 get_adc_nid(codec, adc_idx, idx));
2500 snd_hda_activate_path(codec, path, true, false);
2501 if (spec->cap_sync_hook)
2502 spec->cap_sync_hook(codec);
2508 * Jack detections for HP auto-mute and mic-switch
2511 /* check each pin in the given array; returns true if any of them is plugged */
2512 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2516 for (i = 0; i < num_pins; i++) {
2517 hda_nid_t nid = pins[i];
2520 present |= snd_hda_jack_detect(codec, nid);
2525 /* standard HP/line-out auto-mute helper */
2526 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2527 bool mute, bool hp_out)
2529 struct hda_gen_spec *spec = codec->spec;
2530 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2533 for (i = 0; i < num_pins; i++) {
2534 hda_nid_t nid = pins[i];
2538 /* don't reset VREF value in case it's controlling
2539 * the amp (see alc861_fixup_asus_amp_vref_0f())
2541 if (spec->keep_vref_in_automute) {
2542 val = snd_hda_codec_read(codec, nid, 0,
2543 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2548 snd_hda_set_pin_ctl_cache(codec, nid, val);
2549 set_pin_eapd(codec, nid, !mute);
2553 /* Toggle outputs muting */
2554 void snd_hda_gen_update_outputs(struct hda_codec *codec)
2556 struct hda_gen_spec *spec = codec->spec;
2559 /* Control HP pins/amps depending on master_mute state;
2560 * in general, HP pins/amps control should be enabled in all cases,
2561 * but currently set only for master_mute, just to be safe
2563 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2564 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2565 spec->autocfg.hp_pins, spec->master_mute, true);
2567 if (!spec->automute_speaker)
2570 on = spec->hp_jack_present | spec->line_jack_present;
2571 on |= spec->master_mute;
2572 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2573 spec->autocfg.speaker_pins, on, false);
2575 /* toggle line-out mutes if needed, too */
2576 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2577 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2578 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2580 if (!spec->automute_lo)
2583 on = spec->hp_jack_present;
2584 on |= spec->master_mute;
2585 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2586 spec->autocfg.line_out_pins, on, false);
2588 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
2590 static void call_update_outputs(struct hda_codec *codec)
2592 struct hda_gen_spec *spec = codec->spec;
2593 if (spec->automute_hook)
2594 spec->automute_hook(codec);
2596 snd_hda_gen_update_outputs(codec);
2599 /* standard HP-automute helper */
2600 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2602 struct hda_gen_spec *spec = codec->spec;
2604 spec->hp_jack_present =
2605 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2606 spec->autocfg.hp_pins);
2607 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2609 call_update_outputs(codec);
2611 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
2613 /* standard line-out-automute helper */
2614 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2616 struct hda_gen_spec *spec = codec->spec;
2618 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2620 /* check LO jack only when it's different from HP */
2621 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2624 spec->line_jack_present =
2625 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2626 spec->autocfg.line_out_pins);
2627 if (!spec->automute_speaker || !spec->detect_lo)
2629 call_update_outputs(codec);
2631 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
2633 /* standard mic auto-switch helper */
2634 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
2636 struct hda_gen_spec *spec = codec->spec;
2639 if (!spec->auto_mic)
2642 for (i = spec->am_num_entries - 1; i > 0; i--) {
2643 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2644 mux_select(codec, 0, spec->am_entry[i].idx);
2648 mux_select(codec, 0, spec->am_entry[0].idx);
2650 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
2653 * Auto-Mute mode mixer enum support
2655 static int automute_mode_info(struct snd_kcontrol *kcontrol,
2656 struct snd_ctl_elem_info *uinfo)
2658 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2659 struct hda_gen_spec *spec = codec->spec;
2660 static const char * const texts3[] = {
2661 "Disabled", "Speaker Only", "Line Out+Speaker"
2664 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2665 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2666 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2669 static int automute_mode_get(struct snd_kcontrol *kcontrol,
2670 struct snd_ctl_elem_value *ucontrol)
2672 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2673 struct hda_gen_spec *spec = codec->spec;
2674 unsigned int val = 0;
2675 if (spec->automute_speaker)
2677 if (spec->automute_lo)
2680 ucontrol->value.enumerated.item[0] = val;
2684 static int automute_mode_put(struct snd_kcontrol *kcontrol,
2685 struct snd_ctl_elem_value *ucontrol)
2687 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2688 struct hda_gen_spec *spec = codec->spec;
2690 switch (ucontrol->value.enumerated.item[0]) {
2692 if (!spec->automute_speaker && !spec->automute_lo)
2694 spec->automute_speaker = 0;
2695 spec->automute_lo = 0;
2698 if (spec->automute_speaker_possible) {
2699 if (!spec->automute_lo && spec->automute_speaker)
2701 spec->automute_speaker = 1;
2702 spec->automute_lo = 0;
2703 } else if (spec->automute_lo_possible) {
2704 if (spec->automute_lo)
2706 spec->automute_lo = 1;
2711 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2713 if (spec->automute_speaker && spec->automute_lo)
2715 spec->automute_speaker = 1;
2716 spec->automute_lo = 1;
2721 call_update_outputs(codec);
2725 static const struct snd_kcontrol_new automute_mode_enum = {
2726 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2727 .name = "Auto-Mute Mode",
2728 .info = automute_mode_info,
2729 .get = automute_mode_get,
2730 .put = automute_mode_put,
2733 static int add_automute_mode_enum(struct hda_codec *codec)
2735 struct hda_gen_spec *spec = codec->spec;
2737 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
2743 * Check the availability of HP/line-out auto-mute;
2744 * Set up appropriately if really supported
2746 static int check_auto_mute_availability(struct hda_codec *codec)
2748 struct hda_gen_spec *spec = codec->spec;
2749 struct auto_pin_cfg *cfg = &spec->autocfg;
2753 if (cfg->hp_pins[0])
2755 if (cfg->line_out_pins[0])
2757 if (cfg->speaker_pins[0])
2759 if (present < 2) /* need two different output types */
2762 if (!cfg->speaker_pins[0] &&
2763 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2764 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2765 sizeof(cfg->speaker_pins));
2766 cfg->speaker_outs = cfg->line_outs;
2769 if (!cfg->hp_pins[0] &&
2770 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2771 memcpy(cfg->hp_pins, cfg->line_out_pins,
2772 sizeof(cfg->hp_pins));
2773 cfg->hp_outs = cfg->line_outs;
2776 for (i = 0; i < cfg->hp_outs; i++) {
2777 hda_nid_t nid = cfg->hp_pins[i];
2778 if (!is_jack_detectable(codec, nid))
2780 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2782 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
2783 snd_hda_gen_hp_automute);
2784 spec->detect_hp = 1;
2787 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2788 if (cfg->speaker_outs)
2789 for (i = 0; i < cfg->line_outs; i++) {
2790 hda_nid_t nid = cfg->line_out_pins[i];
2791 if (!is_jack_detectable(codec, nid))
2793 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2794 snd_hda_jack_detect_enable_callback(codec, nid,
2795 HDA_GEN_FRONT_EVENT,
2796 snd_hda_gen_line_automute);
2797 spec->detect_lo = 1;
2799 spec->automute_lo_possible = spec->detect_hp;
2802 spec->automute_speaker_possible = cfg->speaker_outs &&
2803 (spec->detect_hp || spec->detect_lo);
2805 spec->automute_lo = spec->automute_lo_possible;
2806 spec->automute_speaker = spec->automute_speaker_possible;
2808 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2809 /* create a control for automute mode */
2810 err = add_automute_mode_enum(codec);
2817 /* return the position of NID in the list, or -1 if not found */
2818 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2821 for (i = 0; i < nums; i++)
2827 /* check whether all auto-mic pins are valid; setup indices if OK */
2828 static bool auto_mic_check_imux(struct hda_codec *codec)
2830 struct hda_gen_spec *spec = codec->spec;
2831 const struct hda_input_mux *imux;
2834 imux = &spec->input_mux;
2835 for (i = 0; i < spec->am_num_entries; i++) {
2836 spec->am_entry[i].idx =
2837 find_idx_in_nid_list(spec->am_entry[i].pin,
2838 spec->imux_pins, imux->num_items);
2839 if (spec->am_entry[i].idx < 0)
2840 return false; /* no corresponding imux */
2843 /* we don't need the jack detection for the first pin */
2844 for (i = 1; i < spec->am_num_entries; i++)
2845 snd_hda_jack_detect_enable_callback(codec,
2846 spec->am_entry[i].pin,
2848 snd_hda_gen_mic_autoswitch);
2852 static int compare_attr(const void *ap, const void *bp)
2854 const struct automic_entry *a = ap;
2855 const struct automic_entry *b = bp;
2856 return (int)(a->attr - b->attr);
2860 * Check the availability of auto-mic switch;
2861 * Set up if really supported
2863 static int check_auto_mic_availability(struct hda_codec *codec)
2865 struct hda_gen_spec *spec = codec->spec;
2866 struct auto_pin_cfg *cfg = &spec->autocfg;
2872 for (i = 0; i < cfg->num_inputs; i++) {
2873 hda_nid_t nid = cfg->inputs[i].pin;
2875 attr = snd_hda_codec_get_pincfg(codec, nid);
2876 attr = snd_hda_get_input_pin_attr(attr);
2877 if (types & (1 << attr))
2878 return 0; /* already occupied */
2880 case INPUT_PIN_ATTR_INT:
2881 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2882 return 0; /* invalid type */
2884 case INPUT_PIN_ATTR_UNUSED:
2885 return 0; /* invalid entry */
2887 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2888 return 0; /* invalid type */
2889 if (!spec->line_in_auto_switch &&
2890 cfg->inputs[i].type != AUTO_PIN_MIC)
2891 return 0; /* only mic is allowed */
2892 if (!is_jack_detectable(codec, nid))
2893 return 0; /* no unsol support */
2896 if (num_pins >= MAX_AUTO_MIC_PINS)
2898 types |= (1 << attr);
2899 spec->am_entry[num_pins].pin = nid;
2900 spec->am_entry[num_pins].attr = attr;
2907 spec->am_num_entries = num_pins;
2908 /* sort the am_entry in the order of attr so that the pin with a
2909 * higher attr will be selected when the jack is plugged.
2911 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2912 compare_attr, NULL);
2914 if (!auto_mic_check_imux(codec))
2918 spec->num_adc_nids = 1;
2919 spec->cur_mux[0] = spec->am_entry[0].idx;
2920 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2921 spec->am_entry[0].pin,
2922 spec->am_entry[1].pin,
2923 spec->am_entry[2].pin);
2930 * Parse the given BIOS configuration and set up the hda_gen_spec
2932 * return 1 if successful, 0 if the proper config is not found,
2933 * or a negative error code
2935 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
2936 struct auto_pin_cfg *cfg)
2938 struct hda_gen_spec *spec = codec->spec;
2941 if (cfg != &spec->autocfg) {
2942 spec->autocfg = *cfg;
2943 cfg = &spec->autocfg;
2946 if (!cfg->line_outs) {
2947 if (cfg->dig_outs || cfg->dig_in_pin) {
2948 spec->multiout.max_channels = 2;
2949 spec->no_analog = 1;
2952 return 0; /* can't find valid BIOS pin config */
2955 if (!spec->no_primary_hp &&
2956 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2957 cfg->line_outs <= cfg->hp_outs) {
2958 /* use HP as primary out */
2959 cfg->speaker_outs = cfg->line_outs;
2960 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2961 sizeof(cfg->speaker_pins));
2962 cfg->line_outs = cfg->hp_outs;
2963 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2965 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2966 cfg->line_out_type = AUTO_PIN_HP_OUT;
2969 err = parse_output_paths(codec);
2972 err = create_multi_channel_mode(codec);
2975 err = create_multi_out_ctls(codec, cfg);
2978 err = create_hp_out_ctls(codec);
2981 err = create_speaker_out_ctls(codec);
2984 err = create_indep_hp_ctls(codec);
2987 err = create_shared_input(codec);
2990 err = create_input_ctls(codec);
2994 /* check the multiple speaker pins */
2995 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2996 spec->const_channel_count = cfg->line_outs * 2;
2998 spec->const_channel_count = cfg->speaker_outs * 2;
3000 if (spec->multi_ios > 0)
3001 spec->multiout.max_channels = max(spec->ext_channel_count,
3002 spec->const_channel_count);
3004 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3006 err = check_auto_mute_availability(codec);
3010 err = check_dyn_adc_switch(codec);
3014 if (!spec->shared_mic_hp) {
3015 err = check_auto_mic_availability(codec);
3020 err = create_capture_mixers(codec);
3024 err = parse_mic_boost(codec);
3029 parse_digital(codec);
3033 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
3037 * Build control elements
3040 /* slave controls for virtual master */
3041 static const char * const slave_pfxs[] = {
3042 "Front", "Surround", "Center", "LFE", "Side",
3043 "Headphone", "Speaker", "Mono", "Line Out",
3044 "CLFE", "Bass Speaker", "PCM",
3048 int snd_hda_gen_build_controls(struct hda_codec *codec)
3050 struct hda_gen_spec *spec = codec->spec;
3053 if (spec->kctls.used) {
3054 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3059 if (spec->multiout.dig_out_nid) {
3060 err = snd_hda_create_dig_out_ctls(codec,
3061 spec->multiout.dig_out_nid,
3062 spec->multiout.dig_out_nid,
3063 spec->pcm_rec[1].pcm_type);
3066 if (!spec->no_analog) {
3067 err = snd_hda_create_spdif_share_sw(codec,
3071 spec->multiout.share_spdif = 1;
3074 if (spec->dig_in_nid) {
3075 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3080 /* if we have no master control, let's create it */
3081 if (!spec->no_analog &&
3082 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3083 unsigned int vmaster_tlv[4];
3084 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3085 HDA_OUTPUT, vmaster_tlv);
3086 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3087 vmaster_tlv, slave_pfxs,
3092 if (!spec->no_analog &&
3093 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3094 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3097 true, &spec->vmaster_mute.sw_kctl);
3100 if (spec->vmaster_mute.hook)
3101 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3102 spec->vmaster_mute_enum);
3105 free_kctls(spec); /* no longer needed */
3107 if (spec->shared_mic_hp) {
3109 int nid = spec->autocfg.inputs[1].pin;
3110 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3113 err = snd_hda_jack_detect_enable(codec, nid, 0);
3118 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3124 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3132 * Analog playback callbacks
3134 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3135 struct hda_codec *codec,
3136 struct snd_pcm_substream *substream)
3138 struct hda_gen_spec *spec = codec->spec;
3141 mutex_lock(&spec->pcm_mutex);
3142 err = snd_hda_multi_out_analog_open(codec,
3143 &spec->multiout, substream,
3146 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3147 mutex_unlock(&spec->pcm_mutex);
3151 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3152 struct hda_codec *codec,
3153 unsigned int stream_tag,
3154 unsigned int format,
3155 struct snd_pcm_substream *substream)
3157 struct hda_gen_spec *spec = codec->spec;
3158 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3159 stream_tag, format, substream);
3162 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3163 struct hda_codec *codec,
3164 struct snd_pcm_substream *substream)
3166 struct hda_gen_spec *spec = codec->spec;
3167 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3170 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3171 struct hda_codec *codec,
3172 struct snd_pcm_substream *substream)
3174 struct hda_gen_spec *spec = codec->spec;
3175 mutex_lock(&spec->pcm_mutex);
3176 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3177 mutex_unlock(&spec->pcm_mutex);
3181 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3182 struct hda_codec *codec,
3183 struct snd_pcm_substream *substream)
3185 struct hda_gen_spec *spec = codec->spec;
3188 mutex_lock(&spec->pcm_mutex);
3189 if (!spec->indep_hp_enabled)
3192 spec->active_streams |= 1 << STREAM_INDEP_HP;
3193 mutex_unlock(&spec->pcm_mutex);
3197 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3198 struct hda_codec *codec,
3199 struct snd_pcm_substream *substream)
3201 struct hda_gen_spec *spec = codec->spec;
3202 mutex_lock(&spec->pcm_mutex);
3203 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3204 mutex_unlock(&spec->pcm_mutex);
3211 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3212 struct hda_codec *codec,
3213 struct snd_pcm_substream *substream)
3215 struct hda_gen_spec *spec = codec->spec;
3216 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3219 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3220 struct hda_codec *codec,
3221 unsigned int stream_tag,
3222 unsigned int format,
3223 struct snd_pcm_substream *substream)
3225 struct hda_gen_spec *spec = codec->spec;
3226 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3227 stream_tag, format, substream);
3230 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3231 struct hda_codec *codec,
3232 struct snd_pcm_substream *substream)
3234 struct hda_gen_spec *spec = codec->spec;
3235 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3238 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3239 struct hda_codec *codec,
3240 struct snd_pcm_substream *substream)
3242 struct hda_gen_spec *spec = codec->spec;
3243 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3249 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3250 struct hda_codec *codec,
3251 unsigned int stream_tag,
3252 unsigned int format,
3253 struct snd_pcm_substream *substream)
3255 struct hda_gen_spec *spec = codec->spec;
3257 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
3258 stream_tag, 0, format);
3262 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3263 struct hda_codec *codec,
3264 struct snd_pcm_substream *substream)
3266 struct hda_gen_spec *spec = codec->spec;
3268 snd_hda_codec_cleanup_stream(codec,
3269 spec->adc_nids[substream->number + 1]);
3275 static const struct hda_pcm_stream pcm_analog_playback = {
3279 /* NID is set in build_pcms */
3281 .open = playback_pcm_open,
3282 .close = playback_pcm_close,
3283 .prepare = playback_pcm_prepare,
3284 .cleanup = playback_pcm_cleanup
3288 static const struct hda_pcm_stream pcm_analog_capture = {
3292 /* NID is set in build_pcms */
3295 static const struct hda_pcm_stream pcm_analog_alt_playback = {
3299 /* NID is set in build_pcms */
3301 .open = alt_playback_pcm_open,
3302 .close = alt_playback_pcm_close
3306 static const struct hda_pcm_stream pcm_analog_alt_capture = {
3307 .substreams = 2, /* can be overridden */
3310 /* NID is set in build_pcms */
3312 .prepare = alt_capture_pcm_prepare,
3313 .cleanup = alt_capture_pcm_cleanup
3317 static const struct hda_pcm_stream pcm_digital_playback = {
3321 /* NID is set in build_pcms */
3323 .open = dig_playback_pcm_open,
3324 .close = dig_playback_pcm_close,
3325 .prepare = dig_playback_pcm_prepare,
3326 .cleanup = dig_playback_pcm_cleanup
3330 static const struct hda_pcm_stream pcm_digital_capture = {
3334 /* NID is set in build_pcms */
3337 /* Used by build_pcms to flag that a PCM has no playback stream */
3338 static const struct hda_pcm_stream pcm_null_stream = {
3345 * dynamic changing ADC PCM streams
3347 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3349 struct hda_gen_spec *spec = codec->spec;
3350 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3352 if (spec->cur_adc && spec->cur_adc != new_adc) {
3353 /* stream is running, let's swap the current ADC */
3354 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3355 spec->cur_adc = new_adc;
3356 snd_hda_codec_setup_stream(codec, new_adc,
3357 spec->cur_adc_stream_tag, 0,
3358 spec->cur_adc_format);
3364 /* analog capture with dynamic dual-adc changes */
3365 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3366 struct hda_codec *codec,
3367 unsigned int stream_tag,
3368 unsigned int format,
3369 struct snd_pcm_substream *substream)
3371 struct hda_gen_spec *spec = codec->spec;
3372 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3373 spec->cur_adc_stream_tag = stream_tag;
3374 spec->cur_adc_format = format;
3375 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3379 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3380 struct hda_codec *codec,
3381 struct snd_pcm_substream *substream)
3383 struct hda_gen_spec *spec = codec->spec;
3384 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3389 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3393 .nid = 0, /* fill later */
3395 .prepare = dyn_adc_capture_pcm_prepare,
3396 .cleanup = dyn_adc_capture_pcm_cleanup
3400 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3401 const char *chip_name)
3407 strlcpy(str, chip_name, len);
3409 /* drop non-alnum chars after a space */
3410 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3411 if (!isalnum(p[1])) {
3416 strlcat(str, sfx, len);
3419 /* build PCM streams based on the parsed results */
3420 int snd_hda_gen_build_pcms(struct hda_codec *codec)
3422 struct hda_gen_spec *spec = codec->spec;
3423 struct hda_pcm *info = spec->pcm_rec;
3424 const struct hda_pcm_stream *p;
3425 bool have_multi_adcs;
3427 codec->num_pcms = 1;
3428 codec->pcm_info = info;
3430 if (spec->no_analog)
3433 fill_pcm_stream_name(spec->stream_name_analog,
3434 sizeof(spec->stream_name_analog),
3435 " Analog", codec->chip_name);
3436 info->name = spec->stream_name_analog;
3438 if (spec->multiout.num_dacs > 0) {
3439 p = spec->stream_analog_playback;
3441 p = &pcm_analog_playback;
3442 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3443 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3444 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3445 spec->multiout.max_channels;
3446 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3447 spec->autocfg.line_outs == 2)
3448 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3451 if (spec->num_adc_nids) {
3452 p = spec->stream_analog_capture;
3454 if (spec->dyn_adc_switch)
3455 p = &dyn_adc_pcm_analog_capture;
3457 p = &pcm_analog_capture;
3459 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3460 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3464 /* SPDIF for stream index #1 */
3465 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
3466 fill_pcm_stream_name(spec->stream_name_digital,
3467 sizeof(spec->stream_name_digital),
3468 " Digital", codec->chip_name);
3469 codec->num_pcms = 2;
3470 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3471 info = spec->pcm_rec + 1;
3472 info->name = spec->stream_name_digital;
3473 if (spec->dig_out_type)
3474 info->pcm_type = spec->dig_out_type;
3476 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3477 if (spec->multiout.dig_out_nid) {
3478 p = spec->stream_digital_playback;
3480 p = &pcm_digital_playback;
3481 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3482 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3484 if (spec->dig_in_nid) {
3485 p = spec->stream_digital_capture;
3487 p = &pcm_digital_capture;
3488 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3489 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3493 if (spec->no_analog)
3496 /* If the use of more than one ADC is requested for the current
3497 * model, configure a second analog capture-only PCM.
3499 have_multi_adcs = (spec->num_adc_nids > 1) &&
3500 !spec->dyn_adc_switch && !spec->auto_mic;
3501 /* Additional Analaog capture for index #2 */
3502 if (spec->alt_dac_nid || have_multi_adcs) {
3503 codec->num_pcms = 3;
3504 info = spec->pcm_rec + 2;
3505 info->name = spec->stream_name_analog;
3506 if (spec->alt_dac_nid) {
3507 p = spec->stream_analog_alt_playback;
3509 p = &pcm_analog_alt_playback;
3510 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3511 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3514 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3516 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3518 if (have_multi_adcs) {
3519 p = spec->stream_analog_alt_capture;
3521 p = &pcm_analog_alt_capture;
3522 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3523 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3525 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3526 spec->num_adc_nids - 1;
3528 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3530 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3536 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3540 * Standard auto-parser initializations
3543 /* configure the path from the given dac to the pin as the proper output */
3544 static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3545 int pin_type, hda_nid_t dac)
3547 struct nid_path *path;
3549 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3550 path = snd_hda_get_nid_path(codec, dac, pin);
3555 snd_hda_activate_path(codec, path, true, true);
3556 set_pin_eapd(codec, pin, true);
3559 /* initialize primary output paths */
3560 static void init_multi_out(struct hda_codec *codec)
3562 struct hda_gen_spec *spec = codec->spec;
3567 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3572 for (i = 0; i < spec->autocfg.line_outs; i++) {
3573 nid = spec->autocfg.line_out_pins[i];
3575 dac = spec->multiout.dac_nids[i];
3577 dac = spec->multiout.dac_nids[0];
3578 set_output_and_unmute(codec, nid, pin_type, dac);
3584 static void __init_extra_out(struct hda_codec *codec, int num_outs,
3585 hda_nid_t *pins, hda_nid_t *dacs, int type)
3587 struct hda_gen_spec *spec = codec->spec;
3591 for (i = 0; i < num_outs; i++) {
3597 if (i > 0 && dacs[0])
3600 dac = spec->multiout.dac_nids[0];
3602 set_output_and_unmute(codec, pin, type, dac);
3606 /* initialize hp and speaker paths */
3607 static void init_extra_out(struct hda_codec *codec)
3609 struct hda_gen_spec *spec = codec->spec;
3611 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3612 __init_extra_out(codec, spec->autocfg.hp_outs,
3613 spec->autocfg.hp_pins,
3614 spec->multiout.hp_out_nid, PIN_HP);
3615 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3616 __init_extra_out(codec, spec->autocfg.speaker_outs,
3617 spec->autocfg.speaker_pins,
3618 spec->multiout.extra_out_nid, PIN_OUT);
3621 /* initialize multi-io paths */
3622 static void init_multi_io(struct hda_codec *codec)
3624 struct hda_gen_spec *spec = codec->spec;
3627 for (i = 0; i < spec->multi_ios; i++) {
3628 hda_nid_t pin = spec->multi_io[i].pin;
3629 struct nid_path *path;
3630 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3633 if (!spec->multi_io[i].ctl_in)
3634 spec->multi_io[i].ctl_in =
3635 snd_hda_codec_update_cache(codec, pin, 0,
3636 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3637 snd_hda_activate_path(codec, path, path->active, true);
3641 /* set up the input pin config, depending on the given auto-pin type */
3642 static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3645 unsigned int val = PIN_IN;
3646 if (auto_pin_type == AUTO_PIN_MIC)
3647 val |= snd_hda_get_default_vref(codec, nid);
3648 snd_hda_set_pin_ctl_cache(codec, nid, val);
3651 /* set up input pins and loopback paths */
3652 static void init_analog_input(struct hda_codec *codec)
3654 struct hda_gen_spec *spec = codec->spec;
3655 struct auto_pin_cfg *cfg = &spec->autocfg;
3658 for (i = 0; i < cfg->num_inputs; i++) {
3659 hda_nid_t nid = cfg->inputs[i].pin;
3660 if (is_input_pin(codec, nid))
3661 set_input_pin(codec, nid, cfg->inputs[i].type);
3663 /* init loopback inputs */
3664 if (spec->mixer_nid) {
3665 struct nid_path *path;
3666 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3668 snd_hda_activate_path(codec, path,
3669 path->active, false);
3674 /* initialize ADC paths */
3675 static void init_input_src(struct hda_codec *codec)
3677 struct hda_gen_spec *spec = codec->spec;
3678 struct hda_input_mux *imux = &spec->input_mux;
3679 struct nid_path *path;
3682 if (spec->dyn_adc_switch)
3685 nums = spec->num_adc_nids;
3687 for (c = 0; c < nums; c++) {
3688 for (i = 0; i < imux->num_items; i++) {
3689 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3690 get_adc_nid(codec, c, i));
3692 bool active = path->active;
3693 if (i == spec->cur_mux[c])
3695 snd_hda_activate_path(codec, path, active, false);
3700 if (spec->shared_mic_hp)
3701 update_shared_mic_hp(codec, spec->cur_mux[0]);
3703 if (spec->cap_sync_hook)
3704 spec->cap_sync_hook(codec);
3707 /* set right pin controls for digital I/O */
3708 static void init_digital(struct hda_codec *codec)
3710 struct hda_gen_spec *spec = codec->spec;
3714 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3715 pin = spec->autocfg.dig_out_pins[i];
3718 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3720 pin = spec->autocfg.dig_in_pin;
3722 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
3725 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3726 * invalid unsol tags by some reason
3728 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3732 for (i = 0; i < codec->init_pins.used; i++) {
3733 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3734 hda_nid_t nid = pin->nid;
3735 if (is_jack_detectable(codec, nid) &&
3736 !snd_hda_jack_tbl_get(codec, nid))
3737 snd_hda_codec_update_cache(codec, nid, 0,
3738 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3742 int snd_hda_gen_init(struct hda_codec *codec)
3744 struct hda_gen_spec *spec = codec->spec;
3746 if (spec->init_hook)
3747 spec->init_hook(codec);
3749 snd_hda_apply_verbs(codec);
3751 codec->cached_write = 1;
3753 init_multi_out(codec);
3754 init_extra_out(codec);
3755 init_multi_io(codec);
3756 init_analog_input(codec);
3757 init_input_src(codec);
3758 init_digital(codec);
3760 clear_unsol_on_unused_pins(codec);
3762 /* call init functions of standard auto-mute helpers */
3763 snd_hda_gen_hp_automute(codec, NULL);
3764 snd_hda_gen_line_automute(codec, NULL);
3765 snd_hda_gen_mic_autoswitch(codec, NULL);
3767 snd_hda_codec_flush_amp_cache(codec);
3768 snd_hda_codec_flush_cmd_cache(codec);
3770 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3771 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3773 hda_call_check_power_status(codec, 0x01);
3776 EXPORT_SYMBOL(snd_hda_gen_init);
3780 * the generic codec support
3784 static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3786 struct hda_gen_spec *spec = codec->spec;
3787 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3791 static void generic_free(struct hda_codec *codec)
3793 snd_hda_gen_spec_free(codec->spec);
3798 static const struct hda_codec_ops generic_patch_ops = {
3799 .build_controls = snd_hda_gen_build_controls,
3800 .build_pcms = snd_hda_gen_build_pcms,
3801 .init = snd_hda_gen_init,
3802 .free = generic_free,
3803 .unsol_event = snd_hda_jack_unsol_event,
3805 .check_power_status = generic_check_power_status,
3809 int snd_hda_parse_generic_codec(struct hda_codec *codec)
3811 struct hda_gen_spec *spec;
3814 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3817 snd_hda_gen_spec_init(spec);
3820 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3824 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
3828 codec->patch_ops = generic_patch_ops;
3832 generic_free(codec);
3835 EXPORT_SYMBOL(snd_hda_parse_generic_codec);