2 * Universal Interface for Intel High Definition Audio Codec
4 * Generic widget tree parser
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/sort.h>
27 #include <linux/ctype.h>
28 #include <linux/string.h>
29 #include <sound/core.h>
30 #include <sound/jack.h>
31 #include "hda_codec.h"
32 #include "hda_local.h"
33 #include "hda_auto_parser.h"
35 #include "hda_generic.h"
38 /* initialize hda_gen_spec struct */
39 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
41 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
42 snd_array_init(&spec->bind_ctls, sizeof(struct hda_bind_ctls *), 8);
43 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
44 mutex_init(&spec->pcm_mutex);
47 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
49 struct snd_kcontrol_new *
50 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
51 const struct snd_kcontrol_new *temp)
53 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
58 knew->name = kstrdup(name, GFP_KERNEL);
60 knew->name = kstrdup(knew->name, GFP_KERNEL);
65 EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
67 static void free_kctls(struct hda_gen_spec *spec)
69 if (spec->kctls.list) {
70 struct snd_kcontrol_new *kctl = spec->kctls.list;
72 for (i = 0; i < spec->kctls.used; i++)
75 snd_array_free(&spec->kctls);
78 static struct hda_bind_ctls *new_bind_ctl(struct hda_codec *codec,
80 struct hda_ctl_ops *ops)
82 struct hda_gen_spec *spec = codec->spec;
83 struct hda_bind_ctls **ctlp, *ctl;
84 ctlp = snd_array_new(&spec->bind_ctls);
87 ctl = kzalloc(sizeof(*ctl) + sizeof(long) * (nums + 1), GFP_KERNEL);
94 static void free_bind_ctls(struct hda_gen_spec *spec)
96 if (spec->bind_ctls.list) {
97 struct hda_bind_ctls **ctl = spec->bind_ctls.list;
99 for (i = 0; i < spec->bind_ctls.used; i++)
102 snd_array_free(&spec->bind_ctls);
105 void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
110 free_bind_ctls(spec);
111 snd_array_free(&spec->paths);
113 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
119 static struct nid_path *get_nid_path(struct hda_codec *codec,
120 hda_nid_t from_nid, hda_nid_t to_nid,
123 struct hda_gen_spec *spec = codec->spec;
126 for (i = 0; i < spec->paths.used; i++) {
127 struct nid_path *path = snd_array_elem(&spec->paths, i);
128 if (path->depth <= 0)
130 if ((!from_nid || path->path[0] == from_nid) &&
131 (!to_nid || path->path[path->depth - 1] == to_nid)) {
132 if (with_aa_mix == HDA_PARSE_ALL ||
133 path->with_aa_mix == with_aa_mix)
140 /* get the path between the given NIDs;
141 * passing 0 to either @pin or @dac behaves as a wildcard
143 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
144 hda_nid_t from_nid, hda_nid_t to_nid)
146 return get_nid_path(codec, from_nid, to_nid, HDA_PARSE_ALL);
148 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
150 /* get the index number corresponding to the path instance;
151 * the index starts from 1, for easier checking the invalid value
153 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
155 struct hda_gen_spec *spec = codec->spec;
156 struct nid_path *array = spec->paths.list;
159 if (!spec->paths.used)
162 if (idx < 0 || idx >= spec->paths.used)
167 /* get the path instance corresponding to the given index number */
168 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
170 struct hda_gen_spec *spec = codec->spec;
172 if (idx <= 0 || idx > spec->paths.used)
174 return snd_array_elem(&spec->paths, idx - 1);
177 /* check whether the given DAC is already found in any existing paths */
178 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
180 struct hda_gen_spec *spec = codec->spec;
183 for (i = 0; i < spec->paths.used; i++) {
184 struct nid_path *path = snd_array_elem(&spec->paths, i);
185 if (path->path[0] == nid)
191 /* check whether the given two widgets can be connected */
192 static bool is_reachable_path(struct hda_codec *codec,
193 hda_nid_t from_nid, hda_nid_t to_nid)
195 if (!from_nid || !to_nid)
197 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
200 /* nid, dir and idx */
201 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
203 /* check whether the given ctl is already assigned in any path elements */
204 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
206 struct hda_gen_spec *spec = codec->spec;
209 val &= AMP_VAL_COMPARE_MASK;
210 for (i = 0; i < spec->paths.used; i++) {
211 struct nid_path *path = snd_array_elem(&spec->paths, i);
212 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
218 /* check whether a control with the given (nid, dir, idx) was assigned */
219 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
222 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
223 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
224 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
227 static void print_nid_path(const char *pfx, struct nid_path *path)
234 for (i = 0; i < path->depth; i++) {
236 sprintf(tmp, ":%02x", path->path[i]);
237 strlcat(buf, tmp, sizeof(buf));
239 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
242 /* called recursively */
243 static bool __parse_nid_path(struct hda_codec *codec,
244 hda_nid_t from_nid, hda_nid_t to_nid,
245 int with_aa_mix, struct nid_path *path, int depth)
247 struct hda_gen_spec *spec = codec->spec;
248 const hda_nid_t *conn;
251 if (to_nid == spec->mixer_nid) {
252 if (with_aa_mix == HDA_PARSE_NO_AAMIX)
254 with_aa_mix = HDA_PARSE_ALL; /* mark aa-mix is included */
257 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
258 for (i = 0; i < nums; i++) {
259 if (conn[i] != from_nid) {
260 /* special case: when from_nid is 0,
261 * try to find an empty DAC
264 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
265 is_dac_already_used(codec, conn[i]))
268 /* aa-mix is requested but not included? */
269 if (!(spec->mixer_nid && with_aa_mix == HDA_PARSE_ONLY_AAMIX))
272 if (depth >= MAX_NID_PATH_DEPTH)
274 for (i = 0; i < nums; i++) {
276 type = get_wcaps_type(get_wcaps(codec, conn[i]));
277 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
280 if (__parse_nid_path(codec, from_nid, conn[i],
281 with_aa_mix, path, depth + 1))
287 path->path[path->depth] = conn[i];
288 if (conn[i] == spec->mixer_nid)
289 path->with_aa_mix = true;
290 path->idx[path->depth + 1] = i;
291 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
292 path->multi[path->depth + 1] = 1;
297 /* parse the widget path from the given nid to the target nid;
298 * when @from_nid is 0, try to find an empty DAC;
299 * when @with_aa_mix is HDA_PARSE_NO_AAMIX, paths with spec->mixer_nid are
300 * excluded, only the paths that don't go through the mixer will be chosen.
301 * when @with_aa_mix is HDA_PARSE_ONLY_AAMIX, only the paths going through
302 * spec->mixer_nid will be chosen.
303 * when @with_aa_mix is HDA_PARSE_ALL, no special handling about mixer widget.
305 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
306 hda_nid_t to_nid, int with_aa_mix,
307 struct nid_path *path)
309 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
310 path->path[path->depth] = to_nid;
316 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
319 * parse the path between the given NIDs and add to the path list.
320 * if no valid path is found, return NULL
323 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
324 hda_nid_t to_nid, int with_aa_mix)
326 struct hda_gen_spec *spec = codec->spec;
327 struct nid_path *path;
329 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
332 /* check whether the path has been already added */
333 path = get_nid_path(codec, from_nid, to_nid, with_aa_mix);
337 path = snd_array_new(&spec->paths);
340 memset(path, 0, sizeof(*path));
341 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
347 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
349 /* look for an empty DAC slot */
350 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
353 struct hda_gen_spec *spec = codec->spec;
357 for (i = 0; i < spec->num_all_dacs; i++) {
358 hda_nid_t nid = spec->all_dacs[i];
359 if (!nid || is_dac_already_used(codec, nid))
361 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
362 if (is_digital != cap_digital)
364 if (is_reachable_path(codec, nid, pin))
370 /* replace the channels in the composed amp value with the given number */
371 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
373 val &= ~(0x3U << 16);
378 /* check whether the widget has the given amp capability for the direction */
379 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
380 int dir, unsigned int bits)
384 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
385 if (query_amp_caps(codec, nid, dir) & bits)
390 #define nid_has_mute(codec, nid, dir) \
391 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
392 #define nid_has_volume(codec, nid, dir) \
393 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
395 /* look for a widget suitable for assigning a mute switch in the path */
396 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
397 struct nid_path *path)
401 for (i = path->depth - 1; i >= 0; i--) {
402 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
403 return path->path[i];
404 if (i != path->depth - 1 && i != 0 &&
405 nid_has_mute(codec, path->path[i], HDA_INPUT))
406 return path->path[i];
411 /* look for a widget suitable for assigning a volume ctl in the path */
412 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
413 struct nid_path *path)
417 for (i = path->depth - 1; i >= 0; i--) {
418 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
419 return path->path[i];
425 * path activation / deactivation
428 /* can have the amp-in capability? */
429 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
431 hda_nid_t nid = path->path[idx];
432 unsigned int caps = get_wcaps(codec, nid);
433 unsigned int type = get_wcaps_type(caps);
435 if (!(caps & AC_WCAP_IN_AMP))
437 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
442 /* can have the amp-out capability? */
443 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
445 hda_nid_t nid = path->path[idx];
446 unsigned int caps = get_wcaps(codec, nid);
447 unsigned int type = get_wcaps_type(caps);
449 if (!(caps & AC_WCAP_OUT_AMP))
451 if (type == AC_WID_PIN && !idx) /* only for output pins */
456 /* check whether the given (nid,dir,idx) is active */
457 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
458 unsigned int idx, unsigned int dir)
460 struct hda_gen_spec *spec = codec->spec;
463 for (n = 0; n < spec->paths.used; n++) {
464 struct nid_path *path = snd_array_elem(&spec->paths, n);
467 for (i = 0; i < path->depth; i++) {
468 if (path->path[i] == nid) {
469 if (dir == HDA_OUTPUT || path->idx[i] == idx)
478 /* get the default amp value for the target state */
479 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
480 int dir, bool enable)
483 unsigned int val = 0;
485 caps = query_amp_caps(codec, nid, dir);
486 if (caps & AC_AMPCAP_NUM_STEPS) {
489 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
491 if (caps & AC_AMPCAP_MUTE) {
498 /* initialize the amp value (only at the first time) */
499 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
501 int val = get_amp_val_to_activate(codec, nid, dir, false);
502 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
505 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
506 int idx, bool enable)
509 if (is_ctl_associated(codec, nid, dir, idx) ||
510 (!enable && is_active_nid(codec, nid, dir, idx)))
512 val = get_amp_val_to_activate(codec, nid, dir, enable);
513 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
516 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
519 hda_nid_t nid = path->path[i];
520 init_amp(codec, nid, HDA_OUTPUT, 0);
521 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
524 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
525 int i, bool enable, bool add_aamix)
527 struct hda_gen_spec *spec = codec->spec;
528 const hda_nid_t *conn;
531 hda_nid_t nid = path->path[i];
533 nums = snd_hda_get_conn_list(codec, nid, &conn);
534 type = get_wcaps_type(get_wcaps(codec, nid));
535 if (type == AC_WID_PIN ||
536 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
542 for (n = 0; n < nums; n++)
543 init_amp(codec, nid, HDA_INPUT, n);
545 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
548 /* here is a little bit tricky in comparison with activate_amp_out();
549 * when aa-mixer is available, we need to enable the path as well
551 for (n = 0; n < nums; n++) {
552 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
554 activate_amp(codec, nid, HDA_INPUT, n, enable);
558 /* activate or deactivate the given path
559 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
561 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
562 bool enable, bool add_aamix)
567 path->active = false;
569 for (i = path->depth - 1; i >= 0; i--) {
570 if (enable && path->multi[i])
571 snd_hda_codec_write_cache(codec, path->path[i], 0,
572 AC_VERB_SET_CONNECT_SEL,
574 if (has_amp_in(codec, path, i))
575 activate_amp_in(codec, path, i, enable, add_aamix);
576 if (has_amp_out(codec, path, i))
577 activate_amp_out(codec, path, i, enable);
583 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
585 /* turn on/off EAPD on the given pin */
586 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
588 struct hda_gen_spec *spec = codec->spec;
589 if (spec->own_eapd_ctl ||
590 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
594 snd_hda_codec_update_cache(codec, pin, 0,
595 AC_VERB_SET_EAPD_BTLENABLE,
596 enable ? 0x02 : 0x00);
601 * Helper functions for creating mixer ctl elements
611 static const struct snd_kcontrol_new control_templates[] = {
612 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
613 HDA_CODEC_MUTE(NULL, 0, 0, 0),
614 HDA_BIND_MUTE(NULL, 0, 0, 0),
615 HDA_BIND_VOL(NULL, 0),
616 HDA_BIND_SW(NULL, 0),
619 /* add dynamic controls from template */
620 static int add_control(struct hda_gen_spec *spec, int type, const char *name,
621 int cidx, unsigned long val)
623 struct snd_kcontrol_new *knew;
625 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
629 if (get_amp_nid_(val))
630 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
631 knew->private_value = val;
635 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
636 const char *pfx, const char *dir,
637 const char *sfx, int cidx, unsigned long val)
640 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
641 return add_control(spec, type, name, cidx, val);
644 #define add_pb_vol_ctrl(spec, type, pfx, val) \
645 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
646 #define add_pb_sw_ctrl(spec, type, pfx, val) \
647 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
648 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
649 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
650 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
651 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
653 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
654 unsigned int chs, struct nid_path *path)
659 val = path->ctls[NID_PATH_VOL_CTL];
662 val = amp_val_replace_channels(val, chs);
663 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
666 /* return the channel bits suitable for the given path->ctls[] */
667 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
670 int chs = 1; /* mono (left only) */
672 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
673 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
674 chs = 3; /* stereo */
679 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
680 struct nid_path *path)
682 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
683 return add_vol_ctl(codec, pfx, cidx, chs, path);
686 /* create a mute-switch for the given mixer widget;
687 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
689 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
690 unsigned int chs, struct nid_path *path)
693 int type = HDA_CTL_WIDGET_MUTE;
697 val = path->ctls[NID_PATH_MUTE_CTL];
700 val = amp_val_replace_channels(val, chs);
701 if (get_amp_direction_(val) == HDA_INPUT) {
702 hda_nid_t nid = get_amp_nid_(val);
703 int nums = snd_hda_get_num_conns(codec, nid);
705 type = HDA_CTL_BIND_MUTE;
709 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
712 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
713 int cidx, struct nid_path *path)
715 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
716 return add_sw_ctl(codec, pfx, cidx, chs, path);
719 static const char * const channel_name[4] = {
720 "Front", "Surround", "CLFE", "Side"
723 /* give some appropriate ctl name prefix for the given line out channel */
724 static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
725 bool can_be_master, int *index)
727 struct auto_pin_cfg *cfg = &spec->autocfg;
730 if (cfg->line_outs == 1 && !spec->multi_ios &&
731 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
732 return spec->vmaster_mute.hook ? "PCM" : "Master";
734 /* if there is really a single DAC used in the whole output paths,
735 * use it master (or "PCM" if a vmaster hook is present)
737 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
738 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
739 return spec->vmaster_mute.hook ? "PCM" : "Master";
741 switch (cfg->line_out_type) {
742 case AUTO_PIN_SPEAKER_OUT:
743 if (cfg->line_outs == 1)
745 if (cfg->line_outs == 2)
746 return ch ? "Bass Speaker" : "Speaker";
748 case AUTO_PIN_HP_OUT:
749 /* for multi-io case, only the primary out */
750 if (ch && spec->multi_ios)
755 if (cfg->line_outs == 1 && !spec->multi_ios)
759 if (ch >= ARRAY_SIZE(channel_name)) {
764 return channel_name[ch];
771 /* badness definition */
773 /* No primary DAC is found for the main output */
774 BAD_NO_PRIMARY_DAC = 0x10000,
775 /* No DAC is found for the extra output */
777 /* No possible multi-ios */
778 BAD_MULTI_IO = 0x103,
779 /* No individual DAC for extra output */
780 BAD_NO_EXTRA_DAC = 0x102,
781 /* No individual DAC for extra surrounds */
782 BAD_NO_EXTRA_SURR_DAC = 0x101,
783 /* Primary DAC shared with main surrounds */
784 BAD_SHARED_SURROUND = 0x100,
785 /* Primary DAC shared with main CLFE */
786 BAD_SHARED_CLFE = 0x10,
787 /* Primary DAC shared with extra surrounds */
788 BAD_SHARED_EXTRA_SURROUND = 0x10,
789 /* Volume widget is shared */
790 BAD_SHARED_VOL = 0x10,
793 /* look for widgets in the path between the given NIDs appropriate for
794 * volume and mute controls, and assign the values to ctls[].
796 * When no appropriate widget is found in the path, the badness value
797 * is incremented depending on the situation. The function returns the
798 * total badness for both volume and mute controls.
800 static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
803 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
809 return BAD_SHARED_VOL * 2;
810 nid = look_for_out_vol_nid(codec, path);
812 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
813 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
814 badness += BAD_SHARED_VOL;
816 path->ctls[NID_PATH_VOL_CTL] = val;
818 badness += BAD_SHARED_VOL;
819 nid = look_for_out_mute_nid(codec, path);
821 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
822 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
823 nid_has_mute(codec, nid, HDA_OUTPUT))
824 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
826 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
827 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
828 badness += BAD_SHARED_VOL;
830 path->ctls[NID_PATH_MUTE_CTL] = val;
832 badness += BAD_SHARED_VOL;
836 struct badness_table {
837 int no_primary_dac; /* no primary DAC */
838 int no_dac; /* no secondary DACs */
839 int shared_primary; /* primary DAC is shared with main output */
840 int shared_surr; /* secondary DAC shared with main or primary */
841 int shared_clfe; /* third DAC shared with main or primary */
842 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
845 static struct badness_table main_out_badness = {
846 .no_primary_dac = BAD_NO_PRIMARY_DAC,
847 .no_dac = BAD_NO_DAC,
848 .shared_primary = BAD_NO_PRIMARY_DAC,
849 .shared_surr = BAD_SHARED_SURROUND,
850 .shared_clfe = BAD_SHARED_CLFE,
851 .shared_surr_main = BAD_SHARED_SURROUND,
854 static struct badness_table extra_out_badness = {
855 .no_primary_dac = BAD_NO_DAC,
856 .no_dac = BAD_NO_DAC,
857 .shared_primary = BAD_NO_EXTRA_DAC,
858 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
859 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
860 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
863 /* get the DAC of the primary output corresponding to the given array index */
864 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
866 struct hda_gen_spec *spec = codec->spec;
867 struct auto_pin_cfg *cfg = &spec->autocfg;
869 if (cfg->line_outs > idx)
870 return spec->private_dac_nids[idx];
871 idx -= cfg->line_outs;
872 if (spec->multi_ios > idx)
873 return spec->multi_io[idx].dac;
877 /* return the DAC if it's reachable, otherwise zero */
878 static inline hda_nid_t try_dac(struct hda_codec *codec,
879 hda_nid_t dac, hda_nid_t pin)
881 return is_reachable_path(codec, dac, pin) ? dac : 0;
884 /* try to assign DACs to pins and return the resultant badness */
885 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
886 const hda_nid_t *pins, hda_nid_t *dacs,
888 const struct badness_table *bad)
890 struct hda_gen_spec *spec = codec->spec;
898 for (i = 0; i < num_outs; i++) {
899 struct nid_path *path;
900 hda_nid_t pin = pins[i];
903 badness += assign_out_path_ctls(codec, pin, dacs[i]);
907 dacs[i] = look_for_dac(codec, pin, false);
908 if (!dacs[i] && !i) {
909 for (j = 1; j < num_outs; j++) {
910 if (is_reachable_path(codec, dacs[j], pin)) {
921 dac = try_dac(codec, get_primary_out(codec, i), pin);
923 dac = try_dac(codec, dacs[0], pin);
925 dac = try_dac(codec, get_primary_out(codec, i), pin);
928 badness += bad->shared_primary;
930 badness += bad->shared_surr;
932 badness += bad->shared_clfe;
933 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
934 dac = spec->private_dac_nids[0];
935 badness += bad->shared_surr_main;
937 badness += bad->no_primary_dac;
939 badness += bad->no_dac;
941 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_NO_AAMIX);
942 if (!path && !i && spec->mixer_nid) {
944 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_ALL);
949 print_nid_path("output", path);
951 path_idx[i] = snd_hda_get_path_idx(codec, path);
954 badness += assign_out_path_ctls(codec, pin, dac);
960 /* return NID if the given pin has only a single connection to a certain DAC */
961 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
963 struct hda_gen_spec *spec = codec->spec;
965 hda_nid_t nid_found = 0;
967 for (i = 0; i < spec->num_all_dacs; i++) {
968 hda_nid_t nid = spec->all_dacs[i];
969 if (!nid || is_dac_already_used(codec, nid))
971 if (is_reachable_path(codec, nid, pin)) {
980 /* check whether the given pin can be a multi-io pin */
981 static bool can_be_multiio_pin(struct hda_codec *codec,
982 unsigned int location, hda_nid_t nid)
984 unsigned int defcfg, caps;
986 defcfg = snd_hda_codec_get_pincfg(codec, nid);
987 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
989 if (location && get_defcfg_location(defcfg) != location)
991 caps = snd_hda_query_pin_caps(codec, nid);
992 if (!(caps & AC_PINCAP_OUT))
997 /* count the number of input pins that are capable to be multi-io */
998 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1000 struct hda_gen_spec *spec = codec->spec;
1001 struct auto_pin_cfg *cfg = &spec->autocfg;
1002 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1003 unsigned int location = get_defcfg_location(defcfg);
1007 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1008 for (i = 0; i < cfg->num_inputs; i++) {
1009 if (cfg->inputs[i].type != type)
1011 if (can_be_multiio_pin(codec, location,
1012 cfg->inputs[i].pin))
1022 * When hardwired is set, try to fill ony hardwired pins, and returns
1023 * zero if any pins are filled, non-zero if nothing found.
1024 * When hardwired is off, try to fill possible input pins, and returns
1025 * the badness value.
1027 static int fill_multi_ios(struct hda_codec *codec,
1028 hda_nid_t reference_pin,
1031 struct hda_gen_spec *spec = codec->spec;
1032 struct auto_pin_cfg *cfg = &spec->autocfg;
1033 int type, i, j, num_pins, old_pins;
1034 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1035 unsigned int location = get_defcfg_location(defcfg);
1038 old_pins = spec->multi_ios;
1042 num_pins = count_multiio_pins(codec, reference_pin);
1046 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1047 for (i = 0; i < cfg->num_inputs; i++) {
1048 struct nid_path *path;
1049 hda_nid_t nid = cfg->inputs[i].pin;
1052 if (cfg->inputs[i].type != type)
1054 if (!can_be_multiio_pin(codec, location, nid))
1056 for (j = 0; j < spec->multi_ios; j++) {
1057 if (nid == spec->multi_io[j].pin)
1060 if (j < spec->multi_ios)
1064 dac = get_dac_if_single(codec, nid);
1066 dac = look_for_dac(codec, nid, false);
1071 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
1076 print_nid_path("multiio", path);
1077 spec->multi_io[spec->multi_ios].pin = nid;
1078 spec->multi_io[spec->multi_ios].dac = dac;
1079 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1080 snd_hda_get_path_idx(codec, path);
1082 if (spec->multi_ios >= 2)
1088 badness = BAD_MULTI_IO;
1089 if (old_pins == spec->multi_ios) {
1091 return 1; /* nothing found */
1093 return badness; /* no badness if nothing found */
1095 if (!hardwired && spec->multi_ios < 2) {
1096 /* cancel newly assigned paths */
1097 spec->paths.used -= spec->multi_ios - old_pins;
1098 spec->multi_ios = old_pins;
1102 /* assign volume and mute controls */
1103 for (i = old_pins; i < spec->multi_ios; i++)
1104 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1105 spec->multi_io[i].dac);
1110 /* map DACs for all pins in the list if they are single connections */
1111 static bool map_singles(struct hda_codec *codec, int outs,
1112 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1114 struct hda_gen_spec *spec = codec->spec;
1117 for (i = 0; i < outs; i++) {
1118 struct nid_path *path;
1122 dac = get_dac_if_single(codec, pins[i]);
1125 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
1126 if (!path && !i && spec->mixer_nid)
1127 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
1131 print_nid_path("output", path);
1132 path->active = true;
1133 path_idx[i] = snd_hda_get_path_idx(codec, path);
1139 /* create a new path including aamix if available, and return its index */
1140 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1142 struct nid_path *path;
1144 path = snd_hda_get_path_from_idx(codec, path_idx);
1145 if (!path || !path->depth || path->with_aa_mix)
1147 path = snd_hda_add_new_path(codec, path->path[0],
1148 path->path[path->depth - 1],
1149 HDA_PARSE_ONLY_AAMIX);
1152 print_nid_path("output-aamix", path);
1153 path->active = false; /* unused as default */
1154 return snd_hda_get_path_idx(codec, path);
1157 /* fill in the dac_nids table from the parsed pin configuration */
1158 static int fill_and_eval_dacs(struct hda_codec *codec,
1159 bool fill_hardwired,
1160 bool fill_mio_first)
1162 struct hda_gen_spec *spec = codec->spec;
1163 struct auto_pin_cfg *cfg = &spec->autocfg;
1164 int i, err, badness;
1166 /* set num_dacs once to full for look_for_dac() */
1167 spec->multiout.num_dacs = cfg->line_outs;
1168 spec->multiout.dac_nids = spec->private_dac_nids;
1169 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1170 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1171 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1172 spec->multi_ios = 0;
1173 snd_array_free(&spec->paths);
1176 /* fill hard-wired DACs first */
1177 if (fill_hardwired) {
1180 mapped = map_singles(codec, cfg->line_outs,
1182 spec->private_dac_nids,
1184 mapped |= map_singles(codec, cfg->hp_outs,
1186 spec->multiout.hp_out_nid,
1188 mapped |= map_singles(codec, cfg->speaker_outs,
1190 spec->multiout.extra_out_nid,
1191 spec->speaker_paths);
1192 if (fill_mio_first && cfg->line_outs == 1 &&
1193 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1194 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1201 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1202 spec->private_dac_nids, spec->out_paths,
1205 /* re-count num_dacs and squash invalid entries */
1206 spec->multiout.num_dacs = 0;
1207 for (i = 0; i < cfg->line_outs; i++) {
1208 if (spec->private_dac_nids[i])
1209 spec->multiout.num_dacs++;
1211 memmove(spec->private_dac_nids + i,
1212 spec->private_dac_nids + i + 1,
1213 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1214 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1218 if (fill_mio_first &&
1219 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1220 /* try to fill multi-io first */
1221 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1224 /* we don't count badness at this stage yet */
1227 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1228 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1229 spec->multiout.hp_out_nid,
1231 &extra_out_badness);
1236 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1237 err = try_assign_dacs(codec, cfg->speaker_outs,
1239 spec->multiout.extra_out_nid,
1240 spec->speaker_paths,
1241 &extra_out_badness);
1246 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1247 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1253 if (spec->mixer_nid) {
1254 spec->aamix_out_paths[0] =
1255 check_aamix_out_path(codec, spec->out_paths[0]);
1256 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1257 spec->aamix_out_paths[1] =
1258 check_aamix_out_path(codec, spec->hp_paths[0]);
1259 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1260 spec->aamix_out_paths[2] =
1261 check_aamix_out_path(codec, spec->speaker_paths[0]);
1264 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1265 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1266 spec->multi_ios = 1; /* give badness */
1268 if (spec->multi_ios == 2) {
1269 for (i = 0; i < 2; i++)
1270 spec->private_dac_nids[spec->multiout.num_dacs++] =
1271 spec->multi_io[i].dac;
1272 spec->ext_channel_count = 2;
1273 } else if (spec->multi_ios) {
1274 spec->multi_ios = 0;
1275 badness += BAD_MULTI_IO;
1281 #define DEBUG_BADNESS
1283 #ifdef DEBUG_BADNESS
1284 #define debug_badness snd_printdd
1286 #define debug_badness(...)
1289 static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1291 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1292 cfg->line_out_pins[0], cfg->line_out_pins[1],
1293 cfg->line_out_pins[2], cfg->line_out_pins[3],
1294 spec->multiout.dac_nids[0],
1295 spec->multiout.dac_nids[1],
1296 spec->multiout.dac_nids[2],
1297 spec->multiout.dac_nids[3]);
1298 if (spec->multi_ios > 0)
1299 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1301 spec->multi_io[0].pin, spec->multi_io[1].pin,
1302 spec->multi_io[0].dac, spec->multi_io[1].dac);
1303 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1304 cfg->hp_pins[0], cfg->hp_pins[1],
1305 cfg->hp_pins[2], cfg->hp_pins[3],
1306 spec->multiout.hp_out_nid[0],
1307 spec->multiout.hp_out_nid[1],
1308 spec->multiout.hp_out_nid[2],
1309 spec->multiout.hp_out_nid[3]);
1310 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1311 cfg->speaker_pins[0], cfg->speaker_pins[1],
1312 cfg->speaker_pins[2], cfg->speaker_pins[3],
1313 spec->multiout.extra_out_nid[0],
1314 spec->multiout.extra_out_nid[1],
1315 spec->multiout.extra_out_nid[2],
1316 spec->multiout.extra_out_nid[3]);
1319 /* find all available DACs of the codec */
1320 static void fill_all_dac_nids(struct hda_codec *codec)
1322 struct hda_gen_spec *spec = codec->spec;
1324 hda_nid_t nid = codec->start_nid;
1326 spec->num_all_dacs = 0;
1327 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1328 for (i = 0; i < codec->num_nodes; i++, nid++) {
1329 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1331 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1332 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1335 spec->all_dacs[spec->num_all_dacs++] = nid;
1339 static int parse_output_paths(struct hda_codec *codec)
1341 struct hda_gen_spec *spec = codec->spec;
1342 struct auto_pin_cfg *cfg = &spec->autocfg;
1343 struct auto_pin_cfg *best_cfg;
1344 int best_badness = INT_MAX;
1346 bool fill_hardwired = true, fill_mio_first = true;
1347 bool best_wired = true, best_mio = true;
1348 bool hp_spk_swapped = false;
1350 fill_all_dac_nids(codec);
1352 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1358 badness = fill_and_eval_dacs(codec, fill_hardwired,
1364 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1365 cfg->line_out_type, fill_hardwired, fill_mio_first,
1367 debug_show_configs(spec, cfg);
1368 if (badness < best_badness) {
1369 best_badness = badness;
1371 best_wired = fill_hardwired;
1372 best_mio = fill_mio_first;
1376 fill_mio_first = !fill_mio_first;
1377 if (!fill_mio_first)
1379 fill_hardwired = !fill_hardwired;
1380 if (!fill_hardwired)
1384 hp_spk_swapped = true;
1385 if (cfg->speaker_outs > 0 &&
1386 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1387 cfg->hp_outs = cfg->line_outs;
1388 memcpy(cfg->hp_pins, cfg->line_out_pins,
1389 sizeof(cfg->hp_pins));
1390 cfg->line_outs = cfg->speaker_outs;
1391 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1392 sizeof(cfg->speaker_pins));
1393 cfg->speaker_outs = 0;
1394 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1395 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1396 fill_hardwired = true;
1399 if (cfg->hp_outs > 0 &&
1400 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1401 cfg->speaker_outs = cfg->line_outs;
1402 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1403 sizeof(cfg->speaker_pins));
1404 cfg->line_outs = cfg->hp_outs;
1405 memcpy(cfg->line_out_pins, cfg->hp_pins,
1406 sizeof(cfg->hp_pins));
1408 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1409 cfg->line_out_type = AUTO_PIN_HP_OUT;
1410 fill_hardwired = true;
1417 debug_badness("==> restoring best_cfg\n");
1419 fill_and_eval_dacs(codec, best_wired, best_mio);
1421 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1422 cfg->line_out_type, best_wired, best_mio);
1423 debug_show_configs(spec, cfg);
1425 if (cfg->line_out_pins[0]) {
1426 struct nid_path *path;
1427 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1429 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1436 /* add playback controls from the parsed DAC table */
1437 static int create_multi_out_ctls(struct hda_codec *codec,
1438 const struct auto_pin_cfg *cfg)
1440 struct hda_gen_spec *spec = codec->spec;
1441 int i, err, noutputs;
1443 noutputs = cfg->line_outs;
1444 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1445 noutputs += spec->multi_ios;
1447 for (i = 0; i < noutputs; i++) {
1451 struct nid_path *path;
1453 dac = spec->multiout.dac_nids[i];
1456 if (i >= cfg->line_outs) {
1458 name = channel_name[i];
1460 name = get_line_out_pfx(spec, i, true, &index);
1463 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1466 if (!name || !strcmp(name, "CLFE")) {
1468 err = add_vol_ctl(codec, "Center", 0, 1, path);
1471 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1474 err = add_sw_ctl(codec, "Center", 0, 1, path);
1477 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1481 err = add_stereo_vol(codec, name, index, path);
1484 err = add_stereo_sw(codec, name, index, path);
1492 static int create_extra_out(struct hda_codec *codec, hda_nid_t pin,
1493 hda_nid_t dac, int path_idx,
1494 const char *pfx, int cidx)
1496 struct nid_path *path;
1499 path = snd_hda_get_path_from_idx(codec, path_idx);
1502 /* bind volume control will be created in the case of dac = 0 */
1504 err = add_stereo_vol(codec, pfx, cidx, path);
1508 err = add_stereo_sw(codec, pfx, cidx, path);
1514 /* add playback controls for speaker and HP outputs */
1515 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1516 const hda_nid_t *pins, const hda_nid_t *dacs,
1517 const int *paths, const char *pfx)
1519 struct hda_gen_spec *spec = codec->spec;
1520 struct hda_bind_ctls *ctl;
1524 if (!num_pins || !pins[0])
1527 if (num_pins == 1) {
1528 hda_nid_t dac = *dacs;
1530 dac = spec->multiout.dac_nids[0];
1531 return create_extra_out(codec, *pins, dac, paths[0], pfx, 0);
1534 for (i = 0; i < num_pins; i++) {
1536 if (dacs[num_pins - 1])
1537 dac = dacs[i]; /* with individual volumes */
1540 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) {
1541 err = create_extra_out(codec, pins[i], dac, paths[i],
1543 } else if (num_pins >= 3) {
1544 snprintf(name, sizeof(name), "%s %s",
1545 pfx, channel_name[i]);
1546 err = create_extra_out(codec, pins[i], dac, paths[i],
1549 err = create_extra_out(codec, pins[i], dac, paths[i],
1555 if (dacs[num_pins - 1])
1558 /* Let's create a bind-controls for volumes */
1559 ctl = new_bind_ctl(codec, num_pins, &snd_hda_bind_vol);
1563 for (i = 0; i < num_pins; i++) {
1565 struct nid_path *path;
1566 if (!pins[i] || !dacs[i])
1568 path = snd_hda_get_path_from_idx(codec, paths[i]);
1571 vol = look_for_out_vol_nid(codec, path);
1574 HDA_COMPOSE_AMP_VAL(vol, 3, 0, HDA_OUTPUT);
1577 snprintf(name, sizeof(name), "%s Playback Volume", pfx);
1578 err = add_control(spec, HDA_CTL_BIND_VOL, name, 0, (long)ctl);
1585 static int create_hp_out_ctls(struct hda_codec *codec)
1587 struct hda_gen_spec *spec = codec->spec;
1588 return create_extra_outs(codec, spec->autocfg.hp_outs,
1589 spec->autocfg.hp_pins,
1590 spec->multiout.hp_out_nid,
1595 static int create_speaker_out_ctls(struct hda_codec *codec)
1597 struct hda_gen_spec *spec = codec->spec;
1598 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1599 spec->autocfg.speaker_pins,
1600 spec->multiout.extra_out_nid,
1601 spec->speaker_paths,
1606 * independent HP controls
1609 static int indep_hp_info(struct snd_kcontrol *kcontrol,
1610 struct snd_ctl_elem_info *uinfo)
1612 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1615 static int indep_hp_get(struct snd_kcontrol *kcontrol,
1616 struct snd_ctl_elem_value *ucontrol)
1618 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1619 struct hda_gen_spec *spec = codec->spec;
1620 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1624 static int indep_hp_put(struct snd_kcontrol *kcontrol,
1625 struct snd_ctl_elem_value *ucontrol)
1627 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1628 struct hda_gen_spec *spec = codec->spec;
1629 unsigned int select = ucontrol->value.enumerated.item[0];
1632 mutex_lock(&spec->pcm_mutex);
1633 if (spec->active_streams) {
1638 if (spec->indep_hp_enabled != select) {
1639 spec->indep_hp_enabled = select;
1640 if (spec->indep_hp_enabled)
1641 spec->multiout.hp_out_nid[0] = 0;
1643 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1647 mutex_unlock(&spec->pcm_mutex);
1651 static const struct snd_kcontrol_new indep_hp_ctl = {
1652 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1653 .name = "Independent HP",
1654 .info = indep_hp_info,
1655 .get = indep_hp_get,
1656 .put = indep_hp_put,
1660 static int create_indep_hp_ctls(struct hda_codec *codec)
1662 struct hda_gen_spec *spec = codec->spec;
1664 if (!spec->indep_hp)
1666 if (!spec->multiout.hp_out_nid[0]) {
1671 spec->indep_hp_enabled = false;
1672 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1673 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1679 * channel mode enum control
1682 static int ch_mode_info(struct snd_kcontrol *kcontrol,
1683 struct snd_ctl_elem_info *uinfo)
1685 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1686 struct hda_gen_spec *spec = codec->spec;
1688 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1690 uinfo->value.enumerated.items = spec->multi_ios + 1;
1691 if (uinfo->value.enumerated.item > spec->multi_ios)
1692 uinfo->value.enumerated.item = spec->multi_ios;
1693 sprintf(uinfo->value.enumerated.name, "%dch",
1694 (uinfo->value.enumerated.item + 1) * 2);
1698 static int ch_mode_get(struct snd_kcontrol *kcontrol,
1699 struct snd_ctl_elem_value *ucontrol)
1701 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1702 struct hda_gen_spec *spec = codec->spec;
1703 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1707 static inline struct nid_path *
1708 get_multiio_path(struct hda_codec *codec, int idx)
1710 struct hda_gen_spec *spec = codec->spec;
1711 return snd_hda_get_path_from_idx(codec,
1712 spec->out_paths[spec->autocfg.line_outs + idx]);
1715 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1717 struct hda_gen_spec *spec = codec->spec;
1718 hda_nid_t nid = spec->multi_io[idx].pin;
1719 struct nid_path *path;
1721 path = get_multiio_path(codec, idx);
1725 if (path->active == output)
1729 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1730 snd_hda_activate_path(codec, path, true, true);
1731 set_pin_eapd(codec, nid, true);
1733 set_pin_eapd(codec, nid, false);
1734 snd_hda_activate_path(codec, path, false, true);
1735 snd_hda_set_pin_ctl_cache(codec, nid,
1736 spec->multi_io[idx].ctl_in);
1741 static int ch_mode_put(struct snd_kcontrol *kcontrol,
1742 struct snd_ctl_elem_value *ucontrol)
1744 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1745 struct hda_gen_spec *spec = codec->spec;
1748 ch = ucontrol->value.enumerated.item[0];
1749 if (ch < 0 || ch > spec->multi_ios)
1751 if (ch == (spec->ext_channel_count - 1) / 2)
1753 spec->ext_channel_count = (ch + 1) * 2;
1754 for (i = 0; i < spec->multi_ios; i++)
1755 set_multi_io(codec, i, i < ch);
1756 spec->multiout.max_channels = max(spec->ext_channel_count,
1757 spec->const_channel_count);
1758 if (spec->need_dac_fix)
1759 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1763 static const struct snd_kcontrol_new channel_mode_enum = {
1764 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1765 .name = "Channel Mode",
1766 .info = ch_mode_info,
1771 static int create_multi_channel_mode(struct hda_codec *codec)
1773 struct hda_gen_spec *spec = codec->spec;
1775 if (spec->multi_ios > 0) {
1776 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
1783 * aamix loopback enable/disable switch
1786 #define loopback_mixing_info indep_hp_info
1788 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1789 struct snd_ctl_elem_value *ucontrol)
1791 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1792 struct hda_gen_spec *spec = codec->spec;
1793 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1797 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1798 int nomix_path_idx, int mix_path_idx)
1800 struct nid_path *nomix_path, *mix_path;
1802 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1803 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1804 if (!nomix_path || !mix_path)
1807 snd_hda_activate_path(codec, nomix_path, false, true);
1808 snd_hda_activate_path(codec, mix_path, true, true);
1810 snd_hda_activate_path(codec, mix_path, false, true);
1811 snd_hda_activate_path(codec, nomix_path, true, true);
1815 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1816 struct snd_ctl_elem_value *ucontrol)
1818 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1819 struct hda_gen_spec *spec = codec->spec;
1820 unsigned int val = ucontrol->value.enumerated.item[0];
1822 if (val == spec->aamix_mode)
1824 spec->aamix_mode = val;
1825 update_aamix_paths(codec, val, spec->out_paths[0],
1826 spec->aamix_out_paths[0]);
1827 update_aamix_paths(codec, val, spec->hp_paths[0],
1828 spec->aamix_out_paths[1]);
1829 update_aamix_paths(codec, val, spec->speaker_paths[0],
1830 spec->aamix_out_paths[2]);
1834 static const struct snd_kcontrol_new loopback_mixing_enum = {
1835 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1836 .name = "Loopback Mixing",
1837 .info = loopback_mixing_info,
1838 .get = loopback_mixing_get,
1839 .put = loopback_mixing_put,
1842 static int create_loopback_mixing_ctl(struct hda_codec *codec)
1844 struct hda_gen_spec *spec = codec->spec;
1846 if (!spec->mixer_nid)
1848 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1849 spec->aamix_out_paths[2]))
1851 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1857 * shared headphone/mic handling
1860 static void call_update_outputs(struct hda_codec *codec);
1862 /* for shared I/O, change the pin-control accordingly */
1863 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1865 struct hda_gen_spec *spec = codec->spec;
1867 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1868 /* NOTE: this assumes that there are only two inputs, the
1869 * first is the real internal mic and the second is HP/mic jack.
1872 val = snd_hda_get_default_vref(codec, pin);
1874 /* This pin does not have vref caps - let's enable vref on pin 0x18
1875 instead, as suggested by Realtek */
1876 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1877 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1878 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1879 if (vref_val != AC_PINCTL_VREF_HIZ)
1880 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1881 PIN_IN | (set_as_mic ? vref_val : 0));
1884 val = set_as_mic ? val | PIN_IN : PIN_HP;
1885 snd_hda_set_pin_ctl_cache(codec, pin, val);
1887 spec->automute_speaker = !set_as_mic;
1888 call_update_outputs(codec);
1891 /* create a shared input with the headphone out */
1892 static int create_shared_input(struct hda_codec *codec)
1894 struct hda_gen_spec *spec = codec->spec;
1895 struct auto_pin_cfg *cfg = &spec->autocfg;
1896 unsigned int defcfg;
1899 /* only one internal input pin? */
1900 if (cfg->num_inputs != 1)
1902 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1903 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1906 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1907 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1908 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1909 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1911 return 0; /* both not available */
1913 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1914 return 0; /* no input */
1916 cfg->inputs[1].pin = nid;
1917 cfg->inputs[1].type = AUTO_PIN_MIC;
1918 cfg->num_inputs = 2;
1919 spec->shared_mic_hp = 1;
1920 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1930 /* add the powersave loopback-list entry */
1931 static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1933 struct hda_amp_list *list;
1935 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1937 list = spec->loopback_list + spec->num_loopbacks;
1939 list->dir = HDA_INPUT;
1941 spec->num_loopbacks++;
1942 spec->loopback.amplist = spec->loopback_list;
1945 #define add_loopback_list(spec, mix, idx) /* NOP */
1948 /* create input playback/capture controls for the given pin */
1949 static int new_analog_input(struct hda_codec *codec, int input_idx,
1950 hda_nid_t pin, const char *ctlname, int ctlidx,
1953 struct hda_gen_spec *spec = codec->spec;
1954 struct nid_path *path;
1958 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1959 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1960 return 0; /* no need for analog loopback */
1962 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
1965 print_nid_path("loopback", path);
1966 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
1968 idx = path->idx[path->depth - 1];
1969 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1970 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1971 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
1974 path->ctls[NID_PATH_VOL_CTL] = val;
1977 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1978 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1979 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
1982 path->ctls[NID_PATH_MUTE_CTL] = val;
1985 path->active = true;
1986 add_loopback_list(spec, mix_nid, idx);
1990 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
1992 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1993 return (pincap & AC_PINCAP_IN) != 0;
1996 /* Parse the codec tree and retrieve ADCs */
1997 static int fill_adc_nids(struct hda_codec *codec)
1999 struct hda_gen_spec *spec = codec->spec;
2001 hda_nid_t *adc_nids = spec->adc_nids;
2002 int max_nums = ARRAY_SIZE(spec->adc_nids);
2005 nid = codec->start_nid;
2006 for (i = 0; i < codec->num_nodes; i++, nid++) {
2007 unsigned int caps = get_wcaps(codec, nid);
2008 int type = get_wcaps_type(caps);
2010 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2012 adc_nids[nums] = nid;
2013 if (++nums >= max_nums)
2016 spec->num_adc_nids = nums;
2020 /* filter out invalid adc_nids that don't give all active input pins;
2021 * if needed, check whether dynamic ADC-switching is available
2023 static int check_dyn_adc_switch(struct hda_codec *codec)
2025 struct hda_gen_spec *spec = codec->spec;
2026 struct hda_input_mux *imux = &spec->input_mux;
2027 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
2033 for (n = 0; n < spec->num_adc_nids; n++) {
2034 adc = spec->adc_nids[n];
2035 for (i = 0; i < imux->num_items; i++) {
2036 pin = spec->imux_pins[i];
2037 if (!is_reachable_path(codec, pin, adc))
2040 if (i >= imux->num_items)
2041 adc_nids[nums++] = adc;
2045 if (spec->shared_mic_hp) {
2046 spec->shared_mic_hp = 0;
2047 imux->num_items = 1;
2051 /* check whether ADC-switch is possible */
2052 for (i = 0; i < imux->num_items; i++) {
2053 pin = spec->imux_pins[i];
2054 for (n = 0; n < spec->num_adc_nids; n++) {
2055 adc = spec->adc_nids[n];
2056 if (is_reachable_path(codec, pin, adc)) {
2057 spec->dyn_adc_idx[i] = n;
2063 snd_printdd("hda-codec: enabling ADC switching\n");
2064 spec->dyn_adc_switch = 1;
2065 } else if (nums != spec->num_adc_nids) {
2066 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
2067 spec->num_adc_nids = nums;
2070 if (imux->num_items == 1 || spec->shared_mic_hp) {
2071 snd_printdd("hda-codec: reducing to a single ADC\n");
2072 spec->num_adc_nids = 1; /* reduce to a single ADC */
2075 /* single index for individual volumes ctls */
2076 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2077 spec->num_adc_nids = 1;
2083 * create playback/capture controls for input pins
2085 static int create_input_ctls(struct hda_codec *codec)
2087 struct hda_gen_spec *spec = codec->spec;
2088 const struct auto_pin_cfg *cfg = &spec->autocfg;
2089 hda_nid_t mixer = spec->mixer_nid;
2090 struct hda_input_mux *imux = &spec->input_mux;
2092 int i, c, err, type_idx = 0;
2093 const char *prev_label = NULL;
2095 num_adcs = fill_adc_nids(codec);
2099 for (i = 0; i < cfg->num_inputs; i++) {
2104 pin = cfg->inputs[i].pin;
2105 if (!is_input_pin(codec, pin))
2108 label = hda_get_autocfg_input_label(codec, cfg, i);
2109 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2110 label = "Headphone Mic";
2111 if (prev_label && !strcmp(label, prev_label))
2118 if (is_reachable_path(codec, pin, mixer)) {
2119 err = new_analog_input(codec, i, pin,
2120 label, type_idx, mixer);
2127 for (c = 0; c < num_adcs; c++) {
2128 struct nid_path *path;
2129 hda_nid_t adc = spec->adc_nids[c];
2131 if (!is_reachable_path(codec, pin, adc))
2133 path = snd_array_new(&spec->paths);
2136 memset(path, 0, sizeof(*path));
2137 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
2139 "invalid input path 0x%x -> 0x%x\n",
2144 print_nid_path("input", path);
2147 spec->imux_pins[imux->num_items] = pin;
2148 snd_hda_add_imux_item(imux, label,
2149 imux->num_items, NULL);
2163 /* get the ADC NID corresponding to the given index */
2164 static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
2166 struct hda_gen_spec *spec = codec->spec;
2167 if (spec->dyn_adc_switch)
2168 adc_idx = spec->dyn_adc_idx[imux_idx];
2169 return spec->adc_nids[adc_idx];
2172 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2175 static int mux_enum_info(struct snd_kcontrol *kcontrol,
2176 struct snd_ctl_elem_info *uinfo)
2178 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2179 struct hda_gen_spec *spec = codec->spec;
2180 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2183 static int mux_enum_get(struct snd_kcontrol *kcontrol,
2184 struct snd_ctl_elem_value *ucontrol)
2186 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2187 struct hda_gen_spec *spec = codec->spec;
2188 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2190 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2194 static int mux_enum_put(struct snd_kcontrol *kcontrol,
2195 struct snd_ctl_elem_value *ucontrol)
2197 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2198 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2199 return mux_select(codec, adc_idx,
2200 ucontrol->value.enumerated.item[0]);
2203 static const struct snd_kcontrol_new cap_src_temp = {
2204 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2205 .name = "Input Source",
2206 .info = mux_enum_info,
2207 .get = mux_enum_get,
2208 .put = mux_enum_put,
2212 * capture volume and capture switch ctls
2215 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2216 struct snd_ctl_elem_value *ucontrol);
2218 /* call the given amp update function for all amps in the imux list at once */
2219 static int cap_put_caller(struct snd_kcontrol *kcontrol,
2220 struct snd_ctl_elem_value *ucontrol,
2221 put_call_t func, int type)
2223 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2224 struct hda_gen_spec *spec = codec->spec;
2225 const struct hda_input_mux *imux;
2226 struct nid_path *path;
2227 int i, adc_idx, err = 0;
2229 imux = &spec->input_mux;
2230 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2231 mutex_lock(&codec->control_mutex);
2232 /* we use the cache-only update at first since multiple input paths
2233 * may shared the same amp; by updating only caches, the redundant
2234 * writes to hardware can be reduced.
2236 codec->cached_write = 1;
2237 for (i = 0; i < imux->num_items; i++) {
2238 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2239 get_adc_nid(codec, adc_idx, i));
2240 if (!path->ctls[type])
2242 kcontrol->private_value = path->ctls[type];
2243 err = func(kcontrol, ucontrol);
2248 codec->cached_write = 0;
2249 mutex_unlock(&codec->control_mutex);
2250 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
2251 if (err >= 0 && spec->cap_sync_hook)
2252 spec->cap_sync_hook(codec);
2256 /* capture volume ctl callbacks */
2257 #define cap_vol_info snd_hda_mixer_amp_volume_info
2258 #define cap_vol_get snd_hda_mixer_amp_volume_get
2259 #define cap_vol_tlv snd_hda_mixer_amp_tlv
2261 static int cap_vol_put(struct snd_kcontrol *kcontrol,
2262 struct snd_ctl_elem_value *ucontrol)
2264 return cap_put_caller(kcontrol, ucontrol,
2265 snd_hda_mixer_amp_volume_put,
2269 static const struct snd_kcontrol_new cap_vol_temp = {
2270 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2271 .name = "Capture Volume",
2272 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2273 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2274 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2275 .info = cap_vol_info,
2278 .tlv = { .c = cap_vol_tlv },
2281 /* capture switch ctl callbacks */
2282 #define cap_sw_info snd_ctl_boolean_stereo_info
2283 #define cap_sw_get snd_hda_mixer_amp_switch_get
2285 static int cap_sw_put(struct snd_kcontrol *kcontrol,
2286 struct snd_ctl_elem_value *ucontrol)
2288 return cap_put_caller(kcontrol, ucontrol,
2289 snd_hda_mixer_amp_switch_put,
2293 static const struct snd_kcontrol_new cap_sw_temp = {
2294 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2295 .name = "Capture Switch",
2296 .info = cap_sw_info,
2301 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2306 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2307 for (depth = 0; depth < 3; depth++) {
2308 if (depth >= path->depth)
2310 i = path->depth - depth - 1;
2311 nid = path->path[i];
2312 if (!path->ctls[NID_PATH_VOL_CTL]) {
2313 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2314 path->ctls[NID_PATH_VOL_CTL] =
2315 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2316 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2317 int idx = path->idx[i];
2318 if (!depth && codec->single_adc_amp)
2320 path->ctls[NID_PATH_VOL_CTL] =
2321 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2324 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2325 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2326 path->ctls[NID_PATH_MUTE_CTL] =
2327 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2328 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2329 int idx = path->idx[i];
2330 if (!depth && codec->single_adc_amp)
2332 path->ctls[NID_PATH_MUTE_CTL] =
2333 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2340 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2342 struct hda_gen_spec *spec = codec->spec;
2343 struct auto_pin_cfg *cfg = &spec->autocfg;
2347 if (!spec->inv_dmic_split)
2349 for (i = 0; i < cfg->num_inputs; i++) {
2350 if (cfg->inputs[i].pin != nid)
2352 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2354 val = snd_hda_codec_get_pincfg(codec, nid);
2355 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2360 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2361 int idx, bool is_switch, unsigned int ctl,
2364 struct hda_gen_spec *spec = codec->spec;
2366 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2367 const char *sfx = is_switch ? "Switch" : "Volume";
2368 unsigned int chs = inv_dmic ? 1 : 3;
2375 snprintf(tmpname, sizeof(tmpname),
2376 "%s Capture %s", label, sfx);
2378 snprintf(tmpname, sizeof(tmpname),
2380 err = add_control(spec, type, tmpname, idx,
2381 amp_val_replace_channels(ctl, chs));
2382 if (err < 0 || !inv_dmic)
2385 /* Make independent right kcontrol */
2387 snprintf(tmpname, sizeof(tmpname),
2388 "Inverted %s Capture %s", label, sfx);
2390 snprintf(tmpname, sizeof(tmpname),
2391 "Inverted Capture %s", sfx);
2392 return add_control(spec, type, tmpname, idx,
2393 amp_val_replace_channels(ctl, 2));
2396 /* create single (and simple) capture volume and switch controls */
2397 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2398 unsigned int vol_ctl, unsigned int sw_ctl,
2402 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2405 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2411 /* create bound capture volume and switch controls */
2412 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2413 unsigned int vol_ctl, unsigned int sw_ctl)
2415 struct hda_gen_spec *spec = codec->spec;
2416 struct snd_kcontrol_new *knew;
2419 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
2423 knew->private_value = vol_ctl;
2424 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2427 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
2431 knew->private_value = sw_ctl;
2432 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2437 /* return the vol ctl when used first in the imux list */
2438 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2440 struct hda_gen_spec *spec = codec->spec;
2441 struct nid_path *path;
2445 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2446 get_adc_nid(codec, 0, idx));
2449 ctl = path->ctls[type];
2452 for (i = 0; i < idx - 1; i++) {
2453 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2454 get_adc_nid(codec, 0, i));
2455 if (path && path->ctls[type] == ctl)
2461 /* create individual capture volume and switch controls per input */
2462 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2464 struct hda_gen_spec *spec = codec->spec;
2465 struct hda_input_mux *imux = &spec->input_mux;
2466 int i, err, type, type_idx = 0;
2467 const char *prev_label = NULL;
2469 for (i = 0; i < imux->num_items; i++) {
2472 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2473 if (prev_label && !strcmp(label, prev_label))
2478 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2480 for (type = 0; type < 2; type++) {
2481 err = add_single_cap_ctl(codec, label, type_idx, type,
2482 get_first_cap_ctl(codec, i, type),
2491 static int create_capture_mixers(struct hda_codec *codec)
2493 struct hda_gen_spec *spec = codec->spec;
2494 struct hda_input_mux *imux = &spec->input_mux;
2495 int i, n, nums, err;
2497 if (spec->dyn_adc_switch)
2500 nums = spec->num_adc_nids;
2502 if (!spec->auto_mic && imux->num_items > 1) {
2503 struct snd_kcontrol_new *knew;
2505 name = nums > 1 ? "Input Source" : "Capture Source";
2506 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
2512 for (n = 0; n < nums; n++) {
2514 bool inv_dmic = false;
2518 for (i = 0; i < imux->num_items; i++) {
2519 struct nid_path *path;
2520 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2521 get_adc_nid(codec, n, i));
2524 parse_capvol_in_path(codec, path);
2526 vol = path->ctls[NID_PATH_VOL_CTL];
2527 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2530 sw = path->ctls[NID_PATH_MUTE_CTL];
2531 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2533 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2538 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2540 else if (!spec->multi_cap_vol)
2541 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2543 err = create_multi_cap_vol_ctl(codec);
2552 * add mic boosts if needed
2554 static int parse_mic_boost(struct hda_codec *codec)
2556 struct hda_gen_spec *spec = codec->spec;
2557 struct auto_pin_cfg *cfg = &spec->autocfg;
2561 const char *prev_label = NULL;
2563 for (i = 0; i < cfg->num_inputs; i++) {
2564 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2566 nid = cfg->inputs[i].pin;
2567 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2569 char boost_label[44];
2570 struct nid_path *path;
2573 label = hda_get_autocfg_input_label(codec, cfg, i);
2574 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2575 label = "Headphone Mic";
2576 if (prev_label && !strcmp(label, prev_label))
2582 snprintf(boost_label, sizeof(boost_label),
2583 "%s Boost Volume", label);
2584 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2585 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2586 boost_label, type_idx, val);
2590 path = snd_hda_get_nid_path(codec, nid, 0);
2592 path->ctls[NID_PATH_BOOST_CTL] = val;
2599 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2601 static void parse_digital(struct hda_codec *codec)
2603 struct hda_gen_spec *spec = codec->spec;
2604 struct nid_path *path;
2608 /* support multiple SPDIFs; the secondary is set up as a slave */
2610 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2611 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2612 dig_nid = look_for_dac(codec, pin, true);
2615 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
2618 print_nid_path("digout", path);
2619 path->active = true;
2620 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
2622 spec->multiout.dig_out_nid = dig_nid;
2623 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2625 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2626 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2628 spec->slave_dig_outs[nums - 1] = dig_nid;
2633 if (spec->autocfg.dig_in_pin) {
2634 dig_nid = codec->start_nid;
2635 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2636 unsigned int wcaps = get_wcaps(codec, dig_nid);
2637 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2639 if (!(wcaps & AC_WCAP_DIGITAL))
2641 path = snd_hda_add_new_path(codec,
2642 spec->autocfg.dig_in_pin,
2643 dig_nid, HDA_PARSE_ALL);
2645 print_nid_path("digin", path);
2646 path->active = true;
2647 spec->dig_in_nid = dig_nid;
2648 spec->digin_path = snd_hda_get_path_idx(codec, path);
2657 * input MUX handling
2660 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2662 /* select the given imux item; either unmute exclusively or select the route */
2663 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2666 struct hda_gen_spec *spec = codec->spec;
2667 const struct hda_input_mux *imux;
2668 struct nid_path *path;
2670 imux = &spec->input_mux;
2671 if (!imux->num_items)
2674 if (idx >= imux->num_items)
2675 idx = imux->num_items - 1;
2676 if (spec->cur_mux[adc_idx] == idx)
2679 path = snd_hda_get_nid_path(codec,
2680 spec->imux_pins[spec->cur_mux[adc_idx]],
2681 spec->adc_nids[adc_idx]);
2685 snd_hda_activate_path(codec, path, false, false);
2687 spec->cur_mux[adc_idx] = idx;
2689 if (spec->shared_mic_hp)
2690 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2692 if (spec->dyn_adc_switch)
2693 dyn_adc_pcm_resetup(codec, idx);
2695 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2696 get_adc_nid(codec, adc_idx, idx));
2701 snd_hda_activate_path(codec, path, true, false);
2702 if (spec->cap_sync_hook)
2703 spec->cap_sync_hook(codec);
2709 * Jack detections for HP auto-mute and mic-switch
2712 /* check each pin in the given array; returns true if any of them is plugged */
2713 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2717 for (i = 0; i < num_pins; i++) {
2718 hda_nid_t nid = pins[i];
2721 present |= snd_hda_jack_detect(codec, nid);
2726 /* standard HP/line-out auto-mute helper */
2727 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2728 bool mute, bool hp_out)
2730 struct hda_gen_spec *spec = codec->spec;
2731 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2734 for (i = 0; i < num_pins; i++) {
2735 hda_nid_t nid = pins[i];
2739 /* don't reset VREF value in case it's controlling
2740 * the amp (see alc861_fixup_asus_amp_vref_0f())
2742 if (spec->keep_vref_in_automute) {
2743 val = snd_hda_codec_read(codec, nid, 0,
2744 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2749 snd_hda_set_pin_ctl_cache(codec, nid, val);
2750 set_pin_eapd(codec, nid, !mute);
2754 /* Toggle outputs muting */
2755 void snd_hda_gen_update_outputs(struct hda_codec *codec)
2757 struct hda_gen_spec *spec = codec->spec;
2760 /* Control HP pins/amps depending on master_mute state;
2761 * in general, HP pins/amps control should be enabled in all cases,
2762 * but currently set only for master_mute, just to be safe
2764 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2765 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2766 spec->autocfg.hp_pins, spec->master_mute, true);
2768 if (!spec->automute_speaker)
2771 on = spec->hp_jack_present | spec->line_jack_present;
2772 on |= spec->master_mute;
2773 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2774 spec->autocfg.speaker_pins, on, false);
2776 /* toggle line-out mutes if needed, too */
2777 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2778 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2779 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2781 if (!spec->automute_lo)
2784 on = spec->hp_jack_present;
2785 on |= spec->master_mute;
2786 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2787 spec->autocfg.line_out_pins, on, false);
2789 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
2791 static void call_update_outputs(struct hda_codec *codec)
2793 struct hda_gen_spec *spec = codec->spec;
2794 if (spec->automute_hook)
2795 spec->automute_hook(codec);
2797 snd_hda_gen_update_outputs(codec);
2800 /* standard HP-automute helper */
2801 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2803 struct hda_gen_spec *spec = codec->spec;
2805 spec->hp_jack_present =
2806 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2807 spec->autocfg.hp_pins);
2808 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2810 call_update_outputs(codec);
2812 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
2814 /* standard line-out-automute helper */
2815 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2817 struct hda_gen_spec *spec = codec->spec;
2819 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2821 /* check LO jack only when it's different from HP */
2822 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2825 spec->line_jack_present =
2826 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2827 spec->autocfg.line_out_pins);
2828 if (!spec->automute_speaker || !spec->detect_lo)
2830 call_update_outputs(codec);
2832 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
2834 /* standard mic auto-switch helper */
2835 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
2837 struct hda_gen_spec *spec = codec->spec;
2840 if (!spec->auto_mic)
2843 for (i = spec->am_num_entries - 1; i > 0; i--) {
2844 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2845 mux_select(codec, 0, spec->am_entry[i].idx);
2849 mux_select(codec, 0, spec->am_entry[0].idx);
2851 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
2854 * Auto-Mute mode mixer enum support
2856 static int automute_mode_info(struct snd_kcontrol *kcontrol,
2857 struct snd_ctl_elem_info *uinfo)
2859 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2860 struct hda_gen_spec *spec = codec->spec;
2861 static const char * const texts3[] = {
2862 "Disabled", "Speaker Only", "Line Out+Speaker"
2865 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2866 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2867 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2870 static int automute_mode_get(struct snd_kcontrol *kcontrol,
2871 struct snd_ctl_elem_value *ucontrol)
2873 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2874 struct hda_gen_spec *spec = codec->spec;
2875 unsigned int val = 0;
2876 if (spec->automute_speaker)
2878 if (spec->automute_lo)
2881 ucontrol->value.enumerated.item[0] = val;
2885 static int automute_mode_put(struct snd_kcontrol *kcontrol,
2886 struct snd_ctl_elem_value *ucontrol)
2888 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2889 struct hda_gen_spec *spec = codec->spec;
2891 switch (ucontrol->value.enumerated.item[0]) {
2893 if (!spec->automute_speaker && !spec->automute_lo)
2895 spec->automute_speaker = 0;
2896 spec->automute_lo = 0;
2899 if (spec->automute_speaker_possible) {
2900 if (!spec->automute_lo && spec->automute_speaker)
2902 spec->automute_speaker = 1;
2903 spec->automute_lo = 0;
2904 } else if (spec->automute_lo_possible) {
2905 if (spec->automute_lo)
2907 spec->automute_lo = 1;
2912 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2914 if (spec->automute_speaker && spec->automute_lo)
2916 spec->automute_speaker = 1;
2917 spec->automute_lo = 1;
2922 call_update_outputs(codec);
2926 static const struct snd_kcontrol_new automute_mode_enum = {
2927 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2928 .name = "Auto-Mute Mode",
2929 .info = automute_mode_info,
2930 .get = automute_mode_get,
2931 .put = automute_mode_put,
2934 static int add_automute_mode_enum(struct hda_codec *codec)
2936 struct hda_gen_spec *spec = codec->spec;
2938 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
2944 * Check the availability of HP/line-out auto-mute;
2945 * Set up appropriately if really supported
2947 static int check_auto_mute_availability(struct hda_codec *codec)
2949 struct hda_gen_spec *spec = codec->spec;
2950 struct auto_pin_cfg *cfg = &spec->autocfg;
2954 if (cfg->hp_pins[0])
2956 if (cfg->line_out_pins[0])
2958 if (cfg->speaker_pins[0])
2960 if (present < 2) /* need two different output types */
2963 if (!cfg->speaker_pins[0] &&
2964 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2965 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2966 sizeof(cfg->speaker_pins));
2967 cfg->speaker_outs = cfg->line_outs;
2970 if (!cfg->hp_pins[0] &&
2971 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2972 memcpy(cfg->hp_pins, cfg->line_out_pins,
2973 sizeof(cfg->hp_pins));
2974 cfg->hp_outs = cfg->line_outs;
2977 for (i = 0; i < cfg->hp_outs; i++) {
2978 hda_nid_t nid = cfg->hp_pins[i];
2979 if (!is_jack_detectable(codec, nid))
2981 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2983 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
2984 spec->hp_automute_hook ?
2985 spec->hp_automute_hook :
2986 snd_hda_gen_hp_automute);
2987 spec->detect_hp = 1;
2990 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2991 if (cfg->speaker_outs)
2992 for (i = 0; i < cfg->line_outs; i++) {
2993 hda_nid_t nid = cfg->line_out_pins[i];
2994 if (!is_jack_detectable(codec, nid))
2996 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2997 snd_hda_jack_detect_enable_callback(codec, nid,
2998 HDA_GEN_FRONT_EVENT,
2999 spec->line_automute_hook ?
3000 spec->line_automute_hook :
3001 snd_hda_gen_line_automute);
3002 spec->detect_lo = 1;
3004 spec->automute_lo_possible = spec->detect_hp;
3007 spec->automute_speaker_possible = cfg->speaker_outs &&
3008 (spec->detect_hp || spec->detect_lo);
3010 spec->automute_lo = spec->automute_lo_possible;
3011 spec->automute_speaker = spec->automute_speaker_possible;
3013 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3014 /* create a control for automute mode */
3015 err = add_automute_mode_enum(codec);
3022 /* return the position of NID in the list, or -1 if not found */
3023 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
3026 for (i = 0; i < nums; i++)
3032 /* check whether all auto-mic pins are valid; setup indices if OK */
3033 static bool auto_mic_check_imux(struct hda_codec *codec)
3035 struct hda_gen_spec *spec = codec->spec;
3036 const struct hda_input_mux *imux;
3039 imux = &spec->input_mux;
3040 for (i = 0; i < spec->am_num_entries; i++) {
3041 spec->am_entry[i].idx =
3042 find_idx_in_nid_list(spec->am_entry[i].pin,
3043 spec->imux_pins, imux->num_items);
3044 if (spec->am_entry[i].idx < 0)
3045 return false; /* no corresponding imux */
3048 /* we don't need the jack detection for the first pin */
3049 for (i = 1; i < spec->am_num_entries; i++)
3050 snd_hda_jack_detect_enable_callback(codec,
3051 spec->am_entry[i].pin,
3053 spec->mic_autoswitch_hook ?
3054 spec->mic_autoswitch_hook :
3055 snd_hda_gen_mic_autoswitch);
3059 static int compare_attr(const void *ap, const void *bp)
3061 const struct automic_entry *a = ap;
3062 const struct automic_entry *b = bp;
3063 return (int)(a->attr - b->attr);
3067 * Check the availability of auto-mic switch;
3068 * Set up if really supported
3070 static int check_auto_mic_availability(struct hda_codec *codec)
3072 struct hda_gen_spec *spec = codec->spec;
3073 struct auto_pin_cfg *cfg = &spec->autocfg;
3079 for (i = 0; i < cfg->num_inputs; i++) {
3080 hda_nid_t nid = cfg->inputs[i].pin;
3082 attr = snd_hda_codec_get_pincfg(codec, nid);
3083 attr = snd_hda_get_input_pin_attr(attr);
3084 if (types & (1 << attr))
3085 return 0; /* already occupied */
3087 case INPUT_PIN_ATTR_INT:
3088 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3089 return 0; /* invalid type */
3091 case INPUT_PIN_ATTR_UNUSED:
3092 return 0; /* invalid entry */
3094 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3095 return 0; /* invalid type */
3096 if (!spec->line_in_auto_switch &&
3097 cfg->inputs[i].type != AUTO_PIN_MIC)
3098 return 0; /* only mic is allowed */
3099 if (!is_jack_detectable(codec, nid))
3100 return 0; /* no unsol support */
3103 if (num_pins >= MAX_AUTO_MIC_PINS)
3105 types |= (1 << attr);
3106 spec->am_entry[num_pins].pin = nid;
3107 spec->am_entry[num_pins].attr = attr;
3114 spec->am_num_entries = num_pins;
3115 /* sort the am_entry in the order of attr so that the pin with a
3116 * higher attr will be selected when the jack is plugged.
3118 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3119 compare_attr, NULL);
3121 if (!auto_mic_check_imux(codec))
3125 spec->num_adc_nids = 1;
3126 spec->cur_mux[0] = spec->am_entry[0].idx;
3127 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3128 spec->am_entry[0].pin,
3129 spec->am_entry[1].pin,
3130 spec->am_entry[2].pin);
3137 * Parse the given BIOS configuration and set up the hda_gen_spec
3139 * return 1 if successful, 0 if the proper config is not found,
3140 * or a negative error code
3142 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
3143 struct auto_pin_cfg *cfg)
3145 struct hda_gen_spec *spec = codec->spec;
3148 if (cfg != &spec->autocfg) {
3149 spec->autocfg = *cfg;
3150 cfg = &spec->autocfg;
3153 if (!cfg->line_outs) {
3154 if (cfg->dig_outs || cfg->dig_in_pin) {
3155 spec->multiout.max_channels = 2;
3156 spec->no_analog = 1;
3159 return 0; /* can't find valid BIOS pin config */
3162 if (!spec->no_primary_hp &&
3163 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3164 cfg->line_outs <= cfg->hp_outs) {
3165 /* use HP as primary out */
3166 cfg->speaker_outs = cfg->line_outs;
3167 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3168 sizeof(cfg->speaker_pins));
3169 cfg->line_outs = cfg->hp_outs;
3170 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3172 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3173 cfg->line_out_type = AUTO_PIN_HP_OUT;
3176 err = parse_output_paths(codec);
3179 err = create_multi_channel_mode(codec);
3182 err = create_multi_out_ctls(codec, cfg);
3185 err = create_hp_out_ctls(codec);
3188 err = create_speaker_out_ctls(codec);
3191 err = create_indep_hp_ctls(codec);
3194 err = create_loopback_mixing_ctl(codec);
3197 err = create_shared_input(codec);
3200 err = create_input_ctls(codec);
3204 /* check the multiple speaker pins */
3205 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
3206 spec->const_channel_count = cfg->line_outs * 2;
3208 spec->const_channel_count = cfg->speaker_outs * 2;
3210 if (spec->multi_ios > 0)
3211 spec->multiout.max_channels = max(spec->ext_channel_count,
3212 spec->const_channel_count);
3214 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3216 err = check_auto_mute_availability(codec);
3220 err = check_dyn_adc_switch(codec);
3224 if (!spec->shared_mic_hp) {
3225 err = check_auto_mic_availability(codec);
3230 err = create_capture_mixers(codec);
3234 err = parse_mic_boost(codec);
3239 parse_digital(codec);
3243 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
3247 * Build control elements
3250 /* slave controls for virtual master */
3251 static const char * const slave_pfxs[] = {
3252 "Front", "Surround", "Center", "LFE", "Side",
3253 "Headphone", "Speaker", "Mono", "Line Out",
3254 "CLFE", "Bass Speaker", "PCM",
3258 int snd_hda_gen_build_controls(struct hda_codec *codec)
3260 struct hda_gen_spec *spec = codec->spec;
3263 if (spec->kctls.used) {
3264 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3269 if (spec->multiout.dig_out_nid) {
3270 err = snd_hda_create_dig_out_ctls(codec,
3271 spec->multiout.dig_out_nid,
3272 spec->multiout.dig_out_nid,
3273 spec->pcm_rec[1].pcm_type);
3276 if (!spec->no_analog) {
3277 err = snd_hda_create_spdif_share_sw(codec,
3281 spec->multiout.share_spdif = 1;
3284 if (spec->dig_in_nid) {
3285 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3290 /* if we have no master control, let's create it */
3291 if (!spec->no_analog &&
3292 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3293 unsigned int vmaster_tlv[4];
3294 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3295 HDA_OUTPUT, vmaster_tlv);
3296 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3297 vmaster_tlv, slave_pfxs,
3302 if (!spec->no_analog &&
3303 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3304 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3307 true, &spec->vmaster_mute.sw_kctl);
3310 if (spec->vmaster_mute.hook)
3311 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3312 spec->vmaster_mute_enum);
3315 free_kctls(spec); /* no longer needed */
3317 if (spec->shared_mic_hp) {
3319 int nid = spec->autocfg.inputs[1].pin;
3320 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3323 err = snd_hda_jack_detect_enable(codec, nid, 0);
3328 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3334 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3342 * Analog playback callbacks
3344 static int 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;
3351 mutex_lock(&spec->pcm_mutex);
3352 err = snd_hda_multi_out_analog_open(codec,
3353 &spec->multiout, substream,
3356 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3357 mutex_unlock(&spec->pcm_mutex);
3361 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3362 struct hda_codec *codec,
3363 unsigned int stream_tag,
3364 unsigned int format,
3365 struct snd_pcm_substream *substream)
3367 struct hda_gen_spec *spec = codec->spec;
3368 return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3369 stream_tag, format, substream);
3372 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3373 struct hda_codec *codec,
3374 struct snd_pcm_substream *substream)
3376 struct hda_gen_spec *spec = codec->spec;
3377 return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3380 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3381 struct hda_codec *codec,
3382 struct snd_pcm_substream *substream)
3384 struct hda_gen_spec *spec = codec->spec;
3385 mutex_lock(&spec->pcm_mutex);
3386 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3387 mutex_unlock(&spec->pcm_mutex);
3391 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3392 struct hda_codec *codec,
3393 struct snd_pcm_substream *substream)
3395 struct hda_gen_spec *spec = codec->spec;
3398 mutex_lock(&spec->pcm_mutex);
3399 if (!spec->indep_hp_enabled)
3402 spec->active_streams |= 1 << STREAM_INDEP_HP;
3403 mutex_unlock(&spec->pcm_mutex);
3407 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3408 struct hda_codec *codec,
3409 struct snd_pcm_substream *substream)
3411 struct hda_gen_spec *spec = codec->spec;
3412 mutex_lock(&spec->pcm_mutex);
3413 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3414 mutex_unlock(&spec->pcm_mutex);
3421 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3422 struct hda_codec *codec,
3423 struct snd_pcm_substream *substream)
3425 struct hda_gen_spec *spec = codec->spec;
3426 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3429 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3430 struct hda_codec *codec,
3431 unsigned int stream_tag,
3432 unsigned int format,
3433 struct snd_pcm_substream *substream)
3435 struct hda_gen_spec *spec = codec->spec;
3436 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3437 stream_tag, format, substream);
3440 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3441 struct hda_codec *codec,
3442 struct snd_pcm_substream *substream)
3444 struct hda_gen_spec *spec = codec->spec;
3445 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3448 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3449 struct hda_codec *codec,
3450 struct snd_pcm_substream *substream)
3452 struct hda_gen_spec *spec = codec->spec;
3453 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3459 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3460 struct hda_codec *codec,
3461 unsigned int stream_tag,
3462 unsigned int format,
3463 struct snd_pcm_substream *substream)
3465 struct hda_gen_spec *spec = codec->spec;
3467 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
3468 stream_tag, 0, format);
3472 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3473 struct hda_codec *codec,
3474 struct snd_pcm_substream *substream)
3476 struct hda_gen_spec *spec = codec->spec;
3478 snd_hda_codec_cleanup_stream(codec,
3479 spec->adc_nids[substream->number + 1]);
3485 static const struct hda_pcm_stream pcm_analog_playback = {
3489 /* NID is set in build_pcms */
3491 .open = playback_pcm_open,
3492 .close = playback_pcm_close,
3493 .prepare = playback_pcm_prepare,
3494 .cleanup = playback_pcm_cleanup
3498 static const struct hda_pcm_stream pcm_analog_capture = {
3502 /* NID is set in build_pcms */
3505 static const struct hda_pcm_stream pcm_analog_alt_playback = {
3509 /* NID is set in build_pcms */
3511 .open = alt_playback_pcm_open,
3512 .close = alt_playback_pcm_close
3516 static const struct hda_pcm_stream pcm_analog_alt_capture = {
3517 .substreams = 2, /* can be overridden */
3520 /* NID is set in build_pcms */
3522 .prepare = alt_capture_pcm_prepare,
3523 .cleanup = alt_capture_pcm_cleanup
3527 static const struct hda_pcm_stream pcm_digital_playback = {
3531 /* NID is set in build_pcms */
3533 .open = dig_playback_pcm_open,
3534 .close = dig_playback_pcm_close,
3535 .prepare = dig_playback_pcm_prepare,
3536 .cleanup = dig_playback_pcm_cleanup
3540 static const struct hda_pcm_stream pcm_digital_capture = {
3544 /* NID is set in build_pcms */
3547 /* Used by build_pcms to flag that a PCM has no playback stream */
3548 static const struct hda_pcm_stream pcm_null_stream = {
3555 * dynamic changing ADC PCM streams
3557 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3559 struct hda_gen_spec *spec = codec->spec;
3560 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3562 if (spec->cur_adc && spec->cur_adc != new_adc) {
3563 /* stream is running, let's swap the current ADC */
3564 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3565 spec->cur_adc = new_adc;
3566 snd_hda_codec_setup_stream(codec, new_adc,
3567 spec->cur_adc_stream_tag, 0,
3568 spec->cur_adc_format);
3574 /* analog capture with dynamic dual-adc changes */
3575 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3576 struct hda_codec *codec,
3577 unsigned int stream_tag,
3578 unsigned int format,
3579 struct snd_pcm_substream *substream)
3581 struct hda_gen_spec *spec = codec->spec;
3582 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3583 spec->cur_adc_stream_tag = stream_tag;
3584 spec->cur_adc_format = format;
3585 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3589 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3590 struct hda_codec *codec,
3591 struct snd_pcm_substream *substream)
3593 struct hda_gen_spec *spec = codec->spec;
3594 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3599 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3603 .nid = 0, /* fill later */
3605 .prepare = dyn_adc_capture_pcm_prepare,
3606 .cleanup = dyn_adc_capture_pcm_cleanup
3610 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3611 const char *chip_name)
3617 strlcpy(str, chip_name, len);
3619 /* drop non-alnum chars after a space */
3620 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3621 if (!isalnum(p[1])) {
3626 strlcat(str, sfx, len);
3629 /* build PCM streams based on the parsed results */
3630 int snd_hda_gen_build_pcms(struct hda_codec *codec)
3632 struct hda_gen_spec *spec = codec->spec;
3633 struct hda_pcm *info = spec->pcm_rec;
3634 const struct hda_pcm_stream *p;
3635 bool have_multi_adcs;
3637 codec->num_pcms = 1;
3638 codec->pcm_info = info;
3640 if (spec->no_analog)
3643 fill_pcm_stream_name(spec->stream_name_analog,
3644 sizeof(spec->stream_name_analog),
3645 " Analog", codec->chip_name);
3646 info->name = spec->stream_name_analog;
3648 if (spec->multiout.num_dacs > 0) {
3649 p = spec->stream_analog_playback;
3651 p = &pcm_analog_playback;
3652 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3653 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3654 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3655 spec->multiout.max_channels;
3656 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3657 spec->autocfg.line_outs == 2)
3658 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3661 if (spec->num_adc_nids) {
3662 p = spec->stream_analog_capture;
3664 if (spec->dyn_adc_switch)
3665 p = &dyn_adc_pcm_analog_capture;
3667 p = &pcm_analog_capture;
3669 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3670 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3674 /* SPDIF for stream index #1 */
3675 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
3676 fill_pcm_stream_name(spec->stream_name_digital,
3677 sizeof(spec->stream_name_digital),
3678 " Digital", codec->chip_name);
3679 codec->num_pcms = 2;
3680 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3681 info = spec->pcm_rec + 1;
3682 info->name = spec->stream_name_digital;
3683 if (spec->dig_out_type)
3684 info->pcm_type = spec->dig_out_type;
3686 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3687 if (spec->multiout.dig_out_nid) {
3688 p = spec->stream_digital_playback;
3690 p = &pcm_digital_playback;
3691 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3692 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3694 if (spec->dig_in_nid) {
3695 p = spec->stream_digital_capture;
3697 p = &pcm_digital_capture;
3698 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3699 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3703 if (spec->no_analog)
3706 /* If the use of more than one ADC is requested for the current
3707 * model, configure a second analog capture-only PCM.
3709 have_multi_adcs = (spec->num_adc_nids > 1) &&
3710 !spec->dyn_adc_switch && !spec->auto_mic;
3711 /* Additional Analaog capture for index #2 */
3712 if (spec->alt_dac_nid || have_multi_adcs) {
3713 codec->num_pcms = 3;
3714 info = spec->pcm_rec + 2;
3715 info->name = spec->stream_name_analog;
3716 if (spec->alt_dac_nid) {
3717 p = spec->stream_analog_alt_playback;
3719 p = &pcm_analog_alt_playback;
3720 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3721 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3724 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3726 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3728 if (have_multi_adcs) {
3729 p = spec->stream_analog_alt_capture;
3731 p = &pcm_analog_alt_capture;
3732 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3733 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3735 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3736 spec->num_adc_nids - 1;
3738 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3740 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3746 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3750 * Standard auto-parser initializations
3753 /* configure the path from the given dac to the pin as the proper output */
3754 static void set_output_and_unmute(struct hda_codec *codec, hda_nid_t pin,
3755 int pin_type, int path_idx)
3757 struct nid_path *path;
3759 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3760 path = snd_hda_get_path_from_idx(codec, path_idx);
3763 snd_hda_activate_path(codec, path, path->active, true);
3764 set_pin_eapd(codec, pin, path->active);
3767 /* initialize primary output paths */
3768 static void init_multi_out(struct hda_codec *codec)
3770 struct hda_gen_spec *spec = codec->spec;
3775 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3780 for (i = 0; i < spec->autocfg.line_outs; i++) {
3781 nid = spec->autocfg.line_out_pins[i];
3783 set_output_and_unmute(codec, nid, pin_type,
3784 spec->out_paths[i]);
3789 static void __init_extra_out(struct hda_codec *codec, int num_outs,
3790 hda_nid_t *pins, int *paths, int type)
3795 for (i = 0; i < num_outs; i++) {
3799 set_output_and_unmute(codec, pin, type, paths[i]);
3803 /* initialize hp and speaker paths */
3804 static void init_extra_out(struct hda_codec *codec)
3806 struct hda_gen_spec *spec = codec->spec;
3808 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3809 __init_extra_out(codec, spec->autocfg.hp_outs,
3810 spec->autocfg.hp_pins,
3811 spec->hp_paths, PIN_HP);
3812 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3813 __init_extra_out(codec, spec->autocfg.speaker_outs,
3814 spec->autocfg.speaker_pins,
3815 spec->speaker_paths, PIN_OUT);
3818 /* initialize multi-io paths */
3819 static void init_multi_io(struct hda_codec *codec)
3821 struct hda_gen_spec *spec = codec->spec;
3824 for (i = 0; i < spec->multi_ios; i++) {
3825 hda_nid_t pin = spec->multi_io[i].pin;
3826 struct nid_path *path;
3827 path = get_multiio_path(codec, i);
3830 if (!spec->multi_io[i].ctl_in)
3831 spec->multi_io[i].ctl_in =
3832 snd_hda_codec_update_cache(codec, pin, 0,
3833 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3834 snd_hda_activate_path(codec, path, path->active, true);
3838 /* set up the input pin config, depending on the given auto-pin type */
3839 static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3842 unsigned int val = PIN_IN;
3843 if (auto_pin_type == AUTO_PIN_MIC)
3844 val |= snd_hda_get_default_vref(codec, nid);
3845 snd_hda_set_pin_ctl_cache(codec, nid, val);
3848 /* set up input pins and loopback paths */
3849 static void init_analog_input(struct hda_codec *codec)
3851 struct hda_gen_spec *spec = codec->spec;
3852 struct auto_pin_cfg *cfg = &spec->autocfg;
3855 for (i = 0; i < cfg->num_inputs; i++) {
3856 hda_nid_t nid = cfg->inputs[i].pin;
3857 if (is_input_pin(codec, nid))
3858 set_input_pin(codec, nid, cfg->inputs[i].type);
3860 /* init loopback inputs */
3861 if (spec->mixer_nid) {
3862 struct nid_path *path;
3863 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
3865 snd_hda_activate_path(codec, path,
3866 path->active, false);
3871 /* initialize ADC paths */
3872 static void init_input_src(struct hda_codec *codec)
3874 struct hda_gen_spec *spec = codec->spec;
3875 struct hda_input_mux *imux = &spec->input_mux;
3876 struct nid_path *path;
3879 if (spec->dyn_adc_switch)
3882 nums = spec->num_adc_nids;
3884 for (c = 0; c < nums; c++) {
3885 for (i = 0; i < imux->num_items; i++) {
3886 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3887 get_adc_nid(codec, c, i));
3889 bool active = path->active;
3890 if (i == spec->cur_mux[c])
3892 snd_hda_activate_path(codec, path, active, false);
3897 if (spec->shared_mic_hp)
3898 update_shared_mic_hp(codec, spec->cur_mux[0]);
3900 if (spec->cap_sync_hook)
3901 spec->cap_sync_hook(codec);
3904 /* set right pin controls for digital I/O */
3905 static void init_digital(struct hda_codec *codec)
3907 struct hda_gen_spec *spec = codec->spec;
3911 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3912 pin = spec->autocfg.dig_out_pins[i];
3915 set_output_and_unmute(codec, pin, PIN_OUT,
3916 spec->digout_paths[i]);
3918 pin = spec->autocfg.dig_in_pin;
3920 struct nid_path *path;
3921 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
3922 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
3924 snd_hda_activate_path(codec, path, path->active, false);
3928 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3929 * invalid unsol tags by some reason
3931 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3935 for (i = 0; i < codec->init_pins.used; i++) {
3936 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3937 hda_nid_t nid = pin->nid;
3938 if (is_jack_detectable(codec, nid) &&
3939 !snd_hda_jack_tbl_get(codec, nid))
3940 snd_hda_codec_update_cache(codec, nid, 0,
3941 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3945 int snd_hda_gen_init(struct hda_codec *codec)
3947 struct hda_gen_spec *spec = codec->spec;
3949 if (spec->init_hook)
3950 spec->init_hook(codec);
3952 snd_hda_apply_verbs(codec);
3954 codec->cached_write = 1;
3956 init_multi_out(codec);
3957 init_extra_out(codec);
3958 init_multi_io(codec);
3959 init_analog_input(codec);
3960 init_input_src(codec);
3961 init_digital(codec);
3963 clear_unsol_on_unused_pins(codec);
3965 /* call init functions of standard auto-mute helpers */
3966 snd_hda_gen_hp_automute(codec, NULL);
3967 snd_hda_gen_line_automute(codec, NULL);
3968 snd_hda_gen_mic_autoswitch(codec, NULL);
3970 snd_hda_codec_flush_amp_cache(codec);
3971 snd_hda_codec_flush_cmd_cache(codec);
3973 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3974 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3976 hda_call_check_power_status(codec, 0x01);
3979 EXPORT_SYMBOL(snd_hda_gen_init);
3983 * the generic codec support
3987 static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3989 struct hda_gen_spec *spec = codec->spec;
3990 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3994 static void generic_free(struct hda_codec *codec)
3996 snd_hda_gen_spec_free(codec->spec);
4001 static const struct hda_codec_ops generic_patch_ops = {
4002 .build_controls = snd_hda_gen_build_controls,
4003 .build_pcms = snd_hda_gen_build_pcms,
4004 .init = snd_hda_gen_init,
4005 .free = generic_free,
4006 .unsol_event = snd_hda_jack_unsol_event,
4008 .check_power_status = generic_check_power_status,
4012 int snd_hda_parse_generic_codec(struct hda_codec *codec)
4014 struct hda_gen_spec *spec;
4017 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4020 snd_hda_gen_spec_init(spec);
4023 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4027 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
4031 codec->patch_ops = generic_patch_ops;
4035 generic_free(codec);
4038 EXPORT_SYMBOL(snd_hda_parse_generic_codec);