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 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 if (!snd_hda_check_power_state(codec
, nid
, state
)) {
859 snd_hda_codec_write(codec
, nid
, 0,
860 AC_VERB_SET_POWER_STATE
, state
);
862 /* all known codecs seem to be capable to handl
863 * widgets state even in D3, so far.
864 * if any new codecs need to restore the widget
865 * states after D0 transition, call the function
869 if (state
== AC_PWRST_D0
)
870 snd_hdac_regmap_sync_node(&codec
->core
, nid
);
877 /* do sync with the last power state change */
878 static void sync_power_state_change(struct hda_codec
*codec
, hda_nid_t nid
)
882 snd_hda_codec_read(codec
, nid
, 0, AC_VERB_GET_POWER_STATE
, 0);
887 * snd_hda_activate_path - activate or deactivate the given path
888 * @codec: the HDA codec
889 * @path: the path to activate/deactivate
890 * @enable: flag to activate or not
891 * @add_aamix: enable the input from aamix NID
893 * If @add_aamix is set, enable the input from aa-mix NID as well (if any).
895 void snd_hda_activate_path(struct hda_codec
*codec
, struct nid_path
*path
,
896 bool enable
, bool add_aamix
)
898 struct hda_gen_spec
*spec
= codec
->spec
;
901 path
->active
= enable
;
903 /* make sure the widget is powered up */
904 if (enable
&& (spec
->power_down_unused
|| codec
->power_save_node
))
905 path_power_update(codec
, path
, codec
->power_save_node
);
907 for (i
= path
->depth
- 1; i
>= 0; i
--) {
908 hda_nid_t nid
= path
->path
[i
];
910 if (enable
&& path
->multi
[i
])
911 snd_hda_codec_update_cache(codec
, nid
, 0,
912 AC_VERB_SET_CONNECT_SEL
,
914 if (has_amp_in(codec
, path
, i
))
915 activate_amp_in(codec
, path
, i
, enable
, add_aamix
);
916 if (has_amp_out(codec
, path
, i
))
917 activate_amp_out(codec
, path
, i
, enable
);
920 EXPORT_SYMBOL_GPL(snd_hda_activate_path
);
922 /* if the given path is inactive, put widgets into D3 (only if suitable) */
923 static void path_power_down_sync(struct hda_codec
*codec
, struct nid_path
*path
)
925 struct hda_gen_spec
*spec
= codec
->spec
;
927 if (!(spec
->power_down_unused
|| codec
->power_save_node
) || path
->active
)
929 sync_power_state_change(codec
, path_power_update(codec
, path
, true));
932 /* turn on/off EAPD on the given pin */
933 static void set_pin_eapd(struct hda_codec
*codec
, hda_nid_t pin
, bool enable
)
935 struct hda_gen_spec
*spec
= codec
->spec
;
936 if (spec
->own_eapd_ctl
||
937 !(snd_hda_query_pin_caps(codec
, pin
) & AC_PINCAP_EAPD
))
939 if (spec
->keep_eapd_on
&& !enable
)
943 snd_hda_codec_update_cache(codec
, pin
, 0,
944 AC_VERB_SET_EAPD_BTLENABLE
,
945 enable
? 0x02 : 0x00);
948 /* re-initialize the path specified by the given path index */
949 static void resume_path_from_idx(struct hda_codec
*codec
, int path_idx
)
951 struct nid_path
*path
= snd_hda_get_path_from_idx(codec
, path_idx
);
953 snd_hda_activate_path(codec
, path
, path
->active
, false);
958 * Helper functions for creating mixer ctl elements
961 static int hda_gen_mixer_mute_put(struct snd_kcontrol
*kcontrol
,
962 struct snd_ctl_elem_value
*ucontrol
);
963 static int hda_gen_bind_mute_put(struct snd_kcontrol
*kcontrol
,
964 struct snd_ctl_elem_value
*ucontrol
);
971 static const struct snd_kcontrol_new control_templates
[] = {
972 HDA_CODEC_VOLUME(NULL
, 0, 0, 0),
973 /* only the put callback is replaced for handling the special mute */
975 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
976 .subdevice
= HDA_SUBDEV_AMP_FLAG
,
977 .info
= snd_hda_mixer_amp_switch_info
,
978 .get
= snd_hda_mixer_amp_switch_get
,
979 .put
= hda_gen_mixer_mute_put
, /* replaced */
980 .private_value
= HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
983 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
984 .info
= snd_hda_mixer_amp_switch_info
,
985 .get
= snd_hda_mixer_bind_switch_get
,
986 .put
= hda_gen_bind_mute_put
, /* replaced */
987 .private_value
= HDA_COMPOSE_AMP_VAL(0, 3, 0, 0),
991 /* add dynamic controls from template */
992 static struct snd_kcontrol_new
*
993 add_control(struct hda_gen_spec
*spec
, int type
, const char *name
,
994 int cidx
, unsigned long val
)
996 struct snd_kcontrol_new
*knew
;
998 knew
= snd_hda_gen_add_kctl(spec
, name
, &control_templates
[type
]);
1002 if (get_amp_nid_(val
))
1003 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
1004 knew
->private_value
= val
;
1008 static int add_control_with_pfx(struct hda_gen_spec
*spec
, int type
,
1009 const char *pfx
, const char *dir
,
1010 const char *sfx
, int cidx
, unsigned long val
)
1012 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
1013 snprintf(name
, sizeof(name
), "%s %s %s", pfx
, dir
, sfx
);
1014 if (!add_control(spec
, type
, name
, cidx
, val
))
1019 #define add_pb_vol_ctrl(spec, type, pfx, val) \
1020 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
1021 #define add_pb_sw_ctrl(spec, type, pfx, val) \
1022 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
1023 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
1024 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
1025 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
1026 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
1028 static int add_vol_ctl(struct hda_codec
*codec
, const char *pfx
, int cidx
,
1029 unsigned int chs
, struct nid_path
*path
)
1034 val
= path
->ctls
[NID_PATH_VOL_CTL
];
1037 val
= amp_val_replace_channels(val
, chs
);
1038 return __add_pb_vol_ctrl(codec
->spec
, HDA_CTL_WIDGET_VOL
, pfx
, cidx
, val
);
1041 /* return the channel bits suitable for the given path->ctls[] */
1042 static int get_default_ch_nums(struct hda_codec
*codec
, struct nid_path
*path
,
1045 int chs
= 1; /* mono (left only) */
1047 hda_nid_t nid
= get_amp_nid_(path
->ctls
[type
]);
1048 if (nid
&& (get_wcaps(codec
, nid
) & AC_WCAP_STEREO
))
1049 chs
= 3; /* stereo */
1054 static int add_stereo_vol(struct hda_codec
*codec
, const char *pfx
, int cidx
,
1055 struct nid_path
*path
)
1057 int chs
= get_default_ch_nums(codec
, path
, NID_PATH_VOL_CTL
);
1058 return add_vol_ctl(codec
, pfx
, cidx
, chs
, path
);
1061 /* create a mute-switch for the given mixer widget;
1062 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
1064 static int add_sw_ctl(struct hda_codec
*codec
, const char *pfx
, int cidx
,
1065 unsigned int chs
, struct nid_path
*path
)
1068 int type
= HDA_CTL_WIDGET_MUTE
;
1072 val
= path
->ctls
[NID_PATH_MUTE_CTL
];
1075 val
= amp_val_replace_channels(val
, chs
);
1076 if (get_amp_direction_(val
) == HDA_INPUT
) {
1077 hda_nid_t nid
= get_amp_nid_(val
);
1078 int nums
= snd_hda_get_num_conns(codec
, nid
);
1080 type
= HDA_CTL_BIND_MUTE
;
1084 return __add_pb_sw_ctrl(codec
->spec
, type
, pfx
, cidx
, val
);
1087 static int add_stereo_sw(struct hda_codec
*codec
, const char *pfx
,
1088 int cidx
, struct nid_path
*path
)
1090 int chs
= get_default_ch_nums(codec
, path
, NID_PATH_MUTE_CTL
);
1091 return add_sw_ctl(codec
, pfx
, cidx
, chs
, path
);
1094 /* playback mute control with the software mute bit check */
1095 static void sync_auto_mute_bits(struct snd_kcontrol
*kcontrol
,
1096 struct snd_ctl_elem_value
*ucontrol
)
1098 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1099 struct hda_gen_spec
*spec
= codec
->spec
;
1101 if (spec
->auto_mute_via_amp
) {
1102 hda_nid_t nid
= get_amp_nid(kcontrol
);
1103 bool enabled
= !((spec
->mute_bits
>> nid
) & 1);
1104 ucontrol
->value
.integer
.value
[0] &= enabled
;
1105 ucontrol
->value
.integer
.value
[1] &= enabled
;
1109 static int hda_gen_mixer_mute_put(struct snd_kcontrol
*kcontrol
,
1110 struct snd_ctl_elem_value
*ucontrol
)
1112 sync_auto_mute_bits(kcontrol
, ucontrol
);
1113 return snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
1116 static int hda_gen_bind_mute_put(struct snd_kcontrol
*kcontrol
,
1117 struct snd_ctl_elem_value
*ucontrol
)
1119 sync_auto_mute_bits(kcontrol
, ucontrol
);
1120 return snd_hda_mixer_bind_switch_put(kcontrol
, ucontrol
);
1123 /* any ctl assigned to the path with the given index? */
1124 static bool path_has_mixer(struct hda_codec
*codec
, int path_idx
, int ctl_type
)
1126 struct nid_path
*path
= snd_hda_get_path_from_idx(codec
, path_idx
);
1127 return path
&& path
->ctls
[ctl_type
];
1130 static const char * const channel_name
[4] = {
1131 "Front", "Surround", "CLFE", "Side"
1134 /* give some appropriate ctl name prefix for the given line out channel */
1135 static const char *get_line_out_pfx(struct hda_codec
*codec
, int ch
,
1136 int *index
, int ctl_type
)
1138 struct hda_gen_spec
*spec
= codec
->spec
;
1139 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1142 if (cfg
->line_outs
== 1 && !spec
->multi_ios
&&
1143 !cfg
->hp_outs
&& !cfg
->speaker_outs
)
1144 return spec
->vmaster_mute
.hook
? "PCM" : "Master";
1146 /* if there is really a single DAC used in the whole output paths,
1147 * use it master (or "PCM" if a vmaster hook is present)
1149 if (spec
->multiout
.num_dacs
== 1 && !spec
->mixer_nid
&&
1150 !spec
->multiout
.hp_out_nid
[0] && !spec
->multiout
.extra_out_nid
[0])
1151 return spec
->vmaster_mute
.hook
? "PCM" : "Master";
1153 /* multi-io channels */
1154 if (ch
>= cfg
->line_outs
)
1155 return channel_name
[ch
];
1157 switch (cfg
->line_out_type
) {
1158 case AUTO_PIN_SPEAKER_OUT
:
1159 /* if the primary channel vol/mute is shared with HP volume,
1160 * don't name it as Speaker
1162 if (!ch
&& cfg
->hp_outs
&&
1163 !path_has_mixer(codec
, spec
->hp_paths
[0], ctl_type
))
1165 if (cfg
->line_outs
== 1)
1167 if (cfg
->line_outs
== 2)
1168 return ch
? "Bass Speaker" : "Speaker";
1170 case AUTO_PIN_HP_OUT
:
1171 /* if the primary channel vol/mute is shared with spk volume,
1172 * don't name it as Headphone
1174 if (!ch
&& cfg
->speaker_outs
&&
1175 !path_has_mixer(codec
, spec
->speaker_paths
[0], ctl_type
))
1177 /* for multi-io case, only the primary out */
1178 if (ch
&& spec
->multi_ios
)
1182 case AUTO_PIN_LINE_OUT
:
1183 /* This deals with the case where we have two DACs and
1184 * one LO, one HP and one Speaker */
1185 if (!ch
&& cfg
->speaker_outs
&& cfg
->hp_outs
) {
1186 bool hp_lo_shared
= !path_has_mixer(codec
, spec
->hp_paths
[0], ctl_type
);
1187 bool spk_lo_shared
= !path_has_mixer(codec
, spec
->speaker_paths
[0], ctl_type
);
1188 if (hp_lo_shared
&& spk_lo_shared
)
1189 return spec
->vmaster_mute
.hook
? "PCM" : "Master";
1191 return "Headphone+LO";
1193 return "Speaker+LO";
1197 /* for a single channel output, we don't have to name the channel */
1198 if (cfg
->line_outs
== 1 && !spec
->multi_ios
)
1201 if (ch
>= ARRAY_SIZE(channel_name
)) {
1206 return channel_name
[ch
];
1210 * Parse output paths
1213 /* badness definition */
1215 /* No primary DAC is found for the main output */
1216 BAD_NO_PRIMARY_DAC
= 0x10000,
1217 /* No DAC is found for the extra output */
1218 BAD_NO_DAC
= 0x4000,
1219 /* No possible multi-ios */
1220 BAD_MULTI_IO
= 0x120,
1221 /* No individual DAC for extra output */
1222 BAD_NO_EXTRA_DAC
= 0x102,
1223 /* No individual DAC for extra surrounds */
1224 BAD_NO_EXTRA_SURR_DAC
= 0x101,
1225 /* Primary DAC shared with main surrounds */
1226 BAD_SHARED_SURROUND
= 0x100,
1227 /* No independent HP possible */
1228 BAD_NO_INDEP_HP
= 0x10,
1229 /* Primary DAC shared with main CLFE */
1230 BAD_SHARED_CLFE
= 0x10,
1231 /* Primary DAC shared with extra surrounds */
1232 BAD_SHARED_EXTRA_SURROUND
= 0x10,
1233 /* Volume widget is shared */
1234 BAD_SHARED_VOL
= 0x10,
1237 /* look for widgets in the given path which are appropriate for
1238 * volume and mute controls, and assign the values to ctls[].
1240 * When no appropriate widget is found in the path, the badness value
1241 * is incremented depending on the situation. The function returns the
1242 * total badness for both volume and mute controls.
1244 static int assign_out_path_ctls(struct hda_codec
*codec
, struct nid_path
*path
)
1246 struct hda_gen_spec
*spec
= codec
->spec
;
1252 return BAD_SHARED_VOL
* 2;
1254 if (path
->ctls
[NID_PATH_VOL_CTL
] ||
1255 path
->ctls
[NID_PATH_MUTE_CTL
])
1256 return 0; /* already evaluated */
1258 nid
= look_for_out_vol_nid(codec
, path
);
1260 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
1261 if (spec
->dac_min_mute
)
1262 val
|= HDA_AMP_VAL_MIN_MUTE
;
1263 if (is_ctl_used(codec
, val
, NID_PATH_VOL_CTL
))
1264 badness
+= BAD_SHARED_VOL
;
1266 path
->ctls
[NID_PATH_VOL_CTL
] = val
;
1268 badness
+= BAD_SHARED_VOL
;
1269 nid
= look_for_out_mute_nid(codec
, path
);
1271 unsigned int wid_type
= get_wcaps_type(get_wcaps(codec
, nid
));
1272 if (wid_type
== AC_WID_PIN
|| wid_type
== AC_WID_AUD_OUT
||
1273 nid_has_mute(codec
, nid
, HDA_OUTPUT
))
1274 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
1276 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_INPUT
);
1277 if (is_ctl_used(codec
, val
, NID_PATH_MUTE_CTL
))
1278 badness
+= BAD_SHARED_VOL
;
1280 path
->ctls
[NID_PATH_MUTE_CTL
] = val
;
1282 badness
+= BAD_SHARED_VOL
;
1286 const struct badness_table hda_main_out_badness
= {
1287 .no_primary_dac
= BAD_NO_PRIMARY_DAC
,
1288 .no_dac
= BAD_NO_DAC
,
1289 .shared_primary
= BAD_NO_PRIMARY_DAC
,
1290 .shared_surr
= BAD_SHARED_SURROUND
,
1291 .shared_clfe
= BAD_SHARED_CLFE
,
1292 .shared_surr_main
= BAD_SHARED_SURROUND
,
1294 EXPORT_SYMBOL_GPL(hda_main_out_badness
);
1296 const struct badness_table hda_extra_out_badness
= {
1297 .no_primary_dac
= BAD_NO_DAC
,
1298 .no_dac
= BAD_NO_DAC
,
1299 .shared_primary
= BAD_NO_EXTRA_DAC
,
1300 .shared_surr
= BAD_SHARED_EXTRA_SURROUND
,
1301 .shared_clfe
= BAD_SHARED_EXTRA_SURROUND
,
1302 .shared_surr_main
= BAD_NO_EXTRA_SURR_DAC
,
1304 EXPORT_SYMBOL_GPL(hda_extra_out_badness
);
1306 /* get the DAC of the primary output corresponding to the given array index */
1307 static hda_nid_t
get_primary_out(struct hda_codec
*codec
, int idx
)
1309 struct hda_gen_spec
*spec
= codec
->spec
;
1310 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1312 if (cfg
->line_outs
> idx
)
1313 return spec
->private_dac_nids
[idx
];
1314 idx
-= cfg
->line_outs
;
1315 if (spec
->multi_ios
> idx
)
1316 return spec
->multi_io
[idx
].dac
;
1320 /* return the DAC if it's reachable, otherwise zero */
1321 static inline hda_nid_t
try_dac(struct hda_codec
*codec
,
1322 hda_nid_t dac
, hda_nid_t pin
)
1324 return is_reachable_path(codec
, dac
, pin
) ? dac
: 0;
1327 /* try to assign DACs to pins and return the resultant badness */
1328 static int try_assign_dacs(struct hda_codec
*codec
, int num_outs
,
1329 const hda_nid_t
*pins
, hda_nid_t
*dacs
,
1331 const struct badness_table
*bad
)
1333 struct hda_gen_spec
*spec
= codec
->spec
;
1341 for (i
= 0; i
< num_outs
; i
++) {
1342 struct nid_path
*path
;
1343 hda_nid_t pin
= pins
[i
];
1345 path
= snd_hda_get_path_from_idx(codec
, path_idx
[i
]);
1347 badness
+= assign_out_path_ctls(codec
, path
);
1351 dacs
[i
] = get_preferred_dac(codec
, pin
);
1353 if (is_dac_already_used(codec
, dacs
[i
]))
1354 badness
+= bad
->shared_primary
;
1358 dacs
[i
] = look_for_dac(codec
, pin
, false);
1359 if (!dacs
[i
] && !i
) {
1360 /* try to steal the DAC of surrounds for the front */
1361 for (j
= 1; j
< num_outs
; j
++) {
1362 if (is_reachable_path(codec
, dacs
[j
], pin
)) {
1365 invalidate_nid_path(codec
, path_idx
[j
]);
1374 dac
= try_dac(codec
, get_primary_out(codec
, i
), pin
);
1376 dac
= try_dac(codec
, dacs
[0], pin
);
1378 dac
= try_dac(codec
, get_primary_out(codec
, i
), pin
);
1381 badness
+= bad
->shared_primary
;
1383 badness
+= bad
->shared_surr
;
1385 badness
+= bad
->shared_clfe
;
1386 } else if (is_reachable_path(codec
, spec
->private_dac_nids
[0], pin
)) {
1387 dac
= spec
->private_dac_nids
[0];
1388 badness
+= bad
->shared_surr_main
;
1390 badness
+= bad
->no_primary_dac
;
1392 badness
+= bad
->no_dac
;
1396 path
= snd_hda_add_new_path(codec
, dac
, pin
, -spec
->mixer_nid
);
1397 if (!path
&& !i
&& spec
->mixer_nid
) {
1398 /* try with aamix */
1399 path
= snd_hda_add_new_path(codec
, dac
, pin
, 0);
1403 badness
+= bad
->no_dac
;
1405 /* print_nid_path(codec, "output", path); */
1406 path
->active
= true;
1407 path_idx
[i
] = snd_hda_get_path_idx(codec
, path
);
1408 badness
+= assign_out_path_ctls(codec
, path
);
1415 /* return NID if the given pin has only a single connection to a certain DAC */
1416 static hda_nid_t
get_dac_if_single(struct hda_codec
*codec
, hda_nid_t pin
)
1418 struct hda_gen_spec
*spec
= codec
->spec
;
1420 hda_nid_t nid_found
= 0;
1422 for (i
= 0; i
< spec
->num_all_dacs
; i
++) {
1423 hda_nid_t nid
= spec
->all_dacs
[i
];
1424 if (!nid
|| is_dac_already_used(codec
, nid
))
1426 if (is_reachable_path(codec
, nid
, pin
)) {
1435 /* check whether the given pin can be a multi-io pin */
1436 static bool can_be_multiio_pin(struct hda_codec
*codec
,
1437 unsigned int location
, hda_nid_t nid
)
1439 unsigned int defcfg
, caps
;
1441 defcfg
= snd_hda_codec_get_pincfg(codec
, nid
);
1442 if (get_defcfg_connect(defcfg
) != AC_JACK_PORT_COMPLEX
)
1444 if (location
&& get_defcfg_location(defcfg
) != location
)
1446 caps
= snd_hda_query_pin_caps(codec
, nid
);
1447 if (!(caps
& AC_PINCAP_OUT
))
1452 /* count the number of input pins that are capable to be multi-io */
1453 static int count_multiio_pins(struct hda_codec
*codec
, hda_nid_t reference_pin
)
1455 struct hda_gen_spec
*spec
= codec
->spec
;
1456 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1457 unsigned int defcfg
= snd_hda_codec_get_pincfg(codec
, reference_pin
);
1458 unsigned int location
= get_defcfg_location(defcfg
);
1462 for (type
= AUTO_PIN_LINE_IN
; type
>= AUTO_PIN_MIC
; type
--) {
1463 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
1464 if (cfg
->inputs
[i
].type
!= type
)
1466 if (can_be_multiio_pin(codec
, location
,
1467 cfg
->inputs
[i
].pin
))
1477 * When hardwired is set, try to fill ony hardwired pins, and returns
1478 * zero if any pins are filled, non-zero if nothing found.
1479 * When hardwired is off, try to fill possible input pins, and returns
1480 * the badness value.
1482 static int fill_multi_ios(struct hda_codec
*codec
,
1483 hda_nid_t reference_pin
,
1486 struct hda_gen_spec
*spec
= codec
->spec
;
1487 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1488 int type
, i
, j
, num_pins
, old_pins
;
1489 unsigned int defcfg
= snd_hda_codec_get_pincfg(codec
, reference_pin
);
1490 unsigned int location
= get_defcfg_location(defcfg
);
1492 struct nid_path
*path
;
1494 old_pins
= spec
->multi_ios
;
1498 num_pins
= count_multiio_pins(codec
, reference_pin
);
1502 for (type
= AUTO_PIN_LINE_IN
; type
>= AUTO_PIN_MIC
; type
--) {
1503 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
1504 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
1507 if (cfg
->inputs
[i
].type
!= type
)
1509 if (!can_be_multiio_pin(codec
, location
, nid
))
1511 for (j
= 0; j
< spec
->multi_ios
; j
++) {
1512 if (nid
== spec
->multi_io
[j
].pin
)
1515 if (j
< spec
->multi_ios
)
1519 dac
= get_dac_if_single(codec
, nid
);
1521 dac
= look_for_dac(codec
, nid
, false);
1526 path
= snd_hda_add_new_path(codec
, dac
, nid
,
1532 /* print_nid_path(codec, "multiio", path); */
1533 spec
->multi_io
[spec
->multi_ios
].pin
= nid
;
1534 spec
->multi_io
[spec
->multi_ios
].dac
= dac
;
1535 spec
->out_paths
[cfg
->line_outs
+ spec
->multi_ios
] =
1536 snd_hda_get_path_idx(codec
, path
);
1538 if (spec
->multi_ios
>= 2)
1544 badness
= BAD_MULTI_IO
;
1545 if (old_pins
== spec
->multi_ios
) {
1547 return 1; /* nothing found */
1549 return badness
; /* no badness if nothing found */
1551 if (!hardwired
&& spec
->multi_ios
< 2) {
1552 /* cancel newly assigned paths */
1553 spec
->paths
.used
-= spec
->multi_ios
- old_pins
;
1554 spec
->multi_ios
= old_pins
;
1558 /* assign volume and mute controls */
1559 for (i
= old_pins
; i
< spec
->multi_ios
; i
++) {
1560 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[cfg
->line_outs
+ i
]);
1561 badness
+= assign_out_path_ctls(codec
, path
);
1567 /* map DACs for all pins in the list if they are single connections */
1568 static bool map_singles(struct hda_codec
*codec
, int outs
,
1569 const hda_nid_t
*pins
, hda_nid_t
*dacs
, int *path_idx
)
1571 struct hda_gen_spec
*spec
= codec
->spec
;
1574 for (i
= 0; i
< outs
; i
++) {
1575 struct nid_path
*path
;
1579 dac
= get_dac_if_single(codec
, pins
[i
]);
1582 path
= snd_hda_add_new_path(codec
, dac
, pins
[i
],
1584 if (!path
&& !i
&& spec
->mixer_nid
)
1585 path
= snd_hda_add_new_path(codec
, dac
, pins
[i
], 0);
1589 /* print_nid_path(codec, "output", path); */
1590 path
->active
= true;
1591 path_idx
[i
] = snd_hda_get_path_idx(codec
, path
);
1597 static inline bool has_aamix_out_paths(struct hda_gen_spec
*spec
)
1599 return spec
->aamix_out_paths
[0] || spec
->aamix_out_paths
[1] ||
1600 spec
->aamix_out_paths
[2];
1603 /* create a new path including aamix if available, and return its index */
1604 static int check_aamix_out_path(struct hda_codec
*codec
, int path_idx
)
1606 struct hda_gen_spec
*spec
= codec
->spec
;
1607 struct nid_path
*path
;
1608 hda_nid_t path_dac
, dac
, pin
;
1610 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
1611 if (!path
|| !path
->depth
||
1612 is_nid_contained(path
, spec
->mixer_nid
))
1614 path_dac
= path
->path
[0];
1615 dac
= spec
->private_dac_nids
[0];
1616 pin
= path
->path
[path
->depth
- 1];
1617 path
= snd_hda_add_new_path(codec
, dac
, pin
, spec
->mixer_nid
);
1619 if (dac
!= path_dac
)
1621 else if (spec
->multiout
.hp_out_nid
[0])
1622 dac
= spec
->multiout
.hp_out_nid
[0];
1623 else if (spec
->multiout
.extra_out_nid
[0])
1624 dac
= spec
->multiout
.extra_out_nid
[0];
1628 path
= snd_hda_add_new_path(codec
, dac
, pin
,
1633 /* print_nid_path(codec, "output-aamix", path); */
1634 path
->active
= false; /* unused as default */
1635 path
->pin_fixed
= true; /* static route */
1636 return snd_hda_get_path_idx(codec
, path
);
1639 /* check whether the independent HP is available with the current config */
1640 static bool indep_hp_possible(struct hda_codec
*codec
)
1642 struct hda_gen_spec
*spec
= codec
->spec
;
1643 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1644 struct nid_path
*path
;
1647 if (cfg
->line_out_type
== AUTO_PIN_HP_OUT
)
1648 idx
= spec
->out_paths
[0];
1650 idx
= spec
->hp_paths
[0];
1651 path
= snd_hda_get_path_from_idx(codec
, idx
);
1655 /* assume no path conflicts unless aamix is involved */
1656 if (!spec
->mixer_nid
|| !is_nid_contained(path
, spec
->mixer_nid
))
1659 /* check whether output paths contain aamix */
1660 for (i
= 0; i
< cfg
->line_outs
; i
++) {
1661 if (spec
->out_paths
[i
] == idx
)
1663 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[i
]);
1664 if (path
&& is_nid_contained(path
, spec
->mixer_nid
))
1667 for (i
= 0; i
< cfg
->speaker_outs
; i
++) {
1668 path
= snd_hda_get_path_from_idx(codec
, spec
->speaker_paths
[i
]);
1669 if (path
&& is_nid_contained(path
, spec
->mixer_nid
))
1676 /* fill the empty entries in the dac array for speaker/hp with the
1677 * shared dac pointed by the paths
1679 static void refill_shared_dacs(struct hda_codec
*codec
, int num_outs
,
1680 hda_nid_t
*dacs
, int *path_idx
)
1682 struct nid_path
*path
;
1685 for (i
= 0; i
< num_outs
; i
++) {
1688 path
= snd_hda_get_path_from_idx(codec
, path_idx
[i
]);
1691 dacs
[i
] = path
->path
[0];
1695 /* fill in the dac_nids table from the parsed pin configuration */
1696 static int fill_and_eval_dacs(struct hda_codec
*codec
,
1697 bool fill_hardwired
,
1698 bool fill_mio_first
)
1700 struct hda_gen_spec
*spec
= codec
->spec
;
1701 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1702 int i
, err
, badness
;
1704 /* set num_dacs once to full for look_for_dac() */
1705 spec
->multiout
.num_dacs
= cfg
->line_outs
;
1706 spec
->multiout
.dac_nids
= spec
->private_dac_nids
;
1707 memset(spec
->private_dac_nids
, 0, sizeof(spec
->private_dac_nids
));
1708 memset(spec
->multiout
.hp_out_nid
, 0, sizeof(spec
->multiout
.hp_out_nid
));
1709 memset(spec
->multiout
.extra_out_nid
, 0, sizeof(spec
->multiout
.extra_out_nid
));
1710 spec
->multi_ios
= 0;
1711 snd_array_free(&spec
->paths
);
1713 /* clear path indices */
1714 memset(spec
->out_paths
, 0, sizeof(spec
->out_paths
));
1715 memset(spec
->hp_paths
, 0, sizeof(spec
->hp_paths
));
1716 memset(spec
->speaker_paths
, 0, sizeof(spec
->speaker_paths
));
1717 memset(spec
->aamix_out_paths
, 0, sizeof(spec
->aamix_out_paths
));
1718 memset(spec
->digout_paths
, 0, sizeof(spec
->digout_paths
));
1719 memset(spec
->input_paths
, 0, sizeof(spec
->input_paths
));
1720 memset(spec
->loopback_paths
, 0, sizeof(spec
->loopback_paths
));
1721 memset(&spec
->digin_path
, 0, sizeof(spec
->digin_path
));
1725 /* fill hard-wired DACs first */
1726 if (fill_hardwired
) {
1729 mapped
= map_singles(codec
, cfg
->line_outs
,
1731 spec
->private_dac_nids
,
1733 mapped
|= map_singles(codec
, cfg
->hp_outs
,
1735 spec
->multiout
.hp_out_nid
,
1737 mapped
|= map_singles(codec
, cfg
->speaker_outs
,
1739 spec
->multiout
.extra_out_nid
,
1740 spec
->speaker_paths
);
1741 if (!spec
->no_multi_io
&&
1742 fill_mio_first
&& cfg
->line_outs
== 1 &&
1743 cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1744 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], true);
1751 badness
+= try_assign_dacs(codec
, cfg
->line_outs
, cfg
->line_out_pins
,
1752 spec
->private_dac_nids
, spec
->out_paths
,
1753 spec
->main_out_badness
);
1755 if (!spec
->no_multi_io
&& fill_mio_first
&&
1756 cfg
->line_outs
== 1 && cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1757 /* try to fill multi-io first */
1758 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], false);
1761 /* we don't count badness at this stage yet */
1764 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
) {
1765 err
= try_assign_dacs(codec
, cfg
->hp_outs
, cfg
->hp_pins
,
1766 spec
->multiout
.hp_out_nid
,
1768 spec
->extra_out_badness
);
1773 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1774 err
= try_assign_dacs(codec
, cfg
->speaker_outs
,
1776 spec
->multiout
.extra_out_nid
,
1777 spec
->speaker_paths
,
1778 spec
->extra_out_badness
);
1783 if (!spec
->no_multi_io
&&
1784 cfg
->line_outs
== 1 && cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1785 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], false);
1791 if (spec
->mixer_nid
) {
1792 spec
->aamix_out_paths
[0] =
1793 check_aamix_out_path(codec
, spec
->out_paths
[0]);
1794 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
1795 spec
->aamix_out_paths
[1] =
1796 check_aamix_out_path(codec
, spec
->hp_paths
[0]);
1797 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
1798 spec
->aamix_out_paths
[2] =
1799 check_aamix_out_path(codec
, spec
->speaker_paths
[0]);
1802 if (!spec
->no_multi_io
&&
1803 cfg
->hp_outs
&& cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
)
1804 if (count_multiio_pins(codec
, cfg
->hp_pins
[0]) >= 2)
1805 spec
->multi_ios
= 1; /* give badness */
1807 /* re-count num_dacs and squash invalid entries */
1808 spec
->multiout
.num_dacs
= 0;
1809 for (i
= 0; i
< cfg
->line_outs
; i
++) {
1810 if (spec
->private_dac_nids
[i
])
1811 spec
->multiout
.num_dacs
++;
1813 memmove(spec
->private_dac_nids
+ i
,
1814 spec
->private_dac_nids
+ i
+ 1,
1815 sizeof(hda_nid_t
) * (cfg
->line_outs
- i
- 1));
1816 spec
->private_dac_nids
[cfg
->line_outs
- 1] = 0;
1820 spec
->ext_channel_count
= spec
->min_channel_count
=
1821 spec
->multiout
.num_dacs
* 2;
1823 if (spec
->multi_ios
== 2) {
1824 for (i
= 0; i
< 2; i
++)
1825 spec
->private_dac_nids
[spec
->multiout
.num_dacs
++] =
1826 spec
->multi_io
[i
].dac
;
1827 } else if (spec
->multi_ios
) {
1828 spec
->multi_ios
= 0;
1829 badness
+= BAD_MULTI_IO
;
1832 if (spec
->indep_hp
&& !indep_hp_possible(codec
))
1833 badness
+= BAD_NO_INDEP_HP
;
1835 /* re-fill the shared DAC for speaker / headphone */
1836 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
1837 refill_shared_dacs(codec
, cfg
->hp_outs
,
1838 spec
->multiout
.hp_out_nid
,
1840 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
1841 refill_shared_dacs(codec
, cfg
->speaker_outs
,
1842 spec
->multiout
.extra_out_nid
,
1843 spec
->speaker_paths
);
1848 #define DEBUG_BADNESS
1850 #ifdef DEBUG_BADNESS
1851 #define debug_badness(fmt, ...) \
1852 codec_dbg(codec, fmt, ##__VA_ARGS__)
1854 #define debug_badness(fmt, ...) \
1855 do { if (0) codec_dbg(codec, fmt, ##__VA_ARGS__); } while (0)
1858 #ifdef DEBUG_BADNESS
1859 static inline void print_nid_path_idx(struct hda_codec
*codec
,
1860 const char *pfx
, int idx
)
1862 struct nid_path
*path
;
1864 path
= snd_hda_get_path_from_idx(codec
, idx
);
1866 print_nid_path(codec
, pfx
, path
);
1869 static void debug_show_configs(struct hda_codec
*codec
,
1870 struct auto_pin_cfg
*cfg
)
1872 struct hda_gen_spec
*spec
= codec
->spec
;
1873 static const char * const lo_type
[3] = { "LO", "SP", "HP" };
1876 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
1877 cfg
->line_out_pins
[0], cfg
->line_out_pins
[1],
1878 cfg
->line_out_pins
[2], cfg
->line_out_pins
[3],
1879 spec
->multiout
.dac_nids
[0],
1880 spec
->multiout
.dac_nids
[1],
1881 spec
->multiout
.dac_nids
[2],
1882 spec
->multiout
.dac_nids
[3],
1883 lo_type
[cfg
->line_out_type
]);
1884 for (i
= 0; i
< cfg
->line_outs
; i
++)
1885 print_nid_path_idx(codec
, " out", spec
->out_paths
[i
]);
1886 if (spec
->multi_ios
> 0)
1887 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1889 spec
->multi_io
[0].pin
, spec
->multi_io
[1].pin
,
1890 spec
->multi_io
[0].dac
, spec
->multi_io
[1].dac
);
1891 for (i
= 0; i
< spec
->multi_ios
; i
++)
1892 print_nid_path_idx(codec
, " mio",
1893 spec
->out_paths
[cfg
->line_outs
+ i
]);
1895 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1896 cfg
->hp_pins
[0], cfg
->hp_pins
[1],
1897 cfg
->hp_pins
[2], cfg
->hp_pins
[3],
1898 spec
->multiout
.hp_out_nid
[0],
1899 spec
->multiout
.hp_out_nid
[1],
1900 spec
->multiout
.hp_out_nid
[2],
1901 spec
->multiout
.hp_out_nid
[3]);
1902 for (i
= 0; i
< cfg
->hp_outs
; i
++)
1903 print_nid_path_idx(codec
, " hp ", spec
->hp_paths
[i
]);
1904 if (cfg
->speaker_outs
)
1905 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1906 cfg
->speaker_pins
[0], cfg
->speaker_pins
[1],
1907 cfg
->speaker_pins
[2], cfg
->speaker_pins
[3],
1908 spec
->multiout
.extra_out_nid
[0],
1909 spec
->multiout
.extra_out_nid
[1],
1910 spec
->multiout
.extra_out_nid
[2],
1911 spec
->multiout
.extra_out_nid
[3]);
1912 for (i
= 0; i
< cfg
->speaker_outs
; i
++)
1913 print_nid_path_idx(codec
, " spk", spec
->speaker_paths
[i
]);
1914 for (i
= 0; i
< 3; i
++)
1915 print_nid_path_idx(codec
, " mix", spec
->aamix_out_paths
[i
]);
1918 #define debug_show_configs(codec, cfg) /* NOP */
1921 /* find all available DACs of the codec */
1922 static void fill_all_dac_nids(struct hda_codec
*codec
)
1924 struct hda_gen_spec
*spec
= codec
->spec
;
1927 spec
->num_all_dacs
= 0;
1928 memset(spec
->all_dacs
, 0, sizeof(spec
->all_dacs
));
1929 for_each_hda_codec_node(nid
, codec
) {
1930 if (get_wcaps_type(get_wcaps(codec
, nid
)) != AC_WID_AUD_OUT
)
1932 if (spec
->num_all_dacs
>= ARRAY_SIZE(spec
->all_dacs
)) {
1933 codec_err(codec
, "Too many DACs!\n");
1936 spec
->all_dacs
[spec
->num_all_dacs
++] = nid
;
1940 static int parse_output_paths(struct hda_codec
*codec
)
1942 struct hda_gen_spec
*spec
= codec
->spec
;
1943 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1944 struct auto_pin_cfg
*best_cfg
;
1946 int best_badness
= INT_MAX
;
1948 bool fill_hardwired
= true, fill_mio_first
= true;
1949 bool best_wired
= true, best_mio
= true;
1950 bool hp_spk_swapped
= false;
1952 best_cfg
= kmalloc(sizeof(*best_cfg
), GFP_KERNEL
);
1958 badness
= fill_and_eval_dacs(codec
, fill_hardwired
,
1964 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1965 cfg
->line_out_type
, fill_hardwired
, fill_mio_first
,
1967 debug_show_configs(codec
, cfg
);
1968 if (badness
< best_badness
) {
1969 best_badness
= badness
;
1971 best_wired
= fill_hardwired
;
1972 best_mio
= fill_mio_first
;
1976 fill_mio_first
= !fill_mio_first
;
1977 if (!fill_mio_first
)
1979 fill_hardwired
= !fill_hardwired
;
1980 if (!fill_hardwired
)
1984 hp_spk_swapped
= true;
1985 if (cfg
->speaker_outs
> 0 &&
1986 cfg
->line_out_type
== AUTO_PIN_HP_OUT
) {
1987 cfg
->hp_outs
= cfg
->line_outs
;
1988 memcpy(cfg
->hp_pins
, cfg
->line_out_pins
,
1989 sizeof(cfg
->hp_pins
));
1990 cfg
->line_outs
= cfg
->speaker_outs
;
1991 memcpy(cfg
->line_out_pins
, cfg
->speaker_pins
,
1992 sizeof(cfg
->speaker_pins
));
1993 cfg
->speaker_outs
= 0;
1994 memset(cfg
->speaker_pins
, 0, sizeof(cfg
->speaker_pins
));
1995 cfg
->line_out_type
= AUTO_PIN_SPEAKER_OUT
;
1996 fill_hardwired
= true;
1999 if (cfg
->hp_outs
> 0 &&
2000 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
) {
2001 cfg
->speaker_outs
= cfg
->line_outs
;
2002 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
2003 sizeof(cfg
->speaker_pins
));
2004 cfg
->line_outs
= cfg
->hp_outs
;
2005 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
,
2006 sizeof(cfg
->hp_pins
));
2008 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
2009 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
2010 fill_hardwired
= true;
2017 debug_badness("==> restoring best_cfg\n");
2019 fill_and_eval_dacs(codec
, best_wired
, best_mio
);
2021 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
2022 cfg
->line_out_type
, best_wired
, best_mio
);
2023 debug_show_configs(codec
, cfg
);
2025 if (cfg
->line_out_pins
[0]) {
2026 struct nid_path
*path
;
2027 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[0]);
2029 spec
->vmaster_nid
= look_for_out_vol_nid(codec
, path
);
2030 if (spec
->vmaster_nid
) {
2031 snd_hda_set_vmaster_tlv(codec
, spec
->vmaster_nid
,
2032 HDA_OUTPUT
, spec
->vmaster_tlv
);
2033 if (spec
->dac_min_mute
)
2034 spec
->vmaster_tlv
[3] |= TLV_DB_SCALE_MUTE
;
2038 /* set initial pinctl targets */
2039 if (spec
->prefer_hp_amp
|| cfg
->line_out_type
== AUTO_PIN_HP_OUT
)
2043 set_pin_targets(codec
, cfg
->line_outs
, cfg
->line_out_pins
, val
);
2044 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
2045 set_pin_targets(codec
, cfg
->hp_outs
, cfg
->hp_pins
, PIN_HP
);
2046 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
2047 val
= spec
->prefer_hp_amp
? PIN_HP
: PIN_OUT
;
2048 set_pin_targets(codec
, cfg
->speaker_outs
,
2049 cfg
->speaker_pins
, val
);
2052 /* clear indep_hp flag if not available */
2053 if (spec
->indep_hp
&& !indep_hp_possible(codec
))
2060 /* add playback controls from the parsed DAC table */
2061 static int create_multi_out_ctls(struct hda_codec
*codec
,
2062 const struct auto_pin_cfg
*cfg
)
2064 struct hda_gen_spec
*spec
= codec
->spec
;
2065 int i
, err
, noutputs
;
2067 noutputs
= cfg
->line_outs
;
2068 if (spec
->multi_ios
> 0 && cfg
->line_outs
< 3)
2069 noutputs
+= spec
->multi_ios
;
2071 for (i
= 0; i
< noutputs
; i
++) {
2074 struct nid_path
*path
;
2076 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[i
]);
2080 name
= get_line_out_pfx(codec
, i
, &index
, NID_PATH_VOL_CTL
);
2081 if (!name
|| !strcmp(name
, "CLFE")) {
2083 err
= add_vol_ctl(codec
, "Center", 0, 1, path
);
2086 err
= add_vol_ctl(codec
, "LFE", 0, 2, path
);
2090 err
= add_stereo_vol(codec
, name
, index
, path
);
2095 name
= get_line_out_pfx(codec
, i
, &index
, NID_PATH_MUTE_CTL
);
2096 if (!name
|| !strcmp(name
, "CLFE")) {
2097 err
= add_sw_ctl(codec
, "Center", 0, 1, path
);
2100 err
= add_sw_ctl(codec
, "LFE", 0, 2, path
);
2104 err
= add_stereo_sw(codec
, name
, index
, path
);
2112 static int create_extra_out(struct hda_codec
*codec
, int path_idx
,
2113 const char *pfx
, int cidx
)
2115 struct nid_path
*path
;
2118 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
2121 err
= add_stereo_vol(codec
, pfx
, cidx
, path
);
2124 err
= add_stereo_sw(codec
, pfx
, cidx
, path
);
2130 /* add playback controls for speaker and HP outputs */
2131 static int create_extra_outs(struct hda_codec
*codec
, int num_pins
,
2132 const int *paths
, const char *pfx
)
2136 for (i
= 0; i
< num_pins
; i
++) {
2138 char tmp
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
2141 if (num_pins
== 2 && i
== 1 && !strcmp(pfx
, "Speaker"))
2142 name
= "Bass Speaker";
2143 else if (num_pins
>= 3) {
2144 snprintf(tmp
, sizeof(tmp
), "%s %s",
2145 pfx
, channel_name
[i
]);
2151 err
= create_extra_out(codec
, paths
[i
], name
, idx
);
2158 static int create_hp_out_ctls(struct hda_codec
*codec
)
2160 struct hda_gen_spec
*spec
= codec
->spec
;
2161 return create_extra_outs(codec
, spec
->autocfg
.hp_outs
,
2166 static int create_speaker_out_ctls(struct hda_codec
*codec
)
2168 struct hda_gen_spec
*spec
= codec
->spec
;
2169 return create_extra_outs(codec
, spec
->autocfg
.speaker_outs
,
2170 spec
->speaker_paths
,
2175 * independent HP controls
2178 static void call_hp_automute(struct hda_codec
*codec
,
2179 struct hda_jack_callback
*jack
);
2180 static int indep_hp_info(struct snd_kcontrol
*kcontrol
,
2181 struct snd_ctl_elem_info
*uinfo
)
2183 return snd_hda_enum_bool_helper_info(kcontrol
, uinfo
);
2186 static int indep_hp_get(struct snd_kcontrol
*kcontrol
,
2187 struct snd_ctl_elem_value
*ucontrol
)
2189 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2190 struct hda_gen_spec
*spec
= codec
->spec
;
2191 ucontrol
->value
.enumerated
.item
[0] = spec
->indep_hp_enabled
;
2195 static void update_aamix_paths(struct hda_codec
*codec
, bool do_mix
,
2196 int nomix_path_idx
, int mix_path_idx
,
2199 static int indep_hp_put(struct snd_kcontrol
*kcontrol
,
2200 struct snd_ctl_elem_value
*ucontrol
)
2202 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2203 struct hda_gen_spec
*spec
= codec
->spec
;
2204 unsigned int select
= ucontrol
->value
.enumerated
.item
[0];
2207 mutex_lock(&spec
->pcm_mutex
);
2208 if (spec
->active_streams
) {
2213 if (spec
->indep_hp_enabled
!= select
) {
2215 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
2216 dacp
= &spec
->private_dac_nids
[0];
2218 dacp
= &spec
->multiout
.hp_out_nid
[0];
2220 /* update HP aamix paths in case it conflicts with indep HP */
2221 if (spec
->have_aamix_ctl
) {
2222 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
2223 update_aamix_paths(codec
, spec
->aamix_mode
,
2225 spec
->aamix_out_paths
[0],
2226 spec
->autocfg
.line_out_type
);
2228 update_aamix_paths(codec
, spec
->aamix_mode
,
2230 spec
->aamix_out_paths
[1],
2234 spec
->indep_hp_enabled
= select
;
2235 if (spec
->indep_hp_enabled
)
2238 *dacp
= spec
->alt_dac_nid
;
2240 call_hp_automute(codec
, NULL
);
2244 mutex_unlock(&spec
->pcm_mutex
);
2248 static const struct snd_kcontrol_new indep_hp_ctl
= {
2249 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2250 .name
= "Independent HP",
2251 .info
= indep_hp_info
,
2252 .get
= indep_hp_get
,
2253 .put
= indep_hp_put
,
2257 static int create_indep_hp_ctls(struct hda_codec
*codec
)
2259 struct hda_gen_spec
*spec
= codec
->spec
;
2262 if (!spec
->indep_hp
)
2264 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
2265 dac
= spec
->multiout
.dac_nids
[0];
2267 dac
= spec
->multiout
.hp_out_nid
[0];
2273 spec
->indep_hp_enabled
= false;
2274 spec
->alt_dac_nid
= dac
;
2275 if (!snd_hda_gen_add_kctl(spec
, NULL
, &indep_hp_ctl
))
2281 * channel mode enum control
2284 static int ch_mode_info(struct snd_kcontrol
*kcontrol
,
2285 struct snd_ctl_elem_info
*uinfo
)
2287 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2288 struct hda_gen_spec
*spec
= codec
->spec
;
2291 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2293 uinfo
->value
.enumerated
.items
= spec
->multi_ios
+ 1;
2294 if (uinfo
->value
.enumerated
.item
> spec
->multi_ios
)
2295 uinfo
->value
.enumerated
.item
= spec
->multi_ios
;
2296 chs
= uinfo
->value
.enumerated
.item
* 2 + spec
->min_channel_count
;
2297 sprintf(uinfo
->value
.enumerated
.name
, "%dch", chs
);
2301 static int ch_mode_get(struct snd_kcontrol
*kcontrol
,
2302 struct snd_ctl_elem_value
*ucontrol
)
2304 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2305 struct hda_gen_spec
*spec
= codec
->spec
;
2306 ucontrol
->value
.enumerated
.item
[0] =
2307 (spec
->ext_channel_count
- spec
->min_channel_count
) / 2;
2311 static inline struct nid_path
*
2312 get_multiio_path(struct hda_codec
*codec
, int idx
)
2314 struct hda_gen_spec
*spec
= codec
->spec
;
2315 return snd_hda_get_path_from_idx(codec
,
2316 spec
->out_paths
[spec
->autocfg
.line_outs
+ idx
]);
2319 static void update_automute_all(struct hda_codec
*codec
);
2321 /* Default value to be passed as aamix argument for snd_hda_activate_path();
2322 * used for output paths
2324 static bool aamix_default(struct hda_gen_spec
*spec
)
2326 return !spec
->have_aamix_ctl
|| spec
->aamix_mode
;
2329 static int set_multi_io(struct hda_codec
*codec
, int idx
, bool output
)
2331 struct hda_gen_spec
*spec
= codec
->spec
;
2332 hda_nid_t nid
= spec
->multi_io
[idx
].pin
;
2333 struct nid_path
*path
;
2335 path
= get_multiio_path(codec
, idx
);
2339 if (path
->active
== output
)
2343 set_pin_target(codec
, nid
, PIN_OUT
, true);
2344 snd_hda_activate_path(codec
, path
, true, aamix_default(spec
));
2345 set_pin_eapd(codec
, nid
, true);
2347 set_pin_eapd(codec
, nid
, false);
2348 snd_hda_activate_path(codec
, path
, false, aamix_default(spec
));
2349 set_pin_target(codec
, nid
, spec
->multi_io
[idx
].ctl_in
, true);
2350 path_power_down_sync(codec
, path
);
2353 /* update jack retasking in case it modifies any of them */
2354 update_automute_all(codec
);
2359 static int ch_mode_put(struct snd_kcontrol
*kcontrol
,
2360 struct snd_ctl_elem_value
*ucontrol
)
2362 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2363 struct hda_gen_spec
*spec
= codec
->spec
;
2366 ch
= ucontrol
->value
.enumerated
.item
[0];
2367 if (ch
< 0 || ch
> spec
->multi_ios
)
2369 if (ch
== (spec
->ext_channel_count
- spec
->min_channel_count
) / 2)
2371 spec
->ext_channel_count
= ch
* 2 + spec
->min_channel_count
;
2372 for (i
= 0; i
< spec
->multi_ios
; i
++)
2373 set_multi_io(codec
, i
, i
< ch
);
2374 spec
->multiout
.max_channels
= max(spec
->ext_channel_count
,
2375 spec
->const_channel_count
);
2376 if (spec
->need_dac_fix
)
2377 spec
->multiout
.num_dacs
= spec
->multiout
.max_channels
/ 2;
2381 static const struct snd_kcontrol_new channel_mode_enum
= {
2382 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2383 .name
= "Channel Mode",
2384 .info
= ch_mode_info
,
2389 static int create_multi_channel_mode(struct hda_codec
*codec
)
2391 struct hda_gen_spec
*spec
= codec
->spec
;
2393 if (spec
->multi_ios
> 0) {
2394 if (!snd_hda_gen_add_kctl(spec
, NULL
, &channel_mode_enum
))
2401 * aamix loopback enable/disable switch
2404 #define loopback_mixing_info indep_hp_info
2406 static int loopback_mixing_get(struct snd_kcontrol
*kcontrol
,
2407 struct snd_ctl_elem_value
*ucontrol
)
2409 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2410 struct hda_gen_spec
*spec
= codec
->spec
;
2411 ucontrol
->value
.enumerated
.item
[0] = spec
->aamix_mode
;
2415 static void update_aamix_paths(struct hda_codec
*codec
, bool do_mix
,
2416 int nomix_path_idx
, int mix_path_idx
,
2419 struct hda_gen_spec
*spec
= codec
->spec
;
2420 struct nid_path
*nomix_path
, *mix_path
;
2422 nomix_path
= snd_hda_get_path_from_idx(codec
, nomix_path_idx
);
2423 mix_path
= snd_hda_get_path_from_idx(codec
, mix_path_idx
);
2424 if (!nomix_path
|| !mix_path
)
2427 /* if HP aamix path is driven from a different DAC and the
2428 * independent HP mode is ON, can't turn on aamix path
2430 if (out_type
== AUTO_PIN_HP_OUT
&& spec
->indep_hp_enabled
&&
2431 mix_path
->path
[0] != spec
->alt_dac_nid
)
2435 snd_hda_activate_path(codec
, nomix_path
, false, true);
2436 snd_hda_activate_path(codec
, mix_path
, true, true);
2437 path_power_down_sync(codec
, nomix_path
);
2439 snd_hda_activate_path(codec
, mix_path
, false, false);
2440 snd_hda_activate_path(codec
, nomix_path
, true, false);
2441 path_power_down_sync(codec
, mix_path
);
2445 /* re-initialize the output paths; only called from loopback_mixing_put() */
2446 static void update_output_paths(struct hda_codec
*codec
, int num_outs
,
2449 struct hda_gen_spec
*spec
= codec
->spec
;
2450 struct nid_path
*path
;
2453 for (i
= 0; i
< num_outs
; i
++) {
2454 path
= snd_hda_get_path_from_idx(codec
, paths
[i
]);
2456 snd_hda_activate_path(codec
, path
, path
->active
,
2461 static int loopback_mixing_put(struct snd_kcontrol
*kcontrol
,
2462 struct snd_ctl_elem_value
*ucontrol
)
2464 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2465 struct hda_gen_spec
*spec
= codec
->spec
;
2466 const struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
2467 unsigned int val
= ucontrol
->value
.enumerated
.item
[0];
2469 if (val
== spec
->aamix_mode
)
2471 spec
->aamix_mode
= val
;
2472 if (has_aamix_out_paths(spec
)) {
2473 update_aamix_paths(codec
, val
, spec
->out_paths
[0],
2474 spec
->aamix_out_paths
[0],
2475 cfg
->line_out_type
);
2476 update_aamix_paths(codec
, val
, spec
->hp_paths
[0],
2477 spec
->aamix_out_paths
[1],
2479 update_aamix_paths(codec
, val
, spec
->speaker_paths
[0],
2480 spec
->aamix_out_paths
[2],
2481 AUTO_PIN_SPEAKER_OUT
);
2483 update_output_paths(codec
, cfg
->line_outs
, spec
->out_paths
);
2484 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
2485 update_output_paths(codec
, cfg
->hp_outs
, spec
->hp_paths
);
2486 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
2487 update_output_paths(codec
, cfg
->speaker_outs
,
2488 spec
->speaker_paths
);
2493 static const struct snd_kcontrol_new loopback_mixing_enum
= {
2494 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2495 .name
= "Loopback Mixing",
2496 .info
= loopback_mixing_info
,
2497 .get
= loopback_mixing_get
,
2498 .put
= loopback_mixing_put
,
2501 static int create_loopback_mixing_ctl(struct hda_codec
*codec
)
2503 struct hda_gen_spec
*spec
= codec
->spec
;
2505 if (!spec
->mixer_nid
)
2507 if (!snd_hda_gen_add_kctl(spec
, NULL
, &loopback_mixing_enum
))
2509 spec
->have_aamix_ctl
= 1;
2510 /* if no explicit aamix path is present (e.g. for Realtek codecs),
2511 * enable aamix as default -- just for compatibility
2513 spec
->aamix_mode
= !has_aamix_out_paths(spec
);
2518 * shared headphone/mic handling
2521 static void call_update_outputs(struct hda_codec
*codec
);
2523 /* for shared I/O, change the pin-control accordingly */
2524 static void update_hp_mic(struct hda_codec
*codec
, int adc_mux
, bool force
)
2526 struct hda_gen_spec
*spec
= codec
->spec
;
2531 pin
= spec
->hp_mic_pin
;
2532 as_mic
= spec
->cur_mux
[adc_mux
] == spec
->hp_mic_mux_idx
;
2535 val
= snd_hda_codec_get_pin_target(codec
, pin
);
2545 val
= snd_hda_get_default_vref(codec
, pin
);
2546 /* if the HP pin doesn't support VREF and the codec driver gives an
2547 * alternative pin, set up the VREF on that pin instead
2549 if (val
== AC_PINCTL_VREF_HIZ
&& spec
->shared_mic_vref_pin
) {
2550 const hda_nid_t vref_pin
= spec
->shared_mic_vref_pin
;
2551 unsigned int vref_val
= snd_hda_get_default_vref(codec
, vref_pin
);
2552 if (vref_val
!= AC_PINCTL_VREF_HIZ
)
2553 snd_hda_set_pin_ctl_cache(codec
, vref_pin
,
2554 PIN_IN
| (as_mic
? vref_val
: 0));
2557 if (!spec
->hp_mic_jack_modes
) {
2562 set_pin_target(codec
, pin
, val
, true);
2563 call_hp_automute(codec
, NULL
);
2567 /* create a shared input with the headphone out */
2568 static int create_hp_mic(struct hda_codec
*codec
)
2570 struct hda_gen_spec
*spec
= codec
->spec
;
2571 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
2572 unsigned int defcfg
;
2575 if (!spec
->hp_mic
) {
2576 if (spec
->suppress_hp_mic_detect
)
2578 /* automatic detection: only if no input or a single internal
2579 * input pin is found, try to detect the shared hp/mic
2581 if (cfg
->num_inputs
> 1)
2583 else if (cfg
->num_inputs
== 1) {
2584 defcfg
= snd_hda_codec_get_pincfg(codec
, cfg
->inputs
[0].pin
);
2585 if (snd_hda_get_input_pin_attr(defcfg
) != INPUT_PIN_ATTR_INT
)
2590 spec
->hp_mic
= 0; /* clear once */
2591 if (cfg
->num_inputs
>= AUTO_CFG_MAX_INS
)
2595 if (cfg
->line_out_type
== AUTO_PIN_HP_OUT
&& cfg
->line_outs
> 0)
2596 nid
= cfg
->line_out_pins
[0];
2597 else if (cfg
->hp_outs
> 0)
2598 nid
= cfg
->hp_pins
[0];
2602 if (!(snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_IN
))
2603 return 0; /* no input */
2605 cfg
->inputs
[cfg
->num_inputs
].pin
= nid
;
2606 cfg
->inputs
[cfg
->num_inputs
].type
= AUTO_PIN_MIC
;
2607 cfg
->inputs
[cfg
->num_inputs
].is_headphone_mic
= 1;
2610 spec
->hp_mic_pin
= nid
;
2611 /* we can't handle auto-mic together with HP-mic */
2612 spec
->suppress_auto_mic
= 1;
2613 codec_dbg(codec
, "Enable shared I/O jack on NID 0x%x\n", nid
);
2621 static int create_hp_mic_jack_mode(struct hda_codec
*codec
, hda_nid_t pin
);
2623 static const char * const out_jack_texts
[] = {
2624 "Line Out", "Headphone Out",
2627 static int out_jack_mode_info(struct snd_kcontrol
*kcontrol
,
2628 struct snd_ctl_elem_info
*uinfo
)
2630 return snd_hda_enum_helper_info(kcontrol
, uinfo
, 2, out_jack_texts
);
2633 static int out_jack_mode_get(struct snd_kcontrol
*kcontrol
,
2634 struct snd_ctl_elem_value
*ucontrol
)
2636 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2637 hda_nid_t nid
= kcontrol
->private_value
;
2638 if (snd_hda_codec_get_pin_target(codec
, nid
) == PIN_HP
)
2639 ucontrol
->value
.enumerated
.item
[0] = 1;
2641 ucontrol
->value
.enumerated
.item
[0] = 0;
2645 static int out_jack_mode_put(struct snd_kcontrol
*kcontrol
,
2646 struct snd_ctl_elem_value
*ucontrol
)
2648 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2649 hda_nid_t nid
= kcontrol
->private_value
;
2652 val
= ucontrol
->value
.enumerated
.item
[0] ? PIN_HP
: PIN_OUT
;
2653 if (snd_hda_codec_get_pin_target(codec
, nid
) == val
)
2655 snd_hda_set_pin_ctl_cache(codec
, nid
, val
);
2659 static const struct snd_kcontrol_new out_jack_mode_enum
= {
2660 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2661 .info
= out_jack_mode_info
,
2662 .get
= out_jack_mode_get
,
2663 .put
= out_jack_mode_put
,
2666 static bool find_kctl_name(struct hda_codec
*codec
, const char *name
, int idx
)
2668 struct hda_gen_spec
*spec
= codec
->spec
;
2671 for (i
= 0; i
< spec
->kctls
.used
; i
++) {
2672 struct snd_kcontrol_new
*kctl
= snd_array_elem(&spec
->kctls
, i
);
2673 if (!strcmp(kctl
->name
, name
) && kctl
->index
== idx
)
2679 static void get_jack_mode_name(struct hda_codec
*codec
, hda_nid_t pin
,
2680 char *name
, size_t name_len
)
2682 struct hda_gen_spec
*spec
= codec
->spec
;
2685 snd_hda_get_pin_label(codec
, pin
, &spec
->autocfg
, name
, name_len
, &idx
);
2686 strlcat(name
, " Jack Mode", name_len
);
2688 for (; find_kctl_name(codec
, name
, idx
); idx
++)
2692 static int get_out_jack_num_items(struct hda_codec
*codec
, hda_nid_t pin
)
2694 struct hda_gen_spec
*spec
= codec
->spec
;
2695 if (spec
->add_jack_modes
) {
2696 unsigned int pincap
= snd_hda_query_pin_caps(codec
, pin
);
2697 if ((pincap
& AC_PINCAP_OUT
) && (pincap
& AC_PINCAP_HP_DRV
))
2703 static int create_out_jack_modes(struct hda_codec
*codec
, int num_pins
,
2706 struct hda_gen_spec
*spec
= codec
->spec
;
2709 for (i
= 0; i
< num_pins
; i
++) {
2710 hda_nid_t pin
= pins
[i
];
2711 if (pin
== spec
->hp_mic_pin
)
2713 if (get_out_jack_num_items(codec
, pin
) > 1) {
2714 struct snd_kcontrol_new
*knew
;
2715 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
2716 get_jack_mode_name(codec
, pin
, name
, sizeof(name
));
2717 knew
= snd_hda_gen_add_kctl(spec
, name
,
2718 &out_jack_mode_enum
);
2721 knew
->private_value
= pin
;
2732 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2735 static const char * const vref_texts
[NUM_VREFS
] = {
2736 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2737 "", "Mic 80pc Bias", "Mic 100pc Bias"
2740 static unsigned int get_vref_caps(struct hda_codec
*codec
, hda_nid_t pin
)
2742 unsigned int pincap
;
2744 pincap
= snd_hda_query_pin_caps(codec
, pin
);
2745 pincap
= (pincap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
2746 /* filter out unusual vrefs */
2747 pincap
&= ~(AC_PINCAP_VREF_GRD
| AC_PINCAP_VREF_100
);
2751 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2752 static int get_vref_idx(unsigned int vref_caps
, unsigned int item_idx
)
2754 unsigned int i
, n
= 0;
2756 for (i
= 0; i
< NUM_VREFS
; i
++) {
2757 if (vref_caps
& (1 << i
)) {
2766 /* convert back from the vref ctl index to the enum item index */
2767 static int cvt_from_vref_idx(unsigned int vref_caps
, unsigned int idx
)
2769 unsigned int i
, n
= 0;
2771 for (i
= 0; i
< NUM_VREFS
; i
++) {
2774 if (vref_caps
& (1 << i
))
2780 static int in_jack_mode_info(struct snd_kcontrol
*kcontrol
,
2781 struct snd_ctl_elem_info
*uinfo
)
2783 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2784 hda_nid_t nid
= kcontrol
->private_value
;
2785 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2787 snd_hda_enum_helper_info(kcontrol
, uinfo
, hweight32(vref_caps
),
2789 /* set the right text */
2790 strcpy(uinfo
->value
.enumerated
.name
,
2791 vref_texts
[get_vref_idx(vref_caps
, uinfo
->value
.enumerated
.item
)]);
2795 static int in_jack_mode_get(struct snd_kcontrol
*kcontrol
,
2796 struct snd_ctl_elem_value
*ucontrol
)
2798 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2799 hda_nid_t nid
= kcontrol
->private_value
;
2800 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2803 idx
= snd_hda_codec_get_pin_target(codec
, nid
) & AC_PINCTL_VREFEN
;
2804 ucontrol
->value
.enumerated
.item
[0] = cvt_from_vref_idx(vref_caps
, idx
);
2808 static int in_jack_mode_put(struct snd_kcontrol
*kcontrol
,
2809 struct snd_ctl_elem_value
*ucontrol
)
2811 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2812 hda_nid_t nid
= kcontrol
->private_value
;
2813 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2814 unsigned int val
, idx
;
2816 val
= snd_hda_codec_get_pin_target(codec
, nid
);
2817 idx
= cvt_from_vref_idx(vref_caps
, val
& AC_PINCTL_VREFEN
);
2818 if (idx
== ucontrol
->value
.enumerated
.item
[0])
2821 val
&= ~AC_PINCTL_VREFEN
;
2822 val
|= get_vref_idx(vref_caps
, ucontrol
->value
.enumerated
.item
[0]);
2823 snd_hda_set_pin_ctl_cache(codec
, nid
, val
);
2827 static const struct snd_kcontrol_new in_jack_mode_enum
= {
2828 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2829 .info
= in_jack_mode_info
,
2830 .get
= in_jack_mode_get
,
2831 .put
= in_jack_mode_put
,
2834 static int get_in_jack_num_items(struct hda_codec
*codec
, hda_nid_t pin
)
2836 struct hda_gen_spec
*spec
= codec
->spec
;
2838 if (spec
->add_jack_modes
)
2839 nitems
= hweight32(get_vref_caps(codec
, pin
));
2840 return nitems
? nitems
: 1;
2843 static int create_in_jack_mode(struct hda_codec
*codec
, hda_nid_t pin
)
2845 struct hda_gen_spec
*spec
= codec
->spec
;
2846 struct snd_kcontrol_new
*knew
;
2847 char name
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
2848 unsigned int defcfg
;
2850 if (pin
== spec
->hp_mic_pin
)
2851 return 0; /* already done in create_out_jack_mode() */
2853 /* no jack mode for fixed pins */
2854 defcfg
= snd_hda_codec_get_pincfg(codec
, pin
);
2855 if (snd_hda_get_input_pin_attr(defcfg
) == INPUT_PIN_ATTR_INT
)
2858 /* no multiple vref caps? */
2859 if (get_in_jack_num_items(codec
, pin
) <= 1)
2862 get_jack_mode_name(codec
, pin
, name
, sizeof(name
));
2863 knew
= snd_hda_gen_add_kctl(spec
, name
, &in_jack_mode_enum
);
2866 knew
->private_value
= pin
;
2871 * HP/mic shared jack mode
2873 static int hp_mic_jack_mode_info(struct snd_kcontrol
*kcontrol
,
2874 struct snd_ctl_elem_info
*uinfo
)
2876 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2877 hda_nid_t nid
= kcontrol
->private_value
;
2878 int out_jacks
= get_out_jack_num_items(codec
, nid
);
2879 int in_jacks
= get_in_jack_num_items(codec
, nid
);
2880 const char *text
= NULL
;
2883 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
2885 uinfo
->value
.enumerated
.items
= out_jacks
+ in_jacks
;
2886 if (uinfo
->value
.enumerated
.item
>= uinfo
->value
.enumerated
.items
)
2887 uinfo
->value
.enumerated
.item
= uinfo
->value
.enumerated
.items
- 1;
2888 idx
= uinfo
->value
.enumerated
.item
;
2889 if (idx
< out_jacks
) {
2891 text
= out_jack_texts
[idx
];
2893 text
= "Headphone Out";
2897 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2898 text
= vref_texts
[get_vref_idx(vref_caps
, idx
)];
2903 strcpy(uinfo
->value
.enumerated
.name
, text
);
2907 static int get_cur_hp_mic_jack_mode(struct hda_codec
*codec
, hda_nid_t nid
)
2909 int out_jacks
= get_out_jack_num_items(codec
, nid
);
2910 int in_jacks
= get_in_jack_num_items(codec
, nid
);
2911 unsigned int val
= snd_hda_codec_get_pin_target(codec
, nid
);
2914 if (val
& PIN_OUT
) {
2915 if (out_jacks
> 1 && val
== PIN_HP
)
2917 } else if (val
& PIN_IN
) {
2920 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2921 val
&= AC_PINCTL_VREFEN
;
2922 idx
+= cvt_from_vref_idx(vref_caps
, val
);
2928 static int hp_mic_jack_mode_get(struct snd_kcontrol
*kcontrol
,
2929 struct snd_ctl_elem_value
*ucontrol
)
2931 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2932 hda_nid_t nid
= kcontrol
->private_value
;
2933 ucontrol
->value
.enumerated
.item
[0] =
2934 get_cur_hp_mic_jack_mode(codec
, nid
);
2938 static int hp_mic_jack_mode_put(struct snd_kcontrol
*kcontrol
,
2939 struct snd_ctl_elem_value
*ucontrol
)
2941 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2942 hda_nid_t nid
= kcontrol
->private_value
;
2943 int out_jacks
= get_out_jack_num_items(codec
, nid
);
2944 int in_jacks
= get_in_jack_num_items(codec
, nid
);
2945 unsigned int val
, oldval
, idx
;
2947 oldval
= get_cur_hp_mic_jack_mode(codec
, nid
);
2948 idx
= ucontrol
->value
.enumerated
.item
[0];
2952 if (idx
< out_jacks
) {
2954 val
= idx
? PIN_HP
: PIN_OUT
;
2960 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2961 val
= snd_hda_codec_get_pin_target(codec
, nid
);
2962 val
&= ~(AC_PINCTL_VREFEN
| PIN_HP
);
2963 val
|= get_vref_idx(vref_caps
, idx
) | PIN_IN
;
2965 val
= snd_hda_get_default_vref(codec
, nid
) | PIN_IN
;
2967 snd_hda_set_pin_ctl_cache(codec
, nid
, val
);
2968 call_hp_automute(codec
, NULL
);
2973 static const struct snd_kcontrol_new hp_mic_jack_mode_enum
= {
2974 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2975 .info
= hp_mic_jack_mode_info
,
2976 .get
= hp_mic_jack_mode_get
,
2977 .put
= hp_mic_jack_mode_put
,
2980 static int create_hp_mic_jack_mode(struct hda_codec
*codec
, hda_nid_t pin
)
2982 struct hda_gen_spec
*spec
= codec
->spec
;
2983 struct snd_kcontrol_new
*knew
;
2985 knew
= snd_hda_gen_add_kctl(spec
, "Headphone Mic Jack Mode",
2986 &hp_mic_jack_mode_enum
);
2989 knew
->private_value
= pin
;
2990 spec
->hp_mic_jack_modes
= 1;
2998 /* add the powersave loopback-list entry */
2999 static int add_loopback_list(struct hda_gen_spec
*spec
, hda_nid_t mix
, int idx
)
3001 struct hda_amp_list
*list
;
3003 list
= snd_array_new(&spec
->loopback_list
);
3007 list
->dir
= HDA_INPUT
;
3009 spec
->loopback
.amplist
= spec
->loopback_list
.list
;
3013 /* return true if either a volume or a mute amp is found for the given
3014 * aamix path; the amp has to be either in the mixer node or its direct leaf
3016 static bool look_for_mix_leaf_ctls(struct hda_codec
*codec
, hda_nid_t mix_nid
,
3017 hda_nid_t pin
, unsigned int *mix_val
,
3018 unsigned int *mute_val
)
3021 const hda_nid_t
*list
;
3024 idx
= snd_hda_get_conn_index(codec
, mix_nid
, pin
, true);
3028 *mix_val
= *mute_val
= 0;
3029 if (nid_has_volume(codec
, mix_nid
, HDA_INPUT
))
3030 *mix_val
= HDA_COMPOSE_AMP_VAL(mix_nid
, 3, idx
, HDA_INPUT
);
3031 if (nid_has_mute(codec
, mix_nid
, HDA_INPUT
))
3032 *mute_val
= HDA_COMPOSE_AMP_VAL(mix_nid
, 3, idx
, HDA_INPUT
);
3033 if (*mix_val
&& *mute_val
)
3036 /* check leaf node */
3037 num_conns
= snd_hda_get_conn_list(codec
, mix_nid
, &list
);
3038 if (num_conns
< idx
)
3041 if (!*mix_val
&& nid_has_volume(codec
, nid
, HDA_OUTPUT
) &&
3042 !is_ctl_associated(codec
, nid
, HDA_OUTPUT
, 0, NID_PATH_VOL_CTL
))
3043 *mix_val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3044 if (!*mute_val
&& nid_has_mute(codec
, nid
, HDA_OUTPUT
) &&
3045 !is_ctl_associated(codec
, nid
, HDA_OUTPUT
, 0, NID_PATH_MUTE_CTL
))
3046 *mute_val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3048 return *mix_val
|| *mute_val
;
3051 /* create input playback/capture controls for the given pin */
3052 static int new_analog_input(struct hda_codec
*codec
, int input_idx
,
3053 hda_nid_t pin
, const char *ctlname
, int ctlidx
,
3056 struct hda_gen_spec
*spec
= codec
->spec
;
3057 struct nid_path
*path
;
3058 unsigned int mix_val
, mute_val
;
3061 if (!look_for_mix_leaf_ctls(codec
, mix_nid
, pin
, &mix_val
, &mute_val
))
3064 path
= snd_hda_add_new_path(codec
, pin
, mix_nid
, 0);
3067 print_nid_path(codec
, "loopback", path
);
3068 spec
->loopback_paths
[input_idx
] = snd_hda_get_path_idx(codec
, path
);
3070 idx
= path
->idx
[path
->depth
- 1];
3072 err
= __add_pb_vol_ctrl(spec
, HDA_CTL_WIDGET_VOL
, ctlname
, ctlidx
, mix_val
);
3075 path
->ctls
[NID_PATH_VOL_CTL
] = mix_val
;
3079 err
= __add_pb_sw_ctrl(spec
, HDA_CTL_WIDGET_MUTE
, ctlname
, ctlidx
, mute_val
);
3082 path
->ctls
[NID_PATH_MUTE_CTL
] = mute_val
;
3085 path
->active
= true;
3086 path
->stream_enabled
= true; /* no DAC/ADC involved */
3087 err
= add_loopback_list(spec
, mix_nid
, idx
);
3091 if (spec
->mixer_nid
!= spec
->mixer_merge_nid
&&
3092 !spec
->loopback_merge_path
) {
3093 path
= snd_hda_add_new_path(codec
, spec
->mixer_nid
,
3094 spec
->mixer_merge_nid
, 0);
3096 print_nid_path(codec
, "loopback-merge", path
);
3097 path
->active
= true;
3098 path
->pin_fixed
= true; /* static route */
3099 path
->stream_enabled
= true; /* no DAC/ADC involved */
3100 spec
->loopback_merge_path
=
3101 snd_hda_get_path_idx(codec
, path
);
3108 static int is_input_pin(struct hda_codec
*codec
, hda_nid_t nid
)
3110 unsigned int pincap
= snd_hda_query_pin_caps(codec
, nid
);
3111 return (pincap
& AC_PINCAP_IN
) != 0;
3114 /* Parse the codec tree and retrieve ADCs */
3115 static int fill_adc_nids(struct hda_codec
*codec
)
3117 struct hda_gen_spec
*spec
= codec
->spec
;
3119 hda_nid_t
*adc_nids
= spec
->adc_nids
;
3120 int max_nums
= ARRAY_SIZE(spec
->adc_nids
);
3123 for_each_hda_codec_node(nid
, codec
) {
3124 unsigned int caps
= get_wcaps(codec
, nid
);
3125 int type
= get_wcaps_type(caps
);
3127 if (type
!= AC_WID_AUD_IN
|| (caps
& AC_WCAP_DIGITAL
))
3129 adc_nids
[nums
] = nid
;
3130 if (++nums
>= max_nums
)
3133 spec
->num_adc_nids
= nums
;
3135 /* copy the detected ADCs to all_adcs[] */
3136 spec
->num_all_adcs
= nums
;
3137 memcpy(spec
->all_adcs
, spec
->adc_nids
, nums
* sizeof(hda_nid_t
));
3142 /* filter out invalid adc_nids that don't give all active input pins;
3143 * if needed, check whether dynamic ADC-switching is available
3145 static int check_dyn_adc_switch(struct hda_codec
*codec
)
3147 struct hda_gen_spec
*spec
= codec
->spec
;
3148 struct hda_input_mux
*imux
= &spec
->input_mux
;
3149 unsigned int ok_bits
;
3154 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
3155 for (i
= 0; i
< imux
->num_items
; i
++) {
3156 if (!spec
->input_paths
[i
][n
])
3159 if (i
>= imux
->num_items
) {
3160 ok_bits
|= (1 << n
);
3166 /* check whether ADC-switch is possible */
3167 for (i
= 0; i
< imux
->num_items
; i
++) {
3168 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
3169 if (spec
->input_paths
[i
][n
]) {
3170 spec
->dyn_adc_idx
[i
] = n
;
3176 codec_dbg(codec
, "enabling ADC switching\n");
3177 spec
->dyn_adc_switch
= 1;
3178 } else if (nums
!= spec
->num_adc_nids
) {
3179 /* shrink the invalid adcs and input paths */
3181 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
3182 if (!(ok_bits
& (1 << n
)))
3185 spec
->adc_nids
[nums
] = spec
->adc_nids
[n
];
3186 for (i
= 0; i
< imux
->num_items
; i
++) {
3187 invalidate_nid_path(codec
,
3188 spec
->input_paths
[i
][nums
]);
3189 spec
->input_paths
[i
][nums
] =
3190 spec
->input_paths
[i
][n
];
3195 spec
->num_adc_nids
= nums
;
3198 if (imux
->num_items
== 1 ||
3199 (imux
->num_items
== 2 && spec
->hp_mic
)) {
3200 codec_dbg(codec
, "reducing to a single ADC\n");
3201 spec
->num_adc_nids
= 1; /* reduce to a single ADC */
3204 /* single index for individual volumes ctls */
3205 if (!spec
->dyn_adc_switch
&& spec
->multi_cap_vol
)
3206 spec
->num_adc_nids
= 1;
3211 /* parse capture source paths from the given pin and create imux items */
3212 static int parse_capture_source(struct hda_codec
*codec
, hda_nid_t pin
,
3213 int cfg_idx
, int num_adcs
,
3214 const char *label
, int anchor
)
3216 struct hda_gen_spec
*spec
= codec
->spec
;
3217 struct hda_input_mux
*imux
= &spec
->input_mux
;
3218 int imux_idx
= imux
->num_items
;
3219 bool imux_added
= false;
3222 for (c
= 0; c
< num_adcs
; c
++) {
3223 struct nid_path
*path
;
3224 hda_nid_t adc
= spec
->adc_nids
[c
];
3226 if (!is_reachable_path(codec
, pin
, adc
))
3228 path
= snd_hda_add_new_path(codec
, pin
, adc
, anchor
);
3231 print_nid_path(codec
, "input", path
);
3232 spec
->input_paths
[imux_idx
][c
] =
3233 snd_hda_get_path_idx(codec
, path
);
3236 if (spec
->hp_mic_pin
== pin
)
3237 spec
->hp_mic_mux_idx
= imux
->num_items
;
3238 spec
->imux_pins
[imux
->num_items
] = pin
;
3239 snd_hda_add_imux_item(codec
, imux
, label
, cfg_idx
, NULL
);
3241 if (spec
->dyn_adc_switch
)
3242 spec
->dyn_adc_idx
[imux_idx
] = c
;
3250 * create playback/capture controls for input pins
3253 /* fill the label for each input at first */
3254 static int fill_input_pin_labels(struct hda_codec
*codec
)
3256 struct hda_gen_spec
*spec
= codec
->spec
;
3257 const struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3260 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
3261 hda_nid_t pin
= cfg
->inputs
[i
].pin
;
3265 if (!is_input_pin(codec
, pin
))
3268 label
= hda_get_autocfg_input_label(codec
, cfg
, i
);
3270 for (j
= i
- 1; j
>= 0; j
--) {
3271 if (spec
->input_labels
[j
] &&
3272 !strcmp(spec
->input_labels
[j
], label
)) {
3273 idx
= spec
->input_label_idxs
[j
] + 1;
3278 spec
->input_labels
[i
] = label
;
3279 spec
->input_label_idxs
[i
] = idx
;
3285 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
3287 static int create_input_ctls(struct hda_codec
*codec
)
3289 struct hda_gen_spec
*spec
= codec
->spec
;
3290 const struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3291 hda_nid_t mixer
= spec
->mixer_nid
;
3296 num_adcs
= fill_adc_nids(codec
);
3300 err
= fill_input_pin_labels(codec
);
3304 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
3307 pin
= cfg
->inputs
[i
].pin
;
3308 if (!is_input_pin(codec
, pin
))
3312 if (cfg
->inputs
[i
].type
== AUTO_PIN_MIC
)
3313 val
|= snd_hda_get_default_vref(codec
, pin
);
3314 if (pin
!= spec
->hp_mic_pin
&&
3315 !snd_hda_codec_get_pin_target(codec
, pin
))
3316 set_pin_target(codec
, pin
, val
, false);
3319 if (is_reachable_path(codec
, pin
, mixer
)) {
3320 err
= new_analog_input(codec
, i
, pin
,
3321 spec
->input_labels
[i
],
3322 spec
->input_label_idxs
[i
],
3329 err
= parse_capture_source(codec
, pin
, i
, num_adcs
,
3330 spec
->input_labels
[i
], -mixer
);
3334 if (spec
->add_jack_modes
) {
3335 err
= create_in_jack_mode(codec
, pin
);
3341 /* add stereo mix when explicitly enabled via hint */
3342 if (mixer
&& spec
->add_stereo_mix_input
== HDA_HINT_STEREO_MIX_ENABLE
) {
3343 err
= parse_capture_source(codec
, mixer
, CFG_IDX_MIX
, num_adcs
,
3348 spec
->suppress_auto_mic
= 1;
3359 /* get the input path specified by the given adc and imux indices */
3360 static struct nid_path
*get_input_path(struct hda_codec
*codec
, int adc_idx
, int imux_idx
)
3362 struct hda_gen_spec
*spec
= codec
->spec
;
3363 if (imux_idx
< 0 || imux_idx
>= HDA_MAX_NUM_INPUTS
) {
3367 if (spec
->dyn_adc_switch
)
3368 adc_idx
= spec
->dyn_adc_idx
[imux_idx
];
3369 if (adc_idx
< 0 || adc_idx
>= AUTO_CFG_MAX_INS
) {
3373 return snd_hda_get_path_from_idx(codec
, spec
->input_paths
[imux_idx
][adc_idx
]);
3376 static int mux_select(struct hda_codec
*codec
, unsigned int adc_idx
,
3379 static int mux_enum_info(struct snd_kcontrol
*kcontrol
,
3380 struct snd_ctl_elem_info
*uinfo
)
3382 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3383 struct hda_gen_spec
*spec
= codec
->spec
;
3384 return snd_hda_input_mux_info(&spec
->input_mux
, uinfo
);
3387 static int mux_enum_get(struct snd_kcontrol
*kcontrol
,
3388 struct snd_ctl_elem_value
*ucontrol
)
3390 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3391 struct hda_gen_spec
*spec
= codec
->spec
;
3392 /* the ctls are created at once with multiple counts */
3393 unsigned int adc_idx
= snd_ctl_get_ioffidx(kcontrol
, &ucontrol
->id
);
3395 ucontrol
->value
.enumerated
.item
[0] = spec
->cur_mux
[adc_idx
];
3399 static int mux_enum_put(struct snd_kcontrol
*kcontrol
,
3400 struct snd_ctl_elem_value
*ucontrol
)
3402 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3403 unsigned int adc_idx
= snd_ctl_get_ioffidx(kcontrol
, &ucontrol
->id
);
3404 return mux_select(codec
, adc_idx
,
3405 ucontrol
->value
.enumerated
.item
[0]);
3408 static const struct snd_kcontrol_new cap_src_temp
= {
3409 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3410 .name
= "Input Source",
3411 .info
= mux_enum_info
,
3412 .get
= mux_enum_get
,
3413 .put
= mux_enum_put
,
3417 * capture volume and capture switch ctls
3420 typedef int (*put_call_t
)(struct snd_kcontrol
*kcontrol
,
3421 struct snd_ctl_elem_value
*ucontrol
);
3423 /* call the given amp update function for all amps in the imux list at once */
3424 static int cap_put_caller(struct snd_kcontrol
*kcontrol
,
3425 struct snd_ctl_elem_value
*ucontrol
,
3426 put_call_t func
, int type
)
3428 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3429 struct hda_gen_spec
*spec
= codec
->spec
;
3430 const struct hda_input_mux
*imux
;
3431 struct nid_path
*path
;
3432 int i
, adc_idx
, err
= 0;
3434 imux
= &spec
->input_mux
;
3435 adc_idx
= kcontrol
->id
.index
;
3436 mutex_lock(&codec
->control_mutex
);
3437 for (i
= 0; i
< imux
->num_items
; i
++) {
3438 path
= get_input_path(codec
, adc_idx
, i
);
3439 if (!path
|| !path
->ctls
[type
])
3441 kcontrol
->private_value
= path
->ctls
[type
];
3442 err
= func(kcontrol
, ucontrol
);
3446 mutex_unlock(&codec
->control_mutex
);
3447 if (err
>= 0 && spec
->cap_sync_hook
)
3448 spec
->cap_sync_hook(codec
, kcontrol
, ucontrol
);
3452 /* capture volume ctl callbacks */
3453 #define cap_vol_info snd_hda_mixer_amp_volume_info
3454 #define cap_vol_get snd_hda_mixer_amp_volume_get
3455 #define cap_vol_tlv snd_hda_mixer_amp_tlv
3457 static int cap_vol_put(struct snd_kcontrol
*kcontrol
,
3458 struct snd_ctl_elem_value
*ucontrol
)
3460 return cap_put_caller(kcontrol
, ucontrol
,
3461 snd_hda_mixer_amp_volume_put
,
3465 static const struct snd_kcontrol_new cap_vol_temp
= {
3466 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3467 .name
= "Capture Volume",
3468 .access
= (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
3469 SNDRV_CTL_ELEM_ACCESS_TLV_READ
|
3470 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
),
3471 .info
= cap_vol_info
,
3474 .tlv
= { .c
= cap_vol_tlv
},
3477 /* capture switch ctl callbacks */
3478 #define cap_sw_info snd_ctl_boolean_stereo_info
3479 #define cap_sw_get snd_hda_mixer_amp_switch_get
3481 static int cap_sw_put(struct snd_kcontrol
*kcontrol
,
3482 struct snd_ctl_elem_value
*ucontrol
)
3484 return cap_put_caller(kcontrol
, ucontrol
,
3485 snd_hda_mixer_amp_switch_put
,
3489 static const struct snd_kcontrol_new cap_sw_temp
= {
3490 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3491 .name
= "Capture Switch",
3492 .info
= cap_sw_info
,
3497 static int parse_capvol_in_path(struct hda_codec
*codec
, struct nid_path
*path
)
3502 path
->ctls
[NID_PATH_VOL_CTL
] = path
->ctls
[NID_PATH_MUTE_CTL
] = 0;
3503 for (depth
= 0; depth
< 3; depth
++) {
3504 if (depth
>= path
->depth
)
3506 i
= path
->depth
- depth
- 1;
3507 nid
= path
->path
[i
];
3508 if (!path
->ctls
[NID_PATH_VOL_CTL
]) {
3509 if (nid_has_volume(codec
, nid
, HDA_OUTPUT
))
3510 path
->ctls
[NID_PATH_VOL_CTL
] =
3511 HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3512 else if (nid_has_volume(codec
, nid
, HDA_INPUT
)) {
3513 int idx
= path
->idx
[i
];
3514 if (!depth
&& codec
->single_adc_amp
)
3516 path
->ctls
[NID_PATH_VOL_CTL
] =
3517 HDA_COMPOSE_AMP_VAL(nid
, 3, idx
, HDA_INPUT
);
3520 if (!path
->ctls
[NID_PATH_MUTE_CTL
]) {
3521 if (nid_has_mute(codec
, nid
, HDA_OUTPUT
))
3522 path
->ctls
[NID_PATH_MUTE_CTL
] =
3523 HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3524 else if (nid_has_mute(codec
, nid
, HDA_INPUT
)) {
3525 int idx
= path
->idx
[i
];
3526 if (!depth
&& codec
->single_adc_amp
)
3528 path
->ctls
[NID_PATH_MUTE_CTL
] =
3529 HDA_COMPOSE_AMP_VAL(nid
, 3, idx
, HDA_INPUT
);
3536 static bool is_inv_dmic_pin(struct hda_codec
*codec
, hda_nid_t nid
)
3538 struct hda_gen_spec
*spec
= codec
->spec
;
3539 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3543 if (!spec
->inv_dmic_split
)
3545 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
3546 if (cfg
->inputs
[i
].pin
!= nid
)
3548 if (cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
3550 val
= snd_hda_codec_get_pincfg(codec
, nid
);
3551 return snd_hda_get_input_pin_attr(val
) == INPUT_PIN_ATTR_INT
;
3556 /* capture switch put callback for a single control with hook call */
3557 static int cap_single_sw_put(struct snd_kcontrol
*kcontrol
,
3558 struct snd_ctl_elem_value
*ucontrol
)
3560 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3561 struct hda_gen_spec
*spec
= codec
->spec
;
3564 ret
= snd_hda_mixer_amp_switch_put(kcontrol
, ucontrol
);
3568 if (spec
->cap_sync_hook
)
3569 spec
->cap_sync_hook(codec
, kcontrol
, ucontrol
);
3574 static int add_single_cap_ctl(struct hda_codec
*codec
, const char *label
,
3575 int idx
, bool is_switch
, unsigned int ctl
,
3578 struct hda_gen_spec
*spec
= codec
->spec
;
3579 char tmpname
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
3580 int type
= is_switch
? HDA_CTL_WIDGET_MUTE
: HDA_CTL_WIDGET_VOL
;
3581 const char *sfx
= is_switch
? "Switch" : "Volume";
3582 unsigned int chs
= inv_dmic
? 1 : 3;
3583 struct snd_kcontrol_new
*knew
;
3589 snprintf(tmpname
, sizeof(tmpname
),
3590 "%s Capture %s", label
, sfx
);
3592 snprintf(tmpname
, sizeof(tmpname
),
3594 knew
= add_control(spec
, type
, tmpname
, idx
,
3595 amp_val_replace_channels(ctl
, chs
));
3599 knew
->put
= cap_single_sw_put
;
3603 /* Make independent right kcontrol */
3605 snprintf(tmpname
, sizeof(tmpname
),
3606 "Inverted %s Capture %s", label
, sfx
);
3608 snprintf(tmpname
, sizeof(tmpname
),
3609 "Inverted Capture %s", sfx
);
3610 knew
= add_control(spec
, type
, tmpname
, idx
,
3611 amp_val_replace_channels(ctl
, 2));
3615 knew
->put
= cap_single_sw_put
;
3619 /* create single (and simple) capture volume and switch controls */
3620 static int create_single_cap_vol_ctl(struct hda_codec
*codec
, int idx
,
3621 unsigned int vol_ctl
, unsigned int sw_ctl
,
3625 err
= add_single_cap_ctl(codec
, NULL
, idx
, false, vol_ctl
, inv_dmic
);
3628 err
= add_single_cap_ctl(codec
, NULL
, idx
, true, sw_ctl
, inv_dmic
);
3634 /* create bound capture volume and switch controls */
3635 static int create_bind_cap_vol_ctl(struct hda_codec
*codec
, int idx
,
3636 unsigned int vol_ctl
, unsigned int sw_ctl
)
3638 struct hda_gen_spec
*spec
= codec
->spec
;
3639 struct snd_kcontrol_new
*knew
;
3642 knew
= snd_hda_gen_add_kctl(spec
, NULL
, &cap_vol_temp
);
3646 knew
->private_value
= vol_ctl
;
3647 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
3650 knew
= snd_hda_gen_add_kctl(spec
, NULL
, &cap_sw_temp
);
3654 knew
->private_value
= sw_ctl
;
3655 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
3660 /* return the vol ctl when used first in the imux list */
3661 static unsigned int get_first_cap_ctl(struct hda_codec
*codec
, int idx
, int type
)
3663 struct nid_path
*path
;
3667 path
= get_input_path(codec
, 0, idx
);
3670 ctl
= path
->ctls
[type
];
3673 for (i
= 0; i
< idx
- 1; i
++) {
3674 path
= get_input_path(codec
, 0, i
);
3675 if (path
&& path
->ctls
[type
] == ctl
)
3681 /* create individual capture volume and switch controls per input */
3682 static int create_multi_cap_vol_ctl(struct hda_codec
*codec
)
3684 struct hda_gen_spec
*spec
= codec
->spec
;
3685 struct hda_input_mux
*imux
= &spec
->input_mux
;
3688 for (i
= 0; i
< imux
->num_items
; i
++) {
3692 idx
= imux
->items
[i
].index
;
3693 if (idx
>= spec
->autocfg
.num_inputs
)
3695 inv_dmic
= is_inv_dmic_pin(codec
, spec
->imux_pins
[i
]);
3697 for (type
= 0; type
< 2; type
++) {
3698 err
= add_single_cap_ctl(codec
,
3699 spec
->input_labels
[idx
],
3700 spec
->input_label_idxs
[idx
],
3702 get_first_cap_ctl(codec
, i
, type
),
3711 static int create_capture_mixers(struct hda_codec
*codec
)
3713 struct hda_gen_spec
*spec
= codec
->spec
;
3714 struct hda_input_mux
*imux
= &spec
->input_mux
;
3715 int i
, n
, nums
, err
;
3717 if (spec
->dyn_adc_switch
)
3720 nums
= spec
->num_adc_nids
;
3722 if (!spec
->auto_mic
&& imux
->num_items
> 1) {
3723 struct snd_kcontrol_new
*knew
;
3725 name
= nums
> 1 ? "Input Source" : "Capture Source";
3726 knew
= snd_hda_gen_add_kctl(spec
, name
, &cap_src_temp
);
3732 for (n
= 0; n
< nums
; n
++) {
3734 bool multi_cap_vol
= spec
->multi_cap_vol
;
3735 bool inv_dmic
= false;
3739 for (i
= 0; i
< imux
->num_items
; i
++) {
3740 struct nid_path
*path
;
3741 path
= get_input_path(codec
, n
, i
);
3744 parse_capvol_in_path(codec
, path
);
3746 vol
= path
->ctls
[NID_PATH_VOL_CTL
];
3747 else if (vol
!= path
->ctls
[NID_PATH_VOL_CTL
]) {
3749 if (!same_amp_caps(codec
, vol
,
3750 path
->ctls
[NID_PATH_VOL_CTL
], HDA_INPUT
))
3751 multi_cap_vol
= true;
3754 sw
= path
->ctls
[NID_PATH_MUTE_CTL
];
3755 else if (sw
!= path
->ctls
[NID_PATH_MUTE_CTL
]) {
3757 if (!same_amp_caps(codec
, sw
,
3758 path
->ctls
[NID_PATH_MUTE_CTL
], HDA_INPUT
))
3759 multi_cap_vol
= true;
3761 if (is_inv_dmic_pin(codec
, spec
->imux_pins
[i
]))
3766 err
= create_single_cap_vol_ctl(codec
, n
, vol
, sw
,
3768 else if (!multi_cap_vol
&& !inv_dmic
)
3769 err
= create_bind_cap_vol_ctl(codec
, n
, vol
, sw
);
3771 err
= create_multi_cap_vol_ctl(codec
);
3780 * add mic boosts if needed
3783 /* check whether the given amp is feasible as a boost volume */
3784 static bool check_boost_vol(struct hda_codec
*codec
, hda_nid_t nid
,
3789 if (!nid_has_volume(codec
, nid
, dir
) ||
3790 is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_VOL_CTL
) ||
3791 is_ctl_associated(codec
, nid
, dir
, idx
, NID_PATH_BOOST_CTL
))
3794 step
= (query_amp_caps(codec
, nid
, dir
) & AC_AMPCAP_STEP_SIZE
)
3795 >> AC_AMPCAP_STEP_SIZE_SHIFT
;
3801 /* look for a boost amp in a widget close to the pin */
3802 static unsigned int look_for_boost_amp(struct hda_codec
*codec
,
3803 struct nid_path
*path
)
3805 unsigned int val
= 0;
3809 for (depth
= 0; depth
< 3; depth
++) {
3810 if (depth
>= path
->depth
- 1)
3812 nid
= path
->path
[depth
];
3813 if (depth
&& check_boost_vol(codec
, nid
, HDA_OUTPUT
, 0)) {
3814 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
3816 } else if (check_boost_vol(codec
, nid
, HDA_INPUT
,
3817 path
->idx
[depth
])) {
3818 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, path
->idx
[depth
],
3827 static int parse_mic_boost(struct hda_codec
*codec
)
3829 struct hda_gen_spec
*spec
= codec
->spec
;
3830 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3831 struct hda_input_mux
*imux
= &spec
->input_mux
;
3834 if (!spec
->num_adc_nids
)
3837 for (i
= 0; i
< imux
->num_items
; i
++) {
3838 struct nid_path
*path
;
3841 char boost_label
[SNDRV_CTL_ELEM_ID_NAME_MAXLEN
];
3843 idx
= imux
->items
[i
].index
;
3844 if (idx
>= imux
->num_items
)
3847 /* check only line-in and mic pins */
3848 if (cfg
->inputs
[idx
].type
> AUTO_PIN_LINE_IN
)
3851 path
= get_input_path(codec
, 0, i
);
3855 val
= look_for_boost_amp(codec
, path
);
3859 /* create a boost control */
3860 snprintf(boost_label
, sizeof(boost_label
),
3861 "%s Boost Volume", spec
->input_labels
[idx
]);
3862 if (!add_control(spec
, HDA_CTL_WIDGET_VOL
, boost_label
,
3863 spec
->input_label_idxs
[idx
], val
))
3866 path
->ctls
[NID_PATH_BOOST_CTL
] = val
;
3872 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3874 static void parse_digital(struct hda_codec
*codec
)
3876 struct hda_gen_spec
*spec
= codec
->spec
;
3877 struct nid_path
*path
;
3879 hda_nid_t dig_nid
, pin
;
3881 /* support multiple SPDIFs; the secondary is set up as a slave */
3883 for (i
= 0; i
< spec
->autocfg
.dig_outs
; i
++) {
3884 pin
= spec
->autocfg
.dig_out_pins
[i
];
3885 dig_nid
= look_for_dac(codec
, pin
, true);
3888 path
= snd_hda_add_new_path(codec
, dig_nid
, pin
, 0);
3891 print_nid_path(codec
, "digout", path
);
3892 path
->active
= true;
3893 path
->pin_fixed
= true; /* no jack detection */
3894 spec
->digout_paths
[i
] = snd_hda_get_path_idx(codec
, path
);
3895 set_pin_target(codec
, pin
, PIN_OUT
, false);
3897 spec
->multiout
.dig_out_nid
= dig_nid
;
3898 spec
->dig_out_type
= spec
->autocfg
.dig_out_type
[0];
3900 spec
->multiout
.slave_dig_outs
= spec
->slave_dig_outs
;
3901 if (nums
>= ARRAY_SIZE(spec
->slave_dig_outs
) - 1)
3903 spec
->slave_dig_outs
[nums
- 1] = dig_nid
;
3908 if (spec
->autocfg
.dig_in_pin
) {
3909 pin
= spec
->autocfg
.dig_in_pin
;
3910 for_each_hda_codec_node(dig_nid
, codec
) {
3911 unsigned int wcaps
= get_wcaps(codec
, dig_nid
);
3912 if (get_wcaps_type(wcaps
) != AC_WID_AUD_IN
)
3914 if (!(wcaps
& AC_WCAP_DIGITAL
))
3916 path
= snd_hda_add_new_path(codec
, pin
, dig_nid
, 0);
3918 print_nid_path(codec
, "digin", path
);
3919 path
->active
= true;
3920 path
->pin_fixed
= true; /* no jack */
3921 spec
->dig_in_nid
= dig_nid
;
3922 spec
->digin_path
= snd_hda_get_path_idx(codec
, path
);
3923 set_pin_target(codec
, pin
, PIN_IN
, false);
3932 * input MUX handling
3935 static bool dyn_adc_pcm_resetup(struct hda_codec
*codec
, int cur
);
3937 /* select the given imux item; either unmute exclusively or select the route */
3938 static int mux_select(struct hda_codec
*codec
, unsigned int adc_idx
,
3941 struct hda_gen_spec
*spec
= codec
->spec
;
3942 const struct hda_input_mux
*imux
;
3943 struct nid_path
*old_path
, *path
;
3945 imux
= &spec
->input_mux
;
3946 if (!imux
->num_items
)
3949 if (idx
>= imux
->num_items
)
3950 idx
= imux
->num_items
- 1;
3951 if (spec
->cur_mux
[adc_idx
] == idx
)
3954 old_path
= get_input_path(codec
, adc_idx
, spec
->cur_mux
[adc_idx
]);
3957 if (old_path
->active
)
3958 snd_hda_activate_path(codec
, old_path
, false, false);
3960 spec
->cur_mux
[adc_idx
] = idx
;
3963 update_hp_mic(codec
, adc_idx
, false);
3965 if (spec
->dyn_adc_switch
)
3966 dyn_adc_pcm_resetup(codec
, idx
);
3968 path
= get_input_path(codec
, adc_idx
, idx
);
3973 snd_hda_activate_path(codec
, path
, true, false);
3974 if (spec
->cap_sync_hook
)
3975 spec
->cap_sync_hook(codec
, NULL
, NULL
);
3976 path_power_down_sync(codec
, old_path
);
3980 /* power up/down widgets in the all paths that match with the given NID
3981 * as terminals (either start- or endpoint)
3983 * returns the last changed NID, or zero if unchanged.
3985 static hda_nid_t
set_path_power(struct hda_codec
*codec
, hda_nid_t nid
,
3986 int pin_state
, int stream_state
)
3988 struct hda_gen_spec
*spec
= codec
->spec
;
3989 hda_nid_t last
, changed
= 0;
3990 struct nid_path
*path
;
3993 for (n
= 0; n
< spec
->paths
.used
; n
++) {
3994 path
= snd_array_elem(&spec
->paths
, n
);
3995 if (path
->path
[0] == nid
||
3996 path
->path
[path
->depth
- 1] == nid
) {
3997 bool pin_old
= path
->pin_enabled
;
3998 bool stream_old
= path
->stream_enabled
;
4001 path
->pin_enabled
= pin_state
;
4002 if (stream_state
>= 0)
4003 path
->stream_enabled
= stream_state
;
4004 if ((!path
->pin_fixed
&& path
->pin_enabled
!= pin_old
)
4005 || path
->stream_enabled
!= stream_old
) {
4006 last
= path_power_update(codec
, path
, true);
4015 /* check the jack status for power control */
4016 static bool detect_pin_state(struct hda_codec
*codec
, hda_nid_t pin
)
4018 if (!is_jack_detectable(codec
, pin
))
4020 return snd_hda_jack_detect_state(codec
, pin
) != HDA_JACK_NOT_PRESENT
;
4023 /* power up/down the paths of the given pin according to the jack state;
4024 * power = 0/1 : only power up/down if it matches with the jack state,
4025 * < 0 : force power up/down to follow the jack sate
4027 * returns the last changed NID, or zero if unchanged.
4029 static hda_nid_t
set_pin_power_jack(struct hda_codec
*codec
, hda_nid_t pin
,
4034 if (!codec
->power_save_node
)
4037 on
= detect_pin_state(codec
, pin
);
4039 if (power
>= 0 && on
!= power
)
4041 return set_path_power(codec
, pin
, on
, -1);
4044 static void pin_power_callback(struct hda_codec
*codec
,
4045 struct hda_jack_callback
*jack
,
4048 if (jack
&& jack
->tbl
->nid
)
4049 sync_power_state_change(codec
,
4050 set_pin_power_jack(codec
, jack
->tbl
->nid
, on
));
4053 /* callback only doing power up -- called at first */
4054 static void pin_power_up_callback(struct hda_codec
*codec
,
4055 struct hda_jack_callback
*jack
)
4057 pin_power_callback(codec
, jack
, true);
4060 /* callback only doing power down -- called at last */
4061 static void pin_power_down_callback(struct hda_codec
*codec
,
4062 struct hda_jack_callback
*jack
)
4064 pin_power_callback(codec
, jack
, false);
4067 /* set up the power up/down callbacks */
4068 static void add_pin_power_ctls(struct hda_codec
*codec
, int num_pins
,
4069 const hda_nid_t
*pins
, bool on
)
4072 hda_jack_callback_fn cb
=
4073 on
? pin_power_up_callback
: pin_power_down_callback
;
4075 for (i
= 0; i
< num_pins
&& pins
[i
]; i
++) {
4076 if (is_jack_detectable(codec
, pins
[i
]))
4077 snd_hda_jack_detect_enable_callback(codec
, pins
[i
], cb
);
4079 set_path_power(codec
, pins
[i
], true, -1);
4083 /* enabled power callback to each available I/O pin with jack detections;
4084 * the digital I/O pins are excluded because of the unreliable detectsion
4086 static void add_all_pin_power_ctls(struct hda_codec
*codec
, bool on
)
4088 struct hda_gen_spec
*spec
= codec
->spec
;
4089 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4092 if (!codec
->power_save_node
)
4094 add_pin_power_ctls(codec
, cfg
->line_outs
, cfg
->line_out_pins
, on
);
4095 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
4096 add_pin_power_ctls(codec
, cfg
->hp_outs
, cfg
->hp_pins
, on
);
4097 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
4098 add_pin_power_ctls(codec
, cfg
->speaker_outs
, cfg
->speaker_pins
, on
);
4099 for (i
= 0; i
< cfg
->num_inputs
; i
++)
4100 add_pin_power_ctls(codec
, 1, &cfg
->inputs
[i
].pin
, on
);
4103 /* sync path power up/down with the jack states of given pins */
4104 static void sync_pin_power_ctls(struct hda_codec
*codec
, int num_pins
,
4105 const hda_nid_t
*pins
)
4109 for (i
= 0; i
< num_pins
&& pins
[i
]; i
++)
4110 if (is_jack_detectable(codec
, pins
[i
]))
4111 set_pin_power_jack(codec
, pins
[i
], -1);
4114 /* sync path power up/down with pins; called at init and resume */
4115 static void sync_all_pin_power_ctls(struct hda_codec
*codec
)
4117 struct hda_gen_spec
*spec
= codec
->spec
;
4118 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4121 if (!codec
->power_save_node
)
4123 sync_pin_power_ctls(codec
, cfg
->line_outs
, cfg
->line_out_pins
);
4124 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
4125 sync_pin_power_ctls(codec
, cfg
->hp_outs
, cfg
->hp_pins
);
4126 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
4127 sync_pin_power_ctls(codec
, cfg
->speaker_outs
, cfg
->speaker_pins
);
4128 for (i
= 0; i
< cfg
->num_inputs
; i
++)
4129 sync_pin_power_ctls(codec
, 1, &cfg
->inputs
[i
].pin
);
4132 /* add fake paths if not present yet */
4133 static int add_fake_paths(struct hda_codec
*codec
, hda_nid_t nid
,
4134 int num_pins
, const hda_nid_t
*pins
)
4136 struct hda_gen_spec
*spec
= codec
->spec
;
4137 struct nid_path
*path
;
4140 for (i
= 0; i
< num_pins
; i
++) {
4143 if (get_nid_path(codec
, nid
, pins
[i
], 0))
4145 path
= snd_array_new(&spec
->paths
);
4148 memset(path
, 0, sizeof(*path
));
4150 path
->path
[0] = nid
;
4151 path
->path
[1] = pins
[i
];
4152 path
->active
= true;
4157 /* create fake paths to all outputs from beep */
4158 static int add_fake_beep_paths(struct hda_codec
*codec
)
4160 struct hda_gen_spec
*spec
= codec
->spec
;
4161 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4162 hda_nid_t nid
= spec
->beep_nid
;
4165 if (!codec
->power_save_node
|| !nid
)
4167 err
= add_fake_paths(codec
, nid
, cfg
->line_outs
, cfg
->line_out_pins
);
4170 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
) {
4171 err
= add_fake_paths(codec
, nid
, cfg
->hp_outs
, cfg
->hp_pins
);
4175 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
4176 err
= add_fake_paths(codec
, nid
, cfg
->speaker_outs
,
4184 /* power up/down beep widget and its output paths */
4185 static void beep_power_hook(struct hda_beep
*beep
, bool on
)
4187 set_path_power(beep
->codec
, beep
->nid
, -1, on
);
4191 * snd_hda_gen_fix_pin_power - Fix the power of the given pin widget to D0
4192 * @codec: the HDA codec
4193 * @pin: NID of pin to fix
4195 int snd_hda_gen_fix_pin_power(struct hda_codec
*codec
, hda_nid_t pin
)
4197 struct hda_gen_spec
*spec
= codec
->spec
;
4198 struct nid_path
*path
;
4200 path
= snd_array_new(&spec
->paths
);
4203 memset(path
, 0, sizeof(*path
));
4205 path
->path
[0] = pin
;
4206 path
->active
= true;
4207 path
->pin_fixed
= true;
4208 path
->stream_enabled
= true;
4211 EXPORT_SYMBOL_GPL(snd_hda_gen_fix_pin_power
);
4214 * Jack detections for HP auto-mute and mic-switch
4217 /* check each pin in the given array; returns true if any of them is plugged */
4218 static bool detect_jacks(struct hda_codec
*codec
, int num_pins
, hda_nid_t
*pins
)
4221 bool present
= false;
4223 for (i
= 0; i
< num_pins
; i
++) {
4224 hda_nid_t nid
= pins
[i
];
4227 /* don't detect pins retasked as inputs */
4228 if (snd_hda_codec_get_pin_target(codec
, nid
) & AC_PINCTL_IN_EN
)
4230 if (snd_hda_jack_detect_state(codec
, nid
) == HDA_JACK_PRESENT
)
4236 /* standard HP/line-out auto-mute helper */
4237 static void do_automute(struct hda_codec
*codec
, int num_pins
, hda_nid_t
*pins
,
4238 int *paths
, bool mute
)
4240 struct hda_gen_spec
*spec
= codec
->spec
;
4243 for (i
= 0; i
< num_pins
; i
++) {
4244 hda_nid_t nid
= pins
[i
];
4245 unsigned int val
, oldval
;
4249 oldval
= snd_hda_codec_get_pin_target(codec
, nid
);
4250 if (oldval
& PIN_IN
)
4251 continue; /* no mute for inputs */
4253 if (spec
->auto_mute_via_amp
) {
4254 struct nid_path
*path
;
4257 path
= snd_hda_get_path_from_idx(codec
, paths
[i
]);
4260 mute_nid
= get_amp_nid_(path
->ctls
[NID_PATH_MUTE_CTL
]);
4264 spec
->mute_bits
|= (1ULL << mute_nid
);
4266 spec
->mute_bits
&= ~(1ULL << mute_nid
);
4269 /* don't reset VREF value in case it's controlling
4270 * the amp (see alc861_fixup_asus_amp_vref_0f())
4272 if (spec
->keep_vref_in_automute
)
4273 val
= oldval
& ~PIN_HP
;
4278 /* here we call update_pin_ctl() so that the pinctl is
4279 * changed without changing the pinctl target value;
4280 * the original target value will be still referred at
4281 * the init / resume again
4283 update_pin_ctl(codec
, nid
, val
);
4286 set_pin_eapd(codec
, nid
, !mute
);
4287 if (codec
->power_save_node
) {
4290 on
= detect_pin_state(codec
, nid
);
4291 set_path_power(codec
, nid
, on
, -1);
4297 * snd_hda_gen_update_outputs - Toggle outputs muting
4298 * @codec: the HDA codec
4300 * Update the mute status of all outputs based on the current jack states.
4302 void snd_hda_gen_update_outputs(struct hda_codec
*codec
)
4304 struct hda_gen_spec
*spec
= codec
->spec
;
4308 /* Control HP pins/amps depending on master_mute state;
4309 * in general, HP pins/amps control should be enabled in all cases,
4310 * but currently set only for master_mute, just to be safe
4312 if (spec
->autocfg
.line_out_type
== AUTO_PIN_HP_OUT
)
4313 paths
= spec
->out_paths
;
4315 paths
= spec
->hp_paths
;
4316 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.hp_pins
),
4317 spec
->autocfg
.hp_pins
, paths
, spec
->master_mute
);
4319 if (!spec
->automute_speaker
)
4322 on
= spec
->hp_jack_present
| spec
->line_jack_present
;
4323 on
|= spec
->master_mute
;
4324 spec
->speaker_muted
= on
;
4325 if (spec
->autocfg
.line_out_type
== AUTO_PIN_SPEAKER_OUT
)
4326 paths
= spec
->out_paths
;
4328 paths
= spec
->speaker_paths
;
4329 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.speaker_pins
),
4330 spec
->autocfg
.speaker_pins
, paths
, on
);
4332 /* toggle line-out mutes if needed, too */
4333 /* if LO is a copy of either HP or Speaker, don't need to handle it */
4334 if (spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.hp_pins
[0] ||
4335 spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.speaker_pins
[0])
4337 if (!spec
->automute_lo
)
4340 on
= spec
->hp_jack_present
;
4341 on
|= spec
->master_mute
;
4342 spec
->line_out_muted
= on
;
4343 paths
= spec
->out_paths
;
4344 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.line_out_pins
),
4345 spec
->autocfg
.line_out_pins
, paths
, on
);
4347 EXPORT_SYMBOL_GPL(snd_hda_gen_update_outputs
);
4349 static void call_update_outputs(struct hda_codec
*codec
)
4351 struct hda_gen_spec
*spec
= codec
->spec
;
4352 if (spec
->automute_hook
)
4353 spec
->automute_hook(codec
);
4355 snd_hda_gen_update_outputs(codec
);
4357 /* sync the whole vmaster slaves to reflect the new auto-mute status */
4358 if (spec
->auto_mute_via_amp
&& !codec
->bus
->shutdown
)
4359 snd_ctl_sync_vmaster(spec
->vmaster_mute
.sw_kctl
, false);
4363 * snd_hda_gen_hp_automute - standard HP-automute helper
4364 * @codec: the HDA codec
4365 * @jack: jack object, NULL for the whole
4367 void snd_hda_gen_hp_automute(struct hda_codec
*codec
,
4368 struct hda_jack_callback
*jack
)
4370 struct hda_gen_spec
*spec
= codec
->spec
;
4371 hda_nid_t
*pins
= spec
->autocfg
.hp_pins
;
4372 int num_pins
= ARRAY_SIZE(spec
->autocfg
.hp_pins
);
4374 /* No detection for the first HP jack during indep-HP mode */
4375 if (spec
->indep_hp_enabled
) {
4380 spec
->hp_jack_present
= detect_jacks(codec
, num_pins
, pins
);
4381 if (!spec
->detect_hp
|| (!spec
->automute_speaker
&& !spec
->automute_lo
))
4383 call_update_outputs(codec
);
4385 EXPORT_SYMBOL_GPL(snd_hda_gen_hp_automute
);
4388 * snd_hda_gen_line_automute - standard line-out-automute helper
4389 * @codec: the HDA codec
4390 * @jack: jack object, NULL for the whole
4392 void snd_hda_gen_line_automute(struct hda_codec
*codec
,
4393 struct hda_jack_callback
*jack
)
4395 struct hda_gen_spec
*spec
= codec
->spec
;
4397 if (spec
->autocfg
.line_out_type
== AUTO_PIN_SPEAKER_OUT
)
4399 /* check LO jack only when it's different from HP */
4400 if (spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.hp_pins
[0])
4403 spec
->line_jack_present
=
4404 detect_jacks(codec
, ARRAY_SIZE(spec
->autocfg
.line_out_pins
),
4405 spec
->autocfg
.line_out_pins
);
4406 if (!spec
->automute_speaker
|| !spec
->detect_lo
)
4408 call_update_outputs(codec
);
4410 EXPORT_SYMBOL_GPL(snd_hda_gen_line_automute
);
4413 * snd_hda_gen_mic_autoswitch - standard mic auto-switch helper
4414 * @codec: the HDA codec
4415 * @jack: jack object, NULL for the whole
4417 void snd_hda_gen_mic_autoswitch(struct hda_codec
*codec
,
4418 struct hda_jack_callback
*jack
)
4420 struct hda_gen_spec
*spec
= codec
->spec
;
4423 if (!spec
->auto_mic
)
4426 for (i
= spec
->am_num_entries
- 1; i
> 0; i
--) {
4427 hda_nid_t pin
= spec
->am_entry
[i
].pin
;
4428 /* don't detect pins retasked as outputs */
4429 if (snd_hda_codec_get_pin_target(codec
, pin
) & AC_PINCTL_OUT_EN
)
4431 if (snd_hda_jack_detect_state(codec
, pin
) == HDA_JACK_PRESENT
) {
4432 mux_select(codec
, 0, spec
->am_entry
[i
].idx
);
4436 mux_select(codec
, 0, spec
->am_entry
[0].idx
);
4438 EXPORT_SYMBOL_GPL(snd_hda_gen_mic_autoswitch
);
4440 /* call appropriate hooks */
4441 static void call_hp_automute(struct hda_codec
*codec
,
4442 struct hda_jack_callback
*jack
)
4444 struct hda_gen_spec
*spec
= codec
->spec
;
4445 if (spec
->hp_automute_hook
)
4446 spec
->hp_automute_hook(codec
, jack
);
4448 snd_hda_gen_hp_automute(codec
, jack
);
4451 static void call_line_automute(struct hda_codec
*codec
,
4452 struct hda_jack_callback
*jack
)
4454 struct hda_gen_spec
*spec
= codec
->spec
;
4455 if (spec
->line_automute_hook
)
4456 spec
->line_automute_hook(codec
, jack
);
4458 snd_hda_gen_line_automute(codec
, jack
);
4461 static void call_mic_autoswitch(struct hda_codec
*codec
,
4462 struct hda_jack_callback
*jack
)
4464 struct hda_gen_spec
*spec
= codec
->spec
;
4465 if (spec
->mic_autoswitch_hook
)
4466 spec
->mic_autoswitch_hook(codec
, jack
);
4468 snd_hda_gen_mic_autoswitch(codec
, jack
);
4471 /* update jack retasking */
4472 static void update_automute_all(struct hda_codec
*codec
)
4474 call_hp_automute(codec
, NULL
);
4475 call_line_automute(codec
, NULL
);
4476 call_mic_autoswitch(codec
, NULL
);
4480 * Auto-Mute mode mixer enum support
4482 static int automute_mode_info(struct snd_kcontrol
*kcontrol
,
4483 struct snd_ctl_elem_info
*uinfo
)
4485 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
4486 struct hda_gen_spec
*spec
= codec
->spec
;
4487 static const char * const texts3
[] = {
4488 "Disabled", "Speaker Only", "Line Out+Speaker"
4491 if (spec
->automute_speaker_possible
&& spec
->automute_lo_possible
)
4492 return snd_hda_enum_helper_info(kcontrol
, uinfo
, 3, texts3
);
4493 return snd_hda_enum_bool_helper_info(kcontrol
, uinfo
);
4496 static int automute_mode_get(struct snd_kcontrol
*kcontrol
,
4497 struct snd_ctl_elem_value
*ucontrol
)
4499 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
4500 struct hda_gen_spec
*spec
= codec
->spec
;
4501 unsigned int val
= 0;
4502 if (spec
->automute_speaker
)
4504 if (spec
->automute_lo
)
4507 ucontrol
->value
.enumerated
.item
[0] = val
;
4511 static int automute_mode_put(struct snd_kcontrol
*kcontrol
,
4512 struct snd_ctl_elem_value
*ucontrol
)
4514 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
4515 struct hda_gen_spec
*spec
= codec
->spec
;
4517 switch (ucontrol
->value
.enumerated
.item
[0]) {
4519 if (!spec
->automute_speaker
&& !spec
->automute_lo
)
4521 spec
->automute_speaker
= 0;
4522 spec
->automute_lo
= 0;
4525 if (spec
->automute_speaker_possible
) {
4526 if (!spec
->automute_lo
&& spec
->automute_speaker
)
4528 spec
->automute_speaker
= 1;
4529 spec
->automute_lo
= 0;
4530 } else if (spec
->automute_lo_possible
) {
4531 if (spec
->automute_lo
)
4533 spec
->automute_lo
= 1;
4538 if (!spec
->automute_lo_possible
|| !spec
->automute_speaker_possible
)
4540 if (spec
->automute_speaker
&& spec
->automute_lo
)
4542 spec
->automute_speaker
= 1;
4543 spec
->automute_lo
= 1;
4548 call_update_outputs(codec
);
4552 static const struct snd_kcontrol_new automute_mode_enum
= {
4553 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
4554 .name
= "Auto-Mute Mode",
4555 .info
= automute_mode_info
,
4556 .get
= automute_mode_get
,
4557 .put
= automute_mode_put
,
4560 static int add_automute_mode_enum(struct hda_codec
*codec
)
4562 struct hda_gen_spec
*spec
= codec
->spec
;
4564 if (!snd_hda_gen_add_kctl(spec
, NULL
, &automute_mode_enum
))
4570 * Check the availability of HP/line-out auto-mute;
4571 * Set up appropriately if really supported
4573 static int check_auto_mute_availability(struct hda_codec
*codec
)
4575 struct hda_gen_spec
*spec
= codec
->spec
;
4576 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4580 if (spec
->suppress_auto_mute
)
4583 if (cfg
->hp_pins
[0])
4585 if (cfg
->line_out_pins
[0])
4587 if (cfg
->speaker_pins
[0])
4589 if (present
< 2) /* need two different output types */
4592 if (!cfg
->speaker_pins
[0] &&
4593 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
) {
4594 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
4595 sizeof(cfg
->speaker_pins
));
4596 cfg
->speaker_outs
= cfg
->line_outs
;
4599 if (!cfg
->hp_pins
[0] &&
4600 cfg
->line_out_type
== AUTO_PIN_HP_OUT
) {
4601 memcpy(cfg
->hp_pins
, cfg
->line_out_pins
,
4602 sizeof(cfg
->hp_pins
));
4603 cfg
->hp_outs
= cfg
->line_outs
;
4606 for (i
= 0; i
< cfg
->hp_outs
; i
++) {
4607 hda_nid_t nid
= cfg
->hp_pins
[i
];
4608 if (!is_jack_detectable(codec
, nid
))
4610 codec_dbg(codec
, "Enable HP auto-muting on NID 0x%x\n", nid
);
4611 snd_hda_jack_detect_enable_callback(codec
, nid
,
4613 spec
->detect_hp
= 1;
4616 if (cfg
->line_out_type
== AUTO_PIN_LINE_OUT
&& cfg
->line_outs
) {
4617 if (cfg
->speaker_outs
)
4618 for (i
= 0; i
< cfg
->line_outs
; i
++) {
4619 hda_nid_t nid
= cfg
->line_out_pins
[i
];
4620 if (!is_jack_detectable(codec
, nid
))
4622 codec_dbg(codec
, "Enable Line-Out auto-muting on NID 0x%x\n", nid
);
4623 snd_hda_jack_detect_enable_callback(codec
, nid
,
4624 call_line_automute
);
4625 spec
->detect_lo
= 1;
4627 spec
->automute_lo_possible
= spec
->detect_hp
;
4630 spec
->automute_speaker_possible
= cfg
->speaker_outs
&&
4631 (spec
->detect_hp
|| spec
->detect_lo
);
4633 spec
->automute_lo
= spec
->automute_lo_possible
;
4634 spec
->automute_speaker
= spec
->automute_speaker_possible
;
4636 if (spec
->automute_speaker_possible
|| spec
->automute_lo_possible
) {
4637 /* create a control for automute mode */
4638 err
= add_automute_mode_enum(codec
);
4645 /* check whether all auto-mic pins are valid; setup indices if OK */
4646 static bool auto_mic_check_imux(struct hda_codec
*codec
)
4648 struct hda_gen_spec
*spec
= codec
->spec
;
4649 const struct hda_input_mux
*imux
;
4652 imux
= &spec
->input_mux
;
4653 for (i
= 0; i
< spec
->am_num_entries
; i
++) {
4654 spec
->am_entry
[i
].idx
=
4655 find_idx_in_nid_list(spec
->am_entry
[i
].pin
,
4656 spec
->imux_pins
, imux
->num_items
);
4657 if (spec
->am_entry
[i
].idx
< 0)
4658 return false; /* no corresponding imux */
4661 /* we don't need the jack detection for the first pin */
4662 for (i
= 1; i
< spec
->am_num_entries
; i
++)
4663 snd_hda_jack_detect_enable_callback(codec
,
4664 spec
->am_entry
[i
].pin
,
4665 call_mic_autoswitch
);
4669 static int compare_attr(const void *ap
, const void *bp
)
4671 const struct automic_entry
*a
= ap
;
4672 const struct automic_entry
*b
= bp
;
4673 return (int)(a
->attr
- b
->attr
);
4677 * Check the availability of auto-mic switch;
4678 * Set up if really supported
4680 static int check_auto_mic_availability(struct hda_codec
*codec
)
4682 struct hda_gen_spec
*spec
= codec
->spec
;
4683 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4687 if (spec
->suppress_auto_mic
)
4692 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
4693 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
4695 attr
= snd_hda_codec_get_pincfg(codec
, nid
);
4696 attr
= snd_hda_get_input_pin_attr(attr
);
4697 if (types
& (1 << attr
))
4698 return 0; /* already occupied */
4700 case INPUT_PIN_ATTR_INT
:
4701 if (cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
4702 return 0; /* invalid type */
4704 case INPUT_PIN_ATTR_UNUSED
:
4705 return 0; /* invalid entry */
4707 if (cfg
->inputs
[i
].type
> AUTO_PIN_LINE_IN
)
4708 return 0; /* invalid type */
4709 if (!spec
->line_in_auto_switch
&&
4710 cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
4711 return 0; /* only mic is allowed */
4712 if (!is_jack_detectable(codec
, nid
))
4713 return 0; /* no unsol support */
4716 if (num_pins
>= MAX_AUTO_MIC_PINS
)
4718 types
|= (1 << attr
);
4719 spec
->am_entry
[num_pins
].pin
= nid
;
4720 spec
->am_entry
[num_pins
].attr
= attr
;
4727 spec
->am_num_entries
= num_pins
;
4728 /* sort the am_entry in the order of attr so that the pin with a
4729 * higher attr will be selected when the jack is plugged.
4731 sort(spec
->am_entry
, num_pins
, sizeof(spec
->am_entry
[0]),
4732 compare_attr
, NULL
);
4734 if (!auto_mic_check_imux(codec
))
4738 spec
->num_adc_nids
= 1;
4739 spec
->cur_mux
[0] = spec
->am_entry
[0].idx
;
4740 codec_dbg(codec
, "Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
4741 spec
->am_entry
[0].pin
,
4742 spec
->am_entry
[1].pin
,
4743 spec
->am_entry
[2].pin
);
4749 * snd_hda_gen_path_power_filter - power_filter hook to make inactive widgets
4751 * @codec: the HDA codec
4752 * @nid: NID to evalute
4753 * @power_state: target power state
4755 unsigned int snd_hda_gen_path_power_filter(struct hda_codec
*codec
,
4757 unsigned int power_state
)
4759 struct hda_gen_spec
*spec
= codec
->spec
;
4761 if (!spec
->power_down_unused
&& !codec
->power_save_node
)
4763 if (power_state
!= AC_PWRST_D0
|| nid
== codec
->core
.afg
)
4765 if (get_wcaps_type(get_wcaps(codec
, nid
)) >= AC_WID_POWER
)
4767 if (is_active_nid_for_any(codec
, nid
))
4771 EXPORT_SYMBOL_GPL(snd_hda_gen_path_power_filter
);
4773 /* mute all aamix inputs initially; parse up to the first leaves */
4774 static void mute_all_mixer_nid(struct hda_codec
*codec
, hda_nid_t mix
)
4777 const hda_nid_t
*conn
;
4780 nums
= snd_hda_get_conn_list(codec
, mix
, &conn
);
4781 has_amp
= nid_has_mute(codec
, mix
, HDA_INPUT
);
4782 for (i
= 0; i
< nums
; i
++) {
4784 update_amp(codec
, mix
, HDA_INPUT
, i
,
4785 0xff, HDA_AMP_MUTE
);
4786 else if (nid_has_volume(codec
, conn
[i
], HDA_OUTPUT
))
4787 update_amp(codec
, conn
[i
], HDA_OUTPUT
, 0,
4788 0xff, HDA_AMP_MUTE
);
4793 * snd_hda_gen_stream_pm - Stream power management callback
4794 * @codec: the HDA codec
4795 * @nid: audio widget
4796 * @on: power on/off flag
4798 * Set this in patch_ops.stream_pm. Only valid with power_save_node flag.
4800 void snd_hda_gen_stream_pm(struct hda_codec
*codec
, hda_nid_t nid
, bool on
)
4802 if (codec
->power_save_node
)
4803 set_path_power(codec
, nid
, -1, on
);
4805 EXPORT_SYMBOL_GPL(snd_hda_gen_stream_pm
);
4808 * snd_hda_gen_parse_auto_config - Parse the given BIOS configuration and
4809 * set up the hda_gen_spec
4810 * @codec: the HDA codec
4811 * @cfg: Parsed pin configuration
4813 * return 1 if successful, 0 if the proper config is not found,
4814 * or a negative error code
4816 int snd_hda_gen_parse_auto_config(struct hda_codec
*codec
,
4817 struct auto_pin_cfg
*cfg
)
4819 struct hda_gen_spec
*spec
= codec
->spec
;
4822 parse_user_hints(codec
);
4824 if (spec
->mixer_nid
&& !spec
->mixer_merge_nid
)
4825 spec
->mixer_merge_nid
= spec
->mixer_nid
;
4827 if (cfg
!= &spec
->autocfg
) {
4828 spec
->autocfg
= *cfg
;
4829 cfg
= &spec
->autocfg
;
4832 if (!spec
->main_out_badness
)
4833 spec
->main_out_badness
= &hda_main_out_badness
;
4834 if (!spec
->extra_out_badness
)
4835 spec
->extra_out_badness
= &hda_extra_out_badness
;
4837 fill_all_dac_nids(codec
);
4839 if (!cfg
->line_outs
) {
4840 if (cfg
->dig_outs
|| cfg
->dig_in_pin
) {
4841 spec
->multiout
.max_channels
= 2;
4842 spec
->no_analog
= 1;
4845 if (!cfg
->num_inputs
&& !cfg
->dig_in_pin
)
4846 return 0; /* can't find valid BIOS pin config */
4849 if (!spec
->no_primary_hp
&&
4850 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
&&
4851 cfg
->line_outs
<= cfg
->hp_outs
) {
4852 /* use HP as primary out */
4853 cfg
->speaker_outs
= cfg
->line_outs
;
4854 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
4855 sizeof(cfg
->speaker_pins
));
4856 cfg
->line_outs
= cfg
->hp_outs
;
4857 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
, sizeof(cfg
->hp_pins
));
4859 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
4860 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
4863 err
= parse_output_paths(codec
);
4866 err
= create_multi_channel_mode(codec
);
4869 err
= create_multi_out_ctls(codec
, cfg
);
4872 err
= create_hp_out_ctls(codec
);
4875 err
= create_speaker_out_ctls(codec
);
4878 err
= create_indep_hp_ctls(codec
);
4881 err
= create_loopback_mixing_ctl(codec
);
4884 err
= create_hp_mic(codec
);
4887 err
= create_input_ctls(codec
);
4891 /* add power-down pin callbacks at first */
4892 add_all_pin_power_ctls(codec
, false);
4894 spec
->const_channel_count
= spec
->ext_channel_count
;
4895 /* check the multiple speaker and headphone pins */
4896 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
4897 spec
->const_channel_count
= max(spec
->const_channel_count
,
4898 cfg
->speaker_outs
* 2);
4899 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
4900 spec
->const_channel_count
= max(spec
->const_channel_count
,
4902 spec
->multiout
.max_channels
= max(spec
->ext_channel_count
,
4903 spec
->const_channel_count
);
4905 err
= check_auto_mute_availability(codec
);
4909 err
= check_dyn_adc_switch(codec
);
4913 err
= check_auto_mic_availability(codec
);
4917 /* add stereo mix if available and not enabled yet */
4918 if (!spec
->auto_mic
&& spec
->mixer_nid
&&
4919 spec
->add_stereo_mix_input
== HDA_HINT_STEREO_MIX_AUTO
&&
4920 spec
->input_mux
.num_items
> 1) {
4921 err
= parse_capture_source(codec
, spec
->mixer_nid
,
4922 CFG_IDX_MIX
, spec
->num_all_adcs
,
4929 err
= create_capture_mixers(codec
);
4933 err
= parse_mic_boost(codec
);
4937 /* create "Headphone Mic Jack Mode" if no input selection is
4938 * available (or user specifies add_jack_modes hint)
4940 if (spec
->hp_mic_pin
&&
4941 (spec
->auto_mic
|| spec
->input_mux
.num_items
== 1 ||
4942 spec
->add_jack_modes
)) {
4943 err
= create_hp_mic_jack_mode(codec
, spec
->hp_mic_pin
);
4948 if (spec
->add_jack_modes
) {
4949 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
4950 err
= create_out_jack_modes(codec
, cfg
->line_outs
,
4951 cfg
->line_out_pins
);
4955 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
) {
4956 err
= create_out_jack_modes(codec
, cfg
->hp_outs
,
4963 /* add power-up pin callbacks at last */
4964 add_all_pin_power_ctls(codec
, true);
4966 /* mute all aamix input initially */
4967 if (spec
->mixer_nid
)
4968 mute_all_mixer_nid(codec
, spec
->mixer_nid
);
4971 parse_digital(codec
);
4973 if (spec
->power_down_unused
|| codec
->power_save_node
) {
4974 if (!codec
->power_filter
)
4975 codec
->power_filter
= snd_hda_gen_path_power_filter
;
4976 if (!codec
->patch_ops
.stream_pm
)
4977 codec
->patch_ops
.stream_pm
= snd_hda_gen_stream_pm
;
4980 if (!spec
->no_analog
&& spec
->beep_nid
) {
4981 err
= snd_hda_attach_beep_device(codec
, spec
->beep_nid
);
4984 if (codec
->beep
&& codec
->power_save_node
) {
4985 err
= add_fake_beep_paths(codec
);
4988 codec
->beep
->power_hook
= beep_power_hook
;
4994 EXPORT_SYMBOL_GPL(snd_hda_gen_parse_auto_config
);
4998 * Build control elements
5001 /* slave controls for virtual master */
5002 static const char * const slave_pfxs
[] = {
5003 "Front", "Surround", "Center", "LFE", "Side",
5004 "Headphone", "Speaker", "Mono", "Line Out",
5005 "CLFE", "Bass Speaker", "PCM",
5006 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
5007 "Headphone Front", "Headphone Surround", "Headphone CLFE",
5008 "Headphone Side", "Headphone+LO", "Speaker+LO",
5013 * snd_hda_gen_build_controls - Build controls from the parsed results
5014 * @codec: the HDA codec
5016 * Pass this to build_controls patch_ops.
5018 int snd_hda_gen_build_controls(struct hda_codec
*codec
)
5020 struct hda_gen_spec
*spec
= codec
->spec
;
5023 if (spec
->kctls
.used
) {
5024 err
= snd_hda_add_new_ctls(codec
, spec
->kctls
.list
);
5029 if (spec
->multiout
.dig_out_nid
) {
5030 err
= snd_hda_create_dig_out_ctls(codec
,
5031 spec
->multiout
.dig_out_nid
,
5032 spec
->multiout
.dig_out_nid
,
5033 spec
->pcm_rec
[1]->pcm_type
);
5036 if (!spec
->no_analog
) {
5037 err
= snd_hda_create_spdif_share_sw(codec
,
5041 spec
->multiout
.share_spdif
= 1;
5044 if (spec
->dig_in_nid
) {
5045 err
= snd_hda_create_spdif_in_ctls(codec
, spec
->dig_in_nid
);
5050 /* if we have no master control, let's create it */
5051 if (!spec
->no_analog
&&
5052 !snd_hda_find_mixer_ctl(codec
, "Master Playback Volume")) {
5053 err
= snd_hda_add_vmaster(codec
, "Master Playback Volume",
5054 spec
->vmaster_tlv
, slave_pfxs
,
5059 if (!spec
->no_analog
&&
5060 !snd_hda_find_mixer_ctl(codec
, "Master Playback Switch")) {
5061 err
= __snd_hda_add_vmaster(codec
, "Master Playback Switch",
5064 true, &spec
->vmaster_mute
.sw_kctl
);
5067 if (spec
->vmaster_mute
.hook
) {
5068 snd_hda_add_vmaster_hook(codec
, &spec
->vmaster_mute
,
5069 spec
->vmaster_mute_enum
);
5070 snd_hda_sync_vmaster_hook(&spec
->vmaster_mute
);
5074 free_kctls(spec
); /* no longer needed */
5076 err
= snd_hda_jack_add_kctls(codec
, &spec
->autocfg
);
5082 EXPORT_SYMBOL_GPL(snd_hda_gen_build_controls
);
5089 static void call_pcm_playback_hook(struct hda_pcm_stream
*hinfo
,
5090 struct hda_codec
*codec
,
5091 struct snd_pcm_substream
*substream
,
5094 struct hda_gen_spec
*spec
= codec
->spec
;
5095 if (spec
->pcm_playback_hook
)
5096 spec
->pcm_playback_hook(hinfo
, codec
, substream
, action
);
5099 static void call_pcm_capture_hook(struct hda_pcm_stream
*hinfo
,
5100 struct hda_codec
*codec
,
5101 struct snd_pcm_substream
*substream
,
5104 struct hda_gen_spec
*spec
= codec
->spec
;
5105 if (spec
->pcm_capture_hook
)
5106 spec
->pcm_capture_hook(hinfo
, codec
, substream
, action
);
5110 * Analog playback callbacks
5112 static int playback_pcm_open(struct hda_pcm_stream
*hinfo
,
5113 struct hda_codec
*codec
,
5114 struct snd_pcm_substream
*substream
)
5116 struct hda_gen_spec
*spec
= codec
->spec
;
5119 mutex_lock(&spec
->pcm_mutex
);
5120 err
= snd_hda_multi_out_analog_open(codec
,
5121 &spec
->multiout
, substream
,
5124 spec
->active_streams
|= 1 << STREAM_MULTI_OUT
;
5125 call_pcm_playback_hook(hinfo
, codec
, substream
,
5126 HDA_GEN_PCM_ACT_OPEN
);
5128 mutex_unlock(&spec
->pcm_mutex
);
5132 static int playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5133 struct hda_codec
*codec
,
5134 unsigned int stream_tag
,
5135 unsigned int format
,
5136 struct snd_pcm_substream
*substream
)
5138 struct hda_gen_spec
*spec
= codec
->spec
;
5141 err
= snd_hda_multi_out_analog_prepare(codec
, &spec
->multiout
,
5142 stream_tag
, format
, substream
);
5144 call_pcm_playback_hook(hinfo
, codec
, substream
,
5145 HDA_GEN_PCM_ACT_PREPARE
);
5149 static int playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5150 struct hda_codec
*codec
,
5151 struct snd_pcm_substream
*substream
)
5153 struct hda_gen_spec
*spec
= codec
->spec
;
5156 err
= snd_hda_multi_out_analog_cleanup(codec
, &spec
->multiout
);
5158 call_pcm_playback_hook(hinfo
, codec
, substream
,
5159 HDA_GEN_PCM_ACT_CLEANUP
);
5163 static int playback_pcm_close(struct hda_pcm_stream
*hinfo
,
5164 struct hda_codec
*codec
,
5165 struct snd_pcm_substream
*substream
)
5167 struct hda_gen_spec
*spec
= codec
->spec
;
5168 mutex_lock(&spec
->pcm_mutex
);
5169 spec
->active_streams
&= ~(1 << STREAM_MULTI_OUT
);
5170 call_pcm_playback_hook(hinfo
, codec
, substream
,
5171 HDA_GEN_PCM_ACT_CLOSE
);
5172 mutex_unlock(&spec
->pcm_mutex
);
5176 static int capture_pcm_open(struct hda_pcm_stream
*hinfo
,
5177 struct hda_codec
*codec
,
5178 struct snd_pcm_substream
*substream
)
5180 call_pcm_capture_hook(hinfo
, codec
, substream
, HDA_GEN_PCM_ACT_OPEN
);
5184 static int capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5185 struct hda_codec
*codec
,
5186 unsigned int stream_tag
,
5187 unsigned int format
,
5188 struct snd_pcm_substream
*substream
)
5190 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
5191 call_pcm_capture_hook(hinfo
, codec
, substream
,
5192 HDA_GEN_PCM_ACT_PREPARE
);
5196 static int capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5197 struct hda_codec
*codec
,
5198 struct snd_pcm_substream
*substream
)
5200 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
5201 call_pcm_capture_hook(hinfo
, codec
, substream
,
5202 HDA_GEN_PCM_ACT_CLEANUP
);
5206 static int capture_pcm_close(struct hda_pcm_stream
*hinfo
,
5207 struct hda_codec
*codec
,
5208 struct snd_pcm_substream
*substream
)
5210 call_pcm_capture_hook(hinfo
, codec
, substream
, HDA_GEN_PCM_ACT_CLOSE
);
5214 static int alt_playback_pcm_open(struct hda_pcm_stream
*hinfo
,
5215 struct hda_codec
*codec
,
5216 struct snd_pcm_substream
*substream
)
5218 struct hda_gen_spec
*spec
= codec
->spec
;
5221 mutex_lock(&spec
->pcm_mutex
);
5222 if (spec
->indep_hp
&& !spec
->indep_hp_enabled
)
5225 spec
->active_streams
|= 1 << STREAM_INDEP_HP
;
5226 call_pcm_playback_hook(hinfo
, codec
, substream
,
5227 HDA_GEN_PCM_ACT_OPEN
);
5228 mutex_unlock(&spec
->pcm_mutex
);
5232 static int alt_playback_pcm_close(struct hda_pcm_stream
*hinfo
,
5233 struct hda_codec
*codec
,
5234 struct snd_pcm_substream
*substream
)
5236 struct hda_gen_spec
*spec
= codec
->spec
;
5237 mutex_lock(&spec
->pcm_mutex
);
5238 spec
->active_streams
&= ~(1 << STREAM_INDEP_HP
);
5239 call_pcm_playback_hook(hinfo
, codec
, substream
,
5240 HDA_GEN_PCM_ACT_CLOSE
);
5241 mutex_unlock(&spec
->pcm_mutex
);
5245 static int alt_playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5246 struct hda_codec
*codec
,
5247 unsigned int stream_tag
,
5248 unsigned int format
,
5249 struct snd_pcm_substream
*substream
)
5251 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
5252 call_pcm_playback_hook(hinfo
, codec
, substream
,
5253 HDA_GEN_PCM_ACT_PREPARE
);
5257 static int alt_playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5258 struct hda_codec
*codec
,
5259 struct snd_pcm_substream
*substream
)
5261 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
5262 call_pcm_playback_hook(hinfo
, codec
, substream
,
5263 HDA_GEN_PCM_ACT_CLEANUP
);
5270 static int dig_playback_pcm_open(struct hda_pcm_stream
*hinfo
,
5271 struct hda_codec
*codec
,
5272 struct snd_pcm_substream
*substream
)
5274 struct hda_gen_spec
*spec
= codec
->spec
;
5275 return snd_hda_multi_out_dig_open(codec
, &spec
->multiout
);
5278 static int dig_playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5279 struct hda_codec
*codec
,
5280 unsigned int stream_tag
,
5281 unsigned int format
,
5282 struct snd_pcm_substream
*substream
)
5284 struct hda_gen_spec
*spec
= codec
->spec
;
5285 return snd_hda_multi_out_dig_prepare(codec
, &spec
->multiout
,
5286 stream_tag
, format
, substream
);
5289 static int dig_playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5290 struct hda_codec
*codec
,
5291 struct snd_pcm_substream
*substream
)
5293 struct hda_gen_spec
*spec
= codec
->spec
;
5294 return snd_hda_multi_out_dig_cleanup(codec
, &spec
->multiout
);
5297 static int dig_playback_pcm_close(struct hda_pcm_stream
*hinfo
,
5298 struct hda_codec
*codec
,
5299 struct snd_pcm_substream
*substream
)
5301 struct hda_gen_spec
*spec
= codec
->spec
;
5302 return snd_hda_multi_out_dig_close(codec
, &spec
->multiout
);
5308 #define alt_capture_pcm_open capture_pcm_open
5309 #define alt_capture_pcm_close capture_pcm_close
5311 static int alt_capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5312 struct hda_codec
*codec
,
5313 unsigned int stream_tag
,
5314 unsigned int format
,
5315 struct snd_pcm_substream
*substream
)
5317 struct hda_gen_spec
*spec
= codec
->spec
;
5319 snd_hda_codec_setup_stream(codec
, spec
->adc_nids
[substream
->number
+ 1],
5320 stream_tag
, 0, format
);
5321 call_pcm_capture_hook(hinfo
, codec
, substream
,
5322 HDA_GEN_PCM_ACT_PREPARE
);
5326 static int alt_capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5327 struct hda_codec
*codec
,
5328 struct snd_pcm_substream
*substream
)
5330 struct hda_gen_spec
*spec
= codec
->spec
;
5332 snd_hda_codec_cleanup_stream(codec
,
5333 spec
->adc_nids
[substream
->number
+ 1]);
5334 call_pcm_capture_hook(hinfo
, codec
, substream
,
5335 HDA_GEN_PCM_ACT_CLEANUP
);
5341 static const struct hda_pcm_stream pcm_analog_playback
= {
5345 /* NID is set in build_pcms */
5347 .open
= playback_pcm_open
,
5348 .close
= playback_pcm_close
,
5349 .prepare
= playback_pcm_prepare
,
5350 .cleanup
= playback_pcm_cleanup
5354 static const struct hda_pcm_stream pcm_analog_capture
= {
5358 /* NID is set in build_pcms */
5360 .open
= capture_pcm_open
,
5361 .close
= capture_pcm_close
,
5362 .prepare
= capture_pcm_prepare
,
5363 .cleanup
= capture_pcm_cleanup
5367 static const struct hda_pcm_stream pcm_analog_alt_playback
= {
5371 /* NID is set in build_pcms */
5373 .open
= alt_playback_pcm_open
,
5374 .close
= alt_playback_pcm_close
,
5375 .prepare
= alt_playback_pcm_prepare
,
5376 .cleanup
= alt_playback_pcm_cleanup
5380 static const struct hda_pcm_stream pcm_analog_alt_capture
= {
5381 .substreams
= 2, /* can be overridden */
5384 /* NID is set in build_pcms */
5386 .open
= alt_capture_pcm_open
,
5387 .close
= alt_capture_pcm_close
,
5388 .prepare
= alt_capture_pcm_prepare
,
5389 .cleanup
= alt_capture_pcm_cleanup
5393 static const struct hda_pcm_stream pcm_digital_playback
= {
5397 /* NID is set in build_pcms */
5399 .open
= dig_playback_pcm_open
,
5400 .close
= dig_playback_pcm_close
,
5401 .prepare
= dig_playback_pcm_prepare
,
5402 .cleanup
= dig_playback_pcm_cleanup
5406 static const struct hda_pcm_stream pcm_digital_capture
= {
5410 /* NID is set in build_pcms */
5413 /* Used by build_pcms to flag that a PCM has no playback stream */
5414 static const struct hda_pcm_stream pcm_null_stream
= {
5421 * dynamic changing ADC PCM streams
5423 static bool dyn_adc_pcm_resetup(struct hda_codec
*codec
, int cur
)
5425 struct hda_gen_spec
*spec
= codec
->spec
;
5426 hda_nid_t new_adc
= spec
->adc_nids
[spec
->dyn_adc_idx
[cur
]];
5428 if (spec
->cur_adc
&& spec
->cur_adc
!= new_adc
) {
5429 /* stream is running, let's swap the current ADC */
5430 __snd_hda_codec_cleanup_stream(codec
, spec
->cur_adc
, 1);
5431 spec
->cur_adc
= new_adc
;
5432 snd_hda_codec_setup_stream(codec
, new_adc
,
5433 spec
->cur_adc_stream_tag
, 0,
5434 spec
->cur_adc_format
);
5440 /* analog capture with dynamic dual-adc changes */
5441 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
5442 struct hda_codec
*codec
,
5443 unsigned int stream_tag
,
5444 unsigned int format
,
5445 struct snd_pcm_substream
*substream
)
5447 struct hda_gen_spec
*spec
= codec
->spec
;
5448 spec
->cur_adc
= spec
->adc_nids
[spec
->dyn_adc_idx
[spec
->cur_mux
[0]]];
5449 spec
->cur_adc_stream_tag
= stream_tag
;
5450 spec
->cur_adc_format
= format
;
5451 snd_hda_codec_setup_stream(codec
, spec
->cur_adc
, stream_tag
, 0, format
);
5455 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
5456 struct hda_codec
*codec
,
5457 struct snd_pcm_substream
*substream
)
5459 struct hda_gen_spec
*spec
= codec
->spec
;
5460 snd_hda_codec_cleanup_stream(codec
, spec
->cur_adc
);
5465 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture
= {
5469 .nid
= 0, /* fill later */
5471 .prepare
= dyn_adc_capture_pcm_prepare
,
5472 .cleanup
= dyn_adc_capture_pcm_cleanup
5476 static void fill_pcm_stream_name(char *str
, size_t len
, const char *sfx
,
5477 const char *chip_name
)
5483 strlcpy(str
, chip_name
, len
);
5485 /* drop non-alnum chars after a space */
5486 for (p
= strchr(str
, ' '); p
; p
= strchr(p
+ 1, ' ')) {
5487 if (!isalnum(p
[1])) {
5492 strlcat(str
, sfx
, len
);
5495 /* copy PCM stream info from @default_str, and override non-NULL entries
5496 * from @spec_str and @nid
5498 static void setup_pcm_stream(struct hda_pcm_stream
*str
,
5499 const struct hda_pcm_stream
*default_str
,
5500 const struct hda_pcm_stream
*spec_str
,
5503 *str
= *default_str
;
5507 if (spec_str
->substreams
)
5508 str
->substreams
= spec_str
->substreams
;
5509 if (spec_str
->channels_min
)
5510 str
->channels_min
= spec_str
->channels_min
;
5511 if (spec_str
->channels_max
)
5512 str
->channels_max
= spec_str
->channels_max
;
5513 if (spec_str
->rates
)
5514 str
->rates
= spec_str
->rates
;
5515 if (spec_str
->formats
)
5516 str
->formats
= spec_str
->formats
;
5517 if (spec_str
->maxbps
)
5518 str
->maxbps
= spec_str
->maxbps
;
5523 * snd_hda_gen_build_pcms - build PCM streams based on the parsed results
5524 * @codec: the HDA codec
5526 * Pass this to build_pcms patch_ops.
5528 int snd_hda_gen_build_pcms(struct hda_codec
*codec
)
5530 struct hda_gen_spec
*spec
= codec
->spec
;
5531 struct hda_pcm
*info
;
5532 bool have_multi_adcs
;
5534 if (spec
->no_analog
)
5537 fill_pcm_stream_name(spec
->stream_name_analog
,
5538 sizeof(spec
->stream_name_analog
),
5539 " Analog", codec
->core
.chip_name
);
5540 info
= snd_hda_codec_pcm_new(codec
, "%s", spec
->stream_name_analog
);
5543 spec
->pcm_rec
[0] = info
;
5545 if (spec
->multiout
.num_dacs
> 0) {
5546 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
],
5547 &pcm_analog_playback
,
5548 spec
->stream_analog_playback
,
5549 spec
->multiout
.dac_nids
[0]);
5550 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].channels_max
=
5551 spec
->multiout
.max_channels
;
5552 if (spec
->autocfg
.line_out_type
== AUTO_PIN_SPEAKER_OUT
&&
5553 spec
->autocfg
.line_outs
== 2)
5554 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].chmap
=
5557 if (spec
->num_adc_nids
) {
5558 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_CAPTURE
],
5559 (spec
->dyn_adc_switch
?
5560 &dyn_adc_pcm_analog_capture
: &pcm_analog_capture
),
5561 spec
->stream_analog_capture
,
5566 /* SPDIF for stream index #1 */
5567 if (spec
->multiout
.dig_out_nid
|| spec
->dig_in_nid
) {
5568 fill_pcm_stream_name(spec
->stream_name_digital
,
5569 sizeof(spec
->stream_name_digital
),
5570 " Digital", codec
->core
.chip_name
);
5571 info
= snd_hda_codec_pcm_new(codec
, "%s",
5572 spec
->stream_name_digital
);
5575 codec
->slave_dig_outs
= spec
->multiout
.slave_dig_outs
;
5576 spec
->pcm_rec
[1] = info
;
5577 if (spec
->dig_out_type
)
5578 info
->pcm_type
= spec
->dig_out_type
;
5580 info
->pcm_type
= HDA_PCM_TYPE_SPDIF
;
5581 if (spec
->multiout
.dig_out_nid
)
5582 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
],
5583 &pcm_digital_playback
,
5584 spec
->stream_digital_playback
,
5585 spec
->multiout
.dig_out_nid
);
5586 if (spec
->dig_in_nid
)
5587 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_CAPTURE
],
5588 &pcm_digital_capture
,
5589 spec
->stream_digital_capture
,
5593 if (spec
->no_analog
)
5596 /* If the use of more than one ADC is requested for the current
5597 * model, configure a second analog capture-only PCM.
5599 have_multi_adcs
= (spec
->num_adc_nids
> 1) &&
5600 !spec
->dyn_adc_switch
&& !spec
->auto_mic
;
5601 /* Additional Analaog capture for index #2 */
5602 if (spec
->alt_dac_nid
|| have_multi_adcs
) {
5603 fill_pcm_stream_name(spec
->stream_name_alt_analog
,
5604 sizeof(spec
->stream_name_alt_analog
),
5605 " Alt Analog", codec
->core
.chip_name
);
5606 info
= snd_hda_codec_pcm_new(codec
, "%s",
5607 spec
->stream_name_alt_analog
);
5610 spec
->pcm_rec
[2] = info
;
5611 if (spec
->alt_dac_nid
)
5612 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
],
5613 &pcm_analog_alt_playback
,
5614 spec
->stream_analog_alt_playback
,
5617 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
],
5618 &pcm_null_stream
, NULL
, 0);
5619 if (have_multi_adcs
) {
5620 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_CAPTURE
],
5621 &pcm_analog_alt_capture
,
5622 spec
->stream_analog_alt_capture
,
5624 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].substreams
=
5625 spec
->num_adc_nids
- 1;
5627 setup_pcm_stream(&info
->stream
[SNDRV_PCM_STREAM_CAPTURE
],
5628 &pcm_null_stream
, NULL
, 0);
5634 EXPORT_SYMBOL_GPL(snd_hda_gen_build_pcms
);
5638 * Standard auto-parser initializations
5641 /* configure the given path as a proper output */
5642 static void set_output_and_unmute(struct hda_codec
*codec
, int path_idx
)
5644 struct nid_path
*path
;
5647 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
5648 if (!path
|| !path
->depth
)
5650 pin
= path
->path
[path
->depth
- 1];
5651 restore_pin_ctl(codec
, pin
);
5652 snd_hda_activate_path(codec
, path
, path
->active
,
5653 aamix_default(codec
->spec
));
5654 set_pin_eapd(codec
, pin
, path
->active
);
5657 /* initialize primary output paths */
5658 static void init_multi_out(struct hda_codec
*codec
)
5660 struct hda_gen_spec
*spec
= codec
->spec
;
5663 for (i
= 0; i
< spec
->autocfg
.line_outs
; i
++)
5664 set_output_and_unmute(codec
, spec
->out_paths
[i
]);
5668 static void __init_extra_out(struct hda_codec
*codec
, int num_outs
, int *paths
)
5672 for (i
= 0; i
< num_outs
; i
++)
5673 set_output_and_unmute(codec
, paths
[i
]);
5676 /* initialize hp and speaker paths */
5677 static void init_extra_out(struct hda_codec
*codec
)
5679 struct hda_gen_spec
*spec
= codec
->spec
;
5681 if (spec
->autocfg
.line_out_type
!= AUTO_PIN_HP_OUT
)
5682 __init_extra_out(codec
, spec
->autocfg
.hp_outs
, spec
->hp_paths
);
5683 if (spec
->autocfg
.line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
5684 __init_extra_out(codec
, spec
->autocfg
.speaker_outs
,
5685 spec
->speaker_paths
);
5688 /* initialize multi-io paths */
5689 static void init_multi_io(struct hda_codec
*codec
)
5691 struct hda_gen_spec
*spec
= codec
->spec
;
5694 for (i
= 0; i
< spec
->multi_ios
; i
++) {
5695 hda_nid_t pin
= spec
->multi_io
[i
].pin
;
5696 struct nid_path
*path
;
5697 path
= get_multiio_path(codec
, i
);
5700 if (!spec
->multi_io
[i
].ctl_in
)
5701 spec
->multi_io
[i
].ctl_in
=
5702 snd_hda_codec_get_pin_target(codec
, pin
);
5703 snd_hda_activate_path(codec
, path
, path
->active
,
5704 aamix_default(spec
));
5708 static void init_aamix_paths(struct hda_codec
*codec
)
5710 struct hda_gen_spec
*spec
= codec
->spec
;
5712 if (!spec
->have_aamix_ctl
)
5714 if (!has_aamix_out_paths(spec
))
5716 update_aamix_paths(codec
, spec
->aamix_mode
, spec
->out_paths
[0],
5717 spec
->aamix_out_paths
[0],
5718 spec
->autocfg
.line_out_type
);
5719 update_aamix_paths(codec
, spec
->aamix_mode
, spec
->hp_paths
[0],
5720 spec
->aamix_out_paths
[1],
5722 update_aamix_paths(codec
, spec
->aamix_mode
, spec
->speaker_paths
[0],
5723 spec
->aamix_out_paths
[2],
5724 AUTO_PIN_SPEAKER_OUT
);
5727 /* set up input pins and loopback paths */
5728 static void init_analog_input(struct hda_codec
*codec
)
5730 struct hda_gen_spec
*spec
= codec
->spec
;
5731 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
5734 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
5735 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
5736 if (is_input_pin(codec
, nid
))
5737 restore_pin_ctl(codec
, nid
);
5739 /* init loopback inputs */
5740 if (spec
->mixer_nid
) {
5741 resume_path_from_idx(codec
, spec
->loopback_paths
[i
]);
5742 resume_path_from_idx(codec
, spec
->loopback_merge_path
);
5747 /* initialize ADC paths */
5748 static void init_input_src(struct hda_codec
*codec
)
5750 struct hda_gen_spec
*spec
= codec
->spec
;
5751 struct hda_input_mux
*imux
= &spec
->input_mux
;
5752 struct nid_path
*path
;
5755 if (spec
->dyn_adc_switch
)
5758 nums
= spec
->num_adc_nids
;
5760 for (c
= 0; c
< nums
; c
++) {
5761 for (i
= 0; i
< imux
->num_items
; i
++) {
5762 path
= get_input_path(codec
, c
, i
);
5764 bool active
= path
->active
;
5765 if (i
== spec
->cur_mux
[c
])
5767 snd_hda_activate_path(codec
, path
, active
, false);
5771 update_hp_mic(codec
, c
, true);
5774 if (spec
->cap_sync_hook
)
5775 spec
->cap_sync_hook(codec
, NULL
, NULL
);
5778 /* set right pin controls for digital I/O */
5779 static void init_digital(struct hda_codec
*codec
)
5781 struct hda_gen_spec
*spec
= codec
->spec
;
5785 for (i
= 0; i
< spec
->autocfg
.dig_outs
; i
++)
5786 set_output_and_unmute(codec
, spec
->digout_paths
[i
]);
5787 pin
= spec
->autocfg
.dig_in_pin
;
5789 restore_pin_ctl(codec
, pin
);
5790 resume_path_from_idx(codec
, spec
->digin_path
);
5794 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
5795 * invalid unsol tags by some reason
5797 static void clear_unsol_on_unused_pins(struct hda_codec
*codec
)
5801 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
5802 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
5803 hda_nid_t nid
= pin
->nid
;
5804 if (is_jack_detectable(codec
, nid
) &&
5805 !snd_hda_jack_tbl_get(codec
, nid
))
5806 snd_hda_codec_update_cache(codec
, nid
, 0,
5807 AC_VERB_SET_UNSOLICITED_ENABLE
, 0);
5812 * snd_hda_gen_init - initialize the generic spec
5813 * @codec: the HDA codec
5815 * This can be put as patch_ops init function.
5817 int snd_hda_gen_init(struct hda_codec
*codec
)
5819 struct hda_gen_spec
*spec
= codec
->spec
;
5821 if (spec
->init_hook
)
5822 spec
->init_hook(codec
);
5824 snd_hda_apply_verbs(codec
);
5826 init_multi_out(codec
);
5827 init_extra_out(codec
);
5828 init_multi_io(codec
);
5829 init_aamix_paths(codec
);
5830 init_analog_input(codec
);
5831 init_input_src(codec
);
5832 init_digital(codec
);
5834 clear_unsol_on_unused_pins(codec
);
5836 sync_all_pin_power_ctls(codec
);
5838 /* call init functions of standard auto-mute helpers */
5839 update_automute_all(codec
);
5841 regcache_sync(codec
->core
.regmap
);
5843 if (spec
->vmaster_mute
.sw_kctl
&& spec
->vmaster_mute
.hook
)
5844 snd_hda_sync_vmaster_hook(&spec
->vmaster_mute
);
5846 hda_call_check_power_status(codec
, 0x01);
5849 EXPORT_SYMBOL_GPL(snd_hda_gen_init
);
5852 * snd_hda_gen_free - free the generic spec
5853 * @codec: the HDA codec
5855 * This can be put as patch_ops free function.
5857 void snd_hda_gen_free(struct hda_codec
*codec
)
5859 snd_hda_apply_fixup(codec
, HDA_FIXUP_ACT_FREE
);
5860 snd_hda_gen_spec_free(codec
->spec
);
5864 EXPORT_SYMBOL_GPL(snd_hda_gen_free
);
5868 * snd_hda_gen_check_power_status - check the loopback power save state
5869 * @codec: the HDA codec
5870 * @nid: NID to inspect
5872 * This can be put as patch_ops check_power_status function.
5874 int snd_hda_gen_check_power_status(struct hda_codec
*codec
, hda_nid_t nid
)
5876 struct hda_gen_spec
*spec
= codec
->spec
;
5877 return snd_hda_check_amp_list_power(codec
, &spec
->loopback
, nid
);
5879 EXPORT_SYMBOL_GPL(snd_hda_gen_check_power_status
);
5884 * the generic codec support
5887 static const struct hda_codec_ops generic_patch_ops
= {
5888 .build_controls
= snd_hda_gen_build_controls
,
5889 .build_pcms
= snd_hda_gen_build_pcms
,
5890 .init
= snd_hda_gen_init
,
5891 .free
= snd_hda_gen_free
,
5892 .unsol_event
= snd_hda_jack_unsol_event
,
5894 .check_power_status
= snd_hda_gen_check_power_status
,
5899 * snd_hda_parse_generic_codec - Generic codec parser
5900 * @codec: the HDA codec
5902 static int snd_hda_parse_generic_codec(struct hda_codec
*codec
)
5904 struct hda_gen_spec
*spec
;
5907 spec
= kzalloc(sizeof(*spec
), GFP_KERNEL
);
5910 snd_hda_gen_spec_init(spec
);
5913 err
= snd_hda_parse_pin_defcfg(codec
, &spec
->autocfg
, NULL
, 0);
5917 err
= snd_hda_gen_parse_auto_config(codec
, &spec
->autocfg
);
5921 codec
->patch_ops
= generic_patch_ops
;
5925 snd_hda_gen_free(codec
);
5929 static const struct hda_device_id snd_hda_id_generic
[] = {
5930 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC
, "Generic", snd_hda_parse_generic_codec
),
5933 MODULE_DEVICE_TABLE(hdaudio
, snd_hda_id_generic
);
5935 static struct hda_codec_driver generic_driver
= {
5936 .id
= snd_hda_id_generic
,
5939 module_hda_codec_driver(generic_driver
);
5941 MODULE_LICENSE("GPL");
5942 MODULE_DESCRIPTION("Generic HD-audio codec parser");