2 * Universal Interface for Intel High Definition Audio Codec
4 * Generic widget tree parser
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/sort.h>
27 #include <linux/ctype.h>
28 #include <linux/string.h>
29 #include <sound/core.h>
30 #include <sound/jack.h>
31 #include "hda_codec.h"
32 #include "hda_local.h"
33 #include "hda_auto_parser.h"
35 #include "hda_generic.h"
38 /* initialize hda_gen_spec struct */
39 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
41 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
42 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
43 mutex_init(&spec->pcm_mutex);
46 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
48 struct snd_kcontrol_new *
49 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
50 const struct snd_kcontrol_new *temp)
52 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
57 knew->name = kstrdup(name, GFP_KERNEL);
59 knew->name = kstrdup(knew->name, GFP_KERNEL);
64 EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
66 static void free_kctls(struct hda_gen_spec *spec)
68 if (spec->kctls.list) {
69 struct snd_kcontrol_new *kctl = spec->kctls.list;
71 for (i = 0; i < spec->kctls.used; i++)
74 snd_array_free(&spec->kctls);
77 void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
82 snd_array_free(&spec->paths);
84 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
87 * pin control value accesses
90 #define update_pin_ctl(codec, pin, val) \
91 snd_hda_codec_update_cache(codec, pin, 0, \
92 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
94 /* restore the pinctl based on the cached value */
95 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
97 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
100 /* set the pinctl target value and write it if requested */
101 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
102 unsigned int val, bool do_write)
106 val = snd_hda_correct_pin_ctl(codec, pin, val);
107 snd_hda_codec_set_pin_target(codec, pin, val);
109 update_pin_ctl(codec, pin, val);
112 /* set pinctl target values for all given pins */
113 static void set_pin_targets(struct hda_codec *codec, int num_pins,
114 hda_nid_t *pins, unsigned int val)
117 for (i = 0; i < num_pins; i++)
118 set_pin_target(codec, pins[i], val, false);
125 /* return the position of NID in the list, or -1 if not found */
126 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
129 for (i = 0; i < nums; i++)
135 /* return true if the given NID is contained in the path */
136 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
138 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
141 static struct nid_path *get_nid_path(struct hda_codec *codec,
142 hda_nid_t from_nid, hda_nid_t to_nid,
145 struct hda_gen_spec *spec = codec->spec;
148 for (i = 0; i < spec->paths.used; i++) {
149 struct nid_path *path = snd_array_elem(&spec->paths, i);
150 if (path->depth <= 0)
152 if ((!from_nid || path->path[0] == from_nid) &&
153 (!to_nid || path->path[path->depth - 1] == to_nid)) {
155 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
156 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
163 /* get the path between the given NIDs;
164 * passing 0 to either @pin or @dac behaves as a wildcard
166 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
167 hda_nid_t from_nid, hda_nid_t to_nid)
169 return get_nid_path(codec, from_nid, to_nid, 0);
171 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
173 /* get the index number corresponding to the path instance;
174 * the index starts from 1, for easier checking the invalid value
176 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
178 struct hda_gen_spec *spec = codec->spec;
179 struct nid_path *array = spec->paths.list;
182 if (!spec->paths.used)
185 if (idx < 0 || idx >= spec->paths.used)
190 /* get the path instance corresponding to the given index number */
191 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
193 struct hda_gen_spec *spec = codec->spec;
195 if (idx <= 0 || idx > spec->paths.used)
197 return snd_array_elem(&spec->paths, idx - 1);
200 /* check whether the given DAC is already found in any existing paths */
201 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
203 struct hda_gen_spec *spec = codec->spec;
206 for (i = 0; i < spec->paths.used; i++) {
207 struct nid_path *path = snd_array_elem(&spec->paths, i);
208 if (path->path[0] == nid)
214 /* check whether the given two widgets can be connected */
215 static bool is_reachable_path(struct hda_codec *codec,
216 hda_nid_t from_nid, hda_nid_t to_nid)
218 if (!from_nid || !to_nid)
220 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
223 /* nid, dir and idx */
224 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
226 /* check whether the given ctl is already assigned in any path elements */
227 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
229 struct hda_gen_spec *spec = codec->spec;
232 val &= AMP_VAL_COMPARE_MASK;
233 for (i = 0; i < spec->paths.used; i++) {
234 struct nid_path *path = snd_array_elem(&spec->paths, i);
235 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
241 /* check whether a control with the given (nid, dir, idx) was assigned */
242 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
245 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
246 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
247 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
250 static void print_nid_path(const char *pfx, struct nid_path *path)
257 for (i = 0; i < path->depth; i++) {
259 sprintf(tmp, ":%02x", path->path[i]);
260 strlcat(buf, tmp, sizeof(buf));
262 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
265 /* called recursively */
266 static bool __parse_nid_path(struct hda_codec *codec,
267 hda_nid_t from_nid, hda_nid_t to_nid,
268 int anchor_nid, struct nid_path *path,
271 const hda_nid_t *conn;
274 if (to_nid == anchor_nid)
275 anchor_nid = 0; /* anchor passed */
276 else if (to_nid == (hda_nid_t)(-anchor_nid))
277 return false; /* hit the exclusive nid */
279 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
280 for (i = 0; i < nums; i++) {
281 if (conn[i] != from_nid) {
282 /* special case: when from_nid is 0,
283 * try to find an empty DAC
286 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
287 is_dac_already_used(codec, conn[i]))
290 /* anchor is not requested or already passed? */
294 if (depth >= MAX_NID_PATH_DEPTH)
296 for (i = 0; i < nums; i++) {
298 type = get_wcaps_type(get_wcaps(codec, conn[i]));
299 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
302 if (__parse_nid_path(codec, from_nid, conn[i],
303 anchor_nid, path, depth + 1))
309 path->path[path->depth] = conn[i];
310 path->idx[path->depth + 1] = i;
311 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
312 path->multi[path->depth + 1] = 1;
317 /* parse the widget path from the given nid to the target nid;
318 * when @from_nid is 0, try to find an empty DAC;
319 * when @anchor_nid is set to a positive value, only paths through the widget
320 * with the given value are evaluated.
321 * when @anchor_nid is set to a negative value, paths through the widget
322 * with the negative of given value are excluded, only other paths are chosen.
323 * when @anchor_nid is zero, no special handling about path selection.
325 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
326 hda_nid_t to_nid, int anchor_nid,
327 struct nid_path *path)
329 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
330 path->path[path->depth] = to_nid;
336 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
339 * parse the path between the given NIDs and add to the path list.
340 * if no valid path is found, return NULL
343 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
344 hda_nid_t to_nid, int anchor_nid)
346 struct hda_gen_spec *spec = codec->spec;
347 struct nid_path *path;
349 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
352 /* check whether the path has been already added */
353 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
357 path = snd_array_new(&spec->paths);
360 memset(path, 0, sizeof(*path));
361 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
367 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
369 /* clear the given path as invalid so that it won't be picked up later */
370 static void invalidate_nid_path(struct hda_codec *codec, int idx)
372 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
375 memset(path, 0, sizeof(*path));
378 /* look for an empty DAC slot */
379 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
382 struct hda_gen_spec *spec = codec->spec;
386 for (i = 0; i < spec->num_all_dacs; i++) {
387 hda_nid_t nid = spec->all_dacs[i];
388 if (!nid || is_dac_already_used(codec, nid))
390 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
391 if (is_digital != cap_digital)
393 if (is_reachable_path(codec, nid, pin))
399 /* replace the channels in the composed amp value with the given number */
400 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
402 val &= ~(0x3U << 16);
407 /* check whether the widget has the given amp capability for the direction */
408 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
409 int dir, unsigned int bits)
413 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
414 if (query_amp_caps(codec, nid, dir) & bits)
419 #define nid_has_mute(codec, nid, dir) \
420 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
421 #define nid_has_volume(codec, nid, dir) \
422 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
424 /* look for a widget suitable for assigning a mute switch in the path */
425 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
426 struct nid_path *path)
430 for (i = path->depth - 1; i >= 0; i--) {
431 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
432 return path->path[i];
433 if (i != path->depth - 1 && i != 0 &&
434 nid_has_mute(codec, path->path[i], HDA_INPUT))
435 return path->path[i];
440 /* look for a widget suitable for assigning a volume ctl in the path */
441 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
442 struct nid_path *path)
446 for (i = path->depth - 1; i >= 0; i--) {
447 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
448 return path->path[i];
454 * path activation / deactivation
457 /* can have the amp-in capability? */
458 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
460 hda_nid_t nid = path->path[idx];
461 unsigned int caps = get_wcaps(codec, nid);
462 unsigned int type = get_wcaps_type(caps);
464 if (!(caps & AC_WCAP_IN_AMP))
466 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
471 /* can have the amp-out capability? */
472 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
474 hda_nid_t nid = path->path[idx];
475 unsigned int caps = get_wcaps(codec, nid);
476 unsigned int type = get_wcaps_type(caps);
478 if (!(caps & AC_WCAP_OUT_AMP))
480 if (type == AC_WID_PIN && !idx) /* only for output pins */
485 /* check whether the given (nid,dir,idx) is active */
486 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
487 unsigned int idx, unsigned int dir)
489 struct hda_gen_spec *spec = codec->spec;
492 for (n = 0; n < spec->paths.used; n++) {
493 struct nid_path *path = snd_array_elem(&spec->paths, n);
496 for (i = 0; i < path->depth; i++) {
497 if (path->path[i] == nid) {
498 if (dir == HDA_OUTPUT || path->idx[i] == idx)
507 /* get the default amp value for the target state */
508 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
509 int dir, bool enable)
512 unsigned int val = 0;
514 caps = query_amp_caps(codec, nid, dir);
515 if (caps & AC_AMPCAP_NUM_STEPS) {
518 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
520 if (caps & AC_AMPCAP_MUTE) {
527 /* initialize the amp value (only at the first time) */
528 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
530 int val = get_amp_val_to_activate(codec, nid, dir, false);
531 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
534 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
535 int idx, bool enable)
538 if (is_ctl_associated(codec, nid, dir, idx) ||
539 (!enable && is_active_nid(codec, nid, dir, idx)))
541 val = get_amp_val_to_activate(codec, nid, dir, enable);
542 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
545 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
548 hda_nid_t nid = path->path[i];
549 init_amp(codec, nid, HDA_OUTPUT, 0);
550 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
553 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
554 int i, bool enable, bool add_aamix)
556 struct hda_gen_spec *spec = codec->spec;
557 const hda_nid_t *conn;
560 hda_nid_t nid = path->path[i];
562 nums = snd_hda_get_conn_list(codec, nid, &conn);
563 type = get_wcaps_type(get_wcaps(codec, nid));
564 if (type == AC_WID_PIN ||
565 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
571 for (n = 0; n < nums; n++)
572 init_amp(codec, nid, HDA_INPUT, n);
574 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
577 /* here is a little bit tricky in comparison with activate_amp_out();
578 * when aa-mixer is available, we need to enable the path as well
580 for (n = 0; n < nums; n++) {
581 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
583 activate_amp(codec, nid, HDA_INPUT, n, enable);
587 /* activate or deactivate the given path
588 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
590 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
591 bool enable, bool add_aamix)
596 path->active = false;
598 for (i = path->depth - 1; i >= 0; i--) {
599 if (enable && path->multi[i])
600 snd_hda_codec_write_cache(codec, path->path[i], 0,
601 AC_VERB_SET_CONNECT_SEL,
603 if (has_amp_in(codec, path, i))
604 activate_amp_in(codec, path, i, enable, add_aamix);
605 if (has_amp_out(codec, path, i))
606 activate_amp_out(codec, path, i, enable);
612 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
614 /* turn on/off EAPD on the given pin */
615 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
617 struct hda_gen_spec *spec = codec->spec;
618 if (spec->own_eapd_ctl ||
619 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
623 snd_hda_codec_update_cache(codec, pin, 0,
624 AC_VERB_SET_EAPD_BTLENABLE,
625 enable ? 0x02 : 0x00);
630 * Helper functions for creating mixer ctl elements
638 static const struct snd_kcontrol_new control_templates[] = {
639 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
640 HDA_CODEC_MUTE(NULL, 0, 0, 0),
641 HDA_BIND_MUTE(NULL, 0, 0, 0),
644 /* add dynamic controls from template */
645 static int add_control(struct hda_gen_spec *spec, int type, const char *name,
646 int cidx, unsigned long val)
648 struct snd_kcontrol_new *knew;
650 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
654 if (get_amp_nid_(val))
655 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
656 knew->private_value = val;
660 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
661 const char *pfx, const char *dir,
662 const char *sfx, int cidx, unsigned long val)
665 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
666 return add_control(spec, type, name, cidx, val);
669 #define add_pb_vol_ctrl(spec, type, pfx, val) \
670 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
671 #define add_pb_sw_ctrl(spec, type, pfx, val) \
672 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
673 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
674 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
675 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
676 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
678 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
679 unsigned int chs, struct nid_path *path)
684 val = path->ctls[NID_PATH_VOL_CTL];
687 val = amp_val_replace_channels(val, chs);
688 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
691 /* return the channel bits suitable for the given path->ctls[] */
692 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
695 int chs = 1; /* mono (left only) */
697 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
698 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
699 chs = 3; /* stereo */
704 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
705 struct nid_path *path)
707 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
708 return add_vol_ctl(codec, pfx, cidx, chs, path);
711 /* create a mute-switch for the given mixer widget;
712 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
714 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
715 unsigned int chs, struct nid_path *path)
718 int type = HDA_CTL_WIDGET_MUTE;
722 val = path->ctls[NID_PATH_MUTE_CTL];
725 val = amp_val_replace_channels(val, chs);
726 if (get_amp_direction_(val) == HDA_INPUT) {
727 hda_nid_t nid = get_amp_nid_(val);
728 int nums = snd_hda_get_num_conns(codec, nid);
730 type = HDA_CTL_BIND_MUTE;
734 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
737 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
738 int cidx, struct nid_path *path)
740 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
741 return add_sw_ctl(codec, pfx, cidx, chs, path);
744 static const char * const channel_name[4] = {
745 "Front", "Surround", "CLFE", "Side"
748 /* give some appropriate ctl name prefix for the given line out channel */
749 static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
750 bool can_be_master, int *index)
752 struct auto_pin_cfg *cfg = &spec->autocfg;
755 if (cfg->line_outs == 1 && !spec->multi_ios &&
756 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
757 return spec->vmaster_mute.hook ? "PCM" : "Master";
759 /* if there is really a single DAC used in the whole output paths,
760 * use it master (or "PCM" if a vmaster hook is present)
762 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
763 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
764 return spec->vmaster_mute.hook ? "PCM" : "Master";
766 switch (cfg->line_out_type) {
767 case AUTO_PIN_SPEAKER_OUT:
768 if (cfg->line_outs == 1)
770 if (cfg->line_outs == 2)
771 return ch ? "Bass Speaker" : "Speaker";
773 case AUTO_PIN_HP_OUT:
774 /* for multi-io case, only the primary out */
775 if (ch && spec->multi_ios)
780 if (cfg->line_outs == 1 && !spec->multi_ios)
784 if (ch >= ARRAY_SIZE(channel_name)) {
789 return channel_name[ch];
796 /* badness definition */
798 /* No primary DAC is found for the main output */
799 BAD_NO_PRIMARY_DAC = 0x10000,
800 /* No DAC is found for the extra output */
802 /* No possible multi-ios */
803 BAD_MULTI_IO = 0x103,
804 /* No individual DAC for extra output */
805 BAD_NO_EXTRA_DAC = 0x102,
806 /* No individual DAC for extra surrounds */
807 BAD_NO_EXTRA_SURR_DAC = 0x101,
808 /* Primary DAC shared with main surrounds */
809 BAD_SHARED_SURROUND = 0x100,
810 /* Primary DAC shared with main CLFE */
811 BAD_SHARED_CLFE = 0x10,
812 /* Primary DAC shared with extra surrounds */
813 BAD_SHARED_EXTRA_SURROUND = 0x10,
814 /* Volume widget is shared */
815 BAD_SHARED_VOL = 0x10,
818 /* look for widgets in the given path which are appropriate for
819 * volume and mute controls, and assign the values to ctls[].
821 * When no appropriate widget is found in the path, the badness value
822 * is incremented depending on the situation. The function returns the
823 * total badness for both volume and mute controls.
825 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
832 return BAD_SHARED_VOL * 2;
834 if (path->ctls[NID_PATH_VOL_CTL] ||
835 path->ctls[NID_PATH_MUTE_CTL])
836 return 0; /* already evaluated */
838 nid = look_for_out_vol_nid(codec, path);
840 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
841 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
842 badness += BAD_SHARED_VOL;
844 path->ctls[NID_PATH_VOL_CTL] = val;
846 badness += BAD_SHARED_VOL;
847 nid = look_for_out_mute_nid(codec, path);
849 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
850 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
851 nid_has_mute(codec, nid, HDA_OUTPUT))
852 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
854 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
855 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
856 badness += BAD_SHARED_VOL;
858 path->ctls[NID_PATH_MUTE_CTL] = val;
860 badness += BAD_SHARED_VOL;
864 struct badness_table {
865 int no_primary_dac; /* no primary DAC */
866 int no_dac; /* no secondary DACs */
867 int shared_primary; /* primary DAC is shared with main output */
868 int shared_surr; /* secondary DAC shared with main or primary */
869 int shared_clfe; /* third DAC shared with main or primary */
870 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
873 static struct badness_table main_out_badness = {
874 .no_primary_dac = BAD_NO_PRIMARY_DAC,
875 .no_dac = BAD_NO_DAC,
876 .shared_primary = BAD_NO_PRIMARY_DAC,
877 .shared_surr = BAD_SHARED_SURROUND,
878 .shared_clfe = BAD_SHARED_CLFE,
879 .shared_surr_main = BAD_SHARED_SURROUND,
882 static struct badness_table extra_out_badness = {
883 .no_primary_dac = BAD_NO_DAC,
884 .no_dac = BAD_NO_DAC,
885 .shared_primary = BAD_NO_EXTRA_DAC,
886 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
887 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
888 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
891 /* get the DAC of the primary output corresponding to the given array index */
892 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
894 struct hda_gen_spec *spec = codec->spec;
895 struct auto_pin_cfg *cfg = &spec->autocfg;
897 if (cfg->line_outs > idx)
898 return spec->private_dac_nids[idx];
899 idx -= cfg->line_outs;
900 if (spec->multi_ios > idx)
901 return spec->multi_io[idx].dac;
905 /* return the DAC if it's reachable, otherwise zero */
906 static inline hda_nid_t try_dac(struct hda_codec *codec,
907 hda_nid_t dac, hda_nid_t pin)
909 return is_reachable_path(codec, dac, pin) ? dac : 0;
912 /* try to assign DACs to pins and return the resultant badness */
913 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
914 const hda_nid_t *pins, hda_nid_t *dacs,
916 const struct badness_table *bad)
918 struct hda_gen_spec *spec = codec->spec;
926 for (i = 0; i < num_outs; i++) {
927 struct nid_path *path;
928 hda_nid_t pin = pins[i];
930 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
932 badness += assign_out_path_ctls(codec, path);
936 dacs[i] = look_for_dac(codec, pin, false);
937 if (!dacs[i] && !i) {
938 /* try to steal the DAC of surrounds for the front */
939 for (j = 1; j < num_outs; j++) {
940 if (is_reachable_path(codec, dacs[j], pin)) {
943 invalidate_nid_path(codec, path_idx[j]);
952 dac = try_dac(codec, get_primary_out(codec, i), pin);
954 dac = try_dac(codec, dacs[0], pin);
956 dac = try_dac(codec, get_primary_out(codec, i), pin);
959 badness += bad->shared_primary;
961 badness += bad->shared_surr;
963 badness += bad->shared_clfe;
964 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
965 dac = spec->private_dac_nids[0];
966 badness += bad->shared_surr_main;
968 badness += bad->no_primary_dac;
970 badness += bad->no_dac;
972 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
973 if (!path && !i && spec->mixer_nid) {
975 path = snd_hda_add_new_path(codec, dac, pin, 0);
980 print_nid_path("output", path);
982 path_idx[i] = snd_hda_get_path_idx(codec, path);
983 badness += assign_out_path_ctls(codec, path);
990 /* return NID if the given pin has only a single connection to a certain DAC */
991 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
993 struct hda_gen_spec *spec = codec->spec;
995 hda_nid_t nid_found = 0;
997 for (i = 0; i < spec->num_all_dacs; i++) {
998 hda_nid_t nid = spec->all_dacs[i];
999 if (!nid || is_dac_already_used(codec, nid))
1001 if (is_reachable_path(codec, nid, pin)) {
1010 /* check whether the given pin can be a multi-io pin */
1011 static bool can_be_multiio_pin(struct hda_codec *codec,
1012 unsigned int location, hda_nid_t nid)
1014 unsigned int defcfg, caps;
1016 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1017 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1019 if (location && get_defcfg_location(defcfg) != location)
1021 caps = snd_hda_query_pin_caps(codec, nid);
1022 if (!(caps & AC_PINCAP_OUT))
1027 /* count the number of input pins that are capable to be multi-io */
1028 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1030 struct hda_gen_spec *spec = codec->spec;
1031 struct auto_pin_cfg *cfg = &spec->autocfg;
1032 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1033 unsigned int location = get_defcfg_location(defcfg);
1037 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1038 for (i = 0; i < cfg->num_inputs; i++) {
1039 if (cfg->inputs[i].type != type)
1041 if (can_be_multiio_pin(codec, location,
1042 cfg->inputs[i].pin))
1052 * When hardwired is set, try to fill ony hardwired pins, and returns
1053 * zero if any pins are filled, non-zero if nothing found.
1054 * When hardwired is off, try to fill possible input pins, and returns
1055 * the badness value.
1057 static int fill_multi_ios(struct hda_codec *codec,
1058 hda_nid_t reference_pin,
1061 struct hda_gen_spec *spec = codec->spec;
1062 struct auto_pin_cfg *cfg = &spec->autocfg;
1063 int type, i, j, num_pins, old_pins;
1064 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1065 unsigned int location = get_defcfg_location(defcfg);
1067 struct nid_path *path;
1069 old_pins = spec->multi_ios;
1073 num_pins = count_multiio_pins(codec, reference_pin);
1077 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1078 for (i = 0; i < cfg->num_inputs; i++) {
1079 hda_nid_t nid = cfg->inputs[i].pin;
1082 if (cfg->inputs[i].type != type)
1084 if (!can_be_multiio_pin(codec, location, nid))
1086 for (j = 0; j < spec->multi_ios; j++) {
1087 if (nid == spec->multi_io[j].pin)
1090 if (j < spec->multi_ios)
1094 dac = get_dac_if_single(codec, nid);
1096 dac = look_for_dac(codec, nid, false);
1101 path = snd_hda_add_new_path(codec, dac, nid,
1107 print_nid_path("multiio", path);
1108 spec->multi_io[spec->multi_ios].pin = nid;
1109 spec->multi_io[spec->multi_ios].dac = dac;
1110 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1111 snd_hda_get_path_idx(codec, path);
1113 if (spec->multi_ios >= 2)
1119 badness = BAD_MULTI_IO;
1120 if (old_pins == spec->multi_ios) {
1122 return 1; /* nothing found */
1124 return badness; /* no badness if nothing found */
1126 if (!hardwired && spec->multi_ios < 2) {
1127 /* cancel newly assigned paths */
1128 spec->paths.used -= spec->multi_ios - old_pins;
1129 spec->multi_ios = old_pins;
1133 /* assign volume and mute controls */
1134 for (i = old_pins; i < spec->multi_ios; i++) {
1135 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1136 badness += assign_out_path_ctls(codec, path);
1142 /* map DACs for all pins in the list if they are single connections */
1143 static bool map_singles(struct hda_codec *codec, int outs,
1144 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1146 struct hda_gen_spec *spec = codec->spec;
1149 for (i = 0; i < outs; i++) {
1150 struct nid_path *path;
1154 dac = get_dac_if_single(codec, pins[i]);
1157 path = snd_hda_add_new_path(codec, dac, pins[i],
1159 if (!path && !i && spec->mixer_nid)
1160 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1164 print_nid_path("output", path);
1165 path->active = true;
1166 path_idx[i] = snd_hda_get_path_idx(codec, path);
1172 /* create a new path including aamix if available, and return its index */
1173 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1175 struct hda_gen_spec *spec = codec->spec;
1176 struct nid_path *path;
1178 path = snd_hda_get_path_from_idx(codec, path_idx);
1179 if (!path || !path->depth ||
1180 is_nid_contained(path, spec->mixer_nid))
1182 path = snd_hda_add_new_path(codec, path->path[0],
1183 path->path[path->depth - 1],
1187 print_nid_path("output-aamix", path);
1188 path->active = false; /* unused as default */
1189 return snd_hda_get_path_idx(codec, path);
1192 /* fill the empty entries in the dac array for speaker/hp with the
1193 * shared dac pointed by the paths
1195 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1196 hda_nid_t *dacs, int *path_idx)
1198 struct nid_path *path;
1201 for (i = 0; i < num_outs; i++) {
1204 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1207 dacs[i] = path->path[0];
1211 /* fill in the dac_nids table from the parsed pin configuration */
1212 static int fill_and_eval_dacs(struct hda_codec *codec,
1213 bool fill_hardwired,
1214 bool fill_mio_first)
1216 struct hda_gen_spec *spec = codec->spec;
1217 struct auto_pin_cfg *cfg = &spec->autocfg;
1218 int i, err, badness;
1220 /* set num_dacs once to full for look_for_dac() */
1221 spec->multiout.num_dacs = cfg->line_outs;
1222 spec->multiout.dac_nids = spec->private_dac_nids;
1223 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1224 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1225 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1226 spec->multi_ios = 0;
1227 snd_array_free(&spec->paths);
1229 /* clear path indices */
1230 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1231 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1232 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1233 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1234 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1235 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1236 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1237 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1241 /* fill hard-wired DACs first */
1242 if (fill_hardwired) {
1245 mapped = map_singles(codec, cfg->line_outs,
1247 spec->private_dac_nids,
1249 mapped |= map_singles(codec, cfg->hp_outs,
1251 spec->multiout.hp_out_nid,
1253 mapped |= map_singles(codec, cfg->speaker_outs,
1255 spec->multiout.extra_out_nid,
1256 spec->speaker_paths);
1257 if (fill_mio_first && cfg->line_outs == 1 &&
1258 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1259 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1266 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1267 spec->private_dac_nids, spec->out_paths,
1270 if (fill_mio_first &&
1271 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1272 /* try to fill multi-io first */
1273 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1276 /* we don't count badness at this stage yet */
1279 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1280 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1281 spec->multiout.hp_out_nid,
1283 &extra_out_badness);
1288 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1289 err = try_assign_dacs(codec, cfg->speaker_outs,
1291 spec->multiout.extra_out_nid,
1292 spec->speaker_paths,
1293 &extra_out_badness);
1298 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1299 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1305 if (spec->mixer_nid) {
1306 spec->aamix_out_paths[0] =
1307 check_aamix_out_path(codec, spec->out_paths[0]);
1308 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1309 spec->aamix_out_paths[1] =
1310 check_aamix_out_path(codec, spec->hp_paths[0]);
1311 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1312 spec->aamix_out_paths[2] =
1313 check_aamix_out_path(codec, spec->speaker_paths[0]);
1316 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1317 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1318 spec->multi_ios = 1; /* give badness */
1320 /* re-count num_dacs and squash invalid entries */
1321 spec->multiout.num_dacs = 0;
1322 for (i = 0; i < cfg->line_outs; i++) {
1323 if (spec->private_dac_nids[i])
1324 spec->multiout.num_dacs++;
1326 memmove(spec->private_dac_nids + i,
1327 spec->private_dac_nids + i + 1,
1328 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1329 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1333 spec->ext_channel_count = spec->min_channel_count =
1334 spec->multiout.num_dacs;
1336 if (spec->multi_ios == 2) {
1337 for (i = 0; i < 2; i++)
1338 spec->private_dac_nids[spec->multiout.num_dacs++] =
1339 spec->multi_io[i].dac;
1340 } else if (spec->multi_ios) {
1341 spec->multi_ios = 0;
1342 badness += BAD_MULTI_IO;
1345 /* re-fill the shared DAC for speaker / headphone */
1346 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1347 refill_shared_dacs(codec, cfg->hp_outs,
1348 spec->multiout.hp_out_nid,
1350 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1351 refill_shared_dacs(codec, cfg->speaker_outs,
1352 spec->multiout.extra_out_nid,
1353 spec->speaker_paths);
1355 /* set initial pinctl targets */
1356 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins,
1357 cfg->line_out_type == AUTO_PIN_HP_OUT ? PIN_HP : PIN_OUT);
1358 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1359 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1360 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1361 set_pin_targets(codec, cfg->speaker_outs,
1362 cfg->speaker_pins, PIN_OUT);
1367 #define DEBUG_BADNESS
1369 #ifdef DEBUG_BADNESS
1370 #define debug_badness snd_printdd
1372 #define debug_badness(...)
1375 static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1377 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1378 cfg->line_out_pins[0], cfg->line_out_pins[1],
1379 cfg->line_out_pins[2], cfg->line_out_pins[3],
1380 spec->multiout.dac_nids[0],
1381 spec->multiout.dac_nids[1],
1382 spec->multiout.dac_nids[2],
1383 spec->multiout.dac_nids[3]);
1384 if (spec->multi_ios > 0)
1385 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1387 spec->multi_io[0].pin, spec->multi_io[1].pin,
1388 spec->multi_io[0].dac, spec->multi_io[1].dac);
1389 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1390 cfg->hp_pins[0], cfg->hp_pins[1],
1391 cfg->hp_pins[2], cfg->hp_pins[3],
1392 spec->multiout.hp_out_nid[0],
1393 spec->multiout.hp_out_nid[1],
1394 spec->multiout.hp_out_nid[2],
1395 spec->multiout.hp_out_nid[3]);
1396 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1397 cfg->speaker_pins[0], cfg->speaker_pins[1],
1398 cfg->speaker_pins[2], cfg->speaker_pins[3],
1399 spec->multiout.extra_out_nid[0],
1400 spec->multiout.extra_out_nid[1],
1401 spec->multiout.extra_out_nid[2],
1402 spec->multiout.extra_out_nid[3]);
1405 /* find all available DACs of the codec */
1406 static void fill_all_dac_nids(struct hda_codec *codec)
1408 struct hda_gen_spec *spec = codec->spec;
1410 hda_nid_t nid = codec->start_nid;
1412 spec->num_all_dacs = 0;
1413 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1414 for (i = 0; i < codec->num_nodes; i++, nid++) {
1415 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1417 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1418 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1421 spec->all_dacs[spec->num_all_dacs++] = nid;
1425 static int parse_output_paths(struct hda_codec *codec)
1427 struct hda_gen_spec *spec = codec->spec;
1428 struct auto_pin_cfg *cfg = &spec->autocfg;
1429 struct auto_pin_cfg *best_cfg;
1430 int best_badness = INT_MAX;
1432 bool fill_hardwired = true, fill_mio_first = true;
1433 bool best_wired = true, best_mio = true;
1434 bool hp_spk_swapped = false;
1436 fill_all_dac_nids(codec);
1438 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1444 badness = fill_and_eval_dacs(codec, fill_hardwired,
1450 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1451 cfg->line_out_type, fill_hardwired, fill_mio_first,
1453 debug_show_configs(spec, cfg);
1454 if (badness < best_badness) {
1455 best_badness = badness;
1457 best_wired = fill_hardwired;
1458 best_mio = fill_mio_first;
1462 fill_mio_first = !fill_mio_first;
1463 if (!fill_mio_first)
1465 fill_hardwired = !fill_hardwired;
1466 if (!fill_hardwired)
1470 hp_spk_swapped = true;
1471 if (cfg->speaker_outs > 0 &&
1472 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1473 cfg->hp_outs = cfg->line_outs;
1474 memcpy(cfg->hp_pins, cfg->line_out_pins,
1475 sizeof(cfg->hp_pins));
1476 cfg->line_outs = cfg->speaker_outs;
1477 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1478 sizeof(cfg->speaker_pins));
1479 cfg->speaker_outs = 0;
1480 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1481 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1482 fill_hardwired = true;
1485 if (cfg->hp_outs > 0 &&
1486 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1487 cfg->speaker_outs = cfg->line_outs;
1488 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1489 sizeof(cfg->speaker_pins));
1490 cfg->line_outs = cfg->hp_outs;
1491 memcpy(cfg->line_out_pins, cfg->hp_pins,
1492 sizeof(cfg->hp_pins));
1494 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1495 cfg->line_out_type = AUTO_PIN_HP_OUT;
1496 fill_hardwired = true;
1503 debug_badness("==> restoring best_cfg\n");
1505 fill_and_eval_dacs(codec, best_wired, best_mio);
1507 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1508 cfg->line_out_type, best_wired, best_mio);
1509 debug_show_configs(spec, cfg);
1511 if (cfg->line_out_pins[0]) {
1512 struct nid_path *path;
1513 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1515 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1522 /* add playback controls from the parsed DAC table */
1523 static int create_multi_out_ctls(struct hda_codec *codec,
1524 const struct auto_pin_cfg *cfg)
1526 struct hda_gen_spec *spec = codec->spec;
1527 int i, err, noutputs;
1529 noutputs = cfg->line_outs;
1530 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1531 noutputs += spec->multi_ios;
1533 for (i = 0; i < noutputs; i++) {
1536 struct nid_path *path;
1538 if (i >= cfg->line_outs) {
1540 name = channel_name[i];
1542 name = get_line_out_pfx(spec, i, true, &index);
1545 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1548 if (!name || !strcmp(name, "CLFE")) {
1550 err = add_vol_ctl(codec, "Center", 0, 1, path);
1553 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1556 err = add_sw_ctl(codec, "Center", 0, 1, path);
1559 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1563 err = add_stereo_vol(codec, name, index, path);
1566 err = add_stereo_sw(codec, name, index, path);
1574 static int create_extra_out(struct hda_codec *codec, int path_idx,
1575 const char *pfx, int cidx)
1577 struct nid_path *path;
1580 path = snd_hda_get_path_from_idx(codec, path_idx);
1583 err = add_stereo_vol(codec, pfx, cidx, path);
1586 err = add_stereo_sw(codec, pfx, cidx, path);
1592 /* add playback controls for speaker and HP outputs */
1593 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1594 const int *paths, const char *pfx)
1598 for (i = 0; i < num_pins; i++) {
1603 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1604 name = "Bass Speaker";
1605 else if (num_pins >= 3) {
1606 snprintf(tmp, sizeof(tmp), "%s %s",
1607 pfx, channel_name[i]);
1613 err = create_extra_out(codec, paths[i], name, idx);
1620 static int create_hp_out_ctls(struct hda_codec *codec)
1622 struct hda_gen_spec *spec = codec->spec;
1623 return create_extra_outs(codec, spec->autocfg.hp_outs,
1628 static int create_speaker_out_ctls(struct hda_codec *codec)
1630 struct hda_gen_spec *spec = codec->spec;
1631 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1632 spec->speaker_paths,
1637 * independent HP controls
1640 static int indep_hp_info(struct snd_kcontrol *kcontrol,
1641 struct snd_ctl_elem_info *uinfo)
1643 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1646 static int indep_hp_get(struct snd_kcontrol *kcontrol,
1647 struct snd_ctl_elem_value *ucontrol)
1649 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1650 struct hda_gen_spec *spec = codec->spec;
1651 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1655 static int indep_hp_put(struct snd_kcontrol *kcontrol,
1656 struct snd_ctl_elem_value *ucontrol)
1658 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1659 struct hda_gen_spec *spec = codec->spec;
1660 unsigned int select = ucontrol->value.enumerated.item[0];
1663 mutex_lock(&spec->pcm_mutex);
1664 if (spec->active_streams) {
1669 if (spec->indep_hp_enabled != select) {
1670 spec->indep_hp_enabled = select;
1671 if (spec->indep_hp_enabled)
1672 spec->multiout.hp_out_nid[0] = 0;
1674 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1678 mutex_unlock(&spec->pcm_mutex);
1682 static const struct snd_kcontrol_new indep_hp_ctl = {
1683 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1684 .name = "Independent HP",
1685 .info = indep_hp_info,
1686 .get = indep_hp_get,
1687 .put = indep_hp_put,
1691 static int create_indep_hp_ctls(struct hda_codec *codec)
1693 struct hda_gen_spec *spec = codec->spec;
1695 if (!spec->indep_hp)
1697 if (!spec->multiout.hp_out_nid[0]) {
1702 spec->indep_hp_enabled = false;
1703 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1704 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1710 * channel mode enum control
1713 static int ch_mode_info(struct snd_kcontrol *kcontrol,
1714 struct snd_ctl_elem_info *uinfo)
1716 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1717 struct hda_gen_spec *spec = codec->spec;
1720 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1722 uinfo->value.enumerated.items = spec->multi_ios + 1;
1723 if (uinfo->value.enumerated.item > spec->multi_ios)
1724 uinfo->value.enumerated.item = spec->multi_ios;
1725 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1726 sprintf(uinfo->value.enumerated.name, "%dch", chs);
1730 static int ch_mode_get(struct snd_kcontrol *kcontrol,
1731 struct snd_ctl_elem_value *ucontrol)
1733 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1734 struct hda_gen_spec *spec = codec->spec;
1735 ucontrol->value.enumerated.item[0] =
1736 (spec->ext_channel_count - spec->min_channel_count) / 2;
1740 static inline struct nid_path *
1741 get_multiio_path(struct hda_codec *codec, int idx)
1743 struct hda_gen_spec *spec = codec->spec;
1744 return snd_hda_get_path_from_idx(codec,
1745 spec->out_paths[spec->autocfg.line_outs + idx]);
1748 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1750 struct hda_gen_spec *spec = codec->spec;
1751 hda_nid_t nid = spec->multi_io[idx].pin;
1752 struct nid_path *path;
1754 path = get_multiio_path(codec, idx);
1758 if (path->active == output)
1762 set_pin_target(codec, nid, PIN_OUT, true);
1763 snd_hda_activate_path(codec, path, true, true);
1764 set_pin_eapd(codec, nid, true);
1766 set_pin_eapd(codec, nid, false);
1767 snd_hda_activate_path(codec, path, false, true);
1768 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
1771 /* update jack retasking in case it modifies any of them */
1772 snd_hda_gen_hp_automute(codec, NULL);
1773 snd_hda_gen_line_automute(codec, NULL);
1774 snd_hda_gen_mic_autoswitch(codec, NULL);
1779 static int ch_mode_put(struct snd_kcontrol *kcontrol,
1780 struct snd_ctl_elem_value *ucontrol)
1782 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1783 struct hda_gen_spec *spec = codec->spec;
1786 ch = ucontrol->value.enumerated.item[0];
1787 if (ch < 0 || ch > spec->multi_ios)
1789 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
1791 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
1792 for (i = 0; i < spec->multi_ios; i++)
1793 set_multi_io(codec, i, i < ch);
1794 spec->multiout.max_channels = max(spec->ext_channel_count,
1795 spec->const_channel_count);
1796 if (spec->need_dac_fix)
1797 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1801 static const struct snd_kcontrol_new channel_mode_enum = {
1802 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1803 .name = "Channel Mode",
1804 .info = ch_mode_info,
1809 static int create_multi_channel_mode(struct hda_codec *codec)
1811 struct hda_gen_spec *spec = codec->spec;
1813 if (spec->multi_ios > 0) {
1814 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
1821 * aamix loopback enable/disable switch
1824 #define loopback_mixing_info indep_hp_info
1826 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1827 struct snd_ctl_elem_value *ucontrol)
1829 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1830 struct hda_gen_spec *spec = codec->spec;
1831 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1835 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1836 int nomix_path_idx, int mix_path_idx)
1838 struct nid_path *nomix_path, *mix_path;
1840 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1841 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1842 if (!nomix_path || !mix_path)
1845 snd_hda_activate_path(codec, nomix_path, false, true);
1846 snd_hda_activate_path(codec, mix_path, true, true);
1848 snd_hda_activate_path(codec, mix_path, false, true);
1849 snd_hda_activate_path(codec, nomix_path, true, true);
1853 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1854 struct snd_ctl_elem_value *ucontrol)
1856 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1857 struct hda_gen_spec *spec = codec->spec;
1858 unsigned int val = ucontrol->value.enumerated.item[0];
1860 if (val == spec->aamix_mode)
1862 spec->aamix_mode = val;
1863 update_aamix_paths(codec, val, spec->out_paths[0],
1864 spec->aamix_out_paths[0]);
1865 update_aamix_paths(codec, val, spec->hp_paths[0],
1866 spec->aamix_out_paths[1]);
1867 update_aamix_paths(codec, val, spec->speaker_paths[0],
1868 spec->aamix_out_paths[2]);
1872 static const struct snd_kcontrol_new loopback_mixing_enum = {
1873 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1874 .name = "Loopback Mixing",
1875 .info = loopback_mixing_info,
1876 .get = loopback_mixing_get,
1877 .put = loopback_mixing_put,
1880 static int create_loopback_mixing_ctl(struct hda_codec *codec)
1882 struct hda_gen_spec *spec = codec->spec;
1884 if (!spec->mixer_nid)
1886 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1887 spec->aamix_out_paths[2]))
1889 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1895 * shared headphone/mic handling
1898 static void call_update_outputs(struct hda_codec *codec);
1900 /* for shared I/O, change the pin-control accordingly */
1901 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1903 struct hda_gen_spec *spec = codec->spec;
1905 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1906 /* NOTE: this assumes that there are only two inputs, the
1907 * first is the real internal mic and the second is HP/mic jack.
1910 val = snd_hda_get_default_vref(codec, pin);
1912 /* This pin does not have vref caps - let's enable vref on pin 0x18
1913 instead, as suggested by Realtek */
1914 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1915 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1916 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1917 if (vref_val != AC_PINCTL_VREF_HIZ)
1918 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1919 PIN_IN | (set_as_mic ? vref_val : 0));
1922 val = set_as_mic ? val | PIN_IN : PIN_HP;
1923 set_pin_target(codec, pin, val, true);
1925 spec->automute_speaker = !set_as_mic;
1926 call_update_outputs(codec);
1929 /* create a shared input with the headphone out */
1930 static int create_shared_input(struct hda_codec *codec)
1932 struct hda_gen_spec *spec = codec->spec;
1933 struct auto_pin_cfg *cfg = &spec->autocfg;
1934 unsigned int defcfg;
1937 /* only one internal input pin? */
1938 if (cfg->num_inputs != 1)
1940 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1941 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1944 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1945 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1946 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1947 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1949 return 0; /* both not available */
1951 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1952 return 0; /* no input */
1954 cfg->inputs[1].pin = nid;
1955 cfg->inputs[1].type = AUTO_PIN_MIC;
1956 cfg->num_inputs = 2;
1957 spec->shared_mic_hp = 1;
1958 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1968 /* add the powersave loopback-list entry */
1969 static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1971 struct hda_amp_list *list;
1973 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1975 list = spec->loopback_list + spec->num_loopbacks;
1977 list->dir = HDA_INPUT;
1979 spec->num_loopbacks++;
1980 spec->loopback.amplist = spec->loopback_list;
1983 #define add_loopback_list(spec, mix, idx) /* NOP */
1986 /* create input playback/capture controls for the given pin */
1987 static int new_analog_input(struct hda_codec *codec, int input_idx,
1988 hda_nid_t pin, const char *ctlname, int ctlidx,
1991 struct hda_gen_spec *spec = codec->spec;
1992 struct nid_path *path;
1996 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1997 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1998 return 0; /* no need for analog loopback */
2000 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
2003 print_nid_path("loopback", path);
2004 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
2006 idx = path->idx[path->depth - 1];
2007 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2008 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2009 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
2012 path->ctls[NID_PATH_VOL_CTL] = val;
2015 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2016 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2017 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
2020 path->ctls[NID_PATH_MUTE_CTL] = val;
2023 path->active = true;
2024 add_loopback_list(spec, mix_nid, idx);
2028 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2030 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2031 return (pincap & AC_PINCAP_IN) != 0;
2034 /* Parse the codec tree and retrieve ADCs */
2035 static int fill_adc_nids(struct hda_codec *codec)
2037 struct hda_gen_spec *spec = codec->spec;
2039 hda_nid_t *adc_nids = spec->adc_nids;
2040 int max_nums = ARRAY_SIZE(spec->adc_nids);
2043 nid = codec->start_nid;
2044 for (i = 0; i < codec->num_nodes; i++, nid++) {
2045 unsigned int caps = get_wcaps(codec, nid);
2046 int type = get_wcaps_type(caps);
2048 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2050 adc_nids[nums] = nid;
2051 if (++nums >= max_nums)
2054 spec->num_adc_nids = nums;
2058 /* filter out invalid adc_nids that don't give all active input pins;
2059 * if needed, check whether dynamic ADC-switching is available
2061 static int check_dyn_adc_switch(struct hda_codec *codec)
2063 struct hda_gen_spec *spec = codec->spec;
2064 struct hda_input_mux *imux = &spec->input_mux;
2065 unsigned int ok_bits;
2071 for (n = 0; n < spec->num_adc_nids; n++) {
2072 for (i = 0; i < imux->num_items; i++) {
2073 if (!spec->input_paths[i][n])
2076 if (i >= imux->num_items) {
2077 ok_bits |= (1 << n);
2083 if (spec->shared_mic_hp) {
2084 spec->shared_mic_hp = 0;
2085 imux->num_items = 1;
2089 /* check whether ADC-switch is possible */
2090 for (i = 0; i < imux->num_items; i++) {
2091 for (n = 0; n < spec->num_adc_nids; n++) {
2092 if (spec->input_paths[i][n]) {
2093 spec->dyn_adc_idx[i] = n;
2099 snd_printdd("hda-codec: enabling ADC switching\n");
2100 spec->dyn_adc_switch = 1;
2101 } else if (nums != spec->num_adc_nids) {
2102 /* shrink the invalid adcs and input paths */
2104 for (n = 0; n < spec->num_adc_nids; n++) {
2105 if (!(ok_bits & (1 << n)))
2108 spec->adc_nids[nums] = spec->adc_nids[n];
2109 for (i = 0; i < imux->num_items; i++) {
2110 invalidate_nid_path(codec,
2111 spec->input_paths[i][nums]);
2112 spec->input_paths[i][nums] =
2113 spec->input_paths[i][n];
2118 spec->num_adc_nids = nums;
2121 if (imux->num_items == 1 || spec->shared_mic_hp) {
2122 snd_printdd("hda-codec: reducing to a single ADC\n");
2123 spec->num_adc_nids = 1; /* reduce to a single ADC */
2126 /* single index for individual volumes ctls */
2127 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2128 spec->num_adc_nids = 1;
2133 /* parse capture source paths from the given pin and create imux items */
2134 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2135 int num_adcs, const char *label, int anchor)
2137 struct hda_gen_spec *spec = codec->spec;
2138 struct hda_input_mux *imux = &spec->input_mux;
2139 int imux_idx = imux->num_items;
2140 bool imux_added = false;
2143 for (c = 0; c < num_adcs; c++) {
2144 struct nid_path *path;
2145 hda_nid_t adc = spec->adc_nids[c];
2147 if (!is_reachable_path(codec, pin, adc))
2149 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2152 print_nid_path("input", path);
2153 spec->input_paths[imux_idx][c] =
2154 snd_hda_get_path_idx(codec, path);
2157 spec->imux_pins[imux->num_items] = pin;
2158 snd_hda_add_imux_item(imux, label,
2159 imux->num_items, NULL);
2168 * create playback/capture controls for input pins
2170 static int create_input_ctls(struct hda_codec *codec)
2172 struct hda_gen_spec *spec = codec->spec;
2173 const struct auto_pin_cfg *cfg = &spec->autocfg;
2174 hda_nid_t mixer = spec->mixer_nid;
2176 int i, err, type_idx = 0;
2177 const char *prev_label = NULL;
2180 num_adcs = fill_adc_nids(codec);
2184 for (i = 0; i < cfg->num_inputs; i++) {
2188 pin = cfg->inputs[i].pin;
2189 if (!is_input_pin(codec, pin))
2192 label = hda_get_autocfg_input_label(codec, cfg, i);
2193 if (prev_label && !strcmp(label, prev_label))
2200 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2201 val |= snd_hda_get_default_vref(codec, pin);
2202 set_pin_target(codec, pin, val, false);
2205 if (is_reachable_path(codec, pin, mixer)) {
2206 err = new_analog_input(codec, i, pin,
2207 label, type_idx, mixer);
2213 err = parse_capture_source(codec, pin, num_adcs, label, -mixer);
2218 if (mixer && spec->add_stereo_mix_input) {
2219 err = parse_capture_source(codec, mixer, num_adcs,
2233 /* get the input path specified by the given adc and imux indices */
2234 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
2236 struct hda_gen_spec *spec = codec->spec;
2237 if (spec->dyn_adc_switch)
2238 adc_idx = spec->dyn_adc_idx[imux_idx];
2239 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
2242 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2245 static int mux_enum_info(struct snd_kcontrol *kcontrol,
2246 struct snd_ctl_elem_info *uinfo)
2248 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2249 struct hda_gen_spec *spec = codec->spec;
2250 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2253 static int mux_enum_get(struct snd_kcontrol *kcontrol,
2254 struct snd_ctl_elem_value *ucontrol)
2256 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2257 struct hda_gen_spec *spec = codec->spec;
2258 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2260 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2264 static int mux_enum_put(struct snd_kcontrol *kcontrol,
2265 struct snd_ctl_elem_value *ucontrol)
2267 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2268 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2269 return mux_select(codec, adc_idx,
2270 ucontrol->value.enumerated.item[0]);
2273 static const struct snd_kcontrol_new cap_src_temp = {
2274 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2275 .name = "Input Source",
2276 .info = mux_enum_info,
2277 .get = mux_enum_get,
2278 .put = mux_enum_put,
2282 * capture volume and capture switch ctls
2285 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2286 struct snd_ctl_elem_value *ucontrol);
2288 /* call the given amp update function for all amps in the imux list at once */
2289 static int cap_put_caller(struct snd_kcontrol *kcontrol,
2290 struct snd_ctl_elem_value *ucontrol,
2291 put_call_t func, int type)
2293 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2294 struct hda_gen_spec *spec = codec->spec;
2295 const struct hda_input_mux *imux;
2296 struct nid_path *path;
2297 int i, adc_idx, err = 0;
2299 imux = &spec->input_mux;
2300 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2301 mutex_lock(&codec->control_mutex);
2302 /* we use the cache-only update at first since multiple input paths
2303 * may shared the same amp; by updating only caches, the redundant
2304 * writes to hardware can be reduced.
2306 codec->cached_write = 1;
2307 for (i = 0; i < imux->num_items; i++) {
2308 path = get_input_path(codec, adc_idx, i);
2309 if (!path || !path->ctls[type])
2311 kcontrol->private_value = path->ctls[type];
2312 err = func(kcontrol, ucontrol);
2317 codec->cached_write = 0;
2318 mutex_unlock(&codec->control_mutex);
2319 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
2320 if (err >= 0 && spec->cap_sync_hook)
2321 spec->cap_sync_hook(codec);
2325 /* capture volume ctl callbacks */
2326 #define cap_vol_info snd_hda_mixer_amp_volume_info
2327 #define cap_vol_get snd_hda_mixer_amp_volume_get
2328 #define cap_vol_tlv snd_hda_mixer_amp_tlv
2330 static int cap_vol_put(struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_value *ucontrol)
2333 return cap_put_caller(kcontrol, ucontrol,
2334 snd_hda_mixer_amp_volume_put,
2338 static const struct snd_kcontrol_new cap_vol_temp = {
2339 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2340 .name = "Capture Volume",
2341 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2342 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2343 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2344 .info = cap_vol_info,
2347 .tlv = { .c = cap_vol_tlv },
2350 /* capture switch ctl callbacks */
2351 #define cap_sw_info snd_ctl_boolean_stereo_info
2352 #define cap_sw_get snd_hda_mixer_amp_switch_get
2354 static int cap_sw_put(struct snd_kcontrol *kcontrol,
2355 struct snd_ctl_elem_value *ucontrol)
2357 return cap_put_caller(kcontrol, ucontrol,
2358 snd_hda_mixer_amp_switch_put,
2362 static const struct snd_kcontrol_new cap_sw_temp = {
2363 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2364 .name = "Capture Switch",
2365 .info = cap_sw_info,
2370 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2375 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2376 for (depth = 0; depth < 3; depth++) {
2377 if (depth >= path->depth)
2379 i = path->depth - depth - 1;
2380 nid = path->path[i];
2381 if (!path->ctls[NID_PATH_VOL_CTL]) {
2382 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2383 path->ctls[NID_PATH_VOL_CTL] =
2384 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2385 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2386 int idx = path->idx[i];
2387 if (!depth && codec->single_adc_amp)
2389 path->ctls[NID_PATH_VOL_CTL] =
2390 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2393 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2394 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2395 path->ctls[NID_PATH_MUTE_CTL] =
2396 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2397 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2398 int idx = path->idx[i];
2399 if (!depth && codec->single_adc_amp)
2401 path->ctls[NID_PATH_MUTE_CTL] =
2402 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2409 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2411 struct hda_gen_spec *spec = codec->spec;
2412 struct auto_pin_cfg *cfg = &spec->autocfg;
2416 if (!spec->inv_dmic_split)
2418 for (i = 0; i < cfg->num_inputs; i++) {
2419 if (cfg->inputs[i].pin != nid)
2421 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2423 val = snd_hda_codec_get_pincfg(codec, nid);
2424 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2429 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2430 int idx, bool is_switch, unsigned int ctl,
2433 struct hda_gen_spec *spec = codec->spec;
2435 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2436 const char *sfx = is_switch ? "Switch" : "Volume";
2437 unsigned int chs = inv_dmic ? 1 : 3;
2444 snprintf(tmpname, sizeof(tmpname),
2445 "%s Capture %s", label, sfx);
2447 snprintf(tmpname, sizeof(tmpname),
2449 err = add_control(spec, type, tmpname, idx,
2450 amp_val_replace_channels(ctl, chs));
2451 if (err < 0 || !inv_dmic)
2454 /* Make independent right kcontrol */
2456 snprintf(tmpname, sizeof(tmpname),
2457 "Inverted %s Capture %s", label, sfx);
2459 snprintf(tmpname, sizeof(tmpname),
2460 "Inverted Capture %s", sfx);
2461 return add_control(spec, type, tmpname, idx,
2462 amp_val_replace_channels(ctl, 2));
2465 /* create single (and simple) capture volume and switch controls */
2466 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2467 unsigned int vol_ctl, unsigned int sw_ctl,
2471 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2474 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2480 /* create bound capture volume and switch controls */
2481 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2482 unsigned int vol_ctl, unsigned int sw_ctl)
2484 struct hda_gen_spec *spec = codec->spec;
2485 struct snd_kcontrol_new *knew;
2488 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
2492 knew->private_value = vol_ctl;
2493 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2496 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
2500 knew->private_value = sw_ctl;
2501 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2506 /* return the vol ctl when used first in the imux list */
2507 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2509 struct nid_path *path;
2513 path = get_input_path(codec, 0, idx);
2516 ctl = path->ctls[type];
2519 for (i = 0; i < idx - 1; i++) {
2520 path = get_input_path(codec, 0, i);
2521 if (path && path->ctls[type] == ctl)
2527 /* create individual capture volume and switch controls per input */
2528 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2530 struct hda_gen_spec *spec = codec->spec;
2531 struct hda_input_mux *imux = &spec->input_mux;
2532 int i, err, type, type_idx = 0;
2533 const char *prev_label = NULL;
2535 for (i = 0; i < imux->num_items; i++) {
2538 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2539 if (prev_label && !strcmp(label, prev_label))
2544 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2546 for (type = 0; type < 2; type++) {
2547 err = add_single_cap_ctl(codec, label, type_idx, type,
2548 get_first_cap_ctl(codec, i, type),
2557 static int create_capture_mixers(struct hda_codec *codec)
2559 struct hda_gen_spec *spec = codec->spec;
2560 struct hda_input_mux *imux = &spec->input_mux;
2561 int i, n, nums, err;
2563 if (spec->dyn_adc_switch)
2566 nums = spec->num_adc_nids;
2568 if (!spec->auto_mic && imux->num_items > 1) {
2569 struct snd_kcontrol_new *knew;
2571 name = nums > 1 ? "Input Source" : "Capture Source";
2572 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
2578 for (n = 0; n < nums; n++) {
2580 bool inv_dmic = false;
2584 for (i = 0; i < imux->num_items; i++) {
2585 struct nid_path *path;
2586 path = get_input_path(codec, n, i);
2589 parse_capvol_in_path(codec, path);
2591 vol = path->ctls[NID_PATH_VOL_CTL];
2592 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2595 sw = path->ctls[NID_PATH_MUTE_CTL];
2596 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2598 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2603 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2605 else if (!spec->multi_cap_vol)
2606 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2608 err = create_multi_cap_vol_ctl(codec);
2617 * add mic boosts if needed
2619 static int parse_mic_boost(struct hda_codec *codec)
2621 struct hda_gen_spec *spec = codec->spec;
2622 struct auto_pin_cfg *cfg = &spec->autocfg;
2626 const char *prev_label = NULL;
2628 for (i = 0; i < cfg->num_inputs; i++) {
2629 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2631 nid = cfg->inputs[i].pin;
2632 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2634 char boost_label[44];
2635 struct nid_path *path;
2638 label = hda_get_autocfg_input_label(codec, cfg, i);
2639 if (prev_label && !strcmp(label, prev_label))
2645 snprintf(boost_label, sizeof(boost_label),
2646 "%s Boost Volume", label);
2647 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2648 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2649 boost_label, type_idx, val);
2653 path = snd_hda_get_nid_path(codec, nid, 0);
2655 path->ctls[NID_PATH_BOOST_CTL] = val;
2662 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2664 static void parse_digital(struct hda_codec *codec)
2666 struct hda_gen_spec *spec = codec->spec;
2667 struct nid_path *path;
2669 hda_nid_t dig_nid, pin;
2671 /* support multiple SPDIFs; the secondary is set up as a slave */
2673 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2674 pin = spec->autocfg.dig_out_pins[i];
2675 dig_nid = look_for_dac(codec, pin, true);
2678 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
2681 print_nid_path("digout", path);
2682 path->active = true;
2683 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
2684 set_pin_target(codec, pin, PIN_OUT, false);
2686 spec->multiout.dig_out_nid = dig_nid;
2687 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2689 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2690 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2692 spec->slave_dig_outs[nums - 1] = dig_nid;
2697 if (spec->autocfg.dig_in_pin) {
2698 pin = spec->autocfg.dig_in_pin;
2699 dig_nid = codec->start_nid;
2700 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2701 unsigned int wcaps = get_wcaps(codec, dig_nid);
2702 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2704 if (!(wcaps & AC_WCAP_DIGITAL))
2706 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
2708 print_nid_path("digin", path);
2709 path->active = true;
2710 spec->dig_in_nid = dig_nid;
2711 spec->digin_path = snd_hda_get_path_idx(codec, path);
2712 set_pin_target(codec, pin, PIN_IN, false);
2721 * input MUX handling
2724 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2726 /* select the given imux item; either unmute exclusively or select the route */
2727 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2730 struct hda_gen_spec *spec = codec->spec;
2731 const struct hda_input_mux *imux;
2732 struct nid_path *path;
2734 imux = &spec->input_mux;
2735 if (!imux->num_items)
2738 if (idx >= imux->num_items)
2739 idx = imux->num_items - 1;
2740 if (spec->cur_mux[adc_idx] == idx)
2743 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
2747 snd_hda_activate_path(codec, path, false, false);
2749 spec->cur_mux[adc_idx] = idx;
2751 if (spec->shared_mic_hp)
2752 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2754 if (spec->dyn_adc_switch)
2755 dyn_adc_pcm_resetup(codec, idx);
2757 path = get_input_path(codec, adc_idx, idx);
2762 snd_hda_activate_path(codec, path, true, false);
2763 if (spec->cap_sync_hook)
2764 spec->cap_sync_hook(codec);
2770 * Jack detections for HP auto-mute and mic-switch
2773 /* check each pin in the given array; returns true if any of them is plugged */
2774 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2778 for (i = 0; i < num_pins; i++) {
2779 hda_nid_t nid = pins[i];
2782 /* don't detect pins retasked as inputs */
2783 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
2785 present |= snd_hda_jack_detect(codec, nid);
2790 /* standard HP/line-out auto-mute helper */
2791 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2794 struct hda_gen_spec *spec = codec->spec;
2797 for (i = 0; i < num_pins; i++) {
2798 hda_nid_t nid = pins[i];
2802 /* don't reset VREF value in case it's controlling
2803 * the amp (see alc861_fixup_asus_amp_vref_0f())
2805 if (spec->keep_vref_in_automute)
2806 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
2810 val |= snd_hda_codec_get_pin_target(codec, nid);
2811 /* here we call update_pin_ctl() so that the pinctl is changed
2812 * without changing the pinctl target value;
2813 * the original target value will be still referred at the
2814 * init / resume again
2816 update_pin_ctl(codec, nid, val);
2817 set_pin_eapd(codec, nid, !mute);
2821 /* Toggle outputs muting */
2822 void snd_hda_gen_update_outputs(struct hda_codec *codec)
2824 struct hda_gen_spec *spec = codec->spec;
2827 /* Control HP pins/amps depending on master_mute state;
2828 * in general, HP pins/amps control should be enabled in all cases,
2829 * but currently set only for master_mute, just to be safe
2831 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2832 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2833 spec->autocfg.hp_pins, spec->master_mute);
2835 if (!spec->automute_speaker)
2838 on = spec->hp_jack_present | spec->line_jack_present;
2839 on |= spec->master_mute;
2840 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2841 spec->autocfg.speaker_pins, on);
2843 /* toggle line-out mutes if needed, too */
2844 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2845 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2846 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2848 if (!spec->automute_lo)
2851 on = spec->hp_jack_present;
2852 on |= spec->master_mute;
2853 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2854 spec->autocfg.line_out_pins, on);
2856 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
2858 static void call_update_outputs(struct hda_codec *codec)
2860 struct hda_gen_spec *spec = codec->spec;
2861 if (spec->automute_hook)
2862 spec->automute_hook(codec);
2864 snd_hda_gen_update_outputs(codec);
2867 /* standard HP-automute helper */
2868 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2870 struct hda_gen_spec *spec = codec->spec;
2872 spec->hp_jack_present =
2873 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2874 spec->autocfg.hp_pins);
2875 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2877 call_update_outputs(codec);
2879 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
2881 /* standard line-out-automute helper */
2882 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2884 struct hda_gen_spec *spec = codec->spec;
2886 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2888 /* check LO jack only when it's different from HP */
2889 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2892 spec->line_jack_present =
2893 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2894 spec->autocfg.line_out_pins);
2895 if (!spec->automute_speaker || !spec->detect_lo)
2897 call_update_outputs(codec);
2899 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
2901 /* standard mic auto-switch helper */
2902 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
2904 struct hda_gen_spec *spec = codec->spec;
2907 if (!spec->auto_mic)
2910 for (i = spec->am_num_entries - 1; i > 0; i--) {
2911 hda_nid_t pin = spec->am_entry[i].pin;
2912 /* don't detect pins retasked as outputs */
2913 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
2915 if (snd_hda_jack_detect(codec, pin)) {
2916 mux_select(codec, 0, spec->am_entry[i].idx);
2920 mux_select(codec, 0, spec->am_entry[0].idx);
2922 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
2925 * Auto-Mute mode mixer enum support
2927 static int automute_mode_info(struct snd_kcontrol *kcontrol,
2928 struct snd_ctl_elem_info *uinfo)
2930 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2931 struct hda_gen_spec *spec = codec->spec;
2932 static const char * const texts3[] = {
2933 "Disabled", "Speaker Only", "Line Out+Speaker"
2936 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2937 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2938 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2941 static int automute_mode_get(struct snd_kcontrol *kcontrol,
2942 struct snd_ctl_elem_value *ucontrol)
2944 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2945 struct hda_gen_spec *spec = codec->spec;
2946 unsigned int val = 0;
2947 if (spec->automute_speaker)
2949 if (spec->automute_lo)
2952 ucontrol->value.enumerated.item[0] = val;
2956 static int automute_mode_put(struct snd_kcontrol *kcontrol,
2957 struct snd_ctl_elem_value *ucontrol)
2959 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2960 struct hda_gen_spec *spec = codec->spec;
2962 switch (ucontrol->value.enumerated.item[0]) {
2964 if (!spec->automute_speaker && !spec->automute_lo)
2966 spec->automute_speaker = 0;
2967 spec->automute_lo = 0;
2970 if (spec->automute_speaker_possible) {
2971 if (!spec->automute_lo && spec->automute_speaker)
2973 spec->automute_speaker = 1;
2974 spec->automute_lo = 0;
2975 } else if (spec->automute_lo_possible) {
2976 if (spec->automute_lo)
2978 spec->automute_lo = 1;
2983 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2985 if (spec->automute_speaker && spec->automute_lo)
2987 spec->automute_speaker = 1;
2988 spec->automute_lo = 1;
2993 call_update_outputs(codec);
2997 static const struct snd_kcontrol_new automute_mode_enum = {
2998 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2999 .name = "Auto-Mute Mode",
3000 .info = automute_mode_info,
3001 .get = automute_mode_get,
3002 .put = automute_mode_put,
3005 static int add_automute_mode_enum(struct hda_codec *codec)
3007 struct hda_gen_spec *spec = codec->spec;
3009 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
3015 * Check the availability of HP/line-out auto-mute;
3016 * Set up appropriately if really supported
3018 static int check_auto_mute_availability(struct hda_codec *codec)
3020 struct hda_gen_spec *spec = codec->spec;
3021 struct auto_pin_cfg *cfg = &spec->autocfg;
3025 if (cfg->hp_pins[0])
3027 if (cfg->line_out_pins[0])
3029 if (cfg->speaker_pins[0])
3031 if (present < 2) /* need two different output types */
3034 if (!cfg->speaker_pins[0] &&
3035 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3036 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3037 sizeof(cfg->speaker_pins));
3038 cfg->speaker_outs = cfg->line_outs;
3041 if (!cfg->hp_pins[0] &&
3042 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3043 memcpy(cfg->hp_pins, cfg->line_out_pins,
3044 sizeof(cfg->hp_pins));
3045 cfg->hp_outs = cfg->line_outs;
3048 for (i = 0; i < cfg->hp_outs; i++) {
3049 hda_nid_t nid = cfg->hp_pins[i];
3050 if (!is_jack_detectable(codec, nid))
3052 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3054 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
3055 spec->hp_automute_hook ?
3056 spec->hp_automute_hook :
3057 snd_hda_gen_hp_automute);
3058 spec->detect_hp = 1;
3061 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3062 if (cfg->speaker_outs)
3063 for (i = 0; i < cfg->line_outs; i++) {
3064 hda_nid_t nid = cfg->line_out_pins[i];
3065 if (!is_jack_detectable(codec, nid))
3067 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3068 snd_hda_jack_detect_enable_callback(codec, nid,
3069 HDA_GEN_FRONT_EVENT,
3070 spec->line_automute_hook ?
3071 spec->line_automute_hook :
3072 snd_hda_gen_line_automute);
3073 spec->detect_lo = 1;
3075 spec->automute_lo_possible = spec->detect_hp;
3078 spec->automute_speaker_possible = cfg->speaker_outs &&
3079 (spec->detect_hp || spec->detect_lo);
3081 spec->automute_lo = spec->automute_lo_possible;
3082 spec->automute_speaker = spec->automute_speaker_possible;
3084 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3085 /* create a control for automute mode */
3086 err = add_automute_mode_enum(codec);
3093 /* check whether all auto-mic pins are valid; setup indices if OK */
3094 static bool auto_mic_check_imux(struct hda_codec *codec)
3096 struct hda_gen_spec *spec = codec->spec;
3097 const struct hda_input_mux *imux;
3100 imux = &spec->input_mux;
3101 for (i = 0; i < spec->am_num_entries; i++) {
3102 spec->am_entry[i].idx =
3103 find_idx_in_nid_list(spec->am_entry[i].pin,
3104 spec->imux_pins, imux->num_items);
3105 if (spec->am_entry[i].idx < 0)
3106 return false; /* no corresponding imux */
3109 /* we don't need the jack detection for the first pin */
3110 for (i = 1; i < spec->am_num_entries; i++)
3111 snd_hda_jack_detect_enable_callback(codec,
3112 spec->am_entry[i].pin,
3114 spec->mic_autoswitch_hook ?
3115 spec->mic_autoswitch_hook :
3116 snd_hda_gen_mic_autoswitch);
3120 static int compare_attr(const void *ap, const void *bp)
3122 const struct automic_entry *a = ap;
3123 const struct automic_entry *b = bp;
3124 return (int)(a->attr - b->attr);
3128 * Check the availability of auto-mic switch;
3129 * Set up if really supported
3131 static int check_auto_mic_availability(struct hda_codec *codec)
3133 struct hda_gen_spec *spec = codec->spec;
3134 struct auto_pin_cfg *cfg = &spec->autocfg;
3138 if (spec->suppress_auto_mic)
3143 for (i = 0; i < cfg->num_inputs; i++) {
3144 hda_nid_t nid = cfg->inputs[i].pin;
3146 attr = snd_hda_codec_get_pincfg(codec, nid);
3147 attr = snd_hda_get_input_pin_attr(attr);
3148 if (types & (1 << attr))
3149 return 0; /* already occupied */
3151 case INPUT_PIN_ATTR_INT:
3152 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3153 return 0; /* invalid type */
3155 case INPUT_PIN_ATTR_UNUSED:
3156 return 0; /* invalid entry */
3158 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3159 return 0; /* invalid type */
3160 if (!spec->line_in_auto_switch &&
3161 cfg->inputs[i].type != AUTO_PIN_MIC)
3162 return 0; /* only mic is allowed */
3163 if (!is_jack_detectable(codec, nid))
3164 return 0; /* no unsol support */
3167 if (num_pins >= MAX_AUTO_MIC_PINS)
3169 types |= (1 << attr);
3170 spec->am_entry[num_pins].pin = nid;
3171 spec->am_entry[num_pins].attr = attr;
3178 spec->am_num_entries = num_pins;
3179 /* sort the am_entry in the order of attr so that the pin with a
3180 * higher attr will be selected when the jack is plugged.
3182 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3183 compare_attr, NULL);
3185 if (!auto_mic_check_imux(codec))
3189 spec->num_adc_nids = 1;
3190 spec->cur_mux[0] = spec->am_entry[0].idx;
3191 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3192 spec->am_entry[0].pin,
3193 spec->am_entry[1].pin,
3194 spec->am_entry[2].pin);
3201 * Parse the given BIOS configuration and set up the hda_gen_spec
3203 * return 1 if successful, 0 if the proper config is not found,
3204 * or a negative error code
3206 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
3207 struct auto_pin_cfg *cfg)
3209 struct hda_gen_spec *spec = codec->spec;
3212 if (cfg != &spec->autocfg) {
3213 spec->autocfg = *cfg;
3214 cfg = &spec->autocfg;
3217 if (!cfg->line_outs) {
3218 if (cfg->dig_outs || cfg->dig_in_pin) {
3219 spec->multiout.max_channels = 2;
3220 spec->no_analog = 1;
3223 return 0; /* can't find valid BIOS pin config */
3226 if (!spec->no_primary_hp &&
3227 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3228 cfg->line_outs <= cfg->hp_outs) {
3229 /* use HP as primary out */
3230 cfg->speaker_outs = cfg->line_outs;
3231 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3232 sizeof(cfg->speaker_pins));
3233 cfg->line_outs = cfg->hp_outs;
3234 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3236 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3237 cfg->line_out_type = AUTO_PIN_HP_OUT;
3240 err = parse_output_paths(codec);
3243 err = create_multi_channel_mode(codec);
3246 err = create_multi_out_ctls(codec, cfg);
3249 err = create_hp_out_ctls(codec);
3252 err = create_speaker_out_ctls(codec);
3255 err = create_indep_hp_ctls(codec);
3258 err = create_loopback_mixing_ctl(codec);
3261 err = create_shared_input(codec);
3264 err = create_input_ctls(codec);
3268 spec->const_channel_count = spec->ext_channel_count;
3269 /* check the multiple speaker and headphone pins */
3270 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3271 spec->const_channel_count = max(spec->const_channel_count,
3272 cfg->speaker_outs * 2);
3273 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3274 spec->const_channel_count = max(spec->const_channel_count,
3276 spec->multiout.max_channels = max(spec->ext_channel_count,
3277 spec->const_channel_count);
3279 err = check_auto_mute_availability(codec);
3283 err = check_dyn_adc_switch(codec);
3287 if (!spec->shared_mic_hp) {
3288 err = check_auto_mic_availability(codec);
3293 err = create_capture_mixers(codec);
3297 err = parse_mic_boost(codec);
3302 parse_digital(codec);
3306 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
3310 * Build control elements
3313 /* slave controls for virtual master */
3314 static const char * const slave_pfxs[] = {
3315 "Front", "Surround", "Center", "LFE", "Side",
3316 "Headphone", "Speaker", "Mono", "Line Out",
3317 "CLFE", "Bass Speaker", "PCM",
3318 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3319 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3324 int snd_hda_gen_build_controls(struct hda_codec *codec)
3326 struct hda_gen_spec *spec = codec->spec;
3329 if (spec->kctls.used) {
3330 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3335 if (spec->multiout.dig_out_nid) {
3336 err = snd_hda_create_dig_out_ctls(codec,
3337 spec->multiout.dig_out_nid,
3338 spec->multiout.dig_out_nid,
3339 spec->pcm_rec[1].pcm_type);
3342 if (!spec->no_analog) {
3343 err = snd_hda_create_spdif_share_sw(codec,
3347 spec->multiout.share_spdif = 1;
3350 if (spec->dig_in_nid) {
3351 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3356 /* if we have no master control, let's create it */
3357 if (!spec->no_analog &&
3358 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3359 unsigned int vmaster_tlv[4];
3360 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3361 HDA_OUTPUT, vmaster_tlv);
3362 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3363 vmaster_tlv, slave_pfxs,
3368 if (!spec->no_analog &&
3369 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3370 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3373 true, &spec->vmaster_mute.sw_kctl);
3376 if (spec->vmaster_mute.hook)
3377 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3378 spec->vmaster_mute_enum);
3381 free_kctls(spec); /* no longer needed */
3383 if (spec->shared_mic_hp) {
3385 int nid = spec->autocfg.inputs[1].pin;
3386 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3389 err = snd_hda_jack_detect_enable(codec, nid, 0);
3394 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3400 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3407 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3408 struct hda_codec *codec,
3409 struct snd_pcm_substream *substream,
3412 struct hda_gen_spec *spec = codec->spec;
3413 if (spec->pcm_playback_hook)
3414 spec->pcm_playback_hook(hinfo, codec, substream, action);
3418 * Analog playback callbacks
3420 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3421 struct hda_codec *codec,
3422 struct snd_pcm_substream *substream)
3424 struct hda_gen_spec *spec = codec->spec;
3427 mutex_lock(&spec->pcm_mutex);
3428 err = snd_hda_multi_out_analog_open(codec,
3429 &spec->multiout, substream,
3432 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3433 call_pcm_playback_hook(hinfo, codec, substream,
3434 HDA_GEN_PCM_ACT_OPEN);
3436 mutex_unlock(&spec->pcm_mutex);
3440 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3441 struct hda_codec *codec,
3442 unsigned int stream_tag,
3443 unsigned int format,
3444 struct snd_pcm_substream *substream)
3446 struct hda_gen_spec *spec = codec->spec;
3449 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3450 stream_tag, format, substream);
3452 call_pcm_playback_hook(hinfo, codec, substream,
3453 HDA_GEN_PCM_ACT_PREPARE);
3457 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3458 struct hda_codec *codec,
3459 struct snd_pcm_substream *substream)
3461 struct hda_gen_spec *spec = codec->spec;
3464 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3466 call_pcm_playback_hook(hinfo, codec, substream,
3467 HDA_GEN_PCM_ACT_CLEANUP);
3471 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3472 struct hda_codec *codec,
3473 struct snd_pcm_substream *substream)
3475 struct hda_gen_spec *spec = codec->spec;
3476 mutex_lock(&spec->pcm_mutex);
3477 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3478 call_pcm_playback_hook(hinfo, codec, substream,
3479 HDA_GEN_PCM_ACT_CLOSE);
3480 mutex_unlock(&spec->pcm_mutex);
3484 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3485 struct hda_codec *codec,
3486 struct snd_pcm_substream *substream)
3488 struct hda_gen_spec *spec = codec->spec;
3491 mutex_lock(&spec->pcm_mutex);
3492 if (!spec->indep_hp_enabled)
3495 spec->active_streams |= 1 << STREAM_INDEP_HP;
3496 call_pcm_playback_hook(hinfo, codec, substream,
3497 HDA_GEN_PCM_ACT_OPEN);
3498 mutex_unlock(&spec->pcm_mutex);
3502 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3503 struct hda_codec *codec,
3504 struct snd_pcm_substream *substream)
3506 struct hda_gen_spec *spec = codec->spec;
3507 mutex_lock(&spec->pcm_mutex);
3508 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3509 call_pcm_playback_hook(hinfo, codec, substream,
3510 HDA_GEN_PCM_ACT_CLOSE);
3511 mutex_unlock(&spec->pcm_mutex);
3515 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3516 struct hda_codec *codec,
3517 unsigned int stream_tag,
3518 unsigned int format,
3519 struct snd_pcm_substream *substream)
3521 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3522 call_pcm_playback_hook(hinfo, codec, substream,
3523 HDA_GEN_PCM_ACT_PREPARE);
3527 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3528 struct hda_codec *codec,
3529 struct snd_pcm_substream *substream)
3531 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3532 call_pcm_playback_hook(hinfo, codec, substream,
3533 HDA_GEN_PCM_ACT_CLEANUP);
3540 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3541 struct hda_codec *codec,
3542 struct snd_pcm_substream *substream)
3544 struct hda_gen_spec *spec = codec->spec;
3545 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3548 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3549 struct hda_codec *codec,
3550 unsigned int stream_tag,
3551 unsigned int format,
3552 struct snd_pcm_substream *substream)
3554 struct hda_gen_spec *spec = codec->spec;
3555 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3556 stream_tag, format, substream);
3559 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3560 struct hda_codec *codec,
3561 struct snd_pcm_substream *substream)
3563 struct hda_gen_spec *spec = codec->spec;
3564 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3567 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3568 struct hda_codec *codec,
3569 struct snd_pcm_substream *substream)
3571 struct hda_gen_spec *spec = codec->spec;
3572 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3578 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3579 struct hda_codec *codec,
3580 unsigned int stream_tag,
3581 unsigned int format,
3582 struct snd_pcm_substream *substream)
3584 struct hda_gen_spec *spec = codec->spec;
3586 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
3587 stream_tag, 0, format);
3591 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3592 struct hda_codec *codec,
3593 struct snd_pcm_substream *substream)
3595 struct hda_gen_spec *spec = codec->spec;
3597 snd_hda_codec_cleanup_stream(codec,
3598 spec->adc_nids[substream->number + 1]);
3604 static const struct hda_pcm_stream pcm_analog_playback = {
3608 /* NID is set in build_pcms */
3610 .open = playback_pcm_open,
3611 .close = playback_pcm_close,
3612 .prepare = playback_pcm_prepare,
3613 .cleanup = playback_pcm_cleanup
3617 static const struct hda_pcm_stream pcm_analog_capture = {
3621 /* NID is set in build_pcms */
3624 static const struct hda_pcm_stream pcm_analog_alt_playback = {
3628 /* NID is set in build_pcms */
3630 .open = alt_playback_pcm_open,
3631 .close = alt_playback_pcm_close,
3632 .prepare = alt_playback_pcm_prepare,
3633 .cleanup = alt_playback_pcm_cleanup
3637 static const struct hda_pcm_stream pcm_analog_alt_capture = {
3638 .substreams = 2, /* can be overridden */
3641 /* NID is set in build_pcms */
3643 .prepare = alt_capture_pcm_prepare,
3644 .cleanup = alt_capture_pcm_cleanup
3648 static const struct hda_pcm_stream pcm_digital_playback = {
3652 /* NID is set in build_pcms */
3654 .open = dig_playback_pcm_open,
3655 .close = dig_playback_pcm_close,
3656 .prepare = dig_playback_pcm_prepare,
3657 .cleanup = dig_playback_pcm_cleanup
3661 static const struct hda_pcm_stream pcm_digital_capture = {
3665 /* NID is set in build_pcms */
3668 /* Used by build_pcms to flag that a PCM has no playback stream */
3669 static const struct hda_pcm_stream pcm_null_stream = {
3676 * dynamic changing ADC PCM streams
3678 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3680 struct hda_gen_spec *spec = codec->spec;
3681 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3683 if (spec->cur_adc && spec->cur_adc != new_adc) {
3684 /* stream is running, let's swap the current ADC */
3685 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3686 spec->cur_adc = new_adc;
3687 snd_hda_codec_setup_stream(codec, new_adc,
3688 spec->cur_adc_stream_tag, 0,
3689 spec->cur_adc_format);
3695 /* analog capture with dynamic dual-adc changes */
3696 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3697 struct hda_codec *codec,
3698 unsigned int stream_tag,
3699 unsigned int format,
3700 struct snd_pcm_substream *substream)
3702 struct hda_gen_spec *spec = codec->spec;
3703 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3704 spec->cur_adc_stream_tag = stream_tag;
3705 spec->cur_adc_format = format;
3706 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3710 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3711 struct hda_codec *codec,
3712 struct snd_pcm_substream *substream)
3714 struct hda_gen_spec *spec = codec->spec;
3715 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3720 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3724 .nid = 0, /* fill later */
3726 .prepare = dyn_adc_capture_pcm_prepare,
3727 .cleanup = dyn_adc_capture_pcm_cleanup
3731 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3732 const char *chip_name)
3738 strlcpy(str, chip_name, len);
3740 /* drop non-alnum chars after a space */
3741 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3742 if (!isalnum(p[1])) {
3747 strlcat(str, sfx, len);
3750 /* build PCM streams based on the parsed results */
3751 int snd_hda_gen_build_pcms(struct hda_codec *codec)
3753 struct hda_gen_spec *spec = codec->spec;
3754 struct hda_pcm *info = spec->pcm_rec;
3755 const struct hda_pcm_stream *p;
3756 bool have_multi_adcs;
3758 codec->num_pcms = 1;
3759 codec->pcm_info = info;
3761 if (spec->no_analog)
3764 fill_pcm_stream_name(spec->stream_name_analog,
3765 sizeof(spec->stream_name_analog),
3766 " Analog", codec->chip_name);
3767 info->name = spec->stream_name_analog;
3769 if (spec->multiout.num_dacs > 0) {
3770 p = spec->stream_analog_playback;
3772 p = &pcm_analog_playback;
3773 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3774 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3775 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3776 spec->multiout.max_channels;
3777 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3778 spec->autocfg.line_outs == 2)
3779 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3782 if (spec->num_adc_nids) {
3783 p = spec->stream_analog_capture;
3785 if (spec->dyn_adc_switch)
3786 p = &dyn_adc_pcm_analog_capture;
3788 p = &pcm_analog_capture;
3790 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3791 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3795 /* SPDIF for stream index #1 */
3796 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
3797 fill_pcm_stream_name(spec->stream_name_digital,
3798 sizeof(spec->stream_name_digital),
3799 " Digital", codec->chip_name);
3800 codec->num_pcms = 2;
3801 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3802 info = spec->pcm_rec + 1;
3803 info->name = spec->stream_name_digital;
3804 if (spec->dig_out_type)
3805 info->pcm_type = spec->dig_out_type;
3807 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3808 if (spec->multiout.dig_out_nid) {
3809 p = spec->stream_digital_playback;
3811 p = &pcm_digital_playback;
3812 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3813 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3815 if (spec->dig_in_nid) {
3816 p = spec->stream_digital_capture;
3818 p = &pcm_digital_capture;
3819 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3820 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3824 if (spec->no_analog)
3827 /* If the use of more than one ADC is requested for the current
3828 * model, configure a second analog capture-only PCM.
3830 have_multi_adcs = (spec->num_adc_nids > 1) &&
3831 !spec->dyn_adc_switch && !spec->auto_mic;
3832 /* Additional Analaog capture for index #2 */
3833 if (spec->alt_dac_nid || have_multi_adcs) {
3834 codec->num_pcms = 3;
3835 info = spec->pcm_rec + 2;
3836 info->name = spec->stream_name_analog;
3837 if (spec->alt_dac_nid) {
3838 p = spec->stream_analog_alt_playback;
3840 p = &pcm_analog_alt_playback;
3841 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3842 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3845 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3847 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3849 if (have_multi_adcs) {
3850 p = spec->stream_analog_alt_capture;
3852 p = &pcm_analog_alt_capture;
3853 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3854 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3856 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3857 spec->num_adc_nids - 1;
3859 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3861 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3867 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3871 * Standard auto-parser initializations
3874 /* configure the given path as a proper output */
3875 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
3877 struct nid_path *path;
3880 path = snd_hda_get_path_from_idx(codec, path_idx);
3881 if (!path || !path->depth)
3883 pin = path->path[path->depth - 1];
3884 restore_pin_ctl(codec, pin);
3885 snd_hda_activate_path(codec, path, path->active, true);
3886 set_pin_eapd(codec, pin, path->active);
3889 /* initialize primary output paths */
3890 static void init_multi_out(struct hda_codec *codec)
3892 struct hda_gen_spec *spec = codec->spec;
3895 for (i = 0; i < spec->autocfg.line_outs; i++)
3896 set_output_and_unmute(codec, spec->out_paths[i]);
3900 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
3904 for (i = 0; i < num_outs; i++)
3905 set_output_and_unmute(codec, paths[i]);
3908 /* initialize hp and speaker paths */
3909 static void init_extra_out(struct hda_codec *codec)
3911 struct hda_gen_spec *spec = codec->spec;
3913 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3914 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
3915 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3916 __init_extra_out(codec, spec->autocfg.speaker_outs,
3917 spec->speaker_paths);
3920 /* initialize multi-io paths */
3921 static void init_multi_io(struct hda_codec *codec)
3923 struct hda_gen_spec *spec = codec->spec;
3926 for (i = 0; i < spec->multi_ios; i++) {
3927 hda_nid_t pin = spec->multi_io[i].pin;
3928 struct nid_path *path;
3929 path = get_multiio_path(codec, i);
3932 if (!spec->multi_io[i].ctl_in)
3933 spec->multi_io[i].ctl_in =
3934 snd_hda_codec_get_pin_target(codec, pin);
3935 snd_hda_activate_path(codec, path, path->active, true);
3939 /* set up input pins and loopback paths */
3940 static void init_analog_input(struct hda_codec *codec)
3942 struct hda_gen_spec *spec = codec->spec;
3943 struct auto_pin_cfg *cfg = &spec->autocfg;
3946 for (i = 0; i < cfg->num_inputs; i++) {
3947 hda_nid_t nid = cfg->inputs[i].pin;
3948 if (is_input_pin(codec, nid))
3949 restore_pin_ctl(codec, nid);
3951 /* init loopback inputs */
3952 if (spec->mixer_nid) {
3953 struct nid_path *path;
3954 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
3956 snd_hda_activate_path(codec, path,
3957 path->active, false);
3962 /* initialize ADC paths */
3963 static void init_input_src(struct hda_codec *codec)
3965 struct hda_gen_spec *spec = codec->spec;
3966 struct hda_input_mux *imux = &spec->input_mux;
3967 struct nid_path *path;
3970 if (spec->dyn_adc_switch)
3973 nums = spec->num_adc_nids;
3975 for (c = 0; c < nums; c++) {
3976 for (i = 0; i < imux->num_items; i++) {
3977 path = get_input_path(codec, c, i);
3979 bool active = path->active;
3980 if (i == spec->cur_mux[c])
3982 snd_hda_activate_path(codec, path, active, false);
3987 if (spec->shared_mic_hp)
3988 update_shared_mic_hp(codec, spec->cur_mux[0]);
3990 if (spec->cap_sync_hook)
3991 spec->cap_sync_hook(codec);
3994 /* set right pin controls for digital I/O */
3995 static void init_digital(struct hda_codec *codec)
3997 struct hda_gen_spec *spec = codec->spec;
4001 for (i = 0; i < spec->autocfg.dig_outs; i++)
4002 set_output_and_unmute(codec, spec->digout_paths[i]);
4003 pin = spec->autocfg.dig_in_pin;
4005 struct nid_path *path;
4006 restore_pin_ctl(codec, pin);
4007 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4009 snd_hda_activate_path(codec, path, path->active, false);
4013 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4014 * invalid unsol tags by some reason
4016 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4020 for (i = 0; i < codec->init_pins.used; i++) {
4021 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4022 hda_nid_t nid = pin->nid;
4023 if (is_jack_detectable(codec, nid) &&
4024 !snd_hda_jack_tbl_get(codec, nid))
4025 snd_hda_codec_update_cache(codec, nid, 0,
4026 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4031 * initialize the generic spec;
4032 * this can be put as patch_ops.init function
4034 int snd_hda_gen_init(struct hda_codec *codec)
4036 struct hda_gen_spec *spec = codec->spec;
4038 if (spec->init_hook)
4039 spec->init_hook(codec);
4041 snd_hda_apply_verbs(codec);
4043 codec->cached_write = 1;
4045 init_multi_out(codec);
4046 init_extra_out(codec);
4047 init_multi_io(codec);
4048 init_analog_input(codec);
4049 init_input_src(codec);
4050 init_digital(codec);
4052 clear_unsol_on_unused_pins(codec);
4054 /* call init functions of standard auto-mute helpers */
4055 snd_hda_gen_hp_automute(codec, NULL);
4056 snd_hda_gen_line_automute(codec, NULL);
4057 snd_hda_gen_mic_autoswitch(codec, NULL);
4059 snd_hda_codec_flush_amp_cache(codec);
4060 snd_hda_codec_flush_cmd_cache(codec);
4062 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4063 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4065 hda_call_check_power_status(codec, 0x01);
4068 EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4071 * free the generic spec;
4072 * this can be put as patch_ops.free function
4074 void snd_hda_gen_free(struct hda_codec *codec)
4076 snd_hda_gen_spec_free(codec->spec);
4080 EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4084 * check the loopback power save state;
4085 * this can be put as patch_ops.check_power_status function
4087 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4089 struct hda_gen_spec *spec = codec->spec;
4090 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4092 EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4097 * the generic codec support
4100 static const struct hda_codec_ops generic_patch_ops = {
4101 .build_controls = snd_hda_gen_build_controls,
4102 .build_pcms = snd_hda_gen_build_pcms,
4103 .init = snd_hda_gen_init,
4104 .free = snd_hda_gen_free,
4105 .unsol_event = snd_hda_jack_unsol_event,
4107 .check_power_status = snd_hda_gen_check_power_status,
4111 int snd_hda_parse_generic_codec(struct hda_codec *codec)
4113 struct hda_gen_spec *spec;
4116 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4119 snd_hda_gen_spec_init(spec);
4122 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4126 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
4130 codec->patch_ops = generic_patch_ops;
4134 snd_hda_gen_free(codec);
4137 EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);