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
|| idx
== -1 ||
684 /* check whether the NID is referred by any active paths */
685 #define is_active_nid_for_any(codec, nid) \
686 is_active_nid(codec, nid, HDA_OUTPUT, -1)
688 /* get the default amp value for the target state */
689 static int get_amp_val_to_activate(struct hda_codec
*codec
, hda_nid_t nid
,
690 int dir
, unsigned int caps
, bool enable
)
692 unsigned int val
= 0;
694 if (caps
& AC_AMPCAP_NUM_STEPS
) {
697 val
= (caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
;
699 if (caps
& (AC_AMPCAP_MUTE
| AC_AMPCAP_MIN_MUTE
)) {
706 /* is this a stereo widget or a stereo-to-mono mix? */
707 static bool is_stereo_amps(struct hda_codec
*codec
, hda_nid_t nid
, int dir
)
709 unsigned int wcaps
= get_wcaps(codec
, nid
);
712 if (wcaps
& AC_WCAP_STEREO
)
714 if (dir
!= HDA_INPUT
|| get_wcaps_type(wcaps
) != AC_WID_AUD_MIX
)
716 if (snd_hda_get_num_conns(codec
, nid
) != 1)
718 if (snd_hda_get_connections(codec
, nid
, &conn
, 1) < 0)
720 return !!(get_wcaps(codec
, conn
) & AC_WCAP_STEREO
);
723 /* initialize the amp value (only at the first time) */
724 static void init_amp(struct hda_codec
*codec
, hda_nid_t nid
, int dir
, int idx
)
726 unsigned int caps
= query_amp_caps(codec
, nid
, dir
);
727 int val
= get_amp_val_to_activate(codec
, nid
, dir
, caps
, false);
729 if (is_stereo_amps(codec
, nid
, dir
))
730 snd_hda_codec_amp_init_stereo(codec
, nid
, dir
, idx
, 0xff, val
);
732 snd_hda_codec_amp_init(codec
, nid
, 0, dir
, idx
, 0xff, val
);
735 /* update the amp, doing in stereo or mono depending on NID */
736 static int update_amp(struct hda_codec
*codec
, hda_nid_t nid
, int dir
, int idx
,
737 unsigned int mask
, unsigned int val
)
739 if (is_stereo_amps(codec
, nid
, dir
))
740 return snd_hda_codec_amp_stereo(codec
, nid
, dir
, idx
,
743 return snd_hda_codec_amp_update(codec
, nid
, 0, dir
, idx
,
747 /* calculate amp value mask we can modify;
748 * if the given amp is controlled by mixers, don't touch it
750 static unsigned int get_amp_mask_to_modify(struct hda_codec
*codec
,
751 hda_nid_t nid
, int dir
, int idx
,
754 unsigned int mask
= 0xff;
756 if (caps
& (AC_AMPCAP_MUTE
| AC_AMPCAP_MIN_MUTE
)) {
757 if (is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_MUTE_CTL
))
760 if (caps
& AC_AMPCAP_NUM_STEPS
) {
761 if (is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_VOL_CTL
) ||
762 is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_BOOST_CTL
))
768 static void activate_amp(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
769 int idx
, int idx_to_check
, bool enable
)
772 unsigned int mask
, val
;
774 caps
= query_amp_caps(codec
, nid
, dir
);
775 val
= get_amp_val_to_activate(codec
, nid
, dir
, caps
, enable
);
776 mask
= get_amp_mask_to_modify(codec
, nid
, dir
, idx_to_check
, caps
);
781 update_amp(codec
, nid
, dir
, idx
, mask
, val
);
784 static void check_and_activate_amp(struct hda_codec
*codec
, hda_nid_t nid
,
785 int dir
, int idx
, int idx_to_check
,
788 /* check whether the given amp is still used by others */
789 if (!enable
&& is_active_nid(codec
, nid
, dir
, idx_to_check
))
791 activate_amp(codec
, nid
, dir
, idx
, idx_to_check
, enable
);
794 static void activate_amp_out(struct hda_codec
*codec
, struct nid_path
*path
,
797 hda_nid_t nid
= path
->path
[i
];
798 init_amp(codec
, nid
, HDA_OUTPUT
, 0);
799 check_and_activate_amp(codec
, nid
, HDA_OUTPUT
, 0, 0, enable
);
802 static void activate_amp_in(struct hda_codec
*codec
, struct nid_path
*path
,
803 int i
, bool enable
, bool add_aamix
)
805 struct hda_gen_spec
*spec
= codec
->spec
;
806 const hda_nid_t
*conn
;
809 hda_nid_t nid
= path
->path
[i
];
811 nums
= snd_hda_get_conn_list(codec
, nid
, &conn
);
812 type
= get_wcaps_type(get_wcaps(codec
, nid
));
813 if (type
== AC_WID_PIN
||
814 (type
== AC_WID_AUD_IN
&& codec
->single_adc_amp
)) {
820 for (n
= 0; n
< nums
; n
++)
821 init_amp(codec
, nid
, HDA_INPUT
, n
);
823 /* here is a little bit tricky in comparison with activate_amp_out();
824 * when aa-mixer is available, we need to enable the path as well
826 for (n
= 0; n
< nums
; n
++) {
828 if (conn
[n
] != spec
->mixer_merge_nid
)
830 /* when aamix is disabled, force to off */
832 activate_amp(codec
, nid
, HDA_INPUT
, n
, n
, false);
836 check_and_activate_amp(codec
, nid
, HDA_INPUT
, n
, idx
, enable
);
840 /* sync power of each widget in the given path */
841 static hda_nid_t
path_power_update(struct hda_codec
*codec
,
842 struct nid_path
*path
,
843 bool allow_powerdown
)
845 hda_nid_t nid
, changed
= 0;
848 for (i
= 0; i
< path
->depth
; i
++) {
850 if (!(get_wcaps(codec
, nid
) & AC_WCAP_POWER
))
852 if (nid
== codec
->core
.afg
)
854 if (!allow_powerdown
|| is_active_nid_for_any(codec
, nid
))
858 power
= snd_hda_codec_read(codec
, nid
, 0,
859 AC_VERB_GET_POWER_STATE
, 0);
860 if (power
!= (state
| (state
<< 4))) {
861 snd_hda_codec_write(codec
, nid
, 0,
862 AC_VERB_SET_POWER_STATE
, state
);
864 /* all known codecs seem to be capable to handl
865 * widgets state even in D3, so far.
866 * if any new codecs need to restore the widget
867 * states after D0 transition, call the function
871 if (state
== AC_PWRST_D0
)
872 snd_hdac_regmap_sync_node(&codec
->core
, nid
);
879 /* do sync with the last power state change */
880 static void sync_power_state_change(struct hda_codec
*codec
, hda_nid_t nid
)
884 snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_POWER_STATE
, 0);
889 * snd_hda_activate_path - activate or deactivate the given path
890 * @codec: the HDA codec
891 * @path: the path to activate/deactivate
892 * @enable: flag to activate or not
893 * @add_aamix: enable the input from aamix NID
895 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
897 void snd_hda_activate_path(struct hda_codec
*codec
, struct nid_path
*path
,
898 bool enable
, bool add_aamix
)
900 struct hda_gen_spec
*spec
= codec
->spec
;
903 path
->active
= enable
;
905 /* make sure the widget is powered up */
906 if (enable
&& (spec
->power_down_unused
|| codec
->power_save_node
))
907 path_power_update(codec
, path
, codec
->power_save_node
);
909 for (i
= path
->depth
- 1; i
>= 0; i
--) {
910 hda_nid_t nid
= path
->path
[i
];
912 if (enable
&& path
->multi
[i
])
913 snd_hda_codec_update_cache(codec
, nid
, 0,
914 AC_VERB_SET_CONNECT_SEL
,
916 if (has_amp_in(codec
, path
, i
))
917 activate_amp_in(codec
, path
, i
, enable
, add_aamix
);
918 if (has_amp_out(codec
, path
, i
))
919 activate_amp_out(codec
, path
, i
, enable
);
922 EXPORT_SYMBOL_GPL(snd_hda_activate_path
);
924 /* if the given path is inactive, put widgets into D3 (only if suitable) */
925 static void path_power_down_sync(struct hda_codec
*codec
, struct nid_path
*path
)
927 struct hda_gen_spec
*spec
= codec
->spec
;
929 if (!(spec
->power_down_unused
|| codec
->power_save_node
) || path
->active
)
931 sync_power_state_change(codec
, path_power_update(codec
, path
, true));
934 /* turn on/off EAPD on the given pin */
935 static void set_pin_eapd(struct hda_codec
*codec
, hda_nid_t pin
, bool enable
)
937 struct hda_gen_spec
*spec
= codec
->spec
;
938 if (spec
->own_eapd_ctl
||
939 !(snd_hda_query_pin_caps(codec
, pin
) & AC_PINCAP_EAPD
))
941 if (spec
->keep_eapd_on
&& !enable
)
945 snd_hda_codec_update_cache(codec
, pin
, 0,
946 AC_VERB_SET_EAPD_BTLENABLE
,
947 enable
? 0x02 : 0x00);
950 /* re-initialize the path specified by the given path index */
951 static void resume_path_from_idx(struct hda_codec
*codec
, int path_idx
)
953 struct nid_path
*path
= snd_hda_get_path_from_idx(codec
, path_idx
);
955 snd_hda_activate_path(codec
, path
, path
->active
, false);
960 * Helper functions for creating mixer ctl elements
963 static int hda_gen_mixer_mute_put(struct snd_kcontrol
*kcontrol
,
964 struct snd_ctl_elem_value
*ucontrol
);
965 static int hda_gen_bind_mute_put(struct snd_kcontrol
*kcontrol
,
966 struct snd_ctl_elem_value
*ucontrol
);
973 static const struct snd_kcontrol_new control_templates
[] = {
974 HDA_CODEC_VOLUME(NULL
, 0, 0, 0),
975 /* only the put callback is replaced for handling the special mute */
977 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
978 .subdevice
= HDA_SUBDEV_AMP_FLAG
,
979 .info
= snd_hda_mixer_amp_switch_info
,
980 .get
= snd_hda_mixer_amp_switch_get
,
981 .put
= hda_gen_mixer_mute_put
, /* replaced */
982 .private_value
= HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
985 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
986 .info
= snd_hda_mixer_amp_switch_info
,
987 .get
= snd_hda_mixer_bind_switch_get
,
988 .put
= hda_gen_bind_mute_put
, /* replaced */
989 .private_value
= HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
993 /* add dynamic controls from template */
994 static struct snd_kcontrol_new
*
995 add_control(struct hda_gen_spec
*spec
, int type
, const char *name
,
996 int cidx
, unsigned long val
)
998 struct snd_kcontrol_new
*knew
;
1000 knew
= snd_hda_gen_add_kctl(spec
, name
, &control_templates
[type
]);
1004 if (get_amp_nid_(val
))
1005 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
1006 knew
->private_value
= val
;
1010 static int add_control_with_pfx(struct hda_gen_spec
*spec
, int type
,
1011 const char *pfx
, const char *dir
,
1012 const char *sfx
, int cidx
, unsigned long val
)
1014 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
1015 snprintf(name
, sizeof(name
), "%s %s %s", pfx
, dir
, sfx
);
1016 if (!add_control(spec
, type
, name
, cidx
, val
))
1021 #define add_pb_vol_ctrl(spec, type, pfx, val) \
1022 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1023 #define add_pb_sw_ctrl(spec, type, pfx, val) \
1024 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1025 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1026 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1027 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1028 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1030 static int add_vol_ctl(struct hda_codec
*codec
, const char *pfx
, int cidx
,
1031 unsigned int chs
, struct nid_path
*path
)
1036 val
= path
->ctls
[NID_PATH_VOL_CTL
];
1039 val
= amp_val_replace_channels(val
, chs
);
1040 return __add_pb_vol_ctrl(codec
->spec
, HDA_CTL_WIDGET_VOL
, pfx
, cidx
, val
);
1043 /* return the channel bits suitable for the given path->ctls[] */
1044 static int get_default_ch_nums(struct hda_codec
*codec
, struct nid_path
*path
,
1047 int chs
= 1; /* mono (left only) */
1049 hda_nid_t nid
= get_amp_nid_(path
->ctls
[type
]);
1050 if (nid
&& (get_wcaps(codec
, nid
) & AC_WCAP_STEREO
))
1051 chs
= 3; /* stereo */
1056 static int add_stereo_vol(struct hda_codec
*codec
, const char *pfx
, int cidx
,
1057 struct nid_path
*path
)
1059 int chs
= get_default_ch_nums(codec
, path
, NID_PATH_VOL_CTL
);
1060 return add_vol_ctl(codec
, pfx
, cidx
, chs
, path
);
1063 /* create a mute-switch for the given mixer widget;
1064 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1066 static int add_sw_ctl(struct hda_codec
*codec
, const char *pfx
, int cidx
,
1067 unsigned int chs
, struct nid_path
*path
)
1070 int type
= HDA_CTL_WIDGET_MUTE
;
1074 val
= path
->ctls
[NID_PATH_MUTE_CTL
];
1077 val
= amp_val_replace_channels(val
, chs
);
1078 if (get_amp_direction_(val
) == HDA_INPUT
) {
1079 hda_nid_t nid
= get_amp_nid_(val
);
1080 int nums
= snd_hda_get_num_conns(codec
, nid
);
1082 type
= HDA_CTL_BIND_MUTE
;
1086 return __add_pb_sw_ctrl(codec
->spec
, type
, pfx
, cidx
, val
);
1089 static int add_stereo_sw(struct hda_codec
*codec
, const char *pfx
,
1090 int cidx
, struct nid_path
*path
)
1092 int chs
= get_default_ch_nums(codec
, path
, NID_PATH_MUTE_CTL
);
1093 return add_sw_ctl(codec
, pfx
, cidx
, chs
, path
);
1096 /* playback mute control with the software mute bit check */
1097 static void sync_auto_mute_bits(struct snd_kcontrol
*kcontrol
,
1098 struct snd_ctl_elem_value
*ucontrol
)
1100 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1101 struct hda_gen_spec
*spec
= codec
->spec
;
1103 if (spec
->auto_mute_via_amp
) {
1104 hda_nid_t nid
= get_amp_nid(kcontrol
);
1105 bool enabled
= !((spec
->mute_bits
>> nid
) & 1);
1106 ucontrol
->value
.integer
.value
[0] &= enabled
;
1107 ucontrol
->value
.integer
.value
[1] &= enabled
;
1111 static int hda_gen_mixer_mute_put(struct snd_kcontrol
*kcontrol
,
1112 struct snd_ctl_elem_value
*ucontrol
)
1114 sync_auto_mute_bits(kcontrol
, ucontrol
);
1115 return snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
1118 static int hda_gen_bind_mute_put(struct snd_kcontrol
*kcontrol
,
1119 struct snd_ctl_elem_value
*ucontrol
)
1121 sync_auto_mute_bits(kcontrol
, ucontrol
);
1122 return snd_hda_mixer_bind_switch_put(kcontrol
, ucontrol
);
1125 /* any ctl assigned to the path with the given index? */
1126 static bool path_has_mixer(struct hda_codec
*codec
, int path_idx
, int ctl_type
)
1128 struct nid_path
*path
= snd_hda_get_path_from_idx(codec
, path_idx
);
1129 return path
&& path
->ctls
[ctl_type
];
1132 static const char * const channel_name
[4] = {
1133 "Front", "Surround", "CLFE", "Side"
1136 /* give some appropriate ctl name prefix for the given line out channel */
1137 static const char *get_line_out_pfx(struct hda_codec
*codec
, int ch
,
1138 int *index
, int ctl_type
)
1140 struct hda_gen_spec
*spec
= codec
->spec
;
1141 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1144 if (cfg
->line_outs
== 1 && !spec
->multi_ios
&&
1145 !cfg
->hp_outs
&& !cfg
->speaker_outs
)
1146 return spec
->vmaster_mute
.hook
? "PCM" : "Master";
1148 /* if there is really a single DAC used in the whole output paths,
1149 * use it master (or "PCM" if a vmaster hook is present)
1151 if (spec
->multiout
.num_dacs
== 1 && !spec
->mixer_nid
&&
1152 !spec
->multiout
.hp_out_nid
[0] && !spec
->multiout
.extra_out_nid
[0])
1153 return spec
->vmaster_mute
.hook
? "PCM" : "Master";
1155 /* multi-io channels */
1156 if (ch
>= cfg
->line_outs
)
1157 return channel_name
[ch
];
1159 switch (cfg
->line_out_type
) {
1160 case AUTO_PIN_SPEAKER_OUT
:
1161 /* if the primary channel vol/mute is shared with HP volume,
1162 * don't name it as Speaker
1164 if (!ch
&& cfg
->hp_outs
&&
1165 !path_has_mixer(codec
, spec
->hp_paths
[0], ctl_type
))
1167 if (cfg
->line_outs
== 1)
1169 if (cfg
->line_outs
== 2)
1170 return ch
? "Bass Speaker" : "Speaker";
1172 case AUTO_PIN_HP_OUT
:
1173 /* if the primary channel vol/mute is shared with spk volume,
1174 * don't name it as Headphone
1176 if (!ch
&& cfg
->speaker_outs
&&
1177 !path_has_mixer(codec
, spec
->speaker_paths
[0], ctl_type
))
1179 /* for multi-io case, only the primary out */
1180 if (ch
&& spec
->multi_ios
)
1184 case AUTO_PIN_LINE_OUT
:
1185 /* This deals with the case where we have two DACs and
1186 * one LO, one HP and one Speaker */
1187 if (!ch
&& cfg
->speaker_outs
&& cfg
->hp_outs
) {
1188 bool hp_lo_shared
= !path_has_mixer(codec
, spec
->hp_paths
[0], ctl_type
);
1189 bool spk_lo_shared
= !path_has_mixer(codec
, spec
->speaker_paths
[0], ctl_type
);
1190 if (hp_lo_shared
&& spk_lo_shared
)
1191 return spec
->vmaster_mute
.hook
? "PCM" : "Master";
1193 return "Headphone+LO";
1195 return "Speaker+LO";
1199 /* for a single channel output, we don't have to name the channel */
1200 if (cfg
->line_outs
== 1 && !spec
->multi_ios
)
1203 if (ch
>= ARRAY_SIZE(channel_name
)) {
1208 return channel_name
[ch
];
1212 * Parse output paths
1215 /* badness definition */
1217 /* No primary DAC is found for the main output */
1218 BAD_NO_PRIMARY_DAC
= 0x10000,
1219 /* No DAC is found for the extra output */
1220 BAD_NO_DAC
= 0x4000,
1221 /* No possible multi-ios */
1222 BAD_MULTI_IO
= 0x120,
1223 /* No individual DAC for extra output */
1224 BAD_NO_EXTRA_DAC
= 0x102,
1225 /* No individual DAC for extra surrounds */
1226 BAD_NO_EXTRA_SURR_DAC
= 0x101,
1227 /* Primary DAC shared with main surrounds */
1228 BAD_SHARED_SURROUND
= 0x100,
1229 /* No independent HP possible */
1230 BAD_NO_INDEP_HP
= 0x10,
1231 /* Primary DAC shared with main CLFE */
1232 BAD_SHARED_CLFE
= 0x10,
1233 /* Primary DAC shared with extra surrounds */
1234 BAD_SHARED_EXTRA_SURROUND
= 0x10,
1235 /* Volume widget is shared */
1236 BAD_SHARED_VOL
= 0x10,
1239 /* look for widgets in the given path which are appropriate for
1240 * volume and mute controls, and assign the values to ctls[].
1242 * When no appropriate widget is found in the path, the badness value
1243 * is incremented depending on the situation. The function returns the
1244 * total badness for both volume and mute controls.
1246 static int assign_out_path_ctls(struct hda_codec
*codec
, struct nid_path
*path
)
1248 struct hda_gen_spec
*spec
= codec
->spec
;
1254 return BAD_SHARED_VOL
* 2;
1256 if (path
->ctls
[NID_PATH_VOL_CTL
] ||
1257 path
->ctls
[NID_PATH_MUTE_CTL
])
1258 return 0; /* already evaluated */
1260 nid
= look_for_out_vol_nid(codec
, path
);
1262 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
1263 if (spec
->dac_min_mute
)
1264 val
|= HDA_AMP_VAL_MIN_MUTE
;
1265 if (is_ctl_used(codec
, val
, NID_PATH_VOL_CTL
))
1266 badness
+= BAD_SHARED_VOL
;
1268 path
->ctls
[NID_PATH_VOL_CTL
] = val
;
1270 badness
+= BAD_SHARED_VOL
;
1271 nid
= look_for_out_mute_nid(codec
, path
);
1273 unsigned int wid_type
= get_wcaps_type(get_wcaps(codec
, nid
));
1274 if (wid_type
== AC_WID_PIN
|| wid_type
== AC_WID_AUD_OUT
||
1275 nid_has_mute(codec
, nid
, HDA_OUTPUT
))
1276 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
1278 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_INPUT
);
1279 if (is_ctl_used(codec
, val
, NID_PATH_MUTE_CTL
))
1280 badness
+= BAD_SHARED_VOL
;
1282 path
->ctls
[NID_PATH_MUTE_CTL
] = val
;
1284 badness
+= BAD_SHARED_VOL
;
1288 const struct badness_table hda_main_out_badness
= {
1289 .no_primary_dac
= BAD_NO_PRIMARY_DAC
,
1290 .no_dac
= BAD_NO_DAC
,
1291 .shared_primary
= BAD_NO_PRIMARY_DAC
,
1292 .shared_surr
= BAD_SHARED_SURROUND
,
1293 .shared_clfe
= BAD_SHARED_CLFE
,
1294 .shared_surr_main
= BAD_SHARED_SURROUND
,
1296 EXPORT_SYMBOL_GPL(hda_main_out_badness
);
1298 const struct badness_table hda_extra_out_badness
= {
1299 .no_primary_dac
= BAD_NO_DAC
,
1300 .no_dac
= BAD_NO_DAC
,
1301 .shared_primary
= BAD_NO_EXTRA_DAC
,
1302 .shared_surr
= BAD_SHARED_EXTRA_SURROUND
,
1303 .shared_clfe
= BAD_SHARED_EXTRA_SURROUND
,
1304 .shared_surr_main
= BAD_NO_EXTRA_SURR_DAC
,
1306 EXPORT_SYMBOL_GPL(hda_extra_out_badness
);
1308 /* get the DAC of the primary output corresponding to the given array index */
1309 static hda_nid_t
get_primary_out(struct hda_codec
*codec
, int idx
)
1311 struct hda_gen_spec
*spec
= codec
->spec
;
1312 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1314 if (cfg
->line_outs
> idx
)
1315 return spec
->private_dac_nids
[idx
];
1316 idx
-= cfg
->line_outs
;
1317 if (spec
->multi_ios
> idx
)
1318 return spec
->multi_io
[idx
].dac
;
1322 /* return the DAC if it's reachable, otherwise zero */
1323 static inline hda_nid_t
try_dac(struct hda_codec
*codec
,
1324 hda_nid_t dac
, hda_nid_t pin
)
1326 return is_reachable_path(codec
, dac
, pin
) ? dac
: 0;
1329 /* try to assign DACs to pins and return the resultant badness */
1330 static int try_assign_dacs(struct hda_codec
*codec
, int num_outs
,
1331 const hda_nid_t
*pins
, hda_nid_t
*dacs
,
1333 const struct badness_table
*bad
)
1335 struct hda_gen_spec
*spec
= codec
->spec
;
1343 for (i
= 0; i
< num_outs
; i
++) {
1344 struct nid_path
*path
;
1345 hda_nid_t pin
= pins
[i
];
1347 path
= snd_hda_get_path_from_idx(codec
, path_idx
[i
]);
1349 badness
+= assign_out_path_ctls(codec
, path
);
1353 dacs
[i
] = get_preferred_dac(codec
, pin
);
1355 if (is_dac_already_used(codec
, dacs
[i
]))
1356 badness
+= bad
->shared_primary
;
1360 dacs
[i
] = look_for_dac(codec
, pin
, false);
1361 if (!dacs
[i
] && !i
) {
1362 /* try to steal the DAC of surrounds for the front */
1363 for (j
= 1; j
< num_outs
; j
++) {
1364 if (is_reachable_path(codec
, dacs
[j
], pin
)) {
1367 invalidate_nid_path(codec
, path_idx
[j
]);
1376 dac
= try_dac(codec
, get_primary_out(codec
, i
), pin
);
1378 dac
= try_dac(codec
, dacs
[0], pin
);
1380 dac
= try_dac(codec
, get_primary_out(codec
, i
), pin
);
1383 badness
+= bad
->shared_primary
;
1385 badness
+= bad
->shared_surr
;
1387 badness
+= bad
->shared_clfe
;
1388 } else if (is_reachable_path(codec
, spec
->private_dac_nids
[0], pin
)) {
1389 dac
= spec
->private_dac_nids
[0];
1390 badness
+= bad
->shared_surr_main
;
1392 badness
+= bad
->no_primary_dac
;
1394 badness
+= bad
->no_dac
;
1398 path
= snd_hda_add_new_path(codec
, dac
, pin
, -spec
->mixer_nid
);
1399 if (!path
&& !i
&& spec
->mixer_nid
) {
1400 /* try with aamix */
1401 path
= snd_hda_add_new_path(codec
, dac
, pin
, 0);
1405 badness
+= bad
->no_dac
;
1407 /* print_nid_path(codec, "output", path); */
1408 path
->active
= true;
1409 path_idx
[i
] = snd_hda_get_path_idx(codec
, path
);
1410 badness
+= assign_out_path_ctls(codec
, path
);
1417 /* return NID if the given pin has only a single connection to a certain DAC */
1418 static hda_nid_t
get_dac_if_single(struct hda_codec
*codec
, hda_nid_t pin
)
1420 struct hda_gen_spec
*spec
= codec
->spec
;
1422 hda_nid_t nid_found
= 0;
1424 for (i
= 0; i
< spec
->num_all_dacs
; i
++) {
1425 hda_nid_t nid
= spec
->all_dacs
[i
];
1426 if (!nid
|| is_dac_already_used(codec
, nid
))
1428 if (is_reachable_path(codec
, nid
, pin
)) {
1437 /* check whether the given pin can be a multi-io pin */
1438 static bool can_be_multiio_pin(struct hda_codec
*codec
,
1439 unsigned int location
, hda_nid_t nid
)
1441 unsigned int defcfg
, caps
;
1443 defcfg
= snd_hda_codec_get_pincfg(codec
, nid
);
1444 if (get_defcfg_connect(defcfg
) != AC_JACK_PORT_COMPLEX
)
1446 if (location
&& get_defcfg_location(defcfg
) != location
)
1448 caps
= snd_hda_query_pin_caps(codec
, nid
);
1449 if (!(caps
& AC_PINCAP_OUT
))
1454 /* count the number of input pins that are capable to be multi-io */
1455 static int count_multiio_pins(struct hda_codec
*codec
, hda_nid_t reference_pin
)
1457 struct hda_gen_spec
*spec
= codec
->spec
;
1458 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1459 unsigned int defcfg
= snd_hda_codec_get_pincfg(codec
, reference_pin
);
1460 unsigned int location
= get_defcfg_location(defcfg
);
1464 for (type
= AUTO_PIN_LINE_IN
; type
>= AUTO_PIN_MIC
; type
--) {
1465 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
1466 if (cfg
->inputs
[i
].type
!= type
)
1468 if (can_be_multiio_pin(codec
, location
,
1469 cfg
->inputs
[i
].pin
))
1479 * When hardwired is set, try to fill ony hardwired pins, and returns
1480 * zero if any pins are filled, non-zero if nothing found.
1481 * When hardwired is off, try to fill possible input pins, and returns
1482 * the badness value.
1484 static int fill_multi_ios(struct hda_codec
*codec
,
1485 hda_nid_t reference_pin
,
1488 struct hda_gen_spec
*spec
= codec
->spec
;
1489 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1490 int type
, i
, j
, num_pins
, old_pins
;
1491 unsigned int defcfg
= snd_hda_codec_get_pincfg(codec
, reference_pin
);
1492 unsigned int location
= get_defcfg_location(defcfg
);
1494 struct nid_path
*path
;
1496 old_pins
= spec
->multi_ios
;
1500 num_pins
= count_multiio_pins(codec
, reference_pin
);
1504 for (type
= AUTO_PIN_LINE_IN
; type
>= AUTO_PIN_MIC
; type
--) {
1505 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
1506 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
1509 if (cfg
->inputs
[i
].type
!= type
)
1511 if (!can_be_multiio_pin(codec
, location
, nid
))
1513 for (j
= 0; j
< spec
->multi_ios
; j
++) {
1514 if (nid
== spec
->multi_io
[j
].pin
)
1517 if (j
< spec
->multi_ios
)
1521 dac
= get_dac_if_single(codec
, nid
);
1523 dac
= look_for_dac(codec
, nid
, false);
1528 path
= snd_hda_add_new_path(codec
, dac
, nid
,
1534 /* print_nid_path(codec, "multiio", path); */
1535 spec
->multi_io
[spec
->multi_ios
].pin
= nid
;
1536 spec
->multi_io
[spec
->multi_ios
].dac
= dac
;
1537 spec
->out_paths
[cfg
->line_outs
+ spec
->multi_ios
] =
1538 snd_hda_get_path_idx(codec
, path
);
1540 if (spec
->multi_ios
>= 2)
1546 badness
= BAD_MULTI_IO
;
1547 if (old_pins
== spec
->multi_ios
) {
1549 return 1; /* nothing found */
1551 return badness
; /* no badness if nothing found */
1553 if (!hardwired
&& spec
->multi_ios
< 2) {
1554 /* cancel newly assigned paths */
1555 spec
->paths
.used
-= spec
->multi_ios
- old_pins
;
1556 spec
->multi_ios
= old_pins
;
1560 /* assign volume and mute controls */
1561 for (i
= old_pins
; i
< spec
->multi_ios
; i
++) {
1562 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[cfg
->line_outs
+ i
]);
1563 badness
+= assign_out_path_ctls(codec
, path
);
1569 /* map DACs for all pins in the list if they are single connections */
1570 static bool map_singles(struct hda_codec
*codec
, int outs
,
1571 const hda_nid_t
*pins
, hda_nid_t
*dacs
, int *path_idx
)
1573 struct hda_gen_spec
*spec
= codec
->spec
;
1576 for (i
= 0; i
< outs
; i
++) {
1577 struct nid_path
*path
;
1581 dac
= get_dac_if_single(codec
, pins
[i
]);
1584 path
= snd_hda_add_new_path(codec
, dac
, pins
[i
],
1586 if (!path
&& !i
&& spec
->mixer_nid
)
1587 path
= snd_hda_add_new_path(codec
, dac
, pins
[i
], 0);
1591 /* print_nid_path(codec, "output", path); */
1592 path
->active
= true;
1593 path_idx
[i
] = snd_hda_get_path_idx(codec
, path
);
1599 static inline bool has_aamix_out_paths(struct hda_gen_spec
*spec
)
1601 return spec
->aamix_out_paths
[0] || spec
->aamix_out_paths
[1] ||
1602 spec
->aamix_out_paths
[2];
1605 /* create a new path including aamix if available, and return its index */
1606 static int check_aamix_out_path(struct hda_codec
*codec
, int path_idx
)
1608 struct hda_gen_spec
*spec
= codec
->spec
;
1609 struct nid_path
*path
;
1610 hda_nid_t path_dac
, dac
, pin
;
1612 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
1613 if (!path
|| !path
->depth
||
1614 is_nid_contained(path
, spec
->mixer_nid
))
1616 path_dac
= path
->path
[0];
1617 dac
= spec
->private_dac_nids
[0];
1618 pin
= path
->path
[path
->depth
- 1];
1619 path
= snd_hda_add_new_path(codec
, dac
, pin
, spec
->mixer_nid
);
1621 if (dac
!= path_dac
)
1623 else if (spec
->multiout
.hp_out_nid
[0])
1624 dac
= spec
->multiout
.hp_out_nid
[0];
1625 else if (spec
->multiout
.extra_out_nid
[0])
1626 dac
= spec
->multiout
.extra_out_nid
[0];
1630 path
= snd_hda_add_new_path(codec
, dac
, pin
,
1635 /* print_nid_path(codec, "output-aamix", path); */
1636 path
->active
= false; /* unused as default */
1637 path
->pin_fixed
= true; /* static route */
1638 return snd_hda_get_path_idx(codec
, path
);
1641 /* check whether the independent HP is available with the current config */
1642 static bool indep_hp_possible(struct hda_codec
*codec
)
1644 struct hda_gen_spec
*spec
= codec
->spec
;
1645 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1646 struct nid_path
*path
;
1649 if (cfg
->line_out_type
== AUTO_PIN_HP_OUT
)
1650 idx
= spec
->out_paths
[0];
1652 idx
= spec
->hp_paths
[0];
1653 path
= snd_hda_get_path_from_idx(codec
, idx
);
1657 /* assume no path conflicts unless aamix is involved */
1658 if (!spec
->mixer_nid
|| !is_nid_contained(path
, spec
->mixer_nid
))
1661 /* check whether output paths contain aamix */
1662 for (i
= 0; i
< cfg
->line_outs
; i
++) {
1663 if (spec
->out_paths
[i
] == idx
)
1665 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[i
]);
1666 if (path
&& is_nid_contained(path
, spec
->mixer_nid
))
1669 for (i
= 0; i
< cfg
->speaker_outs
; i
++) {
1670 path
= snd_hda_get_path_from_idx(codec
, spec
->speaker_paths
[i
]);
1671 if (path
&& is_nid_contained(path
, spec
->mixer_nid
))
1678 /* fill the empty entries in the dac array for speaker/hp with the
1679 * shared dac pointed by the paths
1681 static void refill_shared_dacs(struct hda_codec
*codec
, int num_outs
,
1682 hda_nid_t
*dacs
, int *path_idx
)
1684 struct nid_path
*path
;
1687 for (i
= 0; i
< num_outs
; i
++) {
1690 path
= snd_hda_get_path_from_idx(codec
, path_idx
[i
]);
1693 dacs
[i
] = path
->path
[0];
1697 /* fill in the dac_nids table from the parsed pin configuration */
1698 static int fill_and_eval_dacs(struct hda_codec
*codec
,
1699 bool fill_hardwired
,
1700 bool fill_mio_first
)
1702 struct hda_gen_spec
*spec
= codec
->spec
;
1703 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1704 int i
, err
, badness
;
1706 /* set num_dacs once to full for look_for_dac() */
1707 spec
->multiout
.num_dacs
= cfg
->line_outs
;
1708 spec
->multiout
.dac_nids
= spec
->private_dac_nids
;
1709 memset(spec
->private_dac_nids
, 0, sizeof(spec
->private_dac_nids
));
1710 memset(spec
->multiout
.hp_out_nid
, 0, sizeof(spec
->multiout
.hp_out_nid
));
1711 memset(spec
->multiout
.extra_out_nid
, 0, sizeof(spec
->multiout
.extra_out_nid
));
1712 spec
->multi_ios
= 0;
1713 snd_array_free(&spec
->paths
);
1715 /* clear path indices */
1716 memset(spec
->out_paths
, 0, sizeof(spec
->out_paths
));
1717 memset(spec
->hp_paths
, 0, sizeof(spec
->hp_paths
));
1718 memset(spec
->speaker_paths
, 0, sizeof(spec
->speaker_paths
));
1719 memset(spec
->aamix_out_paths
, 0, sizeof(spec
->aamix_out_paths
));
1720 memset(spec
->digout_paths
, 0, sizeof(spec
->digout_paths
));
1721 memset(spec
->input_paths
, 0, sizeof(spec
->input_paths
));
1722 memset(spec
->loopback_paths
, 0, sizeof(spec
->loopback_paths
));
1723 memset(&spec
->digin_path
, 0, sizeof(spec
->digin_path
));
1727 /* fill hard-wired DACs first */
1728 if (fill_hardwired
) {
1731 mapped
= map_singles(codec
, cfg
->line_outs
,
1733 spec
->private_dac_nids
,
1735 mapped
|= map_singles(codec
, cfg
->hp_outs
,
1737 spec
->multiout
.hp_out_nid
,
1739 mapped
|= map_singles(codec
, cfg
->speaker_outs
,
1741 spec
->multiout
.extra_out_nid
,
1742 spec
->speaker_paths
);
1743 if (!spec
->no_multi_io
&&
1744 fill_mio_first
&& cfg
->line_outs
== 1 &&
1745 cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1746 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], true);
1753 badness
+= try_assign_dacs(codec
, cfg
->line_outs
, cfg
->line_out_pins
,
1754 spec
->private_dac_nids
, spec
->out_paths
,
1755 spec
->main_out_badness
);
1757 if (!spec
->no_multi_io
&& fill_mio_first
&&
1758 cfg
->line_outs
== 1 && cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1759 /* try to fill multi-io first */
1760 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], false);
1763 /* we don't count badness at this stage yet */
1766 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
) {
1767 err
= try_assign_dacs(codec
, cfg
->hp_outs
, cfg
->hp_pins
,
1768 spec
->multiout
.hp_out_nid
,
1770 spec
->extra_out_badness
);
1775 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1776 err
= try_assign_dacs(codec
, cfg
->speaker_outs
,
1778 spec
->multiout
.extra_out_nid
,
1779 spec
->speaker_paths
,
1780 spec
->extra_out_badness
);
1785 if (!spec
->no_multi_io
&&
1786 cfg
->line_outs
== 1 && cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1787 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], false);
1793 if (spec
->mixer_nid
) {
1794 spec
->aamix_out_paths
[0] =
1795 check_aamix_out_path(codec
, spec
->out_paths
[0]);
1796 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
1797 spec
->aamix_out_paths
[1] =
1798 check_aamix_out_path(codec
, spec
->hp_paths
[0]);
1799 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
1800 spec
->aamix_out_paths
[2] =
1801 check_aamix_out_path(codec
, spec
->speaker_paths
[0]);
1804 if (!spec
->no_multi_io
&&
1805 cfg
->hp_outs
&& cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
)
1806 if (count_multiio_pins(codec
, cfg
->hp_pins
[0]) >= 2)
1807 spec
->multi_ios
= 1; /* give badness */
1809 /* re-count num_dacs and squash invalid entries */
1810 spec
->multiout
.num_dacs
= 0;
1811 for (i
= 0; i
< cfg
->line_outs
; i
++) {
1812 if (spec
->private_dac_nids
[i
])
1813 spec
->multiout
.num_dacs
++;
1815 memmove(spec
->private_dac_nids
+ i
,
1816 spec
->private_dac_nids
+ i
+ 1,
1817 sizeof(hda_nid_t
) * (cfg
->line_outs
- i
- 1));
1818 spec
->private_dac_nids
[cfg
->line_outs
- 1] = 0;
1822 spec
->ext_channel_count
= spec
->min_channel_count
=
1823 spec
->multiout
.num_dacs
* 2;
1825 if (spec
->multi_ios
== 2) {
1826 for (i
= 0; i
< 2; i
++)
1827 spec
->private_dac_nids
[spec
->multiout
.num_dacs
++] =
1828 spec
->multi_io
[i
].dac
;
1829 } else if (spec
->multi_ios
) {
1830 spec
->multi_ios
= 0;
1831 badness
+= BAD_MULTI_IO
;
1834 if (spec
->indep_hp
&& !indep_hp_possible(codec
))
1835 badness
+= BAD_NO_INDEP_HP
;
1837 /* re-fill the shared DAC for speaker / headphone */
1838 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
1839 refill_shared_dacs(codec
, cfg
->hp_outs
,
1840 spec
->multiout
.hp_out_nid
,
1842 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
1843 refill_shared_dacs(codec
, cfg
->speaker_outs
,
1844 spec
->multiout
.extra_out_nid
,
1845 spec
->speaker_paths
);
1850 #define DEBUG_BADNESS
1852 #ifdef DEBUG_BADNESS
1853 #define debug_badness(fmt, ...) \
1854 codec_dbg(codec, fmt, ##__VA_ARGS__)
1856 #define debug_badness(fmt, ...) \
1857 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1860 #ifdef DEBUG_BADNESS
1861 static inline void print_nid_path_idx(struct hda_codec
*codec
,
1862 const char *pfx
, int idx
)
1864 struct nid_path
*path
;
1866 path
= snd_hda_get_path_from_idx(codec
, idx
);
1868 print_nid_path(codec
, pfx
, path
);
1871 static void debug_show_configs(struct hda_codec
*codec
,
1872 struct auto_pin_cfg
*cfg
)
1874 struct hda_gen_spec
*spec
= codec
->spec
;
1875 static const char * const lo_type
[3] = { "LO", "SP", "HP" };
1878 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1879 cfg
->line_out_pins
[0], cfg
->line_out_pins
[1],
1880 cfg
->line_out_pins
[2], cfg
->line_out_pins
[3],
1881 spec
->multiout
.dac_nids
[0],
1882 spec
->multiout
.dac_nids
[1],
1883 spec
->multiout
.dac_nids
[2],
1884 spec
->multiout
.dac_nids
[3],
1885 lo_type
[cfg
->line_out_type
]);
1886 for (i
= 0; i
< cfg
->line_outs
; i
++)
1887 print_nid_path_idx(codec
, " out", spec
->out_paths
[i
]);
1888 if (spec
->multi_ios
> 0)
1889 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1891 spec
->multi_io
[0].pin
, spec
->multi_io
[1].pin
,
1892 spec
->multi_io
[0].dac
, spec
->multi_io
[1].dac
);
1893 for (i
= 0; i
< spec
->multi_ios
; i
++)
1894 print_nid_path_idx(codec
, " mio",
1895 spec
->out_paths
[cfg
->line_outs
+ i
]);
1897 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1898 cfg
->hp_pins
[0], cfg
->hp_pins
[1],
1899 cfg
->hp_pins
[2], cfg
->hp_pins
[3],
1900 spec
->multiout
.hp_out_nid
[0],
1901 spec
->multiout
.hp_out_nid
[1],
1902 spec
->multiout
.hp_out_nid
[2],
1903 spec
->multiout
.hp_out_nid
[3]);
1904 for (i
= 0; i
< cfg
->hp_outs
; i
++)
1905 print_nid_path_idx(codec
, " hp ", spec
->hp_paths
[i
]);
1906 if (cfg
->speaker_outs
)
1907 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1908 cfg
->speaker_pins
[0], cfg
->speaker_pins
[1],
1909 cfg
->speaker_pins
[2], cfg
->speaker_pins
[3],
1910 spec
->multiout
.extra_out_nid
[0],
1911 spec
->multiout
.extra_out_nid
[1],
1912 spec
->multiout
.extra_out_nid
[2],
1913 spec
->multiout
.extra_out_nid
[3]);
1914 for (i
= 0; i
< cfg
->speaker_outs
; i
++)
1915 print_nid_path_idx(codec
, " spk", spec
->speaker_paths
[i
]);
1916 for (i
= 0; i
< 3; i
++)
1917 print_nid_path_idx(codec
, " mix", spec
->aamix_out_paths
[i
]);
1920 #define debug_show_configs(codec, cfg) /* NOP */
1923 /* find all available DACs of the codec */
1924 static void fill_all_dac_nids(struct hda_codec
*codec
)
1926 struct hda_gen_spec
*spec
= codec
->spec
;
1929 spec
->num_all_dacs
= 0;
1930 memset(spec
->all_dacs
, 0, sizeof(spec
->all_dacs
));
1931 for_each_hda_codec_node(nid
, codec
) {
1932 if (get_wcaps_type(get_wcaps(codec
, nid
)) != AC_WID_AUD_OUT
)
1934 if (spec
->num_all_dacs
>= ARRAY_SIZE(spec
->all_dacs
)) {
1935 codec_err(codec
, "Too many DACs!\n");
1938 spec
->all_dacs
[spec
->num_all_dacs
++] = nid
;
1942 static int parse_output_paths(struct hda_codec
*codec
)
1944 struct hda_gen_spec
*spec
= codec
->spec
;
1945 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1946 struct auto_pin_cfg
*best_cfg
;
1948 int best_badness
= INT_MAX
;
1950 bool fill_hardwired
= true, fill_mio_first
= true;
1951 bool best_wired
= true, best_mio
= true;
1952 bool hp_spk_swapped
= false;
1954 best_cfg
= kmalloc(sizeof(*best_cfg
), GFP_KERNEL
);
1960 badness
= fill_and_eval_dacs(codec
, fill_hardwired
,
1966 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1967 cfg
->line_out_type
, fill_hardwired
, fill_mio_first
,
1969 debug_show_configs(codec
, cfg
);
1970 if (badness
< best_badness
) {
1971 best_badness
= badness
;
1973 best_wired
= fill_hardwired
;
1974 best_mio
= fill_mio_first
;
1978 fill_mio_first
= !fill_mio_first
;
1979 if (!fill_mio_first
)
1981 fill_hardwired
= !fill_hardwired
;
1982 if (!fill_hardwired
)
1986 hp_spk_swapped
= true;
1987 if (cfg
->speaker_outs
> 0 &&
1988 cfg
->line_out_type
== AUTO_PIN_HP_OUT
) {
1989 cfg
->hp_outs
= cfg
->line_outs
;
1990 memcpy(cfg
->hp_pins
, cfg
->line_out_pins
,
1991 sizeof(cfg
->hp_pins
));
1992 cfg
->line_outs
= cfg
->speaker_outs
;
1993 memcpy(cfg
->line_out_pins
, cfg
->speaker_pins
,
1994 sizeof(cfg
->speaker_pins
));
1995 cfg
->speaker_outs
= 0;
1996 memset(cfg
->speaker_pins
, 0, sizeof(cfg
->speaker_pins
));
1997 cfg
->line_out_type
= AUTO_PIN_SPEAKER_OUT
;
1998 fill_hardwired
= true;
2001 if (cfg
->hp_outs
> 0 &&
2002 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
) {
2003 cfg
->speaker_outs
= cfg
->line_outs
;
2004 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
2005 sizeof(cfg
->speaker_pins
));
2006 cfg
->line_outs
= cfg
->hp_outs
;
2007 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
,
2008 sizeof(cfg
->hp_pins
));
2010 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
2011 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
2012 fill_hardwired
= true;
2019 debug_badness("==> restoring best_cfg\n");
2021 fill_and_eval_dacs(codec
, best_wired
, best_mio
);
2023 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2024 cfg
->line_out_type
, best_wired
, best_mio
);
2025 debug_show_configs(codec
, cfg
);
2027 if (cfg
->line_out_pins
[0]) {
2028 struct nid_path
*path
;
2029 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[0]);
2031 spec
->vmaster_nid
= look_for_out_vol_nid(codec
, path
);
2032 if (spec
->vmaster_nid
) {
2033 snd_hda_set_vmaster_tlv(codec
, spec
->vmaster_nid
,
2034 HDA_OUTPUT
, spec
->vmaster_tlv
);
2035 if (spec
->dac_min_mute
)
2036 spec
->vmaster_tlv
[3] |= TLV_DB_SCALE_MUTE
;
2040 /* set initial pinctl targets */
2041 if (spec
->prefer_hp_amp
|| cfg
->line_out_type
== AUTO_PIN_HP_OUT
)
2045 set_pin_targets(codec
, cfg
->line_outs
, cfg
->line_out_pins
, val
);
2046 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
2047 set_pin_targets(codec
, cfg
->hp_outs
, cfg
->hp_pins
, PIN_HP
);
2048 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
2049 val
= spec
->prefer_hp_amp
? PIN_HP
: PIN_OUT
;
2050 set_pin_targets(codec
, cfg
->speaker_outs
,
2051 cfg
->speaker_pins
, val
);
2054 /* clear indep_hp flag if not available */
2055 if (spec
->indep_hp
&& !indep_hp_possible(codec
))
2062 /* add playback controls from the parsed DAC table */
2063 static int create_multi_out_ctls(struct hda_codec
*codec
,
2064 const struct auto_pin_cfg
*cfg
)
2066 struct hda_gen_spec
*spec
= codec
->spec
;
2067 int i
, err
, noutputs
;
2069 noutputs
= cfg
->line_outs
;
2070 if (spec
->multi_ios
> 0 && cfg
->line_outs
< 3)
2071 noutputs
+= spec
->multi_ios
;
2073 for (i
= 0; i
< noutputs
; i
++) {
2076 struct nid_path
*path
;
2078 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[i
]);
2082 name
= get_line_out_pfx(codec
, i
, &index
, NID_PATH_VOL_CTL
);
2083 if (!name
|| !strcmp(name
, "CLFE")) {
2085 err
= add_vol_ctl(codec
, "Center", 0, 1, path
);
2088 err
= add_vol_ctl(codec
, "LFE", 0, 2, path
);
2092 err
= add_stereo_vol(codec
, name
, index
, path
);
2097 name
= get_line_out_pfx(codec
, i
, &index
, NID_PATH_MUTE_CTL
);
2098 if (!name
|| !strcmp(name
, "CLFE")) {
2099 err
= add_sw_ctl(codec
, "Center", 0, 1, path
);
2102 err
= add_sw_ctl(codec
, "LFE", 0, 2, path
);
2106 err
= add_stereo_sw(codec
, name
, index
, path
);
2114 static int create_extra_out(struct hda_codec
*codec
, int path_idx
,
2115 const char *pfx
, int cidx
)
2117 struct nid_path
*path
;
2120 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
2123 err
= add_stereo_vol(codec
, pfx
, cidx
, path
);
2126 err
= add_stereo_sw(codec
, pfx
, cidx
, path
);
2132 /* add playback controls for speaker and HP outputs */
2133 static int create_extra_outs(struct hda_codec
*codec
, int num_pins
,
2134 const int *paths
, const char *pfx
)
2138 for (i
= 0; i
< num_pins
; i
++) {
2140 char tmp
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
2143 if (num_pins
== 2 && i
== 1 && !strcmp(pfx
, "Speaker"))
2144 name
= "Bass Speaker";
2145 else if (num_pins
>= 3) {
2146 snprintf(tmp
, sizeof(tmp
), "%s %s",
2147 pfx
, channel_name
[i
]);
2153 err
= create_extra_out(codec
, paths
[i
], name
, idx
);
2160 static int create_hp_out_ctls(struct hda_codec
*codec
)
2162 struct hda_gen_spec
*spec
= codec
->spec
;
2163 return create_extra_outs(codec
, spec
->autocfg
.hp_outs
,
2168 static int create_speaker_out_ctls(struct hda_codec
*codec
)
2170 struct hda_gen_spec
*spec
= codec
->spec
;
2171 return create_extra_outs(codec
, spec
->autocfg
.speaker_outs
,
2172 spec
->speaker_paths
,
2177 * independent HP controls
2180 static void call_hp_automute(struct hda_codec
*codec
,
2181 struct hda_jack_callback
*jack
);
2182 static int indep_hp_info(struct snd_kcontrol
*kcontrol
,
2183 struct snd_ctl_elem_info
*uinfo
)
2185 return snd_hda_enum_bool_helper_info(kcontrol
, uinfo
);
2188 static int indep_hp_get(struct snd_kcontrol
*kcontrol
,
2189 struct snd_ctl_elem_value
*ucontrol
)
2191 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2192 struct hda_gen_spec
*spec
= codec
->spec
;
2193 ucontrol
->value
.enumerated
.item
[0] = spec
->indep_hp_enabled
;
2197 static void update_aamix_paths(struct hda_codec
*codec
, bool do_mix
,
2198 int nomix_path_idx
, int mix_path_idx
,
2201 static int indep_hp_put(struct snd_kcontrol
*kcontrol
,
2202 struct snd_ctl_elem_value
*ucontrol
)
2204 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2205 struct hda_gen_spec
*spec
= codec
->spec
;
2206 unsigned int select
= ucontrol
->value
.enumerated
.item
[0];
2209 mutex_lock(&spec
->pcm_mutex
);
2210 if (spec
->active_streams
) {
2215 if (spec
->indep_hp_enabled
!= select
) {
2217 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
2218 dacp
= &spec
->private_dac_nids
[0];
2220 dacp
= &spec
->multiout
.hp_out_nid
[0];
2222 /* update HP aamix paths in case it conflicts with indep HP */
2223 if (spec
->have_aamix_ctl
) {
2224 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
2225 update_aamix_paths(codec
, spec
->aamix_mode
,
2227 spec
->aamix_out_paths
[0],
2228 spec
->autocfg
.line_out_type
);
2230 update_aamix_paths(codec
, spec
->aamix_mode
,
2232 spec
->aamix_out_paths
[1],
2236 spec
->indep_hp_enabled
= select
;
2237 if (spec
->indep_hp_enabled
)
2240 *dacp
= spec
->alt_dac_nid
;
2242 call_hp_automute(codec
, NULL
);
2246 mutex_unlock(&spec
->pcm_mutex
);
2250 static const struct snd_kcontrol_new indep_hp_ctl
= {
2251 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2252 .name
= "Independent HP",
2253 .info
= indep_hp_info
,
2254 .get
= indep_hp_get
,
2255 .put
= indep_hp_put
,
2259 static int create_indep_hp_ctls(struct hda_codec
*codec
)
2261 struct hda_gen_spec
*spec
= codec
->spec
;
2264 if (!spec
->indep_hp
)
2266 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
2267 dac
= spec
->multiout
.dac_nids
[0];
2269 dac
= spec
->multiout
.hp_out_nid
[0];
2275 spec
->indep_hp_enabled
= false;
2276 spec
->alt_dac_nid
= dac
;
2277 if (!snd_hda_gen_add_kctl(spec
, NULL
, &indep_hp_ctl
))
2283 * channel mode enum control
2286 static int ch_mode_info(struct snd_kcontrol
*kcontrol
,
2287 struct snd_ctl_elem_info
*uinfo
)
2289 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2290 struct hda_gen_spec
*spec
= codec
->spec
;
2293 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2295 uinfo
->value
.enumerated
.items
= spec
->multi_ios
+ 1;
2296 if (uinfo
->value
.enumerated
.item
> spec
->multi_ios
)
2297 uinfo
->value
.enumerated
.item
= spec
->multi_ios
;
2298 chs
= uinfo
->value
.enumerated
.item
* 2 + spec
->min_channel_count
;
2299 sprintf(uinfo
->value
.enumerated
.name
, "%dch", chs
);
2303 static int ch_mode_get(struct snd_kcontrol
*kcontrol
,
2304 struct snd_ctl_elem_value
*ucontrol
)
2306 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2307 struct hda_gen_spec
*spec
= codec
->spec
;
2308 ucontrol
->value
.enumerated
.item
[0] =
2309 (spec
->ext_channel_count
- spec
->min_channel_count
) / 2;
2313 static inline struct nid_path
*
2314 get_multiio_path(struct hda_codec
*codec
, int idx
)
2316 struct hda_gen_spec
*spec
= codec
->spec
;
2317 return snd_hda_get_path_from_idx(codec
,
2318 spec
->out_paths
[spec
->autocfg
.line_outs
+ idx
]);
2321 static void update_automute_all(struct hda_codec
*codec
);
2323 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2324 * used for output paths
2326 static bool aamix_default(struct hda_gen_spec
*spec
)
2328 return !spec
->have_aamix_ctl
|| spec
->aamix_mode
;
2331 static int set_multi_io(struct hda_codec
*codec
, int idx
, bool output
)
2333 struct hda_gen_spec
*spec
= codec
->spec
;
2334 hda_nid_t nid
= spec
->multi_io
[idx
].pin
;
2335 struct nid_path
*path
;
2337 path
= get_multiio_path(codec
, idx
);
2341 if (path
->active
== output
)
2345 set_pin_target(codec
, nid
, PIN_OUT
, true);
2346 snd_hda_activate_path(codec
, path
, true, aamix_default(spec
));
2347 set_pin_eapd(codec
, nid
, true);
2349 set_pin_eapd(codec
, nid
, false);
2350 snd_hda_activate_path(codec
, path
, false, aamix_default(spec
));
2351 set_pin_target(codec
, nid
, spec
->multi_io
[idx
].ctl_in
, true);
2352 path_power_down_sync(codec
, path
);
2355 /* update jack retasking in case it modifies any of them */
2356 update_automute_all(codec
);
2361 static int ch_mode_put(struct snd_kcontrol
*kcontrol
,
2362 struct snd_ctl_elem_value
*ucontrol
)
2364 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2365 struct hda_gen_spec
*spec
= codec
->spec
;
2368 ch
= ucontrol
->value
.enumerated
.item
[0];
2369 if (ch
< 0 || ch
> spec
->multi_ios
)
2371 if (ch
== (spec
->ext_channel_count
- spec
->min_channel_count
) / 2)
2373 spec
->ext_channel_count
= ch
* 2 + spec
->min_channel_count
;
2374 for (i
= 0; i
< spec
->multi_ios
; i
++)
2375 set_multi_io(codec
, i
, i
< ch
);
2376 spec
->multiout
.max_channels
= max(spec
->ext_channel_count
,
2377 spec
->const_channel_count
);
2378 if (spec
->need_dac_fix
)
2379 spec
->multiout
.num_dacs
= spec
->multiout
.max_channels
/ 2;
2383 static const struct snd_kcontrol_new channel_mode_enum
= {
2384 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2385 .name
= "Channel Mode",
2386 .info
= ch_mode_info
,
2391 static int create_multi_channel_mode(struct hda_codec
*codec
)
2393 struct hda_gen_spec
*spec
= codec
->spec
;
2395 if (spec
->multi_ios
> 0) {
2396 if (!snd_hda_gen_add_kctl(spec
, NULL
, &channel_mode_enum
))
2403 * aamix loopback enable/disable switch
2406 #define loopback_mixing_info indep_hp_info
2408 static int loopback_mixing_get(struct snd_kcontrol
*kcontrol
,
2409 struct snd_ctl_elem_value
*ucontrol
)
2411 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2412 struct hda_gen_spec
*spec
= codec
->spec
;
2413 ucontrol
->value
.enumerated
.item
[0] = spec
->aamix_mode
;
2417 static void update_aamix_paths(struct hda_codec
*codec
, bool do_mix
,
2418 int nomix_path_idx
, int mix_path_idx
,
2421 struct hda_gen_spec
*spec
= codec
->spec
;
2422 struct nid_path
*nomix_path
, *mix_path
;
2424 nomix_path
= snd_hda_get_path_from_idx(codec
, nomix_path_idx
);
2425 mix_path
= snd_hda_get_path_from_idx(codec
, mix_path_idx
);
2426 if (!nomix_path
|| !mix_path
)
2429 /* if HP aamix path is driven from a different DAC and the
2430 * independent HP mode is ON, can't turn on aamix path
2432 if (out_type
== AUTO_PIN_HP_OUT
&& spec
->indep_hp_enabled
&&
2433 mix_path
->path
[0] != spec
->alt_dac_nid
)
2437 snd_hda_activate_path(codec
, nomix_path
, false, true);
2438 snd_hda_activate_path(codec
, mix_path
, true, true);
2439 path_power_down_sync(codec
, nomix_path
);
2441 snd_hda_activate_path(codec
, mix_path
, false, false);
2442 snd_hda_activate_path(codec
, nomix_path
, true, false);
2443 path_power_down_sync(codec
, mix_path
);
2447 /* re-initialize the output paths; only called from loopback_mixing_put() */
2448 static void update_output_paths(struct hda_codec
*codec
, int num_outs
,
2451 struct hda_gen_spec
*spec
= codec
->spec
;
2452 struct nid_path
*path
;
2455 for (i
= 0; i
< num_outs
; i
++) {
2456 path
= snd_hda_get_path_from_idx(codec
, paths
[i
]);
2458 snd_hda_activate_path(codec
, path
, path
->active
,
2463 static int loopback_mixing_put(struct snd_kcontrol
*kcontrol
,
2464 struct snd_ctl_elem_value
*ucontrol
)
2466 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2467 struct hda_gen_spec
*spec
= codec
->spec
;
2468 const struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
2469 unsigned int val
= ucontrol
->value
.enumerated
.item
[0];
2471 if (val
== spec
->aamix_mode
)
2473 spec
->aamix_mode
= val
;
2474 if (has_aamix_out_paths(spec
)) {
2475 update_aamix_paths(codec
, val
, spec
->out_paths
[0],
2476 spec
->aamix_out_paths
[0],
2477 cfg
->line_out_type
);
2478 update_aamix_paths(codec
, val
, spec
->hp_paths
[0],
2479 spec
->aamix_out_paths
[1],
2481 update_aamix_paths(codec
, val
, spec
->speaker_paths
[0],
2482 spec
->aamix_out_paths
[2],
2483 AUTO_PIN_SPEAKER_OUT
);
2485 update_output_paths(codec
, cfg
->line_outs
, spec
->out_paths
);
2486 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
2487 update_output_paths(codec
, cfg
->hp_outs
, spec
->hp_paths
);
2488 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
2489 update_output_paths(codec
, cfg
->speaker_outs
,
2490 spec
->speaker_paths
);
2495 static const struct snd_kcontrol_new loopback_mixing_enum
= {
2496 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2497 .name
= "Loopback Mixing",
2498 .info
= loopback_mixing_info
,
2499 .get
= loopback_mixing_get
,
2500 .put
= loopback_mixing_put
,
2503 static int create_loopback_mixing_ctl(struct hda_codec
*codec
)
2505 struct hda_gen_spec
*spec
= codec
->spec
;
2507 if (!spec
->mixer_nid
)
2509 if (!snd_hda_gen_add_kctl(spec
, NULL
, &loopback_mixing_enum
))
2511 spec
->have_aamix_ctl
= 1;
2512 /* if no explicit aamix path is present (e.g. for Realtek codecs),
2513 * enable aamix as default -- just for compatibility
2515 spec
->aamix_mode
= !has_aamix_out_paths(spec
);
2520 * shared headphone/mic handling
2523 static void call_update_outputs(struct hda_codec
*codec
);
2525 /* for shared I/O, change the pin-control accordingly */
2526 static void update_hp_mic(struct hda_codec
*codec
, int adc_mux
, bool force
)
2528 struct hda_gen_spec
*spec
= codec
->spec
;
2533 pin
= spec
->hp_mic_pin
;
2534 as_mic
= spec
->cur_mux
[adc_mux
] == spec
->hp_mic_mux_idx
;
2537 val
= snd_hda_codec_get_pin_target(codec
, pin
);
2547 val
= snd_hda_get_default_vref(codec
, pin
);
2548 /* if the HP pin doesn't support VREF and the codec driver gives an
2549 * alternative pin, set up the VREF on that pin instead
2551 if (val
== AC_PINCTL_VREF_HIZ
&& spec
->shared_mic_vref_pin
) {
2552 const hda_nid_t vref_pin
= spec
->shared_mic_vref_pin
;
2553 unsigned int vref_val
= snd_hda_get_default_vref(codec
, vref_pin
);
2554 if (vref_val
!= AC_PINCTL_VREF_HIZ
)
2555 snd_hda_set_pin_ctl_cache(codec
, vref_pin
,
2556 PIN_IN
| (as_mic
? vref_val
: 0));
2559 if (!spec
->hp_mic_jack_modes
) {
2564 set_pin_target(codec
, pin
, val
, true);
2565 call_hp_automute(codec
, NULL
);
2569 /* create a shared input with the headphone out */
2570 static int create_hp_mic(struct hda_codec
*codec
)
2572 struct hda_gen_spec
*spec
= codec
->spec
;
2573 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
2574 unsigned int defcfg
;
2577 if (!spec
->hp_mic
) {
2578 if (spec
->suppress_hp_mic_detect
)
2580 /* automatic detection: only if no input or a single internal
2581 * input pin is found, try to detect the shared hp/mic
2583 if (cfg
->num_inputs
> 1)
2585 else if (cfg
->num_inputs
== 1) {
2586 defcfg
= snd_hda_codec_get_pincfg(codec
, cfg
->inputs
[0].pin
);
2587 if (snd_hda_get_input_pin_attr(defcfg
) != INPUT_PIN_ATTR_INT
)
2592 spec
->hp_mic
= 0; /* clear once */
2593 if (cfg
->num_inputs
>= AUTO_CFG_MAX_INS
)
2597 if (cfg
->line_out_type
== AUTO_PIN_HP_OUT
&& cfg
->line_outs
> 0)
2598 nid
= cfg
->line_out_pins
[0];
2599 else if (cfg
->hp_outs
> 0)
2600 nid
= cfg
->hp_pins
[0];
2604 if (!(snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_IN
))
2605 return 0; /* no input */
2607 cfg
->inputs
[cfg
->num_inputs
].pin
= nid
;
2608 cfg
->inputs
[cfg
->num_inputs
].type
= AUTO_PIN_MIC
;
2609 cfg
->inputs
[cfg
->num_inputs
].is_headphone_mic
= 1;
2612 spec
->hp_mic_pin
= nid
;
2613 /* we can't handle auto-mic together with HP-mic */
2614 spec
->suppress_auto_mic
= 1;
2615 codec_dbg(codec
, "Enable shared I/O jack on NID 0x%x\n", nid
);
2623 static int create_hp_mic_jack_mode(struct hda_codec
*codec
, hda_nid_t pin
);
2625 static const char * const out_jack_texts
[] = {
2626 "Line Out", "Headphone Out",
2629 static int out_jack_mode_info(struct snd_kcontrol
*kcontrol
,
2630 struct snd_ctl_elem_info
*uinfo
)
2632 return snd_hda_enum_helper_info(kcontrol
, uinfo
, 2, out_jack_texts
);
2635 static int out_jack_mode_get(struct snd_kcontrol
*kcontrol
,
2636 struct snd_ctl_elem_value
*ucontrol
)
2638 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2639 hda_nid_t nid
= kcontrol
->private_value
;
2640 if (snd_hda_codec_get_pin_target(codec
, nid
) == PIN_HP
)
2641 ucontrol
->value
.enumerated
.item
[0] = 1;
2643 ucontrol
->value
.enumerated
.item
[0] = 0;
2647 static int out_jack_mode_put(struct snd_kcontrol
*kcontrol
,
2648 struct snd_ctl_elem_value
*ucontrol
)
2650 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2651 hda_nid_t nid
= kcontrol
->private_value
;
2654 val
= ucontrol
->value
.enumerated
.item
[0] ? PIN_HP
: PIN_OUT
;
2655 if (snd_hda_codec_get_pin_target(codec
, nid
) == val
)
2657 snd_hda_set_pin_ctl_cache(codec
, nid
, val
);
2661 static const struct snd_kcontrol_new out_jack_mode_enum
= {
2662 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2663 .info
= out_jack_mode_info
,
2664 .get
= out_jack_mode_get
,
2665 .put
= out_jack_mode_put
,
2668 static bool find_kctl_name(struct hda_codec
*codec
, const char *name
, int idx
)
2670 struct hda_gen_spec
*spec
= codec
->spec
;
2673 for (i
= 0; i
< spec
->kctls
.used
; i
++) {
2674 struct snd_kcontrol_new
*kctl
= snd_array_elem(&spec
->kctls
, i
);
2675 if (!strcmp(kctl
->name
, name
) && kctl
->index
== idx
)
2681 static void get_jack_mode_name(struct hda_codec
*codec
, hda_nid_t pin
,
2682 char *name
, size_t name_len
)
2684 struct hda_gen_spec
*spec
= codec
->spec
;
2687 snd_hda_get_pin_label(codec
, pin
, &spec
->autocfg
, name
, name_len
, &idx
);
2688 strlcat(name
, " Jack Mode", name_len
);
2690 for (; find_kctl_name(codec
, name
, idx
); idx
++)
2694 static int get_out_jack_num_items(struct hda_codec
*codec
, hda_nid_t pin
)
2696 struct hda_gen_spec
*spec
= codec
->spec
;
2697 if (spec
->add_jack_modes
) {
2698 unsigned int pincap
= snd_hda_query_pin_caps(codec
, pin
);
2699 if ((pincap
& AC_PINCAP_OUT
) && (pincap
& AC_PINCAP_HP_DRV
))
2705 static int create_out_jack_modes(struct hda_codec
*codec
, int num_pins
,
2708 struct hda_gen_spec
*spec
= codec
->spec
;
2711 for (i
= 0; i
< num_pins
; i
++) {
2712 hda_nid_t pin
= pins
[i
];
2713 if (pin
== spec
->hp_mic_pin
)
2715 if (get_out_jack_num_items(codec
, pin
) > 1) {
2716 struct snd_kcontrol_new
*knew
;
2717 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
2718 get_jack_mode_name(codec
, pin
, name
, sizeof(name
));
2719 knew
= snd_hda_gen_add_kctl(spec
, name
,
2720 &out_jack_mode_enum
);
2723 knew
->private_value
= pin
;
2734 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2737 static const char * const vref_texts
[NUM_VREFS
] = {
2738 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2739 "", "Mic 80pc Bias", "Mic 100pc Bias"
2742 static unsigned int get_vref_caps(struct hda_codec
*codec
, hda_nid_t pin
)
2744 unsigned int pincap
;
2746 pincap
= snd_hda_query_pin_caps(codec
, pin
);
2747 pincap
= (pincap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
2748 /* filter out unusual vrefs */
2749 pincap
&= ~(AC_PINCAP_VREF_GRD
| AC_PINCAP_VREF_100
);
2753 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2754 static int get_vref_idx(unsigned int vref_caps
, unsigned int item_idx
)
2756 unsigned int i
, n
= 0;
2758 for (i
= 0; i
< NUM_VREFS
; i
++) {
2759 if (vref_caps
& (1 << i
)) {
2768 /* convert back from the vref ctl index to the enum item index */
2769 static int cvt_from_vref_idx(unsigned int vref_caps
, unsigned int idx
)
2771 unsigned int i
, n
= 0;
2773 for (i
= 0; i
< NUM_VREFS
; i
++) {
2776 if (vref_caps
& (1 << i
))
2782 static int in_jack_mode_info(struct snd_kcontrol
*kcontrol
,
2783 struct snd_ctl_elem_info
*uinfo
)
2785 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2786 hda_nid_t nid
= kcontrol
->private_value
;
2787 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2789 snd_hda_enum_helper_info(kcontrol
, uinfo
, hweight32(vref_caps
),
2791 /* set the right text */
2792 strcpy(uinfo
->value
.enumerated
.name
,
2793 vref_texts
[get_vref_idx(vref_caps
, uinfo
->value
.enumerated
.item
)]);
2797 static int in_jack_mode_get(struct snd_kcontrol
*kcontrol
,
2798 struct snd_ctl_elem_value
*ucontrol
)
2800 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2801 hda_nid_t nid
= kcontrol
->private_value
;
2802 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2805 idx
= snd_hda_codec_get_pin_target(codec
, nid
) & AC_PINCTL_VREFEN
;
2806 ucontrol
->value
.enumerated
.item
[0] = cvt_from_vref_idx(vref_caps
, idx
);
2810 static int in_jack_mode_put(struct snd_kcontrol
*kcontrol
,
2811 struct snd_ctl_elem_value
*ucontrol
)
2813 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2814 hda_nid_t nid
= kcontrol
->private_value
;
2815 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2816 unsigned int val
, idx
;
2818 val
= snd_hda_codec_get_pin_target(codec
, nid
);
2819 idx
= cvt_from_vref_idx(vref_caps
, val
& AC_PINCTL_VREFEN
);
2820 if (idx
== ucontrol
->value
.enumerated
.item
[0])
2823 val
&= ~AC_PINCTL_VREFEN
;
2824 val
|= get_vref_idx(vref_caps
, ucontrol
->value
.enumerated
.item
[0]);
2825 snd_hda_set_pin_ctl_cache(codec
, nid
, val
);
2829 static const struct snd_kcontrol_new in_jack_mode_enum
= {
2830 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2831 .info
= in_jack_mode_info
,
2832 .get
= in_jack_mode_get
,
2833 .put
= in_jack_mode_put
,
2836 static int get_in_jack_num_items(struct hda_codec
*codec
, hda_nid_t pin
)
2838 struct hda_gen_spec
*spec
= codec
->spec
;
2840 if (spec
->add_jack_modes
)
2841 nitems
= hweight32(get_vref_caps(codec
, pin
));
2842 return nitems
? nitems
: 1;
2845 static int create_in_jack_mode(struct hda_codec
*codec
, hda_nid_t pin
)
2847 struct hda_gen_spec
*spec
= codec
->spec
;
2848 struct snd_kcontrol_new
*knew
;
2849 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
2850 unsigned int defcfg
;
2852 if (pin
== spec
->hp_mic_pin
)
2853 return 0; /* already done in create_out_jack_mode() */
2855 /* no jack mode for fixed pins */
2856 defcfg
= snd_hda_codec_get_pincfg(codec
, pin
);
2857 if (snd_hda_get_input_pin_attr(defcfg
) == INPUT_PIN_ATTR_INT
)
2860 /* no multiple vref caps? */
2861 if (get_in_jack_num_items(codec
, pin
) <= 1)
2864 get_jack_mode_name(codec
, pin
, name
, sizeof(name
));
2865 knew
= snd_hda_gen_add_kctl(spec
, name
, &in_jack_mode_enum
);
2868 knew
->private_value
= pin
;
2873 * HP/mic shared jack mode
2875 static int hp_mic_jack_mode_info(struct snd_kcontrol
*kcontrol
,
2876 struct snd_ctl_elem_info
*uinfo
)
2878 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2879 hda_nid_t nid
= kcontrol
->private_value
;
2880 int out_jacks
= get_out_jack_num_items(codec
, nid
);
2881 int in_jacks
= get_in_jack_num_items(codec
, nid
);
2882 const char *text
= NULL
;
2885 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2887 uinfo
->value
.enumerated
.items
= out_jacks
+ in_jacks
;
2888 if (uinfo
->value
.enumerated
.item
>= uinfo
->value
.enumerated
.items
)
2889 uinfo
->value
.enumerated
.item
= uinfo
->value
.enumerated
.items
- 1;
2890 idx
= uinfo
->value
.enumerated
.item
;
2891 if (idx
< out_jacks
) {
2893 text
= out_jack_texts
[idx
];
2895 text
= "Headphone Out";
2899 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2900 text
= vref_texts
[get_vref_idx(vref_caps
, idx
)];
2905 strcpy(uinfo
->value
.enumerated
.name
, text
);
2909 static int get_cur_hp_mic_jack_mode(struct hda_codec
*codec
, hda_nid_t nid
)
2911 int out_jacks
= get_out_jack_num_items(codec
, nid
);
2912 int in_jacks
= get_in_jack_num_items(codec
, nid
);
2913 unsigned int val
= snd_hda_codec_get_pin_target(codec
, nid
);
2916 if (val
& PIN_OUT
) {
2917 if (out_jacks
> 1 && val
== PIN_HP
)
2919 } else if (val
& PIN_IN
) {
2922 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2923 val
&= AC_PINCTL_VREFEN
;
2924 idx
+= cvt_from_vref_idx(vref_caps
, val
);
2930 static int hp_mic_jack_mode_get(struct snd_kcontrol
*kcontrol
,
2931 struct snd_ctl_elem_value
*ucontrol
)
2933 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2934 hda_nid_t nid
= kcontrol
->private_value
;
2935 ucontrol
->value
.enumerated
.item
[0] =
2936 get_cur_hp_mic_jack_mode(codec
, nid
);
2940 static int hp_mic_jack_mode_put(struct snd_kcontrol
*kcontrol
,
2941 struct snd_ctl_elem_value
*ucontrol
)
2943 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2944 hda_nid_t nid
= kcontrol
->private_value
;
2945 int out_jacks
= get_out_jack_num_items(codec
, nid
);
2946 int in_jacks
= get_in_jack_num_items(codec
, nid
);
2947 unsigned int val
, oldval
, idx
;
2949 oldval
= get_cur_hp_mic_jack_mode(codec
, nid
);
2950 idx
= ucontrol
->value
.enumerated
.item
[0];
2954 if (idx
< out_jacks
) {
2956 val
= idx
? PIN_HP
: PIN_OUT
;
2962 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2963 val
= snd_hda_codec_get_pin_target(codec
, nid
);
2964 val
&= ~(AC_PINCTL_VREFEN
| PIN_HP
);
2965 val
|= get_vref_idx(vref_caps
, idx
) | PIN_IN
;
2967 val
= snd_hda_get_default_vref(codec
, nid
) | PIN_IN
;
2969 snd_hda_set_pin_ctl_cache(codec
, nid
, val
);
2970 call_hp_automute(codec
, NULL
);
2975 static const struct snd_kcontrol_new hp_mic_jack_mode_enum
= {
2976 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2977 .info
= hp_mic_jack_mode_info
,
2978 .get
= hp_mic_jack_mode_get
,
2979 .put
= hp_mic_jack_mode_put
,
2982 static int create_hp_mic_jack_mode(struct hda_codec
*codec
, hda_nid_t pin
)
2984 struct hda_gen_spec
*spec
= codec
->spec
;
2985 struct snd_kcontrol_new
*knew
;
2987 knew
= snd_hda_gen_add_kctl(spec
, "Headphone Mic Jack Mode",
2988 &hp_mic_jack_mode_enum
);
2991 knew
->private_value
= pin
;
2992 spec
->hp_mic_jack_modes
= 1;
3000 /* add the powersave loopback-list entry */
3001 static int add_loopback_list(struct hda_gen_spec
*spec
, hda_nid_t mix
, int idx
)
3003 struct hda_amp_list
*list
;
3005 list
= snd_array_new(&spec
->loopback_list
);
3009 list
->dir
= HDA_INPUT
;
3011 spec
->loopback
.amplist
= spec
->loopback_list
.list
;
3015 /* return true if either a volume or a mute amp is found for the given
3016 * aamix path; the amp has to be either in the mixer node or its direct leaf
3018 static bool look_for_mix_leaf_ctls(struct hda_codec
*codec
, hda_nid_t mix_nid
,
3019 hda_nid_t pin
, unsigned int *mix_val
,
3020 unsigned int *mute_val
)
3023 const hda_nid_t
*list
;
3026 idx
= snd_hda_get_conn_index(codec
, mix_nid
, pin
, true);
3030 *mix_val
= *mute_val
= 0;
3031 if (nid_has_volume(codec
, mix_nid
, HDA_INPUT
))
3032 *mix_val
= HDA_COMPOSE_AMP_VAL(mix_nid
, 3, idx
, HDA_INPUT
);
3033 if (nid_has_mute(codec
, mix_nid
, HDA_INPUT
))
3034 *mute_val
= HDA_COMPOSE_AMP_VAL(mix_nid
, 3, idx
, HDA_INPUT
);
3035 if (*mix_val
&& *mute_val
)
3038 /* check leaf node */
3039 num_conns
= snd_hda_get_conn_list(codec
, mix_nid
, &list
);
3040 if (num_conns
< idx
)
3043 if (!*mix_val
&& nid_has_volume(codec
, nid
, HDA_OUTPUT
) &&
3044 !is_ctl_associated(codec
, nid
, HDA_OUTPUT
, 0, NID_PATH_VOL_CTL
))
3045 *mix_val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3046 if (!*mute_val
&& nid_has_mute(codec
, nid
, HDA_OUTPUT
) &&
3047 !is_ctl_associated(codec
, nid
, HDA_OUTPUT
, 0, NID_PATH_MUTE_CTL
))
3048 *mute_val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3050 return *mix_val
|| *mute_val
;
3053 /* create input playback/capture controls for the given pin */
3054 static int new_analog_input(struct hda_codec
*codec
, int input_idx
,
3055 hda_nid_t pin
, const char *ctlname
, int ctlidx
,
3058 struct hda_gen_spec
*spec
= codec
->spec
;
3059 struct nid_path
*path
;
3060 unsigned int mix_val
, mute_val
;
3063 if (!look_for_mix_leaf_ctls(codec
, mix_nid
, pin
, &mix_val
, &mute_val
))
3066 path
= snd_hda_add_new_path(codec
, pin
, mix_nid
, 0);
3069 print_nid_path(codec
, "loopback", path
);
3070 spec
->loopback_paths
[input_idx
] = snd_hda_get_path_idx(codec
, path
);
3072 idx
= path
->idx
[path
->depth
- 1];
3074 err
= __add_pb_vol_ctrl(spec
, HDA_CTL_WIDGET_VOL
, ctlname
, ctlidx
, mix_val
);
3077 path
->ctls
[NID_PATH_VOL_CTL
] = mix_val
;
3081 err
= __add_pb_sw_ctrl(spec
, HDA_CTL_WIDGET_MUTE
, ctlname
, ctlidx
, mute_val
);
3084 path
->ctls
[NID_PATH_MUTE_CTL
] = mute_val
;
3087 path
->active
= true;
3088 path
->stream_enabled
= true; /* no DAC/ADC involved */
3089 err
= add_loopback_list(spec
, mix_nid
, idx
);
3093 if (spec
->mixer_nid
!= spec
->mixer_merge_nid
&&
3094 !spec
->loopback_merge_path
) {
3095 path
= snd_hda_add_new_path(codec
, spec
->mixer_nid
,
3096 spec
->mixer_merge_nid
, 0);
3098 print_nid_path(codec
, "loopback-merge", path
);
3099 path
->active
= true;
3100 path
->pin_fixed
= true; /* static route */
3101 path
->stream_enabled
= true; /* no DAC/ADC involved */
3102 spec
->loopback_merge_path
=
3103 snd_hda_get_path_idx(codec
, path
);
3110 static int is_input_pin(struct hda_codec
*codec
, hda_nid_t nid
)
3112 unsigned int pincap
= snd_hda_query_pin_caps(codec
, nid
);
3113 return (pincap
& AC_PINCAP_IN
) != 0;
3116 /* Parse the codec tree and retrieve ADCs */
3117 static int fill_adc_nids(struct hda_codec
*codec
)
3119 struct hda_gen_spec
*spec
= codec
->spec
;
3121 hda_nid_t
*adc_nids
= spec
->adc_nids
;
3122 int max_nums
= ARRAY_SIZE(spec
->adc_nids
);
3125 for_each_hda_codec_node(nid
, codec
) {
3126 unsigned int caps
= get_wcaps(codec
, nid
);
3127 int type
= get_wcaps_type(caps
);
3129 if (type
!= AC_WID_AUD_IN
|| (caps
& AC_WCAP_DIGITAL
))
3131 adc_nids
[nums
] = nid
;
3132 if (++nums
>= max_nums
)
3135 spec
->num_adc_nids
= nums
;
3137 /* copy the detected ADCs to all_adcs[] */
3138 spec
->num_all_adcs
= nums
;
3139 memcpy(spec
->all_adcs
, spec
->adc_nids
, nums
* sizeof(hda_nid_t
));
3144 /* filter out invalid adc_nids that don't give all active input pins;
3145 * if needed, check whether dynamic ADC-switching is available
3147 static int check_dyn_adc_switch(struct hda_codec
*codec
)
3149 struct hda_gen_spec
*spec
= codec
->spec
;
3150 struct hda_input_mux
*imux
= &spec
->input_mux
;
3151 unsigned int ok_bits
;
3156 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
3157 for (i
= 0; i
< imux
->num_items
; i
++) {
3158 if (!spec
->input_paths
[i
][n
])
3161 if (i
>= imux
->num_items
) {
3162 ok_bits
|= (1 << n
);
3168 /* check whether ADC-switch is possible */
3169 for (i
= 0; i
< imux
->num_items
; i
++) {
3170 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
3171 if (spec
->input_paths
[i
][n
]) {
3172 spec
->dyn_adc_idx
[i
] = n
;
3178 codec_dbg(codec
, "enabling ADC switching\n");
3179 spec
->dyn_adc_switch
= 1;
3180 } else if (nums
!= spec
->num_adc_nids
) {
3181 /* shrink the invalid adcs and input paths */
3183 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
3184 if (!(ok_bits
& (1 << n
)))
3187 spec
->adc_nids
[nums
] = spec
->adc_nids
[n
];
3188 for (i
= 0; i
< imux
->num_items
; i
++) {
3189 invalidate_nid_path(codec
,
3190 spec
->input_paths
[i
][nums
]);
3191 spec
->input_paths
[i
][nums
] =
3192 spec
->input_paths
[i
][n
];
3193 spec
->input_paths
[i
][n
] = 0;
3198 spec
->num_adc_nids
= nums
;
3201 if (imux
->num_items
== 1 ||
3202 (imux
->num_items
== 2 && spec
->hp_mic
)) {
3203 codec_dbg(codec
, "reducing to a single ADC\n");
3204 spec
->num_adc_nids
= 1; /* reduce to a single ADC */
3207 /* single index for individual volumes ctls */
3208 if (!spec
->dyn_adc_switch
&& spec
->multi_cap_vol
)
3209 spec
->num_adc_nids
= 1;
3214 /* parse capture source paths from the given pin and create imux items */
3215 static int parse_capture_source(struct hda_codec
*codec
, hda_nid_t pin
,
3216 int cfg_idx
, int num_adcs
,
3217 const char *label
, int anchor
)
3219 struct hda_gen_spec
*spec
= codec
->spec
;
3220 struct hda_input_mux
*imux
= &spec
->input_mux
;
3221 int imux_idx
= imux
->num_items
;
3222 bool imux_added
= false;
3225 for (c
= 0; c
< num_adcs
; c
++) {
3226 struct nid_path
*path
;
3227 hda_nid_t adc
= spec
->adc_nids
[c
];
3229 if (!is_reachable_path(codec
, pin
, adc
))
3231 path
= snd_hda_add_new_path(codec
, pin
, adc
, anchor
);
3234 print_nid_path(codec
, "input", path
);
3235 spec
->input_paths
[imux_idx
][c
] =
3236 snd_hda_get_path_idx(codec
, path
);
3239 if (spec
->hp_mic_pin
== pin
)
3240 spec
->hp_mic_mux_idx
= imux
->num_items
;
3241 spec
->imux_pins
[imux
->num_items
] = pin
;
3242 snd_hda_add_imux_item(codec
, imux
, label
, cfg_idx
, NULL
);
3244 if (spec
->dyn_adc_switch
)
3245 spec
->dyn_adc_idx
[imux_idx
] = c
;
3253 * create playback/capture controls for input pins
3256 /* fill the label for each input at first */
3257 static int fill_input_pin_labels(struct hda_codec
*codec
)
3259 struct hda_gen_spec
*spec
= codec
->spec
;
3260 const struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3263 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
3264 hda_nid_t pin
= cfg
->inputs
[i
].pin
;
3268 if (!is_input_pin(codec
, pin
))
3271 label
= hda_get_autocfg_input_label(codec
, cfg
, i
);
3273 for (j
= i
- 1; j
>= 0; j
--) {
3274 if (spec
->input_labels
[j
] &&
3275 !strcmp(spec
->input_labels
[j
], label
)) {
3276 idx
= spec
->input_label_idxs
[j
] + 1;
3281 spec
->input_labels
[i
] = label
;
3282 spec
->input_label_idxs
[i
] = idx
;
3288 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3290 static int create_input_ctls(struct hda_codec
*codec
)
3292 struct hda_gen_spec
*spec
= codec
->spec
;
3293 const struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3294 hda_nid_t mixer
= spec
->mixer_nid
;
3299 num_adcs
= fill_adc_nids(codec
);
3303 err
= fill_input_pin_labels(codec
);
3307 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
3310 pin
= cfg
->inputs
[i
].pin
;
3311 if (!is_input_pin(codec
, pin
))
3315 if (cfg
->inputs
[i
].type
== AUTO_PIN_MIC
)
3316 val
|= snd_hda_get_default_vref(codec
, pin
);
3317 if (pin
!= spec
->hp_mic_pin
&&
3318 !snd_hda_codec_get_pin_target(codec
, pin
))
3319 set_pin_target(codec
, pin
, val
, false);
3322 if (is_reachable_path(codec
, pin
, mixer
)) {
3323 err
= new_analog_input(codec
, i
, pin
,
3324 spec
->input_labels
[i
],
3325 spec
->input_label_idxs
[i
],
3332 err
= parse_capture_source(codec
, pin
, i
, num_adcs
,
3333 spec
->input_labels
[i
], -mixer
);
3337 if (spec
->add_jack_modes
) {
3338 err
= create_in_jack_mode(codec
, pin
);
3344 /* add stereo mix when explicitly enabled via hint */
3345 if (mixer
&& spec
->add_stereo_mix_input
== HDA_HINT_STEREO_MIX_ENABLE
) {
3346 err
= parse_capture_source(codec
, mixer
, CFG_IDX_MIX
, num_adcs
,
3351 spec
->suppress_auto_mic
= 1;
3362 /* get the input path specified by the given adc and imux indices */
3363 static struct nid_path
*get_input_path(struct hda_codec
*codec
, int adc_idx
, int imux_idx
)
3365 struct hda_gen_spec
*spec
= codec
->spec
;
3366 if (imux_idx
< 0 || imux_idx
>= HDA_MAX_NUM_INPUTS
) {
3370 if (spec
->dyn_adc_switch
)
3371 adc_idx
= spec
->dyn_adc_idx
[imux_idx
];
3372 if (adc_idx
< 0 || adc_idx
>= AUTO_CFG_MAX_INS
) {
3376 return snd_hda_get_path_from_idx(codec
, spec
->input_paths
[imux_idx
][adc_idx
]);
3379 static int mux_select(struct hda_codec
*codec
, unsigned int adc_idx
,
3382 static int mux_enum_info(struct snd_kcontrol
*kcontrol
,
3383 struct snd_ctl_elem_info
*uinfo
)
3385 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3386 struct hda_gen_spec
*spec
= codec
->spec
;
3387 return snd_hda_input_mux_info(&spec
->input_mux
, uinfo
);
3390 static int mux_enum_get(struct snd_kcontrol
*kcontrol
,
3391 struct snd_ctl_elem_value
*ucontrol
)
3393 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3394 struct hda_gen_spec
*spec
= codec
->spec
;
3395 /* the ctls are created at once with multiple counts */
3396 unsigned int adc_idx
= snd_ctl_get_ioffidx(kcontrol
, &ucontrol
->id
);
3398 ucontrol
->value
.enumerated
.item
[0] = spec
->cur_mux
[adc_idx
];
3402 static int mux_enum_put(struct snd_kcontrol
*kcontrol
,
3403 struct snd_ctl_elem_value
*ucontrol
)
3405 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3406 unsigned int adc_idx
= snd_ctl_get_ioffidx(kcontrol
, &ucontrol
->id
);
3407 return mux_select(codec
, adc_idx
,
3408 ucontrol
->value
.enumerated
.item
[0]);
3411 static const struct snd_kcontrol_new cap_src_temp
= {
3412 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3413 .name
= "Input Source",
3414 .info
= mux_enum_info
,
3415 .get
= mux_enum_get
,
3416 .put
= mux_enum_put
,
3420 * capture volume and capture switch ctls
3423 typedef int (*put_call_t
)(struct snd_kcontrol
*kcontrol
,
3424 struct snd_ctl_elem_value
*ucontrol
);
3426 /* call the given amp update function for all amps in the imux list at once */
3427 static int cap_put_caller(struct snd_kcontrol
*kcontrol
,
3428 struct snd_ctl_elem_value
*ucontrol
,
3429 put_call_t func
, int type
)
3431 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3432 struct hda_gen_spec
*spec
= codec
->spec
;
3433 const struct hda_input_mux
*imux
;
3434 struct nid_path
*path
;
3435 int i
, adc_idx
, err
= 0;
3437 imux
= &spec
->input_mux
;
3438 adc_idx
= kcontrol
->id
.index
;
3439 mutex_lock(&codec
->control_mutex
);
3440 for (i
= 0; i
< imux
->num_items
; i
++) {
3441 path
= get_input_path(codec
, adc_idx
, i
);
3442 if (!path
|| !path
->ctls
[type
])
3444 kcontrol
->private_value
= path
->ctls
[type
];
3445 err
= func(kcontrol
, ucontrol
);
3449 mutex_unlock(&codec
->control_mutex
);
3450 if (err
>= 0 && spec
->cap_sync_hook
)
3451 spec
->cap_sync_hook(codec
, kcontrol
, ucontrol
);
3455 /* capture volume ctl callbacks */
3456 #define cap_vol_info snd_hda_mixer_amp_volume_info
3457 #define cap_vol_get snd_hda_mixer_amp_volume_get
3458 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3460 static int cap_vol_put(struct snd_kcontrol
*kcontrol
,
3461 struct snd_ctl_elem_value
*ucontrol
)
3463 return cap_put_caller(kcontrol
, ucontrol
,
3464 snd_hda_mixer_amp_volume_put
,
3468 static const struct snd_kcontrol_new cap_vol_temp
= {
3469 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3470 .name
= "Capture Volume",
3471 .access
= (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
3472 SNDRV_CTL_ELEM_ACCESS_TLV_READ
|
3473 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
),
3474 .info
= cap_vol_info
,
3477 .tlv
= { .c
= cap_vol_tlv
},
3480 /* capture switch ctl callbacks */
3481 #define cap_sw_info snd_ctl_boolean_stereo_info
3482 #define cap_sw_get snd_hda_mixer_amp_switch_get
3484 static int cap_sw_put(struct snd_kcontrol
*kcontrol
,
3485 struct snd_ctl_elem_value
*ucontrol
)
3487 return cap_put_caller(kcontrol
, ucontrol
,
3488 snd_hda_mixer_amp_switch_put
,
3492 static const struct snd_kcontrol_new cap_sw_temp
= {
3493 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3494 .name
= "Capture Switch",
3495 .info
= cap_sw_info
,
3500 static int parse_capvol_in_path(struct hda_codec
*codec
, struct nid_path
*path
)
3505 path
->ctls
[NID_PATH_VOL_CTL
] = path
->ctls
[NID_PATH_MUTE_CTL
] = 0;
3506 for (depth
= 0; depth
< 3; depth
++) {
3507 if (depth
>= path
->depth
)
3509 i
= path
->depth
- depth
- 1;
3510 nid
= path
->path
[i
];
3511 if (!path
->ctls
[NID_PATH_VOL_CTL
]) {
3512 if (nid_has_volume(codec
, nid
, HDA_OUTPUT
))
3513 path
->ctls
[NID_PATH_VOL_CTL
] =
3514 HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3515 else if (nid_has_volume(codec
, nid
, HDA_INPUT
)) {
3516 int idx
= path
->idx
[i
];
3517 if (!depth
&& codec
->single_adc_amp
)
3519 path
->ctls
[NID_PATH_VOL_CTL
] =
3520 HDA_COMPOSE_AMP_VAL(nid
, 3, idx
, HDA_INPUT
);
3523 if (!path
->ctls
[NID_PATH_MUTE_CTL
]) {
3524 if (nid_has_mute(codec
, nid
, HDA_OUTPUT
))
3525 path
->ctls
[NID_PATH_MUTE_CTL
] =
3526 HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3527 else if (nid_has_mute(codec
, nid
, HDA_INPUT
)) {
3528 int idx
= path
->idx
[i
];
3529 if (!depth
&& codec
->single_adc_amp
)
3531 path
->ctls
[NID_PATH_MUTE_CTL
] =
3532 HDA_COMPOSE_AMP_VAL(nid
, 3, idx
, HDA_INPUT
);
3539 static bool is_inv_dmic_pin(struct hda_codec
*codec
, hda_nid_t nid
)
3541 struct hda_gen_spec
*spec
= codec
->spec
;
3542 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3546 if (!spec
->inv_dmic_split
)
3548 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
3549 if (cfg
->inputs
[i
].pin
!= nid
)
3551 if (cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
3553 val
= snd_hda_codec_get_pincfg(codec
, nid
);
3554 return snd_hda_get_input_pin_attr(val
) == INPUT_PIN_ATTR_INT
;
3559 /* capture switch put callback for a single control with hook call */
3560 static int cap_single_sw_put(struct snd_kcontrol
*kcontrol
,
3561 struct snd_ctl_elem_value
*ucontrol
)
3563 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3564 struct hda_gen_spec
*spec
= codec
->spec
;
3567 ret
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
3571 if (spec
->cap_sync_hook
)
3572 spec
->cap_sync_hook(codec
, kcontrol
, ucontrol
);
3577 static int add_single_cap_ctl(struct hda_codec
*codec
, const char *label
,
3578 int idx
, bool is_switch
, unsigned int ctl
,
3581 struct hda_gen_spec
*spec
= codec
->spec
;
3582 char tmpname
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
3583 int type
= is_switch
? HDA_CTL_WIDGET_MUTE
: HDA_CTL_WIDGET_VOL
;
3584 const char *sfx
= is_switch
? "Switch" : "Volume";
3585 unsigned int chs
= inv_dmic
? 1 : 3;
3586 struct snd_kcontrol_new
*knew
;
3592 snprintf(tmpname
, sizeof(tmpname
),
3593 "%s Capture %s", label
, sfx
);
3595 snprintf(tmpname
, sizeof(tmpname
),
3597 knew
= add_control(spec
, type
, tmpname
, idx
,
3598 amp_val_replace_channels(ctl
, chs
));
3602 knew
->put
= cap_single_sw_put
;
3606 /* Make independent right kcontrol */
3608 snprintf(tmpname
, sizeof(tmpname
),
3609 "Inverted %s Capture %s", label
, sfx
);
3611 snprintf(tmpname
, sizeof(tmpname
),
3612 "Inverted Capture %s", sfx
);
3613 knew
= add_control(spec
, type
, tmpname
, idx
,
3614 amp_val_replace_channels(ctl
, 2));
3618 knew
->put
= cap_single_sw_put
;
3622 /* create single (and simple) capture volume and switch controls */
3623 static int create_single_cap_vol_ctl(struct hda_codec
*codec
, int idx
,
3624 unsigned int vol_ctl
, unsigned int sw_ctl
,
3628 err
= add_single_cap_ctl(codec
, NULL
, idx
, false, vol_ctl
, inv_dmic
);
3631 err
= add_single_cap_ctl(codec
, NULL
, idx
, true, sw_ctl
, inv_dmic
);
3637 /* create bound capture volume and switch controls */
3638 static int create_bind_cap_vol_ctl(struct hda_codec
*codec
, int idx
,
3639 unsigned int vol_ctl
, unsigned int sw_ctl
)
3641 struct hda_gen_spec
*spec
= codec
->spec
;
3642 struct snd_kcontrol_new
*knew
;
3645 knew
= snd_hda_gen_add_kctl(spec
, NULL
, &cap_vol_temp
);
3649 knew
->private_value
= vol_ctl
;
3650 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
3653 knew
= snd_hda_gen_add_kctl(spec
, NULL
, &cap_sw_temp
);
3657 knew
->private_value
= sw_ctl
;
3658 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
3663 /* return the vol ctl when used first in the imux list */
3664 static unsigned int get_first_cap_ctl(struct hda_codec
*codec
, int idx
, int type
)
3666 struct nid_path
*path
;
3670 path
= get_input_path(codec
, 0, idx
);
3673 ctl
= path
->ctls
[type
];
3676 for (i
= 0; i
< idx
- 1; i
++) {
3677 path
= get_input_path(codec
, 0, i
);
3678 if (path
&& path
->ctls
[type
] == ctl
)
3684 /* create individual capture volume and switch controls per input */
3685 static int create_multi_cap_vol_ctl(struct hda_codec
*codec
)
3687 struct hda_gen_spec
*spec
= codec
->spec
;
3688 struct hda_input_mux
*imux
= &spec
->input_mux
;
3691 for (i
= 0; i
< imux
->num_items
; i
++) {
3695 idx
= imux
->items
[i
].index
;
3696 if (idx
>= spec
->autocfg
.num_inputs
)
3698 inv_dmic
= is_inv_dmic_pin(codec
, spec
->imux_pins
[i
]);
3700 for (type
= 0; type
< 2; type
++) {
3701 err
= add_single_cap_ctl(codec
,
3702 spec
->input_labels
[idx
],
3703 spec
->input_label_idxs
[idx
],
3705 get_first_cap_ctl(codec
, i
, type
),
3714 static int create_capture_mixers(struct hda_codec
*codec
)
3716 struct hda_gen_spec
*spec
= codec
->spec
;
3717 struct hda_input_mux
*imux
= &spec
->input_mux
;
3718 int i
, n
, nums
, err
;
3720 if (spec
->dyn_adc_switch
)
3723 nums
= spec
->num_adc_nids
;
3725 if (!spec
->auto_mic
&& imux
->num_items
> 1) {
3726 struct snd_kcontrol_new
*knew
;
3728 name
= nums
> 1 ? "Input Source" : "Capture Source";
3729 knew
= snd_hda_gen_add_kctl(spec
, name
, &cap_src_temp
);
3735 for (n
= 0; n
< nums
; n
++) {
3737 bool multi_cap_vol
= spec
->multi_cap_vol
;
3738 bool inv_dmic
= false;
3742 for (i
= 0; i
< imux
->num_items
; i
++) {
3743 struct nid_path
*path
;
3744 path
= get_input_path(codec
, n
, i
);
3747 parse_capvol_in_path(codec
, path
);
3749 vol
= path
->ctls
[NID_PATH_VOL_CTL
];
3750 else if (vol
!= path
->ctls
[NID_PATH_VOL_CTL
]) {
3752 if (!same_amp_caps(codec
, vol
,
3753 path
->ctls
[NID_PATH_VOL_CTL
], HDA_INPUT
))
3754 multi_cap_vol
= true;
3757 sw
= path
->ctls
[NID_PATH_MUTE_CTL
];
3758 else if (sw
!= path
->ctls
[NID_PATH_MUTE_CTL
]) {
3760 if (!same_amp_caps(codec
, sw
,
3761 path
->ctls
[NID_PATH_MUTE_CTL
], HDA_INPUT
))
3762 multi_cap_vol
= true;
3764 if (is_inv_dmic_pin(codec
, spec
->imux_pins
[i
]))
3769 err
= create_single_cap_vol_ctl(codec
, n
, vol
, sw
,
3771 else if (!multi_cap_vol
&& !inv_dmic
)
3772 err
= create_bind_cap_vol_ctl(codec
, n
, vol
, sw
);
3774 err
= create_multi_cap_vol_ctl(codec
);
3783 * add mic boosts if needed
3786 /* check whether the given amp is feasible as a boost volume */
3787 static bool check_boost_vol(struct hda_codec
*codec
, hda_nid_t nid
,
3792 if (!nid_has_volume(codec
, nid
, dir
) ||
3793 is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_VOL_CTL
) ||
3794 is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_BOOST_CTL
))
3797 step
= (query_amp_caps(codec
, nid
, dir
) & AC_AMPCAP_STEP_SIZE
)
3798 >> AC_AMPCAP_STEP_SIZE_SHIFT
;
3804 /* look for a boost amp in a widget close to the pin */
3805 static unsigned int look_for_boost_amp(struct hda_codec
*codec
,
3806 struct nid_path
*path
)
3808 unsigned int val
= 0;
3812 for (depth
= 0; depth
< 3; depth
++) {
3813 if (depth
>= path
->depth
- 1)
3815 nid
= path
->path
[depth
];
3816 if (depth
&& check_boost_vol(codec
, nid
, HDA_OUTPUT
, 0)) {
3817 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3819 } else if (check_boost_vol(codec
, nid
, HDA_INPUT
,
3820 path
->idx
[depth
])) {
3821 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, path
->idx
[depth
],
3830 static int parse_mic_boost(struct hda_codec
*codec
)
3832 struct hda_gen_spec
*spec
= codec
->spec
;
3833 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3834 struct hda_input_mux
*imux
= &spec
->input_mux
;
3837 if (!spec
->num_adc_nids
)
3840 for (i
= 0; i
< imux
->num_items
; i
++) {
3841 struct nid_path
*path
;
3844 char boost_label
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
3846 idx
= imux
->items
[i
].index
;
3847 if (idx
>= imux
->num_items
)
3850 /* check only line-in and mic pins */
3851 if (cfg
->inputs
[idx
].type
> AUTO_PIN_LINE_IN
)
3854 path
= get_input_path(codec
, 0, i
);
3858 val
= look_for_boost_amp(codec
, path
);
3862 /* create a boost control */
3863 snprintf(boost_label
, sizeof(boost_label
),
3864 "%s Boost Volume", spec
->input_labels
[idx
]);
3865 if (!add_control(spec
, HDA_CTL_WIDGET_VOL
, boost_label
,
3866 spec
->input_label_idxs
[idx
], val
))
3869 path
->ctls
[NID_PATH_BOOST_CTL
] = val
;
3875 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3877 static void parse_digital(struct hda_codec
*codec
)
3879 struct hda_gen_spec
*spec
= codec
->spec
;
3880 struct nid_path
*path
;
3882 hda_nid_t dig_nid
, pin
;
3884 /* support multiple SPDIFs; the secondary is set up as a slave */
3886 for (i
= 0; i
< spec
->autocfg
.dig_outs
; i
++) {
3887 pin
= spec
->autocfg
.dig_out_pins
[i
];
3888 dig_nid
= look_for_dac(codec
, pin
, true);
3891 path
= snd_hda_add_new_path(codec
, dig_nid
, pin
, 0);
3894 print_nid_path(codec
, "digout", path
);
3895 path
->active
= true;
3896 path
->pin_fixed
= true; /* no jack detection */
3897 spec
->digout_paths
[i
] = snd_hda_get_path_idx(codec
, path
);
3898 set_pin_target(codec
, pin
, PIN_OUT
, false);
3900 spec
->multiout
.dig_out_nid
= dig_nid
;
3901 spec
->dig_out_type
= spec
->autocfg
.dig_out_type
[0];
3903 spec
->multiout
.slave_dig_outs
= spec
->slave_dig_outs
;
3904 if (nums
>= ARRAY_SIZE(spec
->slave_dig_outs
) - 1)
3906 spec
->slave_dig_outs
[nums
- 1] = dig_nid
;
3911 if (spec
->autocfg
.dig_in_pin
) {
3912 pin
= spec
->autocfg
.dig_in_pin
;
3913 for_each_hda_codec_node(dig_nid
, codec
) {
3914 unsigned int wcaps
= get_wcaps(codec
, dig_nid
);
3915 if (get_wcaps_type(wcaps
) != AC_WID_AUD_IN
)
3917 if (!(wcaps
& AC_WCAP_DIGITAL
))
3919 path
= snd_hda_add_new_path(codec
, pin
, dig_nid
, 0);
3921 print_nid_path(codec
, "digin", path
);
3922 path
->active
= true;
3923 path
->pin_fixed
= true; /* no jack */
3924 spec
->dig_in_nid
= dig_nid
;
3925 spec
->digin_path
= snd_hda_get_path_idx(codec
, path
);
3926 set_pin_target(codec
, pin
, PIN_IN
, false);
3935 * input MUX handling
3938 static bool dyn_adc_pcm_resetup(struct hda_codec
*codec
, int cur
);
3940 /* select the given imux item; either unmute exclusively or select the route */
3941 static int mux_select(struct hda_codec
*codec
, unsigned int adc_idx
,
3944 struct hda_gen_spec
*spec
= codec
->spec
;
3945 const struct hda_input_mux
*imux
;
3946 struct nid_path
*old_path
, *path
;
3948 imux
= &spec
->input_mux
;
3949 if (!imux
->num_items
)
3952 if (idx
>= imux
->num_items
)
3953 idx
= imux
->num_items
- 1;
3954 if (spec
->cur_mux
[adc_idx
] == idx
)
3957 old_path
= get_input_path(codec
, adc_idx
, spec
->cur_mux
[adc_idx
]);
3960 if (old_path
->active
)
3961 snd_hda_activate_path(codec
, old_path
, false, false);
3963 spec
->cur_mux
[adc_idx
] = idx
;
3966 update_hp_mic(codec
, adc_idx
, false);
3968 if (spec
->dyn_adc_switch
)
3969 dyn_adc_pcm_resetup(codec
, idx
);
3971 path
= get_input_path(codec
, adc_idx
, idx
);
3976 snd_hda_activate_path(codec
, path
, true, false);
3977 if (spec
->cap_sync_hook
)
3978 spec
->cap_sync_hook(codec
, NULL
, NULL
);
3979 path_power_down_sync(codec
, old_path
);
3983 /* power up/down widgets in the all paths that match with the given NID
3984 * as terminals (either start- or endpoint)
3986 * returns the last changed NID, or zero if unchanged.
3988 static hda_nid_t
set_path_power(struct hda_codec
*codec
, hda_nid_t nid
,
3989 int pin_state
, int stream_state
)
3991 struct hda_gen_spec
*spec
= codec
->spec
;
3992 hda_nid_t last
, changed
= 0;
3993 struct nid_path
*path
;
3996 for (n
= 0; n
< spec
->paths
.used
; n
++) {
3997 path
= snd_array_elem(&spec
->paths
, n
);
4000 if (path
->path
[0] == nid
||
4001 path
->path
[path
->depth
- 1] == nid
) {
4002 bool pin_old
= path
->pin_enabled
;
4003 bool stream_old
= path
->stream_enabled
;
4006 path
->pin_enabled
= pin_state
;
4007 if (stream_state
>= 0)
4008 path
->stream_enabled
= stream_state
;
4009 if ((!path
->pin_fixed
&& path
->pin_enabled
!= pin_old
)
4010 || path
->stream_enabled
!= stream_old
) {
4011 last
= path_power_update(codec
, path
, true);
4020 /* check the jack status for power control */
4021 static bool detect_pin_state(struct hda_codec
*codec
, hda_nid_t pin
)
4023 if (!is_jack_detectable(codec
, pin
))
4025 return snd_hda_jack_detect_state(codec
, pin
) != HDA_JACK_NOT_PRESENT
;
4028 /* power up/down the paths of the given pin according to the jack state;
4029 * power = 0/1 : only power up/down if it matches with the jack state,
4030 * < 0 : force power up/down to follow the jack sate
4032 * returns the last changed NID, or zero if unchanged.
4034 static hda_nid_t
set_pin_power_jack(struct hda_codec
*codec
, hda_nid_t pin
,
4039 if (!codec
->power_save_node
)
4042 on
= detect_pin_state(codec
, pin
);
4044 if (power
>= 0 && on
!= power
)
4046 return set_path_power(codec
, pin
, on
, -1);
4049 static void pin_power_callback(struct hda_codec
*codec
,
4050 struct hda_jack_callback
*jack
,
4053 if (jack
&& jack
->nid
)
4054 sync_power_state_change(codec
,
4055 set_pin_power_jack(codec
, jack
->nid
, on
));
4058 /* callback only doing power up -- called at first */
4059 static void pin_power_up_callback(struct hda_codec
*codec
,
4060 struct hda_jack_callback
*jack
)
4062 pin_power_callback(codec
, jack
, true);
4065 /* callback only doing power down -- called at last */
4066 static void pin_power_down_callback(struct hda_codec
*codec
,
4067 struct hda_jack_callback
*jack
)
4069 pin_power_callback(codec
, jack
, false);
4072 /* set up the power up/down callbacks */
4073 static void add_pin_power_ctls(struct hda_codec
*codec
, int num_pins
,
4074 const hda_nid_t
*pins
, bool on
)
4077 hda_jack_callback_fn cb
=
4078 on
? pin_power_up_callback
: pin_power_down_callback
;
4080 for (i
= 0; i
< num_pins
&& pins
[i
]; i
++) {
4081 if (is_jack_detectable(codec
, pins
[i
]))
4082 snd_hda_jack_detect_enable_callback(codec
, pins
[i
], cb
);
4084 set_path_power(codec
, pins
[i
], true, -1);
4088 /* enabled power callback to each available I/O pin with jack detections;
4089 * the digital I/O pins are excluded because of the unreliable detectsion
4091 static void add_all_pin_power_ctls(struct hda_codec
*codec
, bool on
)
4093 struct hda_gen_spec
*spec
= codec
->spec
;
4094 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4097 if (!codec
->power_save_node
)
4099 add_pin_power_ctls(codec
, cfg
->line_outs
, cfg
->line_out_pins
, on
);
4100 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
4101 add_pin_power_ctls(codec
, cfg
->hp_outs
, cfg
->hp_pins
, on
);
4102 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
4103 add_pin_power_ctls(codec
, cfg
->speaker_outs
, cfg
->speaker_pins
, on
);
4104 for (i
= 0; i
< cfg
->num_inputs
; i
++)
4105 add_pin_power_ctls(codec
, 1, &cfg
->inputs
[i
].pin
, on
);
4108 /* sync path power up/down with the jack states of given pins */
4109 static void sync_pin_power_ctls(struct hda_codec
*codec
, int num_pins
,
4110 const hda_nid_t
*pins
)
4114 for (i
= 0; i
< num_pins
&& pins
[i
]; i
++)
4115 if (is_jack_detectable(codec
, pins
[i
]))
4116 set_pin_power_jack(codec
, pins
[i
], -1);
4119 /* sync path power up/down with pins; called at init and resume */
4120 static void sync_all_pin_power_ctls(struct hda_codec
*codec
)
4122 struct hda_gen_spec
*spec
= codec
->spec
;
4123 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4126 if (!codec
->power_save_node
)
4128 sync_pin_power_ctls(codec
, cfg
->line_outs
, cfg
->line_out_pins
);
4129 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
4130 sync_pin_power_ctls(codec
, cfg
->hp_outs
, cfg
->hp_pins
);
4131 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
4132 sync_pin_power_ctls(codec
, cfg
->speaker_outs
, cfg
->speaker_pins
);
4133 for (i
= 0; i
< cfg
->num_inputs
; i
++)
4134 sync_pin_power_ctls(codec
, 1, &cfg
->inputs
[i
].pin
);
4137 /* add fake paths if not present yet */
4138 static int add_fake_paths(struct hda_codec
*codec
, hda_nid_t nid
,
4139 int num_pins
, const hda_nid_t
*pins
)
4141 struct hda_gen_spec
*spec
= codec
->spec
;
4142 struct nid_path
*path
;
4145 for (i
= 0; i
< num_pins
; i
++) {
4148 if (get_nid_path(codec
, nid
, pins
[i
], 0))
4150 path
= snd_array_new(&spec
->paths
);
4153 memset(path
, 0, sizeof(*path
));
4155 path
->path
[0] = nid
;
4156 path
->path
[1] = pins
[i
];
4157 path
->active
= true;
4162 /* create fake paths to all outputs from beep */
4163 static int add_fake_beep_paths(struct hda_codec
*codec
)
4165 struct hda_gen_spec
*spec
= codec
->spec
;
4166 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4167 hda_nid_t nid
= spec
->beep_nid
;
4170 if (!codec
->power_save_node
|| !nid
)
4172 err
= add_fake_paths(codec
, nid
, cfg
->line_outs
, cfg
->line_out_pins
);
4175 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
) {
4176 err
= add_fake_paths(codec
, nid
, cfg
->hp_outs
, cfg
->hp_pins
);
4180 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
4181 err
= add_fake_paths(codec
, nid
, cfg
->speaker_outs
,
4189 /* power up/down beep widget and its output paths */
4190 static void beep_power_hook(struct hda_beep
*beep
, bool on
)
4192 set_path_power(beep
->codec
, beep
->nid
, -1, on
);
4196 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4197 * @codec: the HDA codec
4198 * @pin: NID of pin to fix
4200 int snd_hda_gen_fix_pin_power(struct hda_codec
*codec
, hda_nid_t pin
)
4202 struct hda_gen_spec
*spec
= codec
->spec
;
4203 struct nid_path
*path
;
4205 path
= snd_array_new(&spec
->paths
);
4208 memset(path
, 0, sizeof(*path
));
4210 path
->path
[0] = pin
;
4211 path
->active
= true;
4212 path
->pin_fixed
= true;
4213 path
->stream_enabled
= true;
4216 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power
);
4219 * Jack detections for HP auto-mute and mic-switch
4222 /* check each pin in the given array; returns true if any of them is plugged */
4223 static bool detect_jacks(struct hda_codec
*codec
, int num_pins
, hda_nid_t
*pins
)
4226 bool present
= false;
4228 for (i
= 0; i
< num_pins
; i
++) {
4229 hda_nid_t nid
= pins
[i
];
4232 /* don't detect pins retasked as inputs */
4233 if (snd_hda_codec_get_pin_target(codec
, nid
) & AC_PINCTL_IN_EN
)
4235 if (snd_hda_jack_detect_state(codec
, nid
) == HDA_JACK_PRESENT
)
4241 /* standard HP/line-out auto-mute helper */
4242 static void do_automute(struct hda_codec
*codec
, int num_pins
, hda_nid_t
*pins
,
4243 int *paths
, bool mute
)
4245 struct hda_gen_spec
*spec
= codec
->spec
;
4248 for (i
= 0; i
< num_pins
; i
++) {
4249 hda_nid_t nid
= pins
[i
];
4250 unsigned int val
, oldval
;
4254 oldval
= snd_hda_codec_get_pin_target(codec
, nid
);
4255 if (oldval
& PIN_IN
)
4256 continue; /* no mute for inputs */
4258 if (spec
->auto_mute_via_amp
) {
4259 struct nid_path
*path
;
4262 path
= snd_hda_get_path_from_idx(codec
, paths
[i
]);
4265 mute_nid
= get_amp_nid_(path
->ctls
[NID_PATH_MUTE_CTL
]);
4269 spec
->mute_bits
|= (1ULL << mute_nid
);
4271 spec
->mute_bits
&= ~(1ULL << mute_nid
);
4274 /* don't reset VREF value in case it's controlling
4275 * the amp (see alc861_fixup_asus_amp_vref_0f())
4277 if (spec
->keep_vref_in_automute
)
4278 val
= oldval
& ~PIN_HP
;
4283 /* here we call update_pin_ctl() so that the pinctl is
4284 * changed without changing the pinctl target value;
4285 * the original target value will be still referred at
4286 * the init / resume again
4288 update_pin_ctl(codec
, nid
, val
);
4291 set_pin_eapd(codec
, nid
, !mute
);
4292 if (codec
->power_save_node
) {
4295 on
= detect_pin_state(codec
, nid
);
4296 set_path_power(codec
, nid
, on
, -1);
4302 * snd_hda_gen_update_outputs - Toggle outputs muting
4303 * @codec: the HDA codec
4305 * Update the mute status of all outputs based on the current jack states.
4307 void snd_hda_gen_update_outputs(struct hda_codec
*codec
)
4309 struct hda_gen_spec
*spec
= codec
->spec
;
4313 /* Control HP pins/amps depending on master_mute state;
4314 * in general, HP pins/amps control should be enabled in all cases,
4315 * but currently set only for master_mute, just to be safe
4317 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
4318 paths
= spec
->out_paths
;
4320 paths
= spec
->hp_paths
;
4321 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.hp_pins
),
4322 spec
->autocfg
.hp_pins
, paths
, spec
->master_mute
);
4324 if (!spec
->automute_speaker
)
4327 on
= spec
->hp_jack_present
| spec
->line_jack_present
;
4328 on
|= spec
->master_mute
;
4329 spec
->speaker_muted
= on
;
4330 if (spec
->autocfg
.line_out_type
== AUTO_PIN_SPEAKER_OUT
)
4331 paths
= spec
->out_paths
;
4333 paths
= spec
->speaker_paths
;
4334 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.speaker_pins
),
4335 spec
->autocfg
.speaker_pins
, paths
, on
);
4337 /* toggle line-out mutes if needed, too */
4338 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4339 if (spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.hp_pins
[0] ||
4340 spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.speaker_pins
[0])
4342 if (!spec
->automute_lo
)
4345 on
= spec
->hp_jack_present
;
4346 on
|= spec
->master_mute
;
4347 spec
->line_out_muted
= on
;
4348 paths
= spec
->out_paths
;
4349 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.line_out_pins
),
4350 spec
->autocfg
.line_out_pins
, paths
, on
);
4352 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs
);
4354 static void call_update_outputs(struct hda_codec
*codec
)
4356 struct hda_gen_spec
*spec
= codec
->spec
;
4357 if (spec
->automute_hook
)
4358 spec
->automute_hook(codec
);
4360 snd_hda_gen_update_outputs(codec
);
4362 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4363 if (spec
->auto_mute_via_amp
&& !codec
->bus
->shutdown
)
4364 snd_ctl_sync_vmaster(spec
->vmaster_mute
.sw_kctl
, false);
4368 * snd_hda_gen_hp_automute - standard HP-automute helper
4369 * @codec: the HDA codec
4370 * @jack: jack object, NULL for the whole
4372 void snd_hda_gen_hp_automute(struct hda_codec
*codec
,
4373 struct hda_jack_callback
*jack
)
4375 struct hda_gen_spec
*spec
= codec
->spec
;
4376 hda_nid_t
*pins
= spec
->autocfg
.hp_pins
;
4377 int num_pins
= ARRAY_SIZE(spec
->autocfg
.hp_pins
);
4379 /* No detection for the first HP jack during indep-HP mode */
4380 if (spec
->indep_hp_enabled
) {
4385 spec
->hp_jack_present
= detect_jacks(codec
, num_pins
, pins
);
4386 if (!spec
->detect_hp
|| (!spec
->automute_speaker
&& !spec
->automute_lo
))
4388 call_update_outputs(codec
);
4390 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute
);
4393 * snd_hda_gen_line_automute - standard line-out-automute helper
4394 * @codec: the HDA codec
4395 * @jack: jack object, NULL for the whole
4397 void snd_hda_gen_line_automute(struct hda_codec
*codec
,
4398 struct hda_jack_callback
*jack
)
4400 struct hda_gen_spec
*spec
= codec
->spec
;
4402 if (spec
->autocfg
.line_out_type
== AUTO_PIN_SPEAKER_OUT
)
4404 /* check LO jack only when it's different from HP */
4405 if (spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.hp_pins
[0])
4408 spec
->line_jack_present
=
4409 detect_jacks(codec
, ARRAY_SIZE(spec
->autocfg
.line_out_pins
),
4410 spec
->autocfg
.line_out_pins
);
4411 if (!spec
->automute_speaker
|| !spec
->detect_lo
)
4413 call_update_outputs(codec
);
4415 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute
);
4418 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4419 * @codec: the HDA codec
4420 * @jack: jack object, NULL for the whole
4422 void snd_hda_gen_mic_autoswitch(struct hda_codec
*codec
,
4423 struct hda_jack_callback
*jack
)
4425 struct hda_gen_spec
*spec
= codec
->spec
;
4428 if (!spec
->auto_mic
)
4431 for (i
= spec
->am_num_entries
- 1; i
> 0; i
--) {
4432 hda_nid_t pin
= spec
->am_entry
[i
].pin
;
4433 /* don't detect pins retasked as outputs */
4434 if (snd_hda_codec_get_pin_target(codec
, pin
) & AC_PINCTL_OUT_EN
)
4436 if (snd_hda_jack_detect_state(codec
, pin
) == HDA_JACK_PRESENT
) {
4437 mux_select(codec
, 0, spec
->am_entry
[i
].idx
);
4441 mux_select(codec
, 0, spec
->am_entry
[0].idx
);
4443 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch
);
4445 /* call appropriate hooks */
4446 static void call_hp_automute(struct hda_codec
*codec
,
4447 struct hda_jack_callback
*jack
)
4449 struct hda_gen_spec
*spec
= codec
->spec
;
4450 if (spec
->hp_automute_hook
)
4451 spec
->hp_automute_hook(codec
, jack
);
4453 snd_hda_gen_hp_automute(codec
, jack
);
4456 static void call_line_automute(struct hda_codec
*codec
,
4457 struct hda_jack_callback
*jack
)
4459 struct hda_gen_spec
*spec
= codec
->spec
;
4460 if (spec
->line_automute_hook
)
4461 spec
->line_automute_hook(codec
, jack
);
4463 snd_hda_gen_line_automute(codec
, jack
);
4466 static void call_mic_autoswitch(struct hda_codec
*codec
,
4467 struct hda_jack_callback
*jack
)
4469 struct hda_gen_spec
*spec
= codec
->spec
;
4470 if (spec
->mic_autoswitch_hook
)
4471 spec
->mic_autoswitch_hook(codec
, jack
);
4473 snd_hda_gen_mic_autoswitch(codec
, jack
);
4476 /* update jack retasking */
4477 static void update_automute_all(struct hda_codec
*codec
)
4479 call_hp_automute(codec
, NULL
);
4480 call_line_automute(codec
, NULL
);
4481 call_mic_autoswitch(codec
, NULL
);
4485 * Auto-Mute mode mixer enum support
4487 static int automute_mode_info(struct snd_kcontrol
*kcontrol
,
4488 struct snd_ctl_elem_info
*uinfo
)
4490 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
4491 struct hda_gen_spec
*spec
= codec
->spec
;
4492 static const char * const texts3
[] = {
4493 "Disabled", "Speaker Only", "Line Out+Speaker"
4496 if (spec
->automute_speaker_possible
&& spec
->automute_lo_possible
)
4497 return snd_hda_enum_helper_info(kcontrol
, uinfo
, 3, texts3
);
4498 return snd_hda_enum_bool_helper_info(kcontrol
, uinfo
);
4501 static int automute_mode_get(struct snd_kcontrol
*kcontrol
,
4502 struct snd_ctl_elem_value
*ucontrol
)
4504 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
4505 struct hda_gen_spec
*spec
= codec
->spec
;
4506 unsigned int val
= 0;
4507 if (spec
->automute_speaker
)
4509 if (spec
->automute_lo
)
4512 ucontrol
->value
.enumerated
.item
[0] = val
;
4516 static int automute_mode_put(struct snd_kcontrol
*kcontrol
,
4517 struct snd_ctl_elem_value
*ucontrol
)
4519 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
4520 struct hda_gen_spec
*spec
= codec
->spec
;
4522 switch (ucontrol
->value
.enumerated
.item
[0]) {
4524 if (!spec
->automute_speaker
&& !spec
->automute_lo
)
4526 spec
->automute_speaker
= 0;
4527 spec
->automute_lo
= 0;
4530 if (spec
->automute_speaker_possible
) {
4531 if (!spec
->automute_lo
&& spec
->automute_speaker
)
4533 spec
->automute_speaker
= 1;
4534 spec
->automute_lo
= 0;
4535 } else if (spec
->automute_lo_possible
) {
4536 if (spec
->automute_lo
)
4538 spec
->automute_lo
= 1;
4543 if (!spec
->automute_lo_possible
|| !spec
->automute_speaker_possible
)
4545 if (spec
->automute_speaker
&& spec
->automute_lo
)
4547 spec
->automute_speaker
= 1;
4548 spec
->automute_lo
= 1;
4553 call_update_outputs(codec
);
4557 static const struct snd_kcontrol_new automute_mode_enum
= {
4558 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
4559 .name
= "Auto-Mute Mode",
4560 .info
= automute_mode_info
,
4561 .get
= automute_mode_get
,
4562 .put
= automute_mode_put
,
4565 static int add_automute_mode_enum(struct hda_codec
*codec
)
4567 struct hda_gen_spec
*spec
= codec
->spec
;
4569 if (!snd_hda_gen_add_kctl(spec
, NULL
, &automute_mode_enum
))
4575 * Check the availability of HP/line-out auto-mute;
4576 * Set up appropriately if really supported
4578 static int check_auto_mute_availability(struct hda_codec
*codec
)
4580 struct hda_gen_spec
*spec
= codec
->spec
;
4581 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4585 if (spec
->suppress_auto_mute
)
4588 if (cfg
->hp_pins
[0])
4590 if (cfg
->line_out_pins
[0])
4592 if (cfg
->speaker_pins
[0])
4594 if (present
< 2) /* need two different output types */
4597 if (!cfg
->speaker_pins
[0] &&
4598 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
) {
4599 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
4600 sizeof(cfg
->speaker_pins
));
4601 cfg
->speaker_outs
= cfg
->line_outs
;
4604 if (!cfg
->hp_pins
[0] &&
4605 cfg
->line_out_type
== AUTO_PIN_HP_OUT
) {
4606 memcpy(cfg
->hp_pins
, cfg
->line_out_pins
,
4607 sizeof(cfg
->hp_pins
));
4608 cfg
->hp_outs
= cfg
->line_outs
;
4611 for (i
= 0; i
< cfg
->hp_outs
; i
++) {
4612 hda_nid_t nid
= cfg
->hp_pins
[i
];
4613 if (!is_jack_detectable(codec
, nid
))
4615 codec_dbg(codec
, "Enable HP auto-muting on NID 0x%x\n", nid
);
4616 snd_hda_jack_detect_enable_callback(codec
, nid
,
4618 spec
->detect_hp
= 1;
4621 if (cfg
->line_out_type
== AUTO_PIN_LINE_OUT
&& cfg
->line_outs
) {
4622 if (cfg
->speaker_outs
)
4623 for (i
= 0; i
< cfg
->line_outs
; i
++) {
4624 hda_nid_t nid
= cfg
->line_out_pins
[i
];
4625 if (!is_jack_detectable(codec
, nid
))
4627 codec_dbg(codec
, "Enable Line-Out auto-muting on NID 0x%x\n", nid
);
4628 snd_hda_jack_detect_enable_callback(codec
, nid
,
4629 call_line_automute
);
4630 spec
->detect_lo
= 1;
4632 spec
->automute_lo_possible
= spec
->detect_hp
;
4635 spec
->automute_speaker_possible
= cfg
->speaker_outs
&&
4636 (spec
->detect_hp
|| spec
->detect_lo
);
4638 spec
->automute_lo
= spec
->automute_lo_possible
;
4639 spec
->automute_speaker
= spec
->automute_speaker_possible
;
4641 if (spec
->automute_speaker_possible
|| spec
->automute_lo_possible
) {
4642 /* create a control for automute mode */
4643 err
= add_automute_mode_enum(codec
);
4650 /* check whether all auto-mic pins are valid; setup indices if OK */
4651 static bool auto_mic_check_imux(struct hda_codec
*codec
)
4653 struct hda_gen_spec
*spec
= codec
->spec
;
4654 const struct hda_input_mux
*imux
;
4657 imux
= &spec
->input_mux
;
4658 for (i
= 0; i
< spec
->am_num_entries
; i
++) {
4659 spec
->am_entry
[i
].idx
=
4660 find_idx_in_nid_list(spec
->am_entry
[i
].pin
,
4661 spec
->imux_pins
, imux
->num_items
);
4662 if (spec
->am_entry
[i
].idx
< 0)
4663 return false; /* no corresponding imux */
4666 /* we don't need the jack detection for the first pin */
4667 for (i
= 1; i
< spec
->am_num_entries
; i
++)
4668 snd_hda_jack_detect_enable_callback(codec
,
4669 spec
->am_entry
[i
].pin
,
4670 call_mic_autoswitch
);
4674 static int compare_attr(const void *ap
, const void *bp
)
4676 const struct automic_entry
*a
= ap
;
4677 const struct automic_entry
*b
= bp
;
4678 return (int)(a
->attr
- b
->attr
);
4682 * Check the availability of auto-mic switch;
4683 * Set up if really supported
4685 static int check_auto_mic_availability(struct hda_codec
*codec
)
4687 struct hda_gen_spec
*spec
= codec
->spec
;
4688 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4692 if (spec
->suppress_auto_mic
)
4697 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
4698 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
4700 attr
= snd_hda_codec_get_pincfg(codec
, nid
);
4701 attr
= snd_hda_get_input_pin_attr(attr
);
4702 if (types
& (1 << attr
))
4703 return 0; /* already occupied */
4705 case INPUT_PIN_ATTR_INT
:
4706 if (cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
4707 return 0; /* invalid type */
4709 case INPUT_PIN_ATTR_UNUSED
:
4710 return 0; /* invalid entry */
4712 if (cfg
->inputs
[i
].type
> AUTO_PIN_LINE_IN
)
4713 return 0; /* invalid type */
4714 if (!spec
->line_in_auto_switch
&&
4715 cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
4716 return 0; /* only mic is allowed */
4717 if (!is_jack_detectable(codec
, nid
))
4718 return 0; /* no unsol support */
4721 if (num_pins
>= MAX_AUTO_MIC_PINS
)
4723 types
|= (1 << attr
);
4724 spec
->am_entry
[num_pins
].pin
= nid
;
4725 spec
->am_entry
[num_pins
].attr
= attr
;
4732 spec
->am_num_entries
= num_pins
;
4733 /* sort the am_entry in the order of attr so that the pin with a
4734 * higher attr will be selected when the jack is plugged.
4736 sort(spec
->am_entry
, num_pins
, sizeof(spec
->am_entry
[0]),
4737 compare_attr
, NULL
);
4739 if (!auto_mic_check_imux(codec
))
4743 spec
->num_adc_nids
= 1;
4744 spec
->cur_mux
[0] = spec
->am_entry
[0].idx
;
4745 codec_dbg(codec
, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4746 spec
->am_entry
[0].pin
,
4747 spec
->am_entry
[1].pin
,
4748 spec
->am_entry
[2].pin
);
4754 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4756 * @codec: the HDA codec
4757 * @nid: NID to evalute
4758 * @power_state: target power state
4760 unsigned int snd_hda_gen_path_power_filter(struct hda_codec
*codec
,
4762 unsigned int power_state
)
4764 struct hda_gen_spec
*spec
= codec
->spec
;
4766 if (!spec
->power_down_unused
&& !codec
->power_save_node
)
4768 if (power_state
!= AC_PWRST_D0
|| nid
== codec
->core
.afg
)
4770 if (get_wcaps_type(get_wcaps(codec
, nid
)) >= AC_WID_POWER
)
4772 if (is_active_nid_for_any(codec
, nid
))
4776 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter
);
4778 /* mute all aamix inputs initially; parse up to the first leaves */
4779 static void mute_all_mixer_nid(struct hda_codec
*codec
, hda_nid_t mix
)
4782 const hda_nid_t
*conn
;
4785 nums
= snd_hda_get_conn_list(codec
, mix
, &conn
);
4786 has_amp
= nid_has_mute(codec
, mix
, HDA_INPUT
);
4787 for (i
= 0; i
< nums
; i
++) {
4789 update_amp(codec
, mix
, HDA_INPUT
, i
,
4790 0xff, HDA_AMP_MUTE
);
4791 else if (nid_has_volume(codec
, conn
[i
], HDA_OUTPUT
))
4792 update_amp(codec
, conn
[i
], HDA_OUTPUT
, 0,
4793 0xff, HDA_AMP_MUTE
);
4798 * snd_hda_gen_stream_pm - Stream power management callback
4799 * @codec: the HDA codec
4800 * @nid: audio widget
4801 * @on: power on/off flag
4803 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
4805 void snd_hda_gen_stream_pm(struct hda_codec
*codec
, hda_nid_t nid
, bool on
)
4807 if (codec
->power_save_node
)
4808 set_path_power(codec
, nid
, -1, on
);
4810 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm
);
4813 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4814 * set up the hda_gen_spec
4815 * @codec: the HDA codec
4816 * @cfg: Parsed pin configuration
4818 * return 1 if successful, 0 if the proper config is not found,
4819 * or a negative error code
4821 int snd_hda_gen_parse_auto_config(struct hda_codec
*codec
,
4822 struct auto_pin_cfg
*cfg
)
4824 struct hda_gen_spec
*spec
= codec
->spec
;
4827 parse_user_hints(codec
);
4829 if (spec
->mixer_nid
&& !spec
->mixer_merge_nid
)
4830 spec
->mixer_merge_nid
= spec
->mixer_nid
;
4832 if (cfg
!= &spec
->autocfg
) {
4833 spec
->autocfg
= *cfg
;
4834 cfg
= &spec
->autocfg
;
4837 if (!spec
->main_out_badness
)
4838 spec
->main_out_badness
= &hda_main_out_badness
;
4839 if (!spec
->extra_out_badness
)
4840 spec
->extra_out_badness
= &hda_extra_out_badness
;
4842 fill_all_dac_nids(codec
);
4844 if (!cfg
->line_outs
) {
4845 if (cfg
->dig_outs
|| cfg
->dig_in_pin
) {
4846 spec
->multiout
.max_channels
= 2;
4847 spec
->no_analog
= 1;
4850 if (!cfg
->num_inputs
&& !cfg
->dig_in_pin
)
4851 return 0; /* can't find valid BIOS pin config */
4854 if (!spec
->no_primary_hp
&&
4855 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
&&
4856 cfg
->line_outs
<= cfg
->hp_outs
) {
4857 /* use HP as primary out */
4858 cfg
->speaker_outs
= cfg
->line_outs
;
4859 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
4860 sizeof(cfg
->speaker_pins
));
4861 cfg
->line_outs
= cfg
->hp_outs
;
4862 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
, sizeof(cfg
->hp_pins
));
4864 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
4865 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
4868 err
= parse_output_paths(codec
);
4871 err
= create_multi_channel_mode(codec
);
4874 err
= create_multi_out_ctls(codec
, cfg
);
4877 err
= create_hp_out_ctls(codec
);
4880 err
= create_speaker_out_ctls(codec
);
4883 err
= create_indep_hp_ctls(codec
);
4886 err
= create_loopback_mixing_ctl(codec
);
4889 err
= create_hp_mic(codec
);
4892 err
= create_input_ctls(codec
);
4896 /* add power-down pin callbacks at first */
4897 add_all_pin_power_ctls(codec
, false);
4899 spec
->const_channel_count
= spec
->ext_channel_count
;
4900 /* check the multiple speaker and headphone pins */
4901 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
4902 spec
->const_channel_count
= max(spec
->const_channel_count
,
4903 cfg
->speaker_outs
* 2);
4904 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
4905 spec
->const_channel_count
= max(spec
->const_channel_count
,
4907 spec
->multiout
.max_channels
= max(spec
->ext_channel_count
,
4908 spec
->const_channel_count
);
4910 err
= check_auto_mute_availability(codec
);
4914 err
= check_dyn_adc_switch(codec
);
4918 err
= check_auto_mic_availability(codec
);
4922 /* add stereo mix if available and not enabled yet */
4923 if (!spec
->auto_mic
&& spec
->mixer_nid
&&
4924 spec
->add_stereo_mix_input
== HDA_HINT_STEREO_MIX_AUTO
&&
4925 spec
->input_mux
.num_items
> 1) {
4926 err
= parse_capture_source(codec
, spec
->mixer_nid
,
4927 CFG_IDX_MIX
, spec
->num_all_adcs
,
4934 err
= create_capture_mixers(codec
);
4938 err
= parse_mic_boost(codec
);
4942 /* create "Headphone Mic Jack Mode" if no input selection is
4943 * available (or user specifies add_jack_modes hint)
4945 if (spec
->hp_mic_pin
&&
4946 (spec
->auto_mic
|| spec
->input_mux
.num_items
== 1 ||
4947 spec
->add_jack_modes
)) {
4948 err
= create_hp_mic_jack_mode(codec
, spec
->hp_mic_pin
);
4953 if (spec
->add_jack_modes
) {
4954 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
4955 err
= create_out_jack_modes(codec
, cfg
->line_outs
,
4956 cfg
->line_out_pins
);
4960 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
) {
4961 err
= create_out_jack_modes(codec
, cfg
->hp_outs
,
4968 /* add power-up pin callbacks at last */
4969 add_all_pin_power_ctls(codec
, true);
4971 /* mute all aamix input initially */
4972 if (spec
->mixer_nid
)
4973 mute_all_mixer_nid(codec
, spec
->mixer_nid
);
4976 parse_digital(codec
);
4978 if (spec
->power_down_unused
|| codec
->power_save_node
) {
4979 if (!codec
->power_filter
)
4980 codec
->power_filter
= snd_hda_gen_path_power_filter
;
4981 if (!codec
->patch_ops
.stream_pm
)
4982 codec
->patch_ops
.stream_pm
= snd_hda_gen_stream_pm
;
4985 if (!spec
->no_analog
&& spec
->beep_nid
) {
4986 err
= snd_hda_attach_beep_device(codec
, spec
->beep_nid
);
4989 if (codec
->beep
&& codec
->power_save_node
) {
4990 err
= add_fake_beep_paths(codec
);
4993 codec
->beep
->power_hook
= beep_power_hook
;
4999 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config
);
5003 * Build control elements
5006 /* slave controls for virtual master */
5007 static const char * const slave_pfxs
[] = {
5008 "Front", "Surround", "Center", "LFE", "Side",
5009 "Headphone", "Speaker", "Mono", "Line Out",
5010 "CLFE", "Bass Speaker", "PCM",
5011 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5012 "Headphone Front", "Headphone Surround", "Headphone CLFE",
5013 "Headphone Side", "Headphone+LO", "Speaker+LO",
5018 * snd_hda_gen_build_controls - Build controls from the parsed results
5019 * @codec: the HDA codec
5021 * Pass this to build_controls patch_ops.
5023 int snd_hda_gen_build_controls(struct hda_codec
*codec
)
5025 struct hda_gen_spec
*spec
= codec
->spec
;
5028 if (spec
->kctls
.used
) {
5029 err
= snd_hda_add_new_ctls(codec
, spec
->kctls
.list
);
5034 if (spec
->multiout
.dig_out_nid
) {
5035 err
= snd_hda_create_dig_out_ctls(codec
,
5036 spec
->multiout
.dig_out_nid
,
5037 spec
->multiout
.dig_out_nid
,
5038 spec
->pcm_rec
[1]->pcm_type
);
5041 if (!spec
->no_analog
) {
5042 err
= snd_hda_create_spdif_share_sw(codec
,
5046 spec
->multiout
.share_spdif
= 1;
5049 if (spec
->dig_in_nid
) {
5050 err
= snd_hda_create_spdif_in_ctls(codec
, spec
->dig_in_nid
);
5055 /* if we have no master control, let's create it */
5056 if (!spec
->no_analog
&&
5057 !snd_hda_find_mixer_ctl(codec
, "Master Playback Volume")) {
5058 err
= snd_hda_add_vmaster(codec
, "Master Playback Volume",
5059 spec
->vmaster_tlv
, slave_pfxs
,
5064 if (!spec
->no_analog
&&
5065 !snd_hda_find_mixer_ctl(codec
, "Master Playback Switch")) {
5066 err
= __snd_hda_add_vmaster(codec
, "Master Playback Switch",
5069 true, &spec
->vmaster_mute
.sw_kctl
);
5072 if (spec
->vmaster_mute
.hook
) {
5073 snd_hda_add_vmaster_hook(codec
, &spec
->vmaster_mute
,
5074 spec
->vmaster_mute_enum
);
5075 snd_hda_sync_vmaster_hook(&spec
->vmaster_mute
);
5079 free_kctls(spec
); /* no longer needed */
5081 err
= snd_hda_jack_add_kctls(codec
, &spec
->autocfg
);
5087 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls
);
5094 static void call_pcm_playback_hook(struct hda_pcm_stream
*hinfo
,
5095 struct hda_codec
*codec
,
5096 struct snd_pcm_substream
*substream
,
5099 struct hda_gen_spec
*spec
= codec
->spec
;
5100 if (spec
->pcm_playback_hook
)
5101 spec
->pcm_playback_hook(hinfo
, codec
, substream
, action
);
5104 static void call_pcm_capture_hook(struct hda_pcm_stream
*hinfo
,
5105 struct hda_codec
*codec
,
5106 struct snd_pcm_substream
*substream
,
5109 struct hda_gen_spec
*spec
= codec
->spec
;
5110 if (spec
->pcm_capture_hook
)
5111 spec
->pcm_capture_hook(hinfo
, codec
, substream
, action
);
5115 * Analog playback callbacks
5117 static int playback_pcm_open(struct hda_pcm_stream
*hinfo
,
5118 struct hda_codec
*codec
,
5119 struct snd_pcm_substream
*substream
)
5121 struct hda_gen_spec
*spec
= codec
->spec
;
5124 mutex_lock(&spec
->pcm_mutex
);
5125 err
= snd_hda_multi_out_analog_open(codec
,
5126 &spec
->multiout
, substream
,
5129 spec
->active_streams
|= 1 << STREAM_MULTI_OUT
;
5130 call_pcm_playback_hook(hinfo
, codec
, substream
,
5131 HDA_GEN_PCM_ACT_OPEN
);
5133 mutex_unlock(&spec
->pcm_mutex
);
5137 static int playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5138 struct hda_codec
*codec
,
5139 unsigned int stream_tag
,
5140 unsigned int format
,
5141 struct snd_pcm_substream
*substream
)
5143 struct hda_gen_spec
*spec
= codec
->spec
;
5146 err
= snd_hda_multi_out_analog_prepare(codec
, &spec
->multiout
,
5147 stream_tag
, format
, substream
);
5149 call_pcm_playback_hook(hinfo
, codec
, substream
,
5150 HDA_GEN_PCM_ACT_PREPARE
);
5154 static int playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5155 struct hda_codec
*codec
,
5156 struct snd_pcm_substream
*substream
)
5158 struct hda_gen_spec
*spec
= codec
->spec
;
5161 err
= snd_hda_multi_out_analog_cleanup(codec
, &spec
->multiout
);
5163 call_pcm_playback_hook(hinfo
, codec
, substream
,
5164 HDA_GEN_PCM_ACT_CLEANUP
);
5168 static int playback_pcm_close(struct hda_pcm_stream
*hinfo
,
5169 struct hda_codec
*codec
,
5170 struct snd_pcm_substream
*substream
)
5172 struct hda_gen_spec
*spec
= codec
->spec
;
5173 mutex_lock(&spec
->pcm_mutex
);
5174 spec
->active_streams
&= ~(1 << STREAM_MULTI_OUT
);
5175 call_pcm_playback_hook(hinfo
, codec
, substream
,
5176 HDA_GEN_PCM_ACT_CLOSE
);
5177 mutex_unlock(&spec
->pcm_mutex
);
5181 static int capture_pcm_open(struct hda_pcm_stream
*hinfo
,
5182 struct hda_codec
*codec
,
5183 struct snd_pcm_substream
*substream
)
5185 call_pcm_capture_hook(hinfo
, codec
, substream
, HDA_GEN_PCM_ACT_OPEN
);
5189 static int capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5190 struct hda_codec
*codec
,
5191 unsigned int stream_tag
,
5192 unsigned int format
,
5193 struct snd_pcm_substream
*substream
)
5195 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
5196 call_pcm_capture_hook(hinfo
, codec
, substream
,
5197 HDA_GEN_PCM_ACT_PREPARE
);
5201 static int capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5202 struct hda_codec
*codec
,
5203 struct snd_pcm_substream
*substream
)
5205 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
5206 call_pcm_capture_hook(hinfo
, codec
, substream
,
5207 HDA_GEN_PCM_ACT_CLEANUP
);
5211 static int capture_pcm_close(struct hda_pcm_stream
*hinfo
,
5212 struct hda_codec
*codec
,
5213 struct snd_pcm_substream
*substream
)
5215 call_pcm_capture_hook(hinfo
, codec
, substream
, HDA_GEN_PCM_ACT_CLOSE
);
5219 static int alt_playback_pcm_open(struct hda_pcm_stream
*hinfo
,
5220 struct hda_codec
*codec
,
5221 struct snd_pcm_substream
*substream
)
5223 struct hda_gen_spec
*spec
= codec
->spec
;
5226 mutex_lock(&spec
->pcm_mutex
);
5227 if (spec
->indep_hp
&& !spec
->indep_hp_enabled
)
5230 spec
->active_streams
|= 1 << STREAM_INDEP_HP
;
5231 call_pcm_playback_hook(hinfo
, codec
, substream
,
5232 HDA_GEN_PCM_ACT_OPEN
);
5233 mutex_unlock(&spec
->pcm_mutex
);
5237 static int alt_playback_pcm_close(struct hda_pcm_stream
*hinfo
,
5238 struct hda_codec
*codec
,
5239 struct snd_pcm_substream
*substream
)
5241 struct hda_gen_spec
*spec
= codec
->spec
;
5242 mutex_lock(&spec
->pcm_mutex
);
5243 spec
->active_streams
&= ~(1 << STREAM_INDEP_HP
);
5244 call_pcm_playback_hook(hinfo
, codec
, substream
,
5245 HDA_GEN_PCM_ACT_CLOSE
);
5246 mutex_unlock(&spec
->pcm_mutex
);
5250 static int alt_playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5251 struct hda_codec
*codec
,
5252 unsigned int stream_tag
,
5253 unsigned int format
,
5254 struct snd_pcm_substream
*substream
)
5256 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
5257 call_pcm_playback_hook(hinfo
, codec
, substream
,
5258 HDA_GEN_PCM_ACT_PREPARE
);
5262 static int alt_playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5263 struct hda_codec
*codec
,
5264 struct snd_pcm_substream
*substream
)
5266 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
5267 call_pcm_playback_hook(hinfo
, codec
, substream
,
5268 HDA_GEN_PCM_ACT_CLEANUP
);
5275 static int dig_playback_pcm_open(struct hda_pcm_stream
*hinfo
,
5276 struct hda_codec
*codec
,
5277 struct snd_pcm_substream
*substream
)
5279 struct hda_gen_spec
*spec
= codec
->spec
;
5280 return snd_hda_multi_out_dig_open(codec
, &spec
->multiout
);
5283 static int dig_playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5284 struct hda_codec
*codec
,
5285 unsigned int stream_tag
,
5286 unsigned int format
,
5287 struct snd_pcm_substream
*substream
)
5289 struct hda_gen_spec
*spec
= codec
->spec
;
5290 return snd_hda_multi_out_dig_prepare(codec
, &spec
->multiout
,
5291 stream_tag
, format
, substream
);
5294 static int dig_playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5295 struct hda_codec
*codec
,
5296 struct snd_pcm_substream
*substream
)
5298 struct hda_gen_spec
*spec
= codec
->spec
;
5299 return snd_hda_multi_out_dig_cleanup(codec
, &spec
->multiout
);
5302 static int dig_playback_pcm_close(struct hda_pcm_stream
*hinfo
,
5303 struct hda_codec
*codec
,
5304 struct snd_pcm_substream
*substream
)
5306 struct hda_gen_spec
*spec
= codec
->spec
;
5307 return snd_hda_multi_out_dig_close(codec
, &spec
->multiout
);
5313 #define alt_capture_pcm_open capture_pcm_open
5314 #define alt_capture_pcm_close capture_pcm_close
5316 static int alt_capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5317 struct hda_codec
*codec
,
5318 unsigned int stream_tag
,
5319 unsigned int format
,
5320 struct snd_pcm_substream
*substream
)
5322 struct hda_gen_spec
*spec
= codec
->spec
;
5324 snd_hda_codec_setup_stream(codec
, spec
->adc_nids
[substream
->number
+ 1],
5325 stream_tag
, 0, format
);
5326 call_pcm_capture_hook(hinfo
, codec
, substream
,
5327 HDA_GEN_PCM_ACT_PREPARE
);
5331 static int alt_capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5332 struct hda_codec
*codec
,
5333 struct snd_pcm_substream
*substream
)
5335 struct hda_gen_spec
*spec
= codec
->spec
;
5337 snd_hda_codec_cleanup_stream(codec
,
5338 spec
->adc_nids
[substream
->number
+ 1]);
5339 call_pcm_capture_hook(hinfo
, codec
, substream
,
5340 HDA_GEN_PCM_ACT_CLEANUP
);
5346 static const struct hda_pcm_stream pcm_analog_playback
= {
5350 /* NID is set in build_pcms */
5352 .open
= playback_pcm_open
,
5353 .close
= playback_pcm_close
,
5354 .prepare
= playback_pcm_prepare
,
5355 .cleanup
= playback_pcm_cleanup
5359 static const struct hda_pcm_stream pcm_analog_capture
= {
5363 /* NID is set in build_pcms */
5365 .open
= capture_pcm_open
,
5366 .close
= capture_pcm_close
,
5367 .prepare
= capture_pcm_prepare
,
5368 .cleanup
= capture_pcm_cleanup
5372 static const struct hda_pcm_stream pcm_analog_alt_playback
= {
5376 /* NID is set in build_pcms */
5378 .open
= alt_playback_pcm_open
,
5379 .close
= alt_playback_pcm_close
,
5380 .prepare
= alt_playback_pcm_prepare
,
5381 .cleanup
= alt_playback_pcm_cleanup
5385 static const struct hda_pcm_stream pcm_analog_alt_capture
= {
5386 .substreams
= 2, /* can be overridden */
5389 /* NID is set in build_pcms */
5391 .open
= alt_capture_pcm_open
,
5392 .close
= alt_capture_pcm_close
,
5393 .prepare
= alt_capture_pcm_prepare
,
5394 .cleanup
= alt_capture_pcm_cleanup
5398 static const struct hda_pcm_stream pcm_digital_playback
= {
5402 /* NID is set in build_pcms */
5404 .open
= dig_playback_pcm_open
,
5405 .close
= dig_playback_pcm_close
,
5406 .prepare
= dig_playback_pcm_prepare
,
5407 .cleanup
= dig_playback_pcm_cleanup
5411 static const struct hda_pcm_stream pcm_digital_capture
= {
5415 /* NID is set in build_pcms */
5418 /* Used by build_pcms to flag that a PCM has no playback stream */
5419 static const struct hda_pcm_stream pcm_null_stream
= {
5426 * dynamic changing ADC PCM streams
5428 static bool dyn_adc_pcm_resetup(struct hda_codec
*codec
, int cur
)
5430 struct hda_gen_spec
*spec
= codec
->spec
;
5431 hda_nid_t new_adc
= spec
->adc_nids
[spec
->dyn_adc_idx
[cur
]];
5433 if (spec
->cur_adc
&& spec
->cur_adc
!= new_adc
) {
5434 /* stream is running, let's swap the current ADC */
5435 __snd_hda_codec_cleanup_stream(codec
, spec
->cur_adc
, 1);
5436 spec
->cur_adc
= new_adc
;
5437 snd_hda_codec_setup_stream(codec
, new_adc
,
5438 spec
->cur_adc_stream_tag
, 0,
5439 spec
->cur_adc_format
);
5445 /* analog capture with dynamic dual-adc changes */
5446 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5447 struct hda_codec
*codec
,
5448 unsigned int stream_tag
,
5449 unsigned int format
,
5450 struct snd_pcm_substream
*substream
)
5452 struct hda_gen_spec
*spec
= codec
->spec
;
5453 spec
->cur_adc
= spec
->adc_nids
[spec
->dyn_adc_idx
[spec
->cur_mux
[0]]];
5454 spec
->cur_adc_stream_tag
= stream_tag
;
5455 spec
->cur_adc_format
= format
;
5456 snd_hda_codec_setup_stream(codec
, spec
->cur_adc
, stream_tag
, 0, format
);
5460 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5461 struct hda_codec
*codec
,
5462 struct snd_pcm_substream
*substream
)
5464 struct hda_gen_spec
*spec
= codec
->spec
;
5465 snd_hda_codec_cleanup_stream(codec
, spec
->cur_adc
);
5470 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture
= {
5474 .nid
= 0, /* fill later */
5476 .prepare
= dyn_adc_capture_pcm_prepare
,
5477 .cleanup
= dyn_adc_capture_pcm_cleanup
5481 static void fill_pcm_stream_name(char *str
, size_t len
, const char *sfx
,
5482 const char *chip_name
)
5488 strlcpy(str
, chip_name
, len
);
5490 /* drop non-alnum chars after a space */
5491 for (p
= strchr(str
, ' '); p
; p
= strchr(p
+ 1, ' ')) {
5492 if (!isalnum(p
[1])) {
5497 strlcat(str
, sfx
, len
);
5500 /* copy PCM stream info from @default_str, and override non-NULL entries
5501 * from @spec_str and @nid
5503 static void setup_pcm_stream(struct hda_pcm_stream
*str
,
5504 const struct hda_pcm_stream
*default_str
,
5505 const struct hda_pcm_stream
*spec_str
,
5508 *str
= *default_str
;
5512 if (spec_str
->substreams
)
5513 str
->substreams
= spec_str
->substreams
;
5514 if (spec_str
->channels_min
)
5515 str
->channels_min
= spec_str
->channels_min
;
5516 if (spec_str
->channels_max
)
5517 str
->channels_max
= spec_str
->channels_max
;
5518 if (spec_str
->rates
)
5519 str
->rates
= spec_str
->rates
;
5520 if (spec_str
->formats
)
5521 str
->formats
= spec_str
->formats
;
5522 if (spec_str
->maxbps
)
5523 str
->maxbps
= spec_str
->maxbps
;
5528 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5529 * @codec: the HDA codec
5531 * Pass this to build_pcms patch_ops.
5533 int snd_hda_gen_build_pcms(struct hda_codec
*codec
)
5535 struct hda_gen_spec
*spec
= codec
->spec
;
5536 struct hda_pcm
*info
;
5537 bool have_multi_adcs
;
5539 if (spec
->no_analog
)
5542 fill_pcm_stream_name(spec
->stream_name_analog
,
5543 sizeof(spec
->stream_name_analog
),
5544 " Analog", codec
->core
.chip_name
);
5545 info
= snd_hda_codec_pcm_new(codec
, "%s", spec
->stream_name_analog
);
5548 spec
->pcm_rec
[0] = info
;
5550 if (spec
->multiout
.num_dacs
> 0) {
5551 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
],
5552 &pcm_analog_playback
,
5553 spec
->stream_analog_playback
,
5554 spec
->multiout
.dac_nids
[0]);
5555 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].channels_max
=
5556 spec
->multiout
.max_channels
;
5557 if (spec
->autocfg
.line_out_type
== AUTO_PIN_SPEAKER_OUT
&&
5558 spec
->autocfg
.line_outs
== 2)
5559 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].chmap
=
5562 if (spec
->num_adc_nids
) {
5563 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_CAPTURE
],
5564 (spec
->dyn_adc_switch
?
5565 &dyn_adc_pcm_analog_capture
: &pcm_analog_capture
),
5566 spec
->stream_analog_capture
,
5571 /* SPDIF for stream index #1 */
5572 if (spec
->multiout
.dig_out_nid
|| spec
->dig_in_nid
) {
5573 fill_pcm_stream_name(spec
->stream_name_digital
,
5574 sizeof(spec
->stream_name_digital
),
5575 " Digital", codec
->core
.chip_name
);
5576 info
= snd_hda_codec_pcm_new(codec
, "%s",
5577 spec
->stream_name_digital
);
5580 codec
->slave_dig_outs
= spec
->multiout
.slave_dig_outs
;
5581 spec
->pcm_rec
[1] = info
;
5582 if (spec
->dig_out_type
)
5583 info
->pcm_type
= spec
->dig_out_type
;
5585 info
->pcm_type
= HDA_PCM_TYPE_SPDIF
;
5586 if (spec
->multiout
.dig_out_nid
)
5587 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
],
5588 &pcm_digital_playback
,
5589 spec
->stream_digital_playback
,
5590 spec
->multiout
.dig_out_nid
);
5591 if (spec
->dig_in_nid
)
5592 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_CAPTURE
],
5593 &pcm_digital_capture
,
5594 spec
->stream_digital_capture
,
5598 if (spec
->no_analog
)
5601 /* If the use of more than one ADC is requested for the current
5602 * model, configure a second analog capture-only PCM.
5604 have_multi_adcs
= (spec
->num_adc_nids
> 1) &&
5605 !spec
->dyn_adc_switch
&& !spec
->auto_mic
;
5606 /* Additional Analaog capture for index #2 */
5607 if (spec
->alt_dac_nid
|| have_multi_adcs
) {
5608 fill_pcm_stream_name(spec
->stream_name_alt_analog
,
5609 sizeof(spec
->stream_name_alt_analog
),
5610 " Alt Analog", codec
->core
.chip_name
);
5611 info
= snd_hda_codec_pcm_new(codec
, "%s",
5612 spec
->stream_name_alt_analog
);
5615 spec
->pcm_rec
[2] = info
;
5616 if (spec
->alt_dac_nid
)
5617 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
],
5618 &pcm_analog_alt_playback
,
5619 spec
->stream_analog_alt_playback
,
5622 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
],
5623 &pcm_null_stream
, NULL
, 0);
5624 if (have_multi_adcs
) {
5625 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_CAPTURE
],
5626 &pcm_analog_alt_capture
,
5627 spec
->stream_analog_alt_capture
,
5629 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].substreams
=
5630 spec
->num_adc_nids
- 1;
5632 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_CAPTURE
],
5633 &pcm_null_stream
, NULL
, 0);
5639 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms
);
5643 * Standard auto-parser initializations
5646 /* configure the given path as a proper output */
5647 static void set_output_and_unmute(struct hda_codec
*codec
, int path_idx
)
5649 struct nid_path
*path
;
5652 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
5653 if (!path
|| !path
->depth
)
5655 pin
= path
->path
[path
->depth
- 1];
5656 restore_pin_ctl(codec
, pin
);
5657 snd_hda_activate_path(codec
, path
, path
->active
,
5658 aamix_default(codec
->spec
));
5659 set_pin_eapd(codec
, pin
, path
->active
);
5662 /* initialize primary output paths */
5663 static void init_multi_out(struct hda_codec
*codec
)
5665 struct hda_gen_spec
*spec
= codec
->spec
;
5668 for (i
= 0; i
< spec
->autocfg
.line_outs
; i
++)
5669 set_output_and_unmute(codec
, spec
->out_paths
[i
]);
5673 static void __init_extra_out(struct hda_codec
*codec
, int num_outs
, int *paths
)
5677 for (i
= 0; i
< num_outs
; i
++)
5678 set_output_and_unmute(codec
, paths
[i
]);
5681 /* initialize hp and speaker paths */
5682 static void init_extra_out(struct hda_codec
*codec
)
5684 struct hda_gen_spec
*spec
= codec
->spec
;
5686 if (spec
->autocfg
.line_out_type
!= AUTO_PIN_HP_OUT
)
5687 __init_extra_out(codec
, spec
->autocfg
.hp_outs
, spec
->hp_paths
);
5688 if (spec
->autocfg
.line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
5689 __init_extra_out(codec
, spec
->autocfg
.speaker_outs
,
5690 spec
->speaker_paths
);
5693 /* initialize multi-io paths */
5694 static void init_multi_io(struct hda_codec
*codec
)
5696 struct hda_gen_spec
*spec
= codec
->spec
;
5699 for (i
= 0; i
< spec
->multi_ios
; i
++) {
5700 hda_nid_t pin
= spec
->multi_io
[i
].pin
;
5701 struct nid_path
*path
;
5702 path
= get_multiio_path(codec
, i
);
5705 if (!spec
->multi_io
[i
].ctl_in
)
5706 spec
->multi_io
[i
].ctl_in
=
5707 snd_hda_codec_get_pin_target(codec
, pin
);
5708 snd_hda_activate_path(codec
, path
, path
->active
,
5709 aamix_default(spec
));
5713 static void init_aamix_paths(struct hda_codec
*codec
)
5715 struct hda_gen_spec
*spec
= codec
->spec
;
5717 if (!spec
->have_aamix_ctl
)
5719 if (!has_aamix_out_paths(spec
))
5721 update_aamix_paths(codec
, spec
->aamix_mode
, spec
->out_paths
[0],
5722 spec
->aamix_out_paths
[0],
5723 spec
->autocfg
.line_out_type
);
5724 update_aamix_paths(codec
, spec
->aamix_mode
, spec
->hp_paths
[0],
5725 spec
->aamix_out_paths
[1],
5727 update_aamix_paths(codec
, spec
->aamix_mode
, spec
->speaker_paths
[0],
5728 spec
->aamix_out_paths
[2],
5729 AUTO_PIN_SPEAKER_OUT
);
5732 /* set up input pins and loopback paths */
5733 static void init_analog_input(struct hda_codec
*codec
)
5735 struct hda_gen_spec
*spec
= codec
->spec
;
5736 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
5739 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
5740 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
5741 if (is_input_pin(codec
, nid
))
5742 restore_pin_ctl(codec
, nid
);
5744 /* init loopback inputs */
5745 if (spec
->mixer_nid
) {
5746 resume_path_from_idx(codec
, spec
->loopback_paths
[i
]);
5747 resume_path_from_idx(codec
, spec
->loopback_merge_path
);
5752 /* initialize ADC paths */
5753 static void init_input_src(struct hda_codec
*codec
)
5755 struct hda_gen_spec
*spec
= codec
->spec
;
5756 struct hda_input_mux
*imux
= &spec
->input_mux
;
5757 struct nid_path
*path
;
5760 if (spec
->dyn_adc_switch
)
5763 nums
= spec
->num_adc_nids
;
5765 for (c
= 0; c
< nums
; c
++) {
5766 for (i
= 0; i
< imux
->num_items
; i
++) {
5767 path
= get_input_path(codec
, c
, i
);
5769 bool active
= path
->active
;
5770 if (i
== spec
->cur_mux
[c
])
5772 snd_hda_activate_path(codec
, path
, active
, false);
5776 update_hp_mic(codec
, c
, true);
5779 if (spec
->cap_sync_hook
)
5780 spec
->cap_sync_hook(codec
, NULL
, NULL
);
5783 /* set right pin controls for digital I/O */
5784 static void init_digital(struct hda_codec
*codec
)
5786 struct hda_gen_spec
*spec
= codec
->spec
;
5790 for (i
= 0; i
< spec
->autocfg
.dig_outs
; i
++)
5791 set_output_and_unmute(codec
, spec
->digout_paths
[i
]);
5792 pin
= spec
->autocfg
.dig_in_pin
;
5794 restore_pin_ctl(codec
, pin
);
5795 resume_path_from_idx(codec
, spec
->digin_path
);
5799 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5800 * invalid unsol tags by some reason
5802 static void clear_unsol_on_unused_pins(struct hda_codec
*codec
)
5806 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
5807 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
5808 hda_nid_t nid
= pin
->nid
;
5809 if (is_jack_detectable(codec
, nid
) &&
5810 !snd_hda_jack_tbl_get(codec
, nid
))
5811 snd_hda_codec_update_cache(codec
, nid
, 0,
5812 AC_VERB_SET_UNSOLICITED_ENABLE
, 0);
5817 * snd_hda_gen_init - initialize the generic spec
5818 * @codec: the HDA codec
5820 * This can be put as patch_ops init function.
5822 int snd_hda_gen_init(struct hda_codec
*codec
)
5824 struct hda_gen_spec
*spec
= codec
->spec
;
5826 if (spec
->init_hook
)
5827 spec
->init_hook(codec
);
5829 if (!spec
->skip_verbs
)
5830 snd_hda_apply_verbs(codec
);
5832 init_multi_out(codec
);
5833 init_extra_out(codec
);
5834 init_multi_io(codec
);
5835 init_aamix_paths(codec
);
5836 init_analog_input(codec
);
5837 init_input_src(codec
);
5838 init_digital(codec
);
5840 clear_unsol_on_unused_pins(codec
);
5842 sync_all_pin_power_ctls(codec
);
5844 /* call init functions of standard auto-mute helpers */
5845 update_automute_all(codec
);
5847 regcache_sync(codec
->core
.regmap
);
5849 if (spec
->vmaster_mute
.sw_kctl
&& spec
->vmaster_mute
.hook
)
5850 snd_hda_sync_vmaster_hook(&spec
->vmaster_mute
);
5852 hda_call_check_power_status(codec
, 0x01);
5855 EXPORT_SYMBOL_GPL(snd_hda_gen_init
);
5858 * snd_hda_gen_free - free the generic spec
5859 * @codec: the HDA codec
5861 * This can be put as patch_ops free function.
5863 void snd_hda_gen_free(struct hda_codec
*codec
)
5865 snd_hda_apply_fixup(codec
, HDA_FIXUP_ACT_FREE
);
5866 snd_hda_gen_spec_free(codec
->spec
);
5870 EXPORT_SYMBOL_GPL(snd_hda_gen_free
);
5874 * snd_hda_gen_check_power_status - check the loopback power save state
5875 * @codec: the HDA codec
5876 * @nid: NID to inspect
5878 * This can be put as patch_ops check_power_status function.
5880 int snd_hda_gen_check_power_status(struct hda_codec
*codec
, hda_nid_t nid
)
5882 struct hda_gen_spec
*spec
= codec
->spec
;
5883 return snd_hda_check_amp_list_power(codec
, &spec
->loopback
, nid
);
5885 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status
);
5890 * the generic codec support
5893 static const struct hda_codec_ops generic_patch_ops
= {
5894 .build_controls
= snd_hda_gen_build_controls
,
5895 .build_pcms
= snd_hda_gen_build_pcms
,
5896 .init
= snd_hda_gen_init
,
5897 .free
= snd_hda_gen_free
,
5898 .unsol_event
= snd_hda_jack_unsol_event
,
5900 .check_power_status
= snd_hda_gen_check_power_status
,
5905 * snd_hda_parse_generic_codec - Generic codec parser
5906 * @codec: the HDA codec
5908 static int snd_hda_parse_generic_codec(struct hda_codec
*codec
)
5910 struct hda_gen_spec
*spec
;
5913 spec
= kzalloc(sizeof(*spec
), GFP_KERNEL
);
5916 snd_hda_gen_spec_init(spec
);
5919 err
= snd_hda_parse_pin_defcfg(codec
, &spec
->autocfg
, NULL
, 0);
5923 err
= snd_hda_gen_parse_auto_config(codec
, &spec
->autocfg
);
5927 codec
->patch_ops
= generic_patch_ops
;
5931 snd_hda_gen_free(codec
);
5935 static const struct hda_device_id snd_hda_id_generic
[] = {
5936 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC
, "Generic", snd_hda_parse_generic_codec
),
5939 MODULE_DEVICE_TABLE(hdaudio
, snd_hda_id_generic
);
5941 static struct hda_codec_driver generic_driver
= {
5942 .id
= snd_hda_id_generic
,
5945 module_hda_codec_driver(generic_driver
);
5947 MODULE_LICENSE("GPL");
5948 MODULE_DESCRIPTION("Generic HD-audio codec parser");