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 static struct nid_path *get_nid_path(struct hda_codec *codec,
120 hda_nid_t from_nid, hda_nid_t to_nid,
123 struct hda_gen_spec *spec = codec->spec;
126 for (i = 0; i < spec->paths.used; i++) {
127 struct nid_path *path = snd_array_elem(&spec->paths, i);
128 if (path->depth <= 0)
130 if ((!from_nid || path->path[0] == from_nid) &&
131 (!to_nid || path->path[path->depth - 1] == to_nid)) {
132 if (with_aa_mix == HDA_PARSE_ALL ||
133 path->with_aa_mix == with_aa_mix)
140 /* get the path between the given NIDs;
141 * passing 0 to either @pin or @dac behaves as a wildcard
143 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
144 hda_nid_t from_nid, hda_nid_t to_nid)
146 return get_nid_path(codec, from_nid, to_nid, HDA_PARSE_ALL);
148 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
150 /* check whether the given DAC is already found in any existing paths */
151 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
153 struct hda_gen_spec *spec = codec->spec;
156 for (i = 0; i < spec->paths.used; i++) {
157 struct nid_path *path = snd_array_elem(&spec->paths, i);
158 if (path->path[0] == nid)
164 /* check whether the given two widgets can be connected */
165 static bool is_reachable_path(struct hda_codec *codec,
166 hda_nid_t from_nid, hda_nid_t to_nid)
168 if (!from_nid || !to_nid)
170 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
173 /* nid, dir and idx */
174 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
176 /* check whether the given ctl is already assigned in any path elements */
177 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
179 struct hda_gen_spec *spec = codec->spec;
182 val &= AMP_VAL_COMPARE_MASK;
183 for (i = 0; i < spec->paths.used; i++) {
184 struct nid_path *path = snd_array_elem(&spec->paths, i);
185 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
191 /* check whether a control with the given (nid, dir, idx) was assigned */
192 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
195 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
196 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
197 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
200 static void print_nid_path(const char *pfx, struct nid_path *path)
207 for (i = 0; i < path->depth; i++) {
209 sprintf(tmp, ":%02x", path->path[i]);
210 strlcat(buf, tmp, sizeof(buf));
212 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
215 /* called recursively */
216 static bool __parse_nid_path(struct hda_codec *codec,
217 hda_nid_t from_nid, hda_nid_t to_nid,
218 int with_aa_mix, struct nid_path *path, int depth)
220 struct hda_gen_spec *spec = codec->spec;
221 const hda_nid_t *conn;
224 if (to_nid == spec->mixer_nid) {
225 if (with_aa_mix == HDA_PARSE_NO_AAMIX)
227 with_aa_mix = HDA_PARSE_ALL; /* mark aa-mix is included */
230 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
231 for (i = 0; i < nums; i++) {
232 if (conn[i] != from_nid) {
233 /* special case: when from_nid is 0,
234 * try to find an empty DAC
237 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
238 is_dac_already_used(codec, conn[i]))
241 /* aa-mix is requested but not included? */
242 if (!(spec->mixer_nid && with_aa_mix == HDA_PARSE_ONLY_AAMIX))
245 if (depth >= MAX_NID_PATH_DEPTH)
247 for (i = 0; i < nums; i++) {
249 type = get_wcaps_type(get_wcaps(codec, conn[i]));
250 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
253 if (__parse_nid_path(codec, from_nid, conn[i],
254 with_aa_mix, path, depth + 1))
260 path->path[path->depth] = conn[i];
261 if (conn[i] == spec->mixer_nid)
262 path->with_aa_mix = true;
263 path->idx[path->depth + 1] = i;
264 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
265 path->multi[path->depth + 1] = 1;
270 /* parse the widget path from the given nid to the target nid;
271 * when @from_nid is 0, try to find an empty DAC;
272 * when @with_aa_mix is HDA_PARSE_NO_AAMIX, paths with spec->mixer_nid are
273 * excluded, only the paths that don't go through the mixer will be chosen.
274 * when @with_aa_mix is HDA_PARSE_ONLY_AAMIX, only the paths going through
275 * spec->mixer_nid will be chosen.
276 * when @with_aa_mix is HDA_PARSE_ALL, no special handling about mixer widget.
278 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
279 hda_nid_t to_nid, int with_aa_mix,
280 struct nid_path *path)
282 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
283 path->path[path->depth] = to_nid;
289 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
292 * parse the path between the given NIDs and add to the path list.
293 * if no valid path is found, return NULL
296 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
297 hda_nid_t to_nid, int with_aa_mix)
299 struct hda_gen_spec *spec = codec->spec;
300 struct nid_path *path;
302 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
305 /* check whether the path has been already added */
306 path = get_nid_path(codec, from_nid, to_nid, with_aa_mix);
310 path = snd_array_new(&spec->paths);
313 memset(path, 0, sizeof(*path));
314 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
320 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
322 /* look for an empty DAC slot */
323 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
326 struct hda_gen_spec *spec = codec->spec;
330 for (i = 0; i < spec->num_all_dacs; i++) {
331 hda_nid_t nid = spec->all_dacs[i];
332 if (!nid || is_dac_already_used(codec, nid))
334 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
335 if (is_digital != cap_digital)
337 if (is_reachable_path(codec, nid, pin))
343 /* replace the channels in the composed amp value with the given number */
344 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
346 val &= ~(0x3U << 16);
351 /* check whether the widget has the given amp capability for the direction */
352 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
353 int dir, unsigned int bits)
357 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
358 if (query_amp_caps(codec, nid, dir) & bits)
363 #define nid_has_mute(codec, nid, dir) \
364 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
365 #define nid_has_volume(codec, nid, dir) \
366 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
368 /* look for a widget suitable for assigning a mute switch in the path */
369 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
370 struct nid_path *path)
374 for (i = path->depth - 1; i >= 0; i--) {
375 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
376 return path->path[i];
377 if (i != path->depth - 1 && i != 0 &&
378 nid_has_mute(codec, path->path[i], HDA_INPUT))
379 return path->path[i];
384 /* look for a widget suitable for assigning a volume ctl in the path */
385 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
386 struct nid_path *path)
390 for (i = path->depth - 1; i >= 0; i--) {
391 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
392 return path->path[i];
398 * path activation / deactivation
401 /* can have the amp-in capability? */
402 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
404 hda_nid_t nid = path->path[idx];
405 unsigned int caps = get_wcaps(codec, nid);
406 unsigned int type = get_wcaps_type(caps);
408 if (!(caps & AC_WCAP_IN_AMP))
410 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
415 /* can have the amp-out capability? */
416 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
418 hda_nid_t nid = path->path[idx];
419 unsigned int caps = get_wcaps(codec, nid);
420 unsigned int type = get_wcaps_type(caps);
422 if (!(caps & AC_WCAP_OUT_AMP))
424 if (type == AC_WID_PIN && !idx) /* only for output pins */
429 /* check whether the given (nid,dir,idx) is active */
430 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
431 unsigned int idx, unsigned int dir)
433 struct hda_gen_spec *spec = codec->spec;
436 for (n = 0; n < spec->paths.used; n++) {
437 struct nid_path *path = snd_array_elem(&spec->paths, n);
440 for (i = 0; i < path->depth; i++) {
441 if (path->path[i] == nid) {
442 if (dir == HDA_OUTPUT || path->idx[i] == idx)
451 /* get the default amp value for the target state */
452 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
453 int dir, bool enable)
456 unsigned int val = 0;
458 caps = query_amp_caps(codec, nid, dir);
459 if (caps & AC_AMPCAP_NUM_STEPS) {
462 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
464 if (caps & AC_AMPCAP_MUTE) {
471 /* initialize the amp value (only at the first time) */
472 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
474 int val = get_amp_val_to_activate(codec, nid, dir, false);
475 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
478 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
479 int idx, bool enable)
482 if (is_ctl_associated(codec, nid, dir, idx) ||
483 (!enable && is_active_nid(codec, nid, dir, idx)))
485 val = get_amp_val_to_activate(codec, nid, dir, enable);
486 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
489 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
492 hda_nid_t nid = path->path[i];
493 init_amp(codec, nid, HDA_OUTPUT, 0);
494 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
497 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
498 int i, bool enable, bool add_aamix)
500 struct hda_gen_spec *spec = codec->spec;
501 const hda_nid_t *conn;
504 hda_nid_t nid = path->path[i];
506 nums = snd_hda_get_conn_list(codec, nid, &conn);
507 type = get_wcaps_type(get_wcaps(codec, nid));
508 if (type == AC_WID_PIN ||
509 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
515 for (n = 0; n < nums; n++)
516 init_amp(codec, nid, HDA_INPUT, n);
518 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
521 /* here is a little bit tricky in comparison with activate_amp_out();
522 * when aa-mixer is available, we need to enable the path as well
524 for (n = 0; n < nums; n++) {
525 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
527 activate_amp(codec, nid, HDA_INPUT, n, enable);
531 /* activate or deactivate the given path
532 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
534 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
535 bool enable, bool add_aamix)
540 path->active = false;
542 for (i = path->depth - 1; i >= 0; i--) {
543 if (enable && path->multi[i])
544 snd_hda_codec_write_cache(codec, path->path[i], 0,
545 AC_VERB_SET_CONNECT_SEL,
547 if (has_amp_in(codec, path, i))
548 activate_amp_in(codec, path, i, enable, add_aamix);
549 if (has_amp_out(codec, path, i))
550 activate_amp_out(codec, path, i, enable);
556 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
558 /* turn on/off EAPD on the given pin */
559 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
561 struct hda_gen_spec *spec = codec->spec;
562 if (spec->own_eapd_ctl ||
563 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
567 snd_hda_codec_update_cache(codec, pin, 0,
568 AC_VERB_SET_EAPD_BTLENABLE,
569 enable ? 0x02 : 0x00);
574 * Helper functions for creating mixer ctl elements
584 static const struct snd_kcontrol_new control_templates[] = {
585 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
586 HDA_CODEC_MUTE(NULL, 0, 0, 0),
587 HDA_BIND_MUTE(NULL, 0, 0, 0),
588 HDA_BIND_VOL(NULL, 0),
589 HDA_BIND_SW(NULL, 0),
592 /* add dynamic controls from template */
593 static int add_control(struct hda_gen_spec *spec, int type, const char *name,
594 int cidx, unsigned long val)
596 struct snd_kcontrol_new *knew;
598 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
602 if (get_amp_nid_(val))
603 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
604 knew->private_value = val;
608 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
609 const char *pfx, const char *dir,
610 const char *sfx, int cidx, unsigned long val)
613 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
614 return add_control(spec, type, name, cidx, val);
617 #define add_pb_vol_ctrl(spec, type, pfx, val) \
618 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
619 #define add_pb_sw_ctrl(spec, type, pfx, val) \
620 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
621 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
622 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
623 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
624 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
626 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
627 unsigned int chs, struct nid_path *path)
632 val = path->ctls[NID_PATH_VOL_CTL];
635 val = amp_val_replace_channels(val, chs);
636 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
639 /* return the channel bits suitable for the given path->ctls[] */
640 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
643 int chs = 1; /* mono (left only) */
645 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
646 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
647 chs = 3; /* stereo */
652 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
653 struct nid_path *path)
655 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
656 return add_vol_ctl(codec, pfx, cidx, chs, path);
659 /* create a mute-switch for the given mixer widget;
660 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
662 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
663 unsigned int chs, struct nid_path *path)
666 int type = HDA_CTL_WIDGET_MUTE;
670 val = path->ctls[NID_PATH_MUTE_CTL];
673 val = amp_val_replace_channels(val, chs);
674 if (get_amp_direction_(val) == HDA_INPUT) {
675 hda_nid_t nid = get_amp_nid_(val);
676 int nums = snd_hda_get_num_conns(codec, nid);
678 type = HDA_CTL_BIND_MUTE;
682 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
685 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
686 int cidx, struct nid_path *path)
688 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
689 return add_sw_ctl(codec, pfx, cidx, chs, path);
692 static const char * const channel_name[4] = {
693 "Front", "Surround", "CLFE", "Side"
696 /* give some appropriate ctl name prefix for the given line out channel */
697 static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
698 bool can_be_master, int *index)
700 struct auto_pin_cfg *cfg = &spec->autocfg;
703 if (cfg->line_outs == 1 && !spec->multi_ios &&
704 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
705 return spec->vmaster_mute.hook ? "PCM" : "Master";
707 /* if there is really a single DAC used in the whole output paths,
708 * use it master (or "PCM" if a vmaster hook is present)
710 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
711 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
712 return spec->vmaster_mute.hook ? "PCM" : "Master";
714 switch (cfg->line_out_type) {
715 case AUTO_PIN_SPEAKER_OUT:
716 if (cfg->line_outs == 1)
718 if (cfg->line_outs == 2)
719 return ch ? "Bass Speaker" : "Speaker";
721 case AUTO_PIN_HP_OUT:
722 /* for multi-io case, only the primary out */
723 if (ch && spec->multi_ios)
728 if (cfg->line_outs == 1 && !spec->multi_ios)
732 if (ch >= ARRAY_SIZE(channel_name)) {
737 return channel_name[ch];
744 /* badness definition */
746 /* No primary DAC is found for the main output */
747 BAD_NO_PRIMARY_DAC = 0x10000,
748 /* No DAC is found for the extra output */
750 /* No possible multi-ios */
751 BAD_MULTI_IO = 0x103,
752 /* No individual DAC for extra output */
753 BAD_NO_EXTRA_DAC = 0x102,
754 /* No individual DAC for extra surrounds */
755 BAD_NO_EXTRA_SURR_DAC = 0x101,
756 /* Primary DAC shared with main surrounds */
757 BAD_SHARED_SURROUND = 0x100,
758 /* Primary DAC shared with main CLFE */
759 BAD_SHARED_CLFE = 0x10,
760 /* Primary DAC shared with extra surrounds */
761 BAD_SHARED_EXTRA_SURROUND = 0x10,
762 /* Volume widget is shared */
763 BAD_SHARED_VOL = 0x10,
766 /* look for widgets in the path between the given NIDs appropriate for
767 * volume and mute controls, and assign the values to ctls[].
769 * When no appropriate widget is found in the path, the badness value
770 * is incremented depending on the situation. The function returns the
771 * total badness for both volume and mute controls.
773 static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
776 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
782 return BAD_SHARED_VOL * 2;
783 nid = look_for_out_vol_nid(codec, path);
785 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
786 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
787 badness += BAD_SHARED_VOL;
789 path->ctls[NID_PATH_VOL_CTL] = val;
791 badness += BAD_SHARED_VOL;
792 nid = look_for_out_mute_nid(codec, path);
794 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
795 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
796 nid_has_mute(codec, nid, HDA_OUTPUT))
797 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
799 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
800 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
801 badness += BAD_SHARED_VOL;
803 path->ctls[NID_PATH_MUTE_CTL] = val;
805 badness += BAD_SHARED_VOL;
809 struct badness_table {
810 int no_primary_dac; /* no primary DAC */
811 int no_dac; /* no secondary DACs */
812 int shared_primary; /* primary DAC is shared with main output */
813 int shared_surr; /* secondary DAC shared with main or primary */
814 int shared_clfe; /* third DAC shared with main or primary */
815 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
818 static struct badness_table main_out_badness = {
819 .no_primary_dac = BAD_NO_PRIMARY_DAC,
820 .no_dac = BAD_NO_DAC,
821 .shared_primary = BAD_NO_PRIMARY_DAC,
822 .shared_surr = BAD_SHARED_SURROUND,
823 .shared_clfe = BAD_SHARED_CLFE,
824 .shared_surr_main = BAD_SHARED_SURROUND,
827 static struct badness_table extra_out_badness = {
828 .no_primary_dac = BAD_NO_DAC,
829 .no_dac = BAD_NO_DAC,
830 .shared_primary = BAD_NO_EXTRA_DAC,
831 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
832 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
833 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
836 /* try to assign DACs to pins and return the resultant badness */
837 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
838 const hda_nid_t *pins, hda_nid_t *dacs,
839 const struct badness_table *bad)
841 struct hda_gen_spec *spec = codec->spec;
842 struct auto_pin_cfg *cfg = &spec->autocfg;
850 for (i = 0; i < num_outs; i++) {
851 struct nid_path *path;
852 hda_nid_t pin = pins[i];
855 badness += assign_out_path_ctls(codec, pin, dacs[i]);
859 dacs[i] = look_for_dac(codec, pin, false);
860 if (!dacs[i] && !i) {
861 for (j = 1; j < num_outs; j++) {
862 if (is_reachable_path(codec, dacs[j], pin)) {
871 if (is_reachable_path(codec, dacs[0], pin))
873 else if (cfg->line_outs > i &&
874 is_reachable_path(codec, spec->private_dac_nids[i], pin))
875 dac = spec->private_dac_nids[i];
878 badness += bad->shared_primary;
880 badness += bad->shared_surr;
882 badness += bad->shared_clfe;
883 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
884 dac = spec->private_dac_nids[0];
885 badness += bad->shared_surr_main;
887 badness += bad->no_primary_dac;
889 badness += bad->no_dac;
891 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_NO_AAMIX);
892 if (!path && i > 0 && spec->mixer_nid) {
894 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_ALL);
899 print_nid_path("output", path);
903 badness += assign_out_path_ctls(codec, pin, dac);
909 /* return NID if the given pin has only a single connection to a certain DAC */
910 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
912 struct hda_gen_spec *spec = codec->spec;
914 hda_nid_t nid_found = 0;
916 for (i = 0; i < spec->num_all_dacs; i++) {
917 hda_nid_t nid = spec->all_dacs[i];
918 if (!nid || is_dac_already_used(codec, nid))
920 if (is_reachable_path(codec, nid, pin)) {
929 /* check whether the given pin can be a multi-io pin */
930 static bool can_be_multiio_pin(struct hda_codec *codec,
931 unsigned int location, hda_nid_t nid)
933 unsigned int defcfg, caps;
935 defcfg = snd_hda_codec_get_pincfg(codec, nid);
936 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
938 if (location && get_defcfg_location(defcfg) != location)
940 caps = snd_hda_query_pin_caps(codec, nid);
941 if (!(caps & AC_PINCAP_OUT))
946 /* count the number of input pins that are capable to be multi-io */
947 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
949 struct hda_gen_spec *spec = codec->spec;
950 struct auto_pin_cfg *cfg = &spec->autocfg;
951 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
952 unsigned int location = get_defcfg_location(defcfg);
956 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
957 for (i = 0; i < cfg->num_inputs; i++) {
958 if (cfg->inputs[i].type != type)
960 if (can_be_multiio_pin(codec, location,
971 * When hardwired is set, try to fill ony hardwired pins, and returns
972 * zero if any pins are filled, non-zero if nothing found.
973 * When hardwired is off, try to fill possible input pins, and returns
976 static int fill_multi_ios(struct hda_codec *codec,
977 hda_nid_t reference_pin,
980 struct hda_gen_spec *spec = codec->spec;
981 struct auto_pin_cfg *cfg = &spec->autocfg;
982 int type, i, j, num_pins, old_pins;
983 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
984 unsigned int location = get_defcfg_location(defcfg);
987 old_pins = spec->multi_ios;
991 num_pins = count_multiio_pins(codec, reference_pin);
995 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
996 for (i = 0; i < cfg->num_inputs; i++) {
997 struct nid_path *path;
998 hda_nid_t nid = cfg->inputs[i].pin;
1001 if (cfg->inputs[i].type != type)
1003 if (!can_be_multiio_pin(codec, location, nid))
1005 for (j = 0; j < spec->multi_ios; j++) {
1006 if (nid == spec->multi_io[j].pin)
1009 if (j < spec->multi_ios)
1013 dac = get_dac_if_single(codec, nid);
1015 dac = look_for_dac(codec, nid, false);
1020 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
1025 print_nid_path("multiio", path);
1026 spec->multi_io[spec->multi_ios].pin = nid;
1027 spec->multi_io[spec->multi_ios].dac = dac;
1029 if (spec->multi_ios >= 2)
1035 badness = BAD_MULTI_IO;
1036 if (old_pins == spec->multi_ios) {
1038 return 1; /* nothing found */
1040 return badness; /* no badness if nothing found */
1042 if (!hardwired && spec->multi_ios < 2) {
1043 /* cancel newly assigned paths */
1044 spec->paths.used -= spec->multi_ios - old_pins;
1045 spec->multi_ios = old_pins;
1049 /* assign volume and mute controls */
1050 for (i = old_pins; i < spec->multi_ios; i++)
1051 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1052 spec->multi_io[i].dac);
1057 /* map DACs for all pins in the list if they are single connections */
1058 static bool map_singles(struct hda_codec *codec, int outs,
1059 const hda_nid_t *pins, hda_nid_t *dacs)
1061 struct hda_gen_spec *spec = codec->spec;
1064 for (i = 0; i < outs; i++) {
1065 struct nid_path *path;
1069 dac = get_dac_if_single(codec, pins[i]);
1072 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
1073 if (!path && i > 0 && spec->mixer_nid)
1074 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
1078 print_nid_path("output", path);
1079 path->active = true;
1085 /* fill in the dac_nids table from the parsed pin configuration */
1086 static int fill_and_eval_dacs(struct hda_codec *codec,
1087 bool fill_hardwired,
1088 bool fill_mio_first)
1090 struct hda_gen_spec *spec = codec->spec;
1091 struct auto_pin_cfg *cfg = &spec->autocfg;
1092 int i, err, badness;
1094 /* set num_dacs once to full for look_for_dac() */
1095 spec->multiout.num_dacs = cfg->line_outs;
1096 spec->multiout.dac_nids = spec->private_dac_nids;
1097 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1098 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1099 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1100 spec->multi_ios = 0;
1101 snd_array_free(&spec->paths);
1104 /* fill hard-wired DACs first */
1105 if (fill_hardwired) {
1108 mapped = map_singles(codec, cfg->line_outs,
1110 spec->private_dac_nids);
1111 mapped |= map_singles(codec, cfg->hp_outs,
1113 spec->multiout.hp_out_nid);
1114 mapped |= map_singles(codec, cfg->speaker_outs,
1116 spec->multiout.extra_out_nid);
1117 if (fill_mio_first && cfg->line_outs == 1 &&
1118 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1119 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1126 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1127 spec->private_dac_nids,
1130 /* re-count num_dacs and squash invalid entries */
1131 spec->multiout.num_dacs = 0;
1132 for (i = 0; i < cfg->line_outs; i++) {
1133 if (spec->private_dac_nids[i])
1134 spec->multiout.num_dacs++;
1136 memmove(spec->private_dac_nids + i,
1137 spec->private_dac_nids + i + 1,
1138 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1139 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1143 if (fill_mio_first &&
1144 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1145 /* try to fill multi-io first */
1146 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1149 /* we don't count badness at this stage yet */
1152 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1153 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1154 spec->multiout.hp_out_nid,
1155 &extra_out_badness);
1160 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1161 err = try_assign_dacs(codec, cfg->speaker_outs,
1163 spec->multiout.extra_out_nid,
1164 &extra_out_badness);
1169 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1170 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1176 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1177 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1178 spec->multi_ios = 1; /* give badness */
1180 if (spec->multi_ios == 2) {
1181 for (i = 0; i < 2; i++)
1182 spec->private_dac_nids[spec->multiout.num_dacs++] =
1183 spec->multi_io[i].dac;
1184 spec->ext_channel_count = 2;
1185 } else if (spec->multi_ios) {
1186 spec->multi_ios = 0;
1187 badness += BAD_MULTI_IO;
1193 #define DEBUG_BADNESS
1195 #ifdef DEBUG_BADNESS
1196 #define debug_badness snd_printdd
1198 #define debug_badness(...)
1201 static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1203 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1204 cfg->line_out_pins[0], cfg->line_out_pins[1],
1205 cfg->line_out_pins[2], cfg->line_out_pins[3],
1206 spec->multiout.dac_nids[0],
1207 spec->multiout.dac_nids[1],
1208 spec->multiout.dac_nids[2],
1209 spec->multiout.dac_nids[3]);
1210 if (spec->multi_ios > 0)
1211 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1213 spec->multi_io[0].pin, spec->multi_io[1].pin,
1214 spec->multi_io[0].dac, spec->multi_io[1].dac);
1215 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1216 cfg->hp_pins[0], cfg->hp_pins[1],
1217 cfg->hp_pins[2], cfg->hp_pins[3],
1218 spec->multiout.hp_out_nid[0],
1219 spec->multiout.hp_out_nid[1],
1220 spec->multiout.hp_out_nid[2],
1221 spec->multiout.hp_out_nid[3]);
1222 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1223 cfg->speaker_pins[0], cfg->speaker_pins[1],
1224 cfg->speaker_pins[2], cfg->speaker_pins[3],
1225 spec->multiout.extra_out_nid[0],
1226 spec->multiout.extra_out_nid[1],
1227 spec->multiout.extra_out_nid[2],
1228 spec->multiout.extra_out_nid[3]);
1231 /* find all available DACs of the codec */
1232 static void fill_all_dac_nids(struct hda_codec *codec)
1234 struct hda_gen_spec *spec = codec->spec;
1236 hda_nid_t nid = codec->start_nid;
1238 spec->num_all_dacs = 0;
1239 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1240 for (i = 0; i < codec->num_nodes; i++, nid++) {
1241 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1243 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1244 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1247 spec->all_dacs[spec->num_all_dacs++] = nid;
1251 static int parse_output_paths(struct hda_codec *codec)
1253 struct hda_gen_spec *spec = codec->spec;
1254 struct auto_pin_cfg *cfg = &spec->autocfg;
1255 struct auto_pin_cfg *best_cfg;
1256 int best_badness = INT_MAX;
1258 bool fill_hardwired = true, fill_mio_first = true;
1259 bool best_wired = true, best_mio = true;
1260 bool hp_spk_swapped = false;
1262 fill_all_dac_nids(codec);
1264 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1270 badness = fill_and_eval_dacs(codec, fill_hardwired,
1276 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1277 cfg->line_out_type, fill_hardwired, fill_mio_first,
1279 debug_show_configs(spec, cfg);
1280 if (badness < best_badness) {
1281 best_badness = badness;
1283 best_wired = fill_hardwired;
1284 best_mio = fill_mio_first;
1288 fill_mio_first = !fill_mio_first;
1289 if (!fill_mio_first)
1291 fill_hardwired = !fill_hardwired;
1292 if (!fill_hardwired)
1296 hp_spk_swapped = true;
1297 if (cfg->speaker_outs > 0 &&
1298 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1299 cfg->hp_outs = cfg->line_outs;
1300 memcpy(cfg->hp_pins, cfg->line_out_pins,
1301 sizeof(cfg->hp_pins));
1302 cfg->line_outs = cfg->speaker_outs;
1303 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1304 sizeof(cfg->speaker_pins));
1305 cfg->speaker_outs = 0;
1306 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1307 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1308 fill_hardwired = true;
1311 if (cfg->hp_outs > 0 &&
1312 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1313 cfg->speaker_outs = cfg->line_outs;
1314 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1315 sizeof(cfg->speaker_pins));
1316 cfg->line_outs = cfg->hp_outs;
1317 memcpy(cfg->line_out_pins, cfg->hp_pins,
1318 sizeof(cfg->hp_pins));
1320 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1321 cfg->line_out_type = AUTO_PIN_HP_OUT;
1322 fill_hardwired = true;
1329 debug_badness("==> restoring best_cfg\n");
1331 fill_and_eval_dacs(codec, best_wired, best_mio);
1333 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1334 cfg->line_out_type, best_wired, best_mio);
1335 debug_show_configs(spec, cfg);
1337 if (cfg->line_out_pins[0]) {
1338 struct nid_path *path;
1339 path = snd_hda_get_nid_path(codec,
1340 spec->multiout.dac_nids[0],
1341 cfg->line_out_pins[0]);
1343 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1350 /* add playback controls from the parsed DAC table */
1351 static int create_multi_out_ctls(struct hda_codec *codec,
1352 const struct auto_pin_cfg *cfg)
1354 struct hda_gen_spec *spec = codec->spec;
1355 int i, err, noutputs;
1357 noutputs = cfg->line_outs;
1358 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1359 noutputs += spec->multi_ios;
1361 for (i = 0; i < noutputs; i++) {
1365 struct nid_path *path;
1367 dac = spec->multiout.dac_nids[i];
1370 if (i >= cfg->line_outs) {
1371 pin = spec->multi_io[i - cfg->line_outs].pin;
1373 name = channel_name[i];
1375 pin = cfg->line_out_pins[i];
1376 name = get_line_out_pfx(spec, i, true, &index);
1379 path = snd_hda_get_nid_path(codec, dac, pin);
1382 if (!name || !strcmp(name, "CLFE")) {
1384 err = add_vol_ctl(codec, "Center", 0, 1, path);
1387 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1390 err = add_sw_ctl(codec, "Center", 0, 1, path);
1393 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1397 err = add_stereo_vol(codec, name, index, path);
1400 err = add_stereo_sw(codec, name, index, path);
1408 static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1409 hda_nid_t dac, const char *pfx, int cidx)
1411 struct nid_path *path;
1414 path = snd_hda_get_nid_path(codec, dac, pin);
1417 /* bind volume control will be created in the case of dac = 0 */
1419 err = add_stereo_vol(codec, pfx, cidx, path);
1423 err = add_stereo_sw(codec, pfx, cidx, path);
1429 /* add playback controls for speaker and HP outputs */
1430 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1431 const hda_nid_t *pins, const hda_nid_t *dacs,
1434 struct hda_gen_spec *spec = codec->spec;
1435 struct hda_bind_ctls *ctl;
1439 if (!num_pins || !pins[0])
1442 if (num_pins == 1) {
1443 hda_nid_t dac = *dacs;
1445 dac = spec->multiout.dac_nids[0];
1446 return create_extra_out(codec, *pins, dac, pfx, 0);
1449 for (i = 0; i < num_pins; i++) {
1451 if (dacs[num_pins - 1])
1452 dac = dacs[i]; /* with individual volumes */
1455 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1456 err = create_extra_out(codec, pins[i], dac,
1458 } else if (num_pins >= 3) {
1459 snprintf(name, sizeof(name), "%s %s",
1460 pfx, channel_name[i]);
1461 err = create_extra_out(codec, pins[i], dac, name, 0);
1463 err = create_extra_out(codec, pins[i], dac, pfx, i);
1468 if (dacs[num_pins - 1])
1471 /* Let's create a bind-controls for volumes */
1472 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1476 for (i = 0; i < num_pins; i++) {
1478 struct nid_path *path;
1479 if (!pins[i] || !dacs[i])
1481 path = snd_hda_get_nid_path(codec, dacs[i], pins[i]);
1484 vol = look_for_out_vol_nid(codec, path);
1487 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1490 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1491 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1498 static int create_hp_out_ctls(struct hda_codec *codec)
1500 struct hda_gen_spec *spec = codec->spec;
1501 return create_extra_outs(codec, spec->autocfg.hp_outs,
1502 spec->autocfg.hp_pins,
1503 spec->multiout.hp_out_nid,
1507 static int create_speaker_out_ctls(struct hda_codec *codec)
1509 struct hda_gen_spec *spec = codec->spec;
1510 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1511 spec->autocfg.speaker_pins,
1512 spec->multiout.extra_out_nid,
1517 * independent HP controls
1520 static int indep_hp_info(struct snd_kcontrol *kcontrol,
1521 struct snd_ctl_elem_info *uinfo)
1523 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1526 static int indep_hp_get(struct snd_kcontrol *kcontrol,
1527 struct snd_ctl_elem_value *ucontrol)
1529 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1530 struct hda_gen_spec *spec = codec->spec;
1531 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1535 static int indep_hp_put(struct snd_kcontrol *kcontrol,
1536 struct snd_ctl_elem_value *ucontrol)
1538 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1539 struct hda_gen_spec *spec = codec->spec;
1540 unsigned int select = ucontrol->value.enumerated.item[0];
1543 mutex_lock(&spec->pcm_mutex);
1544 if (spec->active_streams) {
1549 if (spec->indep_hp_enabled != select) {
1550 spec->indep_hp_enabled = select;
1551 if (spec->indep_hp_enabled)
1552 spec->multiout.hp_out_nid[0] = 0;
1554 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1558 mutex_unlock(&spec->pcm_mutex);
1562 static const struct snd_kcontrol_new indep_hp_ctl = {
1563 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1564 .name = "Independent HP",
1565 .info = indep_hp_info,
1566 .get = indep_hp_get,
1567 .put = indep_hp_put,
1571 static int create_indep_hp_ctls(struct hda_codec *codec)
1573 struct hda_gen_spec *spec = codec->spec;
1575 if (!spec->indep_hp)
1577 if (!spec->multiout.hp_out_nid[0]) {
1582 spec->indep_hp_enabled = false;
1583 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1584 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1590 * channel mode enum control
1593 static int ch_mode_info(struct snd_kcontrol *kcontrol,
1594 struct snd_ctl_elem_info *uinfo)
1596 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1597 struct hda_gen_spec *spec = codec->spec;
1599 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1601 uinfo->value.enumerated.items = spec->multi_ios + 1;
1602 if (uinfo->value.enumerated.item > spec->multi_ios)
1603 uinfo->value.enumerated.item = spec->multi_ios;
1604 sprintf(uinfo->value.enumerated.name, "%dch",
1605 (uinfo->value.enumerated.item + 1) * 2);
1609 static int ch_mode_get(struct snd_kcontrol *kcontrol,
1610 struct snd_ctl_elem_value *ucontrol)
1612 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1613 struct hda_gen_spec *spec = codec->spec;
1614 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1618 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1620 struct hda_gen_spec *spec = codec->spec;
1621 hda_nid_t nid = spec->multi_io[idx].pin;
1622 struct nid_path *path;
1624 path = snd_hda_get_nid_path(codec, spec->multi_io[idx].dac, nid);
1628 if (path->active == output)
1632 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1633 snd_hda_activate_path(codec, path, true, true);
1634 set_pin_eapd(codec, nid, true);
1636 set_pin_eapd(codec, nid, false);
1637 snd_hda_activate_path(codec, path, false, true);
1638 snd_hda_set_pin_ctl_cache(codec, nid,
1639 spec->multi_io[idx].ctl_in);
1644 static int ch_mode_put(struct snd_kcontrol *kcontrol,
1645 struct snd_ctl_elem_value *ucontrol)
1647 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1648 struct hda_gen_spec *spec = codec->spec;
1651 ch = ucontrol->value.enumerated.item[0];
1652 if (ch < 0 || ch > spec->multi_ios)
1654 if (ch == (spec->ext_channel_count - 1) / 2)
1656 spec->ext_channel_count = (ch + 1) * 2;
1657 for (i = 0; i < spec->multi_ios; i++)
1658 set_multi_io(codec, i, i < ch);
1659 spec->multiout.max_channels = max(spec->ext_channel_count,
1660 spec->const_channel_count);
1661 if (spec->need_dac_fix)
1662 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1666 static const struct snd_kcontrol_new channel_mode_enum = {
1667 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1668 .name = "Channel Mode",
1669 .info = ch_mode_info,
1674 static int create_multi_channel_mode(struct hda_codec *codec)
1676 struct hda_gen_spec *spec = codec->spec;
1678 if (spec->multi_ios > 0) {
1679 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
1686 * shared headphone/mic handling
1689 static void call_update_outputs(struct hda_codec *codec);
1691 /* for shared I/O, change the pin-control accordingly */
1692 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1694 struct hda_gen_spec *spec = codec->spec;
1696 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1697 /* NOTE: this assumes that there are only two inputs, the
1698 * first is the real internal mic and the second is HP/mic jack.
1701 val = snd_hda_get_default_vref(codec, pin);
1703 /* This pin does not have vref caps - let's enable vref on pin 0x18
1704 instead, as suggested by Realtek */
1705 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1706 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1707 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1708 if (vref_val != AC_PINCTL_VREF_HIZ)
1709 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1710 PIN_IN | (set_as_mic ? vref_val : 0));
1713 val = set_as_mic ? val | PIN_IN : PIN_HP;
1714 snd_hda_set_pin_ctl_cache(codec, pin, val);
1716 spec->automute_speaker = !set_as_mic;
1717 call_update_outputs(codec);
1720 /* create a shared input with the headphone out */
1721 static int create_shared_input(struct hda_codec *codec)
1723 struct hda_gen_spec *spec = codec->spec;
1724 struct auto_pin_cfg *cfg = &spec->autocfg;
1725 unsigned int defcfg;
1728 /* only one internal input pin? */
1729 if (cfg->num_inputs != 1)
1731 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1732 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1735 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1736 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1737 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1738 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1740 return 0; /* both not available */
1742 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1743 return 0; /* no input */
1745 cfg->inputs[1].pin = nid;
1746 cfg->inputs[1].type = AUTO_PIN_MIC;
1747 cfg->num_inputs = 2;
1748 spec->shared_mic_hp = 1;
1749 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1759 /* add the powersave loopback-list entry */
1760 static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1762 struct hda_amp_list *list;
1764 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1766 list = spec->loopback_list + spec->num_loopbacks;
1768 list->dir = HDA_INPUT;
1770 spec->num_loopbacks++;
1771 spec->loopback.amplist = spec->loopback_list;
1774 #define add_loopback_list(spec, mix, idx) /* NOP */
1777 /* create input playback/capture controls for the given pin */
1778 static int new_analog_input(struct hda_codec *codec, hda_nid_t pin,
1779 const char *ctlname, int ctlidx,
1782 struct hda_gen_spec *spec = codec->spec;
1783 struct nid_path *path;
1787 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1788 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1789 return 0; /* no need for analog loopback */
1791 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
1794 print_nid_path("loopback", path);
1796 idx = path->idx[path->depth - 1];
1797 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1798 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1799 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
1802 path->ctls[NID_PATH_VOL_CTL] = val;
1805 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1806 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1807 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
1810 path->ctls[NID_PATH_MUTE_CTL] = val;
1813 path->active = true;
1814 add_loopback_list(spec, mix_nid, idx);
1818 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
1820 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1821 return (pincap & AC_PINCAP_IN) != 0;
1824 /* Parse the codec tree and retrieve ADCs */
1825 static int fill_adc_nids(struct hda_codec *codec)
1827 struct hda_gen_spec *spec = codec->spec;
1829 hda_nid_t *adc_nids = spec->adc_nids;
1830 int max_nums = ARRAY_SIZE(spec->adc_nids);
1833 nid = codec->start_nid;
1834 for (i = 0; i < codec->num_nodes; i++, nid++) {
1835 unsigned int caps = get_wcaps(codec, nid);
1836 int type = get_wcaps_type(caps);
1838 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1840 adc_nids[nums] = nid;
1841 if (++nums >= max_nums)
1844 spec->num_adc_nids = nums;
1848 /* filter out invalid adc_nids that don't give all active input pins;
1849 * if needed, check whether dynamic ADC-switching is available
1851 static int check_dyn_adc_switch(struct hda_codec *codec)
1853 struct hda_gen_spec *spec = codec->spec;
1854 struct hda_input_mux *imux = &spec->input_mux;
1855 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1861 for (n = 0; n < spec->num_adc_nids; n++) {
1862 adc = spec->adc_nids[n];
1863 for (i = 0; i < imux->num_items; i++) {
1864 pin = spec->imux_pins[i];
1865 if (!is_reachable_path(codec, pin, adc))
1868 if (i >= imux->num_items)
1869 adc_nids[nums++] = adc;
1873 if (spec->shared_mic_hp) {
1874 spec->shared_mic_hp = 0;
1875 imux->num_items = 1;
1879 /* check whether ADC-switch is possible */
1880 for (i = 0; i < imux->num_items; i++) {
1881 pin = spec->imux_pins[i];
1882 for (n = 0; n < spec->num_adc_nids; n++) {
1883 adc = spec->adc_nids[n];
1884 if (is_reachable_path(codec, pin, adc)) {
1885 spec->dyn_adc_idx[i] = n;
1891 snd_printdd("hda-codec: enabling ADC switching\n");
1892 spec->dyn_adc_switch = 1;
1893 } else if (nums != spec->num_adc_nids) {
1894 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1895 spec->num_adc_nids = nums;
1898 if (imux->num_items == 1 || spec->shared_mic_hp) {
1899 snd_printdd("hda-codec: reducing to a single ADC\n");
1900 spec->num_adc_nids = 1; /* reduce to a single ADC */
1903 /* single index for individual volumes ctls */
1904 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1905 spec->num_adc_nids = 1;
1911 * create playback/capture controls for input pins
1913 static int create_input_ctls(struct hda_codec *codec)
1915 struct hda_gen_spec *spec = codec->spec;
1916 const struct auto_pin_cfg *cfg = &spec->autocfg;
1917 hda_nid_t mixer = spec->mixer_nid;
1918 struct hda_input_mux *imux = &spec->input_mux;
1920 int i, c, err, type_idx = 0;
1921 const char *prev_label = NULL;
1923 num_adcs = fill_adc_nids(codec);
1927 for (i = 0; i < cfg->num_inputs; i++) {
1932 pin = cfg->inputs[i].pin;
1933 if (!is_input_pin(codec, pin))
1936 label = hda_get_autocfg_input_label(codec, cfg, i);
1937 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
1938 label = "Headphone Mic";
1939 if (prev_label && !strcmp(label, prev_label))
1946 if (is_reachable_path(codec, pin, mixer)) {
1947 err = new_analog_input(codec, pin,
1948 label, type_idx, mixer);
1955 for (c = 0; c < num_adcs; c++) {
1956 struct nid_path *path;
1957 hda_nid_t adc = spec->adc_nids[c];
1959 if (!is_reachable_path(codec, pin, adc))
1961 path = snd_array_new(&spec->paths);
1964 memset(path, 0, sizeof(*path));
1965 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
1967 "invalid input path 0x%x -> 0x%x\n",
1972 print_nid_path("input", path);
1975 spec->imux_pins[imux->num_items] = pin;
1976 snd_hda_add_imux_item(imux, label,
1977 imux->num_items, NULL);
1991 /* get the ADC NID corresponding to the given index */
1992 static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
1994 struct hda_gen_spec *spec = codec->spec;
1995 if (spec->dyn_adc_switch)
1996 adc_idx = spec->dyn_adc_idx[imux_idx];
1997 return spec->adc_nids[adc_idx];
2000 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2003 static int mux_enum_info(struct snd_kcontrol *kcontrol,
2004 struct snd_ctl_elem_info *uinfo)
2006 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2007 struct hda_gen_spec *spec = codec->spec;
2008 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2011 static int mux_enum_get(struct snd_kcontrol *kcontrol,
2012 struct snd_ctl_elem_value *ucontrol)
2014 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2015 struct hda_gen_spec *spec = codec->spec;
2016 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2018 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2022 static int mux_enum_put(struct snd_kcontrol *kcontrol,
2023 struct snd_ctl_elem_value *ucontrol)
2025 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2026 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2027 return mux_select(codec, adc_idx,
2028 ucontrol->value.enumerated.item[0]);
2031 static const struct snd_kcontrol_new cap_src_temp = {
2032 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2033 .name = "Input Source",
2034 .info = mux_enum_info,
2035 .get = mux_enum_get,
2036 .put = mux_enum_put,
2040 * capture volume and capture switch ctls
2043 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2044 struct snd_ctl_elem_value *ucontrol);
2046 /* call the given amp update function for all amps in the imux list at once */
2047 static int cap_put_caller(struct snd_kcontrol *kcontrol,
2048 struct snd_ctl_elem_value *ucontrol,
2049 put_call_t func, int type)
2051 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2052 struct hda_gen_spec *spec = codec->spec;
2053 const struct hda_input_mux *imux;
2054 struct nid_path *path;
2055 int i, adc_idx, err = 0;
2057 imux = &spec->input_mux;
2058 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2059 mutex_lock(&codec->control_mutex);
2060 /* we use the cache-only update at first since multiple input paths
2061 * may shared the same amp; by updating only caches, the redundant
2062 * writes to hardware can be reduced.
2064 codec->cached_write = 1;
2065 for (i = 0; i < imux->num_items; i++) {
2066 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2067 get_adc_nid(codec, adc_idx, i));
2068 if (!path->ctls[type])
2070 kcontrol->private_value = path->ctls[type];
2071 err = func(kcontrol, ucontrol);
2076 codec->cached_write = 0;
2077 mutex_unlock(&codec->control_mutex);
2078 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
2079 if (err >= 0 && spec->cap_sync_hook)
2080 spec->cap_sync_hook(codec);
2084 /* capture volume ctl callbacks */
2085 #define cap_vol_info snd_hda_mixer_amp_volume_info
2086 #define cap_vol_get snd_hda_mixer_amp_volume_get
2087 #define cap_vol_tlv snd_hda_mixer_amp_tlv
2089 static int cap_vol_put(struct snd_kcontrol *kcontrol,
2090 struct snd_ctl_elem_value *ucontrol)
2092 return cap_put_caller(kcontrol, ucontrol,
2093 snd_hda_mixer_amp_volume_put,
2097 static const struct snd_kcontrol_new cap_vol_temp = {
2098 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2099 .name = "Capture Volume",
2100 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2101 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2102 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2103 .info = cap_vol_info,
2106 .tlv = { .c = cap_vol_tlv },
2109 /* capture switch ctl callbacks */
2110 #define cap_sw_info snd_ctl_boolean_stereo_info
2111 #define cap_sw_get snd_hda_mixer_amp_switch_get
2113 static int cap_sw_put(struct snd_kcontrol *kcontrol,
2114 struct snd_ctl_elem_value *ucontrol)
2116 return cap_put_caller(kcontrol, ucontrol,
2117 snd_hda_mixer_amp_switch_put,
2121 static const struct snd_kcontrol_new cap_sw_temp = {
2122 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2123 .name = "Capture Switch",
2124 .info = cap_sw_info,
2129 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2134 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2135 for (depth = 0; depth < 3; depth++) {
2136 if (depth >= path->depth)
2138 i = path->depth - depth - 1;
2139 nid = path->path[i];
2140 if (!path->ctls[NID_PATH_VOL_CTL]) {
2141 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2142 path->ctls[NID_PATH_VOL_CTL] =
2143 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2144 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2145 int idx = path->idx[i];
2146 if (!depth && codec->single_adc_amp)
2148 path->ctls[NID_PATH_VOL_CTL] =
2149 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2152 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2153 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2154 path->ctls[NID_PATH_MUTE_CTL] =
2155 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2156 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2157 int idx = path->idx[i];
2158 if (!depth && codec->single_adc_amp)
2160 path->ctls[NID_PATH_MUTE_CTL] =
2161 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2168 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2170 struct hda_gen_spec *spec = codec->spec;
2171 struct auto_pin_cfg *cfg = &spec->autocfg;
2175 if (!spec->inv_dmic_split)
2177 for (i = 0; i < cfg->num_inputs; i++) {
2178 if (cfg->inputs[i].pin != nid)
2180 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2182 val = snd_hda_codec_get_pincfg(codec, nid);
2183 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2188 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2189 int idx, bool is_switch, unsigned int ctl,
2192 struct hda_gen_spec *spec = codec->spec;
2194 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2195 const char *sfx = is_switch ? "Switch" : "Volume";
2196 unsigned int chs = inv_dmic ? 1 : 3;
2203 snprintf(tmpname, sizeof(tmpname),
2204 "%s Capture %s", label, sfx);
2206 snprintf(tmpname, sizeof(tmpname),
2208 err = add_control(spec, type, tmpname, idx,
2209 amp_val_replace_channels(ctl, chs));
2210 if (err < 0 || !inv_dmic)
2213 /* Make independent right kcontrol */
2215 snprintf(tmpname, sizeof(tmpname),
2216 "Inverted %s Capture %s", label, sfx);
2218 snprintf(tmpname, sizeof(tmpname),
2219 "Inverted Capture %s", sfx);
2220 return add_control(spec, type, tmpname, idx,
2221 amp_val_replace_channels(ctl, 2));
2224 /* create single (and simple) capture volume and switch controls */
2225 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2226 unsigned int vol_ctl, unsigned int sw_ctl,
2230 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2233 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2239 /* create bound capture volume and switch controls */
2240 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2241 unsigned int vol_ctl, unsigned int sw_ctl)
2243 struct hda_gen_spec *spec = codec->spec;
2244 struct snd_kcontrol_new *knew;
2247 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
2251 knew->private_value = vol_ctl;
2252 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2255 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
2259 knew->private_value = sw_ctl;
2260 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2265 /* return the vol ctl when used first in the imux list */
2266 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2268 struct hda_gen_spec *spec = codec->spec;
2269 struct nid_path *path;
2273 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2274 get_adc_nid(codec, 0, idx));
2277 ctl = path->ctls[type];
2280 for (i = 0; i < idx - 1; i++) {
2281 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2282 get_adc_nid(codec, 0, i));
2283 if (path && path->ctls[type] == ctl)
2289 /* create individual capture volume and switch controls per input */
2290 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2292 struct hda_gen_spec *spec = codec->spec;
2293 struct hda_input_mux *imux = &spec->input_mux;
2294 int i, err, type, type_idx = 0;
2295 const char *prev_label = NULL;
2297 for (i = 0; i < imux->num_items; i++) {
2300 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2301 if (prev_label && !strcmp(label, prev_label))
2306 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2308 for (type = 0; type < 2; type++) {
2309 err = add_single_cap_ctl(codec, label, type_idx, type,
2310 get_first_cap_ctl(codec, i, type),
2319 static int create_capture_mixers(struct hda_codec *codec)
2321 struct hda_gen_spec *spec = codec->spec;
2322 struct hda_input_mux *imux = &spec->input_mux;
2323 int i, n, nums, err;
2325 if (spec->dyn_adc_switch)
2328 nums = spec->num_adc_nids;
2330 if (!spec->auto_mic && imux->num_items > 1) {
2331 struct snd_kcontrol_new *knew;
2333 name = nums > 1 ? "Input Source" : "Capture Source";
2334 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
2340 for (n = 0; n < nums; n++) {
2342 bool inv_dmic = false;
2346 for (i = 0; i < imux->num_items; i++) {
2347 struct nid_path *path;
2348 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2349 get_adc_nid(codec, n, i));
2352 parse_capvol_in_path(codec, path);
2354 vol = path->ctls[NID_PATH_VOL_CTL];
2355 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2358 sw = path->ctls[NID_PATH_MUTE_CTL];
2359 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2361 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2366 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2368 else if (!spec->multi_cap_vol)
2369 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2371 err = create_multi_cap_vol_ctl(codec);
2380 * add mic boosts if needed
2382 static int parse_mic_boost(struct hda_codec *codec)
2384 struct hda_gen_spec *spec = codec->spec;
2385 struct auto_pin_cfg *cfg = &spec->autocfg;
2389 const char *prev_label = NULL;
2391 for (i = 0; i < cfg->num_inputs; i++) {
2392 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2394 nid = cfg->inputs[i].pin;
2395 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2397 char boost_label[32];
2398 struct nid_path *path;
2401 label = hda_get_autocfg_input_label(codec, cfg, i);
2402 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2403 label = "Headphone Mic";
2404 if (prev_label && !strcmp(label, prev_label))
2410 snprintf(boost_label, sizeof(boost_label),
2411 "%s Boost Volume", label);
2412 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2413 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2414 boost_label, type_idx, val);
2418 path = snd_hda_get_nid_path(codec, nid, 0);
2420 path->ctls[NID_PATH_BOOST_CTL] = val;
2427 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2429 static void parse_digital(struct hda_codec *codec)
2431 struct hda_gen_spec *spec = codec->spec;
2432 struct nid_path *path;
2436 /* support multiple SPDIFs; the secondary is set up as a slave */
2438 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2439 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2440 dig_nid = look_for_dac(codec, pin, true);
2443 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
2446 print_nid_path("digout", path);
2447 path->active = true;
2449 spec->multiout.dig_out_nid = dig_nid;
2450 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2452 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2453 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2455 spec->slave_dig_outs[nums - 1] = dig_nid;
2460 if (spec->autocfg.dig_in_pin) {
2461 dig_nid = codec->start_nid;
2462 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2463 unsigned int wcaps = get_wcaps(codec, dig_nid);
2464 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2466 if (!(wcaps & AC_WCAP_DIGITAL))
2468 path = snd_hda_add_new_path(codec,
2469 spec->autocfg.dig_in_pin,
2470 dig_nid, HDA_PARSE_ALL);
2472 print_nid_path("digin", path);
2473 path->active = true;
2474 spec->dig_in_nid = dig_nid;
2483 * input MUX handling
2486 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2488 /* select the given imux item; either unmute exclusively or select the route */
2489 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2492 struct hda_gen_spec *spec = codec->spec;
2493 const struct hda_input_mux *imux;
2494 struct nid_path *path;
2496 imux = &spec->input_mux;
2497 if (!imux->num_items)
2500 if (idx >= imux->num_items)
2501 idx = imux->num_items - 1;
2502 if (spec->cur_mux[adc_idx] == idx)
2505 path = snd_hda_get_nid_path(codec,
2506 spec->imux_pins[spec->cur_mux[adc_idx]],
2507 spec->adc_nids[adc_idx]);
2511 snd_hda_activate_path(codec, path, false, false);
2513 spec->cur_mux[adc_idx] = idx;
2515 if (spec->shared_mic_hp)
2516 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2518 if (spec->dyn_adc_switch)
2519 dyn_adc_pcm_resetup(codec, idx);
2521 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2522 get_adc_nid(codec, adc_idx, idx));
2527 snd_hda_activate_path(codec, path, true, false);
2528 if (spec->cap_sync_hook)
2529 spec->cap_sync_hook(codec);
2535 * Jack detections for HP auto-mute and mic-switch
2538 /* check each pin in the given array; returns true if any of them is plugged */
2539 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2543 for (i = 0; i < num_pins; i++) {
2544 hda_nid_t nid = pins[i];
2547 present |= snd_hda_jack_detect(codec, nid);
2552 /* standard HP/line-out auto-mute helper */
2553 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2554 bool mute, bool hp_out)
2556 struct hda_gen_spec *spec = codec->spec;
2557 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2560 for (i = 0; i < num_pins; i++) {
2561 hda_nid_t nid = pins[i];
2565 /* don't reset VREF value in case it's controlling
2566 * the amp (see alc861_fixup_asus_amp_vref_0f())
2568 if (spec->keep_vref_in_automute) {
2569 val = snd_hda_codec_read(codec, nid, 0,
2570 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2575 snd_hda_set_pin_ctl_cache(codec, nid, val);
2576 set_pin_eapd(codec, nid, !mute);
2580 /* Toggle outputs muting */
2581 void snd_hda_gen_update_outputs(struct hda_codec *codec)
2583 struct hda_gen_spec *spec = codec->spec;
2586 /* Control HP pins/amps depending on master_mute state;
2587 * in general, HP pins/amps control should be enabled in all cases,
2588 * but currently set only for master_mute, just to be safe
2590 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2591 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2592 spec->autocfg.hp_pins, spec->master_mute, true);
2594 if (!spec->automute_speaker)
2597 on = spec->hp_jack_present | spec->line_jack_present;
2598 on |= spec->master_mute;
2599 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2600 spec->autocfg.speaker_pins, on, false);
2602 /* toggle line-out mutes if needed, too */
2603 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2604 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2605 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2607 if (!spec->automute_lo)
2610 on = spec->hp_jack_present;
2611 on |= spec->master_mute;
2612 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2613 spec->autocfg.line_out_pins, on, false);
2615 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
2617 static void call_update_outputs(struct hda_codec *codec)
2619 struct hda_gen_spec *spec = codec->spec;
2620 if (spec->automute_hook)
2621 spec->automute_hook(codec);
2623 snd_hda_gen_update_outputs(codec);
2626 /* standard HP-automute helper */
2627 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2629 struct hda_gen_spec *spec = codec->spec;
2631 spec->hp_jack_present =
2632 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2633 spec->autocfg.hp_pins);
2634 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2636 call_update_outputs(codec);
2638 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
2640 /* standard line-out-automute helper */
2641 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2643 struct hda_gen_spec *spec = codec->spec;
2645 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2647 /* check LO jack only when it's different from HP */
2648 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2651 spec->line_jack_present =
2652 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2653 spec->autocfg.line_out_pins);
2654 if (!spec->automute_speaker || !spec->detect_lo)
2656 call_update_outputs(codec);
2658 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
2660 /* standard mic auto-switch helper */
2661 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
2663 struct hda_gen_spec *spec = codec->spec;
2666 if (!spec->auto_mic)
2669 for (i = spec->am_num_entries - 1; i > 0; i--) {
2670 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2671 mux_select(codec, 0, spec->am_entry[i].idx);
2675 mux_select(codec, 0, spec->am_entry[0].idx);
2677 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
2680 * Auto-Mute mode mixer enum support
2682 static int automute_mode_info(struct snd_kcontrol *kcontrol,
2683 struct snd_ctl_elem_info *uinfo)
2685 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2686 struct hda_gen_spec *spec = codec->spec;
2687 static const char * const texts3[] = {
2688 "Disabled", "Speaker Only", "Line Out+Speaker"
2691 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2692 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2693 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2696 static int automute_mode_get(struct snd_kcontrol *kcontrol,
2697 struct snd_ctl_elem_value *ucontrol)
2699 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2700 struct hda_gen_spec *spec = codec->spec;
2701 unsigned int val = 0;
2702 if (spec->automute_speaker)
2704 if (spec->automute_lo)
2707 ucontrol->value.enumerated.item[0] = val;
2711 static int automute_mode_put(struct snd_kcontrol *kcontrol,
2712 struct snd_ctl_elem_value *ucontrol)
2714 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2715 struct hda_gen_spec *spec = codec->spec;
2717 switch (ucontrol->value.enumerated.item[0]) {
2719 if (!spec->automute_speaker && !spec->automute_lo)
2721 spec->automute_speaker = 0;
2722 spec->automute_lo = 0;
2725 if (spec->automute_speaker_possible) {
2726 if (!spec->automute_lo && spec->automute_speaker)
2728 spec->automute_speaker = 1;
2729 spec->automute_lo = 0;
2730 } else if (spec->automute_lo_possible) {
2731 if (spec->automute_lo)
2733 spec->automute_lo = 1;
2738 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2740 if (spec->automute_speaker && spec->automute_lo)
2742 spec->automute_speaker = 1;
2743 spec->automute_lo = 1;
2748 call_update_outputs(codec);
2752 static const struct snd_kcontrol_new automute_mode_enum = {
2753 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2754 .name = "Auto-Mute Mode",
2755 .info = automute_mode_info,
2756 .get = automute_mode_get,
2757 .put = automute_mode_put,
2760 static int add_automute_mode_enum(struct hda_codec *codec)
2762 struct hda_gen_spec *spec = codec->spec;
2764 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
2770 * Check the availability of HP/line-out auto-mute;
2771 * Set up appropriately if really supported
2773 static int check_auto_mute_availability(struct hda_codec *codec)
2775 struct hda_gen_spec *spec = codec->spec;
2776 struct auto_pin_cfg *cfg = &spec->autocfg;
2780 if (cfg->hp_pins[0])
2782 if (cfg->line_out_pins[0])
2784 if (cfg->speaker_pins[0])
2786 if (present < 2) /* need two different output types */
2789 if (!cfg->speaker_pins[0] &&
2790 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2791 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2792 sizeof(cfg->speaker_pins));
2793 cfg->speaker_outs = cfg->line_outs;
2796 if (!cfg->hp_pins[0] &&
2797 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2798 memcpy(cfg->hp_pins, cfg->line_out_pins,
2799 sizeof(cfg->hp_pins));
2800 cfg->hp_outs = cfg->line_outs;
2803 for (i = 0; i < cfg->hp_outs; i++) {
2804 hda_nid_t nid = cfg->hp_pins[i];
2805 if (!is_jack_detectable(codec, nid))
2807 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2809 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
2810 spec->hp_automute_hook ?
2811 spec->hp_automute_hook :
2812 snd_hda_gen_hp_automute);
2813 spec->detect_hp = 1;
2816 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2817 if (cfg->speaker_outs)
2818 for (i = 0; i < cfg->line_outs; i++) {
2819 hda_nid_t nid = cfg->line_out_pins[i];
2820 if (!is_jack_detectable(codec, nid))
2822 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2823 snd_hda_jack_detect_enable_callback(codec, nid,
2824 HDA_GEN_FRONT_EVENT,
2825 spec->line_automute_hook ?
2826 spec->line_automute_hook :
2827 snd_hda_gen_line_automute);
2828 spec->detect_lo = 1;
2830 spec->automute_lo_possible = spec->detect_hp;
2833 spec->automute_speaker_possible = cfg->speaker_outs &&
2834 (spec->detect_hp || spec->detect_lo);
2836 spec->automute_lo = spec->automute_lo_possible;
2837 spec->automute_speaker = spec->automute_speaker_possible;
2839 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2840 /* create a control for automute mode */
2841 err = add_automute_mode_enum(codec);
2848 /* return the position of NID in the list, or -1 if not found */
2849 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2852 for (i = 0; i < nums; i++)
2858 /* check whether all auto-mic pins are valid; setup indices if OK */
2859 static bool auto_mic_check_imux(struct hda_codec *codec)
2861 struct hda_gen_spec *spec = codec->spec;
2862 const struct hda_input_mux *imux;
2865 imux = &spec->input_mux;
2866 for (i = 0; i < spec->am_num_entries; i++) {
2867 spec->am_entry[i].idx =
2868 find_idx_in_nid_list(spec->am_entry[i].pin,
2869 spec->imux_pins, imux->num_items);
2870 if (spec->am_entry[i].idx < 0)
2871 return false; /* no corresponding imux */
2874 /* we don't need the jack detection for the first pin */
2875 for (i = 1; i < spec->am_num_entries; i++)
2876 snd_hda_jack_detect_enable_callback(codec,
2877 spec->am_entry[i].pin,
2879 spec->mic_autoswitch_hook ?
2880 spec->mic_autoswitch_hook :
2881 snd_hda_gen_mic_autoswitch);
2885 static int compare_attr(const void *ap, const void *bp)
2887 const struct automic_entry *a = ap;
2888 const struct automic_entry *b = bp;
2889 return (int)(a->attr - b->attr);
2893 * Check the availability of auto-mic switch;
2894 * Set up if really supported
2896 static int check_auto_mic_availability(struct hda_codec *codec)
2898 struct hda_gen_spec *spec = codec->spec;
2899 struct auto_pin_cfg *cfg = &spec->autocfg;
2905 for (i = 0; i < cfg->num_inputs; i++) {
2906 hda_nid_t nid = cfg->inputs[i].pin;
2908 attr = snd_hda_codec_get_pincfg(codec, nid);
2909 attr = snd_hda_get_input_pin_attr(attr);
2910 if (types & (1 << attr))
2911 return 0; /* already occupied */
2913 case INPUT_PIN_ATTR_INT:
2914 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2915 return 0; /* invalid type */
2917 case INPUT_PIN_ATTR_UNUSED:
2918 return 0; /* invalid entry */
2920 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
2921 return 0; /* invalid type */
2922 if (!spec->line_in_auto_switch &&
2923 cfg->inputs[i].type != AUTO_PIN_MIC)
2924 return 0; /* only mic is allowed */
2925 if (!is_jack_detectable(codec, nid))
2926 return 0; /* no unsol support */
2929 if (num_pins >= MAX_AUTO_MIC_PINS)
2931 types |= (1 << attr);
2932 spec->am_entry[num_pins].pin = nid;
2933 spec->am_entry[num_pins].attr = attr;
2940 spec->am_num_entries = num_pins;
2941 /* sort the am_entry in the order of attr so that the pin with a
2942 * higher attr will be selected when the jack is plugged.
2944 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
2945 compare_attr, NULL);
2947 if (!auto_mic_check_imux(codec))
2951 spec->num_adc_nids = 1;
2952 spec->cur_mux[0] = spec->am_entry[0].idx;
2953 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
2954 spec->am_entry[0].pin,
2955 spec->am_entry[1].pin,
2956 spec->am_entry[2].pin);
2963 * Parse the given BIOS configuration and set up the hda_gen_spec
2965 * return 1 if successful, 0 if the proper config is not found,
2966 * or a negative error code
2968 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
2969 struct auto_pin_cfg *cfg)
2971 struct hda_gen_spec *spec = codec->spec;
2974 if (cfg != &spec->autocfg) {
2975 spec->autocfg = *cfg;
2976 cfg = &spec->autocfg;
2979 if (!cfg->line_outs) {
2980 if (cfg->dig_outs || cfg->dig_in_pin) {
2981 spec->multiout.max_channels = 2;
2982 spec->no_analog = 1;
2985 return 0; /* can't find valid BIOS pin config */
2988 if (!spec->no_primary_hp &&
2989 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2990 cfg->line_outs <= cfg->hp_outs) {
2991 /* use HP as primary out */
2992 cfg->speaker_outs = cfg->line_outs;
2993 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2994 sizeof(cfg->speaker_pins));
2995 cfg->line_outs = cfg->hp_outs;
2996 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
2998 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
2999 cfg->line_out_type = AUTO_PIN_HP_OUT;
3002 err = parse_output_paths(codec);
3005 err = create_multi_channel_mode(codec);
3008 err = create_multi_out_ctls(codec, cfg);
3011 err = create_hp_out_ctls(codec);
3014 err = create_speaker_out_ctls(codec);
3017 err = create_indep_hp_ctls(codec);
3020 err = create_shared_input(codec);
3023 err = create_input_ctls(codec);
3027 /* check the multiple speaker pins */
3028 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
3029 spec->const_channel_count = cfg->line_outs * 2;
3031 spec->const_channel_count = cfg->speaker_outs * 2;
3033 if (spec->multi_ios > 0)
3034 spec->multiout.max_channels = max(spec->ext_channel_count,
3035 spec->const_channel_count);
3037 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3039 err = check_auto_mute_availability(codec);
3043 err = check_dyn_adc_switch(codec);
3047 if (!spec->shared_mic_hp) {
3048 err = check_auto_mic_availability(codec);
3053 err = create_capture_mixers(codec);
3057 err = parse_mic_boost(codec);
3062 parse_digital(codec);
3066 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
3070 * Build control elements
3073 /* slave controls for virtual master */
3074 static const char * const slave_pfxs[] = {
3075 "Front", "Surround", "Center", "LFE", "Side",
3076 "Headphone", "Speaker", "Mono", "Line Out",
3077 "CLFE", "Bass Speaker", "PCM",
3081 int snd_hda_gen_build_controls(struct hda_codec *codec)
3083 struct hda_gen_spec *spec = codec->spec;
3086 if (spec->kctls.used) {
3087 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3092 if (spec->multiout.dig_out_nid) {
3093 err = snd_hda_create_dig_out_ctls(codec,
3094 spec->multiout.dig_out_nid,
3095 spec->multiout.dig_out_nid,
3096 spec->pcm_rec[1].pcm_type);
3099 if (!spec->no_analog) {
3100 err = snd_hda_create_spdif_share_sw(codec,
3104 spec->multiout.share_spdif = 1;
3107 if (spec->dig_in_nid) {
3108 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3113 /* if we have no master control, let's create it */
3114 if (!spec->no_analog &&
3115 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3116 unsigned int vmaster_tlv[4];
3117 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3118 HDA_OUTPUT, vmaster_tlv);
3119 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3120 vmaster_tlv, slave_pfxs,
3125 if (!spec->no_analog &&
3126 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3127 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3130 true, &spec->vmaster_mute.sw_kctl);
3133 if (spec->vmaster_mute.hook)
3134 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3135 spec->vmaster_mute_enum);
3138 free_kctls(spec); /* no longer needed */
3140 if (spec->shared_mic_hp) {
3142 int nid = spec->autocfg.inputs[1].pin;
3143 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3146 err = snd_hda_jack_detect_enable(codec, nid, 0);
3151 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3157 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3165 * Analog playback callbacks
3167 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3168 struct hda_codec *codec,
3169 struct snd_pcm_substream *substream)
3171 struct hda_gen_spec *spec = codec->spec;
3174 mutex_lock(&spec->pcm_mutex);
3175 err = snd_hda_multi_out_analog_open(codec,
3176 &spec->multiout, substream,
3179 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3180 mutex_unlock(&spec->pcm_mutex);
3184 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3185 struct hda_codec *codec,
3186 unsigned int stream_tag,
3187 unsigned int format,
3188 struct snd_pcm_substream *substream)
3190 struct hda_gen_spec *spec = codec->spec;
3191 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3192 stream_tag, format, substream);
3195 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3196 struct hda_codec *codec,
3197 struct snd_pcm_substream *substream)
3199 struct hda_gen_spec *spec = codec->spec;
3200 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3203 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3204 struct hda_codec *codec,
3205 struct snd_pcm_substream *substream)
3207 struct hda_gen_spec *spec = codec->spec;
3208 mutex_lock(&spec->pcm_mutex);
3209 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3210 mutex_unlock(&spec->pcm_mutex);
3214 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3215 struct hda_codec *codec,
3216 struct snd_pcm_substream *substream)
3218 struct hda_gen_spec *spec = codec->spec;
3221 mutex_lock(&spec->pcm_mutex);
3222 if (!spec->indep_hp_enabled)
3225 spec->active_streams |= 1 << STREAM_INDEP_HP;
3226 mutex_unlock(&spec->pcm_mutex);
3230 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3231 struct hda_codec *codec,
3232 struct snd_pcm_substream *substream)
3234 struct hda_gen_spec *spec = codec->spec;
3235 mutex_lock(&spec->pcm_mutex);
3236 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3237 mutex_unlock(&spec->pcm_mutex);
3244 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3245 struct hda_codec *codec,
3246 struct snd_pcm_substream *substream)
3248 struct hda_gen_spec *spec = codec->spec;
3249 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3252 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3253 struct hda_codec *codec,
3254 unsigned int stream_tag,
3255 unsigned int format,
3256 struct snd_pcm_substream *substream)
3258 struct hda_gen_spec *spec = codec->spec;
3259 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3260 stream_tag, format, substream);
3263 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3264 struct hda_codec *codec,
3265 struct snd_pcm_substream *substream)
3267 struct hda_gen_spec *spec = codec->spec;
3268 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3271 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3272 struct hda_codec *codec,
3273 struct snd_pcm_substream *substream)
3275 struct hda_gen_spec *spec = codec->spec;
3276 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3282 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3283 struct hda_codec *codec,
3284 unsigned int stream_tag,
3285 unsigned int format,
3286 struct snd_pcm_substream *substream)
3288 struct hda_gen_spec *spec = codec->spec;
3290 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
3291 stream_tag, 0, format);
3295 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3296 struct hda_codec *codec,
3297 struct snd_pcm_substream *substream)
3299 struct hda_gen_spec *spec = codec->spec;
3301 snd_hda_codec_cleanup_stream(codec,
3302 spec->adc_nids[substream->number + 1]);
3308 static const struct hda_pcm_stream pcm_analog_playback = {
3312 /* NID is set in build_pcms */
3314 .open = playback_pcm_open,
3315 .close = playback_pcm_close,
3316 .prepare = playback_pcm_prepare,
3317 .cleanup = playback_pcm_cleanup
3321 static const struct hda_pcm_stream pcm_analog_capture = {
3325 /* NID is set in build_pcms */
3328 static const struct hda_pcm_stream pcm_analog_alt_playback = {
3332 /* NID is set in build_pcms */
3334 .open = alt_playback_pcm_open,
3335 .close = alt_playback_pcm_close
3339 static const struct hda_pcm_stream pcm_analog_alt_capture = {
3340 .substreams = 2, /* can be overridden */
3343 /* NID is set in build_pcms */
3345 .prepare = alt_capture_pcm_prepare,
3346 .cleanup = alt_capture_pcm_cleanup
3350 static const struct hda_pcm_stream pcm_digital_playback = {
3354 /* NID is set in build_pcms */
3356 .open = dig_playback_pcm_open,
3357 .close = dig_playback_pcm_close,
3358 .prepare = dig_playback_pcm_prepare,
3359 .cleanup = dig_playback_pcm_cleanup
3363 static const struct hda_pcm_stream pcm_digital_capture = {
3367 /* NID is set in build_pcms */
3370 /* Used by build_pcms to flag that a PCM has no playback stream */
3371 static const struct hda_pcm_stream pcm_null_stream = {
3378 * dynamic changing ADC PCM streams
3380 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3382 struct hda_gen_spec *spec = codec->spec;
3383 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3385 if (spec->cur_adc && spec->cur_adc != new_adc) {
3386 /* stream is running, let's swap the current ADC */
3387 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3388 spec->cur_adc = new_adc;
3389 snd_hda_codec_setup_stream(codec, new_adc,
3390 spec->cur_adc_stream_tag, 0,
3391 spec->cur_adc_format);
3397 /* analog capture with dynamic dual-adc changes */
3398 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3399 struct hda_codec *codec,
3400 unsigned int stream_tag,
3401 unsigned int format,
3402 struct snd_pcm_substream *substream)
3404 struct hda_gen_spec *spec = codec->spec;
3405 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3406 spec->cur_adc_stream_tag = stream_tag;
3407 spec->cur_adc_format = format;
3408 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3412 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3413 struct hda_codec *codec,
3414 struct snd_pcm_substream *substream)
3416 struct hda_gen_spec *spec = codec->spec;
3417 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3422 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3426 .nid = 0, /* fill later */
3428 .prepare = dyn_adc_capture_pcm_prepare,
3429 .cleanup = dyn_adc_capture_pcm_cleanup
3433 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3434 const char *chip_name)
3440 strlcpy(str, chip_name, len);
3442 /* drop non-alnum chars after a space */
3443 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3444 if (!isalnum(p[1])) {
3449 strlcat(str, sfx, len);
3452 /* build PCM streams based on the parsed results */
3453 int snd_hda_gen_build_pcms(struct hda_codec *codec)
3455 struct hda_gen_spec *spec = codec->spec;
3456 struct hda_pcm *info = spec->pcm_rec;
3457 const struct hda_pcm_stream *p;
3458 bool have_multi_adcs;
3460 codec->num_pcms = 1;
3461 codec->pcm_info = info;
3463 if (spec->no_analog)
3466 fill_pcm_stream_name(spec->stream_name_analog,
3467 sizeof(spec->stream_name_analog),
3468 " Analog", codec->chip_name);
3469 info->name = spec->stream_name_analog;
3471 if (spec->multiout.num_dacs > 0) {
3472 p = spec->stream_analog_playback;
3474 p = &pcm_analog_playback;
3475 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3476 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3477 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3478 spec->multiout.max_channels;
3479 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3480 spec->autocfg.line_outs == 2)
3481 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3484 if (spec->num_adc_nids) {
3485 p = spec->stream_analog_capture;
3487 if (spec->dyn_adc_switch)
3488 p = &dyn_adc_pcm_analog_capture;
3490 p = &pcm_analog_capture;
3492 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3493 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3497 /* SPDIF for stream index #1 */
3498 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
3499 fill_pcm_stream_name(spec->stream_name_digital,
3500 sizeof(spec->stream_name_digital),
3501 " Digital", codec->chip_name);
3502 codec->num_pcms = 2;
3503 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3504 info = spec->pcm_rec + 1;
3505 info->name = spec->stream_name_digital;
3506 if (spec->dig_out_type)
3507 info->pcm_type = spec->dig_out_type;
3509 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3510 if (spec->multiout.dig_out_nid) {
3511 p = spec->stream_digital_playback;
3513 p = &pcm_digital_playback;
3514 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3515 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3517 if (spec->dig_in_nid) {
3518 p = spec->stream_digital_capture;
3520 p = &pcm_digital_capture;
3521 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3522 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3526 if (spec->no_analog)
3529 /* If the use of more than one ADC is requested for the current
3530 * model, configure a second analog capture-only PCM.
3532 have_multi_adcs = (spec->num_adc_nids > 1) &&
3533 !spec->dyn_adc_switch && !spec->auto_mic;
3534 /* Additional Analaog capture for index #2 */
3535 if (spec->alt_dac_nid || have_multi_adcs) {
3536 codec->num_pcms = 3;
3537 info = spec->pcm_rec + 2;
3538 info->name = spec->stream_name_analog;
3539 if (spec->alt_dac_nid) {
3540 p = spec->stream_analog_alt_playback;
3542 p = &pcm_analog_alt_playback;
3543 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3544 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3547 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3549 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3551 if (have_multi_adcs) {
3552 p = spec->stream_analog_alt_capture;
3554 p = &pcm_analog_alt_capture;
3555 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3556 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3558 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3559 spec->num_adc_nids - 1;
3561 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3563 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3569 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3573 * Standard auto-parser initializations
3576 /* configure the path from the given dac to the pin as the proper output */
3577 static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3578 int pin_type, hda_nid_t dac)
3580 struct nid_path *path;
3582 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3583 path = snd_hda_get_nid_path(codec, dac, pin);
3586 snd_hda_activate_path(codec, path, path->active, true);
3587 set_pin_eapd(codec, pin, path->active);
3590 /* initialize primary output paths */
3591 static void init_multi_out(struct hda_codec *codec)
3593 struct hda_gen_spec *spec = codec->spec;
3598 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3603 for (i = 0; i < spec->autocfg.line_outs; i++) {
3604 nid = spec->autocfg.line_out_pins[i];
3606 dac = spec->multiout.dac_nids[i];
3608 dac = spec->multiout.dac_nids[0];
3609 set_output_and_unmute(codec, nid, pin_type, dac);
3615 static void __init_extra_out(struct hda_codec *codec, int num_outs,
3616 hda_nid_t *pins, hda_nid_t *dacs, int type)
3618 struct hda_gen_spec *spec = codec->spec;
3622 for (i = 0; i < num_outs; i++) {
3628 if (i > 0 && dacs[0])
3631 dac = spec->multiout.dac_nids[0];
3633 set_output_and_unmute(codec, pin, type, dac);
3637 /* initialize hp and speaker paths */
3638 static void init_extra_out(struct hda_codec *codec)
3640 struct hda_gen_spec *spec = codec->spec;
3642 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3643 __init_extra_out(codec, spec->autocfg.hp_outs,
3644 spec->autocfg.hp_pins,
3645 spec->multiout.hp_out_nid, PIN_HP);
3646 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3647 __init_extra_out(codec, spec->autocfg.speaker_outs,
3648 spec->autocfg.speaker_pins,
3649 spec->multiout.extra_out_nid, PIN_OUT);
3652 /* initialize multi-io paths */
3653 static void init_multi_io(struct hda_codec *codec)
3655 struct hda_gen_spec *spec = codec->spec;
3658 for (i = 0; i < spec->multi_ios; i++) {
3659 hda_nid_t pin = spec->multi_io[i].pin;
3660 struct nid_path *path;
3661 path = snd_hda_get_nid_path(codec, spec->multi_io[i].dac, pin);
3664 if (!spec->multi_io[i].ctl_in)
3665 spec->multi_io[i].ctl_in =
3666 snd_hda_codec_update_cache(codec, pin, 0,
3667 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3668 snd_hda_activate_path(codec, path, path->active, true);
3672 /* set up the input pin config, depending on the given auto-pin type */
3673 static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3676 unsigned int val = PIN_IN;
3677 if (auto_pin_type == AUTO_PIN_MIC)
3678 val |= snd_hda_get_default_vref(codec, nid);
3679 snd_hda_set_pin_ctl_cache(codec, nid, val);
3682 /* set up input pins and loopback paths */
3683 static void init_analog_input(struct hda_codec *codec)
3685 struct hda_gen_spec *spec = codec->spec;
3686 struct auto_pin_cfg *cfg = &spec->autocfg;
3689 for (i = 0; i < cfg->num_inputs; i++) {
3690 hda_nid_t nid = cfg->inputs[i].pin;
3691 if (is_input_pin(codec, nid))
3692 set_input_pin(codec, nid, cfg->inputs[i].type);
3694 /* init loopback inputs */
3695 if (spec->mixer_nid) {
3696 struct nid_path *path;
3697 path = snd_hda_get_nid_path(codec, nid, spec->mixer_nid);
3699 snd_hda_activate_path(codec, path,
3700 path->active, false);
3705 /* initialize ADC paths */
3706 static void init_input_src(struct hda_codec *codec)
3708 struct hda_gen_spec *spec = codec->spec;
3709 struct hda_input_mux *imux = &spec->input_mux;
3710 struct nid_path *path;
3713 if (spec->dyn_adc_switch)
3716 nums = spec->num_adc_nids;
3718 for (c = 0; c < nums; c++) {
3719 for (i = 0; i < imux->num_items; i++) {
3720 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3721 get_adc_nid(codec, c, i));
3723 bool active = path->active;
3724 if (i == spec->cur_mux[c])
3726 snd_hda_activate_path(codec, path, active, false);
3731 if (spec->shared_mic_hp)
3732 update_shared_mic_hp(codec, spec->cur_mux[0]);
3734 if (spec->cap_sync_hook)
3735 spec->cap_sync_hook(codec);
3738 /* set right pin controls for digital I/O */
3739 static void init_digital(struct hda_codec *codec)
3741 struct hda_gen_spec *spec = codec->spec;
3745 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3746 pin = spec->autocfg.dig_out_pins[i];
3749 set_output_and_unmute(codec, pin, PIN_OUT, 0);
3751 pin = spec->autocfg.dig_in_pin;
3753 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
3756 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3757 * invalid unsol tags by some reason
3759 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3763 for (i = 0; i < codec->init_pins.used; i++) {
3764 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3765 hda_nid_t nid = pin->nid;
3766 if (is_jack_detectable(codec, nid) &&
3767 !snd_hda_jack_tbl_get(codec, nid))
3768 snd_hda_codec_update_cache(codec, nid, 0,
3769 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3773 int snd_hda_gen_init(struct hda_codec *codec)
3775 struct hda_gen_spec *spec = codec->spec;
3777 if (spec->init_hook)
3778 spec->init_hook(codec);
3780 snd_hda_apply_verbs(codec);
3782 codec->cached_write = 1;
3784 init_multi_out(codec);
3785 init_extra_out(codec);
3786 init_multi_io(codec);
3787 init_analog_input(codec);
3788 init_input_src(codec);
3789 init_digital(codec);
3791 clear_unsol_on_unused_pins(codec);
3793 /* call init functions of standard auto-mute helpers */
3794 snd_hda_gen_hp_automute(codec, NULL);
3795 snd_hda_gen_line_automute(codec, NULL);
3796 snd_hda_gen_mic_autoswitch(codec, NULL);
3798 snd_hda_codec_flush_amp_cache(codec);
3799 snd_hda_codec_flush_cmd_cache(codec);
3801 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3802 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3804 hda_call_check_power_status(codec, 0x01);
3807 EXPORT_SYMBOL(snd_hda_gen_init);
3811 * the generic codec support
3815 static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3817 struct hda_gen_spec *spec = codec->spec;
3818 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3822 static void generic_free(struct hda_codec *codec)
3824 snd_hda_gen_spec_free(codec->spec);
3829 static const struct hda_codec_ops generic_patch_ops = {
3830 .build_controls = snd_hda_gen_build_controls,
3831 .build_pcms = snd_hda_gen_build_pcms,
3832 .init = snd_hda_gen_init,
3833 .free = generic_free,
3834 .unsol_event = snd_hda_jack_unsol_event,
3836 .check_power_status = generic_check_power_status,
3840 int snd_hda_parse_generic_codec(struct hda_codec *codec)
3842 struct hda_gen_spec *spec;
3845 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3848 snd_hda_gen_spec_init(spec);
3851 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3855 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
3859 codec->patch_ops = generic_patch_ops;
3863 generic_free(codec);
3866 EXPORT_SYMBOL(snd_hda_parse_generic_codec);