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/delay.h>
28 #include <linux/ctype.h>
29 #include <linux/string.h>
30 #include <linux/bitops.h>
31 #include <sound/core.h>
32 #include <sound/jack.h>
33 #include "hda_codec.h"
34 #include "hda_local.h"
35 #include "hda_auto_parser.h"
38 #include "hda_generic.h"
41 /* initialize hda_gen_spec struct */
42 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
44 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
45 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
46 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
47 mutex_init(&spec->pcm_mutex);
50 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
52 struct snd_kcontrol_new *
53 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
54 const struct snd_kcontrol_new *temp)
56 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
61 knew->name = kstrdup(name, GFP_KERNEL);
63 knew->name = kstrdup(knew->name, GFP_KERNEL);
68 EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
70 static void free_kctls(struct hda_gen_spec *spec)
72 if (spec->kctls.list) {
73 struct snd_kcontrol_new *kctl = spec->kctls.list;
75 for (i = 0; i < spec->kctls.used; i++)
78 snd_array_free(&spec->kctls);
81 void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
86 snd_array_free(&spec->paths);
87 snd_array_free(&spec->loopback_list);
89 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
94 static void parse_user_hints(struct hda_codec *codec)
96 struct hda_gen_spec *spec = codec->spec;
99 val = snd_hda_get_bool_hint(codec, "jack_detect");
101 codec->no_jack_detect = !val;
102 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
104 codec->inv_jack_detect = !!val;
105 val = snd_hda_get_bool_hint(codec, "trigger_sense");
107 codec->no_trigger_sense = !val;
108 val = snd_hda_get_bool_hint(codec, "inv_eapd");
110 codec->inv_eapd = !!val;
111 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
113 codec->pcm_format_first = !!val;
114 val = snd_hda_get_bool_hint(codec, "sticky_stream");
116 codec->no_sticky_stream = !val;
117 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
119 codec->spdif_status_reset = !!val;
120 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
122 codec->pin_amp_workaround = !!val;
123 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
125 codec->single_adc_amp = !!val;
127 val = snd_hda_get_bool_hint(codec, "auto_mute");
129 spec->suppress_auto_mute = !val;
130 val = snd_hda_get_bool_hint(codec, "auto_mic");
132 spec->suppress_auto_mic = !val;
133 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
135 spec->line_in_auto_switch = !!val;
136 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp");
138 spec->auto_mute_via_amp = !!val;
139 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
141 spec->need_dac_fix = !!val;
142 val = snd_hda_get_bool_hint(codec, "primary_hp");
144 spec->no_primary_hp = !val;
145 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
147 spec->multi_cap_vol = !!val;
148 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
150 spec->inv_dmic_split = !!val;
151 val = snd_hda_get_bool_hint(codec, "indep_hp");
153 spec->indep_hp = !!val;
154 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
156 spec->add_stereo_mix_input = !!val;
157 /* the following two are just for compatibility */
158 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
160 spec->add_jack_modes = !!val;
161 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
163 spec->add_jack_modes = !!val;
164 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
166 spec->add_jack_modes = !!val;
167 val = snd_hda_get_bool_hint(codec, "power_down_unused");
169 spec->power_down_unused = !!val;
170 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
172 spec->hp_mic = !!val;
173 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
175 spec->suppress_hp_mic_detect = !val;
177 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
178 spec->mixer_nid = val;
182 * pin control value accesses
185 #define update_pin_ctl(codec, pin, val) \
186 snd_hda_codec_update_cache(codec, pin, 0, \
187 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
189 /* restore the pinctl based on the cached value */
190 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
192 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
195 /* set the pinctl target value and write it if requested */
196 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
197 unsigned int val, bool do_write)
201 val = snd_hda_correct_pin_ctl(codec, pin, val);
202 snd_hda_codec_set_pin_target(codec, pin, val);
204 update_pin_ctl(codec, pin, val);
207 /* set pinctl target values for all given pins */
208 static void set_pin_targets(struct hda_codec *codec, int num_pins,
209 hda_nid_t *pins, unsigned int val)
212 for (i = 0; i < num_pins; i++)
213 set_pin_target(codec, pins[i], val, false);
220 /* return the position of NID in the list, or -1 if not found */
221 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
224 for (i = 0; i < nums; i++)
230 /* return true if the given NID is contained in the path */
231 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
233 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
236 static struct nid_path *get_nid_path(struct hda_codec *codec,
237 hda_nid_t from_nid, hda_nid_t to_nid,
240 struct hda_gen_spec *spec = codec->spec;
243 for (i = 0; i < spec->paths.used; i++) {
244 struct nid_path *path = snd_array_elem(&spec->paths, i);
245 if (path->depth <= 0)
247 if ((!from_nid || path->path[0] == from_nid) &&
248 (!to_nid || path->path[path->depth - 1] == to_nid)) {
250 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
251 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
258 /* get the path between the given NIDs;
259 * passing 0 to either @pin or @dac behaves as a wildcard
261 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
262 hda_nid_t from_nid, hda_nid_t to_nid)
264 return get_nid_path(codec, from_nid, to_nid, 0);
266 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
268 /* get the index number corresponding to the path instance;
269 * the index starts from 1, for easier checking the invalid value
271 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
273 struct hda_gen_spec *spec = codec->spec;
274 struct nid_path *array = spec->paths.list;
277 if (!spec->paths.used)
280 if (idx < 0 || idx >= spec->paths.used)
284 EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
286 /* get the path instance corresponding to the given index number */
287 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
289 struct hda_gen_spec *spec = codec->spec;
291 if (idx <= 0 || idx > spec->paths.used)
293 return snd_array_elem(&spec->paths, idx - 1);
295 EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
297 /* check whether the given DAC is already found in any existing paths */
298 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
300 struct hda_gen_spec *spec = codec->spec;
303 for (i = 0; i < spec->paths.used; i++) {
304 struct nid_path *path = snd_array_elem(&spec->paths, i);
305 if (path->path[0] == nid)
311 /* check whether the given two widgets can be connected */
312 static bool is_reachable_path(struct hda_codec *codec,
313 hda_nid_t from_nid, hda_nid_t to_nid)
315 if (!from_nid || !to_nid)
317 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
320 /* nid, dir and idx */
321 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
323 /* check whether the given ctl is already assigned in any path elements */
324 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
326 struct hda_gen_spec *spec = codec->spec;
329 val &= AMP_VAL_COMPARE_MASK;
330 for (i = 0; i < spec->paths.used; i++) {
331 struct nid_path *path = snd_array_elem(&spec->paths, i);
332 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
338 /* check whether a control with the given (nid, dir, idx) was assigned */
339 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
340 int dir, int idx, int type)
342 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
343 return is_ctl_used(codec, val, type);
346 static void print_nid_path(const char *pfx, struct nid_path *path)
353 for (i = 0; i < path->depth; i++) {
355 sprintf(tmp, ":%02x", path->path[i]);
356 strlcat(buf, tmp, sizeof(buf));
358 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
361 /* called recursively */
362 static bool __parse_nid_path(struct hda_codec *codec,
363 hda_nid_t from_nid, hda_nid_t to_nid,
364 int anchor_nid, struct nid_path *path,
367 const hda_nid_t *conn;
370 if (to_nid == anchor_nid)
371 anchor_nid = 0; /* anchor passed */
372 else if (to_nid == (hda_nid_t)(-anchor_nid))
373 return false; /* hit the exclusive nid */
375 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
376 for (i = 0; i < nums; i++) {
377 if (conn[i] != from_nid) {
378 /* special case: when from_nid is 0,
379 * try to find an empty DAC
382 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
383 is_dac_already_used(codec, conn[i]))
386 /* anchor is not requested or already passed? */
390 if (depth >= MAX_NID_PATH_DEPTH)
392 for (i = 0; i < nums; i++) {
394 type = get_wcaps_type(get_wcaps(codec, conn[i]));
395 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
398 if (__parse_nid_path(codec, from_nid, conn[i],
399 anchor_nid, path, depth + 1))
405 path->path[path->depth] = conn[i];
406 path->idx[path->depth + 1] = i;
407 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
408 path->multi[path->depth + 1] = 1;
413 /* parse the widget path from the given nid to the target nid;
414 * when @from_nid is 0, try to find an empty DAC;
415 * when @anchor_nid is set to a positive value, only paths through the widget
416 * with the given value are evaluated.
417 * when @anchor_nid is set to a negative value, paths through the widget
418 * with the negative of given value are excluded, only other paths are chosen.
419 * when @anchor_nid is zero, no special handling about path selection.
421 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
422 hda_nid_t to_nid, int anchor_nid,
423 struct nid_path *path)
425 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
426 path->path[path->depth] = to_nid;
432 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
435 * parse the path between the given NIDs and add to the path list.
436 * if no valid path is found, return NULL
439 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
440 hda_nid_t to_nid, int anchor_nid)
442 struct hda_gen_spec *spec = codec->spec;
443 struct nid_path *path;
445 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
448 /* check whether the path has been already added */
449 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
453 path = snd_array_new(&spec->paths);
456 memset(path, 0, sizeof(*path));
457 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
463 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
465 /* clear the given path as invalid so that it won't be picked up later */
466 static void invalidate_nid_path(struct hda_codec *codec, int idx)
468 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
471 memset(path, 0, sizeof(*path));
474 /* look for an empty DAC slot */
475 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
478 struct hda_gen_spec *spec = codec->spec;
482 for (i = 0; i < spec->num_all_dacs; i++) {
483 hda_nid_t nid = spec->all_dacs[i];
484 if (!nid || is_dac_already_used(codec, nid))
486 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
487 if (is_digital != cap_digital)
489 if (is_reachable_path(codec, nid, pin))
495 /* replace the channels in the composed amp value with the given number */
496 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
498 val &= ~(0x3U << 16);
503 /* check whether the widget has the given amp capability for the direction */
504 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
505 int dir, unsigned int bits)
509 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
510 if (query_amp_caps(codec, nid, dir) & bits)
515 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
516 hda_nid_t nid2, int dir)
518 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
519 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
520 return (query_amp_caps(codec, nid1, dir) ==
521 query_amp_caps(codec, nid2, dir));
524 #define nid_has_mute(codec, nid, dir) \
525 check_amp_caps(codec, nid, dir, (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE))
526 #define nid_has_volume(codec, nid, dir) \
527 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
529 /* look for a widget suitable for assigning a mute switch in the path */
530 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
531 struct nid_path *path)
535 for (i = path->depth - 1; i >= 0; i--) {
536 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
537 return path->path[i];
538 if (i != path->depth - 1 && i != 0 &&
539 nid_has_mute(codec, path->path[i], HDA_INPUT))
540 return path->path[i];
545 /* look for a widget suitable for assigning a volume ctl in the path */
546 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
547 struct nid_path *path)
551 for (i = path->depth - 1; i >= 0; i--) {
552 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
553 return path->path[i];
559 * path activation / deactivation
562 /* can have the amp-in capability? */
563 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
565 hda_nid_t nid = path->path[idx];
566 unsigned int caps = get_wcaps(codec, nid);
567 unsigned int type = get_wcaps_type(caps);
569 if (!(caps & AC_WCAP_IN_AMP))
571 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
576 /* can have the amp-out capability? */
577 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
579 hda_nid_t nid = path->path[idx];
580 unsigned int caps = get_wcaps(codec, nid);
581 unsigned int type = get_wcaps_type(caps);
583 if (!(caps & AC_WCAP_OUT_AMP))
585 if (type == AC_WID_PIN && !idx) /* only for output pins */
590 /* check whether the given (nid,dir,idx) is active */
591 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
592 unsigned int dir, unsigned int idx)
594 struct hda_gen_spec *spec = codec->spec;
597 for (n = 0; n < spec->paths.used; n++) {
598 struct nid_path *path = snd_array_elem(&spec->paths, n);
601 for (i = 0; i < path->depth; i++) {
602 if (path->path[i] == nid) {
603 if (dir == HDA_OUTPUT || path->idx[i] == idx)
612 /* check whether the NID is referred by any active paths */
613 #define is_active_nid_for_any(codec, nid) \
614 is_active_nid(codec, nid, HDA_OUTPUT, 0)
616 /* get the default amp value for the target state */
617 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
618 int dir, unsigned int caps, bool enable)
620 unsigned int val = 0;
622 if (caps & AC_AMPCAP_NUM_STEPS) {
625 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
627 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
634 /* initialize the amp value (only at the first time) */
635 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
637 unsigned int caps = query_amp_caps(codec, nid, dir);
638 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
639 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
642 /* calculate amp value mask we can modify;
643 * if the given amp is controlled by mixers, don't touch it
645 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
646 hda_nid_t nid, int dir, int idx,
649 unsigned int mask = 0xff;
651 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
652 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
655 if (caps & AC_AMPCAP_NUM_STEPS) {
656 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
657 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
663 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
664 int idx, int idx_to_check, bool enable)
667 unsigned int mask, val;
669 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
672 caps = query_amp_caps(codec, nid, dir);
673 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
674 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
679 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
682 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
685 hda_nid_t nid = path->path[i];
686 init_amp(codec, nid, HDA_OUTPUT, 0);
687 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
690 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
691 int i, bool enable, bool add_aamix)
693 struct hda_gen_spec *spec = codec->spec;
694 const hda_nid_t *conn;
697 hda_nid_t nid = path->path[i];
699 nums = snd_hda_get_conn_list(codec, nid, &conn);
700 type = get_wcaps_type(get_wcaps(codec, nid));
701 if (type == AC_WID_PIN ||
702 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
708 for (n = 0; n < nums; n++)
709 init_amp(codec, nid, HDA_INPUT, n);
711 /* here is a little bit tricky in comparison with activate_amp_out();
712 * when aa-mixer is available, we need to enable the path as well
714 for (n = 0; n < nums; n++) {
715 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
717 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
721 /* activate or deactivate the given path
722 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
724 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
725 bool enable, bool add_aamix)
727 struct hda_gen_spec *spec = codec->spec;
731 path->active = false;
733 for (i = path->depth - 1; i >= 0; i--) {
734 hda_nid_t nid = path->path[i];
735 if (enable && spec->power_down_unused) {
736 /* make sure the widget is powered up */
737 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
738 snd_hda_codec_write(codec, nid, 0,
739 AC_VERB_SET_POWER_STATE,
742 if (enable && path->multi[i])
743 snd_hda_codec_write_cache(codec, nid, 0,
744 AC_VERB_SET_CONNECT_SEL,
746 if (has_amp_in(codec, path, i))
747 activate_amp_in(codec, path, i, enable, add_aamix);
748 if (has_amp_out(codec, path, i))
749 activate_amp_out(codec, path, i, enable);
755 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
757 /* if the given path is inactive, put widgets into D3 (only if suitable) */
758 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
760 struct hda_gen_spec *spec = codec->spec;
761 bool changed = false;
764 if (!spec->power_down_unused || path->active)
767 for (i = 0; i < path->depth; i++) {
768 hda_nid_t nid = path->path[i];
769 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
770 !is_active_nid_for_any(codec, nid)) {
771 snd_hda_codec_write(codec, nid, 0,
772 AC_VERB_SET_POWER_STATE,
780 snd_hda_codec_read(codec, path->path[0], 0,
781 AC_VERB_GET_POWER_STATE, 0);
785 /* turn on/off EAPD on the given pin */
786 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
788 struct hda_gen_spec *spec = codec->spec;
789 if (spec->own_eapd_ctl ||
790 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
794 if (spec->keep_eapd_on && !enable)
796 snd_hda_codec_update_cache(codec, pin, 0,
797 AC_VERB_SET_EAPD_BTLENABLE,
798 enable ? 0x02 : 0x00);
801 /* re-initialize the path specified by the given path index */
802 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
804 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
806 snd_hda_activate_path(codec, path, path->active, false);
811 * Helper functions for creating mixer ctl elements
814 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
815 struct snd_ctl_elem_value *ucontrol);
822 static const struct snd_kcontrol_new control_templates[] = {
823 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
824 /* only the put callback is replaced for handling the special mute */
826 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
827 .subdevice = HDA_SUBDEV_AMP_FLAG,
828 .info = snd_hda_mixer_amp_switch_info,
829 .get = snd_hda_mixer_amp_switch_get,
830 .put = hda_gen_mixer_mute_put, /* replaced */
831 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
833 HDA_BIND_MUTE(NULL, 0, 0, 0),
836 /* add dynamic controls from template */
837 static struct snd_kcontrol_new *
838 add_control(struct hda_gen_spec *spec, int type, const char *name,
839 int cidx, unsigned long val)
841 struct snd_kcontrol_new *knew;
843 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
847 if (get_amp_nid_(val))
848 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
849 knew->private_value = val;
853 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
854 const char *pfx, const char *dir,
855 const char *sfx, int cidx, unsigned long val)
857 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
858 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
859 if (!add_control(spec, type, name, cidx, val))
864 #define add_pb_vol_ctrl(spec, type, pfx, val) \
865 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
866 #define add_pb_sw_ctrl(spec, type, pfx, val) \
867 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
868 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
869 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
870 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
871 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
873 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
874 unsigned int chs, struct nid_path *path)
879 val = path->ctls[NID_PATH_VOL_CTL];
882 val = amp_val_replace_channels(val, chs);
883 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
886 /* return the channel bits suitable for the given path->ctls[] */
887 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
890 int chs = 1; /* mono (left only) */
892 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
893 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
894 chs = 3; /* stereo */
899 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
900 struct nid_path *path)
902 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
903 return add_vol_ctl(codec, pfx, cidx, chs, path);
906 /* create a mute-switch for the given mixer widget;
907 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
909 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
910 unsigned int chs, struct nid_path *path)
913 int type = HDA_CTL_WIDGET_MUTE;
917 val = path->ctls[NID_PATH_MUTE_CTL];
920 val = amp_val_replace_channels(val, chs);
921 if (get_amp_direction_(val) == HDA_INPUT) {
922 hda_nid_t nid = get_amp_nid_(val);
923 int nums = snd_hda_get_num_conns(codec, nid);
925 type = HDA_CTL_BIND_MUTE;
929 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
932 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
933 int cidx, struct nid_path *path)
935 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
936 return add_sw_ctl(codec, pfx, cidx, chs, path);
939 /* playback mute control with the software mute bit check */
940 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
941 struct snd_ctl_elem_value *ucontrol)
943 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
944 struct hda_gen_spec *spec = codec->spec;
946 if (spec->auto_mute_via_amp) {
947 hda_nid_t nid = get_amp_nid(kcontrol);
948 bool enabled = !((spec->mute_bits >> nid) & 1);
949 ucontrol->value.integer.value[0] &= enabled;
950 ucontrol->value.integer.value[1] &= enabled;
953 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
956 /* any ctl assigned to the path with the given index? */
957 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
959 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
960 return path && path->ctls[ctl_type];
963 static const char * const channel_name[4] = {
964 "Front", "Surround", "CLFE", "Side"
967 /* give some appropriate ctl name prefix for the given line out channel */
968 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
969 int *index, int ctl_type)
971 struct hda_gen_spec *spec = codec->spec;
972 struct auto_pin_cfg *cfg = &spec->autocfg;
975 if (cfg->line_outs == 1 && !spec->multi_ios &&
976 !cfg->hp_outs && !cfg->speaker_outs)
977 return spec->vmaster_mute.hook ? "PCM" : "Master";
979 /* if there is really a single DAC used in the whole output paths,
980 * use it master (or "PCM" if a vmaster hook is present)
982 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
983 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
984 return spec->vmaster_mute.hook ? "PCM" : "Master";
986 /* multi-io channels */
987 if (ch >= cfg->line_outs)
988 return channel_name[ch];
990 switch (cfg->line_out_type) {
991 case AUTO_PIN_SPEAKER_OUT:
992 /* if the primary channel vol/mute is shared with HP volume,
993 * don't name it as Speaker
995 if (!ch && cfg->hp_outs &&
996 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
998 if (cfg->line_outs == 1)
1000 if (cfg->line_outs == 2)
1001 return ch ? "Bass Speaker" : "Speaker";
1003 case AUTO_PIN_HP_OUT:
1004 /* if the primary channel vol/mute is shared with spk volume,
1005 * don't name it as Headphone
1007 if (!ch && cfg->speaker_outs &&
1008 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1010 /* for multi-io case, only the primary out */
1011 if (ch && spec->multi_ios)
1017 /* for a single channel output, we don't have to name the channel */
1018 if (cfg->line_outs == 1 && !spec->multi_ios)
1021 if (ch >= ARRAY_SIZE(channel_name)) {
1026 return channel_name[ch];
1030 * Parse output paths
1033 /* badness definition */
1035 /* No primary DAC is found for the main output */
1036 BAD_NO_PRIMARY_DAC = 0x10000,
1037 /* No DAC is found for the extra output */
1038 BAD_NO_DAC = 0x4000,
1039 /* No possible multi-ios */
1040 BAD_MULTI_IO = 0x120,
1041 /* No individual DAC for extra output */
1042 BAD_NO_EXTRA_DAC = 0x102,
1043 /* No individual DAC for extra surrounds */
1044 BAD_NO_EXTRA_SURR_DAC = 0x101,
1045 /* Primary DAC shared with main surrounds */
1046 BAD_SHARED_SURROUND = 0x100,
1047 /* No independent HP possible */
1048 BAD_NO_INDEP_HP = 0x10,
1049 /* Primary DAC shared with main CLFE */
1050 BAD_SHARED_CLFE = 0x10,
1051 /* Primary DAC shared with extra surrounds */
1052 BAD_SHARED_EXTRA_SURROUND = 0x10,
1053 /* Volume widget is shared */
1054 BAD_SHARED_VOL = 0x10,
1057 /* look for widgets in the given path which are appropriate for
1058 * volume and mute controls, and assign the values to ctls[].
1060 * When no appropriate widget is found in the path, the badness value
1061 * is incremented depending on the situation. The function returns the
1062 * total badness for both volume and mute controls.
1064 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1071 return BAD_SHARED_VOL * 2;
1073 if (path->ctls[NID_PATH_VOL_CTL] ||
1074 path->ctls[NID_PATH_MUTE_CTL])
1075 return 0; /* already evaluated */
1077 nid = look_for_out_vol_nid(codec, path);
1079 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1080 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1081 badness += BAD_SHARED_VOL;
1083 path->ctls[NID_PATH_VOL_CTL] = val;
1085 badness += BAD_SHARED_VOL;
1086 nid = look_for_out_mute_nid(codec, path);
1088 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1089 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1090 nid_has_mute(codec, nid, HDA_OUTPUT))
1091 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1093 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1094 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1095 badness += BAD_SHARED_VOL;
1097 path->ctls[NID_PATH_MUTE_CTL] = val;
1099 badness += BAD_SHARED_VOL;
1103 const struct badness_table hda_main_out_badness = {
1104 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1105 .no_dac = BAD_NO_DAC,
1106 .shared_primary = BAD_NO_PRIMARY_DAC,
1107 .shared_surr = BAD_SHARED_SURROUND,
1108 .shared_clfe = BAD_SHARED_CLFE,
1109 .shared_surr_main = BAD_SHARED_SURROUND,
1111 EXPORT_SYMBOL_HDA(hda_main_out_badness);
1113 const struct badness_table hda_extra_out_badness = {
1114 .no_primary_dac = BAD_NO_DAC,
1115 .no_dac = BAD_NO_DAC,
1116 .shared_primary = BAD_NO_EXTRA_DAC,
1117 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1118 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1119 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1121 EXPORT_SYMBOL_HDA(hda_extra_out_badness);
1123 /* get the DAC of the primary output corresponding to the given array index */
1124 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1126 struct hda_gen_spec *spec = codec->spec;
1127 struct auto_pin_cfg *cfg = &spec->autocfg;
1129 if (cfg->line_outs > idx)
1130 return spec->private_dac_nids[idx];
1131 idx -= cfg->line_outs;
1132 if (spec->multi_ios > idx)
1133 return spec->multi_io[idx].dac;
1137 /* return the DAC if it's reachable, otherwise zero */
1138 static inline hda_nid_t try_dac(struct hda_codec *codec,
1139 hda_nid_t dac, hda_nid_t pin)
1141 return is_reachable_path(codec, dac, pin) ? dac : 0;
1144 /* try to assign DACs to pins and return the resultant badness */
1145 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1146 const hda_nid_t *pins, hda_nid_t *dacs,
1148 const struct badness_table *bad)
1150 struct hda_gen_spec *spec = codec->spec;
1158 for (i = 0; i < num_outs; i++) {
1159 struct nid_path *path;
1160 hda_nid_t pin = pins[i];
1162 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1164 badness += assign_out_path_ctls(codec, path);
1168 dacs[i] = look_for_dac(codec, pin, false);
1169 if (!dacs[i] && !i) {
1170 /* try to steal the DAC of surrounds for the front */
1171 for (j = 1; j < num_outs; j++) {
1172 if (is_reachable_path(codec, dacs[j], pin)) {
1175 invalidate_nid_path(codec, path_idx[j]);
1184 dac = try_dac(codec, get_primary_out(codec, i), pin);
1186 dac = try_dac(codec, dacs[0], pin);
1188 dac = try_dac(codec, get_primary_out(codec, i), pin);
1191 badness += bad->shared_primary;
1193 badness += bad->shared_surr;
1195 badness += bad->shared_clfe;
1196 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1197 dac = spec->private_dac_nids[0];
1198 badness += bad->shared_surr_main;
1200 badness += bad->no_primary_dac;
1202 badness += bad->no_dac;
1206 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1207 if (!path && !i && spec->mixer_nid) {
1208 /* try with aamix */
1209 path = snd_hda_add_new_path(codec, dac, pin, 0);
1213 badness += bad->no_dac;
1215 /* print_nid_path("output", path); */
1216 path->active = true;
1217 path_idx[i] = snd_hda_get_path_idx(codec, path);
1218 badness += assign_out_path_ctls(codec, path);
1225 /* return NID if the given pin has only a single connection to a certain DAC */
1226 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1228 struct hda_gen_spec *spec = codec->spec;
1230 hda_nid_t nid_found = 0;
1232 for (i = 0; i < spec->num_all_dacs; i++) {
1233 hda_nid_t nid = spec->all_dacs[i];
1234 if (!nid || is_dac_already_used(codec, nid))
1236 if (is_reachable_path(codec, nid, pin)) {
1245 /* check whether the given pin can be a multi-io pin */
1246 static bool can_be_multiio_pin(struct hda_codec *codec,
1247 unsigned int location, hda_nid_t nid)
1249 unsigned int defcfg, caps;
1251 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1252 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1254 if (location && get_defcfg_location(defcfg) != location)
1256 caps = snd_hda_query_pin_caps(codec, nid);
1257 if (!(caps & AC_PINCAP_OUT))
1262 /* count the number of input pins that are capable to be multi-io */
1263 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1265 struct hda_gen_spec *spec = codec->spec;
1266 struct auto_pin_cfg *cfg = &spec->autocfg;
1267 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1268 unsigned int location = get_defcfg_location(defcfg);
1272 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1273 for (i = 0; i < cfg->num_inputs; i++) {
1274 if (cfg->inputs[i].type != type)
1276 if (can_be_multiio_pin(codec, location,
1277 cfg->inputs[i].pin))
1287 * When hardwired is set, try to fill ony hardwired pins, and returns
1288 * zero if any pins are filled, non-zero if nothing found.
1289 * When hardwired is off, try to fill possible input pins, and returns
1290 * the badness value.
1292 static int fill_multi_ios(struct hda_codec *codec,
1293 hda_nid_t reference_pin,
1296 struct hda_gen_spec *spec = codec->spec;
1297 struct auto_pin_cfg *cfg = &spec->autocfg;
1298 int type, i, j, num_pins, old_pins;
1299 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1300 unsigned int location = get_defcfg_location(defcfg);
1302 struct nid_path *path;
1304 old_pins = spec->multi_ios;
1308 num_pins = count_multiio_pins(codec, reference_pin);
1312 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1313 for (i = 0; i < cfg->num_inputs; i++) {
1314 hda_nid_t nid = cfg->inputs[i].pin;
1317 if (cfg->inputs[i].type != type)
1319 if (!can_be_multiio_pin(codec, location, nid))
1321 for (j = 0; j < spec->multi_ios; j++) {
1322 if (nid == spec->multi_io[j].pin)
1325 if (j < spec->multi_ios)
1329 dac = get_dac_if_single(codec, nid);
1331 dac = look_for_dac(codec, nid, false);
1336 path = snd_hda_add_new_path(codec, dac, nid,
1342 /* print_nid_path("multiio", path); */
1343 spec->multi_io[spec->multi_ios].pin = nid;
1344 spec->multi_io[spec->multi_ios].dac = dac;
1345 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1346 snd_hda_get_path_idx(codec, path);
1348 if (spec->multi_ios >= 2)
1354 badness = BAD_MULTI_IO;
1355 if (old_pins == spec->multi_ios) {
1357 return 1; /* nothing found */
1359 return badness; /* no badness if nothing found */
1361 if (!hardwired && spec->multi_ios < 2) {
1362 /* cancel newly assigned paths */
1363 spec->paths.used -= spec->multi_ios - old_pins;
1364 spec->multi_ios = old_pins;
1368 /* assign volume and mute controls */
1369 for (i = old_pins; i < spec->multi_ios; i++) {
1370 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1371 badness += assign_out_path_ctls(codec, path);
1377 /* map DACs for all pins in the list if they are single connections */
1378 static bool map_singles(struct hda_codec *codec, int outs,
1379 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1381 struct hda_gen_spec *spec = codec->spec;
1384 for (i = 0; i < outs; i++) {
1385 struct nid_path *path;
1389 dac = get_dac_if_single(codec, pins[i]);
1392 path = snd_hda_add_new_path(codec, dac, pins[i],
1394 if (!path && !i && spec->mixer_nid)
1395 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1399 /* print_nid_path("output", path); */
1400 path->active = true;
1401 path_idx[i] = snd_hda_get_path_idx(codec, path);
1407 /* create a new path including aamix if available, and return its index */
1408 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1410 struct hda_gen_spec *spec = codec->spec;
1411 struct nid_path *path;
1412 hda_nid_t path_dac, dac, pin;
1414 path = snd_hda_get_path_from_idx(codec, path_idx);
1415 if (!path || !path->depth ||
1416 is_nid_contained(path, spec->mixer_nid))
1418 path_dac = path->path[0];
1419 dac = spec->private_dac_nids[0];
1420 pin = path->path[path->depth - 1];
1421 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1423 if (dac != path_dac)
1425 else if (spec->multiout.hp_out_nid[0])
1426 dac = spec->multiout.hp_out_nid[0];
1427 else if (spec->multiout.extra_out_nid[0])
1428 dac = spec->multiout.extra_out_nid[0];
1432 path = snd_hda_add_new_path(codec, dac, pin,
1437 /* print_nid_path("output-aamix", path); */
1438 path->active = false; /* unused as default */
1439 return snd_hda_get_path_idx(codec, path);
1442 /* check whether the independent HP is available with the current config */
1443 static bool indep_hp_possible(struct hda_codec *codec)
1445 struct hda_gen_spec *spec = codec->spec;
1446 struct auto_pin_cfg *cfg = &spec->autocfg;
1447 struct nid_path *path;
1450 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1451 idx = spec->out_paths[0];
1453 idx = spec->hp_paths[0];
1454 path = snd_hda_get_path_from_idx(codec, idx);
1458 /* assume no path conflicts unless aamix is involved */
1459 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1462 /* check whether output paths contain aamix */
1463 for (i = 0; i < cfg->line_outs; i++) {
1464 if (spec->out_paths[i] == idx)
1466 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1467 if (path && is_nid_contained(path, spec->mixer_nid))
1470 for (i = 0; i < cfg->speaker_outs; i++) {
1471 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1472 if (path && is_nid_contained(path, spec->mixer_nid))
1479 /* fill the empty entries in the dac array for speaker/hp with the
1480 * shared dac pointed by the paths
1482 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1483 hda_nid_t *dacs, int *path_idx)
1485 struct nid_path *path;
1488 for (i = 0; i < num_outs; i++) {
1491 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1494 dacs[i] = path->path[0];
1498 /* fill in the dac_nids table from the parsed pin configuration */
1499 static int fill_and_eval_dacs(struct hda_codec *codec,
1500 bool fill_hardwired,
1501 bool fill_mio_first)
1503 struct hda_gen_spec *spec = codec->spec;
1504 struct auto_pin_cfg *cfg = &spec->autocfg;
1505 int i, err, badness;
1507 /* set num_dacs once to full for look_for_dac() */
1508 spec->multiout.num_dacs = cfg->line_outs;
1509 spec->multiout.dac_nids = spec->private_dac_nids;
1510 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1511 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1512 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1513 spec->multi_ios = 0;
1514 snd_array_free(&spec->paths);
1516 /* clear path indices */
1517 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1518 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1519 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1520 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1521 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1522 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1523 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1524 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1528 /* fill hard-wired DACs first */
1529 if (fill_hardwired) {
1532 mapped = map_singles(codec, cfg->line_outs,
1534 spec->private_dac_nids,
1536 mapped |= map_singles(codec, cfg->hp_outs,
1538 spec->multiout.hp_out_nid,
1540 mapped |= map_singles(codec, cfg->speaker_outs,
1542 spec->multiout.extra_out_nid,
1543 spec->speaker_paths);
1544 if (fill_mio_first && cfg->line_outs == 1 &&
1545 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1546 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1553 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1554 spec->private_dac_nids, spec->out_paths,
1555 spec->main_out_badness);
1557 if (fill_mio_first &&
1558 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1559 /* try to fill multi-io first */
1560 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1563 /* we don't count badness at this stage yet */
1566 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1567 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1568 spec->multiout.hp_out_nid,
1570 spec->extra_out_badness);
1575 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1576 err = try_assign_dacs(codec, cfg->speaker_outs,
1578 spec->multiout.extra_out_nid,
1579 spec->speaker_paths,
1580 spec->extra_out_badness);
1585 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1586 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1592 if (spec->mixer_nid) {
1593 spec->aamix_out_paths[0] =
1594 check_aamix_out_path(codec, spec->out_paths[0]);
1595 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1596 spec->aamix_out_paths[1] =
1597 check_aamix_out_path(codec, spec->hp_paths[0]);
1598 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1599 spec->aamix_out_paths[2] =
1600 check_aamix_out_path(codec, spec->speaker_paths[0]);
1603 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1604 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1605 spec->multi_ios = 1; /* give badness */
1607 /* re-count num_dacs and squash invalid entries */
1608 spec->multiout.num_dacs = 0;
1609 for (i = 0; i < cfg->line_outs; i++) {
1610 if (spec->private_dac_nids[i])
1611 spec->multiout.num_dacs++;
1613 memmove(spec->private_dac_nids + i,
1614 spec->private_dac_nids + i + 1,
1615 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1616 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1620 spec->ext_channel_count = spec->min_channel_count =
1621 spec->multiout.num_dacs * 2;
1623 if (spec->multi_ios == 2) {
1624 for (i = 0; i < 2; i++)
1625 spec->private_dac_nids[spec->multiout.num_dacs++] =
1626 spec->multi_io[i].dac;
1627 } else if (spec->multi_ios) {
1628 spec->multi_ios = 0;
1629 badness += BAD_MULTI_IO;
1632 if (spec->indep_hp && !indep_hp_possible(codec))
1633 badness += BAD_NO_INDEP_HP;
1635 /* re-fill the shared DAC for speaker / headphone */
1636 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1637 refill_shared_dacs(codec, cfg->hp_outs,
1638 spec->multiout.hp_out_nid,
1640 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1641 refill_shared_dacs(codec, cfg->speaker_outs,
1642 spec->multiout.extra_out_nid,
1643 spec->speaker_paths);
1648 #define DEBUG_BADNESS
1650 #ifdef DEBUG_BADNESS
1651 #define debug_badness snd_printdd
1653 #define debug_badness(...)
1656 #ifdef DEBUG_BADNESS
1657 static inline void print_nid_path_idx(struct hda_codec *codec,
1658 const char *pfx, int idx)
1660 struct nid_path *path;
1662 path = snd_hda_get_path_from_idx(codec, idx);
1664 print_nid_path(pfx, path);
1667 static void debug_show_configs(struct hda_codec *codec,
1668 struct auto_pin_cfg *cfg)
1670 struct hda_gen_spec *spec = codec->spec;
1671 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1674 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1675 cfg->line_out_pins[0], cfg->line_out_pins[1],
1676 cfg->line_out_pins[2], cfg->line_out_pins[3],
1677 spec->multiout.dac_nids[0],
1678 spec->multiout.dac_nids[1],
1679 spec->multiout.dac_nids[2],
1680 spec->multiout.dac_nids[3],
1681 lo_type[cfg->line_out_type]);
1682 for (i = 0; i < cfg->line_outs; i++)
1683 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1684 if (spec->multi_ios > 0)
1685 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1687 spec->multi_io[0].pin, spec->multi_io[1].pin,
1688 spec->multi_io[0].dac, spec->multi_io[1].dac);
1689 for (i = 0; i < spec->multi_ios; i++)
1690 print_nid_path_idx(codec, " mio",
1691 spec->out_paths[cfg->line_outs + i]);
1693 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1694 cfg->hp_pins[0], cfg->hp_pins[1],
1695 cfg->hp_pins[2], cfg->hp_pins[3],
1696 spec->multiout.hp_out_nid[0],
1697 spec->multiout.hp_out_nid[1],
1698 spec->multiout.hp_out_nid[2],
1699 spec->multiout.hp_out_nid[3]);
1700 for (i = 0; i < cfg->hp_outs; i++)
1701 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1702 if (cfg->speaker_outs)
1703 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1704 cfg->speaker_pins[0], cfg->speaker_pins[1],
1705 cfg->speaker_pins[2], cfg->speaker_pins[3],
1706 spec->multiout.extra_out_nid[0],
1707 spec->multiout.extra_out_nid[1],
1708 spec->multiout.extra_out_nid[2],
1709 spec->multiout.extra_out_nid[3]);
1710 for (i = 0; i < cfg->speaker_outs; i++)
1711 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1712 for (i = 0; i < 3; i++)
1713 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1716 #define debug_show_configs(codec, cfg) /* NOP */
1719 /* find all available DACs of the codec */
1720 static void fill_all_dac_nids(struct hda_codec *codec)
1722 struct hda_gen_spec *spec = codec->spec;
1724 hda_nid_t nid = codec->start_nid;
1726 spec->num_all_dacs = 0;
1727 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1728 for (i = 0; i < codec->num_nodes; i++, nid++) {
1729 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1731 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1732 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1735 spec->all_dacs[spec->num_all_dacs++] = nid;
1739 static int parse_output_paths(struct hda_codec *codec)
1741 struct hda_gen_spec *spec = codec->spec;
1742 struct auto_pin_cfg *cfg = &spec->autocfg;
1743 struct auto_pin_cfg *best_cfg;
1745 int best_badness = INT_MAX;
1747 bool fill_hardwired = true, fill_mio_first = true;
1748 bool best_wired = true, best_mio = true;
1749 bool hp_spk_swapped = false;
1751 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1757 badness = fill_and_eval_dacs(codec, fill_hardwired,
1763 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1764 cfg->line_out_type, fill_hardwired, fill_mio_first,
1766 debug_show_configs(codec, cfg);
1767 if (badness < best_badness) {
1768 best_badness = badness;
1770 best_wired = fill_hardwired;
1771 best_mio = fill_mio_first;
1775 fill_mio_first = !fill_mio_first;
1776 if (!fill_mio_first)
1778 fill_hardwired = !fill_hardwired;
1779 if (!fill_hardwired)
1783 hp_spk_swapped = true;
1784 if (cfg->speaker_outs > 0 &&
1785 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1786 cfg->hp_outs = cfg->line_outs;
1787 memcpy(cfg->hp_pins, cfg->line_out_pins,
1788 sizeof(cfg->hp_pins));
1789 cfg->line_outs = cfg->speaker_outs;
1790 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1791 sizeof(cfg->speaker_pins));
1792 cfg->speaker_outs = 0;
1793 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1794 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1795 fill_hardwired = true;
1798 if (cfg->hp_outs > 0 &&
1799 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1800 cfg->speaker_outs = cfg->line_outs;
1801 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1802 sizeof(cfg->speaker_pins));
1803 cfg->line_outs = cfg->hp_outs;
1804 memcpy(cfg->line_out_pins, cfg->hp_pins,
1805 sizeof(cfg->hp_pins));
1807 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1808 cfg->line_out_type = AUTO_PIN_HP_OUT;
1809 fill_hardwired = true;
1816 debug_badness("==> restoring best_cfg\n");
1818 fill_and_eval_dacs(codec, best_wired, best_mio);
1820 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1821 cfg->line_out_type, best_wired, best_mio);
1822 debug_show_configs(codec, cfg);
1824 if (cfg->line_out_pins[0]) {
1825 struct nid_path *path;
1826 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1828 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1829 if (spec->vmaster_nid)
1830 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1831 HDA_OUTPUT, spec->vmaster_tlv);
1834 /* set initial pinctl targets */
1835 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1839 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1840 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1841 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1842 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1843 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1844 set_pin_targets(codec, cfg->speaker_outs,
1845 cfg->speaker_pins, val);
1848 /* clear indep_hp flag if not available */
1849 if (spec->indep_hp && !indep_hp_possible(codec))
1856 /* add playback controls from the parsed DAC table */
1857 static int create_multi_out_ctls(struct hda_codec *codec,
1858 const struct auto_pin_cfg *cfg)
1860 struct hda_gen_spec *spec = codec->spec;
1861 int i, err, noutputs;
1863 noutputs = cfg->line_outs;
1864 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1865 noutputs += spec->multi_ios;
1867 for (i = 0; i < noutputs; i++) {
1870 struct nid_path *path;
1872 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1876 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
1877 if (!name || !strcmp(name, "CLFE")) {
1879 err = add_vol_ctl(codec, "Center", 0, 1, path);
1882 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1886 err = add_stereo_vol(codec, name, index, path);
1891 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1892 if (!name || !strcmp(name, "CLFE")) {
1893 err = add_sw_ctl(codec, "Center", 0, 1, path);
1896 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1900 err = add_stereo_sw(codec, name, index, path);
1908 static int create_extra_out(struct hda_codec *codec, int path_idx,
1909 const char *pfx, int cidx)
1911 struct nid_path *path;
1914 path = snd_hda_get_path_from_idx(codec, path_idx);
1917 err = add_stereo_vol(codec, pfx, cidx, path);
1920 err = add_stereo_sw(codec, pfx, cidx, path);
1926 /* add playback controls for speaker and HP outputs */
1927 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1928 const int *paths, const char *pfx)
1932 for (i = 0; i < num_pins; i++) {
1934 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1937 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1938 name = "Bass Speaker";
1939 else if (num_pins >= 3) {
1940 snprintf(tmp, sizeof(tmp), "%s %s",
1941 pfx, channel_name[i]);
1947 err = create_extra_out(codec, paths[i], name, idx);
1954 static int create_hp_out_ctls(struct hda_codec *codec)
1956 struct hda_gen_spec *spec = codec->spec;
1957 return create_extra_outs(codec, spec->autocfg.hp_outs,
1962 static int create_speaker_out_ctls(struct hda_codec *codec)
1964 struct hda_gen_spec *spec = codec->spec;
1965 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1966 spec->speaker_paths,
1971 * independent HP controls
1974 static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack);
1975 static int indep_hp_info(struct snd_kcontrol *kcontrol,
1976 struct snd_ctl_elem_info *uinfo)
1978 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1981 static int indep_hp_get(struct snd_kcontrol *kcontrol,
1982 struct snd_ctl_elem_value *ucontrol)
1984 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1985 struct hda_gen_spec *spec = codec->spec;
1986 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1990 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1991 int nomix_path_idx, int mix_path_idx,
1994 static int indep_hp_put(struct snd_kcontrol *kcontrol,
1995 struct snd_ctl_elem_value *ucontrol)
1997 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1998 struct hda_gen_spec *spec = codec->spec;
1999 unsigned int select = ucontrol->value.enumerated.item[0];
2002 mutex_lock(&spec->pcm_mutex);
2003 if (spec->active_streams) {
2008 if (spec->indep_hp_enabled != select) {
2010 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2011 dacp = &spec->private_dac_nids[0];
2013 dacp = &spec->multiout.hp_out_nid[0];
2015 /* update HP aamix paths in case it conflicts with indep HP */
2016 if (spec->have_aamix_ctl) {
2017 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2018 update_aamix_paths(codec, spec->aamix_mode,
2020 spec->aamix_out_paths[0],
2021 spec->autocfg.line_out_type);
2023 update_aamix_paths(codec, spec->aamix_mode,
2025 spec->aamix_out_paths[1],
2029 spec->indep_hp_enabled = select;
2030 if (spec->indep_hp_enabled)
2033 *dacp = spec->alt_dac_nid;
2035 call_hp_automute(codec, NULL);
2039 mutex_unlock(&spec->pcm_mutex);
2043 static const struct snd_kcontrol_new indep_hp_ctl = {
2044 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2045 .name = "Independent HP",
2046 .info = indep_hp_info,
2047 .get = indep_hp_get,
2048 .put = indep_hp_put,
2052 static int create_indep_hp_ctls(struct hda_codec *codec)
2054 struct hda_gen_spec *spec = codec->spec;
2057 if (!spec->indep_hp)
2059 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2060 dac = spec->multiout.dac_nids[0];
2062 dac = spec->multiout.hp_out_nid[0];
2068 spec->indep_hp_enabled = false;
2069 spec->alt_dac_nid = dac;
2070 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2076 * channel mode enum control
2079 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2080 struct snd_ctl_elem_info *uinfo)
2082 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2083 struct hda_gen_spec *spec = codec->spec;
2086 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2088 uinfo->value.enumerated.items = spec->multi_ios + 1;
2089 if (uinfo->value.enumerated.item > spec->multi_ios)
2090 uinfo->value.enumerated.item = spec->multi_ios;
2091 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2092 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2096 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2097 struct snd_ctl_elem_value *ucontrol)
2099 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2100 struct hda_gen_spec *spec = codec->spec;
2101 ucontrol->value.enumerated.item[0] =
2102 (spec->ext_channel_count - spec->min_channel_count) / 2;
2106 static inline struct nid_path *
2107 get_multiio_path(struct hda_codec *codec, int idx)
2109 struct hda_gen_spec *spec = codec->spec;
2110 return snd_hda_get_path_from_idx(codec,
2111 spec->out_paths[spec->autocfg.line_outs + idx]);
2114 static void update_automute_all(struct hda_codec *codec);
2116 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2117 * used for output paths
2119 static bool aamix_default(struct hda_gen_spec *spec)
2121 return !spec->have_aamix_ctl || spec->aamix_mode;
2124 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2126 struct hda_gen_spec *spec = codec->spec;
2127 hda_nid_t nid = spec->multi_io[idx].pin;
2128 struct nid_path *path;
2130 path = get_multiio_path(codec, idx);
2134 if (path->active == output)
2138 set_pin_target(codec, nid, PIN_OUT, true);
2139 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2140 set_pin_eapd(codec, nid, true);
2142 set_pin_eapd(codec, nid, false);
2143 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2144 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2145 path_power_down_sync(codec, path);
2148 /* update jack retasking in case it modifies any of them */
2149 update_automute_all(codec);
2154 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2155 struct snd_ctl_elem_value *ucontrol)
2157 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2158 struct hda_gen_spec *spec = codec->spec;
2161 ch = ucontrol->value.enumerated.item[0];
2162 if (ch < 0 || ch > spec->multi_ios)
2164 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2166 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2167 for (i = 0; i < spec->multi_ios; i++)
2168 set_multi_io(codec, i, i < ch);
2169 spec->multiout.max_channels = max(spec->ext_channel_count,
2170 spec->const_channel_count);
2171 if (spec->need_dac_fix)
2172 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2176 static const struct snd_kcontrol_new channel_mode_enum = {
2177 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2178 .name = "Channel Mode",
2179 .info = ch_mode_info,
2184 static int create_multi_channel_mode(struct hda_codec *codec)
2186 struct hda_gen_spec *spec = codec->spec;
2188 if (spec->multi_ios > 0) {
2189 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2196 * aamix loopback enable/disable switch
2199 #define loopback_mixing_info indep_hp_info
2201 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2202 struct snd_ctl_elem_value *ucontrol)
2204 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2205 struct hda_gen_spec *spec = codec->spec;
2206 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2210 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2211 int nomix_path_idx, int mix_path_idx,
2214 struct hda_gen_spec *spec = codec->spec;
2215 struct nid_path *nomix_path, *mix_path;
2217 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2218 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2219 if (!nomix_path || !mix_path)
2222 /* if HP aamix path is driven from a different DAC and the
2223 * independent HP mode is ON, can't turn on aamix path
2225 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2226 mix_path->path[0] != spec->alt_dac_nid)
2230 snd_hda_activate_path(codec, nomix_path, false, true);
2231 snd_hda_activate_path(codec, mix_path, true, true);
2232 path_power_down_sync(codec, nomix_path);
2234 snd_hda_activate_path(codec, mix_path, false, false);
2235 snd_hda_activate_path(codec, nomix_path, true, false);
2236 path_power_down_sync(codec, mix_path);
2240 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2241 struct snd_ctl_elem_value *ucontrol)
2243 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2244 struct hda_gen_spec *spec = codec->spec;
2245 unsigned int val = ucontrol->value.enumerated.item[0];
2247 if (val == spec->aamix_mode)
2249 spec->aamix_mode = val;
2250 update_aamix_paths(codec, val, spec->out_paths[0],
2251 spec->aamix_out_paths[0],
2252 spec->autocfg.line_out_type);
2253 update_aamix_paths(codec, val, spec->hp_paths[0],
2254 spec->aamix_out_paths[1],
2256 update_aamix_paths(codec, val, spec->speaker_paths[0],
2257 spec->aamix_out_paths[2],
2258 AUTO_PIN_SPEAKER_OUT);
2262 static const struct snd_kcontrol_new loopback_mixing_enum = {
2263 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2264 .name = "Loopback Mixing",
2265 .info = loopback_mixing_info,
2266 .get = loopback_mixing_get,
2267 .put = loopback_mixing_put,
2270 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2272 struct hda_gen_spec *spec = codec->spec;
2274 if (!spec->mixer_nid)
2276 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2277 spec->aamix_out_paths[2]))
2279 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2281 spec->have_aamix_ctl = 1;
2286 * shared headphone/mic handling
2289 static void call_update_outputs(struct hda_codec *codec);
2291 /* for shared I/O, change the pin-control accordingly */
2292 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2294 struct hda_gen_spec *spec = codec->spec;
2299 pin = spec->hp_mic_pin;
2300 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2303 val = snd_hda_codec_get_pin_target(codec, pin);
2313 val = snd_hda_get_default_vref(codec, pin);
2314 /* if the HP pin doesn't support VREF and the codec driver gives an
2315 * alternative pin, set up the VREF on that pin instead
2317 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2318 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2319 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2320 if (vref_val != AC_PINCTL_VREF_HIZ)
2321 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2322 PIN_IN | (as_mic ? vref_val : 0));
2325 if (!spec->hp_mic_jack_modes) {
2330 set_pin_target(codec, pin, val, true);
2331 call_hp_automute(codec, NULL);
2335 /* create a shared input with the headphone out */
2336 static int create_hp_mic(struct hda_codec *codec)
2338 struct hda_gen_spec *spec = codec->spec;
2339 struct auto_pin_cfg *cfg = &spec->autocfg;
2340 unsigned int defcfg;
2343 if (!spec->hp_mic) {
2344 if (spec->suppress_hp_mic_detect)
2346 /* automatic detection: only if no input or a single internal
2347 * input pin is found, try to detect the shared hp/mic
2349 if (cfg->num_inputs > 1)
2351 else if (cfg->num_inputs == 1) {
2352 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2353 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2358 spec->hp_mic = 0; /* clear once */
2359 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2363 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2364 nid = cfg->line_out_pins[0];
2365 else if (cfg->hp_outs > 0)
2366 nid = cfg->hp_pins[0];
2370 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2371 return 0; /* no input */
2373 cfg->inputs[cfg->num_inputs].pin = nid;
2374 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2375 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2378 spec->hp_mic_pin = nid;
2379 /* we can't handle auto-mic together with HP-mic */
2380 spec->suppress_auto_mic = 1;
2381 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2389 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2391 static const char * const out_jack_texts[] = {
2392 "Line Out", "Headphone Out",
2395 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2396 struct snd_ctl_elem_info *uinfo)
2398 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2401 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2402 struct snd_ctl_elem_value *ucontrol)
2404 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2405 hda_nid_t nid = kcontrol->private_value;
2406 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2407 ucontrol->value.enumerated.item[0] = 1;
2409 ucontrol->value.enumerated.item[0] = 0;
2413 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2414 struct snd_ctl_elem_value *ucontrol)
2416 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2417 hda_nid_t nid = kcontrol->private_value;
2420 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2421 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2423 snd_hda_set_pin_ctl_cache(codec, nid, val);
2427 static const struct snd_kcontrol_new out_jack_mode_enum = {
2428 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2429 .info = out_jack_mode_info,
2430 .get = out_jack_mode_get,
2431 .put = out_jack_mode_put,
2434 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2436 struct hda_gen_spec *spec = codec->spec;
2439 for (i = 0; i < spec->kctls.used; i++) {
2440 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2441 if (!strcmp(kctl->name, name) && kctl->index == idx)
2447 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2448 char *name, size_t name_len)
2450 struct hda_gen_spec *spec = codec->spec;
2453 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2454 strlcat(name, " Jack Mode", name_len);
2456 for (; find_kctl_name(codec, name, idx); idx++)
2460 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2462 struct hda_gen_spec *spec = codec->spec;
2463 if (spec->add_jack_modes) {
2464 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2465 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2471 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2474 struct hda_gen_spec *spec = codec->spec;
2477 for (i = 0; i < num_pins; i++) {
2478 hda_nid_t pin = pins[i];
2479 if (pin == spec->hp_mic_pin) {
2480 int ret = create_hp_mic_jack_mode(codec, pin);
2485 if (get_out_jack_num_items(codec, pin) > 1) {
2486 struct snd_kcontrol_new *knew;
2487 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2488 get_jack_mode_name(codec, pin, name, sizeof(name));
2489 knew = snd_hda_gen_add_kctl(spec, name,
2490 &out_jack_mode_enum);
2493 knew->private_value = pin;
2504 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2507 static const char * const vref_texts[NUM_VREFS] = {
2508 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2509 "", "Mic 80pc Bias", "Mic 100pc Bias"
2512 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2514 unsigned int pincap;
2516 pincap = snd_hda_query_pin_caps(codec, pin);
2517 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2518 /* filter out unusual vrefs */
2519 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2523 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2524 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2526 unsigned int i, n = 0;
2528 for (i = 0; i < NUM_VREFS; i++) {
2529 if (vref_caps & (1 << i)) {
2538 /* convert back from the vref ctl index to the enum item index */
2539 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2541 unsigned int i, n = 0;
2543 for (i = 0; i < NUM_VREFS; i++) {
2546 if (vref_caps & (1 << i))
2552 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2553 struct snd_ctl_elem_info *uinfo)
2555 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2556 hda_nid_t nid = kcontrol->private_value;
2557 unsigned int vref_caps = get_vref_caps(codec, nid);
2559 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2561 /* set the right text */
2562 strcpy(uinfo->value.enumerated.name,
2563 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2567 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2568 struct snd_ctl_elem_value *ucontrol)
2570 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2571 hda_nid_t nid = kcontrol->private_value;
2572 unsigned int vref_caps = get_vref_caps(codec, nid);
2575 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2576 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2580 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2581 struct snd_ctl_elem_value *ucontrol)
2583 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2584 hda_nid_t nid = kcontrol->private_value;
2585 unsigned int vref_caps = get_vref_caps(codec, nid);
2586 unsigned int val, idx;
2588 val = snd_hda_codec_get_pin_target(codec, nid);
2589 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2590 if (idx == ucontrol->value.enumerated.item[0])
2593 val &= ~AC_PINCTL_VREFEN;
2594 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2595 snd_hda_set_pin_ctl_cache(codec, nid, val);
2599 static const struct snd_kcontrol_new in_jack_mode_enum = {
2600 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2601 .info = in_jack_mode_info,
2602 .get = in_jack_mode_get,
2603 .put = in_jack_mode_put,
2606 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2608 struct hda_gen_spec *spec = codec->spec;
2610 if (spec->add_jack_modes)
2611 nitems = hweight32(get_vref_caps(codec, pin));
2612 return nitems ? nitems : 1;
2615 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2617 struct hda_gen_spec *spec = codec->spec;
2618 struct snd_kcontrol_new *knew;
2619 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2620 unsigned int defcfg;
2622 if (pin == spec->hp_mic_pin)
2623 return 0; /* already done in create_out_jack_mode() */
2625 /* no jack mode for fixed pins */
2626 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2627 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2630 /* no multiple vref caps? */
2631 if (get_in_jack_num_items(codec, pin) <= 1)
2634 get_jack_mode_name(codec, pin, name, sizeof(name));
2635 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2638 knew->private_value = pin;
2643 * HP/mic shared jack mode
2645 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2646 struct snd_ctl_elem_info *uinfo)
2648 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2649 hda_nid_t nid = kcontrol->private_value;
2650 int out_jacks = get_out_jack_num_items(codec, nid);
2651 int in_jacks = get_in_jack_num_items(codec, nid);
2652 const char *text = NULL;
2655 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2657 uinfo->value.enumerated.items = out_jacks + in_jacks;
2658 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2659 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2660 idx = uinfo->value.enumerated.item;
2661 if (idx < out_jacks) {
2663 text = out_jack_texts[idx];
2665 text = "Headphone Out";
2669 unsigned int vref_caps = get_vref_caps(codec, nid);
2670 text = vref_texts[get_vref_idx(vref_caps, idx)];
2675 strcpy(uinfo->value.enumerated.name, text);
2679 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2681 int out_jacks = get_out_jack_num_items(codec, nid);
2682 int in_jacks = get_in_jack_num_items(codec, nid);
2683 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2686 if (val & PIN_OUT) {
2687 if (out_jacks > 1 && val == PIN_HP)
2689 } else if (val & PIN_IN) {
2692 unsigned int vref_caps = get_vref_caps(codec, nid);
2693 val &= AC_PINCTL_VREFEN;
2694 idx += cvt_from_vref_idx(vref_caps, val);
2700 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2701 struct snd_ctl_elem_value *ucontrol)
2703 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2704 hda_nid_t nid = kcontrol->private_value;
2705 ucontrol->value.enumerated.item[0] =
2706 get_cur_hp_mic_jack_mode(codec, nid);
2710 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2711 struct snd_ctl_elem_value *ucontrol)
2713 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2714 hda_nid_t nid = kcontrol->private_value;
2715 int out_jacks = get_out_jack_num_items(codec, nid);
2716 int in_jacks = get_in_jack_num_items(codec, nid);
2717 unsigned int val, oldval, idx;
2719 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2720 idx = ucontrol->value.enumerated.item[0];
2724 if (idx < out_jacks) {
2726 val = idx ? PIN_HP : PIN_OUT;
2732 unsigned int vref_caps = get_vref_caps(codec, nid);
2733 val = snd_hda_codec_get_pin_target(codec, nid);
2734 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2735 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
2737 val = snd_hda_get_default_vref(codec, nid);
2739 snd_hda_set_pin_ctl_cache(codec, nid, val);
2740 call_hp_automute(codec, NULL);
2745 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2746 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2747 .info = hp_mic_jack_mode_info,
2748 .get = hp_mic_jack_mode_get,
2749 .put = hp_mic_jack_mode_put,
2752 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2754 struct hda_gen_spec *spec = codec->spec;
2755 struct snd_kcontrol_new *knew;
2757 if (get_out_jack_num_items(codec, pin) <= 1 &&
2758 get_in_jack_num_items(codec, pin) <= 1)
2759 return 0; /* no need */
2760 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2761 &hp_mic_jack_mode_enum);
2764 knew->private_value = pin;
2765 spec->hp_mic_jack_modes = 1;
2773 /* add the powersave loopback-list entry */
2774 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2776 struct hda_amp_list *list;
2778 list = snd_array_new(&spec->loopback_list);
2782 list->dir = HDA_INPUT;
2784 spec->loopback.amplist = spec->loopback_list.list;
2788 /* create input playback/capture controls for the given pin */
2789 static int new_analog_input(struct hda_codec *codec, int input_idx,
2790 hda_nid_t pin, const char *ctlname, int ctlidx,
2793 struct hda_gen_spec *spec = codec->spec;
2794 struct nid_path *path;
2798 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2799 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2800 return 0; /* no need for analog loopback */
2802 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
2805 print_nid_path("loopback", path);
2806 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
2808 idx = path->idx[path->depth - 1];
2809 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2810 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2811 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
2814 path->ctls[NID_PATH_VOL_CTL] = val;
2817 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2818 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2819 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
2822 path->ctls[NID_PATH_MUTE_CTL] = val;
2825 path->active = true;
2826 err = add_loopback_list(spec, mix_nid, idx);
2830 if (spec->mixer_nid != spec->mixer_merge_nid &&
2831 !spec->loopback_merge_path) {
2832 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2833 spec->mixer_merge_nid, 0);
2835 print_nid_path("loopback-merge", path);
2836 path->active = true;
2837 spec->loopback_merge_path =
2838 snd_hda_get_path_idx(codec, path);
2845 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2847 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2848 return (pincap & AC_PINCAP_IN) != 0;
2851 /* Parse the codec tree and retrieve ADCs */
2852 static int fill_adc_nids(struct hda_codec *codec)
2854 struct hda_gen_spec *spec = codec->spec;
2856 hda_nid_t *adc_nids = spec->adc_nids;
2857 int max_nums = ARRAY_SIZE(spec->adc_nids);
2860 nid = codec->start_nid;
2861 for (i = 0; i < codec->num_nodes; i++, nid++) {
2862 unsigned int caps = get_wcaps(codec, nid);
2863 int type = get_wcaps_type(caps);
2865 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2867 adc_nids[nums] = nid;
2868 if (++nums >= max_nums)
2871 spec->num_adc_nids = nums;
2873 /* copy the detected ADCs to all_adcs[] */
2874 spec->num_all_adcs = nums;
2875 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2880 /* filter out invalid adc_nids that don't give all active input pins;
2881 * if needed, check whether dynamic ADC-switching is available
2883 static int check_dyn_adc_switch(struct hda_codec *codec)
2885 struct hda_gen_spec *spec = codec->spec;
2886 struct hda_input_mux *imux = &spec->input_mux;
2887 unsigned int ok_bits;
2892 for (n = 0; n < spec->num_adc_nids; n++) {
2893 for (i = 0; i < imux->num_items; i++) {
2894 if (!spec->input_paths[i][n])
2897 if (i >= imux->num_items) {
2898 ok_bits |= (1 << n);
2904 /* check whether ADC-switch is possible */
2905 for (i = 0; i < imux->num_items; i++) {
2906 for (n = 0; n < spec->num_adc_nids; n++) {
2907 if (spec->input_paths[i][n]) {
2908 spec->dyn_adc_idx[i] = n;
2914 snd_printdd("hda-codec: enabling ADC switching\n");
2915 spec->dyn_adc_switch = 1;
2916 } else if (nums != spec->num_adc_nids) {
2917 /* shrink the invalid adcs and input paths */
2919 for (n = 0; n < spec->num_adc_nids; n++) {
2920 if (!(ok_bits & (1 << n)))
2923 spec->adc_nids[nums] = spec->adc_nids[n];
2924 for (i = 0; i < imux->num_items; i++) {
2925 invalidate_nid_path(codec,
2926 spec->input_paths[i][nums]);
2927 spec->input_paths[i][nums] =
2928 spec->input_paths[i][n];
2933 spec->num_adc_nids = nums;
2936 if (imux->num_items == 1 ||
2937 (imux->num_items == 2 && spec->hp_mic)) {
2938 snd_printdd("hda-codec: reducing to a single ADC\n");
2939 spec->num_adc_nids = 1; /* reduce to a single ADC */
2942 /* single index for individual volumes ctls */
2943 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2944 spec->num_adc_nids = 1;
2949 /* parse capture source paths from the given pin and create imux items */
2950 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2951 int cfg_idx, int num_adcs,
2952 const char *label, int anchor)
2954 struct hda_gen_spec *spec = codec->spec;
2955 struct hda_input_mux *imux = &spec->input_mux;
2956 int imux_idx = imux->num_items;
2957 bool imux_added = false;
2960 for (c = 0; c < num_adcs; c++) {
2961 struct nid_path *path;
2962 hda_nid_t adc = spec->adc_nids[c];
2964 if (!is_reachable_path(codec, pin, adc))
2966 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2969 print_nid_path("input", path);
2970 spec->input_paths[imux_idx][c] =
2971 snd_hda_get_path_idx(codec, path);
2974 if (spec->hp_mic_pin == pin)
2975 spec->hp_mic_mux_idx = imux->num_items;
2976 spec->imux_pins[imux->num_items] = pin;
2977 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
2986 * create playback/capture controls for input pins
2989 /* fill the label for each input at first */
2990 static int fill_input_pin_labels(struct hda_codec *codec)
2992 struct hda_gen_spec *spec = codec->spec;
2993 const struct auto_pin_cfg *cfg = &spec->autocfg;
2996 for (i = 0; i < cfg->num_inputs; i++) {
2997 hda_nid_t pin = cfg->inputs[i].pin;
3001 if (!is_input_pin(codec, pin))
3004 label = hda_get_autocfg_input_label(codec, cfg, i);
3006 for (j = i - 1; j >= 0; j--) {
3007 if (spec->input_labels[j] &&
3008 !strcmp(spec->input_labels[j], label)) {
3009 idx = spec->input_label_idxs[j] + 1;
3014 spec->input_labels[i] = label;
3015 spec->input_label_idxs[i] = idx;
3021 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3023 static int create_input_ctls(struct hda_codec *codec)
3025 struct hda_gen_spec *spec = codec->spec;
3026 const struct auto_pin_cfg *cfg = &spec->autocfg;
3027 hda_nid_t mixer = spec->mixer_nid;
3032 num_adcs = fill_adc_nids(codec);
3036 err = fill_input_pin_labels(codec);
3040 for (i = 0; i < cfg->num_inputs; i++) {
3043 pin = cfg->inputs[i].pin;
3044 if (!is_input_pin(codec, pin))
3048 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3049 val |= snd_hda_get_default_vref(codec, pin);
3050 if (pin != spec->hp_mic_pin)
3051 set_pin_target(codec, pin, val, false);
3054 if (is_reachable_path(codec, pin, mixer)) {
3055 err = new_analog_input(codec, i, pin,
3056 spec->input_labels[i],
3057 spec->input_label_idxs[i],
3064 err = parse_capture_source(codec, pin, i, num_adcs,
3065 spec->input_labels[i], -mixer);
3069 if (spec->add_jack_modes) {
3070 err = create_in_jack_mode(codec, pin);
3076 if (mixer && spec->add_stereo_mix_input) {
3077 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3091 /* get the input path specified by the given adc and imux indices */
3092 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3094 struct hda_gen_spec *spec = codec->spec;
3095 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3099 if (spec->dyn_adc_switch)
3100 adc_idx = spec->dyn_adc_idx[imux_idx];
3101 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3105 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3108 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3111 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3112 struct snd_ctl_elem_info *uinfo)
3114 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3115 struct hda_gen_spec *spec = codec->spec;
3116 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3119 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3120 struct snd_ctl_elem_value *ucontrol)
3122 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3123 struct hda_gen_spec *spec = codec->spec;
3124 /* the ctls are created at once with multiple counts */
3125 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3127 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3131 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3132 struct snd_ctl_elem_value *ucontrol)
3134 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3135 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3136 return mux_select(codec, adc_idx,
3137 ucontrol->value.enumerated.item[0]);
3140 static const struct snd_kcontrol_new cap_src_temp = {
3141 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3142 .name = "Input Source",
3143 .info = mux_enum_info,
3144 .get = mux_enum_get,
3145 .put = mux_enum_put,
3149 * capture volume and capture switch ctls
3152 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3153 struct snd_ctl_elem_value *ucontrol);
3155 /* call the given amp update function for all amps in the imux list at once */
3156 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3157 struct snd_ctl_elem_value *ucontrol,
3158 put_call_t func, int type)
3160 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3161 struct hda_gen_spec *spec = codec->spec;
3162 const struct hda_input_mux *imux;
3163 struct nid_path *path;
3164 int i, adc_idx, err = 0;
3166 imux = &spec->input_mux;
3167 adc_idx = kcontrol->id.index;
3168 mutex_lock(&codec->control_mutex);
3169 /* we use the cache-only update at first since multiple input paths
3170 * may shared the same amp; by updating only caches, the redundant
3171 * writes to hardware can be reduced.
3173 codec->cached_write = 1;
3174 for (i = 0; i < imux->num_items; i++) {
3175 path = get_input_path(codec, adc_idx, i);
3176 if (!path || !path->ctls[type])
3178 kcontrol->private_value = path->ctls[type];
3179 err = func(kcontrol, ucontrol);
3184 codec->cached_write = 0;
3185 mutex_unlock(&codec->control_mutex);
3186 snd_hda_codec_flush_cache(codec); /* flush the updates */
3187 if (err >= 0 && spec->cap_sync_hook)
3188 spec->cap_sync_hook(codec, ucontrol);
3192 /* capture volume ctl callbacks */
3193 #define cap_vol_info snd_hda_mixer_amp_volume_info
3194 #define cap_vol_get snd_hda_mixer_amp_volume_get
3195 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3197 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3198 struct snd_ctl_elem_value *ucontrol)
3200 return cap_put_caller(kcontrol, ucontrol,
3201 snd_hda_mixer_amp_volume_put,
3205 static const struct snd_kcontrol_new cap_vol_temp = {
3206 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3207 .name = "Capture Volume",
3208 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3209 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3210 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3211 .info = cap_vol_info,
3214 .tlv = { .c = cap_vol_tlv },
3217 /* capture switch ctl callbacks */
3218 #define cap_sw_info snd_ctl_boolean_stereo_info
3219 #define cap_sw_get snd_hda_mixer_amp_switch_get
3221 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3222 struct snd_ctl_elem_value *ucontrol)
3224 return cap_put_caller(kcontrol, ucontrol,
3225 snd_hda_mixer_amp_switch_put,
3229 static const struct snd_kcontrol_new cap_sw_temp = {
3230 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3231 .name = "Capture Switch",
3232 .info = cap_sw_info,
3237 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3242 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3243 for (depth = 0; depth < 3; depth++) {
3244 if (depth >= path->depth)
3246 i = path->depth - depth - 1;
3247 nid = path->path[i];
3248 if (!path->ctls[NID_PATH_VOL_CTL]) {
3249 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3250 path->ctls[NID_PATH_VOL_CTL] =
3251 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3252 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3253 int idx = path->idx[i];
3254 if (!depth && codec->single_adc_amp)
3256 path->ctls[NID_PATH_VOL_CTL] =
3257 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3260 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3261 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3262 path->ctls[NID_PATH_MUTE_CTL] =
3263 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3264 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3265 int idx = path->idx[i];
3266 if (!depth && codec->single_adc_amp)
3268 path->ctls[NID_PATH_MUTE_CTL] =
3269 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3276 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3278 struct hda_gen_spec *spec = codec->spec;
3279 struct auto_pin_cfg *cfg = &spec->autocfg;
3283 if (!spec->inv_dmic_split)
3285 for (i = 0; i < cfg->num_inputs; i++) {
3286 if (cfg->inputs[i].pin != nid)
3288 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3290 val = snd_hda_codec_get_pincfg(codec, nid);
3291 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3296 /* capture switch put callback for a single control with hook call */
3297 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3298 struct snd_ctl_elem_value *ucontrol)
3300 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3301 struct hda_gen_spec *spec = codec->spec;
3304 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3308 if (spec->cap_sync_hook)
3309 spec->cap_sync_hook(codec, ucontrol);
3314 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3315 int idx, bool is_switch, unsigned int ctl,
3318 struct hda_gen_spec *spec = codec->spec;
3319 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3320 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3321 const char *sfx = is_switch ? "Switch" : "Volume";
3322 unsigned int chs = inv_dmic ? 1 : 3;
3323 struct snd_kcontrol_new *knew;
3329 snprintf(tmpname, sizeof(tmpname),
3330 "%s Capture %s", label, sfx);
3332 snprintf(tmpname, sizeof(tmpname),
3334 knew = add_control(spec, type, tmpname, idx,
3335 amp_val_replace_channels(ctl, chs));
3339 knew->put = cap_single_sw_put;
3343 /* Make independent right kcontrol */
3345 snprintf(tmpname, sizeof(tmpname),
3346 "Inverted %s Capture %s", label, sfx);
3348 snprintf(tmpname, sizeof(tmpname),
3349 "Inverted Capture %s", sfx);
3350 knew = add_control(spec, type, tmpname, idx,
3351 amp_val_replace_channels(ctl, 2));
3355 knew->put = cap_single_sw_put;
3359 /* create single (and simple) capture volume and switch controls */
3360 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3361 unsigned int vol_ctl, unsigned int sw_ctl,
3365 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3368 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3374 /* create bound capture volume and switch controls */
3375 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3376 unsigned int vol_ctl, unsigned int sw_ctl)
3378 struct hda_gen_spec *spec = codec->spec;
3379 struct snd_kcontrol_new *knew;
3382 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3386 knew->private_value = vol_ctl;
3387 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3390 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3394 knew->private_value = sw_ctl;
3395 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3400 /* return the vol ctl when used first in the imux list */
3401 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3403 struct nid_path *path;
3407 path = get_input_path(codec, 0, idx);
3410 ctl = path->ctls[type];
3413 for (i = 0; i < idx - 1; i++) {
3414 path = get_input_path(codec, 0, i);
3415 if (path && path->ctls[type] == ctl)
3421 /* create individual capture volume and switch controls per input */
3422 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3424 struct hda_gen_spec *spec = codec->spec;
3425 struct hda_input_mux *imux = &spec->input_mux;
3428 for (i = 0; i < imux->num_items; i++) {
3432 idx = imux->items[i].index;
3433 if (idx >= spec->autocfg.num_inputs)
3435 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3437 for (type = 0; type < 2; type++) {
3438 err = add_single_cap_ctl(codec,
3439 spec->input_labels[idx],
3440 spec->input_label_idxs[idx],
3442 get_first_cap_ctl(codec, i, type),
3451 static int create_capture_mixers(struct hda_codec *codec)
3453 struct hda_gen_spec *spec = codec->spec;
3454 struct hda_input_mux *imux = &spec->input_mux;
3455 int i, n, nums, err;
3457 if (spec->dyn_adc_switch)
3460 nums = spec->num_adc_nids;
3462 if (!spec->auto_mic && imux->num_items > 1) {
3463 struct snd_kcontrol_new *knew;
3465 name = nums > 1 ? "Input Source" : "Capture Source";
3466 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3472 for (n = 0; n < nums; n++) {
3474 bool multi_cap_vol = spec->multi_cap_vol;
3475 bool inv_dmic = false;
3479 for (i = 0; i < imux->num_items; i++) {
3480 struct nid_path *path;
3481 path = get_input_path(codec, n, i);
3484 parse_capvol_in_path(codec, path);
3486 vol = path->ctls[NID_PATH_VOL_CTL];
3487 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3489 if (!same_amp_caps(codec, vol,
3490 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3491 multi_cap_vol = true;
3494 sw = path->ctls[NID_PATH_MUTE_CTL];
3495 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3497 if (!same_amp_caps(codec, sw,
3498 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3499 multi_cap_vol = true;
3501 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3506 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3508 else if (!multi_cap_vol)
3509 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3511 err = create_multi_cap_vol_ctl(codec);
3520 * add mic boosts if needed
3523 /* check whether the given amp is feasible as a boost volume */
3524 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3529 if (!nid_has_volume(codec, nid, dir) ||
3530 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3531 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3534 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3535 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3541 /* look for a boost amp in a widget close to the pin */
3542 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3543 struct nid_path *path)
3545 unsigned int val = 0;
3549 for (depth = 0; depth < 3; depth++) {
3550 if (depth >= path->depth - 1)
3552 nid = path->path[depth];
3553 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3554 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3556 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3557 path->idx[depth])) {
3558 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3567 static int parse_mic_boost(struct hda_codec *codec)
3569 struct hda_gen_spec *spec = codec->spec;
3570 struct auto_pin_cfg *cfg = &spec->autocfg;
3571 struct hda_input_mux *imux = &spec->input_mux;
3574 if (!spec->num_adc_nids)
3577 for (i = 0; i < imux->num_items; i++) {
3578 struct nid_path *path;
3581 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3583 idx = imux->items[i].index;
3584 if (idx >= imux->num_items)
3587 /* check only line-in and mic pins */
3588 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3591 path = get_input_path(codec, 0, i);
3595 val = look_for_boost_amp(codec, path);
3599 /* create a boost control */
3600 snprintf(boost_label, sizeof(boost_label),
3601 "%s Boost Volume", spec->input_labels[idx]);
3602 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3603 spec->input_label_idxs[idx], val))
3606 path->ctls[NID_PATH_BOOST_CTL] = val;
3612 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3614 static void parse_digital(struct hda_codec *codec)
3616 struct hda_gen_spec *spec = codec->spec;
3617 struct nid_path *path;
3619 hda_nid_t dig_nid, pin;
3621 /* support multiple SPDIFs; the secondary is set up as a slave */
3623 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3624 pin = spec->autocfg.dig_out_pins[i];
3625 dig_nid = look_for_dac(codec, pin, true);
3628 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3631 print_nid_path("digout", path);
3632 path->active = true;
3633 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3634 set_pin_target(codec, pin, PIN_OUT, false);
3636 spec->multiout.dig_out_nid = dig_nid;
3637 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3639 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3640 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3642 spec->slave_dig_outs[nums - 1] = dig_nid;
3647 if (spec->autocfg.dig_in_pin) {
3648 pin = spec->autocfg.dig_in_pin;
3649 dig_nid = codec->start_nid;
3650 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3651 unsigned int wcaps = get_wcaps(codec, dig_nid);
3652 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3654 if (!(wcaps & AC_WCAP_DIGITAL))
3656 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3658 print_nid_path("digin", path);
3659 path->active = true;
3660 spec->dig_in_nid = dig_nid;
3661 spec->digin_path = snd_hda_get_path_idx(codec, path);
3662 set_pin_target(codec, pin, PIN_IN, false);
3671 * input MUX handling
3674 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3676 /* select the given imux item; either unmute exclusively or select the route */
3677 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3680 struct hda_gen_spec *spec = codec->spec;
3681 const struct hda_input_mux *imux;
3682 struct nid_path *old_path, *path;
3684 imux = &spec->input_mux;
3685 if (!imux->num_items)
3688 if (idx >= imux->num_items)
3689 idx = imux->num_items - 1;
3690 if (spec->cur_mux[adc_idx] == idx)
3693 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3696 if (old_path->active)
3697 snd_hda_activate_path(codec, old_path, false, false);
3699 spec->cur_mux[adc_idx] = idx;
3702 update_hp_mic(codec, adc_idx, false);
3704 if (spec->dyn_adc_switch)
3705 dyn_adc_pcm_resetup(codec, idx);
3707 path = get_input_path(codec, adc_idx, idx);
3712 snd_hda_activate_path(codec, path, true, false);
3713 if (spec->cap_sync_hook)
3714 spec->cap_sync_hook(codec, NULL);
3715 path_power_down_sync(codec, old_path);
3721 * Jack detections for HP auto-mute and mic-switch
3724 /* check each pin in the given array; returns true if any of them is plugged */
3725 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3729 for (i = 0; i < num_pins; i++) {
3730 hda_nid_t nid = pins[i];
3733 /* don't detect pins retasked as inputs */
3734 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3736 present |= snd_hda_jack_detect(codec, nid);
3741 /* standard HP/line-out auto-mute helper */
3742 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
3745 struct hda_gen_spec *spec = codec->spec;
3748 for (i = 0; i < num_pins; i++) {
3749 hda_nid_t nid = pins[i];
3750 unsigned int val, oldval;
3754 if (spec->auto_mute_via_amp) {
3756 spec->mute_bits |= (1ULL << nid);
3758 spec->mute_bits &= ~(1ULL << nid);
3759 set_pin_eapd(codec, nid, !mute);
3763 oldval = snd_hda_codec_get_pin_target(codec, nid);
3764 if (oldval & PIN_IN)
3765 continue; /* no mute for inputs */
3766 /* don't reset VREF value in case it's controlling
3767 * the amp (see alc861_fixup_asus_amp_vref_0f())
3769 if (spec->keep_vref_in_automute)
3770 val = oldval & ~PIN_HP;
3775 /* here we call update_pin_ctl() so that the pinctl is changed
3776 * without changing the pinctl target value;
3777 * the original target value will be still referred at the
3778 * init / resume again
3780 update_pin_ctl(codec, nid, val);
3781 set_pin_eapd(codec, nid, !mute);
3785 /* Toggle outputs muting */
3786 void snd_hda_gen_update_outputs(struct hda_codec *codec)
3788 struct hda_gen_spec *spec = codec->spec;
3791 /* Control HP pins/amps depending on master_mute state;
3792 * in general, HP pins/amps control should be enabled in all cases,
3793 * but currently set only for master_mute, just to be safe
3795 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3796 spec->autocfg.hp_pins, spec->master_mute);
3798 if (!spec->automute_speaker)
3801 on = spec->hp_jack_present | spec->line_jack_present;
3802 on |= spec->master_mute;
3803 spec->speaker_muted = on;
3804 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
3805 spec->autocfg.speaker_pins, on);
3807 /* toggle line-out mutes if needed, too */
3808 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3809 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3810 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3812 if (!spec->automute_lo)
3815 on = spec->hp_jack_present;
3816 on |= spec->master_mute;
3817 spec->line_out_muted = on;
3818 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3819 spec->autocfg.line_out_pins, on);
3821 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
3823 static void call_update_outputs(struct hda_codec *codec)
3825 struct hda_gen_spec *spec = codec->spec;
3826 if (spec->automute_hook)
3827 spec->automute_hook(codec);
3829 snd_hda_gen_update_outputs(codec);
3831 /* sync the whole vmaster slaves to reflect the new auto-mute status */
3832 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
3833 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
3836 /* standard HP-automute helper */
3837 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3839 struct hda_gen_spec *spec = codec->spec;
3840 hda_nid_t *pins = spec->autocfg.hp_pins;
3841 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
3843 /* No detection for the first HP jack during indep-HP mode */
3844 if (spec->indep_hp_enabled) {
3849 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
3850 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3852 call_update_outputs(codec);
3854 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
3856 /* standard line-out-automute helper */
3857 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3859 struct hda_gen_spec *spec = codec->spec;
3861 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3863 /* check LO jack only when it's different from HP */
3864 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3867 spec->line_jack_present =
3868 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3869 spec->autocfg.line_out_pins);
3870 if (!spec->automute_speaker || !spec->detect_lo)
3872 call_update_outputs(codec);
3874 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
3876 /* standard mic auto-switch helper */
3877 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
3879 struct hda_gen_spec *spec = codec->spec;
3882 if (!spec->auto_mic)
3885 for (i = spec->am_num_entries - 1; i > 0; i--) {
3886 hda_nid_t pin = spec->am_entry[i].pin;
3887 /* don't detect pins retasked as outputs */
3888 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3890 if (snd_hda_jack_detect(codec, pin)) {
3891 mux_select(codec, 0, spec->am_entry[i].idx);
3895 mux_select(codec, 0, spec->am_entry[0].idx);
3897 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
3899 /* call appropriate hooks */
3900 static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3902 struct hda_gen_spec *spec = codec->spec;
3903 if (spec->hp_automute_hook)
3904 spec->hp_automute_hook(codec, jack);
3906 snd_hda_gen_hp_automute(codec, jack);
3909 static void call_line_automute(struct hda_codec *codec,
3910 struct hda_jack_tbl *jack)
3912 struct hda_gen_spec *spec = codec->spec;
3913 if (spec->line_automute_hook)
3914 spec->line_automute_hook(codec, jack);
3916 snd_hda_gen_line_automute(codec, jack);
3919 static void call_mic_autoswitch(struct hda_codec *codec,
3920 struct hda_jack_tbl *jack)
3922 struct hda_gen_spec *spec = codec->spec;
3923 if (spec->mic_autoswitch_hook)
3924 spec->mic_autoswitch_hook(codec, jack);
3926 snd_hda_gen_mic_autoswitch(codec, jack);
3929 /* update jack retasking */
3930 static void update_automute_all(struct hda_codec *codec)
3932 call_hp_automute(codec, NULL);
3933 call_line_automute(codec, NULL);
3934 call_mic_autoswitch(codec, NULL);
3938 * Auto-Mute mode mixer enum support
3940 static int automute_mode_info(struct snd_kcontrol *kcontrol,
3941 struct snd_ctl_elem_info *uinfo)
3943 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3944 struct hda_gen_spec *spec = codec->spec;
3945 static const char * const texts3[] = {
3946 "Disabled", "Speaker Only", "Line Out+Speaker"
3949 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3950 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3951 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3954 static int automute_mode_get(struct snd_kcontrol *kcontrol,
3955 struct snd_ctl_elem_value *ucontrol)
3957 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3958 struct hda_gen_spec *spec = codec->spec;
3959 unsigned int val = 0;
3960 if (spec->automute_speaker)
3962 if (spec->automute_lo)
3965 ucontrol->value.enumerated.item[0] = val;
3969 static int automute_mode_put(struct snd_kcontrol *kcontrol,
3970 struct snd_ctl_elem_value *ucontrol)
3972 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3973 struct hda_gen_spec *spec = codec->spec;
3975 switch (ucontrol->value.enumerated.item[0]) {
3977 if (!spec->automute_speaker && !spec->automute_lo)
3979 spec->automute_speaker = 0;
3980 spec->automute_lo = 0;
3983 if (spec->automute_speaker_possible) {
3984 if (!spec->automute_lo && spec->automute_speaker)
3986 spec->automute_speaker = 1;
3987 spec->automute_lo = 0;
3988 } else if (spec->automute_lo_possible) {
3989 if (spec->automute_lo)
3991 spec->automute_lo = 1;
3996 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3998 if (spec->automute_speaker && spec->automute_lo)
4000 spec->automute_speaker = 1;
4001 spec->automute_lo = 1;
4006 call_update_outputs(codec);
4010 static const struct snd_kcontrol_new automute_mode_enum = {
4011 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4012 .name = "Auto-Mute Mode",
4013 .info = automute_mode_info,
4014 .get = automute_mode_get,
4015 .put = automute_mode_put,
4018 static int add_automute_mode_enum(struct hda_codec *codec)
4020 struct hda_gen_spec *spec = codec->spec;
4022 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4028 * Check the availability of HP/line-out auto-mute;
4029 * Set up appropriately if really supported
4031 static int check_auto_mute_availability(struct hda_codec *codec)
4033 struct hda_gen_spec *spec = codec->spec;
4034 struct auto_pin_cfg *cfg = &spec->autocfg;
4038 if (spec->suppress_auto_mute)
4041 if (cfg->hp_pins[0])
4043 if (cfg->line_out_pins[0])
4045 if (cfg->speaker_pins[0])
4047 if (present < 2) /* need two different output types */
4050 if (!cfg->speaker_pins[0] &&
4051 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4052 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4053 sizeof(cfg->speaker_pins));
4054 cfg->speaker_outs = cfg->line_outs;
4057 if (!cfg->hp_pins[0] &&
4058 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4059 memcpy(cfg->hp_pins, cfg->line_out_pins,
4060 sizeof(cfg->hp_pins));
4061 cfg->hp_outs = cfg->line_outs;
4064 for (i = 0; i < cfg->hp_outs; i++) {
4065 hda_nid_t nid = cfg->hp_pins[i];
4066 if (!is_jack_detectable(codec, nid))
4068 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
4070 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
4072 spec->detect_hp = 1;
4075 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4076 if (cfg->speaker_outs)
4077 for (i = 0; i < cfg->line_outs; i++) {
4078 hda_nid_t nid = cfg->line_out_pins[i];
4079 if (!is_jack_detectable(codec, nid))
4081 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
4082 snd_hda_jack_detect_enable_callback(codec, nid,
4083 HDA_GEN_FRONT_EVENT,
4084 call_line_automute);
4085 spec->detect_lo = 1;
4087 spec->automute_lo_possible = spec->detect_hp;
4090 spec->automute_speaker_possible = cfg->speaker_outs &&
4091 (spec->detect_hp || spec->detect_lo);
4093 spec->automute_lo = spec->automute_lo_possible;
4094 spec->automute_speaker = spec->automute_speaker_possible;
4096 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4097 /* create a control for automute mode */
4098 err = add_automute_mode_enum(codec);
4105 /* check whether all auto-mic pins are valid; setup indices if OK */
4106 static bool auto_mic_check_imux(struct hda_codec *codec)
4108 struct hda_gen_spec *spec = codec->spec;
4109 const struct hda_input_mux *imux;
4112 imux = &spec->input_mux;
4113 for (i = 0; i < spec->am_num_entries; i++) {
4114 spec->am_entry[i].idx =
4115 find_idx_in_nid_list(spec->am_entry[i].pin,
4116 spec->imux_pins, imux->num_items);
4117 if (spec->am_entry[i].idx < 0)
4118 return false; /* no corresponding imux */
4121 /* we don't need the jack detection for the first pin */
4122 for (i = 1; i < spec->am_num_entries; i++)
4123 snd_hda_jack_detect_enable_callback(codec,
4124 spec->am_entry[i].pin,
4126 call_mic_autoswitch);
4130 static int compare_attr(const void *ap, const void *bp)
4132 const struct automic_entry *a = ap;
4133 const struct automic_entry *b = bp;
4134 return (int)(a->attr - b->attr);
4138 * Check the availability of auto-mic switch;
4139 * Set up if really supported
4141 static int check_auto_mic_availability(struct hda_codec *codec)
4143 struct hda_gen_spec *spec = codec->spec;
4144 struct auto_pin_cfg *cfg = &spec->autocfg;
4148 if (spec->suppress_auto_mic)
4153 for (i = 0; i < cfg->num_inputs; i++) {
4154 hda_nid_t nid = cfg->inputs[i].pin;
4156 attr = snd_hda_codec_get_pincfg(codec, nid);
4157 attr = snd_hda_get_input_pin_attr(attr);
4158 if (types & (1 << attr))
4159 return 0; /* already occupied */
4161 case INPUT_PIN_ATTR_INT:
4162 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4163 return 0; /* invalid type */
4165 case INPUT_PIN_ATTR_UNUSED:
4166 return 0; /* invalid entry */
4168 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4169 return 0; /* invalid type */
4170 if (!spec->line_in_auto_switch &&
4171 cfg->inputs[i].type != AUTO_PIN_MIC)
4172 return 0; /* only mic is allowed */
4173 if (!is_jack_detectable(codec, nid))
4174 return 0; /* no unsol support */
4177 if (num_pins >= MAX_AUTO_MIC_PINS)
4179 types |= (1 << attr);
4180 spec->am_entry[num_pins].pin = nid;
4181 spec->am_entry[num_pins].attr = attr;
4188 spec->am_num_entries = num_pins;
4189 /* sort the am_entry in the order of attr so that the pin with a
4190 * higher attr will be selected when the jack is plugged.
4192 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4193 compare_attr, NULL);
4195 if (!auto_mic_check_imux(codec))
4199 spec->num_adc_nids = 1;
4200 spec->cur_mux[0] = spec->am_entry[0].idx;
4201 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4202 spec->am_entry[0].pin,
4203 spec->am_entry[1].pin,
4204 spec->am_entry[2].pin);
4209 /* power_filter hook; make inactive widgets into power down */
4210 static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4212 unsigned int power_state)
4214 if (power_state != AC_PWRST_D0)
4216 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4218 if (is_active_nid_for_any(codec, nid))
4225 * Parse the given BIOS configuration and set up the hda_gen_spec
4227 * return 1 if successful, 0 if the proper config is not found,
4228 * or a negative error code
4230 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4231 struct auto_pin_cfg *cfg)
4233 struct hda_gen_spec *spec = codec->spec;
4236 parse_user_hints(codec);
4238 if (spec->mixer_nid && !spec->mixer_merge_nid)
4239 spec->mixer_merge_nid = spec->mixer_nid;
4241 if (cfg != &spec->autocfg) {
4242 spec->autocfg = *cfg;
4243 cfg = &spec->autocfg;
4246 if (!spec->main_out_badness)
4247 spec->main_out_badness = &hda_main_out_badness;
4248 if (!spec->extra_out_badness)
4249 spec->extra_out_badness = &hda_extra_out_badness;
4251 fill_all_dac_nids(codec);
4253 if (!cfg->line_outs) {
4254 if (cfg->dig_outs || cfg->dig_in_pin) {
4255 spec->multiout.max_channels = 2;
4256 spec->no_analog = 1;
4259 return 0; /* can't find valid BIOS pin config */
4262 if (!spec->no_primary_hp &&
4263 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4264 cfg->line_outs <= cfg->hp_outs) {
4265 /* use HP as primary out */
4266 cfg->speaker_outs = cfg->line_outs;
4267 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4268 sizeof(cfg->speaker_pins));
4269 cfg->line_outs = cfg->hp_outs;
4270 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4272 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4273 cfg->line_out_type = AUTO_PIN_HP_OUT;
4276 err = parse_output_paths(codec);
4279 err = create_multi_channel_mode(codec);
4282 err = create_multi_out_ctls(codec, cfg);
4285 err = create_hp_out_ctls(codec);
4288 err = create_speaker_out_ctls(codec);
4291 err = create_indep_hp_ctls(codec);
4294 err = create_loopback_mixing_ctl(codec);
4297 err = create_hp_mic(codec);
4300 err = create_input_ctls(codec);
4304 spec->const_channel_count = spec->ext_channel_count;
4305 /* check the multiple speaker and headphone pins */
4306 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4307 spec->const_channel_count = max(spec->const_channel_count,
4308 cfg->speaker_outs * 2);
4309 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4310 spec->const_channel_count = max(spec->const_channel_count,
4312 spec->multiout.max_channels = max(spec->ext_channel_count,
4313 spec->const_channel_count);
4315 err = check_auto_mute_availability(codec);
4319 err = check_dyn_adc_switch(codec);
4323 err = check_auto_mic_availability(codec);
4327 err = create_capture_mixers(codec);
4331 err = parse_mic_boost(codec);
4335 if (spec->add_jack_modes) {
4336 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4337 err = create_out_jack_modes(codec, cfg->line_outs,
4338 cfg->line_out_pins);
4342 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4343 err = create_out_jack_modes(codec, cfg->hp_outs,
4351 parse_digital(codec);
4353 if (spec->power_down_unused)
4354 codec->power_filter = snd_hda_gen_path_power_filter;
4356 if (!spec->no_analog && spec->beep_nid) {
4357 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4364 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
4368 * Build control elements
4371 /* slave controls for virtual master */
4372 static const char * const slave_pfxs[] = {
4373 "Front", "Surround", "Center", "LFE", "Side",
4374 "Headphone", "Speaker", "Mono", "Line Out",
4375 "CLFE", "Bass Speaker", "PCM",
4376 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4377 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4382 int snd_hda_gen_build_controls(struct hda_codec *codec)
4384 struct hda_gen_spec *spec = codec->spec;
4387 if (spec->kctls.used) {
4388 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4393 if (spec->multiout.dig_out_nid) {
4394 err = snd_hda_create_dig_out_ctls(codec,
4395 spec->multiout.dig_out_nid,
4396 spec->multiout.dig_out_nid,
4397 spec->pcm_rec[1].pcm_type);
4400 if (!spec->no_analog) {
4401 err = snd_hda_create_spdif_share_sw(codec,
4405 spec->multiout.share_spdif = 1;
4408 if (spec->dig_in_nid) {
4409 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4414 /* if we have no master control, let's create it */
4415 if (!spec->no_analog &&
4416 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
4417 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
4418 spec->vmaster_tlv, slave_pfxs,
4423 if (!spec->no_analog &&
4424 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4425 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4428 true, &spec->vmaster_mute.sw_kctl);
4431 if (spec->vmaster_mute.hook)
4432 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4433 spec->vmaster_mute_enum);
4436 free_kctls(spec); /* no longer needed */
4438 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4444 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4451 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4452 struct hda_codec *codec,
4453 struct snd_pcm_substream *substream,
4456 struct hda_gen_spec *spec = codec->spec;
4457 if (spec->pcm_playback_hook)
4458 spec->pcm_playback_hook(hinfo, codec, substream, action);
4461 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4462 struct hda_codec *codec,
4463 struct snd_pcm_substream *substream,
4466 struct hda_gen_spec *spec = codec->spec;
4467 if (spec->pcm_capture_hook)
4468 spec->pcm_capture_hook(hinfo, codec, substream, action);
4472 * Analog playback callbacks
4474 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4475 struct hda_codec *codec,
4476 struct snd_pcm_substream *substream)
4478 struct hda_gen_spec *spec = codec->spec;
4481 mutex_lock(&spec->pcm_mutex);
4482 err = snd_hda_multi_out_analog_open(codec,
4483 &spec->multiout, substream,
4486 spec->active_streams |= 1 << STREAM_MULTI_OUT;
4487 call_pcm_playback_hook(hinfo, codec, substream,
4488 HDA_GEN_PCM_ACT_OPEN);
4490 mutex_unlock(&spec->pcm_mutex);
4494 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4495 struct hda_codec *codec,
4496 unsigned int stream_tag,
4497 unsigned int format,
4498 struct snd_pcm_substream *substream)
4500 struct hda_gen_spec *spec = codec->spec;
4503 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4504 stream_tag, format, substream);
4506 call_pcm_playback_hook(hinfo, codec, substream,
4507 HDA_GEN_PCM_ACT_PREPARE);
4511 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4512 struct hda_codec *codec,
4513 struct snd_pcm_substream *substream)
4515 struct hda_gen_spec *spec = codec->spec;
4518 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4520 call_pcm_playback_hook(hinfo, codec, substream,
4521 HDA_GEN_PCM_ACT_CLEANUP);
4525 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4526 struct hda_codec *codec,
4527 struct snd_pcm_substream *substream)
4529 struct hda_gen_spec *spec = codec->spec;
4530 mutex_lock(&spec->pcm_mutex);
4531 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
4532 call_pcm_playback_hook(hinfo, codec, substream,
4533 HDA_GEN_PCM_ACT_CLOSE);
4534 mutex_unlock(&spec->pcm_mutex);
4538 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4539 struct hda_codec *codec,
4540 struct snd_pcm_substream *substream)
4542 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4546 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4547 struct hda_codec *codec,
4548 unsigned int stream_tag,
4549 unsigned int format,
4550 struct snd_pcm_substream *substream)
4552 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4553 call_pcm_capture_hook(hinfo, codec, substream,
4554 HDA_GEN_PCM_ACT_PREPARE);
4558 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4559 struct hda_codec *codec,
4560 struct snd_pcm_substream *substream)
4562 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4563 call_pcm_capture_hook(hinfo, codec, substream,
4564 HDA_GEN_PCM_ACT_CLEANUP);
4568 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4569 struct hda_codec *codec,
4570 struct snd_pcm_substream *substream)
4572 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4576 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4577 struct hda_codec *codec,
4578 struct snd_pcm_substream *substream)
4580 struct hda_gen_spec *spec = codec->spec;
4583 mutex_lock(&spec->pcm_mutex);
4584 if (!spec->indep_hp_enabled)
4587 spec->active_streams |= 1 << STREAM_INDEP_HP;
4588 call_pcm_playback_hook(hinfo, codec, substream,
4589 HDA_GEN_PCM_ACT_OPEN);
4590 mutex_unlock(&spec->pcm_mutex);
4594 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4595 struct hda_codec *codec,
4596 struct snd_pcm_substream *substream)
4598 struct hda_gen_spec *spec = codec->spec;
4599 mutex_lock(&spec->pcm_mutex);
4600 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
4601 call_pcm_playback_hook(hinfo, codec, substream,
4602 HDA_GEN_PCM_ACT_CLOSE);
4603 mutex_unlock(&spec->pcm_mutex);
4607 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4608 struct hda_codec *codec,
4609 unsigned int stream_tag,
4610 unsigned int format,
4611 struct snd_pcm_substream *substream)
4613 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4614 call_pcm_playback_hook(hinfo, codec, substream,
4615 HDA_GEN_PCM_ACT_PREPARE);
4619 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4620 struct hda_codec *codec,
4621 struct snd_pcm_substream *substream)
4623 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4624 call_pcm_playback_hook(hinfo, codec, substream,
4625 HDA_GEN_PCM_ACT_CLEANUP);
4632 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4633 struct hda_codec *codec,
4634 struct snd_pcm_substream *substream)
4636 struct hda_gen_spec *spec = codec->spec;
4637 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4640 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4641 struct hda_codec *codec,
4642 unsigned int stream_tag,
4643 unsigned int format,
4644 struct snd_pcm_substream *substream)
4646 struct hda_gen_spec *spec = codec->spec;
4647 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4648 stream_tag, format, substream);
4651 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4652 struct hda_codec *codec,
4653 struct snd_pcm_substream *substream)
4655 struct hda_gen_spec *spec = codec->spec;
4656 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4659 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4660 struct hda_codec *codec,
4661 struct snd_pcm_substream *substream)
4663 struct hda_gen_spec *spec = codec->spec;
4664 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4670 #define alt_capture_pcm_open capture_pcm_open
4671 #define alt_capture_pcm_close capture_pcm_close
4673 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4674 struct hda_codec *codec,
4675 unsigned int stream_tag,
4676 unsigned int format,
4677 struct snd_pcm_substream *substream)
4679 struct hda_gen_spec *spec = codec->spec;
4681 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
4682 stream_tag, 0, format);
4683 call_pcm_capture_hook(hinfo, codec, substream,
4684 HDA_GEN_PCM_ACT_PREPARE);
4688 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4689 struct hda_codec *codec,
4690 struct snd_pcm_substream *substream)
4692 struct hda_gen_spec *spec = codec->spec;
4694 snd_hda_codec_cleanup_stream(codec,
4695 spec->adc_nids[substream->number + 1]);
4696 call_pcm_capture_hook(hinfo, codec, substream,
4697 HDA_GEN_PCM_ACT_CLEANUP);
4703 static const struct hda_pcm_stream pcm_analog_playback = {
4707 /* NID is set in build_pcms */
4709 .open = playback_pcm_open,
4710 .close = playback_pcm_close,
4711 .prepare = playback_pcm_prepare,
4712 .cleanup = playback_pcm_cleanup
4716 static const struct hda_pcm_stream pcm_analog_capture = {
4720 /* NID is set in build_pcms */
4722 .open = capture_pcm_open,
4723 .close = capture_pcm_close,
4724 .prepare = capture_pcm_prepare,
4725 .cleanup = capture_pcm_cleanup
4729 static const struct hda_pcm_stream pcm_analog_alt_playback = {
4733 /* NID is set in build_pcms */
4735 .open = alt_playback_pcm_open,
4736 .close = alt_playback_pcm_close,
4737 .prepare = alt_playback_pcm_prepare,
4738 .cleanup = alt_playback_pcm_cleanup
4742 static const struct hda_pcm_stream pcm_analog_alt_capture = {
4743 .substreams = 2, /* can be overridden */
4746 /* NID is set in build_pcms */
4748 .open = alt_capture_pcm_open,
4749 .close = alt_capture_pcm_close,
4750 .prepare = alt_capture_pcm_prepare,
4751 .cleanup = alt_capture_pcm_cleanup
4755 static const struct hda_pcm_stream pcm_digital_playback = {
4759 /* NID is set in build_pcms */
4761 .open = dig_playback_pcm_open,
4762 .close = dig_playback_pcm_close,
4763 .prepare = dig_playback_pcm_prepare,
4764 .cleanup = dig_playback_pcm_cleanup
4768 static const struct hda_pcm_stream pcm_digital_capture = {
4772 /* NID is set in build_pcms */
4775 /* Used by build_pcms to flag that a PCM has no playback stream */
4776 static const struct hda_pcm_stream pcm_null_stream = {
4783 * dynamic changing ADC PCM streams
4785 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4787 struct hda_gen_spec *spec = codec->spec;
4788 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4790 if (spec->cur_adc && spec->cur_adc != new_adc) {
4791 /* stream is running, let's swap the current ADC */
4792 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4793 spec->cur_adc = new_adc;
4794 snd_hda_codec_setup_stream(codec, new_adc,
4795 spec->cur_adc_stream_tag, 0,
4796 spec->cur_adc_format);
4802 /* analog capture with dynamic dual-adc changes */
4803 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4804 struct hda_codec *codec,
4805 unsigned int stream_tag,
4806 unsigned int format,
4807 struct snd_pcm_substream *substream)
4809 struct hda_gen_spec *spec = codec->spec;
4810 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4811 spec->cur_adc_stream_tag = stream_tag;
4812 spec->cur_adc_format = format;
4813 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4817 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4818 struct hda_codec *codec,
4819 struct snd_pcm_substream *substream)
4821 struct hda_gen_spec *spec = codec->spec;
4822 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4827 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4831 .nid = 0, /* fill later */
4833 .prepare = dyn_adc_capture_pcm_prepare,
4834 .cleanup = dyn_adc_capture_pcm_cleanup
4838 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4839 const char *chip_name)
4845 strlcpy(str, chip_name, len);
4847 /* drop non-alnum chars after a space */
4848 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4849 if (!isalnum(p[1])) {
4854 strlcat(str, sfx, len);
4857 /* build PCM streams based on the parsed results */
4858 int snd_hda_gen_build_pcms(struct hda_codec *codec)
4860 struct hda_gen_spec *spec = codec->spec;
4861 struct hda_pcm *info = spec->pcm_rec;
4862 const struct hda_pcm_stream *p;
4863 bool have_multi_adcs;
4865 codec->num_pcms = 1;
4866 codec->pcm_info = info;
4868 if (spec->no_analog)
4871 fill_pcm_stream_name(spec->stream_name_analog,
4872 sizeof(spec->stream_name_analog),
4873 " Analog", codec->chip_name);
4874 info->name = spec->stream_name_analog;
4876 if (spec->multiout.num_dacs > 0) {
4877 p = spec->stream_analog_playback;
4879 p = &pcm_analog_playback;
4880 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4881 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4882 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4883 spec->multiout.max_channels;
4884 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4885 spec->autocfg.line_outs == 2)
4886 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4889 if (spec->num_adc_nids) {
4890 p = spec->stream_analog_capture;
4892 if (spec->dyn_adc_switch)
4893 p = &dyn_adc_pcm_analog_capture;
4895 p = &pcm_analog_capture;
4897 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4898 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4902 /* SPDIF for stream index #1 */
4903 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
4904 fill_pcm_stream_name(spec->stream_name_digital,
4905 sizeof(spec->stream_name_digital),
4906 " Digital", codec->chip_name);
4907 codec->num_pcms = 2;
4908 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4909 info = spec->pcm_rec + 1;
4910 info->name = spec->stream_name_digital;
4911 if (spec->dig_out_type)
4912 info->pcm_type = spec->dig_out_type;
4914 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4915 if (spec->multiout.dig_out_nid) {
4916 p = spec->stream_digital_playback;
4918 p = &pcm_digital_playback;
4919 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4920 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4922 if (spec->dig_in_nid) {
4923 p = spec->stream_digital_capture;
4925 p = &pcm_digital_capture;
4926 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4927 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4931 if (spec->no_analog)
4934 /* If the use of more than one ADC is requested for the current
4935 * model, configure a second analog capture-only PCM.
4937 have_multi_adcs = (spec->num_adc_nids > 1) &&
4938 !spec->dyn_adc_switch && !spec->auto_mic;
4939 /* Additional Analaog capture for index #2 */
4940 if (spec->alt_dac_nid || have_multi_adcs) {
4941 fill_pcm_stream_name(spec->stream_name_alt_analog,
4942 sizeof(spec->stream_name_alt_analog),
4943 " Alt Analog", codec->chip_name);
4944 codec->num_pcms = 3;
4945 info = spec->pcm_rec + 2;
4946 info->name = spec->stream_name_alt_analog;
4947 if (spec->alt_dac_nid) {
4948 p = spec->stream_analog_alt_playback;
4950 p = &pcm_analog_alt_playback;
4951 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4952 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4955 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4957 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4959 if (have_multi_adcs) {
4960 p = spec->stream_analog_alt_capture;
4962 p = &pcm_analog_alt_capture;
4963 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4964 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4966 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4967 spec->num_adc_nids - 1;
4969 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4971 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4977 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4981 * Standard auto-parser initializations
4984 /* configure the given path as a proper output */
4985 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
4987 struct nid_path *path;
4990 path = snd_hda_get_path_from_idx(codec, path_idx);
4991 if (!path || !path->depth)
4993 pin = path->path[path->depth - 1];
4994 restore_pin_ctl(codec, pin);
4995 snd_hda_activate_path(codec, path, path->active,
4996 aamix_default(codec->spec));
4997 set_pin_eapd(codec, pin, path->active);
5000 /* initialize primary output paths */
5001 static void init_multi_out(struct hda_codec *codec)
5003 struct hda_gen_spec *spec = codec->spec;
5006 for (i = 0; i < spec->autocfg.line_outs; i++)
5007 set_output_and_unmute(codec, spec->out_paths[i]);
5011 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5015 for (i = 0; i < num_outs; i++)
5016 set_output_and_unmute(codec, paths[i]);
5019 /* initialize hp and speaker paths */
5020 static void init_extra_out(struct hda_codec *codec)
5022 struct hda_gen_spec *spec = codec->spec;
5024 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5025 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5026 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5027 __init_extra_out(codec, spec->autocfg.speaker_outs,
5028 spec->speaker_paths);
5031 /* initialize multi-io paths */
5032 static void init_multi_io(struct hda_codec *codec)
5034 struct hda_gen_spec *spec = codec->spec;
5037 for (i = 0; i < spec->multi_ios; i++) {
5038 hda_nid_t pin = spec->multi_io[i].pin;
5039 struct nid_path *path;
5040 path = get_multiio_path(codec, i);
5043 if (!spec->multi_io[i].ctl_in)
5044 spec->multi_io[i].ctl_in =
5045 snd_hda_codec_get_pin_target(codec, pin);
5046 snd_hda_activate_path(codec, path, path->active,
5047 aamix_default(spec));
5051 /* set up input pins and loopback paths */
5052 static void init_analog_input(struct hda_codec *codec)
5054 struct hda_gen_spec *spec = codec->spec;
5055 struct auto_pin_cfg *cfg = &spec->autocfg;
5058 for (i = 0; i < cfg->num_inputs; i++) {
5059 hda_nid_t nid = cfg->inputs[i].pin;
5060 if (is_input_pin(codec, nid))
5061 restore_pin_ctl(codec, nid);
5063 /* init loopback inputs */
5064 if (spec->mixer_nid) {
5065 resume_path_from_idx(codec, spec->loopback_paths[i]);
5066 resume_path_from_idx(codec, spec->loopback_merge_path);
5071 /* initialize ADC paths */
5072 static void init_input_src(struct hda_codec *codec)
5074 struct hda_gen_spec *spec = codec->spec;
5075 struct hda_input_mux *imux = &spec->input_mux;
5076 struct nid_path *path;
5079 if (spec->dyn_adc_switch)
5082 nums = spec->num_adc_nids;
5084 for (c = 0; c < nums; c++) {
5085 for (i = 0; i < imux->num_items; i++) {
5086 path = get_input_path(codec, c, i);
5088 bool active = path->active;
5089 if (i == spec->cur_mux[c])
5091 snd_hda_activate_path(codec, path, active, false);
5095 update_hp_mic(codec, c, true);
5098 if (spec->cap_sync_hook)
5099 spec->cap_sync_hook(codec, NULL);
5102 /* set right pin controls for digital I/O */
5103 static void init_digital(struct hda_codec *codec)
5105 struct hda_gen_spec *spec = codec->spec;
5109 for (i = 0; i < spec->autocfg.dig_outs; i++)
5110 set_output_and_unmute(codec, spec->digout_paths[i]);
5111 pin = spec->autocfg.dig_in_pin;
5113 restore_pin_ctl(codec, pin);
5114 resume_path_from_idx(codec, spec->digin_path);
5118 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5119 * invalid unsol tags by some reason
5121 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5125 for (i = 0; i < codec->init_pins.used; i++) {
5126 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5127 hda_nid_t nid = pin->nid;
5128 if (is_jack_detectable(codec, nid) &&
5129 !snd_hda_jack_tbl_get(codec, nid))
5130 snd_hda_codec_update_cache(codec, nid, 0,
5131 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5136 * initialize the generic spec;
5137 * this can be put as patch_ops.init function
5139 int snd_hda_gen_init(struct hda_codec *codec)
5141 struct hda_gen_spec *spec = codec->spec;
5143 if (spec->init_hook)
5144 spec->init_hook(codec);
5146 snd_hda_apply_verbs(codec);
5148 codec->cached_write = 1;
5150 init_multi_out(codec);
5151 init_extra_out(codec);
5152 init_multi_io(codec);
5153 init_analog_input(codec);
5154 init_input_src(codec);
5155 init_digital(codec);
5157 clear_unsol_on_unused_pins(codec);
5159 /* call init functions of standard auto-mute helpers */
5160 update_automute_all(codec);
5162 snd_hda_codec_flush_cache(codec);
5164 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5165 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5167 hda_call_check_power_status(codec, 0x01);
5170 EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5173 * free the generic spec;
5174 * this can be put as patch_ops.free function
5176 void snd_hda_gen_free(struct hda_codec *codec)
5178 snd_hda_detach_beep_device(codec);
5179 snd_hda_gen_spec_free(codec->spec);
5183 EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5187 * check the loopback power save state;
5188 * this can be put as patch_ops.check_power_status function
5190 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5192 struct hda_gen_spec *spec = codec->spec;
5193 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5195 EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5200 * the generic codec support
5203 static const struct hda_codec_ops generic_patch_ops = {
5204 .build_controls = snd_hda_gen_build_controls,
5205 .build_pcms = snd_hda_gen_build_pcms,
5206 .init = snd_hda_gen_init,
5207 .free = snd_hda_gen_free,
5208 .unsol_event = snd_hda_jack_unsol_event,
5210 .check_power_status = snd_hda_gen_check_power_status,
5214 int snd_hda_parse_generic_codec(struct hda_codec *codec)
5216 struct hda_gen_spec *spec;
5219 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5222 snd_hda_gen_spec_init(spec);
5225 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5229 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5233 codec->patch_ops = generic_patch_ops;
5237 snd_hda_gen_free(codec);
5240 EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);