1210284Sjmallett/* SPDX-License-Identifier: GPL-2.0-or-later */
2232812Sjmallett/*
3215990Sjmallett * Generic BIOS auto-parser helper functions for HD-audio
4210284Sjmallett *
5210284Sjmallett * Copyright (c) 2012 Takashi Iwai <tiwai@suse.de>
6215990Sjmallett */
7215990Sjmallett
8215990Sjmallett#ifndef __SOUND_HDA_GENERIC_H
9210284Sjmallett#define __SOUND_HDA_GENERIC_H
10215990Sjmallett
11215990Sjmallett#include <linux/leds.h>
12210284Sjmallett#include "hda_auto_parser.h"
13215990Sjmallett
14215990Sjmallettstruct hda_jack_callback;
15215990Sjmallett
16215990Sjmallett/* table entry for multi-io paths */
17215990Sjmallettstruct hda_multi_io {
18232812Sjmallett	hda_nid_t pin;		/* multi-io widget pin NID */
19215990Sjmallett	hda_nid_t dac;		/* DAC to be connected */
20215990Sjmallett	unsigned int ctl_in;	/* cached input-pin control value */
21215990Sjmallett};
22215990Sjmallett
23215990Sjmallett/* Widget connection path
24215990Sjmallett *
25215990Sjmallett * For output, stored in the order of DAC -> ... -> pin,
26215990Sjmallett * for input, pin -> ... -> ADC.
27215990Sjmallett *
28215990Sjmallett * idx[i] contains the source index number to select on of the widget path[i];
29232812Sjmallett * e.g. idx[1] is the index of the DAC (path[0]) selected by path[1] widget
30215990Sjmallett * multi[] indicates whether it's a selector widget with multi-connectors
31215990Sjmallett * (i.e. the connection selection is mandatory)
32215990Sjmallett * vol_ctl and mute_ctl contains the NIDs for the assigned mixers
33215990Sjmallett */
34215990Sjmallett
35215990Sjmallett#define MAX_NID_PATH_DEPTH	10
36215990Sjmallett
37215990Sjmallettenum {
38210284Sjmallett	NID_PATH_VOL_CTL,
39210284Sjmallett	NID_PATH_MUTE_CTL,
40210284Sjmallett	NID_PATH_BOOST_CTL,
41210284Sjmallett	NID_PATH_NUM_CTLS
42210284Sjmallett};
43210284Sjmallett
44210284Sjmallettstruct nid_path {
45215990Sjmallett	int depth;
46210284Sjmallett	hda_nid_t path[MAX_NID_PATH_DEPTH];
47210284Sjmallett	unsigned char idx[MAX_NID_PATH_DEPTH];
48210284Sjmallett	unsigned char multi[MAX_NID_PATH_DEPTH];
49210284Sjmallett	unsigned int ctls[NID_PATH_NUM_CTLS]; /* NID_PATH_XXX_CTL */
50210284Sjmallett	bool active:1;		/* activated by driver */
51232812Sjmallett	bool pin_enabled:1;	/* pins are enabled */
52210284Sjmallett	bool pin_fixed:1;	/* path with fixed pin */
53210284Sjmallett	bool stream_enabled:1;	/* stream is active */
54210284Sjmallett};
55210284Sjmallett
56210284Sjmallett/* mic/line-in auto switching entry */
57210284Sjmallett
58210284Sjmallett#define MAX_AUTO_MIC_PINS	3
59210284Sjmallett
60210284Sjmallettstruct automic_entry {
61210284Sjmallett	hda_nid_t pin;		/* pin */
62210284Sjmallett	int idx;		/* imux index, -1 = invalid */
63210284Sjmallett	unsigned int attr;	/* pin attribute (INPUT_PIN_ATTR_*) */
64210284Sjmallett};
65210284Sjmallett
66210284Sjmallett/* active stream id */
67210284Sjmallettenum { STREAM_MULTI_OUT, STREAM_INDEP_HP };
68210284Sjmallett
69210284Sjmallett/* PCM hook action */
70210284Sjmallettenum {
71210284Sjmallett	HDA_GEN_PCM_ACT_OPEN,
72210284Sjmallett	HDA_GEN_PCM_ACT_PREPARE,
73210284Sjmallett	HDA_GEN_PCM_ACT_CLEANUP,
74210284Sjmallett	HDA_GEN_PCM_ACT_CLOSE,
75210284Sjmallett};
76210284Sjmallett
77210284Sjmallett/* DAC assignment badness table */
78210284Sjmallettstruct badness_table {
79210284Sjmallett	int no_primary_dac;	/* no primary DAC */
80210284Sjmallett	int no_dac;		/* no secondary DACs */
81210284Sjmallett	int shared_primary;	/* primary DAC is shared with main output */
82210284Sjmallett	int shared_surr;	/* secondary DAC shared with main or primary */
83210284Sjmallett	int shared_clfe;	/* third DAC shared with main or primary */
84210284Sjmallett	int shared_surr_main;	/* secondary DAC sahred with main/DAC0 */
85210284Sjmallett};
86210284Sjmallett
87210284Sjmallettextern const struct badness_table hda_main_out_badness;
88210284Sjmallettextern const struct badness_table hda_extra_out_badness;
89210284Sjmallett
90210284Sjmallettstruct hda_gen_spec {
91210284Sjmallett	char stream_name_analog[32];	/* analog PCM stream */
92210284Sjmallett	const struct hda_pcm_stream *stream_analog_playback;
93210284Sjmallett	const struct hda_pcm_stream *stream_analog_capture;
94210284Sjmallett
95210284Sjmallett	char stream_name_alt_analog[32]; /* alternative analog PCM stream */
96210284Sjmallett	const struct hda_pcm_stream *stream_analog_alt_playback;
97210284Sjmallett	const struct hda_pcm_stream *stream_analog_alt_capture;
98210284Sjmallett
99210284Sjmallett	char stream_name_digital[32];	/* digital PCM stream */
100210284Sjmallett	const struct hda_pcm_stream *stream_digital_playback;
101210284Sjmallett	const struct hda_pcm_stream *stream_digital_capture;
102210284Sjmallett
103210284Sjmallett	/* PCM */
104210284Sjmallett	unsigned int active_streams;
105210284Sjmallett	struct mutex pcm_mutex;
106210284Sjmallett
107210284Sjmallett	/* playback */
108210284Sjmallett	struct hda_multi_out multiout;	/* playback set-up
109210284Sjmallett					 * max_channels, dacs must be set
110210284Sjmallett					 * dig_out_nid and hp_nid are optional
111210284Sjmallett					 */
112210284Sjmallett	hda_nid_t alt_dac_nid;
113210284Sjmallett	hda_nid_t follower_dig_outs[3];	/* optional - for auto-parsing */
114210284Sjmallett	int dig_out_type;
115210284Sjmallett
116210284Sjmallett	/* capture */
117210284Sjmallett	unsigned int num_adc_nids;
118215990Sjmallett	hda_nid_t adc_nids[AUTO_CFG_MAX_INS];
119210284Sjmallett	hda_nid_t dig_in_nid;		/* digital-in NID; optional */
120210284Sjmallett	hda_nid_t mixer_nid;		/* analog-mixer NID */
121210284Sjmallett	hda_nid_t mixer_merge_nid;	/* aamix merge-point NID (optional) */
122210284Sjmallett	const char *input_labels[HDA_MAX_NUM_INPUTS];
123210284Sjmallett	int input_label_idxs[HDA_MAX_NUM_INPUTS];
124210284Sjmallett
125210284Sjmallett	/* capture setup for dynamic dual-adc switch */
126210284Sjmallett	hda_nid_t cur_adc;
127210284Sjmallett	unsigned int cur_adc_stream_tag;
128210284Sjmallett	unsigned int cur_adc_format;
129210284Sjmallett
130210284Sjmallett	/* capture source */
131210284Sjmallett	struct hda_input_mux input_mux;
132210284Sjmallett	unsigned int cur_mux[3];
133210284Sjmallett
134210284Sjmallett	/* channel model */
135210284Sjmallett	/* min_channel_count contains the minimum channel count for primary
136210284Sjmallett	 * outputs.  When multi_ios is set, the channels can be configured
137210284Sjmallett	 * between min_channel_count and (min_channel_count + multi_ios * 2).
138210284Sjmallett	 *
139210284Sjmallett	 * ext_channel_count contains the current channel count of the primary
140210284Sjmallett	 * out.  This varies in the range above.
141210284Sjmallett	 *
142210284Sjmallett	 * Meanwhile, const_channel_count is the channel count for all outputs
143210284Sjmallett	 * including headphone and speakers.  It's a constant value, and the
144210284Sjmallett	 * PCM is set up as max(ext_channel_count, const_channel_count).
145210284Sjmallett	 */
146210284Sjmallett	int min_channel_count;		/* min. channel count for primary out */
147210284Sjmallett	int ext_channel_count;		/* current channel count for primary */
148210284Sjmallett	int const_channel_count;	/* channel count for all */
149210284Sjmallett
150210284Sjmallett	/* PCM information */
151210284Sjmallett	struct hda_pcm *pcm_rec[3];	/* used in build_pcms() */
152210284Sjmallett
153210284Sjmallett	/* dynamic controls, init_verbs and input_mux */
154210284Sjmallett	struct auto_pin_cfg autocfg;
155210284Sjmallett	struct snd_array kctls;
156210284Sjmallett	hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
157210284Sjmallett	hda_nid_t imux_pins[HDA_MAX_NUM_INPUTS];
158210284Sjmallett	unsigned int dyn_adc_idx[HDA_MAX_NUM_INPUTS];
159210284Sjmallett	/* shared hp/mic */
160210284Sjmallett	hda_nid_t shared_mic_vref_pin;
161210284Sjmallett	hda_nid_t hp_mic_pin;
162210284Sjmallett	int hp_mic_mux_idx;
163210284Sjmallett
164210284Sjmallett	/* DAC/ADC lists */
165210284Sjmallett	int num_all_dacs;
166210284Sjmallett	hda_nid_t all_dacs[16];
167210284Sjmallett	int num_all_adcs;
168210284Sjmallett	hda_nid_t all_adcs[AUTO_CFG_MAX_INS];
169210284Sjmallett
170210284Sjmallett	/* path list */
171210284Sjmallett	struct snd_array paths;
172210284Sjmallett
173210284Sjmallett	/* path indices */
174210284Sjmallett	int out_paths[AUTO_CFG_MAX_OUTS];
175210284Sjmallett	int hp_paths[AUTO_CFG_MAX_OUTS];
176210284Sjmallett	int speaker_paths[AUTO_CFG_MAX_OUTS];
177210284Sjmallett	int aamix_out_paths[3];
178210284Sjmallett	int digout_paths[AUTO_CFG_MAX_OUTS];
179210284Sjmallett	int input_paths[HDA_MAX_NUM_INPUTS][AUTO_CFG_MAX_INS];
180210284Sjmallett	int loopback_paths[HDA_MAX_NUM_INPUTS];
181210284Sjmallett	int loopback_merge_path;
182210284Sjmallett	int digin_path;
183210284Sjmallett
184210284Sjmallett	/* auto-mic stuff */
185210284Sjmallett	int am_num_entries;
186210284Sjmallett	struct automic_entry am_entry[MAX_AUTO_MIC_PINS];
187210284Sjmallett
188210284Sjmallett	/* for pin sensing */
189210284Sjmallett	/* current status; set in hda_generic.c */
190210284Sjmallett	unsigned int hp_jack_present:1;
191210284Sjmallett	unsigned int line_jack_present:1;
192210284Sjmallett	unsigned int speaker_muted:1; /* current status of speaker mute */
193210284Sjmallett	unsigned int line_out_muted:1; /* current status of LO mute */
194210284Sjmallett
195210284Sjmallett	/* internal states of automute / autoswitch behavior */
196210284Sjmallett	unsigned int auto_mic:1;
197210284Sjmallett	unsigned int automute_speaker:1; /* automute speaker outputs */
198210284Sjmallett	unsigned int automute_lo:1; /* automute LO outputs */
199210284Sjmallett
200210284Sjmallett	/* capabilities detected by parser */
201210284Sjmallett	unsigned int detect_hp:1;	/* Headphone detection enabled */
202210284Sjmallett	unsigned int detect_lo:1;	/* Line-out detection enabled */
203210284Sjmallett	unsigned int automute_speaker_possible:1; /* there are speakers and either LO or HP */
204210284Sjmallett	unsigned int automute_lo_possible:1;	  /* there are line outs and HP */
205210284Sjmallett
206210284Sjmallett	/* additional parameters set by codec drivers */
207210284Sjmallett	unsigned int master_mute:1;	/* master mute over all */
208210284Sjmallett	unsigned int keep_vref_in_automute:1; /* Don't clear VREF in automute */
209210284Sjmallett	unsigned int line_in_auto_switch:1; /* allow line-in auto switch */
210210284Sjmallett	unsigned int auto_mute_via_amp:1; /* auto-mute via amp instead of pinctl */
211210284Sjmallett
212210284Sjmallett	/* parser behavior flags; set before snd_hda_gen_parse_auto_config() */
213210284Sjmallett	unsigned int suppress_auto_mute:1; /* suppress input jack auto mute */
214210284Sjmallett	unsigned int suppress_auto_mic:1; /* suppress input jack auto switch */
215210284Sjmallett
216210284Sjmallett	/* other parse behavior flags */
217210284Sjmallett	unsigned int need_dac_fix:1; /* need to limit DACs for multi channels */
218210284Sjmallett	unsigned int hp_mic:1; /* Allow HP as a mic-in */
219210284Sjmallett	unsigned int suppress_hp_mic_detect:1; /* Don't detect HP/mic */
220210284Sjmallett	unsigned int no_primary_hp:1; /* Don't prefer HP pins to speaker pins */
221210284Sjmallett	unsigned int no_multi_io:1; /* Don't try multi I/O config */
222210284Sjmallett	unsigned int multi_cap_vol:1; /* allow multiple capture xxx volumes */
223210284Sjmallett	unsigned int inv_dmic_split:1; /* inverted dmic w/a for conexant */
224210284Sjmallett	unsigned int own_eapd_ctl:1; /* set EAPD by own function */
225210284Sjmallett	unsigned int keep_eapd_on:1; /* don't turn off EAPD automatically */
226210284Sjmallett	unsigned int vmaster_mute_led:1; /* add SPK-LED flag to vmaster mute switch */
227210284Sjmallett	unsigned int mic_mute_led:1; /* add MIC-LED flag to capture mute switch */
228210284Sjmallett	unsigned int indep_hp:1; /* independent HP supported */
229210284Sjmallett	unsigned int prefer_hp_amp:1; /* enable HP amp for speaker if any */
230210284Sjmallett	unsigned int add_stereo_mix_input:2; /* add aamix as a capture src */
231210284Sjmallett	unsigned int add_jack_modes:1; /* add i/o jack mode enum ctls */
232210284Sjmallett	unsigned int power_down_unused:1; /* power down unused widgets */
233210284Sjmallett	unsigned int dac_min_mute:1; /* minimal = mute for DACs */
234210284Sjmallett	unsigned int suppress_vmaster:1; /* don't create vmaster kctls */
235210284Sjmallett	unsigned int obey_preferred_dacs:1; /* obey preferred_dacs assignment */
236210284Sjmallett
237210284Sjmallett	/* other internal flags */
238210284Sjmallett	unsigned int no_analog:1; /* digital I/O only */
239210284Sjmallett	unsigned int dyn_adc_switch:1; /* switch ADCs (for ALC275) */
240210284Sjmallett	unsigned int indep_hp_enabled:1; /* independent HP enabled */
241210284Sjmallett	unsigned int have_aamix_ctl:1;
242210284Sjmallett	unsigned int hp_mic_jack_modes:1;
243210284Sjmallett	unsigned int skip_verbs:1; /* don't apply verbs at snd_hda_gen_init() */
244210284Sjmallett
245210284Sjmallett	/* additional mute flags (only effective with auto_mute_via_amp=1) */
246210284Sjmallett	u64 mute_bits;
247210284Sjmallett
248210284Sjmallett	/* bitmask for skipping volume controls */
249210284Sjmallett	u64 out_vol_mask;
250210284Sjmallett
251210284Sjmallett	/* badness tables for output path evaluations */
252210284Sjmallett	const struct badness_table *main_out_badness;
253210284Sjmallett	const struct badness_table *extra_out_badness;
254210284Sjmallett
255210284Sjmallett	/* preferred pin/DAC pairs; an array of paired NIDs */
256210284Sjmallett	const hda_nid_t *preferred_dacs;
257210284Sjmallett
258210284Sjmallett	/* loopback mixing mode */
259210284Sjmallett	bool aamix_mode;
260210284Sjmallett
261210284Sjmallett	/* digital beep */
262210284Sjmallett	hda_nid_t beep_nid;
263210284Sjmallett
264210284Sjmallett	/* for virtual master */
265210284Sjmallett	hda_nid_t vmaster_nid;
266210284Sjmallett	unsigned int vmaster_tlv[4];
267210284Sjmallett	struct hda_vmaster_mute_hook vmaster_mute;
268210284Sjmallett
269210284Sjmallett	struct hda_loopback_check loopback;
270210284Sjmallett	struct snd_array loopback_list;
271210284Sjmallett
272210284Sjmallett	/* multi-io */
273210284Sjmallett	int multi_ios;
274210284Sjmallett	struct hda_multi_io multi_io[4];
275210284Sjmallett
276210284Sjmallett	/* hooks */
277210284Sjmallett	void (*init_hook)(struct hda_codec *codec);
278210284Sjmallett	void (*automute_hook)(struct hda_codec *codec);
279210284Sjmallett	void (*cap_sync_hook)(struct hda_codec *codec,
280210284Sjmallett			      struct snd_kcontrol *kcontrol,
281210284Sjmallett			      struct snd_ctl_elem_value *ucontrol);
282210284Sjmallett
283210284Sjmallett	/* PCM hooks */
284210284Sjmallett	void (*pcm_playback_hook)(struct hda_pcm_stream *hinfo,
285210284Sjmallett				  struct hda_codec *codec,
286210284Sjmallett				  struct snd_pcm_substream *substream,
287210284Sjmallett				  int action);
288210284Sjmallett	void (*pcm_capture_hook)(struct hda_pcm_stream *hinfo,
289210284Sjmallett				 struct hda_codec *codec,
290210284Sjmallett				 struct snd_pcm_substream *substream,
291210284Sjmallett				 int action);
292210284Sjmallett
293210284Sjmallett	/* automute / autoswitch hooks */
294210284Sjmallett	void (*hp_automute_hook)(struct hda_codec *codec,
295210284Sjmallett				 struct hda_jack_callback *cb);
296210284Sjmallett	void (*line_automute_hook)(struct hda_codec *codec,
297210284Sjmallett				   struct hda_jack_callback *cb);
298210284Sjmallett	void (*mic_autoswitch_hook)(struct hda_codec *codec,
299210284Sjmallett				    struct hda_jack_callback *cb);
300210284Sjmallett
301210284Sjmallett	/* leds */
302210284Sjmallett	struct led_classdev *led_cdevs[NUM_AUDIO_LEDS];
303210284Sjmallett};
304210284Sjmallett
305210284Sjmallett/* values for add_stereo_mix_input flag */
306210284Sjmallettenum {
307210284Sjmallett	HDA_HINT_STEREO_MIX_DISABLE,	/* No stereo mix input */
308210284Sjmallett	HDA_HINT_STEREO_MIX_ENABLE,	/* Add stereo mix input */
309210284Sjmallett	HDA_HINT_STEREO_MIX_AUTO,	/* Add only if auto-mic is disabled */
310210284Sjmallett};
311210284Sjmallett
312210284Sjmallettint snd_hda_gen_spec_init(struct hda_gen_spec *spec);
313210284Sjmallett
314210284Sjmallettint snd_hda_gen_init(struct hda_codec *codec);
315210284Sjmallettvoid snd_hda_gen_free(struct hda_codec *codec);
316210284Sjmallett
317210284Sjmallettint snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path);
318210284Sjmallettstruct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx);
319210284Sjmallettstruct nid_path *
320210284Sjmallettsnd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
321210284Sjmallett		     hda_nid_t to_nid, int anchor_nid);
322210284Sjmallettvoid snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
323210284Sjmallett			   bool enable, bool add_aamix);
324210284Sjmallett
325210284Sjmallettstruct snd_kcontrol_new *
326210284Sjmallettsnd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
327210284Sjmallett		     const struct snd_kcontrol_new *temp);
328210284Sjmallett
329210284Sjmallettint snd_hda_gen_parse_auto_config(struct hda_codec *codec,
330210284Sjmallett				  struct auto_pin_cfg *cfg);
331210284Sjmallettint snd_hda_gen_build_controls(struct hda_codec *codec);
332210284Sjmallettint snd_hda_gen_build_pcms(struct hda_codec *codec);
333210284Sjmallett
334210284Sjmallett/* standard jack event callbacks */
335210284Sjmallettvoid snd_hda_gen_hp_automute(struct hda_codec *codec,
336210284Sjmallett			     struct hda_jack_callback *jack);
337210284Sjmallettvoid snd_hda_gen_line_automute(struct hda_codec *codec,
338210284Sjmallett			       struct hda_jack_callback *jack);
339210284Sjmallettvoid snd_hda_gen_mic_autoswitch(struct hda_codec *codec,
340210284Sjmallett				struct hda_jack_callback *jack);
341210284Sjmallettvoid snd_hda_gen_update_outputs(struct hda_codec *codec);
342210284Sjmallett
343210284Sjmallett#ifdef CONFIG_PM
344210284Sjmallettint snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid);
345210284Sjmallett#endif
346210284Sjmallettunsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec,
347210284Sjmallett					   hda_nid_t nid,
348210284Sjmallett					   unsigned int power_state);
349210284Sjmallettvoid snd_hda_gen_stream_pm(struct hda_codec *codec, hda_nid_t nid, bool on);
350210284Sjmallettint snd_hda_gen_fix_pin_power(struct hda_codec *codec, hda_nid_t pin);
351210284Sjmallett
352210284Sjmallettint snd_hda_gen_add_mute_led_cdev(struct hda_codec *codec,
353210284Sjmallett				  int (*callback)(struct led_classdev *,
354210284Sjmallett						  enum led_brightness));
355210284Sjmallettint snd_hda_gen_add_micmute_led_cdev(struct hda_codec *codec,
356210284Sjmallett				     int (*callback)(struct led_classdev *,
357210284Sjmallett						     enum led_brightness));
358210284Sjmallett
359210284Sjmallett#endif /* __SOUND_HDA_GENERIC_H */
360210284Sjmallett