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 (!enable && 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);
881 badness += assign_out_path_ctls(codec, pin, dac);
887 /* return NID if the given pin has only a single connection to a certain DAC */
888 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
890 struct hda_gen_spec *spec = codec->spec;
892 hda_nid_t nid_found = 0;
894 for (i = 0; i < spec->num_all_dacs; i++) {
895 hda_nid_t nid = spec->all_dacs[i];
896 if (!nid || is_dac_already_used(codec, nid))
898 if (is_reachable_path(codec, nid, pin)) {
907 /* check whether the given pin can be a multi-io pin */
908 static bool can_be_multiio_pin(struct hda_codec *codec,
909 unsigned int location, hda_nid_t nid)
911 unsigned int defcfg, caps;
913 defcfg = snd_hda_codec_get_pincfg(codec, nid);
914 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
916 if (location && get_defcfg_location(defcfg) != location)
918 caps = snd_hda_query_pin_caps(codec, nid);
919 if (!(caps & AC_PINCAP_OUT))
927 * When hardwired is set, try to fill ony hardwired pins, and returns
928 * zero if any pins are filled, non-zero if nothing found.
929 * When hardwired is off, try to fill possible input pins, and returns
932 static int fill_multi_ios(struct hda_codec *codec,
933 hda_nid_t reference_pin,
934 bool hardwired, int offset)
936 struct hda_gen_spec *spec = codec->spec;
937 struct auto_pin_cfg *cfg = &spec->autocfg;
938 int type, i, j, dacs, num_pins, old_pins;
939 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
940 unsigned int location = get_defcfg_location(defcfg);
943 old_pins = spec->multi_ios;
948 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
949 for (i = 0; i < cfg->num_inputs; i++) {
950 if (cfg->inputs[i].type != type)
952 if (can_be_multiio_pin(codec, location,
960 dacs = spec->multiout.num_dacs;
961 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
962 for (i = 0; i < cfg->num_inputs; i++) {
963 struct nid_path *path;
964 hda_nid_t nid = cfg->inputs[i].pin;
967 if (cfg->inputs[i].type != type)
969 if (!can_be_multiio_pin(codec, location, nid))
971 for (j = 0; j < spec->multi_ios; j++) {
972 if (nid == spec->multi_io[j].pin)
975 if (j < spec->multi_ios)
978 if (offset && offset + spec->multi_ios < dacs) {
979 dac = spec->private_dac_nids[offset + spec->multi_ios];
980 if (!is_reachable_path(codec, dac, nid))
984 dac = get_dac_if_single(codec, nid);
986 dac = look_for_dac(codec, nid, false);
991 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
996 print_nid_path("multiio", path);
997 spec->multi_io[spec->multi_ios].pin = nid;
998 spec->multi_io[spec->multi_ios].dac = dac;
1000 if (spec->multi_ios >= 2)
1006 badness = BAD_MULTI_IO;
1007 if (old_pins == spec->multi_ios) {
1009 return 1; /* nothing found */
1011 return badness; /* no badness if nothing found */
1013 if (!hardwired && spec->multi_ios < 2) {
1014 /* cancel newly assigned paths */
1015 spec->paths.used -= spec->multi_ios - old_pins;
1016 spec->multi_ios = old_pins;
1020 /* assign volume and mute controls */
1021 for (i = old_pins; i < spec->multi_ios; i++)
1022 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1023 spec->multi_io[i].dac);
1028 /* map DACs for all pins in the list if they are single connections */
1029 static bool map_singles(struct hda_codec *codec, int outs,
1030 const hda_nid_t *pins, hda_nid_t *dacs)
1032 struct hda_gen_spec *spec = codec->spec;
1035 for (i = 0; i < outs; i++) {
1036 struct nid_path *path;
1040 dac = get_dac_if_single(codec, pins[i]);
1043 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
1044 if (!path && i > 0 && spec->mixer_nid)
1045 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
1049 print_nid_path("output", path);
1050 path->active = true;
1056 /* fill in the dac_nids table from the parsed pin configuration */
1057 static int fill_and_eval_dacs(struct hda_codec *codec,
1058 bool fill_hardwired,
1059 bool fill_mio_first)
1061 struct hda_gen_spec *spec = codec->spec;
1062 struct auto_pin_cfg *cfg = &spec->autocfg;
1063 int i, err, badness;
1065 /* set num_dacs once to full for look_for_dac() */
1066 spec->multiout.num_dacs = cfg->line_outs;
1067 spec->multiout.dac_nids = spec->private_dac_nids;
1068 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1069 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1070 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1071 spec->multi_ios = 0;
1072 snd_array_free(&spec->paths);
1075 /* fill hard-wired DACs first */
1076 if (fill_hardwired) {
1079 mapped = map_singles(codec, cfg->line_outs,
1081 spec->private_dac_nids);
1082 mapped |= map_singles(codec, cfg->hp_outs,
1084 spec->multiout.hp_out_nid);
1085 mapped |= map_singles(codec, cfg->speaker_outs,
1087 spec->multiout.extra_out_nid);
1088 if (fill_mio_first && cfg->line_outs == 1 &&
1089 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1090 err = fill_multi_ios(codec, cfg->line_out_pins[0], true, 0);
1097 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1098 spec->private_dac_nids,
1101 /* re-count num_dacs and squash invalid entries */
1102 spec->multiout.num_dacs = 0;
1103 for (i = 0; i < cfg->line_outs; i++) {
1104 if (spec->private_dac_nids[i])
1105 spec->multiout.num_dacs++;
1107 memmove(spec->private_dac_nids + i,
1108 spec->private_dac_nids + i + 1,
1109 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1110 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1114 if (fill_mio_first &&
1115 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1116 /* try to fill multi-io first */
1117 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1120 /* we don't count badness at this stage yet */
1123 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1124 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1125 spec->multiout.hp_out_nid,
1126 &extra_out_badness);
1131 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1132 err = try_assign_dacs(codec, cfg->speaker_outs,
1134 spec->multiout.extra_out_nid,
1135 &extra_out_badness);
1140 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1141 err = fill_multi_ios(codec, cfg->line_out_pins[0], false, 0);
1146 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1147 /* try multi-ios with HP + inputs */
1149 if (cfg->line_outs >= 3)
1151 err = fill_multi_ios(codec, cfg->hp_pins[0], false, offset);
1157 if (spec->multi_ios == 2) {
1158 for (i = 0; i < 2; i++)
1159 spec->private_dac_nids[spec->multiout.num_dacs++] =
1160 spec->multi_io[i].dac;
1161 spec->ext_channel_count = 2;
1162 } else if (spec->multi_ios) {
1163 spec->multi_ios = 0;
1164 badness += BAD_MULTI_IO;
1170 #define DEBUG_BADNESS
1172 #ifdef DEBUG_BADNESS
1173 #define debug_badness snd_printdd
1175 #define debug_badness(...)
1178 static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1180 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1181 cfg->line_out_pins[0], cfg->line_out_pins[1],
1182 cfg->line_out_pins[2], cfg->line_out_pins[3],
1183 spec->multiout.dac_nids[0],
1184 spec->multiout.dac_nids[1],
1185 spec->multiout.dac_nids[2],
1186 spec->multiout.dac_nids[3]);
1187 if (spec->multi_ios > 0)
1188 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1190 spec->multi_io[0].pin, spec->multi_io[1].pin,
1191 spec->multi_io[0].dac, spec->multi_io[1].dac);
1192 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1193 cfg->hp_pins[0], cfg->hp_pins[1],
1194 cfg->hp_pins[2], cfg->hp_pins[3],
1195 spec->multiout.hp_out_nid[0],
1196 spec->multiout.hp_out_nid[1],
1197 spec->multiout.hp_out_nid[2],
1198 spec->multiout.hp_out_nid[3]);
1199 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1200 cfg->speaker_pins[0], cfg->speaker_pins[1],
1201 cfg->speaker_pins[2], cfg->speaker_pins[3],
1202 spec->multiout.extra_out_nid[0],
1203 spec->multiout.extra_out_nid[1],
1204 spec->multiout.extra_out_nid[2],
1205 spec->multiout.extra_out_nid[3]);
1208 /* find all available DACs of the codec */
1209 static void fill_all_dac_nids(struct hda_codec *codec)
1211 struct hda_gen_spec *spec = codec->spec;
1213 hda_nid_t nid = codec->start_nid;
1215 spec->num_all_dacs = 0;
1216 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1217 for (i = 0; i < codec->num_nodes; i++, nid++) {
1218 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1220 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1221 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1224 spec->all_dacs[spec->num_all_dacs++] = nid;
1228 static int parse_output_paths(struct hda_codec *codec)
1230 struct hda_gen_spec *spec = codec->spec;
1231 struct auto_pin_cfg *cfg = &spec->autocfg;
1232 struct auto_pin_cfg *best_cfg;
1233 int best_badness = INT_MAX;
1235 bool fill_hardwired = true, fill_mio_first = true;
1236 bool best_wired = true, best_mio = true;
1237 bool hp_spk_swapped = false;
1239 fill_all_dac_nids(codec);
1241 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1247 badness = fill_and_eval_dacs(codec, fill_hardwired,
1253 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1254 cfg->line_out_type, fill_hardwired, fill_mio_first,
1256 debug_show_configs(spec, cfg);
1257 if (badness < best_badness) {
1258 best_badness = badness;
1260 best_wired = fill_hardwired;
1261 best_mio = fill_mio_first;
1265 fill_mio_first = !fill_mio_first;
1266 if (!fill_mio_first)
1268 fill_hardwired = !fill_hardwired;
1269 if (!fill_hardwired)
1273 hp_spk_swapped = true;
1274 if (cfg->speaker_outs > 0 &&
1275 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1276 cfg->hp_outs = cfg->line_outs;
1277 memcpy(cfg->hp_pins, cfg->line_out_pins,
1278 sizeof(cfg->hp_pins));
1279 cfg->line_outs = cfg->speaker_outs;
1280 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1281 sizeof(cfg->speaker_pins));
1282 cfg->speaker_outs = 0;
1283 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1284 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1285 fill_hardwired = true;
1288 if (cfg->hp_outs > 0 &&
1289 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1290 cfg->speaker_outs = cfg->line_outs;
1291 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1292 sizeof(cfg->speaker_pins));
1293 cfg->line_outs = cfg->hp_outs;
1294 memcpy(cfg->line_out_pins, cfg->hp_pins,
1295 sizeof(cfg->hp_pins));
1297 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1298 cfg->line_out_type = AUTO_PIN_HP_OUT;
1299 fill_hardwired = true;
1306 debug_badness("==> restoring best_cfg\n");
1308 fill_and_eval_dacs(codec, best_wired, best_mio);
1310 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1311 cfg->line_out_type, best_wired, best_mio);
1312 debug_show_configs(spec, cfg);
1314 if (cfg->line_out_pins[0]) {
1315 struct nid_path *path;
1316 path = snd_hda_get_nid_path(codec,
1317 spec->multiout.dac_nids[0],
1318 cfg->line_out_pins[0]);
1320 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1327 /* add playback controls from the parsed DAC table */
1328 static int create_multi_out_ctls(struct hda_codec *codec,
1329 const struct auto_pin_cfg *cfg)
1331 struct hda_gen_spec *spec = codec->spec;
1332 int i, err, noutputs;
1334 noutputs = cfg->line_outs;
1335 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1336 noutputs += spec->multi_ios;
1338 for (i = 0; i < noutputs; i++) {
1342 struct nid_path *path;
1344 dac = spec->multiout.dac_nids[i];
1347 if (i >= cfg->line_outs) {
1348 pin = spec->multi_io[i - 1].pin;
1350 name = channel_name[i];
1352 pin = cfg->line_out_pins[i];
1353 name = get_line_out_pfx(spec, i, true, &index);
1356 path = snd_hda_get_nid_path(codec, dac, pin);
1359 if (!name || !strcmp(name, "CLFE")) {
1361 err = add_vol_ctl(codec, "Center", 0, 1, path);
1364 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1367 err = add_sw_ctl(codec, "Center", 0, 1, path);
1370 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1374 err = add_stereo_vol(codec, name, index, path);
1377 err = add_stereo_sw(codec, name, index, path);
1385 static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1386 hda_nid_t dac, const char *pfx, int cidx)
1388 struct nid_path *path;
1391 path = snd_hda_get_nid_path(codec, dac, pin);
1394 /* bind volume control will be created in the case of dac = 0 */
1396 err = add_stereo_vol(codec, pfx, cidx, path);
1400 err = add_stereo_sw(codec, pfx, cidx, path);
1406 /* add playback controls for speaker and HP outputs */
1407 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1408 const hda_nid_t *pins, const hda_nid_t *dacs,
1411 struct hda_gen_spec *spec = codec->spec;
1412 struct hda_bind_ctls *ctl;
1416 if (!num_pins || !pins[0])
1419 if (num_pins == 1) {
1420 hda_nid_t dac = *dacs;
1422 dac = spec->multiout.dac_nids[0];
1423 return create_extra_out(codec, *pins, dac, pfx, 0);
1426 for (i = 0; i < num_pins; i++) {
1428 if (dacs[num_pins - 1])
1429 dac = dacs[i]; /* with individual volumes */
1432 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1433 err = create_extra_out(codec, pins[i], dac,
1435 } else if (num_pins >= 3) {
1436 snprintf(name, sizeof(name), "%s %s",
1437 pfx, channel_name[i]);
1438 err = create_extra_out(codec, pins[i], dac, name, 0);
1440 err = create_extra_out(codec, pins[i], dac, pfx, i);
1445 if (dacs[num_pins - 1])
1448 /* Let's create a bind-controls for volumes */
1449 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1453 for (i = 0; i < num_pins; i++) {
1455 struct nid_path *path;
1456 if (!pins[i] || !dacs[i])
1458 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1461 vol = look_for_out_vol_nid(codec, path);
1464 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1467 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1468 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1475 static int create_hp_out_ctls(struct hda_codec *codec)
1477 struct hda_gen_spec *spec = codec->spec;
1478 return create_extra_outs(codec, spec->autocfg.hp_outs,
1479 spec->autocfg.hp_pins,
1480 spec->multiout.hp_out_nid,
1484 static int create_speaker_out_ctls(struct hda_codec *codec)
1486 struct hda_gen_spec *spec = codec->spec;
1487 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1488 spec->autocfg.speaker_pins,
1489 spec->multiout.extra_out_nid,
1494 * independent HP controls
1497 static int indep_hp_info(struct snd_kcontrol *kcontrol,
1498 struct snd_ctl_elem_info *uinfo)
1500 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1503 static int indep_hp_get(struct snd_kcontrol *kcontrol,
1504 struct snd_ctl_elem_value *ucontrol)
1506 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1507 struct hda_gen_spec *spec = codec->spec;
1508 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1512 static int indep_hp_put(struct snd_kcontrol *kcontrol,
1513 struct snd_ctl_elem_value *ucontrol)
1515 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1516 struct hda_gen_spec *spec = codec->spec;
1517 unsigned int select = ucontrol->value.enumerated.item[0];
1520 mutex_lock(&spec->pcm_mutex);
1521 if (spec->active_streams) {
1526 if (spec->indep_hp_enabled != select) {
1527 spec->indep_hp_enabled = select;
1528 if (spec->indep_hp_enabled)
1529 spec->multiout.hp_out_nid[0] = 0;
1531 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1535 mutex_unlock(&spec->pcm_mutex);
1539 static const struct snd_kcontrol_new indep_hp_ctl = {
1540 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1541 .name = "Independent HP",
1542 .info = indep_hp_info,
1543 .get = indep_hp_get,
1544 .put = indep_hp_put,
1548 static int create_indep_hp_ctls(struct hda_codec *codec)
1550 struct hda_gen_spec *spec = codec->spec;
1552 if (!spec->indep_hp)
1554 if (!spec->multiout.hp_out_nid[0]) {
1559 spec->indep_hp_enabled = false;
1560 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1561 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1567 * channel mode enum control
1570 static int ch_mode_info(struct snd_kcontrol *kcontrol,
1571 struct snd_ctl_elem_info *uinfo)
1573 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1574 struct hda_gen_spec *spec = codec->spec;
1576 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1578 uinfo->value.enumerated.items = spec->multi_ios + 1;
1579 if (uinfo->value.enumerated.item > spec->multi_ios)
1580 uinfo->value.enumerated.item = spec->multi_ios;
1581 sprintf(uinfo->value.enumerated.name, "%dch",
1582 (uinfo->value.enumerated.item + 1) * 2);
1586 static int ch_mode_get(struct snd_kcontrol *kcontrol,
1587 struct snd_ctl_elem_value *ucontrol)
1589 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1590 struct hda_gen_spec *spec = codec->spec;
1591 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1595 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1597 struct hda_gen_spec *spec = codec->spec;
1598 hda_nid_t nid = spec->multi_io[idx].pin;
1599 struct nid_path *path;
1601 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1605 if (path->active == output)
1609 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1610 snd_hda_activate_path(codec, path, true, true);
1611 set_pin_eapd(codec, nid, true);
1613 set_pin_eapd(codec, nid, false);
1614 snd_hda_activate_path(codec, path, false, true);
1615 snd_hda_set_pin_ctl_cache(codec, nid,
1616 spec->multi_io[idx].ctl_in);
1621 static int ch_mode_put(struct snd_kcontrol *kcontrol,
1622 struct snd_ctl_elem_value *ucontrol)
1624 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1625 struct hda_gen_spec *spec = codec->spec;
1628 ch = ucontrol->value.enumerated.item[0];
1629 if (ch < 0 || ch > spec->multi_ios)
1631 if (ch == (spec->ext_channel_count - 1) / 2)
1633 spec->ext_channel_count = (ch + 1) * 2;
1634 for (i = 0; i < spec->multi_ios; i++)
1635 set_multi_io(codec, i, i < ch);
1636 spec->multiout.max_channels = max(spec->ext_channel_count,
1637 spec->const_channel_count);
1638 if (spec->need_dac_fix)
1639 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1643 static const struct snd_kcontrol_new channel_mode_enum = {
1644 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1645 .name = "Channel Mode",
1646 .info = ch_mode_info,
1651 static int create_multi_channel_mode(struct hda_codec *codec)
1653 struct hda_gen_spec *spec = codec->spec;
1655 if (spec->multi_ios > 0) {
1656 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
1663 * shared headphone/mic handling
1666 static void call_update_outputs(struct hda_codec *codec);
1668 /* for shared I/O, change the pin-control accordingly */
1669 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1671 struct hda_gen_spec *spec = codec->spec;
1673 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1674 /* NOTE: this assumes that there are only two inputs, the
1675 * first is the real internal mic and the second is HP/mic jack.
1678 val = snd_hda_get_default_vref(codec, pin);
1680 /* This pin does not have vref caps - let's enable vref on pin 0x18
1681 instead, as suggested by Realtek */
1682 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1683 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1684 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1685 if (vref_val != AC_PINCTL_VREF_HIZ)
1686 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1687 PIN_IN | (set_as_mic ? vref_val : 0));
1690 val = set_as_mic ? val | PIN_IN : PIN_HP;
1691 snd_hda_set_pin_ctl_cache(codec, pin, val);
1693 spec->automute_speaker = !set_as_mic;
1694 call_update_outputs(codec);
1697 /* create a shared input with the headphone out */
1698 static int create_shared_input(struct hda_codec *codec)
1700 struct hda_gen_spec *spec = codec->spec;
1701 struct auto_pin_cfg *cfg = &spec->autocfg;
1702 unsigned int defcfg;
1705 /* only one internal input pin? */
1706 if (cfg->num_inputs != 1)
1708 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1709 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1712 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1713 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1714 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1715 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1717 return 0; /* both not available */
1719 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1720 return 0; /* no input */
1722 cfg->inputs[1].pin = nid;
1723 cfg->inputs[1].type = AUTO_PIN_MIC;
1724 cfg->num_inputs = 2;
1725 spec->shared_mic_hp = 1;
1726 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1736 /* add the powersave loopback-list entry */
1737 static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1739 struct hda_amp_list *list;
1741 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1743 list = spec->loopback_list + spec->num_loopbacks;
1745 list->dir = HDA_INPUT;
1747 spec->num_loopbacks++;
1748 spec->loopback.amplist = spec->loopback_list;
1751 #define add_loopback_list(spec, mix, idx) /* NOP */
1754 /* create input playback/capture controls for the given pin */
1755 static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1756 const char *ctlname, int ctlidx,
1759 struct hda_gen_spec *spec = codec->spec;
1760 struct nid_path *path;
1764 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1765 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1766 return 0; /* no need for analog loopback */
1768 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
1771 print_nid_path("loopback", path);
1773 idx = path->idx[path->depth - 1];
1774 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1775 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1776 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
1779 path->ctls[NID_PATH_VOL_CTL] = val;
1782 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1783 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1784 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
1787 path->ctls[NID_PATH_MUTE_CTL] = val;
1790 path->active = true;
1791 add_loopback_list(spec, mix_nid, idx);
1795 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
1797 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1798 return (pincap & AC_PINCAP_IN) != 0;
1801 /* Parse the codec tree and retrieve ADCs */
1802 static int fill_adc_nids(struct hda_codec *codec)
1804 struct hda_gen_spec *spec = codec->spec;
1806 hda_nid_t *adc_nids = spec->adc_nids;
1807 int max_nums = ARRAY_SIZE(spec->adc_nids);
1810 nid = codec->start_nid;
1811 for (i = 0; i < codec->num_nodes; i++, nid++) {
1812 unsigned int caps = get_wcaps(codec, nid);
1813 int type = get_wcaps_type(caps);
1815 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1817 adc_nids[nums] = nid;
1818 if (++nums >= max_nums)
1821 spec->num_adc_nids = nums;
1825 /* filter out invalid adc_nids that don't give all active input pins;
1826 * if needed, check whether dynamic ADC-switching is available
1828 static int check_dyn_adc_switch(struct hda_codec *codec)
1830 struct hda_gen_spec *spec = codec->spec;
1831 struct hda_input_mux *imux = &spec->input_mux;
1832 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1838 for (n = 0; n < spec->num_adc_nids; n++) {
1839 adc = spec->adc_nids[n];
1840 for (i = 0; i < imux->num_items; i++) {
1841 pin = spec->imux_pins[i];
1842 if (!is_reachable_path(codec, pin, adc))
1845 if (i >= imux->num_items)
1846 adc_nids[nums++] = adc;
1850 if (spec->shared_mic_hp) {
1851 spec->shared_mic_hp = 0;
1852 imux->num_items = 1;
1856 /* check whether ADC-switch is possible */
1857 for (i = 0; i < imux->num_items; i++) {
1858 pin = spec->imux_pins[i];
1859 for (n = 0; n < spec->num_adc_nids; n++) {
1860 adc = spec->adc_nids[n];
1861 if (is_reachable_path(codec, pin, adc)) {
1862 spec->dyn_adc_idx[i] = n;
1868 snd_printdd("hda-codec: enabling ADC switching\n");
1869 spec->dyn_adc_switch = 1;
1870 } else if (nums != spec->num_adc_nids) {
1871 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1872 spec->num_adc_nids = nums;
1875 if (imux->num_items == 1 || spec->shared_mic_hp) {
1876 snd_printdd("hda-codec: reducing to a single ADC\n");
1877 spec->num_adc_nids = 1; /* reduce to a single ADC */
1880 /* single index for individual volumes ctls */
1881 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1882 spec->num_adc_nids = 1;
1888 * create playback/capture controls for input pins
1890 static int create_input_ctls(struct hda_codec *codec)
1892 struct hda_gen_spec *spec = codec->spec;
1893 const struct auto_pin_cfg *cfg = &spec->autocfg;
1894 hda_nid_t mixer = spec->mixer_nid;
1895 struct hda_input_mux *imux = &spec->input_mux;
1897 int i, c, err, type_idx = 0;
1898 const char *prev_label = NULL;
1900 num_adcs = fill_adc_nids(codec);
1904 for (i = 0; i < cfg->num_inputs; i++) {
1909 pin = cfg->inputs[i].pin;
1910 if (!is_input_pin(codec, pin))
1913 label = hda_get_autocfg_input_label(codec, cfg, i);
1914 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1915 label = "Headphone Mic";
1916 if (prev_label && !strcmp(label, prev_label))
1923 if (is_reachable_path(codec, pin, mixer)) {
1924 err = new_analog_input(codec, pin,
1925 label, type_idx, mixer);
1932 for (c = 0; c < num_adcs; c++) {
1933 struct nid_path *path;
1934 hda_nid_t adc = spec->adc_nids[c];
1936 if (!is_reachable_path(codec, pin, adc))
1938 path = snd_array_new(&spec->paths);
1941 memset(path, 0, sizeof(*path));
1942 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
1944 "invalid input path 0x%x -> 0x%x\n",
1949 print_nid_path("input", path);
1952 spec->imux_pins[imux->num_items] = pin;
1953 snd_hda_add_imux_item(imux, label,
1954 imux->num_items, NULL);
1968 /* get the ADC NID corresponding to the given index */
1969 static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1971 struct hda_gen_spec *spec = codec->spec;
1972 if (spec->dyn_adc_switch)
1973 adc_idx = spec->dyn_adc_idx[imux_idx];
1974 return spec->adc_nids[adc_idx];
1977 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
1980 static int mux_enum_info(struct snd_kcontrol *kcontrol,
1981 struct snd_ctl_elem_info *uinfo)
1983 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1984 struct hda_gen_spec *spec = codec->spec;
1985 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
1988 static int mux_enum_get(struct snd_kcontrol *kcontrol,
1989 struct snd_ctl_elem_value *ucontrol)
1991 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1992 struct hda_gen_spec *spec = codec->spec;
1993 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1995 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
1999 static int mux_enum_put(struct snd_kcontrol *kcontrol,
2000 struct snd_ctl_elem_value *ucontrol)
2002 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2003 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2004 return mux_select(codec, adc_idx,
2005 ucontrol->value.enumerated.item[0]);
2008 static const struct snd_kcontrol_new cap_src_temp = {
2009 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2010 .name = "Input Source",
2011 .info = mux_enum_info,
2012 .get = mux_enum_get,
2013 .put = mux_enum_put,
2017 * capture volume and capture switch ctls
2020 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2021 struct snd_ctl_elem_value *ucontrol);
2023 /* call the given amp update function for all amps in the imux list at once */
2024 static int cap_put_caller(struct snd_kcontrol *kcontrol,
2025 struct snd_ctl_elem_value *ucontrol,
2026 put_call_t func, int type)
2028 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2029 struct hda_gen_spec *spec = codec->spec;
2030 const struct hda_input_mux *imux;
2031 struct nid_path *path;
2032 int i, adc_idx, err = 0;
2034 imux = &spec->input_mux;
2035 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2036 mutex_lock(&codec->control_mutex);
2037 /* we use the cache-only update at first since multiple input paths
2038 * may shared the same amp; by updating only caches, the redundant
2039 * writes to hardware can be reduced.
2041 codec->cached_write = 1;
2042 for (i = 0; i < imux->num_items; i++) {
2043 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2044 get_adc_nid(codec, adc_idx, i));
2045 if (!path->ctls[type])
2047 kcontrol->private_value = path->ctls[type];
2048 err = func(kcontrol, ucontrol);
2053 codec->cached_write = 0;
2054 mutex_unlock(&codec->control_mutex);
2055 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
2056 if (err >= 0 && spec->cap_sync_hook)
2057 spec->cap_sync_hook(codec);
2061 /* capture volume ctl callbacks */
2062 #define cap_vol_info snd_hda_mixer_amp_volume_info
2063 #define cap_vol_get snd_hda_mixer_amp_volume_get
2064 #define cap_vol_tlv snd_hda_mixer_amp_tlv
2066 static int cap_vol_put(struct snd_kcontrol *kcontrol,
2067 struct snd_ctl_elem_value *ucontrol)
2069 return cap_put_caller(kcontrol, ucontrol,
2070 snd_hda_mixer_amp_volume_put,
2074 static const struct snd_kcontrol_new cap_vol_temp = {
2075 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2076 .name = "Capture Volume",
2077 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2078 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2079 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2080 .info = cap_vol_info,
2083 .tlv = { .c = cap_vol_tlv },
2086 /* capture switch ctl callbacks */
2087 #define cap_sw_info snd_ctl_boolean_stereo_info
2088 #define cap_sw_get snd_hda_mixer_amp_switch_get
2090 static int cap_sw_put(struct snd_kcontrol *kcontrol,
2091 struct snd_ctl_elem_value *ucontrol)
2093 return cap_put_caller(kcontrol, ucontrol,
2094 snd_hda_mixer_amp_switch_put,
2098 static const struct snd_kcontrol_new cap_sw_temp = {
2099 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2100 .name = "Capture Switch",
2101 .info = cap_sw_info,
2106 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2111 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2112 for (depth = 0; depth < 3; depth++) {
2113 if (depth >= path->depth)
2115 i = path->depth - depth - 1;
2116 nid = path->path[i];
2117 if (!path->ctls[NID_PATH_VOL_CTL]) {
2118 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2119 path->ctls[NID_PATH_VOL_CTL] =
2120 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2121 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2122 int idx = path->idx[i];
2123 if (!depth && codec->single_adc_amp)
2125 path->ctls[NID_PATH_VOL_CTL] =
2126 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2129 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2130 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2131 path->ctls[NID_PATH_MUTE_CTL] =
2132 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2133 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2134 int idx = path->idx[i];
2135 if (!depth && codec->single_adc_amp)
2137 path->ctls[NID_PATH_MUTE_CTL] =
2138 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2145 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2147 struct hda_gen_spec *spec = codec->spec;
2148 struct auto_pin_cfg *cfg = &spec->autocfg;
2152 if (!spec->inv_dmic_split)
2154 for (i = 0; i < cfg->num_inputs; i++) {
2155 if (cfg->inputs[i].pin != nid)
2157 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2159 val = snd_hda_codec_get_pincfg(codec, nid);
2160 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2165 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2166 int idx, bool is_switch, unsigned int ctl,
2169 struct hda_gen_spec *spec = codec->spec;
2171 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2172 const char *sfx = is_switch ? "Switch" : "Volume";
2173 unsigned int chs = inv_dmic ? 1 : 3;
2180 snprintf(tmpname, sizeof(tmpname),
2181 "%s Capture %s", label, sfx);
2183 snprintf(tmpname, sizeof(tmpname),
2185 err = add_control(spec, type, tmpname, idx,
2186 amp_val_replace_channels(ctl, chs));
2187 if (err < 0 || !inv_dmic)
2190 /* Make independent right kcontrol */
2192 snprintf(tmpname, sizeof(tmpname),
2193 "Inverted %s Capture %s", label, sfx);
2195 snprintf(tmpname, sizeof(tmpname),
2196 "Inverted Capture %s", sfx);
2197 return add_control(spec, type, tmpname, idx,
2198 amp_val_replace_channels(ctl, 2));
2201 /* create single (and simple) capture volume and switch controls */
2202 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2203 unsigned int vol_ctl, unsigned int sw_ctl,
2207 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2210 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2216 /* create bound capture volume and switch controls */
2217 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2218 unsigned int vol_ctl, unsigned int sw_ctl)
2220 struct hda_gen_spec *spec = codec->spec;
2221 struct snd_kcontrol_new *knew;
2224 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
2228 knew->private_value = vol_ctl;
2229 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2232 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
2236 knew->private_value = sw_ctl;
2237 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2242 /* return the vol ctl when used first in the imux list */
2243 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2245 struct hda_gen_spec *spec = codec->spec;
2246 struct nid_path *path;
2250 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2251 get_adc_nid(codec, 0, idx));
2254 ctl = path->ctls[type];
2257 for (i = 0; i < idx - 1; i++) {
2258 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2259 get_adc_nid(codec, 0, i));
2260 if (path && path->ctls[type] == ctl)
2266 /* create individual capture volume and switch controls per input */
2267 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2269 struct hda_gen_spec *spec = codec->spec;
2270 struct hda_input_mux *imux = &spec->input_mux;
2271 int i, err, type, type_idx = 0;
2272 const char *prev_label = NULL;
2274 for (i = 0; i < imux->num_items; i++) {
2277 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2278 if (prev_label && !strcmp(label, prev_label))
2283 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2285 for (type = 0; type < 2; type++) {
2286 err = add_single_cap_ctl(codec, label, type_idx, type,
2287 get_first_cap_ctl(codec, i, type),
2296 static int create_capture_mixers(struct hda_codec *codec)
2298 struct hda_gen_spec *spec = codec->spec;
2299 struct hda_input_mux *imux = &spec->input_mux;
2300 int i, n, nums, err;
2302 if (spec->dyn_adc_switch)
2305 nums = spec->num_adc_nids;
2307 if (!spec->auto_mic && imux->num_items > 1) {
2308 struct snd_kcontrol_new *knew;
2310 name = nums > 1 ? "Input Source" : "Capture Source";
2311 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
2317 for (n = 0; n < nums; n++) {
2319 bool inv_dmic = false;
2323 for (i = 0; i < imux->num_items; i++) {
2324 struct nid_path *path;
2325 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2326 get_adc_nid(codec, n, i));
2329 parse_capvol_in_path(codec, path);
2331 vol = path->ctls[NID_PATH_VOL_CTL];
2332 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2335 sw = path->ctls[NID_PATH_MUTE_CTL];
2336 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2338 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2343 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2345 else if (!spec->multi_cap_vol)
2346 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2348 err = create_multi_cap_vol_ctl(codec);
2357 * add mic boosts if needed
2359 static int parse_mic_boost(struct hda_codec *codec)
2361 struct hda_gen_spec *spec = codec->spec;
2362 struct auto_pin_cfg *cfg = &spec->autocfg;
2366 const char *prev_label = NULL;
2368 for (i = 0; i < cfg->num_inputs; i++) {
2369 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2371 nid = cfg->inputs[i].pin;
2372 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2374 char boost_label[32];
2375 struct nid_path *path;
2378 label = hda_get_autocfg_input_label(codec, cfg, i);
2379 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2380 label = "Headphone Mic";
2381 if (prev_label && !strcmp(label, prev_label))
2387 snprintf(boost_label, sizeof(boost_label),
2388 "%s Boost Volume", label);
2389 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2390 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2391 boost_label, type_idx, val);
2395 path = snd_hda_get_nid_path(codec, nid, 0);
2397 path->ctls[NID_PATH_BOOST_CTL] = val;
2404 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2406 static void parse_digital(struct hda_codec *codec)
2408 struct hda_gen_spec *spec = codec->spec;
2409 struct nid_path *path;
2413 /* support multiple SPDIFs; the secondary is set up as a slave */
2415 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2416 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2417 dig_nid = look_for_dac(codec, pin, true);
2420 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
2423 print_nid_path("digout", path);
2424 path->active = true;
2426 spec->multiout.dig_out_nid = dig_nid;
2427 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2429 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2430 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2432 spec->slave_dig_outs[nums - 1] = dig_nid;
2437 if (spec->autocfg.dig_in_pin) {
2438 dig_nid = codec->start_nid;
2439 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2440 unsigned int wcaps = get_wcaps(codec, dig_nid);
2441 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2443 if (!(wcaps & AC_WCAP_DIGITAL))
2445 path = snd_hda_add_new_path(codec,
2446 spec->autocfg.dig_in_pin,
2447 dig_nid, HDA_PARSE_ALL);
2449 print_nid_path("digin", path);
2450 path->active = true;
2451 spec->dig_in_nid = dig_nid;
2460 * input MUX handling
2463 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2465 /* select the given imux item; either unmute exclusively or select the route */
2466 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2469 struct hda_gen_spec *spec = codec->spec;
2470 const struct hda_input_mux *imux;
2471 struct nid_path *path;
2473 imux = &spec->input_mux;
2474 if (!imux->num_items)
2477 if (idx >= imux->num_items)
2478 idx = imux->num_items - 1;
2479 if (spec->cur_mux[adc_idx] == idx)
2482 path = snd_hda_get_nid_path(codec,
2483 spec->imux_pins[spec->cur_mux[adc_idx]],
2484 spec->adc_nids[adc_idx]);
2488 snd_hda_activate_path(codec, path, false, false);
2490 spec->cur_mux[adc_idx] = idx;
2492 if (spec->shared_mic_hp)
2493 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2495 if (spec->dyn_adc_switch)
2496 dyn_adc_pcm_resetup(codec, idx);
2498 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2499 get_adc_nid(codec, adc_idx, idx));
2504 snd_hda_activate_path(codec, path, true, false);
2505 if (spec->cap_sync_hook)
2506 spec->cap_sync_hook(codec);
2512 * Jack detections for HP auto-mute and mic-switch
2515 /* check each pin in the given array; returns true if any of them is plugged */
2516 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2520 for (i = 0; i < num_pins; i++) {
2521 hda_nid_t nid = pins[i];
2524 present |= snd_hda_jack_detect(codec, nid);
2529 /* standard HP/line-out auto-mute helper */
2530 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2531 bool mute, bool hp_out)
2533 struct hda_gen_spec *spec = codec->spec;
2534 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2537 for (i = 0; i < num_pins; i++) {
2538 hda_nid_t nid = pins[i];
2542 /* don't reset VREF value in case it's controlling
2543 * the amp (see alc861_fixup_asus_amp_vref_0f())
2545 if (spec->keep_vref_in_automute) {
2546 val = snd_hda_codec_read(codec, nid, 0,
2547 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2552 snd_hda_set_pin_ctl_cache(codec, nid, val);
2553 set_pin_eapd(codec, nid, !mute);
2557 /* Toggle outputs muting */
2558 void snd_hda_gen_update_outputs(struct hda_codec *codec)
2560 struct hda_gen_spec *spec = codec->spec;
2563 /* Control HP pins/amps depending on master_mute state;
2564 * in general, HP pins/amps control should be enabled in all cases,
2565 * but currently set only for master_mute, just to be safe
2567 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2568 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2569 spec->autocfg.hp_pins, spec->master_mute, true);
2571 if (!spec->automute_speaker)
2574 on = spec->hp_jack_present | spec->line_jack_present;
2575 on |= spec->master_mute;
2576 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2577 spec->autocfg.speaker_pins, on, false);
2579 /* toggle line-out mutes if needed, too */
2580 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2581 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2582 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2584 if (!spec->automute_lo)
2587 on = spec->hp_jack_present;
2588 on |= spec->master_mute;
2589 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2590 spec->autocfg.line_out_pins, on, false);
2592 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
2594 static void call_update_outputs(struct hda_codec *codec)
2596 struct hda_gen_spec *spec = codec->spec;
2597 if (spec->automute_hook)
2598 spec->automute_hook(codec);
2600 snd_hda_gen_update_outputs(codec);
2603 /* standard HP-automute helper */
2604 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2606 struct hda_gen_spec *spec = codec->spec;
2608 spec->hp_jack_present =
2609 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2610 spec->autocfg.hp_pins);
2611 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2613 call_update_outputs(codec);
2615 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
2617 /* standard line-out-automute helper */
2618 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2620 struct hda_gen_spec *spec = codec->spec;
2622 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2624 /* check LO jack only when it's different from HP */
2625 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2628 spec->line_jack_present =
2629 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2630 spec->autocfg.line_out_pins);
2631 if (!spec->automute_speaker || !spec->detect_lo)
2633 call_update_outputs(codec);
2635 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
2637 /* standard mic auto-switch helper */
2638 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
2640 struct hda_gen_spec *spec = codec->spec;
2643 if (!spec->auto_mic)
2646 for (i = spec->am_num_entries - 1; i > 0; i--) {
2647 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2648 mux_select(codec, 0, spec->am_entry[i].idx);
2652 mux_select(codec, 0, spec->am_entry[0].idx);
2654 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
2657 * Auto-Mute mode mixer enum support
2659 static int automute_mode_info(struct snd_kcontrol *kcontrol,
2660 struct snd_ctl_elem_info *uinfo)
2662 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2663 struct hda_gen_spec *spec = codec->spec;
2664 static const char * const texts3[] = {
2665 "Disabled", "Speaker Only", "Line Out+Speaker"
2668 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2669 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2670 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2673 static int automute_mode_get(struct snd_kcontrol *kcontrol,
2674 struct snd_ctl_elem_value *ucontrol)
2676 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2677 struct hda_gen_spec *spec = codec->spec;
2678 unsigned int val = 0;
2679 if (spec->automute_speaker)
2681 if (spec->automute_lo)
2684 ucontrol->value.enumerated.item[0] = val;
2688 static int automute_mode_put(struct snd_kcontrol *kcontrol,
2689 struct snd_ctl_elem_value *ucontrol)
2691 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2692 struct hda_gen_spec *spec = codec->spec;
2694 switch (ucontrol->value.enumerated.item[0]) {
2696 if (!spec->automute_speaker && !spec->automute_lo)
2698 spec->automute_speaker = 0;
2699 spec->automute_lo = 0;
2702 if (spec->automute_speaker_possible) {
2703 if (!spec->automute_lo && spec->automute_speaker)
2705 spec->automute_speaker = 1;
2706 spec->automute_lo = 0;
2707 } else if (spec->automute_lo_possible) {
2708 if (spec->automute_lo)
2710 spec->automute_lo = 1;
2715 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2717 if (spec->automute_speaker && spec->automute_lo)
2719 spec->automute_speaker = 1;
2720 spec->automute_lo = 1;
2725 call_update_outputs(codec);
2729 static const struct snd_kcontrol_new automute_mode_enum = {
2730 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2731 .name = "Auto-Mute Mode",
2732 .info = automute_mode_info,
2733 .get = automute_mode_get,
2734 .put = automute_mode_put,
2737 static int add_automute_mode_enum(struct hda_codec *codec)
2739 struct hda_gen_spec *spec = codec->spec;
2741 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
2747 * Check the availability of HP/line-out auto-mute;
2748 * Set up appropriately if really supported
2750 static int check_auto_mute_availability(struct hda_codec *codec)
2752 struct hda_gen_spec *spec = codec->spec;
2753 struct auto_pin_cfg *cfg = &spec->autocfg;
2757 if (cfg->hp_pins[0])
2759 if (cfg->line_out_pins[0])
2761 if (cfg->speaker_pins[0])
2763 if (present < 2) /* need two different output types */
2766 if (!cfg->speaker_pins[0] &&
2767 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2768 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2769 sizeof(cfg->speaker_pins));
2770 cfg->speaker_outs = cfg->line_outs;
2773 if (!cfg->hp_pins[0] &&
2774 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2775 memcpy(cfg->hp_pins, cfg->line_out_pins,
2776 sizeof(cfg->hp_pins));
2777 cfg->hp_outs = cfg->line_outs;
2780 for (i = 0; i < cfg->hp_outs; i++) {
2781 hda_nid_t nid = cfg->hp_pins[i];
2782 if (!is_jack_detectable(codec, nid))
2784 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2786 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
2787 spec->hp_automute_hook ?
2788 spec->hp_automute_hook :
2789 snd_hda_gen_hp_automute);
2790 spec->detect_hp = 1;
2793 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2794 if (cfg->speaker_outs)
2795 for (i = 0; i < cfg->line_outs; i++) {
2796 hda_nid_t nid = cfg->line_out_pins[i];
2797 if (!is_jack_detectable(codec, nid))
2799 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2800 snd_hda_jack_detect_enable_callback(codec, nid,
2801 HDA_GEN_FRONT_EVENT,
2802 spec->line_automute_hook ?
2803 spec->line_automute_hook :
2804 snd_hda_gen_line_automute);
2805 spec->detect_lo = 1;
2807 spec->automute_lo_possible = spec->detect_hp;
2810 spec->automute_speaker_possible = cfg->speaker_outs &&
2811 (spec->detect_hp || spec->detect_lo);
2813 spec->automute_lo = spec->automute_lo_possible;
2814 spec->automute_speaker = spec->automute_speaker_possible;
2816 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2817 /* create a control for automute mode */
2818 err = add_automute_mode_enum(codec);
2825 /* return the position of NID in the list, or -1 if not found */
2826 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2829 for (i = 0; i < nums; i++)
2835 /* check whether all auto-mic pins are valid; setup indices if OK */
2836 static bool auto_mic_check_imux(struct hda_codec *codec)
2838 struct hda_gen_spec *spec = codec->spec;
2839 const struct hda_input_mux *imux;
2842 imux = &spec->input_mux;
2843 for (i = 0; i < spec->am_num_entries; i++) {
2844 spec->am_entry[i].idx =
2845 find_idx_in_nid_list(spec->am_entry[i].pin,
2846 spec->imux_pins, imux->num_items);
2847 if (spec->am_entry[i].idx < 0)
2848 return false; /* no corresponding imux */
2851 /* we don't need the jack detection for the first pin */
2852 for (i = 1; i < spec->am_num_entries; i++)
2853 snd_hda_jack_detect_enable_callback(codec,
2854 spec->am_entry[i].pin,
2856 spec->mic_autoswitch_hook ?
2857 spec->mic_autoswitch_hook :
2858 snd_hda_gen_mic_autoswitch);
2862 static int compare_attr(const void *ap, const void *bp)
2864 const struct automic_entry *a = ap;
2865 const struct automic_entry *b = bp;
2866 return (int)(a->attr - b->attr);
2870 * Check the availability of auto-mic switch;
2871 * Set up if really supported
2873 static int check_auto_mic_availability(struct hda_codec *codec)
2875 struct hda_gen_spec *spec = codec->spec;
2876 struct auto_pin_cfg *cfg = &spec->autocfg;
2882 for (i = 0; i < cfg->num_inputs; i++) {
2883 hda_nid_t nid = cfg->inputs[i].pin;
2885 attr = snd_hda_codec_get_pincfg(codec, nid);
2886 attr = snd_hda_get_input_pin_attr(attr);
2887 if (types & (1 << attr))
2888 return 0; /* already occupied */
2890 case INPUT_PIN_ATTR_INT:
2891 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2892 return 0; /* invalid type */
2894 case INPUT_PIN_ATTR_UNUSED:
2895 return 0; /* invalid entry */
2897 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2898 return 0; /* invalid type */
2899 if (!spec->line_in_auto_switch &&
2900 cfg->inputs[i].type != AUTO_PIN_MIC)
2901 return 0; /* only mic is allowed */
2902 if (!is_jack_detectable(codec, nid))
2903 return 0; /* no unsol support */
2906 if (num_pins >= MAX_AUTO_MIC_PINS)
2908 types |= (1 << attr);
2909 spec->am_entry[num_pins].pin = nid;
2910 spec->am_entry[num_pins].attr = attr;
2917 spec->am_num_entries = num_pins;
2918 /* sort the am_entry in the order of attr so that the pin with a
2919 * higher attr will be selected when the jack is plugged.
2921 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2922 compare_attr, NULL);
2924 if (!auto_mic_check_imux(codec))
2928 spec->num_adc_nids = 1;
2929 spec->cur_mux[0] = spec->am_entry[0].idx;
2930 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2931 spec->am_entry[0].pin,
2932 spec->am_entry[1].pin,
2933 spec->am_entry[2].pin);
2940 * Parse the given BIOS configuration and set up the hda_gen_spec
2942 * return 1 if successful, 0 if the proper config is not found,
2943 * or a negative error code
2945 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
2946 struct auto_pin_cfg *cfg)
2948 struct hda_gen_spec *spec = codec->spec;
2951 if (cfg != &spec->autocfg) {
2952 spec->autocfg = *cfg;
2953 cfg = &spec->autocfg;
2956 if (!cfg->line_outs) {
2957 if (cfg->dig_outs || cfg->dig_in_pin) {
2958 spec->multiout.max_channels = 2;
2959 spec->no_analog = 1;
2962 return 0; /* can't find valid BIOS pin config */
2965 if (!spec->no_primary_hp &&
2966 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2967 cfg->line_outs <= cfg->hp_outs) {
2968 /* use HP as primary out */
2969 cfg->speaker_outs = cfg->line_outs;
2970 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2971 sizeof(cfg->speaker_pins));
2972 cfg->line_outs = cfg->hp_outs;
2973 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2975 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2976 cfg->line_out_type = AUTO_PIN_HP_OUT;
2979 err = parse_output_paths(codec);
2982 err = create_multi_channel_mode(codec);
2985 err = create_multi_out_ctls(codec, cfg);
2988 err = create_hp_out_ctls(codec);
2991 err = create_speaker_out_ctls(codec);
2994 err = create_indep_hp_ctls(codec);
2997 err = create_shared_input(codec);
3000 err = create_input_ctls(codec);
3004 /* check the multiple speaker pins */
3005 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
3006 spec->const_channel_count = cfg->line_outs * 2;
3008 spec->const_channel_count = cfg->speaker_outs * 2;
3010 if (spec->multi_ios > 0)
3011 spec->multiout.max_channels = max(spec->ext_channel_count,
3012 spec->const_channel_count);
3014 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3016 err = check_auto_mute_availability(codec);
3020 err = check_dyn_adc_switch(codec);
3024 if (!spec->shared_mic_hp) {
3025 err = check_auto_mic_availability(codec);
3030 err = create_capture_mixers(codec);
3034 err = parse_mic_boost(codec);
3039 parse_digital(codec);
3043 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
3047 * Build control elements
3050 /* slave controls for virtual master */
3051 static const char * const slave_pfxs[] = {
3052 "Front", "Surround", "Center", "LFE", "Side",
3053 "Headphone", "Speaker", "Mono", "Line Out",
3054 "CLFE", "Bass Speaker", "PCM",
3058 int snd_hda_gen_build_controls(struct hda_codec *codec)
3060 struct hda_gen_spec *spec = codec->spec;
3063 if (spec->kctls.used) {
3064 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3069 if (spec->multiout.dig_out_nid) {
3070 err = snd_hda_create_dig_out_ctls(codec,
3071 spec->multiout.dig_out_nid,
3072 spec->multiout.dig_out_nid,
3073 spec->pcm_rec[1].pcm_type);
3076 if (!spec->no_analog) {
3077 err = snd_hda_create_spdif_share_sw(codec,
3081 spec->multiout.share_spdif = 1;
3084 if (spec->dig_in_nid) {
3085 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3090 /* if we have no master control, let's create it */
3091 if (!spec->no_analog &&
3092 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3093 unsigned int vmaster_tlv[4];
3094 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3095 HDA_OUTPUT, vmaster_tlv);
3096 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3097 vmaster_tlv, slave_pfxs,
3102 if (!spec->no_analog &&
3103 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3104 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3107 true, &spec->vmaster_mute.sw_kctl);
3110 if (spec->vmaster_mute.hook)
3111 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3112 spec->vmaster_mute_enum);
3115 free_kctls(spec); /* no longer needed */
3117 if (spec->shared_mic_hp) {
3119 int nid = spec->autocfg.inputs[1].pin;
3120 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3123 err = snd_hda_jack_detect_enable(codec, nid, 0);
3128 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3134 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3142 * Analog playback callbacks
3144 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3145 struct hda_codec *codec,
3146 struct snd_pcm_substream *substream)
3148 struct hda_gen_spec *spec = codec->spec;
3151 mutex_lock(&spec->pcm_mutex);
3152 err = snd_hda_multi_out_analog_open(codec,
3153 &spec->multiout, substream,
3156 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3157 mutex_unlock(&spec->pcm_mutex);
3161 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3162 struct hda_codec *codec,
3163 unsigned int stream_tag,
3164 unsigned int format,
3165 struct snd_pcm_substream *substream)
3167 struct hda_gen_spec *spec = codec->spec;
3168 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3169 stream_tag, format, substream);
3172 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3173 struct hda_codec *codec,
3174 struct snd_pcm_substream *substream)
3176 struct hda_gen_spec *spec = codec->spec;
3177 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3180 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3181 struct hda_codec *codec,
3182 struct snd_pcm_substream *substream)
3184 struct hda_gen_spec *spec = codec->spec;
3185 mutex_lock(&spec->pcm_mutex);
3186 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3187 mutex_unlock(&spec->pcm_mutex);
3191 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3192 struct hda_codec *codec,
3193 struct snd_pcm_substream *substream)
3195 struct hda_gen_spec *spec = codec->spec;
3198 mutex_lock(&spec->pcm_mutex);
3199 if (!spec->indep_hp_enabled)
3202 spec->active_streams |= 1 << STREAM_INDEP_HP;
3203 mutex_unlock(&spec->pcm_mutex);
3207 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3208 struct hda_codec *codec,
3209 struct snd_pcm_substream *substream)
3211 struct hda_gen_spec *spec = codec->spec;
3212 mutex_lock(&spec->pcm_mutex);
3213 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3214 mutex_unlock(&spec->pcm_mutex);
3221 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3222 struct hda_codec *codec,
3223 struct snd_pcm_substream *substream)
3225 struct hda_gen_spec *spec = codec->spec;
3226 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3229 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3230 struct hda_codec *codec,
3231 unsigned int stream_tag,
3232 unsigned int format,
3233 struct snd_pcm_substream *substream)
3235 struct hda_gen_spec *spec = codec->spec;
3236 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3237 stream_tag, format, substream);
3240 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3241 struct hda_codec *codec,
3242 struct snd_pcm_substream *substream)
3244 struct hda_gen_spec *spec = codec->spec;
3245 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3248 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3249 struct hda_codec *codec,
3250 struct snd_pcm_substream *substream)
3252 struct hda_gen_spec *spec = codec->spec;
3253 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3259 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3260 struct hda_codec *codec,
3261 unsigned int stream_tag,
3262 unsigned int format,
3263 struct snd_pcm_substream *substream)
3265 struct hda_gen_spec *spec = codec->spec;
3267 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
3268 stream_tag, 0, format);
3272 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3273 struct hda_codec *codec,
3274 struct snd_pcm_substream *substream)
3276 struct hda_gen_spec *spec = codec->spec;
3278 snd_hda_codec_cleanup_stream(codec,
3279 spec->adc_nids[substream->number + 1]);
3285 static const struct hda_pcm_stream pcm_analog_playback = {
3289 /* NID is set in build_pcms */
3291 .open = playback_pcm_open,
3292 .close = playback_pcm_close,
3293 .prepare = playback_pcm_prepare,
3294 .cleanup = playback_pcm_cleanup
3298 static const struct hda_pcm_stream pcm_analog_capture = {
3302 /* NID is set in build_pcms */
3305 static const struct hda_pcm_stream pcm_analog_alt_playback = {
3309 /* NID is set in build_pcms */
3311 .open = alt_playback_pcm_open,
3312 .close = alt_playback_pcm_close
3316 static const struct hda_pcm_stream pcm_analog_alt_capture = {
3317 .substreams = 2, /* can be overridden */
3320 /* NID is set in build_pcms */
3322 .prepare = alt_capture_pcm_prepare,
3323 .cleanup = alt_capture_pcm_cleanup
3327 static const struct hda_pcm_stream pcm_digital_playback = {
3331 /* NID is set in build_pcms */
3333 .open = dig_playback_pcm_open,
3334 .close = dig_playback_pcm_close,
3335 .prepare = dig_playback_pcm_prepare,
3336 .cleanup = dig_playback_pcm_cleanup
3340 static const struct hda_pcm_stream pcm_digital_capture = {
3344 /* NID is set in build_pcms */
3347 /* Used by build_pcms to flag that a PCM has no playback stream */
3348 static const struct hda_pcm_stream pcm_null_stream = {
3355 * dynamic changing ADC PCM streams
3357 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3359 struct hda_gen_spec *spec = codec->spec;
3360 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3362 if (spec->cur_adc && spec->cur_adc != new_adc) {
3363 /* stream is running, let's swap the current ADC */
3364 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3365 spec->cur_adc = new_adc;
3366 snd_hda_codec_setup_stream(codec, new_adc,
3367 spec->cur_adc_stream_tag, 0,
3368 spec->cur_adc_format);
3374 /* analog capture with dynamic dual-adc changes */
3375 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3376 struct hda_codec *codec,
3377 unsigned int stream_tag,
3378 unsigned int format,
3379 struct snd_pcm_substream *substream)
3381 struct hda_gen_spec *spec = codec->spec;
3382 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3383 spec->cur_adc_stream_tag = stream_tag;
3384 spec->cur_adc_format = format;
3385 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3389 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3390 struct hda_codec *codec,
3391 struct snd_pcm_substream *substream)
3393 struct hda_gen_spec *spec = codec->spec;
3394 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3399 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3403 .nid = 0, /* fill later */
3405 .prepare = dyn_adc_capture_pcm_prepare,
3406 .cleanup = dyn_adc_capture_pcm_cleanup
3410 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3411 const char *chip_name)
3417 strlcpy(str, chip_name, len);
3419 /* drop non-alnum chars after a space */
3420 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3421 if (!isalnum(p[1])) {
3426 strlcat(str, sfx, len);
3429 /* build PCM streams based on the parsed results */
3430 int snd_hda_gen_build_pcms(struct hda_codec *codec)
3432 struct hda_gen_spec *spec = codec->spec;
3433 struct hda_pcm *info = spec->pcm_rec;
3434 const struct hda_pcm_stream *p;
3435 bool have_multi_adcs;
3437 codec->num_pcms = 1;
3438 codec->pcm_info = info;
3440 if (spec->no_analog)
3443 fill_pcm_stream_name(spec->stream_name_analog,
3444 sizeof(spec->stream_name_analog),
3445 " Analog", codec->chip_name);
3446 info->name = spec->stream_name_analog;
3448 if (spec->multiout.num_dacs > 0) {
3449 p = spec->stream_analog_playback;
3451 p = &pcm_analog_playback;
3452 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3453 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3454 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3455 spec->multiout.max_channels;
3456 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3457 spec->autocfg.line_outs == 2)
3458 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3461 if (spec->num_adc_nids) {
3462 p = spec->stream_analog_capture;
3464 if (spec->dyn_adc_switch)
3465 p = &dyn_adc_pcm_analog_capture;
3467 p = &pcm_analog_capture;
3469 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3470 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3474 /* SPDIF for stream index #1 */
3475 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
3476 fill_pcm_stream_name(spec->stream_name_digital,
3477 sizeof(spec->stream_name_digital),
3478 " Digital", codec->chip_name);
3479 codec->num_pcms = 2;
3480 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3481 info = spec->pcm_rec + 1;
3482 info->name = spec->stream_name_digital;
3483 if (spec->dig_out_type)
3484 info->pcm_type = spec->dig_out_type;
3486 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3487 if (spec->multiout.dig_out_nid) {
3488 p = spec->stream_digital_playback;
3490 p = &pcm_digital_playback;
3491 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3492 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3494 if (spec->dig_in_nid) {
3495 p = spec->stream_digital_capture;
3497 p = &pcm_digital_capture;
3498 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3499 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3503 if (spec->no_analog)
3506 /* If the use of more than one ADC is requested for the current
3507 * model, configure a second analog capture-only PCM.
3509 have_multi_adcs = (spec->num_adc_nids > 1) &&
3510 !spec->dyn_adc_switch && !spec->auto_mic;
3511 /* Additional Analaog capture for index #2 */
3512 if (spec->alt_dac_nid || have_multi_adcs) {
3513 codec->num_pcms = 3;
3514 info = spec->pcm_rec + 2;
3515 info->name = spec->stream_name_analog;
3516 if (spec->alt_dac_nid) {
3517 p = spec->stream_analog_alt_playback;
3519 p = &pcm_analog_alt_playback;
3520 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3521 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3524 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3526 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3528 if (have_multi_adcs) {
3529 p = spec->stream_analog_alt_capture;
3531 p = &pcm_analog_alt_capture;
3532 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3533 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3535 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3536 spec->num_adc_nids - 1;
3538 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3540 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3546 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3550 * Standard auto-parser initializations
3553 /* configure the path from the given dac to the pin as the proper output */
3554 static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3555 int pin_type, hda_nid_t dac)
3557 struct nid_path *path;
3559 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3560 path = snd_hda_get_nid_path(codec, dac, pin);
3563 snd_hda_activate_path(codec, path, path->active, true);
3564 set_pin_eapd(codec, pin, path->active);
3567 /* initialize primary output paths */
3568 static void init_multi_out(struct hda_codec *codec)
3570 struct hda_gen_spec *spec = codec->spec;
3575 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3580 for (i = 0; i < spec->autocfg.line_outs; i++) {
3581 nid = spec->autocfg.line_out_pins[i];
3583 dac = spec->multiout.dac_nids[i];
3585 dac = spec->multiout.dac_nids[0];
3586 set_output_and_unmute(codec, nid, pin_type, dac);
3592 static void __init_extra_out(struct hda_codec *codec, int num_outs,
3593 hda_nid_t *pins, hda_nid_t *dacs, int type)
3595 struct hda_gen_spec *spec = codec->spec;
3599 for (i = 0; i < num_outs; i++) {
3605 if (i > 0 && dacs[0])
3608 dac = spec->multiout.dac_nids[0];
3610 set_output_and_unmute(codec, pin, type, dac);
3614 /* initialize hp and speaker paths */
3615 static void init_extra_out(struct hda_codec *codec)
3617 struct hda_gen_spec *spec = codec->spec;
3619 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3620 __init_extra_out(codec, spec->autocfg.hp_outs,
3621 spec->autocfg.hp_pins,
3622 spec->multiout.hp_out_nid, PIN_HP);
3623 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3624 __init_extra_out(codec, spec->autocfg.speaker_outs,
3625 spec->autocfg.speaker_pins,
3626 spec->multiout.extra_out_nid, PIN_OUT);
3629 /* initialize multi-io paths */
3630 static void init_multi_io(struct hda_codec *codec)
3632 struct hda_gen_spec *spec = codec->spec;
3635 for (i = 0; i < spec->multi_ios; i++) {
3636 hda_nid_t pin = spec->multi_io[i].pin;
3637 struct nid_path *path;
3638 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3641 if (!spec->multi_io[i].ctl_in)
3642 spec->multi_io[i].ctl_in =
3643 snd_hda_codec_update_cache(codec, pin, 0,
3644 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3645 snd_hda_activate_path(codec, path, path->active, true);
3649 /* set up the input pin config, depending on the given auto-pin type */
3650 static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3653 unsigned int val = PIN_IN;
3654 if (auto_pin_type == AUTO_PIN_MIC)
3655 val |= snd_hda_get_default_vref(codec, nid);
3656 snd_hda_set_pin_ctl_cache(codec, nid, val);
3659 /* set up input pins and loopback paths */
3660 static void init_analog_input(struct hda_codec *codec)
3662 struct hda_gen_spec *spec = codec->spec;
3663 struct auto_pin_cfg *cfg = &spec->autocfg;
3666 for (i = 0; i < cfg->num_inputs; i++) {
3667 hda_nid_t nid = cfg->inputs[i].pin;
3668 if (is_input_pin(codec, nid))
3669 set_input_pin(codec, nid, cfg->inputs[i].type);
3671 /* init loopback inputs */
3672 if (spec->mixer_nid) {
3673 struct nid_path *path;
3674 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3676 snd_hda_activate_path(codec, path,
3677 path->active, false);
3682 /* initialize ADC paths */
3683 static void init_input_src(struct hda_codec *codec)
3685 struct hda_gen_spec *spec = codec->spec;
3686 struct hda_input_mux *imux = &spec->input_mux;
3687 struct nid_path *path;
3690 if (spec->dyn_adc_switch)
3693 nums = spec->num_adc_nids;
3695 for (c = 0; c < nums; c++) {
3696 for (i = 0; i < imux->num_items; i++) {
3697 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3698 get_adc_nid(codec, c, i));
3700 bool active = path->active;
3701 if (i == spec->cur_mux[c])
3703 snd_hda_activate_path(codec, path, active, false);
3708 if (spec->shared_mic_hp)
3709 update_shared_mic_hp(codec, spec->cur_mux[0]);
3711 if (spec->cap_sync_hook)
3712 spec->cap_sync_hook(codec);
3715 /* set right pin controls for digital I/O */
3716 static void init_digital(struct hda_codec *codec)
3718 struct hda_gen_spec *spec = codec->spec;
3722 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3723 pin = spec->autocfg.dig_out_pins[i];
3726 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3728 pin = spec->autocfg.dig_in_pin;
3730 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
3733 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3734 * invalid unsol tags by some reason
3736 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3740 for (i = 0; i < codec->init_pins.used; i++) {
3741 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3742 hda_nid_t nid = pin->nid;
3743 if (is_jack_detectable(codec, nid) &&
3744 !snd_hda_jack_tbl_get(codec, nid))
3745 snd_hda_codec_update_cache(codec, nid, 0,
3746 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3750 int snd_hda_gen_init(struct hda_codec *codec)
3752 struct hda_gen_spec *spec = codec->spec;
3754 if (spec->init_hook)
3755 spec->init_hook(codec);
3757 snd_hda_apply_verbs(codec);
3759 codec->cached_write = 1;
3761 init_multi_out(codec);
3762 init_extra_out(codec);
3763 init_multi_io(codec);
3764 init_analog_input(codec);
3765 init_input_src(codec);
3766 init_digital(codec);
3768 clear_unsol_on_unused_pins(codec);
3770 /* call init functions of standard auto-mute helpers */
3771 snd_hda_gen_hp_automute(codec, NULL);
3772 snd_hda_gen_line_automute(codec, NULL);
3773 snd_hda_gen_mic_autoswitch(codec, NULL);
3775 snd_hda_codec_flush_amp_cache(codec);
3776 snd_hda_codec_flush_cmd_cache(codec);
3778 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3779 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3781 hda_call_check_power_status(codec, 0x01);
3784 EXPORT_SYMBOL(snd_hda_gen_init);
3788 * the generic codec support
3792 static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3794 struct hda_gen_spec *spec = codec->spec;
3795 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3799 static void generic_free(struct hda_codec *codec)
3801 snd_hda_gen_spec_free(codec->spec);
3806 static const struct hda_codec_ops generic_patch_ops = {
3807 .build_controls = snd_hda_gen_build_controls,
3808 .build_pcms = snd_hda_gen_build_pcms,
3809 .init = snd_hda_gen_init,
3810 .free = generic_free,
3811 .unsol_event = snd_hda_jack_unsol_event,
3813 .check_power_status = generic_check_power_status,
3817 int snd_hda_parse_generic_codec(struct hda_codec *codec)
3819 struct hda_gen_spec *spec;
3822 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3825 snd_hda_gen_spec_init(spec);
3828 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3832 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
3836 codec->patch_ops = generic_patch_ops;
3840 generic_free(codec);
3843 EXPORT_SYMBOL(snd_hda_parse_generic_codec);