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 static struct nid_path *get_nid_path(struct hda_codec *codec,
91 hda_nid_t from_nid, hda_nid_t to_nid,
94 struct hda_gen_spec *spec = codec->spec;
97 for (i = 0; i < spec->paths.used; i++) {
98 struct nid_path *path = snd_array_elem(&spec->paths, i);
101 if ((!from_nid || path->path[0] == from_nid) &&
102 (!to_nid || path->path[path->depth - 1] == to_nid)) {
103 if (with_aa_mix == HDA_PARSE_ALL ||
104 path->with_aa_mix == with_aa_mix)
111 /* get the path between the given NIDs;
112 * passing 0 to either @pin or @dac behaves as a wildcard
114 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
115 hda_nid_t from_nid, hda_nid_t to_nid)
117 return get_nid_path(codec, from_nid, to_nid, HDA_PARSE_ALL);
119 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
121 /* get the index number corresponding to the path instance;
122 * the index starts from 1, for easier checking the invalid value
124 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
126 struct hda_gen_spec *spec = codec->spec;
127 struct nid_path *array = spec->paths.list;
130 if (!spec->paths.used)
133 if (idx < 0 || idx >= spec->paths.used)
138 /* get the path instance corresponding to the given index number */
139 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
141 struct hda_gen_spec *spec = codec->spec;
143 if (idx <= 0 || idx > spec->paths.used)
145 return snd_array_elem(&spec->paths, idx - 1);
148 /* check whether the given DAC is already found in any existing paths */
149 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
151 struct hda_gen_spec *spec = codec->spec;
154 for (i = 0; i < spec->paths.used; i++) {
155 struct nid_path *path = snd_array_elem(&spec->paths, i);
156 if (path->path[0] == nid)
162 /* check whether the given two widgets can be connected */
163 static bool is_reachable_path(struct hda_codec *codec,
164 hda_nid_t from_nid, hda_nid_t to_nid)
166 if (!from_nid || !to_nid)
168 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
171 /* nid, dir and idx */
172 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
174 /* check whether the given ctl is already assigned in any path elements */
175 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
177 struct hda_gen_spec *spec = codec->spec;
180 val &= AMP_VAL_COMPARE_MASK;
181 for (i = 0; i < spec->paths.used; i++) {
182 struct nid_path *path = snd_array_elem(&spec->paths, i);
183 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
189 /* check whether a control with the given (nid, dir, idx) was assigned */
190 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
193 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
194 return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
195 is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
198 static void print_nid_path(const char *pfx, struct nid_path *path)
205 for (i = 0; i < path->depth; i++) {
207 sprintf(tmp, ":%02x", path->path[i]);
208 strlcat(buf, tmp, sizeof(buf));
210 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
213 /* called recursively */
214 static bool __parse_nid_path(struct hda_codec *codec,
215 hda_nid_t from_nid, hda_nid_t to_nid,
216 int with_aa_mix, struct nid_path *path, int depth)
218 struct hda_gen_spec *spec = codec->spec;
219 const hda_nid_t *conn;
222 if (to_nid == spec->mixer_nid) {
223 if (with_aa_mix == HDA_PARSE_NO_AAMIX)
225 with_aa_mix = HDA_PARSE_ALL; /* mark aa-mix is included */
228 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
229 for (i = 0; i < nums; i++) {
230 if (conn[i] != from_nid) {
231 /* special case: when from_nid is 0,
232 * try to find an empty DAC
235 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
236 is_dac_already_used(codec, conn[i]))
239 /* aa-mix is requested but not included? */
240 if (!(spec->mixer_nid && with_aa_mix == HDA_PARSE_ONLY_AAMIX))
243 if (depth >= MAX_NID_PATH_DEPTH)
245 for (i = 0; i < nums; i++) {
247 type = get_wcaps_type(get_wcaps(codec, conn[i]));
248 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
251 if (__parse_nid_path(codec, from_nid, conn[i],
252 with_aa_mix, path, depth + 1))
258 path->path[path->depth] = conn[i];
259 if (conn[i] == spec->mixer_nid)
260 path->with_aa_mix = true;
261 path->idx[path->depth + 1] = i;
262 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
263 path->multi[path->depth + 1] = 1;
268 /* parse the widget path from the given nid to the target nid;
269 * when @from_nid is 0, try to find an empty DAC;
270 * when @with_aa_mix is HDA_PARSE_NO_AAMIX, paths with spec->mixer_nid are
271 * excluded, only the paths that don't go through the mixer will be chosen.
272 * when @with_aa_mix is HDA_PARSE_ONLY_AAMIX, only the paths going through
273 * spec->mixer_nid will be chosen.
274 * when @with_aa_mix is HDA_PARSE_ALL, no special handling about mixer widget.
276 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
277 hda_nid_t to_nid, int with_aa_mix,
278 struct nid_path *path)
280 if (__parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path, 1)) {
281 path->path[path->depth] = to_nid;
287 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
290 * parse the path between the given NIDs and add to the path list.
291 * if no valid path is found, return NULL
294 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
295 hda_nid_t to_nid, int with_aa_mix)
297 struct hda_gen_spec *spec = codec->spec;
298 struct nid_path *path;
300 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
303 /* check whether the path has been already added */
304 path = get_nid_path(codec, from_nid, to_nid, with_aa_mix);
308 path = snd_array_new(&spec->paths);
311 memset(path, 0, sizeof(*path));
312 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, with_aa_mix, path))
318 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
320 /* look for an empty DAC slot */
321 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
324 struct hda_gen_spec *spec = codec->spec;
328 for (i = 0; i < spec->num_all_dacs; i++) {
329 hda_nid_t nid = spec->all_dacs[i];
330 if (!nid || is_dac_already_used(codec, nid))
332 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
333 if (is_digital != cap_digital)
335 if (is_reachable_path(codec, nid, pin))
341 /* replace the channels in the composed amp value with the given number */
342 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
344 val &= ~(0x3U << 16);
349 /* check whether the widget has the given amp capability for the direction */
350 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
351 int dir, unsigned int bits)
355 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
356 if (query_amp_caps(codec, nid, dir) & bits)
361 #define nid_has_mute(codec, nid, dir) \
362 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
363 #define nid_has_volume(codec, nid, dir) \
364 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
366 /* look for a widget suitable for assigning a mute switch in the path */
367 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
368 struct nid_path *path)
372 for (i = path->depth - 1; i >= 0; i--) {
373 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
374 return path->path[i];
375 if (i != path->depth - 1 && i != 0 &&
376 nid_has_mute(codec, path->path[i], HDA_INPUT))
377 return path->path[i];
382 /* look for a widget suitable for assigning a volume ctl in the path */
383 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
384 struct nid_path *path)
388 for (i = path->depth - 1; i >= 0; i--) {
389 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
390 return path->path[i];
396 * path activation / deactivation
399 /* can have the amp-in capability? */
400 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
402 hda_nid_t nid = path->path[idx];
403 unsigned int caps = get_wcaps(codec, nid);
404 unsigned int type = get_wcaps_type(caps);
406 if (!(caps & AC_WCAP_IN_AMP))
408 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
413 /* can have the amp-out capability? */
414 static bool has_amp_out(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_OUT_AMP))
422 if (type == AC_WID_PIN && !idx) /* only for output pins */
427 /* check whether the given (nid,dir,idx) is active */
428 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
429 unsigned int idx, unsigned int dir)
431 struct hda_gen_spec *spec = codec->spec;
434 for (n = 0; n < spec->paths.used; n++) {
435 struct nid_path *path = snd_array_elem(&spec->paths, n);
438 for (i = 0; i < path->depth; i++) {
439 if (path->path[i] == nid) {
440 if (dir == HDA_OUTPUT || path->idx[i] == idx)
449 /* get the default amp value for the target state */
450 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
451 int dir, bool enable)
454 unsigned int val = 0;
456 caps = query_amp_caps(codec, nid, dir);
457 if (caps & AC_AMPCAP_NUM_STEPS) {
460 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
462 if (caps & AC_AMPCAP_MUTE) {
469 /* initialize the amp value (only at the first time) */
470 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
472 int val = get_amp_val_to_activate(codec, nid, dir, false);
473 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
476 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
477 int idx, bool enable)
480 if (is_ctl_associated(codec, nid, dir, idx) ||
481 (!enable && is_active_nid(codec, nid, dir, idx)))
483 val = get_amp_val_to_activate(codec, nid, dir, enable);
484 snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
487 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
490 hda_nid_t nid = path->path[i];
491 init_amp(codec, nid, HDA_OUTPUT, 0);
492 activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
495 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
496 int i, bool enable, bool add_aamix)
498 struct hda_gen_spec *spec = codec->spec;
499 const hda_nid_t *conn;
502 hda_nid_t nid = path->path[i];
504 nums = snd_hda_get_conn_list(codec, nid, &conn);
505 type = get_wcaps_type(get_wcaps(codec, nid));
506 if (type == AC_WID_PIN ||
507 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
513 for (n = 0; n < nums; n++)
514 init_amp(codec, nid, HDA_INPUT, n);
516 if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
519 /* here is a little bit tricky in comparison with activate_amp_out();
520 * when aa-mixer is available, we need to enable the path as well
522 for (n = 0; n < nums; n++) {
523 if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
525 activate_amp(codec, nid, HDA_INPUT, n, enable);
529 /* activate or deactivate the given path
530 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
532 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
533 bool enable, bool add_aamix)
538 path->active = false;
540 for (i = path->depth - 1; i >= 0; i--) {
541 if (enable && path->multi[i])
542 snd_hda_codec_write_cache(codec, path->path[i], 0,
543 AC_VERB_SET_CONNECT_SEL,
545 if (has_amp_in(codec, path, i))
546 activate_amp_in(codec, path, i, enable, add_aamix);
547 if (has_amp_out(codec, path, i))
548 activate_amp_out(codec, path, i, enable);
554 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
556 /* turn on/off EAPD on the given pin */
557 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
559 struct hda_gen_spec *spec = codec->spec;
560 if (spec->own_eapd_ctl ||
561 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
565 snd_hda_codec_update_cache(codec, pin, 0,
566 AC_VERB_SET_EAPD_BTLENABLE,
567 enable ? 0x02 : 0x00);
572 * Helper functions for creating mixer ctl elements
580 static const struct snd_kcontrol_new control_templates[] = {
581 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
582 HDA_CODEC_MUTE(NULL, 0, 0, 0),
583 HDA_BIND_MUTE(NULL, 0, 0, 0),
586 /* add dynamic controls from template */
587 static int add_control(struct hda_gen_spec *spec, int type, const char *name,
588 int cidx, unsigned long val)
590 struct snd_kcontrol_new *knew;
592 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
596 if (get_amp_nid_(val))
597 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
598 knew->private_value = val;
602 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
603 const char *pfx, const char *dir,
604 const char *sfx, int cidx, unsigned long val)
607 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
608 return add_control(spec, type, name, cidx, val);
611 #define add_pb_vol_ctrl(spec, type, pfx, val) \
612 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
613 #define add_pb_sw_ctrl(spec, type, pfx, val) \
614 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
615 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
616 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
617 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
618 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
620 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
621 unsigned int chs, struct nid_path *path)
626 val = path->ctls[NID_PATH_VOL_CTL];
629 val = amp_val_replace_channels(val, chs);
630 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
633 /* return the channel bits suitable for the given path->ctls[] */
634 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
637 int chs = 1; /* mono (left only) */
639 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
640 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
641 chs = 3; /* stereo */
646 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
647 struct nid_path *path)
649 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
650 return add_vol_ctl(codec, pfx, cidx, chs, path);
653 /* create a mute-switch for the given mixer widget;
654 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
656 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
657 unsigned int chs, struct nid_path *path)
660 int type = HDA_CTL_WIDGET_MUTE;
664 val = path->ctls[NID_PATH_MUTE_CTL];
667 val = amp_val_replace_channels(val, chs);
668 if (get_amp_direction_(val) == HDA_INPUT) {
669 hda_nid_t nid = get_amp_nid_(val);
670 int nums = snd_hda_get_num_conns(codec, nid);
672 type = HDA_CTL_BIND_MUTE;
676 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
679 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
680 int cidx, struct nid_path *path)
682 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
683 return add_sw_ctl(codec, pfx, cidx, chs, path);
686 static const char * const channel_name[4] = {
687 "Front", "Surround", "CLFE", "Side"
690 /* give some appropriate ctl name prefix for the given line out channel */
691 static const char *get_line_out_pfx(struct hda_gen_spec *spec, int ch,
692 bool can_be_master, int *index)
694 struct auto_pin_cfg *cfg = &spec->autocfg;
697 if (cfg->line_outs == 1 && !spec->multi_ios &&
698 !cfg->hp_outs && !cfg->speaker_outs && can_be_master)
699 return spec->vmaster_mute.hook ? "PCM" : "Master";
701 /* if there is really a single DAC used in the whole output paths,
702 * use it master (or "PCM" if a vmaster hook is present)
704 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
705 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
706 return spec->vmaster_mute.hook ? "PCM" : "Master";
708 switch (cfg->line_out_type) {
709 case AUTO_PIN_SPEAKER_OUT:
710 if (cfg->line_outs == 1)
712 if (cfg->line_outs == 2)
713 return ch ? "Bass Speaker" : "Speaker";
715 case AUTO_PIN_HP_OUT:
716 /* for multi-io case, only the primary out */
717 if (ch && spec->multi_ios)
722 if (cfg->line_outs == 1 && !spec->multi_ios)
726 if (ch >= ARRAY_SIZE(channel_name)) {
731 return channel_name[ch];
738 /* badness definition */
740 /* No primary DAC is found for the main output */
741 BAD_NO_PRIMARY_DAC = 0x10000,
742 /* No DAC is found for the extra output */
744 /* No possible multi-ios */
745 BAD_MULTI_IO = 0x103,
746 /* No individual DAC for extra output */
747 BAD_NO_EXTRA_DAC = 0x102,
748 /* No individual DAC for extra surrounds */
749 BAD_NO_EXTRA_SURR_DAC = 0x101,
750 /* Primary DAC shared with main surrounds */
751 BAD_SHARED_SURROUND = 0x100,
752 /* Primary DAC shared with main CLFE */
753 BAD_SHARED_CLFE = 0x10,
754 /* Primary DAC shared with extra surrounds */
755 BAD_SHARED_EXTRA_SURROUND = 0x10,
756 /* Volume widget is shared */
757 BAD_SHARED_VOL = 0x10,
760 /* look for widgets in the path between the given NIDs appropriate for
761 * volume and mute controls, and assign the values to ctls[].
763 * When no appropriate widget is found in the path, the badness value
764 * is incremented depending on the situation. The function returns the
765 * total badness for both volume and mute controls.
767 static int assign_out_path_ctls(struct hda_codec *codec, hda_nid_t pin,
770 struct nid_path *path = snd_hda_get_nid_path(codec, dac, pin);
776 return BAD_SHARED_VOL * 2;
777 nid = look_for_out_vol_nid(codec, path);
779 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
780 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
781 badness += BAD_SHARED_VOL;
783 path->ctls[NID_PATH_VOL_CTL] = val;
785 badness += BAD_SHARED_VOL;
786 nid = look_for_out_mute_nid(codec, path);
788 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
789 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
790 nid_has_mute(codec, nid, HDA_OUTPUT))
791 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
793 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
794 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
795 badness += BAD_SHARED_VOL;
797 path->ctls[NID_PATH_MUTE_CTL] = val;
799 badness += BAD_SHARED_VOL;
803 struct badness_table {
804 int no_primary_dac; /* no primary DAC */
805 int no_dac; /* no secondary DACs */
806 int shared_primary; /* primary DAC is shared with main output */
807 int shared_surr; /* secondary DAC shared with main or primary */
808 int shared_clfe; /* third DAC shared with main or primary */
809 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
812 static struct badness_table main_out_badness = {
813 .no_primary_dac = BAD_NO_PRIMARY_DAC,
814 .no_dac = BAD_NO_DAC,
815 .shared_primary = BAD_NO_PRIMARY_DAC,
816 .shared_surr = BAD_SHARED_SURROUND,
817 .shared_clfe = BAD_SHARED_CLFE,
818 .shared_surr_main = BAD_SHARED_SURROUND,
821 static struct badness_table extra_out_badness = {
822 .no_primary_dac = BAD_NO_DAC,
823 .no_dac = BAD_NO_DAC,
824 .shared_primary = BAD_NO_EXTRA_DAC,
825 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
826 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
827 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
830 /* get the DAC of the primary output corresponding to the given array index */
831 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
833 struct hda_gen_spec *spec = codec->spec;
834 struct auto_pin_cfg *cfg = &spec->autocfg;
836 if (cfg->line_outs > idx)
837 return spec->private_dac_nids[idx];
838 idx -= cfg->line_outs;
839 if (spec->multi_ios > idx)
840 return spec->multi_io[idx].dac;
844 /* return the DAC if it's reachable, otherwise zero */
845 static inline hda_nid_t try_dac(struct hda_codec *codec,
846 hda_nid_t dac, hda_nid_t pin)
848 return is_reachable_path(codec, dac, pin) ? dac : 0;
851 /* try to assign DACs to pins and return the resultant badness */
852 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
853 const hda_nid_t *pins, hda_nid_t *dacs,
855 const struct badness_table *bad)
857 struct hda_gen_spec *spec = codec->spec;
865 for (i = 0; i < num_outs; i++) {
866 struct nid_path *path;
867 hda_nid_t pin = pins[i];
870 badness += assign_out_path_ctls(codec, pin, dacs[i]);
874 dacs[i] = look_for_dac(codec, pin, false);
875 if (!dacs[i] && !i) {
876 for (j = 1; j < num_outs; j++) {
877 if (is_reachable_path(codec, dacs[j], pin)) {
888 dac = try_dac(codec, get_primary_out(codec, i), pin);
890 dac = try_dac(codec, dacs[0], pin);
892 dac = try_dac(codec, get_primary_out(codec, i), pin);
895 badness += bad->shared_primary;
897 badness += bad->shared_surr;
899 badness += bad->shared_clfe;
900 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
901 dac = spec->private_dac_nids[0];
902 badness += bad->shared_surr_main;
904 badness += bad->no_primary_dac;
906 badness += bad->no_dac;
908 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_NO_AAMIX);
909 if (!path && !i && spec->mixer_nid) {
911 path = snd_hda_add_new_path(codec, dac, pin, HDA_PARSE_ALL);
916 print_nid_path("output", path);
918 path_idx[i] = snd_hda_get_path_idx(codec, path);
921 badness += assign_out_path_ctls(codec, pin, dac);
927 /* return NID if the given pin has only a single connection to a certain DAC */
928 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
930 struct hda_gen_spec *spec = codec->spec;
932 hda_nid_t nid_found = 0;
934 for (i = 0; i < spec->num_all_dacs; i++) {
935 hda_nid_t nid = spec->all_dacs[i];
936 if (!nid || is_dac_already_used(codec, nid))
938 if (is_reachable_path(codec, nid, pin)) {
947 /* check whether the given pin can be a multi-io pin */
948 static bool can_be_multiio_pin(struct hda_codec *codec,
949 unsigned int location, hda_nid_t nid)
951 unsigned int defcfg, caps;
953 defcfg = snd_hda_codec_get_pincfg(codec, nid);
954 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
956 if (location && get_defcfg_location(defcfg) != location)
958 caps = snd_hda_query_pin_caps(codec, nid);
959 if (!(caps & AC_PINCAP_OUT))
964 /* count the number of input pins that are capable to be multi-io */
965 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
967 struct hda_gen_spec *spec = codec->spec;
968 struct auto_pin_cfg *cfg = &spec->autocfg;
969 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
970 unsigned int location = get_defcfg_location(defcfg);
974 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
975 for (i = 0; i < cfg->num_inputs; i++) {
976 if (cfg->inputs[i].type != type)
978 if (can_be_multiio_pin(codec, location,
989 * When hardwired is set, try to fill ony hardwired pins, and returns
990 * zero if any pins are filled, non-zero if nothing found.
991 * When hardwired is off, try to fill possible input pins, and returns
994 static int fill_multi_ios(struct hda_codec *codec,
995 hda_nid_t reference_pin,
998 struct hda_gen_spec *spec = codec->spec;
999 struct auto_pin_cfg *cfg = &spec->autocfg;
1000 int type, i, j, num_pins, old_pins;
1001 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1002 unsigned int location = get_defcfg_location(defcfg);
1005 old_pins = spec->multi_ios;
1009 num_pins = count_multiio_pins(codec, reference_pin);
1013 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1014 for (i = 0; i < cfg->num_inputs; i++) {
1015 struct nid_path *path;
1016 hda_nid_t nid = cfg->inputs[i].pin;
1019 if (cfg->inputs[i].type != type)
1021 if (!can_be_multiio_pin(codec, location, nid))
1023 for (j = 0; j < spec->multi_ios; j++) {
1024 if (nid == spec->multi_io[j].pin)
1027 if (j < spec->multi_ios)
1031 dac = get_dac_if_single(codec, nid);
1033 dac = look_for_dac(codec, nid, false);
1038 path = snd_hda_add_new_path(codec, dac, nid, HDA_PARSE_NO_AAMIX);
1043 print_nid_path("multiio", path);
1044 spec->multi_io[spec->multi_ios].pin = nid;
1045 spec->multi_io[spec->multi_ios].dac = dac;
1046 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1047 snd_hda_get_path_idx(codec, path);
1049 if (spec->multi_ios >= 2)
1055 badness = BAD_MULTI_IO;
1056 if (old_pins == spec->multi_ios) {
1058 return 1; /* nothing found */
1060 return badness; /* no badness if nothing found */
1062 if (!hardwired && spec->multi_ios < 2) {
1063 /* cancel newly assigned paths */
1064 spec->paths.used -= spec->multi_ios - old_pins;
1065 spec->multi_ios = old_pins;
1069 /* assign volume and mute controls */
1070 for (i = old_pins; i < spec->multi_ios; i++)
1071 badness += assign_out_path_ctls(codec, spec->multi_io[i].pin,
1072 spec->multi_io[i].dac);
1077 /* map DACs for all pins in the list if they are single connections */
1078 static bool map_singles(struct hda_codec *codec, int outs,
1079 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1081 struct hda_gen_spec *spec = codec->spec;
1084 for (i = 0; i < outs; i++) {
1085 struct nid_path *path;
1089 dac = get_dac_if_single(codec, pins[i]);
1092 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_NO_AAMIX);
1093 if (!path && !i && spec->mixer_nid)
1094 path = snd_hda_add_new_path(codec, dac, pins[i], HDA_PARSE_ALL);
1098 print_nid_path("output", path);
1099 path->active = true;
1100 path_idx[i] = snd_hda_get_path_idx(codec, path);
1106 /* create a new path including aamix if available, and return its index */
1107 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1109 struct nid_path *path;
1111 path = snd_hda_get_path_from_idx(codec, path_idx);
1112 if (!path || !path->depth || path->with_aa_mix)
1114 path = snd_hda_add_new_path(codec, path->path[0],
1115 path->path[path->depth - 1],
1116 HDA_PARSE_ONLY_AAMIX);
1119 print_nid_path("output-aamix", path);
1120 path->active = false; /* unused as default */
1121 return snd_hda_get_path_idx(codec, path);
1124 /* fill in the dac_nids table from the parsed pin configuration */
1125 static int fill_and_eval_dacs(struct hda_codec *codec,
1126 bool fill_hardwired,
1127 bool fill_mio_first)
1129 struct hda_gen_spec *spec = codec->spec;
1130 struct auto_pin_cfg *cfg = &spec->autocfg;
1131 int i, err, badness;
1133 /* set num_dacs once to full for look_for_dac() */
1134 spec->multiout.num_dacs = cfg->line_outs;
1135 spec->multiout.dac_nids = spec->private_dac_nids;
1136 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1137 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1138 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1139 spec->multi_ios = 0;
1140 snd_array_free(&spec->paths);
1143 /* fill hard-wired DACs first */
1144 if (fill_hardwired) {
1147 mapped = map_singles(codec, cfg->line_outs,
1149 spec->private_dac_nids,
1151 mapped |= map_singles(codec, cfg->hp_outs,
1153 spec->multiout.hp_out_nid,
1155 mapped |= map_singles(codec, cfg->speaker_outs,
1157 spec->multiout.extra_out_nid,
1158 spec->speaker_paths);
1159 if (fill_mio_first && cfg->line_outs == 1 &&
1160 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1161 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1168 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1169 spec->private_dac_nids, spec->out_paths,
1172 /* re-count num_dacs and squash invalid entries */
1173 spec->multiout.num_dacs = 0;
1174 for (i = 0; i < cfg->line_outs; i++) {
1175 if (spec->private_dac_nids[i])
1176 spec->multiout.num_dacs++;
1178 memmove(spec->private_dac_nids + i,
1179 spec->private_dac_nids + i + 1,
1180 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1181 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1185 if (fill_mio_first &&
1186 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1187 /* try to fill multi-io first */
1188 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1191 /* we don't count badness at this stage yet */
1194 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1195 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1196 spec->multiout.hp_out_nid,
1198 &extra_out_badness);
1203 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1204 err = try_assign_dacs(codec, cfg->speaker_outs,
1206 spec->multiout.extra_out_nid,
1207 spec->speaker_paths,
1208 &extra_out_badness);
1213 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1214 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1220 if (spec->mixer_nid) {
1221 spec->aamix_out_paths[0] =
1222 check_aamix_out_path(codec, spec->out_paths[0]);
1223 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1224 spec->aamix_out_paths[1] =
1225 check_aamix_out_path(codec, spec->hp_paths[0]);
1226 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1227 spec->aamix_out_paths[2] =
1228 check_aamix_out_path(codec, spec->speaker_paths[0]);
1231 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1232 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1233 spec->multi_ios = 1; /* give badness */
1235 if (spec->multi_ios == 2) {
1236 for (i = 0; i < 2; i++)
1237 spec->private_dac_nids[spec->multiout.num_dacs++] =
1238 spec->multi_io[i].dac;
1239 spec->ext_channel_count = 2;
1240 } else if (spec->multi_ios) {
1241 spec->multi_ios = 0;
1242 badness += BAD_MULTI_IO;
1248 #define DEBUG_BADNESS
1250 #ifdef DEBUG_BADNESS
1251 #define debug_badness snd_printdd
1253 #define debug_badness(...)
1256 static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
1258 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1259 cfg->line_out_pins[0], cfg->line_out_pins[1],
1260 cfg->line_out_pins[2], cfg->line_out_pins[3],
1261 spec->multiout.dac_nids[0],
1262 spec->multiout.dac_nids[1],
1263 spec->multiout.dac_nids[2],
1264 spec->multiout.dac_nids[3]);
1265 if (spec->multi_ios > 0)
1266 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1268 spec->multi_io[0].pin, spec->multi_io[1].pin,
1269 spec->multi_io[0].dac, spec->multi_io[1].dac);
1270 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1271 cfg->hp_pins[0], cfg->hp_pins[1],
1272 cfg->hp_pins[2], cfg->hp_pins[3],
1273 spec->multiout.hp_out_nid[0],
1274 spec->multiout.hp_out_nid[1],
1275 spec->multiout.hp_out_nid[2],
1276 spec->multiout.hp_out_nid[3]);
1277 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1278 cfg->speaker_pins[0], cfg->speaker_pins[1],
1279 cfg->speaker_pins[2], cfg->speaker_pins[3],
1280 spec->multiout.extra_out_nid[0],
1281 spec->multiout.extra_out_nid[1],
1282 spec->multiout.extra_out_nid[2],
1283 spec->multiout.extra_out_nid[3]);
1286 /* find all available DACs of the codec */
1287 static void fill_all_dac_nids(struct hda_codec *codec)
1289 struct hda_gen_spec *spec = codec->spec;
1291 hda_nid_t nid = codec->start_nid;
1293 spec->num_all_dacs = 0;
1294 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1295 for (i = 0; i < codec->num_nodes; i++, nid++) {
1296 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1298 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1299 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1302 spec->all_dacs[spec->num_all_dacs++] = nid;
1306 static int parse_output_paths(struct hda_codec *codec)
1308 struct hda_gen_spec *spec = codec->spec;
1309 struct auto_pin_cfg *cfg = &spec->autocfg;
1310 struct auto_pin_cfg *best_cfg;
1311 int best_badness = INT_MAX;
1313 bool fill_hardwired = true, fill_mio_first = true;
1314 bool best_wired = true, best_mio = true;
1315 bool hp_spk_swapped = false;
1317 fill_all_dac_nids(codec);
1319 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1325 badness = fill_and_eval_dacs(codec, fill_hardwired,
1331 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1332 cfg->line_out_type, fill_hardwired, fill_mio_first,
1334 debug_show_configs(spec, cfg);
1335 if (badness < best_badness) {
1336 best_badness = badness;
1338 best_wired = fill_hardwired;
1339 best_mio = fill_mio_first;
1343 fill_mio_first = !fill_mio_first;
1344 if (!fill_mio_first)
1346 fill_hardwired = !fill_hardwired;
1347 if (!fill_hardwired)
1351 hp_spk_swapped = true;
1352 if (cfg->speaker_outs > 0 &&
1353 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1354 cfg->hp_outs = cfg->line_outs;
1355 memcpy(cfg->hp_pins, cfg->line_out_pins,
1356 sizeof(cfg->hp_pins));
1357 cfg->line_outs = cfg->speaker_outs;
1358 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1359 sizeof(cfg->speaker_pins));
1360 cfg->speaker_outs = 0;
1361 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1362 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1363 fill_hardwired = true;
1366 if (cfg->hp_outs > 0 &&
1367 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1368 cfg->speaker_outs = cfg->line_outs;
1369 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1370 sizeof(cfg->speaker_pins));
1371 cfg->line_outs = cfg->hp_outs;
1372 memcpy(cfg->line_out_pins, cfg->hp_pins,
1373 sizeof(cfg->hp_pins));
1375 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1376 cfg->line_out_type = AUTO_PIN_HP_OUT;
1377 fill_hardwired = true;
1384 debug_badness("==> restoring best_cfg\n");
1386 fill_and_eval_dacs(codec, best_wired, best_mio);
1388 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1389 cfg->line_out_type, best_wired, best_mio);
1390 debug_show_configs(spec, cfg);
1392 if (cfg->line_out_pins[0]) {
1393 struct nid_path *path;
1394 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1396 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1403 /* add playback controls from the parsed DAC table */
1404 static int create_multi_out_ctls(struct hda_codec *codec,
1405 const struct auto_pin_cfg *cfg)
1407 struct hda_gen_spec *spec = codec->spec;
1408 int i, err, noutputs;
1410 noutputs = cfg->line_outs;
1411 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1412 noutputs += spec->multi_ios;
1414 for (i = 0; i < noutputs; i++) {
1418 struct nid_path *path;
1420 dac = spec->multiout.dac_nids[i];
1423 if (i >= cfg->line_outs) {
1425 name = channel_name[i];
1427 name = get_line_out_pfx(spec, i, true, &index);
1430 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1433 if (!name || !strcmp(name, "CLFE")) {
1435 err = add_vol_ctl(codec, "Center", 0, 1, path);
1438 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1441 err = add_sw_ctl(codec, "Center", 0, 1, path);
1444 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1448 err = add_stereo_vol(codec, name, index, path);
1451 err = add_stereo_sw(codec, name, index, path);
1459 static int create_extra_out(struct hda_codec *codec, int path_idx,
1460 const char *pfx, int cidx)
1462 struct nid_path *path;
1465 path = snd_hda_get_path_from_idx(codec, path_idx);
1468 err = add_stereo_vol(codec, pfx, cidx, path);
1471 err = add_stereo_sw(codec, pfx, cidx, path);
1477 /* add playback controls for speaker and HP outputs */
1478 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1479 const int *paths, const char *pfx)
1483 for (i = 0; i < num_pins; i++) {
1488 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1489 name = "Bass Speaker";
1490 else if (num_pins >= 3) {
1491 snprintf(tmp, sizeof(tmp), "%s %s",
1492 pfx, channel_name[i]);
1498 err = create_extra_out(codec, paths[i], name, idx);
1505 static int create_hp_out_ctls(struct hda_codec *codec)
1507 struct hda_gen_spec *spec = codec->spec;
1508 return create_extra_outs(codec, spec->autocfg.hp_outs,
1513 static int create_speaker_out_ctls(struct hda_codec *codec)
1515 struct hda_gen_spec *spec = codec->spec;
1516 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1517 spec->speaker_paths,
1522 * independent HP controls
1525 static int indep_hp_info(struct snd_kcontrol *kcontrol,
1526 struct snd_ctl_elem_info *uinfo)
1528 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1531 static int indep_hp_get(struct snd_kcontrol *kcontrol,
1532 struct snd_ctl_elem_value *ucontrol)
1534 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1535 struct hda_gen_spec *spec = codec->spec;
1536 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1540 static int indep_hp_put(struct snd_kcontrol *kcontrol,
1541 struct snd_ctl_elem_value *ucontrol)
1543 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1544 struct hda_gen_spec *spec = codec->spec;
1545 unsigned int select = ucontrol->value.enumerated.item[0];
1548 mutex_lock(&spec->pcm_mutex);
1549 if (spec->active_streams) {
1554 if (spec->indep_hp_enabled != select) {
1555 spec->indep_hp_enabled = select;
1556 if (spec->indep_hp_enabled)
1557 spec->multiout.hp_out_nid[0] = 0;
1559 spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
1563 mutex_unlock(&spec->pcm_mutex);
1567 static const struct snd_kcontrol_new indep_hp_ctl = {
1568 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1569 .name = "Independent HP",
1570 .info = indep_hp_info,
1571 .get = indep_hp_get,
1572 .put = indep_hp_put,
1576 static int create_indep_hp_ctls(struct hda_codec *codec)
1578 struct hda_gen_spec *spec = codec->spec;
1580 if (!spec->indep_hp)
1582 if (!spec->multiout.hp_out_nid[0]) {
1587 spec->indep_hp_enabled = false;
1588 spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
1589 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
1595 * channel mode enum control
1598 static int ch_mode_info(struct snd_kcontrol *kcontrol,
1599 struct snd_ctl_elem_info *uinfo)
1601 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1602 struct hda_gen_spec *spec = codec->spec;
1604 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1606 uinfo->value.enumerated.items = spec->multi_ios + 1;
1607 if (uinfo->value.enumerated.item > spec->multi_ios)
1608 uinfo->value.enumerated.item = spec->multi_ios;
1609 sprintf(uinfo->value.enumerated.name, "%dch",
1610 (uinfo->value.enumerated.item + 1) * 2);
1614 static int ch_mode_get(struct snd_kcontrol *kcontrol,
1615 struct snd_ctl_elem_value *ucontrol)
1617 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1618 struct hda_gen_spec *spec = codec->spec;
1619 ucontrol->value.enumerated.item[0] = (spec->ext_channel_count - 1) / 2;
1623 static inline struct nid_path *
1624 get_multiio_path(struct hda_codec *codec, int idx)
1626 struct hda_gen_spec *spec = codec->spec;
1627 return snd_hda_get_path_from_idx(codec,
1628 spec->out_paths[spec->autocfg.line_outs + idx]);
1631 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
1633 struct hda_gen_spec *spec = codec->spec;
1634 hda_nid_t nid = spec->multi_io[idx].pin;
1635 struct nid_path *path;
1637 path = get_multiio_path(codec, idx);
1641 if (path->active == output)
1645 snd_hda_set_pin_ctl_cache(codec, nid, PIN_OUT);
1646 snd_hda_activate_path(codec, path, true, true);
1647 set_pin_eapd(codec, nid, true);
1649 set_pin_eapd(codec, nid, false);
1650 snd_hda_activate_path(codec, path, false, true);
1651 snd_hda_set_pin_ctl_cache(codec, nid,
1652 spec->multi_io[idx].ctl_in);
1657 static int ch_mode_put(struct snd_kcontrol *kcontrol,
1658 struct snd_ctl_elem_value *ucontrol)
1660 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1661 struct hda_gen_spec *spec = codec->spec;
1664 ch = ucontrol->value.enumerated.item[0];
1665 if (ch < 0 || ch > spec->multi_ios)
1667 if (ch == (spec->ext_channel_count - 1) / 2)
1669 spec->ext_channel_count = (ch + 1) * 2;
1670 for (i = 0; i < spec->multi_ios; i++)
1671 set_multi_io(codec, i, i < ch);
1672 spec->multiout.max_channels = max(spec->ext_channel_count,
1673 spec->const_channel_count);
1674 if (spec->need_dac_fix)
1675 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
1679 static const struct snd_kcontrol_new channel_mode_enum = {
1680 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1681 .name = "Channel Mode",
1682 .info = ch_mode_info,
1687 static int create_multi_channel_mode(struct hda_codec *codec)
1689 struct hda_gen_spec *spec = codec->spec;
1691 if (spec->multi_ios > 0) {
1692 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
1699 * aamix loopback enable/disable switch
1702 #define loopback_mixing_info indep_hp_info
1704 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
1705 struct snd_ctl_elem_value *ucontrol)
1707 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1708 struct hda_gen_spec *spec = codec->spec;
1709 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
1713 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1714 int nomix_path_idx, int mix_path_idx)
1716 struct nid_path *nomix_path, *mix_path;
1718 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
1719 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
1720 if (!nomix_path || !mix_path)
1723 snd_hda_activate_path(codec, nomix_path, false, true);
1724 snd_hda_activate_path(codec, mix_path, true, true);
1726 snd_hda_activate_path(codec, mix_path, false, true);
1727 snd_hda_activate_path(codec, nomix_path, true, true);
1731 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
1732 struct snd_ctl_elem_value *ucontrol)
1734 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1735 struct hda_gen_spec *spec = codec->spec;
1736 unsigned int val = ucontrol->value.enumerated.item[0];
1738 if (val == spec->aamix_mode)
1740 spec->aamix_mode = val;
1741 update_aamix_paths(codec, val, spec->out_paths[0],
1742 spec->aamix_out_paths[0]);
1743 update_aamix_paths(codec, val, spec->hp_paths[0],
1744 spec->aamix_out_paths[1]);
1745 update_aamix_paths(codec, val, spec->speaker_paths[0],
1746 spec->aamix_out_paths[2]);
1750 static const struct snd_kcontrol_new loopback_mixing_enum = {
1751 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1752 .name = "Loopback Mixing",
1753 .info = loopback_mixing_info,
1754 .get = loopback_mixing_get,
1755 .put = loopback_mixing_put,
1758 static int create_loopback_mixing_ctl(struct hda_codec *codec)
1760 struct hda_gen_spec *spec = codec->spec;
1762 if (!spec->mixer_nid)
1764 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
1765 spec->aamix_out_paths[2]))
1767 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
1773 * shared headphone/mic handling
1776 static void call_update_outputs(struct hda_codec *codec);
1778 /* for shared I/O, change the pin-control accordingly */
1779 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
1781 struct hda_gen_spec *spec = codec->spec;
1783 hda_nid_t pin = spec->autocfg.inputs[1].pin;
1784 /* NOTE: this assumes that there are only two inputs, the
1785 * first is the real internal mic and the second is HP/mic jack.
1788 val = snd_hda_get_default_vref(codec, pin);
1790 /* This pin does not have vref caps - let's enable vref on pin 0x18
1791 instead, as suggested by Realtek */
1792 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
1793 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
1794 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
1795 if (vref_val != AC_PINCTL_VREF_HIZ)
1796 snd_hda_set_pin_ctl_cache(codec, vref_pin,
1797 PIN_IN | (set_as_mic ? vref_val : 0));
1800 val = set_as_mic ? val | PIN_IN : PIN_HP;
1801 snd_hda_set_pin_ctl_cache(codec, pin, val);
1803 spec->automute_speaker = !set_as_mic;
1804 call_update_outputs(codec);
1807 /* create a shared input with the headphone out */
1808 static int create_shared_input(struct hda_codec *codec)
1810 struct hda_gen_spec *spec = codec->spec;
1811 struct auto_pin_cfg *cfg = &spec->autocfg;
1812 unsigned int defcfg;
1815 /* only one internal input pin? */
1816 if (cfg->num_inputs != 1)
1818 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
1819 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
1822 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1823 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
1824 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
1825 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
1827 return 0; /* both not available */
1829 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
1830 return 0; /* no input */
1832 cfg->inputs[1].pin = nid;
1833 cfg->inputs[1].type = AUTO_PIN_MIC;
1834 cfg->num_inputs = 2;
1835 spec->shared_mic_hp = 1;
1836 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
1846 /* add the powersave loopback-list entry */
1847 static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
1849 struct hda_amp_list *list;
1851 if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
1853 list = spec->loopback_list + spec->num_loopbacks;
1855 list->dir = HDA_INPUT;
1857 spec->num_loopbacks++;
1858 spec->loopback.amplist = spec->loopback_list;
1861 #define add_loopback_list(spec, mix, idx) /* NOP */
1864 /* create input playback/capture controls for the given pin */
1865 static int new_analog_input(struct hda_codec *codec, int input_idx,
1866 hda_nid_t pin, const char *ctlname, int ctlidx,
1869 struct hda_gen_spec *spec = codec->spec;
1870 struct nid_path *path;
1874 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
1875 !nid_has_mute(codec, mix_nid, HDA_INPUT))
1876 return 0; /* no need for analog loopback */
1878 path = snd_hda_add_new_path(codec, pin, mix_nid, HDA_PARSE_ALL);
1881 print_nid_path("loopback", path);
1882 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
1884 idx = path->idx[path->depth - 1];
1885 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
1886 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1887 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
1890 path->ctls[NID_PATH_VOL_CTL] = val;
1893 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
1894 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
1895 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
1898 path->ctls[NID_PATH_MUTE_CTL] = val;
1901 path->active = true;
1902 add_loopback_list(spec, mix_nid, idx);
1906 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
1908 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
1909 return (pincap & AC_PINCAP_IN) != 0;
1912 /* Parse the codec tree and retrieve ADCs */
1913 static int fill_adc_nids(struct hda_codec *codec)
1915 struct hda_gen_spec *spec = codec->spec;
1917 hda_nid_t *adc_nids = spec->adc_nids;
1918 int max_nums = ARRAY_SIZE(spec->adc_nids);
1921 nid = codec->start_nid;
1922 for (i = 0; i < codec->num_nodes; i++, nid++) {
1923 unsigned int caps = get_wcaps(codec, nid);
1924 int type = get_wcaps_type(caps);
1926 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
1928 adc_nids[nums] = nid;
1929 if (++nums >= max_nums)
1932 spec->num_adc_nids = nums;
1936 /* filter out invalid adc_nids that don't give all active input pins;
1937 * if needed, check whether dynamic ADC-switching is available
1939 static int check_dyn_adc_switch(struct hda_codec *codec)
1941 struct hda_gen_spec *spec = codec->spec;
1942 struct hda_input_mux *imux = &spec->input_mux;
1943 hda_nid_t adc_nids[ARRAY_SIZE(spec->adc_nids)];
1949 for (n = 0; n < spec->num_adc_nids; n++) {
1950 adc = spec->adc_nids[n];
1951 for (i = 0; i < imux->num_items; i++) {
1952 pin = spec->imux_pins[i];
1953 if (!is_reachable_path(codec, pin, adc))
1956 if (i >= imux->num_items)
1957 adc_nids[nums++] = adc;
1961 if (spec->shared_mic_hp) {
1962 spec->shared_mic_hp = 0;
1963 imux->num_items = 1;
1967 /* check whether ADC-switch is possible */
1968 for (i = 0; i < imux->num_items; i++) {
1969 pin = spec->imux_pins[i];
1970 for (n = 0; n < spec->num_adc_nids; n++) {
1971 adc = spec->adc_nids[n];
1972 if (is_reachable_path(codec, pin, adc)) {
1973 spec->dyn_adc_idx[i] = n;
1979 snd_printdd("hda-codec: enabling ADC switching\n");
1980 spec->dyn_adc_switch = 1;
1981 } else if (nums != spec->num_adc_nids) {
1982 memcpy(spec->adc_nids, adc_nids, nums * sizeof(hda_nid_t));
1983 spec->num_adc_nids = nums;
1986 if (imux->num_items == 1 || spec->shared_mic_hp) {
1987 snd_printdd("hda-codec: reducing to a single ADC\n");
1988 spec->num_adc_nids = 1; /* reduce to a single ADC */
1991 /* single index for individual volumes ctls */
1992 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
1993 spec->num_adc_nids = 1;
1999 * create playback/capture controls for input pins
2001 static int create_input_ctls(struct hda_codec *codec)
2003 struct hda_gen_spec *spec = codec->spec;
2004 const struct auto_pin_cfg *cfg = &spec->autocfg;
2005 hda_nid_t mixer = spec->mixer_nid;
2006 struct hda_input_mux *imux = &spec->input_mux;
2008 int i, c, err, type_idx = 0;
2009 const char *prev_label = NULL;
2011 num_adcs = fill_adc_nids(codec);
2015 for (i = 0; i < cfg->num_inputs; i++) {
2020 pin = cfg->inputs[i].pin;
2021 if (!is_input_pin(codec, pin))
2024 label = hda_get_autocfg_input_label(codec, cfg, i);
2025 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2026 label = "Headphone Mic";
2027 if (prev_label && !strcmp(label, prev_label))
2034 if (is_reachable_path(codec, pin, mixer)) {
2035 err = new_analog_input(codec, i, pin,
2036 label, type_idx, mixer);
2043 for (c = 0; c < num_adcs; c++) {
2044 struct nid_path *path;
2045 hda_nid_t adc = spec->adc_nids[c];
2047 if (!is_reachable_path(codec, pin, adc))
2049 path = snd_array_new(&spec->paths);
2052 memset(path, 0, sizeof(*path));
2053 if (!snd_hda_parse_nid_path(codec, pin, adc, HDA_PARSE_ALL, path)) {
2055 "invalid input path 0x%x -> 0x%x\n",
2060 print_nid_path("input", path);
2063 spec->imux_pins[imux->num_items] = pin;
2064 snd_hda_add_imux_item(imux, label,
2065 imux->num_items, NULL);
2079 /* get the ADC NID corresponding to the given index */
2080 static hda_nid_t get_adc_nid(struct hda_codec *codec, int adc_idx, int imux_idx)
2082 struct hda_gen_spec *spec = codec->spec;
2083 if (spec->dyn_adc_switch)
2084 adc_idx = spec->dyn_adc_idx[imux_idx];
2085 return spec->adc_nids[adc_idx];
2088 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2091 static int mux_enum_info(struct snd_kcontrol *kcontrol,
2092 struct snd_ctl_elem_info *uinfo)
2094 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2095 struct hda_gen_spec *spec = codec->spec;
2096 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2099 static int mux_enum_get(struct snd_kcontrol *kcontrol,
2100 struct snd_ctl_elem_value *ucontrol)
2102 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2103 struct hda_gen_spec *spec = codec->spec;
2104 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2106 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2110 static int mux_enum_put(struct snd_kcontrol *kcontrol,
2111 struct snd_ctl_elem_value *ucontrol)
2113 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2114 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2115 return mux_select(codec, adc_idx,
2116 ucontrol->value.enumerated.item[0]);
2119 static const struct snd_kcontrol_new cap_src_temp = {
2120 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2121 .name = "Input Source",
2122 .info = mux_enum_info,
2123 .get = mux_enum_get,
2124 .put = mux_enum_put,
2128 * capture volume and capture switch ctls
2131 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2132 struct snd_ctl_elem_value *ucontrol);
2134 /* call the given amp update function for all amps in the imux list at once */
2135 static int cap_put_caller(struct snd_kcontrol *kcontrol,
2136 struct snd_ctl_elem_value *ucontrol,
2137 put_call_t func, int type)
2139 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2140 struct hda_gen_spec *spec = codec->spec;
2141 const struct hda_input_mux *imux;
2142 struct nid_path *path;
2143 int i, adc_idx, err = 0;
2145 imux = &spec->input_mux;
2146 adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2147 mutex_lock(&codec->control_mutex);
2148 /* we use the cache-only update at first since multiple input paths
2149 * may shared the same amp; by updating only caches, the redundant
2150 * writes to hardware can be reduced.
2152 codec->cached_write = 1;
2153 for (i = 0; i < imux->num_items; i++) {
2154 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2155 get_adc_nid(codec, adc_idx, i));
2156 if (!path->ctls[type])
2158 kcontrol->private_value = path->ctls[type];
2159 err = func(kcontrol, ucontrol);
2164 codec->cached_write = 0;
2165 mutex_unlock(&codec->control_mutex);
2166 snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
2167 if (err >= 0 && spec->cap_sync_hook)
2168 spec->cap_sync_hook(codec);
2172 /* capture volume ctl callbacks */
2173 #define cap_vol_info snd_hda_mixer_amp_volume_info
2174 #define cap_vol_get snd_hda_mixer_amp_volume_get
2175 #define cap_vol_tlv snd_hda_mixer_amp_tlv
2177 static int cap_vol_put(struct snd_kcontrol *kcontrol,
2178 struct snd_ctl_elem_value *ucontrol)
2180 return cap_put_caller(kcontrol, ucontrol,
2181 snd_hda_mixer_amp_volume_put,
2185 static const struct snd_kcontrol_new cap_vol_temp = {
2186 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2187 .name = "Capture Volume",
2188 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2189 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2190 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2191 .info = cap_vol_info,
2194 .tlv = { .c = cap_vol_tlv },
2197 /* capture switch ctl callbacks */
2198 #define cap_sw_info snd_ctl_boolean_stereo_info
2199 #define cap_sw_get snd_hda_mixer_amp_switch_get
2201 static int cap_sw_put(struct snd_kcontrol *kcontrol,
2202 struct snd_ctl_elem_value *ucontrol)
2204 return cap_put_caller(kcontrol, ucontrol,
2205 snd_hda_mixer_amp_switch_put,
2209 static const struct snd_kcontrol_new cap_sw_temp = {
2210 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2211 .name = "Capture Switch",
2212 .info = cap_sw_info,
2217 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
2222 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
2223 for (depth = 0; depth < 3; depth++) {
2224 if (depth >= path->depth)
2226 i = path->depth - depth - 1;
2227 nid = path->path[i];
2228 if (!path->ctls[NID_PATH_VOL_CTL]) {
2229 if (nid_has_volume(codec, nid, HDA_OUTPUT))
2230 path->ctls[NID_PATH_VOL_CTL] =
2231 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2232 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
2233 int idx = path->idx[i];
2234 if (!depth && codec->single_adc_amp)
2236 path->ctls[NID_PATH_VOL_CTL] =
2237 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2240 if (!path->ctls[NID_PATH_MUTE_CTL]) {
2241 if (nid_has_mute(codec, nid, HDA_OUTPUT))
2242 path->ctls[NID_PATH_MUTE_CTL] =
2243 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
2244 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
2245 int idx = path->idx[i];
2246 if (!depth && codec->single_adc_amp)
2248 path->ctls[NID_PATH_MUTE_CTL] =
2249 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
2256 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
2258 struct hda_gen_spec *spec = codec->spec;
2259 struct auto_pin_cfg *cfg = &spec->autocfg;
2263 if (!spec->inv_dmic_split)
2265 for (i = 0; i < cfg->num_inputs; i++) {
2266 if (cfg->inputs[i].pin != nid)
2268 if (cfg->inputs[i].type != AUTO_PIN_MIC)
2270 val = snd_hda_codec_get_pincfg(codec, nid);
2271 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
2276 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
2277 int idx, bool is_switch, unsigned int ctl,
2280 struct hda_gen_spec *spec = codec->spec;
2282 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
2283 const char *sfx = is_switch ? "Switch" : "Volume";
2284 unsigned int chs = inv_dmic ? 1 : 3;
2291 snprintf(tmpname, sizeof(tmpname),
2292 "%s Capture %s", label, sfx);
2294 snprintf(tmpname, sizeof(tmpname),
2296 err = add_control(spec, type, tmpname, idx,
2297 amp_val_replace_channels(ctl, chs));
2298 if (err < 0 || !inv_dmic)
2301 /* Make independent right kcontrol */
2303 snprintf(tmpname, sizeof(tmpname),
2304 "Inverted %s Capture %s", label, sfx);
2306 snprintf(tmpname, sizeof(tmpname),
2307 "Inverted Capture %s", sfx);
2308 return add_control(spec, type, tmpname, idx,
2309 amp_val_replace_channels(ctl, 2));
2312 /* create single (and simple) capture volume and switch controls */
2313 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
2314 unsigned int vol_ctl, unsigned int sw_ctl,
2318 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
2321 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
2327 /* create bound capture volume and switch controls */
2328 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
2329 unsigned int vol_ctl, unsigned int sw_ctl)
2331 struct hda_gen_spec *spec = codec->spec;
2332 struct snd_kcontrol_new *knew;
2335 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
2339 knew->private_value = vol_ctl;
2340 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2343 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
2347 knew->private_value = sw_ctl;
2348 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
2353 /* return the vol ctl when used first in the imux list */
2354 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
2356 struct hda_gen_spec *spec = codec->spec;
2357 struct nid_path *path;
2361 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2362 get_adc_nid(codec, 0, idx));
2365 ctl = path->ctls[type];
2368 for (i = 0; i < idx - 1; i++) {
2369 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2370 get_adc_nid(codec, 0, i));
2371 if (path && path->ctls[type] == ctl)
2377 /* create individual capture volume and switch controls per input */
2378 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
2380 struct hda_gen_spec *spec = codec->spec;
2381 struct hda_input_mux *imux = &spec->input_mux;
2382 int i, err, type, type_idx = 0;
2383 const char *prev_label = NULL;
2385 for (i = 0; i < imux->num_items; i++) {
2388 label = hda_get_autocfg_input_label(codec, &spec->autocfg, i);
2389 if (prev_label && !strcmp(label, prev_label))
2394 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
2396 for (type = 0; type < 2; type++) {
2397 err = add_single_cap_ctl(codec, label, type_idx, type,
2398 get_first_cap_ctl(codec, i, type),
2407 static int create_capture_mixers(struct hda_codec *codec)
2409 struct hda_gen_spec *spec = codec->spec;
2410 struct hda_input_mux *imux = &spec->input_mux;
2411 int i, n, nums, err;
2413 if (spec->dyn_adc_switch)
2416 nums = spec->num_adc_nids;
2418 if (!spec->auto_mic && imux->num_items > 1) {
2419 struct snd_kcontrol_new *knew;
2421 name = nums > 1 ? "Input Source" : "Capture Source";
2422 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
2428 for (n = 0; n < nums; n++) {
2430 bool inv_dmic = false;
2434 for (i = 0; i < imux->num_items; i++) {
2435 struct nid_path *path;
2436 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
2437 get_adc_nid(codec, n, i));
2440 parse_capvol_in_path(codec, path);
2442 vol = path->ctls[NID_PATH_VOL_CTL];
2443 else if (vol != path->ctls[NID_PATH_VOL_CTL])
2446 sw = path->ctls[NID_PATH_MUTE_CTL];
2447 else if (sw != path->ctls[NID_PATH_MUTE_CTL])
2449 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
2454 err = create_single_cap_vol_ctl(codec, n, vol, sw,
2456 else if (!spec->multi_cap_vol)
2457 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
2459 err = create_multi_cap_vol_ctl(codec);
2468 * add mic boosts if needed
2470 static int parse_mic_boost(struct hda_codec *codec)
2472 struct hda_gen_spec *spec = codec->spec;
2473 struct auto_pin_cfg *cfg = &spec->autocfg;
2477 const char *prev_label = NULL;
2479 for (i = 0; i < cfg->num_inputs; i++) {
2480 if (cfg->inputs[i].type > AUTO_PIN_MIC)
2482 nid = cfg->inputs[i].pin;
2483 if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
2485 char boost_label[44];
2486 struct nid_path *path;
2489 label = hda_get_autocfg_input_label(codec, cfg, i);
2490 if (spec->shared_mic_hp && !strcmp(label, "Misc"))
2491 label = "Headphone Mic";
2492 if (prev_label && !strcmp(label, prev_label))
2498 snprintf(boost_label, sizeof(boost_label),
2499 "%s Boost Volume", label);
2500 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
2501 err = add_control(spec, HDA_CTL_WIDGET_VOL,
2502 boost_label, type_idx, val);
2506 path = snd_hda_get_nid_path(codec, nid, 0);
2508 path->ctls[NID_PATH_BOOST_CTL] = val;
2515 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
2517 static void parse_digital(struct hda_codec *codec)
2519 struct hda_gen_spec *spec = codec->spec;
2520 struct nid_path *path;
2524 /* support multiple SPDIFs; the secondary is set up as a slave */
2526 for (i = 0; i < spec->autocfg.dig_outs; i++) {
2527 hda_nid_t pin = spec->autocfg.dig_out_pins[i];
2528 dig_nid = look_for_dac(codec, pin, true);
2531 path = snd_hda_add_new_path(codec, dig_nid, pin, HDA_PARSE_ALL);
2534 print_nid_path("digout", path);
2535 path->active = true;
2536 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
2538 spec->multiout.dig_out_nid = dig_nid;
2539 spec->dig_out_type = spec->autocfg.dig_out_type[0];
2541 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
2542 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
2544 spec->slave_dig_outs[nums - 1] = dig_nid;
2549 if (spec->autocfg.dig_in_pin) {
2550 dig_nid = codec->start_nid;
2551 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
2552 unsigned int wcaps = get_wcaps(codec, dig_nid);
2553 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2555 if (!(wcaps & AC_WCAP_DIGITAL))
2557 path = snd_hda_add_new_path(codec,
2558 spec->autocfg.dig_in_pin,
2559 dig_nid, HDA_PARSE_ALL);
2561 print_nid_path("digin", path);
2562 path->active = true;
2563 spec->dig_in_nid = dig_nid;
2564 spec->digin_path = snd_hda_get_path_idx(codec, path);
2573 * input MUX handling
2576 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
2578 /* select the given imux item; either unmute exclusively or select the route */
2579 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2582 struct hda_gen_spec *spec = codec->spec;
2583 const struct hda_input_mux *imux;
2584 struct nid_path *path;
2586 imux = &spec->input_mux;
2587 if (!imux->num_items)
2590 if (idx >= imux->num_items)
2591 idx = imux->num_items - 1;
2592 if (spec->cur_mux[adc_idx] == idx)
2595 path = snd_hda_get_nid_path(codec,
2596 spec->imux_pins[spec->cur_mux[adc_idx]],
2597 spec->adc_nids[adc_idx]);
2601 snd_hda_activate_path(codec, path, false, false);
2603 spec->cur_mux[adc_idx] = idx;
2605 if (spec->shared_mic_hp)
2606 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
2608 if (spec->dyn_adc_switch)
2609 dyn_adc_pcm_resetup(codec, idx);
2611 path = snd_hda_get_nid_path(codec, spec->imux_pins[idx],
2612 get_adc_nid(codec, adc_idx, idx));
2617 snd_hda_activate_path(codec, path, true, false);
2618 if (spec->cap_sync_hook)
2619 spec->cap_sync_hook(codec);
2625 * Jack detections for HP auto-mute and mic-switch
2628 /* check each pin in the given array; returns true if any of them is plugged */
2629 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
2633 for (i = 0; i < num_pins; i++) {
2634 hda_nid_t nid = pins[i];
2637 present |= snd_hda_jack_detect(codec, nid);
2642 /* standard HP/line-out auto-mute helper */
2643 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
2644 bool mute, bool hp_out)
2646 struct hda_gen_spec *spec = codec->spec;
2647 unsigned int pin_bits = mute ? 0 : (hp_out ? PIN_HP : PIN_OUT);
2650 for (i = 0; i < num_pins; i++) {
2651 hda_nid_t nid = pins[i];
2655 /* don't reset VREF value in case it's controlling
2656 * the amp (see alc861_fixup_asus_amp_vref_0f())
2658 if (spec->keep_vref_in_automute) {
2659 val = snd_hda_codec_read(codec, nid, 0,
2660 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2665 snd_hda_set_pin_ctl_cache(codec, nid, val);
2666 set_pin_eapd(codec, nid, !mute);
2670 /* Toggle outputs muting */
2671 void snd_hda_gen_update_outputs(struct hda_codec *codec)
2673 struct hda_gen_spec *spec = codec->spec;
2676 /* Control HP pins/amps depending on master_mute state;
2677 * in general, HP pins/amps control should be enabled in all cases,
2678 * but currently set only for master_mute, just to be safe
2680 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
2681 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2682 spec->autocfg.hp_pins, spec->master_mute, true);
2684 if (!spec->automute_speaker)
2687 on = spec->hp_jack_present | spec->line_jack_present;
2688 on |= spec->master_mute;
2689 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
2690 spec->autocfg.speaker_pins, on, false);
2692 /* toggle line-out mutes if needed, too */
2693 /* if LO is a copy of either HP or Speaker, don't need to handle it */
2694 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
2695 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
2697 if (!spec->automute_lo)
2700 on = spec->hp_jack_present;
2701 on |= spec->master_mute;
2702 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2703 spec->autocfg.line_out_pins, on, false);
2705 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
2707 static void call_update_outputs(struct hda_codec *codec)
2709 struct hda_gen_spec *spec = codec->spec;
2710 if (spec->automute_hook)
2711 spec->automute_hook(codec);
2713 snd_hda_gen_update_outputs(codec);
2716 /* standard HP-automute helper */
2717 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2719 struct hda_gen_spec *spec = codec->spec;
2721 spec->hp_jack_present =
2722 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
2723 spec->autocfg.hp_pins);
2724 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
2726 call_update_outputs(codec);
2728 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
2730 /* standard line-out-automute helper */
2731 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
2733 struct hda_gen_spec *spec = codec->spec;
2735 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
2737 /* check LO jack only when it's different from HP */
2738 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
2741 spec->line_jack_present =
2742 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
2743 spec->autocfg.line_out_pins);
2744 if (!spec->automute_speaker || !spec->detect_lo)
2746 call_update_outputs(codec);
2748 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
2750 /* standard mic auto-switch helper */
2751 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
2753 struct hda_gen_spec *spec = codec->spec;
2756 if (!spec->auto_mic)
2759 for (i = spec->am_num_entries - 1; i > 0; i--) {
2760 if (snd_hda_jack_detect(codec, spec->am_entry[i].pin)) {
2761 mux_select(codec, 0, spec->am_entry[i].idx);
2765 mux_select(codec, 0, spec->am_entry[0].idx);
2767 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
2770 * Auto-Mute mode mixer enum support
2772 static int automute_mode_info(struct snd_kcontrol *kcontrol,
2773 struct snd_ctl_elem_info *uinfo)
2775 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2776 struct hda_gen_spec *spec = codec->spec;
2777 static const char * const texts3[] = {
2778 "Disabled", "Speaker Only", "Line Out+Speaker"
2781 if (spec->automute_speaker_possible && spec->automute_lo_possible)
2782 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
2783 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
2786 static int automute_mode_get(struct snd_kcontrol *kcontrol,
2787 struct snd_ctl_elem_value *ucontrol)
2789 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2790 struct hda_gen_spec *spec = codec->spec;
2791 unsigned int val = 0;
2792 if (spec->automute_speaker)
2794 if (spec->automute_lo)
2797 ucontrol->value.enumerated.item[0] = val;
2801 static int automute_mode_put(struct snd_kcontrol *kcontrol,
2802 struct snd_ctl_elem_value *ucontrol)
2804 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2805 struct hda_gen_spec *spec = codec->spec;
2807 switch (ucontrol->value.enumerated.item[0]) {
2809 if (!spec->automute_speaker && !spec->automute_lo)
2811 spec->automute_speaker = 0;
2812 spec->automute_lo = 0;
2815 if (spec->automute_speaker_possible) {
2816 if (!spec->automute_lo && spec->automute_speaker)
2818 spec->automute_speaker = 1;
2819 spec->automute_lo = 0;
2820 } else if (spec->automute_lo_possible) {
2821 if (spec->automute_lo)
2823 spec->automute_lo = 1;
2828 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
2830 if (spec->automute_speaker && spec->automute_lo)
2832 spec->automute_speaker = 1;
2833 spec->automute_lo = 1;
2838 call_update_outputs(codec);
2842 static const struct snd_kcontrol_new automute_mode_enum = {
2843 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2844 .name = "Auto-Mute Mode",
2845 .info = automute_mode_info,
2846 .get = automute_mode_get,
2847 .put = automute_mode_put,
2850 static int add_automute_mode_enum(struct hda_codec *codec)
2852 struct hda_gen_spec *spec = codec->spec;
2854 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
2860 * Check the availability of HP/line-out auto-mute;
2861 * Set up appropriately if really supported
2863 static int check_auto_mute_availability(struct hda_codec *codec)
2865 struct hda_gen_spec *spec = codec->spec;
2866 struct auto_pin_cfg *cfg = &spec->autocfg;
2870 if (cfg->hp_pins[0])
2872 if (cfg->line_out_pins[0])
2874 if (cfg->speaker_pins[0])
2876 if (present < 2) /* need two different output types */
2879 if (!cfg->speaker_pins[0] &&
2880 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
2881 memcpy(cfg->speaker_pins, cfg->line_out_pins,
2882 sizeof(cfg->speaker_pins));
2883 cfg->speaker_outs = cfg->line_outs;
2886 if (!cfg->hp_pins[0] &&
2887 cfg->line_out_type == AUTO_PIN_HP_OUT) {
2888 memcpy(cfg->hp_pins, cfg->line_out_pins,
2889 sizeof(cfg->hp_pins));
2890 cfg->hp_outs = cfg->line_outs;
2893 for (i = 0; i < cfg->hp_outs; i++) {
2894 hda_nid_t nid = cfg->hp_pins[i];
2895 if (!is_jack_detectable(codec, nid))
2897 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
2899 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
2900 spec->hp_automute_hook ?
2901 spec->hp_automute_hook :
2902 snd_hda_gen_hp_automute);
2903 spec->detect_hp = 1;
2906 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
2907 if (cfg->speaker_outs)
2908 for (i = 0; i < cfg->line_outs; i++) {
2909 hda_nid_t nid = cfg->line_out_pins[i];
2910 if (!is_jack_detectable(codec, nid))
2912 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
2913 snd_hda_jack_detect_enable_callback(codec, nid,
2914 HDA_GEN_FRONT_EVENT,
2915 spec->line_automute_hook ?
2916 spec->line_automute_hook :
2917 snd_hda_gen_line_automute);
2918 spec->detect_lo = 1;
2920 spec->automute_lo_possible = spec->detect_hp;
2923 spec->automute_speaker_possible = cfg->speaker_outs &&
2924 (spec->detect_hp || spec->detect_lo);
2926 spec->automute_lo = spec->automute_lo_possible;
2927 spec->automute_speaker = spec->automute_speaker_possible;
2929 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
2930 /* create a control for automute mode */
2931 err = add_automute_mode_enum(codec);
2938 /* return the position of NID in the list, or -1 if not found */
2939 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
2942 for (i = 0; i < nums; i++)
2948 /* check whether all auto-mic pins are valid; setup indices if OK */
2949 static bool auto_mic_check_imux(struct hda_codec *codec)
2951 struct hda_gen_spec *spec = codec->spec;
2952 const struct hda_input_mux *imux;
2955 imux = &spec->input_mux;
2956 for (i = 0; i < spec->am_num_entries; i++) {
2957 spec->am_entry[i].idx =
2958 find_idx_in_nid_list(spec->am_entry[i].pin,
2959 spec->imux_pins, imux->num_items);
2960 if (spec->am_entry[i].idx < 0)
2961 return false; /* no corresponding imux */
2964 /* we don't need the jack detection for the first pin */
2965 for (i = 1; i < spec->am_num_entries; i++)
2966 snd_hda_jack_detect_enable_callback(codec,
2967 spec->am_entry[i].pin,
2969 spec->mic_autoswitch_hook ?
2970 spec->mic_autoswitch_hook :
2971 snd_hda_gen_mic_autoswitch);
2975 static int compare_attr(const void *ap, const void *bp)
2977 const struct automic_entry *a = ap;
2978 const struct automic_entry *b = bp;
2979 return (int)(a->attr - b->attr);
2983 * Check the availability of auto-mic switch;
2984 * Set up if really supported
2986 static int check_auto_mic_availability(struct hda_codec *codec)
2988 struct hda_gen_spec *spec = codec->spec;
2989 struct auto_pin_cfg *cfg = &spec->autocfg;
2995 for (i = 0; i < cfg->num_inputs; i++) {
2996 hda_nid_t nid = cfg->inputs[i].pin;
2998 attr = snd_hda_codec_get_pincfg(codec, nid);
2999 attr = snd_hda_get_input_pin_attr(attr);
3000 if (types & (1 << attr))
3001 return 0; /* already occupied */
3003 case INPUT_PIN_ATTR_INT:
3004 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3005 return 0; /* invalid type */
3007 case INPUT_PIN_ATTR_UNUSED:
3008 return 0; /* invalid entry */
3010 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3011 return 0; /* invalid type */
3012 if (!spec->line_in_auto_switch &&
3013 cfg->inputs[i].type != AUTO_PIN_MIC)
3014 return 0; /* only mic is allowed */
3015 if (!is_jack_detectable(codec, nid))
3016 return 0; /* no unsol support */
3019 if (num_pins >= MAX_AUTO_MIC_PINS)
3021 types |= (1 << attr);
3022 spec->am_entry[num_pins].pin = nid;
3023 spec->am_entry[num_pins].attr = attr;
3030 spec->am_num_entries = num_pins;
3031 /* sort the am_entry in the order of attr so that the pin with a
3032 * higher attr will be selected when the jack is plugged.
3034 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3035 compare_attr, NULL);
3037 if (!auto_mic_check_imux(codec))
3041 spec->num_adc_nids = 1;
3042 spec->cur_mux[0] = spec->am_entry[0].idx;
3043 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3044 spec->am_entry[0].pin,
3045 spec->am_entry[1].pin,
3046 spec->am_entry[2].pin);
3053 * Parse the given BIOS configuration and set up the hda_gen_spec
3055 * return 1 if successful, 0 if the proper config is not found,
3056 * or a negative error code
3058 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
3059 struct auto_pin_cfg *cfg)
3061 struct hda_gen_spec *spec = codec->spec;
3064 if (cfg != &spec->autocfg) {
3065 spec->autocfg = *cfg;
3066 cfg = &spec->autocfg;
3069 if (!cfg->line_outs) {
3070 if (cfg->dig_outs || cfg->dig_in_pin) {
3071 spec->multiout.max_channels = 2;
3072 spec->no_analog = 1;
3075 return 0; /* can't find valid BIOS pin config */
3078 if (!spec->no_primary_hp &&
3079 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3080 cfg->line_outs <= cfg->hp_outs) {
3081 /* use HP as primary out */
3082 cfg->speaker_outs = cfg->line_outs;
3083 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3084 sizeof(cfg->speaker_pins));
3085 cfg->line_outs = cfg->hp_outs;
3086 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
3088 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3089 cfg->line_out_type = AUTO_PIN_HP_OUT;
3092 err = parse_output_paths(codec);
3095 err = create_multi_channel_mode(codec);
3098 err = create_multi_out_ctls(codec, cfg);
3101 err = create_hp_out_ctls(codec);
3104 err = create_speaker_out_ctls(codec);
3107 err = create_indep_hp_ctls(codec);
3110 err = create_loopback_mixing_ctl(codec);
3113 err = create_shared_input(codec);
3116 err = create_input_ctls(codec);
3120 /* check the multiple speaker pins */
3121 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
3122 spec->const_channel_count = cfg->line_outs * 2;
3124 spec->const_channel_count = cfg->speaker_outs * 2;
3126 if (spec->multi_ios > 0)
3127 spec->multiout.max_channels = max(spec->ext_channel_count,
3128 spec->const_channel_count);
3130 spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3132 err = check_auto_mute_availability(codec);
3136 err = check_dyn_adc_switch(codec);
3140 if (!spec->shared_mic_hp) {
3141 err = check_auto_mic_availability(codec);
3146 err = create_capture_mixers(codec);
3150 err = parse_mic_boost(codec);
3155 parse_digital(codec);
3159 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
3163 * Build control elements
3166 /* slave controls for virtual master */
3167 static const char * const slave_pfxs[] = {
3168 "Front", "Surround", "Center", "LFE", "Side",
3169 "Headphone", "Speaker", "Mono", "Line Out",
3170 "CLFE", "Bass Speaker", "PCM",
3171 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3172 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3177 int snd_hda_gen_build_controls(struct hda_codec *codec)
3179 struct hda_gen_spec *spec = codec->spec;
3182 if (spec->kctls.used) {
3183 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
3188 if (spec->multiout.dig_out_nid) {
3189 err = snd_hda_create_dig_out_ctls(codec,
3190 spec->multiout.dig_out_nid,
3191 spec->multiout.dig_out_nid,
3192 spec->pcm_rec[1].pcm_type);
3195 if (!spec->no_analog) {
3196 err = snd_hda_create_spdif_share_sw(codec,
3200 spec->multiout.share_spdif = 1;
3203 if (spec->dig_in_nid) {
3204 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
3209 /* if we have no master control, let's create it */
3210 if (!spec->no_analog &&
3211 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
3212 unsigned int vmaster_tlv[4];
3213 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
3214 HDA_OUTPUT, vmaster_tlv);
3215 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
3216 vmaster_tlv, slave_pfxs,
3221 if (!spec->no_analog &&
3222 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
3223 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
3226 true, &spec->vmaster_mute.sw_kctl);
3229 if (spec->vmaster_mute.hook)
3230 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
3231 spec->vmaster_mute_enum);
3234 free_kctls(spec); /* no longer needed */
3236 if (spec->shared_mic_hp) {
3238 int nid = spec->autocfg.inputs[1].pin;
3239 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
3242 err = snd_hda_jack_detect_enable(codec, nid, 0);
3247 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
3253 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
3260 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
3261 struct hda_codec *codec,
3262 struct snd_pcm_substream *substream,
3265 struct hda_gen_spec *spec = codec->spec;
3266 if (spec->pcm_playback_hook)
3267 spec->pcm_playback_hook(hinfo, codec, substream, action);
3271 * Analog playback callbacks
3273 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
3274 struct hda_codec *codec,
3275 struct snd_pcm_substream *substream)
3277 struct hda_gen_spec *spec = codec->spec;
3280 mutex_lock(&spec->pcm_mutex);
3281 err = snd_hda_multi_out_analog_open(codec,
3282 &spec->multiout, substream,
3285 spec->active_streams |= 1 << STREAM_MULTI_OUT;
3286 call_pcm_playback_hook(hinfo, codec, substream,
3287 HDA_GEN_PCM_ACT_OPEN);
3289 mutex_unlock(&spec->pcm_mutex);
3293 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3294 struct hda_codec *codec,
3295 unsigned int stream_tag,
3296 unsigned int format,
3297 struct snd_pcm_substream *substream)
3299 struct hda_gen_spec *spec = codec->spec;
3302 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
3303 stream_tag, format, substream);
3305 call_pcm_playback_hook(hinfo, codec, substream,
3306 HDA_GEN_PCM_ACT_PREPARE);
3310 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3311 struct hda_codec *codec,
3312 struct snd_pcm_substream *substream)
3314 struct hda_gen_spec *spec = codec->spec;
3317 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
3319 call_pcm_playback_hook(hinfo, codec, substream,
3320 HDA_GEN_PCM_ACT_CLEANUP);
3324 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
3325 struct hda_codec *codec,
3326 struct snd_pcm_substream *substream)
3328 struct hda_gen_spec *spec = codec->spec;
3329 mutex_lock(&spec->pcm_mutex);
3330 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
3331 call_pcm_playback_hook(hinfo, codec, substream,
3332 HDA_GEN_PCM_ACT_CLOSE);
3333 mutex_unlock(&spec->pcm_mutex);
3337 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
3338 struct hda_codec *codec,
3339 struct snd_pcm_substream *substream)
3341 struct hda_gen_spec *spec = codec->spec;
3344 mutex_lock(&spec->pcm_mutex);
3345 if (!spec->indep_hp_enabled)
3348 spec->active_streams |= 1 << STREAM_INDEP_HP;
3349 call_pcm_playback_hook(hinfo, codec, substream,
3350 HDA_GEN_PCM_ACT_OPEN);
3351 mutex_unlock(&spec->pcm_mutex);
3355 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
3356 struct hda_codec *codec,
3357 struct snd_pcm_substream *substream)
3359 struct hda_gen_spec *spec = codec->spec;
3360 mutex_lock(&spec->pcm_mutex);
3361 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
3362 call_pcm_playback_hook(hinfo, codec, substream,
3363 HDA_GEN_PCM_ACT_CLOSE);
3364 mutex_unlock(&spec->pcm_mutex);
3368 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3369 struct hda_codec *codec,
3370 unsigned int stream_tag,
3371 unsigned int format,
3372 struct snd_pcm_substream *substream)
3374 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3375 call_pcm_playback_hook(hinfo, codec, substream,
3376 HDA_GEN_PCM_ACT_PREPARE);
3380 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3381 struct hda_codec *codec,
3382 struct snd_pcm_substream *substream)
3384 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3385 call_pcm_playback_hook(hinfo, codec, substream,
3386 HDA_GEN_PCM_ACT_CLEANUP);
3393 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
3394 struct hda_codec *codec,
3395 struct snd_pcm_substream *substream)
3397 struct hda_gen_spec *spec = codec->spec;
3398 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3401 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3402 struct hda_codec *codec,
3403 unsigned int stream_tag,
3404 unsigned int format,
3405 struct snd_pcm_substream *substream)
3407 struct hda_gen_spec *spec = codec->spec;
3408 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3409 stream_tag, format, substream);
3412 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
3413 struct hda_codec *codec,
3414 struct snd_pcm_substream *substream)
3416 struct hda_gen_spec *spec = codec->spec;
3417 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
3420 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
3421 struct hda_codec *codec,
3422 struct snd_pcm_substream *substream)
3424 struct hda_gen_spec *spec = codec->spec;
3425 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3431 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3432 struct hda_codec *codec,
3433 unsigned int stream_tag,
3434 unsigned int format,
3435 struct snd_pcm_substream *substream)
3437 struct hda_gen_spec *spec = codec->spec;
3439 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
3440 stream_tag, 0, format);
3444 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3445 struct hda_codec *codec,
3446 struct snd_pcm_substream *substream)
3448 struct hda_gen_spec *spec = codec->spec;
3450 snd_hda_codec_cleanup_stream(codec,
3451 spec->adc_nids[substream->number + 1]);
3457 static const struct hda_pcm_stream pcm_analog_playback = {
3461 /* NID is set in build_pcms */
3463 .open = playback_pcm_open,
3464 .close = playback_pcm_close,
3465 .prepare = playback_pcm_prepare,
3466 .cleanup = playback_pcm_cleanup
3470 static const struct hda_pcm_stream pcm_analog_capture = {
3474 /* NID is set in build_pcms */
3477 static const struct hda_pcm_stream pcm_analog_alt_playback = {
3481 /* NID is set in build_pcms */
3483 .open = alt_playback_pcm_open,
3484 .close = alt_playback_pcm_close,
3485 .prepare = alt_playback_pcm_prepare,
3486 .cleanup = alt_playback_pcm_cleanup
3490 static const struct hda_pcm_stream pcm_analog_alt_capture = {
3491 .substreams = 2, /* can be overridden */
3494 /* NID is set in build_pcms */
3496 .prepare = alt_capture_pcm_prepare,
3497 .cleanup = alt_capture_pcm_cleanup
3501 static const struct hda_pcm_stream pcm_digital_playback = {
3505 /* NID is set in build_pcms */
3507 .open = dig_playback_pcm_open,
3508 .close = dig_playback_pcm_close,
3509 .prepare = dig_playback_pcm_prepare,
3510 .cleanup = dig_playback_pcm_cleanup
3514 static const struct hda_pcm_stream pcm_digital_capture = {
3518 /* NID is set in build_pcms */
3521 /* Used by build_pcms to flag that a PCM has no playback stream */
3522 static const struct hda_pcm_stream pcm_null_stream = {
3529 * dynamic changing ADC PCM streams
3531 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
3533 struct hda_gen_spec *spec = codec->spec;
3534 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
3536 if (spec->cur_adc && spec->cur_adc != new_adc) {
3537 /* stream is running, let's swap the current ADC */
3538 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3539 spec->cur_adc = new_adc;
3540 snd_hda_codec_setup_stream(codec, new_adc,
3541 spec->cur_adc_stream_tag, 0,
3542 spec->cur_adc_format);
3548 /* analog capture with dynamic dual-adc changes */
3549 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3550 struct hda_codec *codec,
3551 unsigned int stream_tag,
3552 unsigned int format,
3553 struct snd_pcm_substream *substream)
3555 struct hda_gen_spec *spec = codec->spec;
3556 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
3557 spec->cur_adc_stream_tag = stream_tag;
3558 spec->cur_adc_format = format;
3559 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
3563 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3564 struct hda_codec *codec,
3565 struct snd_pcm_substream *substream)
3567 struct hda_gen_spec *spec = codec->spec;
3568 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3573 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
3577 .nid = 0, /* fill later */
3579 .prepare = dyn_adc_capture_pcm_prepare,
3580 .cleanup = dyn_adc_capture_pcm_cleanup
3584 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
3585 const char *chip_name)
3591 strlcpy(str, chip_name, len);
3593 /* drop non-alnum chars after a space */
3594 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
3595 if (!isalnum(p[1])) {
3600 strlcat(str, sfx, len);
3603 /* build PCM streams based on the parsed results */
3604 int snd_hda_gen_build_pcms(struct hda_codec *codec)
3606 struct hda_gen_spec *spec = codec->spec;
3607 struct hda_pcm *info = spec->pcm_rec;
3608 const struct hda_pcm_stream *p;
3609 bool have_multi_adcs;
3611 codec->num_pcms = 1;
3612 codec->pcm_info = info;
3614 if (spec->no_analog)
3617 fill_pcm_stream_name(spec->stream_name_analog,
3618 sizeof(spec->stream_name_analog),
3619 " Analog", codec->chip_name);
3620 info->name = spec->stream_name_analog;
3622 if (spec->multiout.num_dacs > 0) {
3623 p = spec->stream_analog_playback;
3625 p = &pcm_analog_playback;
3626 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3627 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
3628 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
3629 spec->multiout.max_channels;
3630 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
3631 spec->autocfg.line_outs == 2)
3632 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
3635 if (spec->num_adc_nids) {
3636 p = spec->stream_analog_capture;
3638 if (spec->dyn_adc_switch)
3639 p = &dyn_adc_pcm_analog_capture;
3641 p = &pcm_analog_capture;
3643 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3644 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
3648 /* SPDIF for stream index #1 */
3649 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
3650 fill_pcm_stream_name(spec->stream_name_digital,
3651 sizeof(spec->stream_name_digital),
3652 " Digital", codec->chip_name);
3653 codec->num_pcms = 2;
3654 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
3655 info = spec->pcm_rec + 1;
3656 info->name = spec->stream_name_digital;
3657 if (spec->dig_out_type)
3658 info->pcm_type = spec->dig_out_type;
3660 info->pcm_type = HDA_PCM_TYPE_SPDIF;
3661 if (spec->multiout.dig_out_nid) {
3662 p = spec->stream_digital_playback;
3664 p = &pcm_digital_playback;
3665 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3666 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
3668 if (spec->dig_in_nid) {
3669 p = spec->stream_digital_capture;
3671 p = &pcm_digital_capture;
3672 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3673 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
3677 if (spec->no_analog)
3680 /* If the use of more than one ADC is requested for the current
3681 * model, configure a second analog capture-only PCM.
3683 have_multi_adcs = (spec->num_adc_nids > 1) &&
3684 !spec->dyn_adc_switch && !spec->auto_mic;
3685 /* Additional Analaog capture for index #2 */
3686 if (spec->alt_dac_nid || have_multi_adcs) {
3687 codec->num_pcms = 3;
3688 info = spec->pcm_rec + 2;
3689 info->name = spec->stream_name_analog;
3690 if (spec->alt_dac_nid) {
3691 p = spec->stream_analog_alt_playback;
3693 p = &pcm_analog_alt_playback;
3694 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
3695 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
3698 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
3700 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
3702 if (have_multi_adcs) {
3703 p = spec->stream_analog_alt_capture;
3705 p = &pcm_analog_alt_capture;
3706 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
3707 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
3709 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
3710 spec->num_adc_nids - 1;
3712 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
3714 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
3720 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
3724 * Standard auto-parser initializations
3727 /* configure the given path as a proper output */
3728 static void set_output_and_unmute(struct hda_codec *codec,
3729 int pin_type, int path_idx)
3731 struct nid_path *path;
3734 path = snd_hda_get_path_from_idx(codec, path_idx);
3735 if (!path || !path->depth)
3737 pin = path->path[path->depth - 1];
3738 snd_hda_set_pin_ctl_cache(codec, pin, pin_type);
3739 snd_hda_activate_path(codec, path, path->active, true);
3740 set_pin_eapd(codec, pin, path->active);
3743 /* initialize primary output paths */
3744 static void init_multi_out(struct hda_codec *codec)
3746 struct hda_gen_spec *spec = codec->spec;
3750 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
3755 for (i = 0; i < spec->autocfg.line_outs; i++)
3756 set_output_and_unmute(codec, pin_type, spec->out_paths[i]);
3760 static void __init_extra_out(struct hda_codec *codec, int num_outs,
3761 int *paths, int type)
3765 for (i = 0; i < num_outs; i++)
3766 set_output_and_unmute(codec, type, paths[i]);
3769 /* initialize hp and speaker paths */
3770 static void init_extra_out(struct hda_codec *codec)
3772 struct hda_gen_spec *spec = codec->spec;
3774 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
3775 __init_extra_out(codec, spec->autocfg.hp_outs,
3776 spec->hp_paths, PIN_HP);
3777 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
3778 __init_extra_out(codec, spec->autocfg.speaker_outs,
3779 spec->speaker_paths, PIN_OUT);
3782 /* initialize multi-io paths */
3783 static void init_multi_io(struct hda_codec *codec)
3785 struct hda_gen_spec *spec = codec->spec;
3788 for (i = 0; i < spec->multi_ios; i++) {
3789 hda_nid_t pin = spec->multi_io[i].pin;
3790 struct nid_path *path;
3791 path = get_multiio_path(codec, i);
3794 if (!spec->multi_io[i].ctl_in)
3795 spec->multi_io[i].ctl_in =
3796 snd_hda_codec_update_cache(codec, pin, 0,
3797 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
3798 snd_hda_activate_path(codec, path, path->active, true);
3802 /* set up the input pin config, depending on the given auto-pin type */
3803 static void set_input_pin(struct hda_codec *codec, hda_nid_t nid,
3806 unsigned int val = PIN_IN;
3807 if (auto_pin_type == AUTO_PIN_MIC)
3808 val |= snd_hda_get_default_vref(codec, nid);
3809 snd_hda_set_pin_ctl_cache(codec, nid, val);
3812 /* set up input pins and loopback paths */
3813 static void init_analog_input(struct hda_codec *codec)
3815 struct hda_gen_spec *spec = codec->spec;
3816 struct auto_pin_cfg *cfg = &spec->autocfg;
3819 for (i = 0; i < cfg->num_inputs; i++) {
3820 hda_nid_t nid = cfg->inputs[i].pin;
3821 if (is_input_pin(codec, nid))
3822 set_input_pin(codec, nid, cfg->inputs[i].type);
3824 /* init loopback inputs */
3825 if (spec->mixer_nid) {
3826 struct nid_path *path;
3827 path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
3829 snd_hda_activate_path(codec, path,
3830 path->active, false);
3835 /* initialize ADC paths */
3836 static void init_input_src(struct hda_codec *codec)
3838 struct hda_gen_spec *spec = codec->spec;
3839 struct hda_input_mux *imux = &spec->input_mux;
3840 struct nid_path *path;
3843 if (spec->dyn_adc_switch)
3846 nums = spec->num_adc_nids;
3848 for (c = 0; c < nums; c++) {
3849 for (i = 0; i < imux->num_items; i++) {
3850 path = snd_hda_get_nid_path(codec, spec->imux_pins[i],
3851 get_adc_nid(codec, c, i));
3853 bool active = path->active;
3854 if (i == spec->cur_mux[c])
3856 snd_hda_activate_path(codec, path, active, false);
3861 if (spec->shared_mic_hp)
3862 update_shared_mic_hp(codec, spec->cur_mux[0]);
3864 if (spec->cap_sync_hook)
3865 spec->cap_sync_hook(codec);
3868 /* set right pin controls for digital I/O */
3869 static void init_digital(struct hda_codec *codec)
3871 struct hda_gen_spec *spec = codec->spec;
3875 for (i = 0; i < spec->autocfg.dig_outs; i++)
3876 set_output_and_unmute(codec, PIN_OUT, spec->digout_paths[i]);
3877 pin = spec->autocfg.dig_in_pin;
3879 struct nid_path *path;
3880 snd_hda_set_pin_ctl_cache(codec, pin, PIN_IN);
3881 path = snd_hda_get_path_from_idx(codec, spec->digin_path);
3883 snd_hda_activate_path(codec, path, path->active, false);
3887 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3888 * invalid unsol tags by some reason
3890 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3894 for (i = 0; i < codec->init_pins.used; i++) {
3895 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3896 hda_nid_t nid = pin->nid;
3897 if (is_jack_detectable(codec, nid) &&
3898 !snd_hda_jack_tbl_get(codec, nid))
3899 snd_hda_codec_update_cache(codec, nid, 0,
3900 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3905 * initialize the generic spec;
3906 * this can be put as patch_ops.init function
3908 int snd_hda_gen_init(struct hda_codec *codec)
3910 struct hda_gen_spec *spec = codec->spec;
3912 if (spec->init_hook)
3913 spec->init_hook(codec);
3915 snd_hda_apply_verbs(codec);
3917 codec->cached_write = 1;
3919 init_multi_out(codec);
3920 init_extra_out(codec);
3921 init_multi_io(codec);
3922 init_analog_input(codec);
3923 init_input_src(codec);
3924 init_digital(codec);
3926 clear_unsol_on_unused_pins(codec);
3928 /* call init functions of standard auto-mute helpers */
3929 snd_hda_gen_hp_automute(codec, NULL);
3930 snd_hda_gen_line_automute(codec, NULL);
3931 snd_hda_gen_mic_autoswitch(codec, NULL);
3933 snd_hda_codec_flush_amp_cache(codec);
3934 snd_hda_codec_flush_cmd_cache(codec);
3936 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
3937 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
3939 hda_call_check_power_status(codec, 0x01);
3942 EXPORT_SYMBOL_HDA(snd_hda_gen_init);
3945 * free the generic spec;
3946 * this can be put as patch_ops.free function
3948 void snd_hda_gen_free(struct hda_codec *codec)
3950 snd_hda_gen_spec_free(codec->spec);
3954 EXPORT_SYMBOL_HDA(snd_hda_gen_free);
3958 * check the loopback power save state;
3959 * this can be put as patch_ops.check_power_status function
3961 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
3963 struct hda_gen_spec *spec = codec->spec;
3964 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
3966 EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
3971 * the generic codec support
3974 static const struct hda_codec_ops generic_patch_ops = {
3975 .build_controls = snd_hda_gen_build_controls,
3976 .build_pcms = snd_hda_gen_build_pcms,
3977 .init = snd_hda_gen_init,
3978 .free = snd_hda_gen_free,
3979 .unsol_event = snd_hda_jack_unsol_event,
3981 .check_power_status = snd_hda_gen_check_power_status,
3985 int snd_hda_parse_generic_codec(struct hda_codec *codec)
3987 struct hda_gen_spec *spec;
3990 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3993 snd_hda_gen_spec_init(spec);
3996 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4000 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
4004 codec->patch_ops = generic_patch_ops;
4008 snd_hda_gen_free(codec);
4011 EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);