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_io");
147 spec->no_multi_io = !val;
148 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
150 spec->multi_cap_vol = !!val;
151 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
153 spec->inv_dmic_split = !!val;
154 val = snd_hda_get_bool_hint(codec, "indep_hp");
156 spec->indep_hp = !!val;
157 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
159 spec->add_stereo_mix_input = !!val;
160 /* the following two are just for compatibility */
161 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
163 spec->add_jack_modes = !!val;
164 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
166 spec->add_jack_modes = !!val;
167 val = snd_hda_get_bool_hint(codec, "add_jack_modes");
169 spec->add_jack_modes = !!val;
170 val = snd_hda_get_bool_hint(codec, "power_down_unused");
172 spec->power_down_unused = !!val;
173 val = snd_hda_get_bool_hint(codec, "add_hp_mic");
175 spec->hp_mic = !!val;
176 val = snd_hda_get_bool_hint(codec, "hp_mic_detect");
178 spec->suppress_hp_mic_detect = !val;
180 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
181 spec->mixer_nid = val;
185 * pin control value accesses
188 #define update_pin_ctl(codec, pin, val) \
189 snd_hda_codec_update_cache(codec, pin, 0, \
190 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
192 /* restore the pinctl based on the cached value */
193 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
195 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
198 /* set the pinctl target value and write it if requested */
199 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
200 unsigned int val, bool do_write)
204 val = snd_hda_correct_pin_ctl(codec, pin, val);
205 snd_hda_codec_set_pin_target(codec, pin, val);
207 update_pin_ctl(codec, pin, val);
210 /* set pinctl target values for all given pins */
211 static void set_pin_targets(struct hda_codec *codec, int num_pins,
212 hda_nid_t *pins, unsigned int val)
215 for (i = 0; i < num_pins; i++)
216 set_pin_target(codec, pins[i], val, false);
223 /* return the position of NID in the list, or -1 if not found */
224 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
227 for (i = 0; i < nums; i++)
233 /* return true if the given NID is contained in the path */
234 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
236 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
239 static struct nid_path *get_nid_path(struct hda_codec *codec,
240 hda_nid_t from_nid, hda_nid_t to_nid,
243 struct hda_gen_spec *spec = codec->spec;
246 for (i = 0; i < spec->paths.used; i++) {
247 struct nid_path *path = snd_array_elem(&spec->paths, i);
248 if (path->depth <= 0)
250 if ((!from_nid || path->path[0] == from_nid) &&
251 (!to_nid || path->path[path->depth - 1] == to_nid)) {
253 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
254 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
261 /* get the path between the given NIDs;
262 * passing 0 to either @pin or @dac behaves as a wildcard
264 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
265 hda_nid_t from_nid, hda_nid_t to_nid)
267 return get_nid_path(codec, from_nid, to_nid, 0);
269 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
271 /* get the index number corresponding to the path instance;
272 * the index starts from 1, for easier checking the invalid value
274 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
276 struct hda_gen_spec *spec = codec->spec;
277 struct nid_path *array = spec->paths.list;
280 if (!spec->paths.used)
283 if (idx < 0 || idx >= spec->paths.used)
287 EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
289 /* get the path instance corresponding to the given index number */
290 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
292 struct hda_gen_spec *spec = codec->spec;
294 if (idx <= 0 || idx > spec->paths.used)
296 return snd_array_elem(&spec->paths, idx - 1);
298 EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
300 /* check whether the given DAC is already found in any existing paths */
301 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
303 struct hda_gen_spec *spec = codec->spec;
306 for (i = 0; i < spec->paths.used; i++) {
307 struct nid_path *path = snd_array_elem(&spec->paths, i);
308 if (path->path[0] == nid)
314 /* check whether the given two widgets can be connected */
315 static bool is_reachable_path(struct hda_codec *codec,
316 hda_nid_t from_nid, hda_nid_t to_nid)
318 if (!from_nid || !to_nid)
320 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
323 /* nid, dir and idx */
324 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
326 /* check whether the given ctl is already assigned in any path elements */
327 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
329 struct hda_gen_spec *spec = codec->spec;
332 val &= AMP_VAL_COMPARE_MASK;
333 for (i = 0; i < spec->paths.used; i++) {
334 struct nid_path *path = snd_array_elem(&spec->paths, i);
335 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
341 /* check whether a control with the given (nid, dir, idx) was assigned */
342 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
343 int dir, int idx, int type)
345 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
346 return is_ctl_used(codec, val, type);
349 static void print_nid_path(const char *pfx, struct nid_path *path)
356 for (i = 0; i < path->depth; i++) {
358 sprintf(tmp, ":%02x", path->path[i]);
359 strlcat(buf, tmp, sizeof(buf));
361 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
364 /* called recursively */
365 static bool __parse_nid_path(struct hda_codec *codec,
366 hda_nid_t from_nid, hda_nid_t to_nid,
367 int anchor_nid, struct nid_path *path,
370 const hda_nid_t *conn;
373 if (to_nid == anchor_nid)
374 anchor_nid = 0; /* anchor passed */
375 else if (to_nid == (hda_nid_t)(-anchor_nid))
376 return false; /* hit the exclusive nid */
378 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
379 for (i = 0; i < nums; i++) {
380 if (conn[i] != from_nid) {
381 /* special case: when from_nid is 0,
382 * try to find an empty DAC
385 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
386 is_dac_already_used(codec, conn[i]))
389 /* anchor is not requested or already passed? */
393 if (depth >= MAX_NID_PATH_DEPTH)
395 for (i = 0; i < nums; i++) {
397 type = get_wcaps_type(get_wcaps(codec, conn[i]));
398 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
401 if (__parse_nid_path(codec, from_nid, conn[i],
402 anchor_nid, path, depth + 1))
408 path->path[path->depth] = conn[i];
409 path->idx[path->depth + 1] = i;
410 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
411 path->multi[path->depth + 1] = 1;
416 /* parse the widget path from the given nid to the target nid;
417 * when @from_nid is 0, try to find an empty DAC;
418 * when @anchor_nid is set to a positive value, only paths through the widget
419 * with the given value are evaluated.
420 * when @anchor_nid is set to a negative value, paths through the widget
421 * with the negative of given value are excluded, only other paths are chosen.
422 * when @anchor_nid is zero, no special handling about path selection.
424 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
425 hda_nid_t to_nid, int anchor_nid,
426 struct nid_path *path)
428 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
429 path->path[path->depth] = to_nid;
435 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
438 * parse the path between the given NIDs and add to the path list.
439 * if no valid path is found, return NULL
442 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
443 hda_nid_t to_nid, int anchor_nid)
445 struct hda_gen_spec *spec = codec->spec;
446 struct nid_path *path;
448 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
451 /* check whether the path has been already added */
452 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
456 path = snd_array_new(&spec->paths);
459 memset(path, 0, sizeof(*path));
460 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
466 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
468 /* clear the given path as invalid so that it won't be picked up later */
469 static void invalidate_nid_path(struct hda_codec *codec, int idx)
471 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
474 memset(path, 0, sizeof(*path));
477 /* look for an empty DAC slot */
478 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
481 struct hda_gen_spec *spec = codec->spec;
485 for (i = 0; i < spec->num_all_dacs; i++) {
486 hda_nid_t nid = spec->all_dacs[i];
487 if (!nid || is_dac_already_used(codec, nid))
489 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
490 if (is_digital != cap_digital)
492 if (is_reachable_path(codec, nid, pin))
498 /* replace the channels in the composed amp value with the given number */
499 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
501 val &= ~(0x3U << 16);
506 /* check whether the widget has the given amp capability for the direction */
507 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
508 int dir, unsigned int bits)
512 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
513 if (query_amp_caps(codec, nid, dir) & bits)
518 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
519 hda_nid_t nid2, int dir)
521 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
522 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
523 return (query_amp_caps(codec, nid1, dir) ==
524 query_amp_caps(codec, nid2, dir));
527 #define nid_has_mute(codec, nid, dir) \
528 check_amp_caps(codec, nid, dir, (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE))
529 #define nid_has_volume(codec, nid, dir) \
530 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
532 /* look for a widget suitable for assigning a mute switch in the path */
533 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
534 struct nid_path *path)
538 for (i = path->depth - 1; i >= 0; i--) {
539 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
540 return path->path[i];
541 if (i != path->depth - 1 && i != 0 &&
542 nid_has_mute(codec, path->path[i], HDA_INPUT))
543 return path->path[i];
548 /* look for a widget suitable for assigning a volume ctl in the path */
549 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
550 struct nid_path *path)
554 for (i = path->depth - 1; i >= 0; i--) {
555 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
556 return path->path[i];
562 * path activation / deactivation
565 /* can have the amp-in capability? */
566 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
568 hda_nid_t nid = path->path[idx];
569 unsigned int caps = get_wcaps(codec, nid);
570 unsigned int type = get_wcaps_type(caps);
572 if (!(caps & AC_WCAP_IN_AMP))
574 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
579 /* can have the amp-out capability? */
580 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
582 hda_nid_t nid = path->path[idx];
583 unsigned int caps = get_wcaps(codec, nid);
584 unsigned int type = get_wcaps_type(caps);
586 if (!(caps & AC_WCAP_OUT_AMP))
588 if (type == AC_WID_PIN && !idx) /* only for output pins */
593 /* check whether the given (nid,dir,idx) is active */
594 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
595 unsigned int dir, unsigned int idx)
597 struct hda_gen_spec *spec = codec->spec;
600 for (n = 0; n < spec->paths.used; n++) {
601 struct nid_path *path = snd_array_elem(&spec->paths, n);
604 for (i = 0; i < path->depth; i++) {
605 if (path->path[i] == nid) {
606 if (dir == HDA_OUTPUT || path->idx[i] == idx)
615 /* check whether the NID is referred by any active paths */
616 #define is_active_nid_for_any(codec, nid) \
617 is_active_nid(codec, nid, HDA_OUTPUT, 0)
619 /* get the default amp value for the target state */
620 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
621 int dir, unsigned int caps, bool enable)
623 unsigned int val = 0;
625 if (caps & AC_AMPCAP_NUM_STEPS) {
628 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
630 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
637 /* initialize the amp value (only at the first time) */
638 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
640 unsigned int caps = query_amp_caps(codec, nid, dir);
641 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
642 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
645 /* calculate amp value mask we can modify;
646 * if the given amp is controlled by mixers, don't touch it
648 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
649 hda_nid_t nid, int dir, int idx,
652 unsigned int mask = 0xff;
654 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) {
655 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
658 if (caps & AC_AMPCAP_NUM_STEPS) {
659 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
660 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
666 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
667 int idx, int idx_to_check, bool enable)
670 unsigned int mask, val;
672 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
675 caps = query_amp_caps(codec, nid, dir);
676 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
677 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
682 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
685 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
688 hda_nid_t nid = path->path[i];
689 init_amp(codec, nid, HDA_OUTPUT, 0);
690 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
693 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
694 int i, bool enable, bool add_aamix)
696 struct hda_gen_spec *spec = codec->spec;
697 const hda_nid_t *conn;
700 hda_nid_t nid = path->path[i];
702 nums = snd_hda_get_conn_list(codec, nid, &conn);
703 type = get_wcaps_type(get_wcaps(codec, nid));
704 if (type == AC_WID_PIN ||
705 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
711 for (n = 0; n < nums; n++)
712 init_amp(codec, nid, HDA_INPUT, n);
714 /* here is a little bit tricky in comparison with activate_amp_out();
715 * when aa-mixer is available, we need to enable the path as well
717 for (n = 0; n < nums; n++) {
718 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
720 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
724 /* activate or deactivate the given path
725 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
727 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
728 bool enable, bool add_aamix)
730 struct hda_gen_spec *spec = codec->spec;
734 path->active = false;
736 for (i = path->depth - 1; i >= 0; i--) {
737 hda_nid_t nid = path->path[i];
738 if (enable && spec->power_down_unused) {
739 /* make sure the widget is powered up */
740 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
741 snd_hda_codec_write(codec, nid, 0,
742 AC_VERB_SET_POWER_STATE,
745 if (enable && path->multi[i])
746 snd_hda_codec_write_cache(codec, nid, 0,
747 AC_VERB_SET_CONNECT_SEL,
749 if (has_amp_in(codec, path, i))
750 activate_amp_in(codec, path, i, enable, add_aamix);
751 if (has_amp_out(codec, path, i))
752 activate_amp_out(codec, path, i, enable);
758 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
760 /* if the given path is inactive, put widgets into D3 (only if suitable) */
761 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
763 struct hda_gen_spec *spec = codec->spec;
764 bool changed = false;
767 if (!spec->power_down_unused || path->active)
770 for (i = 0; i < path->depth; i++) {
771 hda_nid_t nid = path->path[i];
772 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) &&
773 !is_active_nid_for_any(codec, nid)) {
774 snd_hda_codec_write(codec, nid, 0,
775 AC_VERB_SET_POWER_STATE,
783 snd_hda_codec_read(codec, path->path[0], 0,
784 AC_VERB_GET_POWER_STATE, 0);
788 /* turn on/off EAPD on the given pin */
789 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
791 struct hda_gen_spec *spec = codec->spec;
792 if (spec->own_eapd_ctl ||
793 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
797 if (spec->keep_eapd_on && !enable)
799 snd_hda_codec_update_cache(codec, pin, 0,
800 AC_VERB_SET_EAPD_BTLENABLE,
801 enable ? 0x02 : 0x00);
804 /* re-initialize the path specified by the given path index */
805 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
807 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
809 snd_hda_activate_path(codec, path, path->active, false);
814 * Helper functions for creating mixer ctl elements
817 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
818 struct snd_ctl_elem_value *ucontrol);
819 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
820 struct snd_ctl_elem_value *ucontrol);
827 static const struct snd_kcontrol_new control_templates[] = {
828 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
829 /* only the put callback is replaced for handling the special mute */
831 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
832 .subdevice = HDA_SUBDEV_AMP_FLAG,
833 .info = snd_hda_mixer_amp_switch_info,
834 .get = snd_hda_mixer_amp_switch_get,
835 .put = hda_gen_mixer_mute_put, /* replaced */
836 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
839 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
840 .info = snd_hda_mixer_amp_switch_info,
841 .get = snd_hda_mixer_bind_switch_get,
842 .put = hda_gen_bind_mute_put, /* replaced */
843 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
847 /* add dynamic controls from template */
848 static struct snd_kcontrol_new *
849 add_control(struct hda_gen_spec *spec, int type, const char *name,
850 int cidx, unsigned long val)
852 struct snd_kcontrol_new *knew;
854 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
858 if (get_amp_nid_(val))
859 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
860 knew->private_value = val;
864 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
865 const char *pfx, const char *dir,
866 const char *sfx, int cidx, unsigned long val)
868 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
869 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
870 if (!add_control(spec, type, name, cidx, val))
875 #define add_pb_vol_ctrl(spec, type, pfx, val) \
876 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
877 #define add_pb_sw_ctrl(spec, type, pfx, val) \
878 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
879 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
880 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
881 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
882 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
884 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
885 unsigned int chs, struct nid_path *path)
890 val = path->ctls[NID_PATH_VOL_CTL];
893 val = amp_val_replace_channels(val, chs);
894 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
897 /* return the channel bits suitable for the given path->ctls[] */
898 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
901 int chs = 1; /* mono (left only) */
903 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
904 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
905 chs = 3; /* stereo */
910 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
911 struct nid_path *path)
913 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
914 return add_vol_ctl(codec, pfx, cidx, chs, path);
917 /* create a mute-switch for the given mixer widget;
918 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
920 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
921 unsigned int chs, struct nid_path *path)
924 int type = HDA_CTL_WIDGET_MUTE;
928 val = path->ctls[NID_PATH_MUTE_CTL];
931 val = amp_val_replace_channels(val, chs);
932 if (get_amp_direction_(val) == HDA_INPUT) {
933 hda_nid_t nid = get_amp_nid_(val);
934 int nums = snd_hda_get_num_conns(codec, nid);
936 type = HDA_CTL_BIND_MUTE;
940 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
943 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
944 int cidx, struct nid_path *path)
946 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
947 return add_sw_ctl(codec, pfx, cidx, chs, path);
950 /* playback mute control with the software mute bit check */
951 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol,
952 struct snd_ctl_elem_value *ucontrol)
954 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
955 struct hda_gen_spec *spec = codec->spec;
957 if (spec->auto_mute_via_amp) {
958 hda_nid_t nid = get_amp_nid(kcontrol);
959 bool enabled = !((spec->mute_bits >> nid) & 1);
960 ucontrol->value.integer.value[0] &= enabled;
961 ucontrol->value.integer.value[1] &= enabled;
965 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol,
966 struct snd_ctl_elem_value *ucontrol)
968 sync_auto_mute_bits(kcontrol, ucontrol);
969 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
972 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol,
973 struct snd_ctl_elem_value *ucontrol)
975 sync_auto_mute_bits(kcontrol, ucontrol);
976 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol);
979 /* any ctl assigned to the path with the given index? */
980 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
982 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
983 return path && path->ctls[ctl_type];
986 static const char * const channel_name[4] = {
987 "Front", "Surround", "CLFE", "Side"
990 /* give some appropriate ctl name prefix for the given line out channel */
991 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
992 int *index, int ctl_type)
994 struct hda_gen_spec *spec = codec->spec;
995 struct auto_pin_cfg *cfg = &spec->autocfg;
998 if (cfg->line_outs == 1 && !spec->multi_ios &&
999 !cfg->hp_outs && !cfg->speaker_outs)
1000 return spec->vmaster_mute.hook ? "PCM" : "Master";
1002 /* if there is really a single DAC used in the whole output paths,
1003 * use it master (or "PCM" if a vmaster hook is present)
1005 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
1006 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
1007 return spec->vmaster_mute.hook ? "PCM" : "Master";
1009 /* multi-io channels */
1010 if (ch >= cfg->line_outs)
1011 return channel_name[ch];
1013 switch (cfg->line_out_type) {
1014 case AUTO_PIN_SPEAKER_OUT:
1015 /* if the primary channel vol/mute is shared with HP volume,
1016 * don't name it as Speaker
1018 if (!ch && cfg->hp_outs &&
1019 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
1021 if (cfg->line_outs == 1)
1023 if (cfg->line_outs == 2)
1024 return ch ? "Bass Speaker" : "Speaker";
1026 case AUTO_PIN_HP_OUT:
1027 /* if the primary channel vol/mute is shared with spk volume,
1028 * don't name it as Headphone
1030 if (!ch && cfg->speaker_outs &&
1031 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
1033 /* for multi-io case, only the primary out */
1034 if (ch && spec->multi_ios)
1040 /* for a single channel output, we don't have to name the channel */
1041 if (cfg->line_outs == 1 && !spec->multi_ios)
1044 if (ch >= ARRAY_SIZE(channel_name)) {
1049 return channel_name[ch];
1053 * Parse output paths
1056 /* badness definition */
1058 /* No primary DAC is found for the main output */
1059 BAD_NO_PRIMARY_DAC = 0x10000,
1060 /* No DAC is found for the extra output */
1061 BAD_NO_DAC = 0x4000,
1062 /* No possible multi-ios */
1063 BAD_MULTI_IO = 0x120,
1064 /* No individual DAC for extra output */
1065 BAD_NO_EXTRA_DAC = 0x102,
1066 /* No individual DAC for extra surrounds */
1067 BAD_NO_EXTRA_SURR_DAC = 0x101,
1068 /* Primary DAC shared with main surrounds */
1069 BAD_SHARED_SURROUND = 0x100,
1070 /* No independent HP possible */
1071 BAD_NO_INDEP_HP = 0x10,
1072 /* Primary DAC shared with main CLFE */
1073 BAD_SHARED_CLFE = 0x10,
1074 /* Primary DAC shared with extra surrounds */
1075 BAD_SHARED_EXTRA_SURROUND = 0x10,
1076 /* Volume widget is shared */
1077 BAD_SHARED_VOL = 0x10,
1080 /* look for widgets in the given path which are appropriate for
1081 * volume and mute controls, and assign the values to ctls[].
1083 * When no appropriate widget is found in the path, the badness value
1084 * is incremented depending on the situation. The function returns the
1085 * total badness for both volume and mute controls.
1087 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1094 return BAD_SHARED_VOL * 2;
1096 if (path->ctls[NID_PATH_VOL_CTL] ||
1097 path->ctls[NID_PATH_MUTE_CTL])
1098 return 0; /* already evaluated */
1100 nid = look_for_out_vol_nid(codec, path);
1102 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1103 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1104 badness += BAD_SHARED_VOL;
1106 path->ctls[NID_PATH_VOL_CTL] = val;
1108 badness += BAD_SHARED_VOL;
1109 nid = look_for_out_mute_nid(codec, path);
1111 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1112 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1113 nid_has_mute(codec, nid, HDA_OUTPUT))
1114 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1116 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1117 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1118 badness += BAD_SHARED_VOL;
1120 path->ctls[NID_PATH_MUTE_CTL] = val;
1122 badness += BAD_SHARED_VOL;
1126 const struct badness_table hda_main_out_badness = {
1127 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1128 .no_dac = BAD_NO_DAC,
1129 .shared_primary = BAD_NO_PRIMARY_DAC,
1130 .shared_surr = BAD_SHARED_SURROUND,
1131 .shared_clfe = BAD_SHARED_CLFE,
1132 .shared_surr_main = BAD_SHARED_SURROUND,
1134 EXPORT_SYMBOL_HDA(hda_main_out_badness);
1136 const struct badness_table hda_extra_out_badness = {
1137 .no_primary_dac = BAD_NO_DAC,
1138 .no_dac = BAD_NO_DAC,
1139 .shared_primary = BAD_NO_EXTRA_DAC,
1140 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1141 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1142 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1144 EXPORT_SYMBOL_HDA(hda_extra_out_badness);
1146 /* get the DAC of the primary output corresponding to the given array index */
1147 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1149 struct hda_gen_spec *spec = codec->spec;
1150 struct auto_pin_cfg *cfg = &spec->autocfg;
1152 if (cfg->line_outs > idx)
1153 return spec->private_dac_nids[idx];
1154 idx -= cfg->line_outs;
1155 if (spec->multi_ios > idx)
1156 return spec->multi_io[idx].dac;
1160 /* return the DAC if it's reachable, otherwise zero */
1161 static inline hda_nid_t try_dac(struct hda_codec *codec,
1162 hda_nid_t dac, hda_nid_t pin)
1164 return is_reachable_path(codec, dac, pin) ? dac : 0;
1167 /* try to assign DACs to pins and return the resultant badness */
1168 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1169 const hda_nid_t *pins, hda_nid_t *dacs,
1171 const struct badness_table *bad)
1173 struct hda_gen_spec *spec = codec->spec;
1181 for (i = 0; i < num_outs; i++) {
1182 struct nid_path *path;
1183 hda_nid_t pin = pins[i];
1185 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1187 badness += assign_out_path_ctls(codec, path);
1191 dacs[i] = look_for_dac(codec, pin, false);
1192 if (!dacs[i] && !i) {
1193 /* try to steal the DAC of surrounds for the front */
1194 for (j = 1; j < num_outs; j++) {
1195 if (is_reachable_path(codec, dacs[j], pin)) {
1198 invalidate_nid_path(codec, path_idx[j]);
1207 dac = try_dac(codec, get_primary_out(codec, i), pin);
1209 dac = try_dac(codec, dacs[0], pin);
1211 dac = try_dac(codec, get_primary_out(codec, i), pin);
1214 badness += bad->shared_primary;
1216 badness += bad->shared_surr;
1218 badness += bad->shared_clfe;
1219 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1220 dac = spec->private_dac_nids[0];
1221 badness += bad->shared_surr_main;
1223 badness += bad->no_primary_dac;
1225 badness += bad->no_dac;
1229 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1230 if (!path && !i && spec->mixer_nid) {
1231 /* try with aamix */
1232 path = snd_hda_add_new_path(codec, dac, pin, 0);
1236 badness += bad->no_dac;
1238 /* print_nid_path("output", path); */
1239 path->active = true;
1240 path_idx[i] = snd_hda_get_path_idx(codec, path);
1241 badness += assign_out_path_ctls(codec, path);
1248 /* return NID if the given pin has only a single connection to a certain DAC */
1249 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1251 struct hda_gen_spec *spec = codec->spec;
1253 hda_nid_t nid_found = 0;
1255 for (i = 0; i < spec->num_all_dacs; i++) {
1256 hda_nid_t nid = spec->all_dacs[i];
1257 if (!nid || is_dac_already_used(codec, nid))
1259 if (is_reachable_path(codec, nid, pin)) {
1268 /* check whether the given pin can be a multi-io pin */
1269 static bool can_be_multiio_pin(struct hda_codec *codec,
1270 unsigned int location, hda_nid_t nid)
1272 unsigned int defcfg, caps;
1274 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1275 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1277 if (location && get_defcfg_location(defcfg) != location)
1279 caps = snd_hda_query_pin_caps(codec, nid);
1280 if (!(caps & AC_PINCAP_OUT))
1285 /* count the number of input pins that are capable to be multi-io */
1286 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1288 struct hda_gen_spec *spec = codec->spec;
1289 struct auto_pin_cfg *cfg = &spec->autocfg;
1290 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1291 unsigned int location = get_defcfg_location(defcfg);
1295 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1296 for (i = 0; i < cfg->num_inputs; i++) {
1297 if (cfg->inputs[i].type != type)
1299 if (can_be_multiio_pin(codec, location,
1300 cfg->inputs[i].pin))
1310 * When hardwired is set, try to fill ony hardwired pins, and returns
1311 * zero if any pins are filled, non-zero if nothing found.
1312 * When hardwired is off, try to fill possible input pins, and returns
1313 * the badness value.
1315 static int fill_multi_ios(struct hda_codec *codec,
1316 hda_nid_t reference_pin,
1319 struct hda_gen_spec *spec = codec->spec;
1320 struct auto_pin_cfg *cfg = &spec->autocfg;
1321 int type, i, j, num_pins, old_pins;
1322 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1323 unsigned int location = get_defcfg_location(defcfg);
1325 struct nid_path *path;
1327 old_pins = spec->multi_ios;
1331 num_pins = count_multiio_pins(codec, reference_pin);
1335 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1336 for (i = 0; i < cfg->num_inputs; i++) {
1337 hda_nid_t nid = cfg->inputs[i].pin;
1340 if (cfg->inputs[i].type != type)
1342 if (!can_be_multiio_pin(codec, location, nid))
1344 for (j = 0; j < spec->multi_ios; j++) {
1345 if (nid == spec->multi_io[j].pin)
1348 if (j < spec->multi_ios)
1352 dac = get_dac_if_single(codec, nid);
1354 dac = look_for_dac(codec, nid, false);
1359 path = snd_hda_add_new_path(codec, dac, nid,
1365 /* print_nid_path("multiio", path); */
1366 spec->multi_io[spec->multi_ios].pin = nid;
1367 spec->multi_io[spec->multi_ios].dac = dac;
1368 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1369 snd_hda_get_path_idx(codec, path);
1371 if (spec->multi_ios >= 2)
1377 badness = BAD_MULTI_IO;
1378 if (old_pins == spec->multi_ios) {
1380 return 1; /* nothing found */
1382 return badness; /* no badness if nothing found */
1384 if (!hardwired && spec->multi_ios < 2) {
1385 /* cancel newly assigned paths */
1386 spec->paths.used -= spec->multi_ios - old_pins;
1387 spec->multi_ios = old_pins;
1391 /* assign volume and mute controls */
1392 for (i = old_pins; i < spec->multi_ios; i++) {
1393 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1394 badness += assign_out_path_ctls(codec, path);
1400 /* map DACs for all pins in the list if they are single connections */
1401 static bool map_singles(struct hda_codec *codec, int outs,
1402 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1404 struct hda_gen_spec *spec = codec->spec;
1407 for (i = 0; i < outs; i++) {
1408 struct nid_path *path;
1412 dac = get_dac_if_single(codec, pins[i]);
1415 path = snd_hda_add_new_path(codec, dac, pins[i],
1417 if (!path && !i && spec->mixer_nid)
1418 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1422 /* print_nid_path("output", path); */
1423 path->active = true;
1424 path_idx[i] = snd_hda_get_path_idx(codec, path);
1430 /* create a new path including aamix if available, and return its index */
1431 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1433 struct hda_gen_spec *spec = codec->spec;
1434 struct nid_path *path;
1435 hda_nid_t path_dac, dac, pin;
1437 path = snd_hda_get_path_from_idx(codec, path_idx);
1438 if (!path || !path->depth ||
1439 is_nid_contained(path, spec->mixer_nid))
1441 path_dac = path->path[0];
1442 dac = spec->private_dac_nids[0];
1443 pin = path->path[path->depth - 1];
1444 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1446 if (dac != path_dac)
1448 else if (spec->multiout.hp_out_nid[0])
1449 dac = spec->multiout.hp_out_nid[0];
1450 else if (spec->multiout.extra_out_nid[0])
1451 dac = spec->multiout.extra_out_nid[0];
1455 path = snd_hda_add_new_path(codec, dac, pin,
1460 /* print_nid_path("output-aamix", path); */
1461 path->active = false; /* unused as default */
1462 return snd_hda_get_path_idx(codec, path);
1465 /* check whether the independent HP is available with the current config */
1466 static bool indep_hp_possible(struct hda_codec *codec)
1468 struct hda_gen_spec *spec = codec->spec;
1469 struct auto_pin_cfg *cfg = &spec->autocfg;
1470 struct nid_path *path;
1473 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1474 idx = spec->out_paths[0];
1476 idx = spec->hp_paths[0];
1477 path = snd_hda_get_path_from_idx(codec, idx);
1481 /* assume no path conflicts unless aamix is involved */
1482 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1485 /* check whether output paths contain aamix */
1486 for (i = 0; i < cfg->line_outs; i++) {
1487 if (spec->out_paths[i] == idx)
1489 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1490 if (path && is_nid_contained(path, spec->mixer_nid))
1493 for (i = 0; i < cfg->speaker_outs; i++) {
1494 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1495 if (path && is_nid_contained(path, spec->mixer_nid))
1502 /* fill the empty entries in the dac array for speaker/hp with the
1503 * shared dac pointed by the paths
1505 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1506 hda_nid_t *dacs, int *path_idx)
1508 struct nid_path *path;
1511 for (i = 0; i < num_outs; i++) {
1514 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1517 dacs[i] = path->path[0];
1521 /* fill in the dac_nids table from the parsed pin configuration */
1522 static int fill_and_eval_dacs(struct hda_codec *codec,
1523 bool fill_hardwired,
1524 bool fill_mio_first)
1526 struct hda_gen_spec *spec = codec->spec;
1527 struct auto_pin_cfg *cfg = &spec->autocfg;
1528 int i, err, badness;
1530 /* set num_dacs once to full for look_for_dac() */
1531 spec->multiout.num_dacs = cfg->line_outs;
1532 spec->multiout.dac_nids = spec->private_dac_nids;
1533 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1534 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1535 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1536 spec->multi_ios = 0;
1537 snd_array_free(&spec->paths);
1539 /* clear path indices */
1540 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1541 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1542 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1543 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1544 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1545 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1546 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1547 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1551 /* fill hard-wired DACs first */
1552 if (fill_hardwired) {
1555 mapped = map_singles(codec, cfg->line_outs,
1557 spec->private_dac_nids,
1559 mapped |= map_singles(codec, cfg->hp_outs,
1561 spec->multiout.hp_out_nid,
1563 mapped |= map_singles(codec, cfg->speaker_outs,
1565 spec->multiout.extra_out_nid,
1566 spec->speaker_paths);
1567 if (!spec->no_multi_io &&
1568 fill_mio_first && cfg->line_outs == 1 &&
1569 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1570 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1577 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1578 spec->private_dac_nids, spec->out_paths,
1579 spec->main_out_badness);
1581 if (!spec->no_multi_io && fill_mio_first &&
1582 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1583 /* try to fill multi-io first */
1584 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1587 /* we don't count badness at this stage yet */
1590 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1591 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1592 spec->multiout.hp_out_nid,
1594 spec->extra_out_badness);
1599 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1600 err = try_assign_dacs(codec, cfg->speaker_outs,
1602 spec->multiout.extra_out_nid,
1603 spec->speaker_paths,
1604 spec->extra_out_badness);
1609 if (!spec->no_multi_io &&
1610 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1611 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1617 if (spec->mixer_nid) {
1618 spec->aamix_out_paths[0] =
1619 check_aamix_out_path(codec, spec->out_paths[0]);
1620 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1621 spec->aamix_out_paths[1] =
1622 check_aamix_out_path(codec, spec->hp_paths[0]);
1623 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1624 spec->aamix_out_paths[2] =
1625 check_aamix_out_path(codec, spec->speaker_paths[0]);
1628 if (!spec->no_multi_io &&
1629 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1630 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1631 spec->multi_ios = 1; /* give badness */
1633 /* re-count num_dacs and squash invalid entries */
1634 spec->multiout.num_dacs = 0;
1635 for (i = 0; i < cfg->line_outs; i++) {
1636 if (spec->private_dac_nids[i])
1637 spec->multiout.num_dacs++;
1639 memmove(spec->private_dac_nids + i,
1640 spec->private_dac_nids + i + 1,
1641 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1642 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1646 spec->ext_channel_count = spec->min_channel_count =
1647 spec->multiout.num_dacs * 2;
1649 if (spec->multi_ios == 2) {
1650 for (i = 0; i < 2; i++)
1651 spec->private_dac_nids[spec->multiout.num_dacs++] =
1652 spec->multi_io[i].dac;
1653 } else if (spec->multi_ios) {
1654 spec->multi_ios = 0;
1655 badness += BAD_MULTI_IO;
1658 if (spec->indep_hp && !indep_hp_possible(codec))
1659 badness += BAD_NO_INDEP_HP;
1661 /* re-fill the shared DAC for speaker / headphone */
1662 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1663 refill_shared_dacs(codec, cfg->hp_outs,
1664 spec->multiout.hp_out_nid,
1666 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1667 refill_shared_dacs(codec, cfg->speaker_outs,
1668 spec->multiout.extra_out_nid,
1669 spec->speaker_paths);
1674 #define DEBUG_BADNESS
1676 #ifdef DEBUG_BADNESS
1677 #define debug_badness snd_printdd
1679 #define debug_badness(...)
1682 #ifdef DEBUG_BADNESS
1683 static inline void print_nid_path_idx(struct hda_codec *codec,
1684 const char *pfx, int idx)
1686 struct nid_path *path;
1688 path = snd_hda_get_path_from_idx(codec, idx);
1690 print_nid_path(pfx, path);
1693 static void debug_show_configs(struct hda_codec *codec,
1694 struct auto_pin_cfg *cfg)
1696 struct hda_gen_spec *spec = codec->spec;
1697 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1700 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1701 cfg->line_out_pins[0], cfg->line_out_pins[1],
1702 cfg->line_out_pins[2], cfg->line_out_pins[3],
1703 spec->multiout.dac_nids[0],
1704 spec->multiout.dac_nids[1],
1705 spec->multiout.dac_nids[2],
1706 spec->multiout.dac_nids[3],
1707 lo_type[cfg->line_out_type]);
1708 for (i = 0; i < cfg->line_outs; i++)
1709 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1710 if (spec->multi_ios > 0)
1711 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1713 spec->multi_io[0].pin, spec->multi_io[1].pin,
1714 spec->multi_io[0].dac, spec->multi_io[1].dac);
1715 for (i = 0; i < spec->multi_ios; i++)
1716 print_nid_path_idx(codec, " mio",
1717 spec->out_paths[cfg->line_outs + i]);
1719 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1720 cfg->hp_pins[0], cfg->hp_pins[1],
1721 cfg->hp_pins[2], cfg->hp_pins[3],
1722 spec->multiout.hp_out_nid[0],
1723 spec->multiout.hp_out_nid[1],
1724 spec->multiout.hp_out_nid[2],
1725 spec->multiout.hp_out_nid[3]);
1726 for (i = 0; i < cfg->hp_outs; i++)
1727 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1728 if (cfg->speaker_outs)
1729 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1730 cfg->speaker_pins[0], cfg->speaker_pins[1],
1731 cfg->speaker_pins[2], cfg->speaker_pins[3],
1732 spec->multiout.extra_out_nid[0],
1733 spec->multiout.extra_out_nid[1],
1734 spec->multiout.extra_out_nid[2],
1735 spec->multiout.extra_out_nid[3]);
1736 for (i = 0; i < cfg->speaker_outs; i++)
1737 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1738 for (i = 0; i < 3; i++)
1739 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1742 #define debug_show_configs(codec, cfg) /* NOP */
1745 /* find all available DACs of the codec */
1746 static void fill_all_dac_nids(struct hda_codec *codec)
1748 struct hda_gen_spec *spec = codec->spec;
1750 hda_nid_t nid = codec->start_nid;
1752 spec->num_all_dacs = 0;
1753 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1754 for (i = 0; i < codec->num_nodes; i++, nid++) {
1755 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1757 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1758 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1761 spec->all_dacs[spec->num_all_dacs++] = nid;
1765 static int parse_output_paths(struct hda_codec *codec)
1767 struct hda_gen_spec *spec = codec->spec;
1768 struct auto_pin_cfg *cfg = &spec->autocfg;
1769 struct auto_pin_cfg *best_cfg;
1771 int best_badness = INT_MAX;
1773 bool fill_hardwired = true, fill_mio_first = true;
1774 bool best_wired = true, best_mio = true;
1775 bool hp_spk_swapped = false;
1777 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1783 badness = fill_and_eval_dacs(codec, fill_hardwired,
1789 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1790 cfg->line_out_type, fill_hardwired, fill_mio_first,
1792 debug_show_configs(codec, cfg);
1793 if (badness < best_badness) {
1794 best_badness = badness;
1796 best_wired = fill_hardwired;
1797 best_mio = fill_mio_first;
1801 fill_mio_first = !fill_mio_first;
1802 if (!fill_mio_first)
1804 fill_hardwired = !fill_hardwired;
1805 if (!fill_hardwired)
1809 hp_spk_swapped = true;
1810 if (cfg->speaker_outs > 0 &&
1811 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1812 cfg->hp_outs = cfg->line_outs;
1813 memcpy(cfg->hp_pins, cfg->line_out_pins,
1814 sizeof(cfg->hp_pins));
1815 cfg->line_outs = cfg->speaker_outs;
1816 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1817 sizeof(cfg->speaker_pins));
1818 cfg->speaker_outs = 0;
1819 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1820 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1821 fill_hardwired = true;
1824 if (cfg->hp_outs > 0 &&
1825 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1826 cfg->speaker_outs = cfg->line_outs;
1827 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1828 sizeof(cfg->speaker_pins));
1829 cfg->line_outs = cfg->hp_outs;
1830 memcpy(cfg->line_out_pins, cfg->hp_pins,
1831 sizeof(cfg->hp_pins));
1833 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1834 cfg->line_out_type = AUTO_PIN_HP_OUT;
1835 fill_hardwired = true;
1842 debug_badness("==> restoring best_cfg\n");
1844 fill_and_eval_dacs(codec, best_wired, best_mio);
1846 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1847 cfg->line_out_type, best_wired, best_mio);
1848 debug_show_configs(codec, cfg);
1850 if (cfg->line_out_pins[0]) {
1851 struct nid_path *path;
1852 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1854 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1855 if (spec->vmaster_nid)
1856 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1857 HDA_OUTPUT, spec->vmaster_tlv);
1860 /* set initial pinctl targets */
1861 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1865 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1866 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1867 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1868 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1869 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1870 set_pin_targets(codec, cfg->speaker_outs,
1871 cfg->speaker_pins, val);
1874 /* clear indep_hp flag if not available */
1875 if (spec->indep_hp && !indep_hp_possible(codec))
1882 /* add playback controls from the parsed DAC table */
1883 static int create_multi_out_ctls(struct hda_codec *codec,
1884 const struct auto_pin_cfg *cfg)
1886 struct hda_gen_spec *spec = codec->spec;
1887 int i, err, noutputs;
1889 noutputs = cfg->line_outs;
1890 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1891 noutputs += spec->multi_ios;
1893 for (i = 0; i < noutputs; i++) {
1896 struct nid_path *path;
1898 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1902 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
1903 if (!name || !strcmp(name, "CLFE")) {
1905 err = add_vol_ctl(codec, "Center", 0, 1, path);
1908 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1912 err = add_stereo_vol(codec, name, index, path);
1917 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1918 if (!name || !strcmp(name, "CLFE")) {
1919 err = add_sw_ctl(codec, "Center", 0, 1, path);
1922 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1926 err = add_stereo_sw(codec, name, index, path);
1934 static int create_extra_out(struct hda_codec *codec, int path_idx,
1935 const char *pfx, int cidx)
1937 struct nid_path *path;
1940 path = snd_hda_get_path_from_idx(codec, path_idx);
1943 err = add_stereo_vol(codec, pfx, cidx, path);
1946 err = add_stereo_sw(codec, pfx, cidx, path);
1952 /* add playback controls for speaker and HP outputs */
1953 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1954 const int *paths, const char *pfx)
1958 for (i = 0; i < num_pins; i++) {
1960 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1963 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1964 name = "Bass Speaker";
1965 else if (num_pins >= 3) {
1966 snprintf(tmp, sizeof(tmp), "%s %s",
1967 pfx, channel_name[i]);
1973 err = create_extra_out(codec, paths[i], name, idx);
1980 static int create_hp_out_ctls(struct hda_codec *codec)
1982 struct hda_gen_spec *spec = codec->spec;
1983 return create_extra_outs(codec, spec->autocfg.hp_outs,
1988 static int create_speaker_out_ctls(struct hda_codec *codec)
1990 struct hda_gen_spec *spec = codec->spec;
1991 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1992 spec->speaker_paths,
1997 * independent HP controls
2000 static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack);
2001 static int indep_hp_info(struct snd_kcontrol *kcontrol,
2002 struct snd_ctl_elem_info *uinfo)
2004 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2007 static int indep_hp_get(struct snd_kcontrol *kcontrol,
2008 struct snd_ctl_elem_value *ucontrol)
2010 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2011 struct hda_gen_spec *spec = codec->spec;
2012 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
2016 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2017 int nomix_path_idx, int mix_path_idx,
2020 static int indep_hp_put(struct snd_kcontrol *kcontrol,
2021 struct snd_ctl_elem_value *ucontrol)
2023 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2024 struct hda_gen_spec *spec = codec->spec;
2025 unsigned int select = ucontrol->value.enumerated.item[0];
2028 mutex_lock(&spec->pcm_mutex);
2029 if (spec->active_streams) {
2034 if (spec->indep_hp_enabled != select) {
2036 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2037 dacp = &spec->private_dac_nids[0];
2039 dacp = &spec->multiout.hp_out_nid[0];
2041 /* update HP aamix paths in case it conflicts with indep HP */
2042 if (spec->have_aamix_ctl) {
2043 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2044 update_aamix_paths(codec, spec->aamix_mode,
2046 spec->aamix_out_paths[0],
2047 spec->autocfg.line_out_type);
2049 update_aamix_paths(codec, spec->aamix_mode,
2051 spec->aamix_out_paths[1],
2055 spec->indep_hp_enabled = select;
2056 if (spec->indep_hp_enabled)
2059 *dacp = spec->alt_dac_nid;
2061 call_hp_automute(codec, NULL);
2065 mutex_unlock(&spec->pcm_mutex);
2069 static const struct snd_kcontrol_new indep_hp_ctl = {
2070 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2071 .name = "Independent HP",
2072 .info = indep_hp_info,
2073 .get = indep_hp_get,
2074 .put = indep_hp_put,
2078 static int create_indep_hp_ctls(struct hda_codec *codec)
2080 struct hda_gen_spec *spec = codec->spec;
2083 if (!spec->indep_hp)
2085 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2086 dac = spec->multiout.dac_nids[0];
2088 dac = spec->multiout.hp_out_nid[0];
2094 spec->indep_hp_enabled = false;
2095 spec->alt_dac_nid = dac;
2096 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2102 * channel mode enum control
2105 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2106 struct snd_ctl_elem_info *uinfo)
2108 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2109 struct hda_gen_spec *spec = codec->spec;
2112 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2114 uinfo->value.enumerated.items = spec->multi_ios + 1;
2115 if (uinfo->value.enumerated.item > spec->multi_ios)
2116 uinfo->value.enumerated.item = spec->multi_ios;
2117 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2118 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2122 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2123 struct snd_ctl_elem_value *ucontrol)
2125 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2126 struct hda_gen_spec *spec = codec->spec;
2127 ucontrol->value.enumerated.item[0] =
2128 (spec->ext_channel_count - spec->min_channel_count) / 2;
2132 static inline struct nid_path *
2133 get_multiio_path(struct hda_codec *codec, int idx)
2135 struct hda_gen_spec *spec = codec->spec;
2136 return snd_hda_get_path_from_idx(codec,
2137 spec->out_paths[spec->autocfg.line_outs + idx]);
2140 static void update_automute_all(struct hda_codec *codec);
2142 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2143 * used for output paths
2145 static bool aamix_default(struct hda_gen_spec *spec)
2147 return !spec->have_aamix_ctl || spec->aamix_mode;
2150 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2152 struct hda_gen_spec *spec = codec->spec;
2153 hda_nid_t nid = spec->multi_io[idx].pin;
2154 struct nid_path *path;
2156 path = get_multiio_path(codec, idx);
2160 if (path->active == output)
2164 set_pin_target(codec, nid, PIN_OUT, true);
2165 snd_hda_activate_path(codec, path, true, aamix_default(spec));
2166 set_pin_eapd(codec, nid, true);
2168 set_pin_eapd(codec, nid, false);
2169 snd_hda_activate_path(codec, path, false, aamix_default(spec));
2170 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2171 path_power_down_sync(codec, path);
2174 /* update jack retasking in case it modifies any of them */
2175 update_automute_all(codec);
2180 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2181 struct snd_ctl_elem_value *ucontrol)
2183 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2184 struct hda_gen_spec *spec = codec->spec;
2187 ch = ucontrol->value.enumerated.item[0];
2188 if (ch < 0 || ch > spec->multi_ios)
2190 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2192 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2193 for (i = 0; i < spec->multi_ios; i++)
2194 set_multi_io(codec, i, i < ch);
2195 spec->multiout.max_channels = max(spec->ext_channel_count,
2196 spec->const_channel_count);
2197 if (spec->need_dac_fix)
2198 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2202 static const struct snd_kcontrol_new channel_mode_enum = {
2203 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2204 .name = "Channel Mode",
2205 .info = ch_mode_info,
2210 static int create_multi_channel_mode(struct hda_codec *codec)
2212 struct hda_gen_spec *spec = codec->spec;
2214 if (spec->multi_ios > 0) {
2215 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2222 * aamix loopback enable/disable switch
2225 #define loopback_mixing_info indep_hp_info
2227 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2228 struct snd_ctl_elem_value *ucontrol)
2230 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2231 struct hda_gen_spec *spec = codec->spec;
2232 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2236 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2237 int nomix_path_idx, int mix_path_idx,
2240 struct hda_gen_spec *spec = codec->spec;
2241 struct nid_path *nomix_path, *mix_path;
2243 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2244 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2245 if (!nomix_path || !mix_path)
2248 /* if HP aamix path is driven from a different DAC and the
2249 * independent HP mode is ON, can't turn on aamix path
2251 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2252 mix_path->path[0] != spec->alt_dac_nid)
2256 snd_hda_activate_path(codec, nomix_path, false, true);
2257 snd_hda_activate_path(codec, mix_path, true, true);
2258 path_power_down_sync(codec, nomix_path);
2260 snd_hda_activate_path(codec, mix_path, false, false);
2261 snd_hda_activate_path(codec, nomix_path, true, false);
2262 path_power_down_sync(codec, mix_path);
2266 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2267 struct snd_ctl_elem_value *ucontrol)
2269 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2270 struct hda_gen_spec *spec = codec->spec;
2271 unsigned int val = ucontrol->value.enumerated.item[0];
2273 if (val == spec->aamix_mode)
2275 spec->aamix_mode = val;
2276 update_aamix_paths(codec, val, spec->out_paths[0],
2277 spec->aamix_out_paths[0],
2278 spec->autocfg.line_out_type);
2279 update_aamix_paths(codec, val, spec->hp_paths[0],
2280 spec->aamix_out_paths[1],
2282 update_aamix_paths(codec, val, spec->speaker_paths[0],
2283 spec->aamix_out_paths[2],
2284 AUTO_PIN_SPEAKER_OUT);
2288 static const struct snd_kcontrol_new loopback_mixing_enum = {
2289 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2290 .name = "Loopback Mixing",
2291 .info = loopback_mixing_info,
2292 .get = loopback_mixing_get,
2293 .put = loopback_mixing_put,
2296 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2298 struct hda_gen_spec *spec = codec->spec;
2300 if (!spec->mixer_nid)
2302 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2303 spec->aamix_out_paths[2]))
2305 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2307 spec->have_aamix_ctl = 1;
2312 * shared headphone/mic handling
2315 static void call_update_outputs(struct hda_codec *codec);
2317 /* for shared I/O, change the pin-control accordingly */
2318 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force)
2320 struct hda_gen_spec *spec = codec->spec;
2325 pin = spec->hp_mic_pin;
2326 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx;
2329 val = snd_hda_codec_get_pin_target(codec, pin);
2339 val = snd_hda_get_default_vref(codec, pin);
2340 /* if the HP pin doesn't support VREF and the codec driver gives an
2341 * alternative pin, set up the VREF on that pin instead
2343 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2344 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2345 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2346 if (vref_val != AC_PINCTL_VREF_HIZ)
2347 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2348 PIN_IN | (as_mic ? vref_val : 0));
2351 if (!spec->hp_mic_jack_modes) {
2356 set_pin_target(codec, pin, val, true);
2357 call_hp_automute(codec, NULL);
2361 /* create a shared input with the headphone out */
2362 static int create_hp_mic(struct hda_codec *codec)
2364 struct hda_gen_spec *spec = codec->spec;
2365 struct auto_pin_cfg *cfg = &spec->autocfg;
2366 unsigned int defcfg;
2369 if (!spec->hp_mic) {
2370 if (spec->suppress_hp_mic_detect)
2372 /* automatic detection: only if no input or a single internal
2373 * input pin is found, try to detect the shared hp/mic
2375 if (cfg->num_inputs > 1)
2377 else if (cfg->num_inputs == 1) {
2378 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2379 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2384 spec->hp_mic = 0; /* clear once */
2385 if (cfg->num_inputs >= AUTO_CFG_MAX_INS)
2389 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0)
2390 nid = cfg->line_out_pins[0];
2391 else if (cfg->hp_outs > 0)
2392 nid = cfg->hp_pins[0];
2396 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2397 return 0; /* no input */
2399 cfg->inputs[cfg->num_inputs].pin = nid;
2400 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC;
2401 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1;
2404 spec->hp_mic_pin = nid;
2405 /* we can't handle auto-mic together with HP-mic */
2406 spec->suppress_auto_mic = 1;
2407 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2415 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin);
2417 static const char * const out_jack_texts[] = {
2418 "Line Out", "Headphone Out",
2421 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2422 struct snd_ctl_elem_info *uinfo)
2424 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts);
2427 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2428 struct snd_ctl_elem_value *ucontrol)
2430 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2431 hda_nid_t nid = kcontrol->private_value;
2432 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2433 ucontrol->value.enumerated.item[0] = 1;
2435 ucontrol->value.enumerated.item[0] = 0;
2439 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2440 struct snd_ctl_elem_value *ucontrol)
2442 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2443 hda_nid_t nid = kcontrol->private_value;
2446 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2447 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2449 snd_hda_set_pin_ctl_cache(codec, nid, val);
2453 static const struct snd_kcontrol_new out_jack_mode_enum = {
2454 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2455 .info = out_jack_mode_info,
2456 .get = out_jack_mode_get,
2457 .put = out_jack_mode_put,
2460 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2462 struct hda_gen_spec *spec = codec->spec;
2465 for (i = 0; i < spec->kctls.used; i++) {
2466 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2467 if (!strcmp(kctl->name, name) && kctl->index == idx)
2473 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2474 char *name, size_t name_len)
2476 struct hda_gen_spec *spec = codec->spec;
2479 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2480 strlcat(name, " Jack Mode", name_len);
2482 for (; find_kctl_name(codec, name, idx); idx++)
2486 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2488 struct hda_gen_spec *spec = codec->spec;
2489 if (spec->add_jack_modes) {
2490 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2491 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV))
2497 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2500 struct hda_gen_spec *spec = codec->spec;
2503 for (i = 0; i < num_pins; i++) {
2504 hda_nid_t pin = pins[i];
2505 if (pin == spec->hp_mic_pin) {
2506 int ret = create_hp_mic_jack_mode(codec, pin);
2511 if (get_out_jack_num_items(codec, pin) > 1) {
2512 struct snd_kcontrol_new *knew;
2513 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2514 get_jack_mode_name(codec, pin, name, sizeof(name));
2515 knew = snd_hda_gen_add_kctl(spec, name,
2516 &out_jack_mode_enum);
2519 knew->private_value = pin;
2530 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2533 static const char * const vref_texts[NUM_VREFS] = {
2534 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2535 "", "Mic 80pc Bias", "Mic 100pc Bias"
2538 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2540 unsigned int pincap;
2542 pincap = snd_hda_query_pin_caps(codec, pin);
2543 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2544 /* filter out unusual vrefs */
2545 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2549 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2550 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2552 unsigned int i, n = 0;
2554 for (i = 0; i < NUM_VREFS; i++) {
2555 if (vref_caps & (1 << i)) {
2564 /* convert back from the vref ctl index to the enum item index */
2565 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2567 unsigned int i, n = 0;
2569 for (i = 0; i < NUM_VREFS; i++) {
2572 if (vref_caps & (1 << i))
2578 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2579 struct snd_ctl_elem_info *uinfo)
2581 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2582 hda_nid_t nid = kcontrol->private_value;
2583 unsigned int vref_caps = get_vref_caps(codec, nid);
2585 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2587 /* set the right text */
2588 strcpy(uinfo->value.enumerated.name,
2589 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2593 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2594 struct snd_ctl_elem_value *ucontrol)
2596 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2597 hda_nid_t nid = kcontrol->private_value;
2598 unsigned int vref_caps = get_vref_caps(codec, nid);
2601 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2602 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2606 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2607 struct snd_ctl_elem_value *ucontrol)
2609 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2610 hda_nid_t nid = kcontrol->private_value;
2611 unsigned int vref_caps = get_vref_caps(codec, nid);
2612 unsigned int val, idx;
2614 val = snd_hda_codec_get_pin_target(codec, nid);
2615 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2616 if (idx == ucontrol->value.enumerated.item[0])
2619 val &= ~AC_PINCTL_VREFEN;
2620 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2621 snd_hda_set_pin_ctl_cache(codec, nid, val);
2625 static const struct snd_kcontrol_new in_jack_mode_enum = {
2626 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2627 .info = in_jack_mode_info,
2628 .get = in_jack_mode_get,
2629 .put = in_jack_mode_put,
2632 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin)
2634 struct hda_gen_spec *spec = codec->spec;
2636 if (spec->add_jack_modes)
2637 nitems = hweight32(get_vref_caps(codec, pin));
2638 return nitems ? nitems : 1;
2641 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2643 struct hda_gen_spec *spec = codec->spec;
2644 struct snd_kcontrol_new *knew;
2645 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
2646 unsigned int defcfg;
2648 if (pin == spec->hp_mic_pin)
2649 return 0; /* already done in create_out_jack_mode() */
2651 /* no jack mode for fixed pins */
2652 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2653 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2656 /* no multiple vref caps? */
2657 if (get_in_jack_num_items(codec, pin) <= 1)
2660 get_jack_mode_name(codec, pin, name, sizeof(name));
2661 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2664 knew->private_value = pin;
2669 * HP/mic shared jack mode
2671 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol,
2672 struct snd_ctl_elem_info *uinfo)
2674 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2675 hda_nid_t nid = kcontrol->private_value;
2676 int out_jacks = get_out_jack_num_items(codec, nid);
2677 int in_jacks = get_in_jack_num_items(codec, nid);
2678 const char *text = NULL;
2681 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2683 uinfo->value.enumerated.items = out_jacks + in_jacks;
2684 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2685 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
2686 idx = uinfo->value.enumerated.item;
2687 if (idx < out_jacks) {
2689 text = out_jack_texts[idx];
2691 text = "Headphone Out";
2695 unsigned int vref_caps = get_vref_caps(codec, nid);
2696 text = vref_texts[get_vref_idx(vref_caps, idx)];
2701 strcpy(uinfo->value.enumerated.name, text);
2705 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid)
2707 int out_jacks = get_out_jack_num_items(codec, nid);
2708 int in_jacks = get_in_jack_num_items(codec, nid);
2709 unsigned int val = snd_hda_codec_get_pin_target(codec, nid);
2712 if (val & PIN_OUT) {
2713 if (out_jacks > 1 && val == PIN_HP)
2715 } else if (val & PIN_IN) {
2718 unsigned int vref_caps = get_vref_caps(codec, nid);
2719 val &= AC_PINCTL_VREFEN;
2720 idx += cvt_from_vref_idx(vref_caps, val);
2726 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol,
2727 struct snd_ctl_elem_value *ucontrol)
2729 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2730 hda_nid_t nid = kcontrol->private_value;
2731 ucontrol->value.enumerated.item[0] =
2732 get_cur_hp_mic_jack_mode(codec, nid);
2736 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol,
2737 struct snd_ctl_elem_value *ucontrol)
2739 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2740 hda_nid_t nid = kcontrol->private_value;
2741 int out_jacks = get_out_jack_num_items(codec, nid);
2742 int in_jacks = get_in_jack_num_items(codec, nid);
2743 unsigned int val, oldval, idx;
2745 oldval = get_cur_hp_mic_jack_mode(codec, nid);
2746 idx = ucontrol->value.enumerated.item[0];
2750 if (idx < out_jacks) {
2752 val = idx ? PIN_HP : PIN_OUT;
2758 unsigned int vref_caps = get_vref_caps(codec, nid);
2759 val = snd_hda_codec_get_pin_target(codec, nid);
2760 val &= ~(AC_PINCTL_VREFEN | PIN_HP);
2761 val |= get_vref_idx(vref_caps, idx) | PIN_IN;
2763 val = snd_hda_get_default_vref(codec, nid);
2765 snd_hda_set_pin_ctl_cache(codec, nid, val);
2766 call_hp_automute(codec, NULL);
2771 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = {
2772 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2773 .info = hp_mic_jack_mode_info,
2774 .get = hp_mic_jack_mode_get,
2775 .put = hp_mic_jack_mode_put,
2778 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2780 struct hda_gen_spec *spec = codec->spec;
2781 struct snd_kcontrol_new *knew;
2783 if (get_out_jack_num_items(codec, pin) <= 1 &&
2784 get_in_jack_num_items(codec, pin) <= 1)
2785 return 0; /* no need */
2786 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode",
2787 &hp_mic_jack_mode_enum);
2790 knew->private_value = pin;
2791 spec->hp_mic_jack_modes = 1;
2799 /* add the powersave loopback-list entry */
2800 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2802 struct hda_amp_list *list;
2804 list = snd_array_new(&spec->loopback_list);
2808 list->dir = HDA_INPUT;
2810 spec->loopback.amplist = spec->loopback_list.list;
2814 /* create input playback/capture controls for the given pin */
2815 static int new_analog_input(struct hda_codec *codec, int input_idx,
2816 hda_nid_t pin, const char *ctlname, int ctlidx,
2819 struct hda_gen_spec *spec = codec->spec;
2820 struct nid_path *path;
2824 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2825 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2826 return 0; /* no need for analog loopback */
2828 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
2831 print_nid_path("loopback", path);
2832 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
2834 idx = path->idx[path->depth - 1];
2835 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2836 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2837 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
2840 path->ctls[NID_PATH_VOL_CTL] = val;
2843 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2844 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2845 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
2848 path->ctls[NID_PATH_MUTE_CTL] = val;
2851 path->active = true;
2852 err = add_loopback_list(spec, mix_nid, idx);
2856 if (spec->mixer_nid != spec->mixer_merge_nid &&
2857 !spec->loopback_merge_path) {
2858 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2859 spec->mixer_merge_nid, 0);
2861 print_nid_path("loopback-merge", path);
2862 path->active = true;
2863 spec->loopback_merge_path =
2864 snd_hda_get_path_idx(codec, path);
2871 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2873 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2874 return (pincap & AC_PINCAP_IN) != 0;
2877 /* Parse the codec tree and retrieve ADCs */
2878 static int fill_adc_nids(struct hda_codec *codec)
2880 struct hda_gen_spec *spec = codec->spec;
2882 hda_nid_t *adc_nids = spec->adc_nids;
2883 int max_nums = ARRAY_SIZE(spec->adc_nids);
2886 nid = codec->start_nid;
2887 for (i = 0; i < codec->num_nodes; i++, nid++) {
2888 unsigned int caps = get_wcaps(codec, nid);
2889 int type = get_wcaps_type(caps);
2891 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2893 adc_nids[nums] = nid;
2894 if (++nums >= max_nums)
2897 spec->num_adc_nids = nums;
2899 /* copy the detected ADCs to all_adcs[] */
2900 spec->num_all_adcs = nums;
2901 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2906 /* filter out invalid adc_nids that don't give all active input pins;
2907 * if needed, check whether dynamic ADC-switching is available
2909 static int check_dyn_adc_switch(struct hda_codec *codec)
2911 struct hda_gen_spec *spec = codec->spec;
2912 struct hda_input_mux *imux = &spec->input_mux;
2913 unsigned int ok_bits;
2918 for (n = 0; n < spec->num_adc_nids; n++) {
2919 for (i = 0; i < imux->num_items; i++) {
2920 if (!spec->input_paths[i][n])
2923 if (i >= imux->num_items) {
2924 ok_bits |= (1 << n);
2930 /* check whether ADC-switch is possible */
2931 for (i = 0; i < imux->num_items; i++) {
2932 for (n = 0; n < spec->num_adc_nids; n++) {
2933 if (spec->input_paths[i][n]) {
2934 spec->dyn_adc_idx[i] = n;
2940 snd_printdd("hda-codec: enabling ADC switching\n");
2941 spec->dyn_adc_switch = 1;
2942 } else if (nums != spec->num_adc_nids) {
2943 /* shrink the invalid adcs and input paths */
2945 for (n = 0; n < spec->num_adc_nids; n++) {
2946 if (!(ok_bits & (1 << n)))
2949 spec->adc_nids[nums] = spec->adc_nids[n];
2950 for (i = 0; i < imux->num_items; i++) {
2951 invalidate_nid_path(codec,
2952 spec->input_paths[i][nums]);
2953 spec->input_paths[i][nums] =
2954 spec->input_paths[i][n];
2959 spec->num_adc_nids = nums;
2962 if (imux->num_items == 1 ||
2963 (imux->num_items == 2 && spec->hp_mic)) {
2964 snd_printdd("hda-codec: reducing to a single ADC\n");
2965 spec->num_adc_nids = 1; /* reduce to a single ADC */
2968 /* single index for individual volumes ctls */
2969 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2970 spec->num_adc_nids = 1;
2975 /* parse capture source paths from the given pin and create imux items */
2976 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2977 int cfg_idx, int num_adcs,
2978 const char *label, int anchor)
2980 struct hda_gen_spec *spec = codec->spec;
2981 struct hda_input_mux *imux = &spec->input_mux;
2982 int imux_idx = imux->num_items;
2983 bool imux_added = false;
2986 for (c = 0; c < num_adcs; c++) {
2987 struct nid_path *path;
2988 hda_nid_t adc = spec->adc_nids[c];
2990 if (!is_reachable_path(codec, pin, adc))
2992 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2995 print_nid_path("input", path);
2996 spec->input_paths[imux_idx][c] =
2997 snd_hda_get_path_idx(codec, path);
3000 if (spec->hp_mic_pin == pin)
3001 spec->hp_mic_mux_idx = imux->num_items;
3002 spec->imux_pins[imux->num_items] = pin;
3003 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
3012 * create playback/capture controls for input pins
3015 /* fill the label for each input at first */
3016 static int fill_input_pin_labels(struct hda_codec *codec)
3018 struct hda_gen_spec *spec = codec->spec;
3019 const struct auto_pin_cfg *cfg = &spec->autocfg;
3022 for (i = 0; i < cfg->num_inputs; i++) {
3023 hda_nid_t pin = cfg->inputs[i].pin;
3027 if (!is_input_pin(codec, pin))
3030 label = hda_get_autocfg_input_label(codec, cfg, i);
3032 for (j = i - 1; j >= 0; j--) {
3033 if (spec->input_labels[j] &&
3034 !strcmp(spec->input_labels[j], label)) {
3035 idx = spec->input_label_idxs[j] + 1;
3040 spec->input_labels[i] = label;
3041 spec->input_label_idxs[i] = idx;
3047 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3049 static int create_input_ctls(struct hda_codec *codec)
3051 struct hda_gen_spec *spec = codec->spec;
3052 const struct auto_pin_cfg *cfg = &spec->autocfg;
3053 hda_nid_t mixer = spec->mixer_nid;
3058 num_adcs = fill_adc_nids(codec);
3062 err = fill_input_pin_labels(codec);
3066 for (i = 0; i < cfg->num_inputs; i++) {
3069 pin = cfg->inputs[i].pin;
3070 if (!is_input_pin(codec, pin))
3074 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3075 val |= snd_hda_get_default_vref(codec, pin);
3076 if (pin != spec->hp_mic_pin)
3077 set_pin_target(codec, pin, val, false);
3080 if (is_reachable_path(codec, pin, mixer)) {
3081 err = new_analog_input(codec, i, pin,
3082 spec->input_labels[i],
3083 spec->input_label_idxs[i],
3090 err = parse_capture_source(codec, pin, i, num_adcs,
3091 spec->input_labels[i], -mixer);
3095 if (spec->add_jack_modes) {
3096 err = create_in_jack_mode(codec, pin);
3102 if (mixer && spec->add_stereo_mix_input) {
3103 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
3117 /* get the input path specified by the given adc and imux indices */
3118 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
3120 struct hda_gen_spec *spec = codec->spec;
3121 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
3125 if (spec->dyn_adc_switch)
3126 adc_idx = spec->dyn_adc_idx[imux_idx];
3127 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
3131 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
3134 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3137 static int mux_enum_info(struct snd_kcontrol *kcontrol,
3138 struct snd_ctl_elem_info *uinfo)
3140 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3141 struct hda_gen_spec *spec = codec->spec;
3142 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
3145 static int mux_enum_get(struct snd_kcontrol *kcontrol,
3146 struct snd_ctl_elem_value *ucontrol)
3148 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3149 struct hda_gen_spec *spec = codec->spec;
3150 /* the ctls are created at once with multiple counts */
3151 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3153 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
3157 static int mux_enum_put(struct snd_kcontrol *kcontrol,
3158 struct snd_ctl_elem_value *ucontrol)
3160 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3161 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
3162 return mux_select(codec, adc_idx,
3163 ucontrol->value.enumerated.item[0]);
3166 static const struct snd_kcontrol_new cap_src_temp = {
3167 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3168 .name = "Input Source",
3169 .info = mux_enum_info,
3170 .get = mux_enum_get,
3171 .put = mux_enum_put,
3175 * capture volume and capture switch ctls
3178 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
3179 struct snd_ctl_elem_value *ucontrol);
3181 /* call the given amp update function for all amps in the imux list at once */
3182 static int cap_put_caller(struct snd_kcontrol *kcontrol,
3183 struct snd_ctl_elem_value *ucontrol,
3184 put_call_t func, int type)
3186 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3187 struct hda_gen_spec *spec = codec->spec;
3188 const struct hda_input_mux *imux;
3189 struct nid_path *path;
3190 int i, adc_idx, err = 0;
3192 imux = &spec->input_mux;
3193 adc_idx = kcontrol->id.index;
3194 mutex_lock(&codec->control_mutex);
3195 /* we use the cache-only update at first since multiple input paths
3196 * may shared the same amp; by updating only caches, the redundant
3197 * writes to hardware can be reduced.
3199 codec->cached_write = 1;
3200 for (i = 0; i < imux->num_items; i++) {
3201 path = get_input_path(codec, adc_idx, i);
3202 if (!path || !path->ctls[type])
3204 kcontrol->private_value = path->ctls[type];
3205 err = func(kcontrol, ucontrol);
3210 codec->cached_write = 0;
3211 mutex_unlock(&codec->control_mutex);
3212 snd_hda_codec_flush_cache(codec); /* flush the updates */
3213 if (err >= 0 && spec->cap_sync_hook)
3214 spec->cap_sync_hook(codec, ucontrol);
3218 /* capture volume ctl callbacks */
3219 #define cap_vol_info snd_hda_mixer_amp_volume_info
3220 #define cap_vol_get snd_hda_mixer_amp_volume_get
3221 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3223 static int cap_vol_put(struct snd_kcontrol *kcontrol,
3224 struct snd_ctl_elem_value *ucontrol)
3226 return cap_put_caller(kcontrol, ucontrol,
3227 snd_hda_mixer_amp_volume_put,
3231 static const struct snd_kcontrol_new cap_vol_temp = {
3232 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3233 .name = "Capture Volume",
3234 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
3235 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
3236 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
3237 .info = cap_vol_info,
3240 .tlv = { .c = cap_vol_tlv },
3243 /* capture switch ctl callbacks */
3244 #define cap_sw_info snd_ctl_boolean_stereo_info
3245 #define cap_sw_get snd_hda_mixer_amp_switch_get
3247 static int cap_sw_put(struct snd_kcontrol *kcontrol,
3248 struct snd_ctl_elem_value *ucontrol)
3250 return cap_put_caller(kcontrol, ucontrol,
3251 snd_hda_mixer_amp_switch_put,
3255 static const struct snd_kcontrol_new cap_sw_temp = {
3256 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3257 .name = "Capture Switch",
3258 .info = cap_sw_info,
3263 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3268 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3269 for (depth = 0; depth < 3; depth++) {
3270 if (depth >= path->depth)
3272 i = path->depth - depth - 1;
3273 nid = path->path[i];
3274 if (!path->ctls[NID_PATH_VOL_CTL]) {
3275 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3276 path->ctls[NID_PATH_VOL_CTL] =
3277 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3278 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3279 int idx = path->idx[i];
3280 if (!depth && codec->single_adc_amp)
3282 path->ctls[NID_PATH_VOL_CTL] =
3283 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3286 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3287 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3288 path->ctls[NID_PATH_MUTE_CTL] =
3289 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3290 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3291 int idx = path->idx[i];
3292 if (!depth && codec->single_adc_amp)
3294 path->ctls[NID_PATH_MUTE_CTL] =
3295 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3302 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3304 struct hda_gen_spec *spec = codec->spec;
3305 struct auto_pin_cfg *cfg = &spec->autocfg;
3309 if (!spec->inv_dmic_split)
3311 for (i = 0; i < cfg->num_inputs; i++) {
3312 if (cfg->inputs[i].pin != nid)
3314 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3316 val = snd_hda_codec_get_pincfg(codec, nid);
3317 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3322 /* capture switch put callback for a single control with hook call */
3323 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3324 struct snd_ctl_elem_value *ucontrol)
3326 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3327 struct hda_gen_spec *spec = codec->spec;
3330 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3334 if (spec->cap_sync_hook)
3335 spec->cap_sync_hook(codec, ucontrol);
3340 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3341 int idx, bool is_switch, unsigned int ctl,
3344 struct hda_gen_spec *spec = codec->spec;
3345 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3346 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3347 const char *sfx = is_switch ? "Switch" : "Volume";
3348 unsigned int chs = inv_dmic ? 1 : 3;
3349 struct snd_kcontrol_new *knew;
3355 snprintf(tmpname, sizeof(tmpname),
3356 "%s Capture %s", label, sfx);
3358 snprintf(tmpname, sizeof(tmpname),
3360 knew = add_control(spec, type, tmpname, idx,
3361 amp_val_replace_channels(ctl, chs));
3365 knew->put = cap_single_sw_put;
3369 /* Make independent right kcontrol */
3371 snprintf(tmpname, sizeof(tmpname),
3372 "Inverted %s Capture %s", label, sfx);
3374 snprintf(tmpname, sizeof(tmpname),
3375 "Inverted Capture %s", sfx);
3376 knew = add_control(spec, type, tmpname, idx,
3377 amp_val_replace_channels(ctl, 2));
3381 knew->put = cap_single_sw_put;
3385 /* create single (and simple) capture volume and switch controls */
3386 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3387 unsigned int vol_ctl, unsigned int sw_ctl,
3391 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3394 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3400 /* create bound capture volume and switch controls */
3401 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3402 unsigned int vol_ctl, unsigned int sw_ctl)
3404 struct hda_gen_spec *spec = codec->spec;
3405 struct snd_kcontrol_new *knew;
3408 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3412 knew->private_value = vol_ctl;
3413 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3416 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3420 knew->private_value = sw_ctl;
3421 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3426 /* return the vol ctl when used first in the imux list */
3427 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3429 struct nid_path *path;
3433 path = get_input_path(codec, 0, idx);
3436 ctl = path->ctls[type];
3439 for (i = 0; i < idx - 1; i++) {
3440 path = get_input_path(codec, 0, i);
3441 if (path && path->ctls[type] == ctl)
3447 /* create individual capture volume and switch controls per input */
3448 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3450 struct hda_gen_spec *spec = codec->spec;
3451 struct hda_input_mux *imux = &spec->input_mux;
3454 for (i = 0; i < imux->num_items; i++) {
3458 idx = imux->items[i].index;
3459 if (idx >= spec->autocfg.num_inputs)
3461 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3463 for (type = 0; type < 2; type++) {
3464 err = add_single_cap_ctl(codec,
3465 spec->input_labels[idx],
3466 spec->input_label_idxs[idx],
3468 get_first_cap_ctl(codec, i, type),
3477 static int create_capture_mixers(struct hda_codec *codec)
3479 struct hda_gen_spec *spec = codec->spec;
3480 struct hda_input_mux *imux = &spec->input_mux;
3481 int i, n, nums, err;
3483 if (spec->dyn_adc_switch)
3486 nums = spec->num_adc_nids;
3488 if (!spec->auto_mic && imux->num_items > 1) {
3489 struct snd_kcontrol_new *knew;
3491 name = nums > 1 ? "Input Source" : "Capture Source";
3492 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3498 for (n = 0; n < nums; n++) {
3500 bool multi_cap_vol = spec->multi_cap_vol;
3501 bool inv_dmic = false;
3505 for (i = 0; i < imux->num_items; i++) {
3506 struct nid_path *path;
3507 path = get_input_path(codec, n, i);
3510 parse_capvol_in_path(codec, path);
3512 vol = path->ctls[NID_PATH_VOL_CTL];
3513 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3515 if (!same_amp_caps(codec, vol,
3516 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3517 multi_cap_vol = true;
3520 sw = path->ctls[NID_PATH_MUTE_CTL];
3521 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3523 if (!same_amp_caps(codec, sw,
3524 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3525 multi_cap_vol = true;
3527 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3532 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3534 else if (!multi_cap_vol)
3535 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3537 err = create_multi_cap_vol_ctl(codec);
3546 * add mic boosts if needed
3549 /* check whether the given amp is feasible as a boost volume */
3550 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3555 if (!nid_has_volume(codec, nid, dir) ||
3556 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3557 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3560 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3561 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3567 /* look for a boost amp in a widget close to the pin */
3568 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3569 struct nid_path *path)
3571 unsigned int val = 0;
3575 for (depth = 0; depth < 3; depth++) {
3576 if (depth >= path->depth - 1)
3578 nid = path->path[depth];
3579 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3580 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3582 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3583 path->idx[depth])) {
3584 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3593 static int parse_mic_boost(struct hda_codec *codec)
3595 struct hda_gen_spec *spec = codec->spec;
3596 struct auto_pin_cfg *cfg = &spec->autocfg;
3597 struct hda_input_mux *imux = &spec->input_mux;
3600 if (!spec->num_adc_nids)
3603 for (i = 0; i < imux->num_items; i++) {
3604 struct nid_path *path;
3607 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
3609 idx = imux->items[i].index;
3610 if (idx >= imux->num_items)
3613 /* check only line-in and mic pins */
3614 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3617 path = get_input_path(codec, 0, i);
3621 val = look_for_boost_amp(codec, path);
3625 /* create a boost control */
3626 snprintf(boost_label, sizeof(boost_label),
3627 "%s Boost Volume", spec->input_labels[idx]);
3628 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3629 spec->input_label_idxs[idx], val))
3632 path->ctls[NID_PATH_BOOST_CTL] = val;
3638 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3640 static void parse_digital(struct hda_codec *codec)
3642 struct hda_gen_spec *spec = codec->spec;
3643 struct nid_path *path;
3645 hda_nid_t dig_nid, pin;
3647 /* support multiple SPDIFs; the secondary is set up as a slave */
3649 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3650 pin = spec->autocfg.dig_out_pins[i];
3651 dig_nid = look_for_dac(codec, pin, true);
3654 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3657 print_nid_path("digout", path);
3658 path->active = true;
3659 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3660 set_pin_target(codec, pin, PIN_OUT, false);
3662 spec->multiout.dig_out_nid = dig_nid;
3663 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3665 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3666 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3668 spec->slave_dig_outs[nums - 1] = dig_nid;
3673 if (spec->autocfg.dig_in_pin) {
3674 pin = spec->autocfg.dig_in_pin;
3675 dig_nid = codec->start_nid;
3676 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3677 unsigned int wcaps = get_wcaps(codec, dig_nid);
3678 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3680 if (!(wcaps & AC_WCAP_DIGITAL))
3682 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3684 print_nid_path("digin", path);
3685 path->active = true;
3686 spec->dig_in_nid = dig_nid;
3687 spec->digin_path = snd_hda_get_path_idx(codec, path);
3688 set_pin_target(codec, pin, PIN_IN, false);
3697 * input MUX handling
3700 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3702 /* select the given imux item; either unmute exclusively or select the route */
3703 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3706 struct hda_gen_spec *spec = codec->spec;
3707 const struct hda_input_mux *imux;
3708 struct nid_path *old_path, *path;
3710 imux = &spec->input_mux;
3711 if (!imux->num_items)
3714 if (idx >= imux->num_items)
3715 idx = imux->num_items - 1;
3716 if (spec->cur_mux[adc_idx] == idx)
3719 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3722 if (old_path->active)
3723 snd_hda_activate_path(codec, old_path, false, false);
3725 spec->cur_mux[adc_idx] = idx;
3728 update_hp_mic(codec, adc_idx, false);
3730 if (spec->dyn_adc_switch)
3731 dyn_adc_pcm_resetup(codec, idx);
3733 path = get_input_path(codec, adc_idx, idx);
3738 snd_hda_activate_path(codec, path, true, false);
3739 if (spec->cap_sync_hook)
3740 spec->cap_sync_hook(codec, NULL);
3741 path_power_down_sync(codec, old_path);
3747 * Jack detections for HP auto-mute and mic-switch
3750 /* check each pin in the given array; returns true if any of them is plugged */
3751 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3754 bool present = false;
3756 for (i = 0; i < num_pins; i++) {
3757 hda_nid_t nid = pins[i];
3760 /* don't detect pins retasked as inputs */
3761 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3763 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT)
3769 /* standard HP/line-out auto-mute helper */
3770 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
3771 int *paths, bool mute)
3773 struct hda_gen_spec *spec = codec->spec;
3776 for (i = 0; i < num_pins; i++) {
3777 hda_nid_t nid = pins[i];
3778 unsigned int val, oldval;
3782 if (spec->auto_mute_via_amp) {
3783 struct nid_path *path;
3786 path = snd_hda_get_path_from_idx(codec, paths[i]);
3789 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]);
3793 spec->mute_bits |= (1ULL << mute_nid);
3795 spec->mute_bits &= ~(1ULL << mute_nid);
3796 set_pin_eapd(codec, nid, !mute);
3800 oldval = snd_hda_codec_get_pin_target(codec, nid);
3801 if (oldval & PIN_IN)
3802 continue; /* no mute for inputs */
3803 /* don't reset VREF value in case it's controlling
3804 * the amp (see alc861_fixup_asus_amp_vref_0f())
3806 if (spec->keep_vref_in_automute)
3807 val = oldval & ~PIN_HP;
3812 /* here we call update_pin_ctl() so that the pinctl is changed
3813 * without changing the pinctl target value;
3814 * the original target value will be still referred at the
3815 * init / resume again
3817 update_pin_ctl(codec, nid, val);
3818 set_pin_eapd(codec, nid, !mute);
3822 /* Toggle outputs muting */
3823 void snd_hda_gen_update_outputs(struct hda_codec *codec)
3825 struct hda_gen_spec *spec = codec->spec;
3829 /* Control HP pins/amps depending on master_mute state;
3830 * in general, HP pins/amps control should be enabled in all cases,
3831 * but currently set only for master_mute, just to be safe
3833 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3834 paths = spec->out_paths;
3836 paths = spec->hp_paths;
3837 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3838 spec->autocfg.hp_pins, paths, spec->master_mute);
3840 if (!spec->automute_speaker)
3843 on = spec->hp_jack_present | spec->line_jack_present;
3844 on |= spec->master_mute;
3845 spec->speaker_muted = on;
3846 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3847 paths = spec->out_paths;
3849 paths = spec->speaker_paths;
3850 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
3851 spec->autocfg.speaker_pins, paths, on);
3853 /* toggle line-out mutes if needed, too */
3854 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3855 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3856 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3858 if (!spec->automute_lo)
3861 on = spec->hp_jack_present;
3862 on |= spec->master_mute;
3863 spec->line_out_muted = on;
3864 paths = spec->out_paths;
3865 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3866 spec->autocfg.line_out_pins, paths, on);
3868 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
3870 static void call_update_outputs(struct hda_codec *codec)
3872 struct hda_gen_spec *spec = codec->spec;
3873 if (spec->automute_hook)
3874 spec->automute_hook(codec);
3876 snd_hda_gen_update_outputs(codec);
3878 /* sync the whole vmaster slaves to reflect the new auto-mute status */
3879 if (spec->auto_mute_via_amp && !codec->bus->shutdown)
3880 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false);
3883 /* standard HP-automute helper */
3884 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3886 struct hda_gen_spec *spec = codec->spec;
3887 hda_nid_t *pins = spec->autocfg.hp_pins;
3888 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
3890 /* No detection for the first HP jack during indep-HP mode */
3891 if (spec->indep_hp_enabled) {
3896 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
3897 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3899 call_update_outputs(codec);
3901 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
3903 /* standard line-out-automute helper */
3904 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3906 struct hda_gen_spec *spec = codec->spec;
3908 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3910 /* check LO jack only when it's different from HP */
3911 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3914 spec->line_jack_present =
3915 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3916 spec->autocfg.line_out_pins);
3917 if (!spec->automute_speaker || !spec->detect_lo)
3919 call_update_outputs(codec);
3921 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
3923 /* standard mic auto-switch helper */
3924 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
3926 struct hda_gen_spec *spec = codec->spec;
3929 if (!spec->auto_mic)
3932 for (i = spec->am_num_entries - 1; i > 0; i--) {
3933 hda_nid_t pin = spec->am_entry[i].pin;
3934 /* don't detect pins retasked as outputs */
3935 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3937 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) {
3938 mux_select(codec, 0, spec->am_entry[i].idx);
3942 mux_select(codec, 0, spec->am_entry[0].idx);
3944 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
3946 /* call appropriate hooks */
3947 static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3949 struct hda_gen_spec *spec = codec->spec;
3950 if (spec->hp_automute_hook)
3951 spec->hp_automute_hook(codec, jack);
3953 snd_hda_gen_hp_automute(codec, jack);
3956 static void call_line_automute(struct hda_codec *codec,
3957 struct hda_jack_tbl *jack)
3959 struct hda_gen_spec *spec = codec->spec;
3960 if (spec->line_automute_hook)
3961 spec->line_automute_hook(codec, jack);
3963 snd_hda_gen_line_automute(codec, jack);
3966 static void call_mic_autoswitch(struct hda_codec *codec,
3967 struct hda_jack_tbl *jack)
3969 struct hda_gen_spec *spec = codec->spec;
3970 if (spec->mic_autoswitch_hook)
3971 spec->mic_autoswitch_hook(codec, jack);
3973 snd_hda_gen_mic_autoswitch(codec, jack);
3976 /* update jack retasking */
3977 static void update_automute_all(struct hda_codec *codec)
3979 call_hp_automute(codec, NULL);
3980 call_line_automute(codec, NULL);
3981 call_mic_autoswitch(codec, NULL);
3985 * Auto-Mute mode mixer enum support
3987 static int automute_mode_info(struct snd_kcontrol *kcontrol,
3988 struct snd_ctl_elem_info *uinfo)
3990 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3991 struct hda_gen_spec *spec = codec->spec;
3992 static const char * const texts3[] = {
3993 "Disabled", "Speaker Only", "Line Out+Speaker"
3996 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3997 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3998 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
4001 static int automute_mode_get(struct snd_kcontrol *kcontrol,
4002 struct snd_ctl_elem_value *ucontrol)
4004 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4005 struct hda_gen_spec *spec = codec->spec;
4006 unsigned int val = 0;
4007 if (spec->automute_speaker)
4009 if (spec->automute_lo)
4012 ucontrol->value.enumerated.item[0] = val;
4016 static int automute_mode_put(struct snd_kcontrol *kcontrol,
4017 struct snd_ctl_elem_value *ucontrol)
4019 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
4020 struct hda_gen_spec *spec = codec->spec;
4022 switch (ucontrol->value.enumerated.item[0]) {
4024 if (!spec->automute_speaker && !spec->automute_lo)
4026 spec->automute_speaker = 0;
4027 spec->automute_lo = 0;
4030 if (spec->automute_speaker_possible) {
4031 if (!spec->automute_lo && spec->automute_speaker)
4033 spec->automute_speaker = 1;
4034 spec->automute_lo = 0;
4035 } else if (spec->automute_lo_possible) {
4036 if (spec->automute_lo)
4038 spec->automute_lo = 1;
4043 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
4045 if (spec->automute_speaker && spec->automute_lo)
4047 spec->automute_speaker = 1;
4048 spec->automute_lo = 1;
4053 call_update_outputs(codec);
4057 static const struct snd_kcontrol_new automute_mode_enum = {
4058 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
4059 .name = "Auto-Mute Mode",
4060 .info = automute_mode_info,
4061 .get = automute_mode_get,
4062 .put = automute_mode_put,
4065 static int add_automute_mode_enum(struct hda_codec *codec)
4067 struct hda_gen_spec *spec = codec->spec;
4069 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
4075 * Check the availability of HP/line-out auto-mute;
4076 * Set up appropriately if really supported
4078 static int check_auto_mute_availability(struct hda_codec *codec)
4080 struct hda_gen_spec *spec = codec->spec;
4081 struct auto_pin_cfg *cfg = &spec->autocfg;
4085 if (spec->suppress_auto_mute)
4088 if (cfg->hp_pins[0])
4090 if (cfg->line_out_pins[0])
4092 if (cfg->speaker_pins[0])
4094 if (present < 2) /* need two different output types */
4097 if (!cfg->speaker_pins[0] &&
4098 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
4099 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4100 sizeof(cfg->speaker_pins));
4101 cfg->speaker_outs = cfg->line_outs;
4104 if (!cfg->hp_pins[0] &&
4105 cfg->line_out_type == AUTO_PIN_HP_OUT) {
4106 memcpy(cfg->hp_pins, cfg->line_out_pins,
4107 sizeof(cfg->hp_pins));
4108 cfg->hp_outs = cfg->line_outs;
4111 for (i = 0; i < cfg->hp_outs; i++) {
4112 hda_nid_t nid = cfg->hp_pins[i];
4113 if (!is_jack_detectable(codec, nid))
4115 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
4117 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
4119 spec->detect_hp = 1;
4122 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
4123 if (cfg->speaker_outs)
4124 for (i = 0; i < cfg->line_outs; i++) {
4125 hda_nid_t nid = cfg->line_out_pins[i];
4126 if (!is_jack_detectable(codec, nid))
4128 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
4129 snd_hda_jack_detect_enable_callback(codec, nid,
4130 HDA_GEN_FRONT_EVENT,
4131 call_line_automute);
4132 spec->detect_lo = 1;
4134 spec->automute_lo_possible = spec->detect_hp;
4137 spec->automute_speaker_possible = cfg->speaker_outs &&
4138 (spec->detect_hp || spec->detect_lo);
4140 spec->automute_lo = spec->automute_lo_possible;
4141 spec->automute_speaker = spec->automute_speaker_possible;
4143 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
4144 /* create a control for automute mode */
4145 err = add_automute_mode_enum(codec);
4152 /* check whether all auto-mic pins are valid; setup indices if OK */
4153 static bool auto_mic_check_imux(struct hda_codec *codec)
4155 struct hda_gen_spec *spec = codec->spec;
4156 const struct hda_input_mux *imux;
4159 imux = &spec->input_mux;
4160 for (i = 0; i < spec->am_num_entries; i++) {
4161 spec->am_entry[i].idx =
4162 find_idx_in_nid_list(spec->am_entry[i].pin,
4163 spec->imux_pins, imux->num_items);
4164 if (spec->am_entry[i].idx < 0)
4165 return false; /* no corresponding imux */
4168 /* we don't need the jack detection for the first pin */
4169 for (i = 1; i < spec->am_num_entries; i++)
4170 snd_hda_jack_detect_enable_callback(codec,
4171 spec->am_entry[i].pin,
4173 call_mic_autoswitch);
4177 static int compare_attr(const void *ap, const void *bp)
4179 const struct automic_entry *a = ap;
4180 const struct automic_entry *b = bp;
4181 return (int)(a->attr - b->attr);
4185 * Check the availability of auto-mic switch;
4186 * Set up if really supported
4188 static int check_auto_mic_availability(struct hda_codec *codec)
4190 struct hda_gen_spec *spec = codec->spec;
4191 struct auto_pin_cfg *cfg = &spec->autocfg;
4195 if (spec->suppress_auto_mic)
4200 for (i = 0; i < cfg->num_inputs; i++) {
4201 hda_nid_t nid = cfg->inputs[i].pin;
4203 attr = snd_hda_codec_get_pincfg(codec, nid);
4204 attr = snd_hda_get_input_pin_attr(attr);
4205 if (types & (1 << attr))
4206 return 0; /* already occupied */
4208 case INPUT_PIN_ATTR_INT:
4209 if (cfg->inputs[i].type != AUTO_PIN_MIC)
4210 return 0; /* invalid type */
4212 case INPUT_PIN_ATTR_UNUSED:
4213 return 0; /* invalid entry */
4215 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
4216 return 0; /* invalid type */
4217 if (!spec->line_in_auto_switch &&
4218 cfg->inputs[i].type != AUTO_PIN_MIC)
4219 return 0; /* only mic is allowed */
4220 if (!is_jack_detectable(codec, nid))
4221 return 0; /* no unsol support */
4224 if (num_pins >= MAX_AUTO_MIC_PINS)
4226 types |= (1 << attr);
4227 spec->am_entry[num_pins].pin = nid;
4228 spec->am_entry[num_pins].attr = attr;
4235 spec->am_num_entries = num_pins;
4236 /* sort the am_entry in the order of attr so that the pin with a
4237 * higher attr will be selected when the jack is plugged.
4239 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
4240 compare_attr, NULL);
4242 if (!auto_mic_check_imux(codec))
4246 spec->num_adc_nids = 1;
4247 spec->cur_mux[0] = spec->am_entry[0].idx;
4248 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4249 spec->am_entry[0].pin,
4250 spec->am_entry[1].pin,
4251 spec->am_entry[2].pin);
4256 /* power_filter hook; make inactive widgets into power down */
4257 static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
4259 unsigned int power_state)
4261 if (power_state != AC_PWRST_D0)
4263 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
4265 if (is_active_nid_for_any(codec, nid))
4272 * Parse the given BIOS configuration and set up the hda_gen_spec
4274 * return 1 if successful, 0 if the proper config is not found,
4275 * or a negative error code
4277 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
4278 struct auto_pin_cfg *cfg)
4280 struct hda_gen_spec *spec = codec->spec;
4283 parse_user_hints(codec);
4285 if (spec->mixer_nid && !spec->mixer_merge_nid)
4286 spec->mixer_merge_nid = spec->mixer_nid;
4288 if (cfg != &spec->autocfg) {
4289 spec->autocfg = *cfg;
4290 cfg = &spec->autocfg;
4293 if (!spec->main_out_badness)
4294 spec->main_out_badness = &hda_main_out_badness;
4295 if (!spec->extra_out_badness)
4296 spec->extra_out_badness = &hda_extra_out_badness;
4298 fill_all_dac_nids(codec);
4300 if (!cfg->line_outs) {
4301 if (cfg->dig_outs || cfg->dig_in_pin) {
4302 spec->multiout.max_channels = 2;
4303 spec->no_analog = 1;
4306 return 0; /* can't find valid BIOS pin config */
4309 if (!spec->no_primary_hp &&
4310 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
4311 cfg->line_outs <= cfg->hp_outs) {
4312 /* use HP as primary out */
4313 cfg->speaker_outs = cfg->line_outs;
4314 memcpy(cfg->speaker_pins, cfg->line_out_pins,
4315 sizeof(cfg->speaker_pins));
4316 cfg->line_outs = cfg->hp_outs;
4317 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4319 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4320 cfg->line_out_type = AUTO_PIN_HP_OUT;
4323 err = parse_output_paths(codec);
4326 err = create_multi_channel_mode(codec);
4329 err = create_multi_out_ctls(codec, cfg);
4332 err = create_hp_out_ctls(codec);
4335 err = create_speaker_out_ctls(codec);
4338 err = create_indep_hp_ctls(codec);
4341 err = create_loopback_mixing_ctl(codec);
4344 err = create_hp_mic(codec);
4347 err = create_input_ctls(codec);
4351 spec->const_channel_count = spec->ext_channel_count;
4352 /* check the multiple speaker and headphone pins */
4353 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4354 spec->const_channel_count = max(spec->const_channel_count,
4355 cfg->speaker_outs * 2);
4356 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4357 spec->const_channel_count = max(spec->const_channel_count,
4359 spec->multiout.max_channels = max(spec->ext_channel_count,
4360 spec->const_channel_count);
4362 err = check_auto_mute_availability(codec);
4366 err = check_dyn_adc_switch(codec);
4370 err = check_auto_mic_availability(codec);
4374 err = create_capture_mixers(codec);
4378 err = parse_mic_boost(codec);
4382 if (spec->add_jack_modes) {
4383 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4384 err = create_out_jack_modes(codec, cfg->line_outs,
4385 cfg->line_out_pins);
4389 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4390 err = create_out_jack_modes(codec, cfg->hp_outs,
4398 parse_digital(codec);
4400 if (spec->power_down_unused)
4401 codec->power_filter = snd_hda_gen_path_power_filter;
4403 if (!spec->no_analog && spec->beep_nid) {
4404 err = snd_hda_attach_beep_device(codec, spec->beep_nid);
4411 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
4415 * Build control elements
4418 /* slave controls for virtual master */
4419 static const char * const slave_pfxs[] = {
4420 "Front", "Surround", "Center", "LFE", "Side",
4421 "Headphone", "Speaker", "Mono", "Line Out",
4422 "CLFE", "Bass Speaker", "PCM",
4423 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4424 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4429 int snd_hda_gen_build_controls(struct hda_codec *codec)
4431 struct hda_gen_spec *spec = codec->spec;
4434 if (spec->kctls.used) {
4435 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4440 if (spec->multiout.dig_out_nid) {
4441 err = snd_hda_create_dig_out_ctls(codec,
4442 spec->multiout.dig_out_nid,
4443 spec->multiout.dig_out_nid,
4444 spec->pcm_rec[1].pcm_type);
4447 if (!spec->no_analog) {
4448 err = snd_hda_create_spdif_share_sw(codec,
4452 spec->multiout.share_spdif = 1;
4455 if (spec->dig_in_nid) {
4456 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4461 /* if we have no master control, let's create it */
4462 if (!spec->no_analog &&
4463 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
4464 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
4465 spec->vmaster_tlv, slave_pfxs,
4470 if (!spec->no_analog &&
4471 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4472 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4475 true, &spec->vmaster_mute.sw_kctl);
4478 if (spec->vmaster_mute.hook)
4479 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4480 spec->vmaster_mute_enum);
4483 free_kctls(spec); /* no longer needed */
4485 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4491 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4498 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4499 struct hda_codec *codec,
4500 struct snd_pcm_substream *substream,
4503 struct hda_gen_spec *spec = codec->spec;
4504 if (spec->pcm_playback_hook)
4505 spec->pcm_playback_hook(hinfo, codec, substream, action);
4508 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4509 struct hda_codec *codec,
4510 struct snd_pcm_substream *substream,
4513 struct hda_gen_spec *spec = codec->spec;
4514 if (spec->pcm_capture_hook)
4515 spec->pcm_capture_hook(hinfo, codec, substream, action);
4519 * Analog playback callbacks
4521 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4522 struct hda_codec *codec,
4523 struct snd_pcm_substream *substream)
4525 struct hda_gen_spec *spec = codec->spec;
4528 mutex_lock(&spec->pcm_mutex);
4529 err = snd_hda_multi_out_analog_open(codec,
4530 &spec->multiout, substream,
4533 spec->active_streams |= 1 << STREAM_MULTI_OUT;
4534 call_pcm_playback_hook(hinfo, codec, substream,
4535 HDA_GEN_PCM_ACT_OPEN);
4537 mutex_unlock(&spec->pcm_mutex);
4541 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4542 struct hda_codec *codec,
4543 unsigned int stream_tag,
4544 unsigned int format,
4545 struct snd_pcm_substream *substream)
4547 struct hda_gen_spec *spec = codec->spec;
4550 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4551 stream_tag, format, substream);
4553 call_pcm_playback_hook(hinfo, codec, substream,
4554 HDA_GEN_PCM_ACT_PREPARE);
4558 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4559 struct hda_codec *codec,
4560 struct snd_pcm_substream *substream)
4562 struct hda_gen_spec *spec = codec->spec;
4565 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4567 call_pcm_playback_hook(hinfo, codec, substream,
4568 HDA_GEN_PCM_ACT_CLEANUP);
4572 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4573 struct hda_codec *codec,
4574 struct snd_pcm_substream *substream)
4576 struct hda_gen_spec *spec = codec->spec;
4577 mutex_lock(&spec->pcm_mutex);
4578 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
4579 call_pcm_playback_hook(hinfo, codec, substream,
4580 HDA_GEN_PCM_ACT_CLOSE);
4581 mutex_unlock(&spec->pcm_mutex);
4585 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4586 struct hda_codec *codec,
4587 struct snd_pcm_substream *substream)
4589 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4593 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4594 struct hda_codec *codec,
4595 unsigned int stream_tag,
4596 unsigned int format,
4597 struct snd_pcm_substream *substream)
4599 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4600 call_pcm_capture_hook(hinfo, codec, substream,
4601 HDA_GEN_PCM_ACT_PREPARE);
4605 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4606 struct hda_codec *codec,
4607 struct snd_pcm_substream *substream)
4609 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4610 call_pcm_capture_hook(hinfo, codec, substream,
4611 HDA_GEN_PCM_ACT_CLEANUP);
4615 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4616 struct hda_codec *codec,
4617 struct snd_pcm_substream *substream)
4619 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4623 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4624 struct hda_codec *codec,
4625 struct snd_pcm_substream *substream)
4627 struct hda_gen_spec *spec = codec->spec;
4630 mutex_lock(&spec->pcm_mutex);
4631 if (!spec->indep_hp_enabled)
4634 spec->active_streams |= 1 << STREAM_INDEP_HP;
4635 call_pcm_playback_hook(hinfo, codec, substream,
4636 HDA_GEN_PCM_ACT_OPEN);
4637 mutex_unlock(&spec->pcm_mutex);
4641 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4642 struct hda_codec *codec,
4643 struct snd_pcm_substream *substream)
4645 struct hda_gen_spec *spec = codec->spec;
4646 mutex_lock(&spec->pcm_mutex);
4647 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
4648 call_pcm_playback_hook(hinfo, codec, substream,
4649 HDA_GEN_PCM_ACT_CLOSE);
4650 mutex_unlock(&spec->pcm_mutex);
4654 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4655 struct hda_codec *codec,
4656 unsigned int stream_tag,
4657 unsigned int format,
4658 struct snd_pcm_substream *substream)
4660 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4661 call_pcm_playback_hook(hinfo, codec, substream,
4662 HDA_GEN_PCM_ACT_PREPARE);
4666 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4667 struct hda_codec *codec,
4668 struct snd_pcm_substream *substream)
4670 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4671 call_pcm_playback_hook(hinfo, codec, substream,
4672 HDA_GEN_PCM_ACT_CLEANUP);
4679 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4680 struct hda_codec *codec,
4681 struct snd_pcm_substream *substream)
4683 struct hda_gen_spec *spec = codec->spec;
4684 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4687 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4688 struct hda_codec *codec,
4689 unsigned int stream_tag,
4690 unsigned int format,
4691 struct snd_pcm_substream *substream)
4693 struct hda_gen_spec *spec = codec->spec;
4694 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4695 stream_tag, format, substream);
4698 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4699 struct hda_codec *codec,
4700 struct snd_pcm_substream *substream)
4702 struct hda_gen_spec *spec = codec->spec;
4703 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4706 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4707 struct hda_codec *codec,
4708 struct snd_pcm_substream *substream)
4710 struct hda_gen_spec *spec = codec->spec;
4711 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4717 #define alt_capture_pcm_open capture_pcm_open
4718 #define alt_capture_pcm_close capture_pcm_close
4720 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4721 struct hda_codec *codec,
4722 unsigned int stream_tag,
4723 unsigned int format,
4724 struct snd_pcm_substream *substream)
4726 struct hda_gen_spec *spec = codec->spec;
4728 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
4729 stream_tag, 0, format);
4730 call_pcm_capture_hook(hinfo, codec, substream,
4731 HDA_GEN_PCM_ACT_PREPARE);
4735 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4736 struct hda_codec *codec,
4737 struct snd_pcm_substream *substream)
4739 struct hda_gen_spec *spec = codec->spec;
4741 snd_hda_codec_cleanup_stream(codec,
4742 spec->adc_nids[substream->number + 1]);
4743 call_pcm_capture_hook(hinfo, codec, substream,
4744 HDA_GEN_PCM_ACT_CLEANUP);
4750 static const struct hda_pcm_stream pcm_analog_playback = {
4754 /* NID is set in build_pcms */
4756 .open = playback_pcm_open,
4757 .close = playback_pcm_close,
4758 .prepare = playback_pcm_prepare,
4759 .cleanup = playback_pcm_cleanup
4763 static const struct hda_pcm_stream pcm_analog_capture = {
4767 /* NID is set in build_pcms */
4769 .open = capture_pcm_open,
4770 .close = capture_pcm_close,
4771 .prepare = capture_pcm_prepare,
4772 .cleanup = capture_pcm_cleanup
4776 static const struct hda_pcm_stream pcm_analog_alt_playback = {
4780 /* NID is set in build_pcms */
4782 .open = alt_playback_pcm_open,
4783 .close = alt_playback_pcm_close,
4784 .prepare = alt_playback_pcm_prepare,
4785 .cleanup = alt_playback_pcm_cleanup
4789 static const struct hda_pcm_stream pcm_analog_alt_capture = {
4790 .substreams = 2, /* can be overridden */
4793 /* NID is set in build_pcms */
4795 .open = alt_capture_pcm_open,
4796 .close = alt_capture_pcm_close,
4797 .prepare = alt_capture_pcm_prepare,
4798 .cleanup = alt_capture_pcm_cleanup
4802 static const struct hda_pcm_stream pcm_digital_playback = {
4806 /* NID is set in build_pcms */
4808 .open = dig_playback_pcm_open,
4809 .close = dig_playback_pcm_close,
4810 .prepare = dig_playback_pcm_prepare,
4811 .cleanup = dig_playback_pcm_cleanup
4815 static const struct hda_pcm_stream pcm_digital_capture = {
4819 /* NID is set in build_pcms */
4822 /* Used by build_pcms to flag that a PCM has no playback stream */
4823 static const struct hda_pcm_stream pcm_null_stream = {
4830 * dynamic changing ADC PCM streams
4832 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4834 struct hda_gen_spec *spec = codec->spec;
4835 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4837 if (spec->cur_adc && spec->cur_adc != new_adc) {
4838 /* stream is running, let's swap the current ADC */
4839 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4840 spec->cur_adc = new_adc;
4841 snd_hda_codec_setup_stream(codec, new_adc,
4842 spec->cur_adc_stream_tag, 0,
4843 spec->cur_adc_format);
4849 /* analog capture with dynamic dual-adc changes */
4850 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4851 struct hda_codec *codec,
4852 unsigned int stream_tag,
4853 unsigned int format,
4854 struct snd_pcm_substream *substream)
4856 struct hda_gen_spec *spec = codec->spec;
4857 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4858 spec->cur_adc_stream_tag = stream_tag;
4859 spec->cur_adc_format = format;
4860 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4864 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4865 struct hda_codec *codec,
4866 struct snd_pcm_substream *substream)
4868 struct hda_gen_spec *spec = codec->spec;
4869 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4874 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4878 .nid = 0, /* fill later */
4880 .prepare = dyn_adc_capture_pcm_prepare,
4881 .cleanup = dyn_adc_capture_pcm_cleanup
4885 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4886 const char *chip_name)
4892 strlcpy(str, chip_name, len);
4894 /* drop non-alnum chars after a space */
4895 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4896 if (!isalnum(p[1])) {
4901 strlcat(str, sfx, len);
4904 /* build PCM streams based on the parsed results */
4905 int snd_hda_gen_build_pcms(struct hda_codec *codec)
4907 struct hda_gen_spec *spec = codec->spec;
4908 struct hda_pcm *info = spec->pcm_rec;
4909 const struct hda_pcm_stream *p;
4910 bool have_multi_adcs;
4912 codec->num_pcms = 1;
4913 codec->pcm_info = info;
4915 if (spec->no_analog)
4918 fill_pcm_stream_name(spec->stream_name_analog,
4919 sizeof(spec->stream_name_analog),
4920 " Analog", codec->chip_name);
4921 info->name = spec->stream_name_analog;
4923 if (spec->multiout.num_dacs > 0) {
4924 p = spec->stream_analog_playback;
4926 p = &pcm_analog_playback;
4927 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4928 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4929 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4930 spec->multiout.max_channels;
4931 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4932 spec->autocfg.line_outs == 2)
4933 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4936 if (spec->num_adc_nids) {
4937 p = spec->stream_analog_capture;
4939 if (spec->dyn_adc_switch)
4940 p = &dyn_adc_pcm_analog_capture;
4942 p = &pcm_analog_capture;
4944 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4945 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4949 /* SPDIF for stream index #1 */
4950 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
4951 fill_pcm_stream_name(spec->stream_name_digital,
4952 sizeof(spec->stream_name_digital),
4953 " Digital", codec->chip_name);
4954 codec->num_pcms = 2;
4955 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4956 info = spec->pcm_rec + 1;
4957 info->name = spec->stream_name_digital;
4958 if (spec->dig_out_type)
4959 info->pcm_type = spec->dig_out_type;
4961 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4962 if (spec->multiout.dig_out_nid) {
4963 p = spec->stream_digital_playback;
4965 p = &pcm_digital_playback;
4966 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4967 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4969 if (spec->dig_in_nid) {
4970 p = spec->stream_digital_capture;
4972 p = &pcm_digital_capture;
4973 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4974 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4978 if (spec->no_analog)
4981 /* If the use of more than one ADC is requested for the current
4982 * model, configure a second analog capture-only PCM.
4984 have_multi_adcs = (spec->num_adc_nids > 1) &&
4985 !spec->dyn_adc_switch && !spec->auto_mic;
4986 /* Additional Analaog capture for index #2 */
4987 if (spec->alt_dac_nid || have_multi_adcs) {
4988 fill_pcm_stream_name(spec->stream_name_alt_analog,
4989 sizeof(spec->stream_name_alt_analog),
4990 " Alt Analog", codec->chip_name);
4991 codec->num_pcms = 3;
4992 info = spec->pcm_rec + 2;
4993 info->name = spec->stream_name_alt_analog;
4994 if (spec->alt_dac_nid) {
4995 p = spec->stream_analog_alt_playback;
4997 p = &pcm_analog_alt_playback;
4998 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4999 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
5002 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
5004 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
5006 if (have_multi_adcs) {
5007 p = spec->stream_analog_alt_capture;
5009 p = &pcm_analog_alt_capture;
5010 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
5011 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
5013 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
5014 spec->num_adc_nids - 1;
5016 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
5018 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
5024 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
5028 * Standard auto-parser initializations
5031 /* configure the given path as a proper output */
5032 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
5034 struct nid_path *path;
5037 path = snd_hda_get_path_from_idx(codec, path_idx);
5038 if (!path || !path->depth)
5040 pin = path->path[path->depth - 1];
5041 restore_pin_ctl(codec, pin);
5042 snd_hda_activate_path(codec, path, path->active,
5043 aamix_default(codec->spec));
5044 set_pin_eapd(codec, pin, path->active);
5047 /* initialize primary output paths */
5048 static void init_multi_out(struct hda_codec *codec)
5050 struct hda_gen_spec *spec = codec->spec;
5053 for (i = 0; i < spec->autocfg.line_outs; i++)
5054 set_output_and_unmute(codec, spec->out_paths[i]);
5058 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
5062 for (i = 0; i < num_outs; i++)
5063 set_output_and_unmute(codec, paths[i]);
5066 /* initialize hp and speaker paths */
5067 static void init_extra_out(struct hda_codec *codec)
5069 struct hda_gen_spec *spec = codec->spec;
5071 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
5072 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
5073 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
5074 __init_extra_out(codec, spec->autocfg.speaker_outs,
5075 spec->speaker_paths);
5078 /* initialize multi-io paths */
5079 static void init_multi_io(struct hda_codec *codec)
5081 struct hda_gen_spec *spec = codec->spec;
5084 for (i = 0; i < spec->multi_ios; i++) {
5085 hda_nid_t pin = spec->multi_io[i].pin;
5086 struct nid_path *path;
5087 path = get_multiio_path(codec, i);
5090 if (!spec->multi_io[i].ctl_in)
5091 spec->multi_io[i].ctl_in =
5092 snd_hda_codec_get_pin_target(codec, pin);
5093 snd_hda_activate_path(codec, path, path->active,
5094 aamix_default(spec));
5098 /* set up input pins and loopback paths */
5099 static void init_analog_input(struct hda_codec *codec)
5101 struct hda_gen_spec *spec = codec->spec;
5102 struct auto_pin_cfg *cfg = &spec->autocfg;
5105 for (i = 0; i < cfg->num_inputs; i++) {
5106 hda_nid_t nid = cfg->inputs[i].pin;
5107 if (is_input_pin(codec, nid))
5108 restore_pin_ctl(codec, nid);
5110 /* init loopback inputs */
5111 if (spec->mixer_nid) {
5112 resume_path_from_idx(codec, spec->loopback_paths[i]);
5113 resume_path_from_idx(codec, spec->loopback_merge_path);
5118 /* initialize ADC paths */
5119 static void init_input_src(struct hda_codec *codec)
5121 struct hda_gen_spec *spec = codec->spec;
5122 struct hda_input_mux *imux = &spec->input_mux;
5123 struct nid_path *path;
5126 if (spec->dyn_adc_switch)
5129 nums = spec->num_adc_nids;
5131 for (c = 0; c < nums; c++) {
5132 for (i = 0; i < imux->num_items; i++) {
5133 path = get_input_path(codec, c, i);
5135 bool active = path->active;
5136 if (i == spec->cur_mux[c])
5138 snd_hda_activate_path(codec, path, active, false);
5142 update_hp_mic(codec, c, true);
5145 if (spec->cap_sync_hook)
5146 spec->cap_sync_hook(codec, NULL);
5149 /* set right pin controls for digital I/O */
5150 static void init_digital(struct hda_codec *codec)
5152 struct hda_gen_spec *spec = codec->spec;
5156 for (i = 0; i < spec->autocfg.dig_outs; i++)
5157 set_output_and_unmute(codec, spec->digout_paths[i]);
5158 pin = spec->autocfg.dig_in_pin;
5160 restore_pin_ctl(codec, pin);
5161 resume_path_from_idx(codec, spec->digin_path);
5165 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5166 * invalid unsol tags by some reason
5168 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
5172 for (i = 0; i < codec->init_pins.used; i++) {
5173 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
5174 hda_nid_t nid = pin->nid;
5175 if (is_jack_detectable(codec, nid) &&
5176 !snd_hda_jack_tbl_get(codec, nid))
5177 snd_hda_codec_update_cache(codec, nid, 0,
5178 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
5183 * initialize the generic spec;
5184 * this can be put as patch_ops.init function
5186 int snd_hda_gen_init(struct hda_codec *codec)
5188 struct hda_gen_spec *spec = codec->spec;
5190 if (spec->init_hook)
5191 spec->init_hook(codec);
5193 snd_hda_apply_verbs(codec);
5195 codec->cached_write = 1;
5197 init_multi_out(codec);
5198 init_extra_out(codec);
5199 init_multi_io(codec);
5200 init_analog_input(codec);
5201 init_input_src(codec);
5202 init_digital(codec);
5204 clear_unsol_on_unused_pins(codec);
5206 /* call init functions of standard auto-mute helpers */
5207 update_automute_all(codec);
5209 snd_hda_codec_flush_cache(codec);
5211 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
5212 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
5214 hda_call_check_power_status(codec, 0x01);
5217 EXPORT_SYMBOL_HDA(snd_hda_gen_init);
5220 * free the generic spec;
5221 * this can be put as patch_ops.free function
5223 void snd_hda_gen_free(struct hda_codec *codec)
5225 snd_hda_detach_beep_device(codec);
5226 snd_hda_gen_spec_free(codec->spec);
5230 EXPORT_SYMBOL_HDA(snd_hda_gen_free);
5234 * check the loopback power save state;
5235 * this can be put as patch_ops.check_power_status function
5237 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
5239 struct hda_gen_spec *spec = codec->spec;
5240 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
5242 EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
5247 * the generic codec support
5250 static const struct hda_codec_ops generic_patch_ops = {
5251 .build_controls = snd_hda_gen_build_controls,
5252 .build_pcms = snd_hda_gen_build_pcms,
5253 .init = snd_hda_gen_init,
5254 .free = snd_hda_gen_free,
5255 .unsol_event = snd_hda_jack_unsol_event,
5257 .check_power_status = snd_hda_gen_check_power_status,
5261 int snd_hda_parse_generic_codec(struct hda_codec *codec)
5263 struct hda_gen_spec *spec;
5266 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
5269 snd_hda_gen_spec_init(spec);
5272 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
5276 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
5280 codec->patch_ops = generic_patch_ops;
5284 snd_hda_gen_free(codec);
5287 EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);