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->paths, sizeof(struct nid_path), 8);
43 mutex_init(&spec->pcm_mutex);
46 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
48 struct snd_kcontrol_new *
49 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
50 const struct snd_kcontrol_new *temp)
52 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
57 knew->name = kstrdup(name, GFP_KERNEL);
59 knew->name = kstrdup(knew->name, GFP_KERNEL);
64 EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
66 static void free_kctls(struct hda_gen_spec *spec)
68 if (spec->kctls.list) {
69 struct snd_kcontrol_new *kctl = spec->kctls.list;
71 for (i = 0; i < spec->kctls.used; i++)
74 snd_array_free(&spec->kctls);
77 void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
82 snd_array_free(&spec->paths);
84 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
90 static struct nid_path *get_nid_path(struct hda_codec *codec,
91 hda_nid_t from_nid, hda_nid_t to_nid,
94 struct hda_gen_spec *spec = codec->spec;
97 for (i = 0; i < spec->paths.used; i++) {
98 struct nid_path *path = snd_array_elem(&spec->paths, i);
101 if ((!from_nid || path->path[0] == from_nid) &&
102 (!to_nid || path->path[path->depth - 1] == to_nid)) {
103 if (with_aa_mix == HDA_PARSE_ALL ||
104 path->with_aa_mix == with_aa_mix)
111 /* get the path between the given NIDs;
112 * passing 0 to either @pin or @dac behaves as a wildcard
114 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
115 hda_nid_t from_nid, hda_nid_t to_nid)
117 return get_nid_path(codec, from_nid, to_nid, HDA_PARSE_ALL);
119 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
121 /* get the index number corresponding to the path instance;
122 * the index starts from 1, for easier checking the invalid value
124 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
126 struct hda_gen_spec *spec = codec->spec;
127 struct nid_path *array = spec->paths.list;
130 if (!spec->paths.used)
133 if (idx < 0 || idx >= spec->paths.used)
138 /* get the path instance corresponding to the given index number */
139 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
141 struct hda_gen_spec *spec = codec->spec;
143 if (idx <= 0 || idx > spec->paths.used)
145 return snd_array_elem(&spec->paths, idx - 1);
148 /* check whether the given DAC is already found in any existing paths */
149 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
151 struct hda_gen_spec *spec = codec->spec;
154 for (i = 0; i < spec->paths.used; i++) {
155 struct nid_path *path = snd_array_elem(&spec->paths, i);
156 if (path->path[0] == nid)
162 /* check whether the given two widgets can be connected */
163 static bool is_reachable_path(struct hda_codec *codec,
164 hda_nid_t from_nid, hda_nid_t to_nid)
166 if (!from_nid || !to_nid)
168 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
171 /* nid, dir and idx */
172 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
174 /* check whether the given ctl is already assigned in any path elements */
175 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
177 struct hda_gen_spec *spec = codec->spec;
180 val &= AMP_VAL_COMPARE_MASK;
181 for (i = 0; i < spec->paths.used; i++) {
182 struct nid_path *path = snd_array_elem(&spec->paths, i);
183 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
189 /* check whether a control with the given (nid, dir, idx) was assigned */
190 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
193 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
194 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
195 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
198 static void print_nid_path(const char *pfx, struct nid_path *path)
205 for (i = 0; i < path->depth; i++) {
207 sprintf(tmp, ":%02x", path->path[i]);
208 strlcat(buf, tmp, sizeof(buf));
210 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
213 /* called recursively */
214 static bool __parse_nid_path(struct hda_codec *codec,
215 hda_nid_t from_nid, hda_nid_t to_nid,
216 int with_aa_mix, struct nid_path *path, int depth)
218 struct hda_gen_spec *spec = codec->spec;
219 const hda_nid_t *conn;
222 if (to_nid == spec->mixer_nid) {
223 if (with_aa_mix == HDA_PARSE_NO_AAMIX)
225 with_aa_mix = HDA_PARSE_ALL; /* mark aa-mix is included */
228 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
229 for (i = 0; i < nums; i++) {
230 if (conn[i] != from_nid) {
231 /* special case: when from_nid is 0,
232 * try to find an empty DAC
235 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
236 is_dac_already_used(codec, conn[i]))
239 /* aa-mix is requested but not included? */
240 if (!(spec->mixer_nid && with_aa_mix == HDA_PARSE_ONLY_AAMIX))
243 if (depth >= MAX_NID_PATH_DEPTH)
245 for (i = 0; i < nums; i++) {
247 type = get_wcaps_type(get_wcaps(codec, conn[i]));
248 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
251 if (__parse_nid_path(codec, from_nid, conn[i],
252 with_aa_mix, path, depth + 1))
258 path->path[path->depth] = conn[i];
259 if (conn[i] == spec->mixer_nid)
260 path->with_aa_mix = true;
261 path->idx[path->depth + 1] = i;
262 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
263 path->multi[path->depth + 1] = 1;
268 /* parse the widget path from the given nid to the target nid;
269 * when @from_nid is 0, try to find an empty DAC;
270 * when @with_aa_mix is HDA_PARSE_NO_AAMIX, paths with spec->mixer_nid are
271 * excluded, only the paths that don't go through the mixer will be chosen.
272 * when @with_aa_mix is HDA_PARSE_ONLY_AAMIX, only the paths going through
273 * spec->mixer_nid will be chosen.
274 * when @with_aa_mix is HDA_PARSE_ALL, no special handling about mixer widget.
276 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
277 hda_nid_t to_nid, int with_aa_mix,
278 struct nid_path *path)
280 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
281 path->path[path->depth] = to_nid;
287 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
290 * parse the path between the given NIDs and add to the path list.
291 * if no valid path is found, return NULL
294 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
295 hda_nid_t to_nid, int with_aa_mix)
297 struct hda_gen_spec *spec = codec->spec;
298 struct nid_path *path;
300 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
303 /* check whether the path has been already added */
304 path = get_nid_path(codec, from_nid, to_nid, with_aa_mix);
308 path = snd_array_new(&spec->paths);
311 memset(path, 0, sizeof(*path));
312 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
318 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
320 /* look for an empty DAC slot */
321 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
324 struct hda_gen_spec *spec = codec->spec;
328 for (i = 0; i < spec->num_all_dacs; i++) {
329 hda_nid_t nid = spec->all_dacs[i];
330 if (!nid || is_dac_already_used(codec, nid))
332 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
333 if (is_digital != cap_digital)
335 if (is_reachable_path(codec, nid, pin))
341 /* replace the channels in the composed amp value with the given number */
342 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
344 val &= ~(0x3U << 16);
349 /* check whether the widget has the given amp capability for the direction */
350 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
351 int dir, unsigned int bits)
355 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
356 if (query_amp_caps(codec, nid, dir) & bits)
361 #define nid_has_mute(codec, nid, dir) \
362 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
363 #define nid_has_volume(codec, nid, dir) \
364 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
366 /* look for a widget suitable for assigning a mute switch in the path */
367 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
368 struct nid_path *path)
372 for (i = path->depth - 1; i >= 0; i--) {
373 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
374 return path->path[i];
375 if (i != path->depth - 1 && i != 0 &&
376 nid_has_mute(codec, path->path[i], HDA_INPUT))
377 return path->path[i];
382 /* look for a widget suitable for assigning a volume ctl in the path */
383 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
384 struct nid_path *path)
388 for (i = path->depth - 1; i >= 0; i--) {
389 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
390 return path->path[i];
396 * path activation / deactivation
399 /* can have the amp-in capability? */
400 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
402 hda_nid_t nid = path->path[idx];
403 unsigned int caps = get_wcaps(codec, nid);
404 unsigned int type = get_wcaps_type(caps);
406 if (!(caps & AC_WCAP_IN_AMP))
408 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
413 /* can have the amp-out capability? */
414 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
416 hda_nid_t nid = path->path[idx];
417 unsigned int caps = get_wcaps(codec, nid);
418 unsigned int type = get_wcaps_type(caps);
420 if (!(caps & AC_WCAP_OUT_AMP))
422 if (type == AC_WID_PIN && !idx) /* only for output pins */
427 /* check whether the given (nid,dir,idx) is active */
428 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
429 unsigned int idx, unsigned int dir)
431 struct hda_gen_spec *spec = codec->spec;
434 for (n = 0; n < spec->paths.used; n++) {
435 struct nid_path *path = snd_array_elem(&spec->paths, n);
438 for (i = 0; i < path->depth; i++) {
439 if (path->path[i] == nid) {
440 if (dir == HDA_OUTPUT || path->idx[i] == idx)
449 /* get the default amp value for the target state */
450 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
451 int dir, bool enable)
454 unsigned int val = 0;
456 caps = query_amp_caps(codec, nid, dir);
457 if (caps & AC_AMPCAP_NUM_STEPS) {
460 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
462 if (caps & AC_AMPCAP_MUTE) {
469 /* initialize the amp value (only at the first time) */
470 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
472 int val = get_amp_val_to_activate(codec, nid, dir, false);
473 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
476 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
477 int idx, bool enable)
480 if (is_ctl_associated(codec, nid, dir, idx) ||
481 (!enable && is_active_nid(codec, nid, dir, idx)))
483 val = get_amp_val_to_activate(codec, nid, dir, enable);
484 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
487 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
490 hda_nid_t nid = path->path[i];
491 init_amp(codec, nid, HDA_OUTPUT, 0);
492 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
495 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
496 int i, bool enable, bool add_aamix)
498 struct hda_gen_spec *spec = codec->spec;
499 const hda_nid_t *conn;
502 hda_nid_t nid = path->path[i];
504 nums = snd_hda_get_conn_list(codec, nid, &conn);
505 type = get_wcaps_type(get_wcaps(codec, nid));
506 if (type == AC_WID_PIN ||
507 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
513 for (n = 0; n < nums; n++)
514 init_amp(codec, nid, HDA_INPUT, n);
516 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
519 /* here is a little bit tricky in comparison with activate_amp_out();
520 * when aa-mixer is available, we need to enable the path as well
522 for (n = 0; n < nums; n++) {
523 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
525 activate_amp(codec, nid, HDA_INPUT, n, enable);
529 /* activate or deactivate the given path
530 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
532 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
533 bool enable, bool add_aamix)
538 path->active = false;
540 for (i = path->depth - 1; i >= 0; i--) {
541 if (enable && path->multi[i])
542 snd_hda_codec_write_cache(codec, path->path[i], 0,
543 AC_VERB_SET_CONNECT_SEL,
545 if (has_amp_in(codec, path, i))
546 activate_amp_in(codec, path, i, enable, add_aamix);
547 if (has_amp_out(codec, path, i))
548 activate_amp_out(codec, path, i, enable);
554 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
556 /* turn on/off EAPD on the given pin */
557 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
559 struct hda_gen_spec *spec = codec->spec;
560 if (spec->own_eapd_ctl ||
561 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
565 snd_hda_codec_update_cache(codec, pin, 0,
566 AC_VERB_SET_EAPD_BTLENABLE,
567 enable ? 0x02 : 0x00);
572 * Helper functions for creating mixer ctl elements
582 static const struct snd_kcontrol_new control_templates[] = {
583 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
584 HDA_CODEC_MUTE(NULL, 0, 0, 0),
585 HDA_BIND_MUTE(NULL, 0, 0, 0),
586 HDA_BIND_VOL(NULL, 0),
587 HDA_BIND_SW(NULL, 0),
590 /* add dynamic controls from template */
591 static int add_control(struct hda_gen_spec *spec, int type, const char *name,
592 int cidx, unsigned long val)
594 struct snd_kcontrol_new *knew;
596 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
600 if (get_amp_nid_(val))
601 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
602 knew->private_value = val;
606 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
607 const char *pfx, const char *dir,
608 const char *sfx, int cidx, unsigned long val)
611 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
612 return add_control(spec, type, name, cidx, val);
615 #define add_pb_vol_ctrl(spec, type, pfx, val) \
616 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
617 #define add_pb_sw_ctrl(spec, type, pfx, val) \
618 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
619 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
620 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
621 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
622 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
624 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
625 unsigned int chs, struct nid_path *path)
630 val = path->ctls[NID_PATH_VOL_CTL];
633 val = amp_val_replace_channels(val, chs);
634 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
637 /* return the channel bits suitable for the given path->ctls[] */
638 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
641 int chs = 1; /* mono (left only) */
643 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
644 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
645 chs = 3; /* stereo */
650 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
651 struct nid_path *path)
653 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
654 return add_vol_ctl(codec, pfx, cidx, chs, path);
657 /* create a mute-switch for the given mixer widget;
658 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
660 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
661 unsigned int chs, struct nid_path *path)
664 int type = HDA_CTL_WIDGET_MUTE;
668 val = path->ctls[NID_PATH_MUTE_CTL];
671 val = amp_val_replace_channels(val, chs);
672 if (get_amp_direction_(val) == HDA_INPUT) {
673 hda_nid_t nid = get_amp_nid_(val);
674 int nums = snd_hda_get_num_conns(codec, nid);
676 type = HDA_CTL_BIND_MUTE;
680 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
683 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
684 int cidx, struct nid_path *path)
686 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
687 return add_sw_ctl(codec, pfx, cidx, chs, path);
690 static const char * const channel_name[4] = {
691 "Front", "Surround", "CLFE", "Side"
694 /* give some appropriate ctl name prefix for the given line out channel */
695 static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
696 bool can_be_master, int *index)
698 struct auto_pin_cfg *cfg = &spec->autocfg;
701 if (cfg->line_outs == 1 && !spec->multi_ios &&
702 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
703 return spec->vmaster_mute.hook ? "PCM" : "Master";
705 /* if there is really a single DAC used in the whole output paths,
706 * use it master (or "PCM" if a vmaster hook is present)
708 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
709 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
710 return spec->vmaster_mute.hook ? "PCM" : "Master";
712 switch (cfg->line_out_type) {
713 case AUTO_PIN_SPEAKER_OUT:
714 if (cfg->line_outs == 1)
716 if (cfg->line_outs == 2)
717 return ch ? "Bass Speaker" : "Speaker";
719 case AUTO_PIN_HP_OUT:
720 /* for multi-io case, only the primary out */
721 if (ch && spec->multi_ios)
726 if (cfg->line_outs == 1 && !spec->multi_ios)
730 if (ch >= ARRAY_SIZE(channel_name)) {
735 return channel_name[ch];
742 /* badness definition */
744 /* No primary DAC is found for the main output */
745 BAD_NO_PRIMARY_DAC = 0x10000,
746 /* No DAC is found for the extra output */
748 /* No possible multi-ios */
749 BAD_MULTI_IO = 0x103,
750 /* No individual DAC for extra output */
751 BAD_NO_EXTRA_DAC = 0x102,
752 /* No individual DAC for extra surrounds */
753 BAD_NO_EXTRA_SURR_DAC = 0x101,
754 /* Primary DAC shared with main surrounds */
755 BAD_SHARED_SURROUND = 0x100,
756 /* Primary DAC shared with main CLFE */
757 BAD_SHARED_CLFE = 0x10,
758 /* Primary DAC shared with extra surrounds */
759 BAD_SHARED_EXTRA_SURROUND = 0x10,
760 /* Volume widget is shared */
761 BAD_SHARED_VOL = 0x10,
764 /* look for widgets in the path between the given NIDs appropriate for
765 * volume and mute controls, and assign the values to ctls[].
767 * When no appropriate widget is found in the path, the badness value
768 * is incremented depending on the situation. The function returns the
769 * total badness for both volume and mute controls.
771 static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
774 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
780 return BAD_SHARED_VOL * 2;
781 nid = look_for_out_vol_nid(codec, path);
783 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
784 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
785 badness += BAD_SHARED_VOL;
787 path->ctls[NID_PATH_VOL_CTL] = val;
789 badness += BAD_SHARED_VOL;
790 nid = look_for_out_mute_nid(codec, path);
792 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
793 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
794 nid_has_mute(codec, nid, HDA_OUTPUT))
795 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
797 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
798 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
799 badness += BAD_SHARED_VOL;
801 path->ctls[NID_PATH_MUTE_CTL] = val;
803 badness += BAD_SHARED_VOL;
807 struct badness_table {
808 int no_primary_dac; /* no primary DAC */
809 int no_dac; /* no secondary DACs */
810 int shared_primary; /* primary DAC is shared with main output */
811 int shared_surr; /* secondary DAC shared with main or primary */
812 int shared_clfe; /* third DAC shared with main or primary */
813 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
816 static struct badness_table main_out_badness = {
817 .no_primary_dac = BAD_NO_PRIMARY_DAC,
818 .no_dac = BAD_NO_DAC,
819 .shared_primary = BAD_NO_PRIMARY_DAC,
820 .shared_surr = BAD_SHARED_SURROUND,
821 .shared_clfe = BAD_SHARED_CLFE,
822 .shared_surr_main = BAD_SHARED_SURROUND,
825 static struct badness_table extra_out_badness = {
826 .no_primary_dac = BAD_NO_DAC,
827 .no_dac = BAD_NO_DAC,
828 .shared_primary = BAD_NO_EXTRA_DAC,
829 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
830 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
831 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
834 /* get the DAC of the primary output corresponding to the given array index */
835 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
837 struct hda_gen_spec *spec = codec->spec;
838 struct auto_pin_cfg *cfg = &spec->autocfg;
840 if (cfg->line_outs > idx)
841 return spec->private_dac_nids[idx];
842 idx -= cfg->line_outs;
843 if (spec->multi_ios > idx)
844 return spec->multi_io[idx].dac;
848 /* return the DAC if it's reachable, otherwise zero */
849 static inline hda_nid_t try_dac(struct hda_codec *codec,
850 hda_nid_t dac, hda_nid_t pin)
852 return is_reachable_path(codec, dac, pin) ? dac : 0;
855 /* try to assign DACs to pins and return the resultant badness */
856 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
857 const hda_nid_t *pins, hda_nid_t *dacs,
859 const struct badness_table *bad)
861 struct hda_gen_spec *spec = codec->spec;
869 for (i = 0; i < num_outs; i++) {
870 struct nid_path *path;
871 hda_nid_t pin = pins[i];
874 badness += assign_out_path_ctls(codec, pin, dacs[i]);
878 dacs[i] = look_for_dac(codec, pin, false);
879 if (!dacs[i] && !i) {
880 for (j = 1; j < num_outs; j++) {
881 if (is_reachable_path(codec, dacs[j], pin)) {
892 dac = try_dac(codec, get_primary_out(codec, i), pin);
894 dac = try_dac(codec, dacs[0], pin);
896 dac = try_dac(codec, get_primary_out(codec, i), pin);
899 badness += bad->shared_primary;
901 badness += bad->shared_surr;
903 badness += bad->shared_clfe;
904 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
905 dac = spec->private_dac_nids[0];
906 badness += bad->shared_surr_main;
908 badness += bad->no_primary_dac;
910 badness += bad->no_dac;
912 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_NO_AAMIX);
913 if (!path && !i && spec->mixer_nid) {
915 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_ALL);
920 print_nid_path("output", path);
922 path_idx[i] = snd_hda_get_path_idx(codec, path);
925 badness += assign_out_path_ctls(codec, pin, dac);
931 /* return NID if the given pin has only a single connection to a certain DAC */
932 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
934 struct hda_gen_spec *spec = codec->spec;
936 hda_nid_t nid_found = 0;
938 for (i = 0; i < spec->num_all_dacs; i++) {
939 hda_nid_t nid = spec->all_dacs[i];
940 if (!nid || is_dac_already_used(codec, nid))
942 if (is_reachable_path(codec, nid, pin)) {
951 /* check whether the given pin can be a multi-io pin */
952 static bool can_be_multiio_pin(struct hda_codec *codec,
953 unsigned int location, hda_nid_t nid)
955 unsigned int defcfg, caps;
957 defcfg = snd_hda_codec_get_pincfg(codec, nid);
958 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
960 if (location && get_defcfg_location(defcfg) != location)
962 caps = snd_hda_query_pin_caps(codec, nid);
963 if (!(caps & AC_PINCAP_OUT))
968 /* count the number of input pins that are capable to be multi-io */
969 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
971 struct hda_gen_spec *spec = codec->spec;
972 struct auto_pin_cfg *cfg = &spec->autocfg;
973 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
974 unsigned int location = get_defcfg_location(defcfg);
978 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
979 for (i = 0; i < cfg->num_inputs; i++) {
980 if (cfg->inputs[i].type != type)
982 if (can_be_multiio_pin(codec, location,
993 * When hardwired is set, try to fill ony hardwired pins, and returns
994 * zero if any pins are filled, non-zero if nothing found.
995 * When hardwired is off, try to fill possible input pins, and returns
998 static int fill_multi_ios(struct hda_codec *codec,
999 hda_nid_t reference_pin,
1002 struct hda_gen_spec *spec = codec->spec;
1003 struct auto_pin_cfg *cfg = &spec->autocfg;
1004 int type, i, j, num_pins, old_pins;
1005 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1006 unsigned int location = get_defcfg_location(defcfg);
1009 old_pins = spec->multi_ios;
1013 num_pins = count_multiio_pins(codec, reference_pin);
1017 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1018 for (i = 0; i < cfg->num_inputs; i++) {
1019 struct nid_path *path;
1020 hda_nid_t nid = cfg->inputs[i].pin;
1023 if (cfg->inputs[i].type != type)
1025 if (!can_be_multiio_pin(codec, location, nid))
1027 for (j = 0; j < spec->multi_ios; j++) {
1028 if (nid == spec->multi_io[j].pin)
1031 if (j < spec->multi_ios)
1035 dac = get_dac_if_single(codec, nid);
1037 dac = look_for_dac(codec, nid, false);
1042 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
1047 print_nid_path("multiio", path);
1048 spec->multi_io[spec->multi_ios].pin = nid;
1049 spec->multi_io[spec->multi_ios].dac = dac;
1050 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1051 snd_hda_get_path_idx(codec, path);
1053 if (spec->multi_ios >= 2)
1059 badness = BAD_MULTI_IO;
1060 if (old_pins == spec->multi_ios) {
1062 return 1; /* nothing found */
1064 return badness; /* no badness if nothing found */
1066 if (!hardwired && spec->multi_ios < 2) {
1067 /* cancel newly assigned paths */
1068 spec->paths.used -= spec->multi_ios - old_pins;
1069 spec->multi_ios = old_pins;
1073 /* assign volume and mute controls */
1074 for (i = old_pins; i < spec->multi_ios; i++)
1075 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1076 spec->multi_io[i].dac);
1081 /* map DACs for all pins in the list if they are single connections */
1082 static bool map_singles(struct hda_codec *codec, int outs,
1083 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1085 struct hda_gen_spec *spec = codec->spec;
1088 for (i = 0; i < outs; i++) {
1089 struct nid_path *path;
1093 dac = get_dac_if_single(codec, pins[i]);
1096 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
1097 if (!path && !i && spec->mixer_nid)
1098 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
1102 print_nid_path("output", path);
1103 path->active = true;
1104 path_idx[i] = snd_hda_get_path_idx(codec, path);
1110 /* create a new path including aamix if available, and return its index */
1111 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1113 struct nid_path *path;
1115 path = snd_hda_get_path_from_idx(codec, path_idx);
1116 if (!path || !path->depth || path->with_aa_mix)
1118 path = snd_hda_add_new_path(codec, path->path[0],
1119 path->path[path->depth - 1],
1120 HDA_PARSE_ONLY_AAMIX);
1123 print_nid_path("output-aamix", path);
1124 path->active = false; /* unused as default */
1125 return snd_hda_get_path_idx(codec, path);
1128 /* fill in the dac_nids table from the parsed pin configuration */
1129 static int fill_and_eval_dacs(struct hda_codec *codec,
1130 bool fill_hardwired,
1131 bool fill_mio_first)
1133 struct hda_gen_spec *spec = codec->spec;
1134 struct auto_pin_cfg *cfg = &spec->autocfg;
1135 int i, err, badness;
1137 /* set num_dacs once to full for look_for_dac() */
1138 spec->multiout.num_dacs = cfg->line_outs;
1139 spec->multiout.dac_nids = spec->private_dac_nids;
1140 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1141 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1142 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1143 spec->multi_ios = 0;
1144 snd_array_free(&spec->paths);
1147 /* fill hard-wired DACs first */
1148 if (fill_hardwired) {
1151 mapped = map_singles(codec, cfg->line_outs,
1153 spec->private_dac_nids,
1155 mapped |= map_singles(codec, cfg->hp_outs,
1157 spec->multiout.hp_out_nid,
1159 mapped |= map_singles(codec, cfg->speaker_outs,
1161 spec->multiout.extra_out_nid,
1162 spec->speaker_paths);
1163 if (fill_mio_first && cfg->line_outs == 1 &&
1164 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1165 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1172 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1173 spec->private_dac_nids, spec->out_paths,
1176 /* re-count num_dacs and squash invalid entries */
1177 spec->multiout.num_dacs = 0;
1178 for (i = 0; i < cfg->line_outs; i++) {
1179 if (spec->private_dac_nids[i])
1180 spec->multiout.num_dacs++;
1182 memmove(spec->private_dac_nids + i,
1183 spec->private_dac_nids + i + 1,
1184 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1185 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1189 if (fill_mio_first &&
1190 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1191 /* try to fill multi-io first */
1192 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1195 /* we don't count badness at this stage yet */
1198 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1199 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1200 spec->multiout.hp_out_nid,
1202 &extra_out_badness);
1207 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1208 err = try_assign_dacs(codec, cfg->speaker_outs,
1210 spec->multiout.extra_out_nid,
1211 spec->speaker_paths,
1212 &extra_out_badness);
1217 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1218 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1224 if (spec->mixer_nid) {
1225 spec->aamix_out_paths[0] =
1226 check_aamix_out_path(codec, spec->out_paths[0]);
1227 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1228 spec->aamix_out_paths[1] =
1229 check_aamix_out_path(codec, spec->hp_paths[0]);
1230 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1231 spec->aamix_out_paths[2] =
1232 check_aamix_out_path(codec, spec->speaker_paths[0]);
1235 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1236 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1237 spec->multi_ios = 1; /* give badness */
1239 if (spec->multi_ios == 2) {
1240 for (i = 0; i < 2; i++)
1241 spec->private_dac_nids[spec->multiout.num_dacs++] =
1242 spec->multi_io[i].dac;
1243 spec->ext_channel_count = 2;
1244 } else if (spec->multi_ios) {
1245 spec->multi_ios = 0;
1246 badness += BAD_MULTI_IO;
1252 #define DEBUG_BADNESS
1254 #ifdef DEBUG_BADNESS
1255 #define debug_badness snd_printdd
1257 #define debug_badness(...)
1260 static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1262 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1263 cfg->line_out_pins[0], cfg->line_out_pins[1],
1264 cfg->line_out_pins[2], cfg->line_out_pins[3],
1265 spec->multiout.dac_nids[0],
1266 spec->multiout.dac_nids[1],
1267 spec->multiout.dac_nids[2],
1268 spec->multiout.dac_nids[3]);
1269 if (spec->multi_ios > 0)
1270 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1272 spec->multi_io[0].pin, spec->multi_io[1].pin,
1273 spec->multi_io[0].dac, spec->multi_io[1].dac);
1274 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1275 cfg->hp_pins[0], cfg->hp_pins[1],
1276 cfg->hp_pins[2], cfg->hp_pins[3],
1277 spec->multiout.hp_out_nid[0],
1278 spec->multiout.hp_out_nid[1],
1279 spec->multiout.hp_out_nid[2],
1280 spec->multiout.hp_out_nid[3]);
1281 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1282 cfg->speaker_pins[0], cfg->speaker_pins[1],
1283 cfg->speaker_pins[2], cfg->speaker_pins[3],
1284 spec->multiout.extra_out_nid[0],
1285 spec->multiout.extra_out_nid[1],
1286 spec->multiout.extra_out_nid[2],
1287 spec->multiout.extra_out_nid[3]);
1290 /* find all available DACs of the codec */
1291 static void fill_all_dac_nids(struct hda_codec *codec)
1293 struct hda_gen_spec *spec = codec->spec;
1295 hda_nid_t nid = codec->start_nid;
1297 spec->num_all_dacs = 0;
1298 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1299 for (i = 0; i < codec->num_nodes; i++, nid++) {
1300 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1302 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1303 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1306 spec->all_dacs[spec->num_all_dacs++] = nid;
1310 static int parse_output_paths(struct hda_codec *codec)
1312 struct hda_gen_spec *spec = codec->spec;
1313 struct auto_pin_cfg *cfg = &spec->autocfg;
1314 struct auto_pin_cfg *best_cfg;
1315 int best_badness = INT_MAX;
1317 bool fill_hardwired = true, fill_mio_first = true;
1318 bool best_wired = true, best_mio = true;
1319 bool hp_spk_swapped = false;
1321 fill_all_dac_nids(codec);
1323 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1329 badness = fill_and_eval_dacs(codec, fill_hardwired,
1335 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1336 cfg->line_out_type, fill_hardwired, fill_mio_first,
1338 debug_show_configs(spec, cfg);
1339 if (badness < best_badness) {
1340 best_badness = badness;
1342 best_wired = fill_hardwired;
1343 best_mio = fill_mio_first;
1347 fill_mio_first = !fill_mio_first;
1348 if (!fill_mio_first)
1350 fill_hardwired = !fill_hardwired;
1351 if (!fill_hardwired)
1355 hp_spk_swapped = true;
1356 if (cfg->speaker_outs > 0 &&
1357 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1358 cfg->hp_outs = cfg->line_outs;
1359 memcpy(cfg->hp_pins, cfg->line_out_pins,
1360 sizeof(cfg->hp_pins));
1361 cfg->line_outs = cfg->speaker_outs;
1362 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1363 sizeof(cfg->speaker_pins));
1364 cfg->speaker_outs = 0;
1365 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1366 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1367 fill_hardwired = true;
1370 if (cfg->hp_outs > 0 &&
1371 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1372 cfg->speaker_outs = cfg->line_outs;
1373 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1374 sizeof(cfg->speaker_pins));
1375 cfg->line_outs = cfg->hp_outs;
1376 memcpy(cfg->line_out_pins, cfg->hp_pins,
1377 sizeof(cfg->hp_pins));
1379 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1380 cfg->line_out_type = AUTO_PIN_HP_OUT;
1381 fill_hardwired = true;
1388 debug_badness("==> restoring best_cfg\n");
1390 fill_and_eval_dacs(codec, best_wired, best_mio);
1392 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1393 cfg->line_out_type, best_wired, best_mio);
1394 debug_show_configs(spec, cfg);
1396 if (cfg->line_out_pins[0]) {
1397 struct nid_path *path;
1398 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1400 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1407 /* add playback controls from the parsed DAC table */
1408 static int create_multi_out_ctls(struct hda_codec *codec,
1409 const struct auto_pin_cfg *cfg)
1411 struct hda_gen_spec *spec = codec->spec;
1412 int i, err, noutputs;
1414 noutputs = cfg->line_outs;
1415 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1416 noutputs += spec->multi_ios;
1418 for (i = 0; i < noutputs; i++) {
1422 struct nid_path *path;
1424 dac = spec->multiout.dac_nids[i];
1427 if (i >= cfg->line_outs) {
1429 name = channel_name[i];
1431 name = get_line_out_pfx(spec, i, true, &index);
1434 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1437 if (!name || !strcmp(name, "CLFE")) {
1439 err = add_vol_ctl(codec, "Center", 0, 1, path);
1442 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1445 err = add_sw_ctl(codec, "Center", 0, 1, path);
1448 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1452 err = add_stereo_vol(codec, name, index, path);
1455 err = add_stereo_sw(codec, name, index, path);
1463 static int create_extra_out(struct hda_codec *codec, int path_idx,
1464 const char *pfx, int cidx)
1466 struct nid_path *path;
1469 path = snd_hda_get_path_from_idx(codec, path_idx);
1472 err = add_stereo_vol(codec, pfx, cidx, path);
1475 err = add_stereo_sw(codec, pfx, cidx, path);
1481 /* add playback controls for speaker and HP outputs */
1482 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1483 const int *paths, const char *pfx)
1487 for (i = 0; i < num_pins; i++) {
1492 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1493 name = "Bass Speaker";
1494 else if (num_pins >= 3) {
1495 snprintf(tmp, sizeof(tmp), "%s %s",
1496 pfx, channel_name[i]);
1502 err = create_extra_out(codec, paths[i], name, idx);
1509 static int create_hp_out_ctls(struct hda_codec *codec)
1511 struct hda_gen_spec *spec = codec->spec;
1512 return create_extra_outs(codec, spec->autocfg.hp_outs,
1517 static int create_speaker_out_ctls(struct hda_codec *codec)
1519 struct hda_gen_spec *spec = codec->spec;
1520 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1521 spec->speaker_paths,
1526 * independent HP controls
1529 static int indep_hp_info(struct snd_kcontrol *kcontrol,
1530 struct snd_ctl_elem_info *uinfo)
1532 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1535 static int indep_hp_get(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 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1544 static int indep_hp_put(struct snd_kcontrol *kcontrol,
1545 struct snd_ctl_elem_value *ucontrol)
1547 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1548 struct hda_gen_spec *spec = codec->spec;
1549 unsigned int select = ucontrol->value.enumerated.item[0];
1552 mutex_lock(&spec->pcm_mutex);
1553 if (spec->active_streams) {
1558 if (spec->indep_hp_enabled != select) {
1559 spec->indep_hp_enabled = select;
1560 if (spec->indep_hp_enabled)
1561 spec->multiout.hp_out_nid[0] = 0;
1563 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1567 mutex_unlock(&spec->pcm_mutex);
1571 static const struct snd_kcontrol_new indep_hp_ctl = {
1572 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1573 .name = "Independent HP",
1574 .info = indep_hp_info,
1575 .get = indep_hp_get,
1576 .put = indep_hp_put,
1580 static int create_indep_hp_ctls(struct hda_codec *codec)
1582 struct hda_gen_spec *spec = codec->spec;
1584 if (!spec->indep_hp)
1586 if (!spec->multiout.hp_out_nid[0]) {
1591 spec->indep_hp_enabled = false;
1592 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1593 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1599 * channel mode enum control
1602 static int ch_mode_info(struct snd_kcontrol *kcontrol,
1603 struct snd_ctl_elem_info *uinfo)
1605 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1606 struct hda_gen_spec *spec = codec->spec;
1608 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1610 uinfo->value.enumerated.items = spec->multi_ios + 1;
1611 if (uinfo->value.enumerated.item > spec->multi_ios)
1612 uinfo->value.enumerated.item = spec->multi_ios;
1613 sprintf(uinfo->value.enumerated.name, "%dch",
1614 (uinfo->value.enumerated.item + 1) * 2);
1618 static int ch_mode_get(struct snd_kcontrol *kcontrol,
1619 struct snd_ctl_elem_value *ucontrol)
1621 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1622 struct hda_gen_spec *spec = codec->spec;
1623 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1627 static inline struct nid_path *
1628 get_multiio_path(struct hda_codec *codec, int idx)
1630 struct hda_gen_spec *spec = codec->spec;
1631 return snd_hda_get_path_from_idx(codec,
1632 spec->out_paths[spec->autocfg.line_outs + idx]);
1635 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1637 struct hda_gen_spec *spec = codec->spec;
1638 hda_nid_t nid = spec->multi_io[idx].pin;
1639 struct nid_path *path;
1641 path = get_multiio_path(codec, idx);
1645 if (path->active == output)
1649 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1650 snd_hda_activate_path(codec, path, true, true);
1651 set_pin_eapd(codec, nid, true);
1653 set_pin_eapd(codec, nid, false);
1654 snd_hda_activate_path(codec, path, false, true);
1655 snd_hda_set_pin_ctl_cache(codec, nid,
1656 spec->multi_io[idx].ctl_in);
1661 static int ch_mode_put(struct snd_kcontrol *kcontrol,
1662 struct snd_ctl_elem_value *ucontrol)
1664 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1665 struct hda_gen_spec *spec = codec->spec;
1668 ch = ucontrol->value.enumerated.item[0];
1669 if (ch < 0 || ch > spec->multi_ios)
1671 if (ch == (spec->ext_channel_count - 1) / 2)
1673 spec->ext_channel_count = (ch + 1) * 2;
1674 for (i = 0; i < spec->multi_ios; i++)
1675 set_multi_io(codec, i, i < ch);
1676 spec->multiout.max_channels = max(spec->ext_channel_count,
1677 spec->const_channel_count);
1678 if (spec->need_dac_fix)
1679 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1683 static const struct snd_kcontrol_new channel_mode_enum = {
1684 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1685 .name = "Channel Mode",
1686 .info = ch_mode_info,
1691 static int create_multi_channel_mode(struct hda_codec *codec)
1693 struct hda_gen_spec *spec = codec->spec;
1695 if (spec->multi_ios > 0) {
1696 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
1703 * aamix loopback enable/disable switch
1706 #define loopback_mixing_info indep_hp_info
1708 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1709 struct snd_ctl_elem_value *ucontrol)
1711 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1712 struct hda_gen_spec *spec = codec->spec;
1713 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1717 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1718 int nomix_path_idx, int mix_path_idx)
1720 struct nid_path *nomix_path, *mix_path;
1722 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1723 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1724 if (!nomix_path || !mix_path)
1727 snd_hda_activate_path(codec, nomix_path, false, true);
1728 snd_hda_activate_path(codec, mix_path, true, true);
1730 snd_hda_activate_path(codec, mix_path, false, true);
1731 snd_hda_activate_path(codec, nomix_path, true, true);
1735 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1736 struct snd_ctl_elem_value *ucontrol)
1738 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1739 struct hda_gen_spec *spec = codec->spec;
1740 unsigned int val = ucontrol->value.enumerated.item[0];
1742 if (val == spec->aamix_mode)
1744 spec->aamix_mode = val;
1745 update_aamix_paths(codec, val, spec->out_paths[0],
1746 spec->aamix_out_paths[0]);
1747 update_aamix_paths(codec, val, spec->hp_paths[0],
1748 spec->aamix_out_paths[1]);
1749 update_aamix_paths(codec, val, spec->speaker_paths[0],
1750 spec->aamix_out_paths[2]);
1754 static const struct snd_kcontrol_new loopback_mixing_enum = {
1755 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1756 .name = "Loopback Mixing",
1757 .info = loopback_mixing_info,
1758 .get = loopback_mixing_get,
1759 .put = loopback_mixing_put,
1762 static int create_loopback_mixing_ctl(struct hda_codec *codec)
1764 struct hda_gen_spec *spec = codec->spec;
1766 if (!spec->mixer_nid)
1768 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1769 spec->aamix_out_paths[2]))
1771 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1777 * shared headphone/mic handling
1780 static void call_update_outputs(struct hda_codec *codec);
1782 /* for shared I/O, change the pin-control accordingly */
1783 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1785 struct hda_gen_spec *spec = codec->spec;
1787 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1788 /* NOTE: this assumes that there are only two inputs, the
1789 * first is the real internal mic and the second is HP/mic jack.
1792 val = snd_hda_get_default_vref(codec, pin);
1794 /* This pin does not have vref caps - let's enable vref on pin 0x18
1795 instead, as suggested by Realtek */
1796 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1797 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1798 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1799 if (vref_val != AC_PINCTL_VREF_HIZ)
1800 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1801 PIN_IN | (set_as_mic ? vref_val : 0));
1804 val = set_as_mic ? val | PIN_IN : PIN_HP;
1805 snd_hda_set_pin_ctl_cache(codec, pin, val);
1807 spec->automute_speaker = !set_as_mic;
1808 call_update_outputs(codec);
1811 /* create a shared input with the headphone out */
1812 static int create_shared_input(struct hda_codec *codec)
1814 struct hda_gen_spec *spec = codec->spec;
1815 struct auto_pin_cfg *cfg = &spec->autocfg;
1816 unsigned int defcfg;
1819 /* only one internal input pin? */
1820 if (cfg->num_inputs != 1)
1822 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1823 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1826 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1827 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1828 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1829 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1831 return 0; /* both not available */
1833 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1834 return 0; /* no input */
1836 cfg->inputs[1].pin = nid;
1837 cfg->inputs[1].type = AUTO_PIN_MIC;
1838 cfg->num_inputs = 2;
1839 spec->shared_mic_hp = 1;
1840 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1850 /* add the powersave loopback-list entry */
1851 static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1853 struct hda_amp_list *list;
1855 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1857 list = spec->loopback_list + spec->num_loopbacks;
1859 list->dir = HDA_INPUT;
1861 spec->num_loopbacks++;
1862 spec->loopback.amplist = spec->loopback_list;
1865 #define add_loopback_list(spec, mix, idx) /* NOP */
1868 /* create input playback/capture controls for the given pin */
1869 static int new_analog_input(struct hda_codec *codec, int input_idx,
1870 hda_nid_t pin, const char *ctlname, int ctlidx,
1873 struct hda_gen_spec *spec = codec->spec;
1874 struct nid_path *path;
1878 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1879 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1880 return 0; /* no need for analog loopback */
1882 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
1885 print_nid_path("loopback", path);
1886 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
1888 idx = path->idx[path->depth - 1];
1889 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1890 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1891 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
1894 path->ctls[NID_PATH_VOL_CTL] = val;
1897 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1898 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1899 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
1902 path->ctls[NID_PATH_MUTE_CTL] = val;
1905 path->active = true;
1906 add_loopback_list(spec, mix_nid, idx);
1910 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
1912 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1913 return (pincap & AC_PINCAP_IN) != 0;
1916 /* Parse the codec tree and retrieve ADCs */
1917 static int fill_adc_nids(struct hda_codec *codec)
1919 struct hda_gen_spec *spec = codec->spec;
1921 hda_nid_t *adc_nids = spec->adc_nids;
1922 int max_nums = ARRAY_SIZE(spec->adc_nids);
1925 nid = codec->start_nid;
1926 for (i = 0; i < codec->num_nodes; i++, nid++) {
1927 unsigned int caps = get_wcaps(codec, nid);
1928 int type = get_wcaps_type(caps);
1930 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1932 adc_nids[nums] = nid;
1933 if (++nums >= max_nums)
1936 spec->num_adc_nids = nums;
1940 /* filter out invalid adc_nids that don't give all active input pins;
1941 * if needed, check whether dynamic ADC-switching is available
1943 static int check_dyn_adc_switch(struct hda_codec *codec)
1945 struct hda_gen_spec *spec = codec->spec;
1946 struct hda_input_mux *imux = &spec->input_mux;
1947 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1953 for (n = 0; n < spec->num_adc_nids; n++) {
1954 adc = spec->adc_nids[n];
1955 for (i = 0; i < imux->num_items; i++) {
1956 pin = spec->imux_pins[i];
1957 if (!is_reachable_path(codec, pin, adc))
1960 if (i >= imux->num_items)
1961 adc_nids[nums++] = adc;
1965 if (spec->shared_mic_hp) {
1966 spec->shared_mic_hp = 0;
1967 imux->num_items = 1;
1971 /* check whether ADC-switch is possible */
1972 for (i = 0; i < imux->num_items; i++) {
1973 pin = spec->imux_pins[i];
1974 for (n = 0; n < spec->num_adc_nids; n++) {
1975 adc = spec->adc_nids[n];
1976 if (is_reachable_path(codec, pin, adc)) {
1977 spec->dyn_adc_idx[i] = n;
1983 snd_printdd("hda-codec: enabling ADC switching\n");
1984 spec->dyn_adc_switch = 1;
1985 } else if (nums != spec->num_adc_nids) {
1986 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1987 spec->num_adc_nids = nums;
1990 if (imux->num_items == 1 || spec->shared_mic_hp) {
1991 snd_printdd("hda-codec: reducing to a single ADC\n");
1992 spec->num_adc_nids = 1; /* reduce to a single ADC */
1995 /* single index for individual volumes ctls */
1996 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1997 spec->num_adc_nids = 1;
2003 * create playback/capture controls for input pins
2005 static int create_input_ctls(struct hda_codec *codec)
2007 struct hda_gen_spec *spec = codec->spec;
2008 const struct auto_pin_cfg *cfg = &spec->autocfg;
2009 hda_nid_t mixer = spec->mixer_nid;
2010 struct hda_input_mux *imux = &spec->input_mux;
2012 int i, c, err, type_idx = 0;
2013 const char *prev_label = NULL;
2015 num_adcs = fill_adc_nids(codec);
2019 for (i = 0; i < cfg->num_inputs; i++) {
2024 pin = cfg->inputs[i].pin;
2025 if (!is_input_pin(codec, pin))
2028 label = hda_get_autocfg_input_label(codec, cfg, i);
2029 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2030 label = "Headphone Mic";
2031 if (prev_label && !strcmp(label, prev_label))
2038 if (is_reachable_path(codec, pin, mixer)) {
2039 err = new_analog_input(codec, i, pin,
2040 label, type_idx, mixer);
2047 for (c = 0; c < num_adcs; c++) {
2048 struct nid_path *path;
2049 hda_nid_t adc = spec->adc_nids[c];
2051 if (!is_reachable_path(codec, pin, adc))
2053 path = snd_array_new(&spec->paths);
2056 memset(path, 0, sizeof(*path));
2057 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
2059 "invalid input path 0x%x -> 0x%x\n",
2064 print_nid_path("input", path);
2067 spec->imux_pins[imux->num_items] = pin;
2068 snd_hda_add_imux_item(imux, label,
2069 imux->num_items, NULL);
2083 /* get the ADC NID corresponding to the given index */
2084 static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
2086 struct hda_gen_spec *spec = codec->spec;
2087 if (spec->dyn_adc_switch)
2088 adc_idx = spec->dyn_adc_idx[imux_idx];
2089 return spec->adc_nids[adc_idx];
2092 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2095 static int mux_enum_info(struct snd_kcontrol *kcontrol,
2096 struct snd_ctl_elem_info *uinfo)
2098 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2099 struct hda_gen_spec *spec = codec->spec;
2100 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2103 static int mux_enum_get(struct snd_kcontrol *kcontrol,
2104 struct snd_ctl_elem_value *ucontrol)
2106 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2107 struct hda_gen_spec *spec = codec->spec;
2108 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2110 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2114 static int mux_enum_put(struct snd_kcontrol *kcontrol,
2115 struct snd_ctl_elem_value *ucontrol)
2117 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2118 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2119 return mux_select(codec, adc_idx,
2120 ucontrol->value.enumerated.item[0]);
2123 static const struct snd_kcontrol_new cap_src_temp = {
2124 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2125 .name = "Input Source",
2126 .info = mux_enum_info,
2127 .get = mux_enum_get,
2128 .put = mux_enum_put,
2132 * capture volume and capture switch ctls
2135 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2136 struct snd_ctl_elem_value *ucontrol);
2138 /* call the given amp update function for all amps in the imux list at once */
2139 static int cap_put_caller(struct snd_kcontrol *kcontrol,
2140 struct snd_ctl_elem_value *ucontrol,
2141 put_call_t func, int type)
2143 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2144 struct hda_gen_spec *spec = codec->spec;
2145 const struct hda_input_mux *imux;
2146 struct nid_path *path;
2147 int i, adc_idx, err = 0;
2149 imux = &spec->input_mux;
2150 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2151 mutex_lock(&codec->control_mutex);
2152 /* we use the cache-only update at first since multiple input paths
2153 * may shared the same amp; by updating only caches, the redundant
2154 * writes to hardware can be reduced.
2156 codec->cached_write = 1;
2157 for (i = 0; i < imux->num_items; i++) {
2158 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2159 get_adc_nid(codec, adc_idx, i));
2160 if (!path->ctls[type])
2162 kcontrol->private_value = path->ctls[type];
2163 err = func(kcontrol, ucontrol);
2168 codec->cached_write = 0;
2169 mutex_unlock(&codec->control_mutex);
2170 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
2171 if (err >= 0 && spec->cap_sync_hook)
2172 spec->cap_sync_hook(codec);
2176 /* capture volume ctl callbacks */
2177 #define cap_vol_info snd_hda_mixer_amp_volume_info
2178 #define cap_vol_get snd_hda_mixer_amp_volume_get
2179 #define cap_vol_tlv snd_hda_mixer_amp_tlv
2181 static int cap_vol_put(struct snd_kcontrol *kcontrol,
2182 struct snd_ctl_elem_value *ucontrol)
2184 return cap_put_caller(kcontrol, ucontrol,
2185 snd_hda_mixer_amp_volume_put,
2189 static const struct snd_kcontrol_new cap_vol_temp = {
2190 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2191 .name = "Capture Volume",
2192 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2193 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2194 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2195 .info = cap_vol_info,
2198 .tlv = { .c = cap_vol_tlv },
2201 /* capture switch ctl callbacks */
2202 #define cap_sw_info snd_ctl_boolean_stereo_info
2203 #define cap_sw_get snd_hda_mixer_amp_switch_get
2205 static int cap_sw_put(struct snd_kcontrol *kcontrol,
2206 struct snd_ctl_elem_value *ucontrol)
2208 return cap_put_caller(kcontrol, ucontrol,
2209 snd_hda_mixer_amp_switch_put,
2213 static const struct snd_kcontrol_new cap_sw_temp = {
2214 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2215 .name = "Capture Switch",
2216 .info = cap_sw_info,
2221 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2226 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2227 for (depth = 0; depth < 3; depth++) {
2228 if (depth >= path->depth)
2230 i = path->depth - depth - 1;
2231 nid = path->path[i];
2232 if (!path->ctls[NID_PATH_VOL_CTL]) {
2233 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2234 path->ctls[NID_PATH_VOL_CTL] =
2235 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2236 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2237 int idx = path->idx[i];
2238 if (!depth && codec->single_adc_amp)
2240 path->ctls[NID_PATH_VOL_CTL] =
2241 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2244 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2245 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2246 path->ctls[NID_PATH_MUTE_CTL] =
2247 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2248 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2249 int idx = path->idx[i];
2250 if (!depth && codec->single_adc_amp)
2252 path->ctls[NID_PATH_MUTE_CTL] =
2253 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2260 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2262 struct hda_gen_spec *spec = codec->spec;
2263 struct auto_pin_cfg *cfg = &spec->autocfg;
2267 if (!spec->inv_dmic_split)
2269 for (i = 0; i < cfg->num_inputs; i++) {
2270 if (cfg->inputs[i].pin != nid)
2272 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2274 val = snd_hda_codec_get_pincfg(codec, nid);
2275 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2280 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2281 int idx, bool is_switch, unsigned int ctl,
2284 struct hda_gen_spec *spec = codec->spec;
2286 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2287 const char *sfx = is_switch ? "Switch" : "Volume";
2288 unsigned int chs = inv_dmic ? 1 : 3;
2295 snprintf(tmpname, sizeof(tmpname),
2296 "%s Capture %s", label, sfx);
2298 snprintf(tmpname, sizeof(tmpname),
2300 err = add_control(spec, type, tmpname, idx,
2301 amp_val_replace_channels(ctl, chs));
2302 if (err < 0 || !inv_dmic)
2305 /* Make independent right kcontrol */
2307 snprintf(tmpname, sizeof(tmpname),
2308 "Inverted %s Capture %s", label, sfx);
2310 snprintf(tmpname, sizeof(tmpname),
2311 "Inverted Capture %s", sfx);
2312 return add_control(spec, type, tmpname, idx,
2313 amp_val_replace_channels(ctl, 2));
2316 /* create single (and simple) capture volume and switch controls */
2317 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2318 unsigned int vol_ctl, unsigned int sw_ctl,
2322 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2325 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2331 /* create bound capture volume and switch controls */
2332 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2333 unsigned int vol_ctl, unsigned int sw_ctl)
2335 struct hda_gen_spec *spec = codec->spec;
2336 struct snd_kcontrol_new *knew;
2339 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
2343 knew->private_value = vol_ctl;
2344 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2347 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
2351 knew->private_value = sw_ctl;
2352 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2357 /* return the vol ctl when used first in the imux list */
2358 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2360 struct hda_gen_spec *spec = codec->spec;
2361 struct nid_path *path;
2365 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2366 get_adc_nid(codec, 0, idx));
2369 ctl = path->ctls[type];
2372 for (i = 0; i < idx - 1; i++) {
2373 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2374 get_adc_nid(codec, 0, i));
2375 if (path && path->ctls[type] == ctl)
2381 /* create individual capture volume and switch controls per input */
2382 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2384 struct hda_gen_spec *spec = codec->spec;
2385 struct hda_input_mux *imux = &spec->input_mux;
2386 int i, err, type, type_idx = 0;
2387 const char *prev_label = NULL;
2389 for (i = 0; i < imux->num_items; i++) {
2392 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2393 if (prev_label && !strcmp(label, prev_label))
2398 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2400 for (type = 0; type < 2; type++) {
2401 err = add_single_cap_ctl(codec, label, type_idx, type,
2402 get_first_cap_ctl(codec, i, type),
2411 static int create_capture_mixers(struct hda_codec *codec)
2413 struct hda_gen_spec *spec = codec->spec;
2414 struct hda_input_mux *imux = &spec->input_mux;
2415 int i, n, nums, err;
2417 if (spec->dyn_adc_switch)
2420 nums = spec->num_adc_nids;
2422 if (!spec->auto_mic && imux->num_items > 1) {
2423 struct snd_kcontrol_new *knew;
2425 name = nums > 1 ? "Input Source" : "Capture Source";
2426 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
2432 for (n = 0; n < nums; n++) {
2434 bool inv_dmic = false;
2438 for (i = 0; i < imux->num_items; i++) {
2439 struct nid_path *path;
2440 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2441 get_adc_nid(codec, n, i));
2444 parse_capvol_in_path(codec, path);
2446 vol = path->ctls[NID_PATH_VOL_CTL];
2447 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2450 sw = path->ctls[NID_PATH_MUTE_CTL];
2451 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2453 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2458 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2460 else if (!spec->multi_cap_vol)
2461 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2463 err = create_multi_cap_vol_ctl(codec);
2472 * add mic boosts if needed
2474 static int parse_mic_boost(struct hda_codec *codec)
2476 struct hda_gen_spec *spec = codec->spec;
2477 struct auto_pin_cfg *cfg = &spec->autocfg;
2481 const char *prev_label = NULL;
2483 for (i = 0; i < cfg->num_inputs; i++) {
2484 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2486 nid = cfg->inputs[i].pin;
2487 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2489 char boost_label[44];
2490 struct nid_path *path;
2493 label = hda_get_autocfg_input_label(codec, cfg, i);
2494 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2495 label = "Headphone Mic";
2496 if (prev_label && !strcmp(label, prev_label))
2502 snprintf(boost_label, sizeof(boost_label),
2503 "%s Boost Volume", label);
2504 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2505 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2506 boost_label, type_idx, val);
2510 path = snd_hda_get_nid_path(codec, nid, 0);
2512 path->ctls[NID_PATH_BOOST_CTL] = val;
2519 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2521 static void parse_digital(struct hda_codec *codec)
2523 struct hda_gen_spec *spec = codec->spec;
2524 struct nid_path *path;
2528 /* support multiple SPDIFs; the secondary is set up as a slave */
2530 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2531 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2532 dig_nid = look_for_dac(codec, pin, true);
2535 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
2538 print_nid_path("digout", path);
2539 path->active = true;
2540 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
2542 spec->multiout.dig_out_nid = dig_nid;
2543 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2545 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2546 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2548 spec->slave_dig_outs[nums - 1] = dig_nid;
2553 if (spec->autocfg.dig_in_pin) {
2554 dig_nid = codec->start_nid;
2555 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2556 unsigned int wcaps = get_wcaps(codec, dig_nid);
2557 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2559 if (!(wcaps & AC_WCAP_DIGITAL))
2561 path = snd_hda_add_new_path(codec,
2562 spec->autocfg.dig_in_pin,
2563 dig_nid, HDA_PARSE_ALL);
2565 print_nid_path("digin", path);
2566 path->active = true;
2567 spec->dig_in_nid = dig_nid;
2568 spec->digin_path = snd_hda_get_path_idx(codec, path);
2577 * input MUX handling
2580 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2582 /* select the given imux item; either unmute exclusively or select the route */
2583 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2586 struct hda_gen_spec *spec = codec->spec;
2587 const struct hda_input_mux *imux;
2588 struct nid_path *path;
2590 imux = &spec->input_mux;
2591 if (!imux->num_items)
2594 if (idx >= imux->num_items)
2595 idx = imux->num_items - 1;
2596 if (spec->cur_mux[adc_idx] == idx)
2599 path = snd_hda_get_nid_path(codec,
2600 spec->imux_pins[spec->cur_mux[adc_idx]],
2601 spec->adc_nids[adc_idx]);
2605 snd_hda_activate_path(codec, path, false, false);
2607 spec->cur_mux[adc_idx] = idx;
2609 if (spec->shared_mic_hp)
2610 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2612 if (spec->dyn_adc_switch)
2613 dyn_adc_pcm_resetup(codec, idx);
2615 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2616 get_adc_nid(codec, adc_idx, idx));
2621 snd_hda_activate_path(codec, path, true, false);
2622 if (spec->cap_sync_hook)
2623 spec->cap_sync_hook(codec);
2629 * Jack detections for HP auto-mute and mic-switch
2632 /* check each pin in the given array; returns true if any of them is plugged */
2633 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2637 for (i = 0; i < num_pins; i++) {
2638 hda_nid_t nid = pins[i];
2641 present |= snd_hda_jack_detect(codec, nid);
2646 /* standard HP/line-out auto-mute helper */
2647 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2648 bool mute, bool hp_out)
2650 struct hda_gen_spec *spec = codec->spec;
2651 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2654 for (i = 0; i < num_pins; i++) {
2655 hda_nid_t nid = pins[i];
2659 /* don't reset VREF value in case it's controlling
2660 * the amp (see alc861_fixup_asus_amp_vref_0f())
2662 if (spec->keep_vref_in_automute) {
2663 val = snd_hda_codec_read(codec, nid, 0,
2664 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2669 snd_hda_set_pin_ctl_cache(codec, nid, val);
2670 set_pin_eapd(codec, nid, !mute);
2674 /* Toggle outputs muting */
2675 void snd_hda_gen_update_outputs(struct hda_codec *codec)
2677 struct hda_gen_spec *spec = codec->spec;
2680 /* Control HP pins/amps depending on master_mute state;
2681 * in general, HP pins/amps control should be enabled in all cases,
2682 * but currently set only for master_mute, just to be safe
2684 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2685 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2686 spec->autocfg.hp_pins, spec->master_mute, true);
2688 if (!spec->automute_speaker)
2691 on = spec->hp_jack_present | spec->line_jack_present;
2692 on |= spec->master_mute;
2693 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2694 spec->autocfg.speaker_pins, on, false);
2696 /* toggle line-out mutes if needed, too */
2697 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2698 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2699 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2701 if (!spec->automute_lo)
2704 on = spec->hp_jack_present;
2705 on |= spec->master_mute;
2706 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2707 spec->autocfg.line_out_pins, on, false);
2709 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
2711 static void call_update_outputs(struct hda_codec *codec)
2713 struct hda_gen_spec *spec = codec->spec;
2714 if (spec->automute_hook)
2715 spec->automute_hook(codec);
2717 snd_hda_gen_update_outputs(codec);
2720 /* standard HP-automute helper */
2721 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2723 struct hda_gen_spec *spec = codec->spec;
2725 spec->hp_jack_present =
2726 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2727 spec->autocfg.hp_pins);
2728 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2730 call_update_outputs(codec);
2732 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
2734 /* standard line-out-automute helper */
2735 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2737 struct hda_gen_spec *spec = codec->spec;
2739 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2741 /* check LO jack only when it's different from HP */
2742 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2745 spec->line_jack_present =
2746 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2747 spec->autocfg.line_out_pins);
2748 if (!spec->automute_speaker || !spec->detect_lo)
2750 call_update_outputs(codec);
2752 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
2754 /* standard mic auto-switch helper */
2755 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
2757 struct hda_gen_spec *spec = codec->spec;
2760 if (!spec->auto_mic)
2763 for (i = spec->am_num_entries - 1; i > 0; i--) {
2764 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2765 mux_select(codec, 0, spec->am_entry[i].idx);
2769 mux_select(codec, 0, spec->am_entry[0].idx);
2771 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
2774 * Auto-Mute mode mixer enum support
2776 static int automute_mode_info(struct snd_kcontrol *kcontrol,
2777 struct snd_ctl_elem_info *uinfo)
2779 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2780 struct hda_gen_spec *spec = codec->spec;
2781 static const char * const texts3[] = {
2782 "Disabled", "Speaker Only", "Line Out+Speaker"
2785 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2786 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2787 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2790 static int automute_mode_get(struct snd_kcontrol *kcontrol,
2791 struct snd_ctl_elem_value *ucontrol)
2793 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2794 struct hda_gen_spec *spec = codec->spec;
2795 unsigned int val = 0;
2796 if (spec->automute_speaker)
2798 if (spec->automute_lo)
2801 ucontrol->value.enumerated.item[0] = val;
2805 static int automute_mode_put(struct snd_kcontrol *kcontrol,
2806 struct snd_ctl_elem_value *ucontrol)
2808 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2809 struct hda_gen_spec *spec = codec->spec;
2811 switch (ucontrol->value.enumerated.item[0]) {
2813 if (!spec->automute_speaker && !spec->automute_lo)
2815 spec->automute_speaker = 0;
2816 spec->automute_lo = 0;
2819 if (spec->automute_speaker_possible) {
2820 if (!spec->automute_lo && spec->automute_speaker)
2822 spec->automute_speaker = 1;
2823 spec->automute_lo = 0;
2824 } else if (spec->automute_lo_possible) {
2825 if (spec->automute_lo)
2827 spec->automute_lo = 1;
2832 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2834 if (spec->automute_speaker && spec->automute_lo)
2836 spec->automute_speaker = 1;
2837 spec->automute_lo = 1;
2842 call_update_outputs(codec);
2846 static const struct snd_kcontrol_new automute_mode_enum = {
2847 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2848 .name = "Auto-Mute Mode",
2849 .info = automute_mode_info,
2850 .get = automute_mode_get,
2851 .put = automute_mode_put,
2854 static int add_automute_mode_enum(struct hda_codec *codec)
2856 struct hda_gen_spec *spec = codec->spec;
2858 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
2864 * Check the availability of HP/line-out auto-mute;
2865 * Set up appropriately if really supported
2867 static int check_auto_mute_availability(struct hda_codec *codec)
2869 struct hda_gen_spec *spec = codec->spec;
2870 struct auto_pin_cfg *cfg = &spec->autocfg;
2874 if (cfg->hp_pins[0])
2876 if (cfg->line_out_pins[0])
2878 if (cfg->speaker_pins[0])
2880 if (present < 2) /* need two different output types */
2883 if (!cfg->speaker_pins[0] &&
2884 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2885 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2886 sizeof(cfg->speaker_pins));
2887 cfg->speaker_outs = cfg->line_outs;
2890 if (!cfg->hp_pins[0] &&
2891 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2892 memcpy(cfg->hp_pins, cfg->line_out_pins,
2893 sizeof(cfg->hp_pins));
2894 cfg->hp_outs = cfg->line_outs;
2897 for (i = 0; i < cfg->hp_outs; i++) {
2898 hda_nid_t nid = cfg->hp_pins[i];
2899 if (!is_jack_detectable(codec, nid))
2901 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2903 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
2904 spec->hp_automute_hook ?
2905 spec->hp_automute_hook :
2906 snd_hda_gen_hp_automute);
2907 spec->detect_hp = 1;
2910 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2911 if (cfg->speaker_outs)
2912 for (i = 0; i < cfg->line_outs; i++) {
2913 hda_nid_t nid = cfg->line_out_pins[i];
2914 if (!is_jack_detectable(codec, nid))
2916 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2917 snd_hda_jack_detect_enable_callback(codec, nid,
2918 HDA_GEN_FRONT_EVENT,
2919 spec->line_automute_hook ?
2920 spec->line_automute_hook :
2921 snd_hda_gen_line_automute);
2922 spec->detect_lo = 1;
2924 spec->automute_lo_possible = spec->detect_hp;
2927 spec->automute_speaker_possible = cfg->speaker_outs &&
2928 (spec->detect_hp || spec->detect_lo);
2930 spec->automute_lo = spec->automute_lo_possible;
2931 spec->automute_speaker = spec->automute_speaker_possible;
2933 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2934 /* create a control for automute mode */
2935 err = add_automute_mode_enum(codec);
2942 /* return the position of NID in the list, or -1 if not found */
2943 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2946 for (i = 0; i < nums; i++)
2952 /* check whether all auto-mic pins are valid; setup indices if OK */
2953 static bool auto_mic_check_imux(struct hda_codec *codec)
2955 struct hda_gen_spec *spec = codec->spec;
2956 const struct hda_input_mux *imux;
2959 imux = &spec->input_mux;
2960 for (i = 0; i < spec->am_num_entries; i++) {
2961 spec->am_entry[i].idx =
2962 find_idx_in_nid_list(spec->am_entry[i].pin,
2963 spec->imux_pins, imux->num_items);
2964 if (spec->am_entry[i].idx < 0)
2965 return false; /* no corresponding imux */
2968 /* we don't need the jack detection for the first pin */
2969 for (i = 1; i < spec->am_num_entries; i++)
2970 snd_hda_jack_detect_enable_callback(codec,
2971 spec->am_entry[i].pin,
2973 spec->mic_autoswitch_hook ?
2974 spec->mic_autoswitch_hook :
2975 snd_hda_gen_mic_autoswitch);
2979 static int compare_attr(const void *ap, const void *bp)
2981 const struct automic_entry *a = ap;
2982 const struct automic_entry *b = bp;
2983 return (int)(a->attr - b->attr);
2987 * Check the availability of auto-mic switch;
2988 * Set up if really supported
2990 static int check_auto_mic_availability(struct hda_codec *codec)
2992 struct hda_gen_spec *spec = codec->spec;
2993 struct auto_pin_cfg *cfg = &spec->autocfg;
2999 for (i = 0; i < cfg->num_inputs; i++) {
3000 hda_nid_t nid = cfg->inputs[i].pin;
3002 attr = snd_hda_codec_get_pincfg(codec, nid);
3003 attr = snd_hda_get_input_pin_attr(attr);
3004 if (types & (1 << attr))
3005 return 0; /* already occupied */
3007 case INPUT_PIN_ATTR_INT:
3008 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3009 return 0; /* invalid type */
3011 case INPUT_PIN_ATTR_UNUSED:
3012 return 0; /* invalid entry */
3014 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3015 return 0; /* invalid type */
3016 if (!spec->line_in_auto_switch &&
3017 cfg->inputs[i].type != AUTO_PIN_MIC)
3018 return 0; /* only mic is allowed */
3019 if (!is_jack_detectable(codec, nid))
3020 return 0; /* no unsol support */
3023 if (num_pins >= MAX_AUTO_MIC_PINS)
3025 types |= (1 << attr);
3026 spec->am_entry[num_pins].pin = nid;
3027 spec->am_entry[num_pins].attr = attr;
3034 spec->am_num_entries = num_pins;
3035 /* sort the am_entry in the order of attr so that the pin with a
3036 * higher attr will be selected when the jack is plugged.
3038 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3039 compare_attr, NULL);
3041 if (!auto_mic_check_imux(codec))
3045 spec->num_adc_nids = 1;
3046 spec->cur_mux[0] = spec->am_entry[0].idx;
3047 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3048 spec->am_entry[0].pin,
3049 spec->am_entry[1].pin,
3050 spec->am_entry[2].pin);
3057 * Parse the given BIOS configuration and set up the hda_gen_spec
3059 * return 1 if successful, 0 if the proper config is not found,
3060 * or a negative error code
3062 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
3063 struct auto_pin_cfg *cfg)
3065 struct hda_gen_spec *spec = codec->spec;
3068 if (cfg != &spec->autocfg) {
3069 spec->autocfg = *cfg;
3070 cfg = &spec->autocfg;
3073 if (!cfg->line_outs) {
3074 if (cfg->dig_outs || cfg->dig_in_pin) {
3075 spec->multiout.max_channels = 2;
3076 spec->no_analog = 1;
3079 return 0; /* can't find valid BIOS pin config */
3082 if (!spec->no_primary_hp &&
3083 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3084 cfg->line_outs <= cfg->hp_outs) {
3085 /* use HP as primary out */
3086 cfg->speaker_outs = cfg->line_outs;
3087 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3088 sizeof(cfg->speaker_pins));
3089 cfg->line_outs = cfg->hp_outs;
3090 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3092 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3093 cfg->line_out_type = AUTO_PIN_HP_OUT;
3096 err = parse_output_paths(codec);
3099 err = create_multi_channel_mode(codec);
3102 err = create_multi_out_ctls(codec, cfg);
3105 err = create_hp_out_ctls(codec);
3108 err = create_speaker_out_ctls(codec);
3111 err = create_indep_hp_ctls(codec);
3114 err = create_loopback_mixing_ctl(codec);
3117 err = create_shared_input(codec);
3120 err = create_input_ctls(codec);
3124 /* check the multiple speaker pins */
3125 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
3126 spec->const_channel_count = cfg->line_outs * 2;
3128 spec->const_channel_count = cfg->speaker_outs * 2;
3130 if (spec->multi_ios > 0)
3131 spec->multiout.max_channels = max(spec->ext_channel_count,
3132 spec->const_channel_count);
3134 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3136 err = check_auto_mute_availability(codec);
3140 err = check_dyn_adc_switch(codec);
3144 if (!spec->shared_mic_hp) {
3145 err = check_auto_mic_availability(codec);
3150 err = create_capture_mixers(codec);
3154 err = parse_mic_boost(codec);
3159 parse_digital(codec);
3163 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
3167 * Build control elements
3170 /* slave controls for virtual master */
3171 static const char * const slave_pfxs[] = {
3172 "Front", "Surround", "Center", "LFE", "Side",
3173 "Headphone", "Speaker", "Mono", "Line Out",
3174 "CLFE", "Bass Speaker", "PCM",
3175 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3176 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3181 int snd_hda_gen_build_controls(struct hda_codec *codec)
3183 struct hda_gen_spec *spec = codec->spec;
3186 if (spec->kctls.used) {
3187 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3192 if (spec->multiout.dig_out_nid) {
3193 err = snd_hda_create_dig_out_ctls(codec,
3194 spec->multiout.dig_out_nid,
3195 spec->multiout.dig_out_nid,
3196 spec->pcm_rec[1].pcm_type);
3199 if (!spec->no_analog) {
3200 err = snd_hda_create_spdif_share_sw(codec,
3204 spec->multiout.share_spdif = 1;
3207 if (spec->dig_in_nid) {
3208 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3213 /* if we have no master control, let's create it */
3214 if (!spec->no_analog &&
3215 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3216 unsigned int vmaster_tlv[4];
3217 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3218 HDA_OUTPUT, vmaster_tlv);
3219 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3220 vmaster_tlv, slave_pfxs,
3225 if (!spec->no_analog &&
3226 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3227 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3230 true, &spec->vmaster_mute.sw_kctl);
3233 if (spec->vmaster_mute.hook)
3234 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3235 spec->vmaster_mute_enum);
3238 free_kctls(spec); /* no longer needed */
3240 if (spec->shared_mic_hp) {
3242 int nid = spec->autocfg.inputs[1].pin;
3243 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3246 err = snd_hda_jack_detect_enable(codec, nid, 0);
3251 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3257 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3265 * Analog playback callbacks
3267 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3268 struct hda_codec *codec,
3269 struct snd_pcm_substream *substream)
3271 struct hda_gen_spec *spec = codec->spec;
3274 mutex_lock(&spec->pcm_mutex);
3275 err = snd_hda_multi_out_analog_open(codec,
3276 &spec->multiout, substream,
3279 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3280 mutex_unlock(&spec->pcm_mutex);
3284 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3285 struct hda_codec *codec,
3286 unsigned int stream_tag,
3287 unsigned int format,
3288 struct snd_pcm_substream *substream)
3290 struct hda_gen_spec *spec = codec->spec;
3291 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3292 stream_tag, format, substream);
3295 static int playback_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;
3300 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3303 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3304 struct hda_codec *codec,
3305 struct snd_pcm_substream *substream)
3307 struct hda_gen_spec *spec = codec->spec;
3308 mutex_lock(&spec->pcm_mutex);
3309 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3310 mutex_unlock(&spec->pcm_mutex);
3314 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3315 struct hda_codec *codec,
3316 struct snd_pcm_substream *substream)
3318 struct hda_gen_spec *spec = codec->spec;
3321 mutex_lock(&spec->pcm_mutex);
3322 if (!spec->indep_hp_enabled)
3325 spec->active_streams |= 1 << STREAM_INDEP_HP;
3326 mutex_unlock(&spec->pcm_mutex);
3330 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3331 struct hda_codec *codec,
3332 struct snd_pcm_substream *substream)
3334 struct hda_gen_spec *spec = codec->spec;
3335 mutex_lock(&spec->pcm_mutex);
3336 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3337 mutex_unlock(&spec->pcm_mutex);
3344 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3345 struct hda_codec *codec,
3346 struct snd_pcm_substream *substream)
3348 struct hda_gen_spec *spec = codec->spec;
3349 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3352 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3353 struct hda_codec *codec,
3354 unsigned int stream_tag,
3355 unsigned int format,
3356 struct snd_pcm_substream *substream)
3358 struct hda_gen_spec *spec = codec->spec;
3359 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3360 stream_tag, format, substream);
3363 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3364 struct hda_codec *codec,
3365 struct snd_pcm_substream *substream)
3367 struct hda_gen_spec *spec = codec->spec;
3368 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3371 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3372 struct hda_codec *codec,
3373 struct snd_pcm_substream *substream)
3375 struct hda_gen_spec *spec = codec->spec;
3376 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3382 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3383 struct hda_codec *codec,
3384 unsigned int stream_tag,
3385 unsigned int format,
3386 struct snd_pcm_substream *substream)
3388 struct hda_gen_spec *spec = codec->spec;
3390 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
3391 stream_tag, 0, format);
3395 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3396 struct hda_codec *codec,
3397 struct snd_pcm_substream *substream)
3399 struct hda_gen_spec *spec = codec->spec;
3401 snd_hda_codec_cleanup_stream(codec,
3402 spec->adc_nids[substream->number + 1]);
3408 static const struct hda_pcm_stream pcm_analog_playback = {
3412 /* NID is set in build_pcms */
3414 .open = playback_pcm_open,
3415 .close = playback_pcm_close,
3416 .prepare = playback_pcm_prepare,
3417 .cleanup = playback_pcm_cleanup
3421 static const struct hda_pcm_stream pcm_analog_capture = {
3425 /* NID is set in build_pcms */
3428 static const struct hda_pcm_stream pcm_analog_alt_playback = {
3432 /* NID is set in build_pcms */
3434 .open = alt_playback_pcm_open,
3435 .close = alt_playback_pcm_close
3439 static const struct hda_pcm_stream pcm_analog_alt_capture = {
3440 .substreams = 2, /* can be overridden */
3443 /* NID is set in build_pcms */
3445 .prepare = alt_capture_pcm_prepare,
3446 .cleanup = alt_capture_pcm_cleanup
3450 static const struct hda_pcm_stream pcm_digital_playback = {
3454 /* NID is set in build_pcms */
3456 .open = dig_playback_pcm_open,
3457 .close = dig_playback_pcm_close,
3458 .prepare = dig_playback_pcm_prepare,
3459 .cleanup = dig_playback_pcm_cleanup
3463 static const struct hda_pcm_stream pcm_digital_capture = {
3467 /* NID is set in build_pcms */
3470 /* Used by build_pcms to flag that a PCM has no playback stream */
3471 static const struct hda_pcm_stream pcm_null_stream = {
3478 * dynamic changing ADC PCM streams
3480 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3482 struct hda_gen_spec *spec = codec->spec;
3483 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3485 if (spec->cur_adc && spec->cur_adc != new_adc) {
3486 /* stream is running, let's swap the current ADC */
3487 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3488 spec->cur_adc = new_adc;
3489 snd_hda_codec_setup_stream(codec, new_adc,
3490 spec->cur_adc_stream_tag, 0,
3491 spec->cur_adc_format);
3497 /* analog capture with dynamic dual-adc changes */
3498 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3499 struct hda_codec *codec,
3500 unsigned int stream_tag,
3501 unsigned int format,
3502 struct snd_pcm_substream *substream)
3504 struct hda_gen_spec *spec = codec->spec;
3505 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3506 spec->cur_adc_stream_tag = stream_tag;
3507 spec->cur_adc_format = format;
3508 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3512 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3513 struct hda_codec *codec,
3514 struct snd_pcm_substream *substream)
3516 struct hda_gen_spec *spec = codec->spec;
3517 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3522 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3526 .nid = 0, /* fill later */
3528 .prepare = dyn_adc_capture_pcm_prepare,
3529 .cleanup = dyn_adc_capture_pcm_cleanup
3533 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3534 const char *chip_name)
3540 strlcpy(str, chip_name, len);
3542 /* drop non-alnum chars after a space */
3543 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3544 if (!isalnum(p[1])) {
3549 strlcat(str, sfx, len);
3552 /* build PCM streams based on the parsed results */
3553 int snd_hda_gen_build_pcms(struct hda_codec *codec)
3555 struct hda_gen_spec *spec = codec->spec;
3556 struct hda_pcm *info = spec->pcm_rec;
3557 const struct hda_pcm_stream *p;
3558 bool have_multi_adcs;
3560 codec->num_pcms = 1;
3561 codec->pcm_info = info;
3563 if (spec->no_analog)
3566 fill_pcm_stream_name(spec->stream_name_analog,
3567 sizeof(spec->stream_name_analog),
3568 " Analog", codec->chip_name);
3569 info->name = spec->stream_name_analog;
3571 if (spec->multiout.num_dacs > 0) {
3572 p = spec->stream_analog_playback;
3574 p = &pcm_analog_playback;
3575 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3576 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3577 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3578 spec->multiout.max_channels;
3579 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3580 spec->autocfg.line_outs == 2)
3581 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3584 if (spec->num_adc_nids) {
3585 p = spec->stream_analog_capture;
3587 if (spec->dyn_adc_switch)
3588 p = &dyn_adc_pcm_analog_capture;
3590 p = &pcm_analog_capture;
3592 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3593 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3597 /* SPDIF for stream index #1 */
3598 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
3599 fill_pcm_stream_name(spec->stream_name_digital,
3600 sizeof(spec->stream_name_digital),
3601 " Digital", codec->chip_name);
3602 codec->num_pcms = 2;
3603 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3604 info = spec->pcm_rec + 1;
3605 info->name = spec->stream_name_digital;
3606 if (spec->dig_out_type)
3607 info->pcm_type = spec->dig_out_type;
3609 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3610 if (spec->multiout.dig_out_nid) {
3611 p = spec->stream_digital_playback;
3613 p = &pcm_digital_playback;
3614 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3615 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3617 if (spec->dig_in_nid) {
3618 p = spec->stream_digital_capture;
3620 p = &pcm_digital_capture;
3621 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3622 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3626 if (spec->no_analog)
3629 /* If the use of more than one ADC is requested for the current
3630 * model, configure a second analog capture-only PCM.
3632 have_multi_adcs = (spec->num_adc_nids > 1) &&
3633 !spec->dyn_adc_switch && !spec->auto_mic;
3634 /* Additional Analaog capture for index #2 */
3635 if (spec->alt_dac_nid || have_multi_adcs) {
3636 codec->num_pcms = 3;
3637 info = spec->pcm_rec + 2;
3638 info->name = spec->stream_name_analog;
3639 if (spec->alt_dac_nid) {
3640 p = spec->stream_analog_alt_playback;
3642 p = &pcm_analog_alt_playback;
3643 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3644 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3647 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3649 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3651 if (have_multi_adcs) {
3652 p = spec->stream_analog_alt_capture;
3654 p = &pcm_analog_alt_capture;
3655 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3656 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3658 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3659 spec->num_adc_nids - 1;
3661 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3663 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3669 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3673 * Standard auto-parser initializations
3676 /* configure the given path as a proper output */
3677 static void set_output_and_unmute(struct hda_codec *codec,
3678 int pin_type, int path_idx)
3680 struct nid_path *path;
3683 path = snd_hda_get_path_from_idx(codec, path_idx);
3684 if (!path || !path->depth)
3686 pin = path->path[path->depth - 1];
3687 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3688 snd_hda_activate_path(codec, path, path->active, true);
3689 set_pin_eapd(codec, pin, path->active);
3692 /* initialize primary output paths */
3693 static void init_multi_out(struct hda_codec *codec)
3695 struct hda_gen_spec *spec = codec->spec;
3699 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3704 for (i = 0; i < spec->autocfg.line_outs; i++)
3705 set_output_and_unmute(codec, pin_type, spec->out_paths[i]);
3709 static void __init_extra_out(struct hda_codec *codec, int num_outs,
3710 int *paths, int type)
3714 for (i = 0; i < num_outs; i++)
3715 set_output_and_unmute(codec, type, paths[i]);
3718 /* initialize hp and speaker paths */
3719 static void init_extra_out(struct hda_codec *codec)
3721 struct hda_gen_spec *spec = codec->spec;
3723 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3724 __init_extra_out(codec, spec->autocfg.hp_outs,
3725 spec->hp_paths, PIN_HP);
3726 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3727 __init_extra_out(codec, spec->autocfg.speaker_outs,
3728 spec->speaker_paths, PIN_OUT);
3731 /* initialize multi-io paths */
3732 static void init_multi_io(struct hda_codec *codec)
3734 struct hda_gen_spec *spec = codec->spec;
3737 for (i = 0; i < spec->multi_ios; i++) {
3738 hda_nid_t pin = spec->multi_io[i].pin;
3739 struct nid_path *path;
3740 path = get_multiio_path(codec, i);
3743 if (!spec->multi_io[i].ctl_in)
3744 spec->multi_io[i].ctl_in =
3745 snd_hda_codec_update_cache(codec, pin, 0,
3746 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3747 snd_hda_activate_path(codec, path, path->active, true);
3751 /* set up the input pin config, depending on the given auto-pin type */
3752 static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3755 unsigned int val = PIN_IN;
3756 if (auto_pin_type == AUTO_PIN_MIC)
3757 val |= snd_hda_get_default_vref(codec, nid);
3758 snd_hda_set_pin_ctl_cache(codec, nid, val);
3761 /* set up input pins and loopback paths */
3762 static void init_analog_input(struct hda_codec *codec)
3764 struct hda_gen_spec *spec = codec->spec;
3765 struct auto_pin_cfg *cfg = &spec->autocfg;
3768 for (i = 0; i < cfg->num_inputs; i++) {
3769 hda_nid_t nid = cfg->inputs[i].pin;
3770 if (is_input_pin(codec, nid))
3771 set_input_pin(codec, nid, cfg->inputs[i].type);
3773 /* init loopback inputs */
3774 if (spec->mixer_nid) {
3775 struct nid_path *path;
3776 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
3778 snd_hda_activate_path(codec, path,
3779 path->active, false);
3784 /* initialize ADC paths */
3785 static void init_input_src(struct hda_codec *codec)
3787 struct hda_gen_spec *spec = codec->spec;
3788 struct hda_input_mux *imux = &spec->input_mux;
3789 struct nid_path *path;
3792 if (spec->dyn_adc_switch)
3795 nums = spec->num_adc_nids;
3797 for (c = 0; c < nums; c++) {
3798 for (i = 0; i < imux->num_items; i++) {
3799 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3800 get_adc_nid(codec, c, i));
3802 bool active = path->active;
3803 if (i == spec->cur_mux[c])
3805 snd_hda_activate_path(codec, path, active, false);
3810 if (spec->shared_mic_hp)
3811 update_shared_mic_hp(codec, spec->cur_mux[0]);
3813 if (spec->cap_sync_hook)
3814 spec->cap_sync_hook(codec);
3817 /* set right pin controls for digital I/O */
3818 static void init_digital(struct hda_codec *codec)
3820 struct hda_gen_spec *spec = codec->spec;
3824 for (i = 0; i < spec->autocfg.dig_outs; i++)
3825 set_output_and_unmute(codec, PIN_OUT, spec->digout_paths[i]);
3826 pin = spec->autocfg.dig_in_pin;
3828 struct nid_path *path;
3829 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
3830 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
3832 snd_hda_activate_path(codec, path, path->active, false);
3836 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3837 * invalid unsol tags by some reason
3839 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3843 for (i = 0; i < codec->init_pins.used; i++) {
3844 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3845 hda_nid_t nid = pin->nid;
3846 if (is_jack_detectable(codec, nid) &&
3847 !snd_hda_jack_tbl_get(codec, nid))
3848 snd_hda_codec_update_cache(codec, nid, 0,
3849 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3853 int snd_hda_gen_init(struct hda_codec *codec)
3855 struct hda_gen_spec *spec = codec->spec;
3857 if (spec->init_hook)
3858 spec->init_hook(codec);
3860 snd_hda_apply_verbs(codec);
3862 codec->cached_write = 1;
3864 init_multi_out(codec);
3865 init_extra_out(codec);
3866 init_multi_io(codec);
3867 init_analog_input(codec);
3868 init_input_src(codec);
3869 init_digital(codec);
3871 clear_unsol_on_unused_pins(codec);
3873 /* call init functions of standard auto-mute helpers */
3874 snd_hda_gen_hp_automute(codec, NULL);
3875 snd_hda_gen_line_automute(codec, NULL);
3876 snd_hda_gen_mic_autoswitch(codec, NULL);
3878 snd_hda_codec_flush_amp_cache(codec);
3879 snd_hda_codec_flush_cmd_cache(codec);
3881 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3882 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3884 hda_call_check_power_status(codec, 0x01);
3887 EXPORT_SYMBOL(snd_hda_gen_init);
3891 * the generic codec support
3895 static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3897 struct hda_gen_spec *spec = codec->spec;
3898 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3902 static void generic_free(struct hda_codec *codec)
3904 snd_hda_gen_spec_free(codec->spec);
3909 static const struct hda_codec_ops generic_patch_ops = {
3910 .build_controls = snd_hda_gen_build_controls,
3911 .build_pcms = snd_hda_gen_build_pcms,
3912 .init = snd_hda_gen_init,
3913 .free = generic_free,
3914 .unsol_event = snd_hda_jack_unsol_event,
3916 .check_power_status = generic_check_power_status,
3920 int snd_hda_parse_generic_codec(struct hda_codec *codec)
3922 struct hda_gen_spec *spec;
3925 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3928 snd_hda_gen_spec_init(spec);
3931 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
3935 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
3939 codec->patch_ops = generic_patch_ops;
3943 generic_free(codec);
3946 EXPORT_SYMBOL(snd_hda_parse_generic_codec);