Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-2.6/libata-dev.git] / sound / pci / hda / hda_generic.c
blob43c2ea5395618f9ca1c166c9125b006839714f6d
1 /*
2 * Universal Interface for Intel High Definition Audio Codec
4 * Generic widget tree parser
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/sort.h>
27 #include <linux/delay.h>
28 #include <linux/ctype.h>
29 #include <linux/string.h>
30 #include <linux/bitops.h>
31 #include <sound/core.h>
32 #include <sound/jack.h>
33 #include "hda_codec.h"
34 #include "hda_local.h"
35 #include "hda_auto_parser.h"
36 #include "hda_jack.h"
37 #include "hda_generic.h"
40 /* initialize hda_gen_spec struct */
41 int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
43 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
44 snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
45 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8);
46 mutex_init(&spec->pcm_mutex);
47 return 0;
49 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
51 struct snd_kcontrol_new *
52 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
53 const struct snd_kcontrol_new *temp)
55 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
56 if (!knew)
57 return NULL;
58 *knew = *temp;
59 if (name)
60 knew->name = kstrdup(name, GFP_KERNEL);
61 else if (knew->name)
62 knew->name = kstrdup(knew->name, GFP_KERNEL);
63 if (!knew->name)
64 return NULL;
65 return knew;
67 EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
69 static void free_kctls(struct hda_gen_spec *spec)
71 if (spec->kctls.list) {
72 struct snd_kcontrol_new *kctl = spec->kctls.list;
73 int i;
74 for (i = 0; i < spec->kctls.used; i++)
75 kfree(kctl[i].name);
77 snd_array_free(&spec->kctls);
80 void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
82 if (!spec)
83 return;
84 free_kctls(spec);
85 snd_array_free(&spec->paths);
86 snd_array_free(&spec->loopback_list);
88 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
91 * store user hints
93 static void parse_user_hints(struct hda_codec *codec)
95 struct hda_gen_spec *spec = codec->spec;
96 int val;
98 val = snd_hda_get_bool_hint(codec, "jack_detect");
99 if (val >= 0)
100 codec->no_jack_detect = !val;
101 val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
102 if (val >= 0)
103 codec->inv_jack_detect = !!val;
104 val = snd_hda_get_bool_hint(codec, "trigger_sense");
105 if (val >= 0)
106 codec->no_trigger_sense = !val;
107 val = snd_hda_get_bool_hint(codec, "inv_eapd");
108 if (val >= 0)
109 codec->inv_eapd = !!val;
110 val = snd_hda_get_bool_hint(codec, "pcm_format_first");
111 if (val >= 0)
112 codec->pcm_format_first = !!val;
113 val = snd_hda_get_bool_hint(codec, "sticky_stream");
114 if (val >= 0)
115 codec->no_sticky_stream = !val;
116 val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
117 if (val >= 0)
118 codec->spdif_status_reset = !!val;
119 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
120 if (val >= 0)
121 codec->pin_amp_workaround = !!val;
122 val = snd_hda_get_bool_hint(codec, "single_adc_amp");
123 if (val >= 0)
124 codec->single_adc_amp = !!val;
126 val = snd_hda_get_bool_hint(codec, "auto_mute");
127 if (val >= 0)
128 spec->suppress_auto_mute = !val;
129 val = snd_hda_get_bool_hint(codec, "auto_mic");
130 if (val >= 0)
131 spec->suppress_auto_mic = !val;
132 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
133 if (val >= 0)
134 spec->line_in_auto_switch = !!val;
135 val = snd_hda_get_bool_hint(codec, "need_dac_fix");
136 if (val >= 0)
137 spec->need_dac_fix = !!val;
138 val = snd_hda_get_bool_hint(codec, "primary_hp");
139 if (val >= 0)
140 spec->no_primary_hp = !val;
141 val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
142 if (val >= 0)
143 spec->multi_cap_vol = !!val;
144 val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
145 if (val >= 0)
146 spec->inv_dmic_split = !!val;
147 val = snd_hda_get_bool_hint(codec, "indep_hp");
148 if (val >= 0)
149 spec->indep_hp = !!val;
150 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
151 if (val >= 0)
152 spec->add_stereo_mix_input = !!val;
153 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
154 if (val >= 0)
155 spec->add_out_jack_modes = !!val;
156 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
157 if (val >= 0)
158 spec->add_in_jack_modes = !!val;
159 val = snd_hda_get_bool_hint(codec, "power_down_unused");
160 if (val >= 0)
161 spec->power_down_unused = !!val;
163 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
164 spec->mixer_nid = val;
168 * pin control value accesses
171 #define update_pin_ctl(codec, pin, val) \
172 snd_hda_codec_update_cache(codec, pin, 0, \
173 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
175 /* restore the pinctl based on the cached value */
176 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
178 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
181 /* set the pinctl target value and write it if requested */
182 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
183 unsigned int val, bool do_write)
185 if (!pin)
186 return;
187 val = snd_hda_correct_pin_ctl(codec, pin, val);
188 snd_hda_codec_set_pin_target(codec, pin, val);
189 if (do_write)
190 update_pin_ctl(codec, pin, val);
193 /* set pinctl target values for all given pins */
194 static void set_pin_targets(struct hda_codec *codec, int num_pins,
195 hda_nid_t *pins, unsigned int val)
197 int i;
198 for (i = 0; i < num_pins; i++)
199 set_pin_target(codec, pins[i], val, false);
203 * parsing paths
206 /* return the position of NID in the list, or -1 if not found */
207 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
209 int i;
210 for (i = 0; i < nums; i++)
211 if (list[i] == nid)
212 return i;
213 return -1;
216 /* return true if the given NID is contained in the path */
217 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
219 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
222 static struct nid_path *get_nid_path(struct hda_codec *codec,
223 hda_nid_t from_nid, hda_nid_t to_nid,
224 int anchor_nid)
226 struct hda_gen_spec *spec = codec->spec;
227 int i;
229 for (i = 0; i < spec->paths.used; i++) {
230 struct nid_path *path = snd_array_elem(&spec->paths, i);
231 if (path->depth <= 0)
232 continue;
233 if ((!from_nid || path->path[0] == from_nid) &&
234 (!to_nid || path->path[path->depth - 1] == to_nid)) {
235 if (!anchor_nid ||
236 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
237 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
238 return path;
241 return NULL;
244 /* get the path between the given NIDs;
245 * passing 0 to either @pin or @dac behaves as a wildcard
247 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
248 hda_nid_t from_nid, hda_nid_t to_nid)
250 return get_nid_path(codec, from_nid, to_nid, 0);
252 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
254 /* get the index number corresponding to the path instance;
255 * the index starts from 1, for easier checking the invalid value
257 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
259 struct hda_gen_spec *spec = codec->spec;
260 struct nid_path *array = spec->paths.list;
261 ssize_t idx;
263 if (!spec->paths.used)
264 return 0;
265 idx = path - array;
266 if (idx < 0 || idx >= spec->paths.used)
267 return 0;
268 return idx + 1;
270 EXPORT_SYMBOL_HDA(snd_hda_get_path_idx);
272 /* get the path instance corresponding to the given index number */
273 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
275 struct hda_gen_spec *spec = codec->spec;
277 if (idx <= 0 || idx > spec->paths.used)
278 return NULL;
279 return snd_array_elem(&spec->paths, idx - 1);
281 EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx);
283 /* check whether the given DAC is already found in any existing paths */
284 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
286 struct hda_gen_spec *spec = codec->spec;
287 int i;
289 for (i = 0; i < spec->paths.used; i++) {
290 struct nid_path *path = snd_array_elem(&spec->paths, i);
291 if (path->path[0] == nid)
292 return true;
294 return false;
297 /* check whether the given two widgets can be connected */
298 static bool is_reachable_path(struct hda_codec *codec,
299 hda_nid_t from_nid, hda_nid_t to_nid)
301 if (!from_nid || !to_nid)
302 return false;
303 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
306 /* nid, dir and idx */
307 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
309 /* check whether the given ctl is already assigned in any path elements */
310 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
312 struct hda_gen_spec *spec = codec->spec;
313 int i;
315 val &= AMP_VAL_COMPARE_MASK;
316 for (i = 0; i < spec->paths.used; i++) {
317 struct nid_path *path = snd_array_elem(&spec->paths, i);
318 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
319 return true;
321 return false;
324 /* check whether a control with the given (nid, dir, idx) was assigned */
325 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
326 int dir, int idx, int type)
328 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
329 return is_ctl_used(codec, val, type);
332 static void print_nid_path(const char *pfx, struct nid_path *path)
334 char buf[40];
335 int i;
338 buf[0] = 0;
339 for (i = 0; i < path->depth; i++) {
340 char tmp[4];
341 sprintf(tmp, ":%02x", path->path[i]);
342 strlcat(buf, tmp, sizeof(buf));
344 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
347 /* called recursively */
348 static bool __parse_nid_path(struct hda_codec *codec,
349 hda_nid_t from_nid, hda_nid_t to_nid,
350 int anchor_nid, struct nid_path *path,
351 int depth)
353 const hda_nid_t *conn;
354 int i, nums;
356 if (to_nid == anchor_nid)
357 anchor_nid = 0; /* anchor passed */
358 else if (to_nid == (hda_nid_t)(-anchor_nid))
359 return false; /* hit the exclusive nid */
361 nums = snd_hda_get_conn_list(codec, to_nid, &conn);
362 for (i = 0; i < nums; i++) {
363 if (conn[i] != from_nid) {
364 /* special case: when from_nid is 0,
365 * try to find an empty DAC
367 if (from_nid ||
368 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
369 is_dac_already_used(codec, conn[i]))
370 continue;
372 /* anchor is not requested or already passed? */
373 if (anchor_nid <= 0)
374 goto found;
376 if (depth >= MAX_NID_PATH_DEPTH)
377 return false;
378 for (i = 0; i < nums; i++) {
379 unsigned int type;
380 type = get_wcaps_type(get_wcaps(codec, conn[i]));
381 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
382 type == AC_WID_PIN)
383 continue;
384 if (__parse_nid_path(codec, from_nid, conn[i],
385 anchor_nid, path, depth + 1))
386 goto found;
388 return false;
390 found:
391 path->path[path->depth] = conn[i];
392 path->idx[path->depth + 1] = i;
393 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
394 path->multi[path->depth + 1] = 1;
395 path->depth++;
396 return true;
399 /* parse the widget path from the given nid to the target nid;
400 * when @from_nid is 0, try to find an empty DAC;
401 * when @anchor_nid is set to a positive value, only paths through the widget
402 * with the given value are evaluated.
403 * when @anchor_nid is set to a negative value, paths through the widget
404 * with the negative of given value are excluded, only other paths are chosen.
405 * when @anchor_nid is zero, no special handling about path selection.
407 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
408 hda_nid_t to_nid, int anchor_nid,
409 struct nid_path *path)
411 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
412 path->path[path->depth] = to_nid;
413 path->depth++;
414 return true;
416 return false;
418 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
421 * parse the path between the given NIDs and add to the path list.
422 * if no valid path is found, return NULL
424 struct nid_path *
425 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
426 hda_nid_t to_nid, int anchor_nid)
428 struct hda_gen_spec *spec = codec->spec;
429 struct nid_path *path;
431 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
432 return NULL;
434 /* check whether the path has been already added */
435 path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
436 if (path)
437 return path;
439 path = snd_array_new(&spec->paths);
440 if (!path)
441 return NULL;
442 memset(path, 0, sizeof(*path));
443 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
444 return path;
445 /* push back */
446 spec->paths.used--;
447 return NULL;
449 EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
451 /* clear the given path as invalid so that it won't be picked up later */
452 static void invalidate_nid_path(struct hda_codec *codec, int idx)
454 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
455 if (!path)
456 return;
457 memset(path, 0, sizeof(*path));
460 /* look for an empty DAC slot */
461 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
462 bool is_digital)
464 struct hda_gen_spec *spec = codec->spec;
465 bool cap_digital;
466 int i;
468 for (i = 0; i < spec->num_all_dacs; i++) {
469 hda_nid_t nid = spec->all_dacs[i];
470 if (!nid || is_dac_already_used(codec, nid))
471 continue;
472 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
473 if (is_digital != cap_digital)
474 continue;
475 if (is_reachable_path(codec, nid, pin))
476 return nid;
478 return 0;
481 /* replace the channels in the composed amp value with the given number */
482 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
484 val &= ~(0x3U << 16);
485 val |= chs << 16;
486 return val;
489 /* check whether the widget has the given amp capability for the direction */
490 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
491 int dir, unsigned int bits)
493 if (!nid)
494 return false;
495 if (get_wcaps(codec, nid) & (1 << (dir + 1)))
496 if (query_amp_caps(codec, nid, dir) & bits)
497 return true;
498 return false;
501 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
502 hda_nid_t nid2, int dir)
504 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
505 return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
506 return (query_amp_caps(codec, nid1, dir) ==
507 query_amp_caps(codec, nid2, dir));
510 #define nid_has_mute(codec, nid, dir) \
511 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
512 #define nid_has_volume(codec, nid, dir) \
513 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
515 /* look for a widget suitable for assigning a mute switch in the path */
516 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
517 struct nid_path *path)
519 int i;
521 for (i = path->depth - 1; i >= 0; i--) {
522 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
523 return path->path[i];
524 if (i != path->depth - 1 && i != 0 &&
525 nid_has_mute(codec, path->path[i], HDA_INPUT))
526 return path->path[i];
528 return 0;
531 /* look for a widget suitable for assigning a volume ctl in the path */
532 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
533 struct nid_path *path)
535 int i;
537 for (i = path->depth - 1; i >= 0; i--) {
538 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
539 return path->path[i];
541 return 0;
545 * path activation / deactivation
548 /* can have the amp-in capability? */
549 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
551 hda_nid_t nid = path->path[idx];
552 unsigned int caps = get_wcaps(codec, nid);
553 unsigned int type = get_wcaps_type(caps);
555 if (!(caps & AC_WCAP_IN_AMP))
556 return false;
557 if (type == AC_WID_PIN && idx > 0) /* only for input pins */
558 return false;
559 return true;
562 /* can have the amp-out capability? */
563 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
565 hda_nid_t nid = path->path[idx];
566 unsigned int caps = get_wcaps(codec, nid);
567 unsigned int type = get_wcaps_type(caps);
569 if (!(caps & AC_WCAP_OUT_AMP))
570 return false;
571 if (type == AC_WID_PIN && !idx) /* only for output pins */
572 return false;
573 return true;
576 /* check whether the given (nid,dir,idx) is active */
577 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
578 unsigned int dir, unsigned int idx)
580 struct hda_gen_spec *spec = codec->spec;
581 int i, n;
583 for (n = 0; n < spec->paths.used; n++) {
584 struct nid_path *path = snd_array_elem(&spec->paths, n);
585 if (!path->active)
586 continue;
587 for (i = 0; i < path->depth; i++) {
588 if (path->path[i] == nid) {
589 if (dir == HDA_OUTPUT || path->idx[i] == idx)
590 return true;
591 break;
595 return false;
598 /* get the default amp value for the target state */
599 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
600 int dir, unsigned int caps, bool enable)
602 unsigned int val = 0;
604 if (caps & AC_AMPCAP_NUM_STEPS) {
605 /* set to 0dB */
606 if (enable)
607 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
609 if (caps & AC_AMPCAP_MUTE) {
610 if (!enable)
611 val |= HDA_AMP_MUTE;
613 return val;
616 /* initialize the amp value (only at the first time) */
617 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
619 unsigned int caps = query_amp_caps(codec, nid, dir);
620 int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
621 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
624 /* calculate amp value mask we can modify;
625 * if the given amp is controlled by mixers, don't touch it
627 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
628 hda_nid_t nid, int dir, int idx,
629 unsigned int caps)
631 unsigned int mask = 0xff;
633 if (caps & AC_AMPCAP_MUTE) {
634 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
635 mask &= ~0x80;
637 if (caps & AC_AMPCAP_NUM_STEPS) {
638 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
639 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
640 mask &= ~0x7f;
642 return mask;
645 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
646 int idx, int idx_to_check, bool enable)
648 unsigned int caps;
649 unsigned int mask, val;
651 if (!enable && is_active_nid(codec, nid, dir, idx_to_check))
652 return;
654 caps = query_amp_caps(codec, nid, dir);
655 val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
656 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
657 if (!mask)
658 return;
660 val &= mask;
661 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
664 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
665 int i, bool enable)
667 hda_nid_t nid = path->path[i];
668 init_amp(codec, nid, HDA_OUTPUT, 0);
669 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
672 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
673 int i, bool enable, bool add_aamix)
675 struct hda_gen_spec *spec = codec->spec;
676 const hda_nid_t *conn;
677 int n, nums, idx;
678 int type;
679 hda_nid_t nid = path->path[i];
681 nums = snd_hda_get_conn_list(codec, nid, &conn);
682 type = get_wcaps_type(get_wcaps(codec, nid));
683 if (type == AC_WID_PIN ||
684 (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
685 nums = 1;
686 idx = 0;
687 } else
688 idx = path->idx[i];
690 for (n = 0; n < nums; n++)
691 init_amp(codec, nid, HDA_INPUT, n);
693 /* here is a little bit tricky in comparison with activate_amp_out();
694 * when aa-mixer is available, we need to enable the path as well
696 for (n = 0; n < nums; n++) {
697 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid))
698 continue;
699 activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
703 /* activate or deactivate the given path
704 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
706 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
707 bool enable, bool add_aamix)
709 struct hda_gen_spec *spec = codec->spec;
710 int i;
712 if (!enable)
713 path->active = false;
715 for (i = path->depth - 1; i >= 0; i--) {
716 hda_nid_t nid = path->path[i];
717 if (enable && spec->power_down_unused) {
718 /* make sure the widget is powered up */
719 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0))
720 snd_hda_codec_write(codec, nid, 0,
721 AC_VERB_SET_POWER_STATE,
722 AC_PWRST_D0);
724 if (enable && path->multi[i])
725 snd_hda_codec_write_cache(codec, nid, 0,
726 AC_VERB_SET_CONNECT_SEL,
727 path->idx[i]);
728 if (has_amp_in(codec, path, i))
729 activate_amp_in(codec, path, i, enable, add_aamix);
730 if (has_amp_out(codec, path, i))
731 activate_amp_out(codec, path, i, enable);
734 if (enable)
735 path->active = true;
737 EXPORT_SYMBOL_HDA(snd_hda_activate_path);
739 /* if the given path is inactive, put widgets into D3 (only if suitable) */
740 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path)
742 struct hda_gen_spec *spec = codec->spec;
743 bool changed;
744 int i;
746 if (!spec->power_down_unused || path->active)
747 return;
749 for (i = 0; i < path->depth; i++) {
750 hda_nid_t nid = path->path[i];
751 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3)) {
752 snd_hda_codec_write(codec, nid, 0,
753 AC_VERB_SET_POWER_STATE,
754 AC_PWRST_D3);
755 changed = true;
759 if (changed) {
760 msleep(10);
761 snd_hda_codec_read(codec, path->path[0], 0,
762 AC_VERB_GET_POWER_STATE, 0);
766 /* turn on/off EAPD on the given pin */
767 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
769 struct hda_gen_spec *spec = codec->spec;
770 if (spec->own_eapd_ctl ||
771 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
772 return;
773 if (codec->inv_eapd)
774 enable = !enable;
775 snd_hda_codec_update_cache(codec, pin, 0,
776 AC_VERB_SET_EAPD_BTLENABLE,
777 enable ? 0x02 : 0x00);
780 /* re-initialize the path specified by the given path index */
781 static void resume_path_from_idx(struct hda_codec *codec, int path_idx)
783 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
784 if (path)
785 snd_hda_activate_path(codec, path, path->active, false);
790 * Helper functions for creating mixer ctl elements
793 enum {
794 HDA_CTL_WIDGET_VOL,
795 HDA_CTL_WIDGET_MUTE,
796 HDA_CTL_BIND_MUTE,
798 static const struct snd_kcontrol_new control_templates[] = {
799 HDA_CODEC_VOLUME(NULL, 0, 0, 0),
800 HDA_CODEC_MUTE(NULL, 0, 0, 0),
801 HDA_BIND_MUTE(NULL, 0, 0, 0),
804 /* add dynamic controls from template */
805 static struct snd_kcontrol_new *
806 add_control(struct hda_gen_spec *spec, int type, const char *name,
807 int cidx, unsigned long val)
809 struct snd_kcontrol_new *knew;
811 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
812 if (!knew)
813 return NULL;
814 knew->index = cidx;
815 if (get_amp_nid_(val))
816 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
817 knew->private_value = val;
818 return knew;
821 static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
822 const char *pfx, const char *dir,
823 const char *sfx, int cidx, unsigned long val)
825 char name[32];
826 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
827 if (!add_control(spec, type, name, cidx, val))
828 return -ENOMEM;
829 return 0;
832 #define add_pb_vol_ctrl(spec, type, pfx, val) \
833 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
834 #define add_pb_sw_ctrl(spec, type, pfx, val) \
835 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
836 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
837 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
838 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
839 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
841 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
842 unsigned int chs, struct nid_path *path)
844 unsigned int val;
845 if (!path)
846 return 0;
847 val = path->ctls[NID_PATH_VOL_CTL];
848 if (!val)
849 return 0;
850 val = amp_val_replace_channels(val, chs);
851 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
854 /* return the channel bits suitable for the given path->ctls[] */
855 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
856 int type)
858 int chs = 1; /* mono (left only) */
859 if (path) {
860 hda_nid_t nid = get_amp_nid_(path->ctls[type]);
861 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
862 chs = 3; /* stereo */
864 return chs;
867 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
868 struct nid_path *path)
870 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
871 return add_vol_ctl(codec, pfx, cidx, chs, path);
874 /* create a mute-switch for the given mixer widget;
875 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
877 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
878 unsigned int chs, struct nid_path *path)
880 unsigned int val;
881 int type = HDA_CTL_WIDGET_MUTE;
883 if (!path)
884 return 0;
885 val = path->ctls[NID_PATH_MUTE_CTL];
886 if (!val)
887 return 0;
888 val = amp_val_replace_channels(val, chs);
889 if (get_amp_direction_(val) == HDA_INPUT) {
890 hda_nid_t nid = get_amp_nid_(val);
891 int nums = snd_hda_get_num_conns(codec, nid);
892 if (nums > 1) {
893 type = HDA_CTL_BIND_MUTE;
894 val |= nums << 19;
897 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
900 static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
901 int cidx, struct nid_path *path)
903 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
904 return add_sw_ctl(codec, pfx, cidx, chs, path);
907 /* any ctl assigned to the path with the given index? */
908 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
910 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
911 return path && path->ctls[ctl_type];
914 static const char * const channel_name[4] = {
915 "Front", "Surround", "CLFE", "Side"
918 /* give some appropriate ctl name prefix for the given line out channel */
919 static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
920 int *index, int ctl_type)
922 struct hda_gen_spec *spec = codec->spec;
923 struct auto_pin_cfg *cfg = &spec->autocfg;
925 *index = 0;
926 if (cfg->line_outs == 1 && !spec->multi_ios &&
927 !cfg->hp_outs && !cfg->speaker_outs)
928 return spec->vmaster_mute.hook ? "PCM" : "Master";
930 /* if there is really a single DAC used in the whole output paths,
931 * use it master (or "PCM" if a vmaster hook is present)
933 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
934 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
935 return spec->vmaster_mute.hook ? "PCM" : "Master";
937 /* multi-io channels */
938 if (ch >= cfg->line_outs)
939 return channel_name[ch];
941 switch (cfg->line_out_type) {
942 case AUTO_PIN_SPEAKER_OUT:
943 /* if the primary channel vol/mute is shared with HP volume,
944 * don't name it as Speaker
946 if (!ch && cfg->hp_outs &&
947 !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
948 break;
949 if (cfg->line_outs == 1)
950 return "Speaker";
951 if (cfg->line_outs == 2)
952 return ch ? "Bass Speaker" : "Speaker";
953 break;
954 case AUTO_PIN_HP_OUT:
955 /* if the primary channel vol/mute is shared with spk volume,
956 * don't name it as Headphone
958 if (!ch && cfg->speaker_outs &&
959 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
960 break;
961 /* for multi-io case, only the primary out */
962 if (ch && spec->multi_ios)
963 break;
964 *index = ch;
965 return "Headphone";
968 /* for a single channel output, we don't have to name the channel */
969 if (cfg->line_outs == 1 && !spec->multi_ios)
970 return "PCM";
972 if (ch >= ARRAY_SIZE(channel_name)) {
973 snd_BUG();
974 return "PCM";
977 return channel_name[ch];
981 * Parse output paths
984 /* badness definition */
985 enum {
986 /* No primary DAC is found for the main output */
987 BAD_NO_PRIMARY_DAC = 0x10000,
988 /* No DAC is found for the extra output */
989 BAD_NO_DAC = 0x4000,
990 /* No possible multi-ios */
991 BAD_MULTI_IO = 0x120,
992 /* No individual DAC for extra output */
993 BAD_NO_EXTRA_DAC = 0x102,
994 /* No individual DAC for extra surrounds */
995 BAD_NO_EXTRA_SURR_DAC = 0x101,
996 /* Primary DAC shared with main surrounds */
997 BAD_SHARED_SURROUND = 0x100,
998 /* No independent HP possible */
999 BAD_NO_INDEP_HP = 0x40,
1000 /* Primary DAC shared with main CLFE */
1001 BAD_SHARED_CLFE = 0x10,
1002 /* Primary DAC shared with extra surrounds */
1003 BAD_SHARED_EXTRA_SURROUND = 0x10,
1004 /* Volume widget is shared */
1005 BAD_SHARED_VOL = 0x10,
1008 /* look for widgets in the given path which are appropriate for
1009 * volume and mute controls, and assign the values to ctls[].
1011 * When no appropriate widget is found in the path, the badness value
1012 * is incremented depending on the situation. The function returns the
1013 * total badness for both volume and mute controls.
1015 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
1017 hda_nid_t nid;
1018 unsigned int val;
1019 int badness = 0;
1021 if (!path)
1022 return BAD_SHARED_VOL * 2;
1024 if (path->ctls[NID_PATH_VOL_CTL] ||
1025 path->ctls[NID_PATH_MUTE_CTL])
1026 return 0; /* already evaluated */
1028 nid = look_for_out_vol_nid(codec, path);
1029 if (nid) {
1030 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1031 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
1032 badness += BAD_SHARED_VOL;
1033 else
1034 path->ctls[NID_PATH_VOL_CTL] = val;
1035 } else
1036 badness += BAD_SHARED_VOL;
1037 nid = look_for_out_mute_nid(codec, path);
1038 if (nid) {
1039 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
1040 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
1041 nid_has_mute(codec, nid, HDA_OUTPUT))
1042 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
1043 else
1044 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
1045 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
1046 badness += BAD_SHARED_VOL;
1047 else
1048 path->ctls[NID_PATH_MUTE_CTL] = val;
1049 } else
1050 badness += BAD_SHARED_VOL;
1051 return badness;
1054 struct badness_table {
1055 int no_primary_dac; /* no primary DAC */
1056 int no_dac; /* no secondary DACs */
1057 int shared_primary; /* primary DAC is shared with main output */
1058 int shared_surr; /* secondary DAC shared with main or primary */
1059 int shared_clfe; /* third DAC shared with main or primary */
1060 int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
1063 static struct badness_table main_out_badness = {
1064 .no_primary_dac = BAD_NO_PRIMARY_DAC,
1065 .no_dac = BAD_NO_DAC,
1066 .shared_primary = BAD_NO_PRIMARY_DAC,
1067 .shared_surr = BAD_SHARED_SURROUND,
1068 .shared_clfe = BAD_SHARED_CLFE,
1069 .shared_surr_main = BAD_SHARED_SURROUND,
1072 static struct badness_table extra_out_badness = {
1073 .no_primary_dac = BAD_NO_DAC,
1074 .no_dac = BAD_NO_DAC,
1075 .shared_primary = BAD_NO_EXTRA_DAC,
1076 .shared_surr = BAD_SHARED_EXTRA_SURROUND,
1077 .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
1078 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
1081 /* get the DAC of the primary output corresponding to the given array index */
1082 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
1084 struct hda_gen_spec *spec = codec->spec;
1085 struct auto_pin_cfg *cfg = &spec->autocfg;
1087 if (cfg->line_outs > idx)
1088 return spec->private_dac_nids[idx];
1089 idx -= cfg->line_outs;
1090 if (spec->multi_ios > idx)
1091 return spec->multi_io[idx].dac;
1092 return 0;
1095 /* return the DAC if it's reachable, otherwise zero */
1096 static inline hda_nid_t try_dac(struct hda_codec *codec,
1097 hda_nid_t dac, hda_nid_t pin)
1099 return is_reachable_path(codec, dac, pin) ? dac : 0;
1102 /* try to assign DACs to pins and return the resultant badness */
1103 static int try_assign_dacs(struct hda_codec *codec, int num_outs,
1104 const hda_nid_t *pins, hda_nid_t *dacs,
1105 int *path_idx,
1106 const struct badness_table *bad)
1108 struct hda_gen_spec *spec = codec->spec;
1109 int i, j;
1110 int badness = 0;
1111 hda_nid_t dac;
1113 if (!num_outs)
1114 return 0;
1116 for (i = 0; i < num_outs; i++) {
1117 struct nid_path *path;
1118 hda_nid_t pin = pins[i];
1120 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1121 if (path) {
1122 badness += assign_out_path_ctls(codec, path);
1123 continue;
1126 dacs[i] = look_for_dac(codec, pin, false);
1127 if (!dacs[i] && !i) {
1128 /* try to steal the DAC of surrounds for the front */
1129 for (j = 1; j < num_outs; j++) {
1130 if (is_reachable_path(codec, dacs[j], pin)) {
1131 dacs[0] = dacs[j];
1132 dacs[j] = 0;
1133 invalidate_nid_path(codec, path_idx[j]);
1134 path_idx[j] = 0;
1135 break;
1139 dac = dacs[i];
1140 if (!dac) {
1141 if (num_outs > 2)
1142 dac = try_dac(codec, get_primary_out(codec, i), pin);
1143 if (!dac)
1144 dac = try_dac(codec, dacs[0], pin);
1145 if (!dac)
1146 dac = try_dac(codec, get_primary_out(codec, i), pin);
1147 if (dac) {
1148 if (!i)
1149 badness += bad->shared_primary;
1150 else if (i == 1)
1151 badness += bad->shared_surr;
1152 else
1153 badness += bad->shared_clfe;
1154 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
1155 dac = spec->private_dac_nids[0];
1156 badness += bad->shared_surr_main;
1157 } else if (!i)
1158 badness += bad->no_primary_dac;
1159 else
1160 badness += bad->no_dac;
1162 if (!dac)
1163 continue;
1164 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
1165 if (!path && !i && spec->mixer_nid) {
1166 /* try with aamix */
1167 path = snd_hda_add_new_path(codec, dac, pin, 0);
1169 if (!path) {
1170 dac = dacs[i] = 0;
1171 badness += bad->no_dac;
1172 } else {
1173 /* print_nid_path("output", path); */
1174 path->active = true;
1175 path_idx[i] = snd_hda_get_path_idx(codec, path);
1176 badness += assign_out_path_ctls(codec, path);
1180 return badness;
1183 /* return NID if the given pin has only a single connection to a certain DAC */
1184 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
1186 struct hda_gen_spec *spec = codec->spec;
1187 int i;
1188 hda_nid_t nid_found = 0;
1190 for (i = 0; i < spec->num_all_dacs; i++) {
1191 hda_nid_t nid = spec->all_dacs[i];
1192 if (!nid || is_dac_already_used(codec, nid))
1193 continue;
1194 if (is_reachable_path(codec, nid, pin)) {
1195 if (nid_found)
1196 return 0;
1197 nid_found = nid;
1200 return nid_found;
1203 /* check whether the given pin can be a multi-io pin */
1204 static bool can_be_multiio_pin(struct hda_codec *codec,
1205 unsigned int location, hda_nid_t nid)
1207 unsigned int defcfg, caps;
1209 defcfg = snd_hda_codec_get_pincfg(codec, nid);
1210 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
1211 return false;
1212 if (location && get_defcfg_location(defcfg) != location)
1213 return false;
1214 caps = snd_hda_query_pin_caps(codec, nid);
1215 if (!(caps & AC_PINCAP_OUT))
1216 return false;
1217 return true;
1220 /* count the number of input pins that are capable to be multi-io */
1221 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
1223 struct hda_gen_spec *spec = codec->spec;
1224 struct auto_pin_cfg *cfg = &spec->autocfg;
1225 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1226 unsigned int location = get_defcfg_location(defcfg);
1227 int type, i;
1228 int num_pins = 0;
1230 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1231 for (i = 0; i < cfg->num_inputs; i++) {
1232 if (cfg->inputs[i].type != type)
1233 continue;
1234 if (can_be_multiio_pin(codec, location,
1235 cfg->inputs[i].pin))
1236 num_pins++;
1239 return num_pins;
1243 * multi-io helper
1245 * When hardwired is set, try to fill ony hardwired pins, and returns
1246 * zero if any pins are filled, non-zero if nothing found.
1247 * When hardwired is off, try to fill possible input pins, and returns
1248 * the badness value.
1250 static int fill_multi_ios(struct hda_codec *codec,
1251 hda_nid_t reference_pin,
1252 bool hardwired)
1254 struct hda_gen_spec *spec = codec->spec;
1255 struct auto_pin_cfg *cfg = &spec->autocfg;
1256 int type, i, j, num_pins, old_pins;
1257 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
1258 unsigned int location = get_defcfg_location(defcfg);
1259 int badness = 0;
1260 struct nid_path *path;
1262 old_pins = spec->multi_ios;
1263 if (old_pins >= 2)
1264 goto end_fill;
1266 num_pins = count_multiio_pins(codec, reference_pin);
1267 if (num_pins < 2)
1268 goto end_fill;
1270 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
1271 for (i = 0; i < cfg->num_inputs; i++) {
1272 hda_nid_t nid = cfg->inputs[i].pin;
1273 hda_nid_t dac = 0;
1275 if (cfg->inputs[i].type != type)
1276 continue;
1277 if (!can_be_multiio_pin(codec, location, nid))
1278 continue;
1279 for (j = 0; j < spec->multi_ios; j++) {
1280 if (nid == spec->multi_io[j].pin)
1281 break;
1283 if (j < spec->multi_ios)
1284 continue;
1286 if (hardwired)
1287 dac = get_dac_if_single(codec, nid);
1288 else if (!dac)
1289 dac = look_for_dac(codec, nid, false);
1290 if (!dac) {
1291 badness++;
1292 continue;
1294 path = snd_hda_add_new_path(codec, dac, nid,
1295 -spec->mixer_nid);
1296 if (!path) {
1297 badness++;
1298 continue;
1300 /* print_nid_path("multiio", path); */
1301 spec->multi_io[spec->multi_ios].pin = nid;
1302 spec->multi_io[spec->multi_ios].dac = dac;
1303 spec->out_paths[cfg->line_outs + spec->multi_ios] =
1304 snd_hda_get_path_idx(codec, path);
1305 spec->multi_ios++;
1306 if (spec->multi_ios >= 2)
1307 break;
1310 end_fill:
1311 if (badness)
1312 badness = BAD_MULTI_IO;
1313 if (old_pins == spec->multi_ios) {
1314 if (hardwired)
1315 return 1; /* nothing found */
1316 else
1317 return badness; /* no badness if nothing found */
1319 if (!hardwired && spec->multi_ios < 2) {
1320 /* cancel newly assigned paths */
1321 spec->paths.used -= spec->multi_ios - old_pins;
1322 spec->multi_ios = old_pins;
1323 return badness;
1326 /* assign volume and mute controls */
1327 for (i = old_pins; i < spec->multi_ios; i++) {
1328 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
1329 badness += assign_out_path_ctls(codec, path);
1332 return badness;
1335 /* map DACs for all pins in the list if they are single connections */
1336 static bool map_singles(struct hda_codec *codec, int outs,
1337 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
1339 struct hda_gen_spec *spec = codec->spec;
1340 int i;
1341 bool found = false;
1342 for (i = 0; i < outs; i++) {
1343 struct nid_path *path;
1344 hda_nid_t dac;
1345 if (dacs[i])
1346 continue;
1347 dac = get_dac_if_single(codec, pins[i]);
1348 if (!dac)
1349 continue;
1350 path = snd_hda_add_new_path(codec, dac, pins[i],
1351 -spec->mixer_nid);
1352 if (!path && !i && spec->mixer_nid)
1353 path = snd_hda_add_new_path(codec, dac, pins[i], 0);
1354 if (path) {
1355 dacs[i] = dac;
1356 found = true;
1357 /* print_nid_path("output", path); */
1358 path->active = true;
1359 path_idx[i] = snd_hda_get_path_idx(codec, path);
1362 return found;
1365 /* create a new path including aamix if available, and return its index */
1366 static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
1368 struct hda_gen_spec *spec = codec->spec;
1369 struct nid_path *path;
1370 hda_nid_t dac, pin;
1372 path = snd_hda_get_path_from_idx(codec, path_idx);
1373 if (!path || !path->depth ||
1374 is_nid_contained(path, spec->mixer_nid))
1375 return 0;
1376 dac = path->path[0];
1377 pin = path->path[path->depth - 1];
1378 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
1379 if (!path) {
1380 if (dac != spec->multiout.dac_nids[0])
1381 dac = spec->multiout.dac_nids[0];
1382 else if (spec->multiout.hp_out_nid[0])
1383 dac = spec->multiout.hp_out_nid[0];
1384 else if (spec->multiout.extra_out_nid[0])
1385 dac = spec->multiout.extra_out_nid[0];
1386 if (dac)
1387 path = snd_hda_add_new_path(codec, dac, pin,
1388 spec->mixer_nid);
1390 if (!path)
1391 return 0;
1392 /* print_nid_path("output-aamix", path); */
1393 path->active = false; /* unused as default */
1394 return snd_hda_get_path_idx(codec, path);
1397 /* check whether the independent HP is available with the current config */
1398 static bool indep_hp_possible(struct hda_codec *codec)
1400 struct hda_gen_spec *spec = codec->spec;
1401 struct auto_pin_cfg *cfg = &spec->autocfg;
1402 struct nid_path *path;
1403 int i, idx;
1405 if (cfg->line_out_type == AUTO_PIN_HP_OUT)
1406 idx = spec->out_paths[0];
1407 else
1408 idx = spec->hp_paths[0];
1409 path = snd_hda_get_path_from_idx(codec, idx);
1410 if (!path)
1411 return false;
1413 /* assume no path conflicts unless aamix is involved */
1414 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid))
1415 return true;
1417 /* check whether output paths contain aamix */
1418 for (i = 0; i < cfg->line_outs; i++) {
1419 if (spec->out_paths[i] == idx)
1420 break;
1421 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1422 if (path && is_nid_contained(path, spec->mixer_nid))
1423 return false;
1425 for (i = 0; i < cfg->speaker_outs; i++) {
1426 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]);
1427 if (path && is_nid_contained(path, spec->mixer_nid))
1428 return false;
1431 return true;
1434 /* fill the empty entries in the dac array for speaker/hp with the
1435 * shared dac pointed by the paths
1437 static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
1438 hda_nid_t *dacs, int *path_idx)
1440 struct nid_path *path;
1441 int i;
1443 for (i = 0; i < num_outs; i++) {
1444 if (dacs[i])
1445 continue;
1446 path = snd_hda_get_path_from_idx(codec, path_idx[i]);
1447 if (!path)
1448 continue;
1449 dacs[i] = path->path[0];
1453 /* fill in the dac_nids table from the parsed pin configuration */
1454 static int fill_and_eval_dacs(struct hda_codec *codec,
1455 bool fill_hardwired,
1456 bool fill_mio_first)
1458 struct hda_gen_spec *spec = codec->spec;
1459 struct auto_pin_cfg *cfg = &spec->autocfg;
1460 int i, err, badness;
1462 /* set num_dacs once to full for look_for_dac() */
1463 spec->multiout.num_dacs = cfg->line_outs;
1464 spec->multiout.dac_nids = spec->private_dac_nids;
1465 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
1466 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
1467 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
1468 spec->multi_ios = 0;
1469 snd_array_free(&spec->paths);
1471 /* clear path indices */
1472 memset(spec->out_paths, 0, sizeof(spec->out_paths));
1473 memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
1474 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
1475 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
1476 memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
1477 memset(spec->input_paths, 0, sizeof(spec->input_paths));
1478 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
1479 memset(&spec->digin_path, 0, sizeof(spec->digin_path));
1481 badness = 0;
1483 /* fill hard-wired DACs first */
1484 if (fill_hardwired) {
1485 bool mapped;
1486 do {
1487 mapped = map_singles(codec, cfg->line_outs,
1488 cfg->line_out_pins,
1489 spec->private_dac_nids,
1490 spec->out_paths);
1491 mapped |= map_singles(codec, cfg->hp_outs,
1492 cfg->hp_pins,
1493 spec->multiout.hp_out_nid,
1494 spec->hp_paths);
1495 mapped |= map_singles(codec, cfg->speaker_outs,
1496 cfg->speaker_pins,
1497 spec->multiout.extra_out_nid,
1498 spec->speaker_paths);
1499 if (fill_mio_first && cfg->line_outs == 1 &&
1500 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1501 err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
1502 if (!err)
1503 mapped = true;
1505 } while (mapped);
1508 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
1509 spec->private_dac_nids, spec->out_paths,
1510 &main_out_badness);
1512 if (fill_mio_first &&
1513 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1514 /* try to fill multi-io first */
1515 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1516 if (err < 0)
1517 return err;
1518 /* we don't count badness at this stage yet */
1521 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
1522 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
1523 spec->multiout.hp_out_nid,
1524 spec->hp_paths,
1525 &extra_out_badness);
1526 if (err < 0)
1527 return err;
1528 badness += err;
1530 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1531 err = try_assign_dacs(codec, cfg->speaker_outs,
1532 cfg->speaker_pins,
1533 spec->multiout.extra_out_nid,
1534 spec->speaker_paths,
1535 &extra_out_badness);
1536 if (err < 0)
1537 return err;
1538 badness += err;
1540 if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1541 err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
1542 if (err < 0)
1543 return err;
1544 badness += err;
1547 if (spec->mixer_nid) {
1548 spec->aamix_out_paths[0] =
1549 check_aamix_out_path(codec, spec->out_paths[0]);
1550 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1551 spec->aamix_out_paths[1] =
1552 check_aamix_out_path(codec, spec->hp_paths[0]);
1553 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1554 spec->aamix_out_paths[2] =
1555 check_aamix_out_path(codec, spec->speaker_paths[0]);
1558 if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
1559 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
1560 spec->multi_ios = 1; /* give badness */
1562 /* re-count num_dacs and squash invalid entries */
1563 spec->multiout.num_dacs = 0;
1564 for (i = 0; i < cfg->line_outs; i++) {
1565 if (spec->private_dac_nids[i])
1566 spec->multiout.num_dacs++;
1567 else {
1568 memmove(spec->private_dac_nids + i,
1569 spec->private_dac_nids + i + 1,
1570 sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
1571 spec->private_dac_nids[cfg->line_outs - 1] = 0;
1575 spec->ext_channel_count = spec->min_channel_count =
1576 spec->multiout.num_dacs * 2;
1578 if (spec->multi_ios == 2) {
1579 for (i = 0; i < 2; i++)
1580 spec->private_dac_nids[spec->multiout.num_dacs++] =
1581 spec->multi_io[i].dac;
1582 } else if (spec->multi_ios) {
1583 spec->multi_ios = 0;
1584 badness += BAD_MULTI_IO;
1587 if (spec->indep_hp && !indep_hp_possible(codec))
1588 badness += BAD_NO_INDEP_HP;
1590 /* re-fill the shared DAC for speaker / headphone */
1591 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1592 refill_shared_dacs(codec, cfg->hp_outs,
1593 spec->multiout.hp_out_nid,
1594 spec->hp_paths);
1595 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
1596 refill_shared_dacs(codec, cfg->speaker_outs,
1597 spec->multiout.extra_out_nid,
1598 spec->speaker_paths);
1600 return badness;
1603 #define DEBUG_BADNESS
1605 #ifdef DEBUG_BADNESS
1606 #define debug_badness snd_printdd
1607 #else
1608 #define debug_badness(...)
1609 #endif
1611 #ifdef DEBUG_BADNESS
1612 static inline void print_nid_path_idx(struct hda_codec *codec,
1613 const char *pfx, int idx)
1615 struct nid_path *path;
1617 path = snd_hda_get_path_from_idx(codec, idx);
1618 if (path)
1619 print_nid_path(pfx, path);
1622 static void debug_show_configs(struct hda_codec *codec,
1623 struct auto_pin_cfg *cfg)
1625 struct hda_gen_spec *spec = codec->spec;
1626 static const char * const lo_type[3] = { "LO", "SP", "HP" };
1627 int i;
1629 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1630 cfg->line_out_pins[0], cfg->line_out_pins[1],
1631 cfg->line_out_pins[2], cfg->line_out_pins[3],
1632 spec->multiout.dac_nids[0],
1633 spec->multiout.dac_nids[1],
1634 spec->multiout.dac_nids[2],
1635 spec->multiout.dac_nids[3],
1636 lo_type[cfg->line_out_type]);
1637 for (i = 0; i < cfg->line_outs; i++)
1638 print_nid_path_idx(codec, " out", spec->out_paths[i]);
1639 if (spec->multi_ios > 0)
1640 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1641 spec->multi_ios,
1642 spec->multi_io[0].pin, spec->multi_io[1].pin,
1643 spec->multi_io[0].dac, spec->multi_io[1].dac);
1644 for (i = 0; i < spec->multi_ios; i++)
1645 print_nid_path_idx(codec, " mio",
1646 spec->out_paths[cfg->line_outs + i]);
1647 if (cfg->hp_outs)
1648 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1649 cfg->hp_pins[0], cfg->hp_pins[1],
1650 cfg->hp_pins[2], cfg->hp_pins[3],
1651 spec->multiout.hp_out_nid[0],
1652 spec->multiout.hp_out_nid[1],
1653 spec->multiout.hp_out_nid[2],
1654 spec->multiout.hp_out_nid[3]);
1655 for (i = 0; i < cfg->hp_outs; i++)
1656 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
1657 if (cfg->speaker_outs)
1658 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1659 cfg->speaker_pins[0], cfg->speaker_pins[1],
1660 cfg->speaker_pins[2], cfg->speaker_pins[3],
1661 spec->multiout.extra_out_nid[0],
1662 spec->multiout.extra_out_nid[1],
1663 spec->multiout.extra_out_nid[2],
1664 spec->multiout.extra_out_nid[3]);
1665 for (i = 0; i < cfg->speaker_outs; i++)
1666 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
1667 for (i = 0; i < 3; i++)
1668 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
1670 #else
1671 #define debug_show_configs(codec, cfg) /* NOP */
1672 #endif
1674 /* find all available DACs of the codec */
1675 static void fill_all_dac_nids(struct hda_codec *codec)
1677 struct hda_gen_spec *spec = codec->spec;
1678 int i;
1679 hda_nid_t nid = codec->start_nid;
1681 spec->num_all_dacs = 0;
1682 memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
1683 for (i = 0; i < codec->num_nodes; i++, nid++) {
1684 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
1685 continue;
1686 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
1687 snd_printk(KERN_ERR "hda: Too many DACs!\n");
1688 break;
1690 spec->all_dacs[spec->num_all_dacs++] = nid;
1694 static int parse_output_paths(struct hda_codec *codec)
1696 struct hda_gen_spec *spec = codec->spec;
1697 struct auto_pin_cfg *cfg = &spec->autocfg;
1698 struct auto_pin_cfg *best_cfg;
1699 unsigned int val;
1700 int best_badness = INT_MAX;
1701 int badness;
1702 bool fill_hardwired = true, fill_mio_first = true;
1703 bool best_wired = true, best_mio = true;
1704 bool hp_spk_swapped = false;
1706 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
1707 if (!best_cfg)
1708 return -ENOMEM;
1709 *best_cfg = *cfg;
1711 for (;;) {
1712 badness = fill_and_eval_dacs(codec, fill_hardwired,
1713 fill_mio_first);
1714 if (badness < 0) {
1715 kfree(best_cfg);
1716 return badness;
1718 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1719 cfg->line_out_type, fill_hardwired, fill_mio_first,
1720 badness);
1721 debug_show_configs(codec, cfg);
1722 if (badness < best_badness) {
1723 best_badness = badness;
1724 *best_cfg = *cfg;
1725 best_wired = fill_hardwired;
1726 best_mio = fill_mio_first;
1728 if (!badness)
1729 break;
1730 fill_mio_first = !fill_mio_first;
1731 if (!fill_mio_first)
1732 continue;
1733 fill_hardwired = !fill_hardwired;
1734 if (!fill_hardwired)
1735 continue;
1736 if (hp_spk_swapped)
1737 break;
1738 hp_spk_swapped = true;
1739 if (cfg->speaker_outs > 0 &&
1740 cfg->line_out_type == AUTO_PIN_HP_OUT) {
1741 cfg->hp_outs = cfg->line_outs;
1742 memcpy(cfg->hp_pins, cfg->line_out_pins,
1743 sizeof(cfg->hp_pins));
1744 cfg->line_outs = cfg->speaker_outs;
1745 memcpy(cfg->line_out_pins, cfg->speaker_pins,
1746 sizeof(cfg->speaker_pins));
1747 cfg->speaker_outs = 0;
1748 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
1749 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
1750 fill_hardwired = true;
1751 continue;
1753 if (cfg->hp_outs > 0 &&
1754 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
1755 cfg->speaker_outs = cfg->line_outs;
1756 memcpy(cfg->speaker_pins, cfg->line_out_pins,
1757 sizeof(cfg->speaker_pins));
1758 cfg->line_outs = cfg->hp_outs;
1759 memcpy(cfg->line_out_pins, cfg->hp_pins,
1760 sizeof(cfg->hp_pins));
1761 cfg->hp_outs = 0;
1762 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
1763 cfg->line_out_type = AUTO_PIN_HP_OUT;
1764 fill_hardwired = true;
1765 continue;
1767 break;
1770 if (badness) {
1771 debug_badness("==> restoring best_cfg\n");
1772 *cfg = *best_cfg;
1773 fill_and_eval_dacs(codec, best_wired, best_mio);
1775 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1776 cfg->line_out_type, best_wired, best_mio);
1777 debug_show_configs(codec, cfg);
1779 if (cfg->line_out_pins[0]) {
1780 struct nid_path *path;
1781 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
1782 if (path)
1783 spec->vmaster_nid = look_for_out_vol_nid(codec, path);
1784 if (spec->vmaster_nid)
1785 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
1786 HDA_OUTPUT, spec->vmaster_tlv);
1789 /* set initial pinctl targets */
1790 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
1791 val = PIN_HP;
1792 else
1793 val = PIN_OUT;
1794 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
1795 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
1796 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
1797 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
1798 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
1799 set_pin_targets(codec, cfg->speaker_outs,
1800 cfg->speaker_pins, val);
1803 /* clear indep_hp flag if not available */
1804 if (spec->indep_hp && !indep_hp_possible(codec))
1805 spec->indep_hp = 0;
1807 kfree(best_cfg);
1808 return 0;
1811 /* add playback controls from the parsed DAC table */
1812 static int create_multi_out_ctls(struct hda_codec *codec,
1813 const struct auto_pin_cfg *cfg)
1815 struct hda_gen_spec *spec = codec->spec;
1816 int i, err, noutputs;
1818 noutputs = cfg->line_outs;
1819 if (spec->multi_ios > 0 && cfg->line_outs < 3)
1820 noutputs += spec->multi_ios;
1822 for (i = 0; i < noutputs; i++) {
1823 const char *name;
1824 int index;
1825 struct nid_path *path;
1827 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
1828 if (!path)
1829 continue;
1831 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
1832 if (!name || !strcmp(name, "CLFE")) {
1833 /* Center/LFE */
1834 err = add_vol_ctl(codec, "Center", 0, 1, path);
1835 if (err < 0)
1836 return err;
1837 err = add_vol_ctl(codec, "LFE", 0, 2, path);
1838 if (err < 0)
1839 return err;
1840 } else {
1841 err = add_stereo_vol(codec, name, index, path);
1842 if (err < 0)
1843 return err;
1846 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
1847 if (!name || !strcmp(name, "CLFE")) {
1848 err = add_sw_ctl(codec, "Center", 0, 1, path);
1849 if (err < 0)
1850 return err;
1851 err = add_sw_ctl(codec, "LFE", 0, 2, path);
1852 if (err < 0)
1853 return err;
1854 } else {
1855 err = add_stereo_sw(codec, name, index, path);
1856 if (err < 0)
1857 return err;
1860 return 0;
1863 static int create_extra_out(struct hda_codec *codec, int path_idx,
1864 const char *pfx, int cidx)
1866 struct nid_path *path;
1867 int err;
1869 path = snd_hda_get_path_from_idx(codec, path_idx);
1870 if (!path)
1871 return 0;
1872 err = add_stereo_vol(codec, pfx, cidx, path);
1873 if (err < 0)
1874 return err;
1875 err = add_stereo_sw(codec, pfx, cidx, path);
1876 if (err < 0)
1877 return err;
1878 return 0;
1881 /* add playback controls for speaker and HP outputs */
1882 static int create_extra_outs(struct hda_codec *codec, int num_pins,
1883 const int *paths, const char *pfx)
1885 int i;
1887 for (i = 0; i < num_pins; i++) {
1888 const char *name;
1889 char tmp[44];
1890 int err, idx = 0;
1892 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
1893 name = "Bass Speaker";
1894 else if (num_pins >= 3) {
1895 snprintf(tmp, sizeof(tmp), "%s %s",
1896 pfx, channel_name[i]);
1897 name = tmp;
1898 } else {
1899 name = pfx;
1900 idx = i;
1902 err = create_extra_out(codec, paths[i], name, idx);
1903 if (err < 0)
1904 return err;
1906 return 0;
1909 static int create_hp_out_ctls(struct hda_codec *codec)
1911 struct hda_gen_spec *spec = codec->spec;
1912 return create_extra_outs(codec, spec->autocfg.hp_outs,
1913 spec->hp_paths,
1914 "Headphone");
1917 static int create_speaker_out_ctls(struct hda_codec *codec)
1919 struct hda_gen_spec *spec = codec->spec;
1920 return create_extra_outs(codec, spec->autocfg.speaker_outs,
1921 spec->speaker_paths,
1922 "Speaker");
1926 * independent HP controls
1929 static int indep_hp_info(struct snd_kcontrol *kcontrol,
1930 struct snd_ctl_elem_info *uinfo)
1932 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
1935 static int indep_hp_get(struct snd_kcontrol *kcontrol,
1936 struct snd_ctl_elem_value *ucontrol)
1938 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1939 struct hda_gen_spec *spec = codec->spec;
1940 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
1941 return 0;
1944 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
1945 int nomix_path_idx, int mix_path_idx,
1946 int out_type);
1948 static int indep_hp_put(struct snd_kcontrol *kcontrol,
1949 struct snd_ctl_elem_value *ucontrol)
1951 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1952 struct hda_gen_spec *spec = codec->spec;
1953 unsigned int select = ucontrol->value.enumerated.item[0];
1954 int ret = 0;
1956 mutex_lock(&spec->pcm_mutex);
1957 if (spec->active_streams) {
1958 ret = -EBUSY;
1959 goto unlock;
1962 if (spec->indep_hp_enabled != select) {
1963 hda_nid_t *dacp;
1964 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1965 dacp = &spec->private_dac_nids[0];
1966 else
1967 dacp = &spec->multiout.hp_out_nid[0];
1969 /* update HP aamix paths in case it conflicts with indep HP */
1970 if (spec->have_aamix_ctl) {
1971 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
1972 update_aamix_paths(codec, spec->aamix_mode,
1973 spec->out_paths[0],
1974 spec->aamix_out_paths[0],
1975 spec->autocfg.line_out_type);
1976 else
1977 update_aamix_paths(codec, spec->aamix_mode,
1978 spec->hp_paths[0],
1979 spec->aamix_out_paths[1],
1980 AUTO_PIN_HP_OUT);
1983 spec->indep_hp_enabled = select;
1984 if (spec->indep_hp_enabled)
1985 *dacp = 0;
1986 else
1987 *dacp = spec->alt_dac_nid;
1989 /* update HP auto-mute state too */
1990 if (spec->hp_automute_hook)
1991 spec->hp_automute_hook(codec, NULL);
1992 else
1993 snd_hda_gen_hp_automute(codec, NULL);
1995 ret = 1;
1997 unlock:
1998 mutex_unlock(&spec->pcm_mutex);
1999 return ret;
2002 static const struct snd_kcontrol_new indep_hp_ctl = {
2003 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2004 .name = "Independent HP",
2005 .info = indep_hp_info,
2006 .get = indep_hp_get,
2007 .put = indep_hp_put,
2011 static int create_indep_hp_ctls(struct hda_codec *codec)
2013 struct hda_gen_spec *spec = codec->spec;
2014 hda_nid_t dac;
2016 if (!spec->indep_hp)
2017 return 0;
2018 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
2019 dac = spec->multiout.dac_nids[0];
2020 else
2021 dac = spec->multiout.hp_out_nid[0];
2022 if (!dac) {
2023 spec->indep_hp = 0;
2024 return 0;
2027 spec->indep_hp_enabled = false;
2028 spec->alt_dac_nid = dac;
2029 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
2030 return -ENOMEM;
2031 return 0;
2035 * channel mode enum control
2038 static int ch_mode_info(struct snd_kcontrol *kcontrol,
2039 struct snd_ctl_elem_info *uinfo)
2041 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2042 struct hda_gen_spec *spec = codec->spec;
2043 int chs;
2045 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2046 uinfo->count = 1;
2047 uinfo->value.enumerated.items = spec->multi_ios + 1;
2048 if (uinfo->value.enumerated.item > spec->multi_ios)
2049 uinfo->value.enumerated.item = spec->multi_ios;
2050 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
2051 sprintf(uinfo->value.enumerated.name, "%dch", chs);
2052 return 0;
2055 static int ch_mode_get(struct snd_kcontrol *kcontrol,
2056 struct snd_ctl_elem_value *ucontrol)
2058 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2059 struct hda_gen_spec *spec = codec->spec;
2060 ucontrol->value.enumerated.item[0] =
2061 (spec->ext_channel_count - spec->min_channel_count) / 2;
2062 return 0;
2065 static inline struct nid_path *
2066 get_multiio_path(struct hda_codec *codec, int idx)
2068 struct hda_gen_spec *spec = codec->spec;
2069 return snd_hda_get_path_from_idx(codec,
2070 spec->out_paths[spec->autocfg.line_outs + idx]);
2073 static void update_automute_all(struct hda_codec *codec);
2075 static int set_multi_io(struct hda_codec *codec, int idx, bool output)
2077 struct hda_gen_spec *spec = codec->spec;
2078 hda_nid_t nid = spec->multi_io[idx].pin;
2079 struct nid_path *path;
2081 path = get_multiio_path(codec, idx);
2082 if (!path)
2083 return -EINVAL;
2085 if (path->active == output)
2086 return 0;
2088 if (output) {
2089 set_pin_target(codec, nid, PIN_OUT, true);
2090 snd_hda_activate_path(codec, path, true, true);
2091 set_pin_eapd(codec, nid, true);
2092 } else {
2093 set_pin_eapd(codec, nid, false);
2094 snd_hda_activate_path(codec, path, false, true);
2095 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
2096 path_power_down_sync(codec, path);
2099 /* update jack retasking in case it modifies any of them */
2100 update_automute_all(codec);
2102 return 0;
2105 static int ch_mode_put(struct snd_kcontrol *kcontrol,
2106 struct snd_ctl_elem_value *ucontrol)
2108 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2109 struct hda_gen_spec *spec = codec->spec;
2110 int i, ch;
2112 ch = ucontrol->value.enumerated.item[0];
2113 if (ch < 0 || ch > spec->multi_ios)
2114 return -EINVAL;
2115 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
2116 return 0;
2117 spec->ext_channel_count = ch * 2 + spec->min_channel_count;
2118 for (i = 0; i < spec->multi_ios; i++)
2119 set_multi_io(codec, i, i < ch);
2120 spec->multiout.max_channels = max(spec->ext_channel_count,
2121 spec->const_channel_count);
2122 if (spec->need_dac_fix)
2123 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
2124 return 1;
2127 static const struct snd_kcontrol_new channel_mode_enum = {
2128 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2129 .name = "Channel Mode",
2130 .info = ch_mode_info,
2131 .get = ch_mode_get,
2132 .put = ch_mode_put,
2135 static int create_multi_channel_mode(struct hda_codec *codec)
2137 struct hda_gen_spec *spec = codec->spec;
2139 if (spec->multi_ios > 0) {
2140 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
2141 return -ENOMEM;
2143 return 0;
2147 * aamix loopback enable/disable switch
2150 #define loopback_mixing_info indep_hp_info
2152 static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
2153 struct snd_ctl_elem_value *ucontrol)
2155 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2156 struct hda_gen_spec *spec = codec->spec;
2157 ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2158 return 0;
2161 static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
2162 int nomix_path_idx, int mix_path_idx,
2163 int out_type)
2165 struct hda_gen_spec *spec = codec->spec;
2166 struct nid_path *nomix_path, *mix_path;
2168 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
2169 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
2170 if (!nomix_path || !mix_path)
2171 return;
2173 /* if HP aamix path is driven from a different DAC and the
2174 * independent HP mode is ON, can't turn on aamix path
2176 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
2177 mix_path->path[0] != spec->alt_dac_nid)
2178 do_mix = false;
2180 if (do_mix) {
2181 snd_hda_activate_path(codec, nomix_path, false, true);
2182 snd_hda_activate_path(codec, mix_path, true, true);
2183 path_power_down_sync(codec, nomix_path);
2184 } else {
2185 snd_hda_activate_path(codec, mix_path, false, true);
2186 snd_hda_activate_path(codec, nomix_path, true, true);
2187 path_power_down_sync(codec, mix_path);
2191 static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
2192 struct snd_ctl_elem_value *ucontrol)
2194 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2195 struct hda_gen_spec *spec = codec->spec;
2196 unsigned int val = ucontrol->value.enumerated.item[0];
2198 if (val == spec->aamix_mode)
2199 return 0;
2200 spec->aamix_mode = val;
2201 update_aamix_paths(codec, val, spec->out_paths[0],
2202 spec->aamix_out_paths[0],
2203 spec->autocfg.line_out_type);
2204 update_aamix_paths(codec, val, spec->hp_paths[0],
2205 spec->aamix_out_paths[1],
2206 AUTO_PIN_HP_OUT);
2207 update_aamix_paths(codec, val, spec->speaker_paths[0],
2208 spec->aamix_out_paths[2],
2209 AUTO_PIN_SPEAKER_OUT);
2210 return 1;
2213 static const struct snd_kcontrol_new loopback_mixing_enum = {
2214 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2215 .name = "Loopback Mixing",
2216 .info = loopback_mixing_info,
2217 .get = loopback_mixing_get,
2218 .put = loopback_mixing_put,
2221 static int create_loopback_mixing_ctl(struct hda_codec *codec)
2223 struct hda_gen_spec *spec = codec->spec;
2225 if (!spec->mixer_nid)
2226 return 0;
2227 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
2228 spec->aamix_out_paths[2]))
2229 return 0;
2230 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
2231 return -ENOMEM;
2232 spec->have_aamix_ctl = 1;
2233 return 0;
2237 * shared headphone/mic handling
2240 static void call_update_outputs(struct hda_codec *codec);
2242 /* for shared I/O, change the pin-control accordingly */
2243 static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
2245 struct hda_gen_spec *spec = codec->spec;
2246 unsigned int val;
2247 hda_nid_t pin = spec->autocfg.inputs[1].pin;
2248 /* NOTE: this assumes that there are only two inputs, the
2249 * first is the real internal mic and the second is HP/mic jack.
2252 val = snd_hda_get_default_vref(codec, pin);
2254 /* This pin does not have vref caps - let's enable vref on pin 0x18
2255 instead, as suggested by Realtek */
2256 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
2257 const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
2258 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
2259 if (vref_val != AC_PINCTL_VREF_HIZ)
2260 snd_hda_set_pin_ctl_cache(codec, vref_pin,
2261 PIN_IN | (set_as_mic ? vref_val : 0));
2264 val = set_as_mic ? val | PIN_IN : PIN_HP;
2265 set_pin_target(codec, pin, val, true);
2267 spec->automute_speaker = !set_as_mic;
2268 call_update_outputs(codec);
2271 /* create a shared input with the headphone out */
2272 static int create_shared_input(struct hda_codec *codec)
2274 struct hda_gen_spec *spec = codec->spec;
2275 struct auto_pin_cfg *cfg = &spec->autocfg;
2276 unsigned int defcfg;
2277 hda_nid_t nid;
2279 /* only one internal input pin? */
2280 if (cfg->num_inputs != 1)
2281 return 0;
2282 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
2283 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
2284 return 0;
2286 if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
2287 nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
2288 else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
2289 nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
2290 else
2291 return 0; /* both not available */
2293 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
2294 return 0; /* no input */
2296 cfg->inputs[1].pin = nid;
2297 cfg->inputs[1].type = AUTO_PIN_MIC;
2298 cfg->num_inputs = 2;
2299 spec->shared_mic_hp = 1;
2300 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
2301 return 0;
2305 * output jack mode
2307 static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
2308 struct snd_ctl_elem_info *uinfo)
2310 static const char * const texts[] = {
2311 "Line Out", "Headphone Out",
2313 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
2316 static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
2317 struct snd_ctl_elem_value *ucontrol)
2319 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2320 hda_nid_t nid = kcontrol->private_value;
2321 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
2322 ucontrol->value.enumerated.item[0] = 1;
2323 else
2324 ucontrol->value.enumerated.item[0] = 0;
2325 return 0;
2328 static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
2329 struct snd_ctl_elem_value *ucontrol)
2331 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2332 hda_nid_t nid = kcontrol->private_value;
2333 unsigned int val;
2335 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
2336 if (snd_hda_codec_get_pin_target(codec, nid) == val)
2337 return 0;
2338 snd_hda_set_pin_ctl_cache(codec, nid, val);
2339 return 1;
2342 static const struct snd_kcontrol_new out_jack_mode_enum = {
2343 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2344 .info = out_jack_mode_info,
2345 .get = out_jack_mode_get,
2346 .put = out_jack_mode_put,
2349 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
2351 struct hda_gen_spec *spec = codec->spec;
2352 int i;
2354 for (i = 0; i < spec->kctls.used; i++) {
2355 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
2356 if (!strcmp(kctl->name, name) && kctl->index == idx)
2357 return true;
2359 return false;
2362 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
2363 char *name, size_t name_len)
2365 struct hda_gen_spec *spec = codec->spec;
2366 int idx = 0;
2368 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
2369 strlcat(name, " Jack Mode", name_len);
2371 for (; find_kctl_name(codec, name, idx); idx++)
2375 static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
2376 hda_nid_t *pins)
2378 struct hda_gen_spec *spec = codec->spec;
2379 int i;
2381 for (i = 0; i < num_pins; i++) {
2382 hda_nid_t pin = pins[i];
2383 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
2384 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
2385 struct snd_kcontrol_new *knew;
2386 char name[44];
2387 get_jack_mode_name(codec, pin, name, sizeof(name));
2388 knew = snd_hda_gen_add_kctl(spec, name,
2389 &out_jack_mode_enum);
2390 if (!knew)
2391 return -ENOMEM;
2392 knew->private_value = pin;
2396 return 0;
2400 * input jack mode
2403 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2404 #define NUM_VREFS 6
2406 static const char * const vref_texts[NUM_VREFS] = {
2407 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2408 "", "Mic 80pc Bias", "Mic 100pc Bias"
2411 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
2413 unsigned int pincap;
2415 pincap = snd_hda_query_pin_caps(codec, pin);
2416 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
2417 /* filter out unusual vrefs */
2418 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
2419 return pincap;
2422 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2423 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
2425 unsigned int i, n = 0;
2427 for (i = 0; i < NUM_VREFS; i++) {
2428 if (vref_caps & (1 << i)) {
2429 if (n == item_idx)
2430 return i;
2431 n++;
2434 return 0;
2437 /* convert back from the vref ctl index to the enum item index */
2438 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
2440 unsigned int i, n = 0;
2442 for (i = 0; i < NUM_VREFS; i++) {
2443 if (i == idx)
2444 return n;
2445 if (vref_caps & (1 << i))
2446 n++;
2448 return 0;
2451 static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
2452 struct snd_ctl_elem_info *uinfo)
2454 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2455 hda_nid_t nid = kcontrol->private_value;
2456 unsigned int vref_caps = get_vref_caps(codec, nid);
2458 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
2459 vref_texts);
2460 /* set the right text */
2461 strcpy(uinfo->value.enumerated.name,
2462 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
2463 return 0;
2466 static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
2467 struct snd_ctl_elem_value *ucontrol)
2469 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2470 hda_nid_t nid = kcontrol->private_value;
2471 unsigned int vref_caps = get_vref_caps(codec, nid);
2472 unsigned int idx;
2474 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
2475 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
2476 return 0;
2479 static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
2480 struct snd_ctl_elem_value *ucontrol)
2482 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2483 hda_nid_t nid = kcontrol->private_value;
2484 unsigned int vref_caps = get_vref_caps(codec, nid);
2485 unsigned int val, idx;
2487 val = snd_hda_codec_get_pin_target(codec, nid);
2488 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
2489 if (idx == ucontrol->value.enumerated.item[0])
2490 return 0;
2492 val &= ~AC_PINCTL_VREFEN;
2493 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
2494 snd_hda_set_pin_ctl_cache(codec, nid, val);
2495 return 1;
2498 static const struct snd_kcontrol_new in_jack_mode_enum = {
2499 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2500 .info = in_jack_mode_info,
2501 .get = in_jack_mode_get,
2502 .put = in_jack_mode_put,
2505 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
2507 struct hda_gen_spec *spec = codec->spec;
2508 unsigned int defcfg;
2509 struct snd_kcontrol_new *knew;
2510 char name[44];
2512 /* no jack mode for fixed pins */
2513 defcfg = snd_hda_codec_get_pincfg(codec, pin);
2514 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
2515 return 0;
2517 /* no multiple vref caps? */
2518 if (hweight32(get_vref_caps(codec, pin)) <= 1)
2519 return 0;
2521 get_jack_mode_name(codec, pin, name, sizeof(name));
2522 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
2523 if (!knew)
2524 return -ENOMEM;
2525 knew->private_value = pin;
2526 return 0;
2531 * Parse input paths
2534 /* add the powersave loopback-list entry */
2535 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
2537 struct hda_amp_list *list;
2539 list = snd_array_new(&spec->loopback_list);
2540 if (!list)
2541 return -ENOMEM;
2542 list->nid = mix;
2543 list->dir = HDA_INPUT;
2544 list->idx = idx;
2545 spec->loopback.amplist = spec->loopback_list.list;
2546 return 0;
2549 /* create input playback/capture controls for the given pin */
2550 static int new_analog_input(struct hda_codec *codec, int input_idx,
2551 hda_nid_t pin, const char *ctlname, int ctlidx,
2552 hda_nid_t mix_nid)
2554 struct hda_gen_spec *spec = codec->spec;
2555 struct nid_path *path;
2556 unsigned int val;
2557 int err, idx;
2559 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
2560 !nid_has_mute(codec, mix_nid, HDA_INPUT))
2561 return 0; /* no need for analog loopback */
2563 path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
2564 if (!path)
2565 return -EINVAL;
2566 print_nid_path("loopback", path);
2567 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
2569 idx = path->idx[path->depth - 1];
2570 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
2571 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2572 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
2573 if (err < 0)
2574 return err;
2575 path->ctls[NID_PATH_VOL_CTL] = val;
2578 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
2579 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
2580 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
2581 if (err < 0)
2582 return err;
2583 path->ctls[NID_PATH_MUTE_CTL] = val;
2586 path->active = true;
2587 err = add_loopback_list(spec, mix_nid, idx);
2588 if (err < 0)
2589 return err;
2591 if (spec->mixer_nid != spec->mixer_merge_nid &&
2592 !spec->loopback_merge_path) {
2593 path = snd_hda_add_new_path(codec, spec->mixer_nid,
2594 spec->mixer_merge_nid, 0);
2595 if (path) {
2596 print_nid_path("loopback-merge", path);
2597 path->active = true;
2598 spec->loopback_merge_path =
2599 snd_hda_get_path_idx(codec, path);
2603 return 0;
2606 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
2608 unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
2609 return (pincap & AC_PINCAP_IN) != 0;
2612 /* Parse the codec tree and retrieve ADCs */
2613 static int fill_adc_nids(struct hda_codec *codec)
2615 struct hda_gen_spec *spec = codec->spec;
2616 hda_nid_t nid;
2617 hda_nid_t *adc_nids = spec->adc_nids;
2618 int max_nums = ARRAY_SIZE(spec->adc_nids);
2619 int i, nums = 0;
2621 nid = codec->start_nid;
2622 for (i = 0; i < codec->num_nodes; i++, nid++) {
2623 unsigned int caps = get_wcaps(codec, nid);
2624 int type = get_wcaps_type(caps);
2626 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
2627 continue;
2628 adc_nids[nums] = nid;
2629 if (++nums >= max_nums)
2630 break;
2632 spec->num_adc_nids = nums;
2634 /* copy the detected ADCs to all_adcs[] */
2635 spec->num_all_adcs = nums;
2636 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
2638 return nums;
2641 /* filter out invalid adc_nids that don't give all active input pins;
2642 * if needed, check whether dynamic ADC-switching is available
2644 static int check_dyn_adc_switch(struct hda_codec *codec)
2646 struct hda_gen_spec *spec = codec->spec;
2647 struct hda_input_mux *imux = &spec->input_mux;
2648 unsigned int ok_bits;
2649 int i, n, nums;
2651 again:
2652 nums = 0;
2653 ok_bits = 0;
2654 for (n = 0; n < spec->num_adc_nids; n++) {
2655 for (i = 0; i < imux->num_items; i++) {
2656 if (!spec->input_paths[i][n])
2657 break;
2659 if (i >= imux->num_items) {
2660 ok_bits |= (1 << n);
2661 nums++;
2665 if (!ok_bits) {
2666 if (spec->shared_mic_hp) {
2667 spec->shared_mic_hp = 0;
2668 imux->num_items = 1;
2669 goto again;
2672 /* check whether ADC-switch is possible */
2673 for (i = 0; i < imux->num_items; i++) {
2674 for (n = 0; n < spec->num_adc_nids; n++) {
2675 if (spec->input_paths[i][n]) {
2676 spec->dyn_adc_idx[i] = n;
2677 break;
2682 snd_printdd("hda-codec: enabling ADC switching\n");
2683 spec->dyn_adc_switch = 1;
2684 } else if (nums != spec->num_adc_nids) {
2685 /* shrink the invalid adcs and input paths */
2686 nums = 0;
2687 for (n = 0; n < spec->num_adc_nids; n++) {
2688 if (!(ok_bits & (1 << n)))
2689 continue;
2690 if (n != nums) {
2691 spec->adc_nids[nums] = spec->adc_nids[n];
2692 for (i = 0; i < imux->num_items; i++) {
2693 invalidate_nid_path(codec,
2694 spec->input_paths[i][nums]);
2695 spec->input_paths[i][nums] =
2696 spec->input_paths[i][n];
2699 nums++;
2701 spec->num_adc_nids = nums;
2704 if (imux->num_items == 1 || spec->shared_mic_hp) {
2705 snd_printdd("hda-codec: reducing to a single ADC\n");
2706 spec->num_adc_nids = 1; /* reduce to a single ADC */
2709 /* single index for individual volumes ctls */
2710 if (!spec->dyn_adc_switch && spec->multi_cap_vol)
2711 spec->num_adc_nids = 1;
2713 return 0;
2716 /* parse capture source paths from the given pin and create imux items */
2717 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
2718 int cfg_idx, int num_adcs,
2719 const char *label, int anchor)
2721 struct hda_gen_spec *spec = codec->spec;
2722 struct hda_input_mux *imux = &spec->input_mux;
2723 int imux_idx = imux->num_items;
2724 bool imux_added = false;
2725 int c;
2727 for (c = 0; c < num_adcs; c++) {
2728 struct nid_path *path;
2729 hda_nid_t adc = spec->adc_nids[c];
2731 if (!is_reachable_path(codec, pin, adc))
2732 continue;
2733 path = snd_hda_add_new_path(codec, pin, adc, anchor);
2734 if (!path)
2735 continue;
2736 print_nid_path("input", path);
2737 spec->input_paths[imux_idx][c] =
2738 snd_hda_get_path_idx(codec, path);
2740 if (!imux_added) {
2741 spec->imux_pins[imux->num_items] = pin;
2742 snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
2743 imux_added = true;
2747 return 0;
2751 * create playback/capture controls for input pins
2754 /* fill the label for each input at first */
2755 static int fill_input_pin_labels(struct hda_codec *codec)
2757 struct hda_gen_spec *spec = codec->spec;
2758 const struct auto_pin_cfg *cfg = &spec->autocfg;
2759 int i;
2761 for (i = 0; i < cfg->num_inputs; i++) {
2762 hda_nid_t pin = cfg->inputs[i].pin;
2763 const char *label;
2764 int j, idx;
2766 if (!is_input_pin(codec, pin))
2767 continue;
2769 label = hda_get_autocfg_input_label(codec, cfg, i);
2770 idx = 0;
2771 for (j = i - 1; j >= 0; j--) {
2772 if (spec->input_labels[j] &&
2773 !strcmp(spec->input_labels[j], label)) {
2774 idx = spec->input_label_idxs[j] + 1;
2775 break;
2779 spec->input_labels[i] = label;
2780 spec->input_label_idxs[i] = idx;
2783 return 0;
2786 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
2788 static int create_input_ctls(struct hda_codec *codec)
2790 struct hda_gen_spec *spec = codec->spec;
2791 const struct auto_pin_cfg *cfg = &spec->autocfg;
2792 hda_nid_t mixer = spec->mixer_nid;
2793 int num_adcs;
2794 int i, err;
2795 unsigned int val;
2797 num_adcs = fill_adc_nids(codec);
2798 if (num_adcs < 0)
2799 return 0;
2801 err = fill_input_pin_labels(codec);
2802 if (err < 0)
2803 return err;
2805 for (i = 0; i < cfg->num_inputs; i++) {
2806 hda_nid_t pin;
2808 pin = cfg->inputs[i].pin;
2809 if (!is_input_pin(codec, pin))
2810 continue;
2812 val = PIN_IN;
2813 if (cfg->inputs[i].type == AUTO_PIN_MIC)
2814 val |= snd_hda_get_default_vref(codec, pin);
2815 set_pin_target(codec, pin, val, false);
2817 if (mixer) {
2818 if (is_reachable_path(codec, pin, mixer)) {
2819 err = new_analog_input(codec, i, pin,
2820 spec->input_labels[i],
2821 spec->input_label_idxs[i],
2822 mixer);
2823 if (err < 0)
2824 return err;
2828 err = parse_capture_source(codec, pin, i, num_adcs,
2829 spec->input_labels[i], -mixer);
2830 if (err < 0)
2831 return err;
2833 if (spec->add_in_jack_modes) {
2834 err = create_in_jack_mode(codec, pin);
2835 if (err < 0)
2836 return err;
2840 if (mixer && spec->add_stereo_mix_input) {
2841 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
2842 "Stereo Mix", 0);
2843 if (err < 0)
2844 return err;
2847 return 0;
2852 * input source mux
2855 /* get the input path specified by the given adc and imux indices */
2856 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
2858 struct hda_gen_spec *spec = codec->spec;
2859 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
2860 snd_BUG();
2861 return NULL;
2863 if (spec->dyn_adc_switch)
2864 adc_idx = spec->dyn_adc_idx[imux_idx];
2865 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
2866 snd_BUG();
2867 return NULL;
2869 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
2872 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
2873 unsigned int idx);
2875 static int mux_enum_info(struct snd_kcontrol *kcontrol,
2876 struct snd_ctl_elem_info *uinfo)
2878 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2879 struct hda_gen_spec *spec = codec->spec;
2880 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
2883 static int mux_enum_get(struct snd_kcontrol *kcontrol,
2884 struct snd_ctl_elem_value *ucontrol)
2886 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2887 struct hda_gen_spec *spec = codec->spec;
2888 /* the ctls are created at once with multiple counts */
2889 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2891 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
2892 return 0;
2895 static int mux_enum_put(struct snd_kcontrol *kcontrol,
2896 struct snd_ctl_elem_value *ucontrol)
2898 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2899 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2900 return mux_select(codec, adc_idx,
2901 ucontrol->value.enumerated.item[0]);
2904 static const struct snd_kcontrol_new cap_src_temp = {
2905 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2906 .name = "Input Source",
2907 .info = mux_enum_info,
2908 .get = mux_enum_get,
2909 .put = mux_enum_put,
2913 * capture volume and capture switch ctls
2916 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
2917 struct snd_ctl_elem_value *ucontrol);
2919 /* call the given amp update function for all amps in the imux list at once */
2920 static int cap_put_caller(struct snd_kcontrol *kcontrol,
2921 struct snd_ctl_elem_value *ucontrol,
2922 put_call_t func, int type)
2924 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2925 struct hda_gen_spec *spec = codec->spec;
2926 const struct hda_input_mux *imux;
2927 struct nid_path *path;
2928 int i, adc_idx, err = 0;
2930 imux = &spec->input_mux;
2931 adc_idx = kcontrol->id.index;
2932 mutex_lock(&codec->control_mutex);
2933 /* we use the cache-only update at first since multiple input paths
2934 * may shared the same amp; by updating only caches, the redundant
2935 * writes to hardware can be reduced.
2937 codec->cached_write = 1;
2938 for (i = 0; i < imux->num_items; i++) {
2939 path = get_input_path(codec, adc_idx, i);
2940 if (!path || !path->ctls[type])
2941 continue;
2942 kcontrol->private_value = path->ctls[type];
2943 err = func(kcontrol, ucontrol);
2944 if (err < 0)
2945 goto error;
2947 error:
2948 codec->cached_write = 0;
2949 mutex_unlock(&codec->control_mutex);
2950 snd_hda_codec_flush_cache(codec); /* flush the updates */
2951 if (err >= 0 && spec->cap_sync_hook)
2952 spec->cap_sync_hook(codec, ucontrol);
2953 return err;
2956 /* capture volume ctl callbacks */
2957 #define cap_vol_info snd_hda_mixer_amp_volume_info
2958 #define cap_vol_get snd_hda_mixer_amp_volume_get
2959 #define cap_vol_tlv snd_hda_mixer_amp_tlv
2961 static int cap_vol_put(struct snd_kcontrol *kcontrol,
2962 struct snd_ctl_elem_value *ucontrol)
2964 return cap_put_caller(kcontrol, ucontrol,
2965 snd_hda_mixer_amp_volume_put,
2966 NID_PATH_VOL_CTL);
2969 static const struct snd_kcontrol_new cap_vol_temp = {
2970 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2971 .name = "Capture Volume",
2972 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
2973 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2974 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
2975 .info = cap_vol_info,
2976 .get = cap_vol_get,
2977 .put = cap_vol_put,
2978 .tlv = { .c = cap_vol_tlv },
2981 /* capture switch ctl callbacks */
2982 #define cap_sw_info snd_ctl_boolean_stereo_info
2983 #define cap_sw_get snd_hda_mixer_amp_switch_get
2985 static int cap_sw_put(struct snd_kcontrol *kcontrol,
2986 struct snd_ctl_elem_value *ucontrol)
2988 return cap_put_caller(kcontrol, ucontrol,
2989 snd_hda_mixer_amp_switch_put,
2990 NID_PATH_MUTE_CTL);
2993 static const struct snd_kcontrol_new cap_sw_temp = {
2994 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2995 .name = "Capture Switch",
2996 .info = cap_sw_info,
2997 .get = cap_sw_get,
2998 .put = cap_sw_put,
3001 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
3003 hda_nid_t nid;
3004 int i, depth;
3006 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
3007 for (depth = 0; depth < 3; depth++) {
3008 if (depth >= path->depth)
3009 return -EINVAL;
3010 i = path->depth - depth - 1;
3011 nid = path->path[i];
3012 if (!path->ctls[NID_PATH_VOL_CTL]) {
3013 if (nid_has_volume(codec, nid, HDA_OUTPUT))
3014 path->ctls[NID_PATH_VOL_CTL] =
3015 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3016 else if (nid_has_volume(codec, nid, HDA_INPUT)) {
3017 int idx = path->idx[i];
3018 if (!depth && codec->single_adc_amp)
3019 idx = 0;
3020 path->ctls[NID_PATH_VOL_CTL] =
3021 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3024 if (!path->ctls[NID_PATH_MUTE_CTL]) {
3025 if (nid_has_mute(codec, nid, HDA_OUTPUT))
3026 path->ctls[NID_PATH_MUTE_CTL] =
3027 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3028 else if (nid_has_mute(codec, nid, HDA_INPUT)) {
3029 int idx = path->idx[i];
3030 if (!depth && codec->single_adc_amp)
3031 idx = 0;
3032 path->ctls[NID_PATH_MUTE_CTL] =
3033 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
3037 return 0;
3040 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
3042 struct hda_gen_spec *spec = codec->spec;
3043 struct auto_pin_cfg *cfg = &spec->autocfg;
3044 unsigned int val;
3045 int i;
3047 if (!spec->inv_dmic_split)
3048 return false;
3049 for (i = 0; i < cfg->num_inputs; i++) {
3050 if (cfg->inputs[i].pin != nid)
3051 continue;
3052 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3053 return false;
3054 val = snd_hda_codec_get_pincfg(codec, nid);
3055 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
3057 return false;
3060 /* capture switch put callback for a single control with hook call */
3061 static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
3062 struct snd_ctl_elem_value *ucontrol)
3064 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3065 struct hda_gen_spec *spec = codec->spec;
3066 int ret;
3068 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
3069 if (ret < 0)
3070 return ret;
3072 if (spec->cap_sync_hook)
3073 spec->cap_sync_hook(codec, ucontrol);
3075 return ret;
3078 static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
3079 int idx, bool is_switch, unsigned int ctl,
3080 bool inv_dmic)
3082 struct hda_gen_spec *spec = codec->spec;
3083 char tmpname[44];
3084 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
3085 const char *sfx = is_switch ? "Switch" : "Volume";
3086 unsigned int chs = inv_dmic ? 1 : 3;
3087 struct snd_kcontrol_new *knew;
3089 if (!ctl)
3090 return 0;
3092 if (label)
3093 snprintf(tmpname, sizeof(tmpname),
3094 "%s Capture %s", label, sfx);
3095 else
3096 snprintf(tmpname, sizeof(tmpname),
3097 "Capture %s", sfx);
3098 knew = add_control(spec, type, tmpname, idx,
3099 amp_val_replace_channels(ctl, chs));
3100 if (!knew)
3101 return -ENOMEM;
3102 if (is_switch)
3103 knew->put = cap_single_sw_put;
3104 if (!inv_dmic)
3105 return 0;
3107 /* Make independent right kcontrol */
3108 if (label)
3109 snprintf(tmpname, sizeof(tmpname),
3110 "Inverted %s Capture %s", label, sfx);
3111 else
3112 snprintf(tmpname, sizeof(tmpname),
3113 "Inverted Capture %s", sfx);
3114 knew = add_control(spec, type, tmpname, idx,
3115 amp_val_replace_channels(ctl, 2));
3116 if (!knew)
3117 return -ENOMEM;
3118 if (is_switch)
3119 knew->put = cap_single_sw_put;
3120 return 0;
3123 /* create single (and simple) capture volume and switch controls */
3124 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
3125 unsigned int vol_ctl, unsigned int sw_ctl,
3126 bool inv_dmic)
3128 int err;
3129 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
3130 if (err < 0)
3131 return err;
3132 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
3133 if (err < 0)
3134 return err;
3135 return 0;
3138 /* create bound capture volume and switch controls */
3139 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
3140 unsigned int vol_ctl, unsigned int sw_ctl)
3142 struct hda_gen_spec *spec = codec->spec;
3143 struct snd_kcontrol_new *knew;
3145 if (vol_ctl) {
3146 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
3147 if (!knew)
3148 return -ENOMEM;
3149 knew->index = idx;
3150 knew->private_value = vol_ctl;
3151 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3153 if (sw_ctl) {
3154 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
3155 if (!knew)
3156 return -ENOMEM;
3157 knew->index = idx;
3158 knew->private_value = sw_ctl;
3159 knew->subdevice = HDA_SUBDEV_AMP_FLAG;
3161 return 0;
3164 /* return the vol ctl when used first in the imux list */
3165 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
3167 struct nid_path *path;
3168 unsigned int ctl;
3169 int i;
3171 path = get_input_path(codec, 0, idx);
3172 if (!path)
3173 return 0;
3174 ctl = path->ctls[type];
3175 if (!ctl)
3176 return 0;
3177 for (i = 0; i < idx - 1; i++) {
3178 path = get_input_path(codec, 0, i);
3179 if (path && path->ctls[type] == ctl)
3180 return 0;
3182 return ctl;
3185 /* create individual capture volume and switch controls per input */
3186 static int create_multi_cap_vol_ctl(struct hda_codec *codec)
3188 struct hda_gen_spec *spec = codec->spec;
3189 struct hda_input_mux *imux = &spec->input_mux;
3190 int i, err, type;
3192 for (i = 0; i < imux->num_items; i++) {
3193 bool inv_dmic;
3194 int idx;
3196 idx = imux->items[i].index;
3197 if (idx >= spec->autocfg.num_inputs)
3198 continue;
3199 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
3201 for (type = 0; type < 2; type++) {
3202 err = add_single_cap_ctl(codec,
3203 spec->input_labels[idx],
3204 spec->input_label_idxs[idx],
3205 type,
3206 get_first_cap_ctl(codec, i, type),
3207 inv_dmic);
3208 if (err < 0)
3209 return err;
3212 return 0;
3215 static int create_capture_mixers(struct hda_codec *codec)
3217 struct hda_gen_spec *spec = codec->spec;
3218 struct hda_input_mux *imux = &spec->input_mux;
3219 int i, n, nums, err;
3221 if (spec->dyn_adc_switch)
3222 nums = 1;
3223 else
3224 nums = spec->num_adc_nids;
3226 if (!spec->auto_mic && imux->num_items > 1) {
3227 struct snd_kcontrol_new *knew;
3228 const char *name;
3229 name = nums > 1 ? "Input Source" : "Capture Source";
3230 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
3231 if (!knew)
3232 return -ENOMEM;
3233 knew->count = nums;
3236 for (n = 0; n < nums; n++) {
3237 bool multi = false;
3238 bool multi_cap_vol = spec->multi_cap_vol;
3239 bool inv_dmic = false;
3240 int vol, sw;
3242 vol = sw = 0;
3243 for (i = 0; i < imux->num_items; i++) {
3244 struct nid_path *path;
3245 path = get_input_path(codec, n, i);
3246 if (!path)
3247 continue;
3248 parse_capvol_in_path(codec, path);
3249 if (!vol)
3250 vol = path->ctls[NID_PATH_VOL_CTL];
3251 else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
3252 multi = true;
3253 if (!same_amp_caps(codec, vol,
3254 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
3255 multi_cap_vol = true;
3257 if (!sw)
3258 sw = path->ctls[NID_PATH_MUTE_CTL];
3259 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
3260 multi = true;
3261 if (!same_amp_caps(codec, sw,
3262 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
3263 multi_cap_vol = true;
3265 if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
3266 inv_dmic = true;
3269 if (!multi)
3270 err = create_single_cap_vol_ctl(codec, n, vol, sw,
3271 inv_dmic);
3272 else if (!multi_cap_vol)
3273 err = create_bind_cap_vol_ctl(codec, n, vol, sw);
3274 else
3275 err = create_multi_cap_vol_ctl(codec);
3276 if (err < 0)
3277 return err;
3280 return 0;
3284 * add mic boosts if needed
3287 /* check whether the given amp is feasible as a boost volume */
3288 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
3289 int dir, int idx)
3291 unsigned int step;
3293 if (!nid_has_volume(codec, nid, dir) ||
3294 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
3295 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
3296 return false;
3298 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
3299 >> AC_AMPCAP_STEP_SIZE_SHIFT;
3300 if (step < 0x20)
3301 return false;
3302 return true;
3305 /* look for a boost amp in a widget close to the pin */
3306 static unsigned int look_for_boost_amp(struct hda_codec *codec,
3307 struct nid_path *path)
3309 unsigned int val = 0;
3310 hda_nid_t nid;
3311 int depth;
3313 for (depth = 0; depth < 3; depth++) {
3314 if (depth >= path->depth - 1)
3315 break;
3316 nid = path->path[depth];
3317 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
3318 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
3319 break;
3320 } else if (check_boost_vol(codec, nid, HDA_INPUT,
3321 path->idx[depth])) {
3322 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
3323 HDA_INPUT);
3324 break;
3328 return val;
3331 static int parse_mic_boost(struct hda_codec *codec)
3333 struct hda_gen_spec *spec = codec->spec;
3334 struct auto_pin_cfg *cfg = &spec->autocfg;
3335 struct hda_input_mux *imux = &spec->input_mux;
3336 int i;
3338 if (!spec->num_adc_nids)
3339 return 0;
3341 for (i = 0; i < imux->num_items; i++) {
3342 struct nid_path *path;
3343 unsigned int val;
3344 int idx;
3345 char boost_label[44];
3347 idx = imux->items[i].index;
3348 if (idx >= imux->num_items)
3349 continue;
3351 /* check only line-in and mic pins */
3352 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
3353 continue;
3355 path = get_input_path(codec, 0, i);
3356 if (!path)
3357 continue;
3359 val = look_for_boost_amp(codec, path);
3360 if (!val)
3361 continue;
3363 /* create a boost control */
3364 snprintf(boost_label, sizeof(boost_label),
3365 "%s Boost Volume", spec->input_labels[idx]);
3366 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
3367 spec->input_label_idxs[idx], val))
3368 return -ENOMEM;
3370 path->ctls[NID_PATH_BOOST_CTL] = val;
3372 return 0;
3376 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3378 static void parse_digital(struct hda_codec *codec)
3380 struct hda_gen_spec *spec = codec->spec;
3381 struct nid_path *path;
3382 int i, nums;
3383 hda_nid_t dig_nid, pin;
3385 /* support multiple SPDIFs; the secondary is set up as a slave */
3386 nums = 0;
3387 for (i = 0; i < spec->autocfg.dig_outs; i++) {
3388 pin = spec->autocfg.dig_out_pins[i];
3389 dig_nid = look_for_dac(codec, pin, true);
3390 if (!dig_nid)
3391 continue;
3392 path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
3393 if (!path)
3394 continue;
3395 print_nid_path("digout", path);
3396 path->active = true;
3397 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
3398 set_pin_target(codec, pin, PIN_OUT, false);
3399 if (!nums) {
3400 spec->multiout.dig_out_nid = dig_nid;
3401 spec->dig_out_type = spec->autocfg.dig_out_type[0];
3402 } else {
3403 spec->multiout.slave_dig_outs = spec->slave_dig_outs;
3404 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
3405 break;
3406 spec->slave_dig_outs[nums - 1] = dig_nid;
3408 nums++;
3411 if (spec->autocfg.dig_in_pin) {
3412 pin = spec->autocfg.dig_in_pin;
3413 dig_nid = codec->start_nid;
3414 for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3415 unsigned int wcaps = get_wcaps(codec, dig_nid);
3416 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3417 continue;
3418 if (!(wcaps & AC_WCAP_DIGITAL))
3419 continue;
3420 path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
3421 if (path) {
3422 print_nid_path("digin", path);
3423 path->active = true;
3424 spec->dig_in_nid = dig_nid;
3425 spec->digin_path = snd_hda_get_path_idx(codec, path);
3426 set_pin_target(codec, pin, PIN_IN, false);
3427 break;
3435 * input MUX handling
3438 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
3440 /* select the given imux item; either unmute exclusively or select the route */
3441 static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
3442 unsigned int idx)
3444 struct hda_gen_spec *spec = codec->spec;
3445 const struct hda_input_mux *imux;
3446 struct nid_path *old_path, *path;
3448 imux = &spec->input_mux;
3449 if (!imux->num_items)
3450 return 0;
3452 if (idx >= imux->num_items)
3453 idx = imux->num_items - 1;
3454 if (spec->cur_mux[adc_idx] == idx)
3455 return 0;
3457 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
3458 if (!old_path)
3459 return 0;
3460 if (old_path->active)
3461 snd_hda_activate_path(codec, old_path, false, false);
3463 spec->cur_mux[adc_idx] = idx;
3465 if (spec->shared_mic_hp)
3466 update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
3468 if (spec->dyn_adc_switch)
3469 dyn_adc_pcm_resetup(codec, idx);
3471 path = get_input_path(codec, adc_idx, idx);
3472 if (!path)
3473 return 0;
3474 if (path->active)
3475 return 0;
3476 snd_hda_activate_path(codec, path, true, false);
3477 if (spec->cap_sync_hook)
3478 spec->cap_sync_hook(codec, NULL);
3479 path_power_down_sync(codec, old_path);
3480 return 1;
3485 * Jack detections for HP auto-mute and mic-switch
3488 /* check each pin in the given array; returns true if any of them is plugged */
3489 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3491 int i, present = 0;
3493 for (i = 0; i < num_pins; i++) {
3494 hda_nid_t nid = pins[i];
3495 if (!nid)
3496 break;
3497 /* don't detect pins retasked as inputs */
3498 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
3499 continue;
3500 present |= snd_hda_jack_detect(codec, nid);
3502 return present;
3505 /* standard HP/line-out auto-mute helper */
3506 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
3507 bool mute)
3509 struct hda_gen_spec *spec = codec->spec;
3510 int i;
3512 for (i = 0; i < num_pins; i++) {
3513 hda_nid_t nid = pins[i];
3514 unsigned int val;
3515 if (!nid)
3516 break;
3517 /* don't reset VREF value in case it's controlling
3518 * the amp (see alc861_fixup_asus_amp_vref_0f())
3520 if (spec->keep_vref_in_automute)
3521 val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
3522 else
3523 val = 0;
3524 if (!mute)
3525 val |= snd_hda_codec_get_pin_target(codec, nid);
3526 /* here we call update_pin_ctl() so that the pinctl is changed
3527 * without changing the pinctl target value;
3528 * the original target value will be still referred at the
3529 * init / resume again
3531 update_pin_ctl(codec, nid, val);
3532 set_pin_eapd(codec, nid, !mute);
3536 /* Toggle outputs muting */
3537 void snd_hda_gen_update_outputs(struct hda_codec *codec)
3539 struct hda_gen_spec *spec = codec->spec;
3540 int on;
3542 /* Control HP pins/amps depending on master_mute state;
3543 * in general, HP pins/amps control should be enabled in all cases,
3544 * but currently set only for master_mute, just to be safe
3546 if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
3547 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
3548 spec->autocfg.hp_pins, spec->master_mute);
3550 if (!spec->automute_speaker)
3551 on = 0;
3552 else
3553 on = spec->hp_jack_present | spec->line_jack_present;
3554 on |= spec->master_mute;
3555 spec->speaker_muted = on;
3556 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
3557 spec->autocfg.speaker_pins, on);
3559 /* toggle line-out mutes if needed, too */
3560 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3561 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
3562 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
3563 return;
3564 if (!spec->automute_lo)
3565 on = 0;
3566 else
3567 on = spec->hp_jack_present;
3568 on |= spec->master_mute;
3569 spec->line_out_muted = on;
3570 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3571 spec->autocfg.line_out_pins, on);
3573 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
3575 static void call_update_outputs(struct hda_codec *codec)
3577 struct hda_gen_spec *spec = codec->spec;
3578 if (spec->automute_hook)
3579 spec->automute_hook(codec);
3580 else
3581 snd_hda_gen_update_outputs(codec);
3584 /* standard HP-automute helper */
3585 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3587 struct hda_gen_spec *spec = codec->spec;
3588 hda_nid_t *pins = spec->autocfg.hp_pins;
3589 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins);
3591 /* No detection for the first HP jack during indep-HP mode */
3592 if (spec->indep_hp_enabled) {
3593 pins++;
3594 num_pins--;
3597 spec->hp_jack_present = detect_jacks(codec, num_pins, pins);
3598 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
3599 return;
3600 call_update_outputs(codec);
3602 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
3604 /* standard line-out-automute helper */
3605 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3607 struct hda_gen_spec *spec = codec->spec;
3609 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
3610 return;
3611 /* check LO jack only when it's different from HP */
3612 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
3613 return;
3615 spec->line_jack_present =
3616 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
3617 spec->autocfg.line_out_pins);
3618 if (!spec->automute_speaker || !spec->detect_lo)
3619 return;
3620 call_update_outputs(codec);
3622 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
3624 /* standard mic auto-switch helper */
3625 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
3627 struct hda_gen_spec *spec = codec->spec;
3628 int i;
3630 if (!spec->auto_mic)
3631 return;
3633 for (i = spec->am_num_entries - 1; i > 0; i--) {
3634 hda_nid_t pin = spec->am_entry[i].pin;
3635 /* don't detect pins retasked as outputs */
3636 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
3637 continue;
3638 if (snd_hda_jack_detect(codec, pin)) {
3639 mux_select(codec, 0, spec->am_entry[i].idx);
3640 return;
3643 mux_select(codec, 0, spec->am_entry[0].idx);
3645 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
3647 /* update jack retasking */
3648 static void update_automute_all(struct hda_codec *codec)
3650 struct hda_gen_spec *spec = codec->spec;
3652 if (spec->hp_automute_hook)
3653 spec->hp_automute_hook(codec, NULL);
3654 else
3655 snd_hda_gen_hp_automute(codec, NULL);
3656 if (spec->line_automute_hook)
3657 spec->line_automute_hook(codec, NULL);
3658 else
3659 snd_hda_gen_line_automute(codec, NULL);
3660 if (spec->mic_autoswitch_hook)
3661 spec->mic_autoswitch_hook(codec, NULL);
3662 else
3663 snd_hda_gen_mic_autoswitch(codec, NULL);
3667 * Auto-Mute mode mixer enum support
3669 static int automute_mode_info(struct snd_kcontrol *kcontrol,
3670 struct snd_ctl_elem_info *uinfo)
3672 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3673 struct hda_gen_spec *spec = codec->spec;
3674 static const char * const texts3[] = {
3675 "Disabled", "Speaker Only", "Line Out+Speaker"
3678 if (spec->automute_speaker_possible && spec->automute_lo_possible)
3679 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3680 return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3683 static int automute_mode_get(struct snd_kcontrol *kcontrol,
3684 struct snd_ctl_elem_value *ucontrol)
3686 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3687 struct hda_gen_spec *spec = codec->spec;
3688 unsigned int val = 0;
3689 if (spec->automute_speaker)
3690 val++;
3691 if (spec->automute_lo)
3692 val++;
3694 ucontrol->value.enumerated.item[0] = val;
3695 return 0;
3698 static int automute_mode_put(struct snd_kcontrol *kcontrol,
3699 struct snd_ctl_elem_value *ucontrol)
3701 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3702 struct hda_gen_spec *spec = codec->spec;
3704 switch (ucontrol->value.enumerated.item[0]) {
3705 case 0:
3706 if (!spec->automute_speaker && !spec->automute_lo)
3707 return 0;
3708 spec->automute_speaker = 0;
3709 spec->automute_lo = 0;
3710 break;
3711 case 1:
3712 if (spec->automute_speaker_possible) {
3713 if (!spec->automute_lo && spec->automute_speaker)
3714 return 0;
3715 spec->automute_speaker = 1;
3716 spec->automute_lo = 0;
3717 } else if (spec->automute_lo_possible) {
3718 if (spec->automute_lo)
3719 return 0;
3720 spec->automute_lo = 1;
3721 } else
3722 return -EINVAL;
3723 break;
3724 case 2:
3725 if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
3726 return -EINVAL;
3727 if (spec->automute_speaker && spec->automute_lo)
3728 return 0;
3729 spec->automute_speaker = 1;
3730 spec->automute_lo = 1;
3731 break;
3732 default:
3733 return -EINVAL;
3735 call_update_outputs(codec);
3736 return 1;
3739 static const struct snd_kcontrol_new automute_mode_enum = {
3740 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3741 .name = "Auto-Mute Mode",
3742 .info = automute_mode_info,
3743 .get = automute_mode_get,
3744 .put = automute_mode_put,
3747 static int add_automute_mode_enum(struct hda_codec *codec)
3749 struct hda_gen_spec *spec = codec->spec;
3751 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
3752 return -ENOMEM;
3753 return 0;
3757 * Check the availability of HP/line-out auto-mute;
3758 * Set up appropriately if really supported
3760 static int check_auto_mute_availability(struct hda_codec *codec)
3762 struct hda_gen_spec *spec = codec->spec;
3763 struct auto_pin_cfg *cfg = &spec->autocfg;
3764 int present = 0;
3765 int i, err;
3767 if (spec->suppress_auto_mute)
3768 return 0;
3770 if (cfg->hp_pins[0])
3771 present++;
3772 if (cfg->line_out_pins[0])
3773 present++;
3774 if (cfg->speaker_pins[0])
3775 present++;
3776 if (present < 2) /* need two different output types */
3777 return 0;
3779 if (!cfg->speaker_pins[0] &&
3780 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
3781 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3782 sizeof(cfg->speaker_pins));
3783 cfg->speaker_outs = cfg->line_outs;
3786 if (!cfg->hp_pins[0] &&
3787 cfg->line_out_type == AUTO_PIN_HP_OUT) {
3788 memcpy(cfg->hp_pins, cfg->line_out_pins,
3789 sizeof(cfg->hp_pins));
3790 cfg->hp_outs = cfg->line_outs;
3793 for (i = 0; i < cfg->hp_outs; i++) {
3794 hda_nid_t nid = cfg->hp_pins[i];
3795 if (!is_jack_detectable(codec, nid))
3796 continue;
3797 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3798 nid);
3799 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
3800 spec->hp_automute_hook ?
3801 spec->hp_automute_hook :
3802 snd_hda_gen_hp_automute);
3803 spec->detect_hp = 1;
3806 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
3807 if (cfg->speaker_outs)
3808 for (i = 0; i < cfg->line_outs; i++) {
3809 hda_nid_t nid = cfg->line_out_pins[i];
3810 if (!is_jack_detectable(codec, nid))
3811 continue;
3812 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
3813 snd_hda_jack_detect_enable_callback(codec, nid,
3814 HDA_GEN_FRONT_EVENT,
3815 spec->line_automute_hook ?
3816 spec->line_automute_hook :
3817 snd_hda_gen_line_automute);
3818 spec->detect_lo = 1;
3820 spec->automute_lo_possible = spec->detect_hp;
3823 spec->automute_speaker_possible = cfg->speaker_outs &&
3824 (spec->detect_hp || spec->detect_lo);
3826 spec->automute_lo = spec->automute_lo_possible;
3827 spec->automute_speaker = spec->automute_speaker_possible;
3829 if (spec->automute_speaker_possible || spec->automute_lo_possible) {
3830 /* create a control for automute mode */
3831 err = add_automute_mode_enum(codec);
3832 if (err < 0)
3833 return err;
3835 return 0;
3838 /* check whether all auto-mic pins are valid; setup indices if OK */
3839 static bool auto_mic_check_imux(struct hda_codec *codec)
3841 struct hda_gen_spec *spec = codec->spec;
3842 const struct hda_input_mux *imux;
3843 int i;
3845 imux = &spec->input_mux;
3846 for (i = 0; i < spec->am_num_entries; i++) {
3847 spec->am_entry[i].idx =
3848 find_idx_in_nid_list(spec->am_entry[i].pin,
3849 spec->imux_pins, imux->num_items);
3850 if (spec->am_entry[i].idx < 0)
3851 return false; /* no corresponding imux */
3854 /* we don't need the jack detection for the first pin */
3855 for (i = 1; i < spec->am_num_entries; i++)
3856 snd_hda_jack_detect_enable_callback(codec,
3857 spec->am_entry[i].pin,
3858 HDA_GEN_MIC_EVENT,
3859 spec->mic_autoswitch_hook ?
3860 spec->mic_autoswitch_hook :
3861 snd_hda_gen_mic_autoswitch);
3862 return true;
3865 static int compare_attr(const void *ap, const void *bp)
3867 const struct automic_entry *a = ap;
3868 const struct automic_entry *b = bp;
3869 return (int)(a->attr - b->attr);
3873 * Check the availability of auto-mic switch;
3874 * Set up if really supported
3876 static int check_auto_mic_availability(struct hda_codec *codec)
3878 struct hda_gen_spec *spec = codec->spec;
3879 struct auto_pin_cfg *cfg = &spec->autocfg;
3880 unsigned int types;
3881 int i, num_pins;
3883 if (spec->suppress_auto_mic)
3884 return 0;
3886 types = 0;
3887 num_pins = 0;
3888 for (i = 0; i < cfg->num_inputs; i++) {
3889 hda_nid_t nid = cfg->inputs[i].pin;
3890 unsigned int attr;
3891 attr = snd_hda_codec_get_pincfg(codec, nid);
3892 attr = snd_hda_get_input_pin_attr(attr);
3893 if (types & (1 << attr))
3894 return 0; /* already occupied */
3895 switch (attr) {
3896 case INPUT_PIN_ATTR_INT:
3897 if (cfg->inputs[i].type != AUTO_PIN_MIC)
3898 return 0; /* invalid type */
3899 break;
3900 case INPUT_PIN_ATTR_UNUSED:
3901 return 0; /* invalid entry */
3902 default:
3903 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
3904 return 0; /* invalid type */
3905 if (!spec->line_in_auto_switch &&
3906 cfg->inputs[i].type != AUTO_PIN_MIC)
3907 return 0; /* only mic is allowed */
3908 if (!is_jack_detectable(codec, nid))
3909 return 0; /* no unsol support */
3910 break;
3912 if (num_pins >= MAX_AUTO_MIC_PINS)
3913 return 0;
3914 types |= (1 << attr);
3915 spec->am_entry[num_pins].pin = nid;
3916 spec->am_entry[num_pins].attr = attr;
3917 num_pins++;
3920 if (num_pins < 2)
3921 return 0;
3923 spec->am_num_entries = num_pins;
3924 /* sort the am_entry in the order of attr so that the pin with a
3925 * higher attr will be selected when the jack is plugged.
3927 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
3928 compare_attr, NULL);
3930 if (!auto_mic_check_imux(codec))
3931 return 0;
3933 spec->auto_mic = 1;
3934 spec->num_adc_nids = 1;
3935 spec->cur_mux[0] = spec->am_entry[0].idx;
3936 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3937 spec->am_entry[0].pin,
3938 spec->am_entry[1].pin,
3939 spec->am_entry[2].pin);
3941 return 0;
3944 /* power_filter hook; make inactive widgets into power down */
3945 static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
3946 hda_nid_t nid,
3947 unsigned int power_state)
3949 if (power_state != AC_PWRST_D0)
3950 return power_state;
3951 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER)
3952 return power_state;
3953 if (is_active_nid(codec, nid, HDA_OUTPUT, 0))
3954 return power_state;
3955 return AC_PWRST_D3;
3960 * Parse the given BIOS configuration and set up the hda_gen_spec
3962 * return 1 if successful, 0 if the proper config is not found,
3963 * or a negative error code
3965 int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
3966 struct auto_pin_cfg *cfg)
3968 struct hda_gen_spec *spec = codec->spec;
3969 int err;
3971 parse_user_hints(codec);
3973 if (spec->mixer_nid && !spec->mixer_merge_nid)
3974 spec->mixer_merge_nid = spec->mixer_nid;
3976 if (cfg != &spec->autocfg) {
3977 spec->autocfg = *cfg;
3978 cfg = &spec->autocfg;
3981 fill_all_dac_nids(codec);
3983 if (!cfg->line_outs) {
3984 if (cfg->dig_outs || cfg->dig_in_pin) {
3985 spec->multiout.max_channels = 2;
3986 spec->no_analog = 1;
3987 goto dig_only;
3989 return 0; /* can't find valid BIOS pin config */
3992 if (!spec->no_primary_hp &&
3993 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
3994 cfg->line_outs <= cfg->hp_outs) {
3995 /* use HP as primary out */
3996 cfg->speaker_outs = cfg->line_outs;
3997 memcpy(cfg->speaker_pins, cfg->line_out_pins,
3998 sizeof(cfg->speaker_pins));
3999 cfg->line_outs = cfg->hp_outs;
4000 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
4001 cfg->hp_outs = 0;
4002 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
4003 cfg->line_out_type = AUTO_PIN_HP_OUT;
4006 err = parse_output_paths(codec);
4007 if (err < 0)
4008 return err;
4009 err = create_multi_channel_mode(codec);
4010 if (err < 0)
4011 return err;
4012 err = create_multi_out_ctls(codec, cfg);
4013 if (err < 0)
4014 return err;
4015 err = create_hp_out_ctls(codec);
4016 if (err < 0)
4017 return err;
4018 err = create_speaker_out_ctls(codec);
4019 if (err < 0)
4020 return err;
4021 err = create_indep_hp_ctls(codec);
4022 if (err < 0)
4023 return err;
4024 err = create_loopback_mixing_ctl(codec);
4025 if (err < 0)
4026 return err;
4027 err = create_shared_input(codec);
4028 if (err < 0)
4029 return err;
4030 err = create_input_ctls(codec);
4031 if (err < 0)
4032 return err;
4034 spec->const_channel_count = spec->ext_channel_count;
4035 /* check the multiple speaker and headphone pins */
4036 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
4037 spec->const_channel_count = max(spec->const_channel_count,
4038 cfg->speaker_outs * 2);
4039 if (cfg->line_out_type != AUTO_PIN_HP_OUT)
4040 spec->const_channel_count = max(spec->const_channel_count,
4041 cfg->hp_outs * 2);
4042 spec->multiout.max_channels = max(spec->ext_channel_count,
4043 spec->const_channel_count);
4045 err = check_auto_mute_availability(codec);
4046 if (err < 0)
4047 return err;
4049 err = check_dyn_adc_switch(codec);
4050 if (err < 0)
4051 return err;
4053 if (!spec->shared_mic_hp) {
4054 err = check_auto_mic_availability(codec);
4055 if (err < 0)
4056 return err;
4059 err = create_capture_mixers(codec);
4060 if (err < 0)
4061 return err;
4063 err = parse_mic_boost(codec);
4064 if (err < 0)
4065 return err;
4067 if (spec->add_out_jack_modes) {
4068 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
4069 err = create_out_jack_modes(codec, cfg->line_outs,
4070 cfg->line_out_pins);
4071 if (err < 0)
4072 return err;
4074 if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
4075 err = create_out_jack_modes(codec, cfg->hp_outs,
4076 cfg->hp_pins);
4077 if (err < 0)
4078 return err;
4082 dig_only:
4083 parse_digital(codec);
4085 if (spec->power_down_unused)
4086 codec->power_filter = snd_hda_gen_path_power_filter;
4088 return 1;
4090 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
4094 * Build control elements
4097 /* slave controls for virtual master */
4098 static const char * const slave_pfxs[] = {
4099 "Front", "Surround", "Center", "LFE", "Side",
4100 "Headphone", "Speaker", "Mono", "Line Out",
4101 "CLFE", "Bass Speaker", "PCM",
4102 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4103 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4104 "Headphone Side",
4105 NULL,
4108 int snd_hda_gen_build_controls(struct hda_codec *codec)
4110 struct hda_gen_spec *spec = codec->spec;
4111 int err;
4113 if (spec->kctls.used) {
4114 err = snd_hda_add_new_ctls(codec, spec->kctls.list);
4115 if (err < 0)
4116 return err;
4119 if (spec->multiout.dig_out_nid) {
4120 err = snd_hda_create_dig_out_ctls(codec,
4121 spec->multiout.dig_out_nid,
4122 spec->multiout.dig_out_nid,
4123 spec->pcm_rec[1].pcm_type);
4124 if (err < 0)
4125 return err;
4126 if (!spec->no_analog) {
4127 err = snd_hda_create_spdif_share_sw(codec,
4128 &spec->multiout);
4129 if (err < 0)
4130 return err;
4131 spec->multiout.share_spdif = 1;
4134 if (spec->dig_in_nid) {
4135 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
4136 if (err < 0)
4137 return err;
4140 /* if we have no master control, let's create it */
4141 if (!spec->no_analog &&
4142 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
4143 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
4144 spec->vmaster_tlv, slave_pfxs,
4145 "Playback Volume");
4146 if (err < 0)
4147 return err;
4149 if (!spec->no_analog &&
4150 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
4151 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
4152 NULL, slave_pfxs,
4153 "Playback Switch",
4154 true, &spec->vmaster_mute.sw_kctl);
4155 if (err < 0)
4156 return err;
4157 if (spec->vmaster_mute.hook)
4158 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4159 spec->vmaster_mute_enum);
4162 free_kctls(spec); /* no longer needed */
4164 if (spec->shared_mic_hp) {
4165 int err;
4166 int nid = spec->autocfg.inputs[1].pin;
4167 err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
4168 if (err < 0)
4169 return err;
4170 err = snd_hda_jack_detect_enable(codec, nid, 0);
4171 if (err < 0)
4172 return err;
4175 err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4176 if (err < 0)
4177 return err;
4179 return 0;
4181 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
4185 * PCM definitions
4188 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
4189 struct hda_codec *codec,
4190 struct snd_pcm_substream *substream,
4191 int action)
4193 struct hda_gen_spec *spec = codec->spec;
4194 if (spec->pcm_playback_hook)
4195 spec->pcm_playback_hook(hinfo, codec, substream, action);
4198 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
4199 struct hda_codec *codec,
4200 struct snd_pcm_substream *substream,
4201 int action)
4203 struct hda_gen_spec *spec = codec->spec;
4204 if (spec->pcm_capture_hook)
4205 spec->pcm_capture_hook(hinfo, codec, substream, action);
4209 * Analog playback callbacks
4211 static int playback_pcm_open(struct hda_pcm_stream *hinfo,
4212 struct hda_codec *codec,
4213 struct snd_pcm_substream *substream)
4215 struct hda_gen_spec *spec = codec->spec;
4216 int err;
4218 mutex_lock(&spec->pcm_mutex);
4219 err = snd_hda_multi_out_analog_open(codec,
4220 &spec->multiout, substream,
4221 hinfo);
4222 if (!err) {
4223 spec->active_streams |= 1 << STREAM_MULTI_OUT;
4224 call_pcm_playback_hook(hinfo, codec, substream,
4225 HDA_GEN_PCM_ACT_OPEN);
4227 mutex_unlock(&spec->pcm_mutex);
4228 return err;
4231 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4232 struct hda_codec *codec,
4233 unsigned int stream_tag,
4234 unsigned int format,
4235 struct snd_pcm_substream *substream)
4237 struct hda_gen_spec *spec = codec->spec;
4238 int err;
4240 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
4241 stream_tag, format, substream);
4242 if (!err)
4243 call_pcm_playback_hook(hinfo, codec, substream,
4244 HDA_GEN_PCM_ACT_PREPARE);
4245 return err;
4248 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4249 struct hda_codec *codec,
4250 struct snd_pcm_substream *substream)
4252 struct hda_gen_spec *spec = codec->spec;
4253 int err;
4255 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
4256 if (!err)
4257 call_pcm_playback_hook(hinfo, codec, substream,
4258 HDA_GEN_PCM_ACT_CLEANUP);
4259 return err;
4262 static int playback_pcm_close(struct hda_pcm_stream *hinfo,
4263 struct hda_codec *codec,
4264 struct snd_pcm_substream *substream)
4266 struct hda_gen_spec *spec = codec->spec;
4267 mutex_lock(&spec->pcm_mutex);
4268 spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
4269 call_pcm_playback_hook(hinfo, codec, substream,
4270 HDA_GEN_PCM_ACT_CLOSE);
4271 mutex_unlock(&spec->pcm_mutex);
4272 return 0;
4275 static int capture_pcm_open(struct hda_pcm_stream *hinfo,
4276 struct hda_codec *codec,
4277 struct snd_pcm_substream *substream)
4279 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
4280 return 0;
4283 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4284 struct hda_codec *codec,
4285 unsigned int stream_tag,
4286 unsigned int format,
4287 struct snd_pcm_substream *substream)
4289 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4290 call_pcm_capture_hook(hinfo, codec, substream,
4291 HDA_GEN_PCM_ACT_PREPARE);
4292 return 0;
4295 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4296 struct hda_codec *codec,
4297 struct snd_pcm_substream *substream)
4299 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4300 call_pcm_capture_hook(hinfo, codec, substream,
4301 HDA_GEN_PCM_ACT_CLEANUP);
4302 return 0;
4305 static int capture_pcm_close(struct hda_pcm_stream *hinfo,
4306 struct hda_codec *codec,
4307 struct snd_pcm_substream *substream)
4309 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
4310 return 0;
4313 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
4314 struct hda_codec *codec,
4315 struct snd_pcm_substream *substream)
4317 struct hda_gen_spec *spec = codec->spec;
4318 int err = 0;
4320 mutex_lock(&spec->pcm_mutex);
4321 if (!spec->indep_hp_enabled)
4322 err = -EBUSY;
4323 else
4324 spec->active_streams |= 1 << STREAM_INDEP_HP;
4325 call_pcm_playback_hook(hinfo, codec, substream,
4326 HDA_GEN_PCM_ACT_OPEN);
4327 mutex_unlock(&spec->pcm_mutex);
4328 return err;
4331 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
4332 struct hda_codec *codec,
4333 struct snd_pcm_substream *substream)
4335 struct hda_gen_spec *spec = codec->spec;
4336 mutex_lock(&spec->pcm_mutex);
4337 spec->active_streams &= ~(1 << STREAM_INDEP_HP);
4338 call_pcm_playback_hook(hinfo, codec, substream,
4339 HDA_GEN_PCM_ACT_CLOSE);
4340 mutex_unlock(&spec->pcm_mutex);
4341 return 0;
4344 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4345 struct hda_codec *codec,
4346 unsigned int stream_tag,
4347 unsigned int format,
4348 struct snd_pcm_substream *substream)
4350 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
4351 call_pcm_playback_hook(hinfo, codec, substream,
4352 HDA_GEN_PCM_ACT_PREPARE);
4353 return 0;
4356 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4357 struct hda_codec *codec,
4358 struct snd_pcm_substream *substream)
4360 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
4361 call_pcm_playback_hook(hinfo, codec, substream,
4362 HDA_GEN_PCM_ACT_CLEANUP);
4363 return 0;
4367 * Digital out
4369 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
4370 struct hda_codec *codec,
4371 struct snd_pcm_substream *substream)
4373 struct hda_gen_spec *spec = codec->spec;
4374 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
4377 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
4378 struct hda_codec *codec,
4379 unsigned int stream_tag,
4380 unsigned int format,
4381 struct snd_pcm_substream *substream)
4383 struct hda_gen_spec *spec = codec->spec;
4384 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
4385 stream_tag, format, substream);
4388 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
4389 struct hda_codec *codec,
4390 struct snd_pcm_substream *substream)
4392 struct hda_gen_spec *spec = codec->spec;
4393 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
4396 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
4397 struct hda_codec *codec,
4398 struct snd_pcm_substream *substream)
4400 struct hda_gen_spec *spec = codec->spec;
4401 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
4405 * Analog capture
4407 #define alt_capture_pcm_open capture_pcm_open
4408 #define alt_capture_pcm_close capture_pcm_close
4410 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4411 struct hda_codec *codec,
4412 unsigned int stream_tag,
4413 unsigned int format,
4414 struct snd_pcm_substream *substream)
4416 struct hda_gen_spec *spec = codec->spec;
4418 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
4419 stream_tag, 0, format);
4420 call_pcm_capture_hook(hinfo, codec, substream,
4421 HDA_GEN_PCM_ACT_PREPARE);
4422 return 0;
4425 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4426 struct hda_codec *codec,
4427 struct snd_pcm_substream *substream)
4429 struct hda_gen_spec *spec = codec->spec;
4431 snd_hda_codec_cleanup_stream(codec,
4432 spec->adc_nids[substream->number + 1]);
4433 call_pcm_capture_hook(hinfo, codec, substream,
4434 HDA_GEN_PCM_ACT_CLEANUP);
4435 return 0;
4440 static const struct hda_pcm_stream pcm_analog_playback = {
4441 .substreams = 1,
4442 .channels_min = 2,
4443 .channels_max = 8,
4444 /* NID is set in build_pcms */
4445 .ops = {
4446 .open = playback_pcm_open,
4447 .close = playback_pcm_close,
4448 .prepare = playback_pcm_prepare,
4449 .cleanup = playback_pcm_cleanup
4453 static const struct hda_pcm_stream pcm_analog_capture = {
4454 .substreams = 1,
4455 .channels_min = 2,
4456 .channels_max = 2,
4457 /* NID is set in build_pcms */
4458 .ops = {
4459 .open = capture_pcm_open,
4460 .close = capture_pcm_close,
4461 .prepare = capture_pcm_prepare,
4462 .cleanup = capture_pcm_cleanup
4466 static const struct hda_pcm_stream pcm_analog_alt_playback = {
4467 .substreams = 1,
4468 .channels_min = 2,
4469 .channels_max = 2,
4470 /* NID is set in build_pcms */
4471 .ops = {
4472 .open = alt_playback_pcm_open,
4473 .close = alt_playback_pcm_close,
4474 .prepare = alt_playback_pcm_prepare,
4475 .cleanup = alt_playback_pcm_cleanup
4479 static const struct hda_pcm_stream pcm_analog_alt_capture = {
4480 .substreams = 2, /* can be overridden */
4481 .channels_min = 2,
4482 .channels_max = 2,
4483 /* NID is set in build_pcms */
4484 .ops = {
4485 .open = alt_capture_pcm_open,
4486 .close = alt_capture_pcm_close,
4487 .prepare = alt_capture_pcm_prepare,
4488 .cleanup = alt_capture_pcm_cleanup
4492 static const struct hda_pcm_stream pcm_digital_playback = {
4493 .substreams = 1,
4494 .channels_min = 2,
4495 .channels_max = 2,
4496 /* NID is set in build_pcms */
4497 .ops = {
4498 .open = dig_playback_pcm_open,
4499 .close = dig_playback_pcm_close,
4500 .prepare = dig_playback_pcm_prepare,
4501 .cleanup = dig_playback_pcm_cleanup
4505 static const struct hda_pcm_stream pcm_digital_capture = {
4506 .substreams = 1,
4507 .channels_min = 2,
4508 .channels_max = 2,
4509 /* NID is set in build_pcms */
4512 /* Used by build_pcms to flag that a PCM has no playback stream */
4513 static const struct hda_pcm_stream pcm_null_stream = {
4514 .substreams = 0,
4515 .channels_min = 0,
4516 .channels_max = 0,
4520 * dynamic changing ADC PCM streams
4522 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
4524 struct hda_gen_spec *spec = codec->spec;
4525 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
4527 if (spec->cur_adc && spec->cur_adc != new_adc) {
4528 /* stream is running, let's swap the current ADC */
4529 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
4530 spec->cur_adc = new_adc;
4531 snd_hda_codec_setup_stream(codec, new_adc,
4532 spec->cur_adc_stream_tag, 0,
4533 spec->cur_adc_format);
4534 return true;
4536 return false;
4539 /* analog capture with dynamic dual-adc changes */
4540 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
4541 struct hda_codec *codec,
4542 unsigned int stream_tag,
4543 unsigned int format,
4544 struct snd_pcm_substream *substream)
4546 struct hda_gen_spec *spec = codec->spec;
4547 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
4548 spec->cur_adc_stream_tag = stream_tag;
4549 spec->cur_adc_format = format;
4550 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
4551 return 0;
4554 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
4555 struct hda_codec *codec,
4556 struct snd_pcm_substream *substream)
4558 struct hda_gen_spec *spec = codec->spec;
4559 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
4560 spec->cur_adc = 0;
4561 return 0;
4564 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
4565 .substreams = 1,
4566 .channels_min = 2,
4567 .channels_max = 2,
4568 .nid = 0, /* fill later */
4569 .ops = {
4570 .prepare = dyn_adc_capture_pcm_prepare,
4571 .cleanup = dyn_adc_capture_pcm_cleanup
4575 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
4576 const char *chip_name)
4578 char *p;
4580 if (*str)
4581 return;
4582 strlcpy(str, chip_name, len);
4584 /* drop non-alnum chars after a space */
4585 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
4586 if (!isalnum(p[1])) {
4587 *p = 0;
4588 break;
4591 strlcat(str, sfx, len);
4594 /* build PCM streams based on the parsed results */
4595 int snd_hda_gen_build_pcms(struct hda_codec *codec)
4597 struct hda_gen_spec *spec = codec->spec;
4598 struct hda_pcm *info = spec->pcm_rec;
4599 const struct hda_pcm_stream *p;
4600 bool have_multi_adcs;
4602 codec->num_pcms = 1;
4603 codec->pcm_info = info;
4605 if (spec->no_analog)
4606 goto skip_analog;
4608 fill_pcm_stream_name(spec->stream_name_analog,
4609 sizeof(spec->stream_name_analog),
4610 " Analog", codec->chip_name);
4611 info->name = spec->stream_name_analog;
4613 if (spec->multiout.num_dacs > 0) {
4614 p = spec->stream_analog_playback;
4615 if (!p)
4616 p = &pcm_analog_playback;
4617 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4618 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
4619 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
4620 spec->multiout.max_channels;
4621 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
4622 spec->autocfg.line_outs == 2)
4623 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
4624 snd_pcm_2_1_chmaps;
4626 if (spec->num_adc_nids) {
4627 p = spec->stream_analog_capture;
4628 if (!p) {
4629 if (spec->dyn_adc_switch)
4630 p = &dyn_adc_pcm_analog_capture;
4631 else
4632 p = &pcm_analog_capture;
4634 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4635 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
4638 skip_analog:
4639 /* SPDIF for stream index #1 */
4640 if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
4641 fill_pcm_stream_name(spec->stream_name_digital,
4642 sizeof(spec->stream_name_digital),
4643 " Digital", codec->chip_name);
4644 codec->num_pcms = 2;
4645 codec->slave_dig_outs = spec->multiout.slave_dig_outs;
4646 info = spec->pcm_rec + 1;
4647 info->name = spec->stream_name_digital;
4648 if (spec->dig_out_type)
4649 info->pcm_type = spec->dig_out_type;
4650 else
4651 info->pcm_type = HDA_PCM_TYPE_SPDIF;
4652 if (spec->multiout.dig_out_nid) {
4653 p = spec->stream_digital_playback;
4654 if (!p)
4655 p = &pcm_digital_playback;
4656 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4657 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
4659 if (spec->dig_in_nid) {
4660 p = spec->stream_digital_capture;
4661 if (!p)
4662 p = &pcm_digital_capture;
4663 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4664 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
4668 if (spec->no_analog)
4669 return 0;
4671 /* If the use of more than one ADC is requested for the current
4672 * model, configure a second analog capture-only PCM.
4674 have_multi_adcs = (spec->num_adc_nids > 1) &&
4675 !spec->dyn_adc_switch && !spec->auto_mic;
4676 /* Additional Analaog capture for index #2 */
4677 if (spec->alt_dac_nid || have_multi_adcs) {
4678 fill_pcm_stream_name(spec->stream_name_alt_analog,
4679 sizeof(spec->stream_name_alt_analog),
4680 " Alt Analog", codec->chip_name);
4681 codec->num_pcms = 3;
4682 info = spec->pcm_rec + 2;
4683 info->name = spec->stream_name_alt_analog;
4684 if (spec->alt_dac_nid) {
4685 p = spec->stream_analog_alt_playback;
4686 if (!p)
4687 p = &pcm_analog_alt_playback;
4688 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
4689 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
4690 spec->alt_dac_nid;
4691 } else {
4692 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
4693 pcm_null_stream;
4694 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
4696 if (have_multi_adcs) {
4697 p = spec->stream_analog_alt_capture;
4698 if (!p)
4699 p = &pcm_analog_alt_capture;
4700 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
4701 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
4702 spec->adc_nids[1];
4703 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
4704 spec->num_adc_nids - 1;
4705 } else {
4706 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
4707 pcm_null_stream;
4708 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
4712 return 0;
4714 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
4718 * Standard auto-parser initializations
4721 /* configure the given path as a proper output */
4722 static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
4724 struct nid_path *path;
4725 hda_nid_t pin;
4727 path = snd_hda_get_path_from_idx(codec, path_idx);
4728 if (!path || !path->depth)
4729 return;
4730 pin = path->path[path->depth - 1];
4731 restore_pin_ctl(codec, pin);
4732 snd_hda_activate_path(codec, path, path->active, true);
4733 set_pin_eapd(codec, pin, path->active);
4736 /* initialize primary output paths */
4737 static void init_multi_out(struct hda_codec *codec)
4739 struct hda_gen_spec *spec = codec->spec;
4740 int i;
4742 for (i = 0; i < spec->autocfg.line_outs; i++)
4743 set_output_and_unmute(codec, spec->out_paths[i]);
4747 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
4749 int i;
4751 for (i = 0; i < num_outs; i++)
4752 set_output_and_unmute(codec, paths[i]);
4755 /* initialize hp and speaker paths */
4756 static void init_extra_out(struct hda_codec *codec)
4758 struct hda_gen_spec *spec = codec->spec;
4760 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
4761 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
4762 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
4763 __init_extra_out(codec, spec->autocfg.speaker_outs,
4764 spec->speaker_paths);
4767 /* initialize multi-io paths */
4768 static void init_multi_io(struct hda_codec *codec)
4770 struct hda_gen_spec *spec = codec->spec;
4771 int i;
4773 for (i = 0; i < spec->multi_ios; i++) {
4774 hda_nid_t pin = spec->multi_io[i].pin;
4775 struct nid_path *path;
4776 path = get_multiio_path(codec, i);
4777 if (!path)
4778 continue;
4779 if (!spec->multi_io[i].ctl_in)
4780 spec->multi_io[i].ctl_in =
4781 snd_hda_codec_get_pin_target(codec, pin);
4782 snd_hda_activate_path(codec, path, path->active, true);
4786 /* set up input pins and loopback paths */
4787 static void init_analog_input(struct hda_codec *codec)
4789 struct hda_gen_spec *spec = codec->spec;
4790 struct auto_pin_cfg *cfg = &spec->autocfg;
4791 int i;
4793 for (i = 0; i < cfg->num_inputs; i++) {
4794 hda_nid_t nid = cfg->inputs[i].pin;
4795 if (is_input_pin(codec, nid))
4796 restore_pin_ctl(codec, nid);
4798 /* init loopback inputs */
4799 if (spec->mixer_nid) {
4800 resume_path_from_idx(codec, spec->loopback_paths[i]);
4801 resume_path_from_idx(codec, spec->loopback_merge_path);
4806 /* initialize ADC paths */
4807 static void init_input_src(struct hda_codec *codec)
4809 struct hda_gen_spec *spec = codec->spec;
4810 struct hda_input_mux *imux = &spec->input_mux;
4811 struct nid_path *path;
4812 int i, c, nums;
4814 if (spec->dyn_adc_switch)
4815 nums = 1;
4816 else
4817 nums = spec->num_adc_nids;
4819 for (c = 0; c < nums; c++) {
4820 for (i = 0; i < imux->num_items; i++) {
4821 path = get_input_path(codec, c, i);
4822 if (path) {
4823 bool active = path->active;
4824 if (i == spec->cur_mux[c])
4825 active = true;
4826 snd_hda_activate_path(codec, path, active, false);
4831 if (spec->shared_mic_hp)
4832 update_shared_mic_hp(codec, spec->cur_mux[0]);
4834 if (spec->cap_sync_hook)
4835 spec->cap_sync_hook(codec, NULL);
4838 /* set right pin controls for digital I/O */
4839 static void init_digital(struct hda_codec *codec)
4841 struct hda_gen_spec *spec = codec->spec;
4842 int i;
4843 hda_nid_t pin;
4845 for (i = 0; i < spec->autocfg.dig_outs; i++)
4846 set_output_and_unmute(codec, spec->digout_paths[i]);
4847 pin = spec->autocfg.dig_in_pin;
4848 if (pin) {
4849 restore_pin_ctl(codec, pin);
4850 resume_path_from_idx(codec, spec->digin_path);
4854 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4855 * invalid unsol tags by some reason
4857 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
4859 int i;
4861 for (i = 0; i < codec->init_pins.used; i++) {
4862 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
4863 hda_nid_t nid = pin->nid;
4864 if (is_jack_detectable(codec, nid) &&
4865 !snd_hda_jack_tbl_get(codec, nid))
4866 snd_hda_codec_update_cache(codec, nid, 0,
4867 AC_VERB_SET_UNSOLICITED_ENABLE, 0);
4872 * initialize the generic spec;
4873 * this can be put as patch_ops.init function
4875 int snd_hda_gen_init(struct hda_codec *codec)
4877 struct hda_gen_spec *spec = codec->spec;
4879 if (spec->init_hook)
4880 spec->init_hook(codec);
4882 snd_hda_apply_verbs(codec);
4884 codec->cached_write = 1;
4886 init_multi_out(codec);
4887 init_extra_out(codec);
4888 init_multi_io(codec);
4889 init_analog_input(codec);
4890 init_input_src(codec);
4891 init_digital(codec);
4893 clear_unsol_on_unused_pins(codec);
4895 /* call init functions of standard auto-mute helpers */
4896 update_automute_all(codec);
4898 snd_hda_codec_flush_cache(codec);
4900 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
4901 snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4903 hda_call_check_power_status(codec, 0x01);
4904 return 0;
4906 EXPORT_SYMBOL_HDA(snd_hda_gen_init);
4909 * free the generic spec;
4910 * this can be put as patch_ops.free function
4912 void snd_hda_gen_free(struct hda_codec *codec)
4914 snd_hda_gen_spec_free(codec->spec);
4915 kfree(codec->spec);
4916 codec->spec = NULL;
4918 EXPORT_SYMBOL_HDA(snd_hda_gen_free);
4920 #ifdef CONFIG_PM
4922 * check the loopback power save state;
4923 * this can be put as patch_ops.check_power_status function
4925 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
4927 struct hda_gen_spec *spec = codec->spec;
4928 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
4930 EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
4931 #endif
4935 * the generic codec support
4938 static const struct hda_codec_ops generic_patch_ops = {
4939 .build_controls = snd_hda_gen_build_controls,
4940 .build_pcms = snd_hda_gen_build_pcms,
4941 .init = snd_hda_gen_init,
4942 .free = snd_hda_gen_free,
4943 .unsol_event = snd_hda_jack_unsol_event,
4944 #ifdef CONFIG_PM
4945 .check_power_status = snd_hda_gen_check_power_status,
4946 #endif
4949 int snd_hda_parse_generic_codec(struct hda_codec *codec)
4951 struct hda_gen_spec *spec;
4952 int err;
4954 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4955 if (!spec)
4956 return -ENOMEM;
4957 snd_hda_gen_spec_init(spec);
4958 codec->spec = spec;
4960 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
4961 if (err < 0)
4962 return err;
4964 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
4965 if (err < 0)
4966 goto error;
4968 codec->patch_ops = generic_patch_ops;
4969 return 0;
4971 error:
4972 snd_hda_gen_free(codec);
4973 return err;
4975 EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);