2 * Universal Interface for Intel High Definition Audio Codec
4 * Generic widget tree parser
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/sort.h>
27 #include <linux/ctype.h>
28 #include <linux/string.h>
29 #include <sound/core.h>
30 #include <sound/jack.h>
31 #include "hda_codec.h"
32 #include "hda_local.h"
33 #include "hda_auto_parser.h"
35 #include "hda_generic.h"
38 /* initialize hda_gen_spec struct */
39 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
41 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
42 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
43 mutex_init(&spec->pcm_mutex);
46 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
48 struct snd_kcontrol_new *
49 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
50 const struct snd_kcontrol_new *temp)
52 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
57 knew->name = kstrdup(name, GFP_KERNEL);
59 knew->name = kstrdup(knew->name, GFP_KERNEL);
64 EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
66 static void free_kctls(struct hda_gen_spec *spec)
68 if (spec->kctls.list) {
69 struct snd_kcontrol_new *kctl = spec->kctls.list;
71 for (i = 0; i < spec->kctls.used; i++)
74 snd_array_free(&spec->kctls);
77 void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
82 snd_array_free(&spec->paths);
84 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
90 /* return the position of NID in the list, or -1 if not found */
91 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
94 for (i = 0; i < nums; i++)
100 /* return true if the given NID is contained in the path */
101 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
103 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
106 static struct nid_path *get_nid_path(struct hda_codec *codec,
107 hda_nid_t from_nid, hda_nid_t to_nid,
110 struct hda_gen_spec *spec = codec->spec;
113 for (i = 0; i < spec->paths.used; i++) {
114 struct nid_path *path = snd_array_elem(&spec->paths, i);
115 if (path->depth <= 0)
117 if ((!from_nid || path->path[0] == from_nid) &&
118 (!to_nid || path->path[path->depth - 1] == to_nid)) {
120 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
121 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
128 /* get the path between the given NIDs;
129 * passing 0 to either @pin or @dac behaves as a wildcard
131 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
132 hda_nid_t from_nid, hda_nid_t to_nid)
134 return get_nid_path(codec, from_nid, to_nid, 0);
136 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
138 /* get the index number corresponding to the path instance;
139 * the index starts from 1, for easier checking the invalid value
141 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
143 struct hda_gen_spec *spec = codec->spec;
144 struct nid_path *array = spec->paths.list;
147 if (!spec->paths.used)
150 if (idx < 0 || idx >= spec->paths.used)
155 /* get the path instance corresponding to the given index number */
156 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
158 struct hda_gen_spec *spec = codec->spec;
160 if (idx <= 0 || idx > spec->paths.used)
162 return snd_array_elem(&spec->paths, idx - 1);
165 /* check whether the given DAC is already found in any existing paths */
166 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
168 struct hda_gen_spec *spec = codec->spec;
171 for (i = 0; i < spec->paths.used; i++) {
172 struct nid_path *path = snd_array_elem(&spec->paths, i);
173 if (path->path[0] == nid)
179 /* check whether the given two widgets can be connected */
180 static bool is_reachable_path(struct hda_codec *codec,
181 hda_nid_t from_nid, hda_nid_t to_nid)
183 if (!from_nid || !to_nid)
185 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
188 /* nid, dir and idx */
189 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
191 /* check whether the given ctl is already assigned in any path elements */
192 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
194 struct hda_gen_spec *spec = codec->spec;
197 val &= AMP_VAL_COMPARE_MASK;
198 for (i = 0; i < spec->paths.used; i++) {
199 struct nid_path *path = snd_array_elem(&spec->paths, i);
200 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
206 /* check whether a control with the given (nid, dir, idx) was assigned */
207 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
210 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
211 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
212 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
215 static void print_nid_path(const char *pfx, struct nid_path *path)
222 for (i = 0; i < path->depth; i++) {
224 sprintf(tmp, ":%02x", path->path[i]);
225 strlcat(buf, tmp, sizeof(buf));
227 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
230 /* called recursively */
231 static bool __parse_nid_path(struct hda_codec *codec,
232 hda_nid_t from_nid, hda_nid_t to_nid,
233 int anchor_nid, struct nid_path *path,
236 const hda_nid_t *conn;
239 if (to_nid == anchor_nid)
240 anchor_nid = 0; /* anchor passed */
241 else if (to_nid == (hda_nid_t)(-anchor_nid))
242 return false; /* hit the exclusive nid */
244 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
245 for (i = 0; i < nums; i++) {
246 if (conn[i] != from_nid) {
247 /* special case: when from_nid is 0,
248 * try to find an empty DAC
251 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
252 is_dac_already_used(codec, conn[i]))
255 /* anchor is not requested or already passed? */
259 if (depth >= MAX_NID_PATH_DEPTH)
261 for (i = 0; i < nums; i++) {
263 type = get_wcaps_type(get_wcaps(codec, conn[i]));
264 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
267 if (__parse_nid_path(codec, from_nid, conn[i],
268 anchor_nid, path, depth + 1))
274 path->path[path->depth] = conn[i];
275 path->idx[path->depth + 1] = i;
276 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
277 path->multi[path->depth + 1] = 1;
282 /* parse the widget path from the given nid to the target nid;
283 * when @from_nid is 0, try to find an empty DAC;
284 * when @anchor_nid is set to a positive value, only paths through the widget
285 * with the given value are evaluated.
286 * when @anchor_nid is set to a negative value, paths through the widget
287 * with the negative of given value are excluded, only other paths are chosen.
288 * when @anchor_nid is zero, no special handling about path selection.
290 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
291 hda_nid_t to_nid, int anchor_nid,
292 struct nid_path *path)
294 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
295 path->path[path->depth] = to_nid;
301 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
304 * parse the path between the given NIDs and add to the path list.
305 * if no valid path is found, return NULL
308 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
309 hda_nid_t to_nid, int anchor_nid)
311 struct hda_gen_spec *spec = codec->spec;
312 struct nid_path *path;
314 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
317 /* check whether the path has been already added */
318 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
322 path = snd_array_new(&spec->paths);
325 memset(path, 0, sizeof(*path));
326 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
332 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
334 /* look for an empty DAC slot */
335 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
338 struct hda_gen_spec *spec = codec->spec;
342 for (i = 0; i < spec->num_all_dacs; i++) {
343 hda_nid_t nid = spec->all_dacs[i];
344 if (!nid || is_dac_already_used(codec, nid))
346 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
347 if (is_digital != cap_digital)
349 if (is_reachable_path(codec, nid, pin))
355 /* replace the channels in the composed amp value with the given number */
356 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
358 val &= ~(0x3U << 16);
363 /* check whether the widget has the given amp capability for the direction */
364 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
365 int dir, unsigned int bits)
369 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
370 if (query_amp_caps(codec, nid, dir) & bits)
375 #define nid_has_mute(codec, nid, dir) \
376 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
377 #define nid_has_volume(codec, nid, dir) \
378 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
380 /* look for a widget suitable for assigning a mute switch in the path */
381 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
382 struct nid_path *path)
386 for (i = path->depth - 1; i >= 0; i--) {
387 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
388 return path->path[i];
389 if (i != path->depth - 1 && i != 0 &&
390 nid_has_mute(codec, path->path[i], HDA_INPUT))
391 return path->path[i];
396 /* look for a widget suitable for assigning a volume ctl in the path */
397 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
398 struct nid_path *path)
402 for (i = path->depth - 1; i >= 0; i--) {
403 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
404 return path->path[i];
410 * path activation / deactivation
413 /* can have the amp-in capability? */
414 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
416 hda_nid_t nid = path->path[idx];
417 unsigned int caps = get_wcaps(codec, nid);
418 unsigned int type = get_wcaps_type(caps);
420 if (!(caps & AC_WCAP_IN_AMP))
422 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
427 /* can have the amp-out capability? */
428 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
430 hda_nid_t nid = path->path[idx];
431 unsigned int caps = get_wcaps(codec, nid);
432 unsigned int type = get_wcaps_type(caps);
434 if (!(caps & AC_WCAP_OUT_AMP))
436 if (type == AC_WID_PIN && !idx) /* only for output pins */
441 /* check whether the given (nid,dir,idx) is active */
442 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
443 unsigned int idx, unsigned int dir)
445 struct hda_gen_spec *spec = codec->spec;
448 for (n = 0; n < spec->paths.used; n++) {
449 struct nid_path *path = snd_array_elem(&spec->paths, n);
452 for (i = 0; i < path->depth; i++) {
453 if (path->path[i] == nid) {
454 if (dir == HDA_OUTPUT || path->idx[i] == idx)
463 /* get the default amp value for the target state */
464 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
465 int dir, bool enable)
468 unsigned int val = 0;
470 caps = query_amp_caps(codec, nid, dir);
471 if (caps & AC_AMPCAP_NUM_STEPS) {
474 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
476 if (caps & AC_AMPCAP_MUTE) {
483 /* initialize the amp value (only at the first time) */
484 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
486 int val = get_amp_val_to_activate(codec, nid, dir, false);
487 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
490 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
491 int idx, bool enable)
494 if (is_ctl_associated(codec, nid, dir, idx) ||
495 (!enable && is_active_nid(codec, nid, dir, idx)))
497 val = get_amp_val_to_activate(codec, nid, dir, enable);
498 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
501 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
504 hda_nid_t nid = path->path[i];
505 init_amp(codec, nid, HDA_OUTPUT, 0);
506 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
509 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
510 int i, bool enable, bool add_aamix)
512 struct hda_gen_spec *spec = codec->spec;
513 const hda_nid_t *conn;
516 hda_nid_t nid = path->path[i];
518 nums = snd_hda_get_conn_list(codec, nid, &conn);
519 type = get_wcaps_type(get_wcaps(codec, nid));
520 if (type == AC_WID_PIN ||
521 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
527 for (n = 0; n < nums; n++)
528 init_amp(codec, nid, HDA_INPUT, n);
530 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
533 /* here is a little bit tricky in comparison with activate_amp_out();
534 * when aa-mixer is available, we need to enable the path as well
536 for (n = 0; n < nums; n++) {
537 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
539 activate_amp(codec, nid, HDA_INPUT, n, enable);
543 /* activate or deactivate the given path
544 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
546 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
547 bool enable, bool add_aamix)
552 path->active = false;
554 for (i = path->depth - 1; i >= 0; i--) {
555 if (enable && path->multi[i])
556 snd_hda_codec_write_cache(codec, path->path[i], 0,
557 AC_VERB_SET_CONNECT_SEL,
559 if (has_amp_in(codec, path, i))
560 activate_amp_in(codec, path, i, enable, add_aamix);
561 if (has_amp_out(codec, path, i))
562 activate_amp_out(codec, path, i, enable);
568 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
570 /* turn on/off EAPD on the given pin */
571 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
573 struct hda_gen_spec *spec = codec->spec;
574 if (spec->own_eapd_ctl ||
575 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
579 snd_hda_codec_update_cache(codec, pin, 0,
580 AC_VERB_SET_EAPD_BTLENABLE,
581 enable ? 0x02 : 0x00);
586 * Helper functions for creating mixer ctl elements
594 static const struct snd_kcontrol_new control_templates[] = {
595 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
596 HDA_CODEC_MUTE(NULL, 0, 0, 0),
597 HDA_BIND_MUTE(NULL, 0, 0, 0),
600 /* add dynamic controls from template */
601 static int add_control(struct hda_gen_spec *spec, int type, const char *name,
602 int cidx, unsigned long val)
604 struct snd_kcontrol_new *knew;
606 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
610 if (get_amp_nid_(val))
611 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
612 knew->private_value = val;
616 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
617 const char *pfx, const char *dir,
618 const char *sfx, int cidx, unsigned long val)
621 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
622 return add_control(spec, type, name, cidx, val);
625 #define add_pb_vol_ctrl(spec, type, pfx, val) \
626 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
627 #define add_pb_sw_ctrl(spec, type, pfx, val) \
628 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
629 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
630 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
631 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
632 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
634 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
635 unsigned int chs, struct nid_path *path)
640 val = path->ctls[NID_PATH_VOL_CTL];
643 val = amp_val_replace_channels(val, chs);
644 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
647 /* return the channel bits suitable for the given path->ctls[] */
648 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
651 int chs = 1; /* mono (left only) */
653 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
654 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
655 chs = 3; /* stereo */
660 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
661 struct nid_path *path)
663 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
664 return add_vol_ctl(codec, pfx, cidx, chs, path);
667 /* create a mute-switch for the given mixer widget;
668 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
670 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
671 unsigned int chs, struct nid_path *path)
674 int type = HDA_CTL_WIDGET_MUTE;
678 val = path->ctls[NID_PATH_MUTE_CTL];
681 val = amp_val_replace_channels(val, chs);
682 if (get_amp_direction_(val) == HDA_INPUT) {
683 hda_nid_t nid = get_amp_nid_(val);
684 int nums = snd_hda_get_num_conns(codec, nid);
686 type = HDA_CTL_BIND_MUTE;
690 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
693 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
694 int cidx, struct nid_path *path)
696 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
697 return add_sw_ctl(codec, pfx, cidx, chs, path);
700 static const char * const channel_name[4] = {
701 "Front", "Surround", "CLFE", "Side"
704 /* give some appropriate ctl name prefix for the given line out channel */
705 static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
706 bool can_be_master, int *index)
708 struct auto_pin_cfg *cfg = &spec->autocfg;
711 if (cfg->line_outs == 1 && !spec->multi_ios &&
712 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
713 return spec->vmaster_mute.hook ? "PCM" : "Master";
715 /* if there is really a single DAC used in the whole output paths,
716 * use it master (or "PCM" if a vmaster hook is present)
718 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
719 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
720 return spec->vmaster_mute.hook ? "PCM" : "Master";
722 switch (cfg->line_out_type) {
723 case AUTO_PIN_SPEAKER_OUT:
724 if (cfg->line_outs == 1)
726 if (cfg->line_outs == 2)
727 return ch ? "Bass Speaker" : "Speaker";
729 case AUTO_PIN_HP_OUT:
730 /* for multi-io case, only the primary out */
731 if (ch && spec->multi_ios)
736 if (cfg->line_outs == 1 && !spec->multi_ios)
740 if (ch >= ARRAY_SIZE(channel_name)) {
745 return channel_name[ch];
752 /* badness definition */
754 /* No primary DAC is found for the main output */
755 BAD_NO_PRIMARY_DAC = 0x10000,
756 /* No DAC is found for the extra output */
758 /* No possible multi-ios */
759 BAD_MULTI_IO = 0x103,
760 /* No individual DAC for extra output */
761 BAD_NO_EXTRA_DAC = 0x102,
762 /* No individual DAC for extra surrounds */
763 BAD_NO_EXTRA_SURR_DAC = 0x101,
764 /* Primary DAC shared with main surrounds */
765 BAD_SHARED_SURROUND = 0x100,
766 /* Primary DAC shared with main CLFE */
767 BAD_SHARED_CLFE = 0x10,
768 /* Primary DAC shared with extra surrounds */
769 BAD_SHARED_EXTRA_SURROUND = 0x10,
770 /* Volume widget is shared */
771 BAD_SHARED_VOL = 0x10,
774 /* look for widgets in the given path which are appropriate for
775 * volume and mute controls, and assign the values to ctls[].
777 * When no appropriate widget is found in the path, the badness value
778 * is incremented depending on the situation. The function returns the
779 * total badness for both volume and mute controls.
781 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
788 return BAD_SHARED_VOL * 2;
790 if (path->ctls[NID_PATH_VOL_CTL] ||
791 path->ctls[NID_PATH_MUTE_CTL])
792 return 0; /* already evaluated */
794 nid = look_for_out_vol_nid(codec, path);
796 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
797 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
798 badness += BAD_SHARED_VOL;
800 path->ctls[NID_PATH_VOL_CTL] = val;
802 badness += BAD_SHARED_VOL;
803 nid = look_for_out_mute_nid(codec, path);
805 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
806 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
807 nid_has_mute(codec, nid, HDA_OUTPUT))
808 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
810 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
811 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
812 badness += BAD_SHARED_VOL;
814 path->ctls[NID_PATH_MUTE_CTL] = val;
816 badness += BAD_SHARED_VOL;
820 struct badness_table {
821 int no_primary_dac; /* no primary DAC */
822 int no_dac; /* no secondary DACs */
823 int shared_primary; /* primary DAC is shared with main output */
824 int shared_surr; /* secondary DAC shared with main or primary */
825 int shared_clfe; /* third DAC shared with main or primary */
826 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
829 static struct badness_table main_out_badness = {
830 .no_primary_dac = BAD_NO_PRIMARY_DAC,
831 .no_dac = BAD_NO_DAC,
832 .shared_primary = BAD_NO_PRIMARY_DAC,
833 .shared_surr = BAD_SHARED_SURROUND,
834 .shared_clfe = BAD_SHARED_CLFE,
835 .shared_surr_main = BAD_SHARED_SURROUND,
838 static struct badness_table extra_out_badness = {
839 .no_primary_dac = BAD_NO_DAC,
840 .no_dac = BAD_NO_DAC,
841 .shared_primary = BAD_NO_EXTRA_DAC,
842 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
843 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
844 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
847 /* get the DAC of the primary output corresponding to the given array index */
848 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
850 struct hda_gen_spec *spec = codec->spec;
851 struct auto_pin_cfg *cfg = &spec->autocfg;
853 if (cfg->line_outs > idx)
854 return spec->private_dac_nids[idx];
855 idx -= cfg->line_outs;
856 if (spec->multi_ios > idx)
857 return spec->multi_io[idx].dac;
861 /* return the DAC if it's reachable, otherwise zero */
862 static inline hda_nid_t try_dac(struct hda_codec *codec,
863 hda_nid_t dac, hda_nid_t pin)
865 return is_reachable_path(codec, dac, pin) ? dac : 0;
868 /* try to assign DACs to pins and return the resultant badness */
869 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
870 const hda_nid_t *pins, hda_nid_t *dacs,
872 const struct badness_table *bad)
874 struct hda_gen_spec *spec = codec->spec;
882 for (i = 0; i < num_outs; i++) {
883 struct nid_path *path;
884 hda_nid_t pin = pins[i];
886 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
888 badness += assign_out_path_ctls(codec, path);
892 dacs[i] = look_for_dac(codec, pin, false);
893 if (!dacs[i] && !i) {
894 for (j = 1; j < num_outs; j++) {
895 if (is_reachable_path(codec, dacs[j], pin)) {
906 dac = try_dac(codec, get_primary_out(codec, i), pin);
908 dac = try_dac(codec, dacs[0], pin);
910 dac = try_dac(codec, get_primary_out(codec, i), pin);
913 badness += bad->shared_primary;
915 badness += bad->shared_surr;
917 badness += bad->shared_clfe;
918 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
919 dac = spec->private_dac_nids[0];
920 badness += bad->shared_surr_main;
922 badness += bad->no_primary_dac;
924 badness += bad->no_dac;
926 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
927 if (!path && !i && spec->mixer_nid) {
929 path = snd_hda_add_new_path(codec, dac, pin, 0);
934 print_nid_path("output", path);
936 path_idx[i] = snd_hda_get_path_idx(codec, path);
937 badness += assign_out_path_ctls(codec, path);
944 /* return NID if the given pin has only a single connection to a certain DAC */
945 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
947 struct hda_gen_spec *spec = codec->spec;
949 hda_nid_t nid_found = 0;
951 for (i = 0; i < spec->num_all_dacs; i++) {
952 hda_nid_t nid = spec->all_dacs[i];
953 if (!nid || is_dac_already_used(codec, nid))
955 if (is_reachable_path(codec, nid, pin)) {
964 /* check whether the given pin can be a multi-io pin */
965 static bool can_be_multiio_pin(struct hda_codec *codec,
966 unsigned int location, hda_nid_t nid)
968 unsigned int defcfg, caps;
970 defcfg = snd_hda_codec_get_pincfg(codec, nid);
971 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
973 if (location && get_defcfg_location(defcfg) != location)
975 caps = snd_hda_query_pin_caps(codec, nid);
976 if (!(caps & AC_PINCAP_OUT))
981 /* count the number of input pins that are capable to be multi-io */
982 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
984 struct hda_gen_spec *spec = codec->spec;
985 struct auto_pin_cfg *cfg = &spec->autocfg;
986 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
987 unsigned int location = get_defcfg_location(defcfg);
991 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
992 for (i = 0; i < cfg->num_inputs; i++) {
993 if (cfg->inputs[i].type != type)
995 if (can_be_multiio_pin(codec, location,
1006 * When hardwired is set, try to fill ony hardwired pins, and returns
1007 * zero if any pins are filled, non-zero if nothing found.
1008 * When hardwired is off, try to fill possible input pins, and returns
1009 * the badness value.
1011 static int fill_multi_ios(struct hda_codec *codec,
1012 hda_nid_t reference_pin,
1015 struct hda_gen_spec *spec = codec->spec;
1016 struct auto_pin_cfg *cfg = &spec->autocfg;
1017 int type, i, j, num_pins, old_pins;
1018 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1019 unsigned int location = get_defcfg_location(defcfg);
1021 struct nid_path *path;
1023 old_pins = spec->multi_ios;
1027 num_pins = count_multiio_pins(codec, reference_pin);
1031 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1032 for (i = 0; i < cfg->num_inputs; i++) {
1033 hda_nid_t nid = cfg->inputs[i].pin;
1036 if (cfg->inputs[i].type != type)
1038 if (!can_be_multiio_pin(codec, location, nid))
1040 for (j = 0; j < spec->multi_ios; j++) {
1041 if (nid == spec->multi_io[j].pin)
1044 if (j < spec->multi_ios)
1048 dac = get_dac_if_single(codec, nid);
1050 dac = look_for_dac(codec, nid, false);
1055 path = snd_hda_add_new_path(codec, dac, nid,
1061 print_nid_path("multiio", path);
1062 spec->multi_io[spec->multi_ios].pin = nid;
1063 spec->multi_io[spec->multi_ios].dac = dac;
1064 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1065 snd_hda_get_path_idx(codec, path);
1067 if (spec->multi_ios >= 2)
1073 badness = BAD_MULTI_IO;
1074 if (old_pins == spec->multi_ios) {
1076 return 1; /* nothing found */
1078 return badness; /* no badness if nothing found */
1080 if (!hardwired && spec->multi_ios < 2) {
1081 /* cancel newly assigned paths */
1082 spec->paths.used -= spec->multi_ios - old_pins;
1083 spec->multi_ios = old_pins;
1087 /* assign volume and mute controls */
1088 for (i = old_pins; i < spec->multi_ios; i++) {
1089 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1090 badness += assign_out_path_ctls(codec, path);
1096 /* map DACs for all pins in the list if they are single connections */
1097 static bool map_singles(struct hda_codec *codec, int outs,
1098 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1100 struct hda_gen_spec *spec = codec->spec;
1103 for (i = 0; i < outs; i++) {
1104 struct nid_path *path;
1108 dac = get_dac_if_single(codec, pins[i]);
1111 path = snd_hda_add_new_path(codec, dac, pins[i],
1113 if (!path && !i && spec->mixer_nid)
1114 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1118 print_nid_path("output", path);
1119 path->active = true;
1120 path_idx[i] = snd_hda_get_path_idx(codec, path);
1126 /* create a new path including aamix if available, and return its index */
1127 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1129 struct hda_gen_spec *spec = codec->spec;
1130 struct nid_path *path;
1132 path = snd_hda_get_path_from_idx(codec, path_idx);
1133 if (!path || !path->depth ||
1134 is_nid_contained(path, spec->mixer_nid))
1136 path = snd_hda_add_new_path(codec, path->path[0],
1137 path->path[path->depth - 1],
1141 print_nid_path("output-aamix", path);
1142 path->active = false; /* unused as default */
1143 return snd_hda_get_path_idx(codec, path);
1146 /* fill the empty entries in the dac array for speaker/hp with the
1147 * shared dac pointed by the paths
1149 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1150 hda_nid_t *dacs, int *path_idx)
1152 struct nid_path *path;
1155 for (i = 0; i < num_outs; i++) {
1158 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1161 dacs[i] = path->path[0];
1165 /* fill in the dac_nids table from the parsed pin configuration */
1166 static int fill_and_eval_dacs(struct hda_codec *codec,
1167 bool fill_hardwired,
1168 bool fill_mio_first)
1170 struct hda_gen_spec *spec = codec->spec;
1171 struct auto_pin_cfg *cfg = &spec->autocfg;
1172 int i, err, badness;
1174 /* set num_dacs once to full for look_for_dac() */
1175 spec->multiout.num_dacs = cfg->line_outs;
1176 spec->multiout.dac_nids = spec->private_dac_nids;
1177 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1178 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1179 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1180 spec->multi_ios = 0;
1181 snd_array_free(&spec->paths);
1183 /* clear path indices */
1184 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1185 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1186 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1187 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1188 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1189 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1190 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1191 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1195 /* fill hard-wired DACs first */
1196 if (fill_hardwired) {
1199 mapped = map_singles(codec, cfg->line_outs,
1201 spec->private_dac_nids,
1203 mapped |= map_singles(codec, cfg->hp_outs,
1205 spec->multiout.hp_out_nid,
1207 mapped |= map_singles(codec, cfg->speaker_outs,
1209 spec->multiout.extra_out_nid,
1210 spec->speaker_paths);
1211 if (fill_mio_first && cfg->line_outs == 1 &&
1212 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1213 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1220 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1221 spec->private_dac_nids, spec->out_paths,
1224 if (fill_mio_first &&
1225 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1226 /* try to fill multi-io first */
1227 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1230 /* we don't count badness at this stage yet */
1233 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1234 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1235 spec->multiout.hp_out_nid,
1237 &extra_out_badness);
1242 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1243 err = try_assign_dacs(codec, cfg->speaker_outs,
1245 spec->multiout.extra_out_nid,
1246 spec->speaker_paths,
1247 &extra_out_badness);
1252 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1253 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1259 if (spec->mixer_nid) {
1260 spec->aamix_out_paths[0] =
1261 check_aamix_out_path(codec, spec->out_paths[0]);
1262 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1263 spec->aamix_out_paths[1] =
1264 check_aamix_out_path(codec, spec->hp_paths[0]);
1265 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1266 spec->aamix_out_paths[2] =
1267 check_aamix_out_path(codec, spec->speaker_paths[0]);
1270 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1271 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1272 spec->multi_ios = 1; /* give badness */
1274 /* re-count num_dacs and squash invalid entries */
1275 spec->multiout.num_dacs = 0;
1276 for (i = 0; i < cfg->line_outs; i++) {
1277 if (spec->private_dac_nids[i])
1278 spec->multiout.num_dacs++;
1280 memmove(spec->private_dac_nids + i,
1281 spec->private_dac_nids + i + 1,
1282 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1283 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1287 spec->ext_channel_count = spec->min_channel_count =
1288 spec->multiout.num_dacs;
1290 if (spec->multi_ios == 2) {
1291 for (i = 0; i < 2; i++)
1292 spec->private_dac_nids[spec->multiout.num_dacs++] =
1293 spec->multi_io[i].dac;
1294 } else if (spec->multi_ios) {
1295 spec->multi_ios = 0;
1296 badness += BAD_MULTI_IO;
1299 /* re-fill the shared DAC for speaker / headphone */
1300 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1301 refill_shared_dacs(codec, cfg->hp_outs,
1302 spec->multiout.hp_out_nid,
1304 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1305 refill_shared_dacs(codec, cfg->speaker_outs,
1306 spec->multiout.extra_out_nid,
1307 spec->speaker_paths);
1312 #define DEBUG_BADNESS
1314 #ifdef DEBUG_BADNESS
1315 #define debug_badness snd_printdd
1317 #define debug_badness(...)
1320 static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1322 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1323 cfg->line_out_pins[0], cfg->line_out_pins[1],
1324 cfg->line_out_pins[2], cfg->line_out_pins[3],
1325 spec->multiout.dac_nids[0],
1326 spec->multiout.dac_nids[1],
1327 spec->multiout.dac_nids[2],
1328 spec->multiout.dac_nids[3]);
1329 if (spec->multi_ios > 0)
1330 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1332 spec->multi_io[0].pin, spec->multi_io[1].pin,
1333 spec->multi_io[0].dac, spec->multi_io[1].dac);
1334 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1335 cfg->hp_pins[0], cfg->hp_pins[1],
1336 cfg->hp_pins[2], cfg->hp_pins[3],
1337 spec->multiout.hp_out_nid[0],
1338 spec->multiout.hp_out_nid[1],
1339 spec->multiout.hp_out_nid[2],
1340 spec->multiout.hp_out_nid[3]);
1341 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1342 cfg->speaker_pins[0], cfg->speaker_pins[1],
1343 cfg->speaker_pins[2], cfg->speaker_pins[3],
1344 spec->multiout.extra_out_nid[0],
1345 spec->multiout.extra_out_nid[1],
1346 spec->multiout.extra_out_nid[2],
1347 spec->multiout.extra_out_nid[3]);
1350 /* find all available DACs of the codec */
1351 static void fill_all_dac_nids(struct hda_codec *codec)
1353 struct hda_gen_spec *spec = codec->spec;
1355 hda_nid_t nid = codec->start_nid;
1357 spec->num_all_dacs = 0;
1358 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1359 for (i = 0; i < codec->num_nodes; i++, nid++) {
1360 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1362 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1363 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1366 spec->all_dacs[spec->num_all_dacs++] = nid;
1370 static int parse_output_paths(struct hda_codec *codec)
1372 struct hda_gen_spec *spec = codec->spec;
1373 struct auto_pin_cfg *cfg = &spec->autocfg;
1374 struct auto_pin_cfg *best_cfg;
1375 int best_badness = INT_MAX;
1377 bool fill_hardwired = true, fill_mio_first = true;
1378 bool best_wired = true, best_mio = true;
1379 bool hp_spk_swapped = false;
1381 fill_all_dac_nids(codec);
1383 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1389 badness = fill_and_eval_dacs(codec, fill_hardwired,
1395 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1396 cfg->line_out_type, fill_hardwired, fill_mio_first,
1398 debug_show_configs(spec, cfg);
1399 if (badness < best_badness) {
1400 best_badness = badness;
1402 best_wired = fill_hardwired;
1403 best_mio = fill_mio_first;
1407 fill_mio_first = !fill_mio_first;
1408 if (!fill_mio_first)
1410 fill_hardwired = !fill_hardwired;
1411 if (!fill_hardwired)
1415 hp_spk_swapped = true;
1416 if (cfg->speaker_outs > 0 &&
1417 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1418 cfg->hp_outs = cfg->line_outs;
1419 memcpy(cfg->hp_pins, cfg->line_out_pins,
1420 sizeof(cfg->hp_pins));
1421 cfg->line_outs = cfg->speaker_outs;
1422 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1423 sizeof(cfg->speaker_pins));
1424 cfg->speaker_outs = 0;
1425 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1426 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1427 fill_hardwired = true;
1430 if (cfg->hp_outs > 0 &&
1431 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1432 cfg->speaker_outs = cfg->line_outs;
1433 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1434 sizeof(cfg->speaker_pins));
1435 cfg->line_outs = cfg->hp_outs;
1436 memcpy(cfg->line_out_pins, cfg->hp_pins,
1437 sizeof(cfg->hp_pins));
1439 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1440 cfg->line_out_type = AUTO_PIN_HP_OUT;
1441 fill_hardwired = true;
1448 debug_badness("==> restoring best_cfg\n");
1450 fill_and_eval_dacs(codec, best_wired, best_mio);
1452 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1453 cfg->line_out_type, best_wired, best_mio);
1454 debug_show_configs(spec, cfg);
1456 if (cfg->line_out_pins[0]) {
1457 struct nid_path *path;
1458 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1460 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1467 /* add playback controls from the parsed DAC table */
1468 static int create_multi_out_ctls(struct hda_codec *codec,
1469 const struct auto_pin_cfg *cfg)
1471 struct hda_gen_spec *spec = codec->spec;
1472 int i, err, noutputs;
1474 noutputs = cfg->line_outs;
1475 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1476 noutputs += spec->multi_ios;
1478 for (i = 0; i < noutputs; i++) {
1481 struct nid_path *path;
1483 if (i >= cfg->line_outs) {
1485 name = channel_name[i];
1487 name = get_line_out_pfx(spec, i, true, &index);
1490 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1493 if (!name || !strcmp(name, "CLFE")) {
1495 err = add_vol_ctl(codec, "Center", 0, 1, path);
1498 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1501 err = add_sw_ctl(codec, "Center", 0, 1, path);
1504 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1508 err = add_stereo_vol(codec, name, index, path);
1511 err = add_stereo_sw(codec, name, index, path);
1519 static int create_extra_out(struct hda_codec *codec, int path_idx,
1520 const char *pfx, int cidx)
1522 struct nid_path *path;
1525 path = snd_hda_get_path_from_idx(codec, path_idx);
1528 err = add_stereo_vol(codec, pfx, cidx, path);
1531 err = add_stereo_sw(codec, pfx, cidx, path);
1537 /* add playback controls for speaker and HP outputs */
1538 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1539 const int *paths, const char *pfx)
1543 for (i = 0; i < num_pins; i++) {
1548 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1549 name = "Bass Speaker";
1550 else if (num_pins >= 3) {
1551 snprintf(tmp, sizeof(tmp), "%s %s",
1552 pfx, channel_name[i]);
1558 err = create_extra_out(codec, paths[i], name, idx);
1565 static int create_hp_out_ctls(struct hda_codec *codec)
1567 struct hda_gen_spec *spec = codec->spec;
1568 return create_extra_outs(codec, spec->autocfg.hp_outs,
1573 static int create_speaker_out_ctls(struct hda_codec *codec)
1575 struct hda_gen_spec *spec = codec->spec;
1576 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1577 spec->speaker_paths,
1582 * independent HP controls
1585 static int indep_hp_info(struct snd_kcontrol *kcontrol,
1586 struct snd_ctl_elem_info *uinfo)
1588 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1591 static int indep_hp_get(struct snd_kcontrol *kcontrol,
1592 struct snd_ctl_elem_value *ucontrol)
1594 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1595 struct hda_gen_spec *spec = codec->spec;
1596 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1600 static int indep_hp_put(struct snd_kcontrol *kcontrol,
1601 struct snd_ctl_elem_value *ucontrol)
1603 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1604 struct hda_gen_spec *spec = codec->spec;
1605 unsigned int select = ucontrol->value.enumerated.item[0];
1608 mutex_lock(&spec->pcm_mutex);
1609 if (spec->active_streams) {
1614 if (spec->indep_hp_enabled != select) {
1615 spec->indep_hp_enabled = select;
1616 if (spec->indep_hp_enabled)
1617 spec->multiout.hp_out_nid[0] = 0;
1619 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1623 mutex_unlock(&spec->pcm_mutex);
1627 static const struct snd_kcontrol_new indep_hp_ctl = {
1628 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1629 .name = "Independent HP",
1630 .info = indep_hp_info,
1631 .get = indep_hp_get,
1632 .put = indep_hp_put,
1636 static int create_indep_hp_ctls(struct hda_codec *codec)
1638 struct hda_gen_spec *spec = codec->spec;
1640 if (!spec->indep_hp)
1642 if (!spec->multiout.hp_out_nid[0]) {
1647 spec->indep_hp_enabled = false;
1648 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1649 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1655 * channel mode enum control
1658 static int ch_mode_info(struct snd_kcontrol *kcontrol,
1659 struct snd_ctl_elem_info *uinfo)
1661 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1662 struct hda_gen_spec *spec = codec->spec;
1665 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1667 uinfo->value.enumerated.items = spec->multi_ios + 1;
1668 if (uinfo->value.enumerated.item > spec->multi_ios)
1669 uinfo->value.enumerated.item = spec->multi_ios;
1670 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
1671 sprintf(uinfo->value.enumerated.name, "%dch", chs);
1675 static int ch_mode_get(struct snd_kcontrol *kcontrol,
1676 struct snd_ctl_elem_value *ucontrol)
1678 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1679 struct hda_gen_spec *spec = codec->spec;
1680 ucontrol->value.enumerated.item[0] =
1681 (spec->ext_channel_count - spec->min_channel_count) / 2;
1685 static inline struct nid_path *
1686 get_multiio_path(struct hda_codec *codec, int idx)
1688 struct hda_gen_spec *spec = codec->spec;
1689 return snd_hda_get_path_from_idx(codec,
1690 spec->out_paths[spec->autocfg.line_outs + idx]);
1693 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1695 struct hda_gen_spec *spec = codec->spec;
1696 hda_nid_t nid = spec->multi_io[idx].pin;
1697 struct nid_path *path;
1699 path = get_multiio_path(codec, idx);
1703 if (path->active == output)
1707 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1708 snd_hda_activate_path(codec, path, true, true);
1709 set_pin_eapd(codec, nid, true);
1711 set_pin_eapd(codec, nid, false);
1712 snd_hda_activate_path(codec, path, false, true);
1713 snd_hda_set_pin_ctl_cache(codec, nid,
1714 spec->multi_io[idx].ctl_in);
1719 static int ch_mode_put(struct snd_kcontrol *kcontrol,
1720 struct snd_ctl_elem_value *ucontrol)
1722 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1723 struct hda_gen_spec *spec = codec->spec;
1726 ch = ucontrol->value.enumerated.item[0];
1727 if (ch < 0 || ch > spec->multi_ios)
1729 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
1731 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
1732 for (i = 0; i < spec->multi_ios; i++)
1733 set_multi_io(codec, i, i < ch);
1734 spec->multiout.max_channels = max(spec->ext_channel_count,
1735 spec->const_channel_count);
1736 if (spec->need_dac_fix)
1737 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1741 static const struct snd_kcontrol_new channel_mode_enum = {
1742 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1743 .name = "Channel Mode",
1744 .info = ch_mode_info,
1749 static int create_multi_channel_mode(struct hda_codec *codec)
1751 struct hda_gen_spec *spec = codec->spec;
1753 if (spec->multi_ios > 0) {
1754 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
1761 * aamix loopback enable/disable switch
1764 #define loopback_mixing_info indep_hp_info
1766 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1767 struct snd_ctl_elem_value *ucontrol)
1769 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1770 struct hda_gen_spec *spec = codec->spec;
1771 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1775 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1776 int nomix_path_idx, int mix_path_idx)
1778 struct nid_path *nomix_path, *mix_path;
1780 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1781 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1782 if (!nomix_path || !mix_path)
1785 snd_hda_activate_path(codec, nomix_path, false, true);
1786 snd_hda_activate_path(codec, mix_path, true, true);
1788 snd_hda_activate_path(codec, mix_path, false, true);
1789 snd_hda_activate_path(codec, nomix_path, true, true);
1793 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1794 struct snd_ctl_elem_value *ucontrol)
1796 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1797 struct hda_gen_spec *spec = codec->spec;
1798 unsigned int val = ucontrol->value.enumerated.item[0];
1800 if (val == spec->aamix_mode)
1802 spec->aamix_mode = val;
1803 update_aamix_paths(codec, val, spec->out_paths[0],
1804 spec->aamix_out_paths[0]);
1805 update_aamix_paths(codec, val, spec->hp_paths[0],
1806 spec->aamix_out_paths[1]);
1807 update_aamix_paths(codec, val, spec->speaker_paths[0],
1808 spec->aamix_out_paths[2]);
1812 static const struct snd_kcontrol_new loopback_mixing_enum = {
1813 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1814 .name = "Loopback Mixing",
1815 .info = loopback_mixing_info,
1816 .get = loopback_mixing_get,
1817 .put = loopback_mixing_put,
1820 static int create_loopback_mixing_ctl(struct hda_codec *codec)
1822 struct hda_gen_spec *spec = codec->spec;
1824 if (!spec->mixer_nid)
1826 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1827 spec->aamix_out_paths[2]))
1829 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1835 * shared headphone/mic handling
1838 static void call_update_outputs(struct hda_codec *codec);
1840 /* for shared I/O, change the pin-control accordingly */
1841 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1843 struct hda_gen_spec *spec = codec->spec;
1845 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1846 /* NOTE: this assumes that there are only two inputs, the
1847 * first is the real internal mic and the second is HP/mic jack.
1850 val = snd_hda_get_default_vref(codec, pin);
1852 /* This pin does not have vref caps - let's enable vref on pin 0x18
1853 instead, as suggested by Realtek */
1854 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1855 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1856 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1857 if (vref_val != AC_PINCTL_VREF_HIZ)
1858 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1859 PIN_IN | (set_as_mic ? vref_val : 0));
1862 val = set_as_mic ? val | PIN_IN : PIN_HP;
1863 snd_hda_set_pin_ctl_cache(codec, pin, val);
1865 spec->automute_speaker = !set_as_mic;
1866 call_update_outputs(codec);
1869 /* create a shared input with the headphone out */
1870 static int create_shared_input(struct hda_codec *codec)
1872 struct hda_gen_spec *spec = codec->spec;
1873 struct auto_pin_cfg *cfg = &spec->autocfg;
1874 unsigned int defcfg;
1877 /* only one internal input pin? */
1878 if (cfg->num_inputs != 1)
1880 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1881 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1884 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1885 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1886 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1887 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1889 return 0; /* both not available */
1891 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1892 return 0; /* no input */
1894 cfg->inputs[1].pin = nid;
1895 cfg->inputs[1].type = AUTO_PIN_MIC;
1896 cfg->num_inputs = 2;
1897 spec->shared_mic_hp = 1;
1898 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1908 /* add the powersave loopback-list entry */
1909 static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1911 struct hda_amp_list *list;
1913 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1915 list = spec->loopback_list + spec->num_loopbacks;
1917 list->dir = HDA_INPUT;
1919 spec->num_loopbacks++;
1920 spec->loopback.amplist = spec->loopback_list;
1923 #define add_loopback_list(spec, mix, idx) /* NOP */
1926 /* create input playback/capture controls for the given pin */
1927 static int new_analog_input(struct hda_codec *codec, int input_idx,
1928 hda_nid_t pin, const char *ctlname, int ctlidx,
1931 struct hda_gen_spec *spec = codec->spec;
1932 struct nid_path *path;
1936 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1937 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1938 return 0; /* no need for analog loopback */
1940 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
1943 print_nid_path("loopback", path);
1944 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
1946 idx = path->idx[path->depth - 1];
1947 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1948 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1949 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
1952 path->ctls[NID_PATH_VOL_CTL] = val;
1955 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1956 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1957 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
1960 path->ctls[NID_PATH_MUTE_CTL] = val;
1963 path->active = true;
1964 add_loopback_list(spec, mix_nid, idx);
1968 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
1970 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1971 return (pincap & AC_PINCAP_IN) != 0;
1974 /* Parse the codec tree and retrieve ADCs */
1975 static int fill_adc_nids(struct hda_codec *codec)
1977 struct hda_gen_spec *spec = codec->spec;
1979 hda_nid_t *adc_nids = spec->adc_nids;
1980 int max_nums = ARRAY_SIZE(spec->adc_nids);
1983 nid = codec->start_nid;
1984 for (i = 0; i < codec->num_nodes; i++, nid++) {
1985 unsigned int caps = get_wcaps(codec, nid);
1986 int type = get_wcaps_type(caps);
1988 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1990 adc_nids[nums] = nid;
1991 if (++nums >= max_nums)
1994 spec->num_adc_nids = nums;
1998 /* filter out invalid adc_nids that don't give all active input pins;
1999 * if needed, check whether dynamic ADC-switching is available
2001 static int check_dyn_adc_switch(struct hda_codec *codec)
2003 struct hda_gen_spec *spec = codec->spec;
2004 struct hda_input_mux *imux = &spec->input_mux;
2005 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
2011 for (n = 0; n < spec->num_adc_nids; n++) {
2012 adc = spec->adc_nids[n];
2013 for (i = 0; i < imux->num_items; i++) {
2014 pin = spec->imux_pins[i];
2015 if (!is_reachable_path(codec, pin, adc))
2018 if (i >= imux->num_items)
2019 adc_nids[nums++] = adc;
2023 if (spec->shared_mic_hp) {
2024 spec->shared_mic_hp = 0;
2025 imux->num_items = 1;
2029 /* check whether ADC-switch is possible */
2030 for (i = 0; i < imux->num_items; i++) {
2031 pin = spec->imux_pins[i];
2032 for (n = 0; n < spec->num_adc_nids; n++) {
2033 adc = spec->adc_nids[n];
2034 if (is_reachable_path(codec, pin, adc)) {
2035 spec->dyn_adc_idx[i] = n;
2041 snd_printdd("hda-codec: enabling ADC switching\n");
2042 spec->dyn_adc_switch = 1;
2043 } else if (nums != spec->num_adc_nids) {
2044 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
2045 spec->num_adc_nids = nums;
2048 if (imux->num_items == 1 || spec->shared_mic_hp) {
2049 snd_printdd("hda-codec: reducing to a single ADC\n");
2050 spec->num_adc_nids = 1; /* reduce to a single ADC */
2053 /* single index for individual volumes ctls */
2054 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2055 spec->num_adc_nids = 1;
2061 * create playback/capture controls for input pins
2063 static int create_input_ctls(struct hda_codec *codec)
2065 struct hda_gen_spec *spec = codec->spec;
2066 const struct auto_pin_cfg *cfg = &spec->autocfg;
2067 hda_nid_t mixer = spec->mixer_nid;
2068 struct hda_input_mux *imux = &spec->input_mux;
2070 int i, c, err, type_idx = 0;
2071 const char *prev_label = NULL;
2073 num_adcs = fill_adc_nids(codec);
2077 for (i = 0; i < cfg->num_inputs; i++) {
2083 pin = cfg->inputs[i].pin;
2084 if (!is_input_pin(codec, pin))
2087 label = hda_get_autocfg_input_label(codec, cfg, i);
2088 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2089 label = "Headphone Mic";
2090 if (prev_label && !strcmp(label, prev_label))
2097 if (is_reachable_path(codec, pin, mixer)) {
2098 err = new_analog_input(codec, i, pin,
2099 label, type_idx, mixer);
2106 imux_idx = imux->num_items;
2107 for (c = 0; c < num_adcs; c++) {
2108 struct nid_path *path;
2109 hda_nid_t adc = spec->adc_nids[c];
2111 if (!is_reachable_path(codec, pin, adc))
2113 path = snd_hda_add_new_path(codec, pin, adc, 0);
2116 "invalid input path 0x%x -> 0x%x\n",
2120 print_nid_path("input", path);
2121 spec->input_paths[imux_idx][c] =
2122 snd_hda_get_path_idx(codec, path);
2125 spec->imux_pins[imux->num_items] = pin;
2126 snd_hda_add_imux_item(imux, label,
2127 imux->num_items, NULL);
2141 /* get the input path specified by the given adc and imux indices */
2142 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
2144 struct hda_gen_spec *spec = codec->spec;
2145 if (spec->dyn_adc_switch)
2146 adc_idx = spec->dyn_adc_idx[imux_idx];
2147 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
2150 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2153 static int mux_enum_info(struct snd_kcontrol *kcontrol,
2154 struct snd_ctl_elem_info *uinfo)
2156 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2157 struct hda_gen_spec *spec = codec->spec;
2158 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2161 static int mux_enum_get(struct snd_kcontrol *kcontrol,
2162 struct snd_ctl_elem_value *ucontrol)
2164 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2165 struct hda_gen_spec *spec = codec->spec;
2166 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2168 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2172 static int mux_enum_put(struct snd_kcontrol *kcontrol,
2173 struct snd_ctl_elem_value *ucontrol)
2175 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2176 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2177 return mux_select(codec, adc_idx,
2178 ucontrol->value.enumerated.item[0]);
2181 static const struct snd_kcontrol_new cap_src_temp = {
2182 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2183 .name = "Input Source",
2184 .info = mux_enum_info,
2185 .get = mux_enum_get,
2186 .put = mux_enum_put,
2190 * capture volume and capture switch ctls
2193 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2194 struct snd_ctl_elem_value *ucontrol);
2196 /* call the given amp update function for all amps in the imux list at once */
2197 static int cap_put_caller(struct snd_kcontrol *kcontrol,
2198 struct snd_ctl_elem_value *ucontrol,
2199 put_call_t func, int type)
2201 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2202 struct hda_gen_spec *spec = codec->spec;
2203 const struct hda_input_mux *imux;
2204 struct nid_path *path;
2205 int i, adc_idx, err = 0;
2207 imux = &spec->input_mux;
2208 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2209 mutex_lock(&codec->control_mutex);
2210 /* we use the cache-only update at first since multiple input paths
2211 * may shared the same amp; by updating only caches, the redundant
2212 * writes to hardware can be reduced.
2214 codec->cached_write = 1;
2215 for (i = 0; i < imux->num_items; i++) {
2216 path = get_input_path(codec, adc_idx, i);
2217 if (!path || !path->ctls[type])
2219 kcontrol->private_value = path->ctls[type];
2220 err = func(kcontrol, ucontrol);
2225 codec->cached_write = 0;
2226 mutex_unlock(&codec->control_mutex);
2227 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
2228 if (err >= 0 && spec->cap_sync_hook)
2229 spec->cap_sync_hook(codec);
2233 /* capture volume ctl callbacks */
2234 #define cap_vol_info snd_hda_mixer_amp_volume_info
2235 #define cap_vol_get snd_hda_mixer_amp_volume_get
2236 #define cap_vol_tlv snd_hda_mixer_amp_tlv
2238 static int cap_vol_put(struct snd_kcontrol *kcontrol,
2239 struct snd_ctl_elem_value *ucontrol)
2241 return cap_put_caller(kcontrol, ucontrol,
2242 snd_hda_mixer_amp_volume_put,
2246 static const struct snd_kcontrol_new cap_vol_temp = {
2247 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2248 .name = "Capture Volume",
2249 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2250 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2251 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2252 .info = cap_vol_info,
2255 .tlv = { .c = cap_vol_tlv },
2258 /* capture switch ctl callbacks */
2259 #define cap_sw_info snd_ctl_boolean_stereo_info
2260 #define cap_sw_get snd_hda_mixer_amp_switch_get
2262 static int cap_sw_put(struct snd_kcontrol *kcontrol,
2263 struct snd_ctl_elem_value *ucontrol)
2265 return cap_put_caller(kcontrol, ucontrol,
2266 snd_hda_mixer_amp_switch_put,
2270 static const struct snd_kcontrol_new cap_sw_temp = {
2271 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2272 .name = "Capture Switch",
2273 .info = cap_sw_info,
2278 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2283 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2284 for (depth = 0; depth < 3; depth++) {
2285 if (depth >= path->depth)
2287 i = path->depth - depth - 1;
2288 nid = path->path[i];
2289 if (!path->ctls[NID_PATH_VOL_CTL]) {
2290 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2291 path->ctls[NID_PATH_VOL_CTL] =
2292 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2293 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2294 int idx = path->idx[i];
2295 if (!depth && codec->single_adc_amp)
2297 path->ctls[NID_PATH_VOL_CTL] =
2298 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2301 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2302 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2303 path->ctls[NID_PATH_MUTE_CTL] =
2304 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2305 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2306 int idx = path->idx[i];
2307 if (!depth && codec->single_adc_amp)
2309 path->ctls[NID_PATH_MUTE_CTL] =
2310 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2317 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2319 struct hda_gen_spec *spec = codec->spec;
2320 struct auto_pin_cfg *cfg = &spec->autocfg;
2324 if (!spec->inv_dmic_split)
2326 for (i = 0; i < cfg->num_inputs; i++) {
2327 if (cfg->inputs[i].pin != nid)
2329 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2331 val = snd_hda_codec_get_pincfg(codec, nid);
2332 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2337 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2338 int idx, bool is_switch, unsigned int ctl,
2341 struct hda_gen_spec *spec = codec->spec;
2343 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2344 const char *sfx = is_switch ? "Switch" : "Volume";
2345 unsigned int chs = inv_dmic ? 1 : 3;
2352 snprintf(tmpname, sizeof(tmpname),
2353 "%s Capture %s", label, sfx);
2355 snprintf(tmpname, sizeof(tmpname),
2357 err = add_control(spec, type, tmpname, idx,
2358 amp_val_replace_channels(ctl, chs));
2359 if (err < 0 || !inv_dmic)
2362 /* Make independent right kcontrol */
2364 snprintf(tmpname, sizeof(tmpname),
2365 "Inverted %s Capture %s", label, sfx);
2367 snprintf(tmpname, sizeof(tmpname),
2368 "Inverted Capture %s", sfx);
2369 return add_control(spec, type, tmpname, idx,
2370 amp_val_replace_channels(ctl, 2));
2373 /* create single (and simple) capture volume and switch controls */
2374 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2375 unsigned int vol_ctl, unsigned int sw_ctl,
2379 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2382 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2388 /* create bound capture volume and switch controls */
2389 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2390 unsigned int vol_ctl, unsigned int sw_ctl)
2392 struct hda_gen_spec *spec = codec->spec;
2393 struct snd_kcontrol_new *knew;
2396 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
2400 knew->private_value = vol_ctl;
2401 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2404 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
2408 knew->private_value = sw_ctl;
2409 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2414 /* return the vol ctl when used first in the imux list */
2415 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2417 struct nid_path *path;
2421 path = get_input_path(codec, 0, idx);
2424 ctl = path->ctls[type];
2427 for (i = 0; i < idx - 1; i++) {
2428 path = get_input_path(codec, 0, i);
2429 if (path && path->ctls[type] == ctl)
2435 /* create individual capture volume and switch controls per input */
2436 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2438 struct hda_gen_spec *spec = codec->spec;
2439 struct hda_input_mux *imux = &spec->input_mux;
2440 int i, err, type, type_idx = 0;
2441 const char *prev_label = NULL;
2443 for (i = 0; i < imux->num_items; i++) {
2446 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2447 if (prev_label && !strcmp(label, prev_label))
2452 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2454 for (type = 0; type < 2; type++) {
2455 err = add_single_cap_ctl(codec, label, type_idx, type,
2456 get_first_cap_ctl(codec, i, type),
2465 static int create_capture_mixers(struct hda_codec *codec)
2467 struct hda_gen_spec *spec = codec->spec;
2468 struct hda_input_mux *imux = &spec->input_mux;
2469 int i, n, nums, err;
2471 if (spec->dyn_adc_switch)
2474 nums = spec->num_adc_nids;
2476 if (!spec->auto_mic && imux->num_items > 1) {
2477 struct snd_kcontrol_new *knew;
2479 name = nums > 1 ? "Input Source" : "Capture Source";
2480 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
2486 for (n = 0; n < nums; n++) {
2488 bool inv_dmic = false;
2492 for (i = 0; i < imux->num_items; i++) {
2493 struct nid_path *path;
2494 path = get_input_path(codec, n, i);
2497 parse_capvol_in_path(codec, path);
2499 vol = path->ctls[NID_PATH_VOL_CTL];
2500 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2503 sw = path->ctls[NID_PATH_MUTE_CTL];
2504 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2506 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2511 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2513 else if (!spec->multi_cap_vol)
2514 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2516 err = create_multi_cap_vol_ctl(codec);
2525 * add mic boosts if needed
2527 static int parse_mic_boost(struct hda_codec *codec)
2529 struct hda_gen_spec *spec = codec->spec;
2530 struct auto_pin_cfg *cfg = &spec->autocfg;
2534 const char *prev_label = NULL;
2536 for (i = 0; i < cfg->num_inputs; i++) {
2537 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2539 nid = cfg->inputs[i].pin;
2540 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2542 char boost_label[44];
2543 struct nid_path *path;
2546 label = hda_get_autocfg_input_label(codec, cfg, i);
2547 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2548 label = "Headphone Mic";
2549 if (prev_label && !strcmp(label, prev_label))
2555 snprintf(boost_label, sizeof(boost_label),
2556 "%s Boost Volume", label);
2557 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2558 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2559 boost_label, type_idx, val);
2563 path = snd_hda_get_nid_path(codec, nid, 0);
2565 path->ctls[NID_PATH_BOOST_CTL] = val;
2572 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2574 static void parse_digital(struct hda_codec *codec)
2576 struct hda_gen_spec *spec = codec->spec;
2577 struct nid_path *path;
2581 /* support multiple SPDIFs; the secondary is set up as a slave */
2583 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2584 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2585 dig_nid = look_for_dac(codec, pin, true);
2588 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
2591 print_nid_path("digout", path);
2592 path->active = true;
2593 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
2595 spec->multiout.dig_out_nid = dig_nid;
2596 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2598 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2599 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2601 spec->slave_dig_outs[nums - 1] = dig_nid;
2606 if (spec->autocfg.dig_in_pin) {
2607 dig_nid = codec->start_nid;
2608 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2609 unsigned int wcaps = get_wcaps(codec, dig_nid);
2610 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2612 if (!(wcaps & AC_WCAP_DIGITAL))
2614 path = snd_hda_add_new_path(codec,
2615 spec->autocfg.dig_in_pin,
2618 print_nid_path("digin", path);
2619 path->active = true;
2620 spec->dig_in_nid = dig_nid;
2621 spec->digin_path = snd_hda_get_path_idx(codec, path);
2630 * input MUX handling
2633 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2635 /* select the given imux item; either unmute exclusively or select the route */
2636 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2639 struct hda_gen_spec *spec = codec->spec;
2640 const struct hda_input_mux *imux;
2641 struct nid_path *path;
2643 imux = &spec->input_mux;
2644 if (!imux->num_items)
2647 if (idx >= imux->num_items)
2648 idx = imux->num_items - 1;
2649 if (spec->cur_mux[adc_idx] == idx)
2652 path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
2656 snd_hda_activate_path(codec, path, false, false);
2658 spec->cur_mux[adc_idx] = idx;
2660 if (spec->shared_mic_hp)
2661 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2663 if (spec->dyn_adc_switch)
2664 dyn_adc_pcm_resetup(codec, idx);
2666 path = get_input_path(codec, adc_idx, idx);
2671 snd_hda_activate_path(codec, path, true, false);
2672 if (spec->cap_sync_hook)
2673 spec->cap_sync_hook(codec);
2679 * Jack detections for HP auto-mute and mic-switch
2682 /* check each pin in the given array; returns true if any of them is plugged */
2683 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2687 for (i = 0; i < num_pins; i++) {
2688 hda_nid_t nid = pins[i];
2691 present |= snd_hda_jack_detect(codec, nid);
2696 /* standard HP/line-out auto-mute helper */
2697 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2698 bool mute, bool hp_out)
2700 struct hda_gen_spec *spec = codec->spec;
2701 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2704 for (i = 0; i < num_pins; i++) {
2705 hda_nid_t nid = pins[i];
2709 /* don't reset VREF value in case it's controlling
2710 * the amp (see alc861_fixup_asus_amp_vref_0f())
2712 if (spec->keep_vref_in_automute) {
2713 val = snd_hda_codec_read(codec, nid, 0,
2714 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2719 snd_hda_set_pin_ctl_cache(codec, nid, val);
2720 set_pin_eapd(codec, nid, !mute);
2724 /* Toggle outputs muting */
2725 void snd_hda_gen_update_outputs(struct hda_codec *codec)
2727 struct hda_gen_spec *spec = codec->spec;
2730 /* Control HP pins/amps depending on master_mute state;
2731 * in general, HP pins/amps control should be enabled in all cases,
2732 * but currently set only for master_mute, just to be safe
2734 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2735 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2736 spec->autocfg.hp_pins, spec->master_mute, true);
2738 if (!spec->automute_speaker)
2741 on = spec->hp_jack_present | spec->line_jack_present;
2742 on |= spec->master_mute;
2743 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2744 spec->autocfg.speaker_pins, on, false);
2746 /* toggle line-out mutes if needed, too */
2747 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2748 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2749 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2751 if (!spec->automute_lo)
2754 on = spec->hp_jack_present;
2755 on |= spec->master_mute;
2756 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2757 spec->autocfg.line_out_pins, on, false);
2759 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
2761 static void call_update_outputs(struct hda_codec *codec)
2763 struct hda_gen_spec *spec = codec->spec;
2764 if (spec->automute_hook)
2765 spec->automute_hook(codec);
2767 snd_hda_gen_update_outputs(codec);
2770 /* standard HP-automute helper */
2771 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2773 struct hda_gen_spec *spec = codec->spec;
2775 spec->hp_jack_present =
2776 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2777 spec->autocfg.hp_pins);
2778 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2780 call_update_outputs(codec);
2782 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
2784 /* standard line-out-automute helper */
2785 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2787 struct hda_gen_spec *spec = codec->spec;
2789 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2791 /* check LO jack only when it's different from HP */
2792 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2795 spec->line_jack_present =
2796 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2797 spec->autocfg.line_out_pins);
2798 if (!spec->automute_speaker || !spec->detect_lo)
2800 call_update_outputs(codec);
2802 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
2804 /* standard mic auto-switch helper */
2805 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
2807 struct hda_gen_spec *spec = codec->spec;
2810 if (!spec->auto_mic)
2813 for (i = spec->am_num_entries - 1; i > 0; i--) {
2814 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2815 mux_select(codec, 0, spec->am_entry[i].idx);
2819 mux_select(codec, 0, spec->am_entry[0].idx);
2821 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
2824 * Auto-Mute mode mixer enum support
2826 static int automute_mode_info(struct snd_kcontrol *kcontrol,
2827 struct snd_ctl_elem_info *uinfo)
2829 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2830 struct hda_gen_spec *spec = codec->spec;
2831 static const char * const texts3[] = {
2832 "Disabled", "Speaker Only", "Line Out+Speaker"
2835 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2836 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2837 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2840 static int automute_mode_get(struct snd_kcontrol *kcontrol,
2841 struct snd_ctl_elem_value *ucontrol)
2843 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2844 struct hda_gen_spec *spec = codec->spec;
2845 unsigned int val = 0;
2846 if (spec->automute_speaker)
2848 if (spec->automute_lo)
2851 ucontrol->value.enumerated.item[0] = val;
2855 static int automute_mode_put(struct snd_kcontrol *kcontrol,
2856 struct snd_ctl_elem_value *ucontrol)
2858 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2859 struct hda_gen_spec *spec = codec->spec;
2861 switch (ucontrol->value.enumerated.item[0]) {
2863 if (!spec->automute_speaker && !spec->automute_lo)
2865 spec->automute_speaker = 0;
2866 spec->automute_lo = 0;
2869 if (spec->automute_speaker_possible) {
2870 if (!spec->automute_lo && spec->automute_speaker)
2872 spec->automute_speaker = 1;
2873 spec->automute_lo = 0;
2874 } else if (spec->automute_lo_possible) {
2875 if (spec->automute_lo)
2877 spec->automute_lo = 1;
2882 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2884 if (spec->automute_speaker && spec->automute_lo)
2886 spec->automute_speaker = 1;
2887 spec->automute_lo = 1;
2892 call_update_outputs(codec);
2896 static const struct snd_kcontrol_new automute_mode_enum = {
2897 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2898 .name = "Auto-Mute Mode",
2899 .info = automute_mode_info,
2900 .get = automute_mode_get,
2901 .put = automute_mode_put,
2904 static int add_automute_mode_enum(struct hda_codec *codec)
2906 struct hda_gen_spec *spec = codec->spec;
2908 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
2914 * Check the availability of HP/line-out auto-mute;
2915 * Set up appropriately if really supported
2917 static int check_auto_mute_availability(struct hda_codec *codec)
2919 struct hda_gen_spec *spec = codec->spec;
2920 struct auto_pin_cfg *cfg = &spec->autocfg;
2924 if (cfg->hp_pins[0])
2926 if (cfg->line_out_pins[0])
2928 if (cfg->speaker_pins[0])
2930 if (present < 2) /* need two different output types */
2933 if (!cfg->speaker_pins[0] &&
2934 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2935 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2936 sizeof(cfg->speaker_pins));
2937 cfg->speaker_outs = cfg->line_outs;
2940 if (!cfg->hp_pins[0] &&
2941 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2942 memcpy(cfg->hp_pins, cfg->line_out_pins,
2943 sizeof(cfg->hp_pins));
2944 cfg->hp_outs = cfg->line_outs;
2947 for (i = 0; i < cfg->hp_outs; i++) {
2948 hda_nid_t nid = cfg->hp_pins[i];
2949 if (!is_jack_detectable(codec, nid))
2951 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2953 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
2954 spec->hp_automute_hook ?
2955 spec->hp_automute_hook :
2956 snd_hda_gen_hp_automute);
2957 spec->detect_hp = 1;
2960 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2961 if (cfg->speaker_outs)
2962 for (i = 0; i < cfg->line_outs; i++) {
2963 hda_nid_t nid = cfg->line_out_pins[i];
2964 if (!is_jack_detectable(codec, nid))
2966 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2967 snd_hda_jack_detect_enable_callback(codec, nid,
2968 HDA_GEN_FRONT_EVENT,
2969 spec->line_automute_hook ?
2970 spec->line_automute_hook :
2971 snd_hda_gen_line_automute);
2972 spec->detect_lo = 1;
2974 spec->automute_lo_possible = spec->detect_hp;
2977 spec->automute_speaker_possible = cfg->speaker_outs &&
2978 (spec->detect_hp || spec->detect_lo);
2980 spec->automute_lo = spec->automute_lo_possible;
2981 spec->automute_speaker = spec->automute_speaker_possible;
2983 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2984 /* create a control for automute mode */
2985 err = add_automute_mode_enum(codec);
2992 /* check whether all auto-mic pins are valid; setup indices if OK */
2993 static bool auto_mic_check_imux(struct hda_codec *codec)
2995 struct hda_gen_spec *spec = codec->spec;
2996 const struct hda_input_mux *imux;
2999 imux = &spec->input_mux;
3000 for (i = 0; i < spec->am_num_entries; i++) {
3001 spec->am_entry[i].idx =
3002 find_idx_in_nid_list(spec->am_entry[i].pin,
3003 spec->imux_pins, imux->num_items);
3004 if (spec->am_entry[i].idx < 0)
3005 return false; /* no corresponding imux */
3008 /* we don't need the jack detection for the first pin */
3009 for (i = 1; i < spec->am_num_entries; i++)
3010 snd_hda_jack_detect_enable_callback(codec,
3011 spec->am_entry[i].pin,
3013 spec->mic_autoswitch_hook ?
3014 spec->mic_autoswitch_hook :
3015 snd_hda_gen_mic_autoswitch);
3019 static int compare_attr(const void *ap, const void *bp)
3021 const struct automic_entry *a = ap;
3022 const struct automic_entry *b = bp;
3023 return (int)(a->attr - b->attr);
3027 * Check the availability of auto-mic switch;
3028 * Set up if really supported
3030 static int check_auto_mic_availability(struct hda_codec *codec)
3032 struct hda_gen_spec *spec = codec->spec;
3033 struct auto_pin_cfg *cfg = &spec->autocfg;
3037 if (spec->suppress_auto_mic)
3042 for (i = 0; i < cfg->num_inputs; i++) {
3043 hda_nid_t nid = cfg->inputs[i].pin;
3045 attr = snd_hda_codec_get_pincfg(codec, nid);
3046 attr = snd_hda_get_input_pin_attr(attr);
3047 if (types & (1 << attr))
3048 return 0; /* already occupied */
3050 case INPUT_PIN_ATTR_INT:
3051 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3052 return 0; /* invalid type */
3054 case INPUT_PIN_ATTR_UNUSED:
3055 return 0; /* invalid entry */
3057 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3058 return 0; /* invalid type */
3059 if (!spec->line_in_auto_switch &&
3060 cfg->inputs[i].type != AUTO_PIN_MIC)
3061 return 0; /* only mic is allowed */
3062 if (!is_jack_detectable(codec, nid))
3063 return 0; /* no unsol support */
3066 if (num_pins >= MAX_AUTO_MIC_PINS)
3068 types |= (1 << attr);
3069 spec->am_entry[num_pins].pin = nid;
3070 spec->am_entry[num_pins].attr = attr;
3077 spec->am_num_entries = num_pins;
3078 /* sort the am_entry in the order of attr so that the pin with a
3079 * higher attr will be selected when the jack is plugged.
3081 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3082 compare_attr, NULL);
3084 if (!auto_mic_check_imux(codec))
3088 spec->num_adc_nids = 1;
3089 spec->cur_mux[0] = spec->am_entry[0].idx;
3090 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3091 spec->am_entry[0].pin,
3092 spec->am_entry[1].pin,
3093 spec->am_entry[2].pin);
3100 * Parse the given BIOS configuration and set up the hda_gen_spec
3102 * return 1 if successful, 0 if the proper config is not found,
3103 * or a negative error code
3105 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
3106 struct auto_pin_cfg *cfg)
3108 struct hda_gen_spec *spec = codec->spec;
3111 if (cfg != &spec->autocfg) {
3112 spec->autocfg = *cfg;
3113 cfg = &spec->autocfg;
3116 if (!cfg->line_outs) {
3117 if (cfg->dig_outs || cfg->dig_in_pin) {
3118 spec->multiout.max_channels = 2;
3119 spec->no_analog = 1;
3122 return 0; /* can't find valid BIOS pin config */
3125 if (!spec->no_primary_hp &&
3126 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3127 cfg->line_outs <= cfg->hp_outs) {
3128 /* use HP as primary out */
3129 cfg->speaker_outs = cfg->line_outs;
3130 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3131 sizeof(cfg->speaker_pins));
3132 cfg->line_outs = cfg->hp_outs;
3133 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3135 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3136 cfg->line_out_type = AUTO_PIN_HP_OUT;
3139 err = parse_output_paths(codec);
3142 err = create_multi_channel_mode(codec);
3145 err = create_multi_out_ctls(codec, cfg);
3148 err = create_hp_out_ctls(codec);
3151 err = create_speaker_out_ctls(codec);
3154 err = create_indep_hp_ctls(codec);
3157 err = create_loopback_mixing_ctl(codec);
3160 err = create_shared_input(codec);
3163 err = create_input_ctls(codec);
3167 spec->const_channel_count = spec->ext_channel_count;
3168 /* check the multiple speaker and headphone pins */
3169 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
3170 spec->const_channel_count = max(spec->const_channel_count,
3171 cfg->speaker_outs * 2);
3172 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
3173 spec->const_channel_count = max(spec->const_channel_count,
3175 spec->multiout.max_channels = max(spec->ext_channel_count,
3176 spec->const_channel_count);
3178 err = check_auto_mute_availability(codec);
3182 err = check_dyn_adc_switch(codec);
3186 if (!spec->shared_mic_hp) {
3187 err = check_auto_mic_availability(codec);
3192 err = create_capture_mixers(codec);
3196 err = parse_mic_boost(codec);
3201 parse_digital(codec);
3205 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
3209 * Build control elements
3212 /* slave controls for virtual master */
3213 static const char * const slave_pfxs[] = {
3214 "Front", "Surround", "Center", "LFE", "Side",
3215 "Headphone", "Speaker", "Mono", "Line Out",
3216 "CLFE", "Bass Speaker", "PCM",
3217 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3218 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3223 int snd_hda_gen_build_controls(struct hda_codec *codec)
3225 struct hda_gen_spec *spec = codec->spec;
3228 if (spec->kctls.used) {
3229 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3234 if (spec->multiout.dig_out_nid) {
3235 err = snd_hda_create_dig_out_ctls(codec,
3236 spec->multiout.dig_out_nid,
3237 spec->multiout.dig_out_nid,
3238 spec->pcm_rec[1].pcm_type);
3241 if (!spec->no_analog) {
3242 err = snd_hda_create_spdif_share_sw(codec,
3246 spec->multiout.share_spdif = 1;
3249 if (spec->dig_in_nid) {
3250 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3255 /* if we have no master control, let's create it */
3256 if (!spec->no_analog &&
3257 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3258 unsigned int vmaster_tlv[4];
3259 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3260 HDA_OUTPUT, vmaster_tlv);
3261 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3262 vmaster_tlv, slave_pfxs,
3267 if (!spec->no_analog &&
3268 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3269 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3272 true, &spec->vmaster_mute.sw_kctl);
3275 if (spec->vmaster_mute.hook)
3276 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3277 spec->vmaster_mute_enum);
3280 free_kctls(spec); /* no longer needed */
3282 if (spec->shared_mic_hp) {
3284 int nid = spec->autocfg.inputs[1].pin;
3285 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3288 err = snd_hda_jack_detect_enable(codec, nid, 0);
3293 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3299 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3306 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3307 struct hda_codec *codec,
3308 struct snd_pcm_substream *substream,
3311 struct hda_gen_spec *spec = codec->spec;
3312 if (spec->pcm_playback_hook)
3313 spec->pcm_playback_hook(hinfo, codec, substream, action);
3317 * Analog playback callbacks
3319 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3320 struct hda_codec *codec,
3321 struct snd_pcm_substream *substream)
3323 struct hda_gen_spec *spec = codec->spec;
3326 mutex_lock(&spec->pcm_mutex);
3327 err = snd_hda_multi_out_analog_open(codec,
3328 &spec->multiout, substream,
3331 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3332 call_pcm_playback_hook(hinfo, codec, substream,
3333 HDA_GEN_PCM_ACT_OPEN);
3335 mutex_unlock(&spec->pcm_mutex);
3339 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3340 struct hda_codec *codec,
3341 unsigned int stream_tag,
3342 unsigned int format,
3343 struct snd_pcm_substream *substream)
3345 struct hda_gen_spec *spec = codec->spec;
3348 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3349 stream_tag, format, substream);
3351 call_pcm_playback_hook(hinfo, codec, substream,
3352 HDA_GEN_PCM_ACT_PREPARE);
3356 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3357 struct hda_codec *codec,
3358 struct snd_pcm_substream *substream)
3360 struct hda_gen_spec *spec = codec->spec;
3363 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3365 call_pcm_playback_hook(hinfo, codec, substream,
3366 HDA_GEN_PCM_ACT_CLEANUP);
3370 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3371 struct hda_codec *codec,
3372 struct snd_pcm_substream *substream)
3374 struct hda_gen_spec *spec = codec->spec;
3375 mutex_lock(&spec->pcm_mutex);
3376 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3377 call_pcm_playback_hook(hinfo, codec, substream,
3378 HDA_GEN_PCM_ACT_CLOSE);
3379 mutex_unlock(&spec->pcm_mutex);
3383 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3384 struct hda_codec *codec,
3385 struct snd_pcm_substream *substream)
3387 struct hda_gen_spec *spec = codec->spec;
3390 mutex_lock(&spec->pcm_mutex);
3391 if (!spec->indep_hp_enabled)
3394 spec->active_streams |= 1 << STREAM_INDEP_HP;
3395 call_pcm_playback_hook(hinfo, codec, substream,
3396 HDA_GEN_PCM_ACT_OPEN);
3397 mutex_unlock(&spec->pcm_mutex);
3401 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3402 struct hda_codec *codec,
3403 struct snd_pcm_substream *substream)
3405 struct hda_gen_spec *spec = codec->spec;
3406 mutex_lock(&spec->pcm_mutex);
3407 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3408 call_pcm_playback_hook(hinfo, codec, substream,
3409 HDA_GEN_PCM_ACT_CLOSE);
3410 mutex_unlock(&spec->pcm_mutex);
3414 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3415 struct hda_codec *codec,
3416 unsigned int stream_tag,
3417 unsigned int format,
3418 struct snd_pcm_substream *substream)
3420 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3421 call_pcm_playback_hook(hinfo, codec, substream,
3422 HDA_GEN_PCM_ACT_PREPARE);
3426 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3427 struct hda_codec *codec,
3428 struct snd_pcm_substream *substream)
3430 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3431 call_pcm_playback_hook(hinfo, codec, substream,
3432 HDA_GEN_PCM_ACT_CLEANUP);
3439 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3440 struct hda_codec *codec,
3441 struct snd_pcm_substream *substream)
3443 struct hda_gen_spec *spec = codec->spec;
3444 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3447 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3448 struct hda_codec *codec,
3449 unsigned int stream_tag,
3450 unsigned int format,
3451 struct snd_pcm_substream *substream)
3453 struct hda_gen_spec *spec = codec->spec;
3454 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3455 stream_tag, format, substream);
3458 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3459 struct hda_codec *codec,
3460 struct snd_pcm_substream *substream)
3462 struct hda_gen_spec *spec = codec->spec;
3463 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3466 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3467 struct hda_codec *codec,
3468 struct snd_pcm_substream *substream)
3470 struct hda_gen_spec *spec = codec->spec;
3471 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3477 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3478 struct hda_codec *codec,
3479 unsigned int stream_tag,
3480 unsigned int format,
3481 struct snd_pcm_substream *substream)
3483 struct hda_gen_spec *spec = codec->spec;
3485 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
3486 stream_tag, 0, format);
3490 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3491 struct hda_codec *codec,
3492 struct snd_pcm_substream *substream)
3494 struct hda_gen_spec *spec = codec->spec;
3496 snd_hda_codec_cleanup_stream(codec,
3497 spec->adc_nids[substream->number + 1]);
3503 static const struct hda_pcm_stream pcm_analog_playback = {
3507 /* NID is set in build_pcms */
3509 .open = playback_pcm_open,
3510 .close = playback_pcm_close,
3511 .prepare = playback_pcm_prepare,
3512 .cleanup = playback_pcm_cleanup
3516 static const struct hda_pcm_stream pcm_analog_capture = {
3520 /* NID is set in build_pcms */
3523 static const struct hda_pcm_stream pcm_analog_alt_playback = {
3527 /* NID is set in build_pcms */
3529 .open = alt_playback_pcm_open,
3530 .close = alt_playback_pcm_close,
3531 .prepare = alt_playback_pcm_prepare,
3532 .cleanup = alt_playback_pcm_cleanup
3536 static const struct hda_pcm_stream pcm_analog_alt_capture = {
3537 .substreams = 2, /* can be overridden */
3540 /* NID is set in build_pcms */
3542 .prepare = alt_capture_pcm_prepare,
3543 .cleanup = alt_capture_pcm_cleanup
3547 static const struct hda_pcm_stream pcm_digital_playback = {
3551 /* NID is set in build_pcms */
3553 .open = dig_playback_pcm_open,
3554 .close = dig_playback_pcm_close,
3555 .prepare = dig_playback_pcm_prepare,
3556 .cleanup = dig_playback_pcm_cleanup
3560 static const struct hda_pcm_stream pcm_digital_capture = {
3564 /* NID is set in build_pcms */
3567 /* Used by build_pcms to flag that a PCM has no playback stream */
3568 static const struct hda_pcm_stream pcm_null_stream = {
3575 * dynamic changing ADC PCM streams
3577 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3579 struct hda_gen_spec *spec = codec->spec;
3580 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3582 if (spec->cur_adc && spec->cur_adc != new_adc) {
3583 /* stream is running, let's swap the current ADC */
3584 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3585 spec->cur_adc = new_adc;
3586 snd_hda_codec_setup_stream(codec, new_adc,
3587 spec->cur_adc_stream_tag, 0,
3588 spec->cur_adc_format);
3594 /* analog capture with dynamic dual-adc changes */
3595 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3596 struct hda_codec *codec,
3597 unsigned int stream_tag,
3598 unsigned int format,
3599 struct snd_pcm_substream *substream)
3601 struct hda_gen_spec *spec = codec->spec;
3602 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3603 spec->cur_adc_stream_tag = stream_tag;
3604 spec->cur_adc_format = format;
3605 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3609 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3610 struct hda_codec *codec,
3611 struct snd_pcm_substream *substream)
3613 struct hda_gen_spec *spec = codec->spec;
3614 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3619 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3623 .nid = 0, /* fill later */
3625 .prepare = dyn_adc_capture_pcm_prepare,
3626 .cleanup = dyn_adc_capture_pcm_cleanup
3630 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3631 const char *chip_name)
3637 strlcpy(str, chip_name, len);
3639 /* drop non-alnum chars after a space */
3640 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3641 if (!isalnum(p[1])) {
3646 strlcat(str, sfx, len);
3649 /* build PCM streams based on the parsed results */
3650 int snd_hda_gen_build_pcms(struct hda_codec *codec)
3652 struct hda_gen_spec *spec = codec->spec;
3653 struct hda_pcm *info = spec->pcm_rec;
3654 const struct hda_pcm_stream *p;
3655 bool have_multi_adcs;
3657 codec->num_pcms = 1;
3658 codec->pcm_info = info;
3660 if (spec->no_analog)
3663 fill_pcm_stream_name(spec->stream_name_analog,
3664 sizeof(spec->stream_name_analog),
3665 " Analog", codec->chip_name);
3666 info->name = spec->stream_name_analog;
3668 if (spec->multiout.num_dacs > 0) {
3669 p = spec->stream_analog_playback;
3671 p = &pcm_analog_playback;
3672 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3673 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3674 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3675 spec->multiout.max_channels;
3676 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3677 spec->autocfg.line_outs == 2)
3678 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3681 if (spec->num_adc_nids) {
3682 p = spec->stream_analog_capture;
3684 if (spec->dyn_adc_switch)
3685 p = &dyn_adc_pcm_analog_capture;
3687 p = &pcm_analog_capture;
3689 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3690 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3694 /* SPDIF for stream index #1 */
3695 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
3696 fill_pcm_stream_name(spec->stream_name_digital,
3697 sizeof(spec->stream_name_digital),
3698 " Digital", codec->chip_name);
3699 codec->num_pcms = 2;
3700 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3701 info = spec->pcm_rec + 1;
3702 info->name = spec->stream_name_digital;
3703 if (spec->dig_out_type)
3704 info->pcm_type = spec->dig_out_type;
3706 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3707 if (spec->multiout.dig_out_nid) {
3708 p = spec->stream_digital_playback;
3710 p = &pcm_digital_playback;
3711 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3712 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3714 if (spec->dig_in_nid) {
3715 p = spec->stream_digital_capture;
3717 p = &pcm_digital_capture;
3718 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3719 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3723 if (spec->no_analog)
3726 /* If the use of more than one ADC is requested for the current
3727 * model, configure a second analog capture-only PCM.
3729 have_multi_adcs = (spec->num_adc_nids > 1) &&
3730 !spec->dyn_adc_switch && !spec->auto_mic;
3731 /* Additional Analaog capture for index #2 */
3732 if (spec->alt_dac_nid || have_multi_adcs) {
3733 codec->num_pcms = 3;
3734 info = spec->pcm_rec + 2;
3735 info->name = spec->stream_name_analog;
3736 if (spec->alt_dac_nid) {
3737 p = spec->stream_analog_alt_playback;
3739 p = &pcm_analog_alt_playback;
3740 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3741 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3744 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3746 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3748 if (have_multi_adcs) {
3749 p = spec->stream_analog_alt_capture;
3751 p = &pcm_analog_alt_capture;
3752 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3753 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3755 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3756 spec->num_adc_nids - 1;
3758 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3760 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3766 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3770 * Standard auto-parser initializations
3773 /* configure the given path as a proper output */
3774 static void set_output_and_unmute(struct hda_codec *codec,
3775 int pin_type, int path_idx)
3777 struct nid_path *path;
3780 path = snd_hda_get_path_from_idx(codec, path_idx);
3781 if (!path || !path->depth)
3783 pin = path->path[path->depth - 1];
3784 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3785 snd_hda_activate_path(codec, path, path->active, true);
3786 set_pin_eapd(codec, pin, path->active);
3789 /* initialize primary output paths */
3790 static void init_multi_out(struct hda_codec *codec)
3792 struct hda_gen_spec *spec = codec->spec;
3796 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3801 for (i = 0; i < spec->autocfg.line_outs; i++)
3802 set_output_and_unmute(codec, pin_type, spec->out_paths[i]);
3806 static void __init_extra_out(struct hda_codec *codec, int num_outs,
3807 int *paths, int type)
3811 for (i = 0; i < num_outs; i++)
3812 set_output_and_unmute(codec, type, paths[i]);
3815 /* initialize hp and speaker paths */
3816 static void init_extra_out(struct hda_codec *codec)
3818 struct hda_gen_spec *spec = codec->spec;
3820 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3821 __init_extra_out(codec, spec->autocfg.hp_outs,
3822 spec->hp_paths, PIN_HP);
3823 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3824 __init_extra_out(codec, spec->autocfg.speaker_outs,
3825 spec->speaker_paths, PIN_OUT);
3828 /* initialize multi-io paths */
3829 static void init_multi_io(struct hda_codec *codec)
3831 struct hda_gen_spec *spec = codec->spec;
3834 for (i = 0; i < spec->multi_ios; i++) {
3835 hda_nid_t pin = spec->multi_io[i].pin;
3836 struct nid_path *path;
3837 path = get_multiio_path(codec, i);
3840 if (!spec->multi_io[i].ctl_in)
3841 spec->multi_io[i].ctl_in =
3842 snd_hda_codec_update_cache(codec, pin, 0,
3843 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3844 snd_hda_activate_path(codec, path, path->active, true);
3848 /* set up the input pin config, depending on the given auto-pin type */
3849 static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3852 unsigned int val = PIN_IN;
3853 if (auto_pin_type == AUTO_PIN_MIC)
3854 val |= snd_hda_get_default_vref(codec, nid);
3855 snd_hda_set_pin_ctl_cache(codec, nid, val);
3858 /* set up input pins and loopback paths */
3859 static void init_analog_input(struct hda_codec *codec)
3861 struct hda_gen_spec *spec = codec->spec;
3862 struct auto_pin_cfg *cfg = &spec->autocfg;
3865 for (i = 0; i < cfg->num_inputs; i++) {
3866 hda_nid_t nid = cfg->inputs[i].pin;
3867 if (is_input_pin(codec, nid))
3868 set_input_pin(codec, nid, cfg->inputs[i].type);
3870 /* init loopback inputs */
3871 if (spec->mixer_nid) {
3872 struct nid_path *path;
3873 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
3875 snd_hda_activate_path(codec, path,
3876 path->active, false);
3881 /* initialize ADC paths */
3882 static void init_input_src(struct hda_codec *codec)
3884 struct hda_gen_spec *spec = codec->spec;
3885 struct hda_input_mux *imux = &spec->input_mux;
3886 struct nid_path *path;
3889 if (spec->dyn_adc_switch)
3892 nums = spec->num_adc_nids;
3894 for (c = 0; c < nums; c++) {
3895 for (i = 0; i < imux->num_items; i++) {
3896 path = get_input_path(codec, c, i);
3898 bool active = path->active;
3899 if (i == spec->cur_mux[c])
3901 snd_hda_activate_path(codec, path, active, false);
3906 if (spec->shared_mic_hp)
3907 update_shared_mic_hp(codec, spec->cur_mux[0]);
3909 if (spec->cap_sync_hook)
3910 spec->cap_sync_hook(codec);
3913 /* set right pin controls for digital I/O */
3914 static void init_digital(struct hda_codec *codec)
3916 struct hda_gen_spec *spec = codec->spec;
3920 for (i = 0; i < spec->autocfg.dig_outs; i++)
3921 set_output_and_unmute(codec, PIN_OUT, spec->digout_paths[i]);
3922 pin = spec->autocfg.dig_in_pin;
3924 struct nid_path *path;
3925 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
3926 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
3928 snd_hda_activate_path(codec, path, path->active, false);
3932 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3933 * invalid unsol tags by some reason
3935 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3939 for (i = 0; i < codec->init_pins.used; i++) {
3940 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3941 hda_nid_t nid = pin->nid;
3942 if (is_jack_detectable(codec, nid) &&
3943 !snd_hda_jack_tbl_get(codec, nid))
3944 snd_hda_codec_update_cache(codec, nid, 0,
3945 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3950 * initialize the generic spec;
3951 * this can be put as patch_ops.init function
3953 int snd_hda_gen_init(struct hda_codec *codec)
3955 struct hda_gen_spec *spec = codec->spec;
3957 if (spec->init_hook)
3958 spec->init_hook(codec);
3960 snd_hda_apply_verbs(codec);
3962 codec->cached_write = 1;
3964 init_multi_out(codec);
3965 init_extra_out(codec);
3966 init_multi_io(codec);
3967 init_analog_input(codec);
3968 init_input_src(codec);
3969 init_digital(codec);
3971 clear_unsol_on_unused_pins(codec);
3973 /* call init functions of standard auto-mute helpers */
3974 snd_hda_gen_hp_automute(codec, NULL);
3975 snd_hda_gen_line_automute(codec, NULL);
3976 snd_hda_gen_mic_autoswitch(codec, NULL);
3978 snd_hda_codec_flush_amp_cache(codec);
3979 snd_hda_codec_flush_cmd_cache(codec);
3981 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3982 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3984 hda_call_check_power_status(codec, 0x01);
3987 EXPORT_SYMBOL_HDA(snd_hda_gen_init);
3990 * free the generic spec;
3991 * this can be put as patch_ops.free function
3993 void snd_hda_gen_free(struct hda_codec *codec)
3995 snd_hda_gen_spec_free(codec->spec);
3999 EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4003 * check the loopback power save state;
4004 * this can be put as patch_ops.check_power_status function
4006 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4008 struct hda_gen_spec *spec = codec->spec;
4009 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4011 EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4016 * the generic codec support
4019 static const struct hda_codec_ops generic_patch_ops = {
4020 .build_controls = snd_hda_gen_build_controls,
4021 .build_pcms = snd_hda_gen_build_pcms,
4022 .init = snd_hda_gen_init,
4023 .free = snd_hda_gen_free,
4024 .unsol_event = snd_hda_jack_unsol_event,
4026 .check_power_status = snd_hda_gen_check_power_status,
4030 int snd_hda_parse_generic_codec(struct hda_codec *codec)
4032 struct hda_gen_spec *spec;
4035 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4038 snd_hda_gen_spec_init(spec);
4041 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4045 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
4049 codec->patch_ops = generic_patch_ops;
4053 snd_hda_gen_free(codec);
4056 EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);