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 <linux/bitops.h>
30 #include <sound/core.h>
31 #include <sound/jack.h>
32 #include "hda_codec.h"
33 #include "hda_local.h"
34 #include "hda_auto_parser.h"
36 #include "hda_generic.h"
39 /* initialize hda_gen_spec struct */
40 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
42 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
43 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
44 mutex_init(&spec->pcm_mutex);
47 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
49 struct snd_kcontrol_new *
50 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
51 const struct snd_kcontrol_new *temp)
53 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
58 knew->name = kstrdup(name, GFP_KERNEL);
60 knew->name = kstrdup(knew->name, GFP_KERNEL);
65 EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
67 static void free_kctls(struct hda_gen_spec *spec)
69 if (spec->kctls.list) {
70 struct snd_kcontrol_new *kctl = spec->kctls.list;
72 for (i = 0; i < spec->kctls.used; i++)
75 snd_array_free(&spec->kctls);
78 void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
83 snd_array_free(&spec->paths);
85 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
90 static void parse_user_hints(struct hda_codec *codec)
92 struct hda_gen_spec *spec = codec->spec;
95 val = snd_hda_get_bool_hint(codec, "jack_detect");
97 codec->no_jack_detect = !val;
98 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
100 codec->inv_jack_detect = !!val;
101 val = snd_hda_get_bool_hint(codec, "trigger_sense");
103 codec->no_trigger_sense = !val;
104 val = snd_hda_get_bool_hint(codec, "inv_eapd");
106 codec->inv_eapd = !!val;
107 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
109 codec->pcm_format_first = !!val;
110 val = snd_hda_get_bool_hint(codec, "sticky_stream");
112 codec->no_sticky_stream = !val;
113 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
115 codec->spdif_status_reset = !!val;
116 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
118 codec->pin_amp_workaround = !!val;
119 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
121 codec->single_adc_amp = !!val;
123 val = snd_hda_get_bool_hint(codec, "auto_mute");
125 spec->suppress_auto_mute = !val;
126 val = snd_hda_get_bool_hint(codec, "auto_mic");
128 spec->suppress_auto_mic = !val;
129 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
131 spec->line_in_auto_switch = !!val;
132 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
134 spec->need_dac_fix = !!val;
135 val = snd_hda_get_bool_hint(codec, "primary_hp");
137 spec->no_primary_hp = !val;
138 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
140 spec->multi_cap_vol = !!val;
141 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
143 spec->inv_dmic_split = !!val;
144 val = snd_hda_get_bool_hint(codec, "indep_hp");
146 spec->indep_hp = !!val;
147 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
149 spec->add_stereo_mix_input = !!val;
150 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
152 spec->add_out_jack_modes = !!val;
153 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
155 spec->add_in_jack_modes = !!val;
157 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
158 spec->mixer_nid = val;
162 * pin control value accesses
165 #define update_pin_ctl(codec, pin, val) \
166 snd_hda_codec_update_cache(codec, pin, 0, \
167 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
169 /* restore the pinctl based on the cached value */
170 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
172 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
175 /* set the pinctl target value and write it if requested */
176 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
177 unsigned int val, bool do_write)
181 val = snd_hda_correct_pin_ctl(codec, pin, val);
182 snd_hda_codec_set_pin_target(codec, pin, val);
184 update_pin_ctl(codec, pin, val);
187 /* set pinctl target values for all given pins */
188 static void set_pin_targets(struct hda_codec *codec, int num_pins,
189 hda_nid_t *pins, unsigned int val)
192 for (i = 0; i < num_pins; i++)
193 set_pin_target(codec, pins[i], val, false);
200 /* return the position of NID in the list, or -1 if not found */
201 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
204 for (i = 0; i < nums; i++)
210 /* return true if the given NID is contained in the path */
211 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
213 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
216 static struct nid_path *get_nid_path(struct hda_codec *codec,
217 hda_nid_t from_nid, hda_nid_t to_nid,
220 struct hda_gen_spec *spec = codec->spec;
223 for (i = 0; i < spec->paths.used; i++) {
224 struct nid_path *path = snd_array_elem(&spec->paths, i);
225 if (path->depth <= 0)
227 if ((!from_nid || path->path[0] == from_nid) &&
228 (!to_nid || path->path[path->depth - 1] == to_nid)) {
230 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
231 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
238 /* get the path between the given NIDs;
239 * passing 0 to either @pin or @dac behaves as a wildcard
241 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
242 hda_nid_t from_nid, hda_nid_t to_nid)
244 return get_nid_path(codec, from_nid, to_nid, 0);
246 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
248 /* get the index number corresponding to the path instance;
249 * the index starts from 1, for easier checking the invalid value
251 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
253 struct hda_gen_spec *spec = codec->spec;
254 struct nid_path *array = spec->paths.list;
257 if (!spec->paths.used)
260 if (idx < 0 || idx >= spec->paths.used)
264 EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
266 /* get the path instance corresponding to the given index number */
267 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
269 struct hda_gen_spec *spec = codec->spec;
271 if (idx <= 0 || idx > spec->paths.used)
273 return snd_array_elem(&spec->paths, idx - 1);
275 EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
277 /* check whether the given DAC is already found in any existing paths */
278 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
280 struct hda_gen_spec *spec = codec->spec;
283 for (i = 0; i < spec->paths.used; i++) {
284 struct nid_path *path = snd_array_elem(&spec->paths, i);
285 if (path->path[0] == nid)
291 /* check whether the given two widgets can be connected */
292 static bool is_reachable_path(struct hda_codec *codec,
293 hda_nid_t from_nid, hda_nid_t to_nid)
295 if (!from_nid || !to_nid)
297 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
300 /* nid, dir and idx */
301 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
303 /* check whether the given ctl is already assigned in any path elements */
304 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
306 struct hda_gen_spec *spec = codec->spec;
309 val &= AMP_VAL_COMPARE_MASK;
310 for (i = 0; i < spec->paths.used; i++) {
311 struct nid_path *path = snd_array_elem(&spec->paths, i);
312 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
318 /* check whether a control with the given (nid, dir, idx) was assigned */
319 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
320 int dir, int idx, int type)
322 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
323 return is_ctl_used(codec, val, type);
326 static void print_nid_path(const char *pfx, struct nid_path *path)
333 for (i = 0; i < path->depth; i++) {
335 sprintf(tmp, ":%02x", path->path[i]);
336 strlcat(buf, tmp, sizeof(buf));
338 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
341 /* called recursively */
342 static bool __parse_nid_path(struct hda_codec *codec,
343 hda_nid_t from_nid, hda_nid_t to_nid,
344 int anchor_nid, struct nid_path *path,
347 const hda_nid_t *conn;
350 if (to_nid == anchor_nid)
351 anchor_nid = 0; /* anchor passed */
352 else if (to_nid == (hda_nid_t)(-anchor_nid))
353 return false; /* hit the exclusive nid */
355 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
356 for (i = 0; i < nums; i++) {
357 if (conn[i] != from_nid) {
358 /* special case: when from_nid is 0,
359 * try to find an empty DAC
362 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
363 is_dac_already_used(codec, conn[i]))
366 /* anchor is not requested or already passed? */
370 if (depth >= MAX_NID_PATH_DEPTH)
372 for (i = 0; i < nums; i++) {
374 type = get_wcaps_type(get_wcaps(codec, conn[i]));
375 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
378 if (__parse_nid_path(codec, from_nid, conn[i],
379 anchor_nid, path, depth + 1))
385 path->path[path->depth] = conn[i];
386 path->idx[path->depth + 1] = i;
387 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
388 path->multi[path->depth + 1] = 1;
393 /* parse the widget path from the given nid to the target nid;
394 * when @from_nid is 0, try to find an empty DAC;
395 * when @anchor_nid is set to a positive value, only paths through the widget
396 * with the given value are evaluated.
397 * when @anchor_nid is set to a negative value, paths through the widget
398 * with the negative of given value are excluded, only other paths are chosen.
399 * when @anchor_nid is zero, no special handling about path selection.
401 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
402 hda_nid_t to_nid, int anchor_nid,
403 struct nid_path *path)
405 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
406 path->path[path->depth] = to_nid;
412 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
415 * parse the path between the given NIDs and add to the path list.
416 * if no valid path is found, return NULL
419 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
420 hda_nid_t to_nid, int anchor_nid)
422 struct hda_gen_spec *spec = codec->spec;
423 struct nid_path *path;
425 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
428 /* check whether the path has been already added */
429 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
433 path = snd_array_new(&spec->paths);
436 memset(path, 0, sizeof(*path));
437 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
443 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
445 /* clear the given path as invalid so that it won't be picked up later */
446 static void invalidate_nid_path(struct hda_codec *codec, int idx)
448 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
451 memset(path, 0, sizeof(*path));
454 /* look for an empty DAC slot */
455 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
458 struct hda_gen_spec *spec = codec->spec;
462 for (i = 0; i < spec->num_all_dacs; i++) {
463 hda_nid_t nid = spec->all_dacs[i];
464 if (!nid || is_dac_already_used(codec, nid))
466 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
467 if (is_digital != cap_digital)
469 if (is_reachable_path(codec, nid, pin))
475 /* replace the channels in the composed amp value with the given number */
476 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
478 val &= ~(0x3U << 16);
483 /* check whether the widget has the given amp capability for the direction */
484 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
485 int dir, unsigned int bits)
489 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
490 if (query_amp_caps(codec, nid, dir) & bits)
495 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
496 hda_nid_t nid2, int dir)
498 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
499 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
500 return (query_amp_caps(codec, nid1, dir) ==
501 query_amp_caps(codec, nid2, dir));
504 #define nid_has_mute(codec, nid, dir) \
505 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
506 #define nid_has_volume(codec, nid, dir) \
507 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
509 /* look for a widget suitable for assigning a mute switch in the path */
510 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
511 struct nid_path *path)
515 for (i = path->depth - 1; i >= 0; i--) {
516 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
517 return path->path[i];
518 if (i != path->depth - 1 && i != 0 &&
519 nid_has_mute(codec, path->path[i], HDA_INPUT))
520 return path->path[i];
525 /* look for a widget suitable for assigning a volume ctl in the path */
526 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
527 struct nid_path *path)
531 for (i = path->depth - 1; i >= 0; i--) {
532 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
533 return path->path[i];
539 * path activation / deactivation
542 /* can have the amp-in capability? */
543 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
545 hda_nid_t nid = path->path[idx];
546 unsigned int caps = get_wcaps(codec, nid);
547 unsigned int type = get_wcaps_type(caps);
549 if (!(caps & AC_WCAP_IN_AMP))
551 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
556 /* can have the amp-out capability? */
557 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
559 hda_nid_t nid = path->path[idx];
560 unsigned int caps = get_wcaps(codec, nid);
561 unsigned int type = get_wcaps_type(caps);
563 if (!(caps & AC_WCAP_OUT_AMP))
565 if (type == AC_WID_PIN && !idx) /* only for output pins */
570 /* check whether the given (nid,dir,idx) is active */
571 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
572 unsigned int idx, unsigned int dir)
574 struct hda_gen_spec *spec = codec->spec;
577 for (n = 0; n < spec->paths.used; n++) {
578 struct nid_path *path = snd_array_elem(&spec->paths, n);
581 for (i = 0; i < path->depth; i++) {
582 if (path->path[i] == nid) {
583 if (dir == HDA_OUTPUT || path->idx[i] == idx)
592 /* get the default amp value for the target state */
593 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
594 int dir, unsigned int caps, bool enable)
596 unsigned int val = 0;
598 if (caps & AC_AMPCAP_NUM_STEPS) {
601 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
603 if (caps & AC_AMPCAP_MUTE) {
610 /* initialize the amp value (only at the first time) */
611 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
613 unsigned int caps = query_amp_caps(codec, nid, dir);
614 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
615 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
618 /* calculate amp value mask we can modify;
619 * if the given amp is controlled by mixers, don't touch it
621 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
622 hda_nid_t nid, int dir, int idx,
625 unsigned int mask = 0xff;
627 if (caps & AC_AMPCAP_MUTE) {
628 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
631 if (caps & AC_AMPCAP_NUM_STEPS) {
632 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
633 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
639 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
640 int idx, int idx_to_check, bool enable)
643 unsigned int mask, val;
645 if (!enable && is_active_nid(codec, nid, dir, idx))
648 caps = query_amp_caps(codec, nid, dir);
649 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
650 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
655 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
658 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
661 hda_nid_t nid = path->path[i];
662 init_amp(codec, nid, HDA_OUTPUT, 0);
663 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
666 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
667 int i, bool enable, bool add_aamix)
669 struct hda_gen_spec *spec = codec->spec;
670 const hda_nid_t *conn;
673 hda_nid_t nid = path->path[i];
675 nums = snd_hda_get_conn_list(codec, nid, &conn);
676 type = get_wcaps_type(get_wcaps(codec, nid));
677 if (type == AC_WID_PIN ||
678 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
684 for (n = 0; n < nums; n++)
685 init_amp(codec, nid, HDA_INPUT, n);
687 /* here is a little bit tricky in comparison with activate_amp_out();
688 * when aa-mixer is available, we need to enable the path as well
690 for (n = 0; n < nums; n++) {
691 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
693 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
697 /* activate or deactivate the given path
698 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
700 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
701 bool enable, bool add_aamix)
706 path->active = false;
708 for (i = path->depth - 1; i >= 0; i--) {
709 if (enable && path->multi[i])
710 snd_hda_codec_write_cache(codec, path->path[i], 0,
711 AC_VERB_SET_CONNECT_SEL,
713 if (has_amp_in(codec, path, i))
714 activate_amp_in(codec, path, i, enable, add_aamix);
715 if (has_amp_out(codec, path, i))
716 activate_amp_out(codec, path, i, enable);
722 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
724 /* turn on/off EAPD on the given pin */
725 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
727 struct hda_gen_spec *spec = codec->spec;
728 if (spec->own_eapd_ctl ||
729 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
733 snd_hda_codec_update_cache(codec, pin, 0,
734 AC_VERB_SET_EAPD_BTLENABLE,
735 enable ? 0x02 : 0x00);
740 * Helper functions for creating mixer ctl elements
748 static const struct snd_kcontrol_new control_templates[] = {
749 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
750 HDA_CODEC_MUTE(NULL, 0, 0, 0),
751 HDA_BIND_MUTE(NULL, 0, 0, 0),
754 /* add dynamic controls from template */
755 static struct snd_kcontrol_new *
756 add_control(struct hda_gen_spec *spec, int type, const char *name,
757 int cidx, unsigned long val)
759 struct snd_kcontrol_new *knew;
761 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
765 if (get_amp_nid_(val))
766 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
767 knew->private_value = val;
771 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
772 const char *pfx, const char *dir,
773 const char *sfx, int cidx, unsigned long val)
776 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
777 if (!add_control(spec, type, name, cidx, val))
782 #define add_pb_vol_ctrl(spec, type, pfx, val) \
783 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
784 #define add_pb_sw_ctrl(spec, type, pfx, val) \
785 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
786 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
787 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
788 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
789 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
791 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
792 unsigned int chs, struct nid_path *path)
797 val = path->ctls[NID_PATH_VOL_CTL];
800 val = amp_val_replace_channels(val, chs);
801 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
804 /* return the channel bits suitable for the given path->ctls[] */
805 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
808 int chs = 1; /* mono (left only) */
810 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
811 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
812 chs = 3; /* stereo */
817 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
818 struct nid_path *path)
820 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
821 return add_vol_ctl(codec, pfx, cidx, chs, path);
824 /* create a mute-switch for the given mixer widget;
825 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
827 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
828 unsigned int chs, struct nid_path *path)
831 int type = HDA_CTL_WIDGET_MUTE;
835 val = path->ctls[NID_PATH_MUTE_CTL];
838 val = amp_val_replace_channels(val, chs);
839 if (get_amp_direction_(val) == HDA_INPUT) {
840 hda_nid_t nid = get_amp_nid_(val);
841 int nums = snd_hda_get_num_conns(codec, nid);
843 type = HDA_CTL_BIND_MUTE;
847 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
850 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
851 int cidx, struct nid_path *path)
853 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
854 return add_sw_ctl(codec, pfx, cidx, chs, path);
857 /* any ctl assigned to the path with the given index? */
858 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
860 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
861 return path && path->ctls[ctl_type];
864 static const char * const channel_name[4] = {
865 "Front", "Surround", "CLFE", "Side"
868 /* give some appropriate ctl name prefix for the given line out channel */
869 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
870 int *index, int ctl_type)
872 struct hda_gen_spec *spec = codec->spec;
873 struct auto_pin_cfg *cfg = &spec->autocfg;
876 if (cfg->line_outs == 1 && !spec->multi_ios &&
877 !cfg->hp_outs && !cfg->speaker_outs)
878 return spec->vmaster_mute.hook ? "PCM" : "Master";
880 /* if there is really a single DAC used in the whole output paths,
881 * use it master (or "PCM" if a vmaster hook is present)
883 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
884 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
885 return spec->vmaster_mute.hook ? "PCM" : "Master";
887 /* multi-io channels */
888 if (ch >= cfg->line_outs)
889 return channel_name[ch];
891 switch (cfg->line_out_type) {
892 case AUTO_PIN_SPEAKER_OUT:
893 /* if the primary channel vol/mute is shared with HP volume,
894 * don't name it as Speaker
896 if (!ch && cfg->hp_outs &&
897 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
899 if (cfg->line_outs == 1)
901 if (cfg->line_outs == 2)
902 return ch ? "Bass Speaker" : "Speaker";
904 case AUTO_PIN_HP_OUT:
905 /* if the primary channel vol/mute is shared with spk volume,
906 * don't name it as Headphone
908 if (!ch && cfg->speaker_outs &&
909 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
911 /* for multi-io case, only the primary out */
912 if (ch && spec->multi_ios)
918 /* for a single channel output, we don't have to name the channel */
919 if (cfg->line_outs == 1 && !spec->multi_ios)
922 if (ch >= ARRAY_SIZE(channel_name)) {
927 return channel_name[ch];
934 /* badness definition */
936 /* No primary DAC is found for the main output */
937 BAD_NO_PRIMARY_DAC = 0x10000,
938 /* No DAC is found for the extra output */
940 /* No possible multi-ios */
941 BAD_MULTI_IO = 0x103,
942 /* No individual DAC for extra output */
943 BAD_NO_EXTRA_DAC = 0x102,
944 /* No individual DAC for extra surrounds */
945 BAD_NO_EXTRA_SURR_DAC = 0x101,
946 /* Primary DAC shared with main surrounds */
947 BAD_SHARED_SURROUND = 0x100,
948 /* Primary DAC shared with main CLFE */
949 BAD_SHARED_CLFE = 0x10,
950 /* Primary DAC shared with extra surrounds */
951 BAD_SHARED_EXTRA_SURROUND = 0x10,
952 /* Volume widget is shared */
953 BAD_SHARED_VOL = 0x10,
956 /* look for widgets in the given path which are appropriate for
957 * volume and mute controls, and assign the values to ctls[].
959 * When no appropriate widget is found in the path, the badness value
960 * is incremented depending on the situation. The function returns the
961 * total badness for both volume and mute controls.
963 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
970 return BAD_SHARED_VOL * 2;
972 if (path->ctls[NID_PATH_VOL_CTL] ||
973 path->ctls[NID_PATH_MUTE_CTL])
974 return 0; /* already evaluated */
976 nid = look_for_out_vol_nid(codec, path);
978 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
979 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
980 badness += BAD_SHARED_VOL;
982 path->ctls[NID_PATH_VOL_CTL] = val;
984 badness += BAD_SHARED_VOL;
985 nid = look_for_out_mute_nid(codec, path);
987 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
988 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
989 nid_has_mute(codec, nid, HDA_OUTPUT))
990 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
992 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
993 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
994 badness += BAD_SHARED_VOL;
996 path->ctls[NID_PATH_MUTE_CTL] = val;
998 badness += BAD_SHARED_VOL;
1002 struct badness_table {
1003 int no_primary_dac; /* no primary DAC */
1004 int no_dac; /* no secondary DACs */
1005 int shared_primary; /* primary DAC is shared with main output */
1006 int shared_surr; /* secondary DAC shared with main or primary */
1007 int shared_clfe; /* third DAC shared with main or primary */
1008 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
1011 static struct badness_table main_out_badness = {
1012 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1013 .no_dac = BAD_NO_DAC,
1014 .shared_primary = BAD_NO_PRIMARY_DAC,
1015 .shared_surr = BAD_SHARED_SURROUND,
1016 .shared_clfe = BAD_SHARED_CLFE,
1017 .shared_surr_main = BAD_SHARED_SURROUND,
1020 static struct badness_table extra_out_badness = {
1021 .no_primary_dac = BAD_NO_DAC,
1022 .no_dac = BAD_NO_DAC,
1023 .shared_primary = BAD_NO_EXTRA_DAC,
1024 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1025 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1026 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1029 /* get the DAC of the primary output corresponding to the given array index */
1030 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1032 struct hda_gen_spec *spec = codec->spec;
1033 struct auto_pin_cfg *cfg = &spec->autocfg;
1035 if (cfg->line_outs > idx)
1036 return spec->private_dac_nids[idx];
1037 idx -= cfg->line_outs;
1038 if (spec->multi_ios > idx)
1039 return spec->multi_io[idx].dac;
1043 /* return the DAC if it's reachable, otherwise zero */
1044 static inline hda_nid_t try_dac(struct hda_codec *codec,
1045 hda_nid_t dac, hda_nid_t pin)
1047 return is_reachable_path(codec, dac, pin) ? dac : 0;
1050 /* try to assign DACs to pins and return the resultant badness */
1051 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1052 const hda_nid_t *pins, hda_nid_t *dacs,
1054 const struct badness_table *bad)
1056 struct hda_gen_spec *spec = codec->spec;
1064 for (i = 0; i < num_outs; i++) {
1065 struct nid_path *path;
1066 hda_nid_t pin = pins[i];
1068 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1070 badness += assign_out_path_ctls(codec, path);
1074 dacs[i] = look_for_dac(codec, pin, false);
1075 if (!dacs[i] && !i) {
1076 /* try to steal the DAC of surrounds for the front */
1077 for (j = 1; j < num_outs; j++) {
1078 if (is_reachable_path(codec, dacs[j], pin)) {
1081 invalidate_nid_path(codec, path_idx[j]);
1090 dac = try_dac(codec, get_primary_out(codec, i), pin);
1092 dac = try_dac(codec, dacs[0], pin);
1094 dac = try_dac(codec, get_primary_out(codec, i), pin);
1097 badness += bad->shared_primary;
1099 badness += bad->shared_surr;
1101 badness += bad->shared_clfe;
1102 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1103 dac = spec->private_dac_nids[0];
1104 badness += bad->shared_surr_main;
1106 badness += bad->no_primary_dac;
1108 badness += bad->no_dac;
1112 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1113 if (!path && !i && spec->mixer_nid) {
1114 /* try with aamix */
1115 path = snd_hda_add_new_path(codec, dac, pin, 0);
1119 badness += bad->no_dac;
1121 /* print_nid_path("output", path); */
1122 path->active = true;
1123 path_idx[i] = snd_hda_get_path_idx(codec, path);
1124 badness += assign_out_path_ctls(codec, path);
1131 /* return NID if the given pin has only a single connection to a certain DAC */
1132 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1134 struct hda_gen_spec *spec = codec->spec;
1136 hda_nid_t nid_found = 0;
1138 for (i = 0; i < spec->num_all_dacs; i++) {
1139 hda_nid_t nid = spec->all_dacs[i];
1140 if (!nid || is_dac_already_used(codec, nid))
1142 if (is_reachable_path(codec, nid, pin)) {
1151 /* check whether the given pin can be a multi-io pin */
1152 static bool can_be_multiio_pin(struct hda_codec *codec,
1153 unsigned int location, hda_nid_t nid)
1155 unsigned int defcfg, caps;
1157 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1158 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1160 if (location && get_defcfg_location(defcfg) != location)
1162 caps = snd_hda_query_pin_caps(codec, nid);
1163 if (!(caps & AC_PINCAP_OUT))
1168 /* count the number of input pins that are capable to be multi-io */
1169 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1171 struct hda_gen_spec *spec = codec->spec;
1172 struct auto_pin_cfg *cfg = &spec->autocfg;
1173 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1174 unsigned int location = get_defcfg_location(defcfg);
1178 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1179 for (i = 0; i < cfg->num_inputs; i++) {
1180 if (cfg->inputs[i].type != type)
1182 if (can_be_multiio_pin(codec, location,
1183 cfg->inputs[i].pin))
1193 * When hardwired is set, try to fill ony hardwired pins, and returns
1194 * zero if any pins are filled, non-zero if nothing found.
1195 * When hardwired is off, try to fill possible input pins, and returns
1196 * the badness value.
1198 static int fill_multi_ios(struct hda_codec *codec,
1199 hda_nid_t reference_pin,
1202 struct hda_gen_spec *spec = codec->spec;
1203 struct auto_pin_cfg *cfg = &spec->autocfg;
1204 int type, i, j, num_pins, old_pins;
1205 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1206 unsigned int location = get_defcfg_location(defcfg);
1208 struct nid_path *path;
1210 old_pins = spec->multi_ios;
1214 num_pins = count_multiio_pins(codec, reference_pin);
1218 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1219 for (i = 0; i < cfg->num_inputs; i++) {
1220 hda_nid_t nid = cfg->inputs[i].pin;
1223 if (cfg->inputs[i].type != type)
1225 if (!can_be_multiio_pin(codec, location, nid))
1227 for (j = 0; j < spec->multi_ios; j++) {
1228 if (nid == spec->multi_io[j].pin)
1231 if (j < spec->multi_ios)
1235 dac = get_dac_if_single(codec, nid);
1237 dac = look_for_dac(codec, nid, false);
1242 path = snd_hda_add_new_path(codec, dac, nid,
1248 /* print_nid_path("multiio", path); */
1249 spec->multi_io[spec->multi_ios].pin = nid;
1250 spec->multi_io[spec->multi_ios].dac = dac;
1251 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1252 snd_hda_get_path_idx(codec, path);
1254 if (spec->multi_ios >= 2)
1260 badness = BAD_MULTI_IO;
1261 if (old_pins == spec->multi_ios) {
1263 return 1; /* nothing found */
1265 return badness; /* no badness if nothing found */
1267 if (!hardwired && spec->multi_ios < 2) {
1268 /* cancel newly assigned paths */
1269 spec->paths.used -= spec->multi_ios - old_pins;
1270 spec->multi_ios = old_pins;
1274 /* assign volume and mute controls */
1275 for (i = old_pins; i < spec->multi_ios; i++) {
1276 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1277 badness += assign_out_path_ctls(codec, path);
1283 /* map DACs for all pins in the list if they are single connections */
1284 static bool map_singles(struct hda_codec *codec, int outs,
1285 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1287 struct hda_gen_spec *spec = codec->spec;
1290 for (i = 0; i < outs; i++) {
1291 struct nid_path *path;
1295 dac = get_dac_if_single(codec, pins[i]);
1298 path = snd_hda_add_new_path(codec, dac, pins[i],
1300 if (!path && !i && spec->mixer_nid)
1301 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1305 /* print_nid_path("output", path); */
1306 path->active = true;
1307 path_idx[i] = snd_hda_get_path_idx(codec, path);
1313 /* create a new path including aamix if available, and return its index */
1314 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1316 struct hda_gen_spec *spec = codec->spec;
1317 struct nid_path *path;
1320 path = snd_hda_get_path_from_idx(codec, path_idx);
1321 if (!path || !path->depth ||
1322 is_nid_contained(path, spec->mixer_nid))
1324 dac = path->path[0];
1325 pin = path->path[path->depth - 1];
1326 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1328 if (dac != spec->multiout.dac_nids[0])
1329 dac = spec->multiout.dac_nids[0];
1330 else if (spec->multiout.hp_out_nid[0])
1331 dac = spec->multiout.hp_out_nid[0];
1332 else if (spec->multiout.extra_out_nid[0])
1333 dac = spec->multiout.extra_out_nid[0];
1335 path = snd_hda_add_new_path(codec, dac, pin,
1340 /* print_nid_path("output-aamix", path); */
1341 path->active = false; /* unused as default */
1342 return snd_hda_get_path_idx(codec, path);
1345 /* fill the empty entries in the dac array for speaker/hp with the
1346 * shared dac pointed by the paths
1348 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1349 hda_nid_t *dacs, int *path_idx)
1351 struct nid_path *path;
1354 for (i = 0; i < num_outs; i++) {
1357 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1360 dacs[i] = path->path[0];
1364 /* fill in the dac_nids table from the parsed pin configuration */
1365 static int fill_and_eval_dacs(struct hda_codec *codec,
1366 bool fill_hardwired,
1367 bool fill_mio_first)
1369 struct hda_gen_spec *spec = codec->spec;
1370 struct auto_pin_cfg *cfg = &spec->autocfg;
1371 int i, err, badness;
1373 /* set num_dacs once to full for look_for_dac() */
1374 spec->multiout.num_dacs = cfg->line_outs;
1375 spec->multiout.dac_nids = spec->private_dac_nids;
1376 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1377 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1378 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1379 spec->multi_ios = 0;
1380 snd_array_free(&spec->paths);
1382 /* clear path indices */
1383 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1384 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1385 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1386 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1387 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1388 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1389 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1390 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1394 /* fill hard-wired DACs first */
1395 if (fill_hardwired) {
1398 mapped = map_singles(codec, cfg->line_outs,
1400 spec->private_dac_nids,
1402 mapped |= map_singles(codec, cfg->hp_outs,
1404 spec->multiout.hp_out_nid,
1406 mapped |= map_singles(codec, cfg->speaker_outs,
1408 spec->multiout.extra_out_nid,
1409 spec->speaker_paths);
1410 if (fill_mio_first && cfg->line_outs == 1 &&
1411 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1412 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1419 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1420 spec->private_dac_nids, spec->out_paths,
1423 if (fill_mio_first &&
1424 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1425 /* try to fill multi-io first */
1426 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1429 /* we don't count badness at this stage yet */
1432 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1433 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1434 spec->multiout.hp_out_nid,
1436 &extra_out_badness);
1441 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1442 err = try_assign_dacs(codec, cfg->speaker_outs,
1444 spec->multiout.extra_out_nid,
1445 spec->speaker_paths,
1446 &extra_out_badness);
1451 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1452 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1458 if (spec->mixer_nid) {
1459 spec->aamix_out_paths[0] =
1460 check_aamix_out_path(codec, spec->out_paths[0]);
1461 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1462 spec->aamix_out_paths[1] =
1463 check_aamix_out_path(codec, spec->hp_paths[0]);
1464 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1465 spec->aamix_out_paths[2] =
1466 check_aamix_out_path(codec, spec->speaker_paths[0]);
1469 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1470 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1471 spec->multi_ios = 1; /* give badness */
1473 /* re-count num_dacs and squash invalid entries */
1474 spec->multiout.num_dacs = 0;
1475 for (i = 0; i < cfg->line_outs; i++) {
1476 if (spec->private_dac_nids[i])
1477 spec->multiout.num_dacs++;
1479 memmove(spec->private_dac_nids + i,
1480 spec->private_dac_nids + i + 1,
1481 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1482 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1486 spec->ext_channel_count = spec->min_channel_count =
1487 spec->multiout.num_dacs * 2;
1489 if (spec->multi_ios == 2) {
1490 for (i = 0; i < 2; i++)
1491 spec->private_dac_nids[spec->multiout.num_dacs++] =
1492 spec->multi_io[i].dac;
1493 } else if (spec->multi_ios) {
1494 spec->multi_ios = 0;
1495 badness += BAD_MULTI_IO;
1498 /* re-fill the shared DAC for speaker / headphone */
1499 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1500 refill_shared_dacs(codec, cfg->hp_outs,
1501 spec->multiout.hp_out_nid,
1503 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1504 refill_shared_dacs(codec, cfg->speaker_outs,
1505 spec->multiout.extra_out_nid,
1506 spec->speaker_paths);
1511 #define DEBUG_BADNESS
1513 #ifdef DEBUG_BADNESS
1514 #define debug_badness snd_printdd
1516 #define debug_badness(...)
1519 #ifdef DEBUG_BADNESS
1520 static inline void print_nid_path_idx(struct hda_codec *codec,
1521 const char *pfx, int idx)
1523 struct nid_path *path;
1525 path = snd_hda_get_path_from_idx(codec, idx);
1527 print_nid_path(pfx, path);
1530 static void debug_show_configs(struct hda_codec *codec,
1531 struct auto_pin_cfg *cfg)
1533 struct hda_gen_spec *spec = codec->spec;
1534 #ifdef CONFIG_SND_DEBUG_VERBOSE
1535 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1539 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1540 cfg->line_out_pins[0], cfg->line_out_pins[1],
1541 cfg->line_out_pins[2], cfg->line_out_pins[3],
1542 spec->multiout.dac_nids[0],
1543 spec->multiout.dac_nids[1],
1544 spec->multiout.dac_nids[2],
1545 spec->multiout.dac_nids[3],
1546 lo_type[cfg->line_out_type]);
1547 for (i = 0; i < cfg->line_outs; i++)
1548 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1549 if (spec->multi_ios > 0)
1550 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1552 spec->multi_io[0].pin, spec->multi_io[1].pin,
1553 spec->multi_io[0].dac, spec->multi_io[1].dac);
1554 for (i = 0; i < spec->multi_ios; i++)
1555 print_nid_path_idx(codec, " mio",
1556 spec->out_paths[cfg->line_outs + i]);
1558 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1559 cfg->hp_pins[0], cfg->hp_pins[1],
1560 cfg->hp_pins[2], cfg->hp_pins[3],
1561 spec->multiout.hp_out_nid[0],
1562 spec->multiout.hp_out_nid[1],
1563 spec->multiout.hp_out_nid[2],
1564 spec->multiout.hp_out_nid[3]);
1565 for (i = 0; i < cfg->hp_outs; i++)
1566 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1567 if (cfg->speaker_outs)
1568 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1569 cfg->speaker_pins[0], cfg->speaker_pins[1],
1570 cfg->speaker_pins[2], cfg->speaker_pins[3],
1571 spec->multiout.extra_out_nid[0],
1572 spec->multiout.extra_out_nid[1],
1573 spec->multiout.extra_out_nid[2],
1574 spec->multiout.extra_out_nid[3]);
1575 for (i = 0; i < cfg->speaker_outs; i++)
1576 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1577 for (i = 0; i < 3; i++)
1578 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1581 #define debug_show_configs(codec, cfg) /* NOP */
1584 /* find all available DACs of the codec */
1585 static void fill_all_dac_nids(struct hda_codec *codec)
1587 struct hda_gen_spec *spec = codec->spec;
1589 hda_nid_t nid = codec->start_nid;
1591 spec->num_all_dacs = 0;
1592 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1593 for (i = 0; i < codec->num_nodes; i++, nid++) {
1594 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1596 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1597 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1600 spec->all_dacs[spec->num_all_dacs++] = nid;
1604 static int parse_output_paths(struct hda_codec *codec)
1606 struct hda_gen_spec *spec = codec->spec;
1607 struct auto_pin_cfg *cfg = &spec->autocfg;
1608 struct auto_pin_cfg *best_cfg;
1610 int best_badness = INT_MAX;
1612 bool fill_hardwired = true, fill_mio_first = true;
1613 bool best_wired = true, best_mio = true;
1614 bool hp_spk_swapped = false;
1616 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1622 badness = fill_and_eval_dacs(codec, fill_hardwired,
1628 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1629 cfg->line_out_type, fill_hardwired, fill_mio_first,
1631 debug_show_configs(codec, cfg);
1632 if (badness < best_badness) {
1633 best_badness = badness;
1635 best_wired = fill_hardwired;
1636 best_mio = fill_mio_first;
1640 fill_mio_first = !fill_mio_first;
1641 if (!fill_mio_first)
1643 fill_hardwired = !fill_hardwired;
1644 if (!fill_hardwired)
1648 hp_spk_swapped = true;
1649 if (cfg->speaker_outs > 0 &&
1650 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1651 cfg->hp_outs = cfg->line_outs;
1652 memcpy(cfg->hp_pins, cfg->line_out_pins,
1653 sizeof(cfg->hp_pins));
1654 cfg->line_outs = cfg->speaker_outs;
1655 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1656 sizeof(cfg->speaker_pins));
1657 cfg->speaker_outs = 0;
1658 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1659 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1660 fill_hardwired = true;
1663 if (cfg->hp_outs > 0 &&
1664 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1665 cfg->speaker_outs = cfg->line_outs;
1666 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1667 sizeof(cfg->speaker_pins));
1668 cfg->line_outs = cfg->hp_outs;
1669 memcpy(cfg->line_out_pins, cfg->hp_pins,
1670 sizeof(cfg->hp_pins));
1672 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1673 cfg->line_out_type = AUTO_PIN_HP_OUT;
1674 fill_hardwired = true;
1681 debug_badness("==> restoring best_cfg\n");
1683 fill_and_eval_dacs(codec, best_wired, best_mio);
1685 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1686 cfg->line_out_type, best_wired, best_mio);
1687 debug_show_configs(codec, cfg);
1689 if (cfg->line_out_pins[0]) {
1690 struct nid_path *path;
1691 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1693 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1694 if (spec->vmaster_nid)
1695 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1696 HDA_OUTPUT, spec->vmaster_tlv);
1699 /* set initial pinctl targets */
1700 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1704 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1705 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1706 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1707 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1708 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1709 set_pin_targets(codec, cfg->speaker_outs,
1710 cfg->speaker_pins, val);
1717 /* add playback controls from the parsed DAC table */
1718 static int create_multi_out_ctls(struct hda_codec *codec,
1719 const struct auto_pin_cfg *cfg)
1721 struct hda_gen_spec *spec = codec->spec;
1722 int i, err, noutputs;
1724 noutputs = cfg->line_outs;
1725 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1726 noutputs += spec->multi_ios;
1728 for (i = 0; i < noutputs; i++) {
1731 struct nid_path *path;
1733 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1737 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
1738 if (!name || !strcmp(name, "CLFE")) {
1740 err = add_vol_ctl(codec, "Center", 0, 1, path);
1743 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1747 err = add_stereo_vol(codec, name, index, path);
1752 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1753 if (!name || !strcmp(name, "CLFE")) {
1754 err = add_sw_ctl(codec, "Center", 0, 1, path);
1757 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1761 err = add_stereo_sw(codec, name, index, path);
1769 static int create_extra_out(struct hda_codec *codec, int path_idx,
1770 const char *pfx, int cidx)
1772 struct nid_path *path;
1775 path = snd_hda_get_path_from_idx(codec, path_idx);
1778 err = add_stereo_vol(codec, pfx, cidx, path);
1781 err = add_stereo_sw(codec, pfx, cidx, path);
1787 /* add playback controls for speaker and HP outputs */
1788 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1789 const int *paths, const char *pfx)
1793 for (i = 0; i < num_pins; i++) {
1798 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1799 name = "Bass Speaker";
1800 else if (num_pins >= 3) {
1801 snprintf(tmp, sizeof(tmp), "%s %s",
1802 pfx, channel_name[i]);
1808 err = create_extra_out(codec, paths[i], name, idx);
1815 static int create_hp_out_ctls(struct hda_codec *codec)
1817 struct hda_gen_spec *spec = codec->spec;
1818 return create_extra_outs(codec, spec->autocfg.hp_outs,
1823 static int create_speaker_out_ctls(struct hda_codec *codec)
1825 struct hda_gen_spec *spec = codec->spec;
1826 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1827 spec->speaker_paths,
1832 * independent HP controls
1835 static int indep_hp_info(struct snd_kcontrol *kcontrol,
1836 struct snd_ctl_elem_info *uinfo)
1838 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1841 static int indep_hp_get(struct snd_kcontrol *kcontrol,
1842 struct snd_ctl_elem_value *ucontrol)
1844 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1845 struct hda_gen_spec *spec = codec->spec;
1846 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1850 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1851 int nomix_path_idx, int mix_path_idx,
1854 static int indep_hp_put(struct snd_kcontrol *kcontrol,
1855 struct snd_ctl_elem_value *ucontrol)
1857 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1858 struct hda_gen_spec *spec = codec->spec;
1859 unsigned int select = ucontrol->value.enumerated.item[0];
1862 mutex_lock(&spec->pcm_mutex);
1863 if (spec->active_streams) {
1868 if (spec->indep_hp_enabled != select) {
1870 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1871 dacp = &spec->private_dac_nids[0];
1873 dacp = &spec->multiout.hp_out_nid[0];
1875 /* update HP aamix paths in case it conflicts with indep HP */
1876 if (spec->have_aamix_ctl) {
1877 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1878 update_aamix_paths(codec, spec->aamix_mode,
1880 spec->aamix_out_paths[0],
1881 spec->autocfg.line_out_type);
1883 update_aamix_paths(codec, spec->aamix_mode,
1885 spec->aamix_out_paths[1],
1889 spec->indep_hp_enabled = select;
1890 if (spec->indep_hp_enabled)
1893 *dacp = spec->alt_dac_nid;
1895 /* update HP auto-mute state too */
1896 if (spec->hp_automute_hook)
1897 spec->hp_automute_hook(codec, NULL);
1899 snd_hda_gen_hp_automute(codec, NULL);
1904 mutex_unlock(&spec->pcm_mutex);
1908 static const struct snd_kcontrol_new indep_hp_ctl = {
1909 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1910 .name = "Independent HP",
1911 .info = indep_hp_info,
1912 .get = indep_hp_get,
1913 .put = indep_hp_put,
1917 static int create_indep_hp_ctls(struct hda_codec *codec)
1919 struct hda_gen_spec *spec = codec->spec;
1922 if (!spec->indep_hp)
1924 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1925 dac = spec->multiout.dac_nids[0];
1927 dac = spec->multiout.hp_out_nid[0];
1933 spec->indep_hp_enabled = false;
1934 spec->alt_dac_nid = dac;
1935 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1941 * channel mode enum control
1944 static int ch_mode_info(struct snd_kcontrol *kcontrol,
1945 struct snd_ctl_elem_info *uinfo)
1947 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1948 struct hda_gen_spec *spec = codec->spec;
1951 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1953 uinfo->value.enumerated.items = spec->multi_ios + 1;
1954 if (uinfo->value.enumerated.item > spec->multi_ios)
1955 uinfo->value.enumerated.item = spec->multi_ios;
1956 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1957 sprintf(uinfo->value.enumerated.name, "%dch", chs);
1961 static int ch_mode_get(struct snd_kcontrol *kcontrol,
1962 struct snd_ctl_elem_value *ucontrol)
1964 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1965 struct hda_gen_spec *spec = codec->spec;
1966 ucontrol->value.enumerated.item[0] =
1967 (spec->ext_channel_count - spec->min_channel_count) / 2;
1971 static inline struct nid_path *
1972 get_multiio_path(struct hda_codec *codec, int idx)
1974 struct hda_gen_spec *spec = codec->spec;
1975 return snd_hda_get_path_from_idx(codec,
1976 spec->out_paths[spec->autocfg.line_outs + idx]);
1979 static void update_automute_all(struct hda_codec *codec);
1981 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1983 struct hda_gen_spec *spec = codec->spec;
1984 hda_nid_t nid = spec->multi_io[idx].pin;
1985 struct nid_path *path;
1987 path = get_multiio_path(codec, idx);
1991 if (path->active == output)
1995 set_pin_target(codec, nid, PIN_OUT, true);
1996 snd_hda_activate_path(codec, path, true, true);
1997 set_pin_eapd(codec, nid, true);
1999 set_pin_eapd(codec, nid, false);
2000 snd_hda_activate_path(codec, path, false, true);
2001 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2004 /* update jack retasking in case it modifies any of them */
2005 update_automute_all(codec);
2010 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2011 struct snd_ctl_elem_value *ucontrol)
2013 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2014 struct hda_gen_spec *spec = codec->spec;
2017 ch = ucontrol->value.enumerated.item[0];
2018 if (ch < 0 || ch > spec->multi_ios)
2020 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2022 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2023 for (i = 0; i < spec->multi_ios; i++)
2024 set_multi_io(codec, i, i < ch);
2025 spec->multiout.max_channels = max(spec->ext_channel_count,
2026 spec->const_channel_count);
2027 if (spec->need_dac_fix)
2028 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2032 static const struct snd_kcontrol_new channel_mode_enum = {
2033 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2034 .name = "Channel Mode",
2035 .info = ch_mode_info,
2040 static int create_multi_channel_mode(struct hda_codec *codec)
2042 struct hda_gen_spec *spec = codec->spec;
2044 if (spec->multi_ios > 0) {
2045 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2052 * aamix loopback enable/disable switch
2055 #define loopback_mixing_info indep_hp_info
2057 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2058 struct snd_ctl_elem_value *ucontrol)
2060 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2061 struct hda_gen_spec *spec = codec->spec;
2062 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2066 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2067 int nomix_path_idx, int mix_path_idx,
2070 struct hda_gen_spec *spec = codec->spec;
2071 struct nid_path *nomix_path, *mix_path;
2073 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2074 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2075 if (!nomix_path || !mix_path)
2078 /* if HP aamix path is driven from a different DAC and the
2079 * independent HP mode is ON, can't turn on aamix path
2081 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2082 mix_path->path[0] != spec->alt_dac_nid)
2086 snd_hda_activate_path(codec, nomix_path, false, true);
2087 snd_hda_activate_path(codec, mix_path, true, true);
2089 snd_hda_activate_path(codec, mix_path, false, true);
2090 snd_hda_activate_path(codec, nomix_path, true, true);
2094 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2095 struct snd_ctl_elem_value *ucontrol)
2097 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2098 struct hda_gen_spec *spec = codec->spec;
2099 unsigned int val = ucontrol->value.enumerated.item[0];
2101 if (val == spec->aamix_mode)
2103 spec->aamix_mode = val;
2104 update_aamix_paths(codec, val, spec->out_paths[0],
2105 spec->aamix_out_paths[0],
2106 spec->autocfg.line_out_type);
2107 update_aamix_paths(codec, val, spec->hp_paths[0],
2108 spec->aamix_out_paths[1],
2110 update_aamix_paths(codec, val, spec->speaker_paths[0],
2111 spec->aamix_out_paths[2],
2112 AUTO_PIN_SPEAKER_OUT);
2116 static const struct snd_kcontrol_new loopback_mixing_enum = {
2117 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2118 .name = "Loopback Mixing",
2119 .info = loopback_mixing_info,
2120 .get = loopback_mixing_get,
2121 .put = loopback_mixing_put,
2124 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2126 struct hda_gen_spec *spec = codec->spec;
2128 if (!spec->mixer_nid)
2130 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2131 spec->aamix_out_paths[2]))
2133 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2135 spec->have_aamix_ctl = 1;
2140 * shared headphone/mic handling
2143 static void call_update_outputs(struct hda_codec *codec);
2145 /* for shared I/O, change the pin-control accordingly */
2146 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
2148 struct hda_gen_spec *spec = codec->spec;
2150 hda_nid_t pin = spec->autocfg.inputs[1].pin;
2151 /* NOTE: this assumes that there are only two inputs, the
2152 * first is the real internal mic and the second is HP/mic jack.
2155 val = snd_hda_get_default_vref(codec, pin);
2157 /* This pin does not have vref caps - let's enable vref on pin 0x18
2158 instead, as suggested by Realtek */
2159 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2160 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2161 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2162 if (vref_val != AC_PINCTL_VREF_HIZ)
2163 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2164 PIN_IN | (set_as_mic ? vref_val : 0));
2167 val = set_as_mic ? val | PIN_IN : PIN_HP;
2168 set_pin_target(codec, pin, val, true);
2170 spec->automute_speaker = !set_as_mic;
2171 call_update_outputs(codec);
2174 /* create a shared input with the headphone out */
2175 static int create_shared_input(struct hda_codec *codec)
2177 struct hda_gen_spec *spec = codec->spec;
2178 struct auto_pin_cfg *cfg = &spec->autocfg;
2179 unsigned int defcfg;
2182 /* only one internal input pin? */
2183 if (cfg->num_inputs != 1)
2185 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2186 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2189 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2190 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2191 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2192 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2194 return 0; /* both not available */
2196 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2197 return 0; /* no input */
2199 cfg->inputs[1].pin = nid;
2200 cfg->inputs[1].type = AUTO_PIN_MIC;
2201 cfg->num_inputs = 2;
2202 spec->shared_mic_hp = 1;
2203 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2210 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2211 struct snd_ctl_elem_info *uinfo)
2213 static const char * const texts[] = {
2214 "Line Out", "Headphone Out",
2216 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2219 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2220 struct snd_ctl_elem_value *ucontrol)
2222 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2223 hda_nid_t nid = kcontrol->private_value;
2224 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2225 ucontrol->value.enumerated.item[0] = 1;
2227 ucontrol->value.enumerated.item[0] = 0;
2231 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2232 struct snd_ctl_elem_value *ucontrol)
2234 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2235 hda_nid_t nid = kcontrol->private_value;
2238 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2239 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2241 snd_hda_set_pin_ctl_cache(codec, nid, val);
2245 static const struct snd_kcontrol_new out_jack_mode_enum = {
2246 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2247 .info = out_jack_mode_info,
2248 .get = out_jack_mode_get,
2249 .put = out_jack_mode_put,
2252 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2254 struct hda_gen_spec *spec = codec->spec;
2257 for (i = 0; i < spec->kctls.used; i++) {
2258 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2259 if (!strcmp(kctl->name, name) && kctl->index == idx)
2265 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2266 char *name, size_t name_len)
2268 struct hda_gen_spec *spec = codec->spec;
2271 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2272 strlcat(name, " Jack Mode", name_len);
2274 for (; find_kctl_name(codec, name, idx); idx++)
2278 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2281 struct hda_gen_spec *spec = codec->spec;
2284 for (i = 0; i < num_pins; i++) {
2285 hda_nid_t pin = pins[i];
2286 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2287 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2288 struct snd_kcontrol_new *knew;
2290 get_jack_mode_name(codec, pin, name, sizeof(name));
2291 knew = snd_hda_gen_add_kctl(spec, name,
2292 &out_jack_mode_enum);
2295 knew->private_value = pin;
2306 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2309 static const char * const vref_texts[NUM_VREFS] = {
2310 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2311 "", "Mic 80pc Bias", "Mic 100pc Bias"
2314 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2316 unsigned int pincap;
2318 pincap = snd_hda_query_pin_caps(codec, pin);
2319 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2320 /* filter out unusual vrefs */
2321 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2325 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2326 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2328 unsigned int i, n = 0;
2330 for (i = 0; i < NUM_VREFS; i++) {
2331 if (vref_caps & (1 << i)) {
2340 /* convert back from the vref ctl index to the enum item index */
2341 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2343 unsigned int i, n = 0;
2345 for (i = 0; i < NUM_VREFS; i++) {
2348 if (vref_caps & (1 << i))
2354 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2355 struct snd_ctl_elem_info *uinfo)
2357 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2358 hda_nid_t nid = kcontrol->private_value;
2359 unsigned int vref_caps = get_vref_caps(codec, nid);
2361 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2363 /* set the right text */
2364 strcpy(uinfo->value.enumerated.name,
2365 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2369 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2370 struct snd_ctl_elem_value *ucontrol)
2372 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2373 hda_nid_t nid = kcontrol->private_value;
2374 unsigned int vref_caps = get_vref_caps(codec, nid);
2377 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2378 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2382 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2383 struct snd_ctl_elem_value *ucontrol)
2385 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2386 hda_nid_t nid = kcontrol->private_value;
2387 unsigned int vref_caps = get_vref_caps(codec, nid);
2388 unsigned int val, idx;
2390 val = snd_hda_codec_get_pin_target(codec, nid);
2391 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2392 if (idx == ucontrol->value.enumerated.item[0])
2395 val &= ~AC_PINCTL_VREFEN;
2396 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2397 snd_hda_set_pin_ctl_cache(codec, nid, val);
2401 static const struct snd_kcontrol_new in_jack_mode_enum = {
2402 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2403 .info = in_jack_mode_info,
2404 .get = in_jack_mode_get,
2405 .put = in_jack_mode_put,
2408 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2410 struct hda_gen_spec *spec = codec->spec;
2411 unsigned int defcfg;
2412 struct snd_kcontrol_new *knew;
2415 /* no jack mode for fixed pins */
2416 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2417 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2420 /* no multiple vref caps? */
2421 if (hweight32(get_vref_caps(codec, pin)) <= 1)
2424 get_jack_mode_name(codec, pin, name, sizeof(name));
2425 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2428 knew->private_value = pin;
2438 /* add the powersave loopback-list entry */
2439 static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2441 struct hda_amp_list *list;
2443 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2445 list = spec->loopback_list + spec->num_loopbacks;
2447 list->dir = HDA_INPUT;
2449 spec->num_loopbacks++;
2450 spec->loopback.amplist = spec->loopback_list;
2453 #define add_loopback_list(spec, mix, idx) /* NOP */
2456 /* create input playback/capture controls for the given pin */
2457 static int new_analog_input(struct hda_codec *codec, int input_idx,
2458 hda_nid_t pin, const char *ctlname, int ctlidx,
2461 struct hda_gen_spec *spec = codec->spec;
2462 struct nid_path *path;
2466 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2467 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2468 return 0; /* no need for analog loopback */
2470 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
2473 print_nid_path("loopback", path);
2474 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
2476 idx = path->idx[path->depth - 1];
2477 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2478 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2479 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
2482 path->ctls[NID_PATH_VOL_CTL] = val;
2485 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2486 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2487 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
2490 path->ctls[NID_PATH_MUTE_CTL] = val;
2493 path->active = true;
2494 add_loopback_list(spec, mix_nid, idx);
2498 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2500 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2501 return (pincap & AC_PINCAP_IN) != 0;
2504 /* Parse the codec tree and retrieve ADCs */
2505 static int fill_adc_nids(struct hda_codec *codec)
2507 struct hda_gen_spec *spec = codec->spec;
2509 hda_nid_t *adc_nids = spec->adc_nids;
2510 int max_nums = ARRAY_SIZE(spec->adc_nids);
2513 nid = codec->start_nid;
2514 for (i = 0; i < codec->num_nodes; i++, nid++) {
2515 unsigned int caps = get_wcaps(codec, nid);
2516 int type = get_wcaps_type(caps);
2518 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2520 adc_nids[nums] = nid;
2521 if (++nums >= max_nums)
2524 spec->num_adc_nids = nums;
2526 /* copy the detected ADCs to all_adcs[] */
2527 spec->num_all_adcs = nums;
2528 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2533 /* filter out invalid adc_nids that don't give all active input pins;
2534 * if needed, check whether dynamic ADC-switching is available
2536 static int check_dyn_adc_switch(struct hda_codec *codec)
2538 struct hda_gen_spec *spec = codec->spec;
2539 struct hda_input_mux *imux = &spec->input_mux;
2540 unsigned int ok_bits;
2546 for (n = 0; n < spec->num_adc_nids; n++) {
2547 for (i = 0; i < imux->num_items; i++) {
2548 if (!spec->input_paths[i][n])
2551 if (i >= imux->num_items) {
2552 ok_bits |= (1 << n);
2558 if (spec->shared_mic_hp) {
2559 spec->shared_mic_hp = 0;
2560 imux->num_items = 1;
2564 /* check whether ADC-switch is possible */
2565 for (i = 0; i < imux->num_items; i++) {
2566 for (n = 0; n < spec->num_adc_nids; n++) {
2567 if (spec->input_paths[i][n]) {
2568 spec->dyn_adc_idx[i] = n;
2574 snd_printdd("hda-codec: enabling ADC switching\n");
2575 spec->dyn_adc_switch = 1;
2576 } else if (nums != spec->num_adc_nids) {
2577 /* shrink the invalid adcs and input paths */
2579 for (n = 0; n < spec->num_adc_nids; n++) {
2580 if (!(ok_bits & (1 << n)))
2583 spec->adc_nids[nums] = spec->adc_nids[n];
2584 for (i = 0; i < imux->num_items; i++) {
2585 invalidate_nid_path(codec,
2586 spec->input_paths[i][nums]);
2587 spec->input_paths[i][nums] =
2588 spec->input_paths[i][n];
2593 spec->num_adc_nids = nums;
2596 if (imux->num_items == 1 || spec->shared_mic_hp) {
2597 snd_printdd("hda-codec: reducing to a single ADC\n");
2598 spec->num_adc_nids = 1; /* reduce to a single ADC */
2601 /* single index for individual volumes ctls */
2602 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2603 spec->num_adc_nids = 1;
2608 /* parse capture source paths from the given pin and create imux items */
2609 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2610 int cfg_idx, int num_adcs,
2611 const char *label, int anchor)
2613 struct hda_gen_spec *spec = codec->spec;
2614 struct hda_input_mux *imux = &spec->input_mux;
2615 int imux_idx = imux->num_items;
2616 bool imux_added = false;
2619 for (c = 0; c < num_adcs; c++) {
2620 struct nid_path *path;
2621 hda_nid_t adc = spec->adc_nids[c];
2623 if (!is_reachable_path(codec, pin, adc))
2625 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2628 print_nid_path("input", path);
2629 spec->input_paths[imux_idx][c] =
2630 snd_hda_get_path_idx(codec, path);
2633 spec->imux_pins[imux->num_items] = pin;
2634 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
2643 * create playback/capture controls for input pins
2646 /* fill the label for each input at first */
2647 static int fill_input_pin_labels(struct hda_codec *codec)
2649 struct hda_gen_spec *spec = codec->spec;
2650 const struct auto_pin_cfg *cfg = &spec->autocfg;
2653 for (i = 0; i < cfg->num_inputs; i++) {
2654 hda_nid_t pin = cfg->inputs[i].pin;
2658 if (!is_input_pin(codec, pin))
2661 label = hda_get_autocfg_input_label(codec, cfg, i);
2663 for (j = i - 1; j >= 0; j--) {
2664 if (spec->input_labels[j] &&
2665 !strcmp(spec->input_labels[j], label)) {
2666 idx = spec->input_label_idxs[j] + 1;
2671 spec->input_labels[i] = label;
2672 spec->input_label_idxs[i] = idx;
2678 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2680 static int create_input_ctls(struct hda_codec *codec)
2682 struct hda_gen_spec *spec = codec->spec;
2683 const struct auto_pin_cfg *cfg = &spec->autocfg;
2684 hda_nid_t mixer = spec->mixer_nid;
2689 num_adcs = fill_adc_nids(codec);
2693 err = fill_input_pin_labels(codec);
2697 for (i = 0; i < cfg->num_inputs; i++) {
2700 pin = cfg->inputs[i].pin;
2701 if (!is_input_pin(codec, pin))
2705 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2706 val |= snd_hda_get_default_vref(codec, pin);
2707 set_pin_target(codec, pin, val, false);
2710 if (is_reachable_path(codec, pin, mixer)) {
2711 err = new_analog_input(codec, i, pin,
2712 spec->input_labels[i],
2713 spec->input_label_idxs[i],
2720 err = parse_capture_source(codec, pin, i, num_adcs,
2721 spec->input_labels[i], -mixer);
2725 if (spec->add_in_jack_modes) {
2726 err = create_in_jack_mode(codec, pin);
2732 if (mixer && spec->add_stereo_mix_input) {
2733 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
2747 /* get the input path specified by the given adc and imux indices */
2748 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
2750 struct hda_gen_spec *spec = codec->spec;
2751 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2755 if (spec->dyn_adc_switch)
2756 adc_idx = spec->dyn_adc_idx[imux_idx];
2757 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
2761 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
2764 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2767 static int mux_enum_info(struct snd_kcontrol *kcontrol,
2768 struct snd_ctl_elem_info *uinfo)
2770 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2771 struct hda_gen_spec *spec = codec->spec;
2772 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2775 static int mux_enum_get(struct snd_kcontrol *kcontrol,
2776 struct snd_ctl_elem_value *ucontrol)
2778 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2779 struct hda_gen_spec *spec = codec->spec;
2780 /* the ctls are created at once with multiple counts */
2781 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2783 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2787 static int mux_enum_put(struct snd_kcontrol *kcontrol,
2788 struct snd_ctl_elem_value *ucontrol)
2790 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2791 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2792 return mux_select(codec, adc_idx,
2793 ucontrol->value.enumerated.item[0]);
2796 static const struct snd_kcontrol_new cap_src_temp = {
2797 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2798 .name = "Input Source",
2799 .info = mux_enum_info,
2800 .get = mux_enum_get,
2801 .put = mux_enum_put,
2805 * capture volume and capture switch ctls
2808 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2809 struct snd_ctl_elem_value *ucontrol);
2811 /* call the given amp update function for all amps in the imux list at once */
2812 static int cap_put_caller(struct snd_kcontrol *kcontrol,
2813 struct snd_ctl_elem_value *ucontrol,
2814 put_call_t func, int type)
2816 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2817 struct hda_gen_spec *spec = codec->spec;
2818 const struct hda_input_mux *imux;
2819 struct nid_path *path;
2820 int i, adc_idx, err = 0;
2822 imux = &spec->input_mux;
2823 adc_idx = kcontrol->id.index;
2824 mutex_lock(&codec->control_mutex);
2825 /* we use the cache-only update at first since multiple input paths
2826 * may shared the same amp; by updating only caches, the redundant
2827 * writes to hardware can be reduced.
2829 codec->cached_write = 1;
2830 for (i = 0; i < imux->num_items; i++) {
2831 path = get_input_path(codec, adc_idx, i);
2832 if (!path || !path->ctls[type])
2834 kcontrol->private_value = path->ctls[type];
2835 err = func(kcontrol, ucontrol);
2840 codec->cached_write = 0;
2841 mutex_unlock(&codec->control_mutex);
2842 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
2843 if (err >= 0 && spec->cap_sync_hook)
2844 spec->cap_sync_hook(codec, ucontrol);
2848 /* capture volume ctl callbacks */
2849 #define cap_vol_info snd_hda_mixer_amp_volume_info
2850 #define cap_vol_get snd_hda_mixer_amp_volume_get
2851 #define cap_vol_tlv snd_hda_mixer_amp_tlv
2853 static int cap_vol_put(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
2856 return cap_put_caller(kcontrol, ucontrol,
2857 snd_hda_mixer_amp_volume_put,
2861 static const struct snd_kcontrol_new cap_vol_temp = {
2862 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2863 .name = "Capture Volume",
2864 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2865 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2866 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2867 .info = cap_vol_info,
2870 .tlv = { .c = cap_vol_tlv },
2873 /* capture switch ctl callbacks */
2874 #define cap_sw_info snd_ctl_boolean_stereo_info
2875 #define cap_sw_get snd_hda_mixer_amp_switch_get
2877 static int cap_sw_put(struct snd_kcontrol *kcontrol,
2878 struct snd_ctl_elem_value *ucontrol)
2880 return cap_put_caller(kcontrol, ucontrol,
2881 snd_hda_mixer_amp_switch_put,
2885 static const struct snd_kcontrol_new cap_sw_temp = {
2886 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2887 .name = "Capture Switch",
2888 .info = cap_sw_info,
2893 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2898 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2899 for (depth = 0; depth < 3; depth++) {
2900 if (depth >= path->depth)
2902 i = path->depth - depth - 1;
2903 nid = path->path[i];
2904 if (!path->ctls[NID_PATH_VOL_CTL]) {
2905 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2906 path->ctls[NID_PATH_VOL_CTL] =
2907 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2908 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2909 int idx = path->idx[i];
2910 if (!depth && codec->single_adc_amp)
2912 path->ctls[NID_PATH_VOL_CTL] =
2913 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2916 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2917 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2918 path->ctls[NID_PATH_MUTE_CTL] =
2919 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2920 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2921 int idx = path->idx[i];
2922 if (!depth && codec->single_adc_amp)
2924 path->ctls[NID_PATH_MUTE_CTL] =
2925 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2932 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2934 struct hda_gen_spec *spec = codec->spec;
2935 struct auto_pin_cfg *cfg = &spec->autocfg;
2939 if (!spec->inv_dmic_split)
2941 for (i = 0; i < cfg->num_inputs; i++) {
2942 if (cfg->inputs[i].pin != nid)
2944 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2946 val = snd_hda_codec_get_pincfg(codec, nid);
2947 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2952 /* capture switch put callback for a single control with hook call */
2953 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
2954 struct snd_ctl_elem_value *ucontrol)
2956 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2957 struct hda_gen_spec *spec = codec->spec;
2960 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2964 if (spec->cap_sync_hook)
2965 spec->cap_sync_hook(codec, ucontrol);
2970 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2971 int idx, bool is_switch, unsigned int ctl,
2974 struct hda_gen_spec *spec = codec->spec;
2976 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2977 const char *sfx = is_switch ? "Switch" : "Volume";
2978 unsigned int chs = inv_dmic ? 1 : 3;
2979 struct snd_kcontrol_new *knew;
2985 snprintf(tmpname, sizeof(tmpname),
2986 "%s Capture %s", label, sfx);
2988 snprintf(tmpname, sizeof(tmpname),
2990 knew = add_control(spec, type, tmpname, idx,
2991 amp_val_replace_channels(ctl, chs));
2995 knew->put = cap_single_sw_put;
2999 /* Make independent right kcontrol */
3001 snprintf(tmpname, sizeof(tmpname),
3002 "Inverted %s Capture %s", label, sfx);
3004 snprintf(tmpname, sizeof(tmpname),
3005 "Inverted Capture %s", sfx);
3006 knew = add_control(spec, type, tmpname, idx,
3007 amp_val_replace_channels(ctl, 2));
3011 knew->put = cap_single_sw_put;
3015 /* create single (and simple) capture volume and switch controls */
3016 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3017 unsigned int vol_ctl, unsigned int sw_ctl,
3021 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3024 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3030 /* create bound capture volume and switch controls */
3031 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3032 unsigned int vol_ctl, unsigned int sw_ctl)
3034 struct hda_gen_spec *spec = codec->spec;
3035 struct snd_kcontrol_new *knew;
3038 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3042 knew->private_value = vol_ctl;
3043 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3046 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3050 knew->private_value = sw_ctl;
3051 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3056 /* return the vol ctl when used first in the imux list */
3057 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3059 struct nid_path *path;
3063 path = get_input_path(codec, 0, idx);
3066 ctl = path->ctls[type];
3069 for (i = 0; i < idx - 1; i++) {
3070 path = get_input_path(codec, 0, i);
3071 if (path && path->ctls[type] == ctl)
3077 /* create individual capture volume and switch controls per input */
3078 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3080 struct hda_gen_spec *spec = codec->spec;
3081 struct hda_input_mux *imux = &spec->input_mux;
3084 for (i = 0; i < imux->num_items; i++) {
3088 idx = imux->items[i].index;
3089 if (idx >= spec->autocfg.num_inputs)
3091 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3093 for (type = 0; type < 2; type++) {
3094 err = add_single_cap_ctl(codec,
3095 spec->input_labels[idx],
3096 spec->input_label_idxs[idx],
3098 get_first_cap_ctl(codec, i, type),
3107 static int create_capture_mixers(struct hda_codec *codec)
3109 struct hda_gen_spec *spec = codec->spec;
3110 struct hda_input_mux *imux = &spec->input_mux;
3111 int i, n, nums, err;
3113 if (spec->dyn_adc_switch)
3116 nums = spec->num_adc_nids;
3118 if (!spec->auto_mic && imux->num_items > 1) {
3119 struct snd_kcontrol_new *knew;
3121 name = nums > 1 ? "Input Source" : "Capture Source";
3122 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3128 for (n = 0; n < nums; n++) {
3130 bool multi_cap_vol = spec->multi_cap_vol;
3131 bool inv_dmic = false;
3135 for (i = 0; i < imux->num_items; i++) {
3136 struct nid_path *path;
3137 path = get_input_path(codec, n, i);
3140 parse_capvol_in_path(codec, path);
3142 vol = path->ctls[NID_PATH_VOL_CTL];
3143 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3145 if (!same_amp_caps(codec, vol,
3146 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3147 multi_cap_vol = true;
3150 sw = path->ctls[NID_PATH_MUTE_CTL];
3151 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3153 if (!same_amp_caps(codec, sw,
3154 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3155 multi_cap_vol = true;
3157 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3162 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3164 else if (!multi_cap_vol)
3165 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3167 err = create_multi_cap_vol_ctl(codec);
3176 * add mic boosts if needed
3179 /* check whether the given amp is feasible as a boost volume */
3180 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3185 if (!nid_has_volume(codec, nid, dir) ||
3186 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3187 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3190 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3191 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3197 /* look for a boost amp in a widget close to the pin */
3198 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3199 struct nid_path *path)
3201 unsigned int val = 0;
3205 for (depth = 0; depth < 3; depth++) {
3206 if (depth >= path->depth - 1)
3208 nid = path->path[depth];
3209 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3210 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3212 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3213 path->idx[depth])) {
3214 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3223 static int parse_mic_boost(struct hda_codec *codec)
3225 struct hda_gen_spec *spec = codec->spec;
3226 struct auto_pin_cfg *cfg = &spec->autocfg;
3227 struct hda_input_mux *imux = &spec->input_mux;
3230 if (!spec->num_adc_nids)
3233 for (i = 0; i < imux->num_items; i++) {
3234 struct nid_path *path;
3237 char boost_label[44];
3239 idx = imux->items[i].index;
3240 if (idx >= imux->num_items)
3243 /* check only line-in and mic pins */
3244 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3247 path = get_input_path(codec, 0, i);
3251 val = look_for_boost_amp(codec, path);
3255 /* create a boost control */
3256 snprintf(boost_label, sizeof(boost_label),
3257 "%s Boost Volume", spec->input_labels[idx]);
3258 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3259 spec->input_label_idxs[idx], val))
3262 path->ctls[NID_PATH_BOOST_CTL] = val;
3268 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3270 static void parse_digital(struct hda_codec *codec)
3272 struct hda_gen_spec *spec = codec->spec;
3273 struct nid_path *path;
3275 hda_nid_t dig_nid, pin;
3277 /* support multiple SPDIFs; the secondary is set up as a slave */
3279 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3280 pin = spec->autocfg.dig_out_pins[i];
3281 dig_nid = look_for_dac(codec, pin, true);
3284 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3287 print_nid_path("digout", path);
3288 path->active = true;
3289 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3290 set_pin_target(codec, pin, PIN_OUT, false);
3292 spec->multiout.dig_out_nid = dig_nid;
3293 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3295 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3296 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3298 spec->slave_dig_outs[nums - 1] = dig_nid;
3303 if (spec->autocfg.dig_in_pin) {
3304 pin = spec->autocfg.dig_in_pin;
3305 dig_nid = codec->start_nid;
3306 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3307 unsigned int wcaps = get_wcaps(codec, dig_nid);
3308 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3310 if (!(wcaps & AC_WCAP_DIGITAL))
3312 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3314 print_nid_path("digin", path);
3315 path->active = true;
3316 spec->dig_in_nid = dig_nid;
3317 spec->digin_path = snd_hda_get_path_idx(codec, path);
3318 set_pin_target(codec, pin, PIN_IN, false);
3327 * input MUX handling
3330 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3332 /* select the given imux item; either unmute exclusively or select the route */
3333 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3336 struct hda_gen_spec *spec = codec->spec;
3337 const struct hda_input_mux *imux;
3338 struct nid_path *path;
3340 imux = &spec->input_mux;
3341 if (!imux->num_items)
3344 if (idx >= imux->num_items)
3345 idx = imux->num_items - 1;
3346 if (spec->cur_mux[adc_idx] == idx)
3349 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3353 snd_hda_activate_path(codec, path, false, false);
3355 spec->cur_mux[adc_idx] = idx;
3357 if (spec->shared_mic_hp)
3358 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3360 if (spec->dyn_adc_switch)
3361 dyn_adc_pcm_resetup(codec, idx);
3363 path = get_input_path(codec, adc_idx, idx);
3368 snd_hda_activate_path(codec, path, true, false);
3369 if (spec->cap_sync_hook)
3370 spec->cap_sync_hook(codec, NULL);
3376 * Jack detections for HP auto-mute and mic-switch
3379 /* check each pin in the given array; returns true if any of them is plugged */
3380 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3384 for (i = 0; i < num_pins; i++) {
3385 hda_nid_t nid = pins[i];
3388 /* don't detect pins retasked as inputs */
3389 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3391 present |= snd_hda_jack_detect(codec, nid);
3396 /* standard HP/line-out auto-mute helper */
3397 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
3400 struct hda_gen_spec *spec = codec->spec;
3403 for (i = 0; i < num_pins; i++) {
3404 hda_nid_t nid = pins[i];
3408 /* don't reset VREF value in case it's controlling
3409 * the amp (see alc861_fixup_asus_amp_vref_0f())
3411 if (spec->keep_vref_in_automute)
3412 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3416 val |= snd_hda_codec_get_pin_target(codec, nid);
3417 /* here we call update_pin_ctl() so that the pinctl is changed
3418 * without changing the pinctl target value;
3419 * the original target value will be still referred at the
3420 * init / resume again
3422 update_pin_ctl(codec, nid, val);
3423 set_pin_eapd(codec, nid, !mute);
3427 /* Toggle outputs muting */
3428 void snd_hda_gen_update_outputs(struct hda_codec *codec)
3430 struct hda_gen_spec *spec = codec->spec;
3433 /* Control HP pins/amps depending on master_mute state;
3434 * in general, HP pins/amps control should be enabled in all cases,
3435 * but currently set only for master_mute, just to be safe
3437 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3438 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3439 spec->autocfg.hp_pins, spec->master_mute);
3441 if (!spec->automute_speaker)
3444 on = spec->hp_jack_present | spec->line_jack_present;
3445 on |= spec->master_mute;
3446 spec->speaker_muted = on;
3447 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
3448 spec->autocfg.speaker_pins, on);
3450 /* toggle line-out mutes if needed, too */
3451 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3452 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3453 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3455 if (!spec->automute_lo)
3458 on = spec->hp_jack_present;
3459 on |= spec->master_mute;
3460 spec->line_out_muted = on;
3461 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3462 spec->autocfg.line_out_pins, on);
3464 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
3466 static void call_update_outputs(struct hda_codec *codec)
3468 struct hda_gen_spec *spec = codec->spec;
3469 if (spec->automute_hook)
3470 spec->automute_hook(codec);
3472 snd_hda_gen_update_outputs(codec);
3475 /* standard HP-automute helper */
3476 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3478 struct hda_gen_spec *spec = codec->spec;
3479 hda_nid_t *pins = spec->autocfg.hp_pins;
3480 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
3482 /* No detection for the first HP jack during indep-HP mode */
3483 if (spec->indep_hp_enabled) {
3488 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
3489 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3491 call_update_outputs(codec);
3493 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
3495 /* standard line-out-automute helper */
3496 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3498 struct hda_gen_spec *spec = codec->spec;
3500 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3502 /* check LO jack only when it's different from HP */
3503 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3506 spec->line_jack_present =
3507 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3508 spec->autocfg.line_out_pins);
3509 if (!spec->automute_speaker || !spec->detect_lo)
3511 call_update_outputs(codec);
3513 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
3515 /* standard mic auto-switch helper */
3516 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
3518 struct hda_gen_spec *spec = codec->spec;
3521 if (!spec->auto_mic)
3524 for (i = spec->am_num_entries - 1; i > 0; i--) {
3525 hda_nid_t pin = spec->am_entry[i].pin;
3526 /* don't detect pins retasked as outputs */
3527 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3529 if (snd_hda_jack_detect(codec, pin)) {
3530 mux_select(codec, 0, spec->am_entry[i].idx);
3534 mux_select(codec, 0, spec->am_entry[0].idx);
3536 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
3538 /* update jack retasking */
3539 static void update_automute_all(struct hda_codec *codec)
3541 struct hda_gen_spec *spec = codec->spec;
3543 if (spec->hp_automute_hook)
3544 spec->hp_automute_hook(codec, NULL);
3546 snd_hda_gen_hp_automute(codec, NULL);
3547 if (spec->line_automute_hook)
3548 spec->line_automute_hook(codec, NULL);
3550 snd_hda_gen_line_automute(codec, NULL);
3551 if (spec->mic_autoswitch_hook)
3552 spec->mic_autoswitch_hook(codec, NULL);
3554 snd_hda_gen_mic_autoswitch(codec, NULL);
3558 * Auto-Mute mode mixer enum support
3560 static int automute_mode_info(struct snd_kcontrol *kcontrol,
3561 struct snd_ctl_elem_info *uinfo)
3563 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3564 struct hda_gen_spec *spec = codec->spec;
3565 static const char * const texts3[] = {
3566 "Disabled", "Speaker Only", "Line Out+Speaker"
3569 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3570 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3571 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3574 static int automute_mode_get(struct snd_kcontrol *kcontrol,
3575 struct snd_ctl_elem_value *ucontrol)
3577 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3578 struct hda_gen_spec *spec = codec->spec;
3579 unsigned int val = 0;
3580 if (spec->automute_speaker)
3582 if (spec->automute_lo)
3585 ucontrol->value.enumerated.item[0] = val;
3589 static int automute_mode_put(struct snd_kcontrol *kcontrol,
3590 struct snd_ctl_elem_value *ucontrol)
3592 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3593 struct hda_gen_spec *spec = codec->spec;
3595 switch (ucontrol->value.enumerated.item[0]) {
3597 if (!spec->automute_speaker && !spec->automute_lo)
3599 spec->automute_speaker = 0;
3600 spec->automute_lo = 0;
3603 if (spec->automute_speaker_possible) {
3604 if (!spec->automute_lo && spec->automute_speaker)
3606 spec->automute_speaker = 1;
3607 spec->automute_lo = 0;
3608 } else if (spec->automute_lo_possible) {
3609 if (spec->automute_lo)
3611 spec->automute_lo = 1;
3616 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3618 if (spec->automute_speaker && spec->automute_lo)
3620 spec->automute_speaker = 1;
3621 spec->automute_lo = 1;
3626 call_update_outputs(codec);
3630 static const struct snd_kcontrol_new automute_mode_enum = {
3631 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3632 .name = "Auto-Mute Mode",
3633 .info = automute_mode_info,
3634 .get = automute_mode_get,
3635 .put = automute_mode_put,
3638 static int add_automute_mode_enum(struct hda_codec *codec)
3640 struct hda_gen_spec *spec = codec->spec;
3642 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
3648 * Check the availability of HP/line-out auto-mute;
3649 * Set up appropriately if really supported
3651 static int check_auto_mute_availability(struct hda_codec *codec)
3653 struct hda_gen_spec *spec = codec->spec;
3654 struct auto_pin_cfg *cfg = &spec->autocfg;
3658 if (spec->suppress_auto_mute)
3661 if (cfg->hp_pins[0])
3663 if (cfg->line_out_pins[0])
3665 if (cfg->speaker_pins[0])
3667 if (present < 2) /* need two different output types */
3670 if (!cfg->speaker_pins[0] &&
3671 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3672 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3673 sizeof(cfg->speaker_pins));
3674 cfg->speaker_outs = cfg->line_outs;
3677 if (!cfg->hp_pins[0] &&
3678 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3679 memcpy(cfg->hp_pins, cfg->line_out_pins,
3680 sizeof(cfg->hp_pins));
3681 cfg->hp_outs = cfg->line_outs;
3684 for (i = 0; i < cfg->hp_outs; i++) {
3685 hda_nid_t nid = cfg->hp_pins[i];
3686 if (!is_jack_detectable(codec, nid))
3688 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3690 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
3691 spec->hp_automute_hook ?
3692 spec->hp_automute_hook :
3693 snd_hda_gen_hp_automute);
3694 spec->detect_hp = 1;
3697 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3698 if (cfg->speaker_outs)
3699 for (i = 0; i < cfg->line_outs; i++) {
3700 hda_nid_t nid = cfg->line_out_pins[i];
3701 if (!is_jack_detectable(codec, nid))
3703 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3704 snd_hda_jack_detect_enable_callback(codec, nid,
3705 HDA_GEN_FRONT_EVENT,
3706 spec->line_automute_hook ?
3707 spec->line_automute_hook :
3708 snd_hda_gen_line_automute);
3709 spec->detect_lo = 1;
3711 spec->automute_lo_possible = spec->detect_hp;
3714 spec->automute_speaker_possible = cfg->speaker_outs &&
3715 (spec->detect_hp || spec->detect_lo);
3717 spec->automute_lo = spec->automute_lo_possible;
3718 spec->automute_speaker = spec->automute_speaker_possible;
3720 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3721 /* create a control for automute mode */
3722 err = add_automute_mode_enum(codec);
3729 /* check whether all auto-mic pins are valid; setup indices if OK */
3730 static bool auto_mic_check_imux(struct hda_codec *codec)
3732 struct hda_gen_spec *spec = codec->spec;
3733 const struct hda_input_mux *imux;
3736 imux = &spec->input_mux;
3737 for (i = 0; i < spec->am_num_entries; i++) {
3738 spec->am_entry[i].idx =
3739 find_idx_in_nid_list(spec->am_entry[i].pin,
3740 spec->imux_pins, imux->num_items);
3741 if (spec->am_entry[i].idx < 0)
3742 return false; /* no corresponding imux */
3745 /* we don't need the jack detection for the first pin */
3746 for (i = 1; i < spec->am_num_entries; i++)
3747 snd_hda_jack_detect_enable_callback(codec,
3748 spec->am_entry[i].pin,
3750 spec->mic_autoswitch_hook ?
3751 spec->mic_autoswitch_hook :
3752 snd_hda_gen_mic_autoswitch);
3756 static int compare_attr(const void *ap, const void *bp)
3758 const struct automic_entry *a = ap;
3759 const struct automic_entry *b = bp;
3760 return (int)(a->attr - b->attr);
3764 * Check the availability of auto-mic switch;
3765 * Set up if really supported
3767 static int check_auto_mic_availability(struct hda_codec *codec)
3769 struct hda_gen_spec *spec = codec->spec;
3770 struct auto_pin_cfg *cfg = &spec->autocfg;
3774 if (spec->suppress_auto_mic)
3779 for (i = 0; i < cfg->num_inputs; i++) {
3780 hda_nid_t nid = cfg->inputs[i].pin;
3782 attr = snd_hda_codec_get_pincfg(codec, nid);
3783 attr = snd_hda_get_input_pin_attr(attr);
3784 if (types & (1 << attr))
3785 return 0; /* already occupied */
3787 case INPUT_PIN_ATTR_INT:
3788 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3789 return 0; /* invalid type */
3791 case INPUT_PIN_ATTR_UNUSED:
3792 return 0; /* invalid entry */
3794 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3795 return 0; /* invalid type */
3796 if (!spec->line_in_auto_switch &&
3797 cfg->inputs[i].type != AUTO_PIN_MIC)
3798 return 0; /* only mic is allowed */
3799 if (!is_jack_detectable(codec, nid))
3800 return 0; /* no unsol support */
3803 if (num_pins >= MAX_AUTO_MIC_PINS)
3805 types |= (1 << attr);
3806 spec->am_entry[num_pins].pin = nid;
3807 spec->am_entry[num_pins].attr = attr;
3814 spec->am_num_entries = num_pins;
3815 /* sort the am_entry in the order of attr so that the pin with a
3816 * higher attr will be selected when the jack is plugged.
3818 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3819 compare_attr, NULL);
3821 if (!auto_mic_check_imux(codec))
3825 spec->num_adc_nids = 1;
3826 spec->cur_mux[0] = spec->am_entry[0].idx;
3827 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3828 spec->am_entry[0].pin,
3829 spec->am_entry[1].pin,
3830 spec->am_entry[2].pin);
3837 * Parse the given BIOS configuration and set up the hda_gen_spec
3839 * return 1 if successful, 0 if the proper config is not found,
3840 * or a negative error code
3842 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
3843 struct auto_pin_cfg *cfg)
3845 struct hda_gen_spec *spec = codec->spec;
3848 parse_user_hints(codec);
3850 if (cfg != &spec->autocfg) {
3851 spec->autocfg = *cfg;
3852 cfg = &spec->autocfg;
3855 fill_all_dac_nids(codec);
3857 if (!cfg->line_outs) {
3858 if (cfg->dig_outs || cfg->dig_in_pin) {
3859 spec->multiout.max_channels = 2;
3860 spec->no_analog = 1;
3863 return 0; /* can't find valid BIOS pin config */
3866 if (!spec->no_primary_hp &&
3867 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3868 cfg->line_outs <= cfg->hp_outs) {
3869 /* use HP as primary out */
3870 cfg->speaker_outs = cfg->line_outs;
3871 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3872 sizeof(cfg->speaker_pins));
3873 cfg->line_outs = cfg->hp_outs;
3874 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3876 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3877 cfg->line_out_type = AUTO_PIN_HP_OUT;
3880 err = parse_output_paths(codec);
3883 err = create_multi_channel_mode(codec);
3886 err = create_multi_out_ctls(codec, cfg);
3889 err = create_hp_out_ctls(codec);
3892 err = create_speaker_out_ctls(codec);
3895 err = create_indep_hp_ctls(codec);
3898 err = create_loopback_mixing_ctl(codec);
3901 err = create_shared_input(codec);
3904 err = create_input_ctls(codec);
3908 spec->const_channel_count = spec->ext_channel_count;
3909 /* check the multiple speaker and headphone pins */
3910 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3911 spec->const_channel_count = max(spec->const_channel_count,
3912 cfg->speaker_outs * 2);
3913 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3914 spec->const_channel_count = max(spec->const_channel_count,
3916 spec->multiout.max_channels = max(spec->ext_channel_count,
3917 spec->const_channel_count);
3919 err = check_auto_mute_availability(codec);
3923 err = check_dyn_adc_switch(codec);
3927 if (!spec->shared_mic_hp) {
3928 err = check_auto_mic_availability(codec);
3933 err = create_capture_mixers(codec);
3937 err = parse_mic_boost(codec);
3941 if (spec->add_out_jack_modes) {
3942 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
3943 err = create_out_jack_modes(codec, cfg->line_outs,
3944 cfg->line_out_pins);
3948 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
3949 err = create_out_jack_modes(codec, cfg->hp_outs,
3957 parse_digital(codec);
3961 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
3965 * Build control elements
3968 /* slave controls for virtual master */
3969 static const char * const slave_pfxs[] = {
3970 "Front", "Surround", "Center", "LFE", "Side",
3971 "Headphone", "Speaker", "Mono", "Line Out",
3972 "CLFE", "Bass Speaker", "PCM",
3973 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3974 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3979 int snd_hda_gen_build_controls(struct hda_codec *codec)
3981 struct hda_gen_spec *spec = codec->spec;
3984 if (spec->kctls.used) {
3985 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3990 if (spec->multiout.dig_out_nid) {
3991 err = snd_hda_create_dig_out_ctls(codec,
3992 spec->multiout.dig_out_nid,
3993 spec->multiout.dig_out_nid,
3994 spec->pcm_rec[1].pcm_type);
3997 if (!spec->no_analog) {
3998 err = snd_hda_create_spdif_share_sw(codec,
4002 spec->multiout.share_spdif = 1;
4005 if (spec->dig_in_nid) {
4006 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4011 /* if we have no master control, let's create it */
4012 if (!spec->no_analog &&
4013 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
4014 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
4015 spec->vmaster_tlv, slave_pfxs,
4020 if (!spec->no_analog &&
4021 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4022 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4025 true, &spec->vmaster_mute.sw_kctl);
4028 if (spec->vmaster_mute.hook)
4029 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4030 spec->vmaster_mute_enum);
4033 free_kctls(spec); /* no longer needed */
4035 if (spec->shared_mic_hp) {
4037 int nid = spec->autocfg.inputs[1].pin;
4038 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
4041 err = snd_hda_jack_detect_enable(codec, nid, 0);
4046 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4052 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4059 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4060 struct hda_codec *codec,
4061 struct snd_pcm_substream *substream,
4064 struct hda_gen_spec *spec = codec->spec;
4065 if (spec->pcm_playback_hook)
4066 spec->pcm_playback_hook(hinfo, codec, substream, action);
4069 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4070 struct hda_codec *codec,
4071 struct snd_pcm_substream *substream,
4074 struct hda_gen_spec *spec = codec->spec;
4075 if (spec->pcm_capture_hook)
4076 spec->pcm_capture_hook(hinfo, codec, substream, action);
4080 * Analog playback callbacks
4082 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4083 struct hda_codec *codec,
4084 struct snd_pcm_substream *substream)
4086 struct hda_gen_spec *spec = codec->spec;
4089 mutex_lock(&spec->pcm_mutex);
4090 err = snd_hda_multi_out_analog_open(codec,
4091 &spec->multiout, substream,
4094 spec->active_streams |= 1 << STREAM_MULTI_OUT;
4095 call_pcm_playback_hook(hinfo, codec, substream,
4096 HDA_GEN_PCM_ACT_OPEN);
4098 mutex_unlock(&spec->pcm_mutex);
4102 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4103 struct hda_codec *codec,
4104 unsigned int stream_tag,
4105 unsigned int format,
4106 struct snd_pcm_substream *substream)
4108 struct hda_gen_spec *spec = codec->spec;
4111 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4112 stream_tag, format, substream);
4114 call_pcm_playback_hook(hinfo, codec, substream,
4115 HDA_GEN_PCM_ACT_PREPARE);
4119 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4120 struct hda_codec *codec,
4121 struct snd_pcm_substream *substream)
4123 struct hda_gen_spec *spec = codec->spec;
4126 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4128 call_pcm_playback_hook(hinfo, codec, substream,
4129 HDA_GEN_PCM_ACT_CLEANUP);
4133 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4134 struct hda_codec *codec,
4135 struct snd_pcm_substream *substream)
4137 struct hda_gen_spec *spec = codec->spec;
4138 mutex_lock(&spec->pcm_mutex);
4139 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
4140 call_pcm_playback_hook(hinfo, codec, substream,
4141 HDA_GEN_PCM_ACT_CLOSE);
4142 mutex_unlock(&spec->pcm_mutex);
4146 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4147 struct hda_codec *codec,
4148 struct snd_pcm_substream *substream)
4150 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4154 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4155 struct hda_codec *codec,
4156 unsigned int stream_tag,
4157 unsigned int format,
4158 struct snd_pcm_substream *substream)
4160 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4161 call_pcm_capture_hook(hinfo, codec, substream,
4162 HDA_GEN_PCM_ACT_PREPARE);
4166 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4167 struct hda_codec *codec,
4168 struct snd_pcm_substream *substream)
4170 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4171 call_pcm_capture_hook(hinfo, codec, substream,
4172 HDA_GEN_PCM_ACT_CLEANUP);
4176 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4177 struct hda_codec *codec,
4178 struct snd_pcm_substream *substream)
4180 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4184 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4185 struct hda_codec *codec,
4186 struct snd_pcm_substream *substream)
4188 struct hda_gen_spec *spec = codec->spec;
4191 mutex_lock(&spec->pcm_mutex);
4192 if (!spec->indep_hp_enabled)
4195 spec->active_streams |= 1 << STREAM_INDEP_HP;
4196 call_pcm_playback_hook(hinfo, codec, substream,
4197 HDA_GEN_PCM_ACT_OPEN);
4198 mutex_unlock(&spec->pcm_mutex);
4202 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4203 struct hda_codec *codec,
4204 struct snd_pcm_substream *substream)
4206 struct hda_gen_spec *spec = codec->spec;
4207 mutex_lock(&spec->pcm_mutex);
4208 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
4209 call_pcm_playback_hook(hinfo, codec, substream,
4210 HDA_GEN_PCM_ACT_CLOSE);
4211 mutex_unlock(&spec->pcm_mutex);
4215 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4216 struct hda_codec *codec,
4217 unsigned int stream_tag,
4218 unsigned int format,
4219 struct snd_pcm_substream *substream)
4221 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4222 call_pcm_playback_hook(hinfo, codec, substream,
4223 HDA_GEN_PCM_ACT_PREPARE);
4227 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4228 struct hda_codec *codec,
4229 struct snd_pcm_substream *substream)
4231 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4232 call_pcm_playback_hook(hinfo, codec, substream,
4233 HDA_GEN_PCM_ACT_CLEANUP);
4240 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4241 struct hda_codec *codec,
4242 struct snd_pcm_substream *substream)
4244 struct hda_gen_spec *spec = codec->spec;
4245 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4248 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4249 struct hda_codec *codec,
4250 unsigned int stream_tag,
4251 unsigned int format,
4252 struct snd_pcm_substream *substream)
4254 struct hda_gen_spec *spec = codec->spec;
4255 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4256 stream_tag, format, substream);
4259 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4260 struct hda_codec *codec,
4261 struct snd_pcm_substream *substream)
4263 struct hda_gen_spec *spec = codec->spec;
4264 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4267 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4268 struct hda_codec *codec,
4269 struct snd_pcm_substream *substream)
4271 struct hda_gen_spec *spec = codec->spec;
4272 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4278 #define alt_capture_pcm_open capture_pcm_open
4279 #define alt_capture_pcm_close capture_pcm_close
4281 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4282 struct hda_codec *codec,
4283 unsigned int stream_tag,
4284 unsigned int format,
4285 struct snd_pcm_substream *substream)
4287 struct hda_gen_spec *spec = codec->spec;
4289 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
4290 stream_tag, 0, format);
4291 call_pcm_capture_hook(hinfo, codec, substream,
4292 HDA_GEN_PCM_ACT_PREPARE);
4296 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4297 struct hda_codec *codec,
4298 struct snd_pcm_substream *substream)
4300 struct hda_gen_spec *spec = codec->spec;
4302 snd_hda_codec_cleanup_stream(codec,
4303 spec->adc_nids[substream->number + 1]);
4304 call_pcm_capture_hook(hinfo, codec, substream,
4305 HDA_GEN_PCM_ACT_CLEANUP);
4311 static const struct hda_pcm_stream pcm_analog_playback = {
4315 /* NID is set in build_pcms */
4317 .open = playback_pcm_open,
4318 .close = playback_pcm_close,
4319 .prepare = playback_pcm_prepare,
4320 .cleanup = playback_pcm_cleanup
4324 static const struct hda_pcm_stream pcm_analog_capture = {
4328 /* NID is set in build_pcms */
4330 .open = capture_pcm_open,
4331 .close = capture_pcm_close,
4332 .prepare = capture_pcm_prepare,
4333 .cleanup = capture_pcm_cleanup
4337 static const struct hda_pcm_stream pcm_analog_alt_playback = {
4341 /* NID is set in build_pcms */
4343 .open = alt_playback_pcm_open,
4344 .close = alt_playback_pcm_close,
4345 .prepare = alt_playback_pcm_prepare,
4346 .cleanup = alt_playback_pcm_cleanup
4350 static const struct hda_pcm_stream pcm_analog_alt_capture = {
4351 .substreams = 2, /* can be overridden */
4354 /* NID is set in build_pcms */
4356 .open = alt_capture_pcm_open,
4357 .close = alt_capture_pcm_close,
4358 .prepare = alt_capture_pcm_prepare,
4359 .cleanup = alt_capture_pcm_cleanup
4363 static const struct hda_pcm_stream pcm_digital_playback = {
4367 /* NID is set in build_pcms */
4369 .open = dig_playback_pcm_open,
4370 .close = dig_playback_pcm_close,
4371 .prepare = dig_playback_pcm_prepare,
4372 .cleanup = dig_playback_pcm_cleanup
4376 static const struct hda_pcm_stream pcm_digital_capture = {
4380 /* NID is set in build_pcms */
4383 /* Used by build_pcms to flag that a PCM has no playback stream */
4384 static const struct hda_pcm_stream pcm_null_stream = {
4391 * dynamic changing ADC PCM streams
4393 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4395 struct hda_gen_spec *spec = codec->spec;
4396 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4398 if (spec->cur_adc && spec->cur_adc != new_adc) {
4399 /* stream is running, let's swap the current ADC */
4400 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4401 spec->cur_adc = new_adc;
4402 snd_hda_codec_setup_stream(codec, new_adc,
4403 spec->cur_adc_stream_tag, 0,
4404 spec->cur_adc_format);
4410 /* analog capture with dynamic dual-adc changes */
4411 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4412 struct hda_codec *codec,
4413 unsigned int stream_tag,
4414 unsigned int format,
4415 struct snd_pcm_substream *substream)
4417 struct hda_gen_spec *spec = codec->spec;
4418 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4419 spec->cur_adc_stream_tag = stream_tag;
4420 spec->cur_adc_format = format;
4421 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4425 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4426 struct hda_codec *codec,
4427 struct snd_pcm_substream *substream)
4429 struct hda_gen_spec *spec = codec->spec;
4430 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4435 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4439 .nid = 0, /* fill later */
4441 .prepare = dyn_adc_capture_pcm_prepare,
4442 .cleanup = dyn_adc_capture_pcm_cleanup
4446 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4447 const char *chip_name)
4453 strlcpy(str, chip_name, len);
4455 /* drop non-alnum chars after a space */
4456 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4457 if (!isalnum(p[1])) {
4462 strlcat(str, sfx, len);
4465 /* build PCM streams based on the parsed results */
4466 int snd_hda_gen_build_pcms(struct hda_codec *codec)
4468 struct hda_gen_spec *spec = codec->spec;
4469 struct hda_pcm *info = spec->pcm_rec;
4470 const struct hda_pcm_stream *p;
4471 bool have_multi_adcs;
4473 codec->num_pcms = 1;
4474 codec->pcm_info = info;
4476 if (spec->no_analog)
4479 fill_pcm_stream_name(spec->stream_name_analog,
4480 sizeof(spec->stream_name_analog),
4481 " Analog", codec->chip_name);
4482 info->name = spec->stream_name_analog;
4484 if (spec->multiout.num_dacs > 0) {
4485 p = spec->stream_analog_playback;
4487 p = &pcm_analog_playback;
4488 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4489 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4490 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4491 spec->multiout.max_channels;
4492 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4493 spec->autocfg.line_outs == 2)
4494 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4497 if (spec->num_adc_nids) {
4498 p = spec->stream_analog_capture;
4500 if (spec->dyn_adc_switch)
4501 p = &dyn_adc_pcm_analog_capture;
4503 p = &pcm_analog_capture;
4505 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4506 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4510 /* SPDIF for stream index #1 */
4511 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
4512 fill_pcm_stream_name(spec->stream_name_digital,
4513 sizeof(spec->stream_name_digital),
4514 " Digital", codec->chip_name);
4515 codec->num_pcms = 2;
4516 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4517 info = spec->pcm_rec + 1;
4518 info->name = spec->stream_name_digital;
4519 if (spec->dig_out_type)
4520 info->pcm_type = spec->dig_out_type;
4522 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4523 if (spec->multiout.dig_out_nid) {
4524 p = spec->stream_digital_playback;
4526 p = &pcm_digital_playback;
4527 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4528 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4530 if (spec->dig_in_nid) {
4531 p = spec->stream_digital_capture;
4533 p = &pcm_digital_capture;
4534 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4535 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4539 if (spec->no_analog)
4542 /* If the use of more than one ADC is requested for the current
4543 * model, configure a second analog capture-only PCM.
4545 have_multi_adcs = (spec->num_adc_nids > 1) &&
4546 !spec->dyn_adc_switch && !spec->auto_mic;
4547 /* Additional Analaog capture for index #2 */
4548 if (spec->alt_dac_nid || have_multi_adcs) {
4549 fill_pcm_stream_name(spec->stream_name_alt_analog,
4550 sizeof(spec->stream_name_alt_analog),
4551 " Alt Analog", codec->chip_name);
4552 codec->num_pcms = 3;
4553 info = spec->pcm_rec + 2;
4554 info->name = spec->stream_name_alt_analog;
4555 if (spec->alt_dac_nid) {
4556 p = spec->stream_analog_alt_playback;
4558 p = &pcm_analog_alt_playback;
4559 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4560 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4563 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4565 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4567 if (have_multi_adcs) {
4568 p = spec->stream_analog_alt_capture;
4570 p = &pcm_analog_alt_capture;
4571 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4572 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4574 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4575 spec->num_adc_nids - 1;
4577 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4579 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4585 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4589 * Standard auto-parser initializations
4592 /* configure the given path as a proper output */
4593 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
4595 struct nid_path *path;
4598 path = snd_hda_get_path_from_idx(codec, path_idx);
4599 if (!path || !path->depth)
4601 pin = path->path[path->depth - 1];
4602 restore_pin_ctl(codec, pin);
4603 snd_hda_activate_path(codec, path, path->active, true);
4604 set_pin_eapd(codec, pin, path->active);
4607 /* initialize primary output paths */
4608 static void init_multi_out(struct hda_codec *codec)
4610 struct hda_gen_spec *spec = codec->spec;
4613 for (i = 0; i < spec->autocfg.line_outs; i++)
4614 set_output_and_unmute(codec, spec->out_paths[i]);
4618 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
4622 for (i = 0; i < num_outs; i++)
4623 set_output_and_unmute(codec, paths[i]);
4626 /* initialize hp and speaker paths */
4627 static void init_extra_out(struct hda_codec *codec)
4629 struct hda_gen_spec *spec = codec->spec;
4631 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
4632 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
4633 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4634 __init_extra_out(codec, spec->autocfg.speaker_outs,
4635 spec->speaker_paths);
4638 /* initialize multi-io paths */
4639 static void init_multi_io(struct hda_codec *codec)
4641 struct hda_gen_spec *spec = codec->spec;
4644 for (i = 0; i < spec->multi_ios; i++) {
4645 hda_nid_t pin = spec->multi_io[i].pin;
4646 struct nid_path *path;
4647 path = get_multiio_path(codec, i);
4650 if (!spec->multi_io[i].ctl_in)
4651 spec->multi_io[i].ctl_in =
4652 snd_hda_codec_get_pin_target(codec, pin);
4653 snd_hda_activate_path(codec, path, path->active, true);
4657 /* set up input pins and loopback paths */
4658 static void init_analog_input(struct hda_codec *codec)
4660 struct hda_gen_spec *spec = codec->spec;
4661 struct auto_pin_cfg *cfg = &spec->autocfg;
4664 for (i = 0; i < cfg->num_inputs; i++) {
4665 hda_nid_t nid = cfg->inputs[i].pin;
4666 if (is_input_pin(codec, nid))
4667 restore_pin_ctl(codec, nid);
4669 /* init loopback inputs */
4670 if (spec->mixer_nid) {
4671 struct nid_path *path;
4672 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
4674 snd_hda_activate_path(codec, path,
4675 path->active, false);
4680 /* initialize ADC paths */
4681 static void init_input_src(struct hda_codec *codec)
4683 struct hda_gen_spec *spec = codec->spec;
4684 struct hda_input_mux *imux = &spec->input_mux;
4685 struct nid_path *path;
4688 if (spec->dyn_adc_switch)
4691 nums = spec->num_adc_nids;
4693 for (c = 0; c < nums; c++) {
4694 for (i = 0; i < imux->num_items; i++) {
4695 path = get_input_path(codec, c, i);
4697 bool active = path->active;
4698 if (i == spec->cur_mux[c])
4700 snd_hda_activate_path(codec, path, active, false);
4705 if (spec->shared_mic_hp)
4706 update_shared_mic_hp(codec, spec->cur_mux[0]);
4708 if (spec->cap_sync_hook)
4709 spec->cap_sync_hook(codec, NULL);
4712 /* set right pin controls for digital I/O */
4713 static void init_digital(struct hda_codec *codec)
4715 struct hda_gen_spec *spec = codec->spec;
4719 for (i = 0; i < spec->autocfg.dig_outs; i++)
4720 set_output_and_unmute(codec, spec->digout_paths[i]);
4721 pin = spec->autocfg.dig_in_pin;
4723 struct nid_path *path;
4724 restore_pin_ctl(codec, pin);
4725 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
4727 snd_hda_activate_path(codec, path, path->active, false);
4731 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4732 * invalid unsol tags by some reason
4734 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4738 for (i = 0; i < codec->init_pins.used; i++) {
4739 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4740 hda_nid_t nid = pin->nid;
4741 if (is_jack_detectable(codec, nid) &&
4742 !snd_hda_jack_tbl_get(codec, nid))
4743 snd_hda_codec_update_cache(codec, nid, 0,
4744 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4749 * initialize the generic spec;
4750 * this can be put as patch_ops.init function
4752 int snd_hda_gen_init(struct hda_codec *codec)
4754 struct hda_gen_spec *spec = codec->spec;
4756 if (spec->init_hook)
4757 spec->init_hook(codec);
4759 snd_hda_apply_verbs(codec);
4761 codec->cached_write = 1;
4763 init_multi_out(codec);
4764 init_extra_out(codec);
4765 init_multi_io(codec);
4766 init_analog_input(codec);
4767 init_input_src(codec);
4768 init_digital(codec);
4770 clear_unsol_on_unused_pins(codec);
4772 /* call init functions of standard auto-mute helpers */
4773 update_automute_all(codec);
4775 snd_hda_codec_flush_amp_cache(codec);
4776 snd_hda_codec_flush_cmd_cache(codec);
4778 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4779 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4781 hda_call_check_power_status(codec, 0x01);
4784 EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4787 * free the generic spec;
4788 * this can be put as patch_ops.free function
4790 void snd_hda_gen_free(struct hda_codec *codec)
4792 snd_hda_gen_spec_free(codec->spec);
4796 EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4800 * check the loopback power save state;
4801 * this can be put as patch_ops.check_power_status function
4803 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4805 struct hda_gen_spec *spec = codec->spec;
4806 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4808 EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4813 * the generic codec support
4816 static const struct hda_codec_ops generic_patch_ops = {
4817 .build_controls = snd_hda_gen_build_controls,
4818 .build_pcms = snd_hda_gen_build_pcms,
4819 .init = snd_hda_gen_init,
4820 .free = snd_hda_gen_free,
4821 .unsol_event = snd_hda_jack_unsol_event,
4823 .check_power_status = snd_hda_gen_check_power_status,
4827 int snd_hda_parse_generic_codec(struct hda_codec *codec)
4829 struct hda_gen_spec *spec;
4832 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4835 snd_hda_gen_spec_init(spec);
4838 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4842 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
4846 codec->patch_ops = generic_patch_ops;
4850 snd_hda_gen_free(codec);
4853 EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);