1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Universal Interface for Intel High Definition Audio Codec
5 * Generic widget tree parser
7 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/export.h>
13 #include <linux/sort.h>
14 #include <linux/delay.h>
15 #include <linux/ctype.h>
16 #include <linux/string.h>
17 #include <linux/bitops.h>
18 #include <linux/module.h>
19 #include <linux/leds.h>
20 #include <sound/core.h>
21 #include <sound/jack.h>
22 #include <sound/tlv.h>
23 #include <sound/hda_codec.h>
24 #include "hda_local.h"
25 #include "hda_auto_parser.h"
28 #include "hda_generic.h"
32 * snd_hda_gen_spec_init - initialize hda_gen_spec struct
33 * @spec: hda_gen_spec object to initialize
35 * Initialize the given hda_gen_spec object.
37 int snd_hda_gen_spec_init(struct hda_gen_spec
*spec
)
39 snd_array_init(&spec
->kctls
, sizeof(struct snd_kcontrol_new
), 32);
40 snd_array_init(&spec
->paths
, sizeof(struct nid_path
), 8);
41 snd_array_init(&spec
->loopback_list
, sizeof(struct hda_amp_list
), 8);
42 mutex_init(&spec
->pcm_mutex
);
45 EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init
);
48 * snd_hda_gen_add_kctl - Add a new kctl_new struct from the template
49 * @spec: hda_gen_spec object
50 * @name: name string to override the template, NULL if unchanged
51 * @temp: template for the new kctl
53 * Add a new kctl (actually snd_kcontrol_new to be instantiated later)
54 * element based on the given snd_kcontrol_new template @temp and the
55 * name string @name to the list in @spec.
56 * Returns the newly created object or NULL as error.
58 struct snd_kcontrol_new
*
59 snd_hda_gen_add_kctl(struct hda_gen_spec
*spec
, const char *name
,
60 const struct snd_kcontrol_new
*temp
)
62 struct snd_kcontrol_new
*knew
= snd_array_new(&spec
->kctls
);
67 knew
->name
= kstrdup(name
, GFP_KERNEL
);
69 knew
->name
= kstrdup(knew
->name
, GFP_KERNEL
);
74 EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl
);
76 static void free_kctls(struct hda_gen_spec
*spec
)
78 if (spec
->kctls
.list
) {
79 struct snd_kcontrol_new
*kctl
= spec
->kctls
.list
;
81 for (i
= 0; i
< spec
->kctls
.used
; i
++)
84 snd_array_free(&spec
->kctls
);
87 static void snd_hda_gen_spec_free(struct hda_gen_spec
*spec
)
92 snd_array_free(&spec
->paths
);
93 snd_array_free(&spec
->loopback_list
);
99 static void parse_user_hints(struct hda_codec
*codec
)
101 struct hda_gen_spec
*spec
= codec
->spec
;
104 val
= snd_hda_get_bool_hint(codec
, "jack_detect");
106 codec
->no_jack_detect
= !val
;
107 val
= snd_hda_get_bool_hint(codec
, "inv_jack_detect");
109 codec
->inv_jack_detect
= !!val
;
110 val
= snd_hda_get_bool_hint(codec
, "trigger_sense");
112 codec
->no_trigger_sense
= !val
;
113 val
= snd_hda_get_bool_hint(codec
, "inv_eapd");
115 codec
->inv_eapd
= !!val
;
116 val
= snd_hda_get_bool_hint(codec
, "pcm_format_first");
118 codec
->pcm_format_first
= !!val
;
119 val
= snd_hda_get_bool_hint(codec
, "sticky_stream");
121 codec
->no_sticky_stream
= !val
;
122 val
= snd_hda_get_bool_hint(codec
, "spdif_status_reset");
124 codec
->spdif_status_reset
= !!val
;
125 val
= snd_hda_get_bool_hint(codec
, "pin_amp_workaround");
127 codec
->pin_amp_workaround
= !!val
;
128 val
= snd_hda_get_bool_hint(codec
, "single_adc_amp");
130 codec
->single_adc_amp
= !!val
;
131 val
= snd_hda_get_bool_hint(codec
, "power_save_node");
133 codec
->power_save_node
= !!val
;
135 val
= snd_hda_get_bool_hint(codec
, "auto_mute");
137 spec
->suppress_auto_mute
= !val
;
138 val
= snd_hda_get_bool_hint(codec
, "auto_mic");
140 spec
->suppress_auto_mic
= !val
;
141 val
= snd_hda_get_bool_hint(codec
, "line_in_auto_switch");
143 spec
->line_in_auto_switch
= !!val
;
144 val
= snd_hda_get_bool_hint(codec
, "auto_mute_via_amp");
146 spec
->auto_mute_via_amp
= !!val
;
147 val
= snd_hda_get_bool_hint(codec
, "need_dac_fix");
149 spec
->need_dac_fix
= !!val
;
150 val
= snd_hda_get_bool_hint(codec
, "primary_hp");
152 spec
->no_primary_hp
= !val
;
153 val
= snd_hda_get_bool_hint(codec
, "multi_io");
155 spec
->no_multi_io
= !val
;
156 val
= snd_hda_get_bool_hint(codec
, "multi_cap_vol");
158 spec
->multi_cap_vol
= !!val
;
159 val
= snd_hda_get_bool_hint(codec
, "inv_dmic_split");
161 spec
->inv_dmic_split
= !!val
;
162 val
= snd_hda_get_bool_hint(codec
, "indep_hp");
164 spec
->indep_hp
= !!val
;
165 val
= snd_hda_get_bool_hint(codec
, "add_stereo_mix_input");
167 spec
->add_stereo_mix_input
= !!val
;
168 /* the following two are just for compatibility */
169 val
= snd_hda_get_bool_hint(codec
, "add_out_jack_modes");
171 spec
->add_jack_modes
= !!val
;
172 val
= snd_hda_get_bool_hint(codec
, "add_in_jack_modes");
174 spec
->add_jack_modes
= !!val
;
175 val
= snd_hda_get_bool_hint(codec
, "add_jack_modes");
177 spec
->add_jack_modes
= !!val
;
178 val
= snd_hda_get_bool_hint(codec
, "power_down_unused");
180 spec
->power_down_unused
= !!val
;
181 val
= snd_hda_get_bool_hint(codec
, "add_hp_mic");
183 spec
->hp_mic
= !!val
;
184 val
= snd_hda_get_bool_hint(codec
, "hp_mic_detect");
186 spec
->suppress_hp_mic_detect
= !val
;
187 val
= snd_hda_get_bool_hint(codec
, "vmaster");
189 spec
->suppress_vmaster
= !val
;
191 if (!snd_hda_get_int_hint(codec
, "mixer_nid", &val
))
192 spec
->mixer_nid
= val
;
196 * pin control value accesses
199 #define update_pin_ctl(codec, pin, val) \
200 snd_hda_codec_write_cache(codec, pin, 0, \
201 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
203 /* restore the pinctl based on the cached value */
204 static inline void restore_pin_ctl(struct hda_codec
*codec
, hda_nid_t pin
)
206 update_pin_ctl(codec
, pin
, snd_hda_codec_get_pin_target(codec
, pin
));
209 /* set the pinctl target value and write it if requested */
210 static void set_pin_target(struct hda_codec
*codec
, hda_nid_t pin
,
211 unsigned int val
, bool do_write
)
215 val
= snd_hda_correct_pin_ctl(codec
, pin
, val
);
216 snd_hda_codec_set_pin_target(codec
, pin
, val
);
218 update_pin_ctl(codec
, pin
, val
);
221 /* set pinctl target values for all given pins */
222 static void set_pin_targets(struct hda_codec
*codec
, int num_pins
,
223 hda_nid_t
*pins
, unsigned int val
)
226 for (i
= 0; i
< num_pins
; i
++)
227 set_pin_target(codec
, pins
[i
], val
, false);
234 /* return the position of NID in the list, or -1 if not found */
235 static int find_idx_in_nid_list(hda_nid_t nid
, const hda_nid_t
*list
, int nums
)
238 for (i
= 0; i
< nums
; i
++)
244 /* return true if the given NID is contained in the path */
245 static bool is_nid_contained(struct nid_path
*path
, hda_nid_t nid
)
247 return find_idx_in_nid_list(nid
, path
->path
, path
->depth
) >= 0;
250 static struct nid_path
*get_nid_path(struct hda_codec
*codec
,
251 hda_nid_t from_nid
, hda_nid_t to_nid
,
254 struct hda_gen_spec
*spec
= codec
->spec
;
255 struct nid_path
*path
;
258 snd_array_for_each(&spec
->paths
, i
, path
) {
259 if (path
->depth
<= 0)
261 if ((!from_nid
|| path
->path
[0] == from_nid
) &&
262 (!to_nid
|| path
->path
[path
->depth
- 1] == to_nid
)) {
264 (anchor_nid
> 0 && is_nid_contained(path
, anchor_nid
)) ||
265 (anchor_nid
< 0 && !is_nid_contained(path
, anchor_nid
)))
273 * snd_hda_get_path_idx - get the index number corresponding to the path
275 * @codec: the HDA codec
276 * @path: nid_path object
278 * The returned index starts from 1, i.e. the actual array index with offset 1,
279 * and zero is handled as an invalid path
281 int snd_hda_get_path_idx(struct hda_codec
*codec
, struct nid_path
*path
)
283 struct hda_gen_spec
*spec
= codec
->spec
;
284 struct nid_path
*array
= spec
->paths
.list
;
287 if (!spec
->paths
.used
)
290 if (idx
< 0 || idx
>= spec
->paths
.used
)
294 EXPORT_SYMBOL_GPL(snd_hda_get_path_idx
);
297 * snd_hda_get_path_from_idx - get the path instance corresponding to the
299 * @codec: the HDA codec
300 * @idx: the path index
302 struct nid_path
*snd_hda_get_path_from_idx(struct hda_codec
*codec
, int idx
)
304 struct hda_gen_spec
*spec
= codec
->spec
;
306 if (idx
<= 0 || idx
> spec
->paths
.used
)
308 return snd_array_elem(&spec
->paths
, idx
- 1);
310 EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx
);
312 /* check whether the given DAC is already found in any existing paths */
313 static bool is_dac_already_used(struct hda_codec
*codec
, hda_nid_t nid
)
315 struct hda_gen_spec
*spec
= codec
->spec
;
316 const struct nid_path
*path
;
319 snd_array_for_each(&spec
->paths
, i
, path
) {
320 if (path
->path
[0] == nid
)
326 /* check whether the given two widgets can be connected */
327 static bool is_reachable_path(struct hda_codec
*codec
,
328 hda_nid_t from_nid
, hda_nid_t to_nid
)
330 if (!from_nid
|| !to_nid
)
332 return snd_hda_get_conn_index(codec
, to_nid
, from_nid
, true) >= 0;
335 /* nid, dir and idx */
336 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
338 /* check whether the given ctl is already assigned in any path elements */
339 static bool is_ctl_used(struct hda_codec
*codec
, unsigned int val
, int type
)
341 struct hda_gen_spec
*spec
= codec
->spec
;
342 const struct nid_path
*path
;
345 val
&= AMP_VAL_COMPARE_MASK
;
346 snd_array_for_each(&spec
->paths
, i
, path
) {
347 if ((path
->ctls
[type
] & AMP_VAL_COMPARE_MASK
) == val
)
353 /* check whether a control with the given (nid, dir, idx) was assigned */
354 static bool is_ctl_associated(struct hda_codec
*codec
, hda_nid_t nid
,
355 int dir
, int idx
, int type
)
357 unsigned int val
= HDA_COMPOSE_AMP_VAL(nid
, 3, idx
, dir
);
358 return is_ctl_used(codec
, val
, type
);
361 static void print_nid_path(struct hda_codec
*codec
,
362 const char *pfx
, struct nid_path
*path
)
369 for (i
= 0; i
< path
->depth
; i
++)
370 pos
+= scnprintf(pos
, sizeof(buf
) - (pos
- buf
), "%s%02x",
371 pos
!= buf
? ":" : "",
374 codec_dbg(codec
, "%s path: depth=%d '%s'\n", pfx
, path
->depth
, buf
);
377 /* called recursively */
378 static bool __parse_nid_path(struct hda_codec
*codec
,
379 hda_nid_t from_nid
, hda_nid_t to_nid
,
380 int anchor_nid
, struct nid_path
*path
,
383 const hda_nid_t
*conn
;
386 if (to_nid
== anchor_nid
)
387 anchor_nid
= 0; /* anchor passed */
388 else if (to_nid
== (hda_nid_t
)(-anchor_nid
))
389 return false; /* hit the exclusive nid */
391 nums
= snd_hda_get_conn_list(codec
, to_nid
, &conn
);
392 for (i
= 0; i
< nums
; i
++) {
393 if (conn
[i
] != from_nid
) {
394 /* special case: when from_nid is 0,
395 * try to find an empty DAC
398 get_wcaps_type(get_wcaps(codec
, conn
[i
])) != AC_WID_AUD_OUT
||
399 is_dac_already_used(codec
, conn
[i
]))
402 /* anchor is not requested or already passed? */
406 if (depth
>= MAX_NID_PATH_DEPTH
)
408 for (i
= 0; i
< nums
; i
++) {
410 type
= get_wcaps_type(get_wcaps(codec
, conn
[i
]));
411 if (type
== AC_WID_AUD_OUT
|| type
== AC_WID_AUD_IN
||
414 if (__parse_nid_path(codec
, from_nid
, conn
[i
],
415 anchor_nid
, path
, depth
+ 1))
421 path
->path
[path
->depth
] = conn
[i
];
422 path
->idx
[path
->depth
+ 1] = i
;
423 if (nums
> 1 && get_wcaps_type(get_wcaps(codec
, to_nid
)) != AC_WID_AUD_MIX
)
424 path
->multi
[path
->depth
+ 1] = 1;
430 * snd_hda_parse_nid_path - parse the widget path from the given nid to
432 * @codec: the HDA codec
433 * @from_nid: the NID where the path start from
434 * @to_nid: the NID where the path ends at
435 * @anchor_nid: the anchor indication
436 * @path: the path object to store the result
438 * Returns true if a matching path is found.
440 * The parsing behavior depends on parameters:
441 * when @from_nid is 0, try to find an empty DAC;
442 * when @anchor_nid is set to a positive value, only paths through the widget
443 * with the given value are evaluated.
444 * when @anchor_nid is set to a negative value, paths through the widget
445 * with the negative of given value are excluded, only other paths are chosen.
446 * when @anchor_nid is zero, no special handling about path selection.
448 static bool snd_hda_parse_nid_path(struct hda_codec
*codec
, hda_nid_t from_nid
,
449 hda_nid_t to_nid
, int anchor_nid
,
450 struct nid_path
*path
)
452 if (__parse_nid_path(codec
, from_nid
, to_nid
, anchor_nid
, path
, 1)) {
453 path
->path
[path
->depth
] = to_nid
;
461 * snd_hda_add_new_path - parse the path between the given NIDs and
462 * add to the path list
463 * @codec: the HDA codec
464 * @from_nid: the NID where the path start from
465 * @to_nid: the NID where the path ends at
466 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
468 * If no valid path is found, returns NULL.
471 snd_hda_add_new_path(struct hda_codec
*codec
, hda_nid_t from_nid
,
472 hda_nid_t to_nid
, int anchor_nid
)
474 struct hda_gen_spec
*spec
= codec
->spec
;
475 struct nid_path
*path
;
477 if (from_nid
&& to_nid
&& !is_reachable_path(codec
, from_nid
, to_nid
))
480 /* check whether the path has been already added */
481 path
= get_nid_path(codec
, from_nid
, to_nid
, anchor_nid
);
485 path
= snd_array_new(&spec
->paths
);
488 memset(path
, 0, sizeof(*path
));
489 if (snd_hda_parse_nid_path(codec
, from_nid
, to_nid
, anchor_nid
, path
))
495 EXPORT_SYMBOL_GPL(snd_hda_add_new_path
);
497 /* clear the given path as invalid so that it won't be picked up later */
498 static void invalidate_nid_path(struct hda_codec
*codec
, int idx
)
500 struct nid_path
*path
= snd_hda_get_path_from_idx(codec
, idx
);
503 memset(path
, 0, sizeof(*path
));
506 /* return a DAC if paired to the given pin by codec driver */
507 static hda_nid_t
get_preferred_dac(struct hda_codec
*codec
, hda_nid_t pin
)
509 struct hda_gen_spec
*spec
= codec
->spec
;
510 const hda_nid_t
*list
= spec
->preferred_dacs
;
514 for (; *list
; list
+= 2)
520 /* look for an empty DAC slot */
521 static hda_nid_t
look_for_dac(struct hda_codec
*codec
, hda_nid_t pin
,
524 struct hda_gen_spec
*spec
= codec
->spec
;
528 for (i
= 0; i
< spec
->num_all_dacs
; i
++) {
529 hda_nid_t nid
= spec
->all_dacs
[i
];
530 if (!nid
|| is_dac_already_used(codec
, nid
))
532 cap_digital
= !!(get_wcaps(codec
, nid
) & AC_WCAP_DIGITAL
);
533 if (is_digital
!= cap_digital
)
535 if (is_reachable_path(codec
, nid
, pin
))
541 /* replace the channels in the composed amp value with the given number */
542 static unsigned int amp_val_replace_channels(unsigned int val
, unsigned int chs
)
544 val
&= ~(0x3U
<< 16);
549 static bool same_amp_caps(struct hda_codec
*codec
, hda_nid_t nid1
,
550 hda_nid_t nid2
, int dir
)
552 if (!(get_wcaps(codec
, nid1
) & (1 << (dir
+ 1))))
553 return !(get_wcaps(codec
, nid2
) & (1 << (dir
+ 1)));
554 return (query_amp_caps(codec
, nid1
, dir
) ==
555 query_amp_caps(codec
, nid2
, dir
));
558 /* look for a widget suitable for assigning a mute switch in the path */
559 static hda_nid_t
look_for_out_mute_nid(struct hda_codec
*codec
,
560 struct nid_path
*path
)
564 for (i
= path
->depth
- 1; i
>= 0; i
--) {
565 if (nid_has_mute(codec
, path
->path
[i
], HDA_OUTPUT
))
566 return path
->path
[i
];
567 if (i
!= path
->depth
- 1 && i
!= 0 &&
568 nid_has_mute(codec
, path
->path
[i
], HDA_INPUT
))
569 return path
->path
[i
];
574 /* look for a widget suitable for assigning a volume ctl in the path */
575 static hda_nid_t
look_for_out_vol_nid(struct hda_codec
*codec
,
576 struct nid_path
*path
)
578 struct hda_gen_spec
*spec
= codec
->spec
;
581 for (i
= path
->depth
- 1; i
>= 0; i
--) {
582 hda_nid_t nid
= path
->path
[i
];
583 if ((spec
->out_vol_mask
>> nid
) & 1)
585 if (nid_has_volume(codec
, nid
, HDA_OUTPUT
))
592 * path activation / deactivation
595 /* can have the amp-in capability? */
596 static bool has_amp_in(struct hda_codec
*codec
, struct nid_path
*path
, int idx
)
598 hda_nid_t nid
= path
->path
[idx
];
599 unsigned int caps
= get_wcaps(codec
, nid
);
600 unsigned int type
= get_wcaps_type(caps
);
602 if (!(caps
& AC_WCAP_IN_AMP
))
604 if (type
== AC_WID_PIN
&& idx
> 0) /* only for input pins */
609 /* can have the amp-out capability? */
610 static bool has_amp_out(struct hda_codec
*codec
, struct nid_path
*path
, int idx
)
612 hda_nid_t nid
= path
->path
[idx
];
613 unsigned int caps
= get_wcaps(codec
, nid
);
614 unsigned int type
= get_wcaps_type(caps
);
616 if (!(caps
& AC_WCAP_OUT_AMP
))
618 if (type
== AC_WID_PIN
&& !idx
) /* only for output pins */
623 /* check whether the given (nid,dir,idx) is active */
624 static bool is_active_nid(struct hda_codec
*codec
, hda_nid_t nid
,
625 unsigned int dir
, unsigned int idx
)
627 struct hda_gen_spec
*spec
= codec
->spec
;
628 int type
= get_wcaps_type(get_wcaps(codec
, nid
));
629 const struct nid_path
*path
;
632 if (nid
== codec
->core
.afg
)
635 snd_array_for_each(&spec
->paths
, n
, path
) {
638 if (codec
->power_save_node
) {
639 if (!path
->stream_enabled
)
641 /* ignore unplugged paths except for DAC/ADC */
642 if (!(path
->pin_enabled
|| path
->pin_fixed
) &&
643 type
!= AC_WID_AUD_OUT
&& type
!= AC_WID_AUD_IN
)
646 for (i
= 0; i
< path
->depth
; i
++) {
647 if (path
->path
[i
] == nid
) {
648 if (dir
== HDA_OUTPUT
|| idx
== -1 ||
658 /* check whether the NID is referred by any active paths */
659 #define is_active_nid_for_any(codec, nid) \
660 is_active_nid(codec, nid, HDA_OUTPUT, -1)
662 /* get the default amp value for the target state */
663 static int get_amp_val_to_activate(struct hda_codec
*codec
, hda_nid_t nid
,
664 int dir
, unsigned int caps
, bool enable
)
666 unsigned int val
= 0;
668 if (caps
& AC_AMPCAP_NUM_STEPS
) {
671 val
= (caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
;
673 if (caps
& (AC_AMPCAP_MUTE
| AC_AMPCAP_MIN_MUTE
)) {
680 /* is this a stereo widget or a stereo-to-mono mix? */
681 static bool is_stereo_amps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
)
683 unsigned int wcaps
= get_wcaps(codec
, nid
);
686 if (wcaps
& AC_WCAP_STEREO
)
688 if (dir
!= HDA_INPUT
|| get_wcaps_type(wcaps
) != AC_WID_AUD_MIX
)
690 if (snd_hda_get_num_conns(codec
, nid
) != 1)
692 if (snd_hda_get_connections(codec
, nid
, &conn
, 1) < 0)
694 return !!(get_wcaps(codec
, conn
) & AC_WCAP_STEREO
);
697 /* initialize the amp value (only at the first time) */
698 static void init_amp(struct hda_codec
*codec
, hda_nid_t nid
, int dir
, int idx
)
700 unsigned int caps
= query_amp_caps(codec
, nid
, dir
);
701 int val
= get_amp_val_to_activate(codec
, nid
, dir
, caps
, false);
703 if (is_stereo_amps(codec
, nid
, dir
))
704 snd_hda_codec_amp_init_stereo(codec
, nid
, dir
, idx
, 0xff, val
);
706 snd_hda_codec_amp_init(codec
, nid
, 0, dir
, idx
, 0xff, val
);
709 /* update the amp, doing in stereo or mono depending on NID */
710 static int update_amp(struct hda_codec
*codec
, hda_nid_t nid
, int dir
, int idx
,
711 unsigned int mask
, unsigned int val
)
713 if (is_stereo_amps(codec
, nid
, dir
))
714 return snd_hda_codec_amp_stereo(codec
, nid
, dir
, idx
,
717 return snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
721 /* calculate amp value mask we can modify;
722 * if the given amp is controlled by mixers, don't touch it
724 static unsigned int get_amp_mask_to_modify(struct hda_codec
*codec
,
725 hda_nid_t nid
, int dir
, int idx
,
728 unsigned int mask
= 0xff;
730 if (caps
& (AC_AMPCAP_MUTE
| AC_AMPCAP_MIN_MUTE
)) {
731 if (is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_MUTE_CTL
))
734 if (caps
& AC_AMPCAP_NUM_STEPS
) {
735 if (is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_VOL_CTL
) ||
736 is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_BOOST_CTL
))
742 static void activate_amp(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
743 int idx
, int idx_to_check
, bool enable
)
746 unsigned int mask
, val
;
748 caps
= query_amp_caps(codec
, nid
, dir
);
749 val
= get_amp_val_to_activate(codec
, nid
, dir
, caps
, enable
);
750 mask
= get_amp_mask_to_modify(codec
, nid
, dir
, idx_to_check
, caps
);
755 update_amp(codec
, nid
, dir
, idx
, mask
, val
);
758 static void check_and_activate_amp(struct hda_codec
*codec
, hda_nid_t nid
,
759 int dir
, int idx
, int idx_to_check
,
762 /* check whether the given amp is still used by others */
763 if (!enable
&& is_active_nid(codec
, nid
, dir
, idx_to_check
))
765 activate_amp(codec
, nid
, dir
, idx
, idx_to_check
, enable
);
768 static void activate_amp_out(struct hda_codec
*codec
, struct nid_path
*path
,
771 hda_nid_t nid
= path
->path
[i
];
772 init_amp(codec
, nid
, HDA_OUTPUT
, 0);
773 check_and_activate_amp(codec
, nid
, HDA_OUTPUT
, 0, 0, enable
);
776 static void activate_amp_in(struct hda_codec
*codec
, struct nid_path
*path
,
777 int i
, bool enable
, bool add_aamix
)
779 struct hda_gen_spec
*spec
= codec
->spec
;
780 const hda_nid_t
*conn
;
783 hda_nid_t nid
= path
->path
[i
];
785 nums
= snd_hda_get_conn_list(codec
, nid
, &conn
);
788 type
= get_wcaps_type(get_wcaps(codec
, nid
));
789 if (type
== AC_WID_PIN
||
790 (type
== AC_WID_AUD_IN
&& codec
->single_adc_amp
)) {
796 for (n
= 0; n
< nums
; n
++)
797 init_amp(codec
, nid
, HDA_INPUT
, n
);
799 /* here is a little bit tricky in comparison with activate_amp_out();
800 * when aa-mixer is available, we need to enable the path as well
802 for (n
= 0; n
< nums
; n
++) {
804 if (conn
[n
] != spec
->mixer_merge_nid
)
806 /* when aamix is disabled, force to off */
808 activate_amp(codec
, nid
, HDA_INPUT
, n
, n
, false);
812 check_and_activate_amp(codec
, nid
, HDA_INPUT
, n
, idx
, enable
);
816 /* sync power of each widget in the given path */
817 static hda_nid_t
path_power_update(struct hda_codec
*codec
,
818 struct nid_path
*path
,
819 bool allow_powerdown
)
821 hda_nid_t nid
, changed
= 0;
824 for (i
= 0; i
< path
->depth
; i
++) {
826 if (!(get_wcaps(codec
, nid
) & AC_WCAP_POWER
))
828 if (nid
== codec
->core
.afg
)
830 if (!allow_powerdown
|| is_active_nid_for_any(codec
, nid
))
834 power
= snd_hda_codec_read(codec
, nid
, 0,
835 AC_VERB_GET_POWER_STATE
, 0);
836 if (power
!= (state
| (state
<< 4))) {
837 snd_hda_codec_write(codec
, nid
, 0,
838 AC_VERB_SET_POWER_STATE
, state
);
840 /* all known codecs seem to be capable to handl
841 * widgets state even in D3, so far.
842 * if any new codecs need to restore the widget
843 * states after D0 transition, call the function
847 if (state
== AC_PWRST_D0
)
848 snd_hdac_regmap_sync_node(&codec
->core
, nid
);
855 /* do sync with the last power state change */
856 static void sync_power_state_change(struct hda_codec
*codec
, hda_nid_t nid
)
860 snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_POWER_STATE
, 0);
865 * snd_hda_activate_path - activate or deactivate the given path
866 * @codec: the HDA codec
867 * @path: the path to activate/deactivate
868 * @enable: flag to activate or not
869 * @add_aamix: enable the input from aamix NID
871 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
873 void snd_hda_activate_path(struct hda_codec
*codec
, struct nid_path
*path
,
874 bool enable
, bool add_aamix
)
876 struct hda_gen_spec
*spec
= codec
->spec
;
879 path
->active
= enable
;
881 /* make sure the widget is powered up */
882 if (enable
&& (spec
->power_down_unused
|| codec
->power_save_node
))
883 path_power_update(codec
, path
, codec
->power_save_node
);
885 for (i
= path
->depth
- 1; i
>= 0; i
--) {
886 hda_nid_t nid
= path
->path
[i
];
888 if (enable
&& path
->multi
[i
])
889 snd_hda_codec_write_cache(codec
, nid
, 0,
890 AC_VERB_SET_CONNECT_SEL
,
892 if (has_amp_in(codec
, path
, i
))
893 activate_amp_in(codec
, path
, i
, enable
, add_aamix
);
894 if (has_amp_out(codec
, path
, i
))
895 activate_amp_out(codec
, path
, i
, enable
);
898 EXPORT_SYMBOL_GPL(snd_hda_activate_path
);
900 /* if the given path is inactive, put widgets into D3 (only if suitable) */
901 static void path_power_down_sync(struct hda_codec
*codec
, struct nid_path
*path
)
903 struct hda_gen_spec
*spec
= codec
->spec
;
905 if (!(spec
->power_down_unused
|| codec
->power_save_node
) || path
->active
)
907 sync_power_state_change(codec
, path_power_update(codec
, path
, true));
910 /* turn on/off EAPD on the given pin */
911 static void set_pin_eapd(struct hda_codec
*codec
, hda_nid_t pin
, bool enable
)
913 struct hda_gen_spec
*spec
= codec
->spec
;
914 if (spec
->own_eapd_ctl
||
915 !(snd_hda_query_pin_caps(codec
, pin
) & AC_PINCAP_EAPD
))
917 if (spec
->keep_eapd_on
&& !enable
)
921 snd_hda_codec_write_cache(codec
, pin
, 0,
922 AC_VERB_SET_EAPD_BTLENABLE
,
923 enable
? 0x02 : 0x00);
926 /* re-initialize the path specified by the given path index */
927 static void resume_path_from_idx(struct hda_codec
*codec
, int path_idx
)
929 struct nid_path
*path
= snd_hda_get_path_from_idx(codec
, path_idx
);
931 snd_hda_activate_path(codec
, path
, path
->active
, false);
936 * Helper functions for creating mixer ctl elements
939 static int hda_gen_mixer_mute_put(struct snd_kcontrol
*kcontrol
,
940 struct snd_ctl_elem_value
*ucontrol
);
941 static int hda_gen_bind_mute_get(struct snd_kcontrol
*kcontrol
,
942 struct snd_ctl_elem_value
*ucontrol
);
943 static int hda_gen_bind_mute_put(struct snd_kcontrol
*kcontrol
,
944 struct snd_ctl_elem_value
*ucontrol
);
951 static const struct snd_kcontrol_new control_templates
[] = {
952 HDA_CODEC_VOLUME(NULL
, 0, 0, 0),
953 /* only the put callback is replaced for handling the special mute */
955 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
956 .subdevice
= HDA_SUBDEV_AMP_FLAG
,
957 .info
= snd_hda_mixer_amp_switch_info
,
958 .get
= snd_hda_mixer_amp_switch_get
,
959 .put
= hda_gen_mixer_mute_put
, /* replaced */
960 .private_value
= HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
963 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
964 .info
= snd_hda_mixer_amp_switch_info
,
965 .get
= hda_gen_bind_mute_get
,
966 .put
= hda_gen_bind_mute_put
, /* replaced */
967 .private_value
= HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
971 /* add dynamic controls from template */
972 static struct snd_kcontrol_new
*
973 add_control(struct hda_gen_spec
*spec
, int type
, const char *name
,
974 int cidx
, unsigned long val
)
976 struct snd_kcontrol_new
*knew
;
978 knew
= snd_hda_gen_add_kctl(spec
, name
, &control_templates
[type
]);
982 if (get_amp_nid_(val
))
983 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
984 knew
->private_value
= val
;
988 static int add_control_with_pfx(struct hda_gen_spec
*spec
, int type
,
989 const char *pfx
, const char *dir
,
990 const char *sfx
, int cidx
, unsigned long val
)
992 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
993 snprintf(name
, sizeof(name
), "%s %s %s", pfx
, dir
, sfx
);
994 if (!add_control(spec
, type
, name
, cidx
, val
))
999 #define add_pb_vol_ctrl(spec, type, pfx, val) \
1000 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1001 #define add_pb_sw_ctrl(spec, type, pfx, val) \
1002 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1003 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1004 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1005 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1006 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1008 static int add_vol_ctl(struct hda_codec
*codec
, const char *pfx
, int cidx
,
1009 unsigned int chs
, struct nid_path
*path
)
1014 val
= path
->ctls
[NID_PATH_VOL_CTL
];
1017 val
= amp_val_replace_channels(val
, chs
);
1018 return __add_pb_vol_ctrl(codec
->spec
, HDA_CTL_WIDGET_VOL
, pfx
, cidx
, val
);
1021 /* return the channel bits suitable for the given path->ctls[] */
1022 static int get_default_ch_nums(struct hda_codec
*codec
, struct nid_path
*path
,
1025 int chs
= 1; /* mono (left only) */
1027 hda_nid_t nid
= get_amp_nid_(path
->ctls
[type
]);
1028 if (nid
&& (get_wcaps(codec
, nid
) & AC_WCAP_STEREO
))
1029 chs
= 3; /* stereo */
1034 static int add_stereo_vol(struct hda_codec
*codec
, const char *pfx
, int cidx
,
1035 struct nid_path
*path
)
1037 int chs
= get_default_ch_nums(codec
, path
, NID_PATH_VOL_CTL
);
1038 return add_vol_ctl(codec
, pfx
, cidx
, chs
, path
);
1041 /* create a mute-switch for the given mixer widget;
1042 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1044 static int add_sw_ctl(struct hda_codec
*codec
, const char *pfx
, int cidx
,
1045 unsigned int chs
, struct nid_path
*path
)
1048 int type
= HDA_CTL_WIDGET_MUTE
;
1052 val
= path
->ctls
[NID_PATH_MUTE_CTL
];
1055 val
= amp_val_replace_channels(val
, chs
);
1056 if (get_amp_direction_(val
) == HDA_INPUT
) {
1057 hda_nid_t nid
= get_amp_nid_(val
);
1058 int nums
= snd_hda_get_num_conns(codec
, nid
);
1060 type
= HDA_CTL_BIND_MUTE
;
1064 return __add_pb_sw_ctrl(codec
->spec
, type
, pfx
, cidx
, val
);
1067 static int add_stereo_sw(struct hda_codec
*codec
, const char *pfx
,
1068 int cidx
, struct nid_path
*path
)
1070 int chs
= get_default_ch_nums(codec
, path
, NID_PATH_MUTE_CTL
);
1071 return add_sw_ctl(codec
, pfx
, cidx
, chs
, path
);
1074 /* playback mute control with the software mute bit check */
1075 static void sync_auto_mute_bits(struct snd_kcontrol
*kcontrol
,
1076 struct snd_ctl_elem_value
*ucontrol
)
1078 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1079 struct hda_gen_spec
*spec
= codec
->spec
;
1081 if (spec
->auto_mute_via_amp
) {
1082 hda_nid_t nid
= get_amp_nid(kcontrol
);
1083 bool enabled
= !((spec
->mute_bits
>> nid
) & 1);
1084 ucontrol
->value
.integer
.value
[0] &= enabled
;
1085 ucontrol
->value
.integer
.value
[1] &= enabled
;
1089 static int hda_gen_mixer_mute_put(struct snd_kcontrol
*kcontrol
,
1090 struct snd_ctl_elem_value
*ucontrol
)
1092 sync_auto_mute_bits(kcontrol
, ucontrol
);
1093 return snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
1097 * Bound mute controls
1099 #define AMP_VAL_IDX_SHIFT 19
1100 #define AMP_VAL_IDX_MASK (0x0f<<19)
1102 static int hda_gen_bind_mute_get(struct snd_kcontrol
*kcontrol
,
1103 struct snd_ctl_elem_value
*ucontrol
)
1105 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1109 mutex_lock(&codec
->control_mutex
);
1110 pval
= kcontrol
->private_value
;
1111 kcontrol
->private_value
= pval
& ~AMP_VAL_IDX_MASK
; /* index 0 */
1112 err
= snd_hda_mixer_amp_switch_get(kcontrol
, ucontrol
);
1113 kcontrol
->private_value
= pval
;
1114 mutex_unlock(&codec
->control_mutex
);
1118 static int hda_gen_bind_mute_put(struct snd_kcontrol
*kcontrol
,
1119 struct snd_ctl_elem_value
*ucontrol
)
1121 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1123 int i
, indices
, err
= 0, change
= 0;
1125 sync_auto_mute_bits(kcontrol
, ucontrol
);
1127 mutex_lock(&codec
->control_mutex
);
1128 pval
= kcontrol
->private_value
;
1129 indices
= (pval
& AMP_VAL_IDX_MASK
) >> AMP_VAL_IDX_SHIFT
;
1130 for (i
= 0; i
< indices
; i
++) {
1131 kcontrol
->private_value
= (pval
& ~AMP_VAL_IDX_MASK
) |
1132 (i
<< AMP_VAL_IDX_SHIFT
);
1133 err
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
1138 kcontrol
->private_value
= pval
;
1139 mutex_unlock(&codec
->control_mutex
);
1140 return err
< 0 ? err
: change
;
1143 /* any ctl assigned to the path with the given index? */
1144 static bool path_has_mixer(struct hda_codec
*codec
, int path_idx
, int ctl_type
)
1146 struct nid_path
*path
= snd_hda_get_path_from_idx(codec
, path_idx
);
1147 return path
&& path
->ctls
[ctl_type
];
1150 static const char * const channel_name
[4] = {
1151 "Front", "Surround", "CLFE", "Side"
1154 /* give some appropriate ctl name prefix for the given line out channel */
1155 static const char *get_line_out_pfx(struct hda_codec
*codec
, int ch
,
1156 int *index
, int ctl_type
)
1158 struct hda_gen_spec
*spec
= codec
->spec
;
1159 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1162 if (cfg
->line_outs
== 1 && !spec
->multi_ios
&&
1163 !codec
->force_pin_prefix
&&
1164 !cfg
->hp_outs
&& !cfg
->speaker_outs
)
1165 return spec
->vmaster_mute
.hook
? "PCM" : "Master";
1167 /* if there is really a single DAC used in the whole output paths,
1168 * use it master (or "PCM" if a vmaster hook is present)
1170 if (spec
->multiout
.num_dacs
== 1 && !spec
->mixer_nid
&&
1171 !codec
->force_pin_prefix
&&
1172 !spec
->multiout
.hp_out_nid
[0] && !spec
->multiout
.extra_out_nid
[0])
1173 return spec
->vmaster_mute
.hook
? "PCM" : "Master";
1175 /* multi-io channels */
1176 if (ch
>= cfg
->line_outs
)
1177 return channel_name
[ch
];
1179 switch (cfg
->line_out_type
) {
1180 case AUTO_PIN_SPEAKER_OUT
:
1181 /* if the primary channel vol/mute is shared with HP volume,
1182 * don't name it as Speaker
1184 if (!ch
&& cfg
->hp_outs
&&
1185 !path_has_mixer(codec
, spec
->hp_paths
[0], ctl_type
))
1187 if (cfg
->line_outs
== 1)
1189 if (cfg
->line_outs
== 2)
1190 return ch
? "Bass Speaker" : "Speaker";
1192 case AUTO_PIN_HP_OUT
:
1193 /* if the primary channel vol/mute is shared with spk volume,
1194 * don't name it as Headphone
1196 if (!ch
&& cfg
->speaker_outs
&&
1197 !path_has_mixer(codec
, spec
->speaker_paths
[0], ctl_type
))
1199 /* for multi-io case, only the primary out */
1200 if (ch
&& spec
->multi_ios
)
1204 case AUTO_PIN_LINE_OUT
:
1205 /* This deals with the case where we have two DACs and
1206 * one LO, one HP and one Speaker */
1207 if (!ch
&& cfg
->speaker_outs
&& cfg
->hp_outs
) {
1208 bool hp_lo_shared
= !path_has_mixer(codec
, spec
->hp_paths
[0], ctl_type
);
1209 bool spk_lo_shared
= !path_has_mixer(codec
, spec
->speaker_paths
[0], ctl_type
);
1210 if (hp_lo_shared
&& spk_lo_shared
)
1211 return spec
->vmaster_mute
.hook
? "PCM" : "Master";
1213 return "Headphone+LO";
1215 return "Speaker+LO";
1219 /* for a single channel output, we don't have to name the channel */
1220 if (cfg
->line_outs
== 1 && !spec
->multi_ios
)
1223 if (ch
>= ARRAY_SIZE(channel_name
)) {
1228 return channel_name
[ch
];
1232 * Parse output paths
1235 /* badness definition */
1237 /* No primary DAC is found for the main output */
1238 BAD_NO_PRIMARY_DAC
= 0x10000,
1239 /* No DAC is found for the extra output */
1240 BAD_NO_DAC
= 0x4000,
1241 /* No possible multi-ios */
1242 BAD_MULTI_IO
= 0x120,
1243 /* No individual DAC for extra output */
1244 BAD_NO_EXTRA_DAC
= 0x102,
1245 /* No individual DAC for extra surrounds */
1246 BAD_NO_EXTRA_SURR_DAC
= 0x101,
1247 /* Primary DAC shared with main surrounds */
1248 BAD_SHARED_SURROUND
= 0x100,
1249 /* No independent HP possible */
1250 BAD_NO_INDEP_HP
= 0x10,
1251 /* Primary DAC shared with main CLFE */
1252 BAD_SHARED_CLFE
= 0x10,
1253 /* Primary DAC shared with extra surrounds */
1254 BAD_SHARED_EXTRA_SURROUND
= 0x10,
1255 /* Volume widget is shared */
1256 BAD_SHARED_VOL
= 0x10,
1259 /* look for widgets in the given path which are appropriate for
1260 * volume and mute controls, and assign the values to ctls[].
1262 * When no appropriate widget is found in the path, the badness value
1263 * is incremented depending on the situation. The function returns the
1264 * total badness for both volume and mute controls.
1266 static int assign_out_path_ctls(struct hda_codec
*codec
, struct nid_path
*path
)
1268 struct hda_gen_spec
*spec
= codec
->spec
;
1274 return BAD_SHARED_VOL
* 2;
1276 if (path
->ctls
[NID_PATH_VOL_CTL
] ||
1277 path
->ctls
[NID_PATH_MUTE_CTL
])
1278 return 0; /* already evaluated */
1280 nid
= look_for_out_vol_nid(codec
, path
);
1282 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
1283 if (spec
->dac_min_mute
)
1284 val
|= HDA_AMP_VAL_MIN_MUTE
;
1285 if (is_ctl_used(codec
, val
, NID_PATH_VOL_CTL
))
1286 badness
+= BAD_SHARED_VOL
;
1288 path
->ctls
[NID_PATH_VOL_CTL
] = val
;
1290 badness
+= BAD_SHARED_VOL
;
1291 nid
= look_for_out_mute_nid(codec
, path
);
1293 unsigned int wid_type
= get_wcaps_type(get_wcaps(codec
, nid
));
1294 if (wid_type
== AC_WID_PIN
|| wid_type
== AC_WID_AUD_OUT
||
1295 nid_has_mute(codec
, nid
, HDA_OUTPUT
))
1296 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
1298 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_INPUT
);
1299 if (is_ctl_used(codec
, val
, NID_PATH_MUTE_CTL
))
1300 badness
+= BAD_SHARED_VOL
;
1302 path
->ctls
[NID_PATH_MUTE_CTL
] = val
;
1304 badness
+= BAD_SHARED_VOL
;
1308 const struct badness_table hda_main_out_badness
= {
1309 .no_primary_dac
= BAD_NO_PRIMARY_DAC
,
1310 .no_dac
= BAD_NO_DAC
,
1311 .shared_primary
= BAD_NO_PRIMARY_DAC
,
1312 .shared_surr
= BAD_SHARED_SURROUND
,
1313 .shared_clfe
= BAD_SHARED_CLFE
,
1314 .shared_surr_main
= BAD_SHARED_SURROUND
,
1316 EXPORT_SYMBOL_GPL(hda_main_out_badness
);
1318 const struct badness_table hda_extra_out_badness
= {
1319 .no_primary_dac
= BAD_NO_DAC
,
1320 .no_dac
= BAD_NO_DAC
,
1321 .shared_primary
= BAD_NO_EXTRA_DAC
,
1322 .shared_surr
= BAD_SHARED_EXTRA_SURROUND
,
1323 .shared_clfe
= BAD_SHARED_EXTRA_SURROUND
,
1324 .shared_surr_main
= BAD_NO_EXTRA_SURR_DAC
,
1326 EXPORT_SYMBOL_GPL(hda_extra_out_badness
);
1328 /* get the DAC of the primary output corresponding to the given array index */
1329 static hda_nid_t
get_primary_out(struct hda_codec
*codec
, int idx
)
1331 struct hda_gen_spec
*spec
= codec
->spec
;
1332 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1334 if (cfg
->line_outs
> idx
)
1335 return spec
->private_dac_nids
[idx
];
1336 idx
-= cfg
->line_outs
;
1337 if (spec
->multi_ios
> idx
)
1338 return spec
->multi_io
[idx
].dac
;
1342 /* return the DAC if it's reachable, otherwise zero */
1343 static inline hda_nid_t
try_dac(struct hda_codec
*codec
,
1344 hda_nid_t dac
, hda_nid_t pin
)
1346 return is_reachable_path(codec
, dac
, pin
) ? dac
: 0;
1349 /* try to assign DACs to pins and return the resultant badness */
1350 static int try_assign_dacs(struct hda_codec
*codec
, int num_outs
,
1351 const hda_nid_t
*pins
, hda_nid_t
*dacs
,
1353 const struct badness_table
*bad
)
1355 struct hda_gen_spec
*spec
= codec
->spec
;
1363 for (i
= 0; i
< num_outs
; i
++) {
1364 struct nid_path
*path
;
1365 hda_nid_t pin
= pins
[i
];
1367 if (!spec
->obey_preferred_dacs
) {
1368 path
= snd_hda_get_path_from_idx(codec
, path_idx
[i
]);
1370 badness
+= assign_out_path_ctls(codec
, path
);
1375 dacs
[i
] = get_preferred_dac(codec
, pin
);
1377 if (is_dac_already_used(codec
, dacs
[i
]))
1378 badness
+= bad
->shared_primary
;
1379 } else if (spec
->obey_preferred_dacs
) {
1380 badness
+= BAD_NO_PRIMARY_DAC
;
1384 dacs
[i
] = look_for_dac(codec
, pin
, false);
1385 if (!dacs
[i
] && !i
) {
1386 /* try to steal the DAC of surrounds for the front */
1387 for (j
= 1; j
< num_outs
; j
++) {
1388 if (is_reachable_path(codec
, dacs
[j
], pin
)) {
1391 invalidate_nid_path(codec
, path_idx
[j
]);
1400 dac
= try_dac(codec
, get_primary_out(codec
, i
), pin
);
1402 dac
= try_dac(codec
, dacs
[0], pin
);
1404 dac
= try_dac(codec
, get_primary_out(codec
, i
), pin
);
1407 badness
+= bad
->shared_primary
;
1409 badness
+= bad
->shared_surr
;
1411 badness
+= bad
->shared_clfe
;
1412 } else if (is_reachable_path(codec
, spec
->private_dac_nids
[0], pin
)) {
1413 dac
= spec
->private_dac_nids
[0];
1414 badness
+= bad
->shared_surr_main
;
1416 badness
+= bad
->no_primary_dac
;
1418 badness
+= bad
->no_dac
;
1422 path
= snd_hda_add_new_path(codec
, dac
, pin
, -spec
->mixer_nid
);
1423 if (!path
&& !i
&& spec
->mixer_nid
) {
1424 /* try with aamix */
1425 path
= snd_hda_add_new_path(codec
, dac
, pin
, 0);
1429 badness
+= bad
->no_dac
;
1431 /* print_nid_path(codec, "output", path); */
1432 path
->active
= true;
1433 path_idx
[i
] = snd_hda_get_path_idx(codec
, path
);
1434 badness
+= assign_out_path_ctls(codec
, path
);
1441 /* return NID if the given pin has only a single connection to a certain DAC */
1442 static hda_nid_t
get_dac_if_single(struct hda_codec
*codec
, hda_nid_t pin
)
1444 struct hda_gen_spec
*spec
= codec
->spec
;
1446 hda_nid_t nid_found
= 0;
1448 for (i
= 0; i
< spec
->num_all_dacs
; i
++) {
1449 hda_nid_t nid
= spec
->all_dacs
[i
];
1450 if (!nid
|| is_dac_already_used(codec
, nid
))
1452 if (is_reachable_path(codec
, nid
, pin
)) {
1461 /* check whether the given pin can be a multi-io pin */
1462 static bool can_be_multiio_pin(struct hda_codec
*codec
,
1463 unsigned int location
, hda_nid_t nid
)
1465 unsigned int defcfg
, caps
;
1467 defcfg
= snd_hda_codec_get_pincfg(codec
, nid
);
1468 if (get_defcfg_connect(defcfg
) != AC_JACK_PORT_COMPLEX
)
1470 if (location
&& get_defcfg_location(defcfg
) != location
)
1472 caps
= snd_hda_query_pin_caps(codec
, nid
);
1473 if (!(caps
& AC_PINCAP_OUT
))
1478 /* count the number of input pins that are capable to be multi-io */
1479 static int count_multiio_pins(struct hda_codec
*codec
, hda_nid_t reference_pin
)
1481 struct hda_gen_spec
*spec
= codec
->spec
;
1482 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1483 unsigned int defcfg
= snd_hda_codec_get_pincfg(codec
, reference_pin
);
1484 unsigned int location
= get_defcfg_location(defcfg
);
1488 for (type
= AUTO_PIN_LINE_IN
; type
>= AUTO_PIN_MIC
; type
--) {
1489 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
1490 if (cfg
->inputs
[i
].type
!= type
)
1492 if (can_be_multiio_pin(codec
, location
,
1493 cfg
->inputs
[i
].pin
))
1503 * When hardwired is set, try to fill ony hardwired pins, and returns
1504 * zero if any pins are filled, non-zero if nothing found.
1505 * When hardwired is off, try to fill possible input pins, and returns
1506 * the badness value.
1508 static int fill_multi_ios(struct hda_codec
*codec
,
1509 hda_nid_t reference_pin
,
1512 struct hda_gen_spec
*spec
= codec
->spec
;
1513 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1514 int type
, i
, j
, num_pins
, old_pins
;
1515 unsigned int defcfg
= snd_hda_codec_get_pincfg(codec
, reference_pin
);
1516 unsigned int location
= get_defcfg_location(defcfg
);
1518 struct nid_path
*path
;
1520 old_pins
= spec
->multi_ios
;
1524 num_pins
= count_multiio_pins(codec
, reference_pin
);
1528 for (type
= AUTO_PIN_LINE_IN
; type
>= AUTO_PIN_MIC
; type
--) {
1529 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
1530 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
1533 if (cfg
->inputs
[i
].type
!= type
)
1535 if (!can_be_multiio_pin(codec
, location
, nid
))
1537 for (j
= 0; j
< spec
->multi_ios
; j
++) {
1538 if (nid
== spec
->multi_io
[j
].pin
)
1541 if (j
< spec
->multi_ios
)
1545 dac
= get_dac_if_single(codec
, nid
);
1547 dac
= look_for_dac(codec
, nid
, false);
1552 path
= snd_hda_add_new_path(codec
, dac
, nid
,
1558 /* print_nid_path(codec, "multiio", path); */
1559 spec
->multi_io
[spec
->multi_ios
].pin
= nid
;
1560 spec
->multi_io
[spec
->multi_ios
].dac
= dac
;
1561 spec
->out_paths
[cfg
->line_outs
+ spec
->multi_ios
] =
1562 snd_hda_get_path_idx(codec
, path
);
1564 if (spec
->multi_ios
>= 2)
1570 badness
= BAD_MULTI_IO
;
1571 if (old_pins
== spec
->multi_ios
) {
1573 return 1; /* nothing found */
1575 return badness
; /* no badness if nothing found */
1577 if (!hardwired
&& spec
->multi_ios
< 2) {
1578 /* cancel newly assigned paths */
1579 spec
->paths
.used
-= spec
->multi_ios
- old_pins
;
1580 spec
->multi_ios
= old_pins
;
1584 /* assign volume and mute controls */
1585 for (i
= old_pins
; i
< spec
->multi_ios
; i
++) {
1586 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[cfg
->line_outs
+ i
]);
1587 badness
+= assign_out_path_ctls(codec
, path
);
1593 /* map DACs for all pins in the list if they are single connections */
1594 static bool map_singles(struct hda_codec
*codec
, int outs
,
1595 const hda_nid_t
*pins
, hda_nid_t
*dacs
, int *path_idx
)
1597 struct hda_gen_spec
*spec
= codec
->spec
;
1600 for (i
= 0; i
< outs
; i
++) {
1601 struct nid_path
*path
;
1605 dac
= get_dac_if_single(codec
, pins
[i
]);
1608 path
= snd_hda_add_new_path(codec
, dac
, pins
[i
],
1610 if (!path
&& !i
&& spec
->mixer_nid
)
1611 path
= snd_hda_add_new_path(codec
, dac
, pins
[i
], 0);
1615 /* print_nid_path(codec, "output", path); */
1616 path
->active
= true;
1617 path_idx
[i
] = snd_hda_get_path_idx(codec
, path
);
1623 static inline bool has_aamix_out_paths(struct hda_gen_spec
*spec
)
1625 return spec
->aamix_out_paths
[0] || spec
->aamix_out_paths
[1] ||
1626 spec
->aamix_out_paths
[2];
1629 /* create a new path including aamix if available, and return its index */
1630 static int check_aamix_out_path(struct hda_codec
*codec
, int path_idx
)
1632 struct hda_gen_spec
*spec
= codec
->spec
;
1633 struct nid_path
*path
;
1634 hda_nid_t path_dac
, dac
, pin
;
1636 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
1637 if (!path
|| !path
->depth
||
1638 is_nid_contained(path
, spec
->mixer_nid
))
1640 path_dac
= path
->path
[0];
1641 dac
= spec
->private_dac_nids
[0];
1642 pin
= path
->path
[path
->depth
- 1];
1643 path
= snd_hda_add_new_path(codec
, dac
, pin
, spec
->mixer_nid
);
1645 if (dac
!= path_dac
)
1647 else if (spec
->multiout
.hp_out_nid
[0])
1648 dac
= spec
->multiout
.hp_out_nid
[0];
1649 else if (spec
->multiout
.extra_out_nid
[0])
1650 dac
= spec
->multiout
.extra_out_nid
[0];
1654 path
= snd_hda_add_new_path(codec
, dac
, pin
,
1659 /* print_nid_path(codec, "output-aamix", path); */
1660 path
->active
= false; /* unused as default */
1661 path
->pin_fixed
= true; /* static route */
1662 return snd_hda_get_path_idx(codec
, path
);
1665 /* check whether the independent HP is available with the current config */
1666 static bool indep_hp_possible(struct hda_codec
*codec
)
1668 struct hda_gen_spec
*spec
= codec
->spec
;
1669 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1670 struct nid_path
*path
;
1673 if (cfg
->line_out_type
== AUTO_PIN_HP_OUT
)
1674 idx
= spec
->out_paths
[0];
1676 idx
= spec
->hp_paths
[0];
1677 path
= snd_hda_get_path_from_idx(codec
, idx
);
1681 /* assume no path conflicts unless aamix is involved */
1682 if (!spec
->mixer_nid
|| !is_nid_contained(path
, spec
->mixer_nid
))
1685 /* check whether output paths contain aamix */
1686 for (i
= 0; i
< cfg
->line_outs
; i
++) {
1687 if (spec
->out_paths
[i
] == idx
)
1689 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[i
]);
1690 if (path
&& is_nid_contained(path
, spec
->mixer_nid
))
1693 for (i
= 0; i
< cfg
->speaker_outs
; i
++) {
1694 path
= snd_hda_get_path_from_idx(codec
, spec
->speaker_paths
[i
]);
1695 if (path
&& is_nid_contained(path
, spec
->mixer_nid
))
1702 /* fill the empty entries in the dac array for speaker/hp with the
1703 * shared dac pointed by the paths
1705 static void refill_shared_dacs(struct hda_codec
*codec
, int num_outs
,
1706 hda_nid_t
*dacs
, int *path_idx
)
1708 struct nid_path
*path
;
1711 for (i
= 0; i
< num_outs
; i
++) {
1714 path
= snd_hda_get_path_from_idx(codec
, path_idx
[i
]);
1717 dacs
[i
] = path
->path
[0];
1721 /* fill in the dac_nids table from the parsed pin configuration */
1722 static int fill_and_eval_dacs(struct hda_codec
*codec
,
1723 bool fill_hardwired
,
1724 bool fill_mio_first
)
1726 struct hda_gen_spec
*spec
= codec
->spec
;
1727 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1728 int i
, err
, badness
;
1730 /* set num_dacs once to full for look_for_dac() */
1731 spec
->multiout
.num_dacs
= cfg
->line_outs
;
1732 spec
->multiout
.dac_nids
= spec
->private_dac_nids
;
1733 memset(spec
->private_dac_nids
, 0, sizeof(spec
->private_dac_nids
));
1734 memset(spec
->multiout
.hp_out_nid
, 0, sizeof(spec
->multiout
.hp_out_nid
));
1735 memset(spec
->multiout
.extra_out_nid
, 0, sizeof(spec
->multiout
.extra_out_nid
));
1736 spec
->multi_ios
= 0;
1737 snd_array_free(&spec
->paths
);
1739 /* clear path indices */
1740 memset(spec
->out_paths
, 0, sizeof(spec
->out_paths
));
1741 memset(spec
->hp_paths
, 0, sizeof(spec
->hp_paths
));
1742 memset(spec
->speaker_paths
, 0, sizeof(spec
->speaker_paths
));
1743 memset(spec
->aamix_out_paths
, 0, sizeof(spec
->aamix_out_paths
));
1744 memset(spec
->digout_paths
, 0, sizeof(spec
->digout_paths
));
1745 memset(spec
->input_paths
, 0, sizeof(spec
->input_paths
));
1746 memset(spec
->loopback_paths
, 0, sizeof(spec
->loopback_paths
));
1747 memset(&spec
->digin_path
, 0, sizeof(spec
->digin_path
));
1751 /* fill hard-wired DACs first */
1752 if (fill_hardwired
) {
1755 mapped
= map_singles(codec
, cfg
->line_outs
,
1757 spec
->private_dac_nids
,
1759 mapped
|= map_singles(codec
, cfg
->hp_outs
,
1761 spec
->multiout
.hp_out_nid
,
1763 mapped
|= map_singles(codec
, cfg
->speaker_outs
,
1765 spec
->multiout
.extra_out_nid
,
1766 spec
->speaker_paths
);
1767 if (!spec
->no_multi_io
&&
1768 fill_mio_first
&& cfg
->line_outs
== 1 &&
1769 cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1770 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], true);
1777 badness
+= try_assign_dacs(codec
, cfg
->line_outs
, cfg
->line_out_pins
,
1778 spec
->private_dac_nids
, spec
->out_paths
,
1779 spec
->main_out_badness
);
1781 if (!spec
->no_multi_io
&& fill_mio_first
&&
1782 cfg
->line_outs
== 1 && cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1783 /* try to fill multi-io first */
1784 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], false);
1787 /* we don't count badness at this stage yet */
1790 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
) {
1791 err
= try_assign_dacs(codec
, cfg
->hp_outs
, cfg
->hp_pins
,
1792 spec
->multiout
.hp_out_nid
,
1794 spec
->extra_out_badness
);
1799 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1800 err
= try_assign_dacs(codec
, cfg
->speaker_outs
,
1802 spec
->multiout
.extra_out_nid
,
1803 spec
->speaker_paths
,
1804 spec
->extra_out_badness
);
1809 if (!spec
->no_multi_io
&&
1810 cfg
->line_outs
== 1 && cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1811 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], false);
1817 if (spec
->mixer_nid
) {
1818 spec
->aamix_out_paths
[0] =
1819 check_aamix_out_path(codec
, spec
->out_paths
[0]);
1820 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
1821 spec
->aamix_out_paths
[1] =
1822 check_aamix_out_path(codec
, spec
->hp_paths
[0]);
1823 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
1824 spec
->aamix_out_paths
[2] =
1825 check_aamix_out_path(codec
, spec
->speaker_paths
[0]);
1828 if (!spec
->no_multi_io
&&
1829 cfg
->hp_outs
&& cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
)
1830 if (count_multiio_pins(codec
, cfg
->hp_pins
[0]) >= 2)
1831 spec
->multi_ios
= 1; /* give badness */
1833 /* re-count num_dacs and squash invalid entries */
1834 spec
->multiout
.num_dacs
= 0;
1835 for (i
= 0; i
< cfg
->line_outs
; i
++) {
1836 if (spec
->private_dac_nids
[i
])
1837 spec
->multiout
.num_dacs
++;
1839 memmove(spec
->private_dac_nids
+ i
,
1840 spec
->private_dac_nids
+ i
+ 1,
1841 sizeof(hda_nid_t
) * (cfg
->line_outs
- i
- 1));
1842 spec
->private_dac_nids
[cfg
->line_outs
- 1] = 0;
1846 spec
->ext_channel_count
= spec
->min_channel_count
=
1847 spec
->multiout
.num_dacs
* 2;
1849 if (spec
->multi_ios
== 2) {
1850 for (i
= 0; i
< 2; i
++)
1851 spec
->private_dac_nids
[spec
->multiout
.num_dacs
++] =
1852 spec
->multi_io
[i
].dac
;
1853 } else if (spec
->multi_ios
) {
1854 spec
->multi_ios
= 0;
1855 badness
+= BAD_MULTI_IO
;
1858 if (spec
->indep_hp
&& !indep_hp_possible(codec
))
1859 badness
+= BAD_NO_INDEP_HP
;
1861 /* re-fill the shared DAC for speaker / headphone */
1862 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
1863 refill_shared_dacs(codec
, cfg
->hp_outs
,
1864 spec
->multiout
.hp_out_nid
,
1866 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
1867 refill_shared_dacs(codec
, cfg
->speaker_outs
,
1868 spec
->multiout
.extra_out_nid
,
1869 spec
->speaker_paths
);
1874 #define DEBUG_BADNESS
1876 #ifdef DEBUG_BADNESS
1877 #define debug_badness(fmt, ...) \
1878 codec_dbg(codec, fmt, ##__VA_ARGS__)
1880 #define debug_badness(fmt, ...) \
1881 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1884 #ifdef DEBUG_BADNESS
1885 static inline void print_nid_path_idx(struct hda_codec
*codec
,
1886 const char *pfx
, int idx
)
1888 struct nid_path
*path
;
1890 path
= snd_hda_get_path_from_idx(codec
, idx
);
1892 print_nid_path(codec
, pfx
, path
);
1895 static void debug_show_configs(struct hda_codec
*codec
,
1896 struct auto_pin_cfg
*cfg
)
1898 struct hda_gen_spec
*spec
= codec
->spec
;
1899 static const char * const lo_type
[3] = { "LO", "SP", "HP" };
1902 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1903 cfg
->line_out_pins
[0], cfg
->line_out_pins
[1],
1904 cfg
->line_out_pins
[2], cfg
->line_out_pins
[3],
1905 spec
->multiout
.dac_nids
[0],
1906 spec
->multiout
.dac_nids
[1],
1907 spec
->multiout
.dac_nids
[2],
1908 spec
->multiout
.dac_nids
[3],
1909 lo_type
[cfg
->line_out_type
]);
1910 for (i
= 0; i
< cfg
->line_outs
; i
++)
1911 print_nid_path_idx(codec
, " out", spec
->out_paths
[i
]);
1912 if (spec
->multi_ios
> 0)
1913 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1915 spec
->multi_io
[0].pin
, spec
->multi_io
[1].pin
,
1916 spec
->multi_io
[0].dac
, spec
->multi_io
[1].dac
);
1917 for (i
= 0; i
< spec
->multi_ios
; i
++)
1918 print_nid_path_idx(codec
, " mio",
1919 spec
->out_paths
[cfg
->line_outs
+ i
]);
1921 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1922 cfg
->hp_pins
[0], cfg
->hp_pins
[1],
1923 cfg
->hp_pins
[2], cfg
->hp_pins
[3],
1924 spec
->multiout
.hp_out_nid
[0],
1925 spec
->multiout
.hp_out_nid
[1],
1926 spec
->multiout
.hp_out_nid
[2],
1927 spec
->multiout
.hp_out_nid
[3]);
1928 for (i
= 0; i
< cfg
->hp_outs
; i
++)
1929 print_nid_path_idx(codec
, " hp ", spec
->hp_paths
[i
]);
1930 if (cfg
->speaker_outs
)
1931 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1932 cfg
->speaker_pins
[0], cfg
->speaker_pins
[1],
1933 cfg
->speaker_pins
[2], cfg
->speaker_pins
[3],
1934 spec
->multiout
.extra_out_nid
[0],
1935 spec
->multiout
.extra_out_nid
[1],
1936 spec
->multiout
.extra_out_nid
[2],
1937 spec
->multiout
.extra_out_nid
[3]);
1938 for (i
= 0; i
< cfg
->speaker_outs
; i
++)
1939 print_nid_path_idx(codec
, " spk", spec
->speaker_paths
[i
]);
1940 for (i
= 0; i
< 3; i
++)
1941 print_nid_path_idx(codec
, " mix", spec
->aamix_out_paths
[i
]);
1944 #define debug_show_configs(codec, cfg) /* NOP */
1947 /* find all available DACs of the codec */
1948 static void fill_all_dac_nids(struct hda_codec
*codec
)
1950 struct hda_gen_spec
*spec
= codec
->spec
;
1953 spec
->num_all_dacs
= 0;
1954 memset(spec
->all_dacs
, 0, sizeof(spec
->all_dacs
));
1955 for_each_hda_codec_node(nid
, codec
) {
1956 if (get_wcaps_type(get_wcaps(codec
, nid
)) != AC_WID_AUD_OUT
)
1958 if (spec
->num_all_dacs
>= ARRAY_SIZE(spec
->all_dacs
)) {
1959 codec_err(codec
, "Too many DACs!\n");
1962 spec
->all_dacs
[spec
->num_all_dacs
++] = nid
;
1966 static int parse_output_paths(struct hda_codec
*codec
)
1968 struct hda_gen_spec
*spec
= codec
->spec
;
1969 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1970 struct auto_pin_cfg
*best_cfg
;
1972 int best_badness
= INT_MAX
;
1974 bool fill_hardwired
= true, fill_mio_first
= true;
1975 bool best_wired
= true, best_mio
= true;
1976 bool hp_spk_swapped
= false;
1978 best_cfg
= kmalloc(sizeof(*best_cfg
), GFP_KERNEL
);
1984 badness
= fill_and_eval_dacs(codec
, fill_hardwired
,
1990 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1991 cfg
->line_out_type
, fill_hardwired
, fill_mio_first
,
1993 debug_show_configs(codec
, cfg
);
1994 if (badness
< best_badness
) {
1995 best_badness
= badness
;
1997 best_wired
= fill_hardwired
;
1998 best_mio
= fill_mio_first
;
2002 fill_mio_first
= !fill_mio_first
;
2003 if (!fill_mio_first
)
2005 fill_hardwired
= !fill_hardwired
;
2006 if (!fill_hardwired
)
2010 hp_spk_swapped
= true;
2011 if (cfg
->speaker_outs
> 0 &&
2012 cfg
->line_out_type
== AUTO_PIN_HP_OUT
) {
2013 cfg
->hp_outs
= cfg
->line_outs
;
2014 memcpy(cfg
->hp_pins
, cfg
->line_out_pins
,
2015 sizeof(cfg
->hp_pins
));
2016 cfg
->line_outs
= cfg
->speaker_outs
;
2017 memcpy(cfg
->line_out_pins
, cfg
->speaker_pins
,
2018 sizeof(cfg
->speaker_pins
));
2019 cfg
->speaker_outs
= 0;
2020 memset(cfg
->speaker_pins
, 0, sizeof(cfg
->speaker_pins
));
2021 cfg
->line_out_type
= AUTO_PIN_SPEAKER_OUT
;
2022 fill_hardwired
= true;
2025 if (cfg
->hp_outs
> 0 &&
2026 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
) {
2027 cfg
->speaker_outs
= cfg
->line_outs
;
2028 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
2029 sizeof(cfg
->speaker_pins
));
2030 cfg
->line_outs
= cfg
->hp_outs
;
2031 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
,
2032 sizeof(cfg
->hp_pins
));
2034 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
2035 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
2036 fill_hardwired
= true;
2043 debug_badness("==> restoring best_cfg\n");
2045 fill_and_eval_dacs(codec
, best_wired
, best_mio
);
2047 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2048 cfg
->line_out_type
, best_wired
, best_mio
);
2049 debug_show_configs(codec
, cfg
);
2051 if (cfg
->line_out_pins
[0]) {
2052 struct nid_path
*path
;
2053 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[0]);
2055 spec
->vmaster_nid
= look_for_out_vol_nid(codec
, path
);
2056 if (spec
->vmaster_nid
) {
2057 snd_hda_set_vmaster_tlv(codec
, spec
->vmaster_nid
,
2058 HDA_OUTPUT
, spec
->vmaster_tlv
);
2059 if (spec
->dac_min_mute
)
2060 spec
->vmaster_tlv
[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP
] |= TLV_DB_SCALE_MUTE
;
2064 /* set initial pinctl targets */
2065 if (spec
->prefer_hp_amp
|| cfg
->line_out_type
== AUTO_PIN_HP_OUT
)
2069 set_pin_targets(codec
, cfg
->line_outs
, cfg
->line_out_pins
, val
);
2070 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
2071 set_pin_targets(codec
, cfg
->hp_outs
, cfg
->hp_pins
, PIN_HP
);
2072 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
2073 val
= spec
->prefer_hp_amp
? PIN_HP
: PIN_OUT
;
2074 set_pin_targets(codec
, cfg
->speaker_outs
,
2075 cfg
->speaker_pins
, val
);
2078 /* clear indep_hp flag if not available */
2079 if (spec
->indep_hp
&& !indep_hp_possible(codec
))
2086 /* add playback controls from the parsed DAC table */
2087 static int create_multi_out_ctls(struct hda_codec
*codec
,
2088 const struct auto_pin_cfg
*cfg
)
2090 struct hda_gen_spec
*spec
= codec
->spec
;
2091 int i
, err
, noutputs
;
2093 noutputs
= cfg
->line_outs
;
2094 if (spec
->multi_ios
> 0 && cfg
->line_outs
< 3)
2095 noutputs
+= spec
->multi_ios
;
2097 for (i
= 0; i
< noutputs
; i
++) {
2100 struct nid_path
*path
;
2102 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[i
]);
2106 name
= get_line_out_pfx(codec
, i
, &index
, NID_PATH_VOL_CTL
);
2107 if (!name
|| !strcmp(name
, "CLFE")) {
2109 err
= add_vol_ctl(codec
, "Center", 0, 1, path
);
2112 err
= add_vol_ctl(codec
, "LFE", 0, 2, path
);
2116 err
= add_stereo_vol(codec
, name
, index
, path
);
2121 name
= get_line_out_pfx(codec
, i
, &index
, NID_PATH_MUTE_CTL
);
2122 if (!name
|| !strcmp(name
, "CLFE")) {
2123 err
= add_sw_ctl(codec
, "Center", 0, 1, path
);
2126 err
= add_sw_ctl(codec
, "LFE", 0, 2, path
);
2130 err
= add_stereo_sw(codec
, name
, index
, path
);
2138 static int create_extra_out(struct hda_codec
*codec
, int path_idx
,
2139 const char *pfx
, int cidx
)
2141 struct nid_path
*path
;
2144 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
2147 err
= add_stereo_vol(codec
, pfx
, cidx
, path
);
2150 err
= add_stereo_sw(codec
, pfx
, cidx
, path
);
2156 /* add playback controls for speaker and HP outputs */
2157 static int create_extra_outs(struct hda_codec
*codec
, int num_pins
,
2158 const int *paths
, const char *pfx
)
2162 for (i
= 0; i
< num_pins
; i
++) {
2164 char tmp
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
2167 if (num_pins
== 2 && i
== 1 && !strcmp(pfx
, "Speaker"))
2168 name
= "Bass Speaker";
2169 else if (num_pins
>= 3) {
2170 snprintf(tmp
, sizeof(tmp
), "%s %s",
2171 pfx
, channel_name
[i
]);
2177 err
= create_extra_out(codec
, paths
[i
], name
, idx
);
2184 static int create_hp_out_ctls(struct hda_codec
*codec
)
2186 struct hda_gen_spec
*spec
= codec
->spec
;
2187 return create_extra_outs(codec
, spec
->autocfg
.hp_outs
,
2192 static int create_speaker_out_ctls(struct hda_codec
*codec
)
2194 struct hda_gen_spec
*spec
= codec
->spec
;
2195 return create_extra_outs(codec
, spec
->autocfg
.speaker_outs
,
2196 spec
->speaker_paths
,
2201 * independent HP controls
2204 static void call_hp_automute(struct hda_codec
*codec
,
2205 struct hda_jack_callback
*jack
);
2206 static int indep_hp_info(struct snd_kcontrol
*kcontrol
,
2207 struct snd_ctl_elem_info
*uinfo
)
2209 return snd_hda_enum_bool_helper_info(kcontrol
, uinfo
);
2212 static int indep_hp_get(struct snd_kcontrol
*kcontrol
,
2213 struct snd_ctl_elem_value
*ucontrol
)
2215 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2216 struct hda_gen_spec
*spec
= codec
->spec
;
2217 ucontrol
->value
.enumerated
.item
[0] = spec
->indep_hp_enabled
;
2221 static void update_aamix_paths(struct hda_codec
*codec
, bool do_mix
,
2222 int nomix_path_idx
, int mix_path_idx
,
2225 static int indep_hp_put(struct snd_kcontrol
*kcontrol
,
2226 struct snd_ctl_elem_value
*ucontrol
)
2228 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2229 struct hda_gen_spec
*spec
= codec
->spec
;
2230 unsigned int select
= ucontrol
->value
.enumerated
.item
[0];
2233 mutex_lock(&spec
->pcm_mutex
);
2234 if (spec
->active_streams
) {
2239 if (spec
->indep_hp_enabled
!= select
) {
2241 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
2242 dacp
= &spec
->private_dac_nids
[0];
2244 dacp
= &spec
->multiout
.hp_out_nid
[0];
2246 /* update HP aamix paths in case it conflicts with indep HP */
2247 if (spec
->have_aamix_ctl
) {
2248 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
2249 update_aamix_paths(codec
, spec
->aamix_mode
,
2251 spec
->aamix_out_paths
[0],
2252 spec
->autocfg
.line_out_type
);
2254 update_aamix_paths(codec
, spec
->aamix_mode
,
2256 spec
->aamix_out_paths
[1],
2260 spec
->indep_hp_enabled
= select
;
2261 if (spec
->indep_hp_enabled
)
2264 *dacp
= spec
->alt_dac_nid
;
2266 call_hp_automute(codec
, NULL
);
2270 mutex_unlock(&spec
->pcm_mutex
);
2274 static const struct snd_kcontrol_new indep_hp_ctl
= {
2275 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2276 .name
= "Independent HP",
2277 .info
= indep_hp_info
,
2278 .get
= indep_hp_get
,
2279 .put
= indep_hp_put
,
2283 static int create_indep_hp_ctls(struct hda_codec
*codec
)
2285 struct hda_gen_spec
*spec
= codec
->spec
;
2288 if (!spec
->indep_hp
)
2290 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
2291 dac
= spec
->multiout
.dac_nids
[0];
2293 dac
= spec
->multiout
.hp_out_nid
[0];
2299 spec
->indep_hp_enabled
= false;
2300 spec
->alt_dac_nid
= dac
;
2301 if (!snd_hda_gen_add_kctl(spec
, NULL
, &indep_hp_ctl
))
2307 * channel mode enum control
2310 static int ch_mode_info(struct snd_kcontrol
*kcontrol
,
2311 struct snd_ctl_elem_info
*uinfo
)
2313 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2314 struct hda_gen_spec
*spec
= codec
->spec
;
2317 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2319 uinfo
->value
.enumerated
.items
= spec
->multi_ios
+ 1;
2320 if (uinfo
->value
.enumerated
.item
> spec
->multi_ios
)
2321 uinfo
->value
.enumerated
.item
= spec
->multi_ios
;
2322 chs
= uinfo
->value
.enumerated
.item
* 2 + spec
->min_channel_count
;
2323 sprintf(uinfo
->value
.enumerated
.name
, "%dch", chs
);
2327 static int ch_mode_get(struct snd_kcontrol
*kcontrol
,
2328 struct snd_ctl_elem_value
*ucontrol
)
2330 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2331 struct hda_gen_spec
*spec
= codec
->spec
;
2332 ucontrol
->value
.enumerated
.item
[0] =
2333 (spec
->ext_channel_count
- spec
->min_channel_count
) / 2;
2337 static inline struct nid_path
*
2338 get_multiio_path(struct hda_codec
*codec
, int idx
)
2340 struct hda_gen_spec
*spec
= codec
->spec
;
2341 return snd_hda_get_path_from_idx(codec
,
2342 spec
->out_paths
[spec
->autocfg
.line_outs
+ idx
]);
2345 static void update_automute_all(struct hda_codec
*codec
);
2347 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2348 * used for output paths
2350 static bool aamix_default(struct hda_gen_spec
*spec
)
2352 return !spec
->have_aamix_ctl
|| spec
->aamix_mode
;
2355 static int set_multi_io(struct hda_codec
*codec
, int idx
, bool output
)
2357 struct hda_gen_spec
*spec
= codec
->spec
;
2358 hda_nid_t nid
= spec
->multi_io
[idx
].pin
;
2359 struct nid_path
*path
;
2361 path
= get_multiio_path(codec
, idx
);
2365 if (path
->active
== output
)
2369 set_pin_target(codec
, nid
, PIN_OUT
, true);
2370 snd_hda_activate_path(codec
, path
, true, aamix_default(spec
));
2371 set_pin_eapd(codec
, nid
, true);
2373 set_pin_eapd(codec
, nid
, false);
2374 snd_hda_activate_path(codec
, path
, false, aamix_default(spec
));
2375 set_pin_target(codec
, nid
, spec
->multi_io
[idx
].ctl_in
, true);
2376 path_power_down_sync(codec
, path
);
2379 /* update jack retasking in case it modifies any of them */
2380 update_automute_all(codec
);
2385 static int ch_mode_put(struct snd_kcontrol
*kcontrol
,
2386 struct snd_ctl_elem_value
*ucontrol
)
2388 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2389 struct hda_gen_spec
*spec
= codec
->spec
;
2392 ch
= ucontrol
->value
.enumerated
.item
[0];
2393 if (ch
< 0 || ch
> spec
->multi_ios
)
2395 if (ch
== (spec
->ext_channel_count
- spec
->min_channel_count
) / 2)
2397 spec
->ext_channel_count
= ch
* 2 + spec
->min_channel_count
;
2398 for (i
= 0; i
< spec
->multi_ios
; i
++)
2399 set_multi_io(codec
, i
, i
< ch
);
2400 spec
->multiout
.max_channels
= max(spec
->ext_channel_count
,
2401 spec
->const_channel_count
);
2402 if (spec
->need_dac_fix
)
2403 spec
->multiout
.num_dacs
= spec
->multiout
.max_channels
/ 2;
2407 static const struct snd_kcontrol_new channel_mode_enum
= {
2408 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2409 .name
= "Channel Mode",
2410 .info
= ch_mode_info
,
2415 static int create_multi_channel_mode(struct hda_codec
*codec
)
2417 struct hda_gen_spec
*spec
= codec
->spec
;
2419 if (spec
->multi_ios
> 0) {
2420 if (!snd_hda_gen_add_kctl(spec
, NULL
, &channel_mode_enum
))
2427 * aamix loopback enable/disable switch
2430 #define loopback_mixing_info indep_hp_info
2432 static int loopback_mixing_get(struct snd_kcontrol
*kcontrol
,
2433 struct snd_ctl_elem_value
*ucontrol
)
2435 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2436 struct hda_gen_spec
*spec
= codec
->spec
;
2437 ucontrol
->value
.enumerated
.item
[0] = spec
->aamix_mode
;
2441 static void update_aamix_paths(struct hda_codec
*codec
, bool do_mix
,
2442 int nomix_path_idx
, int mix_path_idx
,
2445 struct hda_gen_spec
*spec
= codec
->spec
;
2446 struct nid_path
*nomix_path
, *mix_path
;
2448 nomix_path
= snd_hda_get_path_from_idx(codec
, nomix_path_idx
);
2449 mix_path
= snd_hda_get_path_from_idx(codec
, mix_path_idx
);
2450 if (!nomix_path
|| !mix_path
)
2453 /* if HP aamix path is driven from a different DAC and the
2454 * independent HP mode is ON, can't turn on aamix path
2456 if (out_type
== AUTO_PIN_HP_OUT
&& spec
->indep_hp_enabled
&&
2457 mix_path
->path
[0] != spec
->alt_dac_nid
)
2461 snd_hda_activate_path(codec
, nomix_path
, false, true);
2462 snd_hda_activate_path(codec
, mix_path
, true, true);
2463 path_power_down_sync(codec
, nomix_path
);
2465 snd_hda_activate_path(codec
, mix_path
, false, false);
2466 snd_hda_activate_path(codec
, nomix_path
, true, false);
2467 path_power_down_sync(codec
, mix_path
);
2471 /* re-initialize the output paths; only called from loopback_mixing_put() */
2472 static void update_output_paths(struct hda_codec
*codec
, int num_outs
,
2475 struct hda_gen_spec
*spec
= codec
->spec
;
2476 struct nid_path
*path
;
2479 for (i
= 0; i
< num_outs
; i
++) {
2480 path
= snd_hda_get_path_from_idx(codec
, paths
[i
]);
2482 snd_hda_activate_path(codec
, path
, path
->active
,
2487 static int loopback_mixing_put(struct snd_kcontrol
*kcontrol
,
2488 struct snd_ctl_elem_value
*ucontrol
)
2490 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2491 struct hda_gen_spec
*spec
= codec
->spec
;
2492 const struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
2493 unsigned int val
= ucontrol
->value
.enumerated
.item
[0];
2495 if (val
== spec
->aamix_mode
)
2497 spec
->aamix_mode
= val
;
2498 if (has_aamix_out_paths(spec
)) {
2499 update_aamix_paths(codec
, val
, spec
->out_paths
[0],
2500 spec
->aamix_out_paths
[0],
2501 cfg
->line_out_type
);
2502 update_aamix_paths(codec
, val
, spec
->hp_paths
[0],
2503 spec
->aamix_out_paths
[1],
2505 update_aamix_paths(codec
, val
, spec
->speaker_paths
[0],
2506 spec
->aamix_out_paths
[2],
2507 AUTO_PIN_SPEAKER_OUT
);
2509 update_output_paths(codec
, cfg
->line_outs
, spec
->out_paths
);
2510 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
2511 update_output_paths(codec
, cfg
->hp_outs
, spec
->hp_paths
);
2512 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
2513 update_output_paths(codec
, cfg
->speaker_outs
,
2514 spec
->speaker_paths
);
2519 static const struct snd_kcontrol_new loopback_mixing_enum
= {
2520 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2521 .name
= "Loopback Mixing",
2522 .info
= loopback_mixing_info
,
2523 .get
= loopback_mixing_get
,
2524 .put
= loopback_mixing_put
,
2527 static int create_loopback_mixing_ctl(struct hda_codec
*codec
)
2529 struct hda_gen_spec
*spec
= codec
->spec
;
2531 if (!spec
->mixer_nid
)
2533 if (!snd_hda_gen_add_kctl(spec
, NULL
, &loopback_mixing_enum
))
2535 spec
->have_aamix_ctl
= 1;
2540 * shared headphone/mic handling
2543 static void call_update_outputs(struct hda_codec
*codec
);
2545 /* for shared I/O, change the pin-control accordingly */
2546 static void update_hp_mic(struct hda_codec
*codec
, int adc_mux
, bool force
)
2548 struct hda_gen_spec
*spec
= codec
->spec
;
2553 pin
= spec
->hp_mic_pin
;
2554 as_mic
= spec
->cur_mux
[adc_mux
] == spec
->hp_mic_mux_idx
;
2557 val
= snd_hda_codec_get_pin_target(codec
, pin
);
2567 val
= snd_hda_get_default_vref(codec
, pin
);
2568 /* if the HP pin doesn't support VREF and the codec driver gives an
2569 * alternative pin, set up the VREF on that pin instead
2571 if (val
== AC_PINCTL_VREF_HIZ
&& spec
->shared_mic_vref_pin
) {
2572 const hda_nid_t vref_pin
= spec
->shared_mic_vref_pin
;
2573 unsigned int vref_val
= snd_hda_get_default_vref(codec
, vref_pin
);
2574 if (vref_val
!= AC_PINCTL_VREF_HIZ
)
2575 snd_hda_set_pin_ctl_cache(codec
, vref_pin
,
2576 PIN_IN
| (as_mic
? vref_val
: 0));
2579 if (!spec
->hp_mic_jack_modes
) {
2584 set_pin_target(codec
, pin
, val
, true);
2585 call_hp_automute(codec
, NULL
);
2589 /* create a shared input with the headphone out */
2590 static int create_hp_mic(struct hda_codec
*codec
)
2592 struct hda_gen_spec
*spec
= codec
->spec
;
2593 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
2594 unsigned int defcfg
;
2597 if (!spec
->hp_mic
) {
2598 if (spec
->suppress_hp_mic_detect
)
2600 /* automatic detection: only if no input or a single internal
2601 * input pin is found, try to detect the shared hp/mic
2603 if (cfg
->num_inputs
> 1)
2605 else if (cfg
->num_inputs
== 1) {
2606 defcfg
= snd_hda_codec_get_pincfg(codec
, cfg
->inputs
[0].pin
);
2607 if (snd_hda_get_input_pin_attr(defcfg
) != INPUT_PIN_ATTR_INT
)
2612 spec
->hp_mic
= 0; /* clear once */
2613 if (cfg
->num_inputs
>= AUTO_CFG_MAX_INS
)
2617 if (cfg
->line_out_type
== AUTO_PIN_HP_OUT
&& cfg
->line_outs
> 0)
2618 nid
= cfg
->line_out_pins
[0];
2619 else if (cfg
->hp_outs
> 0)
2620 nid
= cfg
->hp_pins
[0];
2624 if (!(snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_IN
))
2625 return 0; /* no input */
2627 cfg
->inputs
[cfg
->num_inputs
].pin
= nid
;
2628 cfg
->inputs
[cfg
->num_inputs
].type
= AUTO_PIN_MIC
;
2629 cfg
->inputs
[cfg
->num_inputs
].is_headphone_mic
= 1;
2632 spec
->hp_mic_pin
= nid
;
2633 /* we can't handle auto-mic together with HP-mic */
2634 spec
->suppress_auto_mic
= 1;
2635 codec_dbg(codec
, "Enable shared I/O jack on NID 0x%x\n", nid
);
2643 static int create_hp_mic_jack_mode(struct hda_codec
*codec
, hda_nid_t pin
);
2645 static const char * const out_jack_texts
[] = {
2646 "Line Out", "Headphone Out",
2649 static int out_jack_mode_info(struct snd_kcontrol
*kcontrol
,
2650 struct snd_ctl_elem_info
*uinfo
)
2652 return snd_hda_enum_helper_info(kcontrol
, uinfo
, 2, out_jack_texts
);
2655 static int out_jack_mode_get(struct snd_kcontrol
*kcontrol
,
2656 struct snd_ctl_elem_value
*ucontrol
)
2658 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2659 hda_nid_t nid
= kcontrol
->private_value
;
2660 if (snd_hda_codec_get_pin_target(codec
, nid
) == PIN_HP
)
2661 ucontrol
->value
.enumerated
.item
[0] = 1;
2663 ucontrol
->value
.enumerated
.item
[0] = 0;
2667 static int out_jack_mode_put(struct snd_kcontrol
*kcontrol
,
2668 struct snd_ctl_elem_value
*ucontrol
)
2670 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2671 hda_nid_t nid
= kcontrol
->private_value
;
2674 val
= ucontrol
->value
.enumerated
.item
[0] ? PIN_HP
: PIN_OUT
;
2675 if (snd_hda_codec_get_pin_target(codec
, nid
) == val
)
2677 snd_hda_set_pin_ctl_cache(codec
, nid
, val
);
2681 static const struct snd_kcontrol_new out_jack_mode_enum
= {
2682 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2683 .info
= out_jack_mode_info
,
2684 .get
= out_jack_mode_get
,
2685 .put
= out_jack_mode_put
,
2688 static bool find_kctl_name(struct hda_codec
*codec
, const char *name
, int idx
)
2690 struct hda_gen_spec
*spec
= codec
->spec
;
2691 const struct snd_kcontrol_new
*kctl
;
2694 snd_array_for_each(&spec
->kctls
, i
, kctl
) {
2695 if (!strcmp(kctl
->name
, name
) && kctl
->index
== idx
)
2701 static void get_jack_mode_name(struct hda_codec
*codec
, hda_nid_t pin
,
2702 char *name
, size_t name_len
)
2704 struct hda_gen_spec
*spec
= codec
->spec
;
2707 snd_hda_get_pin_label(codec
, pin
, &spec
->autocfg
, name
, name_len
, &idx
);
2708 strlcat(name
, " Jack Mode", name_len
);
2710 for (; find_kctl_name(codec
, name
, idx
); idx
++)
2714 static int get_out_jack_num_items(struct hda_codec
*codec
, hda_nid_t pin
)
2716 struct hda_gen_spec
*spec
= codec
->spec
;
2717 if (spec
->add_jack_modes
) {
2718 unsigned int pincap
= snd_hda_query_pin_caps(codec
, pin
);
2719 if ((pincap
& AC_PINCAP_OUT
) && (pincap
& AC_PINCAP_HP_DRV
))
2725 static int create_out_jack_modes(struct hda_codec
*codec
, int num_pins
,
2728 struct hda_gen_spec
*spec
= codec
->spec
;
2731 for (i
= 0; i
< num_pins
; i
++) {
2732 hda_nid_t pin
= pins
[i
];
2733 if (pin
== spec
->hp_mic_pin
)
2735 if (get_out_jack_num_items(codec
, pin
) > 1) {
2736 struct snd_kcontrol_new
*knew
;
2737 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
2738 get_jack_mode_name(codec
, pin
, name
, sizeof(name
));
2739 knew
= snd_hda_gen_add_kctl(spec
, name
,
2740 &out_jack_mode_enum
);
2743 knew
->private_value
= pin
;
2754 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2757 static const char * const vref_texts
[NUM_VREFS
] = {
2758 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2759 "", "Mic 80pc Bias", "Mic 100pc Bias"
2762 static unsigned int get_vref_caps(struct hda_codec
*codec
, hda_nid_t pin
)
2764 unsigned int pincap
;
2766 pincap
= snd_hda_query_pin_caps(codec
, pin
);
2767 pincap
= (pincap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
2768 /* filter out unusual vrefs */
2769 pincap
&= ~(AC_PINCAP_VREF_GRD
| AC_PINCAP_VREF_100
);
2773 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2774 static int get_vref_idx(unsigned int vref_caps
, unsigned int item_idx
)
2776 unsigned int i
, n
= 0;
2778 for (i
= 0; i
< NUM_VREFS
; i
++) {
2779 if (vref_caps
& (1 << i
)) {
2788 /* convert back from the vref ctl index to the enum item index */
2789 static int cvt_from_vref_idx(unsigned int vref_caps
, unsigned int idx
)
2791 unsigned int i
, n
= 0;
2793 for (i
= 0; i
< NUM_VREFS
; i
++) {
2796 if (vref_caps
& (1 << i
))
2802 static int in_jack_mode_info(struct snd_kcontrol
*kcontrol
,
2803 struct snd_ctl_elem_info
*uinfo
)
2805 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2806 hda_nid_t nid
= kcontrol
->private_value
;
2807 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2809 snd_hda_enum_helper_info(kcontrol
, uinfo
, hweight32(vref_caps
),
2811 /* set the right text */
2812 strcpy(uinfo
->value
.enumerated
.name
,
2813 vref_texts
[get_vref_idx(vref_caps
, uinfo
->value
.enumerated
.item
)]);
2817 static int in_jack_mode_get(struct snd_kcontrol
*kcontrol
,
2818 struct snd_ctl_elem_value
*ucontrol
)
2820 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2821 hda_nid_t nid
= kcontrol
->private_value
;
2822 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2825 idx
= snd_hda_codec_get_pin_target(codec
, nid
) & AC_PINCTL_VREFEN
;
2826 ucontrol
->value
.enumerated
.item
[0] = cvt_from_vref_idx(vref_caps
, idx
);
2830 static int in_jack_mode_put(struct snd_kcontrol
*kcontrol
,
2831 struct snd_ctl_elem_value
*ucontrol
)
2833 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2834 hda_nid_t nid
= kcontrol
->private_value
;
2835 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2836 unsigned int val
, idx
;
2838 val
= snd_hda_codec_get_pin_target(codec
, nid
);
2839 idx
= cvt_from_vref_idx(vref_caps
, val
& AC_PINCTL_VREFEN
);
2840 if (idx
== ucontrol
->value
.enumerated
.item
[0])
2843 val
&= ~AC_PINCTL_VREFEN
;
2844 val
|= get_vref_idx(vref_caps
, ucontrol
->value
.enumerated
.item
[0]);
2845 snd_hda_set_pin_ctl_cache(codec
, nid
, val
);
2849 static const struct snd_kcontrol_new in_jack_mode_enum
= {
2850 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2851 .info
= in_jack_mode_info
,
2852 .get
= in_jack_mode_get
,
2853 .put
= in_jack_mode_put
,
2856 static int get_in_jack_num_items(struct hda_codec
*codec
, hda_nid_t pin
)
2858 struct hda_gen_spec
*spec
= codec
->spec
;
2860 if (spec
->add_jack_modes
)
2861 nitems
= hweight32(get_vref_caps(codec
, pin
));
2862 return nitems
? nitems
: 1;
2865 static int create_in_jack_mode(struct hda_codec
*codec
, hda_nid_t pin
)
2867 struct hda_gen_spec
*spec
= codec
->spec
;
2868 struct snd_kcontrol_new
*knew
;
2869 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
2870 unsigned int defcfg
;
2872 if (pin
== spec
->hp_mic_pin
)
2873 return 0; /* already done in create_out_jack_mode() */
2875 /* no jack mode for fixed pins */
2876 defcfg
= snd_hda_codec_get_pincfg(codec
, pin
);
2877 if (snd_hda_get_input_pin_attr(defcfg
) == INPUT_PIN_ATTR_INT
)
2880 /* no multiple vref caps? */
2881 if (get_in_jack_num_items(codec
, pin
) <= 1)
2884 get_jack_mode_name(codec
, pin
, name
, sizeof(name
));
2885 knew
= snd_hda_gen_add_kctl(spec
, name
, &in_jack_mode_enum
);
2888 knew
->private_value
= pin
;
2893 * HP/mic shared jack mode
2895 static int hp_mic_jack_mode_info(struct snd_kcontrol
*kcontrol
,
2896 struct snd_ctl_elem_info
*uinfo
)
2898 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2899 hda_nid_t nid
= kcontrol
->private_value
;
2900 int out_jacks
= get_out_jack_num_items(codec
, nid
);
2901 int in_jacks
= get_in_jack_num_items(codec
, nid
);
2902 const char *text
= NULL
;
2905 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2907 uinfo
->value
.enumerated
.items
= out_jacks
+ in_jacks
;
2908 if (uinfo
->value
.enumerated
.item
>= uinfo
->value
.enumerated
.items
)
2909 uinfo
->value
.enumerated
.item
= uinfo
->value
.enumerated
.items
- 1;
2910 idx
= uinfo
->value
.enumerated
.item
;
2911 if (idx
< out_jacks
) {
2913 text
= out_jack_texts
[idx
];
2915 text
= "Headphone Out";
2919 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2920 text
= vref_texts
[get_vref_idx(vref_caps
, idx
)];
2925 strcpy(uinfo
->value
.enumerated
.name
, text
);
2929 static int get_cur_hp_mic_jack_mode(struct hda_codec
*codec
, hda_nid_t nid
)
2931 int out_jacks
= get_out_jack_num_items(codec
, nid
);
2932 int in_jacks
= get_in_jack_num_items(codec
, nid
);
2933 unsigned int val
= snd_hda_codec_get_pin_target(codec
, nid
);
2936 if (val
& PIN_OUT
) {
2937 if (out_jacks
> 1 && val
== PIN_HP
)
2939 } else if (val
& PIN_IN
) {
2942 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2943 val
&= AC_PINCTL_VREFEN
;
2944 idx
+= cvt_from_vref_idx(vref_caps
, val
);
2950 static int hp_mic_jack_mode_get(struct snd_kcontrol
*kcontrol
,
2951 struct snd_ctl_elem_value
*ucontrol
)
2953 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2954 hda_nid_t nid
= kcontrol
->private_value
;
2955 ucontrol
->value
.enumerated
.item
[0] =
2956 get_cur_hp_mic_jack_mode(codec
, nid
);
2960 static int hp_mic_jack_mode_put(struct snd_kcontrol
*kcontrol
,
2961 struct snd_ctl_elem_value
*ucontrol
)
2963 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2964 hda_nid_t nid
= kcontrol
->private_value
;
2965 int out_jacks
= get_out_jack_num_items(codec
, nid
);
2966 int in_jacks
= get_in_jack_num_items(codec
, nid
);
2967 unsigned int val
, oldval
, idx
;
2969 oldval
= get_cur_hp_mic_jack_mode(codec
, nid
);
2970 idx
= ucontrol
->value
.enumerated
.item
[0];
2974 if (idx
< out_jacks
) {
2976 val
= idx
? PIN_HP
: PIN_OUT
;
2982 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2983 val
= snd_hda_codec_get_pin_target(codec
, nid
);
2984 val
&= ~(AC_PINCTL_VREFEN
| PIN_HP
);
2985 val
|= get_vref_idx(vref_caps
, idx
) | PIN_IN
;
2987 val
= snd_hda_get_default_vref(codec
, nid
) | PIN_IN
;
2989 snd_hda_set_pin_ctl_cache(codec
, nid
, val
);
2990 call_hp_automute(codec
, NULL
);
2995 static const struct snd_kcontrol_new hp_mic_jack_mode_enum
= {
2996 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2997 .info
= hp_mic_jack_mode_info
,
2998 .get
= hp_mic_jack_mode_get
,
2999 .put
= hp_mic_jack_mode_put
,
3002 static int create_hp_mic_jack_mode(struct hda_codec
*codec
, hda_nid_t pin
)
3004 struct hda_gen_spec
*spec
= codec
->spec
;
3005 struct snd_kcontrol_new
*knew
;
3007 knew
= snd_hda_gen_add_kctl(spec
, "Headphone Mic Jack Mode",
3008 &hp_mic_jack_mode_enum
);
3011 knew
->private_value
= pin
;
3012 spec
->hp_mic_jack_modes
= 1;
3020 /* add the powersave loopback-list entry */
3021 static int add_loopback_list(struct hda_gen_spec
*spec
, hda_nid_t mix
, int idx
)
3023 struct hda_amp_list
*list
;
3025 list
= snd_array_new(&spec
->loopback_list
);
3029 list
->dir
= HDA_INPUT
;
3031 spec
->loopback
.amplist
= spec
->loopback_list
.list
;
3035 /* return true if either a volume or a mute amp is found for the given
3036 * aamix path; the amp has to be either in the mixer node or its direct leaf
3038 static bool look_for_mix_leaf_ctls(struct hda_codec
*codec
, hda_nid_t mix_nid
,
3039 hda_nid_t pin
, unsigned int *mix_val
,
3040 unsigned int *mute_val
)
3043 const hda_nid_t
*list
;
3046 idx
= snd_hda_get_conn_index(codec
, mix_nid
, pin
, true);
3050 *mix_val
= *mute_val
= 0;
3051 if (nid_has_volume(codec
, mix_nid
, HDA_INPUT
))
3052 *mix_val
= HDA_COMPOSE_AMP_VAL(mix_nid
, 3, idx
, HDA_INPUT
);
3053 if (nid_has_mute(codec
, mix_nid
, HDA_INPUT
))
3054 *mute_val
= HDA_COMPOSE_AMP_VAL(mix_nid
, 3, idx
, HDA_INPUT
);
3055 if (*mix_val
&& *mute_val
)
3058 /* check leaf node */
3059 num_conns
= snd_hda_get_conn_list(codec
, mix_nid
, &list
);
3060 if (num_conns
< idx
)
3063 if (!*mix_val
&& nid_has_volume(codec
, nid
, HDA_OUTPUT
) &&
3064 !is_ctl_associated(codec
, nid
, HDA_OUTPUT
, 0, NID_PATH_VOL_CTL
))
3065 *mix_val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3066 if (!*mute_val
&& nid_has_mute(codec
, nid
, HDA_OUTPUT
) &&
3067 !is_ctl_associated(codec
, nid
, HDA_OUTPUT
, 0, NID_PATH_MUTE_CTL
))
3068 *mute_val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3070 return *mix_val
|| *mute_val
;
3073 /* create input playback/capture controls for the given pin */
3074 static int new_analog_input(struct hda_codec
*codec
, int input_idx
,
3075 hda_nid_t pin
, const char *ctlname
, int ctlidx
,
3078 struct hda_gen_spec
*spec
= codec
->spec
;
3079 struct nid_path
*path
;
3080 unsigned int mix_val
, mute_val
;
3083 if (!look_for_mix_leaf_ctls(codec
, mix_nid
, pin
, &mix_val
, &mute_val
))
3086 path
= snd_hda_add_new_path(codec
, pin
, mix_nid
, 0);
3089 print_nid_path(codec
, "loopback", path
);
3090 spec
->loopback_paths
[input_idx
] = snd_hda_get_path_idx(codec
, path
);
3092 idx
= path
->idx
[path
->depth
- 1];
3094 err
= __add_pb_vol_ctrl(spec
, HDA_CTL_WIDGET_VOL
, ctlname
, ctlidx
, mix_val
);
3097 path
->ctls
[NID_PATH_VOL_CTL
] = mix_val
;
3101 err
= __add_pb_sw_ctrl(spec
, HDA_CTL_WIDGET_MUTE
, ctlname
, ctlidx
, mute_val
);
3104 path
->ctls
[NID_PATH_MUTE_CTL
] = mute_val
;
3107 path
->active
= true;
3108 path
->stream_enabled
= true; /* no DAC/ADC involved */
3109 err
= add_loopback_list(spec
, mix_nid
, idx
);
3113 if (spec
->mixer_nid
!= spec
->mixer_merge_nid
&&
3114 !spec
->loopback_merge_path
) {
3115 path
= snd_hda_add_new_path(codec
, spec
->mixer_nid
,
3116 spec
->mixer_merge_nid
, 0);
3118 print_nid_path(codec
, "loopback-merge", path
);
3119 path
->active
= true;
3120 path
->pin_fixed
= true; /* static route */
3121 path
->stream_enabled
= true; /* no DAC/ADC involved */
3122 spec
->loopback_merge_path
=
3123 snd_hda_get_path_idx(codec
, path
);
3130 static int is_input_pin(struct hda_codec
*codec
, hda_nid_t nid
)
3132 unsigned int pincap
= snd_hda_query_pin_caps(codec
, nid
);
3133 return (pincap
& AC_PINCAP_IN
) != 0;
3136 /* Parse the codec tree and retrieve ADCs */
3137 static int fill_adc_nids(struct hda_codec
*codec
)
3139 struct hda_gen_spec
*spec
= codec
->spec
;
3141 hda_nid_t
*adc_nids
= spec
->adc_nids
;
3142 int max_nums
= ARRAY_SIZE(spec
->adc_nids
);
3145 for_each_hda_codec_node(nid
, codec
) {
3146 unsigned int caps
= get_wcaps(codec
, nid
);
3147 int type
= get_wcaps_type(caps
);
3149 if (type
!= AC_WID_AUD_IN
|| (caps
& AC_WCAP_DIGITAL
))
3151 adc_nids
[nums
] = nid
;
3152 if (++nums
>= max_nums
)
3155 spec
->num_adc_nids
= nums
;
3157 /* copy the detected ADCs to all_adcs[] */
3158 spec
->num_all_adcs
= nums
;
3159 memcpy(spec
->all_adcs
, spec
->adc_nids
, nums
* sizeof(hda_nid_t
));
3164 /* filter out invalid adc_nids that don't give all active input pins;
3165 * if needed, check whether dynamic ADC-switching is available
3167 static int check_dyn_adc_switch(struct hda_codec
*codec
)
3169 struct hda_gen_spec
*spec
= codec
->spec
;
3170 struct hda_input_mux
*imux
= &spec
->input_mux
;
3171 unsigned int ok_bits
;
3176 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
3177 for (i
= 0; i
< imux
->num_items
; i
++) {
3178 if (!spec
->input_paths
[i
][n
])
3181 if (i
>= imux
->num_items
) {
3182 ok_bits
|= (1 << n
);
3188 /* check whether ADC-switch is possible */
3189 for (i
= 0; i
< imux
->num_items
; i
++) {
3190 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
3191 if (spec
->input_paths
[i
][n
]) {
3192 spec
->dyn_adc_idx
[i
] = n
;
3198 codec_dbg(codec
, "enabling ADC switching\n");
3199 spec
->dyn_adc_switch
= 1;
3200 } else if (nums
!= spec
->num_adc_nids
) {
3201 /* shrink the invalid adcs and input paths */
3203 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
3204 if (!(ok_bits
& (1 << n
)))
3207 spec
->adc_nids
[nums
] = spec
->adc_nids
[n
];
3208 for (i
= 0; i
< imux
->num_items
; i
++) {
3209 invalidate_nid_path(codec
,
3210 spec
->input_paths
[i
][nums
]);
3211 spec
->input_paths
[i
][nums
] =
3212 spec
->input_paths
[i
][n
];
3213 spec
->input_paths
[i
][n
] = 0;
3218 spec
->num_adc_nids
= nums
;
3221 if (imux
->num_items
== 1 ||
3222 (imux
->num_items
== 2 && spec
->hp_mic
)) {
3223 codec_dbg(codec
, "reducing to a single ADC\n");
3224 spec
->num_adc_nids
= 1; /* reduce to a single ADC */
3227 /* single index for individual volumes ctls */
3228 if (!spec
->dyn_adc_switch
&& spec
->multi_cap_vol
)
3229 spec
->num_adc_nids
= 1;
3234 /* parse capture source paths from the given pin and create imux items */
3235 static int parse_capture_source(struct hda_codec
*codec
, hda_nid_t pin
,
3236 int cfg_idx
, int num_adcs
,
3237 const char *label
, int anchor
)
3239 struct hda_gen_spec
*spec
= codec
->spec
;
3240 struct hda_input_mux
*imux
= &spec
->input_mux
;
3241 int imux_idx
= imux
->num_items
;
3242 bool imux_added
= false;
3245 for (c
= 0; c
< num_adcs
; c
++) {
3246 struct nid_path
*path
;
3247 hda_nid_t adc
= spec
->adc_nids
[c
];
3249 if (!is_reachable_path(codec
, pin
, adc
))
3251 path
= snd_hda_add_new_path(codec
, pin
, adc
, anchor
);
3254 print_nid_path(codec
, "input", path
);
3255 spec
->input_paths
[imux_idx
][c
] =
3256 snd_hda_get_path_idx(codec
, path
);
3259 if (spec
->hp_mic_pin
== pin
)
3260 spec
->hp_mic_mux_idx
= imux
->num_items
;
3261 spec
->imux_pins
[imux
->num_items
] = pin
;
3262 snd_hda_add_imux_item(codec
, imux
, label
, cfg_idx
, NULL
);
3264 if (spec
->dyn_adc_switch
)
3265 spec
->dyn_adc_idx
[imux_idx
] = c
;
3273 * create playback/capture controls for input pins
3276 /* fill the label for each input at first */
3277 static int fill_input_pin_labels(struct hda_codec
*codec
)
3279 struct hda_gen_spec
*spec
= codec
->spec
;
3280 const struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3283 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
3284 hda_nid_t pin
= cfg
->inputs
[i
].pin
;
3288 if (!is_input_pin(codec
, pin
))
3291 label
= hda_get_autocfg_input_label(codec
, cfg
, i
);
3293 for (j
= i
- 1; j
>= 0; j
--) {
3294 if (spec
->input_labels
[j
] &&
3295 !strcmp(spec
->input_labels
[j
], label
)) {
3296 idx
= spec
->input_label_idxs
[j
] + 1;
3301 spec
->input_labels
[i
] = label
;
3302 spec
->input_label_idxs
[i
] = idx
;
3308 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3310 static int create_input_ctls(struct hda_codec
*codec
)
3312 struct hda_gen_spec
*spec
= codec
->spec
;
3313 const struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3314 hda_nid_t mixer
= spec
->mixer_nid
;
3319 num_adcs
= fill_adc_nids(codec
);
3323 err
= fill_input_pin_labels(codec
);
3327 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
3330 pin
= cfg
->inputs
[i
].pin
;
3331 if (!is_input_pin(codec
, pin
))
3335 if (cfg
->inputs
[i
].type
== AUTO_PIN_MIC
)
3336 val
|= snd_hda_get_default_vref(codec
, pin
);
3337 if (pin
!= spec
->hp_mic_pin
&&
3338 !snd_hda_codec_get_pin_target(codec
, pin
))
3339 set_pin_target(codec
, pin
, val
, false);
3342 if (is_reachable_path(codec
, pin
, mixer
)) {
3343 err
= new_analog_input(codec
, i
, pin
,
3344 spec
->input_labels
[i
],
3345 spec
->input_label_idxs
[i
],
3352 err
= parse_capture_source(codec
, pin
, i
, num_adcs
,
3353 spec
->input_labels
[i
], -mixer
);
3357 if (spec
->add_jack_modes
) {
3358 err
= create_in_jack_mode(codec
, pin
);
3364 /* add stereo mix when explicitly enabled via hint */
3365 if (mixer
&& spec
->add_stereo_mix_input
== HDA_HINT_STEREO_MIX_ENABLE
) {
3366 err
= parse_capture_source(codec
, mixer
, CFG_IDX_MIX
, num_adcs
,
3371 spec
->suppress_auto_mic
= 1;
3382 /* get the input path specified by the given adc and imux indices */
3383 static struct nid_path
*get_input_path(struct hda_codec
*codec
, int adc_idx
, int imux_idx
)
3385 struct hda_gen_spec
*spec
= codec
->spec
;
3386 if (imux_idx
< 0 || imux_idx
>= HDA_MAX_NUM_INPUTS
) {
3390 if (spec
->dyn_adc_switch
)
3391 adc_idx
= spec
->dyn_adc_idx
[imux_idx
];
3392 if (adc_idx
< 0 || adc_idx
>= AUTO_CFG_MAX_INS
) {
3396 return snd_hda_get_path_from_idx(codec
, spec
->input_paths
[imux_idx
][adc_idx
]);
3399 static int mux_select(struct hda_codec
*codec
, unsigned int adc_idx
,
3402 static int mux_enum_info(struct snd_kcontrol
*kcontrol
,
3403 struct snd_ctl_elem_info
*uinfo
)
3405 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3406 struct hda_gen_spec
*spec
= codec
->spec
;
3407 return snd_hda_input_mux_info(&spec
->input_mux
, uinfo
);
3410 static int mux_enum_get(struct snd_kcontrol
*kcontrol
,
3411 struct snd_ctl_elem_value
*ucontrol
)
3413 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3414 struct hda_gen_spec
*spec
= codec
->spec
;
3415 /* the ctls are created at once with multiple counts */
3416 unsigned int adc_idx
= snd_ctl_get_ioffidx(kcontrol
, &ucontrol
->id
);
3418 ucontrol
->value
.enumerated
.item
[0] = spec
->cur_mux
[adc_idx
];
3422 static int mux_enum_put(struct snd_kcontrol
*kcontrol
,
3423 struct snd_ctl_elem_value
*ucontrol
)
3425 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3426 unsigned int adc_idx
= snd_ctl_get_ioffidx(kcontrol
, &ucontrol
->id
);
3427 return mux_select(codec
, adc_idx
,
3428 ucontrol
->value
.enumerated
.item
[0]);
3431 static const struct snd_kcontrol_new cap_src_temp
= {
3432 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3433 .name
= "Input Source",
3434 .info
= mux_enum_info
,
3435 .get
= mux_enum_get
,
3436 .put
= mux_enum_put
,
3440 * capture volume and capture switch ctls
3443 typedef int (*put_call_t
)(struct snd_kcontrol
*kcontrol
,
3444 struct snd_ctl_elem_value
*ucontrol
);
3446 /* call the given amp update function for all amps in the imux list at once */
3447 static int cap_put_caller(struct snd_kcontrol
*kcontrol
,
3448 struct snd_ctl_elem_value
*ucontrol
,
3449 put_call_t func
, int type
)
3451 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3452 struct hda_gen_spec
*spec
= codec
->spec
;
3453 const struct hda_input_mux
*imux
;
3454 struct nid_path
*path
;
3455 int i
, adc_idx
, err
= 0;
3457 imux
= &spec
->input_mux
;
3458 adc_idx
= kcontrol
->id
.index
;
3459 mutex_lock(&codec
->control_mutex
);
3460 for (i
= 0; i
< imux
->num_items
; i
++) {
3461 path
= get_input_path(codec
, adc_idx
, i
);
3462 if (!path
|| !path
->ctls
[type
])
3464 kcontrol
->private_value
= path
->ctls
[type
];
3465 err
= func(kcontrol
, ucontrol
);
3469 mutex_unlock(&codec
->control_mutex
);
3470 if (err
>= 0 && spec
->cap_sync_hook
)
3471 spec
->cap_sync_hook(codec
, kcontrol
, ucontrol
);
3475 /* capture volume ctl callbacks */
3476 #define cap_vol_info snd_hda_mixer_amp_volume_info
3477 #define cap_vol_get snd_hda_mixer_amp_volume_get
3478 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3480 static int cap_vol_put(struct snd_kcontrol
*kcontrol
,
3481 struct snd_ctl_elem_value
*ucontrol
)
3483 return cap_put_caller(kcontrol
, ucontrol
,
3484 snd_hda_mixer_amp_volume_put
,
3488 static const struct snd_kcontrol_new cap_vol_temp
= {
3489 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3490 .name
= "Capture Volume",
3491 .access
= (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
3492 SNDRV_CTL_ELEM_ACCESS_TLV_READ
|
3493 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
),
3494 .info
= cap_vol_info
,
3497 .tlv
= { .c
= cap_vol_tlv
},
3500 /* capture switch ctl callbacks */
3501 #define cap_sw_info snd_ctl_boolean_stereo_info
3502 #define cap_sw_get snd_hda_mixer_amp_switch_get
3504 static int cap_sw_put(struct snd_kcontrol
*kcontrol
,
3505 struct snd_ctl_elem_value
*ucontrol
)
3507 return cap_put_caller(kcontrol
, ucontrol
,
3508 snd_hda_mixer_amp_switch_put
,
3512 static const struct snd_kcontrol_new cap_sw_temp
= {
3513 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3514 .name
= "Capture Switch",
3515 .info
= cap_sw_info
,
3520 static int parse_capvol_in_path(struct hda_codec
*codec
, struct nid_path
*path
)
3525 path
->ctls
[NID_PATH_VOL_CTL
] = path
->ctls
[NID_PATH_MUTE_CTL
] = 0;
3526 for (depth
= 0; depth
< 3; depth
++) {
3527 if (depth
>= path
->depth
)
3529 i
= path
->depth
- depth
- 1;
3530 nid
= path
->path
[i
];
3531 if (!path
->ctls
[NID_PATH_VOL_CTL
]) {
3532 if (nid_has_volume(codec
, nid
, HDA_OUTPUT
))
3533 path
->ctls
[NID_PATH_VOL_CTL
] =
3534 HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3535 else if (nid_has_volume(codec
, nid
, HDA_INPUT
)) {
3536 int idx
= path
->idx
[i
];
3537 if (!depth
&& codec
->single_adc_amp
)
3539 path
->ctls
[NID_PATH_VOL_CTL
] =
3540 HDA_COMPOSE_AMP_VAL(nid
, 3, idx
, HDA_INPUT
);
3543 if (!path
->ctls
[NID_PATH_MUTE_CTL
]) {
3544 if (nid_has_mute(codec
, nid
, HDA_OUTPUT
))
3545 path
->ctls
[NID_PATH_MUTE_CTL
] =
3546 HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3547 else if (nid_has_mute(codec
, nid
, HDA_INPUT
)) {
3548 int idx
= path
->idx
[i
];
3549 if (!depth
&& codec
->single_adc_amp
)
3551 path
->ctls
[NID_PATH_MUTE_CTL
] =
3552 HDA_COMPOSE_AMP_VAL(nid
, 3, idx
, HDA_INPUT
);
3559 static bool is_inv_dmic_pin(struct hda_codec
*codec
, hda_nid_t nid
)
3561 struct hda_gen_spec
*spec
= codec
->spec
;
3562 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3566 if (!spec
->inv_dmic_split
)
3568 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
3569 if (cfg
->inputs
[i
].pin
!= nid
)
3571 if (cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
3573 val
= snd_hda_codec_get_pincfg(codec
, nid
);
3574 return snd_hda_get_input_pin_attr(val
) == INPUT_PIN_ATTR_INT
;
3579 /* capture switch put callback for a single control with hook call */
3580 static int cap_single_sw_put(struct snd_kcontrol
*kcontrol
,
3581 struct snd_ctl_elem_value
*ucontrol
)
3583 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3584 struct hda_gen_spec
*spec
= codec
->spec
;
3587 ret
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
3591 if (spec
->cap_sync_hook
)
3592 spec
->cap_sync_hook(codec
, kcontrol
, ucontrol
);
3597 static int add_single_cap_ctl(struct hda_codec
*codec
, const char *label
,
3598 int idx
, bool is_switch
, unsigned int ctl
,
3601 struct hda_gen_spec
*spec
= codec
->spec
;
3602 char tmpname
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
3603 int type
= is_switch
? HDA_CTL_WIDGET_MUTE
: HDA_CTL_WIDGET_VOL
;
3604 const char *sfx
= is_switch
? "Switch" : "Volume";
3605 unsigned int chs
= inv_dmic
? 1 : 3;
3606 struct snd_kcontrol_new
*knew
;
3612 snprintf(tmpname
, sizeof(tmpname
),
3613 "%s Capture %s", label
, sfx
);
3615 snprintf(tmpname
, sizeof(tmpname
),
3617 knew
= add_control(spec
, type
, tmpname
, idx
,
3618 amp_val_replace_channels(ctl
, chs
));
3622 knew
->put
= cap_single_sw_put
;
3626 /* Make independent right kcontrol */
3628 snprintf(tmpname
, sizeof(tmpname
),
3629 "Inverted %s Capture %s", label
, sfx
);
3631 snprintf(tmpname
, sizeof(tmpname
),
3632 "Inverted Capture %s", sfx
);
3633 knew
= add_control(spec
, type
, tmpname
, idx
,
3634 amp_val_replace_channels(ctl
, 2));
3638 knew
->put
= cap_single_sw_put
;
3642 /* create single (and simple) capture volume and switch controls */
3643 static int create_single_cap_vol_ctl(struct hda_codec
*codec
, int idx
,
3644 unsigned int vol_ctl
, unsigned int sw_ctl
,
3648 err
= add_single_cap_ctl(codec
, NULL
, idx
, false, vol_ctl
, inv_dmic
);
3651 err
= add_single_cap_ctl(codec
, NULL
, idx
, true, sw_ctl
, inv_dmic
);
3657 /* create bound capture volume and switch controls */
3658 static int create_bind_cap_vol_ctl(struct hda_codec
*codec
, int idx
,
3659 unsigned int vol_ctl
, unsigned int sw_ctl
)
3661 struct hda_gen_spec
*spec
= codec
->spec
;
3662 struct snd_kcontrol_new
*knew
;
3665 knew
= snd_hda_gen_add_kctl(spec
, NULL
, &cap_vol_temp
);
3669 knew
->private_value
= vol_ctl
;
3670 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
3673 knew
= snd_hda_gen_add_kctl(spec
, NULL
, &cap_sw_temp
);
3677 knew
->private_value
= sw_ctl
;
3678 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
3683 /* return the vol ctl when used first in the imux list */
3684 static unsigned int get_first_cap_ctl(struct hda_codec
*codec
, int idx
, int type
)
3686 struct nid_path
*path
;
3690 path
= get_input_path(codec
, 0, idx
);
3693 ctl
= path
->ctls
[type
];
3696 for (i
= 0; i
< idx
- 1; i
++) {
3697 path
= get_input_path(codec
, 0, i
);
3698 if (path
&& path
->ctls
[type
] == ctl
)
3704 /* create individual capture volume and switch controls per input */
3705 static int create_multi_cap_vol_ctl(struct hda_codec
*codec
)
3707 struct hda_gen_spec
*spec
= codec
->spec
;
3708 struct hda_input_mux
*imux
= &spec
->input_mux
;
3711 for (i
= 0; i
< imux
->num_items
; i
++) {
3715 idx
= imux
->items
[i
].index
;
3716 if (idx
>= spec
->autocfg
.num_inputs
)
3718 inv_dmic
= is_inv_dmic_pin(codec
, spec
->imux_pins
[i
]);
3720 for (type
= 0; type
< 2; type
++) {
3721 err
= add_single_cap_ctl(codec
,
3722 spec
->input_labels
[idx
],
3723 spec
->input_label_idxs
[idx
],
3725 get_first_cap_ctl(codec
, i
, type
),
3734 static int create_capture_mixers(struct hda_codec
*codec
)
3736 struct hda_gen_spec
*spec
= codec
->spec
;
3737 struct hda_input_mux
*imux
= &spec
->input_mux
;
3738 int i
, n
, nums
, err
;
3740 if (spec
->dyn_adc_switch
)
3743 nums
= spec
->num_adc_nids
;
3745 if (!spec
->auto_mic
&& imux
->num_items
> 1) {
3746 struct snd_kcontrol_new
*knew
;
3748 name
= nums
> 1 ? "Input Source" : "Capture Source";
3749 knew
= snd_hda_gen_add_kctl(spec
, name
, &cap_src_temp
);
3755 for (n
= 0; n
< nums
; n
++) {
3757 bool multi_cap_vol
= spec
->multi_cap_vol
;
3758 bool inv_dmic
= false;
3762 for (i
= 0; i
< imux
->num_items
; i
++) {
3763 struct nid_path
*path
;
3764 path
= get_input_path(codec
, n
, i
);
3767 parse_capvol_in_path(codec
, path
);
3769 vol
= path
->ctls
[NID_PATH_VOL_CTL
];
3770 else if (vol
!= path
->ctls
[NID_PATH_VOL_CTL
]) {
3772 if (!same_amp_caps(codec
, vol
,
3773 path
->ctls
[NID_PATH_VOL_CTL
], HDA_INPUT
))
3774 multi_cap_vol
= true;
3777 sw
= path
->ctls
[NID_PATH_MUTE_CTL
];
3778 else if (sw
!= path
->ctls
[NID_PATH_MUTE_CTL
]) {
3780 if (!same_amp_caps(codec
, sw
,
3781 path
->ctls
[NID_PATH_MUTE_CTL
], HDA_INPUT
))
3782 multi_cap_vol
= true;
3784 if (is_inv_dmic_pin(codec
, spec
->imux_pins
[i
]))
3789 err
= create_single_cap_vol_ctl(codec
, n
, vol
, sw
,
3791 else if (!multi_cap_vol
&& !inv_dmic
)
3792 err
= create_bind_cap_vol_ctl(codec
, n
, vol
, sw
);
3794 err
= create_multi_cap_vol_ctl(codec
);
3803 * add mic boosts if needed
3806 /* check whether the given amp is feasible as a boost volume */
3807 static bool check_boost_vol(struct hda_codec
*codec
, hda_nid_t nid
,
3812 if (!nid_has_volume(codec
, nid
, dir
) ||
3813 is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_VOL_CTL
) ||
3814 is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_BOOST_CTL
))
3817 step
= (query_amp_caps(codec
, nid
, dir
) & AC_AMPCAP_STEP_SIZE
)
3818 >> AC_AMPCAP_STEP_SIZE_SHIFT
;
3824 /* look for a boost amp in a widget close to the pin */
3825 static unsigned int look_for_boost_amp(struct hda_codec
*codec
,
3826 struct nid_path
*path
)
3828 unsigned int val
= 0;
3832 for (depth
= 0; depth
< 3; depth
++) {
3833 if (depth
>= path
->depth
- 1)
3835 nid
= path
->path
[depth
];
3836 if (depth
&& check_boost_vol(codec
, nid
, HDA_OUTPUT
, 0)) {
3837 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3839 } else if (check_boost_vol(codec
, nid
, HDA_INPUT
,
3840 path
->idx
[depth
])) {
3841 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, path
->idx
[depth
],
3850 static int parse_mic_boost(struct hda_codec
*codec
)
3852 struct hda_gen_spec
*spec
= codec
->spec
;
3853 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3854 struct hda_input_mux
*imux
= &spec
->input_mux
;
3857 if (!spec
->num_adc_nids
)
3860 for (i
= 0; i
< imux
->num_items
; i
++) {
3861 struct nid_path
*path
;
3864 char boost_label
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
3866 idx
= imux
->items
[i
].index
;
3867 if (idx
>= imux
->num_items
)
3870 /* check only line-in and mic pins */
3871 if (cfg
->inputs
[idx
].type
> AUTO_PIN_LINE_IN
)
3874 path
= get_input_path(codec
, 0, i
);
3878 val
= look_for_boost_amp(codec
, path
);
3882 /* create a boost control */
3883 snprintf(boost_label
, sizeof(boost_label
),
3884 "%s Boost Volume", spec
->input_labels
[idx
]);
3885 if (!add_control(spec
, HDA_CTL_WIDGET_VOL
, boost_label
,
3886 spec
->input_label_idxs
[idx
], val
))
3889 path
->ctls
[NID_PATH_BOOST_CTL
] = val
;
3894 #ifdef CONFIG_SND_HDA_GENERIC_LEDS
3896 * vmaster mute LED hook helpers
3899 static int create_mute_led_cdev(struct hda_codec
*codec
,
3900 int (*callback
)(struct led_classdev
*,
3901 enum led_brightness
),
3904 struct led_classdev
*cdev
;
3906 cdev
= devm_kzalloc(&codec
->core
.dev
, sizeof(*cdev
), GFP_KERNEL
);
3910 cdev
->name
= micmute
? "hda::micmute" : "hda::mute";
3911 cdev
->max_brightness
= 1;
3912 cdev
->default_trigger
= micmute
? "audio-micmute" : "audio-mute";
3913 cdev
->brightness_set_blocking
= callback
;
3914 cdev
->brightness
= ledtrig_audio_get(micmute
? LED_AUDIO_MICMUTE
: LED_AUDIO_MUTE
);
3915 cdev
->flags
= LED_CORE_SUSPENDRESUME
;
3917 return devm_led_classdev_register(&codec
->core
.dev
, cdev
);
3920 static void vmaster_update_mute_led(void *private_data
, int enabled
)
3922 ledtrig_audio_set(LED_AUDIO_MUTE
, enabled
? LED_OFF
: LED_ON
);
3926 * snd_dha_gen_add_mute_led_cdev - Create a LED classdev and enable as vmaster mute LED
3927 * @codec: the HDA codec
3928 * @callback: the callback for LED classdev brightness_set_blocking
3930 int snd_hda_gen_add_mute_led_cdev(struct hda_codec
*codec
,
3931 int (*callback
)(struct led_classdev
*,
3932 enum led_brightness
))
3934 struct hda_gen_spec
*spec
= codec
->spec
;
3938 err
= create_mute_led_cdev(codec
, callback
, false);
3940 codec_warn(codec
, "failed to create a mute LED cdev\n");
3945 if (spec
->vmaster_mute
.hook
)
3946 codec_err(codec
, "vmaster hook already present before cdev!\n");
3948 spec
->vmaster_mute
.hook
= vmaster_update_mute_led
;
3949 spec
->vmaster_mute_enum
= 1;
3952 EXPORT_SYMBOL_GPL(snd_hda_gen_add_mute_led_cdev
);
3955 * mic mute LED hook helpers
3960 MICMUTE_LED_FOLLOW_CAPTURE
,
3961 MICMUTE_LED_FOLLOW_MUTE
,
3964 static void call_micmute_led_update(struct hda_codec
*codec
)
3966 struct hda_gen_spec
*spec
= codec
->spec
;
3969 switch (spec
->micmute_led
.led_mode
) {
3970 case MICMUTE_LED_ON
:
3973 case MICMUTE_LED_OFF
:
3976 case MICMUTE_LED_FOLLOW_CAPTURE
:
3977 val
= !!spec
->micmute_led
.capture
;
3979 case MICMUTE_LED_FOLLOW_MUTE
:
3981 val
= !spec
->micmute_led
.capture
;
3985 if (val
== spec
->micmute_led
.led_value
)
3987 spec
->micmute_led
.led_value
= val
;
3988 ledtrig_audio_set(LED_AUDIO_MICMUTE
,
3989 spec
->micmute_led
.led_value
? LED_ON
: LED_OFF
);
3992 static void update_micmute_led(struct hda_codec
*codec
,
3993 struct snd_kcontrol
*kcontrol
,
3994 struct snd_ctl_elem_value
*ucontrol
)
3996 struct hda_gen_spec
*spec
= codec
->spec
;
3999 if (spec
->micmute_led
.old_hook
)
4000 spec
->micmute_led
.old_hook(codec
, kcontrol
, ucontrol
);
4004 mask
= 1U << snd_ctl_get_ioffidx(kcontrol
, &ucontrol
->id
);
4005 if (!strcmp("Capture Switch", ucontrol
->id
.name
)) {
4006 /* TODO: How do I verify if it's a mono or stereo here? */
4007 if (ucontrol
->value
.integer
.value
[0] ||
4008 ucontrol
->value
.integer
.value
[1])
4009 spec
->micmute_led
.capture
|= mask
;
4011 spec
->micmute_led
.capture
&= ~mask
;
4012 call_micmute_led_update(codec
);
4016 static int micmute_led_mode_info(struct snd_kcontrol
*kcontrol
,
4017 struct snd_ctl_elem_info
*uinfo
)
4019 static const char * const texts
[] = {
4020 "On", "Off", "Follow Capture", "Follow Mute",
4023 return snd_ctl_enum_info(uinfo
, 1, ARRAY_SIZE(texts
), texts
);
4026 static int micmute_led_mode_get(struct snd_kcontrol
*kcontrol
,
4027 struct snd_ctl_elem_value
*ucontrol
)
4029 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
4030 struct hda_gen_spec
*spec
= codec
->spec
;
4032 ucontrol
->value
.enumerated
.item
[0] = spec
->micmute_led
.led_mode
;
4036 static int micmute_led_mode_put(struct snd_kcontrol
*kcontrol
,
4037 struct snd_ctl_elem_value
*ucontrol
)
4039 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
4040 struct hda_gen_spec
*spec
= codec
->spec
;
4043 mode
= ucontrol
->value
.enumerated
.item
[0];
4044 if (mode
> MICMUTE_LED_FOLLOW_MUTE
)
4045 mode
= MICMUTE_LED_FOLLOW_MUTE
;
4046 if (mode
== spec
->micmute_led
.led_mode
)
4048 spec
->micmute_led
.led_mode
= mode
;
4049 call_micmute_led_update(codec
);
4053 static const struct snd_kcontrol_new micmute_led_mode_ctl
= {
4054 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
4055 .name
= "Mic Mute-LED Mode",
4056 .info
= micmute_led_mode_info
,
4057 .get
= micmute_led_mode_get
,
4058 .put
= micmute_led_mode_put
,
4061 /* Set up the capture sync hook for controlling the mic-mute LED */
4062 static int add_micmute_led_hook(struct hda_codec
*codec
)
4064 struct hda_gen_spec
*spec
= codec
->spec
;
4066 spec
->micmute_led
.led_mode
= MICMUTE_LED_FOLLOW_MUTE
;
4067 spec
->micmute_led
.capture
= 0;
4068 spec
->micmute_led
.led_value
= 0;
4069 spec
->micmute_led
.old_hook
= spec
->cap_sync_hook
;
4070 spec
->cap_sync_hook
= update_micmute_led
;
4071 if (!snd_hda_gen_add_kctl(spec
, NULL
, &micmute_led_mode_ctl
))
4077 * snd_dha_gen_add_micmute_led_cdev - Create a LED classdev and enable as mic-mute LED
4078 * @codec: the HDA codec
4079 * @callback: the callback for LED classdev brightness_set_blocking
4081 * Called from the codec drivers for offering the mic mute LED controls.
4082 * This creates a LED classdev and sets up the cap_sync_hook that is called at
4083 * each time when the capture mixer switch changes.
4085 * When NULL is passed to @callback, no classdev is created but only the
4086 * LED-trigger is set up.
4088 * Returns 0 or a negative error.
4090 int snd_hda_gen_add_micmute_led_cdev(struct hda_codec
*codec
,
4091 int (*callback
)(struct led_classdev
*,
4092 enum led_brightness
))
4097 err
= create_mute_led_cdev(codec
, callback
, true);
4099 codec_warn(codec
, "failed to create a mic-mute LED cdev\n");
4104 return add_micmute_led_hook(codec
);
4106 EXPORT_SYMBOL_GPL(snd_hda_gen_add_micmute_led_cdev
);
4107 #endif /* CONFIG_SND_HDA_GENERIC_LEDS */
4110 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
4112 static void parse_digital(struct hda_codec
*codec
)
4114 struct hda_gen_spec
*spec
= codec
->spec
;
4115 struct nid_path
*path
;
4117 hda_nid_t dig_nid
, pin
;
4119 /* support multiple SPDIFs; the secondary is set up as a follower */
4121 for (i
= 0; i
< spec
->autocfg
.dig_outs
; i
++) {
4122 pin
= spec
->autocfg
.dig_out_pins
[i
];
4123 dig_nid
= look_for_dac(codec
, pin
, true);
4126 path
= snd_hda_add_new_path(codec
, dig_nid
, pin
, 0);
4129 print_nid_path(codec
, "digout", path
);
4130 path
->active
= true;
4131 path
->pin_fixed
= true; /* no jack detection */
4132 spec
->digout_paths
[i
] = snd_hda_get_path_idx(codec
, path
);
4133 set_pin_target(codec
, pin
, PIN_OUT
, false);
4135 spec
->multiout
.dig_out_nid
= dig_nid
;
4136 spec
->dig_out_type
= spec
->autocfg
.dig_out_type
[0];
4138 spec
->multiout
.follower_dig_outs
= spec
->follower_dig_outs
;
4139 if (nums
>= ARRAY_SIZE(spec
->follower_dig_outs
) - 1)
4141 spec
->follower_dig_outs
[nums
- 1] = dig_nid
;
4146 if (spec
->autocfg
.dig_in_pin
) {
4147 pin
= spec
->autocfg
.dig_in_pin
;
4148 for_each_hda_codec_node(dig_nid
, codec
) {
4149 unsigned int wcaps
= get_wcaps(codec
, dig_nid
);
4150 if (get_wcaps_type(wcaps
) != AC_WID_AUD_IN
)
4152 if (!(wcaps
& AC_WCAP_DIGITAL
))
4154 path
= snd_hda_add_new_path(codec
, pin
, dig_nid
, 0);
4156 print_nid_path(codec
, "digin", path
);
4157 path
->active
= true;
4158 path
->pin_fixed
= true; /* no jack */
4159 spec
->dig_in_nid
= dig_nid
;
4160 spec
->digin_path
= snd_hda_get_path_idx(codec
, path
);
4161 set_pin_target(codec
, pin
, PIN_IN
, false);
4170 * input MUX handling
4173 static bool dyn_adc_pcm_resetup(struct hda_codec
*codec
, int cur
);
4175 /* select the given imux item; either unmute exclusively or select the route */
4176 static int mux_select(struct hda_codec
*codec
, unsigned int adc_idx
,
4179 struct hda_gen_spec
*spec
= codec
->spec
;
4180 const struct hda_input_mux
*imux
;
4181 struct nid_path
*old_path
, *path
;
4183 imux
= &spec
->input_mux
;
4184 if (!imux
->num_items
)
4187 if (idx
>= imux
->num_items
)
4188 idx
= imux
->num_items
- 1;
4189 if (spec
->cur_mux
[adc_idx
] == idx
)
4192 old_path
= get_input_path(codec
, adc_idx
, spec
->cur_mux
[adc_idx
]);
4195 if (old_path
->active
)
4196 snd_hda_activate_path(codec
, old_path
, false, false);
4198 spec
->cur_mux
[adc_idx
] = idx
;
4201 update_hp_mic(codec
, adc_idx
, false);
4203 if (spec
->dyn_adc_switch
)
4204 dyn_adc_pcm_resetup(codec
, idx
);
4206 path
= get_input_path(codec
, adc_idx
, idx
);
4211 snd_hda_activate_path(codec
, path
, true, false);
4212 if (spec
->cap_sync_hook
)
4213 spec
->cap_sync_hook(codec
, NULL
, NULL
);
4214 path_power_down_sync(codec
, old_path
);
4218 /* power up/down widgets in the all paths that match with the given NID
4219 * as terminals (either start- or endpoint)
4221 * returns the last changed NID, or zero if unchanged.
4223 static hda_nid_t
set_path_power(struct hda_codec
*codec
, hda_nid_t nid
,
4224 int pin_state
, int stream_state
)
4226 struct hda_gen_spec
*spec
= codec
->spec
;
4227 hda_nid_t last
, changed
= 0;
4228 struct nid_path
*path
;
4231 snd_array_for_each(&spec
->paths
, n
, path
) {
4234 if (path
->path
[0] == nid
||
4235 path
->path
[path
->depth
- 1] == nid
) {
4236 bool pin_old
= path
->pin_enabled
;
4237 bool stream_old
= path
->stream_enabled
;
4240 path
->pin_enabled
= pin_state
;
4241 if (stream_state
>= 0)
4242 path
->stream_enabled
= stream_state
;
4243 if ((!path
->pin_fixed
&& path
->pin_enabled
!= pin_old
)
4244 || path
->stream_enabled
!= stream_old
) {
4245 last
= path_power_update(codec
, path
, true);
4254 /* check the jack status for power control */
4255 static bool detect_pin_state(struct hda_codec
*codec
, hda_nid_t pin
)
4257 if (!is_jack_detectable(codec
, pin
))
4259 return snd_hda_jack_detect_state(codec
, pin
) != HDA_JACK_NOT_PRESENT
;
4262 /* power up/down the paths of the given pin according to the jack state;
4263 * power = 0/1 : only power up/down if it matches with the jack state,
4264 * < 0 : force power up/down to follow the jack sate
4266 * returns the last changed NID, or zero if unchanged.
4268 static hda_nid_t
set_pin_power_jack(struct hda_codec
*codec
, hda_nid_t pin
,
4273 if (!codec
->power_save_node
)
4276 on
= detect_pin_state(codec
, pin
);
4278 if (power
>= 0 && on
!= power
)
4280 return set_path_power(codec
, pin
, on
, -1);
4283 static void pin_power_callback(struct hda_codec
*codec
,
4284 struct hda_jack_callback
*jack
,
4287 if (jack
&& jack
->nid
)
4288 sync_power_state_change(codec
,
4289 set_pin_power_jack(codec
, jack
->nid
, on
));
4292 /* callback only doing power up -- called at first */
4293 static void pin_power_up_callback(struct hda_codec
*codec
,
4294 struct hda_jack_callback
*jack
)
4296 pin_power_callback(codec
, jack
, true);
4299 /* callback only doing power down -- called at last */
4300 static void pin_power_down_callback(struct hda_codec
*codec
,
4301 struct hda_jack_callback
*jack
)
4303 pin_power_callback(codec
, jack
, false);
4306 /* set up the power up/down callbacks */
4307 static void add_pin_power_ctls(struct hda_codec
*codec
, int num_pins
,
4308 const hda_nid_t
*pins
, bool on
)
4311 hda_jack_callback_fn cb
=
4312 on
? pin_power_up_callback
: pin_power_down_callback
;
4314 for (i
= 0; i
< num_pins
&& pins
[i
]; i
++) {
4315 if (is_jack_detectable(codec
, pins
[i
]))
4316 snd_hda_jack_detect_enable_callback(codec
, pins
[i
], cb
);
4318 set_path_power(codec
, pins
[i
], true, -1);
4322 /* enabled power callback to each available I/O pin with jack detections;
4323 * the digital I/O pins are excluded because of the unreliable detectsion
4325 static void add_all_pin_power_ctls(struct hda_codec
*codec
, bool on
)
4327 struct hda_gen_spec
*spec
= codec
->spec
;
4328 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4331 if (!codec
->power_save_node
)
4333 add_pin_power_ctls(codec
, cfg
->line_outs
, cfg
->line_out_pins
, on
);
4334 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
4335 add_pin_power_ctls(codec
, cfg
->hp_outs
, cfg
->hp_pins
, on
);
4336 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
4337 add_pin_power_ctls(codec
, cfg
->speaker_outs
, cfg
->speaker_pins
, on
);
4338 for (i
= 0; i
< cfg
->num_inputs
; i
++)
4339 add_pin_power_ctls(codec
, 1, &cfg
->inputs
[i
].pin
, on
);
4342 /* sync path power up/down with the jack states of given pins */
4343 static void sync_pin_power_ctls(struct hda_codec
*codec
, int num_pins
,
4344 const hda_nid_t
*pins
)
4348 for (i
= 0; i
< num_pins
&& pins
[i
]; i
++)
4349 if (is_jack_detectable(codec
, pins
[i
]))
4350 set_pin_power_jack(codec
, pins
[i
], -1);
4353 /* sync path power up/down with pins; called at init and resume */
4354 static void sync_all_pin_power_ctls(struct hda_codec
*codec
)
4356 struct hda_gen_spec
*spec
= codec
->spec
;
4357 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4360 if (!codec
->power_save_node
)
4362 sync_pin_power_ctls(codec
, cfg
->line_outs
, cfg
->line_out_pins
);
4363 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
4364 sync_pin_power_ctls(codec
, cfg
->hp_outs
, cfg
->hp_pins
);
4365 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
4366 sync_pin_power_ctls(codec
, cfg
->speaker_outs
, cfg
->speaker_pins
);
4367 for (i
= 0; i
< cfg
->num_inputs
; i
++)
4368 sync_pin_power_ctls(codec
, 1, &cfg
->inputs
[i
].pin
);
4371 /* add fake paths if not present yet */
4372 static int add_fake_paths(struct hda_codec
*codec
, hda_nid_t nid
,
4373 int num_pins
, const hda_nid_t
*pins
)
4375 struct hda_gen_spec
*spec
= codec
->spec
;
4376 struct nid_path
*path
;
4379 for (i
= 0; i
< num_pins
; i
++) {
4382 if (get_nid_path(codec
, nid
, pins
[i
], 0))
4384 path
= snd_array_new(&spec
->paths
);
4387 memset(path
, 0, sizeof(*path
));
4389 path
->path
[0] = nid
;
4390 path
->path
[1] = pins
[i
];
4391 path
->active
= true;
4396 /* create fake paths to all outputs from beep */
4397 static int add_fake_beep_paths(struct hda_codec
*codec
)
4399 struct hda_gen_spec
*spec
= codec
->spec
;
4400 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4401 hda_nid_t nid
= spec
->beep_nid
;
4404 if (!codec
->power_save_node
|| !nid
)
4406 err
= add_fake_paths(codec
, nid
, cfg
->line_outs
, cfg
->line_out_pins
);
4409 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
) {
4410 err
= add_fake_paths(codec
, nid
, cfg
->hp_outs
, cfg
->hp_pins
);
4414 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
4415 err
= add_fake_paths(codec
, nid
, cfg
->speaker_outs
,
4423 /* power up/down beep widget and its output paths */
4424 static void beep_power_hook(struct hda_beep
*beep
, bool on
)
4426 set_path_power(beep
->codec
, beep
->nid
, -1, on
);
4430 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4431 * @codec: the HDA codec
4432 * @pin: NID of pin to fix
4434 int snd_hda_gen_fix_pin_power(struct hda_codec
*codec
, hda_nid_t pin
)
4436 struct hda_gen_spec
*spec
= codec
->spec
;
4437 struct nid_path
*path
;
4439 path
= snd_array_new(&spec
->paths
);
4442 memset(path
, 0, sizeof(*path
));
4444 path
->path
[0] = pin
;
4445 path
->active
= true;
4446 path
->pin_fixed
= true;
4447 path
->stream_enabled
= true;
4450 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power
);
4453 * Jack detections for HP auto-mute and mic-switch
4456 /* check each pin in the given array; returns true if any of them is plugged */
4457 static bool detect_jacks(struct hda_codec
*codec
, int num_pins
, const hda_nid_t
*pins
)
4460 bool present
= false;
4462 for (i
= 0; i
< num_pins
; i
++) {
4463 hda_nid_t nid
= pins
[i
];
4466 /* don't detect pins retasked as inputs */
4467 if (snd_hda_codec_get_pin_target(codec
, nid
) & AC_PINCTL_IN_EN
)
4469 if (snd_hda_jack_detect_state(codec
, nid
) == HDA_JACK_PRESENT
)
4475 /* standard HP/line-out auto-mute helper */
4476 static void do_automute(struct hda_codec
*codec
, int num_pins
, const hda_nid_t
*pins
,
4477 int *paths
, bool mute
)
4479 struct hda_gen_spec
*spec
= codec
->spec
;
4482 for (i
= 0; i
< num_pins
; i
++) {
4483 hda_nid_t nid
= pins
[i
];
4484 unsigned int val
, oldval
;
4488 oldval
= snd_hda_codec_get_pin_target(codec
, nid
);
4489 if (oldval
& PIN_IN
)
4490 continue; /* no mute for inputs */
4492 if (spec
->auto_mute_via_amp
) {
4493 struct nid_path
*path
;
4496 path
= snd_hda_get_path_from_idx(codec
, paths
[i
]);
4499 mute_nid
= get_amp_nid_(path
->ctls
[NID_PATH_MUTE_CTL
]);
4503 spec
->mute_bits
|= (1ULL << mute_nid
);
4505 spec
->mute_bits
&= ~(1ULL << mute_nid
);
4508 /* don't reset VREF value in case it's controlling
4509 * the amp (see alc861_fixup_asus_amp_vref_0f())
4511 if (spec
->keep_vref_in_automute
)
4512 val
= oldval
& ~PIN_HP
;
4517 /* here we call update_pin_ctl() so that the pinctl is
4518 * changed without changing the pinctl target value;
4519 * the original target value will be still referred at
4520 * the init / resume again
4522 update_pin_ctl(codec
, nid
, val
);
4525 set_pin_eapd(codec
, nid
, !mute
);
4526 if (codec
->power_save_node
) {
4529 on
= detect_pin_state(codec
, nid
);
4530 set_path_power(codec
, nid
, on
, -1);
4536 * snd_hda_gen_update_outputs - Toggle outputs muting
4537 * @codec: the HDA codec
4539 * Update the mute status of all outputs based on the current jack states.
4541 void snd_hda_gen_update_outputs(struct hda_codec
*codec
)
4543 struct hda_gen_spec
*spec
= codec
->spec
;
4547 /* Control HP pins/amps depending on master_mute state;
4548 * in general, HP pins/amps control should be enabled in all cases,
4549 * but currently set only for master_mute, just to be safe
4551 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
4552 paths
= spec
->out_paths
;
4554 paths
= spec
->hp_paths
;
4555 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.hp_pins
),
4556 spec
->autocfg
.hp_pins
, paths
, spec
->master_mute
);
4558 if (!spec
->automute_speaker
)
4561 on
= spec
->hp_jack_present
| spec
->line_jack_present
;
4562 on
|= spec
->master_mute
;
4563 spec
->speaker_muted
= on
;
4564 if (spec
->autocfg
.line_out_type
== AUTO_PIN_SPEAKER_OUT
)
4565 paths
= spec
->out_paths
;
4567 paths
= spec
->speaker_paths
;
4568 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.speaker_pins
),
4569 spec
->autocfg
.speaker_pins
, paths
, on
);
4571 /* toggle line-out mutes if needed, too */
4572 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4573 if (spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.hp_pins
[0] ||
4574 spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.speaker_pins
[0])
4576 if (!spec
->automute_lo
)
4579 on
= spec
->hp_jack_present
;
4580 on
|= spec
->master_mute
;
4581 spec
->line_out_muted
= on
;
4582 paths
= spec
->out_paths
;
4583 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.line_out_pins
),
4584 spec
->autocfg
.line_out_pins
, paths
, on
);
4586 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs
);
4588 static void call_update_outputs(struct hda_codec
*codec
)
4590 struct hda_gen_spec
*spec
= codec
->spec
;
4591 if (spec
->automute_hook
)
4592 spec
->automute_hook(codec
);
4594 snd_hda_gen_update_outputs(codec
);
4596 /* sync the whole vmaster followers to reflect the new auto-mute status */
4597 if (spec
->auto_mute_via_amp
&& !codec
->bus
->shutdown
)
4598 snd_ctl_sync_vmaster(spec
->vmaster_mute
.sw_kctl
, false);
4602 * snd_hda_gen_hp_automute - standard HP-automute helper
4603 * @codec: the HDA codec
4604 * @jack: jack object, NULL for the whole
4606 void snd_hda_gen_hp_automute(struct hda_codec
*codec
,
4607 struct hda_jack_callback
*jack
)
4609 struct hda_gen_spec
*spec
= codec
->spec
;
4610 hda_nid_t
*pins
= spec
->autocfg
.hp_pins
;
4611 int num_pins
= ARRAY_SIZE(spec
->autocfg
.hp_pins
);
4613 /* No detection for the first HP jack during indep-HP mode */
4614 if (spec
->indep_hp_enabled
) {
4619 spec
->hp_jack_present
= detect_jacks(codec
, num_pins
, pins
);
4620 if (!spec
->detect_hp
|| (!spec
->automute_speaker
&& !spec
->automute_lo
))
4622 call_update_outputs(codec
);
4624 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute
);
4627 * snd_hda_gen_line_automute - standard line-out-automute helper
4628 * @codec: the HDA codec
4629 * @jack: jack object, NULL for the whole
4631 void snd_hda_gen_line_automute(struct hda_codec
*codec
,
4632 struct hda_jack_callback
*jack
)
4634 struct hda_gen_spec
*spec
= codec
->spec
;
4636 if (spec
->autocfg
.line_out_type
== AUTO_PIN_SPEAKER_OUT
)
4638 /* check LO jack only when it's different from HP */
4639 if (spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.hp_pins
[0])
4642 spec
->line_jack_present
=
4643 detect_jacks(codec
, ARRAY_SIZE(spec
->autocfg
.line_out_pins
),
4644 spec
->autocfg
.line_out_pins
);
4645 if (!spec
->automute_speaker
|| !spec
->detect_lo
)
4647 call_update_outputs(codec
);
4649 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute
);
4652 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4653 * @codec: the HDA codec
4654 * @jack: jack object, NULL for the whole
4656 void snd_hda_gen_mic_autoswitch(struct hda_codec
*codec
,
4657 struct hda_jack_callback
*jack
)
4659 struct hda_gen_spec
*spec
= codec
->spec
;
4662 if (!spec
->auto_mic
)
4665 for (i
= spec
->am_num_entries
- 1; i
> 0; i
--) {
4666 hda_nid_t pin
= spec
->am_entry
[i
].pin
;
4667 /* don't detect pins retasked as outputs */
4668 if (snd_hda_codec_get_pin_target(codec
, pin
) & AC_PINCTL_OUT_EN
)
4670 if (snd_hda_jack_detect_state(codec
, pin
) == HDA_JACK_PRESENT
) {
4671 mux_select(codec
, 0, spec
->am_entry
[i
].idx
);
4675 mux_select(codec
, 0, spec
->am_entry
[0].idx
);
4677 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch
);
4679 /* call appropriate hooks */
4680 static void call_hp_automute(struct hda_codec
*codec
,
4681 struct hda_jack_callback
*jack
)
4683 struct hda_gen_spec
*spec
= codec
->spec
;
4684 if (spec
->hp_automute_hook
)
4685 spec
->hp_automute_hook(codec
, jack
);
4687 snd_hda_gen_hp_automute(codec
, jack
);
4690 static void call_line_automute(struct hda_codec
*codec
,
4691 struct hda_jack_callback
*jack
)
4693 struct hda_gen_spec
*spec
= codec
->spec
;
4694 if (spec
->line_automute_hook
)
4695 spec
->line_automute_hook(codec
, jack
);
4697 snd_hda_gen_line_automute(codec
, jack
);
4700 static void call_mic_autoswitch(struct hda_codec
*codec
,
4701 struct hda_jack_callback
*jack
)
4703 struct hda_gen_spec
*spec
= codec
->spec
;
4704 if (spec
->mic_autoswitch_hook
)
4705 spec
->mic_autoswitch_hook(codec
, jack
);
4707 snd_hda_gen_mic_autoswitch(codec
, jack
);
4710 /* update jack retasking */
4711 static void update_automute_all(struct hda_codec
*codec
)
4713 call_hp_automute(codec
, NULL
);
4714 call_line_automute(codec
, NULL
);
4715 call_mic_autoswitch(codec
, NULL
);
4719 * Auto-Mute mode mixer enum support
4721 static int automute_mode_info(struct snd_kcontrol
*kcontrol
,
4722 struct snd_ctl_elem_info
*uinfo
)
4724 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
4725 struct hda_gen_spec
*spec
= codec
->spec
;
4726 static const char * const texts3
[] = {
4727 "Disabled", "Speaker Only", "Line Out+Speaker"
4730 if (spec
->automute_speaker_possible
&& spec
->automute_lo_possible
)
4731 return snd_hda_enum_helper_info(kcontrol
, uinfo
, 3, texts3
);
4732 return snd_hda_enum_bool_helper_info(kcontrol
, uinfo
);
4735 static int automute_mode_get(struct snd_kcontrol
*kcontrol
,
4736 struct snd_ctl_elem_value
*ucontrol
)
4738 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
4739 struct hda_gen_spec
*spec
= codec
->spec
;
4740 unsigned int val
= 0;
4741 if (spec
->automute_speaker
)
4743 if (spec
->automute_lo
)
4746 ucontrol
->value
.enumerated
.item
[0] = val
;
4750 static int automute_mode_put(struct snd_kcontrol
*kcontrol
,
4751 struct snd_ctl_elem_value
*ucontrol
)
4753 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
4754 struct hda_gen_spec
*spec
= codec
->spec
;
4756 switch (ucontrol
->value
.enumerated
.item
[0]) {
4758 if (!spec
->automute_speaker
&& !spec
->automute_lo
)
4760 spec
->automute_speaker
= 0;
4761 spec
->automute_lo
= 0;
4764 if (spec
->automute_speaker_possible
) {
4765 if (!spec
->automute_lo
&& spec
->automute_speaker
)
4767 spec
->automute_speaker
= 1;
4768 spec
->automute_lo
= 0;
4769 } else if (spec
->automute_lo_possible
) {
4770 if (spec
->automute_lo
)
4772 spec
->automute_lo
= 1;
4777 if (!spec
->automute_lo_possible
|| !spec
->automute_speaker_possible
)
4779 if (spec
->automute_speaker
&& spec
->automute_lo
)
4781 spec
->automute_speaker
= 1;
4782 spec
->automute_lo
= 1;
4787 call_update_outputs(codec
);
4791 static const struct snd_kcontrol_new automute_mode_enum
= {
4792 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
4793 .name
= "Auto-Mute Mode",
4794 .info
= automute_mode_info
,
4795 .get
= automute_mode_get
,
4796 .put
= automute_mode_put
,
4799 static int add_automute_mode_enum(struct hda_codec
*codec
)
4801 struct hda_gen_spec
*spec
= codec
->spec
;
4803 if (!snd_hda_gen_add_kctl(spec
, NULL
, &automute_mode_enum
))
4809 * Check the availability of HP/line-out auto-mute;
4810 * Set up appropriately if really supported
4812 static int check_auto_mute_availability(struct hda_codec
*codec
)
4814 struct hda_gen_spec
*spec
= codec
->spec
;
4815 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4819 if (spec
->suppress_auto_mute
)
4822 if (cfg
->hp_pins
[0])
4824 if (cfg
->line_out_pins
[0])
4826 if (cfg
->speaker_pins
[0])
4828 if (present
< 2) /* need two different output types */
4831 if (!cfg
->speaker_pins
[0] &&
4832 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
) {
4833 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
4834 sizeof(cfg
->speaker_pins
));
4835 cfg
->speaker_outs
= cfg
->line_outs
;
4838 if (!cfg
->hp_pins
[0] &&
4839 cfg
->line_out_type
== AUTO_PIN_HP_OUT
) {
4840 memcpy(cfg
->hp_pins
, cfg
->line_out_pins
,
4841 sizeof(cfg
->hp_pins
));
4842 cfg
->hp_outs
= cfg
->line_outs
;
4845 for (i
= 0; i
< cfg
->hp_outs
; i
++) {
4846 hda_nid_t nid
= cfg
->hp_pins
[i
];
4847 if (!is_jack_detectable(codec
, nid
))
4849 codec_dbg(codec
, "Enable HP auto-muting on NID 0x%x\n", nid
);
4850 snd_hda_jack_detect_enable_callback(codec
, nid
,
4852 spec
->detect_hp
= 1;
4855 if (cfg
->line_out_type
== AUTO_PIN_LINE_OUT
&& cfg
->line_outs
) {
4856 if (cfg
->speaker_outs
)
4857 for (i
= 0; i
< cfg
->line_outs
; i
++) {
4858 hda_nid_t nid
= cfg
->line_out_pins
[i
];
4859 if (!is_jack_detectable(codec
, nid
))
4861 codec_dbg(codec
, "Enable Line-Out auto-muting on NID 0x%x\n", nid
);
4862 snd_hda_jack_detect_enable_callback(codec
, nid
,
4863 call_line_automute
);
4864 spec
->detect_lo
= 1;
4866 spec
->automute_lo_possible
= spec
->detect_hp
;
4869 spec
->automute_speaker_possible
= cfg
->speaker_outs
&&
4870 (spec
->detect_hp
|| spec
->detect_lo
);
4872 spec
->automute_lo
= spec
->automute_lo_possible
;
4873 spec
->automute_speaker
= spec
->automute_speaker_possible
;
4875 if (spec
->automute_speaker_possible
|| spec
->automute_lo_possible
) {
4876 /* create a control for automute mode */
4877 err
= add_automute_mode_enum(codec
);
4884 /* check whether all auto-mic pins are valid; setup indices if OK */
4885 static bool auto_mic_check_imux(struct hda_codec
*codec
)
4887 struct hda_gen_spec
*spec
= codec
->spec
;
4888 const struct hda_input_mux
*imux
;
4891 imux
= &spec
->input_mux
;
4892 for (i
= 0; i
< spec
->am_num_entries
; i
++) {
4893 spec
->am_entry
[i
].idx
=
4894 find_idx_in_nid_list(spec
->am_entry
[i
].pin
,
4895 spec
->imux_pins
, imux
->num_items
);
4896 if (spec
->am_entry
[i
].idx
< 0)
4897 return false; /* no corresponding imux */
4900 /* we don't need the jack detection for the first pin */
4901 for (i
= 1; i
< spec
->am_num_entries
; i
++)
4902 snd_hda_jack_detect_enable_callback(codec
,
4903 spec
->am_entry
[i
].pin
,
4904 call_mic_autoswitch
);
4908 static int compare_attr(const void *ap
, const void *bp
)
4910 const struct automic_entry
*a
= ap
;
4911 const struct automic_entry
*b
= bp
;
4912 return (int)(a
->attr
- b
->attr
);
4916 * Check the availability of auto-mic switch;
4917 * Set up if really supported
4919 static int check_auto_mic_availability(struct hda_codec
*codec
)
4921 struct hda_gen_spec
*spec
= codec
->spec
;
4922 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4926 if (spec
->suppress_auto_mic
)
4931 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
4932 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
4934 attr
= snd_hda_codec_get_pincfg(codec
, nid
);
4935 attr
= snd_hda_get_input_pin_attr(attr
);
4936 if (types
& (1 << attr
))
4937 return 0; /* already occupied */
4939 case INPUT_PIN_ATTR_INT
:
4940 if (cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
4941 return 0; /* invalid type */
4943 case INPUT_PIN_ATTR_UNUSED
:
4944 return 0; /* invalid entry */
4946 if (cfg
->inputs
[i
].type
> AUTO_PIN_LINE_IN
)
4947 return 0; /* invalid type */
4948 if (!spec
->line_in_auto_switch
&&
4949 cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
4950 return 0; /* only mic is allowed */
4951 if (!is_jack_detectable(codec
, nid
))
4952 return 0; /* no unsol support */
4955 if (num_pins
>= MAX_AUTO_MIC_PINS
)
4957 types
|= (1 << attr
);
4958 spec
->am_entry
[num_pins
].pin
= nid
;
4959 spec
->am_entry
[num_pins
].attr
= attr
;
4966 spec
->am_num_entries
= num_pins
;
4967 /* sort the am_entry in the order of attr so that the pin with a
4968 * higher attr will be selected when the jack is plugged.
4970 sort(spec
->am_entry
, num_pins
, sizeof(spec
->am_entry
[0]),
4971 compare_attr
, NULL
);
4973 if (!auto_mic_check_imux(codec
))
4977 spec
->num_adc_nids
= 1;
4978 spec
->cur_mux
[0] = spec
->am_entry
[0].idx
;
4979 codec_dbg(codec
, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4980 spec
->am_entry
[0].pin
,
4981 spec
->am_entry
[1].pin
,
4982 spec
->am_entry
[2].pin
);
4988 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4990 * @codec: the HDA codec
4991 * @nid: NID to evalute
4992 * @power_state: target power state
4994 unsigned int snd_hda_gen_path_power_filter(struct hda_codec
*codec
,
4996 unsigned int power_state
)
4998 struct hda_gen_spec
*spec
= codec
->spec
;
5000 if (!spec
->power_down_unused
&& !codec
->power_save_node
)
5002 if (power_state
!= AC_PWRST_D0
|| nid
== codec
->core
.afg
)
5004 if (get_wcaps_type(get_wcaps(codec
, nid
)) >= AC_WID_POWER
)
5006 if (is_active_nid_for_any(codec
, nid
))
5010 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter
);
5012 /* mute all aamix inputs initially; parse up to the first leaves */
5013 static void mute_all_mixer_nid(struct hda_codec
*codec
, hda_nid_t mix
)
5016 const hda_nid_t
*conn
;
5019 nums
= snd_hda_get_conn_list(codec
, mix
, &conn
);
5020 has_amp
= nid_has_mute(codec
, mix
, HDA_INPUT
);
5021 for (i
= 0; i
< nums
; i
++) {
5023 update_amp(codec
, mix
, HDA_INPUT
, i
,
5024 0xff, HDA_AMP_MUTE
);
5025 else if (nid_has_volume(codec
, conn
[i
], HDA_OUTPUT
))
5026 update_amp(codec
, conn
[i
], HDA_OUTPUT
, 0,
5027 0xff, HDA_AMP_MUTE
);
5032 * snd_hda_gen_stream_pm - Stream power management callback
5033 * @codec: the HDA codec
5034 * @nid: audio widget
5035 * @on: power on/off flag
5037 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
5039 void snd_hda_gen_stream_pm(struct hda_codec
*codec
, hda_nid_t nid
, bool on
)
5041 if (codec
->power_save_node
)
5042 set_path_power(codec
, nid
, -1, on
);
5044 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm
);
5047 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
5048 * set up the hda_gen_spec
5049 * @codec: the HDA codec
5050 * @cfg: Parsed pin configuration
5052 * return 1 if successful, 0 if the proper config is not found,
5053 * or a negative error code
5055 int snd_hda_gen_parse_auto_config(struct hda_codec
*codec
,
5056 struct auto_pin_cfg
*cfg
)
5058 struct hda_gen_spec
*spec
= codec
->spec
;
5061 parse_user_hints(codec
);
5063 if (spec
->mixer_nid
&& !spec
->mixer_merge_nid
)
5064 spec
->mixer_merge_nid
= spec
->mixer_nid
;
5066 if (cfg
!= &spec
->autocfg
) {
5067 spec
->autocfg
= *cfg
;
5068 cfg
= &spec
->autocfg
;
5071 if (!spec
->main_out_badness
)
5072 spec
->main_out_badness
= &hda_main_out_badness
;
5073 if (!spec
->extra_out_badness
)
5074 spec
->extra_out_badness
= &hda_extra_out_badness
;
5076 fill_all_dac_nids(codec
);
5078 if (!cfg
->line_outs
) {
5079 if (cfg
->dig_outs
|| cfg
->dig_in_pin
) {
5080 spec
->multiout
.max_channels
= 2;
5081 spec
->no_analog
= 1;
5084 if (!cfg
->num_inputs
&& !cfg
->dig_in_pin
)
5085 return 0; /* can't find valid BIOS pin config */
5088 if (!spec
->no_primary_hp
&&
5089 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
&&
5090 cfg
->line_outs
<= cfg
->hp_outs
) {
5091 /* use HP as primary out */
5092 cfg
->speaker_outs
= cfg
->line_outs
;
5093 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
5094 sizeof(cfg
->speaker_pins
));
5095 cfg
->line_outs
= cfg
->hp_outs
;
5096 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
, sizeof(cfg
->hp_pins
));
5098 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
5099 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
5102 err
= parse_output_paths(codec
);
5105 err
= create_multi_channel_mode(codec
);
5108 err
= create_multi_out_ctls(codec
, cfg
);
5111 err
= create_hp_out_ctls(codec
);
5114 err
= create_speaker_out_ctls(codec
);
5117 err
= create_indep_hp_ctls(codec
);
5120 err
= create_loopback_mixing_ctl(codec
);
5123 err
= create_hp_mic(codec
);
5126 err
= create_input_ctls(codec
);
5130 /* add power-down pin callbacks at first */
5131 add_all_pin_power_ctls(codec
, false);
5133 spec
->const_channel_count
= spec
->ext_channel_count
;
5134 /* check the multiple speaker and headphone pins */
5135 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
5136 spec
->const_channel_count
= max(spec
->const_channel_count
,
5137 cfg
->speaker_outs
* 2);
5138 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
5139 spec
->const_channel_count
= max(spec
->const_channel_count
,
5141 spec
->multiout
.max_channels
= max(spec
->ext_channel_count
,
5142 spec
->const_channel_count
);
5144 err
= check_auto_mute_availability(codec
);
5148 err
= check_dyn_adc_switch(codec
);
5152 err
= check_auto_mic_availability(codec
);
5156 /* add stereo mix if available and not enabled yet */
5157 if (!spec
->auto_mic
&& spec
->mixer_nid
&&
5158 spec
->add_stereo_mix_input
== HDA_HINT_STEREO_MIX_AUTO
&&
5159 spec
->input_mux
.num_items
> 1) {
5160 err
= parse_capture_source(codec
, spec
->mixer_nid
,
5161 CFG_IDX_MIX
, spec
->num_all_adcs
,
5168 err
= create_capture_mixers(codec
);
5172 err
= parse_mic_boost(codec
);
5176 /* create "Headphone Mic Jack Mode" if no input selection is
5177 * available (or user specifies add_jack_modes hint)
5179 if (spec
->hp_mic_pin
&&
5180 (spec
->auto_mic
|| spec
->input_mux
.num_items
== 1 ||
5181 spec
->add_jack_modes
)) {
5182 err
= create_hp_mic_jack_mode(codec
, spec
->hp_mic_pin
);
5187 if (spec
->add_jack_modes
) {
5188 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
5189 err
= create_out_jack_modes(codec
, cfg
->line_outs
,
5190 cfg
->line_out_pins
);
5194 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
) {
5195 err
= create_out_jack_modes(codec
, cfg
->hp_outs
,
5202 /* add power-up pin callbacks at last */
5203 add_all_pin_power_ctls(codec
, true);
5205 /* mute all aamix input initially */
5206 if (spec
->mixer_nid
)
5207 mute_all_mixer_nid(codec
, spec
->mixer_nid
);
5210 parse_digital(codec
);
5212 if (spec
->power_down_unused
|| codec
->power_save_node
) {
5213 if (!codec
->power_filter
)
5214 codec
->power_filter
= snd_hda_gen_path_power_filter
;
5215 if (!codec
->patch_ops
.stream_pm
)
5216 codec
->patch_ops
.stream_pm
= snd_hda_gen_stream_pm
;
5219 if (!spec
->no_analog
&& spec
->beep_nid
) {
5220 err
= snd_hda_attach_beep_device(codec
, spec
->beep_nid
);
5223 if (codec
->beep
&& codec
->power_save_node
) {
5224 err
= add_fake_beep_paths(codec
);
5227 codec
->beep
->power_hook
= beep_power_hook
;
5233 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config
);
5237 * Build control elements
5240 /* follower controls for virtual master */
5241 static const char * const follower_pfxs
[] = {
5242 "Front", "Surround", "Center", "LFE", "Side",
5243 "Headphone", "Speaker", "Mono", "Line Out",
5244 "CLFE", "Bass Speaker", "PCM",
5245 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5246 "Headphone Front", "Headphone Surround", "Headphone CLFE",
5247 "Headphone Side", "Headphone+LO", "Speaker+LO",
5252 * snd_hda_gen_build_controls - Build controls from the parsed results
5253 * @codec: the HDA codec
5255 * Pass this to build_controls patch_ops.
5257 int snd_hda_gen_build_controls(struct hda_codec
*codec
)
5259 struct hda_gen_spec
*spec
= codec
->spec
;
5262 if (spec
->kctls
.used
) {
5263 err
= snd_hda_add_new_ctls(codec
, spec
->kctls
.list
);
5268 if (spec
->multiout
.dig_out_nid
) {
5269 err
= snd_hda_create_dig_out_ctls(codec
,
5270 spec
->multiout
.dig_out_nid
,
5271 spec
->multiout
.dig_out_nid
,
5272 spec
->pcm_rec
[1]->pcm_type
);
5275 if (!spec
->no_analog
) {
5276 err
= snd_hda_create_spdif_share_sw(codec
,
5280 spec
->multiout
.share_spdif
= 1;
5283 if (spec
->dig_in_nid
) {
5284 err
= snd_hda_create_spdif_in_ctls(codec
, spec
->dig_in_nid
);
5289 /* if we have no master control, let's create it */
5290 if (!spec
->no_analog
&& !spec
->suppress_vmaster
&&
5291 !snd_hda_find_mixer_ctl(codec
, "Master Playback Volume")) {
5292 err
= snd_hda_add_vmaster(codec
, "Master Playback Volume",
5293 spec
->vmaster_tlv
, follower_pfxs
,
5298 if (!spec
->no_analog
&& !spec
->suppress_vmaster
&&
5299 !snd_hda_find_mixer_ctl(codec
, "Master Playback Switch")) {
5300 err
= __snd_hda_add_vmaster(codec
, "Master Playback Switch",
5301 NULL
, follower_pfxs
,
5303 true, &spec
->vmaster_mute
.sw_kctl
);
5306 if (spec
->vmaster_mute
.hook
) {
5307 snd_hda_add_vmaster_hook(codec
, &spec
->vmaster_mute
,
5308 spec
->vmaster_mute_enum
);
5309 snd_hda_sync_vmaster_hook(&spec
->vmaster_mute
);
5313 free_kctls(spec
); /* no longer needed */
5315 err
= snd_hda_jack_add_kctls(codec
, &spec
->autocfg
);
5321 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls
);
5328 static void call_pcm_playback_hook(struct hda_pcm_stream
*hinfo
,
5329 struct hda_codec
*codec
,
5330 struct snd_pcm_substream
*substream
,
5333 struct hda_gen_spec
*spec
= codec
->spec
;
5334 if (spec
->pcm_playback_hook
)
5335 spec
->pcm_playback_hook(hinfo
, codec
, substream
, action
);
5338 static void call_pcm_capture_hook(struct hda_pcm_stream
*hinfo
,
5339 struct hda_codec
*codec
,
5340 struct snd_pcm_substream
*substream
,
5343 struct hda_gen_spec
*spec
= codec
->spec
;
5344 if (spec
->pcm_capture_hook
)
5345 spec
->pcm_capture_hook(hinfo
, codec
, substream
, action
);
5349 * Analog playback callbacks
5351 static int playback_pcm_open(struct hda_pcm_stream
*hinfo
,
5352 struct hda_codec
*codec
,
5353 struct snd_pcm_substream
*substream
)
5355 struct hda_gen_spec
*spec
= codec
->spec
;
5358 mutex_lock(&spec
->pcm_mutex
);
5359 err
= snd_hda_multi_out_analog_open(codec
,
5360 &spec
->multiout
, substream
,
5363 spec
->active_streams
|= 1 << STREAM_MULTI_OUT
;
5364 call_pcm_playback_hook(hinfo
, codec
, substream
,
5365 HDA_GEN_PCM_ACT_OPEN
);
5367 mutex_unlock(&spec
->pcm_mutex
);
5371 static int playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5372 struct hda_codec
*codec
,
5373 unsigned int stream_tag
,
5374 unsigned int format
,
5375 struct snd_pcm_substream
*substream
)
5377 struct hda_gen_spec
*spec
= codec
->spec
;
5380 err
= snd_hda_multi_out_analog_prepare(codec
, &spec
->multiout
,
5381 stream_tag
, format
, substream
);
5383 call_pcm_playback_hook(hinfo
, codec
, substream
,
5384 HDA_GEN_PCM_ACT_PREPARE
);
5388 static int playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5389 struct hda_codec
*codec
,
5390 struct snd_pcm_substream
*substream
)
5392 struct hda_gen_spec
*spec
= codec
->spec
;
5395 err
= snd_hda_multi_out_analog_cleanup(codec
, &spec
->multiout
);
5397 call_pcm_playback_hook(hinfo
, codec
, substream
,
5398 HDA_GEN_PCM_ACT_CLEANUP
);
5402 static int playback_pcm_close(struct hda_pcm_stream
*hinfo
,
5403 struct hda_codec
*codec
,
5404 struct snd_pcm_substream
*substream
)
5406 struct hda_gen_spec
*spec
= codec
->spec
;
5407 mutex_lock(&spec
->pcm_mutex
);
5408 spec
->active_streams
&= ~(1 << STREAM_MULTI_OUT
);
5409 call_pcm_playback_hook(hinfo
, codec
, substream
,
5410 HDA_GEN_PCM_ACT_CLOSE
);
5411 mutex_unlock(&spec
->pcm_mutex
);
5415 static int capture_pcm_open(struct hda_pcm_stream
*hinfo
,
5416 struct hda_codec
*codec
,
5417 struct snd_pcm_substream
*substream
)
5419 call_pcm_capture_hook(hinfo
, codec
, substream
, HDA_GEN_PCM_ACT_OPEN
);
5423 static int capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5424 struct hda_codec
*codec
,
5425 unsigned int stream_tag
,
5426 unsigned int format
,
5427 struct snd_pcm_substream
*substream
)
5429 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
5430 call_pcm_capture_hook(hinfo
, codec
, substream
,
5431 HDA_GEN_PCM_ACT_PREPARE
);
5435 static int capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5436 struct hda_codec
*codec
,
5437 struct snd_pcm_substream
*substream
)
5439 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
5440 call_pcm_capture_hook(hinfo
, codec
, substream
,
5441 HDA_GEN_PCM_ACT_CLEANUP
);
5445 static int capture_pcm_close(struct hda_pcm_stream
*hinfo
,
5446 struct hda_codec
*codec
,
5447 struct snd_pcm_substream
*substream
)
5449 call_pcm_capture_hook(hinfo
, codec
, substream
, HDA_GEN_PCM_ACT_CLOSE
);
5453 static int alt_playback_pcm_open(struct hda_pcm_stream
*hinfo
,
5454 struct hda_codec
*codec
,
5455 struct snd_pcm_substream
*substream
)
5457 struct hda_gen_spec
*spec
= codec
->spec
;
5460 mutex_lock(&spec
->pcm_mutex
);
5461 if (spec
->indep_hp
&& !spec
->indep_hp_enabled
)
5464 spec
->active_streams
|= 1 << STREAM_INDEP_HP
;
5465 call_pcm_playback_hook(hinfo
, codec
, substream
,
5466 HDA_GEN_PCM_ACT_OPEN
);
5467 mutex_unlock(&spec
->pcm_mutex
);
5471 static int alt_playback_pcm_close(struct hda_pcm_stream
*hinfo
,
5472 struct hda_codec
*codec
,
5473 struct snd_pcm_substream
*substream
)
5475 struct hda_gen_spec
*spec
= codec
->spec
;
5476 mutex_lock(&spec
->pcm_mutex
);
5477 spec
->active_streams
&= ~(1 << STREAM_INDEP_HP
);
5478 call_pcm_playback_hook(hinfo
, codec
, substream
,
5479 HDA_GEN_PCM_ACT_CLOSE
);
5480 mutex_unlock(&spec
->pcm_mutex
);
5484 static int alt_playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5485 struct hda_codec
*codec
,
5486 unsigned int stream_tag
,
5487 unsigned int format
,
5488 struct snd_pcm_substream
*substream
)
5490 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
5491 call_pcm_playback_hook(hinfo
, codec
, substream
,
5492 HDA_GEN_PCM_ACT_PREPARE
);
5496 static int alt_playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5497 struct hda_codec
*codec
,
5498 struct snd_pcm_substream
*substream
)
5500 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
5501 call_pcm_playback_hook(hinfo
, codec
, substream
,
5502 HDA_GEN_PCM_ACT_CLEANUP
);
5509 static int dig_playback_pcm_open(struct hda_pcm_stream
*hinfo
,
5510 struct hda_codec
*codec
,
5511 struct snd_pcm_substream
*substream
)
5513 struct hda_gen_spec
*spec
= codec
->spec
;
5514 return snd_hda_multi_out_dig_open(codec
, &spec
->multiout
);
5517 static int dig_playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5518 struct hda_codec
*codec
,
5519 unsigned int stream_tag
,
5520 unsigned int format
,
5521 struct snd_pcm_substream
*substream
)
5523 struct hda_gen_spec
*spec
= codec
->spec
;
5524 return snd_hda_multi_out_dig_prepare(codec
, &spec
->multiout
,
5525 stream_tag
, format
, substream
);
5528 static int dig_playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5529 struct hda_codec
*codec
,
5530 struct snd_pcm_substream
*substream
)
5532 struct hda_gen_spec
*spec
= codec
->spec
;
5533 return snd_hda_multi_out_dig_cleanup(codec
, &spec
->multiout
);
5536 static int dig_playback_pcm_close(struct hda_pcm_stream
*hinfo
,
5537 struct hda_codec
*codec
,
5538 struct snd_pcm_substream
*substream
)
5540 struct hda_gen_spec
*spec
= codec
->spec
;
5541 return snd_hda_multi_out_dig_close(codec
, &spec
->multiout
);
5547 #define alt_capture_pcm_open capture_pcm_open
5548 #define alt_capture_pcm_close capture_pcm_close
5550 static int alt_capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5551 struct hda_codec
*codec
,
5552 unsigned int stream_tag
,
5553 unsigned int format
,
5554 struct snd_pcm_substream
*substream
)
5556 struct hda_gen_spec
*spec
= codec
->spec
;
5558 snd_hda_codec_setup_stream(codec
, spec
->adc_nids
[substream
->number
+ 1],
5559 stream_tag
, 0, format
);
5560 call_pcm_capture_hook(hinfo
, codec
, substream
,
5561 HDA_GEN_PCM_ACT_PREPARE
);
5565 static int alt_capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5566 struct hda_codec
*codec
,
5567 struct snd_pcm_substream
*substream
)
5569 struct hda_gen_spec
*spec
= codec
->spec
;
5571 snd_hda_codec_cleanup_stream(codec
,
5572 spec
->adc_nids
[substream
->number
+ 1]);
5573 call_pcm_capture_hook(hinfo
, codec
, substream
,
5574 HDA_GEN_PCM_ACT_CLEANUP
);
5580 static const struct hda_pcm_stream pcm_analog_playback
= {
5584 /* NID is set in build_pcms */
5586 .open
= playback_pcm_open
,
5587 .close
= playback_pcm_close
,
5588 .prepare
= playback_pcm_prepare
,
5589 .cleanup
= playback_pcm_cleanup
5593 static const struct hda_pcm_stream pcm_analog_capture
= {
5597 /* NID is set in build_pcms */
5599 .open
= capture_pcm_open
,
5600 .close
= capture_pcm_close
,
5601 .prepare
= capture_pcm_prepare
,
5602 .cleanup
= capture_pcm_cleanup
5606 static const struct hda_pcm_stream pcm_analog_alt_playback
= {
5610 /* NID is set in build_pcms */
5612 .open
= alt_playback_pcm_open
,
5613 .close
= alt_playback_pcm_close
,
5614 .prepare
= alt_playback_pcm_prepare
,
5615 .cleanup
= alt_playback_pcm_cleanup
5619 static const struct hda_pcm_stream pcm_analog_alt_capture
= {
5620 .substreams
= 2, /* can be overridden */
5623 /* NID is set in build_pcms */
5625 .open
= alt_capture_pcm_open
,
5626 .close
= alt_capture_pcm_close
,
5627 .prepare
= alt_capture_pcm_prepare
,
5628 .cleanup
= alt_capture_pcm_cleanup
5632 static const struct hda_pcm_stream pcm_digital_playback
= {
5636 /* NID is set in build_pcms */
5638 .open
= dig_playback_pcm_open
,
5639 .close
= dig_playback_pcm_close
,
5640 .prepare
= dig_playback_pcm_prepare
,
5641 .cleanup
= dig_playback_pcm_cleanup
5645 static const struct hda_pcm_stream pcm_digital_capture
= {
5649 /* NID is set in build_pcms */
5652 /* Used by build_pcms to flag that a PCM has no playback stream */
5653 static const struct hda_pcm_stream pcm_null_stream
= {
5660 * dynamic changing ADC PCM streams
5662 static bool dyn_adc_pcm_resetup(struct hda_codec
*codec
, int cur
)
5664 struct hda_gen_spec
*spec
= codec
->spec
;
5665 hda_nid_t new_adc
= spec
->adc_nids
[spec
->dyn_adc_idx
[cur
]];
5667 if (spec
->cur_adc
&& spec
->cur_adc
!= new_adc
) {
5668 /* stream is running, let's swap the current ADC */
5669 __snd_hda_codec_cleanup_stream(codec
, spec
->cur_adc
, 1);
5670 spec
->cur_adc
= new_adc
;
5671 snd_hda_codec_setup_stream(codec
, new_adc
,
5672 spec
->cur_adc_stream_tag
, 0,
5673 spec
->cur_adc_format
);
5679 /* analog capture with dynamic dual-adc changes */
5680 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5681 struct hda_codec
*codec
,
5682 unsigned int stream_tag
,
5683 unsigned int format
,
5684 struct snd_pcm_substream
*substream
)
5686 struct hda_gen_spec
*spec
= codec
->spec
;
5687 spec
->cur_adc
= spec
->adc_nids
[spec
->dyn_adc_idx
[spec
->cur_mux
[0]]];
5688 spec
->cur_adc_stream_tag
= stream_tag
;
5689 spec
->cur_adc_format
= format
;
5690 snd_hda_codec_setup_stream(codec
, spec
->cur_adc
, stream_tag
, 0, format
);
5691 call_pcm_capture_hook(hinfo
, codec
, substream
, HDA_GEN_PCM_ACT_PREPARE
);
5695 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5696 struct hda_codec
*codec
,
5697 struct snd_pcm_substream
*substream
)
5699 struct hda_gen_spec
*spec
= codec
->spec
;
5700 snd_hda_codec_cleanup_stream(codec
, spec
->cur_adc
);
5702 call_pcm_capture_hook(hinfo
, codec
, substream
, HDA_GEN_PCM_ACT_CLEANUP
);
5706 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture
= {
5710 .nid
= 0, /* fill later */
5712 .prepare
= dyn_adc_capture_pcm_prepare
,
5713 .cleanup
= dyn_adc_capture_pcm_cleanup
5717 static void fill_pcm_stream_name(char *str
, size_t len
, const char *sfx
,
5718 const char *chip_name
)
5724 strlcpy(str
, chip_name
, len
);
5726 /* drop non-alnum chars after a space */
5727 for (p
= strchr(str
, ' '); p
; p
= strchr(p
+ 1, ' ')) {
5728 if (!isalnum(p
[1])) {
5733 strlcat(str
, sfx
, len
);
5736 /* copy PCM stream info from @default_str, and override non-NULL entries
5737 * from @spec_str and @nid
5739 static void setup_pcm_stream(struct hda_pcm_stream
*str
,
5740 const struct hda_pcm_stream
*default_str
,
5741 const struct hda_pcm_stream
*spec_str
,
5744 *str
= *default_str
;
5748 if (spec_str
->substreams
)
5749 str
->substreams
= spec_str
->substreams
;
5750 if (spec_str
->channels_min
)
5751 str
->channels_min
= spec_str
->channels_min
;
5752 if (spec_str
->channels_max
)
5753 str
->channels_max
= spec_str
->channels_max
;
5754 if (spec_str
->rates
)
5755 str
->rates
= spec_str
->rates
;
5756 if (spec_str
->formats
)
5757 str
->formats
= spec_str
->formats
;
5758 if (spec_str
->maxbps
)
5759 str
->maxbps
= spec_str
->maxbps
;
5764 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5765 * @codec: the HDA codec
5767 * Pass this to build_pcms patch_ops.
5769 int snd_hda_gen_build_pcms(struct hda_codec
*codec
)
5771 struct hda_gen_spec
*spec
= codec
->spec
;
5772 struct hda_pcm
*info
;
5773 bool have_multi_adcs
;
5775 if (spec
->no_analog
)
5778 fill_pcm_stream_name(spec
->stream_name_analog
,
5779 sizeof(spec
->stream_name_analog
),
5780 " Analog", codec
->core
.chip_name
);
5781 info
= snd_hda_codec_pcm_new(codec
, "%s", spec
->stream_name_analog
);
5784 spec
->pcm_rec
[0] = info
;
5786 if (spec
->multiout
.num_dacs
> 0) {
5787 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
],
5788 &pcm_analog_playback
,
5789 spec
->stream_analog_playback
,
5790 spec
->multiout
.dac_nids
[0]);
5791 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].channels_max
=
5792 spec
->multiout
.max_channels
;
5793 if (spec
->autocfg
.line_out_type
== AUTO_PIN_SPEAKER_OUT
&&
5794 spec
->autocfg
.line_outs
== 2)
5795 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].chmap
=
5798 if (spec
->num_adc_nids
) {
5799 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_CAPTURE
],
5800 (spec
->dyn_adc_switch
?
5801 &dyn_adc_pcm_analog_capture
: &pcm_analog_capture
),
5802 spec
->stream_analog_capture
,
5807 /* SPDIF for stream index #1 */
5808 if (spec
->multiout
.dig_out_nid
|| spec
->dig_in_nid
) {
5809 fill_pcm_stream_name(spec
->stream_name_digital
,
5810 sizeof(spec
->stream_name_digital
),
5811 " Digital", codec
->core
.chip_name
);
5812 info
= snd_hda_codec_pcm_new(codec
, "%s",
5813 spec
->stream_name_digital
);
5816 codec
->follower_dig_outs
= spec
->multiout
.follower_dig_outs
;
5817 spec
->pcm_rec
[1] = info
;
5818 if (spec
->dig_out_type
)
5819 info
->pcm_type
= spec
->dig_out_type
;
5821 info
->pcm_type
= HDA_PCM_TYPE_SPDIF
;
5822 if (spec
->multiout
.dig_out_nid
)
5823 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
],
5824 &pcm_digital_playback
,
5825 spec
->stream_digital_playback
,
5826 spec
->multiout
.dig_out_nid
);
5827 if (spec
->dig_in_nid
)
5828 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_CAPTURE
],
5829 &pcm_digital_capture
,
5830 spec
->stream_digital_capture
,
5834 if (spec
->no_analog
)
5837 /* If the use of more than one ADC is requested for the current
5838 * model, configure a second analog capture-only PCM.
5840 have_multi_adcs
= (spec
->num_adc_nids
> 1) &&
5841 !spec
->dyn_adc_switch
&& !spec
->auto_mic
;
5842 /* Additional Analaog capture for index #2 */
5843 if (spec
->alt_dac_nid
|| have_multi_adcs
) {
5844 fill_pcm_stream_name(spec
->stream_name_alt_analog
,
5845 sizeof(spec
->stream_name_alt_analog
),
5846 " Alt Analog", codec
->core
.chip_name
);
5847 info
= snd_hda_codec_pcm_new(codec
, "%s",
5848 spec
->stream_name_alt_analog
);
5851 spec
->pcm_rec
[2] = info
;
5852 if (spec
->alt_dac_nid
)
5853 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
],
5854 &pcm_analog_alt_playback
,
5855 spec
->stream_analog_alt_playback
,
5858 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
],
5859 &pcm_null_stream
, NULL
, 0);
5860 if (have_multi_adcs
) {
5861 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_CAPTURE
],
5862 &pcm_analog_alt_capture
,
5863 spec
->stream_analog_alt_capture
,
5865 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].substreams
=
5866 spec
->num_adc_nids
- 1;
5868 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_CAPTURE
],
5869 &pcm_null_stream
, NULL
, 0);
5875 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms
);
5879 * Standard auto-parser initializations
5882 /* configure the given path as a proper output */
5883 static void set_output_and_unmute(struct hda_codec
*codec
, int path_idx
)
5885 struct nid_path
*path
;
5888 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
5889 if (!path
|| !path
->depth
)
5891 pin
= path
->path
[path
->depth
- 1];
5892 restore_pin_ctl(codec
, pin
);
5893 snd_hda_activate_path(codec
, path
, path
->active
,
5894 aamix_default(codec
->spec
));
5895 set_pin_eapd(codec
, pin
, path
->active
);
5898 /* initialize primary output paths */
5899 static void init_multi_out(struct hda_codec
*codec
)
5901 struct hda_gen_spec
*spec
= codec
->spec
;
5904 for (i
= 0; i
< spec
->autocfg
.line_outs
; i
++)
5905 set_output_and_unmute(codec
, spec
->out_paths
[i
]);
5909 static void __init_extra_out(struct hda_codec
*codec
, int num_outs
, int *paths
)
5913 for (i
= 0; i
< num_outs
; i
++)
5914 set_output_and_unmute(codec
, paths
[i
]);
5917 /* initialize hp and speaker paths */
5918 static void init_extra_out(struct hda_codec
*codec
)
5920 struct hda_gen_spec
*spec
= codec
->spec
;
5922 if (spec
->autocfg
.line_out_type
!= AUTO_PIN_HP_OUT
)
5923 __init_extra_out(codec
, spec
->autocfg
.hp_outs
, spec
->hp_paths
);
5924 if (spec
->autocfg
.line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
5925 __init_extra_out(codec
, spec
->autocfg
.speaker_outs
,
5926 spec
->speaker_paths
);
5929 /* initialize multi-io paths */
5930 static void init_multi_io(struct hda_codec
*codec
)
5932 struct hda_gen_spec
*spec
= codec
->spec
;
5935 for (i
= 0; i
< spec
->multi_ios
; i
++) {
5936 hda_nid_t pin
= spec
->multi_io
[i
].pin
;
5937 struct nid_path
*path
;
5938 path
= get_multiio_path(codec
, i
);
5941 if (!spec
->multi_io
[i
].ctl_in
)
5942 spec
->multi_io
[i
].ctl_in
=
5943 snd_hda_codec_get_pin_target(codec
, pin
);
5944 snd_hda_activate_path(codec
, path
, path
->active
,
5945 aamix_default(spec
));
5949 static void init_aamix_paths(struct hda_codec
*codec
)
5951 struct hda_gen_spec
*spec
= codec
->spec
;
5953 if (!spec
->have_aamix_ctl
)
5955 if (!has_aamix_out_paths(spec
))
5957 update_aamix_paths(codec
, spec
->aamix_mode
, spec
->out_paths
[0],
5958 spec
->aamix_out_paths
[0],
5959 spec
->autocfg
.line_out_type
);
5960 update_aamix_paths(codec
, spec
->aamix_mode
, spec
->hp_paths
[0],
5961 spec
->aamix_out_paths
[1],
5963 update_aamix_paths(codec
, spec
->aamix_mode
, spec
->speaker_paths
[0],
5964 spec
->aamix_out_paths
[2],
5965 AUTO_PIN_SPEAKER_OUT
);
5968 /* set up input pins and loopback paths */
5969 static void init_analog_input(struct hda_codec
*codec
)
5971 struct hda_gen_spec
*spec
= codec
->spec
;
5972 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
5975 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
5976 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
5977 if (is_input_pin(codec
, nid
))
5978 restore_pin_ctl(codec
, nid
);
5980 /* init loopback inputs */
5981 if (spec
->mixer_nid
) {
5982 resume_path_from_idx(codec
, spec
->loopback_paths
[i
]);
5983 resume_path_from_idx(codec
, spec
->loopback_merge_path
);
5988 /* initialize ADC paths */
5989 static void init_input_src(struct hda_codec
*codec
)
5991 struct hda_gen_spec
*spec
= codec
->spec
;
5992 struct hda_input_mux
*imux
= &spec
->input_mux
;
5993 struct nid_path
*path
;
5996 if (spec
->dyn_adc_switch
)
5999 nums
= spec
->num_adc_nids
;
6001 for (c
= 0; c
< nums
; c
++) {
6002 for (i
= 0; i
< imux
->num_items
; i
++) {
6003 path
= get_input_path(codec
, c
, i
);
6005 bool active
= path
->active
;
6006 if (i
== spec
->cur_mux
[c
])
6008 snd_hda_activate_path(codec
, path
, active
, false);
6012 update_hp_mic(codec
, c
, true);
6015 if (spec
->cap_sync_hook
)
6016 spec
->cap_sync_hook(codec
, NULL
, NULL
);
6019 /* set right pin controls for digital I/O */
6020 static void init_digital(struct hda_codec
*codec
)
6022 struct hda_gen_spec
*spec
= codec
->spec
;
6026 for (i
= 0; i
< spec
->autocfg
.dig_outs
; i
++)
6027 set_output_and_unmute(codec
, spec
->digout_paths
[i
]);
6028 pin
= spec
->autocfg
.dig_in_pin
;
6030 restore_pin_ctl(codec
, pin
);
6031 resume_path_from_idx(codec
, spec
->digin_path
);
6035 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
6036 * invalid unsol tags by some reason
6038 static void clear_unsol_on_unused_pins(struct hda_codec
*codec
)
6040 const struct hda_pincfg
*pin
;
6043 snd_array_for_each(&codec
->init_pins
, i
, pin
) {
6044 hda_nid_t nid
= pin
->nid
;
6045 if (is_jack_detectable(codec
, nid
) &&
6046 !snd_hda_jack_tbl_get(codec
, nid
))
6047 snd_hda_codec_write_cache(codec
, nid
, 0,
6048 AC_VERB_SET_UNSOLICITED_ENABLE
, 0);
6053 * snd_hda_gen_init - initialize the generic spec
6054 * @codec: the HDA codec
6056 * This can be put as patch_ops init function.
6058 int snd_hda_gen_init(struct hda_codec
*codec
)
6060 struct hda_gen_spec
*spec
= codec
->spec
;
6062 if (spec
->init_hook
)
6063 spec
->init_hook(codec
);
6065 if (!spec
->skip_verbs
)
6066 snd_hda_apply_verbs(codec
);
6068 init_multi_out(codec
);
6069 init_extra_out(codec
);
6070 init_multi_io(codec
);
6071 init_aamix_paths(codec
);
6072 init_analog_input(codec
);
6073 init_input_src(codec
);
6074 init_digital(codec
);
6076 clear_unsol_on_unused_pins(codec
);
6078 sync_all_pin_power_ctls(codec
);
6080 /* call init functions of standard auto-mute helpers */
6081 update_automute_all(codec
);
6083 snd_hda_regmap_sync(codec
);
6085 if (spec
->vmaster_mute
.sw_kctl
&& spec
->vmaster_mute
.hook
)
6086 snd_hda_sync_vmaster_hook(&spec
->vmaster_mute
);
6088 hda_call_check_power_status(codec
, 0x01);
6091 EXPORT_SYMBOL_GPL(snd_hda_gen_init
);
6094 * snd_hda_gen_free - free the generic spec
6095 * @codec: the HDA codec
6097 * This can be put as patch_ops free function.
6099 void snd_hda_gen_free(struct hda_codec
*codec
)
6101 snd_hda_apply_fixup(codec
, HDA_FIXUP_ACT_FREE
);
6102 snd_hda_gen_spec_free(codec
->spec
);
6106 EXPORT_SYMBOL_GPL(snd_hda_gen_free
);
6109 * snd_hda_gen_reboot_notify - Make codec enter D3 before rebooting
6110 * @codec: the HDA codec
6112 * This can be put as patch_ops reboot_notify function.
6114 void snd_hda_gen_reboot_notify(struct hda_codec
*codec
)
6116 /* Make the codec enter D3 to avoid spurious noises from the internal
6117 * speaker during (and after) reboot
6119 snd_hda_codec_set_power_to_all(codec
, codec
->core
.afg
, AC_PWRST_D3
);
6120 snd_hda_codec_write(codec
, codec
->core
.afg
, 0,
6121 AC_VERB_SET_POWER_STATE
, AC_PWRST_D3
);
6124 EXPORT_SYMBOL_GPL(snd_hda_gen_reboot_notify
);
6128 * snd_hda_gen_check_power_status - check the loopback power save state
6129 * @codec: the HDA codec
6130 * @nid: NID to inspect
6132 * This can be put as patch_ops check_power_status function.
6134 int snd_hda_gen_check_power_status(struct hda_codec
*codec
, hda_nid_t nid
)
6136 struct hda_gen_spec
*spec
= codec
->spec
;
6137 return snd_hda_check_amp_list_power(codec
, &spec
->loopback
, nid
);
6139 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status
);
6144 * the generic codec support
6147 static const struct hda_codec_ops generic_patch_ops
= {
6148 .build_controls
= snd_hda_gen_build_controls
,
6149 .build_pcms
= snd_hda_gen_build_pcms
,
6150 .init
= snd_hda_gen_init
,
6151 .free
= snd_hda_gen_free
,
6152 .unsol_event
= snd_hda_jack_unsol_event
,
6153 .reboot_notify
= snd_hda_gen_reboot_notify
,
6155 .check_power_status
= snd_hda_gen_check_power_status
,
6160 * snd_hda_parse_generic_codec - Generic codec parser
6161 * @codec: the HDA codec
6163 static int snd_hda_parse_generic_codec(struct hda_codec
*codec
)
6165 struct hda_gen_spec
*spec
;
6168 spec
= kzalloc(sizeof(*spec
), GFP_KERNEL
);
6171 snd_hda_gen_spec_init(spec
);
6174 err
= snd_hda_parse_pin_defcfg(codec
, &spec
->autocfg
, NULL
, 0);
6178 err
= snd_hda_gen_parse_auto_config(codec
, &spec
->autocfg
);
6182 codec
->patch_ops
= generic_patch_ops
;
6186 snd_hda_gen_free(codec
);
6190 static const struct hda_device_id snd_hda_id_generic
[] = {
6191 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC
, "Generic", snd_hda_parse_generic_codec
),
6194 MODULE_DEVICE_TABLE(hdaudio
, snd_hda_id_generic
);
6196 static struct hda_codec_driver generic_driver
= {
6197 .id
= snd_hda_id_generic
,
6200 module_hda_codec_driver(generic_driver
);
6202 MODULE_LICENSE("GPL");
6203 MODULE_DESCRIPTION("Generic HD-audio codec parser");