2 * Universal Interface for Intel High Definition Audio Codec
4 * Generic widget tree parser
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/sort.h>
27 #include <linux/delay.h>
28 #include <linux/ctype.h>
29 #include <linux/string.h>
30 #include <linux/bitops.h>
31 #include <linux/module.h>
32 #include <sound/core.h>
33 #include <sound/jack.h>
34 #include <sound/tlv.h>
35 #include "hda_codec.h"
36 #include "hda_local.h"
37 #include "hda_auto_parser.h"
40 #include "hda_generic.h"
44 * snd_hda_gen_spec_init - initialize hda_gen_spec struct
45 * @spec: hda_gen_spec object to initialize
47 * Initialize the given hda_gen_spec object.
49 int snd_hda_gen_spec_init(struct hda_gen_spec
*spec
)
51 snd_array_init(&spec
->kctls
, sizeof(struct snd_kcontrol_new
), 32);
52 snd_array_init(&spec
->paths
, sizeof(struct nid_path
), 8);
53 snd_array_init(&spec
->loopback_list
, sizeof(struct hda_amp_list
), 8);
54 mutex_init(&spec
->pcm_mutex
);
57 EXPORT_SYMBOL_GPL(snd_hda_gen_spec_init
);
60 * snd_hda_gen_add_kctl - Add a new kctl_new struct from the template
61 * @spec: hda_gen_spec object
62 * @name: name string to override the template, NULL if unchanged
63 * @temp: template for the new kctl
65 * Add a new kctl (actually snd_kcontrol_new to be instantiated later)
66 * element based on the given snd_kcontrol_new template @temp and the
67 * name string @name to the list in @spec.
68 * Returns the newly created object or NULL as error.
70 struct snd_kcontrol_new
*
71 snd_hda_gen_add_kctl(struct hda_gen_spec
*spec
, const char *name
,
72 const struct snd_kcontrol_new
*temp
)
74 struct snd_kcontrol_new
*knew
= snd_array_new(&spec
->kctls
);
79 knew
->name
= kstrdup(name
, GFP_KERNEL
);
81 knew
->name
= kstrdup(knew
->name
, GFP_KERNEL
);
86 EXPORT_SYMBOL_GPL(snd_hda_gen_add_kctl
);
88 static void free_kctls(struct hda_gen_spec
*spec
)
90 if (spec
->kctls
.list
) {
91 struct snd_kcontrol_new
*kctl
= spec
->kctls
.list
;
93 for (i
= 0; i
< spec
->kctls
.used
; i
++)
96 snd_array_free(&spec
->kctls
);
99 static void snd_hda_gen_spec_free(struct hda_gen_spec
*spec
)
104 snd_array_free(&spec
->paths
);
105 snd_array_free(&spec
->loopback_list
);
111 static void parse_user_hints(struct hda_codec
*codec
)
113 struct hda_gen_spec
*spec
= codec
->spec
;
116 val
= snd_hda_get_bool_hint(codec
, "jack_detect");
118 codec
->no_jack_detect
= !val
;
119 val
= snd_hda_get_bool_hint(codec
, "inv_jack_detect");
121 codec
->inv_jack_detect
= !!val
;
122 val
= snd_hda_get_bool_hint(codec
, "trigger_sense");
124 codec
->no_trigger_sense
= !val
;
125 val
= snd_hda_get_bool_hint(codec
, "inv_eapd");
127 codec
->inv_eapd
= !!val
;
128 val
= snd_hda_get_bool_hint(codec
, "pcm_format_first");
130 codec
->pcm_format_first
= !!val
;
131 val
= snd_hda_get_bool_hint(codec
, "sticky_stream");
133 codec
->no_sticky_stream
= !val
;
134 val
= snd_hda_get_bool_hint(codec
, "spdif_status_reset");
136 codec
->spdif_status_reset
= !!val
;
137 val
= snd_hda_get_bool_hint(codec
, "pin_amp_workaround");
139 codec
->pin_amp_workaround
= !!val
;
140 val
= snd_hda_get_bool_hint(codec
, "single_adc_amp");
142 codec
->single_adc_amp
= !!val
;
143 val
= snd_hda_get_bool_hint(codec
, "power_save_node");
145 codec
->power_save_node
= !!val
;
147 val
= snd_hda_get_bool_hint(codec
, "auto_mute");
149 spec
->suppress_auto_mute
= !val
;
150 val
= snd_hda_get_bool_hint(codec
, "auto_mic");
152 spec
->suppress_auto_mic
= !val
;
153 val
= snd_hda_get_bool_hint(codec
, "line_in_auto_switch");
155 spec
->line_in_auto_switch
= !!val
;
156 val
= snd_hda_get_bool_hint(codec
, "auto_mute_via_amp");
158 spec
->auto_mute_via_amp
= !!val
;
159 val
= snd_hda_get_bool_hint(codec
, "need_dac_fix");
161 spec
->need_dac_fix
= !!val
;
162 val
= snd_hda_get_bool_hint(codec
, "primary_hp");
164 spec
->no_primary_hp
= !val
;
165 val
= snd_hda_get_bool_hint(codec
, "multi_io");
167 spec
->no_multi_io
= !val
;
168 val
= snd_hda_get_bool_hint(codec
, "multi_cap_vol");
170 spec
->multi_cap_vol
= !!val
;
171 val
= snd_hda_get_bool_hint(codec
, "inv_dmic_split");
173 spec
->inv_dmic_split
= !!val
;
174 val
= snd_hda_get_bool_hint(codec
, "indep_hp");
176 spec
->indep_hp
= !!val
;
177 val
= snd_hda_get_bool_hint(codec
, "add_stereo_mix_input");
179 spec
->add_stereo_mix_input
= !!val
;
180 /* the following two are just for compatibility */
181 val
= snd_hda_get_bool_hint(codec
, "add_out_jack_modes");
183 spec
->add_jack_modes
= !!val
;
184 val
= snd_hda_get_bool_hint(codec
, "add_in_jack_modes");
186 spec
->add_jack_modes
= !!val
;
187 val
= snd_hda_get_bool_hint(codec
, "add_jack_modes");
189 spec
->add_jack_modes
= !!val
;
190 val
= snd_hda_get_bool_hint(codec
, "power_down_unused");
192 spec
->power_down_unused
= !!val
;
193 val
= snd_hda_get_bool_hint(codec
, "add_hp_mic");
195 spec
->hp_mic
= !!val
;
196 val
= snd_hda_get_bool_hint(codec
, "hp_mic_detect");
198 spec
->suppress_hp_mic_detect
= !val
;
200 if (!snd_hda_get_int_hint(codec
, "mixer_nid", &val
))
201 spec
->mixer_nid
= val
;
205 * pin control value accesses
208 #define update_pin_ctl(codec, pin, val) \
209 snd_hda_codec_update_cache(codec, pin, 0, \
210 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
212 /* restore the pinctl based on the cached value */
213 static inline void restore_pin_ctl(struct hda_codec
*codec
, hda_nid_t pin
)
215 update_pin_ctl(codec
, pin
, snd_hda_codec_get_pin_target(codec
, pin
));
218 /* set the pinctl target value and write it if requested */
219 static void set_pin_target(struct hda_codec
*codec
, hda_nid_t pin
,
220 unsigned int val
, bool do_write
)
224 val
= snd_hda_correct_pin_ctl(codec
, pin
, val
);
225 snd_hda_codec_set_pin_target(codec
, pin
, val
);
227 update_pin_ctl(codec
, pin
, val
);
230 /* set pinctl target values for all given pins */
231 static void set_pin_targets(struct hda_codec
*codec
, int num_pins
,
232 hda_nid_t
*pins
, unsigned int val
)
235 for (i
= 0; i
< num_pins
; i
++)
236 set_pin_target(codec
, pins
[i
], val
, false);
243 /* return the position of NID in the list, or -1 if not found */
244 static int find_idx_in_nid_list(hda_nid_t nid
, const hda_nid_t
*list
, int nums
)
247 for (i
= 0; i
< nums
; i
++)
253 /* return true if the given NID is contained in the path */
254 static bool is_nid_contained(struct nid_path
*path
, hda_nid_t nid
)
256 return find_idx_in_nid_list(nid
, path
->path
, path
->depth
) >= 0;
259 static struct nid_path
*get_nid_path(struct hda_codec
*codec
,
260 hda_nid_t from_nid
, hda_nid_t to_nid
,
263 struct hda_gen_spec
*spec
= codec
->spec
;
266 for (i
= 0; i
< spec
->paths
.used
; i
++) {
267 struct nid_path
*path
= snd_array_elem(&spec
->paths
, i
);
268 if (path
->depth
<= 0)
270 if ((!from_nid
|| path
->path
[0] == from_nid
) &&
271 (!to_nid
|| path
->path
[path
->depth
- 1] == to_nid
)) {
273 (anchor_nid
> 0 && is_nid_contained(path
, anchor_nid
)) ||
274 (anchor_nid
< 0 && !is_nid_contained(path
, anchor_nid
)))
282 * snd_hda_get_nid_path - get the path between the given NIDs
283 * @codec: the HDA codec
284 * @from_nid: the NID where the path start from
285 * @to_nid: the NID where the path ends at
287 * Return the found nid_path object or NULL for error.
288 * Passing 0 to either @from_nid or @to_nid behaves as a wildcard.
290 struct nid_path
*snd_hda_get_nid_path(struct hda_codec
*codec
,
291 hda_nid_t from_nid
, hda_nid_t to_nid
)
293 return get_nid_path(codec
, from_nid
, to_nid
, 0);
295 EXPORT_SYMBOL_GPL(snd_hda_get_nid_path
);
298 * snd_hda_get_path_idx - get the index number corresponding to the path
300 * @codec: the HDA codec
301 * @path: nid_path object
303 * The returned index starts from 1, i.e. the actual array index with offset 1,
304 * and zero is handled as an invalid path
306 int snd_hda_get_path_idx(struct hda_codec
*codec
, struct nid_path
*path
)
308 struct hda_gen_spec
*spec
= codec
->spec
;
309 struct nid_path
*array
= spec
->paths
.list
;
312 if (!spec
->paths
.used
)
315 if (idx
< 0 || idx
>= spec
->paths
.used
)
319 EXPORT_SYMBOL_GPL(snd_hda_get_path_idx
);
322 * snd_hda_get_path_from_idx - get the path instance corresponding to the
324 * @codec: the HDA codec
325 * @idx: the path index
327 struct nid_path
*snd_hda_get_path_from_idx(struct hda_codec
*codec
, int idx
)
329 struct hda_gen_spec
*spec
= codec
->spec
;
331 if (idx
<= 0 || idx
> spec
->paths
.used
)
333 return snd_array_elem(&spec
->paths
, idx
- 1);
335 EXPORT_SYMBOL_GPL(snd_hda_get_path_from_idx
);
337 /* check whether the given DAC is already found in any existing paths */
338 static bool is_dac_already_used(struct hda_codec
*codec
, hda_nid_t nid
)
340 struct hda_gen_spec
*spec
= codec
->spec
;
343 for (i
= 0; i
< spec
->paths
.used
; i
++) {
344 struct nid_path
*path
= snd_array_elem(&spec
->paths
, i
);
345 if (path
->path
[0] == nid
)
351 /* check whether the given two widgets can be connected */
352 static bool is_reachable_path(struct hda_codec
*codec
,
353 hda_nid_t from_nid
, hda_nid_t to_nid
)
355 if (!from_nid
|| !to_nid
)
357 return snd_hda_get_conn_index(codec
, to_nid
, from_nid
, true) >= 0;
360 /* nid, dir and idx */
361 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
363 /* check whether the given ctl is already assigned in any path elements */
364 static bool is_ctl_used(struct hda_codec
*codec
, unsigned int val
, int type
)
366 struct hda_gen_spec
*spec
= codec
->spec
;
369 val
&= AMP_VAL_COMPARE_MASK
;
370 for (i
= 0; i
< spec
->paths
.used
; i
++) {
371 struct nid_path
*path
= snd_array_elem(&spec
->paths
, i
);
372 if ((path
->ctls
[type
] & AMP_VAL_COMPARE_MASK
) == val
)
378 /* check whether a control with the given (nid, dir, idx) was assigned */
379 static bool is_ctl_associated(struct hda_codec
*codec
, hda_nid_t nid
,
380 int dir
, int idx
, int type
)
382 unsigned int val
= HDA_COMPOSE_AMP_VAL(nid
, 3, idx
, dir
);
383 return is_ctl_used(codec
, val
, type
);
386 static void print_nid_path(struct hda_codec
*codec
,
387 const char *pfx
, struct nid_path
*path
)
394 for (i
= 0; i
< path
->depth
; i
++)
395 pos
+= scnprintf(pos
, sizeof(buf
) - (pos
- buf
), "%s%02x",
396 pos
!= buf
? ":" : "",
399 codec_dbg(codec
, "%s path: depth=%d '%s'\n", pfx
, path
->depth
, buf
);
402 /* called recursively */
403 static bool __parse_nid_path(struct hda_codec
*codec
,
404 hda_nid_t from_nid
, hda_nid_t to_nid
,
405 int anchor_nid
, struct nid_path
*path
,
408 const hda_nid_t
*conn
;
411 if (to_nid
== anchor_nid
)
412 anchor_nid
= 0; /* anchor passed */
413 else if (to_nid
== (hda_nid_t
)(-anchor_nid
))
414 return false; /* hit the exclusive nid */
416 nums
= snd_hda_get_conn_list(codec
, to_nid
, &conn
);
417 for (i
= 0; i
< nums
; i
++) {
418 if (conn
[i
] != from_nid
) {
419 /* special case: when from_nid is 0,
420 * try to find an empty DAC
423 get_wcaps_type(get_wcaps(codec
, conn
[i
])) != AC_WID_AUD_OUT
||
424 is_dac_already_used(codec
, conn
[i
]))
427 /* anchor is not requested or already passed? */
431 if (depth
>= MAX_NID_PATH_DEPTH
)
433 for (i
= 0; i
< nums
; i
++) {
435 type
= get_wcaps_type(get_wcaps(codec
, conn
[i
]));
436 if (type
== AC_WID_AUD_OUT
|| type
== AC_WID_AUD_IN
||
439 if (__parse_nid_path(codec
, from_nid
, conn
[i
],
440 anchor_nid
, path
, depth
+ 1))
446 path
->path
[path
->depth
] = conn
[i
];
447 path
->idx
[path
->depth
+ 1] = i
;
448 if (nums
> 1 && get_wcaps_type(get_wcaps(codec
, to_nid
)) != AC_WID_AUD_MIX
)
449 path
->multi
[path
->depth
+ 1] = 1;
455 * snd_hda_parse_nid_path - parse the widget path from the given nid to
457 * @codec: the HDA codec
458 * @from_nid: the NID where the path start from
459 * @to_nid: the NID where the path ends at
460 * @anchor_nid: the anchor indication
461 * @path: the path object to store the result
463 * Returns true if a matching path is found.
465 * The parsing behavior depends on parameters:
466 * when @from_nid is 0, try to find an empty DAC;
467 * when @anchor_nid is set to a positive value, only paths through the widget
468 * with the given value are evaluated.
469 * when @anchor_nid is set to a negative value, paths through the widget
470 * with the negative of given value are excluded, only other paths are chosen.
471 * when @anchor_nid is zero, no special handling about path selection.
473 bool snd_hda_parse_nid_path(struct hda_codec
*codec
, hda_nid_t from_nid
,
474 hda_nid_t to_nid
, int anchor_nid
,
475 struct nid_path
*path
)
477 if (__parse_nid_path(codec
, from_nid
, to_nid
, anchor_nid
, path
, 1)) {
478 path
->path
[path
->depth
] = to_nid
;
484 EXPORT_SYMBOL_GPL(snd_hda_parse_nid_path
);
487 * snd_hda_add_new_path - parse the path between the given NIDs and
488 * add to the path list
489 * @codec: the HDA codec
490 * @from_nid: the NID where the path start from
491 * @to_nid: the NID where the path ends at
492 * @anchor_nid: the anchor indication, see snd_hda_parse_nid_path()
494 * If no valid path is found, returns NULL.
497 snd_hda_add_new_path(struct hda_codec
*codec
, hda_nid_t from_nid
,
498 hda_nid_t to_nid
, int anchor_nid
)
500 struct hda_gen_spec
*spec
= codec
->spec
;
501 struct nid_path
*path
;
503 if (from_nid
&& to_nid
&& !is_reachable_path(codec
, from_nid
, to_nid
))
506 /* check whether the path has been already added */
507 path
= get_nid_path(codec
, from_nid
, to_nid
, anchor_nid
);
511 path
= snd_array_new(&spec
->paths
);
514 memset(path
, 0, sizeof(*path
));
515 if (snd_hda_parse_nid_path(codec
, from_nid
, to_nid
, anchor_nid
, path
))
521 EXPORT_SYMBOL_GPL(snd_hda_add_new_path
);
523 /* clear the given path as invalid so that it won't be picked up later */
524 static void invalidate_nid_path(struct hda_codec
*codec
, int idx
)
526 struct nid_path
*path
= snd_hda_get_path_from_idx(codec
, idx
);
529 memset(path
, 0, sizeof(*path
));
532 /* return a DAC if paired to the given pin by codec driver */
533 static hda_nid_t
get_preferred_dac(struct hda_codec
*codec
, hda_nid_t pin
)
535 struct hda_gen_spec
*spec
= codec
->spec
;
536 const hda_nid_t
*list
= spec
->preferred_dacs
;
540 for (; *list
; list
+= 2)
546 /* look for an empty DAC slot */
547 static hda_nid_t
look_for_dac(struct hda_codec
*codec
, hda_nid_t pin
,
550 struct hda_gen_spec
*spec
= codec
->spec
;
554 for (i
= 0; i
< spec
->num_all_dacs
; i
++) {
555 hda_nid_t nid
= spec
->all_dacs
[i
];
556 if (!nid
|| is_dac_already_used(codec
, nid
))
558 cap_digital
= !!(get_wcaps(codec
, nid
) & AC_WCAP_DIGITAL
);
559 if (is_digital
!= cap_digital
)
561 if (is_reachable_path(codec
, nid
, pin
))
567 /* replace the channels in the composed amp value with the given number */
568 static unsigned int amp_val_replace_channels(unsigned int val
, unsigned int chs
)
570 val
&= ~(0x3U
<< 16);
575 static bool same_amp_caps(struct hda_codec
*codec
, hda_nid_t nid1
,
576 hda_nid_t nid2
, int dir
)
578 if (!(get_wcaps(codec
, nid1
) & (1 << (dir
+ 1))))
579 return !(get_wcaps(codec
, nid2
) & (1 << (dir
+ 1)));
580 return (query_amp_caps(codec
, nid1
, dir
) ==
581 query_amp_caps(codec
, nid2
, dir
));
584 /* look for a widget suitable for assigning a mute switch in the path */
585 static hda_nid_t
look_for_out_mute_nid(struct hda_codec
*codec
,
586 struct nid_path
*path
)
590 for (i
= path
->depth
- 1; i
>= 0; i
--) {
591 if (nid_has_mute(codec
, path
->path
[i
], HDA_OUTPUT
))
592 return path
->path
[i
];
593 if (i
!= path
->depth
- 1 && i
!= 0 &&
594 nid_has_mute(codec
, path
->path
[i
], HDA_INPUT
))
595 return path
->path
[i
];
600 /* look for a widget suitable for assigning a volume ctl in the path */
601 static hda_nid_t
look_for_out_vol_nid(struct hda_codec
*codec
,
602 struct nid_path
*path
)
604 struct hda_gen_spec
*spec
= codec
->spec
;
607 for (i
= path
->depth
- 1; i
>= 0; i
--) {
608 hda_nid_t nid
= path
->path
[i
];
609 if ((spec
->out_vol_mask
>> nid
) & 1)
611 if (nid_has_volume(codec
, nid
, HDA_OUTPUT
))
618 * path activation / deactivation
621 /* can have the amp-in capability? */
622 static bool has_amp_in(struct hda_codec
*codec
, struct nid_path
*path
, int idx
)
624 hda_nid_t nid
= path
->path
[idx
];
625 unsigned int caps
= get_wcaps(codec
, nid
);
626 unsigned int type
= get_wcaps_type(caps
);
628 if (!(caps
& AC_WCAP_IN_AMP
))
630 if (type
== AC_WID_PIN
&& idx
> 0) /* only for input pins */
635 /* can have the amp-out capability? */
636 static bool has_amp_out(struct hda_codec
*codec
, struct nid_path
*path
, int idx
)
638 hda_nid_t nid
= path
->path
[idx
];
639 unsigned int caps
= get_wcaps(codec
, nid
);
640 unsigned int type
= get_wcaps_type(caps
);
642 if (!(caps
& AC_WCAP_OUT_AMP
))
644 if (type
== AC_WID_PIN
&& !idx
) /* only for output pins */
649 /* check whether the given (nid,dir,idx) is active */
650 static bool is_active_nid(struct hda_codec
*codec
, hda_nid_t nid
,
651 unsigned int dir
, unsigned int idx
)
653 struct hda_gen_spec
*spec
= codec
->spec
;
654 int type
= get_wcaps_type(get_wcaps(codec
, nid
));
657 if (nid
== codec
->core
.afg
)
660 for (n
= 0; n
< spec
->paths
.used
; n
++) {
661 struct nid_path
*path
= snd_array_elem(&spec
->paths
, n
);
664 if (codec
->power_save_node
) {
665 if (!path
->stream_enabled
)
667 /* ignore unplugged paths except for DAC/ADC */
668 if (!(path
->pin_enabled
|| path
->pin_fixed
) &&
669 type
!= AC_WID_AUD_OUT
&& type
!= AC_WID_AUD_IN
)
672 for (i
= 0; i
< path
->depth
; i
++) {
673 if (path
->path
[i
] == nid
) {
674 if (dir
== HDA_OUTPUT
|| path
->idx
[i
] == idx
)
683 /* check whether the NID is referred by any active paths */
684 #define is_active_nid_for_any(codec, nid) \
685 is_active_nid(codec, nid, HDA_OUTPUT, 0)
687 /* get the default amp value for the target state */
688 static int get_amp_val_to_activate(struct hda_codec
*codec
, hda_nid_t nid
,
689 int dir
, unsigned int caps
, bool enable
)
691 unsigned int val
= 0;
693 if (caps
& AC_AMPCAP_NUM_STEPS
) {
696 val
= (caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
;
698 if (caps
& (AC_AMPCAP_MUTE
| AC_AMPCAP_MIN_MUTE
)) {
705 /* is this a stereo widget or a stereo-to-mono mix? */
706 static bool is_stereo_amps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
)
708 unsigned int wcaps
= get_wcaps(codec
, nid
);
711 if (wcaps
& AC_WCAP_STEREO
)
713 if (dir
!= HDA_INPUT
|| get_wcaps_type(wcaps
) != AC_WID_AUD_MIX
)
715 if (snd_hda_get_num_conns(codec
, nid
) != 1)
717 if (snd_hda_get_connections(codec
, nid
, &conn
, 1) < 0)
719 return !!(get_wcaps(codec
, conn
) & AC_WCAP_STEREO
);
722 /* initialize the amp value (only at the first time) */
723 static void init_amp(struct hda_codec
*codec
, hda_nid_t nid
, int dir
, int idx
)
725 unsigned int caps
= query_amp_caps(codec
, nid
, dir
);
726 int val
= get_amp_val_to_activate(codec
, nid
, dir
, caps
, false);
728 if (is_stereo_amps(codec
, nid
, dir
))
729 snd_hda_codec_amp_init_stereo(codec
, nid
, dir
, idx
, 0xff, val
);
731 snd_hda_codec_amp_init(codec
, nid
, 0, dir
, idx
, 0xff, val
);
734 /* update the amp, doing in stereo or mono depending on NID */
735 static int update_amp(struct hda_codec
*codec
, hda_nid_t nid
, int dir
, int idx
,
736 unsigned int mask
, unsigned int val
)
738 if (is_stereo_amps(codec
, nid
, dir
))
739 return snd_hda_codec_amp_stereo(codec
, nid
, dir
, idx
,
742 return snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
746 /* calculate amp value mask we can modify;
747 * if the given amp is controlled by mixers, don't touch it
749 static unsigned int get_amp_mask_to_modify(struct hda_codec
*codec
,
750 hda_nid_t nid
, int dir
, int idx
,
753 unsigned int mask
= 0xff;
755 if (caps
& (AC_AMPCAP_MUTE
| AC_AMPCAP_MIN_MUTE
)) {
756 if (is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_MUTE_CTL
))
759 if (caps
& AC_AMPCAP_NUM_STEPS
) {
760 if (is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_VOL_CTL
) ||
761 is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_BOOST_CTL
))
767 static void activate_amp(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
768 int idx
, int idx_to_check
, bool enable
)
771 unsigned int mask
, val
;
773 if (!enable
&& is_active_nid(codec
, nid
, dir
, idx_to_check
))
776 caps
= query_amp_caps(codec
, nid
, dir
);
777 val
= get_amp_val_to_activate(codec
, nid
, dir
, caps
, enable
);
778 mask
= get_amp_mask_to_modify(codec
, nid
, dir
, idx_to_check
, caps
);
783 update_amp(codec
, nid
, dir
, idx
, mask
, val
);
786 static void activate_amp_out(struct hda_codec
*codec
, struct nid_path
*path
,
789 hda_nid_t nid
= path
->path
[i
];
790 init_amp(codec
, nid
, HDA_OUTPUT
, 0);
791 activate_amp(codec
, nid
, HDA_OUTPUT
, 0, 0, enable
);
794 static void activate_amp_in(struct hda_codec
*codec
, struct nid_path
*path
,
795 int i
, bool enable
, bool add_aamix
)
797 struct hda_gen_spec
*spec
= codec
->spec
;
798 const hda_nid_t
*conn
;
801 hda_nid_t nid
= path
->path
[i
];
803 nums
= snd_hda_get_conn_list(codec
, nid
, &conn
);
804 type
= get_wcaps_type(get_wcaps(codec
, nid
));
805 if (type
== AC_WID_PIN
||
806 (type
== AC_WID_AUD_IN
&& codec
->single_adc_amp
)) {
812 for (n
= 0; n
< nums
; n
++)
813 init_amp(codec
, nid
, HDA_INPUT
, n
);
815 /* here is a little bit tricky in comparison with activate_amp_out();
816 * when aa-mixer is available, we need to enable the path as well
818 for (n
= 0; n
< nums
; n
++) {
819 if (n
!= idx
&& (!add_aamix
|| conn
[n
] != spec
->mixer_merge_nid
))
821 activate_amp(codec
, nid
, HDA_INPUT
, n
, idx
, enable
);
825 /* sync power of each widget in the the given path */
826 static hda_nid_t
path_power_update(struct hda_codec
*codec
,
827 struct nid_path
*path
,
828 bool allow_powerdown
)
830 hda_nid_t nid
, changed
= 0;
833 for (i
= 0; i
< path
->depth
; i
++) {
835 if (!(get_wcaps(codec
, nid
) & AC_WCAP_POWER
))
837 if (nid
== codec
->core
.afg
)
839 if (!allow_powerdown
|| is_active_nid_for_any(codec
, nid
))
843 if (!snd_hda_check_power_state(codec
, nid
, state
)) {
844 snd_hda_codec_write(codec
, nid
, 0,
845 AC_VERB_SET_POWER_STATE
, state
);
847 /* all known codecs seem to be capable to handl
848 * widgets state even in D3, so far.
849 * if any new codecs need to restore the widget
850 * states after D0 transition, call the function
854 if (state
== AC_PWRST_D0
)
855 snd_hdac_regmap_sync_node(&codec
->core
, nid
);
862 /* do sync with the last power state change */
863 static void sync_power_state_change(struct hda_codec
*codec
, hda_nid_t nid
)
867 snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_POWER_STATE
, 0);
872 * snd_hda_activate_path - activate or deactivate the given path
873 * @codec: the HDA codec
874 * @path: the path to activate/deactivate
875 * @enable: flag to activate or not
876 * @add_aamix: enable the input from aamix NID
878 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
880 void snd_hda_activate_path(struct hda_codec
*codec
, struct nid_path
*path
,
881 bool enable
, bool add_aamix
)
883 struct hda_gen_spec
*spec
= codec
->spec
;
887 path
->active
= false;
889 /* make sure the widget is powered up */
890 if (enable
&& (spec
->power_down_unused
|| codec
->power_save_node
))
891 path_power_update(codec
, path
, codec
->power_save_node
);
893 for (i
= path
->depth
- 1; i
>= 0; i
--) {
894 hda_nid_t nid
= path
->path
[i
];
896 if (enable
&& path
->multi
[i
])
897 snd_hda_codec_update_cache(codec
, nid
, 0,
898 AC_VERB_SET_CONNECT_SEL
,
900 if (has_amp_in(codec
, path
, i
))
901 activate_amp_in(codec
, path
, i
, enable
, add_aamix
);
902 if (has_amp_out(codec
, path
, i
))
903 activate_amp_out(codec
, path
, i
, enable
);
909 EXPORT_SYMBOL_GPL(snd_hda_activate_path
);
911 /* if the given path is inactive, put widgets into D3 (only if suitable) */
912 static void path_power_down_sync(struct hda_codec
*codec
, struct nid_path
*path
)
914 struct hda_gen_spec
*spec
= codec
->spec
;
916 if (!(spec
->power_down_unused
|| codec
->power_save_node
) || path
->active
)
918 sync_power_state_change(codec
, path_power_update(codec
, path
, true));
921 /* turn on/off EAPD on the given pin */
922 static void set_pin_eapd(struct hda_codec
*codec
, hda_nid_t pin
, bool enable
)
924 struct hda_gen_spec
*spec
= codec
->spec
;
925 if (spec
->own_eapd_ctl
||
926 !(snd_hda_query_pin_caps(codec
, pin
) & AC_PINCAP_EAPD
))
928 if (spec
->keep_eapd_on
&& !enable
)
932 snd_hda_codec_update_cache(codec
, pin
, 0,
933 AC_VERB_SET_EAPD_BTLENABLE
,
934 enable
? 0x02 : 0x00);
937 /* re-initialize the path specified by the given path index */
938 static void resume_path_from_idx(struct hda_codec
*codec
, int path_idx
)
940 struct nid_path
*path
= snd_hda_get_path_from_idx(codec
, path_idx
);
942 snd_hda_activate_path(codec
, path
, path
->active
, false);
947 * Helper functions for creating mixer ctl elements
950 static int hda_gen_mixer_mute_put(struct snd_kcontrol
*kcontrol
,
951 struct snd_ctl_elem_value
*ucontrol
);
952 static int hda_gen_bind_mute_put(struct snd_kcontrol
*kcontrol
,
953 struct snd_ctl_elem_value
*ucontrol
);
960 static const struct snd_kcontrol_new control_templates
[] = {
961 HDA_CODEC_VOLUME(NULL
, 0, 0, 0),
962 /* only the put callback is replaced for handling the special mute */
964 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
965 .subdevice
= HDA_SUBDEV_AMP_FLAG
,
966 .info
= snd_hda_mixer_amp_switch_info
,
967 .get
= snd_hda_mixer_amp_switch_get
,
968 .put
= hda_gen_mixer_mute_put
, /* replaced */
969 .private_value
= HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
972 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
973 .info
= snd_hda_mixer_amp_switch_info
,
974 .get
= snd_hda_mixer_bind_switch_get
,
975 .put
= hda_gen_bind_mute_put
, /* replaced */
976 .private_value
= HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
980 /* add dynamic controls from template */
981 static struct snd_kcontrol_new
*
982 add_control(struct hda_gen_spec
*spec
, int type
, const char *name
,
983 int cidx
, unsigned long val
)
985 struct snd_kcontrol_new
*knew
;
987 knew
= snd_hda_gen_add_kctl(spec
, name
, &control_templates
[type
]);
991 if (get_amp_nid_(val
))
992 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
993 knew
->private_value
= val
;
997 static int add_control_with_pfx(struct hda_gen_spec
*spec
, int type
,
998 const char *pfx
, const char *dir
,
999 const char *sfx
, int cidx
, unsigned long val
)
1001 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
1002 snprintf(name
, sizeof(name
), "%s %s %s", pfx
, dir
, sfx
);
1003 if (!add_control(spec
, type
, name
, cidx
, val
))
1008 #define add_pb_vol_ctrl(spec, type, pfx, val) \
1009 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1010 #define add_pb_sw_ctrl(spec, type, pfx, val) \
1011 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1012 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1013 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1014 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1015 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1017 static int add_vol_ctl(struct hda_codec
*codec
, const char *pfx
, int cidx
,
1018 unsigned int chs
, struct nid_path
*path
)
1023 val
= path
->ctls
[NID_PATH_VOL_CTL
];
1026 val
= amp_val_replace_channels(val
, chs
);
1027 return __add_pb_vol_ctrl(codec
->spec
, HDA_CTL_WIDGET_VOL
, pfx
, cidx
, val
);
1030 /* return the channel bits suitable for the given path->ctls[] */
1031 static int get_default_ch_nums(struct hda_codec
*codec
, struct nid_path
*path
,
1034 int chs
= 1; /* mono (left only) */
1036 hda_nid_t nid
= get_amp_nid_(path
->ctls
[type
]);
1037 if (nid
&& (get_wcaps(codec
, nid
) & AC_WCAP_STEREO
))
1038 chs
= 3; /* stereo */
1043 static int add_stereo_vol(struct hda_codec
*codec
, const char *pfx
, int cidx
,
1044 struct nid_path
*path
)
1046 int chs
= get_default_ch_nums(codec
, path
, NID_PATH_VOL_CTL
);
1047 return add_vol_ctl(codec
, pfx
, cidx
, chs
, path
);
1050 /* create a mute-switch for the given mixer widget;
1051 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1053 static int add_sw_ctl(struct hda_codec
*codec
, const char *pfx
, int cidx
,
1054 unsigned int chs
, struct nid_path
*path
)
1057 int type
= HDA_CTL_WIDGET_MUTE
;
1061 val
= path
->ctls
[NID_PATH_MUTE_CTL
];
1064 val
= amp_val_replace_channels(val
, chs
);
1065 if (get_amp_direction_(val
) == HDA_INPUT
) {
1066 hda_nid_t nid
= get_amp_nid_(val
);
1067 int nums
= snd_hda_get_num_conns(codec
, nid
);
1069 type
= HDA_CTL_BIND_MUTE
;
1073 return __add_pb_sw_ctrl(codec
->spec
, type
, pfx
, cidx
, val
);
1076 static int add_stereo_sw(struct hda_codec
*codec
, const char *pfx
,
1077 int cidx
, struct nid_path
*path
)
1079 int chs
= get_default_ch_nums(codec
, path
, NID_PATH_MUTE_CTL
);
1080 return add_sw_ctl(codec
, pfx
, cidx
, chs
, path
);
1083 /* playback mute control with the software mute bit check */
1084 static void sync_auto_mute_bits(struct snd_kcontrol
*kcontrol
,
1085 struct snd_ctl_elem_value
*ucontrol
)
1087 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1088 struct hda_gen_spec
*spec
= codec
->spec
;
1090 if (spec
->auto_mute_via_amp
) {
1091 hda_nid_t nid
= get_amp_nid(kcontrol
);
1092 bool enabled
= !((spec
->mute_bits
>> nid
) & 1);
1093 ucontrol
->value
.integer
.value
[0] &= enabled
;
1094 ucontrol
->value
.integer
.value
[1] &= enabled
;
1098 static int hda_gen_mixer_mute_put(struct snd_kcontrol
*kcontrol
,
1099 struct snd_ctl_elem_value
*ucontrol
)
1101 sync_auto_mute_bits(kcontrol
, ucontrol
);
1102 return snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
1105 static int hda_gen_bind_mute_put(struct snd_kcontrol
*kcontrol
,
1106 struct snd_ctl_elem_value
*ucontrol
)
1108 sync_auto_mute_bits(kcontrol
, ucontrol
);
1109 return snd_hda_mixer_bind_switch_put(kcontrol
, ucontrol
);
1112 /* any ctl assigned to the path with the given index? */
1113 static bool path_has_mixer(struct hda_codec
*codec
, int path_idx
, int ctl_type
)
1115 struct nid_path
*path
= snd_hda_get_path_from_idx(codec
, path_idx
);
1116 return path
&& path
->ctls
[ctl_type
];
1119 static const char * const channel_name
[4] = {
1120 "Front", "Surround", "CLFE", "Side"
1123 /* give some appropriate ctl name prefix for the given line out channel */
1124 static const char *get_line_out_pfx(struct hda_codec
*codec
, int ch
,
1125 int *index
, int ctl_type
)
1127 struct hda_gen_spec
*spec
= codec
->spec
;
1128 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1131 if (cfg
->line_outs
== 1 && !spec
->multi_ios
&&
1132 !cfg
->hp_outs
&& !cfg
->speaker_outs
)
1133 return spec
->vmaster_mute
.hook
? "PCM" : "Master";
1135 /* if there is really a single DAC used in the whole output paths,
1136 * use it master (or "PCM" if a vmaster hook is present)
1138 if (spec
->multiout
.num_dacs
== 1 && !spec
->mixer_nid
&&
1139 !spec
->multiout
.hp_out_nid
[0] && !spec
->multiout
.extra_out_nid
[0])
1140 return spec
->vmaster_mute
.hook
? "PCM" : "Master";
1142 /* multi-io channels */
1143 if (ch
>= cfg
->line_outs
)
1144 return channel_name
[ch
];
1146 switch (cfg
->line_out_type
) {
1147 case AUTO_PIN_SPEAKER_OUT
:
1148 /* if the primary channel vol/mute is shared with HP volume,
1149 * don't name it as Speaker
1151 if (!ch
&& cfg
->hp_outs
&&
1152 !path_has_mixer(codec
, spec
->hp_paths
[0], ctl_type
))
1154 if (cfg
->line_outs
== 1)
1156 if (cfg
->line_outs
== 2)
1157 return ch
? "Bass Speaker" : "Speaker";
1159 case AUTO_PIN_HP_OUT
:
1160 /* if the primary channel vol/mute is shared with spk volume,
1161 * don't name it as Headphone
1163 if (!ch
&& cfg
->speaker_outs
&&
1164 !path_has_mixer(codec
, spec
->speaker_paths
[0], ctl_type
))
1166 /* for multi-io case, only the primary out */
1167 if (ch
&& spec
->multi_ios
)
1171 case AUTO_PIN_LINE_OUT
:
1172 /* This deals with the case where we have two DACs and
1173 * one LO, one HP and one Speaker */
1174 if (!ch
&& cfg
->speaker_outs
&& cfg
->hp_outs
) {
1175 bool hp_lo_shared
= !path_has_mixer(codec
, spec
->hp_paths
[0], ctl_type
);
1176 bool spk_lo_shared
= !path_has_mixer(codec
, spec
->speaker_paths
[0], ctl_type
);
1177 if (hp_lo_shared
&& spk_lo_shared
)
1178 return spec
->vmaster_mute
.hook
? "PCM" : "Master";
1180 return "Headphone+LO";
1182 return "Speaker+LO";
1186 /* for a single channel output, we don't have to name the channel */
1187 if (cfg
->line_outs
== 1 && !spec
->multi_ios
)
1190 if (ch
>= ARRAY_SIZE(channel_name
)) {
1195 return channel_name
[ch
];
1199 * Parse output paths
1202 /* badness definition */
1204 /* No primary DAC is found for the main output */
1205 BAD_NO_PRIMARY_DAC
= 0x10000,
1206 /* No DAC is found for the extra output */
1207 BAD_NO_DAC
= 0x4000,
1208 /* No possible multi-ios */
1209 BAD_MULTI_IO
= 0x120,
1210 /* No individual DAC for extra output */
1211 BAD_NO_EXTRA_DAC
= 0x102,
1212 /* No individual DAC for extra surrounds */
1213 BAD_NO_EXTRA_SURR_DAC
= 0x101,
1214 /* Primary DAC shared with main surrounds */
1215 BAD_SHARED_SURROUND
= 0x100,
1216 /* No independent HP possible */
1217 BAD_NO_INDEP_HP
= 0x10,
1218 /* Primary DAC shared with main CLFE */
1219 BAD_SHARED_CLFE
= 0x10,
1220 /* Primary DAC shared with extra surrounds */
1221 BAD_SHARED_EXTRA_SURROUND
= 0x10,
1222 /* Volume widget is shared */
1223 BAD_SHARED_VOL
= 0x10,
1226 /* look for widgets in the given path which are appropriate for
1227 * volume and mute controls, and assign the values to ctls[].
1229 * When no appropriate widget is found in the path, the badness value
1230 * is incremented depending on the situation. The function returns the
1231 * total badness for both volume and mute controls.
1233 static int assign_out_path_ctls(struct hda_codec
*codec
, struct nid_path
*path
)
1235 struct hda_gen_spec
*spec
= codec
->spec
;
1241 return BAD_SHARED_VOL
* 2;
1243 if (path
->ctls
[NID_PATH_VOL_CTL
] ||
1244 path
->ctls
[NID_PATH_MUTE_CTL
])
1245 return 0; /* already evaluated */
1247 nid
= look_for_out_vol_nid(codec
, path
);
1249 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
1250 if (spec
->dac_min_mute
)
1251 val
|= HDA_AMP_VAL_MIN_MUTE
;
1252 if (is_ctl_used(codec
, val
, NID_PATH_VOL_CTL
))
1253 badness
+= BAD_SHARED_VOL
;
1255 path
->ctls
[NID_PATH_VOL_CTL
] = val
;
1257 badness
+= BAD_SHARED_VOL
;
1258 nid
= look_for_out_mute_nid(codec
, path
);
1260 unsigned int wid_type
= get_wcaps_type(get_wcaps(codec
, nid
));
1261 if (wid_type
== AC_WID_PIN
|| wid_type
== AC_WID_AUD_OUT
||
1262 nid_has_mute(codec
, nid
, HDA_OUTPUT
))
1263 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
1265 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_INPUT
);
1266 if (is_ctl_used(codec
, val
, NID_PATH_MUTE_CTL
))
1267 badness
+= BAD_SHARED_VOL
;
1269 path
->ctls
[NID_PATH_MUTE_CTL
] = val
;
1271 badness
+= BAD_SHARED_VOL
;
1275 const struct badness_table hda_main_out_badness
= {
1276 .no_primary_dac
= BAD_NO_PRIMARY_DAC
,
1277 .no_dac
= BAD_NO_DAC
,
1278 .shared_primary
= BAD_NO_PRIMARY_DAC
,
1279 .shared_surr
= BAD_SHARED_SURROUND
,
1280 .shared_clfe
= BAD_SHARED_CLFE
,
1281 .shared_surr_main
= BAD_SHARED_SURROUND
,
1283 EXPORT_SYMBOL_GPL(hda_main_out_badness
);
1285 const struct badness_table hda_extra_out_badness
= {
1286 .no_primary_dac
= BAD_NO_DAC
,
1287 .no_dac
= BAD_NO_DAC
,
1288 .shared_primary
= BAD_NO_EXTRA_DAC
,
1289 .shared_surr
= BAD_SHARED_EXTRA_SURROUND
,
1290 .shared_clfe
= BAD_SHARED_EXTRA_SURROUND
,
1291 .shared_surr_main
= BAD_NO_EXTRA_SURR_DAC
,
1293 EXPORT_SYMBOL_GPL(hda_extra_out_badness
);
1295 /* get the DAC of the primary output corresponding to the given array index */
1296 static hda_nid_t
get_primary_out(struct hda_codec
*codec
, int idx
)
1298 struct hda_gen_spec
*spec
= codec
->spec
;
1299 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1301 if (cfg
->line_outs
> idx
)
1302 return spec
->private_dac_nids
[idx
];
1303 idx
-= cfg
->line_outs
;
1304 if (spec
->multi_ios
> idx
)
1305 return spec
->multi_io
[idx
].dac
;
1309 /* return the DAC if it's reachable, otherwise zero */
1310 static inline hda_nid_t
try_dac(struct hda_codec
*codec
,
1311 hda_nid_t dac
, hda_nid_t pin
)
1313 return is_reachable_path(codec
, dac
, pin
) ? dac
: 0;
1316 /* try to assign DACs to pins and return the resultant badness */
1317 static int try_assign_dacs(struct hda_codec
*codec
, int num_outs
,
1318 const hda_nid_t
*pins
, hda_nid_t
*dacs
,
1320 const struct badness_table
*bad
)
1322 struct hda_gen_spec
*spec
= codec
->spec
;
1330 for (i
= 0; i
< num_outs
; i
++) {
1331 struct nid_path
*path
;
1332 hda_nid_t pin
= pins
[i
];
1334 path
= snd_hda_get_path_from_idx(codec
, path_idx
[i
]);
1336 badness
+= assign_out_path_ctls(codec
, path
);
1340 dacs
[i
] = get_preferred_dac(codec
, pin
);
1342 if (is_dac_already_used(codec
, dacs
[i
]))
1343 badness
+= bad
->shared_primary
;
1347 dacs
[i
] = look_for_dac(codec
, pin
, false);
1348 if (!dacs
[i
] && !i
) {
1349 /* try to steal the DAC of surrounds for the front */
1350 for (j
= 1; j
< num_outs
; j
++) {
1351 if (is_reachable_path(codec
, dacs
[j
], pin
)) {
1354 invalidate_nid_path(codec
, path_idx
[j
]);
1363 dac
= try_dac(codec
, get_primary_out(codec
, i
), pin
);
1365 dac
= try_dac(codec
, dacs
[0], pin
);
1367 dac
= try_dac(codec
, get_primary_out(codec
, i
), pin
);
1370 badness
+= bad
->shared_primary
;
1372 badness
+= bad
->shared_surr
;
1374 badness
+= bad
->shared_clfe
;
1375 } else if (is_reachable_path(codec
, spec
->private_dac_nids
[0], pin
)) {
1376 dac
= spec
->private_dac_nids
[0];
1377 badness
+= bad
->shared_surr_main
;
1379 badness
+= bad
->no_primary_dac
;
1381 badness
+= bad
->no_dac
;
1385 path
= snd_hda_add_new_path(codec
, dac
, pin
, -spec
->mixer_nid
);
1386 if (!path
&& !i
&& spec
->mixer_nid
) {
1387 /* try with aamix */
1388 path
= snd_hda_add_new_path(codec
, dac
, pin
, 0);
1392 badness
+= bad
->no_dac
;
1394 /* print_nid_path(codec, "output", path); */
1395 path
->active
= true;
1396 path_idx
[i
] = snd_hda_get_path_idx(codec
, path
);
1397 badness
+= assign_out_path_ctls(codec
, path
);
1404 /* return NID if the given pin has only a single connection to a certain DAC */
1405 static hda_nid_t
get_dac_if_single(struct hda_codec
*codec
, hda_nid_t pin
)
1407 struct hda_gen_spec
*spec
= codec
->spec
;
1409 hda_nid_t nid_found
= 0;
1411 for (i
= 0; i
< spec
->num_all_dacs
; i
++) {
1412 hda_nid_t nid
= spec
->all_dacs
[i
];
1413 if (!nid
|| is_dac_already_used(codec
, nid
))
1415 if (is_reachable_path(codec
, nid
, pin
)) {
1424 /* check whether the given pin can be a multi-io pin */
1425 static bool can_be_multiio_pin(struct hda_codec
*codec
,
1426 unsigned int location
, hda_nid_t nid
)
1428 unsigned int defcfg
, caps
;
1430 defcfg
= snd_hda_codec_get_pincfg(codec
, nid
);
1431 if (get_defcfg_connect(defcfg
) != AC_JACK_PORT_COMPLEX
)
1433 if (location
&& get_defcfg_location(defcfg
) != location
)
1435 caps
= snd_hda_query_pin_caps(codec
, nid
);
1436 if (!(caps
& AC_PINCAP_OUT
))
1441 /* count the number of input pins that are capable to be multi-io */
1442 static int count_multiio_pins(struct hda_codec
*codec
, hda_nid_t reference_pin
)
1444 struct hda_gen_spec
*spec
= codec
->spec
;
1445 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1446 unsigned int defcfg
= snd_hda_codec_get_pincfg(codec
, reference_pin
);
1447 unsigned int location
= get_defcfg_location(defcfg
);
1451 for (type
= AUTO_PIN_LINE_IN
; type
>= AUTO_PIN_MIC
; type
--) {
1452 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
1453 if (cfg
->inputs
[i
].type
!= type
)
1455 if (can_be_multiio_pin(codec
, location
,
1456 cfg
->inputs
[i
].pin
))
1466 * When hardwired is set, try to fill ony hardwired pins, and returns
1467 * zero if any pins are filled, non-zero if nothing found.
1468 * When hardwired is off, try to fill possible input pins, and returns
1469 * the badness value.
1471 static int fill_multi_ios(struct hda_codec
*codec
,
1472 hda_nid_t reference_pin
,
1475 struct hda_gen_spec
*spec
= codec
->spec
;
1476 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1477 int type
, i
, j
, num_pins
, old_pins
;
1478 unsigned int defcfg
= snd_hda_codec_get_pincfg(codec
, reference_pin
);
1479 unsigned int location
= get_defcfg_location(defcfg
);
1481 struct nid_path
*path
;
1483 old_pins
= spec
->multi_ios
;
1487 num_pins
= count_multiio_pins(codec
, reference_pin
);
1491 for (type
= AUTO_PIN_LINE_IN
; type
>= AUTO_PIN_MIC
; type
--) {
1492 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
1493 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
1496 if (cfg
->inputs
[i
].type
!= type
)
1498 if (!can_be_multiio_pin(codec
, location
, nid
))
1500 for (j
= 0; j
< spec
->multi_ios
; j
++) {
1501 if (nid
== spec
->multi_io
[j
].pin
)
1504 if (j
< spec
->multi_ios
)
1508 dac
= get_dac_if_single(codec
, nid
);
1510 dac
= look_for_dac(codec
, nid
, false);
1515 path
= snd_hda_add_new_path(codec
, dac
, nid
,
1521 /* print_nid_path(codec, "multiio", path); */
1522 spec
->multi_io
[spec
->multi_ios
].pin
= nid
;
1523 spec
->multi_io
[spec
->multi_ios
].dac
= dac
;
1524 spec
->out_paths
[cfg
->line_outs
+ spec
->multi_ios
] =
1525 snd_hda_get_path_idx(codec
, path
);
1527 if (spec
->multi_ios
>= 2)
1533 badness
= BAD_MULTI_IO
;
1534 if (old_pins
== spec
->multi_ios
) {
1536 return 1; /* nothing found */
1538 return badness
; /* no badness if nothing found */
1540 if (!hardwired
&& spec
->multi_ios
< 2) {
1541 /* cancel newly assigned paths */
1542 spec
->paths
.used
-= spec
->multi_ios
- old_pins
;
1543 spec
->multi_ios
= old_pins
;
1547 /* assign volume and mute controls */
1548 for (i
= old_pins
; i
< spec
->multi_ios
; i
++) {
1549 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[cfg
->line_outs
+ i
]);
1550 badness
+= assign_out_path_ctls(codec
, path
);
1556 /* map DACs for all pins in the list if they are single connections */
1557 static bool map_singles(struct hda_codec
*codec
, int outs
,
1558 const hda_nid_t
*pins
, hda_nid_t
*dacs
, int *path_idx
)
1560 struct hda_gen_spec
*spec
= codec
->spec
;
1563 for (i
= 0; i
< outs
; i
++) {
1564 struct nid_path
*path
;
1568 dac
= get_dac_if_single(codec
, pins
[i
]);
1571 path
= snd_hda_add_new_path(codec
, dac
, pins
[i
],
1573 if (!path
&& !i
&& spec
->mixer_nid
)
1574 path
= snd_hda_add_new_path(codec
, dac
, pins
[i
], 0);
1578 /* print_nid_path(codec, "output", path); */
1579 path
->active
= true;
1580 path_idx
[i
] = snd_hda_get_path_idx(codec
, path
);
1586 /* create a new path including aamix if available, and return its index */
1587 static int check_aamix_out_path(struct hda_codec
*codec
, int path_idx
)
1589 struct hda_gen_spec
*spec
= codec
->spec
;
1590 struct nid_path
*path
;
1591 hda_nid_t path_dac
, dac
, pin
;
1593 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
1594 if (!path
|| !path
->depth
||
1595 is_nid_contained(path
, spec
->mixer_nid
))
1597 path_dac
= path
->path
[0];
1598 dac
= spec
->private_dac_nids
[0];
1599 pin
= path
->path
[path
->depth
- 1];
1600 path
= snd_hda_add_new_path(codec
, dac
, pin
, spec
->mixer_nid
);
1602 if (dac
!= path_dac
)
1604 else if (spec
->multiout
.hp_out_nid
[0])
1605 dac
= spec
->multiout
.hp_out_nid
[0];
1606 else if (spec
->multiout
.extra_out_nid
[0])
1607 dac
= spec
->multiout
.extra_out_nid
[0];
1611 path
= snd_hda_add_new_path(codec
, dac
, pin
,
1616 /* print_nid_path(codec, "output-aamix", path); */
1617 path
->active
= false; /* unused as default */
1618 path
->pin_fixed
= true; /* static route */
1619 return snd_hda_get_path_idx(codec
, path
);
1622 /* check whether the independent HP is available with the current config */
1623 static bool indep_hp_possible(struct hda_codec
*codec
)
1625 struct hda_gen_spec
*spec
= codec
->spec
;
1626 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1627 struct nid_path
*path
;
1630 if (cfg
->line_out_type
== AUTO_PIN_HP_OUT
)
1631 idx
= spec
->out_paths
[0];
1633 idx
= spec
->hp_paths
[0];
1634 path
= snd_hda_get_path_from_idx(codec
, idx
);
1638 /* assume no path conflicts unless aamix is involved */
1639 if (!spec
->mixer_nid
|| !is_nid_contained(path
, spec
->mixer_nid
))
1642 /* check whether output paths contain aamix */
1643 for (i
= 0; i
< cfg
->line_outs
; i
++) {
1644 if (spec
->out_paths
[i
] == idx
)
1646 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[i
]);
1647 if (path
&& is_nid_contained(path
, spec
->mixer_nid
))
1650 for (i
= 0; i
< cfg
->speaker_outs
; i
++) {
1651 path
= snd_hda_get_path_from_idx(codec
, spec
->speaker_paths
[i
]);
1652 if (path
&& is_nid_contained(path
, spec
->mixer_nid
))
1659 /* fill the empty entries in the dac array for speaker/hp with the
1660 * shared dac pointed by the paths
1662 static void refill_shared_dacs(struct hda_codec
*codec
, int num_outs
,
1663 hda_nid_t
*dacs
, int *path_idx
)
1665 struct nid_path
*path
;
1668 for (i
= 0; i
< num_outs
; i
++) {
1671 path
= snd_hda_get_path_from_idx(codec
, path_idx
[i
]);
1674 dacs
[i
] = path
->path
[0];
1678 /* fill in the dac_nids table from the parsed pin configuration */
1679 static int fill_and_eval_dacs(struct hda_codec
*codec
,
1680 bool fill_hardwired
,
1681 bool fill_mio_first
)
1683 struct hda_gen_spec
*spec
= codec
->spec
;
1684 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1685 int i
, err
, badness
;
1687 /* set num_dacs once to full for look_for_dac() */
1688 spec
->multiout
.num_dacs
= cfg
->line_outs
;
1689 spec
->multiout
.dac_nids
= spec
->private_dac_nids
;
1690 memset(spec
->private_dac_nids
, 0, sizeof(spec
->private_dac_nids
));
1691 memset(spec
->multiout
.hp_out_nid
, 0, sizeof(spec
->multiout
.hp_out_nid
));
1692 memset(spec
->multiout
.extra_out_nid
, 0, sizeof(spec
->multiout
.extra_out_nid
));
1693 spec
->multi_ios
= 0;
1694 snd_array_free(&spec
->paths
);
1696 /* clear path indices */
1697 memset(spec
->out_paths
, 0, sizeof(spec
->out_paths
));
1698 memset(spec
->hp_paths
, 0, sizeof(spec
->hp_paths
));
1699 memset(spec
->speaker_paths
, 0, sizeof(spec
->speaker_paths
));
1700 memset(spec
->aamix_out_paths
, 0, sizeof(spec
->aamix_out_paths
));
1701 memset(spec
->digout_paths
, 0, sizeof(spec
->digout_paths
));
1702 memset(spec
->input_paths
, 0, sizeof(spec
->input_paths
));
1703 memset(spec
->loopback_paths
, 0, sizeof(spec
->loopback_paths
));
1704 memset(&spec
->digin_path
, 0, sizeof(spec
->digin_path
));
1708 /* fill hard-wired DACs first */
1709 if (fill_hardwired
) {
1712 mapped
= map_singles(codec
, cfg
->line_outs
,
1714 spec
->private_dac_nids
,
1716 mapped
|= map_singles(codec
, cfg
->hp_outs
,
1718 spec
->multiout
.hp_out_nid
,
1720 mapped
|= map_singles(codec
, cfg
->speaker_outs
,
1722 spec
->multiout
.extra_out_nid
,
1723 spec
->speaker_paths
);
1724 if (!spec
->no_multi_io
&&
1725 fill_mio_first
&& cfg
->line_outs
== 1 &&
1726 cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1727 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], true);
1734 badness
+= try_assign_dacs(codec
, cfg
->line_outs
, cfg
->line_out_pins
,
1735 spec
->private_dac_nids
, spec
->out_paths
,
1736 spec
->main_out_badness
);
1738 if (!spec
->no_multi_io
&& fill_mio_first
&&
1739 cfg
->line_outs
== 1 && cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1740 /* try to fill multi-io first */
1741 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], false);
1744 /* we don't count badness at this stage yet */
1747 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
) {
1748 err
= try_assign_dacs(codec
, cfg
->hp_outs
, cfg
->hp_pins
,
1749 spec
->multiout
.hp_out_nid
,
1751 spec
->extra_out_badness
);
1756 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1757 err
= try_assign_dacs(codec
, cfg
->speaker_outs
,
1759 spec
->multiout
.extra_out_nid
,
1760 spec
->speaker_paths
,
1761 spec
->extra_out_badness
);
1766 if (!spec
->no_multi_io
&&
1767 cfg
->line_outs
== 1 && cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1768 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], false);
1774 if (spec
->mixer_nid
) {
1775 spec
->aamix_out_paths
[0] =
1776 check_aamix_out_path(codec
, spec
->out_paths
[0]);
1777 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
1778 spec
->aamix_out_paths
[1] =
1779 check_aamix_out_path(codec
, spec
->hp_paths
[0]);
1780 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
1781 spec
->aamix_out_paths
[2] =
1782 check_aamix_out_path(codec
, spec
->speaker_paths
[0]);
1785 if (!spec
->no_multi_io
&&
1786 cfg
->hp_outs
&& cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
)
1787 if (count_multiio_pins(codec
, cfg
->hp_pins
[0]) >= 2)
1788 spec
->multi_ios
= 1; /* give badness */
1790 /* re-count num_dacs and squash invalid entries */
1791 spec
->multiout
.num_dacs
= 0;
1792 for (i
= 0; i
< cfg
->line_outs
; i
++) {
1793 if (spec
->private_dac_nids
[i
])
1794 spec
->multiout
.num_dacs
++;
1796 memmove(spec
->private_dac_nids
+ i
,
1797 spec
->private_dac_nids
+ i
+ 1,
1798 sizeof(hda_nid_t
) * (cfg
->line_outs
- i
- 1));
1799 spec
->private_dac_nids
[cfg
->line_outs
- 1] = 0;
1803 spec
->ext_channel_count
= spec
->min_channel_count
=
1804 spec
->multiout
.num_dacs
* 2;
1806 if (spec
->multi_ios
== 2) {
1807 for (i
= 0; i
< 2; i
++)
1808 spec
->private_dac_nids
[spec
->multiout
.num_dacs
++] =
1809 spec
->multi_io
[i
].dac
;
1810 } else if (spec
->multi_ios
) {
1811 spec
->multi_ios
= 0;
1812 badness
+= BAD_MULTI_IO
;
1815 if (spec
->indep_hp
&& !indep_hp_possible(codec
))
1816 badness
+= BAD_NO_INDEP_HP
;
1818 /* re-fill the shared DAC for speaker / headphone */
1819 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
1820 refill_shared_dacs(codec
, cfg
->hp_outs
,
1821 spec
->multiout
.hp_out_nid
,
1823 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
1824 refill_shared_dacs(codec
, cfg
->speaker_outs
,
1825 spec
->multiout
.extra_out_nid
,
1826 spec
->speaker_paths
);
1831 #define DEBUG_BADNESS
1833 #ifdef DEBUG_BADNESS
1834 #define debug_badness(fmt, ...) \
1835 codec_dbg(codec, fmt, ##__VA_ARGS__)
1837 #define debug_badness(fmt, ...) \
1838 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1841 #ifdef DEBUG_BADNESS
1842 static inline void print_nid_path_idx(struct hda_codec
*codec
,
1843 const char *pfx
, int idx
)
1845 struct nid_path
*path
;
1847 path
= snd_hda_get_path_from_idx(codec
, idx
);
1849 print_nid_path(codec
, pfx
, path
);
1852 static void debug_show_configs(struct hda_codec
*codec
,
1853 struct auto_pin_cfg
*cfg
)
1855 struct hda_gen_spec
*spec
= codec
->spec
;
1856 static const char * const lo_type
[3] = { "LO", "SP", "HP" };
1859 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1860 cfg
->line_out_pins
[0], cfg
->line_out_pins
[1],
1861 cfg
->line_out_pins
[2], cfg
->line_out_pins
[3],
1862 spec
->multiout
.dac_nids
[0],
1863 spec
->multiout
.dac_nids
[1],
1864 spec
->multiout
.dac_nids
[2],
1865 spec
->multiout
.dac_nids
[3],
1866 lo_type
[cfg
->line_out_type
]);
1867 for (i
= 0; i
< cfg
->line_outs
; i
++)
1868 print_nid_path_idx(codec
, " out", spec
->out_paths
[i
]);
1869 if (spec
->multi_ios
> 0)
1870 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1872 spec
->multi_io
[0].pin
, spec
->multi_io
[1].pin
,
1873 spec
->multi_io
[0].dac
, spec
->multi_io
[1].dac
);
1874 for (i
= 0; i
< spec
->multi_ios
; i
++)
1875 print_nid_path_idx(codec
, " mio",
1876 spec
->out_paths
[cfg
->line_outs
+ i
]);
1878 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1879 cfg
->hp_pins
[0], cfg
->hp_pins
[1],
1880 cfg
->hp_pins
[2], cfg
->hp_pins
[3],
1881 spec
->multiout
.hp_out_nid
[0],
1882 spec
->multiout
.hp_out_nid
[1],
1883 spec
->multiout
.hp_out_nid
[2],
1884 spec
->multiout
.hp_out_nid
[3]);
1885 for (i
= 0; i
< cfg
->hp_outs
; i
++)
1886 print_nid_path_idx(codec
, " hp ", spec
->hp_paths
[i
]);
1887 if (cfg
->speaker_outs
)
1888 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1889 cfg
->speaker_pins
[0], cfg
->speaker_pins
[1],
1890 cfg
->speaker_pins
[2], cfg
->speaker_pins
[3],
1891 spec
->multiout
.extra_out_nid
[0],
1892 spec
->multiout
.extra_out_nid
[1],
1893 spec
->multiout
.extra_out_nid
[2],
1894 spec
->multiout
.extra_out_nid
[3]);
1895 for (i
= 0; i
< cfg
->speaker_outs
; i
++)
1896 print_nid_path_idx(codec
, " spk", spec
->speaker_paths
[i
]);
1897 for (i
= 0; i
< 3; i
++)
1898 print_nid_path_idx(codec
, " mix", spec
->aamix_out_paths
[i
]);
1901 #define debug_show_configs(codec, cfg) /* NOP */
1904 /* find all available DACs of the codec */
1905 static void fill_all_dac_nids(struct hda_codec
*codec
)
1907 struct hda_gen_spec
*spec
= codec
->spec
;
1910 spec
->num_all_dacs
= 0;
1911 memset(spec
->all_dacs
, 0, sizeof(spec
->all_dacs
));
1912 for_each_hda_codec_node(nid
, codec
) {
1913 if (get_wcaps_type(get_wcaps(codec
, nid
)) != AC_WID_AUD_OUT
)
1915 if (spec
->num_all_dacs
>= ARRAY_SIZE(spec
->all_dacs
)) {
1916 codec_err(codec
, "Too many DACs!\n");
1919 spec
->all_dacs
[spec
->num_all_dacs
++] = nid
;
1923 static int parse_output_paths(struct hda_codec
*codec
)
1925 struct hda_gen_spec
*spec
= codec
->spec
;
1926 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1927 struct auto_pin_cfg
*best_cfg
;
1929 int best_badness
= INT_MAX
;
1931 bool fill_hardwired
= true, fill_mio_first
= true;
1932 bool best_wired
= true, best_mio
= true;
1933 bool hp_spk_swapped
= false;
1935 best_cfg
= kmalloc(sizeof(*best_cfg
), GFP_KERNEL
);
1941 badness
= fill_and_eval_dacs(codec
, fill_hardwired
,
1947 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1948 cfg
->line_out_type
, fill_hardwired
, fill_mio_first
,
1950 debug_show_configs(codec
, cfg
);
1951 if (badness
< best_badness
) {
1952 best_badness
= badness
;
1954 best_wired
= fill_hardwired
;
1955 best_mio
= fill_mio_first
;
1959 fill_mio_first
= !fill_mio_first
;
1960 if (!fill_mio_first
)
1962 fill_hardwired
= !fill_hardwired
;
1963 if (!fill_hardwired
)
1967 hp_spk_swapped
= true;
1968 if (cfg
->speaker_outs
> 0 &&
1969 cfg
->line_out_type
== AUTO_PIN_HP_OUT
) {
1970 cfg
->hp_outs
= cfg
->line_outs
;
1971 memcpy(cfg
->hp_pins
, cfg
->line_out_pins
,
1972 sizeof(cfg
->hp_pins
));
1973 cfg
->line_outs
= cfg
->speaker_outs
;
1974 memcpy(cfg
->line_out_pins
, cfg
->speaker_pins
,
1975 sizeof(cfg
->speaker_pins
));
1976 cfg
->speaker_outs
= 0;
1977 memset(cfg
->speaker_pins
, 0, sizeof(cfg
->speaker_pins
));
1978 cfg
->line_out_type
= AUTO_PIN_SPEAKER_OUT
;
1979 fill_hardwired
= true;
1982 if (cfg
->hp_outs
> 0 &&
1983 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
) {
1984 cfg
->speaker_outs
= cfg
->line_outs
;
1985 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
1986 sizeof(cfg
->speaker_pins
));
1987 cfg
->line_outs
= cfg
->hp_outs
;
1988 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
,
1989 sizeof(cfg
->hp_pins
));
1991 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
1992 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
1993 fill_hardwired
= true;
2000 debug_badness("==> restoring best_cfg\n");
2002 fill_and_eval_dacs(codec
, best_wired
, best_mio
);
2004 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2005 cfg
->line_out_type
, best_wired
, best_mio
);
2006 debug_show_configs(codec
, cfg
);
2008 if (cfg
->line_out_pins
[0]) {
2009 struct nid_path
*path
;
2010 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[0]);
2012 spec
->vmaster_nid
= look_for_out_vol_nid(codec
, path
);
2013 if (spec
->vmaster_nid
) {
2014 snd_hda_set_vmaster_tlv(codec
, spec
->vmaster_nid
,
2015 HDA_OUTPUT
, spec
->vmaster_tlv
);
2016 if (spec
->dac_min_mute
)
2017 spec
->vmaster_tlv
[3] |= TLV_DB_SCALE_MUTE
;
2021 /* set initial pinctl targets */
2022 if (spec
->prefer_hp_amp
|| cfg
->line_out_type
== AUTO_PIN_HP_OUT
)
2026 set_pin_targets(codec
, cfg
->line_outs
, cfg
->line_out_pins
, val
);
2027 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
2028 set_pin_targets(codec
, cfg
->hp_outs
, cfg
->hp_pins
, PIN_HP
);
2029 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
2030 val
= spec
->prefer_hp_amp
? PIN_HP
: PIN_OUT
;
2031 set_pin_targets(codec
, cfg
->speaker_outs
,
2032 cfg
->speaker_pins
, val
);
2035 /* clear indep_hp flag if not available */
2036 if (spec
->indep_hp
&& !indep_hp_possible(codec
))
2043 /* add playback controls from the parsed DAC table */
2044 static int create_multi_out_ctls(struct hda_codec
*codec
,
2045 const struct auto_pin_cfg
*cfg
)
2047 struct hda_gen_spec
*spec
= codec
->spec
;
2048 int i
, err
, noutputs
;
2050 noutputs
= cfg
->line_outs
;
2051 if (spec
->multi_ios
> 0 && cfg
->line_outs
< 3)
2052 noutputs
+= spec
->multi_ios
;
2054 for (i
= 0; i
< noutputs
; i
++) {
2057 struct nid_path
*path
;
2059 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[i
]);
2063 name
= get_line_out_pfx(codec
, i
, &index
, NID_PATH_VOL_CTL
);
2064 if (!name
|| !strcmp(name
, "CLFE")) {
2066 err
= add_vol_ctl(codec
, "Center", 0, 1, path
);
2069 err
= add_vol_ctl(codec
, "LFE", 0, 2, path
);
2073 err
= add_stereo_vol(codec
, name
, index
, path
);
2078 name
= get_line_out_pfx(codec
, i
, &index
, NID_PATH_MUTE_CTL
);
2079 if (!name
|| !strcmp(name
, "CLFE")) {
2080 err
= add_sw_ctl(codec
, "Center", 0, 1, path
);
2083 err
= add_sw_ctl(codec
, "LFE", 0, 2, path
);
2087 err
= add_stereo_sw(codec
, name
, index
, path
);
2095 static int create_extra_out(struct hda_codec
*codec
, int path_idx
,
2096 const char *pfx
, int cidx
)
2098 struct nid_path
*path
;
2101 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
2104 err
= add_stereo_vol(codec
, pfx
, cidx
, path
);
2107 err
= add_stereo_sw(codec
, pfx
, cidx
, path
);
2113 /* add playback controls for speaker and HP outputs */
2114 static int create_extra_outs(struct hda_codec
*codec
, int num_pins
,
2115 const int *paths
, const char *pfx
)
2119 for (i
= 0; i
< num_pins
; i
++) {
2121 char tmp
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
2124 if (num_pins
== 2 && i
== 1 && !strcmp(pfx
, "Speaker"))
2125 name
= "Bass Speaker";
2126 else if (num_pins
>= 3) {
2127 snprintf(tmp
, sizeof(tmp
), "%s %s",
2128 pfx
, channel_name
[i
]);
2134 err
= create_extra_out(codec
, paths
[i
], name
, idx
);
2141 static int create_hp_out_ctls(struct hda_codec
*codec
)
2143 struct hda_gen_spec
*spec
= codec
->spec
;
2144 return create_extra_outs(codec
, spec
->autocfg
.hp_outs
,
2149 static int create_speaker_out_ctls(struct hda_codec
*codec
)
2151 struct hda_gen_spec
*spec
= codec
->spec
;
2152 return create_extra_outs(codec
, spec
->autocfg
.speaker_outs
,
2153 spec
->speaker_paths
,
2158 * independent HP controls
2161 static void call_hp_automute(struct hda_codec
*codec
,
2162 struct hda_jack_callback
*jack
);
2163 static int indep_hp_info(struct snd_kcontrol
*kcontrol
,
2164 struct snd_ctl_elem_info
*uinfo
)
2166 return snd_hda_enum_bool_helper_info(kcontrol
, uinfo
);
2169 static int indep_hp_get(struct snd_kcontrol
*kcontrol
,
2170 struct snd_ctl_elem_value
*ucontrol
)
2172 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2173 struct hda_gen_spec
*spec
= codec
->spec
;
2174 ucontrol
->value
.enumerated
.item
[0] = spec
->indep_hp_enabled
;
2178 static void update_aamix_paths(struct hda_codec
*codec
, bool do_mix
,
2179 int nomix_path_idx
, int mix_path_idx
,
2182 static int indep_hp_put(struct snd_kcontrol
*kcontrol
,
2183 struct snd_ctl_elem_value
*ucontrol
)
2185 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2186 struct hda_gen_spec
*spec
= codec
->spec
;
2187 unsigned int select
= ucontrol
->value
.enumerated
.item
[0];
2190 mutex_lock(&spec
->pcm_mutex
);
2191 if (spec
->active_streams
) {
2196 if (spec
->indep_hp_enabled
!= select
) {
2198 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
2199 dacp
= &spec
->private_dac_nids
[0];
2201 dacp
= &spec
->multiout
.hp_out_nid
[0];
2203 /* update HP aamix paths in case it conflicts with indep HP */
2204 if (spec
->have_aamix_ctl
) {
2205 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
2206 update_aamix_paths(codec
, spec
->aamix_mode
,
2208 spec
->aamix_out_paths
[0],
2209 spec
->autocfg
.line_out_type
);
2211 update_aamix_paths(codec
, spec
->aamix_mode
,
2213 spec
->aamix_out_paths
[1],
2217 spec
->indep_hp_enabled
= select
;
2218 if (spec
->indep_hp_enabled
)
2221 *dacp
= spec
->alt_dac_nid
;
2223 call_hp_automute(codec
, NULL
);
2227 mutex_unlock(&spec
->pcm_mutex
);
2231 static const struct snd_kcontrol_new indep_hp_ctl
= {
2232 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2233 .name
= "Independent HP",
2234 .info
= indep_hp_info
,
2235 .get
= indep_hp_get
,
2236 .put
= indep_hp_put
,
2240 static int create_indep_hp_ctls(struct hda_codec
*codec
)
2242 struct hda_gen_spec
*spec
= codec
->spec
;
2245 if (!spec
->indep_hp
)
2247 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
2248 dac
= spec
->multiout
.dac_nids
[0];
2250 dac
= spec
->multiout
.hp_out_nid
[0];
2256 spec
->indep_hp_enabled
= false;
2257 spec
->alt_dac_nid
= dac
;
2258 if (!snd_hda_gen_add_kctl(spec
, NULL
, &indep_hp_ctl
))
2264 * channel mode enum control
2267 static int ch_mode_info(struct snd_kcontrol
*kcontrol
,
2268 struct snd_ctl_elem_info
*uinfo
)
2270 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2271 struct hda_gen_spec
*spec
= codec
->spec
;
2274 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2276 uinfo
->value
.enumerated
.items
= spec
->multi_ios
+ 1;
2277 if (uinfo
->value
.enumerated
.item
> spec
->multi_ios
)
2278 uinfo
->value
.enumerated
.item
= spec
->multi_ios
;
2279 chs
= uinfo
->value
.enumerated
.item
* 2 + spec
->min_channel_count
;
2280 sprintf(uinfo
->value
.enumerated
.name
, "%dch", chs
);
2284 static int ch_mode_get(struct snd_kcontrol
*kcontrol
,
2285 struct snd_ctl_elem_value
*ucontrol
)
2287 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2288 struct hda_gen_spec
*spec
= codec
->spec
;
2289 ucontrol
->value
.enumerated
.item
[0] =
2290 (spec
->ext_channel_count
- spec
->min_channel_count
) / 2;
2294 static inline struct nid_path
*
2295 get_multiio_path(struct hda_codec
*codec
, int idx
)
2297 struct hda_gen_spec
*spec
= codec
->spec
;
2298 return snd_hda_get_path_from_idx(codec
,
2299 spec
->out_paths
[spec
->autocfg
.line_outs
+ idx
]);
2302 static void update_automute_all(struct hda_codec
*codec
);
2304 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2305 * used for output paths
2307 static bool aamix_default(struct hda_gen_spec
*spec
)
2309 return !spec
->have_aamix_ctl
|| spec
->aamix_mode
;
2312 static int set_multi_io(struct hda_codec
*codec
, int idx
, bool output
)
2314 struct hda_gen_spec
*spec
= codec
->spec
;
2315 hda_nid_t nid
= spec
->multi_io
[idx
].pin
;
2316 struct nid_path
*path
;
2318 path
= get_multiio_path(codec
, idx
);
2322 if (path
->active
== output
)
2326 set_pin_target(codec
, nid
, PIN_OUT
, true);
2327 snd_hda_activate_path(codec
, path
, true, aamix_default(spec
));
2328 set_pin_eapd(codec
, nid
, true);
2330 set_pin_eapd(codec
, nid
, false);
2331 snd_hda_activate_path(codec
, path
, false, aamix_default(spec
));
2332 set_pin_target(codec
, nid
, spec
->multi_io
[idx
].ctl_in
, true);
2333 path_power_down_sync(codec
, path
);
2336 /* update jack retasking in case it modifies any of them */
2337 update_automute_all(codec
);
2342 static int ch_mode_put(struct snd_kcontrol
*kcontrol
,
2343 struct snd_ctl_elem_value
*ucontrol
)
2345 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2346 struct hda_gen_spec
*spec
= codec
->spec
;
2349 ch
= ucontrol
->value
.enumerated
.item
[0];
2350 if (ch
< 0 || ch
> spec
->multi_ios
)
2352 if (ch
== (spec
->ext_channel_count
- spec
->min_channel_count
) / 2)
2354 spec
->ext_channel_count
= ch
* 2 + spec
->min_channel_count
;
2355 for (i
= 0; i
< spec
->multi_ios
; i
++)
2356 set_multi_io(codec
, i
, i
< ch
);
2357 spec
->multiout
.max_channels
= max(spec
->ext_channel_count
,
2358 spec
->const_channel_count
);
2359 if (spec
->need_dac_fix
)
2360 spec
->multiout
.num_dacs
= spec
->multiout
.max_channels
/ 2;
2364 static const struct snd_kcontrol_new channel_mode_enum
= {
2365 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2366 .name
= "Channel Mode",
2367 .info
= ch_mode_info
,
2372 static int create_multi_channel_mode(struct hda_codec
*codec
)
2374 struct hda_gen_spec
*spec
= codec
->spec
;
2376 if (spec
->multi_ios
> 0) {
2377 if (!snd_hda_gen_add_kctl(spec
, NULL
, &channel_mode_enum
))
2384 * aamix loopback enable/disable switch
2387 #define loopback_mixing_info indep_hp_info
2389 static int loopback_mixing_get(struct snd_kcontrol
*kcontrol
,
2390 struct snd_ctl_elem_value
*ucontrol
)
2392 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2393 struct hda_gen_spec
*spec
= codec
->spec
;
2394 ucontrol
->value
.enumerated
.item
[0] = spec
->aamix_mode
;
2398 static void update_aamix_paths(struct hda_codec
*codec
, bool do_mix
,
2399 int nomix_path_idx
, int mix_path_idx
,
2402 struct hda_gen_spec
*spec
= codec
->spec
;
2403 struct nid_path
*nomix_path
, *mix_path
;
2405 nomix_path
= snd_hda_get_path_from_idx(codec
, nomix_path_idx
);
2406 mix_path
= snd_hda_get_path_from_idx(codec
, mix_path_idx
);
2407 if (!nomix_path
|| !mix_path
)
2410 /* if HP aamix path is driven from a different DAC and the
2411 * independent HP mode is ON, can't turn on aamix path
2413 if (out_type
== AUTO_PIN_HP_OUT
&& spec
->indep_hp_enabled
&&
2414 mix_path
->path
[0] != spec
->alt_dac_nid
)
2418 snd_hda_activate_path(codec
, nomix_path
, false, true);
2419 snd_hda_activate_path(codec
, mix_path
, true, true);
2420 path_power_down_sync(codec
, nomix_path
);
2422 snd_hda_activate_path(codec
, mix_path
, false, false);
2423 snd_hda_activate_path(codec
, nomix_path
, true, false);
2424 path_power_down_sync(codec
, mix_path
);
2428 static int loopback_mixing_put(struct snd_kcontrol
*kcontrol
,
2429 struct snd_ctl_elem_value
*ucontrol
)
2431 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2432 struct hda_gen_spec
*spec
= codec
->spec
;
2433 unsigned int val
= ucontrol
->value
.enumerated
.item
[0];
2435 if (val
== spec
->aamix_mode
)
2437 spec
->aamix_mode
= val
;
2438 update_aamix_paths(codec
, val
, spec
->out_paths
[0],
2439 spec
->aamix_out_paths
[0],
2440 spec
->autocfg
.line_out_type
);
2441 update_aamix_paths(codec
, val
, spec
->hp_paths
[0],
2442 spec
->aamix_out_paths
[1],
2444 update_aamix_paths(codec
, val
, spec
->speaker_paths
[0],
2445 spec
->aamix_out_paths
[2],
2446 AUTO_PIN_SPEAKER_OUT
);
2450 static const struct snd_kcontrol_new loopback_mixing_enum
= {
2451 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2452 .name
= "Loopback Mixing",
2453 .info
= loopback_mixing_info
,
2454 .get
= loopback_mixing_get
,
2455 .put
= loopback_mixing_put
,
2458 static int create_loopback_mixing_ctl(struct hda_codec
*codec
)
2460 struct hda_gen_spec
*spec
= codec
->spec
;
2462 if (!spec
->mixer_nid
)
2464 if (!(spec
->aamix_out_paths
[0] || spec
->aamix_out_paths
[1] ||
2465 spec
->aamix_out_paths
[2]))
2467 if (!snd_hda_gen_add_kctl(spec
, NULL
, &loopback_mixing_enum
))
2469 spec
->have_aamix_ctl
= 1;
2474 * shared headphone/mic handling
2477 static void call_update_outputs(struct hda_codec
*codec
);
2479 /* for shared I/O, change the pin-control accordingly */
2480 static void update_hp_mic(struct hda_codec
*codec
, int adc_mux
, bool force
)
2482 struct hda_gen_spec
*spec
= codec
->spec
;
2487 pin
= spec
->hp_mic_pin
;
2488 as_mic
= spec
->cur_mux
[adc_mux
] == spec
->hp_mic_mux_idx
;
2491 val
= snd_hda_codec_get_pin_target(codec
, pin
);
2501 val
= snd_hda_get_default_vref(codec
, pin
);
2502 /* if the HP pin doesn't support VREF and the codec driver gives an
2503 * alternative pin, set up the VREF on that pin instead
2505 if (val
== AC_PINCTL_VREF_HIZ
&& spec
->shared_mic_vref_pin
) {
2506 const hda_nid_t vref_pin
= spec
->shared_mic_vref_pin
;
2507 unsigned int vref_val
= snd_hda_get_default_vref(codec
, vref_pin
);
2508 if (vref_val
!= AC_PINCTL_VREF_HIZ
)
2509 snd_hda_set_pin_ctl_cache(codec
, vref_pin
,
2510 PIN_IN
| (as_mic
? vref_val
: 0));
2513 if (!spec
->hp_mic_jack_modes
) {
2518 set_pin_target(codec
, pin
, val
, true);
2519 call_hp_automute(codec
, NULL
);
2523 /* create a shared input with the headphone out */
2524 static int create_hp_mic(struct hda_codec
*codec
)
2526 struct hda_gen_spec
*spec
= codec
->spec
;
2527 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
2528 unsigned int defcfg
;
2531 if (!spec
->hp_mic
) {
2532 if (spec
->suppress_hp_mic_detect
)
2534 /* automatic detection: only if no input or a single internal
2535 * input pin is found, try to detect the shared hp/mic
2537 if (cfg
->num_inputs
> 1)
2539 else if (cfg
->num_inputs
== 1) {
2540 defcfg
= snd_hda_codec_get_pincfg(codec
, cfg
->inputs
[0].pin
);
2541 if (snd_hda_get_input_pin_attr(defcfg
) != INPUT_PIN_ATTR_INT
)
2546 spec
->hp_mic
= 0; /* clear once */
2547 if (cfg
->num_inputs
>= AUTO_CFG_MAX_INS
)
2551 if (cfg
->line_out_type
== AUTO_PIN_HP_OUT
&& cfg
->line_outs
> 0)
2552 nid
= cfg
->line_out_pins
[0];
2553 else if (cfg
->hp_outs
> 0)
2554 nid
= cfg
->hp_pins
[0];
2558 if (!(snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_IN
))
2559 return 0; /* no input */
2561 cfg
->inputs
[cfg
->num_inputs
].pin
= nid
;
2562 cfg
->inputs
[cfg
->num_inputs
].type
= AUTO_PIN_MIC
;
2563 cfg
->inputs
[cfg
->num_inputs
].is_headphone_mic
= 1;
2566 spec
->hp_mic_pin
= nid
;
2567 /* we can't handle auto-mic together with HP-mic */
2568 spec
->suppress_auto_mic
= 1;
2569 codec_dbg(codec
, "Enable shared I/O jack on NID 0x%x\n", nid
);
2577 static int create_hp_mic_jack_mode(struct hda_codec
*codec
, hda_nid_t pin
);
2579 static const char * const out_jack_texts
[] = {
2580 "Line Out", "Headphone Out",
2583 static int out_jack_mode_info(struct snd_kcontrol
*kcontrol
,
2584 struct snd_ctl_elem_info
*uinfo
)
2586 return snd_hda_enum_helper_info(kcontrol
, uinfo
, 2, out_jack_texts
);
2589 static int out_jack_mode_get(struct snd_kcontrol
*kcontrol
,
2590 struct snd_ctl_elem_value
*ucontrol
)
2592 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2593 hda_nid_t nid
= kcontrol
->private_value
;
2594 if (snd_hda_codec_get_pin_target(codec
, nid
) == PIN_HP
)
2595 ucontrol
->value
.enumerated
.item
[0] = 1;
2597 ucontrol
->value
.enumerated
.item
[0] = 0;
2601 static int out_jack_mode_put(struct snd_kcontrol
*kcontrol
,
2602 struct snd_ctl_elem_value
*ucontrol
)
2604 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2605 hda_nid_t nid
= kcontrol
->private_value
;
2608 val
= ucontrol
->value
.enumerated
.item
[0] ? PIN_HP
: PIN_OUT
;
2609 if (snd_hda_codec_get_pin_target(codec
, nid
) == val
)
2611 snd_hda_set_pin_ctl_cache(codec
, nid
, val
);
2615 static const struct snd_kcontrol_new out_jack_mode_enum
= {
2616 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2617 .info
= out_jack_mode_info
,
2618 .get
= out_jack_mode_get
,
2619 .put
= out_jack_mode_put
,
2622 static bool find_kctl_name(struct hda_codec
*codec
, const char *name
, int idx
)
2624 struct hda_gen_spec
*spec
= codec
->spec
;
2627 for (i
= 0; i
< spec
->kctls
.used
; i
++) {
2628 struct snd_kcontrol_new
*kctl
= snd_array_elem(&spec
->kctls
, i
);
2629 if (!strcmp(kctl
->name
, name
) && kctl
->index
== idx
)
2635 static void get_jack_mode_name(struct hda_codec
*codec
, hda_nid_t pin
,
2636 char *name
, size_t name_len
)
2638 struct hda_gen_spec
*spec
= codec
->spec
;
2641 snd_hda_get_pin_label(codec
, pin
, &spec
->autocfg
, name
, name_len
, &idx
);
2642 strlcat(name
, " Jack Mode", name_len
);
2644 for (; find_kctl_name(codec
, name
, idx
); idx
++)
2648 static int get_out_jack_num_items(struct hda_codec
*codec
, hda_nid_t pin
)
2650 struct hda_gen_spec
*spec
= codec
->spec
;
2651 if (spec
->add_jack_modes
) {
2652 unsigned int pincap
= snd_hda_query_pin_caps(codec
, pin
);
2653 if ((pincap
& AC_PINCAP_OUT
) && (pincap
& AC_PINCAP_HP_DRV
))
2659 static int create_out_jack_modes(struct hda_codec
*codec
, int num_pins
,
2662 struct hda_gen_spec
*spec
= codec
->spec
;
2665 for (i
= 0; i
< num_pins
; i
++) {
2666 hda_nid_t pin
= pins
[i
];
2667 if (pin
== spec
->hp_mic_pin
)
2669 if (get_out_jack_num_items(codec
, pin
) > 1) {
2670 struct snd_kcontrol_new
*knew
;
2671 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
2672 get_jack_mode_name(codec
, pin
, name
, sizeof(name
));
2673 knew
= snd_hda_gen_add_kctl(spec
, name
,
2674 &out_jack_mode_enum
);
2677 knew
->private_value
= pin
;
2688 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2691 static const char * const vref_texts
[NUM_VREFS
] = {
2692 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2693 "", "Mic 80pc Bias", "Mic 100pc Bias"
2696 static unsigned int get_vref_caps(struct hda_codec
*codec
, hda_nid_t pin
)
2698 unsigned int pincap
;
2700 pincap
= snd_hda_query_pin_caps(codec
, pin
);
2701 pincap
= (pincap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
2702 /* filter out unusual vrefs */
2703 pincap
&= ~(AC_PINCAP_VREF_GRD
| AC_PINCAP_VREF_100
);
2707 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2708 static int get_vref_idx(unsigned int vref_caps
, unsigned int item_idx
)
2710 unsigned int i
, n
= 0;
2712 for (i
= 0; i
< NUM_VREFS
; i
++) {
2713 if (vref_caps
& (1 << i
)) {
2722 /* convert back from the vref ctl index to the enum item index */
2723 static int cvt_from_vref_idx(unsigned int vref_caps
, unsigned int idx
)
2725 unsigned int i
, n
= 0;
2727 for (i
= 0; i
< NUM_VREFS
; i
++) {
2730 if (vref_caps
& (1 << i
))
2736 static int in_jack_mode_info(struct snd_kcontrol
*kcontrol
,
2737 struct snd_ctl_elem_info
*uinfo
)
2739 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2740 hda_nid_t nid
= kcontrol
->private_value
;
2741 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2743 snd_hda_enum_helper_info(kcontrol
, uinfo
, hweight32(vref_caps
),
2745 /* set the right text */
2746 strcpy(uinfo
->value
.enumerated
.name
,
2747 vref_texts
[get_vref_idx(vref_caps
, uinfo
->value
.enumerated
.item
)]);
2751 static int in_jack_mode_get(struct snd_kcontrol
*kcontrol
,
2752 struct snd_ctl_elem_value
*ucontrol
)
2754 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2755 hda_nid_t nid
= kcontrol
->private_value
;
2756 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2759 idx
= snd_hda_codec_get_pin_target(codec
, nid
) & AC_PINCTL_VREFEN
;
2760 ucontrol
->value
.enumerated
.item
[0] = cvt_from_vref_idx(vref_caps
, idx
);
2764 static int in_jack_mode_put(struct snd_kcontrol
*kcontrol
,
2765 struct snd_ctl_elem_value
*ucontrol
)
2767 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2768 hda_nid_t nid
= kcontrol
->private_value
;
2769 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2770 unsigned int val
, idx
;
2772 val
= snd_hda_codec_get_pin_target(codec
, nid
);
2773 idx
= cvt_from_vref_idx(vref_caps
, val
& AC_PINCTL_VREFEN
);
2774 if (idx
== ucontrol
->value
.enumerated
.item
[0])
2777 val
&= ~AC_PINCTL_VREFEN
;
2778 val
|= get_vref_idx(vref_caps
, ucontrol
->value
.enumerated
.item
[0]);
2779 snd_hda_set_pin_ctl_cache(codec
, nid
, val
);
2783 static const struct snd_kcontrol_new in_jack_mode_enum
= {
2784 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2785 .info
= in_jack_mode_info
,
2786 .get
= in_jack_mode_get
,
2787 .put
= in_jack_mode_put
,
2790 static int get_in_jack_num_items(struct hda_codec
*codec
, hda_nid_t pin
)
2792 struct hda_gen_spec
*spec
= codec
->spec
;
2794 if (spec
->add_jack_modes
)
2795 nitems
= hweight32(get_vref_caps(codec
, pin
));
2796 return nitems
? nitems
: 1;
2799 static int create_in_jack_mode(struct hda_codec
*codec
, hda_nid_t pin
)
2801 struct hda_gen_spec
*spec
= codec
->spec
;
2802 struct snd_kcontrol_new
*knew
;
2803 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
2804 unsigned int defcfg
;
2806 if (pin
== spec
->hp_mic_pin
)
2807 return 0; /* already done in create_out_jack_mode() */
2809 /* no jack mode for fixed pins */
2810 defcfg
= snd_hda_codec_get_pincfg(codec
, pin
);
2811 if (snd_hda_get_input_pin_attr(defcfg
) == INPUT_PIN_ATTR_INT
)
2814 /* no multiple vref caps? */
2815 if (get_in_jack_num_items(codec
, pin
) <= 1)
2818 get_jack_mode_name(codec
, pin
, name
, sizeof(name
));
2819 knew
= snd_hda_gen_add_kctl(spec
, name
, &in_jack_mode_enum
);
2822 knew
->private_value
= pin
;
2827 * HP/mic shared jack mode
2829 static int hp_mic_jack_mode_info(struct snd_kcontrol
*kcontrol
,
2830 struct snd_ctl_elem_info
*uinfo
)
2832 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2833 hda_nid_t nid
= kcontrol
->private_value
;
2834 int out_jacks
= get_out_jack_num_items(codec
, nid
);
2835 int in_jacks
= get_in_jack_num_items(codec
, nid
);
2836 const char *text
= NULL
;
2839 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2841 uinfo
->value
.enumerated
.items
= out_jacks
+ in_jacks
;
2842 if (uinfo
->value
.enumerated
.item
>= uinfo
->value
.enumerated
.items
)
2843 uinfo
->value
.enumerated
.item
= uinfo
->value
.enumerated
.items
- 1;
2844 idx
= uinfo
->value
.enumerated
.item
;
2845 if (idx
< out_jacks
) {
2847 text
= out_jack_texts
[idx
];
2849 text
= "Headphone Out";
2853 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2854 text
= vref_texts
[get_vref_idx(vref_caps
, idx
)];
2859 strcpy(uinfo
->value
.enumerated
.name
, text
);
2863 static int get_cur_hp_mic_jack_mode(struct hda_codec
*codec
, hda_nid_t nid
)
2865 int out_jacks
= get_out_jack_num_items(codec
, nid
);
2866 int in_jacks
= get_in_jack_num_items(codec
, nid
);
2867 unsigned int val
= snd_hda_codec_get_pin_target(codec
, nid
);
2870 if (val
& PIN_OUT
) {
2871 if (out_jacks
> 1 && val
== PIN_HP
)
2873 } else if (val
& PIN_IN
) {
2876 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2877 val
&= AC_PINCTL_VREFEN
;
2878 idx
+= cvt_from_vref_idx(vref_caps
, val
);
2884 static int hp_mic_jack_mode_get(struct snd_kcontrol
*kcontrol
,
2885 struct snd_ctl_elem_value
*ucontrol
)
2887 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2888 hda_nid_t nid
= kcontrol
->private_value
;
2889 ucontrol
->value
.enumerated
.item
[0] =
2890 get_cur_hp_mic_jack_mode(codec
, nid
);
2894 static int hp_mic_jack_mode_put(struct snd_kcontrol
*kcontrol
,
2895 struct snd_ctl_elem_value
*ucontrol
)
2897 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2898 hda_nid_t nid
= kcontrol
->private_value
;
2899 int out_jacks
= get_out_jack_num_items(codec
, nid
);
2900 int in_jacks
= get_in_jack_num_items(codec
, nid
);
2901 unsigned int val
, oldval
, idx
;
2903 oldval
= get_cur_hp_mic_jack_mode(codec
, nid
);
2904 idx
= ucontrol
->value
.enumerated
.item
[0];
2908 if (idx
< out_jacks
) {
2910 val
= idx
? PIN_HP
: PIN_OUT
;
2916 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2917 val
= snd_hda_codec_get_pin_target(codec
, nid
);
2918 val
&= ~(AC_PINCTL_VREFEN
| PIN_HP
);
2919 val
|= get_vref_idx(vref_caps
, idx
) | PIN_IN
;
2921 val
= snd_hda_get_default_vref(codec
, nid
) | PIN_IN
;
2923 snd_hda_set_pin_ctl_cache(codec
, nid
, val
);
2924 call_hp_automute(codec
, NULL
);
2929 static const struct snd_kcontrol_new hp_mic_jack_mode_enum
= {
2930 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2931 .info
= hp_mic_jack_mode_info
,
2932 .get
= hp_mic_jack_mode_get
,
2933 .put
= hp_mic_jack_mode_put
,
2936 static int create_hp_mic_jack_mode(struct hda_codec
*codec
, hda_nid_t pin
)
2938 struct hda_gen_spec
*spec
= codec
->spec
;
2939 struct snd_kcontrol_new
*knew
;
2941 knew
= snd_hda_gen_add_kctl(spec
, "Headphone Mic Jack Mode",
2942 &hp_mic_jack_mode_enum
);
2945 knew
->private_value
= pin
;
2946 spec
->hp_mic_jack_modes
= 1;
2954 /* add the powersave loopback-list entry */
2955 static int add_loopback_list(struct hda_gen_spec
*spec
, hda_nid_t mix
, int idx
)
2957 struct hda_amp_list
*list
;
2959 list
= snd_array_new(&spec
->loopback_list
);
2963 list
->dir
= HDA_INPUT
;
2965 spec
->loopback
.amplist
= spec
->loopback_list
.list
;
2969 /* return true if either a volume or a mute amp is found for the given
2970 * aamix path; the amp has to be either in the mixer node or its direct leaf
2972 static bool look_for_mix_leaf_ctls(struct hda_codec
*codec
, hda_nid_t mix_nid
,
2973 hda_nid_t pin
, unsigned int *mix_val
,
2974 unsigned int *mute_val
)
2977 const hda_nid_t
*list
;
2980 idx
= snd_hda_get_conn_index(codec
, mix_nid
, pin
, true);
2984 *mix_val
= *mute_val
= 0;
2985 if (nid_has_volume(codec
, mix_nid
, HDA_INPUT
))
2986 *mix_val
= HDA_COMPOSE_AMP_VAL(mix_nid
, 3, idx
, HDA_INPUT
);
2987 if (nid_has_mute(codec
, mix_nid
, HDA_INPUT
))
2988 *mute_val
= HDA_COMPOSE_AMP_VAL(mix_nid
, 3, idx
, HDA_INPUT
);
2989 if (*mix_val
&& *mute_val
)
2992 /* check leaf node */
2993 num_conns
= snd_hda_get_conn_list(codec
, mix_nid
, &list
);
2994 if (num_conns
< idx
)
2997 if (!*mix_val
&& nid_has_volume(codec
, nid
, HDA_OUTPUT
) &&
2998 !is_ctl_associated(codec
, nid
, HDA_OUTPUT
, 0, NID_PATH_VOL_CTL
))
2999 *mix_val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3000 if (!*mute_val
&& nid_has_mute(codec
, nid
, HDA_OUTPUT
) &&
3001 !is_ctl_associated(codec
, nid
, HDA_OUTPUT
, 0, NID_PATH_MUTE_CTL
))
3002 *mute_val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3004 return *mix_val
|| *mute_val
;
3007 /* create input playback/capture controls for the given pin */
3008 static int new_analog_input(struct hda_codec
*codec
, int input_idx
,
3009 hda_nid_t pin
, const char *ctlname
, int ctlidx
,
3012 struct hda_gen_spec
*spec
= codec
->spec
;
3013 struct nid_path
*path
;
3014 unsigned int mix_val
, mute_val
;
3017 if (!look_for_mix_leaf_ctls(codec
, mix_nid
, pin
, &mix_val
, &mute_val
))
3020 path
= snd_hda_add_new_path(codec
, pin
, mix_nid
, 0);
3023 print_nid_path(codec
, "loopback", path
);
3024 spec
->loopback_paths
[input_idx
] = snd_hda_get_path_idx(codec
, path
);
3026 idx
= path
->idx
[path
->depth
- 1];
3028 err
= __add_pb_vol_ctrl(spec
, HDA_CTL_WIDGET_VOL
, ctlname
, ctlidx
, mix_val
);
3031 path
->ctls
[NID_PATH_VOL_CTL
] = mix_val
;
3035 err
= __add_pb_sw_ctrl(spec
, HDA_CTL_WIDGET_MUTE
, ctlname
, ctlidx
, mute_val
);
3038 path
->ctls
[NID_PATH_MUTE_CTL
] = mute_val
;
3041 path
->active
= true;
3042 path
->stream_enabled
= true; /* no DAC/ADC involved */
3043 err
= add_loopback_list(spec
, mix_nid
, idx
);
3047 if (spec
->mixer_nid
!= spec
->mixer_merge_nid
&&
3048 !spec
->loopback_merge_path
) {
3049 path
= snd_hda_add_new_path(codec
, spec
->mixer_nid
,
3050 spec
->mixer_merge_nid
, 0);
3052 print_nid_path(codec
, "loopback-merge", path
);
3053 path
->active
= true;
3054 path
->pin_fixed
= true; /* static route */
3055 path
->stream_enabled
= true; /* no DAC/ADC involved */
3056 spec
->loopback_merge_path
=
3057 snd_hda_get_path_idx(codec
, path
);
3064 static int is_input_pin(struct hda_codec
*codec
, hda_nid_t nid
)
3066 unsigned int pincap
= snd_hda_query_pin_caps(codec
, nid
);
3067 return (pincap
& AC_PINCAP_IN
) != 0;
3070 /* Parse the codec tree and retrieve ADCs */
3071 static int fill_adc_nids(struct hda_codec
*codec
)
3073 struct hda_gen_spec
*spec
= codec
->spec
;
3075 hda_nid_t
*adc_nids
= spec
->adc_nids
;
3076 int max_nums
= ARRAY_SIZE(spec
->adc_nids
);
3079 for_each_hda_codec_node(nid
, codec
) {
3080 unsigned int caps
= get_wcaps(codec
, nid
);
3081 int type
= get_wcaps_type(caps
);
3083 if (type
!= AC_WID_AUD_IN
|| (caps
& AC_WCAP_DIGITAL
))
3085 adc_nids
[nums
] = nid
;
3086 if (++nums
>= max_nums
)
3089 spec
->num_adc_nids
= nums
;
3091 /* copy the detected ADCs to all_adcs[] */
3092 spec
->num_all_adcs
= nums
;
3093 memcpy(spec
->all_adcs
, spec
->adc_nids
, nums
* sizeof(hda_nid_t
));
3098 /* filter out invalid adc_nids that don't give all active input pins;
3099 * if needed, check whether dynamic ADC-switching is available
3101 static int check_dyn_adc_switch(struct hda_codec
*codec
)
3103 struct hda_gen_spec
*spec
= codec
->spec
;
3104 struct hda_input_mux
*imux
= &spec
->input_mux
;
3105 unsigned int ok_bits
;
3110 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
3111 for (i
= 0; i
< imux
->num_items
; i
++) {
3112 if (!spec
->input_paths
[i
][n
])
3115 if (i
>= imux
->num_items
) {
3116 ok_bits
|= (1 << n
);
3122 /* check whether ADC-switch is possible */
3123 for (i
= 0; i
< imux
->num_items
; i
++) {
3124 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
3125 if (spec
->input_paths
[i
][n
]) {
3126 spec
->dyn_adc_idx
[i
] = n
;
3132 codec_dbg(codec
, "enabling ADC switching\n");
3133 spec
->dyn_adc_switch
= 1;
3134 } else if (nums
!= spec
->num_adc_nids
) {
3135 /* shrink the invalid adcs and input paths */
3137 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
3138 if (!(ok_bits
& (1 << n
)))
3141 spec
->adc_nids
[nums
] = spec
->adc_nids
[n
];
3142 for (i
= 0; i
< imux
->num_items
; i
++) {
3143 invalidate_nid_path(codec
,
3144 spec
->input_paths
[i
][nums
]);
3145 spec
->input_paths
[i
][nums
] =
3146 spec
->input_paths
[i
][n
];
3151 spec
->num_adc_nids
= nums
;
3154 if (imux
->num_items
== 1 ||
3155 (imux
->num_items
== 2 && spec
->hp_mic
)) {
3156 codec_dbg(codec
, "reducing to a single ADC\n");
3157 spec
->num_adc_nids
= 1; /* reduce to a single ADC */
3160 /* single index for individual volumes ctls */
3161 if (!spec
->dyn_adc_switch
&& spec
->multi_cap_vol
)
3162 spec
->num_adc_nids
= 1;
3167 /* parse capture source paths from the given pin and create imux items */
3168 static int parse_capture_source(struct hda_codec
*codec
, hda_nid_t pin
,
3169 int cfg_idx
, int num_adcs
,
3170 const char *label
, int anchor
)
3172 struct hda_gen_spec
*spec
= codec
->spec
;
3173 struct hda_input_mux
*imux
= &spec
->input_mux
;
3174 int imux_idx
= imux
->num_items
;
3175 bool imux_added
= false;
3178 for (c
= 0; c
< num_adcs
; c
++) {
3179 struct nid_path
*path
;
3180 hda_nid_t adc
= spec
->adc_nids
[c
];
3182 if (!is_reachable_path(codec
, pin
, adc
))
3184 path
= snd_hda_add_new_path(codec
, pin
, adc
, anchor
);
3187 print_nid_path(codec
, "input", path
);
3188 spec
->input_paths
[imux_idx
][c
] =
3189 snd_hda_get_path_idx(codec
, path
);
3192 if (spec
->hp_mic_pin
== pin
)
3193 spec
->hp_mic_mux_idx
= imux
->num_items
;
3194 spec
->imux_pins
[imux
->num_items
] = pin
;
3195 snd_hda_add_imux_item(codec
, imux
, label
, cfg_idx
, NULL
);
3197 if (spec
->dyn_adc_switch
)
3198 spec
->dyn_adc_idx
[imux_idx
] = c
;
3206 * create playback/capture controls for input pins
3209 /* fill the label for each input at first */
3210 static int fill_input_pin_labels(struct hda_codec
*codec
)
3212 struct hda_gen_spec
*spec
= codec
->spec
;
3213 const struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3216 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
3217 hda_nid_t pin
= cfg
->inputs
[i
].pin
;
3221 if (!is_input_pin(codec
, pin
))
3224 label
= hda_get_autocfg_input_label(codec
, cfg
, i
);
3226 for (j
= i
- 1; j
>= 0; j
--) {
3227 if (spec
->input_labels
[j
] &&
3228 !strcmp(spec
->input_labels
[j
], label
)) {
3229 idx
= spec
->input_label_idxs
[j
] + 1;
3234 spec
->input_labels
[i
] = label
;
3235 spec
->input_label_idxs
[i
] = idx
;
3241 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3243 static int create_input_ctls(struct hda_codec
*codec
)
3245 struct hda_gen_spec
*spec
= codec
->spec
;
3246 const struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3247 hda_nid_t mixer
= spec
->mixer_nid
;
3252 num_adcs
= fill_adc_nids(codec
);
3256 err
= fill_input_pin_labels(codec
);
3260 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
3263 pin
= cfg
->inputs
[i
].pin
;
3264 if (!is_input_pin(codec
, pin
))
3268 if (cfg
->inputs
[i
].type
== AUTO_PIN_MIC
)
3269 val
|= snd_hda_get_default_vref(codec
, pin
);
3270 if (pin
!= spec
->hp_mic_pin
&&
3271 !snd_hda_codec_get_pin_target(codec
, pin
))
3272 set_pin_target(codec
, pin
, val
, false);
3275 if (is_reachable_path(codec
, pin
, mixer
)) {
3276 err
= new_analog_input(codec
, i
, pin
,
3277 spec
->input_labels
[i
],
3278 spec
->input_label_idxs
[i
],
3285 err
= parse_capture_source(codec
, pin
, i
, num_adcs
,
3286 spec
->input_labels
[i
], -mixer
);
3290 if (spec
->add_jack_modes
) {
3291 err
= create_in_jack_mode(codec
, pin
);
3297 /* add stereo mix when explicitly enabled via hint */
3298 if (mixer
&& spec
->add_stereo_mix_input
== HDA_HINT_STEREO_MIX_ENABLE
) {
3299 err
= parse_capture_source(codec
, mixer
, CFG_IDX_MIX
, num_adcs
,
3304 spec
->suppress_auto_mic
= 1;
3315 /* get the input path specified by the given adc and imux indices */
3316 static struct nid_path
*get_input_path(struct hda_codec
*codec
, int adc_idx
, int imux_idx
)
3318 struct hda_gen_spec
*spec
= codec
->spec
;
3319 if (imux_idx
< 0 || imux_idx
>= HDA_MAX_NUM_INPUTS
) {
3323 if (spec
->dyn_adc_switch
)
3324 adc_idx
= spec
->dyn_adc_idx
[imux_idx
];
3325 if (adc_idx
< 0 || adc_idx
>= AUTO_CFG_MAX_INS
) {
3329 return snd_hda_get_path_from_idx(codec
, spec
->input_paths
[imux_idx
][adc_idx
]);
3332 static int mux_select(struct hda_codec
*codec
, unsigned int adc_idx
,
3335 static int mux_enum_info(struct snd_kcontrol
*kcontrol
,
3336 struct snd_ctl_elem_info
*uinfo
)
3338 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3339 struct hda_gen_spec
*spec
= codec
->spec
;
3340 return snd_hda_input_mux_info(&spec
->input_mux
, uinfo
);
3343 static int mux_enum_get(struct snd_kcontrol
*kcontrol
,
3344 struct snd_ctl_elem_value
*ucontrol
)
3346 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3347 struct hda_gen_spec
*spec
= codec
->spec
;
3348 /* the ctls are created at once with multiple counts */
3349 unsigned int adc_idx
= snd_ctl_get_ioffidx(kcontrol
, &ucontrol
->id
);
3351 ucontrol
->value
.enumerated
.item
[0] = spec
->cur_mux
[adc_idx
];
3355 static int mux_enum_put(struct snd_kcontrol
*kcontrol
,
3356 struct snd_ctl_elem_value
*ucontrol
)
3358 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3359 unsigned int adc_idx
= snd_ctl_get_ioffidx(kcontrol
, &ucontrol
->id
);
3360 return mux_select(codec
, adc_idx
,
3361 ucontrol
->value
.enumerated
.item
[0]);
3364 static const struct snd_kcontrol_new cap_src_temp
= {
3365 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3366 .name
= "Input Source",
3367 .info
= mux_enum_info
,
3368 .get
= mux_enum_get
,
3369 .put
= mux_enum_put
,
3373 * capture volume and capture switch ctls
3376 typedef int (*put_call_t
)(struct snd_kcontrol
*kcontrol
,
3377 struct snd_ctl_elem_value
*ucontrol
);
3379 /* call the given amp update function for all amps in the imux list at once */
3380 static int cap_put_caller(struct snd_kcontrol
*kcontrol
,
3381 struct snd_ctl_elem_value
*ucontrol
,
3382 put_call_t func
, int type
)
3384 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3385 struct hda_gen_spec
*spec
= codec
->spec
;
3386 const struct hda_input_mux
*imux
;
3387 struct nid_path
*path
;
3388 int i
, adc_idx
, err
= 0;
3390 imux
= &spec
->input_mux
;
3391 adc_idx
= kcontrol
->id
.index
;
3392 mutex_lock(&codec
->control_mutex
);
3393 for (i
= 0; i
< imux
->num_items
; i
++) {
3394 path
= get_input_path(codec
, adc_idx
, i
);
3395 if (!path
|| !path
->ctls
[type
])
3397 kcontrol
->private_value
= path
->ctls
[type
];
3398 err
= func(kcontrol
, ucontrol
);
3402 mutex_unlock(&codec
->control_mutex
);
3403 if (err
>= 0 && spec
->cap_sync_hook
)
3404 spec
->cap_sync_hook(codec
, kcontrol
, ucontrol
);
3408 /* capture volume ctl callbacks */
3409 #define cap_vol_info snd_hda_mixer_amp_volume_info
3410 #define cap_vol_get snd_hda_mixer_amp_volume_get
3411 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3413 static int cap_vol_put(struct snd_kcontrol
*kcontrol
,
3414 struct snd_ctl_elem_value
*ucontrol
)
3416 return cap_put_caller(kcontrol
, ucontrol
,
3417 snd_hda_mixer_amp_volume_put
,
3421 static const struct snd_kcontrol_new cap_vol_temp
= {
3422 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3423 .name
= "Capture Volume",
3424 .access
= (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
3425 SNDRV_CTL_ELEM_ACCESS_TLV_READ
|
3426 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
),
3427 .info
= cap_vol_info
,
3430 .tlv
= { .c
= cap_vol_tlv
},
3433 /* capture switch ctl callbacks */
3434 #define cap_sw_info snd_ctl_boolean_stereo_info
3435 #define cap_sw_get snd_hda_mixer_amp_switch_get
3437 static int cap_sw_put(struct snd_kcontrol
*kcontrol
,
3438 struct snd_ctl_elem_value
*ucontrol
)
3440 return cap_put_caller(kcontrol
, ucontrol
,
3441 snd_hda_mixer_amp_switch_put
,
3445 static const struct snd_kcontrol_new cap_sw_temp
= {
3446 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3447 .name
= "Capture Switch",
3448 .info
= cap_sw_info
,
3453 static int parse_capvol_in_path(struct hda_codec
*codec
, struct nid_path
*path
)
3458 path
->ctls
[NID_PATH_VOL_CTL
] = path
->ctls
[NID_PATH_MUTE_CTL
] = 0;
3459 for (depth
= 0; depth
< 3; depth
++) {
3460 if (depth
>= path
->depth
)
3462 i
= path
->depth
- depth
- 1;
3463 nid
= path
->path
[i
];
3464 if (!path
->ctls
[NID_PATH_VOL_CTL
]) {
3465 if (nid_has_volume(codec
, nid
, HDA_OUTPUT
))
3466 path
->ctls
[NID_PATH_VOL_CTL
] =
3467 HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3468 else if (nid_has_volume(codec
, nid
, HDA_INPUT
)) {
3469 int idx
= path
->idx
[i
];
3470 if (!depth
&& codec
->single_adc_amp
)
3472 path
->ctls
[NID_PATH_VOL_CTL
] =
3473 HDA_COMPOSE_AMP_VAL(nid
, 3, idx
, HDA_INPUT
);
3476 if (!path
->ctls
[NID_PATH_MUTE_CTL
]) {
3477 if (nid_has_mute(codec
, nid
, HDA_OUTPUT
))
3478 path
->ctls
[NID_PATH_MUTE_CTL
] =
3479 HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3480 else if (nid_has_mute(codec
, nid
, HDA_INPUT
)) {
3481 int idx
= path
->idx
[i
];
3482 if (!depth
&& codec
->single_adc_amp
)
3484 path
->ctls
[NID_PATH_MUTE_CTL
] =
3485 HDA_COMPOSE_AMP_VAL(nid
, 3, idx
, HDA_INPUT
);
3492 static bool is_inv_dmic_pin(struct hda_codec
*codec
, hda_nid_t nid
)
3494 struct hda_gen_spec
*spec
= codec
->spec
;
3495 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3499 if (!spec
->inv_dmic_split
)
3501 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
3502 if (cfg
->inputs
[i
].pin
!= nid
)
3504 if (cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
3506 val
= snd_hda_codec_get_pincfg(codec
, nid
);
3507 return snd_hda_get_input_pin_attr(val
) == INPUT_PIN_ATTR_INT
;
3512 /* capture switch put callback for a single control with hook call */
3513 static int cap_single_sw_put(struct snd_kcontrol
*kcontrol
,
3514 struct snd_ctl_elem_value
*ucontrol
)
3516 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3517 struct hda_gen_spec
*spec
= codec
->spec
;
3520 ret
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
3524 if (spec
->cap_sync_hook
)
3525 spec
->cap_sync_hook(codec
, kcontrol
, ucontrol
);
3530 static int add_single_cap_ctl(struct hda_codec
*codec
, const char *label
,
3531 int idx
, bool is_switch
, unsigned int ctl
,
3534 struct hda_gen_spec
*spec
= codec
->spec
;
3535 char tmpname
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
3536 int type
= is_switch
? HDA_CTL_WIDGET_MUTE
: HDA_CTL_WIDGET_VOL
;
3537 const char *sfx
= is_switch
? "Switch" : "Volume";
3538 unsigned int chs
= inv_dmic
? 1 : 3;
3539 struct snd_kcontrol_new
*knew
;
3545 snprintf(tmpname
, sizeof(tmpname
),
3546 "%s Capture %s", label
, sfx
);
3548 snprintf(tmpname
, sizeof(tmpname
),
3550 knew
= add_control(spec
, type
, tmpname
, idx
,
3551 amp_val_replace_channels(ctl
, chs
));
3555 knew
->put
= cap_single_sw_put
;
3559 /* Make independent right kcontrol */
3561 snprintf(tmpname
, sizeof(tmpname
),
3562 "Inverted %s Capture %s", label
, sfx
);
3564 snprintf(tmpname
, sizeof(tmpname
),
3565 "Inverted Capture %s", sfx
);
3566 knew
= add_control(spec
, type
, tmpname
, idx
,
3567 amp_val_replace_channels(ctl
, 2));
3571 knew
->put
= cap_single_sw_put
;
3575 /* create single (and simple) capture volume and switch controls */
3576 static int create_single_cap_vol_ctl(struct hda_codec
*codec
, int idx
,
3577 unsigned int vol_ctl
, unsigned int sw_ctl
,
3581 err
= add_single_cap_ctl(codec
, NULL
, idx
, false, vol_ctl
, inv_dmic
);
3584 err
= add_single_cap_ctl(codec
, NULL
, idx
, true, sw_ctl
, inv_dmic
);
3590 /* create bound capture volume and switch controls */
3591 static int create_bind_cap_vol_ctl(struct hda_codec
*codec
, int idx
,
3592 unsigned int vol_ctl
, unsigned int sw_ctl
)
3594 struct hda_gen_spec
*spec
= codec
->spec
;
3595 struct snd_kcontrol_new
*knew
;
3598 knew
= snd_hda_gen_add_kctl(spec
, NULL
, &cap_vol_temp
);
3602 knew
->private_value
= vol_ctl
;
3603 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
3606 knew
= snd_hda_gen_add_kctl(spec
, NULL
, &cap_sw_temp
);
3610 knew
->private_value
= sw_ctl
;
3611 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
3616 /* return the vol ctl when used first in the imux list */
3617 static unsigned int get_first_cap_ctl(struct hda_codec
*codec
, int idx
, int type
)
3619 struct nid_path
*path
;
3623 path
= get_input_path(codec
, 0, idx
);
3626 ctl
= path
->ctls
[type
];
3629 for (i
= 0; i
< idx
- 1; i
++) {
3630 path
= get_input_path(codec
, 0, i
);
3631 if (path
&& path
->ctls
[type
] == ctl
)
3637 /* create individual capture volume and switch controls per input */
3638 static int create_multi_cap_vol_ctl(struct hda_codec
*codec
)
3640 struct hda_gen_spec
*spec
= codec
->spec
;
3641 struct hda_input_mux
*imux
= &spec
->input_mux
;
3644 for (i
= 0; i
< imux
->num_items
; i
++) {
3648 idx
= imux
->items
[i
].index
;
3649 if (idx
>= spec
->autocfg
.num_inputs
)
3651 inv_dmic
= is_inv_dmic_pin(codec
, spec
->imux_pins
[i
]);
3653 for (type
= 0; type
< 2; type
++) {
3654 err
= add_single_cap_ctl(codec
,
3655 spec
->input_labels
[idx
],
3656 spec
->input_label_idxs
[idx
],
3658 get_first_cap_ctl(codec
, i
, type
),
3667 static int create_capture_mixers(struct hda_codec
*codec
)
3669 struct hda_gen_spec
*spec
= codec
->spec
;
3670 struct hda_input_mux
*imux
= &spec
->input_mux
;
3671 int i
, n
, nums
, err
;
3673 if (spec
->dyn_adc_switch
)
3676 nums
= spec
->num_adc_nids
;
3678 if (!spec
->auto_mic
&& imux
->num_items
> 1) {
3679 struct snd_kcontrol_new
*knew
;
3681 name
= nums
> 1 ? "Input Source" : "Capture Source";
3682 knew
= snd_hda_gen_add_kctl(spec
, name
, &cap_src_temp
);
3688 for (n
= 0; n
< nums
; n
++) {
3690 bool multi_cap_vol
= spec
->multi_cap_vol
;
3691 bool inv_dmic
= false;
3695 for (i
= 0; i
< imux
->num_items
; i
++) {
3696 struct nid_path
*path
;
3697 path
= get_input_path(codec
, n
, i
);
3700 parse_capvol_in_path(codec
, path
);
3702 vol
= path
->ctls
[NID_PATH_VOL_CTL
];
3703 else if (vol
!= path
->ctls
[NID_PATH_VOL_CTL
]) {
3705 if (!same_amp_caps(codec
, vol
,
3706 path
->ctls
[NID_PATH_VOL_CTL
], HDA_INPUT
))
3707 multi_cap_vol
= true;
3710 sw
= path
->ctls
[NID_PATH_MUTE_CTL
];
3711 else if (sw
!= path
->ctls
[NID_PATH_MUTE_CTL
]) {
3713 if (!same_amp_caps(codec
, sw
,
3714 path
->ctls
[NID_PATH_MUTE_CTL
], HDA_INPUT
))
3715 multi_cap_vol
= true;
3717 if (is_inv_dmic_pin(codec
, spec
->imux_pins
[i
]))
3722 err
= create_single_cap_vol_ctl(codec
, n
, vol
, sw
,
3724 else if (!multi_cap_vol
&& !inv_dmic
)
3725 err
= create_bind_cap_vol_ctl(codec
, n
, vol
, sw
);
3727 err
= create_multi_cap_vol_ctl(codec
);
3736 * add mic boosts if needed
3739 /* check whether the given amp is feasible as a boost volume */
3740 static bool check_boost_vol(struct hda_codec
*codec
, hda_nid_t nid
,
3745 if (!nid_has_volume(codec
, nid
, dir
) ||
3746 is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_VOL_CTL
) ||
3747 is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_BOOST_CTL
))
3750 step
= (query_amp_caps(codec
, nid
, dir
) & AC_AMPCAP_STEP_SIZE
)
3751 >> AC_AMPCAP_STEP_SIZE_SHIFT
;
3757 /* look for a boost amp in a widget close to the pin */
3758 static unsigned int look_for_boost_amp(struct hda_codec
*codec
,
3759 struct nid_path
*path
)
3761 unsigned int val
= 0;
3765 for (depth
= 0; depth
< 3; depth
++) {
3766 if (depth
>= path
->depth
- 1)
3768 nid
= path
->path
[depth
];
3769 if (depth
&& check_boost_vol(codec
, nid
, HDA_OUTPUT
, 0)) {
3770 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3772 } else if (check_boost_vol(codec
, nid
, HDA_INPUT
,
3773 path
->idx
[depth
])) {
3774 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, path
->idx
[depth
],
3783 static int parse_mic_boost(struct hda_codec
*codec
)
3785 struct hda_gen_spec
*spec
= codec
->spec
;
3786 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3787 struct hda_input_mux
*imux
= &spec
->input_mux
;
3790 if (!spec
->num_adc_nids
)
3793 for (i
= 0; i
< imux
->num_items
; i
++) {
3794 struct nid_path
*path
;
3797 char boost_label
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
3799 idx
= imux
->items
[i
].index
;
3800 if (idx
>= imux
->num_items
)
3803 /* check only line-in and mic pins */
3804 if (cfg
->inputs
[idx
].type
> AUTO_PIN_LINE_IN
)
3807 path
= get_input_path(codec
, 0, i
);
3811 val
= look_for_boost_amp(codec
, path
);
3815 /* create a boost control */
3816 snprintf(boost_label
, sizeof(boost_label
),
3817 "%s Boost Volume", spec
->input_labels
[idx
]);
3818 if (!add_control(spec
, HDA_CTL_WIDGET_VOL
, boost_label
,
3819 spec
->input_label_idxs
[idx
], val
))
3822 path
->ctls
[NID_PATH_BOOST_CTL
] = val
;
3828 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3830 static void parse_digital(struct hda_codec
*codec
)
3832 struct hda_gen_spec
*spec
= codec
->spec
;
3833 struct nid_path
*path
;
3835 hda_nid_t dig_nid
, pin
;
3837 /* support multiple SPDIFs; the secondary is set up as a slave */
3839 for (i
= 0; i
< spec
->autocfg
.dig_outs
; i
++) {
3840 pin
= spec
->autocfg
.dig_out_pins
[i
];
3841 dig_nid
= look_for_dac(codec
, pin
, true);
3844 path
= snd_hda_add_new_path(codec
, dig_nid
, pin
, 0);
3847 print_nid_path(codec
, "digout", path
);
3848 path
->active
= true;
3849 path
->pin_fixed
= true; /* no jack detection */
3850 spec
->digout_paths
[i
] = snd_hda_get_path_idx(codec
, path
);
3851 set_pin_target(codec
, pin
, PIN_OUT
, false);
3853 spec
->multiout
.dig_out_nid
= dig_nid
;
3854 spec
->dig_out_type
= spec
->autocfg
.dig_out_type
[0];
3856 spec
->multiout
.slave_dig_outs
= spec
->slave_dig_outs
;
3857 if (nums
>= ARRAY_SIZE(spec
->slave_dig_outs
) - 1)
3859 spec
->slave_dig_outs
[nums
- 1] = dig_nid
;
3864 if (spec
->autocfg
.dig_in_pin
) {
3865 pin
= spec
->autocfg
.dig_in_pin
;
3866 for_each_hda_codec_node(dig_nid
, codec
) {
3867 unsigned int wcaps
= get_wcaps(codec
, dig_nid
);
3868 if (get_wcaps_type(wcaps
) != AC_WID_AUD_IN
)
3870 if (!(wcaps
& AC_WCAP_DIGITAL
))
3872 path
= snd_hda_add_new_path(codec
, pin
, dig_nid
, 0);
3874 print_nid_path(codec
, "digin", path
);
3875 path
->active
= true;
3876 path
->pin_fixed
= true; /* no jack */
3877 spec
->dig_in_nid
= dig_nid
;
3878 spec
->digin_path
= snd_hda_get_path_idx(codec
, path
);
3879 set_pin_target(codec
, pin
, PIN_IN
, false);
3888 * input MUX handling
3891 static bool dyn_adc_pcm_resetup(struct hda_codec
*codec
, int cur
);
3893 /* select the given imux item; either unmute exclusively or select the route */
3894 static int mux_select(struct hda_codec
*codec
, unsigned int adc_idx
,
3897 struct hda_gen_spec
*spec
= codec
->spec
;
3898 const struct hda_input_mux
*imux
;
3899 struct nid_path
*old_path
, *path
;
3901 imux
= &spec
->input_mux
;
3902 if (!imux
->num_items
)
3905 if (idx
>= imux
->num_items
)
3906 idx
= imux
->num_items
- 1;
3907 if (spec
->cur_mux
[adc_idx
] == idx
)
3910 old_path
= get_input_path(codec
, adc_idx
, spec
->cur_mux
[adc_idx
]);
3913 if (old_path
->active
)
3914 snd_hda_activate_path(codec
, old_path
, false, false);
3916 spec
->cur_mux
[adc_idx
] = idx
;
3919 update_hp_mic(codec
, adc_idx
, false);
3921 if (spec
->dyn_adc_switch
)
3922 dyn_adc_pcm_resetup(codec
, idx
);
3924 path
= get_input_path(codec
, adc_idx
, idx
);
3929 snd_hda_activate_path(codec
, path
, true, false);
3930 if (spec
->cap_sync_hook
)
3931 spec
->cap_sync_hook(codec
, NULL
, NULL
);
3932 path_power_down_sync(codec
, old_path
);
3936 /* power up/down widgets in the all paths that match with the given NID
3937 * as terminals (either start- or endpoint)
3939 * returns the last changed NID, or zero if unchanged.
3941 static hda_nid_t
set_path_power(struct hda_codec
*codec
, hda_nid_t nid
,
3942 int pin_state
, int stream_state
)
3944 struct hda_gen_spec
*spec
= codec
->spec
;
3945 hda_nid_t last
, changed
= 0;
3946 struct nid_path
*path
;
3949 for (n
= 0; n
< spec
->paths
.used
; n
++) {
3950 path
= snd_array_elem(&spec
->paths
, n
);
3951 if (path
->path
[0] == nid
||
3952 path
->path
[path
->depth
- 1] == nid
) {
3953 bool pin_old
= path
->pin_enabled
;
3954 bool stream_old
= path
->stream_enabled
;
3957 path
->pin_enabled
= pin_state
;
3958 if (stream_state
>= 0)
3959 path
->stream_enabled
= stream_state
;
3960 if ((!path
->pin_fixed
&& path
->pin_enabled
!= pin_old
)
3961 || path
->stream_enabled
!= stream_old
) {
3962 last
= path_power_update(codec
, path
, true);
3971 /* check the jack status for power control */
3972 static bool detect_pin_state(struct hda_codec
*codec
, hda_nid_t pin
)
3974 if (!is_jack_detectable(codec
, pin
))
3976 return snd_hda_jack_detect_state(codec
, pin
) != HDA_JACK_NOT_PRESENT
;
3979 /* power up/down the paths of the given pin according to the jack state;
3980 * power = 0/1 : only power up/down if it matches with the jack state,
3981 * < 0 : force power up/down to follow the jack sate
3983 * returns the last changed NID, or zero if unchanged.
3985 static hda_nid_t
set_pin_power_jack(struct hda_codec
*codec
, hda_nid_t pin
,
3990 if (!codec
->power_save_node
)
3993 on
= detect_pin_state(codec
, pin
);
3995 if (power
>= 0 && on
!= power
)
3997 return set_path_power(codec
, pin
, on
, -1);
4000 static void pin_power_callback(struct hda_codec
*codec
,
4001 struct hda_jack_callback
*jack
,
4004 if (jack
&& jack
->tbl
->nid
)
4005 sync_power_state_change(codec
,
4006 set_pin_power_jack(codec
, jack
->tbl
->nid
, on
));
4009 /* callback only doing power up -- called at first */
4010 static void pin_power_up_callback(struct hda_codec
*codec
,
4011 struct hda_jack_callback
*jack
)
4013 pin_power_callback(codec
, jack
, true);
4016 /* callback only doing power down -- called at last */
4017 static void pin_power_down_callback(struct hda_codec
*codec
,
4018 struct hda_jack_callback
*jack
)
4020 pin_power_callback(codec
, jack
, false);
4023 /* set up the power up/down callbacks */
4024 static void add_pin_power_ctls(struct hda_codec
*codec
, int num_pins
,
4025 const hda_nid_t
*pins
, bool on
)
4028 hda_jack_callback_fn cb
=
4029 on
? pin_power_up_callback
: pin_power_down_callback
;
4031 for (i
= 0; i
< num_pins
&& pins
[i
]; i
++) {
4032 if (is_jack_detectable(codec
, pins
[i
]))
4033 snd_hda_jack_detect_enable_callback(codec
, pins
[i
], cb
);
4035 set_path_power(codec
, pins
[i
], true, -1);
4039 /* enabled power callback to each available I/O pin with jack detections;
4040 * the digital I/O pins are excluded because of the unreliable detectsion
4042 static void add_all_pin_power_ctls(struct hda_codec
*codec
, bool on
)
4044 struct hda_gen_spec
*spec
= codec
->spec
;
4045 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4048 if (!codec
->power_save_node
)
4050 add_pin_power_ctls(codec
, cfg
->line_outs
, cfg
->line_out_pins
, on
);
4051 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
4052 add_pin_power_ctls(codec
, cfg
->hp_outs
, cfg
->hp_pins
, on
);
4053 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
4054 add_pin_power_ctls(codec
, cfg
->speaker_outs
, cfg
->speaker_pins
, on
);
4055 for (i
= 0; i
< cfg
->num_inputs
; i
++)
4056 add_pin_power_ctls(codec
, 1, &cfg
->inputs
[i
].pin
, on
);
4059 /* sync path power up/down with the jack states of given pins */
4060 static void sync_pin_power_ctls(struct hda_codec
*codec
, int num_pins
,
4061 const hda_nid_t
*pins
)
4065 for (i
= 0; i
< num_pins
&& pins
[i
]; i
++)
4066 if (is_jack_detectable(codec
, pins
[i
]))
4067 set_pin_power_jack(codec
, pins
[i
], -1);
4070 /* sync path power up/down with pins; called at init and resume */
4071 static void sync_all_pin_power_ctls(struct hda_codec
*codec
)
4073 struct hda_gen_spec
*spec
= codec
->spec
;
4074 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4077 if (!codec
->power_save_node
)
4079 sync_pin_power_ctls(codec
, cfg
->line_outs
, cfg
->line_out_pins
);
4080 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
4081 sync_pin_power_ctls(codec
, cfg
->hp_outs
, cfg
->hp_pins
);
4082 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
4083 sync_pin_power_ctls(codec
, cfg
->speaker_outs
, cfg
->speaker_pins
);
4084 for (i
= 0; i
< cfg
->num_inputs
; i
++)
4085 sync_pin_power_ctls(codec
, 1, &cfg
->inputs
[i
].pin
);
4088 /* add fake paths if not present yet */
4089 static int add_fake_paths(struct hda_codec
*codec
, hda_nid_t nid
,
4090 int num_pins
, const hda_nid_t
*pins
)
4092 struct hda_gen_spec
*spec
= codec
->spec
;
4093 struct nid_path
*path
;
4096 for (i
= 0; i
< num_pins
; i
++) {
4099 if (get_nid_path(codec
, nid
, pins
[i
], 0))
4101 path
= snd_array_new(&spec
->paths
);
4104 memset(path
, 0, sizeof(*path
));
4106 path
->path
[0] = nid
;
4107 path
->path
[1] = pins
[i
];
4108 path
->active
= true;
4113 /* create fake paths to all outputs from beep */
4114 static int add_fake_beep_paths(struct hda_codec
*codec
)
4116 struct hda_gen_spec
*spec
= codec
->spec
;
4117 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4118 hda_nid_t nid
= spec
->beep_nid
;
4121 if (!codec
->power_save_node
|| !nid
)
4123 err
= add_fake_paths(codec
, nid
, cfg
->line_outs
, cfg
->line_out_pins
);
4126 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
) {
4127 err
= add_fake_paths(codec
, nid
, cfg
->hp_outs
, cfg
->hp_pins
);
4131 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
4132 err
= add_fake_paths(codec
, nid
, cfg
->speaker_outs
,
4140 /* power up/down beep widget and its output paths */
4141 static void beep_power_hook(struct hda_beep
*beep
, bool on
)
4143 set_path_power(beep
->codec
, beep
->nid
, -1, on
);
4147 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4148 * @codec: the HDA codec
4149 * @pin: NID of pin to fix
4151 int snd_hda_gen_fix_pin_power(struct hda_codec
*codec
, hda_nid_t pin
)
4153 struct hda_gen_spec
*spec
= codec
->spec
;
4154 struct nid_path
*path
;
4156 path
= snd_array_new(&spec
->paths
);
4159 memset(path
, 0, sizeof(*path
));
4161 path
->path
[0] = pin
;
4162 path
->active
= true;
4163 path
->pin_fixed
= true;
4164 path
->stream_enabled
= true;
4167 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power
);
4170 * Jack detections for HP auto-mute and mic-switch
4173 /* check each pin in the given array; returns true if any of them is plugged */
4174 static bool detect_jacks(struct hda_codec
*codec
, int num_pins
, hda_nid_t
*pins
)
4177 bool present
= false;
4179 for (i
= 0; i
< num_pins
; i
++) {
4180 hda_nid_t nid
= pins
[i
];
4183 /* don't detect pins retasked as inputs */
4184 if (snd_hda_codec_get_pin_target(codec
, nid
) & AC_PINCTL_IN_EN
)
4186 if (snd_hda_jack_detect_state(codec
, nid
) == HDA_JACK_PRESENT
)
4192 /* standard HP/line-out auto-mute helper */
4193 static void do_automute(struct hda_codec
*codec
, int num_pins
, hda_nid_t
*pins
,
4194 int *paths
, bool mute
)
4196 struct hda_gen_spec
*spec
= codec
->spec
;
4199 for (i
= 0; i
< num_pins
; i
++) {
4200 hda_nid_t nid
= pins
[i
];
4201 unsigned int val
, oldval
;
4205 oldval
= snd_hda_codec_get_pin_target(codec
, nid
);
4206 if (oldval
& PIN_IN
)
4207 continue; /* no mute for inputs */
4209 if (spec
->auto_mute_via_amp
) {
4210 struct nid_path
*path
;
4213 path
= snd_hda_get_path_from_idx(codec
, paths
[i
]);
4216 mute_nid
= get_amp_nid_(path
->ctls
[NID_PATH_MUTE_CTL
]);
4220 spec
->mute_bits
|= (1ULL << mute_nid
);
4222 spec
->mute_bits
&= ~(1ULL << mute_nid
);
4225 /* don't reset VREF value in case it's controlling
4226 * the amp (see alc861_fixup_asus_amp_vref_0f())
4228 if (spec
->keep_vref_in_automute
)
4229 val
= oldval
& ~PIN_HP
;
4234 /* here we call update_pin_ctl() so that the pinctl is
4235 * changed without changing the pinctl target value;
4236 * the original target value will be still referred at
4237 * the init / resume again
4239 update_pin_ctl(codec
, nid
, val
);
4242 set_pin_eapd(codec
, nid
, !mute
);
4243 if (codec
->power_save_node
) {
4246 on
= detect_pin_state(codec
, nid
);
4247 set_path_power(codec
, nid
, on
, -1);
4253 * snd_hda_gen_update_outputs - Toggle outputs muting
4254 * @codec: the HDA codec
4256 * Update the mute status of all outputs based on the current jack states.
4258 void snd_hda_gen_update_outputs(struct hda_codec
*codec
)
4260 struct hda_gen_spec
*spec
= codec
->spec
;
4264 /* Control HP pins/amps depending on master_mute state;
4265 * in general, HP pins/amps control should be enabled in all cases,
4266 * but currently set only for master_mute, just to be safe
4268 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
4269 paths
= spec
->out_paths
;
4271 paths
= spec
->hp_paths
;
4272 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.hp_pins
),
4273 spec
->autocfg
.hp_pins
, paths
, spec
->master_mute
);
4275 if (!spec
->automute_speaker
)
4278 on
= spec
->hp_jack_present
| spec
->line_jack_present
;
4279 on
|= spec
->master_mute
;
4280 spec
->speaker_muted
= on
;
4281 if (spec
->autocfg
.line_out_type
== AUTO_PIN_SPEAKER_OUT
)
4282 paths
= spec
->out_paths
;
4284 paths
= spec
->speaker_paths
;
4285 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.speaker_pins
),
4286 spec
->autocfg
.speaker_pins
, paths
, on
);
4288 /* toggle line-out mutes if needed, too */
4289 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4290 if (spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.hp_pins
[0] ||
4291 spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.speaker_pins
[0])
4293 if (!spec
->automute_lo
)
4296 on
= spec
->hp_jack_present
;
4297 on
|= spec
->master_mute
;
4298 spec
->line_out_muted
= on
;
4299 paths
= spec
->out_paths
;
4300 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.line_out_pins
),
4301 spec
->autocfg
.line_out_pins
, paths
, on
);
4303 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs
);
4305 static void call_update_outputs(struct hda_codec
*codec
)
4307 struct hda_gen_spec
*spec
= codec
->spec
;
4308 if (spec
->automute_hook
)
4309 spec
->automute_hook(codec
);
4311 snd_hda_gen_update_outputs(codec
);
4313 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4314 if (spec
->auto_mute_via_amp
&& !codec
->bus
->shutdown
)
4315 snd_ctl_sync_vmaster(spec
->vmaster_mute
.sw_kctl
, false);
4319 * snd_hda_gen_hp_automute - standard HP-automute helper
4320 * @codec: the HDA codec
4321 * @jack: jack object, NULL for the whole
4323 void snd_hda_gen_hp_automute(struct hda_codec
*codec
,
4324 struct hda_jack_callback
*jack
)
4326 struct hda_gen_spec
*spec
= codec
->spec
;
4327 hda_nid_t
*pins
= spec
->autocfg
.hp_pins
;
4328 int num_pins
= ARRAY_SIZE(spec
->autocfg
.hp_pins
);
4330 /* No detection for the first HP jack during indep-HP mode */
4331 if (spec
->indep_hp_enabled
) {
4336 spec
->hp_jack_present
= detect_jacks(codec
, num_pins
, pins
);
4337 if (!spec
->detect_hp
|| (!spec
->automute_speaker
&& !spec
->automute_lo
))
4339 call_update_outputs(codec
);
4341 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute
);
4344 * snd_hda_gen_line_automute - standard line-out-automute helper
4345 * @codec: the HDA codec
4346 * @jack: jack object, NULL for the whole
4348 void snd_hda_gen_line_automute(struct hda_codec
*codec
,
4349 struct hda_jack_callback
*jack
)
4351 struct hda_gen_spec
*spec
= codec
->spec
;
4353 if (spec
->autocfg
.line_out_type
== AUTO_PIN_SPEAKER_OUT
)
4355 /* check LO jack only when it's different from HP */
4356 if (spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.hp_pins
[0])
4359 spec
->line_jack_present
=
4360 detect_jacks(codec
, ARRAY_SIZE(spec
->autocfg
.line_out_pins
),
4361 spec
->autocfg
.line_out_pins
);
4362 if (!spec
->automute_speaker
|| !spec
->detect_lo
)
4364 call_update_outputs(codec
);
4366 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute
);
4369 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4370 * @codec: the HDA codec
4371 * @jack: jack object, NULL for the whole
4373 void snd_hda_gen_mic_autoswitch(struct hda_codec
*codec
,
4374 struct hda_jack_callback
*jack
)
4376 struct hda_gen_spec
*spec
= codec
->spec
;
4379 if (!spec
->auto_mic
)
4382 for (i
= spec
->am_num_entries
- 1; i
> 0; i
--) {
4383 hda_nid_t pin
= spec
->am_entry
[i
].pin
;
4384 /* don't detect pins retasked as outputs */
4385 if (snd_hda_codec_get_pin_target(codec
, pin
) & AC_PINCTL_OUT_EN
)
4387 if (snd_hda_jack_detect_state(codec
, pin
) == HDA_JACK_PRESENT
) {
4388 mux_select(codec
, 0, spec
->am_entry
[i
].idx
);
4392 mux_select(codec
, 0, spec
->am_entry
[0].idx
);
4394 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch
);
4396 /* call appropriate hooks */
4397 static void call_hp_automute(struct hda_codec
*codec
,
4398 struct hda_jack_callback
*jack
)
4400 struct hda_gen_spec
*spec
= codec
->spec
;
4401 if (spec
->hp_automute_hook
)
4402 spec
->hp_automute_hook(codec
, jack
);
4404 snd_hda_gen_hp_automute(codec
, jack
);
4407 static void call_line_automute(struct hda_codec
*codec
,
4408 struct hda_jack_callback
*jack
)
4410 struct hda_gen_spec
*spec
= codec
->spec
;
4411 if (spec
->line_automute_hook
)
4412 spec
->line_automute_hook(codec
, jack
);
4414 snd_hda_gen_line_automute(codec
, jack
);
4417 static void call_mic_autoswitch(struct hda_codec
*codec
,
4418 struct hda_jack_callback
*jack
)
4420 struct hda_gen_spec
*spec
= codec
->spec
;
4421 if (spec
->mic_autoswitch_hook
)
4422 spec
->mic_autoswitch_hook(codec
, jack
);
4424 snd_hda_gen_mic_autoswitch(codec
, jack
);
4427 /* update jack retasking */
4428 static void update_automute_all(struct hda_codec
*codec
)
4430 call_hp_automute(codec
, NULL
);
4431 call_line_automute(codec
, NULL
);
4432 call_mic_autoswitch(codec
, NULL
);
4436 * Auto-Mute mode mixer enum support
4438 static int automute_mode_info(struct snd_kcontrol
*kcontrol
,
4439 struct snd_ctl_elem_info
*uinfo
)
4441 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
4442 struct hda_gen_spec
*spec
= codec
->spec
;
4443 static const char * const texts3
[] = {
4444 "Disabled", "Speaker Only", "Line Out+Speaker"
4447 if (spec
->automute_speaker_possible
&& spec
->automute_lo_possible
)
4448 return snd_hda_enum_helper_info(kcontrol
, uinfo
, 3, texts3
);
4449 return snd_hda_enum_bool_helper_info(kcontrol
, uinfo
);
4452 static int automute_mode_get(struct snd_kcontrol
*kcontrol
,
4453 struct snd_ctl_elem_value
*ucontrol
)
4455 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
4456 struct hda_gen_spec
*spec
= codec
->spec
;
4457 unsigned int val
= 0;
4458 if (spec
->automute_speaker
)
4460 if (spec
->automute_lo
)
4463 ucontrol
->value
.enumerated
.item
[0] = val
;
4467 static int automute_mode_put(struct snd_kcontrol
*kcontrol
,
4468 struct snd_ctl_elem_value
*ucontrol
)
4470 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
4471 struct hda_gen_spec
*spec
= codec
->spec
;
4473 switch (ucontrol
->value
.enumerated
.item
[0]) {
4475 if (!spec
->automute_speaker
&& !spec
->automute_lo
)
4477 spec
->automute_speaker
= 0;
4478 spec
->automute_lo
= 0;
4481 if (spec
->automute_speaker_possible
) {
4482 if (!spec
->automute_lo
&& spec
->automute_speaker
)
4484 spec
->automute_speaker
= 1;
4485 spec
->automute_lo
= 0;
4486 } else if (spec
->automute_lo_possible
) {
4487 if (spec
->automute_lo
)
4489 spec
->automute_lo
= 1;
4494 if (!spec
->automute_lo_possible
|| !spec
->automute_speaker_possible
)
4496 if (spec
->automute_speaker
&& spec
->automute_lo
)
4498 spec
->automute_speaker
= 1;
4499 spec
->automute_lo
= 1;
4504 call_update_outputs(codec
);
4508 static const struct snd_kcontrol_new automute_mode_enum
= {
4509 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
4510 .name
= "Auto-Mute Mode",
4511 .info
= automute_mode_info
,
4512 .get
= automute_mode_get
,
4513 .put
= automute_mode_put
,
4516 static int add_automute_mode_enum(struct hda_codec
*codec
)
4518 struct hda_gen_spec
*spec
= codec
->spec
;
4520 if (!snd_hda_gen_add_kctl(spec
, NULL
, &automute_mode_enum
))
4526 * Check the availability of HP/line-out auto-mute;
4527 * Set up appropriately if really supported
4529 static int check_auto_mute_availability(struct hda_codec
*codec
)
4531 struct hda_gen_spec
*spec
= codec
->spec
;
4532 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4536 if (spec
->suppress_auto_mute
)
4539 if (cfg
->hp_pins
[0])
4541 if (cfg
->line_out_pins
[0])
4543 if (cfg
->speaker_pins
[0])
4545 if (present
< 2) /* need two different output types */
4548 if (!cfg
->speaker_pins
[0] &&
4549 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
) {
4550 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
4551 sizeof(cfg
->speaker_pins
));
4552 cfg
->speaker_outs
= cfg
->line_outs
;
4555 if (!cfg
->hp_pins
[0] &&
4556 cfg
->line_out_type
== AUTO_PIN_HP_OUT
) {
4557 memcpy(cfg
->hp_pins
, cfg
->line_out_pins
,
4558 sizeof(cfg
->hp_pins
));
4559 cfg
->hp_outs
= cfg
->line_outs
;
4562 for (i
= 0; i
< cfg
->hp_outs
; i
++) {
4563 hda_nid_t nid
= cfg
->hp_pins
[i
];
4564 if (!is_jack_detectable(codec
, nid
))
4566 codec_dbg(codec
, "Enable HP auto-muting on NID 0x%x\n", nid
);
4567 snd_hda_jack_detect_enable_callback(codec
, nid
,
4569 spec
->detect_hp
= 1;
4572 if (cfg
->line_out_type
== AUTO_PIN_LINE_OUT
&& cfg
->line_outs
) {
4573 if (cfg
->speaker_outs
)
4574 for (i
= 0; i
< cfg
->line_outs
; i
++) {
4575 hda_nid_t nid
= cfg
->line_out_pins
[i
];
4576 if (!is_jack_detectable(codec
, nid
))
4578 codec_dbg(codec
, "Enable Line-Out auto-muting on NID 0x%x\n", nid
);
4579 snd_hda_jack_detect_enable_callback(codec
, nid
,
4580 call_line_automute
);
4581 spec
->detect_lo
= 1;
4583 spec
->automute_lo_possible
= spec
->detect_hp
;
4586 spec
->automute_speaker_possible
= cfg
->speaker_outs
&&
4587 (spec
->detect_hp
|| spec
->detect_lo
);
4589 spec
->automute_lo
= spec
->automute_lo_possible
;
4590 spec
->automute_speaker
= spec
->automute_speaker_possible
;
4592 if (spec
->automute_speaker_possible
|| spec
->automute_lo_possible
) {
4593 /* create a control for automute mode */
4594 err
= add_automute_mode_enum(codec
);
4601 /* check whether all auto-mic pins are valid; setup indices if OK */
4602 static bool auto_mic_check_imux(struct hda_codec
*codec
)
4604 struct hda_gen_spec
*spec
= codec
->spec
;
4605 const struct hda_input_mux
*imux
;
4608 imux
= &spec
->input_mux
;
4609 for (i
= 0; i
< spec
->am_num_entries
; i
++) {
4610 spec
->am_entry
[i
].idx
=
4611 find_idx_in_nid_list(spec
->am_entry
[i
].pin
,
4612 spec
->imux_pins
, imux
->num_items
);
4613 if (spec
->am_entry
[i
].idx
< 0)
4614 return false; /* no corresponding imux */
4617 /* we don't need the jack detection for the first pin */
4618 for (i
= 1; i
< spec
->am_num_entries
; i
++)
4619 snd_hda_jack_detect_enable_callback(codec
,
4620 spec
->am_entry
[i
].pin
,
4621 call_mic_autoswitch
);
4625 static int compare_attr(const void *ap
, const void *bp
)
4627 const struct automic_entry
*a
= ap
;
4628 const struct automic_entry
*b
= bp
;
4629 return (int)(a
->attr
- b
->attr
);
4633 * Check the availability of auto-mic switch;
4634 * Set up if really supported
4636 static int check_auto_mic_availability(struct hda_codec
*codec
)
4638 struct hda_gen_spec
*spec
= codec
->spec
;
4639 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4643 if (spec
->suppress_auto_mic
)
4648 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
4649 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
4651 attr
= snd_hda_codec_get_pincfg(codec
, nid
);
4652 attr
= snd_hda_get_input_pin_attr(attr
);
4653 if (types
& (1 << attr
))
4654 return 0; /* already occupied */
4656 case INPUT_PIN_ATTR_INT
:
4657 if (cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
4658 return 0; /* invalid type */
4660 case INPUT_PIN_ATTR_UNUSED
:
4661 return 0; /* invalid entry */
4663 if (cfg
->inputs
[i
].type
> AUTO_PIN_LINE_IN
)
4664 return 0; /* invalid type */
4665 if (!spec
->line_in_auto_switch
&&
4666 cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
4667 return 0; /* only mic is allowed */
4668 if (!is_jack_detectable(codec
, nid
))
4669 return 0; /* no unsol support */
4672 if (num_pins
>= MAX_AUTO_MIC_PINS
)
4674 types
|= (1 << attr
);
4675 spec
->am_entry
[num_pins
].pin
= nid
;
4676 spec
->am_entry
[num_pins
].attr
= attr
;
4683 spec
->am_num_entries
= num_pins
;
4684 /* sort the am_entry in the order of attr so that the pin with a
4685 * higher attr will be selected when the jack is plugged.
4687 sort(spec
->am_entry
, num_pins
, sizeof(spec
->am_entry
[0]),
4688 compare_attr
, NULL
);
4690 if (!auto_mic_check_imux(codec
))
4694 spec
->num_adc_nids
= 1;
4695 spec
->cur_mux
[0] = spec
->am_entry
[0].idx
;
4696 codec_dbg(codec
, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4697 spec
->am_entry
[0].pin
,
4698 spec
->am_entry
[1].pin
,
4699 spec
->am_entry
[2].pin
);
4705 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4707 * @codec: the HDA codec
4708 * @nid: NID to evalute
4709 * @power_state: target power state
4711 unsigned int snd_hda_gen_path_power_filter(struct hda_codec
*codec
,
4713 unsigned int power_state
)
4715 struct hda_gen_spec
*spec
= codec
->spec
;
4717 if (!spec
->power_down_unused
&& !codec
->power_save_node
)
4719 if (power_state
!= AC_PWRST_D0
|| nid
== codec
->core
.afg
)
4721 if (get_wcaps_type(get_wcaps(codec
, nid
)) >= AC_WID_POWER
)
4723 if (is_active_nid_for_any(codec
, nid
))
4727 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter
);
4729 /* mute all aamix inputs initially; parse up to the first leaves */
4730 static void mute_all_mixer_nid(struct hda_codec
*codec
, hda_nid_t mix
)
4733 const hda_nid_t
*conn
;
4736 nums
= snd_hda_get_conn_list(codec
, mix
, &conn
);
4737 has_amp
= nid_has_mute(codec
, mix
, HDA_INPUT
);
4738 for (i
= 0; i
< nums
; i
++) {
4740 update_amp(codec
, mix
, HDA_INPUT
, i
,
4741 0xff, HDA_AMP_MUTE
);
4742 else if (nid_has_volume(codec
, conn
[i
], HDA_OUTPUT
))
4743 update_amp(codec
, conn
[i
], HDA_OUTPUT
, 0,
4744 0xff, HDA_AMP_MUTE
);
4749 * snd_hda_gen_stream_pm - Stream power management callback
4750 * @codec: the HDA codec
4751 * @nid: audio widget
4752 * @on: power on/off flag
4754 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
4756 void snd_hda_gen_stream_pm(struct hda_codec
*codec
, hda_nid_t nid
, bool on
)
4758 if (codec
->power_save_node
)
4759 set_path_power(codec
, nid
, -1, on
);
4761 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm
);
4764 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4765 * set up the hda_gen_spec
4766 * @codec: the HDA codec
4767 * @cfg: Parsed pin configuration
4769 * return 1 if successful, 0 if the proper config is not found,
4770 * or a negative error code
4772 int snd_hda_gen_parse_auto_config(struct hda_codec
*codec
,
4773 struct auto_pin_cfg
*cfg
)
4775 struct hda_gen_spec
*spec
= codec
->spec
;
4778 parse_user_hints(codec
);
4780 if (spec
->mixer_nid
&& !spec
->mixer_merge_nid
)
4781 spec
->mixer_merge_nid
= spec
->mixer_nid
;
4783 if (cfg
!= &spec
->autocfg
) {
4784 spec
->autocfg
= *cfg
;
4785 cfg
= &spec
->autocfg
;
4788 if (!spec
->main_out_badness
)
4789 spec
->main_out_badness
= &hda_main_out_badness
;
4790 if (!spec
->extra_out_badness
)
4791 spec
->extra_out_badness
= &hda_extra_out_badness
;
4793 fill_all_dac_nids(codec
);
4795 if (!cfg
->line_outs
) {
4796 if (cfg
->dig_outs
|| cfg
->dig_in_pin
) {
4797 spec
->multiout
.max_channels
= 2;
4798 spec
->no_analog
= 1;
4801 if (!cfg
->num_inputs
&& !cfg
->dig_in_pin
)
4802 return 0; /* can't find valid BIOS pin config */
4805 if (!spec
->no_primary_hp
&&
4806 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
&&
4807 cfg
->line_outs
<= cfg
->hp_outs
) {
4808 /* use HP as primary out */
4809 cfg
->speaker_outs
= cfg
->line_outs
;
4810 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
4811 sizeof(cfg
->speaker_pins
));
4812 cfg
->line_outs
= cfg
->hp_outs
;
4813 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
, sizeof(cfg
->hp_pins
));
4815 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
4816 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
4819 err
= parse_output_paths(codec
);
4822 err
= create_multi_channel_mode(codec
);
4825 err
= create_multi_out_ctls(codec
, cfg
);
4828 err
= create_hp_out_ctls(codec
);
4831 err
= create_speaker_out_ctls(codec
);
4834 err
= create_indep_hp_ctls(codec
);
4837 err
= create_loopback_mixing_ctl(codec
);
4840 err
= create_hp_mic(codec
);
4843 err
= create_input_ctls(codec
);
4847 /* add power-down pin callbacks at first */
4848 add_all_pin_power_ctls(codec
, false);
4850 spec
->const_channel_count
= spec
->ext_channel_count
;
4851 /* check the multiple speaker and headphone pins */
4852 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
4853 spec
->const_channel_count
= max(spec
->const_channel_count
,
4854 cfg
->speaker_outs
* 2);
4855 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
4856 spec
->const_channel_count
= max(spec
->const_channel_count
,
4858 spec
->multiout
.max_channels
= max(spec
->ext_channel_count
,
4859 spec
->const_channel_count
);
4861 err
= check_auto_mute_availability(codec
);
4865 err
= check_dyn_adc_switch(codec
);
4869 err
= check_auto_mic_availability(codec
);
4873 /* add stereo mix if available and not enabled yet */
4874 if (!spec
->auto_mic
&& spec
->mixer_nid
&&
4875 spec
->add_stereo_mix_input
== HDA_HINT_STEREO_MIX_AUTO
&&
4876 spec
->input_mux
.num_items
> 1) {
4877 err
= parse_capture_source(codec
, spec
->mixer_nid
,
4878 CFG_IDX_MIX
, spec
->num_all_adcs
,
4885 err
= create_capture_mixers(codec
);
4889 err
= parse_mic_boost(codec
);
4893 /* create "Headphone Mic Jack Mode" if no input selection is
4894 * available (or user specifies add_jack_modes hint)
4896 if (spec
->hp_mic_pin
&&
4897 (spec
->auto_mic
|| spec
->input_mux
.num_items
== 1 ||
4898 spec
->add_jack_modes
)) {
4899 err
= create_hp_mic_jack_mode(codec
, spec
->hp_mic_pin
);
4904 if (spec
->add_jack_modes
) {
4905 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
4906 err
= create_out_jack_modes(codec
, cfg
->line_outs
,
4907 cfg
->line_out_pins
);
4911 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
) {
4912 err
= create_out_jack_modes(codec
, cfg
->hp_outs
,
4919 /* add power-up pin callbacks at last */
4920 add_all_pin_power_ctls(codec
, true);
4922 /* mute all aamix input initially */
4923 if (spec
->mixer_nid
)
4924 mute_all_mixer_nid(codec
, spec
->mixer_nid
);
4927 parse_digital(codec
);
4929 if (spec
->power_down_unused
|| codec
->power_save_node
) {
4930 if (!codec
->power_filter
)
4931 codec
->power_filter
= snd_hda_gen_path_power_filter
;
4932 if (!codec
->patch_ops
.stream_pm
)
4933 codec
->patch_ops
.stream_pm
= snd_hda_gen_stream_pm
;
4936 if (!spec
->no_analog
&& spec
->beep_nid
) {
4937 err
= snd_hda_attach_beep_device(codec
, spec
->beep_nid
);
4940 if (codec
->beep
&& codec
->power_save_node
) {
4941 err
= add_fake_beep_paths(codec
);
4944 codec
->beep
->power_hook
= beep_power_hook
;
4950 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config
);
4954 * Build control elements
4957 /* slave controls for virtual master */
4958 static const char * const slave_pfxs
[] = {
4959 "Front", "Surround", "Center", "LFE", "Side",
4960 "Headphone", "Speaker", "Mono", "Line Out",
4961 "CLFE", "Bass Speaker", "PCM",
4962 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
4963 "Headphone Front", "Headphone Surround", "Headphone CLFE",
4964 "Headphone Side", "Headphone+LO", "Speaker+LO",
4969 * snd_hda_gen_build_controls - Build controls from the parsed results
4970 * @codec: the HDA codec
4972 * Pass this to build_controls patch_ops.
4974 int snd_hda_gen_build_controls(struct hda_codec
*codec
)
4976 struct hda_gen_spec
*spec
= codec
->spec
;
4979 if (spec
->kctls
.used
) {
4980 err
= snd_hda_add_new_ctls(codec
, spec
->kctls
.list
);
4985 if (spec
->multiout
.dig_out_nid
) {
4986 err
= snd_hda_create_dig_out_ctls(codec
,
4987 spec
->multiout
.dig_out_nid
,
4988 spec
->multiout
.dig_out_nid
,
4989 spec
->pcm_rec
[1]->pcm_type
);
4992 if (!spec
->no_analog
) {
4993 err
= snd_hda_create_spdif_share_sw(codec
,
4997 spec
->multiout
.share_spdif
= 1;
5000 if (spec
->dig_in_nid
) {
5001 err
= snd_hda_create_spdif_in_ctls(codec
, spec
->dig_in_nid
);
5006 /* if we have no master control, let's create it */
5007 if (!spec
->no_analog
&&
5008 !snd_hda_find_mixer_ctl(codec
, "Master Playback Volume")) {
5009 err
= snd_hda_add_vmaster(codec
, "Master Playback Volume",
5010 spec
->vmaster_tlv
, slave_pfxs
,
5015 if (!spec
->no_analog
&&
5016 !snd_hda_find_mixer_ctl(codec
, "Master Playback Switch")) {
5017 err
= __snd_hda_add_vmaster(codec
, "Master Playback Switch",
5020 true, &spec
->vmaster_mute
.sw_kctl
);
5023 if (spec
->vmaster_mute
.hook
) {
5024 snd_hda_add_vmaster_hook(codec
, &spec
->vmaster_mute
,
5025 spec
->vmaster_mute_enum
);
5026 snd_hda_sync_vmaster_hook(&spec
->vmaster_mute
);
5030 free_kctls(spec
); /* no longer needed */
5032 err
= snd_hda_jack_add_kctls(codec
, &spec
->autocfg
);
5038 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls
);
5045 static void call_pcm_playback_hook(struct hda_pcm_stream
*hinfo
,
5046 struct hda_codec
*codec
,
5047 struct snd_pcm_substream
*substream
,
5050 struct hda_gen_spec
*spec
= codec
->spec
;
5051 if (spec
->pcm_playback_hook
)
5052 spec
->pcm_playback_hook(hinfo
, codec
, substream
, action
);
5055 static void call_pcm_capture_hook(struct hda_pcm_stream
*hinfo
,
5056 struct hda_codec
*codec
,
5057 struct snd_pcm_substream
*substream
,
5060 struct hda_gen_spec
*spec
= codec
->spec
;
5061 if (spec
->pcm_capture_hook
)
5062 spec
->pcm_capture_hook(hinfo
, codec
, substream
, action
);
5066 * Analog playback callbacks
5068 static int playback_pcm_open(struct hda_pcm_stream
*hinfo
,
5069 struct hda_codec
*codec
,
5070 struct snd_pcm_substream
*substream
)
5072 struct hda_gen_spec
*spec
= codec
->spec
;
5075 mutex_lock(&spec
->pcm_mutex
);
5076 err
= snd_hda_multi_out_analog_open(codec
,
5077 &spec
->multiout
, substream
,
5080 spec
->active_streams
|= 1 << STREAM_MULTI_OUT
;
5081 call_pcm_playback_hook(hinfo
, codec
, substream
,
5082 HDA_GEN_PCM_ACT_OPEN
);
5084 mutex_unlock(&spec
->pcm_mutex
);
5088 static int playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5089 struct hda_codec
*codec
,
5090 unsigned int stream_tag
,
5091 unsigned int format
,
5092 struct snd_pcm_substream
*substream
)
5094 struct hda_gen_spec
*spec
= codec
->spec
;
5097 err
= snd_hda_multi_out_analog_prepare(codec
, &spec
->multiout
,
5098 stream_tag
, format
, substream
);
5100 call_pcm_playback_hook(hinfo
, codec
, substream
,
5101 HDA_GEN_PCM_ACT_PREPARE
);
5105 static int playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5106 struct hda_codec
*codec
,
5107 struct snd_pcm_substream
*substream
)
5109 struct hda_gen_spec
*spec
= codec
->spec
;
5112 err
= snd_hda_multi_out_analog_cleanup(codec
, &spec
->multiout
);
5114 call_pcm_playback_hook(hinfo
, codec
, substream
,
5115 HDA_GEN_PCM_ACT_CLEANUP
);
5119 static int playback_pcm_close(struct hda_pcm_stream
*hinfo
,
5120 struct hda_codec
*codec
,
5121 struct snd_pcm_substream
*substream
)
5123 struct hda_gen_spec
*spec
= codec
->spec
;
5124 mutex_lock(&spec
->pcm_mutex
);
5125 spec
->active_streams
&= ~(1 << STREAM_MULTI_OUT
);
5126 call_pcm_playback_hook(hinfo
, codec
, substream
,
5127 HDA_GEN_PCM_ACT_CLOSE
);
5128 mutex_unlock(&spec
->pcm_mutex
);
5132 static int capture_pcm_open(struct hda_pcm_stream
*hinfo
,
5133 struct hda_codec
*codec
,
5134 struct snd_pcm_substream
*substream
)
5136 call_pcm_capture_hook(hinfo
, codec
, substream
, HDA_GEN_PCM_ACT_OPEN
);
5140 static int capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5141 struct hda_codec
*codec
,
5142 unsigned int stream_tag
,
5143 unsigned int format
,
5144 struct snd_pcm_substream
*substream
)
5146 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
5147 call_pcm_capture_hook(hinfo
, codec
, substream
,
5148 HDA_GEN_PCM_ACT_PREPARE
);
5152 static int capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5153 struct hda_codec
*codec
,
5154 struct snd_pcm_substream
*substream
)
5156 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
5157 call_pcm_capture_hook(hinfo
, codec
, substream
,
5158 HDA_GEN_PCM_ACT_CLEANUP
);
5162 static int capture_pcm_close(struct hda_pcm_stream
*hinfo
,
5163 struct hda_codec
*codec
,
5164 struct snd_pcm_substream
*substream
)
5166 call_pcm_capture_hook(hinfo
, codec
, substream
, HDA_GEN_PCM_ACT_CLOSE
);
5170 static int alt_playback_pcm_open(struct hda_pcm_stream
*hinfo
,
5171 struct hda_codec
*codec
,
5172 struct snd_pcm_substream
*substream
)
5174 struct hda_gen_spec
*spec
= codec
->spec
;
5177 mutex_lock(&spec
->pcm_mutex
);
5178 if (spec
->indep_hp
&& !spec
->indep_hp_enabled
)
5181 spec
->active_streams
|= 1 << STREAM_INDEP_HP
;
5182 call_pcm_playback_hook(hinfo
, codec
, substream
,
5183 HDA_GEN_PCM_ACT_OPEN
);
5184 mutex_unlock(&spec
->pcm_mutex
);
5188 static int alt_playback_pcm_close(struct hda_pcm_stream
*hinfo
,
5189 struct hda_codec
*codec
,
5190 struct snd_pcm_substream
*substream
)
5192 struct hda_gen_spec
*spec
= codec
->spec
;
5193 mutex_lock(&spec
->pcm_mutex
);
5194 spec
->active_streams
&= ~(1 << STREAM_INDEP_HP
);
5195 call_pcm_playback_hook(hinfo
, codec
, substream
,
5196 HDA_GEN_PCM_ACT_CLOSE
);
5197 mutex_unlock(&spec
->pcm_mutex
);
5201 static int alt_playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5202 struct hda_codec
*codec
,
5203 unsigned int stream_tag
,
5204 unsigned int format
,
5205 struct snd_pcm_substream
*substream
)
5207 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
5208 call_pcm_playback_hook(hinfo
, codec
, substream
,
5209 HDA_GEN_PCM_ACT_PREPARE
);
5213 static int alt_playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5214 struct hda_codec
*codec
,
5215 struct snd_pcm_substream
*substream
)
5217 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
5218 call_pcm_playback_hook(hinfo
, codec
, substream
,
5219 HDA_GEN_PCM_ACT_CLEANUP
);
5226 static int dig_playback_pcm_open(struct hda_pcm_stream
*hinfo
,
5227 struct hda_codec
*codec
,
5228 struct snd_pcm_substream
*substream
)
5230 struct hda_gen_spec
*spec
= codec
->spec
;
5231 return snd_hda_multi_out_dig_open(codec
, &spec
->multiout
);
5234 static int dig_playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5235 struct hda_codec
*codec
,
5236 unsigned int stream_tag
,
5237 unsigned int format
,
5238 struct snd_pcm_substream
*substream
)
5240 struct hda_gen_spec
*spec
= codec
->spec
;
5241 return snd_hda_multi_out_dig_prepare(codec
, &spec
->multiout
,
5242 stream_tag
, format
, substream
);
5245 static int dig_playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5246 struct hda_codec
*codec
,
5247 struct snd_pcm_substream
*substream
)
5249 struct hda_gen_spec
*spec
= codec
->spec
;
5250 return snd_hda_multi_out_dig_cleanup(codec
, &spec
->multiout
);
5253 static int dig_playback_pcm_close(struct hda_pcm_stream
*hinfo
,
5254 struct hda_codec
*codec
,
5255 struct snd_pcm_substream
*substream
)
5257 struct hda_gen_spec
*spec
= codec
->spec
;
5258 return snd_hda_multi_out_dig_close(codec
, &spec
->multiout
);
5264 #define alt_capture_pcm_open capture_pcm_open
5265 #define alt_capture_pcm_close capture_pcm_close
5267 static int alt_capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5268 struct hda_codec
*codec
,
5269 unsigned int stream_tag
,
5270 unsigned int format
,
5271 struct snd_pcm_substream
*substream
)
5273 struct hda_gen_spec
*spec
= codec
->spec
;
5275 snd_hda_codec_setup_stream(codec
, spec
->adc_nids
[substream
->number
+ 1],
5276 stream_tag
, 0, format
);
5277 call_pcm_capture_hook(hinfo
, codec
, substream
,
5278 HDA_GEN_PCM_ACT_PREPARE
);
5282 static int alt_capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5283 struct hda_codec
*codec
,
5284 struct snd_pcm_substream
*substream
)
5286 struct hda_gen_spec
*spec
= codec
->spec
;
5288 snd_hda_codec_cleanup_stream(codec
,
5289 spec
->adc_nids
[substream
->number
+ 1]);
5290 call_pcm_capture_hook(hinfo
, codec
, substream
,
5291 HDA_GEN_PCM_ACT_CLEANUP
);
5297 static const struct hda_pcm_stream pcm_analog_playback
= {
5301 /* NID is set in build_pcms */
5303 .open
= playback_pcm_open
,
5304 .close
= playback_pcm_close
,
5305 .prepare
= playback_pcm_prepare
,
5306 .cleanup
= playback_pcm_cleanup
5310 static const struct hda_pcm_stream pcm_analog_capture
= {
5314 /* NID is set in build_pcms */
5316 .open
= capture_pcm_open
,
5317 .close
= capture_pcm_close
,
5318 .prepare
= capture_pcm_prepare
,
5319 .cleanup
= capture_pcm_cleanup
5323 static const struct hda_pcm_stream pcm_analog_alt_playback
= {
5327 /* NID is set in build_pcms */
5329 .open
= alt_playback_pcm_open
,
5330 .close
= alt_playback_pcm_close
,
5331 .prepare
= alt_playback_pcm_prepare
,
5332 .cleanup
= alt_playback_pcm_cleanup
5336 static const struct hda_pcm_stream pcm_analog_alt_capture
= {
5337 .substreams
= 2, /* can be overridden */
5340 /* NID is set in build_pcms */
5342 .open
= alt_capture_pcm_open
,
5343 .close
= alt_capture_pcm_close
,
5344 .prepare
= alt_capture_pcm_prepare
,
5345 .cleanup
= alt_capture_pcm_cleanup
5349 static const struct hda_pcm_stream pcm_digital_playback
= {
5353 /* NID is set in build_pcms */
5355 .open
= dig_playback_pcm_open
,
5356 .close
= dig_playback_pcm_close
,
5357 .prepare
= dig_playback_pcm_prepare
,
5358 .cleanup
= dig_playback_pcm_cleanup
5362 static const struct hda_pcm_stream pcm_digital_capture
= {
5366 /* NID is set in build_pcms */
5369 /* Used by build_pcms to flag that a PCM has no playback stream */
5370 static const struct hda_pcm_stream pcm_null_stream
= {
5377 * dynamic changing ADC PCM streams
5379 static bool dyn_adc_pcm_resetup(struct hda_codec
*codec
, int cur
)
5381 struct hda_gen_spec
*spec
= codec
->spec
;
5382 hda_nid_t new_adc
= spec
->adc_nids
[spec
->dyn_adc_idx
[cur
]];
5384 if (spec
->cur_adc
&& spec
->cur_adc
!= new_adc
) {
5385 /* stream is running, let's swap the current ADC */
5386 __snd_hda_codec_cleanup_stream(codec
, spec
->cur_adc
, 1);
5387 spec
->cur_adc
= new_adc
;
5388 snd_hda_codec_setup_stream(codec
, new_adc
,
5389 spec
->cur_adc_stream_tag
, 0,
5390 spec
->cur_adc_format
);
5396 /* analog capture with dynamic dual-adc changes */
5397 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5398 struct hda_codec
*codec
,
5399 unsigned int stream_tag
,
5400 unsigned int format
,
5401 struct snd_pcm_substream
*substream
)
5403 struct hda_gen_spec
*spec
= codec
->spec
;
5404 spec
->cur_adc
= spec
->adc_nids
[spec
->dyn_adc_idx
[spec
->cur_mux
[0]]];
5405 spec
->cur_adc_stream_tag
= stream_tag
;
5406 spec
->cur_adc_format
= format
;
5407 snd_hda_codec_setup_stream(codec
, spec
->cur_adc
, stream_tag
, 0, format
);
5411 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5412 struct hda_codec
*codec
,
5413 struct snd_pcm_substream
*substream
)
5415 struct hda_gen_spec
*spec
= codec
->spec
;
5416 snd_hda_codec_cleanup_stream(codec
, spec
->cur_adc
);
5421 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture
= {
5425 .nid
= 0, /* fill later */
5427 .prepare
= dyn_adc_capture_pcm_prepare
,
5428 .cleanup
= dyn_adc_capture_pcm_cleanup
5432 static void fill_pcm_stream_name(char *str
, size_t len
, const char *sfx
,
5433 const char *chip_name
)
5439 strlcpy(str
, chip_name
, len
);
5441 /* drop non-alnum chars after a space */
5442 for (p
= strchr(str
, ' '); p
; p
= strchr(p
+ 1, ' ')) {
5443 if (!isalnum(p
[1])) {
5448 strlcat(str
, sfx
, len
);
5451 /* copy PCM stream info from @default_str, and override non-NULL entries
5452 * from @spec_str and @nid
5454 static void setup_pcm_stream(struct hda_pcm_stream
*str
,
5455 const struct hda_pcm_stream
*default_str
,
5456 const struct hda_pcm_stream
*spec_str
,
5459 *str
= *default_str
;
5463 if (spec_str
->substreams
)
5464 str
->substreams
= spec_str
->substreams
;
5465 if (spec_str
->channels_min
)
5466 str
->channels_min
= spec_str
->channels_min
;
5467 if (spec_str
->channels_max
)
5468 str
->channels_max
= spec_str
->channels_max
;
5469 if (spec_str
->rates
)
5470 str
->rates
= spec_str
->rates
;
5471 if (spec_str
->formats
)
5472 str
->formats
= spec_str
->formats
;
5473 if (spec_str
->maxbps
)
5474 str
->maxbps
= spec_str
->maxbps
;
5479 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5480 * @codec: the HDA codec
5482 * Pass this to build_pcms patch_ops.
5484 int snd_hda_gen_build_pcms(struct hda_codec
*codec
)
5486 struct hda_gen_spec
*spec
= codec
->spec
;
5487 struct hda_pcm
*info
;
5488 bool have_multi_adcs
;
5490 if (spec
->no_analog
)
5493 fill_pcm_stream_name(spec
->stream_name_analog
,
5494 sizeof(spec
->stream_name_analog
),
5495 " Analog", codec
->core
.chip_name
);
5496 info
= snd_hda_codec_pcm_new(codec
, "%s", spec
->stream_name_analog
);
5499 spec
->pcm_rec
[0] = info
;
5501 if (spec
->multiout
.num_dacs
> 0) {
5502 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
],
5503 &pcm_analog_playback
,
5504 spec
->stream_analog_playback
,
5505 spec
->multiout
.dac_nids
[0]);
5506 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].channels_max
=
5507 spec
->multiout
.max_channels
;
5508 if (spec
->autocfg
.line_out_type
== AUTO_PIN_SPEAKER_OUT
&&
5509 spec
->autocfg
.line_outs
== 2)
5510 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].chmap
=
5513 if (spec
->num_adc_nids
) {
5514 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_CAPTURE
],
5515 (spec
->dyn_adc_switch
?
5516 &dyn_adc_pcm_analog_capture
: &pcm_analog_capture
),
5517 spec
->stream_analog_capture
,
5522 /* SPDIF for stream index #1 */
5523 if (spec
->multiout
.dig_out_nid
|| spec
->dig_in_nid
) {
5524 fill_pcm_stream_name(spec
->stream_name_digital
,
5525 sizeof(spec
->stream_name_digital
),
5526 " Digital", codec
->core
.chip_name
);
5527 info
= snd_hda_codec_pcm_new(codec
, "%s",
5528 spec
->stream_name_digital
);
5531 codec
->slave_dig_outs
= spec
->multiout
.slave_dig_outs
;
5532 spec
->pcm_rec
[1] = info
;
5533 if (spec
->dig_out_type
)
5534 info
->pcm_type
= spec
->dig_out_type
;
5536 info
->pcm_type
= HDA_PCM_TYPE_SPDIF
;
5537 if (spec
->multiout
.dig_out_nid
)
5538 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
],
5539 &pcm_digital_playback
,
5540 spec
->stream_digital_playback
,
5541 spec
->multiout
.dig_out_nid
);
5542 if (spec
->dig_in_nid
)
5543 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_CAPTURE
],
5544 &pcm_digital_capture
,
5545 spec
->stream_digital_capture
,
5549 if (spec
->no_analog
)
5552 /* If the use of more than one ADC is requested for the current
5553 * model, configure a second analog capture-only PCM.
5555 have_multi_adcs
= (spec
->num_adc_nids
> 1) &&
5556 !spec
->dyn_adc_switch
&& !spec
->auto_mic
;
5557 /* Additional Analaog capture for index #2 */
5558 if (spec
->alt_dac_nid
|| have_multi_adcs
) {
5559 fill_pcm_stream_name(spec
->stream_name_alt_analog
,
5560 sizeof(spec
->stream_name_alt_analog
),
5561 " Alt Analog", codec
->core
.chip_name
);
5562 info
= snd_hda_codec_pcm_new(codec
, "%s",
5563 spec
->stream_name_alt_analog
);
5566 spec
->pcm_rec
[2] = info
;
5567 if (spec
->alt_dac_nid
)
5568 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
],
5569 &pcm_analog_alt_playback
,
5570 spec
->stream_analog_alt_playback
,
5573 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
],
5574 &pcm_null_stream
, NULL
, 0);
5575 if (have_multi_adcs
) {
5576 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_CAPTURE
],
5577 &pcm_analog_alt_capture
,
5578 spec
->stream_analog_alt_capture
,
5580 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].substreams
=
5581 spec
->num_adc_nids
- 1;
5583 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_CAPTURE
],
5584 &pcm_null_stream
, NULL
, 0);
5590 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms
);
5594 * Standard auto-parser initializations
5597 /* configure the given path as a proper output */
5598 static void set_output_and_unmute(struct hda_codec
*codec
, int path_idx
)
5600 struct nid_path
*path
;
5603 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
5604 if (!path
|| !path
->depth
)
5606 pin
= path
->path
[path
->depth
- 1];
5607 restore_pin_ctl(codec
, pin
);
5608 snd_hda_activate_path(codec
, path
, path
->active
,
5609 aamix_default(codec
->spec
));
5610 set_pin_eapd(codec
, pin
, path
->active
);
5613 /* initialize primary output paths */
5614 static void init_multi_out(struct hda_codec
*codec
)
5616 struct hda_gen_spec
*spec
= codec
->spec
;
5619 for (i
= 0; i
< spec
->autocfg
.line_outs
; i
++)
5620 set_output_and_unmute(codec
, spec
->out_paths
[i
]);
5624 static void __init_extra_out(struct hda_codec
*codec
, int num_outs
, int *paths
)
5628 for (i
= 0; i
< num_outs
; i
++)
5629 set_output_and_unmute(codec
, paths
[i
]);
5632 /* initialize hp and speaker paths */
5633 static void init_extra_out(struct hda_codec
*codec
)
5635 struct hda_gen_spec
*spec
= codec
->spec
;
5637 if (spec
->autocfg
.line_out_type
!= AUTO_PIN_HP_OUT
)
5638 __init_extra_out(codec
, spec
->autocfg
.hp_outs
, spec
->hp_paths
);
5639 if (spec
->autocfg
.line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
5640 __init_extra_out(codec
, spec
->autocfg
.speaker_outs
,
5641 spec
->speaker_paths
);
5644 /* initialize multi-io paths */
5645 static void init_multi_io(struct hda_codec
*codec
)
5647 struct hda_gen_spec
*spec
= codec
->spec
;
5650 for (i
= 0; i
< spec
->multi_ios
; i
++) {
5651 hda_nid_t pin
= spec
->multi_io
[i
].pin
;
5652 struct nid_path
*path
;
5653 path
= get_multiio_path(codec
, i
);
5656 if (!spec
->multi_io
[i
].ctl_in
)
5657 spec
->multi_io
[i
].ctl_in
=
5658 snd_hda_codec_get_pin_target(codec
, pin
);
5659 snd_hda_activate_path(codec
, path
, path
->active
,
5660 aamix_default(spec
));
5664 static void init_aamix_paths(struct hda_codec
*codec
)
5666 struct hda_gen_spec
*spec
= codec
->spec
;
5668 if (!spec
->have_aamix_ctl
)
5670 update_aamix_paths(codec
, spec
->aamix_mode
, spec
->out_paths
[0],
5671 spec
->aamix_out_paths
[0],
5672 spec
->autocfg
.line_out_type
);
5673 update_aamix_paths(codec
, spec
->aamix_mode
, spec
->hp_paths
[0],
5674 spec
->aamix_out_paths
[1],
5676 update_aamix_paths(codec
, spec
->aamix_mode
, spec
->speaker_paths
[0],
5677 spec
->aamix_out_paths
[2],
5678 AUTO_PIN_SPEAKER_OUT
);
5681 /* set up input pins and loopback paths */
5682 static void init_analog_input(struct hda_codec
*codec
)
5684 struct hda_gen_spec
*spec
= codec
->spec
;
5685 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
5688 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
5689 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
5690 if (is_input_pin(codec
, nid
))
5691 restore_pin_ctl(codec
, nid
);
5693 /* init loopback inputs */
5694 if (spec
->mixer_nid
) {
5695 resume_path_from_idx(codec
, spec
->loopback_paths
[i
]);
5696 resume_path_from_idx(codec
, spec
->loopback_merge_path
);
5701 /* initialize ADC paths */
5702 static void init_input_src(struct hda_codec
*codec
)
5704 struct hda_gen_spec
*spec
= codec
->spec
;
5705 struct hda_input_mux
*imux
= &spec
->input_mux
;
5706 struct nid_path
*path
;
5709 if (spec
->dyn_adc_switch
)
5712 nums
= spec
->num_adc_nids
;
5714 for (c
= 0; c
< nums
; c
++) {
5715 for (i
= 0; i
< imux
->num_items
; i
++) {
5716 path
= get_input_path(codec
, c
, i
);
5718 bool active
= path
->active
;
5719 if (i
== spec
->cur_mux
[c
])
5721 snd_hda_activate_path(codec
, path
, active
, false);
5725 update_hp_mic(codec
, c
, true);
5728 if (spec
->cap_sync_hook
)
5729 spec
->cap_sync_hook(codec
, NULL
, NULL
);
5732 /* set right pin controls for digital I/O */
5733 static void init_digital(struct hda_codec
*codec
)
5735 struct hda_gen_spec
*spec
= codec
->spec
;
5739 for (i
= 0; i
< spec
->autocfg
.dig_outs
; i
++)
5740 set_output_and_unmute(codec
, spec
->digout_paths
[i
]);
5741 pin
= spec
->autocfg
.dig_in_pin
;
5743 restore_pin_ctl(codec
, pin
);
5744 resume_path_from_idx(codec
, spec
->digin_path
);
5748 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5749 * invalid unsol tags by some reason
5751 static void clear_unsol_on_unused_pins(struct hda_codec
*codec
)
5755 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
5756 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
5757 hda_nid_t nid
= pin
->nid
;
5758 if (is_jack_detectable(codec
, nid
) &&
5759 !snd_hda_jack_tbl_get(codec
, nid
))
5760 snd_hda_codec_update_cache(codec
, nid
, 0,
5761 AC_VERB_SET_UNSOLICITED_ENABLE
, 0);
5766 * snd_hda_gen_init - initialize the generic spec
5767 * @codec: the HDA codec
5769 * This can be put as patch_ops init function.
5771 int snd_hda_gen_init(struct hda_codec
*codec
)
5773 struct hda_gen_spec
*spec
= codec
->spec
;
5775 if (spec
->init_hook
)
5776 spec
->init_hook(codec
);
5778 snd_hda_apply_verbs(codec
);
5780 init_multi_out(codec
);
5781 init_extra_out(codec
);
5782 init_multi_io(codec
);
5783 init_aamix_paths(codec
);
5784 init_analog_input(codec
);
5785 init_input_src(codec
);
5786 init_digital(codec
);
5788 clear_unsol_on_unused_pins(codec
);
5790 sync_all_pin_power_ctls(codec
);
5792 /* call init functions of standard auto-mute helpers */
5793 update_automute_all(codec
);
5795 regcache_sync(codec
->core
.regmap
);
5797 if (spec
->vmaster_mute
.sw_kctl
&& spec
->vmaster_mute
.hook
)
5798 snd_hda_sync_vmaster_hook(&spec
->vmaster_mute
);
5800 hda_call_check_power_status(codec
, 0x01);
5803 EXPORT_SYMBOL_GPL(snd_hda_gen_init
);
5806 * snd_hda_gen_free - free the generic spec
5807 * @codec: the HDA codec
5809 * This can be put as patch_ops free function.
5811 void snd_hda_gen_free(struct hda_codec
*codec
)
5813 snd_hda_apply_fixup(codec
, HDA_FIXUP_ACT_FREE
);
5814 snd_hda_gen_spec_free(codec
->spec
);
5818 EXPORT_SYMBOL_GPL(snd_hda_gen_free
);
5822 * snd_hda_gen_check_power_status - check the loopback power save state
5823 * @codec: the HDA codec
5824 * @nid: NID to inspect
5826 * This can be put as patch_ops check_power_status function.
5828 int snd_hda_gen_check_power_status(struct hda_codec
*codec
, hda_nid_t nid
)
5830 struct hda_gen_spec
*spec
= codec
->spec
;
5831 return snd_hda_check_amp_list_power(codec
, &spec
->loopback
, nid
);
5833 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status
);
5838 * the generic codec support
5841 static const struct hda_codec_ops generic_patch_ops
= {
5842 .build_controls
= snd_hda_gen_build_controls
,
5843 .build_pcms
= snd_hda_gen_build_pcms
,
5844 .init
= snd_hda_gen_init
,
5845 .free
= snd_hda_gen_free
,
5846 .unsol_event
= snd_hda_jack_unsol_event
,
5848 .check_power_status
= snd_hda_gen_check_power_status
,
5853 * snd_hda_parse_generic_codec - Generic codec parser
5854 * @codec: the HDA codec
5856 static int snd_hda_parse_generic_codec(struct hda_codec
*codec
)
5858 struct hda_gen_spec
*spec
;
5861 spec
= kzalloc(sizeof(*spec
), GFP_KERNEL
);
5864 snd_hda_gen_spec_init(spec
);
5867 err
= snd_hda_parse_pin_defcfg(codec
, &spec
->autocfg
, NULL
, 0);
5871 err
= snd_hda_gen_parse_auto_config(codec
, &spec
->autocfg
);
5875 codec
->patch_ops
= generic_patch_ops
;
5879 snd_hda_gen_free(codec
);
5883 static const struct hda_codec_preset snd_hda_preset_generic
[] = {
5884 { .id
= HDA_CODEC_ID_GENERIC
, .patch
= snd_hda_parse_generic_codec
},
5888 static struct hda_codec_driver generic_driver
= {
5889 .preset
= snd_hda_preset_generic
,
5892 module_hda_codec_driver(generic_driver
);
5894 MODULE_LICENSE("GPL");
5895 MODULE_DESCRIPTION("Generic HD-audio codec parser");