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/ctype.h>
28 #include <linux/string.h>
29 #include <linux/bitops.h>
30 #include <sound/core.h>
31 #include <sound/jack.h>
32 #include "hda_codec.h"
33 #include "hda_local.h"
34 #include "hda_auto_parser.h"
36 #include "hda_generic.h"
39 /* initialize hda_gen_spec struct */
40 int snd_hda_gen_spec_init(struct hda_gen_spec
*spec
)
42 snd_array_init(&spec
->kctls
, sizeof(struct snd_kcontrol_new
), 32);
43 snd_array_init(&spec
->paths
, sizeof(struct nid_path
), 8);
44 mutex_init(&spec
->pcm_mutex
);
47 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init
);
49 struct snd_kcontrol_new
*
50 snd_hda_gen_add_kctl(struct hda_gen_spec
*spec
, const char *name
,
51 const struct snd_kcontrol_new
*temp
)
53 struct snd_kcontrol_new
*knew
= snd_array_new(&spec
->kctls
);
58 knew
->name
= kstrdup(name
, GFP_KERNEL
);
60 knew
->name
= kstrdup(knew
->name
, GFP_KERNEL
);
65 EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl
);
67 static void free_kctls(struct hda_gen_spec
*spec
)
69 if (spec
->kctls
.list
) {
70 struct snd_kcontrol_new
*kctl
= spec
->kctls
.list
;
72 for (i
= 0; i
< spec
->kctls
.used
; i
++)
75 snd_array_free(&spec
->kctls
);
78 void snd_hda_gen_spec_free(struct hda_gen_spec
*spec
)
83 snd_array_free(&spec
->paths
);
85 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free
);
90 static void parse_user_hints(struct hda_codec
*codec
)
92 struct hda_gen_spec
*spec
= codec
->spec
;
95 val
= snd_hda_get_bool_hint(codec
, "jack_detect");
97 codec
->no_jack_detect
= !val
;
98 val
= snd_hda_get_bool_hint(codec
, "inv_jack_detect");
100 codec
->inv_jack_detect
= !!val
;
101 val
= snd_hda_get_bool_hint(codec
, "trigger_sense");
103 codec
->no_trigger_sense
= !val
;
104 val
= snd_hda_get_bool_hint(codec
, "inv_eapd");
106 codec
->inv_eapd
= !!val
;
107 val
= snd_hda_get_bool_hint(codec
, "pcm_format_first");
109 codec
->pcm_format_first
= !!val
;
110 val
= snd_hda_get_bool_hint(codec
, "sticky_stream");
112 codec
->no_sticky_stream
= !val
;
113 val
= snd_hda_get_bool_hint(codec
, "spdif_status_reset");
115 codec
->spdif_status_reset
= !!val
;
116 val
= snd_hda_get_bool_hint(codec
, "pin_amp_workaround");
118 codec
->pin_amp_workaround
= !!val
;
119 val
= snd_hda_get_bool_hint(codec
, "single_adc_amp");
121 codec
->single_adc_amp
= !!val
;
123 val
= snd_hda_get_bool_hint(codec
, "auto_mute");
125 spec
->suppress_auto_mute
= !val
;
126 val
= snd_hda_get_bool_hint(codec
, "auto_mic");
128 spec
->suppress_auto_mic
= !val
;
129 val
= snd_hda_get_bool_hint(codec
, "line_in_auto_switch");
131 spec
->line_in_auto_switch
= !!val
;
132 val
= snd_hda_get_bool_hint(codec
, "need_dac_fix");
134 spec
->need_dac_fix
= !!val
;
135 val
= snd_hda_get_bool_hint(codec
, "primary_hp");
137 spec
->no_primary_hp
= !val
;
138 val
= snd_hda_get_bool_hint(codec
, "multi_cap_vol");
140 spec
->multi_cap_vol
= !!val
;
141 val
= snd_hda_get_bool_hint(codec
, "inv_dmic_split");
143 spec
->inv_dmic_split
= !!val
;
144 val
= snd_hda_get_bool_hint(codec
, "indep_hp");
146 spec
->indep_hp
= !!val
;
147 val
= snd_hda_get_bool_hint(codec
, "add_stereo_mix_input");
149 spec
->add_stereo_mix_input
= !!val
;
150 val
= snd_hda_get_bool_hint(codec
, "add_out_jack_modes");
152 spec
->add_out_jack_modes
= !!val
;
153 val
= snd_hda_get_bool_hint(codec
, "add_in_jack_modes");
155 spec
->add_in_jack_modes
= !!val
;
157 if (!snd_hda_get_int_hint(codec
, "mixer_nid", &val
))
158 spec
->mixer_nid
= val
;
162 * pin control value accesses
165 #define update_pin_ctl(codec, pin, val) \
166 snd_hda_codec_update_cache(codec, pin, 0, \
167 AC_VERB_SET_PIN_WIDGET_CONTROL, val)
169 /* restore the pinctl based on the cached value */
170 static inline void restore_pin_ctl(struct hda_codec
*codec
, hda_nid_t pin
)
172 update_pin_ctl(codec
, pin
, snd_hda_codec_get_pin_target(codec
, pin
));
175 /* set the pinctl target value and write it if requested */
176 static void set_pin_target(struct hda_codec
*codec
, hda_nid_t pin
,
177 unsigned int val
, bool do_write
)
181 val
= snd_hda_correct_pin_ctl(codec
, pin
, val
);
182 snd_hda_codec_set_pin_target(codec
, pin
, val
);
184 update_pin_ctl(codec
, pin
, val
);
187 /* set pinctl target values for all given pins */
188 static void set_pin_targets(struct hda_codec
*codec
, int num_pins
,
189 hda_nid_t
*pins
, unsigned int val
)
192 for (i
= 0; i
< num_pins
; i
++)
193 set_pin_target(codec
, pins
[i
], val
, false);
200 /* return the position of NID in the list, or -1 if not found */
201 static int find_idx_in_nid_list(hda_nid_t nid
, const hda_nid_t
*list
, int nums
)
204 for (i
= 0; i
< nums
; i
++)
210 /* return true if the given NID is contained in the path */
211 static bool is_nid_contained(struct nid_path
*path
, hda_nid_t nid
)
213 return find_idx_in_nid_list(nid
, path
->path
, path
->depth
) >= 0;
216 static struct nid_path
*get_nid_path(struct hda_codec
*codec
,
217 hda_nid_t from_nid
, hda_nid_t to_nid
,
220 struct hda_gen_spec
*spec
= codec
->spec
;
223 for (i
= 0; i
< spec
->paths
.used
; i
++) {
224 struct nid_path
*path
= snd_array_elem(&spec
->paths
, i
);
225 if (path
->depth
<= 0)
227 if ((!from_nid
|| path
->path
[0] == from_nid
) &&
228 (!to_nid
|| path
->path
[path
->depth
- 1] == to_nid
)) {
230 (anchor_nid
> 0 && is_nid_contained(path
, anchor_nid
)) ||
231 (anchor_nid
< 0 && !is_nid_contained(path
, anchor_nid
)))
238 /* get the path between the given NIDs;
239 * passing 0 to either @pin or @dac behaves as a wildcard
241 struct nid_path
*snd_hda_get_nid_path(struct hda_codec
*codec
,
242 hda_nid_t from_nid
, hda_nid_t to_nid
)
244 return get_nid_path(codec
, from_nid
, to_nid
, 0);
246 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path
);
248 /* get the index number corresponding to the path instance;
249 * the index starts from 1, for easier checking the invalid value
251 int snd_hda_get_path_idx(struct hda_codec
*codec
, struct nid_path
*path
)
253 struct hda_gen_spec
*spec
= codec
->spec
;
254 struct nid_path
*array
= spec
->paths
.list
;
257 if (!spec
->paths
.used
)
260 if (idx
< 0 || idx
>= spec
->paths
.used
)
265 /* get the path instance corresponding to the given index number */
266 struct nid_path
*snd_hda_get_path_from_idx(struct hda_codec
*codec
, int idx
)
268 struct hda_gen_spec
*spec
= codec
->spec
;
270 if (idx
<= 0 || idx
> spec
->paths
.used
)
272 return snd_array_elem(&spec
->paths
, idx
- 1);
275 /* check whether the given DAC is already found in any existing paths */
276 static bool is_dac_already_used(struct hda_codec
*codec
, hda_nid_t nid
)
278 struct hda_gen_spec
*spec
= codec
->spec
;
281 for (i
= 0; i
< spec
->paths
.used
; i
++) {
282 struct nid_path
*path
= snd_array_elem(&spec
->paths
, i
);
283 if (path
->path
[0] == nid
)
289 /* check whether the given two widgets can be connected */
290 static bool is_reachable_path(struct hda_codec
*codec
,
291 hda_nid_t from_nid
, hda_nid_t to_nid
)
293 if (!from_nid
|| !to_nid
)
295 return snd_hda_get_conn_index(codec
, to_nid
, from_nid
, true) >= 0;
298 /* nid, dir and idx */
299 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
301 /* check whether the given ctl is already assigned in any path elements */
302 static bool is_ctl_used(struct hda_codec
*codec
, unsigned int val
, int type
)
304 struct hda_gen_spec
*spec
= codec
->spec
;
307 val
&= AMP_VAL_COMPARE_MASK
;
308 for (i
= 0; i
< spec
->paths
.used
; i
++) {
309 struct nid_path
*path
= snd_array_elem(&spec
->paths
, i
);
310 if ((path
->ctls
[type
] & AMP_VAL_COMPARE_MASK
) == val
)
316 /* check whether a control with the given (nid, dir, idx) was assigned */
317 static bool is_ctl_associated(struct hda_codec
*codec
, hda_nid_t nid
,
320 unsigned int val
= HDA_COMPOSE_AMP_VAL(nid
, 3, idx
, dir
);
321 return is_ctl_used(codec
, val
, NID_PATH_VOL_CTL
) ||
322 is_ctl_used(codec
, val
, NID_PATH_MUTE_CTL
);
325 static void print_nid_path(const char *pfx
, struct nid_path
*path
)
332 for (i
= 0; i
< path
->depth
; i
++) {
334 sprintf(tmp
, ":%02x", path
->path
[i
]);
335 strlcat(buf
, tmp
, sizeof(buf
));
337 snd_printdd("%s path: depth=%d %s\n", pfx
, path
->depth
, buf
);
340 /* called recursively */
341 static bool __parse_nid_path(struct hda_codec
*codec
,
342 hda_nid_t from_nid
, hda_nid_t to_nid
,
343 int anchor_nid
, struct nid_path
*path
,
346 const hda_nid_t
*conn
;
349 if (to_nid
== anchor_nid
)
350 anchor_nid
= 0; /* anchor passed */
351 else if (to_nid
== (hda_nid_t
)(-anchor_nid
))
352 return false; /* hit the exclusive nid */
354 nums
= snd_hda_get_conn_list(codec
, to_nid
, &conn
);
355 for (i
= 0; i
< nums
; i
++) {
356 if (conn
[i
] != from_nid
) {
357 /* special case: when from_nid is 0,
358 * try to find an empty DAC
361 get_wcaps_type(get_wcaps(codec
, conn
[i
])) != AC_WID_AUD_OUT
||
362 is_dac_already_used(codec
, conn
[i
]))
365 /* anchor is not requested or already passed? */
369 if (depth
>= MAX_NID_PATH_DEPTH
)
371 for (i
= 0; i
< nums
; i
++) {
373 type
= get_wcaps_type(get_wcaps(codec
, conn
[i
]));
374 if (type
== AC_WID_AUD_OUT
|| type
== AC_WID_AUD_IN
||
377 if (__parse_nid_path(codec
, from_nid
, conn
[i
],
378 anchor_nid
, path
, depth
+ 1))
384 path
->path
[path
->depth
] = conn
[i
];
385 path
->idx
[path
->depth
+ 1] = i
;
386 if (nums
> 1 && get_wcaps_type(get_wcaps(codec
, to_nid
)) != AC_WID_AUD_MIX
)
387 path
->multi
[path
->depth
+ 1] = 1;
392 /* parse the widget path from the given nid to the target nid;
393 * when @from_nid is 0, try to find an empty DAC;
394 * when @anchor_nid is set to a positive value, only paths through the widget
395 * with the given value are evaluated.
396 * when @anchor_nid is set to a negative value, paths through the widget
397 * with the negative of given value are excluded, only other paths are chosen.
398 * when @anchor_nid is zero, no special handling about path selection.
400 bool snd_hda_parse_nid_path(struct hda_codec
*codec
, hda_nid_t from_nid
,
401 hda_nid_t to_nid
, int anchor_nid
,
402 struct nid_path
*path
)
404 if (__parse_nid_path(codec
, from_nid
, to_nid
, anchor_nid
, path
, 1)) {
405 path
->path
[path
->depth
] = to_nid
;
411 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path
);
414 * parse the path between the given NIDs and add to the path list.
415 * if no valid path is found, return NULL
418 snd_hda_add_new_path(struct hda_codec
*codec
, hda_nid_t from_nid
,
419 hda_nid_t to_nid
, int anchor_nid
)
421 struct hda_gen_spec
*spec
= codec
->spec
;
422 struct nid_path
*path
;
424 if (from_nid
&& to_nid
&& !is_reachable_path(codec
, from_nid
, to_nid
))
427 /* check whether the path has been already added */
428 path
= get_nid_path(codec
, from_nid
, to_nid
, anchor_nid
);
432 path
= snd_array_new(&spec
->paths
);
435 memset(path
, 0, sizeof(*path
));
436 if (snd_hda_parse_nid_path(codec
, from_nid
, to_nid
, anchor_nid
, path
))
442 EXPORT_SYMBOL_HDA(snd_hda_add_new_path
);
444 /* clear the given path as invalid so that it won't be picked up later */
445 static void invalidate_nid_path(struct hda_codec
*codec
, int idx
)
447 struct nid_path
*path
= snd_hda_get_path_from_idx(codec
, idx
);
450 memset(path
, 0, sizeof(*path
));
453 /* look for an empty DAC slot */
454 static hda_nid_t
look_for_dac(struct hda_codec
*codec
, hda_nid_t pin
,
457 struct hda_gen_spec
*spec
= codec
->spec
;
461 for (i
= 0; i
< spec
->num_all_dacs
; i
++) {
462 hda_nid_t nid
= spec
->all_dacs
[i
];
463 if (!nid
|| is_dac_already_used(codec
, nid
))
465 cap_digital
= !!(get_wcaps(codec
, nid
) & AC_WCAP_DIGITAL
);
466 if (is_digital
!= cap_digital
)
468 if (is_reachable_path(codec
, nid
, pin
))
474 /* replace the channels in the composed amp value with the given number */
475 static unsigned int amp_val_replace_channels(unsigned int val
, unsigned int chs
)
477 val
&= ~(0x3U
<< 16);
482 /* check whether the widget has the given amp capability for the direction */
483 static bool check_amp_caps(struct hda_codec
*codec
, hda_nid_t nid
,
484 int dir
, unsigned int bits
)
488 if (get_wcaps(codec
, nid
) & (1 << (dir
+ 1)))
489 if (query_amp_caps(codec
, nid
, dir
) & bits
)
494 static bool same_amp_caps(struct hda_codec
*codec
, hda_nid_t nid1
,
495 hda_nid_t nid2
, int dir
)
497 if (!(get_wcaps(codec
, nid1
) & (1 << (dir
+ 1))))
498 return !(get_wcaps(codec
, nid2
) & (1 << (dir
+ 1)));
499 return (query_amp_caps(codec
, nid1
, dir
) ==
500 query_amp_caps(codec
, nid2
, dir
));
503 #define nid_has_mute(codec, nid, dir) \
504 check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
505 #define nid_has_volume(codec, nid, dir) \
506 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
508 /* look for a widget suitable for assigning a mute switch in the path */
509 static hda_nid_t
look_for_out_mute_nid(struct hda_codec
*codec
,
510 struct nid_path
*path
)
514 for (i
= path
->depth
- 1; i
>= 0; i
--) {
515 if (nid_has_mute(codec
, path
->path
[i
], HDA_OUTPUT
))
516 return path
->path
[i
];
517 if (i
!= path
->depth
- 1 && i
!= 0 &&
518 nid_has_mute(codec
, path
->path
[i
], HDA_INPUT
))
519 return path
->path
[i
];
524 /* look for a widget suitable for assigning a volume ctl in the path */
525 static hda_nid_t
look_for_out_vol_nid(struct hda_codec
*codec
,
526 struct nid_path
*path
)
530 for (i
= path
->depth
- 1; i
>= 0; i
--) {
531 if (nid_has_volume(codec
, path
->path
[i
], HDA_OUTPUT
))
532 return path
->path
[i
];
538 * path activation / deactivation
541 /* can have the amp-in capability? */
542 static bool has_amp_in(struct hda_codec
*codec
, struct nid_path
*path
, int idx
)
544 hda_nid_t nid
= path
->path
[idx
];
545 unsigned int caps
= get_wcaps(codec
, nid
);
546 unsigned int type
= get_wcaps_type(caps
);
548 if (!(caps
& AC_WCAP_IN_AMP
))
550 if (type
== AC_WID_PIN
&& idx
> 0) /* only for input pins */
555 /* can have the amp-out capability? */
556 static bool has_amp_out(struct hda_codec
*codec
, struct nid_path
*path
, int idx
)
558 hda_nid_t nid
= path
->path
[idx
];
559 unsigned int caps
= get_wcaps(codec
, nid
);
560 unsigned int type
= get_wcaps_type(caps
);
562 if (!(caps
& AC_WCAP_OUT_AMP
))
564 if (type
== AC_WID_PIN
&& !idx
) /* only for output pins */
569 /* check whether the given (nid,dir,idx) is active */
570 static bool is_active_nid(struct hda_codec
*codec
, hda_nid_t nid
,
571 unsigned int idx
, unsigned int dir
)
573 struct hda_gen_spec
*spec
= codec
->spec
;
576 for (n
= 0; n
< spec
->paths
.used
; n
++) {
577 struct nid_path
*path
= snd_array_elem(&spec
->paths
, n
);
580 for (i
= 0; i
< path
->depth
; i
++) {
581 if (path
->path
[i
] == nid
) {
582 if (dir
== HDA_OUTPUT
|| path
->idx
[i
] == idx
)
591 /* get the default amp value for the target state */
592 static int get_amp_val_to_activate(struct hda_codec
*codec
, hda_nid_t nid
,
593 int dir
, bool enable
)
596 unsigned int val
= 0;
598 caps
= query_amp_caps(codec
, nid
, dir
);
599 if (caps
& AC_AMPCAP_NUM_STEPS
) {
602 val
= (caps
& AC_AMPCAP_OFFSET
) >> AC_AMPCAP_OFFSET_SHIFT
;
604 if (caps
& AC_AMPCAP_MUTE
) {
611 /* initialize the amp value (only at the first time) */
612 static void init_amp(struct hda_codec
*codec
, hda_nid_t nid
, int dir
, int idx
)
614 int val
= get_amp_val_to_activate(codec
, nid
, dir
, false);
615 snd_hda_codec_amp_init_stereo(codec
, nid
, dir
, idx
, 0xff, val
);
618 static void activate_amp(struct hda_codec
*codec
, hda_nid_t nid
, int dir
,
619 int idx
, bool enable
)
622 if (is_ctl_associated(codec
, nid
, dir
, idx
) ||
623 (!enable
&& is_active_nid(codec
, nid
, dir
, idx
)))
625 val
= get_amp_val_to_activate(codec
, nid
, dir
, enable
);
626 snd_hda_codec_amp_stereo(codec
, nid
, dir
, idx
, 0xff, val
);
629 static void activate_amp_out(struct hda_codec
*codec
, struct nid_path
*path
,
632 hda_nid_t nid
= path
->path
[i
];
633 init_amp(codec
, nid
, HDA_OUTPUT
, 0);
634 activate_amp(codec
, nid
, HDA_OUTPUT
, 0, enable
);
637 static void activate_amp_in(struct hda_codec
*codec
, struct nid_path
*path
,
638 int i
, bool enable
, bool add_aamix
)
640 struct hda_gen_spec
*spec
= codec
->spec
;
641 const hda_nid_t
*conn
;
644 hda_nid_t nid
= path
->path
[i
];
646 nums
= snd_hda_get_conn_list(codec
, nid
, &conn
);
647 type
= get_wcaps_type(get_wcaps(codec
, nid
));
648 if (type
== AC_WID_PIN
||
649 (type
== AC_WID_AUD_IN
&& codec
->single_adc_amp
)) {
655 for (n
= 0; n
< nums
; n
++)
656 init_amp(codec
, nid
, HDA_INPUT
, n
);
658 if (is_ctl_associated(codec
, nid
, HDA_INPUT
, idx
))
661 /* here is a little bit tricky in comparison with activate_amp_out();
662 * when aa-mixer is available, we need to enable the path as well
664 for (n
= 0; n
< nums
; n
++) {
665 if (n
!= idx
&& (!add_aamix
|| conn
[n
] != spec
->mixer_nid
))
667 activate_amp(codec
, nid
, HDA_INPUT
, n
, enable
);
671 /* activate or deactivate the given path
672 * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
674 void snd_hda_activate_path(struct hda_codec
*codec
, struct nid_path
*path
,
675 bool enable
, bool add_aamix
)
680 path
->active
= false;
682 for (i
= path
->depth
- 1; i
>= 0; i
--) {
683 if (enable
&& path
->multi
[i
])
684 snd_hda_codec_write_cache(codec
, path
->path
[i
], 0,
685 AC_VERB_SET_CONNECT_SEL
,
687 if (has_amp_in(codec
, path
, i
))
688 activate_amp_in(codec
, path
, i
, enable
, add_aamix
);
689 if (has_amp_out(codec
, path
, i
))
690 activate_amp_out(codec
, path
, i
, enable
);
696 EXPORT_SYMBOL_HDA(snd_hda_activate_path
);
698 /* turn on/off EAPD on the given pin */
699 static void set_pin_eapd(struct hda_codec
*codec
, hda_nid_t pin
, bool enable
)
701 struct hda_gen_spec
*spec
= codec
->spec
;
702 if (spec
->own_eapd_ctl
||
703 !(snd_hda_query_pin_caps(codec
, pin
) & AC_PINCAP_EAPD
))
707 snd_hda_codec_update_cache(codec
, pin
, 0,
708 AC_VERB_SET_EAPD_BTLENABLE
,
709 enable
? 0x02 : 0x00);
714 * Helper functions for creating mixer ctl elements
722 static const struct snd_kcontrol_new control_templates
[] = {
723 HDA_CODEC_VOLUME(NULL
, 0, 0, 0),
724 HDA_CODEC_MUTE(NULL
, 0, 0, 0),
725 HDA_BIND_MUTE(NULL
, 0, 0, 0),
728 /* add dynamic controls from template */
729 static int add_control(struct hda_gen_spec
*spec
, int type
, const char *name
,
730 int cidx
, unsigned long val
)
732 struct snd_kcontrol_new
*knew
;
734 knew
= snd_hda_gen_add_kctl(spec
, name
, &control_templates
[type
]);
738 if (get_amp_nid_(val
))
739 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
740 knew
->private_value
= val
;
744 static int add_control_with_pfx(struct hda_gen_spec
*spec
, int type
,
745 const char *pfx
, const char *dir
,
746 const char *sfx
, int cidx
, unsigned long val
)
749 snprintf(name
, sizeof(name
), "%s %s %s", pfx
, dir
, sfx
);
750 return add_control(spec
, type
, name
, cidx
, val
);
753 #define add_pb_vol_ctrl(spec, type, pfx, val) \
754 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
755 #define add_pb_sw_ctrl(spec, type, pfx, val) \
756 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
757 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
758 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
759 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
760 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
762 static int add_vol_ctl(struct hda_codec
*codec
, const char *pfx
, int cidx
,
763 unsigned int chs
, struct nid_path
*path
)
768 val
= path
->ctls
[NID_PATH_VOL_CTL
];
771 val
= amp_val_replace_channels(val
, chs
);
772 return __add_pb_vol_ctrl(codec
->spec
, HDA_CTL_WIDGET_VOL
, pfx
, cidx
, val
);
775 /* return the channel bits suitable for the given path->ctls[] */
776 static int get_default_ch_nums(struct hda_codec
*codec
, struct nid_path
*path
,
779 int chs
= 1; /* mono (left only) */
781 hda_nid_t nid
= get_amp_nid_(path
->ctls
[type
]);
782 if (nid
&& (get_wcaps(codec
, nid
) & AC_WCAP_STEREO
))
783 chs
= 3; /* stereo */
788 static int add_stereo_vol(struct hda_codec
*codec
, const char *pfx
, int cidx
,
789 struct nid_path
*path
)
791 int chs
= get_default_ch_nums(codec
, path
, NID_PATH_VOL_CTL
);
792 return add_vol_ctl(codec
, pfx
, cidx
, chs
, path
);
795 /* create a mute-switch for the given mixer widget;
796 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
798 static int add_sw_ctl(struct hda_codec
*codec
, const char *pfx
, int cidx
,
799 unsigned int chs
, struct nid_path
*path
)
802 int type
= HDA_CTL_WIDGET_MUTE
;
806 val
= path
->ctls
[NID_PATH_MUTE_CTL
];
809 val
= amp_val_replace_channels(val
, chs
);
810 if (get_amp_direction_(val
) == HDA_INPUT
) {
811 hda_nid_t nid
= get_amp_nid_(val
);
812 int nums
= snd_hda_get_num_conns(codec
, nid
);
814 type
= HDA_CTL_BIND_MUTE
;
818 return __add_pb_sw_ctrl(codec
->spec
, type
, pfx
, cidx
, val
);
821 static int add_stereo_sw(struct hda_codec
*codec
, const char *pfx
,
822 int cidx
, struct nid_path
*path
)
824 int chs
= get_default_ch_nums(codec
, path
, NID_PATH_MUTE_CTL
);
825 return add_sw_ctl(codec
, pfx
, cidx
, chs
, path
);
828 static const char * const channel_name
[4] = {
829 "Front", "Surround", "CLFE", "Side"
832 /* give some appropriate ctl name prefix for the given line out channel */
833 static const char *get_line_out_pfx(struct hda_gen_spec
*spec
, int ch
,
834 bool can_be_master
, int *index
)
836 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
839 if (cfg
->line_outs
== 1 && !spec
->multi_ios
&&
840 !cfg
->hp_outs
&& !cfg
->speaker_outs
&& can_be_master
)
841 return spec
->vmaster_mute
.hook
? "PCM" : "Master";
843 /* if there is really a single DAC used in the whole output paths,
844 * use it master (or "PCM" if a vmaster hook is present)
846 if (spec
->multiout
.num_dacs
== 1 && !spec
->mixer_nid
&&
847 !spec
->multiout
.hp_out_nid
[0] && !spec
->multiout
.extra_out_nid
[0])
848 return spec
->vmaster_mute
.hook
? "PCM" : "Master";
850 switch (cfg
->line_out_type
) {
851 case AUTO_PIN_SPEAKER_OUT
:
852 if (cfg
->line_outs
== 1)
854 if (cfg
->line_outs
== 2)
855 return ch
? "Bass Speaker" : "Speaker";
857 case AUTO_PIN_HP_OUT
:
858 /* for multi-io case, only the primary out */
859 if (ch
&& spec
->multi_ios
)
864 if (cfg
->line_outs
== 1 && !spec
->multi_ios
)
868 if (ch
>= ARRAY_SIZE(channel_name
)) {
873 return channel_name
[ch
];
880 /* badness definition */
882 /* No primary DAC is found for the main output */
883 BAD_NO_PRIMARY_DAC
= 0x10000,
884 /* No DAC is found for the extra output */
886 /* No possible multi-ios */
887 BAD_MULTI_IO
= 0x103,
888 /* No individual DAC for extra output */
889 BAD_NO_EXTRA_DAC
= 0x102,
890 /* No individual DAC for extra surrounds */
891 BAD_NO_EXTRA_SURR_DAC
= 0x101,
892 /* Primary DAC shared with main surrounds */
893 BAD_SHARED_SURROUND
= 0x100,
894 /* Primary DAC shared with main CLFE */
895 BAD_SHARED_CLFE
= 0x10,
896 /* Primary DAC shared with extra surrounds */
897 BAD_SHARED_EXTRA_SURROUND
= 0x10,
898 /* Volume widget is shared */
899 BAD_SHARED_VOL
= 0x10,
902 /* look for widgets in the given path which are appropriate for
903 * volume and mute controls, and assign the values to ctls[].
905 * When no appropriate widget is found in the path, the badness value
906 * is incremented depending on the situation. The function returns the
907 * total badness for both volume and mute controls.
909 static int assign_out_path_ctls(struct hda_codec
*codec
, struct nid_path
*path
)
916 return BAD_SHARED_VOL
* 2;
918 if (path
->ctls
[NID_PATH_VOL_CTL
] ||
919 path
->ctls
[NID_PATH_MUTE_CTL
])
920 return 0; /* already evaluated */
922 nid
= look_for_out_vol_nid(codec
, path
);
924 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
925 if (is_ctl_used(codec
, val
, NID_PATH_VOL_CTL
))
926 badness
+= BAD_SHARED_VOL
;
928 path
->ctls
[NID_PATH_VOL_CTL
] = val
;
930 badness
+= BAD_SHARED_VOL
;
931 nid
= look_for_out_mute_nid(codec
, path
);
933 unsigned int wid_type
= get_wcaps_type(get_wcaps(codec
, nid
));
934 if (wid_type
== AC_WID_PIN
|| wid_type
== AC_WID_AUD_OUT
||
935 nid_has_mute(codec
, nid
, HDA_OUTPUT
))
936 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
938 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_INPUT
);
939 if (is_ctl_used(codec
, val
, NID_PATH_MUTE_CTL
))
940 badness
+= BAD_SHARED_VOL
;
942 path
->ctls
[NID_PATH_MUTE_CTL
] = val
;
944 badness
+= BAD_SHARED_VOL
;
948 struct badness_table
{
949 int no_primary_dac
; /* no primary DAC */
950 int no_dac
; /* no secondary DACs */
951 int shared_primary
; /* primary DAC is shared with main output */
952 int shared_surr
; /* secondary DAC shared with main or primary */
953 int shared_clfe
; /* third DAC shared with main or primary */
954 int shared_surr_main
; /* secondary DAC sahred with main/DAC0 */
957 static struct badness_table main_out_badness
= {
958 .no_primary_dac
= BAD_NO_PRIMARY_DAC
,
959 .no_dac
= BAD_NO_DAC
,
960 .shared_primary
= BAD_NO_PRIMARY_DAC
,
961 .shared_surr
= BAD_SHARED_SURROUND
,
962 .shared_clfe
= BAD_SHARED_CLFE
,
963 .shared_surr_main
= BAD_SHARED_SURROUND
,
966 static struct badness_table extra_out_badness
= {
967 .no_primary_dac
= BAD_NO_DAC
,
968 .no_dac
= BAD_NO_DAC
,
969 .shared_primary
= BAD_NO_EXTRA_DAC
,
970 .shared_surr
= BAD_SHARED_EXTRA_SURROUND
,
971 .shared_clfe
= BAD_SHARED_EXTRA_SURROUND
,
972 .shared_surr_main
= BAD_NO_EXTRA_SURR_DAC
,
975 /* get the DAC of the primary output corresponding to the given array index */
976 static hda_nid_t
get_primary_out(struct hda_codec
*codec
, int idx
)
978 struct hda_gen_spec
*spec
= codec
->spec
;
979 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
981 if (cfg
->line_outs
> idx
)
982 return spec
->private_dac_nids
[idx
];
983 idx
-= cfg
->line_outs
;
984 if (spec
->multi_ios
> idx
)
985 return spec
->multi_io
[idx
].dac
;
989 /* return the DAC if it's reachable, otherwise zero */
990 static inline hda_nid_t
try_dac(struct hda_codec
*codec
,
991 hda_nid_t dac
, hda_nid_t pin
)
993 return is_reachable_path(codec
, dac
, pin
) ? dac
: 0;
996 /* try to assign DACs to pins and return the resultant badness */
997 static int try_assign_dacs(struct hda_codec
*codec
, int num_outs
,
998 const hda_nid_t
*pins
, hda_nid_t
*dacs
,
1000 const struct badness_table
*bad
)
1002 struct hda_gen_spec
*spec
= codec
->spec
;
1010 for (i
= 0; i
< num_outs
; i
++) {
1011 struct nid_path
*path
;
1012 hda_nid_t pin
= pins
[i
];
1014 path
= snd_hda_get_path_from_idx(codec
, path_idx
[i
]);
1016 badness
+= assign_out_path_ctls(codec
, path
);
1020 dacs
[i
] = look_for_dac(codec
, pin
, false);
1021 if (!dacs
[i
] && !i
) {
1022 /* try to steal the DAC of surrounds for the front */
1023 for (j
= 1; j
< num_outs
; j
++) {
1024 if (is_reachable_path(codec
, dacs
[j
], pin
)) {
1027 invalidate_nid_path(codec
, path_idx
[j
]);
1036 dac
= try_dac(codec
, get_primary_out(codec
, i
), pin
);
1038 dac
= try_dac(codec
, dacs
[0], pin
);
1040 dac
= try_dac(codec
, get_primary_out(codec
, i
), pin
);
1043 badness
+= bad
->shared_primary
;
1045 badness
+= bad
->shared_surr
;
1047 badness
+= bad
->shared_clfe
;
1048 } else if (is_reachable_path(codec
, spec
->private_dac_nids
[0], pin
)) {
1049 dac
= spec
->private_dac_nids
[0];
1050 badness
+= bad
->shared_surr_main
;
1052 badness
+= bad
->no_primary_dac
;
1054 badness
+= bad
->no_dac
;
1056 path
= snd_hda_add_new_path(codec
, dac
, pin
, -spec
->mixer_nid
);
1057 if (!path
&& !i
&& spec
->mixer_nid
) {
1058 /* try with aamix */
1059 path
= snd_hda_add_new_path(codec
, dac
, pin
, 0);
1064 print_nid_path("output", path
);
1065 path
->active
= true;
1066 path_idx
[i
] = snd_hda_get_path_idx(codec
, path
);
1067 badness
+= assign_out_path_ctls(codec
, path
);
1074 /* return NID if the given pin has only a single connection to a certain DAC */
1075 static hda_nid_t
get_dac_if_single(struct hda_codec
*codec
, hda_nid_t pin
)
1077 struct hda_gen_spec
*spec
= codec
->spec
;
1079 hda_nid_t nid_found
= 0;
1081 for (i
= 0; i
< spec
->num_all_dacs
; i
++) {
1082 hda_nid_t nid
= spec
->all_dacs
[i
];
1083 if (!nid
|| is_dac_already_used(codec
, nid
))
1085 if (is_reachable_path(codec
, nid
, pin
)) {
1094 /* check whether the given pin can be a multi-io pin */
1095 static bool can_be_multiio_pin(struct hda_codec
*codec
,
1096 unsigned int location
, hda_nid_t nid
)
1098 unsigned int defcfg
, caps
;
1100 defcfg
= snd_hda_codec_get_pincfg(codec
, nid
);
1101 if (get_defcfg_connect(defcfg
) != AC_JACK_PORT_COMPLEX
)
1103 if (location
&& get_defcfg_location(defcfg
) != location
)
1105 caps
= snd_hda_query_pin_caps(codec
, nid
);
1106 if (!(caps
& AC_PINCAP_OUT
))
1111 /* count the number of input pins that are capable to be multi-io */
1112 static int count_multiio_pins(struct hda_codec
*codec
, hda_nid_t reference_pin
)
1114 struct hda_gen_spec
*spec
= codec
->spec
;
1115 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1116 unsigned int defcfg
= snd_hda_codec_get_pincfg(codec
, reference_pin
);
1117 unsigned int location
= get_defcfg_location(defcfg
);
1121 for (type
= AUTO_PIN_LINE_IN
; type
>= AUTO_PIN_MIC
; type
--) {
1122 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
1123 if (cfg
->inputs
[i
].type
!= type
)
1125 if (can_be_multiio_pin(codec
, location
,
1126 cfg
->inputs
[i
].pin
))
1136 * When hardwired is set, try to fill ony hardwired pins, and returns
1137 * zero if any pins are filled, non-zero if nothing found.
1138 * When hardwired is off, try to fill possible input pins, and returns
1139 * the badness value.
1141 static int fill_multi_ios(struct hda_codec
*codec
,
1142 hda_nid_t reference_pin
,
1145 struct hda_gen_spec
*spec
= codec
->spec
;
1146 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1147 int type
, i
, j
, num_pins
, old_pins
;
1148 unsigned int defcfg
= snd_hda_codec_get_pincfg(codec
, reference_pin
);
1149 unsigned int location
= get_defcfg_location(defcfg
);
1151 struct nid_path
*path
;
1153 old_pins
= spec
->multi_ios
;
1157 num_pins
= count_multiio_pins(codec
, reference_pin
);
1161 for (type
= AUTO_PIN_LINE_IN
; type
>= AUTO_PIN_MIC
; type
--) {
1162 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
1163 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
1166 if (cfg
->inputs
[i
].type
!= type
)
1168 if (!can_be_multiio_pin(codec
, location
, nid
))
1170 for (j
= 0; j
< spec
->multi_ios
; j
++) {
1171 if (nid
== spec
->multi_io
[j
].pin
)
1174 if (j
< spec
->multi_ios
)
1178 dac
= get_dac_if_single(codec
, nid
);
1180 dac
= look_for_dac(codec
, nid
, false);
1185 path
= snd_hda_add_new_path(codec
, dac
, nid
,
1191 print_nid_path("multiio", path
);
1192 spec
->multi_io
[spec
->multi_ios
].pin
= nid
;
1193 spec
->multi_io
[spec
->multi_ios
].dac
= dac
;
1194 spec
->out_paths
[cfg
->line_outs
+ spec
->multi_ios
] =
1195 snd_hda_get_path_idx(codec
, path
);
1197 if (spec
->multi_ios
>= 2)
1203 badness
= BAD_MULTI_IO
;
1204 if (old_pins
== spec
->multi_ios
) {
1206 return 1; /* nothing found */
1208 return badness
; /* no badness if nothing found */
1210 if (!hardwired
&& spec
->multi_ios
< 2) {
1211 /* cancel newly assigned paths */
1212 spec
->paths
.used
-= spec
->multi_ios
- old_pins
;
1213 spec
->multi_ios
= old_pins
;
1217 /* assign volume and mute controls */
1218 for (i
= old_pins
; i
< spec
->multi_ios
; i
++) {
1219 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[cfg
->line_outs
+ i
]);
1220 badness
+= assign_out_path_ctls(codec
, path
);
1226 /* map DACs for all pins in the list if they are single connections */
1227 static bool map_singles(struct hda_codec
*codec
, int outs
,
1228 const hda_nid_t
*pins
, hda_nid_t
*dacs
, int *path_idx
)
1230 struct hda_gen_spec
*spec
= codec
->spec
;
1233 for (i
= 0; i
< outs
; i
++) {
1234 struct nid_path
*path
;
1238 dac
= get_dac_if_single(codec
, pins
[i
]);
1241 path
= snd_hda_add_new_path(codec
, dac
, pins
[i
],
1243 if (!path
&& !i
&& spec
->mixer_nid
)
1244 path
= snd_hda_add_new_path(codec
, dac
, pins
[i
], 0);
1248 print_nid_path("output", path
);
1249 path
->active
= true;
1250 path_idx
[i
] = snd_hda_get_path_idx(codec
, path
);
1256 /* create a new path including aamix if available, and return its index */
1257 static int check_aamix_out_path(struct hda_codec
*codec
, int path_idx
)
1259 struct hda_gen_spec
*spec
= codec
->spec
;
1260 struct nid_path
*path
;
1262 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
1263 if (!path
|| !path
->depth
||
1264 is_nid_contained(path
, spec
->mixer_nid
))
1266 path
= snd_hda_add_new_path(codec
, path
->path
[0],
1267 path
->path
[path
->depth
- 1],
1271 print_nid_path("output-aamix", path
);
1272 path
->active
= false; /* unused as default */
1273 return snd_hda_get_path_idx(codec
, path
);
1276 /* fill the empty entries in the dac array for speaker/hp with the
1277 * shared dac pointed by the paths
1279 static void refill_shared_dacs(struct hda_codec
*codec
, int num_outs
,
1280 hda_nid_t
*dacs
, int *path_idx
)
1282 struct nid_path
*path
;
1285 for (i
= 0; i
< num_outs
; i
++) {
1288 path
= snd_hda_get_path_from_idx(codec
, path_idx
[i
]);
1291 dacs
[i
] = path
->path
[0];
1295 /* fill in the dac_nids table from the parsed pin configuration */
1296 static int fill_and_eval_dacs(struct hda_codec
*codec
,
1297 bool fill_hardwired
,
1298 bool fill_mio_first
)
1300 struct hda_gen_spec
*spec
= codec
->spec
;
1301 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1302 int i
, err
, badness
;
1305 /* set num_dacs once to full for look_for_dac() */
1306 spec
->multiout
.num_dacs
= cfg
->line_outs
;
1307 spec
->multiout
.dac_nids
= spec
->private_dac_nids
;
1308 memset(spec
->private_dac_nids
, 0, sizeof(spec
->private_dac_nids
));
1309 memset(spec
->multiout
.hp_out_nid
, 0, sizeof(spec
->multiout
.hp_out_nid
));
1310 memset(spec
->multiout
.extra_out_nid
, 0, sizeof(spec
->multiout
.extra_out_nid
));
1311 spec
->multi_ios
= 0;
1312 snd_array_free(&spec
->paths
);
1314 /* clear path indices */
1315 memset(spec
->out_paths
, 0, sizeof(spec
->out_paths
));
1316 memset(spec
->hp_paths
, 0, sizeof(spec
->hp_paths
));
1317 memset(spec
->speaker_paths
, 0, sizeof(spec
->speaker_paths
));
1318 memset(spec
->aamix_out_paths
, 0, sizeof(spec
->aamix_out_paths
));
1319 memset(spec
->digout_paths
, 0, sizeof(spec
->digout_paths
));
1320 memset(spec
->input_paths
, 0, sizeof(spec
->input_paths
));
1321 memset(spec
->loopback_paths
, 0, sizeof(spec
->loopback_paths
));
1322 memset(&spec
->digin_path
, 0, sizeof(spec
->digin_path
));
1326 /* fill hard-wired DACs first */
1327 if (fill_hardwired
) {
1330 mapped
= map_singles(codec
, cfg
->line_outs
,
1332 spec
->private_dac_nids
,
1334 mapped
|= map_singles(codec
, cfg
->hp_outs
,
1336 spec
->multiout
.hp_out_nid
,
1338 mapped
|= map_singles(codec
, cfg
->speaker_outs
,
1340 spec
->multiout
.extra_out_nid
,
1341 spec
->speaker_paths
);
1342 if (fill_mio_first
&& cfg
->line_outs
== 1 &&
1343 cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1344 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], true);
1351 badness
+= try_assign_dacs(codec
, cfg
->line_outs
, cfg
->line_out_pins
,
1352 spec
->private_dac_nids
, spec
->out_paths
,
1355 if (fill_mio_first
&&
1356 cfg
->line_outs
== 1 && cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1357 /* try to fill multi-io first */
1358 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], false);
1361 /* we don't count badness at this stage yet */
1364 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
) {
1365 err
= try_assign_dacs(codec
, cfg
->hp_outs
, cfg
->hp_pins
,
1366 spec
->multiout
.hp_out_nid
,
1368 &extra_out_badness
);
1373 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1374 err
= try_assign_dacs(codec
, cfg
->speaker_outs
,
1376 spec
->multiout
.extra_out_nid
,
1377 spec
->speaker_paths
,
1378 &extra_out_badness
);
1383 if (cfg
->line_outs
== 1 && cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1384 err
= fill_multi_ios(codec
, cfg
->line_out_pins
[0], false);
1390 if (spec
->mixer_nid
) {
1391 spec
->aamix_out_paths
[0] =
1392 check_aamix_out_path(codec
, spec
->out_paths
[0]);
1393 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
1394 spec
->aamix_out_paths
[1] =
1395 check_aamix_out_path(codec
, spec
->hp_paths
[0]);
1396 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
1397 spec
->aamix_out_paths
[2] =
1398 check_aamix_out_path(codec
, spec
->speaker_paths
[0]);
1401 if (cfg
->hp_outs
&& cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
)
1402 if (count_multiio_pins(codec
, cfg
->hp_pins
[0]) >= 2)
1403 spec
->multi_ios
= 1; /* give badness */
1405 /* re-count num_dacs and squash invalid entries */
1406 spec
->multiout
.num_dacs
= 0;
1407 for (i
= 0; i
< cfg
->line_outs
; i
++) {
1408 if (spec
->private_dac_nids
[i
])
1409 spec
->multiout
.num_dacs
++;
1411 memmove(spec
->private_dac_nids
+ i
,
1412 spec
->private_dac_nids
+ i
+ 1,
1413 sizeof(hda_nid_t
) * (cfg
->line_outs
- i
- 1));
1414 spec
->private_dac_nids
[cfg
->line_outs
- 1] = 0;
1418 spec
->ext_channel_count
= spec
->min_channel_count
=
1419 spec
->multiout
.num_dacs
* 2;
1421 if (spec
->multi_ios
== 2) {
1422 for (i
= 0; i
< 2; i
++)
1423 spec
->private_dac_nids
[spec
->multiout
.num_dacs
++] =
1424 spec
->multi_io
[i
].dac
;
1425 } else if (spec
->multi_ios
) {
1426 spec
->multi_ios
= 0;
1427 badness
+= BAD_MULTI_IO
;
1430 /* re-fill the shared DAC for speaker / headphone */
1431 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
1432 refill_shared_dacs(codec
, cfg
->hp_outs
,
1433 spec
->multiout
.hp_out_nid
,
1435 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
1436 refill_shared_dacs(codec
, cfg
->speaker_outs
,
1437 spec
->multiout
.extra_out_nid
,
1438 spec
->speaker_paths
);
1440 /* set initial pinctl targets */
1441 if (spec
->prefer_hp_amp
|| cfg
->line_out_type
== AUTO_PIN_HP_OUT
)
1445 set_pin_targets(codec
, cfg
->line_outs
, cfg
->line_out_pins
, val
);
1446 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
1447 set_pin_targets(codec
, cfg
->hp_outs
, cfg
->hp_pins
, PIN_HP
);
1448 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
1449 val
= spec
->prefer_hp_amp
? PIN_HP
: PIN_OUT
;
1450 set_pin_targets(codec
, cfg
->speaker_outs
,
1451 cfg
->speaker_pins
, val
);
1457 #define DEBUG_BADNESS
1459 #ifdef DEBUG_BADNESS
1460 #define debug_badness snd_printdd
1462 #define debug_badness(...)
1465 static void debug_show_configs(struct hda_gen_spec
*spec
, struct auto_pin_cfg
*cfg
)
1467 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1468 cfg
->line_out_pins
[0], cfg
->line_out_pins
[1],
1469 cfg
->line_out_pins
[2], cfg
->line_out_pins
[3],
1470 spec
->multiout
.dac_nids
[0],
1471 spec
->multiout
.dac_nids
[1],
1472 spec
->multiout
.dac_nids
[2],
1473 spec
->multiout
.dac_nids
[3]);
1474 if (spec
->multi_ios
> 0)
1475 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
1477 spec
->multi_io
[0].pin
, spec
->multi_io
[1].pin
,
1478 spec
->multi_io
[0].dac
, spec
->multi_io
[1].dac
);
1479 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1480 cfg
->hp_pins
[0], cfg
->hp_pins
[1],
1481 cfg
->hp_pins
[2], cfg
->hp_pins
[3],
1482 spec
->multiout
.hp_out_nid
[0],
1483 spec
->multiout
.hp_out_nid
[1],
1484 spec
->multiout
.hp_out_nid
[2],
1485 spec
->multiout
.hp_out_nid
[3]);
1486 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
1487 cfg
->speaker_pins
[0], cfg
->speaker_pins
[1],
1488 cfg
->speaker_pins
[2], cfg
->speaker_pins
[3],
1489 spec
->multiout
.extra_out_nid
[0],
1490 spec
->multiout
.extra_out_nid
[1],
1491 spec
->multiout
.extra_out_nid
[2],
1492 spec
->multiout
.extra_out_nid
[3]);
1495 /* find all available DACs of the codec */
1496 static void fill_all_dac_nids(struct hda_codec
*codec
)
1498 struct hda_gen_spec
*spec
= codec
->spec
;
1500 hda_nid_t nid
= codec
->start_nid
;
1502 spec
->num_all_dacs
= 0;
1503 memset(spec
->all_dacs
, 0, sizeof(spec
->all_dacs
));
1504 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
1505 if (get_wcaps_type(get_wcaps(codec
, nid
)) != AC_WID_AUD_OUT
)
1507 if (spec
->num_all_dacs
>= ARRAY_SIZE(spec
->all_dacs
)) {
1508 snd_printk(KERN_ERR
"hda: Too many DACs!\n");
1511 spec
->all_dacs
[spec
->num_all_dacs
++] = nid
;
1515 static int parse_output_paths(struct hda_codec
*codec
)
1517 struct hda_gen_spec
*spec
= codec
->spec
;
1518 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
1519 struct auto_pin_cfg
*best_cfg
;
1520 int best_badness
= INT_MAX
;
1522 bool fill_hardwired
= true, fill_mio_first
= true;
1523 bool best_wired
= true, best_mio
= true;
1524 bool hp_spk_swapped
= false;
1526 best_cfg
= kmalloc(sizeof(*best_cfg
), GFP_KERNEL
);
1532 badness
= fill_and_eval_dacs(codec
, fill_hardwired
,
1538 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
1539 cfg
->line_out_type
, fill_hardwired
, fill_mio_first
,
1541 debug_show_configs(spec
, cfg
);
1542 if (badness
< best_badness
) {
1543 best_badness
= badness
;
1545 best_wired
= fill_hardwired
;
1546 best_mio
= fill_mio_first
;
1550 fill_mio_first
= !fill_mio_first
;
1551 if (!fill_mio_first
)
1553 fill_hardwired
= !fill_hardwired
;
1554 if (!fill_hardwired
)
1558 hp_spk_swapped
= true;
1559 if (cfg
->speaker_outs
> 0 &&
1560 cfg
->line_out_type
== AUTO_PIN_HP_OUT
) {
1561 cfg
->hp_outs
= cfg
->line_outs
;
1562 memcpy(cfg
->hp_pins
, cfg
->line_out_pins
,
1563 sizeof(cfg
->hp_pins
));
1564 cfg
->line_outs
= cfg
->speaker_outs
;
1565 memcpy(cfg
->line_out_pins
, cfg
->speaker_pins
,
1566 sizeof(cfg
->speaker_pins
));
1567 cfg
->speaker_outs
= 0;
1568 memset(cfg
->speaker_pins
, 0, sizeof(cfg
->speaker_pins
));
1569 cfg
->line_out_type
= AUTO_PIN_SPEAKER_OUT
;
1570 fill_hardwired
= true;
1573 if (cfg
->hp_outs
> 0 &&
1574 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
) {
1575 cfg
->speaker_outs
= cfg
->line_outs
;
1576 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
1577 sizeof(cfg
->speaker_pins
));
1578 cfg
->line_outs
= cfg
->hp_outs
;
1579 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
,
1580 sizeof(cfg
->hp_pins
));
1582 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
1583 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
1584 fill_hardwired
= true;
1591 debug_badness("==> restoring best_cfg\n");
1593 fill_and_eval_dacs(codec
, best_wired
, best_mio
);
1595 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
1596 cfg
->line_out_type
, best_wired
, best_mio
);
1597 debug_show_configs(spec
, cfg
);
1599 if (cfg
->line_out_pins
[0]) {
1600 struct nid_path
*path
;
1601 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[0]);
1603 spec
->vmaster_nid
= look_for_out_vol_nid(codec
, path
);
1604 if (spec
->vmaster_nid
)
1605 snd_hda_set_vmaster_tlv(codec
, spec
->vmaster_nid
,
1606 HDA_OUTPUT
, spec
->vmaster_tlv
);
1613 /* add playback controls from the parsed DAC table */
1614 static int create_multi_out_ctls(struct hda_codec
*codec
,
1615 const struct auto_pin_cfg
*cfg
)
1617 struct hda_gen_spec
*spec
= codec
->spec
;
1618 int i
, err
, noutputs
;
1620 noutputs
= cfg
->line_outs
;
1621 if (spec
->multi_ios
> 0 && cfg
->line_outs
< 3)
1622 noutputs
+= spec
->multi_ios
;
1624 for (i
= 0; i
< noutputs
; i
++) {
1627 struct nid_path
*path
;
1629 if (i
>= cfg
->line_outs
) {
1631 name
= channel_name
[i
];
1633 name
= get_line_out_pfx(spec
, i
, true, &index
);
1636 path
= snd_hda_get_path_from_idx(codec
, spec
->out_paths
[i
]);
1639 if (!name
|| !strcmp(name
, "CLFE")) {
1641 err
= add_vol_ctl(codec
, "Center", 0, 1, path
);
1644 err
= add_vol_ctl(codec
, "LFE", 0, 2, path
);
1647 err
= add_sw_ctl(codec
, "Center", 0, 1, path
);
1650 err
= add_sw_ctl(codec
, "LFE", 0, 2, path
);
1654 err
= add_stereo_vol(codec
, name
, index
, path
);
1657 err
= add_stereo_sw(codec
, name
, index
, path
);
1665 static int create_extra_out(struct hda_codec
*codec
, int path_idx
,
1666 const char *pfx
, int cidx
)
1668 struct nid_path
*path
;
1671 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
1674 err
= add_stereo_vol(codec
, pfx
, cidx
, path
);
1677 err
= add_stereo_sw(codec
, pfx
, cidx
, path
);
1683 /* add playback controls for speaker and HP outputs */
1684 static int create_extra_outs(struct hda_codec
*codec
, int num_pins
,
1685 const int *paths
, const char *pfx
)
1689 for (i
= 0; i
< num_pins
; i
++) {
1694 if (num_pins
== 2 && i
== 1 && !strcmp(pfx
, "Speaker"))
1695 name
= "Bass Speaker";
1696 else if (num_pins
>= 3) {
1697 snprintf(tmp
, sizeof(tmp
), "%s %s",
1698 pfx
, channel_name
[i
]);
1704 err
= create_extra_out(codec
, paths
[i
], name
, idx
);
1711 static int create_hp_out_ctls(struct hda_codec
*codec
)
1713 struct hda_gen_spec
*spec
= codec
->spec
;
1714 return create_extra_outs(codec
, spec
->autocfg
.hp_outs
,
1719 static int create_speaker_out_ctls(struct hda_codec
*codec
)
1721 struct hda_gen_spec
*spec
= codec
->spec
;
1722 return create_extra_outs(codec
, spec
->autocfg
.speaker_outs
,
1723 spec
->speaker_paths
,
1728 * independent HP controls
1731 static int indep_hp_info(struct snd_kcontrol
*kcontrol
,
1732 struct snd_ctl_elem_info
*uinfo
)
1734 return snd_hda_enum_bool_helper_info(kcontrol
, uinfo
);
1737 static int indep_hp_get(struct snd_kcontrol
*kcontrol
,
1738 struct snd_ctl_elem_value
*ucontrol
)
1740 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1741 struct hda_gen_spec
*spec
= codec
->spec
;
1742 ucontrol
->value
.enumerated
.item
[0] = spec
->indep_hp_enabled
;
1746 static int indep_hp_put(struct snd_kcontrol
*kcontrol
,
1747 struct snd_ctl_elem_value
*ucontrol
)
1749 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1750 struct hda_gen_spec
*spec
= codec
->spec
;
1751 unsigned int select
= ucontrol
->value
.enumerated
.item
[0];
1754 mutex_lock(&spec
->pcm_mutex
);
1755 if (spec
->active_streams
) {
1760 if (spec
->indep_hp_enabled
!= select
) {
1761 spec
->indep_hp_enabled
= select
;
1762 if (spec
->indep_hp_enabled
)
1763 spec
->multiout
.hp_out_nid
[0] = 0;
1765 spec
->multiout
.hp_out_nid
[0] = spec
->alt_dac_nid
;
1769 mutex_unlock(&spec
->pcm_mutex
);
1773 static const struct snd_kcontrol_new indep_hp_ctl
= {
1774 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1775 .name
= "Independent HP",
1776 .info
= indep_hp_info
,
1777 .get
= indep_hp_get
,
1778 .put
= indep_hp_put
,
1782 static int create_indep_hp_ctls(struct hda_codec
*codec
)
1784 struct hda_gen_spec
*spec
= codec
->spec
;
1786 if (!spec
->indep_hp
)
1788 if (!spec
->multiout
.hp_out_nid
[0]) {
1793 spec
->indep_hp_enabled
= false;
1794 spec
->alt_dac_nid
= spec
->multiout
.hp_out_nid
[0];
1795 if (!snd_hda_gen_add_kctl(spec
, NULL
, &indep_hp_ctl
))
1801 * channel mode enum control
1804 static int ch_mode_info(struct snd_kcontrol
*kcontrol
,
1805 struct snd_ctl_elem_info
*uinfo
)
1807 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1808 struct hda_gen_spec
*spec
= codec
->spec
;
1811 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
1813 uinfo
->value
.enumerated
.items
= spec
->multi_ios
+ 1;
1814 if (uinfo
->value
.enumerated
.item
> spec
->multi_ios
)
1815 uinfo
->value
.enumerated
.item
= spec
->multi_ios
;
1816 chs
= uinfo
->value
.enumerated
.item
* 2 + spec
->min_channel_count
;
1817 sprintf(uinfo
->value
.enumerated
.name
, "%dch", chs
);
1821 static int ch_mode_get(struct snd_kcontrol
*kcontrol
,
1822 struct snd_ctl_elem_value
*ucontrol
)
1824 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1825 struct hda_gen_spec
*spec
= codec
->spec
;
1826 ucontrol
->value
.enumerated
.item
[0] =
1827 (spec
->ext_channel_count
- spec
->min_channel_count
) / 2;
1831 static inline struct nid_path
*
1832 get_multiio_path(struct hda_codec
*codec
, int idx
)
1834 struct hda_gen_spec
*spec
= codec
->spec
;
1835 return snd_hda_get_path_from_idx(codec
,
1836 spec
->out_paths
[spec
->autocfg
.line_outs
+ idx
]);
1839 static void update_automute_all(struct hda_codec
*codec
);
1841 static int set_multi_io(struct hda_codec
*codec
, int idx
, bool output
)
1843 struct hda_gen_spec
*spec
= codec
->spec
;
1844 hda_nid_t nid
= spec
->multi_io
[idx
].pin
;
1845 struct nid_path
*path
;
1847 path
= get_multiio_path(codec
, idx
);
1851 if (path
->active
== output
)
1855 set_pin_target(codec
, nid
, PIN_OUT
, true);
1856 snd_hda_activate_path(codec
, path
, true, true);
1857 set_pin_eapd(codec
, nid
, true);
1859 set_pin_eapd(codec
, nid
, false);
1860 snd_hda_activate_path(codec
, path
, false, true);
1861 set_pin_target(codec
, nid
, spec
->multi_io
[idx
].ctl_in
, true);
1864 /* update jack retasking in case it modifies any of them */
1865 update_automute_all(codec
);
1870 static int ch_mode_put(struct snd_kcontrol
*kcontrol
,
1871 struct snd_ctl_elem_value
*ucontrol
)
1873 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1874 struct hda_gen_spec
*spec
= codec
->spec
;
1877 ch
= ucontrol
->value
.enumerated
.item
[0];
1878 if (ch
< 0 || ch
> spec
->multi_ios
)
1880 if (ch
== (spec
->ext_channel_count
- spec
->min_channel_count
) / 2)
1882 spec
->ext_channel_count
= ch
* 2 + spec
->min_channel_count
;
1883 for (i
= 0; i
< spec
->multi_ios
; i
++)
1884 set_multi_io(codec
, i
, i
< ch
);
1885 spec
->multiout
.max_channels
= max(spec
->ext_channel_count
,
1886 spec
->const_channel_count
);
1887 if (spec
->need_dac_fix
)
1888 spec
->multiout
.num_dacs
= spec
->multiout
.max_channels
/ 2;
1892 static const struct snd_kcontrol_new channel_mode_enum
= {
1893 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1894 .name
= "Channel Mode",
1895 .info
= ch_mode_info
,
1900 static int create_multi_channel_mode(struct hda_codec
*codec
)
1902 struct hda_gen_spec
*spec
= codec
->spec
;
1904 if (spec
->multi_ios
> 0) {
1905 if (!snd_hda_gen_add_kctl(spec
, NULL
, &channel_mode_enum
))
1912 * aamix loopback enable/disable switch
1915 #define loopback_mixing_info indep_hp_info
1917 static int loopback_mixing_get(struct snd_kcontrol
*kcontrol
,
1918 struct snd_ctl_elem_value
*ucontrol
)
1920 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1921 struct hda_gen_spec
*spec
= codec
->spec
;
1922 ucontrol
->value
.enumerated
.item
[0] = spec
->aamix_mode
;
1926 static void update_aamix_paths(struct hda_codec
*codec
, bool do_mix
,
1927 int nomix_path_idx
, int mix_path_idx
)
1929 struct nid_path
*nomix_path
, *mix_path
;
1931 nomix_path
= snd_hda_get_path_from_idx(codec
, nomix_path_idx
);
1932 mix_path
= snd_hda_get_path_from_idx(codec
, mix_path_idx
);
1933 if (!nomix_path
|| !mix_path
)
1936 snd_hda_activate_path(codec
, nomix_path
, false, true);
1937 snd_hda_activate_path(codec
, mix_path
, true, true);
1939 snd_hda_activate_path(codec
, mix_path
, false, true);
1940 snd_hda_activate_path(codec
, nomix_path
, true, true);
1944 static int loopback_mixing_put(struct snd_kcontrol
*kcontrol
,
1945 struct snd_ctl_elem_value
*ucontrol
)
1947 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
1948 struct hda_gen_spec
*spec
= codec
->spec
;
1949 unsigned int val
= ucontrol
->value
.enumerated
.item
[0];
1951 if (val
== spec
->aamix_mode
)
1953 spec
->aamix_mode
= val
;
1954 update_aamix_paths(codec
, val
, spec
->out_paths
[0],
1955 spec
->aamix_out_paths
[0]);
1956 update_aamix_paths(codec
, val
, spec
->hp_paths
[0],
1957 spec
->aamix_out_paths
[1]);
1958 update_aamix_paths(codec
, val
, spec
->speaker_paths
[0],
1959 spec
->aamix_out_paths
[2]);
1963 static const struct snd_kcontrol_new loopback_mixing_enum
= {
1964 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1965 .name
= "Loopback Mixing",
1966 .info
= loopback_mixing_info
,
1967 .get
= loopback_mixing_get
,
1968 .put
= loopback_mixing_put
,
1971 static int create_loopback_mixing_ctl(struct hda_codec
*codec
)
1973 struct hda_gen_spec
*spec
= codec
->spec
;
1975 if (!spec
->mixer_nid
)
1977 if (!(spec
->aamix_out_paths
[0] || spec
->aamix_out_paths
[1] ||
1978 spec
->aamix_out_paths
[2]))
1980 if (!snd_hda_gen_add_kctl(spec
, NULL
, &loopback_mixing_enum
))
1986 * shared headphone/mic handling
1989 static void call_update_outputs(struct hda_codec
*codec
);
1991 /* for shared I/O, change the pin-control accordingly */
1992 static void update_shared_mic_hp(struct hda_codec
*codec
, bool set_as_mic
)
1994 struct hda_gen_spec
*spec
= codec
->spec
;
1996 hda_nid_t pin
= spec
->autocfg
.inputs
[1].pin
;
1997 /* NOTE: this assumes that there are only two inputs, the
1998 * first is the real internal mic and the second is HP/mic jack.
2001 val
= snd_hda_get_default_vref(codec
, pin
);
2003 /* This pin does not have vref caps - let's enable vref on pin 0x18
2004 instead, as suggested by Realtek */
2005 if (val
== AC_PINCTL_VREF_HIZ
&& spec
->shared_mic_vref_pin
) {
2006 const hda_nid_t vref_pin
= spec
->shared_mic_vref_pin
;
2007 unsigned int vref_val
= snd_hda_get_default_vref(codec
, vref_pin
);
2008 if (vref_val
!= AC_PINCTL_VREF_HIZ
)
2009 snd_hda_set_pin_ctl_cache(codec
, vref_pin
,
2010 PIN_IN
| (set_as_mic
? vref_val
: 0));
2013 val
= set_as_mic
? val
| PIN_IN
: PIN_HP
;
2014 set_pin_target(codec
, pin
, val
, true);
2016 spec
->automute_speaker
= !set_as_mic
;
2017 call_update_outputs(codec
);
2020 /* create a shared input with the headphone out */
2021 static int create_shared_input(struct hda_codec
*codec
)
2023 struct hda_gen_spec
*spec
= codec
->spec
;
2024 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
2025 unsigned int defcfg
;
2028 /* only one internal input pin? */
2029 if (cfg
->num_inputs
!= 1)
2031 defcfg
= snd_hda_codec_get_pincfg(codec
, cfg
->inputs
[0].pin
);
2032 if (snd_hda_get_input_pin_attr(defcfg
) != INPUT_PIN_ATTR_INT
)
2035 if (cfg
->hp_outs
== 1 && cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
)
2036 nid
= cfg
->hp_pins
[0]; /* OK, we have a single HP-out */
2037 else if (cfg
->line_outs
== 1 && cfg
->line_out_type
== AUTO_PIN_HP_OUT
)
2038 nid
= cfg
->line_out_pins
[0]; /* OK, we have a single line-out */
2040 return 0; /* both not available */
2042 if (!(snd_hda_query_pin_caps(codec
, nid
) & AC_PINCAP_IN
))
2043 return 0; /* no input */
2045 cfg
->inputs
[1].pin
= nid
;
2046 cfg
->inputs
[1].type
= AUTO_PIN_MIC
;
2047 cfg
->num_inputs
= 2;
2048 spec
->shared_mic_hp
= 1;
2049 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid
);
2056 static int out_jack_mode_info(struct snd_kcontrol
*kcontrol
,
2057 struct snd_ctl_elem_info
*uinfo
)
2059 static const char * const texts
[] = {
2060 "Line Out", "Headphone Out",
2062 return snd_hda_enum_helper_info(kcontrol
, uinfo
, 2, texts
);
2065 static int out_jack_mode_get(struct snd_kcontrol
*kcontrol
,
2066 struct snd_ctl_elem_value
*ucontrol
)
2068 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2069 hda_nid_t nid
= kcontrol
->private_value
;
2070 if (snd_hda_codec_get_pin_target(codec
, nid
) == PIN_HP
)
2071 ucontrol
->value
.enumerated
.item
[0] = 1;
2073 ucontrol
->value
.enumerated
.item
[0] = 0;
2077 static int out_jack_mode_put(struct snd_kcontrol
*kcontrol
,
2078 struct snd_ctl_elem_value
*ucontrol
)
2080 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2081 hda_nid_t nid
= kcontrol
->private_value
;
2084 val
= ucontrol
->value
.enumerated
.item
[0] ? PIN_HP
: PIN_OUT
;
2085 if (snd_hda_codec_get_pin_target(codec
, nid
) == val
)
2087 snd_hda_set_pin_ctl_cache(codec
, nid
, val
);
2091 static const struct snd_kcontrol_new out_jack_mode_enum
= {
2092 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2093 .info
= out_jack_mode_info
,
2094 .get
= out_jack_mode_get
,
2095 .put
= out_jack_mode_put
,
2098 static bool find_kctl_name(struct hda_codec
*codec
, const char *name
, int idx
)
2100 struct hda_gen_spec
*spec
= codec
->spec
;
2103 for (i
= 0; i
< spec
->kctls
.used
; i
++) {
2104 struct snd_kcontrol_new
*kctl
= snd_array_elem(&spec
->kctls
, i
);
2105 if (!strcmp(kctl
->name
, name
) && kctl
->index
== idx
)
2111 static void get_jack_mode_name(struct hda_codec
*codec
, hda_nid_t pin
,
2112 char *name
, size_t name_len
)
2114 struct hda_gen_spec
*spec
= codec
->spec
;
2117 snd_hda_get_pin_label(codec
, pin
, &spec
->autocfg
, name
, name_len
, &idx
);
2118 strlcat(name
, " Jack Mode", name_len
);
2120 for (; find_kctl_name(codec
, name
, idx
); idx
++)
2124 static int create_out_jack_modes(struct hda_codec
*codec
, int num_pins
,
2127 struct hda_gen_spec
*spec
= codec
->spec
;
2130 for (i
= 0; i
< num_pins
; i
++) {
2131 hda_nid_t pin
= pins
[i
];
2132 unsigned int pincap
= snd_hda_query_pin_caps(codec
, pin
);
2133 if ((pincap
& AC_PINCAP_OUT
) && (pincap
& AC_PINCAP_HP_DRV
)) {
2134 struct snd_kcontrol_new
*knew
;
2136 get_jack_mode_name(codec
, pin
, name
, sizeof(name
));
2137 knew
= snd_hda_gen_add_kctl(spec
, name
,
2138 &out_jack_mode_enum
);
2141 knew
->private_value
= pin
;
2152 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
2155 static const char * const vref_texts
[NUM_VREFS
] = {
2156 "Line In", "Mic 50pc Bias", "Mic 0V Bias",
2157 "", "Mic 80pc Bias", "Mic 100pc Bias"
2160 static unsigned int get_vref_caps(struct hda_codec
*codec
, hda_nid_t pin
)
2162 unsigned int pincap
;
2164 pincap
= snd_hda_query_pin_caps(codec
, pin
);
2165 pincap
= (pincap
& AC_PINCAP_VREF
) >> AC_PINCAP_VREF_SHIFT
;
2166 /* filter out unusual vrefs */
2167 pincap
&= ~(AC_PINCAP_VREF_GRD
| AC_PINCAP_VREF_100
);
2171 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
2172 static int get_vref_idx(unsigned int vref_caps
, unsigned int item_idx
)
2174 unsigned int i
, n
= 0;
2176 for (i
= 0; i
< NUM_VREFS
; i
++) {
2177 if (vref_caps
& (1 << i
)) {
2186 /* convert back from the vref ctl index to the enum item index */
2187 static int cvt_from_vref_idx(unsigned int vref_caps
, unsigned int idx
)
2189 unsigned int i
, n
= 0;
2191 for (i
= 0; i
< NUM_VREFS
; i
++) {
2194 if (vref_caps
& (1 << i
))
2200 static int in_jack_mode_info(struct snd_kcontrol
*kcontrol
,
2201 struct snd_ctl_elem_info
*uinfo
)
2203 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2204 hda_nid_t nid
= kcontrol
->private_value
;
2205 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2207 snd_hda_enum_helper_info(kcontrol
, uinfo
, hweight32(vref_caps
),
2209 /* set the right text */
2210 strcpy(uinfo
->value
.enumerated
.name
,
2211 vref_texts
[get_vref_idx(vref_caps
, uinfo
->value
.enumerated
.item
)]);
2215 static int in_jack_mode_get(struct snd_kcontrol
*kcontrol
,
2216 struct snd_ctl_elem_value
*ucontrol
)
2218 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2219 hda_nid_t nid
= kcontrol
->private_value
;
2220 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2223 idx
= snd_hda_codec_get_pin_target(codec
, nid
) & AC_PINCTL_VREFEN
;
2224 ucontrol
->value
.enumerated
.item
[0] = cvt_from_vref_idx(vref_caps
, idx
);
2228 static int in_jack_mode_put(struct snd_kcontrol
*kcontrol
,
2229 struct snd_ctl_elem_value
*ucontrol
)
2231 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2232 hda_nid_t nid
= kcontrol
->private_value
;
2233 unsigned int vref_caps
= get_vref_caps(codec
, nid
);
2234 unsigned int val
, idx
;
2236 val
= snd_hda_codec_get_pin_target(codec
, nid
);
2237 idx
= cvt_from_vref_idx(vref_caps
, val
& AC_PINCTL_VREFEN
);
2238 if (idx
== ucontrol
->value
.enumerated
.item
[0])
2241 val
&= ~AC_PINCTL_VREFEN
;
2242 val
|= get_vref_idx(vref_caps
, ucontrol
->value
.enumerated
.item
[0]);
2243 snd_hda_set_pin_ctl_cache(codec
, nid
, val
);
2247 static const struct snd_kcontrol_new in_jack_mode_enum
= {
2248 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2249 .info
= in_jack_mode_info
,
2250 .get
= in_jack_mode_get
,
2251 .put
= in_jack_mode_put
,
2254 static int create_in_jack_mode(struct hda_codec
*codec
, hda_nid_t pin
)
2256 struct hda_gen_spec
*spec
= codec
->spec
;
2257 unsigned int defcfg
;
2258 struct snd_kcontrol_new
*knew
;
2261 /* no jack mode for fixed pins */
2262 defcfg
= snd_hda_codec_get_pincfg(codec
, pin
);
2263 if (snd_hda_get_input_pin_attr(defcfg
) == INPUT_PIN_ATTR_INT
)
2266 /* no multiple vref caps? */
2267 if (hweight32(get_vref_caps(codec
, pin
)) <= 1)
2270 get_jack_mode_name(codec
, pin
, name
, sizeof(name
));
2271 knew
= snd_hda_gen_add_kctl(spec
, name
, &in_jack_mode_enum
);
2274 knew
->private_value
= pin
;
2284 /* add the powersave loopback-list entry */
2285 static void add_loopback_list(struct hda_gen_spec
*spec
, hda_nid_t mix
, int idx
)
2287 struct hda_amp_list
*list
;
2289 if (spec
->num_loopbacks
>= ARRAY_SIZE(spec
->loopback_list
) - 1)
2291 list
= spec
->loopback_list
+ spec
->num_loopbacks
;
2293 list
->dir
= HDA_INPUT
;
2295 spec
->num_loopbacks
++;
2296 spec
->loopback
.amplist
= spec
->loopback_list
;
2299 #define add_loopback_list(spec, mix, idx) /* NOP */
2302 /* create input playback/capture controls for the given pin */
2303 static int new_analog_input(struct hda_codec
*codec
, int input_idx
,
2304 hda_nid_t pin
, const char *ctlname
, int ctlidx
,
2307 struct hda_gen_spec
*spec
= codec
->spec
;
2308 struct nid_path
*path
;
2312 if (!nid_has_volume(codec
, mix_nid
, HDA_INPUT
) &&
2313 !nid_has_mute(codec
, mix_nid
, HDA_INPUT
))
2314 return 0; /* no need for analog loopback */
2316 path
= snd_hda_add_new_path(codec
, pin
, mix_nid
, 0);
2319 print_nid_path("loopback", path
);
2320 spec
->loopback_paths
[input_idx
] = snd_hda_get_path_idx(codec
, path
);
2322 idx
= path
->idx
[path
->depth
- 1];
2323 if (nid_has_volume(codec
, mix_nid
, HDA_INPUT
)) {
2324 val
= HDA_COMPOSE_AMP_VAL(mix_nid
, 3, idx
, HDA_INPUT
);
2325 err
= __add_pb_vol_ctrl(spec
, HDA_CTL_WIDGET_VOL
, ctlname
, ctlidx
, val
);
2328 path
->ctls
[NID_PATH_VOL_CTL
] = val
;
2331 if (nid_has_mute(codec
, mix_nid
, HDA_INPUT
)) {
2332 val
= HDA_COMPOSE_AMP_VAL(mix_nid
, 3, idx
, HDA_INPUT
);
2333 err
= __add_pb_sw_ctrl(spec
, HDA_CTL_WIDGET_MUTE
, ctlname
, ctlidx
, val
);
2336 path
->ctls
[NID_PATH_MUTE_CTL
] = val
;
2339 path
->active
= true;
2340 add_loopback_list(spec
, mix_nid
, idx
);
2344 static int is_input_pin(struct hda_codec
*codec
, hda_nid_t nid
)
2346 unsigned int pincap
= snd_hda_query_pin_caps(codec
, nid
);
2347 return (pincap
& AC_PINCAP_IN
) != 0;
2350 /* Parse the codec tree and retrieve ADCs */
2351 static int fill_adc_nids(struct hda_codec
*codec
)
2353 struct hda_gen_spec
*spec
= codec
->spec
;
2355 hda_nid_t
*adc_nids
= spec
->adc_nids
;
2356 int max_nums
= ARRAY_SIZE(spec
->adc_nids
);
2359 nid
= codec
->start_nid
;
2360 for (i
= 0; i
< codec
->num_nodes
; i
++, nid
++) {
2361 unsigned int caps
= get_wcaps(codec
, nid
);
2362 int type
= get_wcaps_type(caps
);
2364 if (type
!= AC_WID_AUD_IN
|| (caps
& AC_WCAP_DIGITAL
))
2366 adc_nids
[nums
] = nid
;
2367 if (++nums
>= max_nums
)
2370 spec
->num_adc_nids
= nums
;
2372 /* copy the detected ADCs to all_adcs[] */
2373 spec
->num_all_adcs
= nums
;
2374 memcpy(spec
->all_adcs
, spec
->adc_nids
, nums
* sizeof(hda_nid_t
));
2379 /* filter out invalid adc_nids that don't give all active input pins;
2380 * if needed, check whether dynamic ADC-switching is available
2382 static int check_dyn_adc_switch(struct hda_codec
*codec
)
2384 struct hda_gen_spec
*spec
= codec
->spec
;
2385 struct hda_input_mux
*imux
= &spec
->input_mux
;
2386 unsigned int ok_bits
;
2392 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
2393 for (i
= 0; i
< imux
->num_items
; i
++) {
2394 if (!spec
->input_paths
[i
][n
])
2397 if (i
>= imux
->num_items
) {
2398 ok_bits
|= (1 << n
);
2404 if (spec
->shared_mic_hp
) {
2405 spec
->shared_mic_hp
= 0;
2406 imux
->num_items
= 1;
2410 /* check whether ADC-switch is possible */
2411 for (i
= 0; i
< imux
->num_items
; i
++) {
2412 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
2413 if (spec
->input_paths
[i
][n
]) {
2414 spec
->dyn_adc_idx
[i
] = n
;
2420 snd_printdd("hda-codec: enabling ADC switching\n");
2421 spec
->dyn_adc_switch
= 1;
2422 } else if (nums
!= spec
->num_adc_nids
) {
2423 /* shrink the invalid adcs and input paths */
2425 for (n
= 0; n
< spec
->num_adc_nids
; n
++) {
2426 if (!(ok_bits
& (1 << n
)))
2429 spec
->adc_nids
[nums
] = spec
->adc_nids
[n
];
2430 for (i
= 0; i
< imux
->num_items
; i
++) {
2431 invalidate_nid_path(codec
,
2432 spec
->input_paths
[i
][nums
]);
2433 spec
->input_paths
[i
][nums
] =
2434 spec
->input_paths
[i
][n
];
2439 spec
->num_adc_nids
= nums
;
2442 if (imux
->num_items
== 1 || spec
->shared_mic_hp
) {
2443 snd_printdd("hda-codec: reducing to a single ADC\n");
2444 spec
->num_adc_nids
= 1; /* reduce to a single ADC */
2447 /* single index for individual volumes ctls */
2448 if (!spec
->dyn_adc_switch
&& spec
->multi_cap_vol
)
2449 spec
->num_adc_nids
= 1;
2454 /* parse capture source paths from the given pin and create imux items */
2455 static int parse_capture_source(struct hda_codec
*codec
, hda_nid_t pin
,
2456 int num_adcs
, const char *label
, int anchor
)
2458 struct hda_gen_spec
*spec
= codec
->spec
;
2459 struct hda_input_mux
*imux
= &spec
->input_mux
;
2460 int imux_idx
= imux
->num_items
;
2461 bool imux_added
= false;
2464 for (c
= 0; c
< num_adcs
; c
++) {
2465 struct nid_path
*path
;
2466 hda_nid_t adc
= spec
->adc_nids
[c
];
2468 if (!is_reachable_path(codec
, pin
, adc
))
2470 path
= snd_hda_add_new_path(codec
, pin
, adc
, anchor
);
2473 print_nid_path("input", path
);
2474 spec
->input_paths
[imux_idx
][c
] =
2475 snd_hda_get_path_idx(codec
, path
);
2478 spec
->imux_pins
[imux
->num_items
] = pin
;
2479 snd_hda_add_imux_item(imux
, label
,
2480 imux
->num_items
, NULL
);
2489 * create playback/capture controls for input pins
2491 static int create_input_ctls(struct hda_codec
*codec
)
2493 struct hda_gen_spec
*spec
= codec
->spec
;
2494 const struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
2495 hda_nid_t mixer
= spec
->mixer_nid
;
2497 int i
, err
, type_idx
= 0;
2498 const char *prev_label
= NULL
;
2501 num_adcs
= fill_adc_nids(codec
);
2505 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
2509 pin
= cfg
->inputs
[i
].pin
;
2510 if (!is_input_pin(codec
, pin
))
2513 label
= hda_get_autocfg_input_label(codec
, cfg
, i
);
2514 if (prev_label
&& !strcmp(label
, prev_label
))
2521 if (cfg
->inputs
[i
].type
== AUTO_PIN_MIC
)
2522 val
|= snd_hda_get_default_vref(codec
, pin
);
2523 set_pin_target(codec
, pin
, val
, false);
2526 if (is_reachable_path(codec
, pin
, mixer
)) {
2527 err
= new_analog_input(codec
, i
, pin
,
2528 label
, type_idx
, mixer
);
2534 err
= parse_capture_source(codec
, pin
, num_adcs
, label
, -mixer
);
2538 if (spec
->add_in_jack_modes
) {
2539 err
= create_in_jack_mode(codec
, pin
);
2545 if (mixer
&& spec
->add_stereo_mix_input
) {
2546 err
= parse_capture_source(codec
, mixer
, num_adcs
,
2560 /* get the input path specified by the given adc and imux indices */
2561 static struct nid_path
*get_input_path(struct hda_codec
*codec
, int adc_idx
, int imux_idx
)
2563 struct hda_gen_spec
*spec
= codec
->spec
;
2564 if (imux_idx
< 0 || imux_idx
>= HDA_MAX_NUM_INPUTS
) {
2568 if (spec
->dyn_adc_switch
)
2569 adc_idx
= spec
->dyn_adc_idx
[imux_idx
];
2570 if (adc_idx
< 0 || adc_idx
>= AUTO_CFG_MAX_OUTS
) {
2574 return snd_hda_get_path_from_idx(codec
, spec
->input_paths
[imux_idx
][adc_idx
]);
2577 static int mux_select(struct hda_codec
*codec
, unsigned int adc_idx
,
2580 static int mux_enum_info(struct snd_kcontrol
*kcontrol
,
2581 struct snd_ctl_elem_info
*uinfo
)
2583 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2584 struct hda_gen_spec
*spec
= codec
->spec
;
2585 return snd_hda_input_mux_info(&spec
->input_mux
, uinfo
);
2588 static int mux_enum_get(struct snd_kcontrol
*kcontrol
,
2589 struct snd_ctl_elem_value
*ucontrol
)
2591 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2592 struct hda_gen_spec
*spec
= codec
->spec
;
2593 unsigned int adc_idx
= kcontrol
->id
.index
;
2595 ucontrol
->value
.enumerated
.item
[0] = spec
->cur_mux
[adc_idx
];
2599 static int mux_enum_put(struct snd_kcontrol
*kcontrol
,
2600 struct snd_ctl_elem_value
*ucontrol
)
2602 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2603 unsigned int adc_idx
= kcontrol
->id
.index
;
2604 return mux_select(codec
, adc_idx
,
2605 ucontrol
->value
.enumerated
.item
[0]);
2608 static const struct snd_kcontrol_new cap_src_temp
= {
2609 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2610 .name
= "Input Source",
2611 .info
= mux_enum_info
,
2612 .get
= mux_enum_get
,
2613 .put
= mux_enum_put
,
2617 * capture volume and capture switch ctls
2620 typedef int (*put_call_t
)(struct snd_kcontrol
*kcontrol
,
2621 struct snd_ctl_elem_value
*ucontrol
);
2623 /* call the given amp update function for all amps in the imux list at once */
2624 static int cap_put_caller(struct snd_kcontrol
*kcontrol
,
2625 struct snd_ctl_elem_value
*ucontrol
,
2626 put_call_t func
, int type
)
2628 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2629 struct hda_gen_spec
*spec
= codec
->spec
;
2630 const struct hda_input_mux
*imux
;
2631 struct nid_path
*path
;
2632 int i
, adc_idx
, err
= 0;
2634 imux
= &spec
->input_mux
;
2635 adc_idx
= kcontrol
->id
.index
;
2636 mutex_lock(&codec
->control_mutex
);
2637 /* we use the cache-only update at first since multiple input paths
2638 * may shared the same amp; by updating only caches, the redundant
2639 * writes to hardware can be reduced.
2641 codec
->cached_write
= 1;
2642 for (i
= 0; i
< imux
->num_items
; i
++) {
2643 path
= get_input_path(codec
, adc_idx
, i
);
2644 if (!path
|| !path
->ctls
[type
])
2646 kcontrol
->private_value
= path
->ctls
[type
];
2647 err
= func(kcontrol
, ucontrol
);
2652 codec
->cached_write
= 0;
2653 mutex_unlock(&codec
->control_mutex
);
2654 snd_hda_codec_flush_amp_cache(codec
); /* flush the updates */
2655 if (err
>= 0 && spec
->cap_sync_hook
)
2656 spec
->cap_sync_hook(codec
);
2660 /* capture volume ctl callbacks */
2661 #define cap_vol_info snd_hda_mixer_amp_volume_info
2662 #define cap_vol_get snd_hda_mixer_amp_volume_get
2663 #define cap_vol_tlv snd_hda_mixer_amp_tlv
2665 static int cap_vol_put(struct snd_kcontrol
*kcontrol
,
2666 struct snd_ctl_elem_value
*ucontrol
)
2668 return cap_put_caller(kcontrol
, ucontrol
,
2669 snd_hda_mixer_amp_volume_put
,
2673 static const struct snd_kcontrol_new cap_vol_temp
= {
2674 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2675 .name
= "Capture Volume",
2676 .access
= (SNDRV_CTL_ELEM_ACCESS_READWRITE
|
2677 SNDRV_CTL_ELEM_ACCESS_TLV_READ
|
2678 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK
),
2679 .info
= cap_vol_info
,
2682 .tlv
= { .c
= cap_vol_tlv
},
2685 /* capture switch ctl callbacks */
2686 #define cap_sw_info snd_ctl_boolean_stereo_info
2687 #define cap_sw_get snd_hda_mixer_amp_switch_get
2689 static int cap_sw_put(struct snd_kcontrol
*kcontrol
,
2690 struct snd_ctl_elem_value
*ucontrol
)
2692 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
2693 struct hda_gen_spec
*spec
= codec
->spec
;
2696 ret
= cap_put_caller(kcontrol
, ucontrol
,
2697 snd_hda_mixer_amp_switch_put
,
2702 if (spec
->capture_switch_hook
) {
2703 bool enable
= (ucontrol
->value
.integer
.value
[0] ||
2704 ucontrol
->value
.integer
.value
[1]);
2705 spec
->capture_switch_hook(codec
, enable
);
2711 static const struct snd_kcontrol_new cap_sw_temp
= {
2712 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
2713 .name
= "Capture Switch",
2714 .info
= cap_sw_info
,
2719 static int parse_capvol_in_path(struct hda_codec
*codec
, struct nid_path
*path
)
2724 path
->ctls
[NID_PATH_VOL_CTL
] = path
->ctls
[NID_PATH_MUTE_CTL
] = 0;
2725 for (depth
= 0; depth
< 3; depth
++) {
2726 if (depth
>= path
->depth
)
2728 i
= path
->depth
- depth
- 1;
2729 nid
= path
->path
[i
];
2730 if (!path
->ctls
[NID_PATH_VOL_CTL
]) {
2731 if (nid_has_volume(codec
, nid
, HDA_OUTPUT
))
2732 path
->ctls
[NID_PATH_VOL_CTL
] =
2733 HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
2734 else if (nid_has_volume(codec
, nid
, HDA_INPUT
)) {
2735 int idx
= path
->idx
[i
];
2736 if (!depth
&& codec
->single_adc_amp
)
2738 path
->ctls
[NID_PATH_VOL_CTL
] =
2739 HDA_COMPOSE_AMP_VAL(nid
, 3, idx
, HDA_INPUT
);
2742 if (!path
->ctls
[NID_PATH_MUTE_CTL
]) {
2743 if (nid_has_mute(codec
, nid
, HDA_OUTPUT
))
2744 path
->ctls
[NID_PATH_MUTE_CTL
] =
2745 HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_OUTPUT
);
2746 else if (nid_has_mute(codec
, nid
, HDA_INPUT
)) {
2747 int idx
= path
->idx
[i
];
2748 if (!depth
&& codec
->single_adc_amp
)
2750 path
->ctls
[NID_PATH_MUTE_CTL
] =
2751 HDA_COMPOSE_AMP_VAL(nid
, 3, idx
, HDA_INPUT
);
2758 static bool is_inv_dmic_pin(struct hda_codec
*codec
, hda_nid_t nid
)
2760 struct hda_gen_spec
*spec
= codec
->spec
;
2761 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
2765 if (!spec
->inv_dmic_split
)
2767 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
2768 if (cfg
->inputs
[i
].pin
!= nid
)
2770 if (cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
2772 val
= snd_hda_codec_get_pincfg(codec
, nid
);
2773 return snd_hda_get_input_pin_attr(val
) == INPUT_PIN_ATTR_INT
;
2778 static int add_single_cap_ctl(struct hda_codec
*codec
, const char *label
,
2779 int idx
, bool is_switch
, unsigned int ctl
,
2782 struct hda_gen_spec
*spec
= codec
->spec
;
2784 int type
= is_switch
? HDA_CTL_WIDGET_MUTE
: HDA_CTL_WIDGET_VOL
;
2785 const char *sfx
= is_switch
? "Switch" : "Volume";
2786 unsigned int chs
= inv_dmic
? 1 : 3;
2793 snprintf(tmpname
, sizeof(tmpname
),
2794 "%s Capture %s", label
, sfx
);
2796 snprintf(tmpname
, sizeof(tmpname
),
2798 err
= add_control(spec
, type
, tmpname
, idx
,
2799 amp_val_replace_channels(ctl
, chs
));
2800 if (err
< 0 || !inv_dmic
)
2803 /* Make independent right kcontrol */
2805 snprintf(tmpname
, sizeof(tmpname
),
2806 "Inverted %s Capture %s", label
, sfx
);
2808 snprintf(tmpname
, sizeof(tmpname
),
2809 "Inverted Capture %s", sfx
);
2810 return add_control(spec
, type
, tmpname
, idx
,
2811 amp_val_replace_channels(ctl
, 2));
2814 /* create single (and simple) capture volume and switch controls */
2815 static int create_single_cap_vol_ctl(struct hda_codec
*codec
, int idx
,
2816 unsigned int vol_ctl
, unsigned int sw_ctl
,
2820 err
= add_single_cap_ctl(codec
, NULL
, idx
, false, vol_ctl
, inv_dmic
);
2823 err
= add_single_cap_ctl(codec
, NULL
, idx
, true, sw_ctl
, inv_dmic
);
2829 /* create bound capture volume and switch controls */
2830 static int create_bind_cap_vol_ctl(struct hda_codec
*codec
, int idx
,
2831 unsigned int vol_ctl
, unsigned int sw_ctl
)
2833 struct hda_gen_spec
*spec
= codec
->spec
;
2834 struct snd_kcontrol_new
*knew
;
2837 knew
= snd_hda_gen_add_kctl(spec
, NULL
, &cap_vol_temp
);
2841 knew
->private_value
= vol_ctl
;
2842 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
2845 knew
= snd_hda_gen_add_kctl(spec
, NULL
, &cap_sw_temp
);
2849 knew
->private_value
= sw_ctl
;
2850 knew
->subdevice
= HDA_SUBDEV_AMP_FLAG
;
2855 /* return the vol ctl when used first in the imux list */
2856 static unsigned int get_first_cap_ctl(struct hda_codec
*codec
, int idx
, int type
)
2858 struct nid_path
*path
;
2862 path
= get_input_path(codec
, 0, idx
);
2865 ctl
= path
->ctls
[type
];
2868 for (i
= 0; i
< idx
- 1; i
++) {
2869 path
= get_input_path(codec
, 0, i
);
2870 if (path
&& path
->ctls
[type
] == ctl
)
2876 /* create individual capture volume and switch controls per input */
2877 static int create_multi_cap_vol_ctl(struct hda_codec
*codec
)
2879 struct hda_gen_spec
*spec
= codec
->spec
;
2880 struct hda_input_mux
*imux
= &spec
->input_mux
;
2881 int i
, err
, type
, type_idx
= 0;
2882 const char *prev_label
= NULL
;
2884 for (i
= 0; i
< imux
->num_items
; i
++) {
2887 label
= hda_get_autocfg_input_label(codec
, &spec
->autocfg
, i
);
2888 if (prev_label
&& !strcmp(label
, prev_label
))
2893 inv_dmic
= is_inv_dmic_pin(codec
, spec
->imux_pins
[i
]);
2895 for (type
= 0; type
< 2; type
++) {
2896 err
= add_single_cap_ctl(codec
, label
, type_idx
, type
,
2897 get_first_cap_ctl(codec
, i
, type
),
2906 static int create_capture_mixers(struct hda_codec
*codec
)
2908 struct hda_gen_spec
*spec
= codec
->spec
;
2909 struct hda_input_mux
*imux
= &spec
->input_mux
;
2910 int i
, n
, nums
, err
;
2912 if (spec
->dyn_adc_switch
)
2915 nums
= spec
->num_adc_nids
;
2917 if (!spec
->auto_mic
&& imux
->num_items
> 1) {
2918 struct snd_kcontrol_new
*knew
;
2920 name
= nums
> 1 ? "Input Source" : "Capture Source";
2921 knew
= snd_hda_gen_add_kctl(spec
, name
, &cap_src_temp
);
2927 for (n
= 0; n
< nums
; n
++) {
2929 bool multi_cap_vol
= spec
->multi_cap_vol
;
2930 bool inv_dmic
= false;
2934 for (i
= 0; i
< imux
->num_items
; i
++) {
2935 struct nid_path
*path
;
2936 path
= get_input_path(codec
, n
, i
);
2939 parse_capvol_in_path(codec
, path
);
2941 vol
= path
->ctls
[NID_PATH_VOL_CTL
];
2942 else if (vol
!= path
->ctls
[NID_PATH_VOL_CTL
]) {
2944 if (!same_amp_caps(codec
, vol
,
2945 path
->ctls
[NID_PATH_VOL_CTL
], HDA_INPUT
))
2946 multi_cap_vol
= true;
2949 sw
= path
->ctls
[NID_PATH_MUTE_CTL
];
2950 else if (sw
!= path
->ctls
[NID_PATH_MUTE_CTL
]) {
2952 if (!same_amp_caps(codec
, sw
,
2953 path
->ctls
[NID_PATH_MUTE_CTL
], HDA_INPUT
))
2954 multi_cap_vol
= true;
2956 if (is_inv_dmic_pin(codec
, spec
->imux_pins
[i
]))
2961 err
= create_single_cap_vol_ctl(codec
, n
, vol
, sw
,
2963 else if (!multi_cap_vol
)
2964 err
= create_bind_cap_vol_ctl(codec
, n
, vol
, sw
);
2966 err
= create_multi_cap_vol_ctl(codec
);
2975 * add mic boosts if needed
2977 static int parse_mic_boost(struct hda_codec
*codec
)
2979 struct hda_gen_spec
*spec
= codec
->spec
;
2980 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
2984 const char *prev_label
= NULL
;
2986 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
2987 if (cfg
->inputs
[i
].type
> AUTO_PIN_MIC
)
2989 nid
= cfg
->inputs
[i
].pin
;
2990 if (get_wcaps(codec
, nid
) & AC_WCAP_IN_AMP
) {
2992 char boost_label
[44];
2993 struct nid_path
*path
;
2996 if (!nid_has_volume(codec
, nid
, HDA_INPUT
))
2999 label
= hda_get_autocfg_input_label(codec
, cfg
, i
);
3000 if (prev_label
&& !strcmp(label
, prev_label
))
3006 snprintf(boost_label
, sizeof(boost_label
),
3007 "%s Boost Volume", label
);
3008 val
= HDA_COMPOSE_AMP_VAL(nid
, 3, 0, HDA_INPUT
);
3009 err
= add_control(spec
, HDA_CTL_WIDGET_VOL
,
3010 boost_label
, type_idx
, val
);
3014 path
= snd_hda_get_nid_path(codec
, nid
, 0);
3016 path
->ctls
[NID_PATH_BOOST_CTL
] = val
;
3023 * parse digital I/Os and set up NIDs in BIOS auto-parse mode
3025 static void parse_digital(struct hda_codec
*codec
)
3027 struct hda_gen_spec
*spec
= codec
->spec
;
3028 struct nid_path
*path
;
3030 hda_nid_t dig_nid
, pin
;
3032 /* support multiple SPDIFs; the secondary is set up as a slave */
3034 for (i
= 0; i
< spec
->autocfg
.dig_outs
; i
++) {
3035 pin
= spec
->autocfg
.dig_out_pins
[i
];
3036 dig_nid
= look_for_dac(codec
, pin
, true);
3039 path
= snd_hda_add_new_path(codec
, dig_nid
, pin
, 0);
3042 print_nid_path("digout", path
);
3043 path
->active
= true;
3044 spec
->digout_paths
[i
] = snd_hda_get_path_idx(codec
, path
);
3045 set_pin_target(codec
, pin
, PIN_OUT
, false);
3047 spec
->multiout
.dig_out_nid
= dig_nid
;
3048 spec
->dig_out_type
= spec
->autocfg
.dig_out_type
[0];
3050 spec
->multiout
.slave_dig_outs
= spec
->slave_dig_outs
;
3051 if (nums
>= ARRAY_SIZE(spec
->slave_dig_outs
) - 1)
3053 spec
->slave_dig_outs
[nums
- 1] = dig_nid
;
3058 if (spec
->autocfg
.dig_in_pin
) {
3059 pin
= spec
->autocfg
.dig_in_pin
;
3060 dig_nid
= codec
->start_nid
;
3061 for (i
= 0; i
< codec
->num_nodes
; i
++, dig_nid
++) {
3062 unsigned int wcaps
= get_wcaps(codec
, dig_nid
);
3063 if (get_wcaps_type(wcaps
) != AC_WID_AUD_IN
)
3065 if (!(wcaps
& AC_WCAP_DIGITAL
))
3067 path
= snd_hda_add_new_path(codec
, pin
, dig_nid
, 0);
3069 print_nid_path("digin", path
);
3070 path
->active
= true;
3071 spec
->dig_in_nid
= dig_nid
;
3072 spec
->digin_path
= snd_hda_get_path_idx(codec
, path
);
3073 set_pin_target(codec
, pin
, PIN_IN
, false);
3082 * input MUX handling
3085 static bool dyn_adc_pcm_resetup(struct hda_codec
*codec
, int cur
);
3087 /* select the given imux item; either unmute exclusively or select the route */
3088 static int mux_select(struct hda_codec
*codec
, unsigned int adc_idx
,
3091 struct hda_gen_spec
*spec
= codec
->spec
;
3092 const struct hda_input_mux
*imux
;
3093 struct nid_path
*path
;
3095 imux
= &spec
->input_mux
;
3096 if (!imux
->num_items
)
3099 if (idx
>= imux
->num_items
)
3100 idx
= imux
->num_items
- 1;
3101 if (spec
->cur_mux
[adc_idx
] == idx
)
3104 path
= get_input_path(codec
, adc_idx
, spec
->cur_mux
[adc_idx
]);
3108 snd_hda_activate_path(codec
, path
, false, false);
3110 spec
->cur_mux
[adc_idx
] = idx
;
3112 if (spec
->shared_mic_hp
)
3113 update_shared_mic_hp(codec
, spec
->cur_mux
[adc_idx
]);
3115 if (spec
->dyn_adc_switch
)
3116 dyn_adc_pcm_resetup(codec
, idx
);
3118 path
= get_input_path(codec
, adc_idx
, idx
);
3123 snd_hda_activate_path(codec
, path
, true, false);
3124 if (spec
->cap_sync_hook
)
3125 spec
->cap_sync_hook(codec
);
3131 * Jack detections for HP auto-mute and mic-switch
3134 /* check each pin in the given array; returns true if any of them is plugged */
3135 static bool detect_jacks(struct hda_codec
*codec
, int num_pins
, hda_nid_t
*pins
)
3139 for (i
= 0; i
< num_pins
; i
++) {
3140 hda_nid_t nid
= pins
[i
];
3143 /* don't detect pins retasked as inputs */
3144 if (snd_hda_codec_get_pin_target(codec
, nid
) & AC_PINCTL_IN_EN
)
3146 present
|= snd_hda_jack_detect(codec
, nid
);
3151 /* standard HP/line-out auto-mute helper */
3152 static void do_automute(struct hda_codec
*codec
, int num_pins
, hda_nid_t
*pins
,
3155 struct hda_gen_spec
*spec
= codec
->spec
;
3158 for (i
= 0; i
< num_pins
; i
++) {
3159 hda_nid_t nid
= pins
[i
];
3163 /* don't reset VREF value in case it's controlling
3164 * the amp (see alc861_fixup_asus_amp_vref_0f())
3166 if (spec
->keep_vref_in_automute
)
3167 val
= snd_hda_codec_get_pin_target(codec
, nid
) & ~PIN_HP
;
3171 val
|= snd_hda_codec_get_pin_target(codec
, nid
);
3172 /* here we call update_pin_ctl() so that the pinctl is changed
3173 * without changing the pinctl target value;
3174 * the original target value will be still referred at the
3175 * init / resume again
3177 update_pin_ctl(codec
, nid
, val
);
3178 set_pin_eapd(codec
, nid
, !mute
);
3182 /* Toggle outputs muting */
3183 void snd_hda_gen_update_outputs(struct hda_codec
*codec
)
3185 struct hda_gen_spec
*spec
= codec
->spec
;
3188 /* Control HP pins/amps depending on master_mute state;
3189 * in general, HP pins/amps control should be enabled in all cases,
3190 * but currently set only for master_mute, just to be safe
3192 if (!spec
->shared_mic_hp
) /* don't change HP-pin when shared with mic */
3193 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.hp_pins
),
3194 spec
->autocfg
.hp_pins
, spec
->master_mute
);
3196 if (!spec
->automute_speaker
)
3199 on
= spec
->hp_jack_present
| spec
->line_jack_present
;
3200 on
|= spec
->master_mute
;
3201 spec
->speaker_muted
= on
;
3202 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.speaker_pins
),
3203 spec
->autocfg
.speaker_pins
, on
);
3205 /* toggle line-out mutes if needed, too */
3206 /* if LO is a copy of either HP or Speaker, don't need to handle it */
3207 if (spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.hp_pins
[0] ||
3208 spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.speaker_pins
[0])
3210 if (!spec
->automute_lo
)
3213 on
= spec
->hp_jack_present
;
3214 on
|= spec
->master_mute
;
3215 spec
->line_out_muted
= on
;
3216 do_automute(codec
, ARRAY_SIZE(spec
->autocfg
.line_out_pins
),
3217 spec
->autocfg
.line_out_pins
, on
);
3219 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs
);
3221 static void call_update_outputs(struct hda_codec
*codec
)
3223 struct hda_gen_spec
*spec
= codec
->spec
;
3224 if (spec
->automute_hook
)
3225 spec
->automute_hook(codec
);
3227 snd_hda_gen_update_outputs(codec
);
3230 /* standard HP-automute helper */
3231 void snd_hda_gen_hp_automute(struct hda_codec
*codec
, struct hda_jack_tbl
*jack
)
3233 struct hda_gen_spec
*spec
= codec
->spec
;
3235 spec
->hp_jack_present
=
3236 detect_jacks(codec
, ARRAY_SIZE(spec
->autocfg
.hp_pins
),
3237 spec
->autocfg
.hp_pins
);
3238 if (!spec
->detect_hp
|| (!spec
->automute_speaker
&& !spec
->automute_lo
))
3240 call_update_outputs(codec
);
3242 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute
);
3244 /* standard line-out-automute helper */
3245 void snd_hda_gen_line_automute(struct hda_codec
*codec
, struct hda_jack_tbl
*jack
)
3247 struct hda_gen_spec
*spec
= codec
->spec
;
3249 if (spec
->autocfg
.line_out_type
== AUTO_PIN_SPEAKER_OUT
)
3251 /* check LO jack only when it's different from HP */
3252 if (spec
->autocfg
.line_out_pins
[0] == spec
->autocfg
.hp_pins
[0])
3255 spec
->line_jack_present
=
3256 detect_jacks(codec
, ARRAY_SIZE(spec
->autocfg
.line_out_pins
),
3257 spec
->autocfg
.line_out_pins
);
3258 if (!spec
->automute_speaker
|| !spec
->detect_lo
)
3260 call_update_outputs(codec
);
3262 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute
);
3264 /* standard mic auto-switch helper */
3265 void snd_hda_gen_mic_autoswitch(struct hda_codec
*codec
, struct hda_jack_tbl
*jack
)
3267 struct hda_gen_spec
*spec
= codec
->spec
;
3270 if (!spec
->auto_mic
)
3273 for (i
= spec
->am_num_entries
- 1; i
> 0; i
--) {
3274 hda_nid_t pin
= spec
->am_entry
[i
].pin
;
3275 /* don't detect pins retasked as outputs */
3276 if (snd_hda_codec_get_pin_target(codec
, pin
) & AC_PINCTL_OUT_EN
)
3278 if (snd_hda_jack_detect(codec
, pin
)) {
3279 mux_select(codec
, 0, spec
->am_entry
[i
].idx
);
3283 mux_select(codec
, 0, spec
->am_entry
[0].idx
);
3285 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch
);
3287 /* update jack retasking */
3288 static void update_automute_all(struct hda_codec
*codec
)
3290 struct hda_gen_spec
*spec
= codec
->spec
;
3292 if (spec
->hp_automute_hook
)
3293 spec
->hp_automute_hook(codec
, NULL
);
3295 snd_hda_gen_hp_automute(codec
, NULL
);
3296 if (spec
->line_automute_hook
)
3297 spec
->line_automute_hook(codec
, NULL
);
3299 snd_hda_gen_line_automute(codec
, NULL
);
3300 if (spec
->mic_autoswitch_hook
)
3301 spec
->mic_autoswitch_hook(codec
, NULL
);
3303 snd_hda_gen_mic_autoswitch(codec
, NULL
);
3307 * Auto-Mute mode mixer enum support
3309 static int automute_mode_info(struct snd_kcontrol
*kcontrol
,
3310 struct snd_ctl_elem_info
*uinfo
)
3312 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3313 struct hda_gen_spec
*spec
= codec
->spec
;
3314 static const char * const texts3
[] = {
3315 "Disabled", "Speaker Only", "Line Out+Speaker"
3318 if (spec
->automute_speaker_possible
&& spec
->automute_lo_possible
)
3319 return snd_hda_enum_helper_info(kcontrol
, uinfo
, 3, texts3
);
3320 return snd_hda_enum_bool_helper_info(kcontrol
, uinfo
);
3323 static int automute_mode_get(struct snd_kcontrol
*kcontrol
,
3324 struct snd_ctl_elem_value
*ucontrol
)
3326 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3327 struct hda_gen_spec
*spec
= codec
->spec
;
3328 unsigned int val
= 0;
3329 if (spec
->automute_speaker
)
3331 if (spec
->automute_lo
)
3334 ucontrol
->value
.enumerated
.item
[0] = val
;
3338 static int automute_mode_put(struct snd_kcontrol
*kcontrol
,
3339 struct snd_ctl_elem_value
*ucontrol
)
3341 struct hda_codec
*codec
= snd_kcontrol_chip(kcontrol
);
3342 struct hda_gen_spec
*spec
= codec
->spec
;
3344 switch (ucontrol
->value
.enumerated
.item
[0]) {
3346 if (!spec
->automute_speaker
&& !spec
->automute_lo
)
3348 spec
->automute_speaker
= 0;
3349 spec
->automute_lo
= 0;
3352 if (spec
->automute_speaker_possible
) {
3353 if (!spec
->automute_lo
&& spec
->automute_speaker
)
3355 spec
->automute_speaker
= 1;
3356 spec
->automute_lo
= 0;
3357 } else if (spec
->automute_lo_possible
) {
3358 if (spec
->automute_lo
)
3360 spec
->automute_lo
= 1;
3365 if (!spec
->automute_lo_possible
|| !spec
->automute_speaker_possible
)
3367 if (spec
->automute_speaker
&& spec
->automute_lo
)
3369 spec
->automute_speaker
= 1;
3370 spec
->automute_lo
= 1;
3375 call_update_outputs(codec
);
3379 static const struct snd_kcontrol_new automute_mode_enum
= {
3380 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
3381 .name
= "Auto-Mute Mode",
3382 .info
= automute_mode_info
,
3383 .get
= automute_mode_get
,
3384 .put
= automute_mode_put
,
3387 static int add_automute_mode_enum(struct hda_codec
*codec
)
3389 struct hda_gen_spec
*spec
= codec
->spec
;
3391 if (!snd_hda_gen_add_kctl(spec
, NULL
, &automute_mode_enum
))
3397 * Check the availability of HP/line-out auto-mute;
3398 * Set up appropriately if really supported
3400 static int check_auto_mute_availability(struct hda_codec
*codec
)
3402 struct hda_gen_spec
*spec
= codec
->spec
;
3403 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3407 if (spec
->suppress_auto_mute
)
3410 if (cfg
->hp_pins
[0])
3412 if (cfg
->line_out_pins
[0])
3414 if (cfg
->speaker_pins
[0])
3416 if (present
< 2) /* need two different output types */
3419 if (!cfg
->speaker_pins
[0] &&
3420 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
) {
3421 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
3422 sizeof(cfg
->speaker_pins
));
3423 cfg
->speaker_outs
= cfg
->line_outs
;
3426 if (!cfg
->hp_pins
[0] &&
3427 cfg
->line_out_type
== AUTO_PIN_HP_OUT
) {
3428 memcpy(cfg
->hp_pins
, cfg
->line_out_pins
,
3429 sizeof(cfg
->hp_pins
));
3430 cfg
->hp_outs
= cfg
->line_outs
;
3433 for (i
= 0; i
< cfg
->hp_outs
; i
++) {
3434 hda_nid_t nid
= cfg
->hp_pins
[i
];
3435 if (!is_jack_detectable(codec
, nid
))
3437 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
3439 snd_hda_jack_detect_enable_callback(codec
, nid
, HDA_GEN_HP_EVENT
,
3440 spec
->hp_automute_hook
?
3441 spec
->hp_automute_hook
:
3442 snd_hda_gen_hp_automute
);
3443 spec
->detect_hp
= 1;
3446 if (cfg
->line_out_type
== AUTO_PIN_LINE_OUT
&& cfg
->line_outs
) {
3447 if (cfg
->speaker_outs
)
3448 for (i
= 0; i
< cfg
->line_outs
; i
++) {
3449 hda_nid_t nid
= cfg
->line_out_pins
[i
];
3450 if (!is_jack_detectable(codec
, nid
))
3452 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid
);
3453 snd_hda_jack_detect_enable_callback(codec
, nid
,
3454 HDA_GEN_FRONT_EVENT
,
3455 spec
->line_automute_hook
?
3456 spec
->line_automute_hook
:
3457 snd_hda_gen_line_automute
);
3458 spec
->detect_lo
= 1;
3460 spec
->automute_lo_possible
= spec
->detect_hp
;
3463 spec
->automute_speaker_possible
= cfg
->speaker_outs
&&
3464 (spec
->detect_hp
|| spec
->detect_lo
);
3466 spec
->automute_lo
= spec
->automute_lo_possible
;
3467 spec
->automute_speaker
= spec
->automute_speaker_possible
;
3469 if (spec
->automute_speaker_possible
|| spec
->automute_lo_possible
) {
3470 /* create a control for automute mode */
3471 err
= add_automute_mode_enum(codec
);
3478 /* check whether all auto-mic pins are valid; setup indices if OK */
3479 static bool auto_mic_check_imux(struct hda_codec
*codec
)
3481 struct hda_gen_spec
*spec
= codec
->spec
;
3482 const struct hda_input_mux
*imux
;
3485 imux
= &spec
->input_mux
;
3486 for (i
= 0; i
< spec
->am_num_entries
; i
++) {
3487 spec
->am_entry
[i
].idx
=
3488 find_idx_in_nid_list(spec
->am_entry
[i
].pin
,
3489 spec
->imux_pins
, imux
->num_items
);
3490 if (spec
->am_entry
[i
].idx
< 0)
3491 return false; /* no corresponding imux */
3494 /* we don't need the jack detection for the first pin */
3495 for (i
= 1; i
< spec
->am_num_entries
; i
++)
3496 snd_hda_jack_detect_enable_callback(codec
,
3497 spec
->am_entry
[i
].pin
,
3499 spec
->mic_autoswitch_hook
?
3500 spec
->mic_autoswitch_hook
:
3501 snd_hda_gen_mic_autoswitch
);
3505 static int compare_attr(const void *ap
, const void *bp
)
3507 const struct automic_entry
*a
= ap
;
3508 const struct automic_entry
*b
= bp
;
3509 return (int)(a
->attr
- b
->attr
);
3513 * Check the availability of auto-mic switch;
3514 * Set up if really supported
3516 static int check_auto_mic_availability(struct hda_codec
*codec
)
3518 struct hda_gen_spec
*spec
= codec
->spec
;
3519 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
3523 if (spec
->suppress_auto_mic
)
3528 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
3529 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
3531 attr
= snd_hda_codec_get_pincfg(codec
, nid
);
3532 attr
= snd_hda_get_input_pin_attr(attr
);
3533 if (types
& (1 << attr
))
3534 return 0; /* already occupied */
3536 case INPUT_PIN_ATTR_INT
:
3537 if (cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
3538 return 0; /* invalid type */
3540 case INPUT_PIN_ATTR_UNUSED
:
3541 return 0; /* invalid entry */
3543 if (cfg
->inputs
[i
].type
> AUTO_PIN_LINE_IN
)
3544 return 0; /* invalid type */
3545 if (!spec
->line_in_auto_switch
&&
3546 cfg
->inputs
[i
].type
!= AUTO_PIN_MIC
)
3547 return 0; /* only mic is allowed */
3548 if (!is_jack_detectable(codec
, nid
))
3549 return 0; /* no unsol support */
3552 if (num_pins
>= MAX_AUTO_MIC_PINS
)
3554 types
|= (1 << attr
);
3555 spec
->am_entry
[num_pins
].pin
= nid
;
3556 spec
->am_entry
[num_pins
].attr
= attr
;
3563 spec
->am_num_entries
= num_pins
;
3564 /* sort the am_entry in the order of attr so that the pin with a
3565 * higher attr will be selected when the jack is plugged.
3567 sort(spec
->am_entry
, num_pins
, sizeof(spec
->am_entry
[0]),
3568 compare_attr
, NULL
);
3570 if (!auto_mic_check_imux(codec
))
3574 spec
->num_adc_nids
= 1;
3575 spec
->cur_mux
[0] = spec
->am_entry
[0].idx
;
3576 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
3577 spec
->am_entry
[0].pin
,
3578 spec
->am_entry
[1].pin
,
3579 spec
->am_entry
[2].pin
);
3586 * Parse the given BIOS configuration and set up the hda_gen_spec
3588 * return 1 if successful, 0 if the proper config is not found,
3589 * or a negative error code
3591 int snd_hda_gen_parse_auto_config(struct hda_codec
*codec
,
3592 struct auto_pin_cfg
*cfg
)
3594 struct hda_gen_spec
*spec
= codec
->spec
;
3597 parse_user_hints(codec
);
3599 if (cfg
!= &spec
->autocfg
) {
3600 spec
->autocfg
= *cfg
;
3601 cfg
= &spec
->autocfg
;
3604 fill_all_dac_nids(codec
);
3606 if (!cfg
->line_outs
) {
3607 if (cfg
->dig_outs
|| cfg
->dig_in_pin
) {
3608 spec
->multiout
.max_channels
= 2;
3609 spec
->no_analog
= 1;
3612 return 0; /* can't find valid BIOS pin config */
3615 if (!spec
->no_primary_hp
&&
3616 cfg
->line_out_type
== AUTO_PIN_SPEAKER_OUT
&&
3617 cfg
->line_outs
<= cfg
->hp_outs
) {
3618 /* use HP as primary out */
3619 cfg
->speaker_outs
= cfg
->line_outs
;
3620 memcpy(cfg
->speaker_pins
, cfg
->line_out_pins
,
3621 sizeof(cfg
->speaker_pins
));
3622 cfg
->line_outs
= cfg
->hp_outs
;
3623 memcpy(cfg
->line_out_pins
, cfg
->hp_pins
, sizeof(cfg
->hp_pins
));
3625 memset(cfg
->hp_pins
, 0, sizeof(cfg
->hp_pins
));
3626 cfg
->line_out_type
= AUTO_PIN_HP_OUT
;
3629 err
= parse_output_paths(codec
);
3632 err
= create_multi_channel_mode(codec
);
3635 err
= create_multi_out_ctls(codec
, cfg
);
3638 err
= create_hp_out_ctls(codec
);
3641 err
= create_speaker_out_ctls(codec
);
3644 err
= create_indep_hp_ctls(codec
);
3647 err
= create_loopback_mixing_ctl(codec
);
3650 err
= create_shared_input(codec
);
3653 err
= create_input_ctls(codec
);
3657 spec
->const_channel_count
= spec
->ext_channel_count
;
3658 /* check the multiple speaker and headphone pins */
3659 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
3660 spec
->const_channel_count
= max(spec
->const_channel_count
,
3661 cfg
->speaker_outs
* 2);
3662 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
)
3663 spec
->const_channel_count
= max(spec
->const_channel_count
,
3665 spec
->multiout
.max_channels
= max(spec
->ext_channel_count
,
3666 spec
->const_channel_count
);
3668 err
= check_auto_mute_availability(codec
);
3672 err
= check_dyn_adc_switch(codec
);
3676 if (!spec
->shared_mic_hp
) {
3677 err
= check_auto_mic_availability(codec
);
3682 err
= create_capture_mixers(codec
);
3686 err
= parse_mic_boost(codec
);
3690 if (spec
->add_out_jack_modes
) {
3691 if (cfg
->line_out_type
!= AUTO_PIN_SPEAKER_OUT
) {
3692 err
= create_out_jack_modes(codec
, cfg
->line_outs
,
3693 cfg
->line_out_pins
);
3697 if (cfg
->line_out_type
!= AUTO_PIN_HP_OUT
) {
3698 err
= create_out_jack_modes(codec
, cfg
->hp_outs
,
3706 parse_digital(codec
);
3710 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config
);
3714 * Build control elements
3717 /* slave controls for virtual master */
3718 static const char * const slave_pfxs
[] = {
3719 "Front", "Surround", "Center", "LFE", "Side",
3720 "Headphone", "Speaker", "Mono", "Line Out",
3721 "CLFE", "Bass Speaker", "PCM",
3722 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
3723 "Headphone Front", "Headphone Surround", "Headphone CLFE",
3728 int snd_hda_gen_build_controls(struct hda_codec
*codec
)
3730 struct hda_gen_spec
*spec
= codec
->spec
;
3733 if (spec
->kctls
.used
) {
3734 err
= snd_hda_add_new_ctls(codec
, spec
->kctls
.list
);
3739 if (spec
->multiout
.dig_out_nid
) {
3740 err
= snd_hda_create_dig_out_ctls(codec
,
3741 spec
->multiout
.dig_out_nid
,
3742 spec
->multiout
.dig_out_nid
,
3743 spec
->pcm_rec
[1].pcm_type
);
3746 if (!spec
->no_analog
) {
3747 err
= snd_hda_create_spdif_share_sw(codec
,
3751 spec
->multiout
.share_spdif
= 1;
3754 if (spec
->dig_in_nid
) {
3755 err
= snd_hda_create_spdif_in_ctls(codec
, spec
->dig_in_nid
);
3760 /* if we have no master control, let's create it */
3761 if (!spec
->no_analog
&&
3762 !snd_hda_find_mixer_ctl(codec
, "Master Playback Volume")) {
3763 err
= snd_hda_add_vmaster(codec
, "Master Playback Volume",
3764 spec
->vmaster_tlv
, slave_pfxs
,
3769 if (!spec
->no_analog
&&
3770 !snd_hda_find_mixer_ctl(codec
, "Master Playback Switch")) {
3771 err
= __snd_hda_add_vmaster(codec
, "Master Playback Switch",
3774 true, &spec
->vmaster_mute
.sw_kctl
);
3777 if (spec
->vmaster_mute
.hook
)
3778 snd_hda_add_vmaster_hook(codec
, &spec
->vmaster_mute
,
3779 spec
->vmaster_mute_enum
);
3782 free_kctls(spec
); /* no longer needed */
3784 if (spec
->shared_mic_hp
) {
3786 int nid
= spec
->autocfg
.inputs
[1].pin
;
3787 err
= snd_hda_jack_add_kctl(codec
, nid
, "Headphone Mic", 0);
3790 err
= snd_hda_jack_detect_enable(codec
, nid
, 0);
3795 err
= snd_hda_jack_add_kctls(codec
, &spec
->autocfg
);
3801 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls
);
3808 static void call_pcm_playback_hook(struct hda_pcm_stream
*hinfo
,
3809 struct hda_codec
*codec
,
3810 struct snd_pcm_substream
*substream
,
3813 struct hda_gen_spec
*spec
= codec
->spec
;
3814 if (spec
->pcm_playback_hook
)
3815 spec
->pcm_playback_hook(hinfo
, codec
, substream
, action
);
3818 static void call_pcm_capture_hook(struct hda_pcm_stream
*hinfo
,
3819 struct hda_codec
*codec
,
3820 struct snd_pcm_substream
*substream
,
3823 struct hda_gen_spec
*spec
= codec
->spec
;
3824 if (spec
->pcm_capture_hook
)
3825 spec
->pcm_capture_hook(hinfo
, codec
, substream
, action
);
3829 * Analog playback callbacks
3831 static int playback_pcm_open(struct hda_pcm_stream
*hinfo
,
3832 struct hda_codec
*codec
,
3833 struct snd_pcm_substream
*substream
)
3835 struct hda_gen_spec
*spec
= codec
->spec
;
3838 mutex_lock(&spec
->pcm_mutex
);
3839 err
= snd_hda_multi_out_analog_open(codec
,
3840 &spec
->multiout
, substream
,
3843 spec
->active_streams
|= 1 << STREAM_MULTI_OUT
;
3844 call_pcm_playback_hook(hinfo
, codec
, substream
,
3845 HDA_GEN_PCM_ACT_OPEN
);
3847 mutex_unlock(&spec
->pcm_mutex
);
3851 static int playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
3852 struct hda_codec
*codec
,
3853 unsigned int stream_tag
,
3854 unsigned int format
,
3855 struct snd_pcm_substream
*substream
)
3857 struct hda_gen_spec
*spec
= codec
->spec
;
3860 err
= snd_hda_multi_out_analog_prepare(codec
, &spec
->multiout
,
3861 stream_tag
, format
, substream
);
3863 call_pcm_playback_hook(hinfo
, codec
, substream
,
3864 HDA_GEN_PCM_ACT_PREPARE
);
3868 static int playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
3869 struct hda_codec
*codec
,
3870 struct snd_pcm_substream
*substream
)
3872 struct hda_gen_spec
*spec
= codec
->spec
;
3875 err
= snd_hda_multi_out_analog_cleanup(codec
, &spec
->multiout
);
3877 call_pcm_playback_hook(hinfo
, codec
, substream
,
3878 HDA_GEN_PCM_ACT_CLEANUP
);
3882 static int playback_pcm_close(struct hda_pcm_stream
*hinfo
,
3883 struct hda_codec
*codec
,
3884 struct snd_pcm_substream
*substream
)
3886 struct hda_gen_spec
*spec
= codec
->spec
;
3887 mutex_lock(&spec
->pcm_mutex
);
3888 spec
->active_streams
&= ~(1 << STREAM_MULTI_OUT
);
3889 call_pcm_playback_hook(hinfo
, codec
, substream
,
3890 HDA_GEN_PCM_ACT_CLOSE
);
3891 mutex_unlock(&spec
->pcm_mutex
);
3895 static int capture_pcm_open(struct hda_pcm_stream
*hinfo
,
3896 struct hda_codec
*codec
,
3897 struct snd_pcm_substream
*substream
)
3899 call_pcm_capture_hook(hinfo
, codec
, substream
, HDA_GEN_PCM_ACT_OPEN
);
3903 static int capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
3904 struct hda_codec
*codec
,
3905 unsigned int stream_tag
,
3906 unsigned int format
,
3907 struct snd_pcm_substream
*substream
)
3909 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
3910 call_pcm_capture_hook(hinfo
, codec
, substream
,
3911 HDA_GEN_PCM_ACT_PREPARE
);
3915 static int capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
3916 struct hda_codec
*codec
,
3917 struct snd_pcm_substream
*substream
)
3919 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
3920 call_pcm_capture_hook(hinfo
, codec
, substream
,
3921 HDA_GEN_PCM_ACT_CLEANUP
);
3925 static int capture_pcm_close(struct hda_pcm_stream
*hinfo
,
3926 struct hda_codec
*codec
,
3927 struct snd_pcm_substream
*substream
)
3929 call_pcm_capture_hook(hinfo
, codec
, substream
, HDA_GEN_PCM_ACT_CLOSE
);
3933 static int alt_playback_pcm_open(struct hda_pcm_stream
*hinfo
,
3934 struct hda_codec
*codec
,
3935 struct snd_pcm_substream
*substream
)
3937 struct hda_gen_spec
*spec
= codec
->spec
;
3940 mutex_lock(&spec
->pcm_mutex
);
3941 if (!spec
->indep_hp_enabled
)
3944 spec
->active_streams
|= 1 << STREAM_INDEP_HP
;
3945 call_pcm_playback_hook(hinfo
, codec
, substream
,
3946 HDA_GEN_PCM_ACT_OPEN
);
3947 mutex_unlock(&spec
->pcm_mutex
);
3951 static int alt_playback_pcm_close(struct hda_pcm_stream
*hinfo
,
3952 struct hda_codec
*codec
,
3953 struct snd_pcm_substream
*substream
)
3955 struct hda_gen_spec
*spec
= codec
->spec
;
3956 mutex_lock(&spec
->pcm_mutex
);
3957 spec
->active_streams
&= ~(1 << STREAM_INDEP_HP
);
3958 call_pcm_playback_hook(hinfo
, codec
, substream
,
3959 HDA_GEN_PCM_ACT_CLOSE
);
3960 mutex_unlock(&spec
->pcm_mutex
);
3964 static int alt_playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
3965 struct hda_codec
*codec
,
3966 unsigned int stream_tag
,
3967 unsigned int format
,
3968 struct snd_pcm_substream
*substream
)
3970 snd_hda_codec_setup_stream(codec
, hinfo
->nid
, stream_tag
, 0, format
);
3971 call_pcm_playback_hook(hinfo
, codec
, substream
,
3972 HDA_GEN_PCM_ACT_PREPARE
);
3976 static int alt_playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
3977 struct hda_codec
*codec
,
3978 struct snd_pcm_substream
*substream
)
3980 snd_hda_codec_cleanup_stream(codec
, hinfo
->nid
);
3981 call_pcm_playback_hook(hinfo
, codec
, substream
,
3982 HDA_GEN_PCM_ACT_CLEANUP
);
3989 static int dig_playback_pcm_open(struct hda_pcm_stream
*hinfo
,
3990 struct hda_codec
*codec
,
3991 struct snd_pcm_substream
*substream
)
3993 struct hda_gen_spec
*spec
= codec
->spec
;
3994 return snd_hda_multi_out_dig_open(codec
, &spec
->multiout
);
3997 static int dig_playback_pcm_prepare(struct hda_pcm_stream
*hinfo
,
3998 struct hda_codec
*codec
,
3999 unsigned int stream_tag
,
4000 unsigned int format
,
4001 struct snd_pcm_substream
*substream
)
4003 struct hda_gen_spec
*spec
= codec
->spec
;
4004 return snd_hda_multi_out_dig_prepare(codec
, &spec
->multiout
,
4005 stream_tag
, format
, substream
);
4008 static int dig_playback_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
4009 struct hda_codec
*codec
,
4010 struct snd_pcm_substream
*substream
)
4012 struct hda_gen_spec
*spec
= codec
->spec
;
4013 return snd_hda_multi_out_dig_cleanup(codec
, &spec
->multiout
);
4016 static int dig_playback_pcm_close(struct hda_pcm_stream
*hinfo
,
4017 struct hda_codec
*codec
,
4018 struct snd_pcm_substream
*substream
)
4020 struct hda_gen_spec
*spec
= codec
->spec
;
4021 return snd_hda_multi_out_dig_close(codec
, &spec
->multiout
);
4027 #define alt_capture_pcm_open capture_pcm_open
4028 #define alt_capture_pcm_close capture_pcm_close
4030 static int alt_capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
4031 struct hda_codec
*codec
,
4032 unsigned int stream_tag
,
4033 unsigned int format
,
4034 struct snd_pcm_substream
*substream
)
4036 struct hda_gen_spec
*spec
= codec
->spec
;
4038 snd_hda_codec_setup_stream(codec
, spec
->adc_nids
[substream
->number
+ 1],
4039 stream_tag
, 0, format
);
4040 call_pcm_capture_hook(hinfo
, codec
, substream
,
4041 HDA_GEN_PCM_ACT_PREPARE
);
4045 static int alt_capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
4046 struct hda_codec
*codec
,
4047 struct snd_pcm_substream
*substream
)
4049 struct hda_gen_spec
*spec
= codec
->spec
;
4051 snd_hda_codec_cleanup_stream(codec
,
4052 spec
->adc_nids
[substream
->number
+ 1]);
4053 call_pcm_capture_hook(hinfo
, codec
, substream
,
4054 HDA_GEN_PCM_ACT_CLEANUP
);
4060 static const struct hda_pcm_stream pcm_analog_playback
= {
4064 /* NID is set in build_pcms */
4066 .open
= playback_pcm_open
,
4067 .close
= playback_pcm_close
,
4068 .prepare
= playback_pcm_prepare
,
4069 .cleanup
= playback_pcm_cleanup
4073 static const struct hda_pcm_stream pcm_analog_capture
= {
4077 /* NID is set in build_pcms */
4079 .open
= capture_pcm_open
,
4080 .close
= capture_pcm_close
,
4081 .prepare
= capture_pcm_prepare
,
4082 .cleanup
= capture_pcm_cleanup
4086 static const struct hda_pcm_stream pcm_analog_alt_playback
= {
4090 /* NID is set in build_pcms */
4092 .open
= alt_playback_pcm_open
,
4093 .close
= alt_playback_pcm_close
,
4094 .prepare
= alt_playback_pcm_prepare
,
4095 .cleanup
= alt_playback_pcm_cleanup
4099 static const struct hda_pcm_stream pcm_analog_alt_capture
= {
4100 .substreams
= 2, /* can be overridden */
4103 /* NID is set in build_pcms */
4105 .open
= alt_capture_pcm_open
,
4106 .close
= alt_capture_pcm_close
,
4107 .prepare
= alt_capture_pcm_prepare
,
4108 .cleanup
= alt_capture_pcm_cleanup
4112 static const struct hda_pcm_stream pcm_digital_playback
= {
4116 /* NID is set in build_pcms */
4118 .open
= dig_playback_pcm_open
,
4119 .close
= dig_playback_pcm_close
,
4120 .prepare
= dig_playback_pcm_prepare
,
4121 .cleanup
= dig_playback_pcm_cleanup
4125 static const struct hda_pcm_stream pcm_digital_capture
= {
4129 /* NID is set in build_pcms */
4132 /* Used by build_pcms to flag that a PCM has no playback stream */
4133 static const struct hda_pcm_stream pcm_null_stream
= {
4140 * dynamic changing ADC PCM streams
4142 static bool dyn_adc_pcm_resetup(struct hda_codec
*codec
, int cur
)
4144 struct hda_gen_spec
*spec
= codec
->spec
;
4145 hda_nid_t new_adc
= spec
->adc_nids
[spec
->dyn_adc_idx
[cur
]];
4147 if (spec
->cur_adc
&& spec
->cur_adc
!= new_adc
) {
4148 /* stream is running, let's swap the current ADC */
4149 __snd_hda_codec_cleanup_stream(codec
, spec
->cur_adc
, 1);
4150 spec
->cur_adc
= new_adc
;
4151 snd_hda_codec_setup_stream(codec
, new_adc
,
4152 spec
->cur_adc_stream_tag
, 0,
4153 spec
->cur_adc_format
);
4159 /* analog capture with dynamic dual-adc changes */
4160 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream
*hinfo
,
4161 struct hda_codec
*codec
,
4162 unsigned int stream_tag
,
4163 unsigned int format
,
4164 struct snd_pcm_substream
*substream
)
4166 struct hda_gen_spec
*spec
= codec
->spec
;
4167 spec
->cur_adc
= spec
->adc_nids
[spec
->dyn_adc_idx
[spec
->cur_mux
[0]]];
4168 spec
->cur_adc_stream_tag
= stream_tag
;
4169 spec
->cur_adc_format
= format
;
4170 snd_hda_codec_setup_stream(codec
, spec
->cur_adc
, stream_tag
, 0, format
);
4174 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream
*hinfo
,
4175 struct hda_codec
*codec
,
4176 struct snd_pcm_substream
*substream
)
4178 struct hda_gen_spec
*spec
= codec
->spec
;
4179 snd_hda_codec_cleanup_stream(codec
, spec
->cur_adc
);
4184 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture
= {
4188 .nid
= 0, /* fill later */
4190 .prepare
= dyn_adc_capture_pcm_prepare
,
4191 .cleanup
= dyn_adc_capture_pcm_cleanup
4195 static void fill_pcm_stream_name(char *str
, size_t len
, const char *sfx
,
4196 const char *chip_name
)
4202 strlcpy(str
, chip_name
, len
);
4204 /* drop non-alnum chars after a space */
4205 for (p
= strchr(str
, ' '); p
; p
= strchr(p
+ 1, ' ')) {
4206 if (!isalnum(p
[1])) {
4211 strlcat(str
, sfx
, len
);
4214 /* build PCM streams based on the parsed results */
4215 int snd_hda_gen_build_pcms(struct hda_codec
*codec
)
4217 struct hda_gen_spec
*spec
= codec
->spec
;
4218 struct hda_pcm
*info
= spec
->pcm_rec
;
4219 const struct hda_pcm_stream
*p
;
4220 bool have_multi_adcs
;
4222 codec
->num_pcms
= 1;
4223 codec
->pcm_info
= info
;
4225 if (spec
->no_analog
)
4228 fill_pcm_stream_name(spec
->stream_name_analog
,
4229 sizeof(spec
->stream_name_analog
),
4230 " Analog", codec
->chip_name
);
4231 info
->name
= spec
->stream_name_analog
;
4233 if (spec
->multiout
.num_dacs
> 0) {
4234 p
= spec
->stream_analog_playback
;
4236 p
= &pcm_analog_playback
;
4237 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
] = *p
;
4238 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].nid
= spec
->multiout
.dac_nids
[0];
4239 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].channels_max
=
4240 spec
->multiout
.max_channels
;
4241 if (spec
->autocfg
.line_out_type
== AUTO_PIN_SPEAKER_OUT
&&
4242 spec
->autocfg
.line_outs
== 2)
4243 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].chmap
=
4246 if (spec
->num_adc_nids
) {
4247 p
= spec
->stream_analog_capture
;
4249 if (spec
->dyn_adc_switch
)
4250 p
= &dyn_adc_pcm_analog_capture
;
4252 p
= &pcm_analog_capture
;
4254 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
] = *p
;
4255 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].nid
= spec
->adc_nids
[0];
4259 /* SPDIF for stream index #1 */
4260 if (spec
->multiout
.dig_out_nid
|| spec
->dig_in_nid
) {
4261 fill_pcm_stream_name(spec
->stream_name_digital
,
4262 sizeof(spec
->stream_name_digital
),
4263 " Digital", codec
->chip_name
);
4264 codec
->num_pcms
= 2;
4265 codec
->slave_dig_outs
= spec
->multiout
.slave_dig_outs
;
4266 info
= spec
->pcm_rec
+ 1;
4267 info
->name
= spec
->stream_name_digital
;
4268 if (spec
->dig_out_type
)
4269 info
->pcm_type
= spec
->dig_out_type
;
4271 info
->pcm_type
= HDA_PCM_TYPE_SPDIF
;
4272 if (spec
->multiout
.dig_out_nid
) {
4273 p
= spec
->stream_digital_playback
;
4275 p
= &pcm_digital_playback
;
4276 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
] = *p
;
4277 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].nid
= spec
->multiout
.dig_out_nid
;
4279 if (spec
->dig_in_nid
) {
4280 p
= spec
->stream_digital_capture
;
4282 p
= &pcm_digital_capture
;
4283 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
] = *p
;
4284 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].nid
= spec
->dig_in_nid
;
4288 if (spec
->no_analog
)
4291 /* If the use of more than one ADC is requested for the current
4292 * model, configure a second analog capture-only PCM.
4294 have_multi_adcs
= (spec
->num_adc_nids
> 1) &&
4295 !spec
->dyn_adc_switch
&& !spec
->auto_mic
;
4296 /* Additional Analaog capture for index #2 */
4297 if (spec
->alt_dac_nid
|| have_multi_adcs
) {
4298 codec
->num_pcms
= 3;
4299 info
= spec
->pcm_rec
+ 2;
4300 info
->name
= spec
->stream_name_analog
;
4301 if (spec
->alt_dac_nid
) {
4302 p
= spec
->stream_analog_alt_playback
;
4304 p
= &pcm_analog_alt_playback
;
4305 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
] = *p
;
4306 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].nid
=
4309 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
] =
4311 info
->stream
[SNDRV_PCM_STREAM_PLAYBACK
].nid
= 0;
4313 if (have_multi_adcs
) {
4314 p
= spec
->stream_analog_alt_capture
;
4316 p
= &pcm_analog_alt_capture
;
4317 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
] = *p
;
4318 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].nid
=
4320 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].substreams
=
4321 spec
->num_adc_nids
- 1;
4323 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
] =
4325 info
->stream
[SNDRV_PCM_STREAM_CAPTURE
].nid
= 0;
4331 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms
);
4335 * Standard auto-parser initializations
4338 /* configure the given path as a proper output */
4339 static void set_output_and_unmute(struct hda_codec
*codec
, int path_idx
)
4341 struct nid_path
*path
;
4344 path
= snd_hda_get_path_from_idx(codec
, path_idx
);
4345 if (!path
|| !path
->depth
)
4347 pin
= path
->path
[path
->depth
- 1];
4348 restore_pin_ctl(codec
, pin
);
4349 snd_hda_activate_path(codec
, path
, path
->active
, true);
4350 set_pin_eapd(codec
, pin
, path
->active
);
4353 /* initialize primary output paths */
4354 static void init_multi_out(struct hda_codec
*codec
)
4356 struct hda_gen_spec
*spec
= codec
->spec
;
4359 for (i
= 0; i
< spec
->autocfg
.line_outs
; i
++)
4360 set_output_and_unmute(codec
, spec
->out_paths
[i
]);
4364 static void __init_extra_out(struct hda_codec
*codec
, int num_outs
, int *paths
)
4368 for (i
= 0; i
< num_outs
; i
++)
4369 set_output_and_unmute(codec
, paths
[i
]);
4372 /* initialize hp and speaker paths */
4373 static void init_extra_out(struct hda_codec
*codec
)
4375 struct hda_gen_spec
*spec
= codec
->spec
;
4377 if (spec
->autocfg
.line_out_type
!= AUTO_PIN_HP_OUT
)
4378 __init_extra_out(codec
, spec
->autocfg
.hp_outs
, spec
->hp_paths
);
4379 if (spec
->autocfg
.line_out_type
!= AUTO_PIN_SPEAKER_OUT
)
4380 __init_extra_out(codec
, spec
->autocfg
.speaker_outs
,
4381 spec
->speaker_paths
);
4384 /* initialize multi-io paths */
4385 static void init_multi_io(struct hda_codec
*codec
)
4387 struct hda_gen_spec
*spec
= codec
->spec
;
4390 for (i
= 0; i
< spec
->multi_ios
; i
++) {
4391 hda_nid_t pin
= spec
->multi_io
[i
].pin
;
4392 struct nid_path
*path
;
4393 path
= get_multiio_path(codec
, i
);
4396 if (!spec
->multi_io
[i
].ctl_in
)
4397 spec
->multi_io
[i
].ctl_in
=
4398 snd_hda_codec_get_pin_target(codec
, pin
);
4399 snd_hda_activate_path(codec
, path
, path
->active
, true);
4403 /* set up input pins and loopback paths */
4404 static void init_analog_input(struct hda_codec
*codec
)
4406 struct hda_gen_spec
*spec
= codec
->spec
;
4407 struct auto_pin_cfg
*cfg
= &spec
->autocfg
;
4410 for (i
= 0; i
< cfg
->num_inputs
; i
++) {
4411 hda_nid_t nid
= cfg
->inputs
[i
].pin
;
4412 if (is_input_pin(codec
, nid
))
4413 restore_pin_ctl(codec
, nid
);
4415 /* init loopback inputs */
4416 if (spec
->mixer_nid
) {
4417 struct nid_path
*path
;
4418 path
= snd_hda_get_path_from_idx(codec
, spec
->loopback_paths
[i
]);
4420 snd_hda_activate_path(codec
, path
,
4421 path
->active
, false);
4426 /* initialize ADC paths */
4427 static void init_input_src(struct hda_codec
*codec
)
4429 struct hda_gen_spec
*spec
= codec
->spec
;
4430 struct hda_input_mux
*imux
= &spec
->input_mux
;
4431 struct nid_path
*path
;
4434 if (spec
->dyn_adc_switch
)
4437 nums
= spec
->num_adc_nids
;
4439 for (c
= 0; c
< nums
; c
++) {
4440 for (i
= 0; i
< imux
->num_items
; i
++) {
4441 path
= get_input_path(codec
, c
, i
);
4443 bool active
= path
->active
;
4444 if (i
== spec
->cur_mux
[c
])
4446 snd_hda_activate_path(codec
, path
, active
, false);
4451 if (spec
->shared_mic_hp
)
4452 update_shared_mic_hp(codec
, spec
->cur_mux
[0]);
4454 if (spec
->cap_sync_hook
)
4455 spec
->cap_sync_hook(codec
);
4458 /* set right pin controls for digital I/O */
4459 static void init_digital(struct hda_codec
*codec
)
4461 struct hda_gen_spec
*spec
= codec
->spec
;
4465 for (i
= 0; i
< spec
->autocfg
.dig_outs
; i
++)
4466 set_output_and_unmute(codec
, spec
->digout_paths
[i
]);
4467 pin
= spec
->autocfg
.dig_in_pin
;
4469 struct nid_path
*path
;
4470 restore_pin_ctl(codec
, pin
);
4471 path
= snd_hda_get_path_from_idx(codec
, spec
->digin_path
);
4473 snd_hda_activate_path(codec
, path
, path
->active
, false);
4477 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
4478 * invalid unsol tags by some reason
4480 static void clear_unsol_on_unused_pins(struct hda_codec
*codec
)
4484 for (i
= 0; i
< codec
->init_pins
.used
; i
++) {
4485 struct hda_pincfg
*pin
= snd_array_elem(&codec
->init_pins
, i
);
4486 hda_nid_t nid
= pin
->nid
;
4487 if (is_jack_detectable(codec
, nid
) &&
4488 !snd_hda_jack_tbl_get(codec
, nid
))
4489 snd_hda_codec_update_cache(codec
, nid
, 0,
4490 AC_VERB_SET_UNSOLICITED_ENABLE
, 0);
4495 * initialize the generic spec;
4496 * this can be put as patch_ops.init function
4498 int snd_hda_gen_init(struct hda_codec
*codec
)
4500 struct hda_gen_spec
*spec
= codec
->spec
;
4502 if (spec
->init_hook
)
4503 spec
->init_hook(codec
);
4505 snd_hda_apply_verbs(codec
);
4507 codec
->cached_write
= 1;
4509 init_multi_out(codec
);
4510 init_extra_out(codec
);
4511 init_multi_io(codec
);
4512 init_analog_input(codec
);
4513 init_input_src(codec
);
4514 init_digital(codec
);
4516 clear_unsol_on_unused_pins(codec
);
4518 /* call init functions of standard auto-mute helpers */
4519 update_automute_all(codec
);
4521 snd_hda_codec_flush_amp_cache(codec
);
4522 snd_hda_codec_flush_cmd_cache(codec
);
4524 if (spec
->vmaster_mute
.sw_kctl
&& spec
->vmaster_mute
.hook
)
4525 snd_hda_sync_vmaster_hook(&spec
->vmaster_mute
);
4527 hda_call_check_power_status(codec
, 0x01);
4530 EXPORT_SYMBOL_HDA(snd_hda_gen_init
);
4533 * free the generic spec;
4534 * this can be put as patch_ops.free function
4536 void snd_hda_gen_free(struct hda_codec
*codec
)
4538 snd_hda_gen_spec_free(codec
->spec
);
4542 EXPORT_SYMBOL_HDA(snd_hda_gen_free
);
4546 * check the loopback power save state;
4547 * this can be put as patch_ops.check_power_status function
4549 int snd_hda_gen_check_power_status(struct hda_codec
*codec
, hda_nid_t nid
)
4551 struct hda_gen_spec
*spec
= codec
->spec
;
4552 return snd_hda_check_amp_list_power(codec
, &spec
->loopback
, nid
);
4554 EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status
);
4559 * the generic codec support
4562 static const struct hda_codec_ops generic_patch_ops
= {
4563 .build_controls
= snd_hda_gen_build_controls
,
4564 .build_pcms
= snd_hda_gen_build_pcms
,
4565 .init
= snd_hda_gen_init
,
4566 .free
= snd_hda_gen_free
,
4567 .unsol_event
= snd_hda_jack_unsol_event
,
4569 .check_power_status
= snd_hda_gen_check_power_status
,
4573 int snd_hda_parse_generic_codec(struct hda_codec
*codec
)
4575 struct hda_gen_spec
*spec
;
4578 spec
= kzalloc(sizeof(*spec
), GFP_KERNEL
);
4581 snd_hda_gen_spec_init(spec
);
4584 err
= snd_hda_parse_pin_defcfg(codec
, &spec
->autocfg
, NULL
, 0);
4588 err
= snd_hda_gen_parse_auto_config(codec
, &spec
->autocfg
);
4592 codec
->patch_ops
= generic_patch_ops
;
4596 snd_hda_gen_free(codec
);
4599 EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec
);