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 <linux/module.h>
32 #include <sound/core.h>
33 #include <sound/jack.h>
34 #include <sound/tlv.h>
35 #include "hda_codec.h"
36 #include "hda_local.h"
37 #include "hda_auto_parser.h"
40 #include "hda_generic.h"
44 * snd_hda_gen_spec_init - initialize hda_gen_spec struct
45 * @spec: hda_gen_spec object to initialize
47 * Initialize the given hda_gen_spec object.
49 int snd_hda_gen_spec_init(struct hda_gen_spec
*spec
)
51 snd_array_init(&spec
->kctls
, sizeof(struct snd_kcontrol_new
), 32);
52 snd_array_init(&spec
->paths
, sizeof(struct nid_path
), 8);
53 snd_array_init(&spec
->loopback_list
, sizeof(struct hda_amp_list
), 8);
54 mutex_init(&spec
->pcm_mutex
);
57 EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init
);
60 * snd_hda_gen_add_kctl - Add a new kctl_new struct from the template
61 * @spec: hda_gen_spec object
62 * @name: name string to override the template, NULL if unchanged
63 * @temp: template for the new kctl
65 * Add a new kctl (actually snd_kcontrol_new to be instantiated later)
66 * element based on the given snd_kcontrol_new template @temp and the
67 * name string @name to the list in @spec.
68 * Returns the newly created object or NULL as error.
70 struct snd_kcontrol_new
*
71 snd_hda_gen_add_kctl(struct hda_gen_spec
*spec
, const char *name
,
72 const struct snd_kcontrol_new
*temp
)
74 struct snd_kcontrol_new
*knew
= snd_array_new(&spec
->kctls
);
79 knew
->name
= kstrdup(name
, GFP_KERNEL
);
81 knew
->name
= kstrdup(knew
->name
, GFP_KERNEL
);
86 EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl
);
88 static void free_kctls(struct hda_gen_spec
*spec
)
90 if (spec
->kctls
.list
) {
91 struct snd_kcontrol_new
*kctl
= spec
->kctls
.list
;
93 for (i
= 0; i
< spec
->kctls
.used
; i
++)
96 snd_array_free(&spec
->kctls
);
99 static void snd_hda_gen_spec_free(struct hda_gen_spec
*spec
)
104 snd_array_free(&spec
->paths
);
105 snd_array_free(&spec
->loopback_list
);
111 static void parse_user_hints(struct hda_codec
*codec
)
113 struct hda_gen_spec
*spec
= codec
->spec
;
116 val
= snd_hda_get_bool_hint(codec
, "jack_detect");
118 codec
->no_jack_detect
= !val
;
119 val
= snd_hda_get_bool_hint(codec
, "inv_jack_detect");
121 codec
->inv_jack_detect
= !!val
;
122 val
= snd_hda_get_bool_hint(codec
, "trigger_sense");
124 codec
->no_trigger_sense
= !val
;
125 val
= snd_hda_get_bool_hint(codec
, "inv_eapd");
127 codec
->inv_eapd
= !!val
;
128 val
= snd_hda_get_bool_hint(codec
, "pcm_format_first");
130 codec
->pcm_format_first
= !!val
;
131 val
= snd_hda_get_bool_hint(codec
, "sticky_stream");
133 codec
->no_sticky_stream
= !val
;
134 val
= snd_hda_get_bool_hint(codec
, "spdif_status_reset");
136 codec
->spdif_status_reset
= !!val
;
137 val
= snd_hda_get_bool_hint(codec
, "pin_amp_workaround");
139 codec
->pin_amp_workaround
= !!val
;
140 val
= snd_hda_get_bool_hint(codec
, "single_adc_amp");
142 codec
->single_adc_amp
= !!val
;
144 val
= snd_hda_get_bool_hint(codec
, "auto_mute");
146 spec
->suppress_auto_mute
= !val
;
147 val
= snd_hda_get_bool_hint(codec
, "auto_mic");
149 spec
->suppress_auto_mic
= !val
;
150 val
= snd_hda_get_bool_hint(codec
, "line_in_auto_switch");
152 spec
->line_in_auto_switch
= !!val
;
153 val
= snd_hda_get_bool_hint(codec
, "auto_mute_via_amp");
155 spec
->auto_mute_via_amp
= !!val
;
156 val
= snd_hda_get_bool_hint(codec
, "need_dac_fix");
158 spec
->need_dac_fix
= !!val
;
159 val
= snd_hda_get_bool_hint(codec
, "primary_hp");
161 spec
->no_primary_hp
= !val
;
162 val
= snd_hda_get_bool_hint(codec
, "multi_io");
164 spec
->no_multi_io
= !val
;
165 val
= snd_hda_get_bool_hint(codec
, "multi_cap_vol");
167 spec
->multi_cap_vol
= !!val
;
168 val
= snd_hda_get_bool_hint(codec
, "inv_dmic_split");
170 spec
->inv_dmic_split
= !!val
;
171 val
= snd_hda_get_bool_hint(codec
, "indep_hp");
173 spec
->indep_hp
= !!val
;
174 val
= snd_hda_get_bool_hint(codec
, "add_stereo_mix_input");
176 spec
->add_stereo_mix_input
= !!val
;
177 /* the following two are just for compatibility */
178 val
= snd_hda_get_bool_hint(codec
, "add_out_jack_modes");
180 spec
->add_jack_modes
= !!val
;
181 val
= snd_hda_get_bool_hint(codec
, "add_in_jack_modes");
183 spec
->add_jack_modes
= !!val
;
184 val
= snd_hda_get_bool_hint(codec
, "add_jack_modes");
186 spec
->add_jack_modes
= !!val
;
187 val
= snd_hda_get_bool_hint(codec
, "power_down_unused");
189 spec
->power_down_unused
= !!val
;
190 val
= snd_hda_get_bool_hint(codec
, "add_hp_mic");
192 spec
->hp_mic
= !!val
;
193 val
= snd_hda_get_bool_hint(codec
, "hp_mic_detect");
195 spec
->suppress_hp_mic_detect
= !val
;
197 if (!snd_hda_get_int_hint(codec
, "mixer_nid", &val
))
198 spec
->mixer_nid
= val
;
202 * pin control value accesses
205 #define update_pin_ctl(codec, pin, val) \
206 snd_hda_codec_update_cache(codec, pin, 0, \
207 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
209 /* restore the pinctl based on the cached value */
210 static inline void restore_pin_ctl(struct hda_codec
*codec
, hda_nid_t pin
)
212 update_pin_ctl(codec
, pin
, snd_hda_codec_get_pin_target(codec
, pin
));
215 /* set the pinctl target value and write it if requested */
216 static void set_pin_target(struct hda_codec
*codec
, hda_nid_t pin
,
217 unsigned int val
, bool do_write
)
221 val
= snd_hda_correct_pin_ctl(codec
, pin
, val
);
222 snd_hda_codec_set_pin_target(codec
, pin
, val
);
224 update_pin_ctl(codec
, pin
, val
);
227 /* set pinctl target values for all given pins */
228 static void set_pin_targets(struct hda_codec
*codec
, int num_pins
,
229 hda_nid_t
*pins
, unsigned int val
)
232 for (i
= 0; i
< num_pins
; i
++)
233 set_pin_target(codec
, pins
[i
], val
, false);
240 /* return the position of NID in the list, or -1 if not found */
241 static int find_idx_in_nid_list(hda_nid_t nid
, const hda_nid_t
*list
, int nums
)
244 for (i
= 0; i
< nums
; i
++)
250 /* return true if the given NID is contained in the path */
251 static bool is_nid_contained(struct nid_path
*path
, hda_nid_t nid
)
253 return find_idx_in_nid_list(nid
, path
->path
, path
->depth
) >= 0;
256 static struct nid_path
*get_nid_path(struct hda_codec
*codec
,
257 hda_nid_t from_nid
, hda_nid_t to_nid
,
260 struct hda_gen_spec
*spec
= codec
->spec
;
263 for (i
= 0; i
< spec
->paths
.used
; i
++) {
264 struct nid_path
*path
= snd_array_elem(&spec
->paths
, i
);
265 if (path
->depth
<= 0)
267 if ((!from_nid
|| path
->path
[0] == from_nid
) &&
268 (!to_nid
|| path
->path
[path
->depth
- 1] == to_nid
)) {
270 (anchor_nid
> 0 && is_nid_contained(path
, anchor_nid
)) ||
271 (anchor_nid
< 0 && !is_nid_contained(path
, anchor_nid
)))
279 * snd_hda_get_nid_path - get the path between the given NIDs
280 * @codec: the HDA codec
281 * @from_nid: the NID where the path start from
282 * @to_nid: the NID where the path ends at
284 * Return the found nid_path object or NULL for error.
285 * Passing 0 to either @from_nid or @to_nid behaves as a wildcard.
287 struct nid_path
*snd_hda_get_nid_path(struct hda_codec
*codec
,
288 hda_nid_t from_nid
, hda_nid_t to_nid
)
290 return get_nid_path(codec
, from_nid
, to_nid
, 0);
292 EXPORT_SYMBOL_GPL(snd_hda_get_nid_path
);
295 * snd_hda_get_path_idx - get the index number corresponding to the path
297 * @codec: the HDA codec
298 * @path: nid_path object
300 * The returned index starts from 1, i.e. the actual array index with offset 1,
301 * and zero is handled as an invalid path
303 int snd_hda_get_path_idx(struct hda_codec
*codec
, struct nid_path
*path
)
305 struct hda_gen_spec
*spec
= codec
->spec
;
306 struct nid_path
*array
= spec
->paths
.list
;
309 if (!spec
->paths
.used
)
312 if (idx
< 0 || idx
>= spec
->paths
.used
)
316 EXPORT_SYMBOL_GPL(snd_hda_get_path_idx
);
319 * snd_hda_get_path_from_idx - get the path instance corresponding to the
321 * @codec: the HDA codec
322 * @idx: the path index
324 struct nid_path
*snd_hda_get_path_from_idx(struct hda_codec
*codec
, int idx
)
326 struct hda_gen_spec
*spec
= codec
->spec
;
328 if (idx
<= 0 || idx
> spec
->paths
.used
)
330 return snd_array_elem(&spec
->paths
, idx
- 1);
332 EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx
);
334 /* check whether the given DAC is already found in any existing paths */
335 static bool is_dac_already_used(struct hda_codec
*codec
, hda_nid_t nid
)
337 struct hda_gen_spec
*spec
= codec
->spec
;
340 for (i
= 0; i
< spec
->paths
.used
; i
++) {
341 struct nid_path
*path
= snd_array_elem(&spec
->paths
, i
);
342 if (path
->path
[0] == nid
)
348 /* check whether the given two widgets can be connected */
349 static bool is_reachable_path(struct hda_codec
*codec
,
350 hda_nid_t from_nid
, hda_nid_t to_nid
)
352 if (!from_nid
|| !to_nid
)
354 return snd_hda_get_conn_index(codec
, to_nid
, from_nid
, true) >= 0;
357 /* nid, dir and idx */
358 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
360 /* check whether the given ctl is already assigned in any path elements */
361 static bool is_ctl_used(struct hda_codec
*codec
, unsigned int val
, int type
)
363 struct hda_gen_spec
*spec
= codec
->spec
;
366 val
&= AMP_VAL_COMPARE_MASK
;
367 for (i
= 0; i
< spec
->paths
.used
; i
++) {
368 struct nid_path
*path
= snd_array_elem(&spec
->paths
, i
);
369 if ((path
->ctls
[type
] & AMP_VAL_COMPARE_MASK
) == val
)
375 /* check whether a control with the given (nid, dir, idx) was assigned */
376 static bool is_ctl_associated(struct hda_codec
*codec
, hda_nid_t nid
,
377 int dir
, int idx
, int type
)
379 unsigned int val
= HDA_COMPOSE_AMP_VAL(nid
, 3, idx
, dir
);
380 return is_ctl_used(codec
, val
, type
);
383 static void print_nid_path(struct hda_codec
*codec
,
384 const char *pfx
, struct nid_path
*path
)
391 for (i
= 0; i
< path
->depth
; i
++)
392 pos
+= scnprintf(pos
, sizeof(buf
) - (pos
- buf
), "%s%02x",
393 pos
!= buf
? ":" : "",
396 codec_dbg(codec
, "%s path: depth=%d '%s'\n", pfx
, path
->depth
, buf
);
399 /* called recursively */
400 static bool __parse_nid_path(struct hda_codec
*codec
,
401 hda_nid_t from_nid
, hda_nid_t to_nid
,
402 int anchor_nid
, struct nid_path
*path
,
405 const hda_nid_t
*conn
;
408 if (to_nid
== anchor_nid
)
409 anchor_nid
= 0; /* anchor passed */
410 else if (to_nid
== (hda_nid_t
)(-anchor_nid
))
411 return false; /* hit the exclusive nid */
413 nums
= snd_hda_get_conn_list(codec
, to_nid
, &conn
);
414 for (i
= 0; i
< nums
; i
++) {
415 if (conn
[i
] != from_nid
) {
416 /* special case: when from_nid is 0,
417 * try to find an empty DAC
420 get_wcaps_type(get_wcaps(codec
, conn
[i
])) != AC_WID_AUD_OUT
||
421 is_dac_already_used(codec
, conn
[i
]))
424 /* anchor is not requested or already passed? */
428 if (depth
>= MAX_NID_PATH_DEPTH
)
430 for (i
= 0; i
< nums
; i
++) {
432 type
= get_wcaps_type(get_wcaps(codec
, conn
[i
]));
433 if (type
== AC_WID_AUD_OUT
|| type
== AC_WID_AUD_IN
||
436 if (__parse_nid_path(codec
, from_nid
, conn
[i
],
437 anchor_nid
, path
, depth
+ 1))
443 path
->path
[path
->depth
] = conn
[i
];
444 path
->idx
[path
->depth
+ 1] = i
;
445 if (nums
> 1 && get_wcaps_type(get_wcaps(codec
, to_nid
)) != AC_WID_AUD_MIX
)
446 path
->multi
[path
->depth
+ 1] = 1;
452 * snd_hda_parse_nid_path - parse the widget path from the given nid to
454 * @codec: the HDA codec
455 * @from_nid: the NID where the path start from
456 * @to_nid: the NID where the path ends at
457 * @anchor_nid: the anchor indication
458 * @path: the path object to store the result
460 * Returns true if a matching path is found.
462 * The parsing behavior depends on parameters:
463 * when @from_nid is 0, try to find an empty DAC;
464 * when @anchor_nid is set to a positive value, only paths through the widget
465 * with the given value are evaluated.
466 * when @anchor_nid is set to a negative value, paths through the widget
467 * with the negative of given value are excluded, only other paths are chosen.
468 * when @anchor_nid is zero, no special handling about path selection.
470 bool snd_hda_parse_nid_path(struct hda_codec
*codec
, hda_nid_t from_nid
,
471 hda_nid_t to_nid
, int anchor_nid
,
472 struct nid_path
*path
)
474 if (__parse_nid_path(codec
, from_nid
, to_nid
, anchor_nid
, path
, 1)) {
475 path
->path
[path
->depth
] = to_nid
;
481 EXPORT_SYMBOL_GPL(snd_hda_parse_nid_path
);
484 * snd_hda_add_new_path - parse the path between the given NIDs and
485 * add to the path list
486 * @codec: the HDA codec
487 * @from_nid: the NID where the path start from
488 * @to_nid: the NID where the path ends at
489 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
491 * If no valid path is found, returns NULL.
494 snd_hda_add_new_path(struct hda_codec
*codec
, hda_nid_t from_nid
,
495 hda_nid_t to_nid
, int anchor_nid
)
497 struct hda_gen_spec
*spec
= codec
->spec
;
498 struct nid_path
*path
;
500 if (from_nid
&& to_nid
&& !is_reachable_path(codec
, from_nid
, to_nid
))
503 /* check whether the path has been already added */
504 path
= get_nid_path(codec
, from_nid
, to_nid
, anchor_nid
);
508 path
= snd_array_new(&spec
->paths
);
511 memset(path
, 0, sizeof(*path
));
512 if (snd_hda_parse_nid_path(codec
, from_nid
, to_nid
, anchor_nid
, path
))
518 EXPORT_SYMBOL_GPL(snd_hda_add_new_path
);
520 /* clear the given path as invalid so that it won't be picked up later */
521 static void invalidate_nid_path(struct hda_codec
*codec
, int idx
)
523 struct nid_path
*path
= snd_hda_get_path_from_idx(codec
, idx
);
526 memset(path
, 0, sizeof(*path
));
529 /* return a DAC if paired to the given pin by codec driver */
530 static hda_nid_t
get_preferred_dac(struct hda_codec
*codec
, hda_nid_t pin
)
532 struct hda_gen_spec
*spec
= codec
->spec
;
533 const hda_nid_t
*list
= spec
->preferred_dacs
;
537 for (; *list
; list
+= 2)
543 /* look for an empty DAC slot */
544 static hda_nid_t
look_for_dac(struct hda_codec
*codec
, hda_nid_t pin
,
547 struct hda_gen_spec
*spec
= codec
->spec
;
551 for (i
= 0; i
< spec
->num_all_dacs
; i
++) {
552 hda_nid_t nid
= spec
->all_dacs
[i
];
553 if (!nid
|| is_dac_already_used(codec
, nid
))
555 cap_digital
= !!(get_wcaps(codec
, nid
) & AC_WCAP_DIGITAL
);
556 if (is_digital
!= cap_digital
)
558 if (is_reachable_path(codec
, nid
, pin
))
564 /* replace the channels in the composed amp value with the given number */
565 static unsigned int amp_val_replace_channels(unsigned int val
, unsigned int chs
)
567 val
&= ~(0x3U
<< 16);
572 static bool same_amp_caps(struct hda_codec
*codec
, hda_nid_t nid1
,
573 hda_nid_t nid2
, int dir
)
575 if (!(get_wcaps(codec
, nid1
) & (1 << (dir
+ 1))))
576 return !(get_wcaps(codec
, nid2
) & (1 << (dir
+ 1)));
577 return (query_amp_caps(codec
, nid1
, dir
) ==
578 query_amp_caps(codec
, nid2
, dir
));
581 /* look for a widget suitable for assigning a mute switch in the path */
582 static hda_nid_t
look_for_out_mute_nid(struct hda_codec
*codec
,
583 struct nid_path
*path
)
587 for (i
= path
->depth
- 1; i
>= 0; i
--) {
588 if (nid_has_mute(codec
, path
->path
[i
], HDA_OUTPUT
))
589 return path
->path
[i
];
590 if (i
!= path
->depth
- 1 && i
!= 0 &&
591 nid_has_mute(codec
, path
->path
[i
], HDA_INPUT
))
592 return path
->path
[i
];
597 /* look for a widget suitable for assigning a volume ctl in the path */
598 static hda_nid_t
look_for_out_vol_nid(struct hda_codec
*codec
,
599 struct nid_path
*path
)
601 struct hda_gen_spec
*spec
= codec
->spec
;
604 for (i
= path
->depth
- 1; i
>= 0; i
--) {
605 hda_nid_t nid
= path
->path
[i
];
606 if ((spec
->out_vol_mask
>> nid
) & 1)
608 if (nid_has_volume(codec
, nid
, HDA_OUTPUT
))
615 * path activation / deactivation
618 /* can have the amp-in capability? */
619 static bool has_amp_in(struct hda_codec
*codec
, struct nid_path
*path
, int idx
)
621 hda_nid_t nid
= path
->path
[idx
];
622 unsigned int caps
= get_wcaps(codec
, nid
);
623 unsigned int type
= get_wcaps_type(caps
);
625 if (!(caps
& AC_WCAP_IN_AMP
))
627 if (type
== AC_WID_PIN
&& idx
> 0) /* only for input pins */
632 /* can have the amp-out capability? */
633 static bool has_amp_out(struct hda_codec
*codec
, struct nid_path
*path
, int idx
)
635 hda_nid_t nid
= path
->path
[idx
];
636 unsigned int caps
= get_wcaps(codec
, nid
);
637 unsigned int type
= get_wcaps_type(caps
);
639 if (!(caps
& AC_WCAP_OUT_AMP
))
641 if (type
== AC_WID_PIN
&& !idx
) /* only for output pins */
646 /* check whether the given (nid,dir,idx) is active */
647 static bool is_active_nid(struct hda_codec
*codec
, hda_nid_t nid
,
648 unsigned int dir
, unsigned int idx
)
650 struct hda_gen_spec
*spec
= codec
->spec
;
653 for (n
= 0; n
< spec
->paths
.used
; n
++) {
654 struct nid_path
*path
= snd_array_elem(&spec
->paths
, n
);
657 for (i
= 0; i
< path
->depth
; i
++) {
658 if (path
->path
[i
] == nid
) {
659 if (dir
== HDA_OUTPUT
|| path
->idx
[i
] == idx
)
668 /* check whether the NID is referred by any active paths */
669 #define is_active_nid_for_any(codec, nid) \
670 is_active_nid(codec, nid, HDA_OUTPUT, 0)
672 /* get the default amp value for the target state */
673 static int get_amp_val_to_activate(struct hda_codec
*codec
, hda_nid_t nid
,
674 int dir
, unsigned int caps
, bool enable
)
676 unsigned int val
= 0;
678 if (caps
& AC_AMPCAP_NUM_STEPS
) {
681 val
= (caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
;
683 if (caps
& (AC_AMPCAP_MUTE
| AC_AMPCAP_MIN_MUTE
)) {
690 /* initialize the amp value (only at the first time) */
691 static void init_amp(struct hda_codec
*codec
, hda_nid_t nid
, int dir
, int idx
)
693 unsigned int caps
= query_amp_caps(codec
, nid
, dir
);
694 int val
= get_amp_val_to_activate(codec
, nid
, dir
, caps
, false);
695 snd_hda_codec_amp_init_stereo(codec
, nid
, dir
, idx
, 0xff, val
);
698 /* calculate amp value mask we can modify;
699 * if the given amp is controlled by mixers, don't touch it
701 static unsigned int get_amp_mask_to_modify(struct hda_codec
*codec
,
702 hda_nid_t nid
, int dir
, int idx
,
705 unsigned int mask
= 0xff;
707 if (caps
& (AC_AMPCAP_MUTE
| AC_AMPCAP_MIN_MUTE
)) {
708 if (is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_MUTE_CTL
))
711 if (caps
& AC_AMPCAP_NUM_STEPS
) {
712 if (is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_VOL_CTL
) ||
713 is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_BOOST_CTL
))
719 static void activate_amp(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
720 int idx
, int idx_to_check
, bool enable
)
723 unsigned int mask
, val
;
725 if (!enable
&& is_active_nid(codec
, nid
, dir
, idx_to_check
))
728 caps
= query_amp_caps(codec
, nid
, dir
);
729 val
= get_amp_val_to_activate(codec
, nid
, dir
, caps
, enable
);
730 mask
= get_amp_mask_to_modify(codec
, nid
, dir
, idx_to_check
, caps
);
735 snd_hda_codec_amp_stereo(codec
, nid
, dir
, idx
, mask
, val
);
738 static void activate_amp_out(struct hda_codec
*codec
, struct nid_path
*path
,
741 hda_nid_t nid
= path
->path
[i
];
742 init_amp(codec
, nid
, HDA_OUTPUT
, 0);
743 activate_amp(codec
, nid
, HDA_OUTPUT
, 0, 0, enable
);
746 static void activate_amp_in(struct hda_codec
*codec
, struct nid_path
*path
,
747 int i
, bool enable
, bool add_aamix
)
749 struct hda_gen_spec
*spec
= codec
->spec
;
750 const hda_nid_t
*conn
;
753 hda_nid_t nid
= path
->path
[i
];
755 nums
= snd_hda_get_conn_list(codec
, nid
, &conn
);
756 type
= get_wcaps_type(get_wcaps(codec
, nid
));
757 if (type
== AC_WID_PIN
||
758 (type
== AC_WID_AUD_IN
&& codec
->single_adc_amp
)) {
764 for (n
= 0; n
< nums
; n
++)
765 init_amp(codec
, nid
, HDA_INPUT
, n
);
767 /* here is a little bit tricky in comparison with activate_amp_out();
768 * when aa-mixer is available, we need to enable the path as well
770 for (n
= 0; n
< nums
; n
++) {
771 if (n
!= idx
&& (!add_aamix
|| conn
[n
] != spec
->mixer_merge_nid
))
773 activate_amp(codec
, nid
, HDA_INPUT
, n
, idx
, enable
);
778 * snd_hda_activate_path - activate or deactivate the given path
779 * @codec: the HDA codec
780 * @path: the path to activate/deactivate
781 * @enable: flag to activate or not
782 * @add_aamix: enable the input from aamix NID
784 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
786 void snd_hda_activate_path(struct hda_codec
*codec
, struct nid_path
*path
,
787 bool enable
, bool add_aamix
)
789 struct hda_gen_spec
*spec
= codec
->spec
;
793 path
->active
= false;
795 for (i
= path
->depth
- 1; i
>= 0; i
--) {
796 hda_nid_t nid
= path
->path
[i
];
797 if (enable
&& spec
->power_down_unused
) {
798 /* make sure the widget is powered up */
799 if (!snd_hda_check_power_state(codec
, nid
, AC_PWRST_D0
))
800 snd_hda_codec_write(codec
, nid
, 0,
801 AC_VERB_SET_POWER_STATE
,
804 if (enable
&& path
->multi
[i
])
805 snd_hda_codec_update_cache(codec
, nid
, 0,
806 AC_VERB_SET_CONNECT_SEL
,
808 if (has_amp_in(codec
, path
, i
))
809 activate_amp_in(codec
, path
, i
, enable
, add_aamix
);
810 if (has_amp_out(codec
, path
, i
))
811 activate_amp_out(codec
, path
, i
, enable
);
817 EXPORT_SYMBOL_GPL(snd_hda_activate_path
);
819 /* if the given path is inactive, put widgets into D3 (only if suitable) */
820 static void path_power_down_sync(struct hda_codec
*codec
, struct nid_path
*path
)
822 struct hda_gen_spec
*spec
= codec
->spec
;
823 bool changed
= false;
826 if (!spec
->power_down_unused
|| path
->active
)
829 for (i
= 0; i
< path
->depth
; i
++) {
830 hda_nid_t nid
= path
->path
[i
];
831 if (!snd_hda_check_power_state(codec
, nid
, AC_PWRST_D3
) &&
832 !is_active_nid_for_any(codec
, nid
)) {
833 snd_hda_codec_write(codec
, nid
, 0,
834 AC_VERB_SET_POWER_STATE
,
842 snd_hda_codec_read(codec
, path
->path
[0], 0,
843 AC_VERB_GET_POWER_STATE
, 0);
847 /* turn on/off EAPD on the given pin */
848 static void set_pin_eapd(struct hda_codec
*codec
, hda_nid_t pin
, bool enable
)
850 struct hda_gen_spec
*spec
= codec
->spec
;
851 if (spec
->own_eapd_ctl
||
852 !(snd_hda_query_pin_caps(codec
, pin
) & AC_PINCAP_EAPD
))
854 if (spec
->keep_eapd_on
&& !enable
)
858 snd_hda_codec_update_cache(codec
, pin
, 0,
859 AC_VERB_SET_EAPD_BTLENABLE
,
860 enable
? 0x02 : 0x00);
863 /* re-initialize the path specified by the given path index */
864 static void resume_path_from_idx(struct hda_codec
*codec
, int path_idx
)
866 struct nid_path
*path
= snd_hda_get_path_from_idx(codec
, path_idx
);
868 snd_hda_activate_path(codec
, path
, path
->active
, false);
873 * Helper functions for creating mixer ctl elements
876 static int hda_gen_mixer_mute_put(struct snd_kcontrol
*kcontrol
,
877 struct snd_ctl_elem_value
*ucontrol
);
878 static int hda_gen_bind_mute_put(struct snd_kcontrol
*kcontrol
,
879 struct snd_ctl_elem_value
*ucontrol
);
886 static const struct snd_kcontrol_new control_templates
[] = {
887 HDA_CODEC_VOLUME(NULL
, 0, 0, 0),
888 /* only the put callback is replaced for handling the special mute */
890 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
891 .subdevice
= HDA_SUBDEV_AMP_FLAG
,
892 .info
= snd_hda_mixer_amp_switch_info
,
893 .get
= snd_hda_mixer_amp_switch_get
,
894 .put
= hda_gen_mixer_mute_put
, /* replaced */
895 .private_value
= HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
898 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
899 .info
= snd_hda_mixer_amp_switch_info
,
900 .get
= snd_hda_mixer_bind_switch_get
,
901 .put
= hda_gen_bind_mute_put
, /* replaced */
902 .private_value
= HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
906 /* add dynamic controls from template */
907 static struct snd_kcontrol_new
*
908 add_control(struct hda_gen_spec
*spec
, int type
, const char *name
,
909 int cidx
, unsigned long val
)
911 struct snd_kcontrol_new
*knew
;
913 knew
= snd_hda_gen_add_kctl(spec
, name
, &control_templates
[type
]);
917 if (get_amp_nid_(val
))
918 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
919 knew
->private_value
= val
;
923 static int add_control_with_pfx(struct hda_gen_spec
*spec
, int type
,
924 const char *pfx
, const char *dir
,
925 const char *sfx
, int cidx
, unsigned long val
)
927 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
928 snprintf(name
, sizeof(name
), "%s %s %s", pfx
, dir
, sfx
);
929 if (!add_control(spec
, type
, name
, cidx
, val
))
934 #define add_pb_vol_ctrl(spec, type, pfx, val) \
935 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
936 #define add_pb_sw_ctrl(spec, type, pfx, val) \
937 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
938 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
939 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
940 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
941 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
943 static int add_vol_ctl(struct hda_codec
*codec
, const char *pfx
, int cidx
,
944 unsigned int chs
, struct nid_path
*path
)
949 val
= path
->ctls
[NID_PATH_VOL_CTL
];
952 val
= amp_val_replace_channels(val
, chs
);
953 return __add_pb_vol_ctrl(codec
->spec
, HDA_CTL_WIDGET_VOL
, pfx
, cidx
, val
);
956 /* return the channel bits suitable for the given path->ctls[] */
957 static int get_default_ch_nums(struct hda_codec
*codec
, struct nid_path
*path
,
960 int chs
= 1; /* mono (left only) */
962 hda_nid_t nid
= get_amp_nid_(path
->ctls
[type
]);
963 if (nid
&& (get_wcaps(codec
, nid
) & AC_WCAP_STEREO
))
964 chs
= 3; /* stereo */
969 static int add_stereo_vol(struct hda_codec
*codec
, const char *pfx
, int cidx
,
970 struct nid_path
*path
)
972 int chs
= get_default_ch_nums(codec
, path
, NID_PATH_VOL_CTL
);
973 return add_vol_ctl(codec
, pfx
, cidx
, chs
, path
);
976 /* create a mute-switch for the given mixer widget;
977 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
979 static int add_sw_ctl(struct hda_codec
*codec
, const char *pfx
, int cidx
,
980 unsigned int chs
, struct nid_path
*path
)
983 int type
= HDA_CTL_WIDGET_MUTE
;
987 val
= path
->ctls
[NID_PATH_MUTE_CTL
];
990 val
= amp_val_replace_channels(val
, chs
);
991 if (get_amp_direction_(val
) == HDA_INPUT
) {
992 hda_nid_t nid
= get_amp_nid_(val
);
993 int nums
= snd_hda_get_num_conns(codec
, nid
);
995 type
= HDA_CTL_BIND_MUTE
;
999 return __add_pb_sw_ctrl(codec
->spec
, type
, pfx
, cidx
, val
);
1002 static int add_stereo_sw(struct hda_codec
*codec
, const char *pfx
,
1003 int cidx
, struct nid_path
*path
)
1005 int chs
= get_default_ch_nums(codec
, path
, NID_PATH_MUTE_CTL
);
1006 return add_sw_ctl(codec
, pfx
, cidx
, chs
, path
);
1009 /* playback mute control with the software mute bit check */
1010 static void sync_auto_mute_bits(struct snd_kcontrol
*kcontrol
,
1011 struct snd_ctl_elem_value
*ucontrol
)
1013 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1014 struct hda_gen_spec
*spec
= codec
->spec
;
1016 if (spec
->auto_mute_via_amp
) {
1017 hda_nid_t nid
= get_amp_nid(kcontrol
);
1018 bool enabled
= !((spec
->mute_bits
>> nid
) & 1);
1019 ucontrol
->value
.integer
.value
[0] &= enabled
;
1020 ucontrol
->value
.integer
.value
[1] &= enabled
;
1024 static int hda_gen_mixer_mute_put(struct snd_kcontrol
*kcontrol
,
1025 struct snd_ctl_elem_value
*ucontrol
)
1027 sync_auto_mute_bits(kcontrol
, ucontrol
);
1028 return snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
1031 static int hda_gen_bind_mute_put(struct snd_kcontrol
*kcontrol
,
1032 struct snd_ctl_elem_value
*ucontrol
)
1034 sync_auto_mute_bits(kcontrol
, ucontrol
);
1035 return snd_hda_mixer_bind_switch_put(kcontrol
, ucontrol
);
1038 /* any ctl assigned to the path with the given index? */
1039 static bool path_has_mixer(struct hda_codec
*codec
, int path_idx
, int ctl_type
)
1041 struct nid_path
*path
= snd_hda_get_path_from_idx(codec
, path_idx
);
1042 return path
&& path
->ctls
[ctl_type
];
1045 static const char * const channel_name
[4] = {
1046 "Front", "Surround", "CLFE", "Side"
1049 /* give some appropriate ctl name prefix for the given line out channel */
1050 static const char *get_line_out_pfx(struct hda_codec
*codec
, int ch
,
1051 int *index
, int ctl_type
)
1053 struct hda_gen_spec
*spec
= codec
->spec
;
1054 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1057 if (cfg
->line_outs
== 1 && !spec
->multi_ios
&&
1058 !cfg
->hp_outs
&& !cfg
->speaker_outs
)
1059 return spec
->vmaster_mute
.hook
? "PCM" : "Master";
1061 /* if there is really a single DAC used in the whole output paths,
1062 * use it master (or "PCM" if a vmaster hook is present)
1064 if (spec
->multiout
.num_dacs
== 1 && !spec
->mixer_nid
&&
1065 !spec
->multiout
.hp_out_nid
[0] && !spec
->multiout
.extra_out_nid
[0])
1066 return spec
->vmaster_mute
.hook
? "PCM" : "Master";
1068 /* multi-io channels */
1069 if (ch
>= cfg
->line_outs
)
1070 return channel_name
[ch
];
1072 switch (cfg
->line_out_type
) {
1073 case AUTO_PIN_SPEAKER_OUT
:
1074 /* if the primary channel vol/mute is shared with HP volume,
1075 * don't name it as Speaker
1077 if (!ch
&& cfg
->hp_outs
&&
1078 !path_has_mixer(codec
, spec
->hp_paths
[0], ctl_type
))
1080 if (cfg
->line_outs
== 1)
1082 if (cfg
->line_outs
== 2)
1083 return ch
? "Bass Speaker" : "Speaker";
1085 case AUTO_PIN_HP_OUT
:
1086 /* if the primary channel vol/mute is shared with spk volume,
1087 * don't name it as Headphone
1089 if (!ch
&& cfg
->speaker_outs
&&
1090 !path_has_mixer(codec
, spec
->speaker_paths
[0], ctl_type
))
1092 /* for multi-io case, only the primary out */
1093 if (ch
&& spec
->multi_ios
)
1097 case AUTO_PIN_LINE_OUT
:
1098 /* This deals with the case where we have two DACs and
1099 * one LO, one HP and one Speaker */
1100 if (!ch
&& cfg
->speaker_outs
&& cfg
->hp_outs
) {
1101 bool hp_lo_shared
= !path_has_mixer(codec
, spec
->hp_paths
[0], ctl_type
);
1102 bool spk_lo_shared
= !path_has_mixer(codec
, spec
->speaker_paths
[0], ctl_type
);
1103 if (hp_lo_shared
&& spk_lo_shared
)
1104 return spec
->vmaster_mute
.hook
? "PCM" : "Master";
1106 return "Headphone+LO";
1108 return "Speaker+LO";
1112 /* for a single channel output, we don't have to name the channel */
1113 if (cfg
->line_outs
== 1 && !spec
->multi_ios
)
1116 if (ch
>= ARRAY_SIZE(channel_name
)) {
1121 return channel_name
[ch
];
1125 * Parse output paths
1128 /* badness definition */
1130 /* No primary DAC is found for the main output */
1131 BAD_NO_PRIMARY_DAC
= 0x10000,
1132 /* No DAC is found for the extra output */
1133 BAD_NO_DAC
= 0x4000,
1134 /* No possible multi-ios */
1135 BAD_MULTI_IO
= 0x120,
1136 /* No individual DAC for extra output */
1137 BAD_NO_EXTRA_DAC
= 0x102,
1138 /* No individual DAC for extra surrounds */
1139 BAD_NO_EXTRA_SURR_DAC
= 0x101,
1140 /* Primary DAC shared with main surrounds */
1141 BAD_SHARED_SURROUND
= 0x100,
1142 /* No independent HP possible */
1143 BAD_NO_INDEP_HP
= 0x10,
1144 /* Primary DAC shared with main CLFE */
1145 BAD_SHARED_CLFE
= 0x10,
1146 /* Primary DAC shared with extra surrounds */
1147 BAD_SHARED_EXTRA_SURROUND
= 0x10,
1148 /* Volume widget is shared */
1149 BAD_SHARED_VOL
= 0x10,
1152 /* look for widgets in the given path which are appropriate for
1153 * volume and mute controls, and assign the values to ctls[].
1155 * When no appropriate widget is found in the path, the badness value
1156 * is incremented depending on the situation. The function returns the
1157 * total badness for both volume and mute controls.
1159 static int assign_out_path_ctls(struct hda_codec
*codec
, struct nid_path
*path
)
1161 struct hda_gen_spec
*spec
= codec
->spec
;
1167 return BAD_SHARED_VOL
* 2;
1169 if (path
->ctls
[NID_PATH_VOL_CTL
] ||
1170 path
->ctls
[NID_PATH_MUTE_CTL
])
1171 return 0; /* already evaluated */
1173 nid
= look_for_out_vol_nid(codec
, path
);
1175 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
1176 if (spec
->dac_min_mute
)
1177 val
|= HDA_AMP_VAL_MIN_MUTE
;
1178 if (is_ctl_used(codec
, val
, NID_PATH_VOL_CTL
))
1179 badness
+= BAD_SHARED_VOL
;
1181 path
->ctls
[NID_PATH_VOL_CTL
] = val
;
1183 badness
+= BAD_SHARED_VOL
;
1184 nid
= look_for_out_mute_nid(codec
, path
);
1186 unsigned int wid_type
= get_wcaps_type(get_wcaps(codec
, nid
));
1187 if (wid_type
== AC_WID_PIN
|| wid_type
== AC_WID_AUD_OUT
||
1188 nid_has_mute(codec
, nid
, HDA_OUTPUT
))
1189 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
1191 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_INPUT
);
1192 if (is_ctl_used(codec
, val
, NID_PATH_MUTE_CTL
))
1193 badness
+= BAD_SHARED_VOL
;
1195 path
->ctls
[NID_PATH_MUTE_CTL
] = val
;
1197 badness
+= BAD_SHARED_VOL
;
1201 const struct badness_table hda_main_out_badness
= {
1202 .no_primary_dac
= BAD_NO_PRIMARY_DAC
,
1203 .no_dac
= BAD_NO_DAC
,
1204 .shared_primary
= BAD_NO_PRIMARY_DAC
,
1205 .shared_surr
= BAD_SHARED_SURROUND
,
1206 .shared_clfe
= BAD_SHARED_CLFE
,
1207 .shared_surr_main
= BAD_SHARED_SURROUND
,
1209 EXPORT_SYMBOL_GPL(hda_main_out_badness
);
1211 const struct badness_table hda_extra_out_badness
= {
1212 .no_primary_dac
= BAD_NO_DAC
,
1213 .no_dac
= BAD_NO_DAC
,
1214 .shared_primary
= BAD_NO_EXTRA_DAC
,
1215 .shared_surr
= BAD_SHARED_EXTRA_SURROUND
,
1216 .shared_clfe
= BAD_SHARED_EXTRA_SURROUND
,
1217 .shared_surr_main
= BAD_NO_EXTRA_SURR_DAC
,
1219 EXPORT_SYMBOL_GPL(hda_extra_out_badness
);
1221 /* get the DAC of the primary output corresponding to the given array index */
1222 static hda_nid_t
get_primary_out(struct hda_codec
*codec
, int idx
)
1224 struct hda_gen_spec
*spec
= codec
->spec
;
1225 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1227 if (cfg
->line_outs
> idx
)
1228 return spec
->private_dac_nids
[idx
];
1229 idx
-= cfg
->line_outs
;
1230 if (spec
->multi_ios
> idx
)
1231 return spec
->multi_io
[idx
].dac
;
1235 /* return the DAC if it's reachable, otherwise zero */
1236 static inline hda_nid_t
try_dac(struct hda_codec
*codec
,
1237 hda_nid_t dac
, hda_nid_t pin
)
1239 return is_reachable_path(codec
, dac
, pin
) ? dac
: 0;
1242 /* try to assign DACs to pins and return the resultant badness */
1243 static int try_assign_dacs(struct hda_codec
*codec
, int num_outs
,
1244 const hda_nid_t
*pins
, hda_nid_t
*dacs
,
1246 const struct badness_table
*bad
)
1248 struct hda_gen_spec
*spec
= codec
->spec
;
1256 for (i
= 0; i
< num_outs
; i
++) {
1257 struct nid_path
*path
;
1258 hda_nid_t pin
= pins
[i
];
1260 path
= snd_hda_get_path_from_idx(codec
, path_idx
[i
]);
1262 badness
+= assign_out_path_ctls(codec
, path
);
1266 dacs
[i
] = get_preferred_dac(codec
, pin
);
1268 if (is_dac_already_used(codec
, dacs
[i
]))
1269 badness
+= bad
->shared_primary
;
1273 dacs
[i
] = look_for_dac(codec
, pin
, false);
1274 if (!dacs
[i
] && !i
) {
1275 /* try to steal the DAC of surrounds for the front */
1276 for (j
= 1; j
< num_outs
; j
++) {
1277 if (is_reachable_path(codec
, dacs
[j
], pin
)) {
1280 invalidate_nid_path(codec
, path_idx
[j
]);
1289 dac
= try_dac(codec
, get_primary_out(codec
, i
), pin
);
1291 dac
= try_dac(codec
, dacs
[0], pin
);
1293 dac
= try_dac(codec
, get_primary_out(codec
, i
), pin
);
1296 badness
+= bad
->shared_primary
;
1298 badness
+= bad
->shared_surr
;
1300 badness
+= bad
->shared_clfe
;
1301 } else if (is_reachable_path(codec
, spec
->private_dac_nids
[0], pin
)) {
1302 dac
= spec
->private_dac_nids
[0];
1303 badness
+= bad
->shared_surr_main
;
1305 badness
+= bad
->no_primary_dac
;
1307 badness
+= bad
->no_dac
;
1311 path
= snd_hda_add_new_path(codec
, dac
, pin
, -spec
->mixer_nid
);
1312 if (!path
&& !i
&& spec
->mixer_nid
) {
1313 /* try with aamix */
1314 path
= snd_hda_add_new_path(codec
, dac
, pin
, 0);
1318 badness
+= bad
->no_dac
;
1320 /* print_nid_path(codec, "output", path); */
1321 path
->active
= true;
1322 path_idx
[i
] = snd_hda_get_path_idx(codec
, path
);
1323 badness
+= assign_out_path_ctls(codec
, path
);
1330 /* return NID if the given pin has only a single connection to a certain DAC */
1331 static hda_nid_t
get_dac_if_single(struct hda_codec
*codec
, hda_nid_t pin
)
1333 struct hda_gen_spec
*spec
= codec
->spec
;
1335 hda_nid_t nid_found
= 0;
1337 for (i
= 0; i
< spec
->num_all_dacs
; i
++) {
1338 hda_nid_t nid
= spec
->all_dacs
[i
];
1339 if (!nid
|| is_dac_already_used(codec
, nid
))
1341 if (is_reachable_path(codec
, nid
, pin
)) {
1350 /* check whether the given pin can be a multi-io pin */
1351 static bool can_be_multiio_pin(struct hda_codec
*codec
,
1352 unsigned int location
, hda_nid_t nid
)
1354 unsigned int defcfg
, caps
;
1356 defcfg
= snd_hda_codec_get_pincfg(codec
, nid
);
1357 if (get_defcfg_connect(defcfg
) != AC_JACK_PORT_COMPLEX
)
1359 if (location
&& get_defcfg_location(defcfg
) != location
)
1361 caps
= snd_hda_query_pin_caps(codec
, nid
);
1362 if (!(caps
& AC_PINCAP_OUT
))
1367 /* count the number of input pins that are capable to be multi-io */
1368 static int count_multiio_pins(struct hda_codec
*codec
, hda_nid_t reference_pin
)
1370 struct hda_gen_spec
*spec
= codec
->spec
;
1371 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1372 unsigned int defcfg
= snd_hda_codec_get_pincfg(codec
, reference_pin
);
1373 unsigned int location
= get_defcfg_location(defcfg
);
1377 for (type
= AUTO_PIN_LINE_IN
; type
>= AUTO_PIN_MIC
; type
--) {
1378 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
1379 if (cfg
->inputs
[i
].type
!= type
)
1381 if (can_be_multiio_pin(codec
, location
,
1382 cfg
->inputs
[i
].pin
))
1392 * When hardwired is set, try to fill ony hardwired pins, and returns
1393 * zero if any pins are filled, non-zero if nothing found.
1394 * When hardwired is off, try to fill possible input pins, and returns
1395 * the badness value.
1397 static int fill_multi_ios(struct hda_codec
*codec
,
1398 hda_nid_t reference_pin
,
1401 struct hda_gen_spec
*spec
= codec
->spec
;
1402 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1403 int type
, i
, j
, num_pins
, old_pins
;
1404 unsigned int defcfg
= snd_hda_codec_get_pincfg(codec
, reference_pin
);
1405 unsigned int location
= get_defcfg_location(defcfg
);
1407 struct nid_path
*path
;
1409 old_pins
= spec
->multi_ios
;
1413 num_pins
= count_multiio_pins(codec
, reference_pin
);
1417 for (type
= AUTO_PIN_LINE_IN
; type
>= AUTO_PIN_MIC
; type
--) {
1418 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
1419 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
1422 if (cfg
->inputs
[i
].type
!= type
)
1424 if (!can_be_multiio_pin(codec
, location
, nid
))
1426 for (j
= 0; j
< spec
->multi_ios
; j
++) {
1427 if (nid
== spec
->multi_io
[j
].pin
)
1430 if (j
< spec
->multi_ios
)
1434 dac
= get_dac_if_single(codec
, nid
);
1436 dac
= look_for_dac(codec
, nid
, false);
1441 path
= snd_hda_add_new_path(codec
, dac
, nid
,
1447 /* print_nid_path(codec, "multiio", path); */
1448 spec
->multi_io
[spec
->multi_ios
].pin
= nid
;
1449 spec
->multi_io
[spec
->multi_ios
].dac
= dac
;
1450 spec
->out_paths
[cfg
->line_outs
+ spec
->multi_ios
] =
1451 snd_hda_get_path_idx(codec
, path
);
1453 if (spec
->multi_ios
>= 2)
1459 badness
= BAD_MULTI_IO
;
1460 if (old_pins
== spec
->multi_ios
) {
1462 return 1; /* nothing found */
1464 return badness
; /* no badness if nothing found */
1466 if (!hardwired
&& spec
->multi_ios
< 2) {
1467 /* cancel newly assigned paths */
1468 spec
->paths
.used
-= spec
->multi_ios
- old_pins
;
1469 spec
->multi_ios
= old_pins
;
1473 /* assign volume and mute controls */
1474 for (i
= old_pins
; i
< spec
->multi_ios
; i
++) {
1475 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[cfg
->line_outs
+ i
]);
1476 badness
+= assign_out_path_ctls(codec
, path
);
1482 /* map DACs for all pins in the list if they are single connections */
1483 static bool map_singles(struct hda_codec
*codec
, int outs
,
1484 const hda_nid_t
*pins
, hda_nid_t
*dacs
, int *path_idx
)
1486 struct hda_gen_spec
*spec
= codec
->spec
;
1489 for (i
= 0; i
< outs
; i
++) {
1490 struct nid_path
*path
;
1494 dac
= get_dac_if_single(codec
, pins
[i
]);
1497 path
= snd_hda_add_new_path(codec
, dac
, pins
[i
],
1499 if (!path
&& !i
&& spec
->mixer_nid
)
1500 path
= snd_hda_add_new_path(codec
, dac
, pins
[i
], 0);
1504 /* print_nid_path(codec, "output", path); */
1505 path
->active
= true;
1506 path_idx
[i
] = snd_hda_get_path_idx(codec
, path
);
1512 /* create a new path including aamix if available, and return its index */
1513 static int check_aamix_out_path(struct hda_codec
*codec
, int path_idx
)
1515 struct hda_gen_spec
*spec
= codec
->spec
;
1516 struct nid_path
*path
;
1517 hda_nid_t path_dac
, dac
, pin
;
1519 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
1520 if (!path
|| !path
->depth
||
1521 is_nid_contained(path
, spec
->mixer_nid
))
1523 path_dac
= path
->path
[0];
1524 dac
= spec
->private_dac_nids
[0];
1525 pin
= path
->path
[path
->depth
- 1];
1526 path
= snd_hda_add_new_path(codec
, dac
, pin
, spec
->mixer_nid
);
1528 if (dac
!= path_dac
)
1530 else if (spec
->multiout
.hp_out_nid
[0])
1531 dac
= spec
->multiout
.hp_out_nid
[0];
1532 else if (spec
->multiout
.extra_out_nid
[0])
1533 dac
= spec
->multiout
.extra_out_nid
[0];
1537 path
= snd_hda_add_new_path(codec
, dac
, pin
,
1542 /* print_nid_path(codec, "output-aamix", path); */
1543 path
->active
= false; /* unused as default */
1544 return snd_hda_get_path_idx(codec
, path
);
1547 /* check whether the independent HP is available with the current config */
1548 static bool indep_hp_possible(struct hda_codec
*codec
)
1550 struct hda_gen_spec
*spec
= codec
->spec
;
1551 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1552 struct nid_path
*path
;
1555 if (cfg
->line_out_type
== AUTO_PIN_HP_OUT
)
1556 idx
= spec
->out_paths
[0];
1558 idx
= spec
->hp_paths
[0];
1559 path
= snd_hda_get_path_from_idx(codec
, idx
);
1563 /* assume no path conflicts unless aamix is involved */
1564 if (!spec
->mixer_nid
|| !is_nid_contained(path
, spec
->mixer_nid
))
1567 /* check whether output paths contain aamix */
1568 for (i
= 0; i
< cfg
->line_outs
; i
++) {
1569 if (spec
->out_paths
[i
] == idx
)
1571 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[i
]);
1572 if (path
&& is_nid_contained(path
, spec
->mixer_nid
))
1575 for (i
= 0; i
< cfg
->speaker_outs
; i
++) {
1576 path
= snd_hda_get_path_from_idx(codec
, spec
->speaker_paths
[i
]);
1577 if (path
&& is_nid_contained(path
, spec
->mixer_nid
))
1584 /* fill the empty entries in the dac array for speaker/hp with the
1585 * shared dac pointed by the paths
1587 static void refill_shared_dacs(struct hda_codec
*codec
, int num_outs
,
1588 hda_nid_t
*dacs
, int *path_idx
)
1590 struct nid_path
*path
;
1593 for (i
= 0; i
< num_outs
; i
++) {
1596 path
= snd_hda_get_path_from_idx(codec
, path_idx
[i
]);
1599 dacs
[i
] = path
->path
[0];
1603 /* fill in the dac_nids table from the parsed pin configuration */
1604 static int fill_and_eval_dacs(struct hda_codec
*codec
,
1605 bool fill_hardwired
,
1606 bool fill_mio_first
)
1608 struct hda_gen_spec
*spec
= codec
->spec
;
1609 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1610 int i
, err
, badness
;
1612 /* set num_dacs once to full for look_for_dac() */
1613 spec
->multiout
.num_dacs
= cfg
->line_outs
;
1614 spec
->multiout
.dac_nids
= spec
->private_dac_nids
;
1615 memset(spec
->private_dac_nids
, 0, sizeof(spec
->private_dac_nids
));
1616 memset(spec
->multiout
.hp_out_nid
, 0, sizeof(spec
->multiout
.hp_out_nid
));
1617 memset(spec
->multiout
.extra_out_nid
, 0, sizeof(spec
->multiout
.extra_out_nid
));
1618 spec
->multi_ios
= 0;
1619 snd_array_free(&spec
->paths
);
1621 /* clear path indices */
1622 memset(spec
->out_paths
, 0, sizeof(spec
->out_paths
));
1623 memset(spec
->hp_paths
, 0, sizeof(spec
->hp_paths
));
1624 memset(spec
->speaker_paths
, 0, sizeof(spec
->speaker_paths
));
1625 memset(spec
->aamix_out_paths
, 0, sizeof(spec
->aamix_out_paths
));
1626 memset(spec
->digout_paths
, 0, sizeof(spec
->digout_paths
));
1627 memset(spec
->input_paths
, 0, sizeof(spec
->input_paths
));
1628 memset(spec
->loopback_paths
, 0, sizeof(spec
->loopback_paths
));
1629 memset(&spec
->digin_path
, 0, sizeof(spec
->digin_path
));
1633 /* fill hard-wired DACs first */
1634 if (fill_hardwired
) {
1637 mapped
= map_singles(codec
, cfg
->line_outs
,
1639 spec
->private_dac_nids
,
1641 mapped
|= map_singles(codec
, cfg
->hp_outs
,
1643 spec
->multiout
.hp_out_nid
,
1645 mapped
|= map_singles(codec
, cfg
->speaker_outs
,
1647 spec
->multiout
.extra_out_nid
,
1648 spec
->speaker_paths
);
1649 if (!spec
->no_multi_io
&&
1650 fill_mio_first
&& cfg
->line_outs
== 1 &&
1651 cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1652 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], true);
1659 badness
+= try_assign_dacs(codec
, cfg
->line_outs
, cfg
->line_out_pins
,
1660 spec
->private_dac_nids
, spec
->out_paths
,
1661 spec
->main_out_badness
);
1663 if (!spec
->no_multi_io
&& fill_mio_first
&&
1664 cfg
->line_outs
== 1 && cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1665 /* try to fill multi-io first */
1666 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], false);
1669 /* we don't count badness at this stage yet */
1672 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
) {
1673 err
= try_assign_dacs(codec
, cfg
->hp_outs
, cfg
->hp_pins
,
1674 spec
->multiout
.hp_out_nid
,
1676 spec
->extra_out_badness
);
1681 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1682 err
= try_assign_dacs(codec
, cfg
->speaker_outs
,
1684 spec
->multiout
.extra_out_nid
,
1685 spec
->speaker_paths
,
1686 spec
->extra_out_badness
);
1691 if (!spec
->no_multi_io
&&
1692 cfg
->line_outs
== 1 && cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1693 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], false);
1699 if (spec
->mixer_nid
) {
1700 spec
->aamix_out_paths
[0] =
1701 check_aamix_out_path(codec
, spec
->out_paths
[0]);
1702 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
1703 spec
->aamix_out_paths
[1] =
1704 check_aamix_out_path(codec
, spec
->hp_paths
[0]);
1705 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
1706 spec
->aamix_out_paths
[2] =
1707 check_aamix_out_path(codec
, spec
->speaker_paths
[0]);
1710 if (!spec
->no_multi_io
&&
1711 cfg
->hp_outs
&& cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
)
1712 if (count_multiio_pins(codec
, cfg
->hp_pins
[0]) >= 2)
1713 spec
->multi_ios
= 1; /* give badness */
1715 /* re-count num_dacs and squash invalid entries */
1716 spec
->multiout
.num_dacs
= 0;
1717 for (i
= 0; i
< cfg
->line_outs
; i
++) {
1718 if (spec
->private_dac_nids
[i
])
1719 spec
->multiout
.num_dacs
++;
1721 memmove(spec
->private_dac_nids
+ i
,
1722 spec
->private_dac_nids
+ i
+ 1,
1723 sizeof(hda_nid_t
) * (cfg
->line_outs
- i
- 1));
1724 spec
->private_dac_nids
[cfg
->line_outs
- 1] = 0;
1728 spec
->ext_channel_count
= spec
->min_channel_count
=
1729 spec
->multiout
.num_dacs
* 2;
1731 if (spec
->multi_ios
== 2) {
1732 for (i
= 0; i
< 2; i
++)
1733 spec
->private_dac_nids
[spec
->multiout
.num_dacs
++] =
1734 spec
->multi_io
[i
].dac
;
1735 } else if (spec
->multi_ios
) {
1736 spec
->multi_ios
= 0;
1737 badness
+= BAD_MULTI_IO
;
1740 if (spec
->indep_hp
&& !indep_hp_possible(codec
))
1741 badness
+= BAD_NO_INDEP_HP
;
1743 /* re-fill the shared DAC for speaker / headphone */
1744 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
1745 refill_shared_dacs(codec
, cfg
->hp_outs
,
1746 spec
->multiout
.hp_out_nid
,
1748 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
1749 refill_shared_dacs(codec
, cfg
->speaker_outs
,
1750 spec
->multiout
.extra_out_nid
,
1751 spec
->speaker_paths
);
1756 #define DEBUG_BADNESS
1758 #ifdef DEBUG_BADNESS
1759 #define debug_badness(fmt, ...) \
1760 codec_dbg(codec, fmt, ##__VA_ARGS__)
1762 #define debug_badness(fmt, ...) \
1763 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1766 #ifdef DEBUG_BADNESS
1767 static inline void print_nid_path_idx(struct hda_codec
*codec
,
1768 const char *pfx
, int idx
)
1770 struct nid_path
*path
;
1772 path
= snd_hda_get_path_from_idx(codec
, idx
);
1774 print_nid_path(codec
, pfx
, path
);
1777 static void debug_show_configs(struct hda_codec
*codec
,
1778 struct auto_pin_cfg
*cfg
)
1780 struct hda_gen_spec
*spec
= codec
->spec
;
1781 static const char * const lo_type
[3] = { "LO", "SP", "HP" };
1784 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1785 cfg
->line_out_pins
[0], cfg
->line_out_pins
[1],
1786 cfg
->line_out_pins
[2], cfg
->line_out_pins
[3],
1787 spec
->multiout
.dac_nids
[0],
1788 spec
->multiout
.dac_nids
[1],
1789 spec
->multiout
.dac_nids
[2],
1790 spec
->multiout
.dac_nids
[3],
1791 lo_type
[cfg
->line_out_type
]);
1792 for (i
= 0; i
< cfg
->line_outs
; i
++)
1793 print_nid_path_idx(codec
, " out", spec
->out_paths
[i
]);
1794 if (spec
->multi_ios
> 0)
1795 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1797 spec
->multi_io
[0].pin
, spec
->multi_io
[1].pin
,
1798 spec
->multi_io
[0].dac
, spec
->multi_io
[1].dac
);
1799 for (i
= 0; i
< spec
->multi_ios
; i
++)
1800 print_nid_path_idx(codec
, " mio",
1801 spec
->out_paths
[cfg
->line_outs
+ i
]);
1803 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1804 cfg
->hp_pins
[0], cfg
->hp_pins
[1],
1805 cfg
->hp_pins
[2], cfg
->hp_pins
[3],
1806 spec
->multiout
.hp_out_nid
[0],
1807 spec
->multiout
.hp_out_nid
[1],
1808 spec
->multiout
.hp_out_nid
[2],
1809 spec
->multiout
.hp_out_nid
[3]);
1810 for (i
= 0; i
< cfg
->hp_outs
; i
++)
1811 print_nid_path_idx(codec
, " hp ", spec
->hp_paths
[i
]);
1812 if (cfg
->speaker_outs
)
1813 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1814 cfg
->speaker_pins
[0], cfg
->speaker_pins
[1],
1815 cfg
->speaker_pins
[2], cfg
->speaker_pins
[3],
1816 spec
->multiout
.extra_out_nid
[0],
1817 spec
->multiout
.extra_out_nid
[1],
1818 spec
->multiout
.extra_out_nid
[2],
1819 spec
->multiout
.extra_out_nid
[3]);
1820 for (i
= 0; i
< cfg
->speaker_outs
; i
++)
1821 print_nid_path_idx(codec
, " spk", spec
->speaker_paths
[i
]);
1822 for (i
= 0; i
< 3; i
++)
1823 print_nid_path_idx(codec
, " mix", spec
->aamix_out_paths
[i
]);
1826 #define debug_show_configs(codec, cfg) /* NOP */
1829 /* find all available DACs of the codec */
1830 static void fill_all_dac_nids(struct hda_codec
*codec
)
1832 struct hda_gen_spec
*spec
= codec
->spec
;
1834 hda_nid_t nid
= codec
->start_nid
;
1836 spec
->num_all_dacs
= 0;
1837 memset(spec
->all_dacs
, 0, sizeof(spec
->all_dacs
));
1838 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
1839 if (get_wcaps_type(get_wcaps(codec
, nid
)) != AC_WID_AUD_OUT
)
1841 if (spec
->num_all_dacs
>= ARRAY_SIZE(spec
->all_dacs
)) {
1842 codec_err(codec
, "Too many DACs!\n");
1845 spec
->all_dacs
[spec
->num_all_dacs
++] = nid
;
1849 static int parse_output_paths(struct hda_codec
*codec
)
1851 struct hda_gen_spec
*spec
= codec
->spec
;
1852 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1853 struct auto_pin_cfg
*best_cfg
;
1855 int best_badness
= INT_MAX
;
1857 bool fill_hardwired
= true, fill_mio_first
= true;
1858 bool best_wired
= true, best_mio
= true;
1859 bool hp_spk_swapped
= false;
1861 best_cfg
= kmalloc(sizeof(*best_cfg
), GFP_KERNEL
);
1867 badness
= fill_and_eval_dacs(codec
, fill_hardwired
,
1873 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1874 cfg
->line_out_type
, fill_hardwired
, fill_mio_first
,
1876 debug_show_configs(codec
, cfg
);
1877 if (badness
< best_badness
) {
1878 best_badness
= badness
;
1880 best_wired
= fill_hardwired
;
1881 best_mio
= fill_mio_first
;
1885 fill_mio_first
= !fill_mio_first
;
1886 if (!fill_mio_first
)
1888 fill_hardwired
= !fill_hardwired
;
1889 if (!fill_hardwired
)
1893 hp_spk_swapped
= true;
1894 if (cfg
->speaker_outs
> 0 &&
1895 cfg
->line_out_type
== AUTO_PIN_HP_OUT
) {
1896 cfg
->hp_outs
= cfg
->line_outs
;
1897 memcpy(cfg
->hp_pins
, cfg
->line_out_pins
,
1898 sizeof(cfg
->hp_pins
));
1899 cfg
->line_outs
= cfg
->speaker_outs
;
1900 memcpy(cfg
->line_out_pins
, cfg
->speaker_pins
,
1901 sizeof(cfg
->speaker_pins
));
1902 cfg
->speaker_outs
= 0;
1903 memset(cfg
->speaker_pins
, 0, sizeof(cfg
->speaker_pins
));
1904 cfg
->line_out_type
= AUTO_PIN_SPEAKER_OUT
;
1905 fill_hardwired
= true;
1908 if (cfg
->hp_outs
> 0 &&
1909 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
) {
1910 cfg
->speaker_outs
= cfg
->line_outs
;
1911 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
1912 sizeof(cfg
->speaker_pins
));
1913 cfg
->line_outs
= cfg
->hp_outs
;
1914 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
,
1915 sizeof(cfg
->hp_pins
));
1917 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
1918 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
1919 fill_hardwired
= true;
1926 debug_badness("==> restoring best_cfg\n");
1928 fill_and_eval_dacs(codec
, best_wired
, best_mio
);
1930 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1931 cfg
->line_out_type
, best_wired
, best_mio
);
1932 debug_show_configs(codec
, cfg
);
1934 if (cfg
->line_out_pins
[0]) {
1935 struct nid_path
*path
;
1936 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[0]);
1938 spec
->vmaster_nid
= look_for_out_vol_nid(codec
, path
);
1939 if (spec
->vmaster_nid
) {
1940 snd_hda_set_vmaster_tlv(codec
, spec
->vmaster_nid
,
1941 HDA_OUTPUT
, spec
->vmaster_tlv
);
1942 if (spec
->dac_min_mute
)
1943 spec
->vmaster_tlv
[3] |= TLV_DB_SCALE_MUTE
;
1947 /* set initial pinctl targets */
1948 if (spec
->prefer_hp_amp
|| cfg
->line_out_type
== AUTO_PIN_HP_OUT
)
1952 set_pin_targets(codec
, cfg
->line_outs
, cfg
->line_out_pins
, val
);
1953 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
1954 set_pin_targets(codec
, cfg
->hp_outs
, cfg
->hp_pins
, PIN_HP
);
1955 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1956 val
= spec
->prefer_hp_amp
? PIN_HP
: PIN_OUT
;
1957 set_pin_targets(codec
, cfg
->speaker_outs
,
1958 cfg
->speaker_pins
, val
);
1961 /* clear indep_hp flag if not available */
1962 if (spec
->indep_hp
&& !indep_hp_possible(codec
))
1969 /* add playback controls from the parsed DAC table */
1970 static int create_multi_out_ctls(struct hda_codec
*codec
,
1971 const struct auto_pin_cfg
*cfg
)
1973 struct hda_gen_spec
*spec
= codec
->spec
;
1974 int i
, err
, noutputs
;
1976 noutputs
= cfg
->line_outs
;
1977 if (spec
->multi_ios
> 0 && cfg
->line_outs
< 3)
1978 noutputs
+= spec
->multi_ios
;
1980 for (i
= 0; i
< noutputs
; i
++) {
1983 struct nid_path
*path
;
1985 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[i
]);
1989 name
= get_line_out_pfx(codec
, i
, &index
, NID_PATH_VOL_CTL
);
1990 if (!name
|| !strcmp(name
, "CLFE")) {
1992 err
= add_vol_ctl(codec
, "Center", 0, 1, path
);
1995 err
= add_vol_ctl(codec
, "LFE", 0, 2, path
);
1999 err
= add_stereo_vol(codec
, name
, index
, path
);
2004 name
= get_line_out_pfx(codec
, i
, &index
, NID_PATH_MUTE_CTL
);
2005 if (!name
|| !strcmp(name
, "CLFE")) {
2006 err
= add_sw_ctl(codec
, "Center", 0, 1, path
);
2009 err
= add_sw_ctl(codec
, "LFE", 0, 2, path
);
2013 err
= add_stereo_sw(codec
, name
, index
, path
);
2021 static int create_extra_out(struct hda_codec
*codec
, int path_idx
,
2022 const char *pfx
, int cidx
)
2024 struct nid_path
*path
;
2027 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
2030 err
= add_stereo_vol(codec
, pfx
, cidx
, path
);
2033 err
= add_stereo_sw(codec
, pfx
, cidx
, path
);
2039 /* add playback controls for speaker and HP outputs */
2040 static int create_extra_outs(struct hda_codec
*codec
, int num_pins
,
2041 const int *paths
, const char *pfx
)
2045 for (i
= 0; i
< num_pins
; i
++) {
2047 char tmp
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
2050 if (num_pins
== 2 && i
== 1 && !strcmp(pfx
, "Speaker"))
2051 name
= "Bass Speaker";
2052 else if (num_pins
>= 3) {
2053 snprintf(tmp
, sizeof(tmp
), "%s %s",
2054 pfx
, channel_name
[i
]);
2060 err
= create_extra_out(codec
, paths
[i
], name
, idx
);
2067 static int create_hp_out_ctls(struct hda_codec
*codec
)
2069 struct hda_gen_spec
*spec
= codec
->spec
;
2070 return create_extra_outs(codec
, spec
->autocfg
.hp_outs
,
2075 static int create_speaker_out_ctls(struct hda_codec
*codec
)
2077 struct hda_gen_spec
*spec
= codec
->spec
;
2078 return create_extra_outs(codec
, spec
->autocfg
.speaker_outs
,
2079 spec
->speaker_paths
,
2084 * independent HP controls
2087 static void call_hp_automute(struct hda_codec
*codec
,
2088 struct hda_jack_callback
*jack
);
2089 static int indep_hp_info(struct snd_kcontrol
*kcontrol
,
2090 struct snd_ctl_elem_info
*uinfo
)
2092 return snd_hda_enum_bool_helper_info(kcontrol
, uinfo
);
2095 static int indep_hp_get(struct snd_kcontrol
*kcontrol
,
2096 struct snd_ctl_elem_value
*ucontrol
)
2098 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2099 struct hda_gen_spec
*spec
= codec
->spec
;
2100 ucontrol
->value
.enumerated
.item
[0] = spec
->indep_hp_enabled
;
2104 static void update_aamix_paths(struct hda_codec
*codec
, bool do_mix
,
2105 int nomix_path_idx
, int mix_path_idx
,
2108 static int indep_hp_put(struct snd_kcontrol
*kcontrol
,
2109 struct snd_ctl_elem_value
*ucontrol
)
2111 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2112 struct hda_gen_spec
*spec
= codec
->spec
;
2113 unsigned int select
= ucontrol
->value
.enumerated
.item
[0];
2116 mutex_lock(&spec
->pcm_mutex
);
2117 if (spec
->active_streams
) {
2122 if (spec
->indep_hp_enabled
!= select
) {
2124 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
2125 dacp
= &spec
->private_dac_nids
[0];
2127 dacp
= &spec
->multiout
.hp_out_nid
[0];
2129 /* update HP aamix paths in case it conflicts with indep HP */
2130 if (spec
->have_aamix_ctl
) {
2131 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
2132 update_aamix_paths(codec
, spec
->aamix_mode
,
2134 spec
->aamix_out_paths
[0],
2135 spec
->autocfg
.line_out_type
);
2137 update_aamix_paths(codec
, spec
->aamix_mode
,
2139 spec
->aamix_out_paths
[1],
2143 spec
->indep_hp_enabled
= select
;
2144 if (spec
->indep_hp_enabled
)
2147 *dacp
= spec
->alt_dac_nid
;
2149 call_hp_automute(codec
, NULL
);
2153 mutex_unlock(&spec
->pcm_mutex
);
2157 static const struct snd_kcontrol_new indep_hp_ctl
= {
2158 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2159 .name
= "Independent HP",
2160 .info
= indep_hp_info
,
2161 .get
= indep_hp_get
,
2162 .put
= indep_hp_put
,
2166 static int create_indep_hp_ctls(struct hda_codec
*codec
)
2168 struct hda_gen_spec
*spec
= codec
->spec
;
2171 if (!spec
->indep_hp
)
2173 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
2174 dac
= spec
->multiout
.dac_nids
[0];
2176 dac
= spec
->multiout
.hp_out_nid
[0];
2182 spec
->indep_hp_enabled
= false;
2183 spec
->alt_dac_nid
= dac
;
2184 if (!snd_hda_gen_add_kctl(spec
, NULL
, &indep_hp_ctl
))
2190 * channel mode enum control
2193 static int ch_mode_info(struct snd_kcontrol
*kcontrol
,
2194 struct snd_ctl_elem_info
*uinfo
)
2196 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2197 struct hda_gen_spec
*spec
= codec
->spec
;
2200 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2202 uinfo
->value
.enumerated
.items
= spec
->multi_ios
+ 1;
2203 if (uinfo
->value
.enumerated
.item
> spec
->multi_ios
)
2204 uinfo
->value
.enumerated
.item
= spec
->multi_ios
;
2205 chs
= uinfo
->value
.enumerated
.item
* 2 + spec
->min_channel_count
;
2206 sprintf(uinfo
->value
.enumerated
.name
, "%dch", chs
);
2210 static int ch_mode_get(struct snd_kcontrol
*kcontrol
,
2211 struct snd_ctl_elem_value
*ucontrol
)
2213 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2214 struct hda_gen_spec
*spec
= codec
->spec
;
2215 ucontrol
->value
.enumerated
.item
[0] =
2216 (spec
->ext_channel_count
- spec
->min_channel_count
) / 2;
2220 static inline struct nid_path
*
2221 get_multiio_path(struct hda_codec
*codec
, int idx
)
2223 struct hda_gen_spec
*spec
= codec
->spec
;
2224 return snd_hda_get_path_from_idx(codec
,
2225 spec
->out_paths
[spec
->autocfg
.line_outs
+ idx
]);
2228 static void update_automute_all(struct hda_codec
*codec
);
2230 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2231 * used for output paths
2233 static bool aamix_default(struct hda_gen_spec
*spec
)
2235 return !spec
->have_aamix_ctl
|| spec
->aamix_mode
;
2238 static int set_multi_io(struct hda_codec
*codec
, int idx
, bool output
)
2240 struct hda_gen_spec
*spec
= codec
->spec
;
2241 hda_nid_t nid
= spec
->multi_io
[idx
].pin
;
2242 struct nid_path
*path
;
2244 path
= get_multiio_path(codec
, idx
);
2248 if (path
->active
== output
)
2252 set_pin_target(codec
, nid
, PIN_OUT
, true);
2253 snd_hda_activate_path(codec
, path
, true, aamix_default(spec
));
2254 set_pin_eapd(codec
, nid
, true);
2256 set_pin_eapd(codec
, nid
, false);
2257 snd_hda_activate_path(codec
, path
, false, aamix_default(spec
));
2258 set_pin_target(codec
, nid
, spec
->multi_io
[idx
].ctl_in
, true);
2259 path_power_down_sync(codec
, path
);
2262 /* update jack retasking in case it modifies any of them */
2263 update_automute_all(codec
);
2268 static int ch_mode_put(struct snd_kcontrol
*kcontrol
,
2269 struct snd_ctl_elem_value
*ucontrol
)
2271 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2272 struct hda_gen_spec
*spec
= codec
->spec
;
2275 ch
= ucontrol
->value
.enumerated
.item
[0];
2276 if (ch
< 0 || ch
> spec
->multi_ios
)
2278 if (ch
== (spec
->ext_channel_count
- spec
->min_channel_count
) / 2)
2280 spec
->ext_channel_count
= ch
* 2 + spec
->min_channel_count
;
2281 for (i
= 0; i
< spec
->multi_ios
; i
++)
2282 set_multi_io(codec
, i
, i
< ch
);
2283 spec
->multiout
.max_channels
= max(spec
->ext_channel_count
,
2284 spec
->const_channel_count
);
2285 if (spec
->need_dac_fix
)
2286 spec
->multiout
.num_dacs
= spec
->multiout
.max_channels
/ 2;
2290 static const struct snd_kcontrol_new channel_mode_enum
= {
2291 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2292 .name
= "Channel Mode",
2293 .info
= ch_mode_info
,
2298 static int create_multi_channel_mode(struct hda_codec
*codec
)
2300 struct hda_gen_spec
*spec
= codec
->spec
;
2302 if (spec
->multi_ios
> 0) {
2303 if (!snd_hda_gen_add_kctl(spec
, NULL
, &channel_mode_enum
))
2310 * aamix loopback enable/disable switch
2313 #define loopback_mixing_info indep_hp_info
2315 static int loopback_mixing_get(struct snd_kcontrol
*kcontrol
,
2316 struct snd_ctl_elem_value
*ucontrol
)
2318 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2319 struct hda_gen_spec
*spec
= codec
->spec
;
2320 ucontrol
->value
.enumerated
.item
[0] = spec
->aamix_mode
;
2324 static void update_aamix_paths(struct hda_codec
*codec
, bool do_mix
,
2325 int nomix_path_idx
, int mix_path_idx
,
2328 struct hda_gen_spec
*spec
= codec
->spec
;
2329 struct nid_path
*nomix_path
, *mix_path
;
2331 nomix_path
= snd_hda_get_path_from_idx(codec
, nomix_path_idx
);
2332 mix_path
= snd_hda_get_path_from_idx(codec
, mix_path_idx
);
2333 if (!nomix_path
|| !mix_path
)
2336 /* if HP aamix path is driven from a different DAC and the
2337 * independent HP mode is ON, can't turn on aamix path
2339 if (out_type
== AUTO_PIN_HP_OUT
&& spec
->indep_hp_enabled
&&
2340 mix_path
->path
[0] != spec
->alt_dac_nid
)
2344 snd_hda_activate_path(codec
, nomix_path
, false, true);
2345 snd_hda_activate_path(codec
, mix_path
, true, true);
2346 path_power_down_sync(codec
, nomix_path
);
2348 snd_hda_activate_path(codec
, mix_path
, false, false);
2349 snd_hda_activate_path(codec
, nomix_path
, true, false);
2350 path_power_down_sync(codec
, mix_path
);
2354 static int loopback_mixing_put(struct snd_kcontrol
*kcontrol
,
2355 struct snd_ctl_elem_value
*ucontrol
)
2357 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2358 struct hda_gen_spec
*spec
= codec
->spec
;
2359 unsigned int val
= ucontrol
->value
.enumerated
.item
[0];
2361 if (val
== spec
->aamix_mode
)
2363 spec
->aamix_mode
= val
;
2364 update_aamix_paths(codec
, val
, spec
->out_paths
[0],
2365 spec
->aamix_out_paths
[0],
2366 spec
->autocfg
.line_out_type
);
2367 update_aamix_paths(codec
, val
, spec
->hp_paths
[0],
2368 spec
->aamix_out_paths
[1],
2370 update_aamix_paths(codec
, val
, spec
->speaker_paths
[0],
2371 spec
->aamix_out_paths
[2],
2372 AUTO_PIN_SPEAKER_OUT
);
2376 static const struct snd_kcontrol_new loopback_mixing_enum
= {
2377 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2378 .name
= "Loopback Mixing",
2379 .info
= loopback_mixing_info
,
2380 .get
= loopback_mixing_get
,
2381 .put
= loopback_mixing_put
,
2384 static int create_loopback_mixing_ctl(struct hda_codec
*codec
)
2386 struct hda_gen_spec
*spec
= codec
->spec
;
2388 if (!spec
->mixer_nid
)
2390 if (!(spec
->aamix_out_paths
[0] || spec
->aamix_out_paths
[1] ||
2391 spec
->aamix_out_paths
[2]))
2393 if (!snd_hda_gen_add_kctl(spec
, NULL
, &loopback_mixing_enum
))
2395 spec
->have_aamix_ctl
= 1;
2400 * shared headphone/mic handling
2403 static void call_update_outputs(struct hda_codec
*codec
);
2405 /* for shared I/O, change the pin-control accordingly */
2406 static void update_hp_mic(struct hda_codec
*codec
, int adc_mux
, bool force
)
2408 struct hda_gen_spec
*spec
= codec
->spec
;
2413 pin
= spec
->hp_mic_pin
;
2414 as_mic
= spec
->cur_mux
[adc_mux
] == spec
->hp_mic_mux_idx
;
2417 val
= snd_hda_codec_get_pin_target(codec
, pin
);
2427 val
= snd_hda_get_default_vref(codec
, pin
);
2428 /* if the HP pin doesn't support VREF and the codec driver gives an
2429 * alternative pin, set up the VREF on that pin instead
2431 if (val
== AC_PINCTL_VREF_HIZ
&& spec
->shared_mic_vref_pin
) {
2432 const hda_nid_t vref_pin
= spec
->shared_mic_vref_pin
;
2433 unsigned int vref_val
= snd_hda_get_default_vref(codec
, vref_pin
);
2434 if (vref_val
!= AC_PINCTL_VREF_HIZ
)
2435 snd_hda_set_pin_ctl_cache(codec
, vref_pin
,
2436 PIN_IN
| (as_mic
? vref_val
: 0));
2439 if (!spec
->hp_mic_jack_modes
) {
2444 set_pin_target(codec
, pin
, val
, true);
2445 call_hp_automute(codec
, NULL
);
2449 /* create a shared input with the headphone out */
2450 static int create_hp_mic(struct hda_codec
*codec
)
2452 struct hda_gen_spec
*spec
= codec
->spec
;
2453 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
2454 unsigned int defcfg
;
2457 if (!spec
->hp_mic
) {
2458 if (spec
->suppress_hp_mic_detect
)
2460 /* automatic detection: only if no input or a single internal
2461 * input pin is found, try to detect the shared hp/mic
2463 if (cfg
->num_inputs
> 1)
2465 else if (cfg
->num_inputs
== 1) {
2466 defcfg
= snd_hda_codec_get_pincfg(codec
, cfg
->inputs
[0].pin
);
2467 if (snd_hda_get_input_pin_attr(defcfg
) != INPUT_PIN_ATTR_INT
)
2472 spec
->hp_mic
= 0; /* clear once */
2473 if (cfg
->num_inputs
>= AUTO_CFG_MAX_INS
)
2477 if (cfg
->line_out_type
== AUTO_PIN_HP_OUT
&& cfg
->line_outs
> 0)
2478 nid
= cfg
->line_out_pins
[0];
2479 else if (cfg
->hp_outs
> 0)
2480 nid
= cfg
->hp_pins
[0];
2484 if (!(snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_IN
))
2485 return 0; /* no input */
2487 cfg
->inputs
[cfg
->num_inputs
].pin
= nid
;
2488 cfg
->inputs
[cfg
->num_inputs
].type
= AUTO_PIN_MIC
;
2489 cfg
->inputs
[cfg
->num_inputs
].is_headphone_mic
= 1;
2492 spec
->hp_mic_pin
= nid
;
2493 /* we can't handle auto-mic together with HP-mic */
2494 spec
->suppress_auto_mic
= 1;
2495 codec_dbg(codec
, "Enable shared I/O jack on NID 0x%x\n", nid
);
2503 static int create_hp_mic_jack_mode(struct hda_codec
*codec
, hda_nid_t pin
);
2505 static const char * const out_jack_texts
[] = {
2506 "Line Out", "Headphone Out",
2509 static int out_jack_mode_info(struct snd_kcontrol
*kcontrol
,
2510 struct snd_ctl_elem_info
*uinfo
)
2512 return snd_hda_enum_helper_info(kcontrol
, uinfo
, 2, out_jack_texts
);
2515 static int out_jack_mode_get(struct snd_kcontrol
*kcontrol
,
2516 struct snd_ctl_elem_value
*ucontrol
)
2518 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2519 hda_nid_t nid
= kcontrol
->private_value
;
2520 if (snd_hda_codec_get_pin_target(codec
, nid
) == PIN_HP
)
2521 ucontrol
->value
.enumerated
.item
[0] = 1;
2523 ucontrol
->value
.enumerated
.item
[0] = 0;
2527 static int out_jack_mode_put(struct snd_kcontrol
*kcontrol
,
2528 struct snd_ctl_elem_value
*ucontrol
)
2530 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2531 hda_nid_t nid
= kcontrol
->private_value
;
2534 val
= ucontrol
->value
.enumerated
.item
[0] ? PIN_HP
: PIN_OUT
;
2535 if (snd_hda_codec_get_pin_target(codec
, nid
) == val
)
2537 snd_hda_set_pin_ctl_cache(codec
, nid
, val
);
2541 static const struct snd_kcontrol_new out_jack_mode_enum
= {
2542 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2543 .info
= out_jack_mode_info
,
2544 .get
= out_jack_mode_get
,
2545 .put
= out_jack_mode_put
,
2548 static bool find_kctl_name(struct hda_codec
*codec
, const char *name
, int idx
)
2550 struct hda_gen_spec
*spec
= codec
->spec
;
2553 for (i
= 0; i
< spec
->kctls
.used
; i
++) {
2554 struct snd_kcontrol_new
*kctl
= snd_array_elem(&spec
->kctls
, i
);
2555 if (!strcmp(kctl
->name
, name
) && kctl
->index
== idx
)
2561 static void get_jack_mode_name(struct hda_codec
*codec
, hda_nid_t pin
,
2562 char *name
, size_t name_len
)
2564 struct hda_gen_spec
*spec
= codec
->spec
;
2567 snd_hda_get_pin_label(codec
, pin
, &spec
->autocfg
, name
, name_len
, &idx
);
2568 strlcat(name
, " Jack Mode", name_len
);
2570 for (; find_kctl_name(codec
, name
, idx
); idx
++)
2574 static int get_out_jack_num_items(struct hda_codec
*codec
, hda_nid_t pin
)
2576 struct hda_gen_spec
*spec
= codec
->spec
;
2577 if (spec
->add_jack_modes
) {
2578 unsigned int pincap
= snd_hda_query_pin_caps(codec
, pin
);
2579 if ((pincap
& AC_PINCAP_OUT
) && (pincap
& AC_PINCAP_HP_DRV
))
2585 static int create_out_jack_modes(struct hda_codec
*codec
, int num_pins
,
2588 struct hda_gen_spec
*spec
= codec
->spec
;
2591 for (i
= 0; i
< num_pins
; i
++) {
2592 hda_nid_t pin
= pins
[i
];
2593 if (pin
== spec
->hp_mic_pin
)
2595 if (get_out_jack_num_items(codec
, pin
) > 1) {
2596 struct snd_kcontrol_new
*knew
;
2597 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
2598 get_jack_mode_name(codec
, pin
, name
, sizeof(name
));
2599 knew
= snd_hda_gen_add_kctl(spec
, name
,
2600 &out_jack_mode_enum
);
2603 knew
->private_value
= pin
;
2614 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2617 static const char * const vref_texts
[NUM_VREFS
] = {
2618 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2619 "", "Mic 80pc Bias", "Mic 100pc Bias"
2622 static unsigned int get_vref_caps(struct hda_codec
*codec
, hda_nid_t pin
)
2624 unsigned int pincap
;
2626 pincap
= snd_hda_query_pin_caps(codec
, pin
);
2627 pincap
= (pincap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
2628 /* filter out unusual vrefs */
2629 pincap
&= ~(AC_PINCAP_VREF_GRD
| AC_PINCAP_VREF_100
);
2633 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2634 static int get_vref_idx(unsigned int vref_caps
, unsigned int item_idx
)
2636 unsigned int i
, n
= 0;
2638 for (i
= 0; i
< NUM_VREFS
; i
++) {
2639 if (vref_caps
& (1 << i
)) {
2648 /* convert back from the vref ctl index to the enum item index */
2649 static int cvt_from_vref_idx(unsigned int vref_caps
, unsigned int idx
)
2651 unsigned int i
, n
= 0;
2653 for (i
= 0; i
< NUM_VREFS
; i
++) {
2656 if (vref_caps
& (1 << i
))
2662 static int in_jack_mode_info(struct snd_kcontrol
*kcontrol
,
2663 struct snd_ctl_elem_info
*uinfo
)
2665 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2666 hda_nid_t nid
= kcontrol
->private_value
;
2667 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2669 snd_hda_enum_helper_info(kcontrol
, uinfo
, hweight32(vref_caps
),
2671 /* set the right text */
2672 strcpy(uinfo
->value
.enumerated
.name
,
2673 vref_texts
[get_vref_idx(vref_caps
, uinfo
->value
.enumerated
.item
)]);
2677 static int in_jack_mode_get(struct snd_kcontrol
*kcontrol
,
2678 struct snd_ctl_elem_value
*ucontrol
)
2680 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2681 hda_nid_t nid
= kcontrol
->private_value
;
2682 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2685 idx
= snd_hda_codec_get_pin_target(codec
, nid
) & AC_PINCTL_VREFEN
;
2686 ucontrol
->value
.enumerated
.item
[0] = cvt_from_vref_idx(vref_caps
, idx
);
2690 static int in_jack_mode_put(struct snd_kcontrol
*kcontrol
,
2691 struct snd_ctl_elem_value
*ucontrol
)
2693 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2694 hda_nid_t nid
= kcontrol
->private_value
;
2695 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2696 unsigned int val
, idx
;
2698 val
= snd_hda_codec_get_pin_target(codec
, nid
);
2699 idx
= cvt_from_vref_idx(vref_caps
, val
& AC_PINCTL_VREFEN
);
2700 if (idx
== ucontrol
->value
.enumerated
.item
[0])
2703 val
&= ~AC_PINCTL_VREFEN
;
2704 val
|= get_vref_idx(vref_caps
, ucontrol
->value
.enumerated
.item
[0]);
2705 snd_hda_set_pin_ctl_cache(codec
, nid
, val
);
2709 static const struct snd_kcontrol_new in_jack_mode_enum
= {
2710 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2711 .info
= in_jack_mode_info
,
2712 .get
= in_jack_mode_get
,
2713 .put
= in_jack_mode_put
,
2716 static int get_in_jack_num_items(struct hda_codec
*codec
, hda_nid_t pin
)
2718 struct hda_gen_spec
*spec
= codec
->spec
;
2720 if (spec
->add_jack_modes
)
2721 nitems
= hweight32(get_vref_caps(codec
, pin
));
2722 return nitems
? nitems
: 1;
2725 static int create_in_jack_mode(struct hda_codec
*codec
, hda_nid_t pin
)
2727 struct hda_gen_spec
*spec
= codec
->spec
;
2728 struct snd_kcontrol_new
*knew
;
2729 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
2730 unsigned int defcfg
;
2732 if (pin
== spec
->hp_mic_pin
)
2733 return 0; /* already done in create_out_jack_mode() */
2735 /* no jack mode for fixed pins */
2736 defcfg
= snd_hda_codec_get_pincfg(codec
, pin
);
2737 if (snd_hda_get_input_pin_attr(defcfg
) == INPUT_PIN_ATTR_INT
)
2740 /* no multiple vref caps? */
2741 if (get_in_jack_num_items(codec
, pin
) <= 1)
2744 get_jack_mode_name(codec
, pin
, name
, sizeof(name
));
2745 knew
= snd_hda_gen_add_kctl(spec
, name
, &in_jack_mode_enum
);
2748 knew
->private_value
= pin
;
2753 * HP/mic shared jack mode
2755 static int hp_mic_jack_mode_info(struct snd_kcontrol
*kcontrol
,
2756 struct snd_ctl_elem_info
*uinfo
)
2758 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2759 hda_nid_t nid
= kcontrol
->private_value
;
2760 int out_jacks
= get_out_jack_num_items(codec
, nid
);
2761 int in_jacks
= get_in_jack_num_items(codec
, nid
);
2762 const char *text
= NULL
;
2765 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2767 uinfo
->value
.enumerated
.items
= out_jacks
+ in_jacks
;
2768 if (uinfo
->value
.enumerated
.item
>= uinfo
->value
.enumerated
.items
)
2769 uinfo
->value
.enumerated
.item
= uinfo
->value
.enumerated
.items
- 1;
2770 idx
= uinfo
->value
.enumerated
.item
;
2771 if (idx
< out_jacks
) {
2773 text
= out_jack_texts
[idx
];
2775 text
= "Headphone Out";
2779 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2780 text
= vref_texts
[get_vref_idx(vref_caps
, idx
)];
2785 strcpy(uinfo
->value
.enumerated
.name
, text
);
2789 static int get_cur_hp_mic_jack_mode(struct hda_codec
*codec
, hda_nid_t nid
)
2791 int out_jacks
= get_out_jack_num_items(codec
, nid
);
2792 int in_jacks
= get_in_jack_num_items(codec
, nid
);
2793 unsigned int val
= snd_hda_codec_get_pin_target(codec
, nid
);
2796 if (val
& PIN_OUT
) {
2797 if (out_jacks
> 1 && val
== PIN_HP
)
2799 } else if (val
& PIN_IN
) {
2802 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2803 val
&= AC_PINCTL_VREFEN
;
2804 idx
+= cvt_from_vref_idx(vref_caps
, val
);
2810 static int hp_mic_jack_mode_get(struct snd_kcontrol
*kcontrol
,
2811 struct snd_ctl_elem_value
*ucontrol
)
2813 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2814 hda_nid_t nid
= kcontrol
->private_value
;
2815 ucontrol
->value
.enumerated
.item
[0] =
2816 get_cur_hp_mic_jack_mode(codec
, nid
);
2820 static int hp_mic_jack_mode_put(struct snd_kcontrol
*kcontrol
,
2821 struct snd_ctl_elem_value
*ucontrol
)
2823 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2824 hda_nid_t nid
= kcontrol
->private_value
;
2825 int out_jacks
= get_out_jack_num_items(codec
, nid
);
2826 int in_jacks
= get_in_jack_num_items(codec
, nid
);
2827 unsigned int val
, oldval
, idx
;
2829 oldval
= get_cur_hp_mic_jack_mode(codec
, nid
);
2830 idx
= ucontrol
->value
.enumerated
.item
[0];
2834 if (idx
< out_jacks
) {
2836 val
= idx
? PIN_HP
: PIN_OUT
;
2842 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2843 val
= snd_hda_codec_get_pin_target(codec
, nid
);
2844 val
&= ~(AC_PINCTL_VREFEN
| PIN_HP
);
2845 val
|= get_vref_idx(vref_caps
, idx
) | PIN_IN
;
2847 val
= snd_hda_get_default_vref(codec
, nid
) | PIN_IN
;
2849 snd_hda_set_pin_ctl_cache(codec
, nid
, val
);
2850 call_hp_automute(codec
, NULL
);
2855 static const struct snd_kcontrol_new hp_mic_jack_mode_enum
= {
2856 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2857 .info
= hp_mic_jack_mode_info
,
2858 .get
= hp_mic_jack_mode_get
,
2859 .put
= hp_mic_jack_mode_put
,
2862 static int create_hp_mic_jack_mode(struct hda_codec
*codec
, hda_nid_t pin
)
2864 struct hda_gen_spec
*spec
= codec
->spec
;
2865 struct snd_kcontrol_new
*knew
;
2867 knew
= snd_hda_gen_add_kctl(spec
, "Headphone Mic Jack Mode",
2868 &hp_mic_jack_mode_enum
);
2871 knew
->private_value
= pin
;
2872 spec
->hp_mic_jack_modes
= 1;
2880 /* add the powersave loopback-list entry */
2881 static int add_loopback_list(struct hda_gen_spec
*spec
, hda_nid_t mix
, int idx
)
2883 struct hda_amp_list
*list
;
2885 list
= snd_array_new(&spec
->loopback_list
);
2889 list
->dir
= HDA_INPUT
;
2891 spec
->loopback
.amplist
= spec
->loopback_list
.list
;
2895 /* return true if either a volume or a mute amp is found for the given
2896 * aamix path; the amp has to be either in the mixer node or its direct leaf
2898 static bool look_for_mix_leaf_ctls(struct hda_codec
*codec
, hda_nid_t mix_nid
,
2899 hda_nid_t pin
, unsigned int *mix_val
,
2900 unsigned int *mute_val
)
2903 const hda_nid_t
*list
;
2906 idx
= snd_hda_get_conn_index(codec
, mix_nid
, pin
, true);
2910 *mix_val
= *mute_val
= 0;
2911 if (nid_has_volume(codec
, mix_nid
, HDA_INPUT
))
2912 *mix_val
= HDA_COMPOSE_AMP_VAL(mix_nid
, 3, idx
, HDA_INPUT
);
2913 if (nid_has_mute(codec
, mix_nid
, HDA_INPUT
))
2914 *mute_val
= HDA_COMPOSE_AMP_VAL(mix_nid
, 3, idx
, HDA_INPUT
);
2915 if (*mix_val
&& *mute_val
)
2918 /* check leaf node */
2919 num_conns
= snd_hda_get_conn_list(codec
, mix_nid
, &list
);
2920 if (num_conns
< idx
)
2923 if (!*mix_val
&& nid_has_volume(codec
, nid
, HDA_OUTPUT
) &&
2924 !is_ctl_associated(codec
, nid
, HDA_OUTPUT
, 0, NID_PATH_VOL_CTL
))
2925 *mix_val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
2926 if (!*mute_val
&& nid_has_mute(codec
, nid
, HDA_OUTPUT
) &&
2927 !is_ctl_associated(codec
, nid
, HDA_OUTPUT
, 0, NID_PATH_MUTE_CTL
))
2928 *mute_val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
2930 return *mix_val
|| *mute_val
;
2933 /* create input playback/capture controls for the given pin */
2934 static int new_analog_input(struct hda_codec
*codec
, int input_idx
,
2935 hda_nid_t pin
, const char *ctlname
, int ctlidx
,
2938 struct hda_gen_spec
*spec
= codec
->spec
;
2939 struct nid_path
*path
;
2940 unsigned int mix_val
, mute_val
;
2943 if (!look_for_mix_leaf_ctls(codec
, mix_nid
, pin
, &mix_val
, &mute_val
))
2946 path
= snd_hda_add_new_path(codec
, pin
, mix_nid
, 0);
2949 print_nid_path(codec
, "loopback", path
);
2950 spec
->loopback_paths
[input_idx
] = snd_hda_get_path_idx(codec
, path
);
2952 idx
= path
->idx
[path
->depth
- 1];
2954 err
= __add_pb_vol_ctrl(spec
, HDA_CTL_WIDGET_VOL
, ctlname
, ctlidx
, mix_val
);
2957 path
->ctls
[NID_PATH_VOL_CTL
] = mix_val
;
2961 err
= __add_pb_sw_ctrl(spec
, HDA_CTL_WIDGET_MUTE
, ctlname
, ctlidx
, mute_val
);
2964 path
->ctls
[NID_PATH_MUTE_CTL
] = mute_val
;
2967 path
->active
= true;
2968 err
= add_loopback_list(spec
, mix_nid
, idx
);
2972 if (spec
->mixer_nid
!= spec
->mixer_merge_nid
&&
2973 !spec
->loopback_merge_path
) {
2974 path
= snd_hda_add_new_path(codec
, spec
->mixer_nid
,
2975 spec
->mixer_merge_nid
, 0);
2977 print_nid_path(codec
, "loopback-merge", path
);
2978 path
->active
= true;
2979 spec
->loopback_merge_path
=
2980 snd_hda_get_path_idx(codec
, path
);
2987 static int is_input_pin(struct hda_codec
*codec
, hda_nid_t nid
)
2989 unsigned int pincap
= snd_hda_query_pin_caps(codec
, nid
);
2990 return (pincap
& AC_PINCAP_IN
) != 0;
2993 /* Parse the codec tree and retrieve ADCs */
2994 static int fill_adc_nids(struct hda_codec
*codec
)
2996 struct hda_gen_spec
*spec
= codec
->spec
;
2998 hda_nid_t
*adc_nids
= spec
->adc_nids
;
2999 int max_nums
= ARRAY_SIZE(spec
->adc_nids
);
3002 nid
= codec
->start_nid
;
3003 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
3004 unsigned int caps
= get_wcaps(codec
, nid
);
3005 int type
= get_wcaps_type(caps
);
3007 if (type
!= AC_WID_AUD_IN
|| (caps
& AC_WCAP_DIGITAL
))
3009 adc_nids
[nums
] = nid
;
3010 if (++nums
>= max_nums
)
3013 spec
->num_adc_nids
= nums
;
3015 /* copy the detected ADCs to all_adcs[] */
3016 spec
->num_all_adcs
= nums
;
3017 memcpy(spec
->all_adcs
, spec
->adc_nids
, nums
* sizeof(hda_nid_t
));
3022 /* filter out invalid adc_nids that don't give all active input pins;
3023 * if needed, check whether dynamic ADC-switching is available
3025 static int check_dyn_adc_switch(struct hda_codec
*codec
)
3027 struct hda_gen_spec
*spec
= codec
->spec
;
3028 struct hda_input_mux
*imux
= &spec
->input_mux
;
3029 unsigned int ok_bits
;
3034 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
3035 for (i
= 0; i
< imux
->num_items
; i
++) {
3036 if (!spec
->input_paths
[i
][n
])
3039 if (i
>= imux
->num_items
) {
3040 ok_bits
|= (1 << n
);
3046 /* check whether ADC-switch is possible */
3047 for (i
= 0; i
< imux
->num_items
; i
++) {
3048 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
3049 if (spec
->input_paths
[i
][n
]) {
3050 spec
->dyn_adc_idx
[i
] = n
;
3056 codec_dbg(codec
, "enabling ADC switching\n");
3057 spec
->dyn_adc_switch
= 1;
3058 } else if (nums
!= spec
->num_adc_nids
) {
3059 /* shrink the invalid adcs and input paths */
3061 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
3062 if (!(ok_bits
& (1 << n
)))
3065 spec
->adc_nids
[nums
] = spec
->adc_nids
[n
];
3066 for (i
= 0; i
< imux
->num_items
; i
++) {
3067 invalidate_nid_path(codec
,
3068 spec
->input_paths
[i
][nums
]);
3069 spec
->input_paths
[i
][nums
] =
3070 spec
->input_paths
[i
][n
];
3075 spec
->num_adc_nids
= nums
;
3078 if (imux
->num_items
== 1 ||
3079 (imux
->num_items
== 2 && spec
->hp_mic
)) {
3080 codec_dbg(codec
, "reducing to a single ADC\n");
3081 spec
->num_adc_nids
= 1; /* reduce to a single ADC */
3084 /* single index for individual volumes ctls */
3085 if (!spec
->dyn_adc_switch
&& spec
->multi_cap_vol
)
3086 spec
->num_adc_nids
= 1;
3091 /* parse capture source paths from the given pin and create imux items */
3092 static int parse_capture_source(struct hda_codec
*codec
, hda_nid_t pin
,
3093 int cfg_idx
, int num_adcs
,
3094 const char *label
, int anchor
)
3096 struct hda_gen_spec
*spec
= codec
->spec
;
3097 struct hda_input_mux
*imux
= &spec
->input_mux
;
3098 int imux_idx
= imux
->num_items
;
3099 bool imux_added
= false;
3102 for (c
= 0; c
< num_adcs
; c
++) {
3103 struct nid_path
*path
;
3104 hda_nid_t adc
= spec
->adc_nids
[c
];
3106 if (!is_reachable_path(codec
, pin
, adc
))
3108 path
= snd_hda_add_new_path(codec
, pin
, adc
, anchor
);
3111 print_nid_path(codec
, "input", path
);
3112 spec
->input_paths
[imux_idx
][c
] =
3113 snd_hda_get_path_idx(codec
, path
);
3116 if (spec
->hp_mic_pin
== pin
)
3117 spec
->hp_mic_mux_idx
= imux
->num_items
;
3118 spec
->imux_pins
[imux
->num_items
] = pin
;
3119 snd_hda_add_imux_item(codec
, imux
, label
, cfg_idx
, NULL
);
3121 if (spec
->dyn_adc_switch
)
3122 spec
->dyn_adc_idx
[imux_idx
] = c
;
3130 * create playback/capture controls for input pins
3133 /* fill the label for each input at first */
3134 static int fill_input_pin_labels(struct hda_codec
*codec
)
3136 struct hda_gen_spec
*spec
= codec
->spec
;
3137 const struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3140 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
3141 hda_nid_t pin
= cfg
->inputs
[i
].pin
;
3145 if (!is_input_pin(codec
, pin
))
3148 label
= hda_get_autocfg_input_label(codec
, cfg
, i
);
3150 for (j
= i
- 1; j
>= 0; j
--) {
3151 if (spec
->input_labels
[j
] &&
3152 !strcmp(spec
->input_labels
[j
], label
)) {
3153 idx
= spec
->input_label_idxs
[j
] + 1;
3158 spec
->input_labels
[i
] = label
;
3159 spec
->input_label_idxs
[i
] = idx
;
3165 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3167 static int create_input_ctls(struct hda_codec
*codec
)
3169 struct hda_gen_spec
*spec
= codec
->spec
;
3170 const struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3171 hda_nid_t mixer
= spec
->mixer_nid
;
3176 num_adcs
= fill_adc_nids(codec
);
3180 err
= fill_input_pin_labels(codec
);
3184 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
3187 pin
= cfg
->inputs
[i
].pin
;
3188 if (!is_input_pin(codec
, pin
))
3192 if (cfg
->inputs
[i
].type
== AUTO_PIN_MIC
)
3193 val
|= snd_hda_get_default_vref(codec
, pin
);
3194 if (pin
!= spec
->hp_mic_pin
)
3195 set_pin_target(codec
, pin
, val
, false);
3198 if (is_reachable_path(codec
, pin
, mixer
)) {
3199 err
= new_analog_input(codec
, i
, pin
,
3200 spec
->input_labels
[i
],
3201 spec
->input_label_idxs
[i
],
3208 err
= parse_capture_source(codec
, pin
, i
, num_adcs
,
3209 spec
->input_labels
[i
], -mixer
);
3213 if (spec
->add_jack_modes
) {
3214 err
= create_in_jack_mode(codec
, pin
);
3220 /* add stereo mix when explicitly enabled via hint */
3221 if (mixer
&& spec
->add_stereo_mix_input
== HDA_HINT_STEREO_MIX_ENABLE
) {
3222 err
= parse_capture_source(codec
, mixer
, CFG_IDX_MIX
, num_adcs
,
3227 spec
->suppress_auto_mic
= 1;
3238 /* get the input path specified by the given adc and imux indices */
3239 static struct nid_path
*get_input_path(struct hda_codec
*codec
, int adc_idx
, int imux_idx
)
3241 struct hda_gen_spec
*spec
= codec
->spec
;
3242 if (imux_idx
< 0 || imux_idx
>= HDA_MAX_NUM_INPUTS
) {
3246 if (spec
->dyn_adc_switch
)
3247 adc_idx
= spec
->dyn_adc_idx
[imux_idx
];
3248 if (adc_idx
< 0 || adc_idx
>= AUTO_CFG_MAX_INS
) {
3252 return snd_hda_get_path_from_idx(codec
, spec
->input_paths
[imux_idx
][adc_idx
]);
3255 static int mux_select(struct hda_codec
*codec
, unsigned int adc_idx
,
3258 static int mux_enum_info(struct snd_kcontrol
*kcontrol
,
3259 struct snd_ctl_elem_info
*uinfo
)
3261 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3262 struct hda_gen_spec
*spec
= codec
->spec
;
3263 return snd_hda_input_mux_info(&spec
->input_mux
, uinfo
);
3266 static int mux_enum_get(struct snd_kcontrol
*kcontrol
,
3267 struct snd_ctl_elem_value
*ucontrol
)
3269 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3270 struct hda_gen_spec
*spec
= codec
->spec
;
3271 /* the ctls are created at once with multiple counts */
3272 unsigned int adc_idx
= snd_ctl_get_ioffidx(kcontrol
, &ucontrol
->id
);
3274 ucontrol
->value
.enumerated
.item
[0] = spec
->cur_mux
[adc_idx
];
3278 static int mux_enum_put(struct snd_kcontrol
*kcontrol
,
3279 struct snd_ctl_elem_value
*ucontrol
)
3281 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3282 unsigned int adc_idx
= snd_ctl_get_ioffidx(kcontrol
, &ucontrol
->id
);
3283 return mux_select(codec
, adc_idx
,
3284 ucontrol
->value
.enumerated
.item
[0]);
3287 static const struct snd_kcontrol_new cap_src_temp
= {
3288 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3289 .name
= "Input Source",
3290 .info
= mux_enum_info
,
3291 .get
= mux_enum_get
,
3292 .put
= mux_enum_put
,
3296 * capture volume and capture switch ctls
3299 typedef int (*put_call_t
)(struct snd_kcontrol
*kcontrol
,
3300 struct snd_ctl_elem_value
*ucontrol
);
3302 /* call the given amp update function for all amps in the imux list at once */
3303 static int cap_put_caller(struct snd_kcontrol
*kcontrol
,
3304 struct snd_ctl_elem_value
*ucontrol
,
3305 put_call_t func
, int type
)
3307 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3308 struct hda_gen_spec
*spec
= codec
->spec
;
3309 const struct hda_input_mux
*imux
;
3310 struct nid_path
*path
;
3311 int i
, adc_idx
, err
= 0;
3313 imux
= &spec
->input_mux
;
3314 adc_idx
= kcontrol
->id
.index
;
3315 mutex_lock(&codec
->control_mutex
);
3316 /* we use the cache-only update at first since multiple input paths
3317 * may shared the same amp; by updating only caches, the redundant
3318 * writes to hardware can be reduced.
3320 codec
->cached_write
= 1;
3321 for (i
= 0; i
< imux
->num_items
; i
++) {
3322 path
= get_input_path(codec
, adc_idx
, i
);
3323 if (!path
|| !path
->ctls
[type
])
3325 kcontrol
->private_value
= path
->ctls
[type
];
3326 err
= func(kcontrol
, ucontrol
);
3331 codec
->cached_write
= 0;
3332 mutex_unlock(&codec
->control_mutex
);
3333 snd_hda_codec_flush_cache(codec
); /* flush the updates */
3334 if (err
>= 0 && spec
->cap_sync_hook
)
3335 spec
->cap_sync_hook(codec
, kcontrol
, ucontrol
);
3339 /* capture volume ctl callbacks */
3340 #define cap_vol_info snd_hda_mixer_amp_volume_info
3341 #define cap_vol_get snd_hda_mixer_amp_volume_get
3342 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3344 static int cap_vol_put(struct snd_kcontrol
*kcontrol
,
3345 struct snd_ctl_elem_value
*ucontrol
)
3347 return cap_put_caller(kcontrol
, ucontrol
,
3348 snd_hda_mixer_amp_volume_put
,
3352 static const struct snd_kcontrol_new cap_vol_temp
= {
3353 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3354 .name
= "Capture Volume",
3355 .access
= (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
3356 SNDRV_CTL_ELEM_ACCESS_TLV_READ
|
3357 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
),
3358 .info
= cap_vol_info
,
3361 .tlv
= { .c
= cap_vol_tlv
},
3364 /* capture switch ctl callbacks */
3365 #define cap_sw_info snd_ctl_boolean_stereo_info
3366 #define cap_sw_get snd_hda_mixer_amp_switch_get
3368 static int cap_sw_put(struct snd_kcontrol
*kcontrol
,
3369 struct snd_ctl_elem_value
*ucontrol
)
3371 return cap_put_caller(kcontrol
, ucontrol
,
3372 snd_hda_mixer_amp_switch_put
,
3376 static const struct snd_kcontrol_new cap_sw_temp
= {
3377 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3378 .name
= "Capture Switch",
3379 .info
= cap_sw_info
,
3384 static int parse_capvol_in_path(struct hda_codec
*codec
, struct nid_path
*path
)
3389 path
->ctls
[NID_PATH_VOL_CTL
] = path
->ctls
[NID_PATH_MUTE_CTL
] = 0;
3390 for (depth
= 0; depth
< 3; depth
++) {
3391 if (depth
>= path
->depth
)
3393 i
= path
->depth
- depth
- 1;
3394 nid
= path
->path
[i
];
3395 if (!path
->ctls
[NID_PATH_VOL_CTL
]) {
3396 if (nid_has_volume(codec
, nid
, HDA_OUTPUT
))
3397 path
->ctls
[NID_PATH_VOL_CTL
] =
3398 HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3399 else if (nid_has_volume(codec
, nid
, HDA_INPUT
)) {
3400 int idx
= path
->idx
[i
];
3401 if (!depth
&& codec
->single_adc_amp
)
3403 path
->ctls
[NID_PATH_VOL_CTL
] =
3404 HDA_COMPOSE_AMP_VAL(nid
, 3, idx
, HDA_INPUT
);
3407 if (!path
->ctls
[NID_PATH_MUTE_CTL
]) {
3408 if (nid_has_mute(codec
, nid
, HDA_OUTPUT
))
3409 path
->ctls
[NID_PATH_MUTE_CTL
] =
3410 HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3411 else if (nid_has_mute(codec
, nid
, HDA_INPUT
)) {
3412 int idx
= path
->idx
[i
];
3413 if (!depth
&& codec
->single_adc_amp
)
3415 path
->ctls
[NID_PATH_MUTE_CTL
] =
3416 HDA_COMPOSE_AMP_VAL(nid
, 3, idx
, HDA_INPUT
);
3423 static bool is_inv_dmic_pin(struct hda_codec
*codec
, hda_nid_t nid
)
3425 struct hda_gen_spec
*spec
= codec
->spec
;
3426 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3430 if (!spec
->inv_dmic_split
)
3432 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
3433 if (cfg
->inputs
[i
].pin
!= nid
)
3435 if (cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
3437 val
= snd_hda_codec_get_pincfg(codec
, nid
);
3438 return snd_hda_get_input_pin_attr(val
) == INPUT_PIN_ATTR_INT
;
3443 /* capture switch put callback for a single control with hook call */
3444 static int cap_single_sw_put(struct snd_kcontrol
*kcontrol
,
3445 struct snd_ctl_elem_value
*ucontrol
)
3447 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3448 struct hda_gen_spec
*spec
= codec
->spec
;
3451 ret
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
3455 if (spec
->cap_sync_hook
)
3456 spec
->cap_sync_hook(codec
, kcontrol
, ucontrol
);
3461 static int add_single_cap_ctl(struct hda_codec
*codec
, const char *label
,
3462 int idx
, bool is_switch
, unsigned int ctl
,
3465 struct hda_gen_spec
*spec
= codec
->spec
;
3466 char tmpname
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
3467 int type
= is_switch
? HDA_CTL_WIDGET_MUTE
: HDA_CTL_WIDGET_VOL
;
3468 const char *sfx
= is_switch
? "Switch" : "Volume";
3469 unsigned int chs
= inv_dmic
? 1 : 3;
3470 struct snd_kcontrol_new
*knew
;
3476 snprintf(tmpname
, sizeof(tmpname
),
3477 "%s Capture %s", label
, sfx
);
3479 snprintf(tmpname
, sizeof(tmpname
),
3481 knew
= add_control(spec
, type
, tmpname
, idx
,
3482 amp_val_replace_channels(ctl
, chs
));
3486 knew
->put
= cap_single_sw_put
;
3490 /* Make independent right kcontrol */
3492 snprintf(tmpname
, sizeof(tmpname
),
3493 "Inverted %s Capture %s", label
, sfx
);
3495 snprintf(tmpname
, sizeof(tmpname
),
3496 "Inverted Capture %s", sfx
);
3497 knew
= add_control(spec
, type
, tmpname
, idx
,
3498 amp_val_replace_channels(ctl
, 2));
3502 knew
->put
= cap_single_sw_put
;
3506 /* create single (and simple) capture volume and switch controls */
3507 static int create_single_cap_vol_ctl(struct hda_codec
*codec
, int idx
,
3508 unsigned int vol_ctl
, unsigned int sw_ctl
,
3512 err
= add_single_cap_ctl(codec
, NULL
, idx
, false, vol_ctl
, inv_dmic
);
3515 err
= add_single_cap_ctl(codec
, NULL
, idx
, true, sw_ctl
, inv_dmic
);
3521 /* create bound capture volume and switch controls */
3522 static int create_bind_cap_vol_ctl(struct hda_codec
*codec
, int idx
,
3523 unsigned int vol_ctl
, unsigned int sw_ctl
)
3525 struct hda_gen_spec
*spec
= codec
->spec
;
3526 struct snd_kcontrol_new
*knew
;
3529 knew
= snd_hda_gen_add_kctl(spec
, NULL
, &cap_vol_temp
);
3533 knew
->private_value
= vol_ctl
;
3534 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
3537 knew
= snd_hda_gen_add_kctl(spec
, NULL
, &cap_sw_temp
);
3541 knew
->private_value
= sw_ctl
;
3542 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
3547 /* return the vol ctl when used first in the imux list */
3548 static unsigned int get_first_cap_ctl(struct hda_codec
*codec
, int idx
, int type
)
3550 struct nid_path
*path
;
3554 path
= get_input_path(codec
, 0, idx
);
3557 ctl
= path
->ctls
[type
];
3560 for (i
= 0; i
< idx
- 1; i
++) {
3561 path
= get_input_path(codec
, 0, i
);
3562 if (path
&& path
->ctls
[type
] == ctl
)
3568 /* create individual capture volume and switch controls per input */
3569 static int create_multi_cap_vol_ctl(struct hda_codec
*codec
)
3571 struct hda_gen_spec
*spec
= codec
->spec
;
3572 struct hda_input_mux
*imux
= &spec
->input_mux
;
3575 for (i
= 0; i
< imux
->num_items
; i
++) {
3579 idx
= imux
->items
[i
].index
;
3580 if (idx
>= spec
->autocfg
.num_inputs
)
3582 inv_dmic
= is_inv_dmic_pin(codec
, spec
->imux_pins
[i
]);
3584 for (type
= 0; type
< 2; type
++) {
3585 err
= add_single_cap_ctl(codec
,
3586 spec
->input_labels
[idx
],
3587 spec
->input_label_idxs
[idx
],
3589 get_first_cap_ctl(codec
, i
, type
),
3598 static int create_capture_mixers(struct hda_codec
*codec
)
3600 struct hda_gen_spec
*spec
= codec
->spec
;
3601 struct hda_input_mux
*imux
= &spec
->input_mux
;
3602 int i
, n
, nums
, err
;
3604 if (spec
->dyn_adc_switch
)
3607 nums
= spec
->num_adc_nids
;
3609 if (!spec
->auto_mic
&& imux
->num_items
> 1) {
3610 struct snd_kcontrol_new
*knew
;
3612 name
= nums
> 1 ? "Input Source" : "Capture Source";
3613 knew
= snd_hda_gen_add_kctl(spec
, name
, &cap_src_temp
);
3619 for (n
= 0; n
< nums
; n
++) {
3621 bool multi_cap_vol
= spec
->multi_cap_vol
;
3622 bool inv_dmic
= false;
3626 for (i
= 0; i
< imux
->num_items
; i
++) {
3627 struct nid_path
*path
;
3628 path
= get_input_path(codec
, n
, i
);
3631 parse_capvol_in_path(codec
, path
);
3633 vol
= path
->ctls
[NID_PATH_VOL_CTL
];
3634 else if (vol
!= path
->ctls
[NID_PATH_VOL_CTL
]) {
3636 if (!same_amp_caps(codec
, vol
,
3637 path
->ctls
[NID_PATH_VOL_CTL
], HDA_INPUT
))
3638 multi_cap_vol
= true;
3641 sw
= path
->ctls
[NID_PATH_MUTE_CTL
];
3642 else if (sw
!= path
->ctls
[NID_PATH_MUTE_CTL
]) {
3644 if (!same_amp_caps(codec
, sw
,
3645 path
->ctls
[NID_PATH_MUTE_CTL
], HDA_INPUT
))
3646 multi_cap_vol
= true;
3648 if (is_inv_dmic_pin(codec
, spec
->imux_pins
[i
]))
3653 err
= create_single_cap_vol_ctl(codec
, n
, vol
, sw
,
3655 else if (!multi_cap_vol
&& !inv_dmic
)
3656 err
= create_bind_cap_vol_ctl(codec
, n
, vol
, sw
);
3658 err
= create_multi_cap_vol_ctl(codec
);
3667 * add mic boosts if needed
3670 /* check whether the given amp is feasible as a boost volume */
3671 static bool check_boost_vol(struct hda_codec
*codec
, hda_nid_t nid
,
3676 if (!nid_has_volume(codec
, nid
, dir
) ||
3677 is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_VOL_CTL
) ||
3678 is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_BOOST_CTL
))
3681 step
= (query_amp_caps(codec
, nid
, dir
) & AC_AMPCAP_STEP_SIZE
)
3682 >> AC_AMPCAP_STEP_SIZE_SHIFT
;
3688 /* look for a boost amp in a widget close to the pin */
3689 static unsigned int look_for_boost_amp(struct hda_codec
*codec
,
3690 struct nid_path
*path
)
3692 unsigned int val
= 0;
3696 for (depth
= 0; depth
< 3; depth
++) {
3697 if (depth
>= path
->depth
- 1)
3699 nid
= path
->path
[depth
];
3700 if (depth
&& check_boost_vol(codec
, nid
, HDA_OUTPUT
, 0)) {
3701 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3703 } else if (check_boost_vol(codec
, nid
, HDA_INPUT
,
3704 path
->idx
[depth
])) {
3705 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, path
->idx
[depth
],
3714 static int parse_mic_boost(struct hda_codec
*codec
)
3716 struct hda_gen_spec
*spec
= codec
->spec
;
3717 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3718 struct hda_input_mux
*imux
= &spec
->input_mux
;
3721 if (!spec
->num_adc_nids
)
3724 for (i
= 0; i
< imux
->num_items
; i
++) {
3725 struct nid_path
*path
;
3728 char boost_label
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
3730 idx
= imux
->items
[i
].index
;
3731 if (idx
>= imux
->num_items
)
3734 /* check only line-in and mic pins */
3735 if (cfg
->inputs
[idx
].type
> AUTO_PIN_LINE_IN
)
3738 path
= get_input_path(codec
, 0, i
);
3742 val
= look_for_boost_amp(codec
, path
);
3746 /* create a boost control */
3747 snprintf(boost_label
, sizeof(boost_label
),
3748 "%s Boost Volume", spec
->input_labels
[idx
]);
3749 if (!add_control(spec
, HDA_CTL_WIDGET_VOL
, boost_label
,
3750 spec
->input_label_idxs
[idx
], val
))
3753 path
->ctls
[NID_PATH_BOOST_CTL
] = val
;
3759 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3761 static void parse_digital(struct hda_codec
*codec
)
3763 struct hda_gen_spec
*spec
= codec
->spec
;
3764 struct nid_path
*path
;
3766 hda_nid_t dig_nid
, pin
;
3768 /* support multiple SPDIFs; the secondary is set up as a slave */
3770 for (i
= 0; i
< spec
->autocfg
.dig_outs
; i
++) {
3771 pin
= spec
->autocfg
.dig_out_pins
[i
];
3772 dig_nid
= look_for_dac(codec
, pin
, true);
3775 path
= snd_hda_add_new_path(codec
, dig_nid
, pin
, 0);
3778 print_nid_path(codec
, "digout", path
);
3779 path
->active
= true;
3780 spec
->digout_paths
[i
] = snd_hda_get_path_idx(codec
, path
);
3781 set_pin_target(codec
, pin
, PIN_OUT
, false);
3783 spec
->multiout
.dig_out_nid
= dig_nid
;
3784 spec
->dig_out_type
= spec
->autocfg
.dig_out_type
[0];
3786 spec
->multiout
.slave_dig_outs
= spec
->slave_dig_outs
;
3787 if (nums
>= ARRAY_SIZE(spec
->slave_dig_outs
) - 1)
3789 spec
->slave_dig_outs
[nums
- 1] = dig_nid
;
3794 if (spec
->autocfg
.dig_in_pin
) {
3795 pin
= spec
->autocfg
.dig_in_pin
;
3796 dig_nid
= codec
->start_nid
;
3797 for (i
= 0; i
< codec
->num_nodes
; i
++, dig_nid
++) {
3798 unsigned int wcaps
= get_wcaps(codec
, dig_nid
);
3799 if (get_wcaps_type(wcaps
) != AC_WID_AUD_IN
)
3801 if (!(wcaps
& AC_WCAP_DIGITAL
))
3803 path
= snd_hda_add_new_path(codec
, pin
, dig_nid
, 0);
3805 print_nid_path(codec
, "digin", path
);
3806 path
->active
= true;
3807 spec
->dig_in_nid
= dig_nid
;
3808 spec
->digin_path
= snd_hda_get_path_idx(codec
, path
);
3809 set_pin_target(codec
, pin
, PIN_IN
, false);
3818 * input MUX handling
3821 static bool dyn_adc_pcm_resetup(struct hda_codec
*codec
, int cur
);
3823 /* select the given imux item; either unmute exclusively or select the route */
3824 static int mux_select(struct hda_codec
*codec
, unsigned int adc_idx
,
3827 struct hda_gen_spec
*spec
= codec
->spec
;
3828 const struct hda_input_mux
*imux
;
3829 struct nid_path
*old_path
, *path
;
3831 imux
= &spec
->input_mux
;
3832 if (!imux
->num_items
)
3835 if (idx
>= imux
->num_items
)
3836 idx
= imux
->num_items
- 1;
3837 if (spec
->cur_mux
[adc_idx
] == idx
)
3840 old_path
= get_input_path(codec
, adc_idx
, spec
->cur_mux
[adc_idx
]);
3843 if (old_path
->active
)
3844 snd_hda_activate_path(codec
, old_path
, false, false);
3846 spec
->cur_mux
[adc_idx
] = idx
;
3849 update_hp_mic(codec
, adc_idx
, false);
3851 if (spec
->dyn_adc_switch
)
3852 dyn_adc_pcm_resetup(codec
, idx
);
3854 path
= get_input_path(codec
, adc_idx
, idx
);
3859 snd_hda_activate_path(codec
, path
, true, false);
3860 if (spec
->cap_sync_hook
)
3861 spec
->cap_sync_hook(codec
, NULL
, NULL
);
3862 path_power_down_sync(codec
, old_path
);
3868 * Jack detections for HP auto-mute and mic-switch
3871 /* check each pin in the given array; returns true if any of them is plugged */
3872 static bool detect_jacks(struct hda_codec
*codec
, int num_pins
, hda_nid_t
*pins
)
3875 bool present
= false;
3877 for (i
= 0; i
< num_pins
; i
++) {
3878 hda_nid_t nid
= pins
[i
];
3881 /* don't detect pins retasked as inputs */
3882 if (snd_hda_codec_get_pin_target(codec
, nid
) & AC_PINCTL_IN_EN
)
3884 if (snd_hda_jack_detect_state(codec
, nid
) == HDA_JACK_PRESENT
)
3890 /* standard HP/line-out auto-mute helper */
3891 static void do_automute(struct hda_codec
*codec
, int num_pins
, hda_nid_t
*pins
,
3892 int *paths
, bool mute
)
3894 struct hda_gen_spec
*spec
= codec
->spec
;
3897 for (i
= 0; i
< num_pins
; i
++) {
3898 hda_nid_t nid
= pins
[i
];
3899 unsigned int val
, oldval
;
3903 if (spec
->auto_mute_via_amp
) {
3904 struct nid_path
*path
;
3907 path
= snd_hda_get_path_from_idx(codec
, paths
[i
]);
3910 mute_nid
= get_amp_nid_(path
->ctls
[NID_PATH_MUTE_CTL
]);
3914 spec
->mute_bits
|= (1ULL << mute_nid
);
3916 spec
->mute_bits
&= ~(1ULL << mute_nid
);
3917 set_pin_eapd(codec
, nid
, !mute
);
3921 oldval
= snd_hda_codec_get_pin_target(codec
, nid
);
3922 if (oldval
& PIN_IN
)
3923 continue; /* no mute for inputs */
3924 /* don't reset VREF value in case it's controlling
3925 * the amp (see alc861_fixup_asus_amp_vref_0f())
3927 if (spec
->keep_vref_in_automute
)
3928 val
= oldval
& ~PIN_HP
;
3933 /* here we call update_pin_ctl() so that the pinctl is changed
3934 * without changing the pinctl target value;
3935 * the original target value will be still referred at the
3936 * init / resume again
3938 update_pin_ctl(codec
, nid
, val
);
3939 set_pin_eapd(codec
, nid
, !mute
);
3944 * snd_hda_gen_update_outputs - Toggle outputs muting
3945 * @codec: the HDA codec
3947 * Update the mute status of all outputs based on the current jack states.
3949 void snd_hda_gen_update_outputs(struct hda_codec
*codec
)
3951 struct hda_gen_spec
*spec
= codec
->spec
;
3955 /* Control HP pins/amps depending on master_mute state;
3956 * in general, HP pins/amps control should be enabled in all cases,
3957 * but currently set only for master_mute, just to be safe
3959 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
3960 paths
= spec
->out_paths
;
3962 paths
= spec
->hp_paths
;
3963 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.hp_pins
),
3964 spec
->autocfg
.hp_pins
, paths
, spec
->master_mute
);
3966 if (!spec
->automute_speaker
)
3969 on
= spec
->hp_jack_present
| spec
->line_jack_present
;
3970 on
|= spec
->master_mute
;
3971 spec
->speaker_muted
= on
;
3972 if (spec
->autocfg
.line_out_type
== AUTO_PIN_SPEAKER_OUT
)
3973 paths
= spec
->out_paths
;
3975 paths
= spec
->speaker_paths
;
3976 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.speaker_pins
),
3977 spec
->autocfg
.speaker_pins
, paths
, on
);
3979 /* toggle line-out mutes if needed, too */
3980 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3981 if (spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.hp_pins
[0] ||
3982 spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.speaker_pins
[0])
3984 if (!spec
->automute_lo
)
3987 on
= spec
->hp_jack_present
;
3988 on
|= spec
->master_mute
;
3989 spec
->line_out_muted
= on
;
3990 paths
= spec
->out_paths
;
3991 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.line_out_pins
),
3992 spec
->autocfg
.line_out_pins
, paths
, on
);
3994 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs
);
3996 static void call_update_outputs(struct hda_codec
*codec
)
3998 struct hda_gen_spec
*spec
= codec
->spec
;
3999 if (spec
->automute_hook
)
4000 spec
->automute_hook(codec
);
4002 snd_hda_gen_update_outputs(codec
);
4004 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4005 if (spec
->auto_mute_via_amp
&& !codec
->bus
->shutdown
)
4006 snd_ctl_sync_vmaster(spec
->vmaster_mute
.sw_kctl
, false);
4010 * snd_hda_gen_hp_automute - standard HP-automute helper
4011 * @codec: the HDA codec
4012 * @jack: jack object, NULL for the whole
4014 void snd_hda_gen_hp_automute(struct hda_codec
*codec
,
4015 struct hda_jack_callback
*jack
)
4017 struct hda_gen_spec
*spec
= codec
->spec
;
4018 hda_nid_t
*pins
= spec
->autocfg
.hp_pins
;
4019 int num_pins
= ARRAY_SIZE(spec
->autocfg
.hp_pins
);
4021 /* No detection for the first HP jack during indep-HP mode */
4022 if (spec
->indep_hp_enabled
) {
4027 spec
->hp_jack_present
= detect_jacks(codec
, num_pins
, pins
);
4028 if (!spec
->detect_hp
|| (!spec
->automute_speaker
&& !spec
->automute_lo
))
4030 call_update_outputs(codec
);
4032 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute
);
4035 * snd_hda_gen_line_automute - standard line-out-automute helper
4036 * @codec: the HDA codec
4037 * @jack: jack object, NULL for the whole
4039 void snd_hda_gen_line_automute(struct hda_codec
*codec
,
4040 struct hda_jack_callback
*jack
)
4042 struct hda_gen_spec
*spec
= codec
->spec
;
4044 if (spec
->autocfg
.line_out_type
== AUTO_PIN_SPEAKER_OUT
)
4046 /* check LO jack only when it's different from HP */
4047 if (spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.hp_pins
[0])
4050 spec
->line_jack_present
=
4051 detect_jacks(codec
, ARRAY_SIZE(spec
->autocfg
.line_out_pins
),
4052 spec
->autocfg
.line_out_pins
);
4053 if (!spec
->automute_speaker
|| !spec
->detect_lo
)
4055 call_update_outputs(codec
);
4057 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute
);
4060 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4061 * @codec: the HDA codec
4062 * @jack: jack object, NULL for the whole
4064 void snd_hda_gen_mic_autoswitch(struct hda_codec
*codec
,
4065 struct hda_jack_callback
*jack
)
4067 struct hda_gen_spec
*spec
= codec
->spec
;
4070 if (!spec
->auto_mic
)
4073 for (i
= spec
->am_num_entries
- 1; i
> 0; i
--) {
4074 hda_nid_t pin
= spec
->am_entry
[i
].pin
;
4075 /* don't detect pins retasked as outputs */
4076 if (snd_hda_codec_get_pin_target(codec
, pin
) & AC_PINCTL_OUT_EN
)
4078 if (snd_hda_jack_detect_state(codec
, pin
) == HDA_JACK_PRESENT
) {
4079 mux_select(codec
, 0, spec
->am_entry
[i
].idx
);
4083 mux_select(codec
, 0, spec
->am_entry
[0].idx
);
4085 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch
);
4087 /* call appropriate hooks */
4088 static void call_hp_automute(struct hda_codec
*codec
,
4089 struct hda_jack_callback
*jack
)
4091 struct hda_gen_spec
*spec
= codec
->spec
;
4092 if (spec
->hp_automute_hook
)
4093 spec
->hp_automute_hook(codec
, jack
);
4095 snd_hda_gen_hp_automute(codec
, jack
);
4098 static void call_line_automute(struct hda_codec
*codec
,
4099 struct hda_jack_callback
*jack
)
4101 struct hda_gen_spec
*spec
= codec
->spec
;
4102 if (spec
->line_automute_hook
)
4103 spec
->line_automute_hook(codec
, jack
);
4105 snd_hda_gen_line_automute(codec
, jack
);
4108 static void call_mic_autoswitch(struct hda_codec
*codec
,
4109 struct hda_jack_callback
*jack
)
4111 struct hda_gen_spec
*spec
= codec
->spec
;
4112 if (spec
->mic_autoswitch_hook
)
4113 spec
->mic_autoswitch_hook(codec
, jack
);
4115 snd_hda_gen_mic_autoswitch(codec
, jack
);
4118 /* update jack retasking */
4119 static void update_automute_all(struct hda_codec
*codec
)
4121 call_hp_automute(codec
, NULL
);
4122 call_line_automute(codec
, NULL
);
4123 call_mic_autoswitch(codec
, NULL
);
4127 * Auto-Mute mode mixer enum support
4129 static int automute_mode_info(struct snd_kcontrol
*kcontrol
,
4130 struct snd_ctl_elem_info
*uinfo
)
4132 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
4133 struct hda_gen_spec
*spec
= codec
->spec
;
4134 static const char * const texts3
[] = {
4135 "Disabled", "Speaker Only", "Line Out+Speaker"
4138 if (spec
->automute_speaker_possible
&& spec
->automute_lo_possible
)
4139 return snd_hda_enum_helper_info(kcontrol
, uinfo
, 3, texts3
);
4140 return snd_hda_enum_bool_helper_info(kcontrol
, uinfo
);
4143 static int automute_mode_get(struct snd_kcontrol
*kcontrol
,
4144 struct snd_ctl_elem_value
*ucontrol
)
4146 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
4147 struct hda_gen_spec
*spec
= codec
->spec
;
4148 unsigned int val
= 0;
4149 if (spec
->automute_speaker
)
4151 if (spec
->automute_lo
)
4154 ucontrol
->value
.enumerated
.item
[0] = val
;
4158 static int automute_mode_put(struct snd_kcontrol
*kcontrol
,
4159 struct snd_ctl_elem_value
*ucontrol
)
4161 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
4162 struct hda_gen_spec
*spec
= codec
->spec
;
4164 switch (ucontrol
->value
.enumerated
.item
[0]) {
4166 if (!spec
->automute_speaker
&& !spec
->automute_lo
)
4168 spec
->automute_speaker
= 0;
4169 spec
->automute_lo
= 0;
4172 if (spec
->automute_speaker_possible
) {
4173 if (!spec
->automute_lo
&& spec
->automute_speaker
)
4175 spec
->automute_speaker
= 1;
4176 spec
->automute_lo
= 0;
4177 } else if (spec
->automute_lo_possible
) {
4178 if (spec
->automute_lo
)
4180 spec
->automute_lo
= 1;
4185 if (!spec
->automute_lo_possible
|| !spec
->automute_speaker_possible
)
4187 if (spec
->automute_speaker
&& spec
->automute_lo
)
4189 spec
->automute_speaker
= 1;
4190 spec
->automute_lo
= 1;
4195 call_update_outputs(codec
);
4199 static const struct snd_kcontrol_new automute_mode_enum
= {
4200 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
4201 .name
= "Auto-Mute Mode",
4202 .info
= automute_mode_info
,
4203 .get
= automute_mode_get
,
4204 .put
= automute_mode_put
,
4207 static int add_automute_mode_enum(struct hda_codec
*codec
)
4209 struct hda_gen_spec
*spec
= codec
->spec
;
4211 if (!snd_hda_gen_add_kctl(spec
, NULL
, &automute_mode_enum
))
4217 * Check the availability of HP/line-out auto-mute;
4218 * Set up appropriately if really supported
4220 static int check_auto_mute_availability(struct hda_codec
*codec
)
4222 struct hda_gen_spec
*spec
= codec
->spec
;
4223 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4227 if (spec
->suppress_auto_mute
)
4230 if (cfg
->hp_pins
[0])
4232 if (cfg
->line_out_pins
[0])
4234 if (cfg
->speaker_pins
[0])
4236 if (present
< 2) /* need two different output types */
4239 if (!cfg
->speaker_pins
[0] &&
4240 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
) {
4241 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
4242 sizeof(cfg
->speaker_pins
));
4243 cfg
->speaker_outs
= cfg
->line_outs
;
4246 if (!cfg
->hp_pins
[0] &&
4247 cfg
->line_out_type
== AUTO_PIN_HP_OUT
) {
4248 memcpy(cfg
->hp_pins
, cfg
->line_out_pins
,
4249 sizeof(cfg
->hp_pins
));
4250 cfg
->hp_outs
= cfg
->line_outs
;
4253 for (i
= 0; i
< cfg
->hp_outs
; i
++) {
4254 hda_nid_t nid
= cfg
->hp_pins
[i
];
4255 if (!is_jack_detectable(codec
, nid
))
4257 codec_dbg(codec
, "Enable HP auto-muting on NID 0x%x\n", nid
);
4258 snd_hda_jack_detect_enable_callback(codec
, nid
,
4260 spec
->detect_hp
= 1;
4263 if (cfg
->line_out_type
== AUTO_PIN_LINE_OUT
&& cfg
->line_outs
) {
4264 if (cfg
->speaker_outs
)
4265 for (i
= 0; i
< cfg
->line_outs
; i
++) {
4266 hda_nid_t nid
= cfg
->line_out_pins
[i
];
4267 if (!is_jack_detectable(codec
, nid
))
4269 codec_dbg(codec
, "Enable Line-Out auto-muting on NID 0x%x\n", nid
);
4270 snd_hda_jack_detect_enable_callback(codec
, nid
,
4271 call_line_automute
);
4272 spec
->detect_lo
= 1;
4274 spec
->automute_lo_possible
= spec
->detect_hp
;
4277 spec
->automute_speaker_possible
= cfg
->speaker_outs
&&
4278 (spec
->detect_hp
|| spec
->detect_lo
);
4280 spec
->automute_lo
= spec
->automute_lo_possible
;
4281 spec
->automute_speaker
= spec
->automute_speaker_possible
;
4283 if (spec
->automute_speaker_possible
|| spec
->automute_lo_possible
) {
4284 /* create a control for automute mode */
4285 err
= add_automute_mode_enum(codec
);
4292 /* check whether all auto-mic pins are valid; setup indices if OK */
4293 static bool auto_mic_check_imux(struct hda_codec
*codec
)
4295 struct hda_gen_spec
*spec
= codec
->spec
;
4296 const struct hda_input_mux
*imux
;
4299 imux
= &spec
->input_mux
;
4300 for (i
= 0; i
< spec
->am_num_entries
; i
++) {
4301 spec
->am_entry
[i
].idx
=
4302 find_idx_in_nid_list(spec
->am_entry
[i
].pin
,
4303 spec
->imux_pins
, imux
->num_items
);
4304 if (spec
->am_entry
[i
].idx
< 0)
4305 return false; /* no corresponding imux */
4308 /* we don't need the jack detection for the first pin */
4309 for (i
= 1; i
< spec
->am_num_entries
; i
++)
4310 snd_hda_jack_detect_enable_callback(codec
,
4311 spec
->am_entry
[i
].pin
,
4312 call_mic_autoswitch
);
4316 static int compare_attr(const void *ap
, const void *bp
)
4318 const struct automic_entry
*a
= ap
;
4319 const struct automic_entry
*b
= bp
;
4320 return (int)(a
->attr
- b
->attr
);
4324 * Check the availability of auto-mic switch;
4325 * Set up if really supported
4327 static int check_auto_mic_availability(struct hda_codec
*codec
)
4329 struct hda_gen_spec
*spec
= codec
->spec
;
4330 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4334 if (spec
->suppress_auto_mic
)
4339 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
4340 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
4342 attr
= snd_hda_codec_get_pincfg(codec
, nid
);
4343 attr
= snd_hda_get_input_pin_attr(attr
);
4344 if (types
& (1 << attr
))
4345 return 0; /* already occupied */
4347 case INPUT_PIN_ATTR_INT
:
4348 if (cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
4349 return 0; /* invalid type */
4351 case INPUT_PIN_ATTR_UNUSED
:
4352 return 0; /* invalid entry */
4354 if (cfg
->inputs
[i
].type
> AUTO_PIN_LINE_IN
)
4355 return 0; /* invalid type */
4356 if (!spec
->line_in_auto_switch
&&
4357 cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
4358 return 0; /* only mic is allowed */
4359 if (!is_jack_detectable(codec
, nid
))
4360 return 0; /* no unsol support */
4363 if (num_pins
>= MAX_AUTO_MIC_PINS
)
4365 types
|= (1 << attr
);
4366 spec
->am_entry
[num_pins
].pin
= nid
;
4367 spec
->am_entry
[num_pins
].attr
= attr
;
4374 spec
->am_num_entries
= num_pins
;
4375 /* sort the am_entry in the order of attr so that the pin with a
4376 * higher attr will be selected when the jack is plugged.
4378 sort(spec
->am_entry
, num_pins
, sizeof(spec
->am_entry
[0]),
4379 compare_attr
, NULL
);
4381 if (!auto_mic_check_imux(codec
))
4385 spec
->num_adc_nids
= 1;
4386 spec
->cur_mux
[0] = spec
->am_entry
[0].idx
;
4387 codec_dbg(codec
, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4388 spec
->am_entry
[0].pin
,
4389 spec
->am_entry
[1].pin
,
4390 spec
->am_entry
[2].pin
);
4396 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4398 * @codec: the HDA codec
4399 * @nid: NID to evalute
4400 * @power_state: target power state
4402 unsigned int snd_hda_gen_path_power_filter(struct hda_codec
*codec
,
4404 unsigned int power_state
)
4406 if (power_state
!= AC_PWRST_D0
|| nid
== codec
->afg
)
4408 if (get_wcaps_type(get_wcaps(codec
, nid
)) >= AC_WID_POWER
)
4410 if (is_active_nid_for_any(codec
, nid
))
4414 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter
);
4416 /* mute all aamix inputs initially; parse up to the first leaves */
4417 static void mute_all_mixer_nid(struct hda_codec
*codec
, hda_nid_t mix
)
4420 const hda_nid_t
*conn
;
4423 nums
= snd_hda_get_conn_list(codec
, mix
, &conn
);
4424 has_amp
= nid_has_mute(codec
, mix
, HDA_INPUT
);
4425 for (i
= 0; i
< nums
; i
++) {
4427 snd_hda_codec_amp_stereo(codec
, mix
,
4429 0xff, HDA_AMP_MUTE
);
4430 else if (nid_has_volume(codec
, conn
[i
], HDA_OUTPUT
))
4431 snd_hda_codec_amp_stereo(codec
, conn
[i
],
4433 0xff, HDA_AMP_MUTE
);
4438 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4439 * set up the hda_gen_spec
4440 * @codec: the HDA codec
4441 * @cfg: Parsed pin configuration
4443 * return 1 if successful, 0 if the proper config is not found,
4444 * or a negative error code
4446 int snd_hda_gen_parse_auto_config(struct hda_codec
*codec
,
4447 struct auto_pin_cfg
*cfg
)
4449 struct hda_gen_spec
*spec
= codec
->spec
;
4452 parse_user_hints(codec
);
4454 if (spec
->mixer_nid
&& !spec
->mixer_merge_nid
)
4455 spec
->mixer_merge_nid
= spec
->mixer_nid
;
4457 if (cfg
!= &spec
->autocfg
) {
4458 spec
->autocfg
= *cfg
;
4459 cfg
= &spec
->autocfg
;
4462 if (!spec
->main_out_badness
)
4463 spec
->main_out_badness
= &hda_main_out_badness
;
4464 if (!spec
->extra_out_badness
)
4465 spec
->extra_out_badness
= &hda_extra_out_badness
;
4467 fill_all_dac_nids(codec
);
4469 if (!cfg
->line_outs
) {
4470 if (cfg
->dig_outs
|| cfg
->dig_in_pin
) {
4471 spec
->multiout
.max_channels
= 2;
4472 spec
->no_analog
= 1;
4475 if (!cfg
->num_inputs
&& !cfg
->dig_in_pin
)
4476 return 0; /* can't find valid BIOS pin config */
4479 if (!spec
->no_primary_hp
&&
4480 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
&&
4481 cfg
->line_outs
<= cfg
->hp_outs
) {
4482 /* use HP as primary out */
4483 cfg
->speaker_outs
= cfg
->line_outs
;
4484 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
4485 sizeof(cfg
->speaker_pins
));
4486 cfg
->line_outs
= cfg
->hp_outs
;
4487 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
, sizeof(cfg
->hp_pins
));
4489 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
4490 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
4493 err
= parse_output_paths(codec
);
4496 err
= create_multi_channel_mode(codec
);
4499 err
= create_multi_out_ctls(codec
, cfg
);
4502 err
= create_hp_out_ctls(codec
);
4505 err
= create_speaker_out_ctls(codec
);
4508 err
= create_indep_hp_ctls(codec
);
4511 err
= create_loopback_mixing_ctl(codec
);
4514 err
= create_hp_mic(codec
);
4517 err
= create_input_ctls(codec
);
4521 spec
->const_channel_count
= spec
->ext_channel_count
;
4522 /* check the multiple speaker and headphone pins */
4523 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
4524 spec
->const_channel_count
= max(spec
->const_channel_count
,
4525 cfg
->speaker_outs
* 2);
4526 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
4527 spec
->const_channel_count
= max(spec
->const_channel_count
,
4529 spec
->multiout
.max_channels
= max(spec
->ext_channel_count
,
4530 spec
->const_channel_count
);
4532 err
= check_auto_mute_availability(codec
);
4536 err
= check_dyn_adc_switch(codec
);
4540 err
= check_auto_mic_availability(codec
);
4544 /* add stereo mix if available and not enabled yet */
4545 if (!spec
->auto_mic
&& spec
->mixer_nid
&&
4546 spec
->add_stereo_mix_input
== HDA_HINT_STEREO_MIX_AUTO
&&
4547 spec
->input_mux
.num_items
> 1) {
4548 err
= parse_capture_source(codec
, spec
->mixer_nid
,
4549 CFG_IDX_MIX
, spec
->num_all_adcs
,
4556 err
= create_capture_mixers(codec
);
4560 err
= parse_mic_boost(codec
);
4564 /* create "Headphone Mic Jack Mode" if no input selection is
4565 * available (or user specifies add_jack_modes hint)
4567 if (spec
->hp_mic_pin
&&
4568 (spec
->auto_mic
|| spec
->input_mux
.num_items
== 1 ||
4569 spec
->add_jack_modes
)) {
4570 err
= create_hp_mic_jack_mode(codec
, spec
->hp_mic_pin
);
4575 if (spec
->add_jack_modes
) {
4576 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
4577 err
= create_out_jack_modes(codec
, cfg
->line_outs
,
4578 cfg
->line_out_pins
);
4582 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
) {
4583 err
= create_out_jack_modes(codec
, cfg
->hp_outs
,
4590 /* mute all aamix input initially */
4591 if (spec
->mixer_nid
)
4592 mute_all_mixer_nid(codec
, spec
->mixer_nid
);
4595 parse_digital(codec
);
4597 if (spec
->power_down_unused
)
4598 codec
->power_filter
= snd_hda_gen_path_power_filter
;
4600 if (!spec
->no_analog
&& spec
->beep_nid
) {
4601 err
= snd_hda_attach_beep_device(codec
, spec
->beep_nid
);
4608 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config
);
4612 * Build control elements
4615 /* slave controls for virtual master */
4616 static const char * const slave_pfxs
[] = {
4617 "Front", "Surround", "Center", "LFE", "Side",
4618 "Headphone", "Speaker", "Mono", "Line Out",
4619 "CLFE", "Bass Speaker", "PCM",
4620 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4621 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4622 "Headphone Side", "Headphone+LO", "Speaker+LO",
4627 * snd_hda_gen_build_controls - Build controls from the parsed results
4628 * @codec: the HDA codec
4630 * Pass this to build_controls patch_ops.
4632 int snd_hda_gen_build_controls(struct hda_codec
*codec
)
4634 struct hda_gen_spec
*spec
= codec
->spec
;
4637 if (spec
->kctls
.used
) {
4638 err
= snd_hda_add_new_ctls(codec
, spec
->kctls
.list
);
4643 if (spec
->multiout
.dig_out_nid
) {
4644 err
= snd_hda_create_dig_out_ctls(codec
,
4645 spec
->multiout
.dig_out_nid
,
4646 spec
->multiout
.dig_out_nid
,
4647 spec
->pcm_rec
[1].pcm_type
);
4650 if (!spec
->no_analog
) {
4651 err
= snd_hda_create_spdif_share_sw(codec
,
4655 spec
->multiout
.share_spdif
= 1;
4658 if (spec
->dig_in_nid
) {
4659 err
= snd_hda_create_spdif_in_ctls(codec
, spec
->dig_in_nid
);
4664 /* if we have no master control, let's create it */
4665 if (!spec
->no_analog
&&
4666 !snd_hda_find_mixer_ctl(codec
, "Master Playback Volume")) {
4667 err
= snd_hda_add_vmaster(codec
, "Master Playback Volume",
4668 spec
->vmaster_tlv
, slave_pfxs
,
4673 if (!spec
->no_analog
&&
4674 !snd_hda_find_mixer_ctl(codec
, "Master Playback Switch")) {
4675 err
= __snd_hda_add_vmaster(codec
, "Master Playback Switch",
4678 true, &spec
->vmaster_mute
.sw_kctl
);
4681 if (spec
->vmaster_mute
.hook
) {
4682 snd_hda_add_vmaster_hook(codec
, &spec
->vmaster_mute
,
4683 spec
->vmaster_mute_enum
);
4684 snd_hda_sync_vmaster_hook(&spec
->vmaster_mute
);
4688 free_kctls(spec
); /* no longer needed */
4690 err
= snd_hda_jack_add_kctls(codec
, &spec
->autocfg
);
4696 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls
);
4703 static void call_pcm_playback_hook(struct hda_pcm_stream
*hinfo
,
4704 struct hda_codec
*codec
,
4705 struct snd_pcm_substream
*substream
,
4708 struct hda_gen_spec
*spec
= codec
->spec
;
4709 if (spec
->pcm_playback_hook
)
4710 spec
->pcm_playback_hook(hinfo
, codec
, substream
, action
);
4713 static void call_pcm_capture_hook(struct hda_pcm_stream
*hinfo
,
4714 struct hda_codec
*codec
,
4715 struct snd_pcm_substream
*substream
,
4718 struct hda_gen_spec
*spec
= codec
->spec
;
4719 if (spec
->pcm_capture_hook
)
4720 spec
->pcm_capture_hook(hinfo
, codec
, substream
, action
);
4724 * Analog playback callbacks
4726 static int playback_pcm_open(struct hda_pcm_stream
*hinfo
,
4727 struct hda_codec
*codec
,
4728 struct snd_pcm_substream
*substream
)
4730 struct hda_gen_spec
*spec
= codec
->spec
;
4733 mutex_lock(&spec
->pcm_mutex
);
4734 err
= snd_hda_multi_out_analog_open(codec
,
4735 &spec
->multiout
, substream
,
4738 spec
->active_streams
|= 1 << STREAM_MULTI_OUT
;
4739 call_pcm_playback_hook(hinfo
, codec
, substream
,
4740 HDA_GEN_PCM_ACT_OPEN
);
4742 mutex_unlock(&spec
->pcm_mutex
);
4746 static int playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
4747 struct hda_codec
*codec
,
4748 unsigned int stream_tag
,
4749 unsigned int format
,
4750 struct snd_pcm_substream
*substream
)
4752 struct hda_gen_spec
*spec
= codec
->spec
;
4755 err
= snd_hda_multi_out_analog_prepare(codec
, &spec
->multiout
,
4756 stream_tag
, format
, substream
);
4758 call_pcm_playback_hook(hinfo
, codec
, substream
,
4759 HDA_GEN_PCM_ACT_PREPARE
);
4763 static int playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
4764 struct hda_codec
*codec
,
4765 struct snd_pcm_substream
*substream
)
4767 struct hda_gen_spec
*spec
= codec
->spec
;
4770 err
= snd_hda_multi_out_analog_cleanup(codec
, &spec
->multiout
);
4772 call_pcm_playback_hook(hinfo
, codec
, substream
,
4773 HDA_GEN_PCM_ACT_CLEANUP
);
4777 static int playback_pcm_close(struct hda_pcm_stream
*hinfo
,
4778 struct hda_codec
*codec
,
4779 struct snd_pcm_substream
*substream
)
4781 struct hda_gen_spec
*spec
= codec
->spec
;
4782 mutex_lock(&spec
->pcm_mutex
);
4783 spec
->active_streams
&= ~(1 << STREAM_MULTI_OUT
);
4784 call_pcm_playback_hook(hinfo
, codec
, substream
,
4785 HDA_GEN_PCM_ACT_CLOSE
);
4786 mutex_unlock(&spec
->pcm_mutex
);
4790 static int capture_pcm_open(struct hda_pcm_stream
*hinfo
,
4791 struct hda_codec
*codec
,
4792 struct snd_pcm_substream
*substream
)
4794 call_pcm_capture_hook(hinfo
, codec
, substream
, HDA_GEN_PCM_ACT_OPEN
);
4798 static int capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
4799 struct hda_codec
*codec
,
4800 unsigned int stream_tag
,
4801 unsigned int format
,
4802 struct snd_pcm_substream
*substream
)
4804 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
4805 call_pcm_capture_hook(hinfo
, codec
, substream
,
4806 HDA_GEN_PCM_ACT_PREPARE
);
4810 static int capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
4811 struct hda_codec
*codec
,
4812 struct snd_pcm_substream
*substream
)
4814 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
4815 call_pcm_capture_hook(hinfo
, codec
, substream
,
4816 HDA_GEN_PCM_ACT_CLEANUP
);
4820 static int capture_pcm_close(struct hda_pcm_stream
*hinfo
,
4821 struct hda_codec
*codec
,
4822 struct snd_pcm_substream
*substream
)
4824 call_pcm_capture_hook(hinfo
, codec
, substream
, HDA_GEN_PCM_ACT_CLOSE
);
4828 static int alt_playback_pcm_open(struct hda_pcm_stream
*hinfo
,
4829 struct hda_codec
*codec
,
4830 struct snd_pcm_substream
*substream
)
4832 struct hda_gen_spec
*spec
= codec
->spec
;
4835 mutex_lock(&spec
->pcm_mutex
);
4836 if (!spec
->indep_hp_enabled
)
4839 spec
->active_streams
|= 1 << STREAM_INDEP_HP
;
4840 call_pcm_playback_hook(hinfo
, codec
, substream
,
4841 HDA_GEN_PCM_ACT_OPEN
);
4842 mutex_unlock(&spec
->pcm_mutex
);
4846 static int alt_playback_pcm_close(struct hda_pcm_stream
*hinfo
,
4847 struct hda_codec
*codec
,
4848 struct snd_pcm_substream
*substream
)
4850 struct hda_gen_spec
*spec
= codec
->spec
;
4851 mutex_lock(&spec
->pcm_mutex
);
4852 spec
->active_streams
&= ~(1 << STREAM_INDEP_HP
);
4853 call_pcm_playback_hook(hinfo
, codec
, substream
,
4854 HDA_GEN_PCM_ACT_CLOSE
);
4855 mutex_unlock(&spec
->pcm_mutex
);
4859 static int alt_playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
4860 struct hda_codec
*codec
,
4861 unsigned int stream_tag
,
4862 unsigned int format
,
4863 struct snd_pcm_substream
*substream
)
4865 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
4866 call_pcm_playback_hook(hinfo
, codec
, substream
,
4867 HDA_GEN_PCM_ACT_PREPARE
);
4871 static int alt_playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
4872 struct hda_codec
*codec
,
4873 struct snd_pcm_substream
*substream
)
4875 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
4876 call_pcm_playback_hook(hinfo
, codec
, substream
,
4877 HDA_GEN_PCM_ACT_CLEANUP
);
4884 static int dig_playback_pcm_open(struct hda_pcm_stream
*hinfo
,
4885 struct hda_codec
*codec
,
4886 struct snd_pcm_substream
*substream
)
4888 struct hda_gen_spec
*spec
= codec
->spec
;
4889 return snd_hda_multi_out_dig_open(codec
, &spec
->multiout
);
4892 static int dig_playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
4893 struct hda_codec
*codec
,
4894 unsigned int stream_tag
,
4895 unsigned int format
,
4896 struct snd_pcm_substream
*substream
)
4898 struct hda_gen_spec
*spec
= codec
->spec
;
4899 return snd_hda_multi_out_dig_prepare(codec
, &spec
->multiout
,
4900 stream_tag
, format
, substream
);
4903 static int dig_playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
4904 struct hda_codec
*codec
,
4905 struct snd_pcm_substream
*substream
)
4907 struct hda_gen_spec
*spec
= codec
->spec
;
4908 return snd_hda_multi_out_dig_cleanup(codec
, &spec
->multiout
);
4911 static int dig_playback_pcm_close(struct hda_pcm_stream
*hinfo
,
4912 struct hda_codec
*codec
,
4913 struct snd_pcm_substream
*substream
)
4915 struct hda_gen_spec
*spec
= codec
->spec
;
4916 return snd_hda_multi_out_dig_close(codec
, &spec
->multiout
);
4922 #define alt_capture_pcm_open capture_pcm_open
4923 #define alt_capture_pcm_close capture_pcm_close
4925 static int alt_capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
4926 struct hda_codec
*codec
,
4927 unsigned int stream_tag
,
4928 unsigned int format
,
4929 struct snd_pcm_substream
*substream
)
4931 struct hda_gen_spec
*spec
= codec
->spec
;
4933 snd_hda_codec_setup_stream(codec
, spec
->adc_nids
[substream
->number
+ 1],
4934 stream_tag
, 0, format
);
4935 call_pcm_capture_hook(hinfo
, codec
, substream
,
4936 HDA_GEN_PCM_ACT_PREPARE
);
4940 static int alt_capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
4941 struct hda_codec
*codec
,
4942 struct snd_pcm_substream
*substream
)
4944 struct hda_gen_spec
*spec
= codec
->spec
;
4946 snd_hda_codec_cleanup_stream(codec
,
4947 spec
->adc_nids
[substream
->number
+ 1]);
4948 call_pcm_capture_hook(hinfo
, codec
, substream
,
4949 HDA_GEN_PCM_ACT_CLEANUP
);
4955 static const struct hda_pcm_stream pcm_analog_playback
= {
4959 /* NID is set in build_pcms */
4961 .open
= playback_pcm_open
,
4962 .close
= playback_pcm_close
,
4963 .prepare
= playback_pcm_prepare
,
4964 .cleanup
= playback_pcm_cleanup
4968 static const struct hda_pcm_stream pcm_analog_capture
= {
4972 /* NID is set in build_pcms */
4974 .open
= capture_pcm_open
,
4975 .close
= capture_pcm_close
,
4976 .prepare
= capture_pcm_prepare
,
4977 .cleanup
= capture_pcm_cleanup
4981 static const struct hda_pcm_stream pcm_analog_alt_playback
= {
4985 /* NID is set in build_pcms */
4987 .open
= alt_playback_pcm_open
,
4988 .close
= alt_playback_pcm_close
,
4989 .prepare
= alt_playback_pcm_prepare
,
4990 .cleanup
= alt_playback_pcm_cleanup
4994 static const struct hda_pcm_stream pcm_analog_alt_capture
= {
4995 .substreams
= 2, /* can be overridden */
4998 /* NID is set in build_pcms */
5000 .open
= alt_capture_pcm_open
,
5001 .close
= alt_capture_pcm_close
,
5002 .prepare
= alt_capture_pcm_prepare
,
5003 .cleanup
= alt_capture_pcm_cleanup
5007 static const struct hda_pcm_stream pcm_digital_playback
= {
5011 /* NID is set in build_pcms */
5013 .open
= dig_playback_pcm_open
,
5014 .close
= dig_playback_pcm_close
,
5015 .prepare
= dig_playback_pcm_prepare
,
5016 .cleanup
= dig_playback_pcm_cleanup
5020 static const struct hda_pcm_stream pcm_digital_capture
= {
5024 /* NID is set in build_pcms */
5027 /* Used by build_pcms to flag that a PCM has no playback stream */
5028 static const struct hda_pcm_stream pcm_null_stream
= {
5035 * dynamic changing ADC PCM streams
5037 static bool dyn_adc_pcm_resetup(struct hda_codec
*codec
, int cur
)
5039 struct hda_gen_spec
*spec
= codec
->spec
;
5040 hda_nid_t new_adc
= spec
->adc_nids
[spec
->dyn_adc_idx
[cur
]];
5042 if (spec
->cur_adc
&& spec
->cur_adc
!= new_adc
) {
5043 /* stream is running, let's swap the current ADC */
5044 __snd_hda_codec_cleanup_stream(codec
, spec
->cur_adc
, 1);
5045 spec
->cur_adc
= new_adc
;
5046 snd_hda_codec_setup_stream(codec
, new_adc
,
5047 spec
->cur_adc_stream_tag
, 0,
5048 spec
->cur_adc_format
);
5054 /* analog capture with dynamic dual-adc changes */
5055 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5056 struct hda_codec
*codec
,
5057 unsigned int stream_tag
,
5058 unsigned int format
,
5059 struct snd_pcm_substream
*substream
)
5061 struct hda_gen_spec
*spec
= codec
->spec
;
5062 spec
->cur_adc
= spec
->adc_nids
[spec
->dyn_adc_idx
[spec
->cur_mux
[0]]];
5063 spec
->cur_adc_stream_tag
= stream_tag
;
5064 spec
->cur_adc_format
= format
;
5065 snd_hda_codec_setup_stream(codec
, spec
->cur_adc
, stream_tag
, 0, format
);
5069 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5070 struct hda_codec
*codec
,
5071 struct snd_pcm_substream
*substream
)
5073 struct hda_gen_spec
*spec
= codec
->spec
;
5074 snd_hda_codec_cleanup_stream(codec
, spec
->cur_adc
);
5079 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture
= {
5083 .nid
= 0, /* fill later */
5085 .prepare
= dyn_adc_capture_pcm_prepare
,
5086 .cleanup
= dyn_adc_capture_pcm_cleanup
5090 static void fill_pcm_stream_name(char *str
, size_t len
, const char *sfx
,
5091 const char *chip_name
)
5097 strlcpy(str
, chip_name
, len
);
5099 /* drop non-alnum chars after a space */
5100 for (p
= strchr(str
, ' '); p
; p
= strchr(p
+ 1, ' ')) {
5101 if (!isalnum(p
[1])) {
5106 strlcat(str
, sfx
, len
);
5110 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5111 * @codec: the HDA codec
5113 * Pass this to build_pcms patch_ops.
5115 int snd_hda_gen_build_pcms(struct hda_codec
*codec
)
5117 struct hda_gen_spec
*spec
= codec
->spec
;
5118 struct hda_pcm
*info
= spec
->pcm_rec
;
5119 const struct hda_pcm_stream
*p
;
5120 bool have_multi_adcs
;
5122 codec
->num_pcms
= 1;
5123 codec
->pcm_info
= info
;
5125 if (spec
->no_analog
)
5128 fill_pcm_stream_name(spec
->stream_name_analog
,
5129 sizeof(spec
->stream_name_analog
),
5130 " Analog", codec
->chip_name
);
5131 info
->name
= spec
->stream_name_analog
;
5133 if (spec
->multiout
.num_dacs
> 0) {
5134 p
= spec
->stream_analog_playback
;
5136 p
= &pcm_analog_playback
;
5137 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
] = *p
;
5138 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].nid
= spec
->multiout
.dac_nids
[0];
5139 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].channels_max
=
5140 spec
->multiout
.max_channels
;
5141 if (spec
->autocfg
.line_out_type
== AUTO_PIN_SPEAKER_OUT
&&
5142 spec
->autocfg
.line_outs
== 2)
5143 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].chmap
=
5146 if (spec
->num_adc_nids
) {
5147 p
= spec
->stream_analog_capture
;
5149 if (spec
->dyn_adc_switch
)
5150 p
= &dyn_adc_pcm_analog_capture
;
5152 p
= &pcm_analog_capture
;
5154 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
] = *p
;
5155 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].nid
= spec
->adc_nids
[0];
5159 /* SPDIF for stream index #1 */
5160 if (spec
->multiout
.dig_out_nid
|| spec
->dig_in_nid
) {
5161 fill_pcm_stream_name(spec
->stream_name_digital
,
5162 sizeof(spec
->stream_name_digital
),
5163 " Digital", codec
->chip_name
);
5164 codec
->num_pcms
= 2;
5165 codec
->slave_dig_outs
= spec
->multiout
.slave_dig_outs
;
5166 info
= spec
->pcm_rec
+ 1;
5167 info
->name
= spec
->stream_name_digital
;
5168 if (spec
->dig_out_type
)
5169 info
->pcm_type
= spec
->dig_out_type
;
5171 info
->pcm_type
= HDA_PCM_TYPE_SPDIF
;
5172 if (spec
->multiout
.dig_out_nid
) {
5173 p
= spec
->stream_digital_playback
;
5175 p
= &pcm_digital_playback
;
5176 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
] = *p
;
5177 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].nid
= spec
->multiout
.dig_out_nid
;
5179 if (spec
->dig_in_nid
) {
5180 p
= spec
->stream_digital_capture
;
5182 p
= &pcm_digital_capture
;
5183 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
] = *p
;
5184 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].nid
= spec
->dig_in_nid
;
5188 if (spec
->no_analog
)
5191 /* If the use of more than one ADC is requested for the current
5192 * model, configure a second analog capture-only PCM.
5194 have_multi_adcs
= (spec
->num_adc_nids
> 1) &&
5195 !spec
->dyn_adc_switch
&& !spec
->auto_mic
;
5196 /* Additional Analaog capture for index #2 */
5197 if (spec
->alt_dac_nid
|| have_multi_adcs
) {
5198 fill_pcm_stream_name(spec
->stream_name_alt_analog
,
5199 sizeof(spec
->stream_name_alt_analog
),
5200 " Alt Analog", codec
->chip_name
);
5201 codec
->num_pcms
= 3;
5202 info
= spec
->pcm_rec
+ 2;
5203 info
->name
= spec
->stream_name_alt_analog
;
5204 if (spec
->alt_dac_nid
) {
5205 p
= spec
->stream_analog_alt_playback
;
5207 p
= &pcm_analog_alt_playback
;
5208 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
] = *p
;
5209 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].nid
=
5212 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
] =
5214 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].nid
= 0;
5216 if (have_multi_adcs
) {
5217 p
= spec
->stream_analog_alt_capture
;
5219 p
= &pcm_analog_alt_capture
;
5220 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
] = *p
;
5221 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].nid
=
5223 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].substreams
=
5224 spec
->num_adc_nids
- 1;
5226 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
] =
5228 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].nid
= 0;
5234 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms
);
5238 * Standard auto-parser initializations
5241 /* configure the given path as a proper output */
5242 static void set_output_and_unmute(struct hda_codec
*codec
, int path_idx
)
5244 struct nid_path
*path
;
5247 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
5248 if (!path
|| !path
->depth
)
5250 pin
= path
->path
[path
->depth
- 1];
5251 restore_pin_ctl(codec
, pin
);
5252 snd_hda_activate_path(codec
, path
, path
->active
,
5253 aamix_default(codec
->spec
));
5254 set_pin_eapd(codec
, pin
, path
->active
);
5257 /* initialize primary output paths */
5258 static void init_multi_out(struct hda_codec
*codec
)
5260 struct hda_gen_spec
*spec
= codec
->spec
;
5263 for (i
= 0; i
< spec
->autocfg
.line_outs
; i
++)
5264 set_output_and_unmute(codec
, spec
->out_paths
[i
]);
5268 static void __init_extra_out(struct hda_codec
*codec
, int num_outs
, int *paths
)
5272 for (i
= 0; i
< num_outs
; i
++)
5273 set_output_and_unmute(codec
, paths
[i
]);
5276 /* initialize hp and speaker paths */
5277 static void init_extra_out(struct hda_codec
*codec
)
5279 struct hda_gen_spec
*spec
= codec
->spec
;
5281 if (spec
->autocfg
.line_out_type
!= AUTO_PIN_HP_OUT
)
5282 __init_extra_out(codec
, spec
->autocfg
.hp_outs
, spec
->hp_paths
);
5283 if (spec
->autocfg
.line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
5284 __init_extra_out(codec
, spec
->autocfg
.speaker_outs
,
5285 spec
->speaker_paths
);
5288 /* initialize multi-io paths */
5289 static void init_multi_io(struct hda_codec
*codec
)
5291 struct hda_gen_spec
*spec
= codec
->spec
;
5294 for (i
= 0; i
< spec
->multi_ios
; i
++) {
5295 hda_nid_t pin
= spec
->multi_io
[i
].pin
;
5296 struct nid_path
*path
;
5297 path
= get_multiio_path(codec
, i
);
5300 if (!spec
->multi_io
[i
].ctl_in
)
5301 spec
->multi_io
[i
].ctl_in
=
5302 snd_hda_codec_get_pin_target(codec
, pin
);
5303 snd_hda_activate_path(codec
, path
, path
->active
,
5304 aamix_default(spec
));
5308 static void init_aamix_paths(struct hda_codec
*codec
)
5310 struct hda_gen_spec
*spec
= codec
->spec
;
5312 if (!spec
->have_aamix_ctl
)
5314 update_aamix_paths(codec
, spec
->aamix_mode
, spec
->out_paths
[0],
5315 spec
->aamix_out_paths
[0],
5316 spec
->autocfg
.line_out_type
);
5317 update_aamix_paths(codec
, spec
->aamix_mode
, spec
->hp_paths
[0],
5318 spec
->aamix_out_paths
[1],
5320 update_aamix_paths(codec
, spec
->aamix_mode
, spec
->speaker_paths
[0],
5321 spec
->aamix_out_paths
[2],
5322 AUTO_PIN_SPEAKER_OUT
);
5325 /* set up input pins and loopback paths */
5326 static void init_analog_input(struct hda_codec
*codec
)
5328 struct hda_gen_spec
*spec
= codec
->spec
;
5329 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
5332 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
5333 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
5334 if (is_input_pin(codec
, nid
))
5335 restore_pin_ctl(codec
, nid
);
5337 /* init loopback inputs */
5338 if (spec
->mixer_nid
) {
5339 resume_path_from_idx(codec
, spec
->loopback_paths
[i
]);
5340 resume_path_from_idx(codec
, spec
->loopback_merge_path
);
5345 /* initialize ADC paths */
5346 static void init_input_src(struct hda_codec
*codec
)
5348 struct hda_gen_spec
*spec
= codec
->spec
;
5349 struct hda_input_mux
*imux
= &spec
->input_mux
;
5350 struct nid_path
*path
;
5353 if (spec
->dyn_adc_switch
)
5356 nums
= spec
->num_adc_nids
;
5358 for (c
= 0; c
< nums
; c
++) {
5359 for (i
= 0; i
< imux
->num_items
; i
++) {
5360 path
= get_input_path(codec
, c
, i
);
5362 bool active
= path
->active
;
5363 if (i
== spec
->cur_mux
[c
])
5365 snd_hda_activate_path(codec
, path
, active
, false);
5369 update_hp_mic(codec
, c
, true);
5372 if (spec
->cap_sync_hook
)
5373 spec
->cap_sync_hook(codec
, NULL
, NULL
);
5376 /* set right pin controls for digital I/O */
5377 static void init_digital(struct hda_codec
*codec
)
5379 struct hda_gen_spec
*spec
= codec
->spec
;
5383 for (i
= 0; i
< spec
->autocfg
.dig_outs
; i
++)
5384 set_output_and_unmute(codec
, spec
->digout_paths
[i
]);
5385 pin
= spec
->autocfg
.dig_in_pin
;
5387 restore_pin_ctl(codec
, pin
);
5388 resume_path_from_idx(codec
, spec
->digin_path
);
5392 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5393 * invalid unsol tags by some reason
5395 static void clear_unsol_on_unused_pins(struct hda_codec
*codec
)
5399 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
5400 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
5401 hda_nid_t nid
= pin
->nid
;
5402 if (is_jack_detectable(codec
, nid
) &&
5403 !snd_hda_jack_tbl_get(codec
, nid
))
5404 snd_hda_codec_update_cache(codec
, nid
, 0,
5405 AC_VERB_SET_UNSOLICITED_ENABLE
, 0);
5410 * snd_hda_gen_init - initialize the generic spec
5411 * @codec: the HDA codec
5413 * This can be put as patch_ops init function.
5415 int snd_hda_gen_init(struct hda_codec
*codec
)
5417 struct hda_gen_spec
*spec
= codec
->spec
;
5419 if (spec
->init_hook
)
5420 spec
->init_hook(codec
);
5422 snd_hda_apply_verbs(codec
);
5424 codec
->cached_write
= 1;
5426 init_multi_out(codec
);
5427 init_extra_out(codec
);
5428 init_multi_io(codec
);
5429 init_aamix_paths(codec
);
5430 init_analog_input(codec
);
5431 init_input_src(codec
);
5432 init_digital(codec
);
5434 clear_unsol_on_unused_pins(codec
);
5436 /* call init functions of standard auto-mute helpers */
5437 update_automute_all(codec
);
5439 snd_hda_codec_flush_cache(codec
);
5441 if (spec
->vmaster_mute
.sw_kctl
&& spec
->vmaster_mute
.hook
)
5442 snd_hda_sync_vmaster_hook(&spec
->vmaster_mute
);
5444 hda_call_check_power_status(codec
, 0x01);
5447 EXPORT_SYMBOL_GPL(snd_hda_gen_init
);
5450 * snd_hda_gen_free - free the generic spec
5451 * @codec: the HDA codec
5453 * This can be put as patch_ops free function.
5455 void snd_hda_gen_free(struct hda_codec
*codec
)
5457 snd_hda_apply_fixup(codec
, HDA_FIXUP_ACT_FREE
);
5458 snd_hda_gen_spec_free(codec
->spec
);
5462 EXPORT_SYMBOL_GPL(snd_hda_gen_free
);
5466 * snd_hda_gen_check_power_status - check the loopback power save state
5467 * @codec: the HDA codec
5468 * @nid: NID to inspect
5470 * This can be put as patch_ops check_power_status function.
5472 int snd_hda_gen_check_power_status(struct hda_codec
*codec
, hda_nid_t nid
)
5474 struct hda_gen_spec
*spec
= codec
->spec
;
5475 return snd_hda_check_amp_list_power(codec
, &spec
->loopback
, nid
);
5477 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status
);
5482 * the generic codec support
5485 static const struct hda_codec_ops generic_patch_ops
= {
5486 .build_controls
= snd_hda_gen_build_controls
,
5487 .build_pcms
= snd_hda_gen_build_pcms
,
5488 .init
= snd_hda_gen_init
,
5489 .free
= snd_hda_gen_free
,
5490 .unsol_event
= snd_hda_jack_unsol_event
,
5492 .check_power_status
= snd_hda_gen_check_power_status
,
5497 * snd_hda_parse_generic_codec - Generic codec parser
5498 * @codec: the HDA codec
5500 * This should be called from the HDA codec core.
5502 int snd_hda_parse_generic_codec(struct hda_codec
*codec
)
5504 struct hda_gen_spec
*spec
;
5507 spec
= kzalloc(sizeof(*spec
), GFP_KERNEL
);
5510 snd_hda_gen_spec_init(spec
);
5513 err
= snd_hda_parse_pin_defcfg(codec
, &spec
->autocfg
, NULL
, 0);
5517 err
= snd_hda_gen_parse_auto_config(codec
, &spec
->autocfg
);
5521 codec
->patch_ops
= generic_patch_ops
;
5525 snd_hda_gen_free(codec
);
5528 EXPORT_SYMBOL_GPL(snd_hda_parse_generic_codec
);
5530 MODULE_LICENSE("GPL");
5531 MODULE_DESCRIPTION("Generic HD-audio codec parser");